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 139554 : minmax_from_comparison (tree_code cmp, tree exp0,
158 : const widest_int cst1,
159 : const widest_int cst2)
160 : {
161 139554 : if (cst1 == cst2)
162 : {
163 136 : if (cmp == LE_EXPR || cmp == LT_EXPR)
164 : return MIN_EXPR;
165 117 : if (cmp == GT_EXPR || cmp == GE_EXPR)
166 : return MAX_EXPR;
167 : }
168 139535 : if (cst1 == cst2 - 1)
169 : {
170 : /* X <= Y - 1 equals to X < Y. */
171 83801 : if (cmp == LE_EXPR)
172 : return MIN_EXPR;
173 : /* X > Y - 1 equals to X >= Y. */
174 83362 : 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 71141 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : {
179 18342 : int_range_max r;
180 36684 : get_range_query (cfun)->range_of_expr (r, exp0);
181 18342 : if (r.undefined_p ())
182 0 : r.set_varying (TREE_TYPE (exp0));
183 :
184 18342 : widest_int min = widest_int::from (r.lower_bound (),
185 36684 : TYPE_SIGN (TREE_TYPE (exp0)));
186 18342 : if (min == cst1)
187 721 : return MAX_EXPR;
188 18342 : }
189 : }
190 126154 : if (cst1 == cst2 + 1)
191 : {
192 : /* X < Y + 1 equals to X <= Y. */
193 1175 : if (cmp == LT_EXPR)
194 : return MIN_EXPR;
195 : /* X >= Y + 1 equals to X > Y. */
196 1147 : 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 1017 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : {
201 656 : int_range_max r;
202 1312 : get_range_query (cfun)->range_of_expr (r, exp0);
203 656 : if (r.undefined_p ())
204 0 : r.set_varying (TREE_TYPE (exp0));
205 :
206 656 : widest_int max = widest_int::from (r.upper_bound (),
207 1312 : TYPE_SIGN (TREE_TYPE (exp0)));
208 656 : if (max == cst1)
209 182 : return MIN_EXPR;
210 656 : }
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 173337 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
220 : {
221 173337 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
222 11 : return ERROR_MARK;
223 :
224 173326 : if (!operand_equal_p (exp0, exp2))
225 : return ERROR_MARK;
226 :
227 173326 : if (operand_equal_p (exp1, exp3))
228 : {
229 33666 : if (cmp == LT_EXPR || cmp == LE_EXPR)
230 : return MIN_EXPR;
231 31536 : if (cmp == GT_EXPR || cmp == GE_EXPR)
232 : return MAX_EXPR;
233 : }
234 139777 : if (TREE_CODE (exp3) == INTEGER_CST
235 139313 : && TREE_CODE (exp1) == INTEGER_CST)
236 138855 : 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 3041008 : expr_location_or (tree t, location_t loc)
245 : {
246 954322 : location_t tloc = EXPR_LOCATION (t);
247 3024692 : 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 9047783 : protected_set_expr_location_unshare (tree x, location_t loc)
255 : {
256 9047783 : if (CAN_HAVE_LOCATION_P (x)
257 8018763 : && EXPR_LOCATION (x) != loc
258 2519286 : && !(TREE_CODE (x) == SAVE_EXPR
259 1259858 : || TREE_CODE (x) == TARGET_EXPR
260 : || TREE_CODE (x) == BIND_EXPR))
261 : {
262 1259093 : x = copy_node (x);
263 1259093 : SET_EXPR_LOCATION (x, loc);
264 : }
265 9047783 : 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 2177905 : negate_mathfn_p (combined_fn fn)
273 : {
274 2177905 : 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 2173851 : default:
344 2173851 : break;
345 : }
346 2173851 : return false;
347 : }
348 :
349 : /* Check whether we may negate an integer constant T without causing
350 : overflow. */
351 :
352 : bool
353 3167870 : may_negate_without_overflow_p (const_tree t)
354 : {
355 3167870 : tree type;
356 :
357 3167870 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
358 :
359 3167870 : type = TREE_TYPE (t);
360 3167870 : if (TYPE_UNSIGNED (type))
361 : return false;
362 :
363 3167870 : 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 27787192 : negate_expr_p (tree t)
371 : {
372 27944095 : tree type;
373 :
374 27944095 : if (t == 0)
375 : return false;
376 :
377 27944095 : type = TREE_TYPE (t);
378 :
379 27944095 : STRIP_SIGN_NOPS (t);
380 27944095 : switch (TREE_CODE (t))
381 : {
382 1568813 : case INTEGER_CST:
383 1568813 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
384 : return true;
385 :
386 : /* Check that -CST will not overflow type. */
387 377190 : return may_negate_without_overflow_p (t);
388 563 : case BIT_NOT_EXPR:
389 563 : return (INTEGRAL_TYPE_P (type)
390 563 : && TYPE_OVERFLOW_WRAPS (type));
391 :
392 : case FIXED_CST:
393 : return true;
394 :
395 1297 : case NEGATE_EXPR:
396 1297 : return !TYPE_OVERFLOW_SANITIZED (type);
397 :
398 1301042 : case REAL_CST:
399 : /* We want to canonicalize to positive real constants. Pretend
400 : that only negative ones can be easily negated. */
401 1301042 : 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 127 : case VECTOR_CST:
408 127 : {
409 127 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
410 : return true;
411 :
412 : /* Steps don't prevent negation. */
413 127 : unsigned int count = vector_cst_encoded_nelts (t);
414 254 : for (unsigned int i = 0; i < count; ++i)
415 127 : 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 1518832 : case PLUS_EXPR:
429 1518832 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
430 1518826 : || HONOR_SIGNED_ZEROS (type)
431 2748483 : || (ANY_INTEGRAL_TYPE_P (type)
432 1229469 : && ! TYPE_OVERFLOW_WRAPS (type)))
433 744604 : return false;
434 : /* -(A + B) -> (-B) - A. */
435 774228 : if (negate_expr_p (TREE_OPERAND (t, 1)))
436 : return true;
437 : /* -(A + B) -> (-A) - B. */
438 146658 : return negate_expr_p (TREE_OPERAND (t, 0));
439 :
440 262479 : case MINUS_EXPR:
441 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 262479 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
443 262479 : && !HONOR_SIGNED_ZEROS (type)
444 350312 : && (! ANY_INTEGRAL_TYPE_P (type)
445 87610 : || TYPE_OVERFLOW_WRAPS (type));
446 :
447 2400665 : case MULT_EXPR:
448 2400665 : 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 4136846 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
453 162294 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
454 2228435 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
455 0 : && (wi::popcount
456 2068423 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
457 160012 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 136935 : && (wi::popcount
459 4250704 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
460 : break;
461 :
462 : /* Fall through. */
463 :
464 2340674 : case RDIV_EXPR:
465 2340674 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
466 2340673 : return negate_expr_p (TREE_OPERAND (t, 1))
467 2340673 : || negate_expr_p (TREE_OPERAND (t, 0));
468 : break;
469 :
470 2597 : case TRUNC_DIV_EXPR:
471 2597 : case ROUND_DIV_EXPR:
472 2597 : case EXACT_DIV_EXPR:
473 2597 : 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 547 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
478 547 : && 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 760 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
484 380 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
485 675 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
486 285 : && ! integer_onep (TREE_OPERAND (t, 1))))
487 370 : return negate_expr_p (TREE_OPERAND (t, 1));
488 : break;
489 :
490 5060959 : case NOP_EXPR:
491 : /* Negate -((double)float) as (double)(-float). */
492 5060959 : if (SCALAR_FLOAT_TYPE_P (type))
493 : {
494 9990 : tree tem = strip_float_extensions (t);
495 9990 : if (tem != t)
496 : return negate_expr_p (tem);
497 : }
498 : break;
499 :
500 1096951 : case CALL_EXPR:
501 : /* Negate -f(x) as f(-x). */
502 1096951 : if (negate_mathfn_p (get_call_combined_fn (t)))
503 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
504 : break;
505 :
506 12091 : case RSHIFT_EXPR:
507 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
508 12091 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 : {
510 11946 : tree op1 = TREE_OPERAND (t, 1);
511 11946 : 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 39849032 : fold_negate_expr_1 (location_t loc, tree t)
529 : {
530 39849032 : tree type = TREE_TYPE (t);
531 39849032 : tree tem;
532 :
533 39849032 : switch (TREE_CODE (t))
534 : {
535 : /* Convert - (~A) to A + 1. */
536 162 : case BIT_NOT_EXPR:
537 162 : if (INTEGRAL_TYPE_P (type))
538 162 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
539 162 : build_one_cst (type));
540 : break;
541 :
542 30540542 : case INTEGER_CST:
543 30540542 : tem = fold_negate_const (t, type);
544 30540542 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
545 10080 : || (ANY_INTEGRAL_TYPE_P (type)
546 10080 : && !TYPE_OVERFLOW_TRAPS (type)
547 10080 : && TYPE_OVERFLOW_WRAPS (type))
548 30549832 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
549 : return tem;
550 : break;
551 :
552 2036773 : case POLY_INT_CST:
553 2036773 : case REAL_CST:
554 2036773 : case FIXED_CST:
555 2036773 : tem = fold_negate_const (t, type);
556 2036773 : return tem;
557 :
558 66206 : case COMPLEX_CST:
559 66206 : {
560 66206 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
561 66206 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
562 66206 : if (rpart && ipart)
563 66206 : return build_complex (type, rpart, ipart);
564 : }
565 : break;
566 :
567 50259 : case VECTOR_CST:
568 50259 : {
569 50259 : tree_vector_builder elts;
570 50259 : elts.new_unary_operation (type, t, true);
571 50259 : unsigned int count = elts.encoded_nelts ();
572 123085 : for (unsigned int i = 0; i < count; ++i)
573 : {
574 72826 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 72826 : if (elt == NULL_TREE)
576 0 : return NULL_TREE;
577 72826 : elts.quick_push (elt);
578 : }
579 :
580 50259 : return elts.build ();
581 50259 : }
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 1223 : case NEGATE_EXPR:
597 1223 : if (!TYPE_OVERFLOW_SANITIZED (type))
598 1210 : return TREE_OPERAND (t, 0);
599 : break;
600 :
601 689565 : case PLUS_EXPR:
602 689565 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
603 689565 : && !HONOR_SIGNED_ZEROS (type))
604 : {
605 : /* -(A + B) -> (-B) - A. */
606 689455 : if (negate_expr_p (TREE_OPERAND (t, 1)))
607 : {
608 631441 : tem = negate_expr (TREE_OPERAND (t, 1));
609 631441 : return fold_build2_loc (loc, MINUS_EXPR, type,
610 1262882 : tem, TREE_OPERAND (t, 0));
611 : }
612 :
613 : /* -(A + B) -> (-A) - B. */
614 58014 : if (negate_expr_p (TREE_OPERAND (t, 0)))
615 : {
616 1010 : tem = negate_expr (TREE_OPERAND (t, 0));
617 1010 : return fold_build2_loc (loc, MINUS_EXPR, type,
618 2020 : tem, TREE_OPERAND (t, 1));
619 : }
620 : }
621 : break;
622 :
623 159674 : case MINUS_EXPR:
624 : /* - (A - B) -> B - A */
625 159674 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
626 159674 : && !HONOR_SIGNED_ZEROS (type))
627 82177 : return fold_build2_loc (loc, MINUS_EXPR, type,
628 164354 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
629 : break;
630 :
631 270950 : case MULT_EXPR:
632 270950 : if (TYPE_UNSIGNED (type))
633 : break;
634 :
635 : /* Fall through. */
636 :
637 33729 : case RDIV_EXPR:
638 33729 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
639 : {
640 33729 : tem = TREE_OPERAND (t, 1);
641 33729 : if (negate_expr_p (tem))
642 61066 : return fold_build2_loc (loc, TREE_CODE (t), type,
643 61066 : TREE_OPERAND (t, 0), negate_expr (tem));
644 3196 : tem = TREE_OPERAND (t, 0);
645 3196 : if (negate_expr_p (tem))
646 57 : return fold_build2_loc (loc, TREE_CODE (t), type,
647 114 : negate_expr (tem), TREE_OPERAND (t, 1));
648 : }
649 : break;
650 :
651 2143 : case TRUNC_DIV_EXPR:
652 2143 : case ROUND_DIV_EXPR:
653 2143 : case EXACT_DIV_EXPR:
654 2143 : 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 725 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
659 725 : && negate_expr_p (TREE_OPERAND (t, 0)))
660 325 : return fold_build2_loc (loc, TREE_CODE (t), type,
661 325 : negate_expr (TREE_OPERAND (t, 0)),
662 650 : 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 800 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
667 400 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
668 316 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
669 293 : && ! integer_onep (TREE_OPERAND (t, 1))))
670 777 : && negate_expr_p (TREE_OPERAND (t, 1)))
671 742 : return fold_build2_loc (loc, TREE_CODE (t), type,
672 371 : TREE_OPERAND (t, 0),
673 742 : negate_expr (TREE_OPERAND (t, 1)));
674 : break;
675 :
676 2480778 : case NOP_EXPR:
677 : /* Convert -((double)float) into (double)(-float). */
678 2480778 : if (SCALAR_FLOAT_TYPE_P (type))
679 : {
680 10912 : tem = strip_float_extensions (t);
681 10912 : if (tem != t && negate_expr_p (tem))
682 0 : return fold_convert_loc (loc, type, negate_expr (tem));
683 : }
684 : break;
685 :
686 296747 : case CALL_EXPR:
687 : /* Negate -f(x) as f(-x). */
688 296747 : if (negate_mathfn_p (get_call_combined_fn (t))
689 298036 : && 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 11868 : case RSHIFT_EXPR:
700 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
701 11868 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
702 : {
703 11850 : tree op1 = TREE_OPERAND (t, 1);
704 11850 : if (wi::to_wide (op1) == element_precision (type) - 1)
705 : {
706 11519 : tree ntype = TYPE_UNSIGNED (type)
707 11519 : ? signed_type_for (type)
708 72 : : unsigned_type_for (type);
709 11519 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
710 11519 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
711 11519 : 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 39849032 : fold_negate_expr (location_t loc, tree t)
727 : {
728 39849032 : tree type = TREE_TYPE (t);
729 39849032 : STRIP_SIGN_NOPS (t);
730 39849032 : tree tem = fold_negate_expr_1 (loc, t);
731 39849032 : if (tem == NULL_TREE)
732 : return NULL_TREE;
733 33453705 : 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 3890897 : negate_expr (tree t)
742 : {
743 3890897 : tree type, tem;
744 3890897 : location_t loc;
745 :
746 3890897 : if (t == NULL_TREE)
747 : return NULL_TREE;
748 :
749 3890897 : loc = EXPR_LOCATION (t);
750 3890897 : type = TREE_TYPE (t);
751 3890897 : STRIP_SIGN_NOPS (t);
752 :
753 3890897 : tem = fold_negate_expr (loc, t);
754 3890897 : if (!tem)
755 1998301 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
756 3890897 : 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 234032818 : 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 234032818 : tree var = 0;
788 234032818 : *minus_varp = 0;
789 234032818 : *conp = 0;
790 234032818 : *minus_conp = 0;
791 234032818 : *litp = 0;
792 234032818 : *minus_litp = 0;
793 :
794 : /* Strip any conversions that don't change the machine mode or signedness. */
795 234032818 : STRIP_SIGN_NOPS (in);
796 :
797 234032818 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
798 149596809 : || TREE_CODE (in) == FIXED_CST)
799 84436009 : *litp = in;
800 149596809 : else if (TREE_CODE (in) == code
801 149596809 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
802 144943517 : && ! 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 144943517 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
808 60648629 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
809 143233607 : || (code == MINUS_EXPR
810 23349583 : && (TREE_CODE (in) == PLUS_EXPR
811 21460590 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
812 : {
813 8718605 : tree op0 = TREE_OPERAND (in, 0);
814 8718605 : tree op1 = TREE_OPERAND (in, 1);
815 8718605 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
816 8718605 : 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 8718605 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
820 8500271 : || TREE_CODE (op0) == FIXED_CST)
821 218334 : *litp = op0, op0 = 0;
822 8500271 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
823 5697367 : || TREE_CODE (op1) == FIXED_CST)
824 2802904 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
825 :
826 8718605 : if (op0 != 0 && TREE_CONSTANT (op0))
827 15379 : *conp = op0, op0 = 0;
828 8703226 : else if (op1 != 0 && TREE_CONSTANT (op1))
829 52030 : *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 8718605 : if (op0 != 0 && op1 != 0)
834 : var = in;
835 3081376 : else if (op0 != 0)
836 : var = op0;
837 : else
838 233713 : var = op1, neg_var_p = neg1_p;
839 :
840 : /* Now do any needed negations. */
841 8718605 : if (neg_litp_p)
842 28506 : *minus_litp = *litp, *litp = 0;
843 8718605 : if (neg_conp_p && *conp)
844 11346 : *minus_conp = *conp, *conp = 0;
845 8718605 : if (neg_var_p && var)
846 223086 : *minus_varp = var, var = 0;
847 : }
848 140878204 : else if (TREE_CONSTANT (in))
849 821014 : *conp = in;
850 140057190 : else if (TREE_CODE (in) == BIT_NOT_EXPR
851 513383 : && code == PLUS_EXPR)
852 : {
853 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
854 : when IN is constant. */
855 362405 : *litp = build_minus_one_cst (type);
856 362405 : *minus_varp = TREE_OPERAND (in, 0);
857 : }
858 : else
859 : var = in;
860 :
861 234032818 : if (negate_p)
862 : {
863 12872267 : if (*litp)
864 1270849 : *minus_litp = *litp, *litp = 0;
865 11601418 : else if (*minus_litp)
866 174 : *litp = *minus_litp, *minus_litp = 0;
867 12872267 : if (*conp)
868 46953 : *minus_conp = *conp, *conp = 0;
869 12825314 : else if (*minus_conp)
870 0 : *conp = *minus_conp, *minus_conp = 0;
871 12872267 : if (var)
872 12812884 : *minus_varp = var, var = 0;
873 59383 : else if (*minus_varp)
874 882 : var = *minus_varp, *minus_varp = 0;
875 : }
876 :
877 234032818 : if (*litp
878 234032818 : && TREE_OVERFLOW_P (*litp))
879 19310 : *litp = drop_tree_overflow (*litp);
880 234032818 : if (*minus_litp
881 234032818 : && TREE_OVERFLOW_P (*minus_litp))
882 24 : *minus_litp = drop_tree_overflow (*minus_litp);
883 :
884 234032818 : 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 21026301 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
894 : {
895 21026301 : if (t1 == 0)
896 : {
897 13331601 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
898 : return t2;
899 : }
900 7694700 : 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 4289849 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
907 3378616 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
908 3312048 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
909 : {
910 1676128 : if (code == PLUS_EXPR)
911 : {
912 934728 : 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 934674 : 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 934673 : else if (integer_zerop (t2))
923 37422 : return fold_convert_loc (loc, type, t1);
924 : }
925 741400 : else if (code == MINUS_EXPR)
926 : {
927 717715 : if (integer_zerop (t2))
928 0 : return fold_convert_loc (loc, type, t1);
929 : }
930 :
931 1638651 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 1638651 : fold_convert_loc (loc, type, t2));
933 : }
934 :
935 2613721 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
936 2613721 : 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 2742739776 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
944 : {
945 2742739776 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
946 : return false;
947 2742739776 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
948 : return false;
949 :
950 2742739776 : switch (code)
951 : {
952 : case LSHIFT_EXPR:
953 : case RSHIFT_EXPR:
954 : case LROTATE_EXPR:
955 : case RROTATE_EXPR:
956 : return true;
957 :
958 2742739776 : default:
959 2742739776 : break;
960 : }
961 :
962 2742739776 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
963 2742739776 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
964 5485479552 : && 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 1527899713 : 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 1527899713 : wide_int tmp;
977 1527899713 : *overflow = wi::OVF_NONE;
978 1527899713 : switch (code)
979 : {
980 3783194 : case BIT_IOR_EXPR:
981 3783194 : res = wi::bit_or (arg1, arg2);
982 3783194 : break;
983 :
984 95552 : case BIT_XOR_EXPR:
985 95552 : res = wi::bit_xor (arg1, arg2);
986 95552 : break;
987 :
988 23787951 : case BIT_AND_EXPR:
989 23787951 : res = wi::bit_and (arg1, arg2);
990 23787951 : break;
991 :
992 14963271 : case LSHIFT_EXPR:
993 14963271 : if (wi::neg_p (arg2))
994 : return false;
995 14932947 : res = wi::lshift (arg1, arg2);
996 14932947 : break;
997 :
998 7421015 : case RSHIFT_EXPR:
999 7421015 : 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 7420819 : res = wi::rshift (arg1, arg2, sign);
1005 7420819 : 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 257779025 : case PLUS_EXPR:
1027 257779025 : res = wi::add (arg1, arg2, sign, overflow);
1028 257779025 : break;
1029 :
1030 75146256 : case MINUS_EXPR:
1031 75146256 : res = wi::sub (arg1, arg2, sign, overflow);
1032 75146256 : break;
1033 :
1034 438249116 : case MULT_EXPR:
1035 438249116 : res = wi::mul (arg1, arg2, sign, overflow);
1036 438249116 : break;
1037 :
1038 5488 : case MULT_HIGHPART_EXPR:
1039 5488 : res = wi::mul_high (arg1, arg2, sign);
1040 5488 : break;
1041 :
1042 361605744 : case TRUNC_DIV_EXPR:
1043 361605744 : case EXACT_DIV_EXPR:
1044 361605744 : if (arg2 == 0)
1045 : return false;
1046 361600398 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1047 361600398 : break;
1048 :
1049 81493301 : case FLOOR_DIV_EXPR:
1050 81493301 : if (arg2 == 0)
1051 : return false;
1052 81493301 : res = wi::div_floor (arg1, arg2, sign, overflow);
1053 81493301 : break;
1054 :
1055 87692551 : case CEIL_DIV_EXPR:
1056 87692551 : if (arg2 == 0)
1057 : return false;
1058 87692551 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1059 87692551 : 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 1374901 : case TRUNC_MOD_EXPR:
1068 1374901 : if (arg2 == 0)
1069 : return false;
1070 1373796 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1071 1373796 : break;
1072 :
1073 69936262 : case FLOOR_MOD_EXPR:
1074 69936262 : if (arg2 == 0)
1075 : return false;
1076 69936262 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1077 69936262 : 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 48945 : case MIN_EXPR:
1092 48945 : res = wi::min (arg1, arg2, sign);
1093 48945 : break;
1094 :
1095 104514951 : case MAX_EXPR:
1096 104514951 : res = wi::max (arg1, arg2, sign);
1097 104514951 : break;
1098 :
1099 : default:
1100 : return false;
1101 : }
1102 : return true;
1103 1527899713 : }
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 1527899713 : 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 1527899713 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1134 :
1135 1527899713 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1136 : {
1137 1527899713 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1138 1527899713 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1139 1527899713 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1140 : return NULL_TREE;
1141 1527862615 : res = wi_res;
1142 1527862615 : return true;
1143 1527899962 : }
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 1527899713 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1208 : int overflowable)
1209 : {
1210 1527899713 : poly_wide_int poly_res;
1211 1527899713 : tree type = TREE_TYPE (arg1);
1212 1527899713 : signop sign = TYPE_SIGN (type);
1213 1527899713 : wi::overflow_type overflow = wi::OVF_NONE;
1214 :
1215 1527899713 : if (!poly_int_tree_p (arg1)
1216 1527899713 : || !poly_int_tree_p (arg2)
1217 3055799426 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1218 37098 : return NULL_TREE;
1219 1527862615 : return force_fit_type (type, poly_res, overflowable,
1220 1527862615 : (((sign == SIGNED || overflowable == -1)
1221 1527862615 : && overflow)
1222 1527862615 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1223 1527899713 : }
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 189056 : 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 3921 : default:
1242 3921 : 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 444825 : 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 444825 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1255 : {
1256 360845 : if (integer_zerop (other_op))
1257 : {
1258 27648 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1259 : return op;
1260 26496 : 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 267365 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1274 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1275 : {
1276 191762 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1277 450771 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1278 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1279 : {
1280 183406 : tree type = TREE_TYPE (arg1);
1281 183406 : bool step_ok_p;
1282 183406 : if (VECTOR_CST_STEPPED_P (arg1)
1283 183406 : && 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 180700 : 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 50413 : step_ok_p = distributes_over_addition_p (code, 1);
1302 : else
1303 : /* Similarly in reverse. */
1304 130287 : step_ok_p = distributes_over_addition_p (code, 2);
1305 183406 : tree_vector_builder elts;
1306 183406 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1307 : return NULL_TREE;
1308 183406 : unsigned int count = elts.encoded_nelts ();
1309 712006 : for (unsigned int i = 0; i < count; ++i)
1310 : {
1311 528919 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1312 528919 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1313 :
1314 528919 : 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 528919 : if (elt == NULL_TREE)
1319 319 : return NULL_TREE;
1320 528600 : elts.quick_push (elt);
1321 : }
1322 :
1323 183087 : return elts.build ();
1324 183406 : }
1325 :
1326 83959 : if (TREE_CODE (arg1) == VECTOR_CST
1327 8356 : && TREE_CODE (arg2) == INTEGER_CST)
1328 : {
1329 8356 : tree type = TREE_TYPE (arg1);
1330 8356 : bool step_ok_p = distributes_over_addition_p (code, 1);
1331 8356 : tree_vector_builder elts;
1332 8356 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1333 : return NULL_TREE;
1334 8356 : unsigned int count = elts.encoded_nelts ();
1335 35521 : for (unsigned int i = 0; i < count; ++i)
1336 : {
1337 27252 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1338 :
1339 27252 : 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 27252 : if (elt == NULL_TREE)
1344 87 : return NULL_TREE;
1345 27165 : elts.quick_push (elt);
1346 : }
1347 :
1348 8269 : return elts.build ();
1349 8356 : }
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 221545802 : const_binop (enum tree_code code, tree arg1, tree arg2)
1360 : {
1361 : /* Sanity check for the recursive cases. */
1362 221545802 : if (!arg1 || !arg2)
1363 : return NULL_TREE;
1364 :
1365 221544538 : STRIP_NOPS (arg1);
1366 221544538 : STRIP_NOPS (arg2);
1367 :
1368 221544538 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1369 : {
1370 215725208 : if (code == POINTER_PLUS_EXPR)
1371 104655 : return int_const_binop (PLUS_EXPR,
1372 209310 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1373 :
1374 215620553 : return int_const_binop (code, arg1, arg2);
1375 : }
1376 :
1377 5819330 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1378 : {
1379 5535438 : machine_mode mode;
1380 5535438 : REAL_VALUE_TYPE d1;
1381 5535438 : REAL_VALUE_TYPE d2;
1382 5535438 : REAL_VALUE_TYPE value;
1383 5535438 : REAL_VALUE_TYPE result;
1384 5535438 : bool inexact;
1385 5535438 : tree t, type;
1386 :
1387 : /* The following codes are handled by real_arithmetic. */
1388 5535438 : switch (code)
1389 : {
1390 5535438 : case PLUS_EXPR:
1391 5535438 : case MINUS_EXPR:
1392 5535438 : case MULT_EXPR:
1393 5535438 : case RDIV_EXPR:
1394 5535438 : case MIN_EXPR:
1395 5535438 : case MAX_EXPR:
1396 5535438 : break;
1397 :
1398 : default:
1399 : return NULL_TREE;
1400 : }
1401 :
1402 5535438 : d1 = TREE_REAL_CST (arg1);
1403 5535438 : d2 = TREE_REAL_CST (arg2);
1404 :
1405 5535438 : type = TREE_TYPE (arg1);
1406 5535438 : mode = TYPE_MODE (type);
1407 :
1408 : /* Don't perform operation if we honor signaling NaNs and
1409 : either operand is a signaling NaN. */
1410 5535438 : if (HONOR_SNANS (mode)
1411 5535438 : && (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 5535405 : if (code == RDIV_EXPR
1418 2410774 : && real_equal (&d2, &dconst0)
1419 5546142 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1420 7829 : 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 5527576 : if (REAL_VALUE_ISNAN (d1))
1425 : {
1426 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1427 : is off. */
1428 346 : d1.signalling = 0;
1429 346 : t = build_real (type, d1);
1430 346 : return t;
1431 : }
1432 5527230 : 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 5527169 : inexact = real_arithmetic (&value, code, &d1, &d2);
1442 5527169 : 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 5527169 : if (flag_trapping_math
1449 21467362 : && MODE_HAS_NANS (mode)
1450 5508389 : && REAL_VALUE_ISNAN (result)
1451 2555 : && !REAL_VALUE_ISNAN (d1)
1452 5529724 : && !REAL_VALUE_ISNAN (d2))
1453 2555 : return NULL_TREE;
1454 :
1455 : /* Don't constant fold this floating point operation if
1456 : the result has overflowed and flag_trapping_math. */
1457 5524614 : if (flag_trapping_math
1458 21457484 : && MODE_HAS_INFINITIES (mode)
1459 5505834 : && REAL_VALUE_ISINF (result)
1460 7668 : && !REAL_VALUE_ISINF (d1)
1461 5531624 : && !REAL_VALUE_ISINF (d2))
1462 4727 : 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 5519887 : if ((flag_rounding_math
1469 37493372 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1470 5519887 : && (inexact || !real_identical (&result, &value)))
1471 1107 : return NULL_TREE;
1472 :
1473 5518780 : t = build_real (type, result);
1474 :
1475 5518780 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1476 5518780 : return t;
1477 : }
1478 :
1479 283892 : 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 283892 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1528 : {
1529 11248 : tree type = TREE_TYPE (arg1);
1530 11248 : tree r1 = TREE_REALPART (arg1);
1531 11248 : tree i1 = TREE_IMAGPART (arg1);
1532 11248 : tree r2 = TREE_REALPART (arg2);
1533 11248 : tree i2 = TREE_IMAGPART (arg2);
1534 11248 : tree real, imag;
1535 :
1536 11248 : switch (code)
1537 : {
1538 5319 : case PLUS_EXPR:
1539 5319 : case MINUS_EXPR:
1540 5319 : real = const_binop (code, r1, r2);
1541 5319 : imag = const_binop (code, i1, i2);
1542 5319 : break;
1543 :
1544 3971 : case MULT_EXPR:
1545 3971 : if (COMPLEX_FLOAT_TYPE_P (type))
1546 2819 : return do_mpc_arg2 (arg1, arg2, type,
1547 : /* do_nonfinite= */ folding_initializer,
1548 2819 : 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 6725 : if (real && imag)
1653 6567 : return build_complex (type, real, imag);
1654 : }
1655 :
1656 272802 : tree simplified;
1657 272802 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1658 : return simplified;
1659 :
1660 272227 : if (commutative_tree_code (code)
1661 272227 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1662 : return simplified;
1663 :
1664 267365 : 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 296081626 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1672 : {
1673 296081626 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1674 82151235 : 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 213930391 : 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 269906 : case COMPLEX_EXPR:
1687 269906 : if ((TREE_CODE (arg1) == REAL_CST
1688 259367 : && TREE_CODE (arg2) == REAL_CST)
1689 10541 : || (TREE_CODE (arg1) == INTEGER_CST
1690 10539 : && TREE_CODE (arg2) == INTEGER_CST))
1691 269904 : return build_complex (type, arg1, arg2);
1692 : return NULL_TREE;
1693 :
1694 177653 : case POINTER_DIFF_EXPR:
1695 177653 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1696 : {
1697 354646 : poly_offset_int res = (wi::to_poly_offset (arg1)
1698 177323 : - wi::to_poly_offset (arg2));
1699 177323 : return force_fit_type (type, res, 1,
1700 177323 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1701 : }
1702 : return NULL_TREE;
1703 :
1704 14933 : case VEC_PACK_TRUNC_EXPR:
1705 14933 : case VEC_PACK_FIX_TRUNC_EXPR:
1706 14933 : case VEC_PACK_FLOAT_EXPR:
1707 14933 : {
1708 14933 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1709 :
1710 14933 : if (TREE_CODE (arg1) != VECTOR_CST
1711 14933 : || TREE_CODE (arg2) != VECTOR_CST)
1712 : return NULL_TREE;
1713 :
1714 14933 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1715 : return NULL_TREE;
1716 :
1717 14933 : out_nelts = in_nelts * 2;
1718 14933 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1719 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1720 :
1721 14933 : tree_vector_builder elts (type, out_nelts, 1);
1722 187009 : for (i = 0; i < out_nelts; i++)
1723 : {
1724 172088 : tree elt = (i < in_nelts
1725 172088 : ? VECTOR_CST_ELT (arg1, i)
1726 86038 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1727 173132 : 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 172088 : TREE_TYPE (type), elt);
1732 172088 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1733 12 : return NULL_TREE;
1734 172076 : elts.quick_push (elt);
1735 : }
1736 :
1737 14921 : return elts.build ();
1738 14933 : }
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 213467675 : default:;
1786 : }
1787 :
1788 213467675 : if (TREE_CODE_CLASS (code) != tcc_binary)
1789 : return NULL_TREE;
1790 :
1791 : /* Make sure type and arg0 have the same saturating flag. */
1792 210951815 : gcc_checking_assert (TYPE_SATURATING (type)
1793 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1794 :
1795 210951815 : 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 376820032 : 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 376820032 : if (TREE_CODE (arg0) == REAL_CST
1807 11058992 : && HONOR_SNANS (arg0)
1808 17121 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1809 4740 : && code != NEGATE_EXPR
1810 4740 : && code != ABS_EXPR
1811 376824737 : && code != ABSU_EXPR)
1812 : return NULL_TREE;
1813 :
1814 376815327 : switch (code)
1815 : {
1816 282848602 : CASE_CONVERT:
1817 282848602 : case FLOAT_EXPR:
1818 282848602 : case FIX_TRUNC_EXPR:
1819 282848602 : case FIXED_CONVERT_EXPR:
1820 282848602 : 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 13139839 : case VIEW_CONVERT_EXPR:
1832 13139839 : return fold_view_convert_expr (type, arg0);
1833 :
1834 31500359 : case NEGATE_EXPR:
1835 31500359 : {
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 31500359 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1840 31500359 : if (tem && CONSTANT_CLASS_P (tem))
1841 : return tem;
1842 : break;
1843 : }
1844 :
1845 39757 : case ABS_EXPR:
1846 39757 : case ABSU_EXPR:
1847 39757 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1848 35200 : 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 2310372 : case BIT_NOT_EXPR:
1861 2310372 : if (TREE_CODE (arg0) == INTEGER_CST)
1862 2303304 : return fold_not_const (arg0, type);
1863 7068 : 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 7068 : else if (TREE_CODE (arg0) == VECTOR_CST)
1867 : {
1868 6435 : tree elem;
1869 :
1870 : /* This can cope with stepped encodings because ~x == -1 - x. */
1871 6435 : tree_vector_builder elements;
1872 6435 : elements.new_unary_operation (type, arg0, true);
1873 6435 : unsigned int i, count = elements.encoded_nelts ();
1874 24997 : for (i = 0; i < count; ++i)
1875 : {
1876 18562 : elem = VECTOR_CST_ELT (arg0, i);
1877 18562 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1878 18562 : if (elem == NULL_TREE)
1879 : break;
1880 18562 : elements.quick_push (elem);
1881 : }
1882 6435 : if (i == count)
1883 6435 : return elements.build ();
1884 6435 : }
1885 : break;
1886 :
1887 11290916 : case TRUTH_NOT_EXPR:
1888 11290916 : if (TREE_CODE (arg0) == INTEGER_CST)
1889 11007382 : return constant_boolean_node (integer_zerop (arg0), type);
1890 : break;
1891 :
1892 179750 : case REALPART_EXPR:
1893 179750 : if (TREE_CODE (arg0) == COMPLEX_CST)
1894 179549 : return fold_convert (type, TREE_REALPART (arg0));
1895 : break;
1896 :
1897 185543 : case IMAGPART_EXPR:
1898 185543 : if (TREE_CODE (arg0) == COMPLEX_CST)
1899 185355 : return fold_convert (type, TREE_IMAGPART (arg0));
1900 : break;
1901 :
1902 19086 : case VEC_UNPACK_LO_EXPR:
1903 19086 : case VEC_UNPACK_HI_EXPR:
1904 19086 : case VEC_UNPACK_FLOAT_LO_EXPR:
1905 19086 : case VEC_UNPACK_FLOAT_HI_EXPR:
1906 19086 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1907 19086 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1908 19086 : {
1909 19086 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1910 19086 : enum tree_code subcode;
1911 :
1912 19086 : if (TREE_CODE (arg0) != VECTOR_CST)
1913 : return NULL_TREE;
1914 :
1915 19086 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1916 : return NULL_TREE;
1917 19086 : out_nelts = in_nelts / 2;
1918 19086 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1919 :
1920 19086 : unsigned int offset = 0;
1921 19086 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1922 19086 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1923 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1924 9535 : offset = out_nelts;
1925 :
1926 19086 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1927 : subcode = NOP_EXPR;
1928 7902 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1929 7902 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1930 : subcode = FLOAT_EXPR;
1931 : else
1932 4 : subcode = FIX_TRUNC_EXPR;
1933 :
1934 19086 : tree_vector_builder elts (type, out_nelts, 1);
1935 101500 : for (i = 0; i < out_nelts; i++)
1936 : {
1937 82414 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1938 82414 : VECTOR_CST_ELT (arg0, i + offset));
1939 82414 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1940 0 : return NULL_TREE;
1941 82414 : elts.quick_push (elt);
1942 : }
1943 :
1944 19086 : return elts.build ();
1945 19086 : }
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 3606297655 : size_int_kind (poly_int64 number, enum size_type_kind kind)
1964 : {
1965 3606297655 : 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 2703934487 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1975 : {
1976 2703934487 : tree type = TREE_TYPE (arg0);
1977 :
1978 2703934487 : if (arg0 == error_mark_node || arg1 == error_mark_node)
1979 : return error_mark_node;
1980 :
1981 2703934487 : 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 2703934487 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1986 : {
1987 : /* And some specific cases even faster than that. */
1988 2671034866 : if (code == PLUS_EXPR)
1989 : {
1990 1230674208 : if (integer_zerop (arg0)
1991 1230674208 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1992 : return arg1;
1993 316391675 : if (integer_zerop (arg1)
1994 316391675 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1995 : return arg0;
1996 : }
1997 1440360658 : else if (code == MINUS_EXPR)
1998 : {
1999 122895419 : if (integer_zerop (arg1)
2000 122895419 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2001 : return arg0;
2002 : }
2003 1317465239 : else if (code == MULT_EXPR)
2004 : {
2005 621100918 : if (integer_onep (arg0)
2006 621100918 : && !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 1290055521 : tree res = int_const_binop (code, arg0, arg1, -1);
2014 1290055521 : if (res != NULL_TREE)
2015 : return res;
2016 : }
2017 :
2018 32899621 : 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 38805289 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2027 : {
2028 38805289 : tree type = TREE_TYPE (arg0);
2029 38805289 : tree ctype;
2030 :
2031 38805289 : 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 38805289 : if (!TYPE_UNSIGNED (type))
2036 10448401 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2037 :
2038 28356888 : if (type == sizetype)
2039 28356888 : 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 28356888 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2049 17587 : return size_binop_loc (loc, MINUS_EXPR,
2050 : fold_convert_loc (loc, ctype, arg0),
2051 17587 : 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 28339301 : if (tree_int_cst_equal (arg0, arg1))
2058 25049966 : return build_int_cst (ctype, 0);
2059 3289335 : else if (tree_int_cst_lt (arg1, arg0))
2060 2155677 : return fold_convert_loc (loc, ctype,
2061 2155677 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2062 : else
2063 1133658 : 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 1458165189 : 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 1458165189 : tree arg1_type = TREE_TYPE (arg1);
2080 1458165189 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2081 1458165189 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2082 1458165189 : TYPE_SIGN (arg1_type)),
2083 : overflowable,
2084 1458165189 : 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 56599 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2092 : {
2093 56599 : bool overflow = false;
2094 56599 : 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 56599 : wide_int val;
2106 56599 : REAL_VALUE_TYPE r;
2107 56599 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2108 :
2109 56599 : switch (code)
2110 : {
2111 56599 : case FIX_TRUNC_EXPR:
2112 56599 : real_trunc (&r, VOIDmode, &x);
2113 56599 : 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 56599 : 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 56599 : if (! overflow)
2130 : {
2131 52961 : tree lt = TYPE_MIN_VALUE (type);
2132 52961 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2133 52961 : if (real_less (&r, &l))
2134 : {
2135 1974 : overflow = true;
2136 1974 : val = wi::to_wide (lt);
2137 : }
2138 : }
2139 :
2140 56599 : if (! overflow)
2141 : {
2142 50987 : tree ut = TYPE_MAX_VALUE (type);
2143 50987 : if (ut)
2144 : {
2145 50987 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2146 50987 : if (real_less (&u, &r))
2147 : {
2148 1921 : overflow = true;
2149 1921 : val = wi::to_wide (ut);
2150 : }
2151 : }
2152 : }
2153 :
2154 56599 : if (! overflow)
2155 49068 : 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 56599 : if (!flag_trapping_math || !overflow)
2165 49322 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2166 : else
2167 : t = NULL_TREE;
2168 :
2169 56599 : return t;
2170 56599 : }
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 2211956 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2225 : {
2226 2211956 : REAL_VALUE_TYPE value;
2227 2211956 : 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 2211956 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2233 : {
2234 99062 : t = build_real (type, TREE_REAL_CST (arg1));
2235 99062 : 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 2112894 : if (HONOR_SNANS (arg1)
2241 2114776 : && 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 2112894 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2247 2113550 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2248 509 : return NULL_TREE;
2249 :
2250 2112385 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2251 2112385 : 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 2112385 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2258 2252256 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2259 0 : TREE_OVERFLOW (t) = 1;
2260 2112385 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2261 2248014 : && !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 10558606 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2266 0 : && REAL_VALUE_ISINF (value)
2267 2112385 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2268 0 : TREE_OVERFLOW (t) = 1;
2269 : else
2270 2112385 : 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 1522748137 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2367 : {
2368 1522748137 : tree arg_type = TREE_TYPE (arg1);
2369 1522748137 : 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 1511360108 : 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 1511360108 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2383 : || TREE_CODE (type) == OFFSET_TYPE)
2384 : {
2385 1480048614 : if (TREE_CODE (arg1) == INTEGER_CST)
2386 1458165189 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2387 21883425 : else if (TREE_CODE (arg1) == REAL_CST)
2388 56599 : return fold_convert_const_int_from_real (code, type, arg1);
2389 21826826 : 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 31254660 : if (TREE_CODE (arg1) == INTEGER_CST)
2395 : {
2396 24035576 : 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 24035576 : 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 24033833 : return res;
2408 : }
2409 7219084 : else if (TREE_CODE (arg1) == REAL_CST)
2410 2211956 : return fold_convert_const_real_from_real (type, arg1);
2411 5007128 : 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 4707 : if (TREE_CODE (arg1) == VECTOR_CST
2426 4707 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2427 : {
2428 4707 : tree elttype = TREE_TYPE (type);
2429 4707 : 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 4707 : bool step_ok_p
2433 4707 : = (INTEGRAL_TYPE_P (elttype)
2434 331 : && INTEGRAL_TYPE_P (arg1_elttype)
2435 4980 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2436 4707 : tree_vector_builder v;
2437 4707 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2438 : return NULL_TREE;
2439 4707 : unsigned int len = v.encoded_nelts ();
2440 27827 : for (unsigned int i = 0; i < len; ++i)
2441 : {
2442 23120 : tree elt = VECTOR_CST_ELT (arg1, i);
2443 23120 : tree cvt = fold_convert_const (code, elttype, elt);
2444 23120 : if (cvt == NULL_TREE)
2445 0 : return NULL_TREE;
2446 23120 : v.quick_push (cvt);
2447 : }
2448 4707 : return v.build ();
2449 4707 : }
2450 : }
2451 12216 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2452 12216 : 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 17441 : build_zero_vector (tree type)
2460 : {
2461 17441 : tree t;
2462 :
2463 17441 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2464 17441 : 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 4399 : fold_convertible_p (const_tree type, const_tree arg)
2471 : {
2472 4399 : const_tree orig = TREE_TYPE (arg);
2473 :
2474 4399 : if (type == orig)
2475 : return true;
2476 :
2477 4399 : if (TREE_CODE (arg) == ERROR_MARK
2478 4399 : || TREE_CODE (type) == ERROR_MARK
2479 4399 : || TREE_CODE (orig) == ERROR_MARK)
2480 : return false;
2481 :
2482 4399 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2483 : return true;
2484 :
2485 4399 : switch (TREE_CODE (type))
2486 : {
2487 3819 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2488 3819 : case POINTER_TYPE: case REFERENCE_TYPE:
2489 3819 : case OFFSET_TYPE:
2490 3819 : return (INTEGRAL_TYPE_P (orig)
2491 383 : || (POINTER_TYPE_P (orig)
2492 248 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2493 3954 : || 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 2230472821 : fold_convert_loc (location_t loc, tree type, tree arg)
2516 : {
2517 2230472821 : tree orig = TREE_TYPE (arg);
2518 2230472821 : tree tem;
2519 :
2520 2230472821 : if (type == orig)
2521 : return arg;
2522 :
2523 1507032661 : if (TREE_CODE (arg) == ERROR_MARK
2524 1507031633 : || TREE_CODE (type) == ERROR_MARK
2525 1507031632 : || TREE_CODE (orig) == ERROR_MARK)
2526 1029 : return error_mark_node;
2527 :
2528 1507031632 : switch (TREE_CODE (type))
2529 : {
2530 116828632 : case POINTER_TYPE:
2531 116828632 : case REFERENCE_TYPE:
2532 : /* Handle conversions between pointers to different address spaces. */
2533 116828632 : if (POINTER_TYPE_P (orig)
2534 116828632 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2535 98101895 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2536 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2537 : /* fall through */
2538 :
2539 1475489323 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2540 1475489323 : case OFFSET_TYPE: case BITINT_TYPE:
2541 1475489323 : if (TREE_CODE (arg) == INTEGER_CST)
2542 : {
2543 1239424047 : tem = fold_convert_const (NOP_EXPR, type, arg);
2544 1239424047 : if (tem != NULL_TREE)
2545 : return tem;
2546 : }
2547 236065276 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2548 2510 : || TREE_CODE (orig) == OFFSET_TYPE)
2549 236065276 : 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 553823 : case REAL_TYPE:
2559 553823 : if (TREE_CODE (arg) == INTEGER_CST)
2560 : {
2561 57975 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2562 57975 : if (tem != NULL_TREE)
2563 : return tem;
2564 : }
2565 495848 : else if (TREE_CODE (arg) == REAL_CST)
2566 : {
2567 121386 : tem = fold_convert_const (NOP_EXPR, type, arg);
2568 121386 : if (tem != NULL_TREE)
2569 : return tem;
2570 : }
2571 374462 : 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 374464 : switch (TREE_CODE (orig))
2579 : {
2580 705 : case INTEGER_TYPE: case BITINT_TYPE:
2581 705 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2582 705 : case POINTER_TYPE: case REFERENCE_TYPE:
2583 705 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2584 :
2585 373759 : case REAL_TYPE:
2586 373759 : 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 2269 : case COMPLEX_TYPE:
2627 2269 : 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 1685 : case COMPLEX_TYPE:
2639 1685 : {
2640 1685 : tree rpart, ipart;
2641 :
2642 1685 : 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 151 : arg = save_expr (arg);
2652 151 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2653 151 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2654 151 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2655 151 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2656 151 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2657 : }
2658 :
2659 0 : default:
2660 0 : gcc_unreachable ();
2661 : }
2662 :
2663 30871408 : case VECTOR_TYPE:
2664 30871408 : if (integer_zerop (arg))
2665 17441 : return build_zero_vector (type);
2666 30853967 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 30853967 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2668 : || VECTOR_TYPE_P (orig));
2669 30853967 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2670 :
2671 110902 : case VOID_TYPE:
2672 110902 : tem = fold_ignored_result (arg);
2673 110902 : 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 3766 : default:
2680 3766 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2681 3766 : 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 53204922 : maybe_lvalue_p (const_tree x)
2694 : {
2695 : /* We only need to wrap lvalue tree codes. */
2696 53204922 : 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 39164820 : default:
2730 : /* Assume the worst for front-end tree codes. */
2731 39164820 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2732 : break;
2733 : return false;
2734 : }
2735 :
2736 14069894 : return true;
2737 : }
2738 :
2739 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2740 :
2741 : tree
2742 46686870 : 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 46686870 : if (in_gimple_form)
2747 : return x;
2748 :
2749 10594918 : if (! maybe_lvalue_p (x))
2750 : return x;
2751 2544067 : 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 131189322 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2761 : {
2762 131189322 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2763 1035386 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2764 : return ERROR_MARK;
2765 :
2766 130404888 : switch (code)
2767 : {
2768 : case EQ_EXPR:
2769 : return NE_EXPR;
2770 57582678 : case NE_EXPR:
2771 57582678 : return EQ_EXPR;
2772 12355035 : case GT_EXPR:
2773 12355035 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2774 17710417 : case GE_EXPR:
2775 17710417 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2776 8109030 : case LT_EXPR:
2777 8109030 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2778 8138892 : case LE_EXPR:
2779 8138892 : 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 222197 : case ORDERED_EXPR:
2793 222197 : return UNORDERED_EXPR;
2794 57733 : case UNORDERED_EXPR:
2795 57733 : 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 160278312 : swap_tree_comparison (enum tree_code code)
2806 : {
2807 160278312 : 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 39120591 : case GT_EXPR:
2817 39120591 : return LT_EXPR;
2818 11204111 : case GE_EXPR:
2819 11204111 : return LE_EXPR;
2820 22061644 : case LT_EXPR:
2821 22061644 : return GT_EXPR;
2822 16557831 : case LE_EXPR:
2823 16557831 : return GE_EXPR;
2824 247480 : case UNGT_EXPR:
2825 247480 : return UNLT_EXPR;
2826 19588 : case UNGE_EXPR:
2827 19588 : return UNLE_EXPR;
2828 372474 : case UNLT_EXPR:
2829 372474 : return UNGT_EXPR;
2830 111607 : case UNLE_EXPR:
2831 111607 : 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 399176 : comparison_to_compcode (enum tree_code code)
2844 : {
2845 399176 : 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 20965 : compcode_to_comparison (enum comparison_code code)
2886 : {
2887 20965 : 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 1781225 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2926 : {
2927 1781225 : return (COMPARISON_CLASS_P (cond1)
2928 1688992 : && COMPARISON_CLASS_P (cond2)
2929 1677653 : && (invert_tree_comparison
2930 1677653 : (TREE_CODE (cond1),
2931 3355306 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2932 70156 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2933 70156 : TREE_OPERAND (cond2, 0), 0)
2934 1803589 : && operand_equal_p (TREE_OPERAND (cond1, 1),
2935 22364 : TREE_OPERAND (cond2, 1), 0));
2936 : }
2937 :
2938 : /* Return a code 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 ERROR_MARK
2942 : if this makes the transformation invalid. If the resulting code is
2943 : INTEGER_CST, then *RES will be set to a non-NULL CONSTANT. */
2944 :
2945 : enum tree_code
2946 199588 : combine_comparisons (enum tree_code code, enum tree_code lcode,
2947 : enum tree_code rcode, tree truth_type,
2948 : bool honor_nans, tree *res)
2949 : {
2950 199588 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
2951 199588 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
2952 199588 : int compcode;
2953 199588 : *res = NULL_TREE;
2954 :
2955 199588 : switch (code)
2956 : {
2957 177436 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2958 177436 : case BIT_AND_EXPR:
2959 177436 : compcode = lcompcode & rcompcode;
2960 177436 : break;
2961 :
2962 22152 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2963 22152 : case BIT_IOR_EXPR:
2964 22152 : compcode = lcompcode | rcompcode;
2965 22152 : break;
2966 :
2967 : default:
2968 : return ERROR_MARK;
2969 : }
2970 :
2971 199588 : if (!honor_nans)
2972 : {
2973 : /* Eliminate unordered comparisons, as well as LTGT and ORD
2974 : which are not used unless the mode has NaNs. */
2975 34184 : compcode &= ~COMPCODE_UNORD;
2976 34184 : if (compcode == COMPCODE_LTGT)
2977 : compcode = COMPCODE_NE;
2978 31159 : else if (compcode == COMPCODE_ORD)
2979 5937 : compcode = COMPCODE_TRUE;
2980 : }
2981 165404 : else if (flag_trapping_math)
2982 : {
2983 : /* Check that the original operation and the optimized ones will trap
2984 : under the same condition. */
2985 328844 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2986 161921 : && (lcompcode != COMPCODE_EQ)
2987 164422 : && (lcompcode != COMPCODE_ORD);
2988 328844 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2989 156773 : && (rcompcode != COMPCODE_EQ)
2990 164422 : && (rcompcode != COMPCODE_ORD);
2991 328844 : bool trap = (compcode & COMPCODE_UNORD) == 0
2992 163320 : && (compcode != COMPCODE_EQ)
2993 164422 : && (compcode != COMPCODE_ORD);
2994 :
2995 : /* In a short-circuited boolean expression the LHS might be
2996 : such that the RHS, if evaluated, will never trap. For
2997 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
2998 : if neither x nor y is NaN. (This is a mixed blessing: for
2999 : example, the expression above will never trap, hence
3000 : optimizing it to x < y would be invalid). */
3001 164422 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3002 164065 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3003 164422 : rtrap = false;
3004 :
3005 : /* If the comparison was short-circuited, and only the RHS
3006 : trapped, we may now generate a spurious trap. */
3007 164422 : if (rtrap && !ltrap
3008 102 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3009 : return ERROR_MARK;
3010 :
3011 : /* If we changed the conditions that cause a trap, we lose. */
3012 164320 : if ((ltrap || rtrap) != trap)
3013 : return ERROR_MARK;
3014 : }
3015 :
3016 36649 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3017 : {
3018 15684 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3019 15684 : return INTEGER_CST;
3020 : }
3021 : else
3022 20965 : return compcode_to_comparison ((enum comparison_code) compcode);
3023 : }
3024 :
3025 : /* Return a tree for the comparison which is the combination of
3026 : doing the AND or OR (depending on CODE) of the two operations LCODE
3027 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3028 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3029 : if this makes the transformation invalid. */
3030 :
3031 : tree
3032 39303 : combine_comparisons (location_t loc,
3033 : enum tree_code code, enum tree_code lcode,
3034 : enum tree_code rcode, tree truth_type,
3035 : tree ll_arg, tree lr_arg)
3036 : {
3037 39303 : bool honor_nans = HONOR_NANS (ll_arg);
3038 39303 : tree_code rescode;
3039 39303 : tree res;
3040 39303 : rescode = combine_comparisons (code, lcode, rcode, truth_type,
3041 : honor_nans, &res);
3042 39303 : if (rescode == ERROR_MARK)
3043 : return NULL_TREE;
3044 35782 : if (rescode == INTEGER_CST)
3045 15684 : return res;
3046 :
3047 20098 : return fold_build2_loc (loc, rescode, truth_type, ll_arg, lr_arg);
3048 : }
3049 :
3050 : /* Return nonzero if two operands (typically of the same tree node)
3051 : are necessarily equal. FLAGS modifies behavior as follows:
3052 :
3053 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3054 : This function tests whether the operands are indistinguishable;
3055 : it does not test whether they are equal using C's == operation.
3056 : The distinction is important for IEEE floating point, because
3057 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3058 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3059 :
3060 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3061 : even though it may hold multiple values during a function.
3062 : This is because a GCC tree node guarantees that nothing else is
3063 : executed between the evaluation of its "operands" (which may often
3064 : be evaluated in arbitrary order). Hence if the operands themselves
3065 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3066 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3067 : unset means assuming isochronic (or instantaneous) tree equivalence.
3068 : Unless comparing arbitrary expression trees, such as from different
3069 : statements, this flag can usually be left unset.
3070 :
3071 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3072 : are considered the same. It is used when the caller has other ways
3073 : to ensure that global memory is unchanged in between.
3074 :
3075 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3076 : not values of expressions.
3077 :
3078 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3079 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3080 :
3081 : If OEP_BITWISE is set, then require the values to be bitwise identical
3082 : rather than simply numerically equal. Do not take advantage of things
3083 : like math-related flags or undefined behavior; only return true for
3084 : values that are provably bitwise identical in all circumstances.
3085 :
3086 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3087 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3088 : rather than just numerically equivalent. The compared expressions must
3089 : however perform the same operations but may do intermediate computations in
3090 : differing signs. Because this comparison ignores any possible UB it cannot
3091 : be used blindly without ensuring that the context you are using it in itself
3092 : doesn't guarantee that there will be no UB. Conditional expressions are
3093 : excluded from this relaxation.
3094 :
3095 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3096 : differing hashes even for cases where operand_compare::operand_equal_p
3097 : compares equal.
3098 :
3099 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3100 : any operand with side effect. This is unnecessarily conservative in the
3101 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3102 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3103 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3104 : even if var is volatile. */
3105 :
3106 : bool
3107 7276870288 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3108 : unsigned int flags)
3109 : {
3110 7276870288 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3111 : }
3112 :
3113 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3114 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3115 : expression is being compared under for equality. This means that they can
3116 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3117 :
3118 : bool
3119 7277611737 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3120 : tree type1, const_tree arg1,
3121 : unsigned int flags)
3122 : {
3123 7277611737 : bool r;
3124 7277611737 : if (verify_hash_value (arg0, arg1, flags, &r))
3125 3061063737 : return r;
3126 :
3127 4216548000 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3128 4216548000 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3129 :
3130 : /* If either is ERROR_MARK, they aren't equal. */
3131 4216548000 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3132 4216547388 : || type0 == error_mark_node
3133 4216547386 : || type1 == error_mark_node)
3134 : return false;
3135 :
3136 : /* Similar, if either does not have a type (like a template id),
3137 : they aren't equal. */
3138 4216547385 : if (!type0 || !type1)
3139 : return false;
3140 :
3141 : /* Bitwise identity makes no sense if the values have different layouts. */
3142 4216542230 : if ((flags & OEP_BITWISE)
3143 4216542230 : && !tree_nop_conversion_p (type0, type1))
3144 : return false;
3145 :
3146 : /* We cannot consider pointers to different address space equal. */
3147 4216542230 : if (POINTER_TYPE_P (type0)
3148 645207213 : && POINTER_TYPE_P (type1)
3149 4769432311 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3150 552890081 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3151 : return false;
3152 :
3153 : /* Check equality of integer constants before bailing out due to
3154 : precision differences. */
3155 4216541989 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3156 : {
3157 : /* Address of INTEGER_CST is not defined; check that we did not forget
3158 : to drop the OEP_ADDRESS_OF flags. */
3159 663604465 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3160 663604465 : return tree_int_cst_equal (arg0, arg1);
3161 : }
3162 :
3163 3552937524 : if ((flags & OEP_ASSUME_WRAPV)
3164 2103203 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3165 : {
3166 794798 : const_tree t_arg0 = arg0;
3167 794798 : const_tree t_arg1 = arg1;
3168 794798 : STRIP_NOPS (arg0);
3169 794798 : STRIP_NOPS (arg1);
3170 : /* Only recurse if the conversion was one that was valid to strip. */
3171 794798 : if (t_arg0 != arg0 || t_arg1 != arg1)
3172 741449 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3173 : }
3174 :
3175 3552196075 : if (!(flags & OEP_ADDRESS_OF))
3176 : {
3177 : /* Check if we are checking an operation where the two's compliment
3178 : bitwise representation of the result is not the same between signed and
3179 : unsigned arithmetic. */
3180 3147647219 : bool enforce_signedness = true;
3181 3147647219 : if (flags & OEP_ASSUME_WRAPV)
3182 : {
3183 1267338 : switch (TREE_CODE (arg0))
3184 : {
3185 : case PLUS_EXPR:
3186 : case MINUS_EXPR:
3187 : case MULT_EXPR:
3188 : case BIT_IOR_EXPR:
3189 : case BIT_XOR_EXPR:
3190 : case BIT_AND_EXPR:
3191 : case BIT_NOT_EXPR:
3192 : case ABS_EXPR:
3193 : CASE_CONVERT:
3194 : case SSA_NAME:
3195 : case INTEGER_CST:
3196 : case VAR_DECL:
3197 : case PARM_DECL:
3198 : case RESULT_DECL:
3199 3147647219 : enforce_signedness = false;
3200 : break;
3201 :
3202 : default:
3203 : break;
3204 : }
3205 : }
3206 :
3207 : /* If both types don't have the same signedness, then we can't consider
3208 : them equal. We must check this before the STRIP_NOPS calls
3209 : because they may change the signedness of the arguments. As pointers
3210 : strictly don't have a signedness, require either two pointers or
3211 : two non-pointers as well. */
3212 3147647219 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3213 3147647219 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3214 144667897 : && enforce_signedness))
3215 : return false;
3216 :
3217 : /* If both types don't have the same precision, then it is not safe
3218 : to strip NOPs. */
3219 2847921415 : if (element_precision (type0) != element_precision (type1))
3220 : return false;
3221 :
3222 2696176790 : STRIP_NOPS (arg0);
3223 2696176790 : STRIP_NOPS (arg1);
3224 :
3225 2696176790 : type0 = TREE_TYPE (arg0);
3226 2696176790 : type1 = TREE_TYPE (arg1);
3227 : }
3228 : #if 0
3229 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3230 : sanity check once the issue is solved. */
3231 : else
3232 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3233 : are not defined. Check that we did not forget to drop the
3234 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3235 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3236 : && TREE_CODE (arg0) != SSA_NAME);
3237 : #endif
3238 :
3239 : /* In case both args are comparisons but with different comparison
3240 : code, try to swap the comparison operands of one arg to produce
3241 : a match and compare that variant. */
3242 3100725646 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3243 1250811238 : && COMPARISON_CLASS_P (arg0)
3244 6795580 : && COMPARISON_CLASS_P (arg1))
3245 : {
3246 5066530 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3247 :
3248 5066530 : if (TREE_CODE (arg0) == swap_code)
3249 2158857 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3250 2158857 : TREE_OPERAND (arg1, 1), flags)
3251 2178545 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3252 19688 : TREE_OPERAND (arg1, 0), flags);
3253 : }
3254 :
3255 3098566789 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3256 : {
3257 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3258 1248652381 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3259 : ;
3260 1248588117 : else if (flags & OEP_ADDRESS_OF)
3261 : {
3262 : /* If we are interested in comparing addresses ignore
3263 : MEM_REF wrappings of the base that can appear just for
3264 : TBAA reasons. */
3265 49393186 : if (TREE_CODE (arg0) == MEM_REF
3266 8103963 : && DECL_P (arg1)
3267 5697597 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3268 1312872 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3269 50088624 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3270 : return true;
3271 49164370 : else if (TREE_CODE (arg1) == MEM_REF
3272 30739948 : && DECL_P (arg0)
3273 11200086 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3274 2309907 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3275 49740565 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3276 : return true;
3277 48775354 : return false;
3278 : }
3279 : else
3280 : return false;
3281 : }
3282 :
3283 : /* When not checking addresses, this is needed for conversions and for
3284 : COMPONENT_REF. Might as well play it safe and always test this. */
3285 1849978672 : if (TREE_CODE (type0) == ERROR_MARK
3286 1849978672 : || TREE_CODE (type1) == ERROR_MARK
3287 3699957344 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3288 26059029 : && !(flags & OEP_ADDRESS_OF)))
3289 3904337 : return false;
3290 :
3291 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3292 : We don't care about side effects in that case because the SAVE_EXPR
3293 : takes care of that for us. In all other cases, two expressions are
3294 : equal if they have no side effects. If we have two identical
3295 : expressions with side effects that should be treated the same due
3296 : to the only side effects being identical SAVE_EXPR's, that will
3297 : be detected in the recursive calls below.
3298 : If we are taking an invariant address of two identical objects
3299 : they are necessarily equal as well. */
3300 329351365 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3301 2175425522 : && (TREE_CODE (arg0) == SAVE_EXPR
3302 329325823 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3303 289836945 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3304 : return true;
3305 :
3306 : /* Next handle constant cases, those for which we can return 1 even
3307 : if ONLY_CONST is set. */
3308 1516889845 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3309 25770649 : switch (TREE_CODE (arg0))
3310 : {
3311 151 : case INTEGER_CST:
3312 151 : return tree_int_cst_equal (arg0, arg1);
3313 :
3314 0 : case FIXED_CST:
3315 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3316 : TREE_FIXED_CST (arg1));
3317 :
3318 3762979 : case REAL_CST:
3319 3762979 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3320 : return true;
3321 :
3322 2734240 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3323 : {
3324 : /* If we do not distinguish between signed and unsigned zero,
3325 : consider them equal. */
3326 14687 : if (real_zerop (arg0) && real_zerop (arg1))
3327 : return true;
3328 : }
3329 2734231 : return false;
3330 :
3331 1016092 : case VECTOR_CST:
3332 1016092 : {
3333 1016092 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3334 1016092 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3335 : return false;
3336 :
3337 994415 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3338 994415 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3339 : return false;
3340 :
3341 959577 : unsigned int count = vector_cst_encoded_nelts (arg0);
3342 1338434 : for (unsigned int i = 0; i < count; ++i)
3343 2186446 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3344 1093223 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3345 : return false;
3346 : return true;
3347 : }
3348 :
3349 13969 : case COMPLEX_CST:
3350 13969 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3351 : flags)
3352 13969 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3353 : flags));
3354 :
3355 985629 : case STRING_CST:
3356 985629 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3357 985629 : && ! memcmp (TREE_STRING_POINTER (arg0),
3358 563519 : TREE_STRING_POINTER (arg1),
3359 563519 : TREE_STRING_LENGTH (arg0)));
3360 :
3361 0 : case RAW_DATA_CST:
3362 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3363 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3364 0 : RAW_DATA_POINTER (arg1),
3365 0 : RAW_DATA_LENGTH (arg0)));
3366 :
3367 18818404 : case ADDR_EXPR:
3368 18818404 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3369 18818404 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3370 : flags | OEP_ADDRESS_OF
3371 18818404 : | OEP_MATCH_SIDE_EFFECTS);
3372 183246 : case CONSTRUCTOR:
3373 183246 : {
3374 : /* In GIMPLE empty constructors are allowed in initializers of
3375 : aggregates. */
3376 183246 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3377 : return true;
3378 :
3379 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3380 138140 : if (TREE_CODE (type0) != TREE_CODE (type1))
3381 : return false;
3382 138140 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3383 : {
3384 : /* For arrays, check that the sizes all match. */
3385 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3386 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3387 264 : || siz0 < 0
3388 528 : || siz0 != int_size_in_bytes (type1))
3389 0 : return false;
3390 : }
3391 137876 : else if (!types_compatible_p (type0, type1))
3392 : return false;
3393 :
3394 138140 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3395 138140 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3396 414420 : if (vec_safe_length (v0) != vec_safe_length (v1))
3397 : return false;
3398 :
3399 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3400 : of the CONSTRUCTOR referenced indirectly. */
3401 138140 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3402 :
3403 365750690 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3404 : {
3405 206329 : constructor_elt *c0 = &(*v0)[idx];
3406 206329 : constructor_elt *c1 = &(*v1)[idx];
3407 :
3408 : /* Check that the values are the same... */
3409 206329 : if (c0->value != c1->value
3410 206329 : && !operand_equal_p (c0->value, c1->value, flags))
3411 : return false;
3412 :
3413 : /* ... and that they apply to the same field! */
3414 117765 : if (c0->index != c1->index
3415 117765 : && (TREE_CODE (type0) == ARRAY_TYPE
3416 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3417 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3418 0 : DECL_FIELD_OFFSET (c1->index),
3419 : flags)
3420 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3421 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3422 : flags)))
3423 0 : return false;
3424 : }
3425 :
3426 : return true;
3427 : }
3428 :
3429 : default:
3430 : break;
3431 : }
3432 :
3433 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3434 : two instances of undefined behavior will give identical results. */
3435 1492109375 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3436 : return false;
3437 :
3438 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3439 : variant that allows null and views null as being different from any
3440 : non-null value. In the latter case, if either is null, the both
3441 : must be; otherwise, do the normal comparison. */
3442 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3443 : TREE_OPERAND (arg1, N), flags)
3444 :
3445 : #define OP_SAME_WITH_NULL(N) \
3446 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3447 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3448 :
3449 1492109375 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3450 : {
3451 8549268 : case tcc_unary:
3452 : /* Two conversions are equal only if signedness and modes match. */
3453 8549268 : switch (TREE_CODE (arg0))
3454 : {
3455 8167414 : CASE_CONVERT:
3456 8167414 : case FIX_TRUNC_EXPR:
3457 8167414 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3458 : return false;
3459 : break;
3460 : default:
3461 : break;
3462 : }
3463 :
3464 8549247 : return OP_SAME_WITH_NULL (0);
3465 :
3466 :
3467 23037143 : case tcc_comparison:
3468 23037143 : case tcc_binary:
3469 23037143 : if (OP_SAME (0) && OP_SAME (1))
3470 : return true;
3471 :
3472 : /* For commutative ops, allow the other order. */
3473 16798937 : return (commutative_tree_code (TREE_CODE (arg0))
3474 12693612 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3475 12693612 : TREE_OPERAND (arg1, 1), flags)
3476 17018363 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3477 219426 : TREE_OPERAND (arg1, 0), flags));
3478 :
3479 877939528 : case tcc_reference:
3480 : /* If either of the pointer (or reference) expressions we are
3481 : dereferencing contain a side effect, these cannot be equal,
3482 : but their addresses can be. */
3483 877939528 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3484 877939528 : && (TREE_SIDE_EFFECTS (arg0)
3485 810117923 : || TREE_SIDE_EFFECTS (arg1)))
3486 : return false;
3487 :
3488 877552890 : switch (TREE_CODE (arg0))
3489 : {
3490 5267638 : case INDIRECT_REF:
3491 5267638 : if (!(flags & OEP_ADDRESS_OF))
3492 : {
3493 5245204 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3494 : return false;
3495 : /* Verify that the access types are compatible. */
3496 5239559 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3497 : return false;
3498 : }
3499 5194894 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3500 5194894 : return OP_SAME (0);
3501 :
3502 575579 : case IMAGPART_EXPR:
3503 : /* Require the same offset. */
3504 575579 : if (!operand_equal_p (TYPE_SIZE (type0),
3505 575579 : TYPE_SIZE (type1),
3506 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3507 : return false;
3508 :
3509 : /* Fallthru. */
3510 2301714 : case REALPART_EXPR:
3511 2301714 : case VIEW_CONVERT_EXPR:
3512 2301714 : return OP_SAME (0);
3513 :
3514 86710004 : case TARGET_MEM_REF:
3515 86710004 : case MEM_REF:
3516 86710004 : if (!(flags & OEP_ADDRESS_OF))
3517 : {
3518 : /* Require equal access sizes */
3519 17044927 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3520 17044927 : && (!TYPE_SIZE (type0)
3521 1254712 : || !TYPE_SIZE (type1)
3522 1247840 : || !operand_equal_p (TYPE_SIZE (type0),
3523 1247840 : TYPE_SIZE (type1),
3524 : flags)))
3525 1259391 : return false;
3526 : /* Verify that access happens in similar types. */
3527 15785536 : if (!types_compatible_p (type0, type1))
3528 : return false;
3529 : /* Verify that accesses are TBAA compatible. */
3530 15450255 : if (!alias_ptr_types_compatible_p
3531 15450255 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3532 15450255 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3533 14547402 : || (MR_DEPENDENCE_CLIQUE (arg0)
3534 14547402 : != MR_DEPENDENCE_CLIQUE (arg1))
3535 28246547 : || (MR_DEPENDENCE_BASE (arg0)
3536 12796292 : != MR_DEPENDENCE_BASE (arg1)))
3537 : return false;
3538 : /* Verify that alignment is compatible. */
3539 12278583 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3540 : return false;
3541 : }
3542 81732869 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3543 139642432 : return (OP_SAME (0) && OP_SAME (1)
3544 : /* TARGET_MEM_REF require equal extra operands. */
3545 106780596 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3546 577421 : || (OP_SAME_WITH_NULL (2)
3547 270366 : && OP_SAME_WITH_NULL (3)
3548 264904 : && OP_SAME_WITH_NULL (4))));
3549 :
3550 35883508 : case ARRAY_REF:
3551 35883508 : case ARRAY_RANGE_REF:
3552 35883508 : if (!OP_SAME (0))
3553 : return false;
3554 31007588 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3555 : /* Compare the array index by value if it is constant first as we
3556 : may have different types but same value here. */
3557 31007588 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3558 31007588 : TREE_OPERAND (arg1, 1))
3559 27915205 : || OP_SAME (1))
3560 6213482 : && OP_SAME_WITH_NULL (2)
3561 6212162 : && OP_SAME_WITH_NULL (3)
3562 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3563 : we have to account for the offset of the ref. */
3564 40326491 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3565 3106081 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3566 2736 : || (operand_equal_p (array_ref_low_bound
3567 2736 : (const_cast<tree> (arg0)),
3568 : array_ref_low_bound
3569 2736 : (const_cast<tree> (arg1)),
3570 : flags)
3571 2736 : && operand_equal_p (array_ref_element_size
3572 2736 : (const_cast<tree> (arg0)),
3573 : array_ref_element_size
3574 2736 : (const_cast<tree> (arg1)),
3575 : flags))));
3576 :
3577 746757452 : case COMPONENT_REF:
3578 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3579 : may be NULL when we're called to compare MEM_EXPRs. */
3580 746757452 : if (!OP_SAME_WITH_NULL (0))
3581 : return false;
3582 57941968 : {
3583 57941968 : bool compare_address = flags & OEP_ADDRESS_OF;
3584 :
3585 : /* Most of time we only need to compare FIELD_DECLs for equality.
3586 : However when determining address look into actual offsets.
3587 : These may match for unions and unshared record types. */
3588 57941968 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3589 57941968 : if (!OP_SAME (1))
3590 : {
3591 33749383 : if (compare_address
3592 641277 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3593 : {
3594 641274 : tree field0 = TREE_OPERAND (arg0, 1);
3595 641274 : tree field1 = TREE_OPERAND (arg1, 1);
3596 :
3597 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3598 641274 : if (TREE_CODE (field0) != FIELD_DECL
3599 641274 : || TREE_CODE (field1) != FIELD_DECL)
3600 : return false;
3601 :
3602 641274 : if (!DECL_FIELD_OFFSET (field0)
3603 641274 : || !DECL_FIELD_OFFSET (field1))
3604 3 : return field0 == field1;
3605 :
3606 641271 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3607 641271 : DECL_FIELD_OFFSET (field1), flags)
3608 831196 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3609 189925 : DECL_FIELD_BIT_OFFSET (field1),
3610 : flags))
3611 601055 : return false;
3612 : }
3613 : else
3614 : return false;
3615 : }
3616 : }
3617 24232801 : return OP_SAME_WITH_NULL (2);
3618 :
3619 632478 : case BIT_FIELD_REF:
3620 632478 : if (!OP_SAME (0))
3621 : return false;
3622 367479 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3623 367479 : return OP_SAME (1) && OP_SAME (2);
3624 :
3625 : default:
3626 : return false;
3627 : }
3628 :
3629 59650027 : case tcc_expression:
3630 59650027 : switch (TREE_CODE (arg0))
3631 : {
3632 54288737 : case ADDR_EXPR:
3633 : /* Be sure we pass right ADDRESS_OF flag. */
3634 54288737 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3635 54288737 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3636 54288737 : TREE_OPERAND (arg1, 0),
3637 54288737 : flags | OEP_ADDRESS_OF);
3638 :
3639 609232 : case TRUTH_NOT_EXPR:
3640 609232 : return OP_SAME (0);
3641 :
3642 79255 : case TRUTH_ANDIF_EXPR:
3643 79255 : case TRUTH_ORIF_EXPR:
3644 79255 : return OP_SAME (0) && OP_SAME (1);
3645 :
3646 0 : case WIDEN_MULT_PLUS_EXPR:
3647 0 : case WIDEN_MULT_MINUS_EXPR:
3648 0 : if (!OP_SAME (2))
3649 : return false;
3650 : /* The multiplication operands are commutative. */
3651 : /* FALLTHRU */
3652 :
3653 47667 : case TRUTH_AND_EXPR:
3654 47667 : case TRUTH_OR_EXPR:
3655 47667 : case TRUTH_XOR_EXPR:
3656 47667 : if (OP_SAME (0) && OP_SAME (1))
3657 : return true;
3658 :
3659 : /* Otherwise take into account this is a commutative operation. */
3660 47649 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3661 47649 : TREE_OPERAND (arg1, 1), flags)
3662 47652 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3663 3 : TREE_OPERAND (arg1, 0), flags));
3664 :
3665 214749 : case COND_EXPR:
3666 214749 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3667 45490 : return false;
3668 169259 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3669 169259 : return OP_SAME (0);
3670 :
3671 4 : case BIT_INSERT_EXPR:
3672 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3673 : of op1. Need to check to make sure they are the same. */
3674 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3675 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3676 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3677 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3678 : return false;
3679 : /* FALLTHRU */
3680 :
3681 367 : case VEC_COND_EXPR:
3682 367 : case DOT_PROD_EXPR:
3683 367 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3684 :
3685 38403 : case MODIFY_EXPR:
3686 38403 : case INIT_EXPR:
3687 38403 : case COMPOUND_EXPR:
3688 38403 : case PREDECREMENT_EXPR:
3689 38403 : case PREINCREMENT_EXPR:
3690 38403 : case POSTDECREMENT_EXPR:
3691 38403 : case POSTINCREMENT_EXPR:
3692 38403 : if (flags & OEP_LEXICOGRAPHIC)
3693 165 : return OP_SAME (0) && OP_SAME (1);
3694 : return false;
3695 :
3696 317596 : case CLEANUP_POINT_EXPR:
3697 317596 : case EXPR_STMT:
3698 317596 : case SAVE_EXPR:
3699 317596 : if (flags & OEP_LEXICOGRAPHIC)
3700 208 : return OP_SAME (0);
3701 : return false;
3702 :
3703 79871 : case OBJ_TYPE_REF:
3704 : /* Virtual table reference. */
3705 159742 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3706 79871 : OBJ_TYPE_REF_EXPR (arg1), flags))
3707 : return false;
3708 14640 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3709 14640 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3710 14640 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3711 : return false;
3712 14640 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3713 14640 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3714 : return false;
3715 14640 : if (virtual_method_call_p (arg0))
3716 : {
3717 14640 : if (!virtual_method_call_p (arg1))
3718 : return false;
3719 14640 : return types_same_for_odr (obj_type_ref_class (arg0),
3720 29280 : obj_type_ref_class (arg1));
3721 : }
3722 : return false;
3723 :
3724 598 : case OMP_ARRAY_SECTION:
3725 598 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3726 :
3727 : default:
3728 : return false;
3729 : }
3730 :
3731 3781410 : case tcc_vl_exp:
3732 3781410 : switch (TREE_CODE (arg0))
3733 : {
3734 3781410 : case CALL_EXPR:
3735 3781410 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3736 3781410 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3737 : /* If not both CALL_EXPRs are either internal or normal function
3738 : functions, then they are not equal. */
3739 : return false;
3740 3781410 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3741 : {
3742 : /* If the CALL_EXPRs call different internal functions, then they
3743 : are not equal. */
3744 2 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3745 : return false;
3746 : }
3747 : else
3748 : {
3749 : /* If the CALL_EXPRs call different functions, then they are not
3750 : equal. */
3751 3781408 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3752 : flags))
3753 : return false;
3754 : }
3755 :
3756 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3757 2212877 : {
3758 2212877 : unsigned int cef = call_expr_flags (arg0);
3759 2212877 : if (flags & OEP_PURE_SAME)
3760 0 : cef &= ECF_CONST | ECF_PURE;
3761 : else
3762 2212877 : cef &= ECF_CONST;
3763 2212877 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3764 : return false;
3765 : }
3766 :
3767 : /* Now see if all the arguments are the same. */
3768 34476 : {
3769 34476 : const_call_expr_arg_iterator iter0, iter1;
3770 34476 : const_tree a0, a1;
3771 68952 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3772 34476 : a1 = first_const_call_expr_arg (arg1, &iter1);
3773 42589 : a0 && a1;
3774 8113 : a0 = next_const_call_expr_arg (&iter0),
3775 8113 : a1 = next_const_call_expr_arg (&iter1))
3776 35971 : if (! operand_equal_p (a0, a1, flags))
3777 : return false;
3778 :
3779 : /* If we get here and both argument lists are exhausted
3780 : then the CALL_EXPRs are equal. */
3781 6618 : return ! (a0 || a1);
3782 : }
3783 : default:
3784 : return false;
3785 : }
3786 :
3787 171234012 : case tcc_declaration:
3788 : /* Consider __builtin_sqrt equal to sqrt. */
3789 171234012 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3790 6894019 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3791 265650 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3792 6296661 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3793 265650 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3794 :
3795 164937351 : if (DECL_P (arg0)
3796 164937351 : && (flags & OEP_DECL_NAME)
3797 35 : && (flags & OEP_LEXICOGRAPHIC))
3798 : {
3799 : /* Consider decls with the same name equal. The caller needs
3800 : to make sure they refer to the same entity (such as a function
3801 : formal parameter). */
3802 35 : tree a0name = DECL_NAME (arg0);
3803 35 : tree a1name = DECL_NAME (arg1);
3804 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3805 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3806 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3807 : }
3808 : return false;
3809 :
3810 345303067 : case tcc_exceptional:
3811 345303067 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3812 : {
3813 19797 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3814 : return false;
3815 :
3816 : /* In GIMPLE constructors are used only to build vectors from
3817 : elements. Individual elements in the constructor must be
3818 : indexed in increasing order and form an initial sequence.
3819 :
3820 : We make no effort to compare nonconstant ones in GENERIC. */
3821 19797 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3822 : return false;
3823 :
3824 : /* Be sure that vectors constructed have the same representation.
3825 : We only tested element precision and modes to match.
3826 : Vectors may be BLKmode and thus also check that the number of
3827 : parts match. */
3828 1007 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3829 2014 : TYPE_VECTOR_SUBPARTS (type1)))
3830 : return false;
3831 :
3832 1007 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3833 1007 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3834 1007 : unsigned int len = vec_safe_length (v0);
3835 :
3836 2014 : if (len != vec_safe_length (v1))
3837 : return false;
3838 :
3839 4497 : for (unsigned int i = 0; i < len; i++)
3840 : {
3841 3861 : constructor_elt *c0 = &(*v0)[i];
3842 3861 : constructor_elt *c1 = &(*v1)[i];
3843 :
3844 3861 : if (!operand_equal_p (c0->value, c1->value, flags)
3845 : /* In GIMPLE the indexes can be either NULL or matching i.
3846 : Double check this so we won't get false
3847 : positives for GENERIC. */
3848 3520 : || (c0->index
3849 2688 : && (TREE_CODE (c0->index) != INTEGER_CST
3850 2688 : || compare_tree_int (c0->index, i)))
3851 7381 : || (c1->index
3852 2688 : && (TREE_CODE (c1->index) != INTEGER_CST
3853 2688 : || compare_tree_int (c1->index, i))))
3854 341 : return false;
3855 : }
3856 : return true;
3857 : }
3858 345283270 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3859 3283 : && (flags & OEP_LEXICOGRAPHIC))
3860 : {
3861 : /* Compare the STATEMENT_LISTs. */
3862 16 : tree_stmt_iterator tsi1, tsi2;
3863 16 : tree body1 = const_cast<tree> (arg0);
3864 16 : tree body2 = const_cast<tree> (arg1);
3865 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3866 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3867 : {
3868 : /* The lists don't have the same number of statements. */
3869 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3870 : return false;
3871 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3872 : return true;
3873 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3874 : flags & (OEP_LEXICOGRAPHIC
3875 : | OEP_NO_HASH_CHECK)))
3876 : return false;
3877 : }
3878 : }
3879 : return false;
3880 :
3881 2614730 : case tcc_statement:
3882 2614730 : switch (TREE_CODE (arg0))
3883 : {
3884 52 : case RETURN_EXPR:
3885 52 : if (flags & OEP_LEXICOGRAPHIC)
3886 52 : return OP_SAME_WITH_NULL (0);
3887 : return false;
3888 4 : case DEBUG_BEGIN_STMT:
3889 4 : if (flags & OEP_LEXICOGRAPHIC)
3890 : return true;
3891 : return false;
3892 : default:
3893 : return false;
3894 : }
3895 :
3896 : default:
3897 : return false;
3898 : }
3899 :
3900 : #undef OP_SAME
3901 : #undef OP_SAME_WITH_NULL
3902 : }
3903 :
3904 : /* Generate a hash value for an expression. This can be used iteratively
3905 : by passing a previous result as the HSTATE argument. */
3906 :
3907 : void
3908 3039072170 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3909 : unsigned int flags)
3910 : {
3911 3039072170 : int i;
3912 3039072170 : enum tree_code code;
3913 3039072170 : enum tree_code_class tclass;
3914 :
3915 3039072170 : if (t == NULL_TREE || t == error_mark_node)
3916 : {
3917 77828145 : hstate.merge_hash (0);
3918 77828145 : return;
3919 : }
3920 :
3921 2961244025 : STRIP_ANY_LOCATION_WRAPPER (t);
3922 :
3923 2961244025 : if (!(flags & OEP_ADDRESS_OF))
3924 2705053525 : STRIP_NOPS (t);
3925 :
3926 2961244025 : code = TREE_CODE (t);
3927 :
3928 2961244025 : switch (code)
3929 : {
3930 : /* Alas, constants aren't shared, so we can't rely on pointer
3931 : identity. */
3932 798 : case VOID_CST:
3933 798 : hstate.merge_hash (0);
3934 798 : return;
3935 881439888 : case INTEGER_CST:
3936 881439888 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3937 1781620700 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3938 900180812 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3939 : return;
3940 15689605 : case REAL_CST:
3941 15689605 : {
3942 15689605 : unsigned int val2;
3943 15689605 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3944 : val2 = rvc_zero;
3945 : else
3946 15475409 : val2 = real_hash (TREE_REAL_CST_PTR (t));
3947 15689605 : hstate.merge_hash (val2);
3948 15689605 : return;
3949 : }
3950 0 : case FIXED_CST:
3951 0 : {
3952 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
3953 0 : hstate.merge_hash (val2);
3954 0 : return;
3955 : }
3956 12736800 : case STRING_CST:
3957 12736800 : hstate.add ((const void *) TREE_STRING_POINTER (t),
3958 12736800 : TREE_STRING_LENGTH (t));
3959 12736800 : return;
3960 209 : case RAW_DATA_CST:
3961 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
3962 209 : RAW_DATA_LENGTH (t));
3963 209 : return;
3964 210828 : case COMPLEX_CST:
3965 210828 : hash_operand (TREE_REALPART (t), hstate, flags);
3966 210828 : hash_operand (TREE_IMAGPART (t), hstate, flags);
3967 210828 : return;
3968 3432609 : case VECTOR_CST:
3969 3432609 : {
3970 3432609 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
3971 3432609 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3972 3432609 : unsigned int count = vector_cst_encoded_nelts (t);
3973 10717500 : for (unsigned int i = 0; i < count; ++i)
3974 7284891 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3975 : return;
3976 : }
3977 895019489 : case SSA_NAME:
3978 : /* We can just compare by pointer. */
3979 895019489 : hstate.add_hwi (SSA_NAME_VERSION (t));
3980 895019489 : return;
3981 : case PLACEHOLDER_EXPR:
3982 : /* The node itself doesn't matter. */
3983 : return;
3984 : case BLOCK:
3985 : case OMP_CLAUSE:
3986 : case OMP_NEXT_VARIANT:
3987 : case OMP_TARGET_DEVICE_MATCHES:
3988 : /* Ignore. */
3989 : return;
3990 : case TREE_LIST:
3991 : /* A list of expressions, for a CALL_EXPR or as the elements of a
3992 : VECTOR_CST. */
3993 300584 : for (; t; t = TREE_CHAIN (t))
3994 150292 : hash_operand (TREE_VALUE (t), hstate, flags);
3995 : return;
3996 4940835 : case CONSTRUCTOR:
3997 4940835 : {
3998 4940835 : unsigned HOST_WIDE_INT idx;
3999 4940835 : tree field, value;
4000 4940835 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4001 4940835 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4002 19906751 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4003 : {
4004 : /* In GIMPLE the indexes can be either NULL or matching i. */
4005 14965916 : if (field == NULL_TREE)
4006 1103048 : field = bitsize_int (idx);
4007 14965916 : if (TREE_CODE (field) == FIELD_DECL)
4008 : {
4009 9969414 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4010 9969414 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4011 : }
4012 : else
4013 4996502 : hash_operand (field, hstate, flags);
4014 14965916 : hash_operand (value, hstate, flags);
4015 : }
4016 : return;
4017 : }
4018 182 : case STATEMENT_LIST:
4019 182 : {
4020 182 : tree_stmt_iterator i;
4021 182 : for (i = tsi_start (const_cast<tree> (t));
4022 550 : !tsi_end_p (i); tsi_next (&i))
4023 368 : hash_operand (tsi_stmt (i), hstate, flags);
4024 182 : return;
4025 : }
4026 : case TREE_VEC:
4027 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4028 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4029 : return;
4030 4 : case IDENTIFIER_NODE:
4031 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4032 4 : return;
4033 21346537 : case FUNCTION_DECL:
4034 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4035 : Otherwise nodes that compare equal according to operand_equal_p might
4036 : get different hash codes. However, don't do this for machine specific
4037 : or front end builtins, since the function code is overloaded in those
4038 : cases. */
4039 21346537 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4040 21346537 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4041 : {
4042 7158839 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4043 7158839 : code = TREE_CODE (t);
4044 : }
4045 : /* FALL THROUGH */
4046 1147622359 : default:
4047 1147622359 : if (POLY_INT_CST_P (t))
4048 : {
4049 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4050 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4051 : return;
4052 : }
4053 1147622359 : tclass = TREE_CODE_CLASS (code);
4054 :
4055 1147622359 : if (tclass == tcc_declaration)
4056 : {
4057 : /* DECL's have a unique ID */
4058 808170154 : hstate.add_hwi (DECL_UID (t));
4059 : }
4060 339452205 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4061 : {
4062 : /* For comparisons that can be swapped, use the lower
4063 : tree code. */
4064 147717 : enum tree_code ccode = swap_tree_comparison (code);
4065 147717 : if (code < ccode)
4066 63831 : ccode = code;
4067 147717 : hstate.add_object (ccode);
4068 147717 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4069 147717 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4070 : }
4071 339304488 : else if (CONVERT_EXPR_CODE_P (code))
4072 : {
4073 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4074 : operand_equal_p. */
4075 6906647 : enum tree_code ccode = NOP_EXPR;
4076 6906647 : hstate.add_object (ccode);
4077 :
4078 : /* Don't hash the type, that can lead to having nodes which
4079 : compare equal according to operand_equal_p, but which
4080 : have different hash codes. Make sure to include signedness
4081 : in the hash computation. */
4082 6906647 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4083 6906647 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4084 : }
4085 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4086 332397841 : else if (code == MEM_REF
4087 79905178 : && (flags & OEP_ADDRESS_OF) != 0
4088 70405532 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4089 14175058 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4090 346363359 : && integer_zerop (TREE_OPERAND (t, 1)))
4091 6223282 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4092 : hstate, flags);
4093 : /* Don't ICE on FE specific trees, or their arguments etc.
4094 : during operand_equal_p hash verification. */
4095 326174559 : else if (!IS_EXPR_CODE_CLASS (tclass))
4096 252 : gcc_assert (flags & OEP_HASH_CHECK);
4097 : else
4098 : {
4099 326174307 : unsigned int sflags = flags;
4100 :
4101 326174307 : hstate.add_object (code);
4102 :
4103 326174307 : switch (code)
4104 : {
4105 130627502 : case ADDR_EXPR:
4106 130627502 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4107 130627502 : flags |= OEP_ADDRESS_OF;
4108 130627502 : sflags = flags;
4109 130627502 : break;
4110 :
4111 78598397 : case INDIRECT_REF:
4112 78598397 : case MEM_REF:
4113 78598397 : case TARGET_MEM_REF:
4114 78598397 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4115 78598397 : sflags = flags;
4116 78598397 : break;
4117 :
4118 76258520 : case COMPONENT_REF:
4119 76258520 : if (sflags & OEP_ADDRESS_OF)
4120 : {
4121 38647798 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4122 38647798 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4123 : hstate, flags & ~OEP_ADDRESS_OF);
4124 38647798 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4125 : hstate, flags & ~OEP_ADDRESS_OF);
4126 38647798 : return;
4127 : }
4128 : break;
4129 15817508 : case ARRAY_REF:
4130 15817508 : case ARRAY_RANGE_REF:
4131 15817508 : case BIT_FIELD_REF:
4132 15817508 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4133 15817508 : break;
4134 :
4135 8429 : case COND_EXPR:
4136 8429 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4137 8429 : break;
4138 :
4139 0 : case WIDEN_MULT_PLUS_EXPR:
4140 0 : case WIDEN_MULT_MINUS_EXPR:
4141 0 : {
4142 : /* The multiplication operands are commutative. */
4143 0 : inchash::hash one, two;
4144 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4145 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4146 0 : hstate.add_commutative (one, two);
4147 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4148 0 : return;
4149 : }
4150 :
4151 37924 : case CALL_EXPR:
4152 37924 : if (CALL_EXPR_FN (t) == NULL_TREE)
4153 6 : hstate.add_int (CALL_EXPR_IFN (t));
4154 : break;
4155 :
4156 72 : case TARGET_EXPR:
4157 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4158 : Usually different TARGET_EXPRs just should use
4159 : different temporaries in their slots. */
4160 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4161 72 : return;
4162 :
4163 289720 : case OBJ_TYPE_REF:
4164 : /* Virtual table reference. */
4165 289720 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4166 289720 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4167 289720 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4168 289720 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4169 289720 : if (!virtual_method_call_p (t))
4170 : return;
4171 289699 : if (tree c = obj_type_ref_class (t))
4172 : {
4173 289699 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4174 : /* We compute mangled names only when free_lang_data is run.
4175 : In that case we can hash precisely. */
4176 289699 : if (TREE_CODE (c) == TYPE_DECL
4177 289699 : && DECL_ASSEMBLER_NAME_SET_P (c))
4178 7303 : hstate.add_object
4179 7303 : (IDENTIFIER_HASH_VALUE
4180 : (DECL_ASSEMBLER_NAME (c)));
4181 : }
4182 289699 : return;
4183 : default:
4184 : break;
4185 : }
4186 :
4187 : /* Don't hash the type, that can lead to having nodes which
4188 : compare equal according to operand_equal_p, but which
4189 : have different hash codes. */
4190 287236717 : if (code == NON_LVALUE_EXPR)
4191 : {
4192 : /* Make sure to include signness in the hash computation. */
4193 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4194 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4195 : }
4196 :
4197 287236717 : else if (commutative_tree_code (code))
4198 : {
4199 : /* It's a commutative expression. We want to hash it the same
4200 : however it appears. We do this by first hashing both operands
4201 : and then rehashing based on the order of their independent
4202 : hashes. */
4203 18163237 : inchash::hash one, two;
4204 18163237 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4205 18163237 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4206 18163237 : hstate.add_commutative (one, two);
4207 : }
4208 : else
4209 751186299 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4210 695152422 : hash_operand (TREE_OPERAND (t, i), hstate,
4211 : i == 0 ? flags : sflags);
4212 : }
4213 : return;
4214 : }
4215 : }
4216 :
4217 : bool
4218 7281900621 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4219 : unsigned int flags, bool *ret)
4220 : {
4221 : /* When checking and unless comparing DECL names, verify that if
4222 : the outermost operand_equal_p call returns non-zero then ARG0
4223 : and ARG1 have the same hash value. */
4224 7281900621 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4225 : {
4226 3062921719 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4227 : {
4228 473949734 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4229 : {
4230 84532661 : inchash::hash hstate0 (0), hstate1 (0);
4231 84532661 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4232 84532661 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4233 84532661 : hashval_t h0 = hstate0.end ();
4234 84532661 : hashval_t h1 = hstate1.end ();
4235 84532661 : gcc_assert (h0 == h1);
4236 : }
4237 473949734 : *ret = true;
4238 : }
4239 : else
4240 2588971985 : *ret = false;
4241 :
4242 3062921719 : return true;
4243 : }
4244 :
4245 : return false;
4246 : }
4247 :
4248 :
4249 : static operand_compare default_compare_instance;
4250 :
4251 : /* Convenience wrapper around operand_compare class because usually we do
4252 : not need to play with the valueizer. */
4253 :
4254 : bool
4255 3061072787 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4256 : {
4257 3061072787 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4258 : }
4259 :
4260 : namespace inchash
4261 : {
4262 :
4263 : /* Generate a hash value for an expression. This can be used iteratively
4264 : by passing a previous result as the HSTATE argument.
4265 :
4266 : This function is intended to produce the same hash for expressions which
4267 : would compare equal using operand_equal_p. */
4268 : void
4269 2170925402 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4270 : {
4271 2170925402 : default_compare_instance.hash_operand (t, hstate, flags);
4272 2170925402 : }
4273 :
4274 : }
4275 :
4276 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4277 : with a different signedness or a narrower precision. */
4278 :
4279 : static bool
4280 20352077 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4281 : {
4282 20352077 : if (operand_equal_p (arg0, arg1, 0))
4283 : return true;
4284 :
4285 38954408 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4286 33264003 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4287 : return false;
4288 :
4289 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4290 : and see if the inner values are the same. This removes any
4291 : signedness comparison, which doesn't matter here. */
4292 6167159 : tree op0 = arg0;
4293 6167159 : tree op1 = arg1;
4294 6167159 : STRIP_NOPS (op0);
4295 6167159 : STRIP_NOPS (op1);
4296 6167159 : if (operand_equal_p (op0, op1, 0))
4297 : return true;
4298 :
4299 : /* Discard a single widening conversion from ARG1 and see if the inner
4300 : value is the same as ARG0. */
4301 5100033 : if (CONVERT_EXPR_P (arg1)
4302 885817 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4303 885769 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4304 885769 : < TYPE_PRECISION (TREE_TYPE (arg1))
4305 6293584 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4306 : return true;
4307 :
4308 : return false;
4309 : }
4310 :
4311 : /* See if ARG is an expression that is either a comparison or is performing
4312 : arithmetic on comparisons. The comparisons must only be comparing
4313 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4314 : they are nonzero it means that some operands have already been found.
4315 : No variables may be used anywhere else in the expression except in the
4316 : comparisons.
4317 :
4318 : If this is true, return 1. Otherwise, return zero. */
4319 :
4320 : static bool
4321 60043919 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4322 : {
4323 63965525 : enum tree_code code = TREE_CODE (arg);
4324 63965525 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4325 :
4326 : /* We can handle some of the tcc_expression cases here. */
4327 63965525 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4328 : tclass = tcc_unary;
4329 63434818 : else if (tclass == tcc_expression
4330 695756 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4331 695756 : || code == COMPOUND_EXPR))
4332 : tclass = tcc_binary;
4333 :
4334 63424051 : switch (tclass)
4335 : {
4336 3921606 : case tcc_unary:
4337 3921606 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4338 :
4339 5373682 : case tcc_binary:
4340 5373682 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4341 5373682 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4342 :
4343 : case tcc_constant:
4344 : return true;
4345 :
4346 684989 : case tcc_expression:
4347 684989 : if (code == COND_EXPR)
4348 718 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4349 718 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4350 782 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4351 : return false;
4352 :
4353 535215 : case tcc_comparison:
4354 : /* First see if we can handle the first operand, then the second. For
4355 : the second operand, we know *CVAL1 can't be zero. It must be that
4356 : one side of the comparison is each of the values; test for the
4357 : case where this isn't true by failing if the two operands
4358 : are the same. */
4359 :
4360 535215 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4361 535215 : TREE_OPERAND (arg, 1), 0))
4362 : return false;
4363 :
4364 535215 : if (*cval1 == 0)
4365 533165 : *cval1 = TREE_OPERAND (arg, 0);
4366 2050 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4367 : ;
4368 1931 : else if (*cval2 == 0)
4369 0 : *cval2 = TREE_OPERAND (arg, 0);
4370 1931 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4371 : ;
4372 : else
4373 : return false;
4374 :
4375 533284 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4376 : ;
4377 533284 : else if (*cval2 == 0)
4378 533165 : *cval2 = TREE_OPERAND (arg, 1);
4379 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4380 : ;
4381 : else
4382 : return false;
4383 :
4384 : return true;
4385 :
4386 : default:
4387 : return false;
4388 : }
4389 : }
4390 :
4391 : /* ARG is a tree that is known to contain just arithmetic operations and
4392 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4393 : any occurrence of OLD0 as an operand of a comparison and likewise for
4394 : NEW1 and OLD1. */
4395 :
4396 : static tree
4397 627 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4398 : tree old1, tree new1)
4399 : {
4400 627 : tree type = TREE_TYPE (arg);
4401 627 : enum tree_code code = TREE_CODE (arg);
4402 627 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4403 :
4404 : /* We can handle some of the tcc_expression cases here. */
4405 627 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4406 : tclass = tcc_unary;
4407 627 : else if (tclass == tcc_expression
4408 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4409 : tclass = tcc_binary;
4410 :
4411 618 : switch (tclass)
4412 : {
4413 168 : case tcc_unary:
4414 168 : return fold_build1_loc (loc, code, type,
4415 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4416 168 : old0, new0, old1, new1));
4417 :
4418 141 : case tcc_binary:
4419 282 : return fold_build2_loc (loc, code, type,
4420 141 : eval_subst (loc, TREE_OPERAND (arg, 0),
4421 : old0, new0, old1, new1),
4422 141 : eval_subst (loc, TREE_OPERAND (arg, 1),
4423 141 : old0, new0, old1, new1));
4424 :
4425 9 : case tcc_expression:
4426 9 : switch (code)
4427 : {
4428 0 : case SAVE_EXPR:
4429 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4430 0 : old1, new1);
4431 :
4432 0 : case COMPOUND_EXPR:
4433 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4434 0 : old1, new1);
4435 :
4436 9 : case COND_EXPR:
4437 27 : return fold_build3_loc (loc, code, type,
4438 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4439 : old0, new0, old1, new1),
4440 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4441 : old0, new0, old1, new1),
4442 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4443 9 : old0, new0, old1, new1));
4444 : default:
4445 : break;
4446 : }
4447 : /* Fall through - ??? */
4448 :
4449 156 : case tcc_comparison:
4450 156 : {
4451 156 : tree arg0 = TREE_OPERAND (arg, 0);
4452 156 : tree arg1 = TREE_OPERAND (arg, 1);
4453 :
4454 : /* We need to check both for exact equality and tree equality. The
4455 : former will be true if the operand has a side-effect. In that
4456 : case, we know the operand occurred exactly once. */
4457 :
4458 156 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4459 : arg0 = new0;
4460 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4461 : arg0 = new1;
4462 :
4463 156 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4464 : arg1 = new0;
4465 156 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4466 : arg1 = new1;
4467 :
4468 156 : return fold_build2_loc (loc, code, type, arg0, arg1);
4469 : }
4470 :
4471 : default:
4472 : return arg;
4473 : }
4474 : }
4475 :
4476 : /* Return a tree for the case when the result of an expression is RESULT
4477 : converted to TYPE and OMITTED was previously an operand of the expression
4478 : but is now not needed (e.g., we folded OMITTED * 0).
4479 :
4480 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4481 : the conversion of RESULT to TYPE. */
4482 :
4483 : tree
4484 277045 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4485 : {
4486 277045 : tree t = fold_convert_loc (loc, type, result);
4487 :
4488 : /* If the resulting operand is an empty statement, just return the omitted
4489 : statement casted to void. */
4490 277045 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4491 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4492 0 : fold_ignored_result (omitted));
4493 :
4494 277045 : if (TREE_SIDE_EFFECTS (omitted))
4495 11984 : return build2_loc (loc, COMPOUND_EXPR, type,
4496 11984 : fold_ignored_result (omitted), t);
4497 :
4498 265061 : return non_lvalue_loc (loc, t);
4499 : }
4500 :
4501 : /* Return a tree for the case when the result of an expression is RESULT
4502 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4503 : of the expression but are now not needed.
4504 :
4505 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4506 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4507 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4508 : just do the conversion of RESULT to TYPE. */
4509 :
4510 : tree
4511 5688 : omit_two_operands_loc (location_t loc, tree type, tree result,
4512 : tree omitted1, tree omitted2)
4513 : {
4514 5688 : tree t = fold_convert_loc (loc, type, result);
4515 :
4516 5688 : if (TREE_SIDE_EFFECTS (omitted2))
4517 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4518 5688 : if (TREE_SIDE_EFFECTS (omitted1))
4519 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4520 :
4521 5688 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4522 : }
4523 :
4524 :
4525 : /* Return a simplified tree node for the truth-negation of ARG. This
4526 : never alters ARG itself. We assume that ARG is an operation that
4527 : returns a truth value (0 or 1).
4528 :
4529 : FIXME: one would think we would fold the result, but it causes
4530 : problems with the dominator optimizer. */
4531 :
4532 : static tree
4533 50288470 : fold_truth_not_expr (location_t loc, tree arg)
4534 : {
4535 50288470 : tree type = TREE_TYPE (arg);
4536 50288470 : enum tree_code code = TREE_CODE (arg);
4537 50288470 : location_t loc1, loc2;
4538 :
4539 : /* If this is a comparison, we can simply invert it, except for
4540 : floating-point non-equality comparisons, in which case we just
4541 : enclose a TRUTH_NOT_EXPR around what we have. */
4542 :
4543 50288470 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4544 : {
4545 38241208 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4546 32289287 : if (FLOAT_TYPE_P (op_type)
4547 5962008 : && flag_trapping_math
4548 5931445 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4549 44132134 : && code != NE_EXPR && code != EQ_EXPR)
4550 : return NULL_TREE;
4551 :
4552 33042062 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4553 33042062 : if (code == ERROR_MARK)
4554 : return NULL_TREE;
4555 :
4556 33042062 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4557 33042062 : TREE_OPERAND (arg, 1));
4558 33042062 : copy_warning (ret, arg);
4559 33042062 : return ret;
4560 : }
4561 :
4562 12047262 : switch (code)
4563 : {
4564 0 : case INTEGER_CST:
4565 0 : return constant_boolean_node (integer_zerop (arg), type);
4566 :
4567 51455 : case TRUTH_AND_EXPR:
4568 51455 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4569 51455 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4570 102910 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4571 51455 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4572 102910 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4573 :
4574 2556 : case TRUTH_OR_EXPR:
4575 2556 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4576 2556 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4577 5112 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4578 2556 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4579 5112 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4580 :
4581 72927 : case TRUTH_XOR_EXPR:
4582 : /* Here we can invert either operand. We invert the first operand
4583 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4584 : result is the XOR of the first operand with the inside of the
4585 : negation of the second operand. */
4586 :
4587 72927 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4588 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4589 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4590 : else
4591 72739 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4592 72739 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4593 145478 : TREE_OPERAND (arg, 1));
4594 :
4595 395973 : case TRUTH_ANDIF_EXPR:
4596 395973 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4597 395973 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4598 791946 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4599 395973 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4600 791946 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4601 :
4602 16740 : case TRUTH_ORIF_EXPR:
4603 16740 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4604 16740 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4605 33480 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4606 16740 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4607 33480 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4608 :
4609 789715 : case TRUTH_NOT_EXPR:
4610 789715 : return TREE_OPERAND (arg, 0);
4611 :
4612 9760 : case COND_EXPR:
4613 9760 : {
4614 9760 : tree arg1 = TREE_OPERAND (arg, 1);
4615 9760 : tree arg2 = TREE_OPERAND (arg, 2);
4616 :
4617 9760 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4618 9760 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4619 :
4620 : /* A COND_EXPR may have a throw as one operand, which
4621 : then has void type. Just leave void operands
4622 : as they are. */
4623 9760 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4624 9760 : VOID_TYPE_P (TREE_TYPE (arg1))
4625 9760 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4626 9760 : VOID_TYPE_P (TREE_TYPE (arg2))
4627 19517 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4628 : }
4629 :
4630 959 : case COMPOUND_EXPR:
4631 959 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4632 1918 : return build2_loc (loc, COMPOUND_EXPR, type,
4633 959 : TREE_OPERAND (arg, 0),
4634 1918 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4635 :
4636 0 : case NON_LVALUE_EXPR:
4637 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4638 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4639 :
4640 73663 : CASE_CONVERT:
4641 73663 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4642 73599 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4643 :
4644 : /* fall through */
4645 :
4646 64 : case FLOAT_EXPR:
4647 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4648 64 : return build1_loc (loc, TREE_CODE (arg), type,
4649 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4650 :
4651 498 : case BIT_AND_EXPR:
4652 498 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4653 : return NULL_TREE;
4654 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4655 :
4656 2 : case SAVE_EXPR:
4657 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4658 :
4659 331 : case CLEANUP_POINT_EXPR:
4660 331 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4661 331 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4662 662 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4663 :
4664 : default:
4665 : return NULL_TREE;
4666 : }
4667 : }
4668 :
4669 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4670 : assume that ARG is an operation that returns a truth value (0 or 1
4671 : for scalars, 0 or -1 for vectors). Return the folded expression if
4672 : folding is successful. Otherwise, return NULL_TREE. */
4673 :
4674 : static tree
4675 2086686 : fold_invert_truthvalue (location_t loc, tree arg)
4676 : {
4677 2086686 : tree type = TREE_TYPE (arg);
4678 4173348 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4679 : ? BIT_NOT_EXPR
4680 : : TRUTH_NOT_EXPR,
4681 2086686 : type, arg);
4682 : }
4683 :
4684 : /* Return a simplified tree node for the truth-negation of ARG. This
4685 : never alters ARG itself. We assume that ARG is an operation that
4686 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4687 :
4688 : tree
4689 43338904 : invert_truthvalue_loc (location_t loc, tree arg)
4690 : {
4691 43338904 : if (TREE_CODE (arg) == ERROR_MARK)
4692 : return arg;
4693 :
4694 43338904 : tree type = TREE_TYPE (arg);
4695 86677808 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4696 : ? BIT_NOT_EXPR
4697 : : TRUTH_NOT_EXPR,
4698 43338904 : type, arg);
4699 : }
4700 :
4701 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4702 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4703 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4704 : is the original memory reference used to preserve the alias set of
4705 : the access. */
4706 :
4707 : tree
4708 793665 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4709 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4710 : int unsignedp, int reversep)
4711 : {
4712 793665 : tree result, bftype;
4713 :
4714 : /* Attempt not to lose the access path if possible. */
4715 793665 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4716 : {
4717 789891 : tree ninner = TREE_OPERAND (orig_inner, 0);
4718 789891 : machine_mode nmode;
4719 789891 : poly_int64 nbitsize, nbitpos;
4720 789891 : tree noffset;
4721 789891 : int nunsignedp, nreversep, nvolatilep = 0;
4722 789891 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4723 : &noffset, &nmode, &nunsignedp,
4724 : &nreversep, &nvolatilep);
4725 789891 : if (base == inner
4726 789757 : && noffset == NULL_TREE
4727 789757 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4728 789750 : && !reversep
4729 789678 : && !nreversep
4730 1579569 : && !nvolatilep)
4731 : {
4732 789678 : inner = ninner;
4733 789891 : bitpos -= nbitpos;
4734 : }
4735 : }
4736 :
4737 793665 : alias_set_type iset = get_alias_set (orig_inner);
4738 793665 : if (iset == 0 && get_alias_set (inner) != iset)
4739 234 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4740 : build_fold_addr_expr (inner),
4741 : build_int_cst (ptr_type_node, 0));
4742 :
4743 793665 : if (known_eq (bitpos, 0) && !reversep)
4744 : {
4745 12677 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4746 25354 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4747 12515 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4748 166 : && tree_fits_shwi_p (size)
4749 12843 : && tree_to_shwi (size) == bitsize)
4750 143 : return fold_convert_loc (loc, type, inner);
4751 : }
4752 :
4753 793522 : bftype = type;
4754 793522 : if (TYPE_PRECISION (bftype) != bitsize
4755 793522 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4756 496 : bftype = build_nonstandard_integer_type (bitsize, 0);
4757 :
4758 793522 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4759 793522 : bitsize_int (bitsize), bitsize_int (bitpos));
4760 793522 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4761 :
4762 793522 : if (bftype != type)
4763 496 : result = fold_convert_loc (loc, type, result);
4764 :
4765 : return result;
4766 : }
4767 :
4768 : /* Optimize a bit-field compare.
4769 :
4770 : There are two cases: First is a compare against a constant and the
4771 : second is a comparison of two items where the fields are at the same
4772 : bit position relative to the start of a chunk (byte, halfword, word)
4773 : large enough to contain it. In these cases we can avoid the shift
4774 : implicit in bitfield extractions.
4775 :
4776 : For constants, we emit a compare of the shifted constant with the
4777 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4778 : compared. For two fields at the same position, we do the ANDs with the
4779 : similar mask and compare the result of the ANDs.
4780 :
4781 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4782 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4783 : are the left and right operands of the comparison, respectively.
4784 :
4785 : If the optimization described above can be done, we return the resulting
4786 : tree. Otherwise we return zero. */
4787 :
4788 : static tree
4789 4599892 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4790 : tree compare_type, tree lhs, tree rhs)
4791 : {
4792 4599892 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4793 4599892 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4794 4599892 : tree type = TREE_TYPE (lhs);
4795 4599892 : tree unsigned_type;
4796 4599892 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4797 4599892 : machine_mode lmode, rmode;
4798 4599892 : scalar_int_mode nmode;
4799 4599892 : int lunsignedp, runsignedp;
4800 4599892 : int lreversep, rreversep;
4801 4599892 : int lvolatilep = 0, rvolatilep = 0;
4802 4599892 : tree linner, rinner = NULL_TREE;
4803 4599892 : tree mask;
4804 4599892 : tree offset;
4805 :
4806 : /* Get all the information about the extractions being done. If the bit size
4807 : is the same as the size of the underlying object, we aren't doing an
4808 : extraction at all and so can do nothing. We also don't want to
4809 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4810 : then will no longer be able to replace it. */
4811 4599892 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4812 : &lunsignedp, &lreversep, &lvolatilep);
4813 4599892 : if (linner == lhs
4814 4599892 : || !known_size_p (plbitsize)
4815 4599892 : || !plbitsize.is_constant (&lbitsize)
4816 4599892 : || !plbitpos.is_constant (&lbitpos)
4817 9199784 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4818 753911 : || offset != 0
4819 753886 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4820 5353778 : || lvolatilep)
4821 3846066 : return 0;
4822 :
4823 753826 : if (const_p)
4824 714908 : rreversep = lreversep;
4825 : else
4826 : {
4827 : /* If this is not a constant, we can only do something if bit positions,
4828 : sizes, signedness and storage order are the same. */
4829 38918 : rinner
4830 38918 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4831 : &runsignedp, &rreversep, &rvolatilep);
4832 :
4833 38918 : if (rinner == rhs
4834 38874 : || maybe_ne (lbitpos, rbitpos)
4835 38840 : || maybe_ne (lbitsize, rbitsize)
4836 38840 : || lunsignedp != runsignedp
4837 38840 : || lreversep != rreversep
4838 38840 : || offset != 0
4839 38840 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4840 77758 : || rvolatilep)
4841 : return 0;
4842 : }
4843 :
4844 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4845 753748 : poly_uint64 bitstart = 0;
4846 753748 : poly_uint64 bitend = 0;
4847 753748 : if (TREE_CODE (lhs) == COMPONENT_REF)
4848 : {
4849 753748 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4850 753748 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4851 : return 0;
4852 : }
4853 :
4854 : /* See if we can find a mode to refer to this field. We should be able to,
4855 : but fail if we can't. */
4856 1507496 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4857 714908 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4858 38840 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4859 : TYPE_ALIGN (TREE_TYPE (rinner))),
4860 753748 : BITS_PER_WORD, false, &nmode))
4861 : return 0;
4862 :
4863 : /* Set signed and unsigned types of the precision of this mode for the
4864 : shifts below. */
4865 751745 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4866 :
4867 : /* Compute the bit position and size for the new reference and our offset
4868 : within it. If the new reference is the same size as the original, we
4869 : won't optimize anything, so return zero. */
4870 751745 : nbitsize = GET_MODE_BITSIZE (nmode);
4871 751745 : nbitpos = lbitpos & ~ (nbitsize - 1);
4872 751745 : lbitpos -= nbitpos;
4873 751745 : if (nbitsize == lbitsize)
4874 : return 0;
4875 :
4876 751745 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4877 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4878 :
4879 : /* Make the mask to be used against the extracted field. */
4880 751745 : mask = build_int_cst_type (unsigned_type, -1);
4881 751745 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4882 751745 : mask = const_binop (RSHIFT_EXPR, mask,
4883 751745 : size_int (nbitsize - lbitsize - lbitpos));
4884 :
4885 751745 : if (! const_p)
4886 : {
4887 37285 : if (nbitpos < 0)
4888 : return 0;
4889 :
4890 : /* If not comparing with constant, just rework the comparison
4891 : and return. */
4892 37285 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4893 37285 : nbitsize, nbitpos, 1, lreversep);
4894 37285 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4895 37285 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4896 37285 : nbitsize, nbitpos, 1, rreversep);
4897 37285 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4898 37285 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4899 : }
4900 :
4901 : /* Otherwise, we are handling the constant case. See if the constant is too
4902 : big for the field. Warn and return a tree for 0 (false) if so. We do
4903 : this not only for its own sake, but to avoid having to test for this
4904 : error case below. If we didn't, we might generate wrong code.
4905 :
4906 : For unsigned fields, the constant shifted right by the field length should
4907 : be all zero. For signed fields, the high-order bits should agree with
4908 : the sign bit. */
4909 :
4910 714460 : if (lunsignedp)
4911 : {
4912 713295 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4913 : {
4914 0 : warning (0, "comparison is always %d due to width of bit-field",
4915 : code == NE_EXPR);
4916 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4917 : }
4918 : }
4919 : else
4920 : {
4921 1165 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4922 1165 : if (tem != 0 && tem != -1)
4923 : {
4924 0 : warning (0, "comparison is always %d due to width of bit-field",
4925 : code == NE_EXPR);
4926 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4927 : }
4928 1165 : }
4929 :
4930 714460 : if (nbitpos < 0)
4931 : return 0;
4932 :
4933 : /* Single-bit compares should always be against zero. */
4934 714460 : if (lbitsize == 1 && ! integer_zerop (rhs))
4935 : {
4936 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4937 175 : rhs = build_int_cst (type, 0);
4938 : }
4939 :
4940 : /* Make a new bitfield reference, shift the constant over the
4941 : appropriate number of bits and mask it with the computed mask
4942 : (in case this was a signed field). If we changed it, make a new one. */
4943 714460 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4944 714460 : nbitsize, nbitpos, 1, lreversep);
4945 :
4946 714460 : rhs = const_binop (BIT_AND_EXPR,
4947 : const_binop (LSHIFT_EXPR,
4948 : fold_convert_loc (loc, unsigned_type, rhs),
4949 714460 : size_int (lbitpos)),
4950 : mask);
4951 :
4952 714460 : lhs = build2_loc (loc, code, compare_type,
4953 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4954 714460 : return lhs;
4955 : }
4956 :
4957 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4958 : represents the sign bit of EXP's type. If EXP represents a sign
4959 : or zero extension, also test VAL against the unextended type.
4960 : The return value is the (sub)expression whose sign bit is VAL,
4961 : or NULL_TREE otherwise. */
4962 :
4963 : tree
4964 2206 : sign_bit_p (tree exp, const_tree val)
4965 : {
4966 2206 : int width;
4967 2206 : tree t;
4968 :
4969 : /* Tree EXP must have an integral type. */
4970 2206 : t = TREE_TYPE (exp);
4971 2206 : if (! INTEGRAL_TYPE_P (t))
4972 : return NULL_TREE;
4973 :
4974 : /* Tree VAL must be an integer constant. */
4975 1860 : if (TREE_CODE (val) != INTEGER_CST
4976 1860 : || TREE_OVERFLOW (val))
4977 : return NULL_TREE;
4978 :
4979 1499 : width = TYPE_PRECISION (t);
4980 1499 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
4981 : return exp;
4982 :
4983 : /* Handle extension from a narrower type. */
4984 862 : if (TREE_CODE (exp) == NOP_EXPR
4985 862 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4986 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
4987 :
4988 : return NULL_TREE;
4989 : }
4990 :
4991 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4992 : operand is simple enough to be evaluated unconditionally. */
4993 :
4994 : static bool
4995 65760065 : simple_operand_p (const_tree exp)
4996 : {
4997 : /* Strip any conversions that don't change the machine mode. */
4998 65760065 : STRIP_NOPS (exp);
4999 :
5000 65760065 : return (CONSTANT_CLASS_P (exp)
5001 45651204 : || TREE_CODE (exp) == SSA_NAME
5002 81870723 : || (DECL_P (exp)
5003 5647605 : && ! TREE_ADDRESSABLE (exp)
5004 5560397 : && ! TREE_THIS_VOLATILE (exp)
5005 5560397 : && ! DECL_NONLOCAL (exp)
5006 : /* Don't regard global variables as simple. They may be
5007 : allocated in ways unknown to the compiler (shared memory,
5008 : #pragma weak, etc). */
5009 5558744 : && ! TREE_PUBLIC (exp)
5010 5538019 : && ! DECL_EXTERNAL (exp)
5011 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5012 5538019 : && ! ((VAR_P (exp)
5013 : || TREE_CODE (exp) == PARM_DECL
5014 : || TREE_CODE (exp) == RESULT_DECL)
5015 5538019 : && DECL_HAS_VALUE_EXPR_P (exp))
5016 : /* Weakrefs are not safe to be read, since they can be NULL.
5017 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5018 : have DECL_WEAK flag set. */
5019 5537430 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5020 : /* Loading a static variable is unduly expensive, but global
5021 : registers aren't expensive. */
5022 5537430 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5023 : }
5024 :
5025 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5026 : In addition to simple_operand_p, we assume that comparisons, conversions,
5027 : and logic-not operations are simple, if their operands are simple, too. */
5028 :
5029 : bool
5030 7048144 : simple_condition_p (tree exp)
5031 : {
5032 7142374 : enum tree_code code;
5033 :
5034 7142374 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5035 4860693 : return false;
5036 :
5037 2317204 : while (CONVERT_EXPR_P (exp))
5038 35523 : exp = TREE_OPERAND (exp, 0);
5039 :
5040 2281681 : code = TREE_CODE (exp);
5041 :
5042 2281681 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5043 1785785 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5044 1785785 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5045 :
5046 495896 : if (code == TRUTH_NOT_EXPR)
5047 94230 : return simple_condition_p (TREE_OPERAND (exp, 0));
5048 :
5049 401666 : return simple_operand_p (exp);
5050 : }
5051 :
5052 :
5053 : /* The following functions are subroutines to fold_range_test and allow it to
5054 : try to change a logical combination of comparisons into a range test.
5055 :
5056 : For example, both
5057 : X == 2 || X == 3 || X == 4 || X == 5
5058 : and
5059 : X >= 2 && X <= 5
5060 : are converted to
5061 : (unsigned) (X - 2) <= 3
5062 :
5063 : We describe each set of comparisons as being either inside or outside
5064 : a range, using a variable named like IN_P, and then describe the
5065 : range with a lower and upper bound. If one of the bounds is omitted,
5066 : it represents either the highest or lowest value of the type.
5067 :
5068 : In the comments below, we represent a range by two numbers in brackets
5069 : preceded by a "+" to designate being inside that range, or a "-" to
5070 : designate being outside that range, so the condition can be inverted by
5071 : flipping the prefix. An omitted bound is represented by a "-". For
5072 : example, "- [-, 10]" means being outside the range starting at the lowest
5073 : possible value and ending at 10, in other words, being greater than 10.
5074 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5075 : always false.
5076 :
5077 : We set up things so that the missing bounds are handled in a consistent
5078 : manner so neither a missing bound nor "true" and "false" need to be
5079 : handled using a special case. */
5080 :
5081 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5082 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5083 : and UPPER1_P are nonzero if the respective argument is an upper bound
5084 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5085 : must be specified for a comparison. ARG1 will be converted to ARG0's
5086 : type if both are specified. */
5087 :
5088 : static tree
5089 23060449 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5090 : tree arg1, int upper1_p)
5091 : {
5092 23060449 : tree tem;
5093 23060449 : int result;
5094 23060449 : int sgn0, sgn1;
5095 :
5096 : /* If neither arg represents infinity, do the normal operation.
5097 : Else, if not a comparison, return infinity. Else handle the special
5098 : comparison rules. Note that most of the cases below won't occur, but
5099 : are handled for consistency. */
5100 :
5101 23060449 : if (arg0 != 0 && arg1 != 0)
5102 : {
5103 12076094 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5104 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5105 12076094 : STRIP_NOPS (tem);
5106 12076094 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5107 : }
5108 :
5109 10984355 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5110 : return 0;
5111 :
5112 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5113 : for neither. In real maths, we cannot assume open ended ranges are
5114 : the same. But, this is computer arithmetic, where numbers are finite.
5115 : We can therefore make the transformation of any unbounded range with
5116 : the value Z, Z being greater than any representable number. This permits
5117 : us to treat unbounded ranges as equal. */
5118 10975260 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5119 10975260 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5120 10975260 : switch (code)
5121 : {
5122 5173247 : case EQ_EXPR:
5123 5173247 : result = sgn0 == sgn1;
5124 5173247 : break;
5125 0 : case NE_EXPR:
5126 0 : result = sgn0 != sgn1;
5127 0 : break;
5128 363803 : case LT_EXPR:
5129 363803 : result = sgn0 < sgn1;
5130 363803 : break;
5131 2522807 : case LE_EXPR:
5132 2522807 : result = sgn0 <= sgn1;
5133 2522807 : break;
5134 2915403 : case GT_EXPR:
5135 2915403 : result = sgn0 > sgn1;
5136 2915403 : break;
5137 0 : case GE_EXPR:
5138 0 : result = sgn0 >= sgn1;
5139 0 : break;
5140 0 : default:
5141 0 : gcc_unreachable ();
5142 : }
5143 :
5144 10975260 : return constant_boolean_node (result, type);
5145 : }
5146 :
5147 : /* Helper routine for make_range. Perform one step for it, return
5148 : new expression if the loop should continue or NULL_TREE if it should
5149 : stop. */
5150 :
5151 : tree
5152 60581326 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5153 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5154 : {
5155 60581326 : tree arg0_type = TREE_TYPE (arg0);
5156 60581326 : tree n_low, n_high, low = *p_low, high = *p_high;
5157 60581326 : int in_p = *p_in_p, n_in_p;
5158 :
5159 60581326 : switch (code)
5160 : {
5161 1665903 : case TRUTH_NOT_EXPR:
5162 : /* We can only do something if the range is testing for zero. */
5163 1665903 : if (low == NULL_TREE || high == NULL_TREE
5164 1665903 : || ! integer_zerop (low) || ! integer_zerop (high))
5165 0 : return NULL_TREE;
5166 1665903 : *p_in_p = ! in_p;
5167 1665903 : return arg0;
5168 :
5169 47093198 : case EQ_EXPR: case NE_EXPR:
5170 47093198 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5171 : /* We can only do something if the range is testing for zero
5172 : and if the second operand is an integer constant. Note that
5173 : saying something is "in" the range we make is done by
5174 : complementing IN_P since it will set in the initial case of
5175 : being not equal to zero; "out" is leaving it alone. */
5176 47093198 : if (low == NULL_TREE || high == NULL_TREE
5177 47093198 : || ! integer_zerop (low) || ! integer_zerop (high)
5178 94186308 : || TREE_CODE (arg1) != INTEGER_CST)
5179 17323284 : return NULL_TREE;
5180 :
5181 29769914 : switch (code)
5182 : {
5183 : case NE_EXPR: /* - [c, c] */
5184 : low = high = arg1;
5185 : break;
5186 8151685 : case EQ_EXPR: /* + [c, c] */
5187 8151685 : in_p = ! in_p, low = high = arg1;
5188 8151685 : break;
5189 2215935 : case GT_EXPR: /* - [-, c] */
5190 2215935 : low = 0, high = arg1;
5191 2215935 : break;
5192 743314 : case GE_EXPR: /* + [c, -] */
5193 743314 : in_p = ! in_p, low = arg1, high = 0;
5194 743314 : break;
5195 5707843 : case LT_EXPR: /* - [c, -] */
5196 5707843 : low = arg1, high = 0;
5197 5707843 : break;
5198 4431677 : case LE_EXPR: /* + [-, c] */
5199 4431677 : in_p = ! in_p, low = 0, high = arg1;
5200 4431677 : break;
5201 0 : default:
5202 0 : gcc_unreachable ();
5203 : }
5204 :
5205 : /* If this is an unsigned comparison, we also know that EXP is
5206 : greater than or equal to zero. We base the range tests we make
5207 : on that fact, so we record it here so we can parse existing
5208 : range tests. We test arg0_type since often the return type
5209 : of, e.g. EQ_EXPR, is boolean. */
5210 29769914 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5211 : {
5212 1923200 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5213 : in_p, low, high, 1,
5214 : build_int_cst (arg0_type, 0),
5215 : NULL_TREE))
5216 : return NULL_TREE;
5217 :
5218 1923191 : in_p = n_in_p, low = n_low, high = n_high;
5219 :
5220 : /* If the high bound is missing, but we have a nonzero low
5221 : bound, reverse the range so it goes from zero to the low bound
5222 : minus 1. */
5223 1923191 : if (high == 0 && low && ! integer_zerop (low))
5224 : {
5225 855380 : in_p = ! in_p;
5226 855380 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5227 855380 : build_int_cst (TREE_TYPE (low), 1), 0);
5228 855380 : low = build_int_cst (arg0_type, 0);
5229 : }
5230 : }
5231 :
5232 29769905 : *p_low = low;
5233 29769905 : *p_high = high;
5234 29769905 : *p_in_p = in_p;
5235 29769905 : return arg0;
5236 :
5237 326 : case NEGATE_EXPR:
5238 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5239 : low and high are non-NULL, then normalize will DTRT. */
5240 326 : if (!TYPE_UNSIGNED (arg0_type)
5241 326 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5242 : {
5243 95 : if (low == NULL_TREE)
5244 12 : low = TYPE_MIN_VALUE (arg0_type);
5245 95 : if (high == NULL_TREE)
5246 47 : high = TYPE_MAX_VALUE (arg0_type);
5247 : }
5248 :
5249 : /* (-x) IN [a,b] -> x in [-b, -a] */
5250 326 : n_low = range_binop (MINUS_EXPR, exp_type,
5251 : build_int_cst (exp_type, 0),
5252 : 0, high, 1);
5253 326 : n_high = range_binop (MINUS_EXPR, exp_type,
5254 : build_int_cst (exp_type, 0),
5255 : 0, low, 0);
5256 326 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5257 : return NULL_TREE;
5258 314 : goto normalize;
5259 :
5260 12 : case BIT_NOT_EXPR:
5261 : /* ~ X -> -X - 1 */
5262 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5263 : build_int_cst (exp_type, 1));
5264 :
5265 850883 : case PLUS_EXPR:
5266 850883 : case MINUS_EXPR:
5267 850883 : if (TREE_CODE (arg1) != INTEGER_CST)
5268 : return NULL_TREE;
5269 :
5270 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5271 : move a constant to the other side. */
5272 643444 : if (!TYPE_UNSIGNED (arg0_type)
5273 643444 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5274 : return NULL_TREE;
5275 :
5276 : /* If EXP is signed, any overflow in the computation is undefined,
5277 : so we don't worry about it so long as our computations on
5278 : the bounds don't overflow. For unsigned, overflow is defined
5279 : and this is exactly the right thing. */
5280 908276 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5281 : arg0_type, low, 0, arg1, 0);
5282 455635 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5283 : arg0_type, high, 1, arg1, 0);
5284 452096 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5285 907719 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5286 : return NULL_TREE;
5287 :
5288 455937 : normalize:
5289 : /* Check for an unsigned range which has wrapped around the maximum
5290 : value thus making n_high < n_low, and normalize it. */
5291 455937 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5292 : {
5293 118128 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5294 118128 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5295 118128 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5296 118128 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5297 :
5298 : /* If the range is of the form +/- [ x+1, x ], we won't
5299 : be able to normalize it. But then, it represents the
5300 : whole range or the empty set, so make it
5301 : +/- [ -, - ]. */
5302 118128 : if (tree_int_cst_equal (n_low, low)
5303 118128 : && tree_int_cst_equal (n_high, high))
5304 : low = high = 0;
5305 : else
5306 118128 : in_p = ! in_p;
5307 : }
5308 : else
5309 337809 : low = n_low, high = n_high;
5310 :
5311 455937 : *p_low = low;
5312 455937 : *p_high = high;
5313 455937 : *p_in_p = in_p;
5314 455937 : return arg0;
5315 :
5316 2553014 : CASE_CONVERT:
5317 2553014 : case NON_LVALUE_EXPR:
5318 2553014 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5319 : return NULL_TREE;
5320 :
5321 1172842 : if (! INTEGRAL_TYPE_P (arg0_type)
5322 1137732 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5323 1034169 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5324 : return NULL_TREE;
5325 :
5326 1016122 : n_low = low, n_high = high;
5327 :
5328 1016122 : if (n_low != 0)
5329 851427 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5330 :
5331 1016122 : if (n_high != 0)
5332 950423 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5333 :
5334 : /* If we're converting arg0 from an unsigned type, to exp,
5335 : a signed type, we will be doing the comparison as unsigned.
5336 : The tests above have already verified that LOW and HIGH
5337 : are both positive.
5338 :
5339 : So we have to ensure that we will handle large unsigned
5340 : values the same way that the current signed bounds treat
5341 : negative values. */
5342 :
5343 1016122 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5344 : {
5345 255230 : tree high_positive;
5346 255230 : tree equiv_type;
5347 : /* For fixed-point modes, we need to pass the saturating flag
5348 : as the 2nd parameter. */
5349 255230 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5350 0 : equiv_type
5351 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5352 0 : TYPE_SATURATING (arg0_type));
5353 255230 : else if (BITINT_TYPE_P (arg0_type))
5354 : equiv_type = arg0_type;
5355 : else
5356 255222 : equiv_type
5357 255222 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5358 :
5359 : /* A range without an upper bound is, naturally, unbounded.
5360 : Since convert would have cropped a very large value, use
5361 : the max value for the destination type. */
5362 255230 : high_positive
5363 255230 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5364 0 : : TYPE_MAX_VALUE (arg0_type);
5365 :
5366 255230 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5367 234083 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5368 : fold_convert_loc (loc, arg0_type,
5369 : high_positive),
5370 : build_int_cst (arg0_type, 1));
5371 :
5372 : /* If the low bound is specified, "and" the range with the
5373 : range for which the original unsigned value will be
5374 : positive. */
5375 255230 : if (low != 0)
5376 : {
5377 96030 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5378 : 1, fold_convert_loc (loc, arg0_type,
5379 : integer_zero_node),
5380 : high_positive))
5381 : return NULL_TREE;
5382 :
5383 96030 : in_p = (n_in_p == in_p);
5384 : }
5385 : else
5386 : {
5387 : /* Otherwise, "or" the range with the range of the input
5388 : that will be interpreted as negative. */
5389 159200 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5390 : 1, fold_convert_loc (loc, arg0_type,
5391 : integer_zero_node),
5392 : high_positive))
5393 : return NULL_TREE;
5394 :
5395 159200 : in_p = (in_p != n_in_p);
5396 : }
5397 : }
5398 :
5399 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5400 : an unsigned type, we will do the comparison as signed. If
5401 : high is non-NULL, we punt above if it doesn't fit in the signed
5402 : type, so if we get through here, +[-, high] or +[low, high] are
5403 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5404 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5405 : high is not, the +[low, -] range is equivalent to union of
5406 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5407 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5408 : low being 0, which should be treated as [-, -]. */
5409 760892 : else if (TYPE_UNSIGNED (exp_type)
5410 741783 : && !TYPE_UNSIGNED (arg0_type)
5411 383693 : && low
5412 1144585 : && !high)
5413 : {
5414 12 : if (integer_zerop (low))
5415 12 : n_low = NULL_TREE;
5416 : else
5417 : {
5418 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5419 : n_low, build_int_cst (arg0_type, -1));
5420 0 : n_low = build_zero_cst (arg0_type);
5421 0 : in_p = !in_p;
5422 : }
5423 : }
5424 :
5425 1016122 : *p_low = n_low;
5426 1016122 : *p_high = n_high;
5427 1016122 : *p_in_p = in_p;
5428 1016122 : return arg0;
5429 :
5430 : default:
5431 : return NULL_TREE;
5432 : }
5433 : }
5434 :
5435 : /* Given EXP, a logical expression, set the range it is testing into
5436 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5437 : actually being tested. *PLOW and *PHIGH will be made of the same
5438 : type as the returned expression. If EXP is not a comparison, we
5439 : will most likely not be returning a useful value and range. */
5440 :
5441 : tree
5442 50127856 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5443 : {
5444 50127856 : enum tree_code code;
5445 50127856 : tree arg0, arg1 = NULL_TREE;
5446 50127856 : tree exp_type, nexp;
5447 50127856 : int in_p;
5448 50127856 : tree low, high;
5449 50127856 : location_t loc = EXPR_LOCATION (exp);
5450 :
5451 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5452 : and see if we can refine the range. Some of the cases below may not
5453 : happen, but it doesn't seem worth worrying about this. We "continue"
5454 : the outer loop when we've changed something; otherwise we "break"
5455 : the switch, which will "break" the while. */
5456 :
5457 50127856 : in_p = 0;
5458 50127856 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5459 :
5460 79965194 : while (1)
5461 : {
5462 79965194 : code = TREE_CODE (exp);
5463 79965194 : exp_type = TREE_TYPE (exp);
5464 79965194 : arg0 = NULL_TREE;
5465 :
5466 79965194 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5467 : {
5468 55781005 : if (TREE_OPERAND_LENGTH (exp) > 0)
5469 55781005 : arg0 = TREE_OPERAND (exp, 0);
5470 55781005 : if (TREE_CODE_CLASS (code) == tcc_binary
5471 52616925 : || TREE_CODE_CLASS (code) == tcc_comparison
5472 65443009 : || (TREE_CODE_CLASS (code) == tcc_expression
5473 2854416 : && TREE_OPERAND_LENGTH (exp) > 1))
5474 47290883 : arg1 = TREE_OPERAND (exp, 1);
5475 : }
5476 55781005 : if (arg0 == NULL_TREE)
5477 : break;
5478 :
5479 55780991 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5480 : &high, &in_p);
5481 55780991 : if (nexp == NULL_TREE)
5482 : break;
5483 : exp = nexp;
5484 : }
5485 :
5486 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5487 50127856 : if (TREE_CODE (exp) == INTEGER_CST)
5488 : {
5489 31552 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5490 : exp, 0, low, 0))
5491 31552 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5492 : exp, 1, high, 1)));
5493 31552 : low = high = 0;
5494 31552 : exp = 0;
5495 : }
5496 :
5497 50127856 : *pin_p = in_p, *plow = low, *phigh = high;
5498 50127856 : return exp;
5499 : }
5500 :
5501 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5502 : a bitwise check i.e. when
5503 : LOW == 0xXX...X00...0
5504 : HIGH == 0xXX...X11...1
5505 : Return corresponding mask in MASK and stem in VALUE. */
5506 :
5507 : static bool
5508 125 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5509 : tree *value)
5510 : {
5511 125 : if (TREE_CODE (low) != INTEGER_CST
5512 125 : || TREE_CODE (high) != INTEGER_CST)
5513 : return false;
5514 :
5515 125 : unsigned prec = TYPE_PRECISION (type);
5516 125 : wide_int lo = wi::to_wide (low, prec);
5517 125 : wide_int hi = wi::to_wide (high, prec);
5518 :
5519 125 : wide_int end_mask = lo ^ hi;
5520 250 : if ((end_mask & (end_mask + 1)) != 0
5521 235 : || (lo & end_mask) != 0)
5522 : return false;
5523 :
5524 86 : wide_int stem_mask = ~end_mask;
5525 86 : wide_int stem = lo & stem_mask;
5526 86 : if (stem != (hi & stem_mask))
5527 : return false;
5528 :
5529 86 : *mask = wide_int_to_tree (type, stem_mask);
5530 86 : *value = wide_int_to_tree (type, stem);
5531 :
5532 86 : return true;
5533 211 : }
5534 :
5535 : /* Helper routine for build_range_check and match.pd. Return the type to
5536 : perform the check or NULL if it shouldn't be optimized. */
5537 :
5538 : tree
5539 590686 : range_check_type (tree etype)
5540 : {
5541 : /* First make sure that arithmetics in this type is valid, then make sure
5542 : that it wraps around. */
5543 590686 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5544 0 : etype = TREE_TYPE (etype);
5545 590686 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5546 60978 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5547 :
5548 590686 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5549 : {
5550 406842 : tree utype, minv, maxv;
5551 :
5552 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5553 : for the type in question, as we rely on this here. */
5554 406842 : utype = unsigned_type_for (etype);
5555 406842 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5556 406842 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5557 406842 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5558 406842 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5559 :
5560 406842 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5561 : minv, 1, maxv, 1)))
5562 : etype = utype;
5563 : else
5564 102 : return NULL_TREE;
5565 : }
5566 183844 : else if (POINTER_TYPE_P (etype)
5567 : || TREE_CODE (etype) == OFFSET_TYPE
5568 : /* Right now all BITINT_TYPEs satisfy
5569 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5570 : that like for INTEGER_TYPEs. */
5571 : || TREE_CODE (etype) == BITINT_TYPE)
5572 1362 : etype = unsigned_type_for (etype);
5573 : return etype;
5574 : }
5575 :
5576 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5577 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5578 : on IN_P) the range. Return 0 if the test couldn't be created. */
5579 :
5580 : tree
5581 1615503 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5582 : tree low, tree high)
5583 : {
5584 2821012 : tree etype = TREE_TYPE (exp), mask, value;
5585 :
5586 : /* Disable this optimization for function pointer expressions
5587 : on targets that require function pointer canonicalization. */
5588 2821012 : if (targetm.have_canonicalize_funcptr_for_compare ()
5589 0 : && POINTER_TYPE_P (etype)
5590 2821012 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5591 : return NULL_TREE;
5592 :
5593 2821012 : if (! in_p)
5594 : {
5595 312707 : value = build_range_check (loc, type, exp, 1, low, high);
5596 312707 : if (value != 0)
5597 312707 : return invert_truthvalue_loc (loc, value);
5598 :
5599 : return 0;
5600 : }
5601 :
5602 2508305 : if (low == 0 && high == 0)
5603 123715 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5604 :
5605 2384590 : if (low == 0)
5606 785123 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5607 785123 : fold_convert_loc (loc, etype, high));
5608 :
5609 1599467 : if (high == 0)
5610 72894 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5611 72894 : fold_convert_loc (loc, etype, low));
5612 :
5613 1526573 : if (operand_equal_p (low, high, 0))
5614 320822 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5615 320822 : fold_convert_loc (loc, etype, low));
5616 :
5617 1205751 : if (TREE_CODE (exp) == BIT_AND_EXPR
5618 1205751 : && maskable_range_p (low, high, etype, &mask, &value))
5619 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5620 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5621 : exp, mask),
5622 86 : value);
5623 :
5624 1205665 : if (integer_zerop (low))
5625 : {
5626 704510 : if (! TYPE_UNSIGNED (etype))
5627 : {
5628 185494 : etype = unsigned_type_for (etype);
5629 185494 : high = fold_convert_loc (loc, etype, high);
5630 185494 : exp = fold_convert_loc (loc, etype, exp);
5631 : }
5632 704510 : return build_range_check (loc, type, exp, 1, 0, high);
5633 : }
5634 :
5635 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5636 501155 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5637 : {
5638 128821 : int prec = TYPE_PRECISION (etype);
5639 :
5640 128821 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5641 : {
5642 126 : if (TYPE_UNSIGNED (etype))
5643 : {
5644 120 : tree signed_etype = signed_type_for (etype);
5645 120 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5646 0 : etype
5647 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5648 : else
5649 : etype = signed_etype;
5650 120 : exp = fold_convert_loc (loc, etype, exp);
5651 : }
5652 126 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5653 : build_int_cst (etype, 0));
5654 : }
5655 : }
5656 :
5657 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5658 : This requires wrap-around arithmetics for the type of the expression. */
5659 501029 : etype = range_check_type (etype);
5660 501029 : if (etype == NULL_TREE)
5661 : return NULL_TREE;
5662 :
5663 500999 : high = fold_convert_loc (loc, etype, high);
5664 500999 : low = fold_convert_loc (loc, etype, low);
5665 500999 : exp = fold_convert_loc (loc, etype, exp);
5666 :
5667 500999 : value = const_binop (MINUS_EXPR, high, low);
5668 :
5669 500999 : if (value != 0 && !TREE_OVERFLOW (value))
5670 500999 : return build_range_check (loc, type,
5671 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5672 : 1, build_int_cst (etype, 0), value);
5673 :
5674 : return 0;
5675 : }
5676 :
5677 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5678 :
5679 : static tree
5680 175508 : range_predecessor (tree val)
5681 : {
5682 175508 : tree type = TREE_TYPE (val);
5683 :
5684 175508 : if (INTEGRAL_TYPE_P (type)
5685 175508 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5686 : return 0;
5687 : else
5688 175508 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5689 175508 : build_int_cst (TREE_TYPE (val), 1), 0);
5690 : }
5691 :
5692 : /* Return the successor of VAL in its type, handling the infinite case. */
5693 :
5694 : static tree
5695 1655562 : range_successor (tree val)
5696 : {
5697 1655562 : tree type = TREE_TYPE (val);
5698 :
5699 1655562 : if (INTEGRAL_TYPE_P (type)
5700 1655562 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5701 : return 0;
5702 : else
5703 1655553 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5704 1655553 : build_int_cst (TREE_TYPE (val), 1), 0);
5705 : }
5706 :
5707 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5708 : can, 0 if we can't. Set the output range into the specified parameters. */
5709 :
5710 : bool
5711 3631865 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5712 : tree high0, int in1_p, tree low1, tree high1)
5713 : {
5714 3631865 : bool no_overlap;
5715 3631865 : int subset;
5716 3631865 : int temp;
5717 3631865 : tree tem;
5718 3631865 : int in_p;
5719 3631865 : tree low, high;
5720 3631865 : int lowequal = ((low0 == 0 && low1 == 0)
5721 3631865 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5722 3631865 : low0, 0, low1, 0)));
5723 3631865 : int highequal = ((high0 == 0 && high1 == 0)
5724 3631865 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5725 3631865 : high0, 1, high1, 1)));
5726 :
5727 : /* Make range 0 be the range that starts first, or ends last if they
5728 : start at the same value. Swap them if it isn't. */
5729 3631865 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5730 : low0, 0, low1, 0))
5731 3631865 : || (lowequal
5732 577367 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5733 : high1, 1, high0, 1))))
5734 : {
5735 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5736 : tem = low0, low0 = low1, low1 = tem;
5737 : tem = high0, high0 = high1, high1 = tem;
5738 : }
5739 :
5740 : /* If the second range is != high1 where high1 is the type maximum of
5741 : the type, try first merging with < high1 range. */
5742 3631865 : if (low1
5743 3631865 : && high1
5744 1002815 : && TREE_CODE (low1) == INTEGER_CST
5745 1002815 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5746 127486 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5747 170878 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5748 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5749 4592633 : && operand_equal_p (low1, high1, 0))
5750 : {
5751 560636 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5752 560636 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5753 : !in1_p, NULL_TREE, range_predecessor (low1)))
5754 : return true;
5755 : /* Similarly for the second range != low1 where low1 is the type minimum
5756 : of the type, try first merging with > low1 range. */
5757 449751 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5758 449751 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5759 : !in1_p, range_successor (low1), NULL_TREE))
5760 : return true;
5761 : }
5762 :
5763 : /* Now flag two cases, whether the ranges are disjoint or whether the
5764 : second range is totally subsumed in the first. Note that the tests
5765 : below are simplified by the ones above. */
5766 3425627 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5767 : high0, 1, low1, 0));
5768 3425627 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5769 : high1, 1, high0, 1));
5770 :
5771 : /* We now have four cases, depending on whether we are including or
5772 : excluding the two ranges. */
5773 3425627 : if (in0_p && in1_p)
5774 : {
5775 : /* If they don't overlap, the result is false. If the second range
5776 : is a subset it is the result. Otherwise, the range is from the start
5777 : of the second to the end of the first. */
5778 1569094 : if (no_overlap)
5779 : in_p = 0, low = high = 0;
5780 1567013 : else if (subset)
5781 : in_p = 1, low = low1, high = high1;
5782 : else
5783 1447262 : in_p = 1, low = low1, high = high0;
5784 : }
5785 :
5786 1856533 : else if (in0_p && ! in1_p)
5787 : {
5788 : /* If they don't overlap, the result is the first range. If they are
5789 : equal, the result is false. If the second range is a subset of the
5790 : first, and the ranges begin at the same place, we go from just after
5791 : the end of the second range to the end of the first. If the second
5792 : range is not a subset of the first, or if it is a subset and both
5793 : ranges end at the same place, the range starts at the start of the
5794 : first range and ends just before the second range.
5795 : Otherwise, we can't describe this as a single range. */
5796 324930 : if (no_overlap)
5797 : in_p = 1, low = low0, high = high0;
5798 319365 : else if (lowequal && highequal)
5799 : in_p = 0, low = high = 0;
5800 318569 : else if (subset && lowequal)
5801 : {
5802 242987 : low = range_successor (high1);
5803 242987 : high = high0;
5804 242987 : in_p = 1;
5805 242987 : if (low == 0)
5806 : {
5807 : /* We are in the weird situation where high0 > high1 but
5808 : high1 has no successor. Punt. */
5809 : return 0;
5810 : }
5811 : }
5812 75582 : else if (! subset || highequal)
5813 : {
5814 55147 : low = low0;
5815 55147 : high = range_predecessor (low1);
5816 55147 : in_p = 1;
5817 55147 : if (high == 0)
5818 : {
5819 : /* low0 < low1 but low1 has no predecessor. Punt. */
5820 : return 0;
5821 : }
5822 : }
5823 : else
5824 : return 0;
5825 : }
5826 :
5827 1531603 : else if (! in0_p && in1_p)
5828 : {
5829 : /* If they don't overlap, the result is the second range. If the second
5830 : is a subset of the first, the result is false. Otherwise,
5831 : the range starts just after the first range and ends at the
5832 : end of the second. */
5833 1172616 : if (no_overlap)
5834 : in_p = 1, low = low1, high = high1;
5835 1164862 : else if (subset || highequal)
5836 : in_p = 0, low = high = 0;
5837 : else
5838 : {
5839 1055400 : low = range_successor (high0);
5840 1055400 : high = high1;
5841 1055400 : in_p = 1;
5842 1055400 : if (low == 0)
5843 : {
5844 : /* high1 > high0 but high0 has no successor. Punt. */
5845 : return 0;
5846 : }
5847 : }
5848 : }
5849 :
5850 : else
5851 : {
5852 : /* The case where we are excluding both ranges. Here the complex case
5853 : is if they don't overlap. In that case, the only time we have a
5854 : range is if they are adjacent. If the second is a subset of the
5855 : first, the result is the first. Otherwise, the range to exclude
5856 : starts at the beginning of the first range and ends at the end of the
5857 : second. */
5858 358987 : if (no_overlap)
5859 : {
5860 261502 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5861 : range_successor (high0),
5862 : 1, low1, 0)))
5863 : in_p = 0, low = low0, high = high1;
5864 : else
5865 : {
5866 : /* Canonicalize - [min, x] into - [-, x]. */
5867 210656 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5868 209535 : switch (TREE_CODE (TREE_TYPE (low0)))
5869 : {
5870 51134 : case ENUMERAL_TYPE:
5871 51134 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5872 : GET_MODE_BITSIZE
5873 102268 : (TYPE_MODE (TREE_TYPE (low0)))))
5874 : break;
5875 : /* FALLTHROUGH */
5876 209334 : case INTEGER_TYPE:
5877 209334 : if (tree_int_cst_equal (low0,
5878 209334 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5879 6495 : low0 = 0;
5880 : break;
5881 201 : case POINTER_TYPE:
5882 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5883 201 : && integer_zerop (low0))
5884 : low0 = 0;
5885 : break;
5886 : default:
5887 : break;
5888 : }
5889 :
5890 : /* Canonicalize - [x, max] into - [x, -]. */
5891 210656 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5892 210469 : switch (TREE_CODE (TREE_TYPE (high1)))
5893 : {
5894 51142 : case ENUMERAL_TYPE:
5895 51142 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5896 : GET_MODE_BITSIZE
5897 102284 : (TYPE_MODE (TREE_TYPE (high1)))))
5898 : break;
5899 : /* FALLTHROUGH */
5900 210268 : case INTEGER_TYPE:
5901 210268 : if (tree_int_cst_equal (high1,
5902 210268 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5903 9477 : high1 = 0;
5904 : break;
5905 201 : case POINTER_TYPE:
5906 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5907 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5908 : high1, 1,
5909 201 : build_int_cst (TREE_TYPE (high1), 1),
5910 : 1)))
5911 133 : high1 = 0;
5912 : break;
5913 : default:
5914 : break;
5915 : }
5916 :
5917 : /* The ranges might be also adjacent between the maximum and
5918 : minimum values of the given type. For
5919 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5920 : return + [x + 1, y - 1]. */
5921 210656 : if (low0 == 0 && high1 == 0)
5922 : {
5923 320 : low = range_successor (high0);
5924 320 : high = range_predecessor (low1);
5925 320 : if (low == 0 || high == 0)
5926 : return 0;
5927 :
5928 : in_p = 1;
5929 : }
5930 : else
5931 : return 0;
5932 : }
5933 : }
5934 97485 : else if (subset)
5935 : in_p = 0, low = low0, high = high0;
5936 : else
5937 11638 : in_p = 0, low = low0, high = high1;
5938 : }
5939 :
5940 3194847 : *pin_p = in_p, *plow = low, *phigh = high;
5941 3194847 : return 1;
5942 : }
5943 :
5944 :
5945 : /* Subroutine of fold, looking inside expressions of the form
5946 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5947 : are the three operands of the COND_EXPR. This function is
5948 : being used also to optimize A op B ? C : A, by reversing the
5949 : comparison first.
5950 :
5951 : Return a folded expression whose code is not a COND_EXPR
5952 : anymore, or NULL_TREE if no folding opportunity is found. */
5953 :
5954 : static tree
5955 511604 : fold_cond_expr_with_comparison (location_t loc, tree type,
5956 : enum tree_code comp_code,
5957 : tree arg00, tree arg01, tree arg1, tree arg2)
5958 : {
5959 511604 : tree arg1_type = TREE_TYPE (arg1);
5960 511604 : tree tem;
5961 :
5962 511604 : STRIP_NOPS (arg1);
5963 511604 : STRIP_NOPS (arg2);
5964 :
5965 : /* If we have A op 0 ? A : -A, consider applying the following
5966 : transformations:
5967 :
5968 : A == 0? A : -A same as -A
5969 : A != 0? A : -A same as A
5970 : A >= 0? A : -A same as abs (A)
5971 : A > 0? A : -A same as abs (A)
5972 : A <= 0? A : -A same as -abs (A)
5973 : A < 0? A : -A same as -abs (A)
5974 :
5975 : None of these transformations work for modes with signed
5976 : zeros. If A is +/-0, the first two transformations will
5977 : change the sign of the result (from +0 to -0, or vice
5978 : versa). The last four will fix the sign of the result,
5979 : even though the original expressions could be positive or
5980 : negative, depending on the sign of A.
5981 :
5982 : Note that all these transformations are correct if A is
5983 : NaN, since the two alternatives (A and -A) are also NaNs. */
5984 511604 : if (!HONOR_SIGNED_ZEROS (type)
5985 1023218 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5986 511604 : ? real_zerop (arg01)
5987 510516 : : integer_zerop (arg01))
5988 1370797 : && ((TREE_CODE (arg2) == NEGATE_EXPR
5989 1627 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5990 : /* In the case that A is of the form X-Y, '-A' (arg2) may
5991 : have already been folded to Y-X, check for that. */
5992 346186 : || (TREE_CODE (arg1) == MINUS_EXPR
5993 1718 : && TREE_CODE (arg2) == MINUS_EXPR
5994 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
5995 0 : TREE_OPERAND (arg2, 1), 0)
5996 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
5997 0 : TREE_OPERAND (arg2, 0), 0))))
5998 1403 : switch (comp_code)
5999 : {
6000 0 : case EQ_EXPR:
6001 0 : case UNEQ_EXPR:
6002 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6003 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6004 0 : case NE_EXPR:
6005 0 : case LTGT_EXPR:
6006 0 : return fold_convert_loc (loc, type, arg1);
6007 0 : case UNGE_EXPR:
6008 0 : case UNGT_EXPR:
6009 0 : if (flag_trapping_math)
6010 : break;
6011 : /* Fall through. */
6012 1387 : case GE_EXPR:
6013 1387 : case GT_EXPR:
6014 1387 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6015 : break;
6016 1371 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6017 1371 : return fold_convert_loc (loc, type, tem);
6018 0 : case UNLE_EXPR:
6019 0 : case UNLT_EXPR:
6020 0 : if (flag_trapping_math)
6021 : break;
6022 : /* FALLTHRU */
6023 16 : case LE_EXPR:
6024 16 : case LT_EXPR:
6025 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6026 : break;
6027 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6028 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6029 : {
6030 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6031 : is not, invokes UB both in abs and in the negation of it.
6032 : So, use ABSU_EXPR instead. */
6033 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6034 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6035 16 : tem = negate_expr (tem);
6036 16 : return fold_convert_loc (loc, type, tem);
6037 : }
6038 : else
6039 : {
6040 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6041 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6042 : }
6043 0 : default:
6044 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6045 : break;
6046 : }
6047 :
6048 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6049 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6050 : both transformations are correct when A is NaN: A != 0
6051 : is then true, and A == 0 is false. */
6052 :
6053 510217 : if (!HONOR_SIGNED_ZEROS (type)
6054 510217 : && integer_zerop (arg01) && integer_zerop (arg2))
6055 : {
6056 262590 : if (comp_code == NE_EXPR)
6057 147 : return fold_convert_loc (loc, type, arg1);
6058 262443 : else if (comp_code == EQ_EXPR)
6059 0 : return build_zero_cst (type);
6060 : }
6061 :
6062 : /* Try some transformations of A op B ? A : B.
6063 :
6064 : A == B? A : B same as B
6065 : A != B? A : B same as A
6066 : A >= B? A : B same as max (A, B)
6067 : A > B? A : B same as max (B, A)
6068 : A <= B? A : B same as min (A, B)
6069 : A < B? A : B same as min (B, A)
6070 :
6071 : As above, these transformations don't work in the presence
6072 : of signed zeros. For example, if A and B are zeros of
6073 : opposite sign, the first two transformations will change
6074 : the sign of the result. In the last four, the original
6075 : expressions give different results for (A=+0, B=-0) and
6076 : (A=-0, B=+0), but the transformed expressions do not.
6077 :
6078 : The first two transformations are correct if either A or B
6079 : is a NaN. In the first transformation, the condition will
6080 : be false, and B will indeed be chosen. In the case of the
6081 : second transformation, the condition A != B will be true,
6082 : and A will be chosen.
6083 :
6084 : The conversions to max() and min() are not correct if B is
6085 : a number and A is not. The conditions in the original
6086 : expressions will be false, so all four give B. The min()
6087 : and max() versions would give a NaN instead. */
6088 510070 : if (!HONOR_SIGNED_ZEROS (type)
6089 510070 : && operand_equal_for_comparison_p (arg01, arg2)
6090 : /* Avoid these transformations if the COND_EXPR may be used
6091 : as an lvalue in the C++ front-end. PR c++/19199. */
6092 781291 : && (in_gimple_form
6093 17599 : || VECTOR_TYPE_P (type)
6094 17537 : || (! lang_GNU_CXX ()
6095 15066 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6096 2471 : || ! maybe_lvalue_p (arg1)
6097 2450 : || ! maybe_lvalue_p (arg2)))
6098 : {
6099 269492 : tree comp_op0 = arg00;
6100 269492 : tree comp_op1 = arg01;
6101 269492 : tree comp_type = TREE_TYPE (comp_op0);
6102 :
6103 269492 : switch (comp_code)
6104 : {
6105 0 : case EQ_EXPR:
6106 0 : return fold_convert_loc (loc, type, arg2);
6107 1 : case NE_EXPR:
6108 1 : return fold_convert_loc (loc, type, arg1);
6109 5993 : case LE_EXPR:
6110 5993 : case LT_EXPR:
6111 5993 : case UNLE_EXPR:
6112 5993 : case UNLT_EXPR:
6113 : /* In C++ a ?: expression can be an lvalue, so put the
6114 : operand which will be used if they are equal first
6115 : so that we can convert this back to the
6116 : corresponding COND_EXPR. */
6117 5993 : if (!HONOR_NANS (arg1))
6118 : {
6119 5993 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6120 5993 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6121 5993 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6122 5993 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6123 4647 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6124 : comp_op1, comp_op0);
6125 5993 : return fold_convert_loc (loc, type, tem);
6126 : }
6127 : break;
6128 263498 : case GE_EXPR:
6129 263498 : case GT_EXPR:
6130 263498 : case UNGE_EXPR:
6131 263498 : case UNGT_EXPR:
6132 263498 : if (!HONOR_NANS (arg1))
6133 : {
6134 263496 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6135 263496 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6136 263496 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6137 263496 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6138 3655 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6139 : comp_op1, comp_op0);
6140 263496 : return fold_convert_loc (loc, type, tem);
6141 : }
6142 : break;
6143 0 : case UNEQ_EXPR:
6144 0 : if (!HONOR_NANS (arg1))
6145 0 : return fold_convert_loc (loc, type, arg2);
6146 : break;
6147 0 : case LTGT_EXPR:
6148 0 : if (!HONOR_NANS (arg1))
6149 0 : return fold_convert_loc (loc, type, arg1);
6150 : break;
6151 0 : default:
6152 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6153 : break;
6154 : }
6155 : }
6156 :
6157 : return NULL_TREE;
6158 : }
6159 :
6160 :
6161 :
6162 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6163 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6164 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6165 : false) >= 2)
6166 : #endif
6167 :
6168 : /* EXP is some logical combination of boolean tests. See if we can
6169 : merge it into some range test. Return the new tree if so. */
6170 :
6171 : static tree
6172 25063422 : fold_range_test (location_t loc, enum tree_code code, tree type,
6173 : tree op0, tree op1)
6174 : {
6175 25063422 : int or_op = (code == TRUTH_ORIF_EXPR
6176 25063422 : || code == TRUTH_OR_EXPR);
6177 25063422 : int in0_p, in1_p, in_p;
6178 25063422 : tree low0, low1, low, high0, high1, high;
6179 25063422 : tree tem, lhs, rhs;
6180 :
6181 25063422 : if (!INTEGRAL_TYPE_P (type))
6182 : return 0;
6183 :
6184 25063422 : lhs = make_range (op0, &in0_p, &low0, &high0);
6185 : /* If op0 is known true or false and this is a short-circuiting
6186 : operation we must not merge with op1 since that makes side-effects
6187 : unconditional. So special-case this. */
6188 25063422 : if (!lhs
6189 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6190 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6191 : return op0;
6192 25063420 : rhs = make_range (op1, &in1_p, &low1, &high1);
6193 :
6194 : /* If this is an OR operation, invert both sides; we will invert
6195 : again at the end. */
6196 25063420 : if (or_op)
6197 11760551 : in0_p = ! in0_p, in1_p = ! in1_p;
6198 :
6199 : /* If both expressions are the same, if we can merge the ranges, and we
6200 : can build the range test, return it or it inverted. If one of the
6201 : ranges is always true or always false, consider it to be the same
6202 : expression as the other. */
6203 25031872 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6204 1202744 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6205 : in1_p, low1, high1)
6206 26065629 : && (tem = (build_range_check (loc, type,
6207 : lhs != 0 ? lhs
6208 0 : : rhs != 0 ? rhs : integer_zero_node,
6209 : in_p, low, high))) != 0)
6210 : {
6211 1002179 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6212 : }
6213 :
6214 : /* On machines where the branch cost is expensive, if this is a
6215 : short-circuited branch and the underlying object on both sides
6216 : is the same, make a non-short-circuit operation. */
6217 24061241 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6218 24061241 : if (param_logical_op_non_short_circuit != -1)
6219 7873 : logical_op_non_short_circuit
6220 7873 : = param_logical_op_non_short_circuit;
6221 24061241 : if (logical_op_non_short_circuit
6222 24057270 : && !sanitize_coverage_p ()
6223 24057267 : && lhs != 0 && rhs != 0
6224 24056828 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6225 29542769 : && operand_equal_p (lhs, rhs, 0))
6226 : {
6227 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6228 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6229 : which cases we can't do this. */
6230 170885 : if (simple_operand_p (lhs))
6231 68891 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6232 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6233 34961 : type, op0, op1);
6234 :
6235 135924 : else if (!lang_hooks.decls.global_bindings_p ()
6236 135924 : && !CONTAINS_PLACEHOLDER_P (lhs))
6237 : {
6238 135271 : tree common = save_expr (lhs);
6239 :
6240 248925 : if ((lhs = build_range_check (loc, type, common,
6241 113654 : or_op ? ! in0_p : in0_p,
6242 : low0, high0)) != 0
6243 248925 : && (rhs = build_range_check (loc, type, common,
6244 113654 : or_op ? ! in1_p : in1_p,
6245 : low1, high1)) != 0)
6246 : {
6247 248925 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6248 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6249 135271 : type, lhs, rhs);
6250 : }
6251 : }
6252 : }
6253 :
6254 : return 0;
6255 : }
6256 :
6257 : /* For an expression that has the form
6258 : (A && B) || ~B
6259 : or
6260 : (A || B) && ~B,
6261 : we can drop one of the inner expressions and simplify to
6262 : A || ~B
6263 : or
6264 : A && ~B
6265 : LOC is the location of the resulting expression. OP is the inner
6266 : logical operation; the left-hand side in the examples above, while CMPOP
6267 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6268 : removing a condition that guards another, as in
6269 : (A != NULL && A->...) || A == NULL
6270 : which we must not transform. If RHS_ONLY is true, only eliminate the
6271 : right-most operand of the inner logical operation. */
6272 :
6273 : static tree
6274 129741 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6275 : bool rhs_only)
6276 : {
6277 129741 : enum tree_code code = TREE_CODE (cmpop);
6278 129741 : enum tree_code truthop_code = TREE_CODE (op);
6279 129741 : tree lhs = TREE_OPERAND (op, 0);
6280 129741 : tree rhs = TREE_OPERAND (op, 1);
6281 129741 : tree orig_lhs = lhs, orig_rhs = rhs;
6282 129741 : enum tree_code rhs_code = TREE_CODE (rhs);
6283 129741 : enum tree_code lhs_code = TREE_CODE (lhs);
6284 129741 : enum tree_code inv_code;
6285 :
6286 129741 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6287 : return NULL_TREE;
6288 :
6289 116321 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6290 : return NULL_TREE;
6291 :
6292 38918 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6293 :
6294 38918 : if (rhs_code == truthop_code)
6295 : {
6296 33 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6297 33 : if (newrhs != NULL_TREE)
6298 : {
6299 0 : rhs = newrhs;
6300 0 : rhs_code = TREE_CODE (rhs);
6301 : }
6302 : }
6303 38918 : if (lhs_code == truthop_code && !rhs_only)
6304 : {
6305 476 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6306 476 : if (newlhs != NULL_TREE)
6307 : {
6308 0 : lhs = newlhs;
6309 0 : lhs_code = TREE_CODE (lhs);
6310 : }
6311 : }
6312 :
6313 38918 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6314 38918 : if (inv_code == rhs_code
6315 922 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6316 38954 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6317 : return lhs;
6318 38905 : if (!rhs_only && inv_code == lhs_code
6319 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6320 38997 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6321 : return rhs;
6322 38814 : if (rhs != orig_rhs || lhs != orig_lhs)
6323 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6324 0 : lhs, rhs);
6325 : return NULL_TREE;
6326 : }
6327 :
6328 : /* Find ways of folding logical expressions of LHS and RHS:
6329 : Try to merge two comparisons to the same innermost item.
6330 : Look for range tests like "ch >= '0' && ch <= '9'".
6331 : Look for combinations of simple terms on machines with expensive branches
6332 : and evaluate the RHS unconditionally.
6333 :
6334 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6335 : function and the one above.
6336 :
6337 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6338 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6339 :
6340 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6341 : two operands.
6342 :
6343 : We return the simplified tree or 0 if no optimization is possible. */
6344 :
6345 : static tree
6346 24736992 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6347 : tree lhs, tree rhs)
6348 : {
6349 : /* If this is the "or" of two comparisons, we can do something if
6350 : the comparisons are NE_EXPR. If this is the "and", we can do something
6351 : if the comparisons are EQ_EXPR. I.e.,
6352 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6353 :
6354 : WANTED_CODE is this operation code. For single bit fields, we can
6355 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6356 : comparison for one-bit fields. */
6357 :
6358 24736992 : enum tree_code lcode, rcode;
6359 24736992 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6360 24736992 : tree result;
6361 :
6362 : /* Start by getting the comparison codes. Fail if anything is volatile.
6363 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6364 : it were surrounded with a NE_EXPR. */
6365 :
6366 24736992 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6367 : return 0;
6368 :
6369 21899910 : lcode = TREE_CODE (lhs);
6370 21899910 : rcode = TREE_CODE (rhs);
6371 :
6372 21899910 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6373 : {
6374 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6375 0 : build_int_cst (TREE_TYPE (lhs), 0));
6376 0 : lcode = NE_EXPR;
6377 : }
6378 :
6379 21899910 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6380 : {
6381 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6382 0 : build_int_cst (TREE_TYPE (rhs), 0));
6383 0 : rcode = NE_EXPR;
6384 : }
6385 :
6386 21899910 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6387 19545803 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6388 : return 0;
6389 :
6390 18430856 : ll_arg = TREE_OPERAND (lhs, 0);
6391 18430856 : lr_arg = TREE_OPERAND (lhs, 1);
6392 18430856 : rl_arg = TREE_OPERAND (rhs, 0);
6393 18430856 : rr_arg = TREE_OPERAND (rhs, 1);
6394 :
6395 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6396 18430856 : if (simple_operand_p (ll_arg)
6397 18430856 : && simple_operand_p (lr_arg))
6398 : {
6399 14968369 : if (operand_equal_p (ll_arg, rl_arg, 0)
6400 14968369 : && operand_equal_p (lr_arg, rr_arg, 0))
6401 : {
6402 21239 : result = combine_comparisons (loc, code, lcode, rcode,
6403 : truth_type, ll_arg, lr_arg);
6404 21239 : if (result)
6405 : return result;
6406 : }
6407 14947130 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6408 14947130 : && operand_equal_p (lr_arg, rl_arg, 0))
6409 : {
6410 252 : result = combine_comparisons (loc, code, lcode,
6411 : swap_tree_comparison (rcode),
6412 : truth_type, ll_arg, lr_arg);
6413 252 : if (result)
6414 : return result;
6415 : }
6416 : }
6417 :
6418 8702002 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6419 18409910 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6420 :
6421 : /* If the RHS can be evaluated unconditionally and its operands are
6422 : simple, it wins to evaluate the RHS unconditionally on machines
6423 : with expensive branches. In this case, this isn't a comparison
6424 : that can be merged. */
6425 :
6426 18409910 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6427 : false) >= 2
6428 18409807 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6429 17363875 : && simple_operand_p (rl_arg)
6430 28377819 : && simple_operand_p (rr_arg))
6431 : {
6432 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6433 11148703 : if (code == TRUTH_OR_EXPR
6434 1509766 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6435 618063 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6436 20868 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6437 11166301 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6438 34580 : return build2_loc (loc, NE_EXPR, truth_type,
6439 17290 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6440 : ll_arg, rl_arg),
6441 17290 : build_int_cst (TREE_TYPE (ll_arg), 0));
6442 :
6443 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6444 11131413 : if (code == TRUTH_AND_EXPR
6445 1707459 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6446 821015 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6447 7890 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6448 11133004 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6449 2734 : return build2_loc (loc, EQ_EXPR, truth_type,
6450 1367 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6451 : ll_arg, rl_arg),
6452 1367 : build_int_cst (TREE_TYPE (ll_arg), 0));
6453 : }
6454 :
6455 : return 0;
6456 : }
6457 :
6458 : /* T is an integer expression that is being multiplied, divided, or taken a
6459 : modulus (CODE says which and what kind of divide or modulus) by a
6460 : constant C. See if we can eliminate that operation by folding it with
6461 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6462 : should be used for the computation if wider than our type.
6463 :
6464 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6465 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6466 : expression would not overflow or that overflow is undefined for the type
6467 : in the language in question.
6468 :
6469 : If we return a non-null expression, it is an equivalent form of the
6470 : original computation, but need not be in the original type. */
6471 :
6472 : static tree
6473 101474724 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6474 : {
6475 : /* To avoid exponential search depth, refuse to allow recursion past
6476 : three levels. Beyond that (1) it's highly unlikely that we'll find
6477 : something interesting and (2) we've probably processed it before
6478 : when we built the inner expression. */
6479 :
6480 101474724 : static int depth;
6481 101474724 : tree ret;
6482 :
6483 101474724 : if (depth > 3)
6484 : return NULL;
6485 :
6486 97525452 : depth++;
6487 97525452 : ret = extract_muldiv_1 (t, c, code, wide_type);
6488 97525452 : depth--;
6489 :
6490 97525452 : return ret;
6491 : }
6492 :
6493 : static tree
6494 97525452 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6495 : {
6496 97525452 : tree type = TREE_TYPE (t);
6497 97525452 : enum tree_code tcode = TREE_CODE (t);
6498 97525452 : tree ctype = type;
6499 97525452 : if (wide_type)
6500 : {
6501 32552180 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6502 : {
6503 124 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6504 9102430 : ctype = wide_type;
6505 : }
6506 32552056 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6507 65104112 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6508 9102430 : ctype = wide_type;
6509 : }
6510 97525452 : tree t1, t2;
6511 97525452 : bool same_p = tcode == code;
6512 97525452 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6513 :
6514 : /* Don't deal with constants of zero here; they confuse the code below. */
6515 97525452 : if (integer_zerop (c))
6516 : return NULL_TREE;
6517 :
6518 97509979 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6519 38782404 : op0 = TREE_OPERAND (t, 0);
6520 :
6521 97509979 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6522 12336745 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6523 :
6524 : /* Note that we need not handle conditional operations here since fold
6525 : already handles those cases. So just do arithmetic here. */
6526 97509979 : switch (tcode)
6527 : {
6528 4371180 : case INTEGER_CST:
6529 : /* For a constant, we can always simplify if we are a multiply
6530 : or (for divide and modulus) if it is a multiple of our constant. */
6531 4371180 : if (code == MULT_EXPR
6532 5590308 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6533 1219128 : TYPE_SIGN (type)))
6534 : {
6535 3579698 : tree tem = const_binop (code, fold_convert (ctype, t),
6536 : fold_convert (ctype, c));
6537 : /* If the multiplication overflowed, we lost information on it.
6538 : See PR68142 and PR69845. */
6539 3579698 : if (TREE_OVERFLOW (tem))
6540 : return NULL_TREE;
6541 : return tem;
6542 : }
6543 : break;
6544 :
6545 38245170 : CASE_CONVERT: case NON_LVALUE_EXPR:
6546 38245170 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6547 : break;
6548 : /* If op0 is an expression ... */
6549 36965985 : if ((COMPARISON_CLASS_P (op0)
6550 : || UNARY_CLASS_P (op0)
6551 36965985 : || BINARY_CLASS_P (op0)
6552 33829427 : || VL_EXP_CLASS_P (op0)
6553 33768044 : || EXPRESSION_CLASS_P (op0))
6554 : /* ... and has wrapping overflow, and its type is smaller
6555 : than ctype, then we cannot pass through as widening. */
6556 37117590 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6557 1252015 : && (TYPE_PRECISION (ctype)
6558 1252015 : > TYPE_PRECISION (TREE_TYPE (op0))))
6559 : /* ... or this is a truncation (t is narrower than op0),
6560 : then we cannot pass through this narrowing. */
6561 2732726 : || (TYPE_PRECISION (type)
6562 2732726 : < TYPE_PRECISION (TREE_TYPE (op0)))
6563 : /* ... or signedness changes for division or modulus,
6564 : then we cannot pass through this conversion. */
6565 2703951 : || (code != MULT_EXPR
6566 124157 : && (TYPE_UNSIGNED (ctype)
6567 124157 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6568 : /* ... or has undefined overflow while the converted to
6569 : type has not, we cannot do the operation in the inner type
6570 : as that would introduce undefined overflow. */
6571 2605971 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6572 1930956 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6573 : break;
6574 :
6575 : /* Pass the constant down and see if we can make a simplification. If
6576 : we can, replace this expression with the inner simplification for
6577 : possible later conversion to our or some other type. */
6578 34424988 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6579 34424988 : && TREE_CODE (t2) == INTEGER_CST
6580 34424988 : && !TREE_OVERFLOW (t2)
6581 70119774 : && (t1 = extract_muldiv (op0, t2, code,
6582 : code == MULT_EXPR ? ctype : NULL_TREE))
6583 : != 0)
6584 : return t1;
6585 : break;
6586 :
6587 189 : case ABS_EXPR:
6588 : /* If widening the type changes it from signed to unsigned, then we
6589 : must avoid building ABS_EXPR itself as unsigned. */
6590 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6591 : {
6592 0 : tree cstype = (*signed_type_for) (ctype);
6593 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6594 : {
6595 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6596 0 : return fold_convert (ctype, t1);
6597 : }
6598 : break;
6599 : }
6600 : /* If the constant is negative, we cannot simplify this. */
6601 189 : if (tree_int_cst_sgn (c) == -1)
6602 : break;
6603 : /* FALLTHROUGH */
6604 43749 : case NEGATE_EXPR:
6605 : /* For division and modulus, type can't be unsigned, as e.g.
6606 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6607 : For signed types, even with wrapping overflow, this is fine. */
6608 43749 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6609 : break;
6610 42196 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6611 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6612 : break;
6613 :
6614 785 : case MIN_EXPR: case MAX_EXPR:
6615 : /* If widening the type changes the signedness, then we can't perform
6616 : this optimization as that changes the result. */
6617 785 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6618 : break;
6619 :
6620 : /* Punt for multiplication altogether.
6621 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6622 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6623 : 0U, the latter is 2U.
6624 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6625 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6626 : well defined 0, the latter invokes UB.
6627 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6628 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6629 : well defined 25, the latter invokes UB. */
6630 785 : if (code == MULT_EXPR)
6631 : break;
6632 : /* For division/modulo, punt on c being -1 for MAX, as
6633 : MAX (INT_MIN, 0) / -1 is not equivalent to
6634 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6635 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6636 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6637 : transformation won't make it worse. */
6638 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6639 : break;
6640 :
6641 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6642 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6643 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6644 : {
6645 0 : if (tree_int_cst_sgn (c) < 0)
6646 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6647 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6648 : fold_convert (ctype, t2));
6649 : }
6650 : break;
6651 :
6652 1348 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6653 : /* If the second operand is constant, this is a multiplication
6654 : or floor division, by a power of two, so we can treat it that
6655 : way unless the multiplier or divisor overflows. Signed
6656 : left-shift overflow is implementation-defined rather than
6657 : undefined in C90, so do not convert signed left shift into
6658 : multiplication. */
6659 1348 : if (TREE_CODE (op1) == INTEGER_CST
6660 1332 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6661 : /* const_binop may not detect overflow correctly,
6662 : so check for it explicitly here. */
6663 1214 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6664 1357 : wi::to_wide (op1))
6665 1205 : && (t1 = fold_convert (ctype,
6666 : const_binop (LSHIFT_EXPR, size_one_node,
6667 : op1))) != 0
6668 2553 : && !TREE_OVERFLOW (t1))
6669 2208 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6670 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6671 : ctype,
6672 : fold_convert (ctype, op0),
6673 : t1),
6674 1205 : c, code, wide_type);
6675 : break;
6676 :
6677 8530536 : case PLUS_EXPR: case MINUS_EXPR:
6678 : /* See if we can eliminate the operation on both sides. If we can, we
6679 : can return a new PLUS or MINUS. If we can't, the only remaining
6680 : cases where we can do anything are if the second operand is a
6681 : constant. */
6682 8530536 : t1 = extract_muldiv (op0, c, code, wide_type);
6683 8530536 : t2 = extract_muldiv (op1, c, code, wide_type);
6684 812206 : if (t1 != 0 && t2 != 0
6685 282684 : && TYPE_OVERFLOW_WRAPS (ctype)
6686 8803912 : && (code == MULT_EXPR
6687 : /* If not multiplication, we can only do this if both operands
6688 : are divisible by c. */
6689 0 : || (multiple_of_p (ctype, op0, c)
6690 0 : && multiple_of_p (ctype, op1, c))))
6691 : {
6692 273376 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6693 : fold_convert (ctype, t2));
6694 : }
6695 :
6696 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6697 : This simplifies the logic below. */
6698 8257160 : if (tcode == MINUS_EXPR)
6699 : {
6700 2244457 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6701 : /* If OP1 was not easily negatable, the constant may be OP0. */
6702 2244457 : if (TREE_CODE (op0) == INTEGER_CST)
6703 : {
6704 364020 : std::swap (op0, op1);
6705 364020 : std::swap (t1, t2);
6706 : }
6707 : }
6708 :
6709 8257160 : if (TREE_CODE (op1) != INTEGER_CST)
6710 : break;
6711 :
6712 : /* If either OP1 or C are negative, this optimization is not safe for
6713 : some of the division and remainder types while for others we need
6714 : to change the code. */
6715 3798992 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6716 : {
6717 176570 : if (code == CEIL_DIV_EXPR)
6718 : code = FLOOR_DIV_EXPR;
6719 176568 : else if (code == FLOOR_DIV_EXPR)
6720 : code = CEIL_DIV_EXPR;
6721 176155 : else if (code != MULT_EXPR
6722 176155 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6723 : break;
6724 : }
6725 :
6726 : /* If it's a multiply or a division/modulus operation of a multiple
6727 : of our constant, do the operation and verify it doesn't overflow. */
6728 3793738 : if (code == MULT_EXPR
6729 5008348 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6730 1214610 : TYPE_SIGN (type)))
6731 : {
6732 3002920 : op1 = const_binop (code, fold_convert (ctype, op1),
6733 : fold_convert (ctype, c));
6734 : /* We allow the constant to overflow with wrapping semantics. */
6735 3002920 : if (op1 == 0
6736 3002920 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6737 : break;
6738 : }
6739 : else
6740 : break;
6741 :
6742 : /* If we have an unsigned type, we cannot widen the operation since it
6743 : will change the result if the original computation overflowed. */
6744 2999357 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6745 : break;
6746 :
6747 : /* The last case is if we are a multiply. In that case, we can
6748 : apply the distributive law to commute the multiply and addition
6749 : if the multiplication of the constants doesn't overflow
6750 : and overflow is defined. With undefined overflow
6751 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6752 : But fold_plusminus_mult_expr would factor back any power-of-two
6753 : value so do not distribute in the first place in this case. */
6754 2999357 : if (code == MULT_EXPR
6755 2576300 : && TYPE_OVERFLOW_WRAPS (ctype)
6756 5240115 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6757 680882 : return fold_build2 (tcode, ctype,
6758 : fold_build2 (code, ctype,
6759 : fold_convert (ctype, op0),
6760 : fold_convert (ctype, c)),
6761 : op1);
6762 :
6763 : break;
6764 :
6765 2416984 : case MULT_EXPR:
6766 : /* We have a special case here if we are doing something like
6767 : (C * 8) % 4 since we know that's zero. */
6768 2416984 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6769 2416984 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6770 : /* If the multiplication can overflow we cannot optimize this. */
6771 10806 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6772 337 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6773 2427790 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6774 292 : TYPE_SIGN (type)))
6775 : {
6776 8 : return omit_one_operand (type, integer_zero_node, op0);
6777 : }
6778 :
6779 : /* ... fall through ... */
6780 :
6781 2704950 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6782 2704950 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6783 : /* If we can extract our operation from the LHS, do so and return a
6784 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6785 : do something only if the second operand is a constant. */
6786 2704950 : if (same_p
6787 2281410 : && TYPE_OVERFLOW_WRAPS (ctype)
6788 4811344 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6789 105487 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6790 : fold_convert (ctype, op1));
6791 2599463 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6792 2166514 : && TYPE_OVERFLOW_WRAPS (ctype)
6793 4591009 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6794 939818 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6795 : fold_convert (ctype, t1));
6796 1659645 : else if (TREE_CODE (op1) != INTEGER_CST)
6797 : return 0;
6798 :
6799 : /* If these are the same operation types, we can associate them
6800 : assuming no overflow. */
6801 622075 : if (tcode == code)
6802 : {
6803 199077 : bool overflow_p = false;
6804 199077 : wi::overflow_type overflow_mul;
6805 199077 : signop sign = TYPE_SIGN (ctype);
6806 199077 : unsigned prec = TYPE_PRECISION (ctype);
6807 398154 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6808 199077 : wi::to_wide (c, prec),
6809 199077 : sign, &overflow_mul);
6810 199077 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6811 199077 : if (overflow_mul
6812 1635 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6813 : overflow_p = true;
6814 199010 : if (!overflow_p)
6815 199010 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6816 : wide_int_to_tree (ctype, mul));
6817 199077 : }
6818 :
6819 : /* If these operations "cancel" each other, we have the main
6820 : optimizations of this pass, which occur when either constant is a
6821 : multiple of the other, in which case we replace this with either an
6822 : operation or CODE or TCODE.
6823 :
6824 : If we have an unsigned type, we cannot do this since it will change
6825 : the result if the original computation overflowed. */
6826 423065 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6827 100323 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6828 523345 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6829 100240 : || (tcode == MULT_EXPR
6830 100240 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6831 850 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6832 846 : && code != MULT_EXPR)))
6833 : {
6834 880 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6835 880 : TYPE_SIGN (type)))
6836 : {
6837 104 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6838 : fold_convert (ctype,
6839 : const_binop (TRUNC_DIV_EXPR,
6840 : op1, c)));
6841 : }
6842 776 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6843 776 : TYPE_SIGN (type)))
6844 : {
6845 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6846 : fold_convert (ctype,
6847 : const_binop (TRUNC_DIV_EXPR,
6848 : c, op1)));
6849 : }
6850 : }
6851 : break;
6852 :
6853 : default:
6854 : break;
6855 : }
6856 :
6857 : return 0;
6858 : }
6859 :
6860 : /* Return a node which has the indicated constant VALUE (either 0 or
6861 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6862 : and is of the indicated TYPE. */
6863 :
6864 : tree
6865 117903781 : constant_boolean_node (bool value, tree type)
6866 : {
6867 117903781 : if (type == integer_type_node)
6868 20469382 : return value ? integer_one_node : integer_zero_node;
6869 97434399 : else if (type == boolean_type_node)
6870 92990346 : return value ? boolean_true_node : boolean_false_node;
6871 4444053 : else if (VECTOR_TYPE_P (type))
6872 1997 : return build_vector_from_val (type,
6873 1997 : build_int_cst (TREE_TYPE (type),
6874 2730 : value ? -1 : 0));
6875 : else
6876 4442056 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6877 : }
6878 :
6879 :
6880 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6881 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6882 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6883 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6884 : COND is the first argument to CODE; otherwise (as in the example
6885 : given here), it is the second argument. TYPE is the type of the
6886 : original expression. Return NULL_TREE if no simplification is
6887 : possible. */
6888 :
6889 : static tree
6890 963922 : fold_binary_op_with_conditional_arg (location_t loc,
6891 : enum tree_code code,
6892 : tree type, tree op0, tree op1,
6893 : tree cond, tree arg, int cond_first_p)
6894 : {
6895 963922 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6896 963922 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6897 963922 : tree test, true_value, false_value;
6898 963922 : tree lhs = NULL_TREE;
6899 963922 : tree rhs = NULL_TREE;
6900 963922 : enum tree_code cond_code = COND_EXPR;
6901 :
6902 : /* Do not move possibly trapping operations into the conditional as this
6903 : pessimizes code and causes gimplification issues when applied late. */
6904 983676 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6905 197275 : ANY_INTEGRAL_TYPE_P (type)
6906 967782 : && TYPE_OVERFLOW_TRAPS (type), op1))
6907 : return NULL_TREE;
6908 :
6909 944004 : if (TREE_CODE (cond) == COND_EXPR
6910 344143 : || TREE_CODE (cond) == VEC_COND_EXPR)
6911 : {
6912 603606 : test = TREE_OPERAND (cond, 0);
6913 603606 : true_value = TREE_OPERAND (cond, 1);
6914 603606 : false_value = TREE_OPERAND (cond, 2);
6915 : /* If this operand throws an expression, then it does not make
6916 : sense to try to perform a logical or arithmetic operation
6917 : involving it. */
6918 603606 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6919 7463 : lhs = true_value;
6920 603606 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6921 6 : rhs = false_value;
6922 : }
6923 340398 : else if (!(TREE_CODE (type) != VECTOR_TYPE
6924 340292 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
6925 : {
6926 338426 : tree testtype = TREE_TYPE (cond);
6927 338426 : test = cond;
6928 338426 : true_value = constant_boolean_node (true, testtype);
6929 338426 : false_value = constant_boolean_node (false, testtype);
6930 : }
6931 : else
6932 : /* Detect the case of mixing vector and scalar types - bail out. */
6933 : return NULL_TREE;
6934 :
6935 942032 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
6936 3851 : cond_code = VEC_COND_EXPR;
6937 :
6938 : /* This transformation is only worthwhile if we don't have to wrap ARG
6939 : in a SAVE_EXPR and the operation can be simplified without recursing
6940 : on at least one of the branches once its pushed inside the COND_EXPR. */
6941 942032 : if (!TREE_CONSTANT (arg)
6942 942032 : && (TREE_SIDE_EFFECTS (arg)
6943 448845 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6944 444175 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6945 : return NULL_TREE;
6946 :
6947 509529 : arg = fold_convert_loc (loc, arg_type, arg);
6948 509529 : if (lhs == 0)
6949 : {
6950 503498 : true_value = fold_convert_loc (loc, cond_type, true_value);
6951 503498 : if (cond_first_p)
6952 493423 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
6953 : else
6954 10075 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
6955 : }
6956 509529 : if (rhs == 0)
6957 : {
6958 509523 : false_value = fold_convert_loc (loc, cond_type, false_value);
6959 509523 : if (cond_first_p)
6960 498887 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
6961 : else
6962 10636 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
6963 : }
6964 :
6965 : /* Check that we have simplified at least one of the branches. */
6966 509529 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6967 : return NULL_TREE;
6968 :
6969 488968 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6970 : }
6971 :
6972 :
6973 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
6974 :
6975 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
6976 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
6977 : if ARG - ZERO_ARG is the same as X.
6978 :
6979 : If ARG is NULL, check for any value of type TYPE.
6980 :
6981 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6982 : and finite. The problematic cases are when X is zero, and its mode
6983 : has signed zeros. In the case of rounding towards -infinity,
6984 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6985 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
6986 :
6987 : bool
6988 645027 : fold_real_zero_addition_p (const_tree type, const_tree arg,
6989 : const_tree zero_arg, int negate)
6990 : {
6991 645027 : if (!real_zerop (zero_arg))
6992 : return false;
6993 :
6994 : /* Don't allow the fold with -fsignaling-nans. */
6995 644355 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
6996 : return false;
6997 :
6998 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6999 641017 : if (!HONOR_SIGNED_ZEROS (type))
7000 : return true;
7001 :
7002 : /* There is no case that is safe for all rounding modes. */
7003 624150 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7004 : return false;
7005 :
7006 : /* In a vector or complex, we would need to check the sign of all zeros. */
7007 623487 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7008 2965 : zero_arg = uniform_vector_p (zero_arg);
7009 623487 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7010 1244 : return false;
7011 :
7012 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7013 622243 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7014 252 : negate = !negate;
7015 :
7016 : /* The mode has signed zeros, and we have to honor their sign.
7017 : In this situation, there are only two cases we can return true for.
7018 : (i) X - 0 is the same as X with default rounding.
7019 : (ii) X + 0 is X when X can't possibly be -0.0. */
7020 622243 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7021 : }
7022 :
7023 : /* Subroutine of match.pd that determines if it is safe to optimize
7024 : a floating point comparison of an integer value, known to be between
7025 : LO and HI, using comparison operator CMP, against the real constant
7026 : R in floating point type FMT, as the same integer comparison against
7027 : the integer constant I, with sign ISIGN.
7028 :
7029 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7030 : because the floating point representations of the neighboring integers
7031 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7032 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7033 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7034 : due to truncation/rounding.
7035 : */
7036 : bool
7037 21343 : fold_cmp_float_cst_p (wide_int lo, wide_int hi, enum tree_code cmp,
7038 : const REAL_VALUE_TYPE *r, format_helper fmt,
7039 : wide_int i, signop isign)
7040 : {
7041 21343 : REAL_VALUE_TYPE raw;
7042 21343 : REAL_VALUE_TYPE rnd;
7043 21343 : bool check_im1 = true;
7044 21343 : bool check_ip1 = true;
7045 :
7046 21343 : switch (cmp)
7047 : {
7048 : case EQ_EXPR:
7049 : case NE_EXPR:
7050 : /* Check both i-1 and i+1. */
7051 : break;
7052 :
7053 19655 : case LT_EXPR:
7054 19655 : case GE_EXPR:
7055 : /* Only check i-1. */
7056 19655 : check_ip1 = false;
7057 19655 : break;
7058 :
7059 436 : case LE_EXPR:
7060 436 : case GT_EXPR:
7061 : /* Only check i+1. */
7062 436 : check_im1 = false;
7063 436 : break;
7064 :
7065 : default:
7066 : return false;
7067 : }
7068 :
7069 21343 : if (flag_rounding_math && i != 0)
7070 : {
7071 344 : real_from_integer (&raw, VOIDmode, i, isign);
7072 344 : if (!real_identical (r, &raw))
7073 : return false;
7074 : }
7075 :
7076 21343 : if (check_im1 && wi::gt_p (i, lo, isign))
7077 : {
7078 1360 : if (flag_rounding_math)
7079 : {
7080 344 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7081 344 : real_convert (&rnd, fmt, &raw);
7082 344 : if (!real_identical (&raw, &rnd))
7083 : return false;
7084 : }
7085 : else
7086 1016 : real_from_integer (&rnd, fmt, i - 1, isign);
7087 1016 : if (real_identical (r, &rnd))
7088 : return false;
7089 : }
7090 :
7091 20655 : if (check_ip1 && wi::lt_p (i, hi, isign))
7092 : {
7093 978 : if (flag_rounding_math)
7094 : {
7095 0 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7096 0 : real_convert (&rnd, fmt, &raw);
7097 0 : if (!real_identical (&raw, &rnd))
7098 : return false;
7099 : }
7100 : else
7101 978 : real_from_integer (&rnd, fmt, i + 1, isign);
7102 978 : if (real_identical (r, &rnd))
7103 : return false;
7104 : }
7105 :
7106 : return true;
7107 : }
7108 :
7109 : /* Subroutine of match.pd that optimizes comparisons of a division by
7110 : a nonzero integer constant against an integer constant, i.e.
7111 : X/C1 op C2.
7112 :
7113 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7114 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7115 :
7116 : enum tree_code
7117 1684791 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7118 : tree *hi, bool *neg_overflow)
7119 : {
7120 1684791 : tree prod, tmp, type = TREE_TYPE (c1);
7121 1684791 : signop sign = TYPE_SIGN (type);
7122 1684791 : wi::overflow_type overflow;
7123 :
7124 : /* We have to do this the hard way to detect unsigned overflow.
7125 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7126 1684791 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7127 1684791 : prod = force_fit_type (type, val, -1, overflow);
7128 1684791 : *neg_overflow = false;
7129 :
7130 1684791 : if (sign == UNSIGNED)
7131 : {
7132 1654997 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7133 1654997 : *lo = prod;
7134 :
7135 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7136 1654997 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7137 1654997 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7138 : }
7139 29794 : else if (tree_int_cst_sgn (c1) >= 0)
7140 : {
7141 28395 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7142 28395 : switch (tree_int_cst_sgn (c2))
7143 : {
7144 4860 : case -1:
7145 4860 : *neg_overflow = true;
7146 4860 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7147 4860 : *hi = prod;
7148 4860 : break;
7149 :
7150 14903 : case 0:
7151 14903 : *lo = fold_negate_const (tmp, type);
7152 14903 : *hi = tmp;
7153 14903 : break;
7154 :
7155 8632 : case 1:
7156 8632 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7157 8632 : *lo = prod;
7158 8632 : break;
7159 :
7160 0 : default:
7161 0 : gcc_unreachable ();
7162 : }
7163 : }
7164 : else
7165 : {
7166 : /* A negative divisor reverses the relational operators. */
7167 1399 : code = swap_tree_comparison (code);
7168 :
7169 1399 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7170 1399 : switch (tree_int_cst_sgn (c2))
7171 : {
7172 134 : case -1:
7173 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7174 134 : *lo = prod;
7175 134 : break;
7176 :
7177 167 : case 0:
7178 167 : *hi = fold_negate_const (tmp, type);
7179 167 : *lo = tmp;
7180 167 : break;
7181 :
7182 1098 : case 1:
7183 1098 : *neg_overflow = true;
7184 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7185 1098 : *hi = prod;
7186 1098 : break;
7187 :
7188 0 : default:
7189 0 : gcc_unreachable ();
7190 : }
7191 : }
7192 :
7193 1684791 : if (code != EQ_EXPR && code != NE_EXPR)
7194 : return code;
7195 :
7196 16817 : if (TREE_OVERFLOW (*lo)
7197 16817 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7198 714 : *lo = NULL_TREE;
7199 16817 : if (TREE_OVERFLOW (*hi)
7200 16817 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7201 95 : *hi = NULL_TREE;
7202 :
7203 : return code;
7204 1684791 : }
7205 :
7206 : /* Test whether it is preferable to swap two operands, ARG0 and
7207 : ARG1, for example because ARG0 is an integer constant and ARG1
7208 : isn't. */
7209 :
7210 : bool
7211 1613353342 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7212 : {
7213 1613353342 : if (CONSTANT_CLASS_P (arg1))
7214 : return false;
7215 526215884 : if (CONSTANT_CLASS_P (arg0))
7216 : return true;
7217 :
7218 484820679 : STRIP_NOPS (arg0);
7219 484820679 : STRIP_NOPS (arg1);
7220 :
7221 484820679 : if (TREE_CONSTANT (arg1))
7222 : return false;
7223 467309466 : if (TREE_CONSTANT (arg0))
7224 : return true;
7225 :
7226 : /* Put addresses in arg1. */
7227 466532476 : if (TREE_CODE (arg1) == ADDR_EXPR)
7228 : return false;
7229 447388536 : if (TREE_CODE (arg0) == ADDR_EXPR)
7230 : return true;
7231 :
7232 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7233 : for commutative and comparison operators. Ensuring a canonical
7234 : form allows the optimizers to find additional redundancies without
7235 : having to explicitly check for both orderings. */
7236 446993404 : if (TREE_CODE (arg0) == SSA_NAME
7237 336996223 : && TREE_CODE (arg1) == SSA_NAME
7238 778119864 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7239 : return true;
7240 :
7241 : /* Put SSA_NAMEs last. */
7242 424068737 : if (TREE_CODE (arg1) == SSA_NAME)
7243 : return false;
7244 100765636 : if (TREE_CODE (arg0) == SSA_NAME)
7245 : return true;
7246 :
7247 : /* Put variables last. */
7248 94895873 : if (DECL_P (arg1))
7249 : return false;
7250 51452369 : if (DECL_P (arg0))
7251 : return true;
7252 :
7253 : return false;
7254 : }
7255 :
7256 :
7257 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7258 : means A >= Y && A != MAX, but in this case we know that
7259 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7260 :
7261 : static tree
7262 24031201 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7263 : {
7264 24031201 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7265 :
7266 24031201 : if (TREE_CODE (bound) == LT_EXPR)
7267 4964400 : a = TREE_OPERAND (bound, 0);
7268 19066801 : else if (TREE_CODE (bound) == GT_EXPR)
7269 2790840 : a = TREE_OPERAND (bound, 1);
7270 : else
7271 : return NULL_TREE;
7272 :
7273 7755240 : typea = TREE_TYPE (a);
7274 7755240 : if (!INTEGRAL_TYPE_P (typea)
7275 491463 : && !POINTER_TYPE_P (typea))
7276 : return NULL_TREE;
7277 :
7278 7576765 : if (TREE_CODE (ineq) == LT_EXPR)
7279 : {
7280 1464311 : a1 = TREE_OPERAND (ineq, 1);
7281 1464311 : y = TREE_OPERAND (ineq, 0);
7282 : }
7283 6112454 : else if (TREE_CODE (ineq) == GT_EXPR)
7284 : {
7285 1160838 : a1 = TREE_OPERAND (ineq, 0);
7286 1160838 : y = TREE_OPERAND (ineq, 1);
7287 : }
7288 : else
7289 : return NULL_TREE;
7290 :
7291 2625149 : if (TREE_TYPE (a1) != typea)
7292 : return NULL_TREE;
7293 :
7294 1876365 : if (POINTER_TYPE_P (typea))
7295 : {
7296 : /* Convert the pointer types into integer before taking the difference. */
7297 11287 : tree ta = fold_convert_loc (loc, ssizetype, a);
7298 11287 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7299 11287 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7300 : }
7301 : else
7302 1865078 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7303 :
7304 1876365 : if (!diff || !integer_onep (diff))
7305 1867456 : return NULL_TREE;
7306 :
7307 8909 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7308 : }
7309 :
7310 : /* Fold a sum or difference of at least one multiplication.
7311 : Returns the folded tree or NULL if no simplification could be made. */
7312 :
7313 : static tree
7314 9355394 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7315 : tree arg0, tree arg1)
7316 : {
7317 9355394 : tree arg00, arg01, arg10, arg11;
7318 9355394 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7319 :
7320 : /* (A * C) +- (B * C) -> (A+-B) * C.
7321 : (A * C) +- A -> A * (C+-1).
7322 : We are most concerned about the case where C is a constant,
7323 : but other combinations show up during loop reduction. Since
7324 : it is not difficult, try all four possibilities. */
7325 :
7326 9355394 : if (TREE_CODE (arg0) == MULT_EXPR)
7327 : {
7328 8319860 : arg00 = TREE_OPERAND (arg0, 0);
7329 8319860 : arg01 = TREE_OPERAND (arg0, 1);
7330 : }
7331 1035534 : else if (TREE_CODE (arg0) == INTEGER_CST)
7332 : {
7333 74451 : arg00 = build_one_cst (type);
7334 74451 : arg01 = arg0;
7335 : }
7336 : else
7337 : {
7338 : /* We cannot generate constant 1 for fract. */
7339 961083 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7340 0 : return NULL_TREE;
7341 961083 : arg00 = arg0;
7342 961083 : arg01 = build_one_cst (type);
7343 : }
7344 9355394 : if (TREE_CODE (arg1) == MULT_EXPR)
7345 : {
7346 2400003 : arg10 = TREE_OPERAND (arg1, 0);
7347 2400003 : arg11 = TREE_OPERAND (arg1, 1);
7348 : }
7349 6955391 : else if (TREE_CODE (arg1) == INTEGER_CST)
7350 : {
7351 3638089 : arg10 = build_one_cst (type);
7352 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7353 : the purpose of this canonicalization. */
7354 7045690 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7355 233776 : && negate_expr_p (arg1)
7356 3868577 : && code == PLUS_EXPR)
7357 : {
7358 230488 : arg11 = negate_expr (arg1);
7359 230488 : code = MINUS_EXPR;
7360 : }
7361 : else
7362 : arg11 = arg1;
7363 : }
7364 : else
7365 : {
7366 : /* We cannot generate constant 1 for fract. */
7367 3317302 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7368 0 : return NULL_TREE;
7369 3317302 : arg10 = arg1;
7370 3317302 : arg11 = build_one_cst (type);
7371 : }
7372 9355394 : same = NULL_TREE;
7373 :
7374 : /* Prefer factoring a common non-constant. */
7375 9355394 : if (operand_equal_p (arg00, arg10, 0))
7376 : same = arg00, alt0 = arg01, alt1 = arg11;
7377 9351625 : else if (operand_equal_p (arg01, arg11, 0))
7378 : same = arg01, alt0 = arg00, alt1 = arg10;
7379 9245132 : else if (operand_equal_p (arg00, arg11, 0))
7380 : same = arg00, alt0 = arg01, alt1 = arg10;
7381 9245070 : else if (operand_equal_p (arg01, arg10, 0))
7382 : same = arg01, alt0 = arg00, alt1 = arg11;
7383 :
7384 : /* No identical multiplicands; see if we can find a common
7385 : power-of-two factor in non-power-of-two multiplies. This
7386 : can help in multi-dimensional array access. */
7387 9239880 : else if (TREE_CODE (arg01) == INTEGER_CST
7388 8180634 : && TREE_CODE (arg11) == INTEGER_CST)
7389 : {
7390 7968632 : wide_int int01 = wi::to_wide (arg01);
7391 7968632 : wide_int int11 = wi::to_wide (arg11);
7392 7968632 : bool swap = false;
7393 7968632 : tree maybe_same;
7394 :
7395 : /* Move min of absolute values to int11. */
7396 7968634 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7397 : {
7398 3667033 : std::swap (int01, int11);
7399 3667033 : std::swap (arg00, arg10);
7400 3667033 : maybe_same = arg01;
7401 3667033 : swap = true;
7402 : }
7403 : else
7404 : maybe_same = arg11;
7405 :
7406 7968632 : wide_int factor = wi::abs (int11);
7407 7968632 : if (wi::gtu_p (factor, 1u)
7408 4353492 : && wi::exact_log2 (factor) != -1
7409 12483796 : && (int01 & (factor - 1)) == 0
7410 : /* The remainder should not be a constant, otherwise we
7411 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7412 : increased the number of multiplications necessary. */
7413 9526794 : && TREE_CODE (arg10) != INTEGER_CST)
7414 : {
7415 2624638 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7416 1312319 : wide_int_to_tree (TREE_TYPE (arg00),
7417 1312319 : wi::sdiv_trunc (int01,
7418 : int11)));
7419 1312319 : alt1 = arg10;
7420 1312319 : same = maybe_same;
7421 1312319 : if (swap)
7422 1189864 : std::swap (alt0, alt1);
7423 : }
7424 7968634 : }
7425 :
7426 8084146 : if (!same)
7427 7927561 : return NULL_TREE;
7428 :
7429 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7430 1427833 : || TYPE_OVERFLOW_WRAPS (type)
7431 : /* We are neither factoring zero nor minus one. */
7432 1544916 : || TREE_CODE (same) == INTEGER_CST)
7433 1416207 : return fold_build2_loc (loc, MULT_EXPR, type,
7434 : fold_build2_loc (loc, code, type,
7435 : fold_convert_loc (loc, type, alt0),
7436 : fold_convert_loc (loc, type, alt1)),
7437 1416207 : fold_convert_loc (loc, type, same));
7438 :
7439 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7440 : same may be minus one and thus the multiplication may overflow. Perform
7441 : the sum operation in an unsigned type. */
7442 11626 : tree utype = unsigned_type_for (type);
7443 11626 : tree tem = fold_build2_loc (loc, code, utype,
7444 : fold_convert_loc (loc, utype, alt0),
7445 : fold_convert_loc (loc, utype, alt1));
7446 : /* If the sum evaluated to a constant that is not -INF the multiplication
7447 : cannot overflow. */
7448 23252 : if (TREE_CODE (tem) == INTEGER_CST
7449 18126 : && (wi::to_wide (tem)
7450 18126 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7451 3237 : return fold_build2_loc (loc, MULT_EXPR, type,
7452 3237 : fold_convert (type, tem), same);
7453 :
7454 : /* Do not resort to unsigned multiplication because
7455 : we lose the no-overflow property of the expression. */
7456 : return NULL_TREE;
7457 : }
7458 :
7459 :
7460 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7461 : into the buffer PTR of length LEN bytes.
7462 : Return the number of bytes placed in the buffer, or zero
7463 : upon failure. */
7464 :
7465 : int
7466 56639254 : native_encode_wide_int (tree type, const wide_int_ref &val,
7467 : unsigned char *ptr, int len, int off)
7468 : {
7469 56639254 : int total_bytes;
7470 56639254 : if (BITINT_TYPE_P (type))
7471 : {
7472 17398 : struct bitint_info info;
7473 17398 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7474 17398 : gcc_assert (ok);
7475 17398 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7476 17398 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7477 : {
7478 17093 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7479 : /* More work is needed when adding _BitInt support to PDP endian
7480 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7481 : match target endianity here. */
7482 17093 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7483 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7484 : || (GET_MODE_SIZE (limb_mode)
7485 : >= UNITS_PER_WORD)));
7486 : }
7487 : else
7488 610 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7489 : }
7490 : else
7491 113243712 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7492 56639254 : int byte, offset, word, words;
7493 56639254 : unsigned char value;
7494 :
7495 56639254 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7496 : return 0;
7497 56638766 : if (off == -1)
7498 55760144 : off = 0;
7499 :
7500 56638766 : if (ptr == NULL)
7501 : /* Dry run. */
7502 2827370 : return MIN (len, total_bytes - off);
7503 :
7504 : words = total_bytes / UNITS_PER_WORD;
7505 :
7506 258398368 : for (byte = 0; byte < total_bytes; byte++)
7507 : {
7508 204586972 : int bitpos = byte * BITS_PER_UNIT;
7509 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7510 : number of bytes. */
7511 204586972 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7512 :
7513 204586972 : if (total_bytes > UNITS_PER_WORD)
7514 : {
7515 204586972 : word = byte / UNITS_PER_WORD;
7516 204586972 : if (WORDS_BIG_ENDIAN)
7517 : word = (words - 1) - word;
7518 204586972 : offset = word * UNITS_PER_WORD;
7519 204586972 : if (BYTES_BIG_ENDIAN)
7520 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7521 : else
7522 204586972 : offset += byte % UNITS_PER_WORD;
7523 : }
7524 : else
7525 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7526 204586972 : if (offset >= off && offset - off < len)
7527 203286141 : ptr[offset - off] = value;
7528 : }
7529 53811396 : return MIN (len, total_bytes - off);
7530 : }
7531 :
7532 : /* Subroutine of native_encode_expr. Encode the INTEGER_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 56639254 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7539 : {
7540 56639254 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7541 56639254 : ptr, len, off);
7542 : }
7543 :
7544 :
7545 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7546 : specified by EXPR into the buffer PTR of length LEN bytes.
7547 : Return the number of bytes placed in the buffer, or zero
7548 : upon failure. */
7549 :
7550 : static int
7551 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7552 : {
7553 0 : tree type = TREE_TYPE (expr);
7554 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7555 0 : int total_bytes = GET_MODE_SIZE (mode);
7556 0 : FIXED_VALUE_TYPE value;
7557 0 : tree i_value, i_type;
7558 :
7559 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7560 : return 0;
7561 :
7562 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7563 :
7564 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7565 : return 0;
7566 :
7567 0 : value = TREE_FIXED_CST (expr);
7568 0 : i_value = double_int_to_tree (i_type, value.data);
7569 :
7570 0 : return native_encode_int (i_value, ptr, len, off);
7571 : }
7572 :
7573 :
7574 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7575 : specified by EXPR into the buffer PTR of length LEN bytes.
7576 : Return the number of bytes placed in the buffer, or zero
7577 : upon failure. */
7578 :
7579 : int
7580 843248 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7581 : unsigned char *ptr, int len, int off)
7582 : {
7583 843248 : int total_bytes = GET_MODE_SIZE (mode);
7584 843248 : int byte, offset, word, words, bitpos;
7585 843248 : unsigned char value;
7586 :
7587 : /* There are always 32 bits in each long, no matter the size of
7588 : the hosts long. We handle floating point representations with
7589 : up to 192 bits. */
7590 843248 : long tmp[6];
7591 :
7592 843248 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7593 : return 0;
7594 840948 : if (off == -1)
7595 735718 : off = 0;
7596 :
7597 840948 : if (ptr == NULL)
7598 : /* Dry run. */
7599 136574 : return MIN (len, total_bytes - off);
7600 :
7601 704374 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7602 :
7603 704374 : real_to_target (tmp, val, mode);
7604 :
7605 6921884 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7606 6217510 : bitpos += BITS_PER_UNIT)
7607 : {
7608 6217510 : byte = (bitpos / BITS_PER_UNIT) & 3;
7609 6217510 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7610 :
7611 6217510 : if (UNITS_PER_WORD < 4)
7612 : {
7613 : word = byte / UNITS_PER_WORD;
7614 : if (WORDS_BIG_ENDIAN)
7615 : word = (words - 1) - word;
7616 : offset = word * UNITS_PER_WORD;
7617 : if (BYTES_BIG_ENDIAN)
7618 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7619 : else
7620 : offset += byte % UNITS_PER_WORD;
7621 : }
7622 : else
7623 : {
7624 6217510 : offset = byte;
7625 6217510 : if (BYTES_BIG_ENDIAN)
7626 : {
7627 : /* Reverse bytes within each long, or within the entire float
7628 : if it's smaller than a long (for HFmode). */
7629 : offset = MIN (3, total_bytes - 1) - offset;
7630 : gcc_assert (offset >= 0);
7631 : }
7632 : }
7633 6217510 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7634 6217510 : if (offset >= off
7635 6214270 : && offset - off < len)
7636 6192610 : ptr[offset - off] = value;
7637 : }
7638 704374 : return MIN (len, total_bytes - off);
7639 : }
7640 :
7641 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7642 : specified by EXPR into the buffer PTR of length LEN bytes.
7643 : Return the number of bytes placed in the buffer, or zero
7644 : upon failure. */
7645 :
7646 : static int
7647 10215 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7648 : {
7649 10215 : int rsize, isize;
7650 10215 : tree part;
7651 :
7652 10215 : part = TREE_REALPART (expr);
7653 10215 : rsize = native_encode_expr (part, ptr, len, off);
7654 10215 : if (off == -1 && rsize == 0)
7655 : return 0;
7656 10215 : part = TREE_IMAGPART (expr);
7657 10215 : if (off != -1)
7658 20421 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7659 10215 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7660 : len - rsize, off);
7661 10215 : if (off == -1 && isize != rsize)
7662 : return 0;
7663 10215 : return rsize + isize;
7664 : }
7665 :
7666 : /* Like native_encode_vector, but only encode the first COUNT elements.
7667 : The other arguments are as for native_encode_vector. */
7668 :
7669 : static int
7670 1069231 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7671 : int off, unsigned HOST_WIDE_INT count)
7672 : {
7673 1069231 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7674 2138462 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7675 1070249 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7676 : {
7677 : /* This is the only case in which elements can be smaller than a byte.
7678 : Element 0 is always in the lsb of the containing byte. */
7679 940 : unsigned int elt_bits = TYPE_PRECISION (itype);
7680 940 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7681 940 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7682 : return 0;
7683 :
7684 940 : if (off == -1)
7685 940 : off = 0;
7686 :
7687 : /* Zero the buffer and then set bits later where necessary. */
7688 940 : int extract_bytes = MIN (len, total_bytes - off);
7689 940 : if (ptr)
7690 940 : memset (ptr, 0, extract_bytes);
7691 :
7692 940 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7693 940 : unsigned int first_elt = off * elts_per_byte;
7694 940 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7695 940 : unsigned int elt_mask = (1 << elt_bits) - 1;
7696 17333 : for (unsigned int i = 0; i < extract_elts; ++i)
7697 : {
7698 16393 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7699 16393 : if (TREE_CODE (elt) != INTEGER_CST)
7700 : return 0;
7701 :
7702 16393 : if (ptr && integer_nonzerop (elt))
7703 : {
7704 8441 : unsigned int bit = i * elt_bits;
7705 8441 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7706 : }
7707 : }
7708 : return extract_bytes;
7709 : }
7710 :
7711 1068291 : int offset = 0;
7712 1068291 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7713 4289477 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7714 : {
7715 3817712 : if (off >= size)
7716 : {
7717 22957 : off -= size;
7718 22957 : continue;
7719 : }
7720 3794755 : tree elem = VECTOR_CST_ELT (expr, i);
7721 3794755 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7722 : len - offset, off);
7723 3794755 : if ((off == -1 && res != size) || res == 0)
7724 : return 0;
7725 3794226 : offset += res;
7726 3794226 : if (offset >= len)
7727 595997 : return (off == -1 && i < count - 1) ? 0 : offset;
7728 3198229 : if (off != -1)
7729 435621 : off = 0;
7730 : }
7731 : return offset;
7732 : }
7733 :
7734 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7735 : specified by EXPR into the buffer PTR of length LEN bytes.
7736 : Return the number of bytes placed in the buffer, or zero
7737 : upon failure. */
7738 :
7739 : static int
7740 914686 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7741 : {
7742 914686 : unsigned HOST_WIDE_INT count;
7743 914686 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7744 : return 0;
7745 914686 : return native_encode_vector_part (expr, ptr, len, off, count);
7746 : }
7747 :
7748 :
7749 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7750 : specified by EXPR into the buffer PTR of length LEN bytes.
7751 : Return the number of bytes placed in the buffer, or zero
7752 : upon failure. */
7753 :
7754 : static int
7755 142356 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7756 : {
7757 142356 : tree type = TREE_TYPE (expr);
7758 :
7759 : /* Wide-char strings are encoded in target byte-order so native
7760 : encoding them is trivial. */
7761 142356 : if (BITS_PER_UNIT != CHAR_BIT
7762 142356 : || TREE_CODE (type) != ARRAY_TYPE
7763 142356 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7764 284712 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7765 : return 0;
7766 :
7767 142356 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7768 142356 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7769 : return 0;
7770 141488 : if (off == -1)
7771 56360 : off = 0;
7772 141488 : len = MIN (total_bytes - off, len);
7773 141488 : if (ptr == NULL)
7774 : /* Dry run. */;
7775 : else
7776 : {
7777 141488 : int written = 0;
7778 141488 : if (off < TREE_STRING_LENGTH (expr))
7779 : {
7780 141007 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7781 141007 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7782 : }
7783 141488 : memset (ptr + written, 0, len - written);
7784 : }
7785 : return len;
7786 : }
7787 :
7788 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7789 : specified by EXPR into the buffer PTR of length LEN bytes.
7790 : Return the number of bytes placed in the buffer, or zero
7791 : upon failure. */
7792 :
7793 : static int
7794 46721 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7795 : {
7796 : /* We are only concerned with zero-initialization constructors here. That's
7797 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7798 : deal with. For more general handling of constructors, there is
7799 : native_encode_initializer. */
7800 46721 : if (CONSTRUCTOR_NELTS (expr))
7801 : return 0;
7802 :
7803 : /* Wide-char strings are encoded in target byte-order so native
7804 : encoding them is trivial. */
7805 87048 : if (BITS_PER_UNIT != CHAR_BIT
7806 43524 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7807 : return 0;
7808 :
7809 43524 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7810 43524 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7811 : return 0;
7812 43524 : if (off == -1)
7813 0 : off = 0;
7814 43524 : len = MIN (total_bytes - off, len);
7815 43524 : if (ptr == NULL)
7816 : /* Dry run. */;
7817 : else
7818 43524 : memset (ptr, 0, len);
7819 : return len;
7820 : }
7821 :
7822 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7823 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7824 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7825 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7826 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7827 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7828 : Return the number of bytes placed in the buffer, or zero upon failure. */
7829 :
7830 : int
7831 72996633 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7832 : {
7833 : /* We don't support starting at negative offset and -1 is special. */
7834 72996633 : if (off < -1)
7835 : return 0;
7836 :
7837 72996621 : switch (TREE_CODE (expr))
7838 : {
7839 56637008 : case INTEGER_CST:
7840 56637008 : return native_encode_int (expr, ptr, len, off);
7841 :
7842 843248 : case REAL_CST:
7843 843248 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7844 1686496 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7845 :
7846 0 : case FIXED_CST:
7847 0 : return native_encode_fixed (expr, ptr, len, off);
7848 :
7849 10215 : case COMPLEX_CST:
7850 10215 : return native_encode_complex (expr, ptr, len, off);
7851 :
7852 914686 : case VECTOR_CST:
7853 914686 : return native_encode_vector (expr, ptr, len, off);
7854 :
7855 142356 : case STRING_CST:
7856 142356 : return native_encode_string (expr, ptr, len, off);
7857 :
7858 46721 : case CONSTRUCTOR:
7859 46721 : return native_encode_constructor (expr, ptr, len, off);
7860 :
7861 : default:
7862 : return 0;
7863 : }
7864 : }
7865 :
7866 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7867 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7868 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7869 : machine modes, we can't just use build_nonstandard_integer_type. */
7870 :
7871 : tree
7872 541 : find_bitfield_repr_type (int fieldsize, int len)
7873 : {
7874 541 : machine_mode mode;
7875 1063 : for (int pass = 0; pass < 2; pass++)
7876 : {
7877 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7878 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7879 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7880 7286 : && known_eq (GET_MODE_PRECISION (mode),
7881 : GET_MODE_BITSIZE (mode))
7882 11274 : && known_le (GET_MODE_SIZE (mode), len))
7883 : {
7884 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7885 280 : if (ret && TYPE_MODE (ret) == mode)
7886 : return ret;
7887 : }
7888 : }
7889 :
7890 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7891 261 : if (int_n_enabled_p[i]
7892 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7893 261 : && int_n_trees[i].unsigned_type)
7894 : {
7895 261 : tree ret = int_n_trees[i].unsigned_type;
7896 261 : mode = TYPE_MODE (ret);
7897 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7898 522 : && known_eq (GET_MODE_PRECISION (mode),
7899 : GET_MODE_BITSIZE (mode))
7900 783 : && known_le (GET_MODE_SIZE (mode), len))
7901 : return ret;
7902 : }
7903 :
7904 : return NULL_TREE;
7905 : }
7906 :
7907 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7908 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7909 : to be non-NULL and OFF zero), then in addition to filling the
7910 : bytes pointed by PTR with the value also clear any bits pointed
7911 : by MASK that are known to be initialized, keep them as is for
7912 : e.g. uninitialized padding bits or uninitialized fields. */
7913 :
7914 : int
7915 49698625 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7916 : int off, unsigned char *mask)
7917 : {
7918 49698625 : int r;
7919 :
7920 : /* We don't support starting at negative offset and -1 is special. */
7921 49698625 : if (off < -1 || init == NULL_TREE)
7922 : return 0;
7923 :
7924 49698625 : gcc_assert (mask == NULL || (off == 0 && ptr));
7925 :
7926 49698625 : STRIP_NOPS (init);
7927 49698625 : switch (TREE_CODE (init))
7928 : {
7929 0 : case VIEW_CONVERT_EXPR:
7930 0 : case NON_LVALUE_EXPR:
7931 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7932 0 : mask);
7933 47493365 : default:
7934 47493365 : r = native_encode_expr (init, ptr, len, off);
7935 47493365 : if (mask)
7936 7132 : memset (mask, 0, r);
7937 : return r;
7938 2205260 : case CONSTRUCTOR:
7939 2205260 : tree type = TREE_TYPE (init);
7940 2205260 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7941 2205260 : if (total_bytes < 0)
7942 : return 0;
7943 2205260 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7944 : return 0;
7945 2205184 : int o = off == -1 ? 0 : off;
7946 2205184 : if (TREE_CODE (type) == ARRAY_TYPE)
7947 : {
7948 303318 : tree min_index;
7949 303318 : unsigned HOST_WIDE_INT cnt;
7950 303318 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7951 303318 : constructor_elt *ce;
7952 :
7953 303318 : if (!TYPE_DOMAIN (type)
7954 303318 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7955 : return 0;
7956 :
7957 303318 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7958 303318 : if (fieldsize <= 0)
7959 : return 0;
7960 :
7961 303318 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7962 303318 : if (ptr)
7963 303318 : memset (ptr, '\0', MIN (total_bytes - off, len));
7964 :
7965 46883791 : for (cnt = 0; ; cnt++)
7966 : {
7967 47187109 : tree val = NULL_TREE, index = NULL_TREE;
7968 47187109 : HOST_WIDE_INT pos = curpos, count = 0;
7969 47187109 : bool full = false;
7970 47187109 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7971 : {
7972 47075082 : val = ce->value;
7973 47075082 : index = ce->index;
7974 : }
7975 112027 : else if (mask == NULL
7976 618 : || CONSTRUCTOR_NO_CLEARING (init)
7977 112585 : || curpos >= total_bytes)
7978 : break;
7979 : else
7980 : pos = total_bytes;
7981 :
7982 47075082 : if (index && TREE_CODE (index) == RANGE_EXPR)
7983 : {
7984 18 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
7985 18 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
7986 0 : return 0;
7987 18 : offset_int wpos
7988 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
7989 36 : - wi::to_offset (min_index),
7990 18 : TYPE_PRECISION (sizetype));
7991 18 : wpos *= fieldsize;
7992 18 : if (!wi::fits_shwi_p (pos))
7993 : return 0;
7994 18 : pos = wpos.to_shwi ();
7995 18 : offset_int wcount
7996 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
7997 36 : - wi::to_offset (TREE_OPERAND (index, 0)),
7998 18 : TYPE_PRECISION (sizetype));
7999 18 : if (!wi::fits_shwi_p (wcount))
8000 : return 0;
8001 18 : count = wcount.to_shwi ();
8002 18 : }
8003 46530181 : else if (index)
8004 : {
8005 46530181 : if (TREE_CODE (index) != INTEGER_CST)
8006 0 : return 0;
8007 46530181 : offset_int wpos
8008 46530181 : = wi::sext (wi::to_offset (index)
8009 93060362 : - wi::to_offset (min_index),
8010 46530181 : TYPE_PRECISION (sizetype));
8011 46530181 : wpos *= fieldsize;
8012 46530181 : if (!wi::fits_shwi_p (wpos))
8013 : return 0;
8014 46530181 : pos = wpos.to_shwi ();
8015 : }
8016 :
8017 47076343 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8018 : {
8019 72 : if (valueinit == -1)
8020 : {
8021 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8022 144 : r = native_encode_initializer (zero, ptr + curpos,
8023 : fieldsize, 0,
8024 72 : mask + curpos);
8025 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8026 2 : ggc_free (zero);
8027 72 : if (!r)
8028 : return 0;
8029 72 : valueinit = curpos;
8030 72 : curpos += fieldsize;
8031 : }
8032 102 : while (curpos != pos)
8033 : {
8034 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8035 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8036 30 : curpos += fieldsize;
8037 : }
8038 : }
8039 :
8040 47075154 : curpos = pos;
8041 47075154 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8042 : {
8043 527 : if (count)
8044 : return 0;
8045 527 : if (off == -1
8046 527 : || (curpos >= off
8047 0 : && (curpos + RAW_DATA_LENGTH (val)
8048 0 : <= (HOST_WIDE_INT) off + len)))
8049 : {
8050 527 : if (ptr)
8051 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8052 527 : RAW_DATA_LENGTH (val));
8053 527 : if (mask)
8054 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8055 : }
8056 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8057 0 : && curpos < (HOST_WIDE_INT) off + len)
8058 : {
8059 : /* Partial overlap. */
8060 0 : unsigned char *p = NULL;
8061 0 : int no = 0;
8062 0 : int l;
8063 0 : gcc_assert (mask == NULL);
8064 0 : if (curpos >= off)
8065 : {
8066 0 : if (ptr)
8067 0 : p = ptr + curpos - off;
8068 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8069 : RAW_DATA_LENGTH (val));
8070 : }
8071 : else
8072 : {
8073 0 : p = ptr;
8074 0 : no = off - curpos;
8075 0 : l = len;
8076 : }
8077 0 : if (p)
8078 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8079 : }
8080 527 : curpos += RAW_DATA_LENGTH (val);
8081 527 : val = NULL_TREE;
8082 : }
8083 527 : if (val)
8084 47152597 : do
8085 : {
8086 47152597 : if (off == -1
8087 640693 : || (curpos >= off
8088 214142 : && (curpos + fieldsize
8089 214142 : <= (HOST_WIDE_INT) off + len)))
8090 : {
8091 46695660 : if (full)
8092 : {
8093 78042 : if (ptr)
8094 78042 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8095 : fieldsize);
8096 78042 : if (mask)
8097 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8098 : }
8099 93420109 : else if (!native_encode_initializer (val,
8100 : ptr
8101 46617618 : ? ptr + curpos - o
8102 : : NULL,
8103 : fieldsize,
8104 : off == -1 ? -1
8105 : : 0,
8106 : mask
8107 1117 : ? mask + curpos
8108 : : NULL))
8109 : return 0;
8110 : else
8111 : {
8112 : full = true;
8113 : pos = curpos;
8114 : }
8115 : }
8116 456937 : else if (curpos + fieldsize > off
8117 32646 : && curpos < (HOST_WIDE_INT) off + len)
8118 : {
8119 : /* Partial overlap. */
8120 8135 : unsigned char *p = NULL;
8121 8135 : int no = 0;
8122 8135 : int l;
8123 8135 : gcc_assert (mask == NULL);
8124 8135 : if (curpos >= off)
8125 : {
8126 5875 : if (ptr)
8127 5875 : p = ptr + curpos - off;
8128 5875 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8129 : fieldsize);
8130 : }
8131 : else
8132 : {
8133 2260 : p = ptr;
8134 2260 : no = off - curpos;
8135 2260 : l = len;
8136 : }
8137 8135 : if (!native_encode_initializer (val, p, l, no, NULL))
8138 : return 0;
8139 : }
8140 46961234 : curpos += fieldsize;
8141 : }
8142 46961234 : while (count-- != 0);
8143 46883791 : }
8144 111955 : return MIN (total_bytes - off, len);
8145 : }
8146 1901866 : else if (TREE_CODE (type) == RECORD_TYPE
8147 1901866 : || TREE_CODE (type) == UNION_TYPE)
8148 : {
8149 1901866 : unsigned HOST_WIDE_INT cnt;
8150 1901866 : constructor_elt *ce;
8151 1901866 : tree fld_base = TYPE_FIELDS (type);
8152 1901866 : tree to_free = NULL_TREE;
8153 :
8154 1901866 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8155 1901866 : if (ptr != NULL)
8156 1901866 : memset (ptr, '\0', MIN (total_bytes - o, len));
8157 372787 : for (cnt = 0; ; cnt++)
8158 : {
8159 2274653 : tree val = NULL_TREE, field = NULL_TREE;
8160 2274653 : HOST_WIDE_INT pos = 0, fieldsize;
8161 2274653 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8162 :
8163 2274653 : if (to_free)
8164 : {
8165 0 : ggc_free (to_free);
8166 0 : to_free = NULL_TREE;
8167 : }
8168 :
8169 2274653 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8170 : {
8171 399980 : val = ce->value;
8172 399980 : field = ce->index;
8173 399980 : if (field == NULL_TREE)
8174 : return 0;
8175 :
8176 399980 : pos = int_byte_position (field);
8177 399980 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8178 1496 : continue;
8179 : }
8180 1874673 : else if (mask == NULL
8181 1874673 : || CONSTRUCTOR_NO_CLEARING (init))
8182 : break;
8183 : else
8184 : pos = total_bytes;
8185 :
8186 412509 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8187 : {
8188 : tree fld;
8189 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8190 : {
8191 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8192 44565 : continue;
8193 2083 : if (fld == field)
8194 : break;
8195 528 : if (DECL_PADDING_P (fld))
8196 87 : continue;
8197 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8198 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8199 : return 0;
8200 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8201 382 : continue;
8202 : break;
8203 : }
8204 2509 : if (fld == NULL_TREE)
8205 : {
8206 895 : if (ce == NULL)
8207 : break;
8208 : return 0;
8209 : }
8210 1614 : fld_base = DECL_CHAIN (fld);
8211 1614 : if (fld != field)
8212 : {
8213 59 : cnt--;
8214 59 : field = fld;
8215 59 : pos = int_byte_position (field);
8216 59 : val = build_zero_cst (TREE_TYPE (fld));
8217 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8218 0 : to_free = val;
8219 : }
8220 : }
8221 :
8222 398543 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8223 15087 : && TYPE_DOMAIN (TREE_TYPE (field))
8224 413630 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8225 : {
8226 81 : if (mask || off != -1)
8227 : return 0;
8228 81 : if (val == NULL_TREE)
8229 0 : continue;
8230 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8231 : return 0;
8232 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8233 81 : if (fieldsize < 0
8234 81 : || (int) fieldsize != fieldsize
8235 81 : || (pos + fieldsize) > INT_MAX)
8236 : return 0;
8237 81 : if (pos + fieldsize > total_bytes)
8238 : {
8239 81 : if (ptr != NULL && total_bytes < len)
8240 81 : memset (ptr + total_bytes, '\0',
8241 81 : MIN (pos + fieldsize, len) - total_bytes);
8242 : total_bytes = pos + fieldsize;
8243 : }
8244 : }
8245 : else
8246 : {
8247 398462 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8248 398462 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8249 : return 0;
8250 398462 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8251 : }
8252 398543 : if (fieldsize == 0)
8253 1 : continue;
8254 :
8255 : /* Prepare to deal with integral bit-fields and filter out other
8256 : bit-fields that do not start and end on a byte boundary. */
8257 398542 : if (DECL_BIT_FIELD (field))
8258 : {
8259 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8260 : return 0;
8261 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8262 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8263 : {
8264 2711 : bpos %= BITS_PER_UNIT;
8265 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8266 2711 : epos = fieldsize % BITS_PER_UNIT;
8267 2711 : fieldsize += BITS_PER_UNIT - 1;
8268 2711 : fieldsize /= BITS_PER_UNIT;
8269 : }
8270 0 : else if (bpos % BITS_PER_UNIT
8271 0 : || DECL_SIZE (field) == NULL_TREE
8272 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8273 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8274 : return 0;
8275 : }
8276 :
8277 398542 : if (off != -1 && pos + fieldsize <= off)
8278 3195 : continue;
8279 :
8280 395347 : if (val == NULL_TREE)
8281 0 : continue;
8282 :
8283 395347 : if (DECL_BIT_FIELD (field)
8284 395347 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8285 : {
8286 : /* FIXME: Handle PDP endian. */
8287 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8288 261 : return 0;
8289 :
8290 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8291 6 : val = TREE_OPERAND (val, 0);
8292 2507 : if (TREE_CODE (val) != INTEGER_CST)
8293 : return 0;
8294 :
8295 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8296 2507 : tree repr_type = NULL_TREE;
8297 2507 : HOST_WIDE_INT rpos = 0;
8298 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8299 : {
8300 1978 : rpos = int_byte_position (repr);
8301 1978 : repr_type = TREE_TYPE (repr);
8302 : }
8303 : else
8304 : {
8305 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8306 529 : if (repr_type == NULL_TREE)
8307 : return 0;
8308 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8309 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8310 268 : if (pos + repr_size <= o + len)
8311 : rpos = pos;
8312 : else
8313 : {
8314 14 : rpos = o + len - repr_size;
8315 14 : gcc_assert (rpos <= pos);
8316 : }
8317 : }
8318 :
8319 2246 : if (rpos > pos)
8320 : return 0;
8321 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8322 2246 : int diff = (TYPE_PRECISION (repr_type)
8323 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8324 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8325 2246 : if (!BYTES_BIG_ENDIAN)
8326 2246 : w = wi::lshift (w, bitoff);
8327 : else
8328 : w = wi::lshift (w, diff - bitoff);
8329 2246 : val = wide_int_to_tree (repr_type, w);
8330 :
8331 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8332 : / BITS_PER_UNIT + 1];
8333 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8334 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8335 0 : return 0;
8336 :
8337 2246 : if (ptr == NULL)
8338 0 : continue;
8339 :
8340 : /* If the bitfield does not start at byte boundary, handle
8341 : the partial byte at the start. */
8342 2246 : if (bpos
8343 1351 : && (off == -1 || (pos >= off && len >= 1)))
8344 : {
8345 1276 : if (!BYTES_BIG_ENDIAN)
8346 : {
8347 1276 : int msk = (1 << bpos) - 1;
8348 1276 : buf[pos - rpos] &= ~msk;
8349 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8350 1276 : if (mask)
8351 : {
8352 147 : if (fieldsize > 1 || epos == 0)
8353 129 : mask[pos] &= msk;
8354 : else
8355 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8356 : }
8357 : }
8358 : else
8359 : {
8360 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8361 : buf[pos - rpos] &= msk;
8362 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8363 : if (mask)
8364 : {
8365 : if (fieldsize > 1 || epos == 0)
8366 : mask[pos] &= ~msk;
8367 : else
8368 : mask[pos] &= (~msk
8369 : | ((1 << (BITS_PER_UNIT - epos))
8370 : - 1));
8371 : }
8372 : }
8373 : }
8374 : /* If the bitfield does not end at byte boundary, handle
8375 : the partial byte at the end. */
8376 2246 : if (epos
8377 1724 : && (off == -1
8378 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8379 : {
8380 1621 : if (!BYTES_BIG_ENDIAN)
8381 : {
8382 1621 : int msk = (1 << epos) - 1;
8383 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8384 1621 : buf[pos - rpos + fieldsize - 1]
8385 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8386 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8387 156 : mask[pos + fieldsize - 1] &= ~msk;
8388 : }
8389 : else
8390 : {
8391 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8392 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8393 : buf[pos - rpos + fieldsize - 1]
8394 : |= ptr[pos + fieldsize - 1 - o] & msk;
8395 : if (mask && (fieldsize > 1 || bpos == 0))
8396 : mask[pos + fieldsize - 1] &= msk;
8397 : }
8398 : }
8399 2246 : if (off == -1
8400 1301 : || (pos >= off
8401 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8402 : {
8403 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8404 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8405 75 : memset (mask + pos + (bpos != 0), 0,
8406 75 : fieldsize - (bpos != 0) - (epos != 0));
8407 : }
8408 : else
8409 : {
8410 : /* Partial overlap. */
8411 191 : HOST_WIDE_INT fsz = fieldsize;
8412 191 : gcc_assert (mask == NULL);
8413 191 : if (pos < off)
8414 : {
8415 89 : fsz -= (off - pos);
8416 89 : pos = off;
8417 : }
8418 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8419 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8420 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8421 : }
8422 2246 : continue;
8423 2246 : }
8424 :
8425 392840 : if (off == -1
8426 28604 : || (pos >= off
8427 27774 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8428 : {
8429 384471 : int fldsize = fieldsize;
8430 20235 : if (off == -1)
8431 : {
8432 364236 : tree fld = DECL_CHAIN (field);
8433 5756471 : while (fld)
8434 : {
8435 5410868 : if (TREE_CODE (fld) == FIELD_DECL)
8436 : break;
8437 5392235 : fld = DECL_CHAIN (fld);
8438 : }
8439 364236 : if (fld == NULL_TREE)
8440 345603 : fldsize = len - pos;
8441 : }
8442 416564 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8443 : : NULL,
8444 : fldsize,
8445 : off == -1 ? -1 : 0,
8446 11858 : mask ? mask + pos : NULL);
8447 384471 : if (!r)
8448 : return 0;
8449 364828 : if (off == -1
8450 351833 : && fldsize != fieldsize
8451 1149 : && r > fieldsize
8452 822 : && pos + r > total_bytes)
8453 372787 : total_bytes = pos + r;
8454 : }
8455 : else
8456 : {
8457 : /* Partial overlap. */
8458 8369 : unsigned char *p = NULL;
8459 8369 : int no = 0;
8460 8369 : int l;
8461 8369 : gcc_assert (mask == NULL);
8462 8369 : if (pos >= off)
8463 : {
8464 7539 : if (ptr)
8465 7539 : p = ptr + pos - off;
8466 7539 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8467 : fieldsize);
8468 : }
8469 : else
8470 : {
8471 830 : p = ptr;
8472 830 : no = off - pos;
8473 830 : l = len;
8474 : }
8475 8369 : if (!native_encode_initializer (val, p, l, no, NULL))
8476 : return 0;
8477 : }
8478 372787 : }
8479 1874614 : return MIN (total_bytes - off, len);
8480 : }
8481 : return 0;
8482 : }
8483 : }
8484 :
8485 :
8486 : /* Subroutine of native_interpret_expr. Interpret the contents of
8487 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8488 : If the buffer cannot be interpreted, return NULL_TREE. */
8489 :
8490 : static tree
8491 2865575 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8492 : {
8493 2865575 : int total_bytes;
8494 2865575 : if (BITINT_TYPE_P (type))
8495 : {
8496 31 : struct bitint_info info;
8497 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8498 31 : gcc_assert (ok);
8499 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8500 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8501 : {
8502 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8503 : /* More work is needed when adding _BitInt support to PDP endian
8504 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8505 : match target endianity here. */
8506 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8507 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8508 : || (GET_MODE_SIZE (limb_mode)
8509 : >= UNITS_PER_WORD)));
8510 : }
8511 : else
8512 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8513 : }
8514 : else
8515 5731088 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8516 :
8517 2865575 : if (total_bytes > len)
8518 : return NULL_TREE;
8519 :
8520 2865333 : wide_int result = wi::from_buffer (ptr, total_bytes);
8521 :
8522 2865333 : return wide_int_to_tree (type, result);
8523 2865333 : }
8524 :
8525 :
8526 : /* Subroutine of native_interpret_expr. Interpret the contents of
8527 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8528 : If the buffer cannot be interpreted, return NULL_TREE. */
8529 :
8530 : static tree
8531 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8532 : {
8533 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8534 0 : int total_bytes = GET_MODE_SIZE (mode);
8535 0 : double_int result;
8536 0 : FIXED_VALUE_TYPE fixed_value;
8537 :
8538 0 : if (total_bytes > len
8539 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8540 : return NULL_TREE;
8541 :
8542 0 : result = double_int::from_buffer (ptr, total_bytes);
8543 0 : fixed_value = fixed_from_double_int (result, mode);
8544 :
8545 0 : return build_fixed (type, fixed_value);
8546 : }
8547 :
8548 :
8549 : /* Subroutine of native_interpret_expr. Interpret the contents of
8550 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8551 : If the buffer cannot be interpreted, return NULL_TREE. */
8552 :
8553 : tree
8554 36944 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8555 : {
8556 36944 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8557 36944 : int total_bytes = GET_MODE_SIZE (mode);
8558 36944 : unsigned char value;
8559 : /* There are always 32 bits in each long, no matter the size of
8560 : the hosts long. We handle floating point representations with
8561 : up to 192 bits. */
8562 36944 : REAL_VALUE_TYPE r;
8563 36944 : long tmp[6];
8564 :
8565 36944 : if (total_bytes > len || total_bytes > 24)
8566 : return NULL_TREE;
8567 36883 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8568 :
8569 36883 : memset (tmp, 0, sizeof (tmp));
8570 270691 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8571 233808 : bitpos += BITS_PER_UNIT)
8572 : {
8573 : /* Both OFFSET and BYTE index within a long;
8574 : bitpos indexes the whole float. */
8575 233808 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8576 233808 : if (UNITS_PER_WORD < 4)
8577 : {
8578 : int word = byte / UNITS_PER_WORD;
8579 : if (WORDS_BIG_ENDIAN)
8580 : word = (words - 1) - word;
8581 : offset = word * UNITS_PER_WORD;
8582 : if (BYTES_BIG_ENDIAN)
8583 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8584 : else
8585 : offset += byte % UNITS_PER_WORD;
8586 : }
8587 : else
8588 : {
8589 233808 : offset = byte;
8590 233808 : if (BYTES_BIG_ENDIAN)
8591 : {
8592 : /* Reverse bytes within each long, or within the entire float
8593 : if it's smaller than a long (for HFmode). */
8594 : offset = MIN (3, total_bytes - 1) - offset;
8595 : gcc_assert (offset >= 0);
8596 : }
8597 : }
8598 233808 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8599 :
8600 233808 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8601 : }
8602 :
8603 36883 : real_from_target (&r, tmp, mode);
8604 36883 : return build_real (type, r);
8605 : }
8606 :
8607 :
8608 : /* Subroutine of native_interpret_expr. Interpret the contents of
8609 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8610 : If the buffer cannot be interpreted, return NULL_TREE. */
8611 :
8612 : static tree
8613 1596 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8614 : {
8615 1596 : tree etype, rpart, ipart;
8616 1596 : int size;
8617 :
8618 1596 : etype = TREE_TYPE (type);
8619 1596 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8620 1596 : if (size * 2 > len)
8621 : return NULL_TREE;
8622 1563 : rpart = native_interpret_expr (etype, ptr, size);
8623 1563 : if (!rpart)
8624 : return NULL_TREE;
8625 1382 : ipart = native_interpret_expr (etype, ptr+size, size);
8626 1382 : if (!ipart)
8627 : return NULL_TREE;
8628 1382 : return build_complex (type, rpart, ipart);
8629 : }
8630 :
8631 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8632 : which contains LEN bytes. The vector is known to be encodable using
8633 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8634 :
8635 : Return the vector on success, otherwise return null. */
8636 :
8637 : static tree
8638 236607 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8639 : unsigned int len, unsigned int npatterns,
8640 : unsigned int nelts_per_pattern)
8641 : {
8642 236607 : tree elt_type = TREE_TYPE (type);
8643 236607 : if (VECTOR_BOOLEAN_TYPE_P (type)
8644 236610 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8645 : {
8646 : /* This is the only case in which elements can be smaller than a byte.
8647 : Element 0 is always in the lsb of the containing byte. */
8648 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8649 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8650 : return NULL_TREE;
8651 :
8652 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8653 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8654 : {
8655 16 : unsigned int bit_index = i * elt_bits;
8656 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8657 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8658 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8659 17 : ? build_all_ones_cst (elt_type)
8660 1 : : build_zero_cst (elt_type));
8661 : }
8662 1 : return builder.build ();
8663 1 : }
8664 :
8665 236606 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8666 236606 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8667 : return NULL_TREE;
8668 :
8669 236606 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8670 941804 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8671 : {
8672 705332 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8673 705332 : if (!elt)
8674 134 : return NULL_TREE;
8675 705198 : builder.quick_push (elt);
8676 705198 : bytes += elt_bytes;
8677 : }
8678 236472 : return builder.build ();
8679 236606 : }
8680 :
8681 : /* Subroutine of native_interpret_expr. Interpret the contents of
8682 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8683 : If the buffer cannot be interpreted, return NULL_TREE. */
8684 :
8685 : static tree
8686 82064 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8687 : {
8688 82064 : unsigned HOST_WIDE_INT size;
8689 :
8690 82064 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8691 82064 : || size > len)
8692 2 : return NULL_TREE;
8693 :
8694 82062 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8695 82062 : return native_interpret_vector_part (type, ptr, len, count, 1);
8696 : }
8697 :
8698 :
8699 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8700 : the buffer PTR of length LEN as a constant of type TYPE. For
8701 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8702 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8703 : return NULL_TREE. */
8704 :
8705 : tree
8706 3142935 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8707 : {
8708 3142935 : switch (TREE_CODE (type))
8709 : {
8710 2865575 : case INTEGER_TYPE:
8711 2865575 : case ENUMERAL_TYPE:
8712 2865575 : case BOOLEAN_TYPE:
8713 2865575 : case POINTER_TYPE:
8714 2865575 : case REFERENCE_TYPE:
8715 2865575 : case OFFSET_TYPE:
8716 2865575 : case BITINT_TYPE:
8717 2865575 : return native_interpret_int (type, ptr, len);
8718 :
8719 35357 : case REAL_TYPE:
8720 35357 : if (tree ret = native_interpret_real (type, ptr, len))
8721 : {
8722 : /* For floating point values in composite modes, punt if this
8723 : folding doesn't preserve bit representation. As the mode doesn't
8724 : have fixed precision while GCC pretends it does, there could be
8725 : valid values that GCC can't really represent accurately.
8726 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8727 : bit combinationations which GCC doesn't preserve. */
8728 35296 : unsigned char buf[24 * 2];
8729 35296 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8730 35296 : int total_bytes = GET_MODE_SIZE (mode);
8731 35296 : memcpy (buf + 24, ptr, total_bytes);
8732 35296 : clear_type_padding_in_mask (type, buf + 24);
8733 35296 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8734 35296 : || memcmp (buf + 24, buf, total_bytes) != 0)
8735 488 : return NULL_TREE;
8736 : return ret;
8737 : }
8738 : return NULL_TREE;
8739 :
8740 0 : case FIXED_POINT_TYPE:
8741 0 : return native_interpret_fixed (type, ptr, len);
8742 :
8743 1596 : case COMPLEX_TYPE:
8744 1596 : return native_interpret_complex (type, ptr, len);
8745 :
8746 82064 : case VECTOR_TYPE:
8747 82064 : return native_interpret_vector (type, ptr, len);
8748 :
8749 : default:
8750 : return NULL_TREE;
8751 : }
8752 : }
8753 :
8754 : /* Returns true if we can interpret the contents of a native encoding
8755 : as TYPE. */
8756 :
8757 : bool
8758 341724 : can_native_interpret_type_p (tree type)
8759 : {
8760 341724 : switch (TREE_CODE (type))
8761 : {
8762 : case INTEGER_TYPE:
8763 : case ENUMERAL_TYPE:
8764 : case BOOLEAN_TYPE:
8765 : case POINTER_TYPE:
8766 : case REFERENCE_TYPE:
8767 : case FIXED_POINT_TYPE:
8768 : case REAL_TYPE:
8769 : case COMPLEX_TYPE:
8770 : case VECTOR_TYPE:
8771 : case OFFSET_TYPE:
8772 : return true;
8773 38266 : default:
8774 38266 : return false;
8775 : }
8776 : }
8777 :
8778 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8779 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8780 :
8781 : tree
8782 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8783 : int len)
8784 : {
8785 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8786 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8787 : {
8788 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8789 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8790 : return NULL_TREE;
8791 :
8792 197 : HOST_WIDE_INT cnt = 0;
8793 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8794 : {
8795 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8796 : return NULL_TREE;
8797 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8798 : }
8799 197 : if (eltsz == 0)
8800 0 : cnt = 0;
8801 197 : HOST_WIDE_INT pos = 0;
8802 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8803 : {
8804 439 : tree v = NULL_TREE;
8805 439 : if (pos >= len || pos + eltsz > len)
8806 9631 : return NULL_TREE;
8807 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8808 : {
8809 367 : v = native_interpret_expr (TREE_TYPE (type),
8810 367 : ptr + off + pos, eltsz);
8811 367 : if (v == NULL_TREE)
8812 : return NULL_TREE;
8813 : }
8814 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8815 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8816 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8817 : eltsz);
8818 72 : if (v == NULL_TREE)
8819 0 : return NULL_TREE;
8820 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8821 : }
8822 197 : return build_constructor (type, elts);
8823 : }
8824 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8825 : return NULL_TREE;
8826 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8827 : {
8828 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8829 796932 : || is_empty_type (TREE_TYPE (field)))
8830 761072 : continue;
8831 13494 : tree fld = field;
8832 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8833 13494 : int diff = 0;
8834 13494 : tree v = NULL_TREE;
8835 13494 : if (DECL_BIT_FIELD (field))
8836 : {
8837 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8838 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8839 : {
8840 168 : poly_int64 bitoffset;
8841 168 : poly_uint64 field_offset, fld_offset;
8842 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8843 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8844 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8845 : else
8846 : bitoffset = 0;
8847 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8848 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8849 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8850 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8851 168 : if (!bitoffset.is_constant (&bitoff)
8852 168 : || bitoff < 0
8853 168 : || bitoff > diff)
8854 0 : return NULL_TREE;
8855 : }
8856 : else
8857 : {
8858 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8859 : return NULL_TREE;
8860 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8861 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8862 12 : bpos %= BITS_PER_UNIT;
8863 12 : fieldsize += bpos;
8864 12 : fieldsize += BITS_PER_UNIT - 1;
8865 12 : fieldsize /= BITS_PER_UNIT;
8866 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8867 12 : if (repr_type == NULL_TREE)
8868 : return NULL_TREE;
8869 12 : sz = int_size_in_bytes (repr_type);
8870 12 : if (sz < 0 || sz > len)
8871 : return NULL_TREE;
8872 12 : pos = int_byte_position (field);
8873 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8874 : return NULL_TREE;
8875 12 : HOST_WIDE_INT rpos;
8876 12 : if (pos + sz <= len)
8877 : rpos = pos;
8878 : else
8879 : {
8880 0 : rpos = len - sz;
8881 0 : gcc_assert (rpos <= pos);
8882 : }
8883 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8884 12 : pos = rpos;
8885 12 : diff = (TYPE_PRECISION (repr_type)
8886 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8887 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8888 12 : if (v == NULL_TREE)
8889 : return NULL_TREE;
8890 : fld = NULL_TREE;
8891 : }
8892 : }
8893 :
8894 168 : if (fld)
8895 : {
8896 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
8897 13482 : if (sz < 0 || sz > len)
8898 : return NULL_TREE;
8899 13482 : tree byte_pos = byte_position (fld);
8900 13482 : if (!tree_fits_shwi_p (byte_pos))
8901 : return NULL_TREE;
8902 13482 : pos = tree_to_shwi (byte_pos);
8903 13482 : if (pos < 0 || pos > len || pos + sz > len)
8904 : return NULL_TREE;
8905 : }
8906 13482 : if (fld == NULL_TREE)
8907 : /* Already handled above. */;
8908 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8909 : {
8910 6262 : v = native_interpret_expr (TREE_TYPE (fld),
8911 6262 : ptr + off + pos, sz);
8912 6262 : if (v == NULL_TREE)
8913 : return NULL_TREE;
8914 : }
8915 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8916 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8917 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8918 7232 : if (v == NULL_TREE)
8919 : return NULL_TREE;
8920 13494 : if (fld != field)
8921 : {
8922 180 : if (TREE_CODE (v) != INTEGER_CST)
8923 : return NULL_TREE;
8924 :
8925 : /* FIXME: Figure out how to handle PDP endian bitfields. */
8926 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8927 : return NULL_TREE;
8928 180 : if (!BYTES_BIG_ENDIAN)
8929 180 : v = wide_int_to_tree (TREE_TYPE (field),
8930 360 : wi::lrshift (wi::to_wide (v), bitoff));
8931 : else
8932 : v = wide_int_to_tree (TREE_TYPE (field),
8933 : wi::lrshift (wi::to_wide (v),
8934 : diff - bitoff));
8935 : }
8936 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8937 : }
8938 9434 : return build_constructor (type, elts);
8939 : }
8940 :
8941 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8942 : or extracted constant positions and/or sizes aren't byte aligned. */
8943 :
8944 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8945 : bits between adjacent elements. AMNT should be within
8946 : [0, BITS_PER_UNIT).
8947 : Example, AMNT = 2:
8948 : 00011111|11100000 << 2 = 01111111|10000000
8949 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8950 :
8951 : void
8952 28712 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8953 : unsigned int amnt)
8954 : {
8955 28712 : if (amnt == 0)
8956 : return;
8957 :
8958 16713 : unsigned char carry_over = 0U;
8959 16713 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8960 16713 : unsigned char clear_mask = (~0U) << amnt;
8961 :
8962 97216 : for (unsigned int i = 0; i < sz; i++)
8963 : {
8964 80503 : unsigned prev_carry_over = carry_over;
8965 80503 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8966 :
8967 80503 : ptr[i] <<= amnt;
8968 80503 : if (i != 0)
8969 : {
8970 63790 : ptr[i] &= clear_mask;
8971 63790 : ptr[i] |= prev_carry_over;
8972 : }
8973 : }
8974 : }
8975 :
8976 : /* Like shift_bytes_in_array_left but for big-endian.
8977 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
8978 : bits between adjacent elements. AMNT should be within
8979 : [0, BITS_PER_UNIT).
8980 : Example, AMNT = 2:
8981 : 00011111|11100000 >> 2 = 00000111|11111000
8982 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
8983 :
8984 : void
8985 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
8986 : unsigned int amnt)
8987 : {
8988 8 : if (amnt == 0)
8989 : return;
8990 :
8991 4 : unsigned char carry_over = 0U;
8992 4 : unsigned char carry_mask = ~(~0U << amnt);
8993 :
8994 12 : for (unsigned int i = 0; i < sz; i++)
8995 : {
8996 8 : unsigned prev_carry_over = carry_over;
8997 8 : carry_over = ptr[i] & carry_mask;
8998 :
8999 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9000 8 : ptr[i] >>= amnt;
9001 8 : ptr[i] |= prev_carry_over;
9002 : }
9003 : }
9004 :
9005 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9006 : directly on the VECTOR_CST encoding, in a way that works for variable-
9007 : length vectors. Return the resulting VECTOR_CST on success or null
9008 : on failure. */
9009 :
9010 : static tree
9011 163468 : fold_view_convert_vector_encoding (tree type, tree expr)
9012 : {
9013 163468 : tree expr_type = TREE_TYPE (expr);
9014 163468 : poly_uint64 type_bits, expr_bits;
9015 163468 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9016 163468 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9017 0 : return NULL_TREE;
9018 :
9019 163468 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9020 163468 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9021 163468 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9022 163468 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9023 :
9024 : /* We can only preserve the semantics of a stepped pattern if the new
9025 : vector element is an integer of the same size. */
9026 163468 : if (VECTOR_CST_STEPPED_P (expr)
9027 163468 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9028 : return NULL_TREE;
9029 :
9030 : /* The number of bits needed to encode one element from every pattern
9031 : of the original vector. */
9032 154545 : unsigned int expr_sequence_bits
9033 154545 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9034 :
9035 : /* The number of bits needed to encode one element from every pattern
9036 : of the result. */
9037 154545 : unsigned int type_sequence_bits
9038 154545 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9039 :
9040 : /* Don't try to read more bytes than are available, which can happen
9041 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9042 : The general VIEW_CONVERT handling can cope with that case, so there's
9043 : no point complicating things here. */
9044 154545 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9045 154545 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9046 : BITS_PER_UNIT);
9047 154545 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9048 154545 : if (known_gt (buffer_bits, expr_bits))
9049 : return NULL_TREE;
9050 :
9051 : /* Get enough bytes of EXPR to form the new encoding. */
9052 154545 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9053 154545 : buffer.quick_grow (buffer_bytes);
9054 154545 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9055 154545 : buffer_bits / expr_elt_bits)
9056 : != (int) buffer_bytes)
9057 : return NULL_TREE;
9058 :
9059 : /* Re-encode the bytes as TYPE. */
9060 154545 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9061 309090 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9062 154545 : type_npatterns, nelts_per_pattern);
9063 154545 : }
9064 :
9065 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9066 : TYPE at compile-time. If we're unable to perform the conversion
9067 : return NULL_TREE. */
9068 :
9069 : static tree
9070 13139839 : fold_view_convert_expr (tree type, tree expr)
9071 : {
9072 13139839 : unsigned char buffer[128];
9073 13139839 : unsigned char *buf;
9074 13139839 : int len;
9075 13139839 : HOST_WIDE_INT l;
9076 :
9077 : /* Check that the host and target are sane. */
9078 13139839 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9079 : return NULL_TREE;
9080 :
9081 13139839 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9082 163468 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9083 : return res;
9084 :
9085 12985361 : l = int_size_in_bytes (type);
9086 12985361 : if (l > (int) sizeof (buffer)
9087 12985361 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9088 : {
9089 0 : buf = XALLOCAVEC (unsigned char, l);
9090 0 : len = l;
9091 : }
9092 : else
9093 : {
9094 : buf = buffer;
9095 : len = sizeof (buffer);
9096 : }
9097 12985361 : len = native_encode_expr (expr, buf, len);
9098 12985361 : if (len == 0)
9099 : return NULL_TREE;
9100 :
9101 1857470 : return native_interpret_expr (type, buf, len);
9102 : }
9103 :
9104 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9105 : to avoid confusing the gimplify process. */
9106 :
9107 : tree
9108 578603049 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9109 : {
9110 : /* The size of the object is not relevant when talking about its address. */
9111 578603049 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9112 0 : t = TREE_OPERAND (t, 0);
9113 :
9114 578603049 : if (INDIRECT_REF_P (t))
9115 : {
9116 62446281 : t = TREE_OPERAND (t, 0);
9117 :
9118 62446281 : if (TREE_TYPE (t) != ptrtype)
9119 40027541 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9120 : }
9121 516156768 : else if (TREE_CODE (t) == MEM_REF
9122 516156768 : && integer_zerop (TREE_OPERAND (t, 1)))
9123 : {
9124 1671023 : t = TREE_OPERAND (t, 0);
9125 :
9126 1671023 : if (TREE_TYPE (t) != ptrtype)
9127 1096711 : t = fold_convert_loc (loc, ptrtype, t);
9128 : }
9129 514485745 : else if (TREE_CODE (t) == MEM_REF
9130 514485745 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9131 663 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9132 : TREE_OPERAND (t, 0),
9133 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9134 514485082 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9135 : {
9136 30666836 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9137 :
9138 30666836 : if (TREE_TYPE (t) != ptrtype)
9139 16330 : t = fold_convert_loc (loc, ptrtype, t);
9140 : }
9141 : else
9142 483818246 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9143 :
9144 : return t;
9145 : }
9146 :
9147 : /* Build an expression for the address of T. */
9148 :
9149 : tree
9150 504288201 : build_fold_addr_expr_loc (location_t loc, tree t)
9151 : {
9152 504288201 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9153 :
9154 504288201 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9155 : }
9156 :
9157 : /* Fold a unary expression of code CODE and type TYPE with operand
9158 : OP0. Return the folded expression if folding is successful.
9159 : Otherwise, return NULL_TREE. */
9160 :
9161 : tree
9162 2041308430 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9163 : {
9164 2041308430 : tree tem;
9165 2041308430 : tree arg0;
9166 2041308430 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9167 :
9168 2041308430 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9169 : && TREE_CODE_LENGTH (code) == 1);
9170 :
9171 2041308430 : arg0 = op0;
9172 2041308430 : if (arg0)
9173 : {
9174 2041295178 : if (CONVERT_EXPR_CODE_P (code)
9175 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9176 : {
9177 : /* Don't use STRIP_NOPS, because signedness of argument type
9178 : matters. */
9179 1158779776 : STRIP_SIGN_NOPS (arg0);
9180 : }
9181 : else
9182 : {
9183 : /* Strip any conversions that don't change the mode. This
9184 : is safe for every expression, except for a comparison
9185 : expression because its signedness is derived from its
9186 : operands.
9187 :
9188 : Note that this is done as an internal manipulation within
9189 : the constant folder, in order to find the simplest
9190 : representation of the arguments so that their form can be
9191 : studied. In any cases, the appropriate type conversions
9192 : should be put back in the tree that will get out of the
9193 : constant folder. */
9194 882515402 : STRIP_NOPS (arg0);
9195 : }
9196 :
9197 2041295178 : if (CONSTANT_CLASS_P (arg0))
9198 : {
9199 324968538 : tree tem = const_unop (code, type, arg0);
9200 324968538 : if (tem)
9201 : {
9202 285049382 : if (TREE_TYPE (tem) != type)
9203 9797 : tem = fold_convert_loc (loc, type, tem);
9204 285049382 : return tem;
9205 : }
9206 : }
9207 : }
9208 :
9209 1756259048 : tem = generic_simplify (loc, code, type, op0);
9210 1756259048 : if (tem)
9211 : return tem;
9212 :
9213 1321197600 : if (TREE_CODE_CLASS (code) == tcc_unary)
9214 : {
9215 749137268 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9216 1083465 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9217 : fold_build1_loc (loc, code, type,
9218 1083465 : fold_convert_loc (loc, TREE_TYPE (op0),
9219 2166930 : TREE_OPERAND (arg0, 1))));
9220 748053803 : else if (TREE_CODE (arg0) == COND_EXPR)
9221 : {
9222 549673 : tree arg01 = TREE_OPERAND (arg0, 1);
9223 549673 : tree arg02 = TREE_OPERAND (arg0, 2);
9224 549673 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9225 545491 : arg01 = fold_build1_loc (loc, code, type,
9226 : fold_convert_loc (loc,
9227 545491 : TREE_TYPE (op0), arg01));
9228 549673 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9229 539365 : arg02 = fold_build1_loc (loc, code, type,
9230 : fold_convert_loc (loc,
9231 539365 : TREE_TYPE (op0), arg02));
9232 549673 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9233 : arg01, arg02);
9234 :
9235 : /* If this was a conversion, and all we did was to move into
9236 : inside the COND_EXPR, bring it back out. But leave it if
9237 : it is a conversion from integer to integer and the
9238 : result precision is no wider than a word since such a
9239 : conversion is cheap and may be optimized away by combine,
9240 : while it couldn't if it were outside the COND_EXPR. Then return
9241 : so we don't get into an infinite recursion loop taking the
9242 : conversion out and then back in. */
9243 :
9244 549673 : if ((CONVERT_EXPR_CODE_P (code)
9245 10246 : || code == NON_LVALUE_EXPR)
9246 539446 : && TREE_CODE (tem) == COND_EXPR
9247 519912 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9248 456676 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9249 290168 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9250 289956 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9251 289956 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9252 289956 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9253 860545 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9254 21786 : && (INTEGRAL_TYPE_P
9255 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9256 21746 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9257 21633 : || flag_syntax_only))
9258 267493 : tem = build1_loc (loc, code, type,
9259 : build3 (COND_EXPR,
9260 267493 : TREE_TYPE (TREE_OPERAND
9261 : (TREE_OPERAND (tem, 1), 0)),
9262 267493 : TREE_OPERAND (tem, 0),
9263 267493 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9264 267493 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9265 : 0)));
9266 549673 : return tem;
9267 : }
9268 : }
9269 :
9270 1319564462 : switch (code)
9271 : {
9272 42605083 : case NON_LVALUE_EXPR:
9273 42605083 : if (!maybe_lvalue_p (op0))
9274 31083435 : return fold_convert_loc (loc, type, op0);
9275 : return NULL_TREE;
9276 :
9277 695231993 : CASE_CONVERT:
9278 695231993 : case FLOAT_EXPR:
9279 695231993 : case FIX_TRUNC_EXPR:
9280 695231993 : if (COMPARISON_CLASS_P (op0))
9281 : {
9282 : /* If we have (type) (a CMP b) and type is an integral type, return
9283 : new expression involving the new type. Canonicalize
9284 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9285 : non-integral type.
9286 : Do not fold the result as that would not simplify further, also
9287 : folding again results in recursions. */
9288 732770 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9289 164540 : return build2_loc (loc, TREE_CODE (op0), type,
9290 164540 : TREE_OPERAND (op0, 0),
9291 329080 : TREE_OPERAND (op0, 1));
9292 568230 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9293 7680 : && TREE_CODE (type) != VECTOR_TYPE)
9294 7680 : return build3_loc (loc, COND_EXPR, type, op0,
9295 : constant_boolean_node (true, type),
9296 7680 : constant_boolean_node (false, type));
9297 : }
9298 :
9299 : /* Handle (T *)&A.B.C for A being of type T and B and C
9300 : living at offset zero. This occurs frequently in
9301 : C++ upcasting and then accessing the base. */
9302 695059773 : if (TREE_CODE (op0) == ADDR_EXPR
9303 236207952 : && POINTER_TYPE_P (type)
9304 924392512 : && handled_component_p (TREE_OPERAND (op0, 0)))
9305 : {
9306 49045637 : poly_int64 bitsize, bitpos;
9307 49045637 : tree offset;
9308 49045637 : machine_mode mode;
9309 49045637 : int unsignedp, reversep, volatilep;
9310 49045637 : tree base
9311 49045637 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9312 : &offset, &mode, &unsignedp, &reversep,
9313 : &volatilep);
9314 : /* If the reference was to a (constant) zero offset, we can use
9315 : the address of the base if it has the same base type
9316 : as the result type and the pointer type is unqualified. */
9317 49045637 : if (!offset
9318 48900462 : && known_eq (bitpos, 0)
9319 33382354 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9320 33382354 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9321 49058480 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9322 12642 : return fold_convert_loc (loc, type,
9323 12642 : build_fold_addr_expr_loc (loc, base));
9324 : }
9325 :
9326 695047131 : if (TREE_CODE (op0) == MODIFY_EXPR
9327 292706 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9328 : /* Detect assigning a bitfield. */
9329 695049134 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9330 118 : && DECL_BIT_FIELD
9331 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9332 : {
9333 : /* Don't leave an assignment inside a conversion
9334 : unless assigning a bitfield. */
9335 1955 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9336 : /* First do the assignment, then return converted constant. */
9337 1955 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9338 1955 : suppress_warning (tem /* What warning? */);
9339 1955 : TREE_USED (tem) = 1;
9340 1955 : return tem;
9341 : }
9342 :
9343 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9344 : constants (if x has signed type, the sign bit cannot be set
9345 : in c). This folds extension into the BIT_AND_EXPR.
9346 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9347 : very likely don't have maximal range for their precision and this
9348 : transformation effectively doesn't preserve non-maximal ranges. */
9349 695045176 : if (TREE_CODE (type) == INTEGER_TYPE
9350 245420881 : && TREE_CODE (op0) == BIT_AND_EXPR
9351 695548906 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9352 : {
9353 157451 : tree and_expr = op0;
9354 157451 : tree and0 = TREE_OPERAND (and_expr, 0);
9355 157451 : tree and1 = TREE_OPERAND (and_expr, 1);
9356 157451 : int change = 0;
9357 :
9358 157451 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9359 157451 : || (TYPE_PRECISION (type)
9360 58669 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9361 : change = 1;
9362 17841 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9363 : <= HOST_BITS_PER_WIDE_INT
9364 17841 : && tree_fits_uhwi_p (and1))
9365 : {
9366 16727 : unsigned HOST_WIDE_INT cst;
9367 :
9368 16727 : cst = tree_to_uhwi (and1);
9369 33454 : cst &= HOST_WIDE_INT_M1U
9370 16727 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9371 16727 : change = (cst == 0);
9372 16727 : if (change
9373 16727 : && !flag_syntax_only
9374 33454 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9375 : == ZERO_EXTEND))
9376 : {
9377 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9378 : and0 = fold_convert_loc (loc, uns, and0);
9379 : and1 = fold_convert_loc (loc, uns, and1);
9380 : }
9381 : }
9382 16727 : if (change)
9383 : {
9384 156337 : tree and1_type = TREE_TYPE (and1);
9385 156337 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9386 : TYPE_PRECISION (type));
9387 156337 : tem = force_fit_type (type,
9388 156337 : wide_int::from (wi::to_wide (and1), prec,
9389 156337 : TYPE_SIGN (and1_type)),
9390 156337 : 0, TREE_OVERFLOW (and1));
9391 156337 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9392 156337 : fold_convert_loc (loc, type, and0), tem);
9393 : }
9394 : }
9395 :
9396 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9397 : cast (T1)X will fold away. We assume that this happens when X itself
9398 : is a cast. */
9399 694888839 : if (POINTER_TYPE_P (type)
9400 414685160 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9401 726751332 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9402 : {
9403 29280032 : tree arg00 = TREE_OPERAND (arg0, 0);
9404 29280032 : tree arg01 = TREE_OPERAND (arg0, 1);
9405 :
9406 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9407 : when the pointed type needs higher alignment than
9408 : the p+ first operand's pointed type. */
9409 29280032 : if (!in_gimple_form
9410 29259486 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9411 29281246 : && (min_align_of_type (TREE_TYPE (type))
9412 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9413 : return NULL_TREE;
9414 :
9415 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9416 : when type is a reference type and arg00's type is not,
9417 : because arg00 could be validly nullptr and if arg01 doesn't return,
9418 : we don't want false positive binding of reference to nullptr. */
9419 29279965 : if (TREE_CODE (type) == REFERENCE_TYPE
9420 16466237 : && !in_gimple_form
9421 16466217 : && sanitize_flags_p (SANITIZE_NULL)
9422 29280396 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9423 : return NULL_TREE;
9424 :
9425 29279534 : arg00 = fold_convert_loc (loc, type, arg00);
9426 29279534 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9427 : }
9428 :
9429 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9430 : of the same precision, and X is an integer type not narrower than
9431 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9432 665608807 : if (INTEGRAL_TYPE_P (type)
9433 251585720 : && TREE_CODE (op0) == BIT_NOT_EXPR
9434 538805 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9435 538805 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9436 665951412 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9437 : {
9438 340089 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9439 413218 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9440 413216 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9441 275371 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9442 275371 : fold_convert_loc (loc, type, tem));
9443 : }
9444 :
9445 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9446 : type of X and Y (integer types only). */
9447 665333436 : if (INTEGRAL_TYPE_P (type)
9448 251310349 : && TREE_CODE (op0) == MULT_EXPR
9449 9268647 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9450 9247852 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9451 665402266 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9452 21080 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9453 : {
9454 : /* Be careful not to introduce new overflows. */
9455 68776 : tree mult_type;
9456 68776 : if (TYPE_OVERFLOW_WRAPS (type))
9457 : mult_type = type;
9458 : else
9459 2111 : mult_type = unsigned_type_for (type);
9460 :
9461 68776 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9462 : {
9463 137552 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9464 : fold_convert_loc (loc, mult_type,
9465 68776 : TREE_OPERAND (op0, 0)),
9466 : fold_convert_loc (loc, mult_type,
9467 68776 : TREE_OPERAND (op0, 1)));
9468 68776 : return fold_convert_loc (loc, type, tem);
9469 : }
9470 : }
9471 :
9472 : return NULL_TREE;
9473 :
9474 231325294 : case VIEW_CONVERT_EXPR:
9475 231325294 : if (TREE_CODE (op0) == MEM_REF)
9476 : {
9477 2671 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9478 18 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9479 2671 : tem = fold_build2_loc (loc, MEM_REF, type,
9480 2671 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9481 2671 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9482 2671 : return tem;
9483 : }
9484 :
9485 : return NULL_TREE;
9486 :
9487 4252477 : case NEGATE_EXPR:
9488 4252477 : tem = fold_negate_expr (loc, arg0);
9489 4252477 : if (tem)
9490 1534 : return fold_convert_loc (loc, type, tem);
9491 : return NULL_TREE;
9492 :
9493 2777011 : case ABS_EXPR:
9494 : /* Convert fabs((double)float) into (double)fabsf(float). */
9495 2777011 : if (TREE_CODE (arg0) == NOP_EXPR
9496 23031 : && TREE_CODE (type) == REAL_TYPE)
9497 : {
9498 22977 : tree targ0 = strip_float_extensions (arg0);
9499 22977 : if (targ0 != arg0)
9500 22773 : return fold_convert_loc (loc, type,
9501 : fold_build1_loc (loc, ABS_EXPR,
9502 22773 : TREE_TYPE (targ0),
9503 22773 : targ0));
9504 : }
9505 : return NULL_TREE;
9506 :
9507 2631505 : case BIT_NOT_EXPR:
9508 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9509 2631505 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9510 2633190 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9511 : fold_convert_loc (loc, type,
9512 1685 : TREE_OPERAND (arg0, 0)))))
9513 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9514 : fold_convert_loc (loc, type,
9515 28 : TREE_OPERAND (arg0, 1)));
9516 2631491 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9517 2633162 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9518 : fold_convert_loc (loc, type,
9519 1671 : TREE_OPERAND (arg0, 1)))))
9520 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9521 : fold_convert_loc (loc, type,
9522 46 : TREE_OPERAND (arg0, 0)), tem);
9523 :
9524 : return NULL_TREE;
9525 :
9526 50288470 : case TRUTH_NOT_EXPR:
9527 : /* Note that the operand of this must be an int
9528 : and its values must be 0 or 1.
9529 : ("true" is a fixed value perhaps depending on the language,
9530 : but we don't handle values other than 1 correctly yet.) */
9531 50288470 : tem = fold_truth_not_expr (loc, arg0);
9532 50288470 : if (!tem)
9533 : return NULL_TREE;
9534 34456143 : return fold_convert_loc (loc, type, tem);
9535 :
9536 69550413 : case INDIRECT_REF:
9537 : /* Fold *&X to X if X is an lvalue. */
9538 69550413 : if (TREE_CODE (op0) == ADDR_EXPR)
9539 : {
9540 7631 : tree op00 = TREE_OPERAND (op0, 0);
9541 7631 : if ((VAR_P (op00)
9542 : || TREE_CODE (op00) == PARM_DECL
9543 : || TREE_CODE (op00) == RESULT_DECL)
9544 6459 : && !TREE_READONLY (op00))
9545 : return op00;
9546 : }
9547 : return NULL_TREE;
9548 :
9549 : default:
9550 : return NULL_TREE;
9551 : } /* switch (code) */
9552 : }
9553 :
9554 :
9555 : /* If the operation was a conversion do _not_ mark a resulting constant
9556 : with TREE_OVERFLOW if the original constant was not. These conversions
9557 : have implementation defined behavior and retaining the TREE_OVERFLOW
9558 : flag here would confuse later passes such as VRP. */
9559 : tree
9560 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9561 : tree type, tree op0)
9562 : {
9563 0 : tree res = fold_unary_loc (loc, code, type, op0);
9564 0 : if (res
9565 0 : && TREE_CODE (res) == INTEGER_CST
9566 0 : && TREE_CODE (op0) == INTEGER_CST
9567 0 : && CONVERT_EXPR_CODE_P (code))
9568 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9569 :
9570 0 : return res;
9571 : }
9572 :
9573 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9574 : operands OP0 and OP1. LOC is the location of the resulting expression.
9575 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9576 : Return the folded expression if folding is successful. Otherwise,
9577 : return NULL_TREE. */
9578 : static tree
9579 25536986 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9580 : tree arg0, tree arg1, tree op0, tree op1)
9581 : {
9582 25536986 : tree tem;
9583 :
9584 : /* We only do these simplifications if we are optimizing. */
9585 25536986 : if (!optimize)
9586 : return NULL_TREE;
9587 :
9588 : /* Check for things like (A || B) && (A || C). We can convert this
9589 : to A || (B && C). Note that either operator can be any of the four
9590 : truth and/or operations and the transformation will still be
9591 : valid. Also note that we only care about order for the
9592 : ANDIF and ORIF operators. If B contains side effects, this
9593 : might change the truth-value of A. */
9594 25064314 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9595 5921879 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9596 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9597 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9598 5921879 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9599 25126827 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9600 : {
9601 62019 : tree a00 = TREE_OPERAND (arg0, 0);
9602 62019 : tree a01 = TREE_OPERAND (arg0, 1);
9603 62019 : tree a10 = TREE_OPERAND (arg1, 0);
9604 62019 : tree a11 = TREE_OPERAND (arg1, 1);
9605 124038 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9606 62019 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9607 62019 : && (code == TRUTH_AND_EXPR
9608 23123 : || code == TRUTH_OR_EXPR));
9609 :
9610 62019 : if (operand_equal_p (a00, a10, 0))
9611 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9612 849 : fold_build2_loc (loc, code, type, a01, a11));
9613 61170 : else if (commutative && operand_equal_p (a00, a11, 0))
9614 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9615 0 : fold_build2_loc (loc, code, type, a01, a10));
9616 61170 : else if (commutative && operand_equal_p (a01, a10, 0))
9617 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9618 0 : fold_build2_loc (loc, code, type, a00, a11));
9619 :
9620 : /* This case if tricky because we must either have commutative
9621 : operators or else A10 must not have side-effects. */
9622 :
9623 61126 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9624 121780 : && operand_equal_p (a01, a11, 0))
9625 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9626 : fold_build2_loc (loc, code, type, a00, a10),
9627 43 : a01);
9628 : }
9629 :
9630 : /* See if we can build a range comparison. */
9631 25063422 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9632 : return tem;
9633 :
9634 23891009 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9635 23889003 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9636 : {
9637 39932 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9638 39932 : if (tem)
9639 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9640 : }
9641 :
9642 23890996 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9643 23879979 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9644 : {
9645 89300 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9646 89300 : if (tem)
9647 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9648 : }
9649 :
9650 : /* Check for the possibility of merging component references. If our
9651 : lhs is another similar operation, try to merge its rhs with our
9652 : rhs. Then try to merge our lhs and rhs. */
9653 23890905 : if (TREE_CODE (arg0) == code
9654 24737077 : && (tem = fold_truth_andor_1 (loc, code, type,
9655 846172 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9656 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9657 :
9658 23890820 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9659 : return tem;
9660 :
9661 23851302 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9662 23851302 : if (param_logical_op_non_short_circuit != -1)
9663 7784 : logical_op_non_short_circuit
9664 7784 : = param_logical_op_non_short_circuit;
9665 23851302 : if (logical_op_non_short_circuit
9666 23847362 : && !sanitize_coverage_p ()
9667 23851302 : && (code == TRUTH_AND_EXPR
9668 23847359 : || code == TRUTH_ANDIF_EXPR
9669 10973647 : || code == TRUTH_OR_EXPR
9670 10973647 : || code == TRUTH_ORIF_EXPR))
9671 : {
9672 23847359 : enum tree_code ncode, icode;
9673 :
9674 23847359 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9675 23847359 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9676 12873712 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9677 :
9678 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9679 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9680 : We don't want to pack more than two leafs to a non-IF AND/OR
9681 : expression.
9682 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9683 : equal to IF-CODE, then we don't want to add right-hand operand.
9684 : If the inner right-hand side of left-hand operand has
9685 : side-effects, or isn't simple, then we can't add to it,
9686 : as otherwise we might destroy if-sequence. */
9687 23847359 : if (TREE_CODE (arg0) == icode
9688 837409 : && simple_condition_p (arg1)
9689 : /* Needed for sequence points to handle trappings, and
9690 : side-effects. */
9691 23896238 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9692 : {
9693 42031 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9694 : arg1);
9695 42031 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9696 42031 : tem);
9697 : }
9698 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9699 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9700 23805328 : else if (TREE_CODE (arg1) == icode
9701 6342 : && simple_condition_p (arg0)
9702 : /* Needed for sequence points to handle trappings, and
9703 : side-effects. */
9704 23806281 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9705 : {
9706 36 : tem = fold_build2_loc (loc, ncode, type,
9707 36 : arg0, TREE_OPERAND (arg1, 0));
9708 36 : return fold_build2_loc (loc, icode, type, tem,
9709 72 : TREE_OPERAND (arg1, 1));
9710 : }
9711 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9712 : into (A OR B).
9713 : For sequence point consistency, we need to check for trapping,
9714 : and side-effects. */
9715 5266346 : else if (code == icode && simple_condition_p (arg0)
9716 24692976 : && simple_condition_p (arg1))
9717 443803 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9718 : }
9719 :
9720 : return NULL_TREE;
9721 : }
9722 :
9723 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9724 : by changing CODE to reduce the magnitude of constants involved in
9725 : ARG0 of the comparison.
9726 : Returns a canonicalized comparison tree if a simplification was
9727 : possible, otherwise returns NULL_TREE. */
9728 :
9729 : static tree
9730 179930494 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9731 : tree arg0, tree arg1)
9732 : {
9733 179930494 : enum tree_code code0 = TREE_CODE (arg0);
9734 179930494 : tree t, cst0 = NULL_TREE;
9735 179930494 : int sgn0;
9736 :
9737 : /* Match A +- CST code arg1. We can change this only if overflow
9738 : is undefined. */
9739 179930494 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9740 136940211 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9741 : /* In principle pointers also have undefined overflow behavior,
9742 : but that causes problems elsewhere. */
9743 67609033 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9744 67609033 : && (code0 == MINUS_EXPR
9745 67609033 : || code0 == PLUS_EXPR)
9746 2617716 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9747 : return NULL_TREE;
9748 :
9749 : /* Identify the constant in arg0 and its sign. */
9750 2139059 : cst0 = TREE_OPERAND (arg0, 1);
9751 2139059 : sgn0 = tree_int_cst_sgn (cst0);
9752 :
9753 : /* Overflowed constants and zero will cause problems. */
9754 2139059 : if (integer_zerop (cst0)
9755 2139059 : || TREE_OVERFLOW (cst0))
9756 : return NULL_TREE;
9757 :
9758 : /* See if we can reduce the magnitude of the constant in
9759 : arg0 by changing the comparison code. */
9760 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9761 2139059 : if (code == LT_EXPR
9762 1264852 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9763 : code = LE_EXPR;
9764 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9765 1956508 : else if (code == GT_EXPR
9766 580106 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9767 : code = GE_EXPR;
9768 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9769 1774826 : else if (code == LE_EXPR
9770 674947 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9771 : code = LT_EXPR;
9772 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9773 1535822 : else if (code == GE_EXPR
9774 488661 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9775 : code = GT_EXPR;
9776 : else
9777 : return NULL_TREE;
9778 :
9779 : /* Now build the constant reduced in magnitude. But not if that
9780 : would produce one outside of its types range. */
9781 1612916 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9782 1612916 : && ((sgn0 == 1
9783 420548 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9784 420548 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9785 806458 : || (sgn0 == -1
9786 385910 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9787 385910 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9788 0 : return NULL_TREE;
9789 :
9790 1227006 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9791 806458 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9792 806458 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9793 806458 : t = fold_convert (TREE_TYPE (arg1), t);
9794 :
9795 806458 : return fold_build2_loc (loc, code, type, t, arg1);
9796 : }
9797 :
9798 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9799 : overflow further. Try to decrease the magnitude of constants involved
9800 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9801 : and put sole constants at the second argument position.
9802 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9803 :
9804 : static tree
9805 90347174 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9806 : tree arg0, tree arg1)
9807 : {
9808 90347174 : tree t;
9809 :
9810 : /* Try canonicalization by simplifying arg0. */
9811 90347174 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9812 90347174 : if (t)
9813 : return t;
9814 :
9815 : /* Try canonicalization by simplifying arg1 using the swapped
9816 : comparison. */
9817 89583320 : code = swap_tree_comparison (code);
9818 89583320 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9819 89583320 : return t;
9820 : }
9821 :
9822 : /* Return a positive integer when the symbol DECL is known to have
9823 : a nonzero address, zero when it's known not to (e.g., it's a weak
9824 : symbol), and a negative integer when the symbol is not yet in the
9825 : symbol table and so whether or not its address is zero is unknown.
9826 : For function local objects always return positive integer. */
9827 : static int
9828 12061643 : maybe_nonzero_address (tree decl)
9829 : {
9830 12061643 : if (!DECL_P (decl))
9831 : return -1;
9832 :
9833 : /* Normally, don't do anything for variables and functions before symtab is
9834 : built; it is quite possible that DECL will be declared weak later.
9835 : But if folding_initializer, we need a constant answer now, so create
9836 : the symtab entry and prevent later weak declaration. */
9837 9884190 : if (decl_in_symtab_p (decl))
9838 : {
9839 4366611 : if (struct symtab_node *symbol
9840 4366611 : = (folding_initializer
9841 4366611 : ? symtab_node::get_create (decl)
9842 4349449 : : symtab_node::get (decl)))
9843 4347622 : return symbol->nonzero_address ();
9844 : }
9845 5517579 : else if (folding_cxx_constexpr)
9846 : /* Anything that doesn't go in the symtab has non-zero address. */
9847 : return 1;
9848 :
9849 : /* Function local objects are never NULL. */
9850 5398225 : if (DECL_CONTEXT (decl)
9851 5381465 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9852 10776253 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9853 : return 1;
9854 :
9855 : return -1;
9856 : }
9857 :
9858 : /* Subroutine of fold_binary. This routine performs all of the
9859 : transformations that are common to the equality/inequality
9860 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9861 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9862 : fold_binary should call fold_binary. Fold a comparison with
9863 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9864 : the folded comparison or NULL_TREE. */
9865 :
9866 : static tree
9867 90424056 : fold_comparison (location_t loc, enum tree_code code, tree type,
9868 : tree op0, tree op1)
9869 : {
9870 90424056 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9871 90424056 : tree arg0, arg1, tem;
9872 :
9873 90424056 : arg0 = op0;
9874 90424056 : arg1 = op1;
9875 :
9876 90424056 : STRIP_SIGN_NOPS (arg0);
9877 90424056 : STRIP_SIGN_NOPS (arg1);
9878 :
9879 : /* For comparisons of pointers we can decompose it to a compile time
9880 : comparison of the base objects and the offsets into the object.
9881 : This requires at least one operand being an ADDR_EXPR or a
9882 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9883 167214470 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9884 90644162 : && (TREE_CODE (arg0) == ADDR_EXPR
9885 13799822 : || TREE_CODE (arg1) == ADDR_EXPR
9886 12229320 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9887 11452919 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9888 : {
9889 2410775 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9890 2410775 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9891 2410775 : machine_mode mode;
9892 2410775 : int volatilep, reversep, unsignedp;
9893 2410775 : bool indirect_base0 = false, indirect_base1 = false;
9894 :
9895 : /* Get base and offset for the access. Strip ADDR_EXPR for
9896 : get_inner_reference, but put it back by stripping INDIRECT_REF
9897 : off the base object if possible. indirect_baseN will be true
9898 : if baseN is not an address but refers to the object itself. */
9899 2410775 : base0 = arg0;
9900 2410775 : if (TREE_CODE (arg0) == ADDR_EXPR)
9901 : {
9902 53926 : base0
9903 53926 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9904 : &bitsize, &bitpos0, &offset0, &mode,
9905 : &unsignedp, &reversep, &volatilep);
9906 53926 : if (INDIRECT_REF_P (base0))
9907 2220 : base0 = TREE_OPERAND (base0, 0);
9908 : else
9909 : indirect_base0 = true;
9910 : }
9911 2356849 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9912 : {
9913 842877 : base0 = TREE_OPERAND (arg0, 0);
9914 842877 : STRIP_SIGN_NOPS (base0);
9915 842877 : if (TREE_CODE (base0) == ADDR_EXPR)
9916 : {
9917 33175 : base0
9918 33175 : = get_inner_reference (TREE_OPERAND (base0, 0),
9919 : &bitsize, &bitpos0, &offset0, &mode,
9920 : &unsignedp, &reversep, &volatilep);
9921 33175 : if (INDIRECT_REF_P (base0))
9922 20 : base0 = TREE_OPERAND (base0, 0);
9923 : else
9924 : indirect_base0 = true;
9925 : }
9926 842877 : if (offset0 == NULL_TREE || integer_zerop (offset0))
9927 842877 : offset0 = TREE_OPERAND (arg0, 1);
9928 : else
9929 0 : offset0 = size_binop (PLUS_EXPR, offset0,
9930 : TREE_OPERAND (arg0, 1));
9931 842877 : if (poly_int_tree_p (offset0))
9932 : {
9933 676646 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
9934 676646 : TYPE_PRECISION (sizetype));
9935 676646 : tem <<= LOG2_BITS_PER_UNIT;
9936 676646 : tem += bitpos0;
9937 676646 : if (tem.to_shwi (&bitpos0))
9938 676646 : offset0 = NULL_TREE;
9939 : }
9940 : }
9941 :
9942 2410775 : base1 = arg1;
9943 2410775 : if (TREE_CODE (arg1) == ADDR_EXPR)
9944 : {
9945 1600459 : base1
9946 1600459 : = get_inner_reference (TREE_OPERAND (arg1, 0),
9947 : &bitsize, &bitpos1, &offset1, &mode,
9948 : &unsignedp, &reversep, &volatilep);
9949 1600459 : if (INDIRECT_REF_P (base1))
9950 70354 : base1 = TREE_OPERAND (base1, 0);
9951 : else
9952 : indirect_base1 = true;
9953 : }
9954 810316 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9955 : {
9956 93396 : base1 = TREE_OPERAND (arg1, 0);
9957 93396 : STRIP_SIGN_NOPS (base1);
9958 93396 : if (TREE_CODE (base1) == ADDR_EXPR)
9959 : {
9960 11098 : base1
9961 11098 : = get_inner_reference (TREE_OPERAND (base1, 0),
9962 : &bitsize, &bitpos1, &offset1, &mode,
9963 : &unsignedp, &reversep, &volatilep);
9964 11098 : if (INDIRECT_REF_P (base1))
9965 0 : base1 = TREE_OPERAND (base1, 0);
9966 : else
9967 : indirect_base1 = true;
9968 : }
9969 93396 : if (offset1 == NULL_TREE || integer_zerop (offset1))
9970 93372 : offset1 = TREE_OPERAND (arg1, 1);
9971 : else
9972 24 : offset1 = size_binop (PLUS_EXPR, offset1,
9973 : TREE_OPERAND (arg1, 1));
9974 93396 : if (poly_int_tree_p (offset1))
9975 : {
9976 82743 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
9977 82743 : TYPE_PRECISION (sizetype));
9978 82743 : tem <<= LOG2_BITS_PER_UNIT;
9979 82743 : tem += bitpos1;
9980 82743 : if (tem.to_shwi (&bitpos1))
9981 82743 : offset1 = NULL_TREE;
9982 : }
9983 : }
9984 :
9985 : /* If we have equivalent bases we might be able to simplify. */
9986 2410775 : if (indirect_base0 == indirect_base1
9987 3236945 : && operand_equal_p (base0, base1,
9988 : indirect_base0 ? OEP_ADDRESS_OF : 0))
9989 : {
9990 : /* We can fold this expression to a constant if the non-constant
9991 : offset parts are equal. */
9992 22594 : if ((offset0 == offset1
9993 6640 : || (offset0 && offset1
9994 2676 : && operand_equal_p (offset0, offset1, 0)))
9995 22594 : && (equality_code
9996 9425 : || (indirect_base0
9997 6233 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
9998 3192 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9999 : {
10000 15914 : switch (code)
10001 : {
10002 56 : case EQ_EXPR:
10003 56 : if (known_eq (bitpos0, bitpos1))
10004 54715 : return constant_boolean_node (true, type);
10005 21 : if (known_ne (bitpos0, bitpos1))
10006 21 : return constant_boolean_node (false, type);
10007 : break;
10008 6473 : case NE_EXPR:
10009 6473 : if (known_ne (bitpos0, bitpos1))
10010 6468 : return constant_boolean_node (true, type);
10011 5 : if (known_eq (bitpos0, bitpos1))
10012 5 : return constant_boolean_node (false, type);
10013 : break;
10014 2388 : case LT_EXPR:
10015 2388 : if (known_lt (bitpos0, bitpos1))
10016 2252 : return constant_boolean_node (true, type);
10017 136 : if (known_ge (bitpos0, bitpos1))
10018 136 : return constant_boolean_node (false, type);
10019 : break;
10020 1648 : case LE_EXPR:
10021 1648 : if (known_le (bitpos0, bitpos1))
10022 182 : return constant_boolean_node (true, type);
10023 1466 : if (known_gt (bitpos0, bitpos1))
10024 1466 : return constant_boolean_node (false, type);
10025 : break;
10026 3542 : case GE_EXPR:
10027 3542 : if (known_ge (bitpos0, bitpos1))
10028 1368 : return constant_boolean_node (true, type);
10029 2174 : if (known_lt (bitpos0, bitpos1))
10030 2174 : return constant_boolean_node (false, type);
10031 : break;
10032 1807 : case GT_EXPR:
10033 1807 : if (known_gt (bitpos0, bitpos1))
10034 1748 : return constant_boolean_node (true, type);
10035 59 : if (known_le (bitpos0, bitpos1))
10036 59 : return constant_boolean_node (false, type);
10037 : break;
10038 : default:;
10039 : }
10040 : }
10041 : /* We can simplify the comparison to a comparison of the variable
10042 : offset parts if the constant offset parts are equal.
10043 : Be careful to use signed sizetype here because otherwise we
10044 : mess with array offsets in the wrong way. This is possible
10045 : because pointer arithmetic is restricted to retain within an
10046 : object and overflow on pointer differences is undefined as of
10047 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10048 6680 : else if (known_eq (bitpos0, bitpos1)
10049 6680 : && (equality_code
10050 5216 : || (indirect_base0
10051 271 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10052 4945 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10053 : {
10054 : /* By converting to signed sizetype we cover middle-end pointer
10055 : arithmetic which operates on unsigned pointer types of size
10056 : type size and ARRAY_REF offsets which are properly sign or
10057 : zero extended from their type in case it is narrower than
10058 : sizetype. */
10059 5325 : if (offset0 == NULL_TREE)
10060 0 : offset0 = build_int_cst (ssizetype, 0);
10061 : else
10062 5325 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10063 5325 : if (offset1 == NULL_TREE)
10064 2681 : offset1 = build_int_cst (ssizetype, 0);
10065 : else
10066 2644 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10067 :
10068 5325 : return fold_build2_loc (loc, code, type, offset0, offset1);
10069 : }
10070 : }
10071 : /* For equal offsets we can simplify to a comparison of the
10072 : base addresses. */
10073 2388181 : else if (known_eq (bitpos0, bitpos1)
10074 56458 : && (indirect_base0
10075 1003946 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10076 15938 : && (indirect_base1
10077 191143 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10078 2617147 : && ((offset0 == offset1)
10079 4411 : || (offset0 && offset1
10080 4141 : && operand_equal_p (offset0, offset1, 0))))
10081 : {
10082 33413 : if (indirect_base0)
10083 3764 : base0 = build_fold_addr_expr_loc (loc, base0);
10084 33413 : if (indirect_base1)
10085 5407 : base1 = build_fold_addr_expr_loc (loc, base1);
10086 33413 : return fold_build2_loc (loc, code, type, base0, base1);
10087 : }
10088 : /* Comparison between an ordinary (non-weak) symbol and a null
10089 : pointer can be eliminated since such symbols must have a non
10090 : null address. In C, relational expressions between pointers
10091 : to objects and null pointers are undefined. The results
10092 : below follow the C++ rules with the additional property that
10093 : every object pointer compares greater than a null pointer.
10094 : */
10095 2354768 : else if (((DECL_P (base0)
10096 256742 : && maybe_nonzero_address (base0) > 0
10097 : /* Avoid folding references to struct members at offset 0 to
10098 : prevent tests like '&ptr->firstmember == 0' from getting
10099 : eliminated. When ptr is null, although the -> expression
10100 : is strictly speaking invalid, GCC retains it as a matter
10101 : of QoI. See PR c/44555. */
10102 242265 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10103 2338610 : || CONSTANT_CLASS_P (base0))
10104 20976 : && indirect_base0
10105 : /* The caller guarantees that when one of the arguments is
10106 : constant (i.e., null in this case) it is second. */
10107 2372737 : && integer_zerop (arg1))
10108 : {
10109 63 : switch (code)
10110 : {
10111 24 : case EQ_EXPR:
10112 24 : case LE_EXPR:
10113 24 : case LT_EXPR:
10114 24 : return constant_boolean_node (false, type);
10115 39 : case GE_EXPR:
10116 39 : case GT_EXPR:
10117 39 : case NE_EXPR:
10118 39 : return constant_boolean_node (true, type);
10119 0 : default:
10120 0 : gcc_unreachable ();
10121 : }
10122 : }
10123 : }
10124 :
10125 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10126 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10127 : the resulting offset is smaller in absolute value than the
10128 : original one and has the same sign. */
10129 178056688 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10130 137748408 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10131 34208614 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10132 2359131 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10133 1934498 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10134 1934498 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10135 159414898 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10136 169017 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10137 : {
10138 169017 : tree const1 = TREE_OPERAND (arg0, 1);
10139 169017 : tree const2 = TREE_OPERAND (arg1, 1);
10140 169017 : tree variable1 = TREE_OPERAND (arg0, 0);
10141 169017 : tree variable2 = TREE_OPERAND (arg1, 0);
10142 169017 : tree cst;
10143 :
10144 : /* Put the constant on the side where it doesn't overflow and is
10145 : of lower absolute value and of same sign than before. */
10146 169018 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10147 : ? MINUS_EXPR : PLUS_EXPR,
10148 : const2, const1);
10149 169017 : if (!TREE_OVERFLOW (cst)
10150 169001 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10151 191184 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10152 5660 : return fold_build2_loc (loc, code, type,
10153 : variable1,
10154 5660 : fold_build2_loc (loc, TREE_CODE (arg1),
10155 5660 : TREE_TYPE (arg1),
10156 5660 : variable2, cst));
10157 :
10158 163358 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10159 : ? MINUS_EXPR : PLUS_EXPR,
10160 : const1, const2);
10161 163357 : if (!TREE_OVERFLOW (cst)
10162 163341 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10163 179864 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10164 16507 : return fold_build2_loc (loc, code, type,
10165 16507 : fold_build2_loc (loc, TREE_CODE (arg0),
10166 16507 : TREE_TYPE (arg0),
10167 : variable1, cst),
10168 16507 : variable2);
10169 : }
10170 :
10171 90347174 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10172 90347174 : if (tem)
10173 : return tem;
10174 :
10175 : /* If we are comparing an expression that just has comparisons
10176 : of two integer values, arithmetic expressions of those comparisons,
10177 : and constants, we can simplify it. There are only three cases
10178 : to check: the two values can either be equal, the first can be
10179 : greater, or the second can be greater. Fold the expression for
10180 : those three values. Since each value must be 0 or 1, we have
10181 : eight possibilities, each of which corresponds to the constant 0
10182 : or 1 or one of the six possible comparisons.
10183 :
10184 : This handles common cases like (a > b) == 0 but also handles
10185 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10186 : occur in macroized code. */
10187 :
10188 89540716 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10189 : {
10190 54596224 : tree cval1 = 0, cval2 = 0;
10191 :
10192 54596224 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10193 : /* Don't handle degenerate cases here; they should already
10194 : have been handled anyway. */
10195 531238 : && cval1 != 0 && cval2 != 0
10196 530305 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10197 530305 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10198 530299 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10199 50 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10200 50 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10201 54596274 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10202 50 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10203 : {
10204 50 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10205 50 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10206 :
10207 : /* We can't just pass T to eval_subst in case cval1 or cval2
10208 : was the same as ARG1. */
10209 :
10210 50 : tree high_result
10211 50 : = fold_build2_loc (loc, code, type,
10212 : eval_subst (loc, arg0, cval1, maxval,
10213 : cval2, minval),
10214 : arg1);
10215 50 : tree equal_result
10216 50 : = fold_build2_loc (loc, code, type,
10217 : eval_subst (loc, arg0, cval1, maxval,
10218 : cval2, maxval),
10219 : arg1);
10220 50 : tree low_result
10221 50 : = fold_build2_loc (loc, code, type,
10222 : eval_subst (loc, arg0, cval1, minval,
10223 : cval2, maxval),
10224 : arg1);
10225 :
10226 : /* All three of these results should be 0 or 1. Confirm they are.
10227 : Then use those values to select the proper code to use. */
10228 :
10229 50 : if (TREE_CODE (high_result) == INTEGER_CST
10230 50 : && TREE_CODE (equal_result) == INTEGER_CST
10231 40 : && TREE_CODE (low_result) == INTEGER_CST)
10232 : {
10233 : /* Make a 3-bit mask with the high-order bit being the
10234 : value for `>', the next for '=', and the low for '<'. */
10235 40 : switch ((integer_onep (high_result) * 4)
10236 40 : + (integer_onep (equal_result) * 2)
10237 40 : + integer_onep (low_result))
10238 : {
10239 21 : case 0:
10240 : /* Always false. */
10241 40 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10242 : case 1:
10243 : code = LT_EXPR;
10244 : break;
10245 2 : case 2:
10246 2 : code = EQ_EXPR;
10247 2 : break;
10248 0 : case 3:
10249 0 : code = LE_EXPR;
10250 0 : break;
10251 0 : case 4:
10252 0 : code = GT_EXPR;
10253 0 : break;
10254 1 : case 5:
10255 1 : code = NE_EXPR;
10256 1 : break;
10257 0 : case 6:
10258 0 : code = GE_EXPR;
10259 0 : break;
10260 16 : case 7:
10261 : /* Always true. */
10262 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10263 : }
10264 :
10265 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10266 : }
10267 : }
10268 : }
10269 :
10270 : return NULL_TREE;
10271 : }
10272 :
10273 :
10274 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10275 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10276 : argument EXPR represents the expression "z" of type TYPE. */
10277 :
10278 : static tree
10279 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10280 : {
10281 2 : tree itype = TREE_TYPE (type);
10282 2 : tree rpart, ipart, tem;
10283 :
10284 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10285 : {
10286 0 : rpart = TREE_OPERAND (expr, 0);
10287 0 : ipart = TREE_OPERAND (expr, 1);
10288 : }
10289 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10290 : {
10291 0 : rpart = TREE_REALPART (expr);
10292 0 : ipart = TREE_IMAGPART (expr);
10293 : }
10294 : else
10295 : {
10296 2 : expr = save_expr (expr);
10297 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10298 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10299 : }
10300 :
10301 2 : rpart = save_expr (rpart);
10302 2 : ipart = save_expr (ipart);
10303 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10304 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10305 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10306 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10307 2 : build_zero_cst (itype));
10308 : }
10309 :
10310 :
10311 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10312 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10313 : true if successful. */
10314 :
10315 : static bool
10316 31447 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10317 : {
10318 31447 : unsigned HOST_WIDE_INT i, nunits;
10319 :
10320 31447 : if (TREE_CODE (arg) == VECTOR_CST
10321 31447 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10322 : {
10323 2168 : for (i = 0; i < nunits; ++i)
10324 1710 : elts[i] = VECTOR_CST_ELT (arg, i);
10325 : }
10326 30989 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10327 : {
10328 : constructor_elt *elt;
10329 :
10330 51185 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10331 46032 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10332 25836 : return false;
10333 : else
10334 20196 : elts[i] = elt->value;
10335 : }
10336 : else
10337 : return false;
10338 6385 : for (; i < nelts; i++)
10339 1548 : elts[i]
10340 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10341 : return true;
10342 : }
10343 :
10344 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10345 : mask for VLA vec_perm folding.
10346 : REASON if specified, will contain the reason why SEL is not suitable.
10347 : Used only for debugging and unit-testing. */
10348 :
10349 : static bool
10350 29953 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10351 : const vec_perm_indices &sel,
10352 : const char **reason = NULL)
10353 : {
10354 29953 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10355 29953 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10356 :
10357 59906 : if (!(pow2p_hwi (sel_npatterns)
10358 29953 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10359 29953 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10360 : {
10361 0 : if (reason)
10362 0 : *reason = "npatterns is not power of 2";
10363 0 : return false;
10364 : }
10365 :
10366 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10367 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10368 29953 : poly_uint64 esel;
10369 29953 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10370 : {
10371 0 : if (reason)
10372 0 : *reason = "sel.length is not multiple of sel_npatterns";
10373 0 : return false;
10374 : }
10375 :
10376 29953 : if (sel_nelts_per_pattern < 3)
10377 : return true;
10378 :
10379 5964 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10380 : {
10381 4513 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10382 4513 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10383 4513 : HOST_WIDE_INT step;
10384 4513 : if (!poly_int64 (a2 - a1).is_constant (&step))
10385 : {
10386 : if (reason)
10387 : *reason = "step is not constant";
10388 1124 : return false;
10389 : }
10390 : // FIXME: Punt on step < 0 for now, revisit later.
10391 4513 : if (step < 0)
10392 : return false;
10393 4457 : if (step == 0)
10394 0 : continue;
10395 :
10396 4457 : if (!pow2p_hwi (step))
10397 : {
10398 0 : if (reason)
10399 0 : *reason = "step is not power of 2";
10400 0 : return false;
10401 : }
10402 :
10403 : /* Ensure that stepped sequence of the pattern selects elements
10404 : only from the same input vector. */
10405 4457 : uint64_t q1, qe;
10406 4457 : poly_uint64 r1, re;
10407 4457 : poly_uint64 ae = a1 + (esel - 2) * step;
10408 4457 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10409 :
10410 4457 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10411 4457 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10412 : && q1 == qe))
10413 : {
10414 460 : if (reason)
10415 0 : *reason = "crossed input vectors";
10416 460 : return false;
10417 : }
10418 :
10419 : /* Ensure that the stepped sequence always selects from the same
10420 : input pattern. */
10421 3997 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10422 3997 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10423 :
10424 3997 : if (!multiple_p (step, arg_npatterns))
10425 : {
10426 606 : if (reason)
10427 0 : *reason = "step is not multiple of npatterns";
10428 606 : return false;
10429 : }
10430 :
10431 : /* If a1 chooses base element from arg, ensure that it's a natural
10432 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10433 : to preserve arg's encoding. */
10434 :
10435 3391 : if (maybe_lt (r1, arg_npatterns))
10436 : {
10437 24 : unsigned HOST_WIDE_INT index;
10438 24 : if (!r1.is_constant (&index))
10439 2 : return false;
10440 :
10441 24 : tree arg_elem0 = vector_cst_elt (arg, index);
10442 24 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10443 24 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10444 :
10445 24 : tree step1, step2;
10446 24 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10447 24 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10448 48 : || !operand_equal_p (step1, step2, 0))
10449 : {
10450 2 : if (reason)
10451 0 : *reason = "not a natural stepped sequence";
10452 2 : return false;
10453 : }
10454 : }
10455 : }
10456 :
10457 : return true;
10458 : }
10459 :
10460 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10461 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10462 : REASON has same purpose as described in
10463 : valid_mask_for_fold_vec_perm_cst_p. */
10464 :
10465 : static tree
10466 29953 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10467 : const char **reason = NULL)
10468 : {
10469 29953 : unsigned res_npatterns, res_nelts_per_pattern;
10470 29953 : unsigned HOST_WIDE_INT res_nelts;
10471 :
10472 : /* First try to implement the fold in a VLA-friendly way.
10473 :
10474 : (1) If the selector is simply a duplication of N elements, the
10475 : result is likewise a duplication of N elements.
10476 :
10477 : (2) If the selector is N elements followed by a duplication
10478 : of N elements, the result is too.
10479 :
10480 : (3) If the selector is N elements followed by an interleaving
10481 : of N linear series, the situation is more complex.
10482 :
10483 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10484 : can handle this case. If we can, then each of the N linear
10485 : series either (a) selects the same element each time or
10486 : (b) selects a linear series from one of the input patterns.
10487 :
10488 : If (b) holds for one of the linear series, the result
10489 : will contain a linear series, and so the result will have
10490 : the same shape as the selector. If (a) holds for all of
10491 : the linear series, the result will be the same as (2) above.
10492 :
10493 : (b) can only hold if one of the input patterns has a
10494 : stepped encoding. */
10495 :
10496 29953 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10497 : {
10498 28829 : res_npatterns = sel.encoding ().npatterns ();
10499 28829 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10500 28829 : if (res_nelts_per_pattern == 3
10501 1451 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10502 29775 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10503 : res_nelts_per_pattern = 2;
10504 28829 : res_nelts = res_npatterns * res_nelts_per_pattern;
10505 : }
10506 1124 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10507 : {
10508 1124 : res_npatterns = res_nelts;
10509 1124 : res_nelts_per_pattern = 1;
10510 : }
10511 : else
10512 : return NULL_TREE;
10513 :
10514 29953 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10515 151031 : for (unsigned i = 0; i < res_nelts; i++)
10516 : {
10517 121078 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10518 121078 : uint64_t q;
10519 121078 : poly_uint64 r;
10520 121078 : unsigned HOST_WIDE_INT index;
10521 :
10522 : /* Punt if sel[i] /trunc_div len cannot be determined,
10523 : because the input vector to be chosen will depend on
10524 : runtime vector length.
10525 : For example if len == 4 + 4x, and sel[i] == 4,
10526 : If len at runtime equals 4, we choose arg1[0].
10527 : For any other value of len > 4 at runtime, we choose arg0[4].
10528 : which makes the element choice dependent on runtime vector length. */
10529 121078 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10530 : {
10531 : if (reason)
10532 : *reason = "cannot divide selector element by arg len";
10533 : return NULL_TREE;
10534 : }
10535 :
10536 : /* sel[i] % len will give the index of element in the chosen input
10537 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10538 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10539 121078 : if (!r.is_constant (&index))
10540 : {
10541 : if (reason)
10542 : *reason = "remainder is not constant";
10543 : return NULL_TREE;
10544 : }
10545 :
10546 121078 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10547 121078 : tree elem = vector_cst_elt (arg, index);
10548 121078 : out_elts.quick_push (elem);
10549 : }
10550 :
10551 29953 : return out_elts.build ();
10552 29953 : }
10553 :
10554 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10555 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10556 : NULL_TREE otherwise. */
10557 :
10558 : tree
10559 69191 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10560 : {
10561 69191 : unsigned int i;
10562 69191 : unsigned HOST_WIDE_INT nelts;
10563 :
10564 69191 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10565 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10566 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10567 :
10568 69191 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10569 69191 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10570 : return NULL_TREE;
10571 :
10572 58578 : if (TREE_CODE (arg0) == VECTOR_CST
10573 30227 : && TREE_CODE (arg1) == VECTOR_CST)
10574 29953 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10575 :
10576 : /* For fall back case, we want to ensure we have VLS vectors
10577 : with equal length. */
10578 28625 : if (!sel.length ().is_constant (&nelts)
10579 28625 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10580 0 : return NULL_TREE;
10581 :
10582 28625 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10583 28625 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10584 28625 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10585 25836 : return NULL_TREE;
10586 :
10587 2789 : vec<constructor_elt, va_gc> *v;
10588 2789 : vec_alloc (v, nelts);
10589 13997 : for (i = 0; i < nelts; i++)
10590 : {
10591 11208 : HOST_WIDE_INT index;
10592 11208 : if (!sel[i].is_constant (&index))
10593 : return NULL_TREE;
10594 11208 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10595 : }
10596 2789 : return build_constructor (type, v);
10597 : }
10598 :
10599 : /* Try to fold a pointer difference of type TYPE two address expressions of
10600 : array references AREF0 and AREF1 using location LOC. Return a
10601 : simplified expression for the difference or NULL_TREE. */
10602 :
10603 : static tree
10604 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10605 : tree aref0, tree aref1,
10606 : bool use_pointer_diff)
10607 : {
10608 39 : tree base0 = TREE_OPERAND (aref0, 0);
10609 39 : tree base1 = TREE_OPERAND (aref1, 0);
10610 39 : tree base_offset = build_int_cst (type, 0);
10611 :
10612 : /* If the bases are array references as well, recurse. If the bases
10613 : are pointer indirections compute the difference of the pointers.
10614 : If the bases are equal, we are set. */
10615 39 : if ((TREE_CODE (base0) == ARRAY_REF
10616 1 : && TREE_CODE (base1) == ARRAY_REF
10617 1 : && (base_offset
10618 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10619 : use_pointer_diff)))
10620 38 : || (INDIRECT_REF_P (base0)
10621 7 : && INDIRECT_REF_P (base1)
10622 7 : && (base_offset
10623 : = use_pointer_diff
10624 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10625 1 : TREE_OPERAND (base0, 0),
10626 1 : TREE_OPERAND (base1, 0))
10627 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10628 6 : fold_convert (type,
10629 : TREE_OPERAND (base0, 0)),
10630 6 : fold_convert (type,
10631 : TREE_OPERAND (base1, 0)))))
10632 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10633 : {
10634 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10635 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10636 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10637 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10638 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10639 : base_offset,
10640 : fold_build2_loc (loc, MULT_EXPR, type,
10641 15 : diff, esz));
10642 : }
10643 : return NULL_TREE;
10644 : }
10645 :
10646 : /* If the real or vector real constant CST of type TYPE has an exact
10647 : inverse, return it, else return NULL. */
10648 :
10649 : tree
10650 1158486 : exact_inverse (tree type, tree cst)
10651 : {
10652 1158486 : REAL_VALUE_TYPE r;
10653 1158486 : tree unit_type;
10654 1158486 : machine_mode mode;
10655 :
10656 1158486 : switch (TREE_CODE (cst))
10657 : {
10658 1157873 : case REAL_CST:
10659 1157873 : r = TREE_REAL_CST (cst);
10660 :
10661 1157873 : if (exact_real_inverse (TYPE_MODE (type), &r))
10662 332069 : return build_real (type, r);
10663 :
10664 : return NULL_TREE;
10665 :
10666 613 : case VECTOR_CST:
10667 613 : {
10668 613 : unit_type = TREE_TYPE (type);
10669 613 : mode = TYPE_MODE (unit_type);
10670 :
10671 613 : tree_vector_builder elts;
10672 613 : if (!elts.new_unary_operation (type, cst, false))
10673 : return NULL_TREE;
10674 613 : unsigned int count = elts.encoded_nelts ();
10675 673 : for (unsigned int i = 0; i < count; ++i)
10676 : {
10677 613 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10678 613 : if (!exact_real_inverse (mode, &r))
10679 : return NULL_TREE;
10680 60 : elts.quick_push (build_real (unit_type, r));
10681 : }
10682 :
10683 60 : return elts.build ();
10684 613 : }
10685 :
10686 : default:
10687 : return NULL_TREE;
10688 : }
10689 : }
10690 :
10691 : /* Mask out the tz least significant bits of X of type TYPE where
10692 : tz is the number of trailing zeroes in Y. */
10693 : static wide_int
10694 140171 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10695 : {
10696 140171 : int tz = wi::ctz (y);
10697 140171 : if (tz > 0)
10698 6570 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10699 133601 : return x;
10700 : }
10701 :
10702 : /* Return true when T is an address and is known to be nonzero.
10703 : For floating point we further ensure that T is not denormal.
10704 : Similar logic is present in nonzero_address in rtlanal.h. */
10705 :
10706 : bool
10707 149663500 : tree_expr_nonzero_p (tree t)
10708 : {
10709 150017548 : tree type = TREE_TYPE (t);
10710 150017548 : enum tree_code code;
10711 :
10712 : /* Doing something useful for floating point would need more work. */
10713 150017548 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10714 : return false;
10715 :
10716 149894039 : code = TREE_CODE (t);
10717 149894039 : switch (TREE_CODE_CLASS (code))
10718 : {
10719 976863 : case tcc_unary:
10720 976863 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10721 2951740 : case tcc_binary:
10722 2951740 : case tcc_comparison:
10723 2951740 : return tree_binary_nonzero_p (code, type,
10724 2951740 : TREE_OPERAND (t, 0),
10725 5903480 : TREE_OPERAND (t, 1));
10726 13179626 : case tcc_constant:
10727 13179626 : case tcc_declaration:
10728 13179626 : case tcc_reference:
10729 13179626 : return tree_single_nonzero_p (t);
10730 :
10731 132785810 : default:
10732 132785810 : break;
10733 : }
10734 :
10735 132785810 : switch (code)
10736 : {
10737 529875 : case TRUTH_NOT_EXPR:
10738 529875 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10739 :
10740 70782 : case TRUTH_AND_EXPR:
10741 70782 : case TRUTH_OR_EXPR:
10742 70782 : case TRUTH_XOR_EXPR:
10743 70782 : return tree_binary_nonzero_p (code, type,
10744 70782 : TREE_OPERAND (t, 0),
10745 141564 : TREE_OPERAND (t, 1));
10746 :
10747 128707791 : case COND_EXPR:
10748 128707791 : case CONSTRUCTOR:
10749 128707791 : case OBJ_TYPE_REF:
10750 128707791 : case ADDR_EXPR:
10751 128707791 : case WITH_SIZE_EXPR:
10752 128707791 : case SSA_NAME:
10753 128707791 : return tree_single_nonzero_p (t);
10754 :
10755 84958 : case COMPOUND_EXPR:
10756 84958 : case MODIFY_EXPR:
10757 84958 : case BIND_EXPR:
10758 84958 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10759 :
10760 269090 : case SAVE_EXPR:
10761 269090 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10762 :
10763 3067859 : case CALL_EXPR:
10764 3067859 : {
10765 3067859 : tree fndecl = get_callee_fndecl (t);
10766 3067859 : if (!fndecl) return false;
10767 3065902 : if (flag_delete_null_pointer_checks && !flag_check_new
10768 3065902 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10769 3066604 : && !TREE_NOTHROW (fndecl))
10770 : return true;
10771 3066604 : if (flag_delete_null_pointer_checks
10772 6132506 : && lookup_attribute ("returns_nonnull",
10773 3065902 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10774 : return true;
10775 3066596 : return alloca_call_p (t);
10776 : }
10777 :
10778 : default:
10779 : break;
10780 : }
10781 : return false;
10782 : }
10783 :
10784 : /* Return true if T is known not to be equal to an integer W.
10785 : If STMT is specified, the check is if T on STMT is not equal
10786 : to W. */
10787 :
10788 : bool
10789 101155643 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10790 : {
10791 101155643 : int_range_max vr;
10792 101155643 : switch (TREE_CODE (t))
10793 : {
10794 1085176 : case INTEGER_CST:
10795 1085176 : return wi::to_wide (t) != w;
10796 :
10797 100069396 : case SSA_NAME:
10798 100069396 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10799 : return false;
10800 :
10801 200138792 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10802 100069396 : if (!vr.undefined_p () && !vr.contains_p (w))
10803 : return true;
10804 : /* If T has some known zero bits and W has any of those bits set,
10805 : then T is known not to be equal to W. */
10806 99936041 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10807 199871650 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10808 : return true;
10809 : return false;
10810 :
10811 : default:
10812 : return false;
10813 : }
10814 101155643 : }
10815 :
10816 : /* Fold a binary expression of code CODE and type TYPE with operands
10817 : OP0 and OP1. LOC is the location of the resulting expression.
10818 : Return the folded expression if folding is successful. Otherwise,
10819 : return NULL_TREE. */
10820 :
10821 : tree
10822 961134872 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10823 : tree op0, tree op1)
10824 : {
10825 961134872 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10826 961134872 : tree arg0, arg1, tem;
10827 961134872 : tree t1 = NULL_TREE;
10828 961134872 : unsigned int prec;
10829 :
10830 961134872 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10831 : && TREE_CODE_LENGTH (code) == 2
10832 : && op0 != NULL_TREE
10833 : && op1 != NULL_TREE);
10834 :
10835 961134872 : arg0 = op0;
10836 961134872 : arg1 = op1;
10837 :
10838 : /* Strip any conversions that don't change the mode. This is
10839 : safe for every expression, except for a comparison expression
10840 : because its signedness is derived from its operands. So, in
10841 : the latter case, only strip conversions that don't change the
10842 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10843 : preserved.
10844 :
10845 : Note that this is done as an internal manipulation within the
10846 : constant folder, in order to find the simplest representation
10847 : of the arguments so that their form can be studied. In any
10848 : cases, the appropriate type conversions should be put back in
10849 : the tree that will get out of the constant folder. */
10850 :
10851 961134872 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10852 : {
10853 211025870 : STRIP_SIGN_NOPS (arg0);
10854 211025870 : STRIP_SIGN_NOPS (arg1);
10855 : }
10856 : else
10857 : {
10858 750109002 : STRIP_NOPS (arg0);
10859 750109002 : STRIP_NOPS (arg1);
10860 : }
10861 :
10862 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10863 : constant but we can't do arithmetic on them. */
10864 961134872 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10865 : {
10866 269171940 : tem = const_binop (code, type, arg0, arg1);
10867 269171940 : if (tem != NULL_TREE)
10868 : {
10869 266615336 : if (TREE_TYPE (tem) != type)
10870 4193426 : tem = fold_convert_loc (loc, type, tem);
10871 266615336 : return tem;
10872 : }
10873 : }
10874 :
10875 : /* If this is a commutative operation, and ARG0 is a constant, move it
10876 : to ARG1 to reduce the number of tests below. */
10877 694519536 : if (commutative_tree_code (code)
10878 694519536 : && tree_swap_operands_p (arg0, arg1))
10879 33272224 : return fold_build2_loc (loc, code, type, op1, op0);
10880 :
10881 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10882 : to ARG1 to reduce the number of tests below. */
10883 661247312 : if (kind == tcc_comparison
10884 661247312 : && tree_swap_operands_p (arg0, arg1))
10885 8373635 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10886 :
10887 652873677 : tem = generic_simplify (loc, code, type, op0, op1);
10888 652873677 : if (tem)
10889 : return tem;
10890 :
10891 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10892 :
10893 : First check for cases where an arithmetic operation is applied to a
10894 : compound, conditional, or comparison operation. Push the arithmetic
10895 : operation inside the compound or conditional to see if any folding
10896 : can then be done. Convert comparison to conditional for this purpose.
10897 : The also optimizes non-constant cases that used to be done in
10898 : expand_expr.
10899 :
10900 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10901 : one of the operands is a comparison and the other is a comparison, a
10902 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10903 : code below would make the expression more complex. Change it to a
10904 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10905 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10906 :
10907 552049497 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10908 : || code == EQ_EXPR || code == NE_EXPR)
10909 58304831 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10910 57718979 : && ((truth_value_p (TREE_CODE (arg0))
10911 1157466 : && (truth_value_p (TREE_CODE (arg1))
10912 838664 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10913 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10914 57400161 : || (truth_value_p (TREE_CODE (arg1))
10915 6764 : && (truth_value_p (TREE_CODE (arg0))
10916 6764 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10917 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10918 : {
10919 398328 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10920 79496 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10921 : : TRUTH_XOR_EXPR,
10922 : boolean_type_node,
10923 : fold_convert_loc (loc, boolean_type_node, arg0),
10924 : fold_convert_loc (loc, boolean_type_node, arg1));
10925 :
10926 318832 : if (code == EQ_EXPR)
10927 72911 : tem = invert_truthvalue_loc (loc, tem);
10928 :
10929 318832 : return fold_convert_loc (loc, type, tem);
10930 : }
10931 :
10932 551730665 : if (TREE_CODE_CLASS (code) == tcc_binary
10933 302329549 : || TREE_CODE_CLASS (code) == tcc_comparison)
10934 : {
10935 345546986 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
10936 : {
10937 82855 : tem = fold_build2_loc (loc, code, type,
10938 82855 : fold_convert_loc (loc, TREE_TYPE (op0),
10939 82855 : TREE_OPERAND (arg0, 1)), op1);
10940 82855 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10941 82855 : tem);
10942 : }
10943 345464131 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
10944 : {
10945 3153 : tem = fold_build2_loc (loc, code, type, op0,
10946 3153 : fold_convert_loc (loc, TREE_TYPE (op1),
10947 3153 : TREE_OPERAND (arg1, 1)));
10948 3153 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10949 3153 : tem);
10950 : }
10951 :
10952 345460978 : if (TREE_CODE (arg0) == COND_EXPR
10953 345083936 : || TREE_CODE (arg0) == VEC_COND_EXPR
10954 345080654 : || COMPARISON_CLASS_P (arg0))
10955 : {
10956 708417 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10957 : arg0, arg1,
10958 : /*cond_first_p=*/1);
10959 708417 : if (tem != NULL_TREE)
10960 : return tem;
10961 : }
10962 :
10963 344980084 : if (TREE_CODE (arg1) == COND_EXPR
10964 344737516 : || TREE_CODE (arg1) == VEC_COND_EXPR
10965 344737046 : || COMPARISON_CLASS_P (arg1))
10966 : {
10967 255505 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10968 : arg1, arg0,
10969 : /*cond_first_p=*/0);
10970 255505 : if (tem != NULL_TREE)
10971 : return tem;
10972 : }
10973 : }
10974 :
10975 551155689 : switch (code)
10976 : {
10977 63940449 : case MEM_REF:
10978 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10979 63940449 : if (TREE_CODE (arg0) == ADDR_EXPR
10980 63940449 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10981 : {
10982 837008 : tree iref = TREE_OPERAND (arg0, 0);
10983 837008 : return fold_build2 (MEM_REF, type,
10984 : TREE_OPERAND (iref, 0),
10985 : int_const_binop (PLUS_EXPR, arg1,
10986 : TREE_OPERAND (iref, 1)));
10987 : }
10988 :
10989 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10990 63103441 : if (TREE_CODE (arg0) == ADDR_EXPR
10991 63103441 : && handled_component_p (TREE_OPERAND (arg0, 0)))
10992 : {
10993 6163162 : tree base;
10994 6163162 : poly_int64 coffset;
10995 6163162 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10996 : &coffset);
10997 6163162 : if (!base)
10998 : return NULL_TREE;
10999 6159172 : return fold_build2 (MEM_REF, type,
11000 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11001 : int_const_binop (PLUS_EXPR, arg1,
11002 : size_int (coffset)));
11003 : }
11004 :
11005 : return NULL_TREE;
11006 :
11007 71864371 : case POINTER_PLUS_EXPR:
11008 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11009 143728326 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11010 143719554 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11011 32978 : return fold_convert_loc (loc, type,
11012 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11013 : fold_convert_loc (loc, sizetype,
11014 : arg1),
11015 : fold_convert_loc (loc, sizetype,
11016 32978 : arg0)));
11017 :
11018 : return NULL_TREE;
11019 :
11020 63401071 : case PLUS_EXPR:
11021 63401071 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11022 : {
11023 : /* X + (X / CST) * -CST is X % CST. */
11024 51881510 : if (TREE_CODE (arg1) == MULT_EXPR
11025 2313654 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11026 51887763 : && operand_equal_p (arg0,
11027 6253 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11028 : {
11029 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11030 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11031 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11032 : cst1, cst0);
11033 204 : if (sum && integer_zerop (sum))
11034 204 : return fold_convert_loc (loc, type,
11035 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11036 204 : TREE_TYPE (arg0), arg0,
11037 204 : cst0));
11038 : }
11039 : }
11040 :
11041 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11042 : one. Make sure the type is not saturating and has the signedness of
11043 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11044 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11045 63400867 : if ((TREE_CODE (arg0) == MULT_EXPR
11046 51367093 : || TREE_CODE (arg1) == MULT_EXPR)
11047 13371114 : && !TYPE_SATURATING (type)
11048 13371114 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11049 12974134 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11050 75670075 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11051 : {
11052 8983781 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11053 8983781 : if (tem)
11054 : return tem;
11055 : }
11056 :
11057 62031072 : if (! FLOAT_TYPE_P (type))
11058 : {
11059 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11060 : (plus (plus (mult) (mult)) (foo)) so that we can
11061 : take advantage of the factoring cases below. */
11062 282266 : if (ANY_INTEGRAL_TYPE_P (type)
11063 50513875 : && TYPE_OVERFLOW_WRAPS (type)
11064 50513875 : && (((TREE_CODE (arg0) == PLUS_EXPR
11065 31693449 : || TREE_CODE (arg0) == MINUS_EXPR)
11066 3428302 : && TREE_CODE (arg1) == MULT_EXPR)
11067 31188451 : || ((TREE_CODE (arg1) == PLUS_EXPR
11068 31188451 : || TREE_CODE (arg1) == MINUS_EXPR)
11069 427639 : && TREE_CODE (arg0) == MULT_EXPR)))
11070 : {
11071 551049 : tree parg0, parg1, parg, marg;
11072 551049 : enum tree_code pcode;
11073 :
11074 551049 : if (TREE_CODE (arg1) == MULT_EXPR)
11075 : parg = arg0, marg = arg1;
11076 : else
11077 46051 : parg = arg1, marg = arg0;
11078 551049 : pcode = TREE_CODE (parg);
11079 551049 : parg0 = TREE_OPERAND (parg, 0);
11080 551049 : parg1 = TREE_OPERAND (parg, 1);
11081 551049 : STRIP_NOPS (parg0);
11082 551049 : STRIP_NOPS (parg1);
11083 :
11084 551049 : if (TREE_CODE (parg0) == MULT_EXPR
11085 265552 : && TREE_CODE (parg1) != MULT_EXPR)
11086 231181 : return fold_build2_loc (loc, pcode, type,
11087 : fold_build2_loc (loc, PLUS_EXPR, type,
11088 : fold_convert_loc (loc, type,
11089 : parg0),
11090 : fold_convert_loc (loc, type,
11091 : marg)),
11092 231181 : fold_convert_loc (loc, type, parg1));
11093 319868 : if (TREE_CODE (parg0) != MULT_EXPR
11094 285497 : && TREE_CODE (parg1) == MULT_EXPR)
11095 100360 : return
11096 100360 : fold_build2_loc (loc, PLUS_EXPR, type,
11097 : fold_convert_loc (loc, type, parg0),
11098 : fold_build2_loc (loc, pcode, type,
11099 : fold_convert_loc (loc, type, marg),
11100 : fold_convert_loc (loc, type,
11101 100360 : parg1)));
11102 : }
11103 : }
11104 : else
11105 : {
11106 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11107 : to __complex__ ( x, y ). This is not the same for SNaNs or
11108 : if signed zeros are involved. */
11109 11517197 : if (!HONOR_SNANS (arg0)
11110 11515537 : && !HONOR_SIGNED_ZEROS (arg0)
11111 11538429 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11112 : {
11113 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11114 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11115 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11116 3086 : bool arg0rz = false, arg0iz = false;
11117 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11118 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11119 : {
11120 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11121 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11122 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11123 : {
11124 22 : tree rp = arg1r ? arg1r
11125 0 : : build1 (REALPART_EXPR, rtype, arg1);
11126 22 : tree ip = arg0i ? arg0i
11127 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11128 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11129 : }
11130 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11131 : {
11132 53 : tree rp = arg0r ? arg0r
11133 0 : : build1 (REALPART_EXPR, rtype, arg0);
11134 53 : tree ip = arg1i ? arg1i
11135 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11136 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11137 : }
11138 : }
11139 : }
11140 :
11141 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11142 : We associate floats only if the user has specified
11143 : -fassociative-math. */
11144 11517122 : if (flag_associative_math
11145 21136 : && TREE_CODE (arg1) == PLUS_EXPR
11146 36 : && TREE_CODE (arg0) != MULT_EXPR)
11147 : {
11148 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11149 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11150 21 : if (TREE_CODE (tree11) == MULT_EXPR
11151 5 : && TREE_CODE (tree10) == MULT_EXPR)
11152 : {
11153 1 : tree tree0;
11154 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11155 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11156 : }
11157 : }
11158 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11159 : We associate floats only if the user has specified
11160 : -fassociative-math. */
11161 11517121 : if (flag_associative_math
11162 21135 : && TREE_CODE (arg0) == PLUS_EXPR
11163 1219 : && TREE_CODE (arg1) != MULT_EXPR)
11164 : {
11165 831 : tree tree00 = TREE_OPERAND (arg0, 0);
11166 831 : tree tree01 = TREE_OPERAND (arg0, 1);
11167 831 : if (TREE_CODE (tree01) == MULT_EXPR
11168 49 : && TREE_CODE (tree00) == MULT_EXPR)
11169 : {
11170 9 : tree tree0;
11171 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11172 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11173 : }
11174 : }
11175 : }
11176 :
11177 11516290 : bit_rotate:
11178 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11179 : is a rotate of A by C1 bits. */
11180 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11181 : is a rotate of A by B bits.
11182 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11183 : though in this case CODE must be | and not + or ^, otherwise
11184 : it doesn't return A when B is 0. */
11185 64544998 : {
11186 64544998 : enum tree_code code0, code1;
11187 64544998 : tree rtype;
11188 64544998 : code0 = TREE_CODE (arg0);
11189 64544998 : code1 = TREE_CODE (arg1);
11190 73871 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11191 64528656 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11192 39685 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11193 39685 : TREE_OPERAND (arg1, 0), 0)
11194 36915 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11195 36915 : TYPE_UNSIGNED (rtype))
11196 : /* Only create rotates in complete modes. Other cases are not
11197 : expanded properly. */
11198 64571815 : && (element_precision (rtype)
11199 53634 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11200 : {
11201 26747 : tree tree01, tree11;
11202 26747 : tree orig_tree01, orig_tree11;
11203 26747 : enum tree_code code01, code11;
11204 :
11205 26747 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11206 26747 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11207 26747 : STRIP_NOPS (tree01);
11208 26747 : STRIP_NOPS (tree11);
11209 26747 : code01 = TREE_CODE (tree01);
11210 26747 : code11 = TREE_CODE (tree11);
11211 26747 : if (code11 != MINUS_EXPR
11212 26061 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11213 : {
11214 1462 : std::swap (code0, code1);
11215 1462 : std::swap (code01, code11);
11216 1462 : std::swap (tree01, tree11);
11217 1462 : std::swap (orig_tree01, orig_tree11);
11218 : }
11219 53494 : if (code01 == INTEGER_CST
11220 3152 : && code11 == INTEGER_CST
11221 33049 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11222 33049 : == element_precision (rtype)))
11223 : {
11224 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11225 3011 : rtype, TREE_OPERAND (arg0, 0),
11226 : code0 == LSHIFT_EXPR
11227 : ? orig_tree01 : orig_tree11);
11228 3011 : return fold_convert_loc (loc, type, tem);
11229 : }
11230 23736 : else if (code11 == MINUS_EXPR)
11231 : {
11232 941 : tree tree110, tree111;
11233 941 : tree110 = TREE_OPERAND (tree11, 0);
11234 941 : tree111 = TREE_OPERAND (tree11, 1);
11235 941 : STRIP_NOPS (tree110);
11236 941 : STRIP_NOPS (tree111);
11237 941 : if (TREE_CODE (tree110) == INTEGER_CST
11238 930 : && compare_tree_int (tree110,
11239 930 : element_precision (rtype)) == 0
11240 1855 : && operand_equal_p (tree01, tree111, 0))
11241 : {
11242 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11243 : ? LROTATE_EXPR : RROTATE_EXPR),
11244 558 : rtype, TREE_OPERAND (arg0, 0),
11245 : orig_tree01);
11246 558 : return fold_convert_loc (loc, type, tem);
11247 : }
11248 : }
11249 22795 : else if (code == BIT_IOR_EXPR
11250 21681 : && code11 == BIT_AND_EXPR
11251 44401 : && pow2p_hwi (element_precision (rtype)))
11252 : {
11253 21606 : tree tree110, tree111;
11254 21606 : tree110 = TREE_OPERAND (tree11, 0);
11255 21606 : tree111 = TREE_OPERAND (tree11, 1);
11256 21606 : STRIP_NOPS (tree110);
11257 21606 : STRIP_NOPS (tree111);
11258 21606 : if (TREE_CODE (tree110) == NEGATE_EXPR
11259 21151 : && TREE_CODE (tree111) == INTEGER_CST
11260 21151 : && compare_tree_int (tree111,
11261 21151 : element_precision (rtype) - 1) == 0
11262 42743 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11263 : {
11264 31577 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11265 : ? LROTATE_EXPR : RROTATE_EXPR),
11266 21075 : rtype, TREE_OPERAND (arg0, 0),
11267 : orig_tree01);
11268 21075 : return fold_convert_loc (loc, type, tem);
11269 : }
11270 : }
11271 : }
11272 : }
11273 :
11274 157896150 : associate:
11275 : /* In most languages, can't associate operations on floats through
11276 : parentheses. Rather than remember where the parentheses were, we
11277 : don't associate floats at all, unless the user has specified
11278 : -fassociative-math.
11279 : And, we need to make sure type is not saturating. */
11280 :
11281 157896150 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11282 117044648 : && !TYPE_SATURATING (type)
11283 274940798 : && !TYPE_OVERFLOW_SANITIZED (type))
11284 : {
11285 117016409 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11286 117016409 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11287 117016409 : tree atype = type;
11288 117016409 : bool ok = true;
11289 :
11290 : /* Split both trees into variables, constants, and literals. Then
11291 : associate each group together, the constants with literals,
11292 : then the result with variables. This increases the chances of
11293 : literals being recombined later and of generating relocatable
11294 : expressions for the sum of a constant and literal. */
11295 117016409 : var0 = split_tree (arg0, type, code,
11296 : &minus_var0, &con0, &minus_con0,
11297 : &lit0, &minus_lit0, 0);
11298 117016409 : var1 = split_tree (arg1, type, code,
11299 : &minus_var1, &con1, &minus_con1,
11300 : &lit1, &minus_lit1, code == MINUS_EXPR);
11301 :
11302 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11303 117016409 : if (code == MINUS_EXPR)
11304 12872267 : code = PLUS_EXPR;
11305 :
11306 : /* With undefined overflow prefer doing association in a type
11307 : which wraps on overflow, if that is one of the operand types. */
11308 117016178 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11309 232785976 : && !TYPE_OVERFLOW_WRAPS (type))
11310 : {
11311 62225946 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11312 61570236 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11313 825127 : atype = TREE_TYPE (arg0);
11314 60538898 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11315 60286330 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11316 235713 : atype = TREE_TYPE (arg1);
11317 31370054 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11318 : }
11319 :
11320 : /* With undefined overflow we can only associate constants with one
11321 : variable, and constants whose association doesn't overflow. */
11322 117016178 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11323 232785976 : && !TYPE_OVERFLOW_WRAPS (atype))
11324 : {
11325 30309214 : if ((var0 && var1) || (minus_var0 && minus_var1))
11326 : {
11327 : /* ??? If split_tree would handle NEGATE_EXPR we could
11328 : simply reject these cases and the allowed cases would
11329 : be the var0/minus_var1 ones. */
11330 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11331 5652944 : tree tmp1 = var1 ? var1 : minus_var1;
11332 5652944 : bool one_neg = false;
11333 :
11334 5652944 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11335 : {
11336 730 : tmp0 = TREE_OPERAND (tmp0, 0);
11337 730 : one_neg = !one_neg;
11338 : }
11339 5003605 : if (CONVERT_EXPR_P (tmp0)
11340 675082 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11341 6327075 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11342 674131 : <= TYPE_PRECISION (atype)))
11343 661625 : tmp0 = TREE_OPERAND (tmp0, 0);
11344 5652944 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11345 : {
11346 168 : tmp1 = TREE_OPERAND (tmp1, 0);
11347 168 : one_neg = !one_neg;
11348 : }
11349 5326629 : if (CONVERT_EXPR_P (tmp1)
11350 395420 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11351 6048240 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11352 395296 : <= TYPE_PRECISION (atype)))
11353 378216 : tmp1 = TREE_OPERAND (tmp1, 0);
11354 : /* The only case we can still associate with two variables
11355 : is if they cancel out. */
11356 5652944 : if (!one_neg
11357 5652944 : || !operand_equal_p (tmp0, tmp1, 0))
11358 : ok = false;
11359 : }
11360 24256691 : else if ((var0 && minus_var1
11361 4140108 : && ! operand_equal_p (var0, minus_var1, 0))
11362 44772854 : || (minus_var0 && var1
11363 11393 : && ! operand_equal_p (minus_var0, var1, 0)))
11364 : ok = false;
11365 : }
11366 :
11367 : /* Only do something if we found more than two objects. Otherwise,
11368 : nothing has changed and we risk infinite recursion. */
11369 : if (ok
11370 107212037 : && ((var0 != 0) + (var1 != 0)
11371 107212037 : + (minus_var0 != 0) + (minus_var1 != 0)
11372 107212037 : + (con0 != 0) + (con1 != 0)
11373 107212037 : + (minus_con0 != 0) + (minus_con1 != 0)
11374 107212037 : + (lit0 != 0) + (lit1 != 0)
11375 107212037 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11376 : {
11377 2044003 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11378 4088006 : int minus_var0_origin
11379 2044003 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11380 2044003 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11381 4088006 : int minus_con0_origin
11382 2044003 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11383 2044003 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11384 4088006 : int minus_lit0_origin
11385 2044003 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11386 2044003 : var0 = associate_trees (loc, var0, var1, code, atype);
11387 2044003 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11388 : code, atype);
11389 2044003 : con0 = associate_trees (loc, con0, con1, code, atype);
11390 2044003 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11391 : code, atype);
11392 2044003 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11393 2044003 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11394 : code, atype);
11395 :
11396 2044003 : if (minus_var0 && var0)
11397 : {
11398 1346734 : var0_origin |= minus_var0_origin;
11399 1346734 : var0 = associate_trees (loc, var0, minus_var0,
11400 : MINUS_EXPR, atype);
11401 1346734 : minus_var0 = 0;
11402 1346734 : minus_var0_origin = 0;
11403 : }
11404 2044003 : if (minus_con0 && con0)
11405 : {
11406 3690 : con0_origin |= minus_con0_origin;
11407 3690 : con0 = associate_trees (loc, con0, minus_con0,
11408 : MINUS_EXPR, atype);
11409 3690 : minus_con0 = 0;
11410 3690 : minus_con0_origin = 0;
11411 : }
11412 :
11413 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11414 : greater than the positive part. Otherwise, the multiplicative
11415 : folding code (i.e extract_muldiv) may be fooled in case
11416 : unsigned constants are subtracted, like in the following
11417 : example: ((X*2 + 4) - 8U)/2. */
11418 2044003 : if (minus_lit0 && lit0)
11419 : {
11420 236844 : if (TREE_CODE (lit0) == INTEGER_CST
11421 236844 : && TREE_CODE (minus_lit0) == INTEGER_CST
11422 236844 : && tree_int_cst_lt (lit0, minus_lit0)
11423 : /* But avoid ending up with only negated parts. */
11424 298191 : && (var0 || con0))
11425 : {
11426 56852 : minus_lit0_origin |= lit0_origin;
11427 56852 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11428 : MINUS_EXPR, atype);
11429 56852 : lit0 = 0;
11430 56852 : lit0_origin = 0;
11431 : }
11432 : else
11433 : {
11434 179992 : lit0_origin |= minus_lit0_origin;
11435 179992 : lit0 = associate_trees (loc, lit0, minus_lit0,
11436 : MINUS_EXPR, atype);
11437 179992 : minus_lit0 = 0;
11438 179992 : minus_lit0_origin = 0;
11439 : }
11440 : }
11441 :
11442 : /* Don't introduce overflows through reassociation. */
11443 1351652 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11444 3395617 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11445 2044003 : return NULL_TREE;
11446 :
11447 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11448 2043965 : con0_origin |= lit0_origin;
11449 2043965 : con0 = associate_trees (loc, con0, lit0, code, atype);
11450 2043965 : minus_con0_origin |= minus_lit0_origin;
11451 2043965 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11452 : code, atype);
11453 :
11454 : /* Eliminate minus_con0. */
11455 2043965 : if (minus_con0)
11456 : {
11457 697194 : if (con0)
11458 : {
11459 15903 : con0_origin |= minus_con0_origin;
11460 15903 : con0 = associate_trees (loc, con0, minus_con0,
11461 : MINUS_EXPR, atype);
11462 : }
11463 681291 : else if (var0)
11464 : {
11465 681291 : var0_origin |= minus_con0_origin;
11466 681291 : var0 = associate_trees (loc, var0, minus_con0,
11467 : MINUS_EXPR, atype);
11468 : }
11469 : else
11470 0 : gcc_unreachable ();
11471 : }
11472 :
11473 : /* Eliminate minus_var0. */
11474 2043965 : if (minus_var0)
11475 : {
11476 347555 : if (con0)
11477 : {
11478 347555 : con0_origin |= minus_var0_origin;
11479 347555 : con0 = associate_trees (loc, con0, minus_var0,
11480 : MINUS_EXPR, atype);
11481 : }
11482 : else
11483 0 : gcc_unreachable ();
11484 : }
11485 :
11486 : /* Reassociate only if there has been any actual association
11487 : between subtrees from op0 and subtrees from op1 in at
11488 : least one of the operands, otherwise we risk infinite
11489 : recursion. See PR114084. */
11490 2043965 : if (var0_origin != 3 && con0_origin != 3)
11491 : return NULL_TREE;
11492 :
11493 2042336 : return
11494 2042336 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11495 2042336 : code, atype));
11496 : }
11497 : }
11498 :
11499 : return NULL_TREE;
11500 :
11501 23649141 : case POINTER_DIFF_EXPR:
11502 23649141 : case MINUS_EXPR:
11503 : /* Fold &a[i] - &a[j] to i-j. */
11504 23649141 : if (TREE_CODE (arg0) == ADDR_EXPR
11505 48006 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11506 6199 : && TREE_CODE (arg1) == ADDR_EXPR
11507 23649749 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11508 : {
11509 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11510 38 : TREE_OPERAND (arg0, 0),
11511 38 : TREE_OPERAND (arg1, 0),
11512 : code
11513 : == POINTER_DIFF_EXPR);
11514 38 : if (tem)
11515 : return tem;
11516 : }
11517 :
11518 : /* Further transformations are not for pointers. */
11519 23649127 : if (code == POINTER_DIFF_EXPR)
11520 : return NULL_TREE;
11521 :
11522 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11523 20890785 : if (TREE_CODE (arg0) == NEGATE_EXPR
11524 144460 : && negate_expr_p (op1)
11525 : /* If arg0 is e.g. unsigned int and type is int, then this could
11526 : introduce UB, because if A is INT_MIN at runtime, the original
11527 : expression can be well defined while the latter is not.
11528 : See PR83269. */
11529 20891615 : && !(ANY_INTEGRAL_TYPE_P (type)
11530 830 : && TYPE_OVERFLOW_UNDEFINED (type)
11531 818 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11532 818 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11533 823 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11534 : fold_convert_loc (loc, type,
11535 1646 : TREE_OPERAND (arg0, 0)));
11536 :
11537 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11538 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11539 : signed zeros are involved. */
11540 20889962 : if (!HONOR_SNANS (arg0)
11541 20888811 : && !HONOR_SIGNED_ZEROS (arg0)
11542 34567983 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11543 : {
11544 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11545 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11546 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11547 53 : bool arg0rz = false, arg0iz = false;
11548 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11549 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11550 : {
11551 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11552 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11553 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11554 : {
11555 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11556 : arg1r ? arg1r
11557 0 : : build1 (REALPART_EXPR, rtype, arg1));
11558 9 : tree ip = arg0i ? arg0i
11559 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11560 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11561 : }
11562 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11563 : {
11564 15 : tree rp = arg0r ? arg0r
11565 0 : : build1 (REALPART_EXPR, rtype, arg0);
11566 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11567 : arg1i ? arg1i
11568 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11569 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11570 : }
11571 : }
11572 : }
11573 :
11574 : /* A - B -> A + (-B) if B is easily negatable. */
11575 20889938 : if (negate_expr_p (op1)
11576 751158 : && ! TYPE_OVERFLOW_SANITIZED (type)
11577 21638569 : && ((FLOAT_TYPE_P (type)
11578 : /* Avoid this transformation if B is a positive REAL_CST. */
11579 65 : && (TREE_CODE (op1) != REAL_CST
11580 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11581 748566 : || INTEGRAL_TYPE_P (type)))
11582 748420 : return fold_build2_loc (loc, PLUS_EXPR, type,
11583 : fold_convert_loc (loc, type, arg0),
11584 748420 : negate_expr (op1));
11585 :
11586 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11587 : one. Make sure the type is not saturating and has the signedness of
11588 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11589 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11590 20141518 : if ((TREE_CODE (arg0) == MULT_EXPR
11591 18842378 : || TREE_CODE (arg1) == MULT_EXPR)
11592 2648664 : && !TYPE_SATURATING (type)
11593 2648664 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11594 2508808 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11595 22595611 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11596 : {
11597 371613 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11598 371613 : if (tem)
11599 : return tem;
11600 : }
11601 :
11602 20091869 : goto associate;
11603 :
11604 67139394 : case MULT_EXPR:
11605 67139394 : if (! FLOAT_TYPE_P (type))
11606 : {
11607 : /* Transform x * -C into -x * C if x is easily negatable. */
11608 44965915 : if (TREE_CODE (op1) == INTEGER_CST
11609 41912381 : && tree_int_cst_sgn (op1) == -1
11610 224651 : && negate_expr_p (op0)
11611 340 : && negate_expr_p (op1)
11612 324 : && (tem = negate_expr (op1)) != op1
11613 44966239 : && ! TREE_OVERFLOW (tem))
11614 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11615 : fold_convert_loc (loc, type,
11616 324 : negate_expr (op0)), tem);
11617 :
11618 44965591 : if (TREE_CODE (arg1) == INTEGER_CST
11619 44965591 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11620 : {
11621 731127 : return fold_convert_loc (loc, type, tem);
11622 : }
11623 :
11624 : /* Optimize z * conj(z) for integer complex numbers. */
11625 44234464 : if (TREE_CODE (arg0) == CONJ_EXPR
11626 44234464 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11627 1 : return fold_mult_zconjz (loc, type, arg1);
11628 44234463 : if (TREE_CODE (arg1) == CONJ_EXPR
11629 44234463 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11630 0 : return fold_mult_zconjz (loc, type, arg0);
11631 : }
11632 : else
11633 : {
11634 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11635 : This is not the same for NaNs or if signed zeros are
11636 : involved. */
11637 22173479 : if (!HONOR_NANS (arg0)
11638 32883 : && !HONOR_SIGNED_ZEROS (arg0)
11639 32582 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11640 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11641 22173704 : && real_zerop (TREE_REALPART (arg1)))
11642 : {
11643 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11644 218 : if (real_onep (TREE_IMAGPART (arg1)))
11645 : {
11646 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11647 63 : arg0 = save_expr (arg0);
11648 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11649 : rtype, arg0);
11650 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11651 : rtype, arg0);
11652 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11653 : negate_expr (iarg0),
11654 208 : rarg0);
11655 : }
11656 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11657 : {
11658 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11659 0 : arg0 = save_expr (arg0);
11660 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11661 : rtype, arg0);
11662 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11663 : rtype, arg0);
11664 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11665 : iarg0,
11666 10 : negate_expr (rarg0));
11667 : }
11668 : }
11669 :
11670 : /* Optimize z * conj(z) for floating point complex numbers.
11671 : Guarded by flag_unsafe_math_optimizations as non-finite
11672 : imaginary components don't produce scalar results. */
11673 22173261 : if (flag_unsafe_math_optimizations
11674 32411 : && TREE_CODE (arg0) == CONJ_EXPR
11675 22173263 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11676 1 : return fold_mult_zconjz (loc, type, arg1);
11677 22173260 : if (flag_unsafe_math_optimizations
11678 32410 : && TREE_CODE (arg1) == CONJ_EXPR
11679 22173264 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11680 0 : return fold_mult_zconjz (loc, type, arg0);
11681 : }
11682 66407723 : goto associate;
11683 :
11684 1897512 : case BIT_IOR_EXPR:
11685 : /* Canonicalize (X & C1) | C2. */
11686 1897512 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11687 88917 : && TREE_CODE (arg1) == INTEGER_CST
11688 1941066 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11689 : {
11690 43546 : int width = TYPE_PRECISION (type), w;
11691 43546 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11692 43546 : wide_int c2 = wi::to_wide (arg1);
11693 :
11694 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11695 43546 : if ((c1 & c2) == c1)
11696 0 : return omit_one_operand_loc (loc, type, arg1,
11697 0 : TREE_OPERAND (arg0, 0));
11698 :
11699 43546 : wide_int msk = wi::mask (width, false,
11700 43546 : TYPE_PRECISION (TREE_TYPE (arg1)));
11701 :
11702 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11703 43546 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11704 : {
11705 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11706 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11707 : }
11708 :
11709 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11710 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11711 : mode which allows further optimizations. */
11712 43540 : c1 &= msk;
11713 43540 : c2 &= msk;
11714 43540 : wide_int c3 = wi::bit_and_not (c1, c2);
11715 136802 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11716 : {
11717 93504 : wide_int mask = wi::mask (w, false,
11718 93504 : TYPE_PRECISION (type));
11719 187008 : if (((c1 | c2) & mask) == mask
11720 187008 : && wi::bit_and_not (c1, mask) == 0)
11721 : {
11722 242 : c3 = mask;
11723 242 : break;
11724 : }
11725 93504 : }
11726 :
11727 43540 : if (c3 != c1)
11728 : {
11729 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11730 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11731 558 : wide_int_to_tree (type, c3));
11732 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11733 : }
11734 44668 : }
11735 :
11736 : /* See if this can be simplified into a rotate first. If that
11737 : is unsuccessful continue in the association code. */
11738 1896948 : goto bit_rotate;
11739 :
11740 948604 : case BIT_XOR_EXPR:
11741 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11742 948604 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11743 3674 : && INTEGRAL_TYPE_P (type)
11744 3069 : && integer_onep (TREE_OPERAND (arg0, 1))
11745 949845 : && integer_onep (arg1))
11746 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11747 0 : build_zero_cst (TREE_TYPE (arg0)));
11748 :
11749 : /* See if this can be simplified into a rotate first. If that
11750 : is unsuccessful continue in the association code. */
11751 948604 : goto bit_rotate;
11752 :
11753 6449117 : case BIT_AND_EXPR:
11754 : /* Fold !X & 1 as X == 0. */
11755 6449117 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11756 6449117 : && integer_onep (arg1))
11757 : {
11758 0 : tem = TREE_OPERAND (arg0, 0);
11759 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11760 0 : build_zero_cst (TREE_TYPE (tem)));
11761 : }
11762 :
11763 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11764 : multiple of 1 << CST. */
11765 6449117 : if (TREE_CODE (arg1) == INTEGER_CST)
11766 : {
11767 4611760 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11768 4611760 : wide_int ncst1 = -cst1;
11769 4611760 : if ((cst1 & ncst1) == ncst1
11770 4773348 : && multiple_of_p (type, arg0,
11771 4773348 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11772 467 : return fold_convert_loc (loc, type, arg0);
11773 4611760 : }
11774 :
11775 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11776 : bits from CST2. */
11777 6448650 : if (TREE_CODE (arg1) == INTEGER_CST
11778 4611293 : && TREE_CODE (arg0) == MULT_EXPR
11779 6588883 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11780 : {
11781 140171 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11782 140171 : wide_int masked
11783 140171 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11784 :
11785 140171 : if (masked == 0)
11786 5214 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11787 5214 : arg0, arg1);
11788 134957 : else if (masked != warg1)
11789 : {
11790 : /* Avoid the transform if arg1 is a mask of some
11791 : mode which allows further optimizations. */
11792 648 : int pop = wi::popcount (warg1);
11793 670 : if (!(pop >= BITS_PER_UNIT
11794 50 : && pow2p_hwi (pop)
11795 692 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11796 1252 : return fold_build2_loc (loc, code, type, op0,
11797 1252 : wide_int_to_tree (type, masked));
11798 : }
11799 140171 : }
11800 :
11801 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11802 4605453 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11803 6634124 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11804 : {
11805 113374 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11806 :
11807 113374 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11808 113374 : if (mask == -1)
11809 2509 : return
11810 2509 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11811 113374 : }
11812 :
11813 6440301 : goto associate;
11814 :
11815 6082953 : case RDIV_EXPR:
11816 : /* Don't touch a floating-point divide by zero unless the mode
11817 : of the constant can represent infinity. */
11818 6082953 : if (TREE_CODE (arg1) == REAL_CST
11819 3049325 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11820 6082953 : && real_zerop (arg1))
11821 0 : return NULL_TREE;
11822 :
11823 : /* (-A) / (-B) -> A / B */
11824 6082953 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11825 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11826 3 : TREE_OPERAND (arg0, 0),
11827 3 : negate_expr (arg1));
11828 6082950 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11829 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11830 : negate_expr (arg0),
11831 0 : TREE_OPERAND (arg1, 0));
11832 : return NULL_TREE;
11833 :
11834 2139706 : case TRUNC_DIV_EXPR:
11835 : /* Fall through */
11836 :
11837 2139706 : case FLOOR_DIV_EXPR:
11838 : /* Simplify A / (B << N) where A and B are positive and B is
11839 : a power of 2, to A >> (N + log2(B)). */
11840 2139706 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11841 2139706 : && (TYPE_UNSIGNED (type)
11842 8 : || tree_expr_nonnegative_p (op0)))
11843 : {
11844 17 : tree sval = TREE_OPERAND (arg1, 0);
11845 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11846 : {
11847 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11848 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11849 16 : wi::exact_log2 (wi::to_wide (sval)));
11850 :
11851 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11852 : sh_cnt, pow2);
11853 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11854 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11855 : }
11856 : }
11857 :
11858 : /* Fall through */
11859 :
11860 3552994 : case ROUND_DIV_EXPR:
11861 3552994 : case CEIL_DIV_EXPR:
11862 3552994 : case EXACT_DIV_EXPR:
11863 3552994 : if (integer_zerop (arg1))
11864 : return NULL_TREE;
11865 :
11866 : /* Convert -A / -B to A / B when the type is signed and overflow is
11867 : undefined. */
11868 3550154 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11869 1010493 : && TREE_CODE (op0) == NEGATE_EXPR
11870 3550216 : && negate_expr_p (op1))
11871 60 : return fold_build2_loc (loc, code, type,
11872 : fold_convert_loc (loc, type,
11873 30 : TREE_OPERAND (arg0, 0)),
11874 30 : negate_expr (op1));
11875 3550124 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11876 1010463 : && TREE_CODE (arg1) == NEGATE_EXPR
11877 3550368 : && negate_expr_p (op0))
11878 36 : return fold_build2_loc (loc, code, type,
11879 : negate_expr (op0),
11880 : fold_convert_loc (loc, type,
11881 72 : TREE_OPERAND (arg1, 0)));
11882 :
11883 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11884 : operation, EXACT_DIV_EXPR.
11885 :
11886 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11887 : At one time others generated faster code, it's not clear if they do
11888 : after the last round to changes to the DIV code in expmed.cc. */
11889 3550088 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11890 3550088 : && multiple_of_p (type, arg0, arg1))
11891 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11892 : fold_convert (type, arg0),
11893 0 : fold_convert (type, arg1));
11894 :
11895 3550088 : if (TREE_CODE (arg1) == INTEGER_CST
11896 3550088 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11897 9514 : return fold_convert_loc (loc, type, tem);
11898 :
11899 : return NULL_TREE;
11900 :
11901 930677 : case CEIL_MOD_EXPR:
11902 930677 : case FLOOR_MOD_EXPR:
11903 930677 : case ROUND_MOD_EXPR:
11904 930677 : case TRUNC_MOD_EXPR:
11905 930677 : if (TREE_CODE (arg1) == INTEGER_CST
11906 930677 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11907 0 : return fold_convert_loc (loc, type, tem);
11908 :
11909 : return NULL_TREE;
11910 :
11911 2249193 : case LROTATE_EXPR:
11912 2249193 : case RROTATE_EXPR:
11913 2249193 : case RSHIFT_EXPR:
11914 2249193 : case LSHIFT_EXPR:
11915 : /* Since negative shift count is not well-defined,
11916 : don't try to compute it in the compiler. */
11917 2249193 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11918 : return NULL_TREE;
11919 :
11920 2248152 : prec = element_precision (type);
11921 :
11922 : /* If we have a rotate of a bit operation with the rotate count and
11923 : the second operand of the bit operation both constant,
11924 : permute the two operations. */
11925 2754 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11926 2224 : && (TREE_CODE (arg0) == BIT_AND_EXPR
11927 2224 : || TREE_CODE (arg0) == BIT_IOR_EXPR
11928 2224 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
11929 2248152 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11930 : {
11931 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11932 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11933 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
11934 : fold_build2_loc (loc, code, type,
11935 : arg00, arg1),
11936 : fold_build2_loc (loc, code, type,
11937 0 : arg01, arg1));
11938 : }
11939 :
11940 : return NULL_TREE;
11941 :
11942 435903 : case MIN_EXPR:
11943 435903 : case MAX_EXPR:
11944 435903 : goto associate;
11945 :
11946 6672585 : case TRUTH_ANDIF_EXPR:
11947 : /* Note that the operands of this must be ints
11948 : and their values must be 0 or 1.
11949 : ("true" is a fixed value perhaps depending on the language.) */
11950 : /* If first arg is constant zero, return it. */
11951 6672585 : if (integer_zerop (arg0))
11952 1701488 : return fold_convert_loc (loc, type, arg0);
11953 : /* FALLTHRU */
11954 15780799 : case TRUTH_AND_EXPR:
11955 : /* If either arg is constant true, drop it. */
11956 15780799 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11957 1497698 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11958 823680 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11959 : /* Preserve sequence points. */
11960 15061337 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11961 753289 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11962 : /* If second arg is constant zero, result is zero, but first arg
11963 : must be evaluated. */
11964 13529812 : if (integer_zerop (arg1))
11965 45444 : return omit_one_operand_loc (loc, type, arg1, arg0);
11966 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11967 : case will be handled here. */
11968 13484368 : if (integer_zerop (arg0))
11969 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11970 :
11971 : /* !X && X is always false. */
11972 13484368 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11973 13484368 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11974 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11975 : /* X && !X is always false. */
11976 13484368 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11977 13484368 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11978 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11979 :
11980 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11981 : means A >= Y && A != MAX, but in this case we know that
11982 : A < X <= MAX. */
11983 :
11984 13484368 : if (!TREE_SIDE_EFFECTS (arg0)
11985 13484368 : && !TREE_SIDE_EFFECTS (arg1))
11986 : {
11987 12015825 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11988 12015825 : if (tem && !operand_equal_p (tem, arg0, 0))
11989 449 : return fold_convert (type,
11990 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
11991 : tem, arg1));
11992 :
11993 12015376 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11994 12015376 : if (tem && !operand_equal_p (tem, arg1, 0))
11995 8460 : return fold_convert (type,
11996 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
11997 : arg0, tem));
11998 : }
11999 :
12000 13475459 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12001 : != NULL_TREE)
12002 : return tem;
12003 :
12004 : return NULL_TREE;
12005 :
12006 3444294 : case TRUTH_ORIF_EXPR:
12007 : /* Note that the operands of this must be ints
12008 : and their values must be 0 or true.
12009 : ("true" is a fixed value perhaps depending on the language.) */
12010 : /* If first arg is constant true, return it. */
12011 3444294 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12012 145386 : return fold_convert_loc (loc, type, arg0);
12013 : /* FALLTHRU */
12014 12774373 : case TRUTH_OR_EXPR:
12015 : /* If either arg is constant zero, drop it. */
12016 12774373 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12017 239317 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12018 484759 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12019 : /* Preserve sequence points. */
12020 12966970 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12021 420670 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12022 : /* If second arg is constant true, result is true, but we must
12023 : evaluate first arg. */
12024 12114386 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12025 52845 : return omit_one_operand_loc (loc, type, arg1, arg0);
12026 : /* Likewise for first arg, but note this only occurs here for
12027 : TRUTH_OR_EXPR. */
12028 12061541 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12029 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12030 :
12031 : /* !X || X is always true. */
12032 12061541 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12033 12061541 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12034 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12035 : /* X || !X is always true. */
12036 12061541 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12037 12061541 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12038 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12039 :
12040 : /* (X && !Y) || (!X && Y) is X ^ Y */
12041 12061540 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12042 1643 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12043 : {
12044 668 : tree a0, a1, l0, l1, n0, n1;
12045 :
12046 668 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12047 668 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12048 :
12049 668 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12050 668 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12051 :
12052 668 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12053 668 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12054 :
12055 668 : if ((operand_equal_p (n0, a0, 0)
12056 18 : && operand_equal_p (n1, a1, 0))
12057 676 : || (operand_equal_p (n0, a1, 0)
12058 3 : && operand_equal_p (n1, a0, 0)))
12059 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12060 : }
12061 :
12062 12061527 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12063 : != NULL_TREE)
12064 : return tem;
12065 :
12066 : return NULL_TREE;
12067 :
12068 78551 : case TRUTH_XOR_EXPR:
12069 : /* If the second arg is constant zero, drop it. */
12070 78551 : if (integer_zerop (arg1))
12071 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12072 : /* If the second arg is constant true, this is a logical inversion. */
12073 78551 : if (integer_onep (arg1))
12074 : {
12075 0 : tem = invert_truthvalue_loc (loc, arg0);
12076 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12077 : }
12078 : /* Identical arguments cancel to zero. */
12079 78551 : if (operand_equal_p (arg0, arg1, 0))
12080 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12081 :
12082 : /* !X ^ X is always true. */
12083 78551 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12084 78551 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12085 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12086 :
12087 : /* X ^ !X is always true. */
12088 78551 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12089 78551 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12090 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12091 :
12092 : return NULL_TREE;
12093 :
12094 49207244 : case EQ_EXPR:
12095 49207244 : case NE_EXPR:
12096 49207244 : STRIP_NOPS (arg0);
12097 49207244 : STRIP_NOPS (arg1);
12098 :
12099 49207244 : tem = fold_comparison (loc, code, type, op0, op1);
12100 49207244 : if (tem != NULL_TREE)
12101 : return tem;
12102 :
12103 : /* bool_var != 1 becomes !bool_var. */
12104 50357664 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12105 49240353 : && code == NE_EXPR)
12106 40828 : return fold_convert_loc (loc, type,
12107 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12108 81656 : TREE_TYPE (arg0), arg0));
12109 :
12110 : /* bool_var == 0 becomes !bool_var. */
12111 50276008 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12112 50091779 : && code == EQ_EXPR)
12113 196245 : return fold_convert_loc (loc, type,
12114 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12115 392490 : TREE_TYPE (arg0), arg0));
12116 :
12117 : /* !exp != 0 becomes !exp */
12118 530416 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12119 49486483 : && code == NE_EXPR)
12120 522722 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12121 :
12122 : /* If this is an EQ or NE comparison with zero and ARG0 is
12123 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12124 : two operations, but the latter can be done in one less insn
12125 : on machines that have only two-operand insns or on which a
12126 : constant cannot be the first operand. */
12127 48434056 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12128 48434056 : && integer_zerop (arg1))
12129 : {
12130 1532066 : tree arg00 = TREE_OPERAND (arg0, 0);
12131 1532066 : tree arg01 = TREE_OPERAND (arg0, 1);
12132 1532066 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12133 1532066 : && integer_onep (TREE_OPERAND (arg00, 0)))
12134 : {
12135 4280 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12136 4280 : arg01, TREE_OPERAND (arg00, 1));
12137 4280 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12138 4280 : build_one_cst (TREE_TYPE (arg0)));
12139 4280 : return fold_build2_loc (loc, code, type,
12140 4280 : fold_convert_loc (loc, TREE_TYPE (arg1),
12141 4280 : tem), arg1);
12142 : }
12143 1527786 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12144 1527786 : && integer_onep (TREE_OPERAND (arg01, 0)))
12145 : {
12146 425 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12147 425 : arg00, TREE_OPERAND (arg01, 1));
12148 425 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12149 425 : build_one_cst (TREE_TYPE (arg0)));
12150 425 : return fold_build2_loc (loc, code, type,
12151 425 : fold_convert_loc (loc, TREE_TYPE (arg1),
12152 425 : tem), arg1);
12153 : }
12154 : }
12155 :
12156 : /* If this is a comparison of a field, we may be able to simplify it. */
12157 48429351 : if ((TREE_CODE (arg0) == COMPONENT_REF
12158 48429351 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12159 : /* Handle the constant case even without -O
12160 : to make sure the warnings are given. */
12161 4915191 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12162 : {
12163 4599892 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12164 4599892 : if (t1)
12165 : return t1;
12166 : }
12167 :
12168 : /* Optimize comparisons of strlen vs zero to a compare of the
12169 : first character of the string vs zero. To wit,
12170 : strlen(ptr) == 0 => *ptr == 0
12171 : strlen(ptr) != 0 => *ptr != 0
12172 : Other cases should reduce to one of these two (or a constant)
12173 : due to the return value of strlen being unsigned. */
12174 47677606 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12175 : {
12176 3021236 : tree fndecl = get_callee_fndecl (arg0);
12177 :
12178 3021236 : if (fndecl
12179 3020150 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12180 549 : && call_expr_nargs (arg0) == 1
12181 3021785 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12182 : == POINTER_TYPE))
12183 : {
12184 549 : tree ptrtype
12185 549 : = build_pointer_type (build_qualified_type (char_type_node,
12186 : TYPE_QUAL_CONST));
12187 1098 : tree ptr = fold_convert_loc (loc, ptrtype,
12188 549 : CALL_EXPR_ARG (arg0, 0));
12189 549 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12190 549 : return fold_build2_loc (loc, code, type, iref,
12191 549 : build_int_cst (TREE_TYPE (iref), 0));
12192 : }
12193 : }
12194 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12195 : (X & C) == 0 when C is a single bit. */
12196 47677057 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12197 1692210 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12198 861 : && integer_zerop (arg1)
12199 47677519 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12200 : {
12201 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12202 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12203 140 : TREE_OPERAND (arg0, 1));
12204 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12205 : type, tem,
12206 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12207 140 : arg1));
12208 : }
12209 :
12210 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12211 : constant C is a power of two, i.e. a single bit. */
12212 47676917 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12213 4680 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12214 0 : && integer_zerop (arg1)
12215 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12216 47676917 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12217 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12218 : {
12219 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12220 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12221 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12222 : }
12223 :
12224 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12225 : when is C is a power of two, i.e. a single bit. */
12226 47676917 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12227 1692070 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12228 37931 : && integer_zerop (arg1)
12229 37931 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12230 47712188 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12231 35271 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12232 : {
12233 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12234 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12235 0 : arg000, TREE_OPERAND (arg0, 1));
12236 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12237 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12238 : }
12239 :
12240 47676917 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12241 4680 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12242 : {
12243 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12244 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12245 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12246 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12247 482 : tree itype = TREE_TYPE (arg0);
12248 :
12249 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12250 : operand_equal_p guarantees no side-effects so we don't need
12251 : to use omit_one_operand on Z. */
12252 482 : if (operand_equal_p (arg01, arg11, 0))
12253 8 : return fold_build2_loc (loc, code, type, arg00,
12254 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12255 8 : arg10));
12256 474 : if (operand_equal_p (arg01, arg10, 0))
12257 0 : return fold_build2_loc (loc, code, type, arg00,
12258 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12259 0 : arg11));
12260 474 : if (operand_equal_p (arg00, arg11, 0))
12261 0 : return fold_build2_loc (loc, code, type, arg01,
12262 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12263 0 : arg10));
12264 474 : if (operand_equal_p (arg00, arg10, 0))
12265 0 : return fold_build2_loc (loc, code, type, arg01,
12266 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12267 0 : arg11));
12268 :
12269 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12270 474 : if (TREE_CODE (arg01) == INTEGER_CST
12271 8 : && TREE_CODE (arg11) == INTEGER_CST)
12272 : {
12273 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12274 : fold_convert_loc (loc, itype, arg11));
12275 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12276 8 : return fold_build2_loc (loc, code, type, tem,
12277 8 : fold_convert_loc (loc, itype, arg10));
12278 : }
12279 : }
12280 :
12281 : /* Attempt to simplify equality/inequality comparisons of complex
12282 : values. Only lower the comparison if the result is known or
12283 : can be simplified to a single scalar comparison. */
12284 47676901 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12285 47674374 : || TREE_CODE (arg0) == COMPLEX_CST)
12286 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12287 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12288 : {
12289 1726 : tree real0, imag0, real1, imag1;
12290 1726 : tree rcond, icond;
12291 :
12292 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12293 : {
12294 1726 : real0 = TREE_OPERAND (arg0, 0);
12295 1726 : imag0 = TREE_OPERAND (arg0, 1);
12296 : }
12297 : else
12298 : {
12299 0 : real0 = TREE_REALPART (arg0);
12300 0 : imag0 = TREE_IMAGPART (arg0);
12301 : }
12302 :
12303 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12304 : {
12305 192 : real1 = TREE_OPERAND (arg1, 0);
12306 192 : imag1 = TREE_OPERAND (arg1, 1);
12307 : }
12308 : else
12309 : {
12310 1534 : real1 = TREE_REALPART (arg1);
12311 1534 : imag1 = TREE_IMAGPART (arg1);
12312 : }
12313 :
12314 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12315 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12316 : {
12317 11 : if (integer_zerop (rcond))
12318 : {
12319 11 : if (code == EQ_EXPR)
12320 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12321 0 : imag0, imag1);
12322 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12323 : }
12324 : else
12325 : {
12326 0 : if (code == NE_EXPR)
12327 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12328 0 : imag0, imag1);
12329 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12330 : }
12331 : }
12332 :
12333 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12334 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12335 : {
12336 9 : if (integer_zerop (icond))
12337 : {
12338 7 : if (code == EQ_EXPR)
12339 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12340 1 : real0, real1);
12341 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12342 : }
12343 : else
12344 : {
12345 2 : if (code == NE_EXPR)
12346 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12347 1 : real0, real1);
12348 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12349 : }
12350 : }
12351 : }
12352 :
12353 : return NULL_TREE;
12354 :
12355 41216812 : case LT_EXPR:
12356 41216812 : case GT_EXPR:
12357 41216812 : case LE_EXPR:
12358 41216812 : case GE_EXPR:
12359 41216812 : tem = fold_comparison (loc, code, type, op0, op1);
12360 41216812 : if (tem != NULL_TREE)
12361 : return tem;
12362 :
12363 : /* Transform comparisons of the form X +- C CMP X. */
12364 40346825 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12365 4767542 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12366 51214 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12367 40346841 : && !HONOR_SNANS (arg0))
12368 : {
12369 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12370 14 : enum tree_code code0 = TREE_CODE (arg0);
12371 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12372 :
12373 : /* (X - c) > X becomes false. */
12374 14 : if (code == GT_EXPR
12375 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12376 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12377 4 : return constant_boolean_node (0, type);
12378 :
12379 : /* Likewise (X + c) < X becomes false. */
12380 10 : if (code == LT_EXPR
12381 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12382 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12383 3 : return constant_boolean_node (0, type);
12384 :
12385 : /* Convert (X - c) <= X to true. */
12386 7 : if (!HONOR_NANS (arg1)
12387 6 : && code == LE_EXPR
12388 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12389 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12390 4 : return constant_boolean_node (1, type);
12391 :
12392 : /* Convert (X + c) >= X to true. */
12393 3 : if (!HONOR_NANS (arg1)
12394 2 : && code == GE_EXPR
12395 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12396 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12397 2 : return constant_boolean_node (1, type);
12398 : }
12399 :
12400 : /* If we are comparing an ABS_EXPR with a constant, we can
12401 : convert all the cases into explicit comparisons, but they may
12402 : well not be faster than doing the ABS and one comparison.
12403 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12404 : and a comparison, and is probably faster. */
12405 40346812 : if (code == LE_EXPR
12406 7647885 : && TREE_CODE (arg1) == INTEGER_CST
12407 5437101 : && TREE_CODE (arg0) == ABS_EXPR
12408 818 : && ! TREE_SIDE_EFFECTS (arg0)
12409 818 : && (tem = negate_expr (arg1)) != 0
12410 818 : && TREE_CODE (tem) == INTEGER_CST
12411 40347630 : && !TREE_OVERFLOW (tem))
12412 1636 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12413 : build2 (GE_EXPR, type,
12414 818 : TREE_OPERAND (arg0, 0), tem),
12415 : build2 (LE_EXPR, type,
12416 1636 : TREE_OPERAND (arg0, 0), arg1));
12417 :
12418 : /* Convert ABS_EXPR<x> >= 0 to true. */
12419 40345994 : if (code == GE_EXPR
12420 4222531 : && (integer_zerop (arg1)
12421 3075930 : || (! HONOR_NANS (arg0)
12422 2403130 : && real_zerop (arg1)))
12423 41492826 : && tree_expr_nonnegative_p (arg0))
12424 1081 : return omit_one_operand_loc (loc, type,
12425 : constant_boolean_node (true, type),
12426 1081 : arg0);
12427 :
12428 : /* Convert ABS_EXPR<x> < 0 to false. */
12429 40344913 : if (code == LT_EXPR
12430 13438055 : && (integer_zerop (arg1) || real_zerop (arg1))
12431 43630903 : && tree_expr_nonnegative_p (arg0))
12432 2507 : return omit_one_operand_loc (loc, type,
12433 : constant_boolean_node (false, type),
12434 2507 : arg0);
12435 :
12436 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12437 : and similarly for >= into !=. */
12438 40342406 : if ((code == LT_EXPR || code == GE_EXPR)
12439 17656998 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12440 5552428 : && TREE_CODE (arg1) == LSHIFT_EXPR
12441 40343909 : && integer_onep (TREE_OPERAND (arg1, 0)))
12442 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12443 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12444 1355 : TREE_OPERAND (arg1, 1)),
12445 2710 : build_zero_cst (TREE_TYPE (arg0)));
12446 :
12447 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12448 : otherwise Y might be >= # of bits in X's type and thus e.g.
12449 : (unsigned char) (1 << Y) for Y 15 might be 0.
12450 : If the cast is widening, then 1 << Y should have unsigned type,
12451 : otherwise if Y is number of bits in the signed shift type minus 1,
12452 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12453 : 31 might be 0xffffffff80000000. */
12454 40341051 : if ((code == LT_EXPR || code == GE_EXPR)
12455 17655643 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12456 5750650 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12457 11928335 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12458 4043578 : && CONVERT_EXPR_P (arg1)
12459 1124513 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12460 42 : && (element_precision (TREE_TYPE (arg1))
12461 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12462 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12463 14 : || (element_precision (TREE_TYPE (arg1))
12464 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12465 40341058 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12466 : {
12467 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12468 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12469 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12470 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12471 14 : build_zero_cst (TREE_TYPE (arg0)));
12472 : }
12473 :
12474 : return NULL_TREE;
12475 :
12476 5283577 : case UNORDERED_EXPR:
12477 5283577 : case ORDERED_EXPR:
12478 5283577 : case UNLT_EXPR:
12479 5283577 : case UNLE_EXPR:
12480 5283577 : case UNGT_EXPR:
12481 5283577 : case UNGE_EXPR:
12482 5283577 : case UNEQ_EXPR:
12483 5283577 : case LTGT_EXPR:
12484 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12485 5283577 : {
12486 5283577 : tree targ0 = strip_float_extensions (arg0);
12487 5283577 : tree targ1 = strip_float_extensions (arg1);
12488 5283577 : tree newtype = TREE_TYPE (targ0);
12489 :
12490 5283577 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12491 1289 : newtype = TREE_TYPE (targ1);
12492 :
12493 5283577 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12494 5283577 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12495 328 : return fold_build2_loc (loc, code, type,
12496 : fold_convert_loc (loc, newtype, targ0),
12497 328 : fold_convert_loc (loc, newtype, targ1));
12498 : }
12499 :
12500 : return NULL_TREE;
12501 :
12502 8663515 : case COMPOUND_EXPR:
12503 : /* When pedantic, a compound expression can be neither an lvalue
12504 : nor an integer constant expression. */
12505 8663515 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12506 : return NULL_TREE;
12507 : /* Don't let (0, 0) be null pointer constant. */
12508 512383 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12509 512383 : : fold_convert_loc (loc, type, arg1);
12510 : return tem;
12511 :
12512 : default:
12513 : return NULL_TREE;
12514 : } /* switch (code) */
12515 : }
12516 :
12517 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12518 : ((A & N) + B) & M -> (A + B) & M
12519 : Similarly if (N & M) == 0,
12520 : ((A | N) + B) & M -> (A + B) & M
12521 : and for - instead of + (or unary - instead of +)
12522 : and/or ^ instead of |.
12523 : If B is constant and (B & M) == 0, fold into A & M.
12524 :
12525 : This function is a helper for match.pd patterns. Return non-NULL
12526 : type in which the simplified operation should be performed only
12527 : if any optimization is possible.
12528 :
12529 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12530 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12531 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12532 : +/-. */
12533 : tree
12534 1268805 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12535 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12536 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12537 : tree *pmop)
12538 : {
12539 1268805 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12540 1268805 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12541 1268805 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12542 2537610 : if (~cst1 == 0
12543 3800801 : || (cst1 & (cst1 + 1)) != 0
12544 1058376 : || !INTEGRAL_TYPE_P (type)
12545 1058376 : || (!TYPE_OVERFLOW_WRAPS (type)
12546 41818 : && TREE_CODE (type) != INTEGER_TYPE)
12547 4651555 : || (wi::max_value (type) & cst1) != cst1)
12548 : return NULL_TREE;
12549 :
12550 1058376 : enum tree_code codes[2] = { code00, code01 };
12551 1058376 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12552 1058376 : int which = 0;
12553 1058376 : wide_int cst0;
12554 :
12555 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12556 : arg1 (M) is == (1LL << cst) - 1.
12557 : Store C into PMOP[0] and D into PMOP[1]. */
12558 1058376 : pmop[0] = arg00;
12559 1058376 : pmop[1] = arg01;
12560 1058376 : which = code != NEGATE_EXPR;
12561 :
12562 3174212 : for (; which >= 0; which--)
12563 2115836 : switch (codes[which])
12564 : {
12565 20705 : case BIT_AND_EXPR:
12566 20705 : case BIT_IOR_EXPR:
12567 20705 : case BIT_XOR_EXPR:
12568 20705 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12569 20705 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12570 20705 : if (codes[which] == BIT_AND_EXPR)
12571 : {
12572 20593 : if (cst0 != cst1)
12573 : break;
12574 : }
12575 112 : else if (cst0 != 0)
12576 : break;
12577 : /* If C or D is of the form (A & N) where
12578 : (N & M) == M, or of the form (A | N) or
12579 : (A ^ N) where (N & M) == 0, replace it with A. */
12580 19166 : pmop[which] = arg0xx[2 * which];
12581 19166 : break;
12582 2095131 : case ERROR_MARK:
12583 2095131 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12584 : break;
12585 : /* If C or D is a N where (N & M) == 0, it can be
12586 : omitted (replaced with 0). */
12587 880714 : if ((code == PLUS_EXPR
12588 214134 : || (code == MINUS_EXPR && which == 0))
12589 654491 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12590 134805 : pmop[which] = build_int_cst (type, 0);
12591 : /* Similarly, with C - N where (-N & M) == 0. */
12592 880714 : if (code == MINUS_EXPR
12593 440357 : && which == 1
12594 647506 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12595 199144 : pmop[which] = build_int_cst (type, 0);
12596 : break;
12597 0 : default:
12598 0 : gcc_unreachable ();
12599 : }
12600 :
12601 : /* Only build anything new if we optimized one or both arguments above. */
12602 1058376 : if (pmop[0] == arg00 && pmop[1] == arg01)
12603 : return NULL_TREE;
12604 :
12605 352376 : if (TYPE_OVERFLOW_WRAPS (type))
12606 : return type;
12607 : else
12608 2370 : return unsigned_type_for (type);
12609 1058376 : }
12610 :
12611 : /* Used by contains_label_[p1]. */
12612 :
12613 : struct contains_label_data
12614 : {
12615 : hash_set<tree> *pset;
12616 : bool inside_switch_p;
12617 : };
12618 :
12619 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12620 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12621 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12622 :
12623 : static tree
12624 4491759 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12625 : {
12626 4491759 : contains_label_data *d = (contains_label_data *) data;
12627 4491759 : switch (TREE_CODE (*tp))
12628 : {
12629 : case LABEL_EXPR:
12630 : return *tp;
12631 :
12632 0 : case CASE_LABEL_EXPR:
12633 0 : if (!d->inside_switch_p)
12634 : return *tp;
12635 : return NULL_TREE;
12636 :
12637 0 : case SWITCH_EXPR:
12638 0 : if (!d->inside_switch_p)
12639 : {
12640 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12641 0 : return *tp;
12642 0 : d->inside_switch_p = true;
12643 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12644 0 : return *tp;
12645 0 : d->inside_switch_p = false;
12646 0 : *walk_subtrees = 0;
12647 : }
12648 : return NULL_TREE;
12649 :
12650 6597 : case GOTO_EXPR:
12651 6597 : *walk_subtrees = 0;
12652 6597 : return NULL_TREE;
12653 :
12654 : default:
12655 : return NULL_TREE;
12656 : }
12657 : }
12658 :
12659 : /* Return whether the sub-tree ST contains a label which is accessible from
12660 : outside the sub-tree. */
12661 :
12662 : static bool
12663 323912 : contains_label_p (tree st)
12664 : {
12665 323912 : hash_set<tree> pset;
12666 323912 : contains_label_data data = { &pset, false };
12667 323912 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12668 323912 : }
12669 :
12670 : /* Fold a ternary expression of code CODE and type TYPE with operands
12671 : OP0, OP1, and OP2. Return the folded expression if folding is
12672 : successful. Otherwise, return NULL_TREE. */
12673 :
12674 : tree
12675 45373197 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12676 : tree op0, tree op1, tree op2)
12677 : {
12678 45373197 : tree tem;
12679 45373197 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12680 45373197 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12681 :
12682 45373197 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12683 : && TREE_CODE_LENGTH (code) == 3);
12684 :
12685 : /* If this is a commutative operation, and OP0 is a constant, move it
12686 : to OP1 to reduce the number of tests below. */
12687 45373197 : if (commutative_ternary_tree_code (code)
12688 45373197 : && tree_swap_operands_p (op0, op1))
12689 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12690 :
12691 45373164 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12692 45373164 : if (tem)
12693 : return tem;
12694 :
12695 : /* Strip any conversions that don't change the mode. This is safe
12696 : for every expression, except for a comparison expression because
12697 : its signedness is derived from its operands. So, in the latter
12698 : case, only strip conversions that don't change the signedness.
12699 :
12700 : Note that this is done as an internal manipulation within the
12701 : constant folder, in order to find the simplest representation of
12702 : the arguments so that their form can be studied. In any cases,
12703 : the appropriate type conversions should be put back in the tree
12704 : that will get out of the constant folder. */
12705 44317366 : if (op0)
12706 : {
12707 44250773 : arg0 = op0;
12708 44250773 : STRIP_NOPS (arg0);
12709 : }
12710 :
12711 44317366 : if (op1)
12712 : {
12713 44317366 : arg1 = op1;
12714 44317366 : STRIP_NOPS (arg1);
12715 : }
12716 :
12717 44317366 : if (op2)
12718 : {
12719 14976799 : arg2 = op2;
12720 14976799 : STRIP_NOPS (arg2);
12721 : }
12722 :
12723 44317366 : switch (code)
12724 : {
12725 29340092 : case COMPONENT_REF:
12726 29340092 : if (TREE_CODE (arg0) == CONSTRUCTOR
12727 29340092 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12728 : {
12729 : unsigned HOST_WIDE_INT idx;
12730 : tree field, value;
12731 884 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12732 679 : if (field == arg1)
12733 : return value;
12734 : }
12735 : return NULL_TREE;
12736 :
12737 12528536 : case COND_EXPR:
12738 12528536 : case VEC_COND_EXPR:
12739 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12740 : so all simple results must be passed through pedantic_non_lvalue. */
12741 12528536 : if (TREE_CODE (arg0) == INTEGER_CST)
12742 : {
12743 456503 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12744 456503 : tem = integer_zerop (arg0) ? op2 : op1;
12745 : /* Only optimize constant conditions when the selected branch
12746 : has the same type as the COND_EXPR. This avoids optimizing
12747 : away "c ? x : throw", where the throw has a void type.
12748 : Avoid throwing away that operand which contains label. */
12749 456503 : if ((!TREE_SIDE_EFFECTS (unused_op)
12750 323912 : || !contains_label_p (unused_op))
12751 775594 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12752 372764 : || VOID_TYPE_P (type)))
12753 442476 : return protected_set_expr_location_unshare (tem, loc);
12754 14027 : return NULL_TREE;
12755 : }
12756 12072033 : else if (TREE_CODE (arg0) == VECTOR_CST)
12757 : {
12758 11695 : unsigned HOST_WIDE_INT nelts;
12759 11695 : if ((TREE_CODE (arg1) == VECTOR_CST
12760 9037 : || TREE_CODE (arg1) == CONSTRUCTOR)
12761 2658 : && (TREE_CODE (arg2) == VECTOR_CST
12762 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12763 23390 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12764 : {
12765 2658 : vec_perm_builder sel (nelts, nelts, 1);
12766 26442 : for (unsigned int i = 0; i < nelts; i++)
12767 : {
12768 23784 : tree val = VECTOR_CST_ELT (arg0, i);
12769 23784 : if (integer_all_onesp (val))
12770 11773 : sel.quick_push (i);
12771 12011 : else if (integer_zerop (val))
12772 12011 : sel.quick_push (nelts + i);
12773 : else /* Currently unreachable. */
12774 1966 : return NULL_TREE;
12775 : }
12776 2658 : vec_perm_indices indices (sel, 2, nelts);
12777 2658 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12778 2658 : if (t != NULL_TREE)
12779 1966 : return t;
12780 4624 : }
12781 : }
12782 :
12783 : /* If we have A op B ? A : C, we may be able to convert this to a
12784 : simpler expression, depending on the operation and the values
12785 : of B and C. Signed zeros prevent all of these transformations,
12786 : for reasons given above each one.
12787 :
12788 : Also try swapping the arguments and inverting the conditional. */
12789 12070067 : if (COMPARISON_CLASS_P (arg0)
12790 9924354 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12791 12209557 : && !HONOR_SIGNED_ZEROS (op1))
12792 : {
12793 128746 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12794 128746 : TREE_OPERAND (arg0, 0),
12795 128746 : TREE_OPERAND (arg0, 1),
12796 : op1, op2);
12797 128746 : if (tem)
12798 : return tem;
12799 : }
12800 :
12801 12063366 : if (COMPARISON_CLASS_P (arg0)
12802 9917653 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12803 12532324 : && !HONOR_SIGNED_ZEROS (op2))
12804 : {
12805 382858 : enum tree_code comp_code = TREE_CODE (arg0);
12806 382858 : tree arg00 = TREE_OPERAND (arg0, 0);
12807 382858 : tree arg01 = TREE_OPERAND (arg0, 1);
12808 382858 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12809 382858 : if (comp_code != ERROR_MARK)
12810 382858 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12811 : arg00,
12812 : arg01,
12813 : op2, op1);
12814 382858 : if (tem)
12815 : return tem;
12816 : }
12817 :
12818 : /* If the second operand is simpler than the third, swap them
12819 : since that produces better jump optimization results. */
12820 11799043 : if (truth_value_p (TREE_CODE (arg0))
12821 11799043 : && tree_swap_operands_p (op1, op2))
12822 : {
12823 2049398 : location_t loc0 = expr_location_or (arg0, loc);
12824 : /* See if this can be inverted. If it can't, possibly because
12825 : it was a floating-point inequality comparison, don't do
12826 : anything. */
12827 2049398 : tem = fold_invert_truthvalue (loc0, arg0);
12828 2049398 : if (tem)
12829 1304496 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12830 : }
12831 :
12832 : /* Convert A ? 1 : 0 to simply A. */
12833 10494547 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12834 10042694 : : (integer_onep (op1)
12835 411181 : && !VECTOR_TYPE_P (type)))
12836 689702 : && integer_zerop (op2)
12837 : /* If we try to convert OP0 to our type, the
12838 : call to fold will try to move the conversion inside
12839 : a COND, which will recurse. In that case, the COND_EXPR
12840 : is probably the best choice, so leave it alone. */
12841 11622373 : && type == TREE_TYPE (arg0))
12842 32722 : return protected_set_expr_location_unshare (arg0, loc);
12843 :
12844 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12845 : over COND_EXPR in cases such as floating point comparisons. */
12846 10461825 : if (integer_zerop (op1)
12847 391705 : && code == COND_EXPR
12848 367335 : && integer_onep (op2)
12849 31407 : && !VECTOR_TYPE_P (type)
12850 10493232 : && truth_value_p (TREE_CODE (arg0)))
12851 29852 : return fold_convert_loc (loc, type,
12852 29852 : invert_truthvalue_loc (loc, arg0));
12853 :
12854 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12855 10431973 : if (TREE_CODE (arg0) == LT_EXPR
12856 1372682 : && integer_zerop (TREE_OPERAND (arg0, 1))
12857 37578 : && integer_zerop (op2)
12858 10432943 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12859 : {
12860 : /* sign_bit_p looks through both zero and sign extensions,
12861 : but for this optimization only sign extensions are
12862 : usable. */
12863 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12864 56 : while (tem != tem2)
12865 : {
12866 0 : if (TREE_CODE (tem2) != NOP_EXPR
12867 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12868 : {
12869 : tem = NULL_TREE;
12870 : break;
12871 : }
12872 0 : tem2 = TREE_OPERAND (tem2, 0);
12873 : }
12874 : /* sign_bit_p only checks ARG1 bits within A's precision.
12875 : If <sign bit of A> has wider type than A, bits outside
12876 : of A's precision in <sign bit of A> need to be checked.
12877 : If they are all 0, this optimization needs to be done
12878 : in unsigned A's type, if they are all 1 in signed A's type,
12879 : otherwise this can't be done. */
12880 56 : if (tem
12881 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12882 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12883 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12884 56 : < TYPE_PRECISION (type))
12885 : {
12886 56 : int inner_width, outer_width;
12887 56 : tree tem_type;
12888 :
12889 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12890 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12891 56 : if (outer_width > TYPE_PRECISION (type))
12892 0 : outer_width = TYPE_PRECISION (type);
12893 :
12894 56 : wide_int mask = wi::shifted_mask
12895 56 : (inner_width, outer_width - inner_width, false,
12896 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
12897 :
12898 56 : wide_int common = mask & wi::to_wide (arg1);
12899 56 : if (common == mask)
12900 : {
12901 28 : tem_type = signed_type_for (TREE_TYPE (tem));
12902 28 : tem = fold_convert_loc (loc, tem_type, tem);
12903 : }
12904 28 : else if (common == 0)
12905 : {
12906 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
12907 0 : tem = fold_convert_loc (loc, tem_type, tem);
12908 : }
12909 : else
12910 : tem = NULL;
12911 56 : }
12912 :
12913 56 : if (tem)
12914 28 : return
12915 56 : fold_convert_loc (loc, type,
12916 : fold_build2_loc (loc, BIT_AND_EXPR,
12917 28 : TREE_TYPE (tem), tem,
12918 : fold_convert_loc (loc,
12919 28 : TREE_TYPE (tem),
12920 28 : arg1)));
12921 : }
12922 :
12923 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12924 : already handled above. */
12925 10431945 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12926 347 : && integer_onep (TREE_OPERAND (arg0, 1))
12927 3 : && integer_zerop (op2)
12928 10431945 : && integer_pow2p (arg1))
12929 : {
12930 0 : tree tem = TREE_OPERAND (arg0, 0);
12931 0 : STRIP_NOPS (tem);
12932 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
12933 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12934 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
12935 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
12936 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
12937 : fold_convert_loc (loc, type,
12938 0 : TREE_OPERAND (tem, 0)),
12939 0 : op1);
12940 : }
12941 :
12942 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
12943 : is probably obsolete because the first operand should be a
12944 : truth value (that's why we have the two cases above), but let's
12945 : leave it in until we can confirm this for all front-ends. */
12946 10431945 : if (integer_zerop (op2)
12947 2026541 : && TREE_CODE (arg0) == NE_EXPR
12948 530961 : && integer_zerop (TREE_OPERAND (arg0, 1))
12949 281169 : && integer_pow2p (arg1)
12950 32557 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12951 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12952 : arg1, OEP_ONLY_CONST)
12953 : /* operand_equal_p compares just value, not precision, so e.g.
12954 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
12955 : second operand 32-bit -128, which is not a power of two (or vice
12956 : versa. */
12957 10431945 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
12958 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12959 :
12960 : /* Disable the transformations below for vectors, since
12961 : fold_binary_op_with_conditional_arg may undo them immediately,
12962 : yielding an infinite loop. */
12963 10431945 : if (code == VEC_COND_EXPR)
12964 : return NULL_TREE;
12965 :
12966 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
12967 9980092 : if (integer_zerop (op2)
12968 1670991 : && truth_value_p (TREE_CODE (arg0))
12969 1478669 : && truth_value_p (TREE_CODE (arg1))
12970 10013477 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12971 33385 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12972 : : TRUTH_ANDIF_EXPR,
12973 33385 : type, fold_convert_loc (loc, type, arg0), op1);
12974 :
12975 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12976 9946707 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12977 450649 : && truth_value_p (TREE_CODE (arg0))
12978 303799 : && truth_value_p (TREE_CODE (arg1))
12979 9983967 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12980 : {
12981 37260 : location_t loc0 = expr_location_or (arg0, loc);
12982 : /* Only perform transformation if ARG0 is easily inverted. */
12983 37260 : tem = fold_invert_truthvalue (loc0, arg0);
12984 37260 : if (tem)
12985 36996 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12986 : ? BIT_IOR_EXPR
12987 : : TRUTH_ORIF_EXPR,
12988 : type, fold_convert_loc (loc, type, tem),
12989 36996 : op1);
12990 : }
12991 :
12992 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12993 9909711 : if (integer_zerop (arg1)
12994 337560 : && truth_value_p (TREE_CODE (arg0))
12995 84031 : && truth_value_p (TREE_CODE (op2))
12996 9909739 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12997 : {
12998 28 : location_t loc0 = expr_location_or (arg0, loc);
12999 : /* Only perform transformation if ARG0 is easily inverted. */
13000 28 : tem = fold_invert_truthvalue (loc0, arg0);
13001 28 : if (tem)
13002 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13003 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13004 : type, fold_convert_loc (loc, type, tem),
13005 0 : op2);
13006 : }
13007 :
13008 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13009 9909711 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13010 378459 : && truth_value_p (TREE_CODE (arg0))
13011 269889 : && truth_value_p (TREE_CODE (op2))
13012 9909897 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13013 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13014 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13015 186 : type, fold_convert_loc (loc, type, arg0), op2);
13016 :
13017 : return NULL_TREE;
13018 :
13019 0 : case CALL_EXPR:
13020 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13021 : of fold_ternary on them. */
13022 0 : gcc_unreachable ();
13023 :
13024 915932 : case BIT_FIELD_REF:
13025 915932 : if (TREE_CODE (arg0) == VECTOR_CST
13026 79196 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13027 42756 : || (VECTOR_TYPE_P (type)
13028 41854 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13029 78262 : && tree_fits_uhwi_p (op1)
13030 994194 : && tree_fits_uhwi_p (op2))
13031 : {
13032 78262 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13033 78262 : unsigned HOST_WIDE_INT width
13034 78262 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13035 78262 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13036 78262 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13037 78262 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13038 :
13039 78262 : if (n != 0
13040 78262 : && (idx % width) == 0
13041 78262 : && (n % width) == 0
13042 156524 : && known_le ((idx + n) / width,
13043 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13044 : {
13045 78262 : idx = idx / width;
13046 78262 : n = n / width;
13047 :
13048 78262 : if (TREE_CODE (arg0) == VECTOR_CST)
13049 : {
13050 78262 : if (n == 1)
13051 : {
13052 36444 : tem = VECTOR_CST_ELT (arg0, idx);
13053 36444 : if (VECTOR_TYPE_P (type))
13054 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13055 36444 : return tem;
13056 : }
13057 :
13058 41818 : tree_vector_builder vals (type, n, 1);
13059 185162 : for (unsigned i = 0; i < n; ++i)
13060 143344 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13061 41818 : return vals.build ();
13062 41818 : }
13063 : }
13064 : }
13065 :
13066 : /* On constants we can use native encode/interpret to constant
13067 : fold (nearly) all BIT_FIELD_REFs. */
13068 837670 : if (CONSTANT_CLASS_P (arg0)
13069 1735 : && can_native_interpret_type_p (type)
13070 : && BITS_PER_UNIT == 8
13071 1735 : && tree_fits_uhwi_p (op1)
13072 839405 : && tree_fits_uhwi_p (op2))
13073 : {
13074 1735 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13075 1735 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13076 : /* Limit us to a reasonable amount of work. To relax the
13077 : other limitations we need bit-shifting of the buffer
13078 : and rounding up the size. */
13079 1735 : if (bitpos % BITS_PER_UNIT == 0
13080 1735 : && bitsize % BITS_PER_UNIT == 0
13081 1735 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13082 : {
13083 1735 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13084 1735 : unsigned HOST_WIDE_INT len
13085 1735 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13086 1735 : bitpos / BITS_PER_UNIT);
13087 1735 : if (len > 0
13088 1735 : && len * BITS_PER_UNIT >= bitsize)
13089 : {
13090 1735 : tree v = native_interpret_expr (type, b,
13091 : bitsize / BITS_PER_UNIT);
13092 1735 : if (v)
13093 1681 : return v;
13094 : }
13095 : }
13096 : }
13097 :
13098 : return NULL_TREE;
13099 :
13100 780180 : case VEC_PERM_EXPR:
13101 : /* Perform constant folding of BIT_INSERT_EXPR. */
13102 780180 : if (TREE_CODE (arg2) == VECTOR_CST
13103 768706 : && TREE_CODE (op0) == VECTOR_CST
13104 15422 : && TREE_CODE (op1) == VECTOR_CST)
13105 : {
13106 : /* Build a vector of integers from the tree mask. */
13107 3951 : vec_perm_builder builder;
13108 3951 : if (!tree_to_vec_perm_builder (&builder, arg2))
13109 : return NULL_TREE;
13110 :
13111 : /* Create a vec_perm_indices for the integer vector. */
13112 3951 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13113 3951 : bool single_arg = (op0 == op1);
13114 7902 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13115 3951 : return fold_vec_perm (type, op0, op1, sel);
13116 7902 : }
13117 : return NULL_TREE;
13118 :
13119 15158 : case BIT_INSERT_EXPR:
13120 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13121 15158 : if (TREE_CODE (arg0) == INTEGER_CST
13122 14 : && TREE_CODE (arg1) == INTEGER_CST)
13123 : {
13124 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13125 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13126 2 : if (BYTES_BIG_ENDIAN)
13127 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13128 2 : wide_int tem = (wi::to_wide (arg0)
13129 4 : & wi::shifted_mask (bitpos, bitsize, true,
13130 4 : TYPE_PRECISION (type)));
13131 2 : wide_int tem2
13132 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13133 2 : bitsize), bitpos);
13134 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13135 2 : }
13136 15156 : else if (TREE_CODE (arg0) == VECTOR_CST
13137 906 : && CONSTANT_CLASS_P (arg1)
13138 15458 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13139 302 : TREE_TYPE (arg1)))
13140 : {
13141 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13142 302 : unsigned HOST_WIDE_INT elsize
13143 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13144 302 : if (bitpos % elsize == 0)
13145 : {
13146 302 : unsigned k = bitpos / elsize;
13147 302 : unsigned HOST_WIDE_INT nelts;
13148 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13149 45373197 : return arg0;
13150 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13151 : {
13152 290 : tree_vector_builder elts (type, nelts, 1);
13153 290 : elts.quick_grow (nelts);
13154 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13155 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13156 290 : return elts.build ();
13157 290 : }
13158 : }
13159 : }
13160 : return NULL_TREE;
13161 :
13162 : default:
13163 : return NULL_TREE;
13164 : } /* switch (code) */
13165 : }
13166 :
13167 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13168 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13169 : constructor element index of the value returned. If the element is
13170 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13171 : the index of the element after the ACCESS_INDEX position (which
13172 : may be outside of the CTOR array). */
13173 :
13174 : tree
13175 686158 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13176 : unsigned *ctor_idx)
13177 : {
13178 686158 : tree index_type = NULL_TREE;
13179 686158 : signop index_sgn = UNSIGNED;
13180 686158 : offset_int low_bound = 0;
13181 :
13182 686158 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13183 : {
13184 686158 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13185 686158 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13186 : {
13187 : /* Static constructors for variably sized objects makes no sense. */
13188 686158 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13189 686158 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13190 : /* ??? When it is obvious that the range is signed, treat it so. */
13191 686158 : if (TYPE_UNSIGNED (index_type)
13192 350661 : && TYPE_MAX_VALUE (domain_type)
13193 1036788 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13194 350630 : TYPE_MIN_VALUE (domain_type)))
13195 : {
13196 0 : index_sgn = SIGNED;
13197 0 : low_bound
13198 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13199 : SIGNED);
13200 : }
13201 : else
13202 : {
13203 686158 : index_sgn = TYPE_SIGN (index_type);
13204 686158 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13205 : }
13206 : }
13207 : }
13208 :
13209 686158 : if (index_type)
13210 686158 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13211 : index_sgn);
13212 :
13213 686158 : offset_int index = low_bound;
13214 686158 : if (index_type)
13215 686158 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13216 :
13217 686158 : offset_int max_index = index;
13218 686158 : unsigned cnt;
13219 686158 : tree cfield, cval;
13220 686158 : bool first_p = true;
13221 :
13222 13883156 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13223 : {
13224 : /* Array constructor might explicitly set index, or specify a range,
13225 : or leave index NULL meaning that it is next index after previous
13226 : one. */
13227 13882025 : if (cfield)
13228 : {
13229 5836965 : if (TREE_CODE (cfield) == INTEGER_CST)
13230 11672484 : max_index = index
13231 5836242 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13232 : else
13233 : {
13234 723 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13235 723 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13236 : index_sgn);
13237 723 : max_index
13238 723 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13239 : index_sgn);
13240 723 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13241 : }
13242 : }
13243 8045060 : else if (!first_p)
13244 : {
13245 7838651 : index = max_index + 1;
13246 7838651 : if (index_type)
13247 7838651 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13248 7838651 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13249 7838651 : max_index = index;
13250 : }
13251 : else
13252 : first_p = false;
13253 :
13254 13882025 : if (TREE_CODE (cval) == RAW_DATA_CST)
13255 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13256 :
13257 : /* Do we have match? */
13258 13882025 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13259 : {
13260 13881737 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13261 : {
13262 684911 : if (ctor_idx)
13263 684911 : *ctor_idx = cnt;
13264 684911 : return cval;
13265 : }
13266 : }
13267 288 : else if (in_gimple_form)
13268 : /* We're past the element we search for. Note during parsing
13269 : the elements might not be sorted.
13270 : ??? We should use a binary search and a flag on the
13271 : CONSTRUCTOR as to whether elements are sorted in declaration
13272 : order. */
13273 : break;
13274 : }
13275 1247 : if (ctor_idx)
13276 1247 : *ctor_idx = cnt;
13277 : return NULL_TREE;
13278 : }
13279 :
13280 : /* Perform constant folding and related simplification of EXPR.
13281 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13282 : and application of the associative law.
13283 : NOP_EXPR conversions may be removed freely (as long as we
13284 : are careful not to change the type of the overall expression).
13285 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13286 : but we can constant-fold them if they have constant operands. */
13287 :
13288 : #ifdef ENABLE_FOLD_CHECKING
13289 : # define fold(x) fold_1 (x)
13290 : static tree fold_1 (tree);
13291 : static
13292 : #endif
13293 : tree
13294 1369675084 : fold (tree expr)
13295 : {
13296 1369850626 : const tree t = expr;
13297 1369850626 : enum tree_code code = TREE_CODE (t);
13298 1369850626 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13299 1369850626 : tree tem;
13300 1369850626 : location_t loc = EXPR_LOCATION (expr);
13301 :
13302 : /* Return right away if a constant. */
13303 1369850626 : if (kind == tcc_constant)
13304 : return t;
13305 :
13306 : /* CALL_EXPR-like objects with variable numbers of operands are
13307 : treated specially. */
13308 1264471179 : if (kind == tcc_vl_exp)
13309 : {
13310 179228870 : if (code == CALL_EXPR)
13311 : {
13312 179228342 : tem = fold_call_expr (loc, expr, false);
13313 355736077 : return tem ? tem : expr;
13314 : }
13315 : return expr;
13316 : }
13317 :
13318 1085242309 : if (IS_EXPR_CODE_CLASS (kind))
13319 : {
13320 1082981219 : tree type = TREE_TYPE (t);
13321 1082981219 : tree op0, op1, op2;
13322 :
13323 1082981219 : switch (TREE_CODE_LENGTH (code))
13324 : {
13325 982575421 : case 1:
13326 982575421 : op0 = TREE_OPERAND (t, 0);
13327 982575421 : tem = fold_unary_loc (loc, code, type, op0);
13328 1678504320 : return tem ? tem : expr;
13329 91219054 : case 2:
13330 91219054 : op0 = TREE_OPERAND (t, 0);
13331 91219054 : op1 = TREE_OPERAND (t, 1);
13332 91219054 : tem = fold_binary_loc (loc, code, type, op0, op1);
13333 171749560 : return tem ? tem : expr;
13334 4413911 : case 3:
13335 4413911 : op0 = TREE_OPERAND (t, 0);
13336 4413911 : op1 = TREE_OPERAND (t, 1);
13337 4413911 : op2 = TREE_OPERAND (t, 2);
13338 4413911 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13339 8536893 : return tem ? tem : expr;
13340 : default:
13341 : break;
13342 : }
13343 : }
13344 :
13345 7033923 : switch (code)
13346 : {
13347 4668886 : case ARRAY_REF:
13348 4668886 : {
13349 4668886 : tree op0 = TREE_OPERAND (t, 0);
13350 4668886 : tree op1 = TREE_OPERAND (t, 1);
13351 :
13352 4668886 : if (TREE_CODE (op1) == INTEGER_CST
13353 2921377 : && TREE_CODE (op0) == CONSTRUCTOR
13354 4670341 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13355 : {
13356 1455 : unsigned int idx;
13357 1455 : tree val
13358 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13359 : &idx);
13360 1455 : if (val)
13361 : {
13362 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13363 : return val;
13364 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13365 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13366 : != INTEGER_CST))
13367 : return t;
13368 2 : offset_int o
13369 2 : = (wi::to_offset (op1)
13370 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13371 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13372 2 : return build_int_cst (TREE_TYPE (val),
13373 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13374 : }
13375 : }
13376 :
13377 : return t;
13378 : }
13379 :
13380 : /* Return a VECTOR_CST if possible. */
13381 206038 : case CONSTRUCTOR:
13382 206038 : {
13383 206038 : tree type = TREE_TYPE (t);
13384 206038 : if (TREE_CODE (type) != VECTOR_TYPE)
13385 : return t;
13386 :
13387 : unsigned i;
13388 : tree val;
13389 361133 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13390 312531 : if (! CONSTANT_CLASS_P (val))
13391 : return t;
13392 :
13393 48602 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13394 : }
13395 :
13396 175542 : case CONST_DECL:
13397 175542 : return fold (DECL_INITIAL (t));
13398 :
13399 : default:
13400 : return t;
13401 : } /* switch (code) */
13402 : }
13403 :
13404 : #ifdef ENABLE_FOLD_CHECKING
13405 : #undef fold
13406 :
13407 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13408 : hash_table<nofree_ptr_hash<const tree_node> > *);
13409 : static void fold_check_failed (const_tree, const_tree);
13410 : void print_fold_checksum (const_tree);
13411 :
13412 : /* When --enable-checking=fold, compute a digest of expr before
13413 : and after actual fold call to see if fold did not accidentally
13414 : change original expr. */
13415 :
13416 : tree
13417 : fold (tree expr)
13418 : {
13419 : tree ret;
13420 : struct md5_ctx ctx;
13421 : unsigned char checksum_before[16], checksum_after[16];
13422 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13423 :
13424 : md5_init_ctx (&ctx);
13425 : fold_checksum_tree (expr, &ctx, &ht);
13426 : md5_finish_ctx (&ctx, checksum_before);
13427 : ht.empty ();
13428 :
13429 : ret = fold_1 (expr);
13430 :
13431 : md5_init_ctx (&ctx);
13432 : fold_checksum_tree (expr, &ctx, &ht);
13433 : md5_finish_ctx (&ctx, checksum_after);
13434 :
13435 : if (memcmp (checksum_before, checksum_after, 16))
13436 : fold_check_failed (expr, ret);
13437 :
13438 : return ret;
13439 : }
13440 :
13441 : void
13442 : print_fold_checksum (const_tree expr)
13443 : {
13444 : struct md5_ctx ctx;
13445 : unsigned char checksum[16], cnt;
13446 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13447 :
13448 : md5_init_ctx (&ctx);
13449 : fold_checksum_tree (expr, &ctx, &ht);
13450 : md5_finish_ctx (&ctx, checksum);
13451 : for (cnt = 0; cnt < 16; ++cnt)
13452 : fprintf (stderr, "%02x", checksum[cnt]);
13453 : putc ('\n', stderr);
13454 : }
13455 :
13456 : static void
13457 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13458 : {
13459 : internal_error ("fold check: original tree changed by fold");
13460 : }
13461 :
13462 : static void
13463 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13464 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13465 : {
13466 : const tree_node **slot;
13467 : enum tree_code code;
13468 : union tree_node *buf;
13469 : int i, len;
13470 :
13471 : recursive_label:
13472 : if (expr == NULL)
13473 : return;
13474 : slot = ht->find_slot (expr, INSERT);
13475 : if (*slot != NULL)
13476 : return;
13477 : *slot = expr;
13478 : code = TREE_CODE (expr);
13479 : if (TREE_CODE_CLASS (code) == tcc_declaration
13480 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13481 : {
13482 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13483 : size_t sz = tree_size (expr);
13484 : buf = XALLOCAVAR (union tree_node, sz);
13485 : memcpy ((char *) buf, expr, sz);
13486 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13487 : buf->decl_with_vis.symtab_node = NULL;
13488 : buf->base.nowarning_flag = 0;
13489 : expr = (tree) buf;
13490 : }
13491 : else if (TREE_CODE_CLASS (code) == tcc_type
13492 : && (TYPE_POINTER_TO (expr)
13493 : || TYPE_REFERENCE_TO (expr)
13494 : || TYPE_CACHED_VALUES_P (expr)
13495 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13496 : || TYPE_NEXT_VARIANT (expr)
13497 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13498 : {
13499 : /* Allow these fields to be modified. */
13500 : tree tmp;
13501 : size_t sz = tree_size (expr);
13502 : buf = XALLOCAVAR (union tree_node, sz);
13503 : memcpy ((char *) buf, expr, sz);
13504 : expr = tmp = (tree) buf;
13505 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13506 : TYPE_POINTER_TO (tmp) = NULL;
13507 : TYPE_REFERENCE_TO (tmp) = NULL;
13508 : TYPE_NEXT_VARIANT (tmp) = NULL;
13509 : TYPE_ALIAS_SET (tmp) = -1;
13510 : if (TYPE_CACHED_VALUES_P (tmp))
13511 : {
13512 : TYPE_CACHED_VALUES_P (tmp) = 0;
13513 : TYPE_CACHED_VALUES (tmp) = NULL;
13514 : }
13515 : }
13516 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13517 : {
13518 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13519 : that and change builtins.cc etc. instead - see PR89543. */
13520 : size_t sz = tree_size (expr);
13521 : buf = XALLOCAVAR (union tree_node, sz);
13522 : memcpy ((char *) buf, expr, sz);
13523 : buf->base.nowarning_flag = 0;
13524 : expr = (tree) buf;
13525 : }
13526 : md5_process_bytes (expr, tree_size (expr), ctx);
13527 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13528 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13529 : if (TREE_CODE_CLASS (code) != tcc_type
13530 : && TREE_CODE_CLASS (code) != tcc_declaration
13531 : && code != TREE_LIST
13532 : && code != SSA_NAME
13533 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13534 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13535 : switch (TREE_CODE_CLASS (code))
13536 : {
13537 : case tcc_constant:
13538 : switch (code)
13539 : {
13540 : case STRING_CST:
13541 : md5_process_bytes (TREE_STRING_POINTER (expr),
13542 : TREE_STRING_LENGTH (expr), ctx);
13543 : break;
13544 : case COMPLEX_CST:
13545 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13546 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13547 : break;
13548 : case VECTOR_CST:
13549 : len = vector_cst_encoded_nelts (expr);
13550 : for (i = 0; i < len; ++i)
13551 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13552 : break;
13553 : default:
13554 : break;
13555 : }
13556 : break;
13557 : case tcc_exceptional:
13558 : switch (code)
13559 : {
13560 : case TREE_LIST:
13561 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13562 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13563 : expr = TREE_CHAIN (expr);
13564 : goto recursive_label;
13565 : break;
13566 : case TREE_VEC:
13567 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13568 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13569 : break;
13570 : default:
13571 : break;
13572 : }
13573 : break;
13574 : case tcc_expression:
13575 : case tcc_reference:
13576 : case tcc_comparison:
13577 : case tcc_unary:
13578 : case tcc_binary:
13579 : case tcc_statement:
13580 : case tcc_vl_exp:
13581 : len = TREE_OPERAND_LENGTH (expr);
13582 : for (i = 0; i < len; ++i)
13583 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13584 : break;
13585 : case tcc_declaration:
13586 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13587 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13588 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13589 : {
13590 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13591 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13592 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13593 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13594 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13595 : }
13596 :
13597 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13598 : {
13599 : if (TREE_CODE (expr) == FUNCTION_DECL)
13600 : {
13601 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13602 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13603 : }
13604 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13605 : }
13606 : break;
13607 : case tcc_type:
13608 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13609 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13610 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13611 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13612 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13613 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13614 : if (INTEGRAL_TYPE_P (expr)
13615 : || SCALAR_FLOAT_TYPE_P (expr))
13616 : {
13617 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13618 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13619 : }
13620 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13621 : if (RECORD_OR_UNION_TYPE_P (expr))
13622 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13623 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13624 : break;
13625 : default:
13626 : break;
13627 : }
13628 : }
13629 :
13630 : /* Helper function for outputting the checksum of a tree T. When
13631 : debugging with gdb, you can "define mynext" to be "next" followed
13632 : by "call debug_fold_checksum (op0)", then just trace down till the
13633 : outputs differ. */
13634 :
13635 : DEBUG_FUNCTION void
13636 : debug_fold_checksum (const_tree t)
13637 : {
13638 : int i;
13639 : unsigned char checksum[16];
13640 : struct md5_ctx ctx;
13641 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13642 :
13643 : md5_init_ctx (&ctx);
13644 : fold_checksum_tree (t, &ctx, &ht);
13645 : md5_finish_ctx (&ctx, checksum);
13646 : ht.empty ();
13647 :
13648 : for (i = 0; i < 16; i++)
13649 : fprintf (stderr, "%d ", checksum[i]);
13650 :
13651 : fprintf (stderr, "\n");
13652 : }
13653 :
13654 : #endif
13655 :
13656 : /* Fold a unary tree expression with code CODE of type TYPE with an
13657 : operand OP0. LOC is the location of the resulting expression.
13658 : Return a folded expression if successful. Otherwise, return a tree
13659 : expression with code CODE of type TYPE with an operand OP0. */
13660 :
13661 : tree
13662 1031457817 : fold_build1_loc (location_t loc,
13663 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13664 : {
13665 1031457817 : tree tem;
13666 : #ifdef ENABLE_FOLD_CHECKING
13667 : unsigned char checksum_before[16], checksum_after[16];
13668 : struct md5_ctx ctx;
13669 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13670 :
13671 : md5_init_ctx (&ctx);
13672 : fold_checksum_tree (op0, &ctx, &ht);
13673 : md5_finish_ctx (&ctx, checksum_before);
13674 : ht.empty ();
13675 : #endif
13676 :
13677 1031457817 : tem = fold_unary_loc (loc, code, type, op0);
13678 1031457817 : if (!tem)
13679 523886517 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13680 :
13681 : #ifdef ENABLE_FOLD_CHECKING
13682 : md5_init_ctx (&ctx);
13683 : fold_checksum_tree (op0, &ctx, &ht);
13684 : md5_finish_ctx (&ctx, checksum_after);
13685 :
13686 : if (memcmp (checksum_before, checksum_after, 16))
13687 : fold_check_failed (op0, tem);
13688 : #endif
13689 1031457817 : return tem;
13690 : }
13691 :
13692 : /* Fold a binary tree expression with code CODE of type TYPE with
13693 : operands OP0 and OP1. LOC is the location of the resulting
13694 : expression. Return a folded expression if successful. Otherwise,
13695 : return a tree expression with code CODE of type TYPE with operands
13696 : OP0 and OP1. */
13697 :
13698 : tree
13699 675546102 : fold_build2_loc (location_t loc,
13700 : enum tree_code code, tree type, tree op0, tree op1
13701 : MEM_STAT_DECL)
13702 : {
13703 675546102 : tree tem;
13704 : #ifdef ENABLE_FOLD_CHECKING
13705 : unsigned char checksum_before_op0[16],
13706 : checksum_before_op1[16],
13707 : checksum_after_op0[16],
13708 : checksum_after_op1[16];
13709 : struct md5_ctx ctx;
13710 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13711 :
13712 : md5_init_ctx (&ctx);
13713 : fold_checksum_tree (op0, &ctx, &ht);
13714 : md5_finish_ctx (&ctx, checksum_before_op0);
13715 : ht.empty ();
13716 :
13717 : md5_init_ctx (&ctx);
13718 : fold_checksum_tree (op1, &ctx, &ht);
13719 : md5_finish_ctx (&ctx, checksum_before_op1);
13720 : ht.empty ();
13721 : #endif
13722 :
13723 675546102 : tem = fold_binary_loc (loc, code, type, op0, op1);
13724 675546102 : if (!tem)
13725 380023232 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13726 :
13727 : #ifdef ENABLE_FOLD_CHECKING
13728 : md5_init_ctx (&ctx);
13729 : fold_checksum_tree (op0, &ctx, &ht);
13730 : md5_finish_ctx (&ctx, checksum_after_op0);
13731 : ht.empty ();
13732 :
13733 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13734 : fold_check_failed (op0, tem);
13735 :
13736 : md5_init_ctx (&ctx);
13737 : fold_checksum_tree (op1, &ctx, &ht);
13738 : md5_finish_ctx (&ctx, checksum_after_op1);
13739 :
13740 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13741 : fold_check_failed (op1, tem);
13742 : #endif
13743 675546102 : return tem;
13744 : }
13745 :
13746 : /* Fold a ternary tree expression with code CODE of type TYPE with
13747 : operands OP0, OP1, and OP2. Return a folded expression if
13748 : successful. Otherwise, return a tree expression with code CODE of
13749 : type TYPE with operands OP0, OP1, and OP2. */
13750 :
13751 : tree
13752 38784120 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13753 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13754 : {
13755 38784120 : tree tem;
13756 : #ifdef ENABLE_FOLD_CHECKING
13757 : unsigned char checksum_before_op0[16],
13758 : checksum_before_op1[16],
13759 : checksum_before_op2[16],
13760 : checksum_after_op0[16],
13761 : checksum_after_op1[16],
13762 : checksum_after_op2[16];
13763 : struct md5_ctx ctx;
13764 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13765 :
13766 : md5_init_ctx (&ctx);
13767 : fold_checksum_tree (op0, &ctx, &ht);
13768 : md5_finish_ctx (&ctx, checksum_before_op0);
13769 : ht.empty ();
13770 :
13771 : md5_init_ctx (&ctx);
13772 : fold_checksum_tree (op1, &ctx, &ht);
13773 : md5_finish_ctx (&ctx, checksum_before_op1);
13774 : ht.empty ();
13775 :
13776 : md5_init_ctx (&ctx);
13777 : fold_checksum_tree (op2, &ctx, &ht);
13778 : md5_finish_ctx (&ctx, checksum_before_op2);
13779 : ht.empty ();
13780 : #endif
13781 :
13782 38784120 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13783 38784120 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13784 38784120 : if (!tem)
13785 35935347 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13786 :
13787 : #ifdef ENABLE_FOLD_CHECKING
13788 : md5_init_ctx (&ctx);
13789 : fold_checksum_tree (op0, &ctx, &ht);
13790 : md5_finish_ctx (&ctx, checksum_after_op0);
13791 : ht.empty ();
13792 :
13793 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13794 : fold_check_failed (op0, tem);
13795 :
13796 : md5_init_ctx (&ctx);
13797 : fold_checksum_tree (op1, &ctx, &ht);
13798 : md5_finish_ctx (&ctx, checksum_after_op1);
13799 : ht.empty ();
13800 :
13801 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13802 : fold_check_failed (op1, tem);
13803 :
13804 : md5_init_ctx (&ctx);
13805 : fold_checksum_tree (op2, &ctx, &ht);
13806 : md5_finish_ctx (&ctx, checksum_after_op2);
13807 :
13808 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13809 : fold_check_failed (op2, tem);
13810 : #endif
13811 38784120 : return tem;
13812 : }
13813 :
13814 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13815 : arguments in ARGARRAY, and a null static chain.
13816 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13817 : of type TYPE from the given operands as constructed by build_call_array. */
13818 :
13819 : tree
13820 56230974 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13821 : int nargs, tree *argarray)
13822 : {
13823 56230974 : tree tem;
13824 : #ifdef ENABLE_FOLD_CHECKING
13825 : unsigned char checksum_before_fn[16],
13826 : checksum_before_arglist[16],
13827 : checksum_after_fn[16],
13828 : checksum_after_arglist[16];
13829 : struct md5_ctx ctx;
13830 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13831 : int i;
13832 :
13833 : md5_init_ctx (&ctx);
13834 : fold_checksum_tree (fn, &ctx, &ht);
13835 : md5_finish_ctx (&ctx, checksum_before_fn);
13836 : ht.empty ();
13837 :
13838 : md5_init_ctx (&ctx);
13839 : for (i = 0; i < nargs; i++)
13840 : fold_checksum_tree (argarray[i], &ctx, &ht);
13841 : md5_finish_ctx (&ctx, checksum_before_arglist);
13842 : ht.empty ();
13843 : #endif
13844 :
13845 56230974 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13846 56230974 : if (!tem)
13847 54537383 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13848 :
13849 : #ifdef ENABLE_FOLD_CHECKING
13850 : md5_init_ctx (&ctx);
13851 : fold_checksum_tree (fn, &ctx, &ht);
13852 : md5_finish_ctx (&ctx, checksum_after_fn);
13853 : ht.empty ();
13854 :
13855 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13856 : fold_check_failed (fn, tem);
13857 :
13858 : md5_init_ctx (&ctx);
13859 : for (i = 0; i < nargs; i++)
13860 : fold_checksum_tree (argarray[i], &ctx, &ht);
13861 : md5_finish_ctx (&ctx, checksum_after_arglist);
13862 :
13863 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13864 : fold_check_failed (NULL_TREE, tem);
13865 : #endif
13866 56230974 : return tem;
13867 : }
13868 :
13869 : /* Perform constant folding and related simplification of initializer
13870 : expression EXPR. These behave identically to "fold_buildN" but ignore
13871 : potential run-time traps and exceptions that fold must preserve. */
13872 :
13873 : #define START_FOLD_INIT \
13874 : int saved_signaling_nans = flag_signaling_nans;\
13875 : int saved_trapping_math = flag_trapping_math;\
13876 : int saved_rounding_math = flag_rounding_math;\
13877 : int saved_trapv = flag_trapv;\
13878 : int saved_folding_initializer = folding_initializer;\
13879 : flag_signaling_nans = 0;\
13880 : flag_trapping_math = 0;\
13881 : flag_rounding_math = 0;\
13882 : flag_trapv = 0;\
13883 : folding_initializer = 1;
13884 :
13885 : #define END_FOLD_INIT \
13886 : flag_signaling_nans = saved_signaling_nans;\
13887 : flag_trapping_math = saved_trapping_math;\
13888 : flag_rounding_math = saved_rounding_math;\
13889 : flag_trapv = saved_trapv;\
13890 : folding_initializer = saved_folding_initializer;
13891 :
13892 : tree
13893 546152 : fold_init (tree expr)
13894 : {
13895 546152 : tree result;
13896 546152 : START_FOLD_INIT;
13897 :
13898 546152 : result = fold (expr);
13899 :
13900 546152 : END_FOLD_INIT;
13901 546152 : return result;
13902 : }
13903 :
13904 : tree
13905 2988809 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13906 : tree type, tree op)
13907 : {
13908 2988809 : tree result;
13909 2988809 : START_FOLD_INIT;
13910 :
13911 2988809 : result = fold_build1_loc (loc, code, type, op);
13912 :
13913 2988809 : END_FOLD_INIT;
13914 2988809 : return result;
13915 : }
13916 :
13917 : tree
13918 50374 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13919 : tree type, tree op0, tree op1)
13920 : {
13921 50374 : tree result;
13922 50374 : START_FOLD_INIT;
13923 :
13924 50374 : result = fold_build2_loc (loc, code, type, op0, op1);
13925 :
13926 50374 : END_FOLD_INIT;
13927 50374 : return result;
13928 : }
13929 :
13930 : tree
13931 3464 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13932 : int nargs, tree *argarray)
13933 : {
13934 3464 : tree result;
13935 3464 : START_FOLD_INIT;
13936 :
13937 3464 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13938 :
13939 3464 : END_FOLD_INIT;
13940 3464 : return result;
13941 : }
13942 :
13943 : tree
13944 66619423 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
13945 : tree lhs, tree rhs)
13946 : {
13947 66619423 : tree result;
13948 66619423 : START_FOLD_INIT;
13949 :
13950 66619423 : result = fold_binary_loc (loc, code, type, lhs, rhs);
13951 :
13952 66619423 : END_FOLD_INIT;
13953 66619423 : return result;
13954 : }
13955 :
13956 : #undef START_FOLD_INIT
13957 : #undef END_FOLD_INIT
13958 :
13959 : /* Determine if first argument is a multiple of second argument. Return
13960 : false if it is not, or we cannot easily determined it to be.
13961 :
13962 : An example of the sort of thing we care about (at this point; this routine
13963 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
13964 : fold cases do now) is discovering that
13965 :
13966 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13967 :
13968 : is a multiple of
13969 :
13970 : SAVE_EXPR (J * 8)
13971 :
13972 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13973 :
13974 : This code also handles discovering that
13975 :
13976 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13977 :
13978 : is a multiple of 8 so we don't have to worry about dealing with a
13979 : possible remainder.
13980 :
13981 : Note that we *look* inside a SAVE_EXPR only to determine how it was
13982 : calculated; it is not safe for fold to do much of anything else with the
13983 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13984 : at run time. For example, the latter example above *cannot* be implemented
13985 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13986 : evaluation time of the original SAVE_EXPR is not necessarily the same at
13987 : the time the new expression is evaluated. The only optimization of this
13988 : sort that would be valid is changing
13989 :
13990 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13991 :
13992 : divided by 8 to
13993 :
13994 : SAVE_EXPR (I) * SAVE_EXPR (J)
13995 :
13996 : (where the same SAVE_EXPR (J) is used in the original and the
13997 : transformed version).
13998 :
13999 : NOWRAP specifies whether all outer operations in TYPE should
14000 : be considered not wrapping. Any type conversion within TOP acts
14001 : as a barrier and we will fall back to NOWRAP being false.
14002 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14003 : as not wrapping even though they are generally using unsigned arithmetic. */
14004 :
14005 : bool
14006 1599026 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14007 : {
14008 1599026 : gimple *stmt;
14009 1599026 : tree op1, op2;
14010 :
14011 1599026 : if (operand_equal_p (top, bottom, 0))
14012 : return true;
14013 :
14014 1102973 : if (TREE_CODE (type) != INTEGER_TYPE)
14015 : return false;
14016 :
14017 1102954 : switch (TREE_CODE (top))
14018 : {
14019 702 : case BIT_AND_EXPR:
14020 : /* Bitwise and provides a power of two multiple. If the mask is
14021 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14022 702 : if (!integer_pow2p (bottom))
14023 : return false;
14024 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14025 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14026 :
14027 401904 : case MULT_EXPR:
14028 : /* If the multiplication can wrap we cannot recurse further unless
14029 : the bottom is a power of two which is where wrapping does not
14030 : matter. */
14031 401904 : if (!nowrap
14032 15104 : && !TYPE_OVERFLOW_UNDEFINED (type)
14033 406588 : && !integer_pow2p (bottom))
14034 : return false;
14035 401461 : if (TREE_CODE (bottom) == INTEGER_CST)
14036 : {
14037 399765 : op1 = TREE_OPERAND (top, 0);
14038 399765 : op2 = TREE_OPERAND (top, 1);
14039 399765 : if (TREE_CODE (op1) == INTEGER_CST)
14040 0 : std::swap (op1, op2);
14041 399765 : if (TREE_CODE (op2) == INTEGER_CST)
14042 : {
14043 389571 : if (multiple_of_p (type, op2, bottom, nowrap))
14044 : return true;
14045 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14046 3301 : if (multiple_of_p (type, bottom, op2, nowrap))
14047 : {
14048 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14049 1888 : wi::to_widest (op2));
14050 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14051 : {
14052 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14053 1888 : return multiple_of_p (type, op1, op2, nowrap);
14054 : }
14055 1888 : }
14056 1413 : return multiple_of_p (type, op1, bottom, nowrap);
14057 : }
14058 : }
14059 11890 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14060 11890 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14061 :
14062 403 : case LSHIFT_EXPR:
14063 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14064 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14065 : {
14066 403 : op1 = TREE_OPERAND (top, 1);
14067 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14068 : {
14069 403 : wide_int mul_op
14070 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14071 806 : return multiple_of_p (type,
14072 806 : wide_int_to_tree (type, mul_op), bottom,
14073 : nowrap);
14074 403 : }
14075 : }
14076 : return false;
14077 :
14078 224597 : case MINUS_EXPR:
14079 224597 : case PLUS_EXPR:
14080 : /* If the addition or subtraction can wrap we cannot recurse further
14081 : unless bottom is a power of two which is where wrapping does not
14082 : matter. */
14083 224597 : if (!nowrap
14084 174458 : && !TYPE_OVERFLOW_UNDEFINED (type)
14085 397623 : && !integer_pow2p (bottom))
14086 : return false;
14087 :
14088 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14089 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14090 : but 0xfffffffd is not. */
14091 195721 : op1 = TREE_OPERAND (top, 1);
14092 195721 : if (TREE_CODE (top) == PLUS_EXPR
14093 189470 : && nowrap
14094 43975 : && TYPE_UNSIGNED (type)
14095 238986 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14096 27718 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14097 :
14098 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14099 : precisely, so be conservative here checking if both op0 and op1
14100 : are multiple of bottom. Note we check the second operand first
14101 : since it's usually simpler. */
14102 195721 : return (multiple_of_p (type, op1, bottom, nowrap)
14103 195721 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14104 :
14105 144315 : CASE_CONVERT:
14106 : /* Can't handle conversions from non-integral or wider integral type. */
14107 144315 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14108 144315 : || (TYPE_PRECISION (type)
14109 38872 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14110 : return false;
14111 : /* NOWRAP only extends to operations in the outermost type so
14112 : make sure to strip it off here. */
14113 38614 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14114 77228 : TREE_OPERAND (top, 0), bottom, false);
14115 :
14116 12996 : case SAVE_EXPR:
14117 12996 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14118 :
14119 86 : case COND_EXPR:
14120 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14121 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14122 :
14123 139121 : case INTEGER_CST:
14124 139121 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14125 2702 : return false;
14126 136419 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14127 : SIGNED);
14128 :
14129 61980 : case SSA_NAME:
14130 61980 : if (TREE_CODE (bottom) == INTEGER_CST
14131 58799 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14132 120779 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14133 : {
14134 25513 : enum tree_code code = gimple_assign_rhs_code (stmt);
14135 :
14136 : /* Check for special cases to see if top is defined as multiple
14137 : of bottom:
14138 :
14139 : top = (X & ~(bottom - 1) ; bottom is power of 2
14140 :
14141 : or
14142 :
14143 : Y = X % bottom
14144 : top = X - Y. */
14145 25513 : if (code == BIT_AND_EXPR
14146 310 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14147 310 : && TREE_CODE (op2) == INTEGER_CST
14148 202 : && integer_pow2p (bottom)
14149 25715 : && wi::multiple_of_p (wi::to_widest (op2),
14150 202 : wi::to_widest (bottom), SIGNED))
14151 193 : return true;
14152 :
14153 25320 : op1 = gimple_assign_rhs1 (stmt);
14154 25320 : if (code == MINUS_EXPR
14155 1535 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14156 1535 : && TREE_CODE (op2) == SSA_NAME
14157 1535 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14158 1535 : && gimple_code (stmt) == GIMPLE_ASSIGN
14159 1274 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14160 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14161 25384 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14162 : return true;
14163 : }
14164 :
14165 : /* fall through */
14166 :
14167 : default:
14168 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14169 : return multiple_p (wi::to_poly_widest (top),
14170 : wi::to_poly_widest (bottom));
14171 :
14172 : return false;
14173 : }
14174 : }
14175 :
14176 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14177 : This function returns true for integer expressions, and returns
14178 : false if uncertain. */
14179 :
14180 : bool
14181 507075 : tree_expr_finite_p (const_tree x)
14182 : {
14183 507079 : machine_mode mode = element_mode (x);
14184 507079 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14185 : return true;
14186 506837 : switch (TREE_CODE (x))
14187 : {
14188 592 : case REAL_CST:
14189 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14190 0 : case COMPLEX_CST:
14191 0 : return tree_expr_finite_p (TREE_REALPART (x))
14192 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14193 : case FLOAT_EXPR:
14194 : return true;
14195 4 : case ABS_EXPR:
14196 4 : case CONVERT_EXPR:
14197 4 : case NON_LVALUE_EXPR:
14198 4 : case NEGATE_EXPR:
14199 4 : case SAVE_EXPR:
14200 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14201 0 : case MIN_EXPR:
14202 0 : case MAX_EXPR:
14203 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14204 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14205 0 : case COND_EXPR:
14206 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14207 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14208 38 : case CALL_EXPR:
14209 38 : switch (get_call_combined_fn (x))
14210 : {
14211 0 : CASE_CFN_FABS:
14212 0 : CASE_CFN_FABS_FN:
14213 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14214 0 : CASE_CFN_FMAX:
14215 0 : CASE_CFN_FMAX_FN:
14216 0 : CASE_CFN_FMIN:
14217 0 : CASE_CFN_FMIN_FN:
14218 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14219 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14220 : default:
14221 : return false;
14222 : }
14223 :
14224 : default:
14225 : return false;
14226 : }
14227 : }
14228 :
14229 : /* Return true if expression X evaluates to an infinity.
14230 : This function returns false for integer expressions. */
14231 :
14232 : bool
14233 907836 : tree_expr_infinite_p (const_tree x)
14234 : {
14235 908286 : if (!HONOR_INFINITIES (x))
14236 : return false;
14237 908131 : switch (TREE_CODE (x))
14238 : {
14239 0 : case REAL_CST:
14240 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14241 450 : case ABS_EXPR:
14242 450 : case NEGATE_EXPR:
14243 450 : case NON_LVALUE_EXPR:
14244 450 : case SAVE_EXPR:
14245 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14246 0 : case COND_EXPR:
14247 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14248 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14249 : default:
14250 : return false;
14251 : }
14252 : }
14253 :
14254 : /* Return true if expression X could evaluate to an infinity.
14255 : This function returns false for integer expressions, and returns
14256 : true if uncertain. */
14257 :
14258 : bool
14259 520346 : tree_expr_maybe_infinite_p (const_tree x)
14260 : {
14261 520354 : if (!HONOR_INFINITIES (x))
14262 : return false;
14263 519987 : switch (TREE_CODE (x))
14264 : {
14265 273 : case REAL_CST:
14266 273 : return real_isinf (TREE_REAL_CST_PTR (x));
14267 : case FLOAT_EXPR:
14268 : return false;
14269 8 : case ABS_EXPR:
14270 8 : case NEGATE_EXPR:
14271 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14272 1 : case COND_EXPR:
14273 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14274 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14275 : default:
14276 : return true;
14277 : }
14278 : }
14279 :
14280 : /* Return true if expression X evaluates to a signaling NaN.
14281 : This function returns false for integer expressions. */
14282 :
14283 : bool
14284 385 : tree_expr_signaling_nan_p (const_tree x)
14285 : {
14286 385 : if (!HONOR_SNANS (x))
14287 : return false;
14288 124 : switch (TREE_CODE (x))
14289 : {
14290 124 : case REAL_CST:
14291 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14292 0 : case NON_LVALUE_EXPR:
14293 0 : case SAVE_EXPR:
14294 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14295 0 : case COND_EXPR:
14296 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14297 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14298 : default:
14299 : return false;
14300 : }
14301 : }
14302 :
14303 : /* Return true if expression X could evaluate to a signaling NaN.
14304 : This function returns false for integer expressions, and returns
14305 : true if uncertain. */
14306 :
14307 : bool
14308 727123 : tree_expr_maybe_signaling_nan_p (const_tree x)
14309 : {
14310 727123 : if (!HONOR_SNANS (x))
14311 : return false;
14312 5028 : switch (TREE_CODE (x))
14313 : {
14314 1452 : case REAL_CST:
14315 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14316 : case FLOAT_EXPR:
14317 : return false;
14318 0 : case ABS_EXPR:
14319 0 : case CONVERT_EXPR:
14320 0 : case NEGATE_EXPR:
14321 0 : case NON_LVALUE_EXPR:
14322 0 : case SAVE_EXPR:
14323 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14324 0 : case MIN_EXPR:
14325 0 : case MAX_EXPR:
14326 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14327 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14328 0 : case COND_EXPR:
14329 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14330 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14331 0 : case CALL_EXPR:
14332 0 : switch (get_call_combined_fn (x))
14333 : {
14334 0 : CASE_CFN_FABS:
14335 0 : CASE_CFN_FABS_FN:
14336 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14337 0 : CASE_CFN_FMAX:
14338 0 : CASE_CFN_FMAX_FN:
14339 0 : CASE_CFN_FMIN:
14340 0 : CASE_CFN_FMIN_FN:
14341 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14342 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14343 : default:
14344 : return true;
14345 : }
14346 : default:
14347 : return true;
14348 : }
14349 : }
14350 :
14351 : /* Return true if expression X evaluates to a NaN.
14352 : This function returns false for integer expressions. */
14353 :
14354 : bool
14355 3795189 : tree_expr_nan_p (const_tree x)
14356 : {
14357 4148781 : if (!HONOR_NANS (x))
14358 : return false;
14359 4148431 : switch (TREE_CODE (x))
14360 : {
14361 3806 : case REAL_CST:
14362 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14363 353592 : case NON_LVALUE_EXPR:
14364 353592 : case SAVE_EXPR:
14365 353592 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14366 956 : case COND_EXPR:
14367 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14368 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14369 : default:
14370 : return false;
14371 : }
14372 : }
14373 :
14374 : /* Return true if expression X could evaluate to a NaN.
14375 : This function returns false for integer expressions, and returns
14376 : true if uncertain. */
14377 :
14378 : bool
14379 4745443 : tree_expr_maybe_nan_p (const_tree x)
14380 : {
14381 6704901 : if (!HONOR_NANS (x))
14382 : return false;
14383 6586084 : switch (TREE_CODE (x))
14384 : {
14385 3422 : case REAL_CST:
14386 3422 : return real_isnan (TREE_REAL_CST_PTR (x));
14387 : case FLOAT_EXPR:
14388 : return false;
14389 14796 : case PLUS_EXPR:
14390 14796 : case MINUS_EXPR:
14391 14796 : case MULT_EXPR:
14392 14796 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14393 14796 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14394 1959458 : case ABS_EXPR:
14395 1959458 : case CONVERT_EXPR:
14396 1959458 : case NEGATE_EXPR:
14397 1959458 : case NON_LVALUE_EXPR:
14398 1959458 : case SAVE_EXPR:
14399 1959458 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14400 176 : case MIN_EXPR:
14401 176 : case MAX_EXPR:
14402 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14403 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14404 599 : case COND_EXPR:
14405 599 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14406 599 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14407 1085 : case CALL_EXPR:
14408 1085 : switch (get_call_combined_fn (x))
14409 : {
14410 0 : CASE_CFN_FABS:
14411 0 : CASE_CFN_FABS_FN:
14412 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14413 108 : CASE_CFN_FMAX:
14414 108 : CASE_CFN_FMAX_FN:
14415 108 : CASE_CFN_FMIN:
14416 108 : CASE_CFN_FMIN_FN:
14417 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14418 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14419 : default:
14420 : return true;
14421 : }
14422 : default:
14423 : return true;
14424 : }
14425 : }
14426 :
14427 : /* Return true if expression X could evaluate to -0.0.
14428 : This function returns true if uncertain. */
14429 :
14430 : bool
14431 602386 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14432 : {
14433 602386 : if (!HONOR_SIGNED_ZEROS (x))
14434 : return false;
14435 602386 : switch (TREE_CODE (x))
14436 : {
14437 0 : case REAL_CST:
14438 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14439 : case INTEGER_CST:
14440 : case FLOAT_EXPR:
14441 : case ABS_EXPR:
14442 : return false;
14443 0 : case NON_LVALUE_EXPR:
14444 0 : case SAVE_EXPR:
14445 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14446 0 : case COND_EXPR:
14447 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14448 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14449 2 : case CALL_EXPR:
14450 2 : switch (get_call_combined_fn (x))
14451 : {
14452 : CASE_CFN_FABS:
14453 : CASE_CFN_FABS_FN:
14454 : return false;
14455 : default:
14456 : break;
14457 : }
14458 : default:
14459 : break;
14460 : }
14461 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14462 : * but currently those predicates require tree and not const_tree. */
14463 : return true;
14464 : }
14465 :
14466 : #define tree_expr_nonnegative_p(X, Y) \
14467 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14468 :
14469 : #define RECURSE(X) \
14470 : ((tree_expr_nonnegative_p) (X, depth + 1))
14471 :
14472 : /* Return true if CODE or TYPE is known to be non-negative. */
14473 :
14474 : static bool
14475 25254613 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14476 : {
14477 25254613 : if (!VECTOR_TYPE_P (type)
14478 25219127 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14479 50473475 : && truth_value_p (code))
14480 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14481 : have a signed:1 type (where the value is -1 and 0). */
14482 : return true;
14483 : return false;
14484 : }
14485 :
14486 : /* Return true if (CODE OP0) is known to be non-negative.
14487 : DEPTH is the current nesting depth of the query. */
14488 :
14489 : bool
14490 2536285 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14491 : {
14492 2536285 : if (TYPE_UNSIGNED (type))
14493 : return true;
14494 :
14495 2084853 : switch (code)
14496 : {
14497 291853 : case ABS_EXPR:
14498 : /* We can't return 1 if flag_wrapv is set because
14499 : ABS_EXPR<INT_MIN> = INT_MIN. */
14500 291853 : if (!ANY_INTEGRAL_TYPE_P (type))
14501 : return true;
14502 10447 : if (TYPE_OVERFLOW_UNDEFINED (type))
14503 : return true;
14504 : break;
14505 :
14506 72077 : case NON_LVALUE_EXPR:
14507 72077 : case FLOAT_EXPR:
14508 72077 : case FIX_TRUNC_EXPR:
14509 72077 : return RECURSE (op0);
14510 :
14511 1679947 : CASE_CONVERT:
14512 1679947 : {
14513 1679947 : tree inner_type = TREE_TYPE (op0);
14514 1679947 : tree outer_type = type;
14515 :
14516 1679947 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14517 : {
14518 408158 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14519 408158 : return RECURSE (op0);
14520 0 : if (INTEGRAL_TYPE_P (inner_type))
14521 : {
14522 0 : if (TYPE_UNSIGNED (inner_type))
14523 : return true;
14524 0 : return RECURSE (op0);
14525 : }
14526 : }
14527 1271789 : else if (INTEGRAL_TYPE_P (outer_type))
14528 : {
14529 1271712 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14530 0 : return RECURSE (op0);
14531 1271712 : if (INTEGRAL_TYPE_P (inner_type))
14532 1265697 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14533 1265697 : && TYPE_UNSIGNED (inner_type);
14534 : }
14535 : }
14536 : break;
14537 :
14538 40976 : default:
14539 40976 : return tree_simple_nonnegative_warnv_p (code, type);
14540 : }
14541 :
14542 : /* We don't know sign of `t', so be conservative and return false. */
14543 : return false;
14544 : }
14545 :
14546 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14547 : DEPTH is the current nesting depth of the query. */
14548 :
14549 : bool
14550 5239473 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14551 : tree op1, int depth)
14552 : {
14553 5239473 : if (TYPE_UNSIGNED (type))
14554 : return true;
14555 :
14556 4958164 : switch (code)
14557 : {
14558 1334835 : case POINTER_PLUS_EXPR:
14559 1334835 : case PLUS_EXPR:
14560 1334835 : if (FLOAT_TYPE_P (type))
14561 49204 : return RECURSE (op0) && RECURSE (op1);
14562 :
14563 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14564 : both unsigned and at least 2 bits shorter than the result. */
14565 1285631 : if (TREE_CODE (type) == INTEGER_TYPE
14566 1279637 : && TREE_CODE (op0) == NOP_EXPR
14567 17173 : && TREE_CODE (op1) == NOP_EXPR)
14568 : {
14569 200 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14570 200 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14571 200 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14572 301 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14573 : {
14574 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14575 95 : TYPE_PRECISION (inner2)) + 1;
14576 95 : return prec < TYPE_PRECISION (type);
14577 : }
14578 : }
14579 : break;
14580 :
14581 180411 : case MULT_EXPR:
14582 180411 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14583 : {
14584 : /* x * x is always non-negative for floating point x
14585 : or without overflow. */
14586 163558 : if (operand_equal_p (op0, op1, 0)
14587 163558 : || (RECURSE (op0) && RECURSE (op1)))
14588 1946 : return true;
14589 : }
14590 :
14591 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14592 : both unsigned and their total bits is shorter than the result. */
14593 178465 : if (TREE_CODE (type) == INTEGER_TYPE
14594 107533 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14595 122 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14596 : {
14597 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14598 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14599 116 : : TREE_TYPE (op0);
14600 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14601 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14602 116 : : TREE_TYPE (op1);
14603 :
14604 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14605 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14606 :
14607 116 : if (TREE_CODE (op0) == INTEGER_CST)
14608 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14609 :
14610 116 : if (TREE_CODE (op1) == INTEGER_CST)
14611 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14612 :
14613 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14614 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14615 : {
14616 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14617 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14618 0 : : TYPE_PRECISION (inner0);
14619 :
14620 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14621 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14622 0 : : TYPE_PRECISION (inner1);
14623 :
14624 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14625 : }
14626 : }
14627 : return false;
14628 :
14629 26566 : case BIT_AND_EXPR:
14630 26566 : return RECURSE (op0) || RECURSE (op1);
14631 :
14632 53495 : case MAX_EXPR:
14633 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14634 : things. */
14635 53495 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14636 76 : return RECURSE (op0) && RECURSE (op1);
14637 53419 : return RECURSE (op0) || RECURSE (op1);
14638 :
14639 168567 : case BIT_IOR_EXPR:
14640 168567 : case BIT_XOR_EXPR:
14641 168567 : case MIN_EXPR:
14642 168567 : case RDIV_EXPR:
14643 168567 : case TRUNC_DIV_EXPR:
14644 168567 : case CEIL_DIV_EXPR:
14645 168567 : case FLOOR_DIV_EXPR:
14646 168567 : case ROUND_DIV_EXPR:
14647 168567 : return RECURSE (op0) && RECURSE (op1);
14648 :
14649 88063 : case TRUNC_MOD_EXPR:
14650 88063 : return RECURSE (op0);
14651 :
14652 230 : case FLOOR_MOD_EXPR:
14653 230 : return RECURSE (op1);
14654 :
14655 3105997 : case CEIL_MOD_EXPR:
14656 3105997 : case ROUND_MOD_EXPR:
14657 3105997 : default:
14658 3105997 : return tree_simple_nonnegative_warnv_p (code, type);
14659 : }
14660 :
14661 : /* We don't know sign of `t', so be conservative and return false. */
14662 : return false;
14663 : }
14664 :
14665 : /* Return true if T is known to be non-negative.
14666 : DEPTH is the current nesting depth of the query. */
14667 :
14668 : bool
14669 24401202 : tree_single_nonnegative_p (tree t, int depth)
14670 : {
14671 24401202 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14672 : return true;
14673 :
14674 20913884 : switch (TREE_CODE (t))
14675 : {
14676 2534858 : case INTEGER_CST:
14677 2534858 : return tree_int_cst_sgn (t) >= 0;
14678 :
14679 920212 : case REAL_CST:
14680 920212 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14681 :
14682 0 : case FIXED_CST:
14683 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14684 :
14685 928 : case COND_EXPR:
14686 928 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14687 :
14688 8738029 : case SSA_NAME:
14689 : /* Limit the depth of recursion to avoid quadratic behavior.
14690 : This is expected to catch almost all occurrences in practice.
14691 : If this code misses important cases that unbounded recursion
14692 : would not, passes that need this information could be revised
14693 : to provide it through dataflow propagation. */
14694 8738029 : return (!name_registered_for_update_p (t)
14695 8738028 : && depth < param_max_ssa_name_query_depth
14696 17327838 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14697 :
14698 8719857 : default:
14699 8719857 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14700 : }
14701 : }
14702 :
14703 : /* Return true if T is known to be non-negative.
14704 : DEPTH is the current nesting depth of the query. */
14705 :
14706 : bool
14707 13481520 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14708 : int depth)
14709 : {
14710 13481520 : switch (fn)
14711 : {
14712 : CASE_CFN_ACOS:
14713 : CASE_CFN_ACOS_FN:
14714 : CASE_CFN_ACOSH:
14715 : CASE_CFN_ACOSH_FN:
14716 : CASE_CFN_ACOSPI:
14717 : CASE_CFN_ACOSPI_FN:
14718 : CASE_CFN_CABS:
14719 : CASE_CFN_CABS_FN:
14720 : CASE_CFN_COSH:
14721 : CASE_CFN_COSH_FN:
14722 : CASE_CFN_ERFC:
14723 : CASE_CFN_ERFC_FN:
14724 : CASE_CFN_EXP:
14725 : CASE_CFN_EXP_FN:
14726 : CASE_CFN_EXP10:
14727 : CASE_CFN_EXP2:
14728 : CASE_CFN_EXP2_FN:
14729 : CASE_CFN_FABS:
14730 : CASE_CFN_FABS_FN:
14731 : CASE_CFN_FDIM:
14732 : CASE_CFN_FDIM_FN:
14733 : CASE_CFN_HYPOT:
14734 : CASE_CFN_HYPOT_FN:
14735 : CASE_CFN_POW10:
14736 : CASE_CFN_FFS:
14737 : CASE_CFN_PARITY:
14738 : CASE_CFN_POPCOUNT:
14739 : CASE_CFN_CLRSB:
14740 : CASE_CFN_BSWAP:
14741 : CASE_CFN_BITREVERSE:
14742 : /* Always true. */
14743 : return true;
14744 :
14745 948 : CASE_CFN_CLZ:
14746 948 : CASE_CFN_CTZ:
14747 948 : if (arg1)
14748 0 : return RECURSE (arg1);
14749 : return true;
14750 :
14751 945 : CASE_CFN_SQRT:
14752 945 : CASE_CFN_SQRT_FN:
14753 : /* sqrt(-0.0) is -0.0. */
14754 945 : if (!HONOR_SIGNED_ZEROS (type))
14755 : return true;
14756 913 : return RECURSE (arg0);
14757 :
14758 54666 : CASE_CFN_ASINH:
14759 54666 : CASE_CFN_ASINH_FN:
14760 54666 : CASE_CFN_ASINPI:
14761 54666 : CASE_CFN_ASINPI_FN:
14762 54666 : CASE_CFN_ATAN:
14763 54666 : CASE_CFN_ATAN_FN:
14764 54666 : CASE_CFN_ATANH:
14765 54666 : CASE_CFN_ATANH_FN:
14766 54666 : CASE_CFN_ATANPI:
14767 54666 : CASE_CFN_ATANPI_FN:
14768 54666 : CASE_CFN_CBRT:
14769 54666 : CASE_CFN_CBRT_FN:
14770 54666 : CASE_CFN_CEIL:
14771 54666 : CASE_CFN_CEIL_FN:
14772 54666 : CASE_CFN_ERF:
14773 54666 : CASE_CFN_ERF_FN:
14774 54666 : CASE_CFN_EXPM1:
14775 54666 : CASE_CFN_EXPM1_FN:
14776 54666 : CASE_CFN_FLOOR:
14777 54666 : CASE_CFN_FLOOR_FN:
14778 54666 : CASE_CFN_FMOD:
14779 54666 : CASE_CFN_FMOD_FN:
14780 54666 : CASE_CFN_FREXP:
14781 54666 : CASE_CFN_FREXP_FN:
14782 54666 : CASE_CFN_ICEIL:
14783 54666 : CASE_CFN_IFLOOR:
14784 54666 : CASE_CFN_IRINT:
14785 54666 : CASE_CFN_IROUND:
14786 54666 : CASE_CFN_LCEIL:
14787 54666 : CASE_CFN_LDEXP:
14788 54666 : CASE_CFN_LFLOOR:
14789 54666 : CASE_CFN_LLCEIL:
14790 54666 : CASE_CFN_LLFLOOR:
14791 54666 : CASE_CFN_LLRINT:
14792 54666 : CASE_CFN_LLRINT_FN:
14793 54666 : CASE_CFN_LLROUND:
14794 54666 : CASE_CFN_LLROUND_FN:
14795 54666 : CASE_CFN_LRINT:
14796 54666 : CASE_CFN_LRINT_FN:
14797 54666 : CASE_CFN_LROUND:
14798 54666 : CASE_CFN_LROUND_FN:
14799 54666 : CASE_CFN_MODF:
14800 54666 : CASE_CFN_MODF_FN:
14801 54666 : CASE_CFN_NEARBYINT:
14802 54666 : CASE_CFN_NEARBYINT_FN:
14803 54666 : CASE_CFN_RINT:
14804 54666 : CASE_CFN_RINT_FN:
14805 54666 : CASE_CFN_ROUND:
14806 54666 : CASE_CFN_ROUND_FN:
14807 54666 : CASE_CFN_ROUNDEVEN:
14808 54666 : CASE_CFN_ROUNDEVEN_FN:
14809 54666 : CASE_CFN_SCALB:
14810 54666 : CASE_CFN_SCALBLN:
14811 54666 : CASE_CFN_SCALBLN_FN:
14812 54666 : CASE_CFN_SCALBN:
14813 54666 : CASE_CFN_SCALBN_FN:
14814 54666 : CASE_CFN_SIGNBIT:
14815 54666 : CASE_CFN_SIGNIFICAND:
14816 54666 : CASE_CFN_SINH:
14817 54666 : CASE_CFN_SINH_FN:
14818 54666 : CASE_CFN_TANH:
14819 54666 : CASE_CFN_TANH_FN:
14820 54666 : CASE_CFN_TRUNC:
14821 54666 : CASE_CFN_TRUNC_FN:
14822 : /* True if the 1st argument is nonnegative. */
14823 54666 : return RECURSE (arg0);
14824 :
14825 1319 : CASE_CFN_FMAX:
14826 1319 : CASE_CFN_FMAX_FN:
14827 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14828 : things. In the presence of sNaNs, we're only guaranteed to be
14829 : non-negative if both operands are non-negative. In the presence
14830 : of qNaNs, we're non-negative if either operand is non-negative
14831 : and can't be a qNaN, or if both operands are non-negative. */
14832 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14833 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14834 136 : return RECURSE (arg0) && RECURSE (arg1);
14835 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14836 332 : || RECURSE (arg1))
14837 851 : : (RECURSE (arg1)
14838 851 : && !tree_expr_maybe_nan_p (arg1));
14839 :
14840 910 : CASE_CFN_FMIN:
14841 910 : CASE_CFN_FMIN_FN:
14842 : /* True if the 1st AND 2nd arguments are nonnegative. */
14843 910 : return RECURSE (arg0) && RECURSE (arg1);
14844 :
14845 742 : CASE_CFN_COPYSIGN:
14846 742 : CASE_CFN_COPYSIGN_FN:
14847 : /* True if the 2nd argument is nonnegative. */
14848 742 : return RECURSE (arg1);
14849 :
14850 2302 : CASE_CFN_POWI:
14851 : /* True if the 1st argument is nonnegative or the second
14852 : argument is an even integer. */
14853 2302 : if (TREE_CODE (arg1) == INTEGER_CST
14854 2302 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14855 : return true;
14856 2221 : return RECURSE (arg0);
14857 :
14858 4912 : CASE_CFN_POW:
14859 4912 : CASE_CFN_POW_FN:
14860 : /* True if the 1st argument is nonnegative or the second
14861 : argument is an even integer valued real. */
14862 4912 : if (TREE_CODE (arg1) == REAL_CST)
14863 : {
14864 2208 : REAL_VALUE_TYPE c;
14865 2208 : HOST_WIDE_INT n;
14866 :
14867 2208 : c = TREE_REAL_CST (arg1);
14868 2208 : n = real_to_integer (&c);
14869 2208 : if ((n & 1) == 0)
14870 : {
14871 1579 : REAL_VALUE_TYPE cint;
14872 1579 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14873 1579 : if (real_identical (&c, &cint))
14874 574 : return true;
14875 : }
14876 : }
14877 4338 : return RECURSE (arg0);
14878 :
14879 13385010 : default:
14880 13385010 : break;
14881 : }
14882 13385010 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14883 : }
14884 :
14885 : /* Return true if T is known to be non-negative.
14886 : DEPTH is the current nesting depth of the query. */
14887 :
14888 : static bool
14889 1729913 : tree_invalid_nonnegative_p (tree t, int depth)
14890 : {
14891 1729913 : enum tree_code code = TREE_CODE (t);
14892 1729913 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14893 : return true;
14894 :
14895 1314365 : switch (code)
14896 : {
14897 268 : case TARGET_EXPR:
14898 268 : {
14899 268 : tree temp = TARGET_EXPR_SLOT (t);
14900 268 : t = TARGET_EXPR_INITIAL (t);
14901 :
14902 : /* If the initializer is non-void, then it's a normal expression
14903 : that will be assigned to the slot. */
14904 268 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14905 66 : return RECURSE (t);
14906 :
14907 : /* Otherwise, the initializer sets the slot in some way. One common
14908 : way is an assignment statement at the end of the initializer. */
14909 404 : while (1)
14910 : {
14911 404 : if (TREE_CODE (t) == BIND_EXPR)
14912 202 : t = expr_last (BIND_EXPR_BODY (t));
14913 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14914 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
14915 0 : t = expr_last (TREE_OPERAND (t, 0));
14916 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
14917 0 : t = expr_last (t);
14918 : else
14919 : break;
14920 : }
14921 202 : if (TREE_CODE (t) == MODIFY_EXPR
14922 202 : && TREE_OPERAND (t, 0) == temp)
14923 202 : return RECURSE (TREE_OPERAND (t, 1));
14924 :
14925 : return false;
14926 : }
14927 :
14928 640396 : case CALL_EXPR:
14929 640396 : {
14930 640396 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14931 640396 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14932 :
14933 640396 : return tree_call_nonnegative_p (TREE_TYPE (t),
14934 : get_call_combined_fn (t),
14935 : arg0,
14936 : arg1,
14937 640396 : depth);
14938 : }
14939 3385 : case COMPOUND_EXPR:
14940 3385 : case MODIFY_EXPR:
14941 3385 : return RECURSE (TREE_OPERAND (t, 1));
14942 :
14943 15 : case BIND_EXPR:
14944 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
14945 :
14946 667528 : case SAVE_EXPR:
14947 667528 : return RECURSE (TREE_OPERAND (t, 0));
14948 :
14949 2773 : default:
14950 2773 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14951 : }
14952 : }
14953 :
14954 : #undef RECURSE
14955 : #undef tree_expr_nonnegative_p
14956 :
14957 : /* Return true if T is known to be non-negative.
14958 : DEPTH is the current nesting depth of the query. */
14959 :
14960 : bool
14961 25735672 : tree_expr_nonnegative_p (tree t, int depth)
14962 : {
14963 25735672 : enum tree_code code;
14964 25735672 : if (error_operand_p (t))
14965 : return false;
14966 :
14967 25735671 : code = TREE_CODE (t);
14968 25735671 : switch (TREE_CODE_CLASS (code))
14969 : {
14970 1404944 : case tcc_binary:
14971 1404944 : case tcc_comparison:
14972 1404944 : return tree_binary_nonnegative_p (TREE_CODE (t),
14973 1404944 : TREE_TYPE (t),
14974 1404944 : TREE_OPERAND (t, 0),
14975 1404944 : TREE_OPERAND (t, 1),
14976 1404944 : depth);
14977 :
14978 1985497 : case tcc_unary:
14979 1985497 : return tree_unary_nonnegative_p (TREE_CODE (t),
14980 1985497 : TREE_TYPE (t),
14981 1985497 : TREE_OPERAND (t, 0),
14982 1985497 : depth);
14983 :
14984 12677362 : case tcc_constant:
14985 12677362 : case tcc_declaration:
14986 12677362 : case tcc_reference:
14987 12677362 : return tree_single_nonnegative_p (t, depth);
14988 :
14989 9667868 : default:
14990 9667868 : break;
14991 : }
14992 :
14993 9667868 : switch (code)
14994 : {
14995 7 : case TRUTH_AND_EXPR:
14996 7 : case TRUTH_OR_EXPR:
14997 7 : case TRUTH_XOR_EXPR:
14998 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
14999 7 : TREE_TYPE (t),
15000 7 : TREE_OPERAND (t, 0),
15001 7 : TREE_OPERAND (t, 1),
15002 7 : depth);
15003 72 : case TRUTH_NOT_EXPR:
15004 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15005 72 : TREE_TYPE (t),
15006 72 : TREE_OPERAND (t, 0),
15007 72 : depth);
15008 :
15009 7937876 : case COND_EXPR:
15010 7937876 : case CONSTRUCTOR:
15011 7937876 : case OBJ_TYPE_REF:
15012 7937876 : case ADDR_EXPR:
15013 7937876 : case WITH_SIZE_EXPR:
15014 7937876 : case SSA_NAME:
15015 7937876 : return tree_single_nonnegative_p (t, depth);
15016 :
15017 1729913 : default:
15018 1729913 : return tree_invalid_nonnegative_p (t, depth);
15019 : }
15020 : }
15021 :
15022 :
15023 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15024 : For floating point we further ensure that T is not denormal.
15025 : Similar logic is present in nonzero_address in rtlanal.h. */
15026 :
15027 : bool
15028 1506738 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15029 : {
15030 1506738 : switch (code)
15031 : {
15032 1 : case ABS_EXPR:
15033 1 : return tree_expr_nonzero_p (op0);
15034 :
15035 917196 : case NOP_EXPR:
15036 917196 : {
15037 917196 : tree inner_type = TREE_TYPE (op0);
15038 917196 : tree outer_type = type;
15039 :
15040 917196 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15041 917196 : && tree_expr_nonzero_p (op0));
15042 : }
15043 28113 : break;
15044 :
15045 28113 : case NON_LVALUE_EXPR:
15046 28113 : return tree_expr_nonzero_p (op0);
15047 :
15048 : default:
15049 : break;
15050 : }
15051 :
15052 : return false;
15053 : }
15054 :
15055 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15056 : For floating point we further ensure that T is not denormal.
15057 : Similar logic is present in nonzero_address in rtlanal.h. */
15058 :
15059 : bool
15060 3022522 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15061 : {
15062 3022522 : switch (code)
15063 : {
15064 482216 : case POINTER_PLUS_EXPR:
15065 482216 : case PLUS_EXPR:
15066 482216 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15067 : {
15068 : /* With the presence of negative values it is hard
15069 : to say something. */
15070 108791 : if (!tree_expr_nonnegative_p (op0)
15071 108791 : || !tree_expr_nonnegative_p (op1))
15072 106344 : return false;
15073 : /* One of operands must be positive and the other non-negative. */
15074 2447 : return (tree_expr_nonzero_p (op0)
15075 2447 : || tree_expr_nonzero_p (op1));
15076 : }
15077 : break;
15078 :
15079 19371 : case MULT_EXPR:
15080 19371 : if (TYPE_OVERFLOW_UNDEFINED (type))
15081 : {
15082 538 : if (tree_expr_nonzero_p (op0)
15083 538 : && tree_expr_nonzero_p (op1))
15084 : return true;
15085 : }
15086 : break;
15087 :
15088 : case MIN_EXPR:
15089 : break;
15090 :
15091 31 : case MAX_EXPR:
15092 31 : if (tree_expr_nonzero_p (op0))
15093 : {
15094 :
15095 : /* When both operands are nonzero, then MAX must be too. */
15096 0 : if (tree_expr_nonzero_p (op1))
15097 : return true;
15098 :
15099 : /* MAX where operand 0 is positive is positive. */
15100 0 : return tree_expr_nonnegative_p (op0);
15101 : }
15102 : /* MAX where operand 1 is positive is positive. */
15103 31 : else if (tree_expr_nonzero_p (op1)
15104 31 : && tree_expr_nonnegative_p (op1))
15105 : return true;
15106 : break;
15107 :
15108 268048 : case BIT_IOR_EXPR:
15109 268048 : return (tree_expr_nonzero_p (op1)
15110 268048 : || tree_expr_nonzero_p (op0));
15111 :
15112 : default:
15113 : break;
15114 : }
15115 :
15116 : return false;
15117 : }
15118 :
15119 : /* Return true when T is an address and is known to be nonzero.
15120 : For floating point we further ensure that T is not denormal.
15121 : Similar logic is present in nonzero_address in rtlanal.h. */
15122 :
15123 : bool
15124 153484696 : tree_single_nonzero_p (tree t)
15125 : {
15126 153484696 : switch (TREE_CODE (t))
15127 : {
15128 1158529 : case INTEGER_CST:
15129 1158529 : return !integer_zerop (t);
15130 :
15131 11804901 : case ADDR_EXPR:
15132 11804901 : {
15133 11804901 : tree base = TREE_OPERAND (t, 0);
15134 :
15135 11804901 : if (!DECL_P (base))
15136 5817532 : base = get_base_address (base);
15137 :
15138 11804901 : if (base && TREE_CODE (base) == TARGET_EXPR)
15139 795 : base = TARGET_EXPR_SLOT (base);
15140 :
15141 795 : if (!base)
15142 0 : return false;
15143 :
15144 : /* For objects in symbol table check if we know they are non-zero.
15145 : Don't do anything for variables and functions before symtab is built;
15146 : it is quite possible that they will be declared weak later. */
15147 11804901 : int nonzero_addr = maybe_nonzero_address (base);
15148 11804901 : if (nonzero_addr >= 0)
15149 9540379 : return nonzero_addr;
15150 :
15151 : /* Constants are never weak. */
15152 2264522 : if (CONSTANT_CLASS_P (base))
15153 : return true;
15154 :
15155 : return false;
15156 : }
15157 :
15158 37906 : case COND_EXPR:
15159 37906 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15160 37906 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15161 : return true;
15162 : break;
15163 :
15164 128462014 : case SSA_NAME:
15165 128462014 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15166 : break;
15167 99936132 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15168 :
15169 : default:
15170 : break;
15171 : }
15172 : return false;
15173 : }
15174 :
15175 : #define integer_valued_real_p(X) \
15176 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15177 :
15178 : #define RECURSE(X) \
15179 : ((integer_valued_real_p) (X, depth + 1))
15180 :
15181 : /* Return true if the floating point result of (CODE OP0) has an
15182 : integer value. We also allow +Inf, -Inf and NaN to be considered
15183 : integer values. Return false for signaling NaN.
15184 :
15185 : DEPTH is the current nesting depth of the query. */
15186 :
15187 : bool
15188 15024 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15189 : {
15190 15024 : switch (code)
15191 : {
15192 : case FLOAT_EXPR:
15193 : return true;
15194 :
15195 1403 : case ABS_EXPR:
15196 1403 : return RECURSE (op0);
15197 :
15198 9841 : CASE_CONVERT:
15199 9841 : {
15200 9841 : tree type = TREE_TYPE (op0);
15201 9841 : if (TREE_CODE (type) == INTEGER_TYPE)
15202 : return true;
15203 9841 : if (SCALAR_FLOAT_TYPE_P (type))
15204 9841 : return RECURSE (op0);
15205 : break;
15206 : }
15207 :
15208 : default:
15209 : break;
15210 : }
15211 : return false;
15212 : }
15213 :
15214 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15215 : integer value. We also allow +Inf, -Inf and NaN to be considered
15216 : integer values. Return false for signaling NaN.
15217 :
15218 : DEPTH is the current nesting depth of the query. */
15219 :
15220 : bool
15221 13267 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15222 : {
15223 13267 : switch (code)
15224 : {
15225 7586 : case PLUS_EXPR:
15226 7586 : case MINUS_EXPR:
15227 7586 : case MULT_EXPR:
15228 7586 : case MIN_EXPR:
15229 7586 : case MAX_EXPR:
15230 7586 : return RECURSE (op0) && RECURSE (op1);
15231 :
15232 : default:
15233 : break;
15234 : }
15235 : return false;
15236 : }
15237 :
15238 : /* Return true if the floating point result of calling FNDECL with arguments
15239 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15240 : considered integer values. Return false for signaling NaN. If FNDECL
15241 : takes fewer than 2 arguments, the remaining ARGn are null.
15242 :
15243 : DEPTH is the current nesting depth of the query. */
15244 :
15245 : bool
15246 1089 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15247 : {
15248 1089 : switch (fn)
15249 : {
15250 : CASE_CFN_CEIL:
15251 : CASE_CFN_CEIL_FN:
15252 : CASE_CFN_FLOOR:
15253 : CASE_CFN_FLOOR_FN:
15254 : CASE_CFN_NEARBYINT:
15255 : CASE_CFN_NEARBYINT_FN:
15256 : CASE_CFN_RINT:
15257 : CASE_CFN_RINT_FN:
15258 : CASE_CFN_ROUND:
15259 : CASE_CFN_ROUND_FN:
15260 : CASE_CFN_ROUNDEVEN:
15261 : CASE_CFN_ROUNDEVEN_FN:
15262 : CASE_CFN_TRUNC:
15263 : CASE_CFN_TRUNC_FN:
15264 : return true;
15265 :
15266 336 : CASE_CFN_FMIN:
15267 336 : CASE_CFN_FMIN_FN:
15268 336 : CASE_CFN_FMAX:
15269 336 : CASE_CFN_FMAX_FN:
15270 336 : return RECURSE (arg0) && RECURSE (arg1);
15271 :
15272 : default:
15273 : break;
15274 : }
15275 : return false;
15276 : }
15277 :
15278 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15279 : has an integer value. We also allow +Inf, -Inf and NaN to be
15280 : considered integer values. Return false for signaling NaN.
15281 :
15282 : DEPTH is the current nesting depth of the query. */
15283 :
15284 : bool
15285 127031 : integer_valued_real_single_p (tree t, int depth)
15286 : {
15287 127031 : switch (TREE_CODE (t))
15288 : {
15289 2271 : case REAL_CST:
15290 2271 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15291 :
15292 0 : case COND_EXPR:
15293 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15294 :
15295 89320 : case SSA_NAME:
15296 : /* Limit the depth of recursion to avoid quadratic behavior.
15297 : This is expected to catch almost all occurrences in practice.
15298 : If this code misses important cases that unbounded recursion
15299 : would not, passes that need this information could be revised
15300 : to provide it through dataflow propagation. */
15301 89320 : return (!name_registered_for_update_p (t)
15302 89320 : && depth < param_max_ssa_name_query_depth
15303 177844 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15304 : depth));
15305 :
15306 : default:
15307 : break;
15308 : }
15309 : return false;
15310 : }
15311 :
15312 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15313 : has an integer value. We also allow +Inf, -Inf and NaN to be
15314 : considered integer values. Return false for signaling NaN.
15315 :
15316 : DEPTH is the current nesting depth of the query. */
15317 :
15318 : static bool
15319 0 : integer_valued_real_invalid_p (tree t, int depth)
15320 : {
15321 0 : switch (TREE_CODE (t))
15322 : {
15323 0 : case COMPOUND_EXPR:
15324 0 : case MODIFY_EXPR:
15325 0 : case BIND_EXPR:
15326 0 : return RECURSE (TREE_OPERAND (t, 1));
15327 :
15328 0 : case SAVE_EXPR:
15329 0 : return RECURSE (TREE_OPERAND (t, 0));
15330 :
15331 : default:
15332 : break;
15333 : }
15334 : return false;
15335 : }
15336 :
15337 : #undef RECURSE
15338 : #undef integer_valued_real_p
15339 :
15340 : /* Return true if the floating point expression T has an integer value.
15341 : We also allow +Inf, -Inf and NaN to be considered integer values.
15342 : Return false for signaling NaN.
15343 :
15344 : DEPTH is the current nesting depth of the query. */
15345 :
15346 : bool
15347 96265 : integer_valued_real_p (tree t, int depth)
15348 : {
15349 96265 : if (t == error_mark_node)
15350 : return false;
15351 :
15352 96265 : STRIP_ANY_LOCATION_WRAPPER (t);
15353 :
15354 96265 : tree_code code = TREE_CODE (t);
15355 96265 : switch (TREE_CODE_CLASS (code))
15356 : {
15357 0 : case tcc_binary:
15358 0 : case tcc_comparison:
15359 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15360 0 : TREE_OPERAND (t, 1), depth);
15361 :
15362 0 : case tcc_unary:
15363 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15364 :
15365 8446 : case tcc_constant:
15366 8446 : case tcc_declaration:
15367 8446 : case tcc_reference:
15368 8446 : return integer_valued_real_single_p (t, depth);
15369 :
15370 87819 : default:
15371 87819 : break;
15372 : }
15373 :
15374 87819 : switch (code)
15375 : {
15376 87819 : case COND_EXPR:
15377 87819 : case SSA_NAME:
15378 87819 : return integer_valued_real_single_p (t, depth);
15379 :
15380 0 : case CALL_EXPR:
15381 0 : {
15382 0 : tree arg0 = (call_expr_nargs (t) > 0
15383 0 : ? CALL_EXPR_ARG (t, 0)
15384 0 : : NULL_TREE);
15385 0 : tree arg1 = (call_expr_nargs (t) > 1
15386 0 : ? CALL_EXPR_ARG (t, 1)
15387 0 : : NULL_TREE);
15388 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15389 0 : arg0, arg1, depth);
15390 : }
15391 :
15392 0 : default:
15393 0 : return integer_valued_real_invalid_p (t, depth);
15394 : }
15395 : }
15396 :
15397 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15398 : attempt to fold the expression to a constant without modifying TYPE,
15399 : OP0 or OP1.
15400 :
15401 : If the expression could be simplified to a constant, then return
15402 : the constant. If the expression would not be simplified to a
15403 : constant, then return NULL_TREE. */
15404 :
15405 : tree
15406 15905593 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15407 : {
15408 15905593 : tree tem = fold_binary (code, type, op0, op1);
15409 15905593 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15410 : }
15411 :
15412 : /* Given the components of a unary expression CODE, TYPE and OP0,
15413 : attempt to fold the expression to a constant without modifying
15414 : TYPE or OP0.
15415 :
15416 : If the expression could be simplified to a constant, then return
15417 : the constant. If the expression would not be simplified to a
15418 : constant, then return NULL_TREE. */
15419 :
15420 : tree
15421 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15422 : {
15423 0 : tree tem = fold_unary (code, type, op0);
15424 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15425 : }
15426 :
15427 : /* If EXP represents referencing an element in a constant string
15428 : (either via pointer arithmetic or array indexing), return the
15429 : tree representing the value accessed, otherwise return NULL. */
15430 :
15431 : tree
15432 210223995 : fold_read_from_constant_string (tree exp)
15433 : {
15434 210223995 : if ((INDIRECT_REF_P (exp)
15435 210223976 : || TREE_CODE (exp) == ARRAY_REF)
15436 223593642 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15437 : {
15438 10151803 : tree exp1 = TREE_OPERAND (exp, 0);
15439 10151803 : tree index;
15440 10151803 : tree string;
15441 10151803 : location_t loc = EXPR_LOCATION (exp);
15442 :
15443 10151803 : if (INDIRECT_REF_P (exp))
15444 0 : string = string_constant (exp1, &index, NULL, NULL);
15445 : else
15446 : {
15447 10151803 : tree low_bound = array_ref_low_bound (exp);
15448 10151803 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15449 :
15450 : /* Optimize the special-case of a zero lower bound.
15451 :
15452 : We convert the low_bound to sizetype to avoid some problems
15453 : with constant folding. (E.g. suppose the lower bound is 1,
15454 : and its mode is QI. Without the conversion,l (ARRAY
15455 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15456 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15457 10151803 : if (! integer_zerop (low_bound))
15458 153609 : index = size_diffop_loc (loc, index,
15459 : fold_convert_loc (loc, sizetype, low_bound));
15460 :
15461 : string = exp1;
15462 : }
15463 :
15464 10151803 : scalar_int_mode char_mode;
15465 10151803 : if (string
15466 10151803 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15467 10151803 : && TREE_CODE (string) == STRING_CST
15468 260300 : && tree_fits_uhwi_p (index)
15469 256318 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15470 10407919 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15471 : &char_mode)
15472 20303606 : && GET_MODE_SIZE (char_mode) == 1)
15473 509780 : return build_int_cst_type (TREE_TYPE (exp),
15474 254890 : (TREE_STRING_POINTER (string)
15475 254890 : [TREE_INT_CST_LOW (index)]));
15476 : }
15477 : return NULL;
15478 : }
15479 :
15480 : /* Folds a read from vector element at IDX of vector ARG. */
15481 :
15482 : tree
15483 6689 : fold_read_from_vector (tree arg, poly_uint64 idx)
15484 : {
15485 6689 : unsigned HOST_WIDE_INT i;
15486 6689 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15487 6689 : && known_ge (idx, 0u)
15488 6689 : && idx.is_constant (&i))
15489 : {
15490 6689 : if (TREE_CODE (arg) == VECTOR_CST)
15491 1970 : return VECTOR_CST_ELT (arg, i);
15492 4719 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15493 : {
15494 2575 : if (CONSTRUCTOR_NELTS (arg)
15495 2535 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15496 : return NULL_TREE;
15497 1737 : if (i >= CONSTRUCTOR_NELTS (arg))
15498 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15499 1697 : return CONSTRUCTOR_ELT (arg, i)->value;
15500 : }
15501 : }
15502 : return NULL_TREE;
15503 : }
15504 :
15505 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15506 : an integer constant, real, or fixed-point constant.
15507 :
15508 : TYPE is the type of the result. */
15509 :
15510 : static tree
15511 32617894 : fold_negate_const (tree arg0, tree type)
15512 : {
15513 32617894 : tree t = NULL_TREE;
15514 :
15515 32617894 : switch (TREE_CODE (arg0))
15516 : {
15517 2062270 : case REAL_CST:
15518 2062270 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15519 2062270 : break;
15520 :
15521 0 : case FIXED_CST:
15522 0 : {
15523 0 : FIXED_VALUE_TYPE f;
15524 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15525 0 : &(TREE_FIXED_CST (arg0)), NULL,
15526 0 : TYPE_SATURATING (type));
15527 0 : t = build_fixed (type, f);
15528 : /* Propagate overflow flags. */
15529 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15530 0 : TREE_OVERFLOW (t) = 1;
15531 0 : break;
15532 : }
15533 :
15534 30555624 : default:
15535 30555624 : if (poly_int_tree_p (arg0))
15536 : {
15537 30555624 : wi::overflow_type overflow;
15538 30555624 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15539 30555624 : t = force_fit_type (type, res, 1,
15540 214803 : (overflow && ! TYPE_UNSIGNED (type))
15541 30760310 : || TREE_OVERFLOW (arg0));
15542 30555624 : break;
15543 30555624 : }
15544 :
15545 0 : gcc_unreachable ();
15546 : }
15547 :
15548 32617894 : return t;
15549 : }
15550 :
15551 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15552 : an integer constant or real constant.
15553 :
15554 : TYPE is the type of the result. */
15555 :
15556 : tree
15557 35708 : fold_abs_const (tree arg0, tree type)
15558 : {
15559 35708 : tree t = NULL_TREE;
15560 :
15561 35708 : switch (TREE_CODE (arg0))
15562 : {
15563 7246 : case INTEGER_CST:
15564 7246 : {
15565 : /* If the value is unsigned or non-negative, then the absolute value
15566 : is the same as the ordinary value. */
15567 7246 : wide_int val = wi::to_wide (arg0);
15568 7246 : wi::overflow_type overflow = wi::OVF_NONE;
15569 7246 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15570 : ;
15571 :
15572 : /* If the value is negative, then the absolute value is
15573 : its negation. */
15574 : else
15575 3143 : val = wi::neg (val, &overflow);
15576 :
15577 : /* Force to the destination type, set TREE_OVERFLOW for signed
15578 : TYPE only. */
15579 7246 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15580 7246 : }
15581 7246 : break;
15582 :
15583 28462 : case REAL_CST:
15584 28462 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15585 7597 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15586 : else
15587 : t = arg0;
15588 : break;
15589 :
15590 0 : default:
15591 0 : gcc_unreachable ();
15592 : }
15593 :
15594 35708 : return t;
15595 : }
15596 :
15597 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15598 : constant. TYPE is the type of the result. */
15599 :
15600 : static tree
15601 2303304 : fold_not_const (const_tree arg0, tree type)
15602 : {
15603 2303304 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15604 :
15605 2303304 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15606 : }
15607 :
15608 : /* Given CODE, a relational operator, the target type, TYPE and two
15609 : constant operands OP0 and OP1, return the result of the
15610 : relational operation. If the result is not a compile time
15611 : constant, then return NULL_TREE. */
15612 :
15613 : static tree
15614 82263149 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15615 : {
15616 82263149 : int result, invert;
15617 :
15618 : /* From here on, the only cases we handle are when the result is
15619 : known to be a constant. */
15620 :
15621 82263149 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15622 : {
15623 1220683 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15624 1220683 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15625 :
15626 : /* Handle the cases where either operand is a NaN. */
15627 1220683 : if (real_isnan (c0) || real_isnan (c1))
15628 : {
15629 13524 : switch (code)
15630 : {
15631 : case EQ_EXPR:
15632 : case ORDERED_EXPR:
15633 : result = 0;
15634 : break;
15635 :
15636 : case NE_EXPR:
15637 : case UNORDERED_EXPR:
15638 : case UNLT_EXPR:
15639 : case UNLE_EXPR:
15640 : case UNGT_EXPR:
15641 : case UNGE_EXPR:
15642 : case UNEQ_EXPR:
15643 6608 : result = 1;
15644 : break;
15645 :
15646 6959 : case LT_EXPR:
15647 6959 : case LE_EXPR:
15648 6959 : case GT_EXPR:
15649 6959 : case GE_EXPR:
15650 6959 : case LTGT_EXPR:
15651 6959 : if (flag_trapping_math)
15652 : return NULL_TREE;
15653 : result = 0;
15654 : break;
15655 :
15656 0 : default:
15657 0 : gcc_unreachable ();
15658 : }
15659 :
15660 6608 : return constant_boolean_node (result, type);
15661 : }
15662 :
15663 1207159 : return constant_boolean_node (real_compare (code, c0, c1), type);
15664 : }
15665 :
15666 81042466 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15667 : {
15668 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15669 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15670 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15671 : }
15672 :
15673 : /* Handle equality/inequality of complex constants. */
15674 81042466 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15675 : {
15676 58558 : tree rcond = fold_relational_const (code, type,
15677 29279 : TREE_REALPART (op0),
15678 29279 : TREE_REALPART (op1));
15679 117116 : tree icond = fold_relational_const (code, type,
15680 29279 : TREE_IMAGPART (op0),
15681 29279 : TREE_IMAGPART (op1));
15682 29279 : if (code == EQ_EXPR)
15683 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15684 28977 : else if (code == NE_EXPR)
15685 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15686 : else
15687 : return NULL_TREE;
15688 : }
15689 :
15690 81013187 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15691 : {
15692 20182 : if (!VECTOR_TYPE_P (type))
15693 : {
15694 : /* Have vector comparison with scalar boolean result. */
15695 172 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15696 : && known_eq (VECTOR_CST_NELTS (op0),
15697 : VECTOR_CST_NELTS (op1)));
15698 172 : unsigned HOST_WIDE_INT nunits;
15699 172 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15700 : return NULL_TREE;
15701 479 : for (unsigned i = 0; i < nunits; i++)
15702 : {
15703 420 : tree elem0 = VECTOR_CST_ELT (op0, i);
15704 420 : tree elem1 = VECTOR_CST_ELT (op1, i);
15705 420 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15706 420 : if (tmp == NULL_TREE)
15707 : return NULL_TREE;
15708 420 : if (integer_zerop (tmp))
15709 113 : return constant_boolean_node (code == NE_EXPR, type);
15710 : }
15711 59 : return constant_boolean_node (code == EQ_EXPR, type);
15712 : }
15713 20010 : tree_vector_builder elts;
15714 20010 : if (!elts.new_binary_operation (type, op0, op1, false))
15715 : return NULL_TREE;
15716 20010 : unsigned int count = elts.encoded_nelts ();
15717 72946 : for (unsigned i = 0; i < count; i++)
15718 : {
15719 52936 : tree elem_type = TREE_TYPE (type);
15720 52936 : tree elem0 = VECTOR_CST_ELT (op0, i);
15721 52936 : tree elem1 = VECTOR_CST_ELT (op1, i);
15722 :
15723 52936 : tree tem = fold_relational_const (code, elem_type,
15724 : elem0, elem1);
15725 :
15726 52936 : if (tem == NULL_TREE)
15727 : return NULL_TREE;
15728 :
15729 52936 : elts.quick_push (build_int_cst (elem_type,
15730 84660 : integer_zerop (tem) ? 0 : -1));
15731 : }
15732 :
15733 20010 : return elts.build ();
15734 20010 : }
15735 :
15736 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15737 :
15738 : To compute GT, swap the arguments and do LT.
15739 : To compute GE, do LT and invert the result.
15740 : To compute LE, swap the arguments, do LT and invert the result.
15741 : To compute NE, do EQ and invert the result.
15742 :
15743 : Therefore, the code below must handle only EQ and LT. */
15744 :
15745 80993005 : if (code == LE_EXPR || code == GT_EXPR)
15746 : {
15747 14168148 : std::swap (op0, op1);
15748 14168148 : code = swap_tree_comparison (code);
15749 : }
15750 :
15751 : /* Note that it is safe to invert for real values here because we
15752 : have already handled the one case that it matters. */
15753 :
15754 80993005 : invert = 0;
15755 80993005 : if (code == NE_EXPR || code == GE_EXPR)
15756 : {
15757 36520045 : invert = 1;
15758 36520045 : code = invert_tree_comparison (code, false);
15759 : }
15760 :
15761 : /* Compute a result for LT or EQ if args permit;
15762 : Otherwise return T. */
15763 80993005 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15764 : {
15765 80964844 : if (code == EQ_EXPR)
15766 39852846 : result = tree_int_cst_equal (op0, op1);
15767 : else
15768 41111998 : result = tree_int_cst_lt (op0, op1);
15769 : }
15770 : else
15771 : return NULL_TREE;
15772 :
15773 80964844 : if (invert)
15774 36518205 : result ^= 1;
15775 80964844 : return constant_boolean_node (result, type);
15776 : }
15777 :
15778 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15779 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15780 : itself. */
15781 :
15782 : tree
15783 136419714 : fold_build_cleanup_point_expr (tree type, tree expr)
15784 : {
15785 : /* If the expression does not have side effects then we don't have to wrap
15786 : it with a cleanup point expression. */
15787 136419714 : if (!TREE_SIDE_EFFECTS (expr))
15788 : return expr;
15789 :
15790 : /* If the expression is a return, check to see if the expression inside the
15791 : return has no side effects or the right hand side of the modify expression
15792 : inside the return. If either don't have side effects set we don't need to
15793 : wrap the expression in a cleanup point expression. Note we don't check the
15794 : left hand side of the modify because it should always be a return decl. */
15795 116745188 : if (TREE_CODE (expr) == RETURN_EXPR)
15796 : {
15797 46137755 : tree op = TREE_OPERAND (expr, 0);
15798 46137755 : if (!op || !TREE_SIDE_EFFECTS (op))
15799 : return expr;
15800 45430531 : op = TREE_OPERAND (op, 1);
15801 45430531 : if (!TREE_SIDE_EFFECTS (op))
15802 : return expr;
15803 : }
15804 :
15805 92179785 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15806 : }
15807 :
15808 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15809 : of an indirection through OP0, or NULL_TREE if no simplification is
15810 : possible. */
15811 :
15812 : tree
15813 22288705 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15814 : {
15815 22288705 : tree sub = op0;
15816 22288705 : tree subtype;
15817 22288705 : poly_uint64 const_op01;
15818 :
15819 22288705 : STRIP_NOPS (sub);
15820 22288705 : subtype = TREE_TYPE (sub);
15821 22288705 : if (!POINTER_TYPE_P (subtype)
15822 22288705 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15823 : return NULL_TREE;
15824 :
15825 22136076 : if (TREE_CODE (sub) == ADDR_EXPR)
15826 : {
15827 5006663 : tree op = TREE_OPERAND (sub, 0);
15828 5006663 : tree optype = TREE_TYPE (op);
15829 :
15830 : /* *&CONST_DECL -> to the value of the const decl. */
15831 5006663 : if (TREE_CODE (op) == CONST_DECL)
15832 3214 : return DECL_INITIAL (op);
15833 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15834 5003449 : if (type == optype)
15835 : {
15836 3781629 : tree fop = fold_read_from_constant_string (op);
15837 3781629 : if (fop)
15838 : return fop;
15839 : else
15840 3736269 : return op;
15841 : }
15842 : /* *(foo *)&fooarray => fooarray[0] */
15843 1221820 : else if (TREE_CODE (optype) == ARRAY_TYPE
15844 13838 : && type == TREE_TYPE (optype)
15845 1234513 : && (!in_gimple_form
15846 3119 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15847 : {
15848 12693 : tree type_domain = TYPE_DOMAIN (optype);
15849 12693 : tree min_val = size_zero_node;
15850 12693 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15851 12654 : min_val = TYPE_MIN_VALUE (type_domain);
15852 12693 : if (in_gimple_form
15853 3119 : && TREE_CODE (min_val) != INTEGER_CST)
15854 : return NULL_TREE;
15855 12693 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15856 12693 : NULL_TREE, NULL_TREE);
15857 : }
15858 : /* *(foo *)&complexfoo => __real__ complexfoo */
15859 1209127 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15860 1209127 : && type == TREE_TYPE (optype))
15861 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15862 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15863 1209127 : else if (VECTOR_TYPE_P (optype)
15864 1209127 : && type == TREE_TYPE (optype))
15865 : {
15866 70 : tree part_width = TYPE_SIZE (type);
15867 70 : tree index = bitsize_int (0);
15868 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15869 70 : index);
15870 : }
15871 : }
15872 :
15873 18338470 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15874 18338470 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15875 : {
15876 261913 : tree op00 = TREE_OPERAND (sub, 0);
15877 261913 : tree op01 = TREE_OPERAND (sub, 1);
15878 :
15879 261913 : STRIP_NOPS (op00);
15880 261913 : if (TREE_CODE (op00) == ADDR_EXPR)
15881 : {
15882 2030 : tree op00type;
15883 2030 : op00 = TREE_OPERAND (op00, 0);
15884 2030 : op00type = TREE_TYPE (op00);
15885 :
15886 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15887 2030 : if (VECTOR_TYPE_P (op00type)
15888 240 : && type == TREE_TYPE (op00type)
15889 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15890 : but we want to treat offsets with MSB set as negative.
15891 : For the code below negative offsets are invalid and
15892 : TYPE_SIZE of the element is something unsigned, so
15893 : check whether op01 fits into poly_int64, which implies
15894 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15895 : then just use poly_uint64 because we want to treat the
15896 : value as unsigned. */
15897 2223 : && tree_fits_poly_int64_p (op01))
15898 : {
15899 179 : tree part_width = TYPE_SIZE (type);
15900 179 : poly_uint64 max_offset
15901 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15902 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15903 179 : if (known_lt (const_op01, max_offset))
15904 : {
15905 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15906 179 : return fold_build3_loc (loc,
15907 : BIT_FIELD_REF, type, op00,
15908 179 : part_width, index);
15909 : }
15910 : }
15911 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15912 1851 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
15913 1851 : && type == TREE_TYPE (op00type))
15914 : {
15915 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
15916 : const_op01))
15917 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15918 : }
15919 : /* ((foo *)&fooarray)[1] => fooarray[1] */
15920 1851 : else if (TREE_CODE (op00type) == ARRAY_TYPE
15921 1851 : && type == TREE_TYPE (op00type))
15922 : {
15923 719 : tree type_domain = TYPE_DOMAIN (op00type);
15924 719 : tree min_val = size_zero_node;
15925 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15926 718 : min_val = TYPE_MIN_VALUE (type_domain);
15927 719 : poly_uint64 type_size, index;
15928 719 : if (poly_int_tree_p (min_val)
15929 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
15930 719 : && multiple_p (const_op01, type_size, &index))
15931 : {
15932 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
15933 719 : op01 = wide_int_to_tree (sizetype, off);
15934 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
15935 : NULL_TREE, NULL_TREE);
15936 : }
15937 : }
15938 : }
15939 : }
15940 :
15941 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15942 18337572 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15943 679266 : && type == TREE_TYPE (TREE_TYPE (subtype))
15944 18340632 : && (!in_gimple_form
15945 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15946 : {
15947 3059 : tree type_domain;
15948 3059 : tree min_val = size_zero_node;
15949 3059 : sub = build_fold_indirect_ref_loc (loc, sub);
15950 3059 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15951 3059 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15952 3059 : min_val = TYPE_MIN_VALUE (type_domain);
15953 3059 : if (in_gimple_form
15954 11 : && TREE_CODE (min_val) != INTEGER_CST)
15955 : return NULL_TREE;
15956 3059 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15957 3059 : NULL_TREE);
15958 : }
15959 :
15960 : return NULL_TREE;
15961 : }
15962 :
15963 : /* Builds an expression for an indirection through T, simplifying some
15964 : cases. */
15965 :
15966 : tree
15967 11575405 : build_fold_indirect_ref_loc (location_t loc, tree t)
15968 : {
15969 11575405 : tree type = TREE_TYPE (TREE_TYPE (t));
15970 11575405 : tree sub = fold_indirect_ref_1 (loc, type, t);
15971 :
15972 11575405 : if (sub)
15973 : return sub;
15974 :
15975 7797660 : return build1_loc (loc, INDIRECT_REF, type, t);
15976 : }
15977 :
15978 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
15979 :
15980 : tree
15981 10345544 : fold_indirect_ref_loc (location_t loc, tree t)
15982 : {
15983 10345544 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15984 :
15985 10345544 : if (sub)
15986 : return sub;
15987 : else
15988 10324353 : return t;
15989 : }
15990 :
15991 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
15992 : whose result is ignored. The type of the returned tree need not be
15993 : the same as the original expression. */
15994 :
15995 : tree
15996 135448 : fold_ignored_result (tree t)
15997 : {
15998 135448 : if (!TREE_SIDE_EFFECTS (t))
15999 17922 : return integer_zero_node;
16000 :
16001 156722 : for (;;)
16002 156722 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16003 : {
16004 3838 : case tcc_unary:
16005 3838 : t = TREE_OPERAND (t, 0);
16006 3838 : break;
16007 :
16008 5116 : case tcc_binary:
16009 5116 : case tcc_comparison:
16010 5116 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16011 3201 : t = TREE_OPERAND (t, 0);
16012 1915 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16013 43 : t = TREE_OPERAND (t, 1);
16014 : else
16015 : return t;
16016 : break;
16017 :
16018 100370 : case tcc_expression:
16019 100370 : switch (TREE_CODE (t))
16020 : {
16021 32115 : case COMPOUND_EXPR:
16022 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16023 : return t;
16024 31820 : t = TREE_OPERAND (t, 0);
16025 31820 : break;
16026 :
16027 381 : case COND_EXPR:
16028 381 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16029 381 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16030 : return t;
16031 294 : t = TREE_OPERAND (t, 0);
16032 294 : break;
16033 :
16034 : default:
16035 : return t;
16036 : }
16037 : break;
16038 :
16039 : default:
16040 : return t;
16041 : }
16042 : }
16043 :
16044 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16045 :
16046 : tree
16047 3131065479 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16048 : {
16049 3131065479 : tree div = NULL_TREE;
16050 :
16051 3131065479 : if (divisor == 1)
16052 : return value;
16053 :
16054 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16055 : have to do anything. Only do this when we are not given a const,
16056 : because in that case, this check is more expensive than just
16057 : doing it. */
16058 1949489500 : if (TREE_CODE (value) != INTEGER_CST)
16059 : {
16060 374083 : div = build_int_cst (TREE_TYPE (value), divisor);
16061 :
16062 374083 : if (multiple_of_p (TREE_TYPE (value), value, div))
16063 : return value;
16064 : }
16065 :
16066 : /* If divisor is a power of two, simplify this to bit manipulation. */
16067 1949117323 : if (pow2_or_zerop (divisor))
16068 : {
16069 1949117323 : if (TREE_CODE (value) == INTEGER_CST)
16070 : {
16071 1949115417 : wide_int val = wi::to_wide (value);
16072 1949115417 : bool overflow_p;
16073 :
16074 1949115417 : if ((val & (divisor - 1)) == 0)
16075 : return value;
16076 :
16077 4095921 : overflow_p = TREE_OVERFLOW (value);
16078 4095921 : val += divisor - 1;
16079 4095921 : val &= (int) -divisor;
16080 4095921 : if (val == 0)
16081 4 : overflow_p = true;
16082 :
16083 4095921 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16084 1949115417 : }
16085 : else
16086 : {
16087 1906 : tree t;
16088 :
16089 1906 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16090 1906 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16091 1906 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16092 1906 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16093 : }
16094 : }
16095 : else
16096 : {
16097 0 : if (!div)
16098 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16099 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16100 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16101 : }
16102 :
16103 : return value;
16104 : }
16105 :
16106 : /* Likewise, but round down. */
16107 :
16108 : tree
16109 21676047 : round_down_loc (location_t loc, tree value, int divisor)
16110 : {
16111 21676047 : tree div = NULL_TREE;
16112 :
16113 21676047 : gcc_assert (divisor > 0);
16114 21676047 : if (divisor == 1)
16115 : return value;
16116 :
16117 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16118 : have to do anything. Only do this when we are not given a const,
16119 : because in that case, this check is more expensive than just
16120 : doing it. */
16121 21676047 : if (TREE_CODE (value) != INTEGER_CST)
16122 : {
16123 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16124 :
16125 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16126 : return value;
16127 : }
16128 :
16129 : /* If divisor is a power of two, simplify this to bit manipulation. */
16130 21676047 : if (pow2_or_zerop (divisor))
16131 : {
16132 21676047 : tree t;
16133 :
16134 21676047 : t = build_int_cst (TREE_TYPE (value), -divisor);
16135 21676047 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16136 : }
16137 : else
16138 : {
16139 0 : if (!div)
16140 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16141 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16142 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16143 : }
16144 :
16145 : return value;
16146 : }
16147 :
16148 : /* Returns the pointer to the base of the object addressed by EXP and
16149 : extracts the information about the offset of the access, storing it
16150 : to PBITPOS and POFFSET. */
16151 :
16152 : static tree
16153 2425028 : split_address_to_core_and_offset (tree exp,
16154 : poly_int64 *pbitpos, tree *poffset)
16155 : {
16156 2425028 : tree core;
16157 2425028 : machine_mode mode;
16158 2425028 : int unsignedp, reversep, volatilep;
16159 2425028 : poly_int64 bitsize;
16160 2425028 : location_t loc = EXPR_LOCATION (exp);
16161 :
16162 2425028 : if (TREE_CODE (exp) == SSA_NAME)
16163 452172 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16164 336294 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16165 31158 : exp = gimple_assign_rhs1 (def);
16166 :
16167 2425028 : if (TREE_CODE (exp) == ADDR_EXPR)
16168 : {
16169 1348212 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16170 : poffset, &mode, &unsignedp, &reversep,
16171 : &volatilep);
16172 : /* If we are left with MEM[a + CST] strip that and add it to the
16173 : pbitpos and return a. */
16174 1348212 : if (TREE_CODE (core) == MEM_REF)
16175 : {
16176 27930 : poly_offset_int tem;
16177 27930 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16178 27930 : tem <<= LOG2_BITS_PER_UNIT;
16179 27930 : tem += *pbitpos;
16180 27930 : if (tem.to_shwi (pbitpos))
16181 27752 : return TREE_OPERAND (core, 0);
16182 : }
16183 1320460 : core = build_fold_addr_expr_loc (loc, core);
16184 : }
16185 1076816 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16186 : {
16187 434071 : core = TREE_OPERAND (exp, 0);
16188 434071 : STRIP_NOPS (core);
16189 434071 : *pbitpos = 0;
16190 434071 : *poffset = TREE_OPERAND (exp, 1);
16191 434071 : if (poly_int_tree_p (*poffset))
16192 : {
16193 433986 : poly_offset_int tem
16194 433986 : = wi::sext (wi::to_poly_offset (*poffset),
16195 433986 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16196 433986 : tem <<= LOG2_BITS_PER_UNIT;
16197 433986 : if (tem.to_shwi (pbitpos))
16198 433986 : *poffset = NULL_TREE;
16199 : }
16200 : }
16201 : else
16202 : {
16203 642745 : core = exp;
16204 642745 : *pbitpos = 0;
16205 642745 : *poffset = NULL_TREE;
16206 : }
16207 :
16208 : return core;
16209 : }
16210 :
16211 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16212 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16213 :
16214 : bool
16215 1212514 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16216 : {
16217 1212514 : tree core1, core2;
16218 1212514 : poly_int64 bitpos1, bitpos2;
16219 1212514 : tree toffset1, toffset2, tdiff, type;
16220 :
16221 1212514 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16222 1212514 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16223 :
16224 1212514 : poly_int64 bytepos1, bytepos2;
16225 1212514 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16226 1854350 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16227 2425028 : || !operand_equal_p (core1, core2, 0))
16228 641836 : return false;
16229 :
16230 570678 : if (toffset1 && toffset2)
16231 : {
16232 29 : type = TREE_TYPE (toffset1);
16233 29 : if (type != TREE_TYPE (toffset2))
16234 0 : toffset2 = fold_convert (type, toffset2);
16235 :
16236 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16237 29 : if (!cst_and_fits_in_hwi (tdiff))
16238 : return false;
16239 :
16240 15 : *diff = int_cst_value (tdiff);
16241 : }
16242 570649 : else if (toffset1 || toffset2)
16243 : {
16244 : /* If only one of the offsets is non-constant, the difference cannot
16245 : be a constant. */
16246 : return false;
16247 : }
16248 : else
16249 552279 : *diff = 0;
16250 :
16251 552294 : *diff += bytepos1 - bytepos2;
16252 552294 : return true;
16253 : }
16254 :
16255 : /* Return OFF converted to a pointer offset type suitable as offset for
16256 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16257 : tree
16258 51266524 : convert_to_ptrofftype_loc (location_t loc, tree off)
16259 : {
16260 51266524 : if (ptrofftype_p (TREE_TYPE (off)))
16261 : return off;
16262 5985468 : return fold_convert_loc (loc, sizetype, off);
16263 : }
16264 :
16265 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16266 : tree
16267 45373048 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16268 : {
16269 45373048 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16270 45373048 : ptr, convert_to_ptrofftype_loc (loc, off));
16271 : }
16272 :
16273 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16274 : tree
16275 165178 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16276 : {
16277 165178 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16278 165178 : ptr, size_int (off));
16279 : }
16280 :
16281 : /* Return a pointer to a NUL-terminated string containing the sequence
16282 : of bytes corresponding to the representation of the object referred to
16283 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16284 : to an initialized constant array plus some constant offset).
16285 : Set *STRSIZE the number of bytes in the constant sequence including
16286 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16287 : where A is the array that stores the constant sequence that SRC points
16288 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16289 : need not point to a string or even an array of characters but may point
16290 : to an object of any type. */
16291 :
16292 : const char *
16293 12554807 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16294 : {
16295 : /* The offset into the array A storing the string, and A's byte size. */
16296 12554807 : tree offset_node;
16297 12554807 : tree mem_size;
16298 :
16299 12554807 : if (strsize)
16300 4676555 : *strsize = 0;
16301 :
16302 12554807 : if (strsize)
16303 4676555 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16304 : else
16305 7878252 : src = string_constant (src, &offset_node, &mem_size, NULL);
16306 12554807 : if (!src)
16307 : return NULL;
16308 :
16309 2841159 : unsigned HOST_WIDE_INT offset = 0;
16310 2841159 : if (offset_node != NULL_TREE)
16311 : {
16312 2841159 : if (!tree_fits_uhwi_p (offset_node))
16313 : return NULL;
16314 : else
16315 2839395 : offset = tree_to_uhwi (offset_node);
16316 : }
16317 :
16318 2839395 : if (!tree_fits_uhwi_p (mem_size))
16319 : return NULL;
16320 :
16321 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16322 : is stored in and equal to sizeof A. INIT_BYTES is the number
16323 : of bytes in the constant sequence used to initialize the array,
16324 : including any embedded NULs as well as the terminating NUL (for
16325 : strings), but not including any trailing zeros/NULs past
16326 : the terminating one appended implicitly to a string literal to
16327 : zero out the remainder of the array it's stored in. For example,
16328 : given:
16329 : const char a[7] = "abc\0d";
16330 : n = strlen (a + 1);
16331 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16332 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16333 : is equal to strlen (A) + 1. */
16334 2839395 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16335 2839395 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16336 2839395 : const char *string = TREE_STRING_POINTER (src);
16337 :
16338 : /* Ideally this would turn into a gcc_checking_assert over time. */
16339 2839395 : if (init_bytes > array_size)
16340 : init_bytes = array_size;
16341 :
16342 2839395 : if (init_bytes == 0 || offset >= array_size)
16343 : return NULL;
16344 :
16345 2838130 : if (strsize)
16346 : {
16347 : /* Compute and store the number of characters from the beginning
16348 : of the substring at OFFSET to the end, including the terminating
16349 : nul. Offsets past the initial length refer to null strings. */
16350 1441844 : if (offset < init_bytes)
16351 1441844 : *strsize = init_bytes - offset;
16352 : else
16353 0 : *strsize = 1;
16354 : }
16355 : else
16356 : {
16357 1396286 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16358 : /* Support only properly NUL-terminated single byte strings. */
16359 1396286 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16360 : return NULL;
16361 1391511 : if (string[init_bytes - 1] != '\0')
16362 : return NULL;
16363 : }
16364 :
16365 2810089 : return offset < init_bytes ? string + offset : "";
16366 : }
16367 :
16368 : /* Return a pointer to a NUL-terminated string corresponding to
16369 : the expression STR referencing a constant string, possibly
16370 : involving a constant offset. Return null if STR either doesn't
16371 : reference a constant string or if it involves a nonconstant
16372 : offset. */
16373 :
16374 : const char *
16375 7878252 : c_getstr (tree str)
16376 : {
16377 7878252 : return getbyterep (str, NULL);
16378 : }
16379 :
16380 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16381 : may be nonzero, with precision PREC, the precision of T's type. */
16382 :
16383 : static wide_int
16384 254394164 : tree_nonzero_bits (const_tree t, unsigned prec)
16385 : {
16386 254394164 : switch (TREE_CODE (t))
16387 : {
16388 8857160 : case INTEGER_CST:
16389 8857160 : return wi::to_wide (t);
16390 141511003 : case SSA_NAME:
16391 141511003 : return get_nonzero_bits (t);
16392 258994 : case NON_LVALUE_EXPR:
16393 258994 : case SAVE_EXPR:
16394 258994 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16395 482043 : case BIT_AND_EXPR:
16396 964086 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16397 1446129 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16398 5452 : case BIT_IOR_EXPR:
16399 5452 : case BIT_XOR_EXPR:
16400 10904 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16401 16356 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16402 66320 : case COND_EXPR:
16403 132640 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16404 198960 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16405 53532722 : CASE_CONVERT:
16406 53532722 : if (TREE_TYPE (t) != error_mark_node
16407 53532722 : && !error_operand_p (TREE_OPERAND (t, 0)))
16408 : {
16409 53532721 : tree op0 = TREE_OPERAND (t, 0);
16410 53532721 : tree inner_type = TREE_TYPE (op0);
16411 53532721 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16412 107065442 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16413 107065442 : prec, TYPE_SIGN (inner_type));
16414 : }
16415 : break;
16416 14333215 : case PLUS_EXPR:
16417 14333215 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16418 : {
16419 14333215 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16420 14333215 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16421 14333215 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16422 558941 : return wi::bit_or (nzbits1, nzbits2);
16423 14333215 : }
16424 : break;
16425 170355 : case LSHIFT_EXPR:
16426 170355 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16427 170355 : && TREE_TYPE (t) != error_mark_node)
16428 : {
16429 98469 : tree type = TREE_TYPE (t);
16430 98469 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16431 98469 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16432 98469 : return wi::neg_p (arg1)
16433 196938 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16434 98469 : : wi::lshift (nzbits, arg1);
16435 98469 : }
16436 : break;
16437 159984 : case RSHIFT_EXPR:
16438 159984 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16439 159984 : && TREE_TYPE (t) != error_mark_node)
16440 : {
16441 158087 : tree type = TREE_TYPE (t);
16442 158087 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16443 158087 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16444 158087 : return wi::neg_p (arg1)
16445 316174 : ? wi::lshift (nzbits, -arg1)
16446 158087 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16447 158087 : }
16448 : break;
16449 : default:
16450 : break;
16451 : }
16452 :
16453 48864974 : return wi::shwi (-1, prec);
16454 : }
16455 :
16456 : /* Given a tree T, compute which bits in T may be nonzero. */
16457 :
16458 : wide_int
16459 170571833 : tree_nonzero_bits (const_tree t)
16460 : {
16461 170571833 : if (error_operand_p (t))
16462 0 : return wi::shwi (-1, 64);
16463 170571833 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16464 : }
16465 :
16466 : /* Helper function for address compare simplifications in match.pd.
16467 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16468 : TYPE is the type of comparison operands.
16469 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16470 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16471 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16472 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16473 : and 2 if unknown. */
16474 :
16475 : int
16476 5180004 : address_compare (tree_code code, tree type, tree op0, tree op1,
16477 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16478 : bool generic)
16479 : {
16480 5180004 : if (TREE_CODE (op0) == SSA_NAME)
16481 36801 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16482 5180004 : if (TREE_CODE (op1) == SSA_NAME)
16483 4555 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16484 5180004 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16485 5180004 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16486 5180004 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16487 5180004 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16488 5180004 : if (base0 && TREE_CODE (base0) == MEM_REF)
16489 : {
16490 36319 : off0 += mem_ref_offset (base0).force_shwi ();
16491 36319 : base0 = TREE_OPERAND (base0, 0);
16492 : }
16493 5180004 : if (base1 && TREE_CODE (base1) == MEM_REF)
16494 : {
16495 3714 : off1 += mem_ref_offset (base1).force_shwi ();
16496 3714 : base1 = TREE_OPERAND (base1, 0);
16497 : }
16498 5180004 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16499 : return 2;
16500 :
16501 5167797 : int equal = 2;
16502 : /* Punt in GENERIC on variables with value expressions;
16503 : the value expressions might point to fields/elements
16504 : of other vars etc. */
16505 5167797 : if (generic
16506 5167797 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16507 5032223 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16508 : return 2;
16509 5167140 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16510 : {
16511 1253909 : symtab_node *node0 = symtab_node::get_create (base0);
16512 1253909 : symtab_node *node1 = symtab_node::get_create (base1);
16513 1253909 : equal = node0->equal_address_to (node1);
16514 : }
16515 3913231 : else if ((DECL_P (base0)
16516 245677 : || TREE_CODE (base0) == SSA_NAME
16517 210477 : || TREE_CODE (base0) == STRING_CST)
16518 3913033 : && (DECL_P (base1)
16519 214139 : || TREE_CODE (base1) == SSA_NAME
16520 210651 : || TREE_CODE (base1) == STRING_CST))
16521 3913018 : equal = (base0 == base1);
16522 : /* Assume different STRING_CSTs with the same content will be
16523 : merged. */
16524 5166927 : if (equal == 0
16525 82990 : && TREE_CODE (base0) == STRING_CST
16526 17626 : && TREE_CODE (base1) == STRING_CST
16527 17451 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16528 5166927 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16529 6189 : TREE_STRING_LENGTH (base0)) == 0)
16530 : equal = 1;
16531 5162707 : if (equal == 1)
16532 : {
16533 5063541 : if (code == EQ_EXPR
16534 5063541 : || code == NE_EXPR
16535 : /* If the offsets are equal we can ignore overflow. */
16536 128577 : || known_eq (off0, off1)
16537 256930 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16538 : /* Or if we compare using pointers to decls or strings. */
16539 5192006 : || (POINTER_TYPE_P (type)
16540 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16541 : return 1;
16542 : return 2;
16543 : }
16544 103599 : if (equal != 0)
16545 : return equal;
16546 78557 : if (code != EQ_EXPR && code != NE_EXPR)
16547 : return 2;
16548 :
16549 : /* At this point we know (or assume) the two pointers point at
16550 : different objects. */
16551 73292 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16552 73292 : off0.is_constant (&ioff0);
16553 73292 : off1.is_constant (&ioff1);
16554 : /* Punt on non-zero offsets from functions. */
16555 73292 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16556 73292 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16557 : return 2;
16558 : /* Or if the bases are neither decls nor string literals. */
16559 73292 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16560 : return 2;
16561 38828 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16562 : return 2;
16563 : /* For initializers, assume addresses of different functions are
16564 : different. */
16565 38828 : if (folding_initializer
16566 13528 : && TREE_CODE (base0) == FUNCTION_DECL
16567 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16568 : return 0;
16569 :
16570 : /* Compute whether one address points to the start of one
16571 : object and another one to the end of another one. */
16572 38808 : poly_int64 size0 = 0, size1 = 0;
16573 38808 : if (TREE_CODE (base0) == STRING_CST)
16574 : {
16575 13039 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16576 : equal = 2;
16577 : else
16578 : size0 = TREE_STRING_LENGTH (base0);
16579 : }
16580 25769 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16581 : size0 = 1;
16582 : else
16583 : {
16584 25461 : tree sz0 = DECL_SIZE_UNIT (base0);
16585 25461 : if (!tree_fits_poly_int64_p (sz0))
16586 : equal = 2;
16587 : else
16588 25461 : size0 = tree_to_poly_int64 (sz0);
16589 : }
16590 38808 : if (TREE_CODE (base1) == STRING_CST)
16591 : {
16592 13144 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16593 : equal = 2;
16594 : else
16595 : size1 = TREE_STRING_LENGTH (base1);
16596 : }
16597 25664 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16598 : size1 = 1;
16599 : else
16600 : {
16601 25360 : tree sz1 = DECL_SIZE_UNIT (base1);
16602 25360 : if (!tree_fits_poly_int64_p (sz1))
16603 : equal = 2;
16604 : else
16605 25360 : size1 = tree_to_poly_int64 (sz1);
16606 : }
16607 38808 : if (equal == 0)
16608 : {
16609 : /* If one offset is pointing (or could be) to the beginning of one
16610 : object and the other is pointing to one past the last byte of the
16611 : other object, punt. */
16612 38796 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16613 : equal = 2;
16614 38659 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16615 : equal = 2;
16616 : /* If both offsets are the same, there are some cases we know that are
16617 : ok. Either if we know they aren't zero, or if we know both sizes
16618 : are no zero. */
16619 : if (equal == 2
16620 273 : && known_eq (off0, off1)
16621 22 : && (known_ne (off0, 0)
16622 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16623 : equal = 0;
16624 : }
16625 :
16626 : /* At this point, equal is 2 if either one or both pointers are out of
16627 : bounds of their object, or one points to start of its object and the
16628 : other points to end of its object. This is unspecified behavior
16629 : e.g. in C++. Otherwise equal is 0. */
16630 38808 : if (folding_cxx_constexpr && equal)
16631 : return equal;
16632 :
16633 : /* When both pointers point to string literals, even when equal is 0,
16634 : due to tail merging of string literals the pointers might be the same. */
16635 38745 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16636 : {
16637 13000 : if (ioff0 < 0
16638 13000 : || ioff1 < 0
16639 13000 : || ioff0 > TREE_STRING_LENGTH (base0)
16640 25988 : || ioff1 > TREE_STRING_LENGTH (base1))
16641 : return 2;
16642 :
16643 : /* If the bytes in the string literals starting at the pointers
16644 : differ, the pointers need to be different. */
16645 12988 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16646 12988 : TREE_STRING_POINTER (base1) + ioff1,
16647 12988 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16648 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16649 : {
16650 3910 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16651 3910 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16652 3910 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16653 : ioffmin) == 0)
16654 : /* If even the bytes in the string literal before the
16655 : pointers are the same, the string literals could be
16656 : tail merged. */
16657 : return 2;
16658 : }
16659 : return 0;
16660 : }
16661 :
16662 25745 : if (folding_cxx_constexpr)
16663 : return 0;
16664 :
16665 : /* If this is a pointer comparison, ignore for now even
16666 : valid equalities where one pointer is the offset zero
16667 : of one object and the other to one past end of another one. */
16668 12340 : if (!INTEGRAL_TYPE_P (type))
16669 : return 0;
16670 :
16671 : /* Assume that string literals can't be adjacent to variables
16672 : (automatic or global). */
16673 312 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16674 : return 0;
16675 :
16676 : /* Assume that automatic variables can't be adjacent to global
16677 : variables. */
16678 292 : if (is_global_var (base0) != is_global_var (base1))
16679 : return 0;
16680 :
16681 : return equal;
16682 : }
16683 :
16684 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16685 : tree
16686 52 : ctor_single_nonzero_element (const_tree t)
16687 : {
16688 52 : unsigned HOST_WIDE_INT idx;
16689 52 : constructor_elt *ce;
16690 52 : tree elt = NULL_TREE;
16691 :
16692 52 : if (TREE_CODE (t) != CONSTRUCTOR)
16693 : return NULL_TREE;
16694 113 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16695 110 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16696 : {
16697 101 : if (elt)
16698 : return NULL_TREE;
16699 52 : elt = ce->value;
16700 : }
16701 : return elt;
16702 : }
16703 :
16704 : #if CHECKING_P
16705 :
16706 : namespace selftest {
16707 :
16708 : /* Helper functions for writing tests of folding trees. */
16709 :
16710 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16711 :
16712 : static void
16713 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16714 : tree constant)
16715 : {
16716 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16717 16 : }
16718 :
16719 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16720 : wrapping WRAPPED_EXPR. */
16721 :
16722 : static void
16723 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16724 : tree wrapped_expr)
16725 : {
16726 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16727 12 : ASSERT_NE (wrapped_expr, result);
16728 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16729 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16730 12 : }
16731 :
16732 : /* Verify that various arithmetic binary operations are folded
16733 : correctly. */
16734 :
16735 : static void
16736 4 : test_arithmetic_folding ()
16737 : {
16738 4 : tree type = integer_type_node;
16739 4 : tree x = create_tmp_var_raw (type, "x");
16740 4 : tree zero = build_zero_cst (type);
16741 4 : tree one = build_int_cst (type, 1);
16742 :
16743 : /* Addition. */
16744 : /* 1 <-- (0 + 1) */
16745 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16746 : one);
16747 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16748 : one);
16749 :
16750 : /* (nonlvalue)x <-- (x + 0) */
16751 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16752 : x);
16753 :
16754 : /* Subtraction. */
16755 : /* 0 <-- (x - x) */
16756 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16757 : zero);
16758 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16759 : x);
16760 :
16761 : /* Multiplication. */
16762 : /* 0 <-- (x * 0) */
16763 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16764 : zero);
16765 :
16766 : /* (nonlvalue)x <-- (x * 1) */
16767 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16768 : x);
16769 4 : }
16770 :
16771 : namespace test_operand_equality {
16772 :
16773 : /* Verify structural equality. */
16774 :
16775 : /* Execute fold_vec_perm_cst unit tests. */
16776 :
16777 : static void
16778 4 : test ()
16779 : {
16780 4 : tree stype = integer_type_node;
16781 4 : tree utype = unsigned_type_node;
16782 4 : tree x = create_tmp_var_raw (stype, "x");
16783 4 : tree y = create_tmp_var_raw (stype, "y");
16784 4 : tree z = create_tmp_var_raw (stype, "z");
16785 4 : tree four = build_int_cst (stype, 4);
16786 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16787 4 : tree rhs1 = fold_convert (stype,
16788 : fold_build2 (PLUS_EXPR, utype,
16789 : fold_convert (utype, x),
16790 : fold_convert (utype, y)));
16791 :
16792 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16793 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16794 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16795 :
16796 : /* (int)(unsigned) x == x. */
16797 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16798 : build1 (NOP_EXPR, utype, x));
16799 4 : tree rhs2 = x;
16800 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16801 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16802 :
16803 : /* (unsigned x) + (unsigned y) == x + y. */
16804 4 : tree lhs3 = lhs1;
16805 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16806 : fold_convert (utype, x),
16807 : fold_convert (utype, y));
16808 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16809 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16810 :
16811 : /* (unsigned x) / (unsigned y) == x / y. */
16812 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16813 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16814 : fold_convert (utype, x),
16815 : fold_convert (utype, y));
16816 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16817 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16818 :
16819 : /* (long x) / 4 == (long)(x / 4). */
16820 4 : tree lstype = long_long_integer_type_node;
16821 4 : tree lfour = build_int_cst (lstype, 4);
16822 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16823 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16824 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16825 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16826 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16827 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16828 :
16829 : /* (unsigned x) / 4 == x / 4. */
16830 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16831 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16832 : fold_convert (utype, x),
16833 : fold_convert (utype, four));
16834 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16835 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16836 :
16837 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16838 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16839 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16840 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16841 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16842 :
16843 : /* (unsigned x) + 4 == x + 4. */
16844 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16845 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16846 : fold_convert (utype, x),
16847 : fold_convert (utype, four));
16848 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16849 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16850 :
16851 : /* (unsigned x) + 4 == 4 + x. */
16852 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16853 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16854 : fold_convert (utype, x),
16855 : fold_convert (utype, four));
16856 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16857 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16858 :
16859 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16860 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16861 : fold_build2 (MULT_EXPR, stype,
16862 : fold_build2 (PLUS_EXPR, stype, four, x),
16863 : y),
16864 : z);
16865 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16866 : fold_build2 (PLUS_EXPR, utype,
16867 : fold_convert (utype, x),
16868 : fold_convert (utype, four)),
16869 : fold_convert (utype, y));
16870 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16871 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16872 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16873 4 : }
16874 : }
16875 :
16876 : namespace test_fold_vec_perm_cst {
16877 :
16878 : /* Build a VECTOR_CST corresponding to VMODE, and has
16879 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16880 : Fill it with randomized elements, using rand() % THRESHOLD. */
16881 :
16882 : static tree
16883 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16884 : unsigned nelts_per_pattern,
16885 : int step = 0, bool natural_stepped = false,
16886 : int threshold = 100)
16887 : {
16888 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16889 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16890 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16891 :
16892 : // Fill a0 for each pattern
16893 0 : for (unsigned i = 0; i < npatterns; i++)
16894 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16895 :
16896 0 : if (nelts_per_pattern == 1)
16897 0 : return builder.build ();
16898 :
16899 : // Fill a1 for each pattern
16900 0 : for (unsigned i = 0; i < npatterns; i++)
16901 : {
16902 0 : tree a1;
16903 0 : if (natural_stepped)
16904 : {
16905 0 : tree a0 = builder[i];
16906 0 : wide_int a0_val = wi::to_wide (a0);
16907 0 : wide_int a1_val = a0_val + step;
16908 0 : a1 = wide_int_to_tree (inner_type, a1_val);
16909 0 : }
16910 : else
16911 0 : a1 = build_int_cst (inner_type, rand () % threshold);
16912 0 : builder.quick_push (a1);
16913 : }
16914 0 : if (nelts_per_pattern == 2)
16915 0 : return builder.build ();
16916 :
16917 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
16918 : {
16919 0 : tree prev_elem = builder[i - npatterns];
16920 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
16921 0 : wide_int val = prev_elem_val + step;
16922 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
16923 0 : }
16924 :
16925 0 : return builder.build ();
16926 0 : }
16927 :
16928 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16929 : when result is VLA. */
16930 :
16931 : static void
16932 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
16933 : tree res, tree *expected_res)
16934 : {
16935 : /* Actual npatterns and encoded_elts in res may be less than expected due
16936 : to canonicalization. */
16937 0 : ASSERT_TRUE (res != NULL_TREE);
16938 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
16939 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
16940 :
16941 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
16942 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16943 0 : }
16944 :
16945 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16946 : when the result is VLS. */
16947 :
16948 : static void
16949 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
16950 : {
16951 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
16952 0 : for (unsigned i = 0; i < expected_nelts; i++)
16953 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16954 0 : }
16955 :
16956 : /* Helper routine to push multiple elements into BUILDER. */
16957 : template<unsigned N>
16958 0 : static void builder_push_elems (vec_perm_builder& builder,
16959 : poly_uint64 (&elems)[N])
16960 : {
16961 0 : for (unsigned i = 0; i < N; i++)
16962 0 : builder.quick_push (elems[i]);
16963 0 : }
16964 :
16965 : #define ARG0(index) vector_cst_elt (arg0, index)
16966 : #define ARG1(index) vector_cst_elt (arg1, index)
16967 :
16968 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
16969 :
16970 : static void
16971 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
16972 : {
16973 0 : for (int i = 0; i < 10; i++)
16974 : {
16975 : /* Case 1:
16976 : sel = { 0, 4, 1, 5, ... }
16977 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
16978 0 : {
16979 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16980 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16981 :
16982 0 : tree inner_type
16983 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
16984 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
16985 :
16986 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16987 0 : vec_perm_builder builder (res_len, 4, 1);
16988 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
16989 0 : builder_push_elems (builder, mask_elems);
16990 :
16991 0 : vec_perm_indices sel (builder, 2, res_len);
16992 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
16993 :
16994 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
16995 0 : validate_res (4, 1, res, expected_res);
16996 0 : }
16997 :
16998 : /* Case 2: Same as case 1, but contains an out of bounds access which
16999 : should wrap around.
17000 : sel = {0, 8, 4, 12, ...} (4, 1)
17001 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17002 0 : {
17003 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17004 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17005 :
17006 0 : tree inner_type
17007 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17008 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17009 :
17010 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17011 0 : vec_perm_builder builder (res_len, 4, 1);
17012 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17013 0 : builder_push_elems (builder, mask_elems);
17014 :
17015 0 : vec_perm_indices sel (builder, 2, res_len);
17016 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17017 :
17018 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17019 0 : validate_res (4, 1, res, expected_res);
17020 0 : }
17021 : }
17022 0 : }
17023 :
17024 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17025 :
17026 : static void
17027 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17028 : {
17029 0 : for (int i = 0; i < 10; i++)
17030 : {
17031 : /* Case 1:
17032 : sel = { 0, 1, 2, 3}
17033 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17034 0 : {
17035 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17036 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17037 :
17038 0 : tree inner_type
17039 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17040 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17041 :
17042 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17043 0 : vec_perm_builder builder (res_len, 4, 1);
17044 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17045 0 : builder_push_elems (builder, mask_elems);
17046 :
17047 0 : vec_perm_indices sel (builder, 2, res_len);
17048 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17049 :
17050 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17051 0 : validate_res_vls (res, expected_res, 4);
17052 0 : }
17053 :
17054 : /* Case 2: Same as Case 1, but crossing input vector.
17055 : sel = {0, 2, 4, 6}
17056 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17057 : Since we cannot determine, which vector to choose from during
17058 : compile time, should return NULL_TREE. */
17059 0 : {
17060 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17061 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17062 :
17063 0 : tree inner_type
17064 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17065 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17066 :
17067 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17068 0 : vec_perm_builder builder (res_len, 4, 1);
17069 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17070 0 : builder_push_elems (builder, mask_elems);
17071 :
17072 0 : vec_perm_indices sel (builder, 2, res_len);
17073 0 : const char *reason;
17074 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17075 :
17076 0 : ASSERT_TRUE (res == NULL_TREE);
17077 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17078 0 : }
17079 : }
17080 0 : }
17081 :
17082 : /* Test all input vectors. */
17083 :
17084 : static void
17085 0 : test_all_nunits (machine_mode vmode)
17086 : {
17087 : /* Test with 10 different inputs. */
17088 0 : for (int i = 0; i < 10; i++)
17089 : {
17090 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17091 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17092 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17093 :
17094 : /* Case 1: mask = {0, ...} // (1, 1)
17095 : res = { arg0[0], ... } // (1, 1) */
17096 0 : {
17097 0 : vec_perm_builder builder (len, 1, 1);
17098 0 : builder.quick_push (0);
17099 0 : vec_perm_indices sel (builder, 2, len);
17100 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17101 0 : tree expected_res[] = { ARG0(0) };
17102 0 : validate_res (1, 1, res, expected_res);
17103 0 : }
17104 :
17105 : /* Case 2: mask = {len, ...} // (1, 1)
17106 : res = { arg1[0], ... } // (1, 1) */
17107 0 : {
17108 0 : vec_perm_builder builder (len, 1, 1);
17109 0 : builder.quick_push (len);
17110 0 : vec_perm_indices sel (builder, 2, len);
17111 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17112 :
17113 0 : tree expected_res[] = { ARG1(0) };
17114 0 : validate_res (1, 1, res, expected_res);
17115 0 : }
17116 : }
17117 0 : }
17118 :
17119 : /* Test all vectors which contain at-least 2 elements. */
17120 :
17121 : static void
17122 0 : test_nunits_min_2 (machine_mode vmode)
17123 : {
17124 0 : for (int i = 0; i < 10; i++)
17125 : {
17126 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17127 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17128 0 : {
17129 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17130 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17131 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17132 :
17133 0 : vec_perm_builder builder (len, 2, 1);
17134 0 : poly_uint64 mask_elems[] = { 0, len };
17135 0 : builder_push_elems (builder, mask_elems);
17136 :
17137 0 : vec_perm_indices sel (builder, 2, len);
17138 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17139 :
17140 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17141 0 : validate_res (2, 1, res, expected_res);
17142 0 : }
17143 :
17144 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17145 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17146 0 : {
17147 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17148 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17149 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17150 :
17151 0 : vec_perm_builder builder (len, 2, 2);
17152 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17153 0 : builder_push_elems (builder, mask_elems);
17154 :
17155 0 : vec_perm_indices sel (builder, 2, len);
17156 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17157 :
17158 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17159 0 : validate_res (2, 2, res, expected_res);
17160 0 : }
17161 :
17162 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17163 : Test that the stepped sequence of the pattern selects from
17164 : same input pattern. Since input vectors have npatterns = 2,
17165 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17166 : in input vector. So return NULL_TREE. */
17167 0 : {
17168 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17169 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17170 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17171 :
17172 0 : vec_perm_builder builder (len, 1, 3);
17173 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17174 0 : builder_push_elems (builder, mask_elems);
17175 :
17176 0 : vec_perm_indices sel (builder, 2, len);
17177 0 : const char *reason;
17178 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17179 : &reason);
17180 0 : ASSERT_TRUE (res == NULL_TREE);
17181 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17182 0 : }
17183 :
17184 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17185 : Test that stepped sequence of the pattern selects from arg0.
17186 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17187 0 : {
17188 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17189 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17190 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17191 :
17192 0 : vec_perm_builder builder (len, 1, 3);
17193 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17194 0 : builder_push_elems (builder, mask_elems);
17195 :
17196 0 : vec_perm_indices sel (builder, 2, len);
17197 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17198 :
17199 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17200 0 : validate_res (1, 3, res, expected_res);
17201 0 : }
17202 :
17203 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17204 : In this case ensure that arg has a natural stepped sequence
17205 : to preserve arg's encoding.
17206 :
17207 : As a concrete example, consider:
17208 : arg0: { -16, -9, -10, ... } // (1, 3)
17209 : arg1: { -12, -5, -6, ... } // (1, 3)
17210 : sel = { 0, len, len + 1, ... } // (1, 3)
17211 :
17212 : This will create res with following encoding:
17213 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17214 : = { -16, -12, -5, ... }
17215 :
17216 : The step in above encoding would be: (-5) - (-12) = 7
17217 : And hence res[3] would be computed as -5 + 7 = 2.
17218 : instead of arg1[2], ie, -6.
17219 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17220 : for this case. */
17221 0 : {
17222 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17223 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17224 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17225 :
17226 0 : vec_perm_builder builder (len, 1, 3);
17227 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17228 0 : builder_push_elems (builder, mask_elems);
17229 :
17230 0 : vec_perm_indices sel (builder, 2, len);
17231 0 : const char *reason;
17232 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17233 : stepped pattern, even if we didn't explicitly tell it to. So folding
17234 : may not always fail, but if it does, ensure that's because arg1 does
17235 : not have a natural stepped sequence (and not due to other reason) */
17236 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17237 0 : if (res == NULL_TREE)
17238 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17239 0 : }
17240 :
17241 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17242 : sequence and thus folding should be valid for this case. */
17243 0 : {
17244 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17245 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17246 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17247 :
17248 0 : vec_perm_builder builder (len, 1, 3);
17249 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17250 0 : builder_push_elems (builder, mask_elems);
17251 :
17252 0 : vec_perm_indices sel (builder, 2, len);
17253 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17254 :
17255 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17256 0 : validate_res (1, 3, res, expected_res);
17257 0 : }
17258 :
17259 : /* Case 8: Same as aarch64/sve/slp_3.c:
17260 : arg0, arg1 are dup vectors.
17261 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17262 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17263 :
17264 : In this case, since the input vectors are dup, only the first two
17265 : elements per pattern in sel are considered significant. */
17266 0 : {
17267 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17268 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17269 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17270 :
17271 0 : vec_perm_builder builder (len, 2, 3);
17272 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17273 0 : builder_push_elems (builder, mask_elems);
17274 :
17275 0 : vec_perm_indices sel (builder, 2, len);
17276 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17277 :
17278 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17279 0 : validate_res (2, 1, res, expected_res);
17280 0 : }
17281 : }
17282 0 : }
17283 :
17284 : /* Test all vectors which contain at-least 4 elements. */
17285 :
17286 : static void
17287 0 : test_nunits_min_4 (machine_mode vmode)
17288 : {
17289 0 : for (int i = 0; i < 10; i++)
17290 : {
17291 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17292 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17293 0 : {
17294 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17295 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17296 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17297 :
17298 0 : vec_perm_builder builder (len, 4, 1);
17299 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17300 0 : builder_push_elems (builder, mask_elems);
17301 :
17302 0 : vec_perm_indices sel (builder, 2, len);
17303 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17304 :
17305 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17306 0 : validate_res (4, 1, res, expected_res);
17307 0 : }
17308 :
17309 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17310 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17311 0 : {
17312 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17313 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17314 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17315 :
17316 0 : vec_perm_builder builder (arg0_len, 1, 3);
17317 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17318 0 : builder_push_elems (builder, mask_elems);
17319 :
17320 0 : vec_perm_indices sel (builder, 2, arg0_len);
17321 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17322 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17323 0 : validate_res (1, 3, res, expected_res);
17324 0 : }
17325 :
17326 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17327 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17328 0 : {
17329 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17330 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17331 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17332 :
17333 0 : vec_perm_builder builder (len, 1, 3);
17334 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17335 0 : builder_push_elems (builder, mask_elems);
17336 :
17337 0 : vec_perm_indices sel (builder, 2, len);
17338 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17339 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17340 0 : validate_res (1, 3, res, expected_res);
17341 0 : }
17342 :
17343 : /* Case 4:
17344 : sel = { len, 0, 2, ... } // (1, 3)
17345 : This should return NULL because we cross the input vectors.
17346 : Because,
17347 : Let's assume len = C + Cx
17348 : a1 = 0
17349 : S = 2
17350 : esel = arg0_len / sel_npatterns = C + Cx
17351 : ae = 0 + (esel - 2) * S
17352 : = 0 + (C + Cx - 2) * 2
17353 : = 2(C-2) + 2Cx
17354 :
17355 : For C >= 4:
17356 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17357 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17358 : Since q1 != qe, we cross input vectors.
17359 : So return NULL_TREE. */
17360 0 : {
17361 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17362 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17363 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17364 :
17365 0 : vec_perm_builder builder (arg0_len, 1, 3);
17366 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17367 0 : builder_push_elems (builder, mask_elems);
17368 :
17369 0 : vec_perm_indices sel (builder, 2, arg0_len);
17370 0 : const char *reason;
17371 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17372 0 : ASSERT_TRUE (res == NULL_TREE);
17373 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17374 0 : }
17375 :
17376 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17377 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17378 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17379 :
17380 : Note that fold_vec_perm_cst will set
17381 : res_npatterns = max(4, max(4, 2)) = 4
17382 : However after canonicalizing, we will end up with shape (2, 2). */
17383 0 : {
17384 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17385 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17386 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17387 :
17388 0 : vec_perm_builder builder (len, 2, 2);
17389 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17390 0 : builder_push_elems (builder, mask_elems);
17391 :
17392 0 : vec_perm_indices sel (builder, 2, len);
17393 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17394 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17395 0 : validate_res (2, 2, res, expected_res);
17396 0 : }
17397 :
17398 : /* Case 6: Test combination in sel, where one pattern is dup and other
17399 : is stepped sequence.
17400 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17401 : res = { arg0[0], arg0[0], arg0[0],
17402 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17403 0 : {
17404 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17405 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17406 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17407 :
17408 0 : vec_perm_builder builder (len, 2, 3);
17409 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17410 0 : builder_push_elems (builder, mask_elems);
17411 :
17412 0 : vec_perm_indices sel (builder, 2, len);
17413 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17414 :
17415 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17416 0 : ARG0(1), ARG0(0), ARG0(2) };
17417 0 : validate_res (2, 3, res, expected_res);
17418 0 : }
17419 :
17420 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17421 : when arg0, arg1 and sel have different number of patterns.
17422 : arg0 is of shape (1, 1)
17423 : arg1 is of shape (4, 1)
17424 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17425 :
17426 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17427 : However,
17428 : step = (len+2) - (len+1) = 1
17429 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17430 : Since step is not a multiple of arg_npatterns,
17431 : valid_mask_for_fold_vec_perm_cst should return false,
17432 : and thus fold_vec_perm_cst should return NULL_TREE. */
17433 0 : {
17434 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17435 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17436 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17437 :
17438 0 : vec_perm_builder builder (len, 2, 3);
17439 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17440 0 : builder_push_elems (builder, mask_elems);
17441 :
17442 0 : vec_perm_indices sel (builder, 2, len);
17443 0 : const char *reason;
17444 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17445 :
17446 0 : ASSERT_TRUE (res == NULL_TREE);
17447 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17448 0 : }
17449 :
17450 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17451 : check that the result is not a stepped sequence either, even
17452 : if sel has a stepped sequence. */
17453 0 : {
17454 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17455 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17456 :
17457 0 : vec_perm_builder builder (len, 1, 3);
17458 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17459 0 : builder_push_elems (builder, mask_elems);
17460 :
17461 0 : vec_perm_indices sel (builder, 1, len);
17462 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17463 :
17464 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17465 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17466 0 : }
17467 :
17468 : /* Case 9: If sel doesn't contain a stepped sequence,
17469 : check that the result has same encoding as sel, irrespective
17470 : of shape of input vectors. */
17471 0 : {
17472 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17473 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17474 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17475 :
17476 0 : vec_perm_builder builder (len, 1, 2);
17477 0 : poly_uint64 mask_elems[] = { 0, len };
17478 0 : builder_push_elems (builder, mask_elems);
17479 :
17480 0 : vec_perm_indices sel (builder, 2, len);
17481 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17482 :
17483 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17484 0 : validate_res (sel.encoding ().npatterns (),
17485 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17486 0 : }
17487 : }
17488 0 : }
17489 :
17490 : /* Test all vectors which contain at-least 8 elements. */
17491 :
17492 : static void
17493 0 : test_nunits_min_8 (machine_mode vmode)
17494 : {
17495 0 : for (int i = 0; i < 10; i++)
17496 : {
17497 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17498 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17499 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17500 : arg0[2], arg0[0], arg0[3], arg1[0],
17501 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17502 0 : {
17503 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17504 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17505 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17506 :
17507 0 : vec_perm_builder builder(len, 4, 3);
17508 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17509 0 : 4, 0, 5, len };
17510 0 : builder_push_elems (builder, mask_elems);
17511 :
17512 0 : vec_perm_indices sel (builder, 2, len);
17513 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17514 :
17515 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17516 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17517 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17518 0 : validate_res (4, 3, res, expected_res);
17519 0 : }
17520 : }
17521 0 : }
17522 :
17523 : /* Test vectors for which nunits[0] <= 4. */
17524 :
17525 : static void
17526 0 : test_nunits_max_4 (machine_mode vmode)
17527 : {
17528 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17529 : This should return NULL_TREE because the index 4 may choose
17530 : from either arg0 or arg1 depending on vector length. */
17531 0 : {
17532 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17533 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17534 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17535 :
17536 0 : vec_perm_builder builder (len, 1, 2);
17537 0 : poly_uint64 mask_elems[] = {0, 4};
17538 0 : builder_push_elems (builder, mask_elems);
17539 :
17540 0 : vec_perm_indices sel (builder, 2, len);
17541 0 : const char *reason;
17542 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17543 0 : ASSERT_TRUE (res == NULL_TREE);
17544 0 : ASSERT_TRUE (reason != NULL);
17545 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17546 0 : }
17547 0 : }
17548 :
17549 : #undef ARG0
17550 : #undef ARG1
17551 :
17552 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17553 :
17554 : static bool
17555 0 : is_simple_vla_size (poly_uint64 size)
17556 : {
17557 124 : if (size.is_constant ()
17558 : || !pow2p_hwi (size.coeffs[0]))
17559 0 : return false;
17560 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17561 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17562 : return false;
17563 : return true;
17564 : }
17565 :
17566 : /* Execute fold_vec_perm_cst unit tests. */
17567 :
17568 : static void
17569 4 : test ()
17570 : {
17571 4 : machine_mode vnx4si_mode = E_VOIDmode;
17572 4 : machine_mode v4si_mode = E_VOIDmode;
17573 :
17574 4 : machine_mode vmode;
17575 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17576 : {
17577 : /* Obtain modes corresponding to VNx4SI and V4SI,
17578 : to call mixed mode tests below.
17579 : FIXME: Is there a better way to do this ? */
17580 124 : if (GET_MODE_INNER (vmode) == SImode)
17581 : {
17582 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17583 124 : if (is_simple_vla_size (nunits)
17584 : && nunits.coeffs[0] == 4)
17585 : vnx4si_mode = vmode;
17586 124 : else if (known_eq (nunits, poly_uint64 (4)))
17587 124 : v4si_mode = vmode;
17588 : }
17589 :
17590 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17591 : || !targetm.vector_mode_supported_p (vmode))
17592 124 : continue;
17593 :
17594 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17595 : test_all_nunits (vmode);
17596 : if (nunits.coeffs[0] >= 2)
17597 : test_nunits_min_2 (vmode);
17598 : if (nunits.coeffs[0] >= 4)
17599 : test_nunits_min_4 (vmode);
17600 : if (nunits.coeffs[0] >= 8)
17601 : test_nunits_min_8 (vmode);
17602 :
17603 : if (nunits.coeffs[0] <= 4)
17604 : test_nunits_max_4 (vmode);
17605 : }
17606 :
17607 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17608 : && targetm.vector_mode_supported_p (vnx4si_mode)
17609 : && targetm.vector_mode_supported_p (v4si_mode))
17610 : {
17611 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17612 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17613 : }
17614 4 : }
17615 : } // end of test_fold_vec_perm_cst namespace
17616 :
17617 : /* Verify that various binary operations on vectors are folded
17618 : correctly. */
17619 :
17620 : static void
17621 4 : test_vector_folding ()
17622 : {
17623 4 : tree inner_type = integer_type_node;
17624 4 : tree type = build_vector_type (inner_type, 4);
17625 4 : tree zero = build_zero_cst (type);
17626 4 : tree one = build_one_cst (type);
17627 4 : tree index = build_index_vector (type, 0, 1);
17628 :
17629 : /* Verify equality tests that return a scalar boolean result. */
17630 4 : tree res_type = boolean_type_node;
17631 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17632 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17633 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17634 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17635 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17636 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17637 : index, one)));
17638 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17639 : index, index)));
17640 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17641 : index, index)));
17642 4 : }
17643 :
17644 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17645 :
17646 : static void
17647 4 : test_vec_duplicate_folding ()
17648 : {
17649 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17650 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17651 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17652 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17653 :
17654 4 : tree type = build_vector_type (ssizetype, nunits);
17655 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17656 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17657 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17658 4 : }
17659 :
17660 : /* Run all of the selftests within this file. */
17661 :
17662 : void
17663 4 : fold_const_cc_tests ()
17664 : {
17665 4 : test_arithmetic_folding ();
17666 4 : test_vector_folding ();
17667 4 : test_vec_duplicate_folding ();
17668 4 : test_fold_vec_perm_cst::test ();
17669 4 : test_operand_equality::test ();
17670 4 : }
17671 :
17672 : } // namespace selftest
17673 :
17674 : #endif /* CHECKING_P */
|