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 142396 : minmax_from_comparison (tree_code cmp, tree exp0,
158 : const widest_int cst1,
159 : const widest_int cst2)
160 : {
161 142396 : if (cst1 == cst2)
162 : {
163 125 : if (cmp == LE_EXPR || cmp == LT_EXPR)
164 : return MIN_EXPR;
165 106 : if (cmp == GT_EXPR || cmp == GE_EXPR)
166 : return MAX_EXPR;
167 : }
168 142377 : if (cst1 == cst2 - 1)
169 : {
170 : /* X <= Y - 1 equals to X < Y. */
171 85685 : if (cmp == LE_EXPR)
172 : return MIN_EXPR;
173 : /* X > Y - 1 equals to X >= Y. */
174 85245 : 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 72602 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : {
179 19142 : int_range_max r;
180 38284 : get_range_query (cfun)->range_of_expr (r, exp0);
181 19142 : if (r.undefined_p ())
182 0 : r.set_varying (TREE_TYPE (exp0));
183 :
184 19142 : widest_int min = widest_int::from (r.lower_bound (),
185 38284 : TYPE_SIGN (TREE_TYPE (exp0)));
186 19142 : if (min == cst1)
187 758 : return MAX_EXPR;
188 19142 : }
189 : }
190 128536 : if (cst1 == cst2 + 1)
191 : {
192 : /* X < Y + 1 equals to X <= Y. */
193 1260 : if (cmp == LT_EXPR)
194 : return MIN_EXPR;
195 : /* X >= Y + 1 equals to X > Y. */
196 1232 : 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 1070 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : {
201 662 : int_range_max r;
202 1324 : get_range_query (cfun)->range_of_expr (r, exp0);
203 662 : if (r.undefined_p ())
204 0 : r.set_varying (TREE_TYPE (exp0));
205 :
206 662 : widest_int max = widest_int::from (r.upper_bound (),
207 1324 : TYPE_SIGN (TREE_TYPE (exp0)));
208 662 : if (max == cst1)
209 188 : return MIN_EXPR;
210 662 : }
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 176140 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
220 : {
221 176140 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
222 : return ERROR_MARK;
223 :
224 176129 : if (!operand_equal_p (exp0, exp2))
225 : return ERROR_MARK;
226 :
227 176129 : if (operand_equal_p (exp1, exp3))
228 : {
229 33658 : if (cmp == LT_EXPR || cmp == LE_EXPR)
230 : return MIN_EXPR;
231 31512 : if (cmp == GT_EXPR || cmp == GE_EXPR)
232 : return MAX_EXPR;
233 : }
234 142577 : if (TREE_CODE (exp3) == INTEGER_CST
235 142107 : && TREE_CODE (exp1) == INTEGER_CST)
236 141651 : 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 3062743 : expr_location_or (tree t, location_t loc)
245 : {
246 958554 : location_t tloc = EXPR_LOCATION (t);
247 3046411 : 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 9400958 : protected_set_expr_location_unshare (tree x, location_t loc)
255 : {
256 9400958 : if (CAN_HAVE_LOCATION_P (x)
257 8360578 : && EXPR_LOCATION (x) != loc
258 2673116 : && !(TREE_CODE (x) == SAVE_EXPR
259 1336773 : || TREE_CODE (x) == TARGET_EXPR
260 : || TREE_CODE (x) == BIND_EXPR))
261 : {
262 1336007 : x = copy_node (x);
263 1336007 : SET_EXPR_LOCATION (x, loc);
264 : }
265 9400958 : 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 2188969 : negate_mathfn_p (combined_fn fn)
273 : {
274 2188969 : 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 2184902 : default:
344 2184902 : break;
345 : }
346 2184902 : return false;
347 : }
348 :
349 : /* Check whether we may negate an integer constant T without causing
350 : overflow. */
351 :
352 : bool
353 3190161 : may_negate_without_overflow_p (const_tree t)
354 : {
355 3190161 : tree type;
356 :
357 3190161 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
358 :
359 3190161 : type = TREE_TYPE (t);
360 3190161 : if (TYPE_UNSIGNED (type))
361 : return false;
362 :
363 3190161 : 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 28045019 : negate_expr_p (tree t)
371 : {
372 28203411 : tree type;
373 :
374 28203411 : if (t == 0)
375 : return false;
376 :
377 28203411 : type = TREE_TYPE (t);
378 :
379 28203411 : STRIP_SIGN_NOPS (t);
380 28203411 : switch (TREE_CODE (t))
381 : {
382 1628742 : case INTEGER_CST:
383 1628742 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
384 : return true;
385 :
386 : /* Check that -CST will not overflow type. */
387 378157 : 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 1306835 : case REAL_CST:
399 : /* We want to canonicalize to positive real constants. Pretend
400 : that only negative ones can be easily negated. */
401 1306835 : 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 121 : case VECTOR_CST:
408 121 : {
409 121 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
410 : return true;
411 :
412 : /* Steps don't prevent negation. */
413 121 : unsigned int count = vector_cst_encoded_nelts (t);
414 363 : for (unsigned int i = 0; i < count; ++i)
415 121 : 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 1548145 : case PLUS_EXPR:
429 1548145 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
430 1548139 : || HONOR_SIGNED_ZEROS (type)
431 2805760 : || (ANY_INTEGRAL_TYPE_P (type)
432 1257403 : && ! TYPE_OVERFLOW_WRAPS (type)))
433 : return false;
434 : /* -(A + B) -> (-B) - A. */
435 805108 : if (negate_expr_p (TREE_OPERAND (t, 1)))
436 : return true;
437 : /* -(A + B) -> (-A) - B. */
438 148011 : return negate_expr_p (TREE_OPERAND (t, 0));
439 :
440 263136 : case MINUS_EXPR:
441 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
442 263136 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
443 263136 : && !HONOR_SIGNED_ZEROS (type)
444 350798 : && (! ANY_INTEGRAL_TYPE_P (type)
445 87439 : || TYPE_OVERFLOW_WRAPS (type));
446 :
447 2387473 : case MULT_EXPR:
448 2387473 : 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 4145516 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
453 157639 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
454 2228103 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
455 0 : && (wi::popcount
456 2072758 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
457 155345 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
458 132220 : && (wi::popcount
459 2204978 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
460 : break;
461 :
462 : /* Fall through. */
463 :
464 2351626 : case RDIV_EXPR:
465 2351626 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
466 2351625 : return negate_expr_p (TREE_OPERAND (t, 1))
467 2351625 : || negate_expr_p (TREE_OPERAND (t, 0));
468 : break;
469 :
470 2593 : case TRUNC_DIV_EXPR:
471 2593 : case ROUND_DIV_EXPR:
472 2593 : case EXACT_DIV_EXPR:
473 2593 : 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 5142094 : case NOP_EXPR:
491 : /* Negate -((double)float) as (double)(-float). */
492 5142094 : if (SCALAR_FLOAT_TYPE_P (type))
493 : {
494 10134 : tree tem = strip_float_extensions (t);
495 10134 : if (tem != t)
496 : return negate_expr_p (tem);
497 : }
498 : break;
499 :
500 1100426 : case CALL_EXPR:
501 : /* Negate -f(x) as f(-x). */
502 1100426 : if (negate_mathfn_p (get_call_combined_fn (t)))
503 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
504 : break;
505 :
506 12276 : case RSHIFT_EXPR:
507 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
508 12276 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 : {
510 12131 : tree op1 = TREE_OPERAND (t, 1);
511 12131 : 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 40293644 : fold_negate_expr_1 (location_t loc, tree t)
529 : {
530 40293644 : tree type = TREE_TYPE (t);
531 40293644 : tree tem;
532 :
533 40293644 : 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 30982629 : case INTEGER_CST:
543 30982629 : tem = fold_negate_const (t, type);
544 30982629 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
545 10110 : || (ANY_INTEGRAL_TYPE_P (type)
546 10110 : && !TYPE_OVERFLOW_TRAPS (type)
547 10110 : && TYPE_OVERFLOW_WRAPS (type))
548 30991949 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
549 30982517 : return tem;
550 : break;
551 :
552 2046088 : case POLY_INT_CST:
553 2046088 : case REAL_CST:
554 2046088 : case FIXED_CST:
555 2046088 : tem = fold_negate_const (t, type);
556 2046088 : return tem;
557 :
558 66208 : case COMPLEX_CST:
559 66208 : {
560 66208 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
561 66208 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
562 66208 : if (rpart && ipart)
563 66208 : return build_complex (type, rpart, ipart);
564 : }
565 : break;
566 :
567 51302 : case VECTOR_CST:
568 51302 : {
569 51302 : tree_vector_builder elts;
570 51302 : elts.new_unary_operation (type, t, true);
571 51302 : unsigned int count = elts.encoded_nelts ();
572 125490 : for (unsigned int i = 0; i < count; ++i)
573 : {
574 74188 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
575 74188 : if (elt == NULL_TREE)
576 0 : return NULL_TREE;
577 74188 : elts.quick_push (elt);
578 : }
579 :
580 51302 : return elts.build ();
581 51302 : }
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 1236 : case NEGATE_EXPR:
597 1236 : if (!TYPE_OVERFLOW_SANITIZED (type))
598 1210 : return TREE_OPERAND (t, 0);
599 : break;
600 :
601 719623 : case PLUS_EXPR:
602 719623 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
603 719623 : && !HONOR_SIGNED_ZEROS (type))
604 : {
605 : /* -(A + B) -> (-B) - A. */
606 719513 : if (negate_expr_p (TREE_OPERAND (t, 1)))
607 : {
608 661110 : tem = negate_expr (TREE_OPERAND (t, 1));
609 661110 : return fold_build2_loc (loc, MINUS_EXPR, type,
610 1322220 : tem, TREE_OPERAND (t, 0));
611 : }
612 :
613 : /* -(A + B) -> (-A) - B. */
614 58403 : if (negate_expr_p (TREE_OPERAND (t, 0)))
615 : {
616 997 : tem = negate_expr (TREE_OPERAND (t, 0));
617 997 : return fold_build2_loc (loc, MINUS_EXPR, type,
618 1994 : tem, TREE_OPERAND (t, 1));
619 : }
620 : }
621 : break;
622 :
623 160377 : case MINUS_EXPR:
624 : /* - (A - B) -> B - A */
625 160377 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
626 160377 : && !HONOR_SIGNED_ZEROS (type))
627 82520 : return fold_build2_loc (loc, MINUS_EXPR, type,
628 165040 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
629 : break;
630 :
631 272644 : case MULT_EXPR:
632 272644 : if (TYPE_UNSIGNED (type))
633 : break;
634 :
635 : /* Fall through. */
636 :
637 33687 : case RDIV_EXPR:
638 33687 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
639 : {
640 33687 : tem = TREE_OPERAND (t, 1);
641 33687 : if (negate_expr_p (tem))
642 61042 : return fold_build2_loc (loc, TREE_CODE (t), type,
643 61042 : TREE_OPERAND (t, 0), negate_expr (tem));
644 3166 : tem = TREE_OPERAND (t, 0);
645 3166 : 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 2141 : case TRUNC_DIV_EXPR:
652 2141 : case ROUND_DIV_EXPR:
653 2141 : case EXACT_DIV_EXPR:
654 2141 : 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 2412983 : case NOP_EXPR:
677 : /* Convert -((double)float) into (double)(-float). */
678 2412983 : if (SCALAR_FLOAT_TYPE_P (type))
679 : {
680 10956 : tem = strip_float_extensions (t);
681 10956 : if (tem != t && negate_expr_p (tem))
682 0 : return fold_convert_loc (loc, type, negate_expr (tem));
683 : }
684 : break;
685 :
686 298159 : case CALL_EXPR:
687 : /* Negate -f(x) as f(-x). */
688 298159 : if (negate_mathfn_p (get_call_combined_fn (t))
689 299448 : && 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 12053 : case RSHIFT_EXPR:
700 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
701 12053 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
702 : {
703 12035 : tree op1 = TREE_OPERAND (t, 1);
704 12035 : if (wi::to_wide (op1) == element_precision (type) - 1)
705 : {
706 11704 : tree ntype = TYPE_UNSIGNED (type)
707 11704 : ? signed_type_for (type)
708 72 : : unsigned_type_for (type);
709 11704 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
710 11704 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
711 11704 : 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 40293644 : fold_negate_expr (location_t loc, tree t)
727 : {
728 40293644 : tree type = TREE_TYPE (t);
729 40293644 : STRIP_SIGN_NOPS (t);
730 40293644 : tree tem = fold_negate_expr_1 (loc, t);
731 40293644 : if (tem == NULL_TREE)
732 : return NULL_TREE;
733 33936320 : 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 3923257 : negate_expr (tree t)
742 : {
743 3923257 : tree type, tem;
744 3923257 : location_t loc;
745 :
746 3923257 : if (t == NULL_TREE)
747 : return NULL_TREE;
748 :
749 3923257 : loc = EXPR_LOCATION (t);
750 3923257 : type = TREE_TYPE (t);
751 3923257 : STRIP_SIGN_NOPS (t);
752 :
753 3923257 : tem = fold_negate_expr (loc, t);
754 3923257 : if (!tem)
755 1967077 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
756 3923257 : 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 235382604 : 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 235382604 : tree var = 0;
788 235382604 : *minus_varp = 0;
789 235382604 : *conp = 0;
790 235382604 : *minus_conp = 0;
791 235382604 : *litp = 0;
792 235382604 : *minus_litp = 0;
793 :
794 : /* Strip any conversions that don't change the machine mode or signedness. */
795 235382604 : STRIP_SIGN_NOPS (in);
796 :
797 235382604 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
798 150499705 : || TREE_CODE (in) == FIXED_CST)
799 84882899 : *litp = in;
800 150499705 : else if (TREE_CODE (in) == code
801 150499705 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
802 145809666 : && ! 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 145809666 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
808 61050866 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
809 144096905 : || (code == MINUS_EXPR
810 23522215 : && (TREE_CODE (in) == PLUS_EXPR
811 21669588 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
812 : {
813 8725192 : tree op0 = TREE_OPERAND (in, 0);
814 8725192 : tree op1 = TREE_OPERAND (in, 1);
815 8725192 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
816 8725192 : 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 8725192 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
820 8505010 : || TREE_CODE (op0) == FIXED_CST)
821 220182 : *litp = op0, op0 = 0;
822 8505010 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
823 5706676 : || TREE_CODE (op1) == FIXED_CST)
824 2798334 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
825 :
826 8725192 : if (op0 != 0 && TREE_CONSTANT (op0))
827 14480 : *conp = op0, op0 = 0;
828 8710712 : else if (op1 != 0 && TREE_CONSTANT (op1))
829 52819 : *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 8725192 : if (op0 != 0 && op1 != 0)
834 : var = in;
835 3079455 : else if (op0 != 0)
836 : var = op0;
837 : else
838 234662 : var = op1, neg_var_p = neg1_p;
839 :
840 : /* Now do any needed negations. */
841 8725192 : if (neg_litp_p)
842 28708 : *minus_litp = *litp, *litp = 0;
843 8725192 : if (neg_conp_p && *conp)
844 11354 : *minus_conp = *conp, *conp = 0;
845 8725192 : if (neg_var_p && var)
846 224938 : *minus_varp = var, var = 0;
847 : }
848 141774513 : else if (TREE_CONSTANT (in))
849 821971 : *conp = in;
850 140952542 : else if (TREE_CODE (in) == BIT_NOT_EXPR
851 553885 : && code == PLUS_EXPR)
852 : {
853 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
854 : when IN is constant. */
855 392820 : *litp = build_minus_one_cst (type);
856 392820 : *minus_varp = TREE_OPERAND (in, 0);
857 : }
858 : else
859 : var = in;
860 :
861 235382604 : if (negate_p)
862 : {
863 12983439 : if (*litp)
864 1271215 : *minus_litp = *litp, *litp = 0;
865 11712224 : else if (*minus_litp)
866 174 : *litp = *minus_litp, *minus_litp = 0;
867 12983439 : if (*conp)
868 46008 : *minus_conp = *conp, *conp = 0;
869 12937431 : else if (*minus_conp)
870 0 : *conp = *minus_conp, *minus_conp = 0;
871 12983439 : if (var)
872 12924947 : *minus_varp = var, var = 0;
873 58492 : else if (*minus_varp)
874 883 : var = *minus_varp, *minus_varp = 0;
875 : }
876 :
877 235382604 : if (*litp
878 235382604 : && TREE_OVERFLOW_P (*litp))
879 20129 : *litp = drop_tree_overflow (*litp);
880 235382604 : if (*minus_litp
881 235382604 : && TREE_OVERFLOW_P (*minus_litp))
882 24 : *minus_litp = drop_tree_overflow (*minus_litp);
883 :
884 235382604 : 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 21387642 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
894 : {
895 21387642 : if (t1 == 0)
896 : {
897 13588308 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
898 : return t2;
899 : }
900 7799334 : 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 4360207 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
907 3436758 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
908 3369591 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
909 : {
910 1690773 : if (code == PLUS_EXPR)
911 : {
912 944534 : 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 944480 : 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 944479 : else if (integer_zerop (t2))
923 38042 : return fold_convert_loc (loc, type, t1);
924 : }
925 746239 : else if (code == MINUS_EXPR)
926 : {
927 722611 : if (integer_zerop (t2))
928 0 : return fold_convert_loc (loc, type, t1);
929 : }
930 :
931 1652676 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 1652676 : fold_convert_loc (loc, type, t2));
933 : }
934 :
935 2669434 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
936 2669434 : 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 2770948494 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
944 : {
945 2770948494 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
946 : return false;
947 2770948494 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
948 : return false;
949 :
950 2770948494 : switch (code)
951 : {
952 : case LSHIFT_EXPR:
953 : case RSHIFT_EXPR:
954 : case LROTATE_EXPR:
955 : case RROTATE_EXPR:
956 : return true;
957 :
958 2770948494 : default:
959 2770948494 : break;
960 : }
961 :
962 2770948494 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
963 2770948494 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
964 5541896988 : && 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 1541411026 : 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 1541411026 : wide_int tmp;
977 1541411026 : *overflow = wi::OVF_NONE;
978 1541411026 : switch (code)
979 : {
980 3786678 : case BIT_IOR_EXPR:
981 3786678 : res = wi::bit_or (arg1, arg2);
982 3786678 : break;
983 :
984 96610 : case BIT_XOR_EXPR:
985 96610 : res = wi::bit_xor (arg1, arg2);
986 96610 : break;
987 :
988 23969484 : case BIT_AND_EXPR:
989 23969484 : res = wi::bit_and (arg1, arg2);
990 23969484 : break;
991 :
992 15104236 : case LSHIFT_EXPR:
993 15104236 : if (wi::neg_p (arg2))
994 : return false;
995 15073928 : res = wi::lshift (arg1, arg2);
996 15073928 : break;
997 :
998 7518239 : case RSHIFT_EXPR:
999 7518239 : 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 7518043 : res = wi::rshift (arg1, arg2, sign);
1005 7518043 : break;
1006 :
1007 1720 : case RROTATE_EXPR:
1008 1720 : case LROTATE_EXPR:
1009 1720 : 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 1706 : tmp = arg2;
1019 :
1020 1706 : if (code == RROTATE_EXPR)
1021 1533 : res = wi::rrotate (arg1, tmp);
1022 : else
1023 187 : res = wi::lrotate (arg1, tmp);
1024 : break;
1025 :
1026 260584706 : case PLUS_EXPR:
1027 260584706 : res = wi::add (arg1, arg2, sign, overflow);
1028 260584706 : break;
1029 :
1030 75792556 : case MINUS_EXPR:
1031 75792556 : res = wi::sub (arg1, arg2, sign, overflow);
1032 75792556 : break;
1033 :
1034 442499257 : case MULT_EXPR:
1035 442499257 : res = wi::mul (arg1, arg2, sign, overflow);
1036 442499257 : break;
1037 :
1038 5544 : case MULT_HIGHPART_EXPR:
1039 5544 : res = wi::mul_high (arg1, arg2, sign);
1040 5544 : break;
1041 :
1042 364464475 : case TRUNC_DIV_EXPR:
1043 364464475 : case EXACT_DIV_EXPR:
1044 364464475 : if (arg2 == 0)
1045 : return false;
1046 364458525 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1047 364458525 : break;
1048 :
1049 82073975 : case FLOOR_DIV_EXPR:
1050 82073975 : if (arg2 == 0)
1051 : return false;
1052 82073975 : res = wi::div_floor (arg1, arg2, sign, overflow);
1053 82073975 : break;
1054 :
1055 88526577 : case CEIL_DIV_EXPR:
1056 88526577 : if (arg2 == 0)
1057 : return false;
1058 88526577 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1059 88526577 : 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 1380567 : case TRUNC_MOD_EXPR:
1068 1380567 : if (arg2 == 0)
1069 : return false;
1070 1379465 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1071 1379465 : break;
1072 :
1073 70336418 : case FLOOR_MOD_EXPR:
1074 70336418 : if (arg2 == 0)
1075 : return false;
1076 70336418 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1077 70336418 : 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 49076 : case MIN_EXPR:
1092 49076 : res = wi::min (arg1, arg2, sign);
1093 49076 : break;
1094 :
1095 105220603 : case MAX_EXPR:
1096 105220603 : res = wi::max (arg1, arg2, sign);
1097 105220603 : break;
1098 :
1099 : default:
1100 : return false;
1101 : }
1102 : return true;
1103 1541411026 : }
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 1541411026 : 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 1541411026 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1134 :
1135 1541411026 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1136 : {
1137 1541411026 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1138 1541411026 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1139 1541411026 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1140 : return NULL_TREE;
1141 1541373343 : res = wi_res;
1142 1541373343 : return true;
1143 1541411281 : }
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 1541411026 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1208 : int overflowable)
1209 : {
1210 1541411026 : poly_wide_int poly_res;
1211 1541411026 : tree type = TREE_TYPE (arg1);
1212 1541411026 : signop sign = TYPE_SIGN (type);
1213 1541411026 : wi::overflow_type overflow = wi::OVF_NONE;
1214 :
1215 1541411026 : if (!poly_int_tree_p (arg1)
1216 1541411026 : || !poly_int_tree_p (arg2)
1217 3082822052 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1218 : return NULL_TREE;
1219 1541373343 : return force_fit_type (type, poly_res, overflowable,
1220 1541373343 : (((sign == SIGNED || overflowable == -1)
1221 1541373343 : && overflow)
1222 1541373343 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1223 1541411026 : }
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 189845 : 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 447120 : 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 447120 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1255 : {
1256 362293 : if (integer_zerop (other_op))
1257 : {
1258 27645 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1259 : return op;
1260 26491 : else if (code == BIT_AND_EXPR)
1261 4289 : 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 268977 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1274 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1275 : {
1276 192563 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1277 453148 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1278 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1279 : {
1280 184171 : tree type = TREE_TYPE (arg1);
1281 184171 : bool step_ok_p;
1282 184171 : if (VECTOR_CST_STEPPED_P (arg1)
1283 184171 : && 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 181453 : 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 50535 : step_ok_p = distributes_over_addition_p (code, 1);
1302 : else
1303 : /* Similarly in reverse. */
1304 130918 : step_ok_p = distributes_over_addition_p (code, 2);
1305 184171 : tree_vector_builder elts;
1306 184171 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1307 : return NULL_TREE;
1308 184171 : unsigned int count = elts.encoded_nelts ();
1309 714650 : for (unsigned int i = 0; i < count; ++i)
1310 : {
1311 530798 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1312 530798 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1313 :
1314 530798 : 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 530798 : if (elt == NULL_TREE)
1319 319 : return NULL_TREE;
1320 530479 : elts.quick_push (elt);
1321 : }
1322 :
1323 183852 : return elts.build ();
1324 184171 : }
1325 :
1326 84806 : 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 224789442 : const_binop (enum tree_code code, tree arg1, tree arg2)
1360 : {
1361 : /* Sanity check for the recursive cases. */
1362 224789442 : if (!arg1 || !arg2)
1363 : return NULL_TREE;
1364 :
1365 224788178 : STRIP_NOPS (arg1);
1366 224788178 : STRIP_NOPS (arg2);
1367 :
1368 224788178 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1369 : {
1370 218948357 : if (code == POINTER_PLUS_EXPR)
1371 107105 : return int_const_binop (PLUS_EXPR,
1372 214210 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1373 :
1374 218841252 : return int_const_binop (code, arg1, arg2);
1375 : }
1376 :
1377 5839821 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1378 : {
1379 5554311 : machine_mode mode;
1380 5554311 : REAL_VALUE_TYPE d1;
1381 5554311 : REAL_VALUE_TYPE d2;
1382 5554311 : REAL_VALUE_TYPE value;
1383 5554311 : REAL_VALUE_TYPE result;
1384 5554311 : bool inexact;
1385 5554311 : tree t, type;
1386 :
1387 : /* The following codes are handled by real_arithmetic. */
1388 5554311 : switch (code)
1389 : {
1390 5554311 : case PLUS_EXPR:
1391 5554311 : case MINUS_EXPR:
1392 5554311 : case MULT_EXPR:
1393 5554311 : case RDIV_EXPR:
1394 5554311 : case MIN_EXPR:
1395 5554311 : case MAX_EXPR:
1396 5554311 : break;
1397 :
1398 : default:
1399 : return NULL_TREE;
1400 : }
1401 :
1402 5554311 : d1 = TREE_REAL_CST (arg1);
1403 5554311 : d2 = TREE_REAL_CST (arg2);
1404 :
1405 5554311 : type = TREE_TYPE (arg1);
1406 5554311 : mode = TYPE_MODE (type);
1407 :
1408 : /* Don't perform operation if we honor signaling NaNs and
1409 : either operand is a signaling NaN. */
1410 5554311 : if (HONOR_SNANS (mode)
1411 5554311 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1412 6967 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1413 : return NULL_TREE;
1414 :
1415 : /* Don't perform operation if it would raise a division
1416 : by zero exception. */
1417 5554278 : if (code == RDIV_EXPR
1418 2422251 : && real_equal (&d2, &dconst0)
1419 5564999 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1420 : 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 5546465 : 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 5546119 : 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 5546058 : inexact = real_arithmetic (&value, code, &d1, &d2);
1442 5546058 : 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 5546058 : if (flag_trapping_math
1449 21537902 : && MODE_HAS_NANS (mode)
1450 5526024 : && REAL_VALUE_ISNAN (result)
1451 2583 : && !REAL_VALUE_ISNAN (d1)
1452 5548641 : && !REAL_VALUE_ISNAN (d2))
1453 2583 : return NULL_TREE;
1454 :
1455 : /* Don't constant fold this floating point operation if
1456 : the result has overflowed and flag_trapping_math. */
1457 5543475 : if (flag_trapping_math
1458 21527912 : && MODE_HAS_INFINITIES (mode)
1459 5523441 : && REAL_VALUE_ISINF (result)
1460 7846 : && !REAL_VALUE_ISINF (d1)
1461 5550653 : && !REAL_VALUE_ISINF (d2))
1462 4895 : 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 5538580 : if ((flag_rounding_math
1469 37625330 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1470 5538580 : && (inexact || !real_identical (&result, &value)))
1471 : return NULL_TREE;
1472 :
1473 5537473 : t = build_real (type, result);
1474 :
1475 5537473 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1476 5537473 : return t;
1477 : }
1478 :
1479 285510 : 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 : return t;
1525 : }
1526 :
1527 285510 : 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 274420 : tree simplified;
1657 274420 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1658 : return simplified;
1659 :
1660 273843 : if (commutative_tree_code (code)
1661 273843 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1662 : return simplified;
1663 :
1664 268977 : 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 300238819 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1672 : {
1673 300238819 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1674 83263431 : 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 216975388 : 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 269919 : case COMPLEX_EXPR:
1687 269919 : if ((TREE_CODE (arg1) == REAL_CST
1688 259380 : && TREE_CODE (arg2) == REAL_CST)
1689 10541 : || (TREE_CODE (arg1) == INTEGER_CST
1690 10539 : && TREE_CODE (arg2) == INTEGER_CST))
1691 269917 : return build_complex (type, arg1, arg2);
1692 : return NULL_TREE;
1693 :
1694 180964 : case POINTER_DIFF_EXPR:
1695 180964 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1696 : {
1697 361276 : poly_offset_int res = (wi::to_poly_offset (arg1)
1698 180638 : - wi::to_poly_offset (arg2));
1699 180638 : return force_fit_type (type, res, 1,
1700 180638 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1701 : }
1702 : return NULL_TREE;
1703 :
1704 14959 : case VEC_PACK_TRUNC_EXPR:
1705 14959 : case VEC_PACK_FIX_TRUNC_EXPR:
1706 14959 : case VEC_PACK_FLOAT_EXPR:
1707 14959 : {
1708 14959 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1709 :
1710 14959 : if (TREE_CODE (arg1) != VECTOR_CST
1711 14959 : || TREE_CODE (arg2) != VECTOR_CST)
1712 : return NULL_TREE;
1713 :
1714 14959 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1715 : return NULL_TREE;
1716 :
1717 14959 : out_nelts = in_nelts * 2;
1718 14959 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1719 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1720 :
1721 14959 : tree_vector_builder elts (type, out_nelts, 1);
1722 202314 : for (i = 0; i < out_nelts; i++)
1723 : {
1724 172408 : tree elt = (i < in_nelts
1725 172408 : ? VECTOR_CST_ELT (arg1, i)
1726 86198 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1727 173452 : 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 172408 : TREE_TYPE (type), elt);
1732 172408 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1733 12 : return NULL_TREE;
1734 172396 : elts.quick_push (elt);
1735 : }
1736 :
1737 14947 : return elts.build ();
1738 14959 : }
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 944 : 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 216509322 : default:;
1786 : }
1787 :
1788 216509322 : if (TREE_CODE_CLASS (code) != tcc_binary)
1789 : return NULL_TREE;
1790 :
1791 : /* Make sure type and arg0 have the same saturating flag. */
1792 213972840 : gcc_checking_assert (TYPE_SATURATING (type)
1793 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1794 :
1795 213972840 : 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 380960592 : 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 380960592 : if (TREE_CODE (arg0) == REAL_CST
1807 11146765 : && HONOR_SNANS (arg0)
1808 17121 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1809 4740 : && code != NEGATE_EXPR
1810 4740 : && code != ABS_EXPR
1811 380965297 : && code != ABSU_EXPR)
1812 : return NULL_TREE;
1813 :
1814 380955887 : switch (code)
1815 : {
1816 285789153 : CASE_CONVERT:
1817 285789153 : case FLOAT_EXPR:
1818 285789153 : case FIX_TRUNC_EXPR:
1819 285789153 : case FIXED_CONVERT_EXPR:
1820 285789153 : 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 13284887 : case VIEW_CONVERT_EXPR:
1832 13284887 : return fold_view_convert_expr (type, arg0);
1833 :
1834 31918562 : case NEGATE_EXPR:
1835 31918562 : {
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 31918562 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1840 31918562 : if (tem && CONSTANT_CLASS_P (tem))
1841 31771783 : return tem;
1842 : break;
1843 : }
1844 :
1845 40733 : case ABS_EXPR:
1846 40733 : case ABSU_EXPR:
1847 40733 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1848 36176 : 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 2347345 : case BIT_NOT_EXPR:
1861 2347345 : if (TREE_CODE (arg0) == INTEGER_CST)
1862 2340292 : return fold_not_const (arg0, type);
1863 7053 : 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 7053 : else if (TREE_CODE (arg0) == VECTOR_CST)
1867 : {
1868 6419 : tree elem;
1869 :
1870 : /* This can cope with stepped encodings because ~x == -1 - x. */
1871 6419 : tree_vector_builder elements;
1872 6419 : elements.new_unary_operation (type, arg0, true);
1873 6419 : unsigned int i, count = elements.encoded_nelts ();
1874 24971 : for (i = 0; i < count; ++i)
1875 : {
1876 18552 : elem = VECTOR_CST_ELT (arg0, i);
1877 18552 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1878 18552 : if (elem == NULL_TREE)
1879 : break;
1880 18552 : elements.quick_push (elem);
1881 : }
1882 6419 : if (i == count)
1883 6419 : return elements.build ();
1884 6419 : }
1885 : break;
1886 :
1887 11486274 : case TRUTH_NOT_EXPR:
1888 11486274 : if (TREE_CODE (arg0) == INTEGER_CST)
1889 11118793 : return constant_boolean_node (integer_zerop (arg0), type);
1890 : break;
1891 :
1892 179782 : case REALPART_EXPR:
1893 179782 : if (TREE_CODE (arg0) == COMPLEX_CST)
1894 179581 : return fold_convert (type, TREE_REALPART (arg0));
1895 : break;
1896 :
1897 185580 : case IMAGPART_EXPR:
1898 185580 : if (TREE_CODE (arg0) == COMPLEX_CST)
1899 185392 : 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 120714 : 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 3637239049 : size_int_kind (poly_int64 number, enum size_type_kind kind)
1964 : {
1965 3637239049 : 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 2731946852 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1975 : {
1976 2731946852 : tree type = TREE_TYPE (arg0);
1977 :
1978 2731946852 : if (arg0 == error_mark_node || arg1 == error_mark_node)
1979 : return error_mark_node;
1980 :
1981 2731946852 : 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 2731946852 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1986 : {
1987 : /* And some specific cases even faster than that. */
1988 2699014935 : if (code == PLUS_EXPR)
1989 : {
1990 1247933695 : if (integer_zerop (arg0)
1991 1247933695 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1992 : return arg1;
1993 318916948 : if (integer_zerop (arg1)
1994 318916948 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1995 : return arg0;
1996 : }
1997 1451081240 : else if (code == MINUS_EXPR)
1998 : {
1999 123401816 : if (integer_zerop (arg1)
2000 123401816 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2001 : return arg0;
2002 : }
2003 1327679424 : else if (code == MULT_EXPR)
2004 : {
2005 626139441 : if (integer_onep (arg0)
2006 626139441 : && !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 1299845031 : tree res = int_const_binop (code, arg0, arg1, -1);
2014 1299845031 : if (res != NULL_TREE)
2015 : return res;
2016 : }
2017 :
2018 32931917 : 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 39001642 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2027 : {
2028 39001642 : tree type = TREE_TYPE (arg0);
2029 39001642 : tree ctype;
2030 :
2031 39001642 : 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 39001642 : if (!TYPE_UNSIGNED (type))
2036 10496969 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2037 :
2038 28504673 : if (type == sizetype)
2039 28504673 : 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 28504673 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2049 17595 : return size_binop_loc (loc, MINUS_EXPR,
2050 : fold_convert_loc (loc, ctype, arg0),
2051 17595 : 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 28487078 : if (tree_int_cst_equal (arg0, arg1))
2058 25190756 : return build_int_cst (ctype, 0);
2059 3296322 : else if (tree_int_cst_lt (arg1, arg0))
2060 2158394 : return fold_convert_loc (loc, ctype,
2061 2158394 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2062 : else
2063 1137928 : 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 1471425590 : 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 1471425590 : tree arg1_type = TREE_TYPE (arg1);
2080 1471425590 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2081 1471425590 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2082 1471425590 : TYPE_SIGN (arg1_type)),
2083 : overflowable,
2084 1471425590 : 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 58651 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2092 : {
2093 58651 : bool overflow = false;
2094 58651 : 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 58651 : wide_int val;
2106 58651 : REAL_VALUE_TYPE r;
2107 58651 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2108 :
2109 58651 : switch (code)
2110 : {
2111 58651 : case FIX_TRUNC_EXPR:
2112 58651 : real_trunc (&r, VOIDmode, &x);
2113 58651 : 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 58651 : 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 58651 : if (! overflow)
2130 : {
2131 55013 : tree lt = TYPE_MIN_VALUE (type);
2132 55013 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2133 55013 : if (real_less (&r, &l))
2134 : {
2135 1974 : overflow = true;
2136 1974 : val = wi::to_wide (lt);
2137 : }
2138 : }
2139 :
2140 58651 : if (! overflow)
2141 : {
2142 53039 : tree ut = TYPE_MAX_VALUE (type);
2143 53039 : if (ut)
2144 : {
2145 53039 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2146 53039 : if (real_less (&u, &r))
2147 : {
2148 1921 : overflow = true;
2149 1921 : val = wi::to_wide (ut);
2150 : }
2151 : }
2152 : }
2153 :
2154 58651 : if (! overflow)
2155 51120 : 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 58651 : if (!flag_trapping_math || !overflow)
2165 51374 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2166 : else
2167 : t = NULL_TREE;
2168 :
2169 58651 : return t;
2170 58651 : }
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 2254131 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2225 : {
2226 2254131 : REAL_VALUE_TYPE value;
2227 2254131 : 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 2254131 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2233 : {
2234 99286 : t = build_real (type, TREE_REAL_CST (arg1));
2235 99286 : 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 2154845 : if (HONOR_SNANS (arg1)
2241 2156727 : && 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 2154845 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2247 2155501 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2248 509 : return NULL_TREE;
2249 :
2250 2154336 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2251 2154336 : 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 2154336 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2258 2295134 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2259 0 : TREE_OVERFLOW (t) = 1;
2260 2154336 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2261 2290868 : && !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 10768366 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2266 0 : && REAL_VALUE_ISINF (value)
2267 2154336 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2268 0 : TREE_OVERFLOW (t) = 1;
2269 : else
2270 2154336 : 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 1536568556 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2367 : {
2368 1536568556 : tree arg_type = TREE_TYPE (arg1);
2369 1536568556 : 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 1525077953 : 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 1525077953 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2383 : || TREE_CODE (type) == OFFSET_TYPE)
2384 : {
2385 1493506264 : if (TREE_CODE (arg1) == INTEGER_CST)
2386 1471425590 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2387 22080674 : else if (TREE_CODE (arg1) == REAL_CST)
2388 58651 : return fold_convert_const_int_from_real (code, type, arg1);
2389 22022023 : 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 31514709 : if (TREE_CODE (arg1) == INTEGER_CST)
2395 : {
2396 24223329 : 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 24223329 : 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 : return res;
2408 : }
2409 7291380 : else if (TREE_CODE (arg1) == REAL_CST)
2410 2254131 : return fold_convert_const_real_from_real (type, arg1);
2411 5037249 : 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 4730 : if (TREE_CODE (arg1) == VECTOR_CST
2426 4730 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2427 : {
2428 4730 : tree elttype = TREE_TYPE (type);
2429 4730 : 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 4730 : bool step_ok_p
2433 4730 : = (INTEGRAL_TYPE_P (elttype)
2434 345 : && INTEGRAL_TYPE_P (arg1_elttype)
2435 5015 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2436 4730 : tree_vector_builder v;
2437 4730 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2438 : return NULL_TREE;
2439 4730 : unsigned int len = v.encoded_nelts ();
2440 27918 : for (unsigned int i = 0; i < len; ++i)
2441 : {
2442 23188 : tree elt = VECTOR_CST_ELT (arg1, i);
2443 23188 : tree cvt = fold_convert_const (code, elttype, elt);
2444 23188 : if (cvt == NULL_TREE)
2445 0 : return NULL_TREE;
2446 23188 : v.quick_push (cvt);
2447 : }
2448 4730 : return v.build ();
2449 4730 : }
2450 : }
2451 12255 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2452 12255 : 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 17481 : build_zero_vector (tree type)
2460 : {
2461 17481 : tree t;
2462 :
2463 17481 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2464 17481 : 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 4400 : fold_convertible_p (const_tree type, const_tree arg)
2471 : {
2472 4400 : const_tree orig = TREE_TYPE (arg);
2473 :
2474 4400 : if (type == orig)
2475 : return true;
2476 :
2477 4400 : if (TREE_CODE (arg) == ERROR_MARK
2478 4400 : || TREE_CODE (type) == ERROR_MARK
2479 4400 : || TREE_CODE (orig) == ERROR_MARK)
2480 : return false;
2481 :
2482 4400 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2483 : return true;
2484 :
2485 4400 : switch (TREE_CODE (type))
2486 : {
2487 3820 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2488 3820 : case POINTER_TYPE: case REFERENCE_TYPE:
2489 3820 : case OFFSET_TYPE:
2490 3820 : return (INTEGRAL_TYPE_P (orig)
2491 386 : || (POINTER_TYPE_P (orig)
2492 251 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2493 3955 : || 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 2254229926 : fold_convert_loc (location_t loc, tree type, tree arg)
2516 : {
2517 2254229926 : tree orig = TREE_TYPE (arg);
2518 2254229926 : tree tem;
2519 :
2520 2254229926 : if (type == orig)
2521 : return arg;
2522 :
2523 1524334581 : if (TREE_CODE (arg) == ERROR_MARK
2524 1524333553 : || TREE_CODE (type) == ERROR_MARK
2525 1524333552 : || TREE_CODE (orig) == ERROR_MARK)
2526 1029 : return error_mark_node;
2527 :
2528 1524333552 : switch (TREE_CODE (type))
2529 : {
2530 120889369 : case POINTER_TYPE:
2531 120889369 : case REFERENCE_TYPE:
2532 : /* Handle conversions between pointers to different address spaces. */
2533 120889369 : if (POINTER_TYPE_P (orig)
2534 120889369 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2535 101895749 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2536 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2537 : /* fall through */
2538 :
2539 1491860031 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2540 1491860031 : case OFFSET_TYPE: case BITINT_TYPE:
2541 1491860031 : if (TREE_CODE (arg) == INTEGER_CST)
2542 : {
2543 1250303051 : tem = fold_convert_const (NOP_EXPR, type, arg);
2544 1250303051 : if (tem != NULL_TREE)
2545 : return tem;
2546 : }
2547 241556980 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2548 2536 : || TREE_CODE (orig) == OFFSET_TYPE)
2549 241556980 : 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 557898 : case REAL_TYPE:
2559 557898 : if (TREE_CODE (arg) == INTEGER_CST)
2560 : {
2561 58047 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2562 58047 : if (tem != NULL_TREE)
2563 : return tem;
2564 : }
2565 499851 : else if (TREE_CODE (arg) == REAL_CST)
2566 : {
2567 121686 : tem = fold_convert_const (NOP_EXPR, type, arg);
2568 121686 : if (tem != NULL_TREE)
2569 : return tem;
2570 : }
2571 378165 : 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 378167 : switch (TREE_CODE (orig))
2579 : {
2580 721 : case INTEGER_TYPE: case BITINT_TYPE:
2581 721 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2582 721 : case POINTER_TYPE: case REFERENCE_TYPE:
2583 721 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2584 :
2585 377446 : case REAL_TYPE:
2586 377446 : 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 31798357 : case VECTOR_TYPE:
2664 31798357 : if (integer_zerop (arg))
2665 17481 : return build_zero_vector (type);
2666 31780876 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 31780876 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2668 : || VECTOR_TYPE_P (orig));
2669 31780876 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2670 :
2671 111088 : case VOID_TYPE:
2672 111088 : tem = fold_ignored_result (arg);
2673 111088 : 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 3768 : default:
2680 3768 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2681 3768 : 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 54133838 : maybe_lvalue_p (const_tree x)
2694 : {
2695 : /* We only need to wrap lvalue tree codes. */
2696 54133838 : 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 39798672 : default:
2730 : /* Assume the worst for front-end tree codes. */
2731 39798672 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2732 : break;
2733 : return false;
2734 : }
2735 :
2736 14370070 : return true;
2737 : }
2738 :
2739 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2740 :
2741 : tree
2742 47218105 : 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 47218105 : if (in_gimple_form)
2747 : return x;
2748 :
2749 10904504 : if (! maybe_lvalue_p (x))
2750 : return x;
2751 2596399 : 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 132125853 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2761 : {
2762 132125853 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2763 1046884 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2764 : return ERROR_MARK;
2765 :
2766 131330331 : switch (code)
2767 : {
2768 : case EQ_EXPR:
2769 : return NE_EXPR;
2770 57758470 : case NE_EXPR:
2771 57758470 : return EQ_EXPR;
2772 12455698 : case GT_EXPR:
2773 12455698 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2774 18096688 : case GE_EXPR:
2775 18096688 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2776 8177157 : case LT_EXPR:
2777 8177157 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2778 8165664 : case LE_EXPR:
2779 8165664 : 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 223186 : case ORDERED_EXPR:
2793 223186 : return UNORDERED_EXPR;
2794 57034 : case UNORDERED_EXPR:
2795 57034 : 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 162108569 : swap_tree_comparison (enum tree_code code)
2806 : {
2807 162108569 : 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 39646685 : case GT_EXPR:
2817 39646685 : return LT_EXPR;
2818 11236367 : case GE_EXPR:
2819 11236367 : return LE_EXPR;
2820 22188395 : case LT_EXPR:
2821 22188395 : return GT_EXPR;
2822 16963179 : case LE_EXPR:
2823 16963179 : return GE_EXPR;
2824 249870 : case UNGT_EXPR:
2825 249870 : return UNLT_EXPR;
2826 19382 : case UNGE_EXPR:
2827 19382 : return UNLE_EXPR;
2828 374947 : case UNLT_EXPR:
2829 374947 : return UNGT_EXPR;
2830 111981 : case UNLE_EXPR:
2831 111981 : 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 404398 : comparison_to_compcode (enum tree_code code)
2844 : {
2845 404398 : 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 21800 : compcode_to_comparison (enum comparison_code code)
2886 : {
2887 21800 : 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 1790934 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2926 : {
2927 1790934 : return (COMPARISON_CLASS_P (cond1)
2928 1698448 : && COMPARISON_CLASS_P (cond2)
2929 1687070 : && (invert_tree_comparison
2930 1687070 : (TREE_CODE (cond1),
2931 3374140 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2932 70441 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2933 70441 : TREE_OPERAND (cond2, 0), 0)
2934 1813375 : && operand_equal_p (TREE_OPERAND (cond1, 1),
2935 22441 : 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 202199 : 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 202199 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
2951 202199 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
2952 202199 : int compcode;
2953 202199 : *res = NULL_TREE;
2954 :
2955 202199 : switch (code)
2956 : {
2957 179900 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2958 179900 : case BIT_AND_EXPR:
2959 179900 : compcode = lcompcode & rcompcode;
2960 179900 : break;
2961 :
2962 22299 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2963 22299 : case BIT_IOR_EXPR:
2964 22299 : compcode = lcompcode | rcompcode;
2965 22299 : break;
2966 :
2967 : default:
2968 : return ERROR_MARK;
2969 : }
2970 :
2971 202199 : 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 34254 : compcode &= ~COMPCODE_UNORD;
2976 34254 : if (compcode == COMPCODE_LTGT)
2977 : compcode = COMPCODE_NE;
2978 31253 : else if (compcode == COMPCODE_ORD)
2979 5812 : compcode = COMPCODE_TRUE;
2980 : }
2981 167945 : else if (flag_trapping_math)
2982 : {
2983 : /* Check that the original operation and the optimized ones will trap
2984 : under the same condition. */
2985 333590 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2986 163813 : && (lcompcode != COMPCODE_EQ)
2987 166795 : && (lcompcode != COMPCODE_ORD);
2988 333590 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2989 158938 : && (rcompcode != COMPCODE_EQ)
2990 166795 : && (rcompcode != COMPCODE_ORD);
2991 333590 : bool trap = (compcode & COMPCODE_UNORD) == 0
2992 165367 : && (compcode != COMPCODE_EQ)
2993 166795 : && (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 166795 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3002 166320 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3003 166795 : 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 166795 : if (code == TRUTH_ANDIF_EXPR && lcompcode == COMPCODE_NE && rtrap && trap)
3008 : ;
3009 : /* Likewise of `a == b || a < b` for the same reason. */
3010 166699 : 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 166631 : 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 166631 : else if ((ltrap || rtrap) != trap)
3020 : return ERROR_MARK;
3021 : }
3022 :
3023 37489 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3024 : {
3025 15689 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3026 15689 : return INTEGER_CST;
3027 : }
3028 : else
3029 21800 : 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 40002 : 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 40002 : bool honor_nans = HONOR_NANS (ll_arg);
3045 40002 : tree_code rescode;
3046 40002 : tree res;
3047 40002 : rescode = combine_comparisons (code, lcode, rcode, truth_type,
3048 : honor_nans, &res);
3049 40002 : if (rescode == ERROR_MARK)
3050 : return NULL_TREE;
3051 36438 : if (rescode == INTEGER_CST)
3052 15689 : return res;
3053 :
3054 20749 : 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 7352524444 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3115 : unsigned int flags)
3116 : {
3117 7352524444 : 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 7353265582 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3127 : tree type1, const_tree arg1,
3128 : unsigned int flags)
3129 : {
3130 7353265582 : bool r;
3131 7353265582 : if (verify_hash_value (arg0, arg1, flags, &r))
3132 3096632329 : return r;
3133 :
3134 4256633253 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3135 4256633253 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3136 :
3137 : /* If either is ERROR_MARK, they aren't equal. */
3138 4256633253 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3139 4256632662 : || type0 == error_mark_node
3140 4256632660 : || 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 4256632659 : if (!type0 || !type1)
3146 : return false;
3147 :
3148 : /* Bitwise identity makes no sense if the values have different layouts. */
3149 4256627500 : if ((flags & OEP_BITWISE)
3150 4256627500 : && !tree_nop_conversion_p (type0, type1))
3151 : return false;
3152 :
3153 : /* We cannot consider pointers to different address space equal. */
3154 4256627500 : if (POINTER_TYPE_P (type0)
3155 660638628 : && POINTER_TYPE_P (type1)
3156 4823949391 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3157 567321891 : != 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 4256627256 : 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 667700148 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3167 667700148 : return tree_int_cst_equal (arg0, arg1);
3168 : }
3169 :
3170 3588927108 : if ((flags & OEP_ASSUME_WRAPV)
3171 2097329 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3172 : {
3173 792435 : const_tree t_arg0 = arg0;
3174 792435 : const_tree t_arg1 = arg1;
3175 792435 : STRIP_NOPS (arg0);
3176 792435 : STRIP_NOPS (arg1);
3177 : /* Only recurse if the conversion was one that was valid to strip. */
3178 792435 : if (t_arg0 != arg0 || t_arg1 != arg1)
3179 741138 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3180 : }
3181 :
3182 3588185970 : 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 3183741209 : bool enforce_signedness = true;
3188 3183741209 : if (flags & OEP_ASSUME_WRAPV)
3189 : {
3190 1261712 : 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 3183741209 : 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 3183741209 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3220 3183741209 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3221 146915330 : && 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 2880019826 : if (element_precision (type0) != element_precision (type1))
3227 : return false;
3228 :
3229 2726843004 : STRIP_NOPS (arg0);
3230 2726843004 : STRIP_NOPS (arg1);
3231 :
3232 2726843004 : type0 = TREE_TYPE (arg0);
3233 2726843004 : 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 3131287765 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3250 1263606441 : && COMPARISON_CLASS_P (arg0)
3251 6857683 : && COMPARISON_CLASS_P (arg1))
3252 : {
3253 5122688 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3254 :
3255 5122688 : if (TREE_CODE (arg0) == swap_code)
3256 2184307 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3257 2184307 : TREE_OPERAND (arg1, 1), flags)
3258 2204082 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3259 19775 : TREE_OPERAND (arg1, 0), flags);
3260 : }
3261 :
3262 3129103458 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3263 : {
3264 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3265 1261422134 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3266 : ;
3267 1261357525 : 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 49673179 : if (TREE_CODE (arg0) == MEM_REF
3273 8217676 : && DECL_P (arg1)
3274 5792661 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3275 1377211 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3276 50418538 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3277 : return true;
3278 49441831 : else if (TREE_CODE (arg1) == MEM_REF
3279 30856449 : && DECL_P (arg0)
3280 11295596 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3281 2339686 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3282 50037880 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3283 : return true;
3284 : 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 1867745933 : if (TREE_CODE (type0) == ERROR_MARK
3293 1867745933 : || TREE_CODE (type1) == ERROR_MARK
3294 3735491866 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3295 26280533 : && !(flags & OEP_ADDRESS_OF)))
3296 : 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 332303233 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3308 2196129054 : && (TREE_CODE (arg0) == SAVE_EXPR
3309 332277620 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3310 292661658 : || (! 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 1531688965 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3316 25971380 : 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 3778609 : case REAL_CST:
3326 3778609 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3327 : return true;
3328 :
3329 2744088 : 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 15185 : if (real_zerop (arg0) && real_zerop (arg1))
3334 : return true;
3335 : }
3336 : return false;
3337 :
3338 1018905 : case VECTOR_CST:
3339 1018905 : {
3340 1018905 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3341 1018905 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3342 : return false;
3343 :
3344 997153 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3345 997153 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3346 : return false;
3347 :
3348 961921 : unsigned int count = vector_cst_encoded_nelts (arg0);
3349 2303814 : for (unsigned int i = 0; i < count; ++i)
3350 2190952 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3351 1095476 : 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 1031775 : case STRING_CST:
3363 1031775 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3364 1031775 : && ! memcmp (TREE_STRING_POINTER (arg0),
3365 592928 : TREE_STRING_POINTER (arg1),
3366 592928 : 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 18987303 : case ADDR_EXPR:
3375 18987303 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3376 18987303 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3377 : flags | OEP_ADDRESS_OF
3378 18987303 : | OEP_MATCH_SIDE_EFFECTS);
3379 183912 : case CONSTRUCTOR:
3380 183912 : {
3381 : /* In GIMPLE empty constructors are allowed in initializers of
3382 : aggregates. */
3383 183912 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3384 : return true;
3385 :
3386 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3387 138145 : if (TREE_CODE (type0) != TREE_CODE (type1))
3388 : return false;
3389 138145 : 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 : return false;
3397 : }
3398 137881 : else if (!types_compatible_p (type0, type1))
3399 : return false;
3400 :
3401 138145 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3402 138145 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3403 414435 : 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 138145 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3409 :
3410 368988984 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3411 : {
3412 206349 : constructor_elt *c0 = &(*v0)[idx];
3413 206349 : constructor_elt *c1 = &(*v1)[idx];
3414 :
3415 : /* Check that the values are the same... */
3416 206349 : if (c0->value != c1->value
3417 206349 : && !operand_equal_p (c0->value, c1->value, flags))
3418 : return false;
3419 :
3420 : /* ... and that they apply to the same field! */
3421 117785 : if (c0->index != c1->index
3422 117785 : && (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 : 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 1506674341 : 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 1506674341 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3457 : {
3458 8343739 : case tcc_unary:
3459 : /* Two conversions are equal only if signedness and modes match. */
3460 8343739 : switch (TREE_CODE (arg0))
3461 : {
3462 7974163 : CASE_CONVERT:
3463 7974163 : case FIX_TRUNC_EXPR:
3464 7974163 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3465 : return false;
3466 : break;
3467 : default:
3468 : break;
3469 : }
3470 :
3471 8343718 : return OP_SAME_WITH_NULL (0);
3472 :
3473 :
3474 23334644 : case tcc_comparison:
3475 23334644 : case tcc_binary:
3476 23334644 : if (OP_SAME (0) && OP_SAME (1))
3477 : return true;
3478 :
3479 : /* For commutative ops, allow the other order. */
3480 17083533 : return (commutative_tree_code (TREE_CODE (arg0))
3481 12981539 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3482 12981539 : TREE_OPERAND (arg1, 1), flags)
3483 17321486 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3484 237953 : TREE_OPERAND (arg1, 0), flags));
3485 :
3486 880176397 : 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 880176397 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3491 880176397 : && (TREE_SIDE_EFFECTS (arg0)
3492 812189800 : || TREE_SIDE_EFFECTS (arg1)))
3493 : return false;
3494 :
3495 879608378 : switch (TREE_CODE (arg0))
3496 : {
3497 5589435 : case INDIRECT_REF:
3498 5589435 : if (!(flags & OEP_ADDRESS_OF))
3499 : {
3500 5566913 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3501 : return false;
3502 : /* Verify that the access types are compatible. */
3503 5560622 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3504 : return false;
3505 : }
3506 5514860 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3507 5514860 : return OP_SAME (0);
3508 :
3509 574941 : case IMAGPART_EXPR:
3510 : /* Require the same offset. */
3511 574941 : if (!operand_equal_p (TYPE_SIZE (type0),
3512 574941 : TYPE_SIZE (type1),
3513 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3514 : return false;
3515 :
3516 : /* Fallthru. */
3517 2309177 : case REALPART_EXPR:
3518 2309177 : case VIEW_CONVERT_EXPR:
3519 2309177 : return OP_SAME (0);
3520 :
3521 87154978 : case TARGET_MEM_REF:
3522 87154978 : case MEM_REF:
3523 87154978 : if (!(flags & OEP_ADDRESS_OF))
3524 : {
3525 : /* Require equal access sizes */
3526 17316941 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3527 17316941 : && (!TYPE_SIZE (type0)
3528 1264101 : || !TYPE_SIZE (type1)
3529 1257845 : || !operand_equal_p (TYPE_SIZE (type0),
3530 1257845 : TYPE_SIZE (type1),
3531 : flags)))
3532 : return false;
3533 : /* Verify that access happens in similar types. */
3534 16048232 : if (!types_compatible_p (type0, type1))
3535 : return false;
3536 : /* Verify that accesses are TBAA compatible. */
3537 15710709 : if (!alias_ptr_types_compatible_p
3538 15710709 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3539 15710709 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3540 14810306 : || (MR_DEPENDENCE_CLIQUE (arg0)
3541 14810306 : != MR_DEPENDENCE_CLIQUE (arg1))
3542 28754540 : || (MR_DEPENDENCE_BASE (arg0)
3543 13043831 : != MR_DEPENDENCE_BASE (arg1)))
3544 : return false;
3545 : /* Verify that alignment is compatible. */
3546 12518104 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3547 : return false;
3548 : }
3549 82141772 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3550 140486036 : return (OP_SAME (0) && OP_SAME (1)
3551 : /* TARGET_MEM_REF require equal extra operands. */
3552 107416175 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3553 579794 : || (OP_SAME_WITH_NULL (2)
3554 270908 : && OP_SAME_WITH_NULL (3)
3555 265442 : && OP_SAME_WITH_NULL (4))));
3556 :
3557 36003033 : case ARRAY_REF:
3558 36003033 : case ARRAY_RANGE_REF:
3559 36003033 : if (!OP_SAME (0))
3560 : return false;
3561 31162660 : 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 31162660 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3565 31162660 : TREE_OPERAND (arg1, 1))
3566 28042733 : || OP_SAME (1))
3567 6269624 : && OP_SAME_WITH_NULL (2)
3568 6268288 : && 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 40565760 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3572 3134144 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3573 2646 : || (operand_equal_p (array_ref_low_bound
3574 2646 : (const_cast<tree> (arg0)),
3575 : array_ref_low_bound
3576 2646 : (const_cast<tree> (arg1)),
3577 : flags)
3578 2646 : && operand_equal_p (array_ref_element_size
3579 2646 : (const_cast<tree> (arg0)),
3580 : array_ref_element_size
3581 2646 : (const_cast<tree> (arg1)),
3582 : flags))));
3583 :
3584 747916505 : 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 747916505 : if (!OP_SAME_WITH_NULL (0))
3588 : return false;
3589 58833707 : {
3590 58833707 : 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 58833707 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3596 58833707 : if (!OP_SAME (1))
3597 : {
3598 34061940 : if (compare_address
3599 648553 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3600 : {
3601 648550 : tree field0 = TREE_OPERAND (arg0, 1);
3602 648550 : tree field1 = TREE_OPERAND (arg1, 1);
3603 :
3604 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3605 648550 : if (TREE_CODE (field0) != FIELD_DECL
3606 648550 : || TREE_CODE (field1) != FIELD_DECL)
3607 : return false;
3608 :
3609 648550 : if (!DECL_FIELD_OFFSET (field0)
3610 648550 : || !DECL_FIELD_OFFSET (field1))
3611 3 : return field0 == field1;
3612 :
3613 648547 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3614 648547 : DECL_FIELD_OFFSET (field1), flags)
3615 843699 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3616 195152 : DECL_FIELD_BIT_OFFSET (field1),
3617 : flags))
3618 : return false;
3619 : }
3620 : else
3621 : return false;
3622 : }
3623 : }
3624 24812617 : return OP_SAME_WITH_NULL (2);
3625 :
3626 635154 : case BIT_FIELD_REF:
3627 635154 : if (!OP_SAME (0))
3628 : return false;
3629 368560 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3630 368560 : return OP_SAME (1) && OP_SAME (2);
3631 :
3632 : default:
3633 : return false;
3634 : }
3635 :
3636 59999099 : case tcc_expression:
3637 59999099 : switch (TREE_CODE (arg0))
3638 : {
3639 54620260 : case ADDR_EXPR:
3640 : /* Be sure we pass right ADDRESS_OF flag. */
3641 54620260 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3642 54620260 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3643 54620260 : TREE_OPERAND (arg1, 0),
3644 54620260 : flags | OEP_ADDRESS_OF);
3645 :
3646 613041 : case TRUTH_NOT_EXPR:
3647 613041 : return OP_SAME (0);
3648 :
3649 79677 : case TRUTH_ANDIF_EXPR:
3650 79677 : case TRUTH_ORIF_EXPR:
3651 79677 : 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 47817 : case TRUTH_AND_EXPR:
3661 47817 : case TRUTH_OR_EXPR:
3662 47817 : case TRUTH_XOR_EXPR:
3663 47817 : if (OP_SAME (0) && OP_SAME (1))
3664 : return true;
3665 :
3666 : /* Otherwise take into account this is a commutative operation. */
3667 47799 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3668 47799 : TREE_OPERAND (arg1, 1), flags)
3669 47802 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3670 3 : TREE_OPERAND (arg1, 0), flags));
3671 :
3672 216060 : case COND_EXPR:
3673 216060 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3674 : return false;
3675 170350 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3676 170350 : 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 371 : case VEC_COND_EXPR:
3689 371 : case DOT_PROD_EXPR:
3690 371 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3691 :
3692 38544 : case MODIFY_EXPR:
3693 38544 : case INIT_EXPR:
3694 38544 : case COMPOUND_EXPR:
3695 38544 : case PREDECREMENT_EXPR:
3696 38544 : case PREINCREMENT_EXPR:
3697 38544 : case POSTDECREMENT_EXPR:
3698 38544 : case POSTINCREMENT_EXPR:
3699 38544 : if (flags & OEP_LEXICOGRAPHIC)
3700 165 : return OP_SAME (0) && OP_SAME (1);
3701 : return false;
3702 :
3703 319103 : case CLEANUP_POINT_EXPR:
3704 319103 : case EXPR_STMT:
3705 319103 : case SAVE_EXPR:
3706 319103 : if (flags & OEP_LEXICOGRAPHIC)
3707 208 : return OP_SAME (0);
3708 : return false;
3709 :
3710 81088 : case OBJ_TYPE_REF:
3711 : /* Virtual table reference. */
3712 162176 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3713 81088 : OBJ_TYPE_REF_EXPR (arg1), flags))
3714 : return false;
3715 15856 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3716 15856 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3717 15856 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3718 : return false;
3719 15856 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3720 15856 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3721 : return false;
3722 15856 : if (virtual_method_call_p (arg0))
3723 : {
3724 15856 : if (!virtual_method_call_p (arg1))
3725 : return false;
3726 15856 : return types_same_for_odr (obj_type_ref_class (arg0),
3727 31712 : obj_type_ref_class (arg1));
3728 : }
3729 : return false;
3730 :
3731 661 : case OMP_ARRAY_SECTION:
3732 661 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3733 :
3734 : default:
3735 : return false;
3736 : }
3737 :
3738 3802912 : case tcc_vl_exp:
3739 3802912 : switch (TREE_CODE (arg0))
3740 : {
3741 3802912 : case CALL_EXPR:
3742 3802912 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3743 3802912 : != (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 3802912 : 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 3802898 : 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 2223628 : {
3765 2223628 : unsigned int cef = call_expr_flags (arg0);
3766 2223628 : if (flags & OEP_PURE_SAME)
3767 0 : cef &= ECF_CONST | ECF_PURE;
3768 : else
3769 2223628 : cef &= ECF_CONST;
3770 2223628 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3771 : return false;
3772 : }
3773 :
3774 : /* Now see if all the arguments are the same. */
3775 34591 : {
3776 34591 : const_call_expr_arg_iterator iter0, iter1;
3777 34591 : const_tree a0, a1;
3778 69182 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3779 34591 : a1 = first_const_call_expr_arg (arg1, &iter1);
3780 42716 : a0 && a1;
3781 8125 : a0 = next_const_call_expr_arg (&iter0),
3782 8125 : a1 = next_const_call_expr_arg (&iter1))
3783 36090 : 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 172310613 : case tcc_declaration:
3795 : /* Consider __builtin_sqrt equal to sqrt. */
3796 172310613 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3797 7039397 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3798 267608 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3799 6433979 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3800 267608 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3801 :
3802 165876634 : if (DECL_P (arg0)
3803 165876634 : && (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 35 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3814 : }
3815 : return false;
3816 :
3817 356067691 : case tcc_exceptional:
3818 356067691 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3819 : {
3820 19763 : 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 19763 : 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 1043 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3836 2086 : TYPE_VECTOR_SUBPARTS (type1)))
3837 : return false;
3838 :
3839 1043 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3840 1043 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3841 1043 : unsigned int len = vec_safe_length (v0);
3842 :
3843 2086 : if (len != vec_safe_length (v1))
3844 : return false;
3845 :
3846 4609 : for (unsigned int i = 0; i < len; i++)
3847 : {
3848 3937 : constructor_elt *c0 = &(*v0)[i];
3849 3937 : constructor_elt *c1 = &(*v1)[i];
3850 :
3851 3937 : 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 3596 : || (c0->index
3856 2684 : && (TREE_CODE (c0->index) != INTEGER_CST
3857 2684 : || compare_tree_int (c0->index, i)))
3858 7533 : || (c1->index
3859 2684 : && (TREE_CODE (c1->index) != INTEGER_CST
3860 2684 : || compare_tree_int (c1->index, i))))
3861 : return false;
3862 : }
3863 : return true;
3864 : }
3865 356047928 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3866 3301 : && (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 2639048 : case tcc_statement:
3889 2639048 : 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 3060166686 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3916 : unsigned int flags)
3917 : {
3918 3060166686 : int i;
3919 3060166686 : enum tree_code code;
3920 3060166686 : enum tree_code_class tclass;
3921 :
3922 3060166686 : if (t == NULL_TREE || t == error_mark_node)
3923 : {
3924 78426059 : hstate.merge_hash (0);
3925 78426059 : return;
3926 : }
3927 :
3928 2981740627 : STRIP_ANY_LOCATION_WRAPPER (t);
3929 :
3930 2981740627 : if (!(flags & OEP_ADDRESS_OF))
3931 2723775863 : STRIP_NOPS (t);
3932 :
3933 2981740627 : code = TREE_CODE (t);
3934 :
3935 2981740627 : 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 893114630 : case INTEGER_CST:
3943 893114630 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3944 1805036978 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3945 911922348 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3946 : return;
3947 15766151 : case REAL_CST:
3948 15766151 : {
3949 15766151 : unsigned int val2;
3950 15766151 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3951 : val2 = rvc_zero;
3952 : else
3953 15545483 : val2 = real_hash (TREE_REAL_CST_PTR (t));
3954 15766151 : hstate.merge_hash (val2);
3955 15766151 : 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 12795142 : case STRING_CST:
3964 12795142 : hstate.add ((const void *) TREE_STRING_POINTER (t),
3965 12795142 : TREE_STRING_LENGTH (t));
3966 12795142 : 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 210612 : case COMPLEX_CST:
3972 210612 : hash_operand (TREE_REALPART (t), hstate, flags);
3973 210612 : hash_operand (TREE_IMAGPART (t), hstate, flags);
3974 210612 : return;
3975 3453134 : case VECTOR_CST:
3976 3453134 : {
3977 3453134 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
3978 3453134 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3979 3453134 : unsigned int count = vector_cst_encoded_nelts (t);
3980 14236358 : for (unsigned int i = 0; i < count; ++i)
3981 7330090 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3982 : return;
3983 : }
3984 899542467 : case SSA_NAME:
3985 : /* We can just compare by pointer. */
3986 899542467 : hstate.add_hwi (SSA_NAME_VERSION (t));
3987 899542467 : 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 285806 : for (; t; t = TREE_CHAIN (t))
4001 142903 : hash_operand (TREE_VALUE (t), hstate, flags);
4002 : return;
4003 4960438 : case CONSTRUCTOR:
4004 4960438 : {
4005 4960438 : unsigned HOST_WIDE_INT idx;
4006 4960438 : tree field, value;
4007 4960438 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4008 4960438 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4009 19944679 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4010 : {
4011 : /* In GIMPLE the indexes can be either NULL or matching i. */
4012 14984241 : if (field == NULL_TREE)
4013 1112768 : field = bitsize_int (idx);
4014 14984241 : if (TREE_CODE (field) == FIELD_DECL)
4015 : {
4016 9971698 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4017 9971698 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4018 : }
4019 : else
4020 5012543 : hash_operand (field, hstate, flags);
4021 14984241 : 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 21518289 : 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 21518289 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4047 21518289 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4048 : {
4049 7219045 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4050 7219045 : code = TREE_CODE (t);
4051 : }
4052 : /* FALL THROUGH */
4053 1151753725 : default:
4054 1151753725 : 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 1151753725 : tclass = TREE_CODE_CLASS (code);
4061 :
4062 1151753725 : if (tclass == tcc_declaration)
4063 : {
4064 : /* DECL's have a unique ID */
4065 809864565 : hstate.add_hwi (DECL_UID (t));
4066 : }
4067 341889160 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4068 : {
4069 : /* For comparisons that can be swapped, use the lower
4070 : tree code. */
4071 143421 : enum tree_code ccode = swap_tree_comparison (code);
4072 143421 : if (code < ccode)
4073 61526 : ccode = code;
4074 143421 : hstate.add_object (ccode);
4075 143421 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4076 143421 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4077 : }
4078 341745739 : else if (CONVERT_EXPR_CODE_P (code))
4079 : {
4080 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4081 : operand_equal_p. */
4082 6680972 : enum tree_code ccode = NOP_EXPR;
4083 6680972 : 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 6680972 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4090 6680972 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4091 : }
4092 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4093 335064767 : else if (code == MEM_REF
4094 80670690 : && (flags & OEP_ADDRESS_OF) != 0
4095 70959318 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4096 14380203 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4097 349228330 : && integer_zerop (TREE_OPERAND (t, 1)))
4098 6268850 : 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 328795917 : else if (!IS_EXPR_CODE_CLASS (tclass))
4103 384 : gcc_assert (flags & OEP_HASH_CHECK);
4104 : else
4105 : {
4106 328795533 : unsigned int sflags = flags;
4107 :
4108 328795533 : hstate.add_object (code);
4109 :
4110 328795533 : switch (code)
4111 : {
4112 131867976 : case ADDR_EXPR:
4113 131867976 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4114 131867976 : flags |= OEP_ADDRESS_OF;
4115 131867976 : sflags = flags;
4116 131867976 : break;
4117 :
4118 79344777 : case INDIRECT_REF:
4119 79344777 : case MEM_REF:
4120 79344777 : case TARGET_MEM_REF:
4121 79344777 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4122 79344777 : sflags = flags;
4123 79344777 : break;
4124 :
4125 77010920 : case COMPONENT_REF:
4126 77010920 : if (sflags & OEP_ADDRESS_OF)
4127 : {
4128 38922855 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4129 38922855 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4130 : hstate, flags & ~OEP_ADDRESS_OF);
4131 38922855 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4132 : hstate, flags & ~OEP_ADDRESS_OF);
4133 38922855 : return;
4134 : }
4135 : break;
4136 15780033 : case ARRAY_REF:
4137 15780033 : case ARRAY_RANGE_REF:
4138 15780033 : case BIT_FIELD_REF:
4139 15780033 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4140 15780033 : break;
4141 :
4142 8445 : case COND_EXPR:
4143 8445 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4144 8445 : 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 43502 : case CALL_EXPR:
4159 43502 : 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 293506 : case OBJ_TYPE_REF:
4171 : /* Virtual table reference. */
4172 293506 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4173 293506 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4174 293506 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4175 293506 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4176 293506 : if (!virtual_method_call_p (t))
4177 : return;
4178 293485 : if (tree c = obj_type_ref_class (t))
4179 : {
4180 293485 : 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 293485 : if (TREE_CODE (c) == TYPE_DECL
4184 293485 : && DECL_ASSEMBLER_NAME_SET_P (c))
4185 7309 : hstate.add_object
4186 7309 : (IDENTIFIER_HASH_VALUE
4187 : (DECL_ASSEMBLER_NAME (c)));
4188 : }
4189 : 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 289579100 : 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 289579100 : 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 18095179 : inchash::hash one, two;
4211 18095179 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4212 18095179 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4213 18095179 : hstate.add_commutative (one, two);
4214 : }
4215 : else
4216 757651235 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4217 700850963 : hash_operand (TREE_OPERAND (t, i), hstate,
4218 : i == 0 ? flags : sflags);
4219 : }
4220 : return;
4221 : }
4222 : }
4223 :
4224 : bool
4225 7357577486 : 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 7357577486 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4232 : {
4233 3098500468 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4234 : {
4235 477023061 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4236 : {
4237 85131088 : inchash::hash hstate0 (0), hstate1 (0);
4238 85131088 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4239 85131088 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4240 85131088 : hashval_t h0 = hstate0.end ();
4241 85131088 : hashval_t h1 = hstate1.end ();
4242 85131088 : gcc_assert (h0 == h1);
4243 : }
4244 477023061 : *ret = true;
4245 : }
4246 : else
4247 2621477407 : *ret = false;
4248 :
4249 : 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 3096641347 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4263 : {
4264 3096641347 : 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 2186162677 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4277 : {
4278 2186162677 : default_compare_instance.hash_operand (t, hstate, flags);
4279 2186162677 : }
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 20508628 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4288 : {
4289 20508628 : if (operand_equal_p (arg0, arg1, 0))
4290 : return true;
4291 :
4292 39236376 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4293 33498151 : || ! 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 6221029 : tree op0 = arg0;
4300 6221029 : tree op1 = arg1;
4301 6221029 : STRIP_NOPS (op0);
4302 6221029 : STRIP_NOPS (op1);
4303 6221029 : 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 5141134 : if (CONVERT_EXPR_P (arg1)
4309 901467 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4310 901419 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4311 901419 : < TYPE_PRECISION (TREE_TYPE (arg1))
4312 6349092 : && 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 60783557 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4329 : {
4330 64823713 : enum tree_code code = TREE_CODE (arg);
4331 64823713 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4332 :
4333 : /* We can handle some of the tcc_expression cases here. */
4334 64823713 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4335 : tclass = tcc_unary;
4336 64206207 : else if (tclass == tcc_expression
4337 699332 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4338 699332 : || code == COMPOUND_EXPR))
4339 : tclass = tcc_binary;
4340 :
4341 64195298 : switch (tclass)
4342 : {
4343 4040156 : case tcc_unary:
4344 4040156 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4345 :
4346 5434944 : case tcc_binary:
4347 5434944 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4348 5434944 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4349 :
4350 : case tcc_constant:
4351 : return true;
4352 :
4353 688423 : case tcc_expression:
4354 688423 : if (code == COND_EXPR)
4355 694 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4356 694 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4357 758 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4358 : return false;
4359 :
4360 622252 : 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 622252 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4368 622252 : TREE_OPERAND (arg, 1), 0))
4369 : return false;
4370 :
4371 622252 : if (*cval1 == 0)
4372 620070 : *cval1 = TREE_OPERAND (arg, 0);
4373 2182 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4374 : ;
4375 2063 : else if (*cval2 == 0)
4376 0 : *cval2 = TREE_OPERAND (arg, 0);
4377 2063 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4378 : ;
4379 : else
4380 : return false;
4381 :
4382 620189 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4383 : ;
4384 620189 : else if (*cval2 == 0)
4385 620070 : *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 663 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4405 : tree old1, tree new1)
4406 : {
4407 663 : tree type = TREE_TYPE (arg);
4408 663 : enum tree_code code = TREE_CODE (arg);
4409 663 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4410 :
4411 : /* We can handle some of the tcc_expression cases here. */
4412 663 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4413 : tclass = tcc_unary;
4414 663 : else if (tclass == tcc_expression
4415 30 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4416 : tclass = tcc_binary;
4417 :
4418 642 : 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 153 : case tcc_binary:
4426 306 : return fold_build2_loc (loc, code, type,
4427 153 : eval_subst (loc, TREE_OPERAND (arg, 0),
4428 : old0, new0, old1, new1),
4429 153 : eval_subst (loc, TREE_OPERAND (arg, 1),
4430 153 : 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 168 : case tcc_comparison:
4457 168 : {
4458 168 : tree arg0 = TREE_OPERAND (arg, 0);
4459 168 : 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 168 : 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 168 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4471 : arg1 = new0;
4472 168 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4473 : arg1 = new1;
4474 :
4475 168 : 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 323920 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4492 : {
4493 323920 : 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 323920 : 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 323920 : if (TREE_SIDE_EFFECTS (omitted))
4502 12048 : return build2_loc (loc, COMPOUND_EXPR, type,
4503 12048 : fold_ignored_result (omitted), t);
4504 :
4505 311872 : 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 5655 : omit_two_operands_loc (location_t loc, tree type, tree result,
4519 : tree omitted1, tree omitted2)
4520 : {
4521 5655 : tree t = fold_convert_loc (loc, type, result);
4522 :
4523 5655 : if (TREE_SIDE_EFFECTS (omitted2))
4524 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4525 5655 : if (TREE_SIDE_EFFECTS (omitted1))
4526 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4527 :
4528 5655 : 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 51293229 : fold_truth_not_expr (location_t loc, tree arg)
4541 : {
4542 51293229 : tree type = TREE_TYPE (arg);
4543 51293229 : enum tree_code code = TREE_CODE (arg);
4544 51293229 : 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 51293229 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4551 : {
4552 39181826 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4553 32520926 : if (FLOAT_TYPE_P (op_type)
4554 6670987 : && flag_trapping_math
4555 6639906 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4556 45781641 : && code != NE_EXPR && code != EQ_EXPR)
4557 : return NULL_TREE;
4558 :
4559 33279183 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4560 33279183 : if (code == ERROR_MARK)
4561 : return NULL_TREE;
4562 :
4563 33279183 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4564 33279183 : TREE_OPERAND (arg, 1));
4565 33279183 : copy_warning (ret, arg);
4566 33279183 : return ret;
4567 : }
4568 :
4569 12111403 : switch (code)
4570 : {
4571 0 : case INTEGER_CST:
4572 0 : return constant_boolean_node (integer_zerop (arg), type);
4573 :
4574 51504 : case TRUTH_AND_EXPR:
4575 51504 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4576 51504 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4577 103008 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4578 51504 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4579 103008 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4580 :
4581 2562 : case TRUTH_OR_EXPR:
4582 2562 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4583 2562 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4584 5124 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4585 2562 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4586 5124 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4587 :
4588 73337 : 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 73337 : 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 73149 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4599 73149 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4600 146298 : TREE_OPERAND (arg, 1));
4601 :
4602 397854 : case TRUTH_ANDIF_EXPR:
4603 397854 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4604 397854 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4605 795708 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4606 397854 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4607 795708 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4608 :
4609 16918 : case TRUTH_ORIF_EXPR:
4610 16918 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4611 16918 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4612 33836 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4613 16918 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4614 33836 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4615 :
4616 786035 : case TRUTH_NOT_EXPR:
4617 786035 : return TREE_OPERAND (arg, 0);
4618 :
4619 9762 : case COND_EXPR:
4620 9762 : {
4621 9762 : tree arg1 = TREE_OPERAND (arg, 1);
4622 9762 : tree arg2 = TREE_OPERAND (arg, 2);
4623 :
4624 9762 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4625 9762 : 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 9762 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4631 9762 : VOID_TYPE_P (TREE_TYPE (arg1))
4632 9762 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4633 9762 : VOID_TYPE_P (TREE_TYPE (arg2))
4634 19521 : ? 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 75204 : CASE_CONVERT:
4648 75204 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4649 75140 : 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 508 : case BIT_AND_EXPR:
4659 508 : 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 2104189 : fold_invert_truthvalue (location_t loc, tree arg)
4683 : {
4684 2104189 : tree type = TREE_TYPE (arg);
4685 4208354 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4686 : ? BIT_NOT_EXPR
4687 : : TRUTH_NOT_EXPR,
4688 2104189 : 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 43624730 : invert_truthvalue_loc (location_t loc, tree arg)
4697 : {
4698 43624730 : if (TREE_CODE (arg) == ERROR_MARK)
4699 : return arg;
4700 :
4701 43624730 : tree type = TREE_TYPE (arg);
4702 87249460 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4703 : ? BIT_NOT_EXPR
4704 : : TRUTH_NOT_EXPR,
4705 43624730 : 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 842752 : 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 842752 : tree result, bftype;
4720 :
4721 : /* Attempt not to lose the access path if possible. */
4722 842752 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4723 : {
4724 838978 : tree ninner = TREE_OPERAND (orig_inner, 0);
4725 838978 : machine_mode nmode;
4726 838978 : poly_int64 nbitsize, nbitpos;
4727 838978 : tree noffset;
4728 838978 : int nunsignedp, nreversep, nvolatilep = 0;
4729 838978 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4730 : &noffset, &nmode, &nunsignedp,
4731 : &nreversep, &nvolatilep);
4732 838978 : if (base == inner
4733 838844 : && noffset == NULL_TREE
4734 838844 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4735 838837 : && !reversep
4736 838765 : && !nreversep
4737 1677743 : && !nvolatilep)
4738 : {
4739 838765 : inner = ninner;
4740 838978 : bitpos -= nbitpos;
4741 : }
4742 : }
4743 :
4744 842752 : alias_set_type iset = get_alias_set (orig_inner);
4745 842752 : 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 842752 : if (known_eq (bitpos, 0) && !reversep)
4751 : {
4752 12749 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4753 25498 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4754 12587 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4755 166 : && tree_fits_shwi_p (size)
4756 12915 : && tree_to_shwi (size) == bitsize)
4757 143 : return fold_convert_loc (loc, type, inner);
4758 : }
4759 :
4760 842609 : bftype = type;
4761 842609 : if (TYPE_PRECISION (bftype) != bitsize
4762 842609 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4763 501 : bftype = build_nonstandard_integer_type (bitsize, 0);
4764 :
4765 842609 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4766 842609 : bitsize_int (bitsize), bitsize_int (bitpos));
4767 842609 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4768 :
4769 842609 : 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 4733936 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4797 : tree compare_type, tree lhs, tree rhs)
4798 : {
4799 4733936 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4800 4733936 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4801 4733936 : tree type = TREE_TYPE (lhs);
4802 4733936 : tree unsigned_type;
4803 4733936 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4804 4733936 : machine_mode lmode, rmode;
4805 4733936 : scalar_int_mode nmode;
4806 4733936 : int lunsignedp, runsignedp;
4807 4733936 : int lreversep, rreversep;
4808 4733936 : int lvolatilep = 0, rvolatilep = 0;
4809 4733936 : tree linner, rinner = NULL_TREE;
4810 4733936 : tree mask;
4811 4733936 : 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 4733936 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4819 : &lunsignedp, &lreversep, &lvolatilep);
4820 4733936 : if (linner == lhs
4821 4733936 : || !known_size_p (plbitsize)
4822 4733936 : || !plbitsize.is_constant (&lbitsize)
4823 4733936 : || !plbitpos.is_constant (&lbitpos)
4824 9467872 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4825 802819 : || offset != 0
4826 802794 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4827 5536730 : || lvolatilep)
4828 : return 0;
4829 :
4830 802734 : if (const_p)
4831 763642 : 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 39092 : rinner
4837 39092 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4838 : &runsignedp, &rreversep, &rvolatilep);
4839 :
4840 39092 : if (rinner == rhs
4841 39048 : || maybe_ne (lbitpos, rbitpos)
4842 39014 : || maybe_ne (lbitsize, rbitsize)
4843 39014 : || lunsignedp != runsignedp
4844 39014 : || lreversep != rreversep
4845 39014 : || offset != 0
4846 39014 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4847 78106 : || rvolatilep)
4848 : return 0;
4849 : }
4850 :
4851 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4852 802656 : poly_uint64 bitstart = 0;
4853 802656 : poly_uint64 bitend = 0;
4854 802656 : if (TREE_CODE (lhs) == COMPONENT_REF)
4855 : {
4856 802656 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4857 802656 : 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 1605312 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4864 763642 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4865 39014 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4866 : TYPE_ALIGN (TREE_TYPE (rinner))),
4867 802656 : 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 800653 : 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 800653 : nbitsize = GET_MODE_BITSIZE (nmode);
4878 800653 : nbitpos = lbitpos & ~ (nbitsize - 1);
4879 800653 : lbitpos -= nbitpos;
4880 800653 : if (nbitsize == lbitsize)
4881 : return 0;
4882 :
4883 800653 : 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 800653 : mask = build_int_cst_type (unsigned_type, -1);
4888 800653 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4889 800653 : mask = const_binop (RSHIFT_EXPR, mask,
4890 800653 : size_int (nbitsize - lbitsize - lbitpos));
4891 :
4892 800653 : if (! const_p)
4893 : {
4894 37459 : if (nbitpos < 0)
4895 : return 0;
4896 :
4897 : /* If not comparing with constant, just rework the comparison
4898 : and return. */
4899 37459 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4900 37459 : nbitsize, nbitpos, 1, lreversep);
4901 37459 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4902 37459 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4903 37459 : nbitsize, nbitpos, 1, rreversep);
4904 37459 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4905 37459 : 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 763194 : if (lunsignedp)
4918 : {
4919 762029 : 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 763194 : if (nbitpos < 0)
4938 : return 0;
4939 :
4940 : /* Single-bit compares should always be against zero. */
4941 763194 : 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 763194 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4951 763194 : nbitsize, nbitpos, 1, lreversep);
4952 :
4953 763194 : rhs = const_binop (BIT_AND_EXPR,
4954 : const_binop (LSHIFT_EXPR,
4955 : fold_convert_loc (loc, unsigned_type, rhs),
4956 763194 : size_int (lbitpos)),
4957 : mask);
4958 :
4959 763194 : lhs = build2_loc (loc, code, compare_type,
4960 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4961 763194 : 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 2210 : sign_bit_p (tree exp, const_tree val)
4972 : {
4973 2210 : int width;
4974 2210 : tree t;
4975 :
4976 : /* Tree EXP must have an integral type. */
4977 2210 : t = TREE_TYPE (exp);
4978 2210 : if (! INTEGRAL_TYPE_P (t))
4979 : return NULL_TREE;
4980 :
4981 : /* Tree VAL must be an integer constant. */
4982 1864 : if (TREE_CODE (val) != INTEGER_CST
4983 1864 : || TREE_OVERFLOW (val))
4984 : return NULL_TREE;
4985 :
4986 1502 : width = TYPE_PRECISION (t);
4987 1502 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
4988 : return exp;
4989 :
4990 : /* Handle extension from a narrower type. */
4991 865 : if (TREE_CODE (exp) == NOP_EXPR
4992 865 : && 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 66138862 : simple_operand_p (const_tree exp)
5003 : {
5004 : /* Strip any conversions that don't change the machine mode. */
5005 66138862 : STRIP_NOPS (exp);
5006 :
5007 66138862 : return (CONSTANT_CLASS_P (exp)
5008 45957455 : || TREE_CODE (exp) == SSA_NAME
5009 82453749 : || (DECL_P (exp)
5010 5673746 : && ! TREE_ADDRESSABLE (exp)
5011 5586010 : && ! TREE_THIS_VOLATILE (exp)
5012 5586010 : && ! 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 5584357 : && ! TREE_PUBLIC (exp)
5017 5563640 : && ! DECL_EXTERNAL (exp)
5018 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5019 5563640 : && ! ((VAR_P (exp)
5020 : || TREE_CODE (exp) == PARM_DECL
5021 : || TREE_CODE (exp) == RESULT_DECL)
5022 5563640 : && 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 5563051 : && (! 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 5563051 : && (! 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 7162312 : simple_condition_p (tree exp)
5038 : {
5039 7256979 : enum tree_code code;
5040 :
5041 7256979 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5042 : return false;
5043 :
5044 2330292 : while (CONVERT_EXPR_P (exp))
5045 35699 : exp = TREE_OPERAND (exp, 0);
5046 :
5047 2294593 : code = TREE_CODE (exp);
5048 :
5049 2294593 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5050 1792588 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5051 1792588 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5052 :
5053 502005 : if (code == TRUTH_NOT_EXPR)
5054 94667 : return simple_condition_p (TREE_OPERAND (exp, 0));
5055 :
5056 407338 : 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 23441168 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5097 : tree arg1, int upper1_p)
5098 : {
5099 23441168 : tree tem;
5100 23441168 : int result;
5101 23441168 : 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 23441168 : if (arg0 != 0 && arg1 != 0)
5109 : {
5110 12275828 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5111 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5112 12275828 : STRIP_NOPS (tem);
5113 12275828 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5114 : }
5115 :
5116 11165340 : 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 11156208 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5126 11156208 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5127 11156208 : switch (code)
5128 : {
5129 5264063 : case EQ_EXPR:
5130 5264063 : result = sgn0 == sgn1;
5131 5264063 : break;
5132 0 : case NE_EXPR:
5133 0 : result = sgn0 != sgn1;
5134 0 : break;
5135 363773 : case LT_EXPR:
5136 363773 : result = sgn0 < sgn1;
5137 363773 : break;
5138 2564898 : case LE_EXPR:
5139 2564898 : result = sgn0 <= sgn1;
5140 2564898 : break;
5141 2963474 : case GT_EXPR:
5142 2963474 : result = sgn0 > sgn1;
5143 2963474 : break;
5144 0 : case GE_EXPR:
5145 0 : result = sgn0 >= sgn1;
5146 0 : break;
5147 0 : default:
5148 0 : gcc_unreachable ();
5149 : }
5150 :
5151 11156208 : 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 61337147 : 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 61337147 : tree arg0_type = TREE_TYPE (arg0);
5163 61337147 : tree n_low, n_high, low = *p_low, high = *p_high;
5164 61337147 : int in_p = *p_in_p, n_in_p;
5165 :
5166 61337147 : switch (code)
5167 : {
5168 1736799 : case TRUTH_NOT_EXPR:
5169 : /* We can only do something if the range is testing for zero. */
5170 1736799 : if (low == NULL_TREE || high == NULL_TREE
5171 1736799 : || ! integer_zerop (low) || ! integer_zerop (high))
5172 : return NULL_TREE;
5173 1736799 : *p_in_p = ! in_p;
5174 1736799 : return arg0;
5175 :
5176 47520737 : case EQ_EXPR: case NE_EXPR:
5177 47520737 : 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 47520737 : if (low == NULL_TREE || high == NULL_TREE
5184 47520737 : || ! integer_zerop (low) || ! integer_zerop (high)
5185 95041386 : || TREE_CODE (arg1) != INTEGER_CST)
5186 : return NULL_TREE;
5187 :
5188 30044946 : switch (code)
5189 : {
5190 : case NE_EXPR: /* - [c, c] */
5191 : low = high = arg1;
5192 : break;
5193 8228985 : case EQ_EXPR: /* + [c, c] */
5194 8228985 : in_p = ! in_p, low = high = arg1;
5195 8228985 : break;
5196 2241095 : case GT_EXPR: /* - [-, c] */
5197 2241095 : low = 0, high = arg1;
5198 2241095 : break;
5199 749720 : case GE_EXPR: /* + [c, -] */
5200 749720 : in_p = ! in_p, low = arg1, high = 0;
5201 749720 : break;
5202 5748696 : case LT_EXPR: /* - [c, -] */
5203 5748696 : low = arg1, high = 0;
5204 5748696 : break;
5205 4417918 : case LE_EXPR: /* + [-, c] */
5206 4417918 : in_p = ! in_p, low = 0, high = arg1;
5207 4417918 : break;
5208 : default:
5209 : 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 30044946 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5218 : {
5219 1962791 : 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 1962782 : 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 1962782 : if (high == 0 && low && ! integer_zerop (low))
5231 : {
5232 873352 : in_p = ! in_p;
5233 873352 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5234 873352 : build_int_cst (TREE_TYPE (low), 1), 0);
5235 873352 : low = build_int_cst (arg0_type, 0);
5236 : }
5237 : }
5238 :
5239 30044937 : *p_low = low;
5240 30044937 : *p_high = high;
5241 30044937 : *p_in_p = in_p;
5242 30044937 : return arg0;
5243 :
5244 329 : 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 329 : if (!TYPE_UNSIGNED (arg0_type)
5248 329 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5249 : {
5250 101 : if (low == NULL_TREE)
5251 18 : low = TYPE_MIN_VALUE (arg0_type);
5252 101 : if (high == NULL_TREE)
5253 47 : high = TYPE_MAX_VALUE (arg0_type);
5254 : }
5255 :
5256 : /* (-x) IN [a,b] -> x in [-b, -a] */
5257 329 : n_low = range_binop (MINUS_EXPR, exp_type,
5258 : build_int_cst (exp_type, 0),
5259 : 0, high, 1);
5260 329 : n_high = range_binop (MINUS_EXPR, exp_type,
5261 : build_int_cst (exp_type, 0),
5262 : 0, low, 0);
5263 329 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5264 : return NULL_TREE;
5265 311 : 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 878052 : case PLUS_EXPR:
5273 878052 : case MINUS_EXPR:
5274 878052 : 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 670568 : if (!TYPE_UNSIGNED (arg0_type)
5280 670568 : && !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 958570 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5288 : arg0_type, low, 0, arg1, 0);
5289 480764 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5290 : arg0_type, high, 1, arg1, 0);
5291 477199 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5292 957951 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5293 : return NULL_TREE;
5294 :
5295 481063 : 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 481063 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5299 : {
5300 131294 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5301 131294 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5302 131294 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5303 131294 : 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 131294 : if (tree_int_cst_equal (n_low, low)
5310 131294 : && tree_int_cst_equal (n_high, high))
5311 : low = high = 0;
5312 : else
5313 131294 : in_p = ! in_p;
5314 : }
5315 : else
5316 349769 : low = n_low, high = n_high;
5317 :
5318 481063 : *p_low = low;
5319 481063 : *p_high = high;
5320 481063 : *p_in_p = in_p;
5321 481063 : return arg0;
5322 :
5323 2593031 : CASE_CONVERT:
5324 2593031 : case NON_LVALUE_EXPR:
5325 2593031 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5326 : return NULL_TREE;
5327 :
5328 1188174 : if (! INTEGRAL_TYPE_P (arg0_type)
5329 1147801 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5330 1045280 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5331 : return NULL_TREE;
5332 :
5333 1027135 : n_low = low, n_high = high;
5334 :
5335 1027135 : if (n_low != 0)
5336 857139 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5337 :
5338 1027135 : if (n_high != 0)
5339 962509 : 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 1027135 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5351 : {
5352 259975 : tree high_positive;
5353 259975 : tree equiv_type;
5354 : /* For fixed-point modes, we need to pass the saturating flag
5355 : as the 2nd parameter. */
5356 259975 : 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 259975 : else if (BITINT_TYPE_P (arg0_type))
5361 : equiv_type = arg0_type;
5362 : else
5363 259960 : equiv_type
5364 259960 : = 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 259975 : high_positive
5370 259975 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5371 0 : : TYPE_MAX_VALUE (arg0_type);
5372 :
5373 259975 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5374 238809 : 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 259975 : if (low != 0)
5383 : {
5384 95474 : 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 95474 : 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 164501 : 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 164501 : 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 767160 : else if (TYPE_UNSIGNED (exp_type)
5417 749003 : && !TYPE_UNSIGNED (arg0_type)
5418 388242 : && low
5419 1155402 : && !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 1027135 : *p_low = n_low;
5433 1027135 : *p_high = n_high;
5434 1027135 : *p_in_p = in_p;
5435 1027135 : 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 50566844 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5450 : {
5451 50566844 : enum tree_code code;
5452 50566844 : tree arg0, arg1 = NULL_TREE;
5453 50566844 : tree exp_type, nexp;
5454 50566844 : int in_p;
5455 50566844 : tree low, high;
5456 50566844 : 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 50566844 : in_p = 0;
5465 50566844 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5466 :
5467 80771962 : while (1)
5468 : {
5469 80771962 : code = TREE_CODE (exp);
5470 80771962 : exp_type = TREE_TYPE (exp);
5471 80771962 : arg0 = NULL_TREE;
5472 :
5473 80771962 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5474 : {
5475 56508824 : if (TREE_OPERAND_LENGTH (exp) > 0)
5476 56508824 : arg0 = TREE_OPERAND (exp, 0);
5477 56508824 : if (TREE_CODE_CLASS (code) == tcc_binary
5478 53235108 : || TREE_CODE_CLASS (code) == tcc_comparison
5479 66306345 : || (TREE_CODE_CLASS (code) == tcc_expression
5480 2930020 : && TREE_OPERAND_LENGTH (exp) > 1))
5481 47887891 : arg1 = TREE_OPERAND (exp, 1);
5482 : }
5483 56508824 : if (arg0 == NULL_TREE)
5484 : break;
5485 :
5486 56508810 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5487 : &high, &in_p);
5488 56508810 : 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 50566844 : if (TREE_CODE (exp) == INTEGER_CST)
5495 : {
5496 31688 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5497 : exp, 0, low, 0))
5498 31688 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5499 : exp, 1, high, 1)));
5500 31688 : low = high = 0;
5501 31688 : exp = 0;
5502 : }
5503 :
5504 50566844 : *pin_p = in_p, *plow = low, *phigh = high;
5505 50566844 : 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 592815 : 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 592815 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5551 0 : etype = TREE_TYPE (etype);
5552 592815 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5553 61245 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5554 :
5555 592815 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5556 : {
5557 408056 : 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 408056 : utype = unsigned_type_for (etype);
5562 408056 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5563 408056 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5564 408056 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5565 408056 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5566 :
5567 408056 : 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 184759 : 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 1623144 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5589 : tree low, tree high)
5590 : {
5591 2832836 : 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 2832836 : if (targetm.have_canonicalize_funcptr_for_compare ()
5596 0 : && POINTER_TYPE_P (etype)
5597 2832836 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5598 : return NULL_TREE;
5599 :
5600 2832836 : if (! in_p)
5601 : {
5602 314397 : value = build_range_check (loc, type, exp, 1, low, high);
5603 314397 : if (value != 0)
5604 314397 : return invert_truthvalue_loc (loc, value);
5605 :
5606 : return 0;
5607 : }
5608 :
5609 2518439 : if (low == 0 && high == 0)
5610 124720 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5611 :
5612 2393719 : if (low == 0)
5613 787902 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5614 787902 : fold_convert_loc (loc, etype, high));
5615 :
5616 1605817 : if (high == 0)
5617 73492 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5618 73492 : fold_convert_loc (loc, etype, low));
5619 :
5620 1532325 : if (operand_equal_p (low, high, 0))
5621 322393 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5622 322393 : fold_convert_loc (loc, etype, low));
5623 :
5624 1209932 : if (TREE_CODE (exp) == BIT_AND_EXPR
5625 1209932 : && 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 1209846 : if (integer_zerop (low))
5632 : {
5633 707024 : if (! TYPE_UNSIGNED (etype))
5634 : {
5635 186246 : etype = unsigned_type_for (etype);
5636 186246 : high = fold_convert_loc (loc, etype, high);
5637 186246 : exp = fold_convert_loc (loc, etype, exp);
5638 : }
5639 : return build_range_check (loc, type, exp, 1, 0, high);
5640 : }
5641 :
5642 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5643 502822 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5644 : {
5645 129072 : int prec = TYPE_PRECISION (etype);
5646 :
5647 129072 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5648 : {
5649 124 : if (TYPE_UNSIGNED (etype))
5650 : {
5651 118 : tree signed_etype = signed_type_for (etype);
5652 118 : 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 118 : exp = fold_convert_loc (loc, etype, exp);
5658 : }
5659 124 : 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 502698 : etype = range_check_type (etype);
5667 502698 : if (etype == NULL_TREE)
5668 : return NULL_TREE;
5669 :
5670 502668 : high = fold_convert_loc (loc, etype, high);
5671 502668 : low = fold_convert_loc (loc, etype, low);
5672 502668 : exp = fold_convert_loc (loc, etype, exp);
5673 :
5674 502668 : value = const_binop (MINUS_EXPR, high, low);
5675 :
5676 502668 : if (value != 0 && !TREE_OVERFLOW (value))
5677 502668 : 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 176187 : range_predecessor (tree val)
5688 : {
5689 176187 : tree type = TREE_TYPE (val);
5690 :
5691 176187 : if (INTEGRAL_TYPE_P (type)
5692 176187 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5693 : return 0;
5694 : else
5695 176187 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5696 176187 : 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 1681852 : range_successor (tree val)
5703 : {
5704 1681852 : tree type = TREE_TYPE (val);
5705 :
5706 1681852 : if (INTEGRAL_TYPE_P (type)
5707 1681852 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5708 : return 0;
5709 : else
5710 1681843 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5711 1681843 : 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 3683089 : 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 3683089 : bool no_overlap;
5722 3683089 : int subset;
5723 3683089 : int temp;
5724 3683089 : tree tem;
5725 3683089 : int in_p;
5726 3683089 : tree low, high;
5727 3683089 : int lowequal = ((low0 == 0 && low1 == 0)
5728 3683089 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5729 3683089 : low0, 0, low1, 0)));
5730 3683089 : int highequal = ((high0 == 0 && high1 == 0)
5731 3683089 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5732 3683089 : 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 3683089 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5737 : low0, 0, low1, 0))
5738 3683089 : || (lowequal
5739 577863 : && 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 3683089 : if (low1
5750 3683089 : && high1
5751 1010917 : && TREE_CODE (low1) == INTEGER_CST
5752 1010917 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5753 128195 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5754 171826 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5755 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5756 4651724 : && operand_equal_p (low1, high1, 0))
5757 : {
5758 563312 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5759 563312 : && 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 451985 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5765 451985 : && 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 3476187 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5774 : high0, 1, low1, 0));
5775 3476187 : 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 3476187 : 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 1592168 : if (no_overlap)
5786 : in_p = 0, low = high = 0;
5787 1590086 : else if (subset)
5788 : in_p = 1, low = low1, high = high1;
5789 : else
5790 1470773 : in_p = 1, low = low1, high = high0;
5791 : }
5792 :
5793 1884019 : 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 325649 : if (no_overlap)
5804 : in_p = 1, low = low0, high = high0;
5805 320031 : else if (lowequal && highequal)
5806 : in_p = 0, low = high = 0;
5807 319233 : else if (subset && lowequal)
5808 : {
5809 243387 : low = range_successor (high1);
5810 243387 : high = high0;
5811 243387 : in_p = 1;
5812 243387 : 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 75846 : else if (! subset || highequal)
5820 : {
5821 55272 : low = low0;
5822 55272 : high = range_predecessor (low1);
5823 55272 : in_p = 1;
5824 55272 : 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 1558370 : 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 1197295 : if (no_overlap)
5841 : in_p = 1, low = low1, high = high1;
5842 1189452 : else if (subset || highequal)
5843 : in_p = 0, low = high = 0;
5844 : else
5845 : {
5846 1079366 : low = range_successor (high0);
5847 1079366 : high = high1;
5848 1079366 : in_p = 1;
5849 1079366 : 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 361075 : if (no_overlap)
5866 : {
5867 263199 : 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 212068 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5875 210938 : switch (TREE_CODE (TREE_TYPE (low0)))
5876 : {
5877 51421 : case ENUMERAL_TYPE:
5878 51421 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5879 : GET_MODE_BITSIZE
5880 102842 : (TYPE_MODE (TREE_TYPE (low0)))))
5881 : break;
5882 : /* FALLTHROUGH */
5883 210737 : case INTEGER_TYPE:
5884 210737 : if (tree_int_cst_equal (low0,
5885 210737 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5886 212068 : 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 212068 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5899 211881 : switch (TREE_CODE (TREE_TYPE (high1)))
5900 : {
5901 51429 : case ENUMERAL_TYPE:
5902 51429 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5903 : GET_MODE_BITSIZE
5904 102858 : (TYPE_MODE (TREE_TYPE (high1)))))
5905 : break;
5906 : /* FALLTHROUGH */
5907 211680 : case INTEGER_TYPE:
5908 211680 : if (tree_int_cst_equal (high1,
5909 211680 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5910 212068 : 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 212068 : if (low0 == 0 && high1 == 0)
5929 : {
5930 325 : low = range_successor (high0);
5931 325 : high = range_predecessor (low1);
5932 325 : if (low == 0 || high == 0)
5933 : return 0;
5934 :
5935 : in_p = 1;
5936 : }
5937 : else
5938 : return 0;
5939 : }
5940 : }
5941 97876 : else if (subset)
5942 : in_p = 0, low = low0, high = high0;
5943 : else
5944 12015 : in_p = 0, low = low0, high = high1;
5945 : }
5946 :
5947 3243861 : *pin_p = in_p, *plow = low, *phigh = high;
5948 3243861 : 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 517004 : 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 517004 : tree arg1_type = TREE_TYPE (arg1);
5967 517004 : tree tem;
5968 :
5969 517004 : STRIP_NOPS (arg1);
5970 517004 : 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 517004 : if (!HONOR_SIGNED_ZEROS (type)
5992 1034018 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5993 517004 : ? real_zerop (arg01)
5994 515912 : : integer_zerop (arg01))
5995 1385907 : && ((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 350492 : || (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 515613 : if (!HONOR_SIGNED_ZEROS (type)
6061 515613 : && integer_zerop (arg01) && integer_zerop (arg2))
6062 : {
6063 265496 : if (comp_code == NE_EXPR)
6064 147 : return fold_convert_loc (loc, type, arg1);
6065 265349 : 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 515466 : if (!HONOR_SIGNED_ZEROS (type)
6096 515466 : && 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 789677 : && (in_gimple_form
6100 17797 : || VECTOR_TYPE_P (type)
6101 17724 : || (! lang_GNU_CXX ()
6102 15253 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6103 2471 : || ! maybe_lvalue_p (arg1)
6104 2450 : || ! maybe_lvalue_p (arg2)))
6105 : {
6106 272482 : tree comp_op0 = arg00;
6107 272482 : tree comp_op1 = arg01;
6108 272482 : tree comp_type = TREE_TYPE (comp_op0);
6109 :
6110 272482 : 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 6056 : case LE_EXPR:
6117 6056 : case LT_EXPR:
6118 6056 : case UNLE_EXPR:
6119 6056 : 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 6056 : if (!HONOR_NANS (arg1))
6125 : {
6126 6056 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6127 6056 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6128 6056 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6129 6056 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6130 4696 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6131 : comp_op1, comp_op0);
6132 6056 : return fold_convert_loc (loc, type, tem);
6133 : }
6134 : break;
6135 266425 : case GE_EXPR:
6136 266425 : case GT_EXPR:
6137 266425 : case UNGE_EXPR:
6138 266425 : case UNGT_EXPR:
6139 266425 : if (!HONOR_NANS (arg1))
6140 : {
6141 266423 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6142 266423 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6143 266423 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6144 266423 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6145 3672 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6146 : comp_op1, comp_op0);
6147 266423 : 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 25282916 : fold_range_test (location_t loc, enum tree_code code, tree type,
6180 : tree op0, tree op1)
6181 : {
6182 25282916 : int or_op = (code == TRUTH_ORIF_EXPR
6183 25282916 : || code == TRUTH_OR_EXPR);
6184 25282916 : int in0_p, in1_p, in_p;
6185 25282916 : tree low0, low1, low, high0, high1, high;
6186 25282916 : tree tem, lhs, rhs;
6187 :
6188 25282916 : if (!INTEGRAL_TYPE_P (type))
6189 : return 0;
6190 :
6191 25282916 : 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 25282916 : if (!lhs
6196 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6197 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6198 : return op0;
6199 25282914 : 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 25282914 : if (or_op)
6204 11885729 : 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 25251230 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6211 1208338 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6212 : in1_p, low1, high1)
6213 26289669 : && (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 1006725 : 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 24276189 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6225 24276189 : if (param_logical_op_non_short_circuit != -1)
6226 7865 : logical_op_non_short_circuit
6227 7865 : = param_logical_op_non_short_circuit;
6228 24276189 : if (logical_op_non_short_circuit
6229 24272226 : && !sanitize_coverage_p ()
6230 24272223 : && lhs != 0 && rhs != 0
6231 24271784 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6232 29861987 : && 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 171700 : if (simple_operand_p (lhs))
6238 69428 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6239 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6240 35230 : type, op0, op1);
6241 :
6242 136470 : else if (!lang_hooks.decls.global_bindings_p ()
6243 136470 : && !CONTAINS_PLACEHOLDER_P (lhs))
6244 : {
6245 135817 : tree common = save_expr (lhs);
6246 :
6247 249917 : if ((lhs = build_range_check (loc, type, common,
6248 114100 : or_op ? ! in0_p : in0_p,
6249 : low0, high0)) != 0
6250 249917 : && (rhs = build_range_check (loc, type, common,
6251 114100 : or_op ? ! in1_p : in1_p,
6252 : low1, high1)) != 0)
6253 : {
6254 249917 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6255 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6256 135817 : 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 130327 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6282 : bool rhs_only)
6283 : {
6284 130327 : enum tree_code code = TREE_CODE (cmpop);
6285 130327 : enum tree_code truthop_code = TREE_CODE (op);
6286 130327 : tree lhs = TREE_OPERAND (op, 0);
6287 130327 : tree rhs = TREE_OPERAND (op, 1);
6288 130327 : tree orig_lhs = lhs, orig_rhs = rhs;
6289 130327 : enum tree_code rhs_code = TREE_CODE (rhs);
6290 130327 : enum tree_code lhs_code = TREE_CODE (lhs);
6291 130327 : enum tree_code inv_code;
6292 :
6293 130327 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6294 : return NULL_TREE;
6295 :
6296 116789 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6297 : return NULL_TREE;
6298 :
6299 38968 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6300 :
6301 38968 : 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 38968 : 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 38968 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6321 38968 : if (inv_code == rhs_code
6322 928 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6323 39004 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6324 : return lhs;
6325 38955 : if (!rhs_only && inv_code == lhs_code
6326 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6327 39047 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6328 : return rhs;
6329 38864 : 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 24954744 : 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 24954744 : enum tree_code lcode, rcode;
6366 24954744 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6367 24954744 : 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 24954744 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6374 : return 0;
6375 :
6376 22042204 : lcode = TREE_CODE (lhs);
6377 22042204 : rcode = TREE_CODE (rhs);
6378 :
6379 22042204 : 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 22042204 : 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 22042204 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6394 19678615 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6395 : return 0;
6396 :
6397 18567304 : ll_arg = TREE_OPERAND (lhs, 0);
6398 18567304 : lr_arg = TREE_OPERAND (lhs, 1);
6399 18567304 : rl_arg = TREE_OPERAND (rhs, 0);
6400 18567304 : rr_arg = TREE_OPERAND (rhs, 1);
6401 :
6402 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6403 18567304 : if (simple_operand_p (ll_arg)
6404 18567304 : && simple_operand_p (lr_arg))
6405 : {
6406 15055121 : if (operand_equal_p (ll_arg, rl_arg, 0)
6407 15055121 : && operand_equal_p (lr_arg, rr_arg, 0))
6408 : {
6409 21288 : result = combine_comparisons (loc, code, lcode, rcode,
6410 : truth_type, ll_arg, lr_arg);
6411 21288 : if (result)
6412 : return result;
6413 : }
6414 15033833 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6415 15033833 : && 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 8762114 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6426 18546202 : ? 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 18546202 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6434 : false) >= 2
6435 18546099 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6436 17495489 : && simple_operand_p (rl_arg)
6437 28513587 : && simple_operand_p (rr_arg))
6438 : {
6439 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6440 11155006 : if (code == TRUTH_OR_EXPR
6441 1515511 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6442 622212 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6443 21044 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6444 11172632 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6445 34636 : return build2_loc (loc, NE_EXPR, truth_type,
6446 17318 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6447 : ll_arg, rl_arg),
6448 17318 : build_int_cst (TREE_TYPE (ll_arg), 0));
6449 :
6450 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6451 11137688 : if (code == TRUTH_AND_EXPR
6452 1714319 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6453 826136 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6454 7976 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6455 11139294 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6456 2764 : return build2_loc (loc, EQ_EXPR, truth_type,
6457 1382 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6458 : ll_arg, rl_arg),
6459 1382 : 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 101223898 : 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 101223898 : static int depth;
6488 101223898 : tree ret;
6489 :
6490 101223898 : if (depth > 3)
6491 : return NULL;
6492 :
6493 97269312 : depth++;
6494 97269312 : ret = extract_muldiv_1 (t, c, code, wide_type);
6495 97269312 : depth--;
6496 :
6497 97269312 : return ret;
6498 : }
6499 :
6500 : static tree
6501 97269312 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6502 : {
6503 97269312 : tree type = TREE_TYPE (t);
6504 97269312 : enum tree_code tcode = TREE_CODE (t);
6505 97269312 : tree ctype = type;
6506 97269312 : if (wide_type)
6507 : {
6508 32443306 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6509 : {
6510 124 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6511 97269312 : ctype = wide_type;
6512 : }
6513 32443182 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6514 64886364 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6515 97269312 : ctype = wide_type;
6516 : }
6517 97269312 : tree t1, t2;
6518 97269312 : bool same_p = tcode == code;
6519 97269312 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6520 :
6521 : /* Don't deal with constants of zero here; they confuse the code below. */
6522 97269312 : if (integer_zerop (c))
6523 : return NULL_TREE;
6524 :
6525 97253747 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6526 38594963 : op0 = TREE_OPERAND (t, 0);
6527 :
6528 97253747 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6529 12330842 : 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 97253747 : switch (tcode)
6534 : {
6535 4395942 : 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 4395942 : if (code == MULT_EXPR
6539 5631624 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6540 1235682 : TYPE_SIGN (type)))
6541 : {
6542 3593619 : 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 3593619 : if (TREE_OVERFLOW (tem))
6547 : return NULL_TREE;
6548 3590096 : return tem;
6549 : }
6550 : break;
6551 :
6552 38049638 : CASE_CONVERT: case NON_LVALUE_EXPR:
6553 38049638 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6554 : break;
6555 : /* If op0 is an expression ... */
6556 36752073 : if ((COMPARISON_CLASS_P (op0)
6557 : || UNARY_CLASS_P (op0)
6558 36752073 : || BINARY_CLASS_P (op0)
6559 33688726 : || VL_EXP_CLASS_P (op0)
6560 33627096 : || 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 36903578 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6564 1255897 : && (TYPE_PRECISION (ctype)
6565 1255897 : > 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 2658126 : || (TYPE_PRECISION (type)
6569 2658126 : < TYPE_PRECISION (TREE_TYPE (op0)))
6570 : /* ... or signedness changes for division or modulus,
6571 : then we cannot pass through this conversion. */
6572 2628260 : || (code != MULT_EXPR
6573 124576 : && (TYPE_UNSIGNED (ctype)
6574 124576 : != 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 2529984 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6579 1853199 : && !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 34285093 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6586 34285093 : && TREE_CODE (t2) == INTEGER_CST
6587 34285093 : && !TREE_OVERFLOW (t2)
6588 69809861 : && (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 44271 : 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 44271 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6616 : break;
6617 42487 : 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 798 : 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 798 : 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 798 : 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 8508252 : 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 8508252 : t1 = extract_muldiv (op0, c, code, wide_type);
6690 8508252 : t2 = extract_muldiv (op1, c, code, wide_type);
6691 820731 : if (t1 != 0 && t2 != 0
6692 285012 : && TYPE_OVERFLOW_WRAPS (ctype)
6693 8783953 : && (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 275701 : 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 8232551 : if (tcode == MINUS_EXPR)
6706 : {
6707 2216372 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6708 : /* If OP1 was not easily negatable, the constant may be OP0. */
6709 2216372 : if (TREE_CODE (op0) == INTEGER_CST)
6710 : {
6711 371013 : std::swap (op0, op1);
6712 371013 : std::swap (t1, t2);
6713 : }
6714 : }
6715 :
6716 8232551 : 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 3819377 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6723 : {
6724 177310 : if (code == CEIL_DIV_EXPR)
6725 : code = FLOOR_DIV_EXPR;
6726 177308 : else if (code == FLOOR_DIV_EXPR)
6727 : code = CEIL_DIV_EXPR;
6728 176877 : else if (code != MULT_EXPR
6729 176877 : && 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 3813907 : if (code == MULT_EXPR
6736 5044873 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6737 1230966 : TYPE_SIGN (type)))
6738 : {
6739 3012341 : op1 = const_binop (code, fold_convert (ctype, op1),
6740 : fold_convert (ctype, c));
6741 : /* We allow the constant to overflow with wrapping semantics. */
6742 3012341 : if (op1 == 0
6743 3012341 : || (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 3008820 : 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 3008820 : if (code == MULT_EXPR
6762 2580173 : && TYPE_OVERFLOW_WRAPS (ctype)
6763 5252635 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6764 682261 : 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 2424475 : 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 2424475 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6776 2424475 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6777 : /* If the multiplication can overflow we cannot optimize this. */
6778 10818 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6779 337 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6780 2435293 : && 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 2713904 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6789 2713904 : 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 2713904 : if (same_p
6794 2288123 : && TYPE_OVERFLOW_WRAPS (ctype)
6795 4828660 : && (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 2608101 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6799 2172872 : && TYPE_OVERFLOW_WRAPS (ctype)
6800 4607654 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6801 944989 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6802 : fold_convert (ctype, t1));
6803 1663112 : 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 624744 : if (tcode == code)
6809 : {
6810 199511 : bool overflow_p = false;
6811 199511 : wi::overflow_type overflow_mul;
6812 199511 : signop sign = TYPE_SIGN (ctype);
6813 199511 : unsigned prec = TYPE_PRECISION (ctype);
6814 399022 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6815 199511 : wi::to_wide (c, prec),
6816 199511 : sign, &overflow_mul);
6817 199511 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6818 199511 : if (overflow_mul
6819 1619 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6820 : overflow_p = true;
6821 199444 : if (!overflow_p)
6822 199444 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6823 : wide_int_to_tree (ctype, mul));
6824 199511 : }
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 425300 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6834 100997 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6835 526254 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6836 100650 : || (tcode == MULT_EXPR
6837 100650 : && 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 1144 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6842 1144 : TYPE_SIGN (type)))
6843 : {
6844 124 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6845 : fold_convert (ctype,
6846 : const_binop (TRUNC_DIV_EXPR,
6847 : op1, c)));
6848 : }
6849 1020 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6850 1020 : TYPE_SIGN (type)))
6851 : {
6852 304 : 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 119543435 : constant_boolean_node (bool value, tree type)
6873 : {
6874 119543435 : if (type == integer_type_node)
6875 20879423 : return value ? integer_one_node : integer_zero_node;
6876 98664012 : else if (type == boolean_type_node)
6877 94201292 : return value ? boolean_true_node : boolean_false_node;
6878 4462720 : else if (VECTOR_TYPE_P (type))
6879 2003 : return build_vector_from_val (type,
6880 2003 : build_int_cst (TREE_TYPE (type),
6881 2736 : value ? -1 : 0));
6882 : else
6883 4460717 : 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 969983 : 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 969983 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6903 969983 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6904 969983 : tree test, true_value, false_value;
6905 969983 : tree lhs = NULL_TREE;
6906 969983 : tree rhs = NULL_TREE;
6907 969983 : 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 989835 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6912 969983 : ANY_INTEGRAL_TYPE_P (type)
6913 775924 : && TYPE_OVERFLOW_TRAPS (type), op1))
6914 : return NULL_TREE;
6915 :
6916 949976 : if (TREE_CODE (cond) == COND_EXPR
6917 357874 : || TREE_CODE (cond) == VEC_COND_EXPR)
6918 : {
6919 595823 : test = TREE_OPERAND (cond, 0);
6920 595823 : true_value = TREE_OPERAND (cond, 1);
6921 595823 : 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 595823 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6926 7463 : lhs = true_value;
6927 595823 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6928 6 : rhs = false_value;
6929 : }
6930 354153 : else if (!(TREE_CODE (type) != VECTOR_TYPE
6931 354047 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
6932 : {
6933 352141 : tree testtype = TREE_TYPE (cond);
6934 352141 : test = cond;
6935 352141 : true_value = constant_boolean_node (true, testtype);
6936 352141 : 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 947964 : 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 947964 : if (!TREE_CONSTANT (arg)
6949 947964 : && (TREE_SIDE_EFFECTS (arg)
6950 446648 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6951 441978 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6952 : return NULL_TREE;
6953 :
6954 517649 : arg = fold_convert_loc (loc, arg_type, arg);
6955 517649 : if (lhs == 0)
6956 : {
6957 511618 : true_value = fold_convert_loc (loc, cond_type, true_value);
6958 511618 : if (cond_first_p)
6959 501524 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
6960 : else
6961 10094 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
6962 : }
6963 517649 : if (rhs == 0)
6964 : {
6965 517643 : false_value = fold_convert_loc (loc, cond_type, false_value);
6966 517643 : if (cond_first_p)
6967 506988 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
6968 : else
6969 10655 : 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 517649 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6974 : return NULL_TREE;
6975 :
6976 497060 : 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 636520 : fold_real_zero_addition_p (const_tree type, const_tree arg,
6996 : const_tree zero_arg, int negate)
6997 : {
6998 636520 : if (!real_zerop (zero_arg))
6999 : return false;
7000 :
7001 : /* Don't allow the fold with -fsignaling-nans. */
7002 635743 : 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 632541 : if (!HONOR_SIGNED_ZEROS (type))
7007 : return true;
7008 :
7009 : /* There is no case that is safe for all rounding modes. */
7010 615640 : 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 614977 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7015 2845 : zero_arg = uniform_vector_p (zero_arg);
7016 614977 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7017 : return false;
7018 :
7019 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7020 613733 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7021 210 : 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 613733 : 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 21386 : 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 21386 : REAL_VALUE_TYPE raw;
7049 21386 : REAL_VALUE_TYPE rnd;
7050 21386 : bool check_im1 = true;
7051 21386 : bool check_ip1 = true;
7052 :
7053 21386 : switch (cmp)
7054 : {
7055 : case EQ_EXPR:
7056 : case NE_EXPR:
7057 : /* Check both i-1 and i+1. */
7058 : break;
7059 :
7060 19801 : case LT_EXPR:
7061 19801 : case GE_EXPR:
7062 : /* Only check i-1. */
7063 19801 : check_ip1 = false;
7064 19801 : break;
7065 :
7066 356 : case LE_EXPR:
7067 356 : case GT_EXPR:
7068 : /* Only check i+1. */
7069 356 : check_im1 = false;
7070 356 : break;
7071 :
7072 : default:
7073 : return false;
7074 : }
7075 :
7076 21386 : 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 21386 : if (check_im1 && wi::gt_p (i, lo, isign))
7084 : {
7085 1292 : 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 948 : real_from_integer (&rnd, fmt, i - 1, isign);
7094 948 : if (real_identical (r, &rnd))
7095 : return false;
7096 : }
7097 :
7098 20698 : if (check_ip1 && wi::lt_p (i, hi, isign))
7099 : {
7100 875 : 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 875 : real_from_integer (&rnd, fmt, i + 1, isign);
7109 875 : if (real_identical (r, &rnd))
7110 0 : 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 1698877 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7125 : tree *hi, bool *neg_overflow)
7126 : {
7127 1698877 : tree prod, tmp, type = TREE_TYPE (c1);
7128 1698877 : signop sign = TYPE_SIGN (type);
7129 1698877 : 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 1698877 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7134 1698877 : prod = force_fit_type (type, val, -1, overflow);
7135 1698877 : *neg_overflow = false;
7136 :
7137 1698877 : if (sign == UNSIGNED)
7138 : {
7139 1669284 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7140 1669284 : *lo = prod;
7141 :
7142 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7143 1669284 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7144 1669284 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7145 : }
7146 29593 : else if (tree_int_cst_sgn (c1) >= 0)
7147 : {
7148 28194 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7149 28194 : switch (tree_int_cst_sgn (c2))
7150 : {
7151 4878 : case -1:
7152 4878 : *neg_overflow = true;
7153 4878 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7154 4878 : *hi = prod;
7155 4878 : break;
7156 :
7157 14794 : case 0:
7158 14794 : *lo = fold_negate_const (tmp, type);
7159 14794 : *hi = tmp;
7160 14794 : break;
7161 :
7162 8522 : case 1:
7163 8522 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7164 8522 : *lo = prod;
7165 8522 : 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 1698877 : if (code != EQ_EXPR && code != NE_EXPR)
7201 : return code;
7202 :
7203 16588 : if (TREE_OVERFLOW (*lo)
7204 16588 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7205 715 : *lo = NULL_TREE;
7206 16588 : if (TREE_OVERFLOW (*hi)
7207 16588 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7208 95 : *hi = NULL_TREE;
7209 :
7210 : return code;
7211 1698877 : }
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 1640114210 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7219 : {
7220 1640114210 : if (CONSTANT_CLASS_P (arg1))
7221 : return false;
7222 537218349 : if (CONSTANT_CLASS_P (arg0))
7223 : return true;
7224 :
7225 495624714 : STRIP_NOPS (arg0);
7226 495624714 : STRIP_NOPS (arg1);
7227 :
7228 495624714 : if (TREE_CONSTANT (arg1))
7229 : return false;
7230 477977893 : if (TREE_CONSTANT (arg0))
7231 : return true;
7232 :
7233 : /* Put addresses in arg1. */
7234 477195968 : if (TREE_CODE (arg1) == ADDR_EXPR)
7235 : return false;
7236 457525864 : 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 457127970 : if (TREE_CODE (arg0) == SSA_NAME
7244 346302261 : && TREE_CODE (arg1) == SSA_NAME
7245 797521275 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7246 : return true;
7247 :
7248 : /* Put SSA_NAMEs last. */
7249 433887597 : if (TREE_CODE (arg1) == SSA_NAME)
7250 : return false;
7251 101553855 : if (TREE_CODE (arg0) == SSA_NAME)
7252 : return true;
7253 :
7254 : /* Put variables last. */
7255 95644899 : if (DECL_P (arg1))
7256 : return false;
7257 51743396 : if (DECL_P (arg0))
7258 6053842 : 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 24207104 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7270 : {
7271 24207104 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7272 :
7273 24207104 : if (TREE_CODE (bound) == LT_EXPR)
7274 5019386 : a = TREE_OPERAND (bound, 0);
7275 19187718 : else if (TREE_CODE (bound) == GT_EXPR)
7276 2797527 : a = TREE_OPERAND (bound, 1);
7277 : else
7278 : return NULL_TREE;
7279 :
7280 7816913 : typea = TREE_TYPE (a);
7281 7816913 : if (!INTEGRAL_TYPE_P (typea)
7282 496294 : && !POINTER_TYPE_P (typea))
7283 : return NULL_TREE;
7284 :
7285 7637526 : if (TREE_CODE (ineq) == LT_EXPR)
7286 : {
7287 1475113 : a1 = TREE_OPERAND (ineq, 1);
7288 1475113 : y = TREE_OPERAND (ineq, 0);
7289 : }
7290 6162413 : else if (TREE_CODE (ineq) == GT_EXPR)
7291 : {
7292 1170869 : a1 = TREE_OPERAND (ineq, 0);
7293 1170869 : y = TREE_OPERAND (ineq, 1);
7294 : }
7295 : else
7296 : return NULL_TREE;
7297 :
7298 2645982 : if (TREE_TYPE (a1) != typea)
7299 : return NULL_TREE;
7300 :
7301 1887987 : if (POINTER_TYPE_P (typea))
7302 : {
7303 : /* Convert the pointer types into integer before taking the difference. */
7304 11657 : tree ta = fold_convert_loc (loc, ssizetype, a);
7305 11657 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7306 11657 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7307 : }
7308 : else
7309 1876330 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7310 :
7311 1887987 : if (!diff || !integer_onep (diff))
7312 : return NULL_TREE;
7313 :
7314 9053 : 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 9326632 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7322 : tree arg0, tree arg1)
7323 : {
7324 9326632 : tree arg00, arg01, arg10, arg11;
7325 9326632 : 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 9326632 : if (TREE_CODE (arg0) == MULT_EXPR)
7334 : {
7335 8315542 : arg00 = TREE_OPERAND (arg0, 0);
7336 8315542 : arg01 = TREE_OPERAND (arg0, 1);
7337 : }
7338 1011090 : else if (TREE_CODE (arg0) == INTEGER_CST)
7339 : {
7340 74774 : arg00 = build_one_cst (type);
7341 74774 : arg01 = arg0;
7342 : }
7343 : else
7344 : {
7345 : /* We cannot generate constant 1 for fract. */
7346 936316 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7347 : return NULL_TREE;
7348 936316 : arg00 = arg0;
7349 936316 : arg01 = build_one_cst (type);
7350 : }
7351 9326632 : if (TREE_CODE (arg1) == MULT_EXPR)
7352 : {
7353 2383493 : arg10 = TREE_OPERAND (arg1, 0);
7354 2383493 : arg11 = TREE_OPERAND (arg1, 1);
7355 : }
7356 6943139 : else if (TREE_CODE (arg1) == INTEGER_CST)
7357 : {
7358 3641595 : 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 7051977 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7362 234501 : && negate_expr_p (arg1)
7363 3872808 : && code == PLUS_EXPR)
7364 : {
7365 231213 : arg11 = negate_expr (arg1);
7366 231213 : code = MINUS_EXPR;
7367 : }
7368 : else
7369 : arg11 = arg1;
7370 : }
7371 : else
7372 : {
7373 : /* We cannot generate constant 1 for fract. */
7374 3301544 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7375 : return NULL_TREE;
7376 3301544 : arg10 = arg1;
7377 3301544 : arg11 = build_one_cst (type);
7378 : }
7379 9326632 : same = NULL_TREE;
7380 :
7381 : /* Prefer factoring a common non-constant. */
7382 9326632 : if (operand_equal_p (arg00, arg10, 0))
7383 : same = arg00, alt0 = arg01, alt1 = arg11;
7384 9322868 : else if (operand_equal_p (arg01, arg11, 0))
7385 : same = arg01, alt0 = arg00, alt1 = arg10;
7386 9215904 : else if (operand_equal_p (arg00, arg11, 0))
7387 : same = arg00, alt0 = arg01, alt1 = arg10;
7388 9215842 : 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 9210652 : else if (TREE_CODE (arg01) == INTEGER_CST
7395 8150558 : && TREE_CODE (arg11) == INTEGER_CST)
7396 : {
7397 7938116 : wide_int int01 = wi::to_wide (arg01);
7398 7938116 : wide_int int11 = wi::to_wide (arg11);
7399 7938116 : bool swap = false;
7400 7938116 : tree maybe_same;
7401 :
7402 : /* Move min of absolute values to int11. */
7403 7938118 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7404 : {
7405 3649899 : std::swap (int01, int11);
7406 3649899 : std::swap (arg00, arg10);
7407 3649899 : maybe_same = arg01;
7408 3649899 : swap = true;
7409 : }
7410 : else
7411 : maybe_same = arg11;
7412 :
7413 7938116 : wide_int factor = wi::abs (int11);
7414 7938116 : if (wi::gtu_p (factor, 1u)
7415 4361935 : && wi::exact_log2 (factor) != -1
7416 12457864 : && (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 9496874 : && TREE_CODE (arg10) != INTEGER_CST)
7421 : {
7422 2625336 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7423 1312668 : wide_int_to_tree (TREE_TYPE (arg00),
7424 1312668 : wi::sdiv_trunc (int01,
7425 : int11)));
7426 1312668 : alt1 = arg10;
7427 1312668 : same = maybe_same;
7428 1312668 : if (swap)
7429 1189784 : std::swap (alt0, alt1);
7430 : }
7431 7938118 : }
7432 :
7433 8054096 : if (!same)
7434 : return NULL_TREE;
7435 :
7436 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7437 1428648 : || TYPE_OVERFLOW_WRAPS (type)
7438 : /* We are neither factoring zero nor minus one. */
7439 1545242 : || TREE_CODE (same) == INTEGER_CST)
7440 1417014 : 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 1417014 : 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 11634 : tree utype = unsigned_type_for (type);
7450 11634 : 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 23268 : if (TREE_CODE (tem) == INTEGER_CST
7456 14876 : && (wi::to_wide (tem)
7457 18118 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7458 3229 : return fold_build2_loc (loc, MULT_EXPR, type,
7459 3229 : 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 56848490 : native_encode_wide_int (tree type, const wide_int_ref &val,
7474 : unsigned char *ptr, int len, int off)
7475 : {
7476 56848490 : int total_bytes;
7477 56848490 : if (BITINT_TYPE_P (type))
7478 : {
7479 17389 : struct bitint_info info;
7480 17389 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7481 17389 : gcc_assert (ok);
7482 17389 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7483 17389 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7484 : {
7485 17083 : 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 17083 : 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 113662202 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7499 56848490 : int byte, offset, word, words;
7500 56848490 : unsigned char value;
7501 :
7502 56848490 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7503 : return 0;
7504 56848002 : if (off == -1)
7505 55962868 : off = 0;
7506 :
7507 56848002 : if (ptr == NULL)
7508 : /* Dry run. */
7509 2844962 : return MIN (len, total_bytes - off);
7510 :
7511 : words = total_bytes / UNITS_PER_WORD;
7512 :
7513 259328159 : for (byte = 0; byte < total_bytes; byte++)
7514 : {
7515 205325119 : 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 205325119 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7519 :
7520 205325119 : if (total_bytes > UNITS_PER_WORD)
7521 : {
7522 205325119 : word = byte / UNITS_PER_WORD;
7523 205325119 : if (WORDS_BIG_ENDIAN)
7524 : word = (words - 1) - word;
7525 205325119 : offset = word * UNITS_PER_WORD;
7526 205325119 : if (BYTES_BIG_ENDIAN)
7527 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7528 : else
7529 205325119 : offset += byte % UNITS_PER_WORD;
7530 : }
7531 : else
7532 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7533 205325119 : if (offset >= off && offset - off < len)
7534 203999075 : ptr[offset - off] = value;
7535 : }
7536 54003040 : 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 56848490 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7546 : {
7547 56848490 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7548 56848490 : 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 861635 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7588 : unsigned char *ptr, int len, int off)
7589 : {
7590 861635 : int total_bytes = GET_MODE_SIZE (mode);
7591 861635 : int byte, offset, word, words, bitpos;
7592 861635 : 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 861635 : long tmp[6];
7598 :
7599 861635 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7600 : return 0;
7601 859335 : if (off == -1)
7602 753899 : off = 0;
7603 :
7604 859335 : if (ptr == NULL)
7605 : /* Dry run. */
7606 137361 : return MIN (len, total_bytes - off);
7607 :
7608 721974 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7609 :
7610 721974 : real_to_target (tmp, val, mode);
7611 :
7612 7002788 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7613 6280814 : bitpos += BITS_PER_UNIT)
7614 : {
7615 6280814 : byte = (bitpos / BITS_PER_UNIT) & 3;
7616 6280814 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7617 :
7618 6280814 : 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 6280814 : offset = byte;
7632 6280814 : 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 6280814 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7641 6280814 : if (offset >= off
7642 6277574 : && offset - off < len)
7643 6255914 : ptr[offset - off] = value;
7644 : }
7645 721974 : 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 1075987 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7678 : int off, unsigned HOST_WIDE_INT count)
7679 : {
7680 1075987 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7681 2151974 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7682 1077021 : && 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 1075031 : int offset = 0;
7719 1075031 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7720 4307175 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7721 : {
7722 3833104 : if (off >= size)
7723 : {
7724 23655 : off -= size;
7725 23655 : continue;
7726 : }
7727 3809449 : tree elem = VECTOR_CST_ELT (expr, i);
7728 3809449 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7729 : len - offset, off);
7730 3809449 : if ((off == -1 && res != size) || res == 0)
7731 : return 0;
7732 3808920 : offset += res;
7733 3808920 : if (offset >= len)
7734 600431 : return (off == -1 && i < count - 1) ? 0 : offset;
7735 3208489 : if (off != -1)
7736 438724 : 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 920503 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7748 : {
7749 920503 : unsigned HOST_WIDE_INT count;
7750 920503 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7751 : return 0;
7752 920503 : 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 143299 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7763 : {
7764 143299 : tree type = TREE_TYPE (expr);
7765 :
7766 : /* Wide-char strings are encoded in target byte-order so native
7767 : encoding them is trivial. */
7768 143299 : if (BITS_PER_UNIT != CHAR_BIT
7769 143299 : || TREE_CODE (type) != ARRAY_TYPE
7770 143299 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7771 286598 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7772 : return 0;
7773 :
7774 143299 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7775 143299 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7776 : return 0;
7777 142433 : if (off == -1)
7778 56370 : off = 0;
7779 142433 : len = MIN (total_bytes - off, len);
7780 142433 : if (ptr == NULL)
7781 : /* Dry run. */;
7782 : else
7783 : {
7784 142433 : int written = 0;
7785 142433 : if (off < TREE_STRING_LENGTH (expr))
7786 : {
7787 141940 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7788 141940 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7789 : }
7790 142433 : 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 49031 : 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 49031 : 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 91666 : if (BITS_PER_UNIT != CHAR_BIT
7813 45833 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7814 : return 0;
7815 :
7816 45833 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7817 45833 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7818 : return 0;
7819 45833 : if (off == -1)
7820 0 : off = 0;
7821 45833 : len = MIN (total_bytes - off, len);
7822 45833 : if (ptr == NULL)
7823 : /* Dry run. */;
7824 : else
7825 45833 : 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 73406749 : 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 73406749 : if (off < -1)
7842 : return 0;
7843 :
7844 73406737 : switch (TREE_CODE (expr))
7845 : {
7846 56846244 : case INTEGER_CST:
7847 56846244 : return native_encode_int (expr, ptr, len, off);
7848 :
7849 861635 : case REAL_CST:
7850 861635 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7851 1723270 : 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 920503 : case VECTOR_CST:
7860 920503 : return native_encode_vector (expr, ptr, len, off);
7861 :
7862 143299 : case STRING_CST:
7863 143299 : return native_encode_string (expr, ptr, len, off);
7864 :
7865 49031 : case CONSTRUCTOR:
7866 49031 : 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 49904695 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7923 : int off, unsigned char *mask)
7924 : {
7925 49904695 : int r;
7926 :
7927 : /* We don't support starting at negative offset and -1 is special. */
7928 49904695 : if (off < -1 || init == NULL_TREE)
7929 : return 0;
7930 :
7931 49904695 : gcc_assert (mask == NULL || (off == 0 && ptr));
7932 :
7933 49904695 : STRIP_NOPS (init);
7934 49904695 : 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 47692028 : default:
7941 47692028 : r = native_encode_expr (init, ptr, len, off);
7942 47692028 : if (mask)
7943 7132 : memset (mask, 0, r);
7944 : return r;
7945 2212667 : case CONSTRUCTOR:
7946 2212667 : tree type = TREE_TYPE (init);
7947 2212667 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7948 2212667 : if (total_bytes < 0)
7949 : return 0;
7950 2212667 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7951 : return 0;
7952 2212591 : int o = off == -1 ? 0 : off;
7953 2212591 : if (TREE_CODE (type) == ARRAY_TYPE)
7954 : {
7955 302221 : tree min_index;
7956 302221 : unsigned HOST_WIDE_INT cnt;
7957 302221 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7958 302221 : constructor_elt *ce;
7959 :
7960 302221 : if (!TYPE_DOMAIN (type)
7961 302221 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7962 : return 0;
7963 :
7964 302221 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7965 302221 : if (fieldsize <= 0)
7966 : return 0;
7967 :
7968 302221 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7969 302221 : if (ptr)
7970 302221 : memset (ptr, '\0', MIN (total_bytes - off, len));
7971 :
7972 302221 : for (cnt = 0; ; cnt++)
7973 : {
7974 47359614 : tree val = NULL_TREE, index = NULL_TREE;
7975 47359614 : HOST_WIDE_INT pos = curpos, count = 0;
7976 47359614 : bool full = false;
7977 47359614 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7978 : {
7979 47247151 : val = ce->value;
7980 47247151 : index = ce->index;
7981 : }
7982 112463 : else if (mask == NULL
7983 618 : || CONSTRUCTOR_NO_CLEARING (init)
7984 113021 : || curpos >= total_bytes)
7985 : break;
7986 : else
7987 : pos = total_bytes;
7988 :
7989 47247151 : 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 46712115 : else if (index)
8011 : {
8012 46712115 : if (TREE_CODE (index) != INTEGER_CST)
8013 0 : return 0;
8014 46712115 : offset_int wpos
8015 46712115 : = wi::sext (wi::to_offset (index)
8016 93424230 : - wi::to_offset (min_index),
8017 46712115 : TYPE_PRECISION (sizetype));
8018 46712115 : wpos *= fieldsize;
8019 46712115 : if (!wi::fits_shwi_p (wpos))
8020 : return 0;
8021 46712115 : pos = wpos.to_shwi ();
8022 : }
8023 :
8024 47248412 : 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 47247223 : curpos = pos;
8048 47247223 : 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 47324666 : do
8092 : {
8093 47324666 : if (off == -1
8094 630828 : || (curpos >= off
8095 212581 : && (curpos + fieldsize
8096 212581 : <= (HOST_WIDE_INT) off + len)))
8097 : {
8098 46876033 : 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 93779294 : else if (!native_encode_initializer (val,
8107 : ptr
8108 46797991 : ? 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 448633 : 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 47134836 : curpos += fieldsize;
8148 : }
8149 47134836 : while (count-- != 0);
8150 47057393 : }
8151 112391 : return MIN (total_bytes - off, len);
8152 : }
8153 1910370 : else if (TREE_CODE (type) == RECORD_TYPE
8154 1910370 : || TREE_CODE (type) == UNION_TYPE)
8155 : {
8156 1910370 : unsigned HOST_WIDE_INT cnt;
8157 1910370 : constructor_elt *ce;
8158 1910370 : tree fld_base = TYPE_FIELDS (type);
8159 1910370 : tree to_free = NULL_TREE;
8160 :
8161 1910370 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8162 1910370 : if (ptr != NULL)
8163 1910370 : memset (ptr, '\0', MIN (total_bytes - o, len));
8164 1910370 : for (cnt = 0; ; cnt++)
8165 : {
8166 2284603 : tree val = NULL_TREE, field = NULL_TREE;
8167 2284603 : HOST_WIDE_INT pos = 0, fieldsize;
8168 2284603 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8169 :
8170 2284603 : if (to_free)
8171 : {
8172 0 : ggc_free (to_free);
8173 0 : to_free = NULL_TREE;
8174 : }
8175 :
8176 2284603 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8177 : {
8178 401493 : val = ce->value;
8179 401493 : field = ce->index;
8180 401493 : if (field == NULL_TREE)
8181 : return 0;
8182 :
8183 401493 : pos = int_byte_position (field);
8184 401493 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8185 1496 : continue;
8186 : }
8187 1883110 : else if (mask == NULL
8188 1883110 : || CONSTRUCTOR_NO_CLEARING (init))
8189 : break;
8190 : else
8191 : pos = total_bytes;
8192 :
8193 414022 : 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 1614 : 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 400056 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8230 15127 : && TYPE_DOMAIN (TREE_TYPE (field))
8231 415183 : && ! 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 399975 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8255 399975 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8256 : return 0;
8257 399975 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8258 : }
8259 400056 : 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 400055 : 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 400055 : if (off != -1 && pos + fieldsize <= off)
8285 3195 : continue;
8286 :
8287 396860 : if (val == NULL_TREE)
8288 0 : continue;
8289 :
8290 396860 : if (DECL_BIT_FIELD (field)
8291 396860 : && 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 394353 : if (off == -1
8433 28604 : || (pos >= off
8434 27774 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8435 : {
8436 385984 : int fldsize = fieldsize;
8437 20235 : if (off == -1)
8438 : {
8439 365749 : tree fld = DECL_CHAIN (field);
8440 5777150 : while (fld)
8441 : {
8442 5430108 : if (TREE_CODE (fld) == FIELD_DECL)
8443 : break;
8444 5411401 : fld = DECL_CHAIN (fld);
8445 : }
8446 365749 : if (fld == NULL_TREE)
8447 347042 : fldsize = len - pos;
8448 : }
8449 418077 : 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 385984 : if (!r)
8455 : return 0;
8456 366274 : if (off == -1
8457 353279 : && fldsize != fieldsize
8458 1149 : && r > fieldsize
8459 822 : && pos + r > total_bytes)
8460 374233 : 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 374233 : }
8486 1883051 : 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 2871751 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8499 : {
8500 2871751 : int total_bytes;
8501 2871751 : 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 5743440 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8523 :
8524 2871751 : if (total_bytes > len)
8525 : return NULL_TREE;
8526 :
8527 2871509 : wide_int result = wi::from_buffer (ptr, total_bytes);
8528 :
8529 2871509 : return wide_int_to_tree (type, result);
8530 2871509 : }
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 37036 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8562 : {
8563 37036 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8564 37036 : int total_bytes = GET_MODE_SIZE (mode);
8565 37036 : 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 37036 : REAL_VALUE_TYPE r;
8570 37036 : long tmp[6];
8571 :
8572 37036 : if (total_bytes > len || total_bytes > 24)
8573 : return NULL_TREE;
8574 36975 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8575 :
8576 36975 : memset (tmp, 0, sizeof (tmp));
8577 271167 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8578 234192 : bitpos += BITS_PER_UNIT)
8579 : {
8580 : /* Both OFFSET and BYTE index within a long;
8581 : bitpos indexes the whole float. */
8582 234192 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8583 234192 : 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 234192 : offset = byte;
8597 234192 : 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 234192 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8606 :
8607 234192 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8608 : }
8609 :
8610 36975 : real_from_target (&r, tmp, mode);
8611 36975 : 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 237887 : 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 237887 : tree elt_type = TREE_TYPE (type);
8650 237887 : if (VECTOR_BOOLEAN_TYPE_P (type)
8651 237891 : && 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 22 : 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 237885 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8673 237885 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8674 : return NULL_TREE;
8675 :
8676 237885 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8677 1186191 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8678 : {
8679 710555 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8680 710555 : if (!elt)
8681 134 : return NULL_TREE;
8682 710421 : builder.quick_push (elt);
8683 710421 : bytes += elt_bytes;
8684 : }
8685 237751 : return builder.build ();
8686 237885 : }
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 82405 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8694 : {
8695 82405 : unsigned HOST_WIDE_INT size;
8696 :
8697 82405 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8698 82405 : || size > len)
8699 2 : return NULL_TREE;
8700 :
8701 82403 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8702 82403 : 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 3149584 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8714 : {
8715 3149584 : switch (TREE_CODE (type))
8716 : {
8717 2871751 : case INTEGER_TYPE:
8718 2871751 : case ENUMERAL_TYPE:
8719 2871751 : case BOOLEAN_TYPE:
8720 2871751 : case POINTER_TYPE:
8721 2871751 : case REFERENCE_TYPE:
8722 2871751 : case OFFSET_TYPE:
8723 2871751 : case BITINT_TYPE:
8724 2871751 : return native_interpret_int (type, ptr, len);
8725 :
8726 35449 : case REAL_TYPE:
8727 35449 : 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 35388 : unsigned char buf[24 * 2];
8736 35388 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8737 35388 : int total_bytes = GET_MODE_SIZE (mode);
8738 35388 : memcpy (buf + 24, ptr, total_bytes);
8739 35388 : clear_type_padding_in_mask (type, buf + 24);
8740 35388 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8741 35388 : || 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 82405 : case VECTOR_TYPE:
8754 82405 : 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 341138 : can_native_interpret_type_p (tree type)
8766 : {
8767 341138 : 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 38334 : default:
8781 38334 : 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 : 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 29583 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8960 : unsigned int amnt)
8961 : {
8962 29583 : if (amnt == 0)
8963 : return;
8964 :
8965 17139 : unsigned char carry_over = 0U;
8966 17139 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8967 17139 : unsigned char clear_mask = (~0U) << amnt;
8968 :
8969 101099 : for (unsigned int i = 0; i < sz; i++)
8970 : {
8971 83960 : unsigned prev_carry_over = carry_over;
8972 83960 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8973 :
8974 83960 : ptr[i] <<= amnt;
8975 83960 : if (i != 0)
8976 : {
8977 66821 : ptr[i] &= clear_mask;
8978 66821 : 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 164420 : fold_view_convert_vector_encoding (tree type, tree expr)
9019 : {
9020 164420 : tree expr_type = TREE_TYPE (expr);
9021 164420 : poly_uint64 type_bits, expr_bits;
9022 164420 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9023 164420 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9024 : return NULL_TREE;
9025 :
9026 164420 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9027 164420 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9028 164420 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9029 164420 : 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 164420 : if (VECTOR_CST_STEPPED_P (expr)
9034 164420 : && (!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 155484 : unsigned int expr_sequence_bits
9040 155484 : = 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 155484 : unsigned int type_sequence_bits
9045 155484 : = 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 155484 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9052 155484 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9053 : BITS_PER_UNIT);
9054 155484 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9055 155484 : if (known_gt (buffer_bits, expr_bits))
9056 : return NULL_TREE;
9057 :
9058 : /* Get enough bytes of EXPR to form the new encoding. */
9059 155484 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9060 155484 : buffer.quick_grow (buffer_bytes);
9061 155484 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9062 155484 : buffer_bits / expr_elt_bits)
9063 : != (int) buffer_bytes)
9064 : return NULL_TREE;
9065 :
9066 : /* Re-encode the bytes as TYPE. */
9067 155484 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9068 310968 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9069 155484 : type_npatterns, nelts_per_pattern);
9070 155484 : }
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 13284887 : fold_view_convert_expr (tree type, tree expr)
9078 : {
9079 13284887 : unsigned char buffer[128];
9080 13284887 : unsigned char *buf;
9081 13284887 : int len;
9082 13284887 : HOST_WIDE_INT l;
9083 :
9084 : /* Check that the host and target are sane. */
9085 13284887 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9086 : return NULL_TREE;
9087 :
9088 13284887 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9089 164420 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9090 : return res;
9091 :
9092 13129470 : l = int_size_in_bytes (type);
9093 13129470 : if (l > (int) sizeof (buffer)
9094 13129470 : && 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 13129470 : len = native_encode_expr (expr, buf, len);
9105 13129470 : if (len == 0)
9106 : return NULL_TREE;
9107 :
9108 1847661 : 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 593828930 : 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 593828930 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9119 0 : t = TREE_OPERAND (t, 0);
9120 :
9121 593828930 : if (INDIRECT_REF_P (t))
9122 : {
9123 62643392 : t = TREE_OPERAND (t, 0);
9124 :
9125 62643392 : if (TREE_TYPE (t) != ptrtype)
9126 40054874 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9127 : }
9128 531185538 : else if (TREE_CODE (t) == MEM_REF
9129 531185538 : && integer_zerop (TREE_OPERAND (t, 1)))
9130 : {
9131 1678880 : t = TREE_OPERAND (t, 0);
9132 :
9133 1678880 : if (TREE_TYPE (t) != ptrtype)
9134 1102302 : t = fold_convert_loc (loc, ptrtype, t);
9135 : }
9136 529506658 : else if (TREE_CODE (t) == MEM_REF
9137 529506658 : && 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 529505995 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9142 : {
9143 31421001 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9144 :
9145 31421001 : if (TREE_TYPE (t) != ptrtype)
9146 16340 : t = fold_convert_loc (loc, ptrtype, t);
9147 : }
9148 : else
9149 498084994 : 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 512188026 : build_fold_addr_expr_loc (location_t loc, tree t)
9158 : {
9159 512188026 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9160 :
9161 512188026 : 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 2087309576 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9170 : {
9171 2087309576 : tree tem;
9172 2087309576 : tree arg0;
9173 2087309576 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9174 :
9175 2087309576 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9176 : && TREE_CODE_LENGTH (code) == 1);
9177 :
9178 2087309576 : arg0 = op0;
9179 2087309576 : if (arg0)
9180 : {
9181 2087296324 : 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 1188479796 : 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 898816528 : STRIP_NOPS (arg0);
9202 : }
9203 :
9204 2087296324 : if (CONSTANT_CLASS_P (arg0))
9205 : {
9206 328514773 : tree tem = const_unop (code, type, arg0);
9207 328514773 : if (tem)
9208 : {
9209 288156835 : if (TREE_TYPE (tem) != type)
9210 9941 : tem = fold_convert_loc (loc, type, tem);
9211 : return tem;
9212 : }
9213 : }
9214 : }
9215 :
9216 1799152741 : tem = generic_simplify (loc, code, type, op0);
9217 1799152741 : if (tem)
9218 : return tem;
9219 :
9220 1353568221 : if (TREE_CODE_CLASS (code) == tcc_unary)
9221 : {
9222 769611193 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9223 1087569 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9224 : fold_build1_loc (loc, code, type,
9225 1087569 : fold_convert_loc (loc, TREE_TYPE (op0),
9226 2175138 : TREE_OPERAND (arg0, 1))));
9227 768523624 : else if (TREE_CODE (arg0) == COND_EXPR)
9228 : {
9229 558583 : tree arg01 = TREE_OPERAND (arg0, 1);
9230 558583 : tree arg02 = TREE_OPERAND (arg0, 2);
9231 558583 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9232 554401 : arg01 = fold_build1_loc (loc, code, type,
9233 : fold_convert_loc (loc,
9234 554401 : TREE_TYPE (op0), arg01));
9235 558583 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9236 548201 : arg02 = fold_build1_loc (loc, code, type,
9237 : fold_convert_loc (loc,
9238 548201 : TREE_TYPE (op0), arg02));
9239 558583 : 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 558583 : if ((CONVERT_EXPR_CODE_P (code)
9252 10329 : || code == NON_LVALUE_EXPR)
9253 548273 : && TREE_CODE (tem) == COND_EXPR
9254 528540 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9255 464971 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9256 292289 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9257 292077 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9258 292077 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9259 292077 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9260 871649 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9261 21862 : && (INTEGRAL_TYPE_P
9262 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9263 21822 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9264 21709 : || flag_syntax_only))
9265 269535 : tem = build1_loc (loc, code, type,
9266 : build3 (COND_EXPR,
9267 269535 : TREE_TYPE (TREE_OPERAND
9268 : (TREE_OPERAND (tem, 1), 0)),
9269 269535 : TREE_OPERAND (tem, 0),
9270 269535 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9271 269535 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9272 : 0)));
9273 : return tem;
9274 : }
9275 : }
9276 :
9277 1351922069 : switch (code)
9278 : {
9279 43224413 : case NON_LVALUE_EXPR:
9280 43224413 : if (!maybe_lvalue_p (op0))
9281 31454921 : return fold_convert_loc (loc, type, op0);
9282 : return NULL_TREE;
9283 :
9284 714230475 : CASE_CONVERT:
9285 714230475 : case FLOAT_EXPR:
9286 714230475 : case FIX_TRUNC_EXPR:
9287 714230475 : 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 742789 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9296 166337 : return build2_loc (loc, TREE_CODE (op0), type,
9297 166337 : TREE_OPERAND (op0, 0),
9298 332674 : TREE_OPERAND (op0, 1));
9299 576452 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9300 7681 : && TREE_CODE (type) != VECTOR_TYPE)
9301 7681 : return build3_loc (loc, COND_EXPR, type, op0,
9302 : constant_boolean_node (true, type),
9303 7681 : 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 714056457 : if (TREE_CODE (op0) == ADDR_EXPR
9310 249683198 : && POINTER_TYPE_P (type)
9311 956858534 : && handled_component_p (TREE_OPERAND (op0, 0)))
9312 : {
9313 54100911 : poly_int64 bitsize, bitpos;
9314 54100911 : tree offset;
9315 54100911 : machine_mode mode;
9316 54100911 : int unsignedp, reversep, volatilep;
9317 54100911 : tree base
9318 54100911 : = 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 54100911 : if (!offset
9325 53955332 : && known_eq (bitpos, 0)
9326 37165049 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9327 37165049 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9328 54113872 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9329 12760 : return fold_convert_loc (loc, type,
9330 12760 : build_fold_addr_expr_loc (loc, base));
9331 : }
9332 :
9333 714043697 : if (TREE_CODE (op0) == MODIFY_EXPR
9334 293459 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9335 : /* Detect assigning a bitfield. */
9336 714045700 : && !(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 714041742 : if (TREE_CODE (type) == INTEGER_TYPE
9357 248755331 : && TREE_CODE (op0) == BIT_AND_EXPR
9358 714658172 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9359 : {
9360 217516 : tree and_expr = op0;
9361 217516 : tree and0 = TREE_OPERAND (and_expr, 0);
9362 217516 : tree and1 = TREE_OPERAND (and_expr, 1);
9363 217516 : int change = 0;
9364 :
9365 217516 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9366 217516 : || (TYPE_PRECISION (type)
9367 59201 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9368 : change = 1;
9369 18052 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9370 : <= HOST_BITS_PER_WIDE_INT
9371 18052 : && tree_fits_uhwi_p (and1))
9372 : {
9373 16938 : unsigned HOST_WIDE_INT cst;
9374 :
9375 16938 : cst = tree_to_uhwi (and1);
9376 33876 : cst &= HOST_WIDE_INT_M1U
9377 16938 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9378 16938 : change = (cst == 0);
9379 16938 : if (change
9380 16938 : && !flag_syntax_only
9381 33876 : && (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 16938 : if (change)
9390 : {
9391 216402 : tree and1_type = TREE_TYPE (and1);
9392 216402 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9393 : TYPE_PRECISION (type));
9394 216402 : tem = force_fit_type (type,
9395 216402 : wide_int::from (wi::to_wide (and1), prec,
9396 216402 : TYPE_SIGN (and1_type)),
9397 216402 : 0, TREE_OVERFLOW (and1));
9398 216402 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9399 216402 : 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 713825340 : if (POINTER_TYPE_P (type)
9407 430042971 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9408 746505374 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9409 : {
9410 30059756 : tree arg00 = TREE_OPERAND (arg0, 0);
9411 30059756 : 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 30059756 : if (!in_gimple_form
9417 30039232 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9418 30060970 : && (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 30059689 : if (TREE_CODE (type) == REFERENCE_TYPE
9427 16886920 : && !in_gimple_form
9428 16886900 : && sanitize_flags_p (SANITIZE_NULL)
9429 30060120 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9430 : return NULL_TREE;
9431 :
9432 30059258 : arg00 = fold_convert_loc (loc, type, arg00);
9433 30059258 : 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 683765584 : if (INTEGRAL_TYPE_P (type)
9440 255004794 : && TREE_CODE (op0) == BIT_NOT_EXPR
9441 585124 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9442 585124 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9443 684151187 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9444 : {
9445 383229 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9446 456815 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9447 456813 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9448 318161 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9449 318161 : 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 683447423 : if (INTEGRAL_TYPE_P (type)
9455 254686633 : && TREE_CODE (op0) == MULT_EXPR
9456 9304409 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9457 9283458 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9458 683509547 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9459 21137 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9460 : {
9461 : /* Be careful not to introduce new overflows. */
9462 62070 : tree mult_type;
9463 62070 : if (TYPE_OVERFLOW_WRAPS (type))
9464 : mult_type = type;
9465 : else
9466 2113 : mult_type = unsigned_type_for (type);
9467 :
9468 62070 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9469 : {
9470 124140 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9471 : fold_convert_loc (loc, mult_type,
9472 62070 : TREE_OPERAND (op0, 0)),
9473 : fold_convert_loc (loc, mult_type,
9474 62070 : TREE_OPERAND (op0, 1)));
9475 62070 : return fold_convert_loc (loc, type, tem);
9476 : }
9477 : }
9478 :
9479 : return NULL_TREE;
9480 :
9481 238308895 : case VIEW_CONVERT_EXPR:
9482 238308895 : if (TREE_CODE (op0) == MEM_REF)
9483 : {
9484 2678 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9485 18 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9486 2678 : tem = fold_build2_loc (loc, MEM_REF, type,
9487 2678 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9488 2678 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9489 2678 : return tem;
9490 : }
9491 :
9492 : return NULL_TREE;
9493 :
9494 4245160 : case NEGATE_EXPR:
9495 4245160 : tem = fold_negate_expr (loc, arg0);
9496 4245160 : if (tem)
9497 1680 : return fold_convert_loc (loc, type, tem);
9498 : return NULL_TREE;
9499 :
9500 3461296 : case ABS_EXPR:
9501 : /* Convert fabs((double)float) into (double)fabsf(float). */
9502 3461296 : if (TREE_CODE (arg0) == NOP_EXPR
9503 23337 : && TREE_CODE (type) == REAL_TYPE)
9504 : {
9505 23283 : tree targ0 = strip_float_extensions (arg0);
9506 23283 : if (targ0 != arg0)
9507 23079 : return fold_convert_loc (loc, type,
9508 : fold_build1_loc (loc, ABS_EXPR,
9509 23079 : TREE_TYPE (targ0),
9510 23079 : targ0));
9511 : }
9512 : return NULL_TREE;
9513 :
9514 2797636 : case BIT_NOT_EXPR:
9515 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9516 2797636 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9517 2799328 : && (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 2797622 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9524 2799300 : && (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 51293229 : 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 51293229 : tem = fold_truth_not_expr (loc, arg0);
9539 51293229 : if (!tem)
9540 : return NULL_TREE;
9541 34693651 : return fold_convert_loc (loc, type, tem);
9542 :
9543 70133369 : case INDIRECT_REF:
9544 : /* Fold *&X to X if X is an lvalue. */
9545 70133369 : if (TREE_CODE (op0) == ADDR_EXPR)
9546 : {
9547 7869 : tree op00 = TREE_OPERAND (op0, 0);
9548 7869 : if ((VAR_P (op00)
9549 : || TREE_CODE (op00) == PARM_DECL
9550 : || TREE_CODE (op00) == RESULT_DECL)
9551 6723 : && !TREE_READONLY (op00))
9552 6606 : 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 25766837 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9587 : tree arg0, tree arg1, tree op0, tree op1)
9588 : {
9589 25766837 : tree tem;
9590 :
9591 : /* We only do these simplifications if we are optimizing. */
9592 25766837 : 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 25283808 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9602 5954469 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9603 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9604 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9605 5954469 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9606 25346606 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9607 : {
9608 62302 : tree a00 = TREE_OPERAND (arg0, 0);
9609 62302 : tree a01 = TREE_OPERAND (arg0, 1);
9610 62302 : tree a10 = TREE_OPERAND (arg1, 0);
9611 62302 : tree a11 = TREE_OPERAND (arg1, 1);
9612 124604 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9613 62302 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9614 62302 : && (code == TRUTH_AND_EXPR
9615 23197 : || code == TRUTH_OR_EXPR));
9616 :
9617 62302 : 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 61453 : 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 61453 : 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 61409 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9631 122346 : && 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 25282916 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9639 : return tem;
9640 :
9641 24105142 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9642 24103134 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9643 : {
9644 40143 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9645 40143 : if (tem)
9646 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9647 : }
9648 :
9649 24105129 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9650 24094020 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9651 : {
9652 89675 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9653 89675 : 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 24105038 : if (TREE_CODE (arg0) == code
9661 24954829 : && (tem = fold_truth_andor_1 (loc, code, type,
9662 849791 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9663 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9664 :
9665 24104953 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9666 : return tem;
9667 :
9668 24065236 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9669 24065236 : if (param_logical_op_non_short_circuit != -1)
9670 7776 : logical_op_non_short_circuit
9671 7776 : = param_logical_op_non_short_circuit;
9672 24065236 : if (logical_op_non_short_circuit
9673 24061304 : && !sanitize_coverage_p ()
9674 24065236 : && (code == TRUTH_AND_EXPR
9675 24061301 : || code == TRUTH_ANDIF_EXPR
9676 11094794 : || code == TRUTH_OR_EXPR
9677 11094794 : || code == TRUTH_ORIF_EXPR))
9678 : {
9679 24061301 : enum tree_code ncode, icode;
9680 :
9681 24061301 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9682 24061301 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9683 11094794 : 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 24061301 : if (TREE_CODE (arg0) == icode
9695 840773 : && simple_condition_p (arg1)
9696 : /* Needed for sequence points to handle trappings, and
9697 : side-effects. */
9698 24110364 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9699 : {
9700 42208 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9701 : arg1);
9702 42208 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9703 42208 : 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 24019093 : 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 24020046 : && 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 5369520 : else if (code == icode && simple_condition_p (arg0)
9723 24914187 : && simple_condition_p (arg1))
9724 445404 : 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 181888634 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9738 : tree arg0, tree arg1)
9739 : {
9740 181888634 : enum tree_code code0 = TREE_CODE (arg0);
9741 181888634 : tree t, cst0 = NULL_TREE;
9742 181888634 : int sgn0;
9743 :
9744 : /* Match A +- CST code arg1. We can change this only if overflow
9745 : is undefined. */
9746 181888634 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9747 138536851 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9748 : /* In principle pointers also have undefined overflow behavior,
9749 : but that causes problems elsewhere. */
9750 68122095 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9751 68122095 : && (code0 == MINUS_EXPR
9752 68122095 : || code0 == PLUS_EXPR)
9753 2606807 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9754 : return NULL_TREE;
9755 :
9756 : /* Identify the constant in arg0 and its sign. */
9757 2129681 : cst0 = TREE_OPERAND (arg0, 1);
9758 2129681 : sgn0 = tree_int_cst_sgn (cst0);
9759 :
9760 : /* Overflowed constants and zero will cause problems. */
9761 2129681 : if (integer_zerop (cst0)
9762 2129681 : || 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 2129681 : if (code == LT_EXPR
9769 1260270 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9770 : code = LE_EXPR;
9771 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9772 1950282 : else if (code == GT_EXPR
9773 580878 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9774 : code = GE_EXPR;
9775 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9776 1766261 : else if (code == LE_EXPR
9777 677189 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9778 : code = LT_EXPR;
9779 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9780 1526191 : else if (code == GE_EXPR
9781 481832 : && 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 1614040 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9789 1614040 : && ((sgn0 == 1
9790 423953 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9791 423953 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9792 807020 : || (sgn0 == -1
9793 383067 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9794 383067 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9795 : return NULL_TREE;
9796 :
9797 1230973 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9798 807020 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9799 807020 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9800 807020 : t = fold_convert (TREE_TYPE (arg1), t);
9801 :
9802 807020 : 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 91326083 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9813 : tree arg0, tree arg1)
9814 : {
9815 91326083 : tree t;
9816 :
9817 : /* Try canonicalization by simplifying arg0. */
9818 91326083 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9819 91326083 : if (t)
9820 : return t;
9821 :
9822 : /* Try canonicalization by simplifying arg1 using the swapped
9823 : comparison. */
9824 90562551 : code = swap_tree_comparison (code);
9825 90562551 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9826 90562551 : 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 12612656 : maybe_nonzero_address (tree decl)
9836 : {
9837 12612656 : 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 10388969 : if (decl_in_symtab_p (decl))
9845 : {
9846 4551598 : if (struct symtab_node *symbol
9847 4551598 : = (folding_initializer
9848 4551598 : ? symtab_node::get_create (decl)
9849 4534418 : : symtab_node::get (decl)))
9850 4532577 : return symbol->nonzero_address ();
9851 : }
9852 5837371 : 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 5702535 : if (DECL_CONTEXT (decl)
9858 5685026 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9859 11384094 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9860 5601378 : 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 91397547 : fold_comparison (location_t loc, enum tree_code code, tree type,
9875 : tree op0, tree op1)
9876 : {
9877 91397547 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9878 91397547 : tree arg0, arg1, tem;
9879 :
9880 91397547 : arg0 = op0;
9881 91397547 : arg1 = op1;
9882 :
9883 91397547 : STRIP_SIGN_NOPS (arg0);
9884 91397547 : 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 169082146 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9891 91618666 : && (TREE_CODE (arg0) == ADDR_EXPR
9892 13879846 : || TREE_CODE (arg1) == ADDR_EXPR
9893 12300407 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9894 11508183 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9895 : {
9896 2435912 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9897 2435912 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9898 2435912 : machine_mode mode;
9899 2435912 : int volatilep, reversep, unsignedp;
9900 2435912 : 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 2435912 : base0 = arg0;
9907 2435912 : if (TREE_CODE (arg0) == ADDR_EXPR)
9908 : {
9909 54221 : base0
9910 54221 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9911 : &bitsize, &bitpos0, &offset0, &mode,
9912 : &unsignedp, &reversep, &volatilep);
9913 54221 : if (INDIRECT_REF_P (base0))
9914 2253 : base0 = TREE_OPERAND (base0, 0);
9915 : else
9916 : indirect_base0 = true;
9917 : }
9918 2381691 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9919 : {
9920 854042 : base0 = TREE_OPERAND (arg0, 0);
9921 854042 : STRIP_SIGN_NOPS (base0);
9922 854042 : if (TREE_CODE (base0) == ADDR_EXPR)
9923 : {
9924 28042 : base0
9925 28042 : = get_inner_reference (TREE_OPERAND (base0, 0),
9926 : &bitsize, &bitpos0, &offset0, &mode,
9927 : &unsignedp, &reversep, &volatilep);
9928 28042 : if (INDIRECT_REF_P (base0))
9929 17 : base0 = TREE_OPERAND (base0, 0);
9930 : else
9931 : indirect_base0 = true;
9932 : }
9933 854042 : if (offset0 == NULL_TREE || integer_zerop (offset0))
9934 854042 : offset0 = TREE_OPERAND (arg0, 1);
9935 : else
9936 0 : offset0 = size_binop (PLUS_EXPR, offset0,
9937 : TREE_OPERAND (arg0, 1));
9938 854042 : if (poly_int_tree_p (offset0))
9939 : {
9940 686575 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
9941 686575 : TYPE_PRECISION (sizetype));
9942 686575 : tem <<= LOG2_BITS_PER_UNIT;
9943 686575 : tem += bitpos0;
9944 686575 : if (tem.to_shwi (&bitpos0))
9945 686575 : offset0 = NULL_TREE;
9946 : }
9947 : }
9948 :
9949 2435912 : base1 = arg1;
9950 2435912 : if (TREE_CODE (arg1) == ADDR_EXPR)
9951 : {
9952 1609464 : base1
9953 1609464 : = get_inner_reference (TREE_OPERAND (arg1, 0),
9954 : &bitsize, &bitpos1, &offset1, &mode,
9955 : &unsignedp, &reversep, &volatilep);
9956 1609464 : if (INDIRECT_REF_P (base1))
9957 70758 : base1 = TREE_OPERAND (base1, 0);
9958 : else
9959 : indirect_base1 = true;
9960 : }
9961 826448 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9962 : {
9963 91918 : base1 = TREE_OPERAND (arg1, 0);
9964 91918 : STRIP_SIGN_NOPS (base1);
9965 91918 : if (TREE_CODE (base1) == ADDR_EXPR)
9966 : {
9967 11118 : base1
9968 11118 : = get_inner_reference (TREE_OPERAND (base1, 0),
9969 : &bitsize, &bitpos1, &offset1, &mode,
9970 : &unsignedp, &reversep, &volatilep);
9971 11118 : if (INDIRECT_REF_P (base1))
9972 0 : base1 = TREE_OPERAND (base1, 0);
9973 : else
9974 : indirect_base1 = true;
9975 : }
9976 91918 : if (offset1 == NULL_TREE || integer_zerop (offset1))
9977 91894 : offset1 = TREE_OPERAND (arg1, 1);
9978 : else
9979 24 : offset1 = size_binop (PLUS_EXPR, offset1,
9980 : TREE_OPERAND (arg1, 1));
9981 91918 : if (poly_int_tree_p (offset1))
9982 : {
9983 81377 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
9984 81377 : TYPE_PRECISION (sizetype));
9985 81377 : tem <<= LOG2_BITS_PER_UNIT;
9986 81377 : tem += bitpos1;
9987 81377 : if (tem.to_shwi (&bitpos1))
9988 81377 : offset1 = NULL_TREE;
9989 : }
9990 : }
9991 :
9992 : /* If we have equivalent bases we might be able to simplify. */
9993 2435912 : if (indirect_base0 == indirect_base1
9994 3278478 : && 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 16766 : if ((offset0 == offset1
10000 6676 : || (offset0 && offset1
10001 2722 : && operand_equal_p (offset0, offset1, 0)))
10002 16766 : && (equality_code
10003 10048 : || (indirect_base0
10004 6242 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10005 3806 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10006 : {
10007 10050 : switch (code)
10008 : {
10009 33 : case EQ_EXPR:
10010 33 : if (known_eq (bitpos0, bitpos1))
10011 49166 : return constant_boolean_node (true, type);
10012 21 : if (known_ne (bitpos0, bitpos1))
10013 21 : return constant_boolean_node (false, type);
10014 : break;
10015 9 : case NE_EXPR:
10016 9 : if (known_ne (bitpos0, bitpos1))
10017 7 : return constant_boolean_node (true, type);
10018 2 : if (known_eq (bitpos0, bitpos1))
10019 2 : return constant_boolean_node (false, type);
10020 : break;
10021 2399 : case LT_EXPR:
10022 2399 : if (known_lt (bitpos0, bitpos1))
10023 2263 : 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 3543 : case GE_EXPR:
10034 3543 : if (known_ge (bitpos0, bitpos1))
10035 1376 : return constant_boolean_node (true, type);
10036 2167 : if (known_lt (bitpos0, bitpos1))
10037 2167 : return constant_boolean_node (false, type);
10038 : break;
10039 2410 : case GT_EXPR:
10040 2410 : if (known_gt (bitpos0, bitpos1))
10041 2351 : 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 6716 : else if (known_eq (bitpos0, bitpos1)
10056 6716 : && (equality_code
10057 5239 : || (indirect_base0
10058 271 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10059 4968 : || 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 5348 : if (offset0 == NULL_TREE)
10067 0 : offset0 = build_int_cst (ssizetype, 0);
10068 : else
10069 5348 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10070 5348 : if (offset1 == NULL_TREE)
10071 2658 : offset1 = build_int_cst (ssizetype, 0);
10072 : else
10073 2690 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10074 :
10075 5348 : 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 2419146 : else if (known_eq (bitpos0, bitpos1)
10081 56479 : && (indirect_base0
10082 1010127 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10083 15983 : && (indirect_base1
10084 192656 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10085 2649888 : && ((offset0 == offset1)
10086 4382 : || (offset0 && offset1
10087 4112 : && operand_equal_p (offset0, offset1, 0))))
10088 : {
10089 33705 : if (indirect_base0)
10090 3815 : base0 = build_fold_addr_expr_loc (loc, base0);
10091 33705 : if (indirect_base1)
10092 5449 : base1 = build_fold_addr_expr_loc (loc, base1);
10093 33705 : 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 2385441 : else if (((DECL_P (base0)
10103 257753 : && 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 243001 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10110 2369229 : || CONSTANT_CLASS_P (base0))
10111 21016 : && 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 2403422 : && 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 179925950 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10137 139344988 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10138 34465115 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10139 2353119 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10140 1930214 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10141 1930214 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10142 161184715 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10143 161471 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10144 : {
10145 161471 : tree const1 = TREE_OPERAND (arg0, 1);
10146 161471 : tree const2 = TREE_OPERAND (arg1, 1);
10147 161471 : tree variable1 = TREE_OPERAND (arg0, 0);
10148 161471 : tree variable2 = TREE_OPERAND (arg1, 0);
10149 161471 : 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 161472 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10154 : ? MINUS_EXPR : PLUS_EXPR,
10155 : const2, const1);
10156 161471 : if (!TREE_OVERFLOW (cst)
10157 161455 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10158 183769 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10159 5654 : return fold_build2_loc (loc, code, type,
10160 : variable1,
10161 5654 : fold_build2_loc (loc, TREE_CODE (arg1),
10162 5654 : TREE_TYPE (arg1),
10163 5654 : variable2, cst));
10164 :
10165 155818 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10166 : ? MINUS_EXPR : PLUS_EXPR,
10167 : const1, const2);
10168 155817 : if (!TREE_OVERFLOW (cst)
10169 155801 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10170 172461 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10171 16644 : return fold_build2_loc (loc, code, type,
10172 16644 : fold_build2_loc (loc, TREE_CODE (arg0),
10173 16644 : TREE_TYPE (arg0),
10174 : variable1, cst),
10175 16644 : variable2);
10176 : }
10177 :
10178 91326083 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10179 91326083 : 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 90519063 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10196 : {
10197 55272912 : tree cval1 = 0, cval2 = 0;
10198 :
10199 55272912 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10200 : /* Don't handle degenerate cases here; they should already
10201 : have been handled anyway. */
10202 618402 : && cval1 != 0 && cval2 != 0
10203 617102 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10204 617102 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10205 617096 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10206 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10207 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10208 55272966 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10209 54 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10210 : {
10211 54 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10212 54 : 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 54 : tree high_result
10218 54 : = fold_build2_loc (loc, code, type,
10219 : eval_subst (loc, arg0, cval1, maxval,
10220 : cval2, minval),
10221 : arg1);
10222 54 : tree equal_result
10223 54 : = fold_build2_loc (loc, code, type,
10224 : eval_subst (loc, arg0, cval1, maxval,
10225 : cval2, maxval),
10226 : arg1);
10227 54 : tree low_result
10228 54 : = 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 54 : if (TREE_CODE (high_result) == INTEGER_CST
10237 54 : && TREE_CODE (equal_result) == INTEGER_CST
10238 44 : && 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 44 : switch ((integer_onep (high_result) * 4)
10243 44 : + (integer_onep (equal_result) * 2)
10244 44 : + integer_onep (low_result))
10245 : {
10246 21 : case 0:
10247 : /* Always false. */
10248 44 : 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 5 : case 5:
10262 5 : code = NE_EXPR;
10263 5 : 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 7 : 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 31497 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10324 : {
10325 31497 : unsigned HOST_WIDE_INT i, nunits;
10326 :
10327 31497 : if (TREE_CODE (arg) == VECTOR_CST
10328 31497 : && 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 31039 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10334 : {
10335 : constructor_elt *elt;
10336 :
10337 56812 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10338 46188 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10339 31497 : return false;
10340 : else
10341 20352 : elts[i] = elt->value;
10342 : }
10343 : else
10344 : return false;
10345 6435 : 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 30121 : 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 30121 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10362 30121 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10363 :
10364 60242 : if (!(pow2p_hwi (sel_npatterns)
10365 30121 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10366 30121 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10367 : {
10368 0 : if (reason)
10369 0 : *reason = "npatterns is not power of 2";
10370 : 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 30121 : poly_uint64 esel;
10376 30121 : 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 : return false;
10381 : }
10382 :
10383 30121 : if (sel_nelts_per_pattern < 3)
10384 : return true;
10385 :
10386 6036 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10387 : {
10388 4558 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10389 4558 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10390 4558 : HOST_WIDE_INT step;
10391 4558 : if (!poly_int64 (a2 - a1).is_constant (&step))
10392 : {
10393 : if (reason)
10394 : *reason = "step is not constant";
10395 30121 : return false;
10396 : }
10397 : // FIXME: Punt on step < 0 for now, revisit later.
10398 4558 : if (step < 0)
10399 : return false;
10400 4502 : if (step == 0)
10401 0 : continue;
10402 :
10403 4502 : if (!pow2p_hwi (step))
10404 : {
10405 0 : if (reason)
10406 0 : *reason = "step is not power of 2";
10407 : return false;
10408 : }
10409 :
10410 : /* Ensure that stepped sequence of the pattern selects elements
10411 : only from the same input vector. */
10412 4502 : uint64_t q1, qe;
10413 4502 : poly_uint64 r1, re;
10414 4502 : poly_uint64 ae = a1 + (esel - 2) * step;
10415 4502 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10416 :
10417 4502 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10418 4502 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10419 : && q1 == qe))
10420 : {
10421 449 : if (reason)
10422 0 : *reason = "crossed input vectors";
10423 : return false;
10424 : }
10425 :
10426 : /* Ensure that the stepped sequence always selects from the same
10427 : input pattern. */
10428 4053 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10429 4053 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10430 :
10431 4053 : if (!multiple_p (step, arg_npatterns))
10432 : {
10433 628 : if (reason)
10434 0 : *reason = "step is not multiple of npatterns";
10435 : 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 3425 : if (maybe_lt (r1, arg_npatterns))
10443 : {
10444 44 : unsigned HOST_WIDE_INT index;
10445 44 : if (!r1.is_constant (&index))
10446 30121 : return false;
10447 :
10448 44 : tree arg_elem0 = vector_cst_elt (arg, index);
10449 44 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10450 44 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10451 :
10452 44 : tree step1, step2;
10453 44 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10454 44 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10455 88 : || !operand_equal_p (step1, step2, 0))
10456 : {
10457 2 : if (reason)
10458 0 : *reason = "not a natural stepped sequence";
10459 : 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 30121 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10474 : const char **reason = NULL)
10475 : {
10476 30121 : unsigned res_npatterns, res_nelts_per_pattern;
10477 30121 : 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 30121 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10504 : {
10505 28986 : res_npatterns = sel.encoding ().npatterns ();
10506 28986 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10507 28986 : if (res_nelts_per_pattern == 3
10508 1478 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10509 29939 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10510 : res_nelts_per_pattern = 2;
10511 28986 : res_nelts = res_npatterns * res_nelts_per_pattern;
10512 : }
10513 1135 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10514 : {
10515 1135 : res_npatterns = res_nelts;
10516 1135 : res_nelts_per_pattern = 1;
10517 : }
10518 : else
10519 : return NULL_TREE;
10520 :
10521 30121 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10522 181744 : for (unsigned i = 0; i < res_nelts; i++)
10523 : {
10524 121502 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10525 121502 : uint64_t q;
10526 121502 : poly_uint64 r;
10527 121502 : 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 121502 : 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 121502 : if (!r.is_constant (&index))
10547 : {
10548 : if (reason)
10549 : *reason = "remainder is not constant";
10550 : return NULL_TREE;
10551 : }
10552 :
10553 121502 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10554 121502 : tree elem = vector_cst_elt (arg, index);
10555 121502 : out_elts.quick_push (elem);
10556 : }
10557 :
10558 30121 : return out_elts.build ();
10559 30121 : }
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 69380 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10567 : {
10568 69380 : unsigned int i;
10569 69380 : unsigned HOST_WIDE_INT nelts;
10570 :
10571 69380 : 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 69380 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10576 69380 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10577 : return NULL_TREE;
10578 :
10579 58771 : if (TREE_CODE (arg0) == VECTOR_CST
10580 30395 : && TREE_CODE (arg1) == VECTOR_CST)
10581 30121 : 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 28650 : if (!sel.length ().is_constant (&nelts)
10586 28650 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10587 0 : return NULL_TREE;
10588 :
10589 28650 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10590 28650 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10591 28650 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10592 : return NULL_TREE;
10593 :
10594 2814 : vec<constructor_elt, va_gc> *v;
10595 2814 : vec_alloc (v, nelts);
10596 16914 : for (i = 0; i < nelts; i++)
10597 : {
10598 11286 : HOST_WIDE_INT index;
10599 11286 : if (!sel[i].is_constant (&index))
10600 : return NULL_TREE;
10601 11286 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10602 : }
10603 2814 : 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 1163856 : exact_inverse (tree type, tree cst)
10658 : {
10659 1163856 : REAL_VALUE_TYPE r;
10660 1163856 : tree unit_type;
10661 1163856 : machine_mode mode;
10662 :
10663 1163856 : switch (TREE_CODE (cst))
10664 : {
10665 1163239 : case REAL_CST:
10666 1163239 : r = TREE_REAL_CST (cst);
10667 :
10668 1163239 : if (exact_real_inverse (TYPE_MODE (type), &r))
10669 333418 : 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 168224 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10702 : {
10703 168224 : int tz = wi::ctz (y);
10704 168224 : if (tz > 0)
10705 6537 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10706 161687 : 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 150894080 : tree_expr_nonzero_p (tree t)
10715 : {
10716 151250099 : tree type = TREE_TYPE (t);
10717 151250099 : enum tree_code code;
10718 :
10719 : /* Doing something useful for floating point would need more work. */
10720 151250099 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10721 : return false;
10722 :
10723 151125803 : code = TREE_CODE (t);
10724 151125803 : switch (TREE_CODE_CLASS (code))
10725 : {
10726 984259 : case tcc_unary:
10727 984259 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10728 2938169 : case tcc_binary:
10729 2938169 : case tcc_comparison:
10730 2938169 : return tree_binary_nonzero_p (code, type,
10731 2938169 : TREE_OPERAND (t, 0),
10732 5876338 : TREE_OPERAND (t, 1));
10733 13328778 : case tcc_constant:
10734 13328778 : case tcc_declaration:
10735 13328778 : case tcc_reference:
10736 13328778 : return tree_single_nonzero_p (t);
10737 :
10738 133874597 : default:
10739 133874597 : break;
10740 : }
10741 :
10742 133874597 : switch (code)
10743 : {
10744 616676 : case TRUTH_NOT_EXPR:
10745 616676 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10746 :
10747 71218 : case TRUTH_AND_EXPR:
10748 71218 : case TRUTH_OR_EXPR:
10749 71218 : case TRUTH_XOR_EXPR:
10750 71218 : return tree_binary_nonzero_p (code, type,
10751 71218 : TREE_OPERAND (t, 0),
10752 142436 : TREE_OPERAND (t, 1));
10753 :
10754 129671680 : case COND_EXPR:
10755 129671680 : case CONSTRUCTOR:
10756 129671680 : case OBJ_TYPE_REF:
10757 129671680 : case ADDR_EXPR:
10758 129671680 : case WITH_SIZE_EXPR:
10759 129671680 : case SSA_NAME:
10760 129671680 : return tree_single_nonzero_p (t);
10761 :
10762 85032 : case COMPOUND_EXPR:
10763 85032 : case MODIFY_EXPR:
10764 85032 : case BIND_EXPR:
10765 85032 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10766 :
10767 270987 : case SAVE_EXPR:
10768 270987 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10769 :
10770 3102884 : case CALL_EXPR:
10771 3102884 : {
10772 3102884 : tree fndecl = get_callee_fndecl (t);
10773 3102884 : if (!fndecl) return false;
10774 3100905 : if (flag_delete_null_pointer_checks && !flag_check_new
10775 3100905 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10776 3101623 : && !TREE_NOTHROW (fndecl))
10777 : return true;
10778 3101623 : if (flag_delete_null_pointer_checks
10779 6202528 : && lookup_attribute ("returns_nonnull",
10780 3100905 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10781 : return true;
10782 3101615 : 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 103764975 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10797 : {
10798 103764975 : int_range_max vr;
10799 103764975 : switch (TREE_CODE (t))
10800 : {
10801 2481694 : case INTEGER_CST:
10802 2481694 : return wi::to_wide (t) != w;
10803 :
10804 100849529 : case SSA_NAME:
10805 100849529 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10806 : return false;
10807 :
10808 201699058 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10809 100849529 : 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 100711823 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10814 201423214 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10815 1 : return true;
10816 : return false;
10817 :
10818 : default:
10819 : return false;
10820 : }
10821 103764975 : }
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 972602189 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10830 : tree op0, tree op1)
10831 : {
10832 972602189 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10833 972602189 : tree arg0, arg1, tem;
10834 972602189 : tree t1 = NULL_TREE;
10835 972602189 : unsigned int prec;
10836 :
10837 972602189 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10838 : && TREE_CODE_LENGTH (code) == 2
10839 : && op0 != NULL_TREE
10840 : && op1 != NULL_TREE);
10841 :
10842 972602189 : arg0 = op0;
10843 972602189 : 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 972602189 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10859 : {
10860 214300456 : STRIP_SIGN_NOPS (arg0);
10861 214300456 : STRIP_SIGN_NOPS (arg1);
10862 : }
10863 : else
10864 : {
10865 758301733 : STRIP_NOPS (arg0);
10866 758301733 : 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 972602189 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10872 : {
10873 273182181 : tem = const_binop (code, type, arg0, arg1);
10874 273182181 : if (tem != NULL_TREE)
10875 : {
10876 270604704 : if (TREE_TYPE (tem) != type)
10877 4399038 : tem = fold_convert_loc (loc, type, tem);
10878 : 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 701997485 : if (commutative_tree_code (code)
10885 701997485 : && tree_swap_operands_p (arg0, arg1))
10886 33389785 : 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 668607700 : if (kind == tcc_comparison
10891 668607700 : && tree_swap_operands_p (arg0, arg1))
10892 8421062 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10893 :
10894 660186638 : tem = generic_simplify (loc, code, type, op0, op1);
10895 660186638 : 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 558231942 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10915 : || code == EQ_EXPR || code == NE_EXPR)
10916 59144005 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10917 58556023 : && ((truth_value_p (TREE_CODE (arg0))
10918 1250549 : && (truth_value_p (TREE_CODE (arg1))
10919 929202 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10920 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10921 58234660 : || (truth_value_p (TREE_CODE (arg1))
10922 6832 : && (truth_value_p (TREE_CODE (arg0))
10923 6832 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10924 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10925 : {
10926 401893 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10927 80516 : : 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 321377 : if (code == EQ_EXPR)
10934 73321 : tem = invert_truthvalue_loc (loc, tem);
10935 :
10936 321377 : return fold_convert_loc (loc, type, tem);
10937 : }
10938 :
10939 557910565 : if (TREE_CODE_CLASS (code) == tcc_binary
10940 306066670 : || TREE_CODE_CLASS (code) == tcc_comparison)
10941 : {
10942 349676043 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
10943 : {
10944 82871 : tem = fold_build2_loc (loc, code, type,
10945 82871 : fold_convert_loc (loc, TREE_TYPE (op0),
10946 82871 : TREE_OPERAND (arg0, 1)), op1);
10947 82871 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10948 82871 : tem);
10949 : }
10950 349593172 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
10951 : {
10952 3163 : tem = fold_build2_loc (loc, code, type, op0,
10953 3163 : fold_convert_loc (loc, TREE_TYPE (op1),
10954 3163 : TREE_OPERAND (arg1, 1)));
10955 3163 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10956 3163 : tem);
10957 : }
10958 :
10959 349590009 : if (TREE_CODE (arg0) == COND_EXPR
10960 349195376 : || TREE_CODE (arg0) == VEC_COND_EXPR
10961 349192118 : || COMPARISON_CLASS_P (arg0))
10962 : {
10963 739377 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10964 : arg0, arg1,
10965 : /*cond_first_p=*/1);
10966 739377 : if (tem != NULL_TREE)
10967 : return tem;
10968 : }
10969 :
10970 349101038 : if (TREE_CODE (arg1) == COND_EXPR
10971 348883728 : || TREE_CODE (arg1) == VEC_COND_EXPR
10972 348883258 : || COMPARISON_CLASS_P (arg1))
10973 : {
10974 230606 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10975 : arg1, arg0,
10976 : /*cond_first_p=*/0);
10977 230606 : if (tem != NULL_TREE)
10978 : return tem;
10979 : }
10980 : }
10981 :
10982 557327471 : switch (code)
10983 : {
10984 64673850 : case MEM_REF:
10985 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10986 64673850 : if (TREE_CODE (arg0) == ADDR_EXPR
10987 64673850 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10988 : {
10989 836545 : tree iref = TREE_OPERAND (arg0, 0);
10990 836545 : 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 63837305 : if (TREE_CODE (arg0) == ADDR_EXPR
10998 63837305 : && handled_component_p (TREE_OPERAND (arg0, 0)))
10999 : {
11000 6231255 : tree base;
11001 6231255 : poly_int64 coffset;
11002 6231255 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11003 : &coffset);
11004 6231255 : if (!base)
11005 : return NULL_TREE;
11006 6227223 : 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 73313737 : case POINTER_PLUS_EXPR:
11015 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11016 146627058 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11017 146618279 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11018 33111 : 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 33111 : arg0)));
11024 :
11025 : return NULL_TREE;
11026 :
11027 63831832 : case PLUS_EXPR:
11028 63831832 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11029 : {
11030 : /* X + (X / CST) * -CST is X % CST. */
11031 52257928 : if (TREE_CODE (arg1) == MULT_EXPR
11032 2321046 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11033 52264179 : && operand_equal_p (arg0,
11034 6251 : 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 63831628 : if ((TREE_CODE (arg0) == MULT_EXPR
11053 51785094 : || TREE_CODE (arg1) == MULT_EXPR)
11054 13385380 : && !TYPE_SATURATING (type)
11055 13385380 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11056 12989360 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11057 76110911 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11058 : {
11059 8977920 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11060 8977920 : if (tem)
11061 : return tem;
11062 : }
11063 :
11064 62460974 : 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 283351 : if (ANY_INTEGRAL_TYPE_P (type)
11070 50889434 : && TYPE_OVERFLOW_WRAPS (type)
11071 50889434 : && (((TREE_CODE (arg0) == PLUS_EXPR
11072 32082369 : || TREE_CODE (arg0) == MINUS_EXPR)
11073 3462634 : && TREE_CODE (arg1) == MULT_EXPR)
11074 31578446 : || ((TREE_CODE (arg1) == PLUS_EXPR
11075 31578446 : || TREE_CODE (arg1) == MINUS_EXPR)
11076 428556 : && TREE_CODE (arg0) == MULT_EXPR)))
11077 : {
11078 550528 : tree parg0, parg1, parg, marg;
11079 550528 : enum tree_code pcode;
11080 :
11081 550528 : if (TREE_CODE (arg1) == MULT_EXPR)
11082 : parg = arg0, marg = arg1;
11083 : else
11084 46605 : parg = arg1, marg = arg0;
11085 550528 : pcode = TREE_CODE (parg);
11086 550528 : parg0 = TREE_OPERAND (parg, 0);
11087 550528 : parg1 = TREE_OPERAND (parg, 1);
11088 550528 : STRIP_NOPS (parg0);
11089 550528 : STRIP_NOPS (parg1);
11090 :
11091 550528 : if (TREE_CODE (parg0) == MULT_EXPR
11092 267544 : && TREE_CODE (parg1) != MULT_EXPR)
11093 233083 : 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 233083 : fold_convert_loc (loc, type, parg1));
11100 317445 : if (TREE_CODE (parg0) != MULT_EXPR
11101 282984 : && TREE_CODE (parg1) == MULT_EXPR)
11102 100142 : return
11103 100142 : 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 100142 : 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 11571540 : if (!HONOR_SNANS (arg0)
11117 11569880 : && !HONOR_SIGNED_ZEROS (arg0)
11118 11593064 : && 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 11571465 : if (flag_associative_math
11152 21392 : && TREE_CODE (arg1) == PLUS_EXPR
11153 38 : && TREE_CODE (arg0) != MULT_EXPR)
11154 : {
11155 23 : tree tree10 = TREE_OPERAND (arg1, 0);
11156 23 : tree tree11 = TREE_OPERAND (arg1, 1);
11157 23 : 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 11571464 : if (flag_associative_math
11169 21391 : && TREE_CODE (arg0) == PLUS_EXPR
11170 1284 : && TREE_CODE (arg1) != MULT_EXPR)
11171 : {
11172 860 : tree tree00 = TREE_OPERAND (arg0, 0);
11173 860 : tree tree01 = TREE_OPERAND (arg0, 1);
11174 860 : 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 11570604 : 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 65032674 : {
11193 65032674 : enum tree_code code0, code1;
11194 65032674 : tree rtype;
11195 65032674 : code0 = TREE_CODE (arg0);
11196 65032674 : code1 = TREE_CODE (arg1);
11197 74404 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11198 65016295 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11199 39763 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11200 39763 : TREE_OPERAND (arg1, 0), 0)
11201 36994 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11202 36994 : TYPE_UNSIGNED (rtype))
11203 : /* Only create rotates in complete modes. Other cases are not
11204 : expanded properly. */
11205 65059570 : && (element_precision (rtype)
11206 53792 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11207 : {
11208 26823 : tree tree01, tree11;
11209 26823 : tree orig_tree01, orig_tree11;
11210 26823 : enum tree_code code01, code11;
11211 :
11212 26823 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11213 26823 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11214 26823 : STRIP_NOPS (tree01);
11215 26823 : STRIP_NOPS (tree11);
11216 26823 : code01 = TREE_CODE (tree01);
11217 26823 : code11 = TREE_CODE (tree11);
11218 26823 : if (code11 != MINUS_EXPR
11219 26137 : && (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 53646 : if (code01 == INTEGER_CST
11227 3152 : && code11 == INTEGER_CST
11228 29974 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11229 29974 : == 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 23812 : 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 22871 : else if (code == BIT_IOR_EXPR
11257 21757 : && code11 == BIT_AND_EXPR
11258 44553 : && pow2p_hwi (element_precision (rtype)))
11259 : {
11260 21682 : tree tree110, tree111;
11261 21682 : tree110 = TREE_OPERAND (tree11, 0);
11262 21682 : tree111 = TREE_OPERAND (tree11, 1);
11263 21682 : STRIP_NOPS (tree110);
11264 21682 : STRIP_NOPS (tree111);
11265 21682 : if (TREE_CODE (tree110) == NEGATE_EXPR
11266 21227 : && TREE_CODE (tree111) == INTEGER_CST
11267 21227 : && compare_tree_int (tree111,
11268 21227 : element_precision (rtype) - 1) == 0
11269 42895 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11270 : {
11271 31691 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11272 : ? LROTATE_EXPR : RROTATE_EXPR),
11273 21151 : rtype, TREE_OPERAND (arg0, 0),
11274 : orig_tree01);
11275 21151 : return fold_convert_loc (loc, type, tem);
11276 : }
11277 : }
11278 : }
11279 : }
11280 :
11281 158768879 : 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 158768879 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11289 117723582 : && !TYPE_SATURATING (type)
11290 276492461 : && !TYPE_OVERFLOW_SANITIZED (type))
11291 : {
11292 117691302 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11293 117691302 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11294 117691302 : tree atype = type;
11295 117691302 : 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 117691302 : var0 = split_tree (arg0, type, code,
11303 : &minus_var0, &con0, &minus_con0,
11304 : &lit0, &minus_lit0, 0);
11305 117691302 : 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 117691302 : if (code == MINUS_EXPR)
11311 12983439 : 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 117691071 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11316 234134326 : && !TYPE_OVERFLOW_WRAPS (type))
11317 : {
11318 62208556 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11319 61547385 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11320 883211 : atype = TREE_TYPE (arg0);
11321 60395881 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11322 60147787 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11323 245332 : atype = TREE_TYPE (arg1);
11324 31362558 : 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 117691071 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11330 234134326 : && !TYPE_OVERFLOW_WRAPS (atype))
11331 : {
11332 30234015 : 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 5637717 : tree tmp1 = var1 ? var1 : minus_var1;
11339 5637717 : bool one_neg = false;
11340 :
11341 5637717 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11342 : {
11343 739 : tmp0 = TREE_OPERAND (tmp0, 0);
11344 739 : one_neg = !one_neg;
11345 : }
11346 5011402 : if (CONVERT_EXPR_P (tmp0)
11347 652226 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11348 6288992 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11349 651275 : <= TYPE_PRECISION (atype)))
11350 638736 : tmp0 = TREE_OPERAND (tmp0, 0);
11351 5637717 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11352 : {
11353 168 : tmp1 = TREE_OPERAND (tmp1, 0);
11354 168 : one_neg = !one_neg;
11355 : }
11356 5310650 : if (CONVERT_EXPR_P (tmp1)
11357 396548 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11358 6034137 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11359 396420 : <= TYPE_PRECISION (atype)))
11360 379297 : tmp1 = TREE_OPERAND (tmp1, 0);
11361 : /* The only case we can still associate with two variables
11362 : is if they cancel out. */
11363 5637717 : if (!one_neg
11364 5637717 : || !operand_equal_p (tmp0, tmp1, 0))
11365 : ok = false;
11366 : }
11367 24209247 : else if ((var0 && minus_var1
11368 4113371 : && ! operand_equal_p (var0, minus_var1, 0))
11369 44692175 : || (minus_var0 && var1
11370 11419 : && ! 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 107928869 : && ((var0 != 0) + (var1 != 0)
11378 107928869 : + (minus_var0 != 0) + (minus_var1 != 0)
11379 107928869 : + (con0 != 0) + (con1 != 0)
11380 107928869 : + (minus_con0 != 0) + (minus_con1 != 0)
11381 107928869 : + (lit0 != 0) + (lit1 != 0)
11382 107928869 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11383 : {
11384 2081114 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11385 4162228 : int minus_var0_origin
11386 2081114 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11387 2081114 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11388 4162228 : int minus_con0_origin
11389 2081114 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11390 2081114 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11391 4162228 : int minus_lit0_origin
11392 2081114 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11393 2081114 : var0 = associate_trees (loc, var0, var1, code, atype);
11394 2081114 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11395 : code, atype);
11396 2081114 : con0 = associate_trees (loc, con0, con1, code, atype);
11397 2081114 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11398 : code, atype);
11399 2081114 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11400 2081114 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11401 : code, atype);
11402 :
11403 2081114 : if (minus_var0 && var0)
11404 : {
11405 1348884 : var0_origin |= minus_var0_origin;
11406 1348884 : var0 = associate_trees (loc, var0, minus_var0,
11407 : MINUS_EXPR, atype);
11408 1348884 : minus_var0 = 0;
11409 1348884 : minus_var0_origin = 0;
11410 : }
11411 2081114 : if (minus_con0 && con0)
11412 : {
11413 2705 : con0_origin |= minus_con0_origin;
11414 2705 : con0 = associate_trees (loc, con0, minus_con0,
11415 : MINUS_EXPR, atype);
11416 2705 : minus_con0 = 0;
11417 2705 : 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 2081114 : if (minus_lit0 && lit0)
11426 : {
11427 232933 : if (TREE_CODE (lit0) == INTEGER_CST
11428 232933 : && TREE_CODE (minus_lit0) == INTEGER_CST
11429 232933 : && tree_int_cst_lt (lit0, minus_lit0)
11430 : /* But avoid ending up with only negated parts. */
11431 290616 : && (var0 || con0))
11432 : {
11433 53163 : minus_lit0_origin |= lit0_origin;
11434 53163 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11435 : MINUS_EXPR, atype);
11436 53163 : lit0 = 0;
11437 53163 : lit0_origin = 0;
11438 : }
11439 : else
11440 : {
11441 179770 : lit0_origin |= minus_lit0_origin;
11442 179770 : lit0 = associate_trees (loc, lit0, minus_lit0,
11443 : MINUS_EXPR, atype);
11444 179770 : minus_lit0 = 0;
11445 179770 : minus_lit0_origin = 0;
11446 : }
11447 : }
11448 :
11449 : /* Don't introduce overflows through reassociation. */
11450 1386076 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11451 3467152 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11452 2081114 : return NULL_TREE;
11453 :
11454 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11455 2081076 : con0_origin |= lit0_origin;
11456 2081076 : con0 = associate_trees (loc, con0, lit0, code, atype);
11457 2081076 : minus_con0_origin |= minus_lit0_origin;
11458 2081076 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11459 : code, atype);
11460 :
11461 : /* Eliminate minus_con0. */
11462 2081076 : if (minus_con0)
11463 : {
11464 699890 : if (con0)
11465 : {
11466 15435 : con0_origin |= minus_con0_origin;
11467 15435 : con0 = associate_trees (loc, con0, minus_con0,
11468 : MINUS_EXPR, atype);
11469 : }
11470 684455 : else if (var0)
11471 : {
11472 684455 : var0_origin |= minus_con0_origin;
11473 684455 : 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 2081076 : if (minus_var0)
11482 : {
11483 374960 : if (con0)
11484 : {
11485 374960 : con0_origin |= minus_var0_origin;
11486 374960 : 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 2081076 : if (var0_origin != 3 && con0_origin != 3)
11498 : return NULL_TREE;
11499 :
11500 2079434 : return
11501 2079434 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11502 2079434 : code, atype));
11503 : }
11504 : }
11505 :
11506 : return NULL_TREE;
11507 :
11508 23852539 : case POINTER_DIFF_EXPR:
11509 23852539 : case MINUS_EXPR:
11510 : /* Fold &a[i] - &a[j] to i-j. */
11511 23852539 : if (TREE_CODE (arg0) == ADDR_EXPR
11512 44833 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11513 6048 : && TREE_CODE (arg1) == ADDR_EXPR
11514 23853147 : && 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 23852525 : 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 21066994 : if (TREE_CODE (arg0) == NEGATE_EXPR
11531 145622 : && 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 21067823 : && !(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 21066172 : if (!HONOR_SNANS (arg0)
11548 21065021 : && !HONOR_SIGNED_ZEROS (arg0)
11549 34886434 : && 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 21066148 : if (negate_expr_p (op1)
11583 781230 : && ! TYPE_OVERFLOW_SANITIZED (type)
11584 21844851 : && ((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 778638 : || INTEGRAL_TYPE_P (type)))
11589 778498 : return fold_build2_loc (loc, PLUS_EXPR, type,
11590 : fold_convert_loc (loc, type, arg0),
11591 778498 : 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 20287650 : if ((TREE_CODE (arg0) == MULT_EXPR
11598 18983226 : || TREE_CODE (arg1) == MULT_EXPR)
11599 2635762 : && !TYPE_SATURATING (type)
11600 2635762 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11601 2495829 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11602 22728686 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11603 : {
11604 348712 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11605 348712 : if (tem)
11606 : return tem;
11607 : }
11608 :
11609 20238061 : goto associate;
11610 :
11611 67183826 : case MULT_EXPR:
11612 67183826 : if (! FLOAT_TYPE_P (type))
11613 : {
11614 : /* Transform x * -C into -x * C if x is easily negatable. */
11615 44904214 : if (TREE_CODE (op1) == INTEGER_CST
11616 41821013 : && tree_int_cst_sgn (op1) == -1
11617 220886 : && negate_expr_p (op0)
11618 340 : && negate_expr_p (op1)
11619 324 : && (tem = negate_expr (op1)) != op1
11620 44904538 : && ! 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 44903890 : if (TREE_CODE (arg1) == INTEGER_CST
11626 44903890 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11627 : {
11628 733299 : return fold_convert_loc (loc, type, tem);
11629 : }
11630 :
11631 : /* Optimize z * conj(z) for integer complex numbers. */
11632 44170591 : if (TREE_CODE (arg0) == CONJ_EXPR
11633 44170591 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11634 1 : return fold_mult_zconjz (loc, type, arg1);
11635 44170590 : if (TREE_CODE (arg1) == CONJ_EXPR
11636 44170590 : && 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 22279612 : if (!HONOR_NANS (arg0)
11645 33288 : && !HONOR_SIGNED_ZEROS (arg0)
11646 32987 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11647 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11648 22279837 : && 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 22279394 : if (flag_unsafe_math_optimizations
11681 32810 : && TREE_CODE (arg0) == CONJ_EXPR
11682 22279396 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11683 1 : return fold_mult_zconjz (loc, type, arg1);
11684 22279393 : if (flag_unsafe_math_optimizations
11685 32809 : && TREE_CODE (arg1) == CONJ_EXPR
11686 22279397 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11687 0 : return fold_mult_zconjz (loc, type, arg0);
11688 : }
11689 66449983 : goto associate;
11690 :
11691 1932822 : case BIT_IOR_EXPR:
11692 : /* Canonicalize (X & C1) | C2. */
11693 1932822 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11694 89746 : && TREE_CODE (arg1) == INTEGER_CST
11695 1977008 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11696 : {
11697 44178 : int width = TYPE_PRECISION (type), w;
11698 44178 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11699 44178 : wide_int c2 = wi::to_wide (arg1);
11700 :
11701 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11702 44178 : if ((c1 & c2) == c1)
11703 0 : return omit_one_operand_loc (loc, type, arg1,
11704 0 : TREE_OPERAND (arg0, 0));
11705 :
11706 44178 : wide_int msk = wi::mask (width, false,
11707 44178 : TYPE_PRECISION (TREE_TYPE (arg1)));
11708 :
11709 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11710 44178 : 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 44172 : c1 &= msk;
11720 44172 : c2 &= msk;
11721 44172 : wide_int c3 = wi::bit_and_not (c1, c2);
11722 182934 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11723 : {
11724 94832 : wide_int mask = wi::mask (w, false,
11725 94832 : TYPE_PRECISION (type));
11726 189664 : if (((c1 | c2) & mask) == mask
11727 189664 : && wi::bit_and_not (c1, mask) == 0)
11728 : {
11729 242 : c3 = mask;
11730 242 : break;
11731 : }
11732 94832 : }
11733 :
11734 44172 : 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 45300 : }
11742 :
11743 : /* See if this can be simplified into a rotate first. If that
11744 : is unsuccessful continue in the association code. */
11745 1932258 : goto bit_rotate;
11746 :
11747 972752 : case BIT_XOR_EXPR:
11748 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11749 972752 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11750 3700 : && INTEGRAL_TYPE_P (type)
11751 3095 : && integer_onep (TREE_OPERAND (arg0, 1))
11752 973995 : && 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 972752 : goto bit_rotate;
11759 :
11760 6641860 : case BIT_AND_EXPR:
11761 : /* Fold !X & 1 as X == 0. */
11762 6641860 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11763 6641860 : && 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 6641860 : if (TREE_CODE (arg1) == INTEGER_CST)
11773 : {
11774 4788523 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11775 4788523 : wide_int ncst1 = -cst1;
11776 4788523 : if ((cst1 & ncst1) == ncst1
11777 4944150 : && multiple_of_p (type, arg0,
11778 4944150 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11779 467 : return fold_convert_loc (loc, type, arg0);
11780 4788523 : }
11781 :
11782 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11783 : bits from CST2. */
11784 6641393 : if (TREE_CODE (arg1) == INTEGER_CST
11785 4788056 : && TREE_CODE (arg0) == MULT_EXPR
11786 6809679 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11787 : {
11788 168224 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11789 168224 : wide_int masked
11790 168224 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11791 :
11792 168224 : if (masked == 0)
11793 5181 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11794 5181 : arg0, arg1);
11795 163043 : 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 168224 : }
11807 :
11808 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11809 4782249 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11810 6847527 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11811 : {
11812 114617 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11813 :
11814 114617 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11815 114617 : if (mask == -1)
11816 2507 : return
11817 2507 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11818 114617 : }
11819 :
11820 6633079 : goto associate;
11821 :
11822 6113049 : case RDIV_EXPR:
11823 : /* Don't touch a floating-point divide by zero unless the mode
11824 : of the constant can represent infinity. */
11825 6113049 : if (TREE_CODE (arg1) == REAL_CST
11826 3063845 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11827 6113049 : && real_zerop (arg1))
11828 0 : return NULL_TREE;
11829 :
11830 : /* (-A) / (-B) -> A / B */
11831 6113049 : 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 6113046 : 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 2173077 : case TRUNC_DIV_EXPR:
11842 : /* Fall through */
11843 :
11844 2173077 : 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 2173077 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11848 2173077 : && (TYPE_UNSIGNED (type)
11849 9 : || 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 3599511 : case ROUND_DIV_EXPR:
11868 3599511 : case CEIL_DIV_EXPR:
11869 3599511 : case EXACT_DIV_EXPR:
11870 3599511 : 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 3596477 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11876 1016713 : && TREE_CODE (op0) == NEGATE_EXPR
11877 3596539 : && 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 3596447 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11883 1016683 : && TREE_CODE (arg1) == NEGATE_EXPR
11884 3596691 : && 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 3596411 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11897 3596411 : && 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 3596411 : if (TREE_CODE (arg1) == INTEGER_CST
11903 3596411 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11904 9553 : return fold_convert_loc (loc, type, tem);
11905 :
11906 : return NULL_TREE;
11907 :
11908 896645 : case CEIL_MOD_EXPR:
11909 896645 : case FLOOR_MOD_EXPR:
11910 896645 : case ROUND_MOD_EXPR:
11911 896645 : case TRUNC_MOD_EXPR:
11912 896645 : if (TREE_CODE (arg1) == INTEGER_CST
11913 896645 : && (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 2253793 : case LROTATE_EXPR:
11919 2253793 : case RROTATE_EXPR:
11920 2253793 : case RSHIFT_EXPR:
11921 2253793 : case LSHIFT_EXPR:
11922 : /* Since negative shift count is not well-defined,
11923 : don't try to compute it in the compiler. */
11924 2253793 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11925 : return NULL_TREE;
11926 :
11927 2252752 : 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 2748 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11933 2218 : && (TREE_CODE (arg0) == BIT_AND_EXPR
11934 2218 : || TREE_CODE (arg0) == BIT_IOR_EXPR
11935 2218 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
11936 2252752 : && 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 439802 : case MIN_EXPR:
11950 439802 : case MAX_EXPR:
11951 439802 : goto associate;
11952 :
11953 6734342 : 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 6734342 : if (integer_zerop (arg0))
11959 1714381 : return fold_convert_loc (loc, type, arg0);
11960 : /* FALLTHRU */
11961 15896505 : case TRUTH_AND_EXPR:
11962 : /* If either arg is constant true, drop it. */
11963 15896505 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11964 1504741 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11965 832267 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11966 : /* Preserve sequence points. */
11967 15178519 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11968 761686 : 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 13630078 : if (integer_zerop (arg1))
11972 45512 : 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 13584566 : if (integer_zerop (arg0))
11976 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
11977 :
11978 : /* !X && X is always false. */
11979 13584566 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11980 13584566 : && 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 13584566 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11984 13584566 : && 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 13584566 : if (!TREE_SIDE_EFFECTS (arg0)
11992 13584566 : && !TREE_SIDE_EFFECTS (arg1))
11993 : {
11994 12103770 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11995 12103770 : if (tem && !operand_equal_p (tem, arg0, 0))
11996 436 : return fold_convert (type,
11997 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
11998 : tem, arg1));
11999 :
12000 12103334 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12001 12103334 : if (tem && !operand_equal_p (tem, arg1, 0))
12002 8617 : return fold_convert (type,
12003 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12004 : arg0, tem));
12005 : }
12006 :
12007 13575513 : 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 3531559 : 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 3531559 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12019 146364 : return fold_convert_loc (loc, type, arg0);
12020 : /* FALLTHRU */
12021 12919311 : case TRUTH_OR_EXPR:
12022 : /* If either arg is constant zero, drop it. */
12023 12919311 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12024 249790 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12025 489461 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12026 : /* Preserve sequence points. */
12027 13106028 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12028 425229 : 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 12244292 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12032 52954 : 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 12191338 : 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 12191338 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12040 12191338 : && 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 12191338 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12044 12191338 : && 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 12191337 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12049 1656 : && 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 12191324 : 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 79306 : case TRUTH_XOR_EXPR:
12076 : /* If the second arg is constant zero, drop it. */
12077 79306 : 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 79306 : 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 79306 : 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 79306 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12091 79306 : && 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 79306 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12096 79306 : && 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 49812735 : case EQ_EXPR:
12102 49812735 : case NE_EXPR:
12103 49812735 : STRIP_NOPS (arg0);
12104 49812735 : STRIP_NOPS (arg1);
12105 :
12106 49812735 : tem = fold_comparison (loc, code, type, op0, op1);
12107 49812735 : if (tem != NULL_TREE)
12108 : return tem;
12109 :
12110 : /* bool_var != 1 becomes !bool_var. */
12111 50974017 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12112 49852407 : && code == NE_EXPR)
12113 40927 : return fold_convert_loc (loc, type,
12114 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12115 81854 : TREE_TYPE (arg0), arg0));
12116 :
12117 : /* bool_var == 0 becomes !bool_var. */
12118 50892163 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12119 50706144 : && code == EQ_EXPR)
12120 196412 : return fold_convert_loc (loc, type,
12121 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12122 392824 : TREE_TYPE (arg0), arg0));
12123 :
12124 : /* !exp != 0 becomes !exp */
12125 617209 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12126 50184965 : && code == NE_EXPR)
12127 608195 : 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 48960272 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12135 48960272 : && integer_zerop (arg1))
12136 : {
12137 1529661 : tree arg00 = TREE_OPERAND (arg0, 0);
12138 1529661 : tree arg01 = TREE_OPERAND (arg0, 1);
12139 1529661 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12140 1529661 : && integer_onep (TREE_OPERAND (arg00, 0)))
12141 : {
12142 4323 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12143 4323 : arg01, TREE_OPERAND (arg00, 1));
12144 4323 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12145 4323 : build_one_cst (TREE_TYPE (arg0)));
12146 4323 : return fold_build2_loc (loc, code, type,
12147 4323 : fold_convert_loc (loc, TREE_TYPE (arg1),
12148 4323 : tem), arg1);
12149 : }
12150 1525338 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12151 1525338 : && 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 48955524 : if ((TREE_CODE (arg0) == COMPONENT_REF
12165 48955524 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12166 : /* Handle the constant case even without -O
12167 : to make sure the warnings are given. */
12168 5050795 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12169 : {
12170 4733936 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12171 4733936 : 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 48154871 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12182 : {
12183 3055868 : tree fndecl = get_callee_fndecl (arg0);
12184 :
12185 3055868 : if (fndecl
12186 3054776 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12187 550 : && call_expr_nargs (arg0) == 1
12188 3056418 : && (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 48154321 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12204 1694799 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12205 861 : && integer_zerop (arg1)
12206 48154783 : && 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 48154181 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12220 4756 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12221 0 : && integer_zerop (arg1)
12222 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12223 48154181 : && 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 48154181 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12234 1694659 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12235 38268 : && integer_zerop (arg1)
12236 38268 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12237 48189630 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12238 35449 : 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 48154181 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12248 4756 : && 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 48154165 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12292 48151638 : || 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 41584812 : case LT_EXPR:
12363 41584812 : case GT_EXPR:
12364 41584812 : case LE_EXPR:
12365 41584812 : case GE_EXPR:
12366 41584812 : tem = fold_comparison (loc, code, type, op0, op1);
12367 41584812 : if (tem != NULL_TREE)
12368 : return tem;
12369 :
12370 : /* Transform comparisons of the form X +- C CMP X. */
12371 40713213 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12372 4816787 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12373 51625 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12374 40713229 : && !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 40713200 : if (code == LE_EXPR
12413 7732933 : && TREE_CODE (arg1) == INTEGER_CST
12414 5508553 : && TREE_CODE (arg0) == ABS_EXPR
12415 817 : && ! TREE_SIDE_EFFECTS (arg0)
12416 817 : && (tem = negate_expr (arg1)) != 0
12417 817 : && TREE_CODE (tem) == INTEGER_CST
12418 40714017 : && !TREE_OVERFLOW (tem))
12419 1634 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12420 : build2 (GE_EXPR, type,
12421 817 : TREE_OPERAND (arg0, 0), tem),
12422 : build2 (LE_EXPR, type,
12423 1634 : TREE_OPERAND (arg0, 0), arg1));
12424 :
12425 : /* Convert ABS_EXPR<x> >= 0 to true. */
12426 40712383 : if (code == GE_EXPR
12427 4244183 : && (integer_zerop (arg1)
12428 3101096 : || (! HONOR_NANS (arg0)
12429 2424874 : && real_zerop (arg1)))
12430 41855701 : && tree_expr_nonnegative_p (arg0))
12431 5446 : return omit_one_operand_loc (loc, type,
12432 : constant_boolean_node (true, type),
12433 5446 : arg0);
12434 :
12435 : /* Convert ABS_EXPR<x> < 0 to false. */
12436 40706937 : if (code == LT_EXPR
12437 13518329 : && (integer_zerop (arg1) || real_zerop (arg1))
12438 44010669 : && tree_expr_nonnegative_p (arg0))
12439 43685 : return omit_one_operand_loc (loc, type,
12440 : constant_boolean_node (false, type),
12441 43685 : arg0);
12442 :
12443 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12444 : and similarly for >= into !=. */
12445 40663252 : if ((code == LT_EXPR || code == GE_EXPR)
12446 17713381 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12447 5602603 : && TREE_CODE (arg1) == LSHIFT_EXPR
12448 40664755 : && 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 40661897 : if ((code == LT_EXPR || code == GE_EXPR)
12462 17712026 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12463 5781550 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12464 11953918 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12465 4083571 : && CONVERT_EXPR_P (arg1)
12466 1133675 : && 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 40661904 : && 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 5993316 : case UNORDERED_EXPR:
12484 5993316 : case ORDERED_EXPR:
12485 5993316 : case UNLT_EXPR:
12486 5993316 : case UNLE_EXPR:
12487 5993316 : case UNGT_EXPR:
12488 5993316 : case UNGE_EXPR:
12489 5993316 : case UNEQ_EXPR:
12490 5993316 : case LTGT_EXPR:
12491 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12492 5993316 : {
12493 5993316 : tree targ0 = strip_float_extensions (arg0);
12494 5993316 : tree targ1 = strip_float_extensions (arg1);
12495 5993316 : tree newtype = TREE_TYPE (targ0);
12496 :
12497 5993316 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12498 1283 : newtype = TREE_TYPE (targ1);
12499 :
12500 5993316 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12501 5993316 : && (!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 8715043 : case COMPOUND_EXPR:
12510 : /* When pedantic, a compound expression can be neither an lvalue
12511 : nor an integer constant expression. */
12512 8715043 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12513 : return NULL_TREE;
12514 : /* Don't let (0, 0) be null pointer constant. */
12515 516549 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12516 516549 : : 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 1266908 : 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 1266908 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12547 1266908 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12548 1266908 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12549 2533816 : if (~cst1 == 0
12550 3795940 : || (cst1 & (cst1 + 1)) != 0
12551 1054782 : || !INTEGRAL_TYPE_P (type)
12552 1054782 : || (!TYPE_OVERFLOW_WRAPS (type)
12553 44578 : && TREE_CODE (type) != INTEGER_TYPE)
12554 4640988 : || (wi::max_value (type) & cst1) != cst1)
12555 : return NULL_TREE;
12556 :
12557 1054782 : enum tree_code codes[2] = { code00, code01 };
12558 1054782 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12559 1054782 : int which = 0;
12560 1054782 : 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 1054782 : pmop[0] = arg00;
12566 1054782 : pmop[1] = arg01;
12567 1054782 : which = code != NEGATE_EXPR;
12568 :
12569 3163428 : for (; which >= 0; which--)
12570 2108646 : switch (codes[which])
12571 : {
12572 21222 : case BIT_AND_EXPR:
12573 21222 : case BIT_IOR_EXPR:
12574 21222 : case BIT_XOR_EXPR:
12575 21222 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12576 21222 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12577 21222 : if (codes[which] == BIT_AND_EXPR)
12578 : {
12579 21110 : 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 19683 : pmop[which] = arg0xx[2 * which];
12588 19683 : break;
12589 2087424 : case ERROR_MARK:
12590 2087424 : 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 889084 : if ((code == PLUS_EXPR
12595 216215 : || (code == MINUS_EXPR && which == 0))
12596 660757 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12597 135816 : pmop[which] = build_int_cst (type, 0);
12598 : /* Similarly, with C - N where (-N & M) == 0. */
12599 889084 : if (code == MINUS_EXPR
12600 444542 : && which == 1
12601 653382 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12602 200786 : 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 1054782 : if (pmop[0] == arg00 && pmop[1] == arg01)
12610 : return NULL_TREE;
12611 :
12612 355701 : if (TYPE_OVERFLOW_WRAPS (type))
12613 : return type;
12614 : else
12615 2960 : return unsigned_type_for (type);
12616 1054782 : }
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 4550180 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12632 : {
12633 4550180 : contains_label_data *d = (contains_label_data *) data;
12634 4550180 : 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 327573 : contains_label_p (tree st)
12671 : {
12672 327573 : hash_set<tree> pset;
12673 327573 : contains_label_data data = { &pset, false };
12674 327573 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12675 327573 : }
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 47465756 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12683 : tree op0, tree op1, tree op2)
12684 : {
12685 47465756 : tree tem;
12686 47465756 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12687 47465756 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12688 :
12689 47465756 : 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 47465756 : if (commutative_ternary_tree_code (code)
12695 47465756 : && tree_swap_operands_p (op0, op1))
12696 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12697 :
12698 47465723 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12699 47465723 : 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 46404673 : if (op0)
12713 : {
12714 46337303 : arg0 = op0;
12715 46337303 : STRIP_NOPS (arg0);
12716 : }
12717 :
12718 46404673 : if (op1)
12719 : {
12720 46404673 : arg1 = op1;
12721 46404673 : STRIP_NOPS (arg1);
12722 : }
12723 :
12724 46404673 : if (op2)
12725 : {
12726 15103449 : arg2 = op2;
12727 15103449 : STRIP_NOPS (arg2);
12728 : }
12729 :
12730 46404673 : switch (code)
12731 : {
12732 31300742 : case COMPONENT_REF:
12733 31300742 : if (TREE_CODE (arg0) == CONSTRUCTOR
12734 31300742 : && ! 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 12619782 : case COND_EXPR:
12745 12619782 : 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 12619782 : if (TREE_CODE (arg0) == INTEGER_CST)
12749 : {
12750 461030 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12751 461030 : 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 461030 : if ((!TREE_SIDE_EFFECTS (unused_op)
12757 327573 : || !contains_label_p (unused_op))
12758 783782 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12759 376835 : || VOID_TYPE_P (type)))
12760 447003 : return protected_set_expr_location_unshare (tem, loc);
12761 : return NULL_TREE;
12762 : }
12763 12158752 : else if (TREE_CODE (arg0) == VECTOR_CST)
12764 : {
12765 11718 : unsigned HOST_WIDE_INT nelts;
12766 11718 : if ((TREE_CODE (arg1) == VECTOR_CST
12767 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12768 2677 : && (TREE_CODE (arg2) == VECTOR_CST
12769 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12770 23436 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12771 : {
12772 2677 : vec_perm_builder sel (nelts, nelts, 1);
12773 26567 : for (unsigned int i = 0; i < nelts; i++)
12774 : {
12775 23890 : tree val = VECTOR_CST_ELT (arg0, i);
12776 23890 : if (integer_all_onesp (val))
12777 11830 : sel.quick_push (i);
12778 12060 : else if (integer_zerop (val))
12779 12060 : sel.quick_push (nelts + i);
12780 : else /* Currently unreachable. */
12781 1989 : return NULL_TREE;
12782 : }
12783 2677 : vec_perm_indices indices (sel, 2, nelts);
12784 2677 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12785 2677 : if (t != NULL_TREE)
12786 1989 : return t;
12787 4666 : }
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 12156763 : if (COMPARISON_CLASS_P (arg0)
12797 9999963 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12798 12298015 : && !HONOR_SIGNED_ZEROS (op1))
12799 : {
12800 130469 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12801 130469 : TREE_OPERAND (arg0, 0),
12802 130469 : TREE_OPERAND (arg0, 1),
12803 : op1, op2);
12804 130469 : if (tem)
12805 : return tem;
12806 : }
12807 :
12808 12149999 : if (COMPARISON_CLASS_P (arg0)
12809 9993199 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12810 12623071 : && !HONOR_SIGNED_ZEROS (op2))
12811 : {
12812 386535 : enum tree_code comp_code = TREE_CODE (arg0);
12813 386535 : tree arg00 = TREE_OPERAND (arg0, 0);
12814 386535 : tree arg01 = TREE_OPERAND (arg0, 1);
12815 386535 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12816 386535 : if (comp_code != ERROR_MARK)
12817 386535 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12818 : arg00,
12819 : arg01,
12820 : op2, op1);
12821 386535 : 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 11882745 : if (truth_value_p (TREE_CODE (arg0))
12828 11882745 : && tree_swap_operands_p (op1, op2))
12829 : {
12830 2066844 : 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 2066844 : tem = fold_invert_truthvalue (loc0, arg0);
12835 2066844 : if (tem)
12836 1317562 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12837 : }
12838 :
12839 : /* Convert A ? 1 : 0 to simply A. */
12840 10565183 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12841 10112969 : : (integer_onep (op1)
12842 413653 : && !VECTOR_TYPE_P (type)))
12843 692315 : && 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 11695820 : && type == TREE_TYPE (arg0))
12849 32847 : 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 10532336 : if (integer_zerop (op1)
12854 394551 : && code == COND_EXPR
12855 370497 : && integer_onep (op2)
12856 32281 : && !VECTOR_TYPE_P (type)
12857 10564617 : && truth_value_p (TREE_CODE (arg0)))
12858 30726 : return fold_convert_loc (loc, type,
12859 30726 : invert_truthvalue_loc (loc, arg0));
12860 :
12861 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12862 10501610 : if (TREE_CODE (arg0) == LT_EXPR
12863 1378483 : && integer_zerop (TREE_OPERAND (arg0, 1))
12864 37708 : && integer_zerop (op2)
12865 10502581 : && (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 10501582 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12933 347 : && integer_onep (TREE_OPERAND (arg0, 1))
12934 3 : && integer_zerop (op2)
12935 10501582 : && 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 10501582 : if (integer_zerop (op2)
12954 2063202 : && TREE_CODE (arg0) == NE_EXPR
12955 548650 : && integer_zerop (TREE_OPERAND (arg0, 1))
12956 297333 : && integer_pow2p (arg1)
12957 32752 : && 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 10501582 : && 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 10501582 : 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 10049368 : if (integer_zerop (op2)
12975 1706987 : && truth_value_p (TREE_CODE (arg0))
12976 1513767 : && truth_value_p (TREE_CODE (arg1))
12977 10082815 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12978 33447 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
12979 : : TRUTH_ANDIF_EXPR,
12980 33447 : 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 10015921 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
12984 454809 : && truth_value_p (TREE_CODE (arg0))
12985 307655 : && truth_value_p (TREE_CODE (arg1))
12986 10053238 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
12987 : {
12988 37317 : location_t loc0 = expr_location_or (arg0, loc);
12989 : /* Only perform transformation if ARG0 is easily inverted. */
12990 37317 : tem = fold_invert_truthvalue (loc0, arg0);
12991 37317 : if (tem)
12992 37053 : 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 37053 : op1);
12997 : }
12998 :
12999 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13000 9978868 : if (integer_zerop (arg1)
13001 339848 : && truth_value_p (TREE_CODE (arg0))
13002 84368 : && truth_value_p (TREE_CODE (op2))
13003 9978896 : && (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 9978868 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13017 380806 : && truth_value_p (TREE_CODE (arg0))
13018 271655 : && truth_value_p (TREE_CODE (op2))
13019 9979054 : && (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 940569 : case BIT_FIELD_REF:
13032 940569 : if (TREE_CODE (arg0) == VECTOR_CST
13033 79673 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13034 42749 : || (VECTOR_TYPE_P (type)
13035 41873 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13036 78761 : && tree_fits_uhwi_p (op1)
13037 1019330 : && tree_fits_uhwi_p (op2))
13038 : {
13039 78761 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13040 78761 : unsigned HOST_WIDE_INT width
13041 78761 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13042 78761 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13043 78761 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13044 78761 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13045 :
13046 78761 : if (n != 0
13047 78761 : && (idx % width) == 0
13048 78761 : && (n % width) == 0
13049 157522 : && known_le ((idx + n) / width,
13050 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13051 : {
13052 78761 : idx = idx / width;
13053 78761 : n = n / width;
13054 :
13055 78761 : if (TREE_CODE (arg0) == VECTOR_CST)
13056 : {
13057 78761 : if (n == 1)
13058 : {
13059 36928 : tem = VECTOR_CST_ELT (arg0, idx);
13060 36928 : if (VECTOR_TYPE_P (type))
13061 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13062 : return tem;
13063 : }
13064 :
13065 41833 : tree_vector_builder vals (type, n, 1);
13066 227042 : for (unsigned i = 0; i < n; ++i)
13067 143376 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13068 41833 : return vals.build ();
13069 41833 : }
13070 : }
13071 : }
13072 :
13073 : /* On constants we can use native encode/interpret to constant
13074 : fold (nearly) all BIT_FIELD_REFs. */
13075 861808 : if (CONSTANT_CLASS_P (arg0)
13076 1713 : && can_native_interpret_type_p (type)
13077 : && BITS_PER_UNIT == 8
13078 1713 : && tree_fits_uhwi_p (op1)
13079 863521 : && tree_fits_uhwi_p (op2))
13080 : {
13081 1713 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13082 1713 : 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 1713 : if (bitpos % BITS_PER_UNIT == 0
13087 1713 : && bitsize % BITS_PER_UNIT == 0
13088 1713 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13089 : {
13090 1713 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13091 1713 : unsigned HOST_WIDE_INT len
13092 1713 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13093 1713 : bitpos / BITS_PER_UNIT);
13094 1713 : if (len > 0
13095 1713 : && len * BITS_PER_UNIT >= bitsize)
13096 : {
13097 1713 : tree v = native_interpret_expr (type, b,
13098 : bitsize / BITS_PER_UNIT);
13099 1713 : if (v)
13100 1659 : return v;
13101 : }
13102 : }
13103 : }
13104 :
13105 : return NULL_TREE;
13106 :
13107 783570 : case VEC_PERM_EXPR:
13108 : /* Perform constant folding of BIT_INSERT_EXPR. */
13109 783570 : if (TREE_CODE (arg2) == VECTOR_CST
13110 772096 : && TREE_CODE (op0) == VECTOR_CST
13111 15412 : && 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 16253 : case BIT_INSERT_EXPR:
13127 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13128 16253 : 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 16251 : else if (TREE_CODE (arg0) == VECTOR_CST
13144 901 : && CONSTANT_CLASS_P (arg1)
13145 16548 : && 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 47465756 : 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 1596 : 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 692906 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13183 : unsigned *ctor_idx)
13184 : {
13185 692906 : tree index_type = NULL_TREE;
13186 692906 : signop index_sgn = UNSIGNED;
13187 692906 : offset_int low_bound = 0;
13188 :
13189 692906 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13190 : {
13191 692906 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13192 692906 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13193 : {
13194 : /* Static constructors for variably sized objects makes no sense. */
13195 692906 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13196 692906 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13197 : /* ??? When it is obvious that the range is signed, treat it so. */
13198 692906 : if (TYPE_UNSIGNED (index_type)
13199 353912 : && TYPE_MAX_VALUE (domain_type)
13200 1046787 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13201 353881 : 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 692906 : index_sgn = TYPE_SIGN (index_type);
13211 692906 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13212 : }
13213 : }
13214 : }
13215 :
13216 692906 : if (index_type)
13217 692906 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13218 : index_sgn);
13219 :
13220 692906 : offset_int index = low_bound;
13221 692906 : if (index_type)
13222 692906 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13223 :
13224 692906 : offset_int max_index = index;
13225 692906 : unsigned cnt;
13226 692906 : tree cfield, cval;
13227 692906 : bool first_p = true;
13228 :
13229 13936892 : 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 13935725 : if (cfield)
13235 : {
13236 5900530 : if (TREE_CODE (cfield) == INTEGER_CST)
13237 11799614 : max_index = index
13238 5899807 : = 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 8035195 : else if (!first_p)
13251 : {
13252 7830347 : index = max_index + 1;
13253 7830347 : if (index_type)
13254 7830347 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13255 7830347 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13256 7830347 : max_index = index;
13257 : }
13258 : else
13259 : first_p = false;
13260 :
13261 13935725 : if (TREE_CODE (cval) == RAW_DATA_CST)
13262 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13263 :
13264 : /* Do we have match? */
13265 13935725 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13266 : {
13267 13935437 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13268 : {
13269 691623 : if (ctor_idx)
13270 691623 : *ctor_idx = cnt;
13271 : 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 1283 : if (ctor_idx)
13283 1283 : *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 1392283148 : fold (tree expr)
13302 : {
13303 1392458872 : const tree t = expr;
13304 1392458872 : enum tree_code code = TREE_CODE (t);
13305 1392458872 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13306 1392458872 : tree tem;
13307 1392458872 : location_t loc = EXPR_LOCATION (expr);
13308 :
13309 : /* Return right away if a constant. */
13310 1392458872 : if (kind == tcc_constant)
13311 : return t;
13312 :
13313 : /* CALL_EXPR-like objects with variable numbers of operands are
13314 : treated specially. */
13315 1286647215 : if (kind == tcc_vl_exp)
13316 : {
13317 182326391 : if (code == CALL_EXPR)
13318 : {
13319 182325862 : tem = fold_call_expr (loc, expr, false);
13320 182325862 : return tem ? tem : expr;
13321 : }
13322 : return expr;
13323 : }
13324 :
13325 1104320824 : if (IS_EXPR_CODE_CLASS (kind))
13326 : {
13327 1102053102 : tree type = TREE_TYPE (t);
13328 1102053102 : tree op0, op1, op2;
13329 :
13330 1102053102 : switch (TREE_CODE_LENGTH (code))
13331 : {
13332 1000540574 : case 1:
13333 1000540574 : op0 = TREE_OPERAND (t, 0);
13334 1000540574 : tem = fold_unary_loc (loc, code, type, op0);
13335 1000540574 : return tem ? tem : expr;
13336 92296586 : case 2:
13337 92296586 : op0 = TREE_OPERAND (t, 0);
13338 92296586 : op1 = TREE_OPERAND (t, 1);
13339 92296586 : tem = fold_binary_loc (loc, code, type, op0, op1);
13340 92296586 : return tem ? tem : expr;
13341 4428131 : case 3:
13342 4428131 : op0 = TREE_OPERAND (t, 0);
13343 4428131 : op1 = TREE_OPERAND (t, 1);
13344 4428131 : op2 = TREE_OPERAND (t, 2);
13345 4428131 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13346 4428131 : return tem ? tem : expr;
13347 : default:
13348 : break;
13349 : }
13350 : }
13351 :
13352 7055533 : switch (code)
13353 : {
13354 4683831 : case ARRAY_REF:
13355 4683831 : {
13356 4683831 : tree op0 = TREE_OPERAND (t, 0);
13357 4683831 : tree op1 = TREE_OPERAND (t, 1);
13358 :
13359 4683831 : if (TREE_CODE (op1) == INTEGER_CST
13360 2930757 : && TREE_CODE (op0) == CONSTRUCTOR
13361 4685286 : && ! 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 206757 : case CONSTRUCTOR:
13389 206757 : {
13390 206757 : tree type = TREE_TYPE (t);
13391 206757 : if (TREE_CODE (type) != VECTOR_TYPE)
13392 : return t;
13393 :
13394 : unsigned i;
13395 : tree val;
13396 364028 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13397 314921 : if (! CONSTANT_CLASS_P (val))
13398 : return t;
13399 :
13400 49107 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13401 : }
13402 :
13403 175724 : case CONST_DECL:
13404 175724 : 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 1059279597 : fold_build1_loc (location_t loc,
13670 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13671 : {
13672 1059279597 : 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 1059279597 : tem = fold_unary_loc (loc, code, type, op0);
13685 1059279597 : if (!tem)
13686 540510122 : 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 1059279597 : 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 682905310 : fold_build2_loc (location_t loc,
13707 : enum tree_code code, tree type, tree op0, tree op1
13708 : MEM_STAT_DECL)
13709 : {
13710 682905310 : 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 682905310 : tem = fold_binary_loc (loc, code, type, op0, op1);
13731 682905310 : if (!tem)
13732 384096969 : 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 682905310 : 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 40833370 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13760 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13761 : {
13762 40833370 : 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 40833370 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13790 40833370 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13791 40833370 : if (!tem)
13792 37961630 : 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 40833370 : 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 57775747 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13828 : int nargs, tree *argarray)
13829 : {
13830 57775747 : 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 57775747 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13853 57775747 : if (!tem)
13854 55652140 : 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 57775747 : 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 546313 : fold_init (tree expr)
13901 : {
13902 546313 : tree result;
13903 546313 : START_FOLD_INIT;
13904 :
13905 546313 : result = fold (expr);
13906 :
13907 546313 : END_FOLD_INIT;
13908 546313 : return result;
13909 : }
13910 :
13911 : tree
13912 2988933 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13913 : tree type, tree op)
13914 : {
13915 2988933 : tree result;
13916 2988933 : START_FOLD_INIT;
13917 :
13918 2988933 : result = fold_build1_loc (loc, code, type, op);
13919 :
13920 2988933 : END_FOLD_INIT;
13921 2988933 : return result;
13922 : }
13923 :
13924 : tree
13925 50448 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13926 : tree type, tree op0, tree op1)
13927 : {
13928 50448 : tree result;
13929 50448 : START_FOLD_INIT;
13930 :
13931 50448 : result = fold_build2_loc (loc, code, type, op0, op1);
13932 :
13933 50448 : END_FOLD_INIT;
13934 50448 : 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 67872739 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
13952 : tree lhs, tree rhs)
13953 : {
13954 67872739 : tree result;
13955 67872739 : START_FOLD_INIT;
13956 :
13957 67872739 : result = fold_binary_loc (loc, code, type, lhs, rhs);
13958 :
13959 67872739 : END_FOLD_INIT;
13960 67872739 : 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 1553169 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14014 : {
14015 1606409 : gimple *stmt;
14016 1606409 : tree op1, op2;
14017 :
14018 1606409 : if (operand_equal_p (top, bottom, 0))
14019 : return true;
14020 :
14021 1104680 : if (TREE_CODE (type) != INTEGER_TYPE)
14022 : return false;
14023 :
14024 1104661 : 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 405861 : 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 405861 : if (!nowrap
14039 15172 : && !TYPE_OVERFLOW_UNDEFINED (type)
14040 410613 : && !integer_pow2p (bottom))
14041 : return false;
14042 405426 : if (TREE_CODE (bottom) == INTEGER_CST)
14043 : {
14044 403730 : op1 = TREE_OPERAND (top, 0);
14045 403730 : op2 = TREE_OPERAND (top, 1);
14046 403730 : if (TREE_CODE (op1) == INTEGER_CST)
14047 0 : std::swap (op1, op2);
14048 403730 : if (TREE_CODE (op2) == INTEGER_CST)
14049 : {
14050 393484 : if (multiple_of_p (type, op2, bottom, nowrap))
14051 : return true;
14052 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14053 3302 : if (multiple_of_p (type, bottom, op2, nowrap))
14054 : {
14055 1889 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14056 1889 : wi::to_widest (op2));
14057 1889 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14058 : {
14059 1889 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14060 1889 : return multiple_of_p (type, op1, op2, nowrap);
14061 : }
14062 1889 : }
14063 : return multiple_of_p (type, op1, bottom, nowrap);
14064 : }
14065 : }
14066 11942 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14067 11942 : || 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 225956 : case MINUS_EXPR:
14086 225956 : 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 225956 : if (!nowrap
14091 175235 : && !TYPE_OVERFLOW_UNDEFINED (type)
14092 399747 : && !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 197023 : op1 = TREE_OPERAND (top, 1);
14099 197023 : if (TREE_CODE (top) == PLUS_EXPR
14100 190486 : && nowrap
14101 44275 : && TYPE_UNSIGNED (type)
14102 240568 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14103 27723 : 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 197023 : return (multiple_of_p (type, op1, bottom, nowrap)
14110 197023 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14111 :
14112 144610 : CASE_CONVERT:
14113 : /* Can't handle conversions from non-integral or wider integral type. */
14114 144610 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14115 144610 : || (TYPE_PRECISION (type)
14116 39036 : < 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 38778 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14121 77556 : TREE_OPERAND (top, 0), bottom, false);
14122 :
14123 13049 : case SAVE_EXPR:
14124 13049 : 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 140798 : case INTEGER_CST:
14131 140798 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14132 : return false;
14133 138096 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14134 : SIGNED);
14135 :
14136 61671 : case SSA_NAME:
14137 61671 : if (TREE_CODE (bottom) == INTEGER_CST
14138 58482 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14139 120153 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14140 : {
14141 24794 : 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 24794 : if (code == BIT_AND_EXPR
14153 574 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14154 574 : && TREE_CODE (op2) == INTEGER_CST
14155 466 : && integer_pow2p (bottom)
14156 25260 : && wi::multiple_of_p (wi::to_widest (op2),
14157 466 : wi::to_widest (bottom), SIGNED))
14158 457 : return true;
14159 :
14160 24337 : op1 = gimple_assign_rhs1 (stmt);
14161 24337 : if (code == MINUS_EXPR
14162 1521 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14163 1521 : && TREE_CODE (op2) == SSA_NAME
14164 1521 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14165 1521 : && gimple_code (stmt) == GIMPLE_ASSIGN
14166 1260 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14167 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14168 24401 : && 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 639366 : tree_expr_finite_p (const_tree x)
14189 : {
14190 639370 : machine_mode mode = element_mode (x);
14191 639370 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14192 : return true;
14193 639068 : switch (TREE_CODE (x))
14194 : {
14195 760 : case REAL_CST:
14196 760 : 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 1334224 : tree_expr_infinite_p (const_tree x)
14241 : {
14242 1334674 : if (!HONOR_INFINITIES (x))
14243 : return false;
14244 1334429 : 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 815305 : tree_expr_maybe_infinite_p (const_tree x)
14267 : {
14268 815313 : if (!HONOR_INFINITIES (x))
14269 : return false;
14270 814857 : switch (TREE_CODE (x))
14271 : {
14272 301 : case REAL_CST:
14273 301 : 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 429 : tree_expr_signaling_nan_p (const_tree x)
14292 : {
14293 429 : 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 719132 : tree_expr_maybe_signaling_nan_p (const_tree x)
14316 : {
14317 719132 : if (!HONOR_SNANS (x))
14318 : return false;
14319 4892 : 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 3935082 : tree_expr_nan_p (const_tree x)
14363 : {
14364 4290824 : if (!HONOR_NANS (x))
14365 : return false;
14366 4290474 : switch (TREE_CODE (x))
14367 : {
14368 3806 : case REAL_CST:
14369 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14370 355742 : case NON_LVALUE_EXPR:
14371 355742 : case SAVE_EXPR:
14372 355742 : 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 5161955 : tree_expr_maybe_nan_p (const_tree x)
14387 : {
14388 7554230 : if (!HONOR_NANS (x))
14389 : return false;
14390 7471774 : switch (TREE_CODE (x))
14391 : {
14392 3450 : case REAL_CST:
14393 3450 : return real_isnan (TREE_REAL_CST_PTR (x));
14394 : case FLOAT_EXPR:
14395 : return false;
14396 14833 : case PLUS_EXPR:
14397 14833 : case MINUS_EXPR:
14398 14833 : case MULT_EXPR:
14399 14833 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14400 14833 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14401 2392275 : case ABS_EXPR:
14402 2392275 : case CONVERT_EXPR:
14403 2392275 : case NEGATE_EXPR:
14404 2392275 : case NON_LVALUE_EXPR:
14405 2392275 : case SAVE_EXPR:
14406 2392275 : 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 593922 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14439 : {
14440 593922 : if (!HONOR_SIGNED_ZEROS (x))
14441 : return false;
14442 593922 : 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 26744620 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14483 : {
14484 26744620 : if (!VECTOR_TYPE_P (type)
14485 26708564 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14486 53452919 : && 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 2429181 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14498 : {
14499 2429181 : if (TYPE_UNSIGNED (type))
14500 : return true;
14501 :
14502 1974106 : switch (code)
14503 : {
14504 285203 : case ABS_EXPR:
14505 : /* We can't return 1 if flag_wrapv is set because
14506 : ABS_EXPR<INT_MIN> = INT_MIN. */
14507 285203 : if (!ANY_INTEGRAL_TYPE_P (type))
14508 : return true;
14509 1173 : if (TYPE_OVERFLOW_UNDEFINED (type))
14510 : return true;
14511 : break;
14512 :
14513 75258 : case NON_LVALUE_EXPR:
14514 75258 : case FLOAT_EXPR:
14515 75258 : case FIX_TRUNC_EXPR:
14516 75258 : return RECURSE (op0);
14517 :
14518 1575808 : CASE_CONVERT:
14519 1575808 : {
14520 1575808 : tree inner_type = TREE_TYPE (op0);
14521 1575808 : tree outer_type = type;
14522 :
14523 1575808 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14524 : {
14525 410949 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14526 410949 : 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 1164859 : else if (INTEGRAL_TYPE_P (outer_type))
14535 : {
14536 1164782 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14537 0 : return RECURSE (op0);
14538 1164782 : if (INTEGRAL_TYPE_P (inner_type))
14539 1155959 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14540 1155959 : && TYPE_UNSIGNED (inner_type);
14541 : }
14542 : }
14543 : break;
14544 :
14545 37837 : default:
14546 37837 : 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 4827118 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14558 : tree op1, int depth)
14559 : {
14560 4827118 : if (TYPE_UNSIGNED (type))
14561 : return true;
14562 :
14563 4523791 : switch (code)
14564 : {
14565 1092337 : case POINTER_PLUS_EXPR:
14566 1092337 : case PLUS_EXPR:
14567 1092337 : if (FLOAT_TYPE_P (type))
14568 49231 : 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 1043106 : if (TREE_CODE (type) == INTEGER_TYPE
14573 1037066 : && TREE_CODE (op0) == NOP_EXPR
14574 16209 : && 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 162078 : case MULT_EXPR:
14589 162078 : 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 146495 : if (operand_equal_p (op0, op1, 0)
14594 146495 : || (RECURSE (op0) && RECURSE (op1)))
14595 : 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 159925 : if (TREE_CODE (type) == INTEGER_TYPE
14601 86869 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14602 160 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14603 : {
14604 146 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14605 146 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14606 146 : : TREE_TYPE (op0);
14607 146 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14608 146 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14609 146 : : TREE_TYPE (op1);
14610 :
14611 146 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14612 146 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14613 :
14614 146 : if (TREE_CODE (op0) == INTEGER_CST)
14615 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14616 :
14617 146 : if (TREE_CODE (op1) == INTEGER_CST)
14618 69 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14619 :
14620 146 : 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 9495 : case BIT_AND_EXPR:
14637 9495 : return RECURSE (op0) || RECURSE (op1);
14638 :
14639 35244 : case MAX_EXPR:
14640 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14641 : things. */
14642 35244 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14643 76 : return RECURSE (op0) && RECURSE (op1);
14644 35168 : return RECURSE (op0) || RECURSE (op1);
14645 :
14646 155355 : case BIT_IOR_EXPR:
14647 155355 : case BIT_XOR_EXPR:
14648 155355 : case MIN_EXPR:
14649 155355 : case RDIV_EXPR:
14650 155355 : case TRUNC_DIV_EXPR:
14651 155355 : case CEIL_DIV_EXPR:
14652 155355 : case FLOOR_DIV_EXPR:
14653 155355 : case ROUND_DIV_EXPR:
14654 155355 : return RECURSE (op0) && RECURSE (op1);
14655 :
14656 53485 : case TRUNC_MOD_EXPR:
14657 53485 : return RECURSE (op0);
14658 :
14659 230 : case FLOOR_MOD_EXPR:
14660 230 : return RECURSE (op1);
14661 :
14662 3015567 : case CEIL_MOD_EXPR:
14663 3015567 : case ROUND_MOD_EXPR:
14664 3015567 : default:
14665 3015567 : 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 25388095 : tree_single_nonnegative_p (tree t, int depth)
14677 : {
14678 25388095 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14679 : return true;
14680 :
14681 21542577 : switch (TREE_CODE (t))
14682 : {
14683 2433517 : case INTEGER_CST:
14684 2433517 : return tree_int_cst_sgn (t) >= 0;
14685 :
14686 924909 : case REAL_CST:
14687 924909 : 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 8665166 : case SSA_NAME:
14696 : /* For integral types, query the range if possible. */
14697 8665166 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14698 : {
14699 7209856 : int_range_max r;
14700 14419712 : get_range_query (cfun)->range_of_expr (r, t);
14701 7209856 : if (!r.undefined_p () && !r.varying_p())
14702 : {
14703 1744044 : if (r.nonnegative_p ())
14704 : return true;
14705 1154934 : if (r.nonpositive_p () && !range_includes_zero_p (r))
14706 : return false;
14707 : }
14708 7209856 : }
14709 : /* Limit the depth of recursion to avoid quadratic behavior.
14710 : This is expected to catch almost all occurrences in practice.
14711 : If this code misses important cases that unbounded recursion
14712 : would not, passes that need this information could be revised
14713 : to provide it through dataflow propagation. */
14714 8072409 : return (!name_registered_for_update_p (t)
14715 8072408 : && depth < param_max_ssa_name_query_depth
14716 16015452 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14717 :
14718 9518057 : default:
14719 9518057 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14720 : }
14721 : }
14722 :
14723 : /* Return true if T is known to be non-negative.
14724 : DEPTH is the current nesting depth of the query. */
14725 :
14726 : bool
14727 14270305 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14728 : int depth)
14729 : {
14730 14270305 : switch (fn)
14731 : {
14732 : CASE_CFN_ACOS:
14733 : CASE_CFN_ACOS_FN:
14734 : CASE_CFN_ACOSH:
14735 : CASE_CFN_ACOSH_FN:
14736 : CASE_CFN_ACOSPI:
14737 : CASE_CFN_ACOSPI_FN:
14738 : CASE_CFN_CABS:
14739 : CASE_CFN_CABS_FN:
14740 : CASE_CFN_COSH:
14741 : CASE_CFN_COSH_FN:
14742 : CASE_CFN_ERFC:
14743 : CASE_CFN_ERFC_FN:
14744 : CASE_CFN_EXP:
14745 : CASE_CFN_EXP_FN:
14746 : CASE_CFN_EXP10:
14747 : CASE_CFN_EXP2:
14748 : CASE_CFN_EXP2_FN:
14749 : CASE_CFN_FABS:
14750 : CASE_CFN_FABS_FN:
14751 : CASE_CFN_FDIM:
14752 : CASE_CFN_FDIM_FN:
14753 : CASE_CFN_HYPOT:
14754 : CASE_CFN_HYPOT_FN:
14755 : CASE_CFN_POW10:
14756 : CASE_CFN_FFS:
14757 : CASE_CFN_PARITY:
14758 : CASE_CFN_POPCOUNT:
14759 : CASE_CFN_CLRSB:
14760 : CASE_CFN_BSWAP:
14761 : CASE_CFN_BITREVERSE:
14762 : /* Always true. */
14763 : return true;
14764 :
14765 176 : CASE_CFN_CLZ:
14766 176 : CASE_CFN_CTZ:
14767 176 : if (arg1)
14768 2 : return RECURSE (arg1);
14769 : return true;
14770 :
14771 832 : CASE_CFN_SQRT:
14772 832 : CASE_CFN_SQRT_FN:
14773 : /* sqrt(-0.0) is -0.0. */
14774 832 : if (!HONOR_SIGNED_ZEROS (type))
14775 : return true;
14776 800 : return RECURSE (arg0);
14777 :
14778 58517 : CASE_CFN_ASINH:
14779 58517 : CASE_CFN_ASINH_FN:
14780 58517 : CASE_CFN_ASINPI:
14781 58517 : CASE_CFN_ASINPI_FN:
14782 58517 : CASE_CFN_ATAN:
14783 58517 : CASE_CFN_ATAN_FN:
14784 58517 : CASE_CFN_ATANH:
14785 58517 : CASE_CFN_ATANH_FN:
14786 58517 : CASE_CFN_ATANPI:
14787 58517 : CASE_CFN_ATANPI_FN:
14788 58517 : CASE_CFN_CBRT:
14789 58517 : CASE_CFN_CBRT_FN:
14790 58517 : CASE_CFN_CEIL:
14791 58517 : CASE_CFN_CEIL_FN:
14792 58517 : CASE_CFN_ERF:
14793 58517 : CASE_CFN_ERF_FN:
14794 58517 : CASE_CFN_EXPM1:
14795 58517 : CASE_CFN_EXPM1_FN:
14796 58517 : CASE_CFN_FLOOR:
14797 58517 : CASE_CFN_FLOOR_FN:
14798 58517 : CASE_CFN_FMOD:
14799 58517 : CASE_CFN_FMOD_FN:
14800 58517 : CASE_CFN_FREXP:
14801 58517 : CASE_CFN_FREXP_FN:
14802 58517 : CASE_CFN_ICEIL:
14803 58517 : CASE_CFN_IFLOOR:
14804 58517 : CASE_CFN_IRINT:
14805 58517 : CASE_CFN_IROUND:
14806 58517 : CASE_CFN_LCEIL:
14807 58517 : CASE_CFN_LDEXP:
14808 58517 : CASE_CFN_LFLOOR:
14809 58517 : CASE_CFN_LLCEIL:
14810 58517 : CASE_CFN_LLFLOOR:
14811 58517 : CASE_CFN_LLRINT:
14812 58517 : CASE_CFN_LLRINT_FN:
14813 58517 : CASE_CFN_LLROUND:
14814 58517 : CASE_CFN_LLROUND_FN:
14815 58517 : CASE_CFN_LRINT:
14816 58517 : CASE_CFN_LRINT_FN:
14817 58517 : CASE_CFN_LROUND:
14818 58517 : CASE_CFN_LROUND_FN:
14819 58517 : CASE_CFN_MODF:
14820 58517 : CASE_CFN_MODF_FN:
14821 58517 : CASE_CFN_NEARBYINT:
14822 58517 : CASE_CFN_NEARBYINT_FN:
14823 58517 : CASE_CFN_RINT:
14824 58517 : CASE_CFN_RINT_FN:
14825 58517 : CASE_CFN_ROUND:
14826 58517 : CASE_CFN_ROUND_FN:
14827 58517 : CASE_CFN_ROUNDEVEN:
14828 58517 : CASE_CFN_ROUNDEVEN_FN:
14829 58517 : CASE_CFN_SCALB:
14830 58517 : CASE_CFN_SCALBLN:
14831 58517 : CASE_CFN_SCALBLN_FN:
14832 58517 : CASE_CFN_SCALBN:
14833 58517 : CASE_CFN_SCALBN_FN:
14834 58517 : CASE_CFN_SIGNBIT:
14835 58517 : CASE_CFN_SIGNIFICAND:
14836 58517 : CASE_CFN_SINH:
14837 58517 : CASE_CFN_SINH_FN:
14838 58517 : CASE_CFN_TANH:
14839 58517 : CASE_CFN_TANH_FN:
14840 58517 : CASE_CFN_TRUNC:
14841 58517 : CASE_CFN_TRUNC_FN:
14842 : /* True if the 1st argument is nonnegative. */
14843 58517 : return RECURSE (arg0);
14844 :
14845 1319 : CASE_CFN_FMAX:
14846 1319 : CASE_CFN_FMAX_FN:
14847 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14848 : things. In the presence of sNaNs, we're only guaranteed to be
14849 : non-negative if both operands are non-negative. In the presence
14850 : of qNaNs, we're non-negative if either operand is non-negative
14851 : and can't be a qNaN, or if both operands are non-negative. */
14852 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14853 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14854 136 : return RECURSE (arg0) && RECURSE (arg1);
14855 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14856 332 : || RECURSE (arg1))
14857 851 : : (RECURSE (arg1)
14858 851 : && !tree_expr_maybe_nan_p (arg1));
14859 :
14860 910 : CASE_CFN_FMIN:
14861 910 : CASE_CFN_FMIN_FN:
14862 : /* True if the 1st AND 2nd arguments are nonnegative. */
14863 910 : return RECURSE (arg0) && RECURSE (arg1);
14864 :
14865 769 : CASE_CFN_COPYSIGN:
14866 769 : CASE_CFN_COPYSIGN_FN:
14867 : /* True if the 2nd argument is nonnegative. */
14868 769 : return RECURSE (arg1);
14869 :
14870 2312 : CASE_CFN_POWI:
14871 : /* True if the 1st argument is nonnegative or the second
14872 : argument is an even integer. */
14873 2312 : if (TREE_CODE (arg1) == INTEGER_CST
14874 2312 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14875 : return true;
14876 2229 : return RECURSE (arg0);
14877 :
14878 4910 : CASE_CFN_POW:
14879 4910 : CASE_CFN_POW_FN:
14880 : /* True if the 1st argument is nonnegative or the second
14881 : argument is an even integer valued real. */
14882 4910 : if (TREE_CODE (arg1) == REAL_CST)
14883 : {
14884 2202 : REAL_VALUE_TYPE c;
14885 2202 : HOST_WIDE_INT n;
14886 :
14887 2202 : c = TREE_REAL_CST (arg1);
14888 2202 : n = real_to_integer (&c);
14889 2202 : if ((n & 1) == 0)
14890 : {
14891 1573 : REAL_VALUE_TYPE cint;
14892 1573 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14893 1573 : if (real_identical (&c, &cint))
14894 568 : return true;
14895 : }
14896 : }
14897 4342 : return RECURSE (arg0);
14898 :
14899 14170389 : default:
14900 14170389 : break;
14901 : }
14902 14170389 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14903 : }
14904 :
14905 : /* Return true if T is known to be non-negative.
14906 : DEPTH is the current nesting depth of the query. */
14907 :
14908 : static bool
14909 1738171 : tree_invalid_nonnegative_p (tree t, int depth)
14910 : {
14911 1738171 : enum tree_code code = TREE_CODE (t);
14912 1738171 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14913 : return true;
14914 :
14915 1320792 : switch (code)
14916 : {
14917 268 : case TARGET_EXPR:
14918 268 : {
14919 268 : tree temp = TARGET_EXPR_SLOT (t);
14920 268 : t = TARGET_EXPR_INITIAL (t);
14921 :
14922 : /* If the initializer is non-void, then it's a normal expression
14923 : that will be assigned to the slot. */
14924 268 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14925 66 : return RECURSE (t);
14926 :
14927 : /* Otherwise, the initializer sets the slot in some way. One common
14928 : way is an assignment statement at the end of the initializer. */
14929 404 : while (1)
14930 : {
14931 404 : if (TREE_CODE (t) == BIND_EXPR)
14932 202 : t = expr_last (BIND_EXPR_BODY (t));
14933 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14934 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
14935 0 : t = expr_last (TREE_OPERAND (t, 0));
14936 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
14937 0 : t = expr_last (t);
14938 : else
14939 : break;
14940 : }
14941 202 : if (TREE_CODE (t) == MODIFY_EXPR
14942 202 : && TREE_OPERAND (t, 0) == temp)
14943 202 : return RECURSE (TREE_OPERAND (t, 1));
14944 :
14945 : return false;
14946 : }
14947 :
14948 643357 : case CALL_EXPR:
14949 643357 : {
14950 643357 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14951 643357 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14952 :
14953 643357 : return tree_call_nonnegative_p (TREE_TYPE (t),
14954 : get_call_combined_fn (t),
14955 : arg0,
14956 : arg1,
14957 643357 : depth);
14958 : }
14959 3387 : case COMPOUND_EXPR:
14960 3387 : case MODIFY_EXPR:
14961 3387 : return RECURSE (TREE_OPERAND (t, 1));
14962 :
14963 15 : case BIND_EXPR:
14964 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
14965 :
14966 670995 : case SAVE_EXPR:
14967 670995 : return RECURSE (TREE_OPERAND (t, 0));
14968 :
14969 2770 : default:
14970 2770 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14971 : }
14972 : }
14973 :
14974 : #undef RECURSE
14975 : #undef tree_expr_nonnegative_p
14976 :
14977 : /* Return true if T is known to be non-negative.
14978 : DEPTH is the current nesting depth of the query. */
14979 :
14980 : bool
14981 26953613 : tree_expr_nonnegative_p (tree t, int depth)
14982 : {
14983 26953613 : enum tree_code code;
14984 26953613 : if (error_operand_p (t))
14985 : return false;
14986 :
14987 26953612 : code = TREE_CODE (t);
14988 26953612 : switch (TREE_CODE_CLASS (code))
14989 : {
14990 1383353 : case tcc_binary:
14991 1383353 : case tcc_comparison:
14992 1383353 : return tree_binary_nonnegative_p (TREE_CODE (t),
14993 1383353 : TREE_TYPE (t),
14994 1383353 : TREE_OPERAND (t, 0),
14995 1383353 : TREE_OPERAND (t, 1),
14996 1383353 : depth);
14997 :
14998 1975312 : case tcc_unary:
14999 1975312 : return tree_unary_nonnegative_p (TREE_CODE (t),
15000 1975312 : TREE_TYPE (t),
15001 1975312 : TREE_OPERAND (t, 0),
15002 1975312 : depth);
15003 :
15004 13517055 : case tcc_constant:
15005 13517055 : case tcc_declaration:
15006 13517055 : case tcc_reference:
15007 13517055 : return tree_single_nonnegative_p (t, depth);
15008 :
15009 10077892 : default:
15010 10077892 : break;
15011 : }
15012 :
15013 10077892 : switch (code)
15014 : {
15015 7 : case TRUTH_AND_EXPR:
15016 7 : case TRUTH_OR_EXPR:
15017 7 : case TRUTH_XOR_EXPR:
15018 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15019 7 : TREE_TYPE (t),
15020 7 : TREE_OPERAND (t, 0),
15021 7 : TREE_OPERAND (t, 1),
15022 7 : depth);
15023 72 : case TRUTH_NOT_EXPR:
15024 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15025 72 : TREE_TYPE (t),
15026 72 : TREE_OPERAND (t, 0),
15027 72 : depth);
15028 :
15029 8339642 : case COND_EXPR:
15030 8339642 : case CONSTRUCTOR:
15031 8339642 : case OBJ_TYPE_REF:
15032 8339642 : case ADDR_EXPR:
15033 8339642 : case WITH_SIZE_EXPR:
15034 8339642 : case SSA_NAME:
15035 8339642 : return tree_single_nonnegative_p (t, depth);
15036 :
15037 1738171 : default:
15038 1738171 : return tree_invalid_nonnegative_p (t, depth);
15039 : }
15040 : }
15041 :
15042 :
15043 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15044 : For floating point we further ensure that T is not denormal.
15045 : Similar logic is present in nonzero_address in rtlanal.h. */
15046 :
15047 : bool
15048 1600935 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15049 : {
15050 1600935 : switch (code)
15051 : {
15052 1 : case ABS_EXPR:
15053 1 : return tree_expr_nonzero_p (op0);
15054 :
15055 924562 : case NOP_EXPR:
15056 924562 : {
15057 924562 : tree inner_type = TREE_TYPE (op0);
15058 924562 : tree outer_type = type;
15059 :
15060 924562 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15061 924562 : && tree_expr_nonzero_p (op0));
15062 : }
15063 28113 : break;
15064 :
15065 28113 : case NON_LVALUE_EXPR:
15066 28113 : return tree_expr_nonzero_p (op0);
15067 :
15068 : default:
15069 : break;
15070 : }
15071 :
15072 : return false;
15073 : }
15074 :
15075 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15076 : For floating point we further ensure that T is not denormal.
15077 : Similar logic is present in nonzero_address in rtlanal.h. */
15078 :
15079 : bool
15080 3009387 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15081 : {
15082 3009387 : switch (code)
15083 : {
15084 491515 : case POINTER_PLUS_EXPR:
15085 491515 : case PLUS_EXPR:
15086 491515 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15087 : {
15088 : /* With the presence of negative values it is hard
15089 : to say something. */
15090 109896 : if (!tree_expr_nonnegative_p (op0)
15091 109896 : || !tree_expr_nonnegative_p (op1))
15092 : return false;
15093 : /* One of operands must be positive and the other non-negative. */
15094 3508 : return (tree_expr_nonzero_p (op0)
15095 3508 : || tree_expr_nonzero_p (op1));
15096 : }
15097 : break;
15098 :
15099 31580 : case MULT_EXPR:
15100 31580 : if (TYPE_OVERFLOW_UNDEFINED (type))
15101 : {
15102 555 : if (tree_expr_nonzero_p (op0)
15103 555 : && tree_expr_nonzero_p (op1))
15104 : return true;
15105 : }
15106 : break;
15107 :
15108 : case MIN_EXPR:
15109 : break;
15110 :
15111 43 : case MAX_EXPR:
15112 43 : if (tree_expr_nonzero_p (op0))
15113 : {
15114 :
15115 : /* When both operands are nonzero, then MAX must be too. */
15116 0 : if (tree_expr_nonzero_p (op1))
15117 : return true;
15118 :
15119 : /* MAX where operand 0 is positive is positive. */
15120 0 : return tree_expr_nonnegative_p (op0);
15121 : }
15122 : /* MAX where operand 1 is positive is positive. */
15123 43 : else if (tree_expr_nonzero_p (op1)
15124 43 : && tree_expr_nonnegative_p (op1))
15125 : return true;
15126 : break;
15127 :
15128 268224 : case BIT_IOR_EXPR:
15129 268224 : return (tree_expr_nonzero_p (op1)
15130 268224 : || tree_expr_nonzero_p (op0));
15131 :
15132 : default:
15133 : break;
15134 : }
15135 :
15136 : return false;
15137 : }
15138 :
15139 : /* Return true when T is an address and is known to be nonzero.
15140 : For floating point we further ensure that T is not denormal.
15141 : Similar logic is present in nonzero_address in rtlanal.h. */
15142 :
15143 : bool
15144 155130804 : tree_single_nonzero_p (tree t)
15145 : {
15146 155130804 : switch (TREE_CODE (t))
15147 : {
15148 1158694 : case INTEGER_CST:
15149 1158694 : return !integer_zerop (t);
15150 :
15151 12354903 : case ADDR_EXPR:
15152 12354903 : {
15153 12354903 : tree base = TREE_OPERAND (t, 0);
15154 :
15155 12354903 : if (!DECL_P (base))
15156 6141736 : base = get_base_address (base);
15157 :
15158 12354903 : if (base && TREE_CODE (base) == TARGET_EXPR)
15159 843 : base = TARGET_EXPR_SLOT (base);
15160 :
15161 843 : if (!base)
15162 : return false;
15163 :
15164 : /* For objects in symbol table check if we know they are non-zero.
15165 : Don't do anything for variables and functions before symtab is built;
15166 : it is quite possible that they will be declared weak later. */
15167 12354903 : int nonzero_addr = maybe_nonzero_address (base);
15168 12354903 : if (nonzero_addr >= 0)
15169 10043247 : return nonzero_addr;
15170 :
15171 : /* Constants are never weak. */
15172 2311656 : if (CONSTANT_CLASS_P (base))
15173 2217640 : return true;
15174 :
15175 : return false;
15176 : }
15177 :
15178 38187 : case COND_EXPR:
15179 38187 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15180 38187 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15181 : return true;
15182 : break;
15183 :
15184 129408689 : case SSA_NAME:
15185 129408689 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15186 : break;
15187 100712236 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15188 :
15189 : default:
15190 : break;
15191 : }
15192 : return false;
15193 : }
15194 :
15195 : #define integer_valued_real_p(X) \
15196 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15197 :
15198 : #define RECURSE(X) \
15199 : ((integer_valued_real_p) (X, depth + 1))
15200 :
15201 : /* Return true if the floating point result of (CODE OP0) has an
15202 : integer value. We also allow +Inf, -Inf and NaN to be considered
15203 : integer values. Return false for signaling NaN.
15204 :
15205 : DEPTH is the current nesting depth of the query. */
15206 :
15207 : bool
15208 15122 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15209 : {
15210 15122 : switch (code)
15211 : {
15212 : case FLOAT_EXPR:
15213 : return true;
15214 :
15215 1403 : case ABS_EXPR:
15216 1403 : return RECURSE (op0);
15217 :
15218 9847 : CASE_CONVERT:
15219 9847 : {
15220 9847 : tree type = TREE_TYPE (op0);
15221 9847 : if (TREE_CODE (type) == INTEGER_TYPE)
15222 : return true;
15223 9847 : if (SCALAR_FLOAT_TYPE_P (type))
15224 9847 : return RECURSE (op0);
15225 : break;
15226 : }
15227 :
15228 : default:
15229 : break;
15230 : }
15231 : return false;
15232 : }
15233 :
15234 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15235 : integer value. We also allow +Inf, -Inf and NaN to be considered
15236 : integer values. Return false for signaling NaN.
15237 :
15238 : DEPTH is the current nesting depth of the query. */
15239 :
15240 : bool
15241 13634 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15242 : {
15243 13634 : switch (code)
15244 : {
15245 7899 : case PLUS_EXPR:
15246 7899 : case MINUS_EXPR:
15247 7899 : case MULT_EXPR:
15248 7899 : case MIN_EXPR:
15249 7899 : case MAX_EXPR:
15250 7899 : return RECURSE (op0) && RECURSE (op1);
15251 :
15252 : default:
15253 : break;
15254 : }
15255 : return false;
15256 : }
15257 :
15258 : /* Return true if the floating point result of calling FNDECL with arguments
15259 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15260 : considered integer values. Return false for signaling NaN. If FNDECL
15261 : takes fewer than 2 arguments, the remaining ARGn are null.
15262 :
15263 : DEPTH is the current nesting depth of the query. */
15264 :
15265 : bool
15266 1133 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15267 : {
15268 1133 : switch (fn)
15269 : {
15270 : CASE_CFN_CEIL:
15271 : CASE_CFN_CEIL_FN:
15272 : CASE_CFN_FLOOR:
15273 : CASE_CFN_FLOOR_FN:
15274 : CASE_CFN_NEARBYINT:
15275 : CASE_CFN_NEARBYINT_FN:
15276 : CASE_CFN_RINT:
15277 : CASE_CFN_RINT_FN:
15278 : CASE_CFN_ROUND:
15279 : CASE_CFN_ROUND_FN:
15280 : CASE_CFN_ROUNDEVEN:
15281 : CASE_CFN_ROUNDEVEN_FN:
15282 : CASE_CFN_TRUNC:
15283 : CASE_CFN_TRUNC_FN:
15284 : return true;
15285 :
15286 336 : CASE_CFN_FMIN:
15287 336 : CASE_CFN_FMIN_FN:
15288 336 : CASE_CFN_FMAX:
15289 336 : CASE_CFN_FMAX_FN:
15290 336 : return RECURSE (arg0) && RECURSE (arg1);
15291 :
15292 : default:
15293 : break;
15294 : }
15295 : return false;
15296 : }
15297 :
15298 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15299 : has an integer value. We also allow +Inf, -Inf and NaN to be
15300 : considered integer values. Return false for signaling NaN.
15301 :
15302 : DEPTH is the current nesting depth of the query. */
15303 :
15304 : bool
15305 128455 : integer_valued_real_single_p (tree t, int depth)
15306 : {
15307 128455 : switch (TREE_CODE (t))
15308 : {
15309 2344 : case REAL_CST:
15310 2344 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15311 :
15312 0 : case COND_EXPR:
15313 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15314 :
15315 90306 : case SSA_NAME:
15316 : /* Limit the depth of recursion to avoid quadratic behavior.
15317 : This is expected to catch almost all occurrences in practice.
15318 : If this code misses important cases that unbounded recursion
15319 : would not, passes that need this information could be revised
15320 : to provide it through dataflow propagation. */
15321 90306 : return (!name_registered_for_update_p (t)
15322 90306 : && depth < param_max_ssa_name_query_depth
15323 179798 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15324 : depth));
15325 :
15326 : default:
15327 : break;
15328 : }
15329 : return false;
15330 : }
15331 :
15332 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15333 : has an integer value. We also allow +Inf, -Inf and NaN to be
15334 : considered integer values. Return false for signaling NaN.
15335 :
15336 : DEPTH is the current nesting depth of the query. */
15337 :
15338 : static bool
15339 0 : integer_valued_real_invalid_p (tree t, int depth)
15340 : {
15341 0 : switch (TREE_CODE (t))
15342 : {
15343 0 : case COMPOUND_EXPR:
15344 0 : case MODIFY_EXPR:
15345 0 : case BIND_EXPR:
15346 0 : return RECURSE (TREE_OPERAND (t, 1));
15347 :
15348 0 : case SAVE_EXPR:
15349 0 : return RECURSE (TREE_OPERAND (t, 0));
15350 :
15351 : default:
15352 : break;
15353 : }
15354 : return false;
15355 : }
15356 :
15357 : #undef RECURSE
15358 : #undef integer_valued_real_p
15359 :
15360 : /* Return true if the floating point expression T has an integer value.
15361 : We also allow +Inf, -Inf and NaN to be considered integer values.
15362 : Return false for signaling NaN.
15363 :
15364 : DEPTH is the current nesting depth of the query. */
15365 :
15366 : bool
15367 97256 : integer_valued_real_p (tree t, int depth)
15368 : {
15369 97256 : if (t == error_mark_node)
15370 : return false;
15371 :
15372 97256 : STRIP_ANY_LOCATION_WRAPPER (t);
15373 :
15374 97256 : tree_code code = TREE_CODE (t);
15375 97256 : switch (TREE_CODE_CLASS (code))
15376 : {
15377 0 : case tcc_binary:
15378 0 : case tcc_comparison:
15379 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15380 0 : TREE_OPERAND (t, 1), depth);
15381 :
15382 0 : case tcc_unary:
15383 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15384 :
15385 8525 : case tcc_constant:
15386 8525 : case tcc_declaration:
15387 8525 : case tcc_reference:
15388 8525 : return integer_valued_real_single_p (t, depth);
15389 :
15390 88731 : default:
15391 88731 : break;
15392 : }
15393 :
15394 88731 : switch (code)
15395 : {
15396 88731 : case COND_EXPR:
15397 88731 : case SSA_NAME:
15398 88731 : return integer_valued_real_single_p (t, depth);
15399 :
15400 0 : case CALL_EXPR:
15401 0 : {
15402 0 : tree arg0 = (call_expr_nargs (t) > 0
15403 0 : ? CALL_EXPR_ARG (t, 0)
15404 0 : : NULL_TREE);
15405 0 : tree arg1 = (call_expr_nargs (t) > 1
15406 0 : ? CALL_EXPR_ARG (t, 1)
15407 0 : : NULL_TREE);
15408 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15409 0 : arg0, arg1, depth);
15410 : }
15411 :
15412 0 : default:
15413 0 : return integer_valued_real_invalid_p (t, depth);
15414 : }
15415 : }
15416 :
15417 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15418 : attempt to fold the expression to a constant without modifying TYPE,
15419 : OP0 or OP1.
15420 :
15421 : If the expression could be simplified to a constant, then return
15422 : the constant. If the expression would not be simplified to a
15423 : constant, then return NULL_TREE. */
15424 :
15425 : tree
15426 15963094 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15427 : {
15428 15963094 : tree tem = fold_binary (code, type, op0, op1);
15429 15963094 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15430 : }
15431 :
15432 : /* Given the components of a unary expression CODE, TYPE and OP0,
15433 : attempt to fold the expression to a constant without modifying
15434 : TYPE or OP0.
15435 :
15436 : If the expression could be simplified to a constant, then return
15437 : the constant. If the expression would not be simplified to a
15438 : constant, then return NULL_TREE. */
15439 :
15440 : tree
15441 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15442 : {
15443 0 : tree tem = fold_unary (code, type, op0);
15444 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15445 : }
15446 :
15447 : /* If EXP represents referencing an element in a constant string
15448 : (either via pointer arithmetic or array indexing), return the
15449 : tree representing the value accessed, otherwise return NULL. */
15450 :
15451 : tree
15452 218239775 : fold_read_from_constant_string (tree exp)
15453 : {
15454 218239775 : if ((INDIRECT_REF_P (exp)
15455 218239756 : || TREE_CODE (exp) == ARRAY_REF)
15456 231670544 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15457 : {
15458 10205912 : tree exp1 = TREE_OPERAND (exp, 0);
15459 10205912 : tree index;
15460 10205912 : tree string;
15461 10205912 : location_t loc = EXPR_LOCATION (exp);
15462 :
15463 10205912 : if (INDIRECT_REF_P (exp))
15464 0 : string = string_constant (exp1, &index, NULL, NULL);
15465 : else
15466 : {
15467 10205912 : tree low_bound = array_ref_low_bound (exp);
15468 10205912 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15469 :
15470 : /* Optimize the special-case of a zero lower bound.
15471 :
15472 : We convert the low_bound to sizetype to avoid some problems
15473 : with constant folding. (E.g. suppose the lower bound is 1,
15474 : and its mode is QI. Without the conversion,l (ARRAY
15475 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15476 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15477 10205912 : if (! integer_zerop (low_bound))
15478 155748 : index = size_diffop_loc (loc, index,
15479 : fold_convert_loc (loc, sizetype, low_bound));
15480 :
15481 : string = exp1;
15482 : }
15483 :
15484 10205912 : scalar_int_mode char_mode;
15485 10205912 : if (string
15486 10205912 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15487 10205912 : && TREE_CODE (string) == STRING_CST
15488 260474 : && tree_fits_uhwi_p (index)
15489 256492 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15490 10462202 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15491 : &char_mode)
15492 20411824 : && GET_MODE_SIZE (char_mode) == 1)
15493 510128 : return build_int_cst_type (TREE_TYPE (exp),
15494 255064 : (TREE_STRING_POINTER (string)
15495 255064 : [TREE_INT_CST_LOW (index)]));
15496 : }
15497 : return NULL;
15498 : }
15499 :
15500 : /* Folds a read from vector element at IDX of vector ARG. */
15501 :
15502 : tree
15503 6973 : fold_read_from_vector (tree arg, poly_uint64 idx)
15504 : {
15505 6973 : unsigned HOST_WIDE_INT i;
15506 6973 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15507 6973 : && known_ge (idx, 0u)
15508 6973 : && idx.is_constant (&i))
15509 : {
15510 6973 : if (TREE_CODE (arg) == VECTOR_CST)
15511 2066 : return VECTOR_CST_ELT (arg, i);
15512 4907 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15513 : {
15514 2708 : if (CONSTRUCTOR_NELTS (arg)
15515 2668 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15516 : return NULL_TREE;
15517 1870 : if (i >= CONSTRUCTOR_NELTS (arg))
15518 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15519 1830 : return CONSTRUCTOR_ELT (arg, i)->value;
15520 : }
15521 : }
15522 : return NULL_TREE;
15523 : }
15524 :
15525 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15526 : an integer constant, real, or fixed-point constant.
15527 :
15528 : TYPE is the type of the result. */
15529 :
15530 : static tree
15531 33069187 : fold_negate_const (tree arg0, tree type)
15532 : {
15533 33069187 : tree t = NULL_TREE;
15534 :
15535 33069187 : switch (TREE_CODE (arg0))
15536 : {
15537 2071585 : case REAL_CST:
15538 2071585 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15539 2071585 : break;
15540 :
15541 0 : case FIXED_CST:
15542 0 : {
15543 0 : FIXED_VALUE_TYPE f;
15544 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15545 0 : &(TREE_FIXED_CST (arg0)), NULL,
15546 0 : TYPE_SATURATING (type));
15547 0 : t = build_fixed (type, f);
15548 : /* Propagate overflow flags. */
15549 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15550 0 : TREE_OVERFLOW (t) = 1;
15551 0 : break;
15552 : }
15553 :
15554 30997602 : default:
15555 30997602 : if (poly_int_tree_p (arg0))
15556 : {
15557 30997602 : wi::overflow_type overflow;
15558 30997602 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15559 30997602 : t = force_fit_type (type, res, 1,
15560 30997602 : (overflow && ! TYPE_UNSIGNED (type))
15561 30987455 : || TREE_OVERFLOW (arg0));
15562 30997602 : break;
15563 30997602 : }
15564 :
15565 0 : gcc_unreachable ();
15566 : }
15567 :
15568 33069187 : return t;
15569 : }
15570 :
15571 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15572 : an integer constant or real constant.
15573 :
15574 : TYPE is the type of the result. */
15575 :
15576 : tree
15577 36684 : fold_abs_const (tree arg0, tree type)
15578 : {
15579 36684 : tree t = NULL_TREE;
15580 :
15581 36684 : switch (TREE_CODE (arg0))
15582 : {
15583 7065 : case INTEGER_CST:
15584 7065 : {
15585 : /* If the value is unsigned or non-negative, then the absolute value
15586 : is the same as the ordinary value. */
15587 7065 : wide_int val = wi::to_wide (arg0);
15588 7065 : wi::overflow_type overflow = wi::OVF_NONE;
15589 7065 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15590 : ;
15591 :
15592 : /* If the value is negative, then the absolute value is
15593 : its negation. */
15594 : else
15595 3189 : val = wi::neg (val, &overflow);
15596 :
15597 : /* Force to the destination type, set TREE_OVERFLOW for signed
15598 : TYPE only. */
15599 7065 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15600 7065 : }
15601 7065 : break;
15602 :
15603 29619 : case REAL_CST:
15604 29619 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15605 7597 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15606 : else
15607 : t = arg0;
15608 : break;
15609 :
15610 0 : default:
15611 0 : gcc_unreachable ();
15612 : }
15613 :
15614 36684 : return t;
15615 : }
15616 :
15617 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15618 : constant. TYPE is the type of the result. */
15619 :
15620 : static tree
15621 2340292 : fold_not_const (const_tree arg0, tree type)
15622 : {
15623 2340292 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15624 :
15625 2340292 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15626 : }
15627 :
15628 : /* Given CODE, a relational operator, the target type, TYPE and two
15629 : constant operands OP0 and OP1, return the result of the
15630 : relational operation. If the result is not a compile time
15631 : constant, then return NULL_TREE. */
15632 :
15633 : static tree
15634 83378009 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15635 : {
15636 83378009 : int result, invert;
15637 :
15638 : /* From here on, the only cases we handle are when the result is
15639 : known to be a constant. */
15640 :
15641 83378009 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15642 : {
15643 1222415 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15644 1222415 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15645 :
15646 : /* Handle the cases where either operand is a NaN. */
15647 1222415 : if (real_isnan (c0) || real_isnan (c1))
15648 : {
15649 13727 : switch (code)
15650 : {
15651 : case EQ_EXPR:
15652 : case ORDERED_EXPR:
15653 : result = 0;
15654 : break;
15655 :
15656 : case NE_EXPR:
15657 : case UNORDERED_EXPR:
15658 : case UNLT_EXPR:
15659 : case UNLE_EXPR:
15660 : case UNGT_EXPR:
15661 : case UNGE_EXPR:
15662 : case UNEQ_EXPR:
15663 6805 : result = 1;
15664 : break;
15665 :
15666 6965 : case LT_EXPR:
15667 6965 : case LE_EXPR:
15668 6965 : case GT_EXPR:
15669 6965 : case GE_EXPR:
15670 6965 : case LTGT_EXPR:
15671 6965 : if (flag_trapping_math)
15672 : return NULL_TREE;
15673 : result = 0;
15674 : break;
15675 :
15676 0 : default:
15677 0 : gcc_unreachable ();
15678 : }
15679 :
15680 6805 : return constant_boolean_node (result, type);
15681 : }
15682 :
15683 1208688 : return constant_boolean_node (real_compare (code, c0, c1), type);
15684 : }
15685 :
15686 82155594 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15687 : {
15688 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15689 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15690 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15691 : }
15692 :
15693 : /* Handle equality/inequality of complex constants. */
15694 82155594 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15695 : {
15696 58558 : tree rcond = fold_relational_const (code, type,
15697 29279 : TREE_REALPART (op0),
15698 29279 : TREE_REALPART (op1));
15699 117116 : tree icond = fold_relational_const (code, type,
15700 29279 : TREE_IMAGPART (op0),
15701 29279 : TREE_IMAGPART (op1));
15702 29279 : if (code == EQ_EXPR)
15703 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15704 28977 : else if (code == NE_EXPR)
15705 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15706 : else
15707 : return NULL_TREE;
15708 : }
15709 :
15710 82126315 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15711 : {
15712 22563 : if (!VECTOR_TYPE_P (type))
15713 : {
15714 : /* Have vector comparison with scalar boolean result. */
15715 198 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15716 : && known_eq (VECTOR_CST_NELTS (op0),
15717 : VECTOR_CST_NELTS (op1)));
15718 198 : unsigned HOST_WIDE_INT nunits;
15719 198 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15720 : return NULL_TREE;
15721 657 : for (unsigned i = 0; i < nunits; i++)
15722 : {
15723 560 : tree elem0 = VECTOR_CST_ELT (op0, i);
15724 560 : tree elem1 = VECTOR_CST_ELT (op1, i);
15725 560 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15726 560 : if (tmp == NULL_TREE)
15727 : return NULL_TREE;
15728 560 : if (integer_zerop (tmp))
15729 101 : return constant_boolean_node (code == NE_EXPR, type);
15730 : }
15731 97 : return constant_boolean_node (code == EQ_EXPR, type);
15732 : }
15733 22365 : tree_vector_builder elts;
15734 22365 : if (!elts.new_binary_operation (type, op0, op1, false))
15735 : return NULL_TREE;
15736 22365 : unsigned int count = elts.encoded_nelts ();
15737 77825 : for (unsigned i = 0; i < count; i++)
15738 : {
15739 55460 : tree elem_type = TREE_TYPE (type);
15740 55460 : tree elem0 = VECTOR_CST_ELT (op0, i);
15741 55460 : tree elem1 = VECTOR_CST_ELT (op1, i);
15742 :
15743 55460 : tree tem = fold_relational_const (code, elem_type,
15744 : elem0, elem1);
15745 :
15746 55460 : if (tem == NULL_TREE)
15747 : return NULL_TREE;
15748 :
15749 55460 : elts.quick_push (build_int_cst (elem_type,
15750 89749 : integer_zerop (tem) ? 0 : -1));
15751 : }
15752 :
15753 22365 : return elts.build ();
15754 22365 : }
15755 :
15756 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15757 :
15758 : To compute GT, swap the arguments and do LT.
15759 : To compute GE, do LT and invert the result.
15760 : To compute LE, swap the arguments, do LT and invert the result.
15761 : To compute NE, do EQ and invert the result.
15762 :
15763 : Therefore, the code below must handle only EQ and LT. */
15764 :
15765 82103752 : if (code == LE_EXPR || code == GT_EXPR)
15766 : {
15767 14706468 : std::swap (op0, op1);
15768 14706468 : code = swap_tree_comparison (code);
15769 : }
15770 :
15771 : /* Note that it is safe to invert for real values here because we
15772 : have already handled the one case that it matters. */
15773 :
15774 82103752 : invert = 0;
15775 82103752 : if (code == NE_EXPR || code == GE_EXPR)
15776 : {
15777 36920007 : invert = 1;
15778 36920007 : code = invert_tree_comparison (code, false);
15779 : }
15780 :
15781 : /* Compute a result for LT or EQ if args permit;
15782 : Otherwise return T. */
15783 82103752 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15784 : {
15785 82075602 : if (code == EQ_EXPR)
15786 40006803 : result = tree_int_cst_equal (op0, op1);
15787 : else
15788 42068799 : result = tree_int_cst_lt (op0, op1);
15789 : }
15790 : else
15791 : return NULL_TREE;
15792 :
15793 82075602 : if (invert)
15794 36918147 : result ^= 1;
15795 82075602 : return constant_boolean_node (result, type);
15796 : }
15797 :
15798 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15799 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15800 : itself. */
15801 :
15802 : tree
15803 137854104 : fold_build_cleanup_point_expr (tree type, tree expr)
15804 : {
15805 : /* If the expression does not have side effects then we don't have to wrap
15806 : it with a cleanup point expression. */
15807 137854104 : if (!TREE_SIDE_EFFECTS (expr))
15808 : return expr;
15809 :
15810 : /* If the expression is a return, check to see if the expression inside the
15811 : return has no side effects or the right hand side of the modify expression
15812 : inside the return. If either don't have side effects set we don't need to
15813 : wrap the expression in a cleanup point expression. Note we don't check the
15814 : left hand side of the modify because it should always be a return decl. */
15815 117888124 : if (TREE_CODE (expr) == RETURN_EXPR)
15816 : {
15817 46461224 : tree op = TREE_OPERAND (expr, 0);
15818 46461224 : if (!op || !TREE_SIDE_EFFECTS (op))
15819 : return expr;
15820 45748751 : op = TREE_OPERAND (op, 1);
15821 45748751 : if (!TREE_SIDE_EFFECTS (op))
15822 : return expr;
15823 : }
15824 :
15825 93166796 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15826 : }
15827 :
15828 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15829 : of an indirection through OP0, or NULL_TREE if no simplification is
15830 : possible. */
15831 :
15832 : tree
15833 22662851 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15834 : {
15835 22662851 : tree sub = op0;
15836 22662851 : tree subtype;
15837 22662851 : poly_uint64 const_op01;
15838 :
15839 22662851 : STRIP_NOPS (sub);
15840 22662851 : subtype = TREE_TYPE (sub);
15841 22662851 : if (!POINTER_TYPE_P (subtype)
15842 22662851 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15843 : return NULL_TREE;
15844 :
15845 22505386 : if (TREE_CODE (sub) == ADDR_EXPR)
15846 : {
15847 5047778 : tree op = TREE_OPERAND (sub, 0);
15848 5047778 : tree optype = TREE_TYPE (op);
15849 :
15850 : /* *&CONST_DECL -> to the value of the const decl. */
15851 5047778 : if (TREE_CODE (op) == CONST_DECL)
15852 3274 : return DECL_INITIAL (op);
15853 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15854 5044504 : if (type == optype)
15855 : {
15856 3818124 : tree fop = fold_read_from_constant_string (op);
15857 3818124 : if (fop)
15858 : return fop;
15859 : else
15860 3772591 : return op;
15861 : }
15862 : /* *(foo *)&fooarray => fooarray[0] */
15863 1226380 : else if (TREE_CODE (optype) == ARRAY_TYPE
15864 13847 : && type == TREE_TYPE (optype)
15865 1239084 : && (!in_gimple_form
15866 3130 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15867 : {
15868 12704 : tree type_domain = TYPE_DOMAIN (optype);
15869 12704 : tree min_val = size_zero_node;
15870 12704 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15871 12665 : min_val = TYPE_MIN_VALUE (type_domain);
15872 12704 : if (in_gimple_form
15873 3130 : && TREE_CODE (min_val) != INTEGER_CST)
15874 : return NULL_TREE;
15875 12704 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15876 12704 : NULL_TREE, NULL_TREE);
15877 : }
15878 : /* *(foo *)&complexfoo => __real__ complexfoo */
15879 1213676 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15880 1213676 : && type == TREE_TYPE (optype))
15881 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15882 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15883 1213676 : else if (VECTOR_TYPE_P (optype)
15884 1213676 : && type == TREE_TYPE (optype))
15885 : {
15886 70 : tree part_width = TYPE_SIZE (type);
15887 70 : tree index = bitsize_int (0);
15888 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15889 70 : index);
15890 : }
15891 : }
15892 :
15893 18671214 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15894 18671214 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15895 : {
15896 262652 : tree op00 = TREE_OPERAND (sub, 0);
15897 262652 : tree op01 = TREE_OPERAND (sub, 1);
15898 :
15899 262652 : STRIP_NOPS (op00);
15900 262652 : if (TREE_CODE (op00) == ADDR_EXPR)
15901 : {
15902 2028 : tree op00type;
15903 2028 : op00 = TREE_OPERAND (op00, 0);
15904 2028 : op00type = TREE_TYPE (op00);
15905 :
15906 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15907 2028 : if (VECTOR_TYPE_P (op00type)
15908 240 : && type == TREE_TYPE (op00type)
15909 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15910 : but we want to treat offsets with MSB set as negative.
15911 : For the code below negative offsets are invalid and
15912 : TYPE_SIZE of the element is something unsigned, so
15913 : check whether op01 fits into poly_int64, which implies
15914 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15915 : then just use poly_uint64 because we want to treat the
15916 : value as unsigned. */
15917 2221 : && tree_fits_poly_int64_p (op01))
15918 : {
15919 179 : tree part_width = TYPE_SIZE (type);
15920 179 : poly_uint64 max_offset
15921 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15922 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15923 179 : if (known_lt (const_op01, max_offset))
15924 : {
15925 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15926 179 : return fold_build3_loc (loc,
15927 : BIT_FIELD_REF, type, op00,
15928 179 : part_width, index);
15929 : }
15930 : }
15931 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15932 1849 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
15933 1849 : && type == TREE_TYPE (op00type))
15934 : {
15935 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
15936 : const_op01))
15937 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15938 : }
15939 : /* ((foo *)&fooarray)[1] => fooarray[1] */
15940 1849 : else if (TREE_CODE (op00type) == ARRAY_TYPE
15941 1849 : && type == TREE_TYPE (op00type))
15942 : {
15943 719 : tree type_domain = TYPE_DOMAIN (op00type);
15944 719 : tree min_val = size_zero_node;
15945 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15946 718 : min_val = TYPE_MIN_VALUE (type_domain);
15947 719 : poly_uint64 type_size, index;
15948 719 : if (poly_int_tree_p (min_val)
15949 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
15950 719 : && multiple_p (const_op01, type_size, &index))
15951 : {
15952 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
15953 719 : op01 = wide_int_to_tree (sizetype, off);
15954 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
15955 : NULL_TREE, NULL_TREE);
15956 : }
15957 : }
15958 : }
15959 : }
15960 :
15961 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15962 18670316 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15963 687136 : && type == TREE_TYPE (TREE_TYPE (subtype))
15964 18673987 : && (!in_gimple_form
15965 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15966 : {
15967 3670 : tree type_domain;
15968 3670 : tree min_val = size_zero_node;
15969 3670 : sub = build_fold_indirect_ref_loc (loc, sub);
15970 3670 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15971 3670 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15972 3670 : min_val = TYPE_MIN_VALUE (type_domain);
15973 3670 : if (in_gimple_form
15974 11 : && TREE_CODE (min_val) != INTEGER_CST)
15975 : return NULL_TREE;
15976 3670 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15977 3670 : NULL_TREE);
15978 : }
15979 :
15980 : return NULL_TREE;
15981 : }
15982 :
15983 : /* Builds an expression for an indirection through T, simplifying some
15984 : cases. */
15985 :
15986 : tree
15987 11865579 : build_fold_indirect_ref_loc (location_t loc, tree t)
15988 : {
15989 11865579 : tree type = TREE_TYPE (TREE_TYPE (t));
15990 11865579 : tree sub = fold_indirect_ref_1 (loc, type, t);
15991 :
15992 11865579 : if (sub)
15993 : return sub;
15994 :
15995 8050715 : return build1_loc (loc, INDIRECT_REF, type, t);
15996 : }
15997 :
15998 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
15999 :
16000 : tree
16001 10418838 : fold_indirect_ref_loc (location_t loc, tree t)
16002 : {
16003 10418838 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16004 :
16005 10418838 : if (sub)
16006 : return sub;
16007 : else
16008 10397597 : return t;
16009 : }
16010 :
16011 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16012 : whose result is ignored. The type of the returned tree need not be
16013 : the same as the original expression. */
16014 :
16015 : tree
16016 135695 : fold_ignored_result (tree t)
16017 : {
16018 135695 : if (!TREE_SIDE_EFFECTS (t))
16019 18001 : return integer_zero_node;
16020 :
16021 156888 : for (;;)
16022 156888 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16023 : {
16024 3833 : case tcc_unary:
16025 3833 : t = TREE_OPERAND (t, 0);
16026 3833 : break;
16027 :
16028 5119 : case tcc_binary:
16029 5119 : case tcc_comparison:
16030 5119 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16031 3204 : t = TREE_OPERAND (t, 0);
16032 1915 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16033 43 : t = TREE_OPERAND (t, 1);
16034 : else
16035 : return t;
16036 : break;
16037 :
16038 100437 : case tcc_expression:
16039 100437 : switch (TREE_CODE (t))
16040 : {
16041 32115 : case COMPOUND_EXPR:
16042 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16043 : return t;
16044 31820 : t = TREE_OPERAND (t, 0);
16045 31820 : break;
16046 :
16047 382 : case COND_EXPR:
16048 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16049 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16050 : return t;
16051 294 : t = TREE_OPERAND (t, 0);
16052 294 : break;
16053 :
16054 : default:
16055 : return t;
16056 : }
16057 : break;
16058 :
16059 : default:
16060 : return t;
16061 : }
16062 : }
16063 :
16064 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16065 :
16066 : tree
16067 3158307880 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16068 : {
16069 3158307880 : tree div = NULL_TREE;
16070 :
16071 3158307880 : if (divisor == 1)
16072 : return value;
16073 :
16074 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16075 : have to do anything. Only do this when we are not given a const,
16076 : because in that case, this check is more expensive than just
16077 : doing it. */
16078 1966599699 : if (TREE_CODE (value) != INTEGER_CST)
16079 : {
16080 377907 : div = build_int_cst (TREE_TYPE (value), divisor);
16081 :
16082 377907 : if (multiple_of_p (TREE_TYPE (value), value, div))
16083 : return value;
16084 : }
16085 :
16086 : /* If divisor is a power of two, simplify this to bit manipulation. */
16087 1966223692 : if (pow2_or_zerop (divisor))
16088 : {
16089 1966223692 : if (TREE_CODE (value) == INTEGER_CST)
16090 : {
16091 1966221792 : wide_int val = wi::to_wide (value);
16092 1966221792 : bool overflow_p;
16093 :
16094 1966221792 : if ((val & (divisor - 1)) == 0)
16095 : return value;
16096 :
16097 4111791 : overflow_p = TREE_OVERFLOW (value);
16098 4111791 : val += divisor - 1;
16099 4111791 : val &= (int) -divisor;
16100 4111791 : if (val == 0)
16101 4 : overflow_p = true;
16102 :
16103 4111791 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16104 1966221792 : }
16105 : else
16106 : {
16107 1900 : tree t;
16108 :
16109 1900 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16110 1900 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16111 1900 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16112 1900 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16113 : }
16114 : }
16115 : else
16116 : {
16117 0 : if (!div)
16118 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16119 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16120 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16121 : }
16122 :
16123 : return value;
16124 : }
16125 :
16126 : /* Likewise, but round down. */
16127 :
16128 : tree
16129 21799445 : round_down_loc (location_t loc, tree value, int divisor)
16130 : {
16131 21799445 : tree div = NULL_TREE;
16132 :
16133 21799445 : gcc_assert (divisor > 0);
16134 21799445 : if (divisor == 1)
16135 : return value;
16136 :
16137 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16138 : have to do anything. Only do this when we are not given a const,
16139 : because in that case, this check is more expensive than just
16140 : doing it. */
16141 21799445 : if (TREE_CODE (value) != INTEGER_CST)
16142 : {
16143 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16144 :
16145 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16146 : return value;
16147 : }
16148 :
16149 : /* If divisor is a power of two, simplify this to bit manipulation. */
16150 21799445 : if (pow2_or_zerop (divisor))
16151 : {
16152 21799445 : tree t;
16153 :
16154 21799445 : t = build_int_cst (TREE_TYPE (value), -divisor);
16155 21799445 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16156 : }
16157 : else
16158 : {
16159 0 : if (!div)
16160 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16161 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16162 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16163 : }
16164 :
16165 : return value;
16166 : }
16167 :
16168 : /* Returns the pointer to the base of the object addressed by EXP and
16169 : extracts the information about the offset of the access, storing it
16170 : to PBITPOS and POFFSET. */
16171 :
16172 : static tree
16173 25889870 : split_address_to_core_and_offset (tree exp,
16174 : poly_int64 *pbitpos, tree *poffset)
16175 : {
16176 25889870 : tree core;
16177 25889870 : machine_mode mode;
16178 25889870 : int unsignedp, reversep, volatilep;
16179 25889870 : poly_int64 bitsize;
16180 25889870 : location_t loc = EXPR_LOCATION (exp);
16181 :
16182 25889870 : STRIP_NOPS (exp);
16183 :
16184 25889870 : if (TREE_CODE (exp) == SSA_NAME)
16185 16267413 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16186 19993809 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16187 49855 : exp = gimple_assign_rhs1 (def);
16188 :
16189 25889870 : if (TREE_CODE (exp) == ADDR_EXPR)
16190 : {
16191 1670079 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16192 : poffset, &mode, &unsignedp, &reversep,
16193 : &volatilep);
16194 : /* If we are left with MEM[a + CST] strip that and add it to the
16195 : pbitpos and return a. */
16196 1670079 : if (TREE_CODE (core) == MEM_REF)
16197 : {
16198 48597 : poly_offset_int tem;
16199 48597 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16200 48597 : tem <<= LOG2_BITS_PER_UNIT;
16201 48597 : tem += *pbitpos;
16202 48597 : if (tem.to_shwi (pbitpos))
16203 48409 : return TREE_OPERAND (core, 0);
16204 : }
16205 1621670 : core = build_fold_addr_expr_loc (loc, core);
16206 : }
16207 24219791 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16208 : {
16209 1074071 : core = TREE_OPERAND (exp, 0);
16210 1074071 : STRIP_NOPS (core);
16211 1074071 : *pbitpos = 0;
16212 1074071 : *poffset = TREE_OPERAND (exp, 1);
16213 1074071 : if (poly_int_tree_p (*poffset))
16214 : {
16215 1055323 : poly_offset_int tem
16216 1055323 : = wi::sext (wi::to_poly_offset (*poffset),
16217 1055323 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16218 1055323 : tem <<= LOG2_BITS_PER_UNIT;
16219 1055323 : if (tem.to_shwi (pbitpos))
16220 1055323 : *poffset = NULL_TREE;
16221 : }
16222 : }
16223 : else
16224 : {
16225 23145720 : core = exp;
16226 23145720 : *pbitpos = 0;
16227 23145720 : *poffset = NULL_TREE;
16228 : }
16229 :
16230 : return core;
16231 : }
16232 :
16233 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16234 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16235 :
16236 : bool
16237 12944935 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16238 : {
16239 12944935 : tree core1, core2;
16240 12944935 : poly_int64 bitpos1, bitpos2;
16241 12944935 : tree toffset1, toffset2, tdiff, type;
16242 :
16243 12944935 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16244 12944935 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16245 :
16246 12944935 : poly_int64 bytepos1, bytepos2;
16247 12944935 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16248 25889870 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16249 25889870 : || !operand_equal_p (core1, core2, 0))
16250 : return false;
16251 :
16252 703949 : if (toffset1 && toffset2)
16253 : {
16254 147 : type = TREE_TYPE (toffset1);
16255 147 : if (type != TREE_TYPE (toffset2))
16256 0 : toffset2 = fold_convert (type, toffset2);
16257 :
16258 147 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16259 147 : if (!cst_and_fits_in_hwi (tdiff))
16260 : return false;
16261 :
16262 53 : *diff = int_cst_value (tdiff);
16263 : }
16264 703802 : else if (toffset1 || toffset2)
16265 : {
16266 : /* If only one of the offsets is non-constant, the difference cannot
16267 : be a constant. */
16268 : return false;
16269 : }
16270 : else
16271 685146 : *diff = 0;
16272 :
16273 685199 : *diff += bytepos1 - bytepos2;
16274 685199 : return true;
16275 : }
16276 :
16277 : /* Return OFF converted to a pointer offset type suitable as offset for
16278 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16279 : tree
16280 52291179 : convert_to_ptrofftype_loc (location_t loc, tree off)
16281 : {
16282 52291179 : if (ptrofftype_p (TREE_TYPE (off)))
16283 : return off;
16284 6123120 : return fold_convert_loc (loc, sizetype, off);
16285 : }
16286 :
16287 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16288 : tree
16289 46267677 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16290 : {
16291 46267677 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16292 46267677 : ptr, convert_to_ptrofftype_loc (loc, off));
16293 : }
16294 :
16295 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16296 : tree
16297 165850 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16298 : {
16299 165850 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16300 165850 : ptr, size_int (off));
16301 : }
16302 :
16303 : /* Return a pointer to a NUL-terminated string containing the sequence
16304 : of bytes corresponding to the representation of the object referred to
16305 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16306 : to an initialized constant array plus some constant offset).
16307 : Set *STRSIZE the number of bytes in the constant sequence including
16308 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16309 : where A is the array that stores the constant sequence that SRC points
16310 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16311 : need not point to a string or even an array of characters but may point
16312 : to an object of any type. */
16313 :
16314 : const char *
16315 12773839 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16316 : {
16317 : /* The offset into the array A storing the string, and A's byte size. */
16318 12773839 : tree offset_node;
16319 12773839 : tree mem_size;
16320 :
16321 12773839 : if (strsize)
16322 4681664 : *strsize = 0;
16323 :
16324 12773839 : if (strsize)
16325 4681664 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16326 : else
16327 8092175 : src = string_constant (src, &offset_node, &mem_size, NULL);
16328 12773839 : if (!src)
16329 : return NULL;
16330 :
16331 2854220 : unsigned HOST_WIDE_INT offset = 0;
16332 2854220 : if (offset_node != NULL_TREE)
16333 : {
16334 2854220 : if (!tree_fits_uhwi_p (offset_node))
16335 : return NULL;
16336 : else
16337 2851070 : offset = tree_to_uhwi (offset_node);
16338 : }
16339 :
16340 2851070 : if (!tree_fits_uhwi_p (mem_size))
16341 : return NULL;
16342 :
16343 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16344 : is stored in and equal to sizeof A. INIT_BYTES is the number
16345 : of bytes in the constant sequence used to initialize the array,
16346 : including any embedded NULs as well as the terminating NUL (for
16347 : strings), but not including any trailing zeros/NULs past
16348 : the terminating one appended implicitly to a string literal to
16349 : zero out the remainder of the array it's stored in. For example,
16350 : given:
16351 : const char a[7] = "abc\0d";
16352 : n = strlen (a + 1);
16353 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16354 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16355 : is equal to strlen (A) + 1. */
16356 2851070 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16357 2851070 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16358 2851070 : const char *string = TREE_STRING_POINTER (src);
16359 :
16360 : /* Ideally this would turn into a gcc_checking_assert over time. */
16361 2851070 : if (init_bytes > array_size)
16362 : init_bytes = array_size;
16363 :
16364 2851070 : if (init_bytes == 0 || offset >= array_size)
16365 : return NULL;
16366 :
16367 2849805 : if (strsize)
16368 : {
16369 : /* Compute and store the number of characters from the beginning
16370 : of the substring at OFFSET to the end, including the terminating
16371 : nul. Offsets past the initial length refer to null strings. */
16372 1442422 : if (offset < init_bytes)
16373 1442422 : *strsize = init_bytes - offset;
16374 : else
16375 0 : *strsize = 1;
16376 : }
16377 : else
16378 : {
16379 1407383 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16380 : /* Support only properly NUL-terminated single byte strings. */
16381 1407383 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16382 : return NULL;
16383 1402598 : if (string[init_bytes - 1] != '\0')
16384 : return NULL;
16385 : }
16386 :
16387 2819130 : return offset < init_bytes ? string + offset : "";
16388 : }
16389 :
16390 : /* Return a pointer to a NUL-terminated string corresponding to
16391 : the expression STR referencing a constant string, possibly
16392 : involving a constant offset. Return null if STR either doesn't
16393 : reference a constant string or if it involves a nonconstant
16394 : offset. */
16395 :
16396 : const char *
16397 8092175 : c_getstr (tree str)
16398 : {
16399 8092175 : return getbyterep (str, NULL);
16400 : }
16401 :
16402 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16403 : may be nonzero, with precision PREC, the precision of T's type. */
16404 :
16405 : static wide_int
16406 260734921 : tree_nonzero_bits (const_tree t, unsigned prec)
16407 : {
16408 260734921 : switch (TREE_CODE (t))
16409 : {
16410 8951224 : case INTEGER_CST:
16411 8951224 : return wi::to_wide (t);
16412 146022684 : case SSA_NAME:
16413 146022684 : return get_nonzero_bits (t);
16414 261486 : case NON_LVALUE_EXPR:
16415 261486 : case SAVE_EXPR:
16416 261486 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16417 519811 : case BIT_AND_EXPR:
16418 1039622 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16419 1559433 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16420 19457 : case BIT_IOR_EXPR:
16421 19457 : case BIT_XOR_EXPR:
16422 38914 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16423 58371 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16424 72811 : case COND_EXPR:
16425 145622 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16426 218433 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16427 53338109 : CASE_CONVERT:
16428 53338109 : if (TREE_TYPE (t) != error_mark_node
16429 53338109 : && !error_operand_p (TREE_OPERAND (t, 0)))
16430 : {
16431 53338108 : tree op0 = TREE_OPERAND (t, 0);
16432 53338108 : tree inner_type = TREE_TYPE (op0);
16433 53338108 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16434 106676216 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16435 106676216 : prec, TYPE_SIGN (inner_type));
16436 : }
16437 : break;
16438 14381963 : case PLUS_EXPR:
16439 14381963 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16440 : {
16441 14381963 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16442 14381963 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16443 14381963 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16444 559990 : return wi::bit_or (nzbits1, nzbits2);
16445 14381963 : }
16446 : break;
16447 170585 : case LSHIFT_EXPR:
16448 170585 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16449 170585 : && TREE_TYPE (t) != error_mark_node)
16450 : {
16451 98663 : tree type = TREE_TYPE (t);
16452 98663 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16453 98663 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16454 98663 : return wi::neg_p (arg1)
16455 197326 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16456 98663 : : wi::lshift (nzbits, arg1);
16457 98663 : }
16458 : break;
16459 162682 : case RSHIFT_EXPR:
16460 162682 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16461 162682 : && TREE_TYPE (t) != error_mark_node)
16462 : {
16463 160738 : tree type = TREE_TYPE (t);
16464 160738 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16465 160738 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16466 160738 : return wi::neg_p (arg1)
16467 321476 : ? wi::lshift (nzbits, -arg1)
16468 160738 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16469 160738 : }
16470 : break;
16471 : default:
16472 : break;
16473 : }
16474 :
16475 50729949 : return wi::shwi (-1, prec);
16476 : }
16477 :
16478 : /* Given a tree T, compute which bits in T may be nonzero. */
16479 :
16480 : wide_int
16481 176887842 : tree_nonzero_bits (const_tree t)
16482 : {
16483 176887842 : if (error_operand_p (t))
16484 0 : return wi::shwi (-1, 64);
16485 176887842 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16486 : }
16487 :
16488 : /* Helper function for address compare simplifications in match.pd.
16489 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16490 : TYPE is the type of comparison operands.
16491 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16492 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16493 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16494 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16495 : and 2 if unknown. */
16496 :
16497 : int
16498 5316474 : address_compare (tree_code code, tree type, tree op0, tree op1,
16499 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16500 : bool generic)
16501 : {
16502 5316474 : if (TREE_CODE (op0) == SSA_NAME)
16503 37053 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16504 5316474 : if (TREE_CODE (op1) == SSA_NAME)
16505 4546 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16506 5316474 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16507 5316474 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16508 5316474 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16509 5316474 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16510 5316474 : if (base0 && TREE_CODE (base0) == MEM_REF)
16511 : {
16512 36638 : off0 += mem_ref_offset (base0).force_shwi ();
16513 36638 : base0 = TREE_OPERAND (base0, 0);
16514 : }
16515 5316474 : if (base1 && TREE_CODE (base1) == MEM_REF)
16516 : {
16517 3702 : off1 += mem_ref_offset (base1).force_shwi ();
16518 3702 : base1 = TREE_OPERAND (base1, 0);
16519 : }
16520 5316474 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16521 : return 2;
16522 :
16523 5304260 : int equal = 2;
16524 : /* Punt in GENERIC on variables with value expressions;
16525 : the value expressions might point to fields/elements
16526 : of other vars etc. */
16527 5304260 : if (generic
16528 5304260 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16529 5167306 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16530 : return 2;
16531 5303603 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16532 : {
16533 1257428 : symtab_node *node0 = symtab_node::get_create (base0);
16534 1257428 : symtab_node *node1 = symtab_node::get_create (base1);
16535 1257428 : equal = node0->equal_address_to (node1);
16536 : }
16537 4046175 : else if ((DECL_P (base0)
16538 246446 : || TREE_CODE (base0) == SSA_NAME
16539 210927 : || TREE_CODE (base0) == STRING_CST)
16540 4045977 : && (DECL_P (base1)
16541 214577 : || TREE_CODE (base1) == SSA_NAME
16542 211101 : || TREE_CODE (base1) == STRING_CST))
16543 4045962 : equal = (base0 == base1);
16544 : /* Assume different STRING_CSTs with the same content will be
16545 : merged. */
16546 5303390 : if (equal == 0
16547 83893 : && TREE_CODE (base0) == STRING_CST
16548 17626 : && TREE_CODE (base1) == STRING_CST
16549 17451 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16550 5303390 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16551 6189 : TREE_STRING_LENGTH (base0)) == 0)
16552 : equal = 1;
16553 5299170 : if (equal == 1)
16554 : {
16555 5199079 : if (code == EQ_EXPR
16556 5199079 : || code == NE_EXPR
16557 : /* If the offsets are equal we can ignore overflow. */
16558 144130 : || known_eq (off0, off1)
16559 288052 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16560 : /* Or if we compare using pointers to decls or strings. */
16561 5343105 : || (POINTER_TYPE_P (type)
16562 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16563 5199079 : return 1;
16564 : return 2;
16565 : }
16566 104524 : if (equal != 0)
16567 : return equal;
16568 79460 : if (code != EQ_EXPR && code != NE_EXPR)
16569 : return 2;
16570 :
16571 : /* At this point we know (or assume) the two pointers point at
16572 : different objects. */
16573 74181 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16574 74181 : off0.is_constant (&ioff0);
16575 74181 : off1.is_constant (&ioff1);
16576 : /* Punt on non-zero offsets from functions. */
16577 74181 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16578 74181 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16579 : return 2;
16580 : /* Or if the bases are neither decls nor string literals. */
16581 74181 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16582 : return 2;
16583 39398 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16584 : return 2;
16585 : /* For initializers, assume addresses of different functions are
16586 : different. */
16587 39398 : if (folding_initializer
16588 13832 : && TREE_CODE (base0) == FUNCTION_DECL
16589 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16590 : return 0;
16591 :
16592 : /* Compute whether one address points to the start of one
16593 : object and another one to the end of another one. */
16594 39378 : poly_int64 size0 = 0, size1 = 0;
16595 39378 : if (TREE_CODE (base0) == STRING_CST)
16596 : {
16597 13039 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16598 : equal = 2;
16599 : else
16600 : size0 = TREE_STRING_LENGTH (base0);
16601 : }
16602 26339 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16603 : size0 = 1;
16604 : else
16605 : {
16606 26019 : tree sz0 = DECL_SIZE_UNIT (base0);
16607 26019 : if (!tree_fits_poly_int64_p (sz0))
16608 : equal = 2;
16609 : else
16610 26019 : size0 = tree_to_poly_int64 (sz0);
16611 : }
16612 39378 : if (TREE_CODE (base1) == STRING_CST)
16613 : {
16614 13144 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16615 : equal = 2;
16616 : else
16617 : size1 = TREE_STRING_LENGTH (base1);
16618 : }
16619 26234 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16620 : size1 = 1;
16621 : else
16622 : {
16623 25918 : tree sz1 = DECL_SIZE_UNIT (base1);
16624 25918 : if (!tree_fits_poly_int64_p (sz1))
16625 : equal = 2;
16626 : else
16627 25918 : size1 = tree_to_poly_int64 (sz1);
16628 : }
16629 39378 : if (equal == 0)
16630 : {
16631 : /* If one offset is pointing (or could be) to the beginning of one
16632 : object and the other is pointing to one past the last byte of the
16633 : other object, punt. */
16634 39366 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16635 : equal = 2;
16636 39229 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16637 : equal = 2;
16638 : /* If both offsets are the same, there are some cases we know that are
16639 : ok. Either if we know they aren't zero, or if we know both sizes
16640 : are no zero. */
16641 : if (equal == 2
16642 273 : && known_eq (off0, off1)
16643 22 : && (known_ne (off0, 0)
16644 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16645 : equal = 0;
16646 : }
16647 :
16648 : /* At this point, equal is 2 if either one or both pointers are out of
16649 : bounds of their object, or one points to start of its object and the
16650 : other points to end of its object. This is unspecified behavior
16651 : e.g. in C++. Otherwise equal is 0. */
16652 39378 : if (folding_cxx_constexpr && equal)
16653 : return equal;
16654 :
16655 : /* When both pointers point to string literals, even when equal is 0,
16656 : due to tail merging of string literals the pointers might be the same. */
16657 39315 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16658 : {
16659 13000 : if (ioff0 < 0
16660 13000 : || ioff1 < 0
16661 13000 : || ioff0 > TREE_STRING_LENGTH (base0)
16662 25988 : || ioff1 > TREE_STRING_LENGTH (base1))
16663 : return 2;
16664 :
16665 : /* If the bytes in the string literals starting at the pointers
16666 : differ, the pointers need to be different. */
16667 12988 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16668 12988 : TREE_STRING_POINTER (base1) + ioff1,
16669 12988 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16670 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16671 : {
16672 3910 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16673 3910 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16674 3910 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16675 : ioffmin) == 0)
16676 : /* If even the bytes in the string literal before the
16677 : pointers are the same, the string literals could be
16678 : tail merged. */
16679 : return 2;
16680 : }
16681 9090 : return 0;
16682 : }
16683 :
16684 26315 : if (folding_cxx_constexpr)
16685 : return 0;
16686 :
16687 : /* If this is a pointer comparison, ignore for now even
16688 : valid equalities where one pointer is the offset zero
16689 : of one object and the other to one past end of another one. */
16690 12606 : if (!INTEGRAL_TYPE_P (type))
16691 : return 0;
16692 :
16693 : /* Assume that string literals can't be adjacent to variables
16694 : (automatic or global). */
16695 309 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16696 : return 0;
16697 :
16698 : /* Assume that automatic variables can't be adjacent to global
16699 : variables. */
16700 289 : if (is_global_var (base0) != is_global_var (base1))
16701 2 : return 0;
16702 :
16703 : return equal;
16704 : }
16705 :
16706 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16707 : tree
16708 58 : ctor_single_nonzero_element (const_tree t)
16709 : {
16710 58 : unsigned HOST_WIDE_INT idx;
16711 58 : constructor_elt *ce;
16712 58 : tree elt = NULL_TREE;
16713 :
16714 58 : if (TREE_CODE (t) != CONSTRUCTOR)
16715 : return NULL_TREE;
16716 125 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16717 122 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16718 : {
16719 113 : if (elt)
16720 : return NULL_TREE;
16721 58 : elt = ce->value;
16722 : }
16723 : return elt;
16724 : }
16725 :
16726 : #if CHECKING_P
16727 :
16728 : namespace selftest {
16729 :
16730 : /* Helper functions for writing tests of folding trees. */
16731 :
16732 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16733 :
16734 : static void
16735 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16736 : tree constant)
16737 : {
16738 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16739 16 : }
16740 :
16741 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16742 : wrapping WRAPPED_EXPR. */
16743 :
16744 : static void
16745 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16746 : tree wrapped_expr)
16747 : {
16748 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16749 12 : ASSERT_NE (wrapped_expr, result);
16750 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16751 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16752 12 : }
16753 :
16754 : /* Verify that various arithmetic binary operations are folded
16755 : correctly. */
16756 :
16757 : static void
16758 4 : test_arithmetic_folding ()
16759 : {
16760 4 : tree type = integer_type_node;
16761 4 : tree x = create_tmp_var_raw (type, "x");
16762 4 : tree zero = build_zero_cst (type);
16763 4 : tree one = build_int_cst (type, 1);
16764 :
16765 : /* Addition. */
16766 : /* 1 <-- (0 + 1) */
16767 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16768 : one);
16769 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16770 : one);
16771 :
16772 : /* (nonlvalue)x <-- (x + 0) */
16773 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16774 : x);
16775 :
16776 : /* Subtraction. */
16777 : /* 0 <-- (x - x) */
16778 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16779 : zero);
16780 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16781 : x);
16782 :
16783 : /* Multiplication. */
16784 : /* 0 <-- (x * 0) */
16785 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16786 : zero);
16787 :
16788 : /* (nonlvalue)x <-- (x * 1) */
16789 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16790 : x);
16791 4 : }
16792 :
16793 : namespace test_operand_equality {
16794 :
16795 : /* Verify structural equality. */
16796 :
16797 : /* Execute fold_vec_perm_cst unit tests. */
16798 :
16799 : static void
16800 4 : test ()
16801 : {
16802 4 : tree stype = integer_type_node;
16803 4 : tree utype = unsigned_type_node;
16804 4 : tree x = create_tmp_var_raw (stype, "x");
16805 4 : tree y = create_tmp_var_raw (stype, "y");
16806 4 : tree z = create_tmp_var_raw (stype, "z");
16807 4 : tree four = build_int_cst (stype, 4);
16808 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16809 4 : tree rhs1 = fold_convert (stype,
16810 : fold_build2 (PLUS_EXPR, utype,
16811 : fold_convert (utype, x),
16812 : fold_convert (utype, y)));
16813 :
16814 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16815 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16816 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16817 :
16818 : /* (int)(unsigned) x == x. */
16819 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16820 : build1 (NOP_EXPR, utype, x));
16821 4 : tree rhs2 = x;
16822 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16823 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16824 :
16825 : /* (unsigned x) + (unsigned y) == x + y. */
16826 4 : tree lhs3 = lhs1;
16827 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16828 : fold_convert (utype, x),
16829 : fold_convert (utype, y));
16830 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16831 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16832 :
16833 : /* (unsigned x) / (unsigned y) == x / y. */
16834 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16835 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16836 : fold_convert (utype, x),
16837 : fold_convert (utype, y));
16838 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16839 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16840 :
16841 : /* (long x) / 4 == (long)(x / 4). */
16842 4 : tree lstype = long_long_integer_type_node;
16843 4 : tree lfour = build_int_cst (lstype, 4);
16844 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16845 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16846 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16847 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16848 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16849 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16850 :
16851 : /* (unsigned x) / 4 == x / 4. */
16852 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16853 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16854 : fold_convert (utype, x),
16855 : fold_convert (utype, four));
16856 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16857 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16858 :
16859 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16860 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16861 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16862 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16863 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16864 :
16865 : /* (unsigned x) + 4 == x + 4. */
16866 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16867 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16868 : fold_convert (utype, x),
16869 : fold_convert (utype, four));
16870 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16871 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16872 :
16873 : /* (unsigned x) + 4 == 4 + x. */
16874 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16875 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16876 : fold_convert (utype, x),
16877 : fold_convert (utype, four));
16878 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16879 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16880 :
16881 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16882 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16883 : fold_build2 (MULT_EXPR, stype,
16884 : fold_build2 (PLUS_EXPR, stype, four, x),
16885 : y),
16886 : z);
16887 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16888 : fold_build2 (PLUS_EXPR, utype,
16889 : fold_convert (utype, x),
16890 : fold_convert (utype, four)),
16891 : fold_convert (utype, y));
16892 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16893 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16894 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16895 4 : }
16896 : }
16897 :
16898 : namespace test_fold_vec_perm_cst {
16899 :
16900 : /* Build a VECTOR_CST corresponding to VMODE, and has
16901 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16902 : Fill it with randomized elements, using rand() % THRESHOLD. */
16903 :
16904 : static tree
16905 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16906 : unsigned nelts_per_pattern,
16907 : int step = 0, bool natural_stepped = false,
16908 : int threshold = 100)
16909 : {
16910 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16911 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16912 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16913 :
16914 : // Fill a0 for each pattern
16915 0 : for (unsigned i = 0; i < npatterns; i++)
16916 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16917 :
16918 0 : if (nelts_per_pattern == 1)
16919 0 : return builder.build ();
16920 :
16921 : // Fill a1 for each pattern
16922 0 : for (unsigned i = 0; i < npatterns; i++)
16923 : {
16924 0 : tree a1;
16925 0 : if (natural_stepped)
16926 : {
16927 0 : tree a0 = builder[i];
16928 0 : wide_int a0_val = wi::to_wide (a0);
16929 0 : wide_int a1_val = a0_val + step;
16930 0 : a1 = wide_int_to_tree (inner_type, a1_val);
16931 0 : }
16932 : else
16933 0 : a1 = build_int_cst (inner_type, rand () % threshold);
16934 0 : builder.quick_push (a1);
16935 : }
16936 0 : if (nelts_per_pattern == 2)
16937 0 : return builder.build ();
16938 :
16939 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
16940 : {
16941 0 : tree prev_elem = builder[i - npatterns];
16942 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
16943 0 : wide_int val = prev_elem_val + step;
16944 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
16945 0 : }
16946 :
16947 0 : return builder.build ();
16948 0 : }
16949 :
16950 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16951 : when result is VLA. */
16952 :
16953 : static void
16954 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
16955 : tree res, tree *expected_res)
16956 : {
16957 : /* Actual npatterns and encoded_elts in res may be less than expected due
16958 : to canonicalization. */
16959 0 : ASSERT_TRUE (res != NULL_TREE);
16960 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
16961 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
16962 :
16963 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
16964 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16965 0 : }
16966 :
16967 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
16968 : when the result is VLS. */
16969 :
16970 : static void
16971 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
16972 : {
16973 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
16974 0 : for (unsigned i = 0; i < expected_nelts; i++)
16975 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
16976 0 : }
16977 :
16978 : /* Helper routine to push multiple elements into BUILDER. */
16979 : template<unsigned N>
16980 0 : static void builder_push_elems (vec_perm_builder& builder,
16981 : poly_uint64 (&elems)[N])
16982 : {
16983 0 : for (unsigned i = 0; i < N; i++)
16984 0 : builder.quick_push (elems[i]);
16985 0 : }
16986 :
16987 : #define ARG0(index) vector_cst_elt (arg0, index)
16988 : #define ARG1(index) vector_cst_elt (arg1, index)
16989 :
16990 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
16991 :
16992 : static void
16993 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
16994 : {
16995 0 : for (int i = 0; i < 10; i++)
16996 : {
16997 : /* Case 1:
16998 : sel = { 0, 4, 1, 5, ... }
16999 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17000 0 : {
17001 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17002 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17003 :
17004 0 : tree inner_type
17005 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17006 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17007 :
17008 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17009 0 : vec_perm_builder builder (res_len, 4, 1);
17010 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17011 0 : builder_push_elems (builder, mask_elems);
17012 :
17013 0 : vec_perm_indices sel (builder, 2, 4);
17014 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17015 :
17016 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17017 0 : validate_res (4, 1, res, expected_res);
17018 0 : }
17019 :
17020 : /* Case 2: Same as case 1, but contains an out of bounds access which
17021 : should wrap around.
17022 : sel = {0, 8, 4, 12, ...} (4, 1)
17023 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17024 0 : {
17025 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17026 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17027 :
17028 0 : tree inner_type
17029 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17030 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17031 :
17032 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17033 0 : vec_perm_builder builder (res_len, 4, 1);
17034 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17035 0 : builder_push_elems (builder, mask_elems);
17036 :
17037 0 : vec_perm_indices sel (builder, 2, 4);
17038 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17039 :
17040 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17041 0 : validate_res (4, 1, res, expected_res);
17042 0 : }
17043 : }
17044 0 : }
17045 :
17046 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17047 :
17048 : static void
17049 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17050 : {
17051 0 : for (int i = 0; i < 10; i++)
17052 : {
17053 : /* Case 1:
17054 : sel = { 0, 1, 2, 3}
17055 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17056 0 : {
17057 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17058 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17059 :
17060 0 : tree inner_type
17061 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17062 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17063 :
17064 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17065 0 : vec_perm_builder builder (res_len, 4, 1);
17066 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17067 0 : builder_push_elems (builder, mask_elems);
17068 :
17069 0 : vec_perm_indices sel (builder, 2,
17070 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17071 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17072 :
17073 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17074 0 : validate_res_vls (res, expected_res, 4);
17075 0 : }
17076 :
17077 : /* Case 2: Same as Case 1, but crossing input vector.
17078 : sel = {0, 2, 4, 6}
17079 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17080 : Since we cannot determine, which vector to choose from during
17081 : compile time, should return NULL_TREE. */
17082 0 : {
17083 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17084 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17085 :
17086 0 : tree inner_type
17087 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17088 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17089 :
17090 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17091 0 : vec_perm_builder builder (res_len, 4, 1);
17092 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17093 0 : builder_push_elems (builder, mask_elems);
17094 :
17095 0 : vec_perm_indices sel (builder, 2,
17096 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17097 0 : const char *reason;
17098 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17099 :
17100 0 : ASSERT_TRUE (res == NULL_TREE);
17101 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17102 0 : }
17103 : }
17104 0 : }
17105 :
17106 : /* Test all input vectors. */
17107 :
17108 : static void
17109 0 : test_all_nunits (machine_mode vmode)
17110 : {
17111 : /* Test with 10 different inputs. */
17112 0 : for (int i = 0; i < 10; i++)
17113 : {
17114 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17115 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17116 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17117 :
17118 : /* Case 1: mask = {0, ...} // (1, 1)
17119 : res = { arg0[0], ... } // (1, 1) */
17120 0 : {
17121 0 : vec_perm_builder builder (len, 1, 1);
17122 0 : builder.quick_push (0);
17123 0 : vec_perm_indices sel (builder, 2, len);
17124 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17125 0 : tree expected_res[] = { ARG0(0) };
17126 0 : validate_res (1, 1, res, expected_res);
17127 0 : }
17128 :
17129 : /* Case 2: mask = {len, ...} // (1, 1)
17130 : res = { arg1[0], ... } // (1, 1) */
17131 0 : {
17132 0 : vec_perm_builder builder (len, 1, 1);
17133 0 : builder.quick_push (len);
17134 0 : vec_perm_indices sel (builder, 2, len);
17135 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17136 :
17137 0 : tree expected_res[] = { ARG1(0) };
17138 0 : validate_res (1, 1, res, expected_res);
17139 0 : }
17140 : }
17141 0 : }
17142 :
17143 : /* Test all vectors which contain at-least 2 elements. */
17144 :
17145 : static void
17146 0 : test_nunits_min_2 (machine_mode vmode)
17147 : {
17148 0 : for (int i = 0; i < 10; i++)
17149 : {
17150 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17151 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17152 0 : {
17153 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17154 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17155 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17156 :
17157 0 : vec_perm_builder builder (len, 2, 1);
17158 0 : poly_uint64 mask_elems[] = { 0, len };
17159 0 : builder_push_elems (builder, mask_elems);
17160 :
17161 0 : vec_perm_indices sel (builder, 2, len);
17162 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17163 :
17164 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17165 0 : validate_res (2, 1, res, expected_res);
17166 0 : }
17167 :
17168 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17169 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17170 0 : {
17171 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17172 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17173 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17174 :
17175 0 : vec_perm_builder builder (len, 2, 2);
17176 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17177 0 : builder_push_elems (builder, mask_elems);
17178 :
17179 0 : vec_perm_indices sel (builder, 2, len);
17180 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17181 :
17182 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17183 0 : validate_res (2, 2, res, expected_res);
17184 0 : }
17185 :
17186 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17187 : Test that the stepped sequence of the pattern selects from
17188 : same input pattern. Since input vectors have npatterns = 2,
17189 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17190 : in input vector. So return NULL_TREE. */
17191 0 : {
17192 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17193 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17194 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17195 :
17196 0 : vec_perm_builder builder (len, 1, 3);
17197 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17198 0 : builder_push_elems (builder, mask_elems);
17199 :
17200 0 : vec_perm_indices sel (builder, 2, len);
17201 0 : const char *reason;
17202 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17203 : &reason);
17204 0 : ASSERT_TRUE (res == NULL_TREE);
17205 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17206 0 : }
17207 :
17208 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17209 : Test that stepped sequence of the pattern selects from arg0.
17210 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17211 0 : {
17212 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17213 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17214 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17215 :
17216 0 : vec_perm_builder builder (len, 1, 3);
17217 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17218 0 : builder_push_elems (builder, mask_elems);
17219 :
17220 0 : vec_perm_indices sel (builder, 2, len);
17221 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17222 :
17223 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17224 0 : validate_res (1, 3, res, expected_res);
17225 0 : }
17226 :
17227 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17228 : In this case ensure that arg has a natural stepped sequence
17229 : to preserve arg's encoding.
17230 :
17231 : As a concrete example, consider:
17232 : arg0: { -16, -9, -10, ... } // (1, 3)
17233 : arg1: { -12, -5, -6, ... } // (1, 3)
17234 : sel = { 0, len, len + 1, ... } // (1, 3)
17235 :
17236 : This will create res with following encoding:
17237 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17238 : = { -16, -12, -5, ... }
17239 :
17240 : The step in above encoding would be: (-5) - (-12) = 7
17241 : And hence res[3] would be computed as -5 + 7 = 2.
17242 : instead of arg1[2], ie, -6.
17243 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17244 : for this case. */
17245 0 : {
17246 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17247 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17248 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17249 :
17250 0 : vec_perm_builder builder (len, 1, 3);
17251 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17252 0 : builder_push_elems (builder, mask_elems);
17253 :
17254 0 : vec_perm_indices sel (builder, 2, len);
17255 0 : const char *reason;
17256 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17257 : stepped pattern, even if we didn't explicitly tell it to. So folding
17258 : may not always fail, but if it does, ensure that's because arg1 does
17259 : not have a natural stepped sequence (and not due to other reason) */
17260 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17261 0 : if (res == NULL_TREE)
17262 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17263 0 : }
17264 :
17265 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17266 : sequence and thus folding should be valid for this case. */
17267 0 : {
17268 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17269 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17270 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17271 :
17272 0 : vec_perm_builder builder (len, 1, 3);
17273 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17274 0 : builder_push_elems (builder, mask_elems);
17275 :
17276 0 : vec_perm_indices sel (builder, 2, len);
17277 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17278 :
17279 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17280 0 : validate_res (1, 3, res, expected_res);
17281 0 : }
17282 :
17283 : /* Case 8: Same as aarch64/sve/slp_3.c:
17284 : arg0, arg1 are dup vectors.
17285 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17286 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17287 :
17288 : In this case, since the input vectors are dup, only the first two
17289 : elements per pattern in sel are considered significant. */
17290 0 : {
17291 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17292 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17293 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17294 :
17295 0 : vec_perm_builder builder (len, 2, 3);
17296 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17297 0 : builder_push_elems (builder, mask_elems);
17298 :
17299 0 : vec_perm_indices sel (builder, 2, len);
17300 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17301 :
17302 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17303 0 : validate_res (2, 1, res, expected_res);
17304 0 : }
17305 : }
17306 0 : }
17307 :
17308 : /* Test all vectors which contain at-least 4 elements. */
17309 :
17310 : static void
17311 0 : test_nunits_min_4 (machine_mode vmode)
17312 : {
17313 0 : for (int i = 0; i < 10; i++)
17314 : {
17315 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17316 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17317 0 : {
17318 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17319 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17320 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17321 :
17322 0 : vec_perm_builder builder (len, 4, 1);
17323 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17324 0 : builder_push_elems (builder, mask_elems);
17325 :
17326 0 : vec_perm_indices sel (builder, 2, len);
17327 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17328 :
17329 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17330 0 : validate_res (4, 1, res, expected_res);
17331 0 : }
17332 :
17333 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17334 : res: { arg0[0], arg0[1], arg0[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 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17339 :
17340 0 : vec_perm_builder builder (arg0_len, 1, 3);
17341 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17342 0 : builder_push_elems (builder, mask_elems);
17343 :
17344 0 : vec_perm_indices sel (builder, 2, arg0_len);
17345 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17346 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17347 0 : validate_res (1, 3, res, expected_res);
17348 0 : }
17349 :
17350 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17351 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17352 0 : {
17353 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17354 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17355 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17356 :
17357 0 : vec_perm_builder builder (len, 1, 3);
17358 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17359 0 : builder_push_elems (builder, mask_elems);
17360 :
17361 0 : vec_perm_indices sel (builder, 2, len);
17362 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17363 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17364 0 : validate_res (1, 3, res, expected_res);
17365 0 : }
17366 :
17367 : /* Case 4:
17368 : sel = { len, 0, 2, ... } // (1, 3)
17369 : This should return NULL because we cross the input vectors.
17370 : Because,
17371 : Let's assume len = C + Cx
17372 : a1 = 0
17373 : S = 2
17374 : esel = arg0_len / sel_npatterns = C + Cx
17375 : ae = 0 + (esel - 2) * S
17376 : = 0 + (C + Cx - 2) * 2
17377 : = 2(C-2) + 2Cx
17378 :
17379 : For C >= 4:
17380 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17381 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17382 : Since q1 != qe, we cross input vectors.
17383 : So return NULL_TREE. */
17384 0 : {
17385 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17386 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17387 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17388 :
17389 0 : vec_perm_builder builder (arg0_len, 1, 3);
17390 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17391 0 : builder_push_elems (builder, mask_elems);
17392 :
17393 0 : vec_perm_indices sel (builder, 2, arg0_len);
17394 0 : const char *reason;
17395 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17396 0 : ASSERT_TRUE (res == NULL_TREE);
17397 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17398 0 : }
17399 :
17400 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17401 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17402 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17403 :
17404 : Note that fold_vec_perm_cst will set
17405 : res_npatterns = max(4, max(4, 2)) = 4
17406 : However after canonicalizing, we will end up with shape (2, 2). */
17407 0 : {
17408 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17409 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17410 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17411 :
17412 0 : vec_perm_builder builder (len, 2, 2);
17413 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17414 0 : builder_push_elems (builder, mask_elems);
17415 :
17416 0 : vec_perm_indices sel (builder, 2, len);
17417 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17418 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17419 0 : validate_res (2, 2, res, expected_res);
17420 0 : }
17421 :
17422 : /* Case 6: Test combination in sel, where one pattern is dup and other
17423 : is stepped sequence.
17424 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17425 : res = { arg0[0], arg0[0], arg0[0],
17426 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17427 0 : {
17428 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17429 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17430 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17431 :
17432 0 : vec_perm_builder builder (len, 2, 3);
17433 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17434 0 : builder_push_elems (builder, mask_elems);
17435 :
17436 0 : vec_perm_indices sel (builder, 2, len);
17437 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17438 :
17439 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17440 0 : ARG0(1), ARG0(0), ARG0(2) };
17441 0 : validate_res (2, 3, res, expected_res);
17442 0 : }
17443 :
17444 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17445 : when arg0, arg1 and sel have different number of patterns.
17446 : arg0 is of shape (1, 1)
17447 : arg1 is of shape (4, 1)
17448 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17449 :
17450 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17451 : However,
17452 : step = (len+2) - (len+1) = 1
17453 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17454 : Since step is not a multiple of arg_npatterns,
17455 : valid_mask_for_fold_vec_perm_cst should return false,
17456 : and thus fold_vec_perm_cst should return NULL_TREE. */
17457 0 : {
17458 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17459 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17460 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17461 :
17462 0 : vec_perm_builder builder (len, 2, 3);
17463 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17464 0 : builder_push_elems (builder, mask_elems);
17465 :
17466 0 : vec_perm_indices sel (builder, 2, len);
17467 0 : const char *reason;
17468 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17469 :
17470 0 : ASSERT_TRUE (res == NULL_TREE);
17471 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17472 0 : }
17473 :
17474 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17475 : check that the result is not a stepped sequence either, even
17476 : if sel has a stepped sequence. */
17477 0 : {
17478 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17479 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17480 :
17481 0 : vec_perm_builder builder (len, 1, 3);
17482 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17483 0 : builder_push_elems (builder, mask_elems);
17484 :
17485 0 : vec_perm_indices sel (builder, 1, len);
17486 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17487 :
17488 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17489 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17490 0 : }
17491 :
17492 : /* Case 9: If sel doesn't contain a stepped sequence,
17493 : check that the result has same encoding as sel, irrespective
17494 : of shape of input vectors. */
17495 0 : {
17496 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17497 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17498 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17499 :
17500 0 : vec_perm_builder builder (len, 1, 2);
17501 0 : poly_uint64 mask_elems[] = { 0, len };
17502 0 : builder_push_elems (builder, mask_elems);
17503 :
17504 0 : vec_perm_indices sel (builder, 2, len);
17505 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17506 :
17507 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17508 0 : validate_res (sel.encoding ().npatterns (),
17509 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17510 0 : }
17511 : }
17512 0 : }
17513 :
17514 : /* Test all vectors which contain at-least 8 elements. */
17515 :
17516 : static void
17517 0 : test_nunits_min_8 (machine_mode vmode)
17518 : {
17519 0 : for (int i = 0; i < 10; i++)
17520 : {
17521 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17522 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17523 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17524 : arg0[2], arg0[0], arg0[3], arg1[0],
17525 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17526 0 : {
17527 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17528 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17529 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17530 :
17531 0 : vec_perm_builder builder(len, 4, 3);
17532 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17533 0 : 4, 0, 5, len };
17534 0 : builder_push_elems (builder, mask_elems);
17535 :
17536 0 : vec_perm_indices sel (builder, 2, len);
17537 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17538 :
17539 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17540 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17541 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17542 0 : validate_res (4, 3, res, expected_res);
17543 0 : }
17544 : }
17545 0 : }
17546 :
17547 : /* Test vectors for which nunits[0] <= 4. */
17548 :
17549 : static void
17550 0 : test_nunits_max_4 (machine_mode vmode)
17551 : {
17552 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17553 : This should return NULL_TREE because the index 4 may choose
17554 : from either arg0 or arg1 depending on vector length. */
17555 0 : {
17556 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17557 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17558 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17559 :
17560 0 : vec_perm_builder builder (len, 1, 2);
17561 0 : poly_uint64 mask_elems[] = {0, 4};
17562 0 : builder_push_elems (builder, mask_elems);
17563 :
17564 0 : vec_perm_indices sel (builder, 2, len);
17565 0 : const char *reason;
17566 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17567 0 : ASSERT_TRUE (res == NULL_TREE);
17568 0 : ASSERT_TRUE (reason != NULL);
17569 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17570 0 : }
17571 0 : }
17572 :
17573 : #undef ARG0
17574 : #undef ARG1
17575 :
17576 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17577 :
17578 : static bool
17579 0 : is_simple_vla_size (poly_uint64 size)
17580 : {
17581 124 : if (size.is_constant ()
17582 : || !pow2p_hwi (size.coeffs[0]))
17583 0 : return false;
17584 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17585 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17586 : return false;
17587 : return true;
17588 : }
17589 :
17590 : /* Execute fold_vec_perm_cst unit tests. */
17591 :
17592 : static void
17593 4 : test ()
17594 : {
17595 4 : machine_mode vnx4si_mode = E_VOIDmode;
17596 4 : machine_mode v4si_mode = E_VOIDmode;
17597 :
17598 4 : machine_mode vmode;
17599 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17600 : {
17601 : /* Obtain modes corresponding to VNx4SI and V4SI,
17602 : to call mixed mode tests below.
17603 : FIXME: Is there a better way to do this ? */
17604 124 : if (GET_MODE_INNER (vmode) == SImode)
17605 : {
17606 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17607 124 : if (is_simple_vla_size (nunits)
17608 : && nunits.coeffs[0] == 4)
17609 : vnx4si_mode = vmode;
17610 124 : else if (known_eq (nunits, poly_uint64 (4)))
17611 124 : v4si_mode = vmode;
17612 : }
17613 :
17614 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17615 : || !targetm.vector_mode_supported_p (vmode))
17616 124 : continue;
17617 :
17618 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17619 : test_all_nunits (vmode);
17620 : if (nunits.coeffs[0] >= 2)
17621 : test_nunits_min_2 (vmode);
17622 : if (nunits.coeffs[0] >= 4)
17623 : test_nunits_min_4 (vmode);
17624 : if (nunits.coeffs[0] >= 8)
17625 : test_nunits_min_8 (vmode);
17626 :
17627 : if (nunits.coeffs[0] <= 4)
17628 : test_nunits_max_4 (vmode);
17629 : }
17630 :
17631 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17632 : && targetm.vector_mode_supported_p (vnx4si_mode)
17633 : && targetm.vector_mode_supported_p (v4si_mode))
17634 : {
17635 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17636 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17637 : }
17638 4 : }
17639 : } // end of test_fold_vec_perm_cst namespace
17640 :
17641 : /* Verify that various binary operations on vectors are folded
17642 : correctly. */
17643 :
17644 : static void
17645 4 : test_vector_folding ()
17646 : {
17647 4 : tree inner_type = integer_type_node;
17648 4 : tree type = build_vector_type (inner_type, 4);
17649 4 : tree zero = build_zero_cst (type);
17650 4 : tree one = build_one_cst (type);
17651 4 : tree index = build_index_vector (type, 0, 1);
17652 :
17653 : /* Verify equality tests that return a scalar boolean result. */
17654 4 : tree res_type = boolean_type_node;
17655 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17656 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17657 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17658 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17659 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17660 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17661 : index, one)));
17662 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17663 : index, index)));
17664 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17665 : index, index)));
17666 4 : }
17667 :
17668 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17669 :
17670 : static void
17671 4 : test_vec_duplicate_folding ()
17672 : {
17673 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17674 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17675 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17676 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17677 :
17678 4 : tree type = build_vector_type (ssizetype, nunits);
17679 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17680 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17681 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17682 4 : }
17683 :
17684 : /* Run all of the selftests within this file. */
17685 :
17686 : void
17687 4 : fold_const_cc_tests ()
17688 : {
17689 4 : test_arithmetic_folding ();
17690 4 : test_vector_folding ();
17691 4 : test_vec_duplicate_folding ();
17692 4 : test_fold_vec_perm_cst::test ();
17693 4 : test_operand_equality::test ();
17694 4 : }
17695 :
17696 : } // namespace selftest
17697 :
17698 : #endif /* CHECKING_P */
|