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 138387 : minmax_from_comparison (tree_code cmp, tree exp0,
158 : const widest_int cst1,
159 : const widest_int cst2)
160 : {
161 138387 : if (cst1 == cst2)
162 : {
163 139 : if (cmp == LE_EXPR || cmp == LT_EXPR)
164 : return MIN_EXPR;
165 120 : if (cmp == GT_EXPR || cmp == GE_EXPR)
166 : return MAX_EXPR;
167 : }
168 138368 : if (cst1 == cst2 - 1)
169 : {
170 : /* X <= Y - 1 equals to X < Y. */
171 82556 : if (cmp == LE_EXPR)
172 : return MIN_EXPR;
173 : /* X > Y - 1 equals to X >= Y. */
174 82114 : 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 70196 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : {
179 18375 : int_range_max r;
180 36750 : get_range_query (cfun)->range_of_expr (r, exp0);
181 18375 : if (r.undefined_p ())
182 0 : r.set_varying (TREE_TYPE (exp0));
183 :
184 18375 : widest_int min = widest_int::from (r.lower_bound (),
185 36750 : TYPE_SIGN (TREE_TYPE (exp0)));
186 18375 : if (min == cst1)
187 760 : return MAX_EXPR;
188 18375 : }
189 : }
190 125248 : if (cst1 == cst2 + 1)
191 : {
192 : /* X < Y + 1 equals to X <= Y. */
193 1258 : if (cmp == LT_EXPR)
194 : return MIN_EXPR;
195 : /* X >= Y + 1 equals to X > Y. */
196 1230 : 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 1068 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : {
201 660 : int_range_max r;
202 1320 : get_range_query (cfun)->range_of_expr (r, exp0);
203 660 : if (r.undefined_p ())
204 0 : r.set_varying (TREE_TYPE (exp0));
205 :
206 660 : widest_int max = widest_int::from (r.upper_bound (),
207 1320 : TYPE_SIGN (TREE_TYPE (exp0)));
208 660 : if (max == cst1)
209 186 : return MIN_EXPR;
210 660 : }
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 170685 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
220 : {
221 170685 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
222 11 : return ERROR_MARK;
223 :
224 170674 : if (!operand_equal_p (exp0, exp2))
225 : return ERROR_MARK;
226 :
227 170674 : if (operand_equal_p (exp1, exp3))
228 : {
229 32222 : if (cmp == LT_EXPR || cmp == LE_EXPR)
230 : return MIN_EXPR;
231 30092 : if (cmp == GT_EXPR || cmp == GE_EXPR)
232 : return MAX_EXPR;
233 : }
234 138572 : if (TREE_CODE (exp3) == INTEGER_CST
235 138100 : && TREE_CODE (exp1) == INTEGER_CST)
236 137642 : 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 3025119 : expr_location_or (tree t, location_t loc)
245 : {
246 948674 : location_t tloc = EXPR_LOCATION (t);
247 3008815 : 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 9164628 : protected_set_expr_location_unshare (tree x, location_t loc)
255 : {
256 9164628 : if (CAN_HAVE_LOCATION_P (x)
257 8136956 : && EXPR_LOCATION (x) != loc
258 2515392 : && !(TREE_CODE (x) == SAVE_EXPR
259 1257911 : || TREE_CODE (x) == TARGET_EXPR
260 : || TREE_CODE (x) == BIND_EXPR))
261 : {
262 1257146 : x = copy_node (x);
263 1257146 : SET_EXPR_LOCATION (x, loc);
264 : }
265 9164628 : 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 2162084 : negate_mathfn_p (combined_fn fn)
273 : {
274 2162084 : 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 414 : CASE_CFN_LLRINT:
334 414 : CASE_CFN_LLRINT_FN:
335 414 : CASE_CFN_LRINT:
336 414 : CASE_CFN_LRINT_FN:
337 414 : CASE_CFN_NEARBYINT:
338 414 : CASE_CFN_NEARBYINT_FN:
339 414 : CASE_CFN_RINT:
340 414 : CASE_CFN_RINT_FN:
341 414 : return !flag_rounding_math;
342 :
343 2158024 : default:
344 2158024 : break;
345 : }
346 2158024 : return false;
347 : }
348 :
349 : /* Check whether we may negate an integer constant T without causing
350 : overflow. */
351 :
352 : bool
353 3161163 : may_negate_without_overflow_p (const_tree t)
354 : {
355 3161163 : tree type;
356 :
357 3161163 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
358 :
359 3161163 : type = TREE_TYPE (t);
360 3161163 : if (TYPE_UNSIGNED (type))
361 : return false;
362 :
363 3161163 : 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 27660908 : negate_expr_p (tree t)
371 : {
372 27818496 : tree type;
373 :
374 27818496 : if (t == 0)
375 : return false;
376 :
377 27818496 : type = TREE_TYPE (t);
378 :
379 27818496 : STRIP_SIGN_NOPS (t);
380 27818496 : switch (TREE_CODE (t))
381 : {
382 1569991 : case INTEGER_CST:
383 1569991 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
384 : return true;
385 :
386 : /* Check that -CST will not overflow type. */
387 376660 : return may_negate_without_overflow_p (t);
388 559 : case BIT_NOT_EXPR:
389 559 : return (INTEGRAL_TYPE_P (type)
390 559 : && 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 1295511 : case REAL_CST:
399 : /* We want to canonicalize to positive real constants. Pretend
400 : that only negative ones can be easily negated. */
401 1295511 : 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 1519645 : case PLUS_EXPR:
429 1519645 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
430 1519639 : || HONOR_SIGNED_ZEROS (type)
431 2751450 : || (ANY_INTEGRAL_TYPE_P (type)
432 1231623 : && ! TYPE_OVERFLOW_WRAPS (type)))
433 743950 : return false;
434 : /* -(A + B) -> (-B) - A. */
435 775695 : if (negate_expr_p (TREE_OPERAND (t, 1)))
436 : return true;
437 : /* -(A + B) -> (-A) - B. */
438 147271 : return negate_expr_p (TREE_OPERAND (t, 0));
439 :
440 260297 : case MINUS_EXPR:
441 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 260297 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
443 260297 : && !HONOR_SIGNED_ZEROS (type)
444 346758 : && (! ANY_INTEGRAL_TYPE_P (type)
445 86238 : || TYPE_OVERFLOW_WRAPS (type));
446 :
447 2368720 : case MULT_EXPR:
448 2368720 : 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 4109928 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
453 157609 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
454 2210291 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
455 0 : && (wi::popcount
456 2054964 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
457 155327 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 132257 : && (wi::popcount
459 4219115 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
460 : break;
461 :
462 : /* Fall through. */
463 :
464 2330043 : case RDIV_EXPR:
465 2330043 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
466 2330042 : return negate_expr_p (TREE_OPERAND (t, 1))
467 2330042 : || 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 5065050 : case NOP_EXPR:
491 : /* Negate -((double)float) as (double)(-float). */
492 5065050 : if (SCALAR_FLOAT_TYPE_P (type))
493 : {
494 10070 : tree tem = strip_float_extensions (t);
495 10070 : if (tem != t)
496 : return negate_expr_p (tem);
497 : }
498 : break;
499 :
500 1090421 : case CALL_EXPR:
501 : /* Negate -f(x) as f(-x). */
502 1090421 : if (negate_mathfn_p (get_call_combined_fn (t)))
503 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
504 : break;
505 :
506 12195 : case RSHIFT_EXPR:
507 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
508 12195 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 : {
510 12050 : tree op1 = TREE_OPERAND (t, 1);
511 12050 : 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 39588774 : fold_negate_expr_1 (location_t loc, tree t)
529 : {
530 39588774 : tree type = TREE_TYPE (t);
531 39588774 : tree tem;
532 :
533 39588774 : switch (TREE_CODE (t))
534 : {
535 : /* Convert - (~A) to A + 1. */
536 158 : case BIT_NOT_EXPR:
537 158 : if (INTEGRAL_TYPE_P (type))
538 158 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
539 158 : build_one_cst (type));
540 : break;
541 :
542 30413525 : case INTEGER_CST:
543 30413525 : tem = fold_negate_const (t, type);
544 30413525 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
545 10102 : || (ANY_INTEGRAL_TYPE_P (type)
546 10102 : && !TYPE_OVERFLOW_TRAPS (type)
547 10102 : && TYPE_OVERFLOW_WRAPS (type))
548 30422837 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
549 : return tem;
550 : break;
551 :
552 2023747 : case POLY_INT_CST:
553 2023747 : case REAL_CST:
554 2023747 : case FIXED_CST:
555 2023747 : tem = fold_negate_const (t, type);
556 2023747 : 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 51214 : case VECTOR_CST:
568 51214 : {
569 51214 : tree_vector_builder elts;
570 51214 : elts.new_unary_operation (type, t, true);
571 51214 : unsigned int count = elts.encoded_nelts ();
572 125159 : for (unsigned int i = 0; i < count; ++i)
573 : {
574 73945 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 73945 : if (elt == NULL_TREE)
576 0 : return NULL_TREE;
577 73945 : elts.quick_push (elt);
578 : }
579 :
580 51214 : return elts.build ();
581 51214 : }
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 690533 : case PLUS_EXPR:
602 690533 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
603 690533 : && !HONOR_SIGNED_ZEROS (type))
604 : {
605 : /* -(A + B) -> (-B) - A. */
606 690423 : if (negate_expr_p (TREE_OPERAND (t, 1)))
607 : {
608 632295 : tem = negate_expr (TREE_OPERAND (t, 1));
609 632295 : return fold_build2_loc (loc, MINUS_EXPR, type,
610 1264590 : tem, TREE_OPERAND (t, 0));
611 : }
612 :
613 : /* -(A + B) -> (-A) - B. */
614 58128 : if (negate_expr_p (TREE_OPERAND (t, 0)))
615 : {
616 1009 : tem = negate_expr (TREE_OPERAND (t, 0));
617 1009 : return fold_build2_loc (loc, MINUS_EXPR, type,
618 2018 : tem, TREE_OPERAND (t, 1));
619 : }
620 : }
621 : break;
622 :
623 158455 : case MINUS_EXPR:
624 : /* - (A - B) -> B - A */
625 158455 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
626 158455 : && !HONOR_SIGNED_ZEROS (type))
627 81318 : return fold_build2_loc (loc, MINUS_EXPR, type,
628 162636 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
629 : break;
630 :
631 268926 : case MULT_EXPR:
632 268926 : if (TYPE_UNSIGNED (type))
633 : break;
634 :
635 : /* Fall through. */
636 :
637 33692 : case RDIV_EXPR:
638 33692 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
639 : {
640 33692 : tem = TREE_OPERAND (t, 1);
641 33692 : if (negate_expr_p (tem))
642 61020 : return fold_build2_loc (loc, TREE_CODE (t), type,
643 61020 : TREE_OPERAND (t, 0), negate_expr (tem));
644 3182 : tem = TREE_OPERAND (t, 0);
645 3182 : 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 2374968 : case NOP_EXPR:
677 : /* Convert -((double)float) into (double)(-float). */
678 2374968 : if (SCALAR_FLOAT_TYPE_P (type))
679 : {
680 10867 : tem = strip_float_extensions (t);
681 10867 : if (tem != t && negate_expr_p (tem))
682 0 : return fold_convert_loc (loc, type, negate_expr (tem));
683 : }
684 : break;
685 :
686 295417 : case CALL_EXPR:
687 : /* Negate -f(x) as f(-x). */
688 295417 : if (negate_mathfn_p (get_call_combined_fn (t))
689 296706 : && 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 11972 : case RSHIFT_EXPR:
700 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
701 11972 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
702 : {
703 11954 : tree op1 = TREE_OPERAND (t, 1);
704 11954 : if (wi::to_wide (op1) == element_precision (type) - 1)
705 : {
706 11623 : tree ntype = TYPE_UNSIGNED (type)
707 11623 : ? signed_type_for (type)
708 72 : : unsigned_type_for (type);
709 11623 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
710 11623 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
711 11623 : 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 39588774 : fold_negate_expr (location_t loc, tree t)
727 : {
728 39588774 : tree type = TREE_TYPE (t);
729 39588774 : STRIP_SIGN_NOPS (t);
730 39588774 : tree tem = fold_negate_expr_1 (loc, t);
731 39588774 : if (tem == NULL_TREE)
732 : return NULL_TREE;
733 33314688 : 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 3824348 : negate_expr (tree t)
742 : {
743 3824348 : tree type, tem;
744 3824348 : location_t loc;
745 :
746 3824348 : if (t == NULL_TREE)
747 : return NULL_TREE;
748 :
749 3824348 : loc = EXPR_LOCATION (t);
750 3824348 : type = TREE_TYPE (t);
751 3824348 : STRIP_SIGN_NOPS (t);
752 :
753 3824348 : tem = fold_negate_expr (loc, t);
754 3824348 : if (!tem)
755 1936813 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
756 3824348 : 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 233321800 : 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 233321800 : tree var = 0;
788 233321800 : *minus_varp = 0;
789 233321800 : *conp = 0;
790 233321800 : *minus_conp = 0;
791 233321800 : *litp = 0;
792 233321800 : *minus_litp = 0;
793 :
794 : /* Strip any conversions that don't change the machine mode or signedness. */
795 233321800 : STRIP_SIGN_NOPS (in);
796 :
797 233321800 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
798 149183330 : || TREE_CODE (in) == FIXED_CST)
799 84138470 : *litp = in;
800 149183330 : else if (TREE_CODE (in) == code
801 149183330 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
802 144521934 : && ! 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 144521934 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
808 60516848 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
809 142824538 : || (code == MINUS_EXPR
810 23213968 : && (TREE_CODE (in) == PLUS_EXPR
811 21350258 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
812 : {
813 8682242 : tree op0 = TREE_OPERAND (in, 0);
814 8682242 : tree op1 = TREE_OPERAND (in, 1);
815 8682242 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
816 8682242 : 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 8682242 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
820 8463697 : || TREE_CODE (op0) == FIXED_CST)
821 218545 : *litp = op0, op0 = 0;
822 8463697 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
823 5669020 : || TREE_CODE (op1) == FIXED_CST)
824 2794677 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
825 :
826 8682242 : if (op0 != 0 && TREE_CONSTANT (op0))
827 15381 : *conp = op0, op0 = 0;
828 8666861 : else if (op1 != 0 && TREE_CONSTANT (op1))
829 52811 : *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 8682242 : if (op0 != 0 && op1 != 0)
834 : var = in;
835 3074149 : else if (op0 != 0)
836 : var = op0;
837 : else
838 233926 : var = op1, neg_var_p = neg1_p;
839 :
840 : /* Now do any needed negations. */
841 8682242 : if (neg_litp_p)
842 28668 : *minus_litp = *litp, *litp = 0;
843 8682242 : if (neg_conp_p && *conp)
844 11346 : *minus_conp = *conp, *conp = 0;
845 8682242 : if (neg_var_p && var)
846 223297 : *minus_varp = var, var = 0;
847 : }
848 140501088 : else if (TREE_CONSTANT (in))
849 822080 : *conp = in;
850 139679008 : else if (TREE_CODE (in) == BIT_NOT_EXPR
851 522425 : && code == PLUS_EXPR)
852 : {
853 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
854 : when IN is constant. */
855 362678 : *litp = build_minus_one_cst (type);
856 362678 : *minus_varp = TREE_OPERAND (in, 0);
857 : }
858 : else
859 : var = in;
860 :
861 233321800 : if (negate_p)
862 : {
863 12801166 : if (*litp)
864 1267094 : *minus_litp = *litp, *litp = 0;
865 11534072 : else if (*minus_litp)
866 174 : *litp = *minus_litp, *minus_litp = 0;
867 12801166 : if (*conp)
868 46952 : *minus_conp = *conp, *conp = 0;
869 12754214 : else if (*minus_conp)
870 0 : *conp = *minus_conp, *minus_conp = 0;
871 12801166 : if (var)
872 12741784 : *minus_varp = var, var = 0;
873 59382 : else if (*minus_varp)
874 882 : var = *minus_varp, *minus_varp = 0;
875 : }
876 :
877 233321800 : if (*litp
878 233321800 : && TREE_OVERFLOW_P (*litp))
879 20087 : *litp = drop_tree_overflow (*litp);
880 233321800 : if (*minus_litp
881 233321800 : && TREE_OVERFLOW_P (*minus_litp))
882 24 : *minus_litp = drop_tree_overflow (*minus_litp);
883 :
884 233321800 : 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 20935424 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
894 : {
895 20935424 : if (t1 == 0)
896 : {
897 13274638 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
898 : return t2;
899 : }
900 7660786 : 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 4273033 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
907 3366198 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
908 3299466 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
909 : {
910 1667647 : if (code == PLUS_EXPR)
911 : {
912 931435 : 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 931381 : 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 931380 : else if (integer_zerop (t2))
923 37572 : return fold_convert_loc (loc, type, t1);
924 : }
925 736212 : else if (code == MINUS_EXPR)
926 : {
927 712530 : if (integer_zerop (t2))
928 0 : return fold_convert_loc (loc, type, t1);
929 : }
930 :
931 1630020 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 1630020 : fold_convert_loc (loc, type, t2));
933 : }
934 :
935 2605386 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
936 2605386 : 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 2717813668 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
944 : {
945 2717813668 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
946 : return false;
947 2717813668 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
948 : return false;
949 :
950 2717813668 : switch (code)
951 : {
952 : case LSHIFT_EXPR:
953 : case RSHIFT_EXPR:
954 : case LROTATE_EXPR:
955 : case RROTATE_EXPR:
956 : return true;
957 :
958 2717813668 : default:
959 2717813668 : break;
960 : }
961 :
962 2717813668 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
963 2717813668 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
964 5435627336 : && 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 1515208718 : 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 1515208718 : wide_int tmp;
977 1515208718 : *overflow = wi::OVF_NONE;
978 1515208718 : switch (code)
979 : {
980 3728343 : case BIT_IOR_EXPR:
981 3728343 : res = wi::bit_or (arg1, arg2);
982 3728343 : break;
983 :
984 95833 : case BIT_XOR_EXPR:
985 95833 : res = wi::bit_xor (arg1, arg2);
986 95833 : break;
987 :
988 23568695 : case BIT_AND_EXPR:
989 23568695 : res = wi::bit_and (arg1, arg2);
990 23568695 : break;
991 :
992 14807891 : case LSHIFT_EXPR:
993 14807891 : if (wi::neg_p (arg2))
994 : return false;
995 14777583 : res = wi::lshift (arg1, arg2);
996 14777583 : break;
997 :
998 7405427 : case RSHIFT_EXPR:
999 7405427 : 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 7405231 : res = wi::rshift (arg1, arg2, sign);
1005 7405231 : 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 256179740 : case PLUS_EXPR:
1027 256179740 : res = wi::add (arg1, arg2, sign, overflow);
1028 256179740 : break;
1029 :
1030 74776055 : case MINUS_EXPR:
1031 74776055 : res = wi::sub (arg1, arg2, sign, overflow);
1032 74776055 : break;
1033 :
1034 434475664 : case MULT_EXPR:
1035 434475664 : res = wi::mul (arg1, arg2, sign, overflow);
1036 434475664 : break;
1037 :
1038 5544 : case MULT_HIGHPART_EXPR:
1039 5544 : res = wi::mul_high (arg1, arg2, sign);
1040 5544 : break;
1041 :
1042 358694151 : case TRUNC_DIV_EXPR:
1043 358694151 : case EXACT_DIV_EXPR:
1044 358694151 : if (arg2 == 0)
1045 : return false;
1046 358688799 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1047 358688799 : break;
1048 :
1049 80856427 : case FLOOR_DIV_EXPR:
1050 80856427 : if (arg2 == 0)
1051 : return false;
1052 80856427 : res = wi::div_floor (arg1, arg2, sign, overflow);
1053 80856427 : break;
1054 :
1055 86426379 : case CEIL_DIV_EXPR:
1056 86426379 : if (arg2 == 0)
1057 : return false;
1058 86426379 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1059 86426379 : 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 1368020 : case TRUNC_MOD_EXPR:
1068 1368020 : if (arg2 == 0)
1069 : return false;
1070 1366915 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1071 1366915 : break;
1072 :
1073 69303935 : case FLOOR_MOD_EXPR:
1074 69303935 : if (arg2 == 0)
1075 : return false;
1076 69303935 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1077 69303935 : 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 47558 : case MIN_EXPR:
1092 47558 : res = wi::min (arg1, arg2, sign);
1093 47558 : break;
1094 :
1095 103466866 : case MAX_EXPR:
1096 103466866 : res = wi::max (arg1, arg2, sign);
1097 103466866 : break;
1098 :
1099 : default:
1100 : return false;
1101 : }
1102 : return true;
1103 1515208718 : }
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 1515208718 : 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 1515208718 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1134 :
1135 1515208718 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1136 : {
1137 1515208718 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1138 1515208718 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1139 1515208718 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1140 : return NULL_TREE;
1141 1515171630 : res = wi_res;
1142 1515171630 : return true;
1143 1515208967 : }
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 1515208718 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1208 : int overflowable)
1209 : {
1210 1515208718 : poly_wide_int poly_res;
1211 1515208718 : tree type = TREE_TYPE (arg1);
1212 1515208718 : signop sign = TYPE_SIGN (type);
1213 1515208718 : wi::overflow_type overflow = wi::OVF_NONE;
1214 :
1215 1515208718 : if (!poly_int_tree_p (arg1)
1216 1515208718 : || !poly_int_tree_p (arg2)
1217 3030417436 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1218 37088 : return NULL_TREE;
1219 1515171630 : return force_fit_type (type, poly_res, overflowable,
1220 1515171630 : (((sign == SIGNED || overflowable == -1)
1221 1515171630 : && overflow)
1222 1515171630 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1223 1515208718 : }
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 189553 : 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 3953 : default:
1242 3953 : 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 444283 : 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 444283 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1255 : {
1256 361757 : if (integer_zerop (other_op))
1257 : {
1258 27719 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1259 : return op;
1260 26559 : 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 266384 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1274 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1275 : {
1276 192271 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1277 450263 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1278 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1279 : {
1280 183879 : tree type = TREE_TYPE (arg1);
1281 183879 : bool step_ok_p;
1282 183879 : if (VECTOR_CST_STEPPED_P (arg1)
1283 183879 : && 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 2718 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1293 181161 : 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 50554 : step_ok_p = distributes_over_addition_p (code, 1);
1302 : else
1303 : /* Similarly in reverse. */
1304 130607 : step_ok_p = distributes_over_addition_p (code, 2);
1305 183879 : tree_vector_builder elts;
1306 183879 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1307 : return NULL_TREE;
1308 183879 : unsigned int count = elts.encoded_nelts ();
1309 713542 : for (unsigned int i = 0; i < count; ++i)
1310 : {
1311 529982 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1312 529982 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1313 :
1314 529982 : 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 529982 : if (elt == NULL_TREE)
1319 319 : return NULL_TREE;
1320 529663 : elts.quick_push (elt);
1321 : }
1322 :
1323 183560 : return elts.build ();
1324 183879 : }
1325 :
1326 82505 : if (TREE_CODE (arg1) == VECTOR_CST
1327 8392 : && TREE_CODE (arg2) == INTEGER_CST)
1328 : {
1329 8392 : tree type = TREE_TYPE (arg1);
1330 8392 : bool step_ok_p = distributes_over_addition_p (code, 1);
1331 8392 : tree_vector_builder elts;
1332 8392 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1333 : return NULL_TREE;
1334 8392 : unsigned int count = elts.encoded_nelts ();
1335 35675 : for (unsigned int i = 0; i < count; ++i)
1336 : {
1337 27370 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1338 :
1339 27370 : 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 27370 : if (elt == NULL_TREE)
1344 87 : return NULL_TREE;
1345 27283 : elts.quick_push (elt);
1346 : }
1347 :
1348 8305 : return elts.build ();
1349 8392 : }
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 220968159 : const_binop (enum tree_code code, tree arg1, tree arg2)
1360 : {
1361 : /* Sanity check for the recursive cases. */
1362 220968159 : if (!arg1 || !arg2)
1363 : return NULL_TREE;
1364 :
1365 220966895 : STRIP_NOPS (arg1);
1366 220966895 : STRIP_NOPS (arg2);
1367 :
1368 220966895 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1369 : {
1370 215162685 : if (code == POINTER_PLUS_EXPR)
1371 103672 : return int_const_binop (PLUS_EXPR,
1372 207344 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1373 :
1374 215059013 : return int_const_binop (code, arg1, arg2);
1375 : }
1376 :
1377 5804210 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1378 : {
1379 5521291 : machine_mode mode;
1380 5521291 : REAL_VALUE_TYPE d1;
1381 5521291 : REAL_VALUE_TYPE d2;
1382 5521291 : REAL_VALUE_TYPE value;
1383 5521291 : REAL_VALUE_TYPE result;
1384 5521291 : bool inexact;
1385 5521291 : tree t, type;
1386 :
1387 : /* The following codes are handled by real_arithmetic. */
1388 5521291 : switch (code)
1389 : {
1390 5521291 : case PLUS_EXPR:
1391 5521291 : case MINUS_EXPR:
1392 5521291 : case MULT_EXPR:
1393 5521291 : case RDIV_EXPR:
1394 5521291 : case MIN_EXPR:
1395 5521291 : case MAX_EXPR:
1396 5521291 : break;
1397 :
1398 : default:
1399 : return NULL_TREE;
1400 : }
1401 :
1402 5521291 : d1 = TREE_REAL_CST (arg1);
1403 5521291 : d2 = TREE_REAL_CST (arg2);
1404 :
1405 5521291 : type = TREE_TYPE (arg1);
1406 5521291 : mode = TYPE_MODE (type);
1407 :
1408 : /* Don't perform operation if we honor signaling NaNs and
1409 : either operand is a signaling NaN. */
1410 5521291 : if (HONOR_SNANS (mode)
1411 5521291 : && (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 5521258 : if (code == RDIV_EXPR
1418 2399859 : && real_equal (&d2, &dconst0)
1419 5531979 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1420 7813 : 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 5513445 : 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 5513099 : 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 5513038 : inexact = real_arithmetic (&value, code, &d1, &d2);
1442 5513038 : 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 5513038 : if (flag_trapping_math
1449 21410846 : && MODE_HAS_NANS (mode)
1450 5494260 : && REAL_VALUE_ISNAN (result)
1451 2555 : && !REAL_VALUE_ISNAN (d1)
1452 5515593 : && !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 5510483 : if (flag_trapping_math
1458 21400968 : && MODE_HAS_INFINITIES (mode)
1459 5491705 : && REAL_VALUE_ISINF (result)
1460 7677 : && !REAL_VALUE_ISINF (d1)
1461 5517493 : && !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 5505756 : if ((flag_rounding_math
1469 37394455 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1470 5505756 : && (inexact || !real_identical (&result, &value)))
1471 1107 : return NULL_TREE;
1472 :
1473 5504649 : t = build_real (type, result);
1474 :
1475 5504649 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1476 5504649 : return t;
1477 : }
1478 :
1479 282919 : 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 282919 : 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 271829 : tree simplified;
1657 271829 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1658 : return simplified;
1659 :
1660 271246 : if (commutative_tree_code (code)
1661 271246 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1662 : return simplified;
1663 :
1664 266384 : 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 295302806 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1672 : {
1673 295302806 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1674 81925727 : 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 213377079 : 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 178624 : case POINTER_DIFF_EXPR:
1695 178624 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1696 : {
1697 356596 : poly_offset_int res = (wi::to_poly_offset (arg1)
1698 178298 : - wi::to_poly_offset (arg2));
1699 178298 : return force_fit_type (type, res, 1,
1700 178298 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1701 : }
1702 : return NULL_TREE;
1703 :
1704 14941 : case VEC_PACK_TRUNC_EXPR:
1705 14941 : case VEC_PACK_FIX_TRUNC_EXPR:
1706 14941 : case VEC_PACK_FLOAT_EXPR:
1707 14941 : {
1708 14941 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1709 :
1710 14941 : if (TREE_CODE (arg1) != VECTOR_CST
1711 14941 : || TREE_CODE (arg2) != VECTOR_CST)
1712 : return NULL_TREE;
1713 :
1714 14941 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1715 : return NULL_TREE;
1716 :
1717 14941 : out_nelts = in_nelts * 2;
1718 14941 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1719 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1720 :
1721 14941 : tree_vector_builder elts (type, out_nelts, 1);
1722 187145 : for (i = 0; i < out_nelts; i++)
1723 : {
1724 172216 : tree elt = (i < in_nelts
1725 172216 : ? VECTOR_CST_ELT (arg1, i)
1726 86102 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1727 173260 : 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 172216 : TREE_TYPE (type), elt);
1732 172216 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1733 12 : return NULL_TREE;
1734 172204 : elts.quick_push (elt);
1735 : }
1736 :
1737 14929 : return elts.build ();
1738 14941 : }
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 212913384 : default:;
1786 : }
1787 :
1788 212913384 : if (TREE_CODE_CLASS (code) != tcc_binary)
1789 : return NULL_TREE;
1790 :
1791 : /* Make sure type and arg0 have the same saturating flag. */
1792 210410720 : gcc_checking_assert (TYPE_SATURATING (type)
1793 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1794 :
1795 210410720 : 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 374588351 : 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 374588351 : if (TREE_CODE (arg0) == REAL_CST
1807 10987342 : && HONOR_SNANS (arg0)
1808 17121 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1809 4740 : && code != NEGATE_EXPR
1810 4740 : && code != ABS_EXPR
1811 374593056 : && code != ABSU_EXPR)
1812 : return NULL_TREE;
1813 :
1814 374583646 : switch (code)
1815 : {
1816 281051776 : CASE_CONVERT:
1817 281051776 : case FLOAT_EXPR:
1818 281051776 : case FIX_TRUNC_EXPR:
1819 281051776 : case FIXED_CONVERT_EXPR:
1820 281051776 : 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 13041159 : case VIEW_CONVERT_EXPR:
1832 13041159 : return fold_view_convert_expr (type, arg0);
1833 :
1834 31364609 : case NEGATE_EXPR:
1835 31364609 : {
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 31364609 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1840 31364609 : if (tem && CONSTANT_CLASS_P (tem))
1841 : return tem;
1842 : break;
1843 : }
1844 :
1845 40805 : case ABS_EXPR:
1846 40805 : case ABSU_EXPR:
1847 40805 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1848 36248 : 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 2299254 : case BIT_NOT_EXPR:
1861 2299254 : if (TREE_CODE (arg0) == INTEGER_CST)
1862 2292202 : return fold_not_const (arg0, type);
1863 7052 : 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 7052 : else if (TREE_CODE (arg0) == VECTOR_CST)
1867 : {
1868 6417 : tree elem;
1869 :
1870 : /* This can cope with stepped encodings because ~x == -1 - x. */
1871 6417 : tree_vector_builder elements;
1872 6417 : elements.new_unary_operation (type, arg0, true);
1873 6417 : unsigned int i, count = elements.encoded_nelts ();
1874 24961 : for (i = 0; i < count; ++i)
1875 : {
1876 18544 : elem = VECTOR_CST_ELT (arg0, i);
1877 18544 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1878 18544 : if (elem == NULL_TREE)
1879 : break;
1880 18544 : elements.quick_push (elem);
1881 : }
1882 6417 : if (i == count)
1883 6417 : return elements.build ();
1884 6417 : }
1885 : break;
1886 :
1887 11309501 : case TRUTH_NOT_EXPR:
1888 11309501 : if (TREE_CODE (arg0) == INTEGER_CST)
1889 10968417 : 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 185547 : case IMAGPART_EXPR:
1898 185547 : if (TREE_CODE (arg0) == COMPLEX_CST)
1899 185359 : return fold_convert (type, TREE_IMAGPART (arg0));
1900 : break;
1901 :
1902 19118 : case VEC_UNPACK_LO_EXPR:
1903 19118 : case VEC_UNPACK_HI_EXPR:
1904 19118 : case VEC_UNPACK_FLOAT_LO_EXPR:
1905 19118 : case VEC_UNPACK_FLOAT_HI_EXPR:
1906 19118 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1907 19118 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1908 19118 : {
1909 19118 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1910 19118 : enum tree_code subcode;
1911 :
1912 19118 : if (TREE_CODE (arg0) != VECTOR_CST)
1913 : return NULL_TREE;
1914 :
1915 19118 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1916 : return NULL_TREE;
1917 19118 : out_nelts = in_nelts / 2;
1918 19118 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1919 :
1920 19118 : unsigned int offset = 0;
1921 19118 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1922 19118 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1923 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1924 9551 : offset = out_nelts;
1925 :
1926 19118 : 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 19118 : tree_vector_builder elts (type, out_nelts, 1);
1935 101596 : for (i = 0; i < out_nelts; i++)
1936 : {
1937 82478 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1938 82478 : VECTOR_CST_ELT (arg0, i + offset));
1939 82478 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1940 0 : return NULL_TREE;
1941 82478 : elts.quick_push (elt);
1942 : }
1943 :
1944 19118 : return elts.build ();
1945 19118 : }
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 3589295536 : size_int_kind (poly_int64 number, enum size_type_kind kind)
1964 : {
1965 3589295536 : 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 2679423685 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1975 : {
1976 2679423685 : tree type = TREE_TYPE (arg0);
1977 :
1978 2679423685 : if (arg0 == error_mark_node || arg1 == error_mark_node)
1979 : return error_mark_node;
1980 :
1981 2679423685 : 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 2679423685 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1986 : {
1987 : /* And some specific cases even faster than that. */
1988 2646518646 : if (code == PLUS_EXPR)
1989 : {
1990 1219982790 : if (integer_zerop (arg0)
1991 1219982790 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1992 : return arg1;
1993 313582599 : if (integer_zerop (arg1)
1994 313582599 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1995 : return arg0;
1996 : }
1997 1426535856 : else if (code == MINUS_EXPR)
1998 : {
1999 121409240 : if (integer_zerop (arg1)
2000 121409240 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2001 : return arg0;
2002 : }
2003 1305126616 : else if (code == MULT_EXPR)
2004 : {
2005 615377438 : if (integer_onep (arg0)
2006 615377438 : && !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 1277877980 : tree res = int_const_binop (code, arg0, arg1, -1);
2014 1277877980 : if (res != NULL_TREE)
2015 : return res;
2016 : }
2017 :
2018 32905039 : 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 38389983 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2027 : {
2028 38389983 : tree type = TREE_TYPE (arg0);
2029 38389983 : tree ctype;
2030 :
2031 38389983 : 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 38389983 : if (!TYPE_UNSIGNED (type))
2036 10303775 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2037 :
2038 28086208 : if (type == sizetype)
2039 28086208 : 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 28086208 : 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 28068621 : if (tree_int_cst_equal (arg0, arg1))
2058 24800268 : return build_int_cst (ctype, 0);
2059 3268353 : else if (tree_int_cst_lt (arg1, arg0))
2060 2147376 : return fold_convert_loc (loc, ctype,
2061 2147376 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2062 : else
2063 1120977 : 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 1447038592 : 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 1447038592 : tree arg1_type = TREE_TYPE (arg1);
2080 1447038592 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2081 1447038592 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2082 1447038592 : TYPE_SIGN (arg1_type)),
2083 : overflowable,
2084 1447038592 : 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 56220 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2092 : {
2093 56220 : bool overflow = false;
2094 56220 : 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 56220 : wide_int val;
2106 56220 : REAL_VALUE_TYPE r;
2107 56220 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2108 :
2109 56220 : switch (code)
2110 : {
2111 56220 : case FIX_TRUNC_EXPR:
2112 56220 : real_trunc (&r, VOIDmode, &x);
2113 56220 : 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 56220 : 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 56220 : if (! overflow)
2130 : {
2131 52582 : tree lt = TYPE_MIN_VALUE (type);
2132 52582 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2133 52582 : if (real_less (&r, &l))
2134 : {
2135 1974 : overflow = true;
2136 1974 : val = wi::to_wide (lt);
2137 : }
2138 : }
2139 :
2140 56220 : if (! overflow)
2141 : {
2142 50608 : tree ut = TYPE_MAX_VALUE (type);
2143 50608 : if (ut)
2144 : {
2145 50608 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2146 50608 : if (real_less (&u, &r))
2147 : {
2148 1921 : overflow = true;
2149 1921 : val = wi::to_wide (ut);
2150 : }
2151 : }
2152 : }
2153 :
2154 56220 : if (! overflow)
2155 48689 : 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 56220 : if (!flag_trapping_math || !overflow)
2165 48943 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2166 : else
2167 : t = NULL_TREE;
2168 :
2169 56220 : return t;
2170 56220 : }
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 2201093 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2225 : {
2226 2201093 : REAL_VALUE_TYPE value;
2227 2201093 : 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 2201093 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2233 : {
2234 99252 : t = build_real (type, TREE_REAL_CST (arg1));
2235 99252 : 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 2101841 : if (HONOR_SNANS (arg1)
2241 2103723 : && 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 2101841 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2247 2102497 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2248 509 : return NULL_TREE;
2249 :
2250 2101332 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2251 2101332 : 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 2101332 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2258 2238938 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2259 0 : TREE_OVERFLOW (t) = 1;
2260 2101332 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2261 2234696 : && !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 10503260 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2266 0 : && REAL_VALUE_ISINF (value)
2267 2101332 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2268 0 : TREE_OVERFLOW (t) = 1;
2269 : else
2270 2101332 : 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 1511283458 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2367 : {
2368 1511283458 : tree arg_type = TREE_TYPE (arg1);
2369 1511283458 : 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 1499959107 : 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 1499959107 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2383 : || TREE_CODE (type) == OFFSET_TYPE)
2384 : {
2385 1468731669 : if (TREE_CODE (arg1) == INTEGER_CST)
2386 1447038592 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2387 21693077 : else if (TREE_CODE (arg1) == REAL_CST)
2388 56220 : return fold_convert_const_int_from_real (code, type, arg1);
2389 21636857 : 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 31170910 : if (TREE_CODE (arg1) == INTEGER_CST)
2395 : {
2396 23982225 : 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 23982225 : 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 23980482 : return res;
2408 : }
2409 7188685 : else if (TREE_CODE (arg1) == REAL_CST)
2410 2201093 : return fold_convert_const_real_from_real (type, arg1);
2411 4987592 : 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 4742 : if (TREE_CODE (arg1) == VECTOR_CST
2426 4742 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2427 : {
2428 4742 : tree elttype = TREE_TYPE (type);
2429 4742 : 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 4742 : bool step_ok_p
2433 4742 : = (INTEGRAL_TYPE_P (elttype)
2434 359 : && INTEGRAL_TYPE_P (arg1_elttype)
2435 5043 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2436 4742 : tree_vector_builder v;
2437 4742 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2438 : return NULL_TREE;
2439 4742 : unsigned int len = v.encoded_nelts ();
2440 27952 : for (unsigned int i = 0; i < len; ++i)
2441 : {
2442 23210 : tree elt = VECTOR_CST_ELT (arg1, i);
2443 23210 : tree cvt = fold_convert_const (code, elttype, elt);
2444 23210 : if (cvt == NULL_TREE)
2445 0 : return NULL_TREE;
2446 23210 : v.quick_push (cvt);
2447 : }
2448 4742 : return v.build ();
2449 4742 : }
2450 : }
2451 12164 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2452 12164 : 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 17434 : build_zero_vector (tree type)
2460 : {
2461 17434 : tree t;
2462 :
2463 17434 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2464 17434 : 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 2216334110 : fold_convert_loc (location_t loc, tree type, tree arg)
2516 : {
2517 2216334110 : tree orig = TREE_TYPE (arg);
2518 2216334110 : tree tem;
2519 :
2520 2216334110 : if (type == orig)
2521 : return arg;
2522 :
2523 1497261910 : if (TREE_CODE (arg) == ERROR_MARK
2524 1497260882 : || TREE_CODE (type) == ERROR_MARK
2525 1497260881 : || TREE_CODE (orig) == ERROR_MARK)
2526 1029 : return error_mark_node;
2527 :
2528 1497260881 : switch (TREE_CODE (type))
2529 : {
2530 116548725 : case POINTER_TYPE:
2531 116548725 : case REFERENCE_TYPE:
2532 : /* Handle conversions between pointers to different address spaces. */
2533 116548725 : if (POINTER_TYPE_P (orig)
2534 116548725 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2535 97911967 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2536 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2537 : /* fall through */
2538 :
2539 1465691784 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2540 1465691784 : case OFFSET_TYPE: case BITINT_TYPE:
2541 1465691784 : if (TREE_CODE (arg) == INTEGER_CST)
2542 : {
2543 1229755654 : tem = fold_convert_const (NOP_EXPR, type, arg);
2544 1229755654 : if (tem != NULL_TREE)
2545 : return tem;
2546 : }
2547 235936130 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2548 2536 : || TREE_CODE (orig) == OFFSET_TYPE)
2549 235936130 : 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 554647 : case REAL_TYPE:
2559 554647 : if (TREE_CODE (arg) == INTEGER_CST)
2560 : {
2561 57977 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2562 57977 : if (tem != NULL_TREE)
2563 : return tem;
2564 : }
2565 496670 : else if (TREE_CODE (arg) == REAL_CST)
2566 : {
2567 121649 : tem = fold_convert_const (NOP_EXPR, type, arg);
2568 121649 : if (tem != NULL_TREE)
2569 : return tem;
2570 : }
2571 375021 : 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 375023 : 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 374318 : case REAL_TYPE:
2586 374318 : 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 30897594 : case VECTOR_TYPE:
2664 30897594 : if (integer_zerop (arg))
2665 17434 : return build_zero_vector (type);
2666 30880160 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 30880160 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2668 : || VECTOR_TYPE_P (orig));
2669 30880160 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2670 :
2671 110680 : case VOID_TYPE:
2672 110680 : tem = fold_ignored_result (arg);
2673 110680 : 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 52989694 : maybe_lvalue_p (const_tree x)
2694 : {
2695 : /* We only need to wrap lvalue tree codes. */
2696 52989694 : 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 39017411 : default:
2730 : /* Assume the worst for front-end tree codes. */
2731 39017411 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2732 : break;
2733 : return false;
2734 : }
2735 :
2736 14002317 : return true;
2737 : }
2738 :
2739 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2740 :
2741 : tree
2742 46591220 : 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 46591220 : if (in_gimple_form)
2747 : return x;
2748 :
2749 10612316 : if (! maybe_lvalue_p (x))
2750 : return x;
2751 2523042 : 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 130527718 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2761 : {
2762 130527718 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2763 1034209 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2764 : return ERROR_MARK;
2765 :
2766 129743266 : switch (code)
2767 : {
2768 : case EQ_EXPR:
2769 : return NE_EXPR;
2770 57387488 : case NE_EXPR:
2771 57387488 : return EQ_EXPR;
2772 12166118 : case GT_EXPR:
2773 12166118 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2774 17739740 : case GE_EXPR:
2775 17739740 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2776 8067428 : case LT_EXPR:
2777 8067428 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2778 8123034 : case LE_EXPR:
2779 8123034 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2780 260 : case LTGT_EXPR:
2781 260 : 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 221749 : case ORDERED_EXPR:
2793 221749 : return UNORDERED_EXPR;
2794 56986 : case UNORDERED_EXPR:
2795 56986 : 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 159292282 : swap_tree_comparison (enum tree_code code)
2806 : {
2807 159292282 : 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 38844744 : case GT_EXPR:
2817 38844744 : return LT_EXPR;
2818 11181815 : case GE_EXPR:
2819 11181815 : return LE_EXPR;
2820 21951076 : case LT_EXPR:
2821 21951076 : return GT_EXPR;
2822 16496891 : case LE_EXPR:
2823 16496891 : return GE_EXPR;
2824 247021 : case UNGT_EXPR:
2825 247021 : return UNLT_EXPR;
2826 19365 : case UNGE_EXPR:
2827 19365 : return UNLE_EXPR;
2828 371680 : case UNLT_EXPR:
2829 371680 : return UNGT_EXPR;
2830 111324 : case UNLE_EXPR:
2831 111324 : 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 400842 : comparison_to_compcode (enum tree_code code)
2844 : {
2845 400842 : 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 21628 : compcode_to_comparison (enum comparison_code code)
2886 : {
2887 21628 : 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 1772014 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2926 : {
2927 1772014 : return (COMPARISON_CLASS_P (cond1)
2928 1679823 : && COMPARISON_CLASS_P (cond2)
2929 1668536 : && (invert_tree_comparison
2930 1668536 : (TREE_CODE (cond1),
2931 3337072 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2932 69795 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2933 69795 : TREE_OPERAND (cond2, 0), 0)
2934 1794274 : && operand_equal_p (TREE_OPERAND (cond1, 1),
2935 22260 : 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 200421 : 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 200421 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
2951 200421 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
2952 200421 : int compcode;
2953 200421 : *res = NULL_TREE;
2954 :
2955 200421 : switch (code)
2956 : {
2957 178070 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2958 178070 : case BIT_AND_EXPR:
2959 178070 : compcode = lcompcode & rcompcode;
2960 178070 : break;
2961 :
2962 22351 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2963 22351 : case BIT_IOR_EXPR:
2964 22351 : compcode = lcompcode | rcompcode;
2965 22351 : break;
2966 :
2967 : default:
2968 : return ERROR_MARK;
2969 : }
2970 :
2971 200421 : 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 34154 : compcode &= ~COMPCODE_UNORD;
2976 34154 : if (compcode == COMPCODE_LTGT)
2977 : compcode = COMPCODE_NE;
2978 31129 : else if (compcode == COMPCODE_ORD)
2979 5938 : compcode = COMPCODE_TRUE;
2980 : }
2981 166267 : else if (flag_trapping_math)
2982 : {
2983 : /* Check that the original operation and the optimized ones will trap
2984 : under the same condition. */
2985 330234 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2986 162133 : && (lcompcode != COMPCODE_EQ)
2987 165117 : && (lcompcode != COMPCODE_ORD);
2988 330234 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2989 157258 : && (rcompcode != COMPCODE_EQ)
2990 165117 : && (rcompcode != COMPCODE_ORD);
2991 330234 : bool trap = (compcode & COMPCODE_UNORD) == 0
2992 163687 : && (compcode != COMPCODE_EQ)
2993 165117 : && (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 165117 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3002 164642 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3003 165117 : rtrap = false;
3004 :
3005 : /* Allow combining of `a != b && a < b` since NAN will cause != to be
3006 : always true, and `a < b` will cause a trap. This is trap neutral. */
3007 165117 : if (code == TRUTH_ANDIF_EXPR && lcompcode == COMPCODE_NE && rtrap && trap)
3008 : ;
3009 : /* Likewise of `a == b || a < b` for the same reason. */
3010 165021 : else if (code == TRUTH_ORIF_EXPR && lcompcode == COMPCODE_EQ && rtrap && trap)
3011 : ;
3012 : /* If the comparison was short-circuited, and only the RHS
3013 : trapped, we may now generate a spurious trap. */
3014 164953 : else if (rtrap && !ltrap
3015 0 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3016 : return ERROR_MARK;
3017 :
3018 : /* If we changed the conditions that cause a trap, we lose. */
3019 164953 : else if ((ltrap || rtrap) != trap)
3020 : return ERROR_MARK;
3021 : }
3022 :
3023 37397 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3024 : {
3025 15769 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3026 15769 : return INTEGER_CST;
3027 : }
3028 : else
3029 21628 : return compcode_to_comparison ((enum comparison_code) compcode);
3030 : }
3031 :
3032 : /* Return a tree for the comparison which is the combination of
3033 : doing the AND or OR (depending on CODE) of the two operations LCODE
3034 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3035 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3036 : if this makes the transformation invalid. */
3037 :
3038 : tree
3039 39890 : combine_comparisons (location_t loc,
3040 : enum tree_code code, enum tree_code lcode,
3041 : enum tree_code rcode, tree truth_type,
3042 : tree ll_arg, tree lr_arg)
3043 : {
3044 39890 : bool honor_nans = HONOR_NANS (ll_arg);
3045 39890 : tree_code rescode;
3046 39890 : tree res;
3047 39890 : rescode = combine_comparisons (code, lcode, rcode, truth_type,
3048 : honor_nans, &res);
3049 39890 : if (rescode == ERROR_MARK)
3050 : return NULL_TREE;
3051 36346 : if (rescode == INTEGER_CST)
3052 15769 : return res;
3053 :
3054 20577 : return fold_build2_loc (loc, rescode, truth_type, ll_arg, lr_arg);
3055 : }
3056 :
3057 : /* Return nonzero if two operands (typically of the same tree node)
3058 : are necessarily equal. FLAGS modifies behavior as follows:
3059 :
3060 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3061 : This function tests whether the operands are indistinguishable;
3062 : it does not test whether they are equal using C's == operation.
3063 : The distinction is important for IEEE floating point, because
3064 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3065 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3066 :
3067 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3068 : even though it may hold multiple values during a function.
3069 : This is because a GCC tree node guarantees that nothing else is
3070 : executed between the evaluation of its "operands" (which may often
3071 : be evaluated in arbitrary order). Hence if the operands themselves
3072 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3073 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3074 : unset means assuming isochronic (or instantaneous) tree equivalence.
3075 : Unless comparing arbitrary expression trees, such as from different
3076 : statements, this flag can usually be left unset.
3077 :
3078 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3079 : are considered the same. It is used when the caller has other ways
3080 : to ensure that global memory is unchanged in between.
3081 :
3082 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3083 : not values of expressions.
3084 :
3085 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3086 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3087 :
3088 : If OEP_BITWISE is set, then require the values to be bitwise identical
3089 : rather than simply numerically equal. Do not take advantage of things
3090 : like math-related flags or undefined behavior; only return true for
3091 : values that are provably bitwise identical in all circumstances.
3092 :
3093 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3094 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3095 : rather than just numerically equivalent. The compared expressions must
3096 : however perform the same operations but may do intermediate computations in
3097 : differing signs. Because this comparison ignores any possible UB it cannot
3098 : be used blindly without ensuring that the context you are using it in itself
3099 : doesn't guarantee that there will be no UB. Conditional expressions are
3100 : excluded from this relaxation.
3101 :
3102 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3103 : differing hashes even for cases where operand_compare::operand_equal_p
3104 : compares equal.
3105 :
3106 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3107 : any operand with side effect. This is unnecessarily conservative in the
3108 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3109 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3110 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3111 : even if var is volatile. */
3112 :
3113 : bool
3114 7258401822 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3115 : unsigned int flags)
3116 : {
3117 7258401822 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3118 : }
3119 :
3120 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3121 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3122 : expression is being compared under for equality. This means that they can
3123 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3124 :
3125 : bool
3126 7259134021 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3127 : tree type1, const_tree arg1,
3128 : unsigned int flags)
3129 : {
3130 7259134021 : bool r;
3131 7259134021 : if (verify_hash_value (arg0, arg1, flags, &r))
3132 3052376536 : return r;
3133 :
3134 4206757485 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3135 4206757485 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3136 :
3137 : /* If either is ERROR_MARK, they aren't equal. */
3138 4206757485 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3139 4206756907 : || type0 == error_mark_node
3140 4206756905 : || type1 == error_mark_node)
3141 : return false;
3142 :
3143 : /* Similar, if either does not have a type (like a template id),
3144 : they aren't equal. */
3145 4206756904 : if (!type0 || !type1)
3146 : return false;
3147 :
3148 : /* Bitwise identity makes no sense if the values have different layouts. */
3149 4206751745 : if ((flags & OEP_BITWISE)
3150 4206751745 : && !tree_nop_conversion_p (type0, type1))
3151 : return false;
3152 :
3153 : /* We cannot consider pointers to different address space equal. */
3154 4206751745 : if (POINTER_TYPE_P (type0)
3155 642296337 : && POINTER_TYPE_P (type1)
3156 4756583495 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3157 549831750 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3158 : return false;
3159 :
3160 : /* Check equality of integer constants before bailing out due to
3161 : precision differences. */
3162 4206751504 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3163 : {
3164 : /* Address of INTEGER_CST is not defined; check that we did not forget
3165 : to drop the OEP_ADDRESS_OF flags. */
3166 661567135 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3167 661567135 : return tree_int_cst_equal (arg0, arg1);
3168 : }
3169 :
3170 3545184369 : if ((flags & OEP_ASSUME_WRAPV)
3171 2078988 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3172 : {
3173 783418 : const_tree t_arg0 = arg0;
3174 783418 : const_tree t_arg1 = arg1;
3175 783418 : STRIP_NOPS (arg0);
3176 783418 : STRIP_NOPS (arg1);
3177 : /* Only recurse if the conversion was one that was valid to strip. */
3178 783418 : if (t_arg0 != arg0 || t_arg1 != arg1)
3179 732199 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3180 : }
3181 :
3182 3544452170 : if (!(flags & OEP_ADDRESS_OF))
3183 : {
3184 : /* Check if we are checking an operation where the two's compliment
3185 : bitwise representation of the result is not the same between signed and
3186 : unsigned arithmetic. */
3187 3140158676 : bool enforce_signedness = true;
3188 3140158676 : if (flags & OEP_ASSUME_WRAPV)
3189 : {
3190 1252191 : switch (TREE_CODE (arg0))
3191 : {
3192 : case PLUS_EXPR:
3193 : case MINUS_EXPR:
3194 : case MULT_EXPR:
3195 : case BIT_IOR_EXPR:
3196 : case BIT_XOR_EXPR:
3197 : case BIT_AND_EXPR:
3198 : case BIT_NOT_EXPR:
3199 : case ABS_EXPR:
3200 : CASE_CONVERT:
3201 : case SSA_NAME:
3202 : case INTEGER_CST:
3203 : case VAR_DECL:
3204 : case PARM_DECL:
3205 : case RESULT_DECL:
3206 3140158676 : enforce_signedness = false;
3207 : break;
3208 :
3209 : default:
3210 : break;
3211 : }
3212 : }
3213 :
3214 : /* If both types don't have the same signedness, then we can't consider
3215 : them equal. We must check this before the STRIP_NOPS calls
3216 : because they may change the signedness of the arguments. As pointers
3217 : strictly don't have a signedness, require either two pointers or
3218 : two non-pointers as well. */
3219 3140158676 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3220 3140158676 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3221 144957386 : && enforce_signedness))
3222 : return false;
3223 :
3224 : /* If both types don't have the same precision, then it is not safe
3225 : to strip NOPs. */
3226 2840334382 : if (element_precision (type0) != element_precision (type1))
3227 : return false;
3228 :
3229 2688280673 : STRIP_NOPS (arg0);
3230 2688280673 : STRIP_NOPS (arg1);
3231 :
3232 2688280673 : type0 = TREE_TYPE (arg0);
3233 2688280673 : type1 = TREE_TYPE (arg1);
3234 : }
3235 : #if 0
3236 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3237 : sanity check once the issue is solved. */
3238 : else
3239 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3240 : are not defined. Check that we did not forget to drop the
3241 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3242 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3243 : && TREE_CODE (arg0) != SSA_NAME);
3244 : #endif
3245 :
3246 : /* In case both args are comparisons but with different comparison
3247 : code, try to swap the comparison operands of one arg to produce
3248 : a match and compare that variant. */
3249 3092574167 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3250 1245629109 : && COMPARISON_CLASS_P (arg0)
3251 6778598 : && COMPARISON_CLASS_P (arg1))
3252 : {
3253 5051074 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3254 :
3255 5051074 : if (TREE_CODE (arg0) == swap_code)
3256 2149370 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3257 2149370 : TREE_OPERAND (arg1, 1), flags)
3258 2169120 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3259 19750 : TREE_OPERAND (arg1, 0), flags);
3260 : }
3261 :
3262 3090424797 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3263 : {
3264 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3265 1243479739 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3266 : ;
3267 1243416346 : else if (flags & OEP_ADDRESS_OF)
3268 : {
3269 : /* If we are interested in comparing addresses ignore
3270 : MEM_REF wrappings of the base that can appear just for
3271 : TBAA reasons. */
3272 49292840 : if (TREE_CODE (arg0) == MEM_REF
3273 8123222 : && DECL_P (arg1)
3274 5682525 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3275 1327855 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3276 49989923 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3277 : return true;
3278 49065465 : else if (TREE_CODE (arg1) == MEM_REF
3279 30665875 : && DECL_P (arg0)
3280 11152721 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3281 2302135 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3282 49640879 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3283 : return true;
3284 48678896 : return false;
3285 : }
3286 : else
3287 : return false;
3288 : }
3289 :
3290 : /* When not checking addresses, this is needed for conversions and for
3291 : COMPONENT_REF. Might as well play it safe and always test this. */
3292 1847008451 : if (TREE_CODE (type0) == ERROR_MARK
3293 1847008451 : || TREE_CODE (type1) == ERROR_MARK
3294 3694016902 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3295 26084701 : && !(flags & OEP_ADDRESS_OF)))
3296 3902374 : return false;
3297 :
3298 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3299 : We don't care about side effects in that case because the SAVE_EXPR
3300 : takes care of that for us. In all other cases, two expressions are
3301 : equal if they have no side effects. If we have two identical
3302 : expressions with side effects that should be treated the same due
3303 : to the only side effects being identical SAVE_EXPR's, that will
3304 : be detected in the recursive calls below.
3305 : If we are taking an invariant address of two identical objects
3306 : they are necessarily equal as well. */
3307 328166757 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3308 2171272656 : && (TREE_CODE (arg0) == SAVE_EXPR
3309 328141212 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3310 288684441 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3311 : return true;
3312 :
3313 : /* Next handle constant cases, those for which we can return 1 even
3314 : if ONLY_CONST is set. */
3315 1515106765 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3316 25840489 : switch (TREE_CODE (arg0))
3317 : {
3318 151 : case INTEGER_CST:
3319 151 : return tree_int_cst_equal (arg0, arg1);
3320 :
3321 0 : case FIXED_CST:
3322 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3323 : TREE_FIXED_CST (arg1));
3324 :
3325 3761354 : case REAL_CST:
3326 3761354 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3327 : return true;
3328 :
3329 2731173 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3330 : {
3331 : /* If we do not distinguish between signed and unsigned zero,
3332 : consider them equal. */
3333 14675 : if (real_zerop (arg0) && real_zerop (arg1))
3334 : return true;
3335 : }
3336 2731164 : return false;
3337 :
3338 1019361 : case VECTOR_CST:
3339 1019361 : {
3340 1019361 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3341 1019361 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3342 : return false;
3343 :
3344 997601 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3345 997601 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3346 : return false;
3347 :
3348 962580 : unsigned int count = vector_cst_encoded_nelts (arg0);
3349 1342337 : for (unsigned int i = 0; i < count; ++i)
3350 2192162 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3351 1096081 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3352 : return false;
3353 : return true;
3354 : }
3355 :
3356 13969 : case COMPLEX_CST:
3357 13969 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3358 : flags)
3359 13969 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3360 : flags));
3361 :
3362 1026713 : case STRING_CST:
3363 1026713 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3364 1026713 : && ! memcmp (TREE_STRING_POINTER (arg0),
3365 588744 : TREE_STRING_POINTER (arg1),
3366 588744 : TREE_STRING_LENGTH (arg0)));
3367 :
3368 0 : case RAW_DATA_CST:
3369 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3370 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3371 0 : RAW_DATA_POINTER (arg1),
3372 0 : RAW_DATA_LENGTH (arg0)));
3373 :
3374 18848253 : case ADDR_EXPR:
3375 18848253 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3376 18848253 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3377 : flags | OEP_ADDRESS_OF
3378 18848253 : | OEP_MATCH_SIDE_EFFECTS);
3379 183221 : case CONSTRUCTOR:
3380 183221 : {
3381 : /* In GIMPLE empty constructors are allowed in initializers of
3382 : aggregates. */
3383 183221 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3384 : return true;
3385 :
3386 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3387 138140 : if (TREE_CODE (type0) != TREE_CODE (type1))
3388 : return false;
3389 138140 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3390 : {
3391 : /* For arrays, check that the sizes all match. */
3392 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3393 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3394 264 : || siz0 < 0
3395 528 : || siz0 != int_size_in_bytes (type1))
3396 0 : return false;
3397 : }
3398 137876 : else if (!types_compatible_p (type0, type1))
3399 : return false;
3400 :
3401 138140 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3402 138140 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3403 414420 : if (vec_safe_length (v0) != vec_safe_length (v1))
3404 : return false;
3405 :
3406 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3407 : of the CONSTRUCTOR referenced indirectly. */
3408 138140 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3409 :
3410 364449116 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3411 : {
3412 206329 : constructor_elt *c0 = &(*v0)[idx];
3413 206329 : constructor_elt *c1 = &(*v1)[idx];
3414 :
3415 : /* Check that the values are the same... */
3416 206329 : if (c0->value != c1->value
3417 206329 : && !operand_equal_p (c0->value, c1->value, flags))
3418 : return false;
3419 :
3420 : /* ... and that they apply to the same field! */
3421 117765 : if (c0->index != c1->index
3422 117765 : && (TREE_CODE (type0) == ARRAY_TYPE
3423 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3424 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3425 0 : DECL_FIELD_OFFSET (c1->index),
3426 : flags)
3427 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3428 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3429 : flags)))
3430 0 : return false;
3431 : }
3432 :
3433 : return true;
3434 : }
3435 :
3436 : default:
3437 : break;
3438 : }
3439 :
3440 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3441 : two instances of undefined behavior will give identical results. */
3442 1490253743 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3443 : return false;
3444 :
3445 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3446 : variant that allows null and views null as being different from any
3447 : non-null value. In the latter case, if either is null, the both
3448 : must be; otherwise, do the normal comparison. */
3449 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3450 : TREE_OPERAND (arg1, N), flags)
3451 :
3452 : #define OP_SAME_WITH_NULL(N) \
3453 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3454 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3455 :
3456 1490253743 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3457 : {
3458 8343332 : case tcc_unary:
3459 : /* Two conversions are equal only if signedness and modes match. */
3460 8343332 : switch (TREE_CODE (arg0))
3461 : {
3462 7976835 : CASE_CONVERT:
3463 7976835 : case FIX_TRUNC_EXPR:
3464 7976835 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3465 : return false;
3466 : break;
3467 : default:
3468 : break;
3469 : }
3470 :
3471 8343311 : return OP_SAME_WITH_NULL (0);
3472 :
3473 :
3474 22936197 : case tcc_comparison:
3475 22936197 : case tcc_binary:
3476 22936197 : if (OP_SAME (0) && OP_SAME (1))
3477 : return true;
3478 :
3479 : /* For commutative ops, allow the other order. */
3480 16740239 : return (commutative_tree_code (TREE_CODE (arg0))
3481 12655676 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3482 12655676 : TREE_OPERAND (arg1, 1), flags)
3483 16960313 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3484 220074 : TREE_OPERAND (arg1, 0), flags));
3485 :
3486 877330271 : case tcc_reference:
3487 : /* If either of the pointer (or reference) expressions we are
3488 : dereferencing contain a side effect, these cannot be equal,
3489 : but their addresses can be. */
3490 877330271 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3491 877330271 : && (TREE_SIDE_EFFECTS (arg0)
3492 809478810 : || TREE_SIDE_EFFECTS (arg1)))
3493 : return false;
3494 :
3495 876946144 : switch (TREE_CODE (arg0))
3496 : {
3497 5199369 : case INDIRECT_REF:
3498 5199369 : if (!(flags & OEP_ADDRESS_OF))
3499 : {
3500 5177035 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3501 : return false;
3502 : /* Verify that the access types are compatible. */
3503 5171160 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3504 : return false;
3505 : }
3506 5126574 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3507 5126574 : return OP_SAME (0);
3508 :
3509 574694 : case IMAGPART_EXPR:
3510 : /* Require the same offset. */
3511 574694 : if (!operand_equal_p (TYPE_SIZE (type0),
3512 574694 : TYPE_SIZE (type1),
3513 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3514 : return false;
3515 :
3516 : /* Fallthru. */
3517 2294463 : case REALPART_EXPR:
3518 2294463 : case VIEW_CONVERT_EXPR:
3519 2294463 : return OP_SAME (0);
3520 :
3521 86543420 : case TARGET_MEM_REF:
3522 86543420 : case MEM_REF:
3523 86543420 : if (!(flags & OEP_ADDRESS_OF))
3524 : {
3525 : /* Require equal access sizes */
3526 16958588 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3527 16958588 : && (!TYPE_SIZE (type0)
3528 1240651 : || !TYPE_SIZE (type1)
3529 1234500 : || !operand_equal_p (TYPE_SIZE (type0),
3530 1234500 : TYPE_SIZE (type1),
3531 : flags)))
3532 1245003 : return false;
3533 : /* Verify that access happens in similar types. */
3534 15713585 : if (!types_compatible_p (type0, type1))
3535 : return false;
3536 : /* Verify that accesses are TBAA compatible. */
3537 15374234 : if (!alias_ptr_types_compatible_p
3538 15374234 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3539 15374234 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3540 14476309 : || (MR_DEPENDENCE_CLIQUE (arg0)
3541 14476309 : != MR_DEPENDENCE_CLIQUE (arg1))
3542 28129325 : || (MR_DEPENDENCE_BASE (arg0)
3543 12755091 : != MR_DEPENDENCE_BASE (arg1)))
3544 : return false;
3545 : /* Verify that alignment is compatible. */
3546 12235316 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3547 : return false;
3548 : }
3549 81607955 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3550 139396440 : return (OP_SAME (0) && OP_SAME (1)
3551 : /* TARGET_MEM_REF require equal extra operands. */
3552 106566095 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3553 569632 : || (OP_SAME_WITH_NULL (2)
3554 263661 : && OP_SAME_WITH_NULL (3)
3555 258231 : && OP_SAME_WITH_NULL (4))));
3556 :
3557 35863093 : case ARRAY_REF:
3558 35863093 : case ARRAY_RANGE_REF:
3559 35863093 : if (!OP_SAME (0))
3560 : return false;
3561 31030661 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3562 : /* Compare the array index by value if it is constant first as we
3563 : may have different types but same value here. */
3564 31030661 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3565 31030661 : TREE_OPERAND (arg1, 1))
3566 27924788 : || OP_SAME (1))
3567 6241028 : && OP_SAME_WITH_NULL (2)
3568 6239708 : && OP_SAME_WITH_NULL (3)
3569 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3570 : we have to account for the offset of the ref. */
3571 40390883 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3572 3119854 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3573 2729 : || (operand_equal_p (array_ref_low_bound
3574 2729 : (const_cast<tree> (arg0)),
3575 : array_ref_low_bound
3576 2729 : (const_cast<tree> (arg1)),
3577 : flags)
3578 2729 : && operand_equal_p (array_ref_element_size
3579 2729 : (const_cast<tree> (arg0)),
3580 : array_ref_element_size
3581 2729 : (const_cast<tree> (arg1)),
3582 : flags))));
3583 :
3584 746416654 : case COMPONENT_REF:
3585 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3586 : may be NULL when we're called to compare MEM_EXPRs. */
3587 746416654 : if (!OP_SAME_WITH_NULL (0))
3588 : return false;
3589 57808781 : {
3590 57808781 : bool compare_address = flags & OEP_ADDRESS_OF;
3591 :
3592 : /* Most of time we only need to compare FIELD_DECLs for equality.
3593 : However when determining address look into actual offsets.
3594 : These may match for unions and unshared record types. */
3595 57808781 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3596 57808781 : if (!OP_SAME (1))
3597 : {
3598 33713282 : if (compare_address
3599 644890 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3600 : {
3601 644887 : tree field0 = TREE_OPERAND (arg0, 1);
3602 644887 : tree field1 = TREE_OPERAND (arg1, 1);
3603 :
3604 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3605 644887 : if (TREE_CODE (field0) != FIELD_DECL
3606 644887 : || TREE_CODE (field1) != FIELD_DECL)
3607 : return false;
3608 :
3609 644887 : if (!DECL_FIELD_OFFSET (field0)
3610 644887 : || !DECL_FIELD_OFFSET (field1))
3611 3 : return field0 == field1;
3612 :
3613 644884 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3614 644884 : DECL_FIELD_OFFSET (field1), flags)
3615 836038 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3616 191154 : DECL_FIELD_BIT_OFFSET (field1),
3617 : flags))
3618 605324 : return false;
3619 : }
3620 : else
3621 : return false;
3622 : }
3623 : }
3624 24135059 : return OP_SAME_WITH_NULL (2);
3625 :
3626 629049 : case BIT_FIELD_REF:
3627 629049 : if (!OP_SAME (0))
3628 : return false;
3629 368676 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3630 368676 : return OP_SAME (1) && OP_SAME (2);
3631 :
3632 : default:
3633 : return false;
3634 : }
3635 :
3636 59769697 : case tcc_expression:
3637 59769697 : switch (TREE_CODE (arg0))
3638 : {
3639 54413192 : case ADDR_EXPR:
3640 : /* Be sure we pass right ADDRESS_OF flag. */
3641 54413192 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3642 54413192 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3643 54413192 : TREE_OPERAND (arg1, 0),
3644 54413192 : flags | OEP_ADDRESS_OF);
3645 :
3646 607406 : case TRUTH_NOT_EXPR:
3647 607406 : return OP_SAME (0);
3648 :
3649 78997 : case TRUTH_ANDIF_EXPR:
3650 78997 : case TRUTH_ORIF_EXPR:
3651 78997 : return OP_SAME (0) && OP_SAME (1);
3652 :
3653 0 : case WIDEN_MULT_PLUS_EXPR:
3654 0 : case WIDEN_MULT_MINUS_EXPR:
3655 0 : if (!OP_SAME (2))
3656 : return false;
3657 : /* The multiplication operands are commutative. */
3658 : /* FALLTHRU */
3659 :
3660 47419 : case TRUTH_AND_EXPR:
3661 47419 : case TRUTH_OR_EXPR:
3662 47419 : case TRUTH_XOR_EXPR:
3663 47419 : if (OP_SAME (0) && OP_SAME (1))
3664 : return true;
3665 :
3666 : /* Otherwise take into account this is a commutative operation. */
3667 47401 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3668 47401 : TREE_OPERAND (arg1, 1), flags)
3669 47404 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3670 3 : TREE_OPERAND (arg1, 0), flags));
3671 :
3672 214179 : case COND_EXPR:
3673 214179 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3674 45397 : return false;
3675 168782 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3676 168782 : return OP_SAME (0);
3677 :
3678 4 : case BIT_INSERT_EXPR:
3679 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3680 : of op1. Need to check to make sure they are the same. */
3681 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3682 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3683 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3684 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3685 : return false;
3686 : /* FALLTHRU */
3687 :
3688 367 : case VEC_COND_EXPR:
3689 367 : case DOT_PROD_EXPR:
3690 367 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3691 :
3692 38325 : case MODIFY_EXPR:
3693 38325 : case INIT_EXPR:
3694 38325 : case COMPOUND_EXPR:
3695 38325 : case PREDECREMENT_EXPR:
3696 38325 : case PREINCREMENT_EXPR:
3697 38325 : case POSTDECREMENT_EXPR:
3698 38325 : case POSTINCREMENT_EXPR:
3699 38325 : if (flags & OEP_LEXICOGRAPHIC)
3700 165 : return OP_SAME (0) && OP_SAME (1);
3701 : return false;
3702 :
3703 314571 : case CLEANUP_POINT_EXPR:
3704 314571 : case EXPR_STMT:
3705 314571 : case SAVE_EXPR:
3706 314571 : if (flags & OEP_LEXICOGRAPHIC)
3707 208 : return OP_SAME (0);
3708 : return false;
3709 :
3710 79755 : case OBJ_TYPE_REF:
3711 : /* Virtual table reference. */
3712 159510 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3713 79755 : OBJ_TYPE_REF_EXPR (arg1), flags))
3714 : return false;
3715 15792 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3716 15792 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3717 15792 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3718 : return false;
3719 15792 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3720 15792 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3721 : return false;
3722 15792 : if (virtual_method_call_p (arg0))
3723 : {
3724 15792 : if (!virtual_method_call_p (arg1))
3725 : return false;
3726 15792 : return types_same_for_odr (obj_type_ref_class (arg0),
3727 31584 : obj_type_ref_class (arg1));
3728 : }
3729 : return false;
3730 :
3731 598 : case OMP_ARRAY_SECTION:
3732 598 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3733 :
3734 : default:
3735 : return false;
3736 : }
3737 :
3738 3755812 : case tcc_vl_exp:
3739 3755812 : switch (TREE_CODE (arg0))
3740 : {
3741 3755812 : case CALL_EXPR:
3742 3755812 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3743 3755812 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3744 : /* If not both CALL_EXPRs are either internal or normal function
3745 : functions, then they are not equal. */
3746 : return false;
3747 3755812 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3748 : {
3749 : /* If the CALL_EXPRs call different internal functions, then they
3750 : are not equal. */
3751 14 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3752 : return false;
3753 : }
3754 : else
3755 : {
3756 : /* If the CALL_EXPRs call different functions, then they are not
3757 : equal. */
3758 3755798 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3759 : flags))
3760 : return false;
3761 : }
3762 :
3763 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3764 2198783 : {
3765 2198783 : unsigned int cef = call_expr_flags (arg0);
3766 2198783 : if (flags & OEP_PURE_SAME)
3767 0 : cef &= ECF_CONST | ECF_PURE;
3768 : else
3769 2198783 : cef &= ECF_CONST;
3770 2198783 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3771 : return false;
3772 : }
3773 :
3774 : /* Now see if all the arguments are the same. */
3775 34162 : {
3776 34162 : const_call_expr_arg_iterator iter0, iter1;
3777 34162 : const_tree a0, a1;
3778 68324 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3779 34162 : a1 = first_const_call_expr_arg (arg1, &iter1);
3780 42287 : a0 && a1;
3781 8125 : a0 = next_const_call_expr_arg (&iter0),
3782 8125 : a1 = next_const_call_expr_arg (&iter1))
3783 35661 : if (! operand_equal_p (a0, a1, flags))
3784 : return false;
3785 :
3786 : /* If we get here and both argument lists are exhausted
3787 : then the CALL_EXPRs are equal. */
3788 6626 : return ! (a0 || a1);
3789 : }
3790 : default:
3791 : return false;
3792 : }
3793 :
3794 171044818 : case tcc_declaration:
3795 : /* Consider __builtin_sqrt equal to sqrt. */
3796 171044818 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3797 6855001 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3798 263274 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3799 6260468 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3800 263274 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3801 :
3802 164784350 : if (DECL_P (arg0)
3803 164784350 : && (flags & OEP_DECL_NAME)
3804 35 : && (flags & OEP_LEXICOGRAPHIC))
3805 : {
3806 : /* Consider decls with the same name equal. The caller needs
3807 : to make sure they refer to the same entity (such as a function
3808 : formal parameter). */
3809 35 : tree a0name = DECL_NAME (arg0);
3810 35 : tree a1name = DECL_NAME (arg1);
3811 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3812 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3813 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3814 : }
3815 : return false;
3816 :
3817 344479616 : case tcc_exceptional:
3818 344479616 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3819 : {
3820 19746 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3821 : return false;
3822 :
3823 : /* In GIMPLE constructors are used only to build vectors from
3824 : elements. Individual elements in the constructor must be
3825 : indexed in increasing order and form an initial sequence.
3826 :
3827 : We make no effort to compare nonconstant ones in GENERIC. */
3828 19746 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3829 : return false;
3830 :
3831 : /* Be sure that vectors constructed have the same representation.
3832 : We only tested element precision and modes to match.
3833 : Vectors may be BLKmode and thus also check that the number of
3834 : parts match. */
3835 1011 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3836 2022 : TYPE_VECTOR_SUBPARTS (type1)))
3837 : return false;
3838 :
3839 1011 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3840 1011 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3841 1011 : unsigned int len = vec_safe_length (v0);
3842 :
3843 2022 : if (len != vec_safe_length (v1))
3844 : return false;
3845 :
3846 4509 : for (unsigned int i = 0; i < len; i++)
3847 : {
3848 3869 : constructor_elt *c0 = &(*v0)[i];
3849 3869 : constructor_elt *c1 = &(*v1)[i];
3850 :
3851 3869 : if (!operand_equal_p (c0->value, c1->value, flags)
3852 : /* In GIMPLE the indexes can be either NULL or matching i.
3853 : Double check this so we won't get false
3854 : positives for GENERIC. */
3855 3528 : || (c0->index
3856 2688 : && (TREE_CODE (c0->index) != INTEGER_CST
3857 2688 : || compare_tree_int (c0->index, i)))
3858 7397 : || (c1->index
3859 2688 : && (TREE_CODE (c1->index) != INTEGER_CST
3860 2688 : || compare_tree_int (c1->index, i))))
3861 341 : return false;
3862 : }
3863 : return true;
3864 : }
3865 344459870 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3866 3288 : && (flags & OEP_LEXICOGRAPHIC))
3867 : {
3868 : /* Compare the STATEMENT_LISTs. */
3869 16 : tree_stmt_iterator tsi1, tsi2;
3870 16 : tree body1 = const_cast<tree> (arg0);
3871 16 : tree body2 = const_cast<tree> (arg1);
3872 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3873 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3874 : {
3875 : /* The lists don't have the same number of statements. */
3876 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3877 : return false;
3878 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3879 : return true;
3880 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3881 : flags & (OEP_LEXICOGRAPHIC
3882 : | OEP_NO_HASH_CHECK)))
3883 : return false;
3884 : }
3885 : }
3886 : return false;
3887 :
3888 2593810 : case tcc_statement:
3889 2593810 : switch (TREE_CODE (arg0))
3890 : {
3891 52 : case RETURN_EXPR:
3892 52 : if (flags & OEP_LEXICOGRAPHIC)
3893 52 : return OP_SAME_WITH_NULL (0);
3894 : return false;
3895 4 : case DEBUG_BEGIN_STMT:
3896 4 : if (flags & OEP_LEXICOGRAPHIC)
3897 : return true;
3898 : return false;
3899 : default:
3900 : return false;
3901 : }
3902 :
3903 : default:
3904 : return false;
3905 : }
3906 :
3907 : #undef OP_SAME
3908 : #undef OP_SAME_WITH_NULL
3909 : }
3910 :
3911 : /* Generate a hash value for an expression. This can be used iteratively
3912 : by passing a previous result as the HSTATE argument. */
3913 :
3914 : void
3915 3068283137 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3916 : unsigned int flags)
3917 : {
3918 3068283137 : int i;
3919 3068283137 : enum tree_code code;
3920 3068283137 : enum tree_code_class tclass;
3921 :
3922 3068283137 : if (t == NULL_TREE || t == error_mark_node)
3923 : {
3924 77296372 : hstate.merge_hash (0);
3925 77296372 : return;
3926 : }
3927 :
3928 2990986765 : STRIP_ANY_LOCATION_WRAPPER (t);
3929 :
3930 2990986765 : if (!(flags & OEP_ADDRESS_OF))
3931 2736840816 : STRIP_NOPS (t);
3932 :
3933 2990986765 : code = TREE_CODE (t);
3934 :
3935 2990986765 : switch (code)
3936 : {
3937 : /* Alas, constants aren't shared, so we can't rely on pointer
3938 : identity. */
3939 903 : case VOID_CST:
3940 903 : hstate.merge_hash (0);
3941 903 : return;
3942 882935203 : case INTEGER_CST:
3943 882935203 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3944 1783749566 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3945 900814363 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3946 : return;
3947 15725351 : case REAL_CST:
3948 15725351 : {
3949 15725351 : unsigned int val2;
3950 15725351 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3951 : val2 = rvc_zero;
3952 : else
3953 15511229 : val2 = real_hash (TREE_REAL_CST_PTR (t));
3954 15725351 : hstate.merge_hash (val2);
3955 15725351 : return;
3956 : }
3957 0 : case FIXED_CST:
3958 0 : {
3959 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
3960 0 : hstate.merge_hash (val2);
3961 0 : return;
3962 : }
3963 12695251 : case STRING_CST:
3964 12695251 : hstate.add ((const void *) TREE_STRING_POINTER (t),
3965 12695251 : TREE_STRING_LENGTH (t));
3966 12695251 : return;
3967 209 : case RAW_DATA_CST:
3968 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
3969 209 : RAW_DATA_LENGTH (t));
3970 209 : return;
3971 210944 : case COMPLEX_CST:
3972 210944 : hash_operand (TREE_REALPART (t), hstate, flags);
3973 210944 : hash_operand (TREE_IMAGPART (t), hstate, flags);
3974 210944 : return;
3975 3441517 : case VECTOR_CST:
3976 3441517 : {
3977 3441517 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
3978 3441517 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3979 3441517 : unsigned int count = vector_cst_encoded_nelts (t);
3980 10737939 : for (unsigned int i = 0; i < count; ++i)
3981 7296422 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3982 : return;
3983 : }
3984 889888822 : case SSA_NAME:
3985 : /* We can just compare by pointer. */
3986 889888822 : hstate.add_hwi (SSA_NAME_VERSION (t));
3987 889888822 : return;
3988 : case PLACEHOLDER_EXPR:
3989 : /* The node itself doesn't matter. */
3990 : return;
3991 : case BLOCK:
3992 : case OMP_CLAUSE:
3993 : case OMP_NEXT_VARIANT:
3994 : case OMP_TARGET_DEVICE_MATCHES:
3995 : /* Ignore. */
3996 : return;
3997 : case TREE_LIST:
3998 : /* A list of expressions, for a CALL_EXPR or as the elements of a
3999 : VECTOR_CST. */
4000 300542 : for (; t; t = TREE_CHAIN (t))
4001 150271 : hash_operand (TREE_VALUE (t), hstate, flags);
4002 : return;
4003 4945286 : case CONSTRUCTOR:
4004 4945286 : {
4005 4945286 : unsigned HOST_WIDE_INT idx;
4006 4945286 : tree field, value;
4007 4945286 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4008 4945286 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4009 19916043 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4010 : {
4011 : /* In GIMPLE the indexes can be either NULL or matching i. */
4012 14970757 : if (field == NULL_TREE)
4013 1108954 : field = bitsize_int (idx);
4014 14970757 : if (TREE_CODE (field) == FIELD_DECL)
4015 : {
4016 9968148 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4017 9968148 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4018 : }
4019 : else
4020 5002609 : hash_operand (field, hstate, flags);
4021 14970757 : hash_operand (value, hstate, flags);
4022 : }
4023 : return;
4024 : }
4025 182 : case STATEMENT_LIST:
4026 182 : {
4027 182 : tree_stmt_iterator i;
4028 182 : for (i = tsi_start (const_cast<tree> (t));
4029 550 : !tsi_end_p (i); tsi_next (&i))
4030 368 : hash_operand (tsi_stmt (i), hstate, flags);
4031 182 : return;
4032 : }
4033 : case TREE_VEC:
4034 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4035 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4036 : return;
4037 4 : case IDENTIFIER_NODE:
4038 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4039 4 : return;
4040 21093077 : case FUNCTION_DECL:
4041 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4042 : Otherwise nodes that compare equal according to operand_equal_p might
4043 : get different hash codes. However, don't do this for machine specific
4044 : or front end builtins, since the function code is overloaded in those
4045 : cases. */
4046 21093077 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4047 21093077 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4048 : {
4049 7033611 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4050 7033611 : code = TREE_CODE (t);
4051 : }
4052 : /* FALL THROUGH */
4053 1180992695 : default:
4054 1180992695 : if (POLY_INT_CST_P (t))
4055 : {
4056 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4057 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4058 : return;
4059 : }
4060 1180992695 : tclass = TREE_CODE_CLASS (code);
4061 :
4062 1180992695 : if (tclass == tcc_declaration)
4063 : {
4064 : /* DECL's have a unique ID */
4065 844364634 : hstate.add_hwi (DECL_UID (t));
4066 : }
4067 336628061 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4068 : {
4069 : /* For comparisons that can be swapped, use the lower
4070 : tree code. */
4071 143086 : enum tree_code ccode = swap_tree_comparison (code);
4072 143086 : if (code < ccode)
4073 60941 : ccode = code;
4074 143086 : hstate.add_object (ccode);
4075 143086 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4076 143086 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4077 : }
4078 336484975 : else if (CONVERT_EXPR_CODE_P (code))
4079 : {
4080 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4081 : operand_equal_p. */
4082 6749352 : enum tree_code ccode = NOP_EXPR;
4083 6749352 : hstate.add_object (ccode);
4084 :
4085 : /* Don't hash the type, that can lead to having nodes which
4086 : compare equal according to operand_equal_p, but which
4087 : have different hash codes. Make sure to include signedness
4088 : in the hash computation. */
4089 6749352 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4090 6749352 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4091 : }
4092 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4093 329735623 : else if (code == MEM_REF
4094 79450368 : && (flags & OEP_ADDRESS_OF) != 0
4095 70055163 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4096 14144401 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4097 343664542 : && integer_zerop (TREE_OPERAND (t, 1)))
4098 6275632 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4099 : hstate, flags);
4100 : /* Don't ICE on FE specific trees, or their arguments etc.
4101 : during operand_equal_p hash verification. */
4102 323459991 : else if (!IS_EXPR_CODE_CLASS (tclass))
4103 384 : gcc_assert (flags & OEP_HASH_CHECK);
4104 : else
4105 : {
4106 323459607 : unsigned int sflags = flags;
4107 :
4108 323459607 : hstate.add_object (code);
4109 :
4110 323459607 : switch (code)
4111 : {
4112 129815805 : case ADDR_EXPR:
4113 129815805 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4114 129815805 : flags |= OEP_ADDRESS_OF;
4115 129815805 : sflags = flags;
4116 129815805 : break;
4117 :
4118 78044245 : case INDIRECT_REF:
4119 78044245 : case MEM_REF:
4120 78044245 : case TARGET_MEM_REF:
4121 78044245 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4122 78044245 : sflags = flags;
4123 78044245 : break;
4124 :
4125 75203733 : case COMPONENT_REF:
4126 75203733 : if (sflags & OEP_ADDRESS_OF)
4127 : {
4128 37780539 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4129 37780539 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4130 : hstate, flags & ~OEP_ADDRESS_OF);
4131 37780539 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4132 : hstate, flags & ~OEP_ADDRESS_OF);
4133 37780539 : return;
4134 : }
4135 : break;
4136 15722570 : case ARRAY_REF:
4137 15722570 : case ARRAY_RANGE_REF:
4138 15722570 : case BIT_FIELD_REF:
4139 15722570 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4140 15722570 : break;
4141 :
4142 8438 : case COND_EXPR:
4143 8438 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4144 8438 : break;
4145 :
4146 0 : case WIDEN_MULT_PLUS_EXPR:
4147 0 : case WIDEN_MULT_MINUS_EXPR:
4148 0 : {
4149 : /* The multiplication operands are commutative. */
4150 0 : inchash::hash one, two;
4151 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4152 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4153 0 : hstate.add_commutative (one, two);
4154 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4155 0 : return;
4156 : }
4157 :
4158 37045 : case CALL_EXPR:
4159 37045 : if (CALL_EXPR_FN (t) == NULL_TREE)
4160 14 : hstate.add_int (CALL_EXPR_IFN (t));
4161 : break;
4162 :
4163 72 : case TARGET_EXPR:
4164 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4165 : Usually different TARGET_EXPRs just should use
4166 : different temporaries in their slots. */
4167 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4168 72 : return;
4169 :
4170 284822 : case OBJ_TYPE_REF:
4171 : /* Virtual table reference. */
4172 284822 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4173 284822 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4174 284822 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4175 284822 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4176 284822 : if (!virtual_method_call_p (t))
4177 : return;
4178 284801 : if (tree c = obj_type_ref_class (t))
4179 : {
4180 284801 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4181 : /* We compute mangled names only when free_lang_data is run.
4182 : In that case we can hash precisely. */
4183 284801 : if (TREE_CODE (c) == TYPE_DECL
4184 284801 : && DECL_ASSEMBLER_NAME_SET_P (c))
4185 7309 : hstate.add_object
4186 7309 : (IDENTIFIER_HASH_VALUE
4187 : (DECL_ASSEMBLER_NAME (c)));
4188 : }
4189 284801 : return;
4190 : default:
4191 : break;
4192 : }
4193 :
4194 : /* Don't hash the type, that can lead to having nodes which
4195 : compare equal according to operand_equal_p, but which
4196 : have different hash codes. */
4197 285394174 : if (code == NON_LVALUE_EXPR)
4198 : {
4199 : /* Make sure to include signness in the hash computation. */
4200 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4201 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4202 : }
4203 :
4204 285394174 : else if (commutative_tree_code (code))
4205 : {
4206 : /* It's a commutative expression. We want to hash it the same
4207 : however it appears. We do this by first hashing both operands
4208 : and then rehashing based on the order of their independent
4209 : hashes. */
4210 18006726 : inchash::hash one, two;
4211 18006726 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4212 18006726 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4213 18006726 : hstate.add_commutative (one, two);
4214 : }
4215 : else
4216 746446947 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4217 690731814 : hash_operand (TREE_OPERAND (t, i), hstate,
4218 : i == 0 ? flags : sflags);
4219 : }
4220 : return;
4221 : }
4222 : }
4223 :
4224 : bool
4225 7263408972 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4226 : unsigned int flags, bool *ret)
4227 : {
4228 : /* When checking and unless comparing DECL names, verify that if
4229 : the outermost operand_equal_p call returns non-zero then ARG0
4230 : and ARG1 have the same hash value. */
4231 7263408972 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4232 : {
4233 3054230972 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4234 : {
4235 471590499 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4236 : {
4237 84145477 : inchash::hash hstate0 (0), hstate1 (0);
4238 84145477 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4239 84145477 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4240 84145477 : hashval_t h0 = hstate0.end ();
4241 84145477 : hashval_t h1 = hstate1.end ();
4242 84145477 : gcc_assert (h0 == h1);
4243 : }
4244 471590499 : *ret = true;
4245 : }
4246 : else
4247 2582640473 : *ret = false;
4248 :
4249 3054230972 : return true;
4250 : }
4251 :
4252 : return false;
4253 : }
4254 :
4255 :
4256 : static operand_compare default_compare_instance;
4257 :
4258 : /* Convenience wrapper around operand_compare class because usually we do
4259 : not need to play with the valueizer. */
4260 :
4261 : bool
4262 3052385586 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4263 : {
4264 3052385586 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4265 : }
4266 :
4267 : namespace inchash
4268 : {
4269 :
4270 : /* Generate a hash value for an expression. This can be used iteratively
4271 : by passing a previous result as the HSTATE argument.
4272 :
4273 : This function is intended to produce the same hash for expressions which
4274 : would compare equal using operand_equal_p. */
4275 : void
4276 2206989475 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4277 : {
4278 2206989475 : default_compare_instance.hash_operand (t, hstate, flags);
4279 2206989475 : }
4280 :
4281 : }
4282 :
4283 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4284 : with a different signedness or a narrower precision. */
4285 :
4286 : static bool
4287 20245894 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4288 : {
4289 20245894 : if (operand_equal_p (arg0, arg1, 0))
4290 : return true;
4291 :
4292 38749278 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4293 33083233 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4294 : return false;
4295 :
4296 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4297 : and see if the inner values are the same. This removes any
4298 : signedness comparison, which doesn't matter here. */
4299 6144788 : tree op0 = arg0;
4300 6144788 : tree op1 = arg1;
4301 6144788 : STRIP_NOPS (op0);
4302 6144788 : STRIP_NOPS (op1);
4303 6144788 : if (operand_equal_p (op0, op1, 0))
4304 : return true;
4305 :
4306 : /* Discard a single widening conversion from ARG1 and see if the inner
4307 : value is the same as ARG0. */
4308 5082647 : if (CONVERT_EXPR_P (arg1)
4309 880181 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4310 880133 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4311 880133 : < TYPE_PRECISION (TREE_TYPE (arg1))
4312 6267321 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4313 : return true;
4314 :
4315 : return false;
4316 : }
4317 :
4318 : /* See if ARG is an expression that is either a comparison or is performing
4319 : arithmetic on comparisons. The comparisons must only be comparing
4320 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4321 : they are nonzero it means that some operands have already been found.
4322 : No variables may be used anywhere else in the expression except in the
4323 : comparisons.
4324 :
4325 : If this is true, return 1. Otherwise, return zero. */
4326 :
4327 : static bool
4328 59646734 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4329 : {
4330 63594832 : enum tree_code code = TREE_CODE (arg);
4331 63594832 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4332 :
4333 : /* We can handle some of the tcc_expression cases here. */
4334 63594832 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4335 : tclass = tcc_unary;
4336 63007240 : else if (tclass == tcc_expression
4337 691826 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4338 691826 : || code == COMPOUND_EXPR))
4339 : tclass = tcc_binary;
4340 :
4341 62996473 : switch (tclass)
4342 : {
4343 3948098 : case tcc_unary:
4344 3948098 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4345 :
4346 5335902 : case tcc_binary:
4347 5335902 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4348 5335902 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4349 :
4350 : case tcc_constant:
4351 : return true;
4352 :
4353 681059 : case tcc_expression:
4354 681059 : if (code == COND_EXPR)
4355 718 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4356 718 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4357 782 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4358 : return false;
4359 :
4360 592100 : case tcc_comparison:
4361 : /* First see if we can handle the first operand, then the second. For
4362 : the second operand, we know *CVAL1 can't be zero. It must be that
4363 : one side of the comparison is each of the values; test for the
4364 : case where this isn't true by failing if the two operands
4365 : are the same. */
4366 :
4367 592100 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4368 592100 : TREE_OPERAND (arg, 1), 0))
4369 : return false;
4370 :
4371 592100 : if (*cval1 == 0)
4372 590050 : *cval1 = TREE_OPERAND (arg, 0);
4373 2050 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4374 : ;
4375 1931 : else if (*cval2 == 0)
4376 0 : *cval2 = TREE_OPERAND (arg, 0);
4377 1931 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4378 : ;
4379 : else
4380 : return false;
4381 :
4382 590169 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4383 : ;
4384 590169 : else if (*cval2 == 0)
4385 590050 : *cval2 = TREE_OPERAND (arg, 1);
4386 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4387 : ;
4388 : else
4389 : return false;
4390 :
4391 : return true;
4392 :
4393 : default:
4394 : return false;
4395 : }
4396 : }
4397 :
4398 : /* ARG is a tree that is known to contain just arithmetic operations and
4399 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4400 : any occurrence of OLD0 as an operand of a comparison and likewise for
4401 : NEW1 and OLD1. */
4402 :
4403 : static tree
4404 627 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4405 : tree old1, tree new1)
4406 : {
4407 627 : tree type = TREE_TYPE (arg);
4408 627 : enum tree_code code = TREE_CODE (arg);
4409 627 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4410 :
4411 : /* We can handle some of the tcc_expression cases here. */
4412 627 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4413 : tclass = tcc_unary;
4414 627 : else if (tclass == tcc_expression
4415 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4416 : tclass = tcc_binary;
4417 :
4418 618 : switch (tclass)
4419 : {
4420 168 : case tcc_unary:
4421 168 : return fold_build1_loc (loc, code, type,
4422 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4423 168 : old0, new0, old1, new1));
4424 :
4425 141 : case tcc_binary:
4426 282 : return fold_build2_loc (loc, code, type,
4427 141 : eval_subst (loc, TREE_OPERAND (arg, 0),
4428 : old0, new0, old1, new1),
4429 141 : eval_subst (loc, TREE_OPERAND (arg, 1),
4430 141 : old0, new0, old1, new1));
4431 :
4432 9 : case tcc_expression:
4433 9 : switch (code)
4434 : {
4435 0 : case SAVE_EXPR:
4436 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4437 0 : old1, new1);
4438 :
4439 0 : case COMPOUND_EXPR:
4440 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4441 0 : old1, new1);
4442 :
4443 9 : case COND_EXPR:
4444 27 : return fold_build3_loc (loc, code, type,
4445 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4446 : old0, new0, old1, new1),
4447 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4448 : old0, new0, old1, new1),
4449 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4450 9 : old0, new0, old1, new1));
4451 : default:
4452 : break;
4453 : }
4454 : /* Fall through - ??? */
4455 :
4456 156 : case tcc_comparison:
4457 156 : {
4458 156 : tree arg0 = TREE_OPERAND (arg, 0);
4459 156 : tree arg1 = TREE_OPERAND (arg, 1);
4460 :
4461 : /* We need to check both for exact equality and tree equality. The
4462 : former will be true if the operand has a side-effect. In that
4463 : case, we know the operand occurred exactly once. */
4464 :
4465 156 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4466 : arg0 = new0;
4467 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4468 : arg0 = new1;
4469 :
4470 156 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4471 : arg1 = new0;
4472 156 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4473 : arg1 = new1;
4474 :
4475 156 : return fold_build2_loc (loc, code, type, arg0, arg1);
4476 : }
4477 :
4478 : default:
4479 : return arg;
4480 : }
4481 : }
4482 :
4483 : /* Return a tree for the case when the result of an expression is RESULT
4484 : converted to TYPE and OMITTED was previously an operand of the expression
4485 : but is now not needed (e.g., we folded OMITTED * 0).
4486 :
4487 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4488 : the conversion of RESULT to TYPE. */
4489 :
4490 : tree
4491 274467 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4492 : {
4493 274467 : tree t = fold_convert_loc (loc, type, result);
4494 :
4495 : /* If the resulting operand is an empty statement, just return the omitted
4496 : statement casted to void. */
4497 274467 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4498 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4499 0 : fold_ignored_result (omitted));
4500 :
4501 274467 : if (TREE_SIDE_EFFECTS (omitted))
4502 11904 : return build2_loc (loc, COMPOUND_EXPR, type,
4503 11904 : fold_ignored_result (omitted), t);
4504 :
4505 262563 : return non_lvalue_loc (loc, t);
4506 : }
4507 :
4508 : /* Return a tree for the case when the result of an expression is RESULT
4509 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4510 : of the expression but are now not needed.
4511 :
4512 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4513 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4514 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4515 : just do the conversion of RESULT to TYPE. */
4516 :
4517 : tree
4518 5676 : omit_two_operands_loc (location_t loc, tree type, tree result,
4519 : tree omitted1, tree omitted2)
4520 : {
4521 5676 : tree t = fold_convert_loc (loc, type, result);
4522 :
4523 5676 : if (TREE_SIDE_EFFECTS (omitted2))
4524 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4525 5676 : if (TREE_SIDE_EFFECTS (omitted1))
4526 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4527 :
4528 5676 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4529 : }
4530 :
4531 :
4532 : /* Return a simplified tree node for the truth-negation of ARG. This
4533 : never alters ARG itself. We assume that ARG is an operation that
4534 : returns a truth value (0 or 1).
4535 :
4536 : FIXME: one would think we would fold the result, but it causes
4537 : problems with the dominator optimizer. */
4538 :
4539 : static tree
4540 50373669 : fold_truth_not_expr (location_t loc, tree arg)
4541 : {
4542 50373669 : tree type = TREE_TYPE (arg);
4543 50373669 : enum tree_code code = TREE_CODE (arg);
4544 50373669 : location_t loc1, loc2;
4545 :
4546 : /* If this is a comparison, we can simply invert it, except for
4547 : floating-point non-equality comparisons, in which case we just
4548 : enclose a TRUTH_NOT_EXPR around what we have. */
4549 :
4550 50373669 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4551 : {
4552 38406874 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4553 32002134 : if (FLOAT_TYPE_P (op_type)
4554 6414827 : && flag_trapping_math
4555 6384120 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4556 44750951 : && code != NE_EXPR && code != EQ_EXPR)
4557 : return NULL_TREE;
4558 :
4559 32755693 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4560 32755693 : if (code == ERROR_MARK)
4561 : return NULL_TREE;
4562 :
4563 32755693 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4564 32755693 : TREE_OPERAND (arg, 1));
4565 32755693 : copy_warning (ret, arg);
4566 32755693 : return ret;
4567 : }
4568 :
4569 11966795 : switch (code)
4570 : {
4571 0 : case INTEGER_CST:
4572 0 : return constant_boolean_node (integer_zerop (arg), type);
4573 :
4574 51400 : case TRUTH_AND_EXPR:
4575 51400 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4576 51400 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4577 102800 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4578 51400 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4579 102800 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4580 :
4581 2556 : case TRUTH_OR_EXPR:
4582 2556 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4583 2556 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4584 5112 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4585 2556 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4586 5112 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4587 :
4588 72761 : case TRUTH_XOR_EXPR:
4589 : /* Here we can invert either operand. We invert the first operand
4590 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4591 : result is the XOR of the first operand with the inside of the
4592 : negation of the second operand. */
4593 :
4594 72761 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4595 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4596 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4597 : else
4598 72573 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4599 72573 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4600 145146 : TREE_OPERAND (arg, 1));
4601 :
4602 393234 : case TRUTH_ANDIF_EXPR:
4603 393234 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4604 393234 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4605 786468 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4606 393234 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4607 786468 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4608 :
4609 16722 : case TRUTH_ORIF_EXPR:
4610 16722 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4611 16722 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4612 33444 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4613 16722 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4614 33444 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4615 :
4616 788973 : case TRUTH_NOT_EXPR:
4617 788973 : return TREE_OPERAND (arg, 0);
4618 :
4619 9748 : case COND_EXPR:
4620 9748 : {
4621 9748 : tree arg1 = TREE_OPERAND (arg, 1);
4622 9748 : tree arg2 = TREE_OPERAND (arg, 2);
4623 :
4624 9748 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4625 9748 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4626 :
4627 : /* A COND_EXPR may have a throw as one operand, which
4628 : then has void type. Just leave void operands
4629 : as they are. */
4630 9748 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4631 9748 : VOID_TYPE_P (TREE_TYPE (arg1))
4632 9748 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4633 9748 : VOID_TYPE_P (TREE_TYPE (arg2))
4634 19493 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4635 : }
4636 :
4637 959 : case COMPOUND_EXPR:
4638 959 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4639 1918 : return build2_loc (loc, COMPOUND_EXPR, type,
4640 959 : TREE_OPERAND (arg, 0),
4641 1918 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4642 :
4643 0 : case NON_LVALUE_EXPR:
4644 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4645 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4646 :
4647 73859 : CASE_CONVERT:
4648 73859 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4649 73795 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4650 :
4651 : /* fall through */
4652 :
4653 64 : case FLOAT_EXPR:
4654 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4655 64 : return build1_loc (loc, TREE_CODE (arg), type,
4656 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4657 :
4658 498 : case BIT_AND_EXPR:
4659 498 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4660 : return NULL_TREE;
4661 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4662 :
4663 2 : case SAVE_EXPR:
4664 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4665 :
4666 331 : case CLEANUP_POINT_EXPR:
4667 331 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4668 331 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4669 662 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4670 :
4671 : default:
4672 : return NULL_TREE;
4673 : }
4674 : }
4675 :
4676 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4677 : assume that ARG is an operation that returns a truth value (0 or 1
4678 : for scalars, 0 or -1 for vectors). Return the folded expression if
4679 : folding is successful. Otherwise, return NULL_TREE. */
4680 :
4681 : static tree
4682 2076445 : fold_invert_truthvalue (location_t loc, tree arg)
4683 : {
4684 2076445 : tree type = TREE_TYPE (arg);
4685 4152866 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4686 : ? BIT_NOT_EXPR
4687 : : TRUTH_NOT_EXPR,
4688 2076445 : type, arg);
4689 : }
4690 :
4691 : /* Return a simplified tree node for the truth-negation of ARG. This
4692 : never alters ARG itself. We assume that ARG is an operation that
4693 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4694 :
4695 : tree
4696 42981714 : invert_truthvalue_loc (location_t loc, tree arg)
4697 : {
4698 42981714 : if (TREE_CODE (arg) == ERROR_MARK)
4699 : return arg;
4700 :
4701 42981714 : tree type = TREE_TYPE (arg);
4702 85963428 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4703 : ? BIT_NOT_EXPR
4704 : : TRUTH_NOT_EXPR,
4705 42981714 : type, arg);
4706 : }
4707 :
4708 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4709 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4710 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4711 : is the original memory reference used to preserve the alias set of
4712 : the access. */
4713 :
4714 : tree
4715 787812 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4716 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4717 : int unsignedp, int reversep)
4718 : {
4719 787812 : tree result, bftype;
4720 :
4721 : /* Attempt not to lose the access path if possible. */
4722 787812 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4723 : {
4724 784038 : tree ninner = TREE_OPERAND (orig_inner, 0);
4725 784038 : machine_mode nmode;
4726 784038 : poly_int64 nbitsize, nbitpos;
4727 784038 : tree noffset;
4728 784038 : int nunsignedp, nreversep, nvolatilep = 0;
4729 784038 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4730 : &noffset, &nmode, &nunsignedp,
4731 : &nreversep, &nvolatilep);
4732 784038 : if (base == inner
4733 783904 : && noffset == NULL_TREE
4734 783904 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4735 783897 : && !reversep
4736 783825 : && !nreversep
4737 1567863 : && !nvolatilep)
4738 : {
4739 783825 : inner = ninner;
4740 784038 : bitpos -= nbitpos;
4741 : }
4742 : }
4743 :
4744 787812 : alias_set_type iset = get_alias_set (orig_inner);
4745 787812 : if (iset == 0 && get_alias_set (inner) != iset)
4746 234 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4747 : build_fold_addr_expr (inner),
4748 : build_int_cst (ptr_type_node, 0));
4749 :
4750 787812 : if (known_eq (bitpos, 0) && !reversep)
4751 : {
4752 12717 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4753 25434 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4754 12555 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4755 166 : && tree_fits_shwi_p (size)
4756 12883 : && tree_to_shwi (size) == bitsize)
4757 143 : return fold_convert_loc (loc, type, inner);
4758 : }
4759 :
4760 787669 : bftype = type;
4761 787669 : if (TYPE_PRECISION (bftype) != bitsize
4762 787669 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4763 501 : bftype = build_nonstandard_integer_type (bitsize, 0);
4764 :
4765 787669 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4766 787669 : bitsize_int (bitsize), bitsize_int (bitpos));
4767 787669 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4768 :
4769 787669 : if (bftype != type)
4770 501 : result = fold_convert_loc (loc, type, result);
4771 :
4772 : return result;
4773 : }
4774 :
4775 : /* Optimize a bit-field compare.
4776 :
4777 : There are two cases: First is a compare against a constant and the
4778 : second is a comparison of two items where the fields are at the same
4779 : bit position relative to the start of a chunk (byte, halfword, word)
4780 : large enough to contain it. In these cases we can avoid the shift
4781 : implicit in bitfield extractions.
4782 :
4783 : For constants, we emit a compare of the shifted constant with the
4784 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4785 : compared. For two fields at the same position, we do the ANDs with the
4786 : similar mask and compare the result of the ANDs.
4787 :
4788 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4789 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4790 : are the left and right operands of the comparison, respectively.
4791 :
4792 : If the optimization described above can be done, we return the resulting
4793 : tree. Otherwise we return zero. */
4794 :
4795 : static tree
4796 4562902 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4797 : tree compare_type, tree lhs, tree rhs)
4798 : {
4799 4562902 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4800 4562902 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4801 4562902 : tree type = TREE_TYPE (lhs);
4802 4562902 : tree unsigned_type;
4803 4562902 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4804 4562902 : machine_mode lmode, rmode;
4805 4562902 : scalar_int_mode nmode;
4806 4562902 : int lunsignedp, runsignedp;
4807 4562902 : int lreversep, rreversep;
4808 4562902 : int lvolatilep = 0, rvolatilep = 0;
4809 4562902 : tree linner, rinner = NULL_TREE;
4810 4562902 : tree mask;
4811 4562902 : tree offset;
4812 :
4813 : /* Get all the information about the extractions being done. If the bit size
4814 : is the same as the size of the underlying object, we aren't doing an
4815 : extraction at all and so can do nothing. We also don't want to
4816 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4817 : then will no longer be able to replace it. */
4818 4562902 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4819 : &lunsignedp, &lreversep, &lvolatilep);
4820 4562902 : if (linner == lhs
4821 4562902 : || !known_size_p (plbitsize)
4822 4562902 : || !plbitsize.is_constant (&lbitsize)
4823 4562902 : || !plbitpos.is_constant (&lbitpos)
4824 9125804 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4825 748875 : || offset != 0
4826 748850 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4827 5311752 : || lvolatilep)
4828 3814112 : return 0;
4829 :
4830 748790 : if (const_p)
4831 710694 : rreversep = lreversep;
4832 : else
4833 : {
4834 : /* If this is not a constant, we can only do something if bit positions,
4835 : sizes, signedness and storage order are the same. */
4836 38096 : rinner
4837 38096 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4838 : &runsignedp, &rreversep, &rvolatilep);
4839 :
4840 38096 : if (rinner == rhs
4841 38052 : || maybe_ne (lbitpos, rbitpos)
4842 38018 : || maybe_ne (lbitsize, rbitsize)
4843 38018 : || lunsignedp != runsignedp
4844 38018 : || lreversep != rreversep
4845 38018 : || offset != 0
4846 38018 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4847 76114 : || rvolatilep)
4848 : return 0;
4849 : }
4850 :
4851 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4852 748712 : poly_uint64 bitstart = 0;
4853 748712 : poly_uint64 bitend = 0;
4854 748712 : if (TREE_CODE (lhs) == COMPONENT_REF)
4855 : {
4856 748712 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4857 748712 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4858 : return 0;
4859 : }
4860 :
4861 : /* See if we can find a mode to refer to this field. We should be able to,
4862 : but fail if we can't. */
4863 1497424 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4864 710694 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4865 38018 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4866 : TYPE_ALIGN (TREE_TYPE (rinner))),
4867 748712 : BITS_PER_WORD, false, &nmode))
4868 : return 0;
4869 :
4870 : /* Set signed and unsigned types of the precision of this mode for the
4871 : shifts below. */
4872 746709 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4873 :
4874 : /* Compute the bit position and size for the new reference and our offset
4875 : within it. If the new reference is the same size as the original, we
4876 : won't optimize anything, so return zero. */
4877 746709 : nbitsize = GET_MODE_BITSIZE (nmode);
4878 746709 : nbitpos = lbitpos & ~ (nbitsize - 1);
4879 746709 : lbitpos -= nbitpos;
4880 746709 : if (nbitsize == lbitsize)
4881 : return 0;
4882 :
4883 746709 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4884 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4885 :
4886 : /* Make the mask to be used against the extracted field. */
4887 746709 : mask = build_int_cst_type (unsigned_type, -1);
4888 746709 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4889 746709 : mask = const_binop (RSHIFT_EXPR, mask,
4890 746709 : size_int (nbitsize - lbitsize - lbitpos));
4891 :
4892 746709 : if (! const_p)
4893 : {
4894 36463 : if (nbitpos < 0)
4895 : return 0;
4896 :
4897 : /* If not comparing with constant, just rework the comparison
4898 : and return. */
4899 36463 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4900 36463 : nbitsize, nbitpos, 1, lreversep);
4901 36463 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4902 36463 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4903 36463 : nbitsize, nbitpos, 1, rreversep);
4904 36463 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4905 36463 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4906 : }
4907 :
4908 : /* Otherwise, we are handling the constant case. See if the constant is too
4909 : big for the field. Warn and return a tree for 0 (false) if so. We do
4910 : this not only for its own sake, but to avoid having to test for this
4911 : error case below. If we didn't, we might generate wrong code.
4912 :
4913 : For unsigned fields, the constant shifted right by the field length should
4914 : be all zero. For signed fields, the high-order bits should agree with
4915 : the sign bit. */
4916 :
4917 710246 : if (lunsignedp)
4918 : {
4919 709081 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4920 : {
4921 0 : warning (0, "comparison is always %d due to width of bit-field",
4922 : code == NE_EXPR);
4923 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4924 : }
4925 : }
4926 : else
4927 : {
4928 1165 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4929 1165 : if (tem != 0 && tem != -1)
4930 : {
4931 0 : warning (0, "comparison is always %d due to width of bit-field",
4932 : code == NE_EXPR);
4933 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4934 : }
4935 1165 : }
4936 :
4937 710246 : if (nbitpos < 0)
4938 : return 0;
4939 :
4940 : /* Single-bit compares should always be against zero. */
4941 710246 : if (lbitsize == 1 && ! integer_zerop (rhs))
4942 : {
4943 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4944 175 : rhs = build_int_cst (type, 0);
4945 : }
4946 :
4947 : /* Make a new bitfield reference, shift the constant over the
4948 : appropriate number of bits and mask it with the computed mask
4949 : (in case this was a signed field). If we changed it, make a new one. */
4950 710246 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4951 710246 : nbitsize, nbitpos, 1, lreversep);
4952 :
4953 710246 : rhs = const_binop (BIT_AND_EXPR,
4954 : const_binop (LSHIFT_EXPR,
4955 : fold_convert_loc (loc, unsigned_type, rhs),
4956 710246 : size_int (lbitpos)),
4957 : mask);
4958 :
4959 710246 : lhs = build2_loc (loc, code, compare_type,
4960 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4961 710246 : return lhs;
4962 : }
4963 :
4964 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4965 : represents the sign bit of EXP's type. If EXP represents a sign
4966 : or zero extension, also test VAL against the unextended type.
4967 : The return value is the (sub)expression whose sign bit is VAL,
4968 : or NULL_TREE otherwise. */
4969 :
4970 : tree
4971 2207 : sign_bit_p (tree exp, const_tree val)
4972 : {
4973 2207 : int width;
4974 2207 : tree t;
4975 :
4976 : /* Tree EXP must have an integral type. */
4977 2207 : t = TREE_TYPE (exp);
4978 2207 : if (! INTEGRAL_TYPE_P (t))
4979 : return NULL_TREE;
4980 :
4981 : /* Tree VAL must be an integer constant. */
4982 1861 : if (TREE_CODE (val) != INTEGER_CST
4983 1861 : || TREE_OVERFLOW (val))
4984 : return NULL_TREE;
4985 :
4986 1499 : width = TYPE_PRECISION (t);
4987 1499 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
4988 : return exp;
4989 :
4990 : /* Handle extension from a narrower type. */
4991 862 : if (TREE_CODE (exp) == NOP_EXPR
4992 862 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4993 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
4994 :
4995 : return NULL_TREE;
4996 : }
4997 :
4998 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4999 : operand is simple enough to be evaluated unconditionally. */
5000 :
5001 : static bool
5002 65294907 : simple_operand_p (const_tree exp)
5003 : {
5004 : /* Strip any conversions that don't change the machine mode. */
5005 65294907 : STRIP_NOPS (exp);
5006 :
5007 65294907 : return (CONSTANT_CLASS_P (exp)
5008 45325446 : || TREE_CODE (exp) == SSA_NAME
5009 81221443 : || (DECL_P (exp)
5010 5588346 : && ! TREE_ADDRESSABLE (exp)
5011 5501272 : && ! TREE_THIS_VOLATILE (exp)
5012 5501272 : && ! DECL_NONLOCAL (exp)
5013 : /* Don't regard global variables as simple. They may be
5014 : allocated in ways unknown to the compiler (shared memory,
5015 : #pragma weak, etc). */
5016 5499619 : && ! TREE_PUBLIC (exp)
5017 5478902 : && ! DECL_EXTERNAL (exp)
5018 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5019 5478902 : && ! ((VAR_P (exp)
5020 : || TREE_CODE (exp) == PARM_DECL
5021 : || TREE_CODE (exp) == RESULT_DECL)
5022 5478902 : && DECL_HAS_VALUE_EXPR_P (exp))
5023 : /* Weakrefs are not safe to be read, since they can be NULL.
5024 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5025 : have DECL_WEAK flag set. */
5026 5478313 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5027 : /* Loading a static variable is unduly expensive, but global
5028 : registers aren't expensive. */
5029 5478313 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5030 : }
5031 :
5032 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5033 : In addition to simple_operand_p, we assume that comparisons, conversions,
5034 : and logic-not operations are simple, if their operands are simple, too. */
5035 :
5036 : bool
5037 7045473 : simple_condition_p (tree exp)
5038 : {
5039 7138465 : enum tree_code code;
5040 :
5041 7138465 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5042 4884712 : return false;
5043 :
5044 2288456 : while (CONVERT_EXPR_P (exp))
5045 34703 : exp = TREE_OPERAND (exp, 0);
5046 :
5047 2253753 : code = TREE_CODE (exp);
5048 :
5049 2253753 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5050 1764022 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5051 1764022 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5052 :
5053 489731 : if (code == TRUTH_NOT_EXPR)
5054 92992 : return simple_condition_p (TREE_OPERAND (exp, 0));
5055 :
5056 396739 : return simple_operand_p (exp);
5057 : }
5058 :
5059 :
5060 : /* The following functions are subroutines to fold_range_test and allow it to
5061 : try to change a logical combination of comparisons into a range test.
5062 :
5063 : For example, both
5064 : X == 2 || X == 3 || X == 4 || X == 5
5065 : and
5066 : X >= 2 && X <= 5
5067 : are converted to
5068 : (unsigned) (X - 2) <= 3
5069 :
5070 : We describe each set of comparisons as being either inside or outside
5071 : a range, using a variable named like IN_P, and then describe the
5072 : range with a lower and upper bound. If one of the bounds is omitted,
5073 : it represents either the highest or lowest value of the type.
5074 :
5075 : In the comments below, we represent a range by two numbers in brackets
5076 : preceded by a "+" to designate being inside that range, or a "-" to
5077 : designate being outside that range, so the condition can be inverted by
5078 : flipping the prefix. An omitted bound is represented by a "-". For
5079 : example, "- [-, 10]" means being outside the range starting at the lowest
5080 : possible value and ending at 10, in other words, being greater than 10.
5081 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5082 : always false.
5083 :
5084 : We set up things so that the missing bounds are handled in a consistent
5085 : manner so neither a missing bound nor "true" and "false" need to be
5086 : handled using a special case. */
5087 :
5088 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5089 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5090 : and UPPER1_P are nonzero if the respective argument is an upper bound
5091 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5092 : must be specified for a comparison. ARG1 will be converted to ARG0's
5093 : type if both are specified. */
5094 :
5095 : static tree
5096 22835981 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5097 : tree arg1, int upper1_p)
5098 : {
5099 22835981 : tree tem;
5100 22835981 : int result;
5101 22835981 : int sgn0, sgn1;
5102 :
5103 : /* If neither arg represents infinity, do the normal operation.
5104 : Else, if not a comparison, return infinity. Else handle the special
5105 : comparison rules. Note that most of the cases below won't occur, but
5106 : are handled for consistency. */
5107 :
5108 22835981 : if (arg0 != 0 && arg1 != 0)
5109 : {
5110 11981341 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5111 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5112 11981341 : STRIP_NOPS (tem);
5113 11981341 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5114 : }
5115 :
5116 10854640 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5117 : return 0;
5118 :
5119 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5120 : for neither. In real maths, we cannot assume open ended ranges are
5121 : the same. But, this is computer arithmetic, where numbers are finite.
5122 : We can therefore make the transformation of any unbounded range with
5123 : the value Z, Z being greater than any representable number. This permits
5124 : us to treat unbounded ranges as equal. */
5125 10845536 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5126 10845536 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5127 10845536 : switch (code)
5128 : {
5129 5109376 : case EQ_EXPR:
5130 5109376 : result = sgn0 == sgn1;
5131 5109376 : break;
5132 0 : case NE_EXPR:
5133 0 : result = sgn0 != sgn1;
5134 0 : break;
5135 362713 : case LT_EXPR:
5136 362713 : result = sgn0 < sgn1;
5137 362713 : break;
5138 2490770 : case LE_EXPR:
5139 2490770 : result = sgn0 <= sgn1;
5140 2490770 : break;
5141 2882677 : case GT_EXPR:
5142 2882677 : result = sgn0 > sgn1;
5143 2882677 : break;
5144 0 : case GE_EXPR:
5145 0 : result = sgn0 >= sgn1;
5146 0 : break;
5147 0 : default:
5148 0 : gcc_unreachable ();
5149 : }
5150 :
5151 10845536 : return constant_boolean_node (result, type);
5152 : }
5153 :
5154 : /* Helper routine for make_range. Perform one step for it, return
5155 : new expression if the loop should continue or NULL_TREE if it should
5156 : stop. */
5157 :
5158 : tree
5159 60327037 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5160 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5161 : {
5162 60327037 : tree arg0_type = TREE_TYPE (arg0);
5163 60327037 : tree n_low, n_high, low = *p_low, high = *p_high;
5164 60327037 : int in_p = *p_in_p, n_in_p;
5165 :
5166 60327037 : switch (code)
5167 : {
5168 1719147 : case TRUTH_NOT_EXPR:
5169 : /* We can only do something if the range is testing for zero. */
5170 1719147 : if (low == NULL_TREE || high == NULL_TREE
5171 1719147 : || ! integer_zerop (low) || ! integer_zerop (high))
5172 0 : return NULL_TREE;
5173 1719147 : *p_in_p = ! in_p;
5174 1719147 : return arg0;
5175 :
5176 46883855 : case EQ_EXPR: case NE_EXPR:
5177 46883855 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5178 : /* We can only do something if the range is testing for zero
5179 : and if the second operand is an integer constant. Note that
5180 : saying something is "in" the range we make is done by
5181 : complementing IN_P since it will set in the initial case of
5182 : being not equal to zero; "out" is leaving it alone. */
5183 46883855 : if (low == NULL_TREE || high == NULL_TREE
5184 46883855 : || ! integer_zerop (low) || ! integer_zerop (high)
5185 93767622 : || TREE_CODE (arg1) != INTEGER_CST)
5186 17331441 : return NULL_TREE;
5187 :
5188 29552414 : switch (code)
5189 : {
5190 : case NE_EXPR: /* - [c, c] */
5191 : low = high = arg1;
5192 : break;
5193 7985937 : case EQ_EXPR: /* + [c, c] */
5194 7985937 : in_p = ! in_p, low = high = arg1;
5195 7985937 : break;
5196 2198863 : case GT_EXPR: /* - [-, c] */
5197 2198863 : low = 0, high = arg1;
5198 2198863 : break;
5199 727025 : case GE_EXPR: /* + [c, -] */
5200 727025 : in_p = ! in_p, low = arg1, high = 0;
5201 727025 : break;
5202 5691650 : case LT_EXPR: /* - [c, -] */
5203 5691650 : low = arg1, high = 0;
5204 5691650 : break;
5205 4418424 : case LE_EXPR: /* + [-, c] */
5206 4418424 : in_p = ! in_p, low = 0, high = arg1;
5207 4418424 : break;
5208 0 : default:
5209 0 : gcc_unreachable ();
5210 : }
5211 :
5212 : /* If this is an unsigned comparison, we also know that EXP is
5213 : greater than or equal to zero. We base the range tests we make
5214 : on that fact, so we record it here so we can parse existing
5215 : range tests. We test arg0_type since often the return type
5216 : of, e.g. EQ_EXPR, is boolean. */
5217 29552414 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5218 : {
5219 1896491 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5220 : in_p, low, high, 1,
5221 : build_int_cst (arg0_type, 0),
5222 : NULL_TREE))
5223 : return NULL_TREE;
5224 :
5225 1896482 : in_p = n_in_p, low = n_low, high = n_high;
5226 :
5227 : /* If the high bound is missing, but we have a nonzero low
5228 : bound, reverse the range so it goes from zero to the low bound
5229 : minus 1. */
5230 1896482 : if (high == 0 && low && ! integer_zerop (low))
5231 : {
5232 841974 : in_p = ! in_p;
5233 841974 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5234 841974 : build_int_cst (TREE_TYPE (low), 1), 0);
5235 841974 : low = build_int_cst (arg0_type, 0);
5236 : }
5237 : }
5238 :
5239 29552405 : *p_low = low;
5240 29552405 : *p_high = high;
5241 29552405 : *p_in_p = in_p;
5242 29552405 : return arg0;
5243 :
5244 326 : case NEGATE_EXPR:
5245 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5246 : low and high are non-NULL, then normalize will DTRT. */
5247 326 : if (!TYPE_UNSIGNED (arg0_type)
5248 326 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5249 : {
5250 95 : if (low == NULL_TREE)
5251 12 : low = TYPE_MIN_VALUE (arg0_type);
5252 95 : if (high == NULL_TREE)
5253 47 : high = TYPE_MAX_VALUE (arg0_type);
5254 : }
5255 :
5256 : /* (-x) IN [a,b] -> x in [-b, -a] */
5257 326 : n_low = range_binop (MINUS_EXPR, exp_type,
5258 : build_int_cst (exp_type, 0),
5259 : 0, high, 1);
5260 326 : n_high = range_binop (MINUS_EXPR, exp_type,
5261 : build_int_cst (exp_type, 0),
5262 : 0, low, 0);
5263 326 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5264 : return NULL_TREE;
5265 314 : goto normalize;
5266 :
5267 12 : case BIT_NOT_EXPR:
5268 : /* ~ X -> -X - 1 */
5269 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5270 : build_int_cst (exp_type, 1));
5271 :
5272 851926 : case PLUS_EXPR:
5273 851926 : case MINUS_EXPR:
5274 851926 : if (TREE_CODE (arg1) != INTEGER_CST)
5275 : return NULL_TREE;
5276 :
5277 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5278 : move a constant to the other side. */
5279 644747 : if (!TYPE_UNSIGNED (arg0_type)
5280 644747 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5281 : return NULL_TREE;
5282 :
5283 : /* If EXP is signed, any overflow in the computation is undefined,
5284 : so we don't worry about it so long as our computations on
5285 : the bounds don't overflow. For unsigned, overflow is defined
5286 : and this is exactly the right thing. */
5287 910882 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5288 : arg0_type, low, 0, arg1, 0);
5289 456938 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5290 : arg0_type, high, 1, arg1, 0);
5291 453391 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5292 910317 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5293 : return NULL_TREE;
5294 :
5295 457240 : normalize:
5296 : /* Check for an unsigned range which has wrapped around the maximum
5297 : value thus making n_high < n_low, and normalize it. */
5298 457240 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5299 : {
5300 118957 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5301 118957 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5302 118957 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5303 118957 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5304 :
5305 : /* If the range is of the form +/- [ x+1, x ], we won't
5306 : be able to normalize it. But then, it represents the
5307 : whole range or the empty set, so make it
5308 : +/- [ -, - ]. */
5309 118957 : if (tree_int_cst_equal (n_low, low)
5310 118957 : && tree_int_cst_equal (n_high, high))
5311 : low = high = 0;
5312 : else
5313 118957 : in_p = ! in_p;
5314 : }
5315 : else
5316 338283 : low = n_low, high = n_high;
5317 :
5318 457240 : *p_low = low;
5319 457240 : *p_high = high;
5320 457240 : *p_in_p = in_p;
5321 457240 : return arg0;
5322 :
5323 2544561 : CASE_CONVERT:
5324 2544561 : case NON_LVALUE_EXPR:
5325 2544561 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5326 : return NULL_TREE;
5327 :
5328 1166124 : if (! INTEGRAL_TYPE_P (arg0_type)
5329 1130976 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5330 1029852 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5331 : return NULL_TREE;
5332 :
5333 1011799 : n_low = low, n_high = high;
5334 :
5335 1011799 : if (n_low != 0)
5336 847464 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5337 :
5338 1011799 : if (n_high != 0)
5339 946361 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5340 :
5341 : /* If we're converting arg0 from an unsigned type, to exp,
5342 : a signed type, we will be doing the comparison as unsigned.
5343 : The tests above have already verified that LOW and HIGH
5344 : are both positive.
5345 :
5346 : So we have to ensure that we will handle large unsigned
5347 : values the same way that the current signed bounds treat
5348 : negative values. */
5349 :
5350 1011799 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5351 : {
5352 254573 : tree high_positive;
5353 254573 : tree equiv_type;
5354 : /* For fixed-point modes, we need to pass the saturating flag
5355 : as the 2nd parameter. */
5356 254573 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5357 0 : equiv_type
5358 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5359 0 : TYPE_SATURATING (arg0_type));
5360 254573 : else if (BITINT_TYPE_P (arg0_type))
5361 : equiv_type = arg0_type;
5362 : else
5363 254558 : equiv_type
5364 254558 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5365 :
5366 : /* A range without an upper bound is, naturally, unbounded.
5367 : Since convert would have cropped a very large value, use
5368 : the max value for the destination type. */
5369 254573 : high_positive
5370 254573 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5371 0 : : TYPE_MAX_VALUE (arg0_type);
5372 :
5373 254573 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5374 233476 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5375 : fold_convert_loc (loc, arg0_type,
5376 : high_positive),
5377 : build_int_cst (arg0_type, 1));
5378 :
5379 : /* If the low bound is specified, "and" the range with the
5380 : range for which the original unsigned value will be
5381 : positive. */
5382 254573 : if (low != 0)
5383 : {
5384 95733 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5385 : 1, fold_convert_loc (loc, arg0_type,
5386 : integer_zero_node),
5387 : high_positive))
5388 : return NULL_TREE;
5389 :
5390 95733 : in_p = (n_in_p == in_p);
5391 : }
5392 : else
5393 : {
5394 : /* Otherwise, "or" the range with the range of the input
5395 : that will be interpreted as negative. */
5396 158840 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5397 : 1, fold_convert_loc (loc, arg0_type,
5398 : integer_zero_node),
5399 : high_positive))
5400 : return NULL_TREE;
5401 :
5402 158840 : in_p = (in_p != n_in_p);
5403 : }
5404 : }
5405 :
5406 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5407 : an unsigned type, we will do the comparison as signed. If
5408 : high is non-NULL, we punt above if it doesn't fit in the signed
5409 : type, so if we get through here, +[-, high] or +[low, high] are
5410 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5411 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5412 : high is not, the +[low, -] range is equivalent to union of
5413 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5414 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5415 : low being 0, which should be treated as [-, -]. */
5416 757226 : else if (TYPE_UNSIGNED (exp_type)
5417 738078 : && !TYPE_UNSIGNED (arg0_type)
5418 382047 : && low
5419 1139273 : && !high)
5420 : {
5421 12 : if (integer_zerop (low))
5422 12 : n_low = NULL_TREE;
5423 : else
5424 : {
5425 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5426 : n_low, build_int_cst (arg0_type, -1));
5427 0 : n_low = build_zero_cst (arg0_type);
5428 0 : in_p = !in_p;
5429 : }
5430 : }
5431 :
5432 1011799 : *p_low = n_low;
5433 1011799 : *p_high = n_high;
5434 1011799 : *p_in_p = in_p;
5435 1011799 : return arg0;
5436 :
5437 : default:
5438 : return NULL_TREE;
5439 : }
5440 : }
5441 :
5442 : /* Given EXP, a logical expression, set the range it is testing into
5443 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5444 : actually being tested. *PLOW and *PHIGH will be made of the same
5445 : type as the returned expression. If EXP is not a comparison, we
5446 : will most likely not be returning a useful value and range. */
5447 :
5448 : tree
5449 49901376 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5450 : {
5451 49901376 : enum tree_code code;
5452 49901376 : tree arg0, arg1 = NULL_TREE;
5453 49901376 : tree exp_type, nexp;
5454 49901376 : int in_p;
5455 49901376 : tree low, high;
5456 49901376 : location_t loc = EXPR_LOCATION (exp);
5457 :
5458 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5459 : and see if we can refine the range. Some of the cases below may not
5460 : happen, but it doesn't seem worth worrying about this. We "continue"
5461 : the outer loop when we've changed something; otherwise we "break"
5462 : the switch, which will "break" the while. */
5463 :
5464 49901376 : in_p = 0;
5465 49901376 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5466 :
5467 79586469 : while (1)
5468 : {
5469 79586469 : code = TREE_CODE (exp);
5470 79586469 : exp_type = TREE_TYPE (exp);
5471 79586469 : arg0 = NULL_TREE;
5472 :
5473 79586469 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5474 : {
5475 55546780 : if (TREE_OPERAND_LENGTH (exp) > 0)
5476 55546780 : arg0 = TREE_OPERAND (exp, 0);
5477 55546780 : if (TREE_CODE_CLASS (code) == tcc_binary
5478 52480900 : || TREE_CODE_CLASS (code) == tcc_comparison
5479 65206441 : || (TREE_CODE_CLASS (code) == tcc_expression
5480 2901326 : && TREE_OPERAND_LENGTH (exp) > 1))
5481 47052667 : arg1 = TREE_OPERAND (exp, 1);
5482 : }
5483 55546780 : if (arg0 == NULL_TREE)
5484 : break;
5485 :
5486 55546766 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5487 : &high, &in_p);
5488 55546766 : if (nexp == NULL_TREE)
5489 : break;
5490 : exp = nexp;
5491 : }
5492 :
5493 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5494 49901376 : if (TREE_CODE (exp) == INTEGER_CST)
5495 : {
5496 31414 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5497 : exp, 0, low, 0))
5498 31414 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5499 : exp, 1, high, 1)));
5500 31414 : low = high = 0;
5501 31414 : exp = 0;
5502 : }
5503 :
5504 49901376 : *pin_p = in_p, *plow = low, *phigh = high;
5505 49901376 : return exp;
5506 : }
5507 :
5508 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5509 : a bitwise check i.e. when
5510 : LOW == 0xXX...X00...0
5511 : HIGH == 0xXX...X11...1
5512 : Return corresponding mask in MASK and stem in VALUE. */
5513 :
5514 : static bool
5515 125 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5516 : tree *value)
5517 : {
5518 125 : if (TREE_CODE (low) != INTEGER_CST
5519 125 : || TREE_CODE (high) != INTEGER_CST)
5520 : return false;
5521 :
5522 125 : unsigned prec = TYPE_PRECISION (type);
5523 125 : wide_int lo = wi::to_wide (low, prec);
5524 125 : wide_int hi = wi::to_wide (high, prec);
5525 :
5526 125 : wide_int end_mask = lo ^ hi;
5527 250 : if ((end_mask & (end_mask + 1)) != 0
5528 235 : || (lo & end_mask) != 0)
5529 : return false;
5530 :
5531 86 : wide_int stem_mask = ~end_mask;
5532 86 : wide_int stem = lo & stem_mask;
5533 86 : if (stem != (hi & stem_mask))
5534 : return false;
5535 :
5536 86 : *mask = wide_int_to_tree (type, stem_mask);
5537 86 : *value = wide_int_to_tree (type, stem);
5538 :
5539 86 : return true;
5540 211 : }
5541 :
5542 : /* Helper routine for build_range_check and match.pd. Return the type to
5543 : perform the check or NULL if it shouldn't be optimized. */
5544 :
5545 : tree
5546 585421 : range_check_type (tree etype)
5547 : {
5548 : /* First make sure that arithmetics in this type is valid, then make sure
5549 : that it wraps around. */
5550 585421 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5551 0 : etype = TREE_TYPE (etype);
5552 585421 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5553 60518 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5554 :
5555 585421 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5556 : {
5557 402987 : tree utype, minv, maxv;
5558 :
5559 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5560 : for the type in question, as we rely on this here. */
5561 402987 : utype = unsigned_type_for (etype);
5562 402987 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5563 402987 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5564 402987 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5565 402987 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5566 :
5567 402987 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5568 : minv, 1, maxv, 1)))
5569 : etype = utype;
5570 : else
5571 102 : return NULL_TREE;
5572 : }
5573 182434 : else if (POINTER_TYPE_P (etype)
5574 : || TREE_CODE (etype) == OFFSET_TYPE
5575 : /* Right now all BITINT_TYPEs satisfy
5576 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5577 : that like for INTEGER_TYPEs. */
5578 : || TREE_CODE (etype) == BITINT_TYPE)
5579 1362 : etype = unsigned_type_for (etype);
5580 : return etype;
5581 : }
5582 :
5583 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5584 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5585 : on IN_P) the range. Return 0 if the test couldn't be created. */
5586 :
5587 : tree
5588 1602474 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5589 : tree low, tree high)
5590 : {
5591 2795852 : tree etype = TREE_TYPE (exp), mask, value;
5592 :
5593 : /* Disable this optimization for function pointer expressions
5594 : on targets that require function pointer canonicalization. */
5595 2795852 : if (targetm.have_canonicalize_funcptr_for_compare ()
5596 0 : && POINTER_TYPE_P (etype)
5597 2795852 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5598 : return NULL_TREE;
5599 :
5600 2795852 : if (! in_p)
5601 : {
5602 310135 : value = build_range_check (loc, type, exp, 1, low, high);
5603 310135 : if (value != 0)
5604 310135 : return invert_truthvalue_loc (loc, value);
5605 :
5606 : return 0;
5607 : }
5608 :
5609 2485717 : if (low == 0 && high == 0)
5610 121525 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5611 :
5612 2364192 : if (low == 0)
5613 778385 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5614 778385 : fold_convert_loc (loc, etype, high));
5615 :
5616 1585807 : if (high == 0)
5617 72746 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5618 72746 : fold_convert_loc (loc, etype, low));
5619 :
5620 1513061 : if (operand_equal_p (low, high, 0))
5621 319441 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5622 319441 : fold_convert_loc (loc, etype, low));
5623 :
5624 1193620 : if (TREE_CODE (exp) == BIT_AND_EXPR
5625 1193620 : && maskable_range_p (low, high, etype, &mask, &value))
5626 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5627 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5628 : exp, mask),
5629 86 : value);
5630 :
5631 1193534 : if (integer_zerop (low))
5632 : {
5633 697749 : if (! TYPE_UNSIGNED (etype))
5634 : {
5635 184143 : etype = unsigned_type_for (etype);
5636 184143 : high = fold_convert_loc (loc, etype, high);
5637 184143 : exp = fold_convert_loc (loc, etype, exp);
5638 : }
5639 697749 : return build_range_check (loc, type, exp, 1, 0, high);
5640 : }
5641 :
5642 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5643 495785 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5644 : {
5645 128263 : int prec = TYPE_PRECISION (etype);
5646 :
5647 128263 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5648 : {
5649 126 : if (TYPE_UNSIGNED (etype))
5650 : {
5651 120 : tree signed_etype = signed_type_for (etype);
5652 120 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5653 0 : etype
5654 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5655 : else
5656 : etype = signed_etype;
5657 120 : exp = fold_convert_loc (loc, etype, exp);
5658 : }
5659 126 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5660 : build_int_cst (etype, 0));
5661 : }
5662 : }
5663 :
5664 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5665 : This requires wrap-around arithmetics for the type of the expression. */
5666 495659 : etype = range_check_type (etype);
5667 495659 : if (etype == NULL_TREE)
5668 : return NULL_TREE;
5669 :
5670 495629 : high = fold_convert_loc (loc, etype, high);
5671 495629 : low = fold_convert_loc (loc, etype, low);
5672 495629 : exp = fold_convert_loc (loc, etype, exp);
5673 :
5674 495629 : value = const_binop (MINUS_EXPR, high, low);
5675 :
5676 495629 : if (value != 0 && !TREE_OVERFLOW (value))
5677 495629 : return build_range_check (loc, type,
5678 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5679 : 1, build_int_cst (etype, 0), value);
5680 :
5681 : return 0;
5682 : }
5683 :
5684 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5685 :
5686 : static tree
5687 175449 : range_predecessor (tree val)
5688 : {
5689 175449 : tree type = TREE_TYPE (val);
5690 :
5691 175449 : if (INTEGRAL_TYPE_P (type)
5692 175449 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5693 : return 0;
5694 : else
5695 175449 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5696 175449 : build_int_cst (TREE_TYPE (val), 1), 0);
5697 : }
5698 :
5699 : /* Return the successor of VAL in its type, handling the infinite case. */
5700 :
5701 : static tree
5702 1638904 : range_successor (tree val)
5703 : {
5704 1638904 : tree type = TREE_TYPE (val);
5705 :
5706 1638904 : if (INTEGRAL_TYPE_P (type)
5707 1638904 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5708 : return 0;
5709 : else
5710 1638895 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5711 1638895 : build_int_cst (TREE_TYPE (val), 1), 0);
5712 : }
5713 :
5714 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5715 : can, 0 if we can't. Set the output range into the specified parameters. */
5716 :
5717 : bool
5718 3594461 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5719 : tree high0, int in1_p, tree low1, tree high1)
5720 : {
5721 3594461 : bool no_overlap;
5722 3594461 : int subset;
5723 3594461 : int temp;
5724 3594461 : tree tem;
5725 3594461 : int in_p;
5726 3594461 : tree low, high;
5727 3594461 : int lowequal = ((low0 == 0 && low1 == 0)
5728 3594461 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5729 3594461 : low0, 0, low1, 0)));
5730 3594461 : int highequal = ((high0 == 0 && high1 == 0)
5731 3594461 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5732 3594461 : high0, 1, high1, 1)));
5733 :
5734 : /* Make range 0 be the range that starts first, or ends last if they
5735 : start at the same value. Swap them if it isn't. */
5736 3594461 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5737 : low0, 0, low1, 0))
5738 3594461 : || (lowequal
5739 573005 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5740 : high1, 1, high0, 1))))
5741 : {
5742 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5743 : tem = low0, low0 = low1, low1 = tem;
5744 : tem = high0, high0 = high1, high1 = tem;
5745 : }
5746 :
5747 : /* If the second range is != high1 where high1 is the type maximum of
5748 : the type, try first merging with < high1 range. */
5749 3594461 : if (low1
5750 3594461 : && high1
5751 997813 : && TREE_CODE (low1) == INTEGER_CST
5752 997813 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5753 127017 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5754 170148 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5755 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5756 4550331 : && operand_equal_p (low1, high1, 0))
5757 : {
5758 559396 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5759 559396 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5760 : !in1_p, NULL_TREE, range_predecessor (low1)))
5761 : return true;
5762 : /* Similarly for the second range != low1 where low1 is the type minimum
5763 : of the type, try first merging with > low1 range. */
5764 448578 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5765 448578 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5766 : !in1_p, range_successor (low1), NULL_TREE))
5767 : return true;
5768 : }
5769 :
5770 : /* Now flag two cases, whether the ranges are disjoint or whether the
5771 : second range is totally subsumed in the first. Note that the tests
5772 : below are simplified by the ones above. */
5773 3388672 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5774 : high0, 1, low1, 0));
5775 3388672 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5776 : high1, 1, high0, 1));
5777 :
5778 : /* We now have four cases, depending on whether we are including or
5779 : excluding the two ranges. */
5780 3388672 : if (in0_p && in1_p)
5781 : {
5782 : /* If they don't overlap, the result is false. If the second range
5783 : is a subset it is the result. Otherwise, the range is from the start
5784 : of the second to the end of the first. */
5785 1550558 : if (no_overlap)
5786 : in_p = 0, low = high = 0;
5787 1548477 : else if (subset)
5788 : in_p = 1, low = low1, high = high1;
5789 : else
5790 1429152 : in_p = 1, low = low1, high = high0;
5791 : }
5792 :
5793 1838114 : else if (in0_p && ! in1_p)
5794 : {
5795 : /* If they don't overlap, the result is the first range. If they are
5796 : equal, the result is false. If the second range is a subset of the
5797 : first, and the ranges begin at the same place, we go from just after
5798 : the end of the second range to the end of the first. If the second
5799 : range is not a subset of the first, or if it is a subset and both
5800 : ranges end at the same place, the range starts at the start of the
5801 : first range and ends just before the second range.
5802 : Otherwise, we can't describe this as a single range. */
5803 323461 : if (no_overlap)
5804 : in_p = 1, low = low0, high = high0;
5805 317894 : else if (lowequal && highequal)
5806 : in_p = 0, low = high = 0;
5807 317098 : else if (subset && lowequal)
5808 : {
5809 241494 : low = range_successor (high1);
5810 241494 : high = high0;
5811 241494 : in_p = 1;
5812 241494 : if (low == 0)
5813 : {
5814 : /* We are in the weird situation where high0 > high1 but
5815 : high1 has no successor. Punt. */
5816 : return 0;
5817 : }
5818 : }
5819 75604 : else if (! subset || highequal)
5820 : {
5821 55139 : low = low0;
5822 55139 : high = range_predecessor (low1);
5823 55139 : in_p = 1;
5824 55139 : if (high == 0)
5825 : {
5826 : /* low0 < low1 but low1 has no predecessor. Punt. */
5827 : return 0;
5828 : }
5829 : }
5830 : else
5831 : return 0;
5832 : }
5833 :
5834 1514653 : else if (! in0_p && in1_p)
5835 : {
5836 : /* If they don't overlap, the result is the second range. If the second
5837 : is a subset of the first, the result is false. Otherwise,
5838 : the range starts just after the first range and ends at the
5839 : end of the second. */
5840 1156512 : if (no_overlap)
5841 : in_p = 1, low = low1, high = high1;
5842 1148753 : else if (subset || highequal)
5843 : in_p = 0, low = high = 0;
5844 : else
5845 : {
5846 1041483 : low = range_successor (high0);
5847 1041483 : high = high1;
5848 1041483 : in_p = 1;
5849 1041483 : if (low == 0)
5850 : {
5851 : /* high1 > high0 but high0 has no successor. Punt. */
5852 : return 0;
5853 : }
5854 : }
5855 : }
5856 :
5857 : else
5858 : {
5859 : /* The case where we are excluding both ranges. Here the complex case
5860 : is if they don't overlap. In that case, the only time we have a
5861 : range is if they are adjacent. If the second is a subset of the
5862 : first, the result is the first. Otherwise, the range to exclude
5863 : starts at the beginning of the first range and ends at the end of the
5864 : second. */
5865 358141 : if (no_overlap)
5866 : {
5867 260636 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5868 : range_successor (high0),
5869 : 1, low1, 0)))
5870 : in_p = 0, low = low0, high = high1;
5871 : else
5872 : {
5873 : /* Canonicalize - [min, x] into - [-, x]. */
5874 210063 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5875 208942 : switch (TREE_CODE (TREE_TYPE (low0)))
5876 : {
5877 51005 : case ENUMERAL_TYPE:
5878 51005 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5879 : GET_MODE_BITSIZE
5880 102010 : (TYPE_MODE (TREE_TYPE (low0)))))
5881 : break;
5882 : /* FALLTHROUGH */
5883 208741 : case INTEGER_TYPE:
5884 208741 : if (tree_int_cst_equal (low0,
5885 208741 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5886 6496 : low0 = 0;
5887 : break;
5888 201 : case POINTER_TYPE:
5889 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5890 201 : && integer_zerop (low0))
5891 : low0 = 0;
5892 : break;
5893 : default:
5894 : break;
5895 : }
5896 :
5897 : /* Canonicalize - [x, max] into - [x, -]. */
5898 210063 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5899 209876 : switch (TREE_CODE (TREE_TYPE (high1)))
5900 : {
5901 51013 : case ENUMERAL_TYPE:
5902 51013 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5903 : GET_MODE_BITSIZE
5904 102026 : (TYPE_MODE (TREE_TYPE (high1)))))
5905 : break;
5906 : /* FALLTHROUGH */
5907 209675 : case INTEGER_TYPE:
5908 209675 : if (tree_int_cst_equal (high1,
5909 209675 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5910 9493 : high1 = 0;
5911 : break;
5912 201 : case POINTER_TYPE:
5913 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5914 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5915 : high1, 1,
5916 201 : build_int_cst (TREE_TYPE (high1), 1),
5917 : 1)))
5918 133 : high1 = 0;
5919 : break;
5920 : default:
5921 : break;
5922 : }
5923 :
5924 : /* The ranges might be also adjacent between the maximum and
5925 : minimum values of the given type. For
5926 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5927 : return + [x + 1, y - 1]. */
5928 210063 : if (low0 == 0 && high1 == 0)
5929 : {
5930 320 : low = range_successor (high0);
5931 320 : high = range_predecessor (low1);
5932 320 : if (low == 0 || high == 0)
5933 : return 0;
5934 :
5935 : in_p = 1;
5936 : }
5937 : else
5938 : return 0;
5939 : }
5940 : }
5941 97505 : else if (subset)
5942 : in_p = 0, low = low0, high = high0;
5943 : else
5944 11640 : in_p = 0, low = low0, high = high1;
5945 : }
5946 :
5947 3158455 : *pin_p = in_p, *plow = low, *phigh = high;
5948 3158455 : return 1;
5949 : }
5950 :
5951 :
5952 : /* Subroutine of fold, looking inside expressions of the form
5953 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5954 : are the three operands of the COND_EXPR. This function is
5955 : being used also to optimize A op B ? C : A, by reversing the
5956 : comparison first.
5957 :
5958 : Return a folded expression whose code is not a COND_EXPR
5959 : anymore, or NULL_TREE if no folding opportunity is found. */
5960 :
5961 : static tree
5962 509281 : fold_cond_expr_with_comparison (location_t loc, tree type,
5963 : enum tree_code comp_code,
5964 : tree arg00, tree arg01, tree arg1, tree arg2)
5965 : {
5966 509281 : tree arg1_type = TREE_TYPE (arg1);
5967 509281 : tree tem;
5968 :
5969 509281 : STRIP_NOPS (arg1);
5970 509281 : STRIP_NOPS (arg2);
5971 :
5972 : /* If we have A op 0 ? A : -A, consider applying the following
5973 : transformations:
5974 :
5975 : A == 0? A : -A same as -A
5976 : A != 0? A : -A same as A
5977 : A >= 0? A : -A same as abs (A)
5978 : A > 0? A : -A same as abs (A)
5979 : A <= 0? A : -A same as -abs (A)
5980 : A < 0? A : -A same as -abs (A)
5981 :
5982 : None of these transformations work for modes with signed
5983 : zeros. If A is +/-0, the first two transformations will
5984 : change the sign of the result (from +0 to -0, or vice
5985 : versa). The last four will fix the sign of the result,
5986 : even though the original expressions could be positive or
5987 : negative, depending on the sign of A.
5988 :
5989 : Note that all these transformations are correct if A is
5990 : NaN, since the two alternatives (A and -A) are also NaNs. */
5991 509281 : if (!HONOR_SIGNED_ZEROS (type)
5992 1018572 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5993 509281 : ? real_zerop (arg01)
5994 508193 : : integer_zerop (arg01))
5995 1366544 : && ((TREE_CODE (arg2) == NEGATE_EXPR
5996 1631 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5997 : /* In the case that A is of the form X-Y, '-A' (arg2) may
5998 : have already been folded to Y-X, check for that. */
5999 346575 : || (TREE_CODE (arg1) == MINUS_EXPR
6000 1718 : && TREE_CODE (arg2) == MINUS_EXPR
6001 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6002 0 : TREE_OPERAND (arg2, 1), 0)
6003 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6004 0 : TREE_OPERAND (arg2, 0), 0))))
6005 1407 : switch (comp_code)
6006 : {
6007 0 : case EQ_EXPR:
6008 0 : case UNEQ_EXPR:
6009 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6010 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6011 0 : case NE_EXPR:
6012 0 : case LTGT_EXPR:
6013 0 : return fold_convert_loc (loc, type, arg1);
6014 0 : case UNGE_EXPR:
6015 0 : case UNGT_EXPR:
6016 0 : if (flag_trapping_math)
6017 : break;
6018 : /* Fall through. */
6019 1391 : case GE_EXPR:
6020 1391 : case GT_EXPR:
6021 1391 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6022 : break;
6023 1375 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6024 1375 : return fold_convert_loc (loc, type, tem);
6025 0 : case UNLE_EXPR:
6026 0 : case UNLT_EXPR:
6027 0 : if (flag_trapping_math)
6028 : break;
6029 : /* FALLTHRU */
6030 16 : case LE_EXPR:
6031 16 : case LT_EXPR:
6032 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6033 : break;
6034 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6035 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6036 : {
6037 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6038 : is not, invokes UB both in abs and in the negation of it.
6039 : So, use ABSU_EXPR instead. */
6040 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6041 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6042 16 : tem = negate_expr (tem);
6043 16 : return fold_convert_loc (loc, type, tem);
6044 : }
6045 : else
6046 : {
6047 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6048 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6049 : }
6050 0 : default:
6051 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6052 : break;
6053 : }
6054 :
6055 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6056 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6057 : both transformations are correct when A is NaN: A != 0
6058 : is then true, and A == 0 is false. */
6059 :
6060 507890 : if (!HONOR_SIGNED_ZEROS (type)
6061 507890 : && integer_zerop (arg01) && integer_zerop (arg2))
6062 : {
6063 262831 : if (comp_code == NE_EXPR)
6064 147 : return fold_convert_loc (loc, type, arg1);
6065 262684 : else if (comp_code == EQ_EXPR)
6066 0 : return build_zero_cst (type);
6067 : }
6068 :
6069 : /* Try some transformations of A op B ? A : B.
6070 :
6071 : A == B? A : B same as B
6072 : A != B? A : B same as A
6073 : A >= B? A : B same as max (A, B)
6074 : A > B? A : B same as max (B, A)
6075 : A <= B? A : B same as min (A, B)
6076 : A < B? A : B same as min (B, A)
6077 :
6078 : As above, these transformations don't work in the presence
6079 : of signed zeros. For example, if A and B are zeros of
6080 : opposite sign, the first two transformations will change
6081 : the sign of the result. In the last four, the original
6082 : expressions give different results for (A=+0, B=-0) and
6083 : (A=-0, B=+0), but the transformed expressions do not.
6084 :
6085 : The first two transformations are correct if either A or B
6086 : is a NaN. In the first transformation, the condition will
6087 : be false, and B will indeed be chosen. In the case of the
6088 : second transformation, the condition A != B will be true,
6089 : and A will be chosen.
6090 :
6091 : The conversions to max() and min() are not correct if B is
6092 : a number and A is not. The conditions in the original
6093 : expressions will be false, so all four give B. The min()
6094 : and max() versions would give a NaN instead. */
6095 507743 : if (!HONOR_SIGNED_ZEROS (type)
6096 507743 : && operand_equal_for_comparison_p (arg01, arg2)
6097 : /* Avoid these transformations if the COND_EXPR may be used
6098 : as an lvalue in the C++ front-end. PR c++/19199. */
6099 779250 : && (in_gimple_form
6100 17644 : || VECTOR_TYPE_P (type)
6101 17582 : || (! lang_GNU_CXX ()
6102 15111 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6103 2471 : || ! maybe_lvalue_p (arg1)
6104 2450 : || ! maybe_lvalue_p (arg2)))
6105 : {
6106 269778 : tree comp_op0 = arg00;
6107 269778 : tree comp_op1 = arg01;
6108 269778 : tree comp_type = TREE_TYPE (comp_op0);
6109 :
6110 269778 : switch (comp_code)
6111 : {
6112 0 : case EQ_EXPR:
6113 0 : return fold_convert_loc (loc, type, arg2);
6114 1 : case NE_EXPR:
6115 1 : return fold_convert_loc (loc, type, arg1);
6116 6025 : case LE_EXPR:
6117 6025 : case LT_EXPR:
6118 6025 : case UNLE_EXPR:
6119 6025 : case UNLT_EXPR:
6120 : /* In C++ a ?: expression can be an lvalue, so put the
6121 : operand which will be used if they are equal first
6122 : so that we can convert this back to the
6123 : corresponding COND_EXPR. */
6124 6025 : if (!HONOR_NANS (arg1))
6125 : {
6126 6025 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6127 6025 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6128 6025 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6129 6025 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6130 4666 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6131 : comp_op1, comp_op0);
6132 6025 : return fold_convert_loc (loc, type, tem);
6133 : }
6134 : break;
6135 263752 : case GE_EXPR:
6136 263752 : case GT_EXPR:
6137 263752 : case UNGE_EXPR:
6138 263752 : case UNGT_EXPR:
6139 263752 : if (!HONOR_NANS (arg1))
6140 : {
6141 263750 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6142 263750 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6143 263750 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6144 263750 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6145 3668 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6146 : comp_op1, comp_op0);
6147 263750 : return fold_convert_loc (loc, type, tem);
6148 : }
6149 : break;
6150 0 : case UNEQ_EXPR:
6151 0 : if (!HONOR_NANS (arg1))
6152 0 : return fold_convert_loc (loc, type, arg2);
6153 : break;
6154 0 : case LTGT_EXPR:
6155 0 : if (!HONOR_NANS (arg1))
6156 0 : return fold_convert_loc (loc, type, arg1);
6157 : break;
6158 0 : default:
6159 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6160 : break;
6161 : }
6162 : }
6163 :
6164 : return NULL_TREE;
6165 : }
6166 :
6167 :
6168 :
6169 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6170 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6171 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6172 : false) >= 2)
6173 : #endif
6174 :
6175 : /* EXP is some logical combination of boolean tests. See if we can
6176 : merge it into some range test. Return the new tree if so. */
6177 :
6178 : static tree
6179 24950182 : fold_range_test (location_t loc, enum tree_code code, tree type,
6180 : tree op0, tree op1)
6181 : {
6182 24950182 : int or_op = (code == TRUTH_ORIF_EXPR
6183 24950182 : || code == TRUTH_OR_EXPR);
6184 24950182 : int in0_p, in1_p, in_p;
6185 24950182 : tree low0, low1, low, high0, high1, high;
6186 24950182 : tree tem, lhs, rhs;
6187 :
6188 24950182 : if (!INTEGRAL_TYPE_P (type))
6189 : return 0;
6190 :
6191 24950182 : lhs = make_range (op0, &in0_p, &low0, &high0);
6192 : /* If op0 is known true or false and this is a short-circuiting
6193 : operation we must not merge with op1 since that makes side-effects
6194 : unconditional. So special-case this. */
6195 24950182 : if (!lhs
6196 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6197 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6198 : return op0;
6199 24950180 : rhs = make_range (op1, &in1_p, &low1, &high1);
6200 :
6201 : /* If this is an OR operation, invert both sides; we will invert
6202 : again at the end. */
6203 24950180 : if (or_op)
6204 11738977 : in0_p = ! in0_p, in1_p = ! in1_p;
6205 :
6206 : /* If both expressions are the same, if we can merge the ranges, and we
6207 : can build the range test, return it or it inverted. If one of the
6208 : ranges is always true or always false, consider it to be the same
6209 : expression as the other. */
6210 24918770 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6211 1193480 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6212 : in1_p, low1, high1)
6213 25943774 : && (tem = (build_range_check (loc, type,
6214 : lhs != 0 ? lhs
6215 0 : : rhs != 0 ? rhs : integer_zero_node,
6216 : in_p, low, high))) != 0)
6217 : {
6218 993564 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6219 : }
6220 :
6221 : /* On machines where the branch cost is expensive, if this is a
6222 : short-circuited branch and the underlying object on both sides
6223 : is the same, make a non-short-circuit operation. */
6224 23956616 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6225 23956616 : if (param_logical_op_non_short_circuit != -1)
6226 7873 : logical_op_non_short_circuit
6227 7873 : = param_logical_op_non_short_circuit;
6228 23956616 : if (logical_op_non_short_circuit
6229 23952645 : && !sanitize_coverage_p ()
6230 23952642 : && lhs != 0 && rhs != 0
6231 23952203 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6232 29449113 : && operand_equal_p (lhs, rhs, 0))
6233 : {
6234 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6235 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6236 : which cases we can't do this. */
6237 170214 : if (simple_operand_p (lhs))
6238 68972 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6239 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6240 35002 : type, op0, op1);
6241 :
6242 135212 : else if (!lang_hooks.decls.global_bindings_p ()
6243 135212 : && !CONTAINS_PLACEHOLDER_P (lhs))
6244 : {
6245 134559 : tree common = save_expr (lhs);
6246 :
6247 247591 : if ((lhs = build_range_check (loc, type, common,
6248 113032 : or_op ? ! in0_p : in0_p,
6249 : low0, high0)) != 0
6250 247591 : && (rhs = build_range_check (loc, type, common,
6251 113032 : or_op ? ! in1_p : in1_p,
6252 : low1, high1)) != 0)
6253 : {
6254 247591 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6255 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6256 134559 : type, lhs, rhs);
6257 : }
6258 : }
6259 : }
6260 :
6261 : return 0;
6262 : }
6263 :
6264 : /* For an expression that has the form
6265 : (A && B) || ~B
6266 : or
6267 : (A || B) && ~B,
6268 : we can drop one of the inner expressions and simplify to
6269 : A || ~B
6270 : or
6271 : A && ~B
6272 : LOC is the location of the resulting expression. OP is the inner
6273 : logical operation; the left-hand side in the examples above, while CMPOP
6274 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6275 : removing a condition that guards another, as in
6276 : (A != NULL && A->...) || A == NULL
6277 : which we must not transform. If RHS_ONLY is true, only eliminate the
6278 : right-most operand of the inner logical operation. */
6279 :
6280 : static tree
6281 129486 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6282 : bool rhs_only)
6283 : {
6284 129486 : enum tree_code code = TREE_CODE (cmpop);
6285 129486 : enum tree_code truthop_code = TREE_CODE (op);
6286 129486 : tree lhs = TREE_OPERAND (op, 0);
6287 129486 : tree rhs = TREE_OPERAND (op, 1);
6288 129486 : tree orig_lhs = lhs, orig_rhs = rhs;
6289 129486 : enum tree_code rhs_code = TREE_CODE (rhs);
6290 129486 : enum tree_code lhs_code = TREE_CODE (lhs);
6291 129486 : enum tree_code inv_code;
6292 :
6293 129486 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6294 : return NULL_TREE;
6295 :
6296 116014 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6297 : return NULL_TREE;
6298 :
6299 38873 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6300 :
6301 38873 : if (rhs_code == truthop_code)
6302 : {
6303 33 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6304 33 : if (newrhs != NULL_TREE)
6305 : {
6306 0 : rhs = newrhs;
6307 0 : rhs_code = TREE_CODE (rhs);
6308 : }
6309 : }
6310 38873 : if (lhs_code == truthop_code && !rhs_only)
6311 : {
6312 476 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6313 476 : if (newlhs != NULL_TREE)
6314 : {
6315 0 : lhs = newlhs;
6316 0 : lhs_code = TREE_CODE (lhs);
6317 : }
6318 : }
6319 :
6320 38873 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6321 38873 : if (inv_code == rhs_code
6322 922 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6323 38909 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6324 : return lhs;
6325 38860 : if (!rhs_only && inv_code == lhs_code
6326 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6327 38952 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6328 : return rhs;
6329 38769 : if (rhs != orig_rhs || lhs != orig_lhs)
6330 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6331 0 : lhs, rhs);
6332 : return NULL_TREE;
6333 : }
6334 :
6335 : /* Find ways of folding logical expressions of LHS and RHS:
6336 : Try to merge two comparisons to the same innermost item.
6337 : Look for range tests like "ch >= '0' && ch <= '9'".
6338 : Look for combinations of simple terms on machines with expensive branches
6339 : and evaluate the RHS unconditionally.
6340 :
6341 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6342 : function and the one above.
6343 :
6344 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6345 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6346 :
6347 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6348 : two operands.
6349 :
6350 : We return the simplified tree or 0 if no optimization is possible. */
6351 :
6352 : static tree
6353 24627541 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6354 : tree lhs, tree rhs)
6355 : {
6356 : /* If this is the "or" of two comparisons, we can do something if
6357 : the comparisons are NE_EXPR. If this is the "and", we can do something
6358 : if the comparisons are EQ_EXPR. I.e.,
6359 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6360 :
6361 : WANTED_CODE is this operation code. For single bit fields, we can
6362 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6363 : comparison for one-bit fields. */
6364 :
6365 24627541 : enum tree_code lcode, rcode;
6366 24627541 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6367 24627541 : tree result;
6368 :
6369 : /* Start by getting the comparison codes. Fail if anything is volatile.
6370 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6371 : it were surrounded with a NE_EXPR. */
6372 :
6373 24627541 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6374 : return 0;
6375 :
6376 21753922 : lcode = TREE_CODE (lhs);
6377 21753922 : rcode = TREE_CODE (rhs);
6378 :
6379 21753922 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6380 : {
6381 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6382 0 : build_int_cst (TREE_TYPE (lhs), 0));
6383 0 : lcode = NE_EXPR;
6384 : }
6385 :
6386 21753922 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6387 : {
6388 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6389 0 : build_int_cst (TREE_TYPE (rhs), 0));
6390 0 : rcode = NE_EXPR;
6391 : }
6392 :
6393 21753922 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6394 19407400 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6395 : return 0;
6396 :
6397 18292724 : ll_arg = TREE_OPERAND (lhs, 0);
6398 18292724 : lr_arg = TREE_OPERAND (lhs, 1);
6399 18292724 : rl_arg = TREE_OPERAND (rhs, 0);
6400 18292724 : rr_arg = TREE_OPERAND (rhs, 1);
6401 :
6402 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6403 18292724 : if (simple_operand_p (ll_arg)
6404 18292724 : && simple_operand_p (lr_arg))
6405 : {
6406 14843161 : if (operand_equal_p (ll_arg, rl_arg, 0)
6407 14843161 : && operand_equal_p (lr_arg, rr_arg, 0))
6408 : {
6409 21320 : result = combine_comparisons (loc, code, lcode, rcode,
6410 : truth_type, ll_arg, lr_arg);
6411 21320 : if (result)
6412 : return result;
6413 : }
6414 14821841 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6415 14821841 : && operand_equal_p (lr_arg, rl_arg, 0))
6416 : {
6417 252 : result = combine_comparisons (loc, code, lcode,
6418 : swap_tree_comparison (rcode),
6419 : truth_type, ll_arg, lr_arg);
6420 252 : if (result)
6421 : return result;
6422 : }
6423 : }
6424 :
6425 8639972 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6426 18271590 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6427 :
6428 : /* If the RHS can be evaluated unconditionally and its operands are
6429 : simple, it wins to evaluate the RHS unconditionally on machines
6430 : with expensive branches. In this case, this isn't a comparison
6431 : that can be merged. */
6432 :
6433 18271590 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6434 : false) >= 2
6435 18271487 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6436 17232585 : && simple_operand_p (rl_arg)
6437 28207646 : && simple_operand_p (rr_arg))
6438 : {
6439 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6440 11116839 : if (code == TRUTH_OR_EXPR
6441 1510746 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6442 618145 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6443 20954 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6444 11134437 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6445 34580 : return build2_loc (loc, NE_EXPR, truth_type,
6446 17290 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6447 : ll_arg, rl_arg),
6448 17290 : build_int_cst (TREE_TYPE (ll_arg), 0));
6449 :
6450 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6451 11099549 : if (code == TRUTH_AND_EXPR
6452 1704140 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6453 816776 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6454 7812 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6455 11101140 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6456 2734 : return build2_loc (loc, EQ_EXPR, truth_type,
6457 1367 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6458 : ll_arg, rl_arg),
6459 1367 : build_int_cst (TREE_TYPE (ll_arg), 0));
6460 : }
6461 :
6462 : return 0;
6463 : }
6464 :
6465 : /* T is an integer expression that is being multiplied, divided, or taken a
6466 : modulus (CODE says which and what kind of divide or modulus) by a
6467 : constant C. See if we can eliminate that operation by folding it with
6468 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6469 : should be used for the computation if wider than our type.
6470 :
6471 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6472 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6473 : expression would not overflow or that overflow is undefined for the type
6474 : in the language in question.
6475 :
6476 : If we return a non-null expression, it is an equivalent form of the
6477 : original computation, but need not be in the original type. */
6478 :
6479 : static tree
6480 100836587 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6481 : {
6482 : /* To avoid exponential search depth, refuse to allow recursion past
6483 : three levels. Beyond that (1) it's highly unlikely that we'll find
6484 : something interesting and (2) we've probably processed it before
6485 : when we built the inner expression. */
6486 :
6487 100836587 : static int depth;
6488 100836587 : tree ret;
6489 :
6490 100836587 : if (depth > 3)
6491 : return NULL;
6492 :
6493 96886959 : depth++;
6494 96886959 : ret = extract_muldiv_1 (t, c, code, wide_type);
6495 96886959 : depth--;
6496 :
6497 96886959 : return ret;
6498 : }
6499 :
6500 : static tree
6501 96886959 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6502 : {
6503 96886959 : tree type = TREE_TYPE (t);
6504 96886959 : enum tree_code tcode = TREE_CODE (t);
6505 96886959 : tree ctype = type;
6506 96886959 : if (wide_type)
6507 : {
6508 32370520 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6509 : {
6510 124 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6511 8916305 : ctype = wide_type;
6512 : }
6513 32370396 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6514 64740792 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6515 8916305 : ctype = wide_type;
6516 : }
6517 96886959 : tree t1, t2;
6518 96886959 : bool same_p = tcode == code;
6519 96886959 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6520 :
6521 : /* Don't deal with constants of zero here; they confuse the code below. */
6522 96886959 : if (integer_zerop (c))
6523 : return NULL_TREE;
6524 :
6525 96871448 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6526 38487635 : op0 = TREE_OPERAND (t, 0);
6527 :
6528 96871448 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6529 12250286 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6530 :
6531 : /* Note that we need not handle conditional operations here since fold
6532 : already handles those cases. So just do arithmetic here. */
6533 96871448 : switch (tcode)
6534 : {
6535 4365759 : case INTEGER_CST:
6536 : /* For a constant, we can always simplify if we are a multiply
6537 : or (for divide and modulus) if it is a multiple of our constant. */
6538 4365759 : if (code == MULT_EXPR
6539 5578076 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6540 1212317 : TYPE_SIGN (type)))
6541 : {
6542 3575996 : tree tem = const_binop (code, fold_convert (ctype, t),
6543 : fold_convert (ctype, c));
6544 : /* If the multiplication overflowed, we lost information on it.
6545 : See PR68142 and PR69845. */
6546 3575996 : if (TREE_OVERFLOW (tem))
6547 : return NULL_TREE;
6548 : return tem;
6549 : }
6550 : break;
6551 :
6552 37951827 : CASE_CONVERT: case NON_LVALUE_EXPR:
6553 37951827 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6554 : break;
6555 : /* If op0 is an expression ... */
6556 36692483 : if ((COMPARISON_CLASS_P (op0)
6557 : || UNARY_CLASS_P (op0)
6558 36692483 : || BINARY_CLASS_P (op0)
6559 33642869 : || VL_EXP_CLASS_P (op0)
6560 33581860 : || EXPRESSION_CLASS_P (op0))
6561 : /* ... and has wrapping overflow, and its type is smaller
6562 : than ctype, then we cannot pass through as widening. */
6563 36843968 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6564 1246160 : && (TYPE_PRECISION (ctype)
6565 1246160 : > TYPE_PRECISION (TREE_TYPE (op0))))
6566 : /* ... or this is a truncation (t is narrower than op0),
6567 : then we cannot pass through this narrowing. */
6568 2653731 : || (TYPE_PRECISION (type)
6569 2653731 : < TYPE_PRECISION (TREE_TYPE (op0)))
6570 : /* ... or signedness changes for division or modulus,
6571 : then we cannot pass through this conversion. */
6572 2624060 : || (code != MULT_EXPR
6573 124378 : && (TYPE_UNSIGNED (ctype)
6574 124378 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6575 : /* ... or has undefined overflow while the converted to
6576 : type has not, we cannot do the operation in the inner type
6577 : as that would introduce undefined overflow. */
6578 2525737 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6579 1849147 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6580 : break;
6581 :
6582 : /* Pass the constant down and see if we can make a simplification. If
6583 : we can, replace this expression with the inner simplification for
6584 : possible later conversion to our or some other type. */
6585 34238520 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6586 34238520 : && TREE_CODE (t2) == INTEGER_CST
6587 34238520 : && !TREE_OVERFLOW (t2)
6588 69743719 : && (t1 = extract_muldiv (op0, t2, code,
6589 : code == MULT_EXPR ? ctype : NULL_TREE))
6590 : != 0)
6591 : return t1;
6592 : break;
6593 :
6594 189 : case ABS_EXPR:
6595 : /* If widening the type changes it from signed to unsigned, then we
6596 : must avoid building ABS_EXPR itself as unsigned. */
6597 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6598 : {
6599 0 : tree cstype = (*signed_type_for) (ctype);
6600 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6601 : {
6602 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6603 0 : return fold_convert (ctype, t1);
6604 : }
6605 : break;
6606 : }
6607 : /* If the constant is negative, we cannot simplify this. */
6608 189 : if (tree_int_cst_sgn (c) == -1)
6609 : break;
6610 : /* FALLTHROUGH */
6611 44068 : case NEGATE_EXPR:
6612 : /* For division and modulus, type can't be unsigned, as e.g.
6613 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6614 : For signed types, even with wrapping overflow, this is fine. */
6615 44068 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6616 : break;
6617 42505 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6618 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6619 : break;
6620 :
6621 791 : case MIN_EXPR: case MAX_EXPR:
6622 : /* If widening the type changes the signedness, then we can't perform
6623 : this optimization as that changes the result. */
6624 791 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6625 : break;
6626 :
6627 : /* Punt for multiplication altogether.
6628 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6629 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6630 : 0U, the latter is 2U.
6631 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6632 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6633 : well defined 0, the latter invokes UB.
6634 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6635 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6636 : well defined 25, the latter invokes UB. */
6637 791 : if (code == MULT_EXPR)
6638 : break;
6639 : /* For division/modulo, punt on c being -1 for MAX, as
6640 : MAX (INT_MIN, 0) / -1 is not equivalent to
6641 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6642 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6643 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6644 : transformation won't make it worse. */
6645 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6646 : break;
6647 :
6648 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6649 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6650 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6651 : {
6652 0 : if (tree_int_cst_sgn (c) < 0)
6653 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6654 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6655 : fold_convert (ctype, t2));
6656 : }
6657 : break;
6658 :
6659 1371 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6660 : /* If the second operand is constant, this is a multiplication
6661 : or floor division, by a power of two, so we can treat it that
6662 : way unless the multiplier or divisor overflows. Signed
6663 : left-shift overflow is implementation-defined rather than
6664 : undefined in C90, so do not convert signed left shift into
6665 : multiplication. */
6666 1371 : if (TREE_CODE (op1) == INTEGER_CST
6667 1355 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6668 : /* const_binop may not detect overflow correctly,
6669 : so check for it explicitly here. */
6670 1237 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6671 1380 : wi::to_wide (op1))
6672 1228 : && (t1 = fold_convert (ctype,
6673 : const_binop (LSHIFT_EXPR, size_one_node,
6674 : op1))) != 0
6675 2599 : && !TREE_OVERFLOW (t1))
6676 2254 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6677 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6678 : ctype,
6679 : fold_convert (ctype, op0),
6680 : t1),
6681 1228 : c, code, wide_type);
6682 : break;
6683 :
6684 8454595 : case PLUS_EXPR: case MINUS_EXPR:
6685 : /* See if we can eliminate the operation on both sides. If we can, we
6686 : can return a new PLUS or MINUS. If we can't, the only remaining
6687 : cases where we can do anything are if the second operand is a
6688 : constant. */
6689 8454595 : t1 = extract_muldiv (op0, c, code, wide_type);
6690 8454595 : t2 = extract_muldiv (op1, c, code, wide_type);
6691 818361 : if (t1 != 0 && t2 != 0
6692 285066 : && TYPE_OVERFLOW_WRAPS (ctype)
6693 8730353 : && (code == MULT_EXPR
6694 : /* If not multiplication, we can only do this if both operands
6695 : are divisible by c. */
6696 0 : || (multiple_of_p (ctype, op0, c)
6697 0 : && multiple_of_p (ctype, op1, c))))
6698 : {
6699 275758 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6700 : fold_convert (ctype, t2));
6701 : }
6702 :
6703 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6704 : This simplifies the logic below. */
6705 8178837 : if (tcode == MINUS_EXPR)
6706 : {
6707 2177490 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6708 : /* If OP1 was not easily negatable, the constant may be OP0. */
6709 2177490 : if (TREE_CODE (op0) == INTEGER_CST)
6710 : {
6711 363784 : std::swap (op0, op1);
6712 363784 : std::swap (t1, t2);
6713 : }
6714 : }
6715 :
6716 8178837 : if (TREE_CODE (op1) != INTEGER_CST)
6717 : break;
6718 :
6719 : /* If either OP1 or C are negative, this optimization is not safe for
6720 : some of the division and remainder types while for others we need
6721 : to change the code. */
6722 3787183 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6723 : {
6724 175762 : if (code == CEIL_DIV_EXPR)
6725 : code = FLOOR_DIV_EXPR;
6726 175760 : else if (code == FLOOR_DIV_EXPR)
6727 : code = CEIL_DIV_EXPR;
6728 175347 : else if (code != MULT_EXPR
6729 175347 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6730 : break;
6731 : }
6732 :
6733 : /* If it's a multiply or a division/modulus operation of a multiple
6734 : of our constant, do the operation and verify it doesn't overflow. */
6735 3781915 : if (code == MULT_EXPR
6736 4989700 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6737 1207785 : TYPE_SIGN (type)))
6738 : {
6739 2992740 : op1 = const_binop (code, fold_convert (ctype, op1),
6740 : fold_convert (ctype, c));
6741 : /* We allow the constant to overflow with wrapping semantics. */
6742 2992740 : if (op1 == 0
6743 2992740 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6744 : break;
6745 : }
6746 : else
6747 : break;
6748 :
6749 : /* If we have an unsigned type, we cannot widen the operation since it
6750 : will change the result if the original computation overflowed. */
6751 2989219 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6752 : break;
6753 :
6754 : /* The last case is if we are a multiply. In that case, we can
6755 : apply the distributive law to commute the multiply and addition
6756 : if the multiplication of the constants doesn't overflow
6757 : and overflow is defined. With undefined overflow
6758 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6759 : But fold_plusminus_mult_expr would factor back any power-of-two
6760 : value so do not distribute in the first place in this case. */
6761 2989219 : if (code == MULT_EXPR
6762 2571344 : && TYPE_OVERFLOW_WRAPS (ctype)
6763 5225599 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6764 678761 : return fold_build2 (tcode, ctype,
6765 : fold_build2 (code, ctype,
6766 : fold_convert (ctype, op0),
6767 : fold_convert (ctype, c)),
6768 : op1);
6769 :
6770 : break;
6771 :
6772 2424723 : case MULT_EXPR:
6773 : /* We have a special case here if we are doing something like
6774 : (C * 8) % 4 since we know that's zero. */
6775 2424723 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6776 2424723 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6777 : /* If the multiplication can overflow we cannot optimize this. */
6778 10806 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6779 337 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6780 2435529 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6781 292 : TYPE_SIGN (type)))
6782 : {
6783 8 : return omit_one_operand (type, integer_zero_node, op0);
6784 : }
6785 :
6786 : /* ... fall through ... */
6787 :
6788 2705977 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6789 2705977 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6790 : /* If we can extract our operation from the LHS, do so and return a
6791 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6792 : do something only if the second operand is a constant. */
6793 2705977 : if (same_p
6794 2289150 : && TYPE_OVERFLOW_WRAPS (ctype)
6795 4820278 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6796 105803 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6797 : fold_convert (ctype, op1));
6798 2600174 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6799 2173990 : && TYPE_OVERFLOW_WRAPS (ctype)
6800 4599363 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6801 946070 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6802 : fold_convert (ctype, t1));
6803 1654104 : else if (TREE_CODE (op1) != INTEGER_CST)
6804 : return 0;
6805 :
6806 : /* If these are the same operation types, we can associate them
6807 : assuming no overflow. */
6808 614767 : if (tcode == code)
6809 : {
6810 198482 : bool overflow_p = false;
6811 198482 : wi::overflow_type overflow_mul;
6812 198482 : signop sign = TYPE_SIGN (ctype);
6813 198482 : unsigned prec = TYPE_PRECISION (ctype);
6814 396964 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6815 198482 : wi::to_wide (c, prec),
6816 198482 : sign, &overflow_mul);
6817 198482 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6818 198482 : if (overflow_mul
6819 1607 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6820 : overflow_p = true;
6821 198415 : if (!overflow_p)
6822 198415 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6823 : wide_int_to_tree (ctype, mul));
6824 198482 : }
6825 :
6826 : /* If these operations "cancel" each other, we have the main
6827 : optimizations of this pass, which occur when either constant is a
6828 : multiple of the other, in which case we replace this with either an
6829 : operation or CODE or TCODE.
6830 :
6831 : If we have an unsigned type, we cannot do this since it will change
6832 : the result if the original computation overflowed. */
6833 416352 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6834 99823 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6835 516132 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6836 99740 : || (tcode == MULT_EXPR
6837 99740 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6838 850 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6839 846 : && code != MULT_EXPR)))
6840 : {
6841 880 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6842 880 : TYPE_SIGN (type)))
6843 : {
6844 104 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6845 : fold_convert (ctype,
6846 : const_binop (TRUNC_DIV_EXPR,
6847 : op1, c)));
6848 : }
6849 776 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6850 776 : TYPE_SIGN (type)))
6851 : {
6852 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6853 : fold_convert (ctype,
6854 : const_binop (TRUNC_DIV_EXPR,
6855 : c, op1)));
6856 : }
6857 : }
6858 : break;
6859 :
6860 : default:
6861 : break;
6862 : }
6863 :
6864 : return 0;
6865 : }
6866 :
6867 : /* Return a node which has the indicated constant VALUE (either 0 or
6868 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6869 : and is of the indicated TYPE. */
6870 :
6871 : tree
6872 117427087 : constant_boolean_node (bool value, tree type)
6873 : {
6874 117427087 : if (type == integer_type_node)
6875 20262614 : return value ? integer_one_node : integer_zero_node;
6876 97164473 : else if (type == boolean_type_node)
6877 92769209 : return value ? boolean_true_node : boolean_false_node;
6878 4395264 : else if (VECTOR_TYPE_P (type))
6879 1997 : return build_vector_from_val (type,
6880 1997 : build_int_cst (TREE_TYPE (type),
6881 2730 : value ? -1 : 0));
6882 : else
6883 4393267 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6884 : }
6885 :
6886 :
6887 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6888 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6889 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6890 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6891 : COND is the first argument to CODE; otherwise (as in the example
6892 : given here), it is the second argument. TYPE is the type of the
6893 : original expression. Return NULL_TREE if no simplification is
6894 : possible. */
6895 :
6896 : static tree
6897 961785 : fold_binary_op_with_conditional_arg (location_t loc,
6898 : enum tree_code code,
6899 : tree type, tree op0, tree op1,
6900 : tree cond, tree arg, int cond_first_p)
6901 : {
6902 961785 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6903 961785 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6904 961785 : tree test, true_value, false_value;
6905 961785 : tree lhs = NULL_TREE;
6906 961785 : tree rhs = NULL_TREE;
6907 961785 : enum tree_code cond_code = COND_EXPR;
6908 :
6909 : /* Do not move possibly trapping operations into the conditional as this
6910 : pessimizes code and causes gimplification issues when applied late. */
6911 981449 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6912 194643 : ANY_INTEGRAL_TYPE_P (type)
6913 965621 : && TYPE_OVERFLOW_TRAPS (type), op1))
6914 : return NULL_TREE;
6915 :
6916 941957 : if (TREE_CODE (cond) == COND_EXPR
6917 344386 : || TREE_CODE (cond) == VEC_COND_EXPR)
6918 : {
6919 601292 : test = TREE_OPERAND (cond, 0);
6920 601292 : true_value = TREE_OPERAND (cond, 1);
6921 601292 : false_value = TREE_OPERAND (cond, 2);
6922 : /* If this operand throws an expression, then it does not make
6923 : sense to try to perform a logical or arithmetic operation
6924 : involving it. */
6925 601292 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6926 7463 : lhs = true_value;
6927 601292 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6928 6 : rhs = false_value;
6929 : }
6930 340665 : else if (!(TREE_CODE (type) != VECTOR_TYPE
6931 340559 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
6932 : {
6933 338690 : tree testtype = TREE_TYPE (cond);
6934 338690 : test = cond;
6935 338690 : true_value = constant_boolean_node (true, testtype);
6936 338690 : false_value = constant_boolean_node (false, testtype);
6937 : }
6938 : else
6939 : /* Detect the case of mixing vector and scalar types - bail out. */
6940 : return NULL_TREE;
6941 :
6942 939982 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
6943 3827 : cond_code = VEC_COND_EXPR;
6944 :
6945 : /* This transformation is only worthwhile if we don't have to wrap ARG
6946 : in a SAVE_EXPR and the operation can be simplified without recursing
6947 : on at least one of the branches once its pushed inside the COND_EXPR. */
6948 939982 : if (!TREE_CONSTANT (arg)
6949 939982 : && (TREE_SIDE_EFFECTS (arg)
6950 447160 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6951 442490 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6952 : return NULL_TREE;
6953 :
6954 509128 : arg = fold_convert_loc (loc, arg_type, arg);
6955 509128 : if (lhs == 0)
6956 : {
6957 503097 : true_value = fold_convert_loc (loc, cond_type, true_value);
6958 503097 : if (cond_first_p)
6959 493007 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
6960 : else
6961 10090 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
6962 : }
6963 509128 : if (rhs == 0)
6964 : {
6965 509122 : false_value = fold_convert_loc (loc, cond_type, false_value);
6966 509122 : if (cond_first_p)
6967 498471 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
6968 : else
6969 10651 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
6970 : }
6971 :
6972 : /* Check that we have simplified at least one of the branches. */
6973 509128 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6974 : return NULL_TREE;
6975 :
6976 488567 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6977 : }
6978 :
6979 :
6980 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
6981 :
6982 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
6983 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
6984 : if ARG - ZERO_ARG is the same as X.
6985 :
6986 : If ARG is NULL, check for any value of type TYPE.
6987 :
6988 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6989 : and finite. The problematic cases are when X is zero, and its mode
6990 : has signed zeros. In the case of rounding towards -infinity,
6991 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6992 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
6993 :
6994 : bool
6995 646346 : fold_real_zero_addition_p (const_tree type, const_tree arg,
6996 : const_tree zero_arg, int negate)
6997 : {
6998 646346 : if (!real_zerop (zero_arg))
6999 : return false;
7000 :
7001 : /* Don't allow the fold with -fsignaling-nans. */
7002 645666 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7003 : return false;
7004 :
7005 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7006 642328 : if (!HONOR_SIGNED_ZEROS (type))
7007 : return true;
7008 :
7009 : /* There is no case that is safe for all rounding modes. */
7010 625471 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7011 : return false;
7012 :
7013 : /* In a vector or complex, we would need to check the sign of all zeros. */
7014 624808 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7015 2995 : zero_arg = uniform_vector_p (zero_arg);
7016 624808 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7017 1244 : return false;
7018 :
7019 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7020 623564 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7021 252 : negate = !negate;
7022 :
7023 : /* The mode has signed zeros, and we have to honor their sign.
7024 : In this situation, there are only two cases we can return true for.
7025 : (i) X - 0 is the same as X with default rounding.
7026 : (ii) X + 0 is X when X can't possibly be -0.0. */
7027 623564 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7028 : }
7029 :
7030 : /* Subroutine of match.pd that determines if it is safe to optimize
7031 : a floating point comparison of an integer value, known to be between
7032 : LO and HI, using comparison operator CMP, against the real constant
7033 : R in floating point type FMT, as the same integer comparison against
7034 : the integer constant I, with sign ISIGN.
7035 :
7036 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7037 : because the floating point representations of the neighboring integers
7038 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7039 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7040 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7041 : due to truncation/rounding.
7042 : */
7043 : bool
7044 21253 : fold_cmp_float_cst_p (wide_int lo, wide_int hi, enum tree_code cmp,
7045 : const REAL_VALUE_TYPE *r, format_helper fmt,
7046 : wide_int i, signop isign)
7047 : {
7048 21253 : REAL_VALUE_TYPE raw;
7049 21253 : REAL_VALUE_TYPE rnd;
7050 21253 : bool check_im1 = true;
7051 21253 : bool check_ip1 = true;
7052 :
7053 21253 : switch (cmp)
7054 : {
7055 : case EQ_EXPR:
7056 : case NE_EXPR:
7057 : /* Check both i-1 and i+1. */
7058 : break;
7059 :
7060 19565 : case LT_EXPR:
7061 19565 : case GE_EXPR:
7062 : /* Only check i-1. */
7063 19565 : check_ip1 = false;
7064 19565 : break;
7065 :
7066 436 : case LE_EXPR:
7067 436 : case GT_EXPR:
7068 : /* Only check i+1. */
7069 436 : check_im1 = false;
7070 436 : break;
7071 :
7072 : default:
7073 : return false;
7074 : }
7075 :
7076 21253 : if (flag_rounding_math && i != 0)
7077 : {
7078 344 : real_from_integer (&raw, VOIDmode, i, isign);
7079 344 : if (!real_identical (r, &raw))
7080 : return false;
7081 : }
7082 :
7083 21253 : if (check_im1 && wi::gt_p (i, lo, isign))
7084 : {
7085 1360 : if (flag_rounding_math)
7086 : {
7087 344 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7088 344 : real_convert (&rnd, fmt, &raw);
7089 344 : if (!real_identical (&raw, &rnd))
7090 : return false;
7091 : }
7092 : else
7093 1016 : real_from_integer (&rnd, fmt, i - 1, isign);
7094 1016 : if (real_identical (r, &rnd))
7095 : return false;
7096 : }
7097 :
7098 20565 : if (check_ip1 && wi::lt_p (i, hi, isign))
7099 : {
7100 978 : if (flag_rounding_math)
7101 : {
7102 0 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7103 0 : real_convert (&rnd, fmt, &raw);
7104 0 : if (!real_identical (&raw, &rnd))
7105 : return false;
7106 : }
7107 : else
7108 978 : real_from_integer (&rnd, fmt, i + 1, isign);
7109 978 : if (real_identical (r, &rnd))
7110 : return false;
7111 : }
7112 :
7113 : return true;
7114 : }
7115 :
7116 : /* Subroutine of match.pd that optimizes comparisons of a division by
7117 : a nonzero integer constant against an integer constant, i.e.
7118 : X/C1 op C2.
7119 :
7120 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7121 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7122 :
7123 : enum tree_code
7124 1687664 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7125 : tree *hi, bool *neg_overflow)
7126 : {
7127 1687664 : tree prod, tmp, type = TREE_TYPE (c1);
7128 1687664 : signop sign = TYPE_SIGN (type);
7129 1687664 : wi::overflow_type overflow;
7130 :
7131 : /* We have to do this the hard way to detect unsigned overflow.
7132 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7133 1687664 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7134 1687664 : prod = force_fit_type (type, val, -1, overflow);
7135 1687664 : *neg_overflow = false;
7136 :
7137 1687664 : if (sign == UNSIGNED)
7138 : {
7139 1657872 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7140 1657872 : *lo = prod;
7141 :
7142 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7143 1657872 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7144 1657872 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7145 : }
7146 29792 : else if (tree_int_cst_sgn (c1) >= 0)
7147 : {
7148 28393 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7149 28393 : switch (tree_int_cst_sgn (c2))
7150 : {
7151 4860 : case -1:
7152 4860 : *neg_overflow = true;
7153 4860 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7154 4860 : *hi = prod;
7155 4860 : break;
7156 :
7157 14907 : case 0:
7158 14907 : *lo = fold_negate_const (tmp, type);
7159 14907 : *hi = tmp;
7160 14907 : break;
7161 :
7162 8626 : case 1:
7163 8626 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7164 8626 : *lo = prod;
7165 8626 : break;
7166 :
7167 0 : default:
7168 0 : gcc_unreachable ();
7169 : }
7170 : }
7171 : else
7172 : {
7173 : /* A negative divisor reverses the relational operators. */
7174 1399 : code = swap_tree_comparison (code);
7175 :
7176 1399 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7177 1399 : switch (tree_int_cst_sgn (c2))
7178 : {
7179 134 : case -1:
7180 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7181 134 : *lo = prod;
7182 134 : break;
7183 :
7184 167 : case 0:
7185 167 : *hi = fold_negate_const (tmp, type);
7186 167 : *lo = tmp;
7187 167 : break;
7188 :
7189 1098 : case 1:
7190 1098 : *neg_overflow = true;
7191 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7192 1098 : *hi = prod;
7193 1098 : break;
7194 :
7195 0 : default:
7196 0 : gcc_unreachable ();
7197 : }
7198 : }
7199 :
7200 1687664 : if (code != EQ_EXPR && code != NE_EXPR)
7201 : return code;
7202 :
7203 16809 : if (TREE_OVERFLOW (*lo)
7204 16809 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7205 714 : *lo = NULL_TREE;
7206 16809 : if (TREE_OVERFLOW (*hi)
7207 16809 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7208 95 : *hi = NULL_TREE;
7209 :
7210 : return code;
7211 1687664 : }
7212 :
7213 : /* Test whether it is preferable to swap two operands, ARG0 and
7214 : ARG1, for example because ARG0 is an integer constant and ARG1
7215 : isn't. */
7216 :
7217 : bool
7218 1606476332 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7219 : {
7220 1606476332 : if (CONSTANT_CLASS_P (arg1))
7221 : return false;
7222 524114603 : if (CONSTANT_CLASS_P (arg0))
7223 : return true;
7224 :
7225 482879330 : STRIP_NOPS (arg0);
7226 482879330 : STRIP_NOPS (arg1);
7227 :
7228 482879330 : if (TREE_CONSTANT (arg1))
7229 : return false;
7230 465493709 : if (TREE_CONSTANT (arg0))
7231 : return true;
7232 :
7233 : /* Put addresses in arg1. */
7234 464724092 : if (TREE_CODE (arg1) == ADDR_EXPR)
7235 : return false;
7236 446280207 : if (TREE_CODE (arg0) == ADDR_EXPR)
7237 : return true;
7238 :
7239 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7240 : for commutative and comparison operators. Ensuring a canonical
7241 : form allows the optimizers to find additional redundancies without
7242 : having to explicitly check for both orderings. */
7243 445888094 : if (TREE_CODE (arg0) == SSA_NAME
7244 336372678 : && TREE_CODE (arg1) == SSA_NAME
7245 776384534 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7246 : return true;
7247 :
7248 : /* Put SSA_NAMEs last. */
7249 422993164 : if (TREE_CODE (arg1) == SSA_NAME)
7250 : return false;
7251 100321165 : if (TREE_CODE (arg0) == SSA_NAME)
7252 : return true;
7253 :
7254 : /* Put variables last. */
7255 94444927 : if (DECL_P (arg1))
7256 : return false;
7257 51151868 : if (DECL_P (arg0))
7258 : return true;
7259 :
7260 : return false;
7261 : }
7262 :
7263 :
7264 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7265 : means A >= Y && A != MAX, but in this case we know that
7266 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7267 :
7268 : static tree
7269 23868413 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7270 : {
7271 23868413 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7272 :
7273 23868413 : if (TREE_CODE (bound) == LT_EXPR)
7274 4965699 : a = TREE_OPERAND (bound, 0);
7275 18902714 : else if (TREE_CODE (bound) == GT_EXPR)
7276 2785293 : a = TREE_OPERAND (bound, 1);
7277 : else
7278 : return NULL_TREE;
7279 :
7280 7750992 : typea = TREE_TYPE (a);
7281 7750992 : if (!INTEGRAL_TYPE_P (typea)
7282 485164 : && !POINTER_TYPE_P (typea))
7283 : return NULL_TREE;
7284 :
7285 7573336 : if (TREE_CODE (ineq) == LT_EXPR)
7286 : {
7287 1464586 : a1 = TREE_OPERAND (ineq, 1);
7288 1464586 : y = TREE_OPERAND (ineq, 0);
7289 : }
7290 6108750 : else if (TREE_CODE (ineq) == GT_EXPR)
7291 : {
7292 1159264 : a1 = TREE_OPERAND (ineq, 0);
7293 1159264 : y = TREE_OPERAND (ineq, 1);
7294 : }
7295 : else
7296 : return NULL_TREE;
7297 :
7298 2623850 : if (TREE_TYPE (a1) != typea)
7299 : return NULL_TREE;
7300 :
7301 1875011 : if (POINTER_TYPE_P (typea))
7302 : {
7303 : /* Convert the pointer types into integer before taking the difference. */
7304 11283 : tree ta = fold_convert_loc (loc, ssizetype, a);
7305 11283 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7306 11283 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7307 : }
7308 : else
7309 1863728 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7310 :
7311 1875011 : if (!diff || !integer_onep (diff))
7312 1866102 : return NULL_TREE;
7313 :
7314 8909 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7315 : }
7316 :
7317 : /* Fold a sum or difference of at least one multiplication.
7318 : Returns the folded tree or NULL if no simplification could be made. */
7319 :
7320 : static tree
7321 9304808 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7322 : tree arg0, tree arg1)
7323 : {
7324 9304808 : tree arg00, arg01, arg10, arg11;
7325 9304808 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7326 :
7327 : /* (A * C) +- (B * C) -> (A+-B) * C.
7328 : (A * C) +- A -> A * (C+-1).
7329 : We are most concerned about the case where C is a constant,
7330 : but other combinations show up during loop reduction. Since
7331 : it is not difficult, try all four possibilities. */
7332 :
7333 9304808 : if (TREE_CODE (arg0) == MULT_EXPR)
7334 : {
7335 8293480 : arg00 = TREE_OPERAND (arg0, 0);
7336 8293480 : arg01 = TREE_OPERAND (arg0, 1);
7337 : }
7338 1011328 : else if (TREE_CODE (arg0) == INTEGER_CST)
7339 : {
7340 74538 : arg00 = build_one_cst (type);
7341 74538 : arg01 = arg0;
7342 : }
7343 : else
7344 : {
7345 : /* We cannot generate constant 1 for fract. */
7346 936790 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7347 0 : return NULL_TREE;
7348 936790 : arg00 = arg0;
7349 936790 : arg01 = build_one_cst (type);
7350 : }
7351 9304808 : if (TREE_CODE (arg1) == MULT_EXPR)
7352 : {
7353 2381916 : arg10 = TREE_OPERAND (arg1, 0);
7354 2381916 : arg11 = TREE_OPERAND (arg1, 1);
7355 : }
7356 6922892 : else if (TREE_CODE (arg1) == INTEGER_CST)
7357 : {
7358 3628781 : arg10 = build_one_cst (type);
7359 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7360 : the purpose of this canonicalization. */
7361 7027602 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7362 233248 : && negate_expr_p (arg1)
7363 3858741 : && code == PLUS_EXPR)
7364 : {
7365 229960 : arg11 = negate_expr (arg1);
7366 229960 : code = MINUS_EXPR;
7367 : }
7368 : else
7369 : arg11 = arg1;
7370 : }
7371 : else
7372 : {
7373 : /* We cannot generate constant 1 for fract. */
7374 3294111 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7375 0 : return NULL_TREE;
7376 3294111 : arg10 = arg1;
7377 3294111 : arg11 = build_one_cst (type);
7378 : }
7379 9304808 : same = NULL_TREE;
7380 :
7381 : /* Prefer factoring a common non-constant. */
7382 9304808 : if (operand_equal_p (arg00, arg10, 0))
7383 : same = arg00, alt0 = arg01, alt1 = arg11;
7384 9301039 : else if (operand_equal_p (arg01, arg11, 0))
7385 : same = arg01, alt0 = arg00, alt1 = arg10;
7386 9195659 : else if (operand_equal_p (arg00, arg11, 0))
7387 : same = arg00, alt0 = arg01, alt1 = arg10;
7388 9195597 : else if (operand_equal_p (arg01, arg10, 0))
7389 : same = arg01, alt0 = arg00, alt1 = arg11;
7390 :
7391 : /* No identical multiplicands; see if we can find a common
7392 : power-of-two factor in non-power-of-two multiplies. This
7393 : can help in multi-dimensional array access. */
7394 9190407 : else if (TREE_CODE (arg01) == INTEGER_CST
7395 8134989 : && TREE_CODE (arg11) == INTEGER_CST)
7396 : {
7397 7924026 : wide_int int01 = wi::to_wide (arg01);
7398 7924026 : wide_int int11 = wi::to_wide (arg11);
7399 7924026 : bool swap = false;
7400 7924026 : tree maybe_same;
7401 :
7402 : /* Move min of absolute values to int11. */
7403 7924028 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7404 : {
7405 3643437 : std::swap (int01, int11);
7406 3643437 : std::swap (arg00, arg10);
7407 3643437 : maybe_same = arg01;
7408 3643437 : swap = true;
7409 : }
7410 : else
7411 : maybe_same = arg11;
7412 :
7413 7924026 : wide_int factor = wi::abs (int11);
7414 7924026 : if (wi::gtu_p (factor, 1u)
7415 4353455 : && wi::exact_log2 (factor) != -1
7416 12435148 : && (int01 & (factor - 1)) == 0
7417 : /* The remainder should not be a constant, otherwise we
7418 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7419 : increased the number of multiplications necessary. */
7420 9480571 : && TREE_CODE (arg10) != INTEGER_CST)
7421 : {
7422 2622218 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7423 1311109 : wide_int_to_tree (TREE_TYPE (arg00),
7424 1311109 : wi::sdiv_trunc (int01,
7425 : int11)));
7426 1311109 : alt1 = arg10;
7427 1311109 : same = maybe_same;
7428 1311109 : if (swap)
7429 1188372 : std::swap (alt0, alt1);
7430 : }
7431 7924028 : }
7432 :
7433 8038427 : if (!same)
7434 7879298 : return NULL_TREE;
7435 :
7436 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7437 1425510 : || TYPE_OVERFLOW_WRAPS (type)
7438 : /* We are neither factoring zero nor minus one. */
7439 1542077 : || TREE_CODE (same) == INTEGER_CST)
7440 1413884 : return fold_build2_loc (loc, MULT_EXPR, type,
7441 : fold_build2_loc (loc, code, type,
7442 : fold_convert_loc (loc, type, alt0),
7443 : fold_convert_loc (loc, type, alt1)),
7444 1413884 : fold_convert_loc (loc, type, same));
7445 :
7446 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7447 : same may be minus one and thus the multiplication may overflow. Perform
7448 : the sum operation in an unsigned type. */
7449 11626 : tree utype = unsigned_type_for (type);
7450 11626 : tree tem = fold_build2_loc (loc, code, utype,
7451 : fold_convert_loc (loc, utype, alt0),
7452 : fold_convert_loc (loc, utype, alt1));
7453 : /* If the sum evaluated to a constant that is not -INF the multiplication
7454 : cannot overflow. */
7455 23252 : if (TREE_CODE (tem) == INTEGER_CST
7456 18126 : && (wi::to_wide (tem)
7457 18126 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7458 3237 : return fold_build2_loc (loc, MULT_EXPR, type,
7459 3237 : fold_convert (type, tem), same);
7460 :
7461 : /* Do not resort to unsigned multiplication because
7462 : we lose the no-overflow property of the expression. */
7463 : return NULL_TREE;
7464 : }
7465 :
7466 :
7467 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7468 : into the buffer PTR of length LEN bytes.
7469 : Return the number of bytes placed in the buffer, or zero
7470 : upon failure. */
7471 :
7472 : int
7473 56368885 : native_encode_wide_int (tree type, const wide_int_ref &val,
7474 : unsigned char *ptr, int len, int off)
7475 : {
7476 56368885 : int total_bytes;
7477 56368885 : if (BITINT_TYPE_P (type))
7478 : {
7479 17399 : struct bitint_info info;
7480 17399 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7481 17399 : gcc_assert (ok);
7482 17399 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7483 17399 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7484 : {
7485 17093 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7486 : /* More work is needed when adding _BitInt support to PDP endian
7487 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7488 : match target endianity here. */
7489 17093 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7490 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7491 : || (GET_MODE_SIZE (limb_mode)
7492 : >= UNITS_PER_WORD)));
7493 : }
7494 : else
7495 612 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7496 : }
7497 : else
7498 112702972 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7499 56368885 : int byte, offset, word, words;
7500 56368885 : unsigned char value;
7501 :
7502 56368885 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7503 : return 0;
7504 56368397 : if (off == -1)
7505 55493016 : off = 0;
7506 :
7507 56368397 : if (ptr == NULL)
7508 : /* Dry run. */
7509 2818591 : return MIN (len, total_bytes - off);
7510 :
7511 : words = total_bytes / UNITS_PER_WORD;
7512 :
7513 257158686 : for (byte = 0; byte < total_bytes; byte++)
7514 : {
7515 203608880 : int bitpos = byte * BITS_PER_UNIT;
7516 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7517 : number of bytes. */
7518 203608880 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7519 :
7520 203608880 : if (total_bytes > UNITS_PER_WORD)
7521 : {
7522 203608880 : word = byte / UNITS_PER_WORD;
7523 203608880 : if (WORDS_BIG_ENDIAN)
7524 : word = (words - 1) - word;
7525 203608880 : offset = word * UNITS_PER_WORD;
7526 203608880 : if (BYTES_BIG_ENDIAN)
7527 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7528 : else
7529 203608880 : offset += byte % UNITS_PER_WORD;
7530 : }
7531 : else
7532 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7533 203608880 : if (offset >= off && offset - off < len)
7534 202308016 : ptr[offset - off] = value;
7535 : }
7536 53549806 : return MIN (len, total_bytes - off);
7537 : }
7538 :
7539 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7540 : specified by EXPR into the buffer PTR of length LEN bytes.
7541 : Return the number of bytes placed in the buffer, or zero
7542 : upon failure. */
7543 :
7544 : static int
7545 56368885 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7546 : {
7547 56368885 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7548 56368885 : ptr, len, off);
7549 : }
7550 :
7551 :
7552 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7553 : specified by EXPR into the buffer PTR of length LEN bytes.
7554 : Return the number of bytes placed in the buffer, or zero
7555 : upon failure. */
7556 :
7557 : static int
7558 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7559 : {
7560 0 : tree type = TREE_TYPE (expr);
7561 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7562 0 : int total_bytes = GET_MODE_SIZE (mode);
7563 0 : FIXED_VALUE_TYPE value;
7564 0 : tree i_value, i_type;
7565 :
7566 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7567 : return 0;
7568 :
7569 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7570 :
7571 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7572 : return 0;
7573 :
7574 0 : value = TREE_FIXED_CST (expr);
7575 0 : i_value = double_int_to_tree (i_type, value.data);
7576 :
7577 0 : return native_encode_int (i_value, ptr, len, off);
7578 : }
7579 :
7580 :
7581 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7582 : specified by EXPR into the buffer PTR of length LEN bytes.
7583 : Return the number of bytes placed in the buffer, or zero
7584 : upon failure. */
7585 :
7586 : int
7587 834096 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7588 : unsigned char *ptr, int len, int off)
7589 : {
7590 834096 : int total_bytes = GET_MODE_SIZE (mode);
7591 834096 : int byte, offset, word, words, bitpos;
7592 834096 : unsigned char value;
7593 :
7594 : /* There are always 32 bits in each long, no matter the size of
7595 : the hosts long. We handle floating point representations with
7596 : up to 192 bits. */
7597 834096 : long tmp[6];
7598 :
7599 834096 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7600 : return 0;
7601 831796 : if (off == -1)
7602 726476 : off = 0;
7603 :
7604 831796 : if (ptr == NULL)
7605 : /* Dry run. */
7606 137351 : return MIN (len, total_bytes - off);
7607 :
7608 694445 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7609 :
7610 694445 : real_to_target (tmp, val, mode);
7611 :
7612 6805403 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7613 6110958 : bitpos += BITS_PER_UNIT)
7614 : {
7615 6110958 : byte = (bitpos / BITS_PER_UNIT) & 3;
7616 6110958 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7617 :
7618 6110958 : if (UNITS_PER_WORD < 4)
7619 : {
7620 : word = byte / UNITS_PER_WORD;
7621 : if (WORDS_BIG_ENDIAN)
7622 : word = (words - 1) - word;
7623 : offset = word * UNITS_PER_WORD;
7624 : if (BYTES_BIG_ENDIAN)
7625 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7626 : else
7627 : offset += byte % UNITS_PER_WORD;
7628 : }
7629 : else
7630 : {
7631 6110958 : offset = byte;
7632 6110958 : if (BYTES_BIG_ENDIAN)
7633 : {
7634 : /* Reverse bytes within each long, or within the entire float
7635 : if it's smaller than a long (for HFmode). */
7636 : offset = MIN (3, total_bytes - 1) - offset;
7637 : gcc_assert (offset >= 0);
7638 : }
7639 : }
7640 6110958 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7641 6110958 : if (offset >= off
7642 6107718 : && offset - off < len)
7643 6086058 : ptr[offset - off] = value;
7644 : }
7645 694445 : return MIN (len, total_bytes - off);
7646 : }
7647 :
7648 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7649 : specified by EXPR into the buffer PTR of length LEN bytes.
7650 : Return the number of bytes placed in the buffer, or zero
7651 : upon failure. */
7652 :
7653 : static int
7654 10215 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7655 : {
7656 10215 : int rsize, isize;
7657 10215 : tree part;
7658 :
7659 10215 : part = TREE_REALPART (expr);
7660 10215 : rsize = native_encode_expr (part, ptr, len, off);
7661 10215 : if (off == -1 && rsize == 0)
7662 : return 0;
7663 10215 : part = TREE_IMAGPART (expr);
7664 10215 : if (off != -1)
7665 20421 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7666 10215 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7667 : len - rsize, off);
7668 10215 : if (off == -1 && isize != rsize)
7669 : return 0;
7670 10215 : return rsize + isize;
7671 : }
7672 :
7673 : /* Like native_encode_vector, but only encode the first COUNT elements.
7674 : The other arguments are as for native_encode_vector. */
7675 :
7676 : static int
7677 1068276 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7678 : int off, unsigned HOST_WIDE_INT count)
7679 : {
7680 1068276 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7681 2136552 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7682 1069310 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7683 : {
7684 : /* This is the only case in which elements can be smaller than a byte.
7685 : Element 0 is always in the lsb of the containing byte. */
7686 956 : unsigned int elt_bits = TYPE_PRECISION (itype);
7687 956 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7688 956 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7689 : return 0;
7690 :
7691 956 : if (off == -1)
7692 956 : off = 0;
7693 :
7694 : /* Zero the buffer and then set bits later where necessary. */
7695 956 : int extract_bytes = MIN (len, total_bytes - off);
7696 956 : if (ptr)
7697 956 : memset (ptr, 0, extract_bytes);
7698 :
7699 956 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7700 956 : unsigned int first_elt = off * elts_per_byte;
7701 956 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7702 956 : unsigned int elt_mask = (1 << elt_bits) - 1;
7703 17477 : for (unsigned int i = 0; i < extract_elts; ++i)
7704 : {
7705 16521 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7706 16521 : if (TREE_CODE (elt) != INTEGER_CST)
7707 : return 0;
7708 :
7709 16521 : if (ptr && integer_nonzerop (elt))
7710 : {
7711 8488 : unsigned int bit = i * elt_bits;
7712 8488 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7713 : }
7714 : }
7715 : return extract_bytes;
7716 : }
7717 :
7718 1067320 : int offset = 0;
7719 1067320 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7720 4288846 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7721 : {
7722 3817417 : if (off >= size)
7723 : {
7724 23380 : off -= size;
7725 23380 : continue;
7726 : }
7727 3794037 : tree elem = VECTOR_CST_ELT (expr, i);
7728 3794037 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7729 : len - offset, off);
7730 3794037 : if ((off == -1 && res != size) || res == 0)
7731 : return 0;
7732 3793508 : offset += res;
7733 3793508 : if (offset >= len)
7734 595362 : return (off == -1 && i < count - 1) ? 0 : offset;
7735 3198146 : if (off != -1)
7736 437090 : off = 0;
7737 : }
7738 : return offset;
7739 : }
7740 :
7741 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7742 : specified by EXPR into the buffer PTR of length LEN bytes.
7743 : Return the number of bytes placed in the buffer, or zero
7744 : upon failure. */
7745 :
7746 : static int
7747 913636 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7748 : {
7749 913636 : unsigned HOST_WIDE_INT count;
7750 913636 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7751 : return 0;
7752 913636 : return native_encode_vector_part (expr, ptr, len, off, count);
7753 : }
7754 :
7755 :
7756 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7757 : specified by EXPR into the buffer PTR of length LEN bytes.
7758 : Return the number of bytes placed in the buffer, or zero
7759 : upon failure. */
7760 :
7761 : static int
7762 140282 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7763 : {
7764 140282 : tree type = TREE_TYPE (expr);
7765 :
7766 : /* Wide-char strings are encoded in target byte-order so native
7767 : encoding them is trivial. */
7768 140282 : if (BITS_PER_UNIT != CHAR_BIT
7769 140282 : || TREE_CODE (type) != ARRAY_TYPE
7770 140282 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7771 280564 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7772 : return 0;
7773 :
7774 140282 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7775 140282 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7776 : return 0;
7777 139414 : if (off == -1)
7778 56368 : off = 0;
7779 139414 : len = MIN (total_bytes - off, len);
7780 139414 : if (ptr == NULL)
7781 : /* Dry run. */;
7782 : else
7783 : {
7784 139414 : int written = 0;
7785 139414 : if (off < TREE_STRING_LENGTH (expr))
7786 : {
7787 138933 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7788 138933 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7789 : }
7790 139414 : memset (ptr + written, 0, len - written);
7791 : }
7792 : return len;
7793 : }
7794 :
7795 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7796 : specified by EXPR into the buffer PTR of length LEN bytes.
7797 : Return the number of bytes placed in the buffer, or zero
7798 : upon failure. */
7799 :
7800 : static int
7801 46817 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7802 : {
7803 : /* We are only concerned with zero-initialization constructors here. That's
7804 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7805 : deal with. For more general handling of constructors, there is
7806 : native_encode_initializer. */
7807 46817 : if (CONSTRUCTOR_NELTS (expr))
7808 : return 0;
7809 :
7810 : /* Wide-char strings are encoded in target byte-order so native
7811 : encoding them is trivial. */
7812 87238 : if (BITS_PER_UNIT != CHAR_BIT
7813 43619 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7814 : return 0;
7815 :
7816 43619 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7817 43619 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7818 : return 0;
7819 43619 : if (off == -1)
7820 0 : off = 0;
7821 43619 : len = MIN (total_bytes - off, len);
7822 43619 : if (ptr == NULL)
7823 : /* Dry run. */;
7824 : else
7825 43619 : memset (ptr, 0, len);
7826 : return len;
7827 : }
7828 :
7829 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7830 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7831 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7832 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7833 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7834 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7835 : Return the number of bytes placed in the buffer, or zero upon failure. */
7836 :
7837 : int
7838 72585878 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7839 : {
7840 : /* We don't support starting at negative offset and -1 is special. */
7841 72585878 : if (off < -1)
7842 : return 0;
7843 :
7844 72585866 : switch (TREE_CODE (expr))
7845 : {
7846 56366639 : case INTEGER_CST:
7847 56366639 : return native_encode_int (expr, ptr, len, off);
7848 :
7849 834096 : case REAL_CST:
7850 834096 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7851 1668192 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7852 :
7853 0 : case FIXED_CST:
7854 0 : return native_encode_fixed (expr, ptr, len, off);
7855 :
7856 10215 : case COMPLEX_CST:
7857 10215 : return native_encode_complex (expr, ptr, len, off);
7858 :
7859 913636 : case VECTOR_CST:
7860 913636 : return native_encode_vector (expr, ptr, len, off);
7861 :
7862 140282 : case STRING_CST:
7863 140282 : return native_encode_string (expr, ptr, len, off);
7864 :
7865 46817 : case CONSTRUCTOR:
7866 46817 : return native_encode_constructor (expr, ptr, len, off);
7867 :
7868 : default:
7869 : return 0;
7870 : }
7871 : }
7872 :
7873 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7874 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7875 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7876 : machine modes, we can't just use build_nonstandard_integer_type. */
7877 :
7878 : tree
7879 541 : find_bitfield_repr_type (int fieldsize, int len)
7880 : {
7881 541 : machine_mode mode;
7882 1063 : for (int pass = 0; pass < 2; pass++)
7883 : {
7884 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7885 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7886 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7887 7286 : && known_eq (GET_MODE_PRECISION (mode),
7888 : GET_MODE_BITSIZE (mode))
7889 11274 : && known_le (GET_MODE_SIZE (mode), len))
7890 : {
7891 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7892 280 : if (ret && TYPE_MODE (ret) == mode)
7893 : return ret;
7894 : }
7895 : }
7896 :
7897 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7898 261 : if (int_n_enabled_p[i]
7899 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7900 261 : && int_n_trees[i].unsigned_type)
7901 : {
7902 261 : tree ret = int_n_trees[i].unsigned_type;
7903 261 : mode = TYPE_MODE (ret);
7904 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7905 522 : && known_eq (GET_MODE_PRECISION (mode),
7906 : GET_MODE_BITSIZE (mode))
7907 783 : && known_le (GET_MODE_SIZE (mode), len))
7908 : return ret;
7909 : }
7910 :
7911 : return NULL_TREE;
7912 : }
7913 :
7914 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7915 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7916 : to be non-NULL and OFF zero), then in addition to filling the
7917 : bytes pointed by PTR with the value also clear any bits pointed
7918 : by MASK that are known to be initialized, keep them as is for
7919 : e.g. uninitialized padding bits or uninitialized fields. */
7920 :
7921 : int
7922 49374252 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7923 : int off, unsigned char *mask)
7924 : {
7925 49374252 : int r;
7926 :
7927 : /* We don't support starting at negative offset and -1 is special. */
7928 49374252 : if (off < -1 || init == NULL_TREE)
7929 : return 0;
7930 :
7931 49374252 : gcc_assert (mask == NULL || (off == 0 && ptr));
7932 :
7933 49374252 : STRIP_NOPS (init);
7934 49374252 : switch (TREE_CODE (init))
7935 : {
7936 0 : case VIEW_CONVERT_EXPR:
7937 0 : case NON_LVALUE_EXPR:
7938 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7939 0 : mask);
7940 47210570 : default:
7941 47210570 : r = native_encode_expr (init, ptr, len, off);
7942 47210570 : if (mask)
7943 7132 : memset (mask, 0, r);
7944 : return r;
7945 2163682 : case CONSTRUCTOR:
7946 2163682 : tree type = TREE_TYPE (init);
7947 2163682 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7948 2163682 : if (total_bytes < 0)
7949 : return 0;
7950 2163682 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7951 : return 0;
7952 2163606 : int o = off == -1 ? 0 : off;
7953 2163606 : if (TREE_CODE (type) == ARRAY_TYPE)
7954 : {
7955 290065 : tree min_index;
7956 290065 : unsigned HOST_WIDE_INT cnt;
7957 290065 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7958 290065 : constructor_elt *ce;
7959 :
7960 290065 : if (!TYPE_DOMAIN (type)
7961 290065 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7962 : return 0;
7963 :
7964 290065 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7965 290065 : if (fieldsize <= 0)
7966 : return 0;
7967 :
7968 290065 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7969 290065 : if (ptr)
7970 290065 : memset (ptr, '\0', MIN (total_bytes - off, len));
7971 :
7972 46603703 : for (cnt = 0; ; cnt++)
7973 : {
7974 46893768 : tree val = NULL_TREE, index = NULL_TREE;
7975 46893768 : HOST_WIDE_INT pos = curpos, count = 0;
7976 46893768 : bool full = false;
7977 46893768 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7978 : {
7979 46782049 : val = ce->value;
7980 46782049 : index = ce->index;
7981 : }
7982 111719 : else if (mask == NULL
7983 618 : || CONSTRUCTOR_NO_CLEARING (init)
7984 112277 : || curpos >= total_bytes)
7985 : break;
7986 : else
7987 : pos = total_bytes;
7988 :
7989 46782049 : if (index && TREE_CODE (index) == RANGE_EXPR)
7990 : {
7991 18 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
7992 18 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
7993 0 : return 0;
7994 18 : offset_int wpos
7995 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
7996 36 : - wi::to_offset (min_index),
7997 18 : TYPE_PRECISION (sizetype));
7998 18 : wpos *= fieldsize;
7999 18 : if (!wi::fits_shwi_p (pos))
8000 : return 0;
8001 18 : pos = wpos.to_shwi ();
8002 18 : offset_int wcount
8003 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8004 36 : - wi::to_offset (TREE_OPERAND (index, 0)),
8005 18 : TYPE_PRECISION (sizetype));
8006 18 : if (!wi::fits_shwi_p (wcount))
8007 : return 0;
8008 18 : count = wcount.to_shwi ();
8009 18 : }
8010 46275513 : else if (index)
8011 : {
8012 46275513 : if (TREE_CODE (index) != INTEGER_CST)
8013 0 : return 0;
8014 46275513 : offset_int wpos
8015 46275513 : = wi::sext (wi::to_offset (index)
8016 92551026 : - wi::to_offset (min_index),
8017 46275513 : TYPE_PRECISION (sizetype));
8018 46275513 : wpos *= fieldsize;
8019 46275513 : if (!wi::fits_shwi_p (wpos))
8020 : return 0;
8021 46275513 : pos = wpos.to_shwi ();
8022 : }
8023 :
8024 46783310 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8025 : {
8026 72 : if (valueinit == -1)
8027 : {
8028 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8029 144 : r = native_encode_initializer (zero, ptr + curpos,
8030 : fieldsize, 0,
8031 72 : mask + curpos);
8032 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8033 2 : ggc_free (zero);
8034 72 : if (!r)
8035 : return 0;
8036 72 : valueinit = curpos;
8037 72 : curpos += fieldsize;
8038 : }
8039 102 : while (curpos != pos)
8040 : {
8041 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8042 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8043 30 : curpos += fieldsize;
8044 : }
8045 : }
8046 :
8047 46782121 : curpos = pos;
8048 46782121 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8049 : {
8050 527 : if (count)
8051 : return 0;
8052 527 : if (off == -1
8053 527 : || (curpos >= off
8054 0 : && (curpos + RAW_DATA_LENGTH (val)
8055 0 : <= (HOST_WIDE_INT) off + len)))
8056 : {
8057 527 : if (ptr)
8058 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8059 527 : RAW_DATA_LENGTH (val));
8060 527 : if (mask)
8061 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8062 : }
8063 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8064 0 : && curpos < (HOST_WIDE_INT) off + len)
8065 : {
8066 : /* Partial overlap. */
8067 0 : unsigned char *p = NULL;
8068 0 : int no = 0;
8069 0 : int l;
8070 0 : gcc_assert (mask == NULL);
8071 0 : if (curpos >= off)
8072 : {
8073 0 : if (ptr)
8074 0 : p = ptr + curpos - off;
8075 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8076 : RAW_DATA_LENGTH (val));
8077 : }
8078 : else
8079 : {
8080 0 : p = ptr;
8081 0 : no = off - curpos;
8082 0 : l = len;
8083 : }
8084 0 : if (p)
8085 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8086 : }
8087 527 : curpos += RAW_DATA_LENGTH (val);
8088 527 : val = NULL_TREE;
8089 : }
8090 527 : if (val)
8091 46859564 : do
8092 : {
8093 46859564 : if (off == -1
8094 602328 : || (curpos >= off
8095 201181 : && (curpos + fieldsize
8096 201181 : <= (HOST_WIDE_INT) off + len)))
8097 : {
8098 46428031 : if (full)
8099 : {
8100 78042 : if (ptr)
8101 78042 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8102 : fieldsize);
8103 78042 : if (mask)
8104 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8105 : }
8106 92871890 : else if (!native_encode_initializer (val,
8107 : ptr
8108 46349989 : ? ptr + curpos - o
8109 : : NULL,
8110 : fieldsize,
8111 : off == -1 ? -1
8112 : : 0,
8113 : mask
8114 1117 : ? mask + curpos
8115 : : NULL))
8116 : return 0;
8117 : else
8118 : {
8119 : full = true;
8120 : pos = curpos;
8121 : }
8122 : }
8123 431533 : else if (curpos + fieldsize > off
8124 32646 : && curpos < (HOST_WIDE_INT) off + len)
8125 : {
8126 : /* Partial overlap. */
8127 8135 : unsigned char *p = NULL;
8128 8135 : int no = 0;
8129 8135 : int l;
8130 8135 : gcc_assert (mask == NULL);
8131 8135 : if (curpos >= off)
8132 : {
8133 5875 : if (ptr)
8134 5875 : p = ptr + curpos - off;
8135 5875 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8136 : fieldsize);
8137 : }
8138 : else
8139 : {
8140 2260 : p = ptr;
8141 2260 : no = off - curpos;
8142 2260 : l = len;
8143 : }
8144 8135 : if (!native_encode_initializer (val, p, l, no, NULL))
8145 : return 0;
8146 : }
8147 46681146 : curpos += fieldsize;
8148 : }
8149 46681146 : while (count-- != 0);
8150 46603703 : }
8151 111647 : return MIN (total_bytes - off, len);
8152 : }
8153 1873541 : else if (TREE_CODE (type) == RECORD_TYPE
8154 1873541 : || TREE_CODE (type) == UNION_TYPE)
8155 : {
8156 1873541 : unsigned HOST_WIDE_INT cnt;
8157 1873541 : constructor_elt *ce;
8158 1873541 : tree fld_base = TYPE_FIELDS (type);
8159 1873541 : tree to_free = NULL_TREE;
8160 :
8161 1873541 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8162 1873541 : if (ptr != NULL)
8163 1873541 : memset (ptr, '\0', MIN (total_bytes - o, len));
8164 368184 : for (cnt = 0; ; cnt++)
8165 : {
8166 2241725 : tree val = NULL_TREE, field = NULL_TREE;
8167 2241725 : HOST_WIDE_INT pos = 0, fieldsize;
8168 2241725 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8169 :
8170 2241725 : if (to_free)
8171 : {
8172 0 : ggc_free (to_free);
8173 0 : to_free = NULL_TREE;
8174 : }
8175 :
8176 2241725 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8177 : {
8178 395341 : val = ce->value;
8179 395341 : field = ce->index;
8180 395341 : if (field == NULL_TREE)
8181 : return 0;
8182 :
8183 395341 : pos = int_byte_position (field);
8184 395341 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8185 1496 : continue;
8186 : }
8187 1846384 : else if (mask == NULL
8188 1846384 : || CONSTRUCTOR_NO_CLEARING (init))
8189 : break;
8190 : else
8191 : pos = total_bytes;
8192 :
8193 407870 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8194 : {
8195 : tree fld;
8196 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8197 : {
8198 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8199 44565 : continue;
8200 2083 : if (fld == field)
8201 : break;
8202 528 : if (DECL_PADDING_P (fld))
8203 87 : continue;
8204 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8205 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8206 : return 0;
8207 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8208 382 : continue;
8209 : break;
8210 : }
8211 2509 : if (fld == NULL_TREE)
8212 : {
8213 895 : if (ce == NULL)
8214 : break;
8215 : return 0;
8216 : }
8217 1614 : fld_base = DECL_CHAIN (fld);
8218 1614 : if (fld != field)
8219 : {
8220 59 : cnt--;
8221 59 : field = fld;
8222 59 : pos = int_byte_position (field);
8223 59 : val = build_zero_cst (TREE_TYPE (fld));
8224 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8225 0 : to_free = val;
8226 : }
8227 : }
8228 :
8229 393904 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8230 15035 : && TYPE_DOMAIN (TREE_TYPE (field))
8231 408939 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8232 : {
8233 81 : if (mask || off != -1)
8234 : return 0;
8235 81 : if (val == NULL_TREE)
8236 0 : continue;
8237 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8238 : return 0;
8239 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8240 81 : if (fieldsize < 0
8241 81 : || (int) fieldsize != fieldsize
8242 81 : || (pos + fieldsize) > INT_MAX)
8243 : return 0;
8244 81 : if (pos + fieldsize > total_bytes)
8245 : {
8246 81 : if (ptr != NULL && total_bytes < len)
8247 81 : memset (ptr + total_bytes, '\0',
8248 81 : MIN (pos + fieldsize, len) - total_bytes);
8249 : total_bytes = pos + fieldsize;
8250 : }
8251 : }
8252 : else
8253 : {
8254 393823 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8255 393823 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8256 : return 0;
8257 393823 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8258 : }
8259 393904 : if (fieldsize == 0)
8260 1 : continue;
8261 :
8262 : /* Prepare to deal with integral bit-fields and filter out other
8263 : bit-fields that do not start and end on a byte boundary. */
8264 393903 : if (DECL_BIT_FIELD (field))
8265 : {
8266 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8267 : return 0;
8268 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8269 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8270 : {
8271 2711 : bpos %= BITS_PER_UNIT;
8272 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8273 2711 : epos = fieldsize % BITS_PER_UNIT;
8274 2711 : fieldsize += BITS_PER_UNIT - 1;
8275 2711 : fieldsize /= BITS_PER_UNIT;
8276 : }
8277 0 : else if (bpos % BITS_PER_UNIT
8278 0 : || DECL_SIZE (field) == NULL_TREE
8279 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8280 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8281 : return 0;
8282 : }
8283 :
8284 393903 : if (off != -1 && pos + fieldsize <= off)
8285 3195 : continue;
8286 :
8287 390708 : if (val == NULL_TREE)
8288 0 : continue;
8289 :
8290 390708 : if (DECL_BIT_FIELD (field)
8291 390708 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8292 : {
8293 : /* FIXME: Handle PDP endian. */
8294 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8295 261 : return 0;
8296 :
8297 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8298 6 : val = TREE_OPERAND (val, 0);
8299 2507 : if (TREE_CODE (val) != INTEGER_CST)
8300 : return 0;
8301 :
8302 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8303 2507 : tree repr_type = NULL_TREE;
8304 2507 : HOST_WIDE_INT rpos = 0;
8305 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8306 : {
8307 1978 : rpos = int_byte_position (repr);
8308 1978 : repr_type = TREE_TYPE (repr);
8309 : }
8310 : else
8311 : {
8312 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8313 529 : if (repr_type == NULL_TREE)
8314 : return 0;
8315 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8316 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8317 268 : if (pos + repr_size <= o + len)
8318 : rpos = pos;
8319 : else
8320 : {
8321 14 : rpos = o + len - repr_size;
8322 14 : gcc_assert (rpos <= pos);
8323 : }
8324 : }
8325 :
8326 2246 : if (rpos > pos)
8327 : return 0;
8328 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8329 2246 : int diff = (TYPE_PRECISION (repr_type)
8330 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8331 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8332 2246 : if (!BYTES_BIG_ENDIAN)
8333 2246 : w = wi::lshift (w, bitoff);
8334 : else
8335 : w = wi::lshift (w, diff - bitoff);
8336 2246 : val = wide_int_to_tree (repr_type, w);
8337 :
8338 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8339 : / BITS_PER_UNIT + 1];
8340 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8341 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8342 0 : return 0;
8343 :
8344 2246 : if (ptr == NULL)
8345 0 : continue;
8346 :
8347 : /* If the bitfield does not start at byte boundary, handle
8348 : the partial byte at the start. */
8349 2246 : if (bpos
8350 1351 : && (off == -1 || (pos >= off && len >= 1)))
8351 : {
8352 1276 : if (!BYTES_BIG_ENDIAN)
8353 : {
8354 1276 : int msk = (1 << bpos) - 1;
8355 1276 : buf[pos - rpos] &= ~msk;
8356 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8357 1276 : if (mask)
8358 : {
8359 147 : if (fieldsize > 1 || epos == 0)
8360 129 : mask[pos] &= msk;
8361 : else
8362 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8363 : }
8364 : }
8365 : else
8366 : {
8367 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8368 : buf[pos - rpos] &= msk;
8369 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8370 : if (mask)
8371 : {
8372 : if (fieldsize > 1 || epos == 0)
8373 : mask[pos] &= ~msk;
8374 : else
8375 : mask[pos] &= (~msk
8376 : | ((1 << (BITS_PER_UNIT - epos))
8377 : - 1));
8378 : }
8379 : }
8380 : }
8381 : /* If the bitfield does not end at byte boundary, handle
8382 : the partial byte at the end. */
8383 2246 : if (epos
8384 1724 : && (off == -1
8385 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8386 : {
8387 1621 : if (!BYTES_BIG_ENDIAN)
8388 : {
8389 1621 : int msk = (1 << epos) - 1;
8390 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8391 1621 : buf[pos - rpos + fieldsize - 1]
8392 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8393 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8394 156 : mask[pos + fieldsize - 1] &= ~msk;
8395 : }
8396 : else
8397 : {
8398 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8399 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8400 : buf[pos - rpos + fieldsize - 1]
8401 : |= ptr[pos + fieldsize - 1 - o] & msk;
8402 : if (mask && (fieldsize > 1 || bpos == 0))
8403 : mask[pos + fieldsize - 1] &= msk;
8404 : }
8405 : }
8406 2246 : if (off == -1
8407 1301 : || (pos >= off
8408 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8409 : {
8410 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8411 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8412 75 : memset (mask + pos + (bpos != 0), 0,
8413 75 : fieldsize - (bpos != 0) - (epos != 0));
8414 : }
8415 : else
8416 : {
8417 : /* Partial overlap. */
8418 191 : HOST_WIDE_INT fsz = fieldsize;
8419 191 : gcc_assert (mask == NULL);
8420 191 : if (pos < off)
8421 : {
8422 89 : fsz -= (off - pos);
8423 89 : pos = off;
8424 : }
8425 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8426 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8427 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8428 : }
8429 2246 : continue;
8430 2246 : }
8431 :
8432 388201 : if (off == -1
8433 28604 : || (pos >= off
8434 27774 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8435 : {
8436 379832 : int fldsize = fieldsize;
8437 20235 : if (off == -1)
8438 : {
8439 359597 : tree fld = DECL_CHAIN (field);
8440 5701376 : while (fld)
8441 : {
8442 5360376 : if (TREE_CODE (fld) == FIELD_DECL)
8443 : break;
8444 5341779 : fld = DECL_CHAIN (fld);
8445 : }
8446 359597 : if (fld == NULL_TREE)
8447 341000 : fldsize = len - pos;
8448 : }
8449 411925 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8450 : : NULL,
8451 : fldsize,
8452 : off == -1 ? -1 : 0,
8453 11858 : mask ? mask + pos : NULL);
8454 379832 : if (!r)
8455 : return 0;
8456 360225 : if (off == -1
8457 347230 : && fldsize != fieldsize
8458 1149 : && r > fieldsize
8459 822 : && pos + r > total_bytes)
8460 368184 : total_bytes = pos + r;
8461 : }
8462 : else
8463 : {
8464 : /* Partial overlap. */
8465 8369 : unsigned char *p = NULL;
8466 8369 : int no = 0;
8467 8369 : int l;
8468 8369 : gcc_assert (mask == NULL);
8469 8369 : if (pos >= off)
8470 : {
8471 7539 : if (ptr)
8472 7539 : p = ptr + pos - off;
8473 7539 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8474 : fieldsize);
8475 : }
8476 : else
8477 : {
8478 830 : p = ptr;
8479 830 : no = off - pos;
8480 830 : l = len;
8481 : }
8482 8369 : if (!native_encode_initializer (val, p, l, no, NULL))
8483 : return 0;
8484 : }
8485 368184 : }
8486 1846325 : return MIN (total_bytes - off, len);
8487 : }
8488 : return 0;
8489 : }
8490 : }
8491 :
8492 :
8493 : /* Subroutine of native_interpret_expr. Interpret the contents of
8494 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8495 : If the buffer cannot be interpreted, return NULL_TREE. */
8496 :
8497 : static tree
8498 2866605 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8499 : {
8500 2866605 : int total_bytes;
8501 2866605 : if (BITINT_TYPE_P (type))
8502 : {
8503 31 : struct bitint_info info;
8504 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8505 31 : gcc_assert (ok);
8506 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8507 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8508 : {
8509 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8510 : /* More work is needed when adding _BitInt support to PDP endian
8511 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8512 : match target endianity here. */
8513 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8514 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8515 : || (GET_MODE_SIZE (limb_mode)
8516 : >= UNITS_PER_WORD)));
8517 : }
8518 : else
8519 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8520 : }
8521 : else
8522 5733148 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8523 :
8524 2866605 : if (total_bytes > len)
8525 : return NULL_TREE;
8526 :
8527 2866363 : wide_int result = wi::from_buffer (ptr, total_bytes);
8528 :
8529 2866363 : return wide_int_to_tree (type, result);
8530 2866363 : }
8531 :
8532 :
8533 : /* Subroutine of native_interpret_expr. Interpret the contents of
8534 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8535 : If the buffer cannot be interpreted, return NULL_TREE. */
8536 :
8537 : static tree
8538 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8539 : {
8540 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8541 0 : int total_bytes = GET_MODE_SIZE (mode);
8542 0 : double_int result;
8543 0 : FIXED_VALUE_TYPE fixed_value;
8544 :
8545 0 : if (total_bytes > len
8546 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8547 : return NULL_TREE;
8548 :
8549 0 : result = double_int::from_buffer (ptr, total_bytes);
8550 0 : fixed_value = fixed_from_double_int (result, mode);
8551 :
8552 0 : return build_fixed (type, fixed_value);
8553 : }
8554 :
8555 :
8556 : /* Subroutine of native_interpret_expr. Interpret the contents of
8557 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8558 : If the buffer cannot be interpreted, return NULL_TREE. */
8559 :
8560 : tree
8561 36968 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8562 : {
8563 36968 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8564 36968 : int total_bytes = GET_MODE_SIZE (mode);
8565 36968 : unsigned char value;
8566 : /* There are always 32 bits in each long, no matter the size of
8567 : the hosts long. We handle floating point representations with
8568 : up to 192 bits. */
8569 36968 : REAL_VALUE_TYPE r;
8570 36968 : long tmp[6];
8571 :
8572 36968 : if (total_bytes > len || total_bytes > 24)
8573 : return NULL_TREE;
8574 36907 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8575 :
8576 36907 : memset (tmp, 0, sizeof (tmp));
8577 270883 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8578 233976 : bitpos += BITS_PER_UNIT)
8579 : {
8580 : /* Both OFFSET and BYTE index within a long;
8581 : bitpos indexes the whole float. */
8582 233976 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8583 233976 : if (UNITS_PER_WORD < 4)
8584 : {
8585 : int word = byte / UNITS_PER_WORD;
8586 : if (WORDS_BIG_ENDIAN)
8587 : word = (words - 1) - word;
8588 : offset = word * UNITS_PER_WORD;
8589 : if (BYTES_BIG_ENDIAN)
8590 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8591 : else
8592 : offset += byte % UNITS_PER_WORD;
8593 : }
8594 : else
8595 : {
8596 233976 : offset = byte;
8597 233976 : if (BYTES_BIG_ENDIAN)
8598 : {
8599 : /* Reverse bytes within each long, or within the entire float
8600 : if it's smaller than a long (for HFmode). */
8601 : offset = MIN (3, total_bytes - 1) - offset;
8602 : gcc_assert (offset >= 0);
8603 : }
8604 : }
8605 233976 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8606 :
8607 233976 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8608 : }
8609 :
8610 36907 : real_from_target (&r, tmp, mode);
8611 36907 : return build_real (type, r);
8612 : }
8613 :
8614 :
8615 : /* Subroutine of native_interpret_expr. Interpret the contents of
8616 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8617 : If the buffer cannot be interpreted, return NULL_TREE. */
8618 :
8619 : static tree
8620 1596 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8621 : {
8622 1596 : tree etype, rpart, ipart;
8623 1596 : int size;
8624 :
8625 1596 : etype = TREE_TYPE (type);
8626 1596 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8627 1596 : if (size * 2 > len)
8628 : return NULL_TREE;
8629 1563 : rpart = native_interpret_expr (etype, ptr, size);
8630 1563 : if (!rpart)
8631 : return NULL_TREE;
8632 1382 : ipart = native_interpret_expr (etype, ptr+size, size);
8633 1382 : if (!ipart)
8634 : return NULL_TREE;
8635 1382 : return build_complex (type, rpart, ipart);
8636 : }
8637 :
8638 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8639 : which contains LEN bytes. The vector is known to be encodable using
8640 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8641 :
8642 : Return the vector on success, otherwise return null. */
8643 :
8644 : static tree
8645 236866 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8646 : unsigned int len, unsigned int npatterns,
8647 : unsigned int nelts_per_pattern)
8648 : {
8649 236866 : tree elt_type = TREE_TYPE (type);
8650 236866 : if (VECTOR_BOOLEAN_TYPE_P (type)
8651 236870 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8652 : {
8653 : /* This is the only case in which elements can be smaller than a byte.
8654 : Element 0 is always in the lsb of the containing byte. */
8655 2 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8656 2 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8657 : return NULL_TREE;
8658 :
8659 2 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8660 20 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8661 : {
8662 18 : unsigned int bit_index = i * elt_bits;
8663 18 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8664 18 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8665 36 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8666 20 : ? build_all_ones_cst (elt_type)
8667 2 : : build_zero_cst (elt_type));
8668 : }
8669 2 : return builder.build ();
8670 2 : }
8671 :
8672 236864 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8673 236864 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8674 : return NULL_TREE;
8675 :
8676 236864 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8677 942936 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8678 : {
8679 706206 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8680 706206 : if (!elt)
8681 134 : return NULL_TREE;
8682 706072 : builder.quick_push (elt);
8683 706072 : bytes += elt_bytes;
8684 : }
8685 236730 : return builder.build ();
8686 236864 : }
8687 :
8688 : /* Subroutine of native_interpret_expr. Interpret the contents of
8689 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8690 : If the buffer cannot be interpreted, return NULL_TREE. */
8691 :
8692 : static tree
8693 82228 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8694 : {
8695 82228 : unsigned HOST_WIDE_INT size;
8696 :
8697 82228 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8698 82228 : || size > len)
8699 2 : return NULL_TREE;
8700 :
8701 82226 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8702 82226 : return native_interpret_vector_part (type, ptr, len, count, 1);
8703 : }
8704 :
8705 :
8706 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8707 : the buffer PTR of length LEN as a constant of type TYPE. For
8708 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8709 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8710 : return NULL_TREE. */
8711 :
8712 : tree
8713 3144171 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8714 : {
8715 3144171 : switch (TREE_CODE (type))
8716 : {
8717 2866605 : case INTEGER_TYPE:
8718 2866605 : case ENUMERAL_TYPE:
8719 2866605 : case BOOLEAN_TYPE:
8720 2866605 : case POINTER_TYPE:
8721 2866605 : case REFERENCE_TYPE:
8722 2866605 : case OFFSET_TYPE:
8723 2866605 : case BITINT_TYPE:
8724 2866605 : return native_interpret_int (type, ptr, len);
8725 :
8726 35381 : case REAL_TYPE:
8727 35381 : if (tree ret = native_interpret_real (type, ptr, len))
8728 : {
8729 : /* For floating point values in composite modes, punt if this
8730 : folding doesn't preserve bit representation. As the mode doesn't
8731 : have fixed precision while GCC pretends it does, there could be
8732 : valid values that GCC can't really represent accurately.
8733 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8734 : bit combinationations which GCC doesn't preserve. */
8735 35320 : unsigned char buf[24 * 2];
8736 35320 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8737 35320 : int total_bytes = GET_MODE_SIZE (mode);
8738 35320 : memcpy (buf + 24, ptr, total_bytes);
8739 35320 : clear_type_padding_in_mask (type, buf + 24);
8740 35320 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8741 35320 : || memcmp (buf + 24, buf, total_bytes) != 0)
8742 488 : return NULL_TREE;
8743 : return ret;
8744 : }
8745 : return NULL_TREE;
8746 :
8747 0 : case FIXED_POINT_TYPE:
8748 0 : return native_interpret_fixed (type, ptr, len);
8749 :
8750 1596 : case COMPLEX_TYPE:
8751 1596 : return native_interpret_complex (type, ptr, len);
8752 :
8753 82228 : case VECTOR_TYPE:
8754 82228 : return native_interpret_vector (type, ptr, len);
8755 :
8756 : default:
8757 : return NULL_TREE;
8758 : }
8759 : }
8760 :
8761 : /* Returns true if we can interpret the contents of a native encoding
8762 : as TYPE. */
8763 :
8764 : bool
8765 326695 : can_native_interpret_type_p (tree type)
8766 : {
8767 326695 : switch (TREE_CODE (type))
8768 : {
8769 : case INTEGER_TYPE:
8770 : case ENUMERAL_TYPE:
8771 : case BOOLEAN_TYPE:
8772 : case POINTER_TYPE:
8773 : case REFERENCE_TYPE:
8774 : case FIXED_POINT_TYPE:
8775 : case REAL_TYPE:
8776 : case COMPLEX_TYPE:
8777 : case VECTOR_TYPE:
8778 : case OFFSET_TYPE:
8779 : return true;
8780 38306 : default:
8781 38306 : return false;
8782 : }
8783 : }
8784 :
8785 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8786 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8787 :
8788 : tree
8789 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8790 : int len)
8791 : {
8792 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8793 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8794 : {
8795 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8796 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8797 : return NULL_TREE;
8798 :
8799 197 : HOST_WIDE_INT cnt = 0;
8800 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8801 : {
8802 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8803 : return NULL_TREE;
8804 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8805 : }
8806 197 : if (eltsz == 0)
8807 0 : cnt = 0;
8808 197 : HOST_WIDE_INT pos = 0;
8809 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8810 : {
8811 439 : tree v = NULL_TREE;
8812 439 : if (pos >= len || pos + eltsz > len)
8813 9631 : return NULL_TREE;
8814 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8815 : {
8816 367 : v = native_interpret_expr (TREE_TYPE (type),
8817 367 : ptr + off + pos, eltsz);
8818 367 : if (v == NULL_TREE)
8819 : return NULL_TREE;
8820 : }
8821 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8822 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8823 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8824 : eltsz);
8825 72 : if (v == NULL_TREE)
8826 0 : return NULL_TREE;
8827 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8828 : }
8829 197 : return build_constructor (type, elts);
8830 : }
8831 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8832 : return NULL_TREE;
8833 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8834 : {
8835 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8836 796932 : || is_empty_type (TREE_TYPE (field)))
8837 761072 : continue;
8838 13494 : tree fld = field;
8839 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8840 13494 : int diff = 0;
8841 13494 : tree v = NULL_TREE;
8842 13494 : if (DECL_BIT_FIELD (field))
8843 : {
8844 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8845 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8846 : {
8847 168 : poly_int64 bitoffset;
8848 168 : poly_uint64 field_offset, fld_offset;
8849 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8850 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8851 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8852 : else
8853 : bitoffset = 0;
8854 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8855 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8856 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8857 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8858 168 : if (!bitoffset.is_constant (&bitoff)
8859 168 : || bitoff < 0
8860 168 : || bitoff > diff)
8861 0 : return NULL_TREE;
8862 : }
8863 : else
8864 : {
8865 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8866 : return NULL_TREE;
8867 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8868 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8869 12 : bpos %= BITS_PER_UNIT;
8870 12 : fieldsize += bpos;
8871 12 : fieldsize += BITS_PER_UNIT - 1;
8872 12 : fieldsize /= BITS_PER_UNIT;
8873 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8874 12 : if (repr_type == NULL_TREE)
8875 : return NULL_TREE;
8876 12 : sz = int_size_in_bytes (repr_type);
8877 12 : if (sz < 0 || sz > len)
8878 : return NULL_TREE;
8879 12 : pos = int_byte_position (field);
8880 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8881 : return NULL_TREE;
8882 12 : HOST_WIDE_INT rpos;
8883 12 : if (pos + sz <= len)
8884 : rpos = pos;
8885 : else
8886 : {
8887 0 : rpos = len - sz;
8888 0 : gcc_assert (rpos <= pos);
8889 : }
8890 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8891 12 : pos = rpos;
8892 12 : diff = (TYPE_PRECISION (repr_type)
8893 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8894 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8895 12 : if (v == NULL_TREE)
8896 : return NULL_TREE;
8897 : fld = NULL_TREE;
8898 : }
8899 : }
8900 :
8901 168 : if (fld)
8902 : {
8903 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
8904 13482 : if (sz < 0 || sz > len)
8905 : return NULL_TREE;
8906 13482 : tree byte_pos = byte_position (fld);
8907 13482 : if (!tree_fits_shwi_p (byte_pos))
8908 : return NULL_TREE;
8909 13482 : pos = tree_to_shwi (byte_pos);
8910 13482 : if (pos < 0 || pos > len || pos + sz > len)
8911 : return NULL_TREE;
8912 : }
8913 13482 : if (fld == NULL_TREE)
8914 : /* Already handled above. */;
8915 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8916 : {
8917 6262 : v = native_interpret_expr (TREE_TYPE (fld),
8918 6262 : ptr + off + pos, sz);
8919 6262 : if (v == NULL_TREE)
8920 : return NULL_TREE;
8921 : }
8922 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8923 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8924 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8925 7232 : if (v == NULL_TREE)
8926 : return NULL_TREE;
8927 13494 : if (fld != field)
8928 : {
8929 180 : if (TREE_CODE (v) != INTEGER_CST)
8930 : return NULL_TREE;
8931 :
8932 : /* FIXME: Figure out how to handle PDP endian bitfields. */
8933 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8934 : return NULL_TREE;
8935 180 : if (!BYTES_BIG_ENDIAN)
8936 180 : v = wide_int_to_tree (TREE_TYPE (field),
8937 360 : wi::lrshift (wi::to_wide (v), bitoff));
8938 : else
8939 : v = wide_int_to_tree (TREE_TYPE (field),
8940 : wi::lrshift (wi::to_wide (v),
8941 : diff - bitoff));
8942 : }
8943 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8944 : }
8945 9434 : return build_constructor (type, elts);
8946 : }
8947 :
8948 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8949 : or extracted constant positions and/or sizes aren't byte aligned. */
8950 :
8951 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8952 : bits between adjacent elements. AMNT should be within
8953 : [0, BITS_PER_UNIT).
8954 : Example, AMNT = 2:
8955 : 00011111|11100000 << 2 = 01111111|10000000
8956 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8957 :
8958 : void
8959 29247 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8960 : unsigned int amnt)
8961 : {
8962 29247 : if (amnt == 0)
8963 : return;
8964 :
8965 16972 : unsigned char carry_over = 0U;
8966 16972 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8967 16972 : unsigned char clear_mask = (~0U) << amnt;
8968 :
8969 100016 : for (unsigned int i = 0; i < sz; i++)
8970 : {
8971 83044 : unsigned prev_carry_over = carry_over;
8972 83044 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8973 :
8974 83044 : ptr[i] <<= amnt;
8975 83044 : if (i != 0)
8976 : {
8977 66072 : ptr[i] &= clear_mask;
8978 66072 : ptr[i] |= prev_carry_over;
8979 : }
8980 : }
8981 : }
8982 :
8983 : /* Like shift_bytes_in_array_left but for big-endian.
8984 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
8985 : bits between adjacent elements. AMNT should be within
8986 : [0, BITS_PER_UNIT).
8987 : Example, AMNT = 2:
8988 : 00011111|11100000 >> 2 = 00000111|11111000
8989 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
8990 :
8991 : void
8992 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
8993 : unsigned int amnt)
8994 : {
8995 8 : if (amnt == 0)
8996 : return;
8997 :
8998 4 : unsigned char carry_over = 0U;
8999 4 : unsigned char carry_mask = ~(~0U << amnt);
9000 :
9001 12 : for (unsigned int i = 0; i < sz; i++)
9002 : {
9003 8 : unsigned prev_carry_over = carry_over;
9004 8 : carry_over = ptr[i] & carry_mask;
9005 :
9006 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9007 8 : ptr[i] >>= amnt;
9008 8 : ptr[i] |= prev_carry_over;
9009 : }
9010 : }
9011 :
9012 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9013 : directly on the VECTOR_CST encoding, in a way that works for variable-
9014 : length vectors. Return the resulting VECTOR_CST on success or null
9015 : on failure. */
9016 :
9017 : static tree
9018 163542 : fold_view_convert_vector_encoding (tree type, tree expr)
9019 : {
9020 163542 : tree expr_type = TREE_TYPE (expr);
9021 163542 : poly_uint64 type_bits, expr_bits;
9022 163542 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9023 163542 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9024 0 : return NULL_TREE;
9025 :
9026 163542 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9027 163542 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9028 163542 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9029 163542 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9030 :
9031 : /* We can only preserve the semantics of a stepped pattern if the new
9032 : vector element is an integer of the same size. */
9033 163542 : if (VECTOR_CST_STEPPED_P (expr)
9034 163542 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9035 : return NULL_TREE;
9036 :
9037 : /* The number of bits needed to encode one element from every pattern
9038 : of the original vector. */
9039 154640 : unsigned int expr_sequence_bits
9040 154640 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9041 :
9042 : /* The number of bits needed to encode one element from every pattern
9043 : of the result. */
9044 154640 : unsigned int type_sequence_bits
9045 154640 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9046 :
9047 : /* Don't try to read more bytes than are available, which can happen
9048 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9049 : The general VIEW_CONVERT handling can cope with that case, so there's
9050 : no point complicating things here. */
9051 154640 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9052 154640 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9053 : BITS_PER_UNIT);
9054 154640 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9055 154640 : if (known_gt (buffer_bits, expr_bits))
9056 : return NULL_TREE;
9057 :
9058 : /* Get enough bytes of EXPR to form the new encoding. */
9059 154640 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9060 154640 : buffer.quick_grow (buffer_bytes);
9061 154640 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9062 154640 : buffer_bits / expr_elt_bits)
9063 : != (int) buffer_bytes)
9064 : return NULL_TREE;
9065 :
9066 : /* Re-encode the bytes as TYPE. */
9067 154640 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9068 309280 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9069 154640 : type_npatterns, nelts_per_pattern);
9070 154640 : }
9071 :
9072 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9073 : TYPE at compile-time. If we're unable to perform the conversion
9074 : return NULL_TREE. */
9075 :
9076 : static tree
9077 13041159 : fold_view_convert_expr (tree type, tree expr)
9078 : {
9079 13041159 : unsigned char buffer[128];
9080 13041159 : unsigned char *buf;
9081 13041159 : int len;
9082 13041159 : HOST_WIDE_INT l;
9083 :
9084 : /* Check that the host and target are sane. */
9085 13041159 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9086 : return NULL_TREE;
9087 :
9088 13041159 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9089 163542 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9090 : return res;
9091 :
9092 12886586 : l = int_size_in_bytes (type);
9093 12886586 : if (l > (int) sizeof (buffer)
9094 12886586 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9095 : {
9096 0 : buf = XALLOCAVEC (unsigned char, l);
9097 0 : len = l;
9098 : }
9099 : else
9100 : {
9101 : buf = buffer;
9102 : len = sizeof (buffer);
9103 : }
9104 12886586 : len = native_encode_expr (expr, buf, len);
9105 12886586 : if (len == 0)
9106 : return NULL_TREE;
9107 :
9108 1861302 : return native_interpret_expr (type, buf, len);
9109 : }
9110 :
9111 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9112 : to avoid confusing the gimplify process. */
9113 :
9114 : tree
9115 574127258 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9116 : {
9117 : /* The size of the object is not relevant when talking about its address. */
9118 574127258 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9119 0 : t = TREE_OPERAND (t, 0);
9120 :
9121 574127258 : if (INDIRECT_REF_P (t))
9122 : {
9123 61567298 : t = TREE_OPERAND (t, 0);
9124 :
9125 61567298 : if (TREE_TYPE (t) != ptrtype)
9126 39424133 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9127 : }
9128 512559960 : else if (TREE_CODE (t) == MEM_REF
9129 512559960 : && integer_zerop (TREE_OPERAND (t, 1)))
9130 : {
9131 1657356 : t = TREE_OPERAND (t, 0);
9132 :
9133 1657356 : if (TREE_TYPE (t) != ptrtype)
9134 1087382 : t = fold_convert_loc (loc, ptrtype, t);
9135 : }
9136 510902604 : else if (TREE_CODE (t) == MEM_REF
9137 510902604 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9138 663 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9139 : TREE_OPERAND (t, 0),
9140 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9141 510901941 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9142 : {
9143 30412787 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9144 :
9145 30412787 : if (TREE_TYPE (t) != ptrtype)
9146 16330 : t = fold_convert_loc (loc, ptrtype, t);
9147 : }
9148 : else
9149 480489154 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9150 :
9151 : return t;
9152 : }
9153 :
9154 : /* Build an expression for the address of T. */
9155 :
9156 : tree
9157 499674064 : build_fold_addr_expr_loc (location_t loc, tree t)
9158 : {
9159 499674064 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9160 :
9161 499674064 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9162 : }
9163 :
9164 : /* Fold a unary expression of code CODE and type TYPE with operand
9165 : OP0. Return the folded expression if folding is successful.
9166 : Otherwise, return NULL_TREE. */
9167 :
9168 : tree
9169 2033897015 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9170 : {
9171 2033897015 : tree tem;
9172 2033897015 : tree arg0;
9173 2033897015 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9174 :
9175 2033897015 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9176 : && TREE_CODE_LENGTH (code) == 1);
9177 :
9178 2033897015 : arg0 = op0;
9179 2033897015 : if (arg0)
9180 : {
9181 2033883763 : if (CONVERT_EXPR_CODE_P (code)
9182 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9183 : {
9184 : /* Don't use STRIP_NOPS, because signedness of argument type
9185 : matters. */
9186 1154602804 : STRIP_SIGN_NOPS (arg0);
9187 : }
9188 : else
9189 : {
9190 : /* Strip any conversions that don't change the mode. This
9191 : is safe for every expression, except for a comparison
9192 : expression because its signedness is derived from its
9193 : operands.
9194 :
9195 : Note that this is done as an internal manipulation within
9196 : the constant folder, in order to find the simplest
9197 : representation of the arguments so that their form can be
9198 : studied. In any cases, the appropriate type conversions
9199 : should be put back in the tree that will get out of the
9200 : constant folder. */
9201 879280959 : STRIP_NOPS (arg0);
9202 : }
9203 :
9204 2033883763 : if (CONSTANT_CLASS_P (arg0))
9205 : {
9206 322971352 : tree tem = const_unop (code, type, arg0);
9207 322971352 : if (tem)
9208 : {
9209 283292610 : if (TREE_TYPE (tem) != type)
9210 9790 : tem = fold_convert_loc (loc, type, tem);
9211 283292610 : return tem;
9212 : }
9213 : }
9214 : }
9215 :
9216 1750604405 : tem = generic_simplify (loc, code, type, op0);
9217 1750604405 : if (tem)
9218 : return tem;
9219 :
9220 1317537997 : if (TREE_CODE_CLASS (code) == tcc_unary)
9221 : {
9222 747009460 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9223 1074226 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9224 : fold_build1_loc (loc, code, type,
9225 1074226 : fold_convert_loc (loc, TREE_TYPE (op0),
9226 2148452 : TREE_OPERAND (arg0, 1))));
9227 745935234 : else if (TREE_CODE (arg0) == COND_EXPR)
9228 : {
9229 547586 : tree arg01 = TREE_OPERAND (arg0, 1);
9230 547586 : tree arg02 = TREE_OPERAND (arg0, 2);
9231 547586 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9232 543404 : arg01 = fold_build1_loc (loc, code, type,
9233 : fold_convert_loc (loc,
9234 543404 : TREE_TYPE (op0), arg01));
9235 547586 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9236 537313 : arg02 = fold_build1_loc (loc, code, type,
9237 : fold_convert_loc (loc,
9238 537313 : TREE_TYPE (op0), arg02));
9239 547586 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9240 : arg01, arg02);
9241 :
9242 : /* If this was a conversion, and all we did was to move into
9243 : inside the COND_EXPR, bring it back out. But leave it if
9244 : it is a conversion from integer to integer and the
9245 : result precision is no wider than a word since such a
9246 : conversion is cheap and may be optimized away by combine,
9247 : while it couldn't if it were outside the COND_EXPR. Then return
9248 : so we don't get into an infinite recursion loop taking the
9249 : conversion out and then back in. */
9250 :
9251 547586 : if ((CONVERT_EXPR_CODE_P (code)
9252 10246 : || code == NON_LVALUE_EXPR)
9253 537359 : && TREE_CODE (tem) == COND_EXPR
9254 517825 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9255 454777 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9256 289270 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9257 289058 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9258 289058 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9259 289058 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9260 857420 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9261 21606 : && (INTEGRAL_TYPE_P
9262 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9263 21566 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9264 21453 : || flag_syntax_only))
9265 266815 : tem = build1_loc (loc, code, type,
9266 : build3 (COND_EXPR,
9267 266815 : TREE_TYPE (TREE_OPERAND
9268 : (TREE_OPERAND (tem, 1), 0)),
9269 266815 : TREE_OPERAND (tem, 0),
9270 266815 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9271 266815 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9272 : 0)));
9273 547586 : return tem;
9274 : }
9275 : }
9276 :
9277 1315916185 : switch (code)
9278 : {
9279 42372457 : case NON_LVALUE_EXPR:
9280 42372457 : if (!maybe_lvalue_p (op0))
9281 30897361 : return fold_convert_loc (loc, type, op0);
9282 : return NULL_TREE;
9283 :
9284 692880710 : CASE_CONVERT:
9285 692880710 : case FLOAT_EXPR:
9286 692880710 : case FIX_TRUNC_EXPR:
9287 692880710 : if (COMPARISON_CLASS_P (op0))
9288 : {
9289 : /* If we have (type) (a CMP b) and type is an integral type, return
9290 : new expression involving the new type. Canonicalize
9291 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9292 : non-integral type.
9293 : Do not fold the result as that would not simplify further, also
9294 : folding again results in recursions. */
9295 733961 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9296 164195 : return build2_loc (loc, TREE_CODE (op0), type,
9297 164195 : TREE_OPERAND (op0, 0),
9298 328390 : TREE_OPERAND (op0, 1));
9299 569766 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9300 7652 : && TREE_CODE (type) != VECTOR_TYPE)
9301 7652 : return build3_loc (loc, COND_EXPR, type, op0,
9302 : constant_boolean_node (true, type),
9303 7652 : constant_boolean_node (false, type));
9304 : }
9305 :
9306 : /* Handle (T *)&A.B.C for A being of type T and B and C
9307 : living at offset zero. This occurs frequently in
9308 : C++ upcasting and then accessing the base. */
9309 692708863 : if (TREE_CODE (op0) == ADDR_EXPR
9310 235689820 : && POINTER_TYPE_P (type)
9311 921524978 : && handled_component_p (TREE_OPERAND (op0, 0)))
9312 : {
9313 49032871 : poly_int64 bitsize, bitpos;
9314 49032871 : tree offset;
9315 49032871 : machine_mode mode;
9316 49032871 : int unsignedp, reversep, volatilep;
9317 49032871 : tree base
9318 49032871 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9319 : &offset, &mode, &unsignedp, &reversep,
9320 : &volatilep);
9321 : /* If the reference was to a (constant) zero offset, we can use
9322 : the address of the base if it has the same base type
9323 : as the result type and the pointer type is unqualified. */
9324 49032871 : if (!offset
9325 48887918 : && known_eq (bitpos, 0)
9326 33429537 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9327 33429537 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9328 49045549 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9329 12477 : return fold_convert_loc (loc, type,
9330 12477 : build_fold_addr_expr_loc (loc, base));
9331 : }
9332 :
9333 692696386 : if (TREE_CODE (op0) == MODIFY_EXPR
9334 289922 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9335 : /* Detect assigning a bitfield. */
9336 692698389 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9337 118 : && DECL_BIT_FIELD
9338 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9339 : {
9340 : /* Don't leave an assignment inside a conversion
9341 : unless assigning a bitfield. */
9342 1955 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9343 : /* First do the assignment, then return converted constant. */
9344 1955 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9345 1955 : suppress_warning (tem /* What warning? */);
9346 1955 : TREE_USED (tem) = 1;
9347 1955 : return tem;
9348 : }
9349 :
9350 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9351 : constants (if x has signed type, the sign bit cannot be set
9352 : in c). This folds extension into the BIT_AND_EXPR.
9353 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9354 : very likely don't have maximal range for their precision and this
9355 : transformation effectively doesn't preserve non-maximal ranges. */
9356 692694431 : if (TREE_CODE (type) == INTEGER_TYPE
9357 245118910 : && TREE_CODE (op0) == BIT_AND_EXPR
9358 693300715 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9359 : {
9360 215097 : tree and_expr = op0;
9361 215097 : tree and0 = TREE_OPERAND (and_expr, 0);
9362 215097 : tree and1 = TREE_OPERAND (and_expr, 1);
9363 215097 : int change = 0;
9364 :
9365 215097 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9366 215097 : || (TYPE_PRECISION (type)
9367 58388 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9368 : change = 1;
9369 17620 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9370 : <= HOST_BITS_PER_WIDE_INT
9371 17620 : && tree_fits_uhwi_p (and1))
9372 : {
9373 16506 : unsigned HOST_WIDE_INT cst;
9374 :
9375 16506 : cst = tree_to_uhwi (and1);
9376 33012 : cst &= HOST_WIDE_INT_M1U
9377 16506 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9378 16506 : change = (cst == 0);
9379 16506 : if (change
9380 16506 : && !flag_syntax_only
9381 33012 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9382 : == ZERO_EXTEND))
9383 : {
9384 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9385 : and0 = fold_convert_loc (loc, uns, and0);
9386 : and1 = fold_convert_loc (loc, uns, and1);
9387 : }
9388 : }
9389 16506 : if (change)
9390 : {
9391 213983 : tree and1_type = TREE_TYPE (and1);
9392 213983 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9393 : TYPE_PRECISION (type));
9394 213983 : tem = force_fit_type (type,
9395 213983 : wide_int::from (wi::to_wide (and1), prec,
9396 213983 : TYPE_SIGN (and1_type)),
9397 213983 : 0, TREE_OVERFLOW (and1));
9398 213983 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9399 213983 : fold_convert_loc (loc, type, and0), tem);
9400 : }
9401 : }
9402 :
9403 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9404 : cast (T1)X will fold away. We assume that this happens when X itself
9405 : is a cast. */
9406 692480448 : if (POINTER_TYPE_P (type)
9407 412721871 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9408 724331484 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9409 : {
9410 29303231 : tree arg00 = TREE_OPERAND (arg0, 0);
9411 29303231 : tree arg01 = TREE_OPERAND (arg0, 1);
9412 :
9413 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9414 : when the pointed type needs higher alignment than
9415 : the p+ first operand's pointed type. */
9416 29303231 : if (!in_gimple_form
9417 29282714 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9418 29304445 : && (min_align_of_type (TREE_TYPE (type))
9419 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9420 : return NULL_TREE;
9421 :
9422 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9423 : when type is a reference type and arg00's type is not,
9424 : because arg00 could be validly nullptr and if arg01 doesn't return,
9425 : we don't want false positive binding of reference to nullptr. */
9426 29303164 : if (TREE_CODE (type) == REFERENCE_TYPE
9427 16484925 : && !in_gimple_form
9428 16484905 : && sanitize_flags_p (SANITIZE_NULL)
9429 29303595 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9430 : return NULL_TREE;
9431 :
9432 29302733 : arg00 = fold_convert_loc (loc, type, arg00);
9433 29302733 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9434 : }
9435 :
9436 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9437 : of the same precision, and X is an integer type not narrower than
9438 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9439 663177217 : if (INTEGRAL_TYPE_P (type)
9440 251242948 : && TREE_CODE (op0) == BIT_NOT_EXPR
9441 574774 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9442 574774 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9443 663557477 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9444 : {
9445 377744 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9446 450570 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9447 450568 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9448 313266 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9449 313266 : fold_convert_loc (loc, type, tem));
9450 : }
9451 :
9452 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9453 : type of X and Y (integer types only). */
9454 662863951 : if (INTEGRAL_TYPE_P (type)
9455 250929682 : && TREE_CODE (op0) == MULT_EXPR
9456 9197088 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9457 9176445 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9458 662925902 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9459 21025 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9460 : {
9461 : /* Be careful not to introduce new overflows. */
9462 61897 : tree mult_type;
9463 61897 : if (TYPE_OVERFLOW_WRAPS (type))
9464 : mult_type = type;
9465 : else
9466 2111 : mult_type = unsigned_type_for (type);
9467 :
9468 61897 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9469 : {
9470 123794 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9471 : fold_convert_loc (loc, mult_type,
9472 61897 : TREE_OPERAND (op0, 0)),
9473 : fold_convert_loc (loc, mult_type,
9474 61897 : TREE_OPERAND (op0, 1)));
9475 61897 : return fold_convert_loc (loc, type, tem);
9476 : }
9477 : }
9478 :
9479 : return NULL_TREE;
9480 :
9481 231595646 : case VIEW_CONVERT_EXPR:
9482 231595646 : if (TREE_CODE (op0) == MEM_REF)
9483 : {
9484 2686 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9485 18 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9486 2686 : tem = fold_build2_loc (loc, MEM_REF, type,
9487 2686 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9488 2686 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9489 2686 : return tem;
9490 : }
9491 :
9492 : return NULL_TREE;
9493 :
9494 4193399 : case NEGATE_EXPR:
9495 4193399 : tem = fold_negate_expr (loc, arg0);
9496 4193399 : if (tem)
9497 1534 : return fold_convert_loc (loc, type, tem);
9498 : return NULL_TREE;
9499 :
9500 3229919 : case ABS_EXPR:
9501 : /* Convert fabs((double)float) into (double)fabsf(float). */
9502 3229919 : if (TREE_CODE (arg0) == NOP_EXPR
9503 23199 : && TREE_CODE (type) == REAL_TYPE)
9504 : {
9505 23145 : tree targ0 = strip_float_extensions (arg0);
9506 23145 : if (targ0 != arg0)
9507 22941 : return fold_convert_loc (loc, type,
9508 : fold_build1_loc (loc, ABS_EXPR,
9509 22941 : TREE_TYPE (targ0),
9510 22941 : targ0));
9511 : }
9512 : return NULL_TREE;
9513 :
9514 2705102 : case BIT_NOT_EXPR:
9515 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9516 2705102 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9517 2706794 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9518 : fold_convert_loc (loc, type,
9519 1692 : TREE_OPERAND (arg0, 0)))))
9520 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9521 : fold_convert_loc (loc, type,
9522 28 : TREE_OPERAND (arg0, 1)));
9523 2705088 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9524 2706766 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9525 : fold_convert_loc (loc, type,
9526 1678 : TREE_OPERAND (arg0, 1)))))
9527 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9528 : fold_convert_loc (loc, type,
9529 46 : TREE_OPERAND (arg0, 0)), tem);
9530 :
9531 : return NULL_TREE;
9532 :
9533 50373669 : case TRUTH_NOT_EXPR:
9534 : /* Note that the operand of this must be an int
9535 : and its values must be 0 or 1.
9536 : ("true" is a fixed value perhaps depending on the language,
9537 : but we don't handle values other than 1 correctly yet.) */
9538 50373669 : tem = fold_truth_not_expr (loc, arg0);
9539 50373669 : if (!tem)
9540 : return NULL_TREE;
9541 34166238 : return fold_convert_loc (loc, type, tem);
9542 :
9543 68906028 : case INDIRECT_REF:
9544 : /* Fold *&X to X if X is an lvalue. */
9545 68906028 : if (TREE_CODE (op0) == ADDR_EXPR)
9546 : {
9547 7631 : tree op00 = TREE_OPERAND (op0, 0);
9548 7631 : if ((VAR_P (op00)
9549 : || TREE_CODE (op00) == PARM_DECL
9550 : || TREE_CODE (op00) == RESULT_DECL)
9551 6459 : && !TREE_READONLY (op00))
9552 : return op00;
9553 : }
9554 : return NULL_TREE;
9555 :
9556 : default:
9557 : return NULL_TREE;
9558 : } /* switch (code) */
9559 : }
9560 :
9561 :
9562 : /* If the operation was a conversion do _not_ mark a resulting constant
9563 : with TREE_OVERFLOW if the original constant was not. These conversions
9564 : have implementation defined behavior and retaining the TREE_OVERFLOW
9565 : flag here would confuse later passes such as VRP. */
9566 : tree
9567 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9568 : tree type, tree op0)
9569 : {
9570 0 : tree res = fold_unary_loc (loc, code, type, op0);
9571 0 : if (res
9572 0 : && TREE_CODE (res) == INTEGER_CST
9573 0 : && TREE_CODE (op0) == INTEGER_CST
9574 0 : && CONVERT_EXPR_CODE_P (code))
9575 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9576 :
9577 0 : return res;
9578 : }
9579 :
9580 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9581 : operands OP0 and OP1. LOC is the location of the resulting expression.
9582 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9583 : Return the folded expression if folding is successful. Otherwise,
9584 : return NULL_TREE. */
9585 : static tree
9586 25427565 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9587 : tree arg0, tree arg1, tree op0, tree op1)
9588 : {
9589 25427565 : tree tem;
9590 :
9591 : /* We only do these simplifications if we are optimizing. */
9592 25427565 : if (!optimize)
9593 : return NULL_TREE;
9594 :
9595 : /* Check for things like (A || B) && (A || C). We can convert this
9596 : to A || (B && C). Note that either operator can be any of the four
9597 : truth and/or operations and the transformation will still be
9598 : valid. Also note that we only care about order for the
9599 : ANDIF and ORIF operators. If B contains side effects, this
9600 : might change the truth-value of A. */
9601 24951074 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9602 5864470 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9603 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9604 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9605 5864470 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9606 25013354 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9607 : {
9608 61784 : tree a00 = TREE_OPERAND (arg0, 0);
9609 61784 : tree a01 = TREE_OPERAND (arg0, 1);
9610 61784 : tree a10 = TREE_OPERAND (arg1, 0);
9611 61784 : tree a11 = TREE_OPERAND (arg1, 1);
9612 123568 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9613 61784 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9614 61784 : && (code == TRUTH_AND_EXPR
9615 23019 : || code == TRUTH_OR_EXPR));
9616 :
9617 61784 : if (operand_equal_p (a00, a10, 0))
9618 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9619 849 : fold_build2_loc (loc, code, type, a01, a11));
9620 60935 : else if (commutative && operand_equal_p (a00, a11, 0))
9621 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9622 0 : fold_build2_loc (loc, code, type, a01, a10));
9623 60935 : else if (commutative && operand_equal_p (a01, a10, 0))
9624 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9625 0 : fold_build2_loc (loc, code, type, a00, a11));
9626 :
9627 : /* This case if tricky because we must either have commutative
9628 : operators or else A10 must not have side-effects. */
9629 :
9630 60891 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9631 121310 : && operand_equal_p (a01, a11, 0))
9632 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9633 : fold_build2_loc (loc, code, type, a00, a10),
9634 43 : a01);
9635 : }
9636 :
9637 : /* See if we can build a range comparison. */
9638 24950182 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9639 : return tem;
9640 :
9641 23787055 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9642 23785089 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9643 : {
9644 39803 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9645 39803 : if (tem)
9646 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9647 : }
9648 :
9649 23787042 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9650 23776029 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9651 : {
9652 89174 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9653 89174 : if (tem)
9654 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9655 : }
9656 :
9657 : /* Check for the possibility of merging component references. If our
9658 : lhs is another similar operation, try to merge its rhs with our
9659 : rhs. Then try to merge our lhs and rhs. */
9660 23786951 : if (TREE_CODE (arg0) == code
9661 24627626 : && (tem = fold_truth_andor_1 (loc, code, type,
9662 840675 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9663 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9664 :
9665 23786866 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9666 : return tem;
9667 :
9668 23747160 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9669 23747160 : if (param_logical_op_non_short_circuit != -1)
9670 7784 : logical_op_non_short_circuit
9671 7784 : = param_logical_op_non_short_circuit;
9672 23747160 : if (logical_op_non_short_circuit
9673 23743220 : && !sanitize_coverage_p ()
9674 23747160 : && (code == TRUTH_AND_EXPR
9675 23743217 : || code == TRUTH_ANDIF_EXPR
9676 10959210 : || code == TRUTH_OR_EXPR
9677 10959210 : || code == TRUTH_ORIF_EXPR))
9678 : {
9679 23743217 : enum tree_code ncode, icode;
9680 :
9681 23743217 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9682 23743217 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9683 12784007 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9684 :
9685 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9686 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9687 : We don't want to pack more than two leafs to a non-IF AND/OR
9688 : expression.
9689 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9690 : equal to IF-CODE, then we don't want to add right-hand operand.
9691 : If the inner right-hand side of left-hand operand has
9692 : side-effects, or isn't simple, then we can't add to it,
9693 : as otherwise we might destroy if-sequence. */
9694 23743217 : if (TREE_CODE (arg0) == icode
9695 831912 : && simple_condition_p (arg1)
9696 : /* Needed for sequence points to handle trappings, and
9697 : side-effects. */
9698 23791158 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9699 : {
9700 41086 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9701 : arg1);
9702 41086 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9703 41086 : tem);
9704 : }
9705 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9706 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9707 23702131 : else if (TREE_CODE (arg1) == icode
9708 6342 : && simple_condition_p (arg0)
9709 : /* Needed for sequence points to handle trappings, and
9710 : side-effects. */
9711 23703084 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9712 : {
9713 36 : tem = fold_build2_loc (loc, ncode, type,
9714 36 : arg0, TREE_OPERAND (arg1, 0));
9715 36 : return fold_build2_loc (loc, icode, type, tem,
9716 72 : TREE_OPERAND (arg1, 1));
9717 : }
9718 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9719 : into (A OR B).
9720 : For sequence point consistency, we need to check for trapping,
9721 : and side-effects. */
9722 5278827 : else if (code == icode && simple_condition_p (arg0)
9723 24581062 : && simple_condition_p (arg1))
9724 439644 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9725 : }
9726 :
9727 : return NULL_TREE;
9728 : }
9729 :
9730 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9731 : by changing CODE to reduce the magnitude of constants involved in
9732 : ARG0 of the comparison.
9733 : Returns a canonicalized comparison tree if a simplification was
9734 : possible, otherwise returns NULL_TREE. */
9735 :
9736 : static tree
9737 178797493 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9738 : tree arg0, tree arg1)
9739 : {
9740 178797493 : enum tree_code code0 = TREE_CODE (arg0);
9741 178797493 : tree t, cst0 = NULL_TREE;
9742 178797493 : int sgn0;
9743 :
9744 : /* Match A +- CST code arg1. We can change this only if overflow
9745 : is undefined. */
9746 178797493 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9747 136202186 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9748 : /* In principle pointers also have undefined overflow behavior,
9749 : but that causes problems elsewhere. */
9750 67464554 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9751 67464554 : && (code0 == MINUS_EXPR
9752 67464554 : || code0 == PLUS_EXPR)
9753 2621758 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9754 : return NULL_TREE;
9755 :
9756 : /* Identify the constant in arg0 and its sign. */
9757 2141805 : cst0 = TREE_OPERAND (arg0, 1);
9758 2141805 : sgn0 = tree_int_cst_sgn (cst0);
9759 :
9760 : /* Overflowed constants and zero will cause problems. */
9761 2141805 : if (integer_zerop (cst0)
9762 2141805 : || TREE_OVERFLOW (cst0))
9763 : return NULL_TREE;
9764 :
9765 : /* See if we can reduce the magnitude of the constant in
9766 : arg0 by changing the comparison code. */
9767 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9768 2141805 : if (code == LT_EXPR
9769 1268311 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9770 : code = LE_EXPR;
9771 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9772 1958853 : else if (code == GT_EXPR
9773 581059 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9774 : code = GE_EXPR;
9775 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9776 1776705 : else if (code == LE_EXPR
9777 675564 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9778 : code = LT_EXPR;
9779 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9780 1537393 : else if (code == GE_EXPR
9781 488663 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9782 : code = GT_EXPR;
9783 : else
9784 : return NULL_TREE;
9785 :
9786 : /* Now build the constant reduced in magnitude. But not if that
9787 : would produce one outside of its types range. */
9788 1615398 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9789 1615398 : && ((sgn0 == 1
9790 421322 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9791 421322 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9792 807699 : || (sgn0 == -1
9793 386377 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9794 386377 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9795 0 : return NULL_TREE;
9796 :
9797 1229021 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9798 807699 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9799 807699 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9800 807699 : t = fold_convert (TREE_TYPE (arg1), t);
9801 :
9802 807699 : return fold_build2_loc (loc, code, type, t, arg1);
9803 : }
9804 :
9805 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9806 : overflow further. Try to decrease the magnitude of constants involved
9807 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9808 : and put sole constants at the second argument position.
9809 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9810 :
9811 : static tree
9812 89781234 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9813 : tree arg0, tree arg1)
9814 : {
9815 89781234 : tree t;
9816 :
9817 : /* Try canonicalization by simplifying arg0. */
9818 89781234 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9819 89781234 : if (t)
9820 : return t;
9821 :
9822 : /* Try canonicalization by simplifying arg1 using the swapped
9823 : comparison. */
9824 89016259 : code = swap_tree_comparison (code);
9825 89016259 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9826 89016259 : return t;
9827 : }
9828 :
9829 : /* Return a positive integer when the symbol DECL is known to have
9830 : a nonzero address, zero when it's known not to (e.g., it's a weak
9831 : symbol), and a negative integer when the symbol is not yet in the
9832 : symbol table and so whether or not its address is zero is unknown.
9833 : For function local objects always return positive integer. */
9834 : static int
9835 11855304 : maybe_nonzero_address (tree decl)
9836 : {
9837 11855304 : if (!DECL_P (decl))
9838 : return -1;
9839 :
9840 : /* Normally, don't do anything for variables and functions before symtab is
9841 : built; it is quite possible that DECL will be declared weak later.
9842 : But if folding_initializer, we need a constant answer now, so create
9843 : the symtab entry and prevent later weak declaration. */
9844 9740154 : if (decl_in_symtab_p (decl))
9845 : {
9846 4344768 : if (struct symtab_node *symbol
9847 4344768 : = (folding_initializer
9848 4344768 : ? symtab_node::get_create (decl)
9849 4327594 : : symtab_node::get (decl)))
9850 4325775 : return symbol->nonzero_address ();
9851 : }
9852 5395386 : else if (folding_cxx_constexpr)
9853 : /* Anything that doesn't go in the symtab has non-zero address. */
9854 : return 1;
9855 :
9856 : /* Function local objects are never NULL. */
9857 5276010 : if (DECL_CONTEXT (decl)
9858 5259250 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9859 10531819 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9860 : return 1;
9861 :
9862 : return -1;
9863 : }
9864 :
9865 : /* Subroutine of fold_binary. This routine performs all of the
9866 : transformations that are common to the equality/inequality
9867 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9868 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9869 : fold_binary should call fold_binary. Fold a comparison with
9870 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9871 : the folded comparison or NULL_TREE. */
9872 :
9873 : static tree
9874 89857479 : fold_comparison (location_t loc, enum tree_code code, tree type,
9875 : tree op0, tree op1)
9876 : {
9877 89857479 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9878 89857479 : tree arg0, arg1, tem;
9879 :
9880 89857479 : arg0 = op0;
9881 89857479 : arg1 = op1;
9882 :
9883 89857479 : STRIP_SIGN_NOPS (arg0);
9884 89857479 : STRIP_SIGN_NOPS (arg1);
9885 :
9886 : /* For comparisons of pointers we can decompose it to a compile time
9887 : comparison of the base objects and the offsets into the object.
9888 : This requires at least one operand being an ADDR_EXPR or a
9889 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9890 166302224 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9891 90074022 : && (TREE_CODE (arg0) == ADDR_EXPR
9892 13576184 : || TREE_CODE (arg1) == ADDR_EXPR
9893 12057720 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9894 11291273 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9895 : {
9896 2347965 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9897 2347965 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9898 2347965 : machine_mode mode;
9899 2347965 : int volatilep, reversep, unsignedp;
9900 2347965 : bool indirect_base0 = false, indirect_base1 = false;
9901 :
9902 : /* Get base and offset for the access. Strip ADDR_EXPR for
9903 : get_inner_reference, but put it back by stripping INDIRECT_REF
9904 : off the base object if possible. indirect_baseN will be true
9905 : if baseN is not an address but refers to the object itself. */
9906 2347965 : base0 = arg0;
9907 2347965 : if (TREE_CODE (arg0) == ADDR_EXPR)
9908 : {
9909 53093 : base0
9910 53093 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9911 : &bitsize, &bitpos0, &offset0, &mode,
9912 : &unsignedp, &reversep, &volatilep);
9913 53093 : if (INDIRECT_REF_P (base0))
9914 2221 : base0 = TREE_OPERAND (base0, 0);
9915 : else
9916 : indirect_base0 = true;
9917 : }
9918 2294872 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9919 : {
9920 833230 : base0 = TREE_OPERAND (arg0, 0);
9921 833230 : STRIP_SIGN_NOPS (base0);
9922 833230 : if (TREE_CODE (base0) == ADDR_EXPR)
9923 : {
9924 33144 : base0
9925 33144 : = get_inner_reference (TREE_OPERAND (base0, 0),
9926 : &bitsize, &bitpos0, &offset0, &mode,
9927 : &unsignedp, &reversep, &volatilep);
9928 33144 : if (INDIRECT_REF_P (base0))
9929 20 : base0 = TREE_OPERAND (base0, 0);
9930 : else
9931 : indirect_base0 = true;
9932 : }
9933 833230 : if (offset0 == NULL_TREE || integer_zerop (offset0))
9934 833230 : offset0 = TREE_OPERAND (arg0, 1);
9935 : else
9936 0 : offset0 = size_binop (PLUS_EXPR, offset0,
9937 : TREE_OPERAND (arg0, 1));
9938 833230 : if (poly_int_tree_p (offset0))
9939 : {
9940 668515 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
9941 668515 : TYPE_PRECISION (sizetype));
9942 668515 : tem <<= LOG2_BITS_PER_UNIT;
9943 668515 : tem += bitpos0;
9944 668515 : if (tem.to_shwi (&bitpos0))
9945 668515 : offset0 = NULL_TREE;
9946 : }
9947 : }
9948 :
9949 2347965 : base1 = arg1;
9950 2347965 : if (TREE_CODE (arg1) == ADDR_EXPR)
9951 : {
9952 1547702 : base1
9953 1547702 : = get_inner_reference (TREE_OPERAND (arg1, 0),
9954 : &bitsize, &bitpos1, &offset1, &mode,
9955 : &unsignedp, &reversep, &volatilep);
9956 1547702 : if (INDIRECT_REF_P (base1))
9957 69379 : base1 = TREE_OPERAND (base1, 0);
9958 : else
9959 : indirect_base1 = true;
9960 : }
9961 800263 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9962 : {
9963 93340 : base1 = TREE_OPERAND (arg1, 0);
9964 93340 : STRIP_SIGN_NOPS (base1);
9965 93340 : if (TREE_CODE (base1) == ADDR_EXPR)
9966 : {
9967 11119 : base1
9968 11119 : = get_inner_reference (TREE_OPERAND (base1, 0),
9969 : &bitsize, &bitpos1, &offset1, &mode,
9970 : &unsignedp, &reversep, &volatilep);
9971 11119 : if (INDIRECT_REF_P (base1))
9972 0 : base1 = TREE_OPERAND (base1, 0);
9973 : else
9974 : indirect_base1 = true;
9975 : }
9976 93340 : if (offset1 == NULL_TREE || integer_zerop (offset1))
9977 93316 : offset1 = TREE_OPERAND (arg1, 1);
9978 : else
9979 24 : offset1 = size_binop (PLUS_EXPR, offset1,
9980 : TREE_OPERAND (arg1, 1));
9981 93340 : if (poly_int_tree_p (offset1))
9982 : {
9983 82681 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
9984 82681 : TYPE_PRECISION (sizetype));
9985 82681 : tem <<= LOG2_BITS_PER_UNIT;
9986 82681 : tem += bitpos1;
9987 82681 : if (tem.to_shwi (&bitpos1))
9988 82681 : offset1 = NULL_TREE;
9989 : }
9990 : }
9991 :
9992 : /* If we have equivalent bases we might be able to simplify. */
9993 2347965 : if (indirect_base0 == indirect_base1
9994 3163291 : && operand_equal_p (base0, base1,
9995 : indirect_base0 ? OEP_ADDRESS_OF : 0))
9996 : {
9997 : /* We can fold this expression to a constant if the non-constant
9998 : offset parts are equal. */
9999 22703 : if ((offset0 == offset1
10000 6686 : || (offset0 && offset1
10001 2706 : && operand_equal_p (offset0, offset1, 0)))
10002 22703 : && (equality_code
10003 9444 : || (indirect_base0
10004 6249 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10005 3195 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10006 : {
10007 15977 : switch (code)
10008 : {
10009 56 : case EQ_EXPR:
10010 56 : if (known_eq (bitpos0, bitpos1))
10011 53994 : return constant_boolean_node (true, type);
10012 21 : if (known_ne (bitpos0, bitpos1))
10013 21 : return constant_boolean_node (false, type);
10014 : break;
10015 6517 : case NE_EXPR:
10016 6517 : if (known_ne (bitpos0, bitpos1))
10017 6512 : return constant_boolean_node (true, type);
10018 5 : if (known_eq (bitpos0, bitpos1))
10019 5 : return constant_boolean_node (false, type);
10020 : break;
10021 2388 : case LT_EXPR:
10022 2388 : if (known_lt (bitpos0, bitpos1))
10023 2252 : return constant_boolean_node (true, type);
10024 136 : if (known_ge (bitpos0, bitpos1))
10025 136 : return constant_boolean_node (false, type);
10026 : break;
10027 1656 : case LE_EXPR:
10028 1656 : if (known_le (bitpos0, bitpos1))
10029 182 : return constant_boolean_node (true, type);
10030 1474 : if (known_gt (bitpos0, bitpos1))
10031 1474 : return constant_boolean_node (false, type);
10032 : break;
10033 3550 : case GE_EXPR:
10034 3550 : if (known_ge (bitpos0, bitpos1))
10035 1377 : return constant_boolean_node (true, type);
10036 2173 : if (known_lt (bitpos0, bitpos1))
10037 2173 : return constant_boolean_node (false, type);
10038 : break;
10039 1810 : case GT_EXPR:
10040 1810 : if (known_gt (bitpos0, bitpos1))
10041 1751 : return constant_boolean_node (true, type);
10042 59 : if (known_le (bitpos0, bitpos1))
10043 59 : return constant_boolean_node (false, type);
10044 : break;
10045 : default:;
10046 : }
10047 : }
10048 : /* We can simplify the comparison to a comparison of the variable
10049 : offset parts if the constant offset parts are equal.
10050 : Be careful to use signed sizetype here because otherwise we
10051 : mess with array offsets in the wrong way. This is possible
10052 : because pointer arithmetic is restricted to retain within an
10053 : object and overflow on pointer differences is undefined as of
10054 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10055 6726 : else if (known_eq (bitpos0, bitpos1)
10056 6726 : && (equality_code
10057 5250 : || (indirect_base0
10058 271 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10059 4979 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10060 : {
10061 : /* By converting to signed sizetype we cover middle-end pointer
10062 : arithmetic which operates on unsigned pointer types of size
10063 : type size and ARRAY_REF offsets which are properly sign or
10064 : zero extended from their type in case it is narrower than
10065 : sizetype. */
10066 5359 : if (offset0 == NULL_TREE)
10067 0 : offset0 = build_int_cst (ssizetype, 0);
10068 : else
10069 5359 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10070 5359 : if (offset1 == NULL_TREE)
10071 2685 : offset1 = build_int_cst (ssizetype, 0);
10072 : else
10073 2674 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10074 :
10075 5359 : return fold_build2_loc (loc, code, type, offset0, offset1);
10076 : }
10077 : }
10078 : /* For equal offsets we can simplify to a comparison of the
10079 : base addresses. */
10080 2325262 : else if (known_eq (bitpos0, bitpos1)
10081 55528 : && (indirect_base0
10082 992108 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10083 15210 : && (indirect_base1
10084 188734 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10085 2551005 : && ((offset0 == offset1)
10086 4415 : || (offset0 && offset1
10087 4145 : && operand_equal_p (offset0, offset1, 0))))
10088 : {
10089 32595 : if (indirect_base0)
10090 3042 : base0 = build_fold_addr_expr_loc (loc, base0);
10091 32595 : if (indirect_base1)
10092 4676 : base1 = build_fold_addr_expr_loc (loc, base1);
10093 32595 : return fold_build2_loc (loc, code, type, base0, base1);
10094 : }
10095 : /* Comparison between an ordinary (non-weak) symbol and a null
10096 : pointer can be eliminated since such symbols must have a non
10097 : null address. In C, relational expressions between pointers
10098 : to objects and null pointers are undefined. The results
10099 : below follow the C++ rules with the additional property that
10100 : every object pointer compares greater than a null pointer.
10101 : */
10102 2292667 : else if (((DECL_P (base0)
10103 253728 : && maybe_nonzero_address (base0) > 0
10104 : /* Avoid folding references to struct members at offset 0 to
10105 : prevent tests like '&ptr->firstmember == 0' from getting
10106 : eliminated. When ptr is null, although the -> expression
10107 : is strictly speaking invalid, GCC retains it as a matter
10108 : of QoI. See PR c/44555. */
10109 239250 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10110 2276744 : || CONSTANT_CLASS_P (base0))
10111 20740 : && indirect_base0
10112 : /* The caller guarantees that when one of the arguments is
10113 : constant (i.e., null in this case) it is second. */
10114 2310636 : && integer_zerop (arg1))
10115 : {
10116 63 : switch (code)
10117 : {
10118 24 : case EQ_EXPR:
10119 24 : case LE_EXPR:
10120 24 : case LT_EXPR:
10121 24 : return constant_boolean_node (false, type);
10122 39 : case GE_EXPR:
10123 39 : case GT_EXPR:
10124 39 : case NE_EXPR:
10125 39 : return constant_boolean_node (true, type);
10126 0 : default:
10127 0 : gcc_unreachable ();
10128 : }
10129 : }
10130 : }
10131 :
10132 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10133 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10134 : the resulting offset is smaller in absolute value than the
10135 : original one and has the same sign. */
10136 176934840 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10137 137011672 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10138 34137019 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10139 2362465 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10140 1936843 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10141 1936843 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10142 158480800 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10143 169111 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10144 : {
10145 169111 : tree const1 = TREE_OPERAND (arg0, 1);
10146 169111 : tree const2 = TREE_OPERAND (arg1, 1);
10147 169111 : tree variable1 = TREE_OPERAND (arg0, 0);
10148 169111 : tree variable2 = TREE_OPERAND (arg1, 0);
10149 169111 : tree cst;
10150 :
10151 : /* Put the constant on the side where it doesn't overflow and is
10152 : of lower absolute value and of same sign than before. */
10153 169112 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10154 : ? MINUS_EXPR : PLUS_EXPR,
10155 : const2, const1);
10156 169111 : if (!TREE_OVERFLOW (cst)
10157 169095 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10158 191362 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10159 5660 : return fold_build2_loc (loc, code, type,
10160 : variable1,
10161 5660 : fold_build2_loc (loc, TREE_CODE (arg1),
10162 5660 : TREE_TYPE (arg1),
10163 5660 : variable2, cst));
10164 :
10165 163452 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10166 : ? MINUS_EXPR : PLUS_EXPR,
10167 : const1, const2);
10168 163451 : if (!TREE_OVERFLOW (cst)
10169 163435 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10170 180042 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10171 16591 : return fold_build2_loc (loc, code, type,
10172 16591 : fold_build2_loc (loc, TREE_CODE (arg0),
10173 16591 : TREE_TYPE (arg0),
10174 : variable1, cst),
10175 16591 : variable2);
10176 : }
10177 :
10178 89781234 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10179 89781234 : if (tem)
10180 : return tem;
10181 :
10182 : /* If we are comparing an expression that just has comparisons
10183 : of two integer values, arithmetic expressions of those comparisons,
10184 : and constants, we can simplify it. There are only three cases
10185 : to check: the two values can either be equal, the first can be
10186 : greater, or the second can be greater. Fold the expression for
10187 : those three values. Since each value must be 0 or 1, we have
10188 : eight possibilities, each of which corresponds to the constant 0
10189 : or 1 or one of the six possible comparisons.
10190 :
10191 : This handles common cases like (a > b) == 0 but also handles
10192 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10193 : occur in macroized code. */
10194 :
10195 88973535 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10196 : {
10197 54236781 : tree cval1 = 0, cval2 = 0;
10198 :
10199 54236781 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10200 : /* Don't handle degenerate cases here; they should already
10201 : have been handled anyway. */
10202 588123 : && cval1 != 0 && cval2 != 0
10203 587190 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10204 587190 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10205 587184 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10206 50 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10207 50 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10208 54236831 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10209 50 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10210 : {
10211 50 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10212 50 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10213 :
10214 : /* We can't just pass T to eval_subst in case cval1 or cval2
10215 : was the same as ARG1. */
10216 :
10217 50 : tree high_result
10218 50 : = fold_build2_loc (loc, code, type,
10219 : eval_subst (loc, arg0, cval1, maxval,
10220 : cval2, minval),
10221 : arg1);
10222 50 : tree equal_result
10223 50 : = fold_build2_loc (loc, code, type,
10224 : eval_subst (loc, arg0, cval1, maxval,
10225 : cval2, maxval),
10226 : arg1);
10227 50 : tree low_result
10228 50 : = fold_build2_loc (loc, code, type,
10229 : eval_subst (loc, arg0, cval1, minval,
10230 : cval2, maxval),
10231 : arg1);
10232 :
10233 : /* All three of these results should be 0 or 1. Confirm they are.
10234 : Then use those values to select the proper code to use. */
10235 :
10236 50 : if (TREE_CODE (high_result) == INTEGER_CST
10237 50 : && TREE_CODE (equal_result) == INTEGER_CST
10238 40 : && TREE_CODE (low_result) == INTEGER_CST)
10239 : {
10240 : /* Make a 3-bit mask with the high-order bit being the
10241 : value for `>', the next for '=', and the low for '<'. */
10242 40 : switch ((integer_onep (high_result) * 4)
10243 40 : + (integer_onep (equal_result) * 2)
10244 40 : + integer_onep (low_result))
10245 : {
10246 21 : case 0:
10247 : /* Always false. */
10248 40 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10249 : case 1:
10250 : code = LT_EXPR;
10251 : break;
10252 2 : case 2:
10253 2 : code = EQ_EXPR;
10254 2 : break;
10255 0 : case 3:
10256 0 : code = LE_EXPR;
10257 0 : break;
10258 0 : case 4:
10259 0 : code = GT_EXPR;
10260 0 : break;
10261 1 : case 5:
10262 1 : code = NE_EXPR;
10263 1 : break;
10264 0 : case 6:
10265 0 : code = GE_EXPR;
10266 0 : break;
10267 16 : case 7:
10268 : /* Always true. */
10269 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10270 : }
10271 :
10272 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10273 : }
10274 : }
10275 : }
10276 :
10277 : return NULL_TREE;
10278 : }
10279 :
10280 :
10281 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10282 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10283 : argument EXPR represents the expression "z" of type TYPE. */
10284 :
10285 : static tree
10286 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10287 : {
10288 2 : tree itype = TREE_TYPE (type);
10289 2 : tree rpart, ipart, tem;
10290 :
10291 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10292 : {
10293 0 : rpart = TREE_OPERAND (expr, 0);
10294 0 : ipart = TREE_OPERAND (expr, 1);
10295 : }
10296 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10297 : {
10298 0 : rpart = TREE_REALPART (expr);
10299 0 : ipart = TREE_IMAGPART (expr);
10300 : }
10301 : else
10302 : {
10303 2 : expr = save_expr (expr);
10304 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10305 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10306 : }
10307 :
10308 2 : rpart = save_expr (rpart);
10309 2 : ipart = save_expr (ipart);
10310 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10311 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10312 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10313 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10314 2 : build_zero_cst (itype));
10315 : }
10316 :
10317 :
10318 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10319 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10320 : true if successful. */
10321 :
10322 : static bool
10323 31469 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10324 : {
10325 31469 : unsigned HOST_WIDE_INT i, nunits;
10326 :
10327 31469 : if (TREE_CODE (arg) == VECTOR_CST
10328 31469 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10329 : {
10330 2168 : for (i = 0; i < nunits; ++i)
10331 1710 : elts[i] = VECTOR_CST_ELT (arg, i);
10332 : }
10333 31011 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10334 : {
10335 : constructor_elt *elt;
10336 :
10337 51251 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10338 46076 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10339 25836 : return false;
10340 : else
10341 20240 : elts[i] = elt->value;
10342 : }
10343 : else
10344 : return false;
10345 6407 : for (; i < nelts; i++)
10346 1548 : elts[i]
10347 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10348 : return true;
10349 : }
10350 :
10351 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10352 : mask for VLA vec_perm folding.
10353 : REASON if specified, will contain the reason why SEL is not suitable.
10354 : Used only for debugging and unit-testing. */
10355 :
10356 : static bool
10357 30105 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10358 : const vec_perm_indices &sel,
10359 : const char **reason = NULL)
10360 : {
10361 30105 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10362 30105 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10363 :
10364 60210 : if (!(pow2p_hwi (sel_npatterns)
10365 30105 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10366 30105 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10367 : {
10368 0 : if (reason)
10369 0 : *reason = "npatterns is not power of 2";
10370 0 : return false;
10371 : }
10372 :
10373 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10374 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10375 30105 : poly_uint64 esel;
10376 30105 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10377 : {
10378 0 : if (reason)
10379 0 : *reason = "sel.length is not multiple of sel_npatterns";
10380 0 : return false;
10381 : }
10382 :
10383 30105 : if (sel_nelts_per_pattern < 3)
10384 : return true;
10385 :
10386 5976 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10387 : {
10388 4521 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10389 4521 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10390 4521 : HOST_WIDE_INT step;
10391 4521 : if (!poly_int64 (a2 - a1).is_constant (&step))
10392 : {
10393 : if (reason)
10394 : *reason = "step is not constant";
10395 1124 : return false;
10396 : }
10397 : // FIXME: Punt on step < 0 for now, revisit later.
10398 4521 : if (step < 0)
10399 : return false;
10400 4465 : if (step == 0)
10401 0 : continue;
10402 :
10403 4465 : if (!pow2p_hwi (step))
10404 : {
10405 0 : if (reason)
10406 0 : *reason = "step is not power of 2";
10407 0 : return false;
10408 : }
10409 :
10410 : /* Ensure that stepped sequence of the pattern selects elements
10411 : only from the same input vector. */
10412 4465 : uint64_t q1, qe;
10413 4465 : poly_uint64 r1, re;
10414 4465 : poly_uint64 ae = a1 + (esel - 2) * step;
10415 4465 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10416 :
10417 4465 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10418 4465 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10419 : && q1 == qe))
10420 : {
10421 460 : if (reason)
10422 0 : *reason = "crossed input vectors";
10423 460 : return false;
10424 : }
10425 :
10426 : /* Ensure that the stepped sequence always selects from the same
10427 : input pattern. */
10428 4005 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10429 4005 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10430 :
10431 4005 : if (!multiple_p (step, arg_npatterns))
10432 : {
10433 606 : if (reason)
10434 0 : *reason = "step is not multiple of npatterns";
10435 606 : return false;
10436 : }
10437 :
10438 : /* If a1 chooses base element from arg, ensure that it's a natural
10439 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10440 : to preserve arg's encoding. */
10441 :
10442 3399 : if (maybe_lt (r1, arg_npatterns))
10443 : {
10444 24 : unsigned HOST_WIDE_INT index;
10445 24 : if (!r1.is_constant (&index))
10446 2 : return false;
10447 :
10448 24 : tree arg_elem0 = vector_cst_elt (arg, index);
10449 24 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10450 24 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10451 :
10452 24 : tree step1, step2;
10453 24 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10454 24 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10455 48 : || !operand_equal_p (step1, step2, 0))
10456 : {
10457 2 : if (reason)
10458 0 : *reason = "not a natural stepped sequence";
10459 2 : return false;
10460 : }
10461 : }
10462 : }
10463 :
10464 : return true;
10465 : }
10466 :
10467 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10468 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10469 : REASON has same purpose as described in
10470 : valid_mask_for_fold_vec_perm_cst_p. */
10471 :
10472 : static tree
10473 30105 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10474 : const char **reason = NULL)
10475 : {
10476 30105 : unsigned res_npatterns, res_nelts_per_pattern;
10477 30105 : unsigned HOST_WIDE_INT res_nelts;
10478 :
10479 : /* First try to implement the fold in a VLA-friendly way.
10480 :
10481 : (1) If the selector is simply a duplication of N elements, the
10482 : result is likewise a duplication of N elements.
10483 :
10484 : (2) If the selector is N elements followed by a duplication
10485 : of N elements, the result is too.
10486 :
10487 : (3) If the selector is N elements followed by an interleaving
10488 : of N linear series, the situation is more complex.
10489 :
10490 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10491 : can handle this case. If we can, then each of the N linear
10492 : series either (a) selects the same element each time or
10493 : (b) selects a linear series from one of the input patterns.
10494 :
10495 : If (b) holds for one of the linear series, the result
10496 : will contain a linear series, and so the result will have
10497 : the same shape as the selector. If (a) holds for all of
10498 : the linear series, the result will be the same as (2) above.
10499 :
10500 : (b) can only hold if one of the input patterns has a
10501 : stepped encoding. */
10502 :
10503 30105 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10504 : {
10505 28981 : res_npatterns = sel.encoding ().npatterns ();
10506 28981 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10507 28981 : if (res_nelts_per_pattern == 3
10508 1455 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10509 29931 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10510 : res_nelts_per_pattern = 2;
10511 28981 : res_nelts = res_npatterns * res_nelts_per_pattern;
10512 : }
10513 1124 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10514 : {
10515 1124 : res_npatterns = res_nelts;
10516 1124 : res_nelts_per_pattern = 1;
10517 : }
10518 : else
10519 : return NULL_TREE;
10520 :
10521 30105 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10522 151533 : for (unsigned i = 0; i < res_nelts; i++)
10523 : {
10524 121428 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10525 121428 : uint64_t q;
10526 121428 : poly_uint64 r;
10527 121428 : unsigned HOST_WIDE_INT index;
10528 :
10529 : /* Punt if sel[i] /trunc_div len cannot be determined,
10530 : because the input vector to be chosen will depend on
10531 : runtime vector length.
10532 : For example if len == 4 + 4x, and sel[i] == 4,
10533 : If len at runtime equals 4, we choose arg1[0].
10534 : For any other value of len > 4 at runtime, we choose arg0[4].
10535 : which makes the element choice dependent on runtime vector length. */
10536 121428 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10537 : {
10538 : if (reason)
10539 : *reason = "cannot divide selector element by arg len";
10540 : return NULL_TREE;
10541 : }
10542 :
10543 : /* sel[i] % len will give the index of element in the chosen input
10544 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10545 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10546 121428 : if (!r.is_constant (&index))
10547 : {
10548 : if (reason)
10549 : *reason = "remainder is not constant";
10550 : return NULL_TREE;
10551 : }
10552 :
10553 121428 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10554 121428 : tree elem = vector_cst_elt (arg, index);
10555 121428 : out_elts.quick_push (elem);
10556 : }
10557 :
10558 30105 : return out_elts.build ();
10559 30105 : }
10560 :
10561 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10562 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10563 : NULL_TREE otherwise. */
10564 :
10565 : tree
10566 69354 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10567 : {
10568 69354 : unsigned int i;
10569 69354 : unsigned HOST_WIDE_INT nelts;
10570 :
10571 69354 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10572 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10573 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10574 :
10575 69354 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10576 69354 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10577 : return NULL_TREE;
10578 :
10579 58741 : if (TREE_CODE (arg0) == VECTOR_CST
10580 30379 : && TREE_CODE (arg1) == VECTOR_CST)
10581 30105 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10582 :
10583 : /* For fall back case, we want to ensure we have VLS vectors
10584 : with equal length. */
10585 28636 : if (!sel.length ().is_constant (&nelts)
10586 28636 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10587 0 : return NULL_TREE;
10588 :
10589 28636 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10590 28636 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10591 28636 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10592 25836 : return NULL_TREE;
10593 :
10594 2800 : vec<constructor_elt, va_gc> *v;
10595 2800 : vec_alloc (v, nelts);
10596 14030 : for (i = 0; i < nelts; i++)
10597 : {
10598 11230 : HOST_WIDE_INT index;
10599 11230 : if (!sel[i].is_constant (&index))
10600 : return NULL_TREE;
10601 11230 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10602 : }
10603 2800 : return build_constructor (type, v);
10604 : }
10605 :
10606 : /* Try to fold a pointer difference of type TYPE two address expressions of
10607 : array references AREF0 and AREF1 using location LOC. Return a
10608 : simplified expression for the difference or NULL_TREE. */
10609 :
10610 : static tree
10611 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10612 : tree aref0, tree aref1,
10613 : bool use_pointer_diff)
10614 : {
10615 39 : tree base0 = TREE_OPERAND (aref0, 0);
10616 39 : tree base1 = TREE_OPERAND (aref1, 0);
10617 39 : tree base_offset = build_int_cst (type, 0);
10618 :
10619 : /* If the bases are array references as well, recurse. If the bases
10620 : are pointer indirections compute the difference of the pointers.
10621 : If the bases are equal, we are set. */
10622 39 : if ((TREE_CODE (base0) == ARRAY_REF
10623 1 : && TREE_CODE (base1) == ARRAY_REF
10624 1 : && (base_offset
10625 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10626 : use_pointer_diff)))
10627 38 : || (INDIRECT_REF_P (base0)
10628 7 : && INDIRECT_REF_P (base1)
10629 7 : && (base_offset
10630 : = use_pointer_diff
10631 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10632 1 : TREE_OPERAND (base0, 0),
10633 1 : TREE_OPERAND (base1, 0))
10634 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10635 6 : fold_convert (type,
10636 : TREE_OPERAND (base0, 0)),
10637 6 : fold_convert (type,
10638 : TREE_OPERAND (base1, 0)))))
10639 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10640 : {
10641 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10642 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10643 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10644 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10645 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10646 : base_offset,
10647 : fold_build2_loc (loc, MULT_EXPR, type,
10648 15 : diff, esz));
10649 : }
10650 : return NULL_TREE;
10651 : }
10652 :
10653 : /* If the real or vector real constant CST of type TYPE has an exact
10654 : inverse, return it, else return NULL. */
10655 :
10656 : tree
10657 1153107 : exact_inverse (tree type, tree cst)
10658 : {
10659 1153107 : REAL_VALUE_TYPE r;
10660 1153107 : tree unit_type;
10661 1153107 : machine_mode mode;
10662 :
10663 1153107 : switch (TREE_CODE (cst))
10664 : {
10665 1152490 : case REAL_CST:
10666 1152490 : r = TREE_REAL_CST (cst);
10667 :
10668 1152490 : if (exact_real_inverse (TYPE_MODE (type), &r))
10669 330635 : return build_real (type, r);
10670 :
10671 : return NULL_TREE;
10672 :
10673 617 : case VECTOR_CST:
10674 617 : {
10675 617 : unit_type = TREE_TYPE (type);
10676 617 : mode = TYPE_MODE (unit_type);
10677 :
10678 617 : tree_vector_builder elts;
10679 617 : if (!elts.new_unary_operation (type, cst, false))
10680 : return NULL_TREE;
10681 617 : unsigned int count = elts.encoded_nelts ();
10682 677 : for (unsigned int i = 0; i < count; ++i)
10683 : {
10684 617 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10685 617 : if (!exact_real_inverse (mode, &r))
10686 : return NULL_TREE;
10687 60 : elts.quick_push (build_real (unit_type, r));
10688 : }
10689 :
10690 60 : return elts.build ();
10691 617 : }
10692 :
10693 : default:
10694 : return NULL_TREE;
10695 : }
10696 : }
10697 :
10698 : /* Mask out the tz least significant bits of X of type TYPE where
10699 : tz is the number of trailing zeroes in Y. */
10700 : static wide_int
10701 133834 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10702 : {
10703 133834 : int tz = wi::ctz (y);
10704 133834 : if (tz > 0)
10705 6558 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10706 127276 : return x;
10707 : }
10708 :
10709 : /* Return true when T is an address and is known to be nonzero.
10710 : For floating point we further ensure that T is not denormal.
10711 : Similar logic is present in nonzero_address in rtlanal.h. */
10712 :
10713 : bool
10714 148626946 : tree_expr_nonzero_p (tree t)
10715 : {
10716 148980251 : tree type = TREE_TYPE (t);
10717 148980251 : enum tree_code code;
10718 :
10719 : /* Doing something useful for floating point would need more work. */
10720 148980251 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10721 : return false;
10722 :
10723 148857063 : code = TREE_CODE (t);
10724 148857063 : switch (TREE_CODE_CLASS (code))
10725 : {
10726 970078 : case tcc_unary:
10727 970078 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10728 2930680 : case tcc_binary:
10729 2930680 : case tcc_comparison:
10730 2930680 : return tree_binary_nonzero_p (code, type,
10731 2930680 : TREE_OPERAND (t, 0),
10732 5861360 : TREE_OPERAND (t, 1));
10733 13066362 : case tcc_constant:
10734 13066362 : case tcc_declaration:
10735 13066362 : case tcc_reference:
10736 13066362 : return tree_single_nonzero_p (t);
10737 :
10738 131889943 : default:
10739 131889943 : break;
10740 : }
10741 :
10742 131889943 : switch (code)
10743 : {
10744 586760 : case TRUTH_NOT_EXPR:
10745 586760 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10746 :
10747 70592 : case TRUTH_AND_EXPR:
10748 70592 : case TRUTH_OR_EXPR:
10749 70592 : case TRUTH_XOR_EXPR:
10750 70592 : return tree_binary_nonzero_p (code, type,
10751 70592 : TREE_OPERAND (t, 0),
10752 141184 : TREE_OPERAND (t, 1));
10753 :
10754 127779489 : case COND_EXPR:
10755 127779489 : case CONSTRUCTOR:
10756 127779489 : case OBJ_TYPE_REF:
10757 127779489 : case ADDR_EXPR:
10758 127779489 : case WITH_SIZE_EXPR:
10759 127779489 : case SSA_NAME:
10760 127779489 : return tree_single_nonzero_p (t);
10761 :
10762 84888 : case COMPOUND_EXPR:
10763 84888 : case MODIFY_EXPR:
10764 84888 : case BIND_EXPR:
10765 84888 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10766 :
10767 268417 : case SAVE_EXPR:
10768 268417 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10769 :
10770 3044192 : case CALL_EXPR:
10771 3044192 : {
10772 3044192 : tree fndecl = get_callee_fndecl (t);
10773 3044192 : if (!fndecl) return false;
10774 3042229 : if (flag_delete_null_pointer_checks && !flag_check_new
10775 3042229 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10776 3042931 : && !TREE_NOTHROW (fndecl))
10777 : return true;
10778 3042931 : if (flag_delete_null_pointer_checks
10779 6085160 : && lookup_attribute ("returns_nonnull",
10780 3042229 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10781 : return true;
10782 3042923 : return alloca_call_p (t);
10783 : }
10784 :
10785 : default:
10786 : break;
10787 : }
10788 : return false;
10789 : }
10790 :
10791 : /* Return true if T is known not to be equal to an integer W.
10792 : If STMT is specified, the check is if T on STMT is not equal
10793 : to W. */
10794 :
10795 : bool
10796 100569510 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10797 : {
10798 100569510 : int_range_max vr;
10799 100569510 : switch (TREE_CODE (t))
10800 : {
10801 1084632 : case INTEGER_CST:
10802 1084632 : return wi::to_wide (t) != w;
10803 :
10804 99483807 : case SSA_NAME:
10805 99483807 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10806 : return false;
10807 :
10808 198967614 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10809 99483807 : if (!vr.undefined_p () && !vr.contains_p (w))
10810 : return true;
10811 : /* If T has some known zero bits and W has any of those bits set,
10812 : then T is known not to be equal to W. */
10813 99350122 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10814 198699812 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10815 : return true;
10816 : return false;
10817 :
10818 : default:
10819 : return false;
10820 : }
10821 100569510 : }
10822 :
10823 : /* Fold a binary expression of code CODE and type TYPE with operands
10824 : OP0 and OP1. LOC is the location of the resulting expression.
10825 : Return the folded expression if folding is successful. Otherwise,
10826 : return NULL_TREE. */
10827 :
10828 : tree
10829 957533331 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10830 : tree op0, tree op1)
10831 : {
10832 957533331 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10833 957533331 : tree arg0, arg1, tem;
10834 957533331 : tree t1 = NULL_TREE;
10835 957533331 : unsigned int prec;
10836 :
10837 957533331 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10838 : && TREE_CODE_LENGTH (code) == 2
10839 : && op0 != NULL_TREE
10840 : && op1 != NULL_TREE);
10841 :
10842 957533331 : arg0 = op0;
10843 957533331 : arg1 = op1;
10844 :
10845 : /* Strip any conversions that don't change the mode. This is
10846 : safe for every expression, except for a comparison expression
10847 : because its signedness is derived from its operands. So, in
10848 : the latter case, only strip conversions that don't change the
10849 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10850 : preserved.
10851 :
10852 : Note that this is done as an internal manipulation within the
10853 : constant folder, in order to find the simplest representation
10854 : of the arguments so that their form can be studied. In any
10855 : cases, the appropriate type conversions should be put back in
10856 : the tree that will get out of the constant folder. */
10857 :
10858 957533331 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10859 : {
10860 210547322 : STRIP_SIGN_NOPS (arg0);
10861 210547322 : STRIP_SIGN_NOPS (arg1);
10862 : }
10863 : else
10864 : {
10865 746986009 : STRIP_NOPS (arg0);
10866 746986009 : STRIP_NOPS (arg1);
10867 : }
10868 :
10869 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10870 : constant but we can't do arithmetic on them. */
10871 957533331 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10872 : {
10873 268468006 : tem = const_binop (code, type, arg0, arg1);
10874 268468006 : if (tem != NULL_TREE)
10875 : {
10876 265924602 : if (TREE_TYPE (tem) != type)
10877 4238314 : tem = fold_convert_loc (loc, type, tem);
10878 265924602 : return tem;
10879 : }
10880 : }
10881 :
10882 : /* If this is a commutative operation, and ARG0 is a constant, move it
10883 : to ARG1 to reduce the number of tests below. */
10884 691608729 : if (commutative_tree_code (code)
10885 691608729 : && tree_swap_operands_p (arg0, arg1))
10886 33147639 : return fold_build2_loc (loc, code, type, op1, op0);
10887 :
10888 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10889 : to ARG1 to reduce the number of tests below. */
10890 658461090 : if (kind == tcc_comparison
10891 658461090 : && tree_swap_operands_p (arg0, arg1))
10892 8343284 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10893 :
10894 650117806 : tem = generic_simplify (loc, code, type, op0, op1);
10895 650117806 : if (tem)
10896 : return tem;
10897 :
10898 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10899 :
10900 : First check for cases where an arithmetic operation is applied to a
10901 : compound, conditional, or comparison operation. Push the arithmetic
10902 : operation inside the compound or conditional to see if any folding
10903 : can then be done. Convert comparison to conditional for this purpose.
10904 : The also optimizes non-constant cases that used to be done in
10905 : expand_expr.
10906 :
10907 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10908 : one of the operands is a comparison and the other is a comparison, a
10909 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10910 : code below would make the expression more complex. Change it to a
10911 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10912 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10913 :
10914 549745764 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10915 : || code == EQ_EXPR || code == NE_EXPR)
10916 58037607 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10917 57451360 : && ((truth_value_p (TREE_CODE (arg0))
10918 1213367 : && (truth_value_p (TREE_CODE (arg1))
10919 895458 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10920 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10921 57133435 : || (truth_value_p (TREE_CODE (arg1))
10922 6822 : && (truth_value_p (TREE_CODE (arg0))
10923 6822 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10924 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10925 : {
10926 397207 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10927 79268 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10928 : : TRUTH_XOR_EXPR,
10929 : boolean_type_node,
10930 : fold_convert_loc (loc, boolean_type_node, arg0),
10931 : fold_convert_loc (loc, boolean_type_node, arg1));
10932 :
10933 317939 : if (code == EQ_EXPR)
10934 72745 : tem = invert_truthvalue_loc (loc, tem);
10935 :
10936 317939 : return fold_convert_loc (loc, type, tem);
10937 : }
10938 :
10939 549427825 : if (TREE_CODE_CLASS (code) == tcc_binary
10940 300910584 : || TREE_CODE_CLASS (code) == tcc_comparison)
10941 : {
10942 344548152 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
10943 : {
10944 82864 : tem = fold_build2_loc (loc, code, type,
10945 82864 : fold_convert_loc (loc, TREE_TYPE (op0),
10946 82864 : TREE_OPERAND (arg0, 1)), op1);
10947 82864 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10948 82864 : tem);
10949 : }
10950 344465288 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
10951 : {
10952 3161 : tem = fold_build2_loc (loc, code, type, op0,
10953 3161 : fold_convert_loc (loc, TREE_TYPE (op1),
10954 3161 : TREE_OPERAND (arg1, 1)));
10955 3161 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10956 3161 : tem);
10957 : }
10958 :
10959 344462127 : if (TREE_CODE (arg0) == COND_EXPR
10960 344085390 : || TREE_CODE (arg0) == VEC_COND_EXPR
10961 344082132 : || COMPARISON_CLASS_P (arg0))
10962 : {
10963 708210 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10964 : arg0, arg1,
10965 : /*cond_first_p=*/1);
10966 708210 : if (tem != NULL_TREE)
10967 : return tem;
10968 : }
10969 :
10970 343981649 : if (TREE_CODE (arg1) == COND_EXPR
10971 343741156 : || TREE_CODE (arg1) == VEC_COND_EXPR
10972 343740686 : || COMPARISON_CLASS_P (arg1))
10973 : {
10974 253575 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10975 : arg1, arg0,
10976 : /*cond_first_p=*/0);
10977 253575 : if (tem != NULL_TREE)
10978 : return tem;
10979 : }
10980 : }
10981 :
10982 548853233 : switch (code)
10983 : {
10984 63686891 : case MEM_REF:
10985 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10986 63686891 : if (TREE_CODE (arg0) == ADDR_EXPR
10987 63686891 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10988 : {
10989 837320 : tree iref = TREE_OPERAND (arg0, 0);
10990 837320 : return fold_build2 (MEM_REF, type,
10991 : TREE_OPERAND (iref, 0),
10992 : int_const_binop (PLUS_EXPR, arg1,
10993 : TREE_OPERAND (iref, 1)));
10994 : }
10995 :
10996 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10997 62849571 : if (TREE_CODE (arg0) == ADDR_EXPR
10998 62849571 : && handled_component_p (TREE_OPERAND (arg0, 0)))
10999 : {
11000 6165538 : tree base;
11001 6165538 : poly_int64 coffset;
11002 6165538 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11003 : &coffset);
11004 6165538 : if (!base)
11005 : return NULL_TREE;
11006 6161503 : return fold_build2 (MEM_REF, type,
11007 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11008 : int_const_binop (PLUS_EXPR, arg1,
11009 : size_int (coffset)));
11010 : }
11011 :
11012 : return NULL_TREE;
11013 :
11014 71606836 : case POINTER_PLUS_EXPR:
11015 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11016 143213256 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11017 143204477 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11018 32951 : return fold_convert_loc (loc, type,
11019 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11020 : fold_convert_loc (loc, sizetype,
11021 : arg1),
11022 : fold_convert_loc (loc, sizetype,
11023 32951 : arg0)));
11024 :
11025 : return NULL_TREE;
11026 :
11027 63264599 : case PLUS_EXPR:
11028 63264599 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11029 : {
11030 : /* X + (X / CST) * -CST is X % CST. */
11031 51794651 : if (TREE_CODE (arg1) == MULT_EXPR
11032 2318499 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11033 51800904 : && operand_equal_p (arg0,
11034 6253 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11035 : {
11036 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11037 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11038 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11039 : cst1, cst0);
11040 204 : if (sum && integer_zerop (sum))
11041 204 : return fold_convert_loc (loc, type,
11042 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11043 204 : TREE_TYPE (arg0), arg0,
11044 204 : cst0));
11045 : }
11046 : }
11047 :
11048 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11049 : one. Make sure the type is not saturating and has the signedness of
11050 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11051 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11052 63264395 : if ((TREE_CODE (arg0) == MULT_EXPR
11053 51274053 : || TREE_CODE (arg1) == MULT_EXPR)
11054 13324489 : && !TYPE_SATURATING (type)
11055 13324489 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11056 12931003 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11057 75492140 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11058 : {
11059 8956872 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11060 8956872 : if (tem)
11061 : return tem;
11062 : }
11063 :
11064 61896910 : if (! FLOAT_TYPE_P (type))
11065 : {
11066 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11067 : (plus (plus (mult) (mult)) (foo)) so that we can
11068 : take advantage of the factoring cases below. */
11069 282508 : if (ANY_INTEGRAL_TYPE_P (type)
11070 50429326 : && TYPE_OVERFLOW_WRAPS (type)
11071 50429326 : && (((TREE_CODE (arg0) == PLUS_EXPR
11072 31633755 : || TREE_CODE (arg0) == MINUS_EXPR)
11073 3428999 : && TREE_CODE (arg1) == MULT_EXPR)
11074 31128994 : || ((TREE_CODE (arg1) == PLUS_EXPR
11075 31128994 : || TREE_CODE (arg1) == MINUS_EXPR)
11076 427239 : && TREE_CODE (arg0) == MULT_EXPR)))
11077 : {
11078 551116 : tree parg0, parg1, parg, marg;
11079 551116 : enum tree_code pcode;
11080 :
11081 551116 : if (TREE_CODE (arg1) == MULT_EXPR)
11082 : parg = arg0, marg = arg1;
11083 : else
11084 46355 : parg = arg1, marg = arg0;
11085 551116 : pcode = TREE_CODE (parg);
11086 551116 : parg0 = TREE_OPERAND (parg, 0);
11087 551116 : parg1 = TREE_OPERAND (parg, 1);
11088 551116 : STRIP_NOPS (parg0);
11089 551116 : STRIP_NOPS (parg1);
11090 :
11091 551116 : if (TREE_CODE (parg0) == MULT_EXPR
11092 266745 : && TREE_CODE (parg1) != MULT_EXPR)
11093 232637 : return fold_build2_loc (loc, pcode, type,
11094 : fold_build2_loc (loc, PLUS_EXPR, type,
11095 : fold_convert_loc (loc, type,
11096 : parg0),
11097 : fold_convert_loc (loc, type,
11098 : marg)),
11099 232637 : fold_convert_loc (loc, type, parg1));
11100 318479 : if (TREE_CODE (parg0) != MULT_EXPR
11101 284371 : && TREE_CODE (parg1) == MULT_EXPR)
11102 100310 : return
11103 100310 : fold_build2_loc (loc, PLUS_EXPR, type,
11104 : fold_convert_loc (loc, type, parg0),
11105 : fold_build2_loc (loc, pcode, type,
11106 : fold_convert_loc (loc, type, marg),
11107 : fold_convert_loc (loc, type,
11108 100310 : parg1)));
11109 : }
11110 : }
11111 : else
11112 : {
11113 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11114 : to __complex__ ( x, y ). This is not the same for SNaNs or
11115 : if signed zeros are involved. */
11116 11467584 : if (!HONOR_SNANS (arg0)
11117 11465924 : && !HONOR_SIGNED_ZEROS (arg0)
11118 11488818 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11119 : {
11120 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11121 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11122 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11123 3086 : bool arg0rz = false, arg0iz = false;
11124 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11125 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11126 : {
11127 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11128 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11129 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11130 : {
11131 22 : tree rp = arg1r ? arg1r
11132 0 : : build1 (REALPART_EXPR, rtype, arg1);
11133 22 : tree ip = arg0i ? arg0i
11134 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11135 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11136 : }
11137 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11138 : {
11139 53 : tree rp = arg0r ? arg0r
11140 0 : : build1 (REALPART_EXPR, rtype, arg0);
11141 53 : tree ip = arg1i ? arg1i
11142 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11143 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11144 : }
11145 : }
11146 : }
11147 :
11148 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11149 : We associate floats only if the user has specified
11150 : -fassociative-math. */
11151 11467509 : if (flag_associative_math
11152 21138 : && TREE_CODE (arg1) == PLUS_EXPR
11153 36 : && TREE_CODE (arg0) != MULT_EXPR)
11154 : {
11155 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11156 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11157 21 : if (TREE_CODE (tree11) == MULT_EXPR
11158 5 : && TREE_CODE (tree10) == MULT_EXPR)
11159 : {
11160 1 : tree tree0;
11161 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11162 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11163 : }
11164 : }
11165 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11166 : We associate floats only if the user has specified
11167 : -fassociative-math. */
11168 11467508 : if (flag_associative_math
11169 21137 : && TREE_CODE (arg0) == PLUS_EXPR
11170 1219 : && TREE_CODE (arg1) != MULT_EXPR)
11171 : {
11172 831 : tree tree00 = TREE_OPERAND (arg0, 0);
11173 831 : tree tree01 = TREE_OPERAND (arg0, 1);
11174 831 : if (TREE_CODE (tree01) == MULT_EXPR
11175 49 : && TREE_CODE (tree00) == MULT_EXPR)
11176 : {
11177 9 : tree tree0;
11178 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11179 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11180 : }
11181 : }
11182 : }
11183 :
11184 11466677 : bit_rotate:
11185 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11186 : is a rotate of A by C1 bits. */
11187 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11188 : is a rotate of A by B bits.
11189 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11190 : though in this case CODE must be | and not + or ^, otherwise
11191 : it doesn't return A when B is 0. */
11192 64434787 : {
11193 64434787 : enum tree_code code0, code1;
11194 64434787 : tree rtype;
11195 64434787 : code0 = TREE_CODE (arg0);
11196 64434787 : code1 = TREE_CODE (arg1);
11197 73937 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11198 64418438 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11199 39703 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11200 39703 : TREE_OPERAND (arg1, 0), 0)
11201 36934 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11202 36934 : TYPE_UNSIGNED (rtype))
11203 : /* Only create rotates in complete modes. Other cases are not
11204 : expanded properly. */
11205 64461623 : && (element_precision (rtype)
11206 53672 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11207 : {
11208 26763 : tree tree01, tree11;
11209 26763 : tree orig_tree01, orig_tree11;
11210 26763 : enum tree_code code01, code11;
11211 :
11212 26763 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11213 26763 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11214 26763 : STRIP_NOPS (tree01);
11215 26763 : STRIP_NOPS (tree11);
11216 26763 : code01 = TREE_CODE (tree01);
11217 26763 : code11 = TREE_CODE (tree11);
11218 26763 : if (code11 != MINUS_EXPR
11219 26077 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11220 : {
11221 1462 : std::swap (code0, code1);
11222 1462 : std::swap (code01, code11);
11223 1462 : std::swap (tree01, tree11);
11224 1462 : std::swap (orig_tree01, orig_tree11);
11225 : }
11226 53526 : if (code01 == INTEGER_CST
11227 3152 : && code11 == INTEGER_CST
11228 33065 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11229 33065 : == element_precision (rtype)))
11230 : {
11231 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11232 3011 : rtype, TREE_OPERAND (arg0, 0),
11233 : code0 == LSHIFT_EXPR
11234 : ? orig_tree01 : orig_tree11);
11235 3011 : return fold_convert_loc (loc, type, tem);
11236 : }
11237 23752 : else if (code11 == MINUS_EXPR)
11238 : {
11239 941 : tree tree110, tree111;
11240 941 : tree110 = TREE_OPERAND (tree11, 0);
11241 941 : tree111 = TREE_OPERAND (tree11, 1);
11242 941 : STRIP_NOPS (tree110);
11243 941 : STRIP_NOPS (tree111);
11244 941 : if (TREE_CODE (tree110) == INTEGER_CST
11245 930 : && compare_tree_int (tree110,
11246 930 : element_precision (rtype)) == 0
11247 1855 : && operand_equal_p (tree01, tree111, 0))
11248 : {
11249 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11250 : ? LROTATE_EXPR : RROTATE_EXPR),
11251 558 : rtype, TREE_OPERAND (arg0, 0),
11252 : orig_tree01);
11253 558 : return fold_convert_loc (loc, type, tem);
11254 : }
11255 : }
11256 22811 : else if (code == BIT_IOR_EXPR
11257 21697 : && code11 == BIT_AND_EXPR
11258 44433 : && pow2p_hwi (element_precision (rtype)))
11259 : {
11260 21622 : tree tree110, tree111;
11261 21622 : tree110 = TREE_OPERAND (tree11, 0);
11262 21622 : tree111 = TREE_OPERAND (tree11, 1);
11263 21622 : STRIP_NOPS (tree110);
11264 21622 : STRIP_NOPS (tree111);
11265 21622 : if (TREE_CODE (tree110) == NEGATE_EXPR
11266 21167 : && TREE_CODE (tree111) == INTEGER_CST
11267 21167 : && compare_tree_int (tree111,
11268 21167 : element_precision (rtype) - 1) == 0
11269 42775 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11270 : {
11271 31601 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11272 : ? LROTATE_EXPR : RROTATE_EXPR),
11273 21091 : rtype, TREE_OPERAND (arg0, 0),
11274 : orig_tree01);
11275 21091 : return fold_convert_loc (loc, type, tem);
11276 : }
11277 : }
11278 : }
11279 : }
11280 :
11281 157358822 : associate:
11282 : /* In most languages, can't associate operations on floats through
11283 : parentheses. Rather than remember where the parentheses were, we
11284 : don't associate floats at all, unless the user has specified
11285 : -fassociative-math.
11286 : And, we need to make sure type is not saturating. */
11287 :
11288 157358822 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11289 116689232 : && !TYPE_SATURATING (type)
11290 274048054 : && !TYPE_OVERFLOW_SANITIZED (type))
11291 : {
11292 116660900 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11293 116660900 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11294 116660900 : tree atype = type;
11295 116660900 : bool ok = true;
11296 :
11297 : /* Split both trees into variables, constants, and literals. Then
11298 : associate each group together, the constants with literals,
11299 : then the result with variables. This increases the chances of
11300 : literals being recombined later and of generating relocatable
11301 : expressions for the sum of a constant and literal. */
11302 116660900 : var0 = split_tree (arg0, type, code,
11303 : &minus_var0, &con0, &minus_con0,
11304 : &lit0, &minus_lit0, 0);
11305 116660900 : var1 = split_tree (arg1, type, code,
11306 : &minus_var1, &con1, &minus_con1,
11307 : &lit1, &minus_lit1, code == MINUS_EXPR);
11308 :
11309 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11310 116660900 : if (code == MINUS_EXPR)
11311 12801166 : code = PLUS_EXPR;
11312 :
11313 : /* With undefined overflow prefer doing association in a type
11314 : which wraps on overflow, if that is one of the operand types. */
11315 116660669 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11316 232074006 : && !TYPE_OVERFLOW_WRAPS (type))
11317 : {
11318 62176030 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11319 61528654 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11320 880677 : atype = TREE_TYPE (arg0);
11321 60368759 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11322 60121176 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11323 243956 : atype = TREE_TYPE (arg1);
11324 31340838 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11325 : }
11326 :
11327 : /* With undefined overflow we can only associate constants with one
11328 : variable, and constants whose association doesn't overflow. */
11329 116660669 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11330 232074006 : && !TYPE_OVERFLOW_WRAPS (atype))
11331 : {
11332 30216205 : if ((var0 && var1) || (minus_var0 && minus_var1))
11333 : {
11334 : /* ??? If split_tree would handle NEGATE_EXPR we could
11335 : simply reject these cases and the allowed cases would
11336 : be the var0/minus_var1 ones. */
11337 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11338 5631284 : tree tmp1 = var1 ? var1 : minus_var1;
11339 5631284 : bool one_neg = false;
11340 :
11341 5631284 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11342 : {
11343 737 : tmp0 = TREE_OPERAND (tmp0, 0);
11344 737 : one_neg = !one_neg;
11345 : }
11346 4984034 : if (CONVERT_EXPR_P (tmp0)
11347 672799 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11348 6303132 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11349 671848 : <= TYPE_PRECISION (atype)))
11350 659325 : tmp0 = TREE_OPERAND (tmp0, 0);
11351 5631284 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11352 : {
11353 168 : tmp1 = TREE_OPERAND (tmp1, 0);
11354 168 : one_neg = !one_neg;
11355 : }
11356 5305730 : if (CONVERT_EXPR_P (tmp1)
11357 393563 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11358 6024719 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11359 393435 : <= TYPE_PRECISION (atype)))
11360 376407 : tmp1 = TREE_OPERAND (tmp1, 0);
11361 : /* The only case we can still associate with two variables
11362 : is if they cancel out. */
11363 5631284 : if (!one_neg
11364 5631284 : || !operand_equal_p (tmp0, tmp1, 0))
11365 : ok = false;
11366 : }
11367 24185465 : else if ((var0 && minus_var1
11368 4104846 : && ! operand_equal_p (var0, minus_var1, 0))
11369 44665541 : || (minus_var0 && var1
11370 11400 : && ! operand_equal_p (minus_var0, var1, 0)))
11371 : ok = false;
11372 : }
11373 :
11374 : /* Only do something if we found more than two objects. Otherwise,
11375 : nothing has changed and we risk infinite recursion. */
11376 : if (ok
11377 106913443 : && ((var0 != 0) + (var1 != 0)
11378 106913443 : + (minus_var0 != 0) + (minus_var1 != 0)
11379 106913443 : + (con0 != 0) + (con1 != 0)
11380 106913443 : + (minus_con0 != 0) + (minus_con1 != 0)
11381 106913443 : + (lit0 != 0) + (lit1 != 0)
11382 106913443 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11383 : {
11384 2035478 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11385 4070956 : int minus_var0_origin
11386 2035478 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11387 2035478 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11388 4070956 : int minus_con0_origin
11389 2035478 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11390 2035478 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11391 4070956 : int minus_lit0_origin
11392 2035478 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11393 2035478 : var0 = associate_trees (loc, var0, var1, code, atype);
11394 2035478 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11395 : code, atype);
11396 2035478 : con0 = associate_trees (loc, con0, con1, code, atype);
11397 2035478 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11398 : code, atype);
11399 2035478 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11400 2035478 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11401 : code, atype);
11402 :
11403 2035478 : if (minus_var0 && var0)
11404 : {
11405 1337494 : var0_origin |= minus_var0_origin;
11406 1337494 : var0 = associate_trees (loc, var0, minus_var0,
11407 : MINUS_EXPR, atype);
11408 1337494 : minus_var0 = 0;
11409 1337494 : minus_var0_origin = 0;
11410 : }
11411 2035478 : if (minus_con0 && con0)
11412 : {
11413 3690 : con0_origin |= minus_con0_origin;
11414 3690 : con0 = associate_trees (loc, con0, minus_con0,
11415 : MINUS_EXPR, atype);
11416 3690 : minus_con0 = 0;
11417 3690 : minus_con0_origin = 0;
11418 : }
11419 :
11420 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11421 : greater than the positive part. Otherwise, the multiplicative
11422 : folding code (i.e extract_muldiv) may be fooled in case
11423 : unsigned constants are subtracted, like in the following
11424 : example: ((X*2 + 4) - 8U)/2. */
11425 2035478 : if (minus_lit0 && lit0)
11426 : {
11427 236818 : if (TREE_CODE (lit0) == INTEGER_CST
11428 236818 : && TREE_CODE (minus_lit0) == INTEGER_CST
11429 236818 : && tree_int_cst_lt (lit0, minus_lit0)
11430 : /* But avoid ending up with only negated parts. */
11431 298214 : && (var0 || con0))
11432 : {
11433 56900 : minus_lit0_origin |= lit0_origin;
11434 56900 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11435 : MINUS_EXPR, atype);
11436 56900 : lit0 = 0;
11437 56900 : lit0_origin = 0;
11438 : }
11439 : else
11440 : {
11441 179918 : lit0_origin |= minus_lit0_origin;
11442 179918 : lit0 = associate_trees (loc, lit0, minus_lit0,
11443 : MINUS_EXPR, atype);
11444 179918 : minus_lit0 = 0;
11445 179918 : minus_lit0_origin = 0;
11446 : }
11447 : }
11448 :
11449 : /* Don't introduce overflows through reassociation. */
11450 1347791 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11451 3383231 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11452 2035478 : return NULL_TREE;
11453 :
11454 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11455 2035440 : con0_origin |= lit0_origin;
11456 2035440 : con0 = associate_trees (loc, con0, lit0, code, atype);
11457 2035440 : minus_con0_origin |= minus_lit0_origin;
11458 2035440 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11459 : code, atype);
11460 :
11461 : /* Eliminate minus_con0. */
11462 2035440 : if (minus_con0)
11463 : {
11464 692536 : if (con0)
11465 : {
11466 15903 : con0_origin |= minus_con0_origin;
11467 15903 : con0 = associate_trees (loc, con0, minus_con0,
11468 : MINUS_EXPR, atype);
11469 : }
11470 676633 : else if (var0)
11471 : {
11472 676633 : var0_origin |= minus_con0_origin;
11473 676633 : var0 = associate_trees (loc, var0, minus_con0,
11474 : MINUS_EXPR, atype);
11475 : }
11476 : else
11477 0 : gcc_unreachable ();
11478 : }
11479 :
11480 : /* Eliminate minus_var0. */
11481 2035440 : if (minus_var0)
11482 : {
11483 347321 : if (con0)
11484 : {
11485 347321 : con0_origin |= minus_var0_origin;
11486 347321 : con0 = associate_trees (loc, con0, minus_var0,
11487 : MINUS_EXPR, atype);
11488 : }
11489 : else
11490 0 : gcc_unreachable ();
11491 : }
11492 :
11493 : /* Reassociate only if there has been any actual association
11494 : between subtrees from op0 and subtrees from op1 in at
11495 : least one of the operands, otherwise we risk infinite
11496 : recursion. See PR114084. */
11497 2035440 : if (var0_origin != 3 && con0_origin != 3)
11498 : return NULL_TREE;
11499 :
11500 2033817 : return
11501 2033817 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11502 2033817 : code, atype));
11503 : }
11504 : }
11505 :
11506 : return NULL_TREE;
11507 :
11508 23525216 : case POINTER_DIFF_EXPR:
11509 23525216 : case MINUS_EXPR:
11510 : /* Fold &a[i] - &a[j] to i-j. */
11511 23525216 : if (TREE_CODE (arg0) == ADDR_EXPR
11512 48067 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11513 6199 : && TREE_CODE (arg1) == ADDR_EXPR
11514 23525824 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11515 : {
11516 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11517 38 : TREE_OPERAND (arg0, 0),
11518 38 : TREE_OPERAND (arg1, 0),
11519 : code
11520 : == POINTER_DIFF_EXPR);
11521 38 : if (tem)
11522 : return tem;
11523 : }
11524 :
11525 : /* Further transformations are not for pointers. */
11526 23525202 : if (code == POINTER_DIFF_EXPR)
11527 : return NULL_TREE;
11528 :
11529 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11530 20787773 : if (TREE_CODE (arg0) == NEGATE_EXPR
11531 144642 : && negate_expr_p (op1)
11532 : /* If arg0 is e.g. unsigned int and type is int, then this could
11533 : introduce UB, because if A is INT_MIN at runtime, the original
11534 : expression can be well defined while the latter is not.
11535 : See PR83269. */
11536 20788602 : && !(ANY_INTEGRAL_TYPE_P (type)
11537 829 : && TYPE_OVERFLOW_UNDEFINED (type)
11538 817 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11539 817 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11540 822 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11541 : fold_convert_loc (loc, type,
11542 1644 : TREE_OPERAND (arg0, 0)));
11543 :
11544 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11545 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11546 : signed zeros are involved. */
11547 20786951 : if (!HONOR_SNANS (arg0)
11548 20785800 : && !HONOR_SIGNED_ZEROS (arg0)
11549 34394004 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11550 : {
11551 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11552 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11553 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11554 53 : bool arg0rz = false, arg0iz = false;
11555 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11556 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11557 : {
11558 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11559 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11560 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11561 : {
11562 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11563 : arg1r ? arg1r
11564 0 : : build1 (REALPART_EXPR, rtype, arg1));
11565 9 : tree ip = arg0i ? arg0i
11566 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11567 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11568 : }
11569 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11570 : {
11571 15 : tree rp = arg0r ? arg0r
11572 0 : : build1 (REALPART_EXPR, rtype, arg0);
11573 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11574 : arg1i ? arg1i
11575 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11576 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11577 : }
11578 : }
11579 : }
11580 :
11581 : /* A - B -> A + (-B) if B is easily negatable. */
11582 20786927 : if (negate_expr_p (op1)
11583 751275 : && ! TYPE_OVERFLOW_SANITIZED (type)
11584 21535675 : && ((FLOAT_TYPE_P (type)
11585 : /* Avoid this transformation if B is a positive REAL_CST. */
11586 65 : && (TREE_CODE (op1) != REAL_CST
11587 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11588 748683 : || INTEGRAL_TYPE_P (type)))
11589 748537 : return fold_build2_loc (loc, PLUS_EXPR, type,
11590 : fold_convert_loc (loc, type, arg0),
11591 748537 : negate_expr (op1));
11592 :
11593 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11594 : one. Make sure the type is not saturating and has the signedness of
11595 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11596 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11597 20038390 : if ((TREE_CODE (arg0) == MULT_EXPR
11598 18743989 : || TREE_CODE (arg1) == MULT_EXPR)
11599 2615579 : && !TYPE_SATURATING (type)
11600 2615579 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11601 2475610 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11602 22459226 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11603 : {
11604 347936 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11605 347936 : if (tem)
11606 : return tem;
11607 : }
11608 :
11609 19988754 : goto associate;
11610 :
11611 66747541 : case MULT_EXPR:
11612 66747541 : if (! FLOAT_TYPE_P (type))
11613 : {
11614 : /* Transform x * -C into -x * C if x is easily negatable. */
11615 44674316 : if (TREE_CODE (op1) == INTEGER_CST
11616 41625510 : && tree_int_cst_sgn (op1) == -1
11617 220869 : && negate_expr_p (op0)
11618 340 : && negate_expr_p (op1)
11619 324 : && (tem = negate_expr (op1)) != op1
11620 44674640 : && ! TREE_OVERFLOW (tem))
11621 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11622 : fold_convert_loc (loc, type,
11623 324 : negate_expr (op0)), tem);
11624 :
11625 44673992 : if (TREE_CODE (arg1) == INTEGER_CST
11626 44673992 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11627 : {
11628 729802 : return fold_convert_loc (loc, type, tem);
11629 : }
11630 :
11631 : /* Optimize z * conj(z) for integer complex numbers. */
11632 43944190 : if (TREE_CODE (arg0) == CONJ_EXPR
11633 43944190 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11634 1 : return fold_mult_zconjz (loc, type, arg1);
11635 43944189 : if (TREE_CODE (arg1) == CONJ_EXPR
11636 43944189 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11637 0 : return fold_mult_zconjz (loc, type, arg0);
11638 : }
11639 : else
11640 : {
11641 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11642 : This is not the same for NaNs or if signed zeros are
11643 : involved. */
11644 22073225 : if (!HONOR_NANS (arg0)
11645 32883 : && !HONOR_SIGNED_ZEROS (arg0)
11646 32582 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11647 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11648 22073450 : && real_zerop (TREE_REALPART (arg1)))
11649 : {
11650 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11651 218 : if (real_onep (TREE_IMAGPART (arg1)))
11652 : {
11653 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11654 63 : arg0 = save_expr (arg0);
11655 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11656 : rtype, arg0);
11657 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11658 : rtype, arg0);
11659 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11660 : negate_expr (iarg0),
11661 208 : rarg0);
11662 : }
11663 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11664 : {
11665 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11666 0 : arg0 = save_expr (arg0);
11667 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11668 : rtype, arg0);
11669 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11670 : rtype, arg0);
11671 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11672 : iarg0,
11673 10 : negate_expr (rarg0));
11674 : }
11675 : }
11676 :
11677 : /* Optimize z * conj(z) for floating point complex numbers.
11678 : Guarded by flag_unsafe_math_optimizations as non-finite
11679 : imaginary components don't produce scalar results. */
11680 22073007 : if (flag_unsafe_math_optimizations
11681 32411 : && TREE_CODE (arg0) == CONJ_EXPR
11682 22073009 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11683 1 : return fold_mult_zconjz (loc, type, arg1);
11684 22073006 : if (flag_unsafe_math_optimizations
11685 32410 : && TREE_CODE (arg1) == CONJ_EXPR
11686 22073010 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11687 0 : return fold_mult_zconjz (loc, type, arg0);
11688 : }
11689 66017195 : goto associate;
11690 :
11691 1916656 : case BIT_IOR_EXPR:
11692 : /* Canonicalize (X & C1) | C2. */
11693 1916656 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11694 89343 : && TREE_CODE (arg1) == INTEGER_CST
11695 1960554 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11696 : {
11697 43890 : int width = TYPE_PRECISION (type), w;
11698 43890 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11699 43890 : wide_int c2 = wi::to_wide (arg1);
11700 :
11701 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11702 43890 : if ((c1 & c2) == c1)
11703 0 : return omit_one_operand_loc (loc, type, arg1,
11704 0 : TREE_OPERAND (arg0, 0));
11705 :
11706 43890 : wide_int msk = wi::mask (width, false,
11707 43890 : TYPE_PRECISION (TREE_TYPE (arg1)));
11708 :
11709 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11710 43890 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11711 : {
11712 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11713 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11714 : }
11715 :
11716 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11717 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11718 : mode which allows further optimizations. */
11719 43884 : c1 &= msk;
11720 43884 : c2 &= msk;
11721 43884 : wide_int c3 = wi::bit_and_not (c1, c2);
11722 137866 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11723 : {
11724 94224 : wide_int mask = wi::mask (w, false,
11725 94224 : TYPE_PRECISION (type));
11726 188448 : if (((c1 | c2) & mask) == mask
11727 188448 : && wi::bit_and_not (c1, mask) == 0)
11728 : {
11729 242 : c3 = mask;
11730 242 : break;
11731 : }
11732 94224 : }
11733 :
11734 43884 : if (c3 != c1)
11735 : {
11736 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11737 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11738 558 : wide_int_to_tree (type, c3));
11739 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11740 : }
11741 45012 : }
11742 :
11743 : /* See if this can be simplified into a rotate first. If that
11744 : is unsuccessful continue in the association code. */
11745 1916092 : goto bit_rotate;
11746 :
11747 954817 : case BIT_XOR_EXPR:
11748 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11749 954817 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11750 3692 : && INTEGRAL_TYPE_P (type)
11751 3087 : && integer_onep (TREE_OPERAND (arg0, 1))
11752 956058 : && integer_onep (arg1))
11753 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11754 0 : build_zero_cst (TREE_TYPE (arg0)));
11755 :
11756 : /* See if this can be simplified into a rotate first. If that
11757 : is unsuccessful continue in the association code. */
11758 954817 : goto bit_rotate;
11759 :
11760 6514590 : case BIT_AND_EXPR:
11761 : /* Fold !X & 1 as X == 0. */
11762 6514590 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11763 6514590 : && integer_onep (arg1))
11764 : {
11765 0 : tem = TREE_OPERAND (arg0, 0);
11766 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11767 0 : build_zero_cst (TREE_TYPE (tem)));
11768 : }
11769 :
11770 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11771 : multiple of 1 << CST. */
11772 6514590 : if (TREE_CODE (arg1) == INTEGER_CST)
11773 : {
11774 4652858 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11775 4652858 : wide_int ncst1 = -cst1;
11776 4652858 : if ((cst1 & ncst1) == ncst1
11777 4814400 : && multiple_of_p (type, arg0,
11778 4814400 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11779 467 : return fold_convert_loc (loc, type, arg0);
11780 4652858 : }
11781 :
11782 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11783 : bits from CST2. */
11784 6514123 : if (TREE_CODE (arg1) == INTEGER_CST
11785 4652391 : && TREE_CODE (arg0) == MULT_EXPR
11786 6648019 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11787 : {
11788 133834 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11789 133834 : wide_int masked
11790 133834 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11791 :
11792 133834 : if (masked == 0)
11793 5202 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11794 5202 : arg0, arg1);
11795 128632 : else if (masked != warg1)
11796 : {
11797 : /* Avoid the transform if arg1 is a mask of some
11798 : mode which allows further optimizations. */
11799 648 : int pop = wi::popcount (warg1);
11800 670 : if (!(pop >= BITS_PER_UNIT
11801 50 : && pow2p_hwi (pop)
11802 692 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11803 1252 : return fold_build2_loc (loc, code, type, op0,
11804 1252 : wide_int_to_tree (type, masked));
11805 : }
11806 133834 : }
11807 :
11808 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11809 4646563 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11810 6717712 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11811 : {
11812 113251 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11813 :
11814 113251 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11815 113251 : if (mask == -1)
11816 2427 : return
11817 2427 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11818 113251 : }
11819 :
11820 6505868 : goto associate;
11821 :
11822 6055679 : case RDIV_EXPR:
11823 : /* Don't touch a floating-point divide by zero unless the mode
11824 : of the constant can represent infinity. */
11825 6055679 : if (TREE_CODE (arg1) == REAL_CST
11826 3034985 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11827 6055679 : && real_zerop (arg1))
11828 0 : return NULL_TREE;
11829 :
11830 : /* (-A) / (-B) -> A / B */
11831 6055679 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11832 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11833 3 : TREE_OPERAND (arg0, 0),
11834 3 : negate_expr (arg1));
11835 6055676 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11836 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11837 : negate_expr (arg0),
11838 0 : TREE_OPERAND (arg1, 0));
11839 : return NULL_TREE;
11840 :
11841 2131732 : case TRUNC_DIV_EXPR:
11842 : /* Fall through */
11843 :
11844 2131732 : case FLOOR_DIV_EXPR:
11845 : /* Simplify A / (B << N) where A and B are positive and B is
11846 : a power of 2, to A >> (N + log2(B)). */
11847 2131732 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11848 2131732 : && (TYPE_UNSIGNED (type)
11849 8 : || tree_expr_nonnegative_p (op0)))
11850 : {
11851 17 : tree sval = TREE_OPERAND (arg1, 0);
11852 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11853 : {
11854 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11855 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11856 16 : wi::exact_log2 (wi::to_wide (sval)));
11857 :
11858 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11859 : sh_cnt, pow2);
11860 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11861 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11862 : }
11863 : }
11864 :
11865 : /* Fall through */
11866 :
11867 3525818 : case ROUND_DIV_EXPR:
11868 3525818 : case CEIL_DIV_EXPR:
11869 3525818 : case EXACT_DIV_EXPR:
11870 3525818 : if (integer_zerop (arg1))
11871 : return NULL_TREE;
11872 :
11873 : /* Convert -A / -B to A / B when the type is signed and overflow is
11874 : undefined. */
11875 3522974 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11876 1000227 : && TREE_CODE (op0) == NEGATE_EXPR
11877 3523036 : && negate_expr_p (op1))
11878 60 : return fold_build2_loc (loc, code, type,
11879 : fold_convert_loc (loc, type,
11880 30 : TREE_OPERAND (arg0, 0)),
11881 30 : negate_expr (op1));
11882 3522944 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11883 1000197 : && TREE_CODE (arg1) == NEGATE_EXPR
11884 3523188 : && negate_expr_p (op0))
11885 36 : return fold_build2_loc (loc, code, type,
11886 : negate_expr (op0),
11887 : fold_convert_loc (loc, type,
11888 72 : TREE_OPERAND (arg1, 0)));
11889 :
11890 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11891 : operation, EXACT_DIV_EXPR.
11892 :
11893 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11894 : At one time others generated faster code, it's not clear if they do
11895 : after the last round to changes to the DIV code in expmed.cc. */
11896 3522908 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11897 3522908 : && multiple_of_p (type, arg0, arg1))
11898 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11899 : fold_convert (type, arg0),
11900 0 : fold_convert (type, arg1));
11901 :
11902 3522908 : if (TREE_CODE (arg1) == INTEGER_CST
11903 3522908 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11904 9462 : return fold_convert_loc (loc, type, tem);
11905 :
11906 : return NULL_TREE;
11907 :
11908 925563 : case CEIL_MOD_EXPR:
11909 925563 : case FLOOR_MOD_EXPR:
11910 925563 : case ROUND_MOD_EXPR:
11911 925563 : case TRUNC_MOD_EXPR:
11912 925563 : if (TREE_CODE (arg1) == INTEGER_CST
11913 925563 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11914 0 : return fold_convert_loc (loc, type, tem);
11915 :
11916 : return NULL_TREE;
11917 :
11918 2242098 : case LROTATE_EXPR:
11919 2242098 : case RROTATE_EXPR:
11920 2242098 : case RSHIFT_EXPR:
11921 2242098 : case LSHIFT_EXPR:
11922 : /* Since negative shift count is not well-defined,
11923 : don't try to compute it in the compiler. */
11924 2242098 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11925 : return NULL_TREE;
11926 :
11927 2241057 : prec = element_precision (type);
11928 :
11929 : /* If we have a rotate of a bit operation with the rotate count and
11930 : the second operand of the bit operation both constant,
11931 : permute the two operations. */
11932 2754 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11933 2224 : && (TREE_CODE (arg0) == BIT_AND_EXPR
11934 2224 : || TREE_CODE (arg0) == BIT_IOR_EXPR
11935 2224 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
11936 2241057 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11937 : {
11938 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11939 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11940 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
11941 : fold_build2_loc (loc, code, type,
11942 : arg00, arg1),
11943 : fold_build2_loc (loc, code, type,
11944 0 : arg01, arg1));
11945 : }
11946 :
11947 : return NULL_TREE;
11948 :
11949 436878 : case MIN_EXPR:
11950 436878 : case MAX_EXPR:
11951 436878 : goto associate;
11952 :
11953 6623161 : case TRUTH_ANDIF_EXPR:
11954 : /* Note that the operands of this must be ints
11955 : and their values must be 0 or 1.
11956 : ("true" is a fixed value perhaps depending on the language.) */
11957 : /* If first arg is constant zero, return it. */
11958 6623161 : if (integer_zerop (arg0))
11959 1692348 : return fold_convert_loc (loc, type, arg0);
11960 : /* FALLTHRU */
11961 15666821 : case TRUTH_AND_EXPR:
11962 : /* If either arg is constant true, drop it. */
11963 15666821 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11964 1486786 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11965 811053 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11966 : /* Preserve sequence points. */
11967 14945724 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11968 740902 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11969 : /* If second arg is constant zero, result is zero, but first arg
11970 : must be evaluated. */
11971 13439133 : if (integer_zerop (arg1))
11972 45364 : return omit_one_operand_loc (loc, type, arg1, arg0);
11973 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11974 : case will be handled here. */
11975 13393769 : if (integer_zerop (arg0))
11976 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11977 :
11978 : /* !X && X is always false. */
11979 13393769 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11980 13393769 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11981 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11982 : /* X && !X is always false. */
11983 13393769 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11984 13393769 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11985 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11986 :
11987 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11988 : means A >= Y && A != MAX, but in this case we know that
11989 : A < X <= MAX. */
11990 :
11991 13393769 : if (!TREE_SIDE_EFFECTS (arg0)
11992 13393769 : && !TREE_SIDE_EFFECTS (arg1))
11993 : {
11994 11934431 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11995 11934431 : if (tem && !operand_equal_p (tem, arg0, 0))
11996 449 : return fold_convert (type,
11997 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
11998 : tem, arg1));
11999 :
12000 11933982 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12001 11933982 : if (tem && !operand_equal_p (tem, arg1, 0))
12002 8460 : return fold_convert (type,
12003 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12004 : arg0, tem));
12005 : }
12006 :
12007 13384860 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12008 : != NULL_TREE)
12009 : return tem;
12010 :
12011 : return NULL_TREE;
12012 :
12013 3479174 : case TRUTH_ORIF_EXPR:
12014 : /* Note that the operands of this must be ints
12015 : and their values must be 0 or true.
12016 : ("true" is a fixed value perhaps depending on the language.) */
12017 : /* If first arg is constant true, return it. */
12018 3479174 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12019 144884 : return fold_convert_loc (loc, type, arg0);
12020 : /* FALLTHRU */
12021 12753755 : case TRUTH_OR_EXPR:
12022 : /* If either arg is constant zero, drop it. */
12023 12753755 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12024 239135 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12025 483175 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12026 : /* Preserve sequence points. */
12027 12945348 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12028 419454 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12029 : /* If second arg is constant true, result is true, but we must
12030 : evaluate first arg. */
12031 12095166 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12032 52447 : return omit_one_operand_loc (loc, type, arg1, arg0);
12033 : /* Likewise for first arg, but note this only occurs here for
12034 : TRUTH_OR_EXPR. */
12035 12042719 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12036 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12037 :
12038 : /* !X || X is always true. */
12039 12042719 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12040 12042719 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12041 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12042 : /* X || !X is always true. */
12043 12042719 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12044 12042719 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12045 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12046 :
12047 : /* (X && !Y) || (!X && Y) is X ^ Y */
12048 12042718 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12049 1643 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12050 : {
12051 668 : tree a0, a1, l0, l1, n0, n1;
12052 :
12053 668 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12054 668 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12055 :
12056 668 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12057 668 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12058 :
12059 668 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12060 668 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12061 :
12062 668 : if ((operand_equal_p (n0, a0, 0)
12063 18 : && operand_equal_p (n1, a1, 0))
12064 676 : || (operand_equal_p (n0, a1, 0)
12065 3 : && operand_equal_p (n1, a0, 0)))
12066 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12067 : }
12068 :
12069 12042705 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12070 : != NULL_TREE)
12071 : return tem;
12072 :
12073 : return NULL_TREE;
12074 :
12075 78395 : case TRUTH_XOR_EXPR:
12076 : /* If the second arg is constant zero, drop it. */
12077 78395 : if (integer_zerop (arg1))
12078 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12079 : /* If the second arg is constant true, this is a logical inversion. */
12080 78395 : if (integer_onep (arg1))
12081 : {
12082 0 : tem = invert_truthvalue_loc (loc, arg0);
12083 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12084 : }
12085 : /* Identical arguments cancel to zero. */
12086 78395 : if (operand_equal_p (arg0, arg1, 0))
12087 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12088 :
12089 : /* !X ^ X is always true. */
12090 78395 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12091 78395 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12092 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12093 :
12094 : /* X ^ !X is always true. */
12095 78395 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12096 78395 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12097 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12098 :
12099 : return NULL_TREE;
12100 :
12101 48856572 : case EQ_EXPR:
12102 48856572 : case NE_EXPR:
12103 48856572 : STRIP_NOPS (arg0);
12104 48856572 : STRIP_NOPS (arg1);
12105 :
12106 48856572 : tem = fold_comparison (loc, code, type, op0, op1);
12107 48856572 : if (tem != NULL_TREE)
12108 : return tem;
12109 :
12110 : /* bool_var != 1 becomes !bool_var. */
12111 50005528 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12112 48888499 : && code == NE_EXPR)
12113 39592 : return fold_convert_loc (loc, type,
12114 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12115 79184 : TREE_TYPE (arg0), arg0));
12116 :
12117 : /* bool_var == 0 becomes !bool_var. */
12118 49926344 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12119 49742984 : && code == EQ_EXPR)
12120 195200 : return fold_convert_loc (loc, type,
12121 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12122 390400 : TREE_TYPE (arg0), arg0));
12123 :
12124 : /* !exp != 0 becomes !exp */
12125 587301 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12126 49195655 : && code == NE_EXPR)
12127 579607 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12128 :
12129 : /* If this is an EQ or NE comparison with zero and ARG0 is
12130 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12131 : two operations, but the latter can be done in one less insn
12132 : on machines that have only two-operand insns or on which a
12133 : constant cannot be the first operand. */
12134 48029458 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12135 48029458 : && integer_zerop (arg1))
12136 : {
12137 1512367 : tree arg00 = TREE_OPERAND (arg0, 0);
12138 1512367 : tree arg01 = TREE_OPERAND (arg0, 1);
12139 1512367 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12140 1512367 : && integer_onep (TREE_OPERAND (arg00, 0)))
12141 : {
12142 4291 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12143 4291 : arg01, TREE_OPERAND (arg00, 1));
12144 4291 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12145 4291 : build_one_cst (TREE_TYPE (arg0)));
12146 4291 : return fold_build2_loc (loc, code, type,
12147 4291 : fold_convert_loc (loc, TREE_TYPE (arg1),
12148 4291 : tem), arg1);
12149 : }
12150 1508076 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12151 1508076 : && integer_onep (TREE_OPERAND (arg01, 0)))
12152 : {
12153 425 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12154 425 : arg00, TREE_OPERAND (arg01, 1));
12155 425 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12156 425 : build_one_cst (TREE_TYPE (arg0)));
12157 425 : return fold_build2_loc (loc, code, type,
12158 425 : fold_convert_loc (loc, TREE_TYPE (arg1),
12159 425 : tem), arg1);
12160 : }
12161 : }
12162 :
12163 : /* If this is a comparison of a field, we may be able to simplify it. */
12164 48024742 : if ((TREE_CODE (arg0) == COMPONENT_REF
12165 48024742 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12166 : /* Handle the constant case even without -O
12167 : to make sure the warnings are given. */
12168 4877943 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12169 : {
12170 4562902 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12171 4562902 : if (t1)
12172 : return t1;
12173 : }
12174 :
12175 : /* Optimize comparisons of strlen vs zero to a compare of the
12176 : first character of the string vs zero. To wit,
12177 : strlen(ptr) == 0 => *ptr == 0
12178 : strlen(ptr) != 0 => *ptr != 0
12179 : Other cases should reduce to one of these two (or a constant)
12180 : due to the return value of strlen being unsigned. */
12181 47278033 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12182 : {
12183 2997739 : tree fndecl = get_callee_fndecl (arg0);
12184 :
12185 2997739 : if (fndecl
12186 2996647 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12187 550 : && call_expr_nargs (arg0) == 1
12188 2998289 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12189 : == POINTER_TYPE))
12190 : {
12191 550 : tree ptrtype
12192 550 : = build_pointer_type (build_qualified_type (char_type_node,
12193 : TYPE_QUAL_CONST));
12194 1100 : tree ptr = fold_convert_loc (loc, ptrtype,
12195 550 : CALL_EXPR_ARG (arg0, 0));
12196 550 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12197 550 : return fold_build2_loc (loc, code, type, iref,
12198 550 : build_int_cst (TREE_TYPE (iref), 0));
12199 : }
12200 : }
12201 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12202 : (X & C) == 0 when C is a single bit. */
12203 47277483 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12204 1672798 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12205 861 : && integer_zerop (arg1)
12206 47277945 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12207 : {
12208 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12209 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12210 140 : TREE_OPERAND (arg0, 1));
12211 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12212 : type, tem,
12213 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12214 140 : arg1));
12215 : }
12216 :
12217 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12218 : constant C is a power of two, i.e. a single bit. */
12219 47277343 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12220 4715 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12221 0 : && integer_zerop (arg1)
12222 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12223 47277343 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12224 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12225 : {
12226 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12227 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12228 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12229 : }
12230 :
12231 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12232 : when is C is a power of two, i.e. a single bit. */
12233 47277343 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12234 1672658 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12235 37109 : && integer_zerop (arg1)
12236 37109 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12237 47311792 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12238 34449 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12239 : {
12240 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12241 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12242 0 : arg000, TREE_OPERAND (arg0, 1));
12243 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12244 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12245 : }
12246 :
12247 47277343 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12248 4715 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12249 : {
12250 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12251 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12252 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12253 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12254 482 : tree itype = TREE_TYPE (arg0);
12255 :
12256 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12257 : operand_equal_p guarantees no side-effects so we don't need
12258 : to use omit_one_operand on Z. */
12259 482 : if (operand_equal_p (arg01, arg11, 0))
12260 8 : return fold_build2_loc (loc, code, type, arg00,
12261 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12262 8 : arg10));
12263 474 : if (operand_equal_p (arg01, arg10, 0))
12264 0 : return fold_build2_loc (loc, code, type, arg00,
12265 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12266 0 : arg11));
12267 474 : if (operand_equal_p (arg00, arg11, 0))
12268 0 : return fold_build2_loc (loc, code, type, arg01,
12269 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12270 0 : arg10));
12271 474 : if (operand_equal_p (arg00, arg10, 0))
12272 0 : return fold_build2_loc (loc, code, type, arg01,
12273 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12274 0 : arg11));
12275 :
12276 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12277 474 : if (TREE_CODE (arg01) == INTEGER_CST
12278 8 : && TREE_CODE (arg11) == INTEGER_CST)
12279 : {
12280 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12281 : fold_convert_loc (loc, itype, arg11));
12282 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12283 8 : return fold_build2_loc (loc, code, type, tem,
12284 8 : fold_convert_loc (loc, itype, arg10));
12285 : }
12286 : }
12287 :
12288 : /* Attempt to simplify equality/inequality comparisons of complex
12289 : values. Only lower the comparison if the result is known or
12290 : can be simplified to a single scalar comparison. */
12291 47277327 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12292 47274800 : || TREE_CODE (arg0) == COMPLEX_CST)
12293 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12294 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12295 : {
12296 1726 : tree real0, imag0, real1, imag1;
12297 1726 : tree rcond, icond;
12298 :
12299 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12300 : {
12301 1726 : real0 = TREE_OPERAND (arg0, 0);
12302 1726 : imag0 = TREE_OPERAND (arg0, 1);
12303 : }
12304 : else
12305 : {
12306 0 : real0 = TREE_REALPART (arg0);
12307 0 : imag0 = TREE_IMAGPART (arg0);
12308 : }
12309 :
12310 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12311 : {
12312 192 : real1 = TREE_OPERAND (arg1, 0);
12313 192 : imag1 = TREE_OPERAND (arg1, 1);
12314 : }
12315 : else
12316 : {
12317 1534 : real1 = TREE_REALPART (arg1);
12318 1534 : imag1 = TREE_IMAGPART (arg1);
12319 : }
12320 :
12321 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12322 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12323 : {
12324 11 : if (integer_zerop (rcond))
12325 : {
12326 11 : if (code == EQ_EXPR)
12327 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12328 0 : imag0, imag1);
12329 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12330 : }
12331 : else
12332 : {
12333 0 : if (code == NE_EXPR)
12334 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12335 0 : imag0, imag1);
12336 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12337 : }
12338 : }
12339 :
12340 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12341 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12342 : {
12343 9 : if (integer_zerop (icond))
12344 : {
12345 7 : if (code == EQ_EXPR)
12346 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12347 1 : real0, real1);
12348 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12349 : }
12350 : else
12351 : {
12352 2 : if (code == NE_EXPR)
12353 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12354 1 : real0, real1);
12355 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12356 : }
12357 : }
12358 : }
12359 :
12360 : return NULL_TREE;
12361 :
12362 41000907 : case LT_EXPR:
12363 41000907 : case GT_EXPR:
12364 41000907 : case LE_EXPR:
12365 41000907 : case GE_EXPR:
12366 41000907 : tem = fold_comparison (loc, code, type, op0, op1);
12367 41000907 : if (tem != NULL_TREE)
12368 : return tem;
12369 :
12370 : /* Transform comparisons of the form X +- C CMP X. */
12371 40129638 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12372 4758459 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12373 51265 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12374 40129654 : && !HONOR_SNANS (arg0))
12375 : {
12376 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12377 14 : enum tree_code code0 = TREE_CODE (arg0);
12378 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12379 :
12380 : /* (X - c) > X becomes false. */
12381 14 : if (code == GT_EXPR
12382 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12383 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12384 4 : return constant_boolean_node (0, type);
12385 :
12386 : /* Likewise (X + c) < X becomes false. */
12387 10 : if (code == LT_EXPR
12388 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12389 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12390 3 : return constant_boolean_node (0, type);
12391 :
12392 : /* Convert (X - c) <= X to true. */
12393 7 : if (!HONOR_NANS (arg1)
12394 6 : && code == LE_EXPR
12395 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12396 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12397 4 : return constant_boolean_node (1, type);
12398 :
12399 : /* Convert (X + c) >= X to true. */
12400 3 : if (!HONOR_NANS (arg1)
12401 2 : && code == GE_EXPR
12402 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12403 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12404 2 : return constant_boolean_node (1, type);
12405 : }
12406 :
12407 : /* If we are comparing an ABS_EXPR with a constant, we can
12408 : convert all the cases into explicit comparisons, but they may
12409 : well not be faster than doing the ABS and one comparison.
12410 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12411 : and a comparison, and is probably faster. */
12412 40129625 : if (code == LE_EXPR
12413 7621973 : && TREE_CODE (arg1) == INTEGER_CST
12414 5415375 : && TREE_CODE (arg0) == ABS_EXPR
12415 818 : && ! TREE_SIDE_EFFECTS (arg0)
12416 818 : && (tem = negate_expr (arg1)) != 0
12417 818 : && TREE_CODE (tem) == INTEGER_CST
12418 40130443 : && !TREE_OVERFLOW (tem))
12419 1636 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12420 : build2 (GE_EXPR, type,
12421 818 : TREE_OPERAND (arg0, 0), tem),
12422 : build2 (LE_EXPR, type,
12423 1636 : TREE_OPERAND (arg0, 0), arg1));
12424 :
12425 : /* Convert ABS_EXPR<x> >= 0 to true. */
12426 40128807 : if (code == GE_EXPR
12427 4202496 : && (integer_zerop (arg1)
12428 3062913 : || (! HONOR_NANS (arg0)
12429 2392369 : && real_zerop (arg1)))
12430 41268621 : && tree_expr_nonnegative_p (arg0))
12431 1081 : return omit_one_operand_loc (loc, type,
12432 : constant_boolean_node (true, type),
12433 1081 : arg0);
12434 :
12435 : /* Convert ABS_EXPR<x> < 0 to false. */
12436 40127726 : if (code == LT_EXPR
12437 13349533 : && (integer_zerop (arg1) || real_zerop (arg1))
12438 43387834 : && tree_expr_nonnegative_p (arg0))
12439 2507 : return omit_one_operand_loc (loc, type,
12440 : constant_boolean_node (false, type),
12441 2507 : arg0);
12442 :
12443 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12444 : and similarly for >= into !=. */
12445 40125219 : if ((code == LT_EXPR || code == GE_EXPR)
12446 17548441 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12447 5496511 : && TREE_CODE (arg1) == LSHIFT_EXPR
12448 40126722 : && integer_onep (TREE_OPERAND (arg1, 0)))
12449 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12450 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12451 1355 : TREE_OPERAND (arg1, 1)),
12452 2710 : build_zero_cst (TREE_TYPE (arg0)));
12453 :
12454 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12455 : otherwise Y might be >= # of bits in X's type and thus e.g.
12456 : (unsigned char) (1 << Y) for Y 15 might be 0.
12457 : If the cast is widening, then 1 << Y should have unsigned type,
12458 : otherwise if Y is number of bits in the signed shift type minus 1,
12459 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12460 : 31 might be 0xffffffff80000000. */
12461 40123864 : if ((code == LT_EXPR || code == GE_EXPR)
12462 17547086 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12463 5709172 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12464 11861296 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12465 4007944 : && CONVERT_EXPR_P (arg1)
12466 1116235 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12467 42 : && (element_precision (TREE_TYPE (arg1))
12468 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12469 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12470 14 : || (element_precision (TREE_TYPE (arg1))
12471 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12472 40123871 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12473 : {
12474 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12475 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12476 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12477 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12478 14 : build_zero_cst (TREE_TYPE (arg0)));
12479 : }
12480 :
12481 : return NULL_TREE;
12482 :
12483 5735467 : case UNORDERED_EXPR:
12484 5735467 : case ORDERED_EXPR:
12485 5735467 : case UNLT_EXPR:
12486 5735467 : case UNLE_EXPR:
12487 5735467 : case UNGT_EXPR:
12488 5735467 : case UNGE_EXPR:
12489 5735467 : case UNEQ_EXPR:
12490 5735467 : case LTGT_EXPR:
12491 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12492 5735467 : {
12493 5735467 : tree targ0 = strip_float_extensions (arg0);
12494 5735467 : tree targ1 = strip_float_extensions (arg1);
12495 5735467 : tree newtype = TREE_TYPE (targ0);
12496 :
12497 5735467 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12498 1289 : newtype = TREE_TYPE (targ1);
12499 :
12500 5735467 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12501 5735467 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12502 328 : return fold_build2_loc (loc, code, type,
12503 : fold_convert_loc (loc, newtype, targ0),
12504 328 : fold_convert_loc (loc, newtype, targ1));
12505 : }
12506 :
12507 : return NULL_TREE;
12508 :
12509 8588379 : case COMPOUND_EXPR:
12510 : /* When pedantic, a compound expression can be neither an lvalue
12511 : nor an integer constant expression. */
12512 8588379 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12513 : return NULL_TREE;
12514 : /* Don't let (0, 0) be null pointer constant. */
12515 510867 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12516 510867 : : fold_convert_loc (loc, type, arg1);
12517 : return tem;
12518 :
12519 : default:
12520 : return NULL_TREE;
12521 : } /* switch (code) */
12522 : }
12523 :
12524 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12525 : ((A & N) + B) & M -> (A + B) & M
12526 : Similarly if (N & M) == 0,
12527 : ((A | N) + B) & M -> (A + B) & M
12528 : and for - instead of + (or unary - instead of +)
12529 : and/or ^ instead of |.
12530 : If B is constant and (B & M) == 0, fold into A & M.
12531 :
12532 : This function is a helper for match.pd patterns. Return non-NULL
12533 : type in which the simplified operation should be performed only
12534 : if any optimization is possible.
12535 :
12536 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12537 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12538 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12539 : +/-. */
12540 : tree
12541 1248142 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12542 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12543 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12544 : tree *pmop)
12545 : {
12546 1248142 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12547 1248142 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12548 1248142 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12549 2496284 : if (~cst1 == 0
12550 3738812 : || (cst1 & (cst1 + 1)) != 0
12551 1037069 : || !INTEGRAL_TYPE_P (type)
12552 1037069 : || (!TYPE_OVERFLOW_WRAPS (type)
12553 41819 : && TREE_CODE (type) != INTEGER_TYPE)
12554 4567615 : || (wi::max_value (type) & cst1) != cst1)
12555 : return NULL_TREE;
12556 :
12557 1037069 : enum tree_code codes[2] = { code00, code01 };
12558 1037069 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12559 1037069 : int which = 0;
12560 1037069 : wide_int cst0;
12561 :
12562 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12563 : arg1 (M) is == (1LL << cst) - 1.
12564 : Store C into PMOP[0] and D into PMOP[1]. */
12565 1037069 : pmop[0] = arg00;
12566 1037069 : pmop[1] = arg01;
12567 1037069 : which = code != NEGATE_EXPR;
12568 :
12569 3110291 : for (; which >= 0; which--)
12570 2073222 : switch (codes[which])
12571 : {
12572 20705 : case BIT_AND_EXPR:
12573 20705 : case BIT_IOR_EXPR:
12574 20705 : case BIT_XOR_EXPR:
12575 20705 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12576 20705 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12577 20705 : if (codes[which] == BIT_AND_EXPR)
12578 : {
12579 20593 : if (cst0 != cst1)
12580 : break;
12581 : }
12582 112 : else if (cst0 != 0)
12583 : break;
12584 : /* If C or D is of the form (A & N) where
12585 : (N & M) == M, or of the form (A | N) or
12586 : (A ^ N) where (N & M) == 0, replace it with A. */
12587 19166 : pmop[which] = arg0xx[2 * which];
12588 19166 : break;
12589 2052517 : case ERROR_MARK:
12590 2052517 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12591 : break;
12592 : /* If C or D is a N where (N & M) == 0, it can be
12593 : omitted (replaced with 0). */
12594 865898 : if ((code == PLUS_EXPR
12595 207246 : || (code == MINUS_EXPR && which == 0))
12596 640195 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12597 134352 : pmop[which] = build_int_cst (type, 0);
12598 : /* Similarly, with C - N where (-N & M) == 0. */
12599 865898 : if (code == MINUS_EXPR
12600 432949 : && which == 1
12601 633210 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12602 192269 : pmop[which] = build_int_cst (type, 0);
12603 : break;
12604 0 : default:
12605 0 : gcc_unreachable ();
12606 : }
12607 :
12608 : /* Only build anything new if we optimized one or both arguments above. */
12609 1037069 : if (pmop[0] == arg00 && pmop[1] == arg01)
12610 : return NULL_TREE;
12611 :
12612 345048 : if (TYPE_OVERFLOW_WRAPS (type))
12613 : return type;
12614 : else
12615 2370 : return unsigned_type_for (type);
12616 1037069 : }
12617 :
12618 : /* Used by contains_label_[p1]. */
12619 :
12620 : struct contains_label_data
12621 : {
12622 : hash_set<tree> *pset;
12623 : bool inside_switch_p;
12624 : };
12625 :
12626 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12627 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12628 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12629 :
12630 : static tree
12631 4489716 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12632 : {
12633 4489716 : contains_label_data *d = (contains_label_data *) data;
12634 4489716 : switch (TREE_CODE (*tp))
12635 : {
12636 : case LABEL_EXPR:
12637 : return *tp;
12638 :
12639 0 : case CASE_LABEL_EXPR:
12640 0 : if (!d->inside_switch_p)
12641 : return *tp;
12642 : return NULL_TREE;
12643 :
12644 0 : case SWITCH_EXPR:
12645 0 : if (!d->inside_switch_p)
12646 : {
12647 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12648 0 : return *tp;
12649 0 : d->inside_switch_p = true;
12650 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12651 0 : return *tp;
12652 0 : d->inside_switch_p = false;
12653 0 : *walk_subtrees = 0;
12654 : }
12655 : return NULL_TREE;
12656 :
12657 6598 : case GOTO_EXPR:
12658 6598 : *walk_subtrees = 0;
12659 6598 : return NULL_TREE;
12660 :
12661 : default:
12662 : return NULL_TREE;
12663 : }
12664 : }
12665 :
12666 : /* Return whether the sub-tree ST contains a label which is accessible from
12667 : outside the sub-tree. */
12668 :
12669 : static bool
12670 323289 : contains_label_p (tree st)
12671 : {
12672 323289 : hash_set<tree> pset;
12673 323289 : contains_label_data data = { &pset, false };
12674 323289 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12675 323289 : }
12676 :
12677 : /* Fold a ternary expression of code CODE and type TYPE with operands
12678 : OP0, OP1, and OP2. Return the folded expression if folding is
12679 : successful. Otherwise, return NULL_TREE. */
12680 :
12681 : tree
12682 45473715 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12683 : tree op0, tree op1, tree op2)
12684 : {
12685 45473715 : tree tem;
12686 45473715 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12687 45473715 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12688 :
12689 45473715 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12690 : && TREE_CODE_LENGTH (code) == 3);
12691 :
12692 : /* If this is a commutative operation, and OP0 is a constant, move it
12693 : to OP1 to reduce the number of tests below. */
12694 45473715 : if (commutative_ternary_tree_code (code)
12695 45473715 : && tree_swap_operands_p (op0, op1))
12696 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12697 :
12698 45473682 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12699 45473682 : if (tem)
12700 : return tem;
12701 :
12702 : /* Strip any conversions that don't change the mode. This is safe
12703 : for every expression, except for a comparison expression because
12704 : its signedness is derived from its operands. So, in the latter
12705 : case, only strip conversions that don't change the signedness.
12706 :
12707 : Note that this is done as an internal manipulation within the
12708 : constant folder, in order to find the simplest representation of
12709 : the arguments so that their form can be studied. In any cases,
12710 : the appropriate type conversions should be put back in the tree
12711 : that will get out of the constant folder. */
12712 44429287 : if (op0)
12713 : {
12714 44362201 : arg0 = op0;
12715 44362201 : STRIP_NOPS (arg0);
12716 : }
12717 :
12718 44429287 : if (op1)
12719 : {
12720 44429287 : arg1 = op1;
12721 44429287 : STRIP_NOPS (arg1);
12722 : }
12723 :
12724 44429287 : if (op2)
12725 : {
12726 14928754 : arg2 = op2;
12727 14928754 : STRIP_NOPS (arg2);
12728 : }
12729 :
12730 44429287 : switch (code)
12731 : {
12732 29500051 : case COMPONENT_REF:
12733 29500051 : if (TREE_CODE (arg0) == CONSTRUCTOR
12734 29500051 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12735 : {
12736 : unsigned HOST_WIDE_INT idx;
12737 : tree field, value;
12738 884 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12739 679 : if (field == arg1)
12740 : return value;
12741 : }
12742 : return NULL_TREE;
12743 :
12744 12461714 : case COND_EXPR:
12745 12461714 : case VEC_COND_EXPR:
12746 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12747 : so all simple results must be passed through pedantic_non_lvalue. */
12748 12461714 : if (TREE_CODE (arg0) == INTEGER_CST)
12749 : {
12750 454987 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12751 454987 : tem = integer_zerop (arg0) ? op2 : op1;
12752 : /* Only optimize constant conditions when the selected branch
12753 : has the same type as the COND_EXPR. This avoids optimizing
12754 : away "c ? x : throw", where the throw has a void type.
12755 : Avoid throwing away that operand which contains label. */
12756 454987 : if ((!TREE_SIDE_EFFECTS (unused_op)
12757 323289 : || !contains_label_p (unused_op))
12758 773455 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12759 371481 : || VOID_TYPE_P (type)))
12760 440960 : return protected_set_expr_location_unshare (tem, loc);
12761 14027 : return NULL_TREE;
12762 : }
12763 12006727 : else if (TREE_CODE (arg0) == VECTOR_CST)
12764 : {
12765 11713 : unsigned HOST_WIDE_INT nelts;
12766 11713 : if ((TREE_CODE (arg1) == VECTOR_CST
12767 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12768 2672 : && (TREE_CODE (arg2) == VECTOR_CST
12769 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12770 23426 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12771 : {
12772 2672 : vec_perm_builder sel (nelts, nelts, 1);
12773 26552 : for (unsigned int i = 0; i < nelts; i++)
12774 : {
12775 23880 : tree val = VECTOR_CST_ELT (arg0, i);
12776 23880 : if (integer_all_onesp (val))
12777 11819 : sel.quick_push (i);
12778 12061 : else if (integer_zerop (val))
12779 12061 : sel.quick_push (nelts + i);
12780 : else /* Currently unreachable. */
12781 1980 : return NULL_TREE;
12782 : }
12783 2672 : vec_perm_indices indices (sel, 2, nelts);
12784 2672 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12785 2672 : if (t != NULL_TREE)
12786 1980 : return t;
12787 4652 : }
12788 : }
12789 :
12790 : /* If we have A op B ? A : C, we may be able to convert this to a
12791 : simpler expression, depending on the operation and the values
12792 : of B and C. Signed zeros prevent all of these transformations,
12793 : for reasons given above each one.
12794 :
12795 : Also try swapping the arguments and inverting the conditional. */
12796 12004747 : if (COMPARISON_CLASS_P (arg0)
12797 9872442 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12798 12143130 : && !HONOR_SIGNED_ZEROS (op1))
12799 : {
12800 127631 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12801 127631 : TREE_OPERAND (arg0, 0),
12802 127631 : TREE_OPERAND (arg0, 1),
12803 : op1, op2);
12804 127631 : if (tem)
12805 : return tem;
12806 : }
12807 :
12808 11998014 : if (COMPARISON_CLASS_P (arg0)
12809 9865709 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12810 12465574 : && !HONOR_SIGNED_ZEROS (op2))
12811 : {
12812 381650 : enum tree_code comp_code = TREE_CODE (arg0);
12813 381650 : tree arg00 = TREE_OPERAND (arg0, 0);
12814 381650 : tree arg01 = TREE_OPERAND (arg0, 1);
12815 381650 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12816 381650 : if (comp_code != ERROR_MARK)
12817 381650 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12818 : arg00,
12819 : arg01,
12820 : op2, op1);
12821 381650 : if (tem)
12822 : return tem;
12823 : }
12824 :
12825 : /* If the second operand is simpler than the third, swap them
12826 : since that produces better jump optimization results. */
12827 11733433 : if (truth_value_p (TREE_CODE (arg0))
12828 11733433 : && tree_swap_operands_p (op1, op2))
12829 : {
12830 2039169 : location_t loc0 = expr_location_or (arg0, loc);
12831 : /* See if this can be inverted. If it can't, possibly because
12832 : it was a floating-point inequality comparison, don't do
12833 : anything. */
12834 2039169 : tem = fold_invert_truthvalue (loc0, arg0);
12835 2039169 : if (tem)
12836 1296766 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12837 : }
12838 :
12839 : /* Convert A ? 1 : 0 to simply A. */
12840 10436667 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12841 9984232 : : (integer_onep (op1)
12842 409581 : && !VECTOR_TYPE_P (type)))
12843 688224 : && integer_zerop (op2)
12844 : /* If we try to convert OP0 to our type, the
12845 : call to fold will try to move the conversion inside
12846 : a COND, which will recurse. In that case, the COND_EXPR
12847 : is probably the best choice, so leave it alone. */
12848 11563633 : && type == TREE_TYPE (arg0))
12849 32527 : return protected_set_expr_location_unshare (arg0, loc);
12850 :
12851 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12852 : over COND_EXPR in cases such as floating point comparisons. */
12853 10404140 : if (integer_zerop (op1)
12854 386320 : && code == COND_EXPR
12855 362180 : && integer_onep (op2)
12856 31410 : && !VECTOR_TYPE_P (type)
12857 10435550 : && truth_value_p (TREE_CODE (arg0)))
12858 29855 : return fold_convert_loc (loc, type,
12859 29855 : invert_truthvalue_loc (loc, arg0));
12860 :
12861 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12862 10374285 : if (TREE_CODE (arg0) == LT_EXPR
12863 1361085 : && integer_zerop (TREE_OPERAND (arg0, 1))
12864 37578 : && integer_zerop (op2)
12865 10375256 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12866 : {
12867 : /* sign_bit_p looks through both zero and sign extensions,
12868 : but for this optimization only sign extensions are
12869 : usable. */
12870 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12871 56 : while (tem != tem2)
12872 : {
12873 0 : if (TREE_CODE (tem2) != NOP_EXPR
12874 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12875 : {
12876 : tem = NULL_TREE;
12877 : break;
12878 : }
12879 0 : tem2 = TREE_OPERAND (tem2, 0);
12880 : }
12881 : /* sign_bit_p only checks ARG1 bits within A's precision.
12882 : If <sign bit of A> has wider type than A, bits outside
12883 : of A's precision in <sign bit of A> need to be checked.
12884 : If they are all 0, this optimization needs to be done
12885 : in unsigned A's type, if they are all 1 in signed A's type,
12886 : otherwise this can't be done. */
12887 56 : if (tem
12888 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12889 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12890 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12891 56 : < TYPE_PRECISION (type))
12892 : {
12893 56 : int inner_width, outer_width;
12894 56 : tree tem_type;
12895 :
12896 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12897 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12898 56 : if (outer_width > TYPE_PRECISION (type))
12899 0 : outer_width = TYPE_PRECISION (type);
12900 :
12901 56 : wide_int mask = wi::shifted_mask
12902 56 : (inner_width, outer_width - inner_width, false,
12903 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
12904 :
12905 56 : wide_int common = mask & wi::to_wide (arg1);
12906 56 : if (common == mask)
12907 : {
12908 28 : tem_type = signed_type_for (TREE_TYPE (tem));
12909 28 : tem = fold_convert_loc (loc, tem_type, tem);
12910 : }
12911 28 : else if (common == 0)
12912 : {
12913 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
12914 0 : tem = fold_convert_loc (loc, tem_type, tem);
12915 : }
12916 : else
12917 : tem = NULL;
12918 56 : }
12919 :
12920 56 : if (tem)
12921 28 : return
12922 56 : fold_convert_loc (loc, type,
12923 : fold_build2_loc (loc, BIT_AND_EXPR,
12924 28 : TREE_TYPE (tem), tem,
12925 : fold_convert_loc (loc,
12926 28 : TREE_TYPE (tem),
12927 28 : arg1)));
12928 : }
12929 :
12930 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12931 : already handled above. */
12932 10374257 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12933 347 : && integer_onep (TREE_OPERAND (arg0, 1))
12934 3 : && integer_zerop (op2)
12935 10374257 : && integer_pow2p (arg1))
12936 : {
12937 0 : tree tem = TREE_OPERAND (arg0, 0);
12938 0 : STRIP_NOPS (tem);
12939 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
12940 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
12941 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
12942 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
12943 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
12944 : fold_convert_loc (loc, type,
12945 0 : TREE_OPERAND (tem, 0)),
12946 0 : op1);
12947 : }
12948 :
12949 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
12950 : is probably obsolete because the first operand should be a
12951 : truth value (that's why we have the two cases above), but let's
12952 : leave it in until we can confirm this for all front-ends. */
12953 10374257 : if (integer_zerop (op2)
12954 2023748 : && TREE_CODE (arg0) == NE_EXPR
12955 527476 : && integer_zerop (TREE_OPERAND (arg0, 1))
12956 279328 : && integer_pow2p (arg1)
12957 32039 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12958 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12959 : arg1, OEP_ONLY_CONST)
12960 : /* operand_equal_p compares just value, not precision, so e.g.
12961 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
12962 : second operand 32-bit -128, which is not a power of two (or vice
12963 : versa. */
12964 10374257 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
12965 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12966 :
12967 : /* Disable the transformations below for vectors, since
12968 : fold_binary_op_with_conditional_arg may undo them immediately,
12969 : yielding an infinite loop. */
12970 10374257 : if (code == VEC_COND_EXPR)
12971 : return NULL_TREE;
12972 :
12973 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
12974 9921822 : if (integer_zerop (op2)
12975 1667344 : && truth_value_p (TREE_CODE (arg0))
12976 1475549 : && truth_value_p (TREE_CODE (arg1))
12977 9955183 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12978 33361 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12979 : : TRUTH_ANDIF_EXPR,
12980 33361 : type, fold_convert_loc (loc, type, arg0), op1);
12981 :
12982 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12983 9888461 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12984 450217 : && truth_value_p (TREE_CODE (arg0))
12985 303700 : && truth_value_p (TREE_CODE (arg1))
12986 9925709 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12987 : {
12988 37248 : location_t loc0 = expr_location_or (arg0, loc);
12989 : /* Only perform transformation if ARG0 is easily inverted. */
12990 37248 : tem = fold_invert_truthvalue (loc0, arg0);
12991 37248 : if (tem)
12992 36984 : return fold_build2_loc (loc, code == VEC_COND_EXPR
12993 : ? BIT_IOR_EXPR
12994 : : TRUTH_ORIF_EXPR,
12995 : type, fold_convert_loc (loc, type, tem),
12996 36984 : op1);
12997 : }
12998 :
12999 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13000 9851477 : if (integer_zerop (arg1)
13001 332402 : && truth_value_p (TREE_CODE (arg0))
13002 83216 : && truth_value_p (TREE_CODE (op2))
13003 9851505 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13004 : {
13005 28 : location_t loc0 = expr_location_or (arg0, loc);
13006 : /* Only perform transformation if ARG0 is easily inverted. */
13007 28 : tem = fold_invert_truthvalue (loc0, arg0);
13008 28 : if (tem)
13009 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13010 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13011 : type, fold_convert_loc (loc, type, tem),
13012 0 : op2);
13013 : }
13014 :
13015 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13016 9851477 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13017 377054 : && truth_value_p (TREE_CODE (arg0))
13018 268964 : && truth_value_p (TREE_CODE (op2))
13019 9851663 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13020 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13021 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13022 186 : type, fold_convert_loc (loc, type, arg0), op2);
13023 :
13024 : return NULL_TREE;
13025 :
13026 0 : case CALL_EXPR:
13027 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13028 : of fold_ternary on them. */
13029 0 : gcc_unreachable ();
13030 :
13031 930870 : case BIT_FIELD_REF:
13032 930870 : if (TREE_CODE (arg0) == VECTOR_CST
13033 79552 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13034 42738 : || (VECTOR_TYPE_P (type)
13035 41866 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13036 78644 : && tree_fits_uhwi_p (op1)
13037 1009514 : && tree_fits_uhwi_p (op2))
13038 : {
13039 78644 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13040 78644 : unsigned HOST_WIDE_INT width
13041 78644 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13042 78644 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13043 78644 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13044 78644 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13045 :
13046 78644 : if (n != 0
13047 78644 : && (idx % width) == 0
13048 78644 : && (n % width) == 0
13049 157288 : && known_le ((idx + n) / width,
13050 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13051 : {
13052 78644 : idx = idx / width;
13053 78644 : n = n / width;
13054 :
13055 78644 : if (TREE_CODE (arg0) == VECTOR_CST)
13056 : {
13057 78644 : if (n == 1)
13058 : {
13059 36818 : tem = VECTOR_CST_ELT (arg0, idx);
13060 36818 : if (VECTOR_TYPE_P (type))
13061 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13062 36818 : return tem;
13063 : }
13064 :
13065 41826 : tree_vector_builder vals (type, n, 1);
13066 185186 : for (unsigned i = 0; i < n; ++i)
13067 143360 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13068 41826 : return vals.build ();
13069 41826 : }
13070 : }
13071 : }
13072 :
13073 : /* On constants we can use native encode/interpret to constant
13074 : fold (nearly) all BIT_FIELD_REFs. */
13075 852226 : if (CONSTANT_CLASS_P (arg0)
13076 1709 : && can_native_interpret_type_p (type)
13077 : && BITS_PER_UNIT == 8
13078 1709 : && tree_fits_uhwi_p (op1)
13079 853935 : && tree_fits_uhwi_p (op2))
13080 : {
13081 1709 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13082 1709 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13083 : /* Limit us to a reasonable amount of work. To relax the
13084 : other limitations we need bit-shifting of the buffer
13085 : and rounding up the size. */
13086 1709 : if (bitpos % BITS_PER_UNIT == 0
13087 1709 : && bitsize % BITS_PER_UNIT == 0
13088 1709 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13089 : {
13090 1709 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13091 1709 : unsigned HOST_WIDE_INT len
13092 1709 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13093 1709 : bitpos / BITS_PER_UNIT);
13094 1709 : if (len > 0
13095 1709 : && len * BITS_PER_UNIT >= bitsize)
13096 : {
13097 1709 : tree v = native_interpret_expr (type, b,
13098 : bitsize / BITS_PER_UNIT);
13099 1709 : if (v)
13100 1655 : return v;
13101 : }
13102 : }
13103 : }
13104 :
13105 : return NULL_TREE;
13106 :
13107 781283 : case VEC_PERM_EXPR:
13108 : /* Perform constant folding of BIT_INSERT_EXPR. */
13109 781283 : if (TREE_CODE (arg2) == VECTOR_CST
13110 769809 : && TREE_CODE (op0) == VECTOR_CST
13111 15422 : && TREE_CODE (op1) == VECTOR_CST)
13112 : {
13113 : /* Build a vector of integers from the tree mask. */
13114 3951 : vec_perm_builder builder;
13115 3951 : if (!tree_to_vec_perm_builder (&builder, arg2))
13116 : return NULL_TREE;
13117 :
13118 : /* Create a vec_perm_indices for the integer vector. */
13119 3951 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13120 3951 : bool single_arg = (op0 == op1);
13121 7902 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13122 3951 : return fold_vec_perm (type, op0, op1, sel);
13123 7902 : }
13124 : return NULL_TREE;
13125 :
13126 16035 : case BIT_INSERT_EXPR:
13127 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13128 16035 : if (TREE_CODE (arg0) == INTEGER_CST
13129 14 : && TREE_CODE (arg1) == INTEGER_CST)
13130 : {
13131 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13132 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13133 2 : if (BYTES_BIG_ENDIAN)
13134 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13135 2 : wide_int tem = (wi::to_wide (arg0)
13136 4 : & wi::shifted_mask (bitpos, bitsize, true,
13137 4 : TYPE_PRECISION (type)));
13138 2 : wide_int tem2
13139 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13140 2 : bitsize), bitpos);
13141 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13142 2 : }
13143 16033 : else if (TREE_CODE (arg0) == VECTOR_CST
13144 901 : && CONSTANT_CLASS_P (arg1)
13145 16330 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13146 297 : TREE_TYPE (arg1)))
13147 : {
13148 297 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13149 297 : unsigned HOST_WIDE_INT elsize
13150 297 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13151 297 : if (bitpos % elsize == 0)
13152 : {
13153 297 : unsigned k = bitpos / elsize;
13154 297 : unsigned HOST_WIDE_INT nelts;
13155 297 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13156 45473715 : return arg0;
13157 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13158 : {
13159 290 : tree_vector_builder elts (type, nelts, 1);
13160 290 : elts.quick_grow (nelts);
13161 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13162 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13163 290 : return elts.build ();
13164 290 : }
13165 : }
13166 : }
13167 : return NULL_TREE;
13168 :
13169 : default:
13170 : return NULL_TREE;
13171 : } /* switch (code) */
13172 : }
13173 :
13174 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13175 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13176 : constructor element index of the value returned. If the element is
13177 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13178 : the index of the element after the ACCESS_INDEX position (which
13179 : may be outside of the CTOR array). */
13180 :
13181 : tree
13182 674056 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13183 : unsigned *ctor_idx)
13184 : {
13185 674056 : tree index_type = NULL_TREE;
13186 674056 : signop index_sgn = UNSIGNED;
13187 674056 : offset_int low_bound = 0;
13188 :
13189 674056 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13190 : {
13191 674056 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13192 674056 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13193 : {
13194 : /* Static constructors for variably sized objects makes no sense. */
13195 674056 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13196 674056 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13197 : /* ??? When it is obvious that the range is signed, treat it so. */
13198 674056 : if (TYPE_UNSIGNED (index_type)
13199 338240 : && TYPE_MAX_VALUE (domain_type)
13200 1012265 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13201 338209 : TYPE_MIN_VALUE (domain_type)))
13202 : {
13203 0 : index_sgn = SIGNED;
13204 0 : low_bound
13205 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13206 : SIGNED);
13207 : }
13208 : else
13209 : {
13210 674056 : index_sgn = TYPE_SIGN (index_type);
13211 674056 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13212 : }
13213 : }
13214 : }
13215 :
13216 674056 : if (index_type)
13217 674056 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13218 : index_sgn);
13219 :
13220 674056 : offset_int index = low_bound;
13221 674056 : if (index_type)
13222 674056 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13223 :
13224 674056 : offset_int max_index = index;
13225 674056 : unsigned cnt;
13226 674056 : tree cfield, cval;
13227 674056 : bool first_p = true;
13228 :
13229 13849479 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13230 : {
13231 : /* Array constructor might explicitly set index, or specify a range,
13232 : or leave index NULL meaning that it is next index after previous
13233 : one. */
13234 13848348 : if (cfield)
13235 : {
13236 5841653 : if (TREE_CODE (cfield) == INTEGER_CST)
13237 11681860 : max_index = index
13238 5840930 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13239 : else
13240 : {
13241 723 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13242 723 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13243 : index_sgn);
13244 723 : max_index
13245 723 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13246 : index_sgn);
13247 723 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13248 : }
13249 : }
13250 8006695 : else if (!first_p)
13251 : {
13252 7813247 : index = max_index + 1;
13253 7813247 : if (index_type)
13254 7813247 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13255 7813247 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13256 7813247 : max_index = index;
13257 : }
13258 : else
13259 : first_p = false;
13260 :
13261 13848348 : if (TREE_CODE (cval) == RAW_DATA_CST)
13262 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13263 :
13264 : /* Do we have match? */
13265 13848348 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13266 : {
13267 13848060 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13268 : {
13269 672809 : if (ctor_idx)
13270 672809 : *ctor_idx = cnt;
13271 672809 : return cval;
13272 : }
13273 : }
13274 288 : else if (in_gimple_form)
13275 : /* We're past the element we search for. Note during parsing
13276 : the elements might not be sorted.
13277 : ??? We should use a binary search and a flag on the
13278 : CONSTRUCTOR as to whether elements are sorted in declaration
13279 : order. */
13280 : break;
13281 : }
13282 1247 : if (ctor_idx)
13283 1247 : *ctor_idx = cnt;
13284 : return NULL_TREE;
13285 : }
13286 :
13287 : /* Perform constant folding and related simplification of EXPR.
13288 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13289 : and application of the associative law.
13290 : NOP_EXPR conversions may be removed freely (as long as we
13291 : are careful not to change the type of the overall expression).
13292 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13293 : but we can constant-fold them if they have constant operands. */
13294 :
13295 : #ifdef ENABLE_FOLD_CHECKING
13296 : # define fold(x) fold_1 (x)
13297 : static tree fold_1 (tree);
13298 : static
13299 : #endif
13300 : tree
13301 1361142943 : fold (tree expr)
13302 : {
13303 1361315693 : const tree t = expr;
13304 1361315693 : enum tree_code code = TREE_CODE (t);
13305 1361315693 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13306 1361315693 : tree tem;
13307 1361315693 : location_t loc = EXPR_LOCATION (expr);
13308 :
13309 : /* Return right away if a constant. */
13310 1361315693 : if (kind == tcc_constant)
13311 : return t;
13312 :
13313 : /* CALL_EXPR-like objects with variable numbers of operands are
13314 : treated specially. */
13315 1258669557 : if (kind == tcc_vl_exp)
13316 : {
13317 178392972 : if (code == CALL_EXPR)
13318 : {
13319 178392444 : tem = fold_call_expr (loc, expr, false);
13320 353905114 : return tem ? tem : expr;
13321 : }
13322 : return expr;
13323 : }
13324 :
13325 1080276585 : if (IS_EXPR_CODE_CLASS (kind))
13326 : {
13327 1078094875 : tree type = TREE_TYPE (t);
13328 1078094875 : tree op0, op1, op2;
13329 :
13330 1078094875 : switch (TREE_CODE_LENGTH (code))
13331 : {
13332 978226708 : case 1:
13333 978226708 : op0 = TREE_OPERAND (t, 0);
13334 978226708 : tem = fold_unary_loc (loc, code, type, op0);
13335 1671533098 : return tem ? tem : expr;
13336 90717382 : case 2:
13337 90717382 : op0 = TREE_OPERAND (t, 0);
13338 90717382 : op1 = TREE_OPERAND (t, 1);
13339 90717382 : tem = fold_binary_loc (loc, code, type, op0, op1);
13340 170944554 : return tem ? tem : expr;
13341 4383406 : case 3:
13342 4383406 : op0 = TREE_OPERAND (t, 0);
13343 4383406 : op1 = TREE_OPERAND (t, 1);
13344 4383406 : op2 = TREE_OPERAND (t, 2);
13345 4383406 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13346 8476742 : return tem ? tem : expr;
13347 : default:
13348 : break;
13349 : }
13350 : }
13351 :
13352 6949089 : switch (code)
13353 : {
13354 4663429 : case ARRAY_REF:
13355 4663429 : {
13356 4663429 : tree op0 = TREE_OPERAND (t, 0);
13357 4663429 : tree op1 = TREE_OPERAND (t, 1);
13358 :
13359 4663429 : if (TREE_CODE (op1) == INTEGER_CST
13360 2925004 : && TREE_CODE (op0) == CONSTRUCTOR
13361 4664884 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13362 : {
13363 1455 : unsigned int idx;
13364 1455 : tree val
13365 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13366 : &idx);
13367 1455 : if (val)
13368 : {
13369 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13370 : return val;
13371 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13372 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13373 : != INTEGER_CST))
13374 : return t;
13375 2 : offset_int o
13376 2 : = (wi::to_offset (op1)
13377 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13378 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13379 2 : return build_int_cst (TREE_TYPE (val),
13380 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13381 : }
13382 : }
13383 :
13384 : return t;
13385 : }
13386 :
13387 : /* Return a VECTOR_CST if possible. */
13388 205505 : case CONSTRUCTOR:
13389 205505 : {
13390 205505 : tree type = TREE_TYPE (t);
13391 205505 : if (TREE_CODE (type) != VECTOR_TYPE)
13392 : return t;
13393 :
13394 : unsigned i;
13395 : tree val;
13396 361148 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13397 312543 : if (! CONSTANT_CLASS_P (val))
13398 : return t;
13399 :
13400 48605 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13401 : }
13402 :
13403 172750 : case CONST_DECL:
13404 172750 : return fold (DECL_INITIAL (t));
13405 :
13406 : default:
13407 : return t;
13408 : } /* switch (code) */
13409 : }
13410 :
13411 : #ifdef ENABLE_FOLD_CHECKING
13412 : #undef fold
13413 :
13414 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13415 : hash_table<nofree_ptr_hash<const tree_node> > *);
13416 : static void fold_check_failed (const_tree, const_tree);
13417 : void print_fold_checksum (const_tree);
13418 :
13419 : /* When --enable-checking=fold, compute a digest of expr before
13420 : and after actual fold call to see if fold did not accidentally
13421 : change original expr. */
13422 :
13423 : tree
13424 : fold (tree expr)
13425 : {
13426 : tree ret;
13427 : struct md5_ctx ctx;
13428 : unsigned char checksum_before[16], checksum_after[16];
13429 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13430 :
13431 : md5_init_ctx (&ctx);
13432 : fold_checksum_tree (expr, &ctx, &ht);
13433 : md5_finish_ctx (&ctx, checksum_before);
13434 : ht.empty ();
13435 :
13436 : ret = fold_1 (expr);
13437 :
13438 : md5_init_ctx (&ctx);
13439 : fold_checksum_tree (expr, &ctx, &ht);
13440 : md5_finish_ctx (&ctx, checksum_after);
13441 :
13442 : if (memcmp (checksum_before, checksum_after, 16))
13443 : fold_check_failed (expr, ret);
13444 :
13445 : return ret;
13446 : }
13447 :
13448 : void
13449 : print_fold_checksum (const_tree expr)
13450 : {
13451 : struct md5_ctx ctx;
13452 : unsigned char checksum[16], cnt;
13453 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13454 :
13455 : md5_init_ctx (&ctx);
13456 : fold_checksum_tree (expr, &ctx, &ht);
13457 : md5_finish_ctx (&ctx, checksum);
13458 : for (cnt = 0; cnt < 16; ++cnt)
13459 : fprintf (stderr, "%02x", checksum[cnt]);
13460 : putc ('\n', stderr);
13461 : }
13462 :
13463 : static void
13464 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13465 : {
13466 : internal_error ("fold check: original tree changed by fold");
13467 : }
13468 :
13469 : static void
13470 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13471 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13472 : {
13473 : const tree_node **slot;
13474 : enum tree_code code;
13475 : union tree_node *buf;
13476 : int i, len;
13477 :
13478 : recursive_label:
13479 : if (expr == NULL)
13480 : return;
13481 : slot = ht->find_slot (expr, INSERT);
13482 : if (*slot != NULL)
13483 : return;
13484 : *slot = expr;
13485 : code = TREE_CODE (expr);
13486 : if (TREE_CODE_CLASS (code) == tcc_declaration
13487 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13488 : {
13489 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13490 : size_t sz = tree_size (expr);
13491 : buf = XALLOCAVAR (union tree_node, sz);
13492 : memcpy ((char *) buf, expr, sz);
13493 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13494 : buf->decl_with_vis.symtab_node = NULL;
13495 : buf->base.nowarning_flag = 0;
13496 : expr = (tree) buf;
13497 : }
13498 : else if (TREE_CODE_CLASS (code) == tcc_type
13499 : && (TYPE_POINTER_TO (expr)
13500 : || TYPE_REFERENCE_TO (expr)
13501 : || TYPE_CACHED_VALUES_P (expr)
13502 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13503 : || TYPE_NEXT_VARIANT (expr)
13504 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13505 : {
13506 : /* Allow these fields to be modified. */
13507 : tree tmp;
13508 : size_t sz = tree_size (expr);
13509 : buf = XALLOCAVAR (union tree_node, sz);
13510 : memcpy ((char *) buf, expr, sz);
13511 : expr = tmp = (tree) buf;
13512 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13513 : TYPE_POINTER_TO (tmp) = NULL;
13514 : TYPE_REFERENCE_TO (tmp) = NULL;
13515 : TYPE_NEXT_VARIANT (tmp) = NULL;
13516 : TYPE_ALIAS_SET (tmp) = -1;
13517 : if (TYPE_CACHED_VALUES_P (tmp))
13518 : {
13519 : TYPE_CACHED_VALUES_P (tmp) = 0;
13520 : TYPE_CACHED_VALUES (tmp) = NULL;
13521 : }
13522 : }
13523 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13524 : {
13525 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13526 : that and change builtins.cc etc. instead - see PR89543. */
13527 : size_t sz = tree_size (expr);
13528 : buf = XALLOCAVAR (union tree_node, sz);
13529 : memcpy ((char *) buf, expr, sz);
13530 : buf->base.nowarning_flag = 0;
13531 : expr = (tree) buf;
13532 : }
13533 : md5_process_bytes (expr, tree_size (expr), ctx);
13534 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13535 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13536 : if (TREE_CODE_CLASS (code) != tcc_type
13537 : && TREE_CODE_CLASS (code) != tcc_declaration
13538 : && code != TREE_LIST
13539 : && code != SSA_NAME
13540 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13541 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13542 : switch (TREE_CODE_CLASS (code))
13543 : {
13544 : case tcc_constant:
13545 : switch (code)
13546 : {
13547 : case STRING_CST:
13548 : md5_process_bytes (TREE_STRING_POINTER (expr),
13549 : TREE_STRING_LENGTH (expr), ctx);
13550 : break;
13551 : case COMPLEX_CST:
13552 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13553 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13554 : break;
13555 : case VECTOR_CST:
13556 : len = vector_cst_encoded_nelts (expr);
13557 : for (i = 0; i < len; ++i)
13558 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13559 : break;
13560 : default:
13561 : break;
13562 : }
13563 : break;
13564 : case tcc_exceptional:
13565 : switch (code)
13566 : {
13567 : case TREE_LIST:
13568 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13569 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13570 : expr = TREE_CHAIN (expr);
13571 : goto recursive_label;
13572 : break;
13573 : case TREE_VEC:
13574 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13575 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13576 : break;
13577 : default:
13578 : break;
13579 : }
13580 : break;
13581 : case tcc_expression:
13582 : case tcc_reference:
13583 : case tcc_comparison:
13584 : case tcc_unary:
13585 : case tcc_binary:
13586 : case tcc_statement:
13587 : case tcc_vl_exp:
13588 : len = TREE_OPERAND_LENGTH (expr);
13589 : for (i = 0; i < len; ++i)
13590 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13591 : break;
13592 : case tcc_declaration:
13593 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13594 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13595 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13596 : {
13597 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13598 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13599 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13600 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13601 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13602 : }
13603 :
13604 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13605 : {
13606 : if (TREE_CODE (expr) == FUNCTION_DECL)
13607 : {
13608 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13609 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13610 : }
13611 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13612 : }
13613 : break;
13614 : case tcc_type:
13615 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13616 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13617 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13618 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13619 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13620 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13621 : if (INTEGRAL_TYPE_P (expr)
13622 : || SCALAR_FLOAT_TYPE_P (expr))
13623 : {
13624 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13625 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13626 : }
13627 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13628 : if (RECORD_OR_UNION_TYPE_P (expr))
13629 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13630 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13631 : break;
13632 : default:
13633 : break;
13634 : }
13635 : }
13636 :
13637 : /* Helper function for outputting the checksum of a tree T. When
13638 : debugging with gdb, you can "define mynext" to be "next" followed
13639 : by "call debug_fold_checksum (op0)", then just trace down till the
13640 : outputs differ. */
13641 :
13642 : DEBUG_FUNCTION void
13643 : debug_fold_checksum (const_tree t)
13644 : {
13645 : int i;
13646 : unsigned char checksum[16];
13647 : struct md5_ctx ctx;
13648 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13649 :
13650 : md5_init_ctx (&ctx);
13651 : fold_checksum_tree (t, &ctx, &ht);
13652 : md5_finish_ctx (&ctx, checksum);
13653 : ht.empty ();
13654 :
13655 : for (i = 0; i < 16; i++)
13656 : fprintf (stderr, "%d ", checksum[i]);
13657 :
13658 : fprintf (stderr, "\n");
13659 : }
13660 :
13661 : #endif
13662 :
13663 : /* Fold a unary tree expression with code CODE of type TYPE with an
13664 : operand OP0. LOC is the location of the resulting expression.
13665 : Return a folded expression if successful. Otherwise, return a tree
13666 : expression with code CODE of type TYPE with an operand OP0. */
13667 :
13668 : tree
13669 1028536185 : fold_build1_loc (location_t loc,
13670 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13671 : {
13672 1028536185 : tree tem;
13673 : #ifdef ENABLE_FOLD_CHECKING
13674 : unsigned char checksum_before[16], checksum_after[16];
13675 : struct md5_ctx ctx;
13676 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13677 :
13678 : md5_init_ctx (&ctx);
13679 : fold_checksum_tree (op0, &ctx, &ht);
13680 : md5_finish_ctx (&ctx, checksum_before);
13681 : ht.empty ();
13682 : #endif
13683 :
13684 1028536185 : tem = fold_unary_loc (loc, code, type, op0);
13685 1028536185 : if (!tem)
13686 523222105 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13687 :
13688 : #ifdef ENABLE_FOLD_CHECKING
13689 : md5_init_ctx (&ctx);
13690 : fold_checksum_tree (op0, &ctx, &ht);
13691 : md5_finish_ctx (&ctx, checksum_after);
13692 :
13693 : if (memcmp (checksum_before, checksum_after, 16))
13694 : fold_check_failed (op0, tem);
13695 : #endif
13696 1028536185 : return tem;
13697 : }
13698 :
13699 : /* Fold a binary tree expression with code CODE of type TYPE with
13700 : operands OP0 and OP1. LOC is the location of the resulting
13701 : expression. Return a folded expression if successful. Otherwise,
13702 : return a tree expression with code CODE of type TYPE with operands
13703 : OP0 and OP1. */
13704 :
13705 : tree
13706 673184497 : fold_build2_loc (location_t loc,
13707 : enum tree_code code, tree type, tree op0, tree op1
13708 : MEM_STAT_DECL)
13709 : {
13710 673184497 : tree tem;
13711 : #ifdef ENABLE_FOLD_CHECKING
13712 : unsigned char checksum_before_op0[16],
13713 : checksum_before_op1[16],
13714 : checksum_after_op0[16],
13715 : checksum_after_op1[16];
13716 : struct md5_ctx ctx;
13717 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13718 :
13719 : md5_init_ctx (&ctx);
13720 : fold_checksum_tree (op0, &ctx, &ht);
13721 : md5_finish_ctx (&ctx, checksum_before_op0);
13722 : ht.empty ();
13723 :
13724 : md5_init_ctx (&ctx);
13725 : fold_checksum_tree (op1, &ctx, &ht);
13726 : md5_finish_ctx (&ctx, checksum_before_op1);
13727 : ht.empty ();
13728 : #endif
13729 :
13730 673184497 : tem = fold_binary_loc (loc, code, type, op0, op1);
13731 673184497 : if (!tem)
13732 378442394 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13733 :
13734 : #ifdef ENABLE_FOLD_CHECKING
13735 : md5_init_ctx (&ctx);
13736 : fold_checksum_tree (op0, &ctx, &ht);
13737 : md5_finish_ctx (&ctx, checksum_after_op0);
13738 : ht.empty ();
13739 :
13740 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13741 : fold_check_failed (op0, tem);
13742 :
13743 : md5_init_ctx (&ctx);
13744 : fold_checksum_tree (op1, &ctx, &ht);
13745 : md5_finish_ctx (&ctx, checksum_after_op1);
13746 :
13747 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13748 : fold_check_failed (op1, tem);
13749 : #endif
13750 673184497 : return tem;
13751 : }
13752 :
13753 : /* Fold a ternary tree expression with code CODE of type TYPE with
13754 : operands OP0, OP1, and OP2. Return a folded expression if
13755 : successful. Otherwise, return a tree expression with code CODE of
13756 : type TYPE with operands OP0, OP1, and OP2. */
13757 :
13758 : tree
13759 38898089 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13760 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13761 : {
13762 38898089 : tree tem;
13763 : #ifdef ENABLE_FOLD_CHECKING
13764 : unsigned char checksum_before_op0[16],
13765 : checksum_before_op1[16],
13766 : checksum_before_op2[16],
13767 : checksum_after_op0[16],
13768 : checksum_after_op1[16],
13769 : checksum_after_op2[16];
13770 : struct md5_ctx ctx;
13771 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13772 :
13773 : md5_init_ctx (&ctx);
13774 : fold_checksum_tree (op0, &ctx, &ht);
13775 : md5_finish_ctx (&ctx, checksum_before_op0);
13776 : ht.empty ();
13777 :
13778 : md5_init_ctx (&ctx);
13779 : fold_checksum_tree (op1, &ctx, &ht);
13780 : md5_finish_ctx (&ctx, checksum_before_op1);
13781 : ht.empty ();
13782 :
13783 : md5_init_ctx (&ctx);
13784 : fold_checksum_tree (op2, &ctx, &ht);
13785 : md5_finish_ctx (&ctx, checksum_before_op2);
13786 : ht.empty ();
13787 : #endif
13788 :
13789 38898089 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13790 38898089 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13791 38898089 : if (!tem)
13792 36069259 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13793 :
13794 : #ifdef ENABLE_FOLD_CHECKING
13795 : md5_init_ctx (&ctx);
13796 : fold_checksum_tree (op0, &ctx, &ht);
13797 : md5_finish_ctx (&ctx, checksum_after_op0);
13798 : ht.empty ();
13799 :
13800 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13801 : fold_check_failed (op0, tem);
13802 :
13803 : md5_init_ctx (&ctx);
13804 : fold_checksum_tree (op1, &ctx, &ht);
13805 : md5_finish_ctx (&ctx, checksum_after_op1);
13806 : ht.empty ();
13807 :
13808 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13809 : fold_check_failed (op1, tem);
13810 :
13811 : md5_init_ctx (&ctx);
13812 : fold_checksum_tree (op2, &ctx, &ht);
13813 : md5_finish_ctx (&ctx, checksum_after_op2);
13814 :
13815 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13816 : fold_check_failed (op2, tem);
13817 : #endif
13818 38898089 : return tem;
13819 : }
13820 :
13821 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13822 : arguments in ARGARRAY, and a null static chain.
13823 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13824 : of type TYPE from the given operands as constructed by build_call_array. */
13825 :
13826 : tree
13827 56360555 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13828 : int nargs, tree *argarray)
13829 : {
13830 56360555 : tree tem;
13831 : #ifdef ENABLE_FOLD_CHECKING
13832 : unsigned char checksum_before_fn[16],
13833 : checksum_before_arglist[16],
13834 : checksum_after_fn[16],
13835 : checksum_after_arglist[16];
13836 : struct md5_ctx ctx;
13837 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13838 : int i;
13839 :
13840 : md5_init_ctx (&ctx);
13841 : fold_checksum_tree (fn, &ctx, &ht);
13842 : md5_finish_ctx (&ctx, checksum_before_fn);
13843 : ht.empty ();
13844 :
13845 : md5_init_ctx (&ctx);
13846 : for (i = 0; i < nargs; i++)
13847 : fold_checksum_tree (argarray[i], &ctx, &ht);
13848 : md5_finish_ctx (&ctx, checksum_before_arglist);
13849 : ht.empty ();
13850 : #endif
13851 :
13852 56360555 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13853 56360555 : if (!tem)
13854 54379049 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13855 :
13856 : #ifdef ENABLE_FOLD_CHECKING
13857 : md5_init_ctx (&ctx);
13858 : fold_checksum_tree (fn, &ctx, &ht);
13859 : md5_finish_ctx (&ctx, checksum_after_fn);
13860 : ht.empty ();
13861 :
13862 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13863 : fold_check_failed (fn, tem);
13864 :
13865 : md5_init_ctx (&ctx);
13866 : for (i = 0; i < nargs; i++)
13867 : fold_checksum_tree (argarray[i], &ctx, &ht);
13868 : md5_finish_ctx (&ctx, checksum_after_arglist);
13869 :
13870 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13871 : fold_check_failed (NULL_TREE, tem);
13872 : #endif
13873 56360555 : return tem;
13874 : }
13875 :
13876 : /* Perform constant folding and related simplification of initializer
13877 : expression EXPR. These behave identically to "fold_buildN" but ignore
13878 : potential run-time traps and exceptions that fold must preserve. */
13879 :
13880 : #define START_FOLD_INIT \
13881 : int saved_signaling_nans = flag_signaling_nans;\
13882 : int saved_trapping_math = flag_trapping_math;\
13883 : int saved_rounding_math = flag_rounding_math;\
13884 : int saved_trapv = flag_trapv;\
13885 : int saved_folding_initializer = folding_initializer;\
13886 : flag_signaling_nans = 0;\
13887 : flag_trapping_math = 0;\
13888 : flag_rounding_math = 0;\
13889 : flag_trapv = 0;\
13890 : folding_initializer = 1;
13891 :
13892 : #define END_FOLD_INIT \
13893 : flag_signaling_nans = saved_signaling_nans;\
13894 : flag_trapping_math = saved_trapping_math;\
13895 : flag_rounding_math = saved_rounding_math;\
13896 : flag_trapv = saved_trapv;\
13897 : folding_initializer = saved_folding_initializer;
13898 :
13899 : tree
13900 544402 : fold_init (tree expr)
13901 : {
13902 544402 : tree result;
13903 544402 : START_FOLD_INIT;
13904 :
13905 544402 : result = fold (expr);
13906 :
13907 544402 : END_FOLD_INIT;
13908 544402 : return result;
13909 : }
13910 :
13911 : tree
13912 2988874 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13913 : tree type, tree op)
13914 : {
13915 2988874 : tree result;
13916 2988874 : START_FOLD_INIT;
13917 :
13918 2988874 : result = fold_build1_loc (loc, code, type, op);
13919 :
13920 2988874 : END_FOLD_INIT;
13921 2988874 : return result;
13922 : }
13923 :
13924 : tree
13925 50382 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13926 : tree type, tree op0, tree op1)
13927 : {
13928 50382 : tree result;
13929 50382 : START_FOLD_INIT;
13930 :
13931 50382 : result = fold_build2_loc (loc, code, type, op0, op1);
13932 :
13933 50382 : END_FOLD_INIT;
13934 50382 : return result;
13935 : }
13936 :
13937 : tree
13938 3462 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
13939 : int nargs, tree *argarray)
13940 : {
13941 3462 : tree result;
13942 3462 : START_FOLD_INIT;
13943 :
13944 3462 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
13945 :
13946 3462 : END_FOLD_INIT;
13947 3462 : return result;
13948 : }
13949 :
13950 : tree
13951 66736515 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
13952 : tree lhs, tree rhs)
13953 : {
13954 66736515 : tree result;
13955 66736515 : START_FOLD_INIT;
13956 :
13957 66736515 : result = fold_binary_loc (loc, code, type, lhs, rhs);
13958 :
13959 66736515 : END_FOLD_INIT;
13960 66736515 : return result;
13961 : }
13962 :
13963 : #undef START_FOLD_INIT
13964 : #undef END_FOLD_INIT
13965 :
13966 : /* Determine if first argument is a multiple of second argument. Return
13967 : false if it is not, or we cannot easily determined it to be.
13968 :
13969 : An example of the sort of thing we care about (at this point; this routine
13970 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
13971 : fold cases do now) is discovering that
13972 :
13973 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13974 :
13975 : is a multiple of
13976 :
13977 : SAVE_EXPR (J * 8)
13978 :
13979 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13980 :
13981 : This code also handles discovering that
13982 :
13983 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13984 :
13985 : is a multiple of 8 so we don't have to worry about dealing with a
13986 : possible remainder.
13987 :
13988 : Note that we *look* inside a SAVE_EXPR only to determine how it was
13989 : calculated; it is not safe for fold to do much of anything else with the
13990 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13991 : at run time. For example, the latter example above *cannot* be implemented
13992 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13993 : evaluation time of the original SAVE_EXPR is not necessarily the same at
13994 : the time the new expression is evaluated. The only optimization of this
13995 : sort that would be valid is changing
13996 :
13997 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13998 :
13999 : divided by 8 to
14000 :
14001 : SAVE_EXPR (I) * SAVE_EXPR (J)
14002 :
14003 : (where the same SAVE_EXPR (J) is used in the original and the
14004 : transformed version).
14005 :
14006 : NOWRAP specifies whether all outer operations in TYPE should
14007 : be considered not wrapping. Any type conversion within TOP acts
14008 : as a barrier and we will fall back to NOWRAP being false.
14009 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14010 : as not wrapping even though they are generally using unsigned arithmetic. */
14011 :
14012 : bool
14013 1590515 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14014 : {
14015 1590515 : gimple *stmt;
14016 1590515 : tree op1, op2;
14017 :
14018 1590515 : if (operand_equal_p (top, bottom, 0))
14019 : return true;
14020 :
14021 1098783 : if (TREE_CODE (type) != INTEGER_TYPE)
14022 : return false;
14023 :
14024 1098764 : switch (TREE_CODE (top))
14025 : {
14026 702 : case BIT_AND_EXPR:
14027 : /* Bitwise and provides a power of two multiple. If the mask is
14028 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14029 702 : if (!integer_pow2p (bottom))
14030 : return false;
14031 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14032 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14033 :
14034 397105 : case MULT_EXPR:
14035 : /* If the multiplication can wrap we cannot recurse further unless
14036 : the bottom is a power of two which is where wrapping does not
14037 : matter. */
14038 397105 : if (!nowrap
14039 15108 : && !TYPE_OVERFLOW_UNDEFINED (type)
14040 401793 : && !integer_pow2p (bottom))
14041 : return false;
14042 396662 : if (TREE_CODE (bottom) == INTEGER_CST)
14043 : {
14044 394966 : op1 = TREE_OPERAND (top, 0);
14045 394966 : op2 = TREE_OPERAND (top, 1);
14046 394966 : if (TREE_CODE (op1) == INTEGER_CST)
14047 0 : std::swap (op1, op2);
14048 394966 : if (TREE_CODE (op2) == INTEGER_CST)
14049 : {
14050 384772 : if (multiple_of_p (type, op2, bottom, nowrap))
14051 : return true;
14052 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14053 3301 : if (multiple_of_p (type, bottom, op2, nowrap))
14054 : {
14055 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14056 1888 : wi::to_widest (op2));
14057 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14058 : {
14059 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14060 1888 : return multiple_of_p (type, op1, op2, nowrap);
14061 : }
14062 1888 : }
14063 1413 : return multiple_of_p (type, op1, bottom, nowrap);
14064 : }
14065 : }
14066 11890 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14067 11890 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14068 :
14069 403 : case LSHIFT_EXPR:
14070 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14071 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14072 : {
14073 403 : op1 = TREE_OPERAND (top, 1);
14074 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14075 : {
14076 403 : wide_int mul_op
14077 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14078 806 : return multiple_of_p (type,
14079 806 : wide_int_to_tree (type, mul_op), bottom,
14080 : nowrap);
14081 403 : }
14082 : }
14083 : return false;
14084 :
14085 224933 : case MINUS_EXPR:
14086 224933 : case PLUS_EXPR:
14087 : /* If the addition or subtraction can wrap we cannot recurse further
14088 : unless bottom is a power of two which is where wrapping does not
14089 : matter. */
14090 224933 : if (!nowrap
14091 174724 : && !TYPE_OVERFLOW_UNDEFINED (type)
14092 398213 : && !integer_pow2p (bottom))
14093 : return false;
14094 :
14095 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14096 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14097 : but 0xfffffffd is not. */
14098 196069 : op1 = TREE_OPERAND (top, 1);
14099 196069 : if (TREE_CODE (top) == PLUS_EXPR
14100 189821 : && nowrap
14101 44048 : && TYPE_UNSIGNED (type)
14102 239407 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14103 27687 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14104 :
14105 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14106 : precisely, so be conservative here checking if both op0 and op1
14107 : are multiple of bottom. Note we check the second operand first
14108 : since it's usually simpler. */
14109 196069 : return (multiple_of_p (type, op1, bottom, nowrap)
14110 196069 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14111 :
14112 144677 : CASE_CONVERT:
14113 : /* Can't handle conversions from non-integral or wider integral type. */
14114 144677 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14115 144677 : || (TYPE_PRECISION (type)
14116 38896 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14117 : return false;
14118 : /* NOWRAP only extends to operations in the outermost type so
14119 : make sure to strip it off here. */
14120 38638 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14121 77276 : TREE_OPERAND (top, 0), bottom, false);
14122 :
14123 12996 : case SAVE_EXPR:
14124 12996 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14125 :
14126 86 : case COND_EXPR:
14127 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14128 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14129 :
14130 139386 : case INTEGER_CST:
14131 139386 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14132 2702 : return false;
14133 136684 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14134 : SIGNED);
14135 :
14136 61947 : case SSA_NAME:
14137 61947 : if (TREE_CODE (bottom) == INTEGER_CST
14138 58766 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14139 120713 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14140 : {
14141 25543 : enum tree_code code = gimple_assign_rhs_code (stmt);
14142 :
14143 : /* Check for special cases to see if top is defined as multiple
14144 : of bottom:
14145 :
14146 : top = (X & ~(bottom - 1) ; bottom is power of 2
14147 :
14148 : or
14149 :
14150 : Y = X % bottom
14151 : top = X - Y. */
14152 25543 : if (code == BIT_AND_EXPR
14153 310 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14154 310 : && TREE_CODE (op2) == INTEGER_CST
14155 202 : && integer_pow2p (bottom)
14156 25745 : && wi::multiple_of_p (wi::to_widest (op2),
14157 202 : wi::to_widest (bottom), SIGNED))
14158 193 : return true;
14159 :
14160 25350 : op1 = gimple_assign_rhs1 (stmt);
14161 25350 : if (code == MINUS_EXPR
14162 1551 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14163 1551 : && TREE_CODE (op2) == SSA_NAME
14164 1551 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14165 1551 : && gimple_code (stmt) == GIMPLE_ASSIGN
14166 1290 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14167 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14168 25414 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14169 : return true;
14170 : }
14171 :
14172 : /* fall through */
14173 :
14174 : default:
14175 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14176 : return multiple_p (wi::to_poly_widest (top),
14177 : wi::to_poly_widest (bottom));
14178 :
14179 : return false;
14180 : }
14181 : }
14182 :
14183 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14184 : This function returns true for integer expressions, and returns
14185 : false if uncertain. */
14186 :
14187 : bool
14188 508524 : tree_expr_finite_p (const_tree x)
14189 : {
14190 508528 : machine_mode mode = element_mode (x);
14191 508528 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14192 : return true;
14193 508286 : switch (TREE_CODE (x))
14194 : {
14195 592 : case REAL_CST:
14196 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14197 0 : case COMPLEX_CST:
14198 0 : return tree_expr_finite_p (TREE_REALPART (x))
14199 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14200 : case FLOAT_EXPR:
14201 : return true;
14202 4 : case ABS_EXPR:
14203 4 : case CONVERT_EXPR:
14204 4 : case NON_LVALUE_EXPR:
14205 4 : case NEGATE_EXPR:
14206 4 : case SAVE_EXPR:
14207 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14208 0 : case MIN_EXPR:
14209 0 : case MAX_EXPR:
14210 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14211 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14212 0 : case COND_EXPR:
14213 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14214 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14215 38 : case CALL_EXPR:
14216 38 : switch (get_call_combined_fn (x))
14217 : {
14218 0 : CASE_CFN_FABS:
14219 0 : CASE_CFN_FABS_FN:
14220 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14221 0 : CASE_CFN_FMAX:
14222 0 : CASE_CFN_FMAX_FN:
14223 0 : CASE_CFN_FMIN:
14224 0 : CASE_CFN_FMIN_FN:
14225 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14226 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14227 : default:
14228 : return false;
14229 : }
14230 :
14231 : default:
14232 : return false;
14233 : }
14234 : }
14235 :
14236 : /* Return true if expression X evaluates to an infinity.
14237 : This function returns false for integer expressions. */
14238 :
14239 : bool
14240 1197015 : tree_expr_infinite_p (const_tree x)
14241 : {
14242 1197465 : if (!HONOR_INFINITIES (x))
14243 : return false;
14244 1197220 : switch (TREE_CODE (x))
14245 : {
14246 0 : case REAL_CST:
14247 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14248 450 : case ABS_EXPR:
14249 450 : case NEGATE_EXPR:
14250 450 : case NON_LVALUE_EXPR:
14251 450 : case SAVE_EXPR:
14252 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14253 0 : case COND_EXPR:
14254 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14255 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14256 : default:
14257 : return false;
14258 : }
14259 : }
14260 :
14261 : /* Return true if expression X could evaluate to an infinity.
14262 : This function returns false for integer expressions, and returns
14263 : true if uncertain. */
14264 :
14265 : bool
14266 807744 : tree_expr_maybe_infinite_p (const_tree x)
14267 : {
14268 807752 : if (!HONOR_INFINITIES (x))
14269 : return false;
14270 807296 : switch (TREE_CODE (x))
14271 : {
14272 273 : case REAL_CST:
14273 273 : return real_isinf (TREE_REAL_CST_PTR (x));
14274 : case FLOAT_EXPR:
14275 : return false;
14276 8 : case ABS_EXPR:
14277 8 : case NEGATE_EXPR:
14278 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14279 1 : case COND_EXPR:
14280 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14281 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14282 : default:
14283 : return true;
14284 : }
14285 : }
14286 :
14287 : /* Return true if expression X evaluates to a signaling NaN.
14288 : This function returns false for integer expressions. */
14289 :
14290 : bool
14291 385 : tree_expr_signaling_nan_p (const_tree x)
14292 : {
14293 385 : if (!HONOR_SNANS (x))
14294 : return false;
14295 124 : switch (TREE_CODE (x))
14296 : {
14297 124 : case REAL_CST:
14298 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14299 0 : case NON_LVALUE_EXPR:
14300 0 : case SAVE_EXPR:
14301 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14302 0 : case COND_EXPR:
14303 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14304 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14305 : default:
14306 : return false;
14307 : }
14308 : }
14309 :
14310 : /* Return true if expression X could evaluate to a signaling NaN.
14311 : This function returns false for integer expressions, and returns
14312 : true if uncertain. */
14313 :
14314 : bool
14315 728269 : tree_expr_maybe_signaling_nan_p (const_tree x)
14316 : {
14317 728269 : if (!HONOR_SNANS (x))
14318 : return false;
14319 5028 : switch (TREE_CODE (x))
14320 : {
14321 1452 : case REAL_CST:
14322 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14323 : case FLOAT_EXPR:
14324 : return false;
14325 0 : case ABS_EXPR:
14326 0 : case CONVERT_EXPR:
14327 0 : case NEGATE_EXPR:
14328 0 : case NON_LVALUE_EXPR:
14329 0 : case SAVE_EXPR:
14330 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14331 0 : case MIN_EXPR:
14332 0 : case MAX_EXPR:
14333 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14334 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14335 0 : case COND_EXPR:
14336 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14337 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14338 0 : case CALL_EXPR:
14339 0 : switch (get_call_combined_fn (x))
14340 : {
14341 0 : CASE_CFN_FABS:
14342 0 : CASE_CFN_FABS_FN:
14343 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14344 0 : CASE_CFN_FMAX:
14345 0 : CASE_CFN_FMAX_FN:
14346 0 : CASE_CFN_FMIN:
14347 0 : CASE_CFN_FMIN_FN:
14348 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14349 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14350 : default:
14351 : return true;
14352 : }
14353 : default:
14354 : return true;
14355 : }
14356 : }
14357 :
14358 : /* Return true if expression X evaluates to a NaN.
14359 : This function returns false for integer expressions. */
14360 :
14361 : bool
14362 3780652 : tree_expr_nan_p (const_tree x)
14363 : {
14364 4133264 : if (!HONOR_NANS (x))
14365 : return false;
14366 4132914 : switch (TREE_CODE (x))
14367 : {
14368 3806 : case REAL_CST:
14369 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14370 352612 : case NON_LVALUE_EXPR:
14371 352612 : case SAVE_EXPR:
14372 352612 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14373 956 : case COND_EXPR:
14374 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14375 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14376 : default:
14377 : return false;
14378 : }
14379 : }
14380 :
14381 : /* Return true if expression X could evaluate to a NaN.
14382 : This function returns false for integer expressions, and returns
14383 : true if uncertain. */
14384 :
14385 : bool
14386 5019329 : tree_expr_maybe_nan_p (const_tree x)
14387 : {
14388 7265011 : if (!HONOR_NANS (x))
14389 : return false;
14390 7148111 : switch (TREE_CODE (x))
14391 : {
14392 3422 : case REAL_CST:
14393 3422 : return real_isnan (TREE_REAL_CST_PTR (x));
14394 : case FLOAT_EXPR:
14395 : return false;
14396 14750 : case PLUS_EXPR:
14397 14750 : case MINUS_EXPR:
14398 14750 : case MULT_EXPR:
14399 14750 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14400 14750 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14401 2245682 : case ABS_EXPR:
14402 2245682 : case CONVERT_EXPR:
14403 2245682 : case NEGATE_EXPR:
14404 2245682 : case NON_LVALUE_EXPR:
14405 2245682 : case SAVE_EXPR:
14406 2245682 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14407 176 : case MIN_EXPR:
14408 176 : case MAX_EXPR:
14409 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14410 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14411 599 : case COND_EXPR:
14412 599 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14413 599 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14414 1085 : case CALL_EXPR:
14415 1085 : switch (get_call_combined_fn (x))
14416 : {
14417 0 : CASE_CFN_FABS:
14418 0 : CASE_CFN_FABS_FN:
14419 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14420 108 : CASE_CFN_FMAX:
14421 108 : CASE_CFN_FMAX_FN:
14422 108 : CASE_CFN_FMIN:
14423 108 : CASE_CFN_FMIN_FN:
14424 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14425 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14426 : default:
14427 : return true;
14428 : }
14429 : default:
14430 : return true;
14431 : }
14432 : }
14433 :
14434 : /* Return true if expression X could evaluate to -0.0.
14435 : This function returns true if uncertain. */
14436 :
14437 : bool
14438 603707 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14439 : {
14440 603707 : if (!HONOR_SIGNED_ZEROS (x))
14441 : return false;
14442 603707 : switch (TREE_CODE (x))
14443 : {
14444 0 : case REAL_CST:
14445 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14446 : case INTEGER_CST:
14447 : case FLOAT_EXPR:
14448 : case ABS_EXPR:
14449 : return false;
14450 0 : case NON_LVALUE_EXPR:
14451 0 : case SAVE_EXPR:
14452 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14453 0 : case COND_EXPR:
14454 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14455 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14456 2 : case CALL_EXPR:
14457 2 : switch (get_call_combined_fn (x))
14458 : {
14459 : CASE_CFN_FABS:
14460 : CASE_CFN_FABS_FN:
14461 : return false;
14462 : default:
14463 : break;
14464 : }
14465 : default:
14466 : break;
14467 : }
14468 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14469 : * but currently those predicates require tree and not const_tree. */
14470 : return true;
14471 : }
14472 :
14473 : #define tree_expr_nonnegative_p(X, Y) \
14474 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14475 :
14476 : #define RECURSE(X) \
14477 : ((tree_expr_nonnegative_p) (X, depth + 1))
14478 :
14479 : /* Return true if CODE or TYPE is known to be non-negative. */
14480 :
14481 : static bool
14482 26255444 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14483 : {
14484 26255444 : if (!VECTOR_TYPE_P (type)
14485 26219363 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14486 52474542 : && truth_value_p (code))
14487 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14488 : have a signed:1 type (where the value is -1 and 0). */
14489 : return true;
14490 : return false;
14491 : }
14492 :
14493 : /* Return true if (CODE OP0) is known to be non-negative.
14494 : DEPTH is the current nesting depth of the query. */
14495 :
14496 : bool
14497 2525283 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14498 : {
14499 2525283 : if (TYPE_UNSIGNED (type))
14500 : return true;
14501 :
14502 2077353 : switch (code)
14503 : {
14504 291125 : case ABS_EXPR:
14505 : /* We can't return 1 if flag_wrapv is set because
14506 : ABS_EXPR<INT_MIN> = INT_MIN. */
14507 291125 : if (!ANY_INTEGRAL_TYPE_P (type))
14508 : return true;
14509 10479 : if (TYPE_OVERFLOW_UNDEFINED (type))
14510 : return true;
14511 : break;
14512 :
14513 71927 : case NON_LVALUE_EXPR:
14514 71927 : case FLOAT_EXPR:
14515 71927 : case FIX_TRUNC_EXPR:
14516 71927 : return RECURSE (op0);
14517 :
14518 1673020 : CASE_CONVERT:
14519 1673020 : {
14520 1673020 : tree inner_type = TREE_TYPE (op0);
14521 1673020 : tree outer_type = type;
14522 :
14523 1673020 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14524 : {
14525 406345 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14526 406345 : return RECURSE (op0);
14527 0 : if (INTEGRAL_TYPE_P (inner_type))
14528 : {
14529 0 : if (TYPE_UNSIGNED (inner_type))
14530 : return true;
14531 0 : return RECURSE (op0);
14532 : }
14533 : }
14534 1266675 : else if (INTEGRAL_TYPE_P (outer_type))
14535 : {
14536 1266598 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14537 0 : return RECURSE (op0);
14538 1266598 : if (INTEGRAL_TYPE_P (inner_type))
14539 1260583 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14540 1260583 : && TYPE_UNSIGNED (inner_type);
14541 : }
14542 : }
14543 : break;
14544 :
14545 41281 : default:
14546 41281 : return tree_simple_nonnegative_warnv_p (code, type);
14547 : }
14548 :
14549 : /* We don't know sign of `t', so be conservative and return false. */
14550 : return false;
14551 : }
14552 :
14553 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14554 : DEPTH is the current nesting depth of the query. */
14555 :
14556 : bool
14557 5282074 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14558 : tree op1, int depth)
14559 : {
14560 5282074 : if (TYPE_UNSIGNED (type))
14561 : return true;
14562 :
14563 5000004 : switch (code)
14564 : {
14565 1339494 : case POINTER_PLUS_EXPR:
14566 1339494 : case PLUS_EXPR:
14567 1339494 : if (FLOAT_TYPE_P (type))
14568 49007 : return RECURSE (op0) && RECURSE (op1);
14569 :
14570 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14571 : both unsigned and at least 2 bits shorter than the result. */
14572 1290487 : if (TREE_CODE (type) == INTEGER_TYPE
14573 1284391 : && TREE_CODE (op0) == NOP_EXPR
14574 17117 : && TREE_CODE (op1) == NOP_EXPR)
14575 : {
14576 200 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14577 200 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14578 200 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14579 301 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14580 : {
14581 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14582 95 : TYPE_PRECISION (inner2)) + 1;
14583 95 : return prec < TYPE_PRECISION (type);
14584 : }
14585 : }
14586 : break;
14587 :
14588 180906 : case MULT_EXPR:
14589 180906 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14590 : {
14591 : /* x * x is always non-negative for floating point x
14592 : or without overflow. */
14593 163969 : if (operand_equal_p (op0, op1, 0)
14594 163969 : || (RECURSE (op0) && RECURSE (op1)))
14595 1956 : return true;
14596 : }
14597 :
14598 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14599 : both unsigned and their total bits is shorter than the result. */
14600 178950 : if (TREE_CODE (type) == INTEGER_TYPE
14601 108293 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14602 122 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14603 : {
14604 116 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14605 116 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14606 116 : : TREE_TYPE (op0);
14607 116 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14608 116 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14609 116 : : TREE_TYPE (op1);
14610 :
14611 116 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14612 116 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14613 :
14614 116 : if (TREE_CODE (op0) == INTEGER_CST)
14615 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14616 :
14617 116 : if (TREE_CODE (op1) == INTEGER_CST)
14618 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14619 :
14620 116 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14621 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14622 : {
14623 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14624 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14625 0 : : TYPE_PRECISION (inner0);
14626 :
14627 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14628 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14629 0 : : TYPE_PRECISION (inner1);
14630 :
14631 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14632 : }
14633 : }
14634 : return false;
14635 :
14636 26593 : case BIT_AND_EXPR:
14637 26593 : return RECURSE (op0) || RECURSE (op1);
14638 :
14639 52537 : case MAX_EXPR:
14640 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14641 : things. */
14642 52537 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14643 76 : return RECURSE (op0) && RECURSE (op1);
14644 52461 : return RECURSE (op0) || RECURSE (op1);
14645 :
14646 176215 : case BIT_IOR_EXPR:
14647 176215 : case BIT_XOR_EXPR:
14648 176215 : case MIN_EXPR:
14649 176215 : case RDIV_EXPR:
14650 176215 : case TRUNC_DIV_EXPR:
14651 176215 : case CEIL_DIV_EXPR:
14652 176215 : case FLOOR_DIV_EXPR:
14653 176215 : case ROUND_DIV_EXPR:
14654 176215 : return RECURSE (op0) && RECURSE (op1);
14655 :
14656 87889 : case TRUNC_MOD_EXPR:
14657 87889 : return RECURSE (op0);
14658 :
14659 230 : case FLOOR_MOD_EXPR:
14660 230 : return RECURSE (op1);
14661 :
14662 3136140 : case CEIL_MOD_EXPR:
14663 3136140 : case ROUND_MOD_EXPR:
14664 3136140 : default:
14665 3136140 : return tree_simple_nonnegative_warnv_p (code, type);
14666 : }
14667 :
14668 : /* We don't know sign of `t', so be conservative and return false. */
14669 : return false;
14670 : }
14671 :
14672 : /* Return true if T is known to be non-negative.
14673 : DEPTH is the current nesting depth of the query. */
14674 :
14675 : bool
14676 24974211 : tree_single_nonnegative_p (tree t, int depth)
14677 : {
14678 24974211 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14679 : return true;
14680 :
14681 21497407 : switch (TREE_CODE (t))
14682 : {
14683 2551688 : case INTEGER_CST:
14684 2551688 : return tree_int_cst_sgn (t) >= 0;
14685 :
14686 917640 : case REAL_CST:
14687 917640 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14688 :
14689 0 : case FIXED_CST:
14690 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14691 :
14692 928 : case COND_EXPR:
14693 928 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14694 :
14695 8843209 : case SSA_NAME:
14696 : /* Limit the depth of recursion to avoid quadratic behavior.
14697 : This is expected to catch almost all occurrences in practice.
14698 : If this code misses important cases that unbounded recursion
14699 : would not, passes that need this information could be revised
14700 : to provide it through dataflow propagation. */
14701 8843209 : return (!name_registered_for_update_p (t)
14702 8843208 : && depth < param_max_ssa_name_query_depth
14703 17537924 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14704 :
14705 9183942 : default:
14706 9183942 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14707 : }
14708 : }
14709 :
14710 : /* Return true if T is known to be non-negative.
14711 : DEPTH is the current nesting depth of the query. */
14712 :
14713 : bool
14714 13987987 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14715 : int depth)
14716 : {
14717 13987987 : switch (fn)
14718 : {
14719 : CASE_CFN_ACOS:
14720 : CASE_CFN_ACOS_FN:
14721 : CASE_CFN_ACOSH:
14722 : CASE_CFN_ACOSH_FN:
14723 : CASE_CFN_ACOSPI:
14724 : CASE_CFN_ACOSPI_FN:
14725 : CASE_CFN_CABS:
14726 : CASE_CFN_CABS_FN:
14727 : CASE_CFN_COSH:
14728 : CASE_CFN_COSH_FN:
14729 : CASE_CFN_ERFC:
14730 : CASE_CFN_ERFC_FN:
14731 : CASE_CFN_EXP:
14732 : CASE_CFN_EXP_FN:
14733 : CASE_CFN_EXP10:
14734 : CASE_CFN_EXP2:
14735 : CASE_CFN_EXP2_FN:
14736 : CASE_CFN_FABS:
14737 : CASE_CFN_FABS_FN:
14738 : CASE_CFN_FDIM:
14739 : CASE_CFN_FDIM_FN:
14740 : CASE_CFN_HYPOT:
14741 : CASE_CFN_HYPOT_FN:
14742 : CASE_CFN_POW10:
14743 : CASE_CFN_FFS:
14744 : CASE_CFN_PARITY:
14745 : CASE_CFN_POPCOUNT:
14746 : CASE_CFN_CLRSB:
14747 : CASE_CFN_BSWAP:
14748 : CASE_CFN_BITREVERSE:
14749 : /* Always true. */
14750 : return true;
14751 :
14752 952 : CASE_CFN_CLZ:
14753 952 : CASE_CFN_CTZ:
14754 952 : if (arg1)
14755 2 : return RECURSE (arg1);
14756 : return true;
14757 :
14758 946 : CASE_CFN_SQRT:
14759 946 : CASE_CFN_SQRT_FN:
14760 : /* sqrt(-0.0) is -0.0. */
14761 946 : if (!HONOR_SIGNED_ZEROS (type))
14762 : return true;
14763 914 : return RECURSE (arg0);
14764 :
14765 54796 : CASE_CFN_ASINH:
14766 54796 : CASE_CFN_ASINH_FN:
14767 54796 : CASE_CFN_ASINPI:
14768 54796 : CASE_CFN_ASINPI_FN:
14769 54796 : CASE_CFN_ATAN:
14770 54796 : CASE_CFN_ATAN_FN:
14771 54796 : CASE_CFN_ATANH:
14772 54796 : CASE_CFN_ATANH_FN:
14773 54796 : CASE_CFN_ATANPI:
14774 54796 : CASE_CFN_ATANPI_FN:
14775 54796 : CASE_CFN_CBRT:
14776 54796 : CASE_CFN_CBRT_FN:
14777 54796 : CASE_CFN_CEIL:
14778 54796 : CASE_CFN_CEIL_FN:
14779 54796 : CASE_CFN_ERF:
14780 54796 : CASE_CFN_ERF_FN:
14781 54796 : CASE_CFN_EXPM1:
14782 54796 : CASE_CFN_EXPM1_FN:
14783 54796 : CASE_CFN_FLOOR:
14784 54796 : CASE_CFN_FLOOR_FN:
14785 54796 : CASE_CFN_FMOD:
14786 54796 : CASE_CFN_FMOD_FN:
14787 54796 : CASE_CFN_FREXP:
14788 54796 : CASE_CFN_FREXP_FN:
14789 54796 : CASE_CFN_ICEIL:
14790 54796 : CASE_CFN_IFLOOR:
14791 54796 : CASE_CFN_IRINT:
14792 54796 : CASE_CFN_IROUND:
14793 54796 : CASE_CFN_LCEIL:
14794 54796 : CASE_CFN_LDEXP:
14795 54796 : CASE_CFN_LFLOOR:
14796 54796 : CASE_CFN_LLCEIL:
14797 54796 : CASE_CFN_LLFLOOR:
14798 54796 : CASE_CFN_LLRINT:
14799 54796 : CASE_CFN_LLRINT_FN:
14800 54796 : CASE_CFN_LLROUND:
14801 54796 : CASE_CFN_LLROUND_FN:
14802 54796 : CASE_CFN_LRINT:
14803 54796 : CASE_CFN_LRINT_FN:
14804 54796 : CASE_CFN_LROUND:
14805 54796 : CASE_CFN_LROUND_FN:
14806 54796 : CASE_CFN_MODF:
14807 54796 : CASE_CFN_MODF_FN:
14808 54796 : CASE_CFN_NEARBYINT:
14809 54796 : CASE_CFN_NEARBYINT_FN:
14810 54796 : CASE_CFN_RINT:
14811 54796 : CASE_CFN_RINT_FN:
14812 54796 : CASE_CFN_ROUND:
14813 54796 : CASE_CFN_ROUND_FN:
14814 54796 : CASE_CFN_ROUNDEVEN:
14815 54796 : CASE_CFN_ROUNDEVEN_FN:
14816 54796 : CASE_CFN_SCALB:
14817 54796 : CASE_CFN_SCALBLN:
14818 54796 : CASE_CFN_SCALBLN_FN:
14819 54796 : CASE_CFN_SCALBN:
14820 54796 : CASE_CFN_SCALBN_FN:
14821 54796 : CASE_CFN_SIGNBIT:
14822 54796 : CASE_CFN_SIGNIFICAND:
14823 54796 : CASE_CFN_SINH:
14824 54796 : CASE_CFN_SINH_FN:
14825 54796 : CASE_CFN_TANH:
14826 54796 : CASE_CFN_TANH_FN:
14827 54796 : CASE_CFN_TRUNC:
14828 54796 : CASE_CFN_TRUNC_FN:
14829 : /* True if the 1st argument is nonnegative. */
14830 54796 : return RECURSE (arg0);
14831 :
14832 1319 : CASE_CFN_FMAX:
14833 1319 : CASE_CFN_FMAX_FN:
14834 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14835 : things. In the presence of sNaNs, we're only guaranteed to be
14836 : non-negative if both operands are non-negative. In the presence
14837 : of qNaNs, we're non-negative if either operand is non-negative
14838 : and can't be a qNaN, or if both operands are non-negative. */
14839 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14840 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14841 136 : return RECURSE (arg0) && RECURSE (arg1);
14842 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14843 332 : || RECURSE (arg1))
14844 851 : : (RECURSE (arg1)
14845 851 : && !tree_expr_maybe_nan_p (arg1));
14846 :
14847 910 : CASE_CFN_FMIN:
14848 910 : CASE_CFN_FMIN_FN:
14849 : /* True if the 1st AND 2nd arguments are nonnegative. */
14850 910 : return RECURSE (arg0) && RECURSE (arg1);
14851 :
14852 769 : CASE_CFN_COPYSIGN:
14853 769 : CASE_CFN_COPYSIGN_FN:
14854 : /* True if the 2nd argument is nonnegative. */
14855 769 : return RECURSE (arg1);
14856 :
14857 2302 : CASE_CFN_POWI:
14858 : /* True if the 1st argument is nonnegative or the second
14859 : argument is an even integer. */
14860 2302 : if (TREE_CODE (arg1) == INTEGER_CST
14861 2302 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14862 : return true;
14863 2221 : return RECURSE (arg0);
14864 :
14865 4912 : CASE_CFN_POW:
14866 4912 : CASE_CFN_POW_FN:
14867 : /* True if the 1st argument is nonnegative or the second
14868 : argument is an even integer valued real. */
14869 4912 : if (TREE_CODE (arg1) == REAL_CST)
14870 : {
14871 2208 : REAL_VALUE_TYPE c;
14872 2208 : HOST_WIDE_INT n;
14873 :
14874 2208 : c = TREE_REAL_CST (arg1);
14875 2208 : n = real_to_integer (&c);
14876 2208 : if ((n & 1) == 0)
14877 : {
14878 1579 : REAL_VALUE_TYPE cint;
14879 1579 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14880 1579 : if (real_identical (&c, &cint))
14881 574 : return true;
14882 : }
14883 : }
14884 4338 : return RECURSE (arg0);
14885 :
14886 13891308 : default:
14887 13891308 : break;
14888 : }
14889 13891308 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14890 : }
14891 :
14892 : /* Return true if T is known to be non-negative.
14893 : DEPTH is the current nesting depth of the query. */
14894 :
14895 : static bool
14896 1716030 : tree_invalid_nonnegative_p (tree t, int depth)
14897 : {
14898 1716030 : enum tree_code code = TREE_CODE (t);
14899 1716030 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14900 : return true;
14901 :
14902 1303857 : switch (code)
14903 : {
14904 268 : case TARGET_EXPR:
14905 268 : {
14906 268 : tree temp = TARGET_EXPR_SLOT (t);
14907 268 : t = TARGET_EXPR_INITIAL (t);
14908 :
14909 : /* If the initializer is non-void, then it's a normal expression
14910 : that will be assigned to the slot. */
14911 268 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14912 66 : return RECURSE (t);
14913 :
14914 : /* Otherwise, the initializer sets the slot in some way. One common
14915 : way is an assignment statement at the end of the initializer. */
14916 404 : while (1)
14917 : {
14918 404 : if (TREE_CODE (t) == BIND_EXPR)
14919 202 : t = expr_last (BIND_EXPR_BODY (t));
14920 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14921 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
14922 0 : t = expr_last (TREE_OPERAND (t, 0));
14923 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
14924 0 : t = expr_last (t);
14925 : else
14926 : break;
14927 : }
14928 202 : if (TREE_CODE (t) == MODIFY_EXPR
14929 202 : && TREE_OPERAND (t, 0) == temp)
14930 202 : return RECURSE (TREE_OPERAND (t, 1));
14931 :
14932 : return false;
14933 : }
14934 :
14935 634182 : case CALL_EXPR:
14936 634182 : {
14937 634182 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14938 634182 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14939 :
14940 634182 : return tree_call_nonnegative_p (TREE_TYPE (t),
14941 : get_call_combined_fn (t),
14942 : arg0,
14943 : arg1,
14944 634182 : depth);
14945 : }
14946 3385 : case COMPOUND_EXPR:
14947 3385 : case MODIFY_EXPR:
14948 3385 : return RECURSE (TREE_OPERAND (t, 1));
14949 :
14950 15 : case BIND_EXPR:
14951 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
14952 :
14953 663234 : case SAVE_EXPR:
14954 663234 : return RECURSE (TREE_OPERAND (t, 0));
14955 :
14956 2773 : default:
14957 2773 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14958 : }
14959 : }
14960 :
14961 : #undef RECURSE
14962 : #undef tree_expr_nonnegative_p
14963 :
14964 : /* Return true if T is known to be non-negative.
14965 : DEPTH is the current nesting depth of the query. */
14966 :
14967 : bool
14968 26220651 : tree_expr_nonnegative_p (tree t, int depth)
14969 : {
14970 26220651 : enum tree_code code;
14971 26220651 : if (error_operand_p (t))
14972 : return false;
14973 :
14974 26220650 : code = TREE_CODE (t);
14975 26220650 : switch (TREE_CODE_CLASS (code))
14976 : {
14977 1403678 : case tcc_binary:
14978 1403678 : case tcc_comparison:
14979 1403678 : return tree_binary_nonnegative_p (TREE_CODE (t),
14980 1403678 : TREE_TYPE (t),
14981 1403678 : TREE_OPERAND (t, 0),
14982 1403678 : TREE_OPERAND (t, 1),
14983 1403678 : depth);
14984 :
14985 1974908 : case tcc_unary:
14986 1974908 : return tree_unary_nonnegative_p (TREE_CODE (t),
14987 1974908 : TREE_TYPE (t),
14988 1974908 : TREE_OPERAND (t, 0),
14989 1974908 : depth);
14990 :
14991 13102719 : case tcc_constant:
14992 13102719 : case tcc_declaration:
14993 13102719 : case tcc_reference:
14994 13102719 : return tree_single_nonnegative_p (t, depth);
14995 :
14996 9739345 : default:
14997 9739345 : break;
14998 : }
14999 :
15000 9739345 : switch (code)
15001 : {
15002 7 : case TRUTH_AND_EXPR:
15003 7 : case TRUTH_OR_EXPR:
15004 7 : case TRUTH_XOR_EXPR:
15005 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15006 7 : TREE_TYPE (t),
15007 7 : TREE_OPERAND (t, 0),
15008 7 : TREE_OPERAND (t, 1),
15009 7 : depth);
15010 72 : case TRUTH_NOT_EXPR:
15011 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15012 72 : TREE_TYPE (t),
15013 72 : TREE_OPERAND (t, 0),
15014 72 : depth);
15015 :
15016 8023236 : case COND_EXPR:
15017 8023236 : case CONSTRUCTOR:
15018 8023236 : case OBJ_TYPE_REF:
15019 8023236 : case ADDR_EXPR:
15020 8023236 : case WITH_SIZE_EXPR:
15021 8023236 : case SSA_NAME:
15022 8023236 : return tree_single_nonnegative_p (t, depth);
15023 :
15024 1716030 : default:
15025 1716030 : return tree_invalid_nonnegative_p (t, depth);
15026 : }
15027 : }
15028 :
15029 :
15030 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15031 : For floating point we further ensure that T is not denormal.
15032 : Similar logic is present in nonzero_address in rtlanal.h. */
15033 :
15034 : bool
15035 1556838 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15036 : {
15037 1556838 : switch (code)
15038 : {
15039 1 : case ABS_EXPR:
15040 1 : return tree_expr_nonzero_p (op0);
15041 :
15042 910384 : case NOP_EXPR:
15043 910384 : {
15044 910384 : tree inner_type = TREE_TYPE (op0);
15045 910384 : tree outer_type = type;
15046 :
15047 910384 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15048 910384 : && tree_expr_nonzero_p (op0));
15049 : }
15050 28113 : break;
15051 :
15052 28113 : case NON_LVALUE_EXPR:
15053 28113 : return tree_expr_nonzero_p (op0);
15054 :
15055 : default:
15056 : break;
15057 : }
15058 :
15059 : return false;
15060 : }
15061 :
15062 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15063 : For floating point we further ensure that T is not denormal.
15064 : Similar logic is present in nonzero_address in rtlanal.h. */
15065 :
15066 : bool
15067 3001272 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15068 : {
15069 3001272 : switch (code)
15070 : {
15071 482756 : case POINTER_PLUS_EXPR:
15072 482756 : case PLUS_EXPR:
15073 482756 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15074 : {
15075 : /* With the presence of negative values it is hard
15076 : to say something. */
15077 108953 : if (!tree_expr_nonnegative_p (op0)
15078 108953 : || !tree_expr_nonnegative_p (op1))
15079 106506 : return false;
15080 : /* One of operands must be positive and the other non-negative. */
15081 2447 : return (tree_expr_nonzero_p (op0)
15082 2447 : || tree_expr_nonzero_p (op1));
15083 : }
15084 : break;
15085 :
15086 19382 : case MULT_EXPR:
15087 19382 : if (TYPE_OVERFLOW_UNDEFINED (type))
15088 : {
15089 547 : if (tree_expr_nonzero_p (op0)
15090 547 : && tree_expr_nonzero_p (op1))
15091 : return true;
15092 : }
15093 : break;
15094 :
15095 : case MIN_EXPR:
15096 : break;
15097 :
15098 31 : case MAX_EXPR:
15099 31 : if (tree_expr_nonzero_p (op0))
15100 : {
15101 :
15102 : /* When both operands are nonzero, then MAX must be too. */
15103 0 : if (tree_expr_nonzero_p (op1))
15104 : return true;
15105 :
15106 : /* MAX where operand 0 is positive is positive. */
15107 0 : return tree_expr_nonnegative_p (op0);
15108 : }
15109 : /* MAX where operand 1 is positive is positive. */
15110 31 : else if (tree_expr_nonzero_p (op1)
15111 31 : && tree_expr_nonnegative_p (op1))
15112 : return true;
15113 : break;
15114 :
15115 267377 : case BIT_IOR_EXPR:
15116 267377 : return (tree_expr_nonzero_p (op1)
15117 267377 : || tree_expr_nonzero_p (op0));
15118 :
15119 : default:
15120 : break;
15121 : }
15122 :
15123 : return false;
15124 : }
15125 :
15126 : /* Return true when T is an address and is known to be nonzero.
15127 : For floating point we further ensure that T is not denormal.
15128 : Similar logic is present in nonzero_address in rtlanal.h. */
15129 :
15130 : bool
15131 152291708 : tree_single_nonzero_p (tree t)
15132 : {
15133 152291708 : switch (TREE_CODE (t))
15134 : {
15135 1157906 : case INTEGER_CST:
15136 1157906 : return !integer_zerop (t);
15137 :
15138 11601576 : case ADDR_EXPR:
15139 11601576 : {
15140 11601576 : tree base = TREE_OPERAND (t, 0);
15141 :
15142 11601576 : if (!DECL_P (base))
15143 5647223 : base = get_base_address (base);
15144 :
15145 11601576 : if (base && TREE_CODE (base) == TARGET_EXPR)
15146 795 : base = TARGET_EXPR_SLOT (base);
15147 :
15148 795 : if (!base)
15149 0 : return false;
15150 :
15151 : /* For objects in symbol table check if we know they are non-zero.
15152 : Don't do anything for variables and functions before symtab is built;
15153 : it is quite possible that they will be declared weak later. */
15154 11601576 : int nonzero_addr = maybe_nonzero_address (base);
15155 11601576 : if (nonzero_addr >= 0)
15156 9399002 : return nonzero_addr;
15157 :
15158 : /* Constants are never weak. */
15159 2202574 : if (CONSTANT_CLASS_P (base))
15160 : return true;
15161 :
15162 : return false;
15163 : }
15164 :
15165 37760 : case COND_EXPR:
15166 37760 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15167 37760 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15168 : return true;
15169 : break;
15170 :
15171 127585761 : case SSA_NAME:
15172 127585761 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15173 : break;
15174 99350174 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15175 :
15176 : default:
15177 : break;
15178 : }
15179 : return false;
15180 : }
15181 :
15182 : #define integer_valued_real_p(X) \
15183 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15184 :
15185 : #define RECURSE(X) \
15186 : ((integer_valued_real_p) (X, depth + 1))
15187 :
15188 : /* Return true if the floating point result of (CODE OP0) has an
15189 : integer value. We also allow +Inf, -Inf and NaN to be considered
15190 : integer values. Return false for signaling NaN.
15191 :
15192 : DEPTH is the current nesting depth of the query. */
15193 :
15194 : bool
15195 15030 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15196 : {
15197 15030 : switch (code)
15198 : {
15199 : case FLOAT_EXPR:
15200 : return true;
15201 :
15202 1403 : case ABS_EXPR:
15203 1403 : return RECURSE (op0);
15204 :
15205 9847 : CASE_CONVERT:
15206 9847 : {
15207 9847 : tree type = TREE_TYPE (op0);
15208 9847 : if (TREE_CODE (type) == INTEGER_TYPE)
15209 : return true;
15210 9847 : if (SCALAR_FLOAT_TYPE_P (type))
15211 9847 : return RECURSE (op0);
15212 : break;
15213 : }
15214 :
15215 : default:
15216 : break;
15217 : }
15218 : return false;
15219 : }
15220 :
15221 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15222 : integer value. We also allow +Inf, -Inf and NaN to be considered
15223 : integer values. Return false for signaling NaN.
15224 :
15225 : DEPTH is the current nesting depth of the query. */
15226 :
15227 : bool
15228 13375 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15229 : {
15230 13375 : switch (code)
15231 : {
15232 7640 : case PLUS_EXPR:
15233 7640 : case MINUS_EXPR:
15234 7640 : case MULT_EXPR:
15235 7640 : case MIN_EXPR:
15236 7640 : case MAX_EXPR:
15237 7640 : return RECURSE (op0) && RECURSE (op1);
15238 :
15239 : default:
15240 : break;
15241 : }
15242 : return false;
15243 : }
15244 :
15245 : /* Return true if the floating point result of calling FNDECL with arguments
15246 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15247 : considered integer values. Return false for signaling NaN. If FNDECL
15248 : takes fewer than 2 arguments, the remaining ARGn are null.
15249 :
15250 : DEPTH is the current nesting depth of the query. */
15251 :
15252 : bool
15253 1089 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15254 : {
15255 1089 : switch (fn)
15256 : {
15257 : CASE_CFN_CEIL:
15258 : CASE_CFN_CEIL_FN:
15259 : CASE_CFN_FLOOR:
15260 : CASE_CFN_FLOOR_FN:
15261 : CASE_CFN_NEARBYINT:
15262 : CASE_CFN_NEARBYINT_FN:
15263 : CASE_CFN_RINT:
15264 : CASE_CFN_RINT_FN:
15265 : CASE_CFN_ROUND:
15266 : CASE_CFN_ROUND_FN:
15267 : CASE_CFN_ROUNDEVEN:
15268 : CASE_CFN_ROUNDEVEN_FN:
15269 : CASE_CFN_TRUNC:
15270 : CASE_CFN_TRUNC_FN:
15271 : return true;
15272 :
15273 336 : CASE_CFN_FMIN:
15274 336 : CASE_CFN_FMIN_FN:
15275 336 : CASE_CFN_FMAX:
15276 336 : CASE_CFN_FMAX_FN:
15277 336 : return RECURSE (arg0) && RECURSE (arg1);
15278 :
15279 : default:
15280 : break;
15281 : }
15282 : return false;
15283 : }
15284 :
15285 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15286 : has an integer value. We also allow +Inf, -Inf and NaN to be
15287 : considered integer values. Return false for signaling NaN.
15288 :
15289 : DEPTH is the current nesting depth of the query. */
15290 :
15291 : bool
15292 127827 : integer_valued_real_single_p (tree t, int depth)
15293 : {
15294 127827 : switch (TREE_CODE (t))
15295 : {
15296 2271 : case REAL_CST:
15297 2271 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15298 :
15299 0 : case COND_EXPR:
15300 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15301 :
15302 89784 : case SSA_NAME:
15303 : /* Limit the depth of recursion to avoid quadratic behavior.
15304 : This is expected to catch almost all occurrences in practice.
15305 : If this code misses important cases that unbounded recursion
15306 : would not, passes that need this information could be revised
15307 : to provide it through dataflow propagation. */
15308 89784 : return (!name_registered_for_update_p (t)
15309 89784 : && depth < param_max_ssa_name_query_depth
15310 178772 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15311 : depth));
15312 :
15313 : default:
15314 : break;
15315 : }
15316 : return false;
15317 : }
15318 :
15319 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15320 : has an integer value. We also allow +Inf, -Inf and NaN to be
15321 : considered integer values. Return false for signaling NaN.
15322 :
15323 : DEPTH is the current nesting depth of the query. */
15324 :
15325 : static bool
15326 0 : integer_valued_real_invalid_p (tree t, int depth)
15327 : {
15328 0 : switch (TREE_CODE (t))
15329 : {
15330 0 : case COMPOUND_EXPR:
15331 0 : case MODIFY_EXPR:
15332 0 : case BIND_EXPR:
15333 0 : return RECURSE (TREE_OPERAND (t, 1));
15334 :
15335 0 : case SAVE_EXPR:
15336 0 : return RECURSE (TREE_OPERAND (t, 0));
15337 :
15338 : default:
15339 : break;
15340 : }
15341 : return false;
15342 : }
15343 :
15344 : #undef RECURSE
15345 : #undef integer_valued_real_p
15346 :
15347 : /* Return true if the floating point expression T has an integer value.
15348 : We also allow +Inf, -Inf and NaN to be considered integer values.
15349 : Return false for signaling NaN.
15350 :
15351 : DEPTH is the current nesting depth of the query. */
15352 :
15353 : bool
15354 96735 : integer_valued_real_p (tree t, int depth)
15355 : {
15356 96735 : if (t == error_mark_node)
15357 : return false;
15358 :
15359 96735 : STRIP_ANY_LOCATION_WRAPPER (t);
15360 :
15361 96735 : tree_code code = TREE_CODE (t);
15362 96735 : switch (TREE_CODE_CLASS (code))
15363 : {
15364 0 : case tcc_binary:
15365 0 : case tcc_comparison:
15366 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15367 0 : TREE_OPERAND (t, 1), depth);
15368 :
15369 0 : case tcc_unary:
15370 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15371 :
15372 8452 : case tcc_constant:
15373 8452 : case tcc_declaration:
15374 8452 : case tcc_reference:
15375 8452 : return integer_valued_real_single_p (t, depth);
15376 :
15377 88283 : default:
15378 88283 : break;
15379 : }
15380 :
15381 88283 : switch (code)
15382 : {
15383 88283 : case COND_EXPR:
15384 88283 : case SSA_NAME:
15385 88283 : return integer_valued_real_single_p (t, depth);
15386 :
15387 0 : case CALL_EXPR:
15388 0 : {
15389 0 : tree arg0 = (call_expr_nargs (t) > 0
15390 0 : ? CALL_EXPR_ARG (t, 0)
15391 0 : : NULL_TREE);
15392 0 : tree arg1 = (call_expr_nargs (t) > 1
15393 0 : ? CALL_EXPR_ARG (t, 1)
15394 0 : : NULL_TREE);
15395 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15396 0 : arg0, arg1, depth);
15397 : }
15398 :
15399 0 : default:
15400 0 : return integer_valued_real_invalid_p (t, depth);
15401 : }
15402 : }
15403 :
15404 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15405 : attempt to fold the expression to a constant without modifying TYPE,
15406 : OP0 or OP1.
15407 :
15408 : If the expression could be simplified to a constant, then return
15409 : the constant. If the expression would not be simplified to a
15410 : constant, then return NULL_TREE. */
15411 :
15412 : tree
15413 15787600 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15414 : {
15415 15787600 : tree tem = fold_binary (code, type, op0, op1);
15416 15787600 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15417 : }
15418 :
15419 : /* Given the components of a unary expression CODE, TYPE and OP0,
15420 : attempt to fold the expression to a constant without modifying
15421 : TYPE or OP0.
15422 :
15423 : If the expression could be simplified to a constant, then return
15424 : the constant. If the expression would not be simplified to a
15425 : constant, then return NULL_TREE. */
15426 :
15427 : tree
15428 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15429 : {
15430 0 : tree tem = fold_unary (code, type, op0);
15431 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15432 : }
15433 :
15434 : /* If EXP represents referencing an element in a constant string
15435 : (either via pointer arithmetic or array indexing), return the
15436 : tree representing the value accessed, otherwise return NULL. */
15437 :
15438 : tree
15439 209533139 : fold_read_from_constant_string (tree exp)
15440 : {
15441 209533139 : if ((INDIRECT_REF_P (exp)
15442 209533120 : || TREE_CODE (exp) == ARRAY_REF)
15443 222901081 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15444 : {
15445 10145608 : tree exp1 = TREE_OPERAND (exp, 0);
15446 10145608 : tree index;
15447 10145608 : tree string;
15448 10145608 : location_t loc = EXPR_LOCATION (exp);
15449 :
15450 10145608 : if (INDIRECT_REF_P (exp))
15451 0 : string = string_constant (exp1, &index, NULL, NULL);
15452 : else
15453 : {
15454 10145608 : tree low_bound = array_ref_low_bound (exp);
15455 10145608 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15456 :
15457 : /* Optimize the special-case of a zero lower bound.
15458 :
15459 : We convert the low_bound to sizetype to avoid some problems
15460 : with constant folding. (E.g. suppose the lower bound is 1,
15461 : and its mode is QI. Without the conversion,l (ARRAY
15462 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15463 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15464 10145608 : if (! integer_zerop (low_bound))
15465 155634 : index = size_diffop_loc (loc, index,
15466 : fold_convert_loc (loc, sizetype, low_bound));
15467 :
15468 : string = exp1;
15469 : }
15470 :
15471 10145608 : scalar_int_mode char_mode;
15472 10145608 : if (string
15473 10145608 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15474 10145608 : && TREE_CODE (string) == STRING_CST
15475 260403 : && tree_fits_uhwi_p (index)
15476 256421 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15477 10401827 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15478 : &char_mode)
15479 20291216 : && GET_MODE_SIZE (char_mode) == 1)
15480 509986 : return build_int_cst_type (TREE_TYPE (exp),
15481 254993 : (TREE_STRING_POINTER (string)
15482 254993 : [TREE_INT_CST_LOW (index)]));
15483 : }
15484 : return NULL;
15485 : }
15486 :
15487 : /* Folds a read from vector element at IDX of vector ARG. */
15488 :
15489 : tree
15490 6701 : fold_read_from_vector (tree arg, poly_uint64 idx)
15491 : {
15492 6701 : unsigned HOST_WIDE_INT i;
15493 6701 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15494 6701 : && known_ge (idx, 0u)
15495 6701 : && idx.is_constant (&i))
15496 : {
15497 6701 : if (TREE_CODE (arg) == VECTOR_CST)
15498 1978 : return VECTOR_CST_ELT (arg, i);
15499 4723 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15500 : {
15501 2575 : if (CONSTRUCTOR_NELTS (arg)
15502 2535 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15503 : return NULL_TREE;
15504 1737 : if (i >= CONSTRUCTOR_NELTS (arg))
15505 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15506 1697 : return CONSTRUCTOR_ELT (arg, i)->value;
15507 : }
15508 : }
15509 : return NULL_TREE;
15510 : }
15511 :
15512 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15513 : an integer constant, real, or fixed-point constant.
15514 :
15515 : TYPE is the type of the result. */
15516 :
15517 : static tree
15518 32477855 : fold_negate_const (tree arg0, tree type)
15519 : {
15520 32477855 : tree t = NULL_TREE;
15521 :
15522 32477855 : switch (TREE_CODE (arg0))
15523 : {
15524 2049244 : case REAL_CST:
15525 2049244 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15526 2049244 : break;
15527 :
15528 0 : case FIXED_CST:
15529 0 : {
15530 0 : FIXED_VALUE_TYPE f;
15531 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15532 0 : &(TREE_FIXED_CST (arg0)), NULL,
15533 0 : TYPE_SATURATING (type));
15534 0 : t = build_fixed (type, f);
15535 : /* Propagate overflow flags. */
15536 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15537 0 : TREE_OVERFLOW (t) = 1;
15538 0 : break;
15539 : }
15540 :
15541 30428611 : default:
15542 30428611 : if (poly_int_tree_p (arg0))
15543 : {
15544 30428611 : wi::overflow_type overflow;
15545 30428611 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15546 30428611 : t = force_fit_type (type, res, 1,
15547 215386 : (overflow && ! TYPE_UNSIGNED (type))
15548 30633858 : || TREE_OVERFLOW (arg0));
15549 30428611 : break;
15550 30428611 : }
15551 :
15552 0 : gcc_unreachable ();
15553 : }
15554 :
15555 32477855 : return t;
15556 : }
15557 :
15558 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15559 : an integer constant or real constant.
15560 :
15561 : TYPE is the type of the result. */
15562 :
15563 : tree
15564 36756 : fold_abs_const (tree arg0, tree type)
15565 : {
15566 36756 : tree t = NULL_TREE;
15567 :
15568 36756 : switch (TREE_CODE (arg0))
15569 : {
15570 7276 : case INTEGER_CST:
15571 7276 : {
15572 : /* If the value is unsigned or non-negative, then the absolute value
15573 : is the same as the ordinary value. */
15574 7276 : wide_int val = wi::to_wide (arg0);
15575 7276 : wi::overflow_type overflow = wi::OVF_NONE;
15576 7276 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15577 : ;
15578 :
15579 : /* If the value is negative, then the absolute value is
15580 : its negation. */
15581 : else
15582 3141 : val = wi::neg (val, &overflow);
15583 :
15584 : /* Force to the destination type, set TREE_OVERFLOW for signed
15585 : TYPE only. */
15586 7276 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15587 7276 : }
15588 7276 : break;
15589 :
15590 29480 : case REAL_CST:
15591 29480 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15592 7597 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15593 : else
15594 : t = arg0;
15595 : break;
15596 :
15597 0 : default:
15598 0 : gcc_unreachable ();
15599 : }
15600 :
15601 36756 : return t;
15602 : }
15603 :
15604 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15605 : constant. TYPE is the type of the result. */
15606 :
15607 : static tree
15608 2292202 : fold_not_const (const_tree arg0, tree type)
15609 : {
15610 2292202 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15611 :
15612 2292202 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15613 : }
15614 :
15615 : /* Given CODE, a relational operator, the target type, TYPE and two
15616 : constant operands OP0 and OP1, return the result of the
15617 : relational operation. If the result is not a compile time
15618 : constant, then return NULL_TREE. */
15619 :
15620 : static tree
15621 82037759 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15622 : {
15623 82037759 : int result, invert;
15624 :
15625 : /* From here on, the only cases we handle are when the result is
15626 : known to be a constant. */
15627 :
15628 82037759 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15629 : {
15630 1222170 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15631 1222170 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15632 :
15633 : /* Handle the cases where either operand is a NaN. */
15634 1222170 : if (real_isnan (c0) || real_isnan (c1))
15635 : {
15636 13686 : switch (code)
15637 : {
15638 : case EQ_EXPR:
15639 : case ORDERED_EXPR:
15640 : result = 0;
15641 : break;
15642 :
15643 : case NE_EXPR:
15644 : case UNORDERED_EXPR:
15645 : case UNLT_EXPR:
15646 : case UNLE_EXPR:
15647 : case UNGT_EXPR:
15648 : case UNGE_EXPR:
15649 : case UNEQ_EXPR:
15650 6762 : result = 1;
15651 : break;
15652 :
15653 6967 : case LT_EXPR:
15654 6967 : case LE_EXPR:
15655 6967 : case GT_EXPR:
15656 6967 : case GE_EXPR:
15657 6967 : case LTGT_EXPR:
15658 6967 : if (flag_trapping_math)
15659 : return NULL_TREE;
15660 : result = 0;
15661 : break;
15662 :
15663 0 : default:
15664 0 : gcc_unreachable ();
15665 : }
15666 :
15667 6762 : return constant_boolean_node (result, type);
15668 : }
15669 :
15670 1208484 : return constant_boolean_node (real_compare (code, c0, c1), type);
15671 : }
15672 :
15673 80815589 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15674 : {
15675 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15676 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15677 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15678 : }
15679 :
15680 : /* Handle equality/inequality of complex constants. */
15681 80815589 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15682 : {
15683 58558 : tree rcond = fold_relational_const (code, type,
15684 29279 : TREE_REALPART (op0),
15685 29279 : TREE_REALPART (op1));
15686 117116 : tree icond = fold_relational_const (code, type,
15687 29279 : TREE_IMAGPART (op0),
15688 29279 : TREE_IMAGPART (op1));
15689 29279 : if (code == EQ_EXPR)
15690 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15691 28977 : else if (code == NE_EXPR)
15692 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15693 : else
15694 : return NULL_TREE;
15695 : }
15696 :
15697 80786310 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15698 : {
15699 20120 : if (!VECTOR_TYPE_P (type))
15700 : {
15701 : /* Have vector comparison with scalar boolean result. */
15702 154 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15703 : && known_eq (VECTOR_CST_NELTS (op0),
15704 : VECTOR_CST_NELTS (op1)));
15705 154 : unsigned HOST_WIDE_INT nunits;
15706 154 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15707 : return NULL_TREE;
15708 437 : for (unsigned i = 0; i < nunits; i++)
15709 : {
15710 384 : tree elem0 = VECTOR_CST_ELT (op0, i);
15711 384 : tree elem1 = VECTOR_CST_ELT (op1, i);
15712 384 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15713 384 : if (tmp == NULL_TREE)
15714 : return NULL_TREE;
15715 384 : if (integer_zerop (tmp))
15716 101 : return constant_boolean_node (code == NE_EXPR, type);
15717 : }
15718 53 : return constant_boolean_node (code == EQ_EXPR, type);
15719 : }
15720 19966 : tree_vector_builder elts;
15721 19966 : if (!elts.new_binary_operation (type, op0, op1, false))
15722 : return NULL_TREE;
15723 19966 : unsigned int count = elts.encoded_nelts ();
15724 73056 : for (unsigned i = 0; i < count; i++)
15725 : {
15726 53090 : tree elem_type = TREE_TYPE (type);
15727 53090 : tree elem0 = VECTOR_CST_ELT (op0, i);
15728 53090 : tree elem1 = VECTOR_CST_ELT (op1, i);
15729 :
15730 53090 : tree tem = fold_relational_const (code, elem_type,
15731 : elem0, elem1);
15732 :
15733 53090 : if (tem == NULL_TREE)
15734 : return NULL_TREE;
15735 :
15736 53090 : elts.quick_push (build_int_cst (elem_type,
15737 84902 : integer_zerop (tem) ? 0 : -1));
15738 : }
15739 :
15740 19966 : return elts.build ();
15741 19966 : }
15742 :
15743 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15744 :
15745 : To compute GT, swap the arguments and do LT.
15746 : To compute GE, do LT and invert the result.
15747 : To compute LE, swap the arguments, do LT and invert the result.
15748 : To compute NE, do EQ and invert the result.
15749 :
15750 : Therefore, the code below must handle only EQ and LT. */
15751 :
15752 80766190 : if (code == LE_EXPR || code == GT_EXPR)
15753 : {
15754 14112691 : std::swap (op0, op1);
15755 14112691 : code = swap_tree_comparison (code);
15756 : }
15757 :
15758 : /* Note that it is safe to invert for real values here because we
15759 : have already handled the one case that it matters. */
15760 :
15761 80766190 : invert = 0;
15762 80766190 : if (code == NE_EXPR || code == GE_EXPR)
15763 : {
15764 36468991 : invert = 1;
15765 36468991 : code = invert_tree_comparison (code, false);
15766 : }
15767 :
15768 : /* Compute a result for LT or EQ if args permit;
15769 : Otherwise return T. */
15770 80766190 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15771 : {
15772 80740661 : if (code == EQ_EXPR)
15773 39738297 : result = tree_int_cst_equal (op0, op1);
15774 : else
15775 41002364 : result = tree_int_cst_lt (op0, op1);
15776 : }
15777 : else
15778 : return NULL_TREE;
15779 :
15780 80740661 : if (invert)
15781 36467151 : result ^= 1;
15782 80740661 : return constant_boolean_node (result, type);
15783 : }
15784 :
15785 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15786 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15787 : itself. */
15788 :
15789 : tree
15790 135264485 : fold_build_cleanup_point_expr (tree type, tree expr)
15791 : {
15792 : /* If the expression does not have side effects then we don't have to wrap
15793 : it with a cleanup point expression. */
15794 135264485 : if (!TREE_SIDE_EFFECTS (expr))
15795 : return expr;
15796 :
15797 : /* If the expression is a return, check to see if the expression inside the
15798 : return has no side effects or the right hand side of the modify expression
15799 : inside the return. If either don't have side effects set we don't need to
15800 : wrap the expression in a cleanup point expression. Note we don't check the
15801 : left hand side of the modify because it should always be a return decl. */
15802 115726064 : if (TREE_CODE (expr) == RETURN_EXPR)
15803 : {
15804 45695459 : tree op = TREE_OPERAND (expr, 0);
15805 45695459 : if (!op || !TREE_SIDE_EFFECTS (op))
15806 : return expr;
15807 44991508 : op = TREE_OPERAND (op, 1);
15808 44991508 : if (!TREE_SIDE_EFFECTS (op))
15809 : return expr;
15810 : }
15811 :
15812 91425727 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15813 : }
15814 :
15815 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15816 : of an indirection through OP0, or NULL_TREE if no simplification is
15817 : possible. */
15818 :
15819 : tree
15820 22276079 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15821 : {
15822 22276079 : tree sub = op0;
15823 22276079 : tree subtype;
15824 22276079 : poly_uint64 const_op01;
15825 :
15826 22276079 : STRIP_NOPS (sub);
15827 22276079 : subtype = TREE_TYPE (sub);
15828 22276079 : if (!POINTER_TYPE_P (subtype)
15829 22276079 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15830 : return NULL_TREE;
15831 :
15832 22123450 : if (TREE_CODE (sub) == ADDR_EXPR)
15833 : {
15834 4971703 : tree op = TREE_OPERAND (sub, 0);
15835 4971703 : tree optype = TREE_TYPE (op);
15836 :
15837 : /* *&CONST_DECL -> to the value of the const decl. */
15838 4971703 : if (TREE_CODE (op) == CONST_DECL)
15839 3214 : return DECL_INITIAL (op);
15840 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15841 4968489 : if (type == optype)
15842 : {
15843 3756431 : tree fop = fold_read_from_constant_string (op);
15844 3756431 : if (fop)
15845 : return fop;
15846 : else
15847 3710969 : return op;
15848 : }
15849 : /* *(foo *)&fooarray => fooarray[0] */
15850 1212058 : else if (TREE_CODE (optype) == ARRAY_TYPE
15851 13849 : && type == TREE_TYPE (optype)
15852 1224764 : && (!in_gimple_form
15853 3132 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15854 : {
15855 12706 : tree type_domain = TYPE_DOMAIN (optype);
15856 12706 : tree min_val = size_zero_node;
15857 12706 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15858 12667 : min_val = TYPE_MIN_VALUE (type_domain);
15859 12706 : if (in_gimple_form
15860 3132 : && TREE_CODE (min_val) != INTEGER_CST)
15861 : return NULL_TREE;
15862 12706 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15863 12706 : NULL_TREE, NULL_TREE);
15864 : }
15865 : /* *(foo *)&complexfoo => __real__ complexfoo */
15866 1199352 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15867 1199352 : && type == TREE_TYPE (optype))
15868 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15869 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15870 1199352 : else if (VECTOR_TYPE_P (optype)
15871 1199352 : && type == TREE_TYPE (optype))
15872 : {
15873 70 : tree part_width = TYPE_SIZE (type);
15874 70 : tree index = bitsize_int (0);
15875 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15876 70 : index);
15877 : }
15878 : }
15879 :
15880 18351029 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15881 18351029 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15882 : {
15883 259588 : tree op00 = TREE_OPERAND (sub, 0);
15884 259588 : tree op01 = TREE_OPERAND (sub, 1);
15885 :
15886 259588 : STRIP_NOPS (op00);
15887 259588 : if (TREE_CODE (op00) == ADDR_EXPR)
15888 : {
15889 2028 : tree op00type;
15890 2028 : op00 = TREE_OPERAND (op00, 0);
15891 2028 : op00type = TREE_TYPE (op00);
15892 :
15893 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15894 2028 : if (VECTOR_TYPE_P (op00type)
15895 240 : && type == TREE_TYPE (op00type)
15896 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15897 : but we want to treat offsets with MSB set as negative.
15898 : For the code below negative offsets are invalid and
15899 : TYPE_SIZE of the element is something unsigned, so
15900 : check whether op01 fits into poly_int64, which implies
15901 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15902 : then just use poly_uint64 because we want to treat the
15903 : value as unsigned. */
15904 2221 : && tree_fits_poly_int64_p (op01))
15905 : {
15906 179 : tree part_width = TYPE_SIZE (type);
15907 179 : poly_uint64 max_offset
15908 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15909 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15910 179 : if (known_lt (const_op01, max_offset))
15911 : {
15912 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15913 179 : return fold_build3_loc (loc,
15914 : BIT_FIELD_REF, type, op00,
15915 179 : part_width, index);
15916 : }
15917 : }
15918 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15919 1849 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
15920 1849 : && type == TREE_TYPE (op00type))
15921 : {
15922 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
15923 : const_op01))
15924 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15925 : }
15926 : /* ((foo *)&fooarray)[1] => fooarray[1] */
15927 1849 : else if (TREE_CODE (op00type) == ARRAY_TYPE
15928 1849 : && type == TREE_TYPE (op00type))
15929 : {
15930 719 : tree type_domain = TYPE_DOMAIN (op00type);
15931 719 : tree min_val = size_zero_node;
15932 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15933 718 : min_val = TYPE_MIN_VALUE (type_domain);
15934 719 : poly_uint64 type_size, index;
15935 719 : if (poly_int_tree_p (min_val)
15936 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
15937 719 : && multiple_p (const_op01, type_size, &index))
15938 : {
15939 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
15940 719 : op01 = wide_int_to_tree (sizetype, off);
15941 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
15942 : NULL_TREE, NULL_TREE);
15943 : }
15944 : }
15945 : }
15946 : }
15947 :
15948 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15949 18350131 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15950 680684 : && type == TREE_TYPE (TREE_TYPE (subtype))
15951 18353196 : && (!in_gimple_form
15952 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15953 : {
15954 3064 : tree type_domain;
15955 3064 : tree min_val = size_zero_node;
15956 3064 : sub = build_fold_indirect_ref_loc (loc, sub);
15957 3064 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15958 3064 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15959 3064 : min_val = TYPE_MIN_VALUE (type_domain);
15960 3064 : if (in_gimple_form
15961 11 : && TREE_CODE (min_val) != INTEGER_CST)
15962 : return NULL_TREE;
15963 3064 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15964 3064 : NULL_TREE);
15965 : }
15966 :
15967 : return NULL_TREE;
15968 : }
15969 :
15970 : /* Builds an expression for an indirection through T, simplifying some
15971 : cases. */
15972 :
15973 : tree
15974 11630563 : build_fold_indirect_ref_loc (location_t loc, tree t)
15975 : {
15976 11630563 : tree type = TREE_TYPE (TREE_TYPE (t));
15977 11630563 : tree sub = fold_indirect_ref_1 (loc, type, t);
15978 :
15979 11630563 : if (sub)
15980 : return sub;
15981 :
15982 7878093 : return build1_loc (loc, INDIRECT_REF, type, t);
15983 : }
15984 :
15985 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
15986 :
15987 : tree
15988 10269945 : fold_indirect_ref_loc (location_t loc, tree t)
15989 : {
15990 10269945 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15991 :
15992 10269945 : if (sub)
15993 : return sub;
15994 : else
15995 10248671 : return t;
15996 : }
15997 :
15998 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
15999 : whose result is ignored. The type of the returned tree need not be
16000 : the same as the original expression. */
16001 :
16002 : tree
16003 135129 : fold_ignored_result (tree t)
16004 : {
16005 135129 : if (!TREE_SIDE_EFFECTS (t))
16006 17633 : return integer_zero_node;
16007 :
16008 156686 : for (;;)
16009 156686 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16010 : {
16011 3832 : case tcc_unary:
16012 3832 : t = TREE_OPERAND (t, 0);
16013 3832 : break;
16014 :
16015 5116 : case tcc_binary:
16016 5116 : case tcc_comparison:
16017 5116 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16018 3201 : t = TREE_OPERAND (t, 0);
16019 1915 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16020 43 : t = TREE_OPERAND (t, 1);
16021 : else
16022 : return t;
16023 : break;
16024 :
16025 100273 : case tcc_expression:
16026 100273 : switch (TREE_CODE (t))
16027 : {
16028 32115 : case COMPOUND_EXPR:
16029 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16030 : return t;
16031 31820 : t = TREE_OPERAND (t, 0);
16032 31820 : break;
16033 :
16034 382 : case COND_EXPR:
16035 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16036 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16037 : return t;
16038 294 : t = TREE_OPERAND (t, 0);
16039 294 : break;
16040 :
16041 : default:
16042 : return t;
16043 : }
16044 : break;
16045 :
16046 : default:
16047 : return t;
16048 : }
16049 : }
16050 :
16051 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16052 :
16053 : tree
16054 3117224486 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16055 : {
16056 3117224486 : tree div = NULL_TREE;
16057 :
16058 3117224486 : if (divisor == 1)
16059 : return value;
16060 :
16061 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16062 : have to do anything. Only do this when we are not given a const,
16063 : because in that case, this check is more expensive than just
16064 : doing it. */
16065 1940865118 : if (TREE_CODE (value) != INTEGER_CST)
16066 : {
16067 369274 : div = build_int_cst (TREE_TYPE (value), divisor);
16068 :
16069 369274 : if (multiple_of_p (TREE_TYPE (value), value, div))
16070 : return value;
16071 : }
16072 :
16073 : /* If divisor is a power of two, simplify this to bit manipulation. */
16074 1940497744 : if (pow2_or_zerop (divisor))
16075 : {
16076 1940497744 : if (TREE_CODE (value) == INTEGER_CST)
16077 : {
16078 1940495844 : wide_int val = wi::to_wide (value);
16079 1940495844 : bool overflow_p;
16080 :
16081 1940495844 : if ((val & (divisor - 1)) == 0)
16082 : return value;
16083 :
16084 4052643 : overflow_p = TREE_OVERFLOW (value);
16085 4052643 : val += divisor - 1;
16086 4052643 : val &= (int) -divisor;
16087 4052643 : if (val == 0)
16088 4 : overflow_p = true;
16089 :
16090 4052643 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16091 1940495844 : }
16092 : else
16093 : {
16094 1900 : tree t;
16095 :
16096 1900 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16097 1900 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16098 1900 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16099 1900 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16100 : }
16101 : }
16102 : else
16103 : {
16104 0 : if (!div)
16105 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16106 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16107 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16108 : }
16109 :
16110 : return value;
16111 : }
16112 :
16113 : /* Likewise, but round down. */
16114 :
16115 : tree
16116 21465409 : round_down_loc (location_t loc, tree value, int divisor)
16117 : {
16118 21465409 : tree div = NULL_TREE;
16119 :
16120 21465409 : gcc_assert (divisor > 0);
16121 21465409 : if (divisor == 1)
16122 : return value;
16123 :
16124 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16125 : have to do anything. Only do this when we are not given a const,
16126 : because in that case, this check is more expensive than just
16127 : doing it. */
16128 21465409 : if (TREE_CODE (value) != INTEGER_CST)
16129 : {
16130 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16131 :
16132 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16133 : return value;
16134 : }
16135 :
16136 : /* If divisor is a power of two, simplify this to bit manipulation. */
16137 21465409 : if (pow2_or_zerop (divisor))
16138 : {
16139 21465409 : tree t;
16140 :
16141 21465409 : t = build_int_cst (TREE_TYPE (value), -divisor);
16142 21465409 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16143 : }
16144 : else
16145 : {
16146 0 : if (!div)
16147 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16148 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16149 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16150 : }
16151 :
16152 : return value;
16153 : }
16154 :
16155 : /* Returns the pointer to the base of the object addressed by EXP and
16156 : extracts the information about the offset of the access, storing it
16157 : to PBITPOS and POFFSET. */
16158 :
16159 : static tree
16160 2404436 : split_address_to_core_and_offset (tree exp,
16161 : poly_int64 *pbitpos, tree *poffset)
16162 : {
16163 2404436 : tree core;
16164 2404436 : machine_mode mode;
16165 2404436 : int unsignedp, reversep, volatilep;
16166 2404436 : poly_int64 bitsize;
16167 2404436 : location_t loc = EXPR_LOCATION (exp);
16168 :
16169 2404436 : if (TREE_CODE (exp) == SSA_NAME)
16170 450473 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16171 334489 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16172 31179 : exp = gimple_assign_rhs1 (def);
16173 :
16174 2404436 : if (TREE_CODE (exp) == ADDR_EXPR)
16175 : {
16176 1335426 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16177 : poffset, &mode, &unsignedp, &reversep,
16178 : &volatilep);
16179 : /* If we are left with MEM[a + CST] strip that and add it to the
16180 : pbitpos and return a. */
16181 1335426 : if (TREE_CODE (core) == MEM_REF)
16182 : {
16183 27928 : poly_offset_int tem;
16184 27928 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16185 27928 : tem <<= LOG2_BITS_PER_UNIT;
16186 27928 : tem += *pbitpos;
16187 27928 : if (tem.to_shwi (pbitpos))
16188 27750 : return TREE_OPERAND (core, 0);
16189 : }
16190 1307676 : core = build_fold_addr_expr_loc (loc, core);
16191 : }
16192 1069010 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16193 : {
16194 428092 : core = TREE_OPERAND (exp, 0);
16195 428092 : STRIP_NOPS (core);
16196 428092 : *pbitpos = 0;
16197 428092 : *poffset = TREE_OPERAND (exp, 1);
16198 428092 : if (poly_int_tree_p (*poffset))
16199 : {
16200 428007 : poly_offset_int tem
16201 428007 : = wi::sext (wi::to_poly_offset (*poffset),
16202 428007 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16203 428007 : tem <<= LOG2_BITS_PER_UNIT;
16204 428007 : if (tem.to_shwi (pbitpos))
16205 428007 : *poffset = NULL_TREE;
16206 : }
16207 : }
16208 : else
16209 : {
16210 640918 : core = exp;
16211 640918 : *pbitpos = 0;
16212 640918 : *poffset = NULL_TREE;
16213 : }
16214 :
16215 : return core;
16216 : }
16217 :
16218 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16219 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16220 :
16221 : bool
16222 1202218 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16223 : {
16224 1202218 : tree core1, core2;
16225 1202218 : poly_int64 bitpos1, bitpos2;
16226 1202218 : tree toffset1, toffset2, tdiff, type;
16227 :
16228 1202218 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16229 1202218 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16230 :
16231 1202218 : poly_int64 bytepos1, bytepos2;
16232 1202218 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16233 1841623 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16234 2404436 : || !operand_equal_p (core1, core2, 0))
16235 639405 : return false;
16236 :
16237 562813 : if (toffset1 && toffset2)
16238 : {
16239 29 : type = TREE_TYPE (toffset1);
16240 29 : if (type != TREE_TYPE (toffset2))
16241 0 : toffset2 = fold_convert (type, toffset2);
16242 :
16243 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16244 29 : if (!cst_and_fits_in_hwi (tdiff))
16245 : return false;
16246 :
16247 15 : *diff = int_cst_value (tdiff);
16248 : }
16249 562784 : else if (toffset1 || toffset2)
16250 : {
16251 : /* If only one of the offsets is non-constant, the difference cannot
16252 : be a constant. */
16253 : return false;
16254 : }
16255 : else
16256 544414 : *diff = 0;
16257 :
16258 544429 : *diff += bytepos1 - bytepos2;
16259 544429 : return true;
16260 : }
16261 :
16262 : /* Return OFF converted to a pointer offset type suitable as offset for
16263 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16264 : tree
16265 51109179 : convert_to_ptrofftype_loc (location_t loc, tree off)
16266 : {
16267 51109179 : if (ptrofftype_p (TREE_TYPE (off)))
16268 : return off;
16269 5968257 : return fold_convert_loc (loc, sizetype, off);
16270 : }
16271 :
16272 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16273 : tree
16274 45218459 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16275 : {
16276 45218459 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16277 45218459 : ptr, convert_to_ptrofftype_loc (loc, off));
16278 : }
16279 :
16280 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16281 : tree
16282 165080 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16283 : {
16284 165080 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16285 165080 : ptr, size_int (off));
16286 : }
16287 :
16288 : /* Return a pointer to a NUL-terminated string containing the sequence
16289 : of bytes corresponding to the representation of the object referred to
16290 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16291 : to an initialized constant array plus some constant offset).
16292 : Set *STRSIZE the number of bytes in the constant sequence including
16293 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16294 : where A is the array that stores the constant sequence that SRC points
16295 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16296 : need not point to a string or even an array of characters but may point
16297 : to an object of any type. */
16298 :
16299 : const char *
16300 12469956 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16301 : {
16302 : /* The offset into the array A storing the string, and A's byte size. */
16303 12469956 : tree offset_node;
16304 12469956 : tree mem_size;
16305 :
16306 12469956 : if (strsize)
16307 4660431 : *strsize = 0;
16308 :
16309 12469956 : if (strsize)
16310 4660431 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16311 : else
16312 7809525 : src = string_constant (src, &offset_node, &mem_size, NULL);
16313 12469956 : if (!src)
16314 : return NULL;
16315 :
16316 2825804 : unsigned HOST_WIDE_INT offset = 0;
16317 2825804 : if (offset_node != NULL_TREE)
16318 : {
16319 2825804 : if (!tree_fits_uhwi_p (offset_node))
16320 : return NULL;
16321 : else
16322 2824040 : offset = tree_to_uhwi (offset_node);
16323 : }
16324 :
16325 2824040 : if (!tree_fits_uhwi_p (mem_size))
16326 : return NULL;
16327 :
16328 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16329 : is stored in and equal to sizeof A. INIT_BYTES is the number
16330 : of bytes in the constant sequence used to initialize the array,
16331 : including any embedded NULs as well as the terminating NUL (for
16332 : strings), but not including any trailing zeros/NULs past
16333 : the terminating one appended implicitly to a string literal to
16334 : zero out the remainder of the array it's stored in. For example,
16335 : given:
16336 : const char a[7] = "abc\0d";
16337 : n = strlen (a + 1);
16338 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16339 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16340 : is equal to strlen (A) + 1. */
16341 2824040 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16342 2824040 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16343 2824040 : const char *string = TREE_STRING_POINTER (src);
16344 :
16345 : /* Ideally this would turn into a gcc_checking_assert over time. */
16346 2824040 : if (init_bytes > array_size)
16347 : init_bytes = array_size;
16348 :
16349 2824040 : if (init_bytes == 0 || offset >= array_size)
16350 : return NULL;
16351 :
16352 2822775 : if (strsize)
16353 : {
16354 : /* Compute and store the number of characters from the beginning
16355 : of the substring at OFFSET to the end, including the terminating
16356 : nul. Offsets past the initial length refer to null strings. */
16357 1439496 : if (offset < init_bytes)
16358 1439496 : *strsize = init_bytes - offset;
16359 : else
16360 0 : *strsize = 1;
16361 : }
16362 : else
16363 : {
16364 1383279 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16365 : /* Support only properly NUL-terminated single byte strings. */
16366 1383279 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16367 : return NULL;
16368 1378506 : if (string[init_bytes - 1] != '\0')
16369 : return NULL;
16370 : }
16371 :
16372 2794693 : return offset < init_bytes ? string + offset : "";
16373 : }
16374 :
16375 : /* Return a pointer to a NUL-terminated string corresponding to
16376 : the expression STR referencing a constant string, possibly
16377 : involving a constant offset. Return null if STR either doesn't
16378 : reference a constant string or if it involves a nonconstant
16379 : offset. */
16380 :
16381 : const char *
16382 7809525 : c_getstr (tree str)
16383 : {
16384 7809525 : return getbyterep (str, NULL);
16385 : }
16386 :
16387 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16388 : may be nonzero, with precision PREC, the precision of T's type. */
16389 :
16390 : static wide_int
16391 253023098 : tree_nonzero_bits (const_tree t, unsigned prec)
16392 : {
16393 253023098 : switch (TREE_CODE (t))
16394 : {
16395 8820829 : case INTEGER_CST:
16396 8820829 : return wi::to_wide (t);
16397 141167582 : case SSA_NAME:
16398 141167582 : return get_nonzero_bits (t);
16399 259046 : case NON_LVALUE_EXPR:
16400 259046 : case SAVE_EXPR:
16401 259046 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16402 481766 : case BIT_AND_EXPR:
16403 963532 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16404 1445298 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16405 5476 : case BIT_IOR_EXPR:
16406 5476 : case BIT_XOR_EXPR:
16407 10952 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16408 16428 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16409 66497 : case COND_EXPR:
16410 132994 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16411 199491 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16412 53000883 : CASE_CONVERT:
16413 53000883 : if (TREE_TYPE (t) != error_mark_node
16414 53000883 : && !error_operand_p (TREE_OPERAND (t, 0)))
16415 : {
16416 53000882 : tree op0 = TREE_OPERAND (t, 0);
16417 53000882 : tree inner_type = TREE_TYPE (op0);
16418 53000882 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16419 106001764 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16420 106001764 : prec, TYPE_SIGN (inner_type));
16421 : }
16422 : break;
16423 14309699 : case PLUS_EXPR:
16424 14309699 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16425 : {
16426 14309699 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16427 14309699 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16428 14309699 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16429 559582 : return wi::bit_or (nzbits1, nzbits2);
16430 14309699 : }
16431 : break;
16432 170367 : case LSHIFT_EXPR:
16433 170367 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16434 170367 : && TREE_TYPE (t) != error_mark_node)
16435 : {
16436 98469 : tree type = TREE_TYPE (t);
16437 98469 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16438 98469 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16439 98469 : return wi::neg_p (arg1)
16440 196938 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16441 98469 : : wi::lshift (nzbits, arg1);
16442 98469 : }
16443 : break;
16444 159219 : case RSHIFT_EXPR:
16445 159219 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16446 159219 : && TREE_TYPE (t) != error_mark_node)
16447 : {
16448 157322 : tree type = TREE_TYPE (t);
16449 157322 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16450 157322 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16451 157322 : return wi::neg_p (arg1)
16452 314644 : ? wi::lshift (nzbits, -arg1)
16453 157322 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16454 157322 : }
16455 : break;
16456 : default:
16457 : break;
16458 : }
16459 :
16460 48405647 : return wi::shwi (-1, prec);
16461 : }
16462 :
16463 : /* Given a tree T, compute which bits in T may be nonzero. */
16464 :
16465 : wide_int
16466 169780503 : tree_nonzero_bits (const_tree t)
16467 : {
16468 169780503 : if (error_operand_p (t))
16469 0 : return wi::shwi (-1, 64);
16470 169780503 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16471 : }
16472 :
16473 : /* Helper function for address compare simplifications in match.pd.
16474 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16475 : TYPE is the type of comparison operands.
16476 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16477 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16478 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16479 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16480 : and 2 if unknown. */
16481 :
16482 : int
16483 5173702 : address_compare (tree_code code, tree type, tree op0, tree op1,
16484 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16485 : bool generic)
16486 : {
16487 5173702 : if (TREE_CODE (op0) == SSA_NAME)
16488 33042 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16489 5173702 : if (TREE_CODE (op1) == SSA_NAME)
16490 4543 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16491 5173702 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16492 5173702 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16493 5173702 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16494 5173702 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16495 5173702 : if (base0 && TREE_CODE (base0) == MEM_REF)
16496 : {
16497 31833 : off0 += mem_ref_offset (base0).force_shwi ();
16498 31833 : base0 = TREE_OPERAND (base0, 0);
16499 : }
16500 5173702 : if (base1 && TREE_CODE (base1) == MEM_REF)
16501 : {
16502 3698 : off1 += mem_ref_offset (base1).force_shwi ();
16503 3698 : base1 = TREE_OPERAND (base1, 0);
16504 : }
16505 5173702 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16506 : return 2;
16507 :
16508 5161488 : int equal = 2;
16509 : /* Punt in GENERIC on variables with value expressions;
16510 : the value expressions might point to fields/elements
16511 : of other vars etc. */
16512 5161488 : if (generic
16513 5161488 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16514 5029093 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16515 : return 2;
16516 5160831 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16517 : {
16518 1255209 : symtab_node *node0 = symtab_node::get_create (base0);
16519 1255209 : symtab_node *node1 = symtab_node::get_create (base1);
16520 1255209 : equal = node0->equal_address_to (node1);
16521 : }
16522 3905622 : else if ((DECL_P (base0)
16523 239025 : || TREE_CODE (base0) == SSA_NAME
16524 208311 : || TREE_CODE (base0) == STRING_CST)
16525 3905424 : && (DECL_P (base1)
16526 211957 : || TREE_CODE (base1) == SSA_NAME
16527 208485 : || TREE_CODE (base1) == STRING_CST))
16528 3905409 : equal = (base0 == base1);
16529 : /* Assume different STRING_CSTs with the same content will be
16530 : merged. */
16531 5160618 : if (equal == 0
16532 78293 : && TREE_CODE (base0) == STRING_CST
16533 17626 : && TREE_CODE (base1) == STRING_CST
16534 17451 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16535 5160618 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16536 6189 : TREE_STRING_LENGTH (base0)) == 0)
16537 : equal = 1;
16538 5156398 : if (equal == 1)
16539 : {
16540 5061929 : if (code == EQ_EXPR
16541 5061929 : || code == NE_EXPR
16542 : /* If the offsets are equal we can ignore overflow. */
16543 128610 : || known_eq (off0, off1)
16544 256996 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16545 : /* Or if we compare using pointers to decls or strings. */
16546 5190427 : || (POINTER_TYPE_P (type)
16547 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16548 : return 1;
16549 : return 2;
16550 : }
16551 98902 : if (equal != 0)
16552 : return equal;
16553 73860 : if (code != EQ_EXPR && code != NE_EXPR)
16554 : return 2;
16555 :
16556 : /* At this point we know (or assume) the two pointers point at
16557 : different objects. */
16558 68595 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16559 68595 : off0.is_constant (&ioff0);
16560 68595 : off1.is_constant (&ioff1);
16561 : /* Punt on non-zero offsets from functions. */
16562 68595 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16563 68595 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16564 : return 2;
16565 : /* Or if the bases are neither decls nor string literals. */
16566 68595 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16567 : return 2;
16568 38617 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16569 : return 2;
16570 : /* For initializers, assume addresses of different functions are
16571 : different. */
16572 38617 : if (folding_initializer
16573 13528 : && TREE_CODE (base0) == FUNCTION_DECL
16574 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16575 : return 0;
16576 :
16577 : /* Compute whether one address points to the start of one
16578 : object and another one to the end of another one. */
16579 38597 : poly_int64 size0 = 0, size1 = 0;
16580 38597 : if (TREE_CODE (base0) == STRING_CST)
16581 : {
16582 13039 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16583 : equal = 2;
16584 : else
16585 : size0 = TREE_STRING_LENGTH (base0);
16586 : }
16587 25558 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16588 : size0 = 1;
16589 : else
16590 : {
16591 25250 : tree sz0 = DECL_SIZE_UNIT (base0);
16592 25250 : if (!tree_fits_poly_int64_p (sz0))
16593 : equal = 2;
16594 : else
16595 25250 : size0 = tree_to_poly_int64 (sz0);
16596 : }
16597 38597 : if (TREE_CODE (base1) == STRING_CST)
16598 : {
16599 13144 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16600 : equal = 2;
16601 : else
16602 : size1 = TREE_STRING_LENGTH (base1);
16603 : }
16604 25453 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16605 : size1 = 1;
16606 : else
16607 : {
16608 25149 : tree sz1 = DECL_SIZE_UNIT (base1);
16609 25149 : if (!tree_fits_poly_int64_p (sz1))
16610 : equal = 2;
16611 : else
16612 25149 : size1 = tree_to_poly_int64 (sz1);
16613 : }
16614 38597 : if (equal == 0)
16615 : {
16616 : /* If one offset is pointing (or could be) to the beginning of one
16617 : object and the other is pointing to one past the last byte of the
16618 : other object, punt. */
16619 38585 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16620 : equal = 2;
16621 38448 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16622 : equal = 2;
16623 : /* If both offsets are the same, there are some cases we know that are
16624 : ok. Either if we know they aren't zero, or if we know both sizes
16625 : are no zero. */
16626 : if (equal == 2
16627 273 : && known_eq (off0, off1)
16628 22 : && (known_ne (off0, 0)
16629 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16630 : equal = 0;
16631 : }
16632 :
16633 : /* At this point, equal is 2 if either one or both pointers are out of
16634 : bounds of their object, or one points to start of its object and the
16635 : other points to end of its object. This is unspecified behavior
16636 : e.g. in C++. Otherwise equal is 0. */
16637 38597 : if (folding_cxx_constexpr && equal)
16638 : return equal;
16639 :
16640 : /* When both pointers point to string literals, even when equal is 0,
16641 : due to tail merging of string literals the pointers might be the same. */
16642 38534 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16643 : {
16644 13000 : if (ioff0 < 0
16645 13000 : || ioff1 < 0
16646 13000 : || ioff0 > TREE_STRING_LENGTH (base0)
16647 25988 : || ioff1 > TREE_STRING_LENGTH (base1))
16648 : return 2;
16649 :
16650 : /* If the bytes in the string literals starting at the pointers
16651 : differ, the pointers need to be different. */
16652 12988 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16653 12988 : TREE_STRING_POINTER (base1) + ioff1,
16654 12988 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16655 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16656 : {
16657 3910 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16658 3910 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16659 3910 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16660 : ioffmin) == 0)
16661 : /* If even the bytes in the string literal before the
16662 : pointers are the same, the string literals could be
16663 : tail merged. */
16664 : return 2;
16665 : }
16666 : return 0;
16667 : }
16668 :
16669 25534 : if (folding_cxx_constexpr)
16670 : return 0;
16671 :
16672 : /* If this is a pointer comparison, ignore for now even
16673 : valid equalities where one pointer is the offset zero
16674 : of one object and the other to one past end of another one. */
16675 12129 : if (!INTEGRAL_TYPE_P (type))
16676 : return 0;
16677 :
16678 : /* Assume that string literals can't be adjacent to variables
16679 : (automatic or global). */
16680 312 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16681 : return 0;
16682 :
16683 : /* Assume that automatic variables can't be adjacent to global
16684 : variables. */
16685 292 : if (is_global_var (base0) != is_global_var (base1))
16686 : return 0;
16687 :
16688 : return equal;
16689 : }
16690 :
16691 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16692 : tree
16693 54 : ctor_single_nonzero_element (const_tree t)
16694 : {
16695 54 : unsigned HOST_WIDE_INT idx;
16696 54 : constructor_elt *ce;
16697 54 : tree elt = NULL_TREE;
16698 :
16699 54 : if (TREE_CODE (t) != CONSTRUCTOR)
16700 : return NULL_TREE;
16701 117 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16702 114 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16703 : {
16704 105 : if (elt)
16705 : return NULL_TREE;
16706 54 : elt = ce->value;
16707 : }
16708 : return elt;
16709 : }
16710 :
16711 : #if CHECKING_P
16712 :
16713 : namespace selftest {
16714 :
16715 : /* Helper functions for writing tests of folding trees. */
16716 :
16717 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16718 :
16719 : static void
16720 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16721 : tree constant)
16722 : {
16723 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16724 16 : }
16725 :
16726 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16727 : wrapping WRAPPED_EXPR. */
16728 :
16729 : static void
16730 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16731 : tree wrapped_expr)
16732 : {
16733 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16734 12 : ASSERT_NE (wrapped_expr, result);
16735 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16736 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16737 12 : }
16738 :
16739 : /* Verify that various arithmetic binary operations are folded
16740 : correctly. */
16741 :
16742 : static void
16743 4 : test_arithmetic_folding ()
16744 : {
16745 4 : tree type = integer_type_node;
16746 4 : tree x = create_tmp_var_raw (type, "x");
16747 4 : tree zero = build_zero_cst (type);
16748 4 : tree one = build_int_cst (type, 1);
16749 :
16750 : /* Addition. */
16751 : /* 1 <-- (0 + 1) */
16752 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16753 : one);
16754 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16755 : one);
16756 :
16757 : /* (nonlvalue)x <-- (x + 0) */
16758 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16759 : x);
16760 :
16761 : /* Subtraction. */
16762 : /* 0 <-- (x - x) */
16763 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16764 : zero);
16765 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16766 : x);
16767 :
16768 : /* Multiplication. */
16769 : /* 0 <-- (x * 0) */
16770 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16771 : zero);
16772 :
16773 : /* (nonlvalue)x <-- (x * 1) */
16774 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16775 : x);
16776 4 : }
16777 :
16778 : namespace test_operand_equality {
16779 :
16780 : /* Verify structural equality. */
16781 :
16782 : /* Execute fold_vec_perm_cst unit tests. */
16783 :
16784 : static void
16785 4 : test ()
16786 : {
16787 4 : tree stype = integer_type_node;
16788 4 : tree utype = unsigned_type_node;
16789 4 : tree x = create_tmp_var_raw (stype, "x");
16790 4 : tree y = create_tmp_var_raw (stype, "y");
16791 4 : tree z = create_tmp_var_raw (stype, "z");
16792 4 : tree four = build_int_cst (stype, 4);
16793 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16794 4 : tree rhs1 = fold_convert (stype,
16795 : fold_build2 (PLUS_EXPR, utype,
16796 : fold_convert (utype, x),
16797 : fold_convert (utype, y)));
16798 :
16799 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16800 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16801 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16802 :
16803 : /* (int)(unsigned) x == x. */
16804 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16805 : build1 (NOP_EXPR, utype, x));
16806 4 : tree rhs2 = x;
16807 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16808 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16809 :
16810 : /* (unsigned x) + (unsigned y) == x + y. */
16811 4 : tree lhs3 = lhs1;
16812 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16813 : fold_convert (utype, x),
16814 : fold_convert (utype, y));
16815 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16816 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16817 :
16818 : /* (unsigned x) / (unsigned y) == x / y. */
16819 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16820 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16821 : fold_convert (utype, x),
16822 : fold_convert (utype, y));
16823 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16824 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16825 :
16826 : /* (long x) / 4 == (long)(x / 4). */
16827 4 : tree lstype = long_long_integer_type_node;
16828 4 : tree lfour = build_int_cst (lstype, 4);
16829 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16830 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16831 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16832 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16833 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16834 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16835 :
16836 : /* (unsigned x) / 4 == x / 4. */
16837 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16838 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16839 : fold_convert (utype, x),
16840 : fold_convert (utype, four));
16841 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16842 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16843 :
16844 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16845 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16846 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16847 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16848 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16849 :
16850 : /* (unsigned x) + 4 == x + 4. */
16851 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16852 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16853 : fold_convert (utype, x),
16854 : fold_convert (utype, four));
16855 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16856 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16857 :
16858 : /* (unsigned x) + 4 == 4 + x. */
16859 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16860 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16861 : fold_convert (utype, x),
16862 : fold_convert (utype, four));
16863 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16864 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16865 :
16866 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16867 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16868 : fold_build2 (MULT_EXPR, stype,
16869 : fold_build2 (PLUS_EXPR, stype, four, x),
16870 : y),
16871 : z);
16872 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16873 : fold_build2 (PLUS_EXPR, utype,
16874 : fold_convert (utype, x),
16875 : fold_convert (utype, four)),
16876 : fold_convert (utype, y));
16877 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16878 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16879 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16880 4 : }
16881 : }
16882 :
16883 : namespace test_fold_vec_perm_cst {
16884 :
16885 : /* Build a VECTOR_CST corresponding to VMODE, and has
16886 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16887 : Fill it with randomized elements, using rand() % THRESHOLD. */
16888 :
16889 : static tree
16890 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16891 : unsigned nelts_per_pattern,
16892 : int step = 0, bool natural_stepped = false,
16893 : int threshold = 100)
16894 : {
16895 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16896 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16897 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16898 :
16899 : // Fill a0 for each pattern
16900 0 : for (unsigned i = 0; i < npatterns; i++)
16901 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16902 :
16903 0 : if (nelts_per_pattern == 1)
16904 0 : return builder.build ();
16905 :
16906 : // Fill a1 for each pattern
16907 0 : for (unsigned i = 0; i < npatterns; i++)
16908 : {
16909 0 : tree a1;
16910 0 : if (natural_stepped)
16911 : {
16912 0 : tree a0 = builder[i];
16913 0 : wide_int a0_val = wi::to_wide (a0);
16914 0 : wide_int a1_val = a0_val + step;
16915 0 : a1 = wide_int_to_tree (inner_type, a1_val);
16916 0 : }
16917 : else
16918 0 : a1 = build_int_cst (inner_type, rand () % threshold);
16919 0 : builder.quick_push (a1);
16920 : }
16921 0 : if (nelts_per_pattern == 2)
16922 0 : return builder.build ();
16923 :
16924 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
16925 : {
16926 0 : tree prev_elem = builder[i - npatterns];
16927 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
16928 0 : wide_int val = prev_elem_val + step;
16929 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
16930 0 : }
16931 :
16932 0 : return builder.build ();
16933 0 : }
16934 :
16935 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16936 : when result is VLA. */
16937 :
16938 : static void
16939 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
16940 : tree res, tree *expected_res)
16941 : {
16942 : /* Actual npatterns and encoded_elts in res may be less than expected due
16943 : to canonicalization. */
16944 0 : ASSERT_TRUE (res != NULL_TREE);
16945 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
16946 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
16947 :
16948 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
16949 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16950 0 : }
16951 :
16952 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16953 : when the result is VLS. */
16954 :
16955 : static void
16956 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
16957 : {
16958 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
16959 0 : for (unsigned i = 0; i < expected_nelts; i++)
16960 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16961 0 : }
16962 :
16963 : /* Helper routine to push multiple elements into BUILDER. */
16964 : template<unsigned N>
16965 0 : static void builder_push_elems (vec_perm_builder& builder,
16966 : poly_uint64 (&elems)[N])
16967 : {
16968 0 : for (unsigned i = 0; i < N; i++)
16969 0 : builder.quick_push (elems[i]);
16970 0 : }
16971 :
16972 : #define ARG0(index) vector_cst_elt (arg0, index)
16973 : #define ARG1(index) vector_cst_elt (arg1, index)
16974 :
16975 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
16976 :
16977 : static void
16978 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
16979 : {
16980 0 : for (int i = 0; i < 10; i++)
16981 : {
16982 : /* Case 1:
16983 : sel = { 0, 4, 1, 5, ... }
16984 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
16985 0 : {
16986 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16987 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
16988 :
16989 0 : tree inner_type
16990 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
16991 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
16992 :
16993 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
16994 0 : vec_perm_builder builder (res_len, 4, 1);
16995 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
16996 0 : builder_push_elems (builder, mask_elems);
16997 :
16998 0 : vec_perm_indices sel (builder, 2, res_len);
16999 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17000 :
17001 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17002 0 : validate_res (4, 1, res, expected_res);
17003 0 : }
17004 :
17005 : /* Case 2: Same as case 1, but contains an out of bounds access which
17006 : should wrap around.
17007 : sel = {0, 8, 4, 12, ...} (4, 1)
17008 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17009 0 : {
17010 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17011 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17012 :
17013 0 : tree inner_type
17014 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17015 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17016 :
17017 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17018 0 : vec_perm_builder builder (res_len, 4, 1);
17019 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17020 0 : builder_push_elems (builder, mask_elems);
17021 :
17022 0 : vec_perm_indices sel (builder, 2, res_len);
17023 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17024 :
17025 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17026 0 : validate_res (4, 1, res, expected_res);
17027 0 : }
17028 : }
17029 0 : }
17030 :
17031 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17032 :
17033 : static void
17034 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17035 : {
17036 0 : for (int i = 0; i < 10; i++)
17037 : {
17038 : /* Case 1:
17039 : sel = { 0, 1, 2, 3}
17040 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17041 0 : {
17042 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17043 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17044 :
17045 0 : tree inner_type
17046 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17047 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17048 :
17049 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17050 0 : vec_perm_builder builder (res_len, 4, 1);
17051 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17052 0 : builder_push_elems (builder, mask_elems);
17053 :
17054 0 : vec_perm_indices sel (builder, 2, res_len);
17055 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17056 :
17057 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17058 0 : validate_res_vls (res, expected_res, 4);
17059 0 : }
17060 :
17061 : /* Case 2: Same as Case 1, but crossing input vector.
17062 : sel = {0, 2, 4, 6}
17063 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17064 : Since we cannot determine, which vector to choose from during
17065 : compile time, should return NULL_TREE. */
17066 0 : {
17067 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17068 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17069 :
17070 0 : tree inner_type
17071 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17072 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17073 :
17074 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17075 0 : vec_perm_builder builder (res_len, 4, 1);
17076 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17077 0 : builder_push_elems (builder, mask_elems);
17078 :
17079 0 : vec_perm_indices sel (builder, 2, res_len);
17080 0 : const char *reason;
17081 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17082 :
17083 0 : ASSERT_TRUE (res == NULL_TREE);
17084 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17085 0 : }
17086 : }
17087 0 : }
17088 :
17089 : /* Test all input vectors. */
17090 :
17091 : static void
17092 0 : test_all_nunits (machine_mode vmode)
17093 : {
17094 : /* Test with 10 different inputs. */
17095 0 : for (int i = 0; i < 10; i++)
17096 : {
17097 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17098 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17099 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17100 :
17101 : /* Case 1: mask = {0, ...} // (1, 1)
17102 : res = { arg0[0], ... } // (1, 1) */
17103 0 : {
17104 0 : vec_perm_builder builder (len, 1, 1);
17105 0 : builder.quick_push (0);
17106 0 : vec_perm_indices sel (builder, 2, len);
17107 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17108 0 : tree expected_res[] = { ARG0(0) };
17109 0 : validate_res (1, 1, res, expected_res);
17110 0 : }
17111 :
17112 : /* Case 2: mask = {len, ...} // (1, 1)
17113 : res = { arg1[0], ... } // (1, 1) */
17114 0 : {
17115 0 : vec_perm_builder builder (len, 1, 1);
17116 0 : builder.quick_push (len);
17117 0 : vec_perm_indices sel (builder, 2, len);
17118 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17119 :
17120 0 : tree expected_res[] = { ARG1(0) };
17121 0 : validate_res (1, 1, res, expected_res);
17122 0 : }
17123 : }
17124 0 : }
17125 :
17126 : /* Test all vectors which contain at-least 2 elements. */
17127 :
17128 : static void
17129 0 : test_nunits_min_2 (machine_mode vmode)
17130 : {
17131 0 : for (int i = 0; i < 10; i++)
17132 : {
17133 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17134 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17135 0 : {
17136 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17137 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17138 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17139 :
17140 0 : vec_perm_builder builder (len, 2, 1);
17141 0 : poly_uint64 mask_elems[] = { 0, len };
17142 0 : builder_push_elems (builder, mask_elems);
17143 :
17144 0 : vec_perm_indices sel (builder, 2, len);
17145 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17146 :
17147 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17148 0 : validate_res (2, 1, res, expected_res);
17149 0 : }
17150 :
17151 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17152 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17153 0 : {
17154 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17155 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17156 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17157 :
17158 0 : vec_perm_builder builder (len, 2, 2);
17159 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17160 0 : builder_push_elems (builder, mask_elems);
17161 :
17162 0 : vec_perm_indices sel (builder, 2, len);
17163 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17164 :
17165 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17166 0 : validate_res (2, 2, res, expected_res);
17167 0 : }
17168 :
17169 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17170 : Test that the stepped sequence of the pattern selects from
17171 : same input pattern. Since input vectors have npatterns = 2,
17172 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17173 : in input vector. So return NULL_TREE. */
17174 0 : {
17175 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17176 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17177 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17178 :
17179 0 : vec_perm_builder builder (len, 1, 3);
17180 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17181 0 : builder_push_elems (builder, mask_elems);
17182 :
17183 0 : vec_perm_indices sel (builder, 2, len);
17184 0 : const char *reason;
17185 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17186 : &reason);
17187 0 : ASSERT_TRUE (res == NULL_TREE);
17188 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17189 0 : }
17190 :
17191 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17192 : Test that stepped sequence of the pattern selects from arg0.
17193 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17194 0 : {
17195 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17196 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17197 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17198 :
17199 0 : vec_perm_builder builder (len, 1, 3);
17200 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17201 0 : builder_push_elems (builder, mask_elems);
17202 :
17203 0 : vec_perm_indices sel (builder, 2, len);
17204 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17205 :
17206 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17207 0 : validate_res (1, 3, res, expected_res);
17208 0 : }
17209 :
17210 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17211 : In this case ensure that arg has a natural stepped sequence
17212 : to preserve arg's encoding.
17213 :
17214 : As a concrete example, consider:
17215 : arg0: { -16, -9, -10, ... } // (1, 3)
17216 : arg1: { -12, -5, -6, ... } // (1, 3)
17217 : sel = { 0, len, len + 1, ... } // (1, 3)
17218 :
17219 : This will create res with following encoding:
17220 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17221 : = { -16, -12, -5, ... }
17222 :
17223 : The step in above encoding would be: (-5) - (-12) = 7
17224 : And hence res[3] would be computed as -5 + 7 = 2.
17225 : instead of arg1[2], ie, -6.
17226 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17227 : for this case. */
17228 0 : {
17229 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17230 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17231 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17232 :
17233 0 : vec_perm_builder builder (len, 1, 3);
17234 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17235 0 : builder_push_elems (builder, mask_elems);
17236 :
17237 0 : vec_perm_indices sel (builder, 2, len);
17238 0 : const char *reason;
17239 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17240 : stepped pattern, even if we didn't explicitly tell it to. So folding
17241 : may not always fail, but if it does, ensure that's because arg1 does
17242 : not have a natural stepped sequence (and not due to other reason) */
17243 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17244 0 : if (res == NULL_TREE)
17245 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17246 0 : }
17247 :
17248 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17249 : sequence and thus folding should be valid for this case. */
17250 0 : {
17251 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17252 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17253 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17254 :
17255 0 : vec_perm_builder builder (len, 1, 3);
17256 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17257 0 : builder_push_elems (builder, mask_elems);
17258 :
17259 0 : vec_perm_indices sel (builder, 2, len);
17260 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17261 :
17262 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17263 0 : validate_res (1, 3, res, expected_res);
17264 0 : }
17265 :
17266 : /* Case 8: Same as aarch64/sve/slp_3.c:
17267 : arg0, arg1 are dup vectors.
17268 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17269 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17270 :
17271 : In this case, since the input vectors are dup, only the first two
17272 : elements per pattern in sel are considered significant. */
17273 0 : {
17274 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17275 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17276 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17277 :
17278 0 : vec_perm_builder builder (len, 2, 3);
17279 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17280 0 : builder_push_elems (builder, mask_elems);
17281 :
17282 0 : vec_perm_indices sel (builder, 2, len);
17283 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17284 :
17285 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17286 0 : validate_res (2, 1, res, expected_res);
17287 0 : }
17288 : }
17289 0 : }
17290 :
17291 : /* Test all vectors which contain at-least 4 elements. */
17292 :
17293 : static void
17294 0 : test_nunits_min_4 (machine_mode vmode)
17295 : {
17296 0 : for (int i = 0; i < 10; i++)
17297 : {
17298 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17299 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17300 0 : {
17301 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17302 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17303 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17304 :
17305 0 : vec_perm_builder builder (len, 4, 1);
17306 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17307 0 : builder_push_elems (builder, mask_elems);
17308 :
17309 0 : vec_perm_indices sel (builder, 2, len);
17310 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17311 :
17312 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17313 0 : validate_res (4, 1, res, expected_res);
17314 0 : }
17315 :
17316 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17317 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17318 0 : {
17319 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17320 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17321 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17322 :
17323 0 : vec_perm_builder builder (arg0_len, 1, 3);
17324 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17325 0 : builder_push_elems (builder, mask_elems);
17326 :
17327 0 : vec_perm_indices sel (builder, 2, arg0_len);
17328 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17329 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17330 0 : validate_res (1, 3, res, expected_res);
17331 0 : }
17332 :
17333 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17334 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17335 0 : {
17336 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17337 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17338 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17339 :
17340 0 : vec_perm_builder builder (len, 1, 3);
17341 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17342 0 : builder_push_elems (builder, mask_elems);
17343 :
17344 0 : vec_perm_indices sel (builder, 2, len);
17345 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17346 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17347 0 : validate_res (1, 3, res, expected_res);
17348 0 : }
17349 :
17350 : /* Case 4:
17351 : sel = { len, 0, 2, ... } // (1, 3)
17352 : This should return NULL because we cross the input vectors.
17353 : Because,
17354 : Let's assume len = C + Cx
17355 : a1 = 0
17356 : S = 2
17357 : esel = arg0_len / sel_npatterns = C + Cx
17358 : ae = 0 + (esel - 2) * S
17359 : = 0 + (C + Cx - 2) * 2
17360 : = 2(C-2) + 2Cx
17361 :
17362 : For C >= 4:
17363 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17364 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17365 : Since q1 != qe, we cross input vectors.
17366 : So return NULL_TREE. */
17367 0 : {
17368 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17369 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17370 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17371 :
17372 0 : vec_perm_builder builder (arg0_len, 1, 3);
17373 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17374 0 : builder_push_elems (builder, mask_elems);
17375 :
17376 0 : vec_perm_indices sel (builder, 2, arg0_len);
17377 0 : const char *reason;
17378 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17379 0 : ASSERT_TRUE (res == NULL_TREE);
17380 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17381 0 : }
17382 :
17383 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17384 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17385 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17386 :
17387 : Note that fold_vec_perm_cst will set
17388 : res_npatterns = max(4, max(4, 2)) = 4
17389 : However after canonicalizing, we will end up with shape (2, 2). */
17390 0 : {
17391 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17392 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17393 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17394 :
17395 0 : vec_perm_builder builder (len, 2, 2);
17396 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17397 0 : builder_push_elems (builder, mask_elems);
17398 :
17399 0 : vec_perm_indices sel (builder, 2, len);
17400 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17401 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17402 0 : validate_res (2, 2, res, expected_res);
17403 0 : }
17404 :
17405 : /* Case 6: Test combination in sel, where one pattern is dup and other
17406 : is stepped sequence.
17407 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17408 : res = { arg0[0], arg0[0], arg0[0],
17409 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17410 0 : {
17411 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17412 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17413 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17414 :
17415 0 : vec_perm_builder builder (len, 2, 3);
17416 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17417 0 : builder_push_elems (builder, mask_elems);
17418 :
17419 0 : vec_perm_indices sel (builder, 2, len);
17420 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17421 :
17422 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17423 0 : ARG0(1), ARG0(0), ARG0(2) };
17424 0 : validate_res (2, 3, res, expected_res);
17425 0 : }
17426 :
17427 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17428 : when arg0, arg1 and sel have different number of patterns.
17429 : arg0 is of shape (1, 1)
17430 : arg1 is of shape (4, 1)
17431 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17432 :
17433 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17434 : However,
17435 : step = (len+2) - (len+1) = 1
17436 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17437 : Since step is not a multiple of arg_npatterns,
17438 : valid_mask_for_fold_vec_perm_cst should return false,
17439 : and thus fold_vec_perm_cst should return NULL_TREE. */
17440 0 : {
17441 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17442 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17443 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17444 :
17445 0 : vec_perm_builder builder (len, 2, 3);
17446 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17447 0 : builder_push_elems (builder, mask_elems);
17448 :
17449 0 : vec_perm_indices sel (builder, 2, len);
17450 0 : const char *reason;
17451 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17452 :
17453 0 : ASSERT_TRUE (res == NULL_TREE);
17454 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17455 0 : }
17456 :
17457 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17458 : check that the result is not a stepped sequence either, even
17459 : if sel has a stepped sequence. */
17460 0 : {
17461 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17462 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17463 :
17464 0 : vec_perm_builder builder (len, 1, 3);
17465 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17466 0 : builder_push_elems (builder, mask_elems);
17467 :
17468 0 : vec_perm_indices sel (builder, 1, len);
17469 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17470 :
17471 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17472 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17473 0 : }
17474 :
17475 : /* Case 9: If sel doesn't contain a stepped sequence,
17476 : check that the result has same encoding as sel, irrespective
17477 : of shape of input vectors. */
17478 0 : {
17479 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17480 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17481 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17482 :
17483 0 : vec_perm_builder builder (len, 1, 2);
17484 0 : poly_uint64 mask_elems[] = { 0, len };
17485 0 : builder_push_elems (builder, mask_elems);
17486 :
17487 0 : vec_perm_indices sel (builder, 2, len);
17488 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17489 :
17490 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17491 0 : validate_res (sel.encoding ().npatterns (),
17492 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17493 0 : }
17494 : }
17495 0 : }
17496 :
17497 : /* Test all vectors which contain at-least 8 elements. */
17498 :
17499 : static void
17500 0 : test_nunits_min_8 (machine_mode vmode)
17501 : {
17502 0 : for (int i = 0; i < 10; i++)
17503 : {
17504 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17505 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17506 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17507 : arg0[2], arg0[0], arg0[3], arg1[0],
17508 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17509 0 : {
17510 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17511 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17512 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17513 :
17514 0 : vec_perm_builder builder(len, 4, 3);
17515 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17516 0 : 4, 0, 5, len };
17517 0 : builder_push_elems (builder, mask_elems);
17518 :
17519 0 : vec_perm_indices sel (builder, 2, len);
17520 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17521 :
17522 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17523 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17524 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17525 0 : validate_res (4, 3, res, expected_res);
17526 0 : }
17527 : }
17528 0 : }
17529 :
17530 : /* Test vectors for which nunits[0] <= 4. */
17531 :
17532 : static void
17533 0 : test_nunits_max_4 (machine_mode vmode)
17534 : {
17535 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17536 : This should return NULL_TREE because the index 4 may choose
17537 : from either arg0 or arg1 depending on vector length. */
17538 0 : {
17539 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17540 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17541 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17542 :
17543 0 : vec_perm_builder builder (len, 1, 2);
17544 0 : poly_uint64 mask_elems[] = {0, 4};
17545 0 : builder_push_elems (builder, mask_elems);
17546 :
17547 0 : vec_perm_indices sel (builder, 2, len);
17548 0 : const char *reason;
17549 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17550 0 : ASSERT_TRUE (res == NULL_TREE);
17551 0 : ASSERT_TRUE (reason != NULL);
17552 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17553 0 : }
17554 0 : }
17555 :
17556 : #undef ARG0
17557 : #undef ARG1
17558 :
17559 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17560 :
17561 : static bool
17562 0 : is_simple_vla_size (poly_uint64 size)
17563 : {
17564 124 : if (size.is_constant ()
17565 : || !pow2p_hwi (size.coeffs[0]))
17566 0 : return false;
17567 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17568 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17569 : return false;
17570 : return true;
17571 : }
17572 :
17573 : /* Execute fold_vec_perm_cst unit tests. */
17574 :
17575 : static void
17576 4 : test ()
17577 : {
17578 4 : machine_mode vnx4si_mode = E_VOIDmode;
17579 4 : machine_mode v4si_mode = E_VOIDmode;
17580 :
17581 4 : machine_mode vmode;
17582 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17583 : {
17584 : /* Obtain modes corresponding to VNx4SI and V4SI,
17585 : to call mixed mode tests below.
17586 : FIXME: Is there a better way to do this ? */
17587 124 : if (GET_MODE_INNER (vmode) == SImode)
17588 : {
17589 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17590 124 : if (is_simple_vla_size (nunits)
17591 : && nunits.coeffs[0] == 4)
17592 : vnx4si_mode = vmode;
17593 124 : else if (known_eq (nunits, poly_uint64 (4)))
17594 124 : v4si_mode = vmode;
17595 : }
17596 :
17597 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17598 : || !targetm.vector_mode_supported_p (vmode))
17599 124 : continue;
17600 :
17601 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17602 : test_all_nunits (vmode);
17603 : if (nunits.coeffs[0] >= 2)
17604 : test_nunits_min_2 (vmode);
17605 : if (nunits.coeffs[0] >= 4)
17606 : test_nunits_min_4 (vmode);
17607 : if (nunits.coeffs[0] >= 8)
17608 : test_nunits_min_8 (vmode);
17609 :
17610 : if (nunits.coeffs[0] <= 4)
17611 : test_nunits_max_4 (vmode);
17612 : }
17613 :
17614 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17615 : && targetm.vector_mode_supported_p (vnx4si_mode)
17616 : && targetm.vector_mode_supported_p (v4si_mode))
17617 : {
17618 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17619 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17620 : }
17621 4 : }
17622 : } // end of test_fold_vec_perm_cst namespace
17623 :
17624 : /* Verify that various binary operations on vectors are folded
17625 : correctly. */
17626 :
17627 : static void
17628 4 : test_vector_folding ()
17629 : {
17630 4 : tree inner_type = integer_type_node;
17631 4 : tree type = build_vector_type (inner_type, 4);
17632 4 : tree zero = build_zero_cst (type);
17633 4 : tree one = build_one_cst (type);
17634 4 : tree index = build_index_vector (type, 0, 1);
17635 :
17636 : /* Verify equality tests that return a scalar boolean result. */
17637 4 : tree res_type = boolean_type_node;
17638 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17639 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17640 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17641 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17642 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17643 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17644 : index, one)));
17645 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17646 : index, index)));
17647 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17648 : index, index)));
17649 4 : }
17650 :
17651 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17652 :
17653 : static void
17654 4 : test_vec_duplicate_folding ()
17655 : {
17656 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17657 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17658 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17659 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17660 :
17661 4 : tree type = build_vector_type (ssizetype, nunits);
17662 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17663 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17664 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17665 4 : }
17666 :
17667 : /* Run all of the selftests within this file. */
17668 :
17669 : void
17670 4 : fold_const_cc_tests ()
17671 : {
17672 4 : test_arithmetic_folding ();
17673 4 : test_vector_folding ();
17674 4 : test_vec_duplicate_folding ();
17675 4 : test_fold_vec_perm_cst::test ();
17676 4 : test_operand_equality::test ();
17677 4 : }
17678 :
17679 : } // namespace selftest
17680 :
17681 : #endif /* CHECKING_P */
|