Line data Source code
1 : /* Fold a constant sub-tree into a single node for C-compiler
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it under
7 : the terms of the GNU General Public License as published by the Free
8 : Software Foundation; either version 3, or (at your option) any later
9 : version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /*@@ This file should be rewritten to use an arbitrary precision
21 : @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 : @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 : @@ The routines that translate from the ap rep should
24 : @@ warn if precision et. al. is lost.
25 : @@ This would also make life easier when this technology is used
26 : @@ for cross-compilers. */
27 :
28 : /* The entry points in this file are fold, size_int and size_binop.
29 :
30 : fold takes a tree as argument and returns a simplified tree.
31 :
32 : size_binop takes a tree code for an arithmetic operation
33 : and two operands that are trees, and produces a tree for the
34 : result, assuming the type comes from `sizetype'.
35 :
36 : size_int takes an integer value, and creates a tree constant
37 : with type from `sizetype'.
38 :
39 : Note: Since the folders get called on non-gimple code as well as
40 : gimple code, we need to handle GIMPLE tuples as well as their
41 : corresponding tree equivalents. */
42 :
43 : #define INCLUDE_ALGORITHM
44 : #include "config.h"
45 : #include "system.h"
46 : #include "coretypes.h"
47 : #include "backend.h"
48 : #include "target.h"
49 : #include "rtl.h"
50 : #include "tree.h"
51 : #include "gimple.h"
52 : #include "predict.h"
53 : #include "memmodel.h"
54 : #include "tm_p.h"
55 : #include "tree-ssa-operands.h"
56 : #include "optabs-query.h"
57 : #include "cgraph.h"
58 : #include "diagnostic-core.h"
59 : #include "flags.h"
60 : #include "alias.h"
61 : #include "fold-const.h"
62 : #include "fold-const-call.h"
63 : #include "stor-layout.h"
64 : #include "calls.h"
65 : #include "tree-iterator.h"
66 : #include "expr.h"
67 : #include "intl.h"
68 : #include "langhooks.h"
69 : #include "tree-eh.h"
70 : #include "gimplify.h"
71 : #include "tree-dfa.h"
72 : #include "builtins.h"
73 : #include "generic-match.h"
74 : #include "gimple-iterator.h"
75 : #include "gimple-fold.h"
76 : #include "tree-into-ssa.h"
77 : #include "md5.h"
78 : #include "case-cfn-macros.h"
79 : #include "stringpool.h"
80 : #include "tree-vrp.h"
81 : #include "tree-ssanames.h"
82 : #include "selftest.h"
83 : #include "stringpool.h"
84 : #include "attribs.h"
85 : #include "tree-vector-builder.h"
86 : #include "vec-perm-indices.h"
87 : #include "asan.h"
88 : #include "gimple-range.h"
89 : #include "optabs-tree.h"
90 :
91 : /* Nonzero if we are folding constants inside an initializer or a C++
92 : manifestly-constant-evaluated context; zero otherwise.
93 : Should be used when folding in initializer enables additional
94 : optimizations. */
95 : int folding_initializer = 0;
96 :
97 : /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
98 : otherwise.
99 : Should be used when certain constructs shouldn't be optimized
100 : during folding in that context. */
101 : bool folding_cxx_constexpr = false;
102 :
103 : /* The following constants represent a bit based encoding of GCC's
104 : comparison operators. This encoding simplifies transformations
105 : on relational comparison operators, such as AND and OR. */
106 : enum comparison_code {
107 : COMPCODE_FALSE = 0,
108 : COMPCODE_LT = 1,
109 : COMPCODE_EQ = 2,
110 : COMPCODE_LE = 3,
111 : COMPCODE_GT = 4,
112 : COMPCODE_LTGT = 5,
113 : COMPCODE_GE = 6,
114 : COMPCODE_ORD = 7,
115 : COMPCODE_UNORD = 8,
116 : COMPCODE_UNLT = 9,
117 : COMPCODE_UNEQ = 10,
118 : COMPCODE_UNLE = 11,
119 : COMPCODE_UNGT = 12,
120 : COMPCODE_NE = 13,
121 : COMPCODE_UNGE = 14,
122 : COMPCODE_TRUE = 15
123 : };
124 :
125 : static bool negate_expr_p (tree);
126 : static tree negate_expr (tree);
127 : static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
128 : static enum comparison_code comparison_to_compcode (enum tree_code);
129 : static enum tree_code compcode_to_comparison (enum comparison_code);
130 : static bool twoval_comparison_p (tree, tree *, tree *);
131 : static tree eval_subst (location_t, tree, tree, tree, tree, tree);
132 : static tree optimize_bit_field_compare (location_t, enum tree_code,
133 : tree, tree, tree);
134 : static bool simple_operand_p (const_tree);
135 : static tree range_binop (enum tree_code, tree, tree, int, tree, int);
136 : static tree range_predecessor (tree);
137 : static tree range_successor (tree);
138 : static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
139 : static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
140 : tree, tree, tree, tree);
141 : static tree extract_muldiv (tree, tree, enum tree_code, tree);
142 : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
143 : static tree fold_binary_op_with_conditional_arg (location_t,
144 : enum tree_code, tree,
145 : tree, tree,
146 : tree, tree, int);
147 : static tree fold_negate_const (tree, tree);
148 : static tree fold_not_const (const_tree, tree);
149 : static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 : static tree fold_convert_const (enum tree_code, tree, tree);
151 : static tree fold_view_convert_expr (tree, tree);
152 : static tree fold_negate_expr (location_t, tree);
153 :
154 : /* This is a helper function to detect min/max for some operands of COND_EXPR.
155 : The form is "(exp0 CMP cst1) ? exp0 : cst2". */
156 : tree_code
157 134329 : minmax_from_comparison (tree_code cmp, tree exp0,
158 : const widest_int cst1,
159 : const widest_int cst2)
160 : {
161 134329 : if (cst1 == cst2)
162 : {
163 133 : if (cmp == LE_EXPR || cmp == LT_EXPR)
164 : return MIN_EXPR;
165 114 : if (cmp == GT_EXPR || cmp == GE_EXPR)
166 : return MAX_EXPR;
167 : }
168 134310 : if (cst1 == cst2 - 1)
169 : {
170 : /* X <= Y - 1 equals to X < Y. */
171 80117 : if (cmp == LE_EXPR)
172 : return MIN_EXPR;
173 : /* X > Y - 1 equals to X >= Y. */
174 79688 : if (cmp == GT_EXPR)
175 : return MAX_EXPR;
176 : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
177 67958 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : {
179 18109 : int_range_max r;
180 36218 : get_range_query (cfun)->range_of_expr (r, exp0);
181 18109 : if (r.undefined_p ())
182 0 : r.set_varying (TREE_TYPE (exp0));
183 :
184 18109 : widest_int min = widest_int::from (r.lower_bound (),
185 36218 : TYPE_SIGN (TREE_TYPE (exp0)));
186 18109 : if (min == cst1)
187 705 : return MAX_EXPR;
188 18109 : }
189 : }
190 121446 : if (cst1 == cst2 + 1)
191 : {
192 : /* X < Y + 1 equals to X <= Y. */
193 1137 : if (cmp == LT_EXPR)
194 : return MIN_EXPR;
195 : /* X >= Y + 1 equals to X > Y. */
196 1109 : if (cmp == GE_EXPR)
197 : return MAX_EXPR;
198 : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
199 979 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : {
201 614 : int_range_max r;
202 1228 : get_range_query (cfun)->range_of_expr (r, exp0);
203 614 : if (r.undefined_p ())
204 0 : r.set_varying (TREE_TYPE (exp0));
205 :
206 614 : widest_int max = widest_int::from (r.upper_bound (),
207 1228 : TYPE_SIGN (TREE_TYPE (exp0)));
208 614 : if (max == cst1)
209 140 : return MIN_EXPR;
210 614 : }
211 : }
212 : return ERROR_MARK;
213 : }
214 :
215 :
216 : /* This is a helper function to detect min/max for some operands of COND_EXPR.
217 : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
218 : tree_code
219 165685 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
220 : {
221 165685 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
222 11 : return ERROR_MARK;
223 :
224 165674 : if (!operand_equal_p (exp0, exp2))
225 : return ERROR_MARK;
226 :
227 165674 : if (operand_equal_p (exp1, exp3))
228 : {
229 31614 : if (cmp == LT_EXPR || cmp == LE_EXPR)
230 : return MIN_EXPR;
231 29500 : if (cmp == GT_EXPR || cmp == GE_EXPR)
232 : return MAX_EXPR;
233 : }
234 134174 : if (TREE_CODE (exp3) == INTEGER_CST
235 134088 : && TREE_CODE (exp1) == INTEGER_CST)
236 133630 : return minmax_from_comparison (cmp, exp0, wi::to_widest (exp1), wi::to_widest (exp3));
237 : return ERROR_MARK;
238 : }
239 :
240 : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
241 : Otherwise, return LOC. */
242 :
243 : static location_t
244 2888148 : expr_location_or (tree t, location_t loc)
245 : {
246 900768 : location_t tloc = EXPR_LOCATION (t);
247 2872499 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
248 : }
249 :
250 : /* Similar to protected_set_expr_location, but never modify x in place,
251 : if location can and needs to be set, unshare it. */
252 :
253 : tree
254 7578620 : protected_set_expr_location_unshare (tree x, location_t loc)
255 : {
256 7578620 : if (CAN_HAVE_LOCATION_P (x)
257 6685083 : && EXPR_LOCATION (x) != loc
258 2154656 : && !(TREE_CODE (x) == SAVE_EXPR
259 1077543 : || TREE_CODE (x) == TARGET_EXPR
260 : || TREE_CODE (x) == BIND_EXPR))
261 : {
262 1076789 : x = copy_node (x);
263 1076789 : SET_EXPR_LOCATION (x, loc);
264 : }
265 7578620 : return x;
266 : }
267 :
268 : /* Return true if the built-in mathematical function specified by CODE
269 : is odd, i.e. -f(x) == f(-x). */
270 :
271 : bool
272 2064784 : negate_mathfn_p (combined_fn fn)
273 : {
274 2064784 : switch (fn)
275 : {
276 : CASE_CFN_ASIN:
277 : CASE_CFN_ASIN_FN:
278 : CASE_CFN_ASINH:
279 : CASE_CFN_ASINH_FN:
280 : CASE_CFN_ASINPI:
281 : CASE_CFN_ASINPI_FN:
282 : CASE_CFN_ATAN:
283 : CASE_CFN_ATAN_FN:
284 : CASE_CFN_ATANH:
285 : CASE_CFN_ATANH_FN:
286 : CASE_CFN_ATANPI:
287 : CASE_CFN_ATANPI_FN:
288 : CASE_CFN_CASIN:
289 : CASE_CFN_CASIN_FN:
290 : CASE_CFN_CASINH:
291 : CASE_CFN_CASINH_FN:
292 : CASE_CFN_CATAN:
293 : CASE_CFN_CATAN_FN:
294 : CASE_CFN_CATANH:
295 : CASE_CFN_CATANH_FN:
296 : CASE_CFN_CBRT:
297 : CASE_CFN_CBRT_FN:
298 : CASE_CFN_CPROJ:
299 : CASE_CFN_CPROJ_FN:
300 : CASE_CFN_CSIN:
301 : CASE_CFN_CSIN_FN:
302 : CASE_CFN_CSINH:
303 : CASE_CFN_CSINH_FN:
304 : CASE_CFN_CTAN:
305 : CASE_CFN_CTAN_FN:
306 : CASE_CFN_CTANH:
307 : CASE_CFN_CTANH_FN:
308 : CASE_CFN_ERF:
309 : CASE_CFN_ERF_FN:
310 : CASE_CFN_LLROUND:
311 : CASE_CFN_LLROUND_FN:
312 : CASE_CFN_LROUND:
313 : CASE_CFN_LROUND_FN:
314 : CASE_CFN_ROUND:
315 : CASE_CFN_ROUNDEVEN:
316 : CASE_CFN_ROUNDEVEN_FN:
317 : CASE_CFN_SIN:
318 : CASE_CFN_SIN_FN:
319 : CASE_CFN_SINH:
320 : CASE_CFN_SINH_FN:
321 : CASE_CFN_SINPI:
322 : CASE_CFN_SINPI_FN:
323 : CASE_CFN_TAN:
324 : CASE_CFN_TAN_FN:
325 : CASE_CFN_TANH:
326 : CASE_CFN_TANH_FN:
327 : CASE_CFN_TANPI:
328 : CASE_CFN_TANPI_FN:
329 : CASE_CFN_TRUNC:
330 : CASE_CFN_TRUNC_FN:
331 : return true;
332 :
333 408 : CASE_CFN_LLRINT:
334 408 : CASE_CFN_LLRINT_FN:
335 408 : CASE_CFN_LRINT:
336 408 : CASE_CFN_LRINT_FN:
337 408 : CASE_CFN_NEARBYINT:
338 408 : CASE_CFN_NEARBYINT_FN:
339 408 : CASE_CFN_RINT:
340 408 : CASE_CFN_RINT_FN:
341 408 : return !flag_rounding_math;
342 :
343 2060786 : default:
344 2060786 : break;
345 : }
346 2060786 : return false;
347 : }
348 :
349 : /* Check whether we may negate an integer constant T without causing
350 : overflow. */
351 :
352 : bool
353 3232817 : may_negate_without_overflow_p (const_tree t)
354 : {
355 3232817 : tree type;
356 :
357 3232817 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
358 :
359 3232817 : type = TREE_TYPE (t);
360 3232817 : if (TYPE_UNSIGNED (type))
361 : return false;
362 :
363 3232817 : return !wi::only_sign_bit_p (wi::to_wide (t));
364 : }
365 :
366 : /* Determine whether an expression T can be cheaply negated using
367 : the function negate_expr without introducing undefined overflow. */
368 :
369 : static bool
370 27243833 : negate_expr_p (tree t)
371 : {
372 27403856 : tree type;
373 :
374 27403856 : if (t == 0)
375 : return false;
376 :
377 27403856 : type = TREE_TYPE (t);
378 :
379 27403856 : STRIP_SIGN_NOPS (t);
380 27403856 : switch (TREE_CODE (t))
381 : {
382 1585038 : case INTEGER_CST:
383 1585038 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
384 : return true;
385 :
386 : /* Check that -CST will not overflow type. */
387 386581 : return may_negate_without_overflow_p (t);
388 539 : case BIT_NOT_EXPR:
389 539 : return (INTEGRAL_TYPE_P (type)
390 539 : && TYPE_OVERFLOW_WRAPS (type));
391 :
392 : case FIXED_CST:
393 : return true;
394 :
395 1288 : case NEGATE_EXPR:
396 1288 : return !TYPE_OVERFLOW_SANITIZED (type);
397 :
398 1257666 : case REAL_CST:
399 : /* We want to canonicalize to positive real constants. Pretend
400 : that only negative ones can be easily negated. */
401 1257666 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
402 :
403 454 : case COMPLEX_CST:
404 454 : return negate_expr_p (TREE_REALPART (t))
405 572 : && negate_expr_p (TREE_IMAGPART (t));
406 :
407 120 : case VECTOR_CST:
408 120 : {
409 120 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
410 : return true;
411 :
412 : /* Steps don't prevent negation. */
413 120 : unsigned int count = vector_cst_encoded_nelts (t);
414 240 : for (unsigned int i = 0; i < count; ++i)
415 120 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
416 : return false;
417 :
418 : return true;
419 : }
420 :
421 702 : case COMPLEX_EXPR:
422 702 : return negate_expr_p (TREE_OPERAND (t, 0))
423 702 : && negate_expr_p (TREE_OPERAND (t, 1));
424 :
425 33 : case CONJ_EXPR:
426 33 : return negate_expr_p (TREE_OPERAND (t, 0));
427 :
428 1523550 : case PLUS_EXPR:
429 1523550 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
430 1523544 : || HONOR_SIGNED_ZEROS (type)
431 2768363 : || (ANY_INTEGRAL_TYPE_P (type)
432 1244627 : && ! TYPE_OVERFLOW_WRAPS (type)))
433 740825 : return false;
434 : /* -(A + B) -> (-B) - A. */
435 782725 : if (negate_expr_p (TREE_OPERAND (t, 1)))
436 : return true;
437 : /* -(A + B) -> (-A) - B. */
438 151661 : return negate_expr_p (TREE_OPERAND (t, 0));
439 :
440 253038 : case MINUS_EXPR:
441 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 253038 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
443 253038 : && !HONOR_SIGNED_ZEROS (type)
444 337726 : && (! ANY_INTEGRAL_TYPE_P (type)
445 84465 : || TYPE_OVERFLOW_WRAPS (type));
446 :
447 2372355 : case MULT_EXPR:
448 2372355 : if (TYPE_UNSIGNED (type))
449 : break;
450 : /* INT_MIN/n * n doesn't overflow while negating one operand it does
451 : if n is a (negative) power of two. */
452 4016270 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
453 170278 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
454 2175847 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
455 0 : && (wi::popcount
456 2008135 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
457 167712 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 145693 : && (wi::popcount
459 4139944 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
460 : break;
461 :
462 : /* Fall through. */
463 :
464 2258603 : case RDIV_EXPR:
465 2258603 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
466 2258602 : return negate_expr_p (TREE_OPERAND (t, 1))
467 2258602 : || negate_expr_p (TREE_OPERAND (t, 0));
468 : break;
469 :
470 2709 : case TRUNC_DIV_EXPR:
471 2709 : case ROUND_DIV_EXPR:
472 2709 : case EXACT_DIV_EXPR:
473 2709 : if (TYPE_UNSIGNED (type))
474 : break;
475 : /* In general we can't negate A in A / B, because if A is INT_MIN and
476 : B is not 1 we change the sign of the result. */
477 542 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
478 542 : && negate_expr_p (TREE_OPERAND (t, 0)))
479 : return true;
480 : /* In general we can't negate B in A / B, because if A is INT_MIN and
481 : B is 1, we may turn this into INT_MIN / -1 which is undefined
482 : and actually traps on some architectures. */
483 754 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
484 377 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
485 669 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
486 282 : && ! integer_onep (TREE_OPERAND (t, 1))))
487 367 : return negate_expr_p (TREE_OPERAND (t, 1));
488 : break;
489 :
490 5033864 : case NOP_EXPR:
491 : /* Negate -((double)float) as (double)(-float). */
492 5033864 : if (SCALAR_FLOAT_TYPE_P (type))
493 : {
494 8110 : tree tem = strip_float_extensions (t);
495 8110 : if (tem != t)
496 : return negate_expr_p (tem);
497 : }
498 : break;
499 :
500 1041225 : case CALL_EXPR:
501 : /* Negate -f(x) as f(-x). */
502 1041225 : if (negate_mathfn_p (get_call_combined_fn (t)))
503 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
504 : break;
505 :
506 10656 : case RSHIFT_EXPR:
507 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
508 10656 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 : {
510 10511 : tree op1 = TREE_OPERAND (t, 1);
511 10511 : if (wi::to_wide (op1) == element_precision (type) - 1)
512 : return true;
513 : }
514 : break;
515 :
516 : default:
517 : break;
518 : }
519 : return false;
520 : }
521 :
522 : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
523 : simplification is possible.
524 : If negate_expr_p would return true for T, NULL_TREE will never be
525 : returned. */
526 :
527 : static tree
528 39613694 : fold_negate_expr_1 (location_t loc, tree t)
529 : {
530 39613694 : tree type = TREE_TYPE (t);
531 39613694 : tree tem;
532 :
533 39613694 : switch (TREE_CODE (t))
534 : {
535 : /* Convert - (~A) to A + 1. */
536 138 : case BIT_NOT_EXPR:
537 138 : if (INTEGRAL_TYPE_P (type))
538 138 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
539 138 : build_one_cst (type));
540 : break;
541 :
542 30418148 : case INTEGER_CST:
543 30418148 : tem = fold_negate_const (t, type);
544 30418148 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
545 12669 : || (ANY_INTEGRAL_TYPE_P (type)
546 12669 : && !TYPE_OVERFLOW_TRAPS (type)
547 12669 : && TYPE_OVERFLOW_WRAPS (type))
548 30430033 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
549 : return tem;
550 : break;
551 :
552 1981231 : case POLY_INT_CST:
553 1981231 : case REAL_CST:
554 1981231 : case FIXED_CST:
555 1981231 : tem = fold_negate_const (t, type);
556 1981231 : return tem;
557 :
558 66142 : case COMPLEX_CST:
559 66142 : {
560 66142 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
561 66142 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
562 66142 : if (rpart && ipart)
563 66142 : return build_complex (type, rpart, ipart);
564 : }
565 : break;
566 :
567 49926 : case VECTOR_CST:
568 49926 : {
569 49926 : tree_vector_builder elts;
570 49926 : elts.new_unary_operation (type, t, true);
571 49926 : unsigned int count = elts.encoded_nelts ();
572 122144 : for (unsigned int i = 0; i < count; ++i)
573 : {
574 72218 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 72218 : if (elt == NULL_TREE)
576 0 : return NULL_TREE;
577 72218 : elts.quick_push (elt);
578 : }
579 :
580 49926 : return elts.build ();
581 49926 : }
582 :
583 78 : case COMPLEX_EXPR:
584 78 : if (negate_expr_p (t))
585 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
586 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
587 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
588 : break;
589 :
590 21 : case CONJ_EXPR:
591 21 : if (negate_expr_p (t))
592 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
593 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
594 : break;
595 :
596 1214 : case NEGATE_EXPR:
597 1214 : if (!TYPE_OVERFLOW_SANITIZED (type))
598 1201 : return TREE_OPERAND (t, 0);
599 : break;
600 :
601 696790 : case PLUS_EXPR:
602 696790 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
603 696790 : && !HONOR_SIGNED_ZEROS (type))
604 : {
605 : /* -(A + B) -> (-B) - A. */
606 696680 : if (negate_expr_p (TREE_OPERAND (t, 1)))
607 : {
608 634869 : tem = negate_expr (TREE_OPERAND (t, 1));
609 634869 : return fold_build2_loc (loc, MINUS_EXPR, type,
610 1269738 : tem, TREE_OPERAND (t, 0));
611 : }
612 :
613 : /* -(A + B) -> (-A) - B. */
614 61811 : if (negate_expr_p (TREE_OPERAND (t, 0)))
615 : {
616 1171 : tem = negate_expr (TREE_OPERAND (t, 0));
617 1171 : return fold_build2_loc (loc, MINUS_EXPR, type,
618 2342 : tem, TREE_OPERAND (t, 1));
619 : }
620 : }
621 : break;
622 :
623 153565 : case MINUS_EXPR:
624 : /* - (A - B) -> B - A */
625 153565 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
626 153565 : && !HONOR_SIGNED_ZEROS (type))
627 78860 : return fold_build2_loc (loc, MINUS_EXPR, type,
628 157720 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
629 : break;
630 :
631 261082 : case MULT_EXPR:
632 261082 : if (TYPE_UNSIGNED (type))
633 : break;
634 :
635 : /* Fall through. */
636 :
637 34877 : case RDIV_EXPR:
638 34877 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
639 : {
640 34877 : tem = TREE_OPERAND (t, 1);
641 34877 : if (negate_expr_p (tem))
642 61768 : return fold_build2_loc (loc, TREE_CODE (t), type,
643 61768 : TREE_OPERAND (t, 0), negate_expr (tem));
644 3993 : tem = TREE_OPERAND (t, 0);
645 3993 : if (negate_expr_p (tem))
646 48 : return fold_build2_loc (loc, TREE_CODE (t), type,
647 96 : negate_expr (tem), TREE_OPERAND (t, 1));
648 : }
649 : break;
650 :
651 2039 : case TRUNC_DIV_EXPR:
652 2039 : case ROUND_DIV_EXPR:
653 2039 : case EXACT_DIV_EXPR:
654 2039 : if (TYPE_UNSIGNED (type))
655 : break;
656 : /* In general we can't negate A in A / B, because if A is INT_MIN and
657 : B is not 1 we change the sign of the result. */
658 720 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
659 720 : && negate_expr_p (TREE_OPERAND (t, 0)))
660 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
661 323 : negate_expr (TREE_OPERAND (t, 0)),
662 646 : TREE_OPERAND (t, 1));
663 : /* In general we can't negate B in A / B, because if A is INT_MIN and
664 : B is 1, we may turn this into INT_MIN / -1 which is undefined
665 : and actually traps on some architectures. */
666 794 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
667 397 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
668 313 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
669 290 : && ! integer_onep (TREE_OPERAND (t, 1))))
670 771 : && negate_expr_p (TREE_OPERAND (t, 1)))
671 736 : return fold_build2_loc (loc, TREE_CODE (t), type,
672 368 : TREE_OPERAND (t, 0),
673 736 : negate_expr (TREE_OPERAND (t, 1)));
674 : break;
675 :
676 2496140 : case NOP_EXPR:
677 : /* Convert -((double)float) into (double)(-float). */
678 2496140 : if (SCALAR_FLOAT_TYPE_P (type))
679 : {
680 10564 : tem = strip_float_extensions (t);
681 10564 : if (tem != t && negate_expr_p (tem))
682 0 : return fold_convert_loc (loc, type, negate_expr (tem));
683 : }
684 : break;
685 :
686 286149 : case CALL_EXPR:
687 : /* Negate -f(x) as f(-x). */
688 286149 : if (negate_mathfn_p (get_call_combined_fn (t))
689 287438 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
690 : {
691 1191 : tree fndecl, arg;
692 :
693 1191 : fndecl = get_callee_fndecl (t);
694 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
695 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
696 : }
697 : break;
698 :
699 10432 : case RSHIFT_EXPR:
700 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
701 10432 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
702 : {
703 10414 : tree op1 = TREE_OPERAND (t, 1);
704 10414 : if (wi::to_wide (op1) == element_precision (type) - 1)
705 : {
706 10085 : tree ntype = TYPE_UNSIGNED (type)
707 10085 : ? signed_type_for (type)
708 72 : : unsigned_type_for (type);
709 10085 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
710 10085 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
711 10085 : return fold_convert_loc (loc, type, temp);
712 : }
713 : }
714 : break;
715 :
716 : default:
717 : break;
718 : }
719 :
720 : return NULL_TREE;
721 : }
722 :
723 : /* A wrapper for fold_negate_expr_1. */
724 :
725 : static tree
726 39613694 : fold_negate_expr (location_t loc, tree t)
727 : {
728 39613694 : tree type = TREE_TYPE (t);
729 39613694 : STRIP_SIGN_NOPS (t);
730 39613694 : tree tem = fold_negate_expr_1 (loc, t);
731 39613694 : if (tem == NULL_TREE)
732 : return NULL_TREE;
733 33274514 : return fold_convert_loc (loc, type, tem);
734 : }
735 :
736 : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
737 : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
738 : return NULL_TREE. */
739 :
740 : static tree
741 3839639 : negate_expr (tree t)
742 : {
743 3839639 : tree type, tem;
744 3839639 : location_t loc;
745 :
746 3839639 : if (t == NULL_TREE)
747 : return NULL_TREE;
748 :
749 3839639 : loc = EXPR_LOCATION (t);
750 3839639 : type = TREE_TYPE (t);
751 3839639 : STRIP_SIGN_NOPS (t);
752 :
753 3839639 : tem = fold_negate_expr (loc, t);
754 3839639 : if (!tem)
755 1957623 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
756 3839639 : return fold_convert_loc (loc, type, tem);
757 : }
758 :
759 : /* Split a tree IN into a constant, literal and variable parts that could be
760 : combined with CODE to make IN. "constant" means an expression with
761 : TREE_CONSTANT but that isn't an actual constant. CODE must be a
762 : commutative arithmetic operation. Store the constant part into *CONP,
763 : the literal in *LITP and return the variable part. If a part isn't
764 : present, set it to null. If the tree does not decompose in this way,
765 : return the entire tree as the variable part and the other parts as null.
766 :
767 : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
768 : case, we negate an operand that was subtracted. Except if it is a
769 : literal for which we use *MINUS_LITP instead.
770 :
771 : If NEGATE_P is true, we are negating all of IN, again except a literal
772 : for which we use *MINUS_LITP instead. If a variable part is of pointer
773 : type, it is negated after converting to TYPE. This prevents us from
774 : generating illegal MINUS pointer expression. LOC is the location of
775 : the converted variable part.
776 :
777 : If IN is itself a literal or constant, return it as appropriate.
778 :
779 : Note that we do not guarantee that any of the three values will be the
780 : same type as IN, but they will have the same signedness and mode. */
781 :
782 : static tree
783 231715110 : split_tree (tree in, tree type, enum tree_code code,
784 : tree *minus_varp, tree *conp, tree *minus_conp,
785 : tree *litp, tree *minus_litp, int negate_p)
786 : {
787 231715110 : tree var = 0;
788 231715110 : *minus_varp = 0;
789 231715110 : *conp = 0;
790 231715110 : *minus_conp = 0;
791 231715110 : *litp = 0;
792 231715110 : *minus_litp = 0;
793 :
794 : /* Strip any conversions that don't change the machine mode or signedness. */
795 231715110 : STRIP_SIGN_NOPS (in);
796 :
797 231715110 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
798 147889035 : || TREE_CODE (in) == FIXED_CST)
799 83826075 : *litp = in;
800 147889035 : else if (TREE_CODE (in) == code
801 147889035 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
802 143273226 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
803 : /* We can associate addition and subtraction together (even
804 : though the C standard doesn't say so) for integers because
805 : the value is not affected. For reals, the value might be
806 : affected, so we can't. */
807 143273226 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
808 59539554 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
809 141546400 : || (code == MINUS_EXPR
810 22992578 : && (TREE_CODE (in) == PLUS_EXPR
811 21101871 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
812 : {
813 8680154 : tree op0 = TREE_OPERAND (in, 0);
814 8680154 : tree op1 = TREE_OPERAND (in, 1);
815 8680154 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
816 8680154 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
817 :
818 : /* First see if either of the operands is a literal, then a constant. */
819 8680154 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
820 8460700 : || TREE_CODE (op0) == FIXED_CST)
821 219454 : *litp = op0, op0 = 0;
822 8460700 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
823 5667968 : || TREE_CODE (op1) == FIXED_CST)
824 2792732 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
825 :
826 8680154 : if (op0 != 0 && TREE_CONSTANT (op0))
827 12396 : *conp = op0, op0 = 0;
828 8667758 : else if (op1 != 0 && TREE_CONSTANT (op1))
829 46193 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
830 :
831 : /* If we haven't dealt with either operand, this is not a case we can
832 : decompose. Otherwise, VAR is either of the ones remaining, if any. */
833 8680154 : if (op0 != 0 && op1 != 0)
834 : var = in;
835 3063619 : else if (op0 != 0)
836 : var = op0;
837 : else
838 231850 : var = op1, neg_var_p = neg1_p;
839 :
840 : /* Now do any needed negations. */
841 8680154 : if (neg_litp_p)
842 34221 : *minus_litp = *litp, *litp = 0;
843 8680154 : if (neg_conp_p && *conp)
844 11022 : *minus_conp = *conp, *conp = 0;
845 8680154 : if (neg_var_p && var)
846 224328 : *minus_varp = var, var = 0;
847 : }
848 139208881 : else if (TREE_CONSTANT (in))
849 687295 : *conp = in;
850 138521586 : else if (TREE_CODE (in) == BIT_NOT_EXPR
851 508254 : && code == PLUS_EXPR)
852 : {
853 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
854 : when IN is constant. */
855 361102 : *litp = build_minus_one_cst (type);
856 361102 : *minus_varp = TREE_OPERAND (in, 0);
857 : }
858 : else
859 : var = in;
860 :
861 231715110 : if (negate_p)
862 : {
863 12688087 : if (*litp)
864 1258676 : *minus_litp = *litp, *litp = 0;
865 11429411 : else if (*minus_litp)
866 174 : *litp = *minus_litp, *minus_litp = 0;
867 12688087 : if (*conp)
868 45613 : *minus_conp = *conp, *conp = 0;
869 12642474 : else if (*minus_conp)
870 0 : *conp = *minus_conp, *minus_conp = 0;
871 12688087 : if (var)
872 12626702 : *minus_varp = var, var = 0;
873 61385 : else if (*minus_varp)
874 882 : var = *minus_varp, *minus_varp = 0;
875 : }
876 :
877 231715110 : if (*litp
878 231715110 : && TREE_OVERFLOW_P (*litp))
879 19389 : *litp = drop_tree_overflow (*litp);
880 231715110 : if (*minus_litp
881 231715110 : && TREE_OVERFLOW_P (*minus_litp))
882 24 : *minus_litp = drop_tree_overflow (*minus_litp);
883 :
884 231715110 : return var;
885 : }
886 :
887 : /* Re-associate trees split by the above function. T1 and T2 are
888 : either expressions to associate or null. Return the new
889 : expression, if any. LOC is the location of the new expression. If
890 : we build an operation, do it in TYPE and with CODE. */
891 :
892 : static tree
893 20788661 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
894 : {
895 20788661 : if (t1 == 0)
896 : {
897 13186924 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
898 : return t2;
899 : }
900 7601737 : else if (t2 == 0)
901 : return t1;
902 :
903 : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
904 : try to fold this since we will have infinite recursion. But do
905 : deal with any NEGATE_EXPRs. */
906 4244839 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
907 3364522 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
908 3288344 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
909 : {
910 1663604 : if (code == PLUS_EXPR)
911 : {
912 928827 : if (TREE_CODE (t1) == NEGATE_EXPR)
913 54 : return build2_loc (loc, MINUS_EXPR, type,
914 : fold_convert_loc (loc, type, t2),
915 : fold_convert_loc (loc, type,
916 108 : TREE_OPERAND (t1, 0)));
917 928773 : else if (TREE_CODE (t2) == NEGATE_EXPR)
918 1 : return build2_loc (loc, MINUS_EXPR, type,
919 : fold_convert_loc (loc, type, t1),
920 : fold_convert_loc (loc, type,
921 2 : TREE_OPERAND (t2, 0)));
922 928772 : else if (integer_zerop (t2))
923 39487 : return fold_convert_loc (loc, type, t1);
924 : }
925 734777 : else if (code == MINUS_EXPR)
926 : {
927 707350 : if (integer_zerop (t2))
928 0 : return fold_convert_loc (loc, type, t1);
929 : }
930 :
931 1624062 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 1624062 : fold_convert_loc (loc, type, t2));
933 : }
934 :
935 2581235 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
936 2581235 : fold_convert_loc (loc, type, t2));
937 : }
938 :
939 : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
940 : for use in int_const_binop, size_binop and size_diffop. */
941 :
942 : static bool
943 2569748595 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
944 : {
945 2569748595 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
946 : return false;
947 2569748595 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
948 : return false;
949 :
950 2569748595 : switch (code)
951 : {
952 : case LSHIFT_EXPR:
953 : case RSHIFT_EXPR:
954 : case LROTATE_EXPR:
955 : case RROTATE_EXPR:
956 : return true;
957 :
958 2569748595 : default:
959 2569748595 : break;
960 : }
961 :
962 2569748595 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
963 2569748595 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
964 5139497190 : && TYPE_MODE (type1) == TYPE_MODE (type2);
965 : }
966 :
967 : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
968 : a new constant in RES. Return FALSE if we don't know how to
969 : evaluate CODE at compile-time. */
970 :
971 : bool
972 1439336623 : wide_int_binop (wide_int &res,
973 : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
974 : signop sign, wi::overflow_type *overflow)
975 : {
976 1439336623 : wide_int tmp;
977 1439336623 : *overflow = wi::OVF_NONE;
978 1439336623 : switch (code)
979 : {
980 2844765 : case BIT_IOR_EXPR:
981 2844765 : res = wi::bit_or (arg1, arg2);
982 2844765 : break;
983 :
984 91002 : case BIT_XOR_EXPR:
985 91002 : res = wi::bit_xor (arg1, arg2);
986 91002 : break;
987 :
988 22465576 : case BIT_AND_EXPR:
989 22465576 : res = wi::bit_and (arg1, arg2);
990 22465576 : break;
991 :
992 13570576 : case LSHIFT_EXPR:
993 13570576 : if (wi::neg_p (arg2))
994 : return false;
995 13540252 : res = wi::lshift (arg1, arg2);
996 13540252 : break;
997 :
998 7262399 : case RSHIFT_EXPR:
999 7262399 : if (wi::neg_p (arg2))
1000 : return false;
1001 : /* It's unclear from the C standard whether shifts can overflow.
1002 : The following code ignores overflow; perhaps a C standard
1003 : interpretation ruling is needed. */
1004 7262203 : res = wi::rshift (arg1, arg2, sign);
1005 7262203 : break;
1006 :
1007 1885 : case RROTATE_EXPR:
1008 1885 : case LROTATE_EXPR:
1009 1885 : if (wi::neg_p (arg2))
1010 : {
1011 14 : tmp = -arg2;
1012 14 : if (code == RROTATE_EXPR)
1013 : code = LROTATE_EXPR;
1014 : else
1015 : code = RROTATE_EXPR;
1016 : }
1017 : else
1018 1871 : tmp = arg2;
1019 :
1020 1871 : if (code == RROTATE_EXPR)
1021 1698 : res = wi::rrotate (arg1, tmp);
1022 : else
1023 187 : res = wi::lrotate (arg1, tmp);
1024 : break;
1025 :
1026 243336261 : case PLUS_EXPR:
1027 243336261 : res = wi::add (arg1, arg2, sign, overflow);
1028 243336261 : break;
1029 :
1030 73010452 : case MINUS_EXPR:
1031 73010452 : res = wi::sub (arg1, arg2, sign, overflow);
1032 73010452 : break;
1033 :
1034 407964407 : case MULT_EXPR:
1035 407964407 : res = wi::mul (arg1, arg2, sign, overflow);
1036 407964407 : break;
1037 :
1038 5496 : case MULT_HIGHPART_EXPR:
1039 5496 : res = wi::mul_high (arg1, arg2, sign);
1040 5496 : break;
1041 :
1042 340753195 : case TRUNC_DIV_EXPR:
1043 340753195 : case EXACT_DIV_EXPR:
1044 340753195 : if (arg2 == 0)
1045 : return false;
1046 340747242 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1047 340747242 : break;
1048 :
1049 78266732 : case FLOOR_DIV_EXPR:
1050 78266732 : if (arg2 == 0)
1051 : return false;
1052 78266732 : res = wi::div_floor (arg1, arg2, sign, overflow);
1053 78266732 : break;
1054 :
1055 83239259 : case CEIL_DIV_EXPR:
1056 83239259 : if (arg2 == 0)
1057 : return false;
1058 83239259 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1059 83239259 : break;
1060 :
1061 0 : case ROUND_DIV_EXPR:
1062 0 : if (arg2 == 0)
1063 : return false;
1064 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1065 0 : break;
1066 :
1067 1317644 : case TRUNC_MOD_EXPR:
1068 1317644 : if (arg2 == 0)
1069 : return false;
1070 1316534 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1071 1316534 : break;
1072 :
1073 66712298 : case FLOOR_MOD_EXPR:
1074 66712298 : if (arg2 == 0)
1075 : return false;
1076 66712298 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1077 66712298 : break;
1078 :
1079 178 : case CEIL_MOD_EXPR:
1080 178 : if (arg2 == 0)
1081 : return false;
1082 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1083 178 : break;
1084 :
1085 0 : case ROUND_MOD_EXPR:
1086 0 : if (arg2 == 0)
1087 : return false;
1088 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1089 0 : break;
1090 :
1091 45983 : case MIN_EXPR:
1092 45983 : res = wi::min (arg1, arg2, sign);
1093 45983 : break;
1094 :
1095 98448388 : case MAX_EXPR:
1096 98448388 : res = wi::max (arg1, arg2, sign);
1097 98448388 : break;
1098 :
1099 : default:
1100 : return false;
1101 : }
1102 : return true;
1103 1439336623 : }
1104 :
1105 : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1106 : min value to RES. */
1107 : bool
1108 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1109 : {
1110 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1111 : {
1112 0 : res = wi::to_poly_wide (arg1);
1113 0 : return true;
1114 : }
1115 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1116 : {
1117 0 : res = wi::to_poly_wide (arg2);
1118 0 : return true;
1119 : }
1120 :
1121 : return false;
1122 : }
1123 :
1124 : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1125 : produce a new constant in RES. Return FALSE if we don't know how
1126 : to evaluate CODE at compile-time. */
1127 :
1128 : bool
1129 1439336623 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1130 : const_tree arg1, const_tree arg2,
1131 : signop sign, wi::overflow_type *overflow)
1132 : {
1133 1439336623 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1134 :
1135 1439336623 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1136 : {
1137 1439336623 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1138 1439336623 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1139 1439336623 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1140 : return NULL_TREE;
1141 1439298913 : res = wi_res;
1142 1439298913 : return true;
1143 1439336872 : }
1144 :
1145 : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1146 :
1147 : switch (code)
1148 : {
1149 : case PLUS_EXPR:
1150 : res = wi::add (wi::to_poly_wide (arg1),
1151 : wi::to_poly_wide (arg2), sign, overflow);
1152 : break;
1153 :
1154 : case MINUS_EXPR:
1155 : res = wi::sub (wi::to_poly_wide (arg1),
1156 : wi::to_poly_wide (arg2), sign, overflow);
1157 : break;
1158 :
1159 : case MULT_EXPR:
1160 : if (TREE_CODE (arg2) == INTEGER_CST)
1161 : res = wi::mul (wi::to_poly_wide (arg1),
1162 : wi::to_wide (arg2), sign, overflow);
1163 : else if (TREE_CODE (arg1) == INTEGER_CST)
1164 : res = wi::mul (wi::to_poly_wide (arg2),
1165 : wi::to_wide (arg1), sign, overflow);
1166 : else
1167 : return NULL_TREE;
1168 : break;
1169 :
1170 : case LSHIFT_EXPR:
1171 : if (TREE_CODE (arg2) == INTEGER_CST)
1172 : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1173 : else
1174 : return false;
1175 : break;
1176 :
1177 : case BIT_AND_EXPR:
1178 : if (TREE_CODE (arg2) != INTEGER_CST
1179 : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1180 : &res))
1181 : return false;
1182 : break;
1183 :
1184 : case BIT_IOR_EXPR:
1185 : if (TREE_CODE (arg2) != INTEGER_CST
1186 : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1187 : &res))
1188 : return false;
1189 : break;
1190 :
1191 : case MIN_EXPR:
1192 : if (!can_min_p (arg1, arg2, res))
1193 : return false;
1194 : break;
1195 :
1196 : default:
1197 : return false;
1198 : }
1199 : return true;
1200 : }
1201 :
1202 : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1203 : produce a new constant. Return NULL_TREE if we don't know how to
1204 : evaluate CODE at compile-time. */
1205 :
1206 : tree
1207 1439336623 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1208 : int overflowable)
1209 : {
1210 1439336623 : poly_wide_int poly_res;
1211 1439336623 : tree type = TREE_TYPE (arg1);
1212 1439336623 : signop sign = TYPE_SIGN (type);
1213 1439336623 : wi::overflow_type overflow = wi::OVF_NONE;
1214 :
1215 1439336623 : if (!poly_int_tree_p (arg1)
1216 1439336623 : || !poly_int_tree_p (arg2)
1217 2878673246 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1218 37710 : return NULL_TREE;
1219 1439298913 : return force_fit_type (type, poly_res, overflowable,
1220 1439298913 : (((sign == SIGNED || overflowable == -1)
1221 1439298913 : && overflow)
1222 1439298913 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1223 1439336623 : }
1224 :
1225 : /* Return true if binary operation OP distributes over addition in operand
1226 : OPNO, with the other operand being held constant. OPNO counts from 1. */
1227 :
1228 : static bool
1229 187757 : distributes_over_addition_p (tree_code op, int opno)
1230 : {
1231 0 : switch (op)
1232 : {
1233 : case PLUS_EXPR:
1234 : case MINUS_EXPR:
1235 : case MULT_EXPR:
1236 : return true;
1237 :
1238 0 : case LSHIFT_EXPR:
1239 0 : return opno == 1;
1240 :
1241 3926 : default:
1242 3926 : return false;
1243 : }
1244 : }
1245 :
1246 : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1247 : is the other operand. Try to use the value of OP to simplify the
1248 : operation in one step, without having to process individual elements. */
1249 : static tree
1250 439175 : simplify_const_binop (tree_code code, tree op, tree other_op,
1251 : int index ATTRIBUTE_UNUSED)
1252 : {
1253 : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1254 439175 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1255 : {
1256 358491 : if (integer_zerop (other_op))
1257 : {
1258 27426 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1259 : return op;
1260 26329 : else if (code == BIT_AND_EXPR)
1261 : return other_op;
1262 : }
1263 : }
1264 :
1265 : return NULL_TREE;
1266 : }
1267 :
1268 : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1269 : be an elementwise vector operation, try to fold the operation to a
1270 : constant vector, using ELT_CONST_BINOP to fold each element. Return
1271 : the folded value on success, otherwise return null. */
1272 : tree
1273 262823 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1274 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1275 : {
1276 190463 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1277 444983 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1278 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1279 : {
1280 182160 : tree type = TREE_TYPE (arg1);
1281 182160 : bool step_ok_p;
1282 182160 : if (VECTOR_CST_STEPPED_P (arg1)
1283 182160 : && VECTOR_CST_STEPPED_P (arg2))
1284 : /* We can operate directly on the encoding if:
1285 :
1286 : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1287 : implies
1288 : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1289 :
1290 : Addition and subtraction are the supported operators
1291 : for which this is true. */
1292 2706 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1293 179454 : else if (VECTOR_CST_STEPPED_P (arg1))
1294 : /* We can operate directly on stepped encodings if:
1295 :
1296 : a3 - a2 == a2 - a1
1297 : implies:
1298 : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1299 :
1300 : which is true if (x -> x op c) distributes over addition. */
1301 50304 : step_ok_p = distributes_over_addition_p (code, 1);
1302 : else
1303 : /* Similarly in reverse. */
1304 129150 : step_ok_p = distributes_over_addition_p (code, 2);
1305 182160 : tree_vector_builder elts;
1306 182160 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1307 : return NULL_TREE;
1308 182160 : unsigned int count = elts.encoded_nelts ();
1309 707205 : for (unsigned int i = 0; i < count; ++i)
1310 : {
1311 525364 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1312 525364 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1313 :
1314 525364 : tree elt = elt_const_binop (code, elem1, elem2);
1315 :
1316 : /* It is possible that const_binop cannot handle the given
1317 : code and return NULL_TREE */
1318 525364 : if (elt == NULL_TREE)
1319 319 : return NULL_TREE;
1320 525045 : elts.quick_push (elt);
1321 : }
1322 :
1323 181841 : return elts.build ();
1324 182160 : }
1325 :
1326 80663 : if (TREE_CODE (arg1) == VECTOR_CST
1327 8303 : && TREE_CODE (arg2) == INTEGER_CST)
1328 : {
1329 8303 : tree type = TREE_TYPE (arg1);
1330 8303 : bool step_ok_p = distributes_over_addition_p (code, 1);
1331 8303 : tree_vector_builder elts;
1332 8303 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1333 : return NULL_TREE;
1334 8303 : unsigned int count = elts.encoded_nelts ();
1335 35262 : for (unsigned int i = 0; i < count; ++i)
1336 : {
1337 27046 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1338 :
1339 27046 : tree elt = elt_const_binop (code, elem1, arg2);
1340 :
1341 : /* It is possible that const_binop cannot handle the given
1342 : code and return NULL_TREE. */
1343 27046 : if (elt == NULL_TREE)
1344 87 : return NULL_TREE;
1345 26959 : elts.quick_push (elt);
1346 : }
1347 :
1348 8216 : return elts.build ();
1349 8303 : }
1350 : return NULL_TREE;
1351 : }
1352 :
1353 : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1354 : constant. We assume ARG1 and ARG2 have the same data type, or at least
1355 : are the same kind of constant and the same machine mode. Return zero if
1356 : combining the constants is not allowed in the current operating mode. */
1357 :
1358 : static tree
1359 209212434 : const_binop (enum tree_code code, tree arg1, tree arg2)
1360 : {
1361 : /* Sanity check for the recursive cases. */
1362 209212434 : if (!arg1 || !arg2)
1363 : return NULL_TREE;
1364 :
1365 209211170 : STRIP_NOPS (arg1);
1366 209211170 : STRIP_NOPS (arg2);
1367 :
1368 209211170 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1369 : {
1370 203546101 : if (code == POINTER_PLUS_EXPR)
1371 98565 : return int_const_binop (PLUS_EXPR,
1372 197130 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1373 :
1374 203447536 : return int_const_binop (code, arg1, arg2);
1375 : }
1376 :
1377 5665069 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1378 : {
1379 5385905 : machine_mode mode;
1380 5385905 : REAL_VALUE_TYPE d1;
1381 5385905 : REAL_VALUE_TYPE d2;
1382 5385905 : REAL_VALUE_TYPE value;
1383 5385905 : REAL_VALUE_TYPE result;
1384 5385905 : bool inexact;
1385 5385905 : tree t, type;
1386 :
1387 : /* The following codes are handled by real_arithmetic. */
1388 5385905 : switch (code)
1389 : {
1390 5385905 : case PLUS_EXPR:
1391 5385905 : case MINUS_EXPR:
1392 5385905 : case MULT_EXPR:
1393 5385905 : case RDIV_EXPR:
1394 5385905 : case MIN_EXPR:
1395 5385905 : case MAX_EXPR:
1396 5385905 : break;
1397 :
1398 : default:
1399 : return NULL_TREE;
1400 : }
1401 :
1402 5385905 : d1 = TREE_REAL_CST (arg1);
1403 5385905 : d2 = TREE_REAL_CST (arg2);
1404 :
1405 5385905 : type = TREE_TYPE (arg1);
1406 5385905 : mode = TYPE_MODE (type);
1407 :
1408 : /* Don't perform operation if we honor signaling NaNs and
1409 : either operand is a signaling NaN. */
1410 5385905 : if (HONOR_SNANS (mode)
1411 5385905 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1412 6949 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1413 33 : return NULL_TREE;
1414 :
1415 : /* Don't perform operation if it would raise a division
1416 : by zero exception. */
1417 5385872 : if (code == RDIV_EXPR
1418 2327368 : && real_equal (&d2, &dconst0)
1419 5396321 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1420 7541 : return NULL_TREE;
1421 :
1422 : /* If either operand is a NaN, just return it. Otherwise, set up
1423 : for floating-point trap; we return an overflow. */
1424 5378331 : if (REAL_VALUE_ISNAN (d1))
1425 : {
1426 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1427 : is off. */
1428 250 : d1.signalling = 0;
1429 250 : t = build_real (type, d1);
1430 250 : return t;
1431 : }
1432 5378081 : else if (REAL_VALUE_ISNAN (d2))
1433 : {
1434 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1435 : is off. */
1436 61 : d2.signalling = 0;
1437 61 : t = build_real (type, d2);
1438 61 : return t;
1439 : }
1440 :
1441 5378020 : inexact = real_arithmetic (&value, code, &d1, &d2);
1442 5378020 : real_convert (&result, mode, &value);
1443 :
1444 : /* Don't constant fold this floating point operation if
1445 : both operands are not NaN but the result is NaN, and
1446 : flag_trapping_math. Such operations should raise an
1447 : invalid operation exception. */
1448 5378020 : if (flag_trapping_math
1449 20871102 : && MODE_HAS_NANS (mode)
1450 5359324 : && REAL_VALUE_ISNAN (result)
1451 2459 : && !REAL_VALUE_ISNAN (d1)
1452 5380479 : && !REAL_VALUE_ISNAN (d2))
1453 2459 : return NULL_TREE;
1454 :
1455 : /* Don't constant fold this floating point operation if
1456 : the result has overflowed and flag_trapping_math. */
1457 5375561 : if (flag_trapping_math
1458 20861608 : && MODE_HAS_INFINITIES (mode)
1459 5356865 : && REAL_VALUE_ISINF (result)
1460 7488 : && !REAL_VALUE_ISINF (d1)
1461 5382475 : && !REAL_VALUE_ISINF (d2))
1462 4631 : return NULL_TREE;
1463 :
1464 : /* Don't constant fold this floating point operation if the
1465 : result may dependent upon the run-time rounding mode and
1466 : flag_rounding_math is set, or if GCC's software emulation
1467 : is unable to accurately represent the result. */
1468 5370930 : if ((flag_rounding_math
1469 36450673 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1470 5370930 : && (inexact || !real_identical (&result, &value)))
1471 1107 : return NULL_TREE;
1472 :
1473 5369823 : t = build_real (type, result);
1474 :
1475 5369823 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1476 5369823 : return t;
1477 : }
1478 :
1479 279164 : if (TREE_CODE (arg1) == FIXED_CST)
1480 : {
1481 0 : FIXED_VALUE_TYPE f1;
1482 0 : FIXED_VALUE_TYPE f2;
1483 0 : FIXED_VALUE_TYPE result;
1484 0 : tree t, type;
1485 0 : bool sat_p;
1486 0 : bool overflow_p;
1487 :
1488 : /* The following codes are handled by fixed_arithmetic. */
1489 0 : switch (code)
1490 : {
1491 0 : case PLUS_EXPR:
1492 0 : case MINUS_EXPR:
1493 0 : case MULT_EXPR:
1494 0 : case TRUNC_DIV_EXPR:
1495 0 : if (TREE_CODE (arg2) != FIXED_CST)
1496 : return NULL_TREE;
1497 0 : f2 = TREE_FIXED_CST (arg2);
1498 0 : break;
1499 :
1500 0 : case LSHIFT_EXPR:
1501 0 : case RSHIFT_EXPR:
1502 0 : {
1503 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1504 0 : return NULL_TREE;
1505 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1506 0 : f2.data.high = w2.elt (1);
1507 0 : f2.data.low = w2.ulow ();
1508 0 : f2.mode = SImode;
1509 : }
1510 0 : break;
1511 :
1512 : default:
1513 : return NULL_TREE;
1514 : }
1515 :
1516 0 : f1 = TREE_FIXED_CST (arg1);
1517 0 : type = TREE_TYPE (arg1);
1518 0 : sat_p = TYPE_SATURATING (type);
1519 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1520 0 : t = build_fixed (type, result);
1521 : /* Propagate overflow flags. */
1522 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1523 0 : TREE_OVERFLOW (t) = 1;
1524 0 : return t;
1525 : }
1526 :
1527 279164 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1528 : {
1529 11190 : tree type = TREE_TYPE (arg1);
1530 11190 : tree r1 = TREE_REALPART (arg1);
1531 11190 : tree i1 = TREE_IMAGPART (arg1);
1532 11190 : tree r2 = TREE_REALPART (arg2);
1533 11190 : tree i2 = TREE_IMAGPART (arg2);
1534 11190 : tree real, imag;
1535 :
1536 11190 : switch (code)
1537 : {
1538 5303 : case PLUS_EXPR:
1539 5303 : case MINUS_EXPR:
1540 5303 : real = const_binop (code, r1, r2);
1541 5303 : imag = const_binop (code, i1, i2);
1542 5303 : break;
1543 :
1544 3929 : case MULT_EXPR:
1545 3929 : if (COMPLEX_FLOAT_TYPE_P (type))
1546 2777 : return do_mpc_arg2 (arg1, arg2, type,
1547 : /* do_nonfinite= */ folding_initializer,
1548 2777 : mpc_mul);
1549 :
1550 1152 : real = const_binop (MINUS_EXPR,
1551 : const_binop (MULT_EXPR, r1, r2),
1552 : const_binop (MULT_EXPR, i1, i2));
1553 1152 : imag = const_binop (PLUS_EXPR,
1554 : const_binop (MULT_EXPR, r1, i2),
1555 : const_binop (MULT_EXPR, i1, r2));
1556 1152 : break;
1557 :
1558 1704 : case RDIV_EXPR:
1559 1704 : if (COMPLEX_FLOAT_TYPE_P (type))
1560 1704 : return do_mpc_arg2 (arg1, arg2, type,
1561 : /* do_nonfinite= */ folding_initializer,
1562 1704 : mpc_div);
1563 : /* Fallthru. */
1564 254 : case TRUNC_DIV_EXPR:
1565 254 : case CEIL_DIV_EXPR:
1566 254 : case FLOOR_DIV_EXPR:
1567 254 : case ROUND_DIV_EXPR:
1568 254 : if (flag_complex_method == 0)
1569 : {
1570 : /* Keep this algorithm in sync with
1571 : tree-complex.cc:expand_complex_div_straight().
1572 :
1573 : Expand complex division to scalars, straightforward algorithm.
1574 : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1575 : t = br*br + bi*bi
1576 : */
1577 0 : tree magsquared
1578 0 : = const_binop (PLUS_EXPR,
1579 : const_binop (MULT_EXPR, r2, r2),
1580 : const_binop (MULT_EXPR, i2, i2));
1581 0 : tree t1
1582 0 : = const_binop (PLUS_EXPR,
1583 : const_binop (MULT_EXPR, r1, r2),
1584 : const_binop (MULT_EXPR, i1, i2));
1585 0 : tree t2
1586 0 : = const_binop (MINUS_EXPR,
1587 : const_binop (MULT_EXPR, i1, r2),
1588 : const_binop (MULT_EXPR, r1, i2));
1589 :
1590 0 : real = const_binop (code, t1, magsquared);
1591 0 : imag = const_binop (code, t2, magsquared);
1592 : }
1593 : else
1594 : {
1595 : /* Keep this algorithm in sync with
1596 : tree-complex.cc:expand_complex_div_wide().
1597 :
1598 : Expand complex division to scalars, modified algorithm to minimize
1599 : overflow with wide input ranges. */
1600 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1601 : fold_abs_const (r2, TREE_TYPE (type)),
1602 : fold_abs_const (i2, TREE_TYPE (type)));
1603 :
1604 254 : if (integer_nonzerop (compare))
1605 : {
1606 : /* In the TRUE branch, we compute
1607 : ratio = br/bi;
1608 : div = (br * ratio) + bi;
1609 : tr = (ar * ratio) + ai;
1610 : ti = (ai * ratio) - ar;
1611 : tr = tr / div;
1612 : ti = ti / div; */
1613 48 : tree ratio = const_binop (code, r2, i2);
1614 48 : tree div = const_binop (PLUS_EXPR, i2,
1615 : const_binop (MULT_EXPR, r2, ratio));
1616 48 : real = const_binop (MULT_EXPR, r1, ratio);
1617 48 : real = const_binop (PLUS_EXPR, real, i1);
1618 48 : real = const_binop (code, real, div);
1619 :
1620 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1621 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1622 48 : imag = const_binop (code, imag, div);
1623 : }
1624 : else
1625 : {
1626 : /* In the FALSE branch, we compute
1627 : ratio = d/c;
1628 : divisor = (d * ratio) + c;
1629 : tr = (b * ratio) + a;
1630 : ti = b - (a * ratio);
1631 : tr = tr / div;
1632 : ti = ti / div; */
1633 206 : tree ratio = const_binop (code, i2, r2);
1634 206 : tree div = const_binop (PLUS_EXPR, r2,
1635 : const_binop (MULT_EXPR, i2, ratio));
1636 :
1637 206 : real = const_binop (MULT_EXPR, i1, ratio);
1638 206 : real = const_binop (PLUS_EXPR, real, r1);
1639 206 : real = const_binop (code, real, div);
1640 :
1641 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1642 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1643 206 : imag = const_binop (code, imag, div);
1644 : }
1645 : }
1646 : break;
1647 :
1648 : default:
1649 : return NULL_TREE;
1650 : }
1651 :
1652 6709 : if (real && imag)
1653 6551 : return build_complex (type, real, imag);
1654 : }
1655 :
1656 268132 : tree simplified;
1657 268132 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1658 : return simplified;
1659 :
1660 267620 : if (commutative_tree_code (code)
1661 267620 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1662 : return simplified;
1663 :
1664 262823 : return vector_const_binop (code, arg1, arg2, const_binop);
1665 : }
1666 :
1667 : /* Overload that adds a TYPE parameter to be able to dispatch
1668 : to fold_relational_const. */
1669 :
1670 : tree
1671 276796667 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1672 : {
1673 276796667 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1674 75407894 : return fold_relational_const (code, type, arg1, arg2);
1675 :
1676 : /* ??? Until we make the const_binop worker take the type of the
1677 : result as argument put those cases that need it here. */
1678 201388773 : switch (code)
1679 : {
1680 18 : case VEC_SERIES_EXPR:
1681 18 : if (CONSTANT_CLASS_P (arg1)
1682 18 : && CONSTANT_CLASS_P (arg2))
1683 18 : return build_vec_series (type, arg1, arg2);
1684 : return NULL_TREE;
1685 :
1686 268681 : case COMPLEX_EXPR:
1687 268681 : if ((TREE_CODE (arg1) == REAL_CST
1688 258159 : && TREE_CODE (arg2) == REAL_CST)
1689 10524 : || (TREE_CODE (arg1) == INTEGER_CST
1690 10522 : && TREE_CODE (arg2) == INTEGER_CST))
1691 268679 : return build_complex (type, arg1, arg2);
1692 : return NULL_TREE;
1693 :
1694 96564 : case POINTER_DIFF_EXPR:
1695 96564 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1696 : {
1697 192484 : poly_offset_int res = (wi::to_poly_offset (arg1)
1698 96242 : - wi::to_poly_offset (arg2));
1699 96242 : return force_fit_type (type, res, 1,
1700 96242 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1701 : }
1702 : return NULL_TREE;
1703 :
1704 14849 : case VEC_PACK_TRUNC_EXPR:
1705 14849 : case VEC_PACK_FIX_TRUNC_EXPR:
1706 14849 : case VEC_PACK_FLOAT_EXPR:
1707 14849 : {
1708 14849 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1709 :
1710 14849 : if (TREE_CODE (arg1) != VECTOR_CST
1711 14849 : || TREE_CODE (arg2) != VECTOR_CST)
1712 : return NULL_TREE;
1713 :
1714 14849 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1715 : return NULL_TREE;
1716 :
1717 14849 : out_nelts = in_nelts * 2;
1718 14849 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1719 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1720 :
1721 14849 : tree_vector_builder elts (type, out_nelts, 1);
1722 186133 : for (i = 0; i < out_nelts; i++)
1723 : {
1724 171296 : tree elt = (i < in_nelts
1725 171296 : ? VECTOR_CST_ELT (arg1, i)
1726 85642 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1727 172340 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1728 : ? NOP_EXPR
1729 : : code == VEC_PACK_FLOAT_EXPR
1730 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1731 171296 : TREE_TYPE (type), elt);
1732 171296 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1733 12 : return NULL_TREE;
1734 171284 : elts.quick_push (elt);
1735 : }
1736 :
1737 14837 : return elts.build ();
1738 14849 : }
1739 :
1740 206 : case VEC_WIDEN_MULT_LO_EXPR:
1741 206 : case VEC_WIDEN_MULT_HI_EXPR:
1742 206 : case VEC_WIDEN_MULT_EVEN_EXPR:
1743 206 : case VEC_WIDEN_MULT_ODD_EXPR:
1744 206 : {
1745 206 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1746 :
1747 206 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1748 : return NULL_TREE;
1749 :
1750 206 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1751 : return NULL_TREE;
1752 206 : out_nelts = in_nelts / 2;
1753 206 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1754 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1755 :
1756 206 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1757 : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1758 : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1759 : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1760 : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1761 : scale = 1, ofs = 0;
1762 : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1763 206 : scale = 1, ofs = 1;
1764 :
1765 206 : tree_vector_builder elts (type, out_nelts, 1);
1766 738 : for (out = 0; out < out_nelts; out++)
1767 : {
1768 532 : unsigned int in = (out << scale) + ofs;
1769 532 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1770 : VECTOR_CST_ELT (arg1, in));
1771 532 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1772 : VECTOR_CST_ELT (arg2, in));
1773 :
1774 532 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1775 0 : return NULL_TREE;
1776 532 : tree elt = const_binop (MULT_EXPR, t1, t2);
1777 532 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1778 : return NULL_TREE;
1779 532 : elts.quick_push (elt);
1780 : }
1781 :
1782 206 : return elts.build ();
1783 206 : }
1784 :
1785 201008455 : default:;
1786 : }
1787 :
1788 201008455 : if (TREE_CODE_CLASS (code) != tcc_binary)
1789 : return NULL_TREE;
1790 :
1791 : /* Make sure type and arg0 have the same saturating flag. */
1792 198656006 : gcc_checking_assert (TYPE_SATURATING (type)
1793 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1794 :
1795 198656006 : return const_binop (code, arg1, arg2);
1796 : }
1797 :
1798 : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1799 : Return zero if computing the constants is not possible. */
1800 :
1801 : tree
1802 357549695 : const_unop (enum tree_code code, tree type, tree arg0)
1803 : {
1804 : /* Don't perform the operation, other than NEGATE and ABS, if
1805 : flag_signaling_nans is on and the operand is a signaling NaN. */
1806 357549695 : if (TREE_CODE (arg0) == REAL_CST
1807 10663770 : && HONOR_SNANS (arg0)
1808 17129 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1809 4740 : && code != NEGATE_EXPR
1810 4740 : && code != ABS_EXPR
1811 357554400 : && code != ABSU_EXPR)
1812 : return NULL_TREE;
1813 :
1814 357544990 : switch (code)
1815 : {
1816 267368440 : CASE_CONVERT:
1817 267368440 : case FLOAT_EXPR:
1818 267368440 : case FIX_TRUNC_EXPR:
1819 267368440 : case FIXED_CONVERT_EXPR:
1820 267368440 : return fold_convert_const (code, type, arg0);
1821 :
1822 0 : case ADDR_SPACE_CONVERT_EXPR:
1823 : /* If the source address is 0, and the source address space
1824 : cannot have a valid object at 0, fold to dest type null. */
1825 0 : if (integer_zerop (arg0)
1826 0 : && !(targetm.addr_space.zero_address_valid
1827 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1828 0 : return fold_convert_const (code, type, arg0);
1829 : break;
1830 :
1831 12413694 : case VIEW_CONVERT_EXPR:
1832 12413694 : return fold_view_convert_expr (type, arg0);
1833 :
1834 31327211 : case NEGATE_EXPR:
1835 31327211 : {
1836 : /* Can't call fold_negate_const directly here as that doesn't
1837 : handle all cases and we might not be able to negate some
1838 : constants. */
1839 31327211 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1840 31327211 : if (tem && CONSTANT_CLASS_P (tem))
1841 : return tem;
1842 : break;
1843 : }
1844 :
1845 36724 : case ABS_EXPR:
1846 36724 : case ABSU_EXPR:
1847 36724 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1848 34324 : return fold_abs_const (arg0, type);
1849 : break;
1850 :
1851 25512 : case CONJ_EXPR:
1852 25512 : if (TREE_CODE (arg0) == COMPLEX_CST)
1853 : {
1854 25509 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1855 25509 : TREE_TYPE (type));
1856 25509 : return build_complex (type, TREE_REALPART (arg0), ipart);
1857 : }
1858 : break;
1859 :
1860 2364442 : case BIT_NOT_EXPR:
1861 2364442 : if (TREE_CODE (arg0) == INTEGER_CST)
1862 2361400 : return fold_not_const (arg0, type);
1863 3042 : else if (POLY_INT_CST_P (arg0))
1864 : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1865 : /* Perform BIT_NOT_EXPR on each element individually. */
1866 3042 : else if (TREE_CODE (arg0) == VECTOR_CST)
1867 : {
1868 2418 : tree elem;
1869 :
1870 : /* This can cope with stepped encodings because ~x == -1 - x. */
1871 2418 : tree_vector_builder elements;
1872 2418 : elements.new_unary_operation (type, arg0, true);
1873 2418 : unsigned int i, count = elements.encoded_nelts ();
1874 9092 : for (i = 0; i < count; ++i)
1875 : {
1876 6674 : elem = VECTOR_CST_ELT (arg0, i);
1877 6674 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1878 6674 : if (elem == NULL_TREE)
1879 : break;
1880 6674 : elements.quick_push (elem);
1881 : }
1882 2418 : if (i == count)
1883 2418 : return elements.build ();
1884 2418 : }
1885 : break;
1886 :
1887 10210872 : case TRUTH_NOT_EXPR:
1888 10210872 : if (TREE_CODE (arg0) == INTEGER_CST)
1889 9967175 : return constant_boolean_node (integer_zerop (arg0), type);
1890 : break;
1891 :
1892 179173 : case REALPART_EXPR:
1893 179173 : if (TREE_CODE (arg0) == COMPLEX_CST)
1894 178972 : return fold_convert (type, TREE_REALPART (arg0));
1895 : break;
1896 :
1897 184977 : case IMAGPART_EXPR:
1898 184977 : if (TREE_CODE (arg0) == COMPLEX_CST)
1899 184789 : return fold_convert (type, TREE_IMAGPART (arg0));
1900 : break;
1901 :
1902 19028 : case VEC_UNPACK_LO_EXPR:
1903 19028 : case VEC_UNPACK_HI_EXPR:
1904 19028 : case VEC_UNPACK_FLOAT_LO_EXPR:
1905 19028 : case VEC_UNPACK_FLOAT_HI_EXPR:
1906 19028 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1907 19028 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1908 19028 : {
1909 19028 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1910 19028 : enum tree_code subcode;
1911 :
1912 19028 : if (TREE_CODE (arg0) != VECTOR_CST)
1913 : return NULL_TREE;
1914 :
1915 19028 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1916 : return NULL_TREE;
1917 19028 : out_nelts = in_nelts / 2;
1918 19028 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1919 :
1920 19028 : unsigned int offset = 0;
1921 19028 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1922 19028 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1923 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1924 9506 : offset = out_nelts;
1925 :
1926 19028 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1927 : subcode = NOP_EXPR;
1928 7854 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1929 7854 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1930 : subcode = FLOAT_EXPR;
1931 : else
1932 4 : subcode = FIX_TRUNC_EXPR;
1933 :
1934 19028 : tree_vector_builder elts (type, out_nelts, 1);
1935 101366 : for (i = 0; i < out_nelts; i++)
1936 : {
1937 82338 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1938 82338 : VECTOR_CST_ELT (arg0, i + offset));
1939 82338 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1940 0 : return NULL_TREE;
1941 82338 : elts.quick_push (elt);
1942 : }
1943 :
1944 19028 : return elts.build ();
1945 19028 : }
1946 :
1947 4 : case VEC_DUPLICATE_EXPR:
1948 4 : if (CONSTANT_CLASS_P (arg0))
1949 4 : return build_vector_from_val (type, arg0);
1950 : return NULL_TREE;
1951 :
1952 : default:
1953 : break;
1954 : }
1955 :
1956 : return NULL_TREE;
1957 : }
1958 :
1959 : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1960 : indicates which particular sizetype to create. */
1961 :
1962 : tree
1963 3482426112 : size_int_kind (poly_int64 number, enum size_type_kind kind)
1964 : {
1965 3482426112 : return build_int_cst (sizetype_tab[(int) kind], number);
1966 : }
1967 :
1968 : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1969 : is a tree code. The type of the result is taken from the operands.
1970 : Both must be equivalent integer types, ala int_binop_types_match_p.
1971 : If the operands are constant, so is the result. */
1972 :
1973 : tree
1974 2532804657 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1975 : {
1976 2532804657 : tree type = TREE_TYPE (arg0);
1977 :
1978 2532804657 : if (arg0 == error_mark_node || arg1 == error_mark_node)
1979 : return error_mark_node;
1980 :
1981 2532804657 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1982 : TREE_TYPE (arg1)));
1983 :
1984 : /* Handle the special case of two poly_int constants faster. */
1985 2532804657 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1986 : {
1987 : /* And some specific cases even faster than that. */
1988 2500080388 : if (code == PLUS_EXPR)
1989 : {
1990 1143667928 : if (integer_zerop (arg0)
1991 1143667928 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1992 : return arg1;
1993 299329236 : if (integer_zerop (arg1)
1994 299329236 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1995 : return arg0;
1996 : }
1997 1356412460 : else if (code == MINUS_EXPR)
1998 : {
1999 116934732 : if (integer_zerop (arg1)
2000 116934732 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2001 : return arg0;
2002 : }
2003 1239477728 : else if (code == MULT_EXPR)
2004 : {
2005 581367252 : if (integer_onep (arg0)
2006 581367252 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2007 : return arg1;
2008 : }
2009 :
2010 : /* Handle general case of two integer constants. For sizetype
2011 : constant calculations we always want to know about overflow,
2012 : even in the unsigned case. */
2013 1214939010 : tree res = int_const_binop (code, arg0, arg1, -1);
2014 1214939010 : if (res != NULL_TREE)
2015 : return res;
2016 : }
2017 :
2018 32724269 : return fold_build2_loc (loc, code, type, arg0, arg1);
2019 : }
2020 :
2021 : /* Given two values, either both of sizetype or both of bitsizetype,
2022 : compute the difference between the two values. Return the value
2023 : in signed type corresponding to the type of the operands. */
2024 :
2025 : tree
2026 36943938 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2027 : {
2028 36943938 : tree type = TREE_TYPE (arg0);
2029 36943938 : tree ctype;
2030 :
2031 36943938 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2032 : TREE_TYPE (arg1)));
2033 :
2034 : /* If the type is already signed, just do the simple thing. */
2035 36943938 : if (!TYPE_UNSIGNED (type))
2036 9948320 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2037 :
2038 26995618 : if (type == sizetype)
2039 26995618 : ctype = ssizetype;
2040 0 : else if (type == bitsizetype)
2041 0 : ctype = sbitsizetype;
2042 : else
2043 0 : ctype = signed_type_for (type);
2044 :
2045 : /* If either operand is not a constant, do the conversions to the signed
2046 : type and subtract. The hardware will do the right thing with any
2047 : overflow in the subtraction. */
2048 26995618 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2049 17626 : return size_binop_loc (loc, MINUS_EXPR,
2050 : fold_convert_loc (loc, ctype, arg0),
2051 17626 : fold_convert_loc (loc, ctype, arg1));
2052 :
2053 : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2054 : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2055 : overflow) and negate (which can't either). Special-case a result
2056 : of zero while we're here. */
2057 26977992 : if (tree_int_cst_equal (arg0, arg1))
2058 23758929 : return build_int_cst (ctype, 0);
2059 3219063 : else if (tree_int_cst_lt (arg1, arg0))
2060 2127926 : return fold_convert_loc (loc, ctype,
2061 2127926 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2062 : else
2063 1091137 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2064 : fold_convert_loc (loc, ctype,
2065 : size_binop_loc (loc,
2066 : MINUS_EXPR,
2067 : arg1, arg0)));
2068 : }
2069 :
2070 : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2071 : or pointer type. */
2072 :
2073 : tree
2074 1373788501 : int_const_convert (tree type, const_tree arg1, int overflowable)
2075 : {
2076 : /* Given an integer constant, make new constant with new type,
2077 : appropriately sign-extended or truncated. Use widest_int
2078 : so that any extension is done according ARG1's type. */
2079 1373788501 : tree arg1_type = TREE_TYPE (arg1);
2080 1373788501 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2081 1373788501 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2082 1373788501 : TYPE_SIGN (arg1_type)),
2083 : overflowable,
2084 1373788501 : TREE_OVERFLOW (arg1));
2085 : }
2086 :
2087 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2088 : to an integer type. */
2089 :
2090 : static tree
2091 54789 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2092 : {
2093 54789 : bool overflow = false;
2094 54789 : tree t;
2095 :
2096 : /* The following code implements the floating point to integer
2097 : conversion rules required by the Java Language Specification,
2098 : that IEEE NaNs are mapped to zero and values that overflow
2099 : the target precision saturate, i.e. values greater than
2100 : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2101 : are mapped to INT_MIN. These semantics are allowed by the
2102 : C and C++ standards that simply state that the behavior of
2103 : FP-to-integer conversion is unspecified upon overflow. */
2104 :
2105 54789 : wide_int val;
2106 54789 : REAL_VALUE_TYPE r;
2107 54789 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2108 :
2109 54789 : switch (code)
2110 : {
2111 54789 : case FIX_TRUNC_EXPR:
2112 54789 : real_trunc (&r, VOIDmode, &x);
2113 54789 : break;
2114 :
2115 0 : default:
2116 0 : gcc_unreachable ();
2117 : }
2118 :
2119 : /* If R is NaN, return zero and show we have an overflow. */
2120 54789 : if (REAL_VALUE_ISNAN (r))
2121 : {
2122 3638 : overflow = true;
2123 3638 : val = wi::zero (TYPE_PRECISION (type));
2124 : }
2125 :
2126 : /* See if R is less than the lower bound or greater than the
2127 : upper bound. */
2128 :
2129 54789 : if (! overflow)
2130 : {
2131 51151 : tree lt = TYPE_MIN_VALUE (type);
2132 51151 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2133 51151 : if (real_less (&r, &l))
2134 : {
2135 1974 : overflow = true;
2136 1974 : val = wi::to_wide (lt);
2137 : }
2138 : }
2139 :
2140 54789 : if (! overflow)
2141 : {
2142 49177 : tree ut = TYPE_MAX_VALUE (type);
2143 49177 : if (ut)
2144 : {
2145 49177 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2146 49177 : if (real_less (&u, &r))
2147 : {
2148 1921 : overflow = true;
2149 1921 : val = wi::to_wide (ut);
2150 : }
2151 : }
2152 : }
2153 :
2154 54789 : if (! overflow)
2155 47258 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2156 :
2157 : /* According to IEEE standard, for conversions from floating point to
2158 : integer. When a NaN or infinite operand cannot be represented in the
2159 : destination format and this cannot otherwise be indicated, the invalid
2160 : operation exception shall be signaled. When a numeric operand would
2161 : convert to an integer outside the range of the destination format, the
2162 : invalid operation exception shall be signaled if this situation cannot
2163 : otherwise be indicated. */
2164 54789 : if (!flag_trapping_math || !overflow)
2165 47512 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2166 : else
2167 : t = NULL_TREE;
2168 :
2169 54789 : return t;
2170 54789 : }
2171 :
2172 : /* A subroutine of fold_convert_const handling conversions of a
2173 : FIXED_CST to an integer type. */
2174 :
2175 : static tree
2176 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2177 : {
2178 0 : tree t;
2179 0 : double_int temp, temp_trunc;
2180 0 : scalar_mode mode;
2181 :
2182 : /* Right shift FIXED_CST to temp by fbit. */
2183 0 : temp = TREE_FIXED_CST (arg1).data;
2184 0 : mode = TREE_FIXED_CST (arg1).mode;
2185 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2186 : {
2187 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2188 : HOST_BITS_PER_DOUBLE_INT,
2189 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2190 :
2191 : /* Left shift temp to temp_trunc by fbit. */
2192 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2193 : HOST_BITS_PER_DOUBLE_INT,
2194 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2195 : }
2196 : else
2197 : {
2198 0 : temp = double_int_zero;
2199 0 : temp_trunc = double_int_zero;
2200 : }
2201 :
2202 : /* If FIXED_CST is negative, we need to round the value toward 0.
2203 : By checking if the fractional bits are not zero to add 1 to temp. */
2204 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2205 0 : && temp_trunc.is_negative ()
2206 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2207 0 : temp += double_int_one;
2208 :
2209 : /* Given a fixed-point constant, make new constant with new type,
2210 : appropriately sign-extended or truncated. */
2211 0 : t = force_fit_type (type, temp, -1,
2212 0 : (temp.is_negative ()
2213 0 : && (TYPE_UNSIGNED (type)
2214 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2215 0 : | TREE_OVERFLOW (arg1));
2216 :
2217 0 : return t;
2218 : }
2219 :
2220 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2221 : to another floating point type. */
2222 :
2223 : static tree
2224 2140934 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2225 : {
2226 2140934 : REAL_VALUE_TYPE value;
2227 2140934 : tree t;
2228 :
2229 : /* If the underlying modes are the same, simply treat it as
2230 : copy and rebuild with TREE_REAL_CST information and the
2231 : given type. */
2232 2140934 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2233 : {
2234 98498 : t = build_real (type, TREE_REAL_CST (arg1));
2235 98498 : return t;
2236 : }
2237 :
2238 : /* Don't perform the operation if flag_signaling_nans is on
2239 : and the operand is a signaling NaN. */
2240 2042436 : if (HONOR_SNANS (arg1)
2241 2044318 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2242 : return NULL_TREE;
2243 :
2244 : /* With flag_rounding_math we should respect the current rounding mode
2245 : unless the conversion is exact. */
2246 2042436 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2247 2043092 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2248 509 : return NULL_TREE;
2249 :
2250 2041927 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2251 2041927 : t = build_real (type, value);
2252 :
2253 : /* If converting an infinity or NAN to a representation that doesn't
2254 : have one, set the overflow bit so that we can produce some kind of
2255 : error message at the appropriate point if necessary. It's not the
2256 : most user-friendly message, but it's better than nothing. */
2257 2041927 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2258 2173806 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2259 0 : TREE_OVERFLOW (t) = 1;
2260 2041927 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2261 2169564 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2262 0 : TREE_OVERFLOW (t) = 1;
2263 : /* Regular overflow, conversion produced an infinity in a mode that
2264 : can't represent them. */
2265 10206185 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2266 0 : && REAL_VALUE_ISINF (value)
2267 2041927 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2268 0 : TREE_OVERFLOW (t) = 1;
2269 : else
2270 2041927 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2271 : return t;
2272 : }
2273 :
2274 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2275 : to a floating point type. */
2276 :
2277 : static tree
2278 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2279 : {
2280 0 : REAL_VALUE_TYPE value;
2281 0 : tree t;
2282 :
2283 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2284 0 : &TREE_FIXED_CST (arg1));
2285 0 : t = build_real (type, value);
2286 :
2287 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2288 0 : return t;
2289 : }
2290 :
2291 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2292 : to another fixed-point type. */
2293 :
2294 : static tree
2295 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2296 : {
2297 0 : FIXED_VALUE_TYPE value;
2298 0 : tree t;
2299 0 : bool overflow_p;
2300 :
2301 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2302 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2303 0 : t = build_fixed (type, value);
2304 :
2305 : /* Propagate overflow flags. */
2306 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2307 0 : TREE_OVERFLOW (t) = 1;
2308 0 : return t;
2309 : }
2310 :
2311 : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2312 : to a fixed-point type. */
2313 :
2314 : static tree
2315 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2316 : {
2317 0 : FIXED_VALUE_TYPE value;
2318 0 : tree t;
2319 0 : bool overflow_p;
2320 0 : double_int di;
2321 :
2322 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2323 :
2324 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2325 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2326 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2327 : else
2328 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2329 :
2330 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2331 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2332 0 : TYPE_SATURATING (type));
2333 0 : t = build_fixed (type, value);
2334 :
2335 : /* Propagate overflow flags. */
2336 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2337 0 : TREE_OVERFLOW (t) = 1;
2338 0 : return t;
2339 : }
2340 :
2341 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2342 : to a fixed-point type. */
2343 :
2344 : static tree
2345 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2346 : {
2347 0 : FIXED_VALUE_TYPE value;
2348 0 : tree t;
2349 0 : bool overflow_p;
2350 :
2351 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2352 0 : &TREE_REAL_CST (arg1),
2353 0 : TYPE_SATURATING (type));
2354 0 : t = build_fixed (type, value);
2355 :
2356 : /* Propagate overflow flags. */
2357 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2358 0 : TREE_OVERFLOW (t) = 1;
2359 0 : return t;
2360 : }
2361 :
2362 : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2363 : type TYPE. If no simplification can be done return NULL_TREE. */
2364 :
2365 : static tree
2366 1435158958 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2367 : {
2368 1435158958 : tree arg_type = TREE_TYPE (arg1);
2369 1435158958 : if (arg_type == type)
2370 : return arg1;
2371 :
2372 : /* We can't widen types, since the runtime value could overflow the
2373 : original type before being extended to the new type. */
2374 1424488066 : if (POLY_INT_CST_P (arg1)
2375 : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2376 : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2377 : return build_poly_int_cst (type,
2378 : poly_wide_int::from (poly_int_cst_value (arg1),
2379 : TYPE_PRECISION (type),
2380 : TYPE_SIGN (arg_type)));
2381 :
2382 1424488066 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2383 : || TREE_CODE (type) == OFFSET_TYPE)
2384 : {
2385 1394433625 : if (TREE_CODE (arg1) == INTEGER_CST)
2386 1373788501 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2387 20645124 : else if (TREE_CODE (arg1) == REAL_CST)
2388 54789 : return fold_convert_const_int_from_real (code, type, arg1);
2389 20590335 : else if (TREE_CODE (arg1) == FIXED_CST)
2390 0 : return fold_convert_const_int_from_fixed (type, arg1);
2391 : }
2392 : else if (SCALAR_FLOAT_TYPE_P (type))
2393 : {
2394 29999461 : if (TREE_CODE (arg1) == INTEGER_CST)
2395 : {
2396 23116657 : tree res = build_real_from_int_cst (type, arg1);
2397 : /* Avoid the folding if flag_rounding_math is on and the
2398 : conversion is not exact. */
2399 23116657 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2400 : {
2401 2902 : bool fail = false;
2402 5804 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2403 2902 : TYPE_PRECISION (TREE_TYPE (arg1)));
2404 2902 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2405 1743 : return NULL_TREE;
2406 2902 : }
2407 23114914 : return res;
2408 : }
2409 6882804 : else if (TREE_CODE (arg1) == REAL_CST)
2410 2140934 : return fold_convert_const_real_from_real (type, arg1);
2411 4741870 : else if (TREE_CODE (arg1) == FIXED_CST)
2412 0 : return fold_convert_const_real_from_fixed (type, arg1);
2413 : }
2414 : else if (FIXED_POINT_TYPE_P (type))
2415 : {
2416 0 : if (TREE_CODE (arg1) == FIXED_CST)
2417 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2418 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2419 0 : return fold_convert_const_fixed_from_int (type, arg1);
2420 0 : else if (TREE_CODE (arg1) == REAL_CST)
2421 0 : return fold_convert_const_fixed_from_real (type, arg1);
2422 : }
2423 : else if (VECTOR_TYPE_P (type))
2424 : {
2425 4641 : if (TREE_CODE (arg1) == VECTOR_CST
2426 4641 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2427 : {
2428 4641 : tree elttype = TREE_TYPE (type);
2429 4641 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2430 : /* We can't handle steps directly when extending, since the
2431 : values need to wrap at the original precision first. */
2432 4641 : bool step_ok_p
2433 4641 : = (INTEGRAL_TYPE_P (elttype)
2434 291 : && INTEGRAL_TYPE_P (arg1_elttype)
2435 4874 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2436 4641 : tree_vector_builder v;
2437 4641 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2438 : return NULL_TREE;
2439 4641 : unsigned int len = v.encoded_nelts ();
2440 27640 : for (unsigned int i = 0; i < len; ++i)
2441 : {
2442 22999 : tree elt = VECTOR_CST_ELT (arg1, i);
2443 22999 : tree cvt = fold_convert_const (code, elttype, elt);
2444 22999 : if (cvt == NULL_TREE)
2445 0 : return NULL_TREE;
2446 22999 : v.quick_push (cvt);
2447 : }
2448 4641 : return v.build ();
2449 4641 : }
2450 : }
2451 11345 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2452 11345 : return build_zero_cst (type);
2453 : return NULL_TREE;
2454 : }
2455 :
2456 : /* Construct a vector of zero elements of vector type TYPE. */
2457 :
2458 : static tree
2459 17215 : build_zero_vector (tree type)
2460 : {
2461 17215 : tree t;
2462 :
2463 17215 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2464 17215 : return build_vector_from_val (type, t);
2465 : }
2466 :
2467 : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2468 :
2469 : bool
2470 4377 : fold_convertible_p (const_tree type, const_tree arg)
2471 : {
2472 4377 : const_tree orig = TREE_TYPE (arg);
2473 :
2474 4377 : if (type == orig)
2475 : return true;
2476 :
2477 4377 : if (TREE_CODE (arg) == ERROR_MARK
2478 4377 : || TREE_CODE (type) == ERROR_MARK
2479 4377 : || TREE_CODE (orig) == ERROR_MARK)
2480 : return false;
2481 :
2482 4377 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2483 : return true;
2484 :
2485 4377 : switch (TREE_CODE (type))
2486 : {
2487 3797 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2488 3797 : case POINTER_TYPE: case REFERENCE_TYPE:
2489 3797 : case OFFSET_TYPE:
2490 3797 : return (INTEGRAL_TYPE_P (orig)
2491 383 : || (POINTER_TYPE_P (orig)
2492 248 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2493 3932 : || TREE_CODE (orig) == OFFSET_TYPE);
2494 :
2495 130 : case REAL_TYPE:
2496 130 : case FIXED_POINT_TYPE:
2497 130 : case VOID_TYPE:
2498 130 : return TREE_CODE (type) == TREE_CODE (orig);
2499 :
2500 209 : case VECTOR_TYPE:
2501 209 : return (VECTOR_TYPE_P (orig)
2502 322 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2503 : TYPE_VECTOR_SUBPARTS (orig))
2504 226 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2505 :
2506 : default:
2507 : return false;
2508 : }
2509 : }
2510 :
2511 : /* Convert expression ARG to type TYPE. Used by the middle-end for
2512 : simple conversions in preference to calling the front-end's convert. */
2513 :
2514 : tree
2515 2107467119 : fold_convert_loc (location_t loc, tree type, tree arg)
2516 : {
2517 2107467119 : tree orig = TREE_TYPE (arg);
2518 2107467119 : tree tem;
2519 :
2520 2107467119 : if (type == orig)
2521 : return arg;
2522 :
2523 1415257774 : if (TREE_CODE (arg) == ERROR_MARK
2524 1415256746 : || TREE_CODE (type) == ERROR_MARK
2525 1415256745 : || TREE_CODE (orig) == ERROR_MARK)
2526 1029 : return error_mark_node;
2527 :
2528 1415256745 : switch (TREE_CODE (type))
2529 : {
2530 104103438 : case POINTER_TYPE:
2531 104103438 : case REFERENCE_TYPE:
2532 : /* Handle conversions between pointers to different address spaces. */
2533 104103438 : if (POINTER_TYPE_P (orig)
2534 104103438 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2535 89047720 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2536 84 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2537 : /* fall through */
2538 :
2539 1383742022 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2540 1383742022 : case OFFSET_TYPE: case BITINT_TYPE:
2541 1383742022 : if (TREE_CODE (arg) == INTEGER_CST)
2542 : {
2543 1167317827 : tem = fold_convert_const (NOP_EXPR, type, arg);
2544 1167317827 : if (tem != NULL_TREE)
2545 : return tem;
2546 : }
2547 216424195 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2548 2396 : || TREE_CODE (orig) == OFFSET_TYPE)
2549 216424195 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2550 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2551 0 : return fold_convert_loc (loc, type,
2552 : fold_build1_loc (loc, REALPART_EXPR,
2553 0 : TREE_TYPE (orig), arg));
2554 0 : gcc_assert (VECTOR_TYPE_P (orig)
2555 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2556 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2557 :
2558 548607 : case REAL_TYPE:
2559 548607 : if (TREE_CODE (arg) == INTEGER_CST)
2560 : {
2561 57979 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2562 57979 : if (tem != NULL_TREE)
2563 : return tem;
2564 : }
2565 490628 : else if (TREE_CODE (arg) == REAL_CST)
2566 : {
2567 119800 : tem = fold_convert_const (NOP_EXPR, type, arg);
2568 119800 : if (tem != NULL_TREE)
2569 : return tem;
2570 : }
2571 370828 : else if (TREE_CODE (arg) == FIXED_CST)
2572 : {
2573 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2574 0 : if (tem != NULL_TREE)
2575 : return tem;
2576 : }
2577 :
2578 370830 : switch (TREE_CODE (orig))
2579 : {
2580 651 : case INTEGER_TYPE: case BITINT_TYPE:
2581 651 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2582 651 : case POINTER_TYPE: case REFERENCE_TYPE:
2583 651 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2584 :
2585 370179 : case REAL_TYPE:
2586 370179 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2587 :
2588 0 : case FIXED_POINT_TYPE:
2589 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2590 :
2591 0 : case COMPLEX_TYPE:
2592 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2593 0 : return fold_convert_loc (loc, type, tem);
2594 :
2595 0 : default:
2596 0 : gcc_unreachable ();
2597 : }
2598 :
2599 0 : case FIXED_POINT_TYPE:
2600 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2601 0 : || TREE_CODE (arg) == REAL_CST)
2602 : {
2603 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2604 0 : if (tem != NULL_TREE)
2605 0 : goto fold_convert_exit;
2606 : }
2607 :
2608 0 : switch (TREE_CODE (orig))
2609 : {
2610 0 : case FIXED_POINT_TYPE:
2611 0 : case INTEGER_TYPE:
2612 0 : case ENUMERAL_TYPE:
2613 0 : case BOOLEAN_TYPE:
2614 0 : case REAL_TYPE:
2615 0 : case BITINT_TYPE:
2616 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2617 :
2618 0 : case COMPLEX_TYPE:
2619 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2620 0 : return fold_convert_loc (loc, type, tem);
2621 :
2622 0 : default:
2623 0 : gcc_unreachable ();
2624 : }
2625 :
2626 2263 : case COMPLEX_TYPE:
2627 2263 : switch (TREE_CODE (orig))
2628 : {
2629 584 : case INTEGER_TYPE: case BITINT_TYPE:
2630 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2631 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2632 584 : case REAL_TYPE:
2633 584 : case FIXED_POINT_TYPE:
2634 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2635 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2636 584 : fold_convert_loc (loc, TREE_TYPE (type),
2637 584 : integer_zero_node));
2638 1679 : case COMPLEX_TYPE:
2639 1679 : {
2640 1679 : tree rpart, ipart;
2641 :
2642 1679 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2643 : {
2644 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2645 1534 : TREE_OPERAND (arg, 0));
2646 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2647 1534 : TREE_OPERAND (arg, 1));
2648 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2649 : }
2650 :
2651 145 : arg = save_expr (arg);
2652 145 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2653 145 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2654 145 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2655 145 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2656 145 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2657 : }
2658 :
2659 0 : default:
2660 0 : gcc_unreachable ();
2661 : }
2662 :
2663 30850017 : case VECTOR_TYPE:
2664 30850017 : if (integer_zerop (arg))
2665 17215 : return build_zero_vector (type);
2666 30832802 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 30832802 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2668 : || VECTOR_TYPE_P (orig));
2669 30832802 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2670 :
2671 109981 : case VOID_TYPE:
2672 109981 : tem = fold_ignored_result (arg);
2673 109981 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2674 :
2675 63 : case NULLPTR_TYPE:
2676 63 : if (integer_zerop (arg))
2677 17 : return build_zero_cst (type);
2678 : /* FALLTHRU */
2679 3754 : default:
2680 3754 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2681 3754 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2682 0 : gcc_unreachable ();
2683 : }
2684 0 : fold_convert_exit:
2685 0 : tem = protected_set_expr_location_unshare (tem, loc);
2686 0 : return tem;
2687 : }
2688 :
2689 : /* Return false if expr can be assumed not to be an lvalue, true
2690 : otherwise. */
2691 :
2692 : static bool
2693 50703195 : maybe_lvalue_p (const_tree x)
2694 : {
2695 : /* We only need to wrap lvalue tree codes. */
2696 50703195 : switch (TREE_CODE (x))
2697 : {
2698 : case VAR_DECL:
2699 : case PARM_DECL:
2700 : case RESULT_DECL:
2701 : case LABEL_DECL:
2702 : case FUNCTION_DECL:
2703 : case SSA_NAME:
2704 : case COMPOUND_LITERAL_EXPR:
2705 :
2706 : case COMPONENT_REF:
2707 : case MEM_REF:
2708 : case INDIRECT_REF:
2709 : case ARRAY_REF:
2710 : case ARRAY_RANGE_REF:
2711 : case BIT_FIELD_REF:
2712 : case OBJ_TYPE_REF:
2713 :
2714 : case REALPART_EXPR:
2715 : case IMAGPART_EXPR:
2716 : case PREINCREMENT_EXPR:
2717 : case PREDECREMENT_EXPR:
2718 : case SAVE_EXPR:
2719 : case TRY_CATCH_EXPR:
2720 : case WITH_CLEANUP_EXPR:
2721 : case COMPOUND_EXPR:
2722 : case MODIFY_EXPR:
2723 : case TARGET_EXPR:
2724 : case COND_EXPR:
2725 : case BIND_EXPR:
2726 : case VIEW_CONVERT_EXPR:
2727 : break;
2728 :
2729 37045404 : default:
2730 : /* Assume the worst for front-end tree codes. */
2731 37045404 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2732 : break;
2733 : return false;
2734 : }
2735 :
2736 13681753 : return true;
2737 : }
2738 :
2739 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2740 :
2741 : tree
2742 46192871 : non_lvalue_loc (location_t loc, tree x)
2743 : {
2744 : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2745 : us. */
2746 46192871 : if (in_gimple_form)
2747 : return x;
2748 :
2749 9838060 : if (! maybe_lvalue_p (x))
2750 : return x;
2751 2401490 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2752 : }
2753 :
2754 : /* Given a tree comparison code, return the code that is the logical inverse.
2755 : It is generally not safe to do this for floating-point comparisons, except
2756 : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2757 : ERROR_MARK in this case. */
2758 :
2759 : enum tree_code
2760 125399448 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2761 : {
2762 125399448 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2763 987147 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2764 : return ERROR_MARK;
2765 :
2766 124650674 : switch (code)
2767 : {
2768 : case EQ_EXPR:
2769 : return NE_EXPR;
2770 55442255 : case NE_EXPR:
2771 55442255 : return EQ_EXPR;
2772 11888083 : case GT_EXPR:
2773 11888083 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2774 15620750 : case GE_EXPR:
2775 15620750 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2776 7904443 : case LT_EXPR:
2777 7904443 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2778 8045653 : case LE_EXPR:
2779 8045653 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2780 254 : case LTGT_EXPR:
2781 254 : return UNEQ_EXPR;
2782 291 : case UNEQ_EXPR:
2783 291 : return LTGT_EXPR;
2784 : case UNGT_EXPR:
2785 : return LE_EXPR;
2786 : case UNGE_EXPR:
2787 : return LT_EXPR;
2788 : case UNLT_EXPR:
2789 : return GE_EXPR;
2790 : case UNLE_EXPR:
2791 : return GT_EXPR;
2792 211018 : case ORDERED_EXPR:
2793 211018 : return UNORDERED_EXPR;
2794 56325 : case UNORDERED_EXPR:
2795 56325 : return ORDERED_EXPR;
2796 0 : default:
2797 0 : gcc_unreachable ();
2798 : }
2799 : }
2800 :
2801 : /* Similar, but return the comparison that results if the operands are
2802 : swapped. This is safe for floating-point. */
2803 :
2804 : enum tree_code
2805 155434069 : swap_tree_comparison (enum tree_code code)
2806 : {
2807 155434069 : switch (code)
2808 : {
2809 : case EQ_EXPR:
2810 : case NE_EXPR:
2811 : case ORDERED_EXPR:
2812 : case UNORDERED_EXPR:
2813 : case LTGT_EXPR:
2814 : case UNEQ_EXPR:
2815 : return code;
2816 37994619 : case GT_EXPR:
2817 37994619 : return LT_EXPR;
2818 11006528 : case GE_EXPR:
2819 11006528 : return LE_EXPR;
2820 21581078 : case LT_EXPR:
2821 21581078 : return GT_EXPR;
2822 16106593 : case LE_EXPR:
2823 16106593 : return GE_EXPR;
2824 231498 : case UNGT_EXPR:
2825 231498 : return UNLT_EXPR;
2826 19410 : case UNGE_EXPR:
2827 19410 : return UNLE_EXPR;
2828 347710 : case UNLT_EXPR:
2829 347710 : return UNGT_EXPR;
2830 106726 : case UNLE_EXPR:
2831 106726 : return UNGE_EXPR;
2832 0 : default:
2833 0 : gcc_unreachable ();
2834 : }
2835 : }
2836 :
2837 :
2838 : /* Convert a comparison tree code from an enum tree_code representation
2839 : into a compcode bit-based encoding. This function is the inverse of
2840 : compcode_to_comparison. */
2841 :
2842 : static enum comparison_code
2843 57386 : comparison_to_compcode (enum tree_code code)
2844 : {
2845 57386 : switch (code)
2846 : {
2847 : case LT_EXPR:
2848 : return COMPCODE_LT;
2849 : case EQ_EXPR:
2850 : return COMPCODE_EQ;
2851 : case LE_EXPR:
2852 : return COMPCODE_LE;
2853 : case GT_EXPR:
2854 : return COMPCODE_GT;
2855 : case NE_EXPR:
2856 : return COMPCODE_NE;
2857 : case GE_EXPR:
2858 : return COMPCODE_GE;
2859 : case ORDERED_EXPR:
2860 : return COMPCODE_ORD;
2861 : case UNORDERED_EXPR:
2862 : return COMPCODE_UNORD;
2863 : case UNLT_EXPR:
2864 : return COMPCODE_UNLT;
2865 : case UNEQ_EXPR:
2866 : return COMPCODE_UNEQ;
2867 : case UNLE_EXPR:
2868 : return COMPCODE_UNLE;
2869 : case UNGT_EXPR:
2870 : return COMPCODE_UNGT;
2871 : case LTGT_EXPR:
2872 : return COMPCODE_LTGT;
2873 : case UNGE_EXPR:
2874 : return COMPCODE_UNGE;
2875 0 : default:
2876 0 : gcc_unreachable ();
2877 : }
2878 : }
2879 :
2880 : /* Convert a compcode bit-based encoding of a comparison operator back
2881 : to GCC's enum tree_code representation. This function is the
2882 : inverse of comparison_to_compcode. */
2883 :
2884 : static enum tree_code
2885 14125 : compcode_to_comparison (enum comparison_code code)
2886 : {
2887 14125 : switch (code)
2888 : {
2889 : case COMPCODE_LT:
2890 : return LT_EXPR;
2891 : case COMPCODE_EQ:
2892 : return EQ_EXPR;
2893 : case COMPCODE_LE:
2894 : return LE_EXPR;
2895 : case COMPCODE_GT:
2896 : return GT_EXPR;
2897 : case COMPCODE_NE:
2898 : return NE_EXPR;
2899 : case COMPCODE_GE:
2900 : return GE_EXPR;
2901 : case COMPCODE_ORD:
2902 : return ORDERED_EXPR;
2903 : case COMPCODE_UNORD:
2904 : return UNORDERED_EXPR;
2905 : case COMPCODE_UNLT:
2906 : return UNLT_EXPR;
2907 : case COMPCODE_UNEQ:
2908 : return UNEQ_EXPR;
2909 : case COMPCODE_UNLE:
2910 : return UNLE_EXPR;
2911 : case COMPCODE_UNGT:
2912 : return UNGT_EXPR;
2913 : case COMPCODE_LTGT:
2914 : return LTGT_EXPR;
2915 : case COMPCODE_UNGE:
2916 : return UNGE_EXPR;
2917 0 : default:
2918 0 : gcc_unreachable ();
2919 : }
2920 : }
2921 :
2922 : /* Return true if COND1 tests the opposite condition of COND2. */
2923 :
2924 : bool
2925 1685738 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2926 : {
2927 1685738 : return (COMPARISON_CLASS_P (cond1)
2928 1594608 : && COMPARISON_CLASS_P (cond2)
2929 1583614 : && (invert_tree_comparison
2930 1583614 : (TREE_CODE (cond1),
2931 3167228 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2932 67717 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2933 67717 : TREE_OPERAND (cond2, 0), 0)
2934 1707418 : && operand_equal_p (TREE_OPERAND (cond1, 1),
2935 21680 : TREE_OPERAND (cond2, 1), 0));
2936 : }
2937 :
2938 : /* Return a tree for the comparison which is the combination of
2939 : doing the AND or OR (depending on CODE) of the two operations LCODE
2940 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2941 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
2942 : if this makes the transformation invalid. */
2943 :
2944 : tree
2945 28693 : combine_comparisons (location_t loc,
2946 : enum tree_code code, enum tree_code lcode,
2947 : enum tree_code rcode, tree truth_type,
2948 : tree ll_arg, tree lr_arg)
2949 : {
2950 28693 : bool honor_nans = HONOR_NANS (ll_arg);
2951 28693 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
2952 28693 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
2953 28693 : int compcode;
2954 :
2955 28693 : switch (code)
2956 : {
2957 19180 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2958 19180 : compcode = lcompcode & rcompcode;
2959 19180 : break;
2960 :
2961 9513 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2962 9513 : compcode = lcompcode | rcompcode;
2963 9513 : break;
2964 :
2965 : default:
2966 : return NULL_TREE;
2967 : }
2968 :
2969 28693 : if (!honor_nans)
2970 : {
2971 : /* Eliminate unordered comparisons, as well as LTGT and ORD
2972 : which are not used unless the mode has NaNs. */
2973 23798 : compcode &= ~COMPCODE_UNORD;
2974 23798 : if (compcode == COMPCODE_LTGT)
2975 : compcode = COMPCODE_NE;
2976 22643 : else if (compcode == COMPCODE_ORD)
2977 : compcode = COMPCODE_TRUE;
2978 : }
2979 4895 : else if (flag_trapping_math)
2980 : {
2981 : /* Check that the original operation and the optimized ones will trap
2982 : under the same condition. */
2983 8210 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2984 3495 : && (lcompcode != COMPCODE_EQ)
2985 4105 : && (lcompcode != COMPCODE_ORD);
2986 8210 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2987 3629 : && (rcompcode != COMPCODE_EQ)
2988 4105 : && (rcompcode != COMPCODE_ORD);
2989 8210 : bool trap = (compcode & COMPCODE_UNORD) == 0
2990 3728 : && (compcode != COMPCODE_EQ)
2991 4105 : && (compcode != COMPCODE_ORD);
2992 :
2993 : /* In a short-circuited boolean expression the LHS might be
2994 : such that the RHS, if evaluated, will never trap. For
2995 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
2996 : if neither x nor y is NaN. (This is a mixed blessing: for
2997 : example, the expression above will never trap, hence
2998 : optimizing it to x < y would be invalid). */
2999 4105 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3000 3748 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3001 4105 : rtrap = false;
3002 :
3003 : /* If the comparison was short-circuited, and only the RHS
3004 : trapped, we may now generate a spurious trap. */
3005 4105 : if (rtrap && !ltrap
3006 102 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3007 : return NULL_TREE;
3008 :
3009 : /* If we changed the conditions that cause a trap, we lose. */
3010 4003 : if ((ltrap || rtrap) != trap)
3011 : return NULL_TREE;
3012 : }
3013 :
3014 1446 : if (compcode == COMPCODE_TRUE)
3015 1252 : return constant_boolean_node (true, truth_type);
3016 23992 : else if (compcode == COMPCODE_FALSE)
3017 9867 : return constant_boolean_node (false, truth_type);
3018 : else
3019 : {
3020 14125 : enum tree_code tcode;
3021 :
3022 14125 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3023 14125 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3024 : }
3025 : }
3026 :
3027 : /* Return nonzero if two operands (typically of the same tree node)
3028 : are necessarily equal. FLAGS modifies behavior as follows:
3029 :
3030 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3031 : This function tests whether the operands are indistinguishable;
3032 : it does not test whether they are equal using C's == operation.
3033 : The distinction is important for IEEE floating point, because
3034 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3035 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3036 :
3037 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3038 : even though it may hold multiple values during a function.
3039 : This is because a GCC tree node guarantees that nothing else is
3040 : executed between the evaluation of its "operands" (which may often
3041 : be evaluated in arbitrary order). Hence if the operands themselves
3042 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3043 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3044 : unset means assuming isochronic (or instantaneous) tree equivalence.
3045 : Unless comparing arbitrary expression trees, such as from different
3046 : statements, this flag can usually be left unset.
3047 :
3048 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3049 : are considered the same. It is used when the caller has other ways
3050 : to ensure that global memory is unchanged in between.
3051 :
3052 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3053 : not values of expressions.
3054 :
3055 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3056 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3057 :
3058 : If OEP_BITWISE is set, then require the values to be bitwise identical
3059 : rather than simply numerically equal. Do not take advantage of things
3060 : like math-related flags or undefined behavior; only return true for
3061 : values that are provably bitwise identical in all circumstances.
3062 :
3063 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3064 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3065 : rather than just numerically equivalent. The compared expressions must
3066 : however perform the same operations but may do intermediate computations in
3067 : differing signs. Because this comparison ignores any possible UB it cannot
3068 : be used blindly without ensuring that the context you are using it in itself
3069 : doesn't guarantee that there will be no UB. Conditional expressions are
3070 : excluded from this relaxation.
3071 :
3072 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3073 : differing hashes even for cases where operand_compare::operand_equal_p
3074 : compares equal.
3075 :
3076 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3077 : any operand with side effect. This is unnecessarily conservative in the
3078 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3079 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3080 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3081 : even if var is volatile. */
3082 :
3083 : bool
3084 7122911505 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3085 : unsigned int flags)
3086 : {
3087 7122911505 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3088 : }
3089 :
3090 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3091 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3092 : expression is being compared under for equality. This means that they can
3093 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3094 :
3095 : bool
3096 7123150172 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3097 : tree type1, const_tree arg1,
3098 : unsigned int flags)
3099 : {
3100 7123150172 : bool r;
3101 7123150172 : if (verify_hash_value (arg0, arg1, flags, &r))
3102 2989679127 : return r;
3103 :
3104 4133471045 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3105 4133471045 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3106 :
3107 : /* If either is ERROR_MARK, they aren't equal. */
3108 4133471045 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3109 4133470416 : || type0 == error_mark_node
3110 4133470414 : || type1 == error_mark_node)
3111 : return false;
3112 :
3113 : /* Similar, if either does not have a type (like a template id),
3114 : they aren't equal. */
3115 4133470413 : if (!type0 || !type1)
3116 : return false;
3117 :
3118 : /* Bitwise identity makes no sense if the values have different layouts. */
3119 4133465266 : if ((flags & OEP_BITWISE)
3120 4133465266 : && !tree_nop_conversion_p (type0, type1))
3121 : return false;
3122 :
3123 : /* We cannot consider pointers to different address space equal. */
3124 4133465266 : if (POINTER_TYPE_P (type0)
3125 630588054 : && POINTER_TYPE_P (type1)
3126 4670021034 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3127 536555768 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3128 : return false;
3129 :
3130 : /* Check equality of integer constants before bailing out due to
3131 : precision differences. */
3132 4133465004 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3133 : {
3134 : /* Address of INTEGER_CST is not defined; check that we did not forget
3135 : to drop the OEP_ADDRESS_OF flags. */
3136 648774982 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3137 648774982 : return tree_int_cst_equal (arg0, arg1);
3138 : }
3139 :
3140 3484690022 : if ((flags & OEP_ASSUME_WRAPV)
3141 1690534 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3142 : {
3143 303808 : const_tree t_arg0 = arg0;
3144 303808 : const_tree t_arg1 = arg1;
3145 303808 : STRIP_NOPS (arg0);
3146 303808 : STRIP_NOPS (arg1);
3147 : /* Only recurse if the conversion was one that was valid to strip. */
3148 303808 : if (t_arg0 != arg0 || t_arg1 != arg1)
3149 238667 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3150 : }
3151 :
3152 3484451355 : if (!(flags & OEP_ADDRESS_OF))
3153 : {
3154 : /* Check if we are checking an operation where the two's compliment
3155 : bitwise representation of the result is not the same between signed and
3156 : unsigned arithmetic. */
3157 3086578760 : bool enforce_signedness = true;
3158 3086578760 : if (flags & OEP_ASSUME_WRAPV)
3159 : {
3160 1351128 : switch (TREE_CODE (arg0))
3161 : {
3162 : case PLUS_EXPR:
3163 : case MINUS_EXPR:
3164 : case MULT_EXPR:
3165 : case BIT_IOR_EXPR:
3166 : case BIT_XOR_EXPR:
3167 : case BIT_AND_EXPR:
3168 : case BIT_NOT_EXPR:
3169 : case ABS_EXPR:
3170 : CASE_CONVERT:
3171 : case SSA_NAME:
3172 : case INTEGER_CST:
3173 : case VAR_DECL:
3174 : case PARM_DECL:
3175 : case RESULT_DECL:
3176 3086578760 : enforce_signedness = false;
3177 : break;
3178 :
3179 : default:
3180 : break;
3181 : }
3182 : }
3183 :
3184 : /* If both types don't have the same signedness, then we can't consider
3185 : them equal. We must check this before the STRIP_NOPS calls
3186 : because they may change the signedness of the arguments. As pointers
3187 : strictly don't have a signedness, require either two pointers or
3188 : two non-pointers as well. */
3189 3086578760 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3190 3086578760 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3191 141657523 : && enforce_signedness))
3192 : return false;
3193 :
3194 : /* If both types don't have the same precision, then it is not safe
3195 : to strip NOPs. */
3196 2793149928 : if (element_precision (type0) != element_precision (type1))
3197 : return false;
3198 :
3199 2645872214 : STRIP_NOPS (arg0);
3200 2645872214 : STRIP_NOPS (arg1);
3201 :
3202 2645872214 : type0 = TREE_TYPE (arg0);
3203 2645872214 : type1 = TREE_TYPE (arg1);
3204 : }
3205 : #if 0
3206 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3207 : sanity check once the issue is solved. */
3208 : else
3209 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3210 : are not defined. Check that we did not forget to drop the
3211 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3212 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3213 : && TREE_CODE (arg0) != SSA_NAME);
3214 : #endif
3215 :
3216 : /* In case both args are comparisons but with different comparison
3217 : code, try to swap the comparison operands of one arg to produce
3218 : a match and compare that variant. */
3219 3043744809 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3220 1219778217 : && COMPARISON_CLASS_P (arg0)
3221 6584937 : && COMPARISON_CLASS_P (arg1))
3222 : {
3223 4889084 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3224 :
3225 4889084 : if (TREE_CODE (arg0) == swap_code)
3226 2066905 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3227 2066905 : TREE_OPERAND (arg1, 1), flags)
3228 2086572 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3229 19667 : TREE_OPERAND (arg1, 0), flags);
3230 : }
3231 :
3232 3041677904 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3233 : {
3234 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3235 1217711312 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3236 : ;
3237 1217650302 : else if (flags & OEP_ADDRESS_OF)
3238 : {
3239 : /* If we are interested in comparing addresses ignore
3240 : MEM_REF wrappings of the base that can appear just for
3241 : TBAA reasons. */
3242 48305758 : if (TREE_CODE (arg0) == MEM_REF
3243 7752783 : && DECL_P (arg1)
3244 5435147 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3245 1201832 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3246 48924676 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3247 : return true;
3248 48089525 : else if (TREE_CODE (arg1) == MEM_REF
3249 30207359 : && DECL_P (arg0)
3250 10884284 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3251 2151432 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3252 48630874 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3253 : return true;
3254 47726407 : return false;
3255 : }
3256 : else
3257 : return false;
3258 : }
3259 :
3260 : /* When not checking addresses, this is needed for conversions and for
3261 : COMPONENT_REF. Might as well play it safe and always test this. */
3262 1824027602 : if (TREE_CODE (type0) == ERROR_MARK
3263 1824027602 : || TREE_CODE (type1) == ERROR_MARK
3264 3648055204 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3265 25326977 : && !(flags & OEP_ADDRESS_OF)))
3266 3787513 : return false;
3267 :
3268 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3269 : We don't care about side effects in that case because the SAVE_EXPR
3270 : takes care of that for us. In all other cases, two expressions are
3271 : equal if they have no side effects. If we have two identical
3272 : expressions with side effects that should be treated the same due
3273 : to the only side effects being identical SAVE_EXPR's, that will
3274 : be detected in the recursive calls below.
3275 : If we are taking an invariant address of two identical objects
3276 : they are necessarily equal as well. */
3277 324379422 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3278 2144619333 : && (TREE_CODE (arg0) == SAVE_EXPR
3279 324353885 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3280 285286204 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3281 : return true;
3282 :
3283 : /* Next handle constant cases, those for which we can return 1 even
3284 : if ONLY_CONST is set. */
3285 1496002395 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3286 24836802 : switch (TREE_CODE (arg0))
3287 : {
3288 151 : case INTEGER_CST:
3289 151 : return tree_int_cst_equal (arg0, arg1);
3290 :
3291 0 : case FIXED_CST:
3292 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3293 : TREE_FIXED_CST (arg1));
3294 :
3295 3583719 : case REAL_CST:
3296 3583719 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3297 : return true;
3298 :
3299 2568349 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3300 : {
3301 : /* If we do not distinguish between signed and unsigned zero,
3302 : consider them equal. */
3303 14320 : if (real_zerop (arg0) && real_zerop (arg1))
3304 : return true;
3305 : }
3306 2568339 : return false;
3307 :
3308 883965 : case VECTOR_CST:
3309 883965 : {
3310 883965 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3311 883965 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3312 : return false;
3313 :
3314 862977 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3315 862977 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3316 : return false;
3317 :
3318 829660 : unsigned int count = vector_cst_encoded_nelts (arg0);
3319 1184755 : for (unsigned int i = 0; i < count; ++i)
3320 1898534 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3321 949267 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3322 : return false;
3323 : return true;
3324 : }
3325 :
3326 13903 : case COMPLEX_CST:
3327 13903 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3328 : flags)
3329 13903 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3330 : flags));
3331 :
3332 947971 : case STRING_CST:
3333 947971 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3334 947971 : && ! memcmp (TREE_STRING_POINTER (arg0),
3335 528547 : TREE_STRING_POINTER (arg1),
3336 528547 : TREE_STRING_LENGTH (arg0)));
3337 :
3338 0 : case RAW_DATA_CST:
3339 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3340 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3341 0 : RAW_DATA_POINTER (arg1),
3342 0 : RAW_DATA_LENGTH (arg0)));
3343 :
3344 18322307 : case ADDR_EXPR:
3345 18322307 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3346 18322307 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3347 : flags | OEP_ADDRESS_OF
3348 18322307 : | OEP_MATCH_SIDE_EFFECTS);
3349 179173 : case CONSTRUCTOR:
3350 179173 : {
3351 : /* In GIMPLE empty constructors are allowed in initializers of
3352 : aggregates. */
3353 179173 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3354 : return true;
3355 :
3356 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3357 138139 : if (TREE_CODE (type0) != TREE_CODE (type1))
3358 : return false;
3359 138139 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3360 : {
3361 : /* For arrays, check that the sizes all match. */
3362 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3363 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3364 264 : || siz0 < 0
3365 528 : || siz0 != int_size_in_bytes (type1))
3366 0 : return false;
3367 : }
3368 137875 : else if (!types_compatible_p (type0, type1))
3369 : return false;
3370 :
3371 138139 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3372 138139 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3373 414417 : if (vec_safe_length (v0) != vec_safe_length (v1))
3374 : return false;
3375 :
3376 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3377 : of the CONSTRUCTOR referenced indirectly. */
3378 138139 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3379 :
3380 359980008 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3381 : {
3382 206325 : constructor_elt *c0 = &(*v0)[idx];
3383 206325 : constructor_elt *c1 = &(*v1)[idx];
3384 :
3385 : /* Check that the values are the same... */
3386 206325 : if (c0->value != c1->value
3387 206325 : && !operand_equal_p (c0->value, c1->value, flags))
3388 : return false;
3389 :
3390 : /* ... and that they apply to the same field! */
3391 117761 : if (c0->index != c1->index
3392 117761 : && (TREE_CODE (type0) == ARRAY_TYPE
3393 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3394 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3395 0 : DECL_FIELD_OFFSET (c1->index),
3396 : flags)
3397 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3398 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3399 : flags)))
3400 0 : return false;
3401 : }
3402 :
3403 : return true;
3404 : }
3405 :
3406 : default:
3407 : break;
3408 : }
3409 :
3410 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3411 : two instances of undefined behavior will give identical results. */
3412 1472071206 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3413 : return false;
3414 :
3415 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3416 : variant that allows null and views null as being different from any
3417 : non-null value. In the latter case, if either is null, the both
3418 : must be; otherwise, do the normal comparison. */
3419 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3420 : TREE_OPERAND (arg1, N), flags)
3421 :
3422 : #define OP_SAME_WITH_NULL(N) \
3423 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3424 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3425 :
3426 1472071206 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3427 : {
3428 8950671 : case tcc_unary:
3429 : /* Two conversions are equal only if signedness and modes match. */
3430 8950671 : switch (TREE_CODE (arg0))
3431 : {
3432 8568592 : CASE_CONVERT:
3433 8568592 : case FIX_TRUNC_EXPR:
3434 8568592 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3435 : return false;
3436 : break;
3437 : default:
3438 : break;
3439 : }
3440 :
3441 8950649 : return OP_SAME_WITH_NULL (0);
3442 :
3443 :
3444 22522928 : case tcc_comparison:
3445 22522928 : case tcc_binary:
3446 22522928 : if (OP_SAME (0) && OP_SAME (1))
3447 : return true;
3448 :
3449 : /* For commutative ops, allow the other order. */
3450 16144016 : return (commutative_tree_code (TREE_CODE (arg0))
3451 12103456 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3452 12103456 : TREE_OPERAND (arg1, 1), flags)
3453 16344783 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3454 200767 : TREE_OPERAND (arg1, 0), flags));
3455 :
3456 872778472 : case tcc_reference:
3457 : /* If either of the pointer (or reference) expressions we are
3458 : dereferencing contain a side effect, these cannot be equal,
3459 : but their addresses can be. */
3460 872778472 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3461 872778472 : && (TREE_SIDE_EFFECTS (arg0)
3462 805621794 : || TREE_SIDE_EFFECTS (arg1)))
3463 : return false;
3464 :
3465 872410193 : switch (TREE_CODE (arg0))
3466 : {
3467 5009337 : case INDIRECT_REF:
3468 5009337 : if (!(flags & OEP_ADDRESS_OF))
3469 : {
3470 4987645 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3471 : return false;
3472 : /* Verify that the access types are compatible. */
3473 4981612 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3474 : return false;
3475 : }
3476 4938945 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3477 4938945 : return OP_SAME (0);
3478 :
3479 573230 : case IMAGPART_EXPR:
3480 : /* Require the same offset. */
3481 573230 : if (!operand_equal_p (TYPE_SIZE (type0),
3482 573230 : TYPE_SIZE (type1),
3483 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3484 : return false;
3485 :
3486 : /* Fallthru. */
3487 2269655 : case REALPART_EXPR:
3488 2269655 : case VIEW_CONVERT_EXPR:
3489 2269655 : return OP_SAME (0);
3490 :
3491 84660024 : case TARGET_MEM_REF:
3492 84660024 : case MEM_REF:
3493 84660024 : if (!(flags & OEP_ADDRESS_OF))
3494 : {
3495 : /* Require equal access sizes */
3496 16234489 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3497 16234489 : && (!TYPE_SIZE (type0)
3498 1128236 : || !TYPE_SIZE (type1)
3499 1121423 : || !operand_equal_p (TYPE_SIZE (type0),
3500 1121423 : TYPE_SIZE (type1),
3501 : flags)))
3502 1125715 : return false;
3503 : /* Verify that access happens in similar types. */
3504 15108774 : if (!types_compatible_p (type0, type1))
3505 : return false;
3506 : /* Verify that accesses are TBAA compatible. */
3507 14776672 : if (!alias_ptr_types_compatible_p
3508 14776672 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3509 14776672 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3510 13891029 : || (MR_DEPENDENCE_CLIQUE (arg0)
3511 13891029 : != MR_DEPENDENCE_CLIQUE (arg1))
3512 27010196 : || (MR_DEPENDENCE_BASE (arg0)
3513 12233524 : != MR_DEPENDENCE_BASE (arg1)))
3514 : return false;
3515 : /* Verify that alignment is compatible. */
3516 11712043 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3517 : return false;
3518 : }
3519 79935356 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3520 136557096 : return (OP_SAME (0) && OP_SAME (1)
3521 : /* TARGET_MEM_REF require equal extra operands. */
3522 104197731 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3523 604652 : || (OP_SAME_WITH_NULL (2)
3524 268778 : && OP_SAME_WITH_NULL (3)
3525 263011 : && OP_SAME_WITH_NULL (4))));
3526 :
3527 35174278 : case ARRAY_REF:
3528 35174278 : case ARRAY_RANGE_REF:
3529 35174278 : if (!OP_SAME (0))
3530 : return false;
3531 30443214 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3532 : /* Compare the array index by value if it is constant first as we
3533 : may have different types but same value here. */
3534 30443214 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3535 30443214 : TREE_OPERAND (arg1, 1))
3536 27428132 : || OP_SAME (1))
3537 6059062 : && OP_SAME_WITH_NULL (2)
3538 6057738 : && OP_SAME_WITH_NULL (3)
3539 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3540 : we have to account for the offset of the ref. */
3541 39530483 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3542 3028869 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3543 2704 : || (operand_equal_p (array_ref_low_bound
3544 2704 : (const_cast<tree> (arg0)),
3545 : array_ref_low_bound
3546 2704 : (const_cast<tree> (arg1)),
3547 : flags)
3548 2704 : && operand_equal_p (array_ref_element_size
3549 2704 : (const_cast<tree> (arg0)),
3550 : array_ref_element_size
3551 2704 : (const_cast<tree> (arg1)),
3552 : flags))));
3553 :
3554 744682679 : case COMPONENT_REF:
3555 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3556 : may be NULL when we're called to compare MEM_EXPRs. */
3557 744682679 : if (!OP_SAME_WITH_NULL (0))
3558 : return false;
3559 56757871 : {
3560 56757871 : bool compare_address = flags & OEP_ADDRESS_OF;
3561 :
3562 : /* Most of time we only need to compare FIELD_DECLs for equality.
3563 : However when determining address look into actual offsets.
3564 : These may match for unions and unshared record types. */
3565 56757871 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3566 56757871 : if (!OP_SAME (1))
3567 : {
3568 33193888 : if (compare_address
3569 612078 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3570 : {
3571 612075 : tree field0 = TREE_OPERAND (arg0, 1);
3572 612075 : tree field1 = TREE_OPERAND (arg1, 1);
3573 :
3574 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3575 612075 : if (TREE_CODE (field0) != FIELD_DECL
3576 612075 : || TREE_CODE (field1) != FIELD_DECL)
3577 : return false;
3578 :
3579 612075 : if (!DECL_FIELD_OFFSET (field0)
3580 612075 : || !DECL_FIELD_OFFSET (field1))
3581 3 : return field0 == field1;
3582 :
3583 612072 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3584 612072 : DECL_FIELD_OFFSET (field1), flags)
3585 799083 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3586 187011 : DECL_FIELD_BIT_OFFSET (field1),
3587 : flags))
3588 573038 : return false;
3589 : }
3590 : else
3591 : return false;
3592 : }
3593 : }
3594 23603017 : return OP_SAME_WITH_NULL (2);
3595 :
3596 614172 : case BIT_FIELD_REF:
3597 614172 : if (!OP_SAME (0))
3598 : return false;
3599 362140 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3600 362140 : return OP_SAME (1) && OP_SAME (2);
3601 :
3602 : default:
3603 : return false;
3604 : }
3605 :
3606 58124178 : case tcc_expression:
3607 58124178 : switch (TREE_CODE (arg0))
3608 : {
3609 52840720 : case ADDR_EXPR:
3610 : /* Be sure we pass right ADDRESS_OF flag. */
3611 52840720 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3612 52840720 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3613 52840720 : TREE_OPERAND (arg1, 0),
3614 52840720 : flags | OEP_ADDRESS_OF);
3615 :
3616 568775 : case TRUTH_NOT_EXPR:
3617 568775 : return OP_SAME (0);
3618 :
3619 74633 : case TRUTH_ANDIF_EXPR:
3620 74633 : case TRUTH_ORIF_EXPR:
3621 74633 : return OP_SAME (0) && OP_SAME (1);
3622 :
3623 0 : case WIDEN_MULT_PLUS_EXPR:
3624 0 : case WIDEN_MULT_MINUS_EXPR:
3625 0 : if (!OP_SAME (2))
3626 : return false;
3627 : /* The multiplication operands are commutative. */
3628 : /* FALLTHRU */
3629 :
3630 46187 : case TRUTH_AND_EXPR:
3631 46187 : case TRUTH_OR_EXPR:
3632 46187 : case TRUTH_XOR_EXPR:
3633 46187 : if (OP_SAME (0) && OP_SAME (1))
3634 : return true;
3635 :
3636 : /* Otherwise take into account this is a commutative operation. */
3637 46169 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3638 46169 : TREE_OPERAND (arg1, 1), flags)
3639 46172 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3640 3 : TREE_OPERAND (arg1, 0), flags));
3641 :
3642 201457 : case COND_EXPR:
3643 201457 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3644 43260 : return false;
3645 158197 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3646 158197 : return OP_SAME (0);
3647 :
3648 4 : case BIT_INSERT_EXPR:
3649 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3650 : of op1. Need to check to make sure they are the same. */
3651 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3652 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3653 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3654 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3655 : return false;
3656 : /* FALLTHRU */
3657 :
3658 279 : case VEC_COND_EXPR:
3659 279 : case DOT_PROD_EXPR:
3660 279 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3661 :
3662 37369 : case MODIFY_EXPR:
3663 37369 : case INIT_EXPR:
3664 37369 : case COMPOUND_EXPR:
3665 37369 : case PREDECREMENT_EXPR:
3666 37369 : case PREINCREMENT_EXPR:
3667 37369 : case POSTDECREMENT_EXPR:
3668 37369 : case POSTINCREMENT_EXPR:
3669 37369 : if (flags & OEP_LEXICOGRAPHIC)
3670 165 : return OP_SAME (0) && OP_SAME (1);
3671 : return false;
3672 :
3673 301798 : case CLEANUP_POINT_EXPR:
3674 301798 : case EXPR_STMT:
3675 301798 : case SAVE_EXPR:
3676 301798 : if (flags & OEP_LEXICOGRAPHIC)
3677 208 : return OP_SAME (0);
3678 : return false;
3679 :
3680 76801 : case OBJ_TYPE_REF:
3681 : /* Virtual table reference. */
3682 153602 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3683 76801 : OBJ_TYPE_REF_EXPR (arg1), flags))
3684 : return false;
3685 14376 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3686 14376 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3687 14376 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3688 : return false;
3689 14376 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3690 14376 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3691 : return false;
3692 14376 : if (virtual_method_call_p (arg0))
3693 : {
3694 14376 : if (!virtual_method_call_p (arg1))
3695 : return false;
3696 14376 : return types_same_for_odr (obj_type_ref_class (arg0),
3697 28752 : obj_type_ref_class (arg1));
3698 : }
3699 : return false;
3700 :
3701 598 : case OMP_ARRAY_SECTION:
3702 598 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3703 :
3704 : default:
3705 : return false;
3706 : }
3707 :
3708 3626554 : case tcc_vl_exp:
3709 3626554 : switch (TREE_CODE (arg0))
3710 : {
3711 3626554 : case CALL_EXPR:
3712 3626554 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3713 3626554 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3714 : /* If not both CALL_EXPRs are either internal or normal function
3715 : functions, then they are not equal. */
3716 : return false;
3717 3626554 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3718 : {
3719 : /* If the CALL_EXPRs call different internal functions, then they
3720 : are not equal. */
3721 2 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3722 : return false;
3723 : }
3724 : else
3725 : {
3726 : /* If the CALL_EXPRs call different functions, then they are not
3727 : equal. */
3728 3626552 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3729 : flags))
3730 : return false;
3731 : }
3732 :
3733 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3734 2125530 : {
3735 2125530 : unsigned int cef = call_expr_flags (arg0);
3736 2125530 : if (flags & OEP_PURE_SAME)
3737 0 : cef &= ECF_CONST | ECF_PURE;
3738 : else
3739 2125530 : cef &= ECF_CONST;
3740 2125530 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3741 : return false;
3742 : }
3743 :
3744 : /* Now see if all the arguments are the same. */
3745 33360 : {
3746 33360 : const_call_expr_arg_iterator iter0, iter1;
3747 33360 : const_tree a0, a1;
3748 66720 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3749 33360 : a1 = first_const_call_expr_arg (arg1, &iter1);
3750 41473 : a0 && a1;
3751 8113 : a0 = next_const_call_expr_arg (&iter0),
3752 8113 : a1 = next_const_call_expr_arg (&iter1))
3753 34855 : if (! operand_equal_p (a0, a1, flags))
3754 : return false;
3755 :
3756 : /* If we get here and both argument lists are exhausted
3757 : then the CALL_EXPRs are equal. */
3758 6618 : return ! (a0 || a1);
3759 : }
3760 : default:
3761 : return false;
3762 : }
3763 :
3764 166755517 : case tcc_declaration:
3765 : /* Consider __builtin_sqrt equal to sqrt. */
3766 166755517 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3767 6692581 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3768 285111 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3769 6086970 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3770 285111 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3771 :
3772 160668547 : if (DECL_P (arg0)
3773 160668547 : && (flags & OEP_DECL_NAME)
3774 35 : && (flags & OEP_LEXICOGRAPHIC))
3775 : {
3776 : /* Consider decls with the same name equal. The caller needs
3777 : to make sure they refer to the same entity (such as a function
3778 : formal parameter). */
3779 35 : tree a0name = DECL_NAME (arg0);
3780 35 : tree a1name = DECL_NAME (arg1);
3781 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3782 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3783 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3784 : }
3785 : return false;
3786 :
3787 336819582 : case tcc_exceptional:
3788 336819582 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3789 : {
3790 19458 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3791 : return false;
3792 :
3793 : /* In GIMPLE constructors are used only to build vectors from
3794 : elements. Individual elements in the constructor must be
3795 : indexed in increasing order and form an initial sequence.
3796 :
3797 : We make no effort to compare nonconstant ones in GENERIC. */
3798 19458 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3799 : return false;
3800 :
3801 : /* Be sure that vectors constructed have the same representation.
3802 : We only tested element precision and modes to match.
3803 : Vectors may be BLKmode and thus also check that the number of
3804 : parts match. */
3805 743 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3806 1486 : TYPE_VECTOR_SUBPARTS (type1)))
3807 : return false;
3808 :
3809 743 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3810 743 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3811 743 : unsigned int len = vec_safe_length (v0);
3812 :
3813 1486 : if (len != vec_safe_length (v1))
3814 : return false;
3815 :
3816 3840 : for (unsigned int i = 0; i < len; i++)
3817 : {
3818 3360 : constructor_elt *c0 = &(*v0)[i];
3819 3360 : constructor_elt *c1 = &(*v1)[i];
3820 :
3821 3360 : if (!operand_equal_p (c0->value, c1->value, flags)
3822 : /* In GIMPLE the indexes can be either NULL or matching i.
3823 : Double check this so we won't get false
3824 : positives for GENERIC. */
3825 3112 : || (c0->index
3826 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3827 2588 : || compare_tree_int (c0->index, i)))
3828 6472 : || (c1->index
3829 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3830 2588 : || compare_tree_int (c1->index, i))))
3831 248 : return false;
3832 : }
3833 : return true;
3834 : }
3835 336800124 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3836 3242 : && (flags & OEP_LEXICOGRAPHIC))
3837 : {
3838 : /* Compare the STATEMENT_LISTs. */
3839 16 : tree_stmt_iterator tsi1, tsi2;
3840 16 : tree body1 = const_cast<tree> (arg0);
3841 16 : tree body2 = const_cast<tree> (arg1);
3842 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3843 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3844 : {
3845 : /* The lists don't have the same number of statements. */
3846 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3847 : return false;
3848 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3849 : return true;
3850 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3851 : flags & (OEP_LEXICOGRAPHIC
3852 : | OEP_NO_HASH_CHECK)))
3853 : return false;
3854 : }
3855 : }
3856 : return false;
3857 :
3858 2493114 : case tcc_statement:
3859 2493114 : switch (TREE_CODE (arg0))
3860 : {
3861 52 : case RETURN_EXPR:
3862 52 : if (flags & OEP_LEXICOGRAPHIC)
3863 52 : return OP_SAME_WITH_NULL (0);
3864 : return false;
3865 4 : case DEBUG_BEGIN_STMT:
3866 4 : if (flags & OEP_LEXICOGRAPHIC)
3867 : return true;
3868 : return false;
3869 : default:
3870 : return false;
3871 : }
3872 :
3873 : default:
3874 : return false;
3875 : }
3876 :
3877 : #undef OP_SAME
3878 : #undef OP_SAME_WITH_NULL
3879 : }
3880 :
3881 : /* Generate a hash value for an expression. This can be used iteratively
3882 : by passing a previous result as the HSTATE argument. */
3883 :
3884 : void
3885 3172252424 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3886 : unsigned int flags)
3887 : {
3888 3172252424 : int i;
3889 3172252424 : enum tree_code code;
3890 3172252424 : enum tree_code_class tclass;
3891 :
3892 3172252424 : if (t == NULL_TREE || t == error_mark_node)
3893 : {
3894 76134817 : hstate.merge_hash (0);
3895 76134817 : return;
3896 : }
3897 :
3898 3096117607 : STRIP_ANY_LOCATION_WRAPPER (t);
3899 :
3900 3096117607 : if (!(flags & OEP_ADDRESS_OF))
3901 2848214236 : STRIP_NOPS (t);
3902 :
3903 3096117607 : code = TREE_CODE (t);
3904 :
3905 3096117607 : switch (code)
3906 : {
3907 : /* Alas, constants aren't shared, so we can't rely on pointer
3908 : identity. */
3909 809 : case VOID_CST:
3910 809 : hstate.merge_hash (0);
3911 809 : return;
3912 830155615 : case INTEGER_CST:
3913 830155615 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3914 1678650834 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3915 848495219 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3916 : return;
3917 15536237 : case REAL_CST:
3918 15536237 : {
3919 15536237 : unsigned int val2;
3920 15536237 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3921 : val2 = rvc_zero;
3922 : else
3923 15321235 : val2 = real_hash (TREE_REAL_CST_PTR (t));
3924 15536237 : hstate.merge_hash (val2);
3925 15536237 : return;
3926 : }
3927 0 : case FIXED_CST:
3928 0 : {
3929 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
3930 0 : hstate.merge_hash (val2);
3931 0 : return;
3932 : }
3933 11450975 : case STRING_CST:
3934 11450975 : hstate.add ((const void *) TREE_STRING_POINTER (t),
3935 11450975 : TREE_STRING_LENGTH (t));
3936 11450975 : return;
3937 209 : case RAW_DATA_CST:
3938 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
3939 209 : RAW_DATA_LENGTH (t));
3940 209 : return;
3941 210185 : case COMPLEX_CST:
3942 210185 : hash_operand (TREE_REALPART (t), hstate, flags);
3943 210185 : hash_operand (TREE_IMAGPART (t), hstate, flags);
3944 210185 : return;
3945 3200042 : case VECTOR_CST:
3946 3200042 : {
3947 3200042 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
3948 3200042 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3949 3200042 : unsigned int count = vector_cst_encoded_nelts (t);
3950 10029371 : for (unsigned int i = 0; i < count; ++i)
3951 6829329 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3952 : return;
3953 : }
3954 871844310 : case SSA_NAME:
3955 : /* We can just compare by pointer. */
3956 871844310 : hstate.add_hwi (SSA_NAME_VERSION (t));
3957 871844310 : return;
3958 : case PLACEHOLDER_EXPR:
3959 : /* The node itself doesn't matter. */
3960 : return;
3961 : case BLOCK:
3962 : case OMP_CLAUSE:
3963 : case OMP_NEXT_VARIANT:
3964 : case OMP_TARGET_DEVICE_MATCHES:
3965 : /* Ignore. */
3966 : return;
3967 : case TREE_LIST:
3968 : /* A list of expressions, for a CALL_EXPR or as the elements of a
3969 : VECTOR_CST. */
3970 288238 : for (; t; t = TREE_CHAIN (t))
3971 144119 : hash_operand (TREE_VALUE (t), hstate, flags);
3972 : return;
3973 4889371 : case CONSTRUCTOR:
3974 4889371 : {
3975 4889371 : unsigned HOST_WIDE_INT idx;
3976 4889371 : tree field, value;
3977 4889371 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3978 4889371 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
3979 19782549 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
3980 : {
3981 : /* In GIMPLE the indexes can be either NULL or matching i. */
3982 14893178 : if (field == NULL_TREE)
3983 1086146 : field = bitsize_int (idx);
3984 14893178 : if (TREE_CODE (field) == FIELD_DECL)
3985 : {
3986 9950110 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
3987 9950110 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
3988 : }
3989 : else
3990 4943068 : hash_operand (field, hstate, flags);
3991 14893178 : hash_operand (value, hstate, flags);
3992 : }
3993 : return;
3994 : }
3995 182 : case STATEMENT_LIST:
3996 182 : {
3997 182 : tree_stmt_iterator i;
3998 182 : for (i = tsi_start (const_cast<tree> (t));
3999 550 : !tsi_end_p (i); tsi_next (&i))
4000 368 : hash_operand (tsi_stmt (i), hstate, flags);
4001 182 : return;
4002 : }
4003 : case TREE_VEC:
4004 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4005 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4006 : return;
4007 4 : case IDENTIFIER_NODE:
4008 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4009 4 : return;
4010 20608533 : case FUNCTION_DECL:
4011 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4012 : Otherwise nodes that compare equal according to operand_equal_p might
4013 : get different hash codes. However, don't do this for machine specific
4014 : or front end builtins, since the function code is overloaded in those
4015 : cases. */
4016 20608533 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4017 20608533 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4018 : {
4019 6915307 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4020 6915307 : code = TREE_CODE (t);
4021 : }
4022 : /* FALL THROUGH */
4023 1358685422 : default:
4024 1358685422 : if (POLY_INT_CST_P (t))
4025 : {
4026 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4027 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4028 : return;
4029 : }
4030 1358685422 : tclass = TREE_CODE_CLASS (code);
4031 :
4032 1358685422 : if (tclass == tcc_declaration)
4033 : {
4034 : /* DECL's have a unique ID */
4035 1027847739 : hstate.add_hwi (DECL_UID (t));
4036 : }
4037 330837683 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4038 : {
4039 : /* For comparisons that can be swapped, use the lower
4040 : tree code. */
4041 141649 : enum tree_code ccode = swap_tree_comparison (code);
4042 141649 : if (code < ccode)
4043 60371 : ccode = code;
4044 141649 : hstate.add_object (ccode);
4045 141649 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4046 141649 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4047 : }
4048 330696034 : else if (CONVERT_EXPR_CODE_P (code))
4049 : {
4050 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4051 : operand_equal_p. */
4052 7542492 : enum tree_code ccode = NOP_EXPR;
4053 7542492 : hstate.add_object (ccode);
4054 :
4055 : /* Don't hash the type, that can lead to having nodes which
4056 : compare equal according to operand_equal_p, but which
4057 : have different hash codes. Make sure to include signedness
4058 : in the hash computation. */
4059 7542492 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4060 7542492 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4061 : }
4062 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4063 323153542 : else if (code == MEM_REF
4064 77273406 : && (flags & OEP_ADDRESS_OF) != 0
4065 68217958 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4066 13395329 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4067 336342564 : && integer_zerop (TREE_OPERAND (t, 1)))
4068 6055970 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4069 : hstate, flags);
4070 : /* Don't ICE on FE specific trees, or their arguments etc.
4071 : during operand_equal_p hash verification. */
4072 317097572 : else if (!IS_EXPR_CODE_CLASS (tclass))
4073 252 : gcc_assert (flags & OEP_HASH_CHECK);
4074 : else
4075 : {
4076 317097320 : unsigned int sflags = flags;
4077 :
4078 317097320 : hstate.add_object (code);
4079 :
4080 317097320 : switch (code)
4081 : {
4082 126061454 : case ADDR_EXPR:
4083 126061454 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4084 126061454 : flags |= OEP_ADDRESS_OF;
4085 126061454 : sflags = flags;
4086 126061454 : break;
4087 :
4088 76052427 : case INDIRECT_REF:
4089 76052427 : case MEM_REF:
4090 76052427 : case TARGET_MEM_REF:
4091 76052427 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4092 76052427 : sflags = flags;
4093 76052427 : break;
4094 :
4095 73589353 : case COMPONENT_REF:
4096 73589353 : if (sflags & OEP_ADDRESS_OF)
4097 : {
4098 36755984 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4099 36755984 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4100 : hstate, flags & ~OEP_ADDRESS_OF);
4101 36755984 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4102 : hstate, flags & ~OEP_ADDRESS_OF);
4103 36755984 : return;
4104 : }
4105 : break;
4106 15413084 : case ARRAY_REF:
4107 15413084 : case ARRAY_RANGE_REF:
4108 15413084 : case BIT_FIELD_REF:
4109 15413084 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4110 15413084 : break;
4111 :
4112 8430 : case COND_EXPR:
4113 8430 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4114 8430 : break;
4115 :
4116 0 : case WIDEN_MULT_PLUS_EXPR:
4117 0 : case WIDEN_MULT_MINUS_EXPR:
4118 0 : {
4119 : /* The multiplication operands are commutative. */
4120 0 : inchash::hash one, two;
4121 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4122 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4123 0 : hstate.add_commutative (one, two);
4124 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4125 0 : return;
4126 : }
4127 :
4128 52686 : case CALL_EXPR:
4129 52686 : if (CALL_EXPR_FN (t) == NULL_TREE)
4130 6 : hstate.add_int (CALL_EXPR_IFN (t));
4131 : break;
4132 :
4133 72 : case TARGET_EXPR:
4134 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4135 : Usually different TARGET_EXPRs just should use
4136 : different temporaries in their slots. */
4137 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4138 72 : return;
4139 :
4140 274529 : case OBJ_TYPE_REF:
4141 : /* Virtual table reference. */
4142 274529 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4143 274529 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4144 274529 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4145 274529 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4146 274529 : if (!virtual_method_call_p (t))
4147 : return;
4148 274514 : if (tree c = obj_type_ref_class (t))
4149 : {
4150 274514 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4151 : /* We compute mangled names only when free_lang_data is run.
4152 : In that case we can hash precisely. */
4153 274514 : if (TREE_CODE (c) == TYPE_DECL
4154 274514 : && DECL_ASSEMBLER_NAME_SET_P (c))
4155 7303 : hstate.add_object
4156 7303 : (IDENTIFIER_HASH_VALUE
4157 : (DECL_ASSEMBLER_NAME (c)));
4158 : }
4159 274514 : return;
4160 : default:
4161 : break;
4162 : }
4163 :
4164 : /* Don't hash the type, that can lead to having nodes which
4165 : compare equal according to operand_equal_p, but which
4166 : have different hash codes. */
4167 280066735 : if (code == NON_LVALUE_EXPR)
4168 : {
4169 : /* Make sure to include signness in the hash computation. */
4170 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4171 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4172 : }
4173 :
4174 280066735 : else if (commutative_tree_code (code))
4175 : {
4176 : /* It's a commutative expression. We want to hash it the same
4177 : however it appears. We do this by first hashing both operands
4178 : and then rehashing based on the order of their independent
4179 : hashes. */
4180 18756727 : inchash::hash one, two;
4181 18756727 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4182 18756727 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4183 18756727 : hstate.add_commutative (one, two);
4184 : }
4185 : else
4186 730846264 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4187 677762766 : hash_operand (TREE_OPERAND (t, i), hstate,
4188 : i == 0 ? flags : sflags);
4189 : }
4190 : return;
4191 : }
4192 : }
4193 :
4194 : bool
4195 7127324635 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4196 : unsigned int flags, bool *ret)
4197 : {
4198 : /* When checking and unless comparing DECL names, verify that if
4199 : the outermost operand_equal_p call returns non-zero then ARG0
4200 : and ARG1 have the same hash value. */
4201 7127324635 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4202 : {
4203 2991495156 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4204 : {
4205 463473805 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4206 : {
4207 83165272 : inchash::hash hstate0 (0), hstate1 (0);
4208 83165272 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4209 83165272 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4210 83165272 : hashval_t h0 = hstate0.end ();
4211 83165272 : hashval_t h1 = hstate1.end ();
4212 83165272 : gcc_assert (h0 == h1);
4213 : }
4214 463473805 : *ret = true;
4215 : }
4216 : else
4217 2528021351 : *ret = false;
4218 :
4219 2991495156 : return true;
4220 : }
4221 :
4222 : return false;
4223 : }
4224 :
4225 :
4226 : static operand_compare default_compare_instance;
4227 :
4228 : /* Convenience wrapper around operand_compare class because usually we do
4229 : not need to play with the valueizer. */
4230 :
4231 : bool
4232 2989688324 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4233 : {
4234 2989688324 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4235 : }
4236 :
4237 : namespace inchash
4238 : {
4239 :
4240 : /* Generate a hash value for an expression. This can be used iteratively
4241 : by passing a previous result as the HSTATE argument.
4242 :
4243 : This function is intended to produce the same hash for expressions which
4244 : would compare equal using operand_equal_p. */
4245 : void
4246 2324272032 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4247 : {
4248 2324272032 : default_compare_instance.hash_operand (t, hstate, flags);
4249 2324272032 : }
4250 :
4251 : }
4252 :
4253 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4254 : with a different signedness or a narrower precision. */
4255 :
4256 : static bool
4257 19457299 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4258 : {
4259 19457299 : if (operand_equal_p (arg0, arg1, 0))
4260 : return true;
4261 :
4262 37215822 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4263 31820949 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4264 : return false;
4265 :
4266 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4267 : and see if the inner values are the same. This removes any
4268 : signedness comparison, which doesn't matter here. */
4269 5930932 : tree op0 = arg0;
4270 5930932 : tree op1 = arg1;
4271 5930932 : STRIP_NOPS (op0);
4272 5930932 : STRIP_NOPS (op1);
4273 5930932 : if (operand_equal_p (op0, op1, 0))
4274 : return true;
4275 :
4276 : /* Discard a single widening conversion from ARG1 and see if the inner
4277 : value is the same as ARG0. */
4278 4899318 : if (CONVERT_EXPR_P (arg1)
4279 848473 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4280 848425 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4281 848425 : < TYPE_PRECISION (TREE_TYPE (arg1))
4282 6044017 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4283 : return true;
4284 :
4285 : return false;
4286 : }
4287 :
4288 : /* See if ARG is an expression that is either a comparison or is performing
4289 : arithmetic on comparisons. The comparisons must only be comparing
4290 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4291 : they are nonzero it means that some operands have already been found.
4292 : No variables may be used anywhere else in the expression except in the
4293 : comparisons.
4294 :
4295 : If this is true, return 1. Otherwise, return zero. */
4296 :
4297 : static bool
4298 58079219 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4299 : {
4300 61835143 : enum tree_code code = TREE_CODE (arg);
4301 61835143 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4302 :
4303 : /* We can handle some of the tcc_expression cases here. */
4304 61835143 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4305 : tclass = tcc_unary;
4306 61359862 : else if (tclass == tcc_expression
4307 659381 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4308 659381 : || code == COMPOUND_EXPR))
4309 : tclass = tcc_binary;
4310 :
4311 61349131 : switch (tclass)
4312 : {
4313 3755924 : case tcc_unary:
4314 3755924 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4315 :
4316 5228443 : case tcc_binary:
4317 5228443 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4318 5228443 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4319 :
4320 : case tcc_constant:
4321 : return true;
4322 :
4323 648650 : case tcc_expression:
4324 648650 : if (code == COND_EXPR)
4325 717 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4326 717 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4327 781 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4328 : return false;
4329 :
4330 479725 : case tcc_comparison:
4331 : /* First see if we can handle the first operand, then the second. For
4332 : the second operand, we know *CVAL1 can't be zero. It must be that
4333 : one side of the comparison is each of the values; test for the
4334 : case where this isn't true by failing if the two operands
4335 : are the same. */
4336 :
4337 479725 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4338 479725 : TREE_OPERAND (arg, 1), 0))
4339 : return false;
4340 :
4341 479725 : if (*cval1 == 0)
4342 477711 : *cval1 = TREE_OPERAND (arg, 0);
4343 2014 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4344 : ;
4345 1895 : else if (*cval2 == 0)
4346 0 : *cval2 = TREE_OPERAND (arg, 0);
4347 1895 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4348 : ;
4349 : else
4350 : return false;
4351 :
4352 477830 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4353 : ;
4354 477830 : else if (*cval2 == 0)
4355 477711 : *cval2 = TREE_OPERAND (arg, 1);
4356 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4357 : ;
4358 : else
4359 : return false;
4360 :
4361 : return true;
4362 :
4363 : default:
4364 : return false;
4365 : }
4366 : }
4367 :
4368 : /* ARG is a tree that is known to contain just arithmetic operations and
4369 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4370 : any occurrence of OLD0 as an operand of a comparison and likewise for
4371 : NEW1 and OLD1. */
4372 :
4373 : static tree
4374 708 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4375 : tree old1, tree new1)
4376 : {
4377 708 : tree type = TREE_TYPE (arg);
4378 708 : enum tree_code code = TREE_CODE (arg);
4379 708 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4380 :
4381 : /* We can handle some of the tcc_expression cases here. */
4382 708 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4383 : tclass = tcc_unary;
4384 708 : else if (tclass == tcc_expression
4385 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4386 : tclass = tcc_binary;
4387 :
4388 699 : switch (tclass)
4389 : {
4390 168 : case tcc_unary:
4391 168 : return fold_build1_loc (loc, code, type,
4392 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4393 168 : old0, new0, old1, new1));
4394 :
4395 168 : case tcc_binary:
4396 336 : return fold_build2_loc (loc, code, type,
4397 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4398 : old0, new0, old1, new1),
4399 168 : eval_subst (loc, TREE_OPERAND (arg, 1),
4400 168 : old0, new0, old1, new1));
4401 :
4402 9 : case tcc_expression:
4403 9 : switch (code)
4404 : {
4405 0 : case SAVE_EXPR:
4406 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4407 0 : old1, new1);
4408 :
4409 0 : case COMPOUND_EXPR:
4410 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4411 0 : old1, new1);
4412 :
4413 9 : case COND_EXPR:
4414 27 : return fold_build3_loc (loc, code, type,
4415 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4416 : old0, new0, old1, new1),
4417 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4418 : old0, new0, old1, new1),
4419 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4420 9 : old0, new0, old1, new1));
4421 : default:
4422 : break;
4423 : }
4424 : /* Fall through - ??? */
4425 :
4426 183 : case tcc_comparison:
4427 183 : {
4428 183 : tree arg0 = TREE_OPERAND (arg, 0);
4429 183 : tree arg1 = TREE_OPERAND (arg, 1);
4430 :
4431 : /* We need to check both for exact equality and tree equality. The
4432 : former will be true if the operand has a side-effect. In that
4433 : case, we know the operand occurred exactly once. */
4434 :
4435 183 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4436 : arg0 = new0;
4437 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4438 : arg0 = new1;
4439 :
4440 183 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4441 : arg1 = new0;
4442 183 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4443 : arg1 = new1;
4444 :
4445 183 : return fold_build2_loc (loc, code, type, arg0, arg1);
4446 : }
4447 :
4448 : default:
4449 : return arg;
4450 : }
4451 : }
4452 :
4453 : /* Return a tree for the case when the result of an expression is RESULT
4454 : converted to TYPE and OMITTED was previously an operand of the expression
4455 : but is now not needed (e.g., we folded OMITTED * 0).
4456 :
4457 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4458 : the conversion of RESULT to TYPE. */
4459 :
4460 : tree
4461 274014 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4462 : {
4463 274014 : tree t = fold_convert_loc (loc, type, result);
4464 :
4465 : /* If the resulting operand is an empty statement, just return the omitted
4466 : statement casted to void. */
4467 274014 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4468 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4469 0 : fold_ignored_result (omitted));
4470 :
4471 274014 : if (TREE_SIDE_EFFECTS (omitted))
4472 11263 : return build2_loc (loc, COMPOUND_EXPR, type,
4473 11263 : fold_ignored_result (omitted), t);
4474 :
4475 262751 : return non_lvalue_loc (loc, t);
4476 : }
4477 :
4478 : /* Return a tree for the case when the result of an expression is RESULT
4479 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4480 : of the expression but are now not needed.
4481 :
4482 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4483 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4484 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4485 : just do the conversion of RESULT to TYPE. */
4486 :
4487 : tree
4488 5902 : omit_two_operands_loc (location_t loc, tree type, tree result,
4489 : tree omitted1, tree omitted2)
4490 : {
4491 5902 : tree t = fold_convert_loc (loc, type, result);
4492 :
4493 5902 : if (TREE_SIDE_EFFECTS (omitted2))
4494 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4495 5902 : if (TREE_SIDE_EFFECTS (omitted1))
4496 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4497 :
4498 5902 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4499 : }
4500 :
4501 :
4502 : /* Return a simplified tree node for the truth-negation of ARG. This
4503 : never alters ARG itself. We assume that ARG is an operation that
4504 : returns a truth value (0 or 1).
4505 :
4506 : FIXME: one would think we would fold the result, but it causes
4507 : problems with the dominator optimizer. */
4508 :
4509 : static tree
4510 48070633 : fold_truth_not_expr (location_t loc, tree arg)
4511 : {
4512 48070633 : tree type = TREE_TYPE (arg);
4513 48070633 : enum tree_code code = TREE_CODE (arg);
4514 48070633 : location_t loc1, loc2;
4515 :
4516 : /* If this is a comparison, we can simply invert it, except for
4517 : floating-point non-equality comparisons, in which case we just
4518 : enclose a TRUTH_NOT_EXPR around what we have. */
4519 :
4520 48070633 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4521 : {
4522 36627208 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4523 31446015 : if (FLOAT_TYPE_P (op_type)
4524 5191280 : && flag_trapping_math
4525 5161047 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4526 41748546 : && code != NE_EXPR && code != EQ_EXPR)
4527 : return NULL_TREE;
4528 :
4529 32185740 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4530 32185740 : if (code == ERROR_MARK)
4531 : return NULL_TREE;
4532 :
4533 32185740 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4534 32185740 : TREE_OPERAND (arg, 1));
4535 32185740 : copy_warning (ret, arg);
4536 32185740 : return ret;
4537 : }
4538 :
4539 11443425 : switch (code)
4540 : {
4541 0 : case INTEGER_CST:
4542 0 : return constant_boolean_node (integer_zerop (arg), type);
4543 :
4544 45283 : case TRUTH_AND_EXPR:
4545 45283 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4546 45283 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4547 90566 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4548 45283 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4549 90566 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4550 :
4551 2544 : case TRUTH_OR_EXPR:
4552 2544 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4553 2544 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4554 5088 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4555 2544 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4556 5088 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4557 :
4558 68456 : case TRUTH_XOR_EXPR:
4559 : /* Here we can invert either operand. We invert the first operand
4560 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4561 : result is the XOR of the first operand with the inside of the
4562 : negation of the second operand. */
4563 :
4564 68456 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4565 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4566 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4567 : else
4568 68268 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4569 68268 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4570 136536 : TREE_OPERAND (arg, 1));
4571 :
4572 376194 : case TRUTH_ANDIF_EXPR:
4573 376194 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4574 376194 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4575 752388 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4576 376194 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4577 752388 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4578 :
4579 15943 : case TRUTH_ORIF_EXPR:
4580 15943 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4581 15943 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4582 31886 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4583 15943 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4584 31886 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4585 :
4586 781298 : case TRUTH_NOT_EXPR:
4587 781298 : return TREE_OPERAND (arg, 0);
4588 :
4589 9754 : case COND_EXPR:
4590 9754 : {
4591 9754 : tree arg1 = TREE_OPERAND (arg, 1);
4592 9754 : tree arg2 = TREE_OPERAND (arg, 2);
4593 :
4594 9754 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4595 9754 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4596 :
4597 : /* A COND_EXPR may have a throw as one operand, which
4598 : then has void type. Just leave void operands
4599 : as they are. */
4600 9754 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4601 9754 : VOID_TYPE_P (TREE_TYPE (arg1))
4602 9754 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4603 9754 : VOID_TYPE_P (TREE_TYPE (arg2))
4604 19505 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4605 : }
4606 :
4607 937 : case COMPOUND_EXPR:
4608 937 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4609 1874 : return build2_loc (loc, COMPOUND_EXPR, type,
4610 937 : TREE_OPERAND (arg, 0),
4611 1874 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4612 :
4613 0 : case NON_LVALUE_EXPR:
4614 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4615 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4616 :
4617 73165 : CASE_CONVERT:
4618 73165 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4619 73101 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4620 :
4621 : /* fall through */
4622 :
4623 64 : case FLOAT_EXPR:
4624 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4625 64 : return build1_loc (loc, TREE_CODE (arg), type,
4626 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4627 :
4628 491 : case BIT_AND_EXPR:
4629 491 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4630 : return NULL_TREE;
4631 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4632 :
4633 2 : case SAVE_EXPR:
4634 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4635 :
4636 331 : case CLEANUP_POINT_EXPR:
4637 331 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4638 331 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4639 662 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4640 :
4641 : default:
4642 : return NULL_TREE;
4643 : }
4644 : }
4645 :
4646 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4647 : assume that ARG is an operation that returns a truth value (0 or 1
4648 : for scalars, 0 or -1 for vectors). Return the folded expression if
4649 : folding is successful. Otherwise, return NULL_TREE. */
4650 :
4651 : static tree
4652 1987380 : fold_invert_truthvalue (location_t loc, tree arg)
4653 : {
4654 1987380 : tree type = TREE_TYPE (arg);
4655 3974736 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4656 : ? BIT_NOT_EXPR
4657 : : TRUTH_NOT_EXPR,
4658 1987380 : type, arg);
4659 : }
4660 :
4661 : /* Return a simplified tree node for the truth-negation of ARG. This
4662 : never alters ARG itself. We assume that ARG is an operation that
4663 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4664 :
4665 : tree
4666 41943435 : invert_truthvalue_loc (location_t loc, tree arg)
4667 : {
4668 41943435 : if (TREE_CODE (arg) == ERROR_MARK)
4669 : return arg;
4670 :
4671 41943435 : tree type = TREE_TYPE (arg);
4672 83886870 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4673 : ? BIT_NOT_EXPR
4674 : : TRUTH_NOT_EXPR,
4675 41943435 : type, arg);
4676 : }
4677 :
4678 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4679 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4680 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4681 : is the original memory reference used to preserve the alias set of
4682 : the access. */
4683 :
4684 : tree
4685 736960 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4686 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4687 : int unsignedp, int reversep)
4688 : {
4689 736960 : tree result, bftype;
4690 :
4691 : /* Attempt not to lose the access path if possible. */
4692 736960 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4693 : {
4694 733224 : tree ninner = TREE_OPERAND (orig_inner, 0);
4695 733224 : machine_mode nmode;
4696 733224 : poly_int64 nbitsize, nbitpos;
4697 733224 : tree noffset;
4698 733224 : int nunsignedp, nreversep, nvolatilep = 0;
4699 733224 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4700 : &noffset, &nmode, &nunsignedp,
4701 : &nreversep, &nvolatilep);
4702 733224 : if (base == inner
4703 733087 : && noffset == NULL_TREE
4704 733087 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4705 733080 : && !reversep
4706 733008 : && !nreversep
4707 1466232 : && !nvolatilep)
4708 : {
4709 733008 : inner = ninner;
4710 733224 : bitpos -= nbitpos;
4711 : }
4712 : }
4713 :
4714 736960 : alias_set_type iset = get_alias_set (orig_inner);
4715 736960 : if (iset == 0 && get_alias_set (inner) != iset)
4716 234 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4717 : build_fold_addr_expr (inner),
4718 : build_int_cst (ptr_type_node, 0));
4719 :
4720 736960 : if (known_eq (bitpos, 0) && !reversep)
4721 : {
4722 11626 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4723 23252 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4724 11468 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4725 162 : && tree_fits_shwi_p (size)
4726 11788 : && tree_to_shwi (size) == bitsize)
4727 139 : return fold_convert_loc (loc, type, inner);
4728 : }
4729 :
4730 736821 : bftype = type;
4731 736821 : if (TYPE_PRECISION (bftype) != bitsize
4732 736821 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4733 396 : bftype = build_nonstandard_integer_type (bitsize, 0);
4734 :
4735 736821 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4736 736821 : bitsize_int (bitsize), bitsize_int (bitpos));
4737 736821 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4738 :
4739 736821 : if (bftype != type)
4740 396 : result = fold_convert_loc (loc, type, result);
4741 :
4742 : return result;
4743 : }
4744 :
4745 : /* Optimize a bit-field compare.
4746 :
4747 : There are two cases: First is a compare against a constant and the
4748 : second is a comparison of two items where the fields are at the same
4749 : bit position relative to the start of a chunk (byte, halfword, word)
4750 : large enough to contain it. In these cases we can avoid the shift
4751 : implicit in bitfield extractions.
4752 :
4753 : For constants, we emit a compare of the shifted constant with the
4754 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4755 : compared. For two fields at the same position, we do the ANDs with the
4756 : similar mask and compare the result of the ANDs.
4757 :
4758 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4759 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4760 : are the left and right operands of the comparison, respectively.
4761 :
4762 : If the optimization described above can be done, we return the resulting
4763 : tree. Otherwise we return zero. */
4764 :
4765 : static tree
4766 4364653 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4767 : tree compare_type, tree lhs, tree rhs)
4768 : {
4769 4364653 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4770 4364653 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4771 4364653 : tree type = TREE_TYPE (lhs);
4772 4364653 : tree unsigned_type;
4773 4364653 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4774 4364653 : machine_mode lmode, rmode;
4775 4364653 : scalar_int_mode nmode;
4776 4364653 : int lunsignedp, runsignedp;
4777 4364653 : int lreversep, rreversep;
4778 4364653 : int lvolatilep = 0, rvolatilep = 0;
4779 4364653 : tree linner, rinner = NULL_TREE;
4780 4364653 : tree mask;
4781 4364653 : tree offset;
4782 :
4783 : /* Get all the information about the extractions being done. If the bit size
4784 : is the same as the size of the underlying object, we aren't doing an
4785 : extraction at all and so can do nothing. We also don't want to
4786 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4787 : then will no longer be able to replace it. */
4788 4364653 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4789 : &lunsignedp, &lreversep, &lvolatilep);
4790 4364653 : if (linner == lhs
4791 4364653 : || !known_size_p (plbitsize)
4792 4364653 : || !plbitsize.is_constant (&lbitsize)
4793 4364653 : || !plbitpos.is_constant (&lbitpos)
4794 8729306 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4795 699360 : || offset != 0
4796 699335 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4797 5063988 : || lvolatilep)
4798 3665378 : return 0;
4799 :
4800 699275 : if (const_p)
4801 662391 : rreversep = lreversep;
4802 : else
4803 : {
4804 : /* If this is not a constant, we can only do something if bit positions,
4805 : sizes, signedness and storage order are the same. */
4806 36884 : rinner
4807 36884 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4808 : &runsignedp, &rreversep, &rvolatilep);
4809 :
4810 36884 : if (rinner == rhs
4811 36840 : || maybe_ne (lbitpos, rbitpos)
4812 36806 : || maybe_ne (lbitsize, rbitsize)
4813 36806 : || lunsignedp != runsignedp
4814 36806 : || lreversep != rreversep
4815 36806 : || offset != 0
4816 36806 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4817 73690 : || rvolatilep)
4818 : return 0;
4819 : }
4820 :
4821 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4822 699197 : poly_uint64 bitstart = 0;
4823 699197 : poly_uint64 bitend = 0;
4824 699197 : if (TREE_CODE (lhs) == COMPONENT_REF)
4825 : {
4826 699197 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4827 699197 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4828 : return 0;
4829 : }
4830 :
4831 : /* See if we can find a mode to refer to this field. We should be able to,
4832 : but fail if we can't. */
4833 1398394 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4834 662391 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4835 36806 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4836 : TYPE_ALIGN (TREE_TYPE (rinner))),
4837 699197 : BITS_PER_WORD, false, &nmode))
4838 : return 0;
4839 :
4840 : /* Set signed and unsigned types of the precision of this mode for the
4841 : shifts below. */
4842 697194 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4843 :
4844 : /* Compute the bit position and size for the new reference and our offset
4845 : within it. If the new reference is the same size as the original, we
4846 : won't optimize anything, so return zero. */
4847 697194 : nbitsize = GET_MODE_BITSIZE (nmode);
4848 697194 : nbitpos = lbitpos & ~ (nbitsize - 1);
4849 697194 : lbitpos -= nbitpos;
4850 697194 : if (nbitsize == lbitsize)
4851 : return 0;
4852 :
4853 697194 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4854 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4855 :
4856 : /* Make the mask to be used against the extracted field. */
4857 697194 : mask = build_int_cst_type (unsigned_type, -1);
4858 697194 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4859 697194 : mask = const_binop (RSHIFT_EXPR, mask,
4860 697194 : size_int (nbitsize - lbitsize - lbitpos));
4861 :
4862 697194 : if (! const_p)
4863 : {
4864 35251 : if (nbitpos < 0)
4865 : return 0;
4866 :
4867 : /* If not comparing with constant, just rework the comparison
4868 : and return. */
4869 35251 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4870 35251 : nbitsize, nbitpos, 1, lreversep);
4871 35251 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4872 35251 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4873 35251 : nbitsize, nbitpos, 1, rreversep);
4874 35251 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4875 35251 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4876 : }
4877 :
4878 : /* Otherwise, we are handling the constant case. See if the constant is too
4879 : big for the field. Warn and return a tree for 0 (false) if so. We do
4880 : this not only for its own sake, but to avoid having to test for this
4881 : error case below. If we didn't, we might generate wrong code.
4882 :
4883 : For unsigned fields, the constant shifted right by the field length should
4884 : be all zero. For signed fields, the high-order bits should agree with
4885 : the sign bit. */
4886 :
4887 661943 : if (lunsignedp)
4888 : {
4889 660778 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4890 : {
4891 0 : warning (0, "comparison is always %d due to width of bit-field",
4892 : code == NE_EXPR);
4893 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4894 : }
4895 : }
4896 : else
4897 : {
4898 1165 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4899 1165 : if (tem != 0 && tem != -1)
4900 : {
4901 0 : warning (0, "comparison is always %d due to width of bit-field",
4902 : code == NE_EXPR);
4903 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4904 : }
4905 1165 : }
4906 :
4907 661943 : if (nbitpos < 0)
4908 : return 0;
4909 :
4910 : /* Single-bit compares should always be against zero. */
4911 661943 : if (lbitsize == 1 && ! integer_zerop (rhs))
4912 : {
4913 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4914 175 : rhs = build_int_cst (type, 0);
4915 : }
4916 :
4917 : /* Make a new bitfield reference, shift the constant over the
4918 : appropriate number of bits and mask it with the computed mask
4919 : (in case this was a signed field). If we changed it, make a new one. */
4920 661943 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4921 661943 : nbitsize, nbitpos, 1, lreversep);
4922 :
4923 661943 : rhs = const_binop (BIT_AND_EXPR,
4924 : const_binop (LSHIFT_EXPR,
4925 : fold_convert_loc (loc, unsigned_type, rhs),
4926 661943 : size_int (lbitpos)),
4927 : mask);
4928 :
4929 661943 : lhs = build2_loc (loc, code, compare_type,
4930 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4931 661943 : return lhs;
4932 : }
4933 :
4934 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4935 : represents the sign bit of EXP's type. If EXP represents a sign
4936 : or zero extension, also test VAL against the unextended type.
4937 : The return value is the (sub)expression whose sign bit is VAL,
4938 : or NULL_TREE otherwise. */
4939 :
4940 : tree
4941 2206 : sign_bit_p (tree exp, const_tree val)
4942 : {
4943 2206 : int width;
4944 2206 : tree t;
4945 :
4946 : /* Tree EXP must have an integral type. */
4947 2206 : t = TREE_TYPE (exp);
4948 2206 : if (! INTEGRAL_TYPE_P (t))
4949 : return NULL_TREE;
4950 :
4951 : /* Tree VAL must be an integer constant. */
4952 1860 : if (TREE_CODE (val) != INTEGER_CST
4953 1860 : || TREE_OVERFLOW (val))
4954 : return NULL_TREE;
4955 :
4956 1499 : width = TYPE_PRECISION (t);
4957 1499 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
4958 : return exp;
4959 :
4960 : /* Handle extension from a narrower type. */
4961 862 : if (TREE_CODE (exp) == NOP_EXPR
4962 862 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4963 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
4964 :
4965 : return NULL_TREE;
4966 : }
4967 :
4968 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4969 : operand is simple enough to be evaluated unconditionally. */
4970 :
4971 : static bool
4972 64470583 : simple_operand_p (const_tree exp)
4973 : {
4974 : /* Strip any conversions that don't change the machine mode. */
4975 64470583 : STRIP_NOPS (exp);
4976 :
4977 64470583 : return (CONSTANT_CLASS_P (exp)
4978 44740031 : || TREE_CODE (exp) == SSA_NAME
4979 80052116 : || (DECL_P (exp)
4980 5417885 : && ! TREE_ADDRESSABLE (exp)
4981 5334204 : && ! TREE_THIS_VOLATILE (exp)
4982 5334204 : && ! DECL_NONLOCAL (exp)
4983 : /* Don't regard global variables as simple. They may be
4984 : allocated in ways unknown to the compiler (shared memory,
4985 : #pragma weak, etc). */
4986 5332551 : && ! TREE_PUBLIC (exp)
4987 5311848 : && ! DECL_EXTERNAL (exp)
4988 : /* DECL_VALUE_EXPR will expand to something non-simple. */
4989 5311848 : && ! ((VAR_P (exp)
4990 : || TREE_CODE (exp) == PARM_DECL
4991 : || TREE_CODE (exp) == RESULT_DECL)
4992 5311848 : && DECL_HAS_VALUE_EXPR_P (exp))
4993 : /* Weakrefs are not safe to be read, since they can be NULL.
4994 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4995 : have DECL_WEAK flag set. */
4996 5311259 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4997 : /* Loading a static variable is unduly expensive, but global
4998 : registers aren't expensive. */
4999 5311259 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5000 : }
5001 :
5002 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5003 : In addition to simple_operand_p, we assume that comparisons, conversions,
5004 : and logic-not operations are simple, if their operands are simple, too. */
5005 :
5006 : bool
5007 6728713 : simple_condition_p (tree exp)
5008 : {
5009 6818332 : enum tree_code code;
5010 :
5011 6818332 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5012 4634120 : return false;
5013 :
5014 2217661 : while (CONVERT_EXPR_P (exp))
5015 33449 : exp = TREE_OPERAND (exp, 0);
5016 :
5017 2184212 : code = TREE_CODE (exp);
5018 :
5019 2184212 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5020 1709966 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5021 1709966 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5022 :
5023 474246 : if (code == TRUTH_NOT_EXPR)
5024 89619 : return simple_condition_p (TREE_OPERAND (exp, 0));
5025 :
5026 384627 : return simple_operand_p (exp);
5027 : }
5028 :
5029 :
5030 : /* The following functions are subroutines to fold_range_test and allow it to
5031 : try to change a logical combination of comparisons into a range test.
5032 :
5033 : For example, both
5034 : X == 2 || X == 3 || X == 4 || X == 5
5035 : and
5036 : X >= 2 && X <= 5
5037 : are converted to
5038 : (unsigned) (X - 2) <= 3
5039 :
5040 : We describe each set of comparisons as being either inside or outside
5041 : a range, using a variable named like IN_P, and then describe the
5042 : range with a lower and upper bound. If one of the bounds is omitted,
5043 : it represents either the highest or lowest value of the type.
5044 :
5045 : In the comments below, we represent a range by two numbers in brackets
5046 : preceded by a "+" to designate being inside that range, or a "-" to
5047 : designate being outside that range, so the condition can be inverted by
5048 : flipping the prefix. An omitted bound is represented by a "-". For
5049 : example, "- [-, 10]" means being outside the range starting at the lowest
5050 : possible value and ending at 10, in other words, being greater than 10.
5051 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5052 : always false.
5053 :
5054 : We set up things so that the missing bounds are handled in a consistent
5055 : manner so neither a missing bound nor "true" and "false" need to be
5056 : handled using a special case. */
5057 :
5058 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5059 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5060 : and UPPER1_P are nonzero if the respective argument is an upper bound
5061 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5062 : must be specified for a comparison. ARG1 will be converted to ARG0's
5063 : type if both are specified. */
5064 :
5065 : static tree
5066 22289352 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5067 : tree arg1, int upper1_p)
5068 : {
5069 22289352 : tree tem;
5070 22289352 : int result;
5071 22289352 : int sgn0, sgn1;
5072 :
5073 : /* If neither arg represents infinity, do the normal operation.
5074 : Else, if not a comparison, return infinity. Else handle the special
5075 : comparison rules. Note that most of the cases below won't occur, but
5076 : are handled for consistency. */
5077 :
5078 22289352 : if (arg0 != 0 && arg1 != 0)
5079 : {
5080 11699824 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5081 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5082 11699824 : STRIP_NOPS (tem);
5083 11699824 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5084 : }
5085 :
5086 10589528 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5087 : return 0;
5088 :
5089 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5090 : for neither. In real maths, we cannot assume open ended ranges are
5091 : the same. But, this is computer arithmetic, where numbers are finite.
5092 : We can therefore make the transformation of any unbounded range with
5093 : the value Z, Z being greater than any representable number. This permits
5094 : us to treat unbounded ranges as equal. */
5095 10580895 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5096 10580895 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5097 10580895 : switch (code)
5098 : {
5099 4976431 : case EQ_EXPR:
5100 4976431 : result = sgn0 == sgn1;
5101 4976431 : break;
5102 0 : case NE_EXPR:
5103 0 : result = sgn0 != sgn1;
5104 0 : break;
5105 362117 : case LT_EXPR:
5106 362117 : result = sgn0 < sgn1;
5107 362117 : break;
5108 2423793 : case LE_EXPR:
5109 2423793 : result = sgn0 <= sgn1;
5110 2423793 : break;
5111 2818554 : case GT_EXPR:
5112 2818554 : result = sgn0 > sgn1;
5113 2818554 : break;
5114 0 : case GE_EXPR:
5115 0 : result = sgn0 >= sgn1;
5116 0 : break;
5117 0 : default:
5118 0 : gcc_unreachable ();
5119 : }
5120 :
5121 10580895 : return constant_boolean_node (result, type);
5122 : }
5123 :
5124 : /* Helper routine for make_range. Perform one step for it, return
5125 : new expression if the loop should continue or NULL_TREE if it should
5126 : stop. */
5127 :
5128 : tree
5129 58853028 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5130 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5131 : {
5132 58853028 : tree arg0_type = TREE_TYPE (arg0);
5133 58853028 : tree n_low, n_high, low = *p_low, high = *p_high;
5134 58853028 : int in_p = *p_in_p, n_in_p;
5135 :
5136 58853028 : switch (code)
5137 : {
5138 1603168 : case TRUTH_NOT_EXPR:
5139 : /* We can only do something if the range is testing for zero. */
5140 1603168 : if (low == NULL_TREE || high == NULL_TREE
5141 1603168 : || ! integer_zerop (low) || ! integer_zerop (high))
5142 0 : return NULL_TREE;
5143 1603168 : *p_in_p = ! in_p;
5144 1603168 : return arg0;
5145 :
5146 46006101 : case EQ_EXPR: case NE_EXPR:
5147 46006101 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5148 : /* We can only do something if the range is testing for zero
5149 : and if the second operand is an integer constant. Note that
5150 : saying something is "in" the range we make is done by
5151 : complementing IN_P since it will set in the initial case of
5152 : being not equal to zero; "out" is leaving it alone. */
5153 46006101 : if (low == NULL_TREE || high == NULL_TREE
5154 46006101 : || ! integer_zerop (low) || ! integer_zerop (high)
5155 92012114 : || TREE_CODE (arg1) != INTEGER_CST)
5156 17062012 : return NULL_TREE;
5157 :
5158 28944089 : switch (code)
5159 : {
5160 : case NE_EXPR: /* - [c, c] */
5161 : low = high = arg1;
5162 : break;
5163 7773718 : case EQ_EXPR: /* + [c, c] */
5164 7773718 : in_p = ! in_p, low = high = arg1;
5165 7773718 : break;
5166 2155444 : case GT_EXPR: /* - [-, c] */
5167 2155444 : low = 0, high = arg1;
5168 2155444 : break;
5169 712804 : case GE_EXPR: /* + [c, -] */
5170 712804 : in_p = ! in_p, low = arg1, high = 0;
5171 712804 : break;
5172 5653174 : case LT_EXPR: /* - [c, -] */
5173 5653174 : low = arg1, high = 0;
5174 5653174 : break;
5175 4339287 : case LE_EXPR: /* + [-, c] */
5176 4339287 : in_p = ! in_p, low = 0, high = arg1;
5177 4339287 : break;
5178 0 : default:
5179 0 : gcc_unreachable ();
5180 : }
5181 :
5182 : /* If this is an unsigned comparison, we also know that EXP is
5183 : greater than or equal to zero. We base the range tests we make
5184 : on that fact, so we record it here so we can parse existing
5185 : range tests. We test arg0_type since often the return type
5186 : of, e.g. EQ_EXPR, is boolean. */
5187 28944089 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5188 : {
5189 1846744 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5190 : in_p, low, high, 1,
5191 : build_int_cst (arg0_type, 0),
5192 : NULL_TREE))
5193 : return NULL_TREE;
5194 :
5195 1846735 : in_p = n_in_p, low = n_low, high = n_high;
5196 :
5197 : /* If the high bound is missing, but we have a nonzero low
5198 : bound, reverse the range so it goes from zero to the low bound
5199 : minus 1. */
5200 1846735 : if (high == 0 && low && ! integer_zerop (low))
5201 : {
5202 817546 : in_p = ! in_p;
5203 817546 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5204 817546 : build_int_cst (TREE_TYPE (low), 1), 0);
5205 817546 : low = build_int_cst (arg0_type, 0);
5206 : }
5207 : }
5208 :
5209 28944080 : *p_low = low;
5210 28944080 : *p_high = high;
5211 28944080 : *p_in_p = in_p;
5212 28944080 : return arg0;
5213 :
5214 290 : case NEGATE_EXPR:
5215 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5216 : low and high are non-NULL, then normalize will DTRT. */
5217 290 : if (!TYPE_UNSIGNED (arg0_type)
5218 290 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5219 : {
5220 95 : if (low == NULL_TREE)
5221 12 : low = TYPE_MIN_VALUE (arg0_type);
5222 95 : if (high == NULL_TREE)
5223 47 : high = TYPE_MAX_VALUE (arg0_type);
5224 : }
5225 :
5226 : /* (-x) IN [a,b] -> x in [-b, -a] */
5227 290 : n_low = range_binop (MINUS_EXPR, exp_type,
5228 : build_int_cst (exp_type, 0),
5229 : 0, high, 1);
5230 290 : n_high = range_binop (MINUS_EXPR, exp_type,
5231 : build_int_cst (exp_type, 0),
5232 : 0, low, 0);
5233 290 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5234 : return NULL_TREE;
5235 278 : goto normalize;
5236 :
5237 12 : case BIT_NOT_EXPR:
5238 : /* ~ X -> -X - 1 */
5239 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5240 : build_int_cst (exp_type, 1));
5241 :
5242 834815 : case PLUS_EXPR:
5243 834815 : case MINUS_EXPR:
5244 834815 : if (TREE_CODE (arg1) != INTEGER_CST)
5245 : return NULL_TREE;
5246 :
5247 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5248 : move a constant to the other side. */
5249 629363 : if (!TYPE_UNSIGNED (arg0_type)
5250 629363 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5251 : return NULL_TREE;
5252 :
5253 : /* If EXP is signed, any overflow in the computation is undefined,
5254 : so we don't worry about it so long as our computations on
5255 : the bounds don't overflow. For unsigned, overflow is defined
5256 : and this is exactly the right thing. */
5257 882127 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5258 : arg0_type, low, 0, arg1, 0);
5259 442562 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5260 : arg0_type, high, 1, arg1, 0);
5261 439474 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5262 882024 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5263 : return NULL_TREE;
5264 :
5265 442828 : normalize:
5266 : /* Check for an unsigned range which has wrapped around the maximum
5267 : value thus making n_high < n_low, and normalize it. */
5268 442828 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5269 : {
5270 117926 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5271 117926 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5272 117926 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5273 117926 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5274 :
5275 : /* If the range is of the form +/- [ x+1, x ], we won't
5276 : be able to normalize it. But then, it represents the
5277 : whole range or the empty set, so make it
5278 : +/- [ -, - ]. */
5279 117926 : if (tree_int_cst_equal (n_low, low)
5280 117926 : && tree_int_cst_equal (n_high, high))
5281 : low = high = 0;
5282 : else
5283 117926 : in_p = ! in_p;
5284 : }
5285 : else
5286 324902 : low = n_low, high = n_high;
5287 :
5288 442828 : *p_low = low;
5289 442828 : *p_high = high;
5290 442828 : *p_in_p = in_p;
5291 442828 : return arg0;
5292 :
5293 2473092 : CASE_CONVERT:
5294 2473092 : case NON_LVALUE_EXPR:
5295 2473092 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5296 : return NULL_TREE;
5297 :
5298 1150551 : if (! INTEGRAL_TYPE_P (arg0_type)
5299 1116139 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5300 1015509 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5301 : return NULL_TREE;
5302 :
5303 995770 : n_low = low, n_high = high;
5304 :
5305 995770 : if (n_low != 0)
5306 830400 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5307 :
5308 995770 : if (n_high != 0)
5309 932228 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5310 :
5311 : /* If we're converting arg0 from an unsigned type, to exp,
5312 : a signed type, we will be doing the comparison as unsigned.
5313 : The tests above have already verified that LOW and HIGH
5314 : are both positive.
5315 :
5316 : So we have to ensure that we will handle large unsigned
5317 : values the same way that the current signed bounds treat
5318 : negative values. */
5319 :
5320 995770 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5321 : {
5322 254340 : tree high_positive;
5323 254340 : tree equiv_type;
5324 : /* For fixed-point modes, we need to pass the saturating flag
5325 : as the 2nd parameter. */
5326 254340 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5327 0 : equiv_type
5328 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5329 0 : TYPE_SATURATING (arg0_type));
5330 254340 : else if (BITINT_TYPE_P (arg0_type))
5331 : equiv_type = arg0_type;
5332 : else
5333 254332 : equiv_type
5334 254332 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5335 :
5336 : /* A range without an upper bound is, naturally, unbounded.
5337 : Since convert would have cropped a very large value, use
5338 : the max value for the destination type. */
5339 254340 : high_positive
5340 254340 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5341 0 : : TYPE_MAX_VALUE (arg0_type);
5342 :
5343 254340 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5344 233345 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5345 : fold_convert_loc (loc, arg0_type,
5346 : high_positive),
5347 : build_int_cst (arg0_type, 1));
5348 :
5349 : /* If the low bound is specified, "and" the range with the
5350 : range for which the original unsigned value will be
5351 : positive. */
5352 254340 : if (low != 0)
5353 : {
5354 94405 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5355 : 1, fold_convert_loc (loc, arg0_type,
5356 : integer_zero_node),
5357 : high_positive))
5358 : return NULL_TREE;
5359 :
5360 94405 : in_p = (n_in_p == in_p);
5361 : }
5362 : else
5363 : {
5364 : /* Otherwise, "or" the range with the range of the input
5365 : that will be interpreted as negative. */
5366 159935 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5367 : 1, fold_convert_loc (loc, arg0_type,
5368 : integer_zero_node),
5369 : high_positive))
5370 : return NULL_TREE;
5371 :
5372 159935 : in_p = (in_p != n_in_p);
5373 : }
5374 : }
5375 :
5376 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5377 : an unsigned type, we will do the comparison as signed. If
5378 : high is non-NULL, we punt above if it doesn't fit in the signed
5379 : type, so if we get through here, +[-, high] or +[low, high] are
5380 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5381 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5382 : high is not, the +[low, -] range is equivalent to union of
5383 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5384 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5385 : low being 0, which should be treated as [-, -]. */
5386 741430 : else if (TYPE_UNSIGNED (exp_type)
5387 723039 : && !TYPE_UNSIGNED (arg0_type)
5388 374051 : && low
5389 1115481 : && !high)
5390 : {
5391 12 : if (integer_zerop (low))
5392 12 : n_low = NULL_TREE;
5393 : else
5394 : {
5395 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5396 : n_low, build_int_cst (arg0_type, -1));
5397 0 : n_low = build_zero_cst (arg0_type);
5398 0 : in_p = !in_p;
5399 : }
5400 : }
5401 :
5402 995770 : *p_low = n_low;
5403 995770 : *p_high = n_high;
5404 995770 : *p_in_p = in_p;
5405 995770 : return arg0;
5406 :
5407 : default:
5408 : return NULL_TREE;
5409 : }
5410 : }
5411 :
5412 : /* Given EXP, a logical expression, set the range it is testing into
5413 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5414 : actually being tested. *PLOW and *PHIGH will be made of the same
5415 : type as the returned expression. If EXP is not a comparison, we
5416 : will most likely not be returning a useful value and range. */
5417 :
5418 : tree
5419 48871120 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5420 : {
5421 48871120 : enum tree_code code;
5422 48871120 : tree arg0, arg1 = NULL_TREE;
5423 48871120 : tree exp_type, nexp;
5424 48871120 : int in_p;
5425 48871120 : tree low, high;
5426 48871120 : location_t loc = EXPR_LOCATION (exp);
5427 :
5428 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5429 : and see if we can refine the range. Some of the cases below may not
5430 : happen, but it doesn't seem worth worrying about this. We "continue"
5431 : the outer loop when we've changed something; otherwise we "break"
5432 : the switch, which will "break" the while. */
5433 :
5434 48871120 : in_p = 0;
5435 48871120 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5436 :
5437 77905033 : while (1)
5438 : {
5439 77905033 : code = TREE_CODE (exp);
5440 77905033 : exp_type = TREE_TYPE (exp);
5441 77905033 : arg0 = NULL_TREE;
5442 :
5443 77905033 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5444 : {
5445 54211947 : if (TREE_OPERAND_LENGTH (exp) > 0)
5446 54211947 : arg0 = TREE_OPERAND (exp, 0);
5447 54211947 : if (TREE_CODE_CLASS (code) == tcc_binary
5448 51250154 : || TREE_CODE_CLASS (code) == tcc_comparison
5449 63469580 : || (TREE_CODE_CLASS (code) == tcc_expression
5450 2747778 : && TREE_OPERAND_LENGTH (exp) > 1))
5451 46082256 : arg1 = TREE_OPERAND (exp, 1);
5452 : }
5453 54211947 : if (arg0 == NULL_TREE)
5454 : break;
5455 :
5456 54211933 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5457 : &high, &in_p);
5458 54211933 : if (nexp == NULL_TREE)
5459 : break;
5460 : exp = nexp;
5461 : }
5462 :
5463 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5464 48871120 : if (TREE_CODE (exp) == INTEGER_CST)
5465 : {
5466 30024 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5467 : exp, 0, low, 0))
5468 30024 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5469 : exp, 1, high, 1)));
5470 30024 : low = high = 0;
5471 30024 : exp = 0;
5472 : }
5473 :
5474 48871120 : *pin_p = in_p, *plow = low, *phigh = high;
5475 48871120 : return exp;
5476 : }
5477 :
5478 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5479 : a bitwise check i.e. when
5480 : LOW == 0xXX...X00...0
5481 : HIGH == 0xXX...X11...1
5482 : Return corresponding mask in MASK and stem in VALUE. */
5483 :
5484 : static bool
5485 125 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5486 : tree *value)
5487 : {
5488 125 : if (TREE_CODE (low) != INTEGER_CST
5489 125 : || TREE_CODE (high) != INTEGER_CST)
5490 : return false;
5491 :
5492 125 : unsigned prec = TYPE_PRECISION (type);
5493 125 : wide_int lo = wi::to_wide (low, prec);
5494 125 : wide_int hi = wi::to_wide (high, prec);
5495 :
5496 125 : wide_int end_mask = lo ^ hi;
5497 250 : if ((end_mask & (end_mask + 1)) != 0
5498 235 : || (lo & end_mask) != 0)
5499 : return false;
5500 :
5501 86 : wide_int stem_mask = ~end_mask;
5502 86 : wide_int stem = lo & stem_mask;
5503 86 : if (stem != (hi & stem_mask))
5504 : return false;
5505 :
5506 86 : *mask = wide_int_to_tree (type, stem_mask);
5507 86 : *value = wide_int_to_tree (type, stem);
5508 :
5509 86 : return true;
5510 211 : }
5511 :
5512 : /* Helper routine for build_range_check and match.pd. Return the type to
5513 : perform the check or NULL if it shouldn't be optimized. */
5514 :
5515 : tree
5516 565685 : range_check_type (tree etype)
5517 : {
5518 : /* First make sure that arithmetics in this type is valid, then make sure
5519 : that it wraps around. */
5520 565685 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5521 0 : etype = TREE_TYPE (etype);
5522 565685 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5523 57395 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5524 :
5525 565685 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5526 : {
5527 390989 : tree utype, minv, maxv;
5528 :
5529 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5530 : for the type in question, as we rely on this here. */
5531 390989 : utype = unsigned_type_for (etype);
5532 390989 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5533 390989 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5534 390989 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5535 390989 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5536 :
5537 390989 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5538 : minv, 1, maxv, 1)))
5539 : etype = utype;
5540 : else
5541 0 : return NULL_TREE;
5542 : }
5543 174696 : else if (POINTER_TYPE_P (etype)
5544 : || TREE_CODE (etype) == OFFSET_TYPE
5545 : /* Right now all BITINT_TYPEs satisfy
5546 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5547 : that like for INTEGER_TYPEs. */
5548 : || TREE_CODE (etype) == BITINT_TYPE)
5549 1366 : etype = unsigned_type_for (etype);
5550 : return etype;
5551 : }
5552 :
5553 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5554 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5555 : on IN_P) the range. Return 0 if the test couldn't be created. */
5556 :
5557 : tree
5558 1569513 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5559 : tree low, tree high)
5560 : {
5561 2719801 : tree etype = TREE_TYPE (exp), mask, value;
5562 :
5563 : /* Disable this optimization for function pointer expressions
5564 : on targets that require function pointer canonicalization. */
5565 2719801 : if (targetm.have_canonicalize_funcptr_for_compare ()
5566 0 : && POINTER_TYPE_P (etype)
5567 2719801 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5568 : return NULL_TREE;
5569 :
5570 2719801 : if (! in_p)
5571 : {
5572 308799 : value = build_range_check (loc, type, exp, 1, low, high);
5573 308799 : if (value != 0)
5574 308799 : return invert_truthvalue_loc (loc, value);
5575 :
5576 : return 0;
5577 : }
5578 :
5579 2411002 : if (low == 0 && high == 0)
5580 121932 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5581 :
5582 2289070 : if (low == 0)
5583 757197 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5584 757197 : fold_convert_loc (loc, etype, high));
5585 :
5586 1531873 : if (high == 0)
5587 72410 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5588 72410 : fold_convert_loc (loc, etype, low));
5589 :
5590 1459463 : if (operand_equal_p (low, high, 0))
5591 308987 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5592 308987 : fold_convert_loc (loc, etype, low));
5593 :
5594 1150476 : if (TREE_CODE (exp) == BIT_AND_EXPR
5595 1150476 : && maskable_range_p (low, high, etype, &mask, &value))
5596 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5597 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5598 : exp, mask),
5599 86 : value);
5600 :
5601 1150390 : if (integer_zerop (low))
5602 : {
5603 672166 : if (! TYPE_UNSIGNED (etype))
5604 : {
5605 177310 : etype = unsigned_type_for (etype);
5606 177310 : high = fold_convert_loc (loc, etype, high);
5607 177310 : exp = fold_convert_loc (loc, etype, exp);
5608 : }
5609 672166 : return build_range_check (loc, type, exp, 1, 0, high);
5610 : }
5611 :
5612 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5613 478224 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5614 : {
5615 124577 : int prec = TYPE_PRECISION (etype);
5616 :
5617 124577 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5618 : {
5619 102 : if (TYPE_UNSIGNED (etype))
5620 : {
5621 96 : tree signed_etype = signed_type_for (etype);
5622 96 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5623 0 : etype
5624 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5625 : else
5626 : etype = signed_etype;
5627 96 : exp = fold_convert_loc (loc, etype, exp);
5628 : }
5629 102 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5630 : build_int_cst (etype, 0));
5631 : }
5632 : }
5633 :
5634 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5635 : This requires wrap-around arithmetics for the type of the expression. */
5636 478122 : etype = range_check_type (etype);
5637 478122 : if (etype == NULL_TREE)
5638 : return NULL_TREE;
5639 :
5640 478122 : high = fold_convert_loc (loc, etype, high);
5641 478122 : low = fold_convert_loc (loc, etype, low);
5642 478122 : exp = fold_convert_loc (loc, etype, exp);
5643 :
5644 478122 : value = const_binop (MINUS_EXPR, high, low);
5645 :
5646 478122 : if (value != 0 && !TREE_OVERFLOW (value))
5647 478122 : return build_range_check (loc, type,
5648 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5649 : 1, build_int_cst (etype, 0), value);
5650 :
5651 : return 0;
5652 : }
5653 :
5654 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5655 :
5656 : static tree
5657 173514 : range_predecessor (tree val)
5658 : {
5659 173514 : tree type = TREE_TYPE (val);
5660 :
5661 173514 : if (INTEGRAL_TYPE_P (type)
5662 173514 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5663 : return 0;
5664 : else
5665 173514 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5666 173514 : build_int_cst (TREE_TYPE (val), 1), 0);
5667 : }
5668 :
5669 : /* Return the successor of VAL in its type, handling the infinite case. */
5670 :
5671 : static tree
5672 1596844 : range_successor (tree val)
5673 : {
5674 1596844 : tree type = TREE_TYPE (val);
5675 :
5676 1596844 : if (INTEGRAL_TYPE_P (type)
5677 1596844 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5678 : return 0;
5679 : else
5680 1596835 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5681 1596835 : build_int_cst (TREE_TYPE (val), 1), 0);
5682 : }
5683 :
5684 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5685 : can, 0 if we can't. Set the output range into the specified parameters. */
5686 :
5687 : bool
5688 3512348 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5689 : tree high0, int in1_p, tree low1, tree high1)
5690 : {
5691 3512348 : bool no_overlap;
5692 3512348 : int subset;
5693 3512348 : int temp;
5694 3512348 : tree tem;
5695 3512348 : int in_p;
5696 3512348 : tree low, high;
5697 3512348 : int lowequal = ((low0 == 0 && low1 == 0)
5698 3512348 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5699 3512348 : low0, 0, low1, 0)));
5700 3512348 : int highequal = ((high0 == 0 && high1 == 0)
5701 3512348 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5702 3512348 : high0, 1, high1, 1)));
5703 :
5704 : /* Make range 0 be the range that starts first, or ends last if they
5705 : start at the same value. Swap them if it isn't. */
5706 3512348 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5707 : low0, 0, low1, 0))
5708 3512348 : || (lowequal
5709 567335 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5710 : high1, 1, high0, 1))))
5711 : {
5712 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5713 : tem = low0, low0 = low1, low1 = tem;
5714 : tem = high0, high0 = high1, high1 = tem;
5715 : }
5716 :
5717 : /* If the second range is != high1 where high1 is the type maximum of
5718 : the type, try first merging with < high1 range. */
5719 3512348 : if (low1
5720 3512348 : && high1
5721 978570 : && TREE_CODE (low1) == INTEGER_CST
5722 978570 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5723 119650 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5724 158268 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5725 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5726 4450402 : && operand_equal_p (low1, high1, 0))
5727 : {
5728 547653 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5729 547653 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5730 : !in1_p, NULL_TREE, range_predecessor (low1)))
5731 : return true;
5732 : /* Similarly for the second range != low1 where low1 is the type minimum
5733 : of the type, try first merging with > low1 range. */
5734 438779 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5735 438779 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5736 : !in1_p, range_successor (low1), NULL_TREE))
5737 : return true;
5738 : }
5739 :
5740 : /* Now flag two cases, whether the ranges are disjoint or whether the
5741 : second range is totally subsumed in the first. Note that the tests
5742 : below are simplified by the ones above. */
5743 3310038 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5744 : high0, 1, low1, 0));
5745 3310038 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5746 : high1, 1, high0, 1));
5747 :
5748 : /* We now have four cases, depending on whether we are including or
5749 : excluding the two ranges. */
5750 3310038 : if (in0_p && in1_p)
5751 : {
5752 : /* If they don't overlap, the result is false. If the second range
5753 : is a subset it is the result. Otherwise, the range is from the start
5754 : of the second to the end of the first. */
5755 1508263 : if (no_overlap)
5756 : in_p = 0, low = high = 0;
5757 1506550 : else if (subset)
5758 : in_p = 1, low = low1, high = high1;
5759 : else
5760 1389647 : in_p = 1, low = low1, high = high0;
5761 : }
5762 :
5763 1801775 : else if (in0_p && ! in1_p)
5764 : {
5765 : /* If they don't overlap, the result is the first range. If they are
5766 : equal, the result is false. If the second range is a subset of the
5767 : first, and the ranges begin at the same place, we go from just after
5768 : the end of the second range to the end of the first. If the second
5769 : range is not a subset of the first, or if it is a subset and both
5770 : ranges end at the same place, the range starts at the start of the
5771 : first range and ends just before the second range.
5772 : Otherwise, we can't describe this as a single range. */
5773 315765 : if (no_overlap)
5774 : in_p = 1, low = low0, high = high0;
5775 310176 : else if (lowequal && highequal)
5776 : in_p = 0, low = high = 0;
5777 309392 : else if (subset && lowequal)
5778 : {
5779 233905 : low = range_successor (high1);
5780 233905 : high = high0;
5781 233905 : in_p = 1;
5782 233905 : if (low == 0)
5783 : {
5784 : /* We are in the weird situation where high0 > high1 but
5785 : high1 has no successor. Punt. */
5786 : return 0;
5787 : }
5788 : }
5789 75487 : else if (! subset || highequal)
5790 : {
5791 55588 : low = low0;
5792 55588 : high = range_predecessor (low1);
5793 55588 : in_p = 1;
5794 55588 : if (high == 0)
5795 : {
5796 : /* low0 < low1 but low1 has no predecessor. Punt. */
5797 : return 0;
5798 : }
5799 : }
5800 : else
5801 : return 0;
5802 : }
5803 :
5804 1486010 : else if (! in0_p && in1_p)
5805 : {
5806 : /* If they don't overlap, the result is the second range. If the second
5807 : is a subset of the first, the result is false. Otherwise,
5808 : the range starts just after the first range and ends at the
5809 : end of the second. */
5810 1133372 : if (no_overlap)
5811 : in_p = 1, low = low1, high = high1;
5812 1126059 : else if (subset || highequal)
5813 : in_p = 0, low = high = 0;
5814 : else
5815 : {
5816 1018080 : low = range_successor (high0);
5817 1018080 : high = high1;
5818 1018080 : in_p = 1;
5819 1018080 : if (low == 0)
5820 : {
5821 : /* high1 > high0 but high0 has no successor. Punt. */
5822 : return 0;
5823 : }
5824 : }
5825 : }
5826 :
5827 : else
5828 : {
5829 : /* The case where we are excluding both ranges. Here the complex case
5830 : is if they don't overlap. In that case, the only time we have a
5831 : range is if they are adjacent. If the second is a subset of the
5832 : first, the result is the first. Otherwise, the range to exclude
5833 : starts at the beginning of the first range and ends at the end of the
5834 : second. */
5835 352638 : if (no_overlap)
5836 : {
5837 251093 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5838 : range_successor (high0),
5839 : 1, low1, 0)))
5840 : in_p = 0, low = low0, high = high1;
5841 : else
5842 : {
5843 : /* Canonicalize - [min, x] into - [-, x]. */
5844 203947 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5845 202872 : switch (TREE_CODE (TREE_TYPE (low0)))
5846 : {
5847 47247 : case ENUMERAL_TYPE:
5848 47247 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5849 : GET_MODE_BITSIZE
5850 94494 : (TYPE_MODE (TREE_TYPE (low0)))))
5851 : break;
5852 : /* FALLTHROUGH */
5853 202671 : case INTEGER_TYPE:
5854 202671 : if (tree_int_cst_equal (low0,
5855 202671 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5856 6415 : low0 = 0;
5857 : break;
5858 201 : case POINTER_TYPE:
5859 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5860 201 : && integer_zerop (low0))
5861 : low0 = 0;
5862 : break;
5863 : default:
5864 : break;
5865 : }
5866 :
5867 : /* Canonicalize - [x, max] into - [x, -]. */
5868 203947 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5869 203761 : switch (TREE_CODE (TREE_TYPE (high1)))
5870 : {
5871 47255 : case ENUMERAL_TYPE:
5872 47255 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5873 : GET_MODE_BITSIZE
5874 94510 : (TYPE_MODE (TREE_TYPE (high1)))))
5875 : break;
5876 : /* FALLTHROUGH */
5877 203560 : case INTEGER_TYPE:
5878 203560 : if (tree_int_cst_equal (high1,
5879 203560 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5880 9042 : high1 = 0;
5881 : break;
5882 201 : case POINTER_TYPE:
5883 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5884 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5885 : high1, 1,
5886 201 : build_int_cst (TREE_TYPE (high1), 1),
5887 : 1)))
5888 133 : high1 = 0;
5889 : break;
5890 : default:
5891 : break;
5892 : }
5893 :
5894 : /* The ranges might be also adjacent between the maximum and
5895 : minimum values of the given type. For
5896 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5897 : return + [x + 1, y - 1]. */
5898 203947 : if (low0 == 0 && high1 == 0)
5899 : {
5900 330 : low = range_successor (high0);
5901 330 : high = range_predecessor (low1);
5902 330 : if (low == 0 || high == 0)
5903 : return 0;
5904 :
5905 : in_p = 1;
5906 : }
5907 : else
5908 : return 0;
5909 : }
5910 : }
5911 101545 : else if (subset)
5912 : in_p = 0, low = low0, high = high0;
5913 : else
5914 11720 : in_p = 0, low = low0, high = high1;
5915 : }
5916 :
5917 3086513 : *pin_p = in_p, *plow = low, *phigh = high;
5918 3086513 : return 1;
5919 : }
5920 :
5921 :
5922 : /* Subroutine of fold, looking inside expressions of the form
5923 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5924 : are the three operands of the COND_EXPR. This function is
5925 : being used also to optimize A op B ? C : A, by reversing the
5926 : comparison first.
5927 :
5928 : Return a folded expression whose code is not a COND_EXPR
5929 : anymore, or NULL_TREE if no folding opportunity is found. */
5930 :
5931 : static tree
5932 501884 : fold_cond_expr_with_comparison (location_t loc, tree type,
5933 : enum tree_code comp_code,
5934 : tree arg00, tree arg01, tree arg1, tree arg2)
5935 : {
5936 501884 : tree arg1_type = TREE_TYPE (arg1);
5937 501884 : tree tem;
5938 :
5939 501884 : STRIP_NOPS (arg1);
5940 501884 : STRIP_NOPS (arg2);
5941 :
5942 : /* If we have A op 0 ? A : -A, consider applying the following
5943 : transformations:
5944 :
5945 : A == 0? A : -A same as -A
5946 : A != 0? A : -A same as A
5947 : A >= 0? A : -A same as abs (A)
5948 : A > 0? A : -A same as abs (A)
5949 : A <= 0? A : -A same as -abs (A)
5950 : A < 0? A : -A same as -abs (A)
5951 :
5952 : None of these transformations work for modes with signed
5953 : zeros. If A is +/-0, the first two transformations will
5954 : change the sign of the result (from +0 to -0, or vice
5955 : versa). The last four will fix the sign of the result,
5956 : even though the original expressions could be positive or
5957 : negative, depending on the sign of A.
5958 :
5959 : Note that all these transformations are correct if A is
5960 : NaN, since the two alternatives (A and -A) are also NaNs. */
5961 501884 : if (!HONOR_SIGNED_ZEROS (type)
5962 1003778 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5963 501884 : ? real_zerop (arg01)
5964 500824 : : integer_zerop (arg01))
5965 1349230 : && ((TREE_CODE (arg2) == NEGATE_EXPR
5966 1567 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5967 : /* In the case that A is of the form X-Y, '-A' (arg2) may
5968 : have already been folded to Y-X, check for that. */
5969 344119 : || (TREE_CODE (arg1) == MINUS_EXPR
5970 1718 : && TREE_CODE (arg2) == MINUS_EXPR
5971 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
5972 0 : TREE_OPERAND (arg2, 1), 0)
5973 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
5974 0 : TREE_OPERAND (arg2, 0), 0))))
5975 1343 : switch (comp_code)
5976 : {
5977 0 : case EQ_EXPR:
5978 0 : case UNEQ_EXPR:
5979 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
5980 0 : return fold_convert_loc (loc, type, negate_expr (tem));
5981 0 : case NE_EXPR:
5982 0 : case LTGT_EXPR:
5983 0 : return fold_convert_loc (loc, type, arg1);
5984 0 : case UNGE_EXPR:
5985 0 : case UNGT_EXPR:
5986 0 : if (flag_trapping_math)
5987 : break;
5988 : /* Fall through. */
5989 1327 : case GE_EXPR:
5990 1327 : case GT_EXPR:
5991 1327 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5992 : break;
5993 1311 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5994 1311 : return fold_convert_loc (loc, type, tem);
5995 0 : case UNLE_EXPR:
5996 0 : case UNLT_EXPR:
5997 0 : if (flag_trapping_math)
5998 : break;
5999 : /* FALLTHRU */
6000 16 : case LE_EXPR:
6001 16 : case LT_EXPR:
6002 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6003 : break;
6004 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6005 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6006 : {
6007 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6008 : is not, invokes UB both in abs and in the negation of it.
6009 : So, use ABSU_EXPR instead. */
6010 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6011 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6012 16 : tem = negate_expr (tem);
6013 16 : return fold_convert_loc (loc, type, tem);
6014 : }
6015 : else
6016 : {
6017 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6018 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6019 : }
6020 0 : default:
6021 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6022 : break;
6023 : }
6024 :
6025 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6026 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6027 : both transformations are correct when A is NaN: A != 0
6028 : is then true, and A == 0 is false. */
6029 :
6030 500557 : if (!HONOR_SIGNED_ZEROS (type)
6031 500557 : && integer_zerop (arg01) && integer_zerop (arg2))
6032 : {
6033 262852 : if (comp_code == NE_EXPR)
6034 147 : return fold_convert_loc (loc, type, arg1);
6035 262705 : else if (comp_code == EQ_EXPR)
6036 0 : return build_zero_cst (type);
6037 : }
6038 :
6039 : /* Try some transformations of A op B ? A : B.
6040 :
6041 : A == B? A : B same as B
6042 : A != B? A : B same as A
6043 : A >= B? A : B same as max (A, B)
6044 : A > B? A : B same as max (B, A)
6045 : A <= B? A : B same as min (A, B)
6046 : A < B? A : B same as min (B, A)
6047 :
6048 : As above, these transformations don't work in the presence
6049 : of signed zeros. For example, if A and B are zeros of
6050 : opposite sign, the first two transformations will change
6051 : the sign of the result. In the last four, the original
6052 : expressions give different results for (A=+0, B=-0) and
6053 : (A=-0, B=+0), but the transformed expressions do not.
6054 :
6055 : The first two transformations are correct if either A or B
6056 : is a NaN. In the first transformation, the condition will
6057 : be false, and B will indeed be chosen. In the case of the
6058 : second transformation, the condition A != B will be true,
6059 : and A will be chosen.
6060 :
6061 : The conversions to max() and min() are not correct if B is
6062 : a number and A is not. The conditions in the original
6063 : expressions will be false, so all four give B. The min()
6064 : and max() versions would give a NaN instead. */
6065 500410 : if (!HONOR_SIGNED_ZEROS (type)
6066 500410 : && operand_equal_for_comparison_p (arg01, arg2)
6067 : /* Avoid these transformations if the COND_EXPR may be used
6068 : as an lvalue in the C++ front-end. PR c++/19199. */
6069 771821 : && (in_gimple_form
6070 17174 : || VECTOR_TYPE_P (type)
6071 17112 : || (! lang_GNU_CXX ()
6072 14711 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6073 2401 : || ! maybe_lvalue_p (arg1)
6074 2380 : || ! maybe_lvalue_p (arg2)))
6075 : {
6076 269718 : tree comp_op0 = arg00;
6077 269718 : tree comp_op1 = arg01;
6078 269718 : tree comp_type = TREE_TYPE (comp_op0);
6079 :
6080 269718 : switch (comp_code)
6081 : {
6082 0 : case EQ_EXPR:
6083 0 : return fold_convert_loc (loc, type, arg2);
6084 1 : case NE_EXPR:
6085 1 : return fold_convert_loc (loc, type, arg1);
6086 5967 : case LE_EXPR:
6087 5967 : case LT_EXPR:
6088 5967 : case UNLE_EXPR:
6089 5967 : case UNLT_EXPR:
6090 : /* In C++ a ?: expression can be an lvalue, so put the
6091 : operand which will be used if they are equal first
6092 : so that we can convert this back to the
6093 : corresponding COND_EXPR. */
6094 5967 : if (!HONOR_NANS (arg1))
6095 : {
6096 5967 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6097 5967 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6098 5967 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6099 5967 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6100 4621 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6101 : comp_op1, comp_op0);
6102 5967 : return fold_convert_loc (loc, type, tem);
6103 : }
6104 : break;
6105 263750 : case GE_EXPR:
6106 263750 : case GT_EXPR:
6107 263750 : case UNGE_EXPR:
6108 263750 : case UNGT_EXPR:
6109 263750 : if (!HONOR_NANS (arg1))
6110 : {
6111 263748 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6112 263748 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6113 263748 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6114 263748 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6115 3645 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6116 : comp_op1, comp_op0);
6117 263748 : return fold_convert_loc (loc, type, tem);
6118 : }
6119 : break;
6120 0 : case UNEQ_EXPR:
6121 0 : if (!HONOR_NANS (arg1))
6122 0 : return fold_convert_loc (loc, type, arg2);
6123 : break;
6124 0 : case LTGT_EXPR:
6125 0 : if (!HONOR_NANS (arg1))
6126 0 : return fold_convert_loc (loc, type, arg1);
6127 : break;
6128 0 : default:
6129 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6130 : break;
6131 : }
6132 : }
6133 :
6134 : return NULL_TREE;
6135 : }
6136 :
6137 :
6138 :
6139 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6140 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6141 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6142 : false) >= 2)
6143 : #endif
6144 :
6145 : /* EXP is some logical combination of boolean tests. See if we can
6146 : merge it into some range test. Return the new tree if so. */
6147 :
6148 : static tree
6149 24435054 : fold_range_test (location_t loc, enum tree_code code, tree type,
6150 : tree op0, tree op1)
6151 : {
6152 24435054 : int or_op = (code == TRUTH_ORIF_EXPR
6153 24435054 : || code == TRUTH_OR_EXPR);
6154 24435054 : int in0_p, in1_p, in_p;
6155 24435054 : tree low0, low1, low, high0, high1, high;
6156 24435054 : tree tem, lhs, rhs;
6157 :
6158 24435054 : if (!INTEGRAL_TYPE_P (type))
6159 : return 0;
6160 :
6161 24435054 : lhs = make_range (op0, &in0_p, &low0, &high0);
6162 : /* If op0 is known true or false and this is a short-circuiting
6163 : operation we must not merge with op1 since that makes side-effects
6164 : unconditional. So special-case this. */
6165 24435054 : if (!lhs
6166 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6167 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6168 : return op0;
6169 24435052 : rhs = make_range (op1, &in1_p, &low1, &high1);
6170 :
6171 : /* If this is an OR operation, invert both sides; we will invert
6172 : again at the end. */
6173 24435052 : if (or_op)
6174 11497888 : in0_p = ! in0_p, in1_p = ! in1_p;
6175 :
6176 : /* If both expressions are the same, if we can merge the ranges, and we
6177 : can build the range test, return it or it inverted. If one of the
6178 : ranges is always true or always false, consider it to be the same
6179 : expression as the other. */
6180 24405032 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6181 1165576 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6182 : in1_p, low1, high1)
6183 25406702 : && (tem = (build_range_check (loc, type,
6184 : lhs != 0 ? lhs
6185 0 : : rhs != 0 ? rhs : integer_zero_node,
6186 : in_p, low, high))) != 0)
6187 : {
6188 971650 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6189 : }
6190 :
6191 : /* On machines where the branch cost is expensive, if this is a
6192 : short-circuited branch and the underlying object on both sides
6193 : is the same, make a non-short-circuit operation. */
6194 23463402 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6195 23463402 : if (param_logical_op_non_short_circuit != -1)
6196 7823 : logical_op_non_short_circuit
6197 7823 : = param_logical_op_non_short_circuit;
6198 23463402 : if (logical_op_non_short_circuit
6199 23459484 : && !sanitize_coverage_p ()
6200 23459481 : && lhs != 0 && rhs != 0
6201 23459173 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6202 28691656 : && operand_equal_p (lhs, rhs, 0))
6203 : {
6204 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6205 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6206 : which cases we can't do this. */
6207 164523 : if (simple_operand_p (lhs))
6208 67119 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6209 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6210 34075 : type, op0, op1);
6211 :
6212 130448 : else if (!lang_hooks.decls.global_bindings_p ()
6213 130448 : && !CONTAINS_PLACEHOLDER_P (lhs))
6214 : {
6215 129795 : tree common = save_expr (lhs);
6216 :
6217 239163 : if ((lhs = build_range_check (loc, type, common,
6218 109368 : or_op ? ! in0_p : in0_p,
6219 : low0, high0)) != 0
6220 239163 : && (rhs = build_range_check (loc, type, common,
6221 109368 : or_op ? ! in1_p : in1_p,
6222 : low1, high1)) != 0)
6223 : {
6224 239163 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6225 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6226 129795 : type, lhs, rhs);
6227 : }
6228 : }
6229 : }
6230 :
6231 : return 0;
6232 : }
6233 :
6234 : /* For an expression that has the form
6235 : (A && B) || ~B
6236 : or
6237 : (A || B) && ~B,
6238 : we can drop one of the inner expressions and simplify to
6239 : A || ~B
6240 : or
6241 : A && ~B
6242 : LOC is the location of the resulting expression. OP is the inner
6243 : logical operation; the left-hand side in the examples above, while CMPOP
6244 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6245 : removing a condition that guards another, as in
6246 : (A != NULL && A->...) || A == NULL
6247 : which we must not transform. If RHS_ONLY is true, only eliminate the
6248 : right-most operand of the inner logical operation. */
6249 :
6250 : static tree
6251 123814 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6252 : bool rhs_only)
6253 : {
6254 123814 : enum tree_code code = TREE_CODE (cmpop);
6255 123814 : enum tree_code truthop_code = TREE_CODE (op);
6256 123814 : tree lhs = TREE_OPERAND (op, 0);
6257 123814 : tree rhs = TREE_OPERAND (op, 1);
6258 123814 : tree orig_lhs = lhs, orig_rhs = rhs;
6259 123814 : enum tree_code rhs_code = TREE_CODE (rhs);
6260 123814 : enum tree_code lhs_code = TREE_CODE (lhs);
6261 123814 : enum tree_code inv_code;
6262 :
6263 123814 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6264 : return NULL_TREE;
6265 :
6266 111428 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6267 : return NULL_TREE;
6268 :
6269 38571 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6270 :
6271 38571 : if (rhs_code == truthop_code)
6272 : {
6273 33 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6274 33 : if (newrhs != NULL_TREE)
6275 : {
6276 0 : rhs = newrhs;
6277 0 : rhs_code = TREE_CODE (rhs);
6278 : }
6279 : }
6280 38571 : if (lhs_code == truthop_code && !rhs_only)
6281 : {
6282 476 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6283 476 : if (newlhs != NULL_TREE)
6284 : {
6285 0 : lhs = newlhs;
6286 0 : lhs_code = TREE_CODE (lhs);
6287 : }
6288 : }
6289 :
6290 38571 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6291 38571 : if (inv_code == rhs_code
6292 922 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6293 38607 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6294 : return lhs;
6295 38558 : if (!rhs_only && inv_code == lhs_code
6296 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6297 38650 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6298 : return rhs;
6299 38467 : if (rhs != orig_rhs || lhs != orig_lhs)
6300 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6301 0 : lhs, rhs);
6302 : return NULL_TREE;
6303 : }
6304 :
6305 : /* Find ways of folding logical expressions of LHS and RHS:
6306 : Try to merge two comparisons to the same innermost item.
6307 : Look for range tests like "ch >= '0' && ch <= '9'".
6308 : Look for combinations of simple terms on machines with expensive branches
6309 : and evaluate the RHS unconditionally.
6310 :
6311 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6312 : function and the one above.
6313 :
6314 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6315 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6316 :
6317 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6318 : two operands.
6319 :
6320 : We return the simplified tree or 0 if no optimization is possible. */
6321 :
6322 : static tree
6323 24113057 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6324 : tree lhs, tree rhs)
6325 : {
6326 : /* If this is the "or" of two comparisons, we can do something if
6327 : the comparisons are NE_EXPR. If this is the "and", we can do something
6328 : if the comparisons are EQ_EXPR. I.e.,
6329 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6330 :
6331 : WANTED_CODE is this operation code. For single bit fields, we can
6332 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6333 : comparison for one-bit fields. */
6334 :
6335 24113057 : enum tree_code lcode, rcode;
6336 24113057 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6337 24113057 : tree result;
6338 :
6339 : /* Start by getting the comparison codes. Fail if anything is volatile.
6340 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6341 : it were surrounded with a NE_EXPR. */
6342 :
6343 24113057 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6344 : return 0;
6345 :
6346 21403625 : lcode = TREE_CODE (lhs);
6347 21403625 : rcode = TREE_CODE (rhs);
6348 :
6349 21403625 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6350 : {
6351 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6352 0 : build_int_cst (TREE_TYPE (lhs), 0));
6353 0 : lcode = NE_EXPR;
6354 : }
6355 :
6356 21403625 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6357 : {
6358 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6359 0 : build_int_cst (TREE_TYPE (rhs), 0));
6360 0 : rcode = NE_EXPR;
6361 : }
6362 :
6363 21403625 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6364 19138417 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6365 : return 0;
6366 :
6367 18059155 : ll_arg = TREE_OPERAND (lhs, 0);
6368 18059155 : lr_arg = TREE_OPERAND (lhs, 1);
6369 18059155 : rl_arg = TREE_OPERAND (rhs, 0);
6370 18059155 : rr_arg = TREE_OPERAND (rhs, 1);
6371 :
6372 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6373 18059155 : if (simple_operand_p (ll_arg)
6374 18059155 : && simple_operand_p (lr_arg))
6375 : {
6376 14660864 : if (operand_equal_p (ll_arg, rl_arg, 0)
6377 14660864 : && operand_equal_p (lr_arg, rr_arg, 0))
6378 : {
6379 21193 : result = combine_comparisons (loc, code, lcode, rcode,
6380 : truth_type, ll_arg, lr_arg);
6381 21193 : if (result)
6382 : return result;
6383 : }
6384 14639671 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6385 14639671 : && operand_equal_p (lr_arg, rl_arg, 0))
6386 : {
6387 252 : result = combine_comparisons (loc, code, lcode,
6388 : swap_tree_comparison (rcode),
6389 : truth_type, ll_arg, lr_arg);
6390 252 : if (result)
6391 : return result;
6392 : }
6393 : }
6394 :
6395 8544553 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6396 18038255 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6397 :
6398 : /* If the RHS can be evaluated unconditionally and its operands are
6399 : simple, it wins to evaluate the RHS unconditionally on machines
6400 : with expensive branches. In this case, this isn't a comparison
6401 : that can be merged. */
6402 :
6403 18038255 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6404 : false) >= 2
6405 18038152 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6406 17033996 : && simple_operand_p (rl_arg)
6407 27885047 : && simple_operand_p (rr_arg))
6408 : {
6409 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6410 11018643 : if (code == TRUTH_OR_EXPR
6411 1505246 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6412 618804 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6413 23953 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6414 11038507 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6415 39112 : return build2_loc (loc, NE_EXPR, truth_type,
6416 19556 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6417 : ll_arg, rl_arg),
6418 19556 : build_int_cst (TREE_TYPE (ll_arg), 0));
6419 :
6420 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6421 10999087 : if (code == TRUTH_AND_EXPR
6422 1687950 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6423 810237 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6424 6488 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6425 11000609 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6426 2628 : return build2_loc (loc, EQ_EXPR, truth_type,
6427 1314 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6428 : ll_arg, rl_arg),
6429 1314 : build_int_cst (TREE_TYPE (ll_arg), 0));
6430 : }
6431 :
6432 : return 0;
6433 : }
6434 :
6435 : /* T is an integer expression that is being multiplied, divided, or taken a
6436 : modulus (CODE says which and what kind of divide or modulus) by a
6437 : constant C. See if we can eliminate that operation by folding it with
6438 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6439 : should be used for the computation if wider than our type.
6440 :
6441 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6442 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6443 : expression would not overflow or that overflow is undefined for the type
6444 : in the language in question.
6445 :
6446 : If we return a non-null expression, it is an equivalent form of the
6447 : original computation, but need not be in the original type. */
6448 :
6449 : static tree
6450 101404159 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6451 : {
6452 : /* To avoid exponential search depth, refuse to allow recursion past
6453 : three levels. Beyond that (1) it's highly unlikely that we'll find
6454 : something interesting and (2) we've probably processed it before
6455 : when we built the inner expression. */
6456 :
6457 101404159 : static int depth;
6458 101404159 : tree ret;
6459 :
6460 101404159 : if (depth > 3)
6461 : return NULL;
6462 :
6463 97654809 : depth++;
6464 97654809 : ret = extract_muldiv_1 (t, c, code, wide_type);
6465 97654809 : depth--;
6466 :
6467 97654809 : return ret;
6468 : }
6469 :
6470 : static tree
6471 97654809 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6472 : {
6473 97654809 : tree type = TREE_TYPE (t);
6474 97654809 : enum tree_code tcode = TREE_CODE (t);
6475 97654809 : tree ctype = type;
6476 97654809 : if (wide_type)
6477 : {
6478 32220496 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6479 : {
6480 160 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6481 9142167 : ctype = wide_type;
6482 : }
6483 32220336 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6484 64440672 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6485 9142167 : ctype = wide_type;
6486 : }
6487 97654809 : tree t1, t2;
6488 97654809 : bool same_p = tcode == code;
6489 97654809 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6490 :
6491 : /* Don't deal with constants of zero here; they confuse the code below. */
6492 97654809 : if (integer_zerop (c))
6493 : return NULL_TREE;
6494 :
6495 97639745 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6496 38760884 : op0 = TREE_OPERAND (t, 0);
6497 :
6498 97639745 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6499 12400274 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6500 :
6501 : /* Note that we need not handle conditional operations here since fold
6502 : already handles those cases. So just do arithmetic here. */
6503 97639745 : switch (tcode)
6504 : {
6505 4495972 : case INTEGER_CST:
6506 : /* For a constant, we can always simplify if we are a multiply
6507 : or (for divide and modulus) if it is a multiple of our constant. */
6508 4495972 : if (code == MULT_EXPR
6509 5694123 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6510 1198151 : TYPE_SIGN (type)))
6511 : {
6512 3713105 : tree tem = const_binop (code, fold_convert (ctype, t),
6513 : fold_convert (ctype, c));
6514 : /* If the multiplication overflowed, we lost information on it.
6515 : See PR68142 and PR69845. */
6516 3713105 : if (TREE_OVERFLOW (tem))
6517 : return NULL_TREE;
6518 : return tem;
6519 : }
6520 : break;
6521 :
6522 38129068 : CASE_CONVERT: case NON_LVALUE_EXPR:
6523 38129068 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6524 : break;
6525 : /* If op0 is an expression ... */
6526 36907181 : if ((COMPARISON_CLASS_P (op0)
6527 : || UNARY_CLASS_P (op0)
6528 36907181 : || BINARY_CLASS_P (op0)
6529 33907772 : || VL_EXP_CLASS_P (op0)
6530 33848769 : || EXPRESSION_CLASS_P (op0))
6531 : /* ... and has wrapping overflow, and its type is smaller
6532 : than ctype, then we cannot pass through as widening. */
6533 37056338 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6534 1120757 : && (TYPE_PRECISION (ctype)
6535 1120757 : > TYPE_PRECISION (TREE_TYPE (op0))))
6536 : /* ... or this is a truncation (t is narrower than op0),
6537 : then we cannot pass through this narrowing. */
6538 2599888 : || (TYPE_PRECISION (type)
6539 2599888 : < TYPE_PRECISION (TREE_TYPE (op0)))
6540 : /* ... or signedness changes for division or modulus,
6541 : then we cannot pass through this conversion. */
6542 2555520 : || (code != MULT_EXPR
6543 123974 : && (TYPE_UNSIGNED (ctype)
6544 123974 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6545 : /* ... or has undefined overflow while the converted to
6546 : type has not, we cannot do the operation in the inner type
6547 : as that would introduce undefined overflow. */
6548 2456897 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6549 1920312 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6550 : break;
6551 :
6552 : /* Pass the constant down and see if we can make a simplification. If
6553 : we can, replace this expression with the inner simplification for
6554 : possible later conversion to our or some other type. */
6555 34364414 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6556 34364414 : && TREE_CODE (t2) == INTEGER_CST
6557 34364414 : && !TREE_OVERFLOW (t2)
6558 69976524 : && (t1 = extract_muldiv (op0, t2, code,
6559 : code == MULT_EXPR ? ctype : NULL_TREE))
6560 : != 0)
6561 : return t1;
6562 : break;
6563 :
6564 185 : case ABS_EXPR:
6565 : /* If widening the type changes it from signed to unsigned, then we
6566 : must avoid building ABS_EXPR itself as unsigned. */
6567 185 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6568 : {
6569 0 : tree cstype = (*signed_type_for) (ctype);
6570 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6571 : {
6572 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6573 0 : return fold_convert (ctype, t1);
6574 : }
6575 : break;
6576 : }
6577 : /* If the constant is negative, we cannot simplify this. */
6578 185 : if (tree_int_cst_sgn (c) == -1)
6579 : break;
6580 : /* FALLTHROUGH */
6581 48576 : case NEGATE_EXPR:
6582 : /* For division and modulus, type can't be unsigned, as e.g.
6583 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6584 : For signed types, even with wrapping overflow, this is fine. */
6585 48576 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6586 : break;
6587 47034 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6588 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6589 : break;
6590 :
6591 785 : case MIN_EXPR: case MAX_EXPR:
6592 : /* If widening the type changes the signedness, then we can't perform
6593 : this optimization as that changes the result. */
6594 785 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6595 : break;
6596 :
6597 : /* Punt for multiplication altogether.
6598 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6599 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6600 : 0U, the latter is 2U.
6601 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6602 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6603 : well defined 0, the latter invokes UB.
6604 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6605 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6606 : well defined 25, the latter invokes UB. */
6607 785 : if (code == MULT_EXPR)
6608 : break;
6609 : /* For division/modulo, punt on c being -1 for MAX, as
6610 : MAX (INT_MIN, 0) / -1 is not equivalent to
6611 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6612 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6613 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6614 : transformation won't make it worse. */
6615 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6616 : break;
6617 :
6618 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6619 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6620 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6621 : {
6622 0 : if (tree_int_cst_sgn (c) < 0)
6623 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6624 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6625 : fold_convert (ctype, t2));
6626 : }
6627 : break;
6628 :
6629 1347 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6630 : /* If the second operand is constant, this is a multiplication
6631 : or floor division, by a power of two, so we can treat it that
6632 : way unless the multiplier or divisor overflows. Signed
6633 : left-shift overflow is implementation-defined rather than
6634 : undefined in C90, so do not convert signed left shift into
6635 : multiplication. */
6636 1347 : if (TREE_CODE (op1) == INTEGER_CST
6637 1331 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6638 : /* const_binop may not detect overflow correctly,
6639 : so check for it explicitly here. */
6640 1213 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6641 1356 : wi::to_wide (op1))
6642 1204 : && (t1 = fold_convert (ctype,
6643 : const_binop (LSHIFT_EXPR, size_one_node,
6644 : op1))) != 0
6645 2551 : && !TREE_OVERFLOW (t1))
6646 2206 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6647 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6648 : ctype,
6649 : fold_convert (ctype, op0),
6650 : t1),
6651 1204 : c, code, wide_type);
6652 : break;
6653 :
6654 8601835 : case PLUS_EXPR: case MINUS_EXPR:
6655 : /* See if we can eliminate the operation on both sides. If we can, we
6656 : can return a new PLUS or MINUS. If we can't, the only remaining
6657 : cases where we can do anything are if the second operand is a
6658 : constant. */
6659 8601835 : t1 = extract_muldiv (op0, c, code, wide_type);
6660 8601835 : t2 = extract_muldiv (op1, c, code, wide_type);
6661 836516 : if (t1 != 0 && t2 != 0
6662 284984 : && TYPE_OVERFLOW_WRAPS (ctype)
6663 8877495 : && (code == MULT_EXPR
6664 : /* If not multiplication, we can only do this if both operands
6665 : are divisible by c. */
6666 0 : || (multiple_of_p (ctype, op0, c)
6667 0 : && multiple_of_p (ctype, op1, c))))
6668 : {
6669 275660 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6670 : fold_convert (ctype, t2));
6671 : }
6672 :
6673 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6674 : This simplifies the logic below. */
6675 8326175 : if (tcode == MINUS_EXPR)
6676 : {
6677 2188965 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6678 : /* If OP1 was not easily negatable, the constant may be OP0. */
6679 2188965 : if (TREE_CODE (op0) == INTEGER_CST)
6680 : {
6681 363885 : std::swap (op0, op1);
6682 363885 : std::swap (t1, t2);
6683 : }
6684 : }
6685 :
6686 8326175 : if (TREE_CODE (op1) != INTEGER_CST)
6687 : break;
6688 :
6689 : /* If either OP1 or C are negative, this optimization is not safe for
6690 : some of the division and remainder types while for others we need
6691 : to change the code. */
6692 3860352 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6693 : {
6694 184634 : if (code == CEIL_DIV_EXPR)
6695 : code = FLOOR_DIV_EXPR;
6696 184632 : else if (code == FLOOR_DIV_EXPR)
6697 : code = CEIL_DIV_EXPR;
6698 184219 : else if (code != MULT_EXPR
6699 184219 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6700 : break;
6701 : }
6702 :
6703 : /* If it's a multiply or a division/modulus operation of a multiple
6704 : of our constant, do the operation and verify it doesn't overflow. */
6705 3855141 : if (code == MULT_EXPR
6706 5048817 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6707 1193676 : TYPE_SIGN (type)))
6708 : {
6709 3072783 : op1 = const_binop (code, fold_convert (ctype, op1),
6710 : fold_convert (ctype, c));
6711 : /* We allow the constant to overflow with wrapping semantics. */
6712 3072783 : if (op1 == 0
6713 3072783 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6714 : break;
6715 : }
6716 : else
6717 : break;
6718 :
6719 : /* If we have an unsigned type, we cannot widen the operation since it
6720 : will change the result if the original computation overflowed. */
6721 3067603 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6722 : break;
6723 :
6724 : /* The last case is if we are a multiply. In that case, we can
6725 : apply the distributive law to commute the multiply and addition
6726 : if the multiplication of the constants doesn't overflow
6727 : and overflow is defined. With undefined overflow
6728 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6729 : But fold_plusminus_mult_expr would factor back any power-of-two
6730 : value so do not distribute in the first place in this case. */
6731 3067603 : if (code == MULT_EXPR
6732 2657020 : && TYPE_OVERFLOW_WRAPS (ctype)
6733 5363569 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6734 641927 : return fold_build2 (tcode, ctype,
6735 : fold_build2 (code, ctype,
6736 : fold_convert (ctype, op0),
6737 : fold_convert (ctype, c)),
6738 : op1);
6739 :
6740 : break;
6741 :
6742 2457973 : case MULT_EXPR:
6743 : /* We have a special case here if we are doing something like
6744 : (C * 8) % 4 since we know that's zero. */
6745 2457973 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6746 2457973 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6747 : /* If the multiplication can overflow we cannot optimize this. */
6748 10982 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6749 338 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6750 2468955 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6751 293 : TYPE_SIGN (type)))
6752 : {
6753 8 : return omit_one_operand (type, integer_zero_node, op0);
6754 : }
6755 :
6756 : /* ... fall through ... */
6757 :
6758 2746424 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6759 2746424 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6760 : /* If we can extract our operation from the LHS, do so and return a
6761 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6762 : do something only if the second operand is a constant. */
6763 2746424 : if (same_p
6764 2325380 : && TYPE_OVERFLOW_WRAPS (ctype)
6765 4884464 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6766 94395 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6767 : fold_convert (ctype, op1));
6768 2652029 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6769 2221921 : && TYPE_OVERFLOW_WRAPS (ctype)
6770 4686658 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6771 941601 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6772 : fold_convert (ctype, t1));
6773 1710428 : else if (TREE_CODE (op1) != INTEGER_CST)
6774 : return 0;
6775 :
6776 : /* If these are the same operation types, we can associate them
6777 : assuming no overflow. */
6778 631032 : if (tcode == code)
6779 : {
6780 210522 : bool overflow_p = false;
6781 210522 : wi::overflow_type overflow_mul;
6782 210522 : signop sign = TYPE_SIGN (ctype);
6783 210522 : unsigned prec = TYPE_PRECISION (ctype);
6784 421044 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6785 210522 : wi::to_wide (c, prec),
6786 210522 : sign, &overflow_mul);
6787 210522 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6788 210522 : if (overflow_mul
6789 2241 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6790 : overflow_p = true;
6791 210455 : if (!overflow_p)
6792 210455 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6793 : wide_int_to_tree (ctype, mul));
6794 210522 : }
6795 :
6796 : /* If these operations "cancel" each other, we have the main
6797 : optimizations of this pass, which occur when either constant is a
6798 : multiple of the other, in which case we replace this with either an
6799 : operation or CODE or TCODE.
6800 :
6801 : If we have an unsigned type, we cannot do this since it will change
6802 : the result if the original computation overflowed. */
6803 420577 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6804 96689 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6805 517223 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6806 96606 : || (tcode == MULT_EXPR
6807 96606 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6808 850 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6809 846 : && code != MULT_EXPR)))
6810 : {
6811 880 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6812 880 : TYPE_SIGN (type)))
6813 : {
6814 104 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6815 : fold_convert (ctype,
6816 : const_binop (TRUNC_DIV_EXPR,
6817 : op1, c)));
6818 : }
6819 776 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6820 776 : TYPE_SIGN (type)))
6821 : {
6822 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6823 : fold_convert (ctype,
6824 : const_binop (TRUNC_DIV_EXPR,
6825 : c, op1)));
6826 : }
6827 : }
6828 : break;
6829 :
6830 : default:
6831 : break;
6832 : }
6833 :
6834 : return 0;
6835 : }
6836 :
6837 : /* Return a node which has the indicated constant VALUE (either 0 or
6838 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6839 : and is of the indicated TYPE. */
6840 :
6841 : tree
6842 109190076 : constant_boolean_node (bool value, tree type)
6843 : {
6844 109190076 : if (type == integer_type_node)
6845 19789691 : return value ? integer_one_node : integer_zero_node;
6846 89400385 : else if (type == boolean_type_node)
6847 85151785 : return value ? boolean_true_node : boolean_false_node;
6848 4248600 : else if (VECTOR_TYPE_P (type))
6849 1453 : return build_vector_from_val (type,
6850 1453 : build_int_cst (TREE_TYPE (type),
6851 2089 : value ? -1 : 0));
6852 : else
6853 4247147 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6854 : }
6855 :
6856 :
6857 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6858 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6859 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6860 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6861 : COND is the first argument to CODE; otherwise (as in the example
6862 : given here), it is the second argument. TYPE is the type of the
6863 : original expression. Return NULL_TREE if no simplification is
6864 : possible. */
6865 :
6866 : static tree
6867 944249 : fold_binary_op_with_conditional_arg (location_t loc,
6868 : enum tree_code code,
6869 : tree type, tree op0, tree op1,
6870 : tree cond, tree arg, int cond_first_p)
6871 : {
6872 944249 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6873 944249 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6874 944249 : tree test, true_value, false_value;
6875 944249 : tree lhs = NULL_TREE;
6876 944249 : tree rhs = NULL_TREE;
6877 944249 : enum tree_code cond_code = COND_EXPR;
6878 :
6879 : /* Do not move possibly trapping operations into the conditional as this
6880 : pessimizes code and causes gimplification issues when applied late. */
6881 963304 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6882 188429 : ANY_INTEGRAL_TYPE_P (type)
6883 947458 : && TYPE_OVERFLOW_TRAPS (type), op1))
6884 : return NULL_TREE;
6885 :
6886 925030 : if (TREE_CODE (cond) == COND_EXPR
6887 328065 : || TREE_CODE (cond) == VEC_COND_EXPR)
6888 : {
6889 600011 : test = TREE_OPERAND (cond, 0);
6890 600011 : true_value = TREE_OPERAND (cond, 1);
6891 600011 : false_value = TREE_OPERAND (cond, 2);
6892 : /* If this operand throws an expression, then it does not make
6893 : sense to try to perform a logical or arithmetic operation
6894 : involving it. */
6895 600011 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6896 7463 : lhs = true_value;
6897 600011 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6898 6 : rhs = false_value;
6899 : }
6900 325019 : else if (!(TREE_CODE (type) != VECTOR_TYPE
6901 324865 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
6902 : {
6903 323051 : tree testtype = TREE_TYPE (cond);
6904 323051 : test = cond;
6905 323051 : true_value = constant_boolean_node (true, testtype);
6906 323051 : false_value = constant_boolean_node (false, testtype);
6907 : }
6908 : else
6909 : /* Detect the case of mixing vector and scalar types - bail out. */
6910 : return NULL_TREE;
6911 :
6912 923062 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
6913 3200 : cond_code = VEC_COND_EXPR;
6914 :
6915 : /* This transformation is only worthwhile if we don't have to wrap ARG
6916 : in a SAVE_EXPR and the operation can be simplified without recursing
6917 : on at least one of the branches once its pushed inside the COND_EXPR. */
6918 923062 : if (!TREE_CONSTANT (arg)
6919 923062 : && (TREE_SIDE_EFFECTS (arg)
6920 441837 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6921 437395 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6922 : return NULL_TREE;
6923 :
6924 497092 : arg = fold_convert_loc (loc, arg_type, arg);
6925 497092 : if (lhs == 0)
6926 : {
6927 491061 : true_value = fold_convert_loc (loc, cond_type, true_value);
6928 491061 : if (cond_first_p)
6929 481064 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
6930 : else
6931 9997 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
6932 : }
6933 497092 : if (rhs == 0)
6934 : {
6935 497086 : false_value = fold_convert_loc (loc, cond_type, false_value);
6936 497086 : if (cond_first_p)
6937 486528 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
6938 : else
6939 10558 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
6940 : }
6941 :
6942 : /* Check that we have simplified at least one of the branches. */
6943 497092 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6944 : return NULL_TREE;
6945 :
6946 477026 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6947 : }
6948 :
6949 :
6950 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
6951 :
6952 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
6953 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
6954 : if ARG - ZERO_ARG is the same as X.
6955 :
6956 : If ARG is NULL, check for any value of type TYPE.
6957 :
6958 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6959 : and finite. The problematic cases are when X is zero, and its mode
6960 : has signed zeros. In the case of rounding towards -infinity,
6961 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6962 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
6963 :
6964 : bool
6965 643370 : fold_real_zero_addition_p (const_tree type, const_tree arg,
6966 : const_tree zero_arg, int negate)
6967 : {
6968 643370 : if (!real_zerop (zero_arg))
6969 : return false;
6970 :
6971 : /* Don't allow the fold with -fsignaling-nans. */
6972 642700 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
6973 : return false;
6974 :
6975 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6976 639362 : if (!HONOR_SIGNED_ZEROS (type))
6977 : return true;
6978 :
6979 : /* There is no case that is safe for all rounding modes. */
6980 622503 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
6981 : return false;
6982 :
6983 : /* In a vector or complex, we would need to check the sign of all zeros. */
6984 621840 : if (TREE_CODE (zero_arg) == VECTOR_CST)
6985 1705 : zero_arg = uniform_vector_p (zero_arg);
6986 621840 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
6987 1180 : return false;
6988 :
6989 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6990 620660 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
6991 252 : negate = !negate;
6992 :
6993 : /* The mode has signed zeros, and we have to honor their sign.
6994 : In this situation, there are only two cases we can return true for.
6995 : (i) X - 0 is the same as X with default rounding.
6996 : (ii) X + 0 is X when X can't possibly be -0.0. */
6997 620660 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
6998 : }
6999 :
7000 : /* Subroutine of match.pd that optimizes comparisons of a division by
7001 : a nonzero integer constant against an integer constant, i.e.
7002 : X/C1 op C2.
7003 :
7004 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7005 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7006 :
7007 : enum tree_code
7008 1774551 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7009 : tree *hi, bool *neg_overflow)
7010 : {
7011 1774551 : tree prod, tmp, type = TREE_TYPE (c1);
7012 1774551 : signop sign = TYPE_SIGN (type);
7013 1774551 : wi::overflow_type overflow;
7014 :
7015 : /* We have to do this the hard way to detect unsigned overflow.
7016 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7017 1774551 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7018 1774551 : prod = force_fit_type (type, val, -1, overflow);
7019 1774551 : *neg_overflow = false;
7020 :
7021 1774551 : if (sign == UNSIGNED)
7022 : {
7023 1744910 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7024 1744910 : *lo = prod;
7025 :
7026 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7027 1744910 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7028 1744910 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7029 : }
7030 29641 : else if (tree_int_cst_sgn (c1) >= 0)
7031 : {
7032 28236 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7033 28236 : switch (tree_int_cst_sgn (c2))
7034 : {
7035 4842 : case -1:
7036 4842 : *neg_overflow = true;
7037 4842 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7038 4842 : *hi = prod;
7039 4842 : break;
7040 :
7041 14882 : case 0:
7042 14882 : *lo = fold_negate_const (tmp, type);
7043 14882 : *hi = tmp;
7044 14882 : break;
7045 :
7046 8512 : case 1:
7047 8512 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7048 8512 : *lo = prod;
7049 8512 : break;
7050 :
7051 0 : default:
7052 0 : gcc_unreachable ();
7053 : }
7054 : }
7055 : else
7056 : {
7057 : /* A negative divisor reverses the relational operators. */
7058 1405 : code = swap_tree_comparison (code);
7059 :
7060 1405 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7061 1405 : switch (tree_int_cst_sgn (c2))
7062 : {
7063 134 : case -1:
7064 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7065 134 : *lo = prod;
7066 134 : break;
7067 :
7068 173 : case 0:
7069 173 : *hi = fold_negate_const (tmp, type);
7070 173 : *lo = tmp;
7071 173 : break;
7072 :
7073 1098 : case 1:
7074 1098 : *neg_overflow = true;
7075 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7076 1098 : *hi = prod;
7077 1098 : break;
7078 :
7079 0 : default:
7080 0 : gcc_unreachable ();
7081 : }
7082 : }
7083 :
7084 1774551 : if (code != EQ_EXPR && code != NE_EXPR)
7085 : return code;
7086 :
7087 16674 : if (TREE_OVERFLOW (*lo)
7088 16674 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7089 702 : *lo = NULL_TREE;
7090 16674 : if (TREE_OVERFLOW (*hi)
7091 16674 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7092 95 : *hi = NULL_TREE;
7093 :
7094 : return code;
7095 1774551 : }
7096 :
7097 : /* Test whether it is preferable to swap two operands, ARG0 and
7098 : ARG1, for example because ARG0 is an integer constant and ARG1
7099 : isn't. */
7100 :
7101 : bool
7102 1565396097 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7103 : {
7104 1565396097 : if (CONSTANT_CLASS_P (arg1))
7105 : return false;
7106 512386305 : if (CONSTANT_CLASS_P (arg0))
7107 : return true;
7108 :
7109 471766450 : STRIP_NOPS (arg0);
7110 471766450 : STRIP_NOPS (arg1);
7111 :
7112 471766450 : if (TREE_CONSTANT (arg1))
7113 : return false;
7114 454908029 : if (TREE_CONSTANT (arg0))
7115 : return true;
7116 :
7117 : /* Put addresses in arg1. */
7118 454167173 : if (TREE_CODE (arg1) == ADDR_EXPR)
7119 : return false;
7120 437055506 : if (TREE_CODE (arg0) == ADDR_EXPR)
7121 : return true;
7122 :
7123 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7124 : for commutative and comparison operators. Ensuring a canonical
7125 : form allows the optimizers to find additional redundancies without
7126 : having to explicitly check for both orderings. */
7127 436564500 : if (TREE_CODE (arg0) == SSA_NAME
7128 329537811 : && TREE_CODE (arg1) == SSA_NAME
7129 759780684 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7130 : return true;
7131 :
7132 : /* Put SSA_NAMEs last. */
7133 414029431 : if (TREE_CODE (arg1) == SSA_NAME)
7134 : return false;
7135 98260439 : if (TREE_CODE (arg0) == SSA_NAME)
7136 : return true;
7137 :
7138 : /* Put variables last. */
7139 91938812 : if (DECL_P (arg1))
7140 : return false;
7141 50167301 : if (DECL_P (arg0))
7142 : return true;
7143 :
7144 : return false;
7145 : }
7146 :
7147 :
7148 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7149 : means A >= Y && A != MAX, but in this case we know that
7150 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7151 :
7152 : static tree
7153 23448241 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7154 : {
7155 23448241 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7156 :
7157 23448241 : if (TREE_CODE (bound) == LT_EXPR)
7158 4945560 : a = TREE_OPERAND (bound, 0);
7159 18502681 : else if (TREE_CODE (bound) == GT_EXPR)
7160 2769973 : a = TREE_OPERAND (bound, 1);
7161 : else
7162 : return NULL_TREE;
7163 :
7164 7715533 : typea = TREE_TYPE (a);
7165 7715533 : if (!INTEGRAL_TYPE_P (typea)
7166 470795 : && !POINTER_TYPE_P (typea))
7167 : return NULL_TREE;
7168 :
7169 7543332 : if (TREE_CODE (ineq) == LT_EXPR)
7170 : {
7171 1460184 : a1 = TREE_OPERAND (ineq, 1);
7172 1460184 : y = TREE_OPERAND (ineq, 0);
7173 : }
7174 6083148 : else if (TREE_CODE (ineq) == GT_EXPR)
7175 : {
7176 1153829 : a1 = TREE_OPERAND (ineq, 0);
7177 1153829 : y = TREE_OPERAND (ineq, 1);
7178 : }
7179 : else
7180 : return NULL_TREE;
7181 :
7182 2614013 : if (TREE_TYPE (a1) != typea)
7183 : return NULL_TREE;
7184 :
7185 1875423 : if (POINTER_TYPE_P (typea))
7186 : {
7187 : /* Convert the pointer types into integer before taking the difference. */
7188 11141 : tree ta = fold_convert_loc (loc, ssizetype, a);
7189 11141 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7190 11141 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7191 : }
7192 : else
7193 1864282 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7194 :
7195 1875423 : if (!diff || !integer_onep (diff))
7196 1864703 : return NULL_TREE;
7197 :
7198 10720 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7199 : }
7200 :
7201 : /* Fold a sum or difference of at least one multiplication.
7202 : Returns the folded tree or NULL if no simplification could be made. */
7203 :
7204 : static tree
7205 9278976 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7206 : tree arg0, tree arg1)
7207 : {
7208 9278976 : tree arg00, arg01, arg10, arg11;
7209 9278976 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7210 :
7211 : /* (A * C) +- (B * C) -> (A+-B) * C.
7212 : (A * C) +- A -> A * (C+-1).
7213 : We are most concerned about the case where C is a constant,
7214 : but other combinations show up during loop reduction. Since
7215 : it is not difficult, try all four possibilities. */
7216 :
7217 9278976 : if (TREE_CODE (arg0) == MULT_EXPR)
7218 : {
7219 8110442 : arg00 = TREE_OPERAND (arg0, 0);
7220 8110442 : arg01 = TREE_OPERAND (arg0, 1);
7221 : }
7222 1168534 : else if (TREE_CODE (arg0) == INTEGER_CST)
7223 : {
7224 73385 : arg00 = build_one_cst (type);
7225 73385 : arg01 = arg0;
7226 : }
7227 : else
7228 : {
7229 : /* We cannot generate constant 1 for fract. */
7230 1095149 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7231 0 : return NULL_TREE;
7232 1095149 : arg00 = arg0;
7233 1095149 : arg01 = build_one_cst (type);
7234 : }
7235 9278976 : if (TREE_CODE (arg1) == MULT_EXPR)
7236 : {
7237 2618728 : arg10 = TREE_OPERAND (arg1, 0);
7238 2618728 : arg11 = TREE_OPERAND (arg1, 1);
7239 : }
7240 6660248 : else if (TREE_CODE (arg1) == INTEGER_CST)
7241 : {
7242 3690334 : arg10 = build_one_cst (type);
7243 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7244 : the purpose of this canonicalization. */
7245 7149404 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7246 236392 : && negate_expr_p (arg1)
7247 3921598 : && code == PLUS_EXPR)
7248 : {
7249 231264 : arg11 = negate_expr (arg1);
7250 231264 : code = MINUS_EXPR;
7251 : }
7252 : else
7253 : arg11 = arg1;
7254 : }
7255 : else
7256 : {
7257 : /* We cannot generate constant 1 for fract. */
7258 2969914 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7259 0 : return NULL_TREE;
7260 2969914 : arg10 = arg1;
7261 2969914 : arg11 = build_one_cst (type);
7262 : }
7263 9278976 : same = NULL_TREE;
7264 :
7265 : /* Prefer factoring a common non-constant. */
7266 9278976 : if (operand_equal_p (arg00, arg10, 0))
7267 : same = arg00, alt0 = arg01, alt1 = arg11;
7268 9274785 : else if (operand_equal_p (arg01, arg11, 0))
7269 : same = arg01, alt0 = arg00, alt1 = arg10;
7270 9174309 : else if (operand_equal_p (arg00, arg11, 0))
7271 : same = arg00, alt0 = arg01, alt1 = arg10;
7272 9174276 : else if (operand_equal_p (arg01, arg10, 0))
7273 : same = arg01, alt0 = arg00, alt1 = arg11;
7274 :
7275 : /* No identical multiplicands; see if we can find a common
7276 : power-of-two factor in non-power-of-two multiplies. This
7277 : can help in multi-dimensional array access. */
7278 9169543 : else if (TREE_CODE (arg01) == INTEGER_CST
7279 8134325 : && TREE_CODE (arg11) == INTEGER_CST)
7280 : {
7281 7922870 : wide_int int01 = wi::to_wide (arg01);
7282 7922870 : wide_int int11 = wi::to_wide (arg11);
7283 7922870 : bool swap = false;
7284 7922870 : tree maybe_same;
7285 :
7286 : /* Move min of absolute values to int11. */
7287 7922872 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7288 : {
7289 3795455 : std::swap (int01, int11);
7290 3795455 : std::swap (arg00, arg10);
7291 3795455 : maybe_same = arg01;
7292 3795455 : swap = true;
7293 : }
7294 : else
7295 : maybe_same = arg11;
7296 :
7297 7922870 : wide_int factor = wi::abs (int11);
7298 7922870 : if (wi::gtu_p (factor, 1u)
7299 4462034 : && wi::exact_log2 (factor) != -1
7300 12541240 : && (int01 & (factor - 1)) == 0
7301 : /* The remainder should not be a constant, otherwise we
7302 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7303 : increased the number of multiplications necessary. */
7304 9520692 : && TREE_CODE (arg10) != INTEGER_CST)
7305 : {
7306 2692688 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7307 1346344 : wide_int_to_tree (TREE_TYPE (arg00),
7308 1346344 : wi::sdiv_trunc (int01,
7309 : int11)));
7310 1346344 : alt1 = arg10;
7311 1346344 : same = maybe_same;
7312 1346344 : if (swap)
7313 1159210 : std::swap (alt0, alt1);
7314 : }
7315 7922872 : }
7316 :
7317 8032303 : if (!same)
7318 7823199 : return NULL_TREE;
7319 :
7320 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7321 1455777 : || TYPE_OVERFLOW_WRAPS (type)
7322 : /* We are neither factoring zero nor minus one. */
7323 1590722 : || TREE_CODE (same) == INTEGER_CST)
7324 1444879 : return fold_build2_loc (loc, MULT_EXPR, type,
7325 : fold_build2_loc (loc, code, type,
7326 : fold_convert_loc (loc, type, alt0),
7327 : fold_convert_loc (loc, type, alt1)),
7328 1444879 : fold_convert_loc (loc, type, same));
7329 :
7330 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7331 : same may be minus one and thus the multiplication may overflow. Perform
7332 : the sum operation in an unsigned type. */
7333 10898 : tree utype = unsigned_type_for (type);
7334 10898 : tree tem = fold_build2_loc (loc, code, utype,
7335 : fold_convert_loc (loc, utype, alt0),
7336 : fold_convert_loc (loc, utype, alt1));
7337 : /* If the sum evaluated to a constant that is not -INF the multiplication
7338 : cannot overflow. */
7339 21796 : if (TREE_CODE (tem) == INTEGER_CST
7340 18330 : && (wi::to_wide (tem)
7341 18330 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7342 3703 : return fold_build2_loc (loc, MULT_EXPR, type,
7343 3703 : fold_convert (type, tem), same);
7344 :
7345 : /* Do not resort to unsigned multiplication because
7346 : we lose the no-overflow property of the expression. */
7347 : return NULL_TREE;
7348 : }
7349 :
7350 :
7351 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7352 : into the buffer PTR of length LEN bytes.
7353 : Return the number of bytes placed in the buffer, or zero
7354 : upon failure. */
7355 :
7356 : int
7357 53878184 : native_encode_wide_int (tree type, const wide_int_ref &val,
7358 : unsigned char *ptr, int len, int off)
7359 : {
7360 53878184 : int total_bytes;
7361 53878184 : if (BITINT_TYPE_P (type))
7362 : {
7363 17398 : struct bitint_info info;
7364 17398 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7365 17398 : gcc_assert (ok);
7366 17398 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7367 17398 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7368 : {
7369 17093 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7370 : /* More work is needed when adding _BitInt support to PDP endian
7371 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7372 : match target endianity here. */
7373 17093 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7374 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7375 : || (GET_MODE_SIZE (limb_mode)
7376 : >= UNITS_PER_WORD)));
7377 : }
7378 : else
7379 610 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7380 : }
7381 : else
7382 107721572 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7383 53878184 : int byte, offset, word, words;
7384 53878184 : unsigned char value;
7385 :
7386 53878184 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7387 : return 0;
7388 53877696 : if (off == -1)
7389 53082091 : off = 0;
7390 :
7391 53877696 : if (ptr == NULL)
7392 : /* Dry run. */
7393 2768482 : return MIN (len, total_bytes - off);
7394 :
7395 : words = total_bytes / UNITS_PER_WORD;
7396 :
7397 246751069 : for (byte = 0; byte < total_bytes; byte++)
7398 : {
7399 195641855 : int bitpos = byte * BITS_PER_UNIT;
7400 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7401 : number of bytes. */
7402 195641855 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7403 :
7404 195641855 : if (total_bytes > UNITS_PER_WORD)
7405 : {
7406 195641855 : word = byte / UNITS_PER_WORD;
7407 195641855 : if (WORDS_BIG_ENDIAN)
7408 : word = (words - 1) - word;
7409 195641855 : offset = word * UNITS_PER_WORD;
7410 195641855 : if (BYTES_BIG_ENDIAN)
7411 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7412 : else
7413 195641855 : offset += byte % UNITS_PER_WORD;
7414 : }
7415 : else
7416 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7417 195641855 : if (offset >= off && offset - off < len)
7418 194341188 : ptr[offset - off] = value;
7419 : }
7420 51109214 : return MIN (len, total_bytes - off);
7421 : }
7422 :
7423 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7424 : specified by EXPR into the buffer PTR of length LEN bytes.
7425 : Return the number of bytes placed in the buffer, or zero
7426 : upon failure. */
7427 :
7428 : static int
7429 53878184 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7430 : {
7431 53878184 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7432 53878184 : ptr, len, off);
7433 : }
7434 :
7435 :
7436 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7437 : specified by EXPR into the buffer PTR of length LEN bytes.
7438 : Return the number of bytes placed in the buffer, or zero
7439 : upon failure. */
7440 :
7441 : static int
7442 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7443 : {
7444 0 : tree type = TREE_TYPE (expr);
7445 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7446 0 : int total_bytes = GET_MODE_SIZE (mode);
7447 0 : FIXED_VALUE_TYPE value;
7448 0 : tree i_value, i_type;
7449 :
7450 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7451 : return 0;
7452 :
7453 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7454 :
7455 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7456 : return 0;
7457 :
7458 0 : value = TREE_FIXED_CST (expr);
7459 0 : i_value = double_int_to_tree (i_type, value.data);
7460 :
7461 0 : return native_encode_int (i_value, ptr, len, off);
7462 : }
7463 :
7464 :
7465 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7466 : specified by EXPR into the buffer PTR of length LEN bytes.
7467 : Return the number of bytes placed in the buffer, or zero
7468 : upon failure. */
7469 :
7470 : int
7471 794399 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7472 : unsigned char *ptr, int len, int off)
7473 : {
7474 794399 : int total_bytes = GET_MODE_SIZE (mode);
7475 794399 : int byte, offset, word, words, bitpos;
7476 794399 : unsigned char value;
7477 :
7478 : /* There are always 32 bits in each long, no matter the size of
7479 : the hosts long. We handle floating point representations with
7480 : up to 192 bits. */
7481 794399 : long tmp[6];
7482 :
7483 794399 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7484 : return 0;
7485 792203 : if (off == -1)
7486 689895 : off = 0;
7487 :
7488 792203 : if (ptr == NULL)
7489 : /* Dry run. */
7490 133874 : return MIN (len, total_bytes - off);
7491 :
7492 658329 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7493 :
7494 658329 : real_to_target (tmp, val, mode);
7495 :
7496 6465035 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7497 5806706 : bitpos += BITS_PER_UNIT)
7498 : {
7499 5806706 : byte = (bitpos / BITS_PER_UNIT) & 3;
7500 5806706 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7501 :
7502 5806706 : if (UNITS_PER_WORD < 4)
7503 : {
7504 : word = byte / UNITS_PER_WORD;
7505 : if (WORDS_BIG_ENDIAN)
7506 : word = (words - 1) - word;
7507 : offset = word * UNITS_PER_WORD;
7508 : if (BYTES_BIG_ENDIAN)
7509 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7510 : else
7511 : offset += byte % UNITS_PER_WORD;
7512 : }
7513 : else
7514 : {
7515 5806706 : offset = byte;
7516 5806706 : if (BYTES_BIG_ENDIAN)
7517 : {
7518 : /* Reverse bytes within each long, or within the entire float
7519 : if it's smaller than a long (for HFmode). */
7520 : offset = MIN (3, total_bytes - 1) - offset;
7521 : gcc_assert (offset >= 0);
7522 : }
7523 : }
7524 5806706 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7525 5806706 : if (offset >= off
7526 5803466 : && offset - off < len)
7527 5781806 : ptr[offset - off] = value;
7528 : }
7529 658329 : return MIN (len, total_bytes - off);
7530 : }
7531 :
7532 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7533 : specified by EXPR into the buffer PTR of length LEN bytes.
7534 : Return the number of bytes placed in the buffer, or zero
7535 : upon failure. */
7536 :
7537 : static int
7538 9717 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7539 : {
7540 9717 : int rsize, isize;
7541 9717 : tree part;
7542 :
7543 9717 : part = TREE_REALPART (expr);
7544 9717 : rsize = native_encode_expr (part, ptr, len, off);
7545 9717 : if (off == -1 && rsize == 0)
7546 : return 0;
7547 9717 : part = TREE_IMAGPART (expr);
7548 9717 : if (off != -1)
7549 19549 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7550 9717 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7551 : len - rsize, off);
7552 9717 : if (off == -1 && isize != rsize)
7553 : return 0;
7554 9717 : return rsize + isize;
7555 : }
7556 :
7557 : /* Like native_encode_vector, but only encode the first COUNT elements.
7558 : The other arguments are as for native_encode_vector. */
7559 :
7560 : static int
7561 942083 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7562 : int off, unsigned HOST_WIDE_INT count)
7563 : {
7564 942083 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7565 1884166 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7566 943101 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7567 : {
7568 : /* This is the only case in which elements can be smaller than a byte.
7569 : Element 0 is always in the lsb of the containing byte. */
7570 940 : unsigned int elt_bits = TYPE_PRECISION (itype);
7571 940 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7572 940 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7573 : return 0;
7574 :
7575 940 : if (off == -1)
7576 940 : off = 0;
7577 :
7578 : /* Zero the buffer and then set bits later where necessary. */
7579 940 : int extract_bytes = MIN (len, total_bytes - off);
7580 940 : if (ptr)
7581 940 : memset (ptr, 0, extract_bytes);
7582 :
7583 940 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7584 940 : unsigned int first_elt = off * elts_per_byte;
7585 940 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7586 940 : unsigned int elt_mask = (1 << elt_bits) - 1;
7587 17333 : for (unsigned int i = 0; i < extract_elts; ++i)
7588 : {
7589 16393 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7590 16393 : if (TREE_CODE (elt) != INTEGER_CST)
7591 : return 0;
7592 :
7593 16393 : if (ptr && integer_nonzerop (elt))
7594 : {
7595 8441 : unsigned int bit = i * elt_bits;
7596 8441 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7597 : }
7598 : }
7599 : return extract_bytes;
7600 : }
7601 :
7602 941143 : int offset = 0;
7603 941143 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7604 3320548 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7605 : {
7606 2944536 : if (off >= size)
7607 : {
7608 22240 : off -= size;
7609 22240 : continue;
7610 : }
7611 2922296 : tree elem = VECTOR_CST_ELT (expr, i);
7612 2922296 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7613 : len - offset, off);
7614 2922296 : if ((off == -1 && res != size) || res == 0)
7615 : return 0;
7616 2921767 : offset += res;
7617 2921767 : if (offset >= len)
7618 564602 : return (off == -1 && i < count - 1) ? 0 : offset;
7619 2357165 : if (off != -1)
7620 394978 : off = 0;
7621 : }
7622 : return offset;
7623 : }
7624 :
7625 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7626 : specified by EXPR into the buffer PTR of length LEN bytes.
7627 : Return the number of bytes placed in the buffer, or zero
7628 : upon failure. */
7629 :
7630 : static int
7631 805859 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7632 : {
7633 805859 : unsigned HOST_WIDE_INT count;
7634 805859 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7635 : return 0;
7636 805859 : return native_encode_vector_part (expr, ptr, len, off, count);
7637 : }
7638 :
7639 :
7640 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7641 : specified by EXPR into the buffer PTR of length LEN bytes.
7642 : Return the number of bytes placed in the buffer, or zero
7643 : upon failure. */
7644 :
7645 : static int
7646 138611 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7647 : {
7648 138611 : tree type = TREE_TYPE (expr);
7649 :
7650 : /* Wide-char strings are encoded in target byte-order so native
7651 : encoding them is trivial. */
7652 138611 : if (BITS_PER_UNIT != CHAR_BIT
7653 138611 : || TREE_CODE (type) != ARRAY_TYPE
7654 138611 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7655 277222 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7656 : return 0;
7657 :
7658 138611 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7659 138611 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7660 : return 0;
7661 137743 : if (off == -1)
7662 56056 : off = 0;
7663 137743 : len = MIN (total_bytes - off, len);
7664 137743 : if (ptr == NULL)
7665 : /* Dry run. */;
7666 : else
7667 : {
7668 137743 : int written = 0;
7669 137743 : if (off < TREE_STRING_LENGTH (expr))
7670 : {
7671 137268 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7672 137268 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7673 : }
7674 137743 : memset (ptr + written, 0, len - written);
7675 : }
7676 : return len;
7677 : }
7678 :
7679 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7680 : specified by EXPR into the buffer PTR of length LEN bytes.
7681 : Return the number of bytes placed in the buffer, or zero
7682 : upon failure. */
7683 :
7684 : static int
7685 46086 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7686 : {
7687 : /* We are only concerned with zero-initialization constructors here. That's
7688 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7689 : deal with. For more general handling of constructors, there is
7690 : native_encode_initializer. */
7691 46086 : if (CONSTRUCTOR_NELTS (expr))
7692 : return 0;
7693 :
7694 : /* Wide-char strings are encoded in target byte-order so native
7695 : encoding them is trivial. */
7696 85900 : if (BITS_PER_UNIT != CHAR_BIT
7697 42950 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7698 : return 0;
7699 :
7700 42950 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7701 42950 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7702 : return 0;
7703 42950 : if (off == -1)
7704 0 : off = 0;
7705 42950 : len = MIN (total_bytes - off, len);
7706 42950 : if (ptr == NULL)
7707 : /* Dry run. */;
7708 : else
7709 42950 : memset (ptr, 0, len);
7710 : return len;
7711 : }
7712 :
7713 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7714 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7715 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7716 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7717 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7718 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7719 : Return the number of bytes placed in the buffer, or zero upon failure. */
7720 :
7721 : int
7722 69377108 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7723 : {
7724 : /* We don't support starting at negative offset and -1 is special. */
7725 69377108 : if (off < -1)
7726 : return 0;
7727 :
7728 69377096 : switch (TREE_CODE (expr))
7729 : {
7730 53875938 : case INTEGER_CST:
7731 53875938 : return native_encode_int (expr, ptr, len, off);
7732 :
7733 794399 : case REAL_CST:
7734 794399 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7735 1588798 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7736 :
7737 0 : case FIXED_CST:
7738 0 : return native_encode_fixed (expr, ptr, len, off);
7739 :
7740 9717 : case COMPLEX_CST:
7741 9717 : return native_encode_complex (expr, ptr, len, off);
7742 :
7743 805859 : case VECTOR_CST:
7744 805859 : return native_encode_vector (expr, ptr, len, off);
7745 :
7746 138611 : case STRING_CST:
7747 138611 : return native_encode_string (expr, ptr, len, off);
7748 :
7749 46086 : case CONSTRUCTOR:
7750 46086 : return native_encode_constructor (expr, ptr, len, off);
7751 :
7752 : default:
7753 : return 0;
7754 : }
7755 : }
7756 :
7757 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7758 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7759 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7760 : machine modes, we can't just use build_nonstandard_integer_type. */
7761 :
7762 : tree
7763 541 : find_bitfield_repr_type (int fieldsize, int len)
7764 : {
7765 541 : machine_mode mode;
7766 1063 : for (int pass = 0; pass < 2; pass++)
7767 : {
7768 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7769 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7770 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7771 7286 : && known_eq (GET_MODE_PRECISION (mode),
7772 : GET_MODE_BITSIZE (mode))
7773 11274 : && known_le (GET_MODE_SIZE (mode), len))
7774 : {
7775 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7776 280 : if (ret && TYPE_MODE (ret) == mode)
7777 : return ret;
7778 : }
7779 : }
7780 :
7781 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7782 261 : if (int_n_enabled_p[i]
7783 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7784 261 : && int_n_trees[i].unsigned_type)
7785 : {
7786 261 : tree ret = int_n_trees[i].unsigned_type;
7787 261 : mode = TYPE_MODE (ret);
7788 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7789 522 : && known_eq (GET_MODE_PRECISION (mode),
7790 : GET_MODE_BITSIZE (mode))
7791 783 : && known_le (GET_MODE_SIZE (mode), len))
7792 : return ret;
7793 : }
7794 :
7795 : return NULL_TREE;
7796 : }
7797 :
7798 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7799 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7800 : to be non-NULL and OFF zero), then in addition to filling the
7801 : bytes pointed by PTR with the value also clear any bits pointed
7802 : by MASK that are known to be initialized, keep them as is for
7803 : e.g. uninitialized padding bits or uninitialized fields. */
7804 :
7805 : int
7806 47700578 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7807 : int off, unsigned char *mask)
7808 : {
7809 47700578 : int r;
7810 :
7811 : /* We don't support starting at negative offset and -1 is special. */
7812 47700578 : if (off < -1 || init == NULL_TREE)
7813 : return 0;
7814 :
7815 47700578 : gcc_assert (mask == NULL || (off == 0 && ptr));
7816 :
7817 47700578 : STRIP_NOPS (init);
7818 47700578 : switch (TREE_CODE (init))
7819 : {
7820 0 : case VIEW_CONVERT_EXPR:
7821 0 : case NON_LVALUE_EXPR:
7822 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7823 0 : mask);
7824 45637069 : default:
7825 45637069 : r = native_encode_expr (init, ptr, len, off);
7826 45637069 : if (mask)
7827 1487 : memset (mask, 0, r);
7828 : return r;
7829 2063509 : case CONSTRUCTOR:
7830 2063509 : tree type = TREE_TYPE (init);
7831 2063509 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7832 2063509 : if (total_bytes < 0)
7833 : return 0;
7834 2063509 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7835 : return 0;
7836 2063474 : int o = off == -1 ? 0 : off;
7837 2063474 : if (TREE_CODE (type) == ARRAY_TYPE)
7838 : {
7839 280295 : tree min_index;
7840 280295 : unsigned HOST_WIDE_INT cnt;
7841 280295 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7842 280295 : constructor_elt *ce;
7843 :
7844 280295 : if (!TYPE_DOMAIN (type)
7845 280295 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7846 : return 0;
7847 :
7848 280295 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7849 280295 : if (fieldsize <= 0)
7850 : return 0;
7851 :
7852 280295 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7853 280295 : if (ptr)
7854 280295 : memset (ptr, '\0', MIN (total_bytes - off, len));
7855 :
7856 45083758 : for (cnt = 0; ; cnt++)
7857 : {
7858 45364053 : tree val = NULL_TREE, index = NULL_TREE;
7859 45364053 : HOST_WIDE_INT pos = curpos, count = 0;
7860 45364053 : bool full = false;
7861 45364053 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7862 : {
7863 45256282 : val = ce->value;
7864 45256282 : index = ce->index;
7865 : }
7866 107771 : else if (mask == NULL
7867 287 : || CONSTRUCTOR_NO_CLEARING (init)
7868 108058 : || curpos >= total_bytes)
7869 : break;
7870 : else
7871 : pos = total_bytes;
7872 :
7873 45256282 : if (index && TREE_CODE (index) == RANGE_EXPR)
7874 : {
7875 16 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
7876 16 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
7877 0 : return 0;
7878 16 : offset_int wpos
7879 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
7880 32 : - wi::to_offset (min_index),
7881 16 : TYPE_PRECISION (sizetype));
7882 16 : wpos *= fieldsize;
7883 16 : if (!wi::fits_shwi_p (pos))
7884 : return 0;
7885 16 : pos = wpos.to_shwi ();
7886 16 : offset_int wcount
7887 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
7888 32 : - wi::to_offset (TREE_OPERAND (index, 0)),
7889 16 : TYPE_PRECISION (sizetype));
7890 16 : if (!wi::fits_shwi_p (wcount))
7891 : return 0;
7892 16 : count = wcount.to_shwi ();
7893 16 : }
7894 44761534 : else if (index)
7895 : {
7896 44761534 : if (TREE_CODE (index) != INTEGER_CST)
7897 0 : return 0;
7898 44761534 : offset_int wpos
7899 44761534 : = wi::sext (wi::to_offset (index)
7900 89523068 : - wi::to_offset (min_index),
7901 44761534 : TYPE_PRECISION (sizetype));
7902 44761534 : wpos *= fieldsize;
7903 44761534 : if (!wi::fits_shwi_p (wpos))
7904 : return 0;
7905 44761534 : pos = wpos.to_shwi ();
7906 : }
7907 :
7908 45257053 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
7909 : {
7910 40 : if (valueinit == -1)
7911 : {
7912 40 : tree zero = build_zero_cst (TREE_TYPE (type));
7913 80 : r = native_encode_initializer (zero, ptr + curpos,
7914 : fieldsize, 0,
7915 40 : mask + curpos);
7916 40 : if (TREE_CODE (zero) == CONSTRUCTOR)
7917 0 : ggc_free (zero);
7918 40 : if (!r)
7919 : return 0;
7920 40 : valueinit = curpos;
7921 40 : curpos += fieldsize;
7922 : }
7923 70 : while (curpos != pos)
7924 : {
7925 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
7926 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
7927 30 : curpos += fieldsize;
7928 : }
7929 : }
7930 :
7931 45256322 : curpos = pos;
7932 45256322 : if (val && TREE_CODE (val) == RAW_DATA_CST)
7933 : {
7934 488 : if (count)
7935 : return 0;
7936 488 : if (off == -1
7937 488 : || (curpos >= off
7938 0 : && (curpos + RAW_DATA_LENGTH (val)
7939 0 : <= (HOST_WIDE_INT) off + len)))
7940 : {
7941 488 : if (ptr)
7942 488 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
7943 488 : RAW_DATA_LENGTH (val));
7944 488 : if (mask)
7945 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
7946 : }
7947 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
7948 0 : && curpos < (HOST_WIDE_INT) off + len)
7949 : {
7950 : /* Partial overlap. */
7951 0 : unsigned char *p = NULL;
7952 0 : int no = 0;
7953 0 : int l;
7954 0 : gcc_assert (mask == NULL);
7955 0 : if (curpos >= off)
7956 : {
7957 0 : if (ptr)
7958 0 : p = ptr + curpos - off;
7959 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
7960 : RAW_DATA_LENGTH (val));
7961 : }
7962 : else
7963 : {
7964 0 : p = ptr;
7965 0 : no = off - curpos;
7966 0 : l = len;
7967 : }
7968 0 : if (p)
7969 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
7970 : }
7971 488 : curpos += RAW_DATA_LENGTH (val);
7972 488 : val = NULL_TREE;
7973 : }
7974 488 : if (val)
7975 45333834 : do
7976 : {
7977 45333834 : if (off == -1
7978 589547 : || (curpos >= off
7979 195393 : && (curpos + fieldsize
7980 195393 : <= (HOST_WIDE_INT) off + len)))
7981 : {
7982 44909300 : if (full)
7983 : {
7984 78040 : if (ptr)
7985 78040 : memcpy (ptr + (curpos - o), ptr + (pos - o),
7986 : fieldsize);
7987 78040 : if (mask)
7988 0 : memcpy (mask + curpos, mask + pos, fieldsize);
7989 : }
7990 89828224 : else if (!native_encode_initializer (val,
7991 : ptr
7992 44831260 : ? ptr + curpos - o
7993 : : NULL,
7994 : fieldsize,
7995 : off == -1 ? -1
7996 : : 0,
7997 : mask
7998 691 : ? mask + curpos
7999 : : NULL))
8000 : return 0;
8001 : else
8002 : {
8003 : full = true;
8004 : pos = curpos;
8005 : }
8006 : }
8007 424534 : else if (curpos + fieldsize > off
8008 32640 : && curpos < (HOST_WIDE_INT) off + len)
8009 : {
8010 : /* Partial overlap. */
8011 8135 : unsigned char *p = NULL;
8012 8135 : int no = 0;
8013 8135 : int l;
8014 8135 : gcc_assert (mask == NULL);
8015 8135 : if (curpos >= off)
8016 : {
8017 5875 : if (ptr)
8018 5875 : p = ptr + curpos - off;
8019 5875 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8020 : fieldsize);
8021 : }
8022 : else
8023 : {
8024 2260 : p = ptr;
8025 2260 : no = off - curpos;
8026 2260 : l = len;
8027 : }
8028 8135 : if (!native_encode_initializer (val, p, l, no, NULL))
8029 : return 0;
8030 : }
8031 45161270 : curpos += fieldsize;
8032 : }
8033 45161270 : while (count-- != 0);
8034 45083758 : }
8035 107731 : return MIN (total_bytes - off, len);
8036 : }
8037 1783179 : else if (TREE_CODE (type) == RECORD_TYPE
8038 1783179 : || TREE_CODE (type) == UNION_TYPE)
8039 : {
8040 1783179 : unsigned HOST_WIDE_INT cnt;
8041 1783179 : constructor_elt *ce;
8042 1783179 : tree fld_base = TYPE_FIELDS (type);
8043 1783179 : tree to_free = NULL_TREE;
8044 :
8045 1783179 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8046 1783179 : if (ptr != NULL)
8047 1783179 : memset (ptr, '\0', MIN (total_bytes - o, len));
8048 333525 : for (cnt = 0; ; cnt++)
8049 : {
8050 2116704 : tree val = NULL_TREE, field = NULL_TREE;
8051 2116704 : HOST_WIDE_INT pos = 0, fieldsize;
8052 2116704 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8053 :
8054 2116704 : if (to_free)
8055 : {
8056 0 : ggc_free (to_free);
8057 0 : to_free = NULL_TREE;
8058 : }
8059 :
8060 2116704 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8061 : {
8062 359627 : val = ce->value;
8063 359627 : field = ce->index;
8064 359627 : if (field == NULL_TREE)
8065 : return 0;
8066 :
8067 359627 : pos = int_byte_position (field);
8068 359627 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8069 1496 : continue;
8070 : }
8071 1757077 : else if (mask == NULL
8072 1757077 : || CONSTRUCTOR_NO_CLEARING (init))
8073 : break;
8074 : else
8075 : pos = total_bytes;
8076 :
8077 360196 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8078 : {
8079 : tree fld;
8080 10265 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8081 : {
8082 9864 : if (TREE_CODE (fld) != FIELD_DECL)
8083 8803 : continue;
8084 1061 : if (fld == field)
8085 : break;
8086 146 : if (DECL_PADDING_P (fld))
8087 87 : continue;
8088 59 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8089 59 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8090 : return 0;
8091 59 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8092 2 : continue;
8093 : break;
8094 : }
8095 1373 : if (fld == NULL_TREE)
8096 : {
8097 401 : if (ce == NULL)
8098 : break;
8099 : return 0;
8100 : }
8101 972 : fld_base = DECL_CHAIN (fld);
8102 972 : if (fld != field)
8103 : {
8104 57 : cnt--;
8105 57 : field = fld;
8106 57 : pos = int_byte_position (field);
8107 57 : val = build_zero_cst (TREE_TYPE (fld));
8108 57 : if (TREE_CODE (val) == CONSTRUCTOR)
8109 0 : to_free = val;
8110 : }
8111 : }
8112 :
8113 358188 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8114 13915 : && TYPE_DOMAIN (TREE_TYPE (field))
8115 372103 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8116 : {
8117 81 : if (mask || off != -1)
8118 : return 0;
8119 81 : if (val == NULL_TREE)
8120 0 : continue;
8121 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8122 : return 0;
8123 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8124 81 : if (fieldsize < 0
8125 81 : || (int) fieldsize != fieldsize
8126 81 : || (pos + fieldsize) > INT_MAX)
8127 : return 0;
8128 81 : if (pos + fieldsize > total_bytes)
8129 : {
8130 81 : if (ptr != NULL && total_bytes < len)
8131 81 : memset (ptr + total_bytes, '\0',
8132 81 : MIN (pos + fieldsize, len) - total_bytes);
8133 : total_bytes = pos + fieldsize;
8134 : }
8135 : }
8136 : else
8137 : {
8138 358107 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8139 358107 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8140 : return 0;
8141 358107 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8142 : }
8143 358188 : if (fieldsize == 0)
8144 1 : continue;
8145 :
8146 : /* Prepare to deal with integral bit-fields and filter out other
8147 : bit-fields that do not start and end on a byte boundary. */
8148 358187 : if (DECL_BIT_FIELD (field))
8149 : {
8150 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8151 : return 0;
8152 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8153 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8154 : {
8155 2711 : bpos %= BITS_PER_UNIT;
8156 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8157 2711 : epos = fieldsize % BITS_PER_UNIT;
8158 2711 : fieldsize += BITS_PER_UNIT - 1;
8159 2711 : fieldsize /= BITS_PER_UNIT;
8160 : }
8161 0 : else if (bpos % BITS_PER_UNIT
8162 0 : || DECL_SIZE (field) == NULL_TREE
8163 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8164 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8165 : return 0;
8166 : }
8167 :
8168 358187 : if (off != -1 && pos + fieldsize <= off)
8169 3195 : continue;
8170 :
8171 354992 : if (val == NULL_TREE)
8172 0 : continue;
8173 :
8174 354992 : if (DECL_BIT_FIELD (field)
8175 354992 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8176 : {
8177 : /* FIXME: Handle PDP endian. */
8178 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8179 261 : return 0;
8180 :
8181 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8182 6 : val = TREE_OPERAND (val, 0);
8183 2507 : if (TREE_CODE (val) != INTEGER_CST)
8184 : return 0;
8185 :
8186 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8187 2507 : tree repr_type = NULL_TREE;
8188 2507 : HOST_WIDE_INT rpos = 0;
8189 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8190 : {
8191 1978 : rpos = int_byte_position (repr);
8192 1978 : repr_type = TREE_TYPE (repr);
8193 : }
8194 : else
8195 : {
8196 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8197 529 : if (repr_type == NULL_TREE)
8198 : return 0;
8199 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8200 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8201 268 : if (pos + repr_size <= o + len)
8202 : rpos = pos;
8203 : else
8204 : {
8205 14 : rpos = o + len - repr_size;
8206 14 : gcc_assert (rpos <= pos);
8207 : }
8208 : }
8209 :
8210 2246 : if (rpos > pos)
8211 : return 0;
8212 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8213 2246 : int diff = (TYPE_PRECISION (repr_type)
8214 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8215 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8216 2246 : if (!BYTES_BIG_ENDIAN)
8217 2246 : w = wi::lshift (w, bitoff);
8218 : else
8219 : w = wi::lshift (w, diff - bitoff);
8220 2246 : val = wide_int_to_tree (repr_type, w);
8221 :
8222 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8223 : / BITS_PER_UNIT + 1];
8224 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8225 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8226 0 : return 0;
8227 :
8228 2246 : if (ptr == NULL)
8229 0 : continue;
8230 :
8231 : /* If the bitfield does not start at byte boundary, handle
8232 : the partial byte at the start. */
8233 2246 : if (bpos
8234 1351 : && (off == -1 || (pos >= off && len >= 1)))
8235 : {
8236 1276 : if (!BYTES_BIG_ENDIAN)
8237 : {
8238 1276 : int msk = (1 << bpos) - 1;
8239 1276 : buf[pos - rpos] &= ~msk;
8240 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8241 1276 : if (mask)
8242 : {
8243 147 : if (fieldsize > 1 || epos == 0)
8244 129 : mask[pos] &= msk;
8245 : else
8246 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8247 : }
8248 : }
8249 : else
8250 : {
8251 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8252 : buf[pos - rpos] &= msk;
8253 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8254 : if (mask)
8255 : {
8256 : if (fieldsize > 1 || epos == 0)
8257 : mask[pos] &= ~msk;
8258 : else
8259 : mask[pos] &= (~msk
8260 : | ((1 << (BITS_PER_UNIT - epos))
8261 : - 1));
8262 : }
8263 : }
8264 : }
8265 : /* If the bitfield does not end at byte boundary, handle
8266 : the partial byte at the end. */
8267 2246 : if (epos
8268 1724 : && (off == -1
8269 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8270 : {
8271 1621 : if (!BYTES_BIG_ENDIAN)
8272 : {
8273 1621 : int msk = (1 << epos) - 1;
8274 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8275 1621 : buf[pos - rpos + fieldsize - 1]
8276 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8277 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8278 156 : mask[pos + fieldsize - 1] &= ~msk;
8279 : }
8280 : else
8281 : {
8282 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8283 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8284 : buf[pos - rpos + fieldsize - 1]
8285 : |= ptr[pos + fieldsize - 1 - o] & msk;
8286 : if (mask && (fieldsize > 1 || bpos == 0))
8287 : mask[pos + fieldsize - 1] &= msk;
8288 : }
8289 : }
8290 2246 : if (off == -1
8291 1301 : || (pos >= off
8292 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8293 : {
8294 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8295 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8296 75 : memset (mask + pos + (bpos != 0), 0,
8297 75 : fieldsize - (bpos != 0) - (epos != 0));
8298 : }
8299 : else
8300 : {
8301 : /* Partial overlap. */
8302 191 : HOST_WIDE_INT fsz = fieldsize;
8303 191 : gcc_assert (mask == NULL);
8304 191 : if (pos < off)
8305 : {
8306 89 : fsz -= (off - pos);
8307 89 : pos = off;
8308 : }
8309 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8310 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8311 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8312 : }
8313 2246 : continue;
8314 2246 : }
8315 :
8316 352485 : if (off == -1
8317 17614 : || (pos >= off
8318 16784 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8319 : {
8320 344116 : int fldsize = fieldsize;
8321 9245 : if (off == -1)
8322 : {
8323 334871 : tree fld = DECL_CHAIN (field);
8324 5026404 : while (fld)
8325 : {
8326 4707303 : if (TREE_CODE (fld) == FIELD_DECL)
8327 : break;
8328 4691533 : fld = DECL_CHAIN (fld);
8329 : }
8330 334871 : if (fld == NULL_TREE)
8331 319101 : fldsize = len - pos;
8332 : }
8333 354249 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8334 : : NULL,
8335 : fldsize,
8336 : off == -1 ? -1 : 0,
8337 888 : mask ? mask + pos : NULL);
8338 344116 : if (!r)
8339 : return 0;
8340 325566 : if (off == -1
8341 323561 : && fldsize != fieldsize
8342 620 : && r > fieldsize
8343 327 : && pos + r > total_bytes)
8344 333525 : total_bytes = pos + r;
8345 : }
8346 : else
8347 : {
8348 : /* Partial overlap. */
8349 8369 : unsigned char *p = NULL;
8350 8369 : int no = 0;
8351 8369 : int l;
8352 8369 : gcc_assert (mask == NULL);
8353 8369 : if (pos >= off)
8354 : {
8355 7539 : if (ptr)
8356 7539 : p = ptr + pos - off;
8357 7539 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8358 : fieldsize);
8359 : }
8360 : else
8361 : {
8362 830 : p = ptr;
8363 830 : no = off - pos;
8364 830 : l = len;
8365 : }
8366 8369 : if (!native_encode_initializer (val, p, l, no, NULL))
8367 : return 0;
8368 : }
8369 333525 : }
8370 1757020 : return MIN (total_bytes - off, len);
8371 : }
8372 : return 0;
8373 : }
8374 : }
8375 :
8376 :
8377 : /* Subroutine of native_interpret_expr. Interpret the contents of
8378 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8379 : If the buffer cannot be interpreted, return NULL_TREE. */
8380 :
8381 : static tree
8382 2746670 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8383 : {
8384 2746670 : int total_bytes;
8385 2746670 : if (BITINT_TYPE_P (type))
8386 : {
8387 31 : struct bitint_info info;
8388 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8389 31 : gcc_assert (ok);
8390 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8391 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8392 : {
8393 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8394 : /* More work is needed when adding _BitInt support to PDP endian
8395 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8396 : match target endianity here. */
8397 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8398 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8399 : || (GET_MODE_SIZE (limb_mode)
8400 : >= UNITS_PER_WORD)));
8401 : }
8402 : else
8403 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8404 : }
8405 : else
8406 5493278 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8407 :
8408 2746670 : if (total_bytes > len)
8409 : return NULL_TREE;
8410 :
8411 2746428 : wide_int result = wi::from_buffer (ptr, total_bytes);
8412 :
8413 2746428 : return wide_int_to_tree (type, result);
8414 2746428 : }
8415 :
8416 :
8417 : /* Subroutine of native_interpret_expr. Interpret the contents of
8418 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8419 : If the buffer cannot be interpreted, return NULL_TREE. */
8420 :
8421 : static tree
8422 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8423 : {
8424 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8425 0 : int total_bytes = GET_MODE_SIZE (mode);
8426 0 : double_int result;
8427 0 : FIXED_VALUE_TYPE fixed_value;
8428 :
8429 0 : if (total_bytes > len
8430 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8431 : return NULL_TREE;
8432 :
8433 0 : result = double_int::from_buffer (ptr, total_bytes);
8434 0 : fixed_value = fixed_from_double_int (result, mode);
8435 :
8436 0 : return build_fixed (type, fixed_value);
8437 : }
8438 :
8439 :
8440 : /* Subroutine of native_interpret_expr. Interpret the contents of
8441 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8442 : If the buffer cannot be interpreted, return NULL_TREE. */
8443 :
8444 : tree
8445 35514 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8446 : {
8447 35514 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8448 35514 : int total_bytes = GET_MODE_SIZE (mode);
8449 35514 : unsigned char value;
8450 : /* There are always 32 bits in each long, no matter the size of
8451 : the hosts long. We handle floating point representations with
8452 : up to 192 bits. */
8453 35514 : REAL_VALUE_TYPE r;
8454 35514 : long tmp[6];
8455 :
8456 35514 : if (total_bytes > len || total_bytes > 24)
8457 : return NULL_TREE;
8458 35453 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8459 :
8460 35453 : memset (tmp, 0, sizeof (tmp));
8461 261581 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8462 226128 : bitpos += BITS_PER_UNIT)
8463 : {
8464 : /* Both OFFSET and BYTE index within a long;
8465 : bitpos indexes the whole float. */
8466 226128 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8467 226128 : if (UNITS_PER_WORD < 4)
8468 : {
8469 : int word = byte / UNITS_PER_WORD;
8470 : if (WORDS_BIG_ENDIAN)
8471 : word = (words - 1) - word;
8472 : offset = word * UNITS_PER_WORD;
8473 : if (BYTES_BIG_ENDIAN)
8474 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8475 : else
8476 : offset += byte % UNITS_PER_WORD;
8477 : }
8478 : else
8479 : {
8480 226128 : offset = byte;
8481 226128 : if (BYTES_BIG_ENDIAN)
8482 : {
8483 : /* Reverse bytes within each long, or within the entire float
8484 : if it's smaller than a long (for HFmode). */
8485 : offset = MIN (3, total_bytes - 1) - offset;
8486 : gcc_assert (offset >= 0);
8487 : }
8488 : }
8489 226128 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8490 :
8491 226128 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8492 : }
8493 :
8494 35453 : real_from_target (&r, tmp, mode);
8495 35453 : return build_real (type, r);
8496 : }
8497 :
8498 :
8499 : /* Subroutine of native_interpret_expr. Interpret the contents of
8500 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8501 : If the buffer cannot be interpreted, return NULL_TREE. */
8502 :
8503 : static tree
8504 1590 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8505 : {
8506 1590 : tree etype, rpart, ipart;
8507 1590 : int size;
8508 :
8509 1590 : etype = TREE_TYPE (type);
8510 1590 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8511 1590 : if (size * 2 > len)
8512 : return NULL_TREE;
8513 1557 : rpart = native_interpret_expr (etype, ptr, size);
8514 1557 : if (!rpart)
8515 : return NULL_TREE;
8516 1376 : ipart = native_interpret_expr (etype, ptr+size, size);
8517 1376 : if (!ipart)
8518 : return NULL_TREE;
8519 1376 : return build_complex (type, rpart, ipart);
8520 : }
8521 :
8522 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8523 : which contains LEN bytes. The vector is known to be encodable using
8524 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8525 :
8526 : Return the vector on success, otherwise return null. */
8527 :
8528 : static tree
8529 212794 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8530 : unsigned int len, unsigned int npatterns,
8531 : unsigned int nelts_per_pattern)
8532 : {
8533 212794 : tree elt_type = TREE_TYPE (type);
8534 212794 : if (VECTOR_BOOLEAN_TYPE_P (type)
8535 212797 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8536 : {
8537 : /* This is the only case in which elements can be smaller than a byte.
8538 : Element 0 is always in the lsb of the containing byte. */
8539 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8540 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8541 : return NULL_TREE;
8542 :
8543 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8544 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8545 : {
8546 16 : unsigned int bit_index = i * elt_bits;
8547 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8548 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8549 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8550 17 : ? build_all_ones_cst (elt_type)
8551 1 : : build_zero_cst (elt_type));
8552 : }
8553 1 : return builder.build ();
8554 1 : }
8555 :
8556 212793 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8557 212793 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8558 : return NULL_TREE;
8559 :
8560 212793 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8561 849808 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8562 : {
8563 637149 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8564 637149 : if (!elt)
8565 134 : return NULL_TREE;
8566 637015 : builder.quick_push (elt);
8567 637015 : bytes += elt_bytes;
8568 : }
8569 212659 : return builder.build ();
8570 212793 : }
8571 :
8572 : /* Subroutine of native_interpret_expr. Interpret the contents of
8573 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8574 : If the buffer cannot be interpreted, return NULL_TREE. */
8575 :
8576 : static tree
8577 76572 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8578 : {
8579 76572 : unsigned HOST_WIDE_INT size;
8580 :
8581 76572 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8582 76572 : || size > len)
8583 2 : return NULL_TREE;
8584 :
8585 76570 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8586 76570 : return native_interpret_vector_part (type, ptr, len, count, 1);
8587 : }
8588 :
8589 :
8590 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8591 : the buffer PTR of length LEN as a constant of type TYPE. For
8592 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8593 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8594 : return NULL_TREE. */
8595 :
8596 : tree
8597 2926689 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8598 : {
8599 2926689 : switch (TREE_CODE (type))
8600 : {
8601 2746670 : case INTEGER_TYPE:
8602 2746670 : case ENUMERAL_TYPE:
8603 2746670 : case BOOLEAN_TYPE:
8604 2746670 : case POINTER_TYPE:
8605 2746670 : case REFERENCE_TYPE:
8606 2746670 : case OFFSET_TYPE:
8607 2746670 : case BITINT_TYPE:
8608 2746670 : return native_interpret_int (type, ptr, len);
8609 :
8610 33927 : case REAL_TYPE:
8611 33927 : if (tree ret = native_interpret_real (type, ptr, len))
8612 : {
8613 : /* For floating point values in composite modes, punt if this
8614 : folding doesn't preserve bit representation. As the mode doesn't
8615 : have fixed precision while GCC pretends it does, there could be
8616 : valid values that GCC can't really represent accurately.
8617 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8618 : bit combinationations which GCC doesn't preserve. */
8619 33866 : unsigned char buf[24 * 2];
8620 33866 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8621 33866 : int total_bytes = GET_MODE_SIZE (mode);
8622 33866 : memcpy (buf + 24, ptr, total_bytes);
8623 33866 : clear_type_padding_in_mask (type, buf + 24);
8624 33866 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8625 33866 : || memcmp (buf + 24, buf, total_bytes) != 0)
8626 488 : return NULL_TREE;
8627 : return ret;
8628 : }
8629 : return NULL_TREE;
8630 :
8631 0 : case FIXED_POINT_TYPE:
8632 0 : return native_interpret_fixed (type, ptr, len);
8633 :
8634 1590 : case COMPLEX_TYPE:
8635 1590 : return native_interpret_complex (type, ptr, len);
8636 :
8637 76572 : case VECTOR_TYPE:
8638 76572 : return native_interpret_vector (type, ptr, len);
8639 :
8640 : default:
8641 : return NULL_TREE;
8642 : }
8643 : }
8644 :
8645 : /* Returns true if we can interpret the contents of a native encoding
8646 : as TYPE. */
8647 :
8648 : bool
8649 305155 : can_native_interpret_type_p (tree type)
8650 : {
8651 305155 : switch (TREE_CODE (type))
8652 : {
8653 : case INTEGER_TYPE:
8654 : case ENUMERAL_TYPE:
8655 : case BOOLEAN_TYPE:
8656 : case POINTER_TYPE:
8657 : case REFERENCE_TYPE:
8658 : case FIXED_POINT_TYPE:
8659 : case REAL_TYPE:
8660 : case COMPLEX_TYPE:
8661 : case VECTOR_TYPE:
8662 : case OFFSET_TYPE:
8663 : return true;
8664 29010 : default:
8665 29010 : return false;
8666 : }
8667 : }
8668 :
8669 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8670 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8671 :
8672 : tree
8673 707 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8674 : int len)
8675 : {
8676 707 : vec<constructor_elt, va_gc> *elts = NULL;
8677 707 : if (TREE_CODE (type) == ARRAY_TYPE)
8678 : {
8679 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8680 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8681 : return NULL_TREE;
8682 :
8683 197 : HOST_WIDE_INT cnt = 0;
8684 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8685 : {
8686 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8687 : return NULL_TREE;
8688 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8689 : }
8690 197 : if (eltsz == 0)
8691 0 : cnt = 0;
8692 197 : HOST_WIDE_INT pos = 0;
8693 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8694 : {
8695 439 : tree v = NULL_TREE;
8696 439 : if (pos >= len || pos + eltsz > len)
8697 707 : return NULL_TREE;
8698 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8699 : {
8700 367 : v = native_interpret_expr (TREE_TYPE (type),
8701 367 : ptr + off + pos, eltsz);
8702 367 : if (v == NULL_TREE)
8703 : return NULL_TREE;
8704 : }
8705 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8706 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8707 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8708 : eltsz);
8709 72 : if (v == NULL_TREE)
8710 0 : return NULL_TREE;
8711 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8712 : }
8713 197 : return build_constructor (type, elts);
8714 : }
8715 510 : if (TREE_CODE (type) != RECORD_TYPE)
8716 : return NULL_TREE;
8717 13482 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8718 : {
8719 1236 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8720 14208 : || is_empty_type (TREE_TYPE (field)))
8721 11834 : continue;
8722 1138 : tree fld = field;
8723 1138 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8724 1138 : int diff = 0;
8725 1138 : tree v = NULL_TREE;
8726 1138 : if (DECL_BIT_FIELD (field))
8727 : {
8728 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8729 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8730 : {
8731 168 : poly_int64 bitoffset;
8732 168 : poly_uint64 field_offset, fld_offset;
8733 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8734 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8735 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8736 : else
8737 : bitoffset = 0;
8738 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8739 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8740 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8741 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8742 168 : if (!bitoffset.is_constant (&bitoff)
8743 168 : || bitoff < 0
8744 168 : || bitoff > diff)
8745 0 : return NULL_TREE;
8746 : }
8747 : else
8748 : {
8749 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8750 : return NULL_TREE;
8751 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8752 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8753 12 : bpos %= BITS_PER_UNIT;
8754 12 : fieldsize += bpos;
8755 12 : fieldsize += BITS_PER_UNIT - 1;
8756 12 : fieldsize /= BITS_PER_UNIT;
8757 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8758 12 : if (repr_type == NULL_TREE)
8759 : return NULL_TREE;
8760 12 : sz = int_size_in_bytes (repr_type);
8761 12 : if (sz < 0 || sz > len)
8762 : return NULL_TREE;
8763 12 : pos = int_byte_position (field);
8764 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8765 : return NULL_TREE;
8766 12 : HOST_WIDE_INT rpos;
8767 12 : if (pos + sz <= len)
8768 : rpos = pos;
8769 : else
8770 : {
8771 0 : rpos = len - sz;
8772 0 : gcc_assert (rpos <= pos);
8773 : }
8774 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8775 12 : pos = rpos;
8776 12 : diff = (TYPE_PRECISION (repr_type)
8777 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8778 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8779 12 : if (v == NULL_TREE)
8780 : return NULL_TREE;
8781 : fld = NULL_TREE;
8782 : }
8783 : }
8784 :
8785 168 : if (fld)
8786 : {
8787 1126 : sz = int_size_in_bytes (TREE_TYPE (fld));
8788 1126 : if (sz < 0 || sz > len)
8789 : return NULL_TREE;
8790 1126 : tree byte_pos = byte_position (fld);
8791 1126 : if (!tree_fits_shwi_p (byte_pos))
8792 : return NULL_TREE;
8793 1126 : pos = tree_to_shwi (byte_pos);
8794 1126 : if (pos < 0 || pos > len || pos + sz > len)
8795 : return NULL_TREE;
8796 : }
8797 1126 : if (fld == NULL_TREE)
8798 : /* Already handled above. */;
8799 1126 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8800 : {
8801 830 : v = native_interpret_expr (TREE_TYPE (fld),
8802 830 : ptr + off + pos, sz);
8803 830 : if (v == NULL_TREE)
8804 : return NULL_TREE;
8805 : }
8806 296 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8807 296 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8808 296 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8809 308 : if (v == NULL_TREE)
8810 : return NULL_TREE;
8811 1138 : if (fld != field)
8812 : {
8813 180 : if (TREE_CODE (v) != INTEGER_CST)
8814 : return NULL_TREE;
8815 :
8816 : /* FIXME: Figure out how to handle PDP endian bitfields. */
8817 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8818 : return NULL_TREE;
8819 180 : if (!BYTES_BIG_ENDIAN)
8820 180 : v = wide_int_to_tree (TREE_TYPE (field),
8821 360 : wi::lrshift (wi::to_wide (v), bitoff));
8822 : else
8823 : v = wide_int_to_tree (TREE_TYPE (field),
8824 : wi::lrshift (wi::to_wide (v),
8825 : diff - bitoff));
8826 : }
8827 1138 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8828 : }
8829 510 : return build_constructor (type, elts);
8830 : }
8831 :
8832 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8833 : or extracted constant positions and/or sizes aren't byte aligned. */
8834 :
8835 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8836 : bits between adjacent elements. AMNT should be within
8837 : [0, BITS_PER_UNIT).
8838 : Example, AMNT = 2:
8839 : 00011111|11100000 << 2 = 01111111|10000000
8840 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8841 :
8842 : void
8843 27211 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8844 : unsigned int amnt)
8845 : {
8846 27211 : if (amnt == 0)
8847 : return;
8848 :
8849 15800 : unsigned char carry_over = 0U;
8850 15800 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8851 15800 : unsigned char clear_mask = (~0U) << amnt;
8852 :
8853 93945 : for (unsigned int i = 0; i < sz; i++)
8854 : {
8855 78145 : unsigned prev_carry_over = carry_over;
8856 78145 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8857 :
8858 78145 : ptr[i] <<= amnt;
8859 78145 : if (i != 0)
8860 : {
8861 62345 : ptr[i] &= clear_mask;
8862 62345 : ptr[i] |= prev_carry_over;
8863 : }
8864 : }
8865 : }
8866 :
8867 : /* Like shift_bytes_in_array_left but for big-endian.
8868 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
8869 : bits between adjacent elements. AMNT should be within
8870 : [0, BITS_PER_UNIT).
8871 : Example, AMNT = 2:
8872 : 00011111|11100000 >> 2 = 00000111|11111000
8873 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
8874 :
8875 : void
8876 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
8877 : unsigned int amnt)
8878 : {
8879 8 : if (amnt == 0)
8880 : return;
8881 :
8882 4 : unsigned char carry_over = 0U;
8883 4 : unsigned char carry_mask = ~(~0U << amnt);
8884 :
8885 12 : for (unsigned int i = 0; i < sz; i++)
8886 : {
8887 8 : unsigned prev_carry_over = carry_over;
8888 8 : carry_over = ptr[i] & carry_mask;
8889 :
8890 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
8891 8 : ptr[i] >>= amnt;
8892 8 : ptr[i] |= prev_carry_over;
8893 : }
8894 : }
8895 :
8896 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
8897 : directly on the VECTOR_CST encoding, in a way that works for variable-
8898 : length vectors. Return the resulting VECTOR_CST on success or null
8899 : on failure. */
8900 :
8901 : static tree
8902 145131 : fold_view_convert_vector_encoding (tree type, tree expr)
8903 : {
8904 145131 : tree expr_type = TREE_TYPE (expr);
8905 145131 : poly_uint64 type_bits, expr_bits;
8906 145131 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
8907 145131 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
8908 0 : return NULL_TREE;
8909 :
8910 145131 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
8911 145131 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
8912 145131 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
8913 145131 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
8914 :
8915 : /* We can only preserve the semantics of a stepped pattern if the new
8916 : vector element is an integer of the same size. */
8917 145131 : if (VECTOR_CST_STEPPED_P (expr)
8918 145131 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
8919 : return NULL_TREE;
8920 :
8921 : /* The number of bits needed to encode one element from every pattern
8922 : of the original vector. */
8923 136224 : unsigned int expr_sequence_bits
8924 136224 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
8925 :
8926 : /* The number of bits needed to encode one element from every pattern
8927 : of the result. */
8928 136224 : unsigned int type_sequence_bits
8929 136224 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
8930 :
8931 : /* Don't try to read more bytes than are available, which can happen
8932 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
8933 : The general VIEW_CONVERT handling can cope with that case, so there's
8934 : no point complicating things here. */
8935 136224 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
8936 136224 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
8937 : BITS_PER_UNIT);
8938 136224 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
8939 136224 : if (known_gt (buffer_bits, expr_bits))
8940 : return NULL_TREE;
8941 :
8942 : /* Get enough bytes of EXPR to form the new encoding. */
8943 136224 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
8944 136224 : buffer.quick_grow (buffer_bytes);
8945 136224 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
8946 136224 : buffer_bits / expr_elt_bits)
8947 : != (int) buffer_bytes)
8948 : return NULL_TREE;
8949 :
8950 : /* Re-encode the bytes as TYPE. */
8951 136224 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
8952 272448 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
8953 136224 : type_npatterns, nelts_per_pattern);
8954 136224 : }
8955 :
8956 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8957 : TYPE at compile-time. If we're unable to perform the conversion
8958 : return NULL_TREE. */
8959 :
8960 : static tree
8961 12413694 : fold_view_convert_expr (tree type, tree expr)
8962 : {
8963 12413694 : unsigned char buffer[128];
8964 12413694 : unsigned char *buf;
8965 12413694 : int len;
8966 12413694 : HOST_WIDE_INT l;
8967 :
8968 : /* Check that the host and target are sane. */
8969 12413694 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8970 : return NULL_TREE;
8971 :
8972 12413694 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
8973 145131 : if (tree res = fold_view_convert_vector_encoding (type, expr))
8974 : return res;
8975 :
8976 12277537 : l = int_size_in_bytes (type);
8977 12277537 : if (l > (int) sizeof (buffer)
8978 12277537 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
8979 : {
8980 0 : buf = XALLOCAVEC (unsigned char, l);
8981 0 : len = l;
8982 : }
8983 : else
8984 : {
8985 : buf = buffer;
8986 : len = sizeof (buffer);
8987 : }
8988 12277537 : len = native_encode_expr (expr, buf, len);
8989 12277537 : if (len == 0)
8990 : return NULL_TREE;
8991 :
8992 1743206 : return native_interpret_expr (type, buf, len);
8993 : }
8994 :
8995 : /* Build an expression for the address of T. Folds away INDIRECT_REF
8996 : to avoid confusing the gimplify process. */
8997 :
8998 : tree
8999 533849304 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9000 : {
9001 : /* The size of the object is not relevant when talking about its address. */
9002 533849304 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9003 0 : t = TREE_OPERAND (t, 0);
9004 :
9005 533849304 : if (INDIRECT_REF_P (t))
9006 : {
9007 59169788 : t = TREE_OPERAND (t, 0);
9008 :
9009 59169788 : if (TREE_TYPE (t) != ptrtype)
9010 37894159 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9011 : }
9012 474679516 : else if (TREE_CODE (t) == MEM_REF
9013 474679516 : && integer_zerop (TREE_OPERAND (t, 1)))
9014 : {
9015 1642379 : t = TREE_OPERAND (t, 0);
9016 :
9017 1642379 : if (TREE_TYPE (t) != ptrtype)
9018 1149457 : t = fold_convert_loc (loc, ptrtype, t);
9019 : }
9020 473037137 : else if (TREE_CODE (t) == MEM_REF
9021 473037137 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9022 661 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9023 : TREE_OPERAND (t, 0),
9024 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9025 473036476 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9026 : {
9027 28390149 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9028 :
9029 28390149 : if (TREE_TYPE (t) != ptrtype)
9030 14978 : t = fold_convert_loc (loc, ptrtype, t);
9031 : }
9032 : else
9033 444646327 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9034 :
9035 : return t;
9036 : }
9037 :
9038 : /* Build an expression for the address of T. */
9039 :
9040 : tree
9041 473572733 : build_fold_addr_expr_loc (location_t loc, tree t)
9042 : {
9043 473572733 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9044 :
9045 473572733 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9046 : }
9047 :
9048 : /* Fold a unary expression of code CODE and type TYPE with operand
9049 : OP0. Return the folded expression if folding is successful.
9050 : Otherwise, return NULL_TREE. */
9051 :
9052 : tree
9053 1923928292 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9054 : {
9055 1923928292 : tree tem;
9056 1923928292 : tree arg0;
9057 1923928292 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9058 :
9059 1923928292 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9060 : && TREE_CODE_LENGTH (code) == 1);
9061 :
9062 1923928292 : arg0 = op0;
9063 1923928292 : if (arg0)
9064 : {
9065 1923915040 : if (CONVERT_EXPR_CODE_P (code)
9066 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9067 : {
9068 : /* Don't use STRIP_NOPS, because signedness of argument type
9069 : matters. */
9070 1069974504 : STRIP_SIGN_NOPS (arg0);
9071 : }
9072 : else
9073 : {
9074 : /* Strip any conversions that don't change the mode. This
9075 : is safe for every expression, except for a comparison
9076 : expression because its signedness is derived from its
9077 : operands.
9078 :
9079 : Note that this is done as an internal manipulation within
9080 : the constant folder, in order to find the simplest
9081 : representation of the arguments so that their form can be
9082 : studied. In any cases, the appropriate type conversions
9083 : should be put back in the tree that will get out of the
9084 : constant folder. */
9085 853940536 : STRIP_NOPS (arg0);
9086 : }
9087 :
9088 1923915040 : if (CONSTANT_CLASS_P (arg0))
9089 : {
9090 308325824 : tree tem = const_unop (code, type, arg0);
9091 308325824 : if (tem)
9092 : {
9093 270571045 : if (TREE_TYPE (tem) != type)
9094 9391 : tem = fold_convert_loc (loc, type, tem);
9095 270571045 : return tem;
9096 : }
9097 : }
9098 : }
9099 :
9100 1653357247 : tem = generic_simplify (loc, code, type, op0);
9101 1653357247 : if (tem)
9102 : return tem;
9103 :
9104 1248722589 : if (TREE_CODE_CLASS (code) == tcc_unary)
9105 : {
9106 692328311 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9107 1046155 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9108 : fold_build1_loc (loc, code, type,
9109 1046155 : fold_convert_loc (loc, TREE_TYPE (op0),
9110 2092310 : TREE_OPERAND (arg0, 1))));
9111 691282156 : else if (TREE_CODE (arg0) == COND_EXPR)
9112 : {
9113 510727 : tree arg01 = TREE_OPERAND (arg0, 1);
9114 510727 : tree arg02 = TREE_OPERAND (arg0, 2);
9115 510727 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9116 506545 : arg01 = fold_build1_loc (loc, code, type,
9117 : fold_convert_loc (loc,
9118 506545 : TREE_TYPE (op0), arg01));
9119 510727 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9120 500763 : arg02 = fold_build1_loc (loc, code, type,
9121 : fold_convert_loc (loc,
9122 500763 : TREE_TYPE (op0), arg02));
9123 510727 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9124 : arg01, arg02);
9125 :
9126 : /* If this was a conversion, and all we did was to move into
9127 : inside the COND_EXPR, bring it back out. But leave it if
9128 : it is a conversion from integer to integer and the
9129 : result precision is no wider than a word since such a
9130 : conversion is cheap and may be optimized away by combine,
9131 : while it couldn't if it were outside the COND_EXPR. Then return
9132 : so we don't get into an infinite recursion loop taking the
9133 : conversion out and then back in. */
9134 :
9135 510727 : if ((CONVERT_EXPR_CODE_P (code)
9136 10250 : || code == NON_LVALUE_EXPR)
9137 500496 : && TREE_CODE (tem) == COND_EXPR
9138 480926 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9139 419221 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9140 266748 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9141 266536 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9142 266536 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9143 266536 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9144 797353 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9145 20912 : && (INTEGRAL_TYPE_P
9146 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9147 20872 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9148 20759 : || flag_syntax_only))
9149 244995 : tem = build1_loc (loc, code, type,
9150 : build3 (COND_EXPR,
9151 244995 : TREE_TYPE (TREE_OPERAND
9152 : (TREE_OPERAND (tem, 1), 0)),
9153 244995 : TREE_OPERAND (tem, 0),
9154 244995 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9155 244995 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9156 : 0)));
9157 510727 : return tem;
9158 : }
9159 : }
9160 :
9161 1247165707 : switch (code)
9162 : {
9163 40860354 : case NON_LVALUE_EXPR:
9164 40860354 : if (!maybe_lvalue_p (op0))
9165 29584164 : return fold_convert_loc (loc, type, op0);
9166 : return NULL_TREE;
9167 :
9168 640806389 : CASE_CONVERT:
9169 640806389 : case FLOAT_EXPR:
9170 640806389 : case FIX_TRUNC_EXPR:
9171 640806389 : if (COMPARISON_CLASS_P (op0))
9172 : {
9173 : /* If we have (type) (a CMP b) and type is an integral type, return
9174 : new expression involving the new type. Canonicalize
9175 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9176 : non-integral type.
9177 : Do not fold the result as that would not simplify further, also
9178 : folding again results in recursions. */
9179 710546 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9180 155579 : return build2_loc (loc, TREE_CODE (op0), type,
9181 155579 : TREE_OPERAND (op0, 0),
9182 311158 : TREE_OPERAND (op0, 1));
9183 554967 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9184 7665 : && TREE_CODE (type) != VECTOR_TYPE)
9185 7665 : return build3_loc (loc, COND_EXPR, type, op0,
9186 : constant_boolean_node (true, type),
9187 7665 : constant_boolean_node (false, type));
9188 : }
9189 :
9190 : /* Handle (T *)&A.B.C for A being of type T and B and C
9191 : living at offset zero. This occurs frequently in
9192 : C++ upcasting and then accessing the base. */
9193 640643145 : if (TREE_CODE (op0) == ADDR_EXPR
9194 202900924 : && POINTER_TYPE_P (type)
9195 838495489 : && handled_component_p (TREE_OPERAND (op0, 0)))
9196 : {
9197 40167792 : poly_int64 bitsize, bitpos;
9198 40167792 : tree offset;
9199 40167792 : machine_mode mode;
9200 40167792 : int unsignedp, reversep, volatilep;
9201 40167792 : tree base
9202 40167792 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9203 : &offset, &mode, &unsignedp, &reversep,
9204 : &volatilep);
9205 : /* If the reference was to a (constant) zero offset, we can use
9206 : the address of the base if it has the same base type
9207 : as the result type and the pointer type is unqualified. */
9208 40167792 : if (!offset
9209 40027461 : && known_eq (bitpos, 0)
9210 26892579 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9211 26892579 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9212 40180308 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9213 12315 : return fold_convert_loc (loc, type,
9214 12315 : build_fold_addr_expr_loc (loc, base));
9215 : }
9216 :
9217 640630830 : if (TREE_CODE (op0) == MODIFY_EXPR
9218 284995 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9219 : /* Detect assigning a bitfield. */
9220 640632808 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9221 118 : && DECL_BIT_FIELD
9222 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9223 : {
9224 : /* Don't leave an assignment inside a conversion
9225 : unless assigning a bitfield. */
9226 1930 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9227 : /* First do the assignment, then return converted constant. */
9228 1930 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9229 1930 : suppress_warning (tem /* What warning? */);
9230 1930 : TREE_USED (tem) = 1;
9231 1930 : return tem;
9232 : }
9233 :
9234 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9235 : constants (if x has signed type, the sign bit cannot be set
9236 : in c). This folds extension into the BIT_AND_EXPR.
9237 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9238 : very likely don't have maximal range for their precision and this
9239 : transformation effectively doesn't preserve non-maximal ranges. */
9240 640628900 : if (TREE_CODE (type) == INTEGER_TYPE
9241 235574197 : && TREE_CODE (op0) == BIT_AND_EXPR
9242 641096887 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9243 : {
9244 138426 : tree and_expr = op0;
9245 138426 : tree and0 = TREE_OPERAND (and_expr, 0);
9246 138426 : tree and1 = TREE_OPERAND (and_expr, 1);
9247 138426 : int change = 0;
9248 :
9249 138426 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9250 138426 : || (TYPE_PRECISION (type)
9251 50984 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9252 : change = 1;
9253 14398 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9254 : <= HOST_BITS_PER_WIDE_INT
9255 14398 : && tree_fits_uhwi_p (and1))
9256 : {
9257 13288 : unsigned HOST_WIDE_INT cst;
9258 :
9259 13288 : cst = tree_to_uhwi (and1);
9260 26576 : cst &= HOST_WIDE_INT_M1U
9261 13288 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9262 13288 : change = (cst == 0);
9263 13288 : if (change
9264 13288 : && !flag_syntax_only
9265 26576 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9266 : == ZERO_EXTEND))
9267 : {
9268 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9269 : and0 = fold_convert_loc (loc, uns, and0);
9270 : and1 = fold_convert_loc (loc, uns, and1);
9271 : }
9272 : }
9273 13288 : if (change)
9274 : {
9275 137316 : tree and1_type = TREE_TYPE (and1);
9276 137316 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9277 : TYPE_PRECISION (type));
9278 137316 : tem = force_fit_type (type,
9279 137316 : wide_int::from (wi::to_wide (and1), prec,
9280 137316 : TYPE_SIGN (and1_type)),
9281 137316 : 0, TREE_OVERFLOW (and1));
9282 137316 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9283 137316 : fold_convert_loc (loc, type, and0), tem);
9284 : }
9285 : }
9286 :
9287 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9288 : cast (T1)X will fold away. We assume that this happens when X itself
9289 : is a cast. */
9290 640491584 : if (POINTER_TYPE_P (type)
9291 371848280 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9292 670326114 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9293 : {
9294 27401907 : tree arg00 = TREE_OPERAND (arg0, 0);
9295 27401907 : tree arg01 = TREE_OPERAND (arg0, 1);
9296 :
9297 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9298 : when the pointed type needs higher alignment than
9299 : the p+ first operand's pointed type. */
9300 27401907 : if (!in_gimple_form
9301 27382990 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9302 27403121 : && (min_align_of_type (TREE_TYPE (type))
9303 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9304 : return NULL_TREE;
9305 :
9306 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9307 : when type is a reference type and arg00's type is not,
9308 : because arg00 could be validly nullptr and if arg01 doesn't return,
9309 : we don't want false positive binding of reference to nullptr. */
9310 27401840 : if (TREE_CODE (type) == REFERENCE_TYPE
9311 15414838 : && !in_gimple_form
9312 15414818 : && sanitize_flags_p (SANITIZE_NULL)
9313 27402271 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9314 : return NULL_TREE;
9315 :
9316 27401409 : arg00 = fold_convert_loc (loc, type, arg00);
9317 27401409 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9318 : }
9319 :
9320 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9321 : of the same precision, and X is an integer type not narrower than
9322 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9323 613089677 : if (INTEGRAL_TYPE_P (type)
9324 241423858 : && TREE_CODE (op0) == BIT_NOT_EXPR
9325 512319 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9326 512319 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9327 613417648 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9328 : {
9329 325595 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9330 395936 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9331 395934 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9332 263202 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9333 263202 : fold_convert_loc (loc, type, tem));
9334 : }
9335 :
9336 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9337 : type of X and Y (integer types only). */
9338 612826475 : if (INTEGRAL_TYPE_P (type)
9339 241160656 : && TREE_CODE (op0) == MULT_EXPR
9340 8950847 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9341 8930886 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9342 612923418 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9343 22772 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9344 : {
9345 : /* Be careful not to introduce new overflows. */
9346 96889 : tree mult_type;
9347 96889 : if (TYPE_OVERFLOW_WRAPS (type))
9348 : mult_type = type;
9349 : else
9350 2089 : mult_type = unsigned_type_for (type);
9351 :
9352 96889 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9353 : {
9354 193778 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9355 : fold_convert_loc (loc, mult_type,
9356 96889 : TREE_OPERAND (op0, 0)),
9357 : fold_convert_loc (loc, mult_type,
9358 96889 : TREE_OPERAND (op0, 1)));
9359 96889 : return fold_convert_loc (loc, type, tem);
9360 : }
9361 : }
9362 :
9363 : return NULL_TREE;
9364 :
9365 231042389 : case VIEW_CONVERT_EXPR:
9366 231042389 : if (TREE_CODE (op0) == MEM_REF)
9367 : {
9368 2529 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9369 14 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9370 2529 : tem = fold_build2_loc (loc, MEM_REF, type,
9371 2529 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9372 2529 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9373 2529 : return tem;
9374 : }
9375 :
9376 : return NULL_TREE;
9377 :
9378 4242281 : case NEGATE_EXPR:
9379 4242281 : tem = fold_negate_expr (loc, arg0);
9380 4242281 : if (tem)
9381 1547 : return fold_convert_loc (loc, type, tem);
9382 : return NULL_TREE;
9383 :
9384 2160855 : case ABS_EXPR:
9385 : /* Convert fabs((double)float) into (double)fabsf(float). */
9386 2160855 : if (TREE_CODE (arg0) == NOP_EXPR
9387 19967 : && TREE_CODE (type) == REAL_TYPE)
9388 : {
9389 19911 : tree targ0 = strip_float_extensions (arg0);
9390 19911 : if (targ0 != arg0)
9391 19707 : return fold_convert_loc (loc, type,
9392 : fold_build1_loc (loc, ABS_EXPR,
9393 19707 : TREE_TYPE (targ0),
9394 19707 : targ0));
9395 : }
9396 : return NULL_TREE;
9397 :
9398 2695529 : case BIT_NOT_EXPR:
9399 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9400 2695529 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9401 2697214 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9402 : fold_convert_loc (loc, type,
9403 1685 : TREE_OPERAND (arg0, 0)))))
9404 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9405 : fold_convert_loc (loc, type,
9406 28 : TREE_OPERAND (arg0, 1)));
9407 2695515 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9408 2697186 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9409 : fold_convert_loc (loc, type,
9410 1671 : TREE_OPERAND (arg0, 1)))))
9411 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9412 : fold_convert_loc (loc, type,
9413 46 : TREE_OPERAND (arg0, 0)), tem);
9414 :
9415 : return NULL_TREE;
9416 :
9417 48070633 : case TRUTH_NOT_EXPR:
9418 : /* Note that the operand of this must be an int
9419 : and its values must be 0 or 1.
9420 : ("true" is a fixed value perhaps depending on the language,
9421 : but we don't handle values other than 1 correctly yet.) */
9422 48070633 : tem = fold_truth_not_expr (loc, arg0);
9423 48070633 : if (!tem)
9424 : return NULL_TREE;
9425 33559647 : return fold_convert_loc (loc, type, tem);
9426 :
9427 65913416 : case INDIRECT_REF:
9428 : /* Fold *&X to X if X is an lvalue. */
9429 65913416 : if (TREE_CODE (op0) == ADDR_EXPR)
9430 : {
9431 7509 : tree op00 = TREE_OPERAND (op0, 0);
9432 7509 : if ((VAR_P (op00)
9433 : || TREE_CODE (op00) == PARM_DECL
9434 : || TREE_CODE (op00) == RESULT_DECL)
9435 6337 : && !TREE_READONLY (op00))
9436 : return op00;
9437 : }
9438 : return NULL_TREE;
9439 :
9440 : default:
9441 : return NULL_TREE;
9442 : } /* switch (code) */
9443 : }
9444 :
9445 :
9446 : /* If the operation was a conversion do _not_ mark a resulting constant
9447 : with TREE_OVERFLOW if the original constant was not. These conversions
9448 : have implementation defined behavior and retaining the TREE_OVERFLOW
9449 : flag here would confuse later passes such as VRP. */
9450 : tree
9451 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9452 : tree type, tree op0)
9453 : {
9454 0 : tree res = fold_unary_loc (loc, code, type, op0);
9455 0 : if (res
9456 0 : && TREE_CODE (res) == INTEGER_CST
9457 0 : && TREE_CODE (op0) == INTEGER_CST
9458 0 : && CONVERT_EXPR_CODE_P (code))
9459 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9460 :
9461 0 : return res;
9462 : }
9463 :
9464 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9465 : operands OP0 and OP1. LOC is the location of the resulting expression.
9466 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9467 : Return the folded expression if folding is successful. Otherwise,
9468 : return NULL_TREE. */
9469 : static tree
9470 24908159 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9471 : tree arg0, tree arg1, tree op0, tree op1)
9472 : {
9473 24908159 : tree tem;
9474 :
9475 : /* We only do these simplifications if we are optimizing. */
9476 24908159 : if (!optimize)
9477 : return NULL_TREE;
9478 :
9479 : /* Check for things like (A || B) && (A || C). We can convert this
9480 : to A || (B && C). Note that either operator can be any of the four
9481 : truth and/or operations and the transformation will still be
9482 : valid. Also note that we only care about order for the
9483 : ANDIF and ORIF operators. If B contains side effects, this
9484 : might change the truth-value of A. */
9485 24435946 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9486 5696133 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9487 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9488 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9489 5696133 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9490 24495450 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9491 : {
9492 59010 : tree a00 = TREE_OPERAND (arg0, 0);
9493 59010 : tree a01 = TREE_OPERAND (arg0, 1);
9494 59010 : tree a10 = TREE_OPERAND (arg1, 0);
9495 59010 : tree a11 = TREE_OPERAND (arg1, 1);
9496 118020 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9497 59010 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9498 59010 : && (code == TRUTH_AND_EXPR
9499 22407 : || code == TRUTH_OR_EXPR));
9500 :
9501 59010 : if (operand_equal_p (a00, a10, 0))
9502 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9503 849 : fold_build2_loc (loc, code, type, a01, a11));
9504 58161 : else if (commutative && operand_equal_p (a00, a11, 0))
9505 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9506 0 : fold_build2_loc (loc, code, type, a01, a10));
9507 58161 : else if (commutative && operand_equal_p (a01, a10, 0))
9508 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9509 0 : fold_build2_loc (loc, code, type, a00, a11));
9510 :
9511 : /* This case if tricky because we must either have commutative
9512 : operators or else A10 must not have side-effects. */
9513 :
9514 58117 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9515 115786 : && operand_equal_p (a01, a11, 0))
9516 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9517 : fold_build2_loc (loc, code, type, a00, a10),
9518 43 : a01);
9519 : }
9520 :
9521 : /* See if we can build a range comparison. */
9522 24435054 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9523 : return tem;
9524 :
9525 23299532 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9526 23297606 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9527 : {
9528 37631 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9529 37631 : if (tem)
9530 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9531 : }
9532 :
9533 23299519 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9534 23289046 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9535 : {
9536 85674 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9537 85674 : if (tem)
9538 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9539 : }
9540 :
9541 : /* Check for the possibility of merging component references. If our
9542 : lhs is another similar operation, try to merge its rhs with our
9543 : rhs. Then try to merge our lhs and rhs. */
9544 23299428 : if (TREE_CODE (arg0) == code
9545 24113142 : && (tem = fold_truth_andor_1 (loc, code, type,
9546 813714 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9547 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9548 :
9549 23299343 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9550 : return tem;
9551 :
9552 23257658 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9553 23257658 : if (param_logical_op_non_short_circuit != -1)
9554 7734 : logical_op_non_short_circuit
9555 7734 : = param_logical_op_non_short_circuit;
9556 23257658 : if (logical_op_non_short_circuit
9557 23253771 : && !sanitize_coverage_p ()
9558 23257658 : && (code == TRUTH_AND_EXPR
9559 23253768 : || code == TRUTH_ANDIF_EXPR
9560 10736975 : || code == TRUTH_OR_EXPR
9561 10736975 : || code == TRUTH_ORIF_EXPR))
9562 : {
9563 23253768 : enum tree_code ncode, icode;
9564 :
9565 23253768 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9566 23253768 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9567 12516793 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9568 :
9569 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9570 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9571 : We don't want to pack more than two leafs to a non-IF AND/OR
9572 : expression.
9573 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9574 : equal to IF-CODE, then we don't want to add right-hand operand.
9575 : If the inner right-hand side of left-hand operand has
9576 : side-effects, or isn't simple, then we can't add to it,
9577 : as otherwise we might destroy if-sequence. */
9578 23253768 : if (TREE_CODE (arg0) == icode
9579 804956 : && simple_condition_p (arg1)
9580 : /* Needed for sequence points to handle trappings, and
9581 : side-effects. */
9582 23300476 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9583 : {
9584 39872 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9585 : arg1);
9586 39872 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9587 39872 : tem);
9588 : }
9589 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9590 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9591 23213896 : else if (TREE_CODE (arg1) == icode
9592 4752 : && simple_condition_p (arg0)
9593 : /* Needed for sequence points to handle trappings, and
9594 : side-effects. */
9595 23214849 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9596 : {
9597 36 : tem = fold_build2_loc (loc, ncode, type,
9598 36 : arg0, TREE_OPERAND (arg1, 0));
9599 36 : return fold_build2_loc (loc, icode, type, tem,
9600 72 : TREE_OPERAND (arg1, 1));
9601 : }
9602 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9603 : into (A OR B).
9604 : For sequence point consistency, we need to check for trapping,
9605 : and side-effects. */
9606 5021462 : else if (code == icode && simple_condition_p (arg0)
9607 24063229 : && simple_condition_p (arg1))
9608 427071 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9609 : }
9610 :
9611 : return NULL_TREE;
9612 : }
9613 :
9614 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9615 : by changing CODE to reduce the magnitude of constants involved in
9616 : ARG0 of the comparison.
9617 : Returns a canonicalized comparison tree if a simplification was
9618 : possible, otherwise returns NULL_TREE. */
9619 :
9620 : static tree
9621 174073840 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9622 : tree arg0, tree arg1)
9623 : {
9624 174073840 : enum tree_code code0 = TREE_CODE (arg0);
9625 174073840 : tree t, cst0 = NULL_TREE;
9626 174073840 : int sgn0;
9627 :
9628 : /* Match A +- CST code arg1. We can change this only if overflow
9629 : is undefined. */
9630 174073840 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9631 132922059 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9632 : /* In principle pointers also have undefined overflow behavior,
9633 : but that causes problems elsewhere. */
9634 65653005 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9635 65653005 : && (code0 == MINUS_EXPR
9636 65653005 : || code0 == PLUS_EXPR)
9637 2612649 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9638 : return NULL_TREE;
9639 :
9640 : /* Identify the constant in arg0 and its sign. */
9641 2135504 : cst0 = TREE_OPERAND (arg0, 1);
9642 2135504 : sgn0 = tree_int_cst_sgn (cst0);
9643 :
9644 : /* Overflowed constants and zero will cause problems. */
9645 2135504 : if (integer_zerop (cst0)
9646 2135504 : || TREE_OVERFLOW (cst0))
9647 : return NULL_TREE;
9648 :
9649 : /* See if we can reduce the magnitude of the constant in
9650 : arg0 by changing the comparison code. */
9651 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9652 2135504 : if (code == LT_EXPR
9653 1258750 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9654 : code = LE_EXPR;
9655 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9656 1954705 : else if (code == GT_EXPR
9657 580745 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9658 : code = GE_EXPR;
9659 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9660 1772666 : else if (code == LE_EXPR
9661 673111 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9662 : code = LT_EXPR;
9663 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9664 1534616 : else if (code == GE_EXPR
9665 487872 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9666 : code = GT_EXPR;
9667 : else
9668 : return NULL_TREE;
9669 :
9670 : /* Now build the constant reduced in magnitude. But not if that
9671 : would produce one outside of its types range. */
9672 1607624 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9673 1607624 : && ((sgn0 == 1
9674 419951 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9675 419951 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9676 803812 : || (sgn0 == -1
9677 383861 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9678 383861 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9679 0 : return NULL_TREE;
9680 :
9681 1223763 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9682 803812 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9683 803812 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9684 803812 : t = fold_convert (TREE_TYPE (arg1), t);
9685 :
9686 803812 : return fold_build2_loc (loc, code, type, t, arg1);
9687 : }
9688 :
9689 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9690 : overflow further. Try to decrease the magnitude of constants involved
9691 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9692 : and put sole constants at the second argument position.
9693 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9694 :
9695 : static tree
9696 87417553 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9697 : tree arg0, tree arg1)
9698 : {
9699 87417553 : tree t;
9700 :
9701 : /* Try canonicalization by simplifying arg0. */
9702 87417553 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9703 87417553 : if (t)
9704 : return t;
9705 :
9706 : /* Try canonicalization by simplifying arg1 using the swapped
9707 : comparison. */
9708 86656287 : code = swap_tree_comparison (code);
9709 86656287 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9710 86656287 : return t;
9711 : }
9712 :
9713 : /* Return a positive integer when the symbol DECL is known to have
9714 : a nonzero address, zero when it's known not to (e.g., it's a weak
9715 : symbol), and a negative integer when the symbol is not yet in the
9716 : symbol table and so whether or not its address is zero is unknown.
9717 : For function local objects always return positive integer. */
9718 : static int
9719 11633428 : maybe_nonzero_address (tree decl)
9720 : {
9721 11633428 : if (!DECL_P (decl))
9722 : return -1;
9723 :
9724 : /* Normally, don't do anything for variables and functions before symtab is
9725 : built; it is quite possible that DECL will be declared weak later.
9726 : But if folding_initializer, we need a constant answer now, so create
9727 : the symtab entry and prevent later weak declaration. */
9728 9311340 : if (decl_in_symtab_p (decl))
9729 : {
9730 4227422 : if (struct symtab_node *symbol
9731 4227422 : = (folding_initializer
9732 4227422 : ? symtab_node::get_create (decl)
9733 4210396 : : symtab_node::get (decl)))
9734 4208429 : return symbol->nonzero_address ();
9735 : }
9736 5083918 : else if (folding_cxx_constexpr)
9737 : /* Anything that doesn't go in the symtab has non-zero address. */
9738 : return 1;
9739 :
9740 : /* Function local objects are never NULL. */
9741 4998824 : if (DECL_CONTEXT (decl)
9742 4982286 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9743 9977669 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9744 : return 1;
9745 :
9746 : return -1;
9747 : }
9748 :
9749 : /* Subroutine of fold_binary. This routine performs all of the
9750 : transformations that are common to the equality/inequality
9751 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9752 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9753 : fold_binary should call fold_binary. Fold a comparison with
9754 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9755 : the folded comparison or NULL_TREE. */
9756 :
9757 : static tree
9758 87492552 : fold_comparison (location_t loc, enum tree_code code, tree type,
9759 : tree op0, tree op1)
9760 : {
9761 87492552 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9762 87492552 : tree arg0, arg1, tem;
9763 :
9764 87492552 : arg0 = op0;
9765 87492552 : arg1 = op1;
9766 :
9767 87492552 : STRIP_SIGN_NOPS (arg0);
9768 87492552 : STRIP_SIGN_NOPS (arg1);
9769 :
9770 : /* For comparisons of pointers we can decompose it to a compile time
9771 : comparison of the base objects and the offsets into the object.
9772 : This requires at least one operand being an ADDR_EXPR or a
9773 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9774 161978225 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9775 87700274 : && (TREE_CODE (arg0) == ADDR_EXPR
9776 13162499 : || TREE_CODE (arg1) == ADDR_EXPR
9777 11696681 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9778 10952670 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9779 : {
9780 2271037 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9781 2271037 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9782 2271037 : machine_mode mode;
9783 2271037 : int volatilep, reversep, unsignedp;
9784 2271037 : bool indirect_base0 = false, indirect_base1 = false;
9785 :
9786 : /* Get base and offset for the access. Strip ADDR_EXPR for
9787 : get_inner_reference, but put it back by stripping INDIRECT_REF
9788 : off the base object if possible. indirect_baseN will be true
9789 : if baseN is not an address but refers to the object itself. */
9790 2271037 : base0 = arg0;
9791 2271037 : if (TREE_CODE (arg0) == ADDR_EXPR)
9792 : {
9793 52102 : base0
9794 52102 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9795 : &bitsize, &bitpos0, &offset0, &mode,
9796 : &unsignedp, &reversep, &volatilep);
9797 52102 : if (INDIRECT_REF_P (base0))
9798 2165 : base0 = TREE_OPERAND (base0, 0);
9799 : else
9800 : indirect_base0 = true;
9801 : }
9802 2218935 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9803 : {
9804 807711 : base0 = TREE_OPERAND (arg0, 0);
9805 807711 : STRIP_SIGN_NOPS (base0);
9806 807711 : if (TREE_CODE (base0) == ADDR_EXPR)
9807 : {
9808 31262 : base0
9809 31262 : = get_inner_reference (TREE_OPERAND (base0, 0),
9810 : &bitsize, &bitpos0, &offset0, &mode,
9811 : &unsignedp, &reversep, &volatilep);
9812 31262 : if (INDIRECT_REF_P (base0))
9813 20 : base0 = TREE_OPERAND (base0, 0);
9814 : else
9815 : indirect_base0 = true;
9816 : }
9817 807711 : if (offset0 == NULL_TREE || integer_zerop (offset0))
9818 807711 : offset0 = TREE_OPERAND (arg0, 1);
9819 : else
9820 0 : offset0 = size_binop (PLUS_EXPR, offset0,
9821 : TREE_OPERAND (arg0, 1));
9822 807711 : if (poly_int_tree_p (offset0))
9823 : {
9824 648239 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
9825 648239 : TYPE_PRECISION (sizetype));
9826 648239 : tem <<= LOG2_BITS_PER_UNIT;
9827 648239 : tem += bitpos0;
9828 648239 : if (tem.to_shwi (&bitpos0))
9829 648239 : offset0 = NULL_TREE;
9830 : }
9831 : }
9832 :
9833 2271037 : base1 = arg1;
9834 2271037 : if (TREE_CODE (arg1) == ADDR_EXPR)
9835 : {
9836 1494457 : base1
9837 1494457 : = get_inner_reference (TREE_OPERAND (arg1, 0),
9838 : &bitsize, &bitpos1, &offset1, &mode,
9839 : &unsignedp, &reversep, &volatilep);
9840 1494457 : if (INDIRECT_REF_P (base1))
9841 66304 : base1 = TREE_OPERAND (base1, 0);
9842 : else
9843 : indirect_base1 = true;
9844 : }
9845 776580 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9846 : {
9847 91614 : base1 = TREE_OPERAND (arg1, 0);
9848 91614 : STRIP_SIGN_NOPS (base1);
9849 91614 : if (TREE_CODE (base1) == ADDR_EXPR)
9850 : {
9851 10261 : base1
9852 10261 : = get_inner_reference (TREE_OPERAND (base1, 0),
9853 : &bitsize, &bitpos1, &offset1, &mode,
9854 : &unsignedp, &reversep, &volatilep);
9855 10261 : if (INDIRECT_REF_P (base1))
9856 0 : base1 = TREE_OPERAND (base1, 0);
9857 : else
9858 : indirect_base1 = true;
9859 : }
9860 91614 : if (offset1 == NULL_TREE || integer_zerop (offset1))
9861 91590 : offset1 = TREE_OPERAND (arg1, 1);
9862 : else
9863 24 : offset1 = size_binop (PLUS_EXPR, offset1,
9864 : TREE_OPERAND (arg1, 1));
9865 91614 : if (poly_int_tree_p (offset1))
9866 : {
9867 80954 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
9868 80954 : TYPE_PRECISION (sizetype));
9869 80954 : tem <<= LOG2_BITS_PER_UNIT;
9870 80954 : tem += bitpos1;
9871 80954 : if (tem.to_shwi (&bitpos1))
9872 80954 : offset1 = NULL_TREE;
9873 : }
9874 : }
9875 :
9876 : /* If we have equivalent bases we might be able to simplify. */
9877 2271037 : if (indirect_base0 == indirect_base1
9878 3062048 : && operand_equal_p (base0, base1,
9879 : indirect_base0 ? OEP_ADDRESS_OF : 0))
9880 : {
9881 : /* We can fold this expression to a constant if the non-constant
9882 : offset parts are equal. */
9883 21814 : if ((offset0 == offset1
9884 6618 : || (offset0 && offset1
9885 2660 : && operand_equal_p (offset0, offset1, 0)))
9886 21814 : && (equality_code
9887 9416 : || (indirect_base0
9888 6238 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
9889 3178 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9890 : {
9891 15156 : switch (code)
9892 : {
9893 56 : case EQ_EXPR:
9894 56 : if (known_eq (bitpos0, bitpos1))
9895 52734 : return constant_boolean_node (true, type);
9896 21 : if (known_ne (bitpos0, bitpos1))
9897 21 : return constant_boolean_node (false, type);
9898 : break;
9899 5724 : case NE_EXPR:
9900 5724 : if (known_ne (bitpos0, bitpos1))
9901 5719 : return constant_boolean_node (true, type);
9902 5 : if (known_eq (bitpos0, bitpos1))
9903 5 : return constant_boolean_node (false, type);
9904 : break;
9905 2382 : case LT_EXPR:
9906 2382 : if (known_lt (bitpos0, bitpos1))
9907 2246 : return constant_boolean_node (true, type);
9908 136 : if (known_ge (bitpos0, bitpos1))
9909 136 : return constant_boolean_node (false, type);
9910 : break;
9911 1644 : case LE_EXPR:
9912 1644 : if (known_le (bitpos0, bitpos1))
9913 181 : return constant_boolean_node (true, type);
9914 1463 : if (known_gt (bitpos0, bitpos1))
9915 1463 : return constant_boolean_node (false, type);
9916 : break;
9917 3549 : case GE_EXPR:
9918 3549 : if (known_ge (bitpos0, bitpos1))
9919 1369 : return constant_boolean_node (true, type);
9920 2180 : if (known_lt (bitpos0, bitpos1))
9921 2180 : return constant_boolean_node (false, type);
9922 : break;
9923 1801 : case GT_EXPR:
9924 1801 : if (known_gt (bitpos0, bitpos1))
9925 1742 : return constant_boolean_node (true, type);
9926 59 : if (known_le (bitpos0, bitpos1))
9927 59 : return constant_boolean_node (false, type);
9928 : break;
9929 : default:;
9930 : }
9931 : }
9932 : /* We can simplify the comparison to a comparison of the variable
9933 : offset parts if the constant offset parts are equal.
9934 : Be careful to use signed sizetype here because otherwise we
9935 : mess with array offsets in the wrong way. This is possible
9936 : because pointer arithmetic is restricted to retain within an
9937 : object and overflow on pointer differences is undefined as of
9938 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9939 6658 : else if (known_eq (bitpos0, bitpos1)
9940 6658 : && (equality_code
9941 5198 : || (indirect_base0
9942 271 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
9943 4927 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9944 : {
9945 : /* By converting to signed sizetype we cover middle-end pointer
9946 : arithmetic which operates on unsigned pointer types of size
9947 : type size and ARRAY_REF offsets which are properly sign or
9948 : zero extended from their type in case it is narrower than
9949 : sizetype. */
9950 5307 : if (offset0 == NULL_TREE)
9951 0 : offset0 = build_int_cst (ssizetype, 0);
9952 : else
9953 5307 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
9954 5307 : if (offset1 == NULL_TREE)
9955 2679 : offset1 = build_int_cst (ssizetype, 0);
9956 : else
9957 2628 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
9958 :
9959 5307 : return fold_build2_loc (loc, code, type, offset0, offset1);
9960 : }
9961 : }
9962 : /* For equal offsets we can simplify to a comparison of the
9963 : base addresses. */
9964 2249223 : else if (known_eq (bitpos0, bitpos1)
9965 54354 : && (indirect_base0
9966 970032 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9967 14692 : && (indirect_base1
9968 183470 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9969 2469381 : && ((offset0 == offset1)
9970 4481 : || (offset0 && offset1
9971 4211 : && operand_equal_p (offset0, offset1, 0))))
9972 : {
9973 32208 : if (indirect_base0)
9974 2764 : base0 = build_fold_addr_expr_loc (loc, base0);
9975 32208 : if (indirect_base1)
9976 4279 : base1 = build_fold_addr_expr_loc (loc, base1);
9977 32208 : return fold_build2_loc (loc, code, type, base0, base1);
9978 : }
9979 : /* Comparison between an ordinary (non-weak) symbol and a null
9980 : pointer can be eliminated since such symbols must have a non
9981 : null address. In C, relational expressions between pointers
9982 : to objects and null pointers are undefined. The results
9983 : below follow the C++ rules with the additional property that
9984 : every object pointer compares greater than a null pointer.
9985 : */
9986 2217015 : else if (((DECL_P (base0)
9987 246281 : && maybe_nonzero_address (base0) > 0
9988 : /* Avoid folding references to struct members at offset 0 to
9989 : prevent tests like '&ptr->firstmember == 0' from getting
9990 : eliminated. When ptr is null, although the -> expression
9991 : is strictly speaking invalid, GCC retains it as a matter
9992 : of QoI. See PR c/44555. */
9993 231901 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
9994 2201850 : || CONSTANT_CLASS_P (base0))
9995 19989 : && indirect_base0
9996 : /* The caller guarantees that when one of the arguments is
9997 : constant (i.e., null in this case) it is second. */
9998 2234309 : && integer_zerop (arg1))
9999 : {
10000 63 : switch (code)
10001 : {
10002 24 : case EQ_EXPR:
10003 24 : case LE_EXPR:
10004 24 : case LT_EXPR:
10005 24 : return constant_boolean_node (false, type);
10006 39 : case GE_EXPR:
10007 39 : case GT_EXPR:
10008 39 : case NE_EXPR:
10009 39 : return constant_boolean_node (true, type);
10010 0 : default:
10011 0 : gcc_unreachable ();
10012 : }
10013 : }
10014 : }
10015 :
10016 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10017 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10018 : the resulting offset is smaller in absolute value than the
10019 : original one and has the same sign. */
10020 172334380 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10021 133727864 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10022 33229404 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10023 2356019 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10024 1933022 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10025 1933022 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10026 154475195 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10027 169064 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10028 : {
10029 169064 : tree const1 = TREE_OPERAND (arg0, 1);
10030 169064 : tree const2 = TREE_OPERAND (arg1, 1);
10031 169064 : tree variable1 = TREE_OPERAND (arg0, 0);
10032 169064 : tree variable2 = TREE_OPERAND (arg1, 0);
10033 169064 : tree cst;
10034 :
10035 : /* Put the constant on the side where it doesn't overflow and is
10036 : of lower absolute value and of same sign than before. */
10037 169065 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10038 : ? MINUS_EXPR : PLUS_EXPR,
10039 : const2, const1);
10040 169064 : if (!TREE_OVERFLOW (cst)
10041 169048 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10042 191329 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10043 5706 : return fold_build2_loc (loc, code, type,
10044 : variable1,
10045 5706 : fold_build2_loc (loc, TREE_CODE (arg1),
10046 5706 : TREE_TYPE (arg1),
10047 5706 : variable2, cst));
10048 :
10049 163359 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10050 : ? MINUS_EXPR : PLUS_EXPR,
10051 : const1, const2);
10052 163358 : if (!TREE_OVERFLOW (cst)
10053 163342 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10054 179917 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10055 16559 : return fold_build2_loc (loc, code, type,
10056 16559 : fold_build2_loc (loc, TREE_CODE (arg0),
10057 16559 : TREE_TYPE (arg0),
10058 : variable1, cst),
10059 16559 : variable2);
10060 : }
10061 :
10062 87417553 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10063 87417553 : if (tem)
10064 : return tem;
10065 :
10066 : /* If we are comparing an expression that just has comparisons
10067 : of two integer values, arithmetic expressions of those comparisons,
10068 : and constants, we can simplify it. There are only three cases
10069 : to check: the two values can either be equal, the first can be
10070 : greater, or the second can be greater. Fold the expression for
10071 : those three values. Since each value must be 0 or 1, we have
10072 : eight possibilities, each of which corresponds to the constant 0
10073 : or 1 or one of the six possible comparisons.
10074 :
10075 : This handles common cases like (a > b) == 0 but also handles
10076 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10077 : occur in macroized code. */
10078 :
10079 86613741 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10080 : {
10081 52775825 : tree cval1 = 0, cval2 = 0;
10082 :
10083 52775825 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10084 : /* Don't handle degenerate cases here; they should already
10085 : have been handled anyway. */
10086 476074 : && cval1 != 0 && cval2 != 0
10087 474888 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10088 474888 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10089 474882 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10090 59 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10091 59 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10092 52775884 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10093 59 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10094 : {
10095 59 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10096 59 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10097 :
10098 : /* We can't just pass T to eval_subst in case cval1 or cval2
10099 : was the same as ARG1. */
10100 :
10101 59 : tree high_result
10102 59 : = fold_build2_loc (loc, code, type,
10103 : eval_subst (loc, arg0, cval1, maxval,
10104 : cval2, minval),
10105 : arg1);
10106 59 : tree equal_result
10107 59 : = fold_build2_loc (loc, code, type,
10108 : eval_subst (loc, arg0, cval1, maxval,
10109 : cval2, maxval),
10110 : arg1);
10111 59 : tree low_result
10112 59 : = fold_build2_loc (loc, code, type,
10113 : eval_subst (loc, arg0, cval1, minval,
10114 : cval2, maxval),
10115 : arg1);
10116 :
10117 : /* All three of these results should be 0 or 1. Confirm they are.
10118 : Then use those values to select the proper code to use. */
10119 :
10120 59 : if (TREE_CODE (high_result) == INTEGER_CST
10121 50 : && TREE_CODE (equal_result) == INTEGER_CST
10122 40 : && TREE_CODE (low_result) == INTEGER_CST)
10123 : {
10124 : /* Make a 3-bit mask with the high-order bit being the
10125 : value for `>', the next for '=', and the low for '<'. */
10126 40 : switch ((integer_onep (high_result) * 4)
10127 40 : + (integer_onep (equal_result) * 2)
10128 40 : + integer_onep (low_result))
10129 : {
10130 21 : case 0:
10131 : /* Always false. */
10132 40 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10133 : case 1:
10134 : code = LT_EXPR;
10135 : break;
10136 2 : case 2:
10137 2 : code = EQ_EXPR;
10138 2 : break;
10139 0 : case 3:
10140 0 : code = LE_EXPR;
10141 0 : break;
10142 0 : case 4:
10143 0 : code = GT_EXPR;
10144 0 : break;
10145 1 : case 5:
10146 1 : code = NE_EXPR;
10147 1 : break;
10148 0 : case 6:
10149 0 : code = GE_EXPR;
10150 0 : break;
10151 16 : case 7:
10152 : /* Always true. */
10153 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10154 : }
10155 :
10156 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10157 : }
10158 : }
10159 : }
10160 :
10161 : return NULL_TREE;
10162 : }
10163 :
10164 :
10165 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10166 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10167 : argument EXPR represents the expression "z" of type TYPE. */
10168 :
10169 : static tree
10170 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10171 : {
10172 2 : tree itype = TREE_TYPE (type);
10173 2 : tree rpart, ipart, tem;
10174 :
10175 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10176 : {
10177 0 : rpart = TREE_OPERAND (expr, 0);
10178 0 : ipart = TREE_OPERAND (expr, 1);
10179 : }
10180 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10181 : {
10182 0 : rpart = TREE_REALPART (expr);
10183 0 : ipart = TREE_IMAGPART (expr);
10184 : }
10185 : else
10186 : {
10187 2 : expr = save_expr (expr);
10188 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10189 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10190 : }
10191 :
10192 2 : rpart = save_expr (rpart);
10193 2 : ipart = save_expr (ipart);
10194 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10195 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10196 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10197 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10198 2 : build_zero_cst (itype));
10199 : }
10200 :
10201 :
10202 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10203 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10204 : true if successful. */
10205 :
10206 : static bool
10207 19409 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10208 : {
10209 19409 : unsigned HOST_WIDE_INT i, nunits;
10210 :
10211 19409 : if (TREE_CODE (arg) == VECTOR_CST
10212 19409 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10213 : {
10214 2168 : for (i = 0; i < nunits; ++i)
10215 1710 : elts[i] = VECTOR_CST_ELT (arg, i);
10216 : }
10217 18951 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10218 : {
10219 : constructor_elt *elt;
10220 :
10221 38387 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10222 33434 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10223 13998 : return false;
10224 : else
10225 19436 : elts[i] = elt->value;
10226 : }
10227 : else
10228 : return false;
10229 6185 : for (; i < nelts; i++)
10230 1548 : elts[i]
10231 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10232 : return true;
10233 : }
10234 :
10235 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10236 : mask for VLA vec_perm folding.
10237 : REASON if specified, will contain the reason why SEL is not suitable.
10238 : Used only for debugging and unit-testing. */
10239 :
10240 : static bool
10241 16050 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10242 : const vec_perm_indices &sel,
10243 : const char **reason = NULL)
10244 : {
10245 16050 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10246 16050 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10247 :
10248 32100 : if (!(pow2p_hwi (sel_npatterns)
10249 16050 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10250 16050 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10251 : {
10252 0 : if (reason)
10253 0 : *reason = "npatterns is not power of 2";
10254 0 : return false;
10255 : }
10256 :
10257 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10258 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10259 16050 : poly_uint64 esel;
10260 16050 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10261 : {
10262 0 : if (reason)
10263 0 : *reason = "sel.length is not multiple of sel_npatterns";
10264 0 : return false;
10265 : }
10266 :
10267 16050 : if (sel_nelts_per_pattern < 3)
10268 : return true;
10269 :
10270 5760 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10271 : {
10272 4363 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10273 4363 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10274 4363 : HOST_WIDE_INT step;
10275 4363 : if (!poly_int64 (a2 - a1).is_constant (&step))
10276 : {
10277 : if (reason)
10278 : *reason = "step is not constant";
10279 1064 : return false;
10280 : }
10281 : // FIXME: Punt on step < 0 for now, revisit later.
10282 4363 : if (step < 0)
10283 : return false;
10284 4308 : if (step == 0)
10285 0 : continue;
10286 :
10287 4308 : if (!pow2p_hwi (step))
10288 : {
10289 0 : if (reason)
10290 0 : *reason = "step is not power of 2";
10291 0 : return false;
10292 : }
10293 :
10294 : /* Ensure that stepped sequence of the pattern selects elements
10295 : only from the same input vector. */
10296 4308 : uint64_t q1, qe;
10297 4308 : poly_uint64 r1, re;
10298 4308 : poly_uint64 ae = a1 + (esel - 2) * step;
10299 4308 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10300 :
10301 4308 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10302 4308 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10303 : && q1 == qe))
10304 : {
10305 406 : if (reason)
10306 0 : *reason = "crossed input vectors";
10307 406 : return false;
10308 : }
10309 :
10310 : /* Ensure that the stepped sequence always selects from the same
10311 : input pattern. */
10312 3902 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10313 3902 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10314 :
10315 3902 : if (!multiple_p (step, arg_npatterns))
10316 : {
10317 601 : if (reason)
10318 0 : *reason = "step is not multiple of npatterns";
10319 601 : return false;
10320 : }
10321 :
10322 : /* If a1 chooses base element from arg, ensure that it's a natural
10323 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10324 : to preserve arg's encoding. */
10325 :
10326 3301 : if (maybe_lt (r1, arg_npatterns))
10327 : {
10328 13 : unsigned HOST_WIDE_INT index;
10329 13 : if (!r1.is_constant (&index))
10330 2 : return false;
10331 :
10332 13 : tree arg_elem0 = vector_cst_elt (arg, index);
10333 13 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10334 13 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10335 :
10336 13 : tree step1, step2;
10337 13 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10338 13 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10339 26 : || !operand_equal_p (step1, step2, 0))
10340 : {
10341 2 : if (reason)
10342 0 : *reason = "not a natural stepped sequence";
10343 2 : return false;
10344 : }
10345 : }
10346 : }
10347 :
10348 : return true;
10349 : }
10350 :
10351 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10352 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10353 : REASON has same purpose as described in
10354 : valid_mask_for_fold_vec_perm_cst_p. */
10355 :
10356 : static tree
10357 16050 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10358 : const char **reason = NULL)
10359 : {
10360 16050 : unsigned res_npatterns, res_nelts_per_pattern;
10361 16050 : unsigned HOST_WIDE_INT res_nelts;
10362 :
10363 : /* First try to implement the fold in a VLA-friendly way.
10364 :
10365 : (1) If the selector is simply a duplication of N elements, the
10366 : result is likewise a duplication of N elements.
10367 :
10368 : (2) If the selector is N elements followed by a duplication
10369 : of N elements, the result is too.
10370 :
10371 : (3) If the selector is N elements followed by an interleaving
10372 : of N linear series, the situation is more complex.
10373 :
10374 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10375 : can handle this case. If we can, then each of the N linear
10376 : series either (a) selects the same element each time or
10377 : (b) selects a linear series from one of the input patterns.
10378 :
10379 : If (b) holds for one of the linear series, the result
10380 : will contain a linear series, and so the result will have
10381 : the same shape as the selector. If (a) holds for all of
10382 : the linear series, the result will be the same as (2) above.
10383 :
10384 : (b) can only hold if one of the input patterns has a
10385 : stepped encoding. */
10386 :
10387 16050 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10388 : {
10389 14986 : res_npatterns = sel.encoding ().npatterns ();
10390 14986 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10391 14986 : if (res_nelts_per_pattern == 3
10392 1397 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10393 15894 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10394 : res_nelts_per_pattern = 2;
10395 14986 : res_nelts = res_npatterns * res_nelts_per_pattern;
10396 : }
10397 1064 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10398 : {
10399 1064 : res_npatterns = res_nelts;
10400 1064 : res_nelts_per_pattern = 1;
10401 : }
10402 : else
10403 : return NULL_TREE;
10404 :
10405 16050 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10406 89391 : for (unsigned i = 0; i < res_nelts; i++)
10407 : {
10408 73341 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10409 73341 : uint64_t q;
10410 73341 : poly_uint64 r;
10411 73341 : unsigned HOST_WIDE_INT index;
10412 :
10413 : /* Punt if sel[i] /trunc_div len cannot be determined,
10414 : because the input vector to be chosen will depend on
10415 : runtime vector length.
10416 : For example if len == 4 + 4x, and sel[i] == 4,
10417 : If len at runtime equals 4, we choose arg1[0].
10418 : For any other value of len > 4 at runtime, we choose arg0[4].
10419 : which makes the element choice dependent on runtime vector length. */
10420 73341 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10421 : {
10422 : if (reason)
10423 : *reason = "cannot divide selector element by arg len";
10424 : return NULL_TREE;
10425 : }
10426 :
10427 : /* sel[i] % len will give the index of element in the chosen input
10428 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10429 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10430 73341 : if (!r.is_constant (&index))
10431 : {
10432 : if (reason)
10433 : *reason = "remainder is not constant";
10434 : return NULL_TREE;
10435 : }
10436 :
10437 73341 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10438 73341 : tree elem = vector_cst_elt (arg, index);
10439 73341 : out_elts.quick_push (elem);
10440 : }
10441 :
10442 16050 : return out_elts.build ();
10443 16050 : }
10444 :
10445 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10446 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10447 : NULL_TREE otherwise. */
10448 :
10449 : tree
10450 43350 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10451 : {
10452 43350 : unsigned int i;
10453 43350 : unsigned HOST_WIDE_INT nelts;
10454 :
10455 43350 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10456 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10457 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10458 :
10459 43350 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10460 43350 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10461 : return NULL_TREE;
10462 :
10463 32737 : if (TREE_CODE (arg0) == VECTOR_CST
10464 16324 : && TREE_CODE (arg1) == VECTOR_CST)
10465 16050 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10466 :
10467 : /* For fall back case, we want to ensure we have VLS vectors
10468 : with equal length. */
10469 16687 : if (!sel.length ().is_constant (&nelts)
10470 16687 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10471 0 : return NULL_TREE;
10472 :
10473 16687 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10474 16687 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10475 16687 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10476 13998 : return NULL_TREE;
10477 :
10478 2689 : vec<constructor_elt, va_gc> *v;
10479 2689 : vec_alloc (v, nelts);
10480 13517 : for (i = 0; i < nelts; i++)
10481 : {
10482 10828 : HOST_WIDE_INT index;
10483 10828 : if (!sel[i].is_constant (&index))
10484 : return NULL_TREE;
10485 10828 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10486 : }
10487 2689 : return build_constructor (type, v);
10488 : }
10489 :
10490 : /* Try to fold a pointer difference of type TYPE two address expressions of
10491 : array references AREF0 and AREF1 using location LOC. Return a
10492 : simplified expression for the difference or NULL_TREE. */
10493 :
10494 : static tree
10495 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10496 : tree aref0, tree aref1,
10497 : bool use_pointer_diff)
10498 : {
10499 39 : tree base0 = TREE_OPERAND (aref0, 0);
10500 39 : tree base1 = TREE_OPERAND (aref1, 0);
10501 39 : tree base_offset = build_int_cst (type, 0);
10502 :
10503 : /* If the bases are array references as well, recurse. If the bases
10504 : are pointer indirections compute the difference of the pointers.
10505 : If the bases are equal, we are set. */
10506 39 : if ((TREE_CODE (base0) == ARRAY_REF
10507 1 : && TREE_CODE (base1) == ARRAY_REF
10508 1 : && (base_offset
10509 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10510 : use_pointer_diff)))
10511 38 : || (INDIRECT_REF_P (base0)
10512 7 : && INDIRECT_REF_P (base1)
10513 7 : && (base_offset
10514 : = use_pointer_diff
10515 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10516 1 : TREE_OPERAND (base0, 0),
10517 1 : TREE_OPERAND (base1, 0))
10518 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10519 6 : fold_convert (type,
10520 : TREE_OPERAND (base0, 0)),
10521 6 : fold_convert (type,
10522 : TREE_OPERAND (base1, 0)))))
10523 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10524 : {
10525 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10526 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10527 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10528 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10529 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10530 : base_offset,
10531 : fold_build2_loc (loc, MULT_EXPR, type,
10532 15 : diff, esz));
10533 : }
10534 : return NULL_TREE;
10535 : }
10536 :
10537 : /* If the real or vector real constant CST of type TYPE has an exact
10538 : inverse, return it, else return NULL. */
10539 :
10540 : tree
10541 1117996 : exact_inverse (tree type, tree cst)
10542 : {
10543 1117996 : REAL_VALUE_TYPE r;
10544 1117996 : tree unit_type;
10545 1117996 : machine_mode mode;
10546 :
10547 1117996 : switch (TREE_CODE (cst))
10548 : {
10549 1117417 : case REAL_CST:
10550 1117417 : r = TREE_REAL_CST (cst);
10551 :
10552 1117417 : if (exact_real_inverse (TYPE_MODE (type), &r))
10553 320107 : return build_real (type, r);
10554 :
10555 : return NULL_TREE;
10556 :
10557 579 : case VECTOR_CST:
10558 579 : {
10559 579 : unit_type = TREE_TYPE (type);
10560 579 : mode = TYPE_MODE (unit_type);
10561 :
10562 579 : tree_vector_builder elts;
10563 579 : if (!elts.new_unary_operation (type, cst, false))
10564 : return NULL_TREE;
10565 579 : unsigned int count = elts.encoded_nelts ();
10566 639 : for (unsigned int i = 0; i < count; ++i)
10567 : {
10568 579 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10569 579 : if (!exact_real_inverse (mode, &r))
10570 : return NULL_TREE;
10571 60 : elts.quick_push (build_real (unit_type, r));
10572 : }
10573 :
10574 60 : return elts.build ();
10575 579 : }
10576 :
10577 : default:
10578 : return NULL_TREE;
10579 : }
10580 : }
10581 :
10582 : /* Mask out the tz least significant bits of X of type TYPE where
10583 : tz is the number of trailing zeroes in Y. */
10584 : static wide_int
10585 132458 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10586 : {
10587 132458 : int tz = wi::ctz (y);
10588 132458 : if (tz > 0)
10589 6784 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10590 125674 : return x;
10591 : }
10592 :
10593 : /* Return true when T is an address and is known to be nonzero.
10594 : For floating point we further ensure that T is not denormal.
10595 : Similar logic is present in nonzero_address in rtlanal.h. */
10596 :
10597 : bool
10598 144426076 : tree_expr_nonzero_p (tree t)
10599 : {
10600 144763554 : tree type = TREE_TYPE (t);
10601 144763554 : enum tree_code code;
10602 :
10603 : /* Doing something useful for floating point would need more work. */
10604 144763554 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10605 : return false;
10606 :
10607 144644167 : code = TREE_CODE (t);
10608 144644167 : switch (TREE_CODE_CLASS (code))
10609 : {
10610 941176 : case tcc_unary:
10611 941176 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10612 2852649 : case tcc_binary:
10613 2852649 : case tcc_comparison:
10614 2852649 : return tree_binary_nonzero_p (code, type,
10615 2852649 : TREE_OPERAND (t, 0),
10616 5705298 : TREE_OPERAND (t, 1));
10617 12686047 : case tcc_constant:
10618 12686047 : case tcc_declaration:
10619 12686047 : case tcc_reference:
10620 12686047 : return tree_single_nonzero_p (t);
10621 :
10622 128164295 : default:
10623 128164295 : break;
10624 : }
10625 :
10626 128164295 : switch (code)
10627 : {
10628 474449 : case TRUTH_NOT_EXPR:
10629 474449 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10630 :
10631 66328 : case TRUTH_AND_EXPR:
10632 66328 : case TRUTH_OR_EXPR:
10633 66328 : case TRUTH_XOR_EXPR:
10634 66328 : return tree_binary_nonzero_p (code, type,
10635 66328 : TREE_OPERAND (t, 0),
10636 132656 : TREE_OPERAND (t, 1));
10637 :
10638 124323420 : case COND_EXPR:
10639 124323420 : case CONSTRUCTOR:
10640 124323420 : case OBJ_TYPE_REF:
10641 124323420 : case ADDR_EXPR:
10642 124323420 : case WITH_SIZE_EXPR:
10643 124323420 : case SSA_NAME:
10644 124323420 : return tree_single_nonzero_p (t);
10645 :
10646 84523 : case COMPOUND_EXPR:
10647 84523 : case MODIFY_EXPR:
10648 84523 : case BIND_EXPR:
10649 84523 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10650 :
10651 252955 : case SAVE_EXPR:
10652 252955 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10653 :
10654 2910028 : case CALL_EXPR:
10655 2910028 : {
10656 2910028 : tree fndecl = get_callee_fndecl (t);
10657 2910028 : if (!fndecl) return false;
10658 2908085 : if (flag_delete_null_pointer_checks && !flag_check_new
10659 2908085 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10660 2908781 : && !TREE_NOTHROW (fndecl))
10661 : return true;
10662 2908781 : if (flag_delete_null_pointer_checks
10663 5816866 : && lookup_attribute ("returns_nonnull",
10664 2908085 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10665 : return true;
10666 2908773 : return alloca_call_p (t);
10667 : }
10668 :
10669 : default:
10670 : break;
10671 : }
10672 : return false;
10673 : }
10674 :
10675 : /* Return true if T is known not to be equal to an integer W.
10676 : If STMT is specified, the check is if T on STMT is not equal
10677 : to W. */
10678 :
10679 : bool
10680 97569513 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10681 : {
10682 97569513 : int_range_max vr;
10683 97569513 : switch (TREE_CODE (t))
10684 : {
10685 1072145 : case INTEGER_CST:
10686 1072145 : return wi::to_wide (t) != w;
10687 :
10688 96496232 : case SSA_NAME:
10689 96496232 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10690 : return false;
10691 :
10692 192992464 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10693 96496232 : if (!vr.undefined_p () && !vr.contains_p (w))
10694 : return true;
10695 : /* If T has some known zero bits and W has any of those bits set,
10696 : then T is known not to be equal to W. */
10697 96368115 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10698 192735798 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10699 : return true;
10700 : return false;
10701 :
10702 : default:
10703 : return false;
10704 : }
10705 97569513 : }
10706 :
10707 : /* Fold a binary expression of code CODE and type TYPE with operands
10708 : OP0 and OP1. LOC is the location of the resulting expression.
10709 : Return the folded expression if folding is successful. Otherwise,
10710 : return NULL_TREE. */
10711 :
10712 : tree
10713 922235255 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10714 : tree op0, tree op1)
10715 : {
10716 922235255 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10717 922235255 : tree arg0, arg1, tem;
10718 922235255 : tree t1 = NULL_TREE;
10719 922235255 : unsigned int prec;
10720 :
10721 922235255 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10722 : && TREE_CODE_LENGTH (code) == 2
10723 : && op0 != NULL_TREE
10724 : && op1 != NULL_TREE);
10725 :
10726 922235255 : arg0 = op0;
10727 922235255 : arg1 = op1;
10728 :
10729 : /* Strip any conversions that don't change the mode. This is
10730 : safe for every expression, except for a comparison expression
10731 : because its signedness is derived from its operands. So, in
10732 : the latter case, only strip conversions that don't change the
10733 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10734 : preserved.
10735 :
10736 : Note that this is done as an internal manipulation within the
10737 : constant folder, in order to find the simplest representation
10738 : of the arguments so that their form can be studied. In any
10739 : cases, the appropriate type conversions should be put back in
10740 : the tree that will get out of the constant folder. */
10741 :
10742 922235255 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10743 : {
10744 200159594 : STRIP_SIGN_NOPS (arg0);
10745 200159594 : STRIP_SIGN_NOPS (arg1);
10746 : }
10747 : else
10748 : {
10749 722075661 : STRIP_NOPS (arg0);
10750 722075661 : STRIP_NOPS (arg1);
10751 : }
10752 :
10753 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10754 : constant but we can't do arithmetic on them. */
10755 922235255 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10756 : {
10757 250484778 : tem = const_binop (code, type, arg0, arg1);
10758 250484778 : if (tem != NULL_TREE)
10759 : {
10760 248091901 : if (TREE_TYPE (tem) != type)
10761 3189733 : tem = fold_convert_loc (loc, type, tem);
10762 248091901 : return tem;
10763 : }
10764 : }
10765 :
10766 : /* If this is a commutative operation, and ARG0 is a constant, move it
10767 : to ARG1 to reduce the number of tests below. */
10768 674143354 : if (commutative_tree_code (code)
10769 674143354 : && tree_swap_operands_p (arg0, arg1))
10770 33260782 : return fold_build2_loc (loc, code, type, op1, op0);
10771 :
10772 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10773 : to ARG1 to reduce the number of tests below. */
10774 640882572 : if (kind == tcc_comparison
10775 640882572 : && tree_swap_operands_p (arg0, arg1))
10776 8231768 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10777 :
10778 632650804 : tem = generic_simplify (loc, code, type, op0, op1);
10779 632650804 : if (tem)
10780 : return tem;
10781 :
10782 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10783 :
10784 : First check for cases where an arithmetic operation is applied to a
10785 : compound, conditional, or comparison operation. Push the arithmetic
10786 : operation inside the compound or conditional to see if any folding
10787 : can then be done. Convert comparison to conditional for this purpose.
10788 : The also optimizes non-constant cases that used to be done in
10789 : expand_expr.
10790 :
10791 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10792 : one of the operands is a comparison and the other is a comparison, a
10793 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10794 : code below would make the expression more complex. Change it to a
10795 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10796 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10797 :
10798 532768788 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10799 : || code == EQ_EXPR || code == NE_EXPR)
10800 56065127 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10801 55485465 : && ((truth_value_p (TREE_CODE (arg0))
10802 1071213 : && (truth_value_p (TREE_CODE (arg1))
10803 772603 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10804 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10805 55186839 : || (truth_value_p (TREE_CODE (arg1))
10806 6635 : && (truth_value_p (TREE_CODE (arg0))
10807 6635 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10808 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10809 : {
10810 373537 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10811 74897 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10812 : : TRUTH_XOR_EXPR,
10813 : boolean_type_node,
10814 : fold_convert_loc (loc, boolean_type_node, arg0),
10815 : fold_convert_loc (loc, boolean_type_node, arg1));
10816 :
10817 298640 : if (code == EQ_EXPR)
10818 68442 : tem = invert_truthvalue_loc (loc, tem);
10819 :
10820 298640 : return fold_convert_loc (loc, type, tem);
10821 : }
10822 :
10823 532470148 : if (TREE_CODE_CLASS (code) == tcc_binary
10824 289289012 : || TREE_CODE_CLASS (code) == tcc_comparison)
10825 : {
10826 335601545 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
10827 : {
10828 81827 : tem = fold_build2_loc (loc, code, type,
10829 81827 : fold_convert_loc (loc, TREE_TYPE (op0),
10830 81827 : TREE_OPERAND (arg0, 1)), op1);
10831 81827 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10832 81827 : tem);
10833 : }
10834 335519718 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
10835 : {
10836 3150 : tem = fold_build2_loc (loc, code, type, op0,
10837 3150 : fold_convert_loc (loc, TREE_TYPE (op1),
10838 3150 : TREE_OPERAND (arg1, 1)));
10839 3150 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10840 3150 : tem);
10841 : }
10842 :
10843 335516568 : if (TREE_CODE (arg0) == COND_EXPR
10844 335136490 : || TREE_CODE (arg0) == VEC_COND_EXPR
10845 335133788 : || COMPARISON_CLASS_P (arg0))
10846 : {
10847 696350 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10848 : arg0, arg1,
10849 : /*cond_first_p=*/1);
10850 696350 : if (tem != NULL_TREE)
10851 : return tem;
10852 : }
10853 :
10854 335047599 : if (TREE_CODE (arg1) == COND_EXPR
10855 334811662 : || TREE_CODE (arg1) == VEC_COND_EXPR
10856 334811311 : || COMPARISON_CLASS_P (arg1))
10857 : {
10858 247899 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10859 : arg1, arg0,
10860 : /*cond_first_p=*/0);
10861 247899 : if (tem != NULL_TREE)
10862 : return tem;
10863 : }
10864 : }
10865 :
10866 531908145 : switch (code)
10867 : {
10868 60955587 : case MEM_REF:
10869 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10870 60955587 : if (TREE_CODE (arg0) == ADDR_EXPR
10871 60955587 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10872 : {
10873 815566 : tree iref = TREE_OPERAND (arg0, 0);
10874 815566 : return fold_build2 (MEM_REF, type,
10875 : TREE_OPERAND (iref, 0),
10876 : int_const_binop (PLUS_EXPR, arg1,
10877 : TREE_OPERAND (iref, 1)));
10878 : }
10879 :
10880 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10881 60140021 : if (TREE_CODE (arg0) == ADDR_EXPR
10882 60140021 : && handled_component_p (TREE_OPERAND (arg0, 0)))
10883 : {
10884 5905406 : tree base;
10885 5905406 : poly_int64 coffset;
10886 5905406 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10887 : &coffset);
10888 5905406 : if (!base)
10889 : return NULL_TREE;
10890 5901417 : return fold_build2 (MEM_REF, type,
10891 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
10892 : int_const_binop (PLUS_EXPR, arg1,
10893 : size_int (coffset)));
10894 : }
10895 :
10896 : return NULL_TREE;
10897 :
10898 68784410 : case POINTER_PLUS_EXPR:
10899 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10900 137568404 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10901 137559970 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10902 36872 : return fold_convert_loc (loc, type,
10903 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
10904 : fold_convert_loc (loc, sizetype,
10905 : arg1),
10906 : fold_convert_loc (loc, sizetype,
10907 36872 : arg0)));
10908 :
10909 : return NULL_TREE;
10910 :
10911 62418324 : case PLUS_EXPR:
10912 62418324 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10913 : {
10914 : /* X + (X / CST) * -CST is X % CST. */
10915 51301698 : if (TREE_CODE (arg1) == MULT_EXPR
10916 2507525 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10917 51308436 : && operand_equal_p (arg0,
10918 6738 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10919 : {
10920 214 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10921 214 : tree cst1 = TREE_OPERAND (arg1, 1);
10922 214 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10923 : cst1, cst0);
10924 214 : if (sum && integer_zerop (sum))
10925 214 : return fold_convert_loc (loc, type,
10926 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
10927 214 : TREE_TYPE (arg0), arg0,
10928 214 : cst0));
10929 : }
10930 : }
10931 :
10932 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10933 : one. Make sure the type is not saturating and has the signedness of
10934 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
10935 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10936 62418110 : if ((TREE_CODE (arg0) == MULT_EXPR
10937 50626336 : || TREE_CODE (arg1) == MULT_EXPR)
10938 13220871 : && !TYPE_SATURATING (type)
10939 13220871 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10940 12845232 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10941 74453137 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
10942 : {
10943 8875417 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10944 8875417 : if (tem)
10945 : return tem;
10946 : }
10947 :
10948 61025049 : if (! FLOAT_TYPE_P (type))
10949 : {
10950 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10951 : (plus (plus (mult) (mult)) (foo)) so that we can
10952 : take advantage of the factoring cases below. */
10953 282113 : if (ANY_INTEGRAL_TYPE_P (type)
10954 49910771 : && TYPE_OVERFLOW_WRAPS (type)
10955 49910771 : && (((TREE_CODE (arg0) == PLUS_EXPR
10956 30951344 : || TREE_CODE (arg0) == MINUS_EXPR)
10957 3451223 : && TREE_CODE (arg1) == MULT_EXPR)
10958 30345100 : || ((TREE_CODE (arg1) == PLUS_EXPR
10959 30345100 : || TREE_CODE (arg1) == MINUS_EXPR)
10960 428312 : && TREE_CODE (arg0) == MULT_EXPR)))
10961 : {
10962 652763 : tree parg0, parg1, parg, marg;
10963 652763 : enum tree_code pcode;
10964 :
10965 652763 : if (TREE_CODE (arg1) == MULT_EXPR)
10966 : parg = arg0, marg = arg1;
10967 : else
10968 46519 : parg = arg1, marg = arg0;
10969 652763 : pcode = TREE_CODE (parg);
10970 652763 : parg0 = TREE_OPERAND (parg, 0);
10971 652763 : parg1 = TREE_OPERAND (parg, 1);
10972 652763 : STRIP_NOPS (parg0);
10973 652763 : STRIP_NOPS (parg1);
10974 :
10975 652763 : if (TREE_CODE (parg0) == MULT_EXPR
10976 360871 : && TREE_CODE (parg1) != MULT_EXPR)
10977 327347 : return fold_build2_loc (loc, pcode, type,
10978 : fold_build2_loc (loc, PLUS_EXPR, type,
10979 : fold_convert_loc (loc, type,
10980 : parg0),
10981 : fold_convert_loc (loc, type,
10982 : marg)),
10983 327347 : fold_convert_loc (loc, type, parg1));
10984 325416 : if (TREE_CODE (parg0) != MULT_EXPR
10985 291892 : && TREE_CODE (parg1) == MULT_EXPR)
10986 103351 : return
10987 103351 : fold_build2_loc (loc, PLUS_EXPR, type,
10988 : fold_convert_loc (loc, type, parg0),
10989 : fold_build2_loc (loc, pcode, type,
10990 : fold_convert_loc (loc, type, marg),
10991 : fold_convert_loc (loc, type,
10992 103351 : parg1)));
10993 : }
10994 : }
10995 : else
10996 : {
10997 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10998 : to __complex__ ( x, y ). This is not the same for SNaNs or
10999 : if signed zeros are involved. */
11000 11114278 : if (!HONOR_SNANS (arg0)
11001 11112618 : && !HONOR_SIGNED_ZEROS (arg0)
11002 11135404 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11003 : {
11004 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11005 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11006 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11007 3086 : bool arg0rz = false, arg0iz = false;
11008 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11009 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11010 : {
11011 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11012 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11013 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11014 : {
11015 22 : tree rp = arg1r ? arg1r
11016 0 : : build1 (REALPART_EXPR, rtype, arg1);
11017 22 : tree ip = arg0i ? arg0i
11018 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11019 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11020 : }
11021 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11022 : {
11023 53 : tree rp = arg0r ? arg0r
11024 0 : : build1 (REALPART_EXPR, rtype, arg0);
11025 53 : tree ip = arg1i ? arg1i
11026 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11027 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11028 : }
11029 : }
11030 : }
11031 :
11032 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11033 : We associate floats only if the user has specified
11034 : -fassociative-math. */
11035 11114203 : if (flag_associative_math
11036 21030 : && TREE_CODE (arg1) == PLUS_EXPR
11037 36 : && TREE_CODE (arg0) != MULT_EXPR)
11038 : {
11039 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11040 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11041 21 : if (TREE_CODE (tree11) == MULT_EXPR
11042 5 : && TREE_CODE (tree10) == MULT_EXPR)
11043 : {
11044 1 : tree tree0;
11045 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11046 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11047 : }
11048 : }
11049 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11050 : We associate floats only if the user has specified
11051 : -fassociative-math. */
11052 11114202 : if (flag_associative_math
11053 21029 : && TREE_CODE (arg0) == PLUS_EXPR
11054 1221 : && TREE_CODE (arg1) != MULT_EXPR)
11055 : {
11056 831 : tree tree00 = TREE_OPERAND (arg0, 0);
11057 831 : tree tree01 = TREE_OPERAND (arg0, 1);
11058 831 : if (TREE_CODE (tree01) == MULT_EXPR
11059 49 : && TREE_CODE (tree00) == MULT_EXPR)
11060 : {
11061 9 : tree tree0;
11062 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11063 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11064 : }
11065 : }
11066 : }
11067 :
11068 11113371 : bit_rotate:
11069 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11070 : is a rotate of A by C1 bits. */
11071 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11072 : is a rotate of A by B bits.
11073 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11074 : though in this case CODE must be | and not + or ^, otherwise
11075 : it doesn't return A when B is 0. */
11076 63343254 : {
11077 63343254 : enum tree_code code0, code1;
11078 63343254 : tree rtype;
11079 63343254 : code0 = TREE_CODE (arg0);
11080 63343254 : code1 = TREE_CODE (arg1);
11081 70741 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11082 63326934 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11083 39641 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11084 39641 : TREE_OPERAND (arg1, 0), 0)
11085 36871 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11086 36871 : TYPE_UNSIGNED (rtype))
11087 : /* Only create rotates in complete modes. Other cases are not
11088 : expanded properly. */
11089 63370027 : && (element_precision (rtype)
11090 53546 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11091 : {
11092 26703 : tree tree01, tree11;
11093 26703 : tree orig_tree01, orig_tree11;
11094 26703 : enum tree_code code01, code11;
11095 :
11096 26703 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11097 26703 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11098 26703 : STRIP_NOPS (tree01);
11099 26703 : STRIP_NOPS (tree11);
11100 26703 : code01 = TREE_CODE (tree01);
11101 26703 : code11 = TREE_CODE (tree11);
11102 26703 : if (code11 != MINUS_EXPR
11103 26017 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11104 : {
11105 1446 : std::swap (code0, code1);
11106 1446 : std::swap (code01, code11);
11107 1446 : std::swap (tree01, tree11);
11108 1446 : std::swap (orig_tree01, orig_tree11);
11109 : }
11110 53406 : if (code01 == INTEGER_CST
11111 3152 : && code11 == INTEGER_CST
11112 33005 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11113 33005 : == element_precision (rtype)))
11114 : {
11115 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11116 3011 : rtype, TREE_OPERAND (arg0, 0),
11117 : code0 == LSHIFT_EXPR
11118 : ? orig_tree01 : orig_tree11);
11119 3011 : return fold_convert_loc (loc, type, tem);
11120 : }
11121 23692 : else if (code11 == MINUS_EXPR)
11122 : {
11123 941 : tree tree110, tree111;
11124 941 : tree110 = TREE_OPERAND (tree11, 0);
11125 941 : tree111 = TREE_OPERAND (tree11, 1);
11126 941 : STRIP_NOPS (tree110);
11127 941 : STRIP_NOPS (tree111);
11128 941 : if (TREE_CODE (tree110) == INTEGER_CST
11129 930 : && compare_tree_int (tree110,
11130 930 : element_precision (rtype)) == 0
11131 1855 : && operand_equal_p (tree01, tree111, 0))
11132 : {
11133 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11134 : ? LROTATE_EXPR : RROTATE_EXPR),
11135 558 : rtype, TREE_OPERAND (arg0, 0),
11136 : orig_tree01);
11137 558 : return fold_convert_loc (loc, type, tem);
11138 : }
11139 : }
11140 22751 : else if (code == BIT_IOR_EXPR
11141 21637 : && code11 == BIT_AND_EXPR
11142 44313 : && pow2p_hwi (element_precision (rtype)))
11143 : {
11144 21562 : tree tree110, tree111;
11145 21562 : tree110 = TREE_OPERAND (tree11, 0);
11146 21562 : tree111 = TREE_OPERAND (tree11, 1);
11147 21562 : STRIP_NOPS (tree110);
11148 21562 : STRIP_NOPS (tree111);
11149 21562 : if (TREE_CODE (tree110) == NEGATE_EXPR
11150 21139 : && TREE_CODE (tree111) == INTEGER_CST
11151 21139 : && compare_tree_int (tree111,
11152 21139 : element_precision (rtype) - 1) == 0
11153 42687 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11154 : {
11155 31583 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11156 : ? LROTATE_EXPR : RROTATE_EXPR),
11157 21079 : rtype, TREE_OPERAND (arg0, 0),
11158 : orig_tree01);
11159 21079 : return fold_convert_loc (loc, type, tem);
11160 : }
11161 : }
11162 : }
11163 : }
11164 :
11165 155272107 : associate:
11166 : /* In most languages, can't associate operations on floats through
11167 : parentheses. Rather than remember where the parentheses were, we
11168 : don't associate floats at all, unless the user has specified
11169 : -fassociative-math.
11170 : And, we need to make sure type is not saturating. */
11171 :
11172 155272107 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11173 115885924 : && !TYPE_SATURATING (type)
11174 271158031 : && !TYPE_OVERFLOW_SANITIZED (type))
11175 : {
11176 115857555 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11177 115857555 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11178 115857555 : tree atype = type;
11179 115857555 : bool ok = true;
11180 :
11181 : /* Split both trees into variables, constants, and literals. Then
11182 : associate each group together, the constants with literals,
11183 : then the result with variables. This increases the chances of
11184 : literals being recombined later and of generating relocatable
11185 : expressions for the sum of a constant and literal. */
11186 115857555 : var0 = split_tree (arg0, type, code,
11187 : &minus_var0, &con0, &minus_con0,
11188 : &lit0, &minus_lit0, 0);
11189 115857555 : var1 = split_tree (arg1, type, code,
11190 : &minus_var1, &con1, &minus_con1,
11191 : &lit1, &minus_lit1, code == MINUS_EXPR);
11192 :
11193 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11194 115857555 : if (code == MINUS_EXPR)
11195 12688087 : code = PLUS_EXPR;
11196 :
11197 : /* With undefined overflow prefer doing association in a type
11198 : which wraps on overflow, if that is one of the operand types. */
11199 115857324 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11200 230469900 : && !TYPE_OVERFLOW_WRAPS (type))
11201 : {
11202 62608747 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11203 61931691 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11204 782493 : atype = TREE_TYPE (arg0);
11205 61008107 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11206 60759451 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11207 251070 : atype = TREE_TYPE (arg1);
11208 31548921 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11209 : }
11210 :
11211 : /* With undefined overflow we can only associate constants with one
11212 : variable, and constants whose association doesn't overflow. */
11213 115857324 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11214 230469900 : && !TYPE_OVERFLOW_WRAPS (atype))
11215 : {
11216 30515358 : if ((var0 && var1) || (minus_var0 && minus_var1))
11217 : {
11218 : /* ??? If split_tree would handle NEGATE_EXPR we could
11219 : simply reject these cases and the allowed cases would
11220 : be the var0/minus_var1 ones. */
11221 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11222 5641735 : tree tmp1 = var1 ? var1 : minus_var1;
11223 5641735 : bool one_neg = false;
11224 :
11225 5641735 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11226 : {
11227 730 : tmp0 = TREE_OPERAND (tmp0, 0);
11228 730 : one_neg = !one_neg;
11229 : }
11230 4991598 : if (CONVERT_EXPR_P (tmp0)
11231 674646 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11232 6315303 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11233 673568 : <= TYPE_PRECISION (atype)))
11234 658213 : tmp0 = TREE_OPERAND (tmp0, 0);
11235 5641735 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11236 : {
11237 168 : tmp1 = TREE_OPERAND (tmp1, 0);
11238 168 : one_neg = !one_neg;
11239 : }
11240 5317845 : if (CONVERT_EXPR_P (tmp1)
11241 389578 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11242 6031185 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11243 389450 : <= TYPE_PRECISION (atype)))
11244 371928 : tmp1 = TREE_OPERAND (tmp1, 0);
11245 : /* The only case we can still associate with two variables
11246 : is if they cancel out. */
11247 5641735 : if (!one_neg
11248 5641735 : || !operand_equal_p (tmp0, tmp1, 0))
11249 : ok = false;
11250 : }
11251 24487030 : else if ((var0 && minus_var1
11252 4140739 : && ! operand_equal_p (var0, minus_var1, 0))
11253 45219915 : || (minus_var0 && var1
11254 11395 : && ! operand_equal_p (minus_var0, var1, 0)))
11255 : ok = false;
11256 : }
11257 :
11258 : /* Only do something if we found more than two objects. Otherwise,
11259 : nothing has changed and we risk infinite recursion. */
11260 : if (ok
11261 106063759 : && ((var0 != 0) + (var1 != 0)
11262 106063759 : + (minus_var0 != 0) + (minus_var1 != 0)
11263 106063759 : + (con0 != 0) + (con1 != 0)
11264 106063759 : + (minus_con0 != 0) + (minus_con1 != 0)
11265 106063759 : + (lit0 != 0) + (lit1 != 0)
11266 106063759 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11267 : {
11268 2021163 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11269 4042326 : int minus_var0_origin
11270 2021163 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11271 2021163 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11272 4042326 : int minus_con0_origin
11273 2021163 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11274 2021163 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11275 4042326 : int minus_lit0_origin
11276 2021163 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11277 2021163 : var0 = associate_trees (loc, var0, var1, code, atype);
11278 2021163 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11279 : code, atype);
11280 2021163 : con0 = associate_trees (loc, con0, con1, code, atype);
11281 2021163 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11282 : code, atype);
11283 2021163 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11284 2021163 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11285 : code, atype);
11286 :
11287 2021163 : if (minus_var0 && var0)
11288 : {
11289 1327706 : var0_origin |= minus_var0_origin;
11290 1327706 : var0 = associate_trees (loc, var0, minus_var0,
11291 : MINUS_EXPR, atype);
11292 1327706 : minus_var0 = 0;
11293 1327706 : minus_var0_origin = 0;
11294 : }
11295 2021163 : if (minus_con0 && con0)
11296 : {
11297 3655 : con0_origin |= minus_con0_origin;
11298 3655 : con0 = associate_trees (loc, con0, minus_con0,
11299 : MINUS_EXPR, atype);
11300 3655 : minus_con0 = 0;
11301 3655 : minus_con0_origin = 0;
11302 : }
11303 :
11304 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11305 : greater than the positive part. Otherwise, the multiplicative
11306 : folding code (i.e extract_muldiv) may be fooled in case
11307 : unsigned constants are subtracted, like in the following
11308 : example: ((X*2 + 4) - 8U)/2. */
11309 2021163 : if (minus_lit0 && lit0)
11310 : {
11311 235929 : if (TREE_CODE (lit0) == INTEGER_CST
11312 235929 : && TREE_CODE (minus_lit0) == INTEGER_CST
11313 235929 : && tree_int_cst_lt (lit0, minus_lit0)
11314 : /* But avoid ending up with only negated parts. */
11315 296389 : && (var0 || con0))
11316 : {
11317 56351 : minus_lit0_origin |= lit0_origin;
11318 56351 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11319 : MINUS_EXPR, atype);
11320 56351 : lit0 = 0;
11321 56351 : lit0_origin = 0;
11322 : }
11323 : else
11324 : {
11325 179578 : lit0_origin |= minus_lit0_origin;
11326 179578 : lit0 = associate_trees (loc, lit0, minus_lit0,
11327 : MINUS_EXPR, atype);
11328 179578 : minus_lit0 = 0;
11329 179578 : minus_lit0_origin = 0;
11330 : }
11331 : }
11332 :
11333 : /* Don't introduce overflows through reassociation. */
11334 1342755 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11335 3363880 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11336 2021163 : return NULL_TREE;
11337 :
11338 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11339 2021125 : con0_origin |= lit0_origin;
11340 2021125 : con0 = associate_trees (loc, con0, lit0, code, atype);
11341 2021125 : minus_con0_origin |= minus_lit0_origin;
11342 2021125 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11343 : code, atype);
11344 :
11345 : /* Eliminate minus_con0. */
11346 2021125 : if (minus_con0)
11347 : {
11348 686097 : if (con0)
11349 : {
11350 15600 : con0_origin |= minus_con0_origin;
11351 15600 : con0 = associate_trees (loc, con0, minus_con0,
11352 : MINUS_EXPR, atype);
11353 : }
11354 670497 : else if (var0)
11355 : {
11356 670497 : var0_origin |= minus_con0_origin;
11357 670497 : var0 = associate_trees (loc, var0, minus_con0,
11358 : MINUS_EXPR, atype);
11359 : }
11360 : else
11361 0 : gcc_unreachable ();
11362 : }
11363 :
11364 : /* Eliminate minus_var0. */
11365 2021125 : if (minus_var0)
11366 : {
11367 346521 : if (con0)
11368 : {
11369 346521 : con0_origin |= minus_var0_origin;
11370 346521 : con0 = associate_trees (loc, con0, minus_var0,
11371 : MINUS_EXPR, atype);
11372 : }
11373 : else
11374 0 : gcc_unreachable ();
11375 : }
11376 :
11377 : /* Reassociate only if there has been any actual association
11378 : between subtrees from op0 and subtrees from op1 in at
11379 : least one of the operands, otherwise we risk infinite
11380 : recursion. See PR114084. */
11381 2021125 : if (var0_origin != 3 && con0_origin != 3)
11382 : return NULL_TREE;
11383 :
11384 2019525 : return
11385 2019525 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11386 2019525 : code, atype));
11387 : }
11388 : }
11389 :
11390 : return NULL_TREE;
11391 :
11392 23059526 : case POINTER_DIFF_EXPR:
11393 23059526 : case MINUS_EXPR:
11394 : /* Fold &a[i] - &a[j] to i-j. */
11395 23059526 : if (TREE_CODE (arg0) == ADDR_EXPR
11396 45433 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11397 6199 : && TREE_CODE (arg1) == ADDR_EXPR
11398 23060134 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11399 : {
11400 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11401 38 : TREE_OPERAND (arg0, 0),
11402 38 : TREE_OPERAND (arg1, 0),
11403 : code
11404 : == POINTER_DIFF_EXPR);
11405 38 : if (tem)
11406 : return tem;
11407 : }
11408 :
11409 : /* Further transformations are not for pointers. */
11410 23059512 : if (code == POINTER_DIFF_EXPR)
11411 : return NULL_TREE;
11412 :
11413 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11414 20454778 : if (TREE_CODE (arg0) == NEGATE_EXPR
11415 143769 : && negate_expr_p (op1)
11416 : /* If arg0 is e.g. unsigned int and type is int, then this could
11417 : introduce UB, because if A is INT_MIN at runtime, the original
11418 : expression can be well defined while the latter is not.
11419 : See PR83269. */
11420 20455617 : && !(ANY_INTEGRAL_TYPE_P (type)
11421 839 : && TYPE_OVERFLOW_UNDEFINED (type)
11422 815 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11423 815 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11424 832 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11425 : fold_convert_loc (loc, type,
11426 1664 : TREE_OPERAND (arg0, 0)));
11427 :
11428 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11429 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11430 : signed zeros are involved. */
11431 20453946 : if (!HONOR_SNANS (arg0)
11432 20452795 : && !HONOR_SIGNED_ZEROS (arg0)
11433 33953174 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11434 : {
11435 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11436 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11437 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11438 53 : bool arg0rz = false, arg0iz = false;
11439 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11440 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11441 : {
11442 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11443 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11444 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11445 : {
11446 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11447 : arg1r ? arg1r
11448 0 : : build1 (REALPART_EXPR, rtype, arg1));
11449 9 : tree ip = arg0i ? arg0i
11450 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11451 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11452 : }
11453 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11454 : {
11455 15 : tree rp = arg0r ? arg0r
11456 0 : : build1 (REALPART_EXPR, rtype, arg0);
11457 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11458 : arg1i ? arg1i
11459 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11460 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11461 : }
11462 : }
11463 : }
11464 :
11465 : /* A - B -> A + (-B) if B is easily negatable. */
11466 20453922 : if (negate_expr_p (op1)
11467 750643 : && ! TYPE_OVERFLOW_SANITIZED (type)
11468 21202019 : && ((FLOAT_TYPE_P (type)
11469 : /* Avoid this transformation if B is a positive REAL_CST. */
11470 65 : && (TREE_CODE (op1) != REAL_CST
11471 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11472 748032 : || INTEGRAL_TYPE_P (type)))
11473 747893 : return fold_build2_loc (loc, PLUS_EXPR, type,
11474 : fold_convert_loc (loc, type, arg0),
11475 747893 : negate_expr (op1));
11476 :
11477 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11478 : one. Make sure the type is not saturating and has the signedness of
11479 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11480 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11481 19706029 : if ((TREE_CODE (arg0) == MULT_EXPR
11482 18451667 : || TREE_CODE (arg1) == MULT_EXPR)
11483 2602900 : && !TYPE_SATURATING (type)
11484 2602900 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11485 2471728 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11486 22117484 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11487 : {
11488 403559 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11489 403559 : if (tem)
11490 : return tem;
11491 : }
11492 :
11493 19650508 : goto associate;
11494 :
11495 66343463 : case MULT_EXPR:
11496 66343463 : if (! FLOAT_TYPE_P (type))
11497 : {
11498 : /* Transform x * -C into -x * C if x is easily negatable. */
11499 44975337 : if (TREE_CODE (op1) == INTEGER_CST
11500 41705343 : && tree_int_cst_sgn (op1) == -1
11501 261655 : && negate_expr_p (op0)
11502 365 : && negate_expr_p (op1)
11503 349 : && (tem = negate_expr (op1)) != op1
11504 44975686 : && ! TREE_OVERFLOW (tem))
11505 349 : return fold_build2_loc (loc, MULT_EXPR, type,
11506 : fold_convert_loc (loc, type,
11507 349 : negate_expr (op0)), tem);
11508 :
11509 44974988 : if (TREE_CODE (arg1) == INTEGER_CST
11510 44974988 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11511 : {
11512 682644 : return fold_convert_loc (loc, type, tem);
11513 : }
11514 :
11515 : /* Optimize z * conj(z) for integer complex numbers. */
11516 44292344 : if (TREE_CODE (arg0) == CONJ_EXPR
11517 44292344 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11518 1 : return fold_mult_zconjz (loc, type, arg1);
11519 44292343 : if (TREE_CODE (arg1) == CONJ_EXPR
11520 44292343 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11521 0 : return fold_mult_zconjz (loc, type, arg0);
11522 : }
11523 : else
11524 : {
11525 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11526 : This is not the same for NaNs or if signed zeros are
11527 : involved. */
11528 21368126 : if (!HONOR_NANS (arg0)
11529 32813 : && !HONOR_SIGNED_ZEROS (arg0)
11530 32512 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11531 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11532 21368351 : && real_zerop (TREE_REALPART (arg1)))
11533 : {
11534 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11535 218 : if (real_onep (TREE_IMAGPART (arg1)))
11536 : {
11537 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11538 63 : arg0 = save_expr (arg0);
11539 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11540 : rtype, arg0);
11541 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11542 : rtype, arg0);
11543 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11544 : negate_expr (iarg0),
11545 208 : rarg0);
11546 : }
11547 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11548 : {
11549 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11550 0 : arg0 = save_expr (arg0);
11551 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11552 : rtype, arg0);
11553 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11554 : rtype, arg0);
11555 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11556 : iarg0,
11557 10 : negate_expr (rarg0));
11558 : }
11559 : }
11560 :
11561 : /* Optimize z * conj(z) for floating point complex numbers.
11562 : Guarded by flag_unsafe_math_optimizations as non-finite
11563 : imaginary components don't produce scalar results. */
11564 21367908 : if (flag_unsafe_math_optimizations
11565 32341 : && TREE_CODE (arg0) == CONJ_EXPR
11566 21367910 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11567 1 : return fold_mult_zconjz (loc, type, arg1);
11568 21367907 : if (flag_unsafe_math_optimizations
11569 32340 : && TREE_CODE (arg1) == CONJ_EXPR
11570 21367911 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11571 0 : return fold_mult_zconjz (loc, type, arg0);
11572 : }
11573 65660250 : goto associate;
11574 :
11575 1842479 : case BIT_IOR_EXPR:
11576 : /* Canonicalize (X & C1) | C2. */
11577 1842479 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11578 79713 : && TREE_CODE (arg1) == INTEGER_CST
11579 1877369 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11580 : {
11581 34882 : int width = TYPE_PRECISION (type), w;
11582 34882 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11583 34882 : wide_int c2 = wi::to_wide (arg1);
11584 :
11585 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11586 34882 : if ((c1 & c2) == c1)
11587 0 : return omit_one_operand_loc (loc, type, arg1,
11588 0 : TREE_OPERAND (arg0, 0));
11589 :
11590 34882 : wide_int msk = wi::mask (width, false,
11591 34882 : TYPE_PRECISION (TREE_TYPE (arg1)));
11592 :
11593 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11594 34882 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11595 : {
11596 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11597 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11598 : }
11599 :
11600 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11601 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11602 : mode which allows further optimizations. */
11603 34876 : c1 &= msk;
11604 34876 : c2 &= msk;
11605 34876 : wide_int c3 = wi::bit_and_not (c1, c2);
11606 109866 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11607 : {
11608 75232 : wide_int mask = wi::mask (w, false,
11609 75232 : TYPE_PRECISION (type));
11610 150464 : if (((c1 | c2) & mask) == mask
11611 150464 : && wi::bit_and_not (c1, mask) == 0)
11612 : {
11613 242 : c3 = mask;
11614 242 : break;
11615 : }
11616 75232 : }
11617 :
11618 34876 : if (c3 != c1)
11619 : {
11620 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11621 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11622 558 : wide_int_to_tree (type, c3));
11623 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11624 : }
11625 36004 : }
11626 :
11627 : /* See if this can be simplified into a rotate first. If that
11628 : is unsuccessful continue in the association code. */
11629 1841915 : goto bit_rotate;
11630 :
11631 907073 : case BIT_XOR_EXPR:
11632 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11633 907073 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11634 2956 : && INTEGRAL_TYPE_P (type)
11635 2351 : && integer_onep (TREE_OPERAND (arg0, 1))
11636 907596 : && integer_onep (arg1))
11637 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11638 0 : build_zero_cst (TREE_TYPE (arg0)));
11639 :
11640 : /* See if this can be simplified into a rotate first. If that
11641 : is unsuccessful continue in the association code. */
11642 907073 : goto bit_rotate;
11643 :
11644 6186656 : case BIT_AND_EXPR:
11645 : /* Fold !X & 1 as X == 0. */
11646 6186656 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11647 6186656 : && integer_onep (arg1))
11648 : {
11649 0 : tem = TREE_OPERAND (arg0, 0);
11650 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11651 0 : build_zero_cst (TREE_TYPE (tem)));
11652 : }
11653 :
11654 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11655 : multiple of 1 << CST. */
11656 6186656 : if (TREE_CODE (arg1) == INTEGER_CST)
11657 : {
11658 4409675 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11659 4409675 : wide_int ncst1 = -cst1;
11660 4409675 : if ((cst1 & ncst1) == ncst1
11661 4576603 : && multiple_of_p (type, arg0,
11662 4576603 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11663 475 : return fold_convert_loc (loc, type, arg0);
11664 4409675 : }
11665 :
11666 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11667 : bits from CST2. */
11668 6186181 : if (TREE_CODE (arg1) == INTEGER_CST
11669 4409200 : && TREE_CODE (arg0) == MULT_EXPR
11670 6318685 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11671 : {
11672 132458 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11673 132458 : wide_int masked
11674 132458 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11675 :
11676 132458 : if (masked == 0)
11677 5428 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11678 5428 : arg0, arg1);
11679 127030 : else if (masked != warg1)
11680 : {
11681 : /* Avoid the transform if arg1 is a mask of some
11682 : mode which allows further optimizations. */
11683 648 : int pop = wi::popcount (warg1);
11684 670 : if (!(pop >= BITS_PER_UNIT
11685 50 : && pow2p_hwi (pop)
11686 692 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11687 1252 : return fold_build2_loc (loc, code, type, op0,
11688 1252 : wide_int_to_tree (type, masked));
11689 : }
11690 132458 : }
11691 :
11692 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11693 4403146 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11694 6352072 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11695 : {
11696 97712 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11697 :
11698 97712 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11699 97712 : if (mask == -1)
11700 2429 : return
11701 2429 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11702 97712 : }
11703 :
11704 6177698 : goto associate;
11705 :
11706 5848760 : case RDIV_EXPR:
11707 : /* Don't touch a floating-point divide by zero unless the mode
11708 : of the constant can represent infinity. */
11709 5848760 : if (TREE_CODE (arg1) == REAL_CST
11710 2939201 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11711 5848760 : && real_zerop (arg1))
11712 0 : return NULL_TREE;
11713 :
11714 : /* (-A) / (-B) -> A / B */
11715 5848760 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11716 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11717 3 : TREE_OPERAND (arg0, 0),
11718 3 : negate_expr (arg1));
11719 5848757 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11720 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11721 : negate_expr (arg0),
11722 0 : TREE_OPERAND (arg1, 0));
11723 : return NULL_TREE;
11724 :
11725 2200277 : case TRUNC_DIV_EXPR:
11726 : /* Fall through */
11727 :
11728 2200277 : case FLOOR_DIV_EXPR:
11729 : /* Simplify A / (B << N) where A and B are positive and B is
11730 : a power of 2, to A >> (N + log2(B)). */
11731 2200277 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11732 2200277 : && (TYPE_UNSIGNED (type)
11733 8 : || tree_expr_nonnegative_p (op0)))
11734 : {
11735 17 : tree sval = TREE_OPERAND (arg1, 0);
11736 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11737 : {
11738 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11739 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11740 16 : wi::exact_log2 (wi::to_wide (sval)));
11741 :
11742 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11743 : sh_cnt, pow2);
11744 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11745 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11746 : }
11747 : }
11748 :
11749 : /* Fall through */
11750 :
11751 3538786 : case ROUND_DIV_EXPR:
11752 3538786 : case CEIL_DIV_EXPR:
11753 3538786 : case EXACT_DIV_EXPR:
11754 3538786 : if (integer_zerop (arg1))
11755 : return NULL_TREE;
11756 :
11757 : /* Convert -A / -B to A / B when the type is signed and overflow is
11758 : undefined. */
11759 3535760 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11760 962177 : && TREE_CODE (op0) == NEGATE_EXPR
11761 3535822 : && negate_expr_p (op1))
11762 60 : return fold_build2_loc (loc, code, type,
11763 : fold_convert_loc (loc, type,
11764 30 : TREE_OPERAND (arg0, 0)),
11765 30 : negate_expr (op1));
11766 3535730 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11767 962147 : && TREE_CODE (arg1) == NEGATE_EXPR
11768 3535974 : && negate_expr_p (op0))
11769 36 : return fold_build2_loc (loc, code, type,
11770 : negate_expr (op0),
11771 : fold_convert_loc (loc, type,
11772 72 : TREE_OPERAND (arg1, 0)));
11773 :
11774 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11775 : operation, EXACT_DIV_EXPR.
11776 :
11777 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11778 : At one time others generated faster code, it's not clear if they do
11779 : after the last round to changes to the DIV code in expmed.cc. */
11780 3535694 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11781 3535694 : && multiple_of_p (type, arg0, arg1))
11782 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11783 : fold_convert (type, arg0),
11784 0 : fold_convert (type, arg1));
11785 :
11786 3535694 : if (TREE_CODE (arg1) == INTEGER_CST
11787 3535694 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11788 9169 : return fold_convert_loc (loc, type, tem);
11789 :
11790 : return NULL_TREE;
11791 :
11792 900413 : case CEIL_MOD_EXPR:
11793 900413 : case FLOOR_MOD_EXPR:
11794 900413 : case ROUND_MOD_EXPR:
11795 900413 : case TRUNC_MOD_EXPR:
11796 900413 : if (TREE_CODE (arg1) == INTEGER_CST
11797 900413 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11798 0 : return fold_convert_loc (loc, type, tem);
11799 :
11800 : return NULL_TREE;
11801 :
11802 2174804 : case LROTATE_EXPR:
11803 2174804 : case RROTATE_EXPR:
11804 2174804 : case RSHIFT_EXPR:
11805 2174804 : case LSHIFT_EXPR:
11806 : /* Since negative shift count is not well-defined,
11807 : don't try to compute it in the compiler. */
11808 2174804 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11809 : return NULL_TREE;
11810 :
11811 2173763 : prec = element_precision (type);
11812 :
11813 : /* If we have a rotate of a bit operation with the rotate count and
11814 : the second operand of the bit operation both constant,
11815 : permute the two operations. */
11816 2689 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11817 2159 : && (TREE_CODE (arg0) == BIT_AND_EXPR
11818 2159 : || TREE_CODE (arg0) == BIT_IOR_EXPR
11819 2159 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
11820 2173763 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11821 : {
11822 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11823 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11824 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
11825 : fold_build2_loc (loc, code, type,
11826 : arg00, arg1),
11827 : fold_build2_loc (loc, code, type,
11828 0 : arg01, arg1));
11829 : }
11830 :
11831 : return NULL_TREE;
11832 :
11833 465045 : case MIN_EXPR:
11834 465045 : case MAX_EXPR:
11835 465045 : goto associate;
11836 :
11837 6313973 : case TRUTH_ANDIF_EXPR:
11838 : /* Note that the operands of this must be ints
11839 : and their values must be 0 or 1.
11840 : ("true" is a fixed value perhaps depending on the language.) */
11841 : /* If first arg is constant zero, return it. */
11842 6313973 : if (integer_zerop (arg0))
11843 1588927 : return fold_convert_loc (loc, type, arg0);
11844 : /* FALLTHRU */
11845 15315887 : case TRUTH_AND_EXPR:
11846 : /* If either arg is constant true, drop it. */
11847 15315887 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11848 1423423 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11849 795303 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11850 : /* Preserve sequence points. */
11851 14643140 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11852 727143 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11853 : /* If second arg is constant zero, result is zero, but first arg
11854 : must be evaluated. */
11855 13165321 : if (integer_zerop (arg1))
11856 44627 : return omit_one_operand_loc (loc, type, arg1, arg0);
11857 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11858 : case will be handled here. */
11859 13120694 : if (integer_zerop (arg0))
11860 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11861 :
11862 : /* !X && X is always false. */
11863 13120694 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11864 13120694 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11865 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11866 : /* X && !X is always false. */
11867 13120694 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11868 13120694 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11869 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11870 :
11871 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11872 : means A >= Y && A != MAX, but in this case we know that
11873 : A < X <= MAX. */
11874 :
11875 13120694 : if (!TREE_SIDE_EFFECTS (arg0)
11876 13120694 : && !TREE_SIDE_EFFECTS (arg1))
11877 : {
11878 11724329 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11879 11724329 : if (tem && !operand_equal_p (tem, arg0, 0))
11880 417 : return fold_convert (type,
11881 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
11882 : tem, arg1));
11883 :
11884 11723912 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11885 11723912 : if (tem && !operand_equal_p (tem, arg1, 0))
11886 10303 : return fold_convert (type,
11887 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
11888 : arg0, tem));
11889 : }
11890 :
11891 13109974 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11892 : != NULL_TREE)
11893 : return tem;
11894 :
11895 : return NULL_TREE;
11896 :
11897 3288110 : case TRUTH_ORIF_EXPR:
11898 : /* Note that the operands of this must be ints
11899 : and their values must be 0 or true.
11900 : ("true" is a fixed value perhaps depending on the language.) */
11901 : /* If first arg is constant true, return it. */
11902 3288110 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11903 134959 : return fold_convert_loc (loc, type, arg0);
11904 : /* FALLTHRU */
11905 12485774 : case TRUTH_OR_EXPR:
11906 : /* If either arg is constant zero, drop it. */
11907 12485774 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11908 215299 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11909 483422 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11910 : /* Preserve sequence points. */
11911 12701397 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11912 419776 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11913 : /* If second arg is constant true, result is true, but we must
11914 : evaluate first arg. */
11915 11850699 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11916 52500 : return omit_one_operand_loc (loc, type, arg1, arg0);
11917 : /* Likewise for first arg, but note this only occurs here for
11918 : TRUTH_OR_EXPR. */
11919 11798199 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11920 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11921 :
11922 : /* !X || X is always true. */
11923 11798199 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11924 11798199 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11925 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11926 : /* X || !X is always true. */
11927 11798199 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11928 11798199 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11929 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11930 :
11931 : /* (X && !Y) || (!X && Y) is X ^ Y */
11932 11798198 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
11933 1668 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
11934 : {
11935 668 : tree a0, a1, l0, l1, n0, n1;
11936 :
11937 668 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11938 668 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11939 :
11940 668 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11941 668 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11942 :
11943 668 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
11944 668 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
11945 :
11946 668 : if ((operand_equal_p (n0, a0, 0)
11947 18 : && operand_equal_p (n1, a1, 0))
11948 676 : || (operand_equal_p (n0, a1, 0)
11949 3 : && operand_equal_p (n1, a0, 0)))
11950 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
11951 : }
11952 :
11953 11798185 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
11954 : != NULL_TREE)
11955 : return tem;
11956 :
11957 : return NULL_TREE;
11958 :
11959 74051 : case TRUTH_XOR_EXPR:
11960 : /* If the second arg is constant zero, drop it. */
11961 74051 : if (integer_zerop (arg1))
11962 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11963 : /* If the second arg is constant true, this is a logical inversion. */
11964 74051 : if (integer_onep (arg1))
11965 : {
11966 0 : tem = invert_truthvalue_loc (loc, arg0);
11967 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11968 : }
11969 : /* Identical arguments cancel to zero. */
11970 74051 : if (operand_equal_p (arg0, arg1, 0))
11971 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11972 :
11973 : /* !X ^ X is always true. */
11974 74051 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11975 74051 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11976 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11977 :
11978 : /* X ^ !X is always true. */
11979 74051 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11980 74051 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11981 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11982 :
11983 : return NULL_TREE;
11984 :
11985 47317086 : case EQ_EXPR:
11986 47317086 : case NE_EXPR:
11987 47317086 : STRIP_NOPS (arg0);
11988 47317086 : STRIP_NOPS (arg1);
11989 :
11990 47317086 : tem = fold_comparison (loc, code, type, op0, op1);
11991 47317086 : if (tem != NULL_TREE)
11992 : return tem;
11993 :
11994 : /* bool_var != 1 becomes !bool_var. */
11995 48457906 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11996 47350912 : && code == NE_EXPR)
11997 39931 : return fold_convert_loc (loc, type,
11998 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
11999 79862 : TREE_TYPE (arg0), arg0));
12000 :
12001 : /* bool_var == 0 becomes !bool_var. */
12002 48378044 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12003 48198001 : && code == EQ_EXPR)
12004 190894 : return fold_convert_loc (loc, type,
12005 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12006 381788 : TREE_TYPE (arg0), arg0));
12007 :
12008 : /* !exp != 0 becomes !exp */
12009 474990 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12010 47549073 : && code == NE_EXPR)
12011 467296 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12012 :
12013 : /* If this is an EQ or NE comparison with zero and ARG0 is
12014 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12015 : two operations, but the latter can be done in one less insn
12016 : on machines that have only two-operand insns or on which a
12017 : constant cannot be the first operand. */
12018 46607498 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12019 46607498 : && integer_zerop (arg1))
12020 : {
12021 1478025 : tree arg00 = TREE_OPERAND (arg0, 0);
12022 1478025 : tree arg01 = TREE_OPERAND (arg0, 1);
12023 1478025 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12024 1478025 : && integer_onep (TREE_OPERAND (arg00, 0)))
12025 : {
12026 4349 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12027 4349 : arg01, TREE_OPERAND (arg00, 1));
12028 4349 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12029 4349 : build_one_cst (TREE_TYPE (arg0)));
12030 4349 : return fold_build2_loc (loc, code, type,
12031 4349 : fold_convert_loc (loc, TREE_TYPE (arg1),
12032 4349 : tem), arg1);
12033 : }
12034 1473676 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12035 1473676 : && integer_onep (TREE_OPERAND (arg01, 0)))
12036 : {
12037 365 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12038 365 : arg00, TREE_OPERAND (arg01, 1));
12039 365 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12040 365 : build_one_cst (TREE_TYPE (arg0)));
12041 365 : return fold_build2_loc (loc, code, type,
12042 365 : fold_convert_loc (loc, TREE_TYPE (arg1),
12043 365 : tem), arg1);
12044 : }
12045 : }
12046 :
12047 : /* If this is a comparison of a field, we may be able to simplify it. */
12048 46602784 : if ((TREE_CODE (arg0) == COMPONENT_REF
12049 46602784 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12050 : /* Handle the constant case even without -O
12051 : to make sure the warnings are given. */
12052 4679331 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12053 : {
12054 4364653 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12055 4364653 : if (t1)
12056 : return t1;
12057 : }
12058 :
12059 : /* Optimize comparisons of strlen vs zero to a compare of the
12060 : first character of the string vs zero. To wit,
12061 : strlen(ptr) == 0 => *ptr == 0
12062 : strlen(ptr) != 0 => *ptr != 0
12063 : Other cases should reduce to one of these two (or a constant)
12064 : due to the return value of strlen being unsigned. */
12065 45905590 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12066 : {
12067 2865719 : tree fndecl = get_callee_fndecl (arg0);
12068 :
12069 2865719 : if (fndecl
12070 2864641 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12071 549 : && call_expr_nargs (arg0) == 1
12072 2866268 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12073 : == POINTER_TYPE))
12074 : {
12075 549 : tree ptrtype
12076 549 : = build_pointer_type (build_qualified_type (char_type_node,
12077 : TYPE_QUAL_CONST));
12078 1098 : tree ptr = fold_convert_loc (loc, ptrtype,
12079 549 : CALL_EXPR_ARG (arg0, 0));
12080 549 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12081 549 : return fold_build2_loc (loc, code, type, iref,
12082 549 : build_int_cst (TREE_TYPE (iref), 0));
12083 : }
12084 : }
12085 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12086 : (X & C) == 0 when C is a single bit. */
12087 45905041 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12088 1637314 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12089 861 : && integer_zerop (arg1)
12090 45905503 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12091 : {
12092 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12093 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12094 140 : TREE_OPERAND (arg0, 1));
12095 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12096 : type, tem,
12097 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12098 140 : arg1));
12099 : }
12100 :
12101 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12102 : constant C is a power of two, i.e. a single bit. */
12103 45904901 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12104 4671 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12105 0 : && integer_zerop (arg1)
12106 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12107 45904901 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12108 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12109 : {
12110 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12111 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12112 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12113 : }
12114 :
12115 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12116 : when is C is a power of two, i.e. a single bit. */
12117 45904901 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12118 1637174 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12119 35890 : && integer_zerop (arg1)
12120 35890 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12121 45938138 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12122 33237 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12123 : {
12124 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12125 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12126 0 : arg000, TREE_OPERAND (arg0, 1));
12127 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12128 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12129 : }
12130 :
12131 45904901 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12132 4671 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12133 : {
12134 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12135 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12136 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12137 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12138 482 : tree itype = TREE_TYPE (arg0);
12139 :
12140 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12141 : operand_equal_p guarantees no side-effects so we don't need
12142 : to use omit_one_operand on Z. */
12143 482 : if (operand_equal_p (arg01, arg11, 0))
12144 8 : return fold_build2_loc (loc, code, type, arg00,
12145 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12146 8 : arg10));
12147 474 : if (operand_equal_p (arg01, arg10, 0))
12148 0 : return fold_build2_loc (loc, code, type, arg00,
12149 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12150 0 : arg11));
12151 474 : if (operand_equal_p (arg00, arg11, 0))
12152 0 : return fold_build2_loc (loc, code, type, arg01,
12153 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12154 0 : arg10));
12155 474 : if (operand_equal_p (arg00, arg10, 0))
12156 0 : return fold_build2_loc (loc, code, type, arg01,
12157 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12158 0 : arg11));
12159 :
12160 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12161 474 : if (TREE_CODE (arg01) == INTEGER_CST
12162 8 : && TREE_CODE (arg11) == INTEGER_CST)
12163 : {
12164 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12165 : fold_convert_loc (loc, itype, arg11));
12166 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12167 8 : return fold_build2_loc (loc, code, type, tem,
12168 8 : fold_convert_loc (loc, itype, arg10));
12169 : }
12170 : }
12171 :
12172 : /* Attempt to simplify equality/inequality comparisons of complex
12173 : values. Only lower the comparison if the result is known or
12174 : can be simplified to a single scalar comparison. */
12175 45904885 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12176 45902358 : || TREE_CODE (arg0) == COMPLEX_CST)
12177 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12178 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12179 : {
12180 1726 : tree real0, imag0, real1, imag1;
12181 1726 : tree rcond, icond;
12182 :
12183 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12184 : {
12185 1726 : real0 = TREE_OPERAND (arg0, 0);
12186 1726 : imag0 = TREE_OPERAND (arg0, 1);
12187 : }
12188 : else
12189 : {
12190 0 : real0 = TREE_REALPART (arg0);
12191 0 : imag0 = TREE_IMAGPART (arg0);
12192 : }
12193 :
12194 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12195 : {
12196 192 : real1 = TREE_OPERAND (arg1, 0);
12197 192 : imag1 = TREE_OPERAND (arg1, 1);
12198 : }
12199 : else
12200 : {
12201 1534 : real1 = TREE_REALPART (arg1);
12202 1534 : imag1 = TREE_IMAGPART (arg1);
12203 : }
12204 :
12205 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12206 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12207 : {
12208 11 : if (integer_zerop (rcond))
12209 : {
12210 11 : if (code == EQ_EXPR)
12211 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12212 0 : imag0, imag1);
12213 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12214 : }
12215 : else
12216 : {
12217 0 : if (code == NE_EXPR)
12218 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12219 0 : imag0, imag1);
12220 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12221 : }
12222 : }
12223 :
12224 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12225 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12226 : {
12227 9 : if (integer_zerop (icond))
12228 : {
12229 7 : if (code == EQ_EXPR)
12230 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12231 1 : real0, real1);
12232 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12233 : }
12234 : else
12235 : {
12236 2 : if (code == NE_EXPR)
12237 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12238 1 : real0, real1);
12239 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12240 : }
12241 : }
12242 : }
12243 :
12244 : return NULL_TREE;
12245 :
12246 40175466 : case LT_EXPR:
12247 40175466 : case GT_EXPR:
12248 40175466 : case LE_EXPR:
12249 40175466 : case GE_EXPR:
12250 40175466 : tem = fold_comparison (loc, code, type, op0, op1);
12251 40175466 : if (tem != NULL_TREE)
12252 : return tem;
12253 :
12254 : /* Transform comparisons of the form X +- C CMP X. */
12255 39308082 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12256 4703704 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12257 50924 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12258 39308098 : && !HONOR_SNANS (arg0))
12259 : {
12260 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12261 14 : enum tree_code code0 = TREE_CODE (arg0);
12262 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12263 :
12264 : /* (X - c) > X becomes false. */
12265 14 : if (code == GT_EXPR
12266 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12267 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12268 4 : return constant_boolean_node (0, type);
12269 :
12270 : /* Likewise (X + c) < X becomes false. */
12271 10 : if (code == LT_EXPR
12272 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12273 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12274 3 : return constant_boolean_node (0, type);
12275 :
12276 : /* Convert (X - c) <= X to true. */
12277 7 : if (!HONOR_NANS (arg1)
12278 6 : && code == LE_EXPR
12279 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12280 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12281 4 : return constant_boolean_node (1, type);
12282 :
12283 : /* Convert (X + c) >= X to true. */
12284 3 : if (!HONOR_NANS (arg1)
12285 2 : && code == GE_EXPR
12286 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12287 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12288 2 : return constant_boolean_node (1, type);
12289 : }
12290 :
12291 : /* If we are comparing an ABS_EXPR with a constant, we can
12292 : convert all the cases into explicit comparisons, but they may
12293 : well not be faster than doing the ABS and one comparison.
12294 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12295 : and a comparison, and is probably faster. */
12296 39308069 : if (code == LE_EXPR
12297 7524228 : && TREE_CODE (arg1) == INTEGER_CST
12298 5351789 : && TREE_CODE (arg0) == ABS_EXPR
12299 818 : && ! TREE_SIDE_EFFECTS (arg0)
12300 818 : && (tem = negate_expr (arg1)) != 0
12301 818 : && TREE_CODE (tem) == INTEGER_CST
12302 39308887 : && !TREE_OVERFLOW (tem))
12303 1636 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12304 : build2 (GE_EXPR, type,
12305 818 : TREE_OPERAND (arg0, 0), tem),
12306 : build2 (LE_EXPR, type,
12307 1636 : TREE_OPERAND (arg0, 0), arg1));
12308 :
12309 : /* Convert ABS_EXPR<x> >= 0 to true. */
12310 39307251 : if (code == GE_EXPR
12311 4096586 : && (integer_zerop (arg1)
12312 2985328 : || (! HONOR_NANS (arg0)
12313 2343673 : && real_zerop (arg1)))
12314 40418750 : && tree_expr_nonnegative_p (arg0))
12315 1115 : return omit_one_operand_loc (loc, type,
12316 : constant_boolean_node (true, type),
12317 1115 : arg0);
12318 :
12319 : /* Convert ABS_EXPR<x> < 0 to false. */
12320 39306136 : if (code == LT_EXPR
12321 13038564 : && (integer_zerop (arg1) || real_zerop (arg1))
12322 42480329 : && tree_expr_nonnegative_p (arg0))
12323 2477 : return omit_one_operand_loc (loc, type,
12324 : constant_boolean_node (false, type),
12325 2477 : arg0);
12326 :
12327 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12328 : and similarly for >= into !=. */
12329 39303659 : if ((code == LT_EXPR || code == GE_EXPR)
12330 17131558 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12331 5343606 : && TREE_CODE (arg1) == LSHIFT_EXPR
12332 39305162 : && integer_onep (TREE_OPERAND (arg1, 0)))
12333 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12334 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12335 1355 : TREE_OPERAND (arg1, 1)),
12336 2710 : build_zero_cst (TREE_TYPE (arg0)));
12337 :
12338 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12339 : otherwise Y might be >= # of bits in X's type and thus e.g.
12340 : (unsigned char) (1 << Y) for Y 15 might be 0.
12341 : If the cast is widening, then 1 << Y should have unsigned type,
12342 : otherwise if Y is number of bits in the signed shift type minus 1,
12343 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12344 : 31 might be 0xffffffff80000000. */
12345 39302304 : if ((code == LT_EXPR || code == GE_EXPR)
12346 17130203 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12347 5522273 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12348 11630930 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12349 3897912 : && CONVERT_EXPR_P (arg1)
12350 1081843 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12351 42 : && (element_precision (TREE_TYPE (arg1))
12352 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12353 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12354 14 : || (element_precision (TREE_TYPE (arg1))
12355 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12356 39302311 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12357 : {
12358 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12359 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12360 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12361 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12362 14 : build_zero_cst (TREE_TYPE (arg0)));
12363 : }
12364 :
12365 : return NULL_TREE;
12366 :
12367 4501831 : case UNORDERED_EXPR:
12368 4501831 : case ORDERED_EXPR:
12369 4501831 : case UNLT_EXPR:
12370 4501831 : case UNLE_EXPR:
12371 4501831 : case UNGT_EXPR:
12372 4501831 : case UNGE_EXPR:
12373 4501831 : case UNEQ_EXPR:
12374 4501831 : case LTGT_EXPR:
12375 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12376 4501831 : {
12377 4501831 : tree targ0 = strip_float_extensions (arg0);
12378 4501831 : tree targ1 = strip_float_extensions (arg1);
12379 4501831 : tree newtype = TREE_TYPE (targ0);
12380 :
12381 4501831 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12382 1289 : newtype = TREE_TYPE (targ1);
12383 :
12384 4501831 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12385 4501831 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12386 328 : return fold_build2_loc (loc, code, type,
12387 : fold_convert_loc (loc, newtype, targ0),
12388 328 : fold_convert_loc (loc, newtype, targ1));
12389 : }
12390 :
12391 : return NULL_TREE;
12392 :
12393 8242944 : case COMPOUND_EXPR:
12394 : /* When pedantic, a compound expression can be neither an lvalue
12395 : nor an integer constant expression. */
12396 8242944 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12397 : return NULL_TREE;
12398 : /* Don't let (0, 0) be null pointer constant. */
12399 453201 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12400 453201 : : fold_convert_loc (loc, type, arg1);
12401 : return tem;
12402 :
12403 : default:
12404 : return NULL_TREE;
12405 : } /* switch (code) */
12406 : }
12407 :
12408 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12409 : ((A & N) + B) & M -> (A + B) & M
12410 : Similarly if (N & M) == 0,
12411 : ((A | N) + B) & M -> (A + B) & M
12412 : and for - instead of + (or unary - instead of +)
12413 : and/or ^ instead of |.
12414 : If B is constant and (B & M) == 0, fold into A & M.
12415 :
12416 : This function is a helper for match.pd patterns. Return non-NULL
12417 : type in which the simplified operation should be performed only
12418 : if any optimization is possible.
12419 :
12420 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12421 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12422 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12423 : +/-. */
12424 : tree
12425 1244778 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12426 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12427 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12428 : tree *pmop)
12429 : {
12430 1244778 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12431 1244778 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12432 1244778 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12433 2489556 : if (~cst1 == 0
12434 3729248 : || (cst1 & (cst1 + 1)) != 0
12435 1026082 : || !INTEGRAL_TYPE_P (type)
12436 1026082 : || (!TYPE_OVERFLOW_WRAPS (type)
12437 41556 : && TREE_CODE (type) != INTEGER_TYPE)
12438 4539177 : || (wi::max_value (type) & cst1) != cst1)
12439 : return NULL_TREE;
12440 :
12441 1026082 : enum tree_code codes[2] = { code00, code01 };
12442 1026082 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12443 1026082 : int which = 0;
12444 1026082 : wide_int cst0;
12445 :
12446 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12447 : arg1 (M) is == (1LL << cst) - 1.
12448 : Store C into PMOP[0] and D into PMOP[1]. */
12449 1026082 : pmop[0] = arg00;
12450 1026082 : pmop[1] = arg01;
12451 1026082 : which = code != NEGATE_EXPR;
12452 :
12453 3077330 : for (; which >= 0; which--)
12454 2051248 : switch (codes[which])
12455 : {
12456 20706 : case BIT_AND_EXPR:
12457 20706 : case BIT_IOR_EXPR:
12458 20706 : case BIT_XOR_EXPR:
12459 20706 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12460 20706 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12461 20706 : if (codes[which] == BIT_AND_EXPR)
12462 : {
12463 20594 : if (cst0 != cst1)
12464 : break;
12465 : }
12466 112 : else if (cst0 != 0)
12467 : break;
12468 : /* If C or D is of the form (A & N) where
12469 : (N & M) == M, or of the form (A | N) or
12470 : (A ^ N) where (N & M) == 0, replace it with A. */
12471 19167 : pmop[which] = arg0xx[2 * which];
12472 19167 : break;
12473 2030542 : case ERROR_MARK:
12474 2030542 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12475 : break;
12476 : /* If C or D is a N where (N & M) == 0, it can be
12477 : omitted (replaced with 0). */
12478 861742 : if ((code == PLUS_EXPR
12479 202720 : || (code == MINUS_EXPR && which == 0))
12480 633591 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12481 136509 : pmop[which] = build_int_cst (type, 0);
12482 : /* Similarly, with C - N where (-N & M) == 0. */
12483 861742 : if (code == MINUS_EXPR
12484 430871 : && which == 1
12485 626610 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12486 188064 : pmop[which] = build_int_cst (type, 0);
12487 : break;
12488 0 : default:
12489 0 : gcc_unreachable ();
12490 : }
12491 :
12492 : /* Only build anything new if we optimized one or both arguments above. */
12493 1026082 : if (pmop[0] == arg00 && pmop[1] == arg01)
12494 : return NULL_TREE;
12495 :
12496 343002 : if (TYPE_OVERFLOW_WRAPS (type))
12497 : return type;
12498 : else
12499 2333 : return unsigned_type_for (type);
12500 1026082 : }
12501 :
12502 : /* Used by contains_label_[p1]. */
12503 :
12504 : struct contains_label_data
12505 : {
12506 : hash_set<tree> *pset;
12507 : bool inside_switch_p;
12508 : };
12509 :
12510 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12511 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12512 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12513 :
12514 : static tree
12515 4250848 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12516 : {
12517 4250848 : contains_label_data *d = (contains_label_data *) data;
12518 4250848 : switch (TREE_CODE (*tp))
12519 : {
12520 : case LABEL_EXPR:
12521 : return *tp;
12522 :
12523 0 : case CASE_LABEL_EXPR:
12524 0 : if (!d->inside_switch_p)
12525 : return *tp;
12526 : return NULL_TREE;
12527 :
12528 0 : case SWITCH_EXPR:
12529 0 : if (!d->inside_switch_p)
12530 : {
12531 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12532 0 : return *tp;
12533 0 : d->inside_switch_p = true;
12534 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12535 0 : return *tp;
12536 0 : d->inside_switch_p = false;
12537 0 : *walk_subtrees = 0;
12538 : }
12539 : return NULL_TREE;
12540 :
12541 6597 : case GOTO_EXPR:
12542 6597 : *walk_subtrees = 0;
12543 6597 : return NULL_TREE;
12544 :
12545 : default:
12546 : return NULL_TREE;
12547 : }
12548 : }
12549 :
12550 : /* Return whether the sub-tree ST contains a label which is accessible from
12551 : outside the sub-tree. */
12552 :
12553 : static bool
12554 307918 : contains_label_p (tree st)
12555 : {
12556 307918 : hash_set<tree> pset;
12557 307918 : contains_label_data data = { &pset, false };
12558 307918 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12559 307918 : }
12560 :
12561 : /* Fold a ternary expression of code CODE and type TYPE with operands
12562 : OP0, OP1, and OP2. Return the folded expression if folding is
12563 : successful. Otherwise, return NULL_TREE. */
12564 :
12565 : tree
12566 39044765 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12567 : tree op0, tree op1, tree op2)
12568 : {
12569 39044765 : tree tem;
12570 39044765 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12571 39044765 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12572 :
12573 39044765 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12574 : && TREE_CODE_LENGTH (code) == 3);
12575 :
12576 : /* If this is a commutative operation, and OP0 is a constant, move it
12577 : to OP1 to reduce the number of tests below. */
12578 39044765 : if (commutative_ternary_tree_code (code)
12579 39044765 : && tree_swap_operands_p (op0, op1))
12580 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12581 :
12582 39044732 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12583 39044732 : if (tem)
12584 : return tem;
12585 :
12586 : /* Strip any conversions that don't change the mode. This is safe
12587 : for every expression, except for a comparison expression because
12588 : its signedness is derived from its operands. So, in the latter
12589 : case, only strip conversions that don't change the signedness.
12590 :
12591 : Note that this is done as an internal manipulation within the
12592 : constant folder, in order to find the simplest representation of
12593 : the arguments so that their form can be studied. In any cases,
12594 : the appropriate type conversions should be put back in the tree
12595 : that will get out of the constant folder. */
12596 38056934 : if (op0)
12597 : {
12598 37990266 : arg0 = op0;
12599 37990266 : STRIP_NOPS (arg0);
12600 : }
12601 :
12602 38056934 : if (op1)
12603 : {
12604 38056934 : arg1 = op1;
12605 38056934 : STRIP_NOPS (arg1);
12606 : }
12607 :
12608 38056934 : if (op2)
12609 : {
12610 14171186 : arg2 = op2;
12611 14171186 : STRIP_NOPS (arg2);
12612 : }
12613 :
12614 38056934 : switch (code)
12615 : {
12616 23885273 : case COMPONENT_REF:
12617 23885273 : if (TREE_CODE (arg0) == CONSTRUCTOR
12618 23885273 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12619 : {
12620 : unsigned HOST_WIDE_INT idx;
12621 : tree field, value;
12622 884 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12623 679 : if (field == arg1)
12624 : return value;
12625 : }
12626 : return NULL_TREE;
12627 :
12628 11911056 : case COND_EXPR:
12629 11911056 : case VEC_COND_EXPR:
12630 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12631 : so all simple results must be passed through pedantic_non_lvalue. */
12632 11911056 : if (TREE_CODE (arg0) == INTEGER_CST)
12633 : {
12634 435070 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12635 435070 : tem = integer_zerop (arg0) ? op2 : op1;
12636 : /* Only optimize constant conditions when the selected branch
12637 : has the same type as the COND_EXPR. This avoids optimizing
12638 : away "c ? x : throw", where the throw has a void type.
12639 : Avoid throwing away that operand which contains label. */
12640 435070 : if ((!TREE_SIDE_EFFECTS (unused_op)
12641 307918 : || !contains_label_p (unused_op))
12642 738167 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12643 352417 : || VOID_TYPE_P (type)))
12644 421043 : return protected_set_expr_location_unshare (tem, loc);
12645 14027 : return NULL_TREE;
12646 : }
12647 11475986 : else if (TREE_CODE (arg0) == VECTOR_CST)
12648 : {
12649 10762 : unsigned HOST_WIDE_INT nelts;
12650 10762 : if ((TREE_CODE (arg1) == VECTOR_CST
12651 8187 : || TREE_CODE (arg1) == CONSTRUCTOR)
12652 2575 : && (TREE_CODE (arg2) == VECTOR_CST
12653 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12654 21524 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12655 : {
12656 2575 : vec_perm_builder sel (nelts, nelts, 1);
12657 25997 : for (unsigned int i = 0; i < nelts; i++)
12658 : {
12659 23422 : tree val = VECTOR_CST_ELT (arg0, i);
12660 23422 : if (integer_all_onesp (val))
12661 11633 : sel.quick_push (i);
12662 11789 : else if (integer_zerop (val))
12663 11789 : sel.quick_push (nelts + i);
12664 : else /* Currently unreachable. */
12665 1883 : return NULL_TREE;
12666 : }
12667 2575 : vec_perm_indices indices (sel, 2, nelts);
12668 2575 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12669 2575 : if (t != NULL_TREE)
12670 1883 : return t;
12671 4458 : }
12672 : }
12673 :
12674 : /* If we have A op B ? A : C, we may be able to convert this to a
12675 : simpler expression, depending on the operation and the values
12676 : of B and C. Signed zeros prevent all of these transformations,
12677 : for reasons given above each one.
12678 :
12679 : Also try swapping the arguments and inverting the conditional. */
12680 11474103 : if (COMPARISON_CLASS_P (arg0)
12681 9481777 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12682 11608972 : && !HONOR_SIGNED_ZEROS (op1))
12683 : {
12684 124129 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12685 124129 : TREE_OPERAND (arg0, 0),
12686 124129 : TREE_OPERAND (arg0, 1),
12687 : op1, op2);
12688 124129 : if (tem)
12689 : return tem;
12690 : }
12691 :
12692 11467438 : if (COMPARISON_CLASS_P (arg0)
12693 9475112 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12694 11926772 : && !HONOR_SIGNED_ZEROS (op2))
12695 : {
12696 377755 : enum tree_code comp_code = TREE_CODE (arg0);
12697 377755 : tree arg00 = TREE_OPERAND (arg0, 0);
12698 377755 : tree arg01 = TREE_OPERAND (arg0, 1);
12699 377755 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12700 377755 : if (comp_code != ERROR_MARK)
12701 377755 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12702 : arg00,
12703 : arg01,
12704 : op2, op1);
12705 377755 : if (tem)
12706 : return tem;
12707 : }
12708 :
12709 : /* If the second operand is simpler than the third, swap them
12710 : since that produces better jump optimization results. */
12711 11202913 : if (truth_value_p (TREE_CODE (arg0))
12712 11202913 : && tree_swap_operands_p (op1, op2))
12713 : {
12714 1950127 : location_t loc0 = expr_location_or (arg0, loc);
12715 : /* See if this can be inverted. If it can't, possibly because
12716 : it was a floating-point inequality comparison, don't do
12717 : anything. */
12718 1950127 : tem = fold_invert_truthvalue (loc0, arg0);
12719 1950127 : if (tem)
12720 1246926 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12721 : }
12722 :
12723 : /* Convert A ? 1 : 0 to simply A. */
12724 9955987 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12725 9570160 : : (integer_onep (op1)
12726 395978 : && !VECTOR_TYPE_P (type)))
12727 632653 : && integer_zerop (op2)
12728 : /* If we try to convert OP0 to our type, the
12729 : call to fold will try to move the conversion inside
12730 : a COND, which will recurse. In that case, the COND_EXPR
12731 : is probably the best choice, so leave it alone. */
12732 10960978 : && type == TREE_TYPE (arg0))
12733 31638 : return protected_set_expr_location_unshare (arg0, loc);
12734 :
12735 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12736 : over COND_EXPR in cases such as floating point comparisons. */
12737 9924349 : if (integer_zerop (op1)
12738 362047 : && code == COND_EXPR
12739 350297 : && integer_onep (op2)
12740 30020 : && !VECTOR_TYPE_P (type)
12741 9954369 : && truth_value_p (TREE_CODE (arg0)))
12742 28465 : return fold_convert_loc (loc, type,
12743 28465 : invert_truthvalue_loc (loc, arg0));
12744 :
12745 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12746 9895884 : if (TREE_CODE (arg0) == LT_EXPR
12747 1312039 : && integer_zerop (TREE_OPERAND (arg0, 1))
12748 36520 : && integer_zerop (op2)
12749 9896854 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12750 : {
12751 : /* sign_bit_p looks through both zero and sign extensions,
12752 : but for this optimization only sign extensions are
12753 : usable. */
12754 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12755 56 : while (tem != tem2)
12756 : {
12757 0 : if (TREE_CODE (tem2) != NOP_EXPR
12758 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12759 : {
12760 : tem = NULL_TREE;
12761 : break;
12762 : }
12763 0 : tem2 = TREE_OPERAND (tem2, 0);
12764 : }
12765 : /* sign_bit_p only checks ARG1 bits within A's precision.
12766 : If <sign bit of A> has wider type than A, bits outside
12767 : of A's precision in <sign bit of A> need to be checked.
12768 : If they are all 0, this optimization needs to be done
12769 : in unsigned A's type, if they are all 1 in signed A's type,
12770 : otherwise this can't be done. */
12771 56 : if (tem
12772 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12773 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12774 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12775 56 : < TYPE_PRECISION (type))
12776 : {
12777 56 : int inner_width, outer_width;
12778 56 : tree tem_type;
12779 :
12780 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12781 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12782 56 : if (outer_width > TYPE_PRECISION (type))
12783 0 : outer_width = TYPE_PRECISION (type);
12784 :
12785 56 : wide_int mask = wi::shifted_mask
12786 56 : (inner_width, outer_width - inner_width, false,
12787 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
12788 :
12789 56 : wide_int common = mask & wi::to_wide (arg1);
12790 56 : if (common == mask)
12791 : {
12792 28 : tem_type = signed_type_for (TREE_TYPE (tem));
12793 28 : tem = fold_convert_loc (loc, tem_type, tem);
12794 : }
12795 28 : else if (common == 0)
12796 : {
12797 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
12798 0 : tem = fold_convert_loc (loc, tem_type, tem);
12799 : }
12800 : else
12801 : tem = NULL;
12802 56 : }
12803 :
12804 56 : if (tem)
12805 28 : return
12806 56 : fold_convert_loc (loc, type,
12807 : fold_build2_loc (loc, BIT_AND_EXPR,
12808 28 : TREE_TYPE (tem), tem,
12809 : fold_convert_loc (loc,
12810 28 : TREE_TYPE (tem),
12811 28 : arg1)));
12812 : }
12813 :
12814 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12815 : already handled above. */
12816 9895856 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12817 347 : && integer_onep (TREE_OPERAND (arg0, 1))
12818 3 : && integer_zerop (op2)
12819 9895856 : && integer_pow2p (arg1))
12820 : {
12821 0 : tree tem = TREE_OPERAND (arg0, 0);
12822 0 : STRIP_NOPS (tem);
12823 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
12824 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12825 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
12826 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
12827 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
12828 : fold_convert_loc (loc, type,
12829 0 : TREE_OPERAND (tem, 0)),
12830 0 : op1);
12831 : }
12832 :
12833 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
12834 : is probably obsolete because the first operand should be a
12835 : truth value (that's why we have the two cases above), but let's
12836 : leave it in until we can confirm this for all front-ends. */
12837 9895856 : if (integer_zerop (op2)
12838 1887691 : && TREE_CODE (arg0) == NE_EXPR
12839 492436 : && integer_zerop (TREE_OPERAND (arg0, 1))
12840 260692 : && integer_pow2p (arg1)
12841 31184 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12842 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12843 : arg1, OEP_ONLY_CONST)
12844 : /* operand_equal_p compares just value, not precision, so e.g.
12845 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
12846 : second operand 32-bit -128, which is not a power of two (or vice
12847 : versa. */
12848 9895856 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
12849 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12850 :
12851 : /* Disable the transformations below for vectors, since
12852 : fold_binary_op_with_conditional_arg may undo them immediately,
12853 : yielding an infinite loop. */
12854 9895856 : if (code == VEC_COND_EXPR)
12855 : return NULL_TREE;
12856 :
12857 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
12858 9510029 : if (integer_zerop (op2)
12859 1580017 : && truth_value_p (TREE_CODE (arg0))
12860 1410753 : && truth_value_p (TREE_CODE (arg1))
12861 9543406 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12862 33377 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12863 : : TRUTH_ANDIF_EXPR,
12864 33377 : type, fold_convert_loc (loc, type, arg0), op1);
12865 :
12866 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12867 9476652 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12868 432730 : && truth_value_p (TREE_CODE (arg0))
12869 291998 : && truth_value_p (TREE_CODE (arg1))
12870 9513877 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12871 : {
12872 37225 : location_t loc0 = expr_location_or (arg0, loc);
12873 : /* Only perform transformation if ARG0 is easily inverted. */
12874 37225 : tem = fold_invert_truthvalue (loc0, arg0);
12875 37225 : if (tem)
12876 36961 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12877 : ? BIT_IOR_EXPR
12878 : : TRUTH_ORIF_EXPR,
12879 : type, fold_convert_loc (loc, type, tem),
12880 36961 : op1);
12881 : }
12882 :
12883 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12884 9439691 : if (integer_zerop (arg1)
12885 321909 : && truth_value_p (TREE_CODE (arg0))
12886 80625 : && truth_value_p (TREE_CODE (op2))
12887 9439719 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12888 : {
12889 28 : location_t loc0 = expr_location_or (arg0, loc);
12890 : /* Only perform transformation if ARG0 is easily inverted. */
12891 28 : tem = fold_invert_truthvalue (loc0, arg0);
12892 28 : if (tem)
12893 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12894 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
12895 : type, fold_convert_loc (loc, type, tem),
12896 0 : op2);
12897 : }
12898 :
12899 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
12900 9439691 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
12901 364340 : && truth_value_p (TREE_CODE (arg0))
12902 259041 : && truth_value_p (TREE_CODE (op2))
12903 9439877 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12904 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12905 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
12906 186 : type, fold_convert_loc (loc, type, arg0), op2);
12907 :
12908 : return NULL_TREE;
12909 :
12910 0 : case CALL_EXPR:
12911 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
12912 : of fold_ternary on them. */
12913 0 : gcc_unreachable ();
12914 :
12915 760257 : case BIT_FIELD_REF:
12916 760257 : if (TREE_CODE (arg0) == VECTOR_CST
12917 46729 : && (type == TREE_TYPE (TREE_TYPE (arg0))
12918 16129 : || (VECTOR_TYPE_P (type)
12919 15261 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
12920 45841 : && tree_fits_uhwi_p (op1)
12921 806098 : && tree_fits_uhwi_p (op2))
12922 : {
12923 45841 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
12924 45841 : unsigned HOST_WIDE_INT width
12925 45841 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
12926 45841 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
12927 45841 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
12928 45841 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
12929 :
12930 45841 : if (n != 0
12931 45841 : && (idx % width) == 0
12932 45841 : && (n % width) == 0
12933 91682 : && known_le ((idx + n) / width,
12934 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
12935 : {
12936 45841 : idx = idx / width;
12937 45841 : n = n / width;
12938 :
12939 45841 : if (TREE_CODE (arg0) == VECTOR_CST)
12940 : {
12941 45841 : if (n == 1)
12942 : {
12943 30604 : tem = VECTOR_CST_ELT (arg0, idx);
12944 30604 : if (VECTOR_TYPE_P (type))
12945 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
12946 30604 : return tem;
12947 : }
12948 :
12949 15237 : tree_vector_builder vals (type, n, 1);
12950 71149 : for (unsigned i = 0; i < n; ++i)
12951 55912 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
12952 15237 : return vals.build ();
12953 15237 : }
12954 : }
12955 : }
12956 :
12957 : /* On constants we can use native encode/interpret to constant
12958 : fold (nearly) all BIT_FIELD_REFs. */
12959 714416 : if (CONSTANT_CLASS_P (arg0)
12960 1692 : && can_native_interpret_type_p (type)
12961 : && BITS_PER_UNIT == 8
12962 1692 : && tree_fits_uhwi_p (op1)
12963 716108 : && tree_fits_uhwi_p (op2))
12964 : {
12965 1692 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
12966 1692 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
12967 : /* Limit us to a reasonable amount of work. To relax the
12968 : other limitations we need bit-shifting of the buffer
12969 : and rounding up the size. */
12970 1692 : if (bitpos % BITS_PER_UNIT == 0
12971 1692 : && bitsize % BITS_PER_UNIT == 0
12972 1692 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
12973 : {
12974 1692 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
12975 1692 : unsigned HOST_WIDE_INT len
12976 1692 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
12977 1692 : bitpos / BITS_PER_UNIT);
12978 1692 : if (len > 0
12979 1692 : && len * BITS_PER_UNIT >= bitsize)
12980 : {
12981 1692 : tree v = native_interpret_expr (type, b,
12982 : bitsize / BITS_PER_UNIT);
12983 1692 : if (v)
12984 1638 : return v;
12985 : }
12986 : }
12987 : }
12988 :
12989 : return NULL_TREE;
12990 :
12991 750518 : case VEC_PERM_EXPR:
12992 : /* Perform constant folding of BIT_INSERT_EXPR. */
12993 750518 : if (TREE_CODE (arg2) == VECTOR_CST
12994 739045 : && TREE_CODE (op0) == VECTOR_CST
12995 15342 : && TREE_CODE (op1) == VECTOR_CST)
12996 : {
12997 : /* Build a vector of integers from the tree mask. */
12998 3951 : vec_perm_builder builder;
12999 3951 : if (!tree_to_vec_perm_builder (&builder, arg2))
13000 : return NULL_TREE;
13001 :
13002 : /* Create a vec_perm_indices for the integer vector. */
13003 3951 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13004 3951 : bool single_arg = (op0 == op1);
13005 7902 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13006 3951 : return fold_vec_perm (type, op0, op1, sel);
13007 7902 : }
13008 : return NULL_TREE;
13009 :
13010 14778 : case BIT_INSERT_EXPR:
13011 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13012 14778 : if (TREE_CODE (arg0) == INTEGER_CST
13013 14 : && TREE_CODE (arg1) == INTEGER_CST)
13014 : {
13015 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13016 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13017 2 : if (BYTES_BIG_ENDIAN)
13018 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13019 2 : wide_int tem = (wi::to_wide (arg0)
13020 4 : & wi::shifted_mask (bitpos, bitsize, true,
13021 4 : TYPE_PRECISION (type)));
13022 2 : wide_int tem2
13023 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13024 2 : bitsize), bitpos);
13025 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13026 2 : }
13027 14776 : else if (TREE_CODE (arg0) == VECTOR_CST
13028 906 : && CONSTANT_CLASS_P (arg1)
13029 15078 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13030 302 : TREE_TYPE (arg1)))
13031 : {
13032 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13033 302 : unsigned HOST_WIDE_INT elsize
13034 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13035 302 : if (bitpos % elsize == 0)
13036 : {
13037 302 : unsigned k = bitpos / elsize;
13038 302 : unsigned HOST_WIDE_INT nelts;
13039 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13040 39044765 : return arg0;
13041 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13042 : {
13043 290 : tree_vector_builder elts (type, nelts, 1);
13044 290 : elts.quick_grow (nelts);
13045 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13046 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13047 290 : return elts.build ();
13048 290 : }
13049 : }
13050 : }
13051 : return NULL_TREE;
13052 :
13053 : default:
13054 : return NULL_TREE;
13055 : } /* switch (code) */
13056 : }
13057 :
13058 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13059 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13060 : constructor element index of the value returned. If the element is
13061 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13062 : the index of the element after the ACCESS_INDEX position (which
13063 : may be outside of the CTOR array). */
13064 :
13065 : tree
13066 661633 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13067 : unsigned *ctor_idx)
13068 : {
13069 661633 : tree index_type = NULL_TREE;
13070 661633 : signop index_sgn = UNSIGNED;
13071 661633 : offset_int low_bound = 0;
13072 :
13073 661633 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13074 : {
13075 661633 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13076 661633 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13077 : {
13078 : /* Static constructors for variably sized objects makes no sense. */
13079 661633 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13080 661633 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13081 : /* ??? When it is obvious that the range is signed, treat it so. */
13082 661633 : if (TYPE_UNSIGNED (index_type)
13083 327398 : && TYPE_MAX_VALUE (domain_type)
13084 989000 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13085 327367 : TYPE_MIN_VALUE (domain_type)))
13086 : {
13087 0 : index_sgn = SIGNED;
13088 0 : low_bound
13089 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13090 : SIGNED);
13091 : }
13092 : else
13093 : {
13094 661633 : index_sgn = TYPE_SIGN (index_type);
13095 661633 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13096 : }
13097 : }
13098 : }
13099 :
13100 661633 : if (index_type)
13101 661633 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13102 : index_sgn);
13103 :
13104 661633 : offset_int index = low_bound;
13105 661633 : if (index_type)
13106 661633 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13107 :
13108 661633 : offset_int max_index = index;
13109 661633 : unsigned cnt;
13110 661633 : tree cfield, cval;
13111 661633 : bool first_p = true;
13112 :
13113 13610394 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13114 : {
13115 : /* Array constructor might explicitly set index, or specify a range,
13116 : or leave index NULL meaning that it is next index after previous
13117 : one. */
13118 13609269 : if (cfield)
13119 : {
13120 5597016 : if (TREE_CODE (cfield) == INTEGER_CST)
13121 11192586 : max_index = index
13122 5596293 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13123 : else
13124 : {
13125 723 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13126 723 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13127 : index_sgn);
13128 723 : max_index
13129 723 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13130 : index_sgn);
13131 723 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13132 : }
13133 : }
13134 8012253 : else if (!first_p)
13135 : {
13136 7805362 : index = max_index + 1;
13137 7805362 : if (index_type)
13138 7805362 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13139 7805362 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13140 7805362 : max_index = index;
13141 : }
13142 : else
13143 : first_p = false;
13144 :
13145 13609269 : if (TREE_CODE (cval) == RAW_DATA_CST)
13146 2617 : max_index += RAW_DATA_LENGTH (cval) - 1;
13147 :
13148 : /* Do we have match? */
13149 13609269 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13150 : {
13151 13608981 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13152 : {
13153 660392 : if (ctor_idx)
13154 660392 : *ctor_idx = cnt;
13155 660392 : return cval;
13156 : }
13157 : }
13158 288 : else if (in_gimple_form)
13159 : /* We're past the element we search for. Note during parsing
13160 : the elements might not be sorted.
13161 : ??? We should use a binary search and a flag on the
13162 : CONSTRUCTOR as to whether elements are sorted in declaration
13163 : order. */
13164 : break;
13165 : }
13166 1241 : if (ctor_idx)
13167 1241 : *ctor_idx = cnt;
13168 : return NULL_TREE;
13169 : }
13170 :
13171 : /* Perform constant folding and related simplification of EXPR.
13172 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13173 : and application of the associative law.
13174 : NOP_EXPR conversions may be removed freely (as long as we
13175 : are careful not to change the type of the overall expression).
13176 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13177 : but we can constant-fold them if they have constant operands. */
13178 :
13179 : #ifdef ENABLE_FOLD_CHECKING
13180 : # define fold(x) fold_1 (x)
13181 : static tree fold_1 (tree);
13182 : static
13183 : #endif
13184 : tree
13185 1315327268 : fold (tree expr)
13186 : {
13187 1315501414 : const tree t = expr;
13188 1315501414 : enum tree_code code = TREE_CODE (t);
13189 1315501414 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13190 1315501414 : tree tem;
13191 1315501414 : location_t loc = EXPR_LOCATION (expr);
13192 :
13193 : /* Return right away if a constant. */
13194 1315501414 : if (kind == tcc_constant)
13195 : return t;
13196 :
13197 : /* CALL_EXPR-like objects with variable numbers of operands are
13198 : treated specially. */
13199 1211775741 : if (kind == tcc_vl_exp)
13200 : {
13201 172149337 : if (code == CALL_EXPR)
13202 : {
13203 172148836 : tem = fold_call_expr (loc, expr, false);
13204 341877373 : return tem ? tem : expr;
13205 : }
13206 : return expr;
13207 : }
13208 :
13209 1039626404 : if (IS_EXPR_CODE_CLASS (kind))
13210 : {
13211 1037476801 : tree type = TREE_TYPE (t);
13212 1037476801 : tree op0, op1, op2;
13213 :
13214 1037476801 : switch (TREE_CODE_LENGTH (code))
13215 : {
13216 941467646 : case 1:
13217 941467646 : op0 = TREE_OPERAND (t, 0);
13218 941467646 : tem = fold_unary_loc (loc, code, type, op0);
13219 1611054258 : return tem ? tem : expr;
13220 86974903 : case 2:
13221 86974903 : op0 = TREE_OPERAND (t, 0);
13222 86974903 : op1 = TREE_OPERAND (t, 1);
13223 86974903 : tem = fold_binary_loc (loc, code, type, op0, op1);
13224 163641377 : return tem ? tem : expr;
13225 4319125 : case 3:
13226 4319125 : op0 = TREE_OPERAND (t, 0);
13227 4319125 : op1 = TREE_OPERAND (t, 1);
13228 4319125 : op2 = TREE_OPERAND (t, 2);
13229 4319125 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13230 8365257 : return tem ? tem : expr;
13231 : default:
13232 : break;
13233 : }
13234 : }
13235 :
13236 6864730 : switch (code)
13237 : {
13238 4611203 : case ARRAY_REF:
13239 4611203 : {
13240 4611203 : tree op0 = TREE_OPERAND (t, 0);
13241 4611203 : tree op1 = TREE_OPERAND (t, 1);
13242 :
13243 4611203 : if (TREE_CODE (op1) == INTEGER_CST
13244 2909796 : && TREE_CODE (op0) == CONSTRUCTOR
13245 4612658 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13246 : {
13247 1455 : unsigned int idx;
13248 1455 : tree val
13249 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13250 : &idx);
13251 1455 : if (val)
13252 : {
13253 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13254 : return val;
13255 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13256 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13257 : != INTEGER_CST))
13258 : return t;
13259 2 : offset_int o
13260 2 : = (wi::to_offset (op1)
13261 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13262 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13263 2 : return build_int_cst (TREE_TYPE (val),
13264 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13265 : }
13266 : }
13267 :
13268 : return t;
13269 : }
13270 :
13271 : /* Return a VECTOR_CST if possible. */
13272 154696 : case CONSTRUCTOR:
13273 154696 : {
13274 154696 : tree type = TREE_TYPE (t);
13275 154696 : if (TREE_CODE (type) != VECTOR_TYPE)
13276 : return t;
13277 :
13278 : unsigned i;
13279 : tree val;
13280 324645 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13281 282029 : if (! CONSTANT_CLASS_P (val))
13282 : return t;
13283 :
13284 42616 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13285 : }
13286 :
13287 174146 : case CONST_DECL:
13288 174146 : return fold (DECL_INITIAL (t));
13289 :
13290 : default:
13291 : return t;
13292 : } /* switch (code) */
13293 : }
13294 :
13295 : #ifdef ENABLE_FOLD_CHECKING
13296 : #undef fold
13297 :
13298 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13299 : hash_table<nofree_ptr_hash<const tree_node> > *);
13300 : static void fold_check_failed (const_tree, const_tree);
13301 : void print_fold_checksum (const_tree);
13302 :
13303 : /* When --enable-checking=fold, compute a digest of expr before
13304 : and after actual fold call to see if fold did not accidentally
13305 : change original expr. */
13306 :
13307 : tree
13308 : fold (tree expr)
13309 : {
13310 : tree ret;
13311 : struct md5_ctx ctx;
13312 : unsigned char checksum_before[16], checksum_after[16];
13313 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13314 :
13315 : md5_init_ctx (&ctx);
13316 : fold_checksum_tree (expr, &ctx, &ht);
13317 : md5_finish_ctx (&ctx, checksum_before);
13318 : ht.empty ();
13319 :
13320 : ret = fold_1 (expr);
13321 :
13322 : md5_init_ctx (&ctx);
13323 : fold_checksum_tree (expr, &ctx, &ht);
13324 : md5_finish_ctx (&ctx, checksum_after);
13325 :
13326 : if (memcmp (checksum_before, checksum_after, 16))
13327 : fold_check_failed (expr, ret);
13328 :
13329 : return ret;
13330 : }
13331 :
13332 : void
13333 : print_fold_checksum (const_tree expr)
13334 : {
13335 : struct md5_ctx ctx;
13336 : unsigned char checksum[16], cnt;
13337 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13338 :
13339 : md5_init_ctx (&ctx);
13340 : fold_checksum_tree (expr, &ctx, &ht);
13341 : md5_finish_ctx (&ctx, checksum);
13342 : for (cnt = 0; cnt < 16; ++cnt)
13343 : fprintf (stderr, "%02x", checksum[cnt]);
13344 : putc ('\n', stderr);
13345 : }
13346 :
13347 : static void
13348 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13349 : {
13350 : internal_error ("fold check: original tree changed by fold");
13351 : }
13352 :
13353 : static void
13354 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13355 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13356 : {
13357 : const tree_node **slot;
13358 : enum tree_code code;
13359 : union tree_node *buf;
13360 : int i, len;
13361 :
13362 : recursive_label:
13363 : if (expr == NULL)
13364 : return;
13365 : slot = ht->find_slot (expr, INSERT);
13366 : if (*slot != NULL)
13367 : return;
13368 : *slot = expr;
13369 : code = TREE_CODE (expr);
13370 : if (TREE_CODE_CLASS (code) == tcc_declaration
13371 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13372 : {
13373 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13374 : size_t sz = tree_size (expr);
13375 : buf = XALLOCAVAR (union tree_node, sz);
13376 : memcpy ((char *) buf, expr, sz);
13377 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13378 : buf->decl_with_vis.symtab_node = NULL;
13379 : buf->base.nowarning_flag = 0;
13380 : expr = (tree) buf;
13381 : }
13382 : else if (TREE_CODE_CLASS (code) == tcc_type
13383 : && (TYPE_POINTER_TO (expr)
13384 : || TYPE_REFERENCE_TO (expr)
13385 : || TYPE_CACHED_VALUES_P (expr)
13386 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13387 : || TYPE_NEXT_VARIANT (expr)
13388 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13389 : {
13390 : /* Allow these fields to be modified. */
13391 : tree tmp;
13392 : size_t sz = tree_size (expr);
13393 : buf = XALLOCAVAR (union tree_node, sz);
13394 : memcpy ((char *) buf, expr, sz);
13395 : expr = tmp = (tree) buf;
13396 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13397 : TYPE_POINTER_TO (tmp) = NULL;
13398 : TYPE_REFERENCE_TO (tmp) = NULL;
13399 : TYPE_NEXT_VARIANT (tmp) = NULL;
13400 : TYPE_ALIAS_SET (tmp) = -1;
13401 : if (TYPE_CACHED_VALUES_P (tmp))
13402 : {
13403 : TYPE_CACHED_VALUES_P (tmp) = 0;
13404 : TYPE_CACHED_VALUES (tmp) = NULL;
13405 : }
13406 : }
13407 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13408 : {
13409 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13410 : that and change builtins.cc etc. instead - see PR89543. */
13411 : size_t sz = tree_size (expr);
13412 : buf = XALLOCAVAR (union tree_node, sz);
13413 : memcpy ((char *) buf, expr, sz);
13414 : buf->base.nowarning_flag = 0;
13415 : expr = (tree) buf;
13416 : }
13417 : md5_process_bytes (expr, tree_size (expr), ctx);
13418 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13419 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13420 : if (TREE_CODE_CLASS (code) != tcc_type
13421 : && TREE_CODE_CLASS (code) != tcc_declaration
13422 : && code != TREE_LIST
13423 : && code != SSA_NAME
13424 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13425 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13426 : switch (TREE_CODE_CLASS (code))
13427 : {
13428 : case tcc_constant:
13429 : switch (code)
13430 : {
13431 : case STRING_CST:
13432 : md5_process_bytes (TREE_STRING_POINTER (expr),
13433 : TREE_STRING_LENGTH (expr), ctx);
13434 : break;
13435 : case COMPLEX_CST:
13436 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13437 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13438 : break;
13439 : case VECTOR_CST:
13440 : len = vector_cst_encoded_nelts (expr);
13441 : for (i = 0; i < len; ++i)
13442 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13443 : break;
13444 : default:
13445 : break;
13446 : }
13447 : break;
13448 : case tcc_exceptional:
13449 : switch (code)
13450 : {
13451 : case TREE_LIST:
13452 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13453 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13454 : expr = TREE_CHAIN (expr);
13455 : goto recursive_label;
13456 : break;
13457 : case TREE_VEC:
13458 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13459 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13460 : break;
13461 : default:
13462 : break;
13463 : }
13464 : break;
13465 : case tcc_expression:
13466 : case tcc_reference:
13467 : case tcc_comparison:
13468 : case tcc_unary:
13469 : case tcc_binary:
13470 : case tcc_statement:
13471 : case tcc_vl_exp:
13472 : len = TREE_OPERAND_LENGTH (expr);
13473 : for (i = 0; i < len; ++i)
13474 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13475 : break;
13476 : case tcc_declaration:
13477 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13478 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13479 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13480 : {
13481 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13482 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13483 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13484 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13485 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13486 : }
13487 :
13488 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13489 : {
13490 : if (TREE_CODE (expr) == FUNCTION_DECL)
13491 : {
13492 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13493 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13494 : }
13495 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13496 : }
13497 : break;
13498 : case tcc_type:
13499 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13500 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13501 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13502 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13503 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13504 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13505 : if (INTEGRAL_TYPE_P (expr)
13506 : || SCALAR_FLOAT_TYPE_P (expr))
13507 : {
13508 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13509 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13510 : }
13511 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13512 : if (RECORD_OR_UNION_TYPE_P (expr))
13513 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13514 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13515 : break;
13516 : default:
13517 : break;
13518 : }
13519 : }
13520 :
13521 : /* Helper function for outputting the checksum of a tree T. When
13522 : debugging with gdb, you can "define mynext" to be "next" followed
13523 : by "call debug_fold_checksum (op0)", then just trace down till the
13524 : outputs differ. */
13525 :
13526 : DEBUG_FUNCTION void
13527 : debug_fold_checksum (const_tree t)
13528 : {
13529 : int i;
13530 : unsigned char checksum[16];
13531 : struct md5_ctx ctx;
13532 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13533 :
13534 : md5_init_ctx (&ctx);
13535 : fold_checksum_tree (t, &ctx, &ht);
13536 : md5_finish_ctx (&ctx, checksum);
13537 : ht.empty ();
13538 :
13539 : for (i = 0; i < 16; i++)
13540 : fprintf (stderr, "%d ", checksum[i]);
13541 :
13542 : fprintf (stderr, "\n");
13543 : }
13544 :
13545 : #endif
13546 :
13547 : /* Fold a unary tree expression with code CODE of type TYPE with an
13548 : operand OP0. LOC is the location of the resulting expression.
13549 : Return a folded expression if successful. Otherwise, return a tree
13550 : expression with code CODE of type TYPE with an operand OP0. */
13551 :
13552 : tree
13553 957060687 : fold_build1_loc (location_t loc,
13554 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13555 : {
13556 957060687 : tree tem;
13557 : #ifdef ENABLE_FOLD_CHECKING
13558 : unsigned char checksum_before[16], checksum_after[16];
13559 : struct md5_ctx ctx;
13560 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13561 :
13562 : md5_init_ctx (&ctx);
13563 : fold_checksum_tree (op0, &ctx, &ht);
13564 : md5_finish_ctx (&ctx, checksum_before);
13565 : ht.empty ();
13566 : #endif
13567 :
13568 957060687 : tem = fold_unary_loc (loc, code, type, op0);
13569 957060687 : if (!tem)
13570 482545377 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13571 :
13572 : #ifdef ENABLE_FOLD_CHECKING
13573 : md5_init_ctx (&ctx);
13574 : fold_checksum_tree (op0, &ctx, &ht);
13575 : md5_finish_ctx (&ctx, checksum_after);
13576 :
13577 : if (memcmp (checksum_before, checksum_after, 16))
13578 : fold_check_failed (op0, tem);
13579 : #endif
13580 957060687 : return tem;
13581 : }
13582 :
13583 : /* Fold a binary tree expression with code CODE of type TYPE with
13584 : operands OP0 and OP1. LOC is the location of the resulting
13585 : expression. Return a folded expression if successful. Otherwise,
13586 : return a tree expression with code CODE of type TYPE with operands
13587 : OP0 and OP1. */
13588 :
13589 : tree
13590 655010185 : fold_build2_loc (location_t loc,
13591 : enum tree_code code, tree type, tree op0, tree op1
13592 : MEM_STAT_DECL)
13593 : {
13594 655010185 : tree tem;
13595 : #ifdef ENABLE_FOLD_CHECKING
13596 : unsigned char checksum_before_op0[16],
13597 : checksum_before_op1[16],
13598 : checksum_after_op0[16],
13599 : checksum_after_op1[16];
13600 : struct md5_ctx ctx;
13601 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13602 :
13603 : md5_init_ctx (&ctx);
13604 : fold_checksum_tree (op0, &ctx, &ht);
13605 : md5_finish_ctx (&ctx, checksum_before_op0);
13606 : ht.empty ();
13607 :
13608 : md5_init_ctx (&ctx);
13609 : fold_checksum_tree (op1, &ctx, &ht);
13610 : md5_finish_ctx (&ctx, checksum_before_op1);
13611 : ht.empty ();
13612 : #endif
13613 :
13614 655010185 : tem = fold_binary_loc (loc, code, type, op0, op1);
13615 655010185 : if (!tem)
13616 367628145 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13617 :
13618 : #ifdef ENABLE_FOLD_CHECKING
13619 : md5_init_ctx (&ctx);
13620 : fold_checksum_tree (op0, &ctx, &ht);
13621 : md5_finish_ctx (&ctx, checksum_after_op0);
13622 : ht.empty ();
13623 :
13624 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13625 : fold_check_failed (op0, tem);
13626 :
13627 : md5_init_ctx (&ctx);
13628 : fold_checksum_tree (op1, &ctx, &ht);
13629 : md5_finish_ctx (&ctx, checksum_after_op1);
13630 :
13631 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13632 : fold_check_failed (op1, tem);
13633 : #endif
13634 655010185 : return tem;
13635 : }
13636 :
13637 : /* Fold a ternary tree expression with code CODE of type TYPE with
13638 : operands OP0, OP1, and OP2. Return a folded expression if
13639 : successful. Otherwise, return a tree expression with code CODE of
13640 : type TYPE with operands OP0, OP1, and OP2. */
13641 :
13642 : tree
13643 32832381 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13644 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13645 : {
13646 32832381 : tree tem;
13647 : #ifdef ENABLE_FOLD_CHECKING
13648 : unsigned char checksum_before_op0[16],
13649 : checksum_before_op1[16],
13650 : checksum_before_op2[16],
13651 : checksum_after_op0[16],
13652 : checksum_after_op1[16],
13653 : checksum_after_op2[16];
13654 : struct md5_ctx ctx;
13655 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13656 :
13657 : md5_init_ctx (&ctx);
13658 : fold_checksum_tree (op0, &ctx, &ht);
13659 : md5_finish_ctx (&ctx, checksum_before_op0);
13660 : ht.empty ();
13661 :
13662 : md5_init_ctx (&ctx);
13663 : fold_checksum_tree (op1, &ctx, &ht);
13664 : md5_finish_ctx (&ctx, checksum_before_op1);
13665 : ht.empty ();
13666 :
13667 : md5_init_ctx (&ctx);
13668 : fold_checksum_tree (op2, &ctx, &ht);
13669 : md5_finish_ctx (&ctx, checksum_before_op2);
13670 : ht.empty ();
13671 : #endif
13672 :
13673 32832381 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13674 32832381 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13675 32832381 : if (!tem)
13676 30083304 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13677 :
13678 : #ifdef ENABLE_FOLD_CHECKING
13679 : md5_init_ctx (&ctx);
13680 : fold_checksum_tree (op0, &ctx, &ht);
13681 : md5_finish_ctx (&ctx, checksum_after_op0);
13682 : ht.empty ();
13683 :
13684 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13685 : fold_check_failed (op0, tem);
13686 :
13687 : md5_init_ctx (&ctx);
13688 : fold_checksum_tree (op1, &ctx, &ht);
13689 : md5_finish_ctx (&ctx, checksum_after_op1);
13690 : ht.empty ();
13691 :
13692 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13693 : fold_check_failed (op1, tem);
13694 :
13695 : md5_init_ctx (&ctx);
13696 : fold_checksum_tree (op2, &ctx, &ht);
13697 : md5_finish_ctx (&ctx, checksum_after_op2);
13698 :
13699 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13700 : fold_check_failed (op2, tem);
13701 : #endif
13702 32832381 : return tem;
13703 : }
13704 :
13705 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13706 : arguments in ARGARRAY, and a null static chain.
13707 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13708 : of type TYPE from the given operands as constructed by build_call_array. */
13709 :
13710 : tree
13711 54785112 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13712 : int nargs, tree *argarray)
13713 : {
13714 54785112 : tree tem;
13715 : #ifdef ENABLE_FOLD_CHECKING
13716 : unsigned char checksum_before_fn[16],
13717 : checksum_before_arglist[16],
13718 : checksum_after_fn[16],
13719 : checksum_after_arglist[16];
13720 : struct md5_ctx ctx;
13721 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13722 : int i;
13723 :
13724 : md5_init_ctx (&ctx);
13725 : fold_checksum_tree (fn, &ctx, &ht);
13726 : md5_finish_ctx (&ctx, checksum_before_fn);
13727 : ht.empty ();
13728 :
13729 : md5_init_ctx (&ctx);
13730 : for (i = 0; i < nargs; i++)
13731 : fold_checksum_tree (argarray[i], &ctx, &ht);
13732 : md5_finish_ctx (&ctx, checksum_before_arglist);
13733 : ht.empty ();
13734 : #endif
13735 :
13736 54785112 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13737 54785112 : if (!tem)
13738 53468297 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13739 :
13740 : #ifdef ENABLE_FOLD_CHECKING
13741 : md5_init_ctx (&ctx);
13742 : fold_checksum_tree (fn, &ctx, &ht);
13743 : md5_finish_ctx (&ctx, checksum_after_fn);
13744 : ht.empty ();
13745 :
13746 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13747 : fold_check_failed (fn, tem);
13748 :
13749 : md5_init_ctx (&ctx);
13750 : for (i = 0; i < nargs; i++)
13751 : fold_checksum_tree (argarray[i], &ctx, &ht);
13752 : md5_finish_ctx (&ctx, checksum_after_arglist);
13753 :
13754 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13755 : fold_check_failed (NULL_TREE, tem);
13756 : #endif
13757 54785112 : return tem;
13758 : }
13759 :
13760 : /* Perform constant folding and related simplification of initializer
13761 : expression EXPR. These behave identically to "fold_buildN" but ignore
13762 : potential run-time traps and exceptions that fold must preserve. */
13763 :
13764 : #define START_FOLD_INIT \
13765 : int saved_signaling_nans = flag_signaling_nans;\
13766 : int saved_trapping_math = flag_trapping_math;\
13767 : int saved_rounding_math = flag_rounding_math;\
13768 : int saved_trapv = flag_trapv;\
13769 : int saved_folding_initializer = folding_initializer;\
13770 : flag_signaling_nans = 0;\
13771 : flag_trapping_math = 0;\
13772 : flag_rounding_math = 0;\
13773 : flag_trapv = 0;\
13774 : folding_initializer = 1;
13775 :
13776 : #define END_FOLD_INIT \
13777 : flag_signaling_nans = saved_signaling_nans;\
13778 : flag_trapping_math = saved_trapping_math;\
13779 : flag_rounding_math = saved_rounding_math;\
13780 : flag_trapv = saved_trapv;\
13781 : folding_initializer = saved_folding_initializer;
13782 :
13783 : tree
13784 544977 : fold_init (tree expr)
13785 : {
13786 544977 : tree result;
13787 544977 : START_FOLD_INIT;
13788 :
13789 544977 : result = fold (expr);
13790 :
13791 544977 : END_FOLD_INIT;
13792 544977 : return result;
13793 : }
13794 :
13795 : tree
13796 2988737 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13797 : tree type, tree op)
13798 : {
13799 2988737 : tree result;
13800 2988737 : START_FOLD_INIT;
13801 :
13802 2988737 : result = fold_build1_loc (loc, code, type, op);
13803 :
13804 2988737 : END_FOLD_INIT;
13805 2988737 : return result;
13806 : }
13807 :
13808 : tree
13809 50374 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13810 : tree type, tree op0, tree op1)
13811 : {
13812 50374 : tree result;
13813 50374 : START_FOLD_INIT;
13814 :
13815 50374 : result = fold_build2_loc (loc, code, type, op0, op1);
13816 :
13817 50374 : END_FOLD_INIT;
13818 50374 : return result;
13819 : }
13820 :
13821 : tree
13822 3463 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13823 : int nargs, tree *argarray)
13824 : {
13825 3463 : tree result;
13826 3463 : START_FOLD_INIT;
13827 :
13828 3463 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13829 :
13830 3463 : END_FOLD_INIT;
13831 3463 : return result;
13832 : }
13833 :
13834 : tree
13835 56478676 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
13836 : tree lhs, tree rhs)
13837 : {
13838 56478676 : tree result;
13839 56478676 : START_FOLD_INIT;
13840 :
13841 56478676 : result = fold_binary_loc (loc, code, type, lhs, rhs);
13842 :
13843 56478676 : END_FOLD_INIT;
13844 56478676 : return result;
13845 : }
13846 :
13847 : #undef START_FOLD_INIT
13848 : #undef END_FOLD_INIT
13849 :
13850 : /* Determine if first argument is a multiple of second argument. Return
13851 : false if it is not, or we cannot easily determined it to be.
13852 :
13853 : An example of the sort of thing we care about (at this point; this routine
13854 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
13855 : fold cases do now) is discovering that
13856 :
13857 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13858 :
13859 : is a multiple of
13860 :
13861 : SAVE_EXPR (J * 8)
13862 :
13863 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13864 :
13865 : This code also handles discovering that
13866 :
13867 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13868 :
13869 : is a multiple of 8 so we don't have to worry about dealing with a
13870 : possible remainder.
13871 :
13872 : Note that we *look* inside a SAVE_EXPR only to determine how it was
13873 : calculated; it is not safe for fold to do much of anything else with the
13874 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13875 : at run time. For example, the latter example above *cannot* be implemented
13876 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13877 : evaluation time of the original SAVE_EXPR is not necessarily the same at
13878 : the time the new expression is evaluated. The only optimization of this
13879 : sort that would be valid is changing
13880 :
13881 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13882 :
13883 : divided by 8 to
13884 :
13885 : SAVE_EXPR (I) * SAVE_EXPR (J)
13886 :
13887 : (where the same SAVE_EXPR (J) is used in the original and the
13888 : transformed version).
13889 :
13890 : NOWRAP specifies whether all outer operations in TYPE should
13891 : be considered not wrapping. Any type conversion within TOP acts
13892 : as a barrier and we will fall back to NOWRAP being false.
13893 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
13894 : as not wrapping even though they are generally using unsigned arithmetic. */
13895 :
13896 : bool
13897 1592564 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
13898 : {
13899 1592564 : gimple *stmt;
13900 1592564 : tree op1, op2;
13901 :
13902 1592564 : if (operand_equal_p (top, bottom, 0))
13903 : return true;
13904 :
13905 1107698 : if (TREE_CODE (type) != INTEGER_TYPE)
13906 : return false;
13907 :
13908 1107679 : switch (TREE_CODE (top))
13909 : {
13910 702 : case BIT_AND_EXPR:
13911 : /* Bitwise and provides a power of two multiple. If the mask is
13912 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13913 702 : if (!integer_pow2p (bottom))
13914 : return false;
13915 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
13916 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
13917 :
13918 390086 : case MULT_EXPR:
13919 : /* If the multiplication can wrap we cannot recurse further unless
13920 : the bottom is a power of two which is where wrapping does not
13921 : matter. */
13922 390086 : if (!nowrap
13923 15847 : && !TYPE_OVERFLOW_UNDEFINED (type)
13924 395489 : && !integer_pow2p (bottom))
13925 : return false;
13926 389524 : if (TREE_CODE (bottom) == INTEGER_CST)
13927 : {
13928 387828 : op1 = TREE_OPERAND (top, 0);
13929 387828 : op2 = TREE_OPERAND (top, 1);
13930 387828 : if (TREE_CODE (op1) == INTEGER_CST)
13931 0 : std::swap (op1, op2);
13932 387828 : if (TREE_CODE (op2) == INTEGER_CST)
13933 : {
13934 377814 : if (multiple_of_p (type, op2, bottom, nowrap))
13935 : return true;
13936 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
13937 3283 : if (multiple_of_p (type, bottom, op2, nowrap))
13938 : {
13939 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
13940 1888 : wi::to_widest (op2));
13941 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
13942 : {
13943 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
13944 1888 : return multiple_of_p (type, op1, op2, nowrap);
13945 : }
13946 1888 : }
13947 1395 : return multiple_of_p (type, op1, bottom, nowrap);
13948 : }
13949 : }
13950 11710 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
13951 11710 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
13952 :
13953 403 : case LSHIFT_EXPR:
13954 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
13955 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13956 : {
13957 403 : op1 = TREE_OPERAND (top, 1);
13958 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
13959 : {
13960 403 : wide_int mul_op
13961 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
13962 806 : return multiple_of_p (type,
13963 806 : wide_int_to_tree (type, mul_op), bottom,
13964 : nowrap);
13965 403 : }
13966 : }
13967 : return false;
13968 :
13969 234914 : case MINUS_EXPR:
13970 234914 : case PLUS_EXPR:
13971 : /* If the addition or subtraction can wrap we cannot recurse further
13972 : unless bottom is a power of two which is where wrapping does not
13973 : matter. */
13974 234914 : if (!nowrap
13975 171173 : && !TYPE_OVERFLOW_UNDEFINED (type)
13976 404653 : && !integer_pow2p (bottom))
13977 : return false;
13978 :
13979 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
13980 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
13981 : but 0xfffffffd is not. */
13982 207831 : op1 = TREE_OPERAND (top, 1);
13983 207831 : if (TREE_CODE (top) == PLUS_EXPR
13984 200412 : && nowrap
13985 56409 : && TYPE_UNSIGNED (type)
13986 263530 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
13987 41544 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
13988 :
13989 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
13990 : precisely, so be conservative here checking if both op0 and op1
13991 : are multiple of bottom. Note we check the second operand first
13992 : since it's usually simpler. */
13993 207831 : return (multiple_of_p (type, op1, bottom, nowrap)
13994 207831 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
13995 :
13996 145747 : CASE_CONVERT:
13997 : /* Can't handle conversions from non-integral or wider integral type. */
13998 145747 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13999 145747 : || (TYPE_PRECISION (type)
14000 41080 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14001 : return false;
14002 : /* NOWRAP only extends to operations in the outermost type so
14003 : make sure to strip it off here. */
14004 40822 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14005 81644 : TREE_OPERAND (top, 0), bottom, false);
14006 :
14007 12816 : case SAVE_EXPR:
14008 12816 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14009 :
14010 86 : case COND_EXPR:
14011 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14012 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14013 :
14014 150331 : case INTEGER_CST:
14015 150331 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14016 2702 : return false;
14017 147629 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14018 : SIGNED);
14019 :
14020 63996 : case SSA_NAME:
14021 63996 : if (TREE_CODE (bottom) == INTEGER_CST
14022 60813 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14023 124809 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14024 : {
14025 26180 : enum tree_code code = gimple_assign_rhs_code (stmt);
14026 :
14027 : /* Check for special cases to see if top is defined as multiple
14028 : of bottom:
14029 :
14030 : top = (X & ~(bottom - 1) ; bottom is power of 2
14031 :
14032 : or
14033 :
14034 : Y = X % bottom
14035 : top = X - Y. */
14036 26180 : if (code == BIT_AND_EXPR
14037 298 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14038 298 : && TREE_CODE (op2) == INTEGER_CST
14039 190 : && integer_pow2p (bottom)
14040 26370 : && wi::multiple_of_p (wi::to_widest (op2),
14041 190 : wi::to_widest (bottom), SIGNED))
14042 181 : return true;
14043 :
14044 25999 : op1 = gimple_assign_rhs1 (stmt);
14045 25999 : if (code == MINUS_EXPR
14046 1527 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14047 1527 : && TREE_CODE (op2) == SSA_NAME
14048 1527 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14049 1527 : && gimple_code (stmt) == GIMPLE_ASSIGN
14050 1171 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14051 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14052 26063 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14053 : return true;
14054 : }
14055 :
14056 : /* fall through */
14057 :
14058 : default:
14059 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14060 : return multiple_p (wi::to_poly_widest (top),
14061 : wi::to_poly_widest (bottom));
14062 :
14063 : return false;
14064 : }
14065 : }
14066 :
14067 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14068 : This function returns true for integer expressions, and returns
14069 : false if uncertain. */
14070 :
14071 : bool
14072 350805 : tree_expr_finite_p (const_tree x)
14073 : {
14074 350809 : machine_mode mode = element_mode (x);
14075 350809 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14076 : return true;
14077 350615 : switch (TREE_CODE (x))
14078 : {
14079 592 : case REAL_CST:
14080 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14081 0 : case COMPLEX_CST:
14082 0 : return tree_expr_finite_p (TREE_REALPART (x))
14083 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14084 : case FLOAT_EXPR:
14085 : return true;
14086 4 : case ABS_EXPR:
14087 4 : case CONVERT_EXPR:
14088 4 : case NON_LVALUE_EXPR:
14089 4 : case NEGATE_EXPR:
14090 4 : case SAVE_EXPR:
14091 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14092 0 : case MIN_EXPR:
14093 0 : case MAX_EXPR:
14094 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14095 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14096 0 : case COND_EXPR:
14097 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14098 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14099 38 : case CALL_EXPR:
14100 38 : switch (get_call_combined_fn (x))
14101 : {
14102 0 : CASE_CFN_FABS:
14103 0 : CASE_CFN_FABS_FN:
14104 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14105 0 : CASE_CFN_FMAX:
14106 0 : CASE_CFN_FMAX_FN:
14107 0 : CASE_CFN_FMIN:
14108 0 : CASE_CFN_FMIN_FN:
14109 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14110 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14111 : default:
14112 : return false;
14113 : }
14114 :
14115 : default:
14116 : return false;
14117 : }
14118 : }
14119 :
14120 : /* Return true if expression X evaluates to an infinity.
14121 : This function returns false for integer expressions. */
14122 :
14123 : bool
14124 562569 : tree_expr_infinite_p (const_tree x)
14125 : {
14126 563019 : if (!HONOR_INFINITIES (x))
14127 : return false;
14128 562904 : switch (TREE_CODE (x))
14129 : {
14130 0 : case REAL_CST:
14131 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14132 450 : case ABS_EXPR:
14133 450 : case NEGATE_EXPR:
14134 450 : case NON_LVALUE_EXPR:
14135 450 : case SAVE_EXPR:
14136 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14137 0 : case COND_EXPR:
14138 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14139 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14140 : default:
14141 : return false;
14142 : }
14143 : }
14144 :
14145 : /* Return true if expression X could evaluate to an infinity.
14146 : This function returns false for integer expressions, and returns
14147 : true if uncertain. */
14148 :
14149 : bool
14150 323574 : tree_expr_maybe_infinite_p (const_tree x)
14151 : {
14152 323582 : if (!HONOR_INFINITIES (x))
14153 : return false;
14154 323255 : switch (TREE_CODE (x))
14155 : {
14156 177 : case REAL_CST:
14157 177 : return real_isinf (TREE_REAL_CST_PTR (x));
14158 : case FLOAT_EXPR:
14159 : return false;
14160 8 : case ABS_EXPR:
14161 8 : case NEGATE_EXPR:
14162 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14163 1 : case COND_EXPR:
14164 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14165 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14166 : default:
14167 : return true;
14168 : }
14169 : }
14170 :
14171 : /* Return true if expression X evaluates to a signaling NaN.
14172 : This function returns false for integer expressions. */
14173 :
14174 : bool
14175 357 : tree_expr_signaling_nan_p (const_tree x)
14176 : {
14177 357 : if (!HONOR_SNANS (x))
14178 : return false;
14179 124 : switch (TREE_CODE (x))
14180 : {
14181 124 : case REAL_CST:
14182 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14183 0 : case NON_LVALUE_EXPR:
14184 0 : case SAVE_EXPR:
14185 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14186 0 : case COND_EXPR:
14187 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14188 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14189 : default:
14190 : return false;
14191 : }
14192 : }
14193 :
14194 : /* Return true if expression X could evaluate to a signaling NaN.
14195 : This function returns false for integer expressions, and returns
14196 : true if uncertain. */
14197 :
14198 : bool
14199 723046 : tree_expr_maybe_signaling_nan_p (const_tree x)
14200 : {
14201 723046 : if (!HONOR_SNANS (x))
14202 : return false;
14203 5028 : switch (TREE_CODE (x))
14204 : {
14205 1452 : case REAL_CST:
14206 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14207 : case FLOAT_EXPR:
14208 : return false;
14209 0 : case ABS_EXPR:
14210 0 : case CONVERT_EXPR:
14211 0 : case NEGATE_EXPR:
14212 0 : case NON_LVALUE_EXPR:
14213 0 : case SAVE_EXPR:
14214 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14215 0 : case MIN_EXPR:
14216 0 : case MAX_EXPR:
14217 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14218 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14219 0 : case COND_EXPR:
14220 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14221 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14222 0 : case CALL_EXPR:
14223 0 : switch (get_call_combined_fn (x))
14224 : {
14225 0 : CASE_CFN_FABS:
14226 0 : CASE_CFN_FABS_FN:
14227 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14228 0 : CASE_CFN_FMAX:
14229 0 : CASE_CFN_FMAX_FN:
14230 0 : CASE_CFN_FMIN:
14231 0 : CASE_CFN_FMIN_FN:
14232 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14233 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14234 : default:
14235 : return true;
14236 : }
14237 : default:
14238 : return true;
14239 : }
14240 : }
14241 :
14242 : /* Return true if expression X evaluates to a NaN.
14243 : This function returns false for integer expressions. */
14244 :
14245 : bool
14246 3432946 : tree_expr_nan_p (const_tree x)
14247 : {
14248 3764248 : if (!HONOR_NANS (x))
14249 : return false;
14250 3763898 : switch (TREE_CODE (x))
14251 : {
14252 3774 : case REAL_CST:
14253 3774 : return real_isnan (TREE_REAL_CST_PTR (x));
14254 331302 : case NON_LVALUE_EXPR:
14255 331302 : case SAVE_EXPR:
14256 331302 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14257 900 : case COND_EXPR:
14258 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14259 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14260 : default:
14261 : return false;
14262 : }
14263 : }
14264 :
14265 : /* Return true if expression X could evaluate to a NaN.
14266 : This function returns false for integer expressions, and returns
14267 : true if uncertain. */
14268 :
14269 : bool
14270 4158113 : tree_expr_maybe_nan_p (const_tree x)
14271 : {
14272 5705524 : if (!HONOR_NANS (x))
14273 : return false;
14274 5590535 : switch (TREE_CODE (x))
14275 : {
14276 3302 : case REAL_CST:
14277 3302 : return real_isnan (TREE_REAL_CST_PTR (x));
14278 : case FLOAT_EXPR:
14279 : return false;
14280 13846 : case PLUS_EXPR:
14281 13846 : case MINUS_EXPR:
14282 13846 : case MULT_EXPR:
14283 13846 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14284 13846 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14285 1547411 : case ABS_EXPR:
14286 1547411 : case CONVERT_EXPR:
14287 1547411 : case NEGATE_EXPR:
14288 1547411 : case NON_LVALUE_EXPR:
14289 1547411 : case SAVE_EXPR:
14290 1547411 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14291 176 : case MIN_EXPR:
14292 176 : case MAX_EXPR:
14293 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14294 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14295 557 : case COND_EXPR:
14296 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14297 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14298 1081 : case CALL_EXPR:
14299 1081 : switch (get_call_combined_fn (x))
14300 : {
14301 0 : CASE_CFN_FABS:
14302 0 : CASE_CFN_FABS_FN:
14303 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14304 108 : CASE_CFN_FMAX:
14305 108 : CASE_CFN_FMAX_FN:
14306 108 : CASE_CFN_FMIN:
14307 108 : CASE_CFN_FMIN_FN:
14308 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14309 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14310 : default:
14311 : return true;
14312 : }
14313 : default:
14314 : return true;
14315 : }
14316 : }
14317 :
14318 : /* Return true if expression X could evaluate to -0.0.
14319 : This function returns true if uncertain. */
14320 :
14321 : bool
14322 600869 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14323 : {
14324 600869 : if (!HONOR_SIGNED_ZEROS (x))
14325 : return false;
14326 600869 : switch (TREE_CODE (x))
14327 : {
14328 0 : case REAL_CST:
14329 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14330 : case INTEGER_CST:
14331 : case FLOAT_EXPR:
14332 : case ABS_EXPR:
14333 : return false;
14334 0 : case NON_LVALUE_EXPR:
14335 0 : case SAVE_EXPR:
14336 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14337 0 : case COND_EXPR:
14338 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14339 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14340 1 : case CALL_EXPR:
14341 1 : switch (get_call_combined_fn (x))
14342 : {
14343 : CASE_CFN_FABS:
14344 : CASE_CFN_FABS_FN:
14345 : return false;
14346 : default:
14347 : break;
14348 : }
14349 : default:
14350 : break;
14351 : }
14352 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14353 : * but currently those predicates require tree and not const_tree. */
14354 : return true;
14355 : }
14356 :
14357 : #define tree_expr_nonnegative_p(X, Y) \
14358 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14359 :
14360 : #define RECURSE(X) \
14361 : ((tree_expr_nonnegative_p) (X, depth + 1))
14362 :
14363 : /* Return true if CODE or TYPE is known to be non-negative. */
14364 :
14365 : static bool
14366 24428186 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14367 : {
14368 24428186 : if (!VECTOR_TYPE_P (type)
14369 24401297 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14370 48829218 : && truth_value_p (code))
14371 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14372 : have a signed:1 type (where the value is -1 and 0). */
14373 : return true;
14374 : return false;
14375 : }
14376 :
14377 : /* Return true if (CODE OP0) is known to be non-negative.
14378 : DEPTH is the current nesting depth of the query. */
14379 :
14380 : bool
14381 2394496 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14382 : {
14383 2394496 : if (TYPE_UNSIGNED (type))
14384 : return true;
14385 :
14386 1963665 : switch (code)
14387 : {
14388 273414 : case ABS_EXPR:
14389 : /* We can't return 1 if flag_wrapv is set because
14390 : ABS_EXPR<INT_MIN> = INT_MIN. */
14391 273414 : if (!ANY_INTEGRAL_TYPE_P (type))
14392 : return true;
14393 10164 : if (TYPE_OVERFLOW_UNDEFINED (type))
14394 : return true;
14395 : break;
14396 :
14397 70501 : case NON_LVALUE_EXPR:
14398 70501 : case FLOAT_EXPR:
14399 70501 : case FIX_TRUNC_EXPR:
14400 70501 : return RECURSE (op0);
14401 :
14402 1587911 : CASE_CONVERT:
14403 1587911 : {
14404 1587911 : tree inner_type = TREE_TYPE (op0);
14405 1587911 : tree outer_type = type;
14406 :
14407 1587911 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14408 : {
14409 379349 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14410 379349 : return RECURSE (op0);
14411 0 : if (INTEGRAL_TYPE_P (inner_type))
14412 : {
14413 0 : if (TYPE_UNSIGNED (inner_type))
14414 : return true;
14415 0 : return RECURSE (op0);
14416 : }
14417 : }
14418 1208562 : else if (INTEGRAL_TYPE_P (outer_type))
14419 : {
14420 1208485 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14421 0 : return RECURSE (op0);
14422 1208485 : if (INTEGRAL_TYPE_P (inner_type))
14423 1202490 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14424 1202490 : && TYPE_UNSIGNED (inner_type);
14425 : }
14426 : }
14427 : break;
14428 :
14429 31839 : default:
14430 31839 : return tree_simple_nonnegative_warnv_p (code, type);
14431 : }
14432 :
14433 : /* We don't know sign of `t', so be conservative and return false. */
14434 : return false;
14435 : }
14436 :
14437 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14438 : DEPTH is the current nesting depth of the query. */
14439 :
14440 : bool
14441 5037642 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14442 : tree op1, int depth)
14443 : {
14444 5037642 : if (TYPE_UNSIGNED (type))
14445 : return true;
14446 :
14447 4780202 : switch (code)
14448 : {
14449 1316821 : case POINTER_PLUS_EXPR:
14450 1316821 : case PLUS_EXPR:
14451 1316821 : if (FLOAT_TYPE_P (type))
14452 45284 : return RECURSE (op0) && RECURSE (op1);
14453 :
14454 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14455 : both unsigned and at least 2 bits shorter than the result. */
14456 1271537 : if (TREE_CODE (type) == INTEGER_TYPE
14457 1265543 : && TREE_CODE (op0) == NOP_EXPR
14458 16811 : && TREE_CODE (op1) == NOP_EXPR)
14459 : {
14460 200 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14461 200 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14462 200 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14463 301 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14464 : {
14465 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14466 95 : TYPE_PRECISION (inner2)) + 1;
14467 95 : return prec < TYPE_PRECISION (type);
14468 : }
14469 : }
14470 : break;
14471 :
14472 175176 : case MULT_EXPR:
14473 175176 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14474 : {
14475 : /* x * x is always non-negative for floating point x
14476 : or without overflow. */
14477 158765 : if (operand_equal_p (op0, op1, 0)
14478 158765 : || (RECURSE (op0) && RECURSE (op1)))
14479 1849 : return true;
14480 : }
14481 :
14482 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14483 : both unsigned and their total bits is shorter than the result. */
14484 173327 : if (TREE_CODE (type) == INTEGER_TYPE
14485 106732 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14486 122 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14487 : {
14488 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14489 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14490 116 : : TREE_TYPE (op0);
14491 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14492 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14493 116 : : TREE_TYPE (op1);
14494 :
14495 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14496 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14497 :
14498 116 : if (TREE_CODE (op0) == INTEGER_CST)
14499 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14500 :
14501 116 : if (TREE_CODE (op1) == INTEGER_CST)
14502 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14503 :
14504 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14505 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14506 : {
14507 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14508 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14509 0 : : TYPE_PRECISION (inner0);
14510 :
14511 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14512 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14513 0 : : TYPE_PRECISION (inner1);
14514 :
14515 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14516 : }
14517 : }
14518 : return false;
14519 :
14520 26263 : case BIT_AND_EXPR:
14521 26263 : return RECURSE (op0) || RECURSE (op1);
14522 :
14523 51581 : case MAX_EXPR:
14524 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14525 : things. */
14526 51581 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14527 76 : return RECURSE (op0) && RECURSE (op1);
14528 51505 : return RECURSE (op0) || RECURSE (op1);
14529 :
14530 167403 : case BIT_IOR_EXPR:
14531 167403 : case BIT_XOR_EXPR:
14532 167403 : case MIN_EXPR:
14533 167403 : case RDIV_EXPR:
14534 167403 : case TRUNC_DIV_EXPR:
14535 167403 : case CEIL_DIV_EXPR:
14536 167403 : case FLOOR_DIV_EXPR:
14537 167403 : case ROUND_DIV_EXPR:
14538 167403 : return RECURSE (op0) && RECURSE (op1);
14539 :
14540 85429 : case TRUNC_MOD_EXPR:
14541 85429 : return RECURSE (op0);
14542 :
14543 230 : case FLOOR_MOD_EXPR:
14544 230 : return RECURSE (op1);
14545 :
14546 2957299 : case CEIL_MOD_EXPR:
14547 2957299 : case ROUND_MOD_EXPR:
14548 2957299 : default:
14549 2957299 : return tree_simple_nonnegative_warnv_p (code, type);
14550 : }
14551 :
14552 : /* We don't know sign of `t', so be conservative and return false. */
14553 : return false;
14554 : }
14555 :
14556 : /* Return true if T is known to be non-negative.
14557 : DEPTH is the current nesting depth of the query. */
14558 :
14559 : bool
14560 22974329 : tree_single_nonnegative_p (tree t, int depth)
14561 : {
14562 22974329 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14563 : return true;
14564 :
14565 19693239 : switch (TREE_CODE (t))
14566 : {
14567 2463189 : case INTEGER_CST:
14568 2463189 : return tree_int_cst_sgn (t) >= 0;
14569 :
14570 865730 : case REAL_CST:
14571 865730 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14572 :
14573 0 : case FIXED_CST:
14574 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14575 :
14576 915 : case COND_EXPR:
14577 915 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14578 :
14579 8468649 : case SSA_NAME:
14580 : /* Limit the depth of recursion to avoid quadratic behavior.
14581 : This is expected to catch almost all occurrences in practice.
14582 : If this code misses important cases that unbounded recursion
14583 : would not, passes that need this information could be revised
14584 : to provide it through dataflow propagation. */
14585 8468649 : return (!name_registered_for_update_p (t)
14586 8468648 : && depth < param_max_ssa_name_query_depth
14587 16792511 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14588 :
14589 7894756 : default:
14590 7894756 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14591 : }
14592 : }
14593 :
14594 : /* Return true if T is known to be non-negative.
14595 : DEPTH is the current nesting depth of the query. */
14596 :
14597 : bool
14598 13636514 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14599 : int depth)
14600 : {
14601 13636514 : switch (fn)
14602 : {
14603 : CASE_CFN_ACOS:
14604 : CASE_CFN_ACOS_FN:
14605 : CASE_CFN_ACOSH:
14606 : CASE_CFN_ACOSH_FN:
14607 : CASE_CFN_ACOSPI:
14608 : CASE_CFN_ACOSPI_FN:
14609 : CASE_CFN_CABS:
14610 : CASE_CFN_CABS_FN:
14611 : CASE_CFN_COSH:
14612 : CASE_CFN_COSH_FN:
14613 : CASE_CFN_ERFC:
14614 : CASE_CFN_ERFC_FN:
14615 : CASE_CFN_EXP:
14616 : CASE_CFN_EXP_FN:
14617 : CASE_CFN_EXP10:
14618 : CASE_CFN_EXP2:
14619 : CASE_CFN_EXP2_FN:
14620 : CASE_CFN_FABS:
14621 : CASE_CFN_FABS_FN:
14622 : CASE_CFN_FDIM:
14623 : CASE_CFN_FDIM_FN:
14624 : CASE_CFN_HYPOT:
14625 : CASE_CFN_HYPOT_FN:
14626 : CASE_CFN_POW10:
14627 : CASE_CFN_FFS:
14628 : CASE_CFN_PARITY:
14629 : CASE_CFN_POPCOUNT:
14630 : CASE_CFN_CLRSB:
14631 : CASE_CFN_BSWAP:
14632 : CASE_CFN_BITREVERSE:
14633 : /* Always true. */
14634 : return true;
14635 :
14636 706 : CASE_CFN_CLZ:
14637 706 : CASE_CFN_CTZ:
14638 706 : if (arg1)
14639 0 : return RECURSE (arg1);
14640 : return true;
14641 :
14642 945 : CASE_CFN_SQRT:
14643 945 : CASE_CFN_SQRT_FN:
14644 : /* sqrt(-0.0) is -0.0. */
14645 945 : if (!HONOR_SIGNED_ZEROS (type))
14646 : return true;
14647 913 : return RECURSE (arg0);
14648 :
14649 54456 : CASE_CFN_ASINH:
14650 54456 : CASE_CFN_ASINH_FN:
14651 54456 : CASE_CFN_ASINPI:
14652 54456 : CASE_CFN_ASINPI_FN:
14653 54456 : CASE_CFN_ATAN:
14654 54456 : CASE_CFN_ATAN_FN:
14655 54456 : CASE_CFN_ATANH:
14656 54456 : CASE_CFN_ATANH_FN:
14657 54456 : CASE_CFN_ATANPI:
14658 54456 : CASE_CFN_ATANPI_FN:
14659 54456 : CASE_CFN_CBRT:
14660 54456 : CASE_CFN_CBRT_FN:
14661 54456 : CASE_CFN_CEIL:
14662 54456 : CASE_CFN_CEIL_FN:
14663 54456 : CASE_CFN_ERF:
14664 54456 : CASE_CFN_ERF_FN:
14665 54456 : CASE_CFN_EXPM1:
14666 54456 : CASE_CFN_EXPM1_FN:
14667 54456 : CASE_CFN_FLOOR:
14668 54456 : CASE_CFN_FLOOR_FN:
14669 54456 : CASE_CFN_FMOD:
14670 54456 : CASE_CFN_FMOD_FN:
14671 54456 : CASE_CFN_FREXP:
14672 54456 : CASE_CFN_FREXP_FN:
14673 54456 : CASE_CFN_ICEIL:
14674 54456 : CASE_CFN_IFLOOR:
14675 54456 : CASE_CFN_IRINT:
14676 54456 : CASE_CFN_IROUND:
14677 54456 : CASE_CFN_LCEIL:
14678 54456 : CASE_CFN_LDEXP:
14679 54456 : CASE_CFN_LFLOOR:
14680 54456 : CASE_CFN_LLCEIL:
14681 54456 : CASE_CFN_LLFLOOR:
14682 54456 : CASE_CFN_LLRINT:
14683 54456 : CASE_CFN_LLRINT_FN:
14684 54456 : CASE_CFN_LLROUND:
14685 54456 : CASE_CFN_LLROUND_FN:
14686 54456 : CASE_CFN_LRINT:
14687 54456 : CASE_CFN_LRINT_FN:
14688 54456 : CASE_CFN_LROUND:
14689 54456 : CASE_CFN_LROUND_FN:
14690 54456 : CASE_CFN_MODF:
14691 54456 : CASE_CFN_MODF_FN:
14692 54456 : CASE_CFN_NEARBYINT:
14693 54456 : CASE_CFN_NEARBYINT_FN:
14694 54456 : CASE_CFN_RINT:
14695 54456 : CASE_CFN_RINT_FN:
14696 54456 : CASE_CFN_ROUND:
14697 54456 : CASE_CFN_ROUND_FN:
14698 54456 : CASE_CFN_ROUNDEVEN:
14699 54456 : CASE_CFN_ROUNDEVEN_FN:
14700 54456 : CASE_CFN_SCALB:
14701 54456 : CASE_CFN_SCALBLN:
14702 54456 : CASE_CFN_SCALBLN_FN:
14703 54456 : CASE_CFN_SCALBN:
14704 54456 : CASE_CFN_SCALBN_FN:
14705 54456 : CASE_CFN_SIGNBIT:
14706 54456 : CASE_CFN_SIGNIFICAND:
14707 54456 : CASE_CFN_SINH:
14708 54456 : CASE_CFN_SINH_FN:
14709 54456 : CASE_CFN_TANH:
14710 54456 : CASE_CFN_TANH_FN:
14711 54456 : CASE_CFN_TRUNC:
14712 54456 : CASE_CFN_TRUNC_FN:
14713 : /* True if the 1st argument is nonnegative. */
14714 54456 : return RECURSE (arg0);
14715 :
14716 1319 : CASE_CFN_FMAX:
14717 1319 : CASE_CFN_FMAX_FN:
14718 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14719 : things. In the presence of sNaNs, we're only guaranteed to be
14720 : non-negative if both operands are non-negative. In the presence
14721 : of qNaNs, we're non-negative if either operand is non-negative
14722 : and can't be a qNaN, or if both operands are non-negative. */
14723 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14724 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14725 136 : return RECURSE (arg0) && RECURSE (arg1);
14726 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14727 332 : || RECURSE (arg1))
14728 851 : : (RECURSE (arg1)
14729 851 : && !tree_expr_maybe_nan_p (arg1));
14730 :
14731 910 : CASE_CFN_FMIN:
14732 910 : CASE_CFN_FMIN_FN:
14733 : /* True if the 1st AND 2nd arguments are nonnegative. */
14734 910 : return RECURSE (arg0) && RECURSE (arg1);
14735 :
14736 746 : CASE_CFN_COPYSIGN:
14737 746 : CASE_CFN_COPYSIGN_FN:
14738 : /* True if the 2nd argument is nonnegative. */
14739 746 : return RECURSE (arg1);
14740 :
14741 2336 : CASE_CFN_POWI:
14742 : /* True if the 1st argument is nonnegative or the second
14743 : argument is an even integer. */
14744 2336 : if (TREE_CODE (arg1) == INTEGER_CST
14745 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14746 : return true;
14747 2255 : return RECURSE (arg0);
14748 :
14749 4912 : CASE_CFN_POW:
14750 4912 : CASE_CFN_POW_FN:
14751 : /* True if the 1st argument is nonnegative or the second
14752 : argument is an even integer valued real. */
14753 4912 : if (TREE_CODE (arg1) == REAL_CST)
14754 : {
14755 2208 : REAL_VALUE_TYPE c;
14756 2208 : HOST_WIDE_INT n;
14757 :
14758 2208 : c = TREE_REAL_CST (arg1);
14759 2208 : n = real_to_integer (&c);
14760 2208 : if ((n & 1) == 0)
14761 : {
14762 1579 : REAL_VALUE_TYPE cint;
14763 1579 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14764 1579 : if (real_identical (&c, &cint))
14765 574 : return true;
14766 : }
14767 : }
14768 4338 : return RECURSE (arg0);
14769 :
14770 13541578 : default:
14771 13541578 : break;
14772 : }
14773 13541578 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14774 : }
14775 :
14776 : /* Return true if T is known to be non-negative.
14777 : DEPTH is the current nesting depth of the query. */
14778 :
14779 : static bool
14780 1647694 : tree_invalid_nonnegative_p (tree t, int depth)
14781 : {
14782 1647694 : enum tree_code code = TREE_CODE (t);
14783 1647694 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14784 : return true;
14785 :
14786 1250358 : switch (code)
14787 : {
14788 254 : case TARGET_EXPR:
14789 254 : {
14790 254 : tree temp = TARGET_EXPR_SLOT (t);
14791 254 : t = TARGET_EXPR_INITIAL (t);
14792 :
14793 : /* If the initializer is non-void, then it's a normal expression
14794 : that will be assigned to the slot. */
14795 254 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14796 52 : return RECURSE (t);
14797 :
14798 : /* Otherwise, the initializer sets the slot in some way. One common
14799 : way is an assignment statement at the end of the initializer. */
14800 404 : while (1)
14801 : {
14802 404 : if (TREE_CODE (t) == BIND_EXPR)
14803 202 : t = expr_last (BIND_EXPR_BODY (t));
14804 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14805 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
14806 0 : t = expr_last (TREE_OPERAND (t, 0));
14807 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
14808 0 : t = expr_last (t);
14809 : else
14810 : break;
14811 : }
14812 202 : if (TREE_CODE (t) == MODIFY_EXPR
14813 202 : && TREE_OPERAND (t, 0) == temp)
14814 202 : return RECURSE (TREE_OPERAND (t, 1));
14815 :
14816 : return false;
14817 : }
14818 :
14819 613960 : case CALL_EXPR:
14820 613960 : {
14821 613960 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14822 613960 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14823 :
14824 613960 : return tree_call_nonnegative_p (TREE_TYPE (t),
14825 : get_call_combined_fn (t),
14826 : arg0,
14827 : arg1,
14828 613960 : depth);
14829 : }
14830 3384 : case COMPOUND_EXPR:
14831 3384 : case MODIFY_EXPR:
14832 3384 : return RECURSE (TREE_OPERAND (t, 1));
14833 :
14834 15 : case BIND_EXPR:
14835 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
14836 :
14837 630031 : case SAVE_EXPR:
14838 630031 : return RECURSE (TREE_OPERAND (t, 0));
14839 :
14840 2714 : default:
14841 2714 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14842 : }
14843 : }
14844 :
14845 : #undef RECURSE
14846 : #undef tree_expr_nonnegative_p
14847 :
14848 : /* Return true if T is known to be non-negative.
14849 : DEPTH is the current nesting depth of the query. */
14850 :
14851 : bool
14852 24123314 : tree_expr_nonnegative_p (tree t, int depth)
14853 : {
14854 24123314 : enum tree_code code;
14855 24123314 : if (error_operand_p (t))
14856 : return false;
14857 :
14858 24123313 : code = TREE_CODE (t);
14859 24123313 : switch (TREE_CODE_CLASS (code))
14860 : {
14861 1329119 : case tcc_binary:
14862 1329119 : case tcc_comparison:
14863 1329119 : return tree_binary_nonnegative_p (TREE_CODE (t),
14864 1329119 : TREE_TYPE (t),
14865 1329119 : TREE_OPERAND (t, 0),
14866 1329119 : TREE_OPERAND (t, 1),
14867 1329119 : depth);
14868 :
14869 1891383 : case tcc_unary:
14870 1891383 : return tree_unary_nonnegative_p (TREE_CODE (t),
14871 1891383 : TREE_TYPE (t),
14872 1891383 : TREE_OPERAND (t, 0),
14873 1891383 : depth);
14874 :
14875 11590983 : case tcc_constant:
14876 11590983 : case tcc_declaration:
14877 11590983 : case tcc_reference:
14878 11590983 : return tree_single_nonnegative_p (t, depth);
14879 :
14880 9311828 : default:
14881 9311828 : break;
14882 : }
14883 :
14884 9311828 : switch (code)
14885 : {
14886 7 : case TRUTH_AND_EXPR:
14887 7 : case TRUTH_OR_EXPR:
14888 7 : case TRUTH_XOR_EXPR:
14889 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
14890 7 : TREE_TYPE (t),
14891 7 : TREE_OPERAND (t, 0),
14892 7 : TREE_OPERAND (t, 1),
14893 7 : depth);
14894 72 : case TRUTH_NOT_EXPR:
14895 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
14896 72 : TREE_TYPE (t),
14897 72 : TREE_OPERAND (t, 0),
14898 72 : depth);
14899 :
14900 7664055 : case COND_EXPR:
14901 7664055 : case CONSTRUCTOR:
14902 7664055 : case OBJ_TYPE_REF:
14903 7664055 : case ADDR_EXPR:
14904 7664055 : case WITH_SIZE_EXPR:
14905 7664055 : case SSA_NAME:
14906 7664055 : return tree_single_nonnegative_p (t, depth);
14907 :
14908 1647694 : default:
14909 1647694 : return tree_invalid_nonnegative_p (t, depth);
14910 : }
14911 : }
14912 :
14913 :
14914 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
14915 : For floating point we further ensure that T is not denormal.
14916 : Similar logic is present in nonzero_address in rtlanal.h. */
14917 :
14918 : bool
14919 1415625 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
14920 : {
14921 1415625 : switch (code)
14922 : {
14923 1 : case ABS_EXPR:
14924 1 : return tree_expr_nonzero_p (op0);
14925 :
14926 881531 : case NOP_EXPR:
14927 881531 : {
14928 881531 : tree inner_type = TREE_TYPE (op0);
14929 881531 : tree outer_type = type;
14930 :
14931 881531 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14932 881531 : && tree_expr_nonzero_p (op0));
14933 : }
14934 28113 : break;
14935 :
14936 28113 : case NON_LVALUE_EXPR:
14937 28113 : return tree_expr_nonzero_p (op0);
14938 :
14939 : default:
14940 : break;
14941 : }
14942 :
14943 : return false;
14944 : }
14945 :
14946 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14947 : For floating point we further ensure that T is not denormal.
14948 : Similar logic is present in nonzero_address in rtlanal.h. */
14949 :
14950 : bool
14951 2918977 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
14952 : {
14953 2918977 : switch (code)
14954 : {
14955 473929 : case POINTER_PLUS_EXPR:
14956 473929 : case PLUS_EXPR:
14957 473929 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
14958 : {
14959 : /* With the presence of negative values it is hard
14960 : to say something. */
14961 106491 : if (!tree_expr_nonnegative_p (op0)
14962 106491 : || !tree_expr_nonnegative_p (op1))
14963 104044 : return false;
14964 : /* One of operands must be positive and the other non-negative. */
14965 2447 : return (tree_expr_nonzero_p (op0)
14966 2447 : || tree_expr_nonzero_p (op1));
14967 : }
14968 : break;
14969 :
14970 19007 : case MULT_EXPR:
14971 19007 : if (TYPE_OVERFLOW_UNDEFINED (type))
14972 : {
14973 534 : if (tree_expr_nonzero_p (op0)
14974 534 : && tree_expr_nonzero_p (op1))
14975 : return true;
14976 : }
14977 : break;
14978 :
14979 : case MIN_EXPR:
14980 : break;
14981 :
14982 44 : case MAX_EXPR:
14983 44 : if (tree_expr_nonzero_p (op0))
14984 : {
14985 :
14986 : /* When both operands are nonzero, then MAX must be too. */
14987 0 : if (tree_expr_nonzero_p (op1))
14988 : return true;
14989 :
14990 : /* MAX where operand 0 is positive is positive. */
14991 0 : return tree_expr_nonnegative_p (op0);
14992 : }
14993 : /* MAX where operand 1 is positive is positive. */
14994 44 : else if (tree_expr_nonzero_p (op1)
14995 44 : && tree_expr_nonnegative_p (op1))
14996 : return true;
14997 : break;
14998 :
14999 263038 : case BIT_IOR_EXPR:
15000 263038 : return (tree_expr_nonzero_p (op1)
15001 263038 : || tree_expr_nonzero_p (op0));
15002 :
15003 : default:
15004 : break;
15005 : }
15006 :
15007 : return false;
15008 : }
15009 :
15010 : /* Return true when T is an address and is known to be nonzero.
15011 : For floating point we further ensure that T is not denormal.
15012 : Similar logic is present in nonzero_address in rtlanal.h. */
15013 :
15014 : bool
15015 148108121 : tree_single_nonzero_p (tree t)
15016 : {
15017 148108121 : switch (TREE_CODE (t))
15018 : {
15019 1141948 : case INTEGER_CST:
15020 1141948 : return !integer_zerop (t);
15021 :
15022 11387147 : case ADDR_EXPR:
15023 11387147 : {
15024 11387147 : tree base = TREE_OPERAND (t, 0);
15025 :
15026 11387147 : if (!DECL_P (base))
15027 5657660 : base = get_base_address (base);
15028 :
15029 11387147 : if (base && TREE_CODE (base) == TARGET_EXPR)
15030 779 : base = TARGET_EXPR_SLOT (base);
15031 :
15032 779 : if (!base)
15033 0 : return false;
15034 :
15035 : /* For objects in symbol table check if we know they are non-zero.
15036 : Don't do anything for variables and functions before symtab is built;
15037 : it is quite possible that they will be declared weak later. */
15038 11387147 : int nonzero_addr = maybe_nonzero_address (base);
15039 11387147 : if (nonzero_addr >= 0)
15040 8978104 : return nonzero_addr;
15041 :
15042 : /* Constants are never weak. */
15043 2409043 : if (CONSTANT_CLASS_P (base))
15044 : return true;
15045 :
15046 : return false;
15047 : }
15048 :
15049 36871 : case COND_EXPR:
15050 36871 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15051 36871 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15052 : return true;
15053 : break;
15054 :
15055 123997807 : case SSA_NAME:
15056 123997807 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15057 : break;
15058 96364355 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15059 :
15060 : default:
15061 : break;
15062 : }
15063 : return false;
15064 : }
15065 :
15066 : #define integer_valued_real_p(X) \
15067 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15068 :
15069 : #define RECURSE(X) \
15070 : ((integer_valued_real_p) (X, depth + 1))
15071 :
15072 : /* Return true if the floating point result of (CODE OP0) has an
15073 : integer value. We also allow +Inf, -Inf and NaN to be considered
15074 : integer values. Return false for signaling NaN.
15075 :
15076 : DEPTH is the current nesting depth of the query. */
15077 :
15078 : bool
15079 15024 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15080 : {
15081 15024 : switch (code)
15082 : {
15083 : case FLOAT_EXPR:
15084 : return true;
15085 :
15086 1403 : case ABS_EXPR:
15087 1403 : return RECURSE (op0);
15088 :
15089 9841 : CASE_CONVERT:
15090 9841 : {
15091 9841 : tree type = TREE_TYPE (op0);
15092 9841 : if (TREE_CODE (type) == INTEGER_TYPE)
15093 : return true;
15094 9841 : if (SCALAR_FLOAT_TYPE_P (type))
15095 9841 : return RECURSE (op0);
15096 : break;
15097 : }
15098 :
15099 : default:
15100 : break;
15101 : }
15102 : return false;
15103 : }
15104 :
15105 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15106 : integer value. We also allow +Inf, -Inf and NaN to be considered
15107 : integer values. Return false for signaling NaN.
15108 :
15109 : DEPTH is the current nesting depth of the query. */
15110 :
15111 : bool
15112 13267 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15113 : {
15114 13267 : switch (code)
15115 : {
15116 7586 : case PLUS_EXPR:
15117 7586 : case MINUS_EXPR:
15118 7586 : case MULT_EXPR:
15119 7586 : case MIN_EXPR:
15120 7586 : case MAX_EXPR:
15121 7586 : return RECURSE (op0) && RECURSE (op1);
15122 :
15123 : default:
15124 : break;
15125 : }
15126 : return false;
15127 : }
15128 :
15129 : /* Return true if the floating point result of calling FNDECL with arguments
15130 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15131 : considered integer values. Return false for signaling NaN. If FNDECL
15132 : takes fewer than 2 arguments, the remaining ARGn are null.
15133 :
15134 : DEPTH is the current nesting depth of the query. */
15135 :
15136 : bool
15137 1089 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15138 : {
15139 1089 : switch (fn)
15140 : {
15141 : CASE_CFN_CEIL:
15142 : CASE_CFN_CEIL_FN:
15143 : CASE_CFN_FLOOR:
15144 : CASE_CFN_FLOOR_FN:
15145 : CASE_CFN_NEARBYINT:
15146 : CASE_CFN_NEARBYINT_FN:
15147 : CASE_CFN_RINT:
15148 : CASE_CFN_RINT_FN:
15149 : CASE_CFN_ROUND:
15150 : CASE_CFN_ROUND_FN:
15151 : CASE_CFN_ROUNDEVEN:
15152 : CASE_CFN_ROUNDEVEN_FN:
15153 : CASE_CFN_TRUNC:
15154 : CASE_CFN_TRUNC_FN:
15155 : return true;
15156 :
15157 336 : CASE_CFN_FMIN:
15158 336 : CASE_CFN_FMIN_FN:
15159 336 : CASE_CFN_FMAX:
15160 336 : CASE_CFN_FMAX_FN:
15161 336 : return RECURSE (arg0) && RECURSE (arg1);
15162 :
15163 : default:
15164 : break;
15165 : }
15166 : return false;
15167 : }
15168 :
15169 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15170 : has an integer value. We also allow +Inf, -Inf and NaN to be
15171 : considered integer values. Return false for signaling NaN.
15172 :
15173 : DEPTH is the current nesting depth of the query. */
15174 :
15175 : bool
15176 127015 : integer_valued_real_single_p (tree t, int depth)
15177 : {
15178 127015 : switch (TREE_CODE (t))
15179 : {
15180 2271 : case REAL_CST:
15181 2271 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15182 :
15183 0 : case COND_EXPR:
15184 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15185 :
15186 89312 : case SSA_NAME:
15187 : /* Limit the depth of recursion to avoid quadratic behavior.
15188 : This is expected to catch almost all occurrences in practice.
15189 : If this code misses important cases that unbounded recursion
15190 : would not, passes that need this information could be revised
15191 : to provide it through dataflow propagation. */
15192 89312 : return (!name_registered_for_update_p (t)
15193 89312 : && depth < param_max_ssa_name_query_depth
15194 177828 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15195 : depth));
15196 :
15197 : default:
15198 : break;
15199 : }
15200 : return false;
15201 : }
15202 :
15203 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15204 : has an integer value. We also allow +Inf, -Inf and NaN to be
15205 : considered integer values. Return false for signaling NaN.
15206 :
15207 : DEPTH is the current nesting depth of the query. */
15208 :
15209 : static bool
15210 0 : integer_valued_real_invalid_p (tree t, int depth)
15211 : {
15212 0 : switch (TREE_CODE (t))
15213 : {
15214 0 : case COMPOUND_EXPR:
15215 0 : case MODIFY_EXPR:
15216 0 : case BIND_EXPR:
15217 0 : return RECURSE (TREE_OPERAND (t, 1));
15218 :
15219 0 : case SAVE_EXPR:
15220 0 : return RECURSE (TREE_OPERAND (t, 0));
15221 :
15222 : default:
15223 : break;
15224 : }
15225 : return false;
15226 : }
15227 :
15228 : #undef RECURSE
15229 : #undef integer_valued_real_p
15230 :
15231 : /* Return true if the floating point expression T has an integer value.
15232 : We also allow +Inf, -Inf and NaN to be considered integer values.
15233 : Return false for signaling NaN.
15234 :
15235 : DEPTH is the current nesting depth of the query. */
15236 :
15237 : bool
15238 96257 : integer_valued_real_p (tree t, int depth)
15239 : {
15240 96257 : if (t == error_mark_node)
15241 : return false;
15242 :
15243 96257 : STRIP_ANY_LOCATION_WRAPPER (t);
15244 :
15245 96257 : tree_code code = TREE_CODE (t);
15246 96257 : switch (TREE_CODE_CLASS (code))
15247 : {
15248 0 : case tcc_binary:
15249 0 : case tcc_comparison:
15250 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15251 0 : TREE_OPERAND (t, 1), depth);
15252 :
15253 0 : case tcc_unary:
15254 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15255 :
15256 8446 : case tcc_constant:
15257 8446 : case tcc_declaration:
15258 8446 : case tcc_reference:
15259 8446 : return integer_valued_real_single_p (t, depth);
15260 :
15261 87811 : default:
15262 87811 : break;
15263 : }
15264 :
15265 87811 : switch (code)
15266 : {
15267 87811 : case COND_EXPR:
15268 87811 : case SSA_NAME:
15269 87811 : return integer_valued_real_single_p (t, depth);
15270 :
15271 0 : case CALL_EXPR:
15272 0 : {
15273 0 : tree arg0 = (call_expr_nargs (t) > 0
15274 0 : ? CALL_EXPR_ARG (t, 0)
15275 0 : : NULL_TREE);
15276 0 : tree arg1 = (call_expr_nargs (t) > 1
15277 0 : ? CALL_EXPR_ARG (t, 1)
15278 0 : : NULL_TREE);
15279 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15280 0 : arg0, arg1, depth);
15281 : }
15282 :
15283 0 : default:
15284 0 : return integer_valued_real_invalid_p (t, depth);
15285 : }
15286 : }
15287 :
15288 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15289 : attempt to fold the expression to a constant without modifying TYPE,
15290 : OP0 or OP1.
15291 :
15292 : If the expression could be simplified to a constant, then return
15293 : the constant. If the expression would not be simplified to a
15294 : constant, then return NULL_TREE. */
15295 :
15296 : tree
15297 15692654 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15298 : {
15299 15692654 : tree tem = fold_binary (code, type, op0, op1);
15300 15692654 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15301 : }
15302 :
15303 : /* Given the components of a unary expression CODE, TYPE and OP0,
15304 : attempt to fold the expression to a constant without modifying
15305 : TYPE or OP0.
15306 :
15307 : If the expression could be simplified to a constant, then return
15308 : the constant. If the expression would not be simplified to a
15309 : constant, then return NULL_TREE. */
15310 :
15311 : tree
15312 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15313 : {
15314 0 : tree tem = fold_unary (code, type, op0);
15315 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15316 : }
15317 :
15318 : /* If EXP represents referencing an element in a constant string
15319 : (either via pointer arithmetic or array indexing), return the
15320 : tree representing the value accessed, otherwise return NULL. */
15321 :
15322 : tree
15323 186341632 : fold_read_from_constant_string (tree exp)
15324 : {
15325 186341632 : if ((INDIRECT_REF_P (exp)
15326 186341613 : || TREE_CODE (exp) == ARRAY_REF)
15327 198657238 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15328 : {
15329 9145353 : tree exp1 = TREE_OPERAND (exp, 0);
15330 9145353 : tree index;
15331 9145353 : tree string;
15332 9145353 : location_t loc = EXPR_LOCATION (exp);
15333 :
15334 9145353 : if (INDIRECT_REF_P (exp))
15335 0 : string = string_constant (exp1, &index, NULL, NULL);
15336 : else
15337 : {
15338 9145353 : tree low_bound = array_ref_low_bound (exp);
15339 9145353 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15340 :
15341 : /* Optimize the special-case of a zero lower bound.
15342 :
15343 : We convert the low_bound to sizetype to avoid some problems
15344 : with constant folding. (E.g. suppose the lower bound is 1,
15345 : and its mode is QI. Without the conversion,l (ARRAY
15346 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15347 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15348 9145353 : if (! integer_zerop (low_bound))
15349 153572 : index = size_diffop_loc (loc, index,
15350 : fold_convert_loc (loc, sizetype, low_bound));
15351 :
15352 : string = exp1;
15353 : }
15354 :
15355 9145353 : scalar_int_mode char_mode;
15356 9145353 : if (string
15357 9145353 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15358 9145353 : && TREE_CODE (string) == STRING_CST
15359 246809 : && tree_fits_uhwi_p (index)
15360 242889 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15361 9388031 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15362 : &char_mode)
15363 18290706 : && GET_MODE_SIZE (char_mode) == 1)
15364 482940 : return build_int_cst_type (TREE_TYPE (exp),
15365 241470 : (TREE_STRING_POINTER (string)
15366 241470 : [TREE_INT_CST_LOW (index)]));
15367 : }
15368 : return NULL;
15369 : }
15370 :
15371 : /* Folds a read from vector element at IDX of vector ARG. */
15372 :
15373 : tree
15374 6086 : fold_read_from_vector (tree arg, poly_uint64 idx)
15375 : {
15376 6086 : unsigned HOST_WIDE_INT i;
15377 6086 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15378 6086 : && known_ge (idx, 0u)
15379 6086 : && idx.is_constant (&i))
15380 : {
15381 6086 : if (TREE_CODE (arg) == VECTOR_CST)
15382 1963 : return VECTOR_CST_ELT (arg, i);
15383 4123 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15384 : {
15385 2069 : if (CONSTRUCTOR_NELTS (arg)
15386 2029 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15387 : return NULL_TREE;
15388 1733 : if (i >= CONSTRUCTOR_NELTS (arg))
15389 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15390 1693 : return CONSTRUCTOR_ELT (arg, i)->value;
15391 : }
15392 : }
15393 : return NULL_TREE;
15394 : }
15395 :
15396 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15397 : an integer constant, real, or fixed-point constant.
15398 :
15399 : TYPE is the type of the result. */
15400 :
15401 : static tree
15402 32439943 : fold_negate_const (tree arg0, tree type)
15403 : {
15404 32439943 : tree t = NULL_TREE;
15405 :
15406 32439943 : switch (TREE_CODE (arg0))
15407 : {
15408 2006728 : case REAL_CST:
15409 2006728 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15410 2006728 : break;
15411 :
15412 0 : case FIXED_CST:
15413 0 : {
15414 0 : FIXED_VALUE_TYPE f;
15415 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15416 0 : &(TREE_FIXED_CST (arg0)), NULL,
15417 0 : TYPE_SATURATING (type));
15418 0 : t = build_fixed (type, f);
15419 : /* Propagate overflow flags. */
15420 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15421 0 : TREE_OVERFLOW (t) = 1;
15422 0 : break;
15423 : }
15424 :
15425 30433215 : default:
15426 30433215 : if (poly_int_tree_p (arg0))
15427 : {
15428 30433215 : wi::overflow_type overflow;
15429 30433215 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15430 30433215 : t = force_fit_type (type, res, 1,
15431 231972 : (overflow && ! TYPE_UNSIGNED (type))
15432 30652481 : || TREE_OVERFLOW (arg0));
15433 30433215 : break;
15434 30433215 : }
15435 :
15436 0 : gcc_unreachable ();
15437 : }
15438 :
15439 32439943 : return t;
15440 : }
15441 :
15442 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15443 : an integer constant or real constant.
15444 :
15445 : TYPE is the type of the result. */
15446 :
15447 : tree
15448 34832 : fold_abs_const (tree arg0, tree type)
15449 : {
15450 34832 : tree t = NULL_TREE;
15451 :
15452 34832 : switch (TREE_CODE (arg0))
15453 : {
15454 7187 : case INTEGER_CST:
15455 7187 : {
15456 : /* If the value is unsigned or non-negative, then the absolute value
15457 : is the same as the ordinary value. */
15458 7187 : wide_int val = wi::to_wide (arg0);
15459 7187 : wi::overflow_type overflow = wi::OVF_NONE;
15460 7187 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15461 : ;
15462 :
15463 : /* If the value is negative, then the absolute value is
15464 : its negation. */
15465 : else
15466 3082 : val = wi::neg (val, &overflow);
15467 :
15468 : /* Force to the destination type, set TREE_OVERFLOW for signed
15469 : TYPE only. */
15470 7187 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15471 7187 : }
15472 7187 : break;
15473 :
15474 27645 : case REAL_CST:
15475 27645 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15476 7577 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15477 : else
15478 : t = arg0;
15479 : break;
15480 :
15481 0 : default:
15482 0 : gcc_unreachable ();
15483 : }
15484 :
15485 34832 : return t;
15486 : }
15487 :
15488 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15489 : constant. TYPE is the type of the result. */
15490 :
15491 : static tree
15492 2361400 : fold_not_const (const_tree arg0, tree type)
15493 : {
15494 2361400 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15495 :
15496 2361400 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15497 : }
15498 :
15499 : /* Given CODE, a relational operator, the target type, TYPE and two
15500 : constant operands OP0 and OP1, return the result of the
15501 : relational operation. If the result is not a compile time
15502 : constant, then return NULL_TREE. */
15503 :
15504 : static tree
15505 75512319 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15506 : {
15507 75512319 : int result, invert;
15508 :
15509 : /* From here on, the only cases we handle are when the result is
15510 : known to be a constant. */
15511 :
15512 75512319 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15513 : {
15514 1172853 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15515 1172853 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15516 :
15517 : /* Handle the cases where either operand is a NaN. */
15518 1172853 : if (real_isnan (c0) || real_isnan (c1))
15519 : {
15520 13419 : switch (code)
15521 : {
15522 : case EQ_EXPR:
15523 : case ORDERED_EXPR:
15524 : result = 0;
15525 : break;
15526 :
15527 : case NE_EXPR:
15528 : case UNORDERED_EXPR:
15529 : case UNLT_EXPR:
15530 : case UNLE_EXPR:
15531 : case UNGT_EXPR:
15532 : case UNGE_EXPR:
15533 : case UNEQ_EXPR:
15534 6527 : result = 1;
15535 : break;
15536 :
15537 6911 : case LT_EXPR:
15538 6911 : case LE_EXPR:
15539 6911 : case GT_EXPR:
15540 6911 : case GE_EXPR:
15541 6911 : case LTGT_EXPR:
15542 6911 : if (flag_trapping_math)
15543 : return NULL_TREE;
15544 : result = 0;
15545 : break;
15546 :
15547 0 : default:
15548 0 : gcc_unreachable ();
15549 : }
15550 :
15551 6527 : return constant_boolean_node (result, type);
15552 : }
15553 :
15554 1159434 : return constant_boolean_node (real_compare (code, c0, c1), type);
15555 : }
15556 :
15557 74339466 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15558 : {
15559 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15560 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15561 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15562 : }
15563 :
15564 : /* Handle equality/inequality of complex constants. */
15565 74339466 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15566 : {
15567 58578 : tree rcond = fold_relational_const (code, type,
15568 29289 : TREE_REALPART (op0),
15569 29289 : TREE_REALPART (op1));
15570 117156 : tree icond = fold_relational_const (code, type,
15571 29289 : TREE_IMAGPART (op0),
15572 29289 : TREE_IMAGPART (op1));
15573 29289 : if (code == EQ_EXPR)
15574 307 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15575 28982 : else if (code == NE_EXPR)
15576 28982 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15577 : else
15578 : return NULL_TREE;
15579 : }
15580 :
15581 74310177 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15582 : {
15583 14230 : if (!VECTOR_TYPE_P (type))
15584 : {
15585 : /* Have vector comparison with scalar boolean result. */
15586 172 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15587 : && known_eq (VECTOR_CST_NELTS (op0),
15588 : VECTOR_CST_NELTS (op1)));
15589 172 : unsigned HOST_WIDE_INT nunits;
15590 172 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15591 : return NULL_TREE;
15592 479 : for (unsigned i = 0; i < nunits; i++)
15593 : {
15594 420 : tree elem0 = VECTOR_CST_ELT (op0, i);
15595 420 : tree elem1 = VECTOR_CST_ELT (op1, i);
15596 420 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15597 420 : if (tmp == NULL_TREE)
15598 : return NULL_TREE;
15599 420 : if (integer_zerop (tmp))
15600 113 : return constant_boolean_node (code == NE_EXPR, type);
15601 : }
15602 59 : return constant_boolean_node (code == EQ_EXPR, type);
15603 : }
15604 14058 : tree_vector_builder elts;
15605 14058 : if (!elts.new_binary_operation (type, op0, op1, false))
15606 : return NULL_TREE;
15607 14058 : unsigned int count = elts.encoded_nelts ();
15608 59485 : for (unsigned i = 0; i < count; i++)
15609 : {
15610 45427 : tree elem_type = TREE_TYPE (type);
15611 45427 : tree elem0 = VECTOR_CST_ELT (op0, i);
15612 45427 : tree elem1 = VECTOR_CST_ELT (op1, i);
15613 :
15614 45427 : tree tem = fold_relational_const (code, elem_type,
15615 : elem0, elem1);
15616 :
15617 45427 : if (tem == NULL_TREE)
15618 : return NULL_TREE;
15619 :
15620 45427 : elts.quick_push (build_int_cst (elem_type,
15621 70172 : integer_zerop (tem) ? 0 : -1));
15622 : }
15623 :
15624 14058 : return elts.build ();
15625 14058 : }
15626 :
15627 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15628 :
15629 : To compute GT, swap the arguments and do LT.
15630 : To compute GE, do LT and invert the result.
15631 : To compute LE, swap the arguments, do LT and invert the result.
15632 : To compute NE, do EQ and invert the result.
15633 :
15634 : Therefore, the code below must handle only EQ and LT. */
15635 :
15636 74295947 : if (code == LE_EXPR || code == GT_EXPR)
15637 : {
15638 13587560 : std::swap (op0, op1);
15639 13587560 : code = swap_tree_comparison (code);
15640 : }
15641 :
15642 : /* Note that it is safe to invert for real values here because we
15643 : have already handled the one case that it matters. */
15644 :
15645 74295947 : invert = 0;
15646 74295947 : if (code == NE_EXPR || code == GE_EXPR)
15647 : {
15648 33214351 : invert = 1;
15649 33214351 : code = invert_tree_comparison (code, false);
15650 : }
15651 :
15652 : /* Compute a result for LT or EQ if args permit;
15653 : Otherwise return T. */
15654 74295947 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15655 : {
15656 74275581 : if (code == EQ_EXPR)
15657 37692709 : result = tree_int_cst_equal (op0, op1);
15658 : else
15659 36582872 : result = tree_int_cst_lt (op0, op1);
15660 : }
15661 : else
15662 : return NULL_TREE;
15663 :
15664 74275581 : if (invert)
15665 33212513 : result ^= 1;
15666 74275581 : return constant_boolean_node (result, type);
15667 : }
15668 :
15669 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15670 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15671 : itself. */
15672 :
15673 : tree
15674 128812290 : fold_build_cleanup_point_expr (tree type, tree expr)
15675 : {
15676 : /* If the expression does not have side effects then we don't have to wrap
15677 : it with a cleanup point expression. */
15678 128812290 : if (!TREE_SIDE_EFFECTS (expr))
15679 : return expr;
15680 :
15681 : /* If the expression is a return, check to see if the expression inside the
15682 : return has no side effects or the right hand side of the modify expression
15683 : inside the return. If either don't have side effects set we don't need to
15684 : wrap the expression in a cleanup point expression. Note we don't check the
15685 : left hand side of the modify because it should always be a return decl. */
15686 110438716 : if (TREE_CODE (expr) == RETURN_EXPR)
15687 : {
15688 43643963 : tree op = TREE_OPERAND (expr, 0);
15689 43643963 : if (!op || !TREE_SIDE_EFFECTS (op))
15690 : return expr;
15691 43005069 : op = TREE_OPERAND (op, 1);
15692 43005069 : if (!TREE_SIDE_EFFECTS (op))
15693 : return expr;
15694 : }
15695 :
15696 87177603 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15697 : }
15698 :
15699 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15700 : of an indirection through OP0, or NULL_TREE if no simplification is
15701 : possible. */
15702 :
15703 : tree
15704 21274389 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15705 : {
15706 21274389 : tree sub = op0;
15707 21274389 : tree subtype;
15708 21274389 : poly_uint64 const_op01;
15709 :
15710 21274389 : STRIP_NOPS (sub);
15711 21274389 : subtype = TREE_TYPE (sub);
15712 21274389 : if (!POINTER_TYPE_P (subtype)
15713 21274389 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15714 : return NULL_TREE;
15715 :
15716 21125378 : if (TREE_CODE (sub) == ADDR_EXPR)
15717 : {
15718 4705353 : tree op = TREE_OPERAND (sub, 0);
15719 4705353 : tree optype = TREE_TYPE (op);
15720 :
15721 : /* *&CONST_DECL -> to the value of the const decl. */
15722 4705353 : if (TREE_CODE (op) == CONST_DECL)
15723 3127 : return DECL_INITIAL (op);
15724 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15725 4702226 : if (type == optype)
15726 : {
15727 3517165 : tree fop = fold_read_from_constant_string (op);
15728 3517165 : if (fop)
15729 : return fop;
15730 : else
15731 3471890 : return op;
15732 : }
15733 : /* *(foo *)&fooarray => fooarray[0] */
15734 1185061 : else if (TREE_CODE (optype) == ARRAY_TYPE
15735 13630 : && type == TREE_TYPE (optype)
15736 1197547 : && (!in_gimple_form
15737 2912 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15738 : {
15739 12486 : tree type_domain = TYPE_DOMAIN (optype);
15740 12486 : tree min_val = size_zero_node;
15741 12486 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15742 12447 : min_val = TYPE_MIN_VALUE (type_domain);
15743 12486 : if (in_gimple_form
15744 2912 : && TREE_CODE (min_val) != INTEGER_CST)
15745 : return NULL_TREE;
15746 12486 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15747 12486 : NULL_TREE, NULL_TREE);
15748 : }
15749 : /* *(foo *)&complexfoo => __real__ complexfoo */
15750 1172575 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15751 1172575 : && type == TREE_TYPE (optype))
15752 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15753 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15754 1172575 : else if (VECTOR_TYPE_P (optype)
15755 1172575 : && type == TREE_TYPE (optype))
15756 : {
15757 70 : tree part_width = TYPE_SIZE (type);
15758 70 : tree index = bitsize_int (0);
15759 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15760 70 : index);
15761 : }
15762 : }
15763 :
15764 17592530 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15765 17592530 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15766 : {
15767 258052 : tree op00 = TREE_OPERAND (sub, 0);
15768 258052 : tree op01 = TREE_OPERAND (sub, 1);
15769 :
15770 258052 : STRIP_NOPS (op00);
15771 258052 : if (TREE_CODE (op00) == ADDR_EXPR)
15772 : {
15773 2002 : tree op00type;
15774 2002 : op00 = TREE_OPERAND (op00, 0);
15775 2002 : op00type = TREE_TYPE (op00);
15776 :
15777 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15778 2002 : if (VECTOR_TYPE_P (op00type)
15779 240 : && type == TREE_TYPE (op00type)
15780 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15781 : but we want to treat offsets with MSB set as negative.
15782 : For the code below negative offsets are invalid and
15783 : TYPE_SIZE of the element is something unsigned, so
15784 : check whether op01 fits into poly_int64, which implies
15785 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15786 : then just use poly_uint64 because we want to treat the
15787 : value as unsigned. */
15788 2195 : && tree_fits_poly_int64_p (op01))
15789 : {
15790 179 : tree part_width = TYPE_SIZE (type);
15791 179 : poly_uint64 max_offset
15792 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15793 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15794 179 : if (known_lt (const_op01, max_offset))
15795 : {
15796 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15797 179 : return fold_build3_loc (loc,
15798 : BIT_FIELD_REF, type, op00,
15799 179 : part_width, index);
15800 : }
15801 : }
15802 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15803 1823 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
15804 1823 : && type == TREE_TYPE (op00type))
15805 : {
15806 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
15807 : const_op01))
15808 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15809 : }
15810 : /* ((foo *)&fooarray)[1] => fooarray[1] */
15811 1823 : else if (TREE_CODE (op00type) == ARRAY_TYPE
15812 1823 : && type == TREE_TYPE (op00type))
15813 : {
15814 703 : tree type_domain = TYPE_DOMAIN (op00type);
15815 703 : tree min_val = size_zero_node;
15816 703 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15817 702 : min_val = TYPE_MIN_VALUE (type_domain);
15818 703 : poly_uint64 type_size, index;
15819 703 : if (poly_int_tree_p (min_val)
15820 703 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
15821 703 : && multiple_p (const_op01, type_size, &index))
15822 : {
15823 703 : poly_offset_int off = index + wi::to_poly_offset (min_val);
15824 703 : op01 = wide_int_to_tree (sizetype, off);
15825 703 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
15826 : NULL_TREE, NULL_TREE);
15827 : }
15828 : }
15829 : }
15830 : }
15831 :
15832 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15833 17591648 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15834 677136 : && type == TREE_TYPE (TREE_TYPE (subtype))
15835 17594701 : && (!in_gimple_form
15836 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15837 : {
15838 3052 : tree type_domain;
15839 3052 : tree min_val = size_zero_node;
15840 3052 : sub = build_fold_indirect_ref_loc (loc, sub);
15841 3052 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15842 3052 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15843 3052 : min_val = TYPE_MIN_VALUE (type_domain);
15844 3052 : if (in_gimple_form
15845 11 : && TREE_CODE (min_val) != INTEGER_CST)
15846 : return NULL_TREE;
15847 3052 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15848 3052 : NULL_TREE);
15849 : }
15850 :
15851 : return NULL_TREE;
15852 : }
15853 :
15854 : /* Builds an expression for an indirection through T, simplifying some
15855 : cases. */
15856 :
15857 : tree
15858 10892838 : build_fold_indirect_ref_loc (location_t loc, tree t)
15859 : {
15860 10892838 : tree type = TREE_TYPE (TREE_TYPE (t));
15861 10892838 : tree sub = fold_indirect_ref_1 (loc, type, t);
15862 :
15863 10892838 : if (sub)
15864 : return sub;
15865 :
15866 7378849 : return build1_loc (loc, INDIRECT_REF, type, t);
15867 : }
15868 :
15869 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
15870 :
15871 : tree
15872 10021574 : fold_indirect_ref_loc (location_t loc, tree t)
15873 : {
15874 10021574 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15875 :
15876 10021574 : if (sub)
15877 : return sub;
15878 : else
15879 10001203 : return t;
15880 : }
15881 :
15882 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
15883 : whose result is ignored. The type of the returned tree need not be
15884 : the same as the original expression. */
15885 :
15886 : tree
15887 133629 : fold_ignored_result (tree t)
15888 : {
15889 133629 : if (!TREE_SIDE_EFFECTS (t))
15890 17051 : return integer_zero_node;
15891 :
15892 155689 : for (;;)
15893 155689 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
15894 : {
15895 3838 : case tcc_unary:
15896 3838 : t = TREE_OPERAND (t, 0);
15897 3838 : break;
15898 :
15899 5031 : case tcc_binary:
15900 5031 : case tcc_comparison:
15901 5031 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15902 3129 : t = TREE_OPERAND (t, 0);
15903 1902 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15904 30 : t = TREE_OPERAND (t, 1);
15905 : else
15906 : return t;
15907 : break;
15908 :
15909 99702 : case tcc_expression:
15910 99702 : switch (TREE_CODE (t))
15911 : {
15912 32115 : case COMPOUND_EXPR:
15913 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15914 : return t;
15915 31820 : t = TREE_OPERAND (t, 0);
15916 31820 : break;
15917 :
15918 381 : case COND_EXPR:
15919 381 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15920 381 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15921 : return t;
15922 294 : t = TREE_OPERAND (t, 0);
15923 294 : break;
15924 :
15925 : default:
15926 : return t;
15927 : }
15928 : break;
15929 :
15930 : default:
15931 : return t;
15932 : }
15933 : }
15934 :
15935 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15936 :
15937 : tree
15938 3026049613 : round_up_loc (location_t loc, tree value, unsigned int divisor)
15939 : {
15940 3026049613 : tree div = NULL_TREE;
15941 :
15942 3026049613 : if (divisor == 1)
15943 : return value;
15944 :
15945 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15946 : have to do anything. Only do this when we are not given a const,
15947 : because in that case, this check is more expensive than just
15948 : doing it. */
15949 1883007770 : if (TREE_CODE (value) != INTEGER_CST)
15950 : {
15951 361702 : div = build_int_cst (TREE_TYPE (value), divisor);
15952 :
15953 361702 : if (multiple_of_p (TREE_TYPE (value), value, div))
15954 : return value;
15955 : }
15956 :
15957 : /* If divisor is a power of two, simplify this to bit manipulation. */
15958 1882647974 : if (pow2_or_zerop (divisor))
15959 : {
15960 1882647974 : if (TREE_CODE (value) == INTEGER_CST)
15961 : {
15962 1882646068 : wide_int val = wi::to_wide (value);
15963 1882646068 : bool overflow_p;
15964 :
15965 1882646068 : if ((val & (divisor - 1)) == 0)
15966 : return value;
15967 :
15968 3916090 : overflow_p = TREE_OVERFLOW (value);
15969 3916090 : val += divisor - 1;
15970 3916090 : val &= (int) -divisor;
15971 3916090 : if (val == 0)
15972 4 : overflow_p = true;
15973 :
15974 3916090 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
15975 1882646068 : }
15976 : else
15977 : {
15978 1906 : tree t;
15979 :
15980 1906 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
15981 1906 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
15982 1906 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
15983 1906 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15984 : }
15985 : }
15986 : else
15987 : {
15988 0 : if (!div)
15989 0 : div = build_int_cst (TREE_TYPE (value), divisor);
15990 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15991 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
15992 : }
15993 :
15994 : return value;
15995 : }
15996 :
15997 : /* Likewise, but round down. */
15998 :
15999 : tree
16000 20538379 : round_down_loc (location_t loc, tree value, int divisor)
16001 : {
16002 20538379 : tree div = NULL_TREE;
16003 :
16004 20538379 : gcc_assert (divisor > 0);
16005 20538379 : if (divisor == 1)
16006 : return value;
16007 :
16008 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16009 : have to do anything. Only do this when we are not given a const,
16010 : because in that case, this check is more expensive than just
16011 : doing it. */
16012 20538379 : if (TREE_CODE (value) != INTEGER_CST)
16013 : {
16014 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16015 :
16016 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16017 : return value;
16018 : }
16019 :
16020 : /* If divisor is a power of two, simplify this to bit manipulation. */
16021 20538379 : if (pow2_or_zerop (divisor))
16022 : {
16023 20538379 : tree t;
16024 :
16025 20538379 : t = build_int_cst (TREE_TYPE (value), -divisor);
16026 20538379 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16027 : }
16028 : else
16029 : {
16030 0 : if (!div)
16031 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16032 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16033 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16034 : }
16035 :
16036 : return value;
16037 : }
16038 :
16039 : /* Returns the pointer to the base of the object addressed by EXP and
16040 : extracts the information about the offset of the access, storing it
16041 : to PBITPOS and POFFSET. */
16042 :
16043 : static tree
16044 2173756 : split_address_to_core_and_offset (tree exp,
16045 : poly_int64 *pbitpos, tree *poffset)
16046 : {
16047 2173756 : tree core;
16048 2173756 : machine_mode mode;
16049 2173756 : int unsignedp, reversep, volatilep;
16050 2173756 : poly_int64 bitsize;
16051 2173756 : location_t loc = EXPR_LOCATION (exp);
16052 :
16053 2173756 : if (TREE_CODE (exp) == SSA_NAME)
16054 431879 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16055 324515 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16056 30103 : exp = gimple_assign_rhs1 (def);
16057 :
16058 2173756 : if (TREE_CODE (exp) == ADDR_EXPR)
16059 : {
16060 1213968 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16061 : poffset, &mode, &unsignedp, &reversep,
16062 : &volatilep);
16063 : /* If we are left with MEM[a + CST] strip that and add it to the
16064 : pbitpos and return a. */
16065 1213968 : if (TREE_CODE (core) == MEM_REF)
16066 : {
16067 26882 : poly_offset_int tem;
16068 26882 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16069 26882 : tem <<= LOG2_BITS_PER_UNIT;
16070 26882 : tem += *pbitpos;
16071 26882 : if (tem.to_shwi (pbitpos))
16072 26704 : return TREE_OPERAND (core, 0);
16073 : }
16074 1187264 : core = build_fold_addr_expr_loc (loc, core);
16075 : }
16076 959788 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16077 : {
16078 344497 : core = TREE_OPERAND (exp, 0);
16079 344497 : STRIP_NOPS (core);
16080 344497 : *pbitpos = 0;
16081 344497 : *poffset = TREE_OPERAND (exp, 1);
16082 344497 : if (poly_int_tree_p (*poffset))
16083 : {
16084 344412 : poly_offset_int tem
16085 344412 : = wi::sext (wi::to_poly_offset (*poffset),
16086 344412 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16087 344412 : tem <<= LOG2_BITS_PER_UNIT;
16088 344412 : if (tem.to_shwi (pbitpos))
16089 344412 : *poffset = NULL_TREE;
16090 : }
16091 : }
16092 : else
16093 : {
16094 615291 : core = exp;
16095 615291 : *pbitpos = 0;
16096 615291 : *poffset = NULL_TREE;
16097 : }
16098 :
16099 : return core;
16100 : }
16101 :
16102 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16103 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16104 :
16105 : bool
16106 1086878 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16107 : {
16108 1086878 : tree core1, core2;
16109 1086878 : poly_int64 bitpos1, bitpos2;
16110 1086878 : tree toffset1, toffset2, tdiff, type;
16111 :
16112 1086878 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16113 1086878 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16114 :
16115 1086878 : poly_int64 bytepos1, bytepos2;
16116 1086878 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16117 1699063 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16118 2173756 : || !operand_equal_p (core1, core2, 0))
16119 612185 : return false;
16120 :
16121 474693 : if (toffset1 && toffset2)
16122 : {
16123 29 : type = TREE_TYPE (toffset1);
16124 29 : if (type != TREE_TYPE (toffset2))
16125 0 : toffset2 = fold_convert (type, toffset2);
16126 :
16127 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16128 29 : if (!cst_and_fits_in_hwi (tdiff))
16129 : return false;
16130 :
16131 15 : *diff = int_cst_value (tdiff);
16132 : }
16133 474664 : else if (toffset1 || toffset2)
16134 : {
16135 : /* If only one of the offsets is non-constant, the difference cannot
16136 : be a constant. */
16137 : return false;
16138 : }
16139 : else
16140 456282 : *diff = 0;
16141 :
16142 456297 : *diff += bytepos1 - bytepos2;
16143 456297 : return true;
16144 : }
16145 :
16146 : /* Return OFF converted to a pointer offset type suitable as offset for
16147 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16148 : tree
16149 50716180 : convert_to_ptrofftype_loc (location_t loc, tree off)
16150 : {
16151 50716180 : if (ptrofftype_p (TREE_TYPE (off)))
16152 : return off;
16153 5576046 : return fold_convert_loc (loc, sizetype, off);
16154 : }
16155 :
16156 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16157 : tree
16158 45190683 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16159 : {
16160 45190683 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16161 45190683 : ptr, convert_to_ptrofftype_loc (loc, off));
16162 : }
16163 :
16164 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16165 : tree
16166 163957 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16167 : {
16168 163957 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16169 163957 : ptr, size_int (off));
16170 : }
16171 :
16172 : /* Return a pointer to a NUL-terminated string containing the sequence
16173 : of bytes corresponding to the representation of the object referred to
16174 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16175 : to an initialized constant array plus some constant offset).
16176 : Set *STRSIZE the number of bytes in the constant sequence including
16177 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16178 : where A is the array that stores the constant sequence that SRC points
16179 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16180 : need not point to a string or even an array of characters but may point
16181 : to an object of any type. */
16182 :
16183 : const char *
16184 12308248 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16185 : {
16186 : /* The offset into the array A storing the string, and A's byte size. */
16187 12308248 : tree offset_node;
16188 12308248 : tree mem_size;
16189 :
16190 12308248 : if (strsize)
16191 4645401 : *strsize = 0;
16192 :
16193 12308248 : if (strsize)
16194 4645401 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16195 : else
16196 7662847 : src = string_constant (src, &offset_node, &mem_size, NULL);
16197 12308248 : if (!src)
16198 : return NULL;
16199 :
16200 2773863 : unsigned HOST_WIDE_INT offset = 0;
16201 2773863 : if (offset_node != NULL_TREE)
16202 : {
16203 2773863 : if (!tree_fits_uhwi_p (offset_node))
16204 : return NULL;
16205 : else
16206 2772099 : offset = tree_to_uhwi (offset_node);
16207 : }
16208 :
16209 2772099 : if (!tree_fits_uhwi_p (mem_size))
16210 : return NULL;
16211 :
16212 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16213 : is stored in and equal to sizeof A. INIT_BYTES is the number
16214 : of bytes in the constant sequence used to initialize the array,
16215 : including any embedded NULs as well as the terminating NUL (for
16216 : strings), but not including any trailing zeros/NULs past
16217 : the terminating one appended implicitly to a string literal to
16218 : zero out the remainder of the array it's stored in. For example,
16219 : given:
16220 : const char a[7] = "abc\0d";
16221 : n = strlen (a + 1);
16222 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16223 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16224 : is equal to strlen (A) + 1. */
16225 2772099 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16226 2772099 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16227 2772099 : const char *string = TREE_STRING_POINTER (src);
16228 :
16229 : /* Ideally this would turn into a gcc_checking_assert over time. */
16230 2772099 : if (init_bytes > array_size)
16231 : init_bytes = array_size;
16232 :
16233 2772099 : if (init_bytes == 0 || offset >= array_size)
16234 : return NULL;
16235 :
16236 2770834 : if (strsize)
16237 : {
16238 : /* Compute and store the number of characters from the beginning
16239 : of the substring at OFFSET to the end, including the terminating
16240 : nul. Offsets past the initial length refer to null strings. */
16241 1438025 : if (offset < init_bytes)
16242 1438025 : *strsize = init_bytes - offset;
16243 : else
16244 0 : *strsize = 1;
16245 : }
16246 : else
16247 : {
16248 1332809 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16249 : /* Support only properly NUL-terminated single byte strings. */
16250 1332809 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16251 : return NULL;
16252 1328095 : if (string[init_bytes - 1] != '\0')
16253 : return NULL;
16254 : }
16255 :
16256 2742821 : return offset < init_bytes ? string + offset : "";
16257 : }
16258 :
16259 : /* Return a pointer to a NUL-terminated string corresponding to
16260 : the expression STR referencing a constant string, possibly
16261 : involving a constant offset. Return null if STR either doesn't
16262 : reference a constant string or if it involves a nonconstant
16263 : offset. */
16264 :
16265 : const char *
16266 7662847 : c_getstr (tree str)
16267 : {
16268 7662847 : return getbyterep (str, NULL);
16269 : }
16270 :
16271 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16272 : may be nonzero, with precision PREC, the precision of T's type. */
16273 :
16274 : static wide_int
16275 252675515 : tree_nonzero_bits (const_tree t, unsigned prec)
16276 : {
16277 252675515 : switch (TREE_CODE (t))
16278 : {
16279 9023347 : case INTEGER_CST:
16280 9023347 : return wi::to_wide (t);
16281 140695048 : case SSA_NAME:
16282 140695048 : return get_nonzero_bits (t);
16283 254508 : case NON_LVALUE_EXPR:
16284 254508 : case SAVE_EXPR:
16285 254508 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16286 457522 : case BIT_AND_EXPR:
16287 915044 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16288 1372566 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16289 5396 : case BIT_IOR_EXPR:
16290 5396 : case BIT_XOR_EXPR:
16291 10792 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16292 16188 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16293 66654 : case COND_EXPR:
16294 133308 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16295 199962 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16296 53277559 : CASE_CONVERT:
16297 53277559 : if (TREE_TYPE (t) != error_mark_node
16298 53277559 : && !error_operand_p (TREE_OPERAND (t, 0)))
16299 : {
16300 53277558 : tree op0 = TREE_OPERAND (t, 0);
16301 53277558 : tree inner_type = TREE_TYPE (op0);
16302 53277558 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16303 106555116 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16304 106555116 : prec, TYPE_SIGN (inner_type));
16305 : }
16306 : break;
16307 14470202 : case PLUS_EXPR:
16308 14470202 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16309 : {
16310 14470202 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16311 14470202 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16312 14470202 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16313 629816 : return wi::bit_or (nzbits1, nzbits2);
16314 14470202 : }
16315 : break;
16316 170303 : case LSHIFT_EXPR:
16317 170303 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16318 170303 : && TREE_TYPE (t) != error_mark_node)
16319 : {
16320 98424 : tree type = TREE_TYPE (t);
16321 98424 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16322 98424 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16323 98424 : return wi::neg_p (arg1)
16324 196848 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16325 98424 : : wi::lshift (nzbits, arg1);
16326 98424 : }
16327 : break;
16328 157264 : case RSHIFT_EXPR:
16329 157264 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16330 157264 : && TREE_TYPE (t) != error_mark_node)
16331 : {
16332 155538 : tree type = TREE_TYPE (t);
16333 155538 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16334 155538 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16335 155538 : return wi::neg_p (arg1)
16336 311076 : ? wi::lshift (nzbits, -arg1)
16337 155538 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16338 155538 : }
16339 : break;
16340 : default:
16341 : break;
16342 : }
16343 :
16344 48011704 : return wi::shwi (-1, prec);
16345 : }
16346 :
16347 : /* Given a tree T, compute which bits in T may be nonzero. */
16348 :
16349 : wide_int
16350 168889939 : tree_nonzero_bits (const_tree t)
16351 : {
16352 168889939 : if (error_operand_p (t))
16353 0 : return wi::shwi (-1, 64);
16354 168889939 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16355 : }
16356 :
16357 : /* Helper function for address compare simplifications in match.pd.
16358 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16359 : TYPE is the type of comparison operands.
16360 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16361 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16362 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16363 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16364 : and 2 if unknown. */
16365 :
16366 : int
16367 4870728 : address_compare (tree_code code, tree type, tree op0, tree op1,
16368 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16369 : bool generic)
16370 : {
16371 4870728 : if (TREE_CODE (op0) == SSA_NAME)
16372 31606 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16373 4870728 : if (TREE_CODE (op1) == SSA_NAME)
16374 4469 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16375 4870728 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16376 4870728 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16377 4870728 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16378 4870728 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16379 4870728 : if (base0 && TREE_CODE (base0) == MEM_REF)
16380 : {
16381 30206 : off0 += mem_ref_offset (base0).force_shwi ();
16382 30206 : base0 = TREE_OPERAND (base0, 0);
16383 : }
16384 4870728 : if (base1 && TREE_CODE (base1) == MEM_REF)
16385 : {
16386 3605 : off1 += mem_ref_offset (base1).force_shwi ();
16387 3605 : base1 = TREE_OPERAND (base1, 0);
16388 : }
16389 4870728 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16390 : return 2;
16391 :
16392 4858816 : int equal = 2;
16393 : /* Punt in GENERIC on variables with value expressions;
16394 : the value expressions might point to fields/elements
16395 : of other vars etc. */
16396 4858816 : if (generic
16397 4858816 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16398 4733657 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16399 : return 2;
16400 4858258 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16401 : {
16402 1242424 : symtab_node *node0 = symtab_node::get_create (base0);
16403 1242424 : symtab_node *node1 = symtab_node::get_create (base1);
16404 1242424 : equal = node0->equal_address_to (node1);
16405 : }
16406 3615834 : else if ((DECL_P (base0)
16407 228009 : || TREE_CODE (base0) == SSA_NAME
16408 198922 : || TREE_CODE (base0) == STRING_CST)
16409 3615636 : && (DECL_P (base1)
16410 202453 : || TREE_CODE (base1) == SSA_NAME
16411 199074 : || TREE_CODE (base1) == STRING_CST))
16412 3615621 : equal = (base0 == base1);
16413 : /* Assume different STRING_CSTs with the same content will be
16414 : merged. */
16415 4858045 : if (equal == 0
16416 75495 : && TREE_CODE (base0) == STRING_CST
16417 17623 : && TREE_CODE (base1) == STRING_CST
16418 17435 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16419 4858045 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16420 6187 : TREE_STRING_LENGTH (base0)) == 0)
16421 : equal = 1;
16422 4853825 : if (equal == 1)
16423 : {
16424 4762222 : if (code == EQ_EXPR
16425 4762222 : || code == NE_EXPR
16426 : /* If the offsets are equal we can ignore overflow. */
16427 125328 : || known_eq (off0, off1)
16428 250432 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16429 : /* Or if we compare using pointers to decls or strings. */
16430 4887438 : || (POINTER_TYPE_P (type)
16431 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16432 : return 1;
16433 : return 2;
16434 : }
16435 96036 : if (equal != 0)
16436 : return equal;
16437 71062 : if (code != EQ_EXPR && code != NE_EXPR)
16438 : return 2;
16439 :
16440 : /* At this point we know (or assume) the two pointers point at
16441 : different objects. */
16442 65800 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16443 65800 : off0.is_constant (&ioff0);
16444 65800 : off1.is_constant (&ioff1);
16445 : /* Punt on non-zero offsets from functions. */
16446 65800 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16447 65800 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16448 : return 2;
16449 : /* Or if the bases are neither decls nor string literals. */
16450 65800 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16451 : return 2;
16452 37447 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16453 : return 2;
16454 : /* For initializers, assume addresses of different functions are
16455 : different. */
16456 37447 : if (folding_initializer
16457 12951 : && TREE_CODE (base0) == FUNCTION_DECL
16458 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16459 : return 0;
16460 :
16461 : /* Compute whether one address points to the start of one
16462 : object and another one to the end of another one. */
16463 37427 : poly_int64 size0 = 0, size1 = 0;
16464 37427 : if (TREE_CODE (base0) == STRING_CST)
16465 : {
16466 13024 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16467 : equal = 2;
16468 : else
16469 : size0 = TREE_STRING_LENGTH (base0);
16470 : }
16471 24403 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16472 : size0 = 1;
16473 : else
16474 : {
16475 24136 : tree sz0 = DECL_SIZE_UNIT (base0);
16476 24136 : if (!tree_fits_poly_int64_p (sz0))
16477 : equal = 2;
16478 : else
16479 24136 : size0 = tree_to_poly_int64 (sz0);
16480 : }
16481 37427 : if (TREE_CODE (base1) == STRING_CST)
16482 : {
16483 13128 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16484 : equal = 2;
16485 : else
16486 : size1 = TREE_STRING_LENGTH (base1);
16487 : }
16488 24299 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16489 : size1 = 1;
16490 : else
16491 : {
16492 24036 : tree sz1 = DECL_SIZE_UNIT (base1);
16493 24036 : if (!tree_fits_poly_int64_p (sz1))
16494 : equal = 2;
16495 : else
16496 24036 : size1 = tree_to_poly_int64 (sz1);
16497 : }
16498 37427 : if (equal == 0)
16499 : {
16500 : /* If one offset is pointing (or could be) to the beginning of one
16501 : object and the other is pointing to one past the last byte of the
16502 : other object, punt. */
16503 37415 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16504 : equal = 2;
16505 37278 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16506 : equal = 2;
16507 : /* If both offsets are the same, there are some cases we know that are
16508 : ok. Either if we know they aren't zero, or if we know both sizes
16509 : are no zero. */
16510 : if (equal == 2
16511 273 : && known_eq (off0, off1)
16512 22 : && (known_ne (off0, 0)
16513 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16514 : equal = 0;
16515 : }
16516 :
16517 : /* At this point, equal is 2 if either one or both pointers are out of
16518 : bounds of their object, or one points to start of its object and the
16519 : other points to end of its object. This is unspecified behavior
16520 : e.g. in C++. Otherwise equal is 0. */
16521 37427 : if (folding_cxx_constexpr && equal)
16522 : return equal;
16523 :
16524 : /* When both pointers point to string literals, even when equal is 0,
16525 : due to tail merging of string literals the pointers might be the same. */
16526 37364 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16527 : {
16528 12984 : if (ioff0 < 0
16529 12984 : || ioff1 < 0
16530 12984 : || ioff0 > TREE_STRING_LENGTH (base0)
16531 25956 : || ioff1 > TREE_STRING_LENGTH (base1))
16532 : return 2;
16533 :
16534 : /* If the bytes in the string literals starting at the pointers
16535 : differ, the pointers need to be different. */
16536 12972 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16537 12972 : TREE_STRING_POINTER (base1) + ioff1,
16538 12972 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16539 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16540 : {
16541 3907 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16542 3907 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16543 3907 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16544 : ioffmin) == 0)
16545 : /* If even the bytes in the string literal before the
16546 : pointers are the same, the string literals could be
16547 : tail merged. */
16548 : return 2;
16549 : }
16550 : return 0;
16551 : }
16552 :
16553 24380 : if (folding_cxx_constexpr)
16554 : return 0;
16555 :
16556 : /* If this is a pointer comparison, ignore for now even
16557 : valid equalities where one pointer is the offset zero
16558 : of one object and the other to one past end of another one. */
16559 11552 : if (!INTEGRAL_TYPE_P (type))
16560 : return 0;
16561 :
16562 : /* Assume that string literals can't be adjacent to variables
16563 : (automatic or global). */
16564 312 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16565 : return 0;
16566 :
16567 : /* Assume that automatic variables can't be adjacent to global
16568 : variables. */
16569 291 : if (is_global_var (base0) != is_global_var (base1))
16570 : return 0;
16571 :
16572 : return equal;
16573 : }
16574 :
16575 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16576 : tree
16577 52 : ctor_single_nonzero_element (const_tree t)
16578 : {
16579 52 : unsigned HOST_WIDE_INT idx;
16580 52 : constructor_elt *ce;
16581 52 : tree elt = NULL_TREE;
16582 :
16583 52 : if (TREE_CODE (t) != CONSTRUCTOR)
16584 : return NULL_TREE;
16585 113 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16586 110 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16587 : {
16588 101 : if (elt)
16589 : return NULL_TREE;
16590 52 : elt = ce->value;
16591 : }
16592 : return elt;
16593 : }
16594 :
16595 : #if CHECKING_P
16596 :
16597 : namespace selftest {
16598 :
16599 : /* Helper functions for writing tests of folding trees. */
16600 :
16601 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16602 :
16603 : static void
16604 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16605 : tree constant)
16606 : {
16607 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16608 16 : }
16609 :
16610 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16611 : wrapping WRAPPED_EXPR. */
16612 :
16613 : static void
16614 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16615 : tree wrapped_expr)
16616 : {
16617 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16618 12 : ASSERT_NE (wrapped_expr, result);
16619 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16620 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16621 12 : }
16622 :
16623 : /* Verify that various arithmetic binary operations are folded
16624 : correctly. */
16625 :
16626 : static void
16627 4 : test_arithmetic_folding ()
16628 : {
16629 4 : tree type = integer_type_node;
16630 4 : tree x = create_tmp_var_raw (type, "x");
16631 4 : tree zero = build_zero_cst (type);
16632 4 : tree one = build_int_cst (type, 1);
16633 :
16634 : /* Addition. */
16635 : /* 1 <-- (0 + 1) */
16636 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16637 : one);
16638 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16639 : one);
16640 :
16641 : /* (nonlvalue)x <-- (x + 0) */
16642 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16643 : x);
16644 :
16645 : /* Subtraction. */
16646 : /* 0 <-- (x - x) */
16647 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16648 : zero);
16649 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16650 : x);
16651 :
16652 : /* Multiplication. */
16653 : /* 0 <-- (x * 0) */
16654 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16655 : zero);
16656 :
16657 : /* (nonlvalue)x <-- (x * 1) */
16658 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16659 : x);
16660 4 : }
16661 :
16662 : namespace test_operand_equality {
16663 :
16664 : /* Verify structural equality. */
16665 :
16666 : /* Execute fold_vec_perm_cst unit tests. */
16667 :
16668 : static void
16669 4 : test ()
16670 : {
16671 4 : tree stype = integer_type_node;
16672 4 : tree utype = unsigned_type_node;
16673 4 : tree x = create_tmp_var_raw (stype, "x");
16674 4 : tree y = create_tmp_var_raw (stype, "y");
16675 4 : tree z = create_tmp_var_raw (stype, "z");
16676 4 : tree four = build_int_cst (stype, 4);
16677 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16678 4 : tree rhs1 = fold_convert (stype,
16679 : fold_build2 (PLUS_EXPR, utype,
16680 : fold_convert (utype, x),
16681 : fold_convert (utype, y)));
16682 :
16683 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16684 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16685 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16686 :
16687 : /* (int)(unsigned) x == x. */
16688 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16689 : build1 (NOP_EXPR, utype, x));
16690 4 : tree rhs2 = x;
16691 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16692 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16693 :
16694 : /* (unsigned x) + (unsigned y) == x + y. */
16695 4 : tree lhs3 = lhs1;
16696 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16697 : fold_convert (utype, x),
16698 : fold_convert (utype, y));
16699 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16700 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16701 :
16702 : /* (unsigned x) / (unsigned y) == x / y. */
16703 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16704 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16705 : fold_convert (utype, x),
16706 : fold_convert (utype, y));
16707 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16708 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16709 :
16710 : /* (long x) / 4 == (long)(x / 4). */
16711 4 : tree lstype = long_long_integer_type_node;
16712 4 : tree lfour = build_int_cst (lstype, 4);
16713 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16714 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16715 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16716 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16717 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16718 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16719 :
16720 : /* (unsigned x) / 4 == x / 4. */
16721 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16722 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16723 : fold_convert (utype, x),
16724 : fold_convert (utype, four));
16725 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16726 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16727 :
16728 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16729 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16730 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16731 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16732 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16733 :
16734 : /* (unsigned x) + 4 == x + 4. */
16735 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16736 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16737 : fold_convert (utype, x),
16738 : fold_convert (utype, four));
16739 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16740 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16741 :
16742 : /* (unsigned x) + 4 == 4 + x. */
16743 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16744 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16745 : fold_convert (utype, x),
16746 : fold_convert (utype, four));
16747 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16748 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16749 :
16750 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16751 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16752 : fold_build2 (MULT_EXPR, stype,
16753 : fold_build2 (PLUS_EXPR, stype, four, x),
16754 : y),
16755 : z);
16756 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16757 : fold_build2 (PLUS_EXPR, utype,
16758 : fold_convert (utype, x),
16759 : fold_convert (utype, four)),
16760 : fold_convert (utype, y));
16761 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16762 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16763 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16764 4 : }
16765 : }
16766 :
16767 : namespace test_fold_vec_perm_cst {
16768 :
16769 : /* Build a VECTOR_CST corresponding to VMODE, and has
16770 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16771 : Fill it with randomized elements, using rand() % THRESHOLD. */
16772 :
16773 : static tree
16774 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16775 : unsigned nelts_per_pattern,
16776 : int step = 0, bool natural_stepped = false,
16777 : int threshold = 100)
16778 : {
16779 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16780 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16781 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16782 :
16783 : // Fill a0 for each pattern
16784 0 : for (unsigned i = 0; i < npatterns; i++)
16785 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16786 :
16787 0 : if (nelts_per_pattern == 1)
16788 0 : return builder.build ();
16789 :
16790 : // Fill a1 for each pattern
16791 0 : for (unsigned i = 0; i < npatterns; i++)
16792 : {
16793 0 : tree a1;
16794 0 : if (natural_stepped)
16795 : {
16796 0 : tree a0 = builder[i];
16797 0 : wide_int a0_val = wi::to_wide (a0);
16798 0 : wide_int a1_val = a0_val + step;
16799 0 : a1 = wide_int_to_tree (inner_type, a1_val);
16800 0 : }
16801 : else
16802 0 : a1 = build_int_cst (inner_type, rand () % threshold);
16803 0 : builder.quick_push (a1);
16804 : }
16805 0 : if (nelts_per_pattern == 2)
16806 0 : return builder.build ();
16807 :
16808 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
16809 : {
16810 0 : tree prev_elem = builder[i - npatterns];
16811 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
16812 0 : wide_int val = prev_elem_val + step;
16813 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
16814 0 : }
16815 :
16816 0 : return builder.build ();
16817 0 : }
16818 :
16819 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16820 : when result is VLA. */
16821 :
16822 : static void
16823 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
16824 : tree res, tree *expected_res)
16825 : {
16826 : /* Actual npatterns and encoded_elts in res may be less than expected due
16827 : to canonicalization. */
16828 0 : ASSERT_TRUE (res != NULL_TREE);
16829 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
16830 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
16831 :
16832 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
16833 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16834 0 : }
16835 :
16836 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16837 : when the result is VLS. */
16838 :
16839 : static void
16840 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
16841 : {
16842 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
16843 0 : for (unsigned i = 0; i < expected_nelts; i++)
16844 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16845 0 : }
16846 :
16847 : /* Helper routine to push multiple elements into BUILDER. */
16848 : template<unsigned N>
16849 0 : static void builder_push_elems (vec_perm_builder& builder,
16850 : poly_uint64 (&elems)[N])
16851 : {
16852 0 : for (unsigned i = 0; i < N; i++)
16853 0 : builder.quick_push (elems[i]);
16854 0 : }
16855 :
16856 : #define ARG0(index) vector_cst_elt (arg0, index)
16857 : #define ARG1(index) vector_cst_elt (arg1, index)
16858 :
16859 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
16860 :
16861 : static void
16862 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
16863 : {
16864 0 : for (int i = 0; i < 10; i++)
16865 : {
16866 : /* Case 1:
16867 : sel = { 0, 4, 1, 5, ... }
16868 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
16869 0 : {
16870 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16871 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16872 :
16873 0 : tree inner_type
16874 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
16875 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
16876 :
16877 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16878 0 : vec_perm_builder builder (res_len, 4, 1);
16879 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
16880 0 : builder_push_elems (builder, mask_elems);
16881 :
16882 0 : vec_perm_indices sel (builder, 2, res_len);
16883 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
16884 :
16885 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
16886 0 : validate_res (4, 1, res, expected_res);
16887 0 : }
16888 :
16889 : /* Case 2: Same as case 1, but contains an out of bounds access which
16890 : should wrap around.
16891 : sel = {0, 8, 4, 12, ...} (4, 1)
16892 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
16893 0 : {
16894 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16895 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16896 :
16897 0 : tree inner_type
16898 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
16899 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
16900 :
16901 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16902 0 : vec_perm_builder builder (res_len, 4, 1);
16903 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
16904 0 : builder_push_elems (builder, mask_elems);
16905 :
16906 0 : vec_perm_indices sel (builder, 2, res_len);
16907 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
16908 :
16909 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
16910 0 : validate_res (4, 1, res, expected_res);
16911 0 : }
16912 : }
16913 0 : }
16914 :
16915 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
16916 :
16917 : static void
16918 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
16919 : {
16920 0 : for (int i = 0; i < 10; i++)
16921 : {
16922 : /* Case 1:
16923 : sel = { 0, 1, 2, 3}
16924 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
16925 0 : {
16926 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
16927 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
16928 :
16929 0 : tree inner_type
16930 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
16931 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
16932 :
16933 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16934 0 : vec_perm_builder builder (res_len, 4, 1);
16935 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
16936 0 : builder_push_elems (builder, mask_elems);
16937 :
16938 0 : vec_perm_indices sel (builder, 2, res_len);
16939 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
16940 :
16941 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
16942 0 : validate_res_vls (res, expected_res, 4);
16943 0 : }
16944 :
16945 : /* Case 2: Same as Case 1, but crossing input vector.
16946 : sel = {0, 2, 4, 6}
16947 : In this case,the index 4 is ambiguous since len = 4 + 4x.
16948 : Since we cannot determine, which vector to choose from during
16949 : compile time, should return NULL_TREE. */
16950 0 : {
16951 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
16952 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
16953 :
16954 0 : tree inner_type
16955 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
16956 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
16957 :
16958 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16959 0 : vec_perm_builder builder (res_len, 4, 1);
16960 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
16961 0 : builder_push_elems (builder, mask_elems);
16962 :
16963 0 : vec_perm_indices sel (builder, 2, res_len);
16964 0 : const char *reason;
16965 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
16966 :
16967 0 : ASSERT_TRUE (res == NULL_TREE);
16968 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
16969 0 : }
16970 : }
16971 0 : }
16972 :
16973 : /* Test all input vectors. */
16974 :
16975 : static void
16976 0 : test_all_nunits (machine_mode vmode)
16977 : {
16978 : /* Test with 10 different inputs. */
16979 0 : for (int i = 0; i < 10; i++)
16980 : {
16981 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
16982 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
16983 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
16984 :
16985 : /* Case 1: mask = {0, ...} // (1, 1)
16986 : res = { arg0[0], ... } // (1, 1) */
16987 0 : {
16988 0 : vec_perm_builder builder (len, 1, 1);
16989 0 : builder.quick_push (0);
16990 0 : vec_perm_indices sel (builder, 2, len);
16991 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
16992 0 : tree expected_res[] = { ARG0(0) };
16993 0 : validate_res (1, 1, res, expected_res);
16994 0 : }
16995 :
16996 : /* Case 2: mask = {len, ...} // (1, 1)
16997 : res = { arg1[0], ... } // (1, 1) */
16998 0 : {
16999 0 : vec_perm_builder builder (len, 1, 1);
17000 0 : builder.quick_push (len);
17001 0 : vec_perm_indices sel (builder, 2, len);
17002 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17003 :
17004 0 : tree expected_res[] = { ARG1(0) };
17005 0 : validate_res (1, 1, res, expected_res);
17006 0 : }
17007 : }
17008 0 : }
17009 :
17010 : /* Test all vectors which contain at-least 2 elements. */
17011 :
17012 : static void
17013 0 : test_nunits_min_2 (machine_mode vmode)
17014 : {
17015 0 : for (int i = 0; i < 10; i++)
17016 : {
17017 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17018 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17019 0 : {
17020 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17021 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17022 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17023 :
17024 0 : vec_perm_builder builder (len, 2, 1);
17025 0 : poly_uint64 mask_elems[] = { 0, len };
17026 0 : builder_push_elems (builder, mask_elems);
17027 :
17028 0 : vec_perm_indices sel (builder, 2, len);
17029 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17030 :
17031 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17032 0 : validate_res (2, 1, res, expected_res);
17033 0 : }
17034 :
17035 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17036 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17037 0 : {
17038 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17039 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17040 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17041 :
17042 0 : vec_perm_builder builder (len, 2, 2);
17043 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17044 0 : builder_push_elems (builder, mask_elems);
17045 :
17046 0 : vec_perm_indices sel (builder, 2, len);
17047 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17048 :
17049 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17050 0 : validate_res (2, 2, res, expected_res);
17051 0 : }
17052 :
17053 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17054 : Test that the stepped sequence of the pattern selects from
17055 : same input pattern. Since input vectors have npatterns = 2,
17056 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17057 : in input vector. So return NULL_TREE. */
17058 0 : {
17059 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17060 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17061 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17062 :
17063 0 : vec_perm_builder builder (len, 1, 3);
17064 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17065 0 : builder_push_elems (builder, mask_elems);
17066 :
17067 0 : vec_perm_indices sel (builder, 2, len);
17068 0 : const char *reason;
17069 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17070 : &reason);
17071 0 : ASSERT_TRUE (res == NULL_TREE);
17072 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17073 0 : }
17074 :
17075 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17076 : Test that stepped sequence of the pattern selects from arg0.
17077 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17078 0 : {
17079 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17080 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17081 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17082 :
17083 0 : vec_perm_builder builder (len, 1, 3);
17084 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17085 0 : builder_push_elems (builder, mask_elems);
17086 :
17087 0 : vec_perm_indices sel (builder, 2, len);
17088 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17089 :
17090 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17091 0 : validate_res (1, 3, res, expected_res);
17092 0 : }
17093 :
17094 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17095 : In this case ensure that arg has a natural stepped sequence
17096 : to preserve arg's encoding.
17097 :
17098 : As a concrete example, consider:
17099 : arg0: { -16, -9, -10, ... } // (1, 3)
17100 : arg1: { -12, -5, -6, ... } // (1, 3)
17101 : sel = { 0, len, len + 1, ... } // (1, 3)
17102 :
17103 : This will create res with following encoding:
17104 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17105 : = { -16, -12, -5, ... }
17106 :
17107 : The step in above encoding would be: (-5) - (-12) = 7
17108 : And hence res[3] would be computed as -5 + 7 = 2.
17109 : instead of arg1[2], ie, -6.
17110 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17111 : for this case. */
17112 0 : {
17113 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17114 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17115 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17116 :
17117 0 : vec_perm_builder builder (len, 1, 3);
17118 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17119 0 : builder_push_elems (builder, mask_elems);
17120 :
17121 0 : vec_perm_indices sel (builder, 2, len);
17122 0 : const char *reason;
17123 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17124 : stepped pattern, even if we didn't explicitly tell it to. So folding
17125 : may not always fail, but if it does, ensure that's because arg1 does
17126 : not have a natural stepped sequence (and not due to other reason) */
17127 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17128 0 : if (res == NULL_TREE)
17129 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17130 0 : }
17131 :
17132 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17133 : sequence and thus folding should be valid for this case. */
17134 0 : {
17135 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17136 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17137 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17138 :
17139 0 : vec_perm_builder builder (len, 1, 3);
17140 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17141 0 : builder_push_elems (builder, mask_elems);
17142 :
17143 0 : vec_perm_indices sel (builder, 2, len);
17144 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17145 :
17146 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17147 0 : validate_res (1, 3, res, expected_res);
17148 0 : }
17149 :
17150 : /* Case 8: Same as aarch64/sve/slp_3.c:
17151 : arg0, arg1 are dup vectors.
17152 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17153 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17154 :
17155 : In this case, since the input vectors are dup, only the first two
17156 : elements per pattern in sel are considered significant. */
17157 0 : {
17158 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17159 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17160 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17161 :
17162 0 : vec_perm_builder builder (len, 2, 3);
17163 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17164 0 : builder_push_elems (builder, mask_elems);
17165 :
17166 0 : vec_perm_indices sel (builder, 2, len);
17167 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17168 :
17169 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17170 0 : validate_res (2, 1, res, expected_res);
17171 0 : }
17172 : }
17173 0 : }
17174 :
17175 : /* Test all vectors which contain at-least 4 elements. */
17176 :
17177 : static void
17178 0 : test_nunits_min_4 (machine_mode vmode)
17179 : {
17180 0 : for (int i = 0; i < 10; i++)
17181 : {
17182 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17183 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17184 0 : {
17185 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17186 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17187 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17188 :
17189 0 : vec_perm_builder builder (len, 4, 1);
17190 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17191 0 : builder_push_elems (builder, mask_elems);
17192 :
17193 0 : vec_perm_indices sel (builder, 2, len);
17194 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17195 :
17196 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17197 0 : validate_res (4, 1, res, expected_res);
17198 0 : }
17199 :
17200 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17201 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17202 0 : {
17203 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17204 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17205 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17206 :
17207 0 : vec_perm_builder builder (arg0_len, 1, 3);
17208 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17209 0 : builder_push_elems (builder, mask_elems);
17210 :
17211 0 : vec_perm_indices sel (builder, 2, arg0_len);
17212 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17213 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17214 0 : validate_res (1, 3, res, expected_res);
17215 0 : }
17216 :
17217 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17218 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17219 0 : {
17220 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17221 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17222 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17223 :
17224 0 : vec_perm_builder builder (len, 1, 3);
17225 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17226 0 : builder_push_elems (builder, mask_elems);
17227 :
17228 0 : vec_perm_indices sel (builder, 2, len);
17229 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17230 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17231 0 : validate_res (1, 3, res, expected_res);
17232 0 : }
17233 :
17234 : /* Case 4:
17235 : sel = { len, 0, 2, ... } // (1, 3)
17236 : This should return NULL because we cross the input vectors.
17237 : Because,
17238 : Let's assume len = C + Cx
17239 : a1 = 0
17240 : S = 2
17241 : esel = arg0_len / sel_npatterns = C + Cx
17242 : ae = 0 + (esel - 2) * S
17243 : = 0 + (C + Cx - 2) * 2
17244 : = 2(C-2) + 2Cx
17245 :
17246 : For C >= 4:
17247 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17248 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17249 : Since q1 != qe, we cross input vectors.
17250 : So return NULL_TREE. */
17251 0 : {
17252 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17253 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17254 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17255 :
17256 0 : vec_perm_builder builder (arg0_len, 1, 3);
17257 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17258 0 : builder_push_elems (builder, mask_elems);
17259 :
17260 0 : vec_perm_indices sel (builder, 2, arg0_len);
17261 0 : const char *reason;
17262 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17263 0 : ASSERT_TRUE (res == NULL_TREE);
17264 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17265 0 : }
17266 :
17267 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17268 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17269 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17270 :
17271 : Note that fold_vec_perm_cst will set
17272 : res_npatterns = max(4, max(4, 2)) = 4
17273 : However after canonicalizing, we will end up with shape (2, 2). */
17274 0 : {
17275 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17276 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17277 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17278 :
17279 0 : vec_perm_builder builder (len, 2, 2);
17280 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17281 0 : builder_push_elems (builder, mask_elems);
17282 :
17283 0 : vec_perm_indices sel (builder, 2, len);
17284 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17285 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17286 0 : validate_res (2, 2, res, expected_res);
17287 0 : }
17288 :
17289 : /* Case 6: Test combination in sel, where one pattern is dup and other
17290 : is stepped sequence.
17291 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17292 : res = { arg0[0], arg0[0], arg0[0],
17293 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17294 0 : {
17295 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17296 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17297 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17298 :
17299 0 : vec_perm_builder builder (len, 2, 3);
17300 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17301 0 : builder_push_elems (builder, mask_elems);
17302 :
17303 0 : vec_perm_indices sel (builder, 2, len);
17304 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17305 :
17306 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17307 0 : ARG0(1), ARG0(0), ARG0(2) };
17308 0 : validate_res (2, 3, res, expected_res);
17309 0 : }
17310 :
17311 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17312 : when arg0, arg1 and sel have different number of patterns.
17313 : arg0 is of shape (1, 1)
17314 : arg1 is of shape (4, 1)
17315 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17316 :
17317 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17318 : However,
17319 : step = (len+2) - (len+1) = 1
17320 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17321 : Since step is not a multiple of arg_npatterns,
17322 : valid_mask_for_fold_vec_perm_cst should return false,
17323 : and thus fold_vec_perm_cst should return NULL_TREE. */
17324 0 : {
17325 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17326 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17327 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17328 :
17329 0 : vec_perm_builder builder (len, 2, 3);
17330 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17331 0 : builder_push_elems (builder, mask_elems);
17332 :
17333 0 : vec_perm_indices sel (builder, 2, len);
17334 0 : const char *reason;
17335 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17336 :
17337 0 : ASSERT_TRUE (res == NULL_TREE);
17338 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17339 0 : }
17340 :
17341 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17342 : check that the result is not a stepped sequence either, even
17343 : if sel has a stepped sequence. */
17344 0 : {
17345 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17346 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17347 :
17348 0 : vec_perm_builder builder (len, 1, 3);
17349 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17350 0 : builder_push_elems (builder, mask_elems);
17351 :
17352 0 : vec_perm_indices sel (builder, 1, len);
17353 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17354 :
17355 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17356 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17357 0 : }
17358 :
17359 : /* Case 9: If sel doesn't contain a stepped sequence,
17360 : check that the result has same encoding as sel, irrespective
17361 : of shape of input vectors. */
17362 0 : {
17363 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17364 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17365 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17366 :
17367 0 : vec_perm_builder builder (len, 1, 2);
17368 0 : poly_uint64 mask_elems[] = { 0, len };
17369 0 : builder_push_elems (builder, mask_elems);
17370 :
17371 0 : vec_perm_indices sel (builder, 2, len);
17372 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17373 :
17374 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17375 0 : validate_res (sel.encoding ().npatterns (),
17376 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17377 0 : }
17378 : }
17379 0 : }
17380 :
17381 : /* Test all vectors which contain at-least 8 elements. */
17382 :
17383 : static void
17384 0 : test_nunits_min_8 (machine_mode vmode)
17385 : {
17386 0 : for (int i = 0; i < 10; i++)
17387 : {
17388 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17389 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17390 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17391 : arg0[2], arg0[0], arg0[3], arg1[0],
17392 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17393 0 : {
17394 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17395 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17396 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17397 :
17398 0 : vec_perm_builder builder(len, 4, 3);
17399 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17400 0 : 4, 0, 5, len };
17401 0 : builder_push_elems (builder, mask_elems);
17402 :
17403 0 : vec_perm_indices sel (builder, 2, len);
17404 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17405 :
17406 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17407 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17408 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17409 0 : validate_res (4, 3, res, expected_res);
17410 0 : }
17411 : }
17412 0 : }
17413 :
17414 : /* Test vectors for which nunits[0] <= 4. */
17415 :
17416 : static void
17417 0 : test_nunits_max_4 (machine_mode vmode)
17418 : {
17419 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17420 : This should return NULL_TREE because the index 4 may choose
17421 : from either arg0 or arg1 depending on vector length. */
17422 0 : {
17423 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17424 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17425 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17426 :
17427 0 : vec_perm_builder builder (len, 1, 2);
17428 0 : poly_uint64 mask_elems[] = {0, 4};
17429 0 : builder_push_elems (builder, mask_elems);
17430 :
17431 0 : vec_perm_indices sel (builder, 2, len);
17432 0 : const char *reason;
17433 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17434 0 : ASSERT_TRUE (res == NULL_TREE);
17435 0 : ASSERT_TRUE (reason != NULL);
17436 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17437 0 : }
17438 0 : }
17439 :
17440 : #undef ARG0
17441 : #undef ARG1
17442 :
17443 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17444 :
17445 : static bool
17446 0 : is_simple_vla_size (poly_uint64 size)
17447 : {
17448 124 : if (size.is_constant ()
17449 : || !pow2p_hwi (size.coeffs[0]))
17450 0 : return false;
17451 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17452 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17453 : return false;
17454 : return true;
17455 : }
17456 :
17457 : /* Execute fold_vec_perm_cst unit tests. */
17458 :
17459 : static void
17460 4 : test ()
17461 : {
17462 4 : machine_mode vnx4si_mode = E_VOIDmode;
17463 4 : machine_mode v4si_mode = E_VOIDmode;
17464 :
17465 4 : machine_mode vmode;
17466 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17467 : {
17468 : /* Obtain modes corresponding to VNx4SI and V4SI,
17469 : to call mixed mode tests below.
17470 : FIXME: Is there a better way to do this ? */
17471 124 : if (GET_MODE_INNER (vmode) == SImode)
17472 : {
17473 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17474 124 : if (is_simple_vla_size (nunits)
17475 : && nunits.coeffs[0] == 4)
17476 : vnx4si_mode = vmode;
17477 124 : else if (known_eq (nunits, poly_uint64 (4)))
17478 124 : v4si_mode = vmode;
17479 : }
17480 :
17481 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17482 : || !targetm.vector_mode_supported_p (vmode))
17483 124 : continue;
17484 :
17485 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17486 : test_all_nunits (vmode);
17487 : if (nunits.coeffs[0] >= 2)
17488 : test_nunits_min_2 (vmode);
17489 : if (nunits.coeffs[0] >= 4)
17490 : test_nunits_min_4 (vmode);
17491 : if (nunits.coeffs[0] >= 8)
17492 : test_nunits_min_8 (vmode);
17493 :
17494 : if (nunits.coeffs[0] <= 4)
17495 : test_nunits_max_4 (vmode);
17496 : }
17497 :
17498 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17499 : && targetm.vector_mode_supported_p (vnx4si_mode)
17500 : && targetm.vector_mode_supported_p (v4si_mode))
17501 : {
17502 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17503 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17504 : }
17505 4 : }
17506 : } // end of test_fold_vec_perm_cst namespace
17507 :
17508 : /* Verify that various binary operations on vectors are folded
17509 : correctly. */
17510 :
17511 : static void
17512 4 : test_vector_folding ()
17513 : {
17514 4 : tree inner_type = integer_type_node;
17515 4 : tree type = build_vector_type (inner_type, 4);
17516 4 : tree zero = build_zero_cst (type);
17517 4 : tree one = build_one_cst (type);
17518 4 : tree index = build_index_vector (type, 0, 1);
17519 :
17520 : /* Verify equality tests that return a scalar boolean result. */
17521 4 : tree res_type = boolean_type_node;
17522 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17523 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17524 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17525 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17526 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17527 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17528 : index, one)));
17529 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17530 : index, index)));
17531 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17532 : index, index)));
17533 4 : }
17534 :
17535 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17536 :
17537 : static void
17538 4 : test_vec_duplicate_folding ()
17539 : {
17540 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17541 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17542 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17543 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17544 :
17545 4 : tree type = build_vector_type (ssizetype, nunits);
17546 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17547 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17548 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17549 4 : }
17550 :
17551 : /* Run all of the selftests within this file. */
17552 :
17553 : void
17554 4 : fold_const_cc_tests ()
17555 : {
17556 4 : test_arithmetic_folding ();
17557 4 : test_vector_folding ();
17558 4 : test_vec_duplicate_folding ();
17559 4 : test_fold_vec_perm_cst::test ();
17560 4 : test_operand_equality::test ();
17561 4 : }
17562 :
17563 : } // namespace selftest
17564 :
17565 : #endif /* CHECKING_P */
|