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 "attribs.h"
84 : #include "tree-vector-builder.h"
85 : #include "vec-perm-indices.h"
86 : #include "asan.h"
87 : #include "gimple-range.h"
88 : #include "optabs-tree.h"
89 :
90 : /* Nonzero if we are folding constants inside an initializer or a C++
91 : manifestly-constant-evaluated context; zero otherwise.
92 : Should be used when folding in initializer enables additional
93 : optimizations. */
94 : int folding_initializer = 0;
95 :
96 : /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
97 : otherwise.
98 : Should be used when certain constructs shouldn't be optimized
99 : during folding in that context. */
100 : bool folding_cxx_constexpr = false;
101 :
102 : namespace {
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 : unsigned char {
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 : // Implements bitwise operators on comparison_code so the
126 : // upper unused bits are cleared.
127 :
128 : // Implements bitwise not on comparison_code
129 : // clearing the upper unused bits.
130 : comparison_code
131 355 : operator ~(comparison_code cmp)
132 : {
133 355 : unsigned char newcmp = cmp;
134 355 : newcmp = ~newcmp & 0xF;
135 355 : return (comparison_code)newcmp;
136 : }
137 :
138 : // Implements bitwise ior on comparison_code.
139 : comparison_code
140 17305 : operator |(comparison_code cmp0, comparison_code cmp1)
141 : {
142 17305 : unsigned char newcmp = ((unsigned char)cmp0) | cmp1;
143 17305 : newcmp = newcmp & 0xF;
144 17305 : return (comparison_code)newcmp;
145 : }
146 :
147 : // Implements bitwise and on comparison_code.
148 : comparison_code
149 60819 : operator &(comparison_code cmp0, comparison_code cmp1)
150 : {
151 60819 : unsigned char newcmp = ((unsigned char)cmp0) & cmp1;
152 60819 : newcmp = newcmp & 0xF;
153 60819 : return (comparison_code)newcmp;
154 : }
155 :
156 : // Implements bitwise xor on comparison_code.
157 : comparison_code
158 1292 : operator ^(comparison_code cmp0, comparison_code cmp1)
159 : {
160 1292 : unsigned char newcmp = ((unsigned char)cmp0) ^ cmp1;
161 1292 : newcmp = newcmp & 0xF;
162 1292 : return (comparison_code)newcmp;
163 : }
164 :
165 : }
166 :
167 : static bool negate_expr_p (tree);
168 : static tree negate_expr (tree);
169 : static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
170 : static enum comparison_code comparison_to_compcode (enum tree_code);
171 : static enum tree_code compcode_to_comparison (enum comparison_code);
172 : static bool twoval_comparison_p (tree, tree *, tree *);
173 : static tree eval_subst (location_t, tree, tree, tree, tree, tree);
174 : static tree optimize_bit_field_compare (location_t, enum tree_code,
175 : tree, tree, tree);
176 : static bool simple_operand_p (const_tree);
177 : static tree range_binop (enum tree_code, tree, tree, int, tree, int);
178 : static tree range_predecessor (tree);
179 : static tree range_successor (tree);
180 : static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
181 : static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
182 : tree, tree, tree, tree);
183 : static tree extract_muldiv (tree, tree, enum tree_code, tree);
184 : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
185 : static tree fold_binary_op_with_conditional_arg (location_t,
186 : enum tree_code, tree,
187 : tree, tree,
188 : tree, tree, int);
189 : static tree fold_negate_const (tree, tree);
190 : static tree fold_not_const (const_tree, tree);
191 : static tree fold_relational_const (enum tree_code, tree, tree, tree);
192 : static tree fold_convert_const (enum tree_code, tree, tree);
193 : static tree fold_view_convert_expr (tree, tree);
194 : static tree fold_negate_expr (location_t, tree);
195 :
196 : /* This is a helper function to detect min/max for some operands of COND_EXPR.
197 : The form is "(exp0 CMP cst1) ? exp0 : cst2". */
198 : tree_code
199 144705 : minmax_from_comparison (tree_code cmp, tree exp0,
200 : const widest_int cst1,
201 : const widest_int cst2)
202 : {
203 144705 : if (cst1 == cst2)
204 : {
205 125 : if (cmp == LE_EXPR || cmp == LT_EXPR)
206 : return MIN_EXPR;
207 106 : if (cmp == GT_EXPR || cmp == GE_EXPR)
208 : return MAX_EXPR;
209 : }
210 144686 : if (cst1 == cst2 - 1)
211 : {
212 : /* X <= Y - 1 equals to X < Y. */
213 87445 : if (cmp == LE_EXPR)
214 : return MIN_EXPR;
215 : /* X > Y - 1 equals to X >= Y. */
216 86990 : if (cmp == GT_EXPR)
217 : return MAX_EXPR;
218 : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
219 74497 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
220 : {
221 20724 : int_range_max r;
222 41448 : get_range_query (cfun)->range_of_expr (r, exp0);
223 20724 : if (r.undefined_p ())
224 0 : r.set_varying (TREE_TYPE (exp0));
225 :
226 20724 : widest_int min = widest_int::from (r.lower_bound (),
227 41448 : TYPE_SIGN (TREE_TYPE (exp0)));
228 20724 : if (min == cst1)
229 499 : return MAX_EXPR;
230 20724 : }
231 : }
232 131239 : if (cst1 == cst2 + 1)
233 : {
234 : /* X < Y + 1 equals to X <= Y. */
235 1291 : if (cmp == LT_EXPR)
236 : return MIN_EXPR;
237 : /* X >= Y + 1 equals to X > Y. */
238 1263 : if (cmp == GE_EXPR)
239 : return MAX_EXPR;
240 : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
241 1100 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
242 : {
243 692 : int_range_max r;
244 1384 : get_range_query (cfun)->range_of_expr (r, exp0);
245 692 : if (r.undefined_p ())
246 0 : r.set_varying (TREE_TYPE (exp0));
247 :
248 692 : widest_int max = widest_int::from (r.upper_bound (),
249 1384 : TYPE_SIGN (TREE_TYPE (exp0)));
250 692 : if (max == cst1)
251 186 : return MIN_EXPR;
252 692 : }
253 : }
254 : return ERROR_MARK;
255 : }
256 :
257 :
258 : /* This is a helper function to detect min/max for some operands of COND_EXPR.
259 : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
260 : tree_code
261 178768 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
262 : {
263 178768 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
264 : return ERROR_MARK;
265 :
266 178757 : if (!operand_equal_p (exp0, exp2))
267 : return ERROR_MARK;
268 :
269 178757 : if (operand_equal_p (exp1, exp3))
270 : {
271 33975 : if (cmp == LT_EXPR || cmp == LE_EXPR)
272 : return MIN_EXPR;
273 31791 : if (cmp == GT_EXPR || cmp == GE_EXPR)
274 : return MAX_EXPR;
275 : }
276 144888 : if (TREE_CODE (exp3) == INTEGER_CST
277 144412 : && TREE_CODE (exp1) == INTEGER_CST)
278 143956 : return minmax_from_comparison (cmp, exp0, wi::to_widest (exp1), wi::to_widest (exp3));
279 : return ERROR_MARK;
280 : }
281 :
282 : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
283 : Otherwise, return LOC. */
284 :
285 : static location_t
286 3060275 : expr_location_or (tree t, location_t loc)
287 : {
288 944375 : location_t tloc = EXPR_LOCATION (t);
289 3043913 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
290 : }
291 :
292 : /* Similar to protected_set_expr_location, but never modify x in place,
293 : if location can and needs to be set, unshare it. */
294 :
295 : tree
296 9483154 : protected_set_expr_location_unshare (tree x, location_t loc)
297 : {
298 9483154 : if (CAN_HAVE_LOCATION_P (x)
299 8426904 : && EXPR_LOCATION (x) != loc
300 2690064 : && !(TREE_CODE (x) == SAVE_EXPR
301 1345247 : || TREE_CODE (x) == TARGET_EXPR
302 : || TREE_CODE (x) == BIND_EXPR))
303 : {
304 1344481 : x = copy_node (x);
305 1344481 : SET_EXPR_LOCATION (x, loc);
306 : }
307 9483154 : return x;
308 : }
309 :
310 : /* Return true if the built-in mathematical function specified by CODE
311 : is odd, i.e. -f(x) == f(-x). */
312 :
313 : bool
314 2203335 : negate_mathfn_p (combined_fn fn)
315 : {
316 2203335 : switch (fn)
317 : {
318 : CASE_CFN_ASIN:
319 : CASE_CFN_ASIN_FN:
320 : CASE_CFN_ASINH:
321 : CASE_CFN_ASINH_FN:
322 : CASE_CFN_ASINPI:
323 : CASE_CFN_ASINPI_FN:
324 : CASE_CFN_ATAN:
325 : CASE_CFN_ATAN_FN:
326 : CASE_CFN_ATANH:
327 : CASE_CFN_ATANH_FN:
328 : CASE_CFN_ATANPI:
329 : CASE_CFN_ATANPI_FN:
330 : CASE_CFN_CASIN:
331 : CASE_CFN_CASIN_FN:
332 : CASE_CFN_CASINH:
333 : CASE_CFN_CASINH_FN:
334 : CASE_CFN_CATAN:
335 : CASE_CFN_CATAN_FN:
336 : CASE_CFN_CATANH:
337 : CASE_CFN_CATANH_FN:
338 : CASE_CFN_CBRT:
339 : CASE_CFN_CBRT_FN:
340 : CASE_CFN_CPROJ:
341 : CASE_CFN_CPROJ_FN:
342 : CASE_CFN_CSIN:
343 : CASE_CFN_CSIN_FN:
344 : CASE_CFN_CSINH:
345 : CASE_CFN_CSINH_FN:
346 : CASE_CFN_CTAN:
347 : CASE_CFN_CTAN_FN:
348 : CASE_CFN_CTANH:
349 : CASE_CFN_CTANH_FN:
350 : CASE_CFN_ERF:
351 : CASE_CFN_ERF_FN:
352 : CASE_CFN_LLROUND:
353 : CASE_CFN_LLROUND_FN:
354 : CASE_CFN_LROUND:
355 : CASE_CFN_LROUND_FN:
356 : CASE_CFN_ROUND:
357 : CASE_CFN_ROUNDEVEN:
358 : CASE_CFN_ROUNDEVEN_FN:
359 : CASE_CFN_SIN:
360 : CASE_CFN_SIN_FN:
361 : CASE_CFN_SINH:
362 : CASE_CFN_SINH_FN:
363 : CASE_CFN_SINPI:
364 : CASE_CFN_SINPI_FN:
365 : CASE_CFN_TAN:
366 : CASE_CFN_TAN_FN:
367 : CASE_CFN_TANH:
368 : CASE_CFN_TANH_FN:
369 : CASE_CFN_TANPI:
370 : CASE_CFN_TANPI_FN:
371 : CASE_CFN_TRUNC:
372 : CASE_CFN_TRUNC_FN:
373 : return true;
374 :
375 414 : CASE_CFN_LLRINT:
376 414 : CASE_CFN_LLRINT_FN:
377 414 : CASE_CFN_LRINT:
378 414 : CASE_CFN_LRINT_FN:
379 414 : CASE_CFN_NEARBYINT:
380 414 : CASE_CFN_NEARBYINT_FN:
381 414 : CASE_CFN_RINT:
382 414 : CASE_CFN_RINT_FN:
383 414 : return !flag_rounding_math;
384 :
385 2199247 : default:
386 2199247 : break;
387 : }
388 2199247 : return false;
389 : }
390 :
391 : /* Check whether we may negate an integer constant T without causing
392 : overflow. */
393 :
394 : bool
395 3136742 : may_negate_without_overflow_p (const_tree t)
396 : {
397 3136742 : tree type;
398 :
399 3136742 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
400 :
401 3136742 : type = TREE_TYPE (t);
402 3136742 : if (TYPE_UNSIGNED (type))
403 : return false;
404 :
405 3136742 : return !wi::only_sign_bit_p (wi::to_wide (t));
406 : }
407 :
408 : /* Determine whether an expression T can be cheaply negated using
409 : the function negate_expr without introducing undefined overflow. */
410 :
411 : static bool
412 28063137 : negate_expr_p (tree t)
413 : {
414 28221706 : tree type;
415 :
416 28221706 : if (t == 0)
417 : return false;
418 :
419 28221706 : type = TREE_TYPE (t);
420 :
421 28221706 : STRIP_SIGN_NOPS (t);
422 28221706 : switch (TREE_CODE (t))
423 : {
424 1622242 : case INTEGER_CST:
425 1622242 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
426 : return true;
427 :
428 : /* Check that -CST will not overflow type. */
429 379499 : return may_negate_without_overflow_p (t);
430 539 : case BIT_NOT_EXPR:
431 539 : return (INTEGRAL_TYPE_P (type)
432 539 : && TYPE_OVERFLOW_WRAPS (type));
433 :
434 : case FIXED_CST:
435 : return true;
436 :
437 1297 : case NEGATE_EXPR:
438 1297 : return !TYPE_OVERFLOW_SANITIZED (type);
439 :
440 1311715 : case REAL_CST:
441 : /* We want to canonicalize to positive real constants. Pretend
442 : that only negative ones can be easily negated. */
443 1311715 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
444 :
445 454 : case COMPLEX_CST:
446 454 : return negate_expr_p (TREE_REALPART (t))
447 572 : && negate_expr_p (TREE_IMAGPART (t));
448 :
449 121 : case VECTOR_CST:
450 121 : {
451 121 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
452 : return true;
453 :
454 : /* Steps don't prevent negation. */
455 121 : unsigned int count = vector_cst_encoded_nelts (t);
456 363 : for (unsigned int i = 0; i < count; ++i)
457 121 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
458 : return false;
459 :
460 : return true;
461 : }
462 :
463 702 : case COMPLEX_EXPR:
464 702 : return negate_expr_p (TREE_OPERAND (t, 0))
465 702 : && negate_expr_p (TREE_OPERAND (t, 1));
466 :
467 33 : case CONJ_EXPR:
468 33 : return negate_expr_p (TREE_OPERAND (t, 0));
469 :
470 1547029 : case PLUS_EXPR:
471 1547029 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
472 1547023 : || HONOR_SIGNED_ZEROS (type)
473 2802373 : || (ANY_INTEGRAL_TYPE_P (type)
474 1255136 : && ! TYPE_OVERFLOW_WRAPS (type)))
475 : return false;
476 : /* -(A + B) -> (-B) - A. */
477 802747 : if (negate_expr_p (TREE_OPERAND (t, 1)))
478 : return true;
479 : /* -(A + B) -> (-A) - B. */
480 148075 : return negate_expr_p (TREE_OPERAND (t, 0));
481 :
482 264404 : case MINUS_EXPR:
483 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
484 264404 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
485 264404 : && !HONOR_SIGNED_ZEROS (type)
486 352631 : && (! ANY_INTEGRAL_TYPE_P (type)
487 88004 : || TYPE_OVERFLOW_WRAPS (type));
488 :
489 2375558 : case MULT_EXPR:
490 2375558 : if (TYPE_UNSIGNED (type))
491 : break;
492 : /* INT_MIN/n * n doesn't overflow while negating one operand it does
493 : if n is a (negative) power of two. */
494 4150540 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
495 152525 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
496 2225492 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
497 0 : && (wi::popcount
498 2075270 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
499 150222 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
500 127018 : && (wi::popcount
501 2202288 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
502 : break;
503 :
504 : /* Fall through. */
505 :
506 2360128 : case RDIV_EXPR:
507 2360128 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
508 2360127 : return negate_expr_p (TREE_OPERAND (t, 1))
509 2360127 : || negate_expr_p (TREE_OPERAND (t, 0));
510 : break;
511 :
512 2525 : case TRUNC_DIV_EXPR:
513 2525 : case ROUND_DIV_EXPR:
514 2525 : case EXACT_DIV_EXPR:
515 2525 : if (TYPE_UNSIGNED (type))
516 : break;
517 : /* In general we can't negate A in A / B, because if A is INT_MIN and
518 : B is not 1 we change the sign of the result. */
519 549 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
520 549 : && negate_expr_p (TREE_OPERAND (t, 0)))
521 : return true;
522 : /* In general we can't negate B in A / B, because if A is INT_MIN and
523 : B is 1, we may turn this into INT_MIN / -1 which is undefined
524 : and actually traps on some architectures. */
525 760 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
526 380 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
527 675 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
528 285 : && ! integer_onep (TREE_OPERAND (t, 1))))
529 370 : return negate_expr_p (TREE_OPERAND (t, 1));
530 : break;
531 :
532 5142290 : case NOP_EXPR:
533 : /* Negate -((double)float) as (double)(-float). */
534 5142290 : if (SCALAR_FLOAT_TYPE_P (type))
535 : {
536 10247 : tree tem = strip_float_extensions (t);
537 10247 : if (tem != t)
538 : return negate_expr_p (tem);
539 : }
540 : break;
541 :
542 1105591 : case CALL_EXPR:
543 : /* Negate -f(x) as f(-x). */
544 1105591 : if (negate_mathfn_p (get_call_combined_fn (t)))
545 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
546 : break;
547 :
548 12884 : case RSHIFT_EXPR:
549 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
550 12884 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
551 : {
552 12739 : tree op1 = TREE_OPERAND (t, 1);
553 12739 : if (wi::to_wide (op1) == element_precision (type) - 1)
554 : return true;
555 : }
556 : break;
557 :
558 : default:
559 : break;
560 : }
561 : return false;
562 : }
563 :
564 : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
565 : simplification is possible.
566 : If negate_expr_p would return true for T, NULL_TREE will never be
567 : returned. */
568 :
569 : static tree
570 40414184 : fold_negate_expr_1 (location_t loc, tree t)
571 : {
572 40414184 : tree type = TREE_TYPE (t);
573 40414184 : tree tem;
574 :
575 40414184 : switch (TREE_CODE (t))
576 : {
577 : /* Convert - (~A) to A + 1. */
578 138 : case BIT_NOT_EXPR:
579 138 : if (INTEGRAL_TYPE_P (type))
580 138 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
581 138 : build_one_cst (type));
582 : break;
583 :
584 31084170 : case INTEGER_CST:
585 31084170 : tem = fold_negate_const (t, type);
586 31084170 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
587 9752 : || (ANY_INTEGRAL_TYPE_P (type)
588 9752 : && !TYPE_OVERFLOW_TRAPS (type)
589 9752 : && TYPE_OVERFLOW_WRAPS (type))
590 31093592 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
591 31084058 : return tem;
592 : break;
593 :
594 2046985 : case POLY_INT_CST:
595 2046985 : case REAL_CST:
596 2046985 : case FIXED_CST:
597 2046985 : tem = fold_negate_const (t, type);
598 2046985 : return tem;
599 :
600 66208 : case COMPLEX_CST:
601 66208 : {
602 66208 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
603 66208 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
604 66208 : if (rpart && ipart)
605 66208 : return build_complex (type, rpart, ipart);
606 : }
607 : break;
608 :
609 53273 : case VECTOR_CST:
610 53273 : {
611 53273 : tree_vector_builder elts;
612 53273 : elts.new_unary_operation (type, t, true);
613 53273 : unsigned int count = elts.encoded_nelts ();
614 129610 : for (unsigned int i = 0; i < count; ++i)
615 : {
616 76337 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
617 76337 : if (elt == NULL_TREE)
618 0 : return NULL_TREE;
619 76337 : elts.quick_push (elt);
620 : }
621 :
622 53273 : return elts.build ();
623 53273 : }
624 :
625 78 : case COMPLEX_EXPR:
626 78 : if (negate_expr_p (t))
627 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
628 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
629 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
630 : break;
631 :
632 21 : case CONJ_EXPR:
633 21 : if (negate_expr_p (t))
634 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
635 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
636 : break;
637 :
638 1236 : case NEGATE_EXPR:
639 1236 : if (!TYPE_OVERFLOW_SANITIZED (type))
640 1210 : return TREE_OPERAND (t, 0);
641 : break;
642 :
643 716737 : case PLUS_EXPR:
644 716737 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
645 716737 : && !HONOR_SIGNED_ZEROS (type))
646 : {
647 : /* -(A + B) -> (-B) - A. */
648 716627 : if (negate_expr_p (TREE_OPERAND (t, 1)))
649 : {
650 658692 : tem = negate_expr (TREE_OPERAND (t, 1));
651 658692 : return fold_build2_loc (loc, MINUS_EXPR, type,
652 1317384 : tem, TREE_OPERAND (t, 0));
653 : }
654 :
655 : /* -(A + B) -> (-A) - B. */
656 57935 : if (negate_expr_p (TREE_OPERAND (t, 0)))
657 : {
658 889 : tem = negate_expr (TREE_OPERAND (t, 0));
659 889 : return fold_build2_loc (loc, MINUS_EXPR, type,
660 1778 : tem, TREE_OPERAND (t, 1));
661 : }
662 : }
663 : break;
664 :
665 160863 : case MINUS_EXPR:
666 : /* - (A - B) -> B - A */
667 160863 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
668 160863 : && !HONOR_SIGNED_ZEROS (type))
669 82698 : return fold_build2_loc (loc, MINUS_EXPR, type,
670 165396 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
671 : break;
672 :
673 267675 : case MULT_EXPR:
674 267675 : if (TYPE_UNSIGNED (type))
675 : break;
676 :
677 : /* Fall through. */
678 :
679 32437 : case RDIV_EXPR:
680 32437 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
681 : {
682 32437 : tem = TREE_OPERAND (t, 1);
683 32437 : if (negate_expr_p (tem))
684 58590 : return fold_build2_loc (loc, TREE_CODE (t), type,
685 58590 : TREE_OPERAND (t, 0), negate_expr (tem));
686 3142 : tem = TREE_OPERAND (t, 0);
687 3142 : if (negate_expr_p (tem))
688 57 : return fold_build2_loc (loc, TREE_CODE (t), type,
689 114 : negate_expr (tem), TREE_OPERAND (t, 1));
690 : }
691 : break;
692 :
693 2036 : case TRUNC_DIV_EXPR:
694 2036 : case ROUND_DIV_EXPR:
695 2036 : case EXACT_DIV_EXPR:
696 2036 : if (TYPE_UNSIGNED (type))
697 : break;
698 : /* In general we can't negate A in A / B, because if A is INT_MIN and
699 : B is not 1 we change the sign of the result. */
700 727 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
701 727 : && negate_expr_p (TREE_OPERAND (t, 0)))
702 327 : return fold_build2_loc (loc, TREE_CODE (t), type,
703 327 : negate_expr (TREE_OPERAND (t, 0)),
704 654 : TREE_OPERAND (t, 1));
705 : /* In general we can't negate B in A / B, because if A is INT_MIN and
706 : B is 1, we may turn this into INT_MIN / -1 which is undefined
707 : and actually traps on some architectures. */
708 800 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
709 400 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
710 316 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
711 293 : && ! integer_onep (TREE_OPERAND (t, 1))))
712 777 : && negate_expr_p (TREE_OPERAND (t, 1)))
713 742 : return fold_build2_loc (loc, TREE_CODE (t), type,
714 371 : TREE_OPERAND (t, 0),
715 742 : negate_expr (TREE_OPERAND (t, 1)));
716 : break;
717 :
718 2421130 : case NOP_EXPR:
719 : /* Convert -((double)float) into (double)(-float). */
720 2421130 : if (SCALAR_FLOAT_TYPE_P (type))
721 : {
722 10994 : tem = strip_float_extensions (t);
723 10994 : if (tem != t && negate_expr_p (tem))
724 0 : return fold_convert_loc (loc, type, negate_expr (tem));
725 : }
726 : break;
727 :
728 299411 : case CALL_EXPR:
729 : /* Negate -f(x) as f(-x). */
730 299411 : if (negate_mathfn_p (get_call_combined_fn (t))
731 300700 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
732 : {
733 1191 : tree fndecl, arg;
734 :
735 1191 : fndecl = get_callee_fndecl (t);
736 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
737 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
738 : }
739 : break;
740 :
741 12628 : case RSHIFT_EXPR:
742 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
743 12628 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
744 : {
745 12610 : tree op1 = TREE_OPERAND (t, 1);
746 12610 : if (wi::to_wide (op1) == element_precision (type) - 1)
747 : {
748 12282 : tree ntype = TYPE_UNSIGNED (type)
749 12282 : ? signed_type_for (type)
750 72 : : unsigned_type_for (type);
751 12282 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
752 12282 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
753 12282 : return fold_convert_loc (loc, type, temp);
754 : }
755 : }
756 : break;
757 :
758 : default:
759 : break;
760 : }
761 :
762 : return NULL_TREE;
763 : }
764 :
765 : /* A wrapper for fold_negate_expr_1. */
766 :
767 : static tree
768 40414184 : fold_negate_expr (location_t loc, tree t)
769 : {
770 40414184 : tree type = TREE_TYPE (t);
771 40414184 : STRIP_SIGN_NOPS (t);
772 40414184 : tree tem = fold_negate_expr_1 (loc, t);
773 40414184 : if (tem == NULL_TREE)
774 : return NULL_TREE;
775 34037715 : return fold_convert_loc (loc, type, tem);
776 : }
777 :
778 : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
779 : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
780 : return NULL_TREE. */
781 :
782 : static tree
783 3925682 : negate_expr (tree t)
784 : {
785 3925682 : tree type, tem;
786 3925682 : location_t loc;
787 :
788 3925682 : if (t == NULL_TREE)
789 : return NULL_TREE;
790 :
791 3925682 : loc = EXPR_LOCATION (t);
792 3925682 : type = TREE_TYPE (t);
793 3925682 : STRIP_SIGN_NOPS (t);
794 :
795 3925682 : tem = fold_negate_expr (loc, t);
796 3925682 : if (!tem)
797 1964560 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
798 3925682 : return fold_convert_loc (loc, type, tem);
799 : }
800 :
801 : /* Split a tree IN into a constant, literal and variable parts that could be
802 : combined with CODE to make IN. "constant" means an expression with
803 : TREE_CONSTANT but that isn't an actual constant. CODE must be a
804 : commutative arithmetic operation. Store the constant part into *CONP,
805 : the literal in *LITP and return the variable part. If a part isn't
806 : present, set it to null. If the tree does not decompose in this way,
807 : return the entire tree as the variable part and the other parts as null.
808 :
809 : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
810 : case, we negate an operand that was subtracted. Except if it is a
811 : literal for which we use *MINUS_LITP instead.
812 :
813 : If NEGATE_P is true, we are negating all of IN, again except a literal
814 : for which we use *MINUS_LITP instead. If a variable part is of pointer
815 : type, it is negated after converting to TYPE. This prevents us from
816 : generating illegal MINUS pointer expression. LOC is the location of
817 : the converted variable part.
818 :
819 : If IN is itself a literal or constant, return it as appropriate.
820 :
821 : Note that we do not guarantee that any of the three values will be the
822 : same type as IN, but they will have the same signedness and mode. */
823 :
824 : static tree
825 234623894 : split_tree (tree in, tree type, enum tree_code code,
826 : tree *minus_varp, tree *conp, tree *minus_conp,
827 : tree *litp, tree *minus_litp, int negate_p)
828 : {
829 234623894 : tree var = 0;
830 234623894 : *minus_varp = 0;
831 234623894 : *conp = 0;
832 234623894 : *minus_conp = 0;
833 234623894 : *litp = 0;
834 234623894 : *minus_litp = 0;
835 :
836 : /* Strip any conversions that don't change the machine mode or signedness. */
837 234623894 : STRIP_SIGN_NOPS (in);
838 :
839 234623894 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
840 150070982 : || TREE_CODE (in) == FIXED_CST)
841 84552912 : *litp = in;
842 150070982 : else if (TREE_CODE (in) == code
843 150070982 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
844 145410372 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
845 : /* We can associate addition and subtraction together (even
846 : though the C standard doesn't say so) for integers because
847 : the value is not affected. For reals, the value might be
848 : affected, so we can't. */
849 145410372 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
850 60837381 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
851 143700417 : || (code == MINUS_EXPR
852 23521191 : && (TREE_CODE (in) == PLUS_EXPR
853 21669317 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
854 : {
855 8694414 : tree op0 = TREE_OPERAND (in, 0);
856 8694414 : tree op1 = TREE_OPERAND (in, 1);
857 8694414 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
858 8694414 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
859 :
860 : /* First see if either of the operands is a literal, then a constant. */
861 8694414 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
862 8471510 : || TREE_CODE (op0) == FIXED_CST)
863 222904 : *litp = op0, op0 = 0;
864 8471510 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
865 5667041 : || TREE_CODE (op1) == FIXED_CST)
866 2804469 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
867 :
868 8694414 : if (op0 != 0 && TREE_CONSTANT (op0))
869 13636 : *conp = op0, op0 = 0;
870 8680778 : else if (op1 != 0 && TREE_CONSTANT (op1))
871 50691 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
872 :
873 : /* If we haven't dealt with either operand, this is not a case we can
874 : decompose. Otherwise, VAR is either of the ones remaining, if any. */
875 8694414 : if (op0 != 0 && op1 != 0)
876 : var = in;
877 3085420 : else if (op0 != 0)
878 : var = op0;
879 : else
880 236540 : var = op1, neg_var_p = neg1_p;
881 :
882 : /* Now do any needed negations. */
883 8694414 : if (neg_litp_p)
884 28945 : *minus_litp = *litp, *litp = 0;
885 8694414 : if (neg_conp_p && *conp)
886 11322 : *minus_conp = *conp, *conp = 0;
887 8694414 : if (neg_var_p && var)
888 226988 : *minus_varp = var, var = 0;
889 : }
890 141376568 : else if (TREE_CONSTANT (in))
891 819335 : *conp = in;
892 140557233 : else if (TREE_CODE (in) == BIT_NOT_EXPR
893 548309 : && code == PLUS_EXPR)
894 : {
895 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
896 : when IN is constant. */
897 386342 : *litp = build_minus_one_cst (type);
898 386342 : *minus_varp = TREE_OPERAND (in, 0);
899 : }
900 : else
901 : var = in;
902 :
903 234623894 : if (negate_p)
904 : {
905 12974681 : if (*litp)
906 1285064 : *minus_litp = *litp, *litp = 0;
907 11689617 : else if (*minus_litp)
908 174 : *litp = *minus_litp, *minus_litp = 0;
909 12974681 : if (*conp)
910 46100 : *minus_conp = *conp, *conp = 0;
911 12928581 : else if (*minus_conp)
912 0 : *conp = *minus_conp, *minus_conp = 0;
913 12974681 : if (var)
914 12916079 : *minus_varp = var, var = 0;
915 58602 : else if (*minus_varp)
916 883 : var = *minus_varp, *minus_varp = 0;
917 : }
918 :
919 234623894 : if (*litp
920 234623894 : && TREE_OVERFLOW_P (*litp))
921 20479 : *litp = drop_tree_overflow (*litp);
922 234623894 : if (*minus_litp
923 234623894 : && TREE_OVERFLOW_P (*minus_litp))
924 24 : *minus_litp = drop_tree_overflow (*minus_litp);
925 :
926 234623894 : return var;
927 : }
928 :
929 : /* Re-associate trees split by the above function. T1 and T2 are
930 : either expressions to associate or null. Return the new
931 : expression, if any. LOC is the location of the new expression. If
932 : we build an operation, do it in TYPE and with CODE. */
933 :
934 : static tree
935 21420727 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
936 : {
937 21420727 : if (t1 == 0)
938 : {
939 13608621 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
940 : return t2;
941 : }
942 7812106 : else if (t2 == 0)
943 : return t1;
944 :
945 : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
946 : try to fold this since we will have infinite recursion. But do
947 : deal with any NEGATE_EXPRs. */
948 4358016 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
949 3419505 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
950 3352920 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
951 : {
952 1702529 : if (code == PLUS_EXPR)
953 : {
954 939183 : if (TREE_CODE (t1) == NEGATE_EXPR)
955 59 : return build2_loc (loc, MINUS_EXPR, type,
956 : fold_convert_loc (loc, type, t2),
957 : fold_convert_loc (loc, type,
958 118 : TREE_OPERAND (t1, 0)));
959 939124 : else if (TREE_CODE (t2) == NEGATE_EXPR)
960 1 : return build2_loc (loc, MINUS_EXPR, type,
961 : fold_convert_loc (loc, type, t1),
962 : fold_convert_loc (loc, type,
963 2 : TREE_OPERAND (t2, 0)));
964 939123 : else if (integer_zerop (t2))
965 37585 : return fold_convert_loc (loc, type, t1);
966 : }
967 763346 : else if (code == MINUS_EXPR)
968 : {
969 739784 : if (integer_zerop (t2))
970 0 : return fold_convert_loc (loc, type, t1);
971 : }
972 :
973 1664884 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
974 1664884 : fold_convert_loc (loc, type, t2));
975 : }
976 :
977 2655487 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
978 2655487 : fold_convert_loc (loc, type, t2));
979 : }
980 :
981 : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
982 : for use in int_const_binop, size_binop and size_diffop. */
983 :
984 : static bool
985 2784648687 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
986 : {
987 2784648687 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
988 : return false;
989 2784648687 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
990 : return false;
991 :
992 2784648687 : switch (code)
993 : {
994 : case LSHIFT_EXPR:
995 : case RSHIFT_EXPR:
996 : case LROTATE_EXPR:
997 : case RROTATE_EXPR:
998 : return true;
999 :
1000 2784648687 : default:
1001 2784648687 : break;
1002 : }
1003 :
1004 2784648687 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1005 2784648687 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1006 5569297374 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1007 : }
1008 :
1009 : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1010 : a new constant in RES. Return FALSE if we don't know how to
1011 : evaluate CODE at compile-time. */
1012 :
1013 : bool
1014 1547558271 : wide_int_binop (wide_int &res,
1015 : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1016 : signop sign, wi::overflow_type *overflow)
1017 : {
1018 1547558271 : wide_int tmp;
1019 1547558271 : *overflow = wi::OVF_NONE;
1020 1547558271 : switch (code)
1021 : {
1022 3724986 : case BIT_IOR_EXPR:
1023 3724986 : res = wi::bit_or (arg1, arg2);
1024 3724986 : break;
1025 :
1026 108557 : case BIT_XOR_EXPR:
1027 108557 : res = wi::bit_xor (arg1, arg2);
1028 108557 : break;
1029 :
1030 24105266 : case BIT_AND_EXPR:
1031 24105266 : res = wi::bit_and (arg1, arg2);
1032 24105266 : break;
1033 :
1034 15134033 : case LSHIFT_EXPR:
1035 15134033 : if (wi::neg_p (arg2))
1036 : return false;
1037 15103667 : res = wi::lshift (arg1, arg2);
1038 15103667 : break;
1039 :
1040 7582765 : case RSHIFT_EXPR:
1041 7582765 : if (wi::neg_p (arg2))
1042 : return false;
1043 : /* It's unclear from the C standard whether shifts can overflow.
1044 : The following code ignores overflow; perhaps a C standard
1045 : interpretation ruling is needed. */
1046 7582569 : res = wi::rshift (arg1, arg2, sign);
1047 7582569 : break;
1048 :
1049 1720 : case RROTATE_EXPR:
1050 1720 : case LROTATE_EXPR:
1051 1720 : if (wi::neg_p (arg2))
1052 : {
1053 14 : tmp = -arg2;
1054 14 : if (code == RROTATE_EXPR)
1055 : code = LROTATE_EXPR;
1056 : else
1057 : code = RROTATE_EXPR;
1058 : }
1059 : else
1060 1706 : tmp = arg2;
1061 :
1062 1706 : if (code == RROTATE_EXPR)
1063 1533 : res = wi::rrotate (arg1, tmp);
1064 : else
1065 187 : res = wi::lrotate (arg1, tmp);
1066 : break;
1067 :
1068 261914667 : case PLUS_EXPR:
1069 261914667 : res = wi::add (arg1, arg2, sign, overflow);
1070 261914667 : break;
1071 :
1072 75871561 : case MINUS_EXPR:
1073 75871561 : res = wi::sub (arg1, arg2, sign, overflow);
1074 75871561 : break;
1075 :
1076 444097941 : case MULT_EXPR:
1077 444097941 : res = wi::mul (arg1, arg2, sign, overflow);
1078 444097941 : break;
1079 :
1080 6978 : case MULT_HIGHPART_EXPR:
1081 6978 : res = wi::mul_high (arg1, arg2, sign);
1082 6978 : break;
1083 :
1084 366286416 : case TRUNC_DIV_EXPR:
1085 366286416 : case EXACT_DIV_EXPR:
1086 366286416 : if (arg2 == 0)
1087 : return false;
1088 366280472 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1089 366280472 : break;
1090 :
1091 82404531 : case FLOOR_DIV_EXPR:
1092 82404531 : if (arg2 == 0)
1093 : return false;
1094 82404531 : res = wi::div_floor (arg1, arg2, sign, overflow);
1095 82404531 : break;
1096 :
1097 88383299 : case CEIL_DIV_EXPR:
1098 88383299 : if (arg2 == 0)
1099 : return false;
1100 88383299 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1101 88383299 : break;
1102 :
1103 0 : case ROUND_DIV_EXPR:
1104 0 : if (arg2 == 0)
1105 : return false;
1106 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1107 0 : break;
1108 :
1109 1384965 : case TRUNC_MOD_EXPR:
1110 1384965 : if (arg2 == 0)
1111 : return false;
1112 1383860 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1113 1383860 : break;
1114 :
1115 70697137 : case FLOOR_MOD_EXPR:
1116 70697137 : if (arg2 == 0)
1117 : return false;
1118 70697137 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1119 70697137 : break;
1120 :
1121 178 : case CEIL_MOD_EXPR:
1122 178 : if (arg2 == 0)
1123 : return false;
1124 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1125 178 : break;
1126 :
1127 0 : case ROUND_MOD_EXPR:
1128 0 : if (arg2 == 0)
1129 : return false;
1130 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1131 0 : break;
1132 :
1133 49601 : case MIN_EXPR:
1134 49601 : res = wi::min (arg1, arg2, sign);
1135 49601 : break;
1136 :
1137 105803543 : case MAX_EXPR:
1138 105803543 : res = wi::max (arg1, arg2, sign);
1139 105803543 : break;
1140 :
1141 : default:
1142 : return false;
1143 : }
1144 : return true;
1145 1547558271 : }
1146 :
1147 : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1148 : min value to RES. */
1149 : bool
1150 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1151 : {
1152 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1153 : {
1154 0 : res = wi::to_poly_wide (arg1);
1155 0 : return true;
1156 : }
1157 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1158 : {
1159 0 : res = wi::to_poly_wide (arg2);
1160 0 : return true;
1161 : }
1162 :
1163 : return false;
1164 : }
1165 :
1166 : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1167 : produce a new constant in RES. Return FALSE if we don't know how
1168 : to evaluate CODE at compile-time. */
1169 :
1170 : bool
1171 1547558271 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1172 : const_tree arg1, const_tree arg2,
1173 : signop sign, wi::overflow_type *overflow)
1174 : {
1175 1547558271 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1176 :
1177 1547558271 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1178 : {
1179 1547558271 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1180 1547558271 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1181 1547558271 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1182 : return NULL_TREE;
1183 1547520533 : res = wi_res;
1184 1547520533 : return true;
1185 1547558525 : }
1186 :
1187 : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1188 :
1189 : switch (code)
1190 : {
1191 : case PLUS_EXPR:
1192 : res = wi::add (wi::to_poly_wide (arg1),
1193 : wi::to_poly_wide (arg2), sign, overflow);
1194 : break;
1195 :
1196 : case MINUS_EXPR:
1197 : res = wi::sub (wi::to_poly_wide (arg1),
1198 : wi::to_poly_wide (arg2), sign, overflow);
1199 : break;
1200 :
1201 : case MULT_EXPR:
1202 : if (TREE_CODE (arg2) == INTEGER_CST)
1203 : res = wi::mul (wi::to_poly_wide (arg1),
1204 : wi::to_wide (arg2), sign, overflow);
1205 : else if (TREE_CODE (arg1) == INTEGER_CST)
1206 : res = wi::mul (wi::to_poly_wide (arg2),
1207 : wi::to_wide (arg1), sign, overflow);
1208 : else
1209 : return NULL_TREE;
1210 : break;
1211 :
1212 : case LSHIFT_EXPR:
1213 : if (TREE_CODE (arg2) == INTEGER_CST)
1214 : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1215 : else
1216 : return false;
1217 : break;
1218 :
1219 : case BIT_AND_EXPR:
1220 : if (TREE_CODE (arg2) != INTEGER_CST
1221 : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1222 : &res))
1223 : return false;
1224 : break;
1225 :
1226 : case BIT_IOR_EXPR:
1227 : if (TREE_CODE (arg2) != INTEGER_CST
1228 : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1229 : &res))
1230 : return false;
1231 : break;
1232 :
1233 : case MIN_EXPR:
1234 : if (!can_min_p (arg1, arg2, res))
1235 : return false;
1236 : break;
1237 :
1238 : default:
1239 : return false;
1240 : }
1241 : return true;
1242 : }
1243 :
1244 : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1245 : produce a new constant. Return NULL_TREE if we don't know how to
1246 : evaluate CODE at compile-time. */
1247 :
1248 : tree
1249 1547558271 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1250 : int overflowable)
1251 : {
1252 1547558271 : poly_wide_int poly_res;
1253 1547558271 : tree type = TREE_TYPE (arg1);
1254 1547558271 : signop sign = TYPE_SIGN (type);
1255 1547558271 : wi::overflow_type overflow = wi::OVF_NONE;
1256 :
1257 1547558271 : if (!poly_int_tree_p (arg1)
1258 1547558271 : || !poly_int_tree_p (arg2)
1259 3095116542 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1260 : return NULL_TREE;
1261 1547520533 : return force_fit_type (type, poly_res, overflowable,
1262 1547520533 : (((sign == SIGNED || overflowable == -1)
1263 1547520533 : && overflow)
1264 1547520533 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1265 1547558271 : }
1266 :
1267 : /* Return true if binary operation OP distributes over addition in operand
1268 : OPNO, with the other operand being held constant. OPNO counts from 1. */
1269 :
1270 : static bool
1271 199299 : distributes_over_addition_p (tree_code op, int opno)
1272 : {
1273 0 : switch (op)
1274 : {
1275 : case PLUS_EXPR:
1276 : case MINUS_EXPR:
1277 : case MULT_EXPR:
1278 : return true;
1279 :
1280 0 : case LSHIFT_EXPR:
1281 0 : return opno == 1;
1282 :
1283 4778 : default:
1284 4778 : return false;
1285 : }
1286 : }
1287 :
1288 : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1289 : is the other operand. Try to use the value of OP to simplify the
1290 : operation in one step, without having to process individual elements. */
1291 : static tree
1292 464034 : simplify_const_binop (tree_code code, tree op, tree other_op,
1293 : int index ATTRIBUTE_UNUSED)
1294 : {
1295 : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1296 464034 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1297 : {
1298 378318 : if (integer_zerop (other_op))
1299 : {
1300 28658 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1301 : return op;
1302 27500 : else if (code == BIT_AND_EXPR)
1303 4241 : return other_op;
1304 : }
1305 : }
1306 :
1307 : return NULL_TREE;
1308 : }
1309 :
1310 : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1311 : be an elementwise vector operation, try to fold the operation to a
1312 : constant vector, using ELT_CONST_BINOP to fold each element. Return
1313 : the folded value on success, otherwise return null. */
1314 : tree
1315 278442 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1316 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1317 : {
1318 202025 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1319 471189 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1320 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1321 : {
1322 192747 : tree type = TREE_TYPE (arg1);
1323 192747 : bool step_ok_p;
1324 192747 : if (VECTOR_CST_STEPPED_P (arg1)
1325 192747 : && VECTOR_CST_STEPPED_P (arg2))
1326 : /* We can operate directly on the encoding if:
1327 :
1328 : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1329 : implies
1330 : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1331 :
1332 : Addition and subtraction are the supported operators
1333 : for which this is true. */
1334 2726 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1335 190021 : else if (VECTOR_CST_STEPPED_P (arg1))
1336 : /* We can operate directly on stepped encodings if:
1337 :
1338 : a3 - a2 == a2 - a1
1339 : implies:
1340 : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1341 :
1342 : which is true if (x -> x op c) distributes over addition. */
1343 52007 : step_ok_p = distributes_over_addition_p (code, 1);
1344 : else
1345 : /* Similarly in reverse. */
1346 138014 : step_ok_p = distributes_over_addition_p (code, 2);
1347 192747 : tree_vector_builder elts;
1348 192747 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1349 : return NULL_TREE;
1350 192747 : unsigned int count = elts.encoded_nelts ();
1351 740638 : for (unsigned int i = 0; i < count; ++i)
1352 : {
1353 548210 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1354 548210 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1355 :
1356 548210 : tree elt = elt_const_binop (code, elem1, elem2);
1357 :
1358 : /* It is possible that const_binop cannot handle the given
1359 : code and return NULL_TREE */
1360 548210 : if (elt == NULL_TREE)
1361 319 : return NULL_TREE;
1362 547891 : elts.quick_push (elt);
1363 : }
1364 :
1365 192428 : return elts.build ();
1366 192747 : }
1367 :
1368 85695 : if (TREE_CODE (arg1) == VECTOR_CST
1369 9278 : && TREE_CODE (arg2) == INTEGER_CST)
1370 : {
1371 9278 : tree type = TREE_TYPE (arg1);
1372 9278 : bool step_ok_p = distributes_over_addition_p (code, 1);
1373 9278 : tree_vector_builder elts;
1374 9278 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1375 : return NULL_TREE;
1376 9278 : unsigned int count = elts.encoded_nelts ();
1377 38464 : for (unsigned int i = 0; i < count; ++i)
1378 : {
1379 29273 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1380 :
1381 29273 : tree elt = elt_const_binop (code, elem1, arg2);
1382 :
1383 : /* It is possible that const_binop cannot handle the given
1384 : code and return NULL_TREE. */
1385 29273 : if (elt == NULL_TREE)
1386 87 : return NULL_TREE;
1387 29186 : elts.quick_push (elt);
1388 : }
1389 :
1390 9191 : return elts.build ();
1391 9278 : }
1392 : return NULL_TREE;
1393 : }
1394 :
1395 : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1396 : constant. We assume ARG1 and ARG2 have the same data type, or at least
1397 : are the same kind of constant and the same machine mode. Return zero if
1398 : combining the constants is not allowed in the current operating mode. */
1399 :
1400 : static tree
1401 225353854 : const_binop (enum tree_code code, tree arg1, tree arg2)
1402 : {
1403 : /* Sanity check for the recursive cases. */
1404 225353854 : if (!arg1 || !arg2)
1405 : return NULL_TREE;
1406 :
1407 225352590 : STRIP_NOPS (arg1);
1408 225352590 : STRIP_NOPS (arg2);
1409 :
1410 225352590 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1411 : {
1412 219488406 : if (code == POINTER_PLUS_EXPR)
1413 106714 : return int_const_binop (PLUS_EXPR,
1414 213428 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1415 :
1416 219381692 : return int_const_binop (code, arg1, arg2);
1417 : }
1418 :
1419 5864184 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1420 : {
1421 5569253 : machine_mode mode;
1422 5569253 : REAL_VALUE_TYPE d1;
1423 5569253 : REAL_VALUE_TYPE d2;
1424 5569253 : REAL_VALUE_TYPE value;
1425 5569253 : REAL_VALUE_TYPE result;
1426 5569253 : bool inexact;
1427 5569253 : tree t, type;
1428 :
1429 : /* The following codes are handled by real_arithmetic. */
1430 5569253 : switch (code)
1431 : {
1432 5569253 : case PLUS_EXPR:
1433 5569253 : case MINUS_EXPR:
1434 5569253 : case MULT_EXPR:
1435 5569253 : case RDIV_EXPR:
1436 5569253 : case MIN_EXPR:
1437 5569253 : case MAX_EXPR:
1438 5569253 : break;
1439 :
1440 : default:
1441 : return NULL_TREE;
1442 : }
1443 :
1444 5569253 : d1 = TREE_REAL_CST (arg1);
1445 5569253 : d2 = TREE_REAL_CST (arg2);
1446 :
1447 5569253 : type = TREE_TYPE (arg1);
1448 5569253 : mode = TYPE_MODE (type);
1449 :
1450 : /* Don't perform operation if we honor signaling NaNs and
1451 : either operand is a signaling NaN. */
1452 5569253 : if (HONOR_SNANS (mode)
1453 5569253 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1454 6967 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1455 : return NULL_TREE;
1456 :
1457 : /* Don't perform operation if it would raise a division
1458 : by zero exception. */
1459 5569220 : if (code == RDIV_EXPR
1460 2431484 : && real_equal (&d2, &dconst0)
1461 5579893 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1462 : return NULL_TREE;
1463 :
1464 : /* If either operand is a NaN, just return it. Otherwise, set up
1465 : for floating-point trap; we return an overflow. */
1466 5561459 : if (REAL_VALUE_ISNAN (d1))
1467 : {
1468 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1469 : is off. */
1470 346 : d1.signalling = 0;
1471 346 : t = build_real (type, d1);
1472 346 : return t;
1473 : }
1474 5561113 : else if (REAL_VALUE_ISNAN (d2))
1475 : {
1476 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1477 : is off. */
1478 60 : d2.signalling = 0;
1479 60 : t = build_real (type, d2);
1480 60 : return t;
1481 : }
1482 :
1483 5561053 : inexact = real_arithmetic (&value, code, &d1, &d2);
1484 5561053 : real_convert (&result, mode, &value);
1485 :
1486 : /* Don't constant fold this floating point operation if
1487 : both operands are not NaN but the result is NaN, and
1488 : flag_trapping_math. Such operations should raise an
1489 : invalid operation exception. */
1490 5561053 : if (flag_trapping_math
1491 21597914 : && MODE_HAS_NANS (mode)
1492 5541027 : && REAL_VALUE_ISNAN (result)
1493 2583 : && !REAL_VALUE_ISNAN (d1)
1494 5563636 : && !REAL_VALUE_ISNAN (d2))
1495 2583 : return NULL_TREE;
1496 :
1497 : /* Don't constant fold this floating point operation if
1498 : the result has overflowed and flag_trapping_math. */
1499 5558470 : if (flag_trapping_math
1500 21587924 : && MODE_HAS_INFINITIES (mode)
1501 5538444 : && REAL_VALUE_ISINF (result)
1502 7846 : && !REAL_VALUE_ISINF (d1)
1503 5565648 : && !REAL_VALUE_ISINF (d2))
1504 4895 : return NULL_TREE;
1505 :
1506 : /* Don't constant fold this floating point operation if the
1507 : result may dependent upon the run-time rounding mode and
1508 : flag_rounding_math is set, or if GCC's software emulation
1509 : is unable to accurately represent the result. */
1510 5553575 : if ((flag_rounding_math
1511 37730295 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1512 5553575 : && (inexact || !real_identical (&result, &value)))
1513 : return NULL_TREE;
1514 :
1515 5552468 : t = build_real (type, result);
1516 :
1517 5552468 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1518 5552468 : return t;
1519 : }
1520 :
1521 294931 : if (TREE_CODE (arg1) == FIXED_CST)
1522 : {
1523 0 : FIXED_VALUE_TYPE f1;
1524 0 : FIXED_VALUE_TYPE f2;
1525 0 : FIXED_VALUE_TYPE result;
1526 0 : tree t, type;
1527 0 : bool sat_p;
1528 0 : bool overflow_p;
1529 :
1530 : /* The following codes are handled by fixed_arithmetic. */
1531 0 : switch (code)
1532 : {
1533 0 : case PLUS_EXPR:
1534 0 : case MINUS_EXPR:
1535 0 : case MULT_EXPR:
1536 0 : case TRUNC_DIV_EXPR:
1537 0 : if (TREE_CODE (arg2) != FIXED_CST)
1538 : return NULL_TREE;
1539 0 : f2 = TREE_FIXED_CST (arg2);
1540 0 : break;
1541 :
1542 0 : case LSHIFT_EXPR:
1543 0 : case RSHIFT_EXPR:
1544 0 : {
1545 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1546 0 : return NULL_TREE;
1547 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1548 0 : f2.data.high = w2.elt (1);
1549 0 : f2.data.low = w2.ulow ();
1550 0 : f2.mode = SImode;
1551 : }
1552 0 : break;
1553 :
1554 : default:
1555 : return NULL_TREE;
1556 : }
1557 :
1558 0 : f1 = TREE_FIXED_CST (arg1);
1559 0 : type = TREE_TYPE (arg1);
1560 0 : sat_p = TYPE_SATURATING (type);
1561 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1562 0 : t = build_fixed (type, result);
1563 : /* Propagate overflow flags. */
1564 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1565 0 : TREE_OVERFLOW (t) = 1;
1566 : return t;
1567 : }
1568 :
1569 294931 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1570 : {
1571 11248 : tree type = TREE_TYPE (arg1);
1572 11248 : tree r1 = TREE_REALPART (arg1);
1573 11248 : tree i1 = TREE_IMAGPART (arg1);
1574 11248 : tree r2 = TREE_REALPART (arg2);
1575 11248 : tree i2 = TREE_IMAGPART (arg2);
1576 11248 : tree real, imag;
1577 :
1578 11248 : switch (code)
1579 : {
1580 5319 : case PLUS_EXPR:
1581 5319 : case MINUS_EXPR:
1582 5319 : real = const_binop (code, r1, r2);
1583 5319 : imag = const_binop (code, i1, i2);
1584 5319 : break;
1585 :
1586 3971 : case MULT_EXPR:
1587 3971 : if (COMPLEX_FLOAT_TYPE_P (type))
1588 2819 : return do_mpc_arg2 (arg1, arg2, type,
1589 : /* do_nonfinite= */ folding_initializer,
1590 2819 : mpc_mul);
1591 :
1592 1152 : real = const_binop (MINUS_EXPR,
1593 : const_binop (MULT_EXPR, r1, r2),
1594 : const_binop (MULT_EXPR, i1, i2));
1595 1152 : imag = const_binop (PLUS_EXPR,
1596 : const_binop (MULT_EXPR, r1, i2),
1597 : const_binop (MULT_EXPR, i1, r2));
1598 1152 : break;
1599 :
1600 1704 : case RDIV_EXPR:
1601 1704 : if (COMPLEX_FLOAT_TYPE_P (type))
1602 1704 : return do_mpc_arg2 (arg1, arg2, type,
1603 : /* do_nonfinite= */ folding_initializer,
1604 1704 : mpc_div);
1605 : /* Fallthru. */
1606 254 : case TRUNC_DIV_EXPR:
1607 254 : case CEIL_DIV_EXPR:
1608 254 : case FLOOR_DIV_EXPR:
1609 254 : case ROUND_DIV_EXPR:
1610 254 : if (flag_complex_method == 0)
1611 : {
1612 : /* Keep this algorithm in sync with
1613 : tree-complex.cc:expand_complex_div_straight().
1614 :
1615 : Expand complex division to scalars, straightforward algorithm.
1616 : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1617 : t = br*br + bi*bi
1618 : */
1619 0 : tree magsquared
1620 0 : = const_binop (PLUS_EXPR,
1621 : const_binop (MULT_EXPR, r2, r2),
1622 : const_binop (MULT_EXPR, i2, i2));
1623 0 : tree t1
1624 0 : = const_binop (PLUS_EXPR,
1625 : const_binop (MULT_EXPR, r1, r2),
1626 : const_binop (MULT_EXPR, i1, i2));
1627 0 : tree t2
1628 0 : = const_binop (MINUS_EXPR,
1629 : const_binop (MULT_EXPR, i1, r2),
1630 : const_binop (MULT_EXPR, r1, i2));
1631 :
1632 0 : real = const_binop (code, t1, magsquared);
1633 0 : imag = const_binop (code, t2, magsquared);
1634 : }
1635 : else
1636 : {
1637 : /* Keep this algorithm in sync with
1638 : tree-complex.cc:expand_complex_div_wide().
1639 :
1640 : Expand complex division to scalars, modified algorithm to minimize
1641 : overflow with wide input ranges. */
1642 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1643 : fold_abs_const (r2, TREE_TYPE (type)),
1644 : fold_abs_const (i2, TREE_TYPE (type)));
1645 :
1646 254 : if (integer_nonzerop (compare))
1647 : {
1648 : /* In the TRUE branch, we compute
1649 : ratio = br/bi;
1650 : div = (br * ratio) + bi;
1651 : tr = (ar * ratio) + ai;
1652 : ti = (ai * ratio) - ar;
1653 : tr = tr / div;
1654 : ti = ti / div; */
1655 48 : tree ratio = const_binop (code, r2, i2);
1656 48 : tree div = const_binop (PLUS_EXPR, i2,
1657 : const_binop (MULT_EXPR, r2, ratio));
1658 48 : real = const_binop (MULT_EXPR, r1, ratio);
1659 48 : real = const_binop (PLUS_EXPR, real, i1);
1660 48 : real = const_binop (code, real, div);
1661 :
1662 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1663 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1664 48 : imag = const_binop (code, imag, div);
1665 : }
1666 : else
1667 : {
1668 : /* In the FALSE branch, we compute
1669 : ratio = d/c;
1670 : divisor = (d * ratio) + c;
1671 : tr = (b * ratio) + a;
1672 : ti = b - (a * ratio);
1673 : tr = tr / div;
1674 : ti = ti / div; */
1675 206 : tree ratio = const_binop (code, i2, r2);
1676 206 : tree div = const_binop (PLUS_EXPR, r2,
1677 : const_binop (MULT_EXPR, i2, ratio));
1678 :
1679 206 : real = const_binop (MULT_EXPR, i1, ratio);
1680 206 : real = const_binop (PLUS_EXPR, real, r1);
1681 206 : real = const_binop (code, real, div);
1682 :
1683 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1684 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1685 206 : imag = const_binop (code, imag, div);
1686 : }
1687 : }
1688 : break;
1689 :
1690 : default:
1691 : return NULL_TREE;
1692 : }
1693 :
1694 6725 : if (real && imag)
1695 6567 : return build_complex (type, real, imag);
1696 : }
1697 :
1698 283841 : tree simplified;
1699 283841 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1700 : return simplified;
1701 :
1702 283266 : if (commutative_tree_code (code)
1703 283266 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1704 : return simplified;
1705 :
1706 278442 : return vector_const_binop (code, arg1, arg2, const_binop);
1707 : }
1708 :
1709 : /* Overload that adds a TYPE parameter to be able to dispatch
1710 : to fold_relational_const. */
1711 :
1712 : tree
1713 301619386 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1714 : {
1715 301619386 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1716 84032102 : return fold_relational_const (code, type, arg1, arg2);
1717 :
1718 : /* ??? Until we make the const_binop worker take the type of the
1719 : result as argument put those cases that need it here. */
1720 217587284 : switch (code)
1721 : {
1722 21 : case VEC_SERIES_EXPR:
1723 21 : if (CONSTANT_CLASS_P (arg1)
1724 21 : && CONSTANT_CLASS_P (arg2))
1725 21 : return build_vec_series (type, arg1, arg2);
1726 : return NULL_TREE;
1727 :
1728 269968 : case COMPLEX_EXPR:
1729 269968 : if ((TREE_CODE (arg1) == REAL_CST
1730 259409 : && TREE_CODE (arg2) == REAL_CST)
1731 10564 : || (TREE_CODE (arg1) == INTEGER_CST
1732 10556 : && TREE_CODE (arg2) == INTEGER_CST))
1733 269960 : return build_complex (type, arg1, arg2);
1734 : return NULL_TREE;
1735 :
1736 180677 : case POINTER_DIFF_EXPR:
1737 180677 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1738 : {
1739 360706 : poly_offset_int res = (wi::to_poly_offset (arg1)
1740 180353 : - wi::to_poly_offset (arg2));
1741 180353 : return force_fit_type (type, res, 1,
1742 180353 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1743 : }
1744 : return NULL_TREE;
1745 :
1746 15801 : case VEC_PACK_TRUNC_EXPR:
1747 15801 : case VEC_PACK_FIX_TRUNC_EXPR:
1748 15801 : case VEC_PACK_FLOAT_EXPR:
1749 15801 : {
1750 15801 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1751 :
1752 15801 : if (TREE_CODE (arg1) != VECTOR_CST
1753 15801 : || TREE_CODE (arg2) != VECTOR_CST)
1754 : return NULL_TREE;
1755 :
1756 15801 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1757 : return NULL_TREE;
1758 :
1759 15801 : out_nelts = in_nelts * 2;
1760 15801 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1761 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1762 :
1763 15801 : tree_vector_builder elts (type, out_nelts, 1);
1764 209026 : for (i = 0; i < out_nelts; i++)
1765 : {
1766 177436 : tree elt = (i < in_nelts
1767 177436 : ? VECTOR_CST_ELT (arg1, i)
1768 88712 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1769 178480 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1770 : ? NOP_EXPR
1771 : : code == VEC_PACK_FLOAT_EXPR
1772 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1773 177436 : TREE_TYPE (type), elt);
1774 177436 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1775 12 : return NULL_TREE;
1776 177424 : elts.quick_push (elt);
1777 : }
1778 :
1779 15789 : return elts.build ();
1780 15801 : }
1781 :
1782 210 : case VEC_WIDEN_MULT_LO_EXPR:
1783 210 : case VEC_WIDEN_MULT_HI_EXPR:
1784 210 : case VEC_WIDEN_MULT_EVEN_EXPR:
1785 210 : case VEC_WIDEN_MULT_ODD_EXPR:
1786 210 : {
1787 210 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1788 :
1789 210 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1790 : return NULL_TREE;
1791 :
1792 210 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1793 : return NULL_TREE;
1794 210 : out_nelts = in_nelts / 2;
1795 210 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1796 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1797 :
1798 210 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1799 : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1800 : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1801 : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1802 : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1803 : scale = 1, ofs = 0;
1804 : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1805 210 : scale = 1, ofs = 1;
1806 :
1807 210 : tree_vector_builder elts (type, out_nelts, 1);
1808 960 : for (out = 0; out < out_nelts; out++)
1809 : {
1810 540 : unsigned int in = (out << scale) + ofs;
1811 540 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1812 : VECTOR_CST_ELT (arg1, in));
1813 540 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1814 : VECTOR_CST_ELT (arg2, in));
1815 :
1816 540 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1817 0 : return NULL_TREE;
1818 540 : tree elt = const_binop (MULT_EXPR, t1, t2);
1819 540 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1820 : return NULL_TREE;
1821 540 : elts.quick_push (elt);
1822 : }
1823 :
1824 210 : return elts.build ();
1825 210 : }
1826 :
1827 217120607 : default:;
1828 : }
1829 :
1830 217120607 : if (TREE_CODE_CLASS (code) != tcc_binary)
1831 : return NULL_TREE;
1832 :
1833 : /* Make sure type and arg0 have the same saturating flag. */
1834 214612137 : gcc_checking_assert (TYPE_SATURATING (type)
1835 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1836 :
1837 214612137 : return const_binop (code, arg1, arg2);
1838 : }
1839 :
1840 : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1841 : Return zero if computing the constants is not possible. */
1842 :
1843 : tree
1844 383884150 : const_unop (enum tree_code code, tree type, tree arg0)
1845 : {
1846 : /* Don't perform the operation, other than NEGATE and ABS, if
1847 : flag_signaling_nans is on and the operand is a signaling NaN. */
1848 383884150 : if (TREE_CODE (arg0) == REAL_CST
1849 11185131 : && HONOR_SNANS (arg0)
1850 17121 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1851 4740 : && code != NEGATE_EXPR
1852 4740 : && code != ABS_EXPR
1853 383888855 : && code != ABSU_EXPR)
1854 : return NULL_TREE;
1855 :
1856 383879445 : switch (code)
1857 : {
1858 288091312 : CASE_CONVERT:
1859 288091312 : case FLOAT_EXPR:
1860 288091312 : case FIX_TRUNC_EXPR:
1861 288091312 : case FIXED_CONVERT_EXPR:
1862 288091312 : return fold_convert_const (code, type, arg0);
1863 :
1864 0 : case ADDR_SPACE_CONVERT_EXPR:
1865 : /* If the source address is 0, and the source address space
1866 : cannot have a valid object at 0, fold to dest type null. */
1867 0 : if (integer_zerop (arg0)
1868 0 : && !(targetm.addr_space.zero_address_valid
1869 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1870 0 : return fold_convert_const (code, type, arg0);
1871 : break;
1872 :
1873 13333720 : case VIEW_CONVERT_EXPR:
1874 13333720 : return fold_view_convert_expr (type, arg0);
1875 :
1876 32013528 : case NEGATE_EXPR:
1877 32013528 : {
1878 : /* Can't call fold_negate_const directly here as that doesn't
1879 : handle all cases and we might not be able to negate some
1880 : constants. */
1881 32013528 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1882 32013528 : if (tem && CONSTANT_CLASS_P (tem))
1883 31866071 : return tem;
1884 : break;
1885 : }
1886 :
1887 40310 : case ABS_EXPR:
1888 40310 : case ABSU_EXPR:
1889 40310 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1890 35609 : return fold_abs_const (arg0, type);
1891 : break;
1892 :
1893 25516 : case CONJ_EXPR:
1894 25516 : if (TREE_CODE (arg0) == COMPLEX_CST)
1895 : {
1896 25513 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1897 25513 : TREE_TYPE (type));
1898 25513 : return build_complex (type, TREE_REALPART (arg0), ipart);
1899 : }
1900 : break;
1901 :
1902 2407483 : case BIT_NOT_EXPR:
1903 2407483 : if (TREE_CODE (arg0) == INTEGER_CST)
1904 2400323 : return fold_not_const (arg0, type);
1905 7160 : else if (POLY_INT_CST_P (arg0))
1906 : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1907 : /* Perform BIT_NOT_EXPR on each element individually. */
1908 7160 : else if (TREE_CODE (arg0) == VECTOR_CST)
1909 : {
1910 6497 : tree elem;
1911 :
1912 : /* This can cope with stepped encodings because ~x == -1 - x. */
1913 6497 : tree_vector_builder elements;
1914 6497 : elements.new_unary_operation (type, arg0, true);
1915 6497 : unsigned int i, count = elements.encoded_nelts ();
1916 25135 : for (i = 0; i < count; ++i)
1917 : {
1918 18638 : elem = VECTOR_CST_ELT (arg0, i);
1919 18638 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1920 18638 : if (elem == NULL_TREE)
1921 : break;
1922 18638 : elements.quick_push (elem);
1923 : }
1924 6497 : if (i == count)
1925 6497 : return elements.build ();
1926 6497 : }
1927 : break;
1928 :
1929 11748483 : case TRUTH_NOT_EXPR:
1930 11748483 : if (TREE_CODE (arg0) == INTEGER_CST)
1931 11378894 : return constant_boolean_node (integer_zerop (arg0), type);
1932 : break;
1933 :
1934 179907 : case REALPART_EXPR:
1935 179907 : if (TREE_CODE (arg0) == COMPLEX_CST)
1936 179706 : return fold_convert (type, TREE_REALPART (arg0));
1937 : break;
1938 :
1939 185704 : case IMAGPART_EXPR:
1940 185704 : if (TREE_CODE (arg0) == COMPLEX_CST)
1941 185516 : return fold_convert (type, TREE_IMAGPART (arg0));
1942 : break;
1943 :
1944 19104 : case VEC_UNPACK_LO_EXPR:
1945 19104 : case VEC_UNPACK_HI_EXPR:
1946 19104 : case VEC_UNPACK_FLOAT_LO_EXPR:
1947 19104 : case VEC_UNPACK_FLOAT_HI_EXPR:
1948 19104 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1949 19104 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1950 19104 : {
1951 19104 : poly_uint64 out_nelts = TYPE_VECTOR_SUBPARTS (type);
1952 19104 : unsigned HOST_WIDE_INT npatterns, nelts_per_pattern, i, offset = 0;
1953 19104 : enum tree_code subcode;
1954 :
1955 19104 : if (TREE_CODE (arg0) != VECTOR_CST)
1956 : return NULL_TREE;
1957 :
1958 19104 : gcc_assert (known_eq (VECTOR_CST_NELTS (arg0), out_nelts * 2));
1959 :
1960 19104 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1961 : subcode = NOP_EXPR;
1962 8006 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1963 8006 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1964 : subcode = FLOAT_EXPR;
1965 : else
1966 4 : subcode = FIX_TRUNC_EXPR;
1967 :
1968 38208 : bool low_p = (code == VEC_UNPACK_LO_EXPR
1969 19104 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1970 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR);
1971 :
1972 19104 : npatterns = VECTOR_CST_NPATTERNS (arg0);
1973 19104 : nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (arg0);
1974 25289 : if (nelts_per_pattern <= 2 && multiple_p (out_nelts, npatterns))
1975 : /* The input is:
1976 :
1977 : { a1,...,an,b1,...,bn,b1,...,bn,b1,...,bn,... }
1978 :
1979 : where N == NPATTERNS and where ai == bi for NELTS_PER_PATTERN == 1
1980 : but not necessarily for NELTS_PER_PATTERN == 2.
1981 :
1982 : When unpacking the first half, the shape of the output is the same
1983 : as the input. That shape would also work for the second half,
1984 : but setting the output nelts_per_patterns to 1 is always enough
1985 : in that case (since the output always starts on b1). */
1986 : ;
1987 : else
1988 : {
1989 : if (!out_nelts.is_constant (&npatterns))
1990 : return NULL_TREE;
1991 : nelts_per_pattern = 1;
1992 : }
1993 :
1994 19104 : if ((!BYTES_BIG_ENDIAN) ^ low_p)
1995 : {
1996 9544 : offset = npatterns;
1997 9544 : nelts_per_pattern = 1;
1998 : }
1999 :
2000 19104 : tree_vector_builder elts (type, npatterns, nelts_per_pattern);
2001 116476 : for (i = 0; i < npatterns * nelts_per_pattern; i++)
2002 : {
2003 78268 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2004 78268 : VECTOR_CST_ELT (arg0, i + offset));
2005 78268 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2006 0 : return NULL_TREE;
2007 78268 : elts.quick_push (elt);
2008 : }
2009 :
2010 19104 : return elts.build ();
2011 19104 : }
2012 :
2013 4 : case VEC_DUPLICATE_EXPR:
2014 4 : if (CONSTANT_CLASS_P (arg0))
2015 4 : return build_vector_from_val (type, arg0);
2016 : return NULL_TREE;
2017 :
2018 : default:
2019 : break;
2020 : }
2021 :
2022 : return NULL_TREE;
2023 : }
2024 :
2025 : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2026 : indicates which particular sizetype to create. */
2027 :
2028 : tree
2029 3666903301 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2030 : {
2031 3666903301 : return build_int_cst (sizetype_tab[(int) kind], number);
2032 : }
2033 :
2034 : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2035 : is a tree code. The type of the result is taken from the operands.
2036 : Both must be equivalent integer types, ala int_binop_types_match_p.
2037 : If the operands are constant, so is the result. */
2038 :
2039 : tree
2040 2745480885 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2041 : {
2042 2745480885 : tree type = TREE_TYPE (arg0);
2043 :
2044 2745480885 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2045 : return error_mark_node;
2046 :
2047 2745480885 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2048 : TREE_TYPE (arg1)));
2049 :
2050 : /* Handle the special case of two poly_int constants faster. */
2051 2745480885 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2052 : {
2053 : /* And some specific cases even faster than that. */
2054 2712559655 : if (code == PLUS_EXPR)
2055 : {
2056 1254786728 : if (integer_zerop (arg0)
2057 1254786728 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2058 : return arg1;
2059 320350615 : if (integer_zerop (arg1)
2060 320350615 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2061 : return arg0;
2062 : }
2063 1457772927 : else if (code == MINUS_EXPR)
2064 : {
2065 124065283 : if (integer_zerop (arg1)
2066 124065283 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2067 : return arg0;
2068 : }
2069 1333707644 : else if (code == MULT_EXPR)
2070 : {
2071 629174850 : if (integer_onep (arg0)
2072 629174850 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2073 : return arg1;
2074 : }
2075 :
2076 : /* Handle general case of two integer constants. For sizetype
2077 : constant calculations we always want to know about overflow,
2078 : even in the unsigned case. */
2079 1305400292 : tree res = int_const_binop (code, arg0, arg1, -1);
2080 1305400292 : if (res != NULL_TREE)
2081 : return res;
2082 : }
2083 :
2084 32921230 : return fold_build2_loc (loc, code, type, arg0, arg1);
2085 : }
2086 :
2087 : /* Given two values, either both of sizetype or both of bitsizetype,
2088 : compute the difference between the two values. Return the value
2089 : in signed type corresponding to the type of the operands. */
2090 :
2091 : tree
2092 39167802 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2093 : {
2094 39167802 : tree type = TREE_TYPE (arg0);
2095 39167802 : tree ctype;
2096 :
2097 39167802 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2098 : TREE_TYPE (arg1)));
2099 :
2100 : /* If the type is already signed, just do the simple thing. */
2101 39167802 : if (!TYPE_UNSIGNED (type))
2102 10531022 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2103 :
2104 28636780 : if (type == sizetype)
2105 28636780 : ctype = ssizetype;
2106 0 : else if (type == bitsizetype)
2107 0 : ctype = sbitsizetype;
2108 : else
2109 0 : ctype = signed_type_for (type);
2110 :
2111 : /* If either operand is not a constant, do the conversions to the signed
2112 : type and subtract. The hardware will do the right thing with any
2113 : overflow in the subtraction. */
2114 28636780 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2115 17834 : return size_binop_loc (loc, MINUS_EXPR,
2116 : fold_convert_loc (loc, ctype, arg0),
2117 17834 : fold_convert_loc (loc, ctype, arg1));
2118 :
2119 : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2120 : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2121 : overflow) and negate (which can't either). Special-case a result
2122 : of zero while we're here. */
2123 28618946 : if (tree_int_cst_equal (arg0, arg1))
2124 25317043 : return build_int_cst (ctype, 0);
2125 3301903 : else if (tree_int_cst_lt (arg1, arg0))
2126 2160659 : return fold_convert_loc (loc, ctype,
2127 2160659 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2128 : else
2129 1141244 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2130 : fold_convert_loc (loc, ctype,
2131 : size_binop_loc (loc,
2132 : MINUS_EXPR,
2133 : arg1, arg0)));
2134 : }
2135 :
2136 : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2137 : or pointer type. */
2138 :
2139 : tree
2140 1479066700 : int_const_convert (tree type, const_tree arg1, int overflowable)
2141 : {
2142 : /* Given an integer constant, make new constant with new type,
2143 : appropriately sign-extended or truncated. Use widest_int
2144 : so that any extension is done according ARG1's type. */
2145 1479066700 : tree arg1_type = TREE_TYPE (arg1);
2146 1479066700 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2147 1479066700 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2148 1479066700 : TYPE_SIGN (arg1_type)),
2149 : overflowable,
2150 1479066700 : TREE_OVERFLOW (arg1));
2151 : }
2152 :
2153 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2154 : to an integer type. */
2155 :
2156 : static tree
2157 58835 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2158 : {
2159 58835 : bool overflow = false;
2160 58835 : tree t;
2161 :
2162 : /* The following code implements the floating point to integer
2163 : conversion rules required by the Java Language Specification,
2164 : that IEEE NaNs are mapped to zero and values that overflow
2165 : the target precision saturate, i.e. values greater than
2166 : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2167 : are mapped to INT_MIN. These semantics are allowed by the
2168 : C and C++ standards that simply state that the behavior of
2169 : FP-to-integer conversion is unspecified upon overflow. */
2170 :
2171 58835 : wide_int val;
2172 58835 : REAL_VALUE_TYPE r;
2173 58835 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2174 :
2175 58835 : switch (code)
2176 : {
2177 58835 : case FIX_TRUNC_EXPR:
2178 58835 : real_trunc (&r, VOIDmode, &x);
2179 58835 : break;
2180 :
2181 0 : default:
2182 0 : gcc_unreachable ();
2183 : }
2184 :
2185 : /* If R is NaN, return zero and show we have an overflow. */
2186 58835 : if (REAL_VALUE_ISNAN (r))
2187 : {
2188 3638 : overflow = true;
2189 3638 : val = wi::zero (TYPE_PRECISION (type));
2190 : }
2191 :
2192 : /* See if R is less than the lower bound or greater than the
2193 : upper bound. */
2194 :
2195 58835 : if (! overflow)
2196 : {
2197 55197 : tree lt = TYPE_MIN_VALUE (type);
2198 55197 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2199 55197 : if (real_less (&r, &l))
2200 : {
2201 1992 : overflow = true;
2202 1992 : val = wi::to_wide (lt);
2203 : }
2204 : }
2205 :
2206 58835 : if (! overflow)
2207 : {
2208 53205 : tree ut = TYPE_MAX_VALUE (type);
2209 53205 : if (ut)
2210 : {
2211 53205 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2212 53205 : if (real_less (&u, &r))
2213 : {
2214 1921 : overflow = true;
2215 1921 : val = wi::to_wide (ut);
2216 : }
2217 : }
2218 : }
2219 :
2220 58835 : if (! overflow)
2221 51286 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2222 :
2223 : /* According to IEEE standard, for conversions from floating point to
2224 : integer. When a NaN or infinite operand cannot be represented in the
2225 : destination format and this cannot otherwise be indicated, the invalid
2226 : operation exception shall be signaled. When a numeric operand would
2227 : convert to an integer outside the range of the destination format, the
2228 : invalid operation exception shall be signaled if this situation cannot
2229 : otherwise be indicated. */
2230 58835 : if (!flag_trapping_math || !overflow)
2231 51540 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2232 : else
2233 : t = NULL_TREE;
2234 :
2235 58835 : return t;
2236 58835 : }
2237 :
2238 : /* A subroutine of fold_convert_const handling conversions of a
2239 : FIXED_CST to an integer type. */
2240 :
2241 : static tree
2242 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2243 : {
2244 0 : tree t;
2245 0 : double_int temp, temp_trunc;
2246 0 : scalar_mode mode;
2247 :
2248 : /* Right shift FIXED_CST to temp by fbit. */
2249 0 : temp = TREE_FIXED_CST (arg1).data;
2250 0 : mode = TREE_FIXED_CST (arg1).mode;
2251 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2252 : {
2253 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2254 : HOST_BITS_PER_DOUBLE_INT,
2255 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2256 :
2257 : /* Left shift temp to temp_trunc by fbit. */
2258 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2259 : HOST_BITS_PER_DOUBLE_INT,
2260 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2261 : }
2262 : else
2263 : {
2264 0 : temp = double_int_zero;
2265 0 : temp_trunc = double_int_zero;
2266 : }
2267 :
2268 : /* If FIXED_CST is negative, we need to round the value toward 0.
2269 : By checking if the fractional bits are not zero to add 1 to temp. */
2270 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2271 0 : && temp_trunc.is_negative ()
2272 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2273 0 : temp += double_int_one;
2274 :
2275 : /* Given a fixed-point constant, make new constant with new type,
2276 : appropriately sign-extended or truncated. */
2277 0 : t = force_fit_type (type, temp, -1,
2278 0 : (temp.is_negative ()
2279 0 : && (TYPE_UNSIGNED (type)
2280 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2281 0 : | TREE_OVERFLOW (arg1));
2282 :
2283 0 : return t;
2284 : }
2285 :
2286 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2287 : to another floating point type. */
2288 :
2289 : static tree
2290 2264163 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2291 : {
2292 2264163 : REAL_VALUE_TYPE value;
2293 2264163 : tree t;
2294 :
2295 : /* If the underlying modes are the same, simply treat it as
2296 : copy and rebuild with TREE_REAL_CST information and the
2297 : given type. */
2298 2264163 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2299 : {
2300 99294 : t = build_real (type, TREE_REAL_CST (arg1));
2301 99294 : return t;
2302 : }
2303 :
2304 : /* Don't perform the operation if flag_signaling_nans is on
2305 : and the operand is a signaling NaN. */
2306 2164869 : if (HONOR_SNANS (arg1)
2307 2166751 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2308 : return NULL_TREE;
2309 :
2310 : /* With flag_rounding_math we should respect the current rounding mode
2311 : unless the conversion is exact. */
2312 2164869 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2313 2165525 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2314 509 : return NULL_TREE;
2315 :
2316 2164360 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2317 2164360 : t = build_real (type, value);
2318 :
2319 : /* If converting an infinity or NAN to a representation that doesn't
2320 : have one, set the overflow bit so that we can produce some kind of
2321 : error message at the appropriate point if necessary. It's not the
2322 : most user-friendly message, but it's better than nothing. */
2323 2164360 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2324 2305974 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2325 0 : TREE_OVERFLOW (t) = 1;
2326 2164360 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2327 2301654 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2328 0 : TREE_OVERFLOW (t) = 1;
2329 : /* Regular overflow, conversion produced an infinity in a mode that
2330 : can't represent them. */
2331 10818482 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2332 0 : && REAL_VALUE_ISINF (value)
2333 2164360 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2334 0 : TREE_OVERFLOW (t) = 1;
2335 : else
2336 2164360 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2337 : return t;
2338 : }
2339 :
2340 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2341 : to a floating point type. */
2342 :
2343 : static tree
2344 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2345 : {
2346 0 : REAL_VALUE_TYPE value;
2347 0 : tree t;
2348 :
2349 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2350 0 : &TREE_FIXED_CST (arg1));
2351 0 : t = build_real (type, value);
2352 :
2353 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2354 0 : return t;
2355 : }
2356 :
2357 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2358 : to another fixed-point type. */
2359 :
2360 : static tree
2361 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2362 : {
2363 0 : FIXED_VALUE_TYPE value;
2364 0 : tree t;
2365 0 : bool overflow_p;
2366 :
2367 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2368 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2369 0 : t = build_fixed (type, value);
2370 :
2371 : /* Propagate overflow flags. */
2372 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2373 0 : TREE_OVERFLOW (t) = 1;
2374 0 : return t;
2375 : }
2376 :
2377 : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2378 : to a fixed-point type. */
2379 :
2380 : static tree
2381 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2382 : {
2383 0 : FIXED_VALUE_TYPE value;
2384 0 : tree t;
2385 0 : bool overflow_p;
2386 0 : double_int di;
2387 :
2388 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2389 :
2390 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2391 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2392 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2393 : else
2394 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2395 :
2396 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2397 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2398 0 : TYPE_SATURATING (type));
2399 0 : t = build_fixed (type, value);
2400 :
2401 : /* Propagate overflow flags. */
2402 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2403 0 : TREE_OVERFLOW (t) = 1;
2404 0 : return t;
2405 : }
2406 :
2407 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2408 : to a fixed-point type. */
2409 :
2410 : static tree
2411 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2412 : {
2413 0 : FIXED_VALUE_TYPE value;
2414 0 : tree t;
2415 0 : bool overflow_p;
2416 :
2417 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2418 0 : &TREE_REAL_CST (arg1),
2419 0 : TYPE_SATURATING (type));
2420 0 : t = build_fixed (type, value);
2421 :
2422 : /* Propagate overflow flags. */
2423 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2424 0 : TREE_OVERFLOW (t) = 1;
2425 0 : return t;
2426 : }
2427 :
2428 : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2429 : type TYPE. If no simplification can be done return NULL_TREE. */
2430 :
2431 : static tree
2432 1544491064 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2433 : {
2434 1544491064 : tree arg_type = TREE_TYPE (arg1);
2435 1544491064 : if (arg_type == type)
2436 : return arg1;
2437 :
2438 : /* We can't widen types, since the runtime value could overflow the
2439 : original type before being extended to the new type. */
2440 1532969786 : if (POLY_INT_CST_P (arg1)
2441 : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2442 : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2443 : return build_poly_int_cst (type,
2444 : poly_wide_int::from (poly_int_cst_value (arg1),
2445 : TYPE_PRECISION (type),
2446 : TYPE_SIGN (arg_type)));
2447 :
2448 1532969786 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2449 : || TREE_CODE (type) == OFFSET_TYPE)
2450 : {
2451 1501265658 : if (TREE_CODE (arg1) == INTEGER_CST)
2452 1479066700 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2453 22198958 : else if (TREE_CODE (arg1) == REAL_CST)
2454 58835 : return fold_convert_const_int_from_real (code, type, arg1);
2455 22140123 : else if (TREE_CODE (arg1) == FIXED_CST)
2456 0 : return fold_convert_const_int_from_fixed (type, arg1);
2457 : }
2458 : else if (SCALAR_FLOAT_TYPE_P (type))
2459 : {
2460 31646771 : if (TREE_CODE (arg1) == INTEGER_CST)
2461 : {
2462 24320783 : tree res = build_real_from_int_cst (type, arg1);
2463 : /* Avoid the folding if flag_rounding_math is on and the
2464 : conversion is not exact. */
2465 24320783 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2466 : {
2467 2914 : bool fail = false;
2468 5828 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2469 2914 : TYPE_PRECISION (TREE_TYPE (arg1)));
2470 2914 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2471 1755 : return NULL_TREE;
2472 2914 : }
2473 : return res;
2474 : }
2475 7325988 : else if (TREE_CODE (arg1) == REAL_CST)
2476 2264163 : return fold_convert_const_real_from_real (type, arg1);
2477 5061825 : else if (TREE_CODE (arg1) == FIXED_CST)
2478 0 : return fold_convert_const_real_from_fixed (type, arg1);
2479 : }
2480 : else if (FIXED_POINT_TYPE_P (type))
2481 : {
2482 0 : if (TREE_CODE (arg1) == FIXED_CST)
2483 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2484 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2485 0 : return fold_convert_const_fixed_from_int (type, arg1);
2486 0 : else if (TREE_CODE (arg1) == REAL_CST)
2487 0 : return fold_convert_const_fixed_from_real (type, arg1);
2488 : }
2489 : else if (VECTOR_TYPE_P (type))
2490 : {
2491 4836 : if (TREE_CODE (arg1) == VECTOR_CST
2492 4836 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2493 : {
2494 4836 : tree elttype = TREE_TYPE (type);
2495 4836 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2496 : /* We can't handle steps directly when extending, since the
2497 : values need to wrap at the original precision first. */
2498 4836 : bool step_ok_p
2499 4836 : = (INTEGRAL_TYPE_P (elttype)
2500 341 : && INTEGRAL_TYPE_P (arg1_elttype)
2501 5117 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2502 4836 : tree_vector_builder v;
2503 4836 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2504 : return NULL_TREE;
2505 4836 : unsigned int len = v.encoded_nelts ();
2506 28314 : for (unsigned int i = 0; i < len; ++i)
2507 : {
2508 23478 : tree elt = VECTOR_CST_ELT (arg1, i);
2509 23478 : tree cvt = fold_convert_const (code, elttype, elt);
2510 23478 : if (cvt == NULL_TREE)
2511 0 : return NULL_TREE;
2512 23478 : v.quick_push (cvt);
2513 : }
2514 4836 : return v.build ();
2515 4836 : }
2516 : }
2517 12292 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2518 12292 : return build_zero_cst (type);
2519 : return NULL_TREE;
2520 : }
2521 :
2522 : /* Construct a vector of zero elements of vector type TYPE. */
2523 :
2524 : static tree
2525 17493 : build_zero_vector (tree type)
2526 : {
2527 17493 : tree t;
2528 :
2529 17493 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2530 17493 : return build_vector_from_val (type, t);
2531 : }
2532 :
2533 : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2534 :
2535 : bool
2536 4386 : fold_convertible_p (const_tree type, const_tree arg)
2537 : {
2538 4386 : const_tree orig = TREE_TYPE (arg);
2539 :
2540 4386 : if (type == orig)
2541 : return true;
2542 :
2543 4386 : if (TREE_CODE (arg) == ERROR_MARK
2544 4386 : || TREE_CODE (type) == ERROR_MARK
2545 4386 : || TREE_CODE (orig) == ERROR_MARK)
2546 : return false;
2547 :
2548 4386 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2549 : return true;
2550 :
2551 4386 : switch (TREE_CODE (type))
2552 : {
2553 3823 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2554 3823 : case POINTER_TYPE: case REFERENCE_TYPE:
2555 3823 : case OFFSET_TYPE:
2556 3823 : return (INTEGRAL_TYPE_P (orig)
2557 386 : || (POINTER_TYPE_P (orig)
2558 251 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2559 3958 : || TREE_CODE (orig) == OFFSET_TYPE);
2560 :
2561 130 : case REAL_TYPE:
2562 130 : case FIXED_POINT_TYPE:
2563 130 : case VOID_TYPE:
2564 130 : return TREE_CODE (type) == TREE_CODE (orig);
2565 :
2566 209 : case VECTOR_TYPE:
2567 209 : return (VECTOR_TYPE_P (orig)
2568 322 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2569 : TYPE_VECTOR_SUBPARTS (orig))
2570 226 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2571 :
2572 : default:
2573 : return false;
2574 : }
2575 : }
2576 :
2577 : /* Convert expression ARG to type TYPE. Used by the middle-end for
2578 : simple conversions in preference to calling the front-end's convert. */
2579 :
2580 : tree
2581 2274256413 : fold_convert_loc (location_t loc, tree type, tree arg)
2582 : {
2583 2274256413 : tree orig = TREE_TYPE (arg);
2584 2274256413 : tree tem;
2585 :
2586 2274256413 : if (type == orig)
2587 : return arg;
2588 :
2589 1532160072 : if (TREE_CODE (arg) == ERROR_MARK
2590 1532159026 : || TREE_CODE (type) == ERROR_MARK
2591 1532159025 : || TREE_CODE (orig) == ERROR_MARK)
2592 1047 : return error_mark_node;
2593 :
2594 1532159025 : switch (TREE_CODE (type))
2595 : {
2596 123178770 : case POINTER_TYPE:
2597 123178770 : case REFERENCE_TYPE:
2598 : /* Handle conversions between pointers to different address spaces. */
2599 123178770 : if (POINTER_TYPE_P (orig)
2600 123178770 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2601 103844629 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2602 125 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2603 : /* fall through */
2604 :
2605 1499620784 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2606 1499620784 : case OFFSET_TYPE: case BITINT_TYPE:
2607 1499620784 : if (TREE_CODE (arg) == INTEGER_CST)
2608 : {
2609 1255922099 : tem = fold_convert_const (NOP_EXPR, type, arg);
2610 1255922099 : if (tem != NULL_TREE)
2611 : return tem;
2612 : }
2613 243698685 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2614 2548 : || TREE_CODE (orig) == OFFSET_TYPE)
2615 243698685 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2616 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2617 0 : return fold_convert_loc (loc, type,
2618 : fold_build1_loc (loc, REALPART_EXPR,
2619 0 : TREE_TYPE (orig), arg));
2620 0 : gcc_assert (VECTOR_TYPE_P (orig)
2621 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2622 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2623 :
2624 558552 : case REAL_TYPE:
2625 558552 : if (TREE_CODE (arg) == INTEGER_CST)
2626 : {
2627 58055 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2628 58055 : if (tem != NULL_TREE)
2629 : return tem;
2630 : }
2631 500497 : else if (TREE_CODE (arg) == REAL_CST)
2632 : {
2633 121843 : tem = fold_convert_const (NOP_EXPR, type, arg);
2634 121843 : if (tem != NULL_TREE)
2635 : return tem;
2636 : }
2637 378654 : else if (TREE_CODE (arg) == FIXED_CST)
2638 : {
2639 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2640 0 : if (tem != NULL_TREE)
2641 : return tem;
2642 : }
2643 :
2644 378656 : switch (TREE_CODE (orig))
2645 : {
2646 721 : case INTEGER_TYPE: case BITINT_TYPE:
2647 721 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2648 721 : case POINTER_TYPE: case REFERENCE_TYPE:
2649 721 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2650 :
2651 377935 : case REAL_TYPE:
2652 377935 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2653 :
2654 0 : case FIXED_POINT_TYPE:
2655 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2656 :
2657 0 : case COMPLEX_TYPE:
2658 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2659 0 : return fold_convert_loc (loc, type, tem);
2660 :
2661 0 : default:
2662 0 : gcc_unreachable ();
2663 : }
2664 :
2665 0 : case FIXED_POINT_TYPE:
2666 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2667 0 : || TREE_CODE (arg) == REAL_CST)
2668 : {
2669 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2670 0 : if (tem != NULL_TREE)
2671 0 : goto fold_convert_exit;
2672 : }
2673 :
2674 0 : switch (TREE_CODE (orig))
2675 : {
2676 0 : case FIXED_POINT_TYPE:
2677 0 : case INTEGER_TYPE:
2678 0 : case ENUMERAL_TYPE:
2679 0 : case BOOLEAN_TYPE:
2680 0 : case REAL_TYPE:
2681 0 : case BITINT_TYPE:
2682 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2683 :
2684 0 : case COMPLEX_TYPE:
2685 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2686 0 : return fold_convert_loc (loc, type, tem);
2687 :
2688 0 : default:
2689 0 : gcc_unreachable ();
2690 : }
2691 :
2692 2269 : case COMPLEX_TYPE:
2693 2269 : switch (TREE_CODE (orig))
2694 : {
2695 584 : case INTEGER_TYPE: case BITINT_TYPE:
2696 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2697 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2698 584 : case REAL_TYPE:
2699 584 : case FIXED_POINT_TYPE:
2700 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2701 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2702 584 : fold_convert_loc (loc, TREE_TYPE (type),
2703 584 : integer_zero_node));
2704 1685 : case COMPLEX_TYPE:
2705 1685 : {
2706 1685 : tree rpart, ipart;
2707 :
2708 1685 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2709 : {
2710 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2711 1534 : TREE_OPERAND (arg, 0));
2712 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2713 1534 : TREE_OPERAND (arg, 1));
2714 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2715 : }
2716 :
2717 151 : arg = save_expr (arg);
2718 151 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2719 151 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2720 151 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2721 151 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2722 151 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2723 : }
2724 :
2725 0 : default:
2726 0 : gcc_unreachable ();
2727 : }
2728 :
2729 31862062 : case VECTOR_TYPE:
2730 31862062 : if (integer_zerop (arg))
2731 17493 : return build_zero_vector (type);
2732 31844569 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2733 31844569 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2734 : || VECTOR_TYPE_P (orig));
2735 31844569 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2736 :
2737 111433 : case VOID_TYPE:
2738 111433 : tem = fold_ignored_result (arg);
2739 111433 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2740 :
2741 63 : case NULLPTR_TYPE:
2742 63 : if (integer_zerop (arg))
2743 17 : return build_zero_cst (type);
2744 : /* FALLTHRU */
2745 3783 : default:
2746 3783 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2747 3783 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2748 0 : gcc_unreachable ();
2749 : }
2750 0 : fold_convert_exit:
2751 0 : tem = protected_set_expr_location_unshare (tem, loc);
2752 0 : return tem;
2753 : }
2754 :
2755 : /* Return false if expr can be assumed not to be an lvalue, true
2756 : otherwise. */
2757 :
2758 : static bool
2759 54369166 : maybe_lvalue_p (const_tree x)
2760 : {
2761 : /* We only need to wrap lvalue tree codes. */
2762 54369166 : switch (TREE_CODE (x))
2763 : {
2764 : case VAR_DECL:
2765 : case PARM_DECL:
2766 : case RESULT_DECL:
2767 : case LABEL_DECL:
2768 : case FUNCTION_DECL:
2769 : case SSA_NAME:
2770 : case COMPOUND_LITERAL_EXPR:
2771 :
2772 : case COMPONENT_REF:
2773 : case MEM_REF:
2774 : case INDIRECT_REF:
2775 : case ARRAY_REF:
2776 : case ARRAY_RANGE_REF:
2777 : case BIT_FIELD_REF:
2778 : case OBJ_TYPE_REF:
2779 :
2780 : case REALPART_EXPR:
2781 : case IMAGPART_EXPR:
2782 : case PREINCREMENT_EXPR:
2783 : case PREDECREMENT_EXPR:
2784 : case SAVE_EXPR:
2785 : case TRY_CATCH_EXPR:
2786 : case WITH_CLEANUP_EXPR:
2787 : case COMPOUND_EXPR:
2788 : case MODIFY_EXPR:
2789 : case TARGET_EXPR:
2790 : case COND_EXPR:
2791 : case BIND_EXPR:
2792 : case VIEW_CONVERT_EXPR:
2793 : break;
2794 :
2795 39983008 : default:
2796 : /* Assume the worst for front-end tree codes. */
2797 39983008 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2798 : break;
2799 : return false;
2800 : }
2801 :
2802 14421347 : return true;
2803 : }
2804 :
2805 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2806 :
2807 : tree
2808 47245904 : non_lvalue_loc (location_t loc, tree x)
2809 : {
2810 : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2811 : us. */
2812 47245904 : if (in_gimple_form)
2813 : return x;
2814 :
2815 10962777 : if (! maybe_lvalue_p (x))
2816 : return x;
2817 2612540 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2818 : }
2819 :
2820 : /* Given a tree comparison code, return the code that is the logical inverse.
2821 : It is generally not safe to do this for floating-point comparisons, except
2822 : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2823 : ERROR_MARK in this case. */
2824 :
2825 : enum tree_code
2826 133042642 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2827 : {
2828 133042642 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2829 1062371 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2830 : return ERROR_MARK;
2831 :
2832 132231798 : switch (code)
2833 : {
2834 : case EQ_EXPR:
2835 : return NE_EXPR;
2836 58413261 : case NE_EXPR:
2837 58413261 : return EQ_EXPR;
2838 12489219 : case GT_EXPR:
2839 12489219 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2840 18115869 : case GE_EXPR:
2841 18115869 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2842 8136427 : case LT_EXPR:
2843 8136427 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2844 8176414 : case LE_EXPR:
2845 8176414 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2846 261 : case LTGT_EXPR:
2847 261 : return UNEQ_EXPR;
2848 292 : case UNEQ_EXPR:
2849 292 : return LTGT_EXPR;
2850 : case UNGT_EXPR:
2851 : return LE_EXPR;
2852 : case UNGE_EXPR:
2853 : return LT_EXPR;
2854 : case UNLT_EXPR:
2855 : return GE_EXPR;
2856 : case UNLE_EXPR:
2857 : return GT_EXPR;
2858 223727 : case ORDERED_EXPR:
2859 223727 : return UNORDERED_EXPR;
2860 56644 : case UNORDERED_EXPR:
2861 56644 : return ORDERED_EXPR;
2862 0 : default:
2863 0 : gcc_unreachable ();
2864 : }
2865 : }
2866 :
2867 : /* Similar, but return the comparison that results if the operands are
2868 : swapped. This is safe for floating-point. */
2869 :
2870 : enum tree_code
2871 162789553 : swap_tree_comparison (enum tree_code code)
2872 : {
2873 162789553 : switch (code)
2874 : {
2875 : case EQ_EXPR:
2876 : case NE_EXPR:
2877 : case ORDERED_EXPR:
2878 : case UNORDERED_EXPR:
2879 : case LTGT_EXPR:
2880 : case UNEQ_EXPR:
2881 : return code;
2882 39679280 : case GT_EXPR:
2883 39679280 : return LT_EXPR;
2884 11236022 : case GE_EXPR:
2885 11236022 : return LE_EXPR;
2886 22156985 : case LT_EXPR:
2887 22156985 : return GT_EXPR;
2888 17035034 : case LE_EXPR:
2889 17035034 : return GE_EXPR;
2890 254329 : case UNGT_EXPR:
2891 254329 : return UNLT_EXPR;
2892 20718 : case UNGE_EXPR:
2893 20718 : return UNLE_EXPR;
2894 377941 : case UNLT_EXPR:
2895 377941 : return UNGT_EXPR;
2896 112342 : case UNLE_EXPR:
2897 112342 : return UNGE_EXPR;
2898 0 : default:
2899 0 : gcc_unreachable ();
2900 : }
2901 : }
2902 :
2903 :
2904 : /* Convert a comparison tree code from an enum tree_code representation
2905 : into a compcode bit-based encoding. This function is the inverse of
2906 : compcode_to_comparison. */
2907 :
2908 : static enum comparison_code
2909 97932 : comparison_to_compcode (enum tree_code code)
2910 : {
2911 97932 : switch (code)
2912 : {
2913 : case LT_EXPR:
2914 : return COMPCODE_LT;
2915 : case EQ_EXPR:
2916 : return COMPCODE_EQ;
2917 : case LE_EXPR:
2918 : return COMPCODE_LE;
2919 : case GT_EXPR:
2920 : return COMPCODE_GT;
2921 : case NE_EXPR:
2922 : return COMPCODE_NE;
2923 : case GE_EXPR:
2924 : return COMPCODE_GE;
2925 : case ORDERED_EXPR:
2926 : return COMPCODE_ORD;
2927 : case UNORDERED_EXPR:
2928 : return COMPCODE_UNORD;
2929 : case UNLT_EXPR:
2930 : return COMPCODE_UNLT;
2931 : case UNEQ_EXPR:
2932 : return COMPCODE_UNEQ;
2933 : case UNLE_EXPR:
2934 : return COMPCODE_UNLE;
2935 : case UNGT_EXPR:
2936 : return COMPCODE_UNGT;
2937 : case LTGT_EXPR:
2938 : return COMPCODE_LTGT;
2939 : case UNGE_EXPR:
2940 : return COMPCODE_UNGE;
2941 0 : default:
2942 0 : gcc_unreachable ();
2943 : }
2944 : }
2945 :
2946 : /* Convert a compcode bit-based encoding of a comparison operator back
2947 : to GCC's enum tree_code representation. This function is the
2948 : inverse of comparison_to_compcode. */
2949 :
2950 : static enum tree_code
2951 21490 : compcode_to_comparison (enum comparison_code code)
2952 : {
2953 21490 : switch (code)
2954 : {
2955 : case COMPCODE_LT:
2956 : return LT_EXPR;
2957 : case COMPCODE_EQ:
2958 : return EQ_EXPR;
2959 : case COMPCODE_LE:
2960 : return LE_EXPR;
2961 : case COMPCODE_GT:
2962 : return GT_EXPR;
2963 : case COMPCODE_NE:
2964 : return NE_EXPR;
2965 : case COMPCODE_GE:
2966 : return GE_EXPR;
2967 : case COMPCODE_ORD:
2968 : return ORDERED_EXPR;
2969 : case COMPCODE_UNORD:
2970 : return UNORDERED_EXPR;
2971 : case COMPCODE_UNLT:
2972 : return UNLT_EXPR;
2973 : case COMPCODE_UNEQ:
2974 : return UNEQ_EXPR;
2975 : case COMPCODE_UNLE:
2976 : return UNLE_EXPR;
2977 : case COMPCODE_UNGT:
2978 : return UNGT_EXPR;
2979 : case COMPCODE_LTGT:
2980 : return LTGT_EXPR;
2981 : case COMPCODE_UNGE:
2982 : return UNGE_EXPR;
2983 0 : default:
2984 0 : gcc_unreachable ();
2985 : }
2986 : }
2987 :
2988 : /* Return true if COND1 tests the opposite condition of COND2. */
2989 :
2990 : bool
2991 1802402 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2992 : {
2993 1802402 : return (COMPARISON_CLASS_P (cond1)
2994 1708969 : && COMPARISON_CLASS_P (cond2)
2995 1697557 : && (invert_tree_comparison
2996 1697557 : (TREE_CODE (cond1),
2997 3395114 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2998 70772 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2999 70772 : TREE_OPERAND (cond2, 0), 0)
3000 1824938 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3001 22536 : TREE_OPERAND (cond2, 1), 0));
3002 : }
3003 :
3004 : /* Return a code for the comparison which is the combination of
3005 : doing the AND or OR (depending on CODE) of the two operations LCODE
3006 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3007 : the possibility of trapping if the mode has NaNs, and return ERROR_MARK
3008 : if this makes the transformation invalid. If the resulting code is
3009 : INTEGER_CST, then *RES will be set to a non-NULL CONSTANT. */
3010 :
3011 : enum tree_code
3012 48966 : combine_comparisons (enum tree_code code, enum tree_code lcode,
3013 : enum tree_code rcode, tree truth_type,
3014 : bool honor_nans, tree *res)
3015 : {
3016 48966 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3017 48966 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3018 48966 : int compcode;
3019 48966 : *res = NULL_TREE;
3020 :
3021 48966 : switch (code)
3022 : {
3023 30346 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3024 30346 : case BIT_AND_EXPR:
3025 30346 : compcode = lcompcode & rcompcode;
3026 30346 : break;
3027 :
3028 17305 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3029 17305 : case BIT_IOR_EXPR:
3030 17305 : compcode = lcompcode | rcompcode;
3031 17305 : break;
3032 :
3033 960 : case BIT_XOR_EXPR:
3034 960 : case NE_EXPR:
3035 960 : compcode = lcompcode ^ rcompcode;
3036 960 : break;
3037 :
3038 332 : case EQ_EXPR:
3039 332 : compcode = ~(lcompcode ^ rcompcode);
3040 332 : break;
3041 :
3042 : //`bool0 < bool1` is `!bool0 & bool1`
3043 23 : case LT_EXPR:
3044 23 : compcode = ~lcompcode & rcompcode;
3045 23 : break;
3046 :
3047 : //`bool0 > bool1` is `bool0 & !bool1`
3048 0 : case GT_EXPR:
3049 0 : compcode = lcompcode & ~rcompcode;
3050 0 : break;
3051 :
3052 : // `bool0 <= bool1` as !bool0 | bool1
3053 0 : case LE_EXPR:
3054 0 : compcode = ~lcompcode | rcompcode;
3055 0 : break;
3056 :
3057 : //`bool0 >= bool1` is `bool0 | !bool1`
3058 0 : case GE_EXPR:
3059 0 : compcode = lcompcode | ~rcompcode;
3060 0 : break;
3061 :
3062 : default:
3063 : return ERROR_MARK;
3064 : }
3065 :
3066 48966 : if (!honor_nans)
3067 : {
3068 : /* Eliminate unordered comparisons, as well as LTGT and ORD
3069 : which are not used unless the mode has NaNs. */
3070 32843 : compcode &= ~COMPCODE_UNORD;
3071 32843 : if (compcode == COMPCODE_LTGT)
3072 : compcode = COMPCODE_NE;
3073 30694 : else if (compcode == COMPCODE_ORD)
3074 3634 : compcode = COMPCODE_TRUE;
3075 : }
3076 16123 : else if (flag_trapping_math)
3077 : {
3078 : /* Check that the original operation and the optimized ones will trap
3079 : under the same condition. */
3080 15225 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3081 14034 : && (lcompcode != COMPCODE_EQ)
3082 15225 : && (lcompcode != COMPCODE_ORD);
3083 15225 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3084 14006 : && (rcompcode != COMPCODE_EQ)
3085 15225 : && (rcompcode != COMPCODE_ORD);
3086 30450 : bool trap = (compcode & COMPCODE_UNORD) == 0
3087 14405 : && (compcode != COMPCODE_EQ)
3088 14405 : && (compcode != COMPCODE_ORD)
3089 1649 : && (compcode != COMPCODE_FALSE)
3090 15225 : && (compcode != COMPCODE_TRUE);
3091 :
3092 : /* In a short-circuited boolean expression the LHS might be
3093 : such that the RHS, if evaluated, will never trap. For
3094 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3095 : if neither x nor y is NaN. (This is a mixed blessing: for
3096 : example, the expression above will never trap, hence
3097 : optimizing it to x < y would be invalid). */
3098 710 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3099 15460 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3100 : rtrap = false;
3101 :
3102 : /* Allow combining of `a != b && a < b` since NAN will cause != to be
3103 : always true, and `a < b` will cause a trap. This is trap neutral. */
3104 15225 : if (code == TRUTH_ANDIF_EXPR && lcompcode == COMPCODE_NE && rtrap && trap)
3105 : ;
3106 : /* Likewise of `a == b || a < b` for the same reason. */
3107 15128 : else if (code == TRUTH_ORIF_EXPR && lcompcode == COMPCODE_EQ && rtrap && trap)
3108 : ;
3109 : /* If the comparison was short-circuited, and only the RHS
3110 : trapped, we may now generate a spurious trap. */
3111 15060 : else if (rtrap && !ltrap
3112 10 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3113 : return ERROR_MARK;
3114 :
3115 : /* If we changed the conditions that cause a trap, we lose. */
3116 15060 : else if ((ltrap || rtrap) != trap)
3117 : return ERROR_MARK;
3118 : }
3119 :
3120 35179 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3121 : {
3122 13689 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3123 13689 : return INTEGER_CST;
3124 : }
3125 : else
3126 21490 : return compcode_to_comparison ((enum comparison_code) compcode);
3127 : }
3128 :
3129 : /* Return a tree for the comparison which is the combination of
3130 : doing the AND or OR (depending on CODE) of the two operations LCODE
3131 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3132 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3133 : if this makes the transformation invalid. */
3134 :
3135 : tree
3136 32509 : combine_comparisons (location_t loc,
3137 : enum tree_code code, enum tree_code lcode,
3138 : enum tree_code rcode, tree truth_type,
3139 : tree ll_arg, tree lr_arg)
3140 : {
3141 32509 : bool honor_nans = HONOR_NANS (ll_arg);
3142 32509 : tree_code rescode;
3143 32509 : tree res;
3144 32509 : rescode = combine_comparisons (code, lcode, rcode, truth_type,
3145 : honor_nans, &res);
3146 32509 : if (rescode == ERROR_MARK)
3147 : return NULL_TREE;
3148 30472 : if (rescode == INTEGER_CST)
3149 13194 : return res;
3150 :
3151 17278 : return fold_build2_loc (loc, rescode, truth_type, ll_arg, lr_arg);
3152 : }
3153 :
3154 : /* Return nonzero if two operands (typically of the same tree node)
3155 : are necessarily equal. FLAGS modifies behavior as follows:
3156 :
3157 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3158 : This function tests whether the operands are indistinguishable;
3159 : it does not test whether they are equal using C's == operation.
3160 : The distinction is important for IEEE floating point, because
3161 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3162 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3163 :
3164 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3165 : even though it may hold multiple values during a function.
3166 : This is because a GCC tree node guarantees that nothing else is
3167 : executed between the evaluation of its "operands" (which may often
3168 : be evaluated in arbitrary order). Hence if the operands themselves
3169 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3170 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3171 : unset means assuming isochronic (or instantaneous) tree equivalence.
3172 : Unless comparing arbitrary expression trees, such as from different
3173 : statements, this flag can usually be left unset.
3174 :
3175 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3176 : are considered the same. It is used when the caller has other ways
3177 : to ensure that global memory is unchanged in between.
3178 :
3179 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3180 : not values of expressions.
3181 :
3182 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3183 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3184 :
3185 : If OEP_BITWISE is set, then require the values to be bitwise identical
3186 : rather than simply numerically equal. Do not take advantage of things
3187 : like math-related flags or undefined behavior; only return true for
3188 : values that are provably bitwise identical in all circumstances.
3189 :
3190 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3191 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3192 : rather than just numerically equivalent. The compared expressions must
3193 : however perform the same operations but may do intermediate computations in
3194 : differing signs. Because this comparison ignores any possible UB it cannot
3195 : be used blindly without ensuring that the context you are using it in itself
3196 : doesn't guarantee that there will be no UB. Conditional expressions are
3197 : excluded from this relaxation.
3198 :
3199 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3200 : differing hashes even for cases where operand_compare::operand_equal_p
3201 : compares equal.
3202 :
3203 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3204 : any operand with side effect. This is unnecessarily conservative in the
3205 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3206 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3207 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3208 : even if var is volatile. */
3209 :
3210 : bool
3211 7355556200 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3212 : unsigned int flags)
3213 : {
3214 7355556200 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3215 : }
3216 :
3217 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3218 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3219 : expression is being compared under for equality. This means that they can
3220 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3221 :
3222 : bool
3223 7356298989 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3224 : tree type1, const_tree arg1,
3225 : unsigned int flags)
3226 : {
3227 7356298989 : bool r;
3228 7356298989 : if (verify_hash_value (arg0, arg1, flags, &r))
3229 3097689599 : return r;
3230 :
3231 4258609390 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3232 4258609390 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3233 :
3234 : /* If either is ERROR_MARK, they aren't equal. */
3235 4258609390 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3236 4258608728 : || type0 == error_mark_node
3237 4258608726 : || type1 == error_mark_node)
3238 : return false;
3239 :
3240 : /* Similar, if either does not have a type (like a template id),
3241 : they aren't equal. */
3242 4258608725 : if (!type0 || !type1)
3243 : return false;
3244 :
3245 : /* Bitwise identity makes no sense if the values have different layouts. */
3246 4258603571 : if ((flags & OEP_BITWISE)
3247 4258603571 : && !tree_nop_conversion_p (type0, type1))
3248 : return false;
3249 :
3250 : /* We cannot consider pointers to different address space equal. */
3251 4258603571 : if (POINTER_TYPE_P (type0)
3252 664927501 : && POINTER_TYPE_P (type1)
3253 4829832828 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3254 571229257 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3255 : return false;
3256 :
3257 : /* Check equality of integer constants before bailing out due to
3258 : precision differences. */
3259 4258603324 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3260 : {
3261 : /* Address of INTEGER_CST is not defined; check that we did not forget
3262 : to drop the OEP_ADDRESS_OF flags. */
3263 672757564 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3264 672757564 : return tree_int_cst_equal (arg0, arg1);
3265 : }
3266 :
3267 3585845760 : if ((flags & OEP_ASSUME_WRAPV)
3268 2104054 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3269 : {
3270 794554 : const_tree t_arg0 = arg0;
3271 794554 : const_tree t_arg1 = arg1;
3272 794554 : STRIP_NOPS (arg0);
3273 794554 : STRIP_NOPS (arg1);
3274 : /* Only recurse if the conversion was one that was valid to strip. */
3275 794554 : if (t_arg0 != arg0 || t_arg1 != arg1)
3276 742789 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3277 : }
3278 :
3279 3585102971 : if (!(flags & OEP_ADDRESS_OF))
3280 : {
3281 : /* Check if we are checking an operation where the two's compliment
3282 : bitwise representation of the result is not the same between signed and
3283 : unsigned arithmetic. */
3284 3177583412 : bool enforce_signedness = true;
3285 3177583412 : if (flags & OEP_ASSUME_WRAPV)
3286 : {
3287 1265772 : switch (TREE_CODE (arg0))
3288 : {
3289 : case PLUS_EXPR:
3290 : case MINUS_EXPR:
3291 : case MULT_EXPR:
3292 : case BIT_IOR_EXPR:
3293 : case BIT_XOR_EXPR:
3294 : case BIT_AND_EXPR:
3295 : case BIT_NOT_EXPR:
3296 : case ABS_EXPR:
3297 : CASE_CONVERT:
3298 : case SSA_NAME:
3299 : case INTEGER_CST:
3300 : case VAR_DECL:
3301 : case PARM_DECL:
3302 : case RESULT_DECL:
3303 3177583412 : enforce_signedness = false;
3304 : break;
3305 :
3306 : default:
3307 : break;
3308 : }
3309 : }
3310 :
3311 : /* If both types don't have the same signedness, then we can't consider
3312 : them equal. We must check this before the STRIP_NOPS calls
3313 : because they may change the signedness of the arguments. As pointers
3314 : strictly don't have a signedness, require either two pointers or
3315 : two non-pointers as well. */
3316 3177583412 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3317 3177583412 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3318 146585320 : && enforce_signedness))
3319 : return false;
3320 :
3321 : /* If both types don't have the same precision, then it is not safe
3322 : to strip NOPs. */
3323 2873376128 : if (element_precision (type0) != element_precision (type1))
3324 : return false;
3325 :
3326 2718852539 : STRIP_NOPS (arg0);
3327 2718852539 : STRIP_NOPS (arg1);
3328 :
3329 2718852539 : type0 = TREE_TYPE (arg0);
3330 2718852539 : type1 = TREE_TYPE (arg1);
3331 : }
3332 : #if 0
3333 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3334 : sanity check once the issue is solved. */
3335 : else
3336 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3337 : are not defined. Check that we did not forget to drop the
3338 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3339 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3340 : && TREE_CODE (arg0) != SSA_NAME);
3341 : #endif
3342 :
3343 : /* In case both args are comparisons but with different comparison
3344 : code, try to swap the comparison operands of one arg to produce
3345 : a match and compare that variant. */
3346 3126372098 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3347 1258456969 : && COMPARISON_CLASS_P (arg0)
3348 6855641 : && COMPARISON_CLASS_P (arg1))
3349 : {
3350 5134475 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3351 :
3352 5134475 : if (TREE_CODE (arg0) == swap_code)
3353 2182710 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3354 2182710 : TREE_OPERAND (arg1, 1), flags)
3355 2202296 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3356 19586 : TREE_OPERAND (arg1, 0), flags);
3357 : }
3358 :
3359 3124189388 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3360 : {
3361 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3362 1256274259 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3363 : ;
3364 1256209407 : else if (flags & OEP_ADDRESS_OF)
3365 : {
3366 : /* If we are interested in comparing addresses ignore
3367 : MEM_REF wrappings of the base that can appear just for
3368 : TBAA reasons. */
3369 49999332 : if (TREE_CODE (arg0) == MEM_REF
3370 8238855 : && DECL_P (arg1)
3371 5821866 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3372 1361580 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3373 50723814 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3374 : return true;
3375 49773597 : else if (TREE_CODE (arg1) == MEM_REF
3376 31078639 : && DECL_P (arg0)
3377 11402037 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3378 2386497 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3379 50377314 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3380 : return true;
3381 : return false;
3382 : }
3383 : else
3384 : return false;
3385 : }
3386 :
3387 : /* When not checking addresses, this is needed for conversions and for
3388 : COMPONENT_REF. Might as well play it safe and always test this. */
3389 1867979981 : if (TREE_CODE (type0) == ERROR_MARK
3390 1867979981 : || TREE_CODE (type1) == ERROR_MARK
3391 3735959962 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3392 26505192 : && !(flags & OEP_ADDRESS_OF)))
3393 : return false;
3394 :
3395 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3396 : We don't care about side effects in that case because the SAVE_EXPR
3397 : takes care of that for us. In all other cases, two expressions are
3398 : equal if they have no side effects. If we have two identical
3399 : expressions with side effects that should be treated the same due
3400 : to the only side effects being identical SAVE_EXPR's, that will
3401 : be detected in the recursive calls below.
3402 : If we are taking an invariant address of two identical objects
3403 : they are necessarily equal as well. */
3404 334324392 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3405 2198369130 : && (TREE_CODE (arg0) == SAVE_EXPR
3406 334298747 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3407 294165546 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3408 : return true;
3409 :
3410 : /* Next handle constant cases, those for which we can return 1 even
3411 : if ONLY_CONST is set. */
3412 1529876977 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3413 26364179 : switch (TREE_CODE (arg0))
3414 : {
3415 151 : case INTEGER_CST:
3416 151 : return tree_int_cst_equal (arg0, arg1);
3417 :
3418 0 : case FIXED_CST:
3419 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3420 : TREE_FIXED_CST (arg1));
3421 :
3422 3802199 : case REAL_CST:
3423 3802199 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3424 : return true;
3425 :
3426 2761075 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3427 : {
3428 : /* If we do not distinguish between signed and unsigned zero,
3429 : consider them equal. */
3430 15303 : if (real_zerop (arg0) && real_zerop (arg1))
3431 : return true;
3432 : }
3433 : return false;
3434 :
3435 1021752 : case VECTOR_CST:
3436 1021752 : {
3437 1021752 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3438 1021752 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3439 : return false;
3440 :
3441 999950 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3442 999950 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3443 : return false;
3444 :
3445 964442 : unsigned int count = vector_cst_encoded_nelts (arg0);
3446 2312955 : for (unsigned int i = 0; i < count; ++i)
3447 2199396 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3448 1099698 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3449 : return false;
3450 : return true;
3451 : }
3452 :
3453 14151 : case COMPLEX_CST:
3454 14151 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3455 : flags)
3456 14151 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3457 : flags));
3458 :
3459 1004375 : case STRING_CST:
3460 1004375 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3461 1004375 : && ! memcmp (TREE_STRING_POINTER (arg0),
3462 569097 : TREE_STRING_POINTER (arg1),
3463 569097 : TREE_STRING_LENGTH (arg0)));
3464 :
3465 0 : case RAW_DATA_CST:
3466 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3467 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3468 0 : RAW_DATA_POINTER (arg1),
3469 0 : RAW_DATA_LENGTH (arg0)));
3470 :
3471 19368375 : case ADDR_EXPR:
3472 19368375 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3473 19368375 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3474 : flags | OEP_ADDRESS_OF
3475 19368375 : | OEP_MATCH_SIDE_EFFECTS);
3476 184219 : case CONSTRUCTOR:
3477 184219 : {
3478 : /* In GIMPLE empty constructors are allowed in initializers of
3479 : aggregates. */
3480 184219 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3481 : return true;
3482 :
3483 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3484 138145 : if (TREE_CODE (type0) != TREE_CODE (type1))
3485 : return false;
3486 138145 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3487 : {
3488 : /* For arrays, check that the sizes all match. */
3489 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3490 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3491 264 : || siz0 < 0
3492 528 : || siz0 != int_size_in_bytes (type1))
3493 : return false;
3494 : }
3495 137881 : else if (!types_compatible_p (type0, type1))
3496 : return false;
3497 :
3498 138145 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3499 138145 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3500 414435 : if (vec_safe_length (v0) != vec_safe_length (v1))
3501 : return false;
3502 :
3503 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3504 : of the CONSTRUCTOR referenced indirectly. */
3505 138145 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3506 :
3507 370854953 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3508 : {
3509 206349 : constructor_elt *c0 = &(*v0)[idx];
3510 206349 : constructor_elt *c1 = &(*v1)[idx];
3511 :
3512 : /* Check that the values are the same... */
3513 206349 : if (c0->value != c1->value
3514 206349 : && !operand_equal_p (c0->value, c1->value, flags))
3515 : return false;
3516 :
3517 : /* ... and that they apply to the same field! */
3518 117785 : if (c0->index != c1->index
3519 117785 : && (TREE_CODE (type0) == ARRAY_TYPE
3520 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3521 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3522 0 : DECL_FIELD_OFFSET (c1->index),
3523 : flags)
3524 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3525 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3526 : flags)))
3527 : return false;
3528 : }
3529 :
3530 : return true;
3531 : }
3532 :
3533 : default:
3534 : break;
3535 : }
3536 :
3537 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3538 : two instances of undefined behavior will give identical results. */
3539 1504481755 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3540 : return false;
3541 :
3542 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3543 : variant that allows null and views null as being different from any
3544 : non-null value. In the latter case, if either is null, the both
3545 : must be; otherwise, do the normal comparison. */
3546 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3547 : TREE_OPERAND (arg1, N), flags)
3548 :
3549 : #define OP_SAME_WITH_NULL(N) \
3550 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3551 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3552 :
3553 1504481755 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3554 : {
3555 7768760 : case tcc_unary:
3556 : /* Two conversions are equal only if signedness and modes match. */
3557 7768760 : switch (TREE_CODE (arg0))
3558 : {
3559 7397907 : CASE_CONVERT:
3560 7397907 : case FIX_TRUNC_EXPR:
3561 7397907 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3562 : return false;
3563 : break;
3564 : default:
3565 : break;
3566 : }
3567 :
3568 7768739 : return OP_SAME_WITH_NULL (0);
3569 :
3570 :
3571 23125903 : case tcc_comparison:
3572 23125903 : case tcc_binary:
3573 23125903 : if (OP_SAME (0) && OP_SAME (1))
3574 : return true;
3575 :
3576 : /* For commutative ops, allow the other order. */
3577 17058209 : return (commutative_tree_code (TREE_CODE (arg0))
3578 12949692 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3579 12949692 : TREE_OPERAND (arg1, 1), flags)
3580 17292105 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3581 233896 : TREE_OPERAND (arg1, 0), flags));
3582 :
3583 880725970 : case tcc_reference:
3584 : /* If either of the pointer (or reference) expressions we are
3585 : dereferencing contain a side effect, these cannot be equal,
3586 : but their addresses can be. */
3587 880725970 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3588 880725970 : && (TREE_SIDE_EFFECTS (arg0)
3589 812113776 : || TREE_SIDE_EFFECTS (arg1)))
3590 : return false;
3591 :
3592 880194345 : switch (TREE_CODE (arg0))
3593 : {
3594 4783416 : case INDIRECT_REF:
3595 4783416 : if (!(flags & OEP_ADDRESS_OF))
3596 : {
3597 4760775 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3598 : return false;
3599 : /* Verify that the access types are compatible. */
3600 4754462 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3601 : return false;
3602 : }
3603 4710794 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3604 4710794 : return OP_SAME (0);
3605 :
3606 575458 : case IMAGPART_EXPR:
3607 : /* Require the same offset. */
3608 575458 : if (!operand_equal_p (TYPE_SIZE (type0),
3609 575458 : TYPE_SIZE (type1),
3610 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3611 : return false;
3612 :
3613 : /* Fallthru. */
3614 2312455 : case REALPART_EXPR:
3615 2312455 : case VIEW_CONVERT_EXPR:
3616 2312455 : return OP_SAME (0);
3617 :
3618 87793152 : case TARGET_MEM_REF:
3619 87793152 : case MEM_REF:
3620 87793152 : if (!(flags & OEP_ADDRESS_OF))
3621 : {
3622 : /* Require equal access sizes */
3623 17464084 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3624 17464084 : && (!TYPE_SIZE (type0)
3625 1326806 : || !TYPE_SIZE (type1)
3626 1320020 : || !operand_equal_p (TYPE_SIZE (type0),
3627 1320020 : TYPE_SIZE (type1),
3628 : flags)))
3629 : return false;
3630 : /* Verify that access happens in similar types. */
3631 16132988 : if (!types_compatible_p (type0, type1))
3632 : return false;
3633 : /* Verify that accesses are TBAA compatible. */
3634 15786909 : if (!alias_ptr_types_compatible_p
3635 15786909 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3636 15786909 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3637 14898407 : || (MR_DEPENDENCE_CLIQUE (arg0)
3638 14898407 : != MR_DEPENDENCE_CLIQUE (arg1))
3639 28855523 : || (MR_DEPENDENCE_BASE (arg0)
3640 13068614 : != MR_DEPENDENCE_BASE (arg1)))
3641 : return false;
3642 : /* Verify that alignment is compatible. */
3643 12536429 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3644 : return false;
3645 : }
3646 82648939 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3647 141337740 : return (OP_SAME (0) && OP_SAME (1)
3648 : /* TARGET_MEM_REF require equal extra operands. */
3649 107926940 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3650 541751 : || (OP_SAME_WITH_NULL (2)
3651 282359 : && OP_SAME_WITH_NULL (3)
3652 276821 : && OP_SAME_WITH_NULL (4))));
3653 :
3654 36024437 : case ARRAY_REF:
3655 36024437 : case ARRAY_RANGE_REF:
3656 36024437 : if (!OP_SAME (0))
3657 : return false;
3658 31234801 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3659 : /* Compare the array index by value if it is constant first as we
3660 : may have different types but same value here. */
3661 31234801 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3662 31234801 : TREE_OPERAND (arg1, 1))
3663 28153044 : || OP_SAME (1))
3664 6193604 : && OP_SAME_WITH_NULL (2)
3665 6192284 : && OP_SAME_WITH_NULL (3)
3666 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3667 : we have to account for the offset of the ref. */
3668 40523887 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3669 3096142 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3670 2529 : || (operand_equal_p (array_ref_low_bound
3671 2529 : (const_cast<tree> (arg0)),
3672 : array_ref_low_bound
3673 2529 : (const_cast<tree> (arg1)),
3674 : flags)
3675 2529 : && operand_equal_p (array_ref_element_size
3676 2529 : (const_cast<tree> (arg0)),
3677 : array_ref_element_size
3678 2529 : (const_cast<tree> (arg1)),
3679 : flags))));
3680 :
3681 748644086 : case COMPONENT_REF:
3682 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3683 : may be NULL when we're called to compare MEM_EXPRs. */
3684 748644086 : if (!OP_SAME_WITH_NULL (0))
3685 : return false;
3686 59306817 : {
3687 59306817 : bool compare_address = flags & OEP_ADDRESS_OF;
3688 :
3689 : /* Most of time we only need to compare FIELD_DECLs for equality.
3690 : However when determining address look into actual offsets.
3691 : These may match for unions and unshared record types. */
3692 59306817 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3693 59306817 : if (!OP_SAME (1))
3694 : {
3695 34224228 : if (compare_address
3696 656541 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3697 : {
3698 653890 : tree field0 = TREE_OPERAND (arg0, 1);
3699 653890 : tree field1 = TREE_OPERAND (arg1, 1);
3700 :
3701 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3702 653890 : if (TREE_CODE (field0) != FIELD_DECL
3703 653890 : || TREE_CODE (field1) != FIELD_DECL)
3704 : return false;
3705 :
3706 653890 : if (!DECL_FIELD_OFFSET (field0)
3707 653890 : || !DECL_FIELD_OFFSET (field1))
3708 3 : return field0 == field1;
3709 :
3710 653887 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3711 653887 : DECL_FIELD_OFFSET (field1), flags)
3712 848380 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3713 194493 : DECL_FIELD_BIT_OFFSET (field1),
3714 : flags))
3715 : return false;
3716 : }
3717 : else
3718 : return false;
3719 : }
3720 : }
3721 25116625 : return OP_SAME_WITH_NULL (2);
3722 :
3723 636607 : case BIT_FIELD_REF:
3724 636607 : if (!OP_SAME (0))
3725 : return false;
3726 370173 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3727 370173 : return OP_SAME (1) && OP_SAME (2);
3728 :
3729 : default:
3730 : return false;
3731 : }
3732 :
3733 60173743 : case tcc_expression:
3734 60173743 : switch (TREE_CODE (arg0))
3735 : {
3736 54771598 : case ADDR_EXPR:
3737 : /* Be sure we pass right ADDRESS_OF flag. */
3738 54771598 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3739 54771598 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3740 54771598 : TREE_OPERAND (arg1, 0),
3741 54771598 : flags | OEP_ADDRESS_OF);
3742 :
3743 616172 : case TRUTH_NOT_EXPR:
3744 616172 : return OP_SAME (0);
3745 :
3746 80155 : case TRUTH_ANDIF_EXPR:
3747 80155 : case TRUTH_ORIF_EXPR:
3748 80155 : return OP_SAME (0) && OP_SAME (1);
3749 :
3750 0 : case WIDEN_MULT_PLUS_EXPR:
3751 0 : case WIDEN_MULT_MINUS_EXPR:
3752 0 : if (!OP_SAME (2))
3753 : return false;
3754 : /* The multiplication operands are commutative. */
3755 : /* FALLTHRU */
3756 :
3757 48100 : case TRUTH_AND_EXPR:
3758 48100 : case TRUTH_OR_EXPR:
3759 48100 : case TRUTH_XOR_EXPR:
3760 48100 : if (OP_SAME (0) && OP_SAME (1))
3761 : return true;
3762 :
3763 : /* Otherwise take into account this is a commutative operation. */
3764 48082 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3765 48082 : TREE_OPERAND (arg1, 1), flags)
3766 48085 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3767 3 : TREE_OPERAND (arg1, 0), flags));
3768 :
3769 217261 : case COND_EXPR:
3770 217261 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3771 : return false;
3772 171265 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3773 171265 : return OP_SAME (0);
3774 :
3775 4 : case BIT_INSERT_EXPR:
3776 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3777 : of op1. Need to check to make sure they are the same. */
3778 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3779 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3780 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3781 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3782 : return false;
3783 : /* FALLTHRU */
3784 :
3785 371 : case VEC_COND_EXPR:
3786 371 : case DOT_PROD_EXPR:
3787 371 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3788 :
3789 38678 : case MODIFY_EXPR:
3790 38678 : case INIT_EXPR:
3791 38678 : case COMPOUND_EXPR:
3792 38678 : case PREDECREMENT_EXPR:
3793 38678 : case PREINCREMENT_EXPR:
3794 38678 : case POSTDECREMENT_EXPR:
3795 38678 : case POSTINCREMENT_EXPR:
3796 38678 : if (flags & OEP_LEXICOGRAPHIC)
3797 165 : return OP_SAME (0) && OP_SAME (1);
3798 : return false;
3799 :
3800 320152 : case CLEANUP_POINT_EXPR:
3801 320152 : case EXPR_STMT:
3802 320152 : case SAVE_EXPR:
3803 320152 : if (flags & OEP_LEXICOGRAPHIC)
3804 208 : return OP_SAME (0);
3805 : return false;
3806 :
3807 82394 : case OBJ_TYPE_REF:
3808 : /* Virtual table reference. */
3809 164788 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3810 82394 : OBJ_TYPE_REF_EXPR (arg1), flags))
3811 : return false;
3812 15852 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3813 15852 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3814 15852 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3815 : return false;
3816 15852 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3817 15852 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3818 : return false;
3819 15852 : if (virtual_method_call_p (arg0))
3820 : {
3821 15852 : if (!virtual_method_call_p (arg1))
3822 : return false;
3823 15852 : return types_same_for_odr (obj_type_ref_class (arg0),
3824 31704 : obj_type_ref_class (arg1));
3825 : }
3826 : return false;
3827 :
3828 661 : case OMP_ARRAY_SECTION:
3829 661 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3830 :
3831 : default:
3832 : return false;
3833 : }
3834 :
3835 3792539 : case tcc_vl_exp:
3836 3792539 : switch (TREE_CODE (arg0))
3837 : {
3838 3792539 : case CALL_EXPR:
3839 3792539 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3840 3792539 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3841 : /* If not both CALL_EXPRs are either internal or normal function
3842 : functions, then they are not equal. */
3843 : return false;
3844 3792539 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3845 : {
3846 : /* If the CALL_EXPRs call different internal functions, then they
3847 : are not equal. */
3848 14 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3849 : return false;
3850 : }
3851 : else
3852 : {
3853 : /* If the CALL_EXPRs call different functions, then they are not
3854 : equal. */
3855 3792525 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3856 : flags))
3857 : return false;
3858 : }
3859 :
3860 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3861 2208125 : {
3862 2208125 : unsigned int cef = call_expr_flags (arg0);
3863 2208125 : if (flags & OEP_PURE_SAME)
3864 0 : cef &= ECF_CONST | ECF_PURE;
3865 : else
3866 2208125 : cef &= ECF_CONST;
3867 2208125 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3868 : return false;
3869 : }
3870 :
3871 : /* Now see if all the arguments are the same. */
3872 34952 : {
3873 34952 : const_call_expr_arg_iterator iter0, iter1;
3874 34952 : const_tree a0, a1;
3875 69904 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3876 34952 : a1 = first_const_call_expr_arg (arg1, &iter1);
3877 43085 : a0 && a1;
3878 8133 : a0 = next_const_call_expr_arg (&iter0),
3879 8133 : a1 = next_const_call_expr_arg (&iter1))
3880 36451 : if (! operand_equal_p (a0, a1, flags))
3881 : return false;
3882 :
3883 : /* If we get here and both argument lists are exhausted
3884 : then the CALL_EXPRs are equal. */
3885 6634 : return ! (a0 || a1);
3886 : }
3887 : default:
3888 : return false;
3889 : }
3890 :
3891 172095647 : case tcc_declaration:
3892 : /* Consider __builtin_sqrt equal to sqrt. */
3893 172095647 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3894 7208530 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3895 270289 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3896 6592469 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3897 270289 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3898 :
3899 165503178 : if (DECL_P (arg0)
3900 165503178 : && (flags & OEP_DECL_NAME)
3901 35 : && (flags & OEP_LEXICOGRAPHIC))
3902 : {
3903 : /* Consider decls with the same name equal. The caller needs
3904 : to make sure they refer to the same entity (such as a function
3905 : formal parameter). */
3906 35 : tree a0name = DECL_NAME (arg0);
3907 35 : tree a1name = DECL_NAME (arg1);
3908 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3909 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3910 35 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3911 : }
3912 : return false;
3913 :
3914 354146126 : case tcc_exceptional:
3915 354146126 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3916 : {
3917 19834 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3918 : return false;
3919 :
3920 : /* In GIMPLE constructors are used only to build vectors from
3921 : elements. Individual elements in the constructor must be
3922 : indexed in increasing order and form an initial sequence.
3923 :
3924 : We make no effort to compare nonconstant ones in GENERIC. */
3925 19834 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3926 : return false;
3927 :
3928 : /* Be sure that vectors constructed have the same representation.
3929 : We only tested element precision and modes to match.
3930 : Vectors may be BLKmode and thus also check that the number of
3931 : parts match. */
3932 1079 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3933 2158 : TYPE_VECTOR_SUBPARTS (type1)))
3934 : return false;
3935 :
3936 1079 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3937 1079 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3938 1079 : unsigned int len = vec_safe_length (v0);
3939 :
3940 2158 : if (len != vec_safe_length (v1))
3941 : return false;
3942 :
3943 4665 : for (unsigned int i = 0; i < len; i++)
3944 : {
3945 3991 : constructor_elt *c0 = &(*v0)[i];
3946 3991 : constructor_elt *c1 = &(*v1)[i];
3947 :
3948 3991 : if (!operand_equal_p (c0->value, c1->value, flags)
3949 : /* In GIMPLE the indexes can be either NULL or matching i.
3950 : Double check this so we won't get false
3951 : positives for GENERIC. */
3952 3616 : || (c0->index
3953 2714 : && (TREE_CODE (c0->index) != INTEGER_CST
3954 2714 : || compare_tree_int (c0->index, i)))
3955 7607 : || (c1->index
3956 2714 : && (TREE_CODE (c1->index) != INTEGER_CST
3957 2714 : || compare_tree_int (c1->index, i))))
3958 : return false;
3959 : }
3960 : return true;
3961 : }
3962 354126292 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3963 3309 : && (flags & OEP_LEXICOGRAPHIC))
3964 : {
3965 : /* Compare the STATEMENT_LISTs. */
3966 16 : tree_stmt_iterator tsi1, tsi2;
3967 16 : tree body1 = const_cast<tree> (arg0);
3968 16 : tree body2 = const_cast<tree> (arg1);
3969 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3970 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3971 : {
3972 : /* The lists don't have the same number of statements. */
3973 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3974 : return false;
3975 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3976 : return true;
3977 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3978 : flags & (OEP_LEXICOGRAPHIC
3979 : | OEP_NO_HASH_CHECK)))
3980 : return false;
3981 : }
3982 : }
3983 : return false;
3984 :
3985 2652869 : case tcc_statement:
3986 2652869 : switch (TREE_CODE (arg0))
3987 : {
3988 52 : case RETURN_EXPR:
3989 52 : if (flags & OEP_LEXICOGRAPHIC)
3990 52 : return OP_SAME_WITH_NULL (0);
3991 : return false;
3992 4 : case DEBUG_BEGIN_STMT:
3993 4 : if (flags & OEP_LEXICOGRAPHIC)
3994 : return true;
3995 : return false;
3996 : default:
3997 : return false;
3998 : }
3999 :
4000 : default:
4001 : return false;
4002 : }
4003 :
4004 : #undef OP_SAME
4005 : #undef OP_SAME_WITH_NULL
4006 : }
4007 :
4008 : /* Generate a hash value for an expression. This can be used iteratively
4009 : by passing a previous result as the HSTATE argument. */
4010 :
4011 : void
4012 3091753277 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
4013 : unsigned int flags)
4014 : {
4015 3091753277 : int i;
4016 3091753277 : enum tree_code code;
4017 3091753277 : enum tree_code_class tclass;
4018 :
4019 3091753277 : if (t == NULL_TREE || t == error_mark_node)
4020 : {
4021 78774808 : hstate.merge_hash (0);
4022 78774808 : return;
4023 : }
4024 :
4025 3012978469 : STRIP_ANY_LOCATION_WRAPPER (t);
4026 :
4027 3012978469 : if (!(flags & OEP_ADDRESS_OF))
4028 2754842243 : STRIP_NOPS (t);
4029 :
4030 3012978469 : code = TREE_CODE (t);
4031 :
4032 3012978469 : switch (code)
4033 : {
4034 : /* Alas, constants aren't shared, so we can't rely on pointer
4035 : identity. */
4036 843 : case VOID_CST:
4037 843 : hstate.merge_hash (0);
4038 843 : return;
4039 863766255 : case INTEGER_CST:
4040 863766255 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4041 1746120130 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4042 882353875 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4043 : return;
4044 14975000 : case REAL_CST:
4045 14975000 : {
4046 14975000 : unsigned int val2;
4047 14975000 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4048 : val2 = rvc_zero;
4049 : else
4050 14756984 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4051 14975000 : hstate.merge_hash (val2);
4052 14975000 : return;
4053 : }
4054 0 : case FIXED_CST:
4055 0 : {
4056 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4057 0 : hstate.merge_hash (val2);
4058 0 : return;
4059 : }
4060 12767420 : case STRING_CST:
4061 12767420 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4062 12767420 : TREE_STRING_LENGTH (t));
4063 12767420 : return;
4064 209 : case RAW_DATA_CST:
4065 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4066 209 : RAW_DATA_LENGTH (t));
4067 209 : return;
4068 211383 : case COMPLEX_CST:
4069 211383 : hash_operand (TREE_REALPART (t), hstate, flags);
4070 211383 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4071 211383 : return;
4072 3464656 : case VECTOR_CST:
4073 3464656 : {
4074 3464656 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4075 3464656 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4076 3464656 : unsigned int count = vector_cst_encoded_nelts (t);
4077 14286878 : for (unsigned int i = 0; i < count; ++i)
4078 7357566 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4079 : return;
4080 : }
4081 866386352 : case SSA_NAME:
4082 : /* We can just compare by pointer. */
4083 866386352 : hstate.add_hwi (SSA_NAME_VERSION (t));
4084 866386352 : return;
4085 : case PLACEHOLDER_EXPR:
4086 : /* The node itself doesn't matter. */
4087 : return;
4088 : case BLOCK:
4089 : case OMP_CLAUSE:
4090 : case OMP_NEXT_VARIANT:
4091 : case OMP_TARGET_DEVICE_MATCHES:
4092 : /* Ignore. */
4093 : return;
4094 : case TREE_LIST:
4095 : /* A list of expressions, for a CALL_EXPR or as the elements of a
4096 : VECTOR_CST. */
4097 258540 : for (; t; t = TREE_CHAIN (t))
4098 129270 : hash_operand (TREE_VALUE (t), hstate, flags);
4099 : return;
4100 4976158 : case CONSTRUCTOR:
4101 4976158 : {
4102 4976158 : unsigned HOST_WIDE_INT idx;
4103 4976158 : tree field, value;
4104 4976158 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4105 4976158 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4106 19957337 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4107 : {
4108 : /* In GIMPLE the indexes can be either NULL or matching i. */
4109 14981179 : if (field == NULL_TREE)
4110 1115454 : field = bitsize_int (idx);
4111 14981179 : if (TREE_CODE (field) == FIELD_DECL)
4112 : {
4113 9974990 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4114 9974990 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4115 : }
4116 : else
4117 5006189 : hash_operand (field, hstate, flags);
4118 14981179 : hash_operand (value, hstate, flags);
4119 : }
4120 : return;
4121 : }
4122 182 : case STATEMENT_LIST:
4123 182 : {
4124 182 : tree_stmt_iterator i;
4125 182 : for (i = tsi_start (const_cast<tree> (t));
4126 550 : !tsi_end_p (i); tsi_next (&i))
4127 368 : hash_operand (tsi_stmt (i), hstate, flags);
4128 182 : return;
4129 : }
4130 : case TREE_VEC:
4131 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4132 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4133 : return;
4134 4 : case IDENTIFIER_NODE:
4135 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4136 4 : return;
4137 21717843 : case FUNCTION_DECL:
4138 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4139 : Otherwise nodes that compare equal according to operand_equal_p might
4140 : get different hash codes. However, don't do this for machine specific
4141 : or front end builtins, since the function code is overloaded in those
4142 : cases. */
4143 21717843 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4144 21717843 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4145 : {
4146 7311707 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4147 7311707 : code = TREE_CODE (t);
4148 : }
4149 : /* FALL THROUGH */
4150 1246300610 : default:
4151 1246300610 : if (POLY_INT_CST_P (t))
4152 : {
4153 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4154 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4155 : return;
4156 : }
4157 1246300610 : tclass = TREE_CODE_CLASS (code);
4158 :
4159 1246300610 : if (tclass == tcc_declaration)
4160 : {
4161 : /* DECL's have a unique ID */
4162 904849766 : hstate.add_hwi (DECL_UID (t));
4163 : }
4164 341450844 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4165 : {
4166 : /* For comparisons that can be swapped, use the lower
4167 : tree code. */
4168 153649 : enum tree_code ccode = swap_tree_comparison (code);
4169 153649 : if (code < ccode)
4170 69457 : ccode = code;
4171 153649 : hstate.add_object (ccode);
4172 153649 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4173 153649 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4174 : }
4175 341297195 : else if (CONVERT_EXPR_CODE_P (code))
4176 : {
4177 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4178 : operand_equal_p. */
4179 6627054 : enum tree_code ccode = NOP_EXPR;
4180 6627054 : hstate.add_object (ccode);
4181 :
4182 : /* Don't hash the type, that can lead to having nodes which
4183 : compare equal according to operand_equal_p, but which
4184 : have different hash codes. Make sure to include signedness
4185 : in the hash computation. */
4186 6627054 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4187 6627054 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4188 : }
4189 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4190 334670141 : else if (code == MEM_REF
4191 80748726 : && (flags & OEP_ADDRESS_OF) != 0
4192 71133296 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4193 14110148 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4194 348572319 : && integer_zerop (TREE_OPERAND (t, 1)))
4195 6282609 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4196 : hstate, flags);
4197 : /* Don't ICE on FE specific trees, or their arguments etc.
4198 : during operand_equal_p hash verification. */
4199 328387532 : else if (!IS_EXPR_CODE_CLASS (tclass))
4200 384 : gcc_assert (flags & OEP_HASH_CHECK);
4201 : else
4202 : {
4203 328387148 : unsigned int sflags = flags;
4204 :
4205 328387148 : hstate.add_object (code);
4206 :
4207 328387148 : switch (code)
4208 : {
4209 131810438 : case ADDR_EXPR:
4210 131810438 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4211 131810438 : flags |= OEP_ADDRESS_OF;
4212 131810438 : sflags = flags;
4213 131810438 : break;
4214 :
4215 79454114 : case INDIRECT_REF:
4216 79454114 : case MEM_REF:
4217 79454114 : case TARGET_MEM_REF:
4218 79454114 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4219 79454114 : sflags = flags;
4220 79454114 : break;
4221 :
4222 77236815 : case COMPONENT_REF:
4223 77236815 : if (sflags & OEP_ADDRESS_OF)
4224 : {
4225 38657474 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4226 38657474 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4227 : hstate, flags & ~OEP_ADDRESS_OF);
4228 38657474 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4229 : hstate, flags & ~OEP_ADDRESS_OF);
4230 38657474 : return;
4231 : }
4232 : break;
4233 15674614 : case ARRAY_REF:
4234 15674614 : case ARRAY_RANGE_REF:
4235 15674614 : case BIT_FIELD_REF:
4236 15674614 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4237 15674614 : break;
4238 :
4239 8482 : case COND_EXPR:
4240 8482 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4241 8482 : break;
4242 :
4243 0 : case WIDEN_MULT_PLUS_EXPR:
4244 0 : case WIDEN_MULT_MINUS_EXPR:
4245 0 : {
4246 : /* The multiplication operands are commutative. */
4247 0 : inchash::hash one, two;
4248 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4249 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4250 0 : hstate.add_commutative (one, two);
4251 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4252 0 : return;
4253 : }
4254 :
4255 44080 : case CALL_EXPR:
4256 44080 : if (CALL_EXPR_FN (t) == NULL_TREE)
4257 14 : hstate.add_int (CALL_EXPR_IFN (t));
4258 : break;
4259 :
4260 72 : case TARGET_EXPR:
4261 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4262 : Usually different TARGET_EXPRs just should use
4263 : different temporaries in their slots. */
4264 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4265 72 : return;
4266 :
4267 301660 : case OBJ_TYPE_REF:
4268 : /* Virtual table reference. */
4269 301660 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4270 301660 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4271 301660 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4272 301660 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4273 301660 : if (!virtual_method_call_p (t))
4274 : return;
4275 301639 : if (tree c = obj_type_ref_class (t))
4276 : {
4277 301639 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4278 : /* We compute mangled names only when free_lang_data is run.
4279 : In that case we can hash precisely. */
4280 301639 : if (TREE_CODE (c) == TYPE_DECL
4281 301639 : && DECL_ASSEMBLER_NAME_SET_P (c))
4282 7309 : hstate.add_object
4283 7309 : (IDENTIFIER_HASH_VALUE
4284 : (DECL_ASSEMBLER_NAME (c)));
4285 : }
4286 : return;
4287 : default:
4288 : break;
4289 : }
4290 :
4291 : /* Don't hash the type, that can lead to having nodes which
4292 : compare equal according to operand_equal_p, but which
4293 : have different hash codes. */
4294 289427942 : if (code == NON_LVALUE_EXPR)
4295 : {
4296 : /* Make sure to include signness in the hash computation. */
4297 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4298 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4299 : }
4300 :
4301 289427942 : else if (commutative_tree_code (code))
4302 : {
4303 : /* It's a commutative expression. We want to hash it the same
4304 : however it appears. We do this by first hashing both operands
4305 : and then rehashing based on the order of their independent
4306 : hashes. */
4307 17547790 : inchash::hash one, two;
4308 17547790 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4309 17547790 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4310 17547790 : hstate.add_commutative (one, two);
4311 : }
4312 : else
4313 759264437 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4314 702888488 : hash_operand (TREE_OPERAND (t, i), hstate,
4315 : i == 0 ? flags : sflags);
4316 : }
4317 : return;
4318 : }
4319 : }
4320 :
4321 : bool
4322 7360356114 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4323 : unsigned int flags, bool *ret)
4324 : {
4325 : /* When checking and unless comparing DECL names, verify that if
4326 : the outermost operand_equal_p call returns non-zero then ARG0
4327 : and ARG1 have the same hash value. */
4328 7360356114 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4329 : {
4330 3099444882 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4331 : {
4332 479496760 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4333 : {
4334 85539762 : inchash::hash hstate0 (0), hstate1 (0);
4335 85539762 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4336 85539762 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4337 85539762 : hashval_t h0 = hstate0.end ();
4338 85539762 : hashval_t h1 = hstate1.end ();
4339 85539762 : gcc_assert (h0 == h1);
4340 : }
4341 479496760 : *ret = true;
4342 : }
4343 : else
4344 2619948122 : *ret = false;
4345 :
4346 : return true;
4347 : }
4348 :
4349 : return false;
4350 : }
4351 :
4352 :
4353 : static operand_compare default_compare_instance;
4354 :
4355 : /* Convenience wrapper around operand_compare class because usually we do
4356 : not need to play with the valueizer. */
4357 :
4358 : bool
4359 3097698648 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4360 : {
4361 3097698648 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4362 : }
4363 :
4364 : namespace inchash
4365 : {
4366 :
4367 : /* Generate a hash value for an expression. This can be used iteratively
4368 : by passing a previous result as the HSTATE argument.
4369 :
4370 : This function is intended to produce the same hash for expressions which
4371 : would compare equal using operand_equal_p. */
4372 : void
4373 2217389148 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4374 : {
4375 2217389148 : default_compare_instance.hash_operand (t, hstate, flags);
4376 2217389148 : }
4377 :
4378 : }
4379 :
4380 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4381 : with a different signedness or a narrower precision. */
4382 :
4383 : static bool
4384 20623396 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4385 : {
4386 20623396 : if (operand_equal_p (arg0, arg1, 0))
4387 : return true;
4388 :
4389 39457300 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4390 33680179 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4391 : return false;
4392 :
4393 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4394 : and see if the inner values are the same. This removes any
4395 : signedness comparison, which doesn't matter here. */
4396 6256101 : tree op0 = arg0;
4397 6256101 : tree op1 = arg1;
4398 6256101 : STRIP_NOPS (op0);
4399 6256101 : STRIP_NOPS (op1);
4400 6256101 : if (operand_equal_p (op0, op1, 0))
4401 : return true;
4402 :
4403 : /* Discard a single widening conversion from ARG1 and see if the inner
4404 : value is the same as ARG0. */
4405 5170929 : if (CONVERT_EXPR_P (arg1)
4406 906573 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4407 906519 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4408 906519 : < TYPE_PRECISION (TREE_TYPE (arg1))
4409 6384438 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4410 : return true;
4411 :
4412 : return false;
4413 : }
4414 :
4415 : /* See if ARG is an expression that is either a comparison or is performing
4416 : arithmetic on comparisons. The comparisons must only be comparing
4417 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4418 : they are nonzero it means that some operands have already been found.
4419 : No variables may be used anywhere else in the expression except in the
4420 : comparisons.
4421 :
4422 : If this is true, return 1. Otherwise, return zero. */
4423 :
4424 : static bool
4425 60982087 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4426 : {
4427 65018480 : enum tree_code code = TREE_CODE (arg);
4428 65018480 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4429 :
4430 : /* We can handle some of the tcc_expression cases here. */
4431 65018480 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4432 : tclass = tcc_unary;
4433 64397592 : else if (tclass == tcc_expression
4434 702463 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4435 702463 : || code == COMPOUND_EXPR))
4436 : tclass = tcc_binary;
4437 :
4438 64386659 : switch (tclass)
4439 : {
4440 4036393 : case tcc_unary:
4441 4036393 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4442 :
4443 5397733 : case tcc_binary:
4444 5397733 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4445 5397733 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4446 :
4447 : case tcc_constant:
4448 : return true;
4449 :
4450 691530 : case tcc_expression:
4451 691530 : if (code == COND_EXPR)
4452 692 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4453 692 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4454 756 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4455 : return false;
4456 :
4457 625680 : case tcc_comparison:
4458 : /* First see if we can handle the first operand, then the second. For
4459 : the second operand, we know *CVAL1 can't be zero. It must be that
4460 : one side of the comparison is each of the values; test for the
4461 : case where this isn't true by failing if the two operands
4462 : are the same. */
4463 :
4464 625680 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4465 625680 : TREE_OPERAND (arg, 1), 0))
4466 : return false;
4467 :
4468 625680 : if (*cval1 == 0)
4469 623474 : *cval1 = TREE_OPERAND (arg, 0);
4470 2206 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4471 : ;
4472 2087 : else if (*cval2 == 0)
4473 0 : *cval2 = TREE_OPERAND (arg, 0);
4474 2087 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4475 : ;
4476 : else
4477 : return false;
4478 :
4479 623593 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4480 : ;
4481 623593 : else if (*cval2 == 0)
4482 623474 : *cval2 = TREE_OPERAND (arg, 1);
4483 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4484 : ;
4485 : else
4486 : return false;
4487 :
4488 : return true;
4489 :
4490 : default:
4491 : return false;
4492 : }
4493 : }
4494 :
4495 : /* ARG is a tree that is known to contain just arithmetic operations and
4496 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4497 : any occurrence of OLD0 as an operand of a comparison and likewise for
4498 : NEW1 and OLD1. */
4499 :
4500 : static tree
4501 663 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4502 : tree old1, tree new1)
4503 : {
4504 663 : tree type = TREE_TYPE (arg);
4505 663 : enum tree_code code = TREE_CODE (arg);
4506 663 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4507 :
4508 : /* We can handle some of the tcc_expression cases here. */
4509 663 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4510 : tclass = tcc_unary;
4511 663 : else if (tclass == tcc_expression
4512 30 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4513 : tclass = tcc_binary;
4514 :
4515 642 : switch (tclass)
4516 : {
4517 168 : case tcc_unary:
4518 168 : return fold_build1_loc (loc, code, type,
4519 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4520 168 : old0, new0, old1, new1));
4521 :
4522 153 : case tcc_binary:
4523 306 : return fold_build2_loc (loc, code, type,
4524 153 : eval_subst (loc, TREE_OPERAND (arg, 0),
4525 : old0, new0, old1, new1),
4526 153 : eval_subst (loc, TREE_OPERAND (arg, 1),
4527 153 : old0, new0, old1, new1));
4528 :
4529 9 : case tcc_expression:
4530 9 : switch (code)
4531 : {
4532 0 : case SAVE_EXPR:
4533 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4534 0 : old1, new1);
4535 :
4536 0 : case COMPOUND_EXPR:
4537 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4538 0 : old1, new1);
4539 :
4540 9 : case COND_EXPR:
4541 27 : return fold_build3_loc (loc, code, type,
4542 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4543 : old0, new0, old1, new1),
4544 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4545 : old0, new0, old1, new1),
4546 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4547 9 : old0, new0, old1, new1));
4548 : default:
4549 : break;
4550 : }
4551 : /* Fall through - ??? */
4552 :
4553 168 : case tcc_comparison:
4554 168 : {
4555 168 : tree arg0 = TREE_OPERAND (arg, 0);
4556 168 : tree arg1 = TREE_OPERAND (arg, 1);
4557 :
4558 : /* We need to check both for exact equality and tree equality. The
4559 : former will be true if the operand has a side-effect. In that
4560 : case, we know the operand occurred exactly once. */
4561 :
4562 168 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4563 : arg0 = new0;
4564 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4565 : arg0 = new1;
4566 :
4567 168 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4568 : arg1 = new0;
4569 168 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4570 : arg1 = new1;
4571 :
4572 168 : return fold_build2_loc (loc, code, type, arg0, arg1);
4573 : }
4574 :
4575 : default:
4576 : return arg;
4577 : }
4578 : }
4579 :
4580 : /* Return a tree for the case when the result of an expression is RESULT
4581 : converted to TYPE and OMITTED was previously an operand of the expression
4582 : but is now not needed (e.g., we folded OMITTED * 0).
4583 :
4584 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4585 : the conversion of RESULT to TYPE. */
4586 :
4587 : tree
4588 315930 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4589 : {
4590 315930 : tree t = fold_convert_loc (loc, type, result);
4591 :
4592 : /* If the resulting operand is an empty statement, just return the omitted
4593 : statement casted to void. */
4594 315930 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4595 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4596 0 : fold_ignored_result (omitted));
4597 :
4598 315930 : if (TREE_SIDE_EFFECTS (omitted))
4599 12092 : return build2_loc (loc, COMPOUND_EXPR, type,
4600 12092 : fold_ignored_result (omitted), t);
4601 :
4602 303838 : return non_lvalue_loc (loc, t);
4603 : }
4604 :
4605 : /* Return a tree for the case when the result of an expression is RESULT
4606 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4607 : of the expression but are now not needed.
4608 :
4609 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4610 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4611 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4612 : just do the conversion of RESULT to TYPE. */
4613 :
4614 : tree
4615 6105 : omit_two_operands_loc (location_t loc, tree type, tree result,
4616 : tree omitted1, tree omitted2)
4617 : {
4618 6105 : tree t = fold_convert_loc (loc, type, result);
4619 :
4620 6105 : if (TREE_SIDE_EFFECTS (omitted2))
4621 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4622 6105 : if (TREE_SIDE_EFFECTS (omitted1))
4623 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4624 :
4625 6105 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4626 : }
4627 :
4628 :
4629 : /* Return a simplified tree node for the truth-negation of ARG. This
4630 : never alters ARG itself. We assume that ARG is an operation that
4631 : returns a truth value (0 or 1).
4632 :
4633 : FIXME: one would think we would fold the result, but it causes
4634 : problems with the dominator optimizer. */
4635 :
4636 : static tree
4637 51486608 : fold_truth_not_expr (location_t loc, tree arg)
4638 : {
4639 51486608 : tree type = TREE_TYPE (arg);
4640 51486608 : enum tree_code code = TREE_CODE (arg);
4641 51486608 : location_t loc1, loc2;
4642 :
4643 : /* If this is a comparison, we can simply invert it, except for
4644 : floating-point non-equality comparisons, in which case we just
4645 : enclose a TRUTH_NOT_EXPR around what we have. */
4646 :
4647 51486608 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4648 : {
4649 39357916 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4650 32659973 : if (FLOAT_TYPE_P (op_type)
4651 6708030 : && flag_trapping_math
4652 6677187 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4653 45995781 : && code != NE_EXPR && code != EQ_EXPR)
4654 : return NULL_TREE;
4655 :
4656 33415325 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4657 33415325 : if (code == ERROR_MARK)
4658 : return NULL_TREE;
4659 :
4660 33415325 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4661 33415325 : TREE_OPERAND (arg, 1));
4662 33415325 : copy_warning (ret, arg);
4663 33415325 : return ret;
4664 : }
4665 :
4666 12128692 : switch (code)
4667 : {
4668 0 : case INTEGER_CST:
4669 0 : return constant_boolean_node (integer_zerop (arg), type);
4670 :
4671 42720 : case TRUTH_AND_EXPR:
4672 42720 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4673 42720 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4674 85440 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4675 42720 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4676 85440 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4677 :
4678 2638 : case TRUTH_OR_EXPR:
4679 2638 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4680 2638 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4681 5276 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4682 2638 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4683 5276 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4684 :
4685 73308 : case TRUTH_XOR_EXPR:
4686 : /* Here we can invert either operand. We invert the first operand
4687 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4688 : result is the XOR of the first operand with the inside of the
4689 : negation of the second operand. */
4690 :
4691 73308 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4692 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4693 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4694 : else
4695 73120 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4696 73120 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4697 146240 : TREE_OPERAND (arg, 1));
4698 :
4699 399355 : case TRUTH_ANDIF_EXPR:
4700 399355 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4701 399355 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4702 798710 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4703 399355 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4704 798710 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4705 :
4706 16984 : case TRUTH_ORIF_EXPR:
4707 16984 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4708 16984 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4709 33968 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4710 16984 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4711 33968 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4712 :
4713 769744 : case TRUTH_NOT_EXPR:
4714 769744 : return TREE_OPERAND (arg, 0);
4715 :
4716 9747 : case COND_EXPR:
4717 9747 : {
4718 9747 : tree arg1 = TREE_OPERAND (arg, 1);
4719 9747 : tree arg2 = TREE_OPERAND (arg, 2);
4720 :
4721 9747 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4722 9747 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4723 :
4724 : /* A COND_EXPR may have a throw as one operand, which
4725 : then has void type. Just leave void operands
4726 : as they are. */
4727 9747 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4728 9747 : VOID_TYPE_P (TREE_TYPE (arg1))
4729 9747 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4730 9747 : VOID_TYPE_P (TREE_TYPE (arg2))
4731 19491 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4732 : }
4733 :
4734 991 : case COMPOUND_EXPR:
4735 991 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4736 1982 : return build2_loc (loc, COMPOUND_EXPR, type,
4737 991 : TREE_OPERAND (arg, 0),
4738 1982 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4739 :
4740 0 : case NON_LVALUE_EXPR:
4741 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4742 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4743 :
4744 75558 : CASE_CONVERT:
4745 75558 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4746 75494 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4747 :
4748 : /* fall through */
4749 :
4750 64 : case FLOAT_EXPR:
4751 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4752 64 : return build1_loc (loc, TREE_CODE (arg), type,
4753 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4754 :
4755 492 : case BIT_AND_EXPR:
4756 492 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4757 : return NULL_TREE;
4758 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4759 :
4760 2 : case SAVE_EXPR:
4761 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4762 :
4763 432 : case CLEANUP_POINT_EXPR:
4764 432 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4765 432 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4766 864 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4767 :
4768 : default:
4769 : return NULL_TREE;
4770 : }
4771 : }
4772 :
4773 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4774 : assume that ARG is an operation that returns a truth value (0 or 1
4775 : for scalars, 0 or -1 for vectors). Return the folded expression if
4776 : folding is successful. Otherwise, return NULL_TREE. */
4777 :
4778 : static tree
4779 2115900 : fold_invert_truthvalue (location_t loc, tree arg)
4780 : {
4781 2115900 : tree type = TREE_TYPE (arg);
4782 4231776 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4783 : ? BIT_NOT_EXPR
4784 : : TRUTH_NOT_EXPR,
4785 2115900 : type, arg);
4786 : }
4787 :
4788 : /* Return a simplified tree node for the truth-negation of ARG. This
4789 : never alters ARG itself. We assume that ARG is an operation that
4790 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4791 :
4792 : tree
4793 43802322 : invert_truthvalue_loc (location_t loc, tree arg)
4794 : {
4795 43802322 : if (TREE_CODE (arg) == ERROR_MARK)
4796 : return arg;
4797 :
4798 43802322 : tree type = TREE_TYPE (arg);
4799 87604644 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4800 : ? BIT_NOT_EXPR
4801 : : TRUTH_NOT_EXPR,
4802 43802322 : type, arg);
4803 : }
4804 :
4805 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4806 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4807 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4808 : is the original memory reference used to preserve the alias set of
4809 : the access. */
4810 :
4811 : tree
4812 847746 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4813 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4814 : int unsignedp, int reversep)
4815 : {
4816 847746 : tree result, bftype;
4817 :
4818 : /* Attempt not to lose the access path if possible. */
4819 847746 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4820 : {
4821 843884 : tree ninner = TREE_OPERAND (orig_inner, 0);
4822 843884 : machine_mode nmode;
4823 843884 : poly_int64 nbitsize, nbitpos;
4824 843884 : tree noffset;
4825 843884 : int nunsignedp, nreversep, nvolatilep = 0;
4826 843884 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4827 : &noffset, &nmode, &nunsignedp,
4828 : &nreversep, &nvolatilep);
4829 843884 : if (base == inner
4830 843757 : && noffset == NULL_TREE
4831 843757 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4832 843750 : && !reversep
4833 843671 : && !nreversep
4834 1687555 : && !nvolatilep)
4835 : {
4836 843671 : inner = ninner;
4837 843884 : bitpos -= nbitpos;
4838 : }
4839 : }
4840 :
4841 847746 : alias_set_type iset = get_alias_set (orig_inner);
4842 847746 : if (iset == 0 && get_alias_set (inner) != iset)
4843 242 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4844 : build_fold_addr_expr (inner),
4845 : build_int_cst (ptr_type_node, 0));
4846 :
4847 847746 : if (known_eq (bitpos, 0) && !reversep)
4848 : {
4849 13008 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4850 26016 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4851 12851 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4852 161 : && tree_fits_shwi_p (size)
4853 13169 : && tree_to_shwi (size) == bitsize)
4854 138 : return fold_convert_loc (loc, type, inner);
4855 : }
4856 :
4857 847608 : bftype = type;
4858 847608 : if (TYPE_PRECISION (bftype) != bitsize
4859 847608 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4860 585 : bftype = build_nonstandard_integer_type (bitsize, 0);
4861 :
4862 847608 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4863 847608 : bitsize_int (bitsize), bitsize_int (bitpos));
4864 847608 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4865 :
4866 847608 : if (bftype != type)
4867 585 : result = fold_convert_loc (loc, type, result);
4868 :
4869 : return result;
4870 : }
4871 :
4872 : /* Optimize a bit-field compare.
4873 :
4874 : There are two cases: First is a compare against a constant and the
4875 : second is a comparison of two items where the fields are at the same
4876 : bit position relative to the start of a chunk (byte, halfword, word)
4877 : large enough to contain it. In these cases we can avoid the shift
4878 : implicit in bitfield extractions.
4879 :
4880 : For constants, we emit a compare of the shifted constant with the
4881 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4882 : compared. For two fields at the same position, we do the ANDs with the
4883 : similar mask and compare the result of the ANDs.
4884 :
4885 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4886 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4887 : are the left and right operands of the comparison, respectively.
4888 :
4889 : If the optimization described above can be done, we return the resulting
4890 : tree. Otherwise we return zero. */
4891 :
4892 : static tree
4893 4758096 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4894 : tree compare_type, tree lhs, tree rhs)
4895 : {
4896 4758096 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4897 4758096 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4898 4758096 : tree type = TREE_TYPE (lhs);
4899 4758096 : tree unsigned_type;
4900 4758096 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4901 4758096 : machine_mode lmode, rmode;
4902 4758096 : scalar_int_mode nmode;
4903 4758096 : int lunsignedp, runsignedp;
4904 4758096 : int lreversep, rreversep;
4905 4758096 : int lvolatilep = 0, rvolatilep = 0;
4906 4758096 : tree linner, rinner = NULL_TREE;
4907 4758096 : tree mask;
4908 4758096 : tree offset;
4909 :
4910 : /* Get all the information about the extractions being done. If the bit size
4911 : is the same as the size of the underlying object, we aren't doing an
4912 : extraction at all and so can do nothing. We also don't want to
4913 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4914 : then will no longer be able to replace it. */
4915 4758096 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4916 : &lunsignedp, &lreversep, &lvolatilep);
4917 4758096 : if (linner == lhs
4918 4758096 : || !known_size_p (plbitsize)
4919 4758096 : || !plbitsize.is_constant (&lbitsize)
4920 4758096 : || !plbitpos.is_constant (&lbitpos)
4921 9516192 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4922 807610 : || offset != 0
4923 807585 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4924 5565681 : || lvolatilep)
4925 : return 0;
4926 :
4927 807525 : if (const_p)
4928 768313 : rreversep = lreversep;
4929 : else
4930 : {
4931 : /* If this is not a constant, we can only do something if bit positions,
4932 : sizes, signedness and storage order are the same. */
4933 39212 : rinner
4934 39212 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4935 : &runsignedp, &rreversep, &rvolatilep);
4936 :
4937 39212 : if (rinner == rhs
4938 39168 : || maybe_ne (lbitpos, rbitpos)
4939 39134 : || maybe_ne (lbitsize, rbitsize)
4940 39134 : || lunsignedp != runsignedp
4941 39134 : || lreversep != rreversep
4942 39134 : || offset != 0
4943 39134 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4944 78346 : || rvolatilep)
4945 : return 0;
4946 : }
4947 :
4948 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4949 807447 : poly_uint64 bitstart = 0;
4950 807447 : poly_uint64 bitend = 0;
4951 807447 : if (TREE_CODE (lhs) == COMPONENT_REF)
4952 : {
4953 807447 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4954 807447 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4955 : return 0;
4956 : }
4957 :
4958 : /* See if we can find a mode to refer to this field. We should be able to,
4959 : but fail if we can't. */
4960 1614894 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4961 768313 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4962 39134 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4963 : TYPE_ALIGN (TREE_TYPE (rinner))),
4964 807447 : BITS_PER_WORD, false, &nmode))
4965 : return 0;
4966 :
4967 : /* Set signed and unsigned types of the precision of this mode for the
4968 : shifts below. */
4969 805444 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4970 :
4971 : /* Compute the bit position and size for the new reference and our offset
4972 : within it. If the new reference is the same size as the original, we
4973 : won't optimize anything, so return zero. */
4974 805444 : nbitsize = GET_MODE_BITSIZE (nmode);
4975 805444 : nbitpos = lbitpos & ~ (nbitsize - 1);
4976 805444 : lbitpos -= nbitpos;
4977 805444 : if (nbitsize == lbitsize)
4978 : return 0;
4979 :
4980 805444 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4981 61 : lbitpos = nbitsize - lbitsize - lbitpos;
4982 :
4983 : /* Make the mask to be used against the extracted field. */
4984 805444 : mask = build_int_cst_type (unsigned_type, -1);
4985 805444 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4986 805444 : mask = const_binop (RSHIFT_EXPR, mask,
4987 805444 : size_int (nbitsize - lbitsize - lbitpos));
4988 :
4989 805444 : if (! const_p)
4990 : {
4991 37579 : if (nbitpos < 0)
4992 : return 0;
4993 :
4994 : /* If not comparing with constant, just rework the comparison
4995 : and return. */
4996 37579 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4997 37579 : nbitsize, nbitpos, 1, lreversep);
4998 37579 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4999 37579 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
5000 37579 : nbitsize, nbitpos, 1, rreversep);
5001 37579 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
5002 37579 : return fold_build2_loc (loc, code, compare_type, t1, t2);
5003 : }
5004 :
5005 : /* Otherwise, we are handling the constant case. See if the constant is too
5006 : big for the field. Warn and return a tree for 0 (false) if so. We do
5007 : this not only for its own sake, but to avoid having to test for this
5008 : error case below. If we didn't, we might generate wrong code.
5009 :
5010 : For unsigned fields, the constant shifted right by the field length should
5011 : be all zero. For signed fields, the high-order bits should agree with
5012 : the sign bit. */
5013 :
5014 767865 : if (lunsignedp)
5015 : {
5016 766693 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
5017 : {
5018 0 : warning (0, "comparison is always %d due to width of bit-field",
5019 : code == NE_EXPR);
5020 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5021 : }
5022 : }
5023 : else
5024 : {
5025 1172 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5026 1172 : if (tem != 0 && tem != -1)
5027 : {
5028 0 : warning (0, "comparison is always %d due to width of bit-field",
5029 : code == NE_EXPR);
5030 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5031 : }
5032 1172 : }
5033 :
5034 767865 : if (nbitpos < 0)
5035 : return 0;
5036 :
5037 : /* Single-bit compares should always be against zero. */
5038 767865 : if (lbitsize == 1 && ! integer_zerop (rhs))
5039 : {
5040 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5041 175 : rhs = build_int_cst (type, 0);
5042 : }
5043 :
5044 : /* Make a new bitfield reference, shift the constant over the
5045 : appropriate number of bits and mask it with the computed mask
5046 : (in case this was a signed field). If we changed it, make a new one. */
5047 767865 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5048 767865 : nbitsize, nbitpos, 1, lreversep);
5049 :
5050 767865 : rhs = const_binop (BIT_AND_EXPR,
5051 : const_binop (LSHIFT_EXPR,
5052 : fold_convert_loc (loc, unsigned_type, rhs),
5053 767865 : size_int (lbitpos)),
5054 : mask);
5055 :
5056 767865 : lhs = build2_loc (loc, code, compare_type,
5057 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5058 767865 : return lhs;
5059 : }
5060 :
5061 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5062 : represents the sign bit of EXP's type. If EXP represents a sign
5063 : or zero extension, also test VAL against the unextended type.
5064 : The return value is the (sub)expression whose sign bit is VAL,
5065 : or NULL_TREE otherwise. */
5066 :
5067 : tree
5068 2605 : sign_bit_p (tree exp, const_tree val)
5069 : {
5070 2605 : int width;
5071 2605 : tree t;
5072 :
5073 : /* Tree EXP must have an integral type. */
5074 2605 : t = TREE_TYPE (exp);
5075 2605 : if (! INTEGRAL_TYPE_P (t))
5076 : return NULL_TREE;
5077 :
5078 : /* Tree VAL must be an integer constant. */
5079 2259 : if (TREE_CODE (val) != INTEGER_CST
5080 2259 : || TREE_OVERFLOW (val))
5081 : return NULL_TREE;
5082 :
5083 1894 : width = TYPE_PRECISION (t);
5084 1894 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5085 : return exp;
5086 :
5087 : /* Handle extension from a narrower type. */
5088 1257 : if (TREE_CODE (exp) == NOP_EXPR
5089 1257 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5090 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5091 :
5092 : return NULL_TREE;
5093 : }
5094 :
5095 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5096 : operand is simple enough to be evaluated unconditionally. */
5097 :
5098 : static bool
5099 66070324 : simple_operand_p (const_tree exp)
5100 : {
5101 : /* Strip any conversions that don't change the machine mode. */
5102 66070324 : STRIP_NOPS (exp);
5103 :
5104 66070324 : return (CONSTANT_CLASS_P (exp)
5105 45949506 : || TREE_CODE (exp) == SSA_NAME
5106 82444966 : || (DECL_P (exp)
5107 5691713 : && ! TREE_ADDRESSABLE (exp)
5108 5603598 : && ! TREE_THIS_VOLATILE (exp)
5109 5603598 : && ! DECL_NONLOCAL (exp)
5110 : /* Don't regard global variables as simple. They may be
5111 : allocated in ways unknown to the compiler (shared memory,
5112 : #pragma weak, etc). */
5113 5601945 : && ! TREE_PUBLIC (exp)
5114 5581186 : && ! DECL_EXTERNAL (exp)
5115 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5116 5581186 : && ! ((VAR_P (exp)
5117 : || TREE_CODE (exp) == PARM_DECL
5118 : || TREE_CODE (exp) == RESULT_DECL)
5119 5581186 : && DECL_HAS_VALUE_EXPR_P (exp))
5120 : /* Weakrefs are not safe to be read, since they can be NULL.
5121 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5122 : have DECL_WEAK flag set. */
5123 5580597 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5124 : /* Loading a static variable is unduly expensive, but global
5125 : registers aren't expensive. */
5126 5580597 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5127 : }
5128 :
5129 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5130 : In addition to simple_operand_p, we assume that comparisons, conversions,
5131 : and logic-not operations are simple, if their operands are simple, too. */
5132 :
5133 : bool
5134 7191806 : simple_condition_p (tree exp)
5135 : {
5136 7286861 : enum tree_code code;
5137 :
5138 7286861 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5139 : return false;
5140 :
5141 2337579 : while (CONVERT_EXPR_P (exp))
5142 35872 : exp = TREE_OPERAND (exp, 0);
5143 :
5144 2301707 : code = TREE_CODE (exp);
5145 :
5146 2301707 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5147 1797952 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5148 1797952 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5149 :
5150 503755 : if (code == TRUTH_NOT_EXPR)
5151 95055 : return simple_condition_p (TREE_OPERAND (exp, 0));
5152 :
5153 408700 : return simple_operand_p (exp);
5154 : }
5155 :
5156 :
5157 : /* The following functions are subroutines to fold_range_test and allow it to
5158 : try to change a logical combination of comparisons into a range test.
5159 :
5160 : For example, both
5161 : X == 2 || X == 3 || X == 4 || X == 5
5162 : and
5163 : X >= 2 && X <= 5
5164 : are converted to
5165 : (unsigned) (X - 2) <= 3
5166 :
5167 : We describe each set of comparisons as being either inside or outside
5168 : a range, using a variable named like IN_P, and then describe the
5169 : range with a lower and upper bound. If one of the bounds is omitted,
5170 : it represents either the highest or lowest value of the type.
5171 :
5172 : In the comments below, we represent a range by two numbers in brackets
5173 : preceded by a "+" to designate being inside that range, or a "-" to
5174 : designate being outside that range, so the condition can be inverted by
5175 : flipping the prefix. An omitted bound is represented by a "-". For
5176 : example, "- [-, 10]" means being outside the range starting at the lowest
5177 : possible value and ending at 10, in other words, being greater than 10.
5178 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5179 : always false.
5180 :
5181 : We set up things so that the missing bounds are handled in a consistent
5182 : manner so neither a missing bound nor "true" and "false" need to be
5183 : handled using a special case. */
5184 :
5185 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5186 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5187 : and UPPER1_P are nonzero if the respective argument is an upper bound
5188 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5189 : must be specified for a comparison. ARG1 will be converted to ARG0's
5190 : type if both are specified. */
5191 :
5192 : static tree
5193 23350736 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5194 : tree arg1, int upper1_p)
5195 : {
5196 23350736 : tree tem;
5197 23350736 : int result;
5198 23350736 : int sgn0, sgn1;
5199 :
5200 : /* If neither arg represents infinity, do the normal operation.
5201 : Else, if not a comparison, return infinity. Else handle the special
5202 : comparison rules. Note that most of the cases below won't occur, but
5203 : are handled for consistency. */
5204 :
5205 23350736 : if (arg0 != 0 && arg1 != 0)
5206 : {
5207 12251406 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5208 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5209 12251406 : STRIP_NOPS (tem);
5210 12251406 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5211 : }
5212 :
5213 11099330 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5214 : return 0;
5215 :
5216 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5217 : for neither. In real maths, we cannot assume open ended ranges are
5218 : the same. But, this is computer arithmetic, where numbers are finite.
5219 : We can therefore make the transformation of any unbounded range with
5220 : the value Z, Z being greater than any representable number. This permits
5221 : us to treat unbounded ranges as equal. */
5222 11090183 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5223 11090183 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5224 11090183 : switch (code)
5225 : {
5226 5230638 : case EQ_EXPR:
5227 5230638 : result = sgn0 == sgn1;
5228 5230638 : break;
5229 0 : case NE_EXPR:
5230 0 : result = sgn0 != sgn1;
5231 0 : break;
5232 363743 : case LT_EXPR:
5233 363743 : result = sgn0 < sgn1;
5234 363743 : break;
5235 2551014 : case LE_EXPR:
5236 2551014 : result = sgn0 <= sgn1;
5237 2551014 : break;
5238 2944788 : case GT_EXPR:
5239 2944788 : result = sgn0 > sgn1;
5240 2944788 : break;
5241 0 : case GE_EXPR:
5242 0 : result = sgn0 >= sgn1;
5243 0 : break;
5244 0 : default:
5245 0 : gcc_unreachable ();
5246 : }
5247 :
5248 11090183 : return constant_boolean_node (result, type);
5249 : }
5250 :
5251 : /* Helper routine for make_range. Perform one step for it, return
5252 : new expression if the loop should continue or NULL_TREE if it should
5253 : stop. */
5254 :
5255 : tree
5256 61312602 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5257 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5258 : {
5259 61312602 : tree arg0_type = TREE_TYPE (arg0);
5260 61312602 : tree n_low, n_high, low = *p_low, high = *p_high;
5261 61312602 : int in_p = *p_in_p, n_in_p;
5262 :
5263 61312602 : switch (code)
5264 : {
5265 1716422 : case TRUTH_NOT_EXPR:
5266 : /* We can only do something if the range is testing for zero. */
5267 1716422 : if (low == NULL_TREE || high == NULL_TREE
5268 1716422 : || ! integer_zerop (low) || ! integer_zerop (high))
5269 : return NULL_TREE;
5270 1716422 : *p_in_p = ! in_p;
5271 1716422 : return arg0;
5272 :
5273 47472616 : case EQ_EXPR: case NE_EXPR:
5274 47472616 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5275 : /* We can only do something if the range is testing for zero
5276 : and if the second operand is an integer constant. Note that
5277 : saying something is "in" the range we make is done by
5278 : complementing IN_P since it will set in the initial case of
5279 : being not equal to zero; "out" is leaving it alone. */
5280 47472616 : if (low == NULL_TREE || high == NULL_TREE
5281 47472616 : || ! integer_zerop (low) || ! integer_zerop (high)
5282 94945144 : || TREE_CODE (arg1) != INTEGER_CST)
5283 : return NULL_TREE;
5284 :
5285 29986723 : switch (code)
5286 : {
5287 : case NE_EXPR: /* - [c, c] */
5288 : low = high = arg1;
5289 : break;
5290 8283105 : case EQ_EXPR: /* + [c, c] */
5291 8283105 : in_p = ! in_p, low = high = arg1;
5292 8283105 : break;
5293 2217910 : case GT_EXPR: /* - [-, c] */
5294 2217910 : low = 0, high = arg1;
5295 2217910 : break;
5296 748389 : case GE_EXPR: /* + [c, -] */
5297 748389 : in_p = ! in_p, low = arg1, high = 0;
5298 748389 : break;
5299 5716388 : case LT_EXPR: /* - [c, -] */
5300 5716388 : low = arg1, high = 0;
5301 5716388 : break;
5302 4336986 : case LE_EXPR: /* + [-, c] */
5303 4336986 : in_p = ! in_p, low = 0, high = arg1;
5304 4336986 : break;
5305 : default:
5306 : gcc_unreachable ();
5307 : }
5308 :
5309 : /* If this is an unsigned comparison, we also know that EXP is
5310 : greater than or equal to zero. We base the range tests we make
5311 : on that fact, so we record it here so we can parse existing
5312 : range tests. We test arg0_type since often the return type
5313 : of, e.g. EQ_EXPR, is boolean. */
5314 29986723 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5315 : {
5316 1948460 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5317 : in_p, low, high, 1,
5318 : build_int_cst (arg0_type, 0),
5319 : NULL_TREE))
5320 : return NULL_TREE;
5321 :
5322 1948451 : in_p = n_in_p, low = n_low, high = n_high;
5323 :
5324 : /* If the high bound is missing, but we have a nonzero low
5325 : bound, reverse the range so it goes from zero to the low bound
5326 : minus 1. */
5327 1948451 : if (high == 0 && low && ! integer_zerop (low))
5328 : {
5329 867593 : in_p = ! in_p;
5330 867593 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5331 867593 : build_int_cst (TREE_TYPE (low), 1), 0);
5332 867593 : low = build_int_cst (arg0_type, 0);
5333 : }
5334 : }
5335 :
5336 29986714 : *p_low = low;
5337 29986714 : *p_high = high;
5338 29986714 : *p_in_p = in_p;
5339 29986714 : return arg0;
5340 :
5341 335 : case NEGATE_EXPR:
5342 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5343 : low and high are non-NULL, then normalize will DTRT. */
5344 335 : if (!TYPE_UNSIGNED (arg0_type)
5345 335 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5346 : {
5347 93 : if (low == NULL_TREE)
5348 18 : low = TYPE_MIN_VALUE (arg0_type);
5349 93 : if (high == NULL_TREE)
5350 39 : high = TYPE_MAX_VALUE (arg0_type);
5351 : }
5352 :
5353 : /* (-x) IN [a,b] -> x in [-b, -a] */
5354 335 : n_low = range_binop (MINUS_EXPR, exp_type,
5355 : build_int_cst (exp_type, 0),
5356 : 0, high, 1);
5357 335 : n_high = range_binop (MINUS_EXPR, exp_type,
5358 : build_int_cst (exp_type, 0),
5359 : 0, low, 0);
5360 335 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5361 : return NULL_TREE;
5362 317 : goto normalize;
5363 :
5364 26 : case BIT_NOT_EXPR:
5365 : /* ~ X -> -X - 1 */
5366 26 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5367 : build_int_cst (exp_type, 1));
5368 :
5369 868445 : case PLUS_EXPR:
5370 868445 : case MINUS_EXPR:
5371 868445 : if (TREE_CODE (arg1) != INTEGER_CST)
5372 : return NULL_TREE;
5373 :
5374 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5375 : move a constant to the other side. */
5376 661971 : if (!TYPE_UNSIGNED (arg0_type)
5377 661971 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5378 : return NULL_TREE;
5379 :
5380 : /* If EXP is signed, any overflow in the computation is undefined,
5381 : so we don't worry about it so long as our computations on
5382 : the bounds don't overflow. For unsigned, overflow is defined
5383 : and this is exactly the right thing. */
5384 948025 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5385 : arg0_type, low, 0, arg1, 0);
5386 475524 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5387 : arg0_type, high, 1, arg1, 0);
5388 471948 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5389 947460 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5390 : return NULL_TREE;
5391 :
5392 475829 : normalize:
5393 : /* Check for an unsigned range which has wrapped around the maximum
5394 : value thus making n_high < n_low, and normalize it. */
5395 475829 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5396 : {
5397 133800 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5398 133800 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5399 133800 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5400 133800 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5401 :
5402 : /* If the range is of the form +/- [ x+1, x ], we won't
5403 : be able to normalize it. But then, it represents the
5404 : whole range or the empty set, so make it
5405 : +/- [ -, - ]. */
5406 133800 : if (tree_int_cst_equal (n_low, low)
5407 133800 : && tree_int_cst_equal (n_high, high))
5408 : low = high = 0;
5409 : else
5410 133800 : in_p = ! in_p;
5411 : }
5412 : else
5413 342029 : low = n_low, high = n_high;
5414 :
5415 475829 : *p_low = low;
5416 475829 : *p_high = high;
5417 475829 : *p_in_p = in_p;
5418 475829 : return arg0;
5419 :
5420 2583011 : CASE_CONVERT:
5421 2583011 : case NON_LVALUE_EXPR:
5422 2583011 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5423 : return NULL_TREE;
5424 :
5425 1169577 : if (! INTEGRAL_TYPE_P (arg0_type)
5426 1133955 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5427 1040562 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5428 : return NULL_TREE;
5429 :
5430 1021963 : n_low = low, n_high = high;
5431 :
5432 1021963 : if (n_low != 0)
5433 852567 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5434 :
5435 1021963 : if (n_high != 0)
5436 957071 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5437 :
5438 : /* If we're converting arg0 from an unsigned type, to exp,
5439 : a signed type, we will be doing the comparison as unsigned.
5440 : The tests above have already verified that LOW and HIGH
5441 : are both positive.
5442 :
5443 : So we have to ensure that we will handle large unsigned
5444 : values the same way that the current signed bounds treat
5445 : negative values. */
5446 :
5447 1021963 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5448 : {
5449 259543 : tree high_positive;
5450 259543 : tree equiv_type;
5451 : /* For fixed-point modes, we need to pass the saturating flag
5452 : as the 2nd parameter. */
5453 259543 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5454 0 : equiv_type
5455 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5456 0 : TYPE_SATURATING (arg0_type));
5457 259543 : else if (BITINT_TYPE_P (arg0_type))
5458 : equiv_type = arg0_type;
5459 : else
5460 259528 : equiv_type
5461 259528 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5462 :
5463 : /* A range without an upper bound is, naturally, unbounded.
5464 : Since convert would have cropped a very large value, use
5465 : the max value for the destination type. */
5466 259543 : high_positive
5467 259543 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5468 0 : : TYPE_MAX_VALUE (arg0_type);
5469 :
5470 259543 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5471 238178 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5472 : fold_convert_loc (loc, arg0_type,
5473 : high_positive),
5474 : build_int_cst (arg0_type, 1));
5475 :
5476 : /* If the low bound is specified, "and" the range with the
5477 : range for which the original unsigned value will be
5478 : positive. */
5479 259543 : if (low != 0)
5480 : {
5481 95778 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5482 : 1, fold_convert_loc (loc, arg0_type,
5483 : integer_zero_node),
5484 : high_positive))
5485 : return NULL_TREE;
5486 :
5487 95778 : in_p = (n_in_p == in_p);
5488 : }
5489 : else
5490 : {
5491 : /* Otherwise, "or" the range with the range of the input
5492 : that will be interpreted as negative. */
5493 163765 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5494 : 1, fold_convert_loc (loc, arg0_type,
5495 : integer_zero_node),
5496 : high_positive))
5497 : return NULL_TREE;
5498 :
5499 163765 : in_p = (in_p != n_in_p);
5500 : }
5501 : }
5502 :
5503 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5504 : an unsigned type, we will do the comparison as signed. If
5505 : high is non-NULL, we punt above if it doesn't fit in the signed
5506 : type, so if we get through here, +[-, high] or +[low, high] are
5507 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5508 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5509 : high is not, the +[low, -] range is equivalent to union of
5510 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5511 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5512 : low being 0, which should be treated as [-, -]. */
5513 762420 : else if (TYPE_UNSIGNED (exp_type)
5514 744042 : && !TYPE_UNSIGNED (arg0_type)
5515 385106 : && low
5516 1147526 : && !high)
5517 : {
5518 12 : if (integer_zerop (low))
5519 12 : n_low = NULL_TREE;
5520 : else
5521 : {
5522 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5523 : n_low, build_int_cst (arg0_type, -1));
5524 0 : n_low = build_zero_cst (arg0_type);
5525 0 : in_p = !in_p;
5526 : }
5527 : }
5528 :
5529 1021963 : *p_low = n_low;
5530 1021963 : *p_high = n_high;
5531 1021963 : *p_in_p = in_p;
5532 1021963 : return arg0;
5533 :
5534 : default:
5535 : return NULL_TREE;
5536 : }
5537 : }
5538 :
5539 : /* Given EXP, a logical expression, set the range it is testing into
5540 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5541 : actually being tested. *PLOW and *PHIGH will be made of the same
5542 : type as the returned expression. If EXP is not a comparison, we
5543 : will most likely not be returning a useful value and range. */
5544 :
5545 : tree
5546 50500128 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5547 : {
5548 50500128 : enum tree_code code;
5549 50500128 : tree arg0, arg1 = NULL_TREE;
5550 50500128 : tree exp_type, nexp;
5551 50500128 : int in_p;
5552 50500128 : tree low, high;
5553 50500128 : location_t loc = EXPR_LOCATION (exp);
5554 :
5555 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5556 : and see if we can refine the range. Some of the cases below may not
5557 : happen, but it doesn't seem worth worrying about this. We "continue"
5558 : the outer loop when we've changed something; otherwise we "break"
5559 : the switch, which will "break" the while. */
5560 :
5561 50500128 : in_p = 0;
5562 50500128 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5563 :
5564 80603350 : while (1)
5565 : {
5566 80603350 : code = TREE_CODE (exp);
5567 80603350 : exp_type = TREE_TYPE (exp);
5568 80603350 : arg0 = NULL_TREE;
5569 :
5570 80603350 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5571 : {
5572 56482782 : if (TREE_OPERAND_LENGTH (exp) > 0)
5573 56482782 : arg0 = TREE_OPERAND (exp, 0);
5574 56482782 : if (TREE_CODE_CLASS (code) == tcc_binary
5575 53181515 : || TREE_CODE_CLASS (code) == tcc_comparison
5576 66274128 : || (TREE_CODE_CLASS (code) == tcc_expression
5577 2914664 : && TREE_OPERAND_LENGTH (exp) > 1))
5578 47872472 : arg1 = TREE_OPERAND (exp, 1);
5579 : }
5580 56482782 : if (arg0 == NULL_TREE)
5581 : break;
5582 :
5583 56482768 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5584 : &high, &in_p);
5585 56482768 : if (nexp == NULL_TREE)
5586 : break;
5587 : exp = nexp;
5588 : }
5589 :
5590 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5591 50500128 : if (TREE_CODE (exp) == INTEGER_CST)
5592 : {
5593 31776 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5594 : exp, 0, low, 0))
5595 31776 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5596 : exp, 1, high, 1)));
5597 31776 : low = high = 0;
5598 31776 : exp = 0;
5599 : }
5600 :
5601 50500128 : *pin_p = in_p, *plow = low, *phigh = high;
5602 50500128 : return exp;
5603 : }
5604 :
5605 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5606 : a bitwise check i.e. when
5607 : LOW == 0xXX...X00...0
5608 : HIGH == 0xXX...X11...1
5609 : Return corresponding mask in MASK and stem in VALUE. */
5610 :
5611 : static bool
5612 112 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5613 : tree *value)
5614 : {
5615 112 : if (TREE_CODE (low) != INTEGER_CST
5616 112 : || TREE_CODE (high) != INTEGER_CST)
5617 : return false;
5618 :
5619 112 : unsigned prec = TYPE_PRECISION (type);
5620 112 : wide_int lo = wi::to_wide (low, prec);
5621 112 : wide_int hi = wi::to_wide (high, prec);
5622 :
5623 112 : wide_int end_mask = lo ^ hi;
5624 224 : if ((end_mask & (end_mask + 1)) != 0
5625 222 : || (lo & end_mask) != 0)
5626 : return false;
5627 :
5628 86 : wide_int stem_mask = ~end_mask;
5629 86 : wide_int stem = lo & stem_mask;
5630 86 : if (stem != (hi & stem_mask))
5631 : return false;
5632 :
5633 86 : *mask = wide_int_to_tree (type, stem_mask);
5634 86 : *value = wide_int_to_tree (type, stem);
5635 :
5636 86 : return true;
5637 198 : }
5638 :
5639 : /* Helper routine for build_range_check and match.pd. Return the type to
5640 : perform the check or NULL if it shouldn't be optimized. */
5641 :
5642 : tree
5643 595975 : range_check_type (tree etype)
5644 : {
5645 : /* First make sure that arithmetics in this type is valid, then make sure
5646 : that it wraps around. */
5647 595975 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5648 0 : etype = TREE_TYPE (etype);
5649 595975 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5650 61741 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5651 :
5652 595975 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5653 : {
5654 409420 : tree utype, minv, maxv;
5655 :
5656 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5657 : for the type in question, as we rely on this here. */
5658 409420 : utype = unsigned_type_for (etype);
5659 409420 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5660 409420 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5661 409420 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5662 409420 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5663 :
5664 409420 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5665 : minv, 1, maxv, 1)))
5666 : etype = utype;
5667 : else
5668 109 : return NULL_TREE;
5669 : }
5670 186555 : else if (POINTER_TYPE_P (etype)
5671 : || TREE_CODE (etype) == OFFSET_TYPE
5672 : /* Right now all BITINT_TYPEs satisfy
5673 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5674 : that like for INTEGER_TYPEs. */
5675 : || TREE_CODE (etype) == BITINT_TYPE)
5676 1364 : etype = unsigned_type_for (etype);
5677 : return etype;
5678 : }
5679 :
5680 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5681 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5682 : on IN_P) the range. Return 0 if the test couldn't be created. */
5683 :
5684 : tree
5685 1621768 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5686 : tree low, tree high)
5687 : {
5688 2835711 : tree etype = TREE_TYPE (exp), mask, value;
5689 :
5690 : /* Disable this optimization for function pointer expressions
5691 : on targets that require function pointer canonicalization. */
5692 2835711 : if (targetm.have_canonicalize_funcptr_for_compare ()
5693 0 : && POINTER_TYPE_P (etype)
5694 2835711 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5695 : return NULL_TREE;
5696 :
5697 2835711 : if (! in_p)
5698 : {
5699 312804 : value = build_range_check (loc, type, exp, 1, low, high);
5700 312804 : if (value != 0)
5701 312804 : return invert_truthvalue_loc (loc, value);
5702 :
5703 : return 0;
5704 : }
5705 :
5706 2522907 : if (low == 0 && high == 0)
5707 123316 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5708 :
5709 2399591 : if (low == 0)
5710 788326 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5711 788326 : fold_convert_loc (loc, etype, high));
5712 :
5713 1611265 : if (high == 0)
5714 73665 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5715 73665 : fold_convert_loc (loc, etype, low));
5716 :
5717 1537600 : if (operand_equal_p (low, high, 0))
5718 323417 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5719 323417 : fold_convert_loc (loc, etype, low));
5720 :
5721 1214183 : if (TREE_CODE (exp) == BIT_AND_EXPR
5722 1214183 : && maskable_range_p (low, high, etype, &mask, &value))
5723 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5724 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5725 : exp, mask),
5726 86 : value);
5727 :
5728 1214097 : if (integer_zerop (low))
5729 : {
5730 709180 : if (! TYPE_UNSIGNED (etype))
5731 : {
5732 186160 : etype = unsigned_type_for (etype);
5733 186160 : high = fold_convert_loc (loc, etype, high);
5734 186160 : exp = fold_convert_loc (loc, etype, exp);
5735 : }
5736 : return build_range_check (loc, type, exp, 1, 0, high);
5737 : }
5738 :
5739 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5740 504917 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5741 : {
5742 130146 : int prec = TYPE_PRECISION (etype);
5743 :
5744 130146 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5745 : {
5746 124 : if (TYPE_UNSIGNED (etype))
5747 : {
5748 118 : tree signed_etype = signed_type_for (etype);
5749 118 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5750 0 : etype
5751 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5752 : else
5753 : etype = signed_etype;
5754 118 : exp = fold_convert_loc (loc, etype, exp);
5755 : }
5756 124 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5757 : build_int_cst (etype, 0));
5758 : }
5759 : }
5760 :
5761 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5762 : This requires wrap-around arithmetics for the type of the expression. */
5763 504793 : etype = range_check_type (etype);
5764 504793 : if (etype == NULL_TREE)
5765 : return NULL_TREE;
5766 :
5767 504763 : high = fold_convert_loc (loc, etype, high);
5768 504763 : low = fold_convert_loc (loc, etype, low);
5769 504763 : exp = fold_convert_loc (loc, etype, exp);
5770 :
5771 504763 : value = const_binop (MINUS_EXPR, high, low);
5772 :
5773 504763 : if (value != 0 && !TREE_OVERFLOW (value))
5774 504763 : return build_range_check (loc, type,
5775 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5776 : 1, build_int_cst (etype, 0), value);
5777 :
5778 : return 0;
5779 : }
5780 :
5781 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5782 :
5783 : static tree
5784 175258 : range_predecessor (tree val)
5785 : {
5786 175258 : tree type = TREE_TYPE (val);
5787 :
5788 175258 : if (INTEGRAL_TYPE_P (type)
5789 175258 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5790 : return 0;
5791 : else
5792 175258 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5793 175258 : build_int_cst (TREE_TYPE (val), 1), 0);
5794 : }
5795 :
5796 : /* Return the successor of VAL in its type, handling the infinite case. */
5797 :
5798 : static tree
5799 1677111 : range_successor (tree val)
5800 : {
5801 1677111 : tree type = TREE_TYPE (val);
5802 :
5803 1677111 : if (INTEGRAL_TYPE_P (type)
5804 1677111 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5805 : return 0;
5806 : else
5807 1677102 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5808 1677102 : build_int_cst (TREE_TYPE (val), 1), 0);
5809 : }
5810 :
5811 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5812 : can, 0 if we can't. Set the output range into the specified parameters. */
5813 :
5814 : bool
5815 3666872 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5816 : tree high0, int in1_p, tree low1, tree high1)
5817 : {
5818 3666872 : bool no_overlap;
5819 3666872 : int subset;
5820 3666872 : int temp;
5821 3666872 : tree tem;
5822 3666872 : int in_p;
5823 3666872 : tree low, high;
5824 3666872 : int lowequal = ((low0 == 0 && low1 == 0)
5825 3666872 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5826 3666872 : low0, 0, low1, 0)));
5827 3666872 : int highequal = ((high0 == 0 && high1 == 0)
5828 3666872 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5829 3666872 : high0, 1, high1, 1)));
5830 :
5831 : /* Make range 0 be the range that starts first, or ends last if they
5832 : start at the same value. Swap them if it isn't. */
5833 3666872 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5834 : low0, 0, low1, 0))
5835 3666872 : || (lowequal
5836 577895 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5837 : high1, 1, high0, 1))))
5838 : {
5839 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5840 : tem = low0, low0 = low1, low1 = tem;
5841 : tem = high0, high0 = high1, high1 = tem;
5842 : }
5843 :
5844 : /* If the second range is != high1 where high1 is the type maximum of
5845 : the type, try first merging with < high1 range. */
5846 3666872 : if (low1
5847 3666872 : && high1
5848 1011526 : && TREE_CODE (low1) == INTEGER_CST
5849 1011526 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5850 128730 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5851 172672 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5852 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5853 4636004 : && operand_equal_p (low1, high1, 0))
5854 : {
5855 563541 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5856 563541 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5857 : !in1_p, NULL_TREE, range_predecessor (low1)))
5858 : return true;
5859 : /* Similarly for the second range != low1 where low1 is the type minimum
5860 : of the type, try first merging with > low1 range. */
5861 452937 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5862 452937 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5863 : !in1_p, range_successor (low1), NULL_TREE))
5864 : return true;
5865 : }
5866 :
5867 : /* Now flag two cases, whether the ranges are disjoint or whether the
5868 : second range is totally subsumed in the first. Note that the tests
5869 : below are simplified by the ones above. */
5870 3461070 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5871 : high0, 1, low1, 0));
5872 3461070 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5873 : high1, 1, high0, 1));
5874 :
5875 : /* We now have four cases, depending on whether we are including or
5876 : excluding the two ranges. */
5877 3461070 : if (in0_p && in1_p)
5878 : {
5879 : /* If they don't overlap, the result is false. If the second range
5880 : is a subset it is the result. Otherwise, the range is from the start
5881 : of the second to the end of the first. */
5882 1583577 : if (no_overlap)
5883 : in_p = 0, low = high = 0;
5884 1582045 : else if (subset)
5885 : in_p = 1, low = low1, high = high1;
5886 : else
5887 1462259 : in_p = 1, low = low1, high = high0;
5888 : }
5889 :
5890 1877493 : else if (in0_p && ! in1_p)
5891 : {
5892 : /* If they don't overlap, the result is the first range. If they are
5893 : equal, the result is false. If the second range is a subset of the
5894 : first, and the ranges begin at the same place, we go from just after
5895 : the end of the second range to the end of the first. If the second
5896 : range is not a subset of the first, or if it is a subset and both
5897 : ranges end at the same place, the range starts at the start of the
5898 : first range and ends just before the second range.
5899 : Otherwise, we can't describe this as a single range. */
5900 326824 : if (no_overlap)
5901 : in_p = 1, low = low0, high = high0;
5902 321285 : else if (lowequal && highequal)
5903 : in_p = 0, low = high = 0;
5904 320444 : else if (subset && lowequal)
5905 : {
5906 244817 : low = range_successor (high1);
5907 244817 : high = high0;
5908 244817 : in_p = 1;
5909 244817 : if (low == 0)
5910 : {
5911 : /* We are in the weird situation where high0 > high1 but
5912 : high1 has no successor. Punt. */
5913 : return 0;
5914 : }
5915 : }
5916 75627 : else if (! subset || highequal)
5917 : {
5918 55049 : low = low0;
5919 55049 : high = range_predecessor (low1);
5920 55049 : in_p = 1;
5921 55049 : if (high == 0)
5922 : {
5923 : /* low0 < low1 but low1 has no predecessor. Punt. */
5924 : return 0;
5925 : }
5926 : }
5927 : else
5928 : return 0;
5929 : }
5930 :
5931 1550669 : else if (! in0_p && in1_p)
5932 : {
5933 : /* If they don't overlap, the result is the second range. If the second
5934 : is a subset of the first, the result is false. Otherwise,
5935 : the range starts just after the first range and ends at the
5936 : end of the second. */
5937 1189336 : if (no_overlap)
5938 : in_p = 1, low = low1, high = high1;
5939 1181518 : else if (subset || highequal)
5940 : in_p = 0, low = high = 0;
5941 : else
5942 : {
5943 1072316 : low = range_successor (high0);
5944 1072316 : high = high1;
5945 1072316 : in_p = 1;
5946 1072316 : if (low == 0)
5947 : {
5948 : /* high1 > high0 but high0 has no successor. Punt. */
5949 : return 0;
5950 : }
5951 : }
5952 : }
5953 :
5954 : else
5955 : {
5956 : /* The case where we are excluding both ranges. Here the complex case
5957 : is if they don't overlap. In that case, the only time we have a
5958 : range is if they are adjacent. If the second is a subset of the
5959 : first, the result is the first. Otherwise, the range to exclude
5960 : starts at the beginning of the first range and ends at the end of the
5961 : second. */
5962 361333 : if (no_overlap)
5963 : {
5964 264456 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5965 : range_successor (high0),
5966 : 1, low1, 0)))
5967 : in_p = 0, low = low0, high = high1;
5968 : else
5969 : {
5970 : /* Canonicalize - [min, x] into - [-, x]. */
5971 212712 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5972 211569 : switch (TREE_CODE (TREE_TYPE (low0)))
5973 : {
5974 51676 : case ENUMERAL_TYPE:
5975 51676 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5976 : GET_MODE_BITSIZE
5977 103352 : (TYPE_MODE (TREE_TYPE (low0)))))
5978 : break;
5979 : /* FALLTHROUGH */
5980 211368 : case INTEGER_TYPE:
5981 211368 : if (tree_int_cst_equal (low0,
5982 211368 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5983 212712 : low0 = 0;
5984 : break;
5985 201 : case POINTER_TYPE:
5986 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5987 201 : && integer_zerop (low0))
5988 : low0 = 0;
5989 : break;
5990 : default:
5991 : break;
5992 : }
5993 :
5994 : /* Canonicalize - [x, max] into - [x, -]. */
5995 212712 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5996 212525 : switch (TREE_CODE (TREE_TYPE (high1)))
5997 : {
5998 51684 : case ENUMERAL_TYPE:
5999 51684 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
6000 : GET_MODE_BITSIZE
6001 103368 : (TYPE_MODE (TREE_TYPE (high1)))))
6002 : break;
6003 : /* FALLTHROUGH */
6004 212324 : case INTEGER_TYPE:
6005 212324 : if (tree_int_cst_equal (high1,
6006 212324 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
6007 212712 : high1 = 0;
6008 : break;
6009 201 : case POINTER_TYPE:
6010 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
6011 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
6012 : high1, 1,
6013 201 : build_int_cst (TREE_TYPE (high1), 1),
6014 : 1)))
6015 133 : high1 = 0;
6016 : break;
6017 : default:
6018 : break;
6019 : }
6020 :
6021 : /* The ranges might be also adjacent between the maximum and
6022 : minimum values of the given type. For
6023 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6024 : return + [x + 1, y - 1]. */
6025 212712 : if (low0 == 0 && high1 == 0)
6026 : {
6027 324 : low = range_successor (high0);
6028 324 : high = range_predecessor (low1);
6029 324 : if (low == 0 || high == 0)
6030 : return 0;
6031 :
6032 : in_p = 1;
6033 : }
6034 : else
6035 : return 0;
6036 : }
6037 : }
6038 96877 : else if (subset)
6039 : in_p = 0, low = low0, high = high0;
6040 : else
6041 12002 : in_p = 0, low = low0, high = high1;
6042 : }
6043 :
6044 3228095 : *pin_p = in_p, *plow = low, *phigh = high;
6045 3228095 : return 1;
6046 : }
6047 :
6048 :
6049 : /* Subroutine of fold, looking inside expressions of the form
6050 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6051 : are the three operands of the COND_EXPR. This function is
6052 : being used also to optimize A op B ? C : A, by reversing the
6053 : comparison first.
6054 :
6055 : Return a folded expression whose code is not a COND_EXPR
6056 : anymore, or NULL_TREE if no folding opportunity is found. */
6057 :
6058 : static tree
6059 519556 : fold_cond_expr_with_comparison (location_t loc, tree type,
6060 : enum tree_code comp_code,
6061 : tree arg00, tree arg01, tree arg1, tree arg2)
6062 : {
6063 519556 : tree arg1_type = TREE_TYPE (arg1);
6064 519556 : tree tem;
6065 :
6066 519556 : STRIP_NOPS (arg1);
6067 519556 : STRIP_NOPS (arg2);
6068 :
6069 : /* If we have A op 0 ? A : -A, consider applying the following
6070 : transformations:
6071 :
6072 : A == 0? A : -A same as -A
6073 : A != 0? A : -A same as A
6074 : A >= 0? A : -A same as abs (A)
6075 : A > 0? A : -A same as abs (A)
6076 : A <= 0? A : -A same as -abs (A)
6077 : A < 0? A : -A same as -abs (A)
6078 :
6079 : None of these transformations work for modes with signed
6080 : zeros. If A is +/-0, the first two transformations will
6081 : change the sign of the result (from +0 to -0, or vice
6082 : versa). The last four will fix the sign of the result,
6083 : even though the original expressions could be positive or
6084 : negative, depending on the sign of A.
6085 :
6086 : Note that all these transformations are correct if A is
6087 : NaN, since the two alternatives (A and -A) are also NaNs. */
6088 519556 : if (!HONOR_SIGNED_ZEROS (type)
6089 1039122 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6090 519556 : ? real_zerop (arg01)
6091 518464 : : integer_zerop (arg01))
6092 1392906 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6093 1647 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6094 : /* In the case that A is of the form X-Y, '-A' (arg2) may
6095 : have already been folded to Y-X, check for that. */
6096 352371 : || (TREE_CODE (arg1) == MINUS_EXPR
6097 1711 : && TREE_CODE (arg2) == MINUS_EXPR
6098 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6099 0 : TREE_OPERAND (arg2, 1), 0)
6100 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6101 0 : TREE_OPERAND (arg2, 0), 0))))
6102 1423 : switch (comp_code)
6103 : {
6104 0 : case EQ_EXPR:
6105 0 : case UNEQ_EXPR:
6106 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6107 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6108 0 : case NE_EXPR:
6109 0 : case LTGT_EXPR:
6110 0 : return fold_convert_loc (loc, type, arg1);
6111 0 : case UNGE_EXPR:
6112 0 : case UNGT_EXPR:
6113 0 : if (flag_trapping_math)
6114 : break;
6115 : /* Fall through. */
6116 1407 : case GE_EXPR:
6117 1407 : case GT_EXPR:
6118 1407 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6119 : break;
6120 1391 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6121 1391 : return fold_convert_loc (loc, type, tem);
6122 0 : case UNLE_EXPR:
6123 0 : case UNLT_EXPR:
6124 0 : if (flag_trapping_math)
6125 : break;
6126 : /* FALLTHRU */
6127 16 : case LE_EXPR:
6128 16 : case LT_EXPR:
6129 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6130 : break;
6131 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6132 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6133 : {
6134 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6135 : is not, invokes UB both in abs and in the negation of it.
6136 : So, use ABSU_EXPR instead. */
6137 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6138 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6139 16 : tem = negate_expr (tem);
6140 16 : return fold_convert_loc (loc, type, tem);
6141 : }
6142 : else
6143 : {
6144 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6145 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6146 : }
6147 0 : default:
6148 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6149 : break;
6150 : }
6151 :
6152 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6153 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6154 : both transformations are correct when A is NaN: A != 0
6155 : is then true, and A == 0 is false. */
6156 :
6157 518149 : if (!HONOR_SIGNED_ZEROS (type)
6158 518149 : && integer_zerop (arg01) && integer_zerop (arg2))
6159 : {
6160 266838 : if (comp_code == NE_EXPR)
6161 147 : return fold_convert_loc (loc, type, arg1);
6162 266691 : else if (comp_code == EQ_EXPR)
6163 0 : return build_zero_cst (type);
6164 : }
6165 :
6166 : /* Try some transformations of A op B ? A : B.
6167 :
6168 : A == B? A : B same as B
6169 : A != B? A : B same as A
6170 : A >= B? A : B same as max (A, B)
6171 : A > B? A : B same as max (B, A)
6172 : A <= B? A : B same as min (A, B)
6173 : A < B? A : B same as min (B, A)
6174 :
6175 : As above, these transformations don't work in the presence
6176 : of signed zeros. For example, if A and B are zeros of
6177 : opposite sign, the first two transformations will change
6178 : the sign of the result. In the last four, the original
6179 : expressions give different results for (A=+0, B=-0) and
6180 : (A=-0, B=+0), but the transformed expressions do not.
6181 :
6182 : The first two transformations are correct if either A or B
6183 : is a NaN. In the first transformation, the condition will
6184 : be false, and B will indeed be chosen. In the case of the
6185 : second transformation, the condition A != B will be true,
6186 : and A will be chosen.
6187 :
6188 : The conversions to max() and min() are not correct if B is
6189 : a number and A is not. The conditions in the original
6190 : expressions will be false, so all four give B. The min()
6191 : and max() versions would give a NaN instead. */
6192 518002 : if (!HONOR_SIGNED_ZEROS (type)
6193 518002 : && operand_equal_for_comparison_p (arg01, arg2)
6194 : /* Avoid these transformations if the COND_EXPR may be used
6195 : as an lvalue in the C++ front-end. PR c++/19199. */
6196 793741 : && (in_gimple_form
6197 18077 : || VECTOR_TYPE_P (type)
6198 18004 : || (! lang_GNU_CXX ()
6199 15517 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6200 2487 : || ! maybe_lvalue_p (arg1)
6201 2466 : || ! maybe_lvalue_p (arg2)))
6202 : {
6203 274010 : tree comp_op0 = arg00;
6204 274010 : tree comp_op1 = arg01;
6205 274010 : tree comp_type = TREE_TYPE (comp_op0);
6206 :
6207 274010 : switch (comp_code)
6208 : {
6209 0 : case EQ_EXPR:
6210 0 : return fold_convert_loc (loc, type, arg2);
6211 1 : case NE_EXPR:
6212 1 : return fold_convert_loc (loc, type, arg1);
6213 6162 : case LE_EXPR:
6214 6162 : case LT_EXPR:
6215 6162 : case UNLE_EXPR:
6216 6162 : case UNLT_EXPR:
6217 : /* In C++ a ?: expression can be an lvalue, so put the
6218 : operand which will be used if they are equal first
6219 : so that we can convert this back to the
6220 : corresponding COND_EXPR. */
6221 6162 : if (!HONOR_NANS (arg1))
6222 : {
6223 6162 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6224 6162 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6225 6162 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6226 6162 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6227 4779 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6228 : comp_op1, comp_op0);
6229 6162 : return fold_convert_loc (loc, type, tem);
6230 : }
6231 : break;
6232 267847 : case GE_EXPR:
6233 267847 : case GT_EXPR:
6234 267847 : case UNGE_EXPR:
6235 267847 : case UNGT_EXPR:
6236 267847 : if (!HONOR_NANS (arg1))
6237 : {
6238 267845 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6239 267845 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6240 267845 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6241 267845 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6242 3763 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6243 : comp_op1, comp_op0);
6244 267845 : return fold_convert_loc (loc, type, tem);
6245 : }
6246 : break;
6247 0 : case UNEQ_EXPR:
6248 0 : if (!HONOR_NANS (arg1))
6249 0 : return fold_convert_loc (loc, type, arg2);
6250 : break;
6251 0 : case LTGT_EXPR:
6252 0 : if (!HONOR_NANS (arg1))
6253 0 : return fold_convert_loc (loc, type, arg1);
6254 : break;
6255 0 : default:
6256 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6257 : break;
6258 : }
6259 : }
6260 :
6261 : return NULL_TREE;
6262 : }
6263 :
6264 :
6265 :
6266 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6267 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6268 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6269 : false) >= 2)
6270 : #endif
6271 :
6272 : /* EXP is some logical combination of boolean tests. See if we can
6273 : merge it into some range test. Return the new tree if so. */
6274 :
6275 : static tree
6276 25249558 : fold_range_test (location_t loc, enum tree_code code, tree type,
6277 : tree op0, tree op1)
6278 : {
6279 25249558 : int or_op = (code == TRUTH_ORIF_EXPR
6280 25249558 : || code == TRUTH_OR_EXPR);
6281 25249558 : int in0_p, in1_p, in_p;
6282 25249558 : tree low0, low1, low, high0, high1, high;
6283 25249558 : tree tem, lhs, rhs;
6284 :
6285 25249558 : if (!INTEGRAL_TYPE_P (type))
6286 : return 0;
6287 :
6288 25249558 : lhs = make_range (op0, &in0_p, &low0, &high0);
6289 : /* If op0 is known true or false and this is a short-circuiting
6290 : operation we must not merge with op1 since that makes side-effects
6291 : unconditional. So special-case this. */
6292 25249558 : if (!lhs
6293 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6294 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6295 : return op0;
6296 25249556 : rhs = make_range (op1, &in1_p, &low1, &high1);
6297 :
6298 : /* If this is an OR operation, invert both sides; we will invert
6299 : again at the end. */
6300 25249556 : if (or_op)
6301 11871124 : in0_p = ! in0_p, in1_p = ! in1_p;
6302 :
6303 : /* If both expressions are the same, if we can merge the ranges, and we
6304 : can build the range test, return it or it inverted. If one of the
6305 : ranges is always true or always false, consider it to be the same
6306 : expression as the other. */
6307 25217784 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6308 1207883 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6309 : in1_p, low1, high1)
6310 26255235 : && (tem = (build_range_check (loc, type,
6311 : lhs != 0 ? lhs
6312 0 : : rhs != 0 ? rhs : integer_zero_node,
6313 : in_p, low, high))) != 0)
6314 : {
6315 1005649 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6316 : }
6317 :
6318 : /* On machines where the branch cost is expensive, if this is a
6319 : short-circuited branch and the underlying object on both sides
6320 : is the same, make a non-short-circuit operation. */
6321 24243907 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6322 24243907 : if (param_logical_op_non_short_circuit != -1)
6323 7836 : logical_op_non_short_circuit
6324 7836 : = param_logical_op_non_short_circuit;
6325 24243907 : if (logical_op_non_short_circuit
6326 24239966 : && !sanitize_coverage_p ()
6327 24239963 : && lhs != 0 && rhs != 0
6328 24239524 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6329 29853545 : && operand_equal_p (lhs, rhs, 0))
6330 : {
6331 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6332 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6333 : which cases we can't do this. */
6334 172430 : if (simple_operand_p (lhs))
6335 69838 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6336 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6337 35440 : type, op0, op1);
6338 :
6339 136990 : else if (!lang_hooks.decls.global_bindings_p ()
6340 136990 : && !CONTAINS_PLACEHOLDER_P (lhs))
6341 : {
6342 136337 : tree common = save_expr (lhs);
6343 :
6344 250859 : if ((lhs = build_range_check (loc, type, common,
6345 114522 : or_op ? ! in0_p : in0_p,
6346 : low0, high0)) != 0
6347 250859 : && (rhs = build_range_check (loc, type, common,
6348 114522 : or_op ? ! in1_p : in1_p,
6349 : low1, high1)) != 0)
6350 : {
6351 250859 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6352 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6353 136337 : type, lhs, rhs);
6354 : }
6355 : }
6356 : }
6357 :
6358 : return 0;
6359 : }
6360 :
6361 : /* For an expression that has the form
6362 : (A && B) || ~B
6363 : or
6364 : (A || B) && ~B,
6365 : we can drop one of the inner expressions and simplify to
6366 : A || ~B
6367 : or
6368 : A && ~B
6369 : LOC is the location of the resulting expression. OP is the inner
6370 : logical operation; the left-hand side in the examples above, while CMPOP
6371 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6372 : removing a condition that guards another, as in
6373 : (A != NULL && A->...) || A == NULL
6374 : which we must not transform. If RHS_ONLY is true, only eliminate the
6375 : right-most operand of the inner logical operation. */
6376 :
6377 : static tree
6378 130882 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6379 : bool rhs_only)
6380 : {
6381 130882 : enum tree_code code = TREE_CODE (cmpop);
6382 130882 : enum tree_code truthop_code = TREE_CODE (op);
6383 130882 : tree lhs = TREE_OPERAND (op, 0);
6384 130882 : tree rhs = TREE_OPERAND (op, 1);
6385 130882 : tree orig_lhs = lhs, orig_rhs = rhs;
6386 130882 : enum tree_code rhs_code = TREE_CODE (rhs);
6387 130882 : enum tree_code lhs_code = TREE_CODE (lhs);
6388 130882 : enum tree_code inv_code;
6389 :
6390 130882 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6391 : return NULL_TREE;
6392 :
6393 117231 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6394 : return NULL_TREE;
6395 :
6396 39038 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6397 :
6398 39038 : if (rhs_code == truthop_code)
6399 : {
6400 37 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6401 37 : if (newrhs != NULL_TREE)
6402 : {
6403 0 : rhs = newrhs;
6404 0 : rhs_code = TREE_CODE (rhs);
6405 : }
6406 : }
6407 39038 : if (lhs_code == truthop_code && !rhs_only)
6408 : {
6409 480 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6410 480 : if (newlhs != NULL_TREE)
6411 : {
6412 0 : lhs = newlhs;
6413 0 : lhs_code = TREE_CODE (lhs);
6414 : }
6415 : }
6416 :
6417 39038 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6418 39038 : if (inv_code == rhs_code
6419 932 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6420 39074 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6421 : return lhs;
6422 39025 : if (!rhs_only && inv_code == lhs_code
6423 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6424 39117 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6425 : return rhs;
6426 38934 : if (rhs != orig_rhs || lhs != orig_lhs)
6427 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6428 0 : lhs, rhs);
6429 : return NULL_TREE;
6430 : }
6431 :
6432 : /* Find ways of folding logical expressions of LHS and RHS:
6433 : Try to merge two comparisons to the same innermost item.
6434 : Look for range tests like "ch >= '0' && ch <= '9'".
6435 : Look for combinations of simple terms on machines with expensive branches
6436 : and evaluate the RHS unconditionally.
6437 :
6438 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6439 : function and the one above.
6440 :
6441 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6442 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6443 :
6444 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6445 : two operands.
6446 :
6447 : We return the simplified tree or 0 if no optimization is possible. */
6448 :
6449 : static tree
6450 24924875 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6451 : tree lhs, tree rhs)
6452 : {
6453 : /* If this is the "or" of two comparisons, we can do something if
6454 : the comparisons are NE_EXPR. If this is the "and", we can do something
6455 : if the comparisons are EQ_EXPR. I.e.,
6456 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6457 :
6458 : WANTED_CODE is this operation code. For single bit fields, we can
6459 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6460 : comparison for one-bit fields. */
6461 :
6462 24924875 : enum tree_code lcode, rcode;
6463 24924875 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6464 24924875 : tree result;
6465 :
6466 : /* Start by getting the comparison codes. Fail if anything is volatile.
6467 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6468 : it were surrounded with a NE_EXPR. */
6469 :
6470 24924875 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6471 : return 0;
6472 :
6473 21999170 : lcode = TREE_CODE (lhs);
6474 21999170 : rcode = TREE_CODE (rhs);
6475 :
6476 21999170 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6477 : {
6478 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6479 0 : build_int_cst (TREE_TYPE (lhs), 0));
6480 0 : lcode = NE_EXPR;
6481 : }
6482 :
6483 21999170 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6484 : {
6485 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6486 0 : build_int_cst (TREE_TYPE (rhs), 0));
6487 0 : rcode = NE_EXPR;
6488 : }
6489 :
6490 21999170 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6491 19645448 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6492 : return 0;
6493 :
6494 18558506 : ll_arg = TREE_OPERAND (lhs, 0);
6495 18558506 : lr_arg = TREE_OPERAND (lhs, 1);
6496 18558506 : rl_arg = TREE_OPERAND (rhs, 0);
6497 18558506 : rr_arg = TREE_OPERAND (rhs, 1);
6498 :
6499 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6500 18558506 : if (simple_operand_p (ll_arg)
6501 18558506 : && simple_operand_p (lr_arg))
6502 : {
6503 15041083 : if (operand_equal_p (ll_arg, rl_arg, 0)
6504 15041083 : && operand_equal_p (lr_arg, rr_arg, 0))
6505 : {
6506 21111 : result = combine_comparisons (loc, code, lcode, rcode,
6507 : truth_type, ll_arg, lr_arg);
6508 21111 : if (result)
6509 : return result;
6510 : }
6511 15019972 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6512 15019972 : && operand_equal_p (lr_arg, rl_arg, 0))
6513 : {
6514 252 : result = combine_comparisons (loc, code, lcode,
6515 : swap_tree_comparison (rcode),
6516 : truth_type, ll_arg, lr_arg);
6517 252 : if (result)
6518 : return result;
6519 : }
6520 : }
6521 :
6522 8758123 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6523 18537583 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6524 :
6525 : /* If the RHS can be evaluated unconditionally and its operands are
6526 : simple, it wins to evaluate the RHS unconditionally on machines
6527 : with expensive branches. In this case, this isn't a comparison
6528 : that can be merged. */
6529 :
6530 18537583 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6531 : false) >= 2
6532 18537484 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6533 17482977 : && simple_operand_p (rl_arg)
6534 28457492 : && simple_operand_p (rr_arg))
6535 : {
6536 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6537 11108015 : if (code == TRUTH_OR_EXPR
6538 1503634 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6539 619165 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6540 21164 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6541 11125639 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6542 34628 : return build2_loc (loc, NE_EXPR, truth_type,
6543 17314 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6544 : ll_arg, rl_arg),
6545 17314 : build_int_cst (TREE_TYPE (ll_arg), 0));
6546 :
6547 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6548 11090701 : if (code == TRUTH_AND_EXPR
6549 1703382 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6550 823512 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6551 8156 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6552 11092322 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6553 2800 : return build2_loc (loc, EQ_EXPR, truth_type,
6554 1400 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6555 : ll_arg, rl_arg),
6556 1400 : build_int_cst (TREE_TYPE (ll_arg), 0));
6557 : }
6558 :
6559 : return 0;
6560 : }
6561 :
6562 : /* T is an integer expression that is being multiplied, divided, or taken a
6563 : modulus (CODE says which and what kind of divide or modulus) by a
6564 : constant C. See if we can eliminate that operation by folding it with
6565 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6566 : should be used for the computation if wider than our type.
6567 :
6568 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6569 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6570 : expression would not overflow or that overflow is undefined for the type
6571 : in the language in question.
6572 :
6573 : If we return a non-null expression, it is an equivalent form of the
6574 : original computation, but need not be in the original type. */
6575 :
6576 : static tree
6577 100389341 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6578 : {
6579 : /* To avoid exponential search depth, refuse to allow recursion past
6580 : three levels. Beyond that (1) it's highly unlikely that we'll find
6581 : something interesting and (2) we've probably processed it before
6582 : when we built the inner expression. */
6583 :
6584 100389341 : static int depth;
6585 100389341 : tree ret;
6586 :
6587 100389341 : if (depth > 3)
6588 : return NULL;
6589 :
6590 96701416 : depth++;
6591 96701416 : ret = extract_muldiv_1 (t, c, code, wide_type);
6592 96701416 : depth--;
6593 :
6594 96701416 : return ret;
6595 : }
6596 :
6597 : static tree
6598 96701416 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6599 : {
6600 96701416 : tree type = TREE_TYPE (t);
6601 96701416 : enum tree_code tcode = TREE_CODE (t);
6602 96701416 : tree ctype = type;
6603 96701416 : if (wide_type)
6604 : {
6605 32167119 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6606 : {
6607 177 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6608 96701416 : ctype = wide_type;
6609 : }
6610 32166942 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6611 64333884 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6612 96701416 : ctype = wide_type;
6613 : }
6614 96701416 : tree t1, t2;
6615 96701416 : bool same_p = tcode == code;
6616 96701416 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6617 :
6618 : /* Don't deal with constants of zero here; they confuse the code below. */
6619 96701416 : if (integer_zerop (c))
6620 : return NULL_TREE;
6621 :
6622 96685827 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6623 38371217 : op0 = TREE_OPERAND (t, 0);
6624 :
6625 96685827 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6626 12210428 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6627 :
6628 : /* Note that we need not handle conditional operations here since fold
6629 : already handles those cases. So just do arithmetic here. */
6630 96685827 : switch (tcode)
6631 : {
6632 4362251 : case INTEGER_CST:
6633 : /* For a constant, we can always simplify if we are a multiply
6634 : or (for divide and modulus) if it is a multiple of our constant. */
6635 4362251 : if (code == MULT_EXPR
6636 5601839 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6637 1239588 : TYPE_SIGN (type)))
6638 : {
6639 3562520 : tree tem = const_binop (code, fold_convert (ctype, t),
6640 : fold_convert (ctype, c));
6641 : /* If the multiplication overflowed, we lost information on it.
6642 : See PR68142 and PR69845. */
6643 3562520 : if (TREE_OVERFLOW (tem))
6644 : return NULL_TREE;
6645 3558997 : return tem;
6646 : }
6647 : break;
6648 :
6649 37783111 : CASE_CONVERT: case NON_LVALUE_EXPR:
6650 37783111 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6651 : break;
6652 : /* If op0 is an expression ... */
6653 36462724 : if ((COMPARISON_CLASS_P (op0)
6654 : || UNARY_CLASS_P (op0)
6655 36462724 : || BINARY_CLASS_P (op0)
6656 33457285 : || VL_EXP_CLASS_P (op0)
6657 33395422 : || EXPRESSION_CLASS_P (op0))
6658 : /* ... and has wrapping overflow, and its type is smaller
6659 : than ctype, then we cannot pass through as widening. */
6660 36615734 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6661 1234911 : && (TYPE_PRECISION (ctype)
6662 1234911 : > TYPE_PRECISION (TREE_TYPE (op0))))
6663 : /* ... or this is a truncation (t is narrower than op0),
6664 : then we cannot pass through this narrowing. */
6665 2600108 : || (TYPE_PRECISION (type)
6666 2600108 : < TYPE_PRECISION (TREE_TYPE (op0)))
6667 : /* ... or signedness changes for division or modulus,
6668 : then we cannot pass through this conversion. */
6669 2571288 : || (code != MULT_EXPR
6670 126988 : && (TYPE_UNSIGNED (ctype)
6671 126988 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6672 : /* ... or has undefined overflow while the converted to
6673 : type has not, we cannot do the operation in the inner type
6674 : as that would introduce undefined overflow. */
6675 2471033 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6676 1815975 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6677 : break;
6678 :
6679 : /* Pass the constant down and see if we can make a simplification. If
6680 : we can, replace this expression with the inner simplification for
6681 : possible later conversion to our or some other type. */
6682 34026191 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6683 34026191 : && TREE_CODE (t2) == INTEGER_CST
6684 34026191 : && !TREE_OVERFLOW (t2)
6685 69293553 : && (t1 = extract_muldiv (op0, t2, code,
6686 : code == MULT_EXPR ? ctype : NULL_TREE))
6687 : != 0)
6688 : return t1;
6689 : break;
6690 :
6691 189 : case ABS_EXPR:
6692 : /* If widening the type changes it from signed to unsigned, then we
6693 : must avoid building ABS_EXPR itself as unsigned. */
6694 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6695 : {
6696 0 : tree cstype = (*signed_type_for) (ctype);
6697 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6698 : {
6699 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6700 0 : return fold_convert (ctype, t1);
6701 : }
6702 : break;
6703 : }
6704 : /* If the constant is negative, we cannot simplify this. */
6705 189 : if (tree_int_cst_sgn (c) == -1)
6706 : break;
6707 : /* FALLTHROUGH */
6708 46820 : case NEGATE_EXPR:
6709 : /* For division and modulus, type can't be unsigned, as e.g.
6710 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6711 : For signed types, even with wrapping overflow, this is fine. */
6712 46820 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6713 : break;
6714 45067 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6715 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6716 : break;
6717 :
6718 814 : case MIN_EXPR: case MAX_EXPR:
6719 : /* If widening the type changes the signedness, then we can't perform
6720 : this optimization as that changes the result. */
6721 814 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6722 : break;
6723 :
6724 : /* Punt for multiplication altogether.
6725 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6726 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6727 : 0U, the latter is 2U.
6728 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6729 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6730 : well defined 0, the latter invokes UB.
6731 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6732 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6733 : well defined 25, the latter invokes UB. */
6734 814 : if (code == MULT_EXPR)
6735 : break;
6736 : /* For division/modulo, punt on c being -1 for MAX, as
6737 : MAX (INT_MIN, 0) / -1 is not equivalent to
6738 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6739 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6740 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6741 : transformation won't make it worse. */
6742 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6743 : break;
6744 :
6745 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6746 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6747 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6748 : {
6749 0 : if (tree_int_cst_sgn (c) < 0)
6750 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6751 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6752 : fold_convert (ctype, t2));
6753 : }
6754 : break;
6755 :
6756 1268 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6757 : /* If the second operand is constant, this is a multiplication
6758 : or floor division, by a power of two, so we can treat it that
6759 : way unless the multiplier or divisor overflows. Signed
6760 : left-shift overflow is implementation-defined rather than
6761 : undefined in C90, so do not convert signed left shift into
6762 : multiplication. */
6763 1268 : if (TREE_CODE (op1) == INTEGER_CST
6764 1244 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6765 : /* const_binop may not detect overflow correctly,
6766 : so check for it explicitly here. */
6767 1126 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6768 1277 : wi::to_wide (op1))
6769 1117 : && (t1 = fold_convert (ctype,
6770 : const_binop (LSHIFT_EXPR, size_one_node,
6771 : op1))) != 0
6772 2385 : && !TREE_OVERFLOW (t1))
6773 2032 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6774 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6775 : ctype,
6776 : fold_convert (ctype, op0),
6777 : t1),
6778 1117 : c, code, wide_type);
6779 : break;
6780 :
6781 8430052 : case PLUS_EXPR: case MINUS_EXPR:
6782 : /* See if we can eliminate the operation on both sides. If we can, we
6783 : can return a new PLUS or MINUS. If we can't, the only remaining
6784 : cases where we can do anything are if the second operand is a
6785 : constant. */
6786 8430052 : t1 = extract_muldiv (op0, c, code, wide_type);
6787 8430052 : t2 = extract_muldiv (op1, c, code, wide_type);
6788 820069 : if (t1 != 0 && t2 != 0
6789 284357 : && TYPE_OVERFLOW_WRAPS (ctype)
6790 8705092 : && (code == MULT_EXPR
6791 : /* If not multiplication, we can only do this if both operands
6792 : are divisible by c. */
6793 0 : || (multiple_of_p (ctype, op0, c)
6794 0 : && multiple_of_p (ctype, op1, c))))
6795 : {
6796 275040 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6797 : fold_convert (ctype, t2));
6798 : }
6799 :
6800 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6801 : This simplifies the logic below. */
6802 8155012 : if (tcode == MINUS_EXPR)
6803 : {
6804 2225010 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6805 : /* If OP1 was not easily negatable, the constant may be OP0. */
6806 2225010 : if (TREE_CODE (op0) == INTEGER_CST)
6807 : {
6808 368669 : std::swap (op0, op1);
6809 368669 : std::swap (t1, t2);
6810 : }
6811 : }
6812 :
6813 8155012 : if (TREE_CODE (op1) != INTEGER_CST)
6814 : break;
6815 :
6816 : /* If either OP1 or C are negative, this optimization is not safe for
6817 : some of the division and remainder types while for others we need
6818 : to change the code. */
6819 3732822 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6820 : {
6821 177246 : if (code == CEIL_DIV_EXPR)
6822 : code = FLOOR_DIV_EXPR;
6823 177244 : else if (code == FLOOR_DIV_EXPR)
6824 : code = CEIL_DIV_EXPR;
6825 176807 : else if (code != MULT_EXPR
6826 176807 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6827 : break;
6828 : }
6829 :
6830 : /* If it's a multiply or a division/modulus operation of a multiple
6831 : of our constant, do the operation and verify it doesn't overflow. */
6832 3727295 : if (code == MULT_EXPR
6833 4962116 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6834 1234821 : TYPE_SIGN (type)))
6835 : {
6836 2928324 : op1 = const_binop (code, fold_convert (ctype, op1),
6837 : fold_convert (ctype, c));
6838 : /* We allow the constant to overflow with wrapping semantics. */
6839 2928324 : if (op1 == 0
6840 2928324 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6841 : break;
6842 : }
6843 : else
6844 : break;
6845 :
6846 : /* If we have an unsigned type, we cannot widen the operation since it
6847 : will change the result if the original computation overflowed. */
6848 2924803 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6849 : break;
6850 :
6851 : /* The last case is if we are a multiply. In that case, we can
6852 : apply the distributive law to commute the multiply and addition
6853 : if the multiplication of the constants doesn't overflow
6854 : and overflow is defined. With undefined overflow
6855 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6856 : But fold_plusminus_mult_expr would factor back any power-of-two
6857 : value so do not distribute in the first place in this case. */
6858 2924803 : if (code == MULT_EXPR
6859 2489712 : && TYPE_OVERFLOW_WRAPS (ctype)
6860 5079370 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6861 605393 : return fold_build2 (tcode, ctype,
6862 : fold_build2 (code, ctype,
6863 : fold_convert (ctype, op0),
6864 : fold_convert (ctype, c)),
6865 : op1);
6866 :
6867 : break;
6868 :
6869 2368382 : case MULT_EXPR:
6870 : /* We have a special case here if we are doing something like
6871 : (C * 8) % 4 since we know that's zero. */
6872 2368382 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6873 2368382 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6874 : /* If the multiplication can overflow we cannot optimize this. */
6875 10834 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6876 363 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6877 2379216 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6878 318 : TYPE_SIGN (type)))
6879 : {
6880 8 : return omit_one_operand (type, integer_zero_node, op0);
6881 : }
6882 :
6883 : /* ... fall through ... */
6884 :
6885 2661393 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6886 2661393 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6887 : /* If we can extract our operation from the LHS, do so and return a
6888 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6889 : do something only if the second operand is a constant. */
6890 2661393 : if (same_p
6891 2235910 : && TYPE_OVERFLOW_WRAPS (ctype)
6892 4725125 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6893 66868 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6894 : fold_convert (ctype, op1));
6895 2594525 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6896 2159560 : && TYPE_OVERFLOW_WRAPS (ctype)
6897 4581955 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6898 942506 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6899 : fold_convert (ctype, t1));
6900 1652019 : else if (TREE_CODE (op1) != INTEGER_CST)
6901 : return 0;
6902 :
6903 : /* If these are the same operation types, we can associate them
6904 : assuming no overflow. */
6905 625832 : if (tcode == code)
6906 : {
6907 200911 : bool overflow_p = false;
6908 200911 : wi::overflow_type overflow_mul;
6909 200911 : signop sign = TYPE_SIGN (ctype);
6910 200911 : unsigned prec = TYPE_PRECISION (ctype);
6911 401822 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6912 200911 : wi::to_wide (c, prec),
6913 200911 : sign, &overflow_mul);
6914 200911 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6915 200911 : if (overflow_mul
6916 1551 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6917 : overflow_p = true;
6918 200844 : if (!overflow_p)
6919 200844 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6920 : wide_int_to_tree (ctype, mul));
6921 200911 : }
6922 :
6923 : /* If these operations "cancel" each other, we have the main
6924 : optimizations of this pass, which occur when either constant is a
6925 : multiple of the other, in which case we replace this with either an
6926 : operation or CODE or TCODE.
6927 :
6928 : If we have an unsigned type, we cannot do this since it will change
6929 : the result if the original computation overflowed. */
6930 424988 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6931 101463 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6932 526408 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6933 101116 : || (tcode == MULT_EXPR
6934 101116 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6935 857 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6936 853 : && code != MULT_EXPR)))
6937 : {
6938 1151 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6939 1151 : TYPE_SIGN (type)))
6940 : {
6941 124 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6942 : fold_convert (ctype,
6943 : const_binop (TRUNC_DIV_EXPR,
6944 : op1, c)));
6945 : }
6946 1027 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6947 1027 : TYPE_SIGN (type)))
6948 : {
6949 304 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6950 : fold_convert (ctype,
6951 : const_binop (TRUNC_DIV_EXPR,
6952 : c, op1)));
6953 : }
6954 : }
6955 : break;
6956 :
6957 : default:
6958 : break;
6959 : }
6960 :
6961 : return 0;
6962 : }
6963 :
6964 : /* Return a node which has the indicated constant VALUE (either 0 or
6965 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6966 : and is of the indicated TYPE. */
6967 :
6968 : tree
6969 120846914 : constant_boolean_node (bool value, tree type)
6970 : {
6971 120846914 : if (type == integer_type_node)
6972 20814914 : return value ? integer_one_node : integer_zero_node;
6973 100032000 : else if (type == boolean_type_node)
6974 95551606 : return value ? boolean_true_node : boolean_false_node;
6975 4480394 : else if (VECTOR_TYPE_P (type))
6976 1931 : return build_vector_from_val (type,
6977 1931 : build_int_cst (TREE_TYPE (type),
6978 2664 : value ? -1 : 0));
6979 : else
6980 4478463 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6981 : }
6982 :
6983 :
6984 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6985 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6986 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6987 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6988 : COND is the first argument to CODE; otherwise (as in the example
6989 : given here), it is the second argument. TYPE is the type of the
6990 : original expression. Return NULL_TREE if no simplification is
6991 : possible. */
6992 :
6993 : static tree
6994 974336 : fold_binary_op_with_conditional_arg (location_t loc,
6995 : enum tree_code code,
6996 : tree type, tree op0, tree op1,
6997 : tree cond, tree arg, int cond_first_p)
6998 : {
6999 974336 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7000 974336 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7001 974336 : tree test, true_value, false_value;
7002 974336 : tree lhs = NULL_TREE;
7003 974336 : tree rhs = NULL_TREE;
7004 974336 : enum tree_code cond_code = COND_EXPR;
7005 :
7006 : /* Do not move possibly trapping operations into the conditional as this
7007 : pessimizes code and causes gimplification issues when applied late. */
7008 994267 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7009 974336 : ANY_INTEGRAL_TYPE_P (type)
7010 779456 : && TYPE_OVERFLOW_TRAPS (type), op1))
7011 : return NULL_TREE;
7012 :
7013 954231 : if (TREE_CODE (cond) == COND_EXPR
7014 360587 : || TREE_CODE (cond) == VEC_COND_EXPR)
7015 : {
7016 597437 : test = TREE_OPERAND (cond, 0);
7017 597437 : true_value = TREE_OPERAND (cond, 1);
7018 597437 : false_value = TREE_OPERAND (cond, 2);
7019 : /* If this operand throws an expression, then it does not make
7020 : sense to try to perform a logical or arithmetic operation
7021 : involving it. */
7022 597437 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7023 7463 : lhs = true_value;
7024 597437 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7025 6 : rhs = false_value;
7026 : }
7027 356794 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7028 356688 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7029 : {
7030 354803 : tree testtype = TREE_TYPE (cond);
7031 354803 : test = cond;
7032 354803 : true_value = constant_boolean_node (true, testtype);
7033 354803 : false_value = constant_boolean_node (false, testtype);
7034 : }
7035 : else
7036 : /* Detect the case of mixing vector and scalar types - bail out. */
7037 : return NULL_TREE;
7038 :
7039 952240 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7040 3899 : cond_code = VEC_COND_EXPR;
7041 :
7042 : /* This transformation is only worthwhile if we don't have to wrap ARG
7043 : in a SAVE_EXPR and the operation can be simplified without recursing
7044 : on at least one of the branches once its pushed inside the COND_EXPR. */
7045 952240 : if (!TREE_CONSTANT (arg)
7046 952240 : && (TREE_SIDE_EFFECTS (arg)
7047 448539 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7048 443839 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7049 : return NULL_TREE;
7050 :
7051 520195 : arg = fold_convert_loc (loc, arg_type, arg);
7052 520195 : if (lhs == 0)
7053 : {
7054 514164 : true_value = fold_convert_loc (loc, cond_type, true_value);
7055 514164 : if (cond_first_p)
7056 503889 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7057 : else
7058 10275 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7059 : }
7060 520195 : if (rhs == 0)
7061 : {
7062 520189 : false_value = fold_convert_loc (loc, cond_type, false_value);
7063 520189 : if (cond_first_p)
7064 509353 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7065 : else
7066 10836 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7067 : }
7068 :
7069 : /* Check that we have simplified at least one of the branches. */
7070 520195 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7071 : return NULL_TREE;
7072 :
7073 499428 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7074 : }
7075 :
7076 :
7077 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7078 :
7079 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7080 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7081 : if ARG - ZERO_ARG is the same as X.
7082 :
7083 : If ARG is NULL, check for any value of type TYPE.
7084 :
7085 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7086 : and finite. The problematic cases are when X is zero, and its mode
7087 : has signed zeros. In the case of rounding towards -infinity,
7088 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7089 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7090 :
7091 : bool
7092 637143 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7093 : const_tree zero_arg, int negate)
7094 : {
7095 637143 : if (!real_zerop (zero_arg))
7096 : return false;
7097 :
7098 : /* Don't allow the fold with -fsignaling-nans. */
7099 636351 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7100 : return false;
7101 :
7102 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7103 633149 : if (!HONOR_SIGNED_ZEROS (type))
7104 : return true;
7105 :
7106 : /* There is no case that is safe for all rounding modes. */
7107 616106 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7108 : return false;
7109 :
7110 : /* In a vector or complex, we would need to check the sign of all zeros. */
7111 615443 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7112 2849 : zero_arg = uniform_vector_p (zero_arg);
7113 615443 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7114 : return false;
7115 :
7116 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7117 614139 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7118 210 : negate = !negate;
7119 :
7120 : /* The mode has signed zeros, and we have to honor their sign.
7121 : In this situation, there are only two cases we can return true for.
7122 : (i) X - 0 is the same as X with default rounding.
7123 : (ii) X + 0 is X when X can't possibly be -0.0. */
7124 614139 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7125 : }
7126 :
7127 : /* Subroutine of match.pd that determines if it is safe to optimize
7128 : a floating point comparison of an integer value, known to be between
7129 : LO and HI, using comparison operator CMP, against the real constant
7130 : R in floating point type FMT, as the same integer comparison against
7131 : the integer constant I, with sign ISIGN.
7132 :
7133 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7134 : because the floating point representations of the neighboring integers
7135 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7136 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7137 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7138 : due to truncation/rounding.
7139 : */
7140 : bool
7141 21472 : fold_cmp_float_cst_p (wide_int lo, wide_int hi, enum tree_code cmp,
7142 : const REAL_VALUE_TYPE *r, format_helper fmt,
7143 : wide_int i, signop isign)
7144 : {
7145 21472 : REAL_VALUE_TYPE raw;
7146 21472 : REAL_VALUE_TYPE rnd;
7147 21472 : bool check_im1 = true;
7148 21472 : bool check_ip1 = true;
7149 :
7150 21472 : switch (cmp)
7151 : {
7152 : case EQ_EXPR:
7153 : case NE_EXPR:
7154 : /* Check both i-1 and i+1. */
7155 : break;
7156 :
7157 19887 : case LT_EXPR:
7158 19887 : case GE_EXPR:
7159 : /* Only check i-1. */
7160 19887 : check_ip1 = false;
7161 19887 : break;
7162 :
7163 356 : case LE_EXPR:
7164 356 : case GT_EXPR:
7165 : /* Only check i+1. */
7166 356 : check_im1 = false;
7167 356 : break;
7168 :
7169 : default:
7170 : return false;
7171 : }
7172 :
7173 21472 : if (flag_rounding_math && i != 0)
7174 : {
7175 344 : real_from_integer (&raw, VOIDmode, i, isign);
7176 344 : if (!real_identical (r, &raw))
7177 : return false;
7178 : }
7179 :
7180 21472 : if (check_im1 && wi::gt_p (i, lo, isign))
7181 : {
7182 1300 : if (flag_rounding_math)
7183 : {
7184 344 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7185 344 : real_convert (&rnd, fmt, &raw);
7186 344 : if (!real_identical (&raw, &rnd))
7187 : return false;
7188 : }
7189 : else
7190 956 : real_from_integer (&rnd, fmt, i - 1, isign);
7191 956 : if (real_identical (r, &rnd))
7192 : return false;
7193 : }
7194 :
7195 20784 : if (check_ip1 && wi::lt_p (i, hi, isign))
7196 : {
7197 875 : if (flag_rounding_math)
7198 : {
7199 0 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7200 0 : real_convert (&rnd, fmt, &raw);
7201 0 : if (!real_identical (&raw, &rnd))
7202 : return false;
7203 : }
7204 : else
7205 875 : real_from_integer (&rnd, fmt, i + 1, isign);
7206 875 : if (real_identical (r, &rnd))
7207 0 : return false;
7208 : }
7209 :
7210 : return true;
7211 : }
7212 :
7213 : /* Subroutine of match.pd that optimizes comparisons of a division by
7214 : a nonzero integer constant against an integer constant, i.e.
7215 : X/C1 op C2.
7216 :
7217 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7218 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7219 :
7220 : enum tree_code
7221 1748306 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7222 : tree *hi, bool *neg_overflow)
7223 : {
7224 1748306 : tree prod, tmp, type = TREE_TYPE (c1);
7225 1748306 : signop sign = TYPE_SIGN (type);
7226 1748306 : wi::overflow_type overflow;
7227 :
7228 : /* We have to do this the hard way to detect unsigned overflow.
7229 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7230 1748306 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7231 1748306 : prod = force_fit_type (type, val, -1, overflow);
7232 1748306 : *neg_overflow = false;
7233 :
7234 1748306 : if (sign == UNSIGNED)
7235 : {
7236 1718776 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7237 1718776 : *lo = prod;
7238 :
7239 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7240 1718776 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7241 1718776 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7242 : }
7243 29530 : else if (tree_int_cst_sgn (c1) >= 0)
7244 : {
7245 28131 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7246 28131 : switch (tree_int_cst_sgn (c2))
7247 : {
7248 4914 : case -1:
7249 4914 : *neg_overflow = true;
7250 4914 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7251 4914 : *hi = prod;
7252 4914 : break;
7253 :
7254 14730 : case 0:
7255 14730 : *lo = fold_negate_const (tmp, type);
7256 14730 : *hi = tmp;
7257 14730 : break;
7258 :
7259 8487 : case 1:
7260 8487 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7261 8487 : *lo = prod;
7262 8487 : break;
7263 :
7264 0 : default:
7265 0 : gcc_unreachable ();
7266 : }
7267 : }
7268 : else
7269 : {
7270 : /* A negative divisor reverses the relational operators. */
7271 1399 : code = swap_tree_comparison (code);
7272 :
7273 1399 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7274 1399 : switch (tree_int_cst_sgn (c2))
7275 : {
7276 134 : case -1:
7277 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7278 134 : *lo = prod;
7279 134 : break;
7280 :
7281 167 : case 0:
7282 167 : *hi = fold_negate_const (tmp, type);
7283 167 : *lo = tmp;
7284 167 : break;
7285 :
7286 1098 : case 1:
7287 1098 : *neg_overflow = true;
7288 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7289 1098 : *hi = prod;
7290 1098 : break;
7291 :
7292 0 : default:
7293 0 : gcc_unreachable ();
7294 : }
7295 : }
7296 :
7297 1748306 : if (code != EQ_EXPR && code != NE_EXPR)
7298 : return code;
7299 :
7300 16807 : if (TREE_OVERFLOW (*lo)
7301 16807 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7302 745 : *lo = NULL_TREE;
7303 16807 : if (TREE_OVERFLOW (*hi)
7304 16807 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7305 95 : *hi = NULL_TREE;
7306 :
7307 : return code;
7308 1748306 : }
7309 :
7310 : /* Test whether it is preferable to swap two operands, ARG0 and
7311 : ARG1, for example because ARG0 is an integer constant and ARG1
7312 : isn't. */
7313 :
7314 : bool
7315 1637875294 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7316 : {
7317 1637875294 : if (CONSTANT_CLASS_P (arg1))
7318 : return false;
7319 533942783 : if (CONSTANT_CLASS_P (arg0))
7320 : return true;
7321 :
7322 492564757 : STRIP_NOPS (arg0);
7323 492564757 : STRIP_NOPS (arg1);
7324 :
7325 492564757 : if (TREE_CONSTANT (arg1))
7326 : return false;
7327 474390120 : if (TREE_CONSTANT (arg0))
7328 : return true;
7329 :
7330 : /* Put addresses in arg1. */
7331 473601772 : if (TREE_CODE (arg1) == ADDR_EXPR)
7332 : return false;
7333 453515711 : if (TREE_CODE (arg0) == ADDR_EXPR)
7334 : return true;
7335 :
7336 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7337 : for commutative and comparison operators. Ensuring a canonical
7338 : form allows the optimizers to find additional redundancies without
7339 : having to explicitly check for both orderings. */
7340 453113109 : if (TREE_CODE (arg0) == SSA_NAME
7341 342009144 : && TREE_CODE (arg1) == SSA_NAME
7342 789211471 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7343 : return true;
7344 :
7345 : /* Put SSA_NAMEs last. */
7346 430397253 : if (TREE_CODE (arg1) == SSA_NAME)
7347 : return false;
7348 101853843 : if (TREE_CODE (arg0) == SSA_NAME)
7349 : return true;
7350 :
7351 : /* Put variables last. */
7352 95943061 : if (DECL_P (arg1))
7353 : return false;
7354 51871284 : if (DECL_P (arg0))
7355 6080811 : return true;
7356 :
7357 : return false;
7358 : }
7359 :
7360 :
7361 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7362 : means A >= Y && A != MAX, but in this case we know that
7363 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7364 :
7365 : static tree
7366 24157114 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7367 : {
7368 24157114 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7369 :
7370 24157114 : if (TREE_CODE (bound) == LT_EXPR)
7371 4982550 : a = TREE_OPERAND (bound, 0);
7372 19174564 : else if (TREE_CODE (bound) == GT_EXPR)
7373 2803197 : a = TREE_OPERAND (bound, 1);
7374 : else
7375 : return NULL_TREE;
7376 :
7377 7785747 : typea = TREE_TYPE (a);
7378 7785747 : if (!INTEGRAL_TYPE_P (typea)
7379 499656 : && !POINTER_TYPE_P (typea))
7380 : return NULL_TREE;
7381 :
7382 7605571 : if (TREE_CODE (ineq) == LT_EXPR)
7383 : {
7384 1475494 : a1 = TREE_OPERAND (ineq, 1);
7385 1475494 : y = TREE_OPERAND (ineq, 0);
7386 : }
7387 6130077 : else if (TREE_CODE (ineq) == GT_EXPR)
7388 : {
7389 1166548 : a1 = TREE_OPERAND (ineq, 0);
7390 1166548 : y = TREE_OPERAND (ineq, 1);
7391 : }
7392 : else
7393 : return NULL_TREE;
7394 :
7395 2642042 : if (TREE_TYPE (a1) != typea)
7396 : return NULL_TREE;
7397 :
7398 1880073 : if (POINTER_TYPE_P (typea))
7399 : {
7400 : /* Convert the pointer types into integer before taking the difference. */
7401 12237 : tree ta = fold_convert_loc (loc, ssizetype, a);
7402 12237 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7403 12237 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7404 : }
7405 : else
7406 1867836 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7407 :
7408 1880073 : if (!diff || !integer_onep (diff))
7409 : return NULL_TREE;
7410 :
7411 8867 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7412 : }
7413 :
7414 : /* Fold a sum or difference of at least one multiplication.
7415 : Returns the folded tree or NULL if no simplification could be made. */
7416 :
7417 : static tree
7418 9187450 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7419 : tree arg0, tree arg1)
7420 : {
7421 9187450 : tree arg00, arg01, arg10, arg11;
7422 9187450 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7423 :
7424 : /* (A * C) +- (B * C) -> (A+-B) * C.
7425 : (A * C) +- A -> A * (C+-1).
7426 : We are most concerned about the case where C is a constant,
7427 : but other combinations show up during loop reduction. Since
7428 : it is not difficult, try all four possibilities. */
7429 :
7430 9187450 : if (TREE_CODE (arg0) == MULT_EXPR)
7431 : {
7432 8192917 : arg00 = TREE_OPERAND (arg0, 0);
7433 8192917 : arg01 = TREE_OPERAND (arg0, 1);
7434 : }
7435 994533 : else if (TREE_CODE (arg0) == INTEGER_CST)
7436 : {
7437 68419 : arg00 = build_one_cst (type);
7438 68419 : arg01 = arg0;
7439 : }
7440 : else
7441 : {
7442 : /* We cannot generate constant 1 for fract. */
7443 926114 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7444 : return NULL_TREE;
7445 926114 : arg00 = arg0;
7446 926114 : arg01 = build_one_cst (type);
7447 : }
7448 9187450 : if (TREE_CODE (arg1) == MULT_EXPR)
7449 : {
7450 2407642 : arg10 = TREE_OPERAND (arg1, 0);
7451 2407642 : arg11 = TREE_OPERAND (arg1, 1);
7452 : }
7453 6779808 : else if (TREE_CODE (arg1) == INTEGER_CST)
7454 : {
7455 3515325 : arg10 = build_one_cst (type);
7456 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7457 : the purpose of this canonicalization. */
7458 6799125 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7459 234813 : && negate_expr_p (arg1)
7460 3746850 : && code == PLUS_EXPR)
7461 : {
7462 231525 : arg11 = negate_expr (arg1);
7463 231525 : code = MINUS_EXPR;
7464 : }
7465 : else
7466 : arg11 = arg1;
7467 : }
7468 : else
7469 : {
7470 : /* We cannot generate constant 1 for fract. */
7471 3264483 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7472 : return NULL_TREE;
7473 3264483 : arg10 = arg1;
7474 3264483 : arg11 = build_one_cst (type);
7475 : }
7476 9187450 : same = NULL_TREE;
7477 :
7478 : /* Prefer factoring a common non-constant. */
7479 9187450 : if (operand_equal_p (arg00, arg10, 0))
7480 : same = arg00, alt0 = arg01, alt1 = arg11;
7481 9183669 : else if (operand_equal_p (arg01, arg11, 0))
7482 : same = arg01, alt0 = arg00, alt1 = arg10;
7483 9076134 : else if (operand_equal_p (arg00, arg11, 0))
7484 : same = arg00, alt0 = arg01, alt1 = arg10;
7485 9076072 : else if (operand_equal_p (arg01, arg10, 0))
7486 : same = arg01, alt0 = arg00, alt1 = arg11;
7487 :
7488 : /* No identical multiplicands; see if we can find a common
7489 : power-of-two factor in non-power-of-two multiplies. This
7490 : can help in multi-dimensional array access. */
7491 9070711 : else if (TREE_CODE (arg01) == INTEGER_CST
7492 7969167 : && TREE_CODE (arg11) == INTEGER_CST)
7493 : {
7494 7742448 : wide_int int01 = wi::to_wide (arg01);
7495 7742448 : wide_int int11 = wi::to_wide (arg11);
7496 7742448 : bool swap = false;
7497 7742448 : tree maybe_same;
7498 :
7499 : /* Move min of absolute values to int11. */
7500 7742450 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7501 : {
7502 3540963 : std::swap (int01, int11);
7503 3540963 : std::swap (arg00, arg10);
7504 3540963 : maybe_same = arg01;
7505 3540963 : swap = true;
7506 : }
7507 : else
7508 : maybe_same = arg11;
7509 :
7510 7742448 : wide_int factor = wi::abs (int11);
7511 7742448 : if (wi::gtu_p (factor, 1u)
7512 4229751 : && wi::exact_log2 (factor) != -1
7513 12059244 : && (int01 & (factor - 1)) == 0
7514 : /* The remainder should not be a constant, otherwise we
7515 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7516 : increased the number of multiplications necessary. */
7517 9206138 : && TREE_CODE (arg10) != INTEGER_CST)
7518 : {
7519 2468170 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7520 1234085 : wide_int_to_tree (TREE_TYPE (arg00),
7521 1234085 : wi::sdiv_trunc (int01,
7522 : int11)));
7523 1234085 : alt1 = arg10;
7524 1234085 : same = maybe_same;
7525 1234085 : if (swap)
7526 1109572 : std::swap (alt0, alt1);
7527 : }
7528 7742450 : }
7529 :
7530 7859187 : if (!same)
7531 : return NULL_TREE;
7532 :
7533 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7534 1350824 : || TYPE_OVERFLOW_WRAPS (type)
7535 : /* We are neither factoring zero nor minus one. */
7536 1468836 : || TREE_CODE (same) == INTEGER_CST)
7537 1339201 : return fold_build2_loc (loc, MULT_EXPR, type,
7538 : fold_build2_loc (loc, code, type,
7539 : fold_convert_loc (loc, type, alt0),
7540 : fold_convert_loc (loc, type, alt1)),
7541 1339201 : fold_convert_loc (loc, type, same));
7542 :
7543 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7544 : same may be minus one and thus the multiplication may overflow. Perform
7545 : the sum operation in an unsigned type. */
7546 11623 : tree utype = unsigned_type_for (type);
7547 11623 : tree tem = fold_build2_loc (loc, code, utype,
7548 : fold_convert_loc (loc, utype, alt0),
7549 : fold_convert_loc (loc, utype, alt1));
7550 : /* If the sum evaluated to a constant that is not -INF the multiplication
7551 : cannot overflow. */
7552 23246 : if (TREE_CODE (tem) == INTEGER_CST
7553 14860 : && (wi::to_wide (tem)
7554 18097 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7555 3224 : return fold_build2_loc (loc, MULT_EXPR, type,
7556 3224 : fold_convert (type, tem), same);
7557 :
7558 : /* Do not resort to unsigned multiplication because
7559 : we lose the no-overflow property of the expression. */
7560 : return NULL_TREE;
7561 : }
7562 :
7563 :
7564 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7565 : into the buffer PTR of length LEN bytes.
7566 : Return the number of bytes placed in the buffer, or zero
7567 : upon failure. */
7568 :
7569 : int
7570 54888862 : native_encode_wide_int (tree type, const wide_int_ref &val,
7571 : unsigned char *ptr, int len, int off)
7572 : {
7573 54888862 : int total_bytes;
7574 54888862 : if (BITINT_TYPE_P (type))
7575 : {
7576 17655 : struct bitint_info info;
7577 17655 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7578 17655 : gcc_assert (ok);
7579 17655 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7580 17655 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7581 : {
7582 17083 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7583 : /* More work is needed when adding _BitInt support to PDP endian
7584 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7585 : match target endianity here. */
7586 17083 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7587 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7588 : || (GET_MODE_SIZE (limb_mode)
7589 : >= UNITS_PER_WORD)));
7590 : }
7591 : else
7592 1144 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7593 : }
7594 : else
7595 109742414 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7596 54888862 : int byte, offset, word, words;
7597 54888862 : unsigned char value;
7598 :
7599 54888862 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7600 : return 0;
7601 54888374 : if (off == -1)
7602 53991435 : off = 0;
7603 :
7604 54888374 : if (ptr == NULL)
7605 : /* Dry run. */
7606 2863186 : return MIN (len, total_bytes - off);
7607 :
7608 : words = total_bytes / UNITS_PER_WORD;
7609 :
7610 256572286 : for (byte = 0; byte < total_bytes; byte++)
7611 : {
7612 204547098 : int bitpos = byte * BITS_PER_UNIT;
7613 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7614 : number of bytes. */
7615 204547098 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7616 :
7617 204547098 : if (total_bytes > UNITS_PER_WORD)
7618 : {
7619 204547098 : word = byte / UNITS_PER_WORD;
7620 204547098 : if (WORDS_BIG_ENDIAN)
7621 : word = (words - 1) - word;
7622 204547098 : offset = word * UNITS_PER_WORD;
7623 204547098 : if (BYTES_BIG_ENDIAN)
7624 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7625 : else
7626 204547098 : offset += byte % UNITS_PER_WORD;
7627 : }
7628 : else
7629 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7630 204547098 : if (offset >= off && offset - off < len)
7631 203220783 : ptr[offset - off] = value;
7632 : }
7633 52025188 : return MIN (len, total_bytes - off);
7634 : }
7635 :
7636 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7637 : specified by EXPR into the buffer PTR of length LEN bytes.
7638 : Return the number of bytes placed in the buffer, or zero
7639 : upon failure. */
7640 :
7641 : static int
7642 54888862 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7643 : {
7644 54888862 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7645 54888862 : ptr, len, off);
7646 : }
7647 :
7648 :
7649 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7650 : specified by EXPR into the buffer PTR of length LEN bytes.
7651 : Return the number of bytes placed in the buffer, or zero
7652 : upon failure. */
7653 :
7654 : static int
7655 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7656 : {
7657 0 : tree type = TREE_TYPE (expr);
7658 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7659 0 : int total_bytes = GET_MODE_SIZE (mode);
7660 0 : FIXED_VALUE_TYPE value;
7661 0 : tree i_value, i_type;
7662 :
7663 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7664 : return 0;
7665 :
7666 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7667 :
7668 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7669 : return 0;
7670 :
7671 0 : value = TREE_FIXED_CST (expr);
7672 0 : i_value = double_int_to_tree (i_type, value.data);
7673 :
7674 0 : return native_encode_int (i_value, ptr, len, off);
7675 : }
7676 :
7677 :
7678 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7679 : specified by EXPR into the buffer PTR of length LEN bytes.
7680 : Return the number of bytes placed in the buffer, or zero
7681 : upon failure. */
7682 :
7683 : int
7684 869895 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7685 : unsigned char *ptr, int len, int off)
7686 : {
7687 869895 : int total_bytes = GET_MODE_SIZE (mode);
7688 869895 : int byte, offset, word, words, bitpos;
7689 869895 : unsigned char value;
7690 :
7691 : /* There are always 32 bits in each long, no matter the size of
7692 : the hosts long. We handle floating point representations with
7693 : up to 192 bits. */
7694 869895 : long tmp[6];
7695 :
7696 869895 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7697 : return 0;
7698 867595 : if (off == -1)
7699 759487 : off = 0;
7700 :
7701 867595 : if (ptr == NULL)
7702 : /* Dry run. */
7703 139843 : return MIN (len, total_bytes - off);
7704 :
7705 727752 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7706 :
7707 727752 : real_to_target (tmp, val, mode);
7708 :
7709 7044054 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7710 6316302 : bitpos += BITS_PER_UNIT)
7711 : {
7712 6316302 : byte = (bitpos / BITS_PER_UNIT) & 3;
7713 6316302 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7714 :
7715 6316302 : if (UNITS_PER_WORD < 4)
7716 : {
7717 : word = byte / UNITS_PER_WORD;
7718 : if (WORDS_BIG_ENDIAN)
7719 : word = (words - 1) - word;
7720 : offset = word * UNITS_PER_WORD;
7721 : if (BYTES_BIG_ENDIAN)
7722 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7723 : else
7724 : offset += byte % UNITS_PER_WORD;
7725 : }
7726 : else
7727 : {
7728 6316302 : offset = byte;
7729 6316302 : if (BYTES_BIG_ENDIAN)
7730 : {
7731 : /* Reverse bytes within each long, or within the entire float
7732 : if it's smaller than a long (for HFmode). */
7733 : offset = MIN (3, total_bytes - 1) - offset;
7734 : gcc_assert (offset >= 0);
7735 : }
7736 : }
7737 6316302 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7738 6316302 : if (offset >= off
7739 6313062 : && offset - off < len)
7740 6291402 : ptr[offset - off] = value;
7741 : }
7742 727752 : return MIN (len, total_bytes - off);
7743 : }
7744 :
7745 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7746 : specified by EXPR into the buffer PTR of length LEN bytes.
7747 : Return the number of bytes placed in the buffer, or zero
7748 : upon failure. */
7749 :
7750 : static int
7751 10215 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7752 : {
7753 10215 : int rsize, isize;
7754 10215 : tree part;
7755 :
7756 10215 : part = TREE_REALPART (expr);
7757 10215 : rsize = native_encode_expr (part, ptr, len, off);
7758 10215 : if (off == -1 && rsize == 0)
7759 : return 0;
7760 10215 : part = TREE_IMAGPART (expr);
7761 10215 : if (off != -1)
7762 20421 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7763 10215 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7764 : len - rsize, off);
7765 10215 : if (off == -1 && isize != rsize)
7766 : return 0;
7767 10215 : return rsize + isize;
7768 : }
7769 :
7770 : /* Like native_encode_vector, but only encode the first COUNT elements.
7771 : The other arguments are as for native_encode_vector. */
7772 :
7773 : static int
7774 1082404 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7775 : int off, unsigned HOST_WIDE_INT count)
7776 : {
7777 1082404 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7778 2164808 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7779 1083438 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7780 : {
7781 : /* This is the only case in which elements can be smaller than a byte.
7782 : Element 0 is always in the lsb of the containing byte. */
7783 956 : unsigned int elt_bits = TYPE_PRECISION (itype);
7784 956 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7785 956 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7786 : return 0;
7787 :
7788 956 : if (off == -1)
7789 956 : off = 0;
7790 :
7791 : /* Zero the buffer and then set bits later where necessary. */
7792 956 : int extract_bytes = MIN (len, total_bytes - off);
7793 956 : if (ptr)
7794 956 : memset (ptr, 0, extract_bytes);
7795 :
7796 956 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7797 956 : unsigned int first_elt = off * elts_per_byte;
7798 956 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7799 956 : unsigned int elt_mask = (1 << elt_bits) - 1;
7800 17477 : for (unsigned int i = 0; i < extract_elts; ++i)
7801 : {
7802 16521 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7803 16521 : if (TREE_CODE (elt) != INTEGER_CST)
7804 : return 0;
7805 :
7806 16521 : if (ptr && integer_nonzerop (elt))
7807 : {
7808 8488 : unsigned int bit = i * elt_bits;
7809 8488 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7810 : }
7811 : }
7812 : return extract_bytes;
7813 : }
7814 :
7815 1081448 : int offset = 0;
7816 1081448 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7817 4341276 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7818 : {
7819 3864616 : if (off >= size)
7820 : {
7821 23647 : off -= size;
7822 23647 : continue;
7823 : }
7824 3840969 : tree elem = VECTOR_CST_ELT (expr, i);
7825 3840969 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7826 : len - offset, off);
7827 3840969 : if ((off == -1 && res != size) || res == 0)
7828 : return 0;
7829 3840440 : offset += res;
7830 3840440 : if (offset >= len)
7831 604259 : return (off == -1 && i < count - 1) ? 0 : offset;
7832 3236181 : if (off != -1)
7833 444881 : off = 0;
7834 : }
7835 : return offset;
7836 : }
7837 :
7838 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7839 : specified by EXPR into the buffer PTR of length LEN bytes.
7840 : Return the number of bytes placed in the buffer, or zero
7841 : upon failure. */
7842 :
7843 : static int
7844 925713 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7845 : {
7846 925713 : unsigned HOST_WIDE_INT count;
7847 925713 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7848 : return 0;
7849 925713 : return native_encode_vector_part (expr, ptr, len, off, count);
7850 : }
7851 :
7852 :
7853 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7854 : specified by EXPR into the buffer PTR of length LEN bytes.
7855 : Return the number of bytes placed in the buffer, or zero
7856 : upon failure. */
7857 :
7858 : static int
7859 153043 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7860 : {
7861 153043 : tree type = TREE_TYPE (expr);
7862 :
7863 : /* Wide-char strings are encoded in target byte-order so native
7864 : encoding them is trivial. */
7865 153043 : if (BITS_PER_UNIT != CHAR_BIT
7866 153043 : || TREE_CODE (type) != ARRAY_TYPE
7867 153043 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7868 306086 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7869 : return 0;
7870 :
7871 153043 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7872 153043 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7873 : return 0;
7874 152177 : if (off == -1)
7875 65788 : off = 0;
7876 152177 : len = MIN (total_bytes - off, len);
7877 152177 : if (ptr == NULL)
7878 : /* Dry run. */;
7879 : else
7880 : {
7881 152177 : int written = 0;
7882 152177 : if (off < TREE_STRING_LENGTH (expr))
7883 : {
7884 151905 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7885 151905 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7886 : }
7887 152177 : memset (ptr + written, 0, len - written);
7888 : }
7889 : return len;
7890 : }
7891 :
7892 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7893 : specified by EXPR into the buffer PTR of length LEN bytes.
7894 : Return the number of bytes placed in the buffer, or zero
7895 : upon failure. */
7896 :
7897 : static int
7898 49497 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7899 : {
7900 : /* We are only concerned with zero-initialization constructors here. That's
7901 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7902 : deal with. For more general handling of constructors, there is
7903 : native_encode_initializer. */
7904 49497 : if (CONSTRUCTOR_NELTS (expr))
7905 : return 0;
7906 :
7907 : /* Wide-char strings are encoded in target byte-order so native
7908 : encoding them is trivial. */
7909 92490 : if (BITS_PER_UNIT != CHAR_BIT
7910 46245 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7911 : return 0;
7912 :
7913 46245 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7914 46245 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7915 : return 0;
7916 46245 : if (off == -1)
7917 0 : off = 0;
7918 46245 : len = MIN (total_bytes - off, len);
7919 46245 : if (ptr == NULL)
7920 : /* Dry run. */;
7921 : else
7922 46245 : memset (ptr, 0, len);
7923 : return len;
7924 : }
7925 :
7926 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7927 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7928 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7929 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7930 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7931 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7932 : Return the number of bytes placed in the buffer, or zero upon failure. */
7933 :
7934 : int
7935 71561686 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7936 : {
7937 : /* We don't support starting at negative offset and -1 is special. */
7938 71561686 : if (off < -1)
7939 : return 0;
7940 :
7941 71561674 : switch (TREE_CODE (expr))
7942 : {
7943 54886616 : case INTEGER_CST:
7944 54886616 : return native_encode_int (expr, ptr, len, off);
7945 :
7946 869895 : case REAL_CST:
7947 869895 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7948 1739790 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7949 :
7950 0 : case FIXED_CST:
7951 0 : return native_encode_fixed (expr, ptr, len, off);
7952 :
7953 10215 : case COMPLEX_CST:
7954 10215 : return native_encode_complex (expr, ptr, len, off);
7955 :
7956 925713 : case VECTOR_CST:
7957 925713 : return native_encode_vector (expr, ptr, len, off);
7958 :
7959 153043 : case STRING_CST:
7960 153043 : return native_encode_string (expr, ptr, len, off);
7961 :
7962 49497 : case CONSTRUCTOR:
7963 49497 : return native_encode_constructor (expr, ptr, len, off);
7964 :
7965 : default:
7966 : return 0;
7967 : }
7968 : }
7969 :
7970 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7971 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7972 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7973 : machine modes, we can't just use build_nonstandard_integer_type. */
7974 :
7975 : tree
7976 541 : find_bitfield_repr_type (int fieldsize, int len)
7977 : {
7978 541 : machine_mode mode;
7979 1063 : for (int pass = 0; pass < 2; pass++)
7980 : {
7981 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7982 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7983 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7984 7286 : && known_eq (GET_MODE_PRECISION (mode),
7985 : GET_MODE_BITSIZE (mode))
7986 11274 : && known_le (GET_MODE_SIZE (mode), len))
7987 : {
7988 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7989 280 : if (ret && TYPE_MODE (ret) == mode)
7990 : return ret;
7991 : }
7992 : }
7993 :
7994 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7995 261 : if (int_n_enabled_p[i]
7996 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7997 261 : && int_n_trees[i].unsigned_type)
7998 : {
7999 261 : tree ret = int_n_trees[i].unsigned_type;
8000 261 : mode = TYPE_MODE (ret);
8001 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8002 522 : && known_eq (GET_MODE_PRECISION (mode),
8003 : GET_MODE_BITSIZE (mode))
8004 783 : && known_le (GET_MODE_SIZE (mode), len))
8005 : return ret;
8006 : }
8007 :
8008 : return NULL_TREE;
8009 : }
8010 :
8011 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8012 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8013 : to be non-NULL and OFF zero), then in addition to filling the
8014 : bytes pointed by PTR with the value also clear any bits pointed
8015 : by MASK that are known to be initialized, keep them as is for
8016 : e.g. uninitialized padding bits or uninitialized fields. */
8017 :
8018 : int
8019 47915335 : native_encode_initializer (tree init, unsigned char *ptr, int len,
8020 : int off, unsigned char *mask)
8021 : {
8022 47915335 : int r;
8023 :
8024 : /* We don't support starting at negative offset and -1 is special. */
8025 47915335 : if (off < -1 || init == NULL_TREE)
8026 : return 0;
8027 :
8028 47915335 : gcc_assert (mask == NULL || (off == 0 && ptr));
8029 :
8030 47915335 : STRIP_NOPS (init);
8031 47915335 : switch (TREE_CODE (init))
8032 : {
8033 0 : case VIEW_CONVERT_EXPR:
8034 0 : case NON_LVALUE_EXPR:
8035 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8036 0 : mask);
8037 45699381 : default:
8038 45699381 : r = native_encode_expr (init, ptr, len, off);
8039 45699381 : if (mask)
8040 7135 : memset (mask, 0, r);
8041 : return r;
8042 2215954 : case CONSTRUCTOR:
8043 2215954 : tree type = TREE_TYPE (init);
8044 2215954 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8045 2215954 : if (total_bytes < 0)
8046 : return 0;
8047 2215954 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
8048 : return 0;
8049 2215878 : int o = off == -1 ? 0 : off;
8050 2215878 : if (TREE_CODE (type) == ARRAY_TYPE)
8051 : {
8052 298523 : tree min_index;
8053 298523 : unsigned HOST_WIDE_INT cnt;
8054 298523 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8055 298523 : constructor_elt *ce;
8056 :
8057 298523 : if (!TYPE_DOMAIN (type)
8058 298523 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8059 : return 0;
8060 :
8061 298523 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
8062 298523 : if (fieldsize <= 0)
8063 : return 0;
8064 :
8065 298523 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8066 298523 : if (ptr)
8067 298523 : memset (ptr, '\0', MIN (total_bytes - off, len));
8068 :
8069 298523 : for (cnt = 0; ; cnt++)
8070 : {
8071 45354897 : tree val = NULL_TREE, index = NULL_TREE;
8072 45354897 : HOST_WIDE_INT pos = curpos, count = 0;
8073 45354897 : bool full = false;
8074 45354897 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8075 : {
8076 45251521 : val = ce->value;
8077 45251521 : index = ce->index;
8078 : }
8079 103376 : else if (mask == NULL
8080 618 : || CONSTRUCTOR_NO_CLEARING (init)
8081 103934 : || curpos >= total_bytes)
8082 : break;
8083 : else
8084 : pos = total_bytes;
8085 :
8086 45251521 : if (index && TREE_CODE (index) == RANGE_EXPR)
8087 : {
8088 18 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8089 18 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8090 0 : return 0;
8091 18 : offset_int wpos
8092 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8093 36 : - wi::to_offset (min_index),
8094 18 : TYPE_PRECISION (sizetype));
8095 18 : wpos *= fieldsize;
8096 18 : if (!wi::fits_shwi_p (pos))
8097 : return 0;
8098 18 : pos = wpos.to_shwi ();
8099 18 : offset_int wcount
8100 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8101 36 : - wi::to_offset (TREE_OPERAND (index, 0)),
8102 18 : TYPE_PRECISION (sizetype));
8103 18 : if (!wi::fits_shwi_p (wcount))
8104 : return 0;
8105 18 : count = wcount.to_shwi ();
8106 18 : }
8107 44701421 : else if (index)
8108 : {
8109 44701421 : if (TREE_CODE (index) != INTEGER_CST)
8110 0 : return 0;
8111 44701421 : offset_int wpos
8112 44701421 : = wi::sext (wi::to_offset (index)
8113 89402842 : - wi::to_offset (min_index),
8114 44701421 : TYPE_PRECISION (sizetype));
8115 44701421 : wpos *= fieldsize;
8116 44701421 : if (!wi::fits_shwi_p (wpos))
8117 : return 0;
8118 44701421 : pos = wpos.to_shwi ();
8119 : }
8120 :
8121 45252782 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8122 : {
8123 72 : if (valueinit == -1)
8124 : {
8125 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8126 144 : r = native_encode_initializer (zero, ptr + curpos,
8127 : fieldsize, 0,
8128 72 : mask + curpos);
8129 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8130 2 : ggc_free (zero);
8131 72 : if (!r)
8132 : return 0;
8133 72 : valueinit = curpos;
8134 72 : curpos += fieldsize;
8135 : }
8136 102 : while (curpos != pos)
8137 : {
8138 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8139 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8140 30 : curpos += fieldsize;
8141 : }
8142 : }
8143 :
8144 45251593 : curpos = pos;
8145 45251593 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8146 : {
8147 527 : if (count)
8148 : return 0;
8149 527 : if (off == -1
8150 527 : || (curpos >= off
8151 0 : && (curpos + RAW_DATA_LENGTH (val)
8152 0 : <= (HOST_WIDE_INT) off + len)))
8153 : {
8154 527 : if (ptr)
8155 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8156 527 : RAW_DATA_LENGTH (val));
8157 527 : if (mask)
8158 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8159 : }
8160 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8161 0 : && curpos < (HOST_WIDE_INT) off + len)
8162 : {
8163 : /* Partial overlap. */
8164 0 : unsigned char *p = NULL;
8165 0 : int no = 0;
8166 0 : int l;
8167 0 : gcc_assert (mask == NULL);
8168 0 : if (curpos >= off)
8169 : {
8170 0 : if (ptr)
8171 0 : p = ptr + curpos - off;
8172 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8173 : RAW_DATA_LENGTH (val));
8174 : }
8175 : else
8176 : {
8177 0 : p = ptr;
8178 0 : no = off - curpos;
8179 0 : l = len;
8180 : }
8181 0 : if (p)
8182 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8183 : }
8184 527 : curpos += RAW_DATA_LENGTH (val);
8185 527 : val = NULL_TREE;
8186 : }
8187 527 : if (val)
8188 45329036 : do
8189 : {
8190 45329036 : if (off == -1
8191 645870 : || (curpos >= off
8192 217930 : && (curpos + fieldsize
8193 217930 : <= (HOST_WIDE_INT) off + len)))
8194 : {
8195 44870709 : if (full)
8196 : {
8197 78042 : if (ptr)
8198 78042 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8199 : fieldsize);
8200 78042 : if (mask)
8201 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8202 : }
8203 89773994 : else if (!native_encode_initializer (val,
8204 : ptr
8205 44792667 : ? ptr + curpos - o
8206 : : NULL,
8207 : fieldsize,
8208 : off == -1 ? -1
8209 : : 0,
8210 : mask
8211 1117 : ? mask + curpos
8212 : : NULL))
8213 : return 0;
8214 : else
8215 : {
8216 : full = true;
8217 : pos = curpos;
8218 : }
8219 : }
8220 458327 : else if (curpos + fieldsize > off
8221 32650 : && curpos < (HOST_WIDE_INT) off + len)
8222 : {
8223 : /* Partial overlap. */
8224 8139 : unsigned char *p = NULL;
8225 8139 : int no = 0;
8226 8139 : int l;
8227 8139 : gcc_assert (mask == NULL);
8228 8139 : if (curpos >= off)
8229 : {
8230 5876 : if (ptr)
8231 5876 : p = ptr + curpos - off;
8232 5876 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8233 : fieldsize);
8234 : }
8235 : else
8236 : {
8237 2263 : p = ptr;
8238 2263 : no = off - curpos;
8239 2263 : l = len;
8240 : }
8241 8139 : if (!native_encode_initializer (val, p, l, no, NULL))
8242 : return 0;
8243 : }
8244 45133817 : curpos += fieldsize;
8245 : }
8246 45133817 : while (count-- != 0);
8247 45056374 : }
8248 103304 : return MIN (total_bytes - off, len);
8249 : }
8250 1917355 : else if (TREE_CODE (type) == RECORD_TYPE
8251 1917355 : || TREE_CODE (type) == UNION_TYPE)
8252 : {
8253 1917355 : unsigned HOST_WIDE_INT cnt;
8254 1917355 : constructor_elt *ce;
8255 1917355 : tree fld_base = TYPE_FIELDS (type);
8256 1917355 : tree to_free = NULL_TREE;
8257 :
8258 1917355 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8259 1917355 : if (ptr != NULL)
8260 1917355 : memset (ptr, '\0', MIN (total_bytes - o, len));
8261 1917355 : for (cnt = 0; ; cnt++)
8262 : {
8263 2292640 : tree val = NULL_TREE, field = NULL_TREE;
8264 2292640 : HOST_WIDE_INT pos = 0, fieldsize;
8265 2292640 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8266 :
8267 2292640 : if (to_free)
8268 : {
8269 0 : ggc_free (to_free);
8270 0 : to_free = NULL_TREE;
8271 : }
8272 :
8273 2292640 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8274 : {
8275 402607 : val = ce->value;
8276 402607 : field = ce->index;
8277 402607 : if (field == NULL_TREE)
8278 : return 0;
8279 :
8280 402607 : pos = int_byte_position (field);
8281 402607 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8282 1501 : continue;
8283 : }
8284 1890033 : else if (mask == NULL
8285 1890033 : || CONSTRUCTOR_NO_CLEARING (init))
8286 : break;
8287 : else
8288 : pos = total_bytes;
8289 :
8290 415131 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8291 : {
8292 : tree fld;
8293 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8294 : {
8295 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8296 44565 : continue;
8297 2083 : if (fld == field)
8298 : break;
8299 528 : if (DECL_PADDING_P (fld))
8300 87 : continue;
8301 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8302 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8303 : return 0;
8304 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8305 382 : continue;
8306 : break;
8307 : }
8308 1614 : if (fld == NULL_TREE)
8309 : {
8310 895 : if (ce == NULL)
8311 : break;
8312 : return 0;
8313 : }
8314 1614 : fld_base = DECL_CHAIN (fld);
8315 1614 : if (fld != field)
8316 : {
8317 59 : cnt--;
8318 59 : field = fld;
8319 59 : pos = int_byte_position (field);
8320 59 : val = build_zero_cst (TREE_TYPE (fld));
8321 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8322 0 : to_free = val;
8323 : }
8324 : }
8325 :
8326 401165 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8327 15188 : && TYPE_DOMAIN (TREE_TYPE (field))
8328 416353 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8329 : {
8330 81 : if (mask || off != -1)
8331 : return 0;
8332 81 : if (val == NULL_TREE)
8333 0 : continue;
8334 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8335 : return 0;
8336 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8337 81 : if (fieldsize < 0
8338 81 : || (int) fieldsize != fieldsize
8339 81 : || (pos + fieldsize) > INT_MAX)
8340 : return 0;
8341 81 : if (pos + fieldsize > total_bytes)
8342 : {
8343 81 : if (ptr != NULL && total_bytes < len)
8344 81 : memset (ptr + total_bytes, '\0',
8345 81 : MIN (pos + fieldsize, len) - total_bytes);
8346 : total_bytes = pos + fieldsize;
8347 : }
8348 : }
8349 : else
8350 : {
8351 401084 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8352 401084 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8353 : return 0;
8354 401084 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8355 : }
8356 401165 : if (fieldsize == 0)
8357 1 : continue;
8358 :
8359 : /* Prepare to deal with integral bit-fields and filter out other
8360 : bit-fields that do not start and end on a byte boundary. */
8361 401164 : if (DECL_BIT_FIELD (field))
8362 : {
8363 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8364 : return 0;
8365 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8366 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8367 : {
8368 2711 : bpos %= BITS_PER_UNIT;
8369 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8370 2711 : epos = fieldsize % BITS_PER_UNIT;
8371 2711 : fieldsize += BITS_PER_UNIT - 1;
8372 2711 : fieldsize /= BITS_PER_UNIT;
8373 : }
8374 0 : else if (bpos % BITS_PER_UNIT
8375 0 : || DECL_SIZE (field) == NULL_TREE
8376 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8377 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8378 : return 0;
8379 : }
8380 :
8381 401164 : if (off != -1 && pos + fieldsize <= off)
8382 3199 : continue;
8383 :
8384 397965 : if (val == NULL_TREE)
8385 0 : continue;
8386 :
8387 397965 : if (DECL_BIT_FIELD (field)
8388 397965 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8389 : {
8390 : /* FIXME: Handle PDP endian. */
8391 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8392 261 : return 0;
8393 :
8394 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8395 6 : val = TREE_OPERAND (val, 0);
8396 2507 : if (TREE_CODE (val) != INTEGER_CST)
8397 : return 0;
8398 :
8399 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8400 2507 : tree repr_type = NULL_TREE;
8401 2507 : HOST_WIDE_INT rpos = 0;
8402 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8403 : {
8404 1978 : rpos = int_byte_position (repr);
8405 1978 : repr_type = TREE_TYPE (repr);
8406 : }
8407 : else
8408 : {
8409 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8410 529 : if (repr_type == NULL_TREE)
8411 : return 0;
8412 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8413 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8414 268 : if (pos + repr_size <= o + len)
8415 : rpos = pos;
8416 : else
8417 : {
8418 14 : rpos = o + len - repr_size;
8419 14 : gcc_assert (rpos <= pos);
8420 : }
8421 : }
8422 :
8423 2246 : if (rpos > pos)
8424 : return 0;
8425 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8426 2246 : int diff = (TYPE_PRECISION (repr_type)
8427 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8428 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8429 2246 : if (!BYTES_BIG_ENDIAN)
8430 2246 : w = wi::lshift (w, bitoff);
8431 : else
8432 : w = wi::lshift (w, diff - bitoff);
8433 2246 : val = wide_int_to_tree (repr_type, w);
8434 :
8435 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8436 : / BITS_PER_UNIT + 1];
8437 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8438 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8439 0 : return 0;
8440 :
8441 2246 : if (ptr == NULL)
8442 0 : continue;
8443 :
8444 : /* If the bitfield does not start at byte boundary, handle
8445 : the partial byte at the start. */
8446 2246 : if (bpos
8447 1351 : && (off == -1 || (pos >= off && len >= 1)))
8448 : {
8449 1276 : if (!BYTES_BIG_ENDIAN)
8450 : {
8451 1276 : int msk = (1 << bpos) - 1;
8452 1276 : buf[pos - rpos] &= ~msk;
8453 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8454 1276 : if (mask)
8455 : {
8456 147 : if (fieldsize > 1 || epos == 0)
8457 129 : mask[pos] &= msk;
8458 : else
8459 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8460 : }
8461 : }
8462 : else
8463 : {
8464 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8465 : buf[pos - rpos] &= msk;
8466 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8467 : if (mask)
8468 : {
8469 : if (fieldsize > 1 || epos == 0)
8470 : mask[pos] &= ~msk;
8471 : else
8472 : mask[pos] &= (~msk
8473 : | ((1 << (BITS_PER_UNIT - epos))
8474 : - 1));
8475 : }
8476 : }
8477 : }
8478 : /* If the bitfield does not end at byte boundary, handle
8479 : the partial byte at the end. */
8480 2246 : if (epos
8481 1724 : && (off == -1
8482 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8483 : {
8484 1621 : if (!BYTES_BIG_ENDIAN)
8485 : {
8486 1621 : int msk = (1 << epos) - 1;
8487 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8488 1621 : buf[pos - rpos + fieldsize - 1]
8489 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8490 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8491 156 : mask[pos + fieldsize - 1] &= ~msk;
8492 : }
8493 : else
8494 : {
8495 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8496 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8497 : buf[pos - rpos + fieldsize - 1]
8498 : |= ptr[pos + fieldsize - 1 - o] & msk;
8499 : if (mask && (fieldsize > 1 || bpos == 0))
8500 : mask[pos + fieldsize - 1] &= msk;
8501 : }
8502 : }
8503 2246 : if (off == -1
8504 1301 : || (pos >= off
8505 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8506 : {
8507 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8508 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8509 75 : memset (mask + pos + (bpos != 0), 0,
8510 75 : fieldsize - (bpos != 0) - (epos != 0));
8511 : }
8512 : else
8513 : {
8514 : /* Partial overlap. */
8515 191 : HOST_WIDE_INT fsz = fieldsize;
8516 191 : gcc_assert (mask == NULL);
8517 191 : if (pos < off)
8518 : {
8519 89 : fsz -= (off - pos);
8520 89 : pos = off;
8521 : }
8522 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8523 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8524 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8525 : }
8526 2246 : continue;
8527 2246 : }
8528 :
8529 395458 : if (off == -1
8530 28592 : || (pos >= off
8531 27760 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8532 : {
8533 387085 : int fldsize = fieldsize;
8534 20219 : if (off == -1)
8535 : {
8536 366866 : tree fld = DECL_CHAIN (field);
8537 5793992 : while (fld)
8538 : {
8539 5445915 : if (TREE_CODE (fld) == FIELD_DECL)
8540 : break;
8541 5427126 : fld = DECL_CHAIN (fld);
8542 : }
8543 366866 : if (fld == NULL_TREE)
8544 348077 : fldsize = len - pos;
8545 : }
8546 419162 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8547 : : NULL,
8548 : fldsize,
8549 : off == -1 ? -1 : 0,
8550 11858 : mask ? mask + pos : NULL);
8551 387085 : if (!r)
8552 : return 0;
8553 367315 : if (off == -1
8554 354338 : && fldsize != fieldsize
8555 1149 : && r > fieldsize
8556 822 : && pos + r > total_bytes)
8557 375285 : total_bytes = pos + r;
8558 : }
8559 : else
8560 : {
8561 : /* Partial overlap. */
8562 8373 : unsigned char *p = NULL;
8563 8373 : int no = 0;
8564 8373 : int l;
8565 8373 : gcc_assert (mask == NULL);
8566 8373 : if (pos >= off)
8567 : {
8568 7541 : if (ptr)
8569 7541 : p = ptr + pos - off;
8570 7541 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8571 : fieldsize);
8572 : }
8573 : else
8574 : {
8575 832 : p = ptr;
8576 832 : no = off - pos;
8577 832 : l = len;
8578 : }
8579 8373 : if (!native_encode_initializer (val, p, l, no, NULL))
8580 : return 0;
8581 : }
8582 375285 : }
8583 1889974 : return MIN (total_bytes - off, len);
8584 : }
8585 : return 0;
8586 : }
8587 : }
8588 :
8589 :
8590 : /* Subroutine of native_interpret_expr. Interpret the contents of
8591 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8592 : If the buffer cannot be interpreted, return NULL_TREE. */
8593 :
8594 : static tree
8595 2876176 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8596 : {
8597 2876176 : int total_bytes;
8598 2876176 : if (BITINT_TYPE_P (type))
8599 : {
8600 31 : struct bitint_info info;
8601 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8602 31 : gcc_assert (ok);
8603 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8604 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8605 : {
8606 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8607 : /* More work is needed when adding _BitInt support to PDP endian
8608 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8609 : match target endianity here. */
8610 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8611 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8612 : || (GET_MODE_SIZE (limb_mode)
8613 : >= UNITS_PER_WORD)));
8614 : }
8615 : else
8616 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8617 : }
8618 : else
8619 5752290 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8620 :
8621 2876176 : if (total_bytes > len)
8622 : return NULL_TREE;
8623 :
8624 2875934 : wide_int result = wi::from_buffer (ptr, total_bytes);
8625 :
8626 2875934 : return wide_int_to_tree (type, result);
8627 2875934 : }
8628 :
8629 :
8630 : /* Subroutine of native_interpret_expr. Interpret the contents of
8631 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8632 : If the buffer cannot be interpreted, return NULL_TREE. */
8633 :
8634 : static tree
8635 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8636 : {
8637 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8638 0 : int total_bytes = GET_MODE_SIZE (mode);
8639 0 : double_int result;
8640 0 : FIXED_VALUE_TYPE fixed_value;
8641 :
8642 0 : if (total_bytes > len
8643 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8644 : return NULL_TREE;
8645 :
8646 0 : result = double_int::from_buffer (ptr, total_bytes);
8647 0 : fixed_value = fixed_from_double_int (result, mode);
8648 :
8649 0 : return build_fixed (type, fixed_value);
8650 : }
8651 :
8652 :
8653 : /* Subroutine of native_interpret_expr. Interpret the contents of
8654 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8655 : If the buffer cannot be interpreted, return NULL_TREE. */
8656 :
8657 : tree
8658 38060 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8659 : {
8660 38060 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8661 38060 : int total_bytes = GET_MODE_SIZE (mode);
8662 38060 : unsigned char value;
8663 : /* There are always 32 bits in each long, no matter the size of
8664 : the hosts long. We handle floating point representations with
8665 : up to 192 bits. */
8666 38060 : REAL_VALUE_TYPE r;
8667 38060 : long tmp[6];
8668 :
8669 38060 : if (total_bytes > len || total_bytes > 24)
8670 : return NULL_TREE;
8671 37999 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8672 :
8673 37999 : memset (tmp, 0, sizeof (tmp));
8674 276229 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8675 238230 : bitpos += BITS_PER_UNIT)
8676 : {
8677 : /* Both OFFSET and BYTE index within a long;
8678 : bitpos indexes the whole float. */
8679 238230 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8680 238230 : if (UNITS_PER_WORD < 4)
8681 : {
8682 : int word = byte / UNITS_PER_WORD;
8683 : if (WORDS_BIG_ENDIAN)
8684 : word = (words - 1) - word;
8685 : offset = word * UNITS_PER_WORD;
8686 : if (BYTES_BIG_ENDIAN)
8687 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8688 : else
8689 : offset += byte % UNITS_PER_WORD;
8690 : }
8691 : else
8692 : {
8693 238230 : offset = byte;
8694 238230 : if (BYTES_BIG_ENDIAN)
8695 : {
8696 : /* Reverse bytes within each long, or within the entire float
8697 : if it's smaller than a long (for HFmode). */
8698 : offset = MIN (3, total_bytes - 1) - offset;
8699 : gcc_assert (offset >= 0);
8700 : }
8701 : }
8702 238230 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8703 :
8704 238230 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8705 : }
8706 :
8707 37999 : real_from_target (&r, tmp, mode);
8708 37999 : return build_real (type, r);
8709 : }
8710 :
8711 :
8712 : /* Subroutine of native_interpret_expr. Interpret the contents of
8713 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8714 : If the buffer cannot be interpreted, return NULL_TREE. */
8715 :
8716 : static tree
8717 1596 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8718 : {
8719 1596 : tree etype, rpart, ipart;
8720 1596 : int size;
8721 :
8722 1596 : etype = TREE_TYPE (type);
8723 1596 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8724 1596 : if (size * 2 > len)
8725 : return NULL_TREE;
8726 1563 : rpart = native_interpret_expr (etype, ptr, size);
8727 1563 : if (!rpart)
8728 : return NULL_TREE;
8729 1382 : ipart = native_interpret_expr (etype, ptr+size, size);
8730 1382 : if (!ipart)
8731 : return NULL_TREE;
8732 1382 : return build_complex (type, rpart, ipart);
8733 : }
8734 :
8735 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8736 : which contains LEN bytes. The vector is known to be encodable using
8737 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8738 :
8739 : Return the vector on success, otherwise return null. */
8740 :
8741 : static tree
8742 239627 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8743 : unsigned int len, unsigned int npatterns,
8744 : unsigned int nelts_per_pattern)
8745 : {
8746 239627 : tree elt_type = TREE_TYPE (type);
8747 239627 : if (VECTOR_BOOLEAN_TYPE_P (type)
8748 239631 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8749 : {
8750 : /* This is the only case in which elements can be smaller than a byte.
8751 : Element 0 is always in the lsb of the containing byte. */
8752 2 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8753 2 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8754 : return NULL_TREE;
8755 :
8756 2 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8757 22 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8758 : {
8759 18 : unsigned int bit_index = i * elt_bits;
8760 18 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8761 18 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8762 36 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8763 20 : ? build_all_ones_cst (elt_type)
8764 2 : : build_zero_cst (elt_type));
8765 : }
8766 2 : return builder.build ();
8767 2 : }
8768 :
8769 239625 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8770 239625 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8771 : return NULL_TREE;
8772 :
8773 239625 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8774 1192203 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8775 : {
8776 713087 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8777 713087 : if (!elt)
8778 134 : return NULL_TREE;
8779 712953 : builder.quick_push (elt);
8780 712953 : bytes += elt_bytes;
8781 : }
8782 239491 : return builder.build ();
8783 239625 : }
8784 :
8785 : /* Subroutine of native_interpret_expr. Interpret the contents of
8786 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8787 : If the buffer cannot be interpreted, return NULL_TREE. */
8788 :
8789 : static tree
8790 82938 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8791 : {
8792 82938 : unsigned HOST_WIDE_INT size;
8793 :
8794 82938 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8795 82938 : || size > len)
8796 2 : return NULL_TREE;
8797 :
8798 82936 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8799 82936 : return native_interpret_vector_part (type, ptr, len, count, 1);
8800 : }
8801 :
8802 :
8803 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8804 : the buffer PTR of length LEN as a constant of type TYPE. For
8805 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8806 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8807 : return NULL_TREE. */
8808 :
8809 : tree
8810 3155598 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8811 : {
8812 3155598 : switch (TREE_CODE (type))
8813 : {
8814 2876176 : case INTEGER_TYPE:
8815 2876176 : case ENUMERAL_TYPE:
8816 2876176 : case BOOLEAN_TYPE:
8817 2876176 : case POINTER_TYPE:
8818 2876176 : case REFERENCE_TYPE:
8819 2876176 : case OFFSET_TYPE:
8820 2876176 : case BITINT_TYPE:
8821 2876176 : return native_interpret_int (type, ptr, len);
8822 :
8823 36473 : case REAL_TYPE:
8824 36473 : if (tree ret = native_interpret_real (type, ptr, len))
8825 : {
8826 : /* For floating point values in composite modes, punt if this
8827 : folding doesn't preserve bit representation. As the mode doesn't
8828 : have fixed precision while GCC pretends it does, there could be
8829 : valid values that GCC can't really represent accurately.
8830 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8831 : bit combinationations which GCC doesn't preserve. */
8832 36412 : unsigned char buf[24 * 2];
8833 36412 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8834 36412 : int total_bytes = GET_MODE_SIZE (mode);
8835 36412 : memcpy (buf + 24, ptr, total_bytes);
8836 36412 : clear_type_padding_in_mask (type, buf + 24);
8837 36412 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8838 36412 : || memcmp (buf + 24, buf, total_bytes) != 0)
8839 488 : return NULL_TREE;
8840 : return ret;
8841 : }
8842 : return NULL_TREE;
8843 :
8844 0 : case FIXED_POINT_TYPE:
8845 0 : return native_interpret_fixed (type, ptr, len);
8846 :
8847 1596 : case COMPLEX_TYPE:
8848 1596 : return native_interpret_complex (type, ptr, len);
8849 :
8850 82938 : case VECTOR_TYPE:
8851 82938 : return native_interpret_vector (type, ptr, len);
8852 :
8853 : default:
8854 : return NULL_TREE;
8855 : }
8856 : }
8857 :
8858 : /* Returns true if we can interpret the contents of a native encoding
8859 : as TYPE. */
8860 :
8861 : bool
8862 347287 : can_native_interpret_type_p (tree type)
8863 : {
8864 347287 : switch (TREE_CODE (type))
8865 : {
8866 : case INTEGER_TYPE:
8867 : case ENUMERAL_TYPE:
8868 : case BOOLEAN_TYPE:
8869 : case POINTER_TYPE:
8870 : case REFERENCE_TYPE:
8871 : case FIXED_POINT_TYPE:
8872 : case REAL_TYPE:
8873 : case COMPLEX_TYPE:
8874 : case VECTOR_TYPE:
8875 : case OFFSET_TYPE:
8876 : return true;
8877 38525 : default:
8878 38525 : return false;
8879 : }
8880 : }
8881 :
8882 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8883 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8884 :
8885 : tree
8886 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8887 : int len)
8888 : {
8889 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8890 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8891 : {
8892 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8893 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8894 : return NULL_TREE;
8895 :
8896 197 : HOST_WIDE_INT cnt = 0;
8897 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8898 : {
8899 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8900 : return NULL_TREE;
8901 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8902 : }
8903 197 : if (eltsz == 0)
8904 0 : cnt = 0;
8905 197 : HOST_WIDE_INT pos = 0;
8906 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8907 : {
8908 439 : tree v = NULL_TREE;
8909 439 : if (pos >= len || pos + eltsz > len)
8910 9631 : return NULL_TREE;
8911 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8912 : {
8913 367 : v = native_interpret_expr (TREE_TYPE (type),
8914 367 : ptr + off + pos, eltsz);
8915 367 : if (v == NULL_TREE)
8916 : return NULL_TREE;
8917 : }
8918 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8919 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8920 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8921 : eltsz);
8922 72 : if (v == NULL_TREE)
8923 : return NULL_TREE;
8924 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8925 : }
8926 197 : return build_constructor (type, elts);
8927 : }
8928 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8929 : return NULL_TREE;
8930 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8931 : {
8932 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8933 796932 : || is_empty_type (TREE_TYPE (field)))
8934 761072 : continue;
8935 13494 : tree fld = field;
8936 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8937 13494 : int diff = 0;
8938 13494 : tree v = NULL_TREE;
8939 13494 : if (DECL_BIT_FIELD (field))
8940 : {
8941 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8942 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8943 : {
8944 168 : poly_int64 bitoffset;
8945 168 : poly_uint64 field_offset, fld_offset;
8946 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8947 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8948 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8949 : else
8950 : bitoffset = 0;
8951 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8952 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8953 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8954 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8955 168 : if (!bitoffset.is_constant (&bitoff)
8956 168 : || bitoff < 0
8957 168 : || bitoff > diff)
8958 0 : return NULL_TREE;
8959 : }
8960 : else
8961 : {
8962 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8963 : return NULL_TREE;
8964 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8965 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8966 12 : bpos %= BITS_PER_UNIT;
8967 12 : fieldsize += bpos;
8968 12 : fieldsize += BITS_PER_UNIT - 1;
8969 12 : fieldsize /= BITS_PER_UNIT;
8970 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8971 12 : if (repr_type == NULL_TREE)
8972 : return NULL_TREE;
8973 12 : sz = int_size_in_bytes (repr_type);
8974 12 : if (sz < 0 || sz > len)
8975 : return NULL_TREE;
8976 12 : pos = int_byte_position (field);
8977 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8978 : return NULL_TREE;
8979 12 : HOST_WIDE_INT rpos;
8980 12 : if (pos + sz <= len)
8981 : rpos = pos;
8982 : else
8983 : {
8984 0 : rpos = len - sz;
8985 0 : gcc_assert (rpos <= pos);
8986 : }
8987 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8988 12 : pos = rpos;
8989 12 : diff = (TYPE_PRECISION (repr_type)
8990 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8991 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8992 12 : if (v == NULL_TREE)
8993 : return NULL_TREE;
8994 : fld = NULL_TREE;
8995 : }
8996 : }
8997 :
8998 168 : if (fld)
8999 : {
9000 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
9001 13482 : if (sz < 0 || sz > len)
9002 : return NULL_TREE;
9003 13482 : tree byte_pos = byte_position (fld);
9004 13482 : if (!tree_fits_shwi_p (byte_pos))
9005 : return NULL_TREE;
9006 13482 : pos = tree_to_shwi (byte_pos);
9007 13482 : if (pos < 0 || pos > len || pos + sz > len)
9008 : return NULL_TREE;
9009 : }
9010 13482 : if (fld == NULL_TREE)
9011 : /* Already handled above. */;
9012 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
9013 : {
9014 6262 : v = native_interpret_expr (TREE_TYPE (fld),
9015 6262 : ptr + off + pos, sz);
9016 6262 : if (v == NULL_TREE)
9017 : return NULL_TREE;
9018 : }
9019 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
9020 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
9021 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
9022 7232 : if (v == NULL_TREE)
9023 : return NULL_TREE;
9024 13494 : if (fld != field)
9025 : {
9026 180 : if (TREE_CODE (v) != INTEGER_CST)
9027 : return NULL_TREE;
9028 :
9029 : /* FIXME: Figure out how to handle PDP endian bitfields. */
9030 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9031 : return NULL_TREE;
9032 180 : if (!BYTES_BIG_ENDIAN)
9033 180 : v = wide_int_to_tree (TREE_TYPE (field),
9034 360 : wi::lrshift (wi::to_wide (v), bitoff));
9035 : else
9036 : v = wide_int_to_tree (TREE_TYPE (field),
9037 : wi::lrshift (wi::to_wide (v),
9038 : diff - bitoff));
9039 : }
9040 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
9041 : }
9042 9434 : return build_constructor (type, elts);
9043 : }
9044 :
9045 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
9046 : or extracted constant positions and/or sizes aren't byte aligned. */
9047 :
9048 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9049 : bits between adjacent elements. AMNT should be within
9050 : [0, BITS_PER_UNIT).
9051 : Example, AMNT = 2:
9052 : 00011111|11100000 << 2 = 01111111|10000000
9053 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9054 :
9055 : void
9056 30448 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9057 : unsigned int amnt)
9058 : {
9059 30448 : if (amnt == 0)
9060 : return;
9061 :
9062 17710 : unsigned char carry_over = 0U;
9063 17710 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9064 17710 : unsigned char clear_mask = (~0U) << amnt;
9065 :
9066 103909 : for (unsigned int i = 0; i < sz; i++)
9067 : {
9068 86199 : unsigned prev_carry_over = carry_over;
9069 86199 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9070 :
9071 86199 : ptr[i] <<= amnt;
9072 86199 : if (i != 0)
9073 : {
9074 68489 : ptr[i] &= clear_mask;
9075 68489 : ptr[i] |= prev_carry_over;
9076 : }
9077 : }
9078 : }
9079 :
9080 : /* Like shift_bytes_in_array_left but for big-endian.
9081 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9082 : bits between adjacent elements. AMNT should be within
9083 : [0, BITS_PER_UNIT).
9084 : Example, AMNT = 2:
9085 : 00011111|11100000 >> 2 = 00000111|11111000
9086 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9087 :
9088 : void
9089 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9090 : unsigned int amnt)
9091 : {
9092 8 : if (amnt == 0)
9093 : return;
9094 :
9095 4 : unsigned char carry_over = 0U;
9096 4 : unsigned char carry_mask = ~(~0U << amnt);
9097 :
9098 12 : for (unsigned int i = 0; i < sz; i++)
9099 : {
9100 8 : unsigned prev_carry_over = carry_over;
9101 8 : carry_over = ptr[i] & carry_mask;
9102 :
9103 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9104 8 : ptr[i] >>= amnt;
9105 8 : ptr[i] |= prev_carry_over;
9106 : }
9107 : }
9108 :
9109 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9110 : directly on the VECTOR_CST encoding, in a way that works for variable-
9111 : length vectors. Return the resulting VECTOR_CST on success or null
9112 : on failure. */
9113 :
9114 : static tree
9115 165945 : fold_view_convert_vector_encoding (tree type, tree expr)
9116 : {
9117 165945 : tree expr_type = TREE_TYPE (expr);
9118 165945 : poly_uint64 type_bits, expr_bits;
9119 165945 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9120 165945 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9121 : return NULL_TREE;
9122 :
9123 165945 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9124 165945 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9125 165945 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9126 165945 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9127 :
9128 : /* We can only preserve the semantics of a stepped pattern if the new
9129 : vector element is an integer of the same size. */
9130 165945 : if (VECTOR_CST_STEPPED_P (expr)
9131 165945 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9132 : return NULL_TREE;
9133 :
9134 : /* The number of bits needed to encode one element from every pattern
9135 : of the original vector. */
9136 156691 : unsigned int expr_sequence_bits
9137 156691 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9138 :
9139 : /* The number of bits needed to encode one element from every pattern
9140 : of the result. */
9141 156691 : unsigned int type_sequence_bits
9142 156691 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9143 :
9144 : /* Don't try to read more bytes than are available, which can happen
9145 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9146 : The general VIEW_CONVERT handling can cope with that case, so there's
9147 : no point complicating things here. */
9148 156691 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9149 156691 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9150 : BITS_PER_UNIT);
9151 156691 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9152 156691 : if (known_gt (buffer_bits, expr_bits))
9153 : return NULL_TREE;
9154 :
9155 : /* Get enough bytes of EXPR to form the new encoding. */
9156 156691 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9157 156691 : buffer.quick_grow (buffer_bytes);
9158 156691 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9159 156691 : buffer_bits / expr_elt_bits)
9160 : != (int) buffer_bytes)
9161 : return NULL_TREE;
9162 :
9163 : /* Re-encode the bytes as TYPE. */
9164 156691 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9165 313382 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9166 156691 : type_npatterns, nelts_per_pattern);
9167 156691 : }
9168 :
9169 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9170 : TYPE at compile-time. If we're unable to perform the conversion
9171 : return NULL_TREE. */
9172 :
9173 : static tree
9174 13333720 : fold_view_convert_expr (tree type, tree expr)
9175 : {
9176 13333720 : unsigned char buffer[128];
9177 13333720 : unsigned char *buf;
9178 13333720 : int len;
9179 13333720 : HOST_WIDE_INT l;
9180 :
9181 : /* Check that the host and target are sane. */
9182 13333720 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9183 : return NULL_TREE;
9184 :
9185 13333720 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9186 165945 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9187 : return res;
9188 :
9189 13177096 : l = int_size_in_bytes (type);
9190 13177096 : if (l > (int) sizeof (buffer)
9191 13177096 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9192 : {
9193 0 : buf = XALLOCAVEC (unsigned char, l);
9194 0 : len = l;
9195 : }
9196 : else
9197 : {
9198 : buf = buffer;
9199 : len = sizeof (buffer);
9200 : }
9201 13177096 : len = native_encode_expr (expr, buf, len);
9202 13177096 : if (len == 0)
9203 : return NULL_TREE;
9204 :
9205 1847418 : return native_interpret_expr (type, buf, len);
9206 : }
9207 :
9208 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9209 : to avoid confusing the gimplify process. */
9210 :
9211 : tree
9212 598888977 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9213 : {
9214 : /* The size of the object is not relevant when talking about its address. */
9215 598888977 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9216 0 : t = TREE_OPERAND (t, 0);
9217 :
9218 598888977 : if (INDIRECT_REF_P (t))
9219 : {
9220 63156216 : t = TREE_OPERAND (t, 0);
9221 :
9222 63156216 : if (TREE_TYPE (t) != ptrtype)
9223 40449378 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9224 : }
9225 535732761 : else if (TREE_CODE (t) == MEM_REF
9226 535732761 : && integer_zerop (TREE_OPERAND (t, 1)))
9227 : {
9228 1709781 : t = TREE_OPERAND (t, 0);
9229 :
9230 1709781 : if (TREE_TYPE (t) != ptrtype)
9231 1126301 : t = fold_convert_loc (loc, ptrtype, t);
9232 : }
9233 534022980 : else if (TREE_CODE (t) == MEM_REF
9234 534022980 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9235 662 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9236 : TREE_OPERAND (t, 0),
9237 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9238 534022318 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9239 : {
9240 31764462 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9241 :
9242 31764462 : if (TREE_TYPE (t) != ptrtype)
9243 16308 : t = fold_convert_loc (loc, ptrtype, t);
9244 : }
9245 : else
9246 502257856 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9247 :
9248 : return t;
9249 : }
9250 :
9251 : /* Build an expression for the address of T. */
9252 :
9253 : tree
9254 516521942 : build_fold_addr_expr_loc (location_t loc, tree t)
9255 : {
9256 516521942 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9257 :
9258 516521942 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9259 : }
9260 :
9261 : /* Fold a unary expression of code CODE and type TYPE with operand
9262 : OP0. Return the folded expression if folding is successful.
9263 : Otherwise, return NULL_TREE. */
9264 :
9265 : tree
9266 2103722043 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9267 : {
9268 2103722043 : tree tem;
9269 2103722043 : tree arg0;
9270 2103722043 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9271 :
9272 2103722043 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9273 : && TREE_CODE_LENGTH (code) == 1);
9274 :
9275 2103722043 : arg0 = op0;
9276 2103722043 : if (arg0)
9277 : {
9278 2103708761 : if (CONVERT_EXPR_CODE_P (code)
9279 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9280 : {
9281 : /* Don't use STRIP_NOPS, because signedness of argument type
9282 : matters. */
9283 1200755644 : STRIP_SIGN_NOPS (arg0);
9284 : }
9285 : else
9286 : {
9287 : /* Strip any conversions that don't change the mode. This
9288 : is safe for every expression, except for a comparison
9289 : expression because its signedness is derived from its
9290 : operands.
9291 :
9292 : Note that this is done as an internal manipulation within
9293 : the constant folder, in order to find the simplest
9294 : representation of the arguments so that their form can be
9295 : studied. In any cases, the appropriate type conversions
9296 : should be put back in the tree that will get out of the
9297 : constant folder. */
9298 902953117 : STRIP_NOPS (arg0);
9299 : }
9300 :
9301 2103708761 : if (CONSTANT_CLASS_P (arg0))
9302 : {
9303 331120616 : tree tem = const_unop (code, type, arg0);
9304 331120616 : if (tem)
9305 : {
9306 290578175 : if (TREE_TYPE (tem) != type)
9307 9977 : tem = fold_convert_loc (loc, type, tem);
9308 : return tem;
9309 : }
9310 : }
9311 : }
9312 :
9313 1813143868 : tem = generic_simplify (loc, code, type, op0);
9314 1813143868 : if (tem)
9315 : return tem;
9316 :
9317 1363215566 : if (TREE_CODE_CLASS (code) == tcc_unary)
9318 : {
9319 776822090 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9320 1090534 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9321 : fold_build1_loc (loc, code, type,
9322 1090534 : fold_convert_loc (loc, TREE_TYPE (op0),
9323 2181068 : TREE_OPERAND (arg0, 1))));
9324 775731556 : else if (TREE_CODE (arg0) == COND_EXPR)
9325 : {
9326 561311 : tree arg01 = TREE_OPERAND (arg0, 1);
9327 561311 : tree arg02 = TREE_OPERAND (arg0, 2);
9328 561311 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9329 557129 : arg01 = fold_build1_loc (loc, code, type,
9330 : fold_convert_loc (loc,
9331 557129 : TREE_TYPE (op0), arg01));
9332 561311 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9333 550886 : arg02 = fold_build1_loc (loc, code, type,
9334 : fold_convert_loc (loc,
9335 550886 : TREE_TYPE (op0), arg02));
9336 561311 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9337 : arg01, arg02);
9338 :
9339 : /* If this was a conversion, and all we did was to move into
9340 : inside the COND_EXPR, bring it back out. But leave it if
9341 : it is a conversion from integer to integer and the
9342 : result precision is no wider than a word since such a
9343 : conversion is cheap and may be optimized away by combine,
9344 : while it couldn't if it were outside the COND_EXPR. Then return
9345 : so we don't get into an infinite recursion loop taking the
9346 : conversion out and then back in. */
9347 :
9348 561311 : if ((CONVERT_EXPR_CODE_P (code)
9349 10449 : || code == NON_LVALUE_EXPR)
9350 550881 : && TREE_CODE (tem) == COND_EXPR
9351 531193 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9352 467457 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9353 293925 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9354 293713 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9355 293713 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9356 293713 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9357 876089 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9358 21942 : && (INTEGRAL_TYPE_P
9359 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9360 21902 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9361 21789 : || flag_syntax_only))
9362 271087 : tem = build1_loc (loc, code, type,
9363 : build3 (COND_EXPR,
9364 271087 : TREE_TYPE (TREE_OPERAND
9365 : (TREE_OPERAND (tem, 1), 0)),
9366 271087 : TREE_OPERAND (tem, 0),
9367 271087 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9368 271087 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9369 : 0)));
9370 : return tem;
9371 : }
9372 : }
9373 :
9374 1361563721 : switch (code)
9375 : {
9376 43401436 : case NON_LVALUE_EXPR:
9377 43401436 : if (!maybe_lvalue_p (op0))
9378 31596824 : return fold_convert_loc (loc, type, op0);
9379 : return NULL_TREE;
9380 :
9381 721175957 : CASE_CONVERT:
9382 721175957 : case FLOAT_EXPR:
9383 721175957 : case FIX_TRUNC_EXPR:
9384 721175957 : if (COMPARISON_CLASS_P (op0))
9385 : {
9386 : /* If we have (type) (a CMP b) and type is an integral type, return
9387 : new expression involving the new type. Canonicalize
9388 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9389 : non-integral type.
9390 : Do not fold the result as that would not simplify further, also
9391 : folding again results in recursions. */
9392 749856 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9393 167531 : return build2_loc (loc, TREE_CODE (op0), type,
9394 167531 : TREE_OPERAND (op0, 0),
9395 335062 : TREE_OPERAND (op0, 1));
9396 582325 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9397 7671 : && TREE_CODE (type) != VECTOR_TYPE)
9398 7671 : return build3_loc (loc, COND_EXPR, type, op0,
9399 : constant_boolean_node (true, type),
9400 7671 : constant_boolean_node (false, type));
9401 : }
9402 :
9403 : /* Handle (T *)&A.B.C for A being of type T and B and C
9404 : living at offset zero. This occurs frequently in
9405 : C++ upcasting and then accessing the base. */
9406 721000755 : if (TREE_CODE (op0) == ADDR_EXPR
9407 253611011 : && POINTER_TYPE_P (type)
9408 967702643 : && handled_component_p (TREE_OPERAND (op0, 0)))
9409 : {
9410 54414207 : poly_int64 bitsize, bitpos;
9411 54414207 : tree offset;
9412 54414207 : machine_mode mode;
9413 54414207 : int unsignedp, reversep, volatilep;
9414 54414207 : tree base
9415 54414207 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9416 : &offset, &mode, &unsignedp, &reversep,
9417 : &volatilep);
9418 : /* If the reference was to a (constant) zero offset, we can use
9419 : the address of the base if it has the same base type
9420 : as the result type and the pointer type is unqualified. */
9421 54414207 : if (!offset
9422 54267858 : && known_eq (bitpos, 0)
9423 37445954 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9424 37445954 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9425 54427306 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9426 12898 : return fold_convert_loc (loc, type,
9427 12898 : build_fold_addr_expr_loc (loc, base));
9428 : }
9429 :
9430 720987857 : if (TREE_CODE (op0) == MODIFY_EXPR
9431 293693 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9432 : /* Detect assigning a bitfield. */
9433 720989910 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9434 120 : && DECL_BIT_FIELD
9435 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9436 : {
9437 : /* Don't leave an assignment inside a conversion
9438 : unless assigning a bitfield. */
9439 2005 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9440 : /* First do the assignment, then return converted constant. */
9441 2005 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9442 2005 : suppress_warning (tem /* What warning? */);
9443 2005 : TREE_USED (tem) = 1;
9444 2005 : return tem;
9445 : }
9446 :
9447 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9448 : constants (if x has signed type, the sign bit cannot be set
9449 : in c). This folds extension into the BIT_AND_EXPR.
9450 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9451 : very likely don't have maximal range for their precision and this
9452 : transformation effectively doesn't preserve non-maximal ranges. */
9453 720985852 : if (TREE_CODE (type) == INTEGER_TYPE
9454 249552013 : && TREE_CODE (op0) == BIT_AND_EXPR
9455 721605657 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9456 : {
9457 219510 : tree and_expr = op0;
9458 219510 : tree and0 = TREE_OPERAND (and_expr, 0);
9459 219510 : tree and1 = TREE_OPERAND (and_expr, 1);
9460 219510 : int change = 0;
9461 :
9462 219510 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9463 219510 : || (TYPE_PRECISION (type)
9464 59650 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9465 : change = 1;
9466 18238 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9467 : <= HOST_BITS_PER_WIDE_INT
9468 18238 : && tree_fits_uhwi_p (and1))
9469 : {
9470 17123 : unsigned HOST_WIDE_INT cst;
9471 :
9472 17123 : cst = tree_to_uhwi (and1);
9473 34246 : cst &= HOST_WIDE_INT_M1U
9474 17123 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9475 17123 : change = (cst == 0);
9476 17123 : if (change
9477 17123 : && !flag_syntax_only
9478 34246 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9479 : == ZERO_EXTEND))
9480 : {
9481 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9482 : and0 = fold_convert_loc (loc, uns, and0);
9483 : and1 = fold_convert_loc (loc, uns, and1);
9484 : }
9485 : }
9486 17123 : if (change)
9487 : {
9488 218395 : tree and1_type = TREE_TYPE (and1);
9489 218395 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9490 : TYPE_PRECISION (type));
9491 218395 : tem = force_fit_type (type,
9492 218395 : wide_int::from (wi::to_wide (and1), prec,
9493 218395 : TYPE_SIGN (and1_type)),
9494 218395 : 0, TREE_OVERFLOW (and1));
9495 218395 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9496 218395 : fold_convert_loc (loc, type, and0), tem);
9497 : }
9498 : }
9499 :
9500 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9501 : cast (T1)X will fold away. We assume that this happens when X itself
9502 : is a cast. */
9503 720767457 : if (POINTER_TYPE_P (type)
9504 436026437 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9505 754657146 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9506 : {
9507 31252959 : tree arg00 = TREE_OPERAND (arg0, 0);
9508 31252959 : tree arg01 = TREE_OPERAND (arg0, 1);
9509 :
9510 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9511 : when the pointed type needs higher alignment than
9512 : the p+ first operand's pointed type. */
9513 31252959 : if (!in_gimple_form
9514 31232355 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9515 31254173 : && (min_align_of_type (TREE_TYPE (type))
9516 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9517 : return NULL_TREE;
9518 :
9519 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9520 : when type is a reference type and arg00's type is not,
9521 : because arg00 could be validly nullptr and if arg01 doesn't return,
9522 : we don't want false positive binding of reference to nullptr. */
9523 31252892 : if (TREE_CODE (type) == REFERENCE_TYPE
9524 17585332 : && !in_gimple_form
9525 17585312 : && sanitize_flags_p (SANITIZE_NULL)
9526 31253323 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9527 : return NULL_TREE;
9528 :
9529 31252461 : arg00 = fold_convert_loc (loc, type, arg00);
9530 31252461 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9531 : }
9532 :
9533 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9534 : of the same precision, and X is an integer type not narrower than
9535 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9536 689514498 : if (INTEGRAL_TYPE_P (type)
9537 255831315 : && TREE_CODE (op0) == BIT_NOT_EXPR
9538 587157 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9539 587157 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9540 689901506 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9541 : {
9542 384604 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9543 458472 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9544 458470 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9545 319259 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9546 319259 : fold_convert_loc (loc, type, tem));
9547 : }
9548 :
9549 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9550 : type of X and Y (integer types only). */
9551 689195239 : if (INTEGRAL_TYPE_P (type)
9552 255512056 : && TREE_CODE (op0) == MULT_EXPR
9553 9266537 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9554 9245481 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9555 689255113 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9556 21028 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9557 : {
9558 : /* Be careful not to introduce new overflows. */
9559 59820 : tree mult_type;
9560 59820 : if (TYPE_OVERFLOW_WRAPS (type))
9561 : mult_type = type;
9562 : else
9563 2104 : mult_type = unsigned_type_for (type);
9564 :
9565 59820 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9566 : {
9567 119640 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9568 : fold_convert_loc (loc, mult_type,
9569 59820 : TREE_OPERAND (op0, 0)),
9570 : fold_convert_loc (loc, mult_type,
9571 59820 : TREE_OPERAND (op0, 1)));
9572 59820 : return fold_convert_loc (loc, type, tem);
9573 : }
9574 : }
9575 :
9576 : return NULL_TREE;
9577 :
9578 238853929 : case VIEW_CONVERT_EXPR:
9579 238853929 : if (TREE_CODE (op0) == MEM_REF)
9580 : {
9581 2722 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9582 18 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9583 2722 : tem = fold_build2_loc (loc, MEM_REF, type,
9584 2722 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9585 2722 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9586 2722 : return tem;
9587 : }
9588 :
9589 : return NULL_TREE;
9590 :
9591 4266160 : case NEGATE_EXPR:
9592 4266160 : tem = fold_negate_expr (loc, arg0);
9593 4266160 : if (tem)
9594 1692 : return fold_convert_loc (loc, type, tem);
9595 : return NULL_TREE;
9596 :
9597 3481182 : case ABS_EXPR:
9598 : /* Convert fabs((double)float) into (double)fabsf(float). */
9599 3481182 : if (TREE_CODE (arg0) == NOP_EXPR
9600 23649 : && TREE_CODE (type) == REAL_TYPE)
9601 : {
9602 23595 : tree targ0 = strip_float_extensions (arg0);
9603 23595 : if (targ0 != arg0)
9604 23391 : return fold_convert_loc (loc, type,
9605 : fold_build1_loc (loc, ABS_EXPR,
9606 23391 : TREE_TYPE (targ0),
9607 23391 : targ0));
9608 : }
9609 : return NULL_TREE;
9610 :
9611 2839446 : case BIT_NOT_EXPR:
9612 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9613 2839446 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9614 2841138 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9615 : fold_convert_loc (loc, type,
9616 1692 : TREE_OPERAND (arg0, 0)))))
9617 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9618 : fold_convert_loc (loc, type,
9619 28 : TREE_OPERAND (arg0, 1)));
9620 2839432 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9621 2841110 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9622 : fold_convert_loc (loc, type,
9623 1678 : TREE_OPERAND (arg0, 1)))))
9624 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9625 : fold_convert_loc (loc, type,
9626 46 : TREE_OPERAND (arg0, 0)), tem);
9627 :
9628 : return NULL_TREE;
9629 :
9630 51486608 : case TRUTH_NOT_EXPR:
9631 : /* Note that the operand of this must be an int
9632 : and its values must be 0 or 1.
9633 : ("true" is a fixed value perhaps depending on the language,
9634 : but we don't handle values other than 1 correctly yet.) */
9635 51486608 : tem = fold_truth_not_expr (loc, arg0);
9636 51486608 : if (!tem)
9637 : return NULL_TREE;
9638 34806804 : return fold_convert_loc (loc, type, tem);
9639 :
9640 70549170 : case INDIRECT_REF:
9641 : /* Fold *&X to X if X is an lvalue. */
9642 70549170 : if (TREE_CODE (op0) == ADDR_EXPR)
9643 : {
9644 7909 : tree op00 = TREE_OPERAND (op0, 0);
9645 7909 : if ((VAR_P (op00)
9646 : || TREE_CODE (op00) == PARM_DECL
9647 : || TREE_CODE (op00) == RESULT_DECL)
9648 6763 : && !TREE_READONLY (op00))
9649 6646 : return op00;
9650 : }
9651 : return NULL_TREE;
9652 :
9653 : default:
9654 : return NULL_TREE;
9655 : } /* switch (code) */
9656 : }
9657 :
9658 :
9659 : /* If the operation was a conversion do _not_ mark a resulting constant
9660 : with TREE_OVERFLOW if the original constant was not. These conversions
9661 : have implementation defined behavior and retaining the TREE_OVERFLOW
9662 : flag here would confuse later passes such as VRP. */
9663 : tree
9664 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9665 : tree type, tree op0)
9666 : {
9667 0 : tree res = fold_unary_loc (loc, code, type, op0);
9668 0 : if (res
9669 0 : && TREE_CODE (res) == INTEGER_CST
9670 0 : && TREE_CODE (op0) == INTEGER_CST
9671 0 : && CONVERT_EXPR_CODE_P (code))
9672 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9673 :
9674 0 : return res;
9675 : }
9676 :
9677 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9678 : operands OP0 and OP1. LOC is the location of the resulting expression.
9679 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9680 : Return the folded expression if folding is successful. Otherwise,
9681 : return NULL_TREE. */
9682 : static tree
9683 25742944 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9684 : tree arg0, tree arg1, tree op0, tree op1)
9685 : {
9686 25742944 : tree tem;
9687 :
9688 : /* We only do these simplifications if we are optimizing. */
9689 25742944 : if (!optimize)
9690 : return NULL_TREE;
9691 :
9692 : /* Check for things like (A || B) && (A || C). We can convert this
9693 : to A || (B && C). Note that either operator can be any of the four
9694 : truth and/or operations and the transformation will still be
9695 : valid. Also note that we only care about order for the
9696 : ANDIF and ORIF operators. If B contains side effects, this
9697 : might change the truth-value of A. */
9698 25250438 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9699 5988100 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9700 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9701 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9702 5988100 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9703 25313533 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9704 : {
9705 62587 : tree a00 = TREE_OPERAND (arg0, 0);
9706 62587 : tree a01 = TREE_OPERAND (arg0, 1);
9707 62587 : tree a10 = TREE_OPERAND (arg1, 0);
9708 62587 : tree a11 = TREE_OPERAND (arg1, 1);
9709 125174 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9710 62587 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9711 62587 : && (code == TRUTH_AND_EXPR
9712 23280 : || code == TRUTH_OR_EXPR));
9713 :
9714 62587 : if (operand_equal_p (a00, a10, 0))
9715 837 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9716 837 : fold_build2_loc (loc, code, type, a01, a11));
9717 61750 : else if (commutative && operand_equal_p (a00, a11, 0))
9718 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9719 0 : fold_build2_loc (loc, code, type, a01, a10));
9720 61750 : else if (commutative && operand_equal_p (a01, a10, 0))
9721 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9722 0 : fold_build2_loc (loc, code, type, a00, a11));
9723 :
9724 : /* This case if tricky because we must either have commutative
9725 : operators or else A10 must not have side-effects. */
9726 :
9727 61697 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9728 122919 : && operand_equal_p (a01, a11, 0))
9729 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9730 : fold_build2_loc (loc, code, type, a00, a10),
9731 43 : a01);
9732 : }
9733 :
9734 : /* See if we can build a range comparison. */
9735 25249558 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9736 : return tem;
9737 :
9738 24072130 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9739 24070106 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9740 : {
9741 40368 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9742 40368 : if (tem)
9743 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9744 : }
9745 :
9746 24072117 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9747 24060963 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9748 : {
9749 89997 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9750 89997 : if (tem)
9751 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9752 : }
9753 :
9754 : /* Check for the possibility of merging component references. If our
9755 : lhs is another similar operation, try to merge its rhs with our
9756 : rhs. Then try to merge our lhs and rhs. */
9757 24072026 : if (TREE_CODE (arg0) == code
9758 24924960 : && (tem = fold_truth_andor_1 (loc, code, type,
9759 852934 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9760 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9761 :
9762 24071941 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9763 : return tem;
9764 :
9765 24032389 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9766 24032389 : if (param_logical_op_non_short_circuit != -1)
9767 7747 : logical_op_non_short_circuit
9768 7747 : = param_logical_op_non_short_circuit;
9769 24032389 : if (logical_op_non_short_circuit
9770 24028479 : && !sanitize_coverage_p ()
9771 24032389 : && (code == TRUTH_AND_EXPR
9772 24028476 : || code == TRUTH_ANDIF_EXPR
9773 11079909 : || code == TRUTH_OR_EXPR
9774 11079909 : || code == TRUTH_ORIF_EXPR))
9775 : {
9776 24028476 : enum tree_code ncode, icode;
9777 :
9778 24028476 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9779 24028476 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9780 11079909 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9781 :
9782 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9783 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9784 : We don't want to pack more than two leafs to a non-IF AND/OR
9785 : expression.
9786 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9787 : equal to IF-CODE, then we don't want to add right-hand operand.
9788 : If the inner right-hand side of left-hand operand has
9789 : side-effects, or isn't simple, then we can't add to it,
9790 : as otherwise we might destroy if-sequence. */
9791 24028476 : if (TREE_CODE (arg0) == icode
9792 843998 : && simple_condition_p (arg1)
9793 : /* Needed for sequence points to handle trappings, and
9794 : side-effects. */
9795 24077671 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9796 : {
9797 42320 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9798 : arg1);
9799 42320 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9800 42320 : tem);
9801 : }
9802 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9803 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9804 23986156 : else if (TREE_CODE (arg1) == icode
9805 6358 : && simple_condition_p (arg0)
9806 : /* Needed for sequence points to handle trappings, and
9807 : side-effects. */
9808 23987113 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9809 : {
9810 36 : tem = fold_build2_loc (loc, ncode, type,
9811 36 : arg0, TREE_OPERAND (arg1, 0));
9812 36 : return fold_build2_loc (loc, icode, type, tem,
9813 72 : TREE_OPERAND (arg1, 1));
9814 : }
9815 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9816 : into (A OR B).
9817 : For sequence point consistency, we need to check for trapping,
9818 : and side-effects. */
9819 5392518 : else if (code == icode && simple_condition_p (arg0)
9820 24884367 : && simple_condition_p (arg1))
9821 446729 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9822 : }
9823 :
9824 : return NULL_TREE;
9825 : }
9826 :
9827 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9828 : by changing CODE to reduce the magnitude of constants involved in
9829 : ARG0 of the comparison.
9830 : Returns a canonicalized comparison tree if a simplification was
9831 : possible, otherwise returns NULL_TREE. */
9832 :
9833 : static tree
9834 182736387 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9835 : tree arg0, tree arg1)
9836 : {
9837 182736387 : enum tree_code code0 = TREE_CODE (arg0);
9838 182736387 : tree t, cst0 = NULL_TREE;
9839 182736387 : int sgn0;
9840 :
9841 : /* Match A +- CST code arg1. We can change this only if overflow
9842 : is undefined. */
9843 182736387 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9844 139041206 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9845 : /* In principle pointers also have undefined overflow behavior,
9846 : but that causes problems elsewhere. */
9847 68171102 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9848 68171102 : && (code0 == MINUS_EXPR
9849 68171102 : || code0 == PLUS_EXPR)
9850 2595142 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9851 : return NULL_TREE;
9852 :
9853 : /* Identify the constant in arg0 and its sign. */
9854 2129961 : cst0 = TREE_OPERAND (arg0, 1);
9855 2129961 : sgn0 = tree_int_cst_sgn (cst0);
9856 :
9857 : /* Overflowed constants and zero will cause problems. */
9858 2129961 : if (integer_zerop (cst0)
9859 2129961 : || TREE_OVERFLOW (cst0))
9860 : return NULL_TREE;
9861 :
9862 : /* See if we can reduce the magnitude of the constant in
9863 : arg0 by changing the comparison code. */
9864 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9865 2129961 : if (code == LT_EXPR
9866 1256137 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9867 : code = LE_EXPR;
9868 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9869 1950585 : else if (code == GT_EXPR
9870 581342 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9871 : code = GE_EXPR;
9872 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9873 1766616 : else if (code == LE_EXPR
9874 678856 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9875 : code = LT_EXPR;
9876 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9877 1525711 : else if (code == GE_EXPR
9878 475102 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9879 : code = GT_EXPR;
9880 : else
9881 : return NULL_TREE;
9882 :
9883 : /* Now build the constant reduced in magnitude. But not if that
9884 : would produce one outside of its types range. */
9885 1614908 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9886 1614908 : && ((sgn0 == 1
9887 424736 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9888 424736 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9889 807454 : || (sgn0 == -1
9890 382718 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9891 382718 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9892 : return NULL_TREE;
9893 :
9894 1232190 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9895 807454 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9896 807454 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9897 807454 : t = fold_convert (TREE_TYPE (arg1), t);
9898 :
9899 807454 : return fold_build2_loc (loc, code, type, t, arg1);
9900 : }
9901 :
9902 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9903 : overflow further. Try to decrease the magnitude of constants involved
9904 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9905 : and put sole constants at the second argument position.
9906 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9907 :
9908 : static tree
9909 91750262 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9910 : tree arg0, tree arg1)
9911 : {
9912 91750262 : tree t;
9913 :
9914 : /* Try canonicalization by simplifying arg0. */
9915 91750262 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9916 91750262 : if (t)
9917 : return t;
9918 :
9919 : /* Try canonicalization by simplifying arg1 using the swapped
9920 : comparison. */
9921 90986125 : code = swap_tree_comparison (code);
9922 90986125 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9923 90986125 : return t;
9924 : }
9925 :
9926 : /* Return a positive integer when the symbol DECL is known to have
9927 : a nonzero address, zero when it's known not to (e.g., it's a weak
9928 : symbol), and a negative integer when the symbol is not yet in the
9929 : symbol table and so whether or not its address is zero is unknown.
9930 : For function local objects always return positive integer. */
9931 : static int
9932 12925485 : maybe_nonzero_address (tree decl)
9933 : {
9934 12925485 : if (!DECL_P (decl))
9935 : return -1;
9936 :
9937 : /* Normally, don't do anything for variables and functions before symtab is
9938 : built; it is quite possible that DECL will be declared weak later.
9939 : But if folding_initializer, we need a constant answer now, so create
9940 : the symtab entry and prevent later weak declaration. */
9941 10586279 : if (decl_in_symtab_p (decl))
9942 : {
9943 4583865 : if (struct symtab_node *symbol
9944 4583865 : = (folding_initializer
9945 4583865 : ? symtab_node::get_create (decl)
9946 4566540 : : symtab_node::get (decl)))
9947 4564846 : return symbol->nonzero_address ();
9948 : }
9949 6002414 : else if (folding_cxx_constexpr)
9950 : /* Anything that doesn't go in the symtab has non-zero address. */
9951 : return 1;
9952 :
9953 : /* Function local objects are never NULL. */
9954 5864769 : if (DECL_CONTEXT (decl)
9955 5847216 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9956 11708522 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9957 5762130 : return 1;
9958 :
9959 : return -1;
9960 : }
9961 :
9962 : /* Subroutine of fold_binary. This routine performs all of the
9963 : transformations that are common to the equality/inequality
9964 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9965 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9966 : fold_binary should call fold_binary. Fold a comparison with
9967 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9968 : the folded comparison or NULL_TREE. */
9969 :
9970 : static tree
9971 91822953 : fold_comparison (location_t loc, enum tree_code code, tree type,
9972 : tree op0, tree op1)
9973 : {
9974 91822953 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9975 91822953 : tree arg0, arg1, tem;
9976 :
9977 91822953 : arg0 = op0;
9978 91822953 : arg1 = op1;
9979 :
9980 91822953 : STRIP_SIGN_NOPS (arg0);
9981 91822953 : STRIP_SIGN_NOPS (arg1);
9982 :
9983 : /* For comparisons of pointers we can decompose it to a compile time
9984 : comparison of the base objects and the offsets into the object.
9985 : This requires at least one operand being an ADDR_EXPR or a
9986 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9987 169805943 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9988 92045601 : && (TREE_CODE (arg0) == ADDR_EXPR
9989 14009495 : || TREE_CODE (arg1) == ADDR_EXPR
9990 12398349 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9991 11596810 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9992 : {
9993 2475933 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9994 2475933 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9995 2475933 : machine_mode mode;
9996 2475933 : int volatilep, reversep, unsignedp;
9997 2475933 : bool indirect_base0 = false, indirect_base1 = false;
9998 :
9999 : /* Get base and offset for the access. Strip ADDR_EXPR for
10000 : get_inner_reference, but put it back by stripping INDIRECT_REF
10001 : off the base object if possible. indirect_baseN will be true
10002 : if baseN is not an address but refers to the object itself. */
10003 2475933 : base0 = arg0;
10004 2475933 : if (TREE_CODE (arg0) == ADDR_EXPR)
10005 : {
10006 53116 : base0
10007 53116 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10008 : &bitsize, &bitpos0, &offset0, &mode,
10009 : &unsignedp, &reversep, &volatilep);
10010 53116 : if (INDIRECT_REF_P (base0))
10011 1952 : base0 = TREE_OPERAND (base0, 0);
10012 : else
10013 : indirect_base0 = true;
10014 : }
10015 2422817 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10016 : {
10017 863974 : base0 = TREE_OPERAND (arg0, 0);
10018 863974 : STRIP_SIGN_NOPS (base0);
10019 863974 : if (TREE_CODE (base0) == ADDR_EXPR)
10020 : {
10021 27902 : base0
10022 27902 : = get_inner_reference (TREE_OPERAND (base0, 0),
10023 : &bitsize, &bitpos0, &offset0, &mode,
10024 : &unsignedp, &reversep, &volatilep);
10025 27902 : if (INDIRECT_REF_P (base0))
10026 20 : base0 = TREE_OPERAND (base0, 0);
10027 : else
10028 : indirect_base0 = true;
10029 : }
10030 863974 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10031 863974 : offset0 = TREE_OPERAND (arg0, 1);
10032 : else
10033 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10034 : TREE_OPERAND (arg0, 1));
10035 863974 : if (poly_int_tree_p (offset0))
10036 : {
10037 695710 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10038 695710 : TYPE_PRECISION (sizetype));
10039 695710 : tem <<= LOG2_BITS_PER_UNIT;
10040 695710 : tem += bitpos0;
10041 695710 : if (tem.to_shwi (&bitpos0))
10042 695710 : offset0 = NULL_TREE;
10043 : }
10044 : }
10045 :
10046 2475933 : base1 = arg1;
10047 2475933 : if (TREE_CODE (arg1) == ADDR_EXPR)
10048 : {
10049 1641139 : base1
10050 1641139 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10051 : &bitsize, &bitpos1, &offset1, &mode,
10052 : &unsignedp, &reversep, &volatilep);
10053 1641139 : if (INDIRECT_REF_P (base1))
10054 71066 : base1 = TREE_OPERAND (base1, 0);
10055 : else
10056 : indirect_base1 = true;
10057 : }
10058 834794 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10059 : {
10060 86765 : base1 = TREE_OPERAND (arg1, 0);
10061 86765 : STRIP_SIGN_NOPS (base1);
10062 86765 : if (TREE_CODE (base1) == ADDR_EXPR)
10063 : {
10064 11666 : base1
10065 11666 : = get_inner_reference (TREE_OPERAND (base1, 0),
10066 : &bitsize, &bitpos1, &offset1, &mode,
10067 : &unsignedp, &reversep, &volatilep);
10068 11666 : if (INDIRECT_REF_P (base1))
10069 0 : base1 = TREE_OPERAND (base1, 0);
10070 : else
10071 : indirect_base1 = true;
10072 : }
10073 86765 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10074 86741 : offset1 = TREE_OPERAND (arg1, 1);
10075 : else
10076 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10077 : TREE_OPERAND (arg1, 1));
10078 86765 : if (poly_int_tree_p (offset1))
10079 : {
10080 76061 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10081 76061 : TYPE_PRECISION (sizetype));
10082 76061 : tem <<= LOG2_BITS_PER_UNIT;
10083 76061 : tem += bitpos1;
10084 76061 : if (tem.to_shwi (&bitpos1))
10085 76061 : offset1 = NULL_TREE;
10086 : }
10087 : }
10088 :
10089 : /* If we have equivalent bases we might be able to simplify. */
10090 2475933 : if (indirect_base0 == indirect_base1
10091 3327880 : && operand_equal_p (base0, base1,
10092 : indirect_base0 ? OEP_ADDRESS_OF : 0))
10093 : {
10094 : /* We can fold this expression to a constant if the non-constant
10095 : offset parts are equal. */
10096 17228 : if ((offset0 == offset1
10097 6726 : || (offset0 && offset1
10098 2742 : && operand_equal_p (offset0, offset1, 0)))
10099 17228 : && (equality_code
10100 10458 : || (indirect_base0
10101 6624 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10102 3834 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10103 : {
10104 10453 : switch (code)
10105 : {
10106 35 : case EQ_EXPR:
10107 35 : if (known_eq (bitpos0, bitpos1))
10108 49749 : return constant_boolean_node (true, type);
10109 21 : if (known_ne (bitpos0, bitpos1))
10110 21 : return constant_boolean_node (false, type);
10111 : break;
10112 9 : case NE_EXPR:
10113 9 : if (known_ne (bitpos0, bitpos1))
10114 7 : return constant_boolean_node (true, type);
10115 2 : if (known_eq (bitpos0, bitpos1))
10116 2 : return constant_boolean_node (false, type);
10117 : break;
10118 2359 : case LT_EXPR:
10119 2359 : if (known_lt (bitpos0, bitpos1))
10120 2215 : return constant_boolean_node (true, type);
10121 144 : if (known_ge (bitpos0, bitpos1))
10122 144 : return constant_boolean_node (false, type);
10123 : break;
10124 1893 : case LE_EXPR:
10125 1893 : if (known_le (bitpos0, bitpos1))
10126 173 : return constant_boolean_node (true, type);
10127 1720 : if (known_gt (bitpos0, bitpos1))
10128 1720 : return constant_boolean_node (false, type);
10129 : break;
10130 3765 : case GE_EXPR:
10131 3765 : if (known_ge (bitpos0, bitpos1))
10132 1687 : return constant_boolean_node (true, type);
10133 2078 : if (known_lt (bitpos0, bitpos1))
10134 2078 : return constant_boolean_node (false, type);
10135 : break;
10136 2392 : case GT_EXPR:
10137 2392 : if (known_gt (bitpos0, bitpos1))
10138 2333 : return constant_boolean_node (true, type);
10139 59 : if (known_le (bitpos0, bitpos1))
10140 59 : return constant_boolean_node (false, type);
10141 : break;
10142 : default:;
10143 : }
10144 : }
10145 : /* We can simplify the comparison to a comparison of the variable
10146 : offset parts if the constant offset parts are equal.
10147 : Be careful to use signed sizetype here because otherwise we
10148 : mess with array offsets in the wrong way. This is possible
10149 : because pointer arithmetic is restricted to retain within an
10150 : object and overflow on pointer differences is undefined as of
10151 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10152 6775 : else if (known_eq (bitpos0, bitpos1)
10153 6775 : && (equality_code
10154 5294 : || (indirect_base0
10155 285 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10156 5009 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10157 : {
10158 : /* By converting to signed sizetype we cover middle-end pointer
10159 : arithmetic which operates on unsigned pointer types of size
10160 : type size and ARRAY_REF offsets which are properly sign or
10161 : zero extended from their type in case it is narrower than
10162 : sizetype. */
10163 5403 : if (offset0 == NULL_TREE)
10164 0 : offset0 = build_int_cst (ssizetype, 0);
10165 : else
10166 5403 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10167 5403 : if (offset1 == NULL_TREE)
10168 2693 : offset1 = build_int_cst (ssizetype, 0);
10169 : else
10170 2710 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10171 :
10172 5403 : return fold_build2_loc (loc, code, type, offset0, offset1);
10173 : }
10174 : }
10175 : /* For equal offsets we can simplify to a comparison of the
10176 : base addresses. */
10177 2458705 : else if (known_eq (bitpos0, bitpos1)
10178 55440 : && (indirect_base0
10179 1018025 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10180 16117 : && (indirect_base1
10181 192884 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10182 2689767 : && ((offset0 == offset1)
10183 4409 : || (offset0 && offset1
10184 4139 : && operand_equal_p (offset0, offset1, 0))))
10185 : {
10186 33770 : if (indirect_base0)
10187 3837 : base0 = build_fold_addr_expr_loc (loc, base0);
10188 33770 : if (indirect_base1)
10189 5486 : base1 = build_fold_addr_expr_loc (loc, base1);
10190 33770 : return fold_build2_loc (loc, code, type, base0, base1);
10191 : }
10192 : /* Comparison between an ordinary (non-weak) symbol and a null
10193 : pointer can be eliminated since such symbols must have a non
10194 : null address. In C, relational expressions between pointers
10195 : to objects and null pointers are undefined. The results
10196 : below follow the C++ rules with the additional property that
10197 : every object pointer compares greater than a null pointer.
10198 : */
10199 2424935 : else if (((DECL_P (base0)
10200 257272 : && maybe_nonzero_address (base0) > 0
10201 : /* Avoid folding references to struct members at offset 0 to
10202 : prevent tests like '&ptr->firstmember == 0' from getting
10203 : eliminated. When ptr is null, although the -> expression
10204 : is strictly speaking invalid, GCC retains it as a matter
10205 : of QoI. See PR c/44555. */
10206 242365 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10207 2408856 : || CONSTANT_CLASS_P (base0))
10208 20861 : && indirect_base0
10209 : /* The caller guarantees that when one of the arguments is
10210 : constant (i.e., null in this case) it is second. */
10211 2442910 : && integer_zerop (arg1))
10212 : {
10213 123 : switch (code)
10214 : {
10215 24 : case EQ_EXPR:
10216 24 : case LE_EXPR:
10217 24 : case LT_EXPR:
10218 24 : return constant_boolean_node (false, type);
10219 99 : case GE_EXPR:
10220 99 : case GT_EXPR:
10221 99 : case NE_EXPR:
10222 99 : return constant_boolean_node (true, type);
10223 0 : default:
10224 0 : gcc_unreachable ();
10225 : }
10226 : }
10227 : }
10228 :
10229 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10230 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10231 : the resulting offset is smaller in absolute value than the
10232 : original one and has the same sign. */
10233 180745957 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10234 139850896 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10235 34490565 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10236 2341358 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10237 1930506 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10238 1930506 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10239 161863005 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10240 161933 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10241 : {
10242 161933 : tree const1 = TREE_OPERAND (arg0, 1);
10243 161933 : tree const2 = TREE_OPERAND (arg1, 1);
10244 161933 : tree variable1 = TREE_OPERAND (arg0, 0);
10245 161933 : tree variable2 = TREE_OPERAND (arg1, 0);
10246 161933 : tree cst;
10247 :
10248 : /* Put the constant on the side where it doesn't overflow and is
10249 : of lower absolute value and of same sign than before. */
10250 161934 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10251 : ? MINUS_EXPR : PLUS_EXPR,
10252 : const2, const1);
10253 161933 : if (!TREE_OVERFLOW (cst)
10254 161909 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10255 184875 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10256 5926 : return fold_build2_loc (loc, code, type,
10257 : variable1,
10258 5926 : fold_build2_loc (loc, TREE_CODE (arg1),
10259 5926 : TREE_TYPE (arg1),
10260 5926 : variable2, cst));
10261 :
10262 156008 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10263 : ? MINUS_EXPR : PLUS_EXPR,
10264 : const1, const2);
10265 156007 : if (!TREE_OVERFLOW (cst)
10266 155983 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10267 173023 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10268 17016 : return fold_build2_loc (loc, code, type,
10269 17016 : fold_build2_loc (loc, TREE_CODE (arg0),
10270 17016 : TREE_TYPE (arg0),
10271 : variable1, cst),
10272 17016 : variable2);
10273 : }
10274 :
10275 91750262 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10276 91750262 : if (tem)
10277 : return tem;
10278 :
10279 : /* If we are comparing an expression that just has comparisons
10280 : of two integer values, arithmetic expressions of those comparisons,
10281 : and constants, we can simplify it. There are only three cases
10282 : to check: the two values can either be equal, the first can be
10283 : greater, or the second can be greater. Fold the expression for
10284 : those three values. Since each value must be 0 or 1, we have
10285 : eight possibilities, each of which corresponds to the constant 0
10286 : or 1 or one of the six possible comparisons.
10287 :
10288 : This handles common cases like (a > b) == 0 but also handles
10289 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10290 : occur in macroized code. */
10291 :
10292 90942808 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10293 : {
10294 55511848 : tree cval1 = 0, cval2 = 0;
10295 :
10296 55511848 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10297 : /* Don't handle degenerate cases here; they should already
10298 : have been handled anyway. */
10299 621776 : && cval1 != 0 && cval2 != 0
10300 620484 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10301 620484 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10302 620478 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10303 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10304 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10305 55511902 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10306 54 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10307 : {
10308 54 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10309 54 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10310 :
10311 : /* We can't just pass T to eval_subst in case cval1 or cval2
10312 : was the same as ARG1. */
10313 :
10314 54 : tree high_result
10315 54 : = fold_build2_loc (loc, code, type,
10316 : eval_subst (loc, arg0, cval1, maxval,
10317 : cval2, minval),
10318 : arg1);
10319 54 : tree equal_result
10320 54 : = fold_build2_loc (loc, code, type,
10321 : eval_subst (loc, arg0, cval1, maxval,
10322 : cval2, maxval),
10323 : arg1);
10324 54 : tree low_result
10325 54 : = fold_build2_loc (loc, code, type,
10326 : eval_subst (loc, arg0, cval1, minval,
10327 : cval2, maxval),
10328 : arg1);
10329 :
10330 : /* All three of these results should be 0 or 1. Confirm they are.
10331 : Then use those values to select the proper code to use. */
10332 :
10333 54 : if (TREE_CODE (high_result) == INTEGER_CST
10334 54 : && TREE_CODE (equal_result) == INTEGER_CST
10335 44 : && TREE_CODE (low_result) == INTEGER_CST)
10336 : {
10337 : /* Make a 3-bit mask with the high-order bit being the
10338 : value for `>', the next for '=', and the low for '<'. */
10339 44 : switch ((integer_onep (high_result) * 4)
10340 44 : + (integer_onep (equal_result) * 2)
10341 44 : + integer_onep (low_result))
10342 : {
10343 21 : case 0:
10344 : /* Always false. */
10345 44 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10346 : case 1:
10347 : code = LT_EXPR;
10348 : break;
10349 2 : case 2:
10350 2 : code = EQ_EXPR;
10351 2 : break;
10352 0 : case 3:
10353 0 : code = LE_EXPR;
10354 0 : break;
10355 0 : case 4:
10356 0 : code = GT_EXPR;
10357 0 : break;
10358 5 : case 5:
10359 5 : code = NE_EXPR;
10360 5 : break;
10361 0 : case 6:
10362 0 : code = GE_EXPR;
10363 0 : break;
10364 16 : case 7:
10365 : /* Always true. */
10366 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10367 : }
10368 :
10369 7 : return fold_build2_loc (loc, code, type, cval1, cval2);
10370 : }
10371 : }
10372 : }
10373 :
10374 : return NULL_TREE;
10375 : }
10376 :
10377 :
10378 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10379 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10380 : argument EXPR represents the expression "z" of type TYPE. */
10381 :
10382 : static tree
10383 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10384 : {
10385 2 : tree itype = TREE_TYPE (type);
10386 2 : tree rpart, ipart, tem;
10387 :
10388 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10389 : {
10390 0 : rpart = TREE_OPERAND (expr, 0);
10391 0 : ipart = TREE_OPERAND (expr, 1);
10392 : }
10393 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10394 : {
10395 0 : rpart = TREE_REALPART (expr);
10396 0 : ipart = TREE_IMAGPART (expr);
10397 : }
10398 : else
10399 : {
10400 2 : expr = save_expr (expr);
10401 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10402 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10403 : }
10404 :
10405 2 : rpart = save_expr (rpart);
10406 2 : ipart = save_expr (ipart);
10407 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10408 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10409 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10410 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10411 2 : build_zero_cst (itype));
10412 : }
10413 :
10414 :
10415 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10416 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10417 : true if successful. */
10418 :
10419 : static bool
10420 31669 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10421 : {
10422 31669 : unsigned HOST_WIDE_INT i, nunits;
10423 :
10424 31669 : if (TREE_CODE (arg) == VECTOR_CST
10425 31669 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10426 : {
10427 2160 : for (i = 0; i < nunits; ++i)
10428 1702 : elts[i] = VECTOR_CST_ELT (arg, i);
10429 : }
10430 31211 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10431 : {
10432 : constructor_elt *elt;
10433 :
10434 56788 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10435 46220 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10436 31669 : return false;
10437 : else
10438 20184 : elts[i] = elt->value;
10439 : }
10440 : else
10441 : return false;
10442 6407 : for (; i < nelts; i++)
10443 1548 : elts[i]
10444 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10445 : return true;
10446 : }
10447 :
10448 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10449 : mask for VLA vec_perm folding.
10450 : REASON if specified, will contain the reason why SEL is not suitable.
10451 : Used only for debugging and unit-testing. */
10452 :
10453 : static bool
10454 30702 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10455 : const vec_perm_indices &sel,
10456 : const char **reason = NULL)
10457 : {
10458 30702 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10459 30702 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10460 :
10461 61404 : if (!(pow2p_hwi (sel_npatterns)
10462 30702 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10463 30702 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10464 : {
10465 0 : if (reason)
10466 0 : *reason = "npatterns is not power of 2";
10467 : return false;
10468 : }
10469 :
10470 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10471 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10472 30702 : poly_uint64 esel;
10473 30702 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10474 : {
10475 0 : if (reason)
10476 0 : *reason = "sel.length is not multiple of sel_npatterns";
10477 : return false;
10478 : }
10479 :
10480 30702 : if (sel_nelts_per_pattern < 3)
10481 : return true;
10482 :
10483 6224 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10484 : {
10485 4649 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10486 4649 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10487 4649 : HOST_WIDE_INT step;
10488 4649 : if (!poly_int64 (a2 - a1).is_constant (&step))
10489 : {
10490 : if (reason)
10491 : *reason = "step is not constant";
10492 30702 : return false;
10493 : }
10494 : // FIXME: Punt on step < 0 for now, revisit later.
10495 4649 : if (step < 0)
10496 : return false;
10497 4585 : if (step == 0)
10498 0 : continue;
10499 :
10500 4585 : if (!pow2p_hwi (step))
10501 : {
10502 0 : if (reason)
10503 0 : *reason = "step is not power of 2";
10504 : return false;
10505 : }
10506 :
10507 : /* Ensure that stepped sequence of the pattern selects elements
10508 : only from the same input vector. */
10509 4585 : uint64_t q1, qe;
10510 4585 : poly_uint64 r1, re;
10511 4585 : poly_uint64 ae = a1 + (esel - 2) * step;
10512 4585 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10513 :
10514 4585 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10515 4585 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10516 : && q1 == qe))
10517 : {
10518 435 : if (reason)
10519 0 : *reason = "crossed input vectors";
10520 : return false;
10521 : }
10522 :
10523 : /* Ensure that the stepped sequence always selects from the same
10524 : input pattern. */
10525 4150 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10526 4150 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10527 :
10528 4150 : if (!multiple_p (step, arg_npatterns))
10529 : {
10530 628 : if (reason)
10531 0 : *reason = "step is not multiple of npatterns";
10532 : return false;
10533 : }
10534 :
10535 : /* If a1 chooses base element from arg, ensure that it's a natural
10536 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10537 : to preserve arg's encoding. */
10538 :
10539 3522 : if (maybe_lt (r1, arg_npatterns))
10540 : {
10541 44 : unsigned HOST_WIDE_INT index;
10542 44 : if (!r1.is_constant (&index))
10543 30702 : return false;
10544 :
10545 44 : tree arg_elem0 = vector_cst_elt (arg, index);
10546 44 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10547 44 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10548 :
10549 44 : tree step1, step2;
10550 44 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10551 44 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10552 88 : || !operand_equal_p (step1, step2, 0))
10553 : {
10554 2 : if (reason)
10555 0 : *reason = "not a natural stepped sequence";
10556 : return false;
10557 : }
10558 : }
10559 : }
10560 :
10561 : return true;
10562 : }
10563 :
10564 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10565 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10566 : REASON has same purpose as described in
10567 : valid_mask_for_fold_vec_perm_cst_p. */
10568 :
10569 : static tree
10570 30702 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10571 : const char **reason = NULL)
10572 : {
10573 30702 : unsigned res_npatterns, res_nelts_per_pattern;
10574 30702 : unsigned HOST_WIDE_INT res_nelts;
10575 :
10576 : /* First try to implement the fold in a VLA-friendly way.
10577 :
10578 : (1) If the selector is simply a duplication of N elements, the
10579 : result is likewise a duplication of N elements.
10580 :
10581 : (2) If the selector is N elements followed by a duplication
10582 : of N elements, the result is too.
10583 :
10584 : (3) If the selector is N elements followed by an interleaving
10585 : of N linear series, the situation is more complex.
10586 :
10587 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10588 : can handle this case. If we can, then each of the N linear
10589 : series either (a) selects the same element each time or
10590 : (b) selects a linear series from one of the input patterns.
10591 :
10592 : If (b) holds for one of the linear series, the result
10593 : will contain a linear series, and so the result will have
10594 : the same shape as the selector. If (a) holds for all of
10595 : the linear series, the result will be the same as (2) above.
10596 :
10597 : (b) can only hold if one of the input patterns has a
10598 : stepped encoding. */
10599 :
10600 30702 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10601 : {
10602 29573 : res_npatterns = sel.encoding ().npatterns ();
10603 29573 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10604 29573 : if (res_nelts_per_pattern == 3
10605 1575 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10606 30603 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10607 : res_nelts_per_pattern = 2;
10608 29573 : res_nelts = res_npatterns * res_nelts_per_pattern;
10609 : }
10610 1129 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10611 : {
10612 1129 : res_npatterns = res_nelts;
10613 1129 : res_nelts_per_pattern = 1;
10614 : }
10615 : else
10616 : return NULL_TREE;
10617 :
10618 30702 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10619 184812 : for (unsigned i = 0; i < res_nelts; i++)
10620 : {
10621 123408 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10622 123408 : uint64_t q;
10623 123408 : poly_uint64 r;
10624 123408 : unsigned HOST_WIDE_INT index;
10625 :
10626 : /* Punt if sel[i] /trunc_div len cannot be determined,
10627 : because the input vector to be chosen will depend on
10628 : runtime vector length.
10629 : For example if len == 4 + 4x, and sel[i] == 4,
10630 : If len at runtime equals 4, we choose arg1[0].
10631 : For any other value of len > 4 at runtime, we choose arg0[4].
10632 : which makes the element choice dependent on runtime vector length. */
10633 123408 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10634 : {
10635 : if (reason)
10636 : *reason = "cannot divide selector element by arg len";
10637 : return NULL_TREE;
10638 : }
10639 :
10640 : /* sel[i] % len will give the index of element in the chosen input
10641 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10642 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10643 123408 : if (!r.is_constant (&index))
10644 : {
10645 : if (reason)
10646 : *reason = "remainder is not constant";
10647 : return NULL_TREE;
10648 : }
10649 :
10650 123408 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10651 123408 : tree elem = vector_cst_elt (arg, index);
10652 123408 : out_elts.quick_push (elem);
10653 : }
10654 :
10655 30702 : return out_elts.build ();
10656 30702 : }
10657 :
10658 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10659 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10660 : NULL_TREE otherwise. */
10661 :
10662 : tree
10663 70117 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10664 : {
10665 70117 : unsigned int i;
10666 70117 : unsigned HOST_WIDE_INT nelts;
10667 :
10668 70117 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10669 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10670 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10671 :
10672 70117 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10673 70117 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10674 : return NULL_TREE;
10675 :
10676 59538 : if (TREE_CODE (arg0) == VECTOR_CST
10677 30976 : && TREE_CODE (arg1) == VECTOR_CST)
10678 30702 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10679 :
10680 : /* For fall back case, we want to ensure we have VLS vectors
10681 : with equal length. */
10682 28836 : if (!sel.length ().is_constant (&nelts)
10683 28836 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10684 0 : return NULL_TREE;
10685 :
10686 28836 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10687 28836 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10688 28836 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10689 : return NULL_TREE;
10690 :
10691 2800 : vec<constructor_elt, va_gc> *v;
10692 2800 : vec_alloc (v, nelts);
10693 16798 : for (i = 0; i < nelts; i++)
10694 : {
10695 11198 : HOST_WIDE_INT index;
10696 11198 : if (!sel[i].is_constant (&index))
10697 : return NULL_TREE;
10698 11198 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10699 : }
10700 2800 : return build_constructor (type, v);
10701 : }
10702 :
10703 : /* Try to fold a pointer difference of type TYPE two address expressions of
10704 : array references AREF0 and AREF1 using location LOC. Return a
10705 : simplified expression for the difference or NULL_TREE. */
10706 :
10707 : static tree
10708 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10709 : tree aref0, tree aref1,
10710 : bool use_pointer_diff)
10711 : {
10712 39 : tree base0 = TREE_OPERAND (aref0, 0);
10713 39 : tree base1 = TREE_OPERAND (aref1, 0);
10714 39 : tree base_offset = build_int_cst (type, 0);
10715 :
10716 : /* If the bases are array references as well, recurse. If the bases
10717 : are pointer indirections compute the difference of the pointers.
10718 : If the bases are equal, we are set. */
10719 39 : if ((TREE_CODE (base0) == ARRAY_REF
10720 1 : && TREE_CODE (base1) == ARRAY_REF
10721 1 : && (base_offset
10722 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10723 : use_pointer_diff)))
10724 38 : || (INDIRECT_REF_P (base0)
10725 7 : && INDIRECT_REF_P (base1)
10726 7 : && (base_offset
10727 : = use_pointer_diff
10728 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10729 1 : TREE_OPERAND (base0, 0),
10730 1 : TREE_OPERAND (base1, 0))
10731 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10732 6 : fold_convert (type,
10733 : TREE_OPERAND (base0, 0)),
10734 6 : fold_convert (type,
10735 : TREE_OPERAND (base1, 0)))))
10736 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10737 : {
10738 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10739 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10740 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10741 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10742 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10743 : base_offset,
10744 : fold_build2_loc (loc, MULT_EXPR, type,
10745 15 : diff, esz));
10746 : }
10747 : return NULL_TREE;
10748 : }
10749 :
10750 : /* If the real or vector real constant CST of type TYPE has an exact
10751 : inverse, return it, else return NULL. */
10752 :
10753 : tree
10754 1189189 : exact_inverse (tree type, tree cst)
10755 : {
10756 1189189 : REAL_VALUE_TYPE r;
10757 1189189 : tree unit_type;
10758 1189189 : machine_mode mode;
10759 :
10760 1189189 : switch (TREE_CODE (cst))
10761 : {
10762 1188548 : case REAL_CST:
10763 1188548 : r = TREE_REAL_CST (cst);
10764 :
10765 1188548 : if (exact_real_inverse (TYPE_MODE (type), &r))
10766 334791 : return build_real (type, r);
10767 :
10768 : return NULL_TREE;
10769 :
10770 641 : case VECTOR_CST:
10771 641 : {
10772 641 : unit_type = TREE_TYPE (type);
10773 641 : mode = TYPE_MODE (unit_type);
10774 :
10775 641 : tree_vector_builder elts;
10776 641 : if (!elts.new_unary_operation (type, cst, false))
10777 : return NULL_TREE;
10778 641 : unsigned int count = elts.encoded_nelts ();
10779 701 : for (unsigned int i = 0; i < count; ++i)
10780 : {
10781 641 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10782 641 : if (!exact_real_inverse (mode, &r))
10783 : return NULL_TREE;
10784 60 : elts.quick_push (build_real (unit_type, r));
10785 : }
10786 :
10787 60 : return elts.build ();
10788 641 : }
10789 :
10790 : default:
10791 : return NULL_TREE;
10792 : }
10793 : }
10794 :
10795 : /* Mask out the tz least significant bits of X of type TYPE where
10796 : tz is the number of trailing zeroes in Y. */
10797 : static wide_int
10798 172374 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10799 : {
10800 172374 : int tz = wi::ctz (y);
10801 172374 : if (tz > 0)
10802 6967 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10803 165407 : return x;
10804 : }
10805 :
10806 : /* Return true when T is an address and is known to be nonzero.
10807 : For floating point we further ensure that T is not denormal.
10808 : Similar logic is present in nonzero_address in rtlanal.h. */
10809 :
10810 : bool
10811 152450434 : tree_expr_nonzero_p (tree t)
10812 : {
10813 152808092 : tree type = TREE_TYPE (t);
10814 152808092 : enum tree_code code;
10815 :
10816 : /* Doing something useful for floating point would need more work. */
10817 152808092 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10818 : return false;
10819 :
10820 152683638 : code = TREE_CODE (t);
10821 152683638 : switch (TREE_CODE_CLASS (code))
10822 : {
10823 987831 : case tcc_unary:
10824 987831 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10825 2937801 : case tcc_binary:
10826 2937801 : case tcc_comparison:
10827 2937801 : return tree_binary_nonzero_p (code, type,
10828 2937801 : TREE_OPERAND (t, 0),
10829 5875602 : TREE_OPERAND (t, 1));
10830 13380666 : case tcc_constant:
10831 13380666 : case tcc_declaration:
10832 13380666 : case tcc_reference:
10833 13380666 : return tree_single_nonzero_p (t);
10834 :
10835 135377340 : default:
10836 135377340 : break;
10837 : }
10838 :
10839 135377340 : switch (code)
10840 : {
10841 620058 : case TRUTH_NOT_EXPR:
10842 620058 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10843 :
10844 71618 : case TRUTH_AND_EXPR:
10845 71618 : case TRUTH_OR_EXPR:
10846 71618 : case TRUTH_XOR_EXPR:
10847 71618 : return tree_binary_nonzero_p (code, type,
10848 71618 : TREE_OPERAND (t, 0),
10849 143236 : TREE_OPERAND (t, 1));
10850 :
10851 131151707 : case COND_EXPR:
10852 131151707 : case CONSTRUCTOR:
10853 131151707 : case OBJ_TYPE_REF:
10854 131151707 : case ADDR_EXPR:
10855 131151707 : case WITH_SIZE_EXPR:
10856 131151707 : case SSA_NAME:
10857 131151707 : return tree_single_nonzero_p (t);
10858 :
10859 85157 : case COMPOUND_EXPR:
10860 85157 : case MODIFY_EXPR:
10861 85157 : case BIND_EXPR:
10862 85157 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10863 :
10864 272501 : case SAVE_EXPR:
10865 272501 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10866 :
10867 3119760 : case CALL_EXPR:
10868 3119760 : {
10869 3119760 : tree fndecl = get_callee_fndecl (t);
10870 3119760 : if (!fndecl) return false;
10871 3117778 : if (flag_delete_null_pointer_checks && !flag_check_new
10872 3117778 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10873 3118496 : && !TREE_NOTHROW (fndecl))
10874 : return true;
10875 3118496 : if (flag_delete_null_pointer_checks
10876 6236274 : && lookup_attribute ("returns_nonnull",
10877 3117778 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10878 : return true;
10879 3118488 : return alloca_call_p (t);
10880 : }
10881 :
10882 : default:
10883 : break;
10884 : }
10885 : return false;
10886 : }
10887 :
10888 : /* Return true if T is known not to be equal to an integer W.
10889 : If STMT is specified, the check is if T on STMT is not equal
10890 : to W. */
10891 :
10892 : bool
10893 104656764 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10894 : {
10895 104656764 : int_range_max vr;
10896 104656764 : switch (TREE_CODE (t))
10897 : {
10898 2455272 : case INTEGER_CST:
10899 2455272 : return wi::to_wide (t) != w;
10900 :
10901 101758371 : case SSA_NAME:
10902 101758371 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10903 : return false;
10904 :
10905 203516742 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10906 101758371 : if (!vr.undefined_p () && !vr.contains_p (w))
10907 : return true;
10908 : /* If T has some known zero bits and W has any of those bits set,
10909 : then T is known not to be equal to W. */
10910 101614757 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10911 203229082 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10912 1 : return true;
10913 : return false;
10914 :
10915 : default:
10916 : return false;
10917 : }
10918 104656764 : }
10919 :
10920 : /* Fold a binary expression of code CODE and type TYPE with operands
10921 : OP0 and OP1. LOC is the location of the resulting expression.
10922 : Return the folded expression if folding is successful. Otherwise,
10923 : return NULL_TREE. */
10924 :
10925 : tree
10926 977828492 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10927 : tree op0, tree op1)
10928 : {
10929 977828492 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10930 977828492 : tree arg0, arg1, tem;
10931 977828492 : tree t1 = NULL_TREE;
10932 977828492 : unsigned int prec;
10933 :
10934 977828492 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10935 : && TREE_CODE_LENGTH (code) == 2
10936 : && op0 != NULL_TREE
10937 : && op1 != NULL_TREE);
10938 :
10939 977828492 : arg0 = op0;
10940 977828492 : arg1 = op1;
10941 :
10942 : /* Strip any conversions that don't change the mode. This is
10943 : safe for every expression, except for a comparison expression
10944 : because its signedness is derived from its operands. So, in
10945 : the latter case, only strip conversions that don't change the
10946 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10947 : preserved.
10948 :
10949 : Note that this is done as an internal manipulation within the
10950 : constant folder, in order to find the simplest representation
10951 : of the arguments so that their form can be studied. In any
10952 : cases, the appropriate type conversions should be put back in
10953 : the tree that will get out of the constant folder. */
10954 :
10955 977828492 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10956 : {
10957 215748597 : STRIP_SIGN_NOPS (arg0);
10958 215748597 : STRIP_SIGN_NOPS (arg1);
10959 : }
10960 : else
10961 : {
10962 762079895 : STRIP_NOPS (arg0);
10963 762079895 : STRIP_NOPS (arg1);
10964 : }
10965 :
10966 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10967 : constant but we can't do arithmetic on them. */
10968 977828492 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10969 : {
10970 274225241 : tem = const_binop (code, type, arg0, arg1);
10971 274225241 : if (tem != NULL_TREE)
10972 : {
10973 271675839 : if (TREE_TYPE (tem) != type)
10974 4203268 : tem = fold_convert_loc (loc, type, tem);
10975 : return tem;
10976 : }
10977 : }
10978 :
10979 : /* If this is a commutative operation, and ARG0 is a constant, move it
10980 : to ARG1 to reduce the number of tests below. */
10981 706152653 : if (commutative_tree_code (code)
10982 706152653 : && tree_swap_operands_p (arg0, arg1))
10983 33473889 : return fold_build2_loc (loc, code, type, op1, op0);
10984 :
10985 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10986 : to ARG1 to reduce the number of tests below. */
10987 672678764 : if (kind == tcc_comparison
10988 672678764 : && tree_swap_operands_p (arg0, arg1))
10989 8426569 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10990 :
10991 664252195 : tem = generic_simplify (loc, code, type, op0, op1);
10992 664252195 : if (tem)
10993 : return tem;
10994 :
10995 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10996 :
10997 : First check for cases where an arithmetic operation is applied to a
10998 : compound, conditional, or comparison operation. Push the arithmetic
10999 : operation inside the compound or conditional to see if any folding
11000 : can then be done. Convert comparison to conditional for this purpose.
11001 : The also optimizes non-constant cases that used to be done in
11002 : expand_expr.
11003 :
11004 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11005 : one of the operands is a comparison and the other is a comparison, a
11006 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11007 : code below would make the expression more complex. Change it to a
11008 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11009 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11010 :
11011 561557870 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11012 : || code == EQ_EXPR || code == NE_EXPR)
11013 59571301 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11014 58983075 : && ((truth_value_p (TREE_CODE (arg0))
11015 1257839 : && (truth_value_p (TREE_CODE (arg1))
11016 935114 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11017 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11018 58660334 : || (truth_value_p (TREE_CODE (arg1))
11019 7190 : && (truth_value_p (TREE_CODE (arg0))
11020 7190 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11021 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11022 : {
11023 403546 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11024 80791 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11025 : : TRUTH_XOR_EXPR,
11026 : boolean_type_node,
11027 : fold_convert_loc (loc, boolean_type_node, arg0),
11028 : fold_convert_loc (loc, boolean_type_node, arg1));
11029 :
11030 322755 : if (code == EQ_EXPR)
11031 73292 : tem = invert_truthvalue_loc (loc, tem);
11032 :
11033 322755 : return fold_convert_loc (loc, type, tem);
11034 : }
11035 :
11036 561235115 : if (TREE_CODE_CLASS (code) == tcc_binary
11037 308010797 : || TREE_CODE_CLASS (code) == tcc_comparison)
11038 : {
11039 351517391 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11040 : {
11041 82935 : tem = fold_build2_loc (loc, code, type,
11042 82935 : fold_convert_loc (loc, TREE_TYPE (op0),
11043 82935 : TREE_OPERAND (arg0, 1)), op1);
11044 82935 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11045 82935 : tem);
11046 : }
11047 351434456 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11048 : {
11049 3161 : tem = fold_build2_loc (loc, code, type, op0,
11050 3161 : fold_convert_loc (loc, TREE_TYPE (op1),
11051 3161 : TREE_OPERAND (arg1, 1)));
11052 3161 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11053 3161 : tem);
11054 : }
11055 :
11056 351431295 : if (TREE_CODE (arg0) == COND_EXPR
11057 351035827 : || TREE_CODE (arg0) == VEC_COND_EXPR
11058 351032497 : || COMPARISON_CLASS_P (arg0))
11059 : {
11060 742806 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11061 : arg0, arg1,
11062 : /*cond_first_p=*/1);
11063 742806 : if (tem != NULL_TREE)
11064 : return tem;
11065 : }
11066 :
11067 350940097 : if (TREE_CODE (arg1) == COND_EXPR
11068 350721982 : || TREE_CODE (arg1) == VEC_COND_EXPR
11069 350721512 : || COMPARISON_CLASS_P (arg1))
11070 : {
11071 231530 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11072 : arg1, arg0,
11073 : /*cond_first_p=*/0);
11074 231530 : if (tem != NULL_TREE)
11075 : return tem;
11076 : }
11077 : }
11078 :
11079 560649591 : switch (code)
11080 : {
11081 65604252 : case MEM_REF:
11082 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11083 65604252 : if (TREE_CODE (arg0) == ADDR_EXPR
11084 65604252 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11085 : {
11086 824937 : tree iref = TREE_OPERAND (arg0, 0);
11087 824937 : return fold_build2 (MEM_REF, type,
11088 : TREE_OPERAND (iref, 0),
11089 : int_const_binop (PLUS_EXPR, arg1,
11090 : TREE_OPERAND (iref, 1)));
11091 : }
11092 :
11093 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11094 64779315 : if (TREE_CODE (arg0) == ADDR_EXPR
11095 64779315 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11096 : {
11097 6264144 : tree base;
11098 6264144 : poly_int64 coffset;
11099 6264144 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11100 : &coffset);
11101 6264144 : if (!base)
11102 : return NULL_TREE;
11103 6259999 : return fold_build2 (MEM_REF, type,
11104 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11105 : int_const_binop (PLUS_EXPR, arg1,
11106 : size_int (coffset)));
11107 : }
11108 :
11109 : return NULL_TREE;
11110 :
11111 74945850 : case POINTER_PLUS_EXPR:
11112 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11113 149891284 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11114 149882405 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11115 32819 : return fold_convert_loc (loc, type,
11116 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11117 : fold_convert_loc (loc, sizetype,
11118 : arg1),
11119 : fold_convert_loc (loc, sizetype,
11120 32819 : arg0)));
11121 :
11122 : return NULL_TREE;
11123 :
11124 63682605 : case PLUS_EXPR:
11125 63682605 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11126 : {
11127 : /* X + (X / CST) * -CST is X % CST. */
11128 52063596 : if (TREE_CODE (arg1) == MULT_EXPR
11129 2361503 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11130 52069875 : && operand_equal_p (arg0,
11131 6279 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11132 : {
11133 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11134 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11135 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11136 : cst1, cst0);
11137 204 : if (sum && integer_zerop (sum))
11138 204 : return fold_convert_loc (loc, type,
11139 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11140 204 : TREE_TYPE (arg0), arg0,
11141 204 : cst0));
11142 : }
11143 : }
11144 :
11145 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11146 : one. Make sure the type is not saturating and has the signedness of
11147 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11148 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11149 63682401 : if ((TREE_CODE (arg0) == MULT_EXPR
11150 51764048 : || TREE_CODE (arg1) == MULT_EXPR)
11151 13259705 : && !TYPE_SATURATING (type)
11152 13259705 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11153 12862544 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11154 75852471 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11155 : {
11156 8855228 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11157 8855228 : if (tem)
11158 : return tem;
11159 : }
11160 :
11161 62390195 : if (! FLOAT_TYPE_P (type))
11162 : {
11163 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11164 : (plus (plus (mult) (mult)) (foo)) so that we can
11165 : take advantage of the factoring cases below. */
11166 283663 : if (ANY_INTEGRAL_TYPE_P (type)
11167 50773550 : && TYPE_OVERFLOW_WRAPS (type)
11168 50773550 : && (((TREE_CODE (arg0) == PLUS_EXPR
11169 31978973 : || TREE_CODE (arg0) == MINUS_EXPR)
11170 3459876 : && TREE_CODE (arg1) == MULT_EXPR)
11171 31471502 : || ((TREE_CODE (arg1) == PLUS_EXPR
11172 31471502 : || TREE_CODE (arg1) == MINUS_EXPR)
11173 432003 : && TREE_CODE (arg0) == MULT_EXPR)))
11174 : {
11175 554985 : tree parg0, parg1, parg, marg;
11176 554985 : enum tree_code pcode;
11177 :
11178 554985 : if (TREE_CODE (arg1) == MULT_EXPR)
11179 : parg = arg0, marg = arg1;
11180 : else
11181 47514 : parg = arg1, marg = arg0;
11182 554985 : pcode = TREE_CODE (parg);
11183 554985 : parg0 = TREE_OPERAND (parg, 0);
11184 554985 : parg1 = TREE_OPERAND (parg, 1);
11185 554985 : STRIP_NOPS (parg0);
11186 554985 : STRIP_NOPS (parg1);
11187 :
11188 554985 : if (TREE_CODE (parg0) == MULT_EXPR
11189 273626 : && TREE_CODE (parg1) != MULT_EXPR)
11190 234436 : return fold_build2_loc (loc, pcode, type,
11191 : fold_build2_loc (loc, PLUS_EXPR, type,
11192 : fold_convert_loc (loc, type,
11193 : parg0),
11194 : fold_convert_loc (loc, type,
11195 : marg)),
11196 234436 : fold_convert_loc (loc, type, parg1));
11197 320549 : if (TREE_CODE (parg0) != MULT_EXPR
11198 281359 : && TREE_CODE (parg1) == MULT_EXPR)
11199 99588 : return
11200 99588 : fold_build2_loc (loc, PLUS_EXPR, type,
11201 : fold_convert_loc (loc, type, parg0),
11202 : fold_build2_loc (loc, pcode, type,
11203 : fold_convert_loc (loc, type, marg),
11204 : fold_convert_loc (loc, type,
11205 99588 : parg1)));
11206 : }
11207 : }
11208 : else
11209 : {
11210 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11211 : to __complex__ ( x, y ). This is not the same for SNaNs or
11212 : if signed zeros are involved. */
11213 11616645 : if (!HONOR_SNANS (arg0)
11214 11614985 : && !HONOR_SIGNED_ZEROS (arg0)
11215 11638208 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11216 : {
11217 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11218 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11219 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11220 3086 : bool arg0rz = false, arg0iz = false;
11221 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11222 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11223 : {
11224 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11225 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11226 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11227 : {
11228 22 : tree rp = arg1r ? arg1r
11229 0 : : build1 (REALPART_EXPR, rtype, arg1);
11230 22 : tree ip = arg0i ? arg0i
11231 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11232 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11233 : }
11234 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11235 : {
11236 53 : tree rp = arg0r ? arg0r
11237 0 : : build1 (REALPART_EXPR, rtype, arg0);
11238 53 : tree ip = arg1i ? arg1i
11239 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11240 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11241 : }
11242 : }
11243 : }
11244 :
11245 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11246 : We associate floats only if the user has specified
11247 : -fassociative-math. */
11248 11616570 : if (flag_associative_math
11249 21431 : && TREE_CODE (arg1) == PLUS_EXPR
11250 38 : && TREE_CODE (arg0) != MULT_EXPR)
11251 : {
11252 23 : tree tree10 = TREE_OPERAND (arg1, 0);
11253 23 : tree tree11 = TREE_OPERAND (arg1, 1);
11254 23 : if (TREE_CODE (tree11) == MULT_EXPR
11255 5 : && TREE_CODE (tree10) == MULT_EXPR)
11256 : {
11257 1 : tree tree0;
11258 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11259 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11260 : }
11261 : }
11262 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11263 : We associate floats only if the user has specified
11264 : -fassociative-math. */
11265 11616569 : if (flag_associative_math
11266 21430 : && TREE_CODE (arg0) == PLUS_EXPR
11267 1292 : && TREE_CODE (arg1) != MULT_EXPR)
11268 : {
11269 864 : tree tree00 = TREE_OPERAND (arg0, 0);
11270 864 : tree tree01 = TREE_OPERAND (arg0, 1);
11271 864 : if (TREE_CODE (tree01) == MULT_EXPR
11272 49 : && TREE_CODE (tree00) == MULT_EXPR)
11273 : {
11274 9 : tree tree0;
11275 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11276 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11277 : }
11278 : }
11279 : }
11280 :
11281 11615705 : bit_rotate:
11282 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11283 : is a rotate of A by C1 bits. */
11284 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11285 : is a rotate of A by B bits.
11286 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11287 : though in this case CODE must be | and not + or ^, otherwise
11288 : it doesn't return A when B is 0. */
11289 64976788 : {
11290 64976788 : enum tree_code code0, code1;
11291 64976788 : tree rtype;
11292 64976788 : code0 = TREE_CODE (arg0);
11293 64976788 : code1 = TREE_CODE (arg1);
11294 75257 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11295 64960400 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11296 39781 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11297 39781 : TREE_OPERAND (arg1, 0), 0)
11298 37012 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11299 37012 : TYPE_UNSIGNED (rtype))
11300 : /* Only create rotates in complete modes. Other cases are not
11301 : expanded properly. */
11302 65003702 : && (element_precision (rtype)
11303 53828 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11304 : {
11305 26841 : tree tree01, tree11;
11306 26841 : tree orig_tree01, orig_tree11;
11307 26841 : enum tree_code code01, code11;
11308 :
11309 26841 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11310 26841 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11311 26841 : STRIP_NOPS (tree01);
11312 26841 : STRIP_NOPS (tree11);
11313 26841 : code01 = TREE_CODE (tree01);
11314 26841 : code11 = TREE_CODE (tree11);
11315 26841 : if (code11 != MINUS_EXPR
11316 26153 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11317 : {
11318 1462 : std::swap (code0, code1);
11319 1462 : std::swap (code01, code11);
11320 1462 : std::swap (tree01, tree11);
11321 1462 : std::swap (orig_tree01, orig_tree11);
11322 : }
11323 53682 : if (code01 == INTEGER_CST
11324 3152 : && code11 == INTEGER_CST
11325 29992 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11326 29992 : == element_precision (rtype)))
11327 : {
11328 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11329 3011 : rtype, TREE_OPERAND (arg0, 0),
11330 : code0 == LSHIFT_EXPR
11331 : ? orig_tree01 : orig_tree11);
11332 3011 : return fold_convert_loc (loc, type, tem);
11333 : }
11334 23830 : else if (code11 == MINUS_EXPR)
11335 : {
11336 943 : tree tree110, tree111;
11337 943 : tree110 = TREE_OPERAND (tree11, 0);
11338 943 : tree111 = TREE_OPERAND (tree11, 1);
11339 943 : STRIP_NOPS (tree110);
11340 943 : STRIP_NOPS (tree111);
11341 943 : if (TREE_CODE (tree110) == INTEGER_CST
11342 932 : && compare_tree_int (tree110,
11343 932 : element_precision (rtype)) == 0
11344 1859 : && operand_equal_p (tree01, tree111, 0))
11345 : {
11346 780 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11347 : ? LROTATE_EXPR : RROTATE_EXPR),
11348 560 : rtype, TREE_OPERAND (arg0, 0),
11349 : orig_tree01);
11350 560 : return fold_convert_loc (loc, type, tem);
11351 : }
11352 : }
11353 22887 : else if (code == BIT_IOR_EXPR
11354 21773 : && code11 == BIT_AND_EXPR
11355 44585 : && pow2p_hwi (element_precision (rtype)))
11356 : {
11357 21698 : tree tree110, tree111;
11358 21698 : tree110 = TREE_OPERAND (tree11, 0);
11359 21698 : tree111 = TREE_OPERAND (tree11, 1);
11360 21698 : STRIP_NOPS (tree110);
11361 21698 : STRIP_NOPS (tree111);
11362 21698 : if (TREE_CODE (tree110) == NEGATE_EXPR
11363 21243 : && TREE_CODE (tree111) == INTEGER_CST
11364 21243 : && compare_tree_int (tree111,
11365 21243 : element_precision (rtype) - 1) == 0
11366 42927 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11367 : {
11368 31715 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11369 : ? LROTATE_EXPR : RROTATE_EXPR),
11370 21167 : rtype, TREE_OPERAND (arg0, 0),
11371 : orig_tree01);
11372 21167 : return fold_convert_loc (loc, type, tem);
11373 : }
11374 : }
11375 : }
11376 : }
11377 :
11378 158552592 : associate:
11379 : /* In most languages, can't associate operations on floats through
11380 : parentheses. Rather than remember where the parentheses were, we
11381 : don't associate floats at all, unless the user has specified
11382 : -fassociative-math.
11383 : And, we need to make sure type is not saturating. */
11384 :
11385 158552592 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11386 117344237 : && !TYPE_SATURATING (type)
11387 275896829 : && !TYPE_OVERFLOW_SANITIZED (type))
11388 : {
11389 117311947 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11390 117311947 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11391 117311947 : tree atype = type;
11392 117311947 : bool ok = true;
11393 :
11394 : /* Split both trees into variables, constants, and literals. Then
11395 : associate each group together, the constants with literals,
11396 : then the result with variables. This increases the chances of
11397 : literals being recombined later and of generating relocatable
11398 : expressions for the sum of a constant and literal. */
11399 117311947 : var0 = split_tree (arg0, type, code,
11400 : &minus_var0, &con0, &minus_con0,
11401 : &lit0, &minus_lit0, 0);
11402 117311947 : var1 = split_tree (arg1, type, code,
11403 : &minus_var1, &con1, &minus_con1,
11404 : &lit1, &minus_lit1, code == MINUS_EXPR);
11405 :
11406 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11407 117311947 : if (code == MINUS_EXPR)
11408 12974681 : code = PLUS_EXPR;
11409 :
11410 : /* With undefined overflow prefer doing association in a type
11411 : which wraps on overflow, if that is one of the operand types. */
11412 117311716 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11413 233374377 : && !TYPE_OVERFLOW_WRAPS (type))
11414 : {
11415 62061205 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11416 61407306 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11417 851364 : atype = TREE_TYPE (arg0);
11418 60312106 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11419 60076134 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11420 235341 : atype = TREE_TYPE (arg1);
11421 31289915 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11422 : }
11423 :
11424 : /* With undefined overflow we can only associate constants with one
11425 : variable, and constants whose association doesn't overflow. */
11426 117311716 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11427 233374377 : && !TYPE_OVERFLOW_WRAPS (atype))
11428 : {
11429 30203210 : if ((var0 && var1) || (minus_var0 && minus_var1))
11430 : {
11431 : /* ??? If split_tree would handle NEGATE_EXPR we could
11432 : simply reject these cases and the allowed cases would
11433 : be the var0/minus_var1 ones. */
11434 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11435 5627930 : tree tmp1 = var1 ? var1 : minus_var1;
11436 5627930 : bool one_neg = false;
11437 :
11438 5627930 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11439 : {
11440 746 : tmp0 = TREE_OPERAND (tmp0, 0);
11441 746 : one_neg = !one_neg;
11442 : }
11443 5001035 : if (CONVERT_EXPR_P (tmp0)
11444 652910 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11445 6279885 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11446 651955 : <= TYPE_PRECISION (atype)))
11447 639002 : tmp0 = TREE_OPERAND (tmp0, 0);
11448 5627930 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11449 : {
11450 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11451 170 : one_neg = !one_neg;
11452 : }
11453 5300872 : if (CONVERT_EXPR_P (tmp1)
11454 396810 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11455 6024684 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11456 396754 : <= TYPE_PRECISION (atype)))
11457 379622 : tmp1 = TREE_OPERAND (tmp1, 0);
11458 : /* The only case we can still associate with two variables
11459 : is if they cancel out. */
11460 5627930 : if (!one_neg
11461 5627930 : || !operand_equal_p (tmp0, tmp1, 0))
11462 : ok = false;
11463 : }
11464 24192503 : else if ((var0 && minus_var1
11465 4103393 : && ! operand_equal_p (var0, minus_var1, 0))
11466 44664391 : || (minus_var0 && var1
11467 11588 : && ! operand_equal_p (minus_var0, var1, 0)))
11468 : ok = false;
11469 : }
11470 :
11471 : /* Only do something if we found more than two objects. Otherwise,
11472 : nothing has changed and we risk infinite recursion. */
11473 : if (ok
11474 107569117 : && ((var0 != 0) + (var1 != 0)
11475 107569117 : + (minus_var0 != 0) + (minus_var1 != 0)
11476 107569117 : + (con0 != 0) + (con1 != 0)
11477 107569117 : + (minus_con0 != 0) + (minus_con1 != 0)
11478 107569117 : + (lit0 != 0) + (lit1 != 0)
11479 107569117 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11480 : {
11481 2082737 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11482 4165474 : int minus_var0_origin
11483 2082737 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11484 2082737 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11485 4165474 : int minus_con0_origin
11486 2082737 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11487 2082737 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11488 4165474 : int minus_lit0_origin
11489 2082737 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11490 2082737 : var0 = associate_trees (loc, var0, var1, code, atype);
11491 2082737 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11492 : code, atype);
11493 2082737 : con0 = associate_trees (loc, con0, con1, code, atype);
11494 2082737 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11495 : code, atype);
11496 2082737 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11497 2082737 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11498 : code, atype);
11499 :
11500 2082737 : if (minus_var0 && var0)
11501 : {
11502 1363427 : var0_origin |= minus_var0_origin;
11503 1363427 : var0 = associate_trees (loc, var0, minus_var0,
11504 : MINUS_EXPR, atype);
11505 1363427 : minus_var0 = 0;
11506 1363427 : minus_var0_origin = 0;
11507 : }
11508 2082737 : if (minus_con0 && con0)
11509 : {
11510 2722 : con0_origin |= minus_con0_origin;
11511 2722 : con0 = associate_trees (loc, con0, minus_con0,
11512 : MINUS_EXPR, atype);
11513 2722 : minus_con0 = 0;
11514 2722 : minus_con0_origin = 0;
11515 : }
11516 :
11517 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11518 : greater than the positive part. Otherwise, the multiplicative
11519 : folding code (i.e extract_muldiv) may be fooled in case
11520 : unsigned constants are subtracted, like in the following
11521 : example: ((X*2 + 4) - 8U)/2. */
11522 2082737 : if (minus_lit0 && lit0)
11523 : {
11524 227737 : if (TREE_CODE (lit0) == INTEGER_CST
11525 227737 : && TREE_CODE (minus_lit0) == INTEGER_CST
11526 227737 : && tree_int_cst_lt (lit0, minus_lit0)
11527 : /* But avoid ending up with only negated parts. */
11528 286437 : && (var0 || con0))
11529 : {
11530 53527 : minus_lit0_origin |= lit0_origin;
11531 53527 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11532 : MINUS_EXPR, atype);
11533 53527 : lit0 = 0;
11534 53527 : lit0_origin = 0;
11535 : }
11536 : else
11537 : {
11538 174210 : lit0_origin |= minus_lit0_origin;
11539 174210 : lit0 = associate_trees (loc, lit0, minus_lit0,
11540 : MINUS_EXPR, atype);
11541 174210 : minus_lit0 = 0;
11542 174210 : minus_lit0_origin = 0;
11543 : }
11544 : }
11545 :
11546 : /* Don't introduce overflows through reassociation. */
11547 1370086 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11548 3452785 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11549 2082737 : return NULL_TREE;
11550 :
11551 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11552 2082699 : con0_origin |= lit0_origin;
11553 2082699 : con0 = associate_trees (loc, con0, lit0, code, atype);
11554 2082699 : minus_con0_origin |= minus_lit0_origin;
11555 2082699 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11556 : code, atype);
11557 :
11558 : /* Eliminate minus_con0. */
11559 2082699 : if (minus_con0)
11560 : {
11561 717826 : if (con0)
11562 : {
11563 15377 : con0_origin |= minus_con0_origin;
11564 15377 : con0 = associate_trees (loc, con0, minus_con0,
11565 : MINUS_EXPR, atype);
11566 : }
11567 702449 : else if (var0)
11568 : {
11569 702449 : var0_origin |= minus_con0_origin;
11570 702449 : var0 = associate_trees (loc, var0, minus_con0,
11571 : MINUS_EXPR, atype);
11572 : }
11573 : else
11574 0 : gcc_unreachable ();
11575 : }
11576 :
11577 : /* Eliminate minus_var0. */
11578 2082699 : if (minus_var0)
11579 : {
11580 366055 : if (con0)
11581 : {
11582 366055 : con0_origin |= minus_var0_origin;
11583 366055 : con0 = associate_trees (loc, con0, minus_var0,
11584 : MINUS_EXPR, atype);
11585 : }
11586 : else
11587 0 : gcc_unreachable ();
11588 : }
11589 :
11590 : /* Reassociate only if there has been any actual association
11591 : between subtrees from op0 and subtrees from op1 in at
11592 : least one of the operands, otherwise we risk infinite
11593 : recursion. See PR114084. */
11594 2082699 : if (var0_origin != 3 && con0_origin != 3)
11595 : return NULL_TREE;
11596 :
11597 2081140 : return
11598 2081140 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11599 2081140 : code, atype));
11600 : }
11601 : }
11602 :
11603 : return NULL_TREE;
11604 :
11605 23885288 : case POINTER_DIFF_EXPR:
11606 23885288 : case MINUS_EXPR:
11607 : /* Fold &a[i] - &a[j] to i-j. */
11608 23885288 : if (TREE_CODE (arg0) == ADDR_EXPR
11609 41897 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11610 6036 : && TREE_CODE (arg1) == ADDR_EXPR
11611 23885896 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11612 : {
11613 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11614 38 : TREE_OPERAND (arg0, 0),
11615 38 : TREE_OPERAND (arg1, 0),
11616 : code
11617 : == POINTER_DIFF_EXPR);
11618 38 : if (tem)
11619 : return tem;
11620 : }
11621 :
11622 : /* Further transformations are not for pointers. */
11623 23885274 : if (code == POINTER_DIFF_EXPR)
11624 : return NULL_TREE;
11625 :
11626 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11627 21084936 : if (TREE_CODE (arg0) == NEGATE_EXPR
11628 148252 : && negate_expr_p (op1)
11629 : /* If arg0 is e.g. unsigned int and type is int, then this could
11630 : introduce UB, because if A is INT_MIN at runtime, the original
11631 : expression can be well defined while the latter is not.
11632 : See PR83269. */
11633 21085783 : && !(ANY_INTEGRAL_TYPE_P (type)
11634 847 : && TYPE_OVERFLOW_UNDEFINED (type)
11635 835 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11636 835 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11637 840 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11638 : fold_convert_loc (loc, type,
11639 1680 : TREE_OPERAND (arg0, 0)));
11640 :
11641 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11642 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11643 : signed zeros are involved. */
11644 21084096 : if (!HONOR_SNANS (arg0)
11645 21082945 : && !HONOR_SIGNED_ZEROS (arg0)
11646 34893761 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11647 : {
11648 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11649 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11650 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11651 53 : bool arg0rz = false, arg0iz = false;
11652 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11653 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11654 : {
11655 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11656 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11657 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11658 : {
11659 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11660 : arg1r ? arg1r
11661 0 : : build1 (REALPART_EXPR, rtype, arg1));
11662 9 : tree ip = arg0i ? arg0i
11663 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11664 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11665 : }
11666 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11667 : {
11668 15 : tree rp = arg0r ? arg0r
11669 0 : : build1 (REALPART_EXPR, rtype, arg0);
11670 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11671 : arg1i ? arg1i
11672 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11673 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11674 : }
11675 : }
11676 : }
11677 :
11678 : /* A - B -> A + (-B) if B is easily negatable. */
11679 21084072 : if (negate_expr_p (op1)
11680 778761 : && ! TYPE_OVERFLOW_SANITIZED (type)
11681 21860301 : && ((FLOAT_TYPE_P (type)
11682 : /* Avoid this transformation if B is a positive REAL_CST. */
11683 65 : && (TREE_CODE (op1) != REAL_CST
11684 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11685 776164 : || INTEGRAL_TYPE_P (type)))
11686 776024 : return fold_build2_loc (loc, PLUS_EXPR, type,
11687 : fold_convert_loc (loc, type, arg0),
11688 776024 : negate_expr (op1));
11689 :
11690 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11691 : one. Make sure the type is not saturating and has the signedness of
11692 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11693 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11694 20308048 : if ((TREE_CODE (arg0) == MULT_EXPR
11695 19009107 : || TREE_CODE (arg1) == MULT_EXPR)
11696 2616257 : && !TYPE_SATURATING (type)
11697 2616257 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11698 2485113 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11699 22740930 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11700 : {
11701 332222 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11702 332222 : if (tem)
11703 : return tem;
11704 : }
11705 :
11706 20257829 : goto associate;
11707 :
11708 66887072 : case MULT_EXPR:
11709 66887072 : if (! FLOAT_TYPE_P (type))
11710 : {
11711 : /* Transform x * -C into -x * C if x is easily negatable. */
11712 44517967 : if (TREE_CODE (op1) == INTEGER_CST
11713 41402133 : && tree_int_cst_sgn (op1) == -1
11714 207059 : && negate_expr_p (op0)
11715 340 : && negate_expr_p (op1)
11716 324 : && (tem = negate_expr (op1)) != op1
11717 44518291 : && ! TREE_OVERFLOW (tem))
11718 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11719 : fold_convert_loc (loc, type,
11720 324 : negate_expr (op0)), tem);
11721 :
11722 44517643 : if (TREE_CODE (arg1) == INTEGER_CST
11723 44517643 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11724 : {
11725 657569 : return fold_convert_loc (loc, type, tem);
11726 : }
11727 :
11728 : /* Optimize z * conj(z) for integer complex numbers. */
11729 43860074 : if (TREE_CODE (arg0) == CONJ_EXPR
11730 43860074 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11731 1 : return fold_mult_zconjz (loc, type, arg1);
11732 43860073 : if (TREE_CODE (arg1) == CONJ_EXPR
11733 43860073 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11734 0 : return fold_mult_zconjz (loc, type, arg0);
11735 : }
11736 : else
11737 : {
11738 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11739 : This is not the same for NaNs or if signed zeros are
11740 : involved. */
11741 22369105 : if (!HONOR_NANS (arg0)
11742 33346 : && !HONOR_SIGNED_ZEROS (arg0)
11743 33009 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11744 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11745 22369330 : && real_zerop (TREE_REALPART (arg1)))
11746 : {
11747 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11748 218 : if (real_onep (TREE_IMAGPART (arg1)))
11749 : {
11750 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11751 63 : arg0 = save_expr (arg0);
11752 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11753 : rtype, arg0);
11754 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11755 : rtype, arg0);
11756 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11757 : negate_expr (iarg0),
11758 208 : rarg0);
11759 : }
11760 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11761 : {
11762 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11763 0 : arg0 = save_expr (arg0);
11764 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11765 : rtype, arg0);
11766 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11767 : rtype, arg0);
11768 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11769 : iarg0,
11770 10 : negate_expr (rarg0));
11771 : }
11772 : }
11773 :
11774 : /* Optimize z * conj(z) for floating point complex numbers.
11775 : Guarded by flag_unsafe_math_optimizations as non-finite
11776 : imaginary components don't produce scalar results. */
11777 22368887 : if (flag_unsafe_math_optimizations
11778 32832 : && TREE_CODE (arg0) == CONJ_EXPR
11779 22368889 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11780 1 : return fold_mult_zconjz (loc, type, arg1);
11781 22368886 : if (flag_unsafe_math_optimizations
11782 32831 : && TREE_CODE (arg1) == CONJ_EXPR
11783 22368890 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11784 0 : return fold_mult_zconjz (loc, type, arg0);
11785 : }
11786 66228959 : goto associate;
11787 :
11788 1940017 : case BIT_IOR_EXPR:
11789 : /* Canonicalize (X & C1) | C2. */
11790 1940017 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11791 90404 : && TREE_CODE (arg1) == INTEGER_CST
11792 1984587 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11793 : {
11794 44562 : int width = TYPE_PRECISION (type), w;
11795 44562 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11796 44562 : wide_int c2 = wi::to_wide (arg1);
11797 :
11798 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11799 44562 : if ((c1 & c2) == c1)
11800 0 : return omit_one_operand_loc (loc, type, arg1,
11801 0 : TREE_OPERAND (arg0, 0));
11802 :
11803 44562 : wide_int msk = wi::mask (width, false,
11804 44562 : TYPE_PRECISION (TREE_TYPE (arg1)));
11805 :
11806 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11807 44562 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11808 : {
11809 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11810 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11811 : }
11812 :
11813 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11814 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11815 : mode which allows further optimizations. */
11816 44556 : c1 &= msk;
11817 44556 : c2 &= msk;
11818 44556 : wide_int c3 = wi::bit_and_not (c1, c2);
11819 184522 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11820 : {
11821 95652 : wide_int mask = wi::mask (w, false,
11822 95652 : TYPE_PRECISION (type));
11823 191304 : if (((c1 | c2) & mask) == mask
11824 191304 : && wi::bit_and_not (c1, mask) == 0)
11825 : {
11826 242 : c3 = mask;
11827 242 : break;
11828 : }
11829 95652 : }
11830 :
11831 44556 : if (c3 != c1)
11832 : {
11833 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11834 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11835 558 : wide_int_to_tree (type, c3));
11836 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11837 : }
11838 45684 : }
11839 :
11840 : /* See if this can be simplified into a rotate first. If that
11841 : is unsuccessful continue in the association code. */
11842 1939453 : goto bit_rotate;
11843 :
11844 981249 : case BIT_XOR_EXPR:
11845 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11846 981249 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11847 3751 : && INTEGRAL_TYPE_P (type)
11848 3146 : && integer_onep (TREE_OPERAND (arg0, 1))
11849 982492 : && integer_onep (arg1))
11850 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11851 0 : build_zero_cst (TREE_TYPE (arg0)));
11852 :
11853 : /* See if this can be simplified into a rotate first. If that
11854 : is unsuccessful continue in the association code. */
11855 981249 : goto bit_rotate;
11856 :
11857 6688399 : case BIT_AND_EXPR:
11858 : /* Fold !X & 1 as X == 0. */
11859 6688399 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11860 6688399 : && integer_onep (arg1))
11861 : {
11862 0 : tem = TREE_OPERAND (arg0, 0);
11863 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11864 0 : build_zero_cst (TREE_TYPE (tem)));
11865 : }
11866 :
11867 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11868 : multiple of 1 << CST. */
11869 6688399 : if (TREE_CODE (arg1) == INTEGER_CST)
11870 : {
11871 4828021 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11872 4828021 : wide_int ncst1 = -cst1;
11873 4828021 : if ((cst1 & ncst1) == ncst1
11874 4985569 : && multiple_of_p (type, arg0,
11875 4985569 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11876 462 : return fold_convert_loc (loc, type, arg0);
11877 4828021 : }
11878 :
11879 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11880 : bits from CST2. */
11881 6687937 : if (TREE_CODE (arg1) == INTEGER_CST
11882 4827559 : && TREE_CODE (arg0) == MULT_EXPR
11883 6860373 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11884 : {
11885 172374 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11886 172374 : wide_int masked
11887 172374 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11888 :
11889 172374 : if (masked == 0)
11890 5631 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11891 5631 : arg0, arg1);
11892 166743 : else if (masked != warg1)
11893 : {
11894 : /* Avoid the transform if arg1 is a mask of some
11895 : mode which allows further optimizations. */
11896 638 : int pop = wi::popcount (warg1);
11897 660 : if (!(pop >= BITS_PER_UNIT
11898 50 : && pow2p_hwi (pop)
11899 682 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11900 1232 : return fold_build2_loc (loc, code, type, op0,
11901 1232 : wide_int_to_tree (type, masked));
11902 : }
11903 172374 : }
11904 :
11905 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11906 4821312 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11907 6895218 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11908 : {
11909 115602 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11910 :
11911 115602 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11912 115602 : if (mask == -1)
11913 2307 : return
11914 2307 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11915 115602 : }
11916 :
11917 6679383 : goto associate;
11918 :
11919 6142435 : case RDIV_EXPR:
11920 : /* Don't touch a floating-point divide by zero unless the mode
11921 : of the constant can represent infinity. */
11922 6142435 : if (TREE_CODE (arg1) == REAL_CST
11923 3093305 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11924 6142435 : && real_zerop (arg1))
11925 0 : return NULL_TREE;
11926 :
11927 : /* (-A) / (-B) -> A / B */
11928 6142435 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11929 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11930 3 : TREE_OPERAND (arg0, 0),
11931 3 : negate_expr (arg1));
11932 6142432 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11933 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11934 : negate_expr (arg0),
11935 0 : TREE_OPERAND (arg1, 0));
11936 : return NULL_TREE;
11937 :
11938 2218242 : case TRUNC_DIV_EXPR:
11939 : /* Fall through */
11940 :
11941 2218242 : case FLOOR_DIV_EXPR:
11942 : /* Simplify A / (B << N) where A and B are positive and B is
11943 : a power of 2, to A >> (N + log2(B)). */
11944 2218242 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11945 2218242 : && (TYPE_UNSIGNED (type)
11946 9 : || tree_expr_nonnegative_p (op0)))
11947 : {
11948 17 : tree sval = TREE_OPERAND (arg1, 0);
11949 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11950 : {
11951 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11952 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11953 16 : wi::exact_log2 (wi::to_wide (sval)));
11954 :
11955 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11956 : sh_cnt, pow2);
11957 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11958 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11959 : }
11960 : }
11961 :
11962 : /* Fall through */
11963 :
11964 3658663 : case ROUND_DIV_EXPR:
11965 3658663 : case CEIL_DIV_EXPR:
11966 3658663 : case EXACT_DIV_EXPR:
11967 3658663 : if (integer_zerop (arg1))
11968 : return NULL_TREE;
11969 :
11970 : /* Convert -A / -B to A / B when the type is signed and overflow is
11971 : undefined. */
11972 3655629 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11973 1023095 : && TREE_CODE (op0) == NEGATE_EXPR
11974 3655691 : && negate_expr_p (op1))
11975 60 : return fold_build2_loc (loc, code, type,
11976 : fold_convert_loc (loc, type,
11977 30 : TREE_OPERAND (arg0, 0)),
11978 30 : negate_expr (op1));
11979 3655599 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11980 1023065 : && TREE_CODE (arg1) == NEGATE_EXPR
11981 3655843 : && negate_expr_p (op0))
11982 36 : return fold_build2_loc (loc, code, type,
11983 : negate_expr (op0),
11984 : fold_convert_loc (loc, type,
11985 72 : TREE_OPERAND (arg1, 0)));
11986 :
11987 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11988 : operation, EXACT_DIV_EXPR.
11989 :
11990 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11991 : At one time others generated faster code, it's not clear if they do
11992 : after the last round to changes to the DIV code in expmed.cc. */
11993 3655563 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11994 3655563 : && multiple_of_p (type, arg0, arg1))
11995 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11996 : fold_convert (type, arg0),
11997 0 : fold_convert (type, arg1));
11998 :
11999 3655563 : if (TREE_CODE (arg1) == INTEGER_CST
12000 3655563 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12001 9587 : return fold_convert_loc (loc, type, tem);
12002 :
12003 : return NULL_TREE;
12004 :
12005 899668 : case CEIL_MOD_EXPR:
12006 899668 : case FLOOR_MOD_EXPR:
12007 899668 : case ROUND_MOD_EXPR:
12008 899668 : case TRUNC_MOD_EXPR:
12009 899668 : if (TREE_CODE (arg1) == INTEGER_CST
12010 899668 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12011 0 : return fold_convert_loc (loc, type, tem);
12012 :
12013 : return NULL_TREE;
12014 :
12015 2261359 : case LROTATE_EXPR:
12016 2261359 : case RROTATE_EXPR:
12017 2261359 : case RSHIFT_EXPR:
12018 2261359 : case LSHIFT_EXPR:
12019 : /* Since negative shift count is not well-defined,
12020 : don't try to compute it in the compiler. */
12021 2261359 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12022 : return NULL_TREE;
12023 :
12024 2260318 : prec = element_precision (type);
12025 :
12026 : /* If we have a rotate of a bit operation with the rotate count and
12027 : the second operand of the bit operation both constant,
12028 : permute the two operations. */
12029 2807 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12030 2242 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12031 2242 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12032 2242 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12033 2260318 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12034 : {
12035 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12036 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12037 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12038 : fold_build2_loc (loc, code, type,
12039 : arg00, arg1),
12040 : fold_build2_loc (loc, code, type,
12041 0 : arg01, arg1));
12042 : }
12043 :
12044 : return NULL_TREE;
12045 :
12046 434371 : case MIN_EXPR:
12047 434371 : case MAX_EXPR:
12048 434371 : goto associate;
12049 :
12050 6767091 : case TRUTH_ANDIF_EXPR:
12051 : /* Note that the operands of this must be ints
12052 : and their values must be 0 or 1.
12053 : ("true" is a fixed value perhaps depending on the language.) */
12054 : /* If first arg is constant zero, return it. */
12055 6767091 : if (integer_zerop (arg0))
12056 1714168 : return fold_convert_loc (loc, type, arg0);
12057 : /* FALLTHRU */
12058 15902683 : case TRUTH_AND_EXPR:
12059 : /* If either arg is constant true, drop it. */
12060 15902683 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12061 1516446 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12062 840807 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12063 : /* Preserve sequence points. */
12064 15181487 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12065 767997 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12066 : /* If second arg is constant zero, result is zero, but first arg
12067 : must be evaluated. */
12068 13618240 : if (integer_zerop (arg1))
12069 45557 : return omit_one_operand_loc (loc, type, arg1, arg0);
12070 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12071 : case will be handled here. */
12072 13572683 : if (integer_zerop (arg0))
12073 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12074 :
12075 : /* !X && X is always false. */
12076 13572683 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12077 13572683 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12078 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12079 : /* X && !X is always false. */
12080 13572683 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12081 13572683 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12082 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12083 :
12084 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12085 : means A >= Y && A != MAX, but in this case we know that
12086 : A < X <= MAX. */
12087 :
12088 13572683 : if (!TREE_SIDE_EFFECTS (arg0)
12089 13572683 : && !TREE_SIDE_EFFECTS (arg1))
12090 : {
12091 12078769 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12092 12078769 : if (tem && !operand_equal_p (tem, arg0, 0))
12093 424 : return fold_convert (type,
12094 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12095 : tem, arg1));
12096 :
12097 12078345 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12098 12078345 : if (tem && !operand_equal_p (tem, arg1, 0))
12099 8443 : return fold_convert (type,
12100 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12101 : arg0, tem));
12102 : }
12103 :
12104 13563816 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12105 : != NULL_TREE)
12106 : return tem;
12107 :
12108 : return NULL_TREE;
12109 :
12110 3547692 : case TRUTH_ORIF_EXPR:
12111 : /* Note that the operands of this must be ints
12112 : and their values must be 0 or true.
12113 : ("true" is a fixed value perhaps depending on the language.) */
12114 : /* If first arg is constant true, return it. */
12115 3547692 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12116 146941 : return fold_convert_loc (loc, type, arg0);
12117 : /* FALLTHRU */
12118 12873397 : case TRUTH_OR_EXPR:
12119 : /* If either arg is constant zero, drop it. */
12120 12873397 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12121 250573 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12122 454960 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12123 : /* Preserve sequence points. */
12124 13031027 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12125 396925 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12126 : /* If second arg is constant true, result is true, but we must
12127 : evaluate first arg. */
12128 12225899 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12129 46757 : return omit_one_operand_loc (loc, type, arg1, arg0);
12130 : /* Likewise for first arg, but note this only occurs here for
12131 : TRUTH_OR_EXPR. */
12132 12179142 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12133 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12134 :
12135 : /* !X || X is always true. */
12136 12179142 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12137 12179142 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12138 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12139 : /* X || !X is always true. */
12140 12179142 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12141 12179142 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12142 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12143 :
12144 : /* (X && !Y) || (!X && Y) is X ^ Y */
12145 12179141 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12146 1644 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12147 : {
12148 654 : tree a0, a1, l0, l1, n0, n1;
12149 :
12150 654 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12151 654 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12152 :
12153 654 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12154 654 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12155 :
12156 654 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12157 654 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12158 :
12159 654 : if ((operand_equal_p (n0, a0, 0)
12160 18 : && operand_equal_p (n1, a1, 0))
12161 662 : || (operand_equal_p (n0, a1, 0)
12162 3 : && operand_equal_p (n1, a0, 0)))
12163 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12164 : }
12165 :
12166 12179128 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12167 : != NULL_TREE)
12168 : return tem;
12169 :
12170 : return NULL_TREE;
12171 :
12172 79563 : case TRUTH_XOR_EXPR:
12173 : /* If the second arg is constant zero, drop it. */
12174 79563 : if (integer_zerop (arg1))
12175 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12176 : /* If the second arg is constant true, this is a logical inversion. */
12177 79563 : if (integer_onep (arg1))
12178 : {
12179 0 : tem = invert_truthvalue_loc (loc, arg0);
12180 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12181 : }
12182 : /* Identical arguments cancel to zero. */
12183 79563 : if (operand_equal_p (arg0, arg1, 0))
12184 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12185 :
12186 : /* !X ^ X is always true. */
12187 79563 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12188 79563 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12189 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12190 :
12191 : /* X ^ !X is always true. */
12192 79563 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12193 79563 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12194 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12195 :
12196 : return NULL_TREE;
12197 :
12198 50182774 : case EQ_EXPR:
12199 50182774 : case NE_EXPR:
12200 50182774 : STRIP_NOPS (arg0);
12201 50182774 : STRIP_NOPS (arg1);
12202 :
12203 50182774 : tem = fold_comparison (loc, code, type, op0, op1);
12204 50182774 : if (tem != NULL_TREE)
12205 : return tem;
12206 :
12207 : /* bool_var != 1 becomes !bool_var. */
12208 51336497 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12209 50220029 : && code == NE_EXPR)
12210 39389 : return fold_convert_loc (loc, type,
12211 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12212 78778 : TREE_TYPE (arg0), arg0));
12213 :
12214 : /* bool_var == 0 becomes !bool_var. */
12215 51257719 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12216 51069563 : && code == EQ_EXPR)
12217 188875 : return fold_convert_loc (loc, type,
12218 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12219 377750 : TREE_TYPE (arg0), arg0));
12220 :
12221 : /* !exp != 0 becomes !exp */
12222 620623 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12223 50567339 : && code == NE_EXPR)
12224 611577 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12225 :
12226 : /* If this is an EQ or NE comparison with zero and ARG0 is
12227 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12228 : two operations, but the latter can be done in one less insn
12229 : on machines that have only two-operand insns or on which a
12230 : constant cannot be the first operand. */
12231 49335882 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12232 49335882 : && integer_zerop (arg1))
12233 : {
12234 1547733 : tree arg00 = TREE_OPERAND (arg0, 0);
12235 1547733 : tree arg01 = TREE_OPERAND (arg0, 1);
12236 1547733 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12237 1547733 : && integer_onep (TREE_OPERAND (arg00, 0)))
12238 : {
12239 4326 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12240 4326 : arg01, TREE_OPERAND (arg00, 1));
12241 4326 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12242 4326 : build_one_cst (TREE_TYPE (arg0)));
12243 4326 : return fold_build2_loc (loc, code, type,
12244 4326 : fold_convert_loc (loc, TREE_TYPE (arg1),
12245 4326 : tem), arg1);
12246 : }
12247 1543407 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12248 1543407 : && integer_onep (TREE_OPERAND (arg01, 0)))
12249 : {
12250 425 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12251 425 : arg00, TREE_OPERAND (arg01, 1));
12252 425 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12253 425 : build_one_cst (TREE_TYPE (arg0)));
12254 425 : return fold_build2_loc (loc, code, type,
12255 425 : fold_convert_loc (loc, TREE_TYPE (arg1),
12256 425 : tem), arg1);
12257 : }
12258 : }
12259 :
12260 : /* If this is a comparison of a field, we may be able to simplify it. */
12261 49331131 : if ((TREE_CODE (arg0) == COMPONENT_REF
12262 49331131 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12263 : /* Handle the constant case even without -O
12264 : to make sure the warnings are given. */
12265 5074687 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12266 : {
12267 4758096 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12268 4758096 : if (t1)
12269 : return t1;
12270 : }
12271 :
12272 : /* Optimize comparisons of strlen vs zero to a compare of the
12273 : first character of the string vs zero. To wit,
12274 : strlen(ptr) == 0 => *ptr == 0
12275 : strlen(ptr) != 0 => *ptr != 0
12276 : Other cases should reduce to one of these two (or a constant)
12277 : due to the return value of strlen being unsigned. */
12278 48525687 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12279 : {
12280 3072471 : tree fndecl = get_callee_fndecl (arg0);
12281 :
12282 3072471 : if (fndecl
12283 3071376 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12284 550 : && call_expr_nargs (arg0) == 1
12285 3073021 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12286 : == POINTER_TYPE))
12287 : {
12288 550 : tree ptrtype
12289 550 : = build_pointer_type (build_qualified_type (char_type_node,
12290 : TYPE_QUAL_CONST));
12291 1100 : tree ptr = fold_convert_loc (loc, ptrtype,
12292 550 : CALL_EXPR_ARG (arg0, 0));
12293 550 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12294 550 : return fold_build2_loc (loc, code, type, iref,
12295 550 : build_int_cst (TREE_TYPE (iref), 0));
12296 : }
12297 : }
12298 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12299 : (X & C) == 0 when C is a single bit. */
12300 48525137 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12301 1716463 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12302 861 : && integer_zerop (arg1)
12303 48525599 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12304 : {
12305 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12306 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12307 140 : TREE_OPERAND (arg0, 1));
12308 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12309 : type, tem,
12310 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12311 140 : arg1));
12312 : }
12313 :
12314 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12315 : constant C is a power of two, i.e. a single bit. */
12316 48524997 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12317 3936 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12318 0 : && integer_zerop (arg1)
12319 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12320 48524997 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12321 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12322 : {
12323 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12324 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12325 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12326 : }
12327 :
12328 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12329 : when is C is a power of two, i.e. a single bit. */
12330 48524997 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12331 1716323 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12332 38410 : && integer_zerop (arg1)
12333 38410 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12334 48560566 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12335 35569 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12336 : {
12337 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12338 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12339 0 : arg000, TREE_OPERAND (arg0, 1));
12340 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12341 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12342 : }
12343 :
12344 48524997 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12345 3936 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12346 : {
12347 74 : tree arg00 = TREE_OPERAND (arg0, 0);
12348 74 : tree arg01 = TREE_OPERAND (arg0, 1);
12349 74 : tree arg10 = TREE_OPERAND (arg1, 0);
12350 74 : tree arg11 = TREE_OPERAND (arg1, 1);
12351 74 : tree itype = TREE_TYPE (arg0);
12352 :
12353 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12354 : operand_equal_p guarantees no side-effects so we don't need
12355 : to use omit_one_operand on Z. */
12356 74 : if (operand_equal_p (arg01, arg11, 0))
12357 8 : return fold_build2_loc (loc, code, type, arg00,
12358 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12359 8 : arg10));
12360 66 : if (operand_equal_p (arg01, arg10, 0))
12361 0 : return fold_build2_loc (loc, code, type, arg00,
12362 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12363 0 : arg11));
12364 66 : if (operand_equal_p (arg00, arg11, 0))
12365 0 : return fold_build2_loc (loc, code, type, arg01,
12366 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12367 0 : arg10));
12368 66 : if (operand_equal_p (arg00, arg10, 0))
12369 0 : return fold_build2_loc (loc, code, type, arg01,
12370 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12371 0 : arg11));
12372 :
12373 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12374 66 : if (TREE_CODE (arg01) == INTEGER_CST
12375 8 : && TREE_CODE (arg11) == INTEGER_CST)
12376 : {
12377 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12378 : fold_convert_loc (loc, itype, arg11));
12379 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12380 8 : return fold_build2_loc (loc, code, type, tem,
12381 8 : fold_convert_loc (loc, itype, arg10));
12382 : }
12383 : }
12384 :
12385 : /* Attempt to simplify equality/inequality comparisons of complex
12386 : values. Only lower the comparison if the result is known or
12387 : can be simplified to a single scalar comparison. */
12388 48524981 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12389 48522418 : || TREE_CODE (arg0) == COMPLEX_CST)
12390 2563 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12391 2365 : || TREE_CODE (arg1) == COMPLEX_CST))
12392 : {
12393 1750 : tree real0, imag0, real1, imag1;
12394 1750 : tree rcond, icond;
12395 :
12396 1750 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12397 : {
12398 1750 : real0 = TREE_OPERAND (arg0, 0);
12399 1750 : imag0 = TREE_OPERAND (arg0, 1);
12400 : }
12401 : else
12402 : {
12403 0 : real0 = TREE_REALPART (arg0);
12404 0 : imag0 = TREE_IMAGPART (arg0);
12405 : }
12406 :
12407 1750 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12408 : {
12409 198 : real1 = TREE_OPERAND (arg1, 0);
12410 198 : imag1 = TREE_OPERAND (arg1, 1);
12411 : }
12412 : else
12413 : {
12414 1552 : real1 = TREE_REALPART (arg1);
12415 1552 : imag1 = TREE_IMAGPART (arg1);
12416 : }
12417 :
12418 1750 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12419 1750 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12420 : {
12421 11 : if (integer_zerop (rcond))
12422 : {
12423 11 : if (code == EQ_EXPR)
12424 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12425 0 : imag0, imag1);
12426 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12427 : }
12428 : else
12429 : {
12430 0 : if (code == NE_EXPR)
12431 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12432 0 : imag0, imag1);
12433 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12434 : }
12435 : }
12436 :
12437 1739 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12438 1739 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12439 : {
12440 9 : if (integer_zerop (icond))
12441 : {
12442 7 : if (code == EQ_EXPR)
12443 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12444 1 : real0, real1);
12445 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12446 : }
12447 : else
12448 : {
12449 2 : if (code == NE_EXPR)
12450 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12451 1 : real0, real1);
12452 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12453 : }
12454 : }
12455 : }
12456 :
12457 : return NULL_TREE;
12458 :
12459 41640179 : case LT_EXPR:
12460 41640179 : case GT_EXPR:
12461 41640179 : case LE_EXPR:
12462 41640179 : case GE_EXPR:
12463 41640179 : tem = fold_comparison (loc, code, type, op0, op1);
12464 41640179 : if (tem != NULL_TREE)
12465 : return tem;
12466 :
12467 : /* Transform comparisons of the form X +- C CMP X. */
12468 40767041 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12469 4785950 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12470 51387 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12471 40767057 : && !HONOR_SNANS (arg0))
12472 : {
12473 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12474 14 : enum tree_code code0 = TREE_CODE (arg0);
12475 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12476 :
12477 : /* (X - c) > X becomes false. */
12478 14 : if (code == GT_EXPR
12479 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12480 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12481 4 : return constant_boolean_node (0, type);
12482 :
12483 : /* Likewise (X + c) < X becomes false. */
12484 10 : if (code == LT_EXPR
12485 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12486 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12487 3 : return constant_boolean_node (0, type);
12488 :
12489 : /* Convert (X - c) <= X to true. */
12490 7 : if (!HONOR_NANS (arg1)
12491 6 : && code == LE_EXPR
12492 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12493 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12494 4 : return constant_boolean_node (1, type);
12495 :
12496 : /* Convert (X + c) >= X to true. */
12497 3 : if (!HONOR_NANS (arg1)
12498 2 : && code == GE_EXPR
12499 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12500 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12501 2 : return constant_boolean_node (1, type);
12502 : }
12503 :
12504 : /* If we are comparing an ABS_EXPR with a constant, we can
12505 : convert all the cases into explicit comparisons, but they may
12506 : well not be faster than doing the ABS and one comparison.
12507 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12508 : and a comparison, and is probably faster. */
12509 40767028 : if (code == LE_EXPR
12510 7774243 : && TREE_CODE (arg1) == INTEGER_CST
12511 5541465 : && TREE_CODE (arg0) == ABS_EXPR
12512 484 : && ! TREE_SIDE_EFFECTS (arg0)
12513 484 : && (tem = negate_expr (arg1)) != 0
12514 484 : && TREE_CODE (tem) == INTEGER_CST
12515 40767512 : && !TREE_OVERFLOW (tem))
12516 968 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12517 : build2 (GE_EXPR, type,
12518 484 : TREE_OPERAND (arg0, 0), tem),
12519 : build2 (LE_EXPR, type,
12520 968 : TREE_OPERAND (arg0, 0), arg1));
12521 :
12522 : /* Convert ABS_EXPR<x> >= 0 to true. */
12523 40766544 : if (code == GE_EXPR
12524 4248084 : && (integer_zerop (arg1)
12525 3102063 : || (! HONOR_NANS (arg0)
12526 2422033 : && real_zerop (arg1)))
12527 41912796 : && tree_expr_nonnegative_p (arg0))
12528 5221 : return omit_one_operand_loc (loc, type,
12529 : constant_boolean_node (true, type),
12530 5221 : arg0);
12531 :
12532 : /* Convert ABS_EXPR<x> < 0 to false. */
12533 40761323 : if (code == LT_EXPR
12534 13526515 : && (integer_zerop (arg1) || real_zerop (arg1))
12535 44049436 : && tree_expr_nonnegative_p (arg0))
12536 43457 : return omit_one_operand_loc (loc, type,
12537 : constant_boolean_node (false, type),
12538 43457 : arg0);
12539 :
12540 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12541 : and similarly for >= into !=. */
12542 40717866 : if ((code == LT_EXPR || code == GE_EXPR)
12543 17725921 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12544 5628230 : && TREE_CODE (arg1) == LSHIFT_EXPR
12545 40719390 : && integer_onep (TREE_OPERAND (arg1, 0)))
12546 4048 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12547 1352 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12548 1352 : TREE_OPERAND (arg1, 1)),
12549 2704 : build_zero_cst (TREE_TYPE (arg0)));
12550 :
12551 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12552 : otherwise Y might be >= # of bits in X's type and thus e.g.
12553 : (unsigned char) (1 << Y) for Y 15 might be 0.
12554 : If the cast is widening, then 1 << Y should have unsigned type,
12555 : otherwise if Y is number of bits in the signed shift type minus 1,
12556 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12557 : 31 might be 0xffffffff80000000. */
12558 40716514 : if ((code == LT_EXPR || code == GE_EXPR)
12559 17724569 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12560 5816593 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12561 11931465 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12562 4102441 : && CONVERT_EXPR_P (arg1)
12563 1141083 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12564 42 : && (element_precision (TREE_TYPE (arg1))
12565 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12566 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12567 14 : || (element_precision (TREE_TYPE (arg1))
12568 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12569 40716521 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12570 : {
12571 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12572 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12573 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12574 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12575 14 : build_zero_cst (TREE_TYPE (arg0)));
12576 : }
12577 :
12578 : return NULL_TREE;
12579 :
12580 6026647 : case UNORDERED_EXPR:
12581 6026647 : case ORDERED_EXPR:
12582 6026647 : case UNLT_EXPR:
12583 6026647 : case UNLE_EXPR:
12584 6026647 : case UNGT_EXPR:
12585 6026647 : case UNGE_EXPR:
12586 6026647 : case UNEQ_EXPR:
12587 6026647 : case LTGT_EXPR:
12588 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12589 6026647 : {
12590 6026647 : tree targ0 = strip_float_extensions (arg0);
12591 6026647 : tree targ1 = strip_float_extensions (arg1);
12592 6026647 : tree newtype = TREE_TYPE (targ0);
12593 :
12594 6026647 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12595 1215 : newtype = TREE_TYPE (targ1);
12596 :
12597 6026647 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12598 6026647 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12599 328 : return fold_build2_loc (loc, code, type,
12600 : fold_convert_loc (loc, newtype, targ0),
12601 328 : fold_convert_loc (loc, newtype, targ1));
12602 : }
12603 :
12604 : return NULL_TREE;
12605 :
12606 8772226 : case COMPOUND_EXPR:
12607 : /* When pedantic, a compound expression can be neither an lvalue
12608 : nor an integer constant expression. */
12609 8772226 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12610 : return NULL_TREE;
12611 : /* Don't let (0, 0) be null pointer constant. */
12612 520792 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12613 520792 : : fold_convert_loc (loc, type, arg1);
12614 : return tem;
12615 :
12616 : default:
12617 : return NULL_TREE;
12618 : } /* switch (code) */
12619 : }
12620 :
12621 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12622 : ((A & N) + B) & M -> (A + B) & M
12623 : Similarly if (N & M) == 0,
12624 : ((A | N) + B) & M -> (A + B) & M
12625 : and for - instead of + (or unary - instead of +)
12626 : and/or ^ instead of |.
12627 : If B is constant and (B & M) == 0, fold into A & M.
12628 :
12629 : This function is a helper for match.pd patterns. Return non-NULL
12630 : type in which the simplified operation should be performed only
12631 : if any optimization is possible.
12632 :
12633 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12634 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12635 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12636 : +/-. */
12637 : tree
12638 1283145 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12639 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12640 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12641 : tree *pmop)
12642 : {
12643 1283145 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12644 1283145 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12645 1283145 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12646 2566290 : if (~cst1 == 0
12647 3845077 : || (cst1 & (cst1 + 1)) != 0
12648 1070045 : || !INTEGRAL_TYPE_P (type)
12649 1070045 : || (!TYPE_OVERFLOW_WRAPS (type)
12650 44668 : && TREE_CODE (type) != INTEGER_TYPE)
12651 4704201 : || (wi::max_value (type) & cst1) != cst1)
12652 : return NULL_TREE;
12653 :
12654 1070045 : enum tree_code codes[2] = { code00, code01 };
12655 1070045 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12656 1070045 : int which = 0;
12657 1070045 : wide_int cst0;
12658 :
12659 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12660 : arg1 (M) is == (1LL << cst) - 1.
12661 : Store C into PMOP[0] and D into PMOP[1]. */
12662 1070045 : pmop[0] = arg00;
12663 1070045 : pmop[1] = arg01;
12664 1070045 : which = code != NEGATE_EXPR;
12665 :
12666 3209217 : for (; which >= 0; which--)
12667 2139172 : switch (codes[which])
12668 : {
12669 21222 : case BIT_AND_EXPR:
12670 21222 : case BIT_IOR_EXPR:
12671 21222 : case BIT_XOR_EXPR:
12672 21222 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12673 21222 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12674 21222 : if (codes[which] == BIT_AND_EXPR)
12675 : {
12676 21110 : if (cst0 != cst1)
12677 : break;
12678 : }
12679 112 : else if (cst0 != 0)
12680 : break;
12681 : /* If C or D is of the form (A & N) where
12682 : (N & M) == M, or of the form (A | N) or
12683 : (A ^ N) where (N & M) == 0, replace it with A. */
12684 19683 : pmop[which] = arg0xx[2 * which];
12685 19683 : break;
12686 2117950 : case ERROR_MARK:
12687 2117950 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12688 : break;
12689 : /* If C or D is a N where (N & M) == 0, it can be
12690 : omitted (replaced with 0). */
12691 901942 : if ((code == PLUS_EXPR
12692 219083 : || (code == MINUS_EXPR && which == 0))
12693 670054 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12694 138969 : pmop[which] = build_int_cst (type, 0);
12695 : /* Similarly, with C - N where (-N & M) == 0. */
12696 901942 : if (code == MINUS_EXPR
12697 450971 : && which == 1
12698 662802 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12699 203798 : pmop[which] = build_int_cst (type, 0);
12700 : break;
12701 0 : default:
12702 0 : gcc_unreachable ();
12703 : }
12704 :
12705 : /* Only build anything new if we optimized one or both arguments above. */
12706 1070045 : if (pmop[0] == arg00 && pmop[1] == arg01)
12707 : return NULL_TREE;
12708 :
12709 361869 : if (TYPE_OVERFLOW_WRAPS (type))
12710 : return type;
12711 : else
12712 3010 : return unsigned_type_for (type);
12713 1070045 : }
12714 :
12715 : /* Used by contains_label_[p1]. */
12716 :
12717 : struct contains_label_data
12718 : {
12719 : hash_set<tree> *pset;
12720 : bool inside_switch_p;
12721 : };
12722 :
12723 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12724 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12725 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12726 :
12727 : static tree
12728 4617868 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12729 : {
12730 4617868 : contains_label_data *d = (contains_label_data *) data;
12731 4617868 : switch (TREE_CODE (*tp))
12732 : {
12733 : case LABEL_EXPR:
12734 : return *tp;
12735 :
12736 0 : case CASE_LABEL_EXPR:
12737 0 : if (!d->inside_switch_p)
12738 : return *tp;
12739 : return NULL_TREE;
12740 :
12741 0 : case SWITCH_EXPR:
12742 0 : if (!d->inside_switch_p)
12743 : {
12744 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12745 0 : return *tp;
12746 0 : d->inside_switch_p = true;
12747 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12748 0 : return *tp;
12749 0 : d->inside_switch_p = false;
12750 0 : *walk_subtrees = 0;
12751 : }
12752 : return NULL_TREE;
12753 :
12754 6653 : case GOTO_EXPR:
12755 6653 : *walk_subtrees = 0;
12756 6653 : return NULL_TREE;
12757 :
12758 : default:
12759 : return NULL_TREE;
12760 : }
12761 : }
12762 :
12763 : /* Return whether the sub-tree ST contains a label which is accessible from
12764 : outside the sub-tree. */
12765 :
12766 : static bool
12767 330903 : contains_label_p (tree st)
12768 : {
12769 330903 : hash_set<tree> pset;
12770 330903 : contains_label_data data = { &pset, false };
12771 330903 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12772 330903 : }
12773 :
12774 : /* Fold a ternary expression of code CODE and type TYPE with operands
12775 : OP0, OP1, and OP2. Return the folded expression if folding is
12776 : successful. Otherwise, return NULL_TREE. */
12777 :
12778 : tree
12779 47728677 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12780 : tree op0, tree op1, tree op2)
12781 : {
12782 47728677 : tree tem;
12783 47728677 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12784 47728677 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12785 :
12786 47728677 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12787 : && TREE_CODE_LENGTH (code) == 3);
12788 :
12789 : /* If this is a commutative operation, and OP0 is a constant, move it
12790 : to OP1 to reduce the number of tests below. */
12791 47728677 : if (commutative_ternary_tree_code (code)
12792 47728677 : && tree_swap_operands_p (op0, op1))
12793 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12794 :
12795 47728644 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12796 47728644 : if (tem)
12797 : return tem;
12798 :
12799 : /* Strip any conversions that don't change the mode. This is safe
12800 : for every expression, except for a comparison expression because
12801 : its signedness is derived from its operands. So, in the latter
12802 : case, only strip conversions that don't change the signedness.
12803 :
12804 : Note that this is done as an internal manipulation within the
12805 : constant folder, in order to find the simplest representation of
12806 : the arguments so that their form can be studied. In any cases,
12807 : the appropriate type conversions should be put back in the tree
12808 : that will get out of the constant folder. */
12809 46630996 : if (op0)
12810 : {
12811 46563187 : arg0 = op0;
12812 46563187 : STRIP_NOPS (arg0);
12813 : }
12814 :
12815 46630996 : if (op1)
12816 : {
12817 46630996 : arg1 = op1;
12818 46630996 : STRIP_NOPS (arg1);
12819 : }
12820 :
12821 46630996 : if (op2)
12822 : {
12823 15115883 : arg2 = op2;
12824 15115883 : STRIP_NOPS (arg2);
12825 : }
12826 :
12827 46630996 : switch (code)
12828 : {
12829 31514631 : case COMPONENT_REF:
12830 31514631 : if (TREE_CODE (arg0) == CONSTRUCTOR
12831 31514631 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12832 : {
12833 : unsigned HOST_WIDE_INT idx;
12834 : tree field, value;
12835 886 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12836 681 : if (field == arg1)
12837 : return value;
12838 : }
12839 : return NULL_TREE;
12840 :
12841 12694332 : case COND_EXPR:
12842 12694332 : case VEC_COND_EXPR:
12843 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12844 : so all simple results must be passed through pedantic_non_lvalue. */
12845 12694332 : if (TREE_CODE (arg0) == INTEGER_CST)
12846 : {
12847 465246 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12848 465246 : tem = integer_zerop (arg0) ? op2 : op1;
12849 : /* Only optimize constant conditions when the selected branch
12850 : has the same type as the COND_EXPR. This avoids optimizing
12851 : away "c ? x : throw", where the throw has a void type.
12852 : Avoid throwing away that operand which contains label. */
12853 465246 : if ((!TREE_SIDE_EFFECTS (unused_op)
12854 330903 : || !contains_label_p (unused_op))
12855 791307 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12856 380500 : || VOID_TYPE_P (type)))
12857 451182 : return protected_set_expr_location_unshare (tem, loc);
12858 : return NULL_TREE;
12859 : }
12860 12229086 : else if (TREE_CODE (arg0) == VECTOR_CST)
12861 : {
12862 11849 : unsigned HOST_WIDE_INT nelts;
12863 11849 : if ((TREE_CODE (arg1) == VECTOR_CST
12864 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12865 2808 : && (TREE_CODE (arg2) == VECTOR_CST
12866 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12867 23698 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12868 : {
12869 2808 : vec_perm_builder sel (nelts, nelts, 1);
12870 27234 : for (unsigned int i = 0; i < nelts; i++)
12871 : {
12872 24426 : tree val = VECTOR_CST_ELT (arg0, i);
12873 24426 : if (integer_all_onesp (val))
12874 11988 : sel.quick_push (i);
12875 12438 : else if (integer_zerop (val))
12876 12438 : sel.quick_push (nelts + i);
12877 : else /* Currently unreachable. */
12878 2120 : return NULL_TREE;
12879 : }
12880 2808 : vec_perm_indices indices (sel, 2, nelts);
12881 2808 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12882 2808 : if (t != NULL_TREE)
12883 2120 : return t;
12884 4928 : }
12885 : }
12886 :
12887 : /* If we have A op B ? A : C, we may be able to convert this to a
12888 : simpler expression, depending on the operation and the values
12889 : of B and C. Signed zeros prevent all of these transformations,
12890 : for reasons given above each one.
12891 :
12892 : Also try swapping the arguments and inverting the conditional. */
12893 12226966 : if (COMPARISON_CLASS_P (arg0)
12894 10056158 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12895 12368768 : && !HONOR_SIGNED_ZEROS (op1))
12896 : {
12897 131007 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12898 131007 : TREE_OPERAND (arg0, 0),
12899 131007 : TREE_OPERAND (arg0, 1),
12900 : op1, op2);
12901 131007 : if (tem)
12902 : return tem;
12903 : }
12904 :
12905 12220044 : if (COMPARISON_CLASS_P (arg0)
12906 10049236 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12907 12695506 : && !HONOR_SIGNED_ZEROS (op2))
12908 : {
12909 388549 : enum tree_code comp_code = TREE_CODE (arg0);
12910 388549 : tree arg00 = TREE_OPERAND (arg0, 0);
12911 388549 : tree arg01 = TREE_OPERAND (arg0, 1);
12912 388549 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12913 388549 : if (comp_code != ERROR_MARK)
12914 388549 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12915 : arg00,
12916 : arg01,
12917 : op2, op1);
12918 388549 : if (tem)
12919 : return tem;
12920 : }
12921 :
12922 : /* If the second operand is simpler than the third, swap them
12923 : since that produces better jump optimization results. */
12924 11951404 : if (truth_value_p (TREE_CODE (arg0))
12925 11951404 : && tree_swap_operands_p (op1, op2))
12926 : {
12927 2078561 : location_t loc0 = expr_location_or (arg0, loc);
12928 : /* See if this can be inverted. If it can't, possibly because
12929 : it was a floating-point inequality comparison, don't do
12930 : anything. */
12931 2078561 : tem = fold_invert_truthvalue (loc0, arg0);
12932 2078561 : if (tem)
12933 1325611 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12934 : }
12935 :
12936 : /* Convert A ? 1 : 0 to simply A. */
12937 10625793 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12938 10173964 : : (integer_onep (op1)
12939 416182 : && !VECTOR_TYPE_P (type)))
12940 694394 : && integer_zerop (op2)
12941 : /* If we try to convert OP0 to our type, the
12942 : call to fold will try to move the conversion inside
12943 : a COND, which will recurse. In that case, the COND_EXPR
12944 : is probably the best choice, so leave it alone. */
12945 11758123 : && type == TREE_TYPE (arg0))
12946 32970 : return protected_set_expr_location_unshare (arg0, loc);
12947 :
12948 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12949 : over COND_EXPR in cases such as floating point comparisons. */
12950 10592823 : if (integer_zerop (op1)
12951 395811 : && code == COND_EXPR
12952 371849 : && integer_onep (op2)
12953 32468 : && !VECTOR_TYPE_P (type)
12954 10625291 : && truth_value_p (TREE_CODE (arg0)))
12955 30912 : return fold_convert_loc (loc, type,
12956 30912 : invert_truthvalue_loc (loc, arg0));
12957 :
12958 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12959 10561911 : if (TREE_CODE (arg0) == LT_EXPR
12960 1385977 : && integer_zerop (TREE_OPERAND (arg0, 1))
12961 38430 : && integer_zerop (op2)
12962 10563277 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12963 : {
12964 : /* sign_bit_p looks through both zero and sign extensions,
12965 : but for this optimization only sign extensions are
12966 : usable. */
12967 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12968 56 : while (tem != tem2)
12969 : {
12970 0 : if (TREE_CODE (tem2) != NOP_EXPR
12971 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12972 : {
12973 : tem = NULL_TREE;
12974 : break;
12975 : }
12976 0 : tem2 = TREE_OPERAND (tem2, 0);
12977 : }
12978 : /* sign_bit_p only checks ARG1 bits within A's precision.
12979 : If <sign bit of A> has wider type than A, bits outside
12980 : of A's precision in <sign bit of A> need to be checked.
12981 : If they are all 0, this optimization needs to be done
12982 : in unsigned A's type, if they are all 1 in signed A's type,
12983 : otherwise this can't be done. */
12984 56 : if (tem
12985 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12986 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12987 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12988 56 : < TYPE_PRECISION (type))
12989 : {
12990 56 : int inner_width, outer_width;
12991 56 : tree tem_type;
12992 :
12993 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12994 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12995 56 : if (outer_width > TYPE_PRECISION (type))
12996 0 : outer_width = TYPE_PRECISION (type);
12997 :
12998 56 : wide_int mask = wi::shifted_mask
12999 56 : (inner_width, outer_width - inner_width, false,
13000 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13001 :
13002 56 : wide_int common = mask & wi::to_wide (arg1);
13003 56 : if (common == mask)
13004 : {
13005 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13006 28 : tem = fold_convert_loc (loc, tem_type, tem);
13007 : }
13008 28 : else if (common == 0)
13009 : {
13010 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13011 0 : tem = fold_convert_loc (loc, tem_type, tem);
13012 : }
13013 : else
13014 : tem = NULL;
13015 56 : }
13016 :
13017 56 : if (tem)
13018 28 : return
13019 56 : fold_convert_loc (loc, type,
13020 : fold_build2_loc (loc, BIT_AND_EXPR,
13021 28 : TREE_TYPE (tem), tem,
13022 : fold_convert_loc (loc,
13023 28 : TREE_TYPE (tem),
13024 28 : arg1)));
13025 : }
13026 :
13027 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13028 : already handled above. */
13029 10561883 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13030 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13031 3 : && integer_zerop (op2)
13032 10561883 : && integer_pow2p (arg1))
13033 : {
13034 0 : tree tem = TREE_OPERAND (arg0, 0);
13035 0 : STRIP_NOPS (tem);
13036 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13037 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13038 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13039 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13040 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13041 : fold_convert_loc (loc, type,
13042 0 : TREE_OPERAND (tem, 0)),
13043 0 : op1);
13044 : }
13045 :
13046 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13047 : is probably obsolete because the first operand should be a
13048 : truth value (that's why we have the two cases above), but let's
13049 : leave it in until we can confirm this for all front-ends. */
13050 10561883 : if (integer_zerop (op2)
13051 2071321 : && TREE_CODE (arg0) == NE_EXPR
13052 552039 : && integer_zerop (TREE_OPERAND (arg0, 1))
13053 299599 : && integer_pow2p (arg1)
13054 32925 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13055 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13056 : arg1, OEP_ONLY_CONST)
13057 : /* operand_equal_p compares just value, not precision, so e.g.
13058 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13059 : second operand 32-bit -128, which is not a power of two (or vice
13060 : versa. */
13061 10561883 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13062 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13063 :
13064 : /* Disable the transformations below for vectors, since
13065 : fold_binary_op_with_conditional_arg may undo them immediately,
13066 : yielding an infinite loop. */
13067 10561883 : if (code == VEC_COND_EXPR)
13068 : return NULL_TREE;
13069 :
13070 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13071 10110054 : if (integer_zerop (op2)
13072 1716171 : && truth_value_p (TREE_CODE (arg0))
13073 1522214 : && truth_value_p (TREE_CODE (arg1))
13074 10143510 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13075 33456 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13076 : : TRUTH_ANDIF_EXPR,
13077 33456 : type, fold_convert_loc (loc, type, arg0), op1);
13078 :
13079 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13080 10076598 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13081 456814 : && truth_value_p (TREE_CODE (arg0))
13082 308605 : && truth_value_p (TREE_CODE (arg1))
13083 10113909 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13084 : {
13085 37311 : location_t loc0 = expr_location_or (arg0, loc);
13086 : /* Only perform transformation if ARG0 is easily inverted. */
13087 37311 : tem = fold_invert_truthvalue (loc0, arg0);
13088 37311 : if (tem)
13089 37047 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13090 : ? BIT_IOR_EXPR
13091 : : TRUTH_ORIF_EXPR,
13092 : type, fold_convert_loc (loc, type, tem),
13093 37047 : op1);
13094 : }
13095 :
13096 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13097 10039551 : if (integer_zerop (arg1)
13098 341014 : && truth_value_p (TREE_CODE (arg0))
13099 84680 : && truth_value_p (TREE_CODE (op2))
13100 10039579 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13101 : {
13102 28 : location_t loc0 = expr_location_or (arg0, loc);
13103 : /* Only perform transformation if ARG0 is easily inverted. */
13104 28 : tem = fold_invert_truthvalue (loc0, arg0);
13105 28 : if (tem)
13106 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13107 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13108 : type, fold_convert_loc (loc, type, tem),
13109 0 : op2);
13110 : }
13111 :
13112 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13113 10039551 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13114 383212 : && truth_value_p (TREE_CODE (arg0))
13115 273675 : && truth_value_p (TREE_CODE (op2))
13116 10039737 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13117 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13118 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13119 186 : type, fold_convert_loc (loc, type, arg0), op2);
13120 :
13121 : return NULL_TREE;
13122 :
13123 0 : case CALL_EXPR:
13124 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13125 : of fold_ternary on them. */
13126 0 : gcc_unreachable ();
13127 :
13128 872188 : case BIT_FIELD_REF:
13129 872188 : if (TREE_CODE (arg0) == VECTOR_CST
13130 80290 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13131 42683 : || (VECTOR_TYPE_P (type)
13132 41855 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13133 79434 : && tree_fits_uhwi_p (op1)
13134 951622 : && tree_fits_uhwi_p (op2))
13135 : {
13136 79434 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13137 79434 : unsigned HOST_WIDE_INT width
13138 79434 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13139 79434 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13140 79434 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13141 79434 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13142 :
13143 79434 : if (n != 0
13144 79434 : && (idx % width) == 0
13145 79434 : && (n % width) == 0
13146 158868 : && known_le ((idx + n) / width,
13147 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13148 : {
13149 79434 : idx = idx / width;
13150 79434 : n = n / width;
13151 :
13152 79434 : if (TREE_CODE (arg0) == VECTOR_CST)
13153 : {
13154 79434 : if (n == 1)
13155 : {
13156 37611 : tem = VECTOR_CST_ELT (arg0, idx);
13157 37611 : if (VECTOR_TYPE_P (type))
13158 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13159 : return tem;
13160 : }
13161 :
13162 41823 : tree_vector_builder vals (type, n, 1);
13163 227002 : for (unsigned i = 0; i < n; ++i)
13164 143356 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13165 41823 : return vals.build ();
13166 41823 : }
13167 : }
13168 : }
13169 :
13170 : /* On constants we can use native encode/interpret to constant
13171 : fold (nearly) all BIT_FIELD_REFs. */
13172 792754 : if (CONSTANT_CLASS_P (arg0)
13173 1661 : && can_native_interpret_type_p (type)
13174 : && BITS_PER_UNIT == 8
13175 1661 : && tree_fits_uhwi_p (op1)
13176 794415 : && tree_fits_uhwi_p (op2))
13177 : {
13178 1661 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13179 1661 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13180 : /* Limit us to a reasonable amount of work. To relax the
13181 : other limitations we need bit-shifting of the buffer
13182 : and rounding up the size. */
13183 1661 : if (bitpos % BITS_PER_UNIT == 0
13184 1661 : && bitsize % BITS_PER_UNIT == 0
13185 1661 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13186 : {
13187 1661 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13188 1661 : unsigned HOST_WIDE_INT len
13189 1661 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13190 1661 : bitpos / BITS_PER_UNIT);
13191 1661 : if (len > 0
13192 1661 : && len * BITS_PER_UNIT >= bitsize)
13193 : {
13194 1661 : tree v = native_interpret_expr (type, b,
13195 : bitsize / BITS_PER_UNIT);
13196 1661 : if (v)
13197 1607 : return v;
13198 : }
13199 : }
13200 : }
13201 :
13202 : return NULL_TREE;
13203 :
13204 783669 : case VEC_PERM_EXPR:
13205 : /* Perform constant folding of BIT_INSERT_EXPR. */
13206 783669 : if (TREE_CODE (arg2) == VECTOR_CST
13207 772195 : && TREE_CODE (op0) == VECTOR_CST
13208 16559 : && TREE_CODE (op1) == VECTOR_CST)
13209 : {
13210 : /* Build a vector of integers from the tree mask. */
13211 3939 : vec_perm_builder builder;
13212 3939 : if (!tree_to_vec_perm_builder (&builder, arg2))
13213 : return NULL_TREE;
13214 :
13215 : /* Create a vec_perm_indices for the integer vector. */
13216 3939 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13217 3939 : bool single_arg = (op0 == op1);
13218 7878 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13219 3939 : return fold_vec_perm (type, op0, op1, sel);
13220 7878 : }
13221 : return NULL_TREE;
13222 :
13223 15969 : case BIT_INSERT_EXPR:
13224 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13225 15969 : if (TREE_CODE (arg0) == INTEGER_CST
13226 14 : && TREE_CODE (arg1) == INTEGER_CST)
13227 : {
13228 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13229 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13230 2 : if (BYTES_BIG_ENDIAN)
13231 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13232 2 : wide_int tem = (wi::to_wide (arg0)
13233 4 : & wi::shifted_mask (bitpos, bitsize, true,
13234 4 : TYPE_PRECISION (type)));
13235 2 : wide_int tem2
13236 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13237 2 : bitsize), bitpos);
13238 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13239 2 : }
13240 15967 : else if (TREE_CODE (arg0) == VECTOR_CST
13241 903 : && CONSTANT_CLASS_P (arg1)
13242 16266 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13243 299 : TREE_TYPE (arg1)))
13244 : {
13245 299 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13246 299 : unsigned HOST_WIDE_INT elsize
13247 299 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13248 299 : if (bitpos % elsize == 0)
13249 : {
13250 299 : unsigned k = bitpos / elsize;
13251 299 : unsigned HOST_WIDE_INT nelts;
13252 299 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13253 47728677 : return arg0;
13254 292 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13255 : {
13256 292 : tree_vector_builder elts (type, nelts, 1);
13257 292 : elts.quick_grow (nelts);
13258 1608 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13259 1024 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13260 292 : return elts.build ();
13261 292 : }
13262 : }
13263 : }
13264 : return NULL_TREE;
13265 :
13266 : default:
13267 : return NULL_TREE;
13268 : } /* switch (code) */
13269 : }
13270 :
13271 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13272 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13273 : constructor element index of the value returned. If the element is
13274 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13275 : the index of the element after the ACCESS_INDEX position (which
13276 : may be outside of the CTOR array). */
13277 :
13278 : tree
13279 697822 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13280 : unsigned *ctor_idx)
13281 : {
13282 697822 : tree index_type = NULL_TREE;
13283 697822 : signop index_sgn = UNSIGNED;
13284 697822 : offset_int low_bound = 0;
13285 :
13286 697822 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13287 : {
13288 697822 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13289 697822 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13290 : {
13291 : /* Static constructors for variably sized objects makes no sense. */
13292 697822 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13293 697822 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13294 : /* ??? When it is obvious that the range is signed, treat it so. */
13295 697822 : if (TYPE_UNSIGNED (index_type)
13296 357954 : && TYPE_MAX_VALUE (domain_type)
13297 1055745 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13298 357923 : TYPE_MIN_VALUE (domain_type)))
13299 : {
13300 0 : index_sgn = SIGNED;
13301 0 : low_bound
13302 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13303 : SIGNED);
13304 : }
13305 : else
13306 : {
13307 697822 : index_sgn = TYPE_SIGN (index_type);
13308 697822 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13309 : }
13310 : }
13311 : }
13312 :
13313 697822 : if (index_type)
13314 697822 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13315 : index_sgn);
13316 :
13317 697822 : offset_int index = low_bound;
13318 697822 : if (index_type)
13319 697822 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13320 :
13321 697822 : offset_int max_index = index;
13322 697822 : unsigned cnt;
13323 697822 : tree cfield, cval;
13324 697822 : bool first_p = true;
13325 :
13326 14046370 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13327 : {
13328 : /* Array constructor might explicitly set index, or specify a range,
13329 : or leave index NULL meaning that it is next index after previous
13330 : one. */
13331 14045308 : if (cfield)
13332 : {
13333 5994593 : if (TREE_CODE (cfield) == INTEGER_CST)
13334 11988046 : max_index = index
13335 5994023 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13336 : else
13337 : {
13338 570 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13339 570 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13340 : index_sgn);
13341 570 : max_index
13342 570 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13343 : index_sgn);
13344 570 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13345 : }
13346 : }
13347 8050715 : else if (!first_p)
13348 : {
13349 7840369 : index = max_index + 1;
13350 7840369 : if (index_type)
13351 7840369 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13352 7840369 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13353 7840369 : max_index = index;
13354 : }
13355 : else
13356 : first_p = false;
13357 :
13358 14045308 : if (TREE_CODE (cval) == RAW_DATA_CST)
13359 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13360 :
13361 : /* Do we have match? */
13362 14045308 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13363 : {
13364 14045020 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13365 : {
13366 696644 : if (ctor_idx)
13367 696644 : *ctor_idx = cnt;
13368 : return cval;
13369 : }
13370 : }
13371 288 : else if (in_gimple_form)
13372 : /* We're past the element we search for. Note during parsing
13373 : the elements might not be sorted.
13374 : ??? We should use a binary search and a flag on the
13375 : CONSTRUCTOR as to whether elements are sorted in declaration
13376 : order. */
13377 : break;
13378 : }
13379 1178 : if (ctor_idx)
13380 1178 : *ctor_idx = cnt;
13381 : return NULL_TREE;
13382 : }
13383 :
13384 : /* Perform constant folding and related simplification of EXPR.
13385 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13386 : and application of the associative law.
13387 : NOP_EXPR conversions may be removed freely (as long as we
13388 : are careful not to change the type of the overall expression).
13389 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13390 : but we can constant-fold them if they have constant operands. */
13391 :
13392 : #ifdef ENABLE_FOLD_CHECKING
13393 : # define fold(x) fold_1 (x)
13394 : static tree fold_1 (tree);
13395 : static
13396 : #endif
13397 : tree
13398 1395026593 : fold (tree expr)
13399 : {
13400 1395199010 : const tree t = expr;
13401 1395199010 : enum tree_code code = TREE_CODE (t);
13402 1395199010 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13403 1395199010 : tree tem;
13404 1395199010 : location_t loc = EXPR_LOCATION (expr);
13405 :
13406 : /* Return right away if a constant. */
13407 1395199010 : if (kind == tcc_constant)
13408 : return t;
13409 :
13410 : /* CALL_EXPR-like objects with variable numbers of operands are
13411 : treated specially. */
13412 1292655871 : if (kind == tcc_vl_exp)
13413 : {
13414 183246870 : if (code == CALL_EXPR)
13415 : {
13416 183246342 : tem = fold_call_expr (loc, expr, false);
13417 183246342 : return tem ? tem : expr;
13418 : }
13419 : return expr;
13420 : }
13421 :
13422 1109409001 : if (IS_EXPR_CODE_CLASS (kind))
13423 : {
13424 1107235073 : tree type = TREE_TYPE (t);
13425 1107235073 : tree op0, op1, op2;
13426 :
13427 1107235073 : switch (TREE_CODE_LENGTH (code))
13428 : {
13429 1005427779 : case 1:
13430 1005427779 : op0 = TREE_OPERAND (t, 0);
13431 1005427779 : tem = fold_unary_loc (loc, code, type, op0);
13432 1005427779 : return tem ? tem : expr;
13433 92569855 : case 2:
13434 92569855 : op0 = TREE_OPERAND (t, 0);
13435 92569855 : op1 = TREE_OPERAND (t, 1);
13436 92569855 : tem = fold_binary_loc (loc, code, type, op0, op1);
13437 92569855 : return tem ? tem : expr;
13438 4435834 : case 3:
13439 4435834 : op0 = TREE_OPERAND (t, 0);
13440 4435834 : op1 = TREE_OPERAND (t, 1);
13441 4435834 : op2 = TREE_OPERAND (t, 2);
13442 4435834 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13443 4435834 : return tem ? tem : expr;
13444 : default:
13445 : break;
13446 : }
13447 : }
13448 :
13449 6975533 : switch (code)
13450 : {
13451 4697393 : case ARRAY_REF:
13452 4697393 : {
13453 4697393 : tree op0 = TREE_OPERAND (t, 0);
13454 4697393 : tree op1 = TREE_OPERAND (t, 1);
13455 :
13456 4697393 : if (TREE_CODE (op1) == INTEGER_CST
13457 2938079 : && TREE_CODE (op0) == CONSTRUCTOR
13458 4698848 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13459 : {
13460 1455 : unsigned int idx;
13461 1455 : tree val
13462 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13463 : &idx);
13464 1455 : if (val)
13465 : {
13466 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13467 : return val;
13468 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13469 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13470 : != INTEGER_CST))
13471 : return t;
13472 2 : offset_int o
13473 2 : = (wi::to_offset (op1)
13474 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13475 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13476 2 : return build_int_cst (TREE_TYPE (val),
13477 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13478 : }
13479 : }
13480 :
13481 : return t;
13482 : }
13483 :
13484 : /* Return a VECTOR_CST if possible. */
13485 207060 : case CONSTRUCTOR:
13486 207060 : {
13487 207060 : tree type = TREE_TYPE (t);
13488 207060 : if (TREE_CODE (type) != VECTOR_TYPE)
13489 : return t;
13490 :
13491 : unsigned i;
13492 : tree val;
13493 364040 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13494 314933 : if (! CONSTANT_CLASS_P (val))
13495 : return t;
13496 :
13497 49107 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13498 : }
13499 :
13500 172417 : case CONST_DECL:
13501 172417 : return fold (DECL_INITIAL (t));
13502 :
13503 : default:
13504 : return t;
13505 : } /* switch (code) */
13506 : }
13507 :
13508 : #ifdef ENABLE_FOLD_CHECKING
13509 : #undef fold
13510 :
13511 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13512 : hash_table<nofree_ptr_hash<const tree_node> > *);
13513 : static void fold_check_failed (const_tree, const_tree);
13514 : void print_fold_checksum (const_tree);
13515 :
13516 : /* When --enable-checking=fold, compute a digest of expr before
13517 : and after actual fold call to see if fold did not accidentally
13518 : change original expr. */
13519 :
13520 : tree
13521 : fold (tree expr)
13522 : {
13523 : tree ret;
13524 : struct md5_ctx ctx;
13525 : unsigned char checksum_before[16], checksum_after[16];
13526 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13527 :
13528 : md5_init_ctx (&ctx);
13529 : fold_checksum_tree (expr, &ctx, &ht);
13530 : md5_finish_ctx (&ctx, checksum_before);
13531 : ht.empty ();
13532 :
13533 : ret = fold_1 (expr);
13534 :
13535 : md5_init_ctx (&ctx);
13536 : fold_checksum_tree (expr, &ctx, &ht);
13537 : md5_finish_ctx (&ctx, checksum_after);
13538 :
13539 : if (memcmp (checksum_before, checksum_after, 16))
13540 : fold_check_failed (expr, ret);
13541 :
13542 : return ret;
13543 : }
13544 :
13545 : void
13546 : print_fold_checksum (const_tree expr)
13547 : {
13548 : struct md5_ctx ctx;
13549 : unsigned char checksum[16], cnt;
13550 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13551 :
13552 : md5_init_ctx (&ctx);
13553 : fold_checksum_tree (expr, &ctx, &ht);
13554 : md5_finish_ctx (&ctx, checksum);
13555 : for (cnt = 0; cnt < 16; ++cnt)
13556 : fprintf (stderr, "%02x", checksum[cnt]);
13557 : putc ('\n', stderr);
13558 : }
13559 :
13560 : static void
13561 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13562 : {
13563 : internal_error ("fold check: original tree changed by fold");
13564 : }
13565 :
13566 : static void
13567 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13568 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13569 : {
13570 : const tree_node **slot;
13571 : enum tree_code code;
13572 : union tree_node *buf;
13573 : int i, len;
13574 :
13575 : recursive_label:
13576 : if (expr == NULL)
13577 : return;
13578 : slot = ht->find_slot (expr, INSERT);
13579 : if (*slot != NULL)
13580 : return;
13581 : *slot = expr;
13582 : code = TREE_CODE (expr);
13583 : if (TREE_CODE_CLASS (code) == tcc_declaration
13584 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13585 : {
13586 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13587 : size_t sz = tree_size (expr);
13588 : buf = XALLOCAVAR (union tree_node, sz);
13589 : memcpy ((char *) buf, expr, sz);
13590 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13591 : buf->decl_with_vis.symtab_node = NULL;
13592 : buf->base.nowarning_flag = 0;
13593 : expr = (tree) buf;
13594 : }
13595 : else if (TREE_CODE_CLASS (code) == tcc_type
13596 : && (TYPE_POINTER_TO (expr)
13597 : || TYPE_REFERENCE_TO (expr)
13598 : || TYPE_CACHED_VALUES_P (expr)
13599 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13600 : || TYPE_NEXT_VARIANT (expr)
13601 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13602 : {
13603 : /* Allow these fields to be modified. */
13604 : tree tmp;
13605 : size_t sz = tree_size (expr);
13606 : buf = XALLOCAVAR (union tree_node, sz);
13607 : memcpy ((char *) buf, expr, sz);
13608 : expr = tmp = (tree) buf;
13609 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13610 : TYPE_POINTER_TO (tmp) = NULL;
13611 : TYPE_REFERENCE_TO (tmp) = NULL;
13612 : TYPE_NEXT_VARIANT (tmp) = NULL;
13613 : TYPE_ALIAS_SET (tmp) = -1;
13614 : if (TYPE_CACHED_VALUES_P (tmp))
13615 : {
13616 : TYPE_CACHED_VALUES_P (tmp) = 0;
13617 : TYPE_CACHED_VALUES (tmp) = NULL;
13618 : }
13619 : }
13620 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13621 : {
13622 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13623 : that and change builtins.cc etc. instead - see PR89543. */
13624 : size_t sz = tree_size (expr);
13625 : buf = XALLOCAVAR (union tree_node, sz);
13626 : memcpy ((char *) buf, expr, sz);
13627 : buf->base.nowarning_flag = 0;
13628 : expr = (tree) buf;
13629 : }
13630 : md5_process_bytes (expr, tree_size (expr), ctx);
13631 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13632 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13633 : if (TREE_CODE_CLASS (code) != tcc_type
13634 : && TREE_CODE_CLASS (code) != tcc_declaration
13635 : && code != TREE_LIST
13636 : && code != SSA_NAME
13637 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13638 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13639 : switch (TREE_CODE_CLASS (code))
13640 : {
13641 : case tcc_constant:
13642 : switch (code)
13643 : {
13644 : case STRING_CST:
13645 : md5_process_bytes (TREE_STRING_POINTER (expr),
13646 : TREE_STRING_LENGTH (expr), ctx);
13647 : break;
13648 : case COMPLEX_CST:
13649 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13650 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13651 : break;
13652 : case VECTOR_CST:
13653 : len = vector_cst_encoded_nelts (expr);
13654 : for (i = 0; i < len; ++i)
13655 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13656 : break;
13657 : default:
13658 : break;
13659 : }
13660 : break;
13661 : case tcc_exceptional:
13662 : switch (code)
13663 : {
13664 : case TREE_LIST:
13665 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13666 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13667 : expr = TREE_CHAIN (expr);
13668 : goto recursive_label;
13669 : break;
13670 : case TREE_VEC:
13671 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13672 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13673 : break;
13674 : default:
13675 : break;
13676 : }
13677 : break;
13678 : case tcc_expression:
13679 : case tcc_reference:
13680 : case tcc_comparison:
13681 : case tcc_unary:
13682 : case tcc_binary:
13683 : case tcc_statement:
13684 : case tcc_vl_exp:
13685 : len = TREE_OPERAND_LENGTH (expr);
13686 : for (i = 0; i < len; ++i)
13687 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13688 : break;
13689 : case tcc_declaration:
13690 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13691 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13692 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13693 : {
13694 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13695 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13696 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13697 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13698 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13699 : }
13700 :
13701 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13702 : {
13703 : if (TREE_CODE (expr) == FUNCTION_DECL)
13704 : {
13705 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13706 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13707 : }
13708 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13709 : }
13710 : break;
13711 : case tcc_type:
13712 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13713 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13714 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13715 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13716 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13717 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13718 : if (INTEGRAL_TYPE_P (expr)
13719 : || SCALAR_FLOAT_TYPE_P (expr))
13720 : {
13721 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13722 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13723 : }
13724 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13725 : if (RECORD_OR_UNION_TYPE_P (expr))
13726 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13727 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13728 : break;
13729 : default:
13730 : break;
13731 : }
13732 : }
13733 :
13734 : /* Helper function for outputting the checksum of a tree T. When
13735 : debugging with gdb, you can "define mynext" to be "next" followed
13736 : by "call debug_fold_checksum (op0)", then just trace down till the
13737 : outputs differ. */
13738 :
13739 : DEBUG_FUNCTION void
13740 : debug_fold_checksum (const_tree t)
13741 : {
13742 : int i;
13743 : unsigned char checksum[16];
13744 : struct md5_ctx ctx;
13745 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13746 :
13747 : md5_init_ctx (&ctx);
13748 : fold_checksum_tree (t, &ctx, &ht);
13749 : md5_finish_ctx (&ctx, checksum);
13750 : ht.empty ();
13751 :
13752 : for (i = 0; i < 16; i++)
13753 : fprintf (stderr, "%d ", checksum[i]);
13754 :
13755 : fprintf (stderr, "\n");
13756 : }
13757 :
13758 : #endif
13759 :
13760 : /* Fold a unary tree expression with code CODE of type TYPE with an
13761 : operand OP0. LOC is the location of the resulting expression.
13762 : Return a folded expression if successful. Otherwise, return a tree
13763 : expression with code CODE of type TYPE with an operand OP0. */
13764 :
13765 : tree
13766 1070354929 : fold_build1_loc (location_t loc,
13767 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13768 : {
13769 1070354929 : tree tem;
13770 : #ifdef ENABLE_FOLD_CHECKING
13771 : unsigned char checksum_before[16], checksum_after[16];
13772 : struct md5_ctx ctx;
13773 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13774 :
13775 : md5_init_ctx (&ctx);
13776 : fold_checksum_tree (op0, &ctx, &ht);
13777 : md5_finish_ctx (&ctx, checksum_before);
13778 : ht.empty ();
13779 : #endif
13780 :
13781 1070354929 : tem = fold_unary_loc (loc, code, type, op0);
13782 1070354929 : if (!tem)
13783 545118240 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13784 :
13785 : #ifdef ENABLE_FOLD_CHECKING
13786 : md5_init_ctx (&ctx);
13787 : fold_checksum_tree (op0, &ctx, &ht);
13788 : md5_finish_ctx (&ctx, checksum_after);
13789 :
13790 : if (memcmp (checksum_before, checksum_after, 16))
13791 : fold_check_failed (op0, tem);
13792 : #endif
13793 1070354929 : return tem;
13794 : }
13795 :
13796 : /* Fold a binary tree expression with code CODE of type TYPE with
13797 : operands OP0 and OP1. LOC is the location of the resulting
13798 : expression. Return a folded expression if successful. Otherwise,
13799 : return a tree expression with code CODE of type TYPE with operands
13800 : OP0 and OP1. */
13801 :
13802 : tree
13803 686337358 : fold_build2_loc (location_t loc,
13804 : enum tree_code code, tree type, tree op0, tree op1
13805 : MEM_STAT_DECL)
13806 : {
13807 686337358 : tree tem;
13808 : #ifdef ENABLE_FOLD_CHECKING
13809 : unsigned char checksum_before_op0[16],
13810 : checksum_before_op1[16],
13811 : checksum_after_op0[16],
13812 : checksum_after_op1[16];
13813 : struct md5_ctx ctx;
13814 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13815 :
13816 : md5_init_ctx (&ctx);
13817 : fold_checksum_tree (op0, &ctx, &ht);
13818 : md5_finish_ctx (&ctx, checksum_before_op0);
13819 : ht.empty ();
13820 :
13821 : md5_init_ctx (&ctx);
13822 : fold_checksum_tree (op1, &ctx, &ht);
13823 : md5_finish_ctx (&ctx, checksum_before_op1);
13824 : ht.empty ();
13825 : #endif
13826 :
13827 686337358 : tem = fold_binary_loc (loc, code, type, op0, op1);
13828 686337358 : if (!tem)
13829 386683554 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13830 :
13831 : #ifdef ENABLE_FOLD_CHECKING
13832 : md5_init_ctx (&ctx);
13833 : fold_checksum_tree (op0, &ctx, &ht);
13834 : md5_finish_ctx (&ctx, checksum_after_op0);
13835 : ht.empty ();
13836 :
13837 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13838 : fold_check_failed (op0, tem);
13839 :
13840 : md5_init_ctx (&ctx);
13841 : fold_checksum_tree (op1, &ctx, &ht);
13842 : md5_finish_ctx (&ctx, checksum_after_op1);
13843 :
13844 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13845 : fold_check_failed (op1, tem);
13846 : #endif
13847 686337358 : return tem;
13848 : }
13849 :
13850 : /* Fold a ternary tree expression with code CODE of type TYPE with
13851 : operands OP0, OP1, and OP2. Return a folded expression if
13852 : successful. Otherwise, return a tree expression with code CODE of
13853 : type TYPE with operands OP0, OP1, and OP2. */
13854 :
13855 : tree
13856 41149936 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13857 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13858 : {
13859 41149936 : tree tem;
13860 : #ifdef ENABLE_FOLD_CHECKING
13861 : unsigned char checksum_before_op0[16],
13862 : checksum_before_op1[16],
13863 : checksum_before_op2[16],
13864 : checksum_after_op0[16],
13865 : checksum_after_op1[16],
13866 : checksum_after_op2[16];
13867 : struct md5_ctx ctx;
13868 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13869 :
13870 : md5_init_ctx (&ctx);
13871 : fold_checksum_tree (op0, &ctx, &ht);
13872 : md5_finish_ctx (&ctx, checksum_before_op0);
13873 : ht.empty ();
13874 :
13875 : md5_init_ctx (&ctx);
13876 : fold_checksum_tree (op1, &ctx, &ht);
13877 : md5_finish_ctx (&ctx, checksum_before_op1);
13878 : ht.empty ();
13879 :
13880 : md5_init_ctx (&ctx);
13881 : fold_checksum_tree (op2, &ctx, &ht);
13882 : md5_finish_ctx (&ctx, checksum_before_op2);
13883 : ht.empty ();
13884 : #endif
13885 :
13886 41149936 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13887 41149936 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13888 41149936 : if (!tem)
13889 38258177 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13890 :
13891 : #ifdef ENABLE_FOLD_CHECKING
13892 : md5_init_ctx (&ctx);
13893 : fold_checksum_tree (op0, &ctx, &ht);
13894 : md5_finish_ctx (&ctx, checksum_after_op0);
13895 : ht.empty ();
13896 :
13897 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13898 : fold_check_failed (op0, tem);
13899 :
13900 : md5_init_ctx (&ctx);
13901 : fold_checksum_tree (op1, &ctx, &ht);
13902 : md5_finish_ctx (&ctx, checksum_after_op1);
13903 : ht.empty ();
13904 :
13905 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13906 : fold_check_failed (op1, tem);
13907 :
13908 : md5_init_ctx (&ctx);
13909 : fold_checksum_tree (op2, &ctx, &ht);
13910 : md5_finish_ctx (&ctx, checksum_after_op2);
13911 :
13912 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13913 : fold_check_failed (op2, tem);
13914 : #endif
13915 41149936 : return tem;
13916 : }
13917 :
13918 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13919 : arguments in ARGARRAY, and a null static chain.
13920 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13921 : of type TYPE from the given operands as constructed by build_call_array. */
13922 :
13923 : tree
13924 58054028 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13925 : int nargs, tree *argarray)
13926 : {
13927 58054028 : tree tem;
13928 : #ifdef ENABLE_FOLD_CHECKING
13929 : unsigned char checksum_before_fn[16],
13930 : checksum_before_arglist[16],
13931 : checksum_after_fn[16],
13932 : checksum_after_arglist[16];
13933 : struct md5_ctx ctx;
13934 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13935 : int i;
13936 :
13937 : md5_init_ctx (&ctx);
13938 : fold_checksum_tree (fn, &ctx, &ht);
13939 : md5_finish_ctx (&ctx, checksum_before_fn);
13940 : ht.empty ();
13941 :
13942 : md5_init_ctx (&ctx);
13943 : for (i = 0; i < nargs; i++)
13944 : fold_checksum_tree (argarray[i], &ctx, &ht);
13945 : md5_finish_ctx (&ctx, checksum_before_arglist);
13946 : ht.empty ();
13947 : #endif
13948 :
13949 58054028 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13950 58054028 : if (!tem)
13951 55919204 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13952 :
13953 : #ifdef ENABLE_FOLD_CHECKING
13954 : md5_init_ctx (&ctx);
13955 : fold_checksum_tree (fn, &ctx, &ht);
13956 : md5_finish_ctx (&ctx, checksum_after_fn);
13957 : ht.empty ();
13958 :
13959 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13960 : fold_check_failed (fn, tem);
13961 :
13962 : md5_init_ctx (&ctx);
13963 : for (i = 0; i < nargs; i++)
13964 : fold_checksum_tree (argarray[i], &ctx, &ht);
13965 : md5_finish_ctx (&ctx, checksum_after_arglist);
13966 :
13967 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13968 : fold_check_failed (NULL_TREE, tem);
13969 : #endif
13970 58054028 : return tem;
13971 : }
13972 :
13973 : /* Perform constant folding and related simplification of initializer
13974 : expression EXPR. These behave identically to "fold_buildN" but ignore
13975 : potential run-time traps and exceptions that fold must preserve. */
13976 :
13977 : #define START_FOLD_INIT \
13978 : int saved_signaling_nans = flag_signaling_nans;\
13979 : int saved_trapping_math = flag_trapping_math;\
13980 : int saved_rounding_math = flag_rounding_math;\
13981 : int saved_trapv = flag_trapv;\
13982 : int saved_folding_initializer = folding_initializer;\
13983 : flag_signaling_nans = 0;\
13984 : flag_trapping_math = 0;\
13985 : flag_rounding_math = 0;\
13986 : flag_trapv = 0;\
13987 : folding_initializer = 1;
13988 :
13989 : #define END_FOLD_INIT \
13990 : flag_signaling_nans = saved_signaling_nans;\
13991 : flag_trapping_math = saved_trapping_math;\
13992 : flag_rounding_math = saved_rounding_math;\
13993 : flag_trapv = saved_trapv;\
13994 : folding_initializer = saved_folding_initializer;
13995 :
13996 : tree
13997 544858 : fold_init (tree expr)
13998 : {
13999 544858 : tree result;
14000 544858 : START_FOLD_INIT;
14001 :
14002 544858 : result = fold (expr);
14003 :
14004 544858 : END_FOLD_INIT;
14005 544858 : return result;
14006 : }
14007 :
14008 : tree
14009 2989474 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14010 : tree type, tree op)
14011 : {
14012 2989474 : tree result;
14013 2989474 : START_FOLD_INIT;
14014 :
14015 2989474 : result = fold_build1_loc (loc, code, type, op);
14016 :
14017 2989474 : END_FOLD_INIT;
14018 2989474 : return result;
14019 : }
14020 :
14021 : tree
14022 50464 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14023 : tree type, tree op0, tree op1)
14024 : {
14025 50464 : tree result;
14026 50464 : START_FOLD_INIT;
14027 :
14028 50464 : result = fold_build2_loc (loc, code, type, op0, op1);
14029 :
14030 50464 : END_FOLD_INIT;
14031 50464 : return result;
14032 : }
14033 :
14034 : tree
14035 3461 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14036 : int nargs, tree *argarray)
14037 : {
14038 3461 : tree result;
14039 3461 : START_FOLD_INIT;
14040 :
14041 3461 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14042 :
14043 3461 : END_FOLD_INIT;
14044 3461 : return result;
14045 : }
14046 :
14047 : tree
14048 68471532 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14049 : tree lhs, tree rhs)
14050 : {
14051 68471532 : tree result;
14052 68471532 : START_FOLD_INIT;
14053 :
14054 68471532 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14055 :
14056 68471532 : END_FOLD_INIT;
14057 68471532 : return result;
14058 : }
14059 :
14060 : #undef START_FOLD_INIT
14061 : #undef END_FOLD_INIT
14062 :
14063 : /* Determine if first argument is a multiple of second argument. Return
14064 : false if it is not, or we cannot easily determined it to be.
14065 :
14066 : An example of the sort of thing we care about (at this point; this routine
14067 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14068 : fold cases do now) is discovering that
14069 :
14070 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14071 :
14072 : is a multiple of
14073 :
14074 : SAVE_EXPR (J * 8)
14075 :
14076 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14077 :
14078 : This code also handles discovering that
14079 :
14080 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14081 :
14082 : is a multiple of 8 so we don't have to worry about dealing with a
14083 : possible remainder.
14084 :
14085 : Note that we *look* inside a SAVE_EXPR only to determine how it was
14086 : calculated; it is not safe for fold to do much of anything else with the
14087 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14088 : at run time. For example, the latter example above *cannot* be implemented
14089 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14090 : evaluation time of the original SAVE_EXPR is not necessarily the same at
14091 : the time the new expression is evaluated. The only optimization of this
14092 : sort that would be valid is changing
14093 :
14094 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14095 :
14096 : divided by 8 to
14097 :
14098 : SAVE_EXPR (I) * SAVE_EXPR (J)
14099 :
14100 : (where the same SAVE_EXPR (J) is used in the original and the
14101 : transformed version).
14102 :
14103 : NOWRAP specifies whether all outer operations in TYPE should
14104 : be considered not wrapping. Any type conversion within TOP acts
14105 : as a barrier and we will fall back to NOWRAP being false.
14106 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14107 : as not wrapping even though they are generally using unsigned arithmetic. */
14108 :
14109 : bool
14110 1591271 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14111 : {
14112 1644431 : gimple *stmt;
14113 1644431 : tree op1, op2;
14114 :
14115 1644431 : if (operand_equal_p (top, bottom, 0))
14116 : return true;
14117 :
14118 1127241 : if (TREE_CODE (type) != INTEGER_TYPE)
14119 : return false;
14120 :
14121 1127222 : switch (TREE_CODE (top))
14122 : {
14123 702 : case BIT_AND_EXPR:
14124 : /* Bitwise and provides a power of two multiple. If the mask is
14125 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14126 702 : if (!integer_pow2p (bottom))
14127 : return false;
14128 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14129 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14130 :
14131 419939 : case MULT_EXPR:
14132 : /* If the multiplication can wrap we cannot recurse further unless
14133 : the bottom is a power of two which is where wrapping does not
14134 : matter. */
14135 419939 : if (!nowrap
14136 14673 : && !TYPE_OVERFLOW_UNDEFINED (type)
14137 424154 : && !integer_pow2p (bottom))
14138 : return false;
14139 419443 : if (TREE_CODE (bottom) == INTEGER_CST)
14140 : {
14141 417735 : op1 = TREE_OPERAND (top, 0);
14142 417735 : op2 = TREE_OPERAND (top, 1);
14143 417735 : if (TREE_CODE (op1) == INTEGER_CST)
14144 0 : std::swap (op1, op2);
14145 417735 : if (TREE_CODE (op2) == INTEGER_CST)
14146 : {
14147 407403 : if (multiple_of_p (type, op2, bottom, nowrap))
14148 : return true;
14149 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14150 3308 : if (multiple_of_p (type, bottom, op2, nowrap))
14151 : {
14152 1909 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14153 1909 : wi::to_widest (op2));
14154 1909 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14155 : {
14156 1909 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14157 1909 : return multiple_of_p (type, op1, op2, nowrap);
14158 : }
14159 1909 : }
14160 : return multiple_of_p (type, op1, bottom, nowrap);
14161 : }
14162 : }
14163 12040 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14164 12040 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14165 :
14166 400 : case LSHIFT_EXPR:
14167 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14168 400 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14169 : {
14170 400 : op1 = TREE_OPERAND (top, 1);
14171 400 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14172 : {
14173 400 : wide_int mul_op
14174 400 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14175 800 : return multiple_of_p (type,
14176 800 : wide_int_to_tree (type, mul_op), bottom,
14177 : nowrap);
14178 400 : }
14179 : }
14180 : return false;
14181 :
14182 229456 : case MINUS_EXPR:
14183 229456 : case PLUS_EXPR:
14184 : /* If the addition or subtraction can wrap we cannot recurse further
14185 : unless bottom is a power of two which is where wrapping does not
14186 : matter. */
14187 229456 : if (!nowrap
14188 177944 : && !TYPE_OVERFLOW_UNDEFINED (type)
14189 405948 : && !integer_pow2p (bottom))
14190 : return false;
14191 :
14192 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14193 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14194 : but 0xfffffffd is not. */
14195 199979 : op1 = TREE_OPERAND (top, 1);
14196 199979 : if (TREE_CODE (top) == PLUS_EXPR
14197 193795 : && nowrap
14198 45414 : && TYPE_UNSIGNED (type)
14199 244657 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14200 28266 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14201 :
14202 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14203 : precisely, so be conservative here checking if both op0 and op1
14204 : are multiple of bottom. Note we check the second operand first
14205 : since it's usually simpler. */
14206 199979 : return (multiple_of_p (type, op1, bottom, nowrap)
14207 199979 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14208 :
14209 147436 : CASE_CONVERT:
14210 : /* Can't handle conversions from non-integral or wider integral type. */
14211 147436 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14212 147436 : || (TYPE_PRECISION (type)
14213 38856 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14214 : return false;
14215 : /* NOWRAP only extends to operations in the outermost type so
14216 : make sure to strip it off here. */
14217 38594 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14218 77188 : TREE_OPERAND (top, 0), bottom, false);
14219 :
14220 13167 : case SAVE_EXPR:
14221 13167 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14222 :
14223 86 : case COND_EXPR:
14224 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14225 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14226 :
14227 142918 : case INTEGER_CST:
14228 142918 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14229 : return false;
14230 140199 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14231 : SIGNED);
14232 :
14233 60562 : case SSA_NAME:
14234 60562 : if (TREE_CODE (bottom) == INTEGER_CST
14235 57422 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14236 117984 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14237 : {
14238 23565 : enum tree_code code = gimple_assign_rhs_code (stmt);
14239 :
14240 : /* Check for special cases to see if top is defined as multiple
14241 : of bottom:
14242 :
14243 : top = (X & ~(bottom - 1) ; bottom is power of 2
14244 :
14245 : or
14246 :
14247 : Y = X % bottom
14248 : top = X - Y. */
14249 23565 : if (code == BIT_AND_EXPR
14250 628 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14251 628 : && TREE_CODE (op2) == INTEGER_CST
14252 520 : && integer_pow2p (bottom)
14253 24085 : && wi::multiple_of_p (wi::to_widest (op2),
14254 520 : wi::to_widest (bottom), SIGNED))
14255 511 : return true;
14256 :
14257 23054 : op1 = gimple_assign_rhs1 (stmt);
14258 23054 : if (code == MINUS_EXPR
14259 1904 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14260 1904 : && TREE_CODE (op2) == SSA_NAME
14261 1904 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14262 1904 : && gimple_code (stmt) == GIMPLE_ASSIGN
14263 1542 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14264 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14265 23118 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14266 : return true;
14267 : }
14268 :
14269 : /* fall through */
14270 :
14271 : default:
14272 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14273 : return multiple_p (wi::to_poly_widest (top),
14274 : wi::to_poly_widest (bottom));
14275 :
14276 : return false;
14277 : }
14278 : }
14279 :
14280 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14281 : This function returns true for integer expressions, and returns
14282 : false if uncertain. */
14283 :
14284 : bool
14285 643879 : tree_expr_finite_p (const_tree x)
14286 : {
14287 643883 : machine_mode mode = element_mode (x);
14288 643883 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14289 : return true;
14290 643581 : switch (TREE_CODE (x))
14291 : {
14292 760 : case REAL_CST:
14293 760 : return real_isfinite (TREE_REAL_CST_PTR (x));
14294 0 : case COMPLEX_CST:
14295 0 : return tree_expr_finite_p (TREE_REALPART (x))
14296 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14297 : case FLOAT_EXPR:
14298 : return true;
14299 4 : case ABS_EXPR:
14300 4 : case CONVERT_EXPR:
14301 4 : case NON_LVALUE_EXPR:
14302 4 : case NEGATE_EXPR:
14303 4 : case SAVE_EXPR:
14304 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14305 0 : case MIN_EXPR:
14306 0 : case MAX_EXPR:
14307 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14308 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14309 0 : case COND_EXPR:
14310 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14311 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14312 38 : case CALL_EXPR:
14313 38 : switch (get_call_combined_fn (x))
14314 : {
14315 0 : CASE_CFN_FABS:
14316 0 : CASE_CFN_FABS_FN:
14317 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14318 0 : CASE_CFN_FMAX:
14319 0 : CASE_CFN_FMAX_FN:
14320 0 : CASE_CFN_FMIN:
14321 0 : CASE_CFN_FMIN_FN:
14322 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14323 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14324 : default:
14325 : return false;
14326 : }
14327 :
14328 : default:
14329 : return false;
14330 : }
14331 : }
14332 :
14333 : /* Return true if expression X evaluates to an infinity.
14334 : This function returns false for integer expressions. */
14335 :
14336 : bool
14337 1341884 : tree_expr_infinite_p (const_tree x)
14338 : {
14339 1342334 : if (!HONOR_INFINITIES (x))
14340 : return false;
14341 1342089 : switch (TREE_CODE (x))
14342 : {
14343 0 : case REAL_CST:
14344 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14345 450 : case ABS_EXPR:
14346 450 : case NEGATE_EXPR:
14347 450 : case NON_LVALUE_EXPR:
14348 450 : case SAVE_EXPR:
14349 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14350 0 : case COND_EXPR:
14351 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14352 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14353 : default:
14354 : return false;
14355 : }
14356 : }
14357 :
14358 : /* Return true if expression X could evaluate to an infinity.
14359 : This function returns false for integer expressions, and returns
14360 : true if uncertain. */
14361 :
14362 : bool
14363 818999 : tree_expr_maybe_infinite_p (const_tree x)
14364 : {
14365 819007 : if (!HONOR_INFINITIES (x))
14366 : return false;
14367 818551 : switch (TREE_CODE (x))
14368 : {
14369 302 : case REAL_CST:
14370 302 : return real_isinf (TREE_REAL_CST_PTR (x));
14371 : case FLOAT_EXPR:
14372 : return false;
14373 8 : case ABS_EXPR:
14374 8 : case NEGATE_EXPR:
14375 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14376 1 : case COND_EXPR:
14377 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14378 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14379 : default:
14380 : return true;
14381 : }
14382 : }
14383 :
14384 : /* Return true if expression X evaluates to a signaling NaN.
14385 : This function returns false for integer expressions. */
14386 :
14387 : bool
14388 421 : tree_expr_signaling_nan_p (const_tree x)
14389 : {
14390 421 : if (!HONOR_SNANS (x))
14391 : return false;
14392 116 : switch (TREE_CODE (x))
14393 : {
14394 116 : case REAL_CST:
14395 116 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14396 0 : case NON_LVALUE_EXPR:
14397 0 : case SAVE_EXPR:
14398 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14399 0 : case COND_EXPR:
14400 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14401 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14402 : default:
14403 : return false;
14404 : }
14405 : }
14406 :
14407 : /* Return true if expression X could evaluate to a signaling NaN.
14408 : This function returns false for integer expressions, and returns
14409 : true if uncertain. */
14410 :
14411 : bool
14412 721876 : tree_expr_maybe_signaling_nan_p (const_tree x)
14413 : {
14414 721876 : if (!HONOR_SNANS (x))
14415 : return false;
14416 4892 : switch (TREE_CODE (x))
14417 : {
14418 1452 : case REAL_CST:
14419 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14420 : case FLOAT_EXPR:
14421 : return false;
14422 0 : case ABS_EXPR:
14423 0 : case CONVERT_EXPR:
14424 0 : case NEGATE_EXPR:
14425 0 : case NON_LVALUE_EXPR:
14426 0 : case SAVE_EXPR:
14427 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14428 0 : case MIN_EXPR:
14429 0 : case MAX_EXPR:
14430 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14431 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14432 0 : case COND_EXPR:
14433 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14434 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14435 0 : case CALL_EXPR:
14436 0 : switch (get_call_combined_fn (x))
14437 : {
14438 0 : CASE_CFN_FABS:
14439 0 : CASE_CFN_FABS_FN:
14440 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14441 0 : CASE_CFN_FMAX:
14442 0 : CASE_CFN_FMAX_FN:
14443 0 : CASE_CFN_FMIN:
14444 0 : CASE_CFN_FMIN_FN:
14445 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14446 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14447 : default:
14448 : return true;
14449 : }
14450 : default:
14451 : return true;
14452 : }
14453 : }
14454 :
14455 : /* Return true if expression X evaluates to a NaN.
14456 : This function returns false for integer expressions. */
14457 :
14458 : bool
14459 3953424 : tree_expr_nan_p (const_tree x)
14460 : {
14461 4311026 : if (!HONOR_NANS (x))
14462 : return false;
14463 4310676 : switch (TREE_CODE (x))
14464 : {
14465 3806 : case REAL_CST:
14466 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14467 357602 : case NON_LVALUE_EXPR:
14468 357602 : case SAVE_EXPR:
14469 357602 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14470 956 : case COND_EXPR:
14471 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14472 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14473 : default:
14474 : return false;
14475 : }
14476 : }
14477 :
14478 : /* Return true if expression X could evaluate to a NaN.
14479 : This function returns false for integer expressions, and returns
14480 : true if uncertain. */
14481 :
14482 : bool
14483 5185322 : tree_expr_maybe_nan_p (const_tree x)
14484 : {
14485 7590739 : if (!HONOR_NANS (x))
14486 : return false;
14487 7509718 : switch (TREE_CODE (x))
14488 : {
14489 3454 : case REAL_CST:
14490 3454 : return real_isnan (TREE_REAL_CST_PTR (x));
14491 : case FLOAT_EXPR:
14492 : return false;
14493 14832 : case PLUS_EXPR:
14494 14832 : case MINUS_EXPR:
14495 14832 : case MULT_EXPR:
14496 14832 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14497 14832 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14498 2405417 : case ABS_EXPR:
14499 2405417 : case CONVERT_EXPR:
14500 2405417 : case NEGATE_EXPR:
14501 2405417 : case NON_LVALUE_EXPR:
14502 2405417 : case SAVE_EXPR:
14503 2405417 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14504 176 : case MIN_EXPR:
14505 176 : case MAX_EXPR:
14506 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14507 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14508 603 : case COND_EXPR:
14509 603 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14510 603 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14511 1085 : case CALL_EXPR:
14512 1085 : switch (get_call_combined_fn (x))
14513 : {
14514 0 : CASE_CFN_FABS:
14515 0 : CASE_CFN_FABS_FN:
14516 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14517 108 : CASE_CFN_FMAX:
14518 108 : CASE_CFN_FMAX_FN:
14519 108 : CASE_CFN_FMIN:
14520 108 : CASE_CFN_FMIN_FN:
14521 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14522 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14523 : default:
14524 : return true;
14525 : }
14526 : default:
14527 : return true;
14528 : }
14529 : }
14530 :
14531 : /* Return true if expression X could evaluate to -0.0.
14532 : This function returns true if uncertain. */
14533 :
14534 : bool
14535 594323 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14536 : {
14537 594323 : if (!HONOR_SIGNED_ZEROS (x))
14538 : return false;
14539 594321 : switch (TREE_CODE (x))
14540 : {
14541 0 : case REAL_CST:
14542 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14543 : case INTEGER_CST:
14544 : case FLOAT_EXPR:
14545 : case ABS_EXPR:
14546 : return false;
14547 0 : case NON_LVALUE_EXPR:
14548 0 : case SAVE_EXPR:
14549 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14550 1 : case COND_EXPR:
14551 1 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14552 1 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14553 2 : case CALL_EXPR:
14554 2 : switch (get_call_combined_fn (x))
14555 : {
14556 : CASE_CFN_FABS:
14557 : CASE_CFN_FABS_FN:
14558 : return false;
14559 : default:
14560 : break;
14561 : }
14562 : default:
14563 : break;
14564 : }
14565 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14566 : * but currently those predicates require tree and not const_tree. */
14567 : return true;
14568 : }
14569 :
14570 : #define tree_expr_nonnegative_p(X, Y) \
14571 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14572 :
14573 : #define RECURSE(X) \
14574 : ((tree_expr_nonnegative_p) (X, depth + 1))
14575 :
14576 : /* Return true if CODE or TYPE is known to be non-negative. */
14577 :
14578 : static bool
14579 27091781 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14580 : {
14581 27091781 : if (!VECTOR_TYPE_P (type)
14582 27054071 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14583 54145587 : && truth_value_p (code))
14584 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14585 : have a signed:1 type (where the value is -1 and 0). */
14586 : return true;
14587 : return false;
14588 : }
14589 :
14590 : /* Return true if (CODE OP0) is known to be non-negative.
14591 : DEPTH is the current nesting depth of the query. */
14592 :
14593 : bool
14594 2452352 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14595 : {
14596 2452352 : if (TYPE_UNSIGNED (type))
14597 : return true;
14598 :
14599 1990897 : switch (code)
14600 : {
14601 285940 : case ABS_EXPR:
14602 : /* We can't return 1 if flag_wrapv is set because
14603 : ABS_EXPR<INT_MIN> = INT_MIN. */
14604 285940 : if (!ANY_INTEGRAL_TYPE_P (type))
14605 : return true;
14606 1176 : if (TYPE_OVERFLOW_UNDEFINED (type))
14607 : return true;
14608 : break;
14609 :
14610 74994 : case NON_LVALUE_EXPR:
14611 74994 : case FLOAT_EXPR:
14612 74994 : case FIX_TRUNC_EXPR:
14613 74994 : return RECURSE (op0);
14614 :
14615 1591470 : CASE_CONVERT:
14616 1591470 : {
14617 1591470 : tree inner_type = TREE_TYPE (op0);
14618 1591470 : tree outer_type = type;
14619 :
14620 1591470 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14621 : {
14622 409648 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14623 409648 : return RECURSE (op0);
14624 0 : if (INTEGRAL_TYPE_P (inner_type))
14625 : {
14626 0 : if (TYPE_UNSIGNED (inner_type))
14627 : return true;
14628 0 : return RECURSE (op0);
14629 : }
14630 : }
14631 1181822 : else if (INTEGRAL_TYPE_P (outer_type))
14632 : {
14633 1181745 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14634 0 : return RECURSE (op0);
14635 1181745 : if (INTEGRAL_TYPE_P (inner_type))
14636 1175470 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14637 1175470 : && TYPE_UNSIGNED (inner_type);
14638 : }
14639 : }
14640 : break;
14641 :
14642 38493 : default:
14643 38493 : return tree_simple_nonnegative_warnv_p (code, type);
14644 : }
14645 :
14646 : /* We don't know sign of `t', so be conservative and return false. */
14647 : return false;
14648 : }
14649 :
14650 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14651 : DEPTH is the current nesting depth of the query. */
14652 :
14653 : bool
14654 4810037 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14655 : tree op1, int depth)
14656 : {
14657 4810037 : if (TYPE_UNSIGNED (type))
14658 : return true;
14659 :
14660 4505367 : switch (code)
14661 : {
14662 1079690 : case POINTER_PLUS_EXPR:
14663 1079690 : case PLUS_EXPR:
14664 1079690 : if (FLOAT_TYPE_P (type))
14665 48177 : return RECURSE (op0) && RECURSE (op1);
14666 :
14667 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14668 : both unsigned and at least 2 bits shorter than the result. */
14669 1031513 : if (TREE_CODE (type) == INTEGER_TYPE
14670 1025441 : && TREE_CODE (op0) == NOP_EXPR
14671 16231 : && TREE_CODE (op1) == NOP_EXPR)
14672 : {
14673 201 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14674 201 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14675 201 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14676 302 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14677 : {
14678 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14679 95 : TYPE_PRECISION (inner2)) + 1;
14680 95 : return prec < TYPE_PRECISION (type);
14681 : }
14682 : }
14683 : break;
14684 :
14685 158417 : case MULT_EXPR:
14686 158417 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14687 : {
14688 : /* x * x is always non-negative for floating point x
14689 : or without overflow. */
14690 142752 : if (operand_equal_p (op0, op1, 0)
14691 142752 : || (RECURSE (op0) && RECURSE (op1)))
14692 : return true;
14693 : }
14694 :
14695 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14696 : both unsigned and their total bits is shorter than the result. */
14697 156287 : if (TREE_CODE (type) == INTEGER_TYPE
14698 86326 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14699 161 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14700 : {
14701 147 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14702 147 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14703 147 : : TREE_TYPE (op0);
14704 147 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14705 147 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14706 147 : : TREE_TYPE (op1);
14707 :
14708 147 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14709 147 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14710 :
14711 147 : if (TREE_CODE (op0) == INTEGER_CST)
14712 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14713 :
14714 147 : if (TREE_CODE (op1) == INTEGER_CST)
14715 70 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14716 :
14717 147 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14718 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14719 : {
14720 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14721 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14722 0 : : TYPE_PRECISION (inner0);
14723 :
14724 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14725 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14726 0 : : TYPE_PRECISION (inner1);
14727 :
14728 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14729 : }
14730 : }
14731 : return false;
14732 :
14733 9835 : case BIT_AND_EXPR:
14734 9835 : return RECURSE (op0) || RECURSE (op1);
14735 :
14736 34528 : case MAX_EXPR:
14737 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14738 : things. */
14739 34528 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14740 76 : return RECURSE (op0) && RECURSE (op1);
14741 34452 : return RECURSE (op0) || RECURSE (op1);
14742 :
14743 147793 : case BIT_IOR_EXPR:
14744 147793 : case BIT_XOR_EXPR:
14745 147793 : case MIN_EXPR:
14746 147793 : case RDIV_EXPR:
14747 147793 : case TRUNC_DIV_EXPR:
14748 147793 : case CEIL_DIV_EXPR:
14749 147793 : case FLOOR_DIV_EXPR:
14750 147793 : case ROUND_DIV_EXPR:
14751 147793 : return RECURSE (op0) && RECURSE (op1);
14752 :
14753 53525 : case TRUNC_MOD_EXPR:
14754 53525 : return RECURSE (op0);
14755 :
14756 248 : case FLOOR_MOD_EXPR:
14757 248 : return RECURSE (op1);
14758 :
14759 3021331 : case CEIL_MOD_EXPR:
14760 3021331 : case ROUND_MOD_EXPR:
14761 3021331 : default:
14762 3021331 : return tree_simple_nonnegative_warnv_p (code, type);
14763 : }
14764 :
14765 : /* We don't know sign of `t', so be conservative and return false. */
14766 : return false;
14767 : }
14768 :
14769 : /* Return true if T is known to be non-negative.
14770 : DEPTH is the current nesting depth of the query. */
14771 :
14772 : bool
14773 25648799 : tree_single_nonnegative_p (tree t, int depth)
14774 : {
14775 25648799 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14776 : return true;
14777 :
14778 21620918 : switch (TREE_CODE (t))
14779 : {
14780 2457773 : case INTEGER_CST:
14781 2457773 : return tree_int_cst_sgn (t) >= 0;
14782 :
14783 927357 : case REAL_CST:
14784 927357 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14785 :
14786 0 : case FIXED_CST:
14787 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14788 :
14789 928 : case COND_EXPR:
14790 928 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14791 :
14792 8675686 : case SSA_NAME:
14793 : /* For integral types, query the range if possible. */
14794 8675686 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14795 : {
14796 7256209 : int_range_max r;
14797 14512418 : get_range_query (cfun)->range_of_expr (r, t);
14798 7256209 : if (!r.undefined_p () && !r.varying_p())
14799 : {
14800 1757621 : if (r.nonnegative_p ())
14801 : return true;
14802 1163422 : if (r.nonpositive_p () && !range_includes_zero_p (r))
14803 : return false;
14804 : }
14805 7256209 : }
14806 : /* Limit the depth of recursion to avoid quadratic behavior.
14807 : This is expected to catch almost all occurrences in practice.
14808 : If this code misses important cases that unbounded recursion
14809 : would not, passes that need this information could be revised
14810 : to provide it through dataflow propagation. */
14811 8077766 : return (!name_registered_for_update_p (t)
14812 8077765 : && depth < param_max_ssa_name_query_depth
14813 16028953 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14814 :
14815 9559174 : default:
14816 9559174 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14817 : }
14818 : }
14819 :
14820 : /* Return true if T is known to be non-negative.
14821 : DEPTH is the current nesting depth of the query. */
14822 :
14823 : bool
14824 14512465 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14825 : int depth)
14826 : {
14827 14512465 : switch (fn)
14828 : {
14829 : CASE_CFN_ACOS:
14830 : CASE_CFN_ACOS_FN:
14831 : CASE_CFN_ACOSH:
14832 : CASE_CFN_ACOSH_FN:
14833 : CASE_CFN_ACOSPI:
14834 : CASE_CFN_ACOSPI_FN:
14835 : CASE_CFN_CABS:
14836 : CASE_CFN_CABS_FN:
14837 : CASE_CFN_COSH:
14838 : CASE_CFN_COSH_FN:
14839 : CASE_CFN_ERFC:
14840 : CASE_CFN_ERFC_FN:
14841 : CASE_CFN_EXP:
14842 : CASE_CFN_EXP_FN:
14843 : CASE_CFN_EXP10:
14844 : CASE_CFN_EXP2:
14845 : CASE_CFN_EXP2_FN:
14846 : CASE_CFN_FABS:
14847 : CASE_CFN_FABS_FN:
14848 : CASE_CFN_FDIM:
14849 : CASE_CFN_FDIM_FN:
14850 : CASE_CFN_HYPOT:
14851 : CASE_CFN_HYPOT_FN:
14852 : CASE_CFN_POW10:
14853 : CASE_CFN_FFS:
14854 : CASE_CFN_PARITY:
14855 : CASE_CFN_POPCOUNT:
14856 : CASE_CFN_CLRSB:
14857 : CASE_CFN_BSWAP:
14858 : CASE_CFN_BITREVERSE:
14859 : /* Always true. */
14860 : return true;
14861 :
14862 176 : CASE_CFN_CLZ:
14863 176 : CASE_CFN_CTZ:
14864 176 : if (arg1)
14865 2 : return RECURSE (arg1);
14866 : return true;
14867 :
14868 845 : CASE_CFN_SQRT:
14869 845 : CASE_CFN_SQRT_FN:
14870 : /* sqrt(-0.0) is -0.0. */
14871 845 : if (!HONOR_SIGNED_ZEROS (type))
14872 : return true;
14873 813 : return RECURSE (arg0);
14874 :
14875 23112 : CASE_CFN_ASINH:
14876 23112 : CASE_CFN_ASINH_FN:
14877 23112 : CASE_CFN_ASINPI:
14878 23112 : CASE_CFN_ASINPI_FN:
14879 23112 : CASE_CFN_ATAN:
14880 23112 : CASE_CFN_ATAN_FN:
14881 23112 : CASE_CFN_ATANH:
14882 23112 : CASE_CFN_ATANH_FN:
14883 23112 : CASE_CFN_ATANPI:
14884 23112 : CASE_CFN_ATANPI_FN:
14885 23112 : CASE_CFN_CBRT:
14886 23112 : CASE_CFN_CBRT_FN:
14887 23112 : CASE_CFN_CEIL:
14888 23112 : CASE_CFN_CEIL_FN:
14889 23112 : CASE_CFN_ERF:
14890 23112 : CASE_CFN_ERF_FN:
14891 23112 : CASE_CFN_EXPM1:
14892 23112 : CASE_CFN_EXPM1_FN:
14893 23112 : CASE_CFN_FLOOR:
14894 23112 : CASE_CFN_FLOOR_FN:
14895 23112 : CASE_CFN_FMOD:
14896 23112 : CASE_CFN_FMOD_FN:
14897 23112 : CASE_CFN_FREXP:
14898 23112 : CASE_CFN_FREXP_FN:
14899 23112 : CASE_CFN_ICEIL:
14900 23112 : CASE_CFN_IFLOOR:
14901 23112 : CASE_CFN_IRINT:
14902 23112 : CASE_CFN_IROUND:
14903 23112 : CASE_CFN_LCEIL:
14904 23112 : CASE_CFN_LDEXP:
14905 23112 : CASE_CFN_LFLOOR:
14906 23112 : CASE_CFN_LLCEIL:
14907 23112 : CASE_CFN_LLFLOOR:
14908 23112 : CASE_CFN_LLRINT:
14909 23112 : CASE_CFN_LLRINT_FN:
14910 23112 : CASE_CFN_LLROUND:
14911 23112 : CASE_CFN_LLROUND_FN:
14912 23112 : CASE_CFN_LRINT:
14913 23112 : CASE_CFN_LRINT_FN:
14914 23112 : CASE_CFN_LROUND:
14915 23112 : CASE_CFN_LROUND_FN:
14916 23112 : CASE_CFN_MODF:
14917 23112 : CASE_CFN_MODF_FN:
14918 23112 : CASE_CFN_NEARBYINT:
14919 23112 : CASE_CFN_NEARBYINT_FN:
14920 23112 : CASE_CFN_RINT:
14921 23112 : CASE_CFN_RINT_FN:
14922 23112 : CASE_CFN_ROUND:
14923 23112 : CASE_CFN_ROUND_FN:
14924 23112 : CASE_CFN_ROUNDEVEN:
14925 23112 : CASE_CFN_ROUNDEVEN_FN:
14926 23112 : CASE_CFN_SCALB:
14927 23112 : CASE_CFN_SCALBLN:
14928 23112 : CASE_CFN_SCALBLN_FN:
14929 23112 : CASE_CFN_SCALBN:
14930 23112 : CASE_CFN_SCALBN_FN:
14931 23112 : CASE_CFN_SIGNBIT:
14932 23112 : CASE_CFN_SIGNIFICAND:
14933 23112 : CASE_CFN_SINH:
14934 23112 : CASE_CFN_SINH_FN:
14935 23112 : CASE_CFN_TANH:
14936 23112 : CASE_CFN_TANH_FN:
14937 23112 : CASE_CFN_TRUNC:
14938 23112 : CASE_CFN_TRUNC_FN:
14939 : /* True if the 1st argument is nonnegative. */
14940 23112 : return RECURSE (arg0);
14941 :
14942 1313 : CASE_CFN_FMAX:
14943 1313 : CASE_CFN_FMAX_FN:
14944 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14945 : things. In the presence of sNaNs, we're only guaranteed to be
14946 : non-negative if both operands are non-negative. In the presence
14947 : of qNaNs, we're non-negative if either operand is non-negative
14948 : and can't be a qNaN, or if both operands are non-negative. */
14949 1313 : if (tree_expr_maybe_signaling_nan_p (arg0)
14950 1313 : || tree_expr_maybe_signaling_nan_p (arg1))
14951 136 : return RECURSE (arg0) && RECURSE (arg1);
14952 1177 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14953 332 : || RECURSE (arg1))
14954 845 : : (RECURSE (arg1)
14955 845 : && !tree_expr_maybe_nan_p (arg1));
14956 :
14957 910 : CASE_CFN_FMIN:
14958 910 : CASE_CFN_FMIN_FN:
14959 : /* True if the 1st AND 2nd arguments are nonnegative. */
14960 910 : return RECURSE (arg0) && RECURSE (arg1);
14961 :
14962 789 : CASE_CFN_COPYSIGN:
14963 789 : CASE_CFN_COPYSIGN_FN:
14964 : /* True if the 2nd argument is nonnegative. */
14965 789 : return RECURSE (arg1);
14966 :
14967 2309 : CASE_CFN_POWI:
14968 : /* True if the 1st argument is nonnegative or the second
14969 : argument is an even integer. */
14970 2309 : if (TREE_CODE (arg1) == INTEGER_CST
14971 2309 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14972 : return true;
14973 2226 : return RECURSE (arg0);
14974 :
14975 5457 : CASE_CFN_POW:
14976 5457 : CASE_CFN_POW_FN:
14977 : /* True if the 1st argument is nonnegative or the second
14978 : argument is an even integer valued real. */
14979 5457 : if (TREE_CODE (arg1) == REAL_CST)
14980 : {
14981 2214 : REAL_VALUE_TYPE c;
14982 2214 : HOST_WIDE_INT n;
14983 :
14984 2214 : c = TREE_REAL_CST (arg1);
14985 2214 : n = real_to_integer (&c);
14986 2214 : if ((n & 1) == 0)
14987 : {
14988 1585 : REAL_VALUE_TYPE cint;
14989 1585 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14990 1585 : if (real_identical (&c, &cint))
14991 568 : return true;
14992 : }
14993 : }
14994 4889 : return RECURSE (arg0);
14995 :
14996 14469944 : default:
14997 14469944 : break;
14998 : }
14999 14469944 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15000 : }
15001 :
15002 : /* Return true if T is known to be non-negative.
15003 : DEPTH is the current nesting depth of the query. */
15004 :
15005 : static bool
15006 1744913 : tree_invalid_nonnegative_p (tree t, int depth)
15007 : {
15008 1744913 : enum tree_code code = TREE_CODE (t);
15009 1744913 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15010 : return true;
15011 :
15012 1326049 : switch (code)
15013 : {
15014 268 : case TARGET_EXPR:
15015 268 : {
15016 268 : tree temp = TARGET_EXPR_SLOT (t);
15017 268 : t = TARGET_EXPR_INITIAL (t);
15018 :
15019 : /* If the initializer is non-void, then it's a normal expression
15020 : that will be assigned to the slot. */
15021 268 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15022 66 : return RECURSE (t);
15023 :
15024 : /* Otherwise, the initializer sets the slot in some way. One common
15025 : way is an assignment statement at the end of the initializer. */
15026 404 : while (1)
15027 : {
15028 404 : if (TREE_CODE (t) == BIND_EXPR)
15029 202 : t = expr_last (BIND_EXPR_BODY (t));
15030 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15031 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15032 0 : t = expr_last (TREE_OPERAND (t, 0));
15033 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
15034 0 : t = expr_last (t);
15035 : else
15036 : break;
15037 : }
15038 202 : if (TREE_CODE (t) == MODIFY_EXPR
15039 202 : && TREE_OPERAND (t, 0) == temp)
15040 202 : return RECURSE (TREE_OPERAND (t, 1));
15041 :
15042 : return false;
15043 : }
15044 :
15045 645829 : case CALL_EXPR:
15046 645829 : {
15047 645829 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15048 645829 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15049 :
15050 645829 : return tree_call_nonnegative_p (TREE_TYPE (t),
15051 : get_call_combined_fn (t),
15052 : arg0,
15053 : arg1,
15054 645829 : depth);
15055 : }
15056 3390 : case COMPOUND_EXPR:
15057 3390 : case MODIFY_EXPR:
15058 3390 : return RECURSE (TREE_OPERAND (t, 1));
15059 :
15060 15 : case BIND_EXPR:
15061 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15062 :
15063 673708 : case SAVE_EXPR:
15064 673708 : return RECURSE (TREE_OPERAND (t, 0));
15065 :
15066 2839 : default:
15067 2839 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15068 : }
15069 : }
15070 :
15071 : #undef RECURSE
15072 : #undef tree_expr_nonnegative_p
15073 :
15074 : /* Return true if T is known to be non-negative.
15075 : DEPTH is the current nesting depth of the query. */
15076 :
15077 : bool
15078 27210227 : tree_expr_nonnegative_p (tree t, int depth)
15079 : {
15080 27210227 : enum tree_code code;
15081 27210227 : if (error_operand_p (t))
15082 : return false;
15083 :
15084 27210226 : code = TREE_CODE (t);
15085 27210226 : switch (TREE_CODE_CLASS (code))
15086 : {
15087 1380611 : case tcc_binary:
15088 1380611 : case tcc_comparison:
15089 1380611 : return tree_binary_nonnegative_p (TREE_CODE (t),
15090 1380611 : TREE_TYPE (t),
15091 1380611 : TREE_OPERAND (t, 0),
15092 1380611 : TREE_OPERAND (t, 1),
15093 1380611 : depth);
15094 :
15095 1984161 : case tcc_unary:
15096 1984161 : return tree_unary_nonnegative_p (TREE_CODE (t),
15097 1984161 : TREE_TYPE (t),
15098 1984161 : TREE_OPERAND (t, 0),
15099 1984161 : depth);
15100 :
15101 13605598 : case tcc_constant:
15102 13605598 : case tcc_declaration:
15103 13605598 : case tcc_reference:
15104 13605598 : return tree_single_nonnegative_p (t, depth);
15105 :
15106 10239856 : default:
15107 10239856 : break;
15108 : }
15109 :
15110 10239856 : switch (code)
15111 : {
15112 7 : case TRUTH_AND_EXPR:
15113 7 : case TRUTH_OR_EXPR:
15114 7 : case TRUTH_XOR_EXPR:
15115 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15116 7 : TREE_TYPE (t),
15117 7 : TREE_OPERAND (t, 0),
15118 7 : TREE_OPERAND (t, 1),
15119 7 : depth);
15120 72 : case TRUTH_NOT_EXPR:
15121 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15122 72 : TREE_TYPE (t),
15123 72 : TREE_OPERAND (t, 0),
15124 72 : depth);
15125 :
15126 8494864 : case COND_EXPR:
15127 8494864 : case CONSTRUCTOR:
15128 8494864 : case OBJ_TYPE_REF:
15129 8494864 : case ADDR_EXPR:
15130 8494864 : case WITH_SIZE_EXPR:
15131 8494864 : case SSA_NAME:
15132 8494864 : return tree_single_nonnegative_p (t, depth);
15133 :
15134 1744913 : default:
15135 1744913 : return tree_invalid_nonnegative_p (t, depth);
15136 : }
15137 : }
15138 :
15139 :
15140 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15141 : For floating point we further ensure that T is not denormal.
15142 : Similar logic is present in nonzero_address in rtlanal.h. */
15143 :
15144 : bool
15145 1607889 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15146 : {
15147 1607889 : switch (code)
15148 : {
15149 1 : case ABS_EXPR:
15150 1 : return tree_expr_nonzero_p (op0);
15151 :
15152 928009 : case NOP_EXPR:
15153 928009 : {
15154 928009 : tree inner_type = TREE_TYPE (op0);
15155 928009 : tree outer_type = type;
15156 :
15157 928009 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15158 928009 : && tree_expr_nonzero_p (op0));
15159 : }
15160 28119 : break;
15161 :
15162 28119 : case NON_LVALUE_EXPR:
15163 28119 : return tree_expr_nonzero_p (op0);
15164 :
15165 : default:
15166 : break;
15167 : }
15168 :
15169 : return false;
15170 : }
15171 :
15172 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15173 : For floating point we further ensure that T is not denormal.
15174 : Similar logic is present in nonzero_address in rtlanal.h. */
15175 :
15176 : bool
15177 3009419 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15178 : {
15179 3009419 : switch (code)
15180 : {
15181 467273 : case POINTER_PLUS_EXPR:
15182 467273 : case PLUS_EXPR:
15183 467273 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15184 : {
15185 : /* With the presence of negative values it is hard
15186 : to say something. */
15187 113929 : if (!tree_expr_nonnegative_p (op0)
15188 113929 : || !tree_expr_nonnegative_p (op1))
15189 : return false;
15190 : /* One of operands must be positive and the other non-negative. */
15191 3412 : return (tree_expr_nonzero_p (op0)
15192 3412 : || tree_expr_nonzero_p (op1));
15193 : }
15194 : break;
15195 :
15196 31374 : case MULT_EXPR:
15197 31374 : if (TYPE_OVERFLOW_UNDEFINED (type))
15198 : {
15199 556 : if (tree_expr_nonzero_p (op0)
15200 556 : && tree_expr_nonzero_p (op1))
15201 : return true;
15202 : }
15203 : break;
15204 :
15205 : case MIN_EXPR:
15206 : break;
15207 :
15208 43 : case MAX_EXPR:
15209 43 : if (tree_expr_nonzero_p (op0))
15210 : {
15211 :
15212 : /* When both operands are nonzero, then MAX must be too. */
15213 0 : if (tree_expr_nonzero_p (op1))
15214 : return true;
15215 :
15216 : /* MAX where operand 0 is positive is positive. */
15217 0 : return tree_expr_nonnegative_p (op0);
15218 : }
15219 : /* MAX where operand 1 is positive is positive. */
15220 43 : else if (tree_expr_nonzero_p (op1)
15221 43 : && tree_expr_nonnegative_p (op1))
15222 : return true;
15223 : break;
15224 :
15225 270466 : case BIT_IOR_EXPR:
15226 270466 : return (tree_expr_nonzero_p (op1)
15227 270466 : || tree_expr_nonzero_p (op0));
15228 :
15229 : default:
15230 : break;
15231 : }
15232 :
15233 : return false;
15234 : }
15235 :
15236 : /* Return true when T is an address and is known to be nonzero.
15237 : For floating point we further ensure that T is not denormal.
15238 : Similar logic is present in nonzero_address in rtlanal.h. */
15239 :
15240 : bool
15241 156833119 : tree_single_nonzero_p (tree t)
15242 : {
15243 156833119 : switch (TREE_CODE (t))
15244 : {
15245 1149842 : case INTEGER_CST:
15246 1149842 : return !integer_zerop (t);
15247 :
15248 12668213 : case ADDR_EXPR:
15249 12668213 : {
15250 12668213 : tree base = TREE_OPERAND (t, 0);
15251 :
15252 12668213 : if (!DECL_P (base))
15253 6352154 : base = get_base_address (base);
15254 :
15255 12668213 : if (base && TREE_CODE (base) == TARGET_EXPR)
15256 844 : base = TARGET_EXPR_SLOT (base);
15257 :
15258 844 : if (!base)
15259 : return false;
15260 :
15261 : /* For objects in symbol table check if we know they are non-zero.
15262 : Don't do anything for variables and functions before symtab is built;
15263 : it is quite possible that they will be declared weak later. */
15264 12668213 : int nonzero_addr = maybe_nonzero_address (base);
15265 12668213 : if (nonzero_addr >= 0)
15266 10239711 : return nonzero_addr;
15267 :
15268 : /* Constants are never weak. */
15269 2428502 : if (CONSTANT_CLASS_P (base))
15270 2334068 : return true;
15271 :
15272 : return false;
15273 : }
15274 :
15275 38373 : case COND_EXPR:
15276 38373 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15277 38373 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15278 : return true;
15279 : break;
15280 :
15281 130745658 : case SSA_NAME:
15282 130745658 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15283 : break;
15284 101610308 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15285 :
15286 : default:
15287 : break;
15288 : }
15289 : return false;
15290 : }
15291 :
15292 : #define integer_valued_real_p(X) \
15293 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15294 :
15295 : #define RECURSE(X) \
15296 : ((integer_valued_real_p) (X, depth + 1))
15297 :
15298 : /* Return true if the floating point result of (CODE OP0) has an
15299 : integer value. We also allow +Inf, -Inf and NaN to be considered
15300 : integer values. Return false for signaling NaN.
15301 :
15302 : DEPTH is the current nesting depth of the query. */
15303 :
15304 : bool
15305 15209 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15306 : {
15307 15209 : switch (code)
15308 : {
15309 : case FLOAT_EXPR:
15310 : return true;
15311 :
15312 1400 : case ABS_EXPR:
15313 1400 : return RECURSE (op0);
15314 :
15315 9859 : CASE_CONVERT:
15316 9859 : {
15317 9859 : tree type = TREE_TYPE (op0);
15318 9859 : if (TREE_CODE (type) == INTEGER_TYPE)
15319 : return true;
15320 9859 : if (SCALAR_FLOAT_TYPE_P (type))
15321 9859 : return RECURSE (op0);
15322 : break;
15323 : }
15324 :
15325 : default:
15326 : break;
15327 : }
15328 : return false;
15329 : }
15330 :
15331 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15332 : integer value. We also allow +Inf, -Inf and NaN to be considered
15333 : integer values. Return false for signaling NaN.
15334 :
15335 : DEPTH is the current nesting depth of the query. */
15336 :
15337 : bool
15338 13863 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15339 : {
15340 13863 : switch (code)
15341 : {
15342 8070 : case PLUS_EXPR:
15343 8070 : case MINUS_EXPR:
15344 8070 : case MULT_EXPR:
15345 8070 : case MIN_EXPR:
15346 8070 : case MAX_EXPR:
15347 8070 : return RECURSE (op0) && RECURSE (op1);
15348 :
15349 : default:
15350 : break;
15351 : }
15352 : return false;
15353 : }
15354 :
15355 : /* Return true if the floating point result of calling FNDECL with arguments
15356 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15357 : considered integer values. Return false for signaling NaN. If FNDECL
15358 : takes fewer than 2 arguments, the remaining ARGn are null.
15359 :
15360 : DEPTH is the current nesting depth of the query. */
15361 :
15362 : bool
15363 1189 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15364 : {
15365 1189 : switch (fn)
15366 : {
15367 : CASE_CFN_CEIL:
15368 : CASE_CFN_CEIL_FN:
15369 : CASE_CFN_FLOOR:
15370 : CASE_CFN_FLOOR_FN:
15371 : CASE_CFN_NEARBYINT:
15372 : CASE_CFN_NEARBYINT_FN:
15373 : CASE_CFN_RINT:
15374 : CASE_CFN_RINT_FN:
15375 : CASE_CFN_ROUND:
15376 : CASE_CFN_ROUND_FN:
15377 : CASE_CFN_ROUNDEVEN:
15378 : CASE_CFN_ROUNDEVEN_FN:
15379 : CASE_CFN_TRUNC:
15380 : CASE_CFN_TRUNC_FN:
15381 : return true;
15382 :
15383 336 : CASE_CFN_FMIN:
15384 336 : CASE_CFN_FMIN_FN:
15385 336 : CASE_CFN_FMAX:
15386 336 : CASE_CFN_FMAX_FN:
15387 336 : return RECURSE (arg0) && RECURSE (arg1);
15388 :
15389 : default:
15390 : break;
15391 : }
15392 : return false;
15393 : }
15394 :
15395 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15396 : has an integer value. We also allow +Inf, -Inf and NaN to be
15397 : considered integer values. Return false for signaling NaN.
15398 :
15399 : DEPTH is the current nesting depth of the query. */
15400 :
15401 : bool
15402 130316 : integer_valued_real_single_p (tree t, int depth)
15403 : {
15404 130316 : switch (TREE_CODE (t))
15405 : {
15406 2347 : case REAL_CST:
15407 2347 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15408 :
15409 0 : case COND_EXPR:
15410 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15411 :
15412 91470 : case SSA_NAME:
15413 : /* Limit the depth of recursion to avoid quadratic behavior.
15414 : This is expected to catch almost all occurrences in practice.
15415 : If this code misses important cases that unbounded recursion
15416 : would not, passes that need this information could be revised
15417 : to provide it through dataflow propagation. */
15418 91470 : return (!name_registered_for_update_p (t)
15419 91470 : && depth < param_max_ssa_name_query_depth
15420 182125 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15421 : depth));
15422 :
15423 : default:
15424 : break;
15425 : }
15426 : return false;
15427 : }
15428 :
15429 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15430 : has an integer value. We also allow +Inf, -Inf and NaN to be
15431 : considered integer values. Return false for signaling NaN.
15432 :
15433 : DEPTH is the current nesting depth of the query. */
15434 :
15435 : static bool
15436 0 : integer_valued_real_invalid_p (tree t, int depth)
15437 : {
15438 0 : switch (TREE_CODE (t))
15439 : {
15440 0 : case COMPOUND_EXPR:
15441 0 : case MODIFY_EXPR:
15442 0 : case BIND_EXPR:
15443 0 : return RECURSE (TREE_OPERAND (t, 1));
15444 :
15445 0 : case SAVE_EXPR:
15446 0 : return RECURSE (TREE_OPERAND (t, 0));
15447 :
15448 : default:
15449 : break;
15450 : }
15451 : return false;
15452 : }
15453 :
15454 : #undef RECURSE
15455 : #undef integer_valued_real_p
15456 :
15457 : /* Return true if the floating point expression T has an integer value.
15458 : We also allow +Inf, -Inf and NaN to be considered integer values.
15459 : Return false for signaling NaN.
15460 :
15461 : DEPTH is the current nesting depth of the query. */
15462 :
15463 : bool
15464 98362 : integer_valued_real_p (tree t, int depth)
15465 : {
15466 98362 : if (t == error_mark_node)
15467 : return false;
15468 :
15469 98362 : STRIP_ANY_LOCATION_WRAPPER (t);
15470 :
15471 98362 : tree_code code = TREE_CODE (t);
15472 98362 : switch (TREE_CODE_CLASS (code))
15473 : {
15474 0 : case tcc_binary:
15475 0 : case tcc_comparison:
15476 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15477 0 : TREE_OPERAND (t, 1), depth);
15478 :
15479 0 : case tcc_unary:
15480 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15481 :
15482 8533 : case tcc_constant:
15483 8533 : case tcc_declaration:
15484 8533 : case tcc_reference:
15485 8533 : return integer_valued_real_single_p (t, depth);
15486 :
15487 89829 : default:
15488 89829 : break;
15489 : }
15490 :
15491 89829 : switch (code)
15492 : {
15493 89829 : case COND_EXPR:
15494 89829 : case SSA_NAME:
15495 89829 : return integer_valued_real_single_p (t, depth);
15496 :
15497 0 : case CALL_EXPR:
15498 0 : {
15499 0 : tree arg0 = (call_expr_nargs (t) > 0
15500 0 : ? CALL_EXPR_ARG (t, 0)
15501 0 : : NULL_TREE);
15502 0 : tree arg1 = (call_expr_nargs (t) > 1
15503 0 : ? CALL_EXPR_ARG (t, 1)
15504 0 : : NULL_TREE);
15505 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15506 0 : arg0, arg1, depth);
15507 : }
15508 :
15509 0 : default:
15510 0 : return integer_valued_real_invalid_p (t, depth);
15511 : }
15512 : }
15513 :
15514 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15515 : attempt to fold the expression to a constant without modifying TYPE,
15516 : OP0 or OP1.
15517 :
15518 : If the expression could be simplified to a constant, then return
15519 : the constant. If the expression would not be simplified to a
15520 : constant, then return NULL_TREE. */
15521 :
15522 : tree
15523 16283202 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15524 : {
15525 16283202 : tree tem = fold_binary (code, type, op0, op1);
15526 16283202 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15527 : }
15528 :
15529 : /* Given the components of a unary expression CODE, TYPE and OP0,
15530 : attempt to fold the expression to a constant without modifying
15531 : TYPE or OP0.
15532 :
15533 : If the expression could be simplified to a constant, then return
15534 : the constant. If the expression would not be simplified to a
15535 : constant, then return NULL_TREE. */
15536 :
15537 : tree
15538 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15539 : {
15540 0 : tree tem = fold_unary (code, type, op0);
15541 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15542 : }
15543 :
15544 : /* If EXP represents referencing an element in a constant string
15545 : (either via pointer arithmetic or array indexing), return the
15546 : tree representing the value accessed, otherwise return NULL. */
15547 :
15548 : tree
15549 220307364 : fold_read_from_constant_string (tree exp)
15550 : {
15551 220307364 : if ((INDIRECT_REF_P (exp)
15552 220307345 : || TREE_CODE (exp) == ARRAY_REF)
15553 233787338 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15554 : {
15555 10238074 : tree exp1 = TREE_OPERAND (exp, 0);
15556 10238074 : tree index;
15557 10238074 : tree string;
15558 10238074 : location_t loc = EXPR_LOCATION (exp);
15559 :
15560 10238074 : if (INDIRECT_REF_P (exp))
15561 0 : string = string_constant (exp1, &index, NULL, NULL);
15562 : else
15563 : {
15564 10238074 : tree low_bound = array_ref_low_bound (exp);
15565 10238074 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15566 :
15567 : /* Optimize the special-case of a zero lower bound.
15568 :
15569 : We convert the low_bound to sizetype to avoid some problems
15570 : with constant folding. (E.g. suppose the lower bound is 1,
15571 : and its mode is QI. Without the conversion,l (ARRAY
15572 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15573 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15574 10238074 : if (! integer_zerop (low_bound))
15575 156694 : index = size_diffop_loc (loc, index,
15576 : fold_convert_loc (loc, sizetype, low_bound));
15577 :
15578 : string = exp1;
15579 : }
15580 :
15581 10238074 : scalar_int_mode char_mode;
15582 10238074 : if (string
15583 10238074 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15584 10238074 : && TREE_CODE (string) == STRING_CST
15585 261321 : && tree_fits_uhwi_p (index)
15586 257409 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15587 10495297 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15588 : &char_mode)
15589 20476148 : && GET_MODE_SIZE (char_mode) == 1)
15590 511994 : return build_int_cst_type (TREE_TYPE (exp),
15591 255997 : (TREE_STRING_POINTER (string)
15592 255997 : [TREE_INT_CST_LOW (index)]));
15593 : }
15594 : return NULL;
15595 : }
15596 :
15597 : /* Folds a read from vector element at IDX of vector ARG. */
15598 :
15599 : tree
15600 9699 : fold_read_from_vector (tree arg, poly_uint64 idx)
15601 : {
15602 9699 : unsigned HOST_WIDE_INT i;
15603 9699 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15604 9699 : && known_ge (idx, 0u)
15605 9699 : && idx.is_constant (&i))
15606 : {
15607 9699 : if (TREE_CODE (arg) == VECTOR_CST)
15608 2020 : return VECTOR_CST_ELT (arg, i);
15609 7679 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15610 : {
15611 2565 : if (CONSTRUCTOR_NELTS (arg)
15612 2525 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15613 : return NULL_TREE;
15614 1727 : if (i >= CONSTRUCTOR_NELTS (arg))
15615 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15616 1687 : return CONSTRUCTOR_ELT (arg, i)->value;
15617 : }
15618 : }
15619 : return NULL_TREE;
15620 : }
15621 :
15622 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15623 : an integer constant, real, or fixed-point constant.
15624 :
15625 : TYPE is the type of the result. */
15626 :
15627 : static tree
15628 33171565 : fold_negate_const (tree arg0, tree type)
15629 : {
15630 33171565 : tree t = NULL_TREE;
15631 :
15632 33171565 : switch (TREE_CODE (arg0))
15633 : {
15634 2072486 : case REAL_CST:
15635 2072486 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15636 2072486 : break;
15637 :
15638 0 : case FIXED_CST:
15639 0 : {
15640 0 : FIXED_VALUE_TYPE f;
15641 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15642 0 : &(TREE_FIXED_CST (arg0)), NULL,
15643 0 : TYPE_SATURATING (type));
15644 0 : t = build_fixed (type, f);
15645 : /* Propagate overflow flags. */
15646 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15647 0 : TREE_OVERFLOW (t) = 1;
15648 0 : break;
15649 : }
15650 :
15651 31099079 : default:
15652 31099079 : if (poly_int_tree_p (arg0))
15653 : {
15654 31099079 : wi::overflow_type overflow;
15655 31099079 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15656 31099079 : t = force_fit_type (type, res, 1,
15657 31099079 : (overflow && ! TYPE_UNSIGNED (type))
15658 31089290 : || TREE_OVERFLOW (arg0));
15659 31099079 : break;
15660 31099079 : }
15661 :
15662 0 : gcc_unreachable ();
15663 : }
15664 :
15665 33171565 : return t;
15666 : }
15667 :
15668 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15669 : an integer constant or real constant.
15670 :
15671 : TYPE is the type of the result. */
15672 :
15673 : tree
15674 36117 : fold_abs_const (tree arg0, tree type)
15675 : {
15676 36117 : tree t = NULL_TREE;
15677 :
15678 36117 : switch (TREE_CODE (arg0))
15679 : {
15680 6621 : case INTEGER_CST:
15681 6621 : {
15682 : /* If the value is unsigned or non-negative, then the absolute value
15683 : is the same as the ordinary value. */
15684 6621 : wide_int val = wi::to_wide (arg0);
15685 6621 : wi::overflow_type overflow = wi::OVF_NONE;
15686 6621 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15687 : ;
15688 :
15689 : /* If the value is negative, then the absolute value is
15690 : its negation. */
15691 : else
15692 3015 : val = wi::neg (val, &overflow);
15693 :
15694 : /* Force to the destination type, set TREE_OVERFLOW for signed
15695 : TYPE only. */
15696 6621 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15697 6621 : }
15698 6621 : break;
15699 :
15700 29496 : case REAL_CST:
15701 29496 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15702 7515 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15703 : else
15704 : t = arg0;
15705 : break;
15706 :
15707 0 : default:
15708 0 : gcc_unreachable ();
15709 : }
15710 :
15711 36117 : return t;
15712 : }
15713 :
15714 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15715 : constant. TYPE is the type of the result. */
15716 :
15717 : static tree
15718 2400323 : fold_not_const (const_tree arg0, tree type)
15719 : {
15720 2400323 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15721 :
15722 2400323 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15723 : }
15724 :
15725 : /* Given CODE, a relational operator, the target type, TYPE and two
15726 : constant operands OP0 and OP1, return the result of the
15727 : relational operation. If the result is not a compile time
15728 : constant, then return NULL_TREE. */
15729 :
15730 : static tree
15731 84147055 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15732 : {
15733 84147055 : int result, invert;
15734 :
15735 : /* From here on, the only cases we handle are when the result is
15736 : known to be a constant. */
15737 :
15738 84147055 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15739 : {
15740 1224594 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15741 1224594 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15742 :
15743 : /* Handle the cases where either operand is a NaN. */
15744 1224594 : if (real_isnan (c0) || real_isnan (c1))
15745 : {
15746 13732 : switch (code)
15747 : {
15748 : case EQ_EXPR:
15749 : case ORDERED_EXPR:
15750 : result = 0;
15751 : break;
15752 :
15753 : case NE_EXPR:
15754 : case UNORDERED_EXPR:
15755 : case UNLT_EXPR:
15756 : case UNLE_EXPR:
15757 : case UNGT_EXPR:
15758 : case UNGE_EXPR:
15759 : case UNEQ_EXPR:
15760 6810 : result = 1;
15761 : break;
15762 :
15763 6970 : case LT_EXPR:
15764 6970 : case LE_EXPR:
15765 6970 : case GT_EXPR:
15766 6970 : case GE_EXPR:
15767 6970 : case LTGT_EXPR:
15768 6970 : if (flag_trapping_math)
15769 : return NULL_TREE;
15770 : result = 0;
15771 : break;
15772 :
15773 0 : default:
15774 0 : gcc_unreachable ();
15775 : }
15776 :
15777 6810 : return constant_boolean_node (result, type);
15778 : }
15779 :
15780 1210862 : return constant_boolean_node (real_compare (code, c0, c1), type);
15781 : }
15782 :
15783 82922461 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15784 : {
15785 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15786 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15787 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15788 : }
15789 :
15790 : /* Handle equality/inequality of complex constants. */
15791 82922461 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15792 : {
15793 58558 : tree rcond = fold_relational_const (code, type,
15794 29279 : TREE_REALPART (op0),
15795 29279 : TREE_REALPART (op1));
15796 117116 : tree icond = fold_relational_const (code, type,
15797 29279 : TREE_IMAGPART (op0),
15798 29279 : TREE_IMAGPART (op1));
15799 29279 : if (code == EQ_EXPR)
15800 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15801 28977 : else if (code == NE_EXPR)
15802 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15803 : else
15804 : return NULL_TREE;
15805 : }
15806 :
15807 82893182 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15808 : {
15809 22642 : if (!VECTOR_TYPE_P (type))
15810 : {
15811 : /* Have vector comparison with scalar boolean result. */
15812 154 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15813 : && known_eq (VECTOR_CST_NELTS (op0),
15814 : VECTOR_CST_NELTS (op1)));
15815 154 : unsigned HOST_WIDE_INT nunits;
15816 154 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15817 : return NULL_TREE;
15818 437 : for (unsigned i = 0; i < nunits; i++)
15819 : {
15820 384 : tree elem0 = VECTOR_CST_ELT (op0, i);
15821 384 : tree elem1 = VECTOR_CST_ELT (op1, i);
15822 384 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15823 384 : if (tmp == NULL_TREE)
15824 : return NULL_TREE;
15825 384 : if (integer_zerop (tmp))
15826 101 : return constant_boolean_node (code == NE_EXPR, type);
15827 : }
15828 53 : return constant_boolean_node (code == EQ_EXPR, type);
15829 : }
15830 22488 : tree_vector_builder elts;
15831 22488 : if (!elts.new_binary_operation (type, op0, op1, false))
15832 : return NULL_TREE;
15833 22488 : unsigned int count = elts.encoded_nelts ();
15834 78499 : for (unsigned i = 0; i < count; i++)
15835 : {
15836 56011 : tree elem_type = TREE_TYPE (type);
15837 56011 : tree elem0 = VECTOR_CST_ELT (op0, i);
15838 56011 : tree elem1 = VECTOR_CST_ELT (op1, i);
15839 :
15840 56011 : tree tem = fold_relational_const (code, elem_type,
15841 : elem0, elem1);
15842 :
15843 56011 : if (tem == NULL_TREE)
15844 : return NULL_TREE;
15845 :
15846 56011 : elts.quick_push (build_int_cst (elem_type,
15847 90429 : integer_zerop (tem) ? 0 : -1));
15848 : }
15849 :
15850 22488 : return elts.build ();
15851 22488 : }
15852 :
15853 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15854 :
15855 : To compute GT, swap the arguments and do LT.
15856 : To compute GE, do LT and invert the result.
15857 : To compute LE, swap the arguments, do LT and invert the result.
15858 : To compute NE, do EQ and invert the result.
15859 :
15860 : Therefore, the code below must handle only EQ and LT. */
15861 :
15862 82870540 : if (code == LE_EXPR || code == GT_EXPR)
15863 : {
15864 14686827 : std::swap (op0, op1);
15865 14686827 : code = swap_tree_comparison (code);
15866 : }
15867 :
15868 : /* Note that it is safe to invert for real values here because we
15869 : have already handled the one case that it matters. */
15870 :
15871 82870540 : invert = 0;
15872 82870540 : if (code == NE_EXPR || code == GE_EXPR)
15873 : {
15874 37388972 : invert = 1;
15875 37388972 : code = invert_tree_comparison (code, false);
15876 : }
15877 :
15878 : /* Compute a result for LT or EQ if args permit;
15879 : Otherwise return T. */
15880 82870540 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15881 : {
15882 82841384 : if (code == EQ_EXPR)
15883 40709196 : result = tree_int_cst_equal (op0, op1);
15884 : else
15885 42132188 : result = tree_int_cst_lt (op0, op1);
15886 : }
15887 : else
15888 : return NULL_TREE;
15889 :
15890 82841384 : if (invert)
15891 37387022 : result ^= 1;
15892 82841384 : return constant_boolean_node (result, type);
15893 : }
15894 :
15895 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15896 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15897 : itself. */
15898 :
15899 : tree
15900 138433338 : fold_build_cleanup_point_expr (tree type, tree expr)
15901 : {
15902 : /* If the expression does not have side effects then we don't have to wrap
15903 : it with a cleanup point expression. */
15904 138433338 : if (!TREE_SIDE_EFFECTS (expr))
15905 : return expr;
15906 :
15907 : /* If the expression is a return, check to see if the expression inside the
15908 : return has no side effects or the right hand side of the modify expression
15909 : inside the return. If either don't have side effects set we don't need to
15910 : wrap the expression in a cleanup point expression. Note we don't check the
15911 : left hand side of the modify because it should always be a return decl. */
15912 118459342 : if (TREE_CODE (expr) == RETURN_EXPR)
15913 : {
15914 46660211 : tree op = TREE_OPERAND (expr, 0);
15915 46660211 : if (!op || !TREE_SIDE_EFFECTS (op))
15916 : return expr;
15917 45945780 : op = TREE_OPERAND (op, 1);
15918 45945780 : if (!TREE_SIDE_EFFECTS (op))
15919 : return expr;
15920 : }
15921 :
15922 93628286 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15923 : }
15924 :
15925 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15926 : of an indirection through OP0, or NULL_TREE if no simplification is
15927 : possible. */
15928 :
15929 : tree
15930 22855884 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15931 : {
15932 22855884 : tree sub = op0;
15933 22855884 : tree subtype;
15934 22855884 : poly_uint64 const_op01;
15935 :
15936 22855884 : STRIP_NOPS (sub);
15937 22855884 : subtype = TREE_TYPE (sub);
15938 22855884 : if (!POINTER_TYPE_P (subtype)
15939 22855884 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15940 : return NULL_TREE;
15941 :
15942 22697377 : if (TREE_CODE (sub) == ADDR_EXPR)
15943 : {
15944 5072671 : tree op = TREE_OPERAND (sub, 0);
15945 5072671 : tree optype = TREE_TYPE (op);
15946 :
15947 : /* *&CONST_DECL -> to the value of the const decl. */
15948 5072671 : if (TREE_CODE (op) == CONST_DECL)
15949 3316 : return DECL_INITIAL (op);
15950 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15951 5069355 : if (type == optype)
15952 : {
15953 3839392 : tree fop = fold_read_from_constant_string (op);
15954 3839392 : if (fop)
15955 : return fop;
15956 : else
15957 3793184 : return op;
15958 : }
15959 : /* *(foo *)&fooarray => fooarray[0] */
15960 1229963 : else if (TREE_CODE (optype) == ARRAY_TYPE
15961 13973 : && type == TREE_TYPE (optype)
15962 1242735 : && (!in_gimple_form
15963 3189 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15964 : {
15965 12772 : tree type_domain = TYPE_DOMAIN (optype);
15966 12772 : tree min_val = size_zero_node;
15967 12772 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15968 12733 : min_val = TYPE_MIN_VALUE (type_domain);
15969 12772 : if (in_gimple_form
15970 3189 : && TREE_CODE (min_val) != INTEGER_CST)
15971 : return NULL_TREE;
15972 12772 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15973 12772 : NULL_TREE, NULL_TREE);
15974 : }
15975 : /* *(foo *)&complexfoo => __real__ complexfoo */
15976 1217191 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15977 1217191 : && type == TREE_TYPE (optype))
15978 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15979 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15980 1217191 : else if (VECTOR_TYPE_P (optype)
15981 1217191 : && type == TREE_TYPE (optype))
15982 : {
15983 70 : tree part_width = TYPE_SIZE (type);
15984 70 : tree index = bitsize_int (0);
15985 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15986 70 : index);
15987 : }
15988 : }
15989 :
15990 18841827 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15991 18841827 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15992 : {
15993 262517 : tree op00 = TREE_OPERAND (sub, 0);
15994 262517 : tree op01 = TREE_OPERAND (sub, 1);
15995 :
15996 262517 : STRIP_NOPS (op00);
15997 262517 : if (TREE_CODE (op00) == ADDR_EXPR)
15998 : {
15999 1747 : tree op00type;
16000 1747 : op00 = TREE_OPERAND (op00, 0);
16001 1747 : op00type = TREE_TYPE (op00);
16002 :
16003 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16004 1747 : if (VECTOR_TYPE_P (op00type)
16005 240 : && type == TREE_TYPE (op00type)
16006 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16007 : but we want to treat offsets with MSB set as negative.
16008 : For the code below negative offsets are invalid and
16009 : TYPE_SIZE of the element is something unsigned, so
16010 : check whether op01 fits into poly_int64, which implies
16011 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16012 : then just use poly_uint64 because we want to treat the
16013 : value as unsigned. */
16014 1940 : && tree_fits_poly_int64_p (op01))
16015 : {
16016 179 : tree part_width = TYPE_SIZE (type);
16017 179 : poly_uint64 max_offset
16018 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16019 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16020 179 : if (known_lt (const_op01, max_offset))
16021 : {
16022 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16023 179 : return fold_build3_loc (loc,
16024 : BIT_FIELD_REF, type, op00,
16025 179 : part_width, index);
16026 : }
16027 : }
16028 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16029 1568 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16030 1568 : && type == TREE_TYPE (op00type))
16031 : {
16032 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16033 : const_op01))
16034 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16035 : }
16036 : /* ((foo *)&fooarray)[1] => fooarray[1] */
16037 1568 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16038 1568 : && type == TREE_TYPE (op00type))
16039 : {
16040 719 : tree type_domain = TYPE_DOMAIN (op00type);
16041 719 : tree min_val = size_zero_node;
16042 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16043 718 : min_val = TYPE_MIN_VALUE (type_domain);
16044 719 : poly_uint64 type_size, index;
16045 719 : if (poly_int_tree_p (min_val)
16046 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16047 719 : && multiple_p (const_op01, type_size, &index))
16048 : {
16049 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16050 719 : op01 = wide_int_to_tree (sizetype, off);
16051 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16052 : NULL_TREE, NULL_TREE);
16053 : }
16054 : }
16055 : }
16056 : }
16057 :
16058 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16059 18840929 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16060 692408 : && type == TREE_TYPE (TREE_TYPE (subtype))
16061 18844312 : && (!in_gimple_form
16062 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16063 : {
16064 3382 : tree type_domain;
16065 3382 : tree min_val = size_zero_node;
16066 3382 : sub = build_fold_indirect_ref_loc (loc, sub);
16067 3382 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16068 3382 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16069 3382 : min_val = TYPE_MIN_VALUE (type_domain);
16070 3382 : if (in_gimple_form
16071 11 : && TREE_CODE (min_val) != INTEGER_CST)
16072 : return NULL_TREE;
16073 3382 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16074 3382 : NULL_TREE);
16075 : }
16076 :
16077 : return NULL_TREE;
16078 : }
16079 :
16080 : /* Builds an expression for an indirection through T, simplifying some
16081 : cases. */
16082 :
16083 : tree
16084 11941793 : build_fold_indirect_ref_loc (location_t loc, tree t)
16085 : {
16086 11941793 : tree type = TREE_TYPE (TREE_TYPE (t));
16087 11941793 : tree sub = fold_indirect_ref_1 (loc, type, t);
16088 :
16089 11941793 : if (sub)
16090 : return sub;
16091 :
16092 8105965 : return build1_loc (loc, INDIRECT_REF, type, t);
16093 : }
16094 :
16095 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16096 :
16097 : tree
16098 10530840 : fold_indirect_ref_loc (location_t loc, tree t)
16099 : {
16100 10530840 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16101 :
16102 10530840 : if (sub)
16103 : return sub;
16104 : else
16105 10509528 : return t;
16106 : }
16107 :
16108 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16109 : whose result is ignored. The type of the returned tree need not be
16110 : the same as the original expression. */
16111 :
16112 : tree
16113 136106 : fold_ignored_result (tree t)
16114 : {
16115 136106 : if (!TREE_SIDE_EFFECTS (t))
16116 18165 : return integer_zero_node;
16117 :
16118 157140 : for (;;)
16119 157140 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16120 : {
16121 3835 : case tcc_unary:
16122 3835 : t = TREE_OPERAND (t, 0);
16123 3835 : break;
16124 :
16125 5123 : case tcc_binary:
16126 5123 : case tcc_comparison:
16127 5123 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16128 3207 : t = TREE_OPERAND (t, 0);
16129 1916 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16130 43 : t = TREE_OPERAND (t, 1);
16131 : else
16132 : return t;
16133 : break;
16134 :
16135 100547 : case tcc_expression:
16136 100547 : switch (TREE_CODE (t))
16137 : {
16138 32115 : case COMPOUND_EXPR:
16139 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16140 : return t;
16141 31820 : t = TREE_OPERAND (t, 0);
16142 31820 : break;
16143 :
16144 382 : case COND_EXPR:
16145 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16146 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16147 : return t;
16148 294 : t = TREE_OPERAND (t, 0);
16149 294 : break;
16150 :
16151 : default:
16152 : return t;
16153 : }
16154 : break;
16155 :
16156 : default:
16157 : return t;
16158 : }
16159 : }
16160 :
16161 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16162 :
16163 : tree
16164 3185058404 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16165 : {
16166 3185058404 : tree div = NULL_TREE;
16167 :
16168 3185058404 : if (divisor == 1)
16169 : return value;
16170 :
16171 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16172 : have to do anything. Only do this when we are not given a const,
16173 : because in that case, this check is more expensive than just
16174 : doing it. */
16175 1979130379 : if (TREE_CODE (value) != INTEGER_CST)
16176 : {
16177 392359 : div = build_int_cst (TREE_TYPE (value), divisor);
16178 :
16179 392359 : if (multiple_of_p (TREE_TYPE (value), value, div))
16180 : return value;
16181 : }
16182 :
16183 : /* If divisor is a power of two, simplify this to bit manipulation. */
16184 1978739932 : if (pow2_or_zerop (divisor))
16185 : {
16186 1978739932 : if (TREE_CODE (value) == INTEGER_CST)
16187 : {
16188 1978738020 : wide_int val = wi::to_wide (value);
16189 1978738020 : bool overflow_p;
16190 :
16191 1978738020 : if ((val & (divisor - 1)) == 0)
16192 : return value;
16193 :
16194 4119413 : overflow_p = TREE_OVERFLOW (value);
16195 4119413 : val += divisor - 1;
16196 4119413 : val &= (int) -divisor;
16197 4119413 : if (val == 0)
16198 4 : overflow_p = true;
16199 :
16200 4119413 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16201 1978738020 : }
16202 : else
16203 : {
16204 1912 : tree t;
16205 :
16206 1912 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16207 1912 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16208 1912 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16209 1912 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16210 : }
16211 : }
16212 : else
16213 : {
16214 0 : if (!div)
16215 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16216 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16217 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16218 : }
16219 :
16220 : return value;
16221 : }
16222 :
16223 : /* Likewise, but round down. */
16224 :
16225 : tree
16226 21912547 : round_down_loc (location_t loc, tree value, int divisor)
16227 : {
16228 21912547 : tree div = NULL_TREE;
16229 :
16230 21912547 : gcc_assert (divisor > 0);
16231 21912547 : if (divisor == 1)
16232 : return value;
16233 :
16234 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16235 : have to do anything. Only do this when we are not given a const,
16236 : because in that case, this check is more expensive than just
16237 : doing it. */
16238 21912547 : if (TREE_CODE (value) != INTEGER_CST)
16239 : {
16240 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16241 :
16242 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16243 : return value;
16244 : }
16245 :
16246 : /* If divisor is a power of two, simplify this to bit manipulation. */
16247 21912547 : if (pow2_or_zerop (divisor))
16248 : {
16249 21912547 : tree t;
16250 :
16251 21912547 : t = build_int_cst (TREE_TYPE (value), -divisor);
16252 21912547 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16253 : }
16254 : else
16255 : {
16256 0 : if (!div)
16257 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16258 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16259 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16260 : }
16261 :
16262 : return value;
16263 : }
16264 :
16265 : /* Returns the pointer to the base of the object addressed by EXP and
16266 : extracts the information about the offset of the access, storing it
16267 : to PBITPOS and POFFSET. */
16268 :
16269 : static tree
16270 25780378 : split_address_to_core_and_offset (tree exp,
16271 : poly_int64 *pbitpos, tree *poffset)
16272 : {
16273 25780378 : tree core;
16274 25780378 : machine_mode mode;
16275 25780378 : int unsignedp, reversep, volatilep;
16276 25780378 : poly_int64 bitsize;
16277 25780378 : location_t loc = EXPR_LOCATION (exp);
16278 :
16279 25780378 : STRIP_NOPS (exp);
16280 :
16281 25780378 : if (TREE_CODE (exp) == SSA_NAME)
16282 16214558 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16283 20188791 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16284 49749 : exp = gimple_assign_rhs1 (def);
16285 :
16286 25780378 : if (TREE_CODE (exp) == ADDR_EXPR)
16287 : {
16288 2055916 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16289 : poffset, &mode, &unsignedp, &reversep,
16290 : &volatilep);
16291 : /* If we are left with MEM[a + CST] strip that and add it to the
16292 : pbitpos and return a. */
16293 2055916 : if (TREE_CODE (core) == MEM_REF)
16294 : {
16295 48498 : poly_offset_int tem;
16296 48498 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16297 48498 : tem <<= LOG2_BITS_PER_UNIT;
16298 48498 : tem += *pbitpos;
16299 48498 : if (tem.to_shwi (pbitpos))
16300 48310 : return TREE_OPERAND (core, 0);
16301 : }
16302 2007606 : core = build_fold_addr_expr_loc (loc, core);
16303 : }
16304 23724462 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16305 : {
16306 604206 : core = TREE_OPERAND (exp, 0);
16307 604206 : STRIP_NOPS (core);
16308 604206 : *pbitpos = 0;
16309 604206 : *poffset = TREE_OPERAND (exp, 1);
16310 604206 : if (poly_int_tree_p (*poffset))
16311 : {
16312 585318 : poly_offset_int tem
16313 585318 : = wi::sext (wi::to_poly_offset (*poffset),
16314 585318 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16315 585318 : tem <<= LOG2_BITS_PER_UNIT;
16316 585318 : if (tem.to_shwi (pbitpos))
16317 585318 : *poffset = NULL_TREE;
16318 : }
16319 : }
16320 : else
16321 : {
16322 23120256 : core = exp;
16323 23120256 : *pbitpos = 0;
16324 23120256 : *poffset = NULL_TREE;
16325 : }
16326 :
16327 : return core;
16328 : }
16329 :
16330 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16331 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16332 :
16333 : bool
16334 12890189 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16335 : {
16336 12890189 : tree core1, core2;
16337 12890189 : poly_int64 bitpos1, bitpos2;
16338 12890189 : tree toffset1, toffset2, tdiff, type;
16339 :
16340 12890189 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16341 12890189 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16342 :
16343 12890189 : poly_int64 bytepos1, bytepos2;
16344 12890189 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16345 25780378 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16346 25780378 : || !operand_equal_p (core1, core2, 0))
16347 : return false;
16348 :
16349 707950 : if (toffset1 && toffset2)
16350 : {
16351 149 : type = TREE_TYPE (toffset1);
16352 149 : if (type != TREE_TYPE (toffset2))
16353 0 : toffset2 = fold_convert (type, toffset2);
16354 :
16355 149 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16356 149 : if (!cst_and_fits_in_hwi (tdiff))
16357 : return false;
16358 :
16359 53 : *diff = int_cst_value (tdiff);
16360 : }
16361 707801 : else if (toffset1 || toffset2)
16362 : {
16363 : /* If only one of the offsets is non-constant, the difference cannot
16364 : be a constant. */
16365 : return false;
16366 : }
16367 : else
16368 689193 : *diff = 0;
16369 :
16370 689246 : *diff += bytepos1 - bytepos2;
16371 689246 : return true;
16372 : }
16373 :
16374 : /* Return OFF converted to a pointer offset type suitable as offset for
16375 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16376 : tree
16377 53704382 : convert_to_ptrofftype_loc (location_t loc, tree off)
16378 : {
16379 53704382 : if (ptrofftype_p (TREE_TYPE (off)))
16380 : return off;
16381 6275790 : return fold_convert_loc (loc, sizetype, off);
16382 : }
16383 :
16384 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16385 : tree
16386 47538451 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16387 : {
16388 47538451 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16389 47538451 : ptr, convert_to_ptrofftype_loc (loc, off));
16390 : }
16391 :
16392 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16393 : tree
16394 167169 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16395 : {
16396 167169 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16397 167169 : ptr, size_int (off));
16398 : }
16399 :
16400 : /* Return a pointer to a NUL-terminated string containing the sequence
16401 : of bytes corresponding to the representation of the object referred to
16402 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16403 : to an initialized constant array plus some constant offset).
16404 : Set *STRSIZE the number of bytes in the constant sequence including
16405 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16406 : where A is the array that stores the constant sequence that SRC points
16407 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16408 : need not point to a string or even an array of characters but may point
16409 : to an object of any type. */
16410 :
16411 : const char *
16412 12830908 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16413 : {
16414 : /* The offset into the array A storing the string, and A's byte size. */
16415 12830908 : tree offset_node;
16416 12830908 : tree mem_size;
16417 :
16418 12830908 : if (strsize)
16419 4691016 : *strsize = 0;
16420 :
16421 12830908 : if (strsize)
16422 4691016 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16423 : else
16424 8139892 : src = string_constant (src, &offset_node, &mem_size, NULL);
16425 12830908 : if (!src)
16426 : return NULL;
16427 :
16428 2861709 : unsigned HOST_WIDE_INT offset = 0;
16429 2861709 : if (offset_node != NULL_TREE)
16430 : {
16431 2861709 : if (!tree_fits_uhwi_p (offset_node))
16432 : return NULL;
16433 : else
16434 2858559 : offset = tree_to_uhwi (offset_node);
16435 : }
16436 :
16437 2858559 : if (!tree_fits_uhwi_p (mem_size))
16438 : return NULL;
16439 :
16440 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16441 : is stored in and equal to sizeof A. INIT_BYTES is the number
16442 : of bytes in the constant sequence used to initialize the array,
16443 : including any embedded NULs as well as the terminating NUL (for
16444 : strings), but not including any trailing zeros/NULs past
16445 : the terminating one appended implicitly to a string literal to
16446 : zero out the remainder of the array it's stored in. For example,
16447 : given:
16448 : const char a[7] = "abc\0d";
16449 : n = strlen (a + 1);
16450 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16451 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16452 : is equal to strlen (A) + 1. */
16453 2858559 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16454 2858559 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16455 2858559 : const char *string = TREE_STRING_POINTER (src);
16456 :
16457 : /* Ideally this would turn into a gcc_checking_assert over time. */
16458 2858559 : if (init_bytes > array_size)
16459 : init_bytes = array_size;
16460 :
16461 2858559 : if (init_bytes == 0 || offset >= array_size)
16462 : return NULL;
16463 :
16464 2857292 : if (strsize)
16465 : {
16466 : /* Compute and store the number of characters from the beginning
16467 : of the substring at OFFSET to the end, including the terminating
16468 : nul. Offsets past the initial length refer to null strings. */
16469 1444445 : if (offset < init_bytes)
16470 1444445 : *strsize = init_bytes - offset;
16471 : else
16472 0 : *strsize = 1;
16473 : }
16474 : else
16475 : {
16476 1412847 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16477 : /* Support only properly NUL-terminated single byte strings. */
16478 1412847 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16479 : return NULL;
16480 1408001 : if (string[init_bytes - 1] != '\0')
16481 : return NULL;
16482 : }
16483 :
16484 2826390 : return offset < init_bytes ? string + offset : "";
16485 : }
16486 :
16487 : /* Return a pointer to a NUL-terminated string corresponding to
16488 : the expression STR referencing a constant string, possibly
16489 : involving a constant offset. Return null if STR either doesn't
16490 : reference a constant string or if it involves a nonconstant
16491 : offset. */
16492 :
16493 : const char *
16494 8139892 : c_getstr (tree str)
16495 : {
16496 8139892 : return getbyterep (str, NULL);
16497 : }
16498 :
16499 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16500 : may be nonzero, with precision PREC, the precision of T's type. */
16501 :
16502 : static wide_int
16503 259204021 : tree_nonzero_bits (const_tree t, unsigned prec)
16504 : {
16505 259204021 : switch (TREE_CODE (t))
16506 : {
16507 8741703 : case INTEGER_CST:
16508 8741703 : return wi::to_wide (t);
16509 145448757 : case SSA_NAME:
16510 145448757 : return get_nonzero_bits (t);
16511 284048 : case NON_LVALUE_EXPR:
16512 284048 : case SAVE_EXPR:
16513 284048 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16514 528524 : case BIT_AND_EXPR:
16515 1057048 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16516 1585572 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16517 19665 : case BIT_IOR_EXPR:
16518 19665 : case BIT_XOR_EXPR:
16519 39330 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16520 58995 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16521 73235 : case COND_EXPR:
16522 146470 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16523 219705 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16524 52742553 : CASE_CONVERT:
16525 52742553 : if (TREE_TYPE (t) != error_mark_node
16526 52742553 : && !error_operand_p (TREE_OPERAND (t, 0)))
16527 : {
16528 52742552 : tree op0 = TREE_OPERAND (t, 0);
16529 52742552 : tree inner_type = TREE_TYPE (op0);
16530 52742552 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16531 105485104 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16532 105485104 : prec, TYPE_SIGN (inner_type));
16533 : }
16534 : break;
16535 14181473 : case PLUS_EXPR:
16536 14181473 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16537 : {
16538 14181473 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16539 14181473 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16540 14181473 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16541 544205 : return wi::bit_or (nzbits1, nzbits2);
16542 14181473 : }
16543 : break;
16544 170645 : case LSHIFT_EXPR:
16545 170645 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16546 170645 : && TREE_TYPE (t) != error_mark_node)
16547 : {
16548 98664 : tree type = TREE_TYPE (t);
16549 98664 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16550 98664 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16551 98664 : return wi::neg_p (arg1)
16552 197328 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16553 98664 : : wi::lshift (nzbits, arg1);
16554 98664 : }
16555 : break;
16556 162505 : case RSHIFT_EXPR:
16557 162505 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16558 162505 : && TREE_TYPE (t) != error_mark_node)
16559 : {
16560 160557 : tree type = TREE_TYPE (t);
16561 160557 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16562 160557 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16563 160557 : return wi::neg_p (arg1)
16564 321114 : ? wi::lshift (nzbits, -arg1)
16565 160557 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16566 160557 : }
16567 : break;
16568 : default:
16569 : break;
16570 : }
16571 :
16572 50562111 : return wi::shwi (-1, prec);
16573 : }
16574 :
16575 : /* Given a tree T, compute which bits in T may be nonzero. */
16576 :
16577 : wide_int
16578 176312406 : tree_nonzero_bits (const_tree t)
16579 : {
16580 176312406 : if (error_operand_p (t))
16581 0 : return wi::shwi (-1, 64);
16582 176312406 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16583 : }
16584 :
16585 : /* Helper function for address compare simplifications in match.pd.
16586 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16587 : TYPE is the type of comparison operands.
16588 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16589 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16590 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16591 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16592 : and 2 if unknown. */
16593 :
16594 : int
16595 5531429 : address_compare (tree_code code, tree type, tree op0, tree op1,
16596 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16597 : bool generic)
16598 : {
16599 5531429 : if (TREE_CODE (op0) == SSA_NAME)
16600 37387 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16601 5531429 : if (TREE_CODE (op1) == SSA_NAME)
16602 4600 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16603 5531429 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16604 5531429 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16605 5531429 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16606 5531429 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16607 5531429 : if (base0 && TREE_CODE (base0) == MEM_REF)
16608 : {
16609 36796 : off0 += mem_ref_offset (base0).force_shwi ();
16610 36796 : base0 = TREE_OPERAND (base0, 0);
16611 : }
16612 5531429 : if (base1 && TREE_CODE (base1) == MEM_REF)
16613 : {
16614 3769 : off1 += mem_ref_offset (base1).force_shwi ();
16615 3769 : base1 = TREE_OPERAND (base1, 0);
16616 : }
16617 5531429 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16618 : return 2;
16619 :
16620 5518894 : int equal = 2;
16621 : /* Punt in GENERIC on variables with value expressions;
16622 : the value expressions might point to fields/elements
16623 : of other vars etc. */
16624 5518894 : if (generic
16625 5518894 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16626 5379640 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16627 : return 2;
16628 5518237 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16629 : {
16630 1429333 : symtab_node *node0 = symtab_node::get_create (base0);
16631 1429333 : symtab_node *node1 = symtab_node::get_create (base1);
16632 1429333 : equal = node0->equal_address_to (node1);
16633 : }
16634 4088904 : else if ((DECL_P (base0)
16635 247110 : || TREE_CODE (base0) == SSA_NAME
16636 211433 : || TREE_CODE (base0) == STRING_CST)
16637 4088706 : && (DECL_P (base1)
16638 215126 : || TREE_CODE (base1) == SSA_NAME
16639 211583 : || TREE_CODE (base1) == STRING_CST))
16640 4088691 : equal = (base0 == base1);
16641 : /* Assume different STRING_CSTs with the same content will be
16642 : merged. */
16643 5518024 : if (equal == 0
16644 84087 : && TREE_CODE (base0) == STRING_CST
16645 17476 : && TREE_CODE (base1) == STRING_CST
16646 17277 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16647 5518024 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16648 6190 : TREE_STRING_LENGTH (base0)) == 0)
16649 : equal = 1;
16650 5513804 : if (equal == 1)
16651 : {
16652 5413759 : if (code == EQ_EXPR
16653 5413759 : || code == NE_EXPR
16654 : /* If the offsets are equal we can ignore overflow. */
16655 204756 : || known_eq (off0, off1)
16656 301512 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16657 : /* Or if we compare using pointers to decls or strings. */
16658 5564515 : || (POINTER_TYPE_P (type)
16659 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16660 5413759 : return 1;
16661 : return 2;
16662 : }
16663 104478 : if (equal != 0)
16664 : return equal;
16665 79654 : if (code != EQ_EXPR && code != NE_EXPR)
16666 : return 2;
16667 :
16668 : /* At this point we know (or assume) the two pointers point at
16669 : different objects. */
16670 74429 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16671 74429 : off0.is_constant (&ioff0);
16672 74429 : off1.is_constant (&ioff1);
16673 : /* Punt on non-zero offsets from functions. */
16674 74429 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16675 74429 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16676 : return 2;
16677 : /* Or if the bases are neither decls nor string literals. */
16678 74429 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16679 : return 2;
16680 39488 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16681 : return 2;
16682 : /* For initializers, assume addresses of different functions are
16683 : different. */
16684 39488 : if (folding_initializer
16685 13913 : && TREE_CODE (base0) == FUNCTION_DECL
16686 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16687 : return 0;
16688 :
16689 : /* Compute whether one address points to the start of one
16690 : object and another one to the end of another one. */
16691 39468 : poly_int64 size0 = 0, size1 = 0;
16692 39468 : if (TREE_CODE (base0) == STRING_CST)
16693 : {
16694 12871 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16695 : equal = 2;
16696 : else
16697 : size0 = TREE_STRING_LENGTH (base0);
16698 : }
16699 26597 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16700 : size0 = 1;
16701 : else
16702 : {
16703 26278 : tree sz0 = DECL_SIZE_UNIT (base0);
16704 26278 : if (!tree_fits_poly_int64_p (sz0))
16705 : equal = 2;
16706 : else
16707 26278 : size0 = tree_to_poly_int64 (sz0);
16708 : }
16709 39468 : if (TREE_CODE (base1) == STRING_CST)
16710 : {
16711 12970 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16712 : equal = 2;
16713 : else
16714 : size1 = TREE_STRING_LENGTH (base1);
16715 : }
16716 26498 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16717 : size1 = 1;
16718 : else
16719 : {
16720 26183 : tree sz1 = DECL_SIZE_UNIT (base1);
16721 26183 : if (!tree_fits_poly_int64_p (sz1))
16722 : equal = 2;
16723 : else
16724 26183 : size1 = tree_to_poly_int64 (sz1);
16725 : }
16726 39468 : if (equal == 0)
16727 : {
16728 : /* If one offset is pointing (or could be) to the beginning of one
16729 : object and the other is pointing to one past the last byte of the
16730 : other object, punt. */
16731 39456 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16732 : equal = 2;
16733 39263 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16734 : equal = 2;
16735 : /* If both offsets are the same, there are some cases we know that are
16736 : ok. Either if we know they aren't zero, or if we know both sizes
16737 : are no zero. */
16738 : if (equal == 2
16739 343 : && known_eq (off0, off1)
16740 22 : && (known_ne (off0, 0)
16741 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16742 : equal = 0;
16743 : }
16744 :
16745 : /* At this point, equal is 2 if either one or both pointers are out of
16746 : bounds of their object, or one points to start of its object and the
16747 : other points to end of its object. This is unspecified behavior
16748 : e.g. in C++. Otherwise equal is 0. */
16749 39468 : if (folding_cxx_constexpr && equal)
16750 : return equal;
16751 :
16752 : /* When both pointers point to string literals, even when equal is 0,
16753 : due to tail merging of string literals the pointers might be the same. */
16754 39405 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16755 : {
16756 12826 : if (ioff0 < 0
16757 12826 : || ioff1 < 0
16758 12826 : || ioff0 > TREE_STRING_LENGTH (base0)
16759 25640 : || ioff1 > TREE_STRING_LENGTH (base1))
16760 : return 2;
16761 :
16762 : /* If the bytes in the string literals starting at the pointers
16763 : differ, the pointers need to be different. */
16764 12814 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16765 12814 : TREE_STRING_POINTER (base1) + ioff1,
16766 12814 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16767 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16768 : {
16769 3908 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16770 3908 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16771 3908 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16772 : ioffmin) == 0)
16773 : /* If even the bytes in the string literal before the
16774 : pointers are the same, the string literals could be
16775 : tail merged. */
16776 : return 2;
16777 : }
16778 8918 : return 0;
16779 : }
16780 :
16781 26579 : if (folding_cxx_constexpr)
16782 : return 0;
16783 :
16784 : /* If this is a pointer comparison, ignore for now even
16785 : valid equalities where one pointer is the offset zero
16786 : of one object and the other to one past end of another one. */
16787 12790 : if (!INTEGRAL_TYPE_P (type))
16788 : return 0;
16789 :
16790 : /* Assume that string literals can't be adjacent to variables
16791 : (automatic or global). */
16792 385 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16793 : return 0;
16794 :
16795 : /* Assume that automatic variables can't be adjacent to global
16796 : variables. */
16797 359 : if (is_global_var (base0) != is_global_var (base1))
16798 2 : return 0;
16799 :
16800 : return equal;
16801 : }
16802 :
16803 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16804 : tree
16805 50 : ctor_single_nonzero_element (const_tree t)
16806 : {
16807 50 : unsigned HOST_WIDE_INT idx;
16808 50 : constructor_elt *ce;
16809 50 : tree elt = NULL_TREE;
16810 :
16811 50 : if (TREE_CODE (t) != CONSTRUCTOR)
16812 : return NULL_TREE;
16813 109 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16814 106 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16815 : {
16816 97 : if (elt)
16817 : return NULL_TREE;
16818 50 : elt = ce->value;
16819 : }
16820 : return elt;
16821 : }
16822 :
16823 : #if CHECKING_P
16824 :
16825 : namespace selftest {
16826 :
16827 : /* Helper functions for writing tests of folding trees. */
16828 :
16829 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16830 :
16831 : static void
16832 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16833 : tree constant)
16834 : {
16835 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16836 16 : }
16837 :
16838 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16839 : wrapping WRAPPED_EXPR. */
16840 :
16841 : static void
16842 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16843 : tree wrapped_expr)
16844 : {
16845 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16846 12 : ASSERT_NE (wrapped_expr, result);
16847 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16848 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16849 12 : }
16850 :
16851 : /* Verify that various arithmetic binary operations are folded
16852 : correctly. */
16853 :
16854 : static void
16855 4 : test_arithmetic_folding ()
16856 : {
16857 4 : tree type = integer_type_node;
16858 4 : tree x = create_tmp_var_raw (type, "x");
16859 4 : tree zero = build_zero_cst (type);
16860 4 : tree one = build_int_cst (type, 1);
16861 :
16862 : /* Addition. */
16863 : /* 1 <-- (0 + 1) */
16864 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16865 : one);
16866 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16867 : one);
16868 :
16869 : /* (nonlvalue)x <-- (x + 0) */
16870 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16871 : x);
16872 :
16873 : /* Subtraction. */
16874 : /* 0 <-- (x - x) */
16875 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16876 : zero);
16877 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16878 : x);
16879 :
16880 : /* Multiplication. */
16881 : /* 0 <-- (x * 0) */
16882 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16883 : zero);
16884 :
16885 : /* (nonlvalue)x <-- (x * 1) */
16886 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16887 : x);
16888 4 : }
16889 :
16890 : namespace test_operand_equality {
16891 :
16892 : /* Verify structural equality. */
16893 :
16894 : /* Execute fold_vec_perm_cst unit tests. */
16895 :
16896 : static void
16897 4 : test ()
16898 : {
16899 4 : tree stype = integer_type_node;
16900 4 : tree utype = unsigned_type_node;
16901 4 : tree x = create_tmp_var_raw (stype, "x");
16902 4 : tree y = create_tmp_var_raw (stype, "y");
16903 4 : tree z = create_tmp_var_raw (stype, "z");
16904 4 : tree four = build_int_cst (stype, 4);
16905 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16906 4 : tree rhs1 = fold_convert (stype,
16907 : fold_build2 (PLUS_EXPR, utype,
16908 : fold_convert (utype, x),
16909 : fold_convert (utype, y)));
16910 :
16911 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16912 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16913 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16914 :
16915 : /* (int)(unsigned) x == x. */
16916 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16917 : build1 (NOP_EXPR, utype, x));
16918 4 : tree rhs2 = x;
16919 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16920 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16921 :
16922 : /* (unsigned x) + (unsigned y) == x + y. */
16923 4 : tree lhs3 = lhs1;
16924 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16925 : fold_convert (utype, x),
16926 : fold_convert (utype, y));
16927 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16928 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16929 :
16930 : /* (unsigned x) / (unsigned y) == x / y. */
16931 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16932 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16933 : fold_convert (utype, x),
16934 : fold_convert (utype, y));
16935 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16936 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16937 :
16938 : /* (long x) / 4 == (long)(x / 4). */
16939 4 : tree lstype = long_long_integer_type_node;
16940 4 : tree lfour = build_int_cst (lstype, 4);
16941 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16942 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16943 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16944 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16945 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16946 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16947 :
16948 : /* (unsigned x) / 4 == x / 4. */
16949 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16950 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16951 : fold_convert (utype, x),
16952 : fold_convert (utype, four));
16953 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16954 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16955 :
16956 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16957 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16958 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16959 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16960 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16961 :
16962 : /* (unsigned x) + 4 == x + 4. */
16963 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16964 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16965 : fold_convert (utype, x),
16966 : fold_convert (utype, four));
16967 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16968 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16969 :
16970 : /* (unsigned x) + 4 == 4 + x. */
16971 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16972 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16973 : fold_convert (utype, x),
16974 : fold_convert (utype, four));
16975 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16976 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16977 :
16978 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16979 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16980 : fold_build2 (MULT_EXPR, stype,
16981 : fold_build2 (PLUS_EXPR, stype, four, x),
16982 : y),
16983 : z);
16984 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16985 : fold_build2 (PLUS_EXPR, utype,
16986 : fold_convert (utype, x),
16987 : fold_convert (utype, four)),
16988 : fold_convert (utype, y));
16989 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16990 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16991 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16992 4 : }
16993 : }
16994 :
16995 : namespace test_fold_vec_perm_cst {
16996 :
16997 : /* Build a VECTOR_CST corresponding to VMODE, and has
16998 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16999 : Fill it with randomized elements, using rand() % THRESHOLD. */
17000 :
17001 : static tree
17002 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17003 : unsigned nelts_per_pattern,
17004 : int step = 0, bool natural_stepped = false,
17005 : int threshold = 100)
17006 : {
17007 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17008 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17009 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17010 :
17011 : // Fill a0 for each pattern
17012 0 : for (unsigned i = 0; i < npatterns; i++)
17013 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17014 :
17015 0 : if (nelts_per_pattern == 1)
17016 0 : return builder.build ();
17017 :
17018 : // Fill a1 for each pattern
17019 0 : for (unsigned i = 0; i < npatterns; i++)
17020 : {
17021 0 : tree a1;
17022 0 : if (natural_stepped)
17023 : {
17024 0 : tree a0 = builder[i];
17025 0 : wide_int a0_val = wi::to_wide (a0);
17026 0 : wide_int a1_val = a0_val + step;
17027 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17028 0 : }
17029 : else
17030 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17031 0 : builder.quick_push (a1);
17032 : }
17033 0 : if (nelts_per_pattern == 2)
17034 0 : return builder.build ();
17035 :
17036 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17037 : {
17038 0 : tree prev_elem = builder[i - npatterns];
17039 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17040 0 : wide_int val = prev_elem_val + step;
17041 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17042 0 : }
17043 :
17044 0 : return builder.build ();
17045 0 : }
17046 :
17047 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17048 : when result is VLA. */
17049 :
17050 : static void
17051 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17052 : tree res, tree *expected_res)
17053 : {
17054 : /* Actual npatterns and encoded_elts in res may be less than expected due
17055 : to canonicalization. */
17056 0 : ASSERT_TRUE (res != NULL_TREE);
17057 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17058 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17059 :
17060 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17061 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17062 0 : }
17063 :
17064 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17065 : when the result is VLS. */
17066 :
17067 : static void
17068 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17069 : {
17070 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17071 0 : for (unsigned i = 0; i < expected_nelts; i++)
17072 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17073 0 : }
17074 :
17075 : /* Helper routine to push multiple elements into BUILDER. */
17076 : template<unsigned N>
17077 0 : static void builder_push_elems (vec_perm_builder& builder,
17078 : poly_uint64 (&elems)[N])
17079 : {
17080 0 : for (unsigned i = 0; i < N; i++)
17081 0 : builder.quick_push (elems[i]);
17082 0 : }
17083 :
17084 : #define ARG0(index) vector_cst_elt (arg0, index)
17085 : #define ARG1(index) vector_cst_elt (arg1, index)
17086 :
17087 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17088 :
17089 : static void
17090 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17091 : {
17092 0 : for (int i = 0; i < 10; i++)
17093 : {
17094 : /* Case 1:
17095 : sel = { 0, 4, 1, 5, ... }
17096 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17097 0 : {
17098 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17099 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17100 :
17101 0 : tree inner_type
17102 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17103 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17104 :
17105 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17106 0 : vec_perm_builder builder (res_len, 4, 1);
17107 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17108 0 : builder_push_elems (builder, mask_elems);
17109 :
17110 0 : vec_perm_indices sel (builder, 2, 4);
17111 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17112 :
17113 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17114 0 : validate_res (4, 1, res, expected_res);
17115 0 : }
17116 :
17117 : /* Case 2: Same as case 1, but contains an out of bounds access which
17118 : should wrap around.
17119 : sel = {0, 8, 4, 12, ...} (4, 1)
17120 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17121 0 : {
17122 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17123 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17124 :
17125 0 : tree inner_type
17126 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17127 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17128 :
17129 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17130 0 : vec_perm_builder builder (res_len, 4, 1);
17131 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17132 0 : builder_push_elems (builder, mask_elems);
17133 :
17134 0 : vec_perm_indices sel (builder, 2, 4);
17135 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17136 :
17137 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17138 0 : validate_res (4, 1, res, expected_res);
17139 0 : }
17140 : }
17141 0 : }
17142 :
17143 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17144 :
17145 : static void
17146 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17147 : {
17148 0 : for (int i = 0; i < 10; i++)
17149 : {
17150 : /* Case 1:
17151 : sel = { 0, 1, 2, 3}
17152 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17153 0 : {
17154 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17155 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17156 :
17157 0 : tree inner_type
17158 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17159 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17160 :
17161 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17162 0 : vec_perm_builder builder (res_len, 4, 1);
17163 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17164 0 : builder_push_elems (builder, mask_elems);
17165 :
17166 0 : vec_perm_indices sel (builder, 2,
17167 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17168 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17169 :
17170 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17171 0 : validate_res_vls (res, expected_res, 4);
17172 0 : }
17173 :
17174 : /* Case 2: Same as Case 1, but crossing input vector.
17175 : sel = {0, 2, 4, 6}
17176 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17177 : Since we cannot determine, which vector to choose from during
17178 : compile time, should return NULL_TREE. */
17179 0 : {
17180 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17181 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17182 :
17183 0 : tree inner_type
17184 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17185 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17186 :
17187 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17188 0 : vec_perm_builder builder (res_len, 4, 1);
17189 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17190 0 : builder_push_elems (builder, mask_elems);
17191 :
17192 0 : vec_perm_indices sel (builder, 2,
17193 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17194 0 : const char *reason;
17195 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17196 :
17197 0 : ASSERT_TRUE (res == NULL_TREE);
17198 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17199 0 : }
17200 : }
17201 0 : }
17202 :
17203 : /* Test all input vectors. */
17204 :
17205 : static void
17206 0 : test_all_nunits (machine_mode vmode)
17207 : {
17208 : /* Test with 10 different inputs. */
17209 0 : for (int i = 0; i < 10; i++)
17210 : {
17211 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17212 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17213 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17214 :
17215 : /* Case 1: mask = {0, ...} // (1, 1)
17216 : res = { arg0[0], ... } // (1, 1) */
17217 0 : {
17218 0 : vec_perm_builder builder (len, 1, 1);
17219 0 : builder.quick_push (0);
17220 0 : vec_perm_indices sel (builder, 2, len);
17221 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17222 0 : tree expected_res[] = { ARG0(0) };
17223 0 : validate_res (1, 1, res, expected_res);
17224 0 : }
17225 :
17226 : /* Case 2: mask = {len, ...} // (1, 1)
17227 : res = { arg1[0], ... } // (1, 1) */
17228 0 : {
17229 0 : vec_perm_builder builder (len, 1, 1);
17230 0 : builder.quick_push (len);
17231 0 : vec_perm_indices sel (builder, 2, len);
17232 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17233 :
17234 0 : tree expected_res[] = { ARG1(0) };
17235 0 : validate_res (1, 1, res, expected_res);
17236 0 : }
17237 : }
17238 0 : }
17239 :
17240 : /* Test all vectors which contain at-least 2 elements. */
17241 :
17242 : static void
17243 0 : test_nunits_min_2 (machine_mode vmode)
17244 : {
17245 0 : for (int i = 0; i < 10; i++)
17246 : {
17247 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17248 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17249 0 : {
17250 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17251 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17252 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17253 :
17254 0 : vec_perm_builder builder (len, 2, 1);
17255 0 : poly_uint64 mask_elems[] = { 0, len };
17256 0 : builder_push_elems (builder, mask_elems);
17257 :
17258 0 : vec_perm_indices sel (builder, 2, len);
17259 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17260 :
17261 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17262 0 : validate_res (2, 1, res, expected_res);
17263 0 : }
17264 :
17265 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17266 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
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);
17270 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17271 :
17272 0 : vec_perm_builder builder (len, 2, 2);
17273 0 : poly_uint64 mask_elems[] = { 0, len, 1, 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), ARG0(1), ARG1(1) };
17280 0 : validate_res (2, 2, res, expected_res);
17281 0 : }
17282 :
17283 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17284 : Test that the stepped sequence of the pattern selects from
17285 : same input pattern. Since input vectors have npatterns = 2,
17286 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17287 : in input vector. So return NULL_TREE. */
17288 0 : {
17289 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17290 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17291 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17292 :
17293 0 : vec_perm_builder builder (len, 1, 3);
17294 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17295 0 : builder_push_elems (builder, mask_elems);
17296 :
17297 0 : vec_perm_indices sel (builder, 2, len);
17298 0 : const char *reason;
17299 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17300 : &reason);
17301 0 : ASSERT_TRUE (res == NULL_TREE);
17302 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17303 0 : }
17304 :
17305 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17306 : Test that stepped sequence of the pattern selects from arg0.
17307 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17308 0 : {
17309 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17310 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17311 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17312 :
17313 0 : vec_perm_builder builder (len, 1, 3);
17314 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17315 0 : builder_push_elems (builder, mask_elems);
17316 :
17317 0 : vec_perm_indices sel (builder, 2, len);
17318 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17319 :
17320 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17321 0 : validate_res (1, 3, res, expected_res);
17322 0 : }
17323 :
17324 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17325 : In this case ensure that arg has a natural stepped sequence
17326 : to preserve arg's encoding.
17327 :
17328 : As a concrete example, consider:
17329 : arg0: { -16, -9, -10, ... } // (1, 3)
17330 : arg1: { -12, -5, -6, ... } // (1, 3)
17331 : sel = { 0, len, len + 1, ... } // (1, 3)
17332 :
17333 : This will create res with following encoding:
17334 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17335 : = { -16, -12, -5, ... }
17336 :
17337 : The step in above encoding would be: (-5) - (-12) = 7
17338 : And hence res[3] would be computed as -5 + 7 = 2.
17339 : instead of arg1[2], ie, -6.
17340 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17341 : for this case. */
17342 0 : {
17343 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17344 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17345 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17346 :
17347 0 : vec_perm_builder builder (len, 1, 3);
17348 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17349 0 : builder_push_elems (builder, mask_elems);
17350 :
17351 0 : vec_perm_indices sel (builder, 2, len);
17352 0 : const char *reason;
17353 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17354 : stepped pattern, even if we didn't explicitly tell it to. So folding
17355 : may not always fail, but if it does, ensure that's because arg1 does
17356 : not have a natural stepped sequence (and not due to other reason) */
17357 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17358 0 : if (res == NULL_TREE)
17359 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17360 0 : }
17361 :
17362 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17363 : sequence and thus folding should be valid for this case. */
17364 0 : {
17365 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17366 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17367 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17368 :
17369 0 : vec_perm_builder builder (len, 1, 3);
17370 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17371 0 : builder_push_elems (builder, mask_elems);
17372 :
17373 0 : vec_perm_indices sel (builder, 2, len);
17374 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17375 :
17376 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17377 0 : validate_res (1, 3, res, expected_res);
17378 0 : }
17379 :
17380 : /* Case 8: Same as aarch64/sve/slp_3.c:
17381 : arg0, arg1 are dup vectors.
17382 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17383 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17384 :
17385 : In this case, since the input vectors are dup, only the first two
17386 : elements per pattern in sel are considered significant. */
17387 0 : {
17388 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17389 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17390 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17391 :
17392 0 : vec_perm_builder builder (len, 2, 3);
17393 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17394 0 : builder_push_elems (builder, mask_elems);
17395 :
17396 0 : vec_perm_indices sel (builder, 2, len);
17397 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17398 :
17399 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17400 0 : validate_res (2, 1, res, expected_res);
17401 0 : }
17402 : }
17403 0 : }
17404 :
17405 : /* Test all vectors which contain at-least 4 elements. */
17406 :
17407 : static void
17408 0 : test_nunits_min_4 (machine_mode vmode)
17409 : {
17410 0 : for (int i = 0; i < 10; i++)
17411 : {
17412 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17413 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17414 0 : {
17415 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17416 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17417 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17418 :
17419 0 : vec_perm_builder builder (len, 4, 1);
17420 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17421 0 : builder_push_elems (builder, mask_elems);
17422 :
17423 0 : vec_perm_indices sel (builder, 2, len);
17424 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17425 :
17426 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17427 0 : validate_res (4, 1, res, expected_res);
17428 0 : }
17429 :
17430 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17431 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17432 0 : {
17433 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17434 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17435 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17436 :
17437 0 : vec_perm_builder builder (arg0_len, 1, 3);
17438 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17439 0 : builder_push_elems (builder, mask_elems);
17440 :
17441 0 : vec_perm_indices sel (builder, 2, arg0_len);
17442 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17443 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17444 0 : validate_res (1, 3, res, expected_res);
17445 0 : }
17446 :
17447 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17448 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17449 0 : {
17450 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17451 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17452 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17453 :
17454 0 : vec_perm_builder builder (len, 1, 3);
17455 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17456 0 : builder_push_elems (builder, mask_elems);
17457 :
17458 0 : vec_perm_indices sel (builder, 2, len);
17459 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17460 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17461 0 : validate_res (1, 3, res, expected_res);
17462 0 : }
17463 :
17464 : /* Case 4:
17465 : sel = { len, 0, 2, ... } // (1, 3)
17466 : This should return NULL because we cross the input vectors.
17467 : Because,
17468 : Let's assume len = C + Cx
17469 : a1 = 0
17470 : S = 2
17471 : esel = arg0_len / sel_npatterns = C + Cx
17472 : ae = 0 + (esel - 2) * S
17473 : = 0 + (C + Cx - 2) * 2
17474 : = 2(C-2) + 2Cx
17475 :
17476 : For C >= 4:
17477 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17478 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17479 : Since q1 != qe, we cross input vectors.
17480 : So return NULL_TREE. */
17481 0 : {
17482 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17483 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17484 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17485 :
17486 0 : vec_perm_builder builder (arg0_len, 1, 3);
17487 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17488 0 : builder_push_elems (builder, mask_elems);
17489 :
17490 0 : vec_perm_indices sel (builder, 2, arg0_len);
17491 0 : const char *reason;
17492 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17493 0 : ASSERT_TRUE (res == NULL_TREE);
17494 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17495 0 : }
17496 :
17497 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17498 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17499 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17500 :
17501 : Note that fold_vec_perm_cst will set
17502 : res_npatterns = max(4, max(4, 2)) = 4
17503 : However after canonicalizing, we will end up with shape (2, 2). */
17504 0 : {
17505 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17506 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17507 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17508 :
17509 0 : vec_perm_builder builder (len, 2, 2);
17510 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17511 0 : builder_push_elems (builder, mask_elems);
17512 :
17513 0 : vec_perm_indices sel (builder, 2, len);
17514 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17515 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17516 0 : validate_res (2, 2, res, expected_res);
17517 0 : }
17518 :
17519 : /* Case 6: Test combination in sel, where one pattern is dup and other
17520 : is stepped sequence.
17521 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17522 : res = { arg0[0], arg0[0], arg0[0],
17523 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17524 0 : {
17525 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17526 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17527 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17528 :
17529 0 : vec_perm_builder builder (len, 2, 3);
17530 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17531 0 : builder_push_elems (builder, mask_elems);
17532 :
17533 0 : vec_perm_indices sel (builder, 2, len);
17534 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17535 :
17536 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17537 0 : ARG0(1), ARG0(0), ARG0(2) };
17538 0 : validate_res (2, 3, res, expected_res);
17539 0 : }
17540 :
17541 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17542 : when arg0, arg1 and sel have different number of patterns.
17543 : arg0 is of shape (1, 1)
17544 : arg1 is of shape (4, 1)
17545 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17546 :
17547 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17548 : However,
17549 : step = (len+2) - (len+1) = 1
17550 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17551 : Since step is not a multiple of arg_npatterns,
17552 : valid_mask_for_fold_vec_perm_cst should return false,
17553 : and thus fold_vec_perm_cst should return NULL_TREE. */
17554 0 : {
17555 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17556 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17557 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17558 :
17559 0 : vec_perm_builder builder (len, 2, 3);
17560 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17561 0 : builder_push_elems (builder, mask_elems);
17562 :
17563 0 : vec_perm_indices sel (builder, 2, len);
17564 0 : const char *reason;
17565 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17566 :
17567 0 : ASSERT_TRUE (res == NULL_TREE);
17568 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17569 0 : }
17570 :
17571 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17572 : check that the result is not a stepped sequence either, even
17573 : if sel has a stepped sequence. */
17574 0 : {
17575 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17576 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17577 :
17578 0 : vec_perm_builder builder (len, 1, 3);
17579 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17580 0 : builder_push_elems (builder, mask_elems);
17581 :
17582 0 : vec_perm_indices sel (builder, 1, len);
17583 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17584 :
17585 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17586 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17587 0 : }
17588 :
17589 : /* Case 9: If sel doesn't contain a stepped sequence,
17590 : check that the result has same encoding as sel, irrespective
17591 : of shape of input vectors. */
17592 0 : {
17593 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17594 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17595 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17596 :
17597 0 : vec_perm_builder builder (len, 1, 2);
17598 0 : poly_uint64 mask_elems[] = { 0, len };
17599 0 : builder_push_elems (builder, mask_elems);
17600 :
17601 0 : vec_perm_indices sel (builder, 2, len);
17602 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17603 :
17604 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17605 0 : validate_res (sel.encoding ().npatterns (),
17606 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17607 0 : }
17608 : }
17609 0 : }
17610 :
17611 : /* Test all vectors which contain at-least 8 elements. */
17612 :
17613 : static void
17614 0 : test_nunits_min_8 (machine_mode vmode)
17615 : {
17616 0 : for (int i = 0; i < 10; i++)
17617 : {
17618 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17619 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17620 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17621 : arg0[2], arg0[0], arg0[3], arg1[0],
17622 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17623 0 : {
17624 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17625 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17626 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17627 :
17628 0 : vec_perm_builder builder(len, 4, 3);
17629 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17630 0 : 4, 0, 5, len };
17631 0 : builder_push_elems (builder, mask_elems);
17632 :
17633 0 : vec_perm_indices sel (builder, 2, len);
17634 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17635 :
17636 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17637 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17638 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17639 0 : validate_res (4, 3, res, expected_res);
17640 0 : }
17641 : }
17642 0 : }
17643 :
17644 : /* Test vectors for which nunits[0] <= 4. */
17645 :
17646 : static void
17647 0 : test_nunits_max_4 (machine_mode vmode)
17648 : {
17649 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17650 : This should return NULL_TREE because the index 4 may choose
17651 : from either arg0 or arg1 depending on vector length. */
17652 0 : {
17653 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17654 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17655 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17656 :
17657 0 : vec_perm_builder builder (len, 1, 2);
17658 0 : poly_uint64 mask_elems[] = {0, 4};
17659 0 : builder_push_elems (builder, mask_elems);
17660 :
17661 0 : vec_perm_indices sel (builder, 2, len);
17662 0 : const char *reason;
17663 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17664 0 : ASSERT_TRUE (res == NULL_TREE);
17665 0 : ASSERT_TRUE (reason != NULL);
17666 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17667 0 : }
17668 0 : }
17669 :
17670 : #undef ARG0
17671 : #undef ARG1
17672 :
17673 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17674 :
17675 : static bool
17676 0 : is_simple_vla_size (poly_uint64 size)
17677 : {
17678 124 : if (size.is_constant ()
17679 : || !pow2p_hwi (size.coeffs[0]))
17680 0 : return false;
17681 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17682 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17683 : return false;
17684 : return true;
17685 : }
17686 :
17687 : /* Execute fold_vec_perm_cst unit tests. */
17688 :
17689 : static void
17690 4 : test ()
17691 : {
17692 4 : machine_mode vnx4si_mode = E_VOIDmode;
17693 4 : machine_mode v4si_mode = E_VOIDmode;
17694 :
17695 4 : machine_mode vmode;
17696 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17697 : {
17698 : /* Obtain modes corresponding to VNx4SI and V4SI,
17699 : to call mixed mode tests below.
17700 : FIXME: Is there a better way to do this ? */
17701 124 : if (GET_MODE_INNER (vmode) == SImode)
17702 : {
17703 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17704 124 : if (is_simple_vla_size (nunits)
17705 : && nunits.coeffs[0] == 4)
17706 : vnx4si_mode = vmode;
17707 124 : else if (known_eq (nunits, poly_uint64 (4)))
17708 124 : v4si_mode = vmode;
17709 : }
17710 :
17711 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17712 : || !targetm.vector_mode_supported_p (vmode))
17713 124 : continue;
17714 :
17715 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17716 : test_all_nunits (vmode);
17717 : if (nunits.coeffs[0] >= 2)
17718 : test_nunits_min_2 (vmode);
17719 : if (nunits.coeffs[0] >= 4)
17720 : test_nunits_min_4 (vmode);
17721 : if (nunits.coeffs[0] >= 8)
17722 : test_nunits_min_8 (vmode);
17723 :
17724 : if (nunits.coeffs[0] <= 4)
17725 : test_nunits_max_4 (vmode);
17726 : }
17727 :
17728 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17729 : && targetm.vector_mode_supported_p (vnx4si_mode)
17730 : && targetm.vector_mode_supported_p (v4si_mode))
17731 : {
17732 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17733 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17734 : }
17735 4 : }
17736 : } // end of test_fold_vec_perm_cst namespace
17737 :
17738 : /* Verify that various binary operations on vectors are folded
17739 : correctly. */
17740 :
17741 : static void
17742 4 : test_vector_folding ()
17743 : {
17744 4 : tree inner_type = integer_type_node;
17745 4 : tree type = build_vector_type (inner_type, 4);
17746 4 : tree zero = build_zero_cst (type);
17747 4 : tree one = build_one_cst (type);
17748 4 : tree index = build_index_vector (type, 0, 1);
17749 :
17750 : /* Verify equality tests that return a scalar boolean result. */
17751 4 : tree res_type = boolean_type_node;
17752 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17753 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17754 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17755 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17756 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17757 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17758 : index, one)));
17759 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17760 : index, index)));
17761 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17762 : index, index)));
17763 4 : }
17764 :
17765 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17766 :
17767 : static void
17768 4 : test_vec_duplicate_folding ()
17769 : {
17770 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17771 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17772 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17773 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17774 :
17775 4 : tree type = build_vector_type (ssizetype, nunits);
17776 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17777 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17778 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17779 4 : }
17780 :
17781 : /* Verify folding of VEC_UNPACK_{LO,HI}_EXPRs. */
17782 :
17783 : static void
17784 4 : test_vec_unpack_folding ()
17785 : {
17786 4 : machine_mode vmode;
17787 0 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17788 : {
17789 0 : poly_uint64 in_nelts = GET_MODE_NUNITS (vmode);
17790 0 : if (!test_fold_vec_perm_cst::is_simple_vla_size (in_nelts)
17791 : || in_nelts.coeffs[0] < 2
17792 : || !targetm.vector_mode_supported_p (vmode))
17793 0 : continue;
17794 :
17795 : unsigned int in_bits = GET_MODE_UNIT_BITSIZE (vmode);
17796 : if (in_bits >= HOST_BITS_PER_WIDE_INT)
17797 : continue;
17798 :
17799 : unsigned int min_out_nelts = in_nelts.coeffs[0] / 2;
17800 : tree in_inner_type = lang_hooks.types.type_for_mode
17801 : (GET_MODE_INNER (vmode), 1);
17802 : tree out_inner_type = build_nonstandard_integer_type (in_bits * 2, 1);
17803 : tree in_type = build_vector_type_for_mode (in_inner_type, vmode);
17804 : poly_uint64 out_nelts = exact_div (in_nelts, 2);
17805 : tree out_type = build_vector_type (out_inner_type, out_nelts);
17806 :
17807 : tree_vector_builder builder (in_type, min_out_nelts, 2);
17808 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17809 : builder.quick_push (build_int_cst (in_inner_type, i + 1));
17810 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17811 : builder.quick_push (build_int_cst (in_inner_type, 77));
17812 : tree arg = builder.build ();
17813 :
17814 : /* Check integer unpacking with a duplicated scalable high half. */
17815 : tree lo = const_unop (VEC_UNPACK_LO_EXPR, out_type, arg);
17816 : ASSERT_TRUE (lo != NULL_TREE);
17817 : tree hi = const_unop (VEC_UNPACK_HI_EXPR, out_type, arg);
17818 : ASSERT_TRUE (hi != NULL_TREE);
17819 :
17820 : tree prefix = (!BYTES_BIG_ENDIAN ? lo : hi);
17821 : tree dup = (!BYTES_BIG_ENDIAN ? hi : lo);
17822 :
17823 : ASSERT_EQ (VECTOR_CST, TREE_CODE (prefix));
17824 : ASSERT_EQ (VECTOR_CST, TREE_CODE (dup));
17825 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17826 : VECTOR_CST_NELTS (prefix));
17827 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17828 : VECTOR_CST_NELTS (dup));
17829 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (prefix));
17830 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (prefix));
17831 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (dup));
17832 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (dup));
17833 :
17834 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17835 : {
17836 : tree elt = build_int_cst (out_inner_type, i + 1);
17837 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (prefix, i),
17838 : elt, 0));
17839 : tree fill = build_int_cst (out_inner_type, 77);
17840 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17841 : (prefix, min_out_nelts + i),
17842 : fill, 0));
17843 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (dup, i),
17844 : fill, 0));
17845 : }
17846 :
17847 : /* Check int-to-float unpacking with the same scalable shape. */
17848 : tree float_out_type = build_vector_type (float_type_node, out_nelts);
17849 : tree float_lo = const_unop (VEC_UNPACK_FLOAT_LO_EXPR, float_out_type,
17850 : arg);
17851 : ASSERT_TRUE (float_lo != NULL_TREE);
17852 : tree float_hi = const_unop (VEC_UNPACK_FLOAT_HI_EXPR, float_out_type,
17853 : arg);
17854 : ASSERT_TRUE (float_hi != NULL_TREE);
17855 :
17856 : tree float_prefix = (!BYTES_BIG_ENDIAN ? float_lo : float_hi);
17857 : tree float_dup = (!BYTES_BIG_ENDIAN ? float_hi : float_lo);
17858 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_prefix));
17859 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_dup));
17860 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (float_prefix));
17861 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (float_prefix));
17862 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (float_dup));
17863 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (float_dup));
17864 :
17865 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17866 : {
17867 : tree elt = fold_convert_const (FLOAT_EXPR, float_type_node,
17868 : build_int_cst (in_inner_type, i + 1));
17869 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_prefix, i),
17870 : elt, 0));
17871 : tree fill = fold_convert_const (FLOAT_EXPR, float_type_node,
17872 : build_int_cst (in_inner_type, 77));
17873 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17874 : (float_prefix, min_out_nelts + i),
17875 : fill, 0));
17876 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_dup, i),
17877 : fill, 0));
17878 : }
17879 :
17880 : /* Check float-to-int unpacking with the same scalable shape. */
17881 : tree float_in_type = build_vector_type (float_type_node, in_nelts);
17882 : tree_vector_builder fix_builder (float_in_type, min_out_nelts, 2);
17883 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17884 : fix_builder.quick_push
17885 : (build_real_from_int_cst (float_type_node,
17886 : build_int_cst (integer_type_node, i + 1)));
17887 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17888 : fix_builder.quick_push
17889 : (build_real_from_int_cst (float_type_node,
17890 : build_int_cst (integer_type_node, 77)));
17891 : tree fix_arg = fix_builder.build ();
17892 : tree fix_out_type = build_vector_type (integer_type_node, out_nelts);
17893 : tree fix_lo = const_unop (VEC_UNPACK_FIX_TRUNC_LO_EXPR, fix_out_type,
17894 : fix_arg);
17895 : ASSERT_TRUE (fix_lo != NULL_TREE);
17896 : tree fix_hi = const_unop (VEC_UNPACK_FIX_TRUNC_HI_EXPR, fix_out_type,
17897 : fix_arg);
17898 : ASSERT_TRUE (fix_hi != NULL_TREE);
17899 :
17900 : tree fix_prefix = (!BYTES_BIG_ENDIAN ? fix_lo : fix_hi);
17901 : tree fix_dup = (!BYTES_BIG_ENDIAN ? fix_hi : fix_lo);
17902 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_prefix));
17903 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_dup));
17904 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (fix_prefix));
17905 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (fix_prefix));
17906 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (fix_dup));
17907 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (fix_dup));
17908 :
17909 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17910 : {
17911 : tree elt = build_int_cst (integer_type_node, i + 1);
17912 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_prefix, i),
17913 : elt, 0));
17914 : tree fill = build_int_cst (integer_type_node, 77);
17915 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17916 : (fix_prefix, min_out_nelts + i),
17917 : fill, 0));
17918 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_dup, i),
17919 : fill, 0));
17920 : }
17921 :
17922 : /* Reject scalable constants whose high half is not duplicated. */
17923 : tree_vector_builder varied (in_type, in_nelts.coeffs[0], 2);
17924 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
17925 : varied.quick_push (build_int_cst (in_inner_type, i + 1));
17926 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
17927 : varied.quick_push (build_int_cst (in_inner_type, 100 + i));
17928 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
17929 : varied.build ()));
17930 :
17931 : /* Reject stepped scalable constants. */
17932 : tree_vector_builder stepped (in_type, 1, 3);
17933 : stepped.quick_push (build_int_cst (in_inner_type, 1));
17934 : stepped.quick_push (build_int_cst (in_inner_type, 2));
17935 : stepped.quick_push (build_int_cst (in_inner_type, 3));
17936 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
17937 : stepped.build ()));
17938 :
17939 : return;
17940 : }
17941 : }
17942 :
17943 : /* Run all of the selftests within this file. */
17944 :
17945 : void
17946 4 : fold_const_cc_tests ()
17947 : {
17948 4 : test_arithmetic_folding ();
17949 4 : test_vector_folding ();
17950 4 : test_vec_duplicate_folding ();
17951 4 : test_vec_unpack_folding ();
17952 4 : test_fold_vec_perm_cst::test ();
17953 4 : test_operand_equality::test ();
17954 4 : }
17955 :
17956 : } // namespace selftest
17957 :
17958 : #endif /* CHECKING_P */
|