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 363 : operator ~(comparison_code cmp)
132 : {
133 363 : unsigned char newcmp = cmp;
134 363 : newcmp = ~newcmp & 0xF;
135 363 : return (comparison_code)newcmp;
136 : }
137 :
138 : // Implements bitwise ior on comparison_code.
139 : comparison_code
140 12892 : operator |(comparison_code cmp0, comparison_code cmp1)
141 : {
142 12892 : unsigned char newcmp = ((unsigned char)cmp0) | cmp1;
143 12892 : newcmp = newcmp & 0xF;
144 12892 : return (comparison_code)newcmp;
145 : }
146 :
147 : // Implements bitwise and on comparison_code.
148 : comparison_code
149 65796 : operator &(comparison_code cmp0, comparison_code cmp1)
150 : {
151 65796 : unsigned char newcmp = ((unsigned char)cmp0) & cmp1;
152 65796 : newcmp = newcmp & 0xF;
153 65796 : return (comparison_code)newcmp;
154 : }
155 :
156 : // Implements bitwise xor on comparison_code.
157 : comparison_code
158 1411 : operator ^(comparison_code cmp0, comparison_code cmp1)
159 : {
160 1411 : unsigned char newcmp = ((unsigned char)cmp0) ^ cmp1;
161 1411 : newcmp = newcmp & 0xF;
162 1411 : 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 142389 : minmax_from_comparison (tree_code cmp, tree exp0,
200 : const widest_int &cst1,
201 : const widest_int &cst2)
202 : {
203 142389 : if (cst1 == cst2)
204 : {
205 133 : if (cmp == LE_EXPR || cmp == LT_EXPR)
206 : return MIN_EXPR;
207 114 : if (cmp == GT_EXPR || cmp == GE_EXPR)
208 : return MAX_EXPR;
209 : }
210 142370 : if (cst1 == cst2 - 1)
211 : {
212 : /* X <= Y - 1 equals to X < Y. */
213 85273 : if (cmp == LE_EXPR)
214 : return MIN_EXPR;
215 : /* X > Y - 1 equals to X >= Y. */
216 84818 : 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 72762 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
220 : {
221 20708 : int_range_max r;
222 41416 : get_range_query (cfun)->range_of_expr (r, exp0);
223 20708 : if (r.undefined_p ())
224 0 : r.set_varying (TREE_TYPE (exp0));
225 :
226 20708 : widest_int min = widest_int::from (r.lower_bound (),
227 41416 : TYPE_SIGN (TREE_TYPE (exp0)));
228 20708 : if (min == cst1)
229 502 : return MAX_EXPR;
230 20708 : }
231 : }
232 129357 : if (cst1 == cst2 + 1)
233 : {
234 : /* X < Y + 1 equals to X <= Y. */
235 1293 : if (cmp == LT_EXPR)
236 : return MIN_EXPR;
237 : /* X >= Y + 1 equals to X > Y. */
238 1265 : 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 1102 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
242 : {
243 694 : int_range_max r;
244 1388 : get_range_query (cfun)->range_of_expr (r, exp0);
245 694 : if (r.undefined_p ())
246 0 : r.set_varying (TREE_TYPE (exp0));
247 :
248 694 : widest_int max = widest_int::from (r.upper_bound (),
249 1388 : TYPE_SIGN (TREE_TYPE (exp0)));
250 694 : if (max == cst1)
251 188 : return MIN_EXPR;
252 694 : }
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 174855 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
262 : {
263 174855 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
264 : return ERROR_MARK;
265 :
266 174844 : if (!operand_equal_p (exp0, exp2))
267 : return ERROR_MARK;
268 :
269 174844 : if (operand_equal_p (exp1, exp3))
270 : {
271 32378 : if (cmp == LT_EXPR || cmp == LE_EXPR)
272 : return MIN_EXPR;
273 30192 : if (cmp == GT_EXPR || cmp == GE_EXPR)
274 : return MAX_EXPR;
275 : }
276 142580 : if (TREE_CODE (exp3) == INTEGER_CST
277 142096 : && TREE_CODE (exp1) == INTEGER_CST)
278 141640 : 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 3044236 : expr_location_or (tree t, location_t loc)
287 : {
288 934771 : location_t tloc = EXPR_LOCATION (t);
289 3027848 : 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 9437465 : protected_set_expr_location_unshare (tree x, location_t loc)
297 : {
298 9437465 : if (CAN_HAVE_LOCATION_P (x)
299 8385591 : && EXPR_LOCATION (x) != loc
300 2678576 : && !(TREE_CODE (x) == SAVE_EXPR
301 1339503 : || TREE_CODE (x) == TARGET_EXPR
302 : || TREE_CODE (x) == BIND_EXPR))
303 : {
304 1338737 : x = copy_node (x);
305 1338737 : SET_EXPR_LOCATION (x, loc);
306 : }
307 9437465 : 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 2175748 : negate_mathfn_p (combined_fn fn)
315 : {
316 2175748 : 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 2171556 : default:
386 2171556 : break;
387 : }
388 2171556 : return false;
389 : }
390 :
391 : /* Check whether we may negate an integer constant T without causing
392 : overflow. */
393 :
394 : bool
395 3128367 : may_negate_without_overflow_p (const_tree t)
396 : {
397 3128367 : tree type;
398 :
399 3128367 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
400 :
401 3128367 : type = TREE_TYPE (t);
402 3128367 : if (TYPE_UNSIGNED (type))
403 : return false;
404 :
405 3128367 : 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 28044800 : negate_expr_p (tree t)
413 : {
414 28204208 : tree type;
415 :
416 28204208 : if (t == 0)
417 : return false;
418 :
419 28204208 : type = TREE_TYPE (t);
420 :
421 28204208 : STRIP_SIGN_NOPS (t);
422 28204208 : switch (TREE_CODE (t))
423 : {
424 1637347 : case INTEGER_CST:
425 1637347 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
426 : return true;
427 :
428 : /* Check that -CST will not overflow type. */
429 378406 : 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 1299792 : case REAL_CST:
441 : /* We want to canonicalize to positive real constants. Pretend
442 : that only negative ones can be easily negated. */
443 1299792 : 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 1568255 : case PLUS_EXPR:
471 1568255 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
472 1568249 : || HONOR_SIGNED_ZEROS (type)
473 2847795 : || (ANY_INTEGRAL_TYPE_P (type)
474 1279332 : && ! TYPE_OVERFLOW_WRAPS (type)))
475 : return false;
476 : /* -(A + B) -> (-B) - A. */
477 811477 : if (negate_expr_p (TREE_OPERAND (t, 1)))
478 : return true;
479 : /* -(A + B) -> (-A) - B. */
480 148870 : return negate_expr_p (TREE_OPERAND (t, 0));
481 :
482 261682 : case MINUS_EXPR:
483 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
484 261682 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
485 261682 : && !HONOR_SIGNED_ZEROS (type)
486 348969 : && (! ANY_INTEGRAL_TYPE_P (type)
487 87064 : || TYPE_OVERFLOW_WRAPS (type));
488 :
489 2360708 : case MULT_EXPR:
490 2360708 : 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 4113246 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
495 153172 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
496 2207492 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
497 0 : && (wi::popcount
498 2056623 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
499 150869 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
500 127575 : && (wi::popcount
501 2184198 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
502 : break;
503 :
504 : /* Fall through. */
505 :
506 2336685 : case RDIV_EXPR:
507 2336685 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
508 2336684 : return negate_expr_p (TREE_OPERAND (t, 1))
509 2336684 : || negate_expr_p (TREE_OPERAND (t, 0));
510 : break;
511 :
512 2604 : case TRUNC_DIV_EXPR:
513 2604 : case ROUND_DIV_EXPR:
514 2604 : case EXACT_DIV_EXPR:
515 2604 : 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 5151690 : case NOP_EXPR:
533 : /* Negate -((double)float) as (double)(-float). */
534 5151690 : if (SCALAR_FLOAT_TYPE_P (type))
535 : {
536 10287 : tree tem = strip_float_extensions (t);
537 10287 : if (tem != t)
538 : return negate_expr_p (tem);
539 : }
540 : break;
541 :
542 1089971 : case CALL_EXPR:
543 : /* Negate -f(x) as f(-x). */
544 1089971 : if (negate_mathfn_p (get_call_combined_fn (t)))
545 67 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
546 : break;
547 :
548 13032 : case RSHIFT_EXPR:
549 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
550 13032 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
551 : {
552 12887 : tree op1 = TREE_OPERAND (t, 1);
553 12887 : 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 40382241 : fold_negate_expr_1 (location_t loc, tree t)
571 : {
572 40382241 : tree type = TREE_TYPE (t);
573 40382241 : tree tem;
574 :
575 40382241 : 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 31117860 : case INTEGER_CST:
585 31117860 : tem = fold_negate_const (t, type);
586 31117860 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
587 9967 : || (ANY_INTEGRAL_TYPE_P (type)
588 9967 : && !TYPE_OVERFLOW_TRAPS (type)
589 9967 : && TYPE_OVERFLOW_WRAPS (type))
590 31127497 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
591 31117748 : return tem;
592 : break;
593 :
594 2029652 : case POLY_INT_CST:
595 2029652 : case REAL_CST:
596 2029652 : case FIXED_CST:
597 2029652 : tem = fold_negate_const (t, type);
598 2029652 : return tem;
599 :
600 63324 : case COMPLEX_CST:
601 63324 : {
602 63324 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
603 63324 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
604 63324 : if (rpart && ipart)
605 63324 : return build_complex (type, rpart, ipart);
606 : }
607 : break;
608 :
609 52100 : case VECTOR_CST:
610 52100 : {
611 52100 : tree_vector_builder elts;
612 52100 : elts.new_unary_operation (type, t, true);
613 52100 : unsigned int count = elts.encoded_nelts ();
614 127626 : for (unsigned int i = 0; i < count; ++i)
615 : {
616 75526 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
617 75526 : if (elt == NULL_TREE)
618 0 : return NULL_TREE;
619 75526 : elts.quick_push (elt);
620 : }
621 :
622 52100 : return elts.build ();
623 52100 : }
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 725449 : case PLUS_EXPR:
644 725449 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
645 725449 : && !HONOR_SIGNED_ZEROS (type))
646 : {
647 : /* -(A + B) -> (-B) - A. */
648 725387 : if (negate_expr_p (TREE_OPERAND (t, 1)))
649 : {
650 666619 : tem = negate_expr (TREE_OPERAND (t, 1));
651 666619 : return fold_build2_loc (loc, MINUS_EXPR, type,
652 1333238 : tem, TREE_OPERAND (t, 0));
653 : }
654 :
655 : /* -(A + B) -> (-A) - B. */
656 58768 : if (negate_expr_p (TREE_OPERAND (t, 0)))
657 : {
658 1117 : tem = negate_expr (TREE_OPERAND (t, 0));
659 1117 : return fold_build2_loc (loc, MINUS_EXPR, type,
660 2234 : tem, TREE_OPERAND (t, 1));
661 : }
662 : }
663 : break;
664 :
665 158753 : case MINUS_EXPR:
666 : /* - (A - B) -> B - A */
667 158753 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
668 158753 : && !HONOR_SIGNED_ZEROS (type))
669 81524 : return fold_build2_loc (loc, MINUS_EXPR, type,
670 163048 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
671 : break;
672 :
673 265124 : case MULT_EXPR:
674 265124 : if (TYPE_UNSIGNED (type))
675 : break;
676 :
677 : /* Fall through. */
678 :
679 32464 : case RDIV_EXPR:
680 32464 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
681 : {
682 32464 : tem = TREE_OPERAND (t, 1);
683 32464 : if (negate_expr_p (tem))
684 58608 : return fold_build2_loc (loc, TREE_CODE (t), type,
685 58608 : TREE_OPERAND (t, 0), negate_expr (tem));
686 3160 : tem = TREE_OPERAND (t, 0);
687 3160 : 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 2085 : case TRUNC_DIV_EXPR:
694 2085 : case ROUND_DIV_EXPR:
695 2085 : case EXACT_DIV_EXPR:
696 2085 : 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 2389416 : case NOP_EXPR:
719 : /* Convert -((double)float) into (double)(-float). */
720 2389416 : if (SCALAR_FLOAT_TYPE_P (type))
721 : {
722 10895 : tem = strip_float_extensions (t);
723 10895 : if (tem != t && negate_expr_p (tem))
724 0 : return fold_convert_loc (loc, type, negate_expr (tem));
725 : }
726 : break;
727 :
728 296477 : case CALL_EXPR:
729 : /* Negate -f(x) as f(-x). */
730 296477 : if (negate_mathfn_p (get_call_combined_fn (t))
731 297766 : && 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 12740 : case RSHIFT_EXPR:
742 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
743 12740 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
744 : {
745 12722 : tree op1 = TREE_OPERAND (t, 1);
746 12722 : if (wi::to_wide (op1) == element_precision (type) - 1)
747 : {
748 12382 : tree ntype = TYPE_UNSIGNED (type)
749 12382 : ? signed_type_for (type)
750 72 : : unsigned_type_for (type);
751 12382 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
752 12382 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
753 12382 : 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 40382241 : fold_negate_expr (location_t loc, tree t)
769 : {
770 40382241 : tree type = TREE_TYPE (t);
771 40382241 : STRIP_SIGN_NOPS (t);
772 40382241 : tree tem = fold_negate_expr_1 (loc, t);
773 40382241 : if (tem == NULL_TREE)
774 : return NULL_TREE;
775 34057105 : 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 3903434 : negate_expr (tree t)
784 : {
785 3903434 : tree type, tem;
786 3903434 : location_t loc;
787 :
788 3903434 : if (t == NULL_TREE)
789 : return NULL_TREE;
790 :
791 3903434 : loc = EXPR_LOCATION (t);
792 3903434 : type = TREE_TYPE (t);
793 3903434 : STRIP_SIGN_NOPS (t);
794 :
795 3903434 : tem = fold_negate_expr (loc, t);
796 3903434 : if (!tem)
797 1942002 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
798 3903434 : 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 235533876 : 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 235533876 : tree var = 0;
830 235533876 : *minus_varp = 0;
831 235533876 : *conp = 0;
832 235533876 : *minus_conp = 0;
833 235533876 : *litp = 0;
834 235533876 : *minus_litp = 0;
835 :
836 : /* Strip any conversions that don't change the machine mode or signedness. */
837 235533876 : STRIP_SIGN_NOPS (in);
838 :
839 235533876 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
840 150619403 : || TREE_CODE (in) == FIXED_CST)
841 84914473 : *litp = in;
842 150619403 : else if (TREE_CODE (in) == code
843 150619403 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
844 145932295 : && ! 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 145932295 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
850 61228447 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
851 144209282 : || (code == MINUS_EXPR
852 23665653 : && (TREE_CODE (in) == PLUS_EXPR
853 21770862 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
854 : {
855 8753499 : tree op0 = TREE_OPERAND (in, 0);
856 8753499 : tree op1 = TREE_OPERAND (in, 1);
857 8753499 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
858 8753499 : 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 8753499 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
862 8526714 : || TREE_CODE (op0) == FIXED_CST)
863 226785 : *litp = op0, op0 = 0;
864 8526714 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
865 5686617 : || TREE_CODE (op1) == FIXED_CST)
866 2840097 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
867 :
868 8753499 : if (op0 != 0 && TREE_CONSTANT (op0))
869 13730 : *conp = op0, op0 = 0;
870 8739769 : else if (op1 != 0 && TREE_CONSTANT (op1))
871 51285 : *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 8753499 : if (op0 != 0 && op1 != 0)
876 : var = in;
877 3125483 : else if (op0 != 0)
878 : var = op0;
879 : else
880 240515 : var = op1, neg_var_p = neg1_p;
881 :
882 : /* Now do any needed negations. */
883 8753499 : if (neg_litp_p)
884 28834 : *minus_litp = *litp, *litp = 0;
885 8753499 : if (neg_conp_p && *conp)
886 11378 : *minus_conp = *conp, *conp = 0;
887 8753499 : if (neg_var_p && var)
888 230815 : *minus_varp = var, var = 0;
889 : }
890 141865904 : else if (TREE_CONSTANT (in))
891 822642 : *conp = in;
892 141043262 : else if (TREE_CODE (in) == BIT_NOT_EXPR
893 546150 : && code == PLUS_EXPR)
894 : {
895 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
896 : when IN is constant. */
897 385447 : *litp = build_minus_one_cst (type);
898 385447 : *minus_varp = TREE_OPERAND (in, 0);
899 : }
900 : else
901 : var = in;
902 :
903 235533876 : if (negate_p)
904 : {
905 13050578 : if (*litp)
906 1293566 : *minus_litp = *litp, *litp = 0;
907 11757012 : else if (*minus_litp)
908 174 : *litp = *minus_litp, *minus_litp = 0;
909 13050578 : if (*conp)
910 46268 : *minus_conp = *conp, *conp = 0;
911 13004310 : else if (*minus_conp)
912 0 : *conp = *minus_conp, *minus_conp = 0;
913 13050578 : if (var)
914 12991797 : *minus_varp = var, var = 0;
915 58781 : else if (*minus_varp)
916 889 : var = *minus_varp, *minus_varp = 0;
917 : }
918 :
919 235533876 : if (*litp
920 235533876 : && TREE_OVERFLOW_P (*litp))
921 20560 : *litp = drop_tree_overflow (*litp);
922 235533876 : if (*minus_litp
923 235533876 : && TREE_OVERFLOW_P (*minus_litp))
924 24 : *minus_litp = drop_tree_overflow (*minus_litp);
925 :
926 235533876 : 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 21408133 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
936 : {
937 21408133 : if (t1 == 0)
938 : {
939 13588378 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
940 : return t2;
941 : }
942 7819755 : 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 4365027 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
949 3430287 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
950 3363759 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
951 : {
952 1705105 : if (code == PLUS_EXPR)
953 : {
954 947383 : 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 947324 : 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 947323 : else if (integer_zerop (t2))
965 37149 : return fold_convert_loc (loc, type, t1);
966 : }
967 757722 : else if (code == MINUS_EXPR)
968 : {
969 734061 : if (integer_zerop (t2))
970 0 : return fold_convert_loc (loc, type, t1);
971 : }
972 :
973 1667896 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
974 1667896 : fold_convert_loc (loc, type, t2));
975 : }
976 :
977 2659922 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
978 2659922 : 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 2743024395 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
986 : {
987 2743024395 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
988 : return false;
989 2743024395 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
990 : return false;
991 :
992 2743024395 : switch (code)
993 : {
994 : case LSHIFT_EXPR:
995 : case RSHIFT_EXPR:
996 : case LROTATE_EXPR:
997 : case RROTATE_EXPR:
998 : return true;
999 :
1000 2743024395 : default:
1001 2743024395 : break;
1002 : }
1003 :
1004 2743024395 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1005 2743024395 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1006 5486048790 : && 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 1530108588 : 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 1530108588 : wide_int tmp;
1019 1530108588 : *overflow = wi::OVF_NONE;
1020 1530108588 : switch (code)
1021 : {
1022 3715539 : case BIT_IOR_EXPR:
1023 3715539 : res = wi::bit_or (arg1, arg2);
1024 3715539 : break;
1025 :
1026 110822 : case BIT_XOR_EXPR:
1027 110822 : res = wi::bit_xor (arg1, arg2);
1028 110822 : break;
1029 :
1030 23622103 : case BIT_AND_EXPR:
1031 23622103 : res = wi::bit_and (arg1, arg2);
1032 23622103 : break;
1033 :
1034 15005642 : case LSHIFT_EXPR:
1035 15005642 : if (wi::neg_p (arg2))
1036 : return false;
1037 14975276 : res = wi::lshift (arg1, arg2);
1038 14975276 : break;
1039 :
1040 7591783 : case RSHIFT_EXPR:
1041 7591783 : 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 7591587 : res = wi::rshift (arg1, arg2, sign);
1047 7591587 : 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 260518558 : case PLUS_EXPR:
1069 260518558 : res = wi::add (arg1, arg2, sign, overflow);
1070 260518558 : break;
1071 :
1072 75766052 : case MINUS_EXPR:
1073 75766052 : res = wi::sub (arg1, arg2, sign, overflow);
1074 75766052 : break;
1075 :
1076 438656186 : case MULT_EXPR:
1077 438656186 : res = wi::mul (arg1, arg2, sign, overflow);
1078 438656186 : break;
1079 :
1080 5780 : case MULT_HIGHPART_EXPR:
1081 5780 : res = wi::mul_high (arg1, arg2, sign);
1082 5780 : break;
1083 :
1084 360931941 : case TRUNC_DIV_EXPR:
1085 360931941 : case EXACT_DIV_EXPR:
1086 360931941 : if (arg2 == 0)
1087 : return false;
1088 360926002 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1089 360926002 : break;
1090 :
1091 81394689 : case FLOOR_DIV_EXPR:
1092 81394689 : if (arg2 == 0)
1093 : return false;
1094 81394689 : res = wi::div_floor (arg1, arg2, sign, overflow);
1095 81394689 : break;
1096 :
1097 88230939 : case CEIL_DIV_EXPR:
1098 88230939 : if (arg2 == 0)
1099 : return false;
1100 88230939 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1101 88230939 : 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 1377651 : case TRUNC_MOD_EXPR:
1110 1377651 : if (arg2 == 0)
1111 : return false;
1112 1376547 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1113 1376547 : break;
1114 :
1115 69509179 : case FLOOR_MOD_EXPR:
1116 69509179 : if (arg2 == 0)
1117 : return false;
1118 69509179 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1119 69509179 : 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 50053 : case MIN_EXPR:
1134 50053 : res = wi::min (arg1, arg2, sign);
1135 50053 : break;
1136 :
1137 103619646 : case MAX_EXPR:
1138 103619646 : res = wi::max (arg1, arg2, sign);
1139 103619646 : break;
1140 :
1141 : default:
1142 : return false;
1143 : }
1144 : return true;
1145 1530108588 : }
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 1530108588 : 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 1530108588 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1176 :
1177 1530108588 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1178 : {
1179 1530108588 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1180 1530108588 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1181 1530108588 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1182 : return NULL_TREE;
1183 1530070856 : res = wi_res;
1184 1530070856 : return true;
1185 1530109012 : }
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 1530108588 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1250 : int overflowable)
1251 : {
1252 1530108588 : poly_wide_int poly_res;
1253 1530108588 : tree type = TREE_TYPE (arg1);
1254 1530108588 : signop sign = TYPE_SIGN (type);
1255 1530108588 : wi::overflow_type overflow = wi::OVF_NONE;
1256 :
1257 1530108588 : if (!poly_int_tree_p (arg1)
1258 1530108588 : || !poly_int_tree_p (arg2)
1259 3060217176 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1260 : return NULL_TREE;
1261 1530070856 : return force_fit_type (type, poly_res, overflowable,
1262 1530070856 : (((sign == SIGNED || overflowable == -1)
1263 1530070856 : && overflow)
1264 1530070856 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1265 1530108588 : }
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 197520 : 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 3965 : default:
1284 3965 : 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 460039 : 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 460039 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1297 : {
1298 376991 : if (integer_zerop (other_op))
1299 : {
1300 28369 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1301 : return op;
1302 27211 : else if (code == BIT_AND_EXPR)
1303 4223 : 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 274774 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1316 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1317 : {
1318 200252 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1319 466521 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1320 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1321 : {
1322 191747 : tree type = TREE_TYPE (arg1);
1323 191747 : bool step_ok_p;
1324 191747 : if (VECTOR_CST_STEPPED_P (arg1)
1325 191747 : && 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 2732 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1335 189015 : 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 53247 : step_ok_p = distributes_over_addition_p (code, 1);
1344 : else
1345 : /* Similarly in reverse. */
1346 135768 : step_ok_p = distributes_over_addition_p (code, 2);
1347 191747 : tree_vector_builder elts;
1348 191747 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1349 : return NULL_TREE;
1350 191747 : unsigned int count = elts.encoded_nelts ();
1351 741404 : for (unsigned int i = 0; i < count; ++i)
1352 : {
1353 549976 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1354 549976 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1355 :
1356 549976 : 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 549976 : if (elt == NULL_TREE)
1361 319 : return NULL_TREE;
1362 549657 : elts.quick_push (elt);
1363 : }
1364 :
1365 191428 : return elts.build ();
1366 191747 : }
1367 :
1368 83027 : if (TREE_CODE (arg1) == VECTOR_CST
1369 8505 : && TREE_CODE (arg2) == INTEGER_CST)
1370 : {
1371 8505 : tree type = TREE_TYPE (arg1);
1372 8505 : bool step_ok_p = distributes_over_addition_p (code, 1);
1373 8505 : tree_vector_builder elts;
1374 8505 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1375 : return NULL_TREE;
1376 8505 : unsigned int count = elts.encoded_nelts ();
1377 36343 : for (unsigned int i = 0; i < count; ++i)
1378 : {
1379 27925 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1380 :
1381 27925 : 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 27925 : if (elt == NULL_TREE)
1386 87 : return NULL_TREE;
1387 27838 : elts.quick_push (elt);
1388 : }
1389 :
1390 8418 : return elts.build ();
1391 8505 : }
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 226010443 : const_binop (enum tree_code code, tree arg1, tree arg2)
1402 : {
1403 : /* Sanity check for the recursive cases. */
1404 226010443 : if (!arg1 || !arg2)
1405 : return NULL_TREE;
1406 :
1407 226009179 : STRIP_NOPS (arg1);
1408 226009179 : STRIP_NOPS (arg2);
1409 :
1410 226009179 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1411 : {
1412 220178647 : if (code == POINTER_PLUS_EXPR)
1413 103813 : return int_const_binop (PLUS_EXPR,
1414 207626 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1415 :
1416 220074834 : return int_const_binop (code, arg1, arg2);
1417 : }
1418 :
1419 5830532 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1420 : {
1421 5539285 : machine_mode mode;
1422 5539285 : REAL_VALUE_TYPE d1;
1423 5539285 : REAL_VALUE_TYPE d2;
1424 5539285 : REAL_VALUE_TYPE value;
1425 5539285 : REAL_VALUE_TYPE result;
1426 5539285 : bool inexact;
1427 5539285 : tree t, type;
1428 :
1429 : /* The following codes are handled by real_arithmetic. */
1430 5539285 : switch (code)
1431 : {
1432 5539285 : case PLUS_EXPR:
1433 5539285 : case MINUS_EXPR:
1434 5539285 : case MULT_EXPR:
1435 5539285 : case RDIV_EXPR:
1436 5539285 : case MIN_EXPR:
1437 5539285 : case MAX_EXPR:
1438 5539285 : break;
1439 :
1440 : default:
1441 : return NULL_TREE;
1442 : }
1443 :
1444 5539285 : d1 = TREE_REAL_CST (arg1);
1445 5539285 : d2 = TREE_REAL_CST (arg2);
1446 :
1447 5539285 : type = TREE_TYPE (arg1);
1448 5539285 : mode = TYPE_MODE (type);
1449 :
1450 : /* Don't perform operation if we honor signaling NaNs and
1451 : either operand is a signaling NaN. */
1452 5539285 : if (HONOR_SNANS (mode)
1453 5539285 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1454 7960 : || 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 5539252 : if (code == RDIV_EXPR
1460 2407138 : && real_equal (&d2, &dconst0)
1461 5549921 : && (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 5531495 : 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 5531149 : 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 5531089 : inexact = real_arithmetic (&value, code, &d1, &d2);
1484 5531089 : 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 5531089 : if (flag_trapping_math
1491 21478042 : && MODE_HAS_NANS (mode)
1492 5511059 : && REAL_VALUE_ISNAN (result)
1493 2583 : && !REAL_VALUE_ISNAN (d1)
1494 5533672 : && !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 5528506 : if (flag_trapping_math
1500 21468052 : && MODE_HAS_INFINITIES (mode)
1501 5508476 : && REAL_VALUE_ISINF (result)
1502 7846 : && !REAL_VALUE_ISINF (d1)
1503 5535684 : && !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. When the
1510 : result is known to be exactly zero, rounding may affect
1511 : the sign for modes that support signed zeros. */
1512 5523611 : if ((flag_rounding_math
1513 37515220 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1514 5523611 : && (inexact
1515 2649 : || !real_identical (&result, &value)
1516 2185 : || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
1517 : return NULL_TREE;
1518 :
1519 5520754 : t = build_real (type, result);
1520 :
1521 5520754 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1522 5520754 : return t;
1523 : }
1524 :
1525 291247 : if (TREE_CODE (arg1) == FIXED_CST)
1526 : {
1527 0 : FIXED_VALUE_TYPE f1;
1528 0 : FIXED_VALUE_TYPE f2;
1529 0 : FIXED_VALUE_TYPE result;
1530 0 : tree t, type;
1531 0 : bool sat_p;
1532 0 : bool overflow_p;
1533 :
1534 : /* The following codes are handled by fixed_arithmetic. */
1535 0 : switch (code)
1536 : {
1537 0 : case PLUS_EXPR:
1538 0 : case MINUS_EXPR:
1539 0 : case MULT_EXPR:
1540 0 : case TRUNC_DIV_EXPR:
1541 0 : if (TREE_CODE (arg2) != FIXED_CST)
1542 : return NULL_TREE;
1543 0 : f2 = TREE_FIXED_CST (arg2);
1544 0 : break;
1545 :
1546 0 : case LSHIFT_EXPR:
1547 0 : case RSHIFT_EXPR:
1548 0 : {
1549 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1550 0 : return NULL_TREE;
1551 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1552 0 : f2.data.high = w2.elt (1);
1553 0 : f2.data.low = w2.ulow ();
1554 0 : f2.mode = SImode;
1555 : }
1556 0 : break;
1557 :
1558 : default:
1559 : return NULL_TREE;
1560 : }
1561 :
1562 0 : f1 = TREE_FIXED_CST (arg1);
1563 0 : type = TREE_TYPE (arg1);
1564 0 : sat_p = TYPE_SATURATING (type);
1565 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1566 0 : t = build_fixed (type, result);
1567 : /* Propagate overflow flags. */
1568 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1569 0 : TREE_OVERFLOW (t) = 1;
1570 : return t;
1571 : }
1572 :
1573 291247 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1574 : {
1575 11250 : tree type = TREE_TYPE (arg1);
1576 11250 : tree r1 = TREE_REALPART (arg1);
1577 11250 : tree i1 = TREE_IMAGPART (arg1);
1578 11250 : tree r2 = TREE_REALPART (arg2);
1579 11250 : tree i2 = TREE_IMAGPART (arg2);
1580 11250 : tree real, imag;
1581 :
1582 11250 : switch (code)
1583 : {
1584 5319 : case PLUS_EXPR:
1585 5319 : case MINUS_EXPR:
1586 5319 : real = const_binop (code, r1, r2);
1587 5319 : imag = const_binop (code, i1, i2);
1588 5319 : break;
1589 :
1590 3971 : case MULT_EXPR:
1591 3971 : if (COMPLEX_FLOAT_TYPE_P (type))
1592 2819 : return do_mpc_arg2 (arg1, arg2, type,
1593 : /* do_nonfinite= */ folding_initializer,
1594 2819 : mpc_mul);
1595 :
1596 1152 : real = const_binop (MINUS_EXPR,
1597 : const_binop (MULT_EXPR, r1, r2),
1598 : const_binop (MULT_EXPR, i1, i2));
1599 1152 : imag = const_binop (PLUS_EXPR,
1600 : const_binop (MULT_EXPR, r1, i2),
1601 : const_binop (MULT_EXPR, i1, r2));
1602 1152 : break;
1603 :
1604 1706 : case RDIV_EXPR:
1605 1706 : if (COMPLEX_FLOAT_TYPE_P (type))
1606 1706 : return do_mpc_arg2 (arg1, arg2, type,
1607 : /* do_nonfinite= */ folding_initializer,
1608 1706 : mpc_div);
1609 : /* Fallthru. */
1610 254 : case TRUNC_DIV_EXPR:
1611 254 : case CEIL_DIV_EXPR:
1612 254 : case FLOOR_DIV_EXPR:
1613 254 : case ROUND_DIV_EXPR:
1614 254 : if (flag_complex_method == 0)
1615 : {
1616 : /* Keep this algorithm in sync with
1617 : tree-complex.cc:expand_complex_div_straight().
1618 :
1619 : Expand complex division to scalars, straightforward algorithm.
1620 : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1621 : t = br*br + bi*bi
1622 : */
1623 0 : tree magsquared
1624 0 : = const_binop (PLUS_EXPR,
1625 : const_binop (MULT_EXPR, r2, r2),
1626 : const_binop (MULT_EXPR, i2, i2));
1627 0 : tree t1
1628 0 : = const_binop (PLUS_EXPR,
1629 : const_binop (MULT_EXPR, r1, r2),
1630 : const_binop (MULT_EXPR, i1, i2));
1631 0 : tree t2
1632 0 : = const_binop (MINUS_EXPR,
1633 : const_binop (MULT_EXPR, i1, r2),
1634 : const_binop (MULT_EXPR, r1, i2));
1635 :
1636 0 : real = const_binop (code, t1, magsquared);
1637 0 : imag = const_binop (code, t2, magsquared);
1638 : }
1639 : else
1640 : {
1641 : /* Keep this algorithm in sync with
1642 : tree-complex.cc:expand_complex_div_wide().
1643 :
1644 : Expand complex division to scalars, modified algorithm to minimize
1645 : overflow with wide input ranges. */
1646 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1647 : fold_abs_const (r2, TREE_TYPE (type)),
1648 : fold_abs_const (i2, TREE_TYPE (type)));
1649 :
1650 254 : if (integer_nonzerop (compare))
1651 : {
1652 : /* In the TRUE branch, we compute
1653 : ratio = br/bi;
1654 : div = (br * ratio) + bi;
1655 : tr = (ar * ratio) + ai;
1656 : ti = (ai * ratio) - ar;
1657 : tr = tr / div;
1658 : ti = ti / div; */
1659 48 : tree ratio = const_binop (code, r2, i2);
1660 48 : tree div = const_binop (PLUS_EXPR, i2,
1661 : const_binop (MULT_EXPR, r2, ratio));
1662 48 : real = const_binop (MULT_EXPR, r1, ratio);
1663 48 : real = const_binop (PLUS_EXPR, real, i1);
1664 48 : real = const_binop (code, real, div);
1665 :
1666 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1667 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1668 48 : imag = const_binop (code, imag, div);
1669 : }
1670 : else
1671 : {
1672 : /* In the FALSE branch, we compute
1673 : ratio = d/c;
1674 : divisor = (d * ratio) + c;
1675 : tr = (b * ratio) + a;
1676 : ti = b - (a * ratio);
1677 : tr = tr / div;
1678 : ti = ti / div; */
1679 206 : tree ratio = const_binop (code, i2, r2);
1680 206 : tree div = const_binop (PLUS_EXPR, r2,
1681 : const_binop (MULT_EXPR, i2, ratio));
1682 :
1683 206 : real = const_binop (MULT_EXPR, i1, ratio);
1684 206 : real = const_binop (PLUS_EXPR, real, r1);
1685 206 : real = const_binop (code, real, div);
1686 :
1687 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1688 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1689 206 : imag = const_binop (code, imag, div);
1690 : }
1691 : }
1692 : break;
1693 :
1694 : default:
1695 : return NULL_TREE;
1696 : }
1697 :
1698 6725 : if (real && imag)
1699 6567 : return build_complex (type, real, imag);
1700 : }
1701 :
1702 280155 : tree simplified;
1703 280155 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1704 : return simplified;
1705 :
1706 279580 : if (commutative_tree_code (code)
1707 279580 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1708 : return simplified;
1709 :
1710 274774 : return vector_const_binop (code, arg1, arg2, const_binop);
1711 : }
1712 :
1713 : /* Overload that adds a TYPE parameter to be able to dispatch
1714 : to fold_relational_const. */
1715 :
1716 : tree
1717 302143908 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1718 : {
1719 302143908 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1720 83870957 : return fold_relational_const (code, type, arg1, arg2);
1721 :
1722 : /* ??? Until we make the const_binop worker take the type of the
1723 : result as argument put those cases that need it here. */
1724 218272951 : switch (code)
1725 : {
1726 21 : case VEC_SERIES_EXPR:
1727 21 : if (CONSTANT_CLASS_P (arg1)
1728 21 : && CONSTANT_CLASS_P (arg2))
1729 21 : return build_vec_series (type, arg1, arg2);
1730 : return NULL_TREE;
1731 :
1732 269991 : case COMPLEX_EXPR:
1733 269991 : if ((TREE_CODE (arg1) == REAL_CST
1734 259425 : && TREE_CODE (arg2) == REAL_CST)
1735 10571 : || (TREE_CODE (arg1) == INTEGER_CST
1736 10563 : && TREE_CODE (arg2) == INTEGER_CST))
1737 269983 : return build_complex (type, arg1, arg2);
1738 : return NULL_TREE;
1739 :
1740 180264 : case POINTER_DIFF_EXPR:
1741 180264 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1742 : {
1743 359880 : poly_offset_int res = (wi::to_poly_offset (arg1)
1744 179940 : - wi::to_poly_offset (arg2));
1745 179940 : return force_fit_type (type, res, 1,
1746 179940 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1747 : }
1748 : return NULL_TREE;
1749 :
1750 15579 : case VEC_PACK_TRUNC_EXPR:
1751 15579 : case VEC_PACK_FIX_TRUNC_EXPR:
1752 15579 : case VEC_PACK_FLOAT_EXPR:
1753 15579 : {
1754 15579 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1755 :
1756 15579 : if (TREE_CODE (arg1) != VECTOR_CST
1757 15579 : || TREE_CODE (arg2) != VECTOR_CST)
1758 : return NULL_TREE;
1759 :
1760 15579 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1761 : return NULL_TREE;
1762 :
1763 15579 : out_nelts = in_nelts * 2;
1764 15579 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1765 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1766 :
1767 15579 : tree_vector_builder elts (type, out_nelts, 1);
1768 209366 : for (i = 0; i < out_nelts; i++)
1769 : {
1770 178220 : tree elt = (i < in_nelts
1771 178220 : ? VECTOR_CST_ELT (arg1, i)
1772 89104 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1773 179264 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1774 : ? NOP_EXPR
1775 : : code == VEC_PACK_FLOAT_EXPR
1776 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1777 178220 : TREE_TYPE (type), elt);
1778 178220 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1779 12 : return NULL_TREE;
1780 178208 : elts.quick_push (elt);
1781 : }
1782 :
1783 15567 : return elts.build ();
1784 15579 : }
1785 :
1786 210 : case VEC_WIDEN_MULT_LO_EXPR:
1787 210 : case VEC_WIDEN_MULT_HI_EXPR:
1788 210 : case VEC_WIDEN_MULT_EVEN_EXPR:
1789 210 : case VEC_WIDEN_MULT_ODD_EXPR:
1790 210 : {
1791 210 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1792 :
1793 210 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1794 : return NULL_TREE;
1795 :
1796 210 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1797 : return NULL_TREE;
1798 210 : out_nelts = in_nelts / 2;
1799 210 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1800 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1801 :
1802 210 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1803 : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1804 : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1805 : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1806 : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1807 : scale = 1, ofs = 0;
1808 : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1809 210 : scale = 1, ofs = 1;
1810 :
1811 210 : tree_vector_builder elts (type, out_nelts, 1);
1812 960 : for (out = 0; out < out_nelts; out++)
1813 : {
1814 540 : unsigned int in = (out << scale) + ofs;
1815 540 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1816 : VECTOR_CST_ELT (arg1, in));
1817 540 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1818 : VECTOR_CST_ELT (arg2, in));
1819 :
1820 540 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1821 0 : return NULL_TREE;
1822 540 : tree elt = const_binop (MULT_EXPR, t1, t2);
1823 540 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1824 : return NULL_TREE;
1825 540 : elts.quick_push (elt);
1826 : }
1827 :
1828 210 : return elts.build ();
1829 210 : }
1830 :
1831 217806886 : default:;
1832 : }
1833 :
1834 217806886 : if (TREE_CODE_CLASS (code) != tcc_binary)
1835 : return NULL_TREE;
1836 :
1837 : /* Make sure type and arg0 have the same saturating flag. */
1838 215317556 : gcc_checking_assert (TYPE_SATURATING (type)
1839 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1840 :
1841 215317556 : return const_binop (code, arg1, arg2);
1842 : }
1843 :
1844 : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1845 : Return zero if computing the constants is not possible. */
1846 :
1847 : tree
1848 380054507 : const_unop (enum tree_code code, tree type, tree arg0)
1849 : {
1850 : /* Don't perform the operation, other than NEGATE and ABS, if
1851 : flag_signaling_nans is on and the operand is a signaling NaN. */
1852 380054507 : if (TREE_CODE (arg0) == REAL_CST
1853 11059954 : && HONOR_SNANS (arg0)
1854 17259 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1855 4740 : && code != NEGATE_EXPR
1856 4740 : && code != ABS_EXPR
1857 380059212 : && code != ABSU_EXPR)
1858 : return NULL_TREE;
1859 :
1860 380049802 : switch (code)
1861 : {
1862 284941667 : CASE_CONVERT:
1863 284941667 : case FLOAT_EXPR:
1864 284941667 : case FIX_TRUNC_EXPR:
1865 284941667 : case FIXED_CONVERT_EXPR:
1866 284941667 : return fold_convert_const (code, type, arg0);
1867 :
1868 0 : case ADDR_SPACE_CONVERT_EXPR:
1869 : /* If the source address is 0, and the source address space
1870 : cannot have a valid object at 0, fold to dest type null. */
1871 0 : if (integer_zerop (arg0)
1872 0 : && !(targetm.addr_space.zero_address_valid
1873 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1874 0 : return fold_convert_const (code, type, arg0);
1875 : break;
1876 :
1877 13194930 : case VIEW_CONVERT_EXPR:
1878 13194930 : return fold_view_convert_expr (type, arg0);
1879 :
1880 32037702 : case NEGATE_EXPR:
1881 32037702 : {
1882 : /* Can't call fold_negate_const directly here as that doesn't
1883 : handle all cases and we might not be able to negate some
1884 : constants. */
1885 32037702 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1886 32037702 : if (tem && CONSTANT_CLASS_P (tem))
1887 31891730 : return tem;
1888 : break;
1889 : }
1890 :
1891 40342 : case ABS_EXPR:
1892 40342 : case ABSU_EXPR:
1893 40342 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1894 35641 : return fold_abs_const (arg0, type);
1895 : break;
1896 :
1897 25516 : case CONJ_EXPR:
1898 25516 : if (TREE_CODE (arg0) == COMPLEX_CST)
1899 : {
1900 25513 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1901 25513 : TREE_TYPE (type));
1902 25513 : return build_complex (type, TREE_REALPART (arg0), ipart);
1903 : }
1904 : break;
1905 :
1906 2396198 : case BIT_NOT_EXPR:
1907 2396198 : if (TREE_CODE (arg0) == INTEGER_CST)
1908 2389043 : return fold_not_const (arg0, type);
1909 7155 : else if (POLY_INT_CST_P (arg0))
1910 : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1911 : /* Perform BIT_NOT_EXPR on each element individually. */
1912 7155 : else if (TREE_CODE (arg0) == VECTOR_CST)
1913 : {
1914 6493 : tree elem;
1915 :
1916 : /* This can cope with stepped encodings because ~x == -1 - x. */
1917 6493 : tree_vector_builder elements;
1918 6493 : elements.new_unary_operation (type, arg0, true);
1919 6493 : unsigned int i, count = elements.encoded_nelts ();
1920 25099 : for (i = 0; i < count; ++i)
1921 : {
1922 18606 : elem = VECTOR_CST_ELT (arg0, i);
1923 18606 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1924 18606 : if (elem == NULL_TREE)
1925 : break;
1926 18606 : elements.quick_push (elem);
1927 : }
1928 6493 : if (i == count)
1929 6493 : return elements.build ();
1930 6493 : }
1931 : break;
1932 :
1933 11626522 : case TRUTH_NOT_EXPR:
1934 11626522 : if (TREE_CODE (arg0) == INTEGER_CST)
1935 11259131 : return constant_boolean_node (integer_zerop (arg0), type);
1936 : break;
1937 :
1938 180080 : case REALPART_EXPR:
1939 180080 : if (TREE_CODE (arg0) == COMPLEX_CST)
1940 179879 : return fold_convert (type, TREE_REALPART (arg0));
1941 : break;
1942 :
1943 185867 : case IMAGPART_EXPR:
1944 185867 : if (TREE_CODE (arg0) == COMPLEX_CST)
1945 185679 : return fold_convert (type, TREE_IMAGPART (arg0));
1946 : break;
1947 :
1948 19102 : case VEC_UNPACK_LO_EXPR:
1949 19102 : case VEC_UNPACK_HI_EXPR:
1950 19102 : case VEC_UNPACK_FLOAT_LO_EXPR:
1951 19102 : case VEC_UNPACK_FLOAT_HI_EXPR:
1952 19102 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1953 19102 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1954 19102 : {
1955 19102 : poly_uint64 out_nelts = TYPE_VECTOR_SUBPARTS (type);
1956 19102 : unsigned HOST_WIDE_INT npatterns, nelts_per_pattern, i, offset = 0;
1957 19102 : enum tree_code subcode;
1958 :
1959 19102 : if (TREE_CODE (arg0) != VECTOR_CST)
1960 : return NULL_TREE;
1961 :
1962 19102 : gcc_assert (known_eq (VECTOR_CST_NELTS (arg0), out_nelts * 2));
1963 :
1964 19102 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1965 : subcode = NOP_EXPR;
1966 7998 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1967 7998 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1968 : subcode = FLOAT_EXPR;
1969 : else
1970 4 : subcode = FIX_TRUNC_EXPR;
1971 :
1972 38204 : bool low_p = (code == VEC_UNPACK_LO_EXPR
1973 19102 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1974 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR);
1975 :
1976 19102 : npatterns = VECTOR_CST_NPATTERNS (arg0);
1977 19102 : nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (arg0);
1978 25295 : if (nelts_per_pattern <= 2 && multiple_p (out_nelts, npatterns))
1979 : /* The input is:
1980 :
1981 : { a1,...,an,b1,...,bn,b1,...,bn,b1,...,bn,... }
1982 :
1983 : where N == NPATTERNS and where ai == bi for NELTS_PER_PATTERN == 1
1984 : but not necessarily for NELTS_PER_PATTERN == 2.
1985 :
1986 : When unpacking the first half, the shape of the output is the same
1987 : as the input. That shape would also work for the second half,
1988 : but setting the output nelts_per_patterns to 1 is always enough
1989 : in that case (since the output always starts on b1). */
1990 : ;
1991 : else
1992 : {
1993 : if (!out_nelts.is_constant (&npatterns))
1994 : return NULL_TREE;
1995 : nelts_per_pattern = 1;
1996 : }
1997 :
1998 19102 : if ((!BYTES_BIG_ENDIAN) ^ low_p)
1999 : {
2000 9543 : offset = npatterns;
2001 9543 : nelts_per_pattern = 1;
2002 : }
2003 :
2004 19102 : tree_vector_builder elts (type, npatterns, nelts_per_pattern);
2005 116440 : for (i = 0; i < npatterns * nelts_per_pattern; i++)
2006 : {
2007 78236 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2008 78236 : VECTOR_CST_ELT (arg0, i + offset));
2009 78236 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2010 0 : return NULL_TREE;
2011 78236 : elts.quick_push (elt);
2012 : }
2013 :
2014 19102 : return elts.build ();
2015 19102 : }
2016 :
2017 4 : case VEC_DUPLICATE_EXPR:
2018 4 : if (CONSTANT_CLASS_P (arg0))
2019 4 : return build_vector_from_val (type, arg0);
2020 : return NULL_TREE;
2021 :
2022 : default:
2023 : break;
2024 : }
2025 :
2026 : return NULL_TREE;
2027 : }
2028 :
2029 : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2030 : indicates which particular sizetype to create. */
2031 :
2032 : tree
2033 3630922370 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2034 : {
2035 3630922370 : return build_int_cst (sizetype_tab[(int) kind], number);
2036 : }
2037 :
2038 : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2039 : is a tree code. The type of the result is taken from the operands.
2040 : Both must be equivalent integer types, ala int_binop_types_match_p.
2041 : If the operands are constant, so is the result. */
2042 :
2043 : tree
2044 2704709279 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2045 : {
2046 2704709279 : tree type = TREE_TYPE (arg0);
2047 :
2048 2704709279 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2049 : return error_mark_node;
2050 :
2051 2704709279 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2052 : TREE_TYPE (arg1)));
2053 :
2054 : /* Handle the special case of two poly_int constants faster. */
2055 2704709279 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2056 : {
2057 : /* And some specific cases even faster than that. */
2058 2671439889 : if (code == PLUS_EXPR)
2059 : {
2060 1235437624 : if (integer_zerop (arg0)
2061 1235437624 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2062 : return arg1;
2063 316467754 : if (integer_zerop (arg1)
2064 316467754 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2065 : return arg0;
2066 : }
2067 1436002265 : else if (code == MINUS_EXPR)
2068 : {
2069 122082512 : if (integer_zerop (arg1)
2070 122082512 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2071 : return arg0;
2072 : }
2073 1313919753 : else if (code == MULT_EXPR)
2074 : {
2075 619907032 : if (integer_onep (arg0)
2076 619907032 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2077 : return arg1;
2078 : }
2079 :
2080 : /* Handle general case of two integer constants. For sizetype
2081 : constant calculations we always want to know about overflow,
2082 : even in the unsigned case. */
2083 1287314474 : tree res = int_const_binop (code, arg0, arg1, -1);
2084 1287314474 : if (res != NULL_TREE)
2085 : return res;
2086 : }
2087 :
2088 33269390 : return fold_build2_loc (loc, code, type, arg0, arg1);
2089 : }
2090 :
2091 : /* Given two values, either both of sizetype or both of bitsizetype,
2092 : compute the difference between the two values. Return the value
2093 : in signed type corresponding to the type of the operands. */
2094 :
2095 : tree
2096 38315116 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2097 : {
2098 38315116 : tree type = TREE_TYPE (arg0);
2099 38315116 : tree ctype;
2100 :
2101 38315116 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2102 : TREE_TYPE (arg1)));
2103 :
2104 : /* If the type is already signed, just do the simple thing. */
2105 38315116 : if (!TYPE_UNSIGNED (type))
2106 10302492 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2107 :
2108 28012624 : if (type == sizetype)
2109 28012624 : ctype = ssizetype;
2110 0 : else if (type == bitsizetype)
2111 0 : ctype = sbitsizetype;
2112 : else
2113 0 : ctype = signed_type_for (type);
2114 :
2115 : /* If either operand is not a constant, do the conversions to the signed
2116 : type and subtract. The hardware will do the right thing with any
2117 : overflow in the subtraction. */
2118 28012624 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2119 17842 : return size_binop_loc (loc, MINUS_EXPR,
2120 : fold_convert_loc (loc, ctype, arg0),
2121 17842 : fold_convert_loc (loc, ctype, arg1));
2122 :
2123 : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2124 : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2125 : overflow) and negate (which can't either). Special-case a result
2126 : of zero while we're here. */
2127 27994782 : if (tree_int_cst_equal (arg0, arg1))
2128 24723752 : return build_int_cst (ctype, 0);
2129 3271030 : else if (tree_int_cst_lt (arg1, arg0))
2130 2149504 : return fold_convert_loc (loc, ctype,
2131 2149504 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2132 : else
2133 1121526 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2134 : fold_convert_loc (loc, ctype,
2135 : size_binop_loc (loc,
2136 : MINUS_EXPR,
2137 : arg1, arg0)));
2138 : }
2139 :
2140 : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2141 : or pointer type. */
2142 :
2143 : tree
2144 1461237494 : int_const_convert (tree type, const_tree arg1, int overflowable)
2145 : {
2146 : /* Given an integer constant, make new constant with new type,
2147 : appropriately sign-extended or truncated. Use widest_int
2148 : so that any extension is done according ARG1's type. */
2149 1461237494 : tree arg1_type = TREE_TYPE (arg1);
2150 1461237494 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2151 1461237494 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2152 1461237494 : TYPE_SIGN (arg1_type)),
2153 : overflowable,
2154 1461237494 : TREE_OVERFLOW (arg1));
2155 : }
2156 :
2157 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2158 : to an integer type. */
2159 :
2160 : static tree
2161 58223 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2162 : {
2163 58223 : bool overflow = false;
2164 58223 : tree t;
2165 :
2166 : /* The following code implements the floating point to integer
2167 : conversion rules required by the Java Language Specification,
2168 : that IEEE NaNs are mapped to zero and values that overflow
2169 : the target precision saturate, i.e. values greater than
2170 : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2171 : are mapped to INT_MIN. These semantics are allowed by the
2172 : C and C++ standards that simply state that the behavior of
2173 : FP-to-integer conversion is unspecified upon overflow. */
2174 :
2175 58223 : wide_int val;
2176 58223 : REAL_VALUE_TYPE r;
2177 58223 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2178 :
2179 58223 : switch (code)
2180 : {
2181 58223 : case FIX_TRUNC_EXPR:
2182 58223 : real_trunc (&r, VOIDmode, &x);
2183 58223 : break;
2184 :
2185 0 : default:
2186 0 : gcc_unreachable ();
2187 : }
2188 :
2189 : /* If R is NaN, return zero and show we have an overflow. */
2190 58223 : if (REAL_VALUE_ISNAN (r))
2191 : {
2192 3638 : overflow = true;
2193 3638 : val = wi::zero (TYPE_PRECISION (type));
2194 : }
2195 :
2196 : /* See if R is less than the lower bound or greater than the
2197 : upper bound. */
2198 :
2199 58223 : if (! overflow)
2200 : {
2201 54585 : tree lt = TYPE_MIN_VALUE (type);
2202 54585 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2203 54585 : if (real_less (&r, &l))
2204 : {
2205 1992 : overflow = true;
2206 1992 : val = wi::to_wide (lt);
2207 : }
2208 : }
2209 :
2210 58223 : if (! overflow)
2211 : {
2212 52593 : tree ut = TYPE_MAX_VALUE (type);
2213 52593 : if (ut)
2214 : {
2215 52593 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2216 52593 : if (real_less (&u, &r))
2217 : {
2218 1921 : overflow = true;
2219 1921 : val = wi::to_wide (ut);
2220 : }
2221 : }
2222 : }
2223 :
2224 58223 : if (! overflow)
2225 50674 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2226 :
2227 : /* According to IEEE standard, for conversions from floating point to
2228 : integer. When a NaN or infinite operand cannot be represented in the
2229 : destination format and this cannot otherwise be indicated, the invalid
2230 : operation exception shall be signaled. When a numeric operand would
2231 : convert to an integer outside the range of the destination format, the
2232 : invalid operation exception shall be signaled if this situation cannot
2233 : otherwise be indicated. */
2234 58223 : if (!flag_trapping_math || !overflow)
2235 50928 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2236 : else
2237 : t = NULL_TREE;
2238 :
2239 58223 : return t;
2240 58223 : }
2241 :
2242 : /* A subroutine of fold_convert_const handling conversions of a
2243 : FIXED_CST to an integer type. */
2244 :
2245 : static tree
2246 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2247 : {
2248 0 : tree t;
2249 0 : double_int temp, temp_trunc;
2250 0 : scalar_mode mode;
2251 :
2252 : /* Right shift FIXED_CST to temp by fbit. */
2253 0 : temp = TREE_FIXED_CST (arg1).data;
2254 0 : mode = TREE_FIXED_CST (arg1).mode;
2255 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2256 : {
2257 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2258 : HOST_BITS_PER_DOUBLE_INT,
2259 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2260 :
2261 : /* Left shift temp to temp_trunc by fbit. */
2262 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2263 : HOST_BITS_PER_DOUBLE_INT,
2264 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2265 : }
2266 : else
2267 : {
2268 0 : temp = double_int_zero;
2269 0 : temp_trunc = double_int_zero;
2270 : }
2271 :
2272 : /* If FIXED_CST is negative, we need to round the value toward 0.
2273 : By checking if the fractional bits are not zero to add 1 to temp. */
2274 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2275 0 : && temp_trunc.is_negative ()
2276 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2277 0 : temp += double_int_one;
2278 :
2279 : /* Given a fixed-point constant, make new constant with new type,
2280 : appropriately sign-extended or truncated. */
2281 0 : t = force_fit_type (type, temp, -1,
2282 0 : (temp.is_negative ()
2283 0 : && (TYPE_UNSIGNED (type)
2284 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2285 0 : | TREE_OVERFLOW (arg1));
2286 :
2287 0 : return t;
2288 : }
2289 :
2290 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2291 : to another floating point type. */
2292 :
2293 : static tree
2294 2242733 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2295 : {
2296 2242733 : REAL_VALUE_TYPE value;
2297 2242733 : tree t;
2298 :
2299 : /* If the underlying modes are the same, simply treat it as
2300 : copy and rebuild with TREE_REAL_CST information and the
2301 : given type. */
2302 2242733 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2303 : {
2304 99330 : t = build_real (type, TREE_REAL_CST (arg1));
2305 99330 : return t;
2306 : }
2307 :
2308 : /* Don't perform the operation if flag_signaling_nans is on
2309 : and the operand is a signaling NaN. */
2310 2143403 : if (HONOR_SNANS (arg1)
2311 2145285 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2312 : return NULL_TREE;
2313 :
2314 : /* With flag_rounding_math we should respect the current rounding mode
2315 : unless the conversion is exact. */
2316 2143403 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2317 2144059 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2318 509 : return NULL_TREE;
2319 :
2320 2142894 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2321 2142894 : t = build_real (type, value);
2322 :
2323 : /* If converting an infinity or NAN to a representation that doesn't
2324 : have one, set the overflow bit so that we can produce some kind of
2325 : error message at the appropriate point if necessary. It's not the
2326 : most user-friendly message, but it's better than nothing. */
2327 2142894 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2328 2280932 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2329 0 : TREE_OVERFLOW (t) = 1;
2330 2142894 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2331 2276612 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2332 0 : TREE_OVERFLOW (t) = 1;
2333 : /* Regular overflow, conversion produced an infinity in a mode that
2334 : can't represent them. */
2335 10711146 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2336 0 : && REAL_VALUE_ISINF (value)
2337 2142894 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2338 0 : TREE_OVERFLOW (t) = 1;
2339 : else
2340 2142894 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2341 : return t;
2342 : }
2343 :
2344 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2345 : to a floating point type. */
2346 :
2347 : static tree
2348 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2349 : {
2350 0 : REAL_VALUE_TYPE value;
2351 0 : tree t;
2352 :
2353 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2354 0 : &TREE_FIXED_CST (arg1));
2355 0 : t = build_real (type, value);
2356 :
2357 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2358 0 : return t;
2359 : }
2360 :
2361 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2362 : to another fixed-point type. */
2363 :
2364 : static tree
2365 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2366 : {
2367 0 : FIXED_VALUE_TYPE value;
2368 0 : tree t;
2369 0 : bool overflow_p;
2370 :
2371 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2372 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2373 0 : t = build_fixed (type, value);
2374 :
2375 : /* Propagate overflow flags. */
2376 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2377 0 : TREE_OVERFLOW (t) = 1;
2378 0 : return t;
2379 : }
2380 :
2381 : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2382 : to a fixed-point type. */
2383 :
2384 : static tree
2385 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2386 : {
2387 0 : FIXED_VALUE_TYPE value;
2388 0 : tree t;
2389 0 : bool overflow_p;
2390 0 : double_int di;
2391 :
2392 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2393 :
2394 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2395 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2396 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2397 : else
2398 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2399 :
2400 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2401 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2402 0 : TYPE_SATURATING (type));
2403 0 : t = build_fixed (type, value);
2404 :
2405 : /* Propagate overflow flags. */
2406 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2407 0 : TREE_OVERFLOW (t) = 1;
2408 0 : return t;
2409 : }
2410 :
2411 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2412 : to a fixed-point type. */
2413 :
2414 : static tree
2415 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2416 : {
2417 0 : FIXED_VALUE_TYPE value;
2418 0 : tree t;
2419 0 : bool overflow_p;
2420 :
2421 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2422 0 : &TREE_REAL_CST (arg1),
2423 0 : TYPE_SATURATING (type));
2424 0 : t = build_fixed (type, value);
2425 :
2426 : /* Propagate overflow flags. */
2427 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2428 0 : TREE_OVERFLOW (t) = 1;
2429 0 : return t;
2430 : }
2431 :
2432 : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2433 : type TYPE. If no simplification can be done return NULL_TREE. */
2434 :
2435 : static tree
2436 1525894829 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2437 : {
2438 1525894829 : tree arg_type = TREE_TYPE (arg1);
2439 1525894829 : if (arg_type == type)
2440 : return arg1;
2441 :
2442 : /* We can't widen types, since the runtime value could overflow the
2443 : original type before being extended to the new type. */
2444 1514491785 : if (POLY_INT_CST_P (arg1)
2445 : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2446 : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2447 : return build_poly_int_cst (type,
2448 : poly_wide_int::from (poly_int_cst_value (arg1),
2449 : TYPE_PRECISION (type),
2450 : TYPE_SIGN (arg_type)));
2451 :
2452 1514491785 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2453 : || TREE_CODE (type) == OFFSET_TYPE)
2454 : {
2455 1483100940 : if (TREE_CODE (arg1) == INTEGER_CST)
2456 1461237494 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2457 21863446 : else if (TREE_CODE (arg1) == REAL_CST)
2458 58223 : return fold_convert_const_int_from_real (code, type, arg1);
2459 21805223 : else if (TREE_CODE (arg1) == FIXED_CST)
2460 0 : return fold_convert_const_int_from_fixed (type, arg1);
2461 : }
2462 : else if (SCALAR_FLOAT_TYPE_P (type))
2463 : {
2464 31334486 : if (TREE_CODE (arg1) == INTEGER_CST)
2465 : {
2466 24078925 : tree res = build_real_from_int_cst (type, arg1);
2467 : /* Avoid the folding if flag_rounding_math is on and the
2468 : conversion is not exact. */
2469 24078925 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2470 : {
2471 2916 : bool fail = false;
2472 5832 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2473 2916 : TYPE_PRECISION (TREE_TYPE (arg1)));
2474 2916 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2475 1755 : return NULL_TREE;
2476 2916 : }
2477 : return res;
2478 : }
2479 7255561 : else if (TREE_CODE (arg1) == REAL_CST)
2480 2242733 : return fold_convert_const_real_from_real (type, arg1);
2481 5012828 : else if (TREE_CODE (arg1) == FIXED_CST)
2482 0 : return fold_convert_const_real_from_fixed (type, arg1);
2483 : }
2484 : else if (FIXED_POINT_TYPE_P (type))
2485 : {
2486 0 : if (TREE_CODE (arg1) == FIXED_CST)
2487 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2488 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2489 0 : return fold_convert_const_fixed_from_int (type, arg1);
2490 0 : else if (TREE_CODE (arg1) == REAL_CST)
2491 0 : return fold_convert_const_fixed_from_real (type, arg1);
2492 : }
2493 : else if (VECTOR_TYPE_P (type))
2494 : {
2495 5062 : if (TREE_CODE (arg1) == VECTOR_CST
2496 5062 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2497 : {
2498 5062 : tree elttype = TREE_TYPE (type);
2499 5062 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2500 : /* We can't handle steps directly when extending, since the
2501 : values need to wrap at the original precision first. */
2502 5062 : bool step_ok_p
2503 5062 : = (INTEGRAL_TYPE_P (elttype)
2504 367 : && INTEGRAL_TYPE_P (arg1_elttype)
2505 5365 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2506 5062 : tree_vector_builder v;
2507 5062 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2508 : return NULL_TREE;
2509 5062 : unsigned int len = v.encoded_nelts ();
2510 29191 : for (unsigned int i = 0; i < len; ++i)
2511 : {
2512 24129 : tree elt = VECTOR_CST_ELT (arg1, i);
2513 24129 : tree cvt = fold_convert_const (code, elttype, elt);
2514 24129 : if (cvt == NULL_TREE)
2515 0 : return NULL_TREE;
2516 24129 : v.quick_push (cvt);
2517 : }
2518 5062 : return v.build ();
2519 5062 : }
2520 : }
2521 12199 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2522 12199 : return build_zero_cst (type);
2523 : return NULL_TREE;
2524 : }
2525 :
2526 : /* Construct a vector of zero elements of vector type TYPE. */
2527 :
2528 : static tree
2529 17566 : build_zero_vector (tree type)
2530 : {
2531 17566 : tree t;
2532 :
2533 17566 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2534 17566 : return build_vector_from_val (type, t);
2535 : }
2536 :
2537 : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2538 :
2539 : bool
2540 4374 : fold_convertible_p (const_tree type, const_tree arg)
2541 : {
2542 4374 : const_tree orig = TREE_TYPE (arg);
2543 :
2544 4374 : if (type == orig)
2545 : return true;
2546 :
2547 4374 : if (TREE_CODE (arg) == ERROR_MARK
2548 4374 : || TREE_CODE (type) == ERROR_MARK
2549 4374 : || TREE_CODE (orig) == ERROR_MARK)
2550 : return false;
2551 :
2552 4374 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2553 : return true;
2554 :
2555 4374 : switch (TREE_CODE (type))
2556 : {
2557 3811 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2558 3811 : case POINTER_TYPE: case REFERENCE_TYPE:
2559 3811 : case OFFSET_TYPE:
2560 3811 : return (INTEGRAL_TYPE_P (orig)
2561 386 : || (POINTER_TYPE_P (orig)
2562 251 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2563 3946 : || TREE_CODE (orig) == OFFSET_TYPE);
2564 :
2565 130 : case REAL_TYPE:
2566 130 : case FIXED_POINT_TYPE:
2567 130 : case VOID_TYPE:
2568 130 : return TREE_CODE (type) == TREE_CODE (orig);
2569 :
2570 209 : case VECTOR_TYPE:
2571 209 : return (VECTOR_TYPE_P (orig)
2572 322 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2573 : TYPE_VECTOR_SUBPARTS (orig))
2574 226 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2575 :
2576 : default:
2577 : return false;
2578 : }
2579 : }
2580 :
2581 : /* Convert expression ARG to type TYPE. Used by the middle-end for
2582 : simple conversions in preference to calling the front-end's convert. */
2583 :
2584 : tree
2585 2254959307 : fold_convert_loc (location_t loc, tree type, tree arg)
2586 : {
2587 2254959307 : tree orig = TREE_TYPE (arg);
2588 2254959307 : tree tem;
2589 :
2590 2254959307 : if (type == orig)
2591 : return arg;
2592 :
2593 1517290119 : if (TREE_CODE (arg) == ERROR_MARK
2594 1517289073 : || TREE_CODE (type) == ERROR_MARK
2595 1517289072 : || TREE_CODE (orig) == ERROR_MARK)
2596 1047 : return error_mark_node;
2597 :
2598 1517289072 : switch (TREE_CODE (type))
2599 : {
2600 122528162 : case POINTER_TYPE:
2601 122528162 : case REFERENCE_TYPE:
2602 : /* Handle conversions between pointers to different address spaces. */
2603 122528162 : if (POINTER_TYPE_P (orig)
2604 122528162 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2605 103271993 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2606 125 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2607 : /* fall through */
2608 :
2609 1484604827 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2610 1484604827 : case OFFSET_TYPE: case BITINT_TYPE:
2611 1484604827 : if (TREE_CODE (arg) == INTEGER_CST)
2612 : {
2613 1240473960 : tem = fold_convert_const (NOP_EXPR, type, arg);
2614 1240473960 : if (tem != NULL_TREE)
2615 : return tem;
2616 : }
2617 244130867 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2618 2550 : || TREE_CODE (orig) == OFFSET_TYPE)
2619 244130867 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2620 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2621 0 : return fold_convert_loc (loc, type,
2622 : fold_build1_loc (loc, REALPART_EXPR,
2623 0 : TREE_TYPE (orig), arg));
2624 0 : gcc_assert (VECTOR_TYPE_P (orig)
2625 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2626 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2627 :
2628 560047 : case REAL_TYPE:
2629 560047 : if (TREE_CODE (arg) == INTEGER_CST)
2630 : {
2631 58004 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2632 58004 : if (tem != NULL_TREE)
2633 : return tem;
2634 : }
2635 502043 : else if (TREE_CODE (arg) == REAL_CST)
2636 : {
2637 121967 : tem = fold_convert_const (NOP_EXPR, type, arg);
2638 121967 : if (tem != NULL_TREE)
2639 : return tem;
2640 : }
2641 380076 : else if (TREE_CODE (arg) == FIXED_CST)
2642 : {
2643 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2644 0 : if (tem != NULL_TREE)
2645 : return tem;
2646 : }
2647 :
2648 380078 : switch (TREE_CODE (orig))
2649 : {
2650 721 : case INTEGER_TYPE: case BITINT_TYPE:
2651 721 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2652 721 : case POINTER_TYPE: case REFERENCE_TYPE:
2653 721 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2654 :
2655 379357 : case REAL_TYPE:
2656 379357 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2657 :
2658 0 : case FIXED_POINT_TYPE:
2659 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2660 :
2661 0 : case COMPLEX_TYPE:
2662 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2663 0 : return fold_convert_loc (loc, type, tem);
2664 :
2665 0 : default:
2666 0 : gcc_unreachable ();
2667 : }
2668 :
2669 0 : case FIXED_POINT_TYPE:
2670 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2671 0 : || TREE_CODE (arg) == REAL_CST)
2672 : {
2673 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2674 0 : if (tem != NULL_TREE)
2675 0 : goto fold_convert_exit;
2676 : }
2677 :
2678 0 : switch (TREE_CODE (orig))
2679 : {
2680 0 : case FIXED_POINT_TYPE:
2681 0 : case INTEGER_TYPE:
2682 0 : case ENUMERAL_TYPE:
2683 0 : case BOOLEAN_TYPE:
2684 0 : case REAL_TYPE:
2685 0 : case BITINT_TYPE:
2686 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2687 :
2688 0 : case COMPLEX_TYPE:
2689 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2690 0 : return fold_convert_loc (loc, type, tem);
2691 :
2692 0 : default:
2693 0 : gcc_unreachable ();
2694 : }
2695 :
2696 2269 : case COMPLEX_TYPE:
2697 2269 : switch (TREE_CODE (orig))
2698 : {
2699 584 : case INTEGER_TYPE: case BITINT_TYPE:
2700 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2701 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2702 584 : case REAL_TYPE:
2703 584 : case FIXED_POINT_TYPE:
2704 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2705 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2706 584 : fold_convert_loc (loc, TREE_TYPE (type),
2707 584 : integer_zero_node));
2708 1685 : case COMPLEX_TYPE:
2709 1685 : {
2710 1685 : tree rpart, ipart;
2711 :
2712 1685 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2713 : {
2714 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2715 1534 : TREE_OPERAND (arg, 0));
2716 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2717 1534 : TREE_OPERAND (arg, 1));
2718 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2719 : }
2720 :
2721 151 : arg = save_expr (arg);
2722 151 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2723 151 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2724 151 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2725 151 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2726 151 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2727 : }
2728 :
2729 0 : default:
2730 0 : gcc_unreachable ();
2731 : }
2732 :
2733 32006825 : case VECTOR_TYPE:
2734 32006825 : if (integer_zerop (arg))
2735 17566 : return build_zero_vector (type);
2736 31989259 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2737 31989259 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2738 : || VECTOR_TYPE_P (orig));
2739 31989259 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2740 :
2741 111155 : case VOID_TYPE:
2742 111155 : tem = fold_ignored_result (arg);
2743 111155 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2744 :
2745 63 : case NULLPTR_TYPE:
2746 63 : if (integer_zerop (arg))
2747 17 : return build_zero_cst (type);
2748 : /* FALLTHRU */
2749 3807 : default:
2750 3807 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2751 3807 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2752 0 : gcc_unreachable ();
2753 : }
2754 0 : fold_convert_exit:
2755 0 : tem = protected_set_expr_location_unshare (tem, loc);
2756 0 : return tem;
2757 : }
2758 :
2759 : /* Return false if expr can be assumed not to be an lvalue, true
2760 : otherwise. */
2761 :
2762 : static bool
2763 53719478 : maybe_lvalue_p (const_tree x)
2764 : {
2765 : /* We only need to wrap lvalue tree codes. */
2766 53719478 : switch (TREE_CODE (x))
2767 : {
2768 : case VAR_DECL:
2769 : case PARM_DECL:
2770 : case RESULT_DECL:
2771 : case LABEL_DECL:
2772 : case FUNCTION_DECL:
2773 : case SSA_NAME:
2774 : case COMPOUND_LITERAL_EXPR:
2775 :
2776 : case COMPONENT_REF:
2777 : case MEM_REF:
2778 : case INDIRECT_REF:
2779 : case ARRAY_REF:
2780 : case ARRAY_RANGE_REF:
2781 : case BIT_FIELD_REF:
2782 : case OBJ_TYPE_REF:
2783 :
2784 : case REALPART_EXPR:
2785 : case IMAGPART_EXPR:
2786 : case PREINCREMENT_EXPR:
2787 : case PREDECREMENT_EXPR:
2788 : case SAVE_EXPR:
2789 : case TRY_CATCH_EXPR:
2790 : case WITH_CLEANUP_EXPR:
2791 : case COMPOUND_EXPR:
2792 : case MODIFY_EXPR:
2793 : case TARGET_EXPR:
2794 : case COND_EXPR:
2795 : case BIND_EXPR:
2796 : case VIEW_CONVERT_EXPR:
2797 : break;
2798 :
2799 39455457 : default:
2800 : /* Assume the worst for front-end tree codes. */
2801 39455457 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2802 : break;
2803 : return false;
2804 : }
2805 :
2806 14299350 : return true;
2807 : }
2808 :
2809 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2810 :
2811 : tree
2812 47449531 : non_lvalue_loc (location_t loc, tree x)
2813 : {
2814 : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2815 : us. */
2816 47449531 : if (in_gimple_form)
2817 : return x;
2818 :
2819 10837401 : if (! maybe_lvalue_p (x))
2820 : return x;
2821 2574592 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2822 : }
2823 :
2824 : /* Given a tree comparison code, return the code that is the logical inverse.
2825 : It is generally not safe to do this for floating-point comparisons, except
2826 : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2827 : ERROR_MARK in this case. */
2828 :
2829 : enum tree_code
2830 133087690 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2831 : {
2832 133087690 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2833 1060726 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2834 : return ERROR_MARK;
2835 :
2836 132278259 : switch (code)
2837 : {
2838 : case EQ_EXPR:
2839 : return NE_EXPR;
2840 58532931 : case NE_EXPR:
2841 58532931 : return EQ_EXPR;
2842 12332824 : case GT_EXPR:
2843 12332824 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2844 18409733 : case GE_EXPR:
2845 18409733 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2846 8098814 : case LT_EXPR:
2847 8098814 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2848 8286168 : case LE_EXPR:
2849 8286168 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2850 261 : case LTGT_EXPR:
2851 261 : return UNEQ_EXPR;
2852 292 : case UNEQ_EXPR:
2853 292 : return LTGT_EXPR;
2854 : case UNGT_EXPR:
2855 : return LE_EXPR;
2856 : case UNGE_EXPR:
2857 : return LT_EXPR;
2858 : case UNLT_EXPR:
2859 : return GE_EXPR;
2860 : case UNLE_EXPR:
2861 : return GT_EXPR;
2862 223282 : case ORDERED_EXPR:
2863 223282 : return UNORDERED_EXPR;
2864 56844 : case UNORDERED_EXPR:
2865 56844 : return ORDERED_EXPR;
2866 0 : default:
2867 0 : gcc_unreachable ();
2868 : }
2869 : }
2870 :
2871 : /* Similar, but return the comparison that results if the operands are
2872 : swapped. This is safe for floating-point. */
2873 :
2874 : enum tree_code
2875 161883093 : swap_tree_comparison (enum tree_code code)
2876 : {
2877 161883093 : switch (code)
2878 : {
2879 : case EQ_EXPR:
2880 : case NE_EXPR:
2881 : case ORDERED_EXPR:
2882 : case UNORDERED_EXPR:
2883 : case LTGT_EXPR:
2884 : case UNEQ_EXPR:
2885 : return code;
2886 39383844 : case GT_EXPR:
2887 39383844 : return LT_EXPR;
2888 11223164 : case GE_EXPR:
2889 11223164 : return LE_EXPR;
2890 22114055 : case LT_EXPR:
2891 22114055 : return GT_EXPR;
2892 17293611 : case LE_EXPR:
2893 17293611 : return GE_EXPR;
2894 252458 : case UNGT_EXPR:
2895 252458 : return UNLT_EXPR;
2896 20695 : case UNGE_EXPR:
2897 20695 : return UNLE_EXPR;
2898 375033 : case UNLT_EXPR:
2899 375033 : return UNGT_EXPR;
2900 111816 : case UNLE_EXPR:
2901 111816 : return UNGE_EXPR;
2902 0 : default:
2903 0 : gcc_unreachable ();
2904 : }
2905 : }
2906 :
2907 :
2908 : /* Convert a comparison tree code from an enum tree_code representation
2909 : into a compcode bit-based encoding. This function is the inverse of
2910 : compcode_to_comparison. */
2911 :
2912 : static enum comparison_code
2913 91778 : comparison_to_compcode (enum tree_code code)
2914 : {
2915 91778 : switch (code)
2916 : {
2917 : case LT_EXPR:
2918 : return COMPCODE_LT;
2919 : case EQ_EXPR:
2920 : return COMPCODE_EQ;
2921 : case LE_EXPR:
2922 : return COMPCODE_LE;
2923 : case GT_EXPR:
2924 : return COMPCODE_GT;
2925 : case NE_EXPR:
2926 : return COMPCODE_NE;
2927 : case GE_EXPR:
2928 : return COMPCODE_GE;
2929 : case ORDERED_EXPR:
2930 : return COMPCODE_ORD;
2931 : case UNORDERED_EXPR:
2932 : return COMPCODE_UNORD;
2933 : case UNLT_EXPR:
2934 : return COMPCODE_UNLT;
2935 : case UNEQ_EXPR:
2936 : return COMPCODE_UNEQ;
2937 : case UNLE_EXPR:
2938 : return COMPCODE_UNLE;
2939 : case UNGT_EXPR:
2940 : return COMPCODE_UNGT;
2941 : case LTGT_EXPR:
2942 : return COMPCODE_LTGT;
2943 : case UNGE_EXPR:
2944 : return COMPCODE_UNGE;
2945 0 : default:
2946 0 : gcc_unreachable ();
2947 : }
2948 : }
2949 :
2950 : /* Convert a compcode bit-based encoding of a comparison operator back
2951 : to GCC's enum tree_code representation. This function is the
2952 : inverse of comparison_to_compcode. */
2953 :
2954 : static enum tree_code
2955 18834 : compcode_to_comparison (enum comparison_code code)
2956 : {
2957 18834 : switch (code)
2958 : {
2959 : case COMPCODE_LT:
2960 : return LT_EXPR;
2961 : case COMPCODE_EQ:
2962 : return EQ_EXPR;
2963 : case COMPCODE_LE:
2964 : return LE_EXPR;
2965 : case COMPCODE_GT:
2966 : return GT_EXPR;
2967 : case COMPCODE_NE:
2968 : return NE_EXPR;
2969 : case COMPCODE_GE:
2970 : return GE_EXPR;
2971 : case COMPCODE_ORD:
2972 : return ORDERED_EXPR;
2973 : case COMPCODE_UNORD:
2974 : return UNORDERED_EXPR;
2975 : case COMPCODE_UNLT:
2976 : return UNLT_EXPR;
2977 : case COMPCODE_UNEQ:
2978 : return UNEQ_EXPR;
2979 : case COMPCODE_UNLE:
2980 : return UNLE_EXPR;
2981 : case COMPCODE_UNGT:
2982 : return UNGT_EXPR;
2983 : case COMPCODE_LTGT:
2984 : return LTGT_EXPR;
2985 : case COMPCODE_UNGE:
2986 : return UNGE_EXPR;
2987 0 : default:
2988 0 : gcc_unreachable ();
2989 : }
2990 : }
2991 :
2992 : /* Return true if COND1 tests the opposite condition of COND2. */
2993 :
2994 : bool
2995 1781559 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2996 : {
2997 1781559 : return (COMPARISON_CLASS_P (cond1)
2998 1687906 : && COMPARISON_CLASS_P (cond2)
2999 1676587 : && (invert_tree_comparison
3000 1676587 : (TREE_CODE (cond1),
3001 3353174 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3002 68897 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3003 68897 : TREE_OPERAND (cond2, 0), 0)
3004 1802785 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3005 21226 : TREE_OPERAND (cond2, 1), 0));
3006 : }
3007 :
3008 : /* Return a code for the comparison which is the combination of
3009 : doing the AND or OR (depending on CODE) of the two operations LCODE
3010 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3011 : the possibility of trapping if the mode has NaNs, and return ERROR_MARK
3012 : if this makes the transformation invalid. If the resulting code is
3013 : INTEGER_CST, then *RES will be set to a non-NULL CONSTANT. */
3014 :
3015 : enum tree_code
3016 45889 : combine_comparisons (enum tree_code code, enum tree_code lcode,
3017 : enum tree_code rcode, tree truth_type,
3018 : tree inner_truth_type,
3019 : bool honor_nans, tree *res)
3020 : {
3021 45889 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3022 45889 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3023 45889 : int compcode;
3024 45889 : *res = NULL_TREE;
3025 45889 : if (TREE_CODE (inner_truth_type) == VECTOR_TYPE)
3026 199 : inner_truth_type = TREE_TYPE (inner_truth_type);
3027 45889 : bool signed_bool = false;
3028 45889 : if (TYPE_SIGN (inner_truth_type) == SIGNED
3029 45889 : && (TREE_CODE (inner_truth_type) == BOOLEAN_TYPE
3030 2140 : || TYPE_PRECISION (inner_truth_type) == 1))
3031 : signed_bool = true;
3032 :
3033 45889 : switch (code)
3034 : {
3035 31562 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3036 31562 : case BIT_AND_EXPR:
3037 31562 : compcode = lcompcode & rcompcode;
3038 31562 : break;
3039 :
3040 12889 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3041 12889 : case BIT_IOR_EXPR:
3042 12889 : compcode = lcompcode | rcompcode;
3043 12889 : break;
3044 :
3045 1075 : case BIT_XOR_EXPR:
3046 1075 : case NE_EXPR:
3047 1075 : compcode = lcompcode ^ rcompcode;
3048 1075 : break;
3049 :
3050 336 : case EQ_EXPR:
3051 336 : compcode = ~(lcompcode ^ rcompcode);
3052 336 : break;
3053 :
3054 : //`bool0 < bool1` is `!bool0 & bool1` for unsigned
3055 : //`bool0 < bool1` is `bool0 & !bool1` for signed
3056 24 : case LT_EXPR:
3057 24 : if (signed_bool)
3058 1 : compcode = lcompcode & ~rcompcode;
3059 : else
3060 23 : compcode = ~lcompcode & rcompcode;
3061 : break;
3062 :
3063 : //`bool0 > bool1` is `bool0 & !bool1` for unsigned
3064 : //`bool0 > bool1` is `!bool0 & bool1` for signed
3065 0 : case GT_EXPR:
3066 0 : if (signed_bool)
3067 0 : compcode = ~lcompcode & rcompcode;
3068 : else
3069 0 : compcode = lcompcode & ~rcompcode;
3070 : break;
3071 :
3072 : // `bool0 <= bool1` as !bool0 | bool1 for unsigned
3073 : // `bool0 <= bool1` as bool0 | !bool1 for signed
3074 3 : case LE_EXPR:
3075 3 : if (signed_bool)
3076 3 : compcode = lcompcode | ~rcompcode;
3077 : else
3078 0 : compcode = ~lcompcode | rcompcode;
3079 : break;
3080 :
3081 : //`bool0 >= bool1` is `bool0 | !bool1` for unsigned
3082 : //`bool0 >= bool1` is `bool0 | !bool1` for signed
3083 0 : case GE_EXPR:
3084 0 : if (signed_bool)
3085 0 : compcode = ~lcompcode | rcompcode;
3086 : else
3087 0 : compcode = lcompcode | ~rcompcode;
3088 : break;
3089 :
3090 : default:
3091 : return ERROR_MARK;
3092 : }
3093 :
3094 45889 : if (!honor_nans)
3095 : {
3096 : /* Eliminate unordered comparisons, as well as LTGT and ORD
3097 : which are not used unless the mode has NaNs. */
3098 27853 : compcode &= ~COMPCODE_UNORD;
3099 27853 : if (compcode == COMPCODE_LTGT)
3100 : compcode = COMPCODE_NE;
3101 26788 : else if (compcode == COMPCODE_ORD)
3102 1355 : compcode = COMPCODE_TRUE;
3103 : }
3104 18036 : else if (flag_trapping_math)
3105 : {
3106 : /* Check that the original operation and the optimized ones will trap
3107 : under the same condition. */
3108 17105 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3109 15276 : && (lcompcode != COMPCODE_EQ)
3110 17105 : && (lcompcode != COMPCODE_ORD);
3111 17105 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3112 15000 : && (rcompcode != COMPCODE_EQ)
3113 17105 : && (rcompcode != COMPCODE_ORD);
3114 34210 : bool trap = (compcode & COMPCODE_UNORD) == 0
3115 15993 : && (compcode != COMPCODE_EQ)
3116 15993 : && (compcode != COMPCODE_ORD)
3117 2810 : && (compcode != COMPCODE_FALSE)
3118 17105 : && (compcode != COMPCODE_TRUE);
3119 :
3120 : /* In a short-circuited boolean expression the LHS might be
3121 : such that the RHS, if evaluated, will never trap. For
3122 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3123 : if neither x nor y is NaN. (This is a mixed blessing: for
3124 : example, the expression above will never trap, hence
3125 : optimizing it to x < y would be invalid). */
3126 596 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3127 17264 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3128 : rtrap = false;
3129 :
3130 : /* Allow combining of `a != b && a < b` since NAN will cause != to be
3131 : always true, and `a < b` will cause a trap. This is trap neutral. */
3132 17105 : if (code == TRUTH_ANDIF_EXPR && lcompcode == COMPCODE_NE && rtrap && trap)
3133 : ;
3134 : /* Likewise of `a == b || a < b` for the same reason. */
3135 17008 : else if (code == TRUTH_ORIF_EXPR && lcompcode == COMPCODE_EQ && rtrap && trap)
3136 : ;
3137 : /* If the comparison was short-circuited, and only the RHS
3138 : trapped, we may now generate a spurious trap. */
3139 16940 : else if (rtrap && !ltrap
3140 309 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3141 : return ERROR_MARK;
3142 :
3143 : /* If we changed the conditions that cause a trap, we lose. */
3144 16940 : else if ((ltrap || rtrap) != trap)
3145 : return ERROR_MARK;
3146 : }
3147 :
3148 30256 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3149 : {
3150 11422 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3151 11422 : return INTEGER_CST;
3152 : }
3153 : else
3154 18834 : return compcode_to_comparison ((enum comparison_code) compcode);
3155 : }
3156 :
3157 : /* Return a tree for the comparison which is the combination of
3158 : doing the AND or OR (depending on CODE) of the two operations LCODE
3159 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3160 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3161 : if this makes the transformation invalid. */
3162 :
3163 : tree
3164 27933 : combine_comparisons (location_t loc,
3165 : enum tree_code code, enum tree_code lcode,
3166 : enum tree_code rcode, tree truth_type,
3167 : tree inner_truth_type,
3168 : tree ll_arg, tree lr_arg)
3169 : {
3170 27933 : bool honor_nans = HONOR_NANS (ll_arg);
3171 27933 : tree_code rescode;
3172 27933 : tree res;
3173 27933 : rescode = combine_comparisons (code, lcode, rcode,
3174 : truth_type, inner_truth_type,
3175 : honor_nans, &res);
3176 27933 : if (rescode == ERROR_MARK)
3177 : return NULL_TREE;
3178 25564 : if (rescode == INTEGER_CST)
3179 10935 : return res;
3180 :
3181 14629 : return fold_build2_loc (loc, rescode, truth_type, ll_arg, lr_arg);
3182 : }
3183 :
3184 : /* Return nonzero if two operands (typically of the same tree node)
3185 : are necessarily equal. FLAGS modifies behavior as follows:
3186 :
3187 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3188 : This function tests whether the operands are indistinguishable;
3189 : it does not test whether they are equal using C's == operation.
3190 : The distinction is important for IEEE floating point, because
3191 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3192 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3193 :
3194 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3195 : even though it may hold multiple values during a function.
3196 : This is because a GCC tree node guarantees that nothing else is
3197 : executed between the evaluation of its "operands" (which may often
3198 : be evaluated in arbitrary order). Hence if the operands themselves
3199 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3200 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3201 : unset means assuming isochronic (or instantaneous) tree equivalence.
3202 : Unless comparing arbitrary expression trees, such as from different
3203 : statements, this flag can usually be left unset.
3204 :
3205 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3206 : are considered the same. It is used when the caller has other ways
3207 : to ensure that global memory is unchanged in between.
3208 :
3209 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3210 : not values of expressions.
3211 :
3212 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3213 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3214 :
3215 : If OEP_BITWISE is set, then require the values to be bitwise identical
3216 : rather than simply numerically equal. Do not take advantage of things
3217 : like math-related flags or undefined behavior; only return true for
3218 : values that are provably bitwise identical in all circumstances.
3219 :
3220 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3221 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3222 : rather than just numerically equivalent. The compared expressions must
3223 : however perform the same operations but may do intermediate computations in
3224 : differing signs. Because this comparison ignores any possible UB it cannot
3225 : be used blindly without ensuring that the context you are using it in itself
3226 : doesn't guarantee that there will be no UB. Conditional expressions are
3227 : excluded from this relaxation.
3228 :
3229 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3230 : differing hashes even for cases where operand_compare::operand_equal_p
3231 : compares equal.
3232 :
3233 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3234 : any operand with side effect. This is unnecessarily conservative in the
3235 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3236 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3237 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3238 : even if var is volatile. */
3239 :
3240 : bool
3241 7424207596 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3242 : unsigned int flags)
3243 : {
3244 7424207596 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3245 : }
3246 :
3247 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3248 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3249 : expression is being compared under for equality. This means that they can
3250 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3251 :
3252 : bool
3253 7424943018 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3254 : tree type1, const_tree arg1,
3255 : unsigned int flags)
3256 : {
3257 7424943018 : bool r;
3258 7424943018 : if (verify_hash_value (arg0, arg1, flags, &r))
3259 3131927422 : return r;
3260 :
3261 4293015596 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3262 4293015596 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3263 :
3264 : /* If either is ERROR_MARK, they aren't equal. */
3265 4293015596 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3266 4293014932 : || type0 == error_mark_node
3267 4293014930 : || type1 == error_mark_node)
3268 : return false;
3269 :
3270 : /* Similar, if either does not have a type (like a template id),
3271 : they aren't equal. */
3272 4293014929 : if (!type0 || !type1)
3273 : return false;
3274 :
3275 : /* Bitwise identity makes no sense if the values have different layouts. */
3276 4293009775 : if ((flags & OEP_BITWISE)
3277 4293009775 : && !tree_nop_conversion_p (type0, type1))
3278 : return false;
3279 :
3280 : /* We cannot consider pointers to different address space equal. */
3281 4293009775 : if (POINTER_TYPE_P (type0)
3282 659354268 : && POINTER_TYPE_P (type1)
3283 4858924335 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3284 565914560 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3285 : return false;
3286 :
3287 : /* Check equality of integer constants before bailing out due to
3288 : precision differences. */
3289 4293009528 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3290 : {
3291 : /* Address of INTEGER_CST is not defined; check that we did not forget
3292 : to drop the OEP_ADDRESS_OF flags. */
3293 671090631 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3294 671090631 : return tree_int_cst_equal (arg0, arg1);
3295 : }
3296 :
3297 3621918897 : if ((flags & OEP_ASSUME_WRAPV)
3298 2091492 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3299 : {
3300 787345 : const_tree t_arg0 = arg0;
3301 787345 : const_tree t_arg1 = arg1;
3302 787345 : STRIP_NOPS (arg0);
3303 787345 : STRIP_NOPS (arg1);
3304 : /* Only recurse if the conversion was one that was valid to strip. */
3305 787345 : if (t_arg0 != arg0 || t_arg1 != arg1)
3306 735422 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3307 : }
3308 :
3309 3621183475 : if (!(flags & OEP_ADDRESS_OF))
3310 : {
3311 : /* Check if we are checking an operation where the two's compliment
3312 : bitwise representation of the result is not the same between signed and
3313 : unsigned arithmetic. */
3314 3212018724 : bool enforce_signedness = true;
3315 3212018724 : if (flags & OEP_ASSUME_WRAPV)
3316 : {
3317 1259158 : switch (TREE_CODE (arg0))
3318 : {
3319 : case PLUS_EXPR:
3320 : case MINUS_EXPR:
3321 : case MULT_EXPR:
3322 : case BIT_IOR_EXPR:
3323 : case BIT_XOR_EXPR:
3324 : case BIT_AND_EXPR:
3325 : case BIT_NOT_EXPR:
3326 : case ABS_EXPR:
3327 : CASE_CONVERT:
3328 : case SSA_NAME:
3329 : case INTEGER_CST:
3330 : case VAR_DECL:
3331 : case PARM_DECL:
3332 : case RESULT_DECL:
3333 3212018724 : enforce_signedness = false;
3334 : break;
3335 :
3336 : default:
3337 : break;
3338 : }
3339 : }
3340 :
3341 : /* If both types don't have the same signedness, then we can't consider
3342 : them equal. We must check this before the STRIP_NOPS calls
3343 : because they may change the signedness of the arguments. As pointers
3344 : strictly don't have a signedness, require either two pointers or
3345 : two non-pointers as well. */
3346 3212018724 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3347 3212018724 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3348 146382732 : && enforce_signedness))
3349 : return false;
3350 :
3351 : /* If both types don't have the same precision, then it is not safe
3352 : to strip NOPs. */
3353 2908954845 : if (element_precision (type0) != element_precision (type1))
3354 : return false;
3355 :
3356 2753975855 : STRIP_NOPS (arg0);
3357 2753975855 : STRIP_NOPS (arg1);
3358 :
3359 2753975855 : type0 = TREE_TYPE (arg0);
3360 2753975855 : type1 = TREE_TYPE (arg1);
3361 : }
3362 : #if 0
3363 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3364 : sanity check once the issue is solved. */
3365 : else
3366 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3367 : are not defined. Check that we did not forget to drop the
3368 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3369 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3370 : && TREE_CODE (arg0) != SSA_NAME);
3371 : #endif
3372 :
3373 : /* In case both args are comparisons but with different comparison
3374 : code, try to swap the comparison operands of one arg to produce
3375 : a match and compare that variant. */
3376 3163140606 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3377 1254317092 : && COMPARISON_CLASS_P (arg0)
3378 6860403 : && COMPARISON_CLASS_P (arg1))
3379 : {
3380 5119252 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3381 :
3382 5119252 : if (TREE_CODE (arg0) == swap_code)
3383 2184159 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3384 2184159 : TREE_OPERAND (arg1, 1), flags)
3385 2203678 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3386 19519 : TREE_OPERAND (arg1, 0), flags);
3387 : }
3388 :
3389 3160956447 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3390 : {
3391 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3392 1252132933 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3393 : ;
3394 1252069454 : else if (flags & OEP_ADDRESS_OF)
3395 : {
3396 : /* If we are interested in comparing addresses ignore
3397 : MEM_REF wrappings of the base that can appear just for
3398 : TBAA reasons. */
3399 49883192 : if (TREE_CODE (arg0) == MEM_REF
3400 8192627 : && DECL_P (arg1)
3401 5786611 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3402 1356161 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3403 50611507 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3404 : return true;
3405 49660352 : else if (TREE_CODE (arg1) == MEM_REF
3406 31011236 : && DECL_P (arg0)
3407 11314860 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3408 2368767 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3409 50260841 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3410 : return true;
3411 : return false;
3412 : }
3413 : else
3414 : return false;
3415 : }
3416 :
3417 : /* When not checking addresses, this is needed for conversions and for
3418 : COMPONENT_REF. Might as well play it safe and always test this. */
3419 1908886993 : if (TREE_CODE (type0) == ERROR_MARK
3420 1908886993 : || TREE_CODE (type1) == ERROR_MARK
3421 3817773986 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3422 26494215 : && !(flags & OEP_ADDRESS_OF)))
3423 : return false;
3424 :
3425 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3426 : We don't care about side effects in that case because the SAVE_EXPR
3427 : takes care of that for us. In all other cases, two expressions are
3428 : equal if they have no side effects. If we have two identical
3429 : expressions with side effects that should be treated the same due
3430 : to the only side effects being identical SAVE_EXPR's, that will
3431 : be detected in the recursive calls below.
3432 : If we are taking an invariant address of two identical objects
3433 : they are necessarily equal as well. */
3434 373773213 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3435 2278720936 : && (TREE_CODE (arg0) == SAVE_EXPR
3436 373747568 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3437 333712601 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3438 : return true;
3439 :
3440 : /* Next handle constant cases, those for which we can return 1 even
3441 : if ONLY_CONST is set. */
3442 1572062968 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3443 28063568 : switch (TREE_CODE (arg0))
3444 : {
3445 151 : case INTEGER_CST:
3446 151 : return tree_int_cst_equal (arg0, arg1);
3447 :
3448 0 : case FIXED_CST:
3449 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3450 : TREE_FIXED_CST (arg1));
3451 :
3452 3792888 : case REAL_CST:
3453 3792888 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3454 : return true;
3455 :
3456 2749723 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3457 : {
3458 : /* If we do not distinguish between signed and unsigned zero,
3459 : consider them equal. */
3460 15306 : if (real_zerop (arg0) && real_zerop (arg1))
3461 : return true;
3462 : }
3463 : return false;
3464 :
3465 1023099 : case VECTOR_CST:
3466 1023099 : {
3467 1023099 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3468 1023099 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3469 : return false;
3470 :
3471 1000960 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3472 1000960 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3473 : return false;
3474 :
3475 964861 : unsigned int count = vector_cst_encoded_nelts (arg0);
3476 2314161 : for (unsigned int i = 0; i < count; ++i)
3477 2202700 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3478 1101350 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3479 : return false;
3480 : return true;
3481 : }
3482 :
3483 14157 : case COMPLEX_CST:
3484 14157 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3485 : flags)
3486 14157 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3487 : flags));
3488 :
3489 1138131 : case STRING_CST:
3490 1138131 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3491 1138131 : && ! memcmp (TREE_STRING_POINTER (arg0),
3492 661659 : TREE_STRING_POINTER (arg1),
3493 661659 : TREE_STRING_LENGTH (arg0)));
3494 :
3495 0 : case RAW_DATA_CST:
3496 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3497 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3498 0 : RAW_DATA_POINTER (arg1),
3499 0 : RAW_DATA_LENGTH (arg0)));
3500 :
3501 20190905 : case ADDR_EXPR:
3502 20190905 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3503 20190905 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3504 : flags | OEP_ADDRESS_OF
3505 20190905 : | OEP_MATCH_SIDE_EFFECTS);
3506 184271 : case CONSTRUCTOR:
3507 184271 : {
3508 : /* In GIMPLE empty constructors are allowed in initializers of
3509 : aggregates. */
3510 184271 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3511 : return true;
3512 :
3513 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3514 138238 : if (TREE_CODE (type0) != TREE_CODE (type1))
3515 : return false;
3516 138238 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3517 : {
3518 : /* For arrays, check that the sizes all match. */
3519 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3520 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3521 264 : || siz0 < 0
3522 528 : || siz0 != int_size_in_bytes (type1))
3523 : return false;
3524 : }
3525 137974 : else if (!types_compatible_p (type0, type1))
3526 : return false;
3527 :
3528 138238 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3529 138238 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3530 414714 : if (vec_safe_length (v0) != vec_safe_length (v1))
3531 : return false;
3532 :
3533 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3534 : of the CONSTRUCTOR referenced indirectly. */
3535 138238 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3536 :
3537 369505357 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3538 : {
3539 206559 : constructor_elt *c0 = &(*v0)[idx];
3540 206559 : constructor_elt *c1 = &(*v1)[idx];
3541 :
3542 : /* Check that the values are the same... */
3543 206559 : if (c0->value != c1->value
3544 206559 : && !operand_equal_p (c0->value, c1->value, flags))
3545 : return false;
3546 :
3547 : /* ... and that they apply to the same field! */
3548 117989 : if (c0->index != c1->index
3549 117989 : && (TREE_CODE (type0) == ARRAY_TYPE
3550 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3551 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3552 0 : DECL_FIELD_OFFSET (c1->index),
3553 : flags)
3554 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3555 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3556 : flags)))
3557 : return false;
3558 : }
3559 :
3560 : return true;
3561 : }
3562 :
3563 : default:
3564 : break;
3565 : }
3566 :
3567 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3568 : two instances of undefined behavior will give identical results. */
3569 1545719366 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3570 : return false;
3571 :
3572 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3573 : variant that allows null and views null as being different from any
3574 : non-null value. In the latter case, if either is null, the both
3575 : must be; otherwise, do the normal comparison. */
3576 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3577 : TREE_OPERAND (arg1, N), flags)
3578 :
3579 : #define OP_SAME_WITH_NULL(N) \
3580 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3581 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3582 :
3583 1545719366 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3584 : {
3585 7854431 : case tcc_unary:
3586 : /* Two conversions are equal only if signedness and modes match. */
3587 7854431 : switch (TREE_CODE (arg0))
3588 : {
3589 7486989 : CASE_CONVERT:
3590 7486989 : case FIX_TRUNC_EXPR:
3591 7486989 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3592 : return false;
3593 : break;
3594 : default:
3595 : break;
3596 : }
3597 :
3598 7854410 : return OP_SAME_WITH_NULL (0);
3599 :
3600 :
3601 23103379 : case tcc_comparison:
3602 23103379 : case tcc_binary:
3603 23103379 : if (OP_SAME (0) && OP_SAME (1))
3604 : return true;
3605 :
3606 : /* For commutative ops, allow the other order. */
3607 16996715 : return (commutative_tree_code (TREE_CODE (arg0))
3608 12917923 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3609 12917923 : TREE_OPERAND (arg1, 1), flags)
3610 17230770 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3611 234055 : TREE_OPERAND (arg1, 0), flags));
3612 :
3613 880181879 : case tcc_reference:
3614 : /* If either of the pointer (or reference) expressions we are
3615 : dereferencing contain a side effect, these cannot be equal,
3616 : but their addresses can be. */
3617 880181879 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3618 880181879 : && (TREE_SIDE_EFFECTS (arg0)
3619 811465076 : || TREE_SIDE_EFFECTS (arg1)))
3620 : return false;
3621 :
3622 879656288 : switch (TREE_CODE (arg0))
3623 : {
3624 4694489 : case INDIRECT_REF:
3625 4694489 : if (!(flags & OEP_ADDRESS_OF))
3626 : {
3627 4672034 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3628 : return false;
3629 : /* Verify that the access types are compatible. */
3630 4665497 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3631 : return false;
3632 : }
3633 4621512 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3634 4621512 : return OP_SAME (0);
3635 :
3636 575622 : case IMAGPART_EXPR:
3637 : /* Require the same offset. */
3638 575622 : if (!operand_equal_p (TYPE_SIZE (type0),
3639 575622 : TYPE_SIZE (type1),
3640 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3641 : return false;
3642 :
3643 : /* Fallthru. */
3644 2305321 : case REALPART_EXPR:
3645 2305321 : case VIEW_CONVERT_EXPR:
3646 2305321 : return OP_SAME (0);
3647 :
3648 87592970 : case TARGET_MEM_REF:
3649 87592970 : case MEM_REF:
3650 87592970 : if (!(flags & OEP_ADDRESS_OF))
3651 : {
3652 : /* Require equal access sizes */
3653 17288486 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3654 17288486 : && (!TYPE_SIZE (type0)
3655 1294319 : || !TYPE_SIZE (type1)
3656 1288129 : || !operand_equal_p (TYPE_SIZE (type0),
3657 1288129 : TYPE_SIZE (type1),
3658 : flags)))
3659 : return false;
3660 : /* Verify that access happens in similar types. */
3661 15989629 : if (!types_compatible_p (type0, type1))
3662 : return false;
3663 : /* Verify that accesses are TBAA compatible. */
3664 15651058 : if (!alias_ptr_types_compatible_p
3665 15651058 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3666 15651058 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3667 14768966 : || (MR_DEPENDENCE_CLIQUE (arg0)
3668 14768966 : != MR_DEPENDENCE_CLIQUE (arg1))
3669 28646736 : || (MR_DEPENDENCE_BASE (arg0)
3670 12995678 : != MR_DEPENDENCE_BASE (arg1)))
3671 : return false;
3672 : /* Verify that alignment is compatible. */
3673 12455948 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3674 : return false;
3675 : }
3676 82545339 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3677 141090246 : return (OP_SAME (0) && OP_SAME (1)
3678 : /* TARGET_MEM_REF require equal extra operands. */
3679 107709263 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3680 536711 : || (OP_SAME_WITH_NULL (2)
3681 276479 : && OP_SAME_WITH_NULL (3)
3682 270897 : && OP_SAME_WITH_NULL (4))));
3683 :
3684 36113546 : case ARRAY_REF:
3685 36113546 : case ARRAY_RANGE_REF:
3686 36113546 : if (!OP_SAME (0))
3687 : return false;
3688 31316207 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3689 : /* Compare the array index by value if it is constant first as we
3690 : may have different types but same value here. */
3691 31316207 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3692 31316207 : TREE_OPERAND (arg1, 1))
3693 28220181 : || OP_SAME (1))
3694 6224976 : && OP_SAME_WITH_NULL (2)
3695 6223656 : && OP_SAME_WITH_NULL (3)
3696 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3697 : we have to account for the offset of the ref. */
3698 40652351 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3699 3111828 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3700 2536 : || (operand_equal_p (array_ref_low_bound
3701 2536 : (const_cast<tree> (arg0)),
3702 : array_ref_low_bound
3703 2536 : (const_cast<tree> (arg1)),
3704 : flags)
3705 2536 : && operand_equal_p (array_ref_element_size
3706 2536 : (const_cast<tree> (arg0)),
3707 : array_ref_element_size
3708 2536 : (const_cast<tree> (arg1)),
3709 : flags))));
3710 :
3711 748309306 : case COMPONENT_REF:
3712 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3713 : may be NULL when we're called to compare MEM_EXPRs. */
3714 748309306 : if (!OP_SAME_WITH_NULL (0))
3715 : return false;
3716 59161720 : {
3717 59161720 : bool compare_address = flags & OEP_ADDRESS_OF;
3718 :
3719 : /* Most of time we only need to compare FIELD_DECLs for equality.
3720 : However when determining address look into actual offsets.
3721 : These may match for unions and unshared record types. */
3722 59161720 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3723 59161720 : if (!OP_SAME (1))
3724 : {
3725 34179286 : if (compare_address
3726 650522 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3727 : {
3728 648413 : tree field0 = TREE_OPERAND (arg0, 1);
3729 648413 : tree field1 = TREE_OPERAND (arg1, 1);
3730 :
3731 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3732 648413 : if (TREE_CODE (field0) != FIELD_DECL
3733 648413 : || TREE_CODE (field1) != FIELD_DECL)
3734 : return false;
3735 :
3736 648413 : if (!DECL_FIELD_OFFSET (field0)
3737 648413 : || !DECL_FIELD_OFFSET (field1))
3738 3 : return field0 == field1;
3739 :
3740 648410 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3741 648410 : DECL_FIELD_OFFSET (field1), flags)
3742 840338 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3743 191928 : DECL_FIELD_BIT_OFFSET (field1),
3744 : flags))
3745 : return false;
3746 : }
3747 : else
3748 : return false;
3749 : }
3750 : }
3751 25017208 : return OP_SAME_WITH_NULL (2);
3752 :
3753 640464 : case BIT_FIELD_REF:
3754 640464 : if (!OP_SAME (0))
3755 : return false;
3756 378880 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3757 378880 : return OP_SAME (1) && OP_SAME (2);
3758 :
3759 : default:
3760 : return false;
3761 : }
3762 :
3763 60230233 : case tcc_expression:
3764 60230233 : switch (TREE_CODE (arg0))
3765 : {
3766 54837262 : case ADDR_EXPR:
3767 : /* Be sure we pass right ADDRESS_OF flag. */
3768 54837262 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3769 54837262 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3770 54837262 : TREE_OPERAND (arg1, 0),
3771 54837262 : flags | OEP_ADDRESS_OF);
3772 :
3773 611108 : case TRUTH_NOT_EXPR:
3774 611108 : return OP_SAME (0);
3775 :
3776 79511 : case TRUTH_ANDIF_EXPR:
3777 79511 : case TRUTH_ORIF_EXPR:
3778 79511 : return OP_SAME (0) && OP_SAME (1);
3779 :
3780 0 : case WIDEN_MULT_PLUS_EXPR:
3781 0 : case WIDEN_MULT_MINUS_EXPR:
3782 0 : if (!OP_SAME (2))
3783 : return false;
3784 : /* The multiplication operands are commutative. */
3785 : /* FALLTHRU */
3786 :
3787 47700 : case TRUTH_AND_EXPR:
3788 47700 : case TRUTH_OR_EXPR:
3789 47700 : case TRUTH_XOR_EXPR:
3790 47700 : if (OP_SAME (0) && OP_SAME (1))
3791 : return true;
3792 :
3793 : /* Otherwise take into account this is a commutative operation. */
3794 47682 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3795 47682 : TREE_OPERAND (arg1, 1), flags)
3796 47685 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3797 3 : TREE_OPERAND (arg1, 0), flags));
3798 :
3799 215781 : case COND_EXPR:
3800 215781 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3801 : return false;
3802 169924 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3803 169924 : return OP_SAME (0);
3804 :
3805 4 : case BIT_INSERT_EXPR:
3806 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3807 : of op1. Need to check to make sure they are the same. */
3808 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3809 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3810 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3811 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3812 : return false;
3813 : /* FALLTHRU */
3814 :
3815 371 : case VEC_COND_EXPR:
3816 371 : case DOT_PROD_EXPR:
3817 371 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3818 :
3819 38525 : case MODIFY_EXPR:
3820 38525 : case INIT_EXPR:
3821 38525 : case COMPOUND_EXPR:
3822 38525 : case PREDECREMENT_EXPR:
3823 38525 : case PREINCREMENT_EXPR:
3824 38525 : case POSTDECREMENT_EXPR:
3825 38525 : case POSTINCREMENT_EXPR:
3826 38525 : if (flags & OEP_LEXICOGRAPHIC)
3827 165 : return OP_SAME (0) && OP_SAME (1);
3828 : return false;
3829 :
3830 315130 : case CLEANUP_POINT_EXPR:
3831 315130 : case EXPR_STMT:
3832 315130 : case SAVE_EXPR:
3833 315130 : if (flags & OEP_LEXICOGRAPHIC)
3834 208 : return OP_SAME (0);
3835 : return false;
3836 :
3837 81156 : case OBJ_TYPE_REF:
3838 : /* Virtual table reference. */
3839 162312 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3840 81156 : OBJ_TYPE_REF_EXPR (arg1), flags))
3841 : return false;
3842 15968 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3843 15968 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3844 15968 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3845 : return false;
3846 15968 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3847 15968 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3848 : return false;
3849 15968 : if (virtual_method_call_p (arg0))
3850 : {
3851 15968 : if (!virtual_method_call_p (arg1))
3852 : return false;
3853 15968 : return types_same_for_odr (obj_type_ref_class (arg0),
3854 31936 : obj_type_ref_class (arg1));
3855 : }
3856 : return false;
3857 :
3858 661 : case OMP_ARRAY_SECTION:
3859 661 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3860 :
3861 : default:
3862 : return false;
3863 : }
3864 :
3865 3747420 : case tcc_vl_exp:
3866 3747420 : switch (TREE_CODE (arg0))
3867 : {
3868 3747420 : case CALL_EXPR:
3869 3747420 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3870 3747420 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3871 : /* If not both CALL_EXPRs are either internal or normal function
3872 : functions, then they are not equal. */
3873 : return false;
3874 3747226 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3875 : {
3876 : /* If the CALL_EXPRs call different internal functions, then they
3877 : are not equal. */
3878 14 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3879 : return false;
3880 : }
3881 : else
3882 : {
3883 : /* If the CALL_EXPRs call different functions, then they are not
3884 : equal. */
3885 3747212 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3886 : flags))
3887 : return false;
3888 : }
3889 :
3890 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3891 2182845 : {
3892 2182845 : unsigned int cef = call_expr_flags (arg0);
3893 2182845 : if (flags & OEP_PURE_SAME)
3894 0 : cef &= ECF_CONST | ECF_PURE;
3895 : else
3896 2182845 : cef &= ECF_CONST;
3897 2182845 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3898 : return false;
3899 : }
3900 :
3901 : /* Now see if all the arguments are the same. */
3902 35175 : {
3903 35175 : const_call_expr_arg_iterator iter0, iter1;
3904 35175 : const_tree a0, a1;
3905 70350 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3906 35175 : a1 = first_const_call_expr_arg (arg1, &iter1);
3907 43315 : a0 && a1;
3908 8140 : a0 = next_const_call_expr_arg (&iter0),
3909 8140 : a1 = next_const_call_expr_arg (&iter1))
3910 36681 : if (! operand_equal_p (a0, a1, flags))
3911 : return false;
3912 :
3913 : /* If we get here and both argument lists are exhausted
3914 : then the CALL_EXPRs are equal. */
3915 6634 : return ! (a0 || a1);
3916 : }
3917 : default:
3918 : return false;
3919 : }
3920 :
3921 213834969 : case tcc_declaration:
3922 : /* Consider __builtin_sqrt equal to sqrt. */
3923 213834969 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3924 7098176 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3925 267555 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3926 6484361 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3927 267555 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3928 :
3929 207350608 : if (DECL_P (arg0)
3930 207350608 : && (flags & OEP_DECL_NAME)
3931 35 : && (flags & OEP_LEXICOGRAPHIC))
3932 : {
3933 : /* Consider decls with the same name equal. The caller needs
3934 : to make sure they refer to the same entity (such as a function
3935 : formal parameter). */
3936 35 : tree a0name = DECL_NAME (arg0);
3937 35 : tree a1name = DECL_NAME (arg1);
3938 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3939 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3940 35 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3941 : }
3942 : return false;
3943 :
3944 354148285 : case tcc_exceptional:
3945 354148285 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3946 : {
3947 19834 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3948 : return false;
3949 :
3950 : /* In GIMPLE constructors are used only to build vectors from
3951 : elements. Individual elements in the constructor must be
3952 : indexed in increasing order and form an initial sequence.
3953 :
3954 : We make no effort to compare nonconstant ones in GENERIC. */
3955 19834 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3956 : return false;
3957 :
3958 : /* Be sure that vectors constructed have the same representation.
3959 : We only tested element precision and modes to match.
3960 : Vectors may be BLKmode and thus also check that the number of
3961 : parts match. */
3962 1079 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3963 2158 : TYPE_VECTOR_SUBPARTS (type1)))
3964 : return false;
3965 :
3966 1079 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3967 1079 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3968 1079 : unsigned int len = vec_safe_length (v0);
3969 :
3970 2158 : if (len != vec_safe_length (v1))
3971 : return false;
3972 :
3973 4665 : for (unsigned int i = 0; i < len; i++)
3974 : {
3975 3991 : constructor_elt *c0 = &(*v0)[i];
3976 3991 : constructor_elt *c1 = &(*v1)[i];
3977 :
3978 3991 : if (!operand_equal_p (c0->value, c1->value, flags)
3979 : /* In GIMPLE the indexes can be either NULL or matching i.
3980 : Double check this so we won't get false
3981 : positives for GENERIC. */
3982 3616 : || (c0->index
3983 2714 : && (TREE_CODE (c0->index) != INTEGER_CST
3984 2714 : || compare_tree_int (c0->index, i)))
3985 7607 : || (c1->index
3986 2714 : && (TREE_CODE (c1->index) != INTEGER_CST
3987 2714 : || compare_tree_int (c1->index, i))))
3988 : return false;
3989 : }
3990 : return true;
3991 : }
3992 354128451 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3993 3322 : && (flags & OEP_LEXICOGRAPHIC))
3994 : {
3995 : /* Compare the STATEMENT_LISTs. */
3996 16 : tree_stmt_iterator tsi1, tsi2;
3997 16 : tree body1 = const_cast<tree> (arg0);
3998 16 : tree body2 = const_cast<tree> (arg1);
3999 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
4000 40 : tsi_next (&tsi1), tsi_next (&tsi2))
4001 : {
4002 : /* The lists don't have the same number of statements. */
4003 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
4004 : return false;
4005 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
4006 : return true;
4007 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
4008 : flags & (OEP_LEXICOGRAPHIC
4009 : | OEP_NO_HASH_CHECK)))
4010 : return false;
4011 : }
4012 : }
4013 : return false;
4014 :
4015 2618572 : case tcc_statement:
4016 2618572 : switch (TREE_CODE (arg0))
4017 : {
4018 52 : case RETURN_EXPR:
4019 52 : if (flags & OEP_LEXICOGRAPHIC)
4020 52 : return OP_SAME_WITH_NULL (0);
4021 : return false;
4022 4 : case DEBUG_BEGIN_STMT:
4023 4 : if (flags & OEP_LEXICOGRAPHIC)
4024 : return true;
4025 : return false;
4026 : default:
4027 : return false;
4028 : }
4029 :
4030 : default:
4031 : return false;
4032 : }
4033 :
4034 : #undef OP_SAME
4035 : #undef OP_SAME_WITH_NULL
4036 : }
4037 :
4038 : /* Generate a hash value for an expression. This can be used iteratively
4039 : by passing a previous result as the HSTATE argument. */
4040 :
4041 : void
4042 3016071265 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
4043 : unsigned int flags)
4044 : {
4045 3016071265 : int i;
4046 3016071265 : enum tree_code code;
4047 3016071265 : enum tree_code_class tclass;
4048 :
4049 3016071265 : if (t == NULL_TREE || t == error_mark_node)
4050 : {
4051 77442918 : hstate.merge_hash (0);
4052 77442918 : return;
4053 : }
4054 :
4055 2938628347 : STRIP_ANY_LOCATION_WRAPPER (t);
4056 :
4057 2938628347 : if (!(flags & OEP_ADDRESS_OF))
4058 2682841754 : STRIP_NOPS (t);
4059 :
4060 2938628347 : code = TREE_CODE (t);
4061 :
4062 2938628347 : switch (code)
4063 : {
4064 : /* Alas, constants aren't shared, so we can't rely on pointer
4065 : identity. */
4066 806 : case VOID_CST:
4067 806 : hstate.merge_hash (0);
4068 806 : return;
4069 864998148 : case INTEGER_CST:
4070 864998148 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4071 1750469217 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4072 885471069 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4073 : return;
4074 14992631 : case REAL_CST:
4075 14992631 : {
4076 14992631 : unsigned int val2;
4077 14992631 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4078 : val2 = rvc_zero;
4079 : else
4080 14773334 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4081 14992631 : hstate.merge_hash (val2);
4082 14992631 : return;
4083 : }
4084 0 : case FIXED_CST:
4085 0 : {
4086 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4087 0 : hstate.merge_hash (val2);
4088 0 : return;
4089 : }
4090 12713956 : case STRING_CST:
4091 12713956 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4092 12713956 : TREE_STRING_LENGTH (t));
4093 12713956 : return;
4094 211 : case RAW_DATA_CST:
4095 211 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4096 211 : RAW_DATA_LENGTH (t));
4097 211 : return;
4098 208845 : case COMPLEX_CST:
4099 208845 : hash_operand (TREE_REALPART (t), hstate, flags);
4100 208845 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4101 208845 : return;
4102 3464172 : case VECTOR_CST:
4103 3464172 : {
4104 3464172 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4105 3464172 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4106 3464172 : unsigned int count = vector_cst_encoded_nelts (t);
4107 14314035 : for (unsigned int i = 0; i < count; ++i)
4108 7385691 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4109 : return;
4110 : }
4111 863691536 : case SSA_NAME:
4112 : /* We can just compare by pointer. */
4113 863691536 : hstate.add_hwi (SSA_NAME_VERSION (t));
4114 863691536 : return;
4115 : case PLACEHOLDER_EXPR:
4116 : /* The node itself doesn't matter. */
4117 : return;
4118 : case BLOCK:
4119 : case OMP_CLAUSE:
4120 : case OMP_NEXT_VARIANT:
4121 : case OMP_TARGET_DEVICE_MATCHES:
4122 : /* Ignore. */
4123 : return;
4124 : case TREE_LIST:
4125 : /* A list of expressions, for a CALL_EXPR or as the elements of a
4126 : VECTOR_CST. */
4127 259290 : for (; t; t = TREE_CHAIN (t))
4128 129645 : hash_operand (TREE_VALUE (t), hstate, flags);
4129 : return;
4130 4982738 : case CONSTRUCTOR:
4131 4982738 : {
4132 4982738 : unsigned HOST_WIDE_INT idx;
4133 4982738 : tree field, value;
4134 4982738 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4135 4982738 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4136 19986534 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4137 : {
4138 : /* In GIMPLE the indexes can be either NULL or matching i. */
4139 15003796 : if (field == NULL_TREE)
4140 1126328 : field = bitsize_int (idx);
4141 15003796 : if (TREE_CODE (field) == FIELD_DECL)
4142 : {
4143 9978938 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4144 9978938 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4145 : }
4146 : else
4147 5024858 : hash_operand (field, hstate, flags);
4148 15003796 : hash_operand (value, hstate, flags);
4149 : }
4150 : return;
4151 : }
4152 182 : case STATEMENT_LIST:
4153 182 : {
4154 182 : tree_stmt_iterator i;
4155 182 : for (i = tsi_start (const_cast<tree> (t));
4156 550 : !tsi_end_p (i); tsi_next (&i))
4157 368 : hash_operand (tsi_stmt (i), hstate, flags);
4158 182 : return;
4159 : }
4160 : case TREE_VEC:
4161 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4162 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4163 : return;
4164 4 : case IDENTIFIER_NODE:
4165 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4166 4 : return;
4167 21266484 : case FUNCTION_DECL:
4168 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4169 : Otherwise nodes that compare equal according to operand_equal_p might
4170 : get different hash codes. However, don't do this for machine specific
4171 : or front end builtins, since the function code is overloaded in those
4172 : cases. */
4173 21266484 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4174 21266484 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4175 : {
4176 7164526 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4177 7164526 : code = TREE_CODE (t);
4178 : }
4179 : /* FALL THROUGH */
4180 1173445346 : default:
4181 1173445346 : if (POLY_INT_CST_P (t))
4182 : {
4183 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4184 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4185 : return;
4186 : }
4187 1173445346 : tclass = TREE_CODE_CLASS (code);
4188 :
4189 1173445346 : if (tclass == tcc_declaration)
4190 : {
4191 : /* DECL's have a unique ID */
4192 834532137 : hstate.add_hwi (DECL_UID (t));
4193 : }
4194 338913209 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4195 : {
4196 : /* For comparisons that can be swapped, use the lower
4197 : tree code. */
4198 153732 : enum tree_code ccode = swap_tree_comparison (code);
4199 153732 : if (code < ccode)
4200 69518 : ccode = code;
4201 153732 : hstate.add_object (ccode);
4202 153732 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4203 153732 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4204 : }
4205 338759477 : else if (CONVERT_EXPR_CODE_P (code))
4206 : {
4207 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4208 : operand_equal_p. */
4209 6835633 : enum tree_code ccode = NOP_EXPR;
4210 6835633 : hstate.add_object (ccode);
4211 :
4212 : /* Don't hash the type, that can lead to having nodes which
4213 : compare equal according to operand_equal_p, but which
4214 : have different hash codes. Make sure to include signedness
4215 : in the hash computation. */
4216 6835633 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4217 6835633 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4218 : }
4219 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4220 331923844 : else if (code == MEM_REF
4221 80020569 : && (flags & OEP_ADDRESS_OF) != 0
4222 70544551 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4223 13930042 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4224 345645053 : && integer_zerop (TREE_OPERAND (t, 1)))
4225 6247736 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4226 : hstate, flags);
4227 : /* Don't ICE on FE specific trees, or their arguments etc.
4228 : during operand_equal_p hash verification. */
4229 325676108 : else if (!IS_EXPR_CODE_CLASS (tclass))
4230 384 : gcc_assert (flags & OEP_HASH_CHECK);
4231 : else
4232 : {
4233 325675724 : unsigned int sflags = flags;
4234 :
4235 325675724 : hstate.add_object (code);
4236 :
4237 325675724 : switch (code)
4238 : {
4239 130697121 : case ADDR_EXPR:
4240 130697121 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4241 130697121 : flags |= OEP_ADDRESS_OF;
4242 130697121 : sflags = flags;
4243 130697121 : break;
4244 :
4245 78713838 : case INDIRECT_REF:
4246 78713838 : case MEM_REF:
4247 78713838 : case TARGET_MEM_REF:
4248 78713838 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4249 78713838 : sflags = flags;
4250 78713838 : break;
4251 :
4252 76133037 : case COMPONENT_REF:
4253 76133037 : if (sflags & OEP_ADDRESS_OF)
4254 : {
4255 37776897 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4256 37776897 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4257 : hstate, flags & ~OEP_ADDRESS_OF);
4258 37776897 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4259 : hstate, flags & ~OEP_ADDRESS_OF);
4260 37776897 : return;
4261 : }
4262 : break;
4263 15756975 : case ARRAY_REF:
4264 15756975 : case ARRAY_RANGE_REF:
4265 15756975 : case BIT_FIELD_REF:
4266 15756975 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4267 15756975 : break;
4268 :
4269 8522 : case COND_EXPR:
4270 8522 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4271 8522 : break;
4272 :
4273 0 : case WIDEN_MULT_PLUS_EXPR:
4274 0 : case WIDEN_MULT_MINUS_EXPR:
4275 0 : {
4276 : /* The multiplication operands are commutative. */
4277 0 : inchash::hash one, two;
4278 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4279 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4280 0 : hstate.add_commutative (one, two);
4281 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4282 0 : return;
4283 : }
4284 :
4285 44100 : case CALL_EXPR:
4286 44100 : if (CALL_EXPR_FN (t) == NULL_TREE)
4287 14 : hstate.add_int (CALL_EXPR_IFN (t));
4288 : break;
4289 :
4290 72 : case TARGET_EXPR:
4291 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4292 : Usually different TARGET_EXPRs just should use
4293 : different temporaries in their slots. */
4294 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4295 72 : return;
4296 :
4297 291781 : case OBJ_TYPE_REF:
4298 : /* Virtual table reference. */
4299 291781 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4300 291781 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4301 291781 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4302 291781 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4303 291781 : if (!virtual_method_call_p (t))
4304 : return;
4305 291760 : if (tree c = obj_type_ref_class (t))
4306 : {
4307 291760 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4308 : /* We compute mangled names only when free_lang_data is run.
4309 : In that case we can hash precisely. */
4310 291760 : if (TREE_CODE (c) == TYPE_DECL
4311 291760 : && DECL_ASSEMBLER_NAME_SET_P (c))
4312 7314 : hstate.add_object
4313 7314 : (IDENTIFIER_HASH_VALUE
4314 : (DECL_ASSEMBLER_NAME (c)));
4315 : }
4316 : return;
4317 : default:
4318 : break;
4319 : }
4320 :
4321 : /* Don't hash the type, that can lead to having nodes which
4322 : compare equal according to operand_equal_p, but which
4323 : have different hash codes. */
4324 287606974 : if (code == NON_LVALUE_EXPR)
4325 : {
4326 : /* Make sure to include signness in the hash computation. */
4327 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4328 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4329 : }
4330 :
4331 287606974 : else if (commutative_tree_code (code))
4332 : {
4333 : /* It's a commutative expression. We want to hash it the same
4334 : however it appears. We do this by first hashing both operands
4335 : and then rehashing based on the order of their independent
4336 : hashes. */
4337 17642799 : inchash::hash one, two;
4338 17642799 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4339 17642799 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4340 17642799 : hstate.add_commutative (one, two);
4341 : }
4342 : else
4343 754484270 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4344 699076085 : hash_operand (TREE_OPERAND (t, i), hstate,
4345 : i == 0 ? flags : sflags);
4346 : }
4347 : return;
4348 : }
4349 : }
4350 :
4351 : bool
4352 7429340375 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4353 : unsigned int flags, bool *ret)
4354 : {
4355 : /* When checking and unless comparing DECL names, verify that if
4356 : the outermost operand_equal_p call returns non-zero then ARG0
4357 : and ARG1 have the same hash value. */
4358 7429340375 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4359 : {
4360 3133828599 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4361 : {
4362 476695223 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4363 : {
4364 85335258 : inchash::hash hstate0 (0), hstate1 (0);
4365 85335258 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4366 85335258 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4367 85335258 : hashval_t h0 = hstate0.end ();
4368 85335258 : hashval_t h1 = hstate1.end ();
4369 85335258 : gcc_assert (h0 == h1);
4370 : }
4371 476695223 : *ret = true;
4372 : }
4373 : else
4374 2657133376 : *ret = false;
4375 :
4376 : return true;
4377 : }
4378 :
4379 : return false;
4380 : }
4381 :
4382 :
4383 : static operand_compare default_compare_instance;
4384 :
4385 : /* Convenience wrapper around operand_compare class because usually we do
4386 : not need to play with the valueizer. */
4387 :
4388 : bool
4389 3131936577 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4390 : {
4391 3131936577 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4392 : }
4393 :
4394 : namespace inchash
4395 : {
4396 :
4397 : /* Generate a hash value for an expression. This can be used iteratively
4398 : by passing a previous result as the HSTATE argument.
4399 :
4400 : This function is intended to produce the same hash for expressions which
4401 : would compare equal using operand_equal_p. */
4402 : void
4403 2147376937 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4404 : {
4405 2147376937 : default_compare_instance.hash_operand (t, hstate, flags);
4406 2147376937 : }
4407 :
4408 : }
4409 :
4410 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4411 : with a different signedness or a narrower precision. */
4412 :
4413 : static bool
4414 20472596 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4415 : {
4416 20472596 : if (operand_equal_p (arg0, arg1, 0))
4417 : return true;
4418 :
4419 39156052 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4420 33427296 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4421 : return false;
4422 :
4423 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4424 : and see if the inner values are the same. This removes any
4425 : signedness comparison, which doesn't matter here. */
4426 6237994 : tree op0 = arg0;
4427 6237994 : tree op1 = arg1;
4428 6237994 : STRIP_NOPS (op0);
4429 6237994 : STRIP_NOPS (op1);
4430 6237994 : if (operand_equal_p (op0, op1, 0))
4431 : return true;
4432 :
4433 : /* Discard a single widening conversion from ARG1 and see if the inner
4434 : value is the same as ARG0. */
4435 5159661 : if (CONVERT_EXPR_P (arg1)
4436 894632 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4437 894578 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4438 894578 : < TYPE_PRECISION (TREE_TYPE (arg1))
4439 6358050 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4440 : return true;
4441 :
4442 : return false;
4443 : }
4444 :
4445 : /* See if ARG is an expression that is either a comparison or is performing
4446 : arithmetic on comparisons. The comparisons must only be comparing
4447 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4448 : they are nonzero it means that some operands have already been found.
4449 : No variables may be used anywhere else in the expression except in the
4450 : comparisons.
4451 :
4452 : If this is true, return 1. Otherwise, return zero. */
4453 :
4454 : static bool
4455 60520947 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4456 : {
4457 64530574 : enum tree_code code = TREE_CODE (arg);
4458 64530574 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4459 :
4460 : /* We can handle some of the tcc_expression cases here. */
4461 64530574 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4462 : tclass = tcc_unary;
4463 63913788 : else if (tclass == tcc_expression
4464 695557 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4465 695557 : || code == COMPOUND_EXPR))
4466 : tclass = tcc_binary;
4467 :
4468 63902849 : switch (tclass)
4469 : {
4470 4009627 : case tcc_unary:
4471 4009627 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4472 :
4473 5346298 : case tcc_binary:
4474 5346298 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4475 5346298 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4476 :
4477 : case tcc_constant:
4478 : return true;
4479 :
4480 684618 : case tcc_expression:
4481 684618 : if (code == COND_EXPR)
4482 692 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4483 692 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4484 756 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4485 : return false;
4486 :
4487 621590 : case tcc_comparison:
4488 : /* First see if we can handle the first operand, then the second. For
4489 : the second operand, we know *CVAL1 can't be zero. It must be that
4490 : one side of the comparison is each of the values; test for the
4491 : case where this isn't true by failing if the two operands
4492 : are the same. */
4493 :
4494 621590 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4495 621590 : TREE_OPERAND (arg, 1), 0))
4496 : return false;
4497 :
4498 621590 : if (*cval1 == 0)
4499 619378 : *cval1 = TREE_OPERAND (arg, 0);
4500 2212 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4501 : ;
4502 2093 : else if (*cval2 == 0)
4503 0 : *cval2 = TREE_OPERAND (arg, 0);
4504 2093 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4505 : ;
4506 : else
4507 : return false;
4508 :
4509 619497 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4510 : ;
4511 619497 : else if (*cval2 == 0)
4512 619378 : *cval2 = TREE_OPERAND (arg, 1);
4513 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4514 : ;
4515 : else
4516 : return false;
4517 :
4518 : return true;
4519 :
4520 : default:
4521 : return false;
4522 : }
4523 : }
4524 :
4525 : /* ARG is a tree that is known to contain just arithmetic operations and
4526 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4527 : any occurrence of OLD0 as an operand of a comparison and likewise for
4528 : NEW1 and OLD1. */
4529 :
4530 : static tree
4531 663 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4532 : tree old1, tree new1)
4533 : {
4534 663 : tree type = TREE_TYPE (arg);
4535 663 : enum tree_code code = TREE_CODE (arg);
4536 663 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4537 :
4538 : /* We can handle some of the tcc_expression cases here. */
4539 663 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4540 : tclass = tcc_unary;
4541 663 : else if (tclass == tcc_expression
4542 30 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4543 : tclass = tcc_binary;
4544 :
4545 642 : switch (tclass)
4546 : {
4547 168 : case tcc_unary:
4548 168 : return fold_build1_loc (loc, code, type,
4549 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4550 168 : old0, new0, old1, new1));
4551 :
4552 153 : case tcc_binary:
4553 306 : return fold_build2_loc (loc, code, type,
4554 153 : eval_subst (loc, TREE_OPERAND (arg, 0),
4555 : old0, new0, old1, new1),
4556 153 : eval_subst (loc, TREE_OPERAND (arg, 1),
4557 153 : old0, new0, old1, new1));
4558 :
4559 9 : case tcc_expression:
4560 9 : switch (code)
4561 : {
4562 0 : case SAVE_EXPR:
4563 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4564 0 : old1, new1);
4565 :
4566 0 : case COMPOUND_EXPR:
4567 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4568 0 : old1, new1);
4569 :
4570 9 : case COND_EXPR:
4571 27 : return fold_build3_loc (loc, code, type,
4572 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4573 : old0, new0, old1, new1),
4574 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4575 : old0, new0, old1, new1),
4576 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4577 9 : old0, new0, old1, new1));
4578 : default:
4579 : break;
4580 : }
4581 : /* Fall through - ??? */
4582 :
4583 168 : case tcc_comparison:
4584 168 : {
4585 168 : tree arg0 = TREE_OPERAND (arg, 0);
4586 168 : tree arg1 = TREE_OPERAND (arg, 1);
4587 :
4588 : /* We need to check both for exact equality and tree equality. The
4589 : former will be true if the operand has a side-effect. In that
4590 : case, we know the operand occurred exactly once. */
4591 :
4592 168 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4593 : arg0 = new0;
4594 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4595 : arg0 = new1;
4596 :
4597 168 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4598 : arg1 = new0;
4599 168 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4600 : arg1 = new1;
4601 :
4602 168 : return fold_build2_loc (loc, code, type, arg0, arg1);
4603 : }
4604 :
4605 : default:
4606 : return arg;
4607 : }
4608 : }
4609 :
4610 : /* Return a tree for the case when the result of an expression is RESULT
4611 : converted to TYPE and OMITTED was previously an operand of the expression
4612 : but is now not needed (e.g., we folded OMITTED * 0).
4613 :
4614 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4615 : the conversion of RESULT to TYPE. */
4616 :
4617 : tree
4618 310857 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4619 : {
4620 310857 : tree t = fold_convert_loc (loc, type, result);
4621 :
4622 : /* If the resulting operand is an empty statement, just return the omitted
4623 : statement casted to void. */
4624 310857 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4625 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4626 0 : fold_ignored_result (omitted));
4627 :
4628 310857 : if (TREE_SIDE_EFFECTS (omitted))
4629 11948 : return build2_loc (loc, COMPOUND_EXPR, type,
4630 11948 : fold_ignored_result (omitted), t);
4631 :
4632 298909 : return non_lvalue_loc (loc, t);
4633 : }
4634 :
4635 : /* Return a tree for the case when the result of an expression is RESULT
4636 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4637 : of the expression but are now not needed.
4638 :
4639 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4640 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4641 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4642 : just do the conversion of RESULT to TYPE. */
4643 :
4644 : tree
4645 6135 : omit_two_operands_loc (location_t loc, tree type, tree result,
4646 : tree omitted1, tree omitted2)
4647 : {
4648 6135 : tree t = fold_convert_loc (loc, type, result);
4649 :
4650 6135 : if (TREE_SIDE_EFFECTS (omitted2))
4651 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4652 6135 : if (TREE_SIDE_EFFECTS (omitted1))
4653 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4654 :
4655 6135 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4656 : }
4657 :
4658 :
4659 : /* Return a simplified tree node for the truth-negation of ARG. This
4660 : never alters ARG itself. We assume that ARG is an operation that
4661 : returns a truth value (0 or 1).
4662 :
4663 : FIXME: one would think we would fold the result, but it causes
4664 : problems with the dominator optimizer. */
4665 :
4666 : static tree
4667 51126077 : fold_truth_not_expr (location_t loc, tree arg)
4668 : {
4669 51126077 : tree type = TREE_TYPE (arg);
4670 51126077 : enum tree_code code = TREE_CODE (arg);
4671 51126077 : location_t loc1, loc2;
4672 :
4673 : /* If this is a comparison, we can simply invert it, except for
4674 : floating-point non-equality comparisons, in which case we just
4675 : enclose a TRUTH_NOT_EXPR around what we have. */
4676 :
4677 51126077 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4678 : {
4679 39078597 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4680 32420152 : if (FLOAT_TYPE_P (op_type)
4681 6668532 : && flag_trapping_math
4682 6637597 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4683 45676842 : && code != NE_EXPR && code != EQ_EXPR)
4684 : return NULL_TREE;
4685 :
4686 33176048 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4687 33176048 : if (code == ERROR_MARK)
4688 : return NULL_TREE;
4689 :
4690 33176048 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4691 33176048 : TREE_OPERAND (arg, 1));
4692 33176048 : copy_warning (ret, arg);
4693 33176048 : return ret;
4694 : }
4695 :
4696 12047480 : switch (code)
4697 : {
4698 0 : case INTEGER_CST:
4699 0 : return constant_boolean_node (integer_zerop (arg), type);
4700 :
4701 42708 : case TRUTH_AND_EXPR:
4702 42708 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4703 42708 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4704 85416 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4705 42708 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4706 85416 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4707 :
4708 2650 : case TRUTH_OR_EXPR:
4709 2650 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4710 2650 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4711 5300 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4712 2650 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4713 5300 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4714 :
4715 72882 : case TRUTH_XOR_EXPR:
4716 : /* Here we can invert either operand. We invert the first operand
4717 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4718 : result is the XOR of the first operand with the inside of the
4719 : negation of the second operand. */
4720 :
4721 72882 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4722 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4723 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4724 : else
4725 72694 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4726 72694 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4727 145388 : TREE_OPERAND (arg, 1));
4728 :
4729 394589 : case TRUTH_ANDIF_EXPR:
4730 394589 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4731 394589 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4732 789178 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4733 394589 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4734 789178 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4735 :
4736 16942 : case TRUTH_ORIF_EXPR:
4737 16942 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4738 16942 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4739 33884 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4740 16942 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4741 33884 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4742 :
4743 791532 : case TRUTH_NOT_EXPR:
4744 791532 : return TREE_OPERAND (arg, 0);
4745 :
4746 9753 : case COND_EXPR:
4747 9753 : {
4748 9753 : tree arg1 = TREE_OPERAND (arg, 1);
4749 9753 : tree arg2 = TREE_OPERAND (arg, 2);
4750 :
4751 9753 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4752 9753 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4753 :
4754 : /* A COND_EXPR may have a throw as one operand, which
4755 : then has void type. Just leave void operands
4756 : as they are. */
4757 9753 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4758 9753 : VOID_TYPE_P (TREE_TYPE (arg1))
4759 9753 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4760 9753 : VOID_TYPE_P (TREE_TYPE (arg2))
4761 19503 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4762 : }
4763 :
4764 991 : case COMPOUND_EXPR:
4765 991 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4766 1982 : return build2_loc (loc, COMPOUND_EXPR, type,
4767 991 : TREE_OPERAND (arg, 0),
4768 1982 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4769 :
4770 0 : case NON_LVALUE_EXPR:
4771 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4772 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4773 :
4774 78725 : CASE_CONVERT:
4775 78725 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4776 78661 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4777 :
4778 : /* fall through */
4779 :
4780 64 : case FLOAT_EXPR:
4781 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4782 64 : return build1_loc (loc, TREE_CODE (arg), type,
4783 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4784 :
4785 492 : case BIT_AND_EXPR:
4786 492 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4787 : return NULL_TREE;
4788 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4789 :
4790 2 : case SAVE_EXPR:
4791 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4792 :
4793 432 : case CLEANUP_POINT_EXPR:
4794 432 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4795 432 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4796 864 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4797 :
4798 : default:
4799 : return NULL_TREE;
4800 : }
4801 : }
4802 :
4803 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4804 : assume that ARG is an operation that returns a truth value (0 or 1
4805 : for scalars, 0 or -1 for vectors). Return the folded expression if
4806 : folding is successful. Otherwise, return NULL_TREE. */
4807 :
4808 : static tree
4809 2109465 : fold_invert_truthvalue (location_t loc, tree arg)
4810 : {
4811 2109465 : tree type = TREE_TYPE (arg);
4812 4218906 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4813 : ? BIT_NOT_EXPR
4814 : : TRUTH_NOT_EXPR,
4815 2109465 : type, arg);
4816 : }
4817 :
4818 : /* Return a simplified tree node for the truth-negation of ARG. This
4819 : never alters ARG itself. We assume that ARG is an operation that
4820 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4821 :
4822 : tree
4823 43474499 : invert_truthvalue_loc (location_t loc, tree arg)
4824 : {
4825 43474499 : if (TREE_CODE (arg) == ERROR_MARK)
4826 : return arg;
4827 :
4828 43474499 : tree type = TREE_TYPE (arg);
4829 86948998 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4830 : ? BIT_NOT_EXPR
4831 : : TRUTH_NOT_EXPR,
4832 43474499 : type, arg);
4833 : }
4834 :
4835 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4836 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4837 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4838 : is the original memory reference used to preserve the alias set of
4839 : the access. */
4840 :
4841 : tree
4842 836356 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4843 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4844 : int unsignedp, int reversep)
4845 : {
4846 836356 : tree result, bftype;
4847 :
4848 : /* Attempt not to lose the access path if possible. */
4849 836356 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4850 : {
4851 832350 : tree ninner = TREE_OPERAND (orig_inner, 0);
4852 832350 : machine_mode nmode;
4853 832350 : poly_int64 nbitsize, nbitpos;
4854 832350 : tree noffset;
4855 832350 : int nunsignedp, nreversep, nvolatilep = 0;
4856 832350 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4857 : &noffset, &nmode, &nunsignedp,
4858 : &nreversep, &nvolatilep);
4859 832350 : if (base == inner
4860 832223 : && noffset == NULL_TREE
4861 832223 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4862 832216 : && !reversep
4863 832135 : && !nreversep
4864 1664485 : && !nvolatilep)
4865 : {
4866 832135 : inner = ninner;
4867 832350 : bitpos -= nbitpos;
4868 : }
4869 : }
4870 :
4871 836356 : alias_set_type iset = get_alias_set (orig_inner);
4872 836356 : if (iset == 0 && get_alias_set (inner) != iset)
4873 242 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4874 : build_fold_addr_expr (inner),
4875 : build_int_cst (ptr_type_node, 0));
4876 :
4877 836356 : if (known_eq (bitpos, 0) && !reversep)
4878 : {
4879 13172 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4880 26344 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4881 12959 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4882 217 : && tree_fits_shwi_p (size)
4883 13389 : && tree_to_shwi (size) == bitsize)
4884 194 : return fold_convert_loc (loc, type, inner);
4885 : }
4886 :
4887 836162 : bftype = type;
4888 836162 : if (TYPE_PRECISION (bftype) != bitsize
4889 836162 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4890 641 : bftype = build_nonstandard_integer_type (bitsize, 0);
4891 :
4892 836162 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4893 836162 : bitsize_int (bitsize), bitsize_int (bitpos));
4894 836162 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4895 :
4896 836162 : if (bftype != type)
4897 641 : result = fold_convert_loc (loc, type, result);
4898 :
4899 : return result;
4900 : }
4901 :
4902 : /* Optimize a bit-field compare.
4903 :
4904 : There are two cases: First is a compare against a constant and the
4905 : second is a comparison of two items where the fields are at the same
4906 : bit position relative to the start of a chunk (byte, halfword, word)
4907 : large enough to contain it. In these cases we can avoid the shift
4908 : implicit in bitfield extractions.
4909 :
4910 : For constants, we emit a compare of the shifted constant with the
4911 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4912 : compared. For two fields at the same position, we do the ANDs with the
4913 : similar mask and compare the result of the ANDs.
4914 :
4915 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4916 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4917 : are the left and right operands of the comparison, respectively.
4918 :
4919 : If the optimization described above can be done, we return the resulting
4920 : tree. Otherwise we return zero. */
4921 :
4922 : static tree
4923 4698951 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4924 : tree compare_type, tree lhs, tree rhs)
4925 : {
4926 4698951 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4927 4698951 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4928 4698951 : tree type = TREE_TYPE (lhs);
4929 4698951 : tree unsigned_type;
4930 4698951 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4931 4698951 : machine_mode lmode, rmode;
4932 4698951 : scalar_int_mode nmode;
4933 4698951 : int lunsignedp, runsignedp;
4934 4698951 : int lreversep, rreversep;
4935 4698951 : int lvolatilep = 0, rvolatilep = 0;
4936 4698951 : tree linner, rinner = NULL_TREE;
4937 4698951 : tree mask;
4938 4698951 : tree offset;
4939 :
4940 : /* Get all the information about the extractions being done. If the bit size
4941 : is the same as the size of the underlying object, we aren't doing an
4942 : extraction at all and so can do nothing. We also don't want to
4943 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4944 : then will no longer be able to replace it. */
4945 4698951 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4946 : &lunsignedp, &lreversep, &lvolatilep);
4947 4698951 : if (linner == lhs
4948 4698951 : || !known_size_p (plbitsize)
4949 4698951 : || !plbitsize.is_constant (&lbitsize)
4950 4698951 : || !plbitpos.is_constant (&lbitpos)
4951 9397902 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4952 797333 : || offset != 0
4953 797308 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4954 5496259 : || lvolatilep)
4955 : return 0;
4956 :
4957 797248 : if (const_p)
4958 759293 : rreversep = lreversep;
4959 : else
4960 : {
4961 : /* If this is not a constant, we can only do something if bit positions,
4962 : sizes, signedness and storage order are the same. */
4963 37955 : rinner
4964 37955 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4965 : &runsignedp, &rreversep, &rvolatilep);
4966 :
4967 37955 : if (rinner == rhs
4968 37911 : || maybe_ne (lbitpos, rbitpos)
4969 37877 : || maybe_ne (lbitsize, rbitsize)
4970 37877 : || lunsignedp != runsignedp
4971 37877 : || lreversep != rreversep
4972 37877 : || offset != 0
4973 37877 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4974 75832 : || rvolatilep)
4975 : return 0;
4976 : }
4977 :
4978 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4979 797170 : poly_uint64 bitstart = 0;
4980 797170 : poly_uint64 bitend = 0;
4981 797170 : if (TREE_CODE (lhs) == COMPONENT_REF)
4982 : {
4983 797170 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4984 797170 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4985 : return 0;
4986 : }
4987 :
4988 : /* See if we can find a mode to refer to this field. We should be able to,
4989 : but fail if we can't. */
4990 1594340 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4991 759293 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4992 37877 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4993 : TYPE_ALIGN (TREE_TYPE (rinner))),
4994 797170 : BITS_PER_WORD, false, &nmode))
4995 : return 0;
4996 :
4997 : /* Set signed and unsigned types of the precision of this mode for the
4998 : shifts below. */
4999 795167 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
5000 :
5001 : /* Compute the bit position and size for the new reference and our offset
5002 : within it. If the new reference is the same size as the original, we
5003 : won't optimize anything, so return zero. */
5004 795167 : nbitsize = GET_MODE_BITSIZE (nmode);
5005 795167 : nbitpos = lbitpos & ~ (nbitsize - 1);
5006 795167 : lbitpos -= nbitpos;
5007 795167 : if (nbitsize == lbitsize)
5008 : return 0;
5009 :
5010 795167 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5011 63 : lbitpos = nbitsize - lbitsize - lbitpos;
5012 :
5013 : /* Make the mask to be used against the extracted field. */
5014 795167 : mask = build_int_cst_type (unsigned_type, -1);
5015 795167 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
5016 795167 : mask = const_binop (RSHIFT_EXPR, mask,
5017 795167 : size_int (nbitsize - lbitsize - lbitpos));
5018 :
5019 795167 : if (! const_p)
5020 : {
5021 36322 : if (nbitpos < 0)
5022 : return 0;
5023 :
5024 : /* If not comparing with constant, just rework the comparison
5025 : and return. */
5026 36322 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5027 36322 : nbitsize, nbitpos, 1, lreversep);
5028 36322 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
5029 36322 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
5030 36322 : nbitsize, nbitpos, 1, rreversep);
5031 36322 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
5032 36322 : return fold_build2_loc (loc, code, compare_type, t1, t2);
5033 : }
5034 :
5035 : /* Otherwise, we are handling the constant case. See if the constant is too
5036 : big for the field. Warn and return a tree for 0 (false) if so. We do
5037 : this not only for its own sake, but to avoid having to test for this
5038 : error case below. If we didn't, we might generate wrong code.
5039 :
5040 : For unsigned fields, the constant shifted right by the field length should
5041 : be all zero. For signed fields, the high-order bits should agree with
5042 : the sign bit. */
5043 :
5044 758845 : if (lunsignedp)
5045 : {
5046 757673 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
5047 : {
5048 0 : warning (0, "comparison is always %d due to width of bit-field",
5049 : code == NE_EXPR);
5050 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5051 : }
5052 : }
5053 : else
5054 : {
5055 1172 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5056 1172 : if (tem != 0 && tem != -1)
5057 : {
5058 0 : warning (0, "comparison is always %d due to width of bit-field",
5059 : code == NE_EXPR);
5060 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5061 : }
5062 1172 : }
5063 :
5064 758845 : if (nbitpos < 0)
5065 : return 0;
5066 :
5067 : /* Single-bit compares should always be against zero. */
5068 758845 : if (lbitsize == 1 && ! integer_zerop (rhs))
5069 : {
5070 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5071 175 : rhs = build_int_cst (type, 0);
5072 : }
5073 :
5074 : /* Make a new bitfield reference, shift the constant over the
5075 : appropriate number of bits and mask it with the computed mask
5076 : (in case this was a signed field). If we changed it, make a new one. */
5077 758845 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5078 758845 : nbitsize, nbitpos, 1, lreversep);
5079 :
5080 758845 : rhs = const_binop (BIT_AND_EXPR,
5081 : const_binop (LSHIFT_EXPR,
5082 : fold_convert_loc (loc, unsigned_type, rhs),
5083 758845 : size_int (lbitpos)),
5084 : mask);
5085 :
5086 758845 : lhs = build2_loc (loc, code, compare_type,
5087 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5088 758845 : return lhs;
5089 : }
5090 :
5091 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5092 : represents the sign bit of EXP's type. If EXP represents a sign
5093 : or zero extension, also test VAL against the unextended type.
5094 : The return value is the (sub)expression whose sign bit is VAL,
5095 : or NULL_TREE otherwise. */
5096 :
5097 : tree
5098 2892 : sign_bit_p (tree exp, const_tree val)
5099 : {
5100 2892 : int width;
5101 2892 : tree t;
5102 :
5103 : /* Tree EXP must have an integral type. */
5104 2892 : t = TREE_TYPE (exp);
5105 2892 : if (! INTEGRAL_TYPE_P (t))
5106 : return NULL_TREE;
5107 :
5108 : /* Tree VAL must be an integer constant. */
5109 2546 : if (TREE_CODE (val) != INTEGER_CST
5110 2546 : || TREE_OVERFLOW (val))
5111 : return NULL_TREE;
5112 :
5113 2181 : width = TYPE_PRECISION (t);
5114 2181 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5115 : return exp;
5116 :
5117 : /* Handle extension from a narrower type. */
5118 1310 : if (TREE_CODE (exp) == NOP_EXPR
5119 1310 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5120 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5121 :
5122 : return NULL_TREE;
5123 : }
5124 :
5125 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5126 : operand is simple enough to be evaluated unconditionally. */
5127 :
5128 : static bool
5129 66116055 : simple_operand_p (const_tree exp)
5130 : {
5131 : /* Strip any conversions that don't change the machine mode. */
5132 66116055 : STRIP_NOPS (exp);
5133 :
5134 66116055 : return (CONSTANT_CLASS_P (exp)
5135 45916175 : || TREE_CODE (exp) == SSA_NAME
5136 82244179 : || (DECL_P (exp)
5137 5596676 : && ! TREE_ADDRESSABLE (exp)
5138 5508860 : && ! TREE_THIS_VOLATILE (exp)
5139 5508860 : && ! DECL_NONLOCAL (exp)
5140 : /* Don't regard global variables as simple. They may be
5141 : allocated in ways unknown to the compiler (shared memory,
5142 : #pragma weak, etc). */
5143 5507207 : && ! TREE_PUBLIC (exp)
5144 5486441 : && ! DECL_EXTERNAL (exp)
5145 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5146 5486441 : && ! ((VAR_P (exp)
5147 : || TREE_CODE (exp) == PARM_DECL
5148 : || TREE_CODE (exp) == RESULT_DECL)
5149 5486441 : && DECL_HAS_VALUE_EXPR_P (exp))
5150 : /* Weakrefs are not safe to be read, since they can be NULL.
5151 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5152 : have DECL_WEAK flag set. */
5153 5485852 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5154 : /* Loading a static variable is unduly expensive, but global
5155 : registers aren't expensive. */
5156 5485852 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5157 : }
5158 :
5159 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5160 : In addition to simple_operand_p, we assume that comparisons, conversions,
5161 : and logic-not operations are simple, if their operands are simple, too. */
5162 :
5163 : bool
5164 7092132 : simple_condition_p (tree exp)
5165 : {
5166 7185178 : enum tree_code code;
5167 :
5168 7185178 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5169 : return false;
5170 :
5171 2292791 : while (CONVERT_EXPR_P (exp))
5172 34620 : exp = TREE_OPERAND (exp, 0);
5173 :
5174 2258171 : code = TREE_CODE (exp);
5175 :
5176 2258171 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5177 1764235 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5178 1764235 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5179 :
5180 493936 : if (code == TRUTH_NOT_EXPR)
5181 93046 : return simple_condition_p (TREE_OPERAND (exp, 0));
5182 :
5183 400890 : return simple_operand_p (exp);
5184 : }
5185 :
5186 :
5187 : /* The following functions are subroutines to fold_range_test and allow it to
5188 : try to change a logical combination of comparisons into a range test.
5189 :
5190 : For example, both
5191 : X == 2 || X == 3 || X == 4 || X == 5
5192 : and
5193 : X >= 2 && X <= 5
5194 : are converted to
5195 : (unsigned) (X - 2) <= 3
5196 :
5197 : We describe each set of comparisons as being either inside or outside
5198 : a range, using a variable named like IN_P, and then describe the
5199 : range with a lower and upper bound. If one of the bounds is omitted,
5200 : it represents either the highest or lowest value of the type.
5201 :
5202 : In the comments below, we represent a range by two numbers in brackets
5203 : preceded by a "+" to designate being inside that range, or a "-" to
5204 : designate being outside that range, so the condition can be inverted by
5205 : flipping the prefix. An omitted bound is represented by a "-". For
5206 : example, "- [-, 10]" means being outside the range starting at the lowest
5207 : possible value and ending at 10, in other words, being greater than 10.
5208 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5209 : always false.
5210 :
5211 : We set up things so that the missing bounds are handled in a consistent
5212 : manner so neither a missing bound nor "true" and "false" need to be
5213 : handled using a special case. */
5214 :
5215 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5216 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5217 : and UPPER1_P are nonzero if the respective argument is an upper bound
5218 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5219 : must be specified for a comparison. ARG1 will be converted to ARG0's
5220 : type if both are specified. */
5221 :
5222 : static tree
5223 23001581 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5224 : tree arg1, int upper1_p)
5225 : {
5226 23001581 : tree tem;
5227 23001581 : int result;
5228 23001581 : int sgn0, sgn1;
5229 :
5230 : /* If neither arg represents infinity, do the normal operation.
5231 : Else, if not a comparison, return infinity. Else handle the special
5232 : comparison rules. Note that most of the cases below won't occur, but
5233 : are handled for consistency. */
5234 :
5235 23001581 : if (arg0 != 0 && arg1 != 0)
5236 : {
5237 12098636 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5238 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5239 12098636 : STRIP_NOPS (tem);
5240 12098636 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5241 : }
5242 :
5243 10902945 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5244 : return 0;
5245 :
5246 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5247 : for neither. In real maths, we cannot assume open ended ranges are
5248 : the same. But, this is computer arithmetic, where numbers are finite.
5249 : We can therefore make the transformation of any unbounded range with
5250 : the value Z, Z being greater than any representable number. This permits
5251 : us to treat unbounded ranges as equal. */
5252 10893813 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5253 10893813 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5254 10893813 : switch (code)
5255 : {
5256 5132987 : case EQ_EXPR:
5257 5132987 : result = sgn0 == sgn1;
5258 5132987 : break;
5259 0 : case NE_EXPR:
5260 0 : result = sgn0 != sgn1;
5261 0 : break;
5262 363060 : case LT_EXPR:
5263 363060 : result = sgn0 < sgn1;
5264 363060 : break;
5265 2501236 : case LE_EXPR:
5266 2501236 : result = sgn0 <= sgn1;
5267 2501236 : break;
5268 2896530 : case GT_EXPR:
5269 2896530 : result = sgn0 > sgn1;
5270 2896530 : break;
5271 0 : case GE_EXPR:
5272 0 : result = sgn0 >= sgn1;
5273 0 : break;
5274 0 : default:
5275 0 : gcc_unreachable ();
5276 : }
5277 :
5278 10893813 : return constant_boolean_node (result, type);
5279 : }
5280 :
5281 : /* Helper routine for make_range. Perform one step for it, return
5282 : new expression if the loop should continue or NULL_TREE if it should
5283 : stop. */
5284 :
5285 : tree
5286 61050763 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5287 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5288 : {
5289 61050763 : tree arg0_type = TREE_TYPE (arg0);
5290 61050763 : tree n_low, n_high, low = *p_low, high = *p_high;
5291 61050763 : int in_p = *p_in_p, n_in_p;
5292 :
5293 61050763 : switch (code)
5294 : {
5295 1735102 : case TRUTH_NOT_EXPR:
5296 : /* We can only do something if the range is testing for zero. */
5297 1735102 : if (low == NULL_TREE || high == NULL_TREE
5298 1735102 : || ! integer_zerop (low) || ! integer_zerop (high))
5299 : return NULL_TREE;
5300 1735102 : *p_in_p = ! in_p;
5301 1735102 : return arg0;
5302 :
5303 47437751 : case EQ_EXPR: case NE_EXPR:
5304 47437751 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5305 : /* We can only do something if the range is testing for zero
5306 : and if the second operand is an integer constant. Note that
5307 : saying something is "in" the range we make is done by
5308 : complementing IN_P since it will set in the initial case of
5309 : being not equal to zero; "out" is leaving it alone. */
5310 47437751 : if (low == NULL_TREE || high == NULL_TREE
5311 47437751 : || ! integer_zerop (low) || ! integer_zerop (high)
5312 94875413 : || TREE_CODE (arg1) != INTEGER_CST)
5313 : return NULL_TREE;
5314 :
5315 29879799 : switch (code)
5316 : {
5317 : case NE_EXPR: /* - [c, c] */
5318 : low = high = arg1;
5319 : break;
5320 8076828 : case EQ_EXPR: /* + [c, c] */
5321 8076828 : in_p = ! in_p, low = high = arg1;
5322 8076828 : break;
5323 2203565 : case GT_EXPR: /* - [-, c] */
5324 2203565 : low = 0, high = arg1;
5325 2203565 : break;
5326 726893 : case GE_EXPR: /* + [c, -] */
5327 726893 : in_p = ! in_p, low = arg1, high = 0;
5328 726893 : break;
5329 5793737 : case LT_EXPR: /* - [c, -] */
5330 5793737 : low = arg1, high = 0;
5331 5793737 : break;
5332 4384787 : case LE_EXPR: /* + [-, c] */
5333 4384787 : in_p = ! in_p, low = 0, high = arg1;
5334 4384787 : break;
5335 : default:
5336 : gcc_unreachable ();
5337 : }
5338 :
5339 : /* If this is an unsigned comparison, we also know that EXP is
5340 : greater than or equal to zero. We base the range tests we make
5341 : on that fact, so we record it here so we can parse existing
5342 : range tests. We test arg0_type since often the return type
5343 : of, e.g. EQ_EXPR, is boolean. */
5344 29879799 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5345 : {
5346 1908365 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5347 : in_p, low, high, 1,
5348 : build_int_cst (arg0_type, 0),
5349 : NULL_TREE))
5350 : return NULL_TREE;
5351 :
5352 1908356 : in_p = n_in_p, low = n_low, high = n_high;
5353 :
5354 : /* If the high bound is missing, but we have a nonzero low
5355 : bound, reverse the range so it goes from zero to the low bound
5356 : minus 1. */
5357 1908356 : if (high == 0 && low && ! integer_zerop (low))
5358 : {
5359 846744 : in_p = ! in_p;
5360 846744 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5361 846744 : build_int_cst (TREE_TYPE (low), 1), 0);
5362 846744 : low = build_int_cst (arg0_type, 0);
5363 : }
5364 : }
5365 :
5366 29879790 : *p_low = low;
5367 29879790 : *p_high = high;
5368 29879790 : *p_in_p = in_p;
5369 29879790 : return arg0;
5370 :
5371 365 : case NEGATE_EXPR:
5372 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5373 : low and high are non-NULL, then normalize will DTRT. */
5374 365 : if (!TYPE_UNSIGNED (arg0_type)
5375 365 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5376 : {
5377 93 : if (low == NULL_TREE)
5378 18 : low = TYPE_MIN_VALUE (arg0_type);
5379 93 : if (high == NULL_TREE)
5380 39 : high = TYPE_MAX_VALUE (arg0_type);
5381 : }
5382 :
5383 : /* (-x) IN [a,b] -> x in [-b, -a] */
5384 365 : n_low = range_binop (MINUS_EXPR, exp_type,
5385 : build_int_cst (exp_type, 0),
5386 : 0, high, 1);
5387 365 : n_high = range_binop (MINUS_EXPR, exp_type,
5388 : build_int_cst (exp_type, 0),
5389 : 0, low, 0);
5390 365 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5391 : return NULL_TREE;
5392 347 : goto normalize;
5393 :
5394 56 : case BIT_NOT_EXPR:
5395 : /* ~ X -> -X - 1 */
5396 56 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5397 : build_int_cst (exp_type, 1));
5398 :
5399 871230 : case PLUS_EXPR:
5400 871230 : case MINUS_EXPR:
5401 871230 : if (TREE_CODE (arg1) != INTEGER_CST)
5402 : return NULL_TREE;
5403 :
5404 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5405 : move a constant to the other side. */
5406 663933 : if (!TYPE_UNSIGNED (arg0_type)
5407 663933 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5408 : return NULL_TREE;
5409 :
5410 : /* If EXP is signed, any overflow in the computation is undefined,
5411 : so we don't worry about it so long as our computations on
5412 : the bounds don't overflow. For unsigned, overflow is defined
5413 : and this is exactly the right thing. */
5414 952731 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5415 : arg0_type, low, 0, arg1, 0);
5416 477898 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5417 : arg0_type, high, 1, arg1, 0);
5418 474323 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5419 952209 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5420 : return NULL_TREE;
5421 :
5422 478233 : normalize:
5423 : /* Check for an unsigned range which has wrapped around the maximum
5424 : value thus making n_high < n_low, and normalize it. */
5425 478233 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5426 : {
5427 135190 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5428 135190 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5429 135190 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5430 135190 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5431 :
5432 : /* If the range is of the form +/- [ x+1, x ], we won't
5433 : be able to normalize it. But then, it represents the
5434 : whole range or the empty set, so make it
5435 : +/- [ -, - ]. */
5436 135190 : if (tree_int_cst_equal (n_low, low)
5437 135190 : && tree_int_cst_equal (n_high, high))
5438 : low = high = 0;
5439 : else
5440 135190 : in_p = ! in_p;
5441 : }
5442 : else
5443 343043 : low = n_low, high = n_high;
5444 :
5445 478233 : *p_low = low;
5446 478233 : *p_high = high;
5447 478233 : *p_in_p = in_p;
5448 478233 : return arg0;
5449 :
5450 2606036 : CASE_CONVERT:
5451 2606036 : case NON_LVALUE_EXPR:
5452 2606036 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5453 : return NULL_TREE;
5454 :
5455 1171433 : if (! INTEGRAL_TYPE_P (arg0_type)
5456 1136930 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5457 1043870 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5458 : return NULL_TREE;
5459 :
5460 1025289 : n_low = low, n_high = high;
5461 :
5462 1025289 : if (n_low != 0)
5463 856509 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5464 :
5465 1025289 : if (n_high != 0)
5466 960366 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5467 :
5468 : /* If we're converting arg0 from an unsigned type, to exp,
5469 : a signed type, we will be doing the comparison as unsigned.
5470 : The tests above have already verified that LOW and HIGH
5471 : are both positive.
5472 :
5473 : So we have to ensure that we will handle large unsigned
5474 : values the same way that the current signed bounds treat
5475 : negative values. */
5476 :
5477 1025289 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5478 : {
5479 258734 : tree high_positive;
5480 258734 : tree equiv_type;
5481 : /* For fixed-point modes, we need to pass the saturating flag
5482 : as the 2nd parameter. */
5483 258734 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5484 0 : equiv_type
5485 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5486 0 : TYPE_SATURATING (arg0_type));
5487 258734 : else if (BITINT_TYPE_P (arg0_type))
5488 : equiv_type = arg0_type;
5489 : else
5490 258719 : equiv_type
5491 258719 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5492 :
5493 : /* A range without an upper bound is, naturally, unbounded.
5494 : Since convert would have cropped a very large value, use
5495 : the max value for the destination type. */
5496 258734 : high_positive
5497 258734 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5498 0 : : TYPE_MAX_VALUE (arg0_type);
5499 :
5500 258734 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5501 237402 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5502 : fold_convert_loc (loc, arg0_type,
5503 : high_positive),
5504 : build_int_cst (arg0_type, 1));
5505 :
5506 : /* If the low bound is specified, "and" the range with the
5507 : range for which the original unsigned value will be
5508 : positive. */
5509 258734 : if (low != 0)
5510 : {
5511 95591 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5512 : 1, fold_convert_loc (loc, arg0_type,
5513 : integer_zero_node),
5514 : high_positive))
5515 : return NULL_TREE;
5516 :
5517 95591 : in_p = (n_in_p == in_p);
5518 : }
5519 : else
5520 : {
5521 : /* Otherwise, "or" the range with the range of the input
5522 : that will be interpreted as negative. */
5523 163143 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5524 : 1, fold_convert_loc (loc, arg0_type,
5525 : integer_zero_node),
5526 : high_positive))
5527 : return NULL_TREE;
5528 :
5529 163143 : in_p = (in_p != n_in_p);
5530 : }
5531 : }
5532 :
5533 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5534 : an unsigned type, we will do the comparison as signed. If
5535 : high is non-NULL, we punt above if it doesn't fit in the signed
5536 : type, so if we get through here, +[-, high] or +[low, high] are
5537 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5538 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5539 : high is not, the +[low, -] range is equivalent to union of
5540 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5541 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5542 : low being 0, which should be treated as [-, -]. */
5543 766555 : else if (TYPE_UNSIGNED (exp_type)
5544 747911 : && !TYPE_UNSIGNED (arg0_type)
5545 387086 : && low
5546 1153641 : && !high)
5547 : {
5548 12 : if (integer_zerop (low))
5549 12 : n_low = NULL_TREE;
5550 : else
5551 : {
5552 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5553 : n_low, build_int_cst (arg0_type, -1));
5554 0 : n_low = build_zero_cst (arg0_type);
5555 0 : in_p = !in_p;
5556 : }
5557 : }
5558 :
5559 1025289 : *p_low = n_low;
5560 1025289 : *p_high = n_high;
5561 1025289 : *p_in_p = in_p;
5562 1025289 : return arg0;
5563 :
5564 : default:
5565 : return NULL_TREE;
5566 : }
5567 : }
5568 :
5569 : /* Given EXP, a logical expression, set the range it is testing into
5570 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5571 : actually being tested. *PLOW and *PHIGH will be made of the same
5572 : type as the returned expression. If EXP is not a comparison, we
5573 : will most likely not be returning a useful value and range. */
5574 :
5575 : tree
5576 50471788 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5577 : {
5578 50471788 : enum tree_code code;
5579 50471788 : tree arg0, arg1 = NULL_TREE;
5580 50471788 : tree exp_type, nexp;
5581 50471788 : int in_p;
5582 50471788 : tree low, high;
5583 50471788 : location_t loc = EXPR_LOCATION (exp);
5584 :
5585 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5586 : and see if we can refine the range. Some of the cases below may not
5587 : happen, but it doesn't seem worth worrying about this. We "continue"
5588 : the outer loop when we've changed something; otherwise we "break"
5589 : the switch, which will "break" the while. */
5590 :
5591 50471788 : in_p = 0;
5592 50471788 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5593 :
5594 80505561 : while (1)
5595 : {
5596 80505561 : code = TREE_CODE (exp);
5597 80505561 : exp_type = TREE_TYPE (exp);
5598 80505561 : arg0 = NULL_TREE;
5599 :
5600 80505561 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5601 : {
5602 56238754 : if (TREE_OPERAND_LENGTH (exp) > 0)
5603 56238754 : arg0 = TREE_OPERAND (exp, 0);
5604 56238754 : if (TREE_CODE_CLASS (code) == tcc_binary
5605 53124947 : || TREE_CODE_CLASS (code) == tcc_comparison
5606 65993908 : || (TREE_CODE_CLASS (code) == tcc_expression
5607 2922454 : && TREE_OPERAND_LENGTH (exp) > 1))
5608 47653786 : arg1 = TREE_OPERAND (exp, 1);
5609 : }
5610 56238754 : if (arg0 == NULL_TREE)
5611 : break;
5612 :
5613 56238740 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5614 : &high, &in_p);
5615 56238740 : if (nexp == NULL_TREE)
5616 : break;
5617 : exp = nexp;
5618 : }
5619 :
5620 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5621 50471788 : if (TREE_CODE (exp) == INTEGER_CST)
5622 : {
5623 31614 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5624 : exp, 0, low, 0))
5625 31614 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5626 : exp, 1, high, 1)));
5627 31614 : low = high = 0;
5628 31614 : exp = 0;
5629 : }
5630 :
5631 50471788 : *pin_p = in_p, *plow = low, *phigh = high;
5632 50471788 : return exp;
5633 : }
5634 :
5635 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5636 : a bitwise check i.e. when
5637 : LOW == 0xXX...X00...0
5638 : HIGH == 0xXX...X11...1
5639 : Return corresponding mask in MASK and stem in VALUE. */
5640 :
5641 : static bool
5642 112 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5643 : tree *value)
5644 : {
5645 112 : if (TREE_CODE (low) != INTEGER_CST
5646 112 : || TREE_CODE (high) != INTEGER_CST)
5647 : return false;
5648 :
5649 112 : unsigned prec = TYPE_PRECISION (type);
5650 112 : wide_int lo = wi::to_wide (low, prec);
5651 112 : wide_int hi = wi::to_wide (high, prec);
5652 :
5653 112 : wide_int end_mask = lo ^ hi;
5654 224 : if ((end_mask & (end_mask + 1)) != 0
5655 222 : || (lo & end_mask) != 0)
5656 : return false;
5657 :
5658 86 : wide_int stem_mask = ~end_mask;
5659 86 : wide_int stem = lo & stem_mask;
5660 86 : if (stem != (hi & stem_mask))
5661 : return false;
5662 :
5663 86 : *mask = wide_int_to_tree (type, stem_mask);
5664 86 : *value = wide_int_to_tree (type, stem);
5665 :
5666 86 : return true;
5667 198 : }
5668 :
5669 : /* Helper routine for build_range_check and match.pd. Return the type to
5670 : perform the check or NULL if it shouldn't be optimized. */
5671 :
5672 : tree
5673 587650 : range_check_type (tree etype)
5674 : {
5675 : /* First make sure that arithmetics in this type is valid, then make sure
5676 : that it wraps around. */
5677 587650 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5678 0 : etype = TREE_TYPE (etype);
5679 587650 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5680 61030 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5681 :
5682 587650 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5683 : {
5684 403803 : tree utype, minv, maxv;
5685 :
5686 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5687 : for the type in question, as we rely on this here. */
5688 403803 : utype = unsigned_type_for (etype);
5689 403803 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5690 403803 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5691 403803 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5692 403803 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5693 :
5694 403803 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5695 : minv, 1, maxv, 1)))
5696 : etype = utype;
5697 : else
5698 109 : return NULL_TREE;
5699 : }
5700 183847 : else if (POINTER_TYPE_P (etype)
5701 : || TREE_CODE (etype) == OFFSET_TYPE
5702 : /* Right now all BITINT_TYPEs satisfy
5703 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5704 : that like for INTEGER_TYPEs. */
5705 : || TREE_CODE (etype) == BITINT_TYPE)
5706 1371 : etype = unsigned_type_for (etype);
5707 : return etype;
5708 : }
5709 :
5710 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5711 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5712 : on IN_P) the range. Return 0 if the test couldn't be created. */
5713 :
5714 : tree
5715 1601778 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5716 : tree low, tree high)
5717 : {
5718 2795890 : tree etype = TREE_TYPE (exp), mask, value;
5719 :
5720 : /* Disable this optimization for function pointer expressions
5721 : on targets that require function pointer canonicalization. */
5722 2795890 : if (targetm.have_canonicalize_funcptr_for_compare ()
5723 0 : && POINTER_TYPE_P (etype)
5724 2795890 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5725 : return NULL_TREE;
5726 :
5727 2795890 : if (! in_p)
5728 : {
5729 309649 : value = build_range_check (loc, type, exp, 1, low, high);
5730 309649 : if (value != 0)
5731 309649 : return invert_truthvalue_loc (loc, value);
5732 :
5733 : return 0;
5734 : }
5735 :
5736 2486241 : if (low == 0 && high == 0)
5737 120325 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5738 :
5739 2365916 : if (low == 0)
5740 777869 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5741 777869 : fold_convert_loc (loc, etype, high));
5742 :
5743 1588047 : if (high == 0)
5744 73537 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5745 73537 : fold_convert_loc (loc, etype, low));
5746 :
5747 1514510 : if (operand_equal_p (low, high, 0))
5748 320158 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5749 320158 : fold_convert_loc (loc, etype, low));
5750 :
5751 1194352 : if (TREE_CODE (exp) == BIT_AND_EXPR
5752 1194352 : && maskable_range_p (low, high, etype, &mask, &value))
5753 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5754 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5755 : exp, mask),
5756 86 : value);
5757 :
5758 1194266 : if (integer_zerop (low))
5759 : {
5760 698149 : if (! TYPE_UNSIGNED (etype))
5761 : {
5762 183960 : etype = unsigned_type_for (etype);
5763 183960 : high = fold_convert_loc (loc, etype, high);
5764 183960 : exp = fold_convert_loc (loc, etype, exp);
5765 : }
5766 : return build_range_check (loc, type, exp, 1, 0, high);
5767 : }
5768 :
5769 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5770 496117 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5771 : {
5772 128630 : int prec = TYPE_PRECISION (etype);
5773 :
5774 128630 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5775 : {
5776 124 : if (TYPE_UNSIGNED (etype))
5777 : {
5778 118 : tree signed_etype = signed_type_for (etype);
5779 118 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5780 0 : etype
5781 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5782 : else
5783 : etype = signed_etype;
5784 118 : exp = fold_convert_loc (loc, etype, exp);
5785 : }
5786 124 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5787 : build_int_cst (etype, 0));
5788 : }
5789 : }
5790 :
5791 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5792 : This requires wrap-around arithmetics for the type of the expression. */
5793 495993 : etype = range_check_type (etype);
5794 495993 : if (etype == NULL_TREE)
5795 : return NULL_TREE;
5796 :
5797 495963 : high = fold_convert_loc (loc, etype, high);
5798 495963 : low = fold_convert_loc (loc, etype, low);
5799 495963 : exp = fold_convert_loc (loc, etype, exp);
5800 :
5801 495963 : value = const_binop (MINUS_EXPR, high, low);
5802 :
5803 495963 : if (value != 0 && !TREE_OVERFLOW (value))
5804 495963 : return build_range_check (loc, type,
5805 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5806 : 1, build_int_cst (etype, 0), value);
5807 :
5808 : return 0;
5809 : }
5810 :
5811 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5812 :
5813 : static tree
5814 174722 : range_predecessor (tree val)
5815 : {
5816 174722 : tree type = TREE_TYPE (val);
5817 :
5818 174722 : if (INTEGRAL_TYPE_P (type)
5819 174722 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5820 : return 0;
5821 : else
5822 174722 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5823 174722 : build_int_cst (TREE_TYPE (val), 1), 0);
5824 : }
5825 :
5826 : /* Return the successor of VAL in its type, handling the infinite case. */
5827 :
5828 : static tree
5829 1649655 : range_successor (tree val)
5830 : {
5831 1649655 : tree type = TREE_TYPE (val);
5832 :
5833 1649655 : if (INTEGRAL_TYPE_P (type)
5834 1649655 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5835 : return 0;
5836 : else
5837 1649646 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5838 1649646 : build_int_cst (TREE_TYPE (val), 1), 0);
5839 : }
5840 :
5841 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5842 : can, 0 if we can't. Set the output range into the specified parameters. */
5843 :
5844 : bool
5845 3608514 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5846 : tree high0, int in1_p, tree low1, tree high1)
5847 : {
5848 3608514 : bool no_overlap;
5849 3608514 : int subset;
5850 3608514 : int temp;
5851 3608514 : tree tem;
5852 3608514 : int in_p;
5853 3608514 : tree low, high;
5854 3608514 : int lowequal = ((low0 == 0 && low1 == 0)
5855 3608514 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5856 3608514 : low0, 0, low1, 0)));
5857 3608514 : int highequal = ((high0 == 0 && high1 == 0)
5858 3608514 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5859 3608514 : high0, 1, high1, 1)));
5860 :
5861 : /* Make range 0 be the range that starts first, or ends last if they
5862 : start at the same value. Swap them if it isn't. */
5863 3608514 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5864 : low0, 0, low1, 0))
5865 3608514 : || (lowequal
5866 570922 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5867 : high1, 1, high0, 1))))
5868 : {
5869 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5870 : tem = low0, low0 = low1, low1 = tem;
5871 : tem = high0, high0 = high1, high1 = tem;
5872 : }
5873 :
5874 : /* If the second range is != high1 where high1 is the type maximum of
5875 : the type, try first merging with < high1 range. */
5876 3608514 : if (low1
5877 3608514 : && high1
5878 1002782 : && TREE_CODE (low1) == INTEGER_CST
5879 1002782 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5880 127744 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5881 171036 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5882 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5883 4569070 : && operand_equal_p (low1, high1, 0))
5884 : {
5885 560178 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5886 560178 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5887 : !in1_p, NULL_TREE, range_predecessor (low1)))
5888 : return true;
5889 : /* Similarly for the second range != low1 where low1 is the type minimum
5890 : of the type, try first merging with > low1 range. */
5891 449425 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5892 449425 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5893 : !in1_p, range_successor (low1), NULL_TREE))
5894 : return true;
5895 : }
5896 :
5897 : /* Now flag two cases, whether the ranges are disjoint or whether the
5898 : second range is totally subsumed in the first. Note that the tests
5899 : below are simplified by the ones above. */
5900 3404643 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5901 : high0, 1, low1, 0));
5902 3404643 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5903 : high1, 1, high0, 1));
5904 :
5905 : /* We now have four cases, depending on whether we are including or
5906 : excluding the two ranges. */
5907 3404643 : if (in0_p && in1_p)
5908 : {
5909 : /* If they don't overlap, the result is false. If the second range
5910 : is a subset it is the result. Otherwise, the range is from the start
5911 : of the second to the end of the first. */
5912 1555658 : if (no_overlap)
5913 : in_p = 0, low = high = 0;
5914 1554142 : else if (subset)
5915 : in_p = 1, low = low1, high = high1;
5916 : else
5917 1434648 : in_p = 1, low = low1, high = high0;
5918 : }
5919 :
5920 1848985 : else if (in0_p && ! in1_p)
5921 : {
5922 : /* If they don't overlap, the result is the first range. If they are
5923 : equal, the result is false. If the second range is a subset of the
5924 : first, and the ranges begin at the same place, we go from just after
5925 : the end of the second range to the end of the first. If the second
5926 : range is not a subset of the first, or if it is a subset and both
5927 : ranges end at the same place, the range starts at the start of the
5928 : first range and ends just before the second range.
5929 : Otherwise, we can't describe this as a single range. */
5930 323963 : if (no_overlap)
5931 : in_p = 1, low = low0, high = high0;
5932 318438 : else if (lowequal && highequal)
5933 : in_p = 0, low = high = 0;
5934 317587 : else if (subset && lowequal)
5935 : {
5936 242674 : low = range_successor (high1);
5937 242674 : high = high0;
5938 242674 : in_p = 1;
5939 242674 : if (low == 0)
5940 : {
5941 : /* We are in the weird situation where high0 > high1 but
5942 : high1 has no successor. Punt. */
5943 : return 0;
5944 : }
5945 : }
5946 74913 : else if (! subset || highequal)
5947 : {
5948 54326 : low = low0;
5949 54326 : high = range_predecessor (low1);
5950 54326 : in_p = 1;
5951 54326 : if (high == 0)
5952 : {
5953 : /* low0 < low1 but low1 has no predecessor. Punt. */
5954 : return 0;
5955 : }
5956 : }
5957 : else
5958 : return 0;
5959 : }
5960 :
5961 1525022 : else if (! in0_p && in1_p)
5962 : {
5963 : /* If they don't overlap, the result is the second range. If the second
5964 : is a subset of the first, the result is false. Otherwise,
5965 : the range starts just after the first range and ends at the
5966 : end of the second. */
5967 1164804 : if (no_overlap)
5968 : in_p = 1, low = low1, high = high1;
5969 1156989 : else if (subset || highequal)
5970 : in_p = 0, low = high = 0;
5971 : else
5972 : {
5973 1050772 : low = range_successor (high0);
5974 1050772 : high = high1;
5975 1050772 : in_p = 1;
5976 1050772 : if (low == 0)
5977 : {
5978 : /* high1 > high0 but high0 has no successor. Punt. */
5979 : return 0;
5980 : }
5981 : }
5982 : }
5983 :
5984 : else
5985 : {
5986 : /* The case where we are excluding both ranges. Here the complex case
5987 : is if they don't overlap. In that case, the only time we have a
5988 : range is if they are adjacent. If the second is a subset of the
5989 : first, the result is the first. Otherwise, the range to exclude
5990 : starts at the beginning of the first range and ends at the end of the
5991 : second. */
5992 360218 : if (no_overlap)
5993 : {
5994 262753 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5995 : range_successor (high0),
5996 : 1, low1, 0)))
5997 : in_p = 0, low = low0, high = high1;
5998 : else
5999 : {
6000 : /* Canonicalize - [min, x] into - [-, x]. */
6001 211492 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
6002 210351 : switch (TREE_CODE (TREE_TYPE (low0)))
6003 : {
6004 51354 : case ENUMERAL_TYPE:
6005 51354 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
6006 : GET_MODE_BITSIZE
6007 102708 : (TYPE_MODE (TREE_TYPE (low0)))))
6008 : break;
6009 : /* FALLTHROUGH */
6010 210150 : case INTEGER_TYPE:
6011 210150 : if (tree_int_cst_equal (low0,
6012 210150 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
6013 211492 : low0 = 0;
6014 : break;
6015 201 : case POINTER_TYPE:
6016 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
6017 201 : && integer_zerop (low0))
6018 : low0 = 0;
6019 : break;
6020 : default:
6021 : break;
6022 : }
6023 :
6024 : /* Canonicalize - [x, max] into - [x, -]. */
6025 211492 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
6026 211305 : switch (TREE_CODE (TREE_TYPE (high1)))
6027 : {
6028 51362 : case ENUMERAL_TYPE:
6029 51362 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
6030 : GET_MODE_BITSIZE
6031 102724 : (TYPE_MODE (TREE_TYPE (high1)))))
6032 : break;
6033 : /* FALLTHROUGH */
6034 211104 : case INTEGER_TYPE:
6035 211104 : if (tree_int_cst_equal (high1,
6036 211104 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
6037 211492 : high1 = 0;
6038 : break;
6039 201 : case POINTER_TYPE:
6040 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
6041 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
6042 : high1, 1,
6043 201 : build_int_cst (TREE_TYPE (high1), 1),
6044 : 1)))
6045 133 : high1 = 0;
6046 : break;
6047 : default:
6048 : break;
6049 : }
6050 :
6051 : /* The ranges might be also adjacent between the maximum and
6052 : minimum values of the given type. For
6053 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6054 : return + [x + 1, y - 1]. */
6055 211492 : if (low0 == 0 && high1 == 0)
6056 : {
6057 338 : low = range_successor (high0);
6058 338 : high = range_predecessor (low1);
6059 338 : if (low == 0 || high == 0)
6060 : return 0;
6061 :
6062 : in_p = 1;
6063 : }
6064 : else
6065 : return 0;
6066 : }
6067 : }
6068 97465 : else if (subset)
6069 : in_p = 0, low = low0, high = high0;
6070 : else
6071 12006 : in_p = 0, low = low0, high = high1;
6072 : }
6073 :
6074 3172893 : *pin_p = in_p, *plow = low, *phigh = high;
6075 3172893 : return 1;
6076 : }
6077 :
6078 :
6079 : /* Subroutine of fold, looking inside expressions of the form
6080 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6081 : are the three operands of the COND_EXPR. This function is
6082 : being used also to optimize A op B ? C : A, by reversing the
6083 : comparison first.
6084 :
6085 : Return a folded expression whose code is not a COND_EXPR
6086 : anymore, or NULL_TREE if no folding opportunity is found. */
6087 :
6088 : static tree
6089 521434 : fold_cond_expr_with_comparison (location_t loc, tree type,
6090 : enum tree_code comp_code,
6091 : tree arg00, tree arg01, tree arg1, tree arg2)
6092 : {
6093 521434 : tree arg1_type = TREE_TYPE (arg1);
6094 521434 : tree tem;
6095 :
6096 521434 : STRIP_NOPS (arg1);
6097 521434 : STRIP_NOPS (arg2);
6098 :
6099 : /* If we have A op 0 ? A : -A, consider applying the following
6100 : transformations:
6101 :
6102 : A == 0? A : -A same as -A
6103 : A != 0? A : -A same as A
6104 : A >= 0? A : -A same as abs (A)
6105 : A > 0? A : -A same as abs (A)
6106 : A <= 0? A : -A same as -abs (A)
6107 : A < 0? A : -A same as -abs (A)
6108 :
6109 : None of these transformations work for modes with signed
6110 : zeros. If A is +/-0, the first two transformations will
6111 : change the sign of the result (from +0 to -0, or vice
6112 : versa). The last four will fix the sign of the result,
6113 : even though the original expressions could be positive or
6114 : negative, depending on the sign of A.
6115 :
6116 : Note that all these transformations are correct if A is
6117 : NaN, since the two alternatives (A and -A) are also NaNs. */
6118 521434 : if (!HONOR_SIGNED_ZEROS (type)
6119 1042872 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6120 521434 : ? real_zerop (arg01)
6121 520948 : : integer_zerop (arg01))
6122 1402474 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6123 1041 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6124 : /* In the case that A is of the form X-Y, '-A' (arg2) may
6125 : have already been folded to Y-X, check for that. */
6126 358789 : || (TREE_CODE (arg1) == MINUS_EXPR
6127 1711 : && TREE_CODE (arg2) == MINUS_EXPR
6128 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6129 0 : TREE_OPERAND (arg2, 1), 0)
6130 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6131 0 : TREE_OPERAND (arg2, 0), 0))))
6132 817 : switch (comp_code)
6133 : {
6134 0 : case EQ_EXPR:
6135 0 : case UNEQ_EXPR:
6136 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6137 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6138 0 : case NE_EXPR:
6139 0 : case LTGT_EXPR:
6140 0 : return fold_convert_loc (loc, type, arg1);
6141 0 : case UNGE_EXPR:
6142 0 : case UNGT_EXPR:
6143 0 : if (flag_trapping_math)
6144 : break;
6145 : /* Fall through. */
6146 801 : case GE_EXPR:
6147 801 : case GT_EXPR:
6148 801 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6149 : break;
6150 785 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6151 785 : return fold_convert_loc (loc, type, tem);
6152 0 : case UNLE_EXPR:
6153 0 : case UNLT_EXPR:
6154 0 : if (flag_trapping_math)
6155 : break;
6156 : /* FALLTHRU */
6157 16 : case LE_EXPR:
6158 16 : case LT_EXPR:
6159 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6160 : break;
6161 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6162 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6163 : {
6164 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6165 : is not, invokes UB both in abs and in the negation of it.
6166 : So, use ABSU_EXPR instead. */
6167 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6168 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6169 16 : tem = negate_expr (tem);
6170 16 : return fold_convert_loc (loc, type, tem);
6171 : }
6172 : else
6173 : {
6174 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6175 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6176 : }
6177 0 : default:
6178 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6179 : break;
6180 : }
6181 :
6182 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6183 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6184 : both transformations are correct when A is NaN: A != 0
6185 : is then true, and A == 0 is false. */
6186 :
6187 520633 : if (!HONOR_SIGNED_ZEROS (type)
6188 520633 : && integer_zerop (arg01) && integer_zerop (arg2))
6189 : {
6190 273340 : if (comp_code == NE_EXPR)
6191 147 : return fold_convert_loc (loc, type, arg1);
6192 273193 : else if (comp_code == EQ_EXPR)
6193 0 : return build_zero_cst (type);
6194 : }
6195 :
6196 : /* Try some transformations of A op B ? A : B.
6197 :
6198 : A == B? A : B same as B
6199 : A != B? A : B same as A
6200 : A >= B? A : B same as max (A, B)
6201 : A > B? A : B same as max (B, A)
6202 : A <= B? A : B same as min (A, B)
6203 : A < B? A : B same as min (B, A)
6204 :
6205 : As above, these transformations don't work in the presence
6206 : of signed zeros. For example, if A and B are zeros of
6207 : opposite sign, the first two transformations will change
6208 : the sign of the result. In the last four, the original
6209 : expressions give different results for (A=+0, B=-0) and
6210 : (A=-0, B=+0), but the transformed expressions do not.
6211 :
6212 : The first two transformations are correct if either A or B
6213 : is a NaN. In the first transformation, the condition will
6214 : be false, and B will indeed be chosen. In the case of the
6215 : second transformation, the condition A != B will be true,
6216 : and A will be chosen.
6217 :
6218 : The conversions to max() and min() are not correct if B is
6219 : a number and A is not. The conditions in the original
6220 : expressions will be false, so all four give B. The min()
6221 : and max() versions would give a NaN instead. */
6222 520486 : if (!HONOR_SIGNED_ZEROS (type)
6223 520486 : && operand_equal_for_comparison_p (arg01, arg2)
6224 : /* Avoid these transformations if the COND_EXPR may be used
6225 : as an lvalue in the C++ front-end. PR c++/19199. */
6226 802737 : && (in_gimple_form
6227 18228 : || VECTOR_TYPE_P (type)
6228 18155 : || (! lang_GNU_CXX ()
6229 15628 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6230 2527 : || ! maybe_lvalue_p (arg1)
6231 2506 : || ! maybe_lvalue_p (arg2)))
6232 : {
6233 280522 : tree comp_op0 = arg00;
6234 280522 : tree comp_op1 = arg01;
6235 280522 : tree comp_type = TREE_TYPE (comp_op0);
6236 :
6237 280522 : switch (comp_code)
6238 : {
6239 0 : case EQ_EXPR:
6240 0 : return fold_convert_loc (loc, type, arg2);
6241 1 : case NE_EXPR:
6242 1 : return fold_convert_loc (loc, type, arg1);
6243 6168 : case LE_EXPR:
6244 6168 : case LT_EXPR:
6245 6168 : case UNLE_EXPR:
6246 6168 : case UNLT_EXPR:
6247 : /* In C++ a ?: expression can be an lvalue, so put the
6248 : operand which will be used if they are equal first
6249 : so that we can convert this back to the
6250 : corresponding COND_EXPR. */
6251 6168 : if (!HONOR_NANS (arg1))
6252 : {
6253 6168 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6254 6168 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6255 6168 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6256 6168 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6257 4783 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6258 : comp_op1, comp_op0);
6259 6168 : return fold_convert_loc (loc, type, tem);
6260 : }
6261 : break;
6262 274353 : case GE_EXPR:
6263 274353 : case GT_EXPR:
6264 274353 : case UNGE_EXPR:
6265 274353 : case UNGT_EXPR:
6266 274353 : if (!HONOR_NANS (arg1))
6267 : {
6268 274351 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6269 274351 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6270 274351 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6271 274351 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6272 3711 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6273 : comp_op1, comp_op0);
6274 274351 : return fold_convert_loc (loc, type, tem);
6275 : }
6276 : break;
6277 0 : case UNEQ_EXPR:
6278 0 : if (!HONOR_NANS (arg1))
6279 0 : return fold_convert_loc (loc, type, arg2);
6280 : break;
6281 0 : case LTGT_EXPR:
6282 0 : if (!HONOR_NANS (arg1))
6283 0 : return fold_convert_loc (loc, type, arg1);
6284 : break;
6285 0 : default:
6286 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6287 : break;
6288 : }
6289 : }
6290 :
6291 : return NULL_TREE;
6292 : }
6293 :
6294 :
6295 :
6296 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6297 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6298 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6299 : false) >= 2)
6300 : #endif
6301 :
6302 : /* EXP is some logical combination of boolean tests. See if we can
6303 : merge it into some range test. Return the new tree if so. */
6304 :
6305 : static tree
6306 25235388 : fold_range_test (location_t loc, enum tree_code code, tree type,
6307 : tree op0, tree op1)
6308 : {
6309 25235388 : int or_op = (code == TRUTH_ORIF_EXPR
6310 25235388 : || code == TRUTH_OR_EXPR);
6311 25235388 : int in0_p, in1_p, in_p;
6312 25235388 : tree low0, low1, low, high0, high1, high;
6313 25235388 : tree tem, lhs, rhs;
6314 :
6315 25235388 : if (!INTEGRAL_TYPE_P (type))
6316 : return 0;
6317 :
6318 25235388 : lhs = make_range (op0, &in0_p, &low0, &high0);
6319 : /* If op0 is known true or false and this is a short-circuiting
6320 : operation we must not merge with op1 since that makes side-effects
6321 : unconditional. So special-case this. */
6322 25235388 : if (!lhs
6323 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6324 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6325 : return op0;
6326 25235386 : rhs = make_range (op1, &in1_p, &low1, &high1);
6327 :
6328 : /* If this is an OR operation, invert both sides; we will invert
6329 : again at the end. */
6330 25235386 : if (or_op)
6331 11874979 : in0_p = ! in0_p, in1_p = ! in1_p;
6332 :
6333 : /* If both expressions are the same, if we can merge the ranges, and we
6334 : can build the range test, return it or it inverted. If one of the
6335 : ranges is always true or always false, consider it to be the same
6336 : expression as the other. */
6337 25203776 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6338 1192819 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6339 : in1_p, low1, high1)
6340 26227298 : && (tem = (build_range_check (loc, type,
6341 : lhs != 0 ? lhs
6342 0 : : rhs != 0 ? rhs : integer_zero_node,
6343 : in_p, low, high))) != 0)
6344 : {
6345 991882 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6346 : }
6347 :
6348 : /* On machines where the branch cost is expensive, if this is a
6349 : short-circuited branch and the underlying object on both sides
6350 : is the same, make a non-short-circuit operation. */
6351 24243504 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6352 24243504 : if (param_logical_op_non_short_circuit != -1)
6353 7827 : logical_op_non_short_circuit
6354 7827 : = param_logical_op_non_short_circuit;
6355 24243504 : if (logical_op_non_short_circuit
6356 24239563 : && !sanitize_coverage_p ()
6357 24239560 : && lhs != 0 && rhs != 0
6358 24239121 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6359 29774942 : && operand_equal_p (lhs, rhs, 0))
6360 : {
6361 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6362 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6363 : which cases we can't do this. */
6364 171128 : if (simple_operand_p (lhs))
6365 69730 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6366 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6367 35388 : type, op0, op1);
6368 :
6369 135740 : else if (!lang_hooks.decls.global_bindings_p ()
6370 135740 : && !CONTAINS_PLACEHOLDER_P (lhs))
6371 : {
6372 135063 : tree common = save_expr (lhs);
6373 :
6374 248469 : if ((lhs = build_range_check (loc, type, common,
6375 113406 : or_op ? ! in0_p : in0_p,
6376 : low0, high0)) != 0
6377 248469 : && (rhs = build_range_check (loc, type, common,
6378 113406 : or_op ? ! in1_p : in1_p,
6379 : low1, high1)) != 0)
6380 : {
6381 248469 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6382 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6383 135063 : type, lhs, rhs);
6384 : }
6385 : }
6386 : }
6387 :
6388 : return 0;
6389 : }
6390 :
6391 : /* For an expression that has the form
6392 : (A && B) || ~B
6393 : or
6394 : (A || B) && ~B,
6395 : we can drop one of the inner expressions and simplify to
6396 : A || ~B
6397 : or
6398 : A && ~B
6399 : LOC is the location of the resulting expression. OP is the inner
6400 : logical operation; the left-hand side in the examples above, while CMPOP
6401 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6402 : removing a condition that guards another, as in
6403 : (A != NULL && A->...) || A == NULL
6404 : which we must not transform. If RHS_ONLY is true, only eliminate the
6405 : right-most operand of the inner logical operation. */
6406 :
6407 : static tree
6408 130146 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6409 : bool rhs_only)
6410 : {
6411 130146 : enum tree_code code = TREE_CODE (cmpop);
6412 130146 : enum tree_code truthop_code = TREE_CODE (op);
6413 130146 : tree lhs = TREE_OPERAND (op, 0);
6414 130146 : tree rhs = TREE_OPERAND (op, 1);
6415 130146 : tree orig_lhs = lhs, orig_rhs = rhs;
6416 130146 : enum tree_code rhs_code = TREE_CODE (rhs);
6417 130146 : enum tree_code lhs_code = TREE_CODE (lhs);
6418 130146 : enum tree_code inv_code;
6419 :
6420 130146 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6421 : return NULL_TREE;
6422 :
6423 116516 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6424 : return NULL_TREE;
6425 :
6426 38951 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6427 :
6428 38951 : if (rhs_code == truthop_code)
6429 : {
6430 37 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6431 37 : if (newrhs != NULL_TREE)
6432 : {
6433 0 : rhs = newrhs;
6434 0 : rhs_code = TREE_CODE (rhs);
6435 : }
6436 : }
6437 38951 : if (lhs_code == truthop_code && !rhs_only)
6438 : {
6439 480 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6440 480 : if (newlhs != NULL_TREE)
6441 : {
6442 0 : lhs = newlhs;
6443 0 : lhs_code = TREE_CODE (lhs);
6444 : }
6445 : }
6446 :
6447 38951 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6448 38951 : if (inv_code == rhs_code
6449 932 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6450 38987 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6451 : return lhs;
6452 38938 : if (!rhs_only && inv_code == lhs_code
6453 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6454 39030 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6455 : return rhs;
6456 38847 : if (rhs != orig_rhs || lhs != orig_lhs)
6457 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6458 0 : lhs, rhs);
6459 : return NULL_TREE;
6460 : }
6461 :
6462 : /* Find ways of folding logical expressions of LHS and RHS:
6463 : Try to merge two comparisons to the same innermost item.
6464 : Look for range tests like "ch >= '0' && ch <= '9'".
6465 : Look for combinations of simple terms on machines with expensive branches
6466 : and evaluate the RHS unconditionally.
6467 :
6468 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6469 : function and the one above.
6470 :
6471 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6472 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6473 :
6474 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6475 : two operands.
6476 :
6477 : We return the simplified tree or 0 if no optimization is possible. */
6478 :
6479 : static tree
6480 24916445 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6481 : tree lhs, tree rhs)
6482 : {
6483 : /* If this is the "or" of two comparisons, we can do something if
6484 : the comparisons are NE_EXPR. If this is the "and", we can do something
6485 : if the comparisons are EQ_EXPR. I.e.,
6486 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6487 :
6488 : WANTED_CODE is this operation code. For single bit fields, we can
6489 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6490 : comparison for one-bit fields. */
6491 :
6492 24916445 : enum tree_code lcode, rcode;
6493 24916445 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6494 24916445 : tree result;
6495 :
6496 : /* Start by getting the comparison codes. Fail if anything is volatile.
6497 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6498 : it were surrounded with a NE_EXPR. */
6499 :
6500 24916445 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6501 : return 0;
6502 :
6503 22026757 : lcode = TREE_CODE (lhs);
6504 22026757 : rcode = TREE_CODE (rhs);
6505 :
6506 22026757 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6507 : {
6508 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6509 0 : build_int_cst (TREE_TYPE (lhs), 0));
6510 0 : lcode = NE_EXPR;
6511 : }
6512 :
6513 22026757 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6514 : {
6515 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6516 0 : build_int_cst (TREE_TYPE (rhs), 0));
6517 0 : rcode = NE_EXPR;
6518 : }
6519 :
6520 22026757 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6521 19657759 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6522 : return 0;
6523 :
6524 18546049 : ll_arg = TREE_OPERAND (lhs, 0);
6525 18546049 : lr_arg = TREE_OPERAND (lhs, 1);
6526 18546049 : rl_arg = TREE_OPERAND (rhs, 0);
6527 18546049 : rr_arg = TREE_OPERAND (rhs, 1);
6528 :
6529 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6530 18546049 : if (simple_operand_p (ll_arg)
6531 18546049 : && simple_operand_p (lr_arg))
6532 : {
6533 15014578 : if (operand_equal_p (ll_arg, rl_arg, 0)
6534 15014578 : && operand_equal_p (lr_arg, rr_arg, 0))
6535 : {
6536 21447 : result = combine_comparisons (loc, code, lcode, rcode,
6537 21447 : truth_type, TREE_TYPE (lhs),
6538 : ll_arg, lr_arg);
6539 21447 : if (result)
6540 : return result;
6541 : }
6542 14993131 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6543 14993131 : && operand_equal_p (lr_arg, rl_arg, 0))
6544 : {
6545 241 : result = combine_comparisons (loc, code, lcode,
6546 : swap_tree_comparison (rcode),
6547 241 : truth_type, TREE_TYPE (lhs),
6548 : ll_arg, lr_arg);
6549 241 : if (result)
6550 : return result;
6551 : }
6552 : }
6553 :
6554 8763160 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6555 18524881 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6556 :
6557 : /* If the RHS can be evaluated unconditionally and its operands are
6558 : simple, it wins to evaluate the RHS unconditionally on machines
6559 : with expensive branches. In this case, this isn't a comparison
6560 : that can be merged. */
6561 :
6562 18524881 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6563 : false) >= 2
6564 18524782 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6565 17483330 : && simple_operand_p (rl_arg)
6566 28549856 : && simple_operand_p (rr_arg))
6567 : {
6568 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6569 11254714 : if (code == TRUTH_OR_EXPR
6570 1545206 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6571 625512 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6572 20292 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6573 11272390 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6574 34732 : return build2_loc (loc, NE_EXPR, truth_type,
6575 17366 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6576 : ll_arg, rl_arg),
6577 17366 : build_int_cst (TREE_TYPE (ll_arg), 0));
6578 :
6579 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6580 11237348 : if (code == TRUTH_AND_EXPR
6581 1738031 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6582 823131 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6583 8304 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6584 11239180 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6585 3220 : return build2_loc (loc, EQ_EXPR, truth_type,
6586 1610 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6587 : ll_arg, rl_arg),
6588 1610 : build_int_cst (TREE_TYPE (ll_arg), 0));
6589 : }
6590 :
6591 : return 0;
6592 : }
6593 :
6594 : /* T is an integer expression that is being multiplied, divided, or taken a
6595 : modulus (CODE says which and what kind of divide or modulus) by a
6596 : constant C. See if we can eliminate that operation by folding it with
6597 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6598 : should be used for the computation if wider than our type.
6599 :
6600 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6601 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6602 : expression would not overflow or that overflow is undefined for the type
6603 : in the language in question.
6604 :
6605 : If we return a non-null expression, it is an equivalent form of the
6606 : original computation, but need not be in the original type. */
6607 :
6608 : static tree
6609 100595428 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6610 : {
6611 : /* To avoid exponential search depth, refuse to allow recursion past
6612 : three levels. Beyond that (1) it's highly unlikely that we'll find
6613 : something interesting and (2) we've probably processed it before
6614 : when we built the inner expression. */
6615 :
6616 100595428 : static int depth;
6617 100595428 : tree ret;
6618 :
6619 100595428 : if (depth > 3)
6620 : return NULL;
6621 :
6622 96921852 : depth++;
6623 96921852 : ret = extract_muldiv_1 (t, c, code, wide_type);
6624 96921852 : depth--;
6625 :
6626 96921852 : return ret;
6627 : }
6628 :
6629 : static tree
6630 96921852 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6631 : {
6632 96921852 : tree type = TREE_TYPE (t);
6633 96921852 : enum tree_code tcode = TREE_CODE (t);
6634 96921852 : tree ctype = type;
6635 96921852 : if (wide_type)
6636 : {
6637 32357759 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6638 : {
6639 177 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6640 96921852 : ctype = wide_type;
6641 : }
6642 32357582 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6643 64715164 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6644 96921852 : ctype = wide_type;
6645 : }
6646 96921852 : tree t1, t2;
6647 96921852 : bool same_p = tcode == code;
6648 96921852 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6649 :
6650 : /* Don't deal with constants of zero here; they confuse the code below. */
6651 96921852 : if (integer_zerop (c))
6652 : return NULL_TREE;
6653 :
6654 96906245 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6655 38519764 : op0 = TREE_OPERAND (t, 0);
6656 :
6657 96906245 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6658 12153596 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6659 :
6660 : /* Note that we need not handle conditional operations here since fold
6661 : already handles those cases. So just do arithmetic here. */
6662 96906245 : switch (tcode)
6663 : {
6664 4355400 : case INTEGER_CST:
6665 : /* For a constant, we can always simplify if we are a multiply
6666 : or (for divide and modulus) if it is a multiple of our constant. */
6667 4355400 : if (code == MULT_EXPR
6668 5582368 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6669 1226968 : TYPE_SIGN (type)))
6670 : {
6671 3555225 : tree tem = const_binop (code, fold_convert (ctype, t),
6672 : fold_convert (ctype, c));
6673 : /* If the multiplication overflowed, we lost information on it.
6674 : See PR68142 and PR69845. */
6675 3555225 : if (TREE_OVERFLOW (tem))
6676 : return NULL_TREE;
6677 3551702 : return tem;
6678 : }
6679 : break;
6680 :
6681 37930293 : CASE_CONVERT: case NON_LVALUE_EXPR:
6682 37930293 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6683 : break;
6684 : /* If op0 is an expression ... */
6685 36653299 : if ((COMPARISON_CLASS_P (op0)
6686 : || UNARY_CLASS_P (op0)
6687 36653299 : || BINARY_CLASS_P (op0)
6688 33630912 : || VL_EXP_CLASS_P (op0)
6689 33569714 : || EXPRESSION_CLASS_P (op0))
6690 : /* ... and has wrapping overflow, and its type is smaller
6691 : than ctype, then we cannot pass through as widening. */
6692 36805675 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6693 1228851 : && (TYPE_PRECISION (ctype)
6694 1228851 : > TYPE_PRECISION (TREE_TYPE (op0))))
6695 : /* ... or this is a truncation (t is narrower than op0),
6696 : then we cannot pass through this narrowing. */
6697 2621558 : || (TYPE_PRECISION (type)
6698 2621558 : < TYPE_PRECISION (TREE_TYPE (op0)))
6699 : /* ... or signedness changes for division or modulus,
6700 : then we cannot pass through this conversion. */
6701 2592015 : || (code != MULT_EXPR
6702 126915 : && (TYPE_UNSIGNED (ctype)
6703 126915 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6704 : /* ... or has undefined overflow while the converted to
6705 : type has not, we cannot do the operation in the inner type
6706 : as that would introduce undefined overflow. */
6707 2491688 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6708 1838231 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6709 : break;
6710 :
6711 : /* Pass the constant down and see if we can make a simplification. If
6712 : we can, replace this expression with the inner simplification for
6713 : possible later conversion to our or some other type. */
6714 34198383 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6715 34198383 : && TREE_CODE (t2) == INTEGER_CST
6716 34198383 : && !TREE_OVERFLOW (t2)
6717 69633389 : && (t1 = extract_muldiv (op0, t2, code,
6718 : code == MULT_EXPR ? ctype : NULL_TREE))
6719 : != 0)
6720 : return t1;
6721 : break;
6722 :
6723 189 : case ABS_EXPR:
6724 : /* If widening the type changes it from signed to unsigned, then we
6725 : must avoid building ABS_EXPR itself as unsigned. */
6726 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6727 : {
6728 0 : tree cstype = (*signed_type_for) (ctype);
6729 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6730 : {
6731 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6732 0 : return fold_convert (ctype, t1);
6733 : }
6734 : break;
6735 : }
6736 : /* If the constant is negative, we cannot simplify this. */
6737 189 : if (tree_int_cst_sgn (c) == -1)
6738 : break;
6739 : /* FALLTHROUGH */
6740 46923 : case NEGATE_EXPR:
6741 : /* For division and modulus, type can't be unsigned, as e.g.
6742 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6743 : For signed types, even with wrapping overflow, this is fine. */
6744 46923 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6745 : break;
6746 45177 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6747 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6748 : break;
6749 :
6750 814 : case MIN_EXPR: case MAX_EXPR:
6751 : /* If widening the type changes the signedness, then we can't perform
6752 : this optimization as that changes the result. */
6753 814 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6754 : break;
6755 :
6756 : /* Punt for multiplication altogether.
6757 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6758 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6759 : 0U, the latter is 2U.
6760 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6761 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6762 : well defined 0, the latter invokes UB.
6763 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6764 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6765 : well defined 25, the latter invokes UB. */
6766 814 : if (code == MULT_EXPR)
6767 : break;
6768 : /* For division/modulo, punt on c being -1 for MAX, as
6769 : MAX (INT_MIN, 0) / -1 is not equivalent to
6770 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6771 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6772 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6773 : transformation won't make it worse. */
6774 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6775 : break;
6776 :
6777 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6778 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6779 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6780 : {
6781 0 : if (tree_int_cst_sgn (c) < 0)
6782 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6783 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6784 : fold_convert (ctype, t2));
6785 : }
6786 : break;
6787 :
6788 1265 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6789 : /* If the second operand is constant, this is a multiplication
6790 : or floor division, by a power of two, so we can treat it that
6791 : way unless the multiplier or divisor overflows. Signed
6792 : left-shift overflow is implementation-defined rather than
6793 : undefined in C90, so do not convert signed left shift into
6794 : multiplication. */
6795 1265 : if (TREE_CODE (op1) == INTEGER_CST
6796 1241 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6797 : /* const_binop may not detect overflow correctly,
6798 : so check for it explicitly here. */
6799 1123 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6800 1274 : wi::to_wide (op1))
6801 1114 : && (t1 = fold_convert (ctype,
6802 : const_binop (LSHIFT_EXPR, size_one_node,
6803 : op1))) != 0
6804 2379 : && !TREE_OVERFLOW (t1))
6805 2026 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6806 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6807 : ctype,
6808 : fold_convert (ctype, op0),
6809 : t1),
6810 1114 : c, code, wide_type);
6811 : break;
6812 :
6813 8413441 : case PLUS_EXPR: case MINUS_EXPR:
6814 : /* See if we can eliminate the operation on both sides. If we can, we
6815 : can return a new PLUS or MINUS. If we can't, the only remaining
6816 : cases where we can do anything are if the second operand is a
6817 : constant. */
6818 8413441 : t1 = extract_muldiv (op0, c, code, wide_type);
6819 8413441 : t2 = extract_muldiv (op1, c, code, wide_type);
6820 812333 : if (t1 != 0 && t2 != 0
6821 280759 : && TYPE_OVERFLOW_WRAPS (ctype)
6822 8684280 : && (code == MULT_EXPR
6823 : /* If not multiplication, we can only do this if both operands
6824 : are divisible by c. */
6825 0 : || (multiple_of_p (ctype, op0, c)
6826 0 : && multiple_of_p (ctype, op1, c))))
6827 : {
6828 270839 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6829 : fold_convert (ctype, t2));
6830 : }
6831 :
6832 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6833 : This simplifies the logic below. */
6834 8142602 : if (tcode == MINUS_EXPR)
6835 : {
6836 2188488 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6837 : /* If OP1 was not easily negatable, the constant may be OP0. */
6838 2188488 : if (TREE_CODE (op0) == INTEGER_CST)
6839 : {
6840 367948 : std::swap (op0, op1);
6841 367948 : std::swap (t1, t2);
6842 : }
6843 : }
6844 :
6845 8142602 : if (TREE_CODE (op1) != INTEGER_CST)
6846 : break;
6847 :
6848 : /* If either OP1 or C are negative, this optimization is not safe for
6849 : some of the division and remainder types while for others we need
6850 : to change the code. */
6851 3738706 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6852 : {
6853 177006 : if (code == CEIL_DIV_EXPR)
6854 : code = FLOOR_DIV_EXPR;
6855 177004 : else if (code == FLOOR_DIV_EXPR)
6856 : code = CEIL_DIV_EXPR;
6857 176567 : else if (code != MULT_EXPR
6858 176567 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6859 : break;
6860 : }
6861 :
6862 : /* If it's a multiply or a division/modulus operation of a multiple
6863 : of our constant, do the operation and verify it doesn't overflow. */
6864 3733175 : if (code == MULT_EXPR
6865 4955372 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6866 1222197 : TYPE_SIGN (type)))
6867 : {
6868 2933767 : op1 = const_binop (code, fold_convert (ctype, op1),
6869 : fold_convert (ctype, c));
6870 : /* We allow the constant to overflow with wrapping semantics. */
6871 2933767 : if (op1 == 0
6872 2933767 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6873 : break;
6874 : }
6875 : else
6876 : break;
6877 :
6878 : /* If we have an unsigned type, we cannot widen the operation since it
6879 : will change the result if the original computation overflowed. */
6880 2930246 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6881 : break;
6882 :
6883 : /* The last case is if we are a multiply. In that case, we can
6884 : apply the distributive law to commute the multiply and addition
6885 : if the multiplication of the constants doesn't overflow
6886 : and overflow is defined. With undefined overflow
6887 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6888 : But fold_plusminus_mult_expr would factor back any power-of-two
6889 : value so do not distribute in the first place in this case. */
6890 2930246 : if (code == MULT_EXPR
6891 2508216 : && TYPE_OVERFLOW_WRAPS (ctype)
6892 5099982 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6893 608180 : return fold_build2 (tcode, ctype,
6894 : fold_build2 (code, ctype,
6895 : fold_convert (ctype, op0),
6896 : fold_convert (ctype, c)),
6897 : op1);
6898 :
6899 : break;
6900 :
6901 2358142 : case MULT_EXPR:
6902 : /* We have a special case here if we are doing something like
6903 : (C * 8) % 4 since we know that's zero. */
6904 2358142 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6905 2358142 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6906 : /* If the multiplication can overflow we cannot optimize this. */
6907 10856 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6908 374 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6909 2368998 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6910 329 : TYPE_SIGN (type)))
6911 : {
6912 8 : return omit_one_operand (type, integer_zero_node, op0);
6913 : }
6914 :
6915 : /* ... fall through ... */
6916 :
6917 2640052 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6918 2640052 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6919 : /* If we can extract our operation from the LHS, do so and return a
6920 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6921 : do something only if the second operand is a constant. */
6922 2640052 : if (same_p
6923 2224460 : && TYPE_OVERFLOW_WRAPS (ctype)
6924 4689732 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6925 66869 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6926 : fold_convert (ctype, op1));
6927 2573183 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6928 2148202 : && TYPE_OVERFLOW_WRAPS (ctype)
6929 4546653 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6930 930444 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6931 : fold_convert (ctype, t1));
6932 1642739 : else if (TREE_CODE (op1) != INTEGER_CST)
6933 : return 0;
6934 :
6935 : /* If these are the same operation types, we can associate them
6936 : assuming no overflow. */
6937 617519 : if (tcode == code)
6938 : {
6939 202502 : bool overflow_p = false;
6940 202502 : wi::overflow_type overflow_mul;
6941 202502 : signop sign = TYPE_SIGN (ctype);
6942 202502 : unsigned prec = TYPE_PRECISION (ctype);
6943 405004 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6944 202502 : wi::to_wide (c, prec),
6945 202502 : sign, &overflow_mul);
6946 202502 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6947 202502 : if (overflow_mul
6948 1549 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6949 : overflow_p = true;
6950 202435 : if (!overflow_p)
6951 202435 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6952 : wide_int_to_tree (ctype, mul));
6953 202502 : }
6954 :
6955 : /* If these operations "cancel" each other, we have the main
6956 : optimizations of this pass, which occur when either constant is a
6957 : multiple of the other, in which case we replace this with either an
6958 : operation or CODE or TCODE.
6959 :
6960 : If we have an unsigned type, we cannot do this since it will change
6961 : the result if the original computation overflowed. */
6962 415084 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6963 100575 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6964 515616 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6965 100227 : || (tcode == MULT_EXPR
6966 100227 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6967 857 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6968 853 : && code != MULT_EXPR)))
6969 : {
6970 1152 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6971 1152 : TYPE_SIGN (type)))
6972 : {
6973 124 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6974 : fold_convert (ctype,
6975 : const_binop (TRUNC_DIV_EXPR,
6976 : op1, c)));
6977 : }
6978 1028 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6979 1028 : TYPE_SIGN (type)))
6980 : {
6981 305 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6982 : fold_convert (ctype,
6983 : const_binop (TRUNC_DIV_EXPR,
6984 : c, op1)));
6985 : }
6986 : }
6987 : break;
6988 :
6989 : default:
6990 : break;
6991 : }
6992 :
6993 : return 0;
6994 : }
6995 :
6996 : /* Return a node which has the indicated constant VALUE (either 0 or
6997 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6998 : and is of the indicated TYPE. */
6999 :
7000 : tree
7001 120264655 : constant_boolean_node (bool value, tree type)
7002 : {
7003 120264655 : if (type == integer_type_node)
7004 20496649 : return value ? integer_one_node : integer_zero_node;
7005 99768006 : else if (type == boolean_type_node)
7006 95344732 : return value ? boolean_true_node : boolean_false_node;
7007 4423274 : else if (VECTOR_TYPE_P (type))
7008 1951 : return build_vector_from_val (type,
7009 1951 : build_int_cst (TREE_TYPE (type),
7010 2692 : value ? -1 : 0));
7011 : else
7012 4421323 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
7013 : }
7014 :
7015 :
7016 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7017 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7018 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7019 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7020 : COND is the first argument to CODE; otherwise (as in the example
7021 : given here), it is the second argument. TYPE is the type of the
7022 : original expression. Return NULL_TREE if no simplification is
7023 : possible. */
7024 :
7025 : static tree
7026 963182 : fold_binary_op_with_conditional_arg (location_t loc,
7027 : enum tree_code code,
7028 : tree type, tree op0, tree op1,
7029 : tree cond, tree arg, int cond_first_p)
7030 : {
7031 963182 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7032 963182 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7033 963182 : tree test, true_value, false_value;
7034 963182 : tree lhs = NULL_TREE;
7035 963182 : tree rhs = NULL_TREE;
7036 963182 : enum tree_code cond_code = COND_EXPR;
7037 :
7038 : /* Do not move possibly trapping operations into the conditional as this
7039 : pessimizes code and causes gimplification issues when applied late. */
7040 982915 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7041 963182 : ANY_INTEGRAL_TYPE_P (type)
7042 771763 : && TYPE_OVERFLOW_TRAPS (type), op1))
7043 : return NULL_TREE;
7044 :
7045 943273 : if (TREE_CODE (cond) == COND_EXPR
7046 360041 : || TREE_CODE (cond) == VEC_COND_EXPR)
7047 : {
7048 587025 : test = TREE_OPERAND (cond, 0);
7049 587025 : true_value = TREE_OPERAND (cond, 1);
7050 587025 : false_value = TREE_OPERAND (cond, 2);
7051 : /* If this operand throws an expression, then it does not make
7052 : sense to try to perform a logical or arithmetic operation
7053 : involving it. */
7054 587025 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7055 7463 : lhs = true_value;
7056 587025 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7057 6 : rhs = false_value;
7058 : }
7059 356248 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7060 356134 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7061 : {
7062 354190 : tree testtype = TREE_TYPE (cond);
7063 354190 : test = cond;
7064 354190 : true_value = constant_boolean_node (true, testtype);
7065 354190 : false_value = constant_boolean_node (false, testtype);
7066 : }
7067 : else
7068 : /* Detect the case of mixing vector and scalar types - bail out. */
7069 : return NULL_TREE;
7070 :
7071 941215 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7072 3907 : cond_code = VEC_COND_EXPR;
7073 :
7074 : /* This transformation is only worthwhile if we don't have to wrap ARG
7075 : in a SAVE_EXPR and the operation can be simplified without recursing
7076 : on at least one of the branches once its pushed inside the COND_EXPR. */
7077 941215 : if (!TREE_CONSTANT (arg)
7078 941215 : && (TREE_SIDE_EFFECTS (arg)
7079 448497 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7080 443581 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7081 : return NULL_TREE;
7082 :
7083 509211 : arg = fold_convert_loc (loc, arg_type, arg);
7084 509211 : if (lhs == 0)
7085 : {
7086 503180 : true_value = fold_convert_loc (loc, cond_type, true_value);
7087 503180 : if (cond_first_p)
7088 497554 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7089 : else
7090 5626 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7091 : }
7092 509211 : if (rhs == 0)
7093 : {
7094 509205 : false_value = fold_convert_loc (loc, cond_type, false_value);
7095 509205 : if (cond_first_p)
7096 503018 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7097 : else
7098 6187 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7099 : }
7100 :
7101 : /* Check that we have simplified at least one of the branches. */
7102 509211 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7103 : return NULL_TREE;
7104 :
7105 488421 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7106 : }
7107 :
7108 :
7109 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7110 :
7111 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7112 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7113 : if ARG - ZERO_ARG is the same as X.
7114 :
7115 : If ARG is NULL, check for any value of type TYPE.
7116 :
7117 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7118 : and finite. The problematic cases are when X is zero, and its mode
7119 : has signed zeros. In the case of rounding towards -infinity,
7120 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7121 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7122 :
7123 : bool
7124 639502 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7125 : const_tree zero_arg, int negate)
7126 : {
7127 639502 : if (!real_zerop (zero_arg))
7128 : return false;
7129 :
7130 : /* Don't allow the fold with -fsignaling-nans. */
7131 638710 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7132 : return false;
7133 :
7134 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7135 635547 : if (!HONOR_SIGNED_ZEROS (type))
7136 : return true;
7137 :
7138 : /* There is no case that is safe for all rounding modes. */
7139 618333 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7140 : return false;
7141 :
7142 : /* In a vector or complex, we would need to check the sign of all zeros. */
7143 616970 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7144 2853 : zero_arg = uniform_vector_p (zero_arg);
7145 616970 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7146 : return false;
7147 :
7148 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7149 615666 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7150 220 : negate = !negate;
7151 :
7152 : /* The mode has signed zeros, and we have to honor their sign.
7153 : In this situation, there are only two cases we can return true for.
7154 : (i) X - 0 is the same as X with default rounding.
7155 : (ii) X + 0 is X when X can't possibly be -0.0. */
7156 615666 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7157 : }
7158 :
7159 : /* Subroutine of match.pd that determines if it is safe to optimize
7160 : a floating point comparison of an integer value, known to be between
7161 : LO and HI, using comparison operator CMP, against the real constant
7162 : R in floating point type FMT, as the same integer comparison against
7163 : the integer constant I, with sign ISIGN.
7164 :
7165 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7166 : because the floating point representations of the neighboring integers
7167 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7168 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7169 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7170 : due to truncation/rounding.
7171 : */
7172 : bool
7173 21274 : fold_cmp_float_cst_p (const wide_int &lo, const wide_int &hi,
7174 : enum tree_code cmp,
7175 : const REAL_VALUE_TYPE *r, format_helper fmt,
7176 : const wide_int &i, signop isign)
7177 : {
7178 21274 : REAL_VALUE_TYPE raw;
7179 21274 : REAL_VALUE_TYPE rnd;
7180 21274 : bool check_im1 = true;
7181 21274 : bool check_ip1 = true;
7182 :
7183 21274 : switch (cmp)
7184 : {
7185 : case EQ_EXPR:
7186 : case NE_EXPR:
7187 : /* Check both i-1 and i+1. */
7188 : break;
7189 :
7190 19689 : case LT_EXPR:
7191 19689 : case GE_EXPR:
7192 : /* Only check i-1. */
7193 19689 : check_ip1 = false;
7194 19689 : break;
7195 :
7196 356 : case LE_EXPR:
7197 356 : case GT_EXPR:
7198 : /* Only check i+1. */
7199 356 : check_im1 = false;
7200 356 : break;
7201 :
7202 : default:
7203 : return false;
7204 : }
7205 :
7206 21274 : if (flag_rounding_math && i != 0)
7207 : {
7208 344 : real_from_integer (&raw, VOIDmode, i, isign);
7209 344 : if (!real_identical (r, &raw))
7210 : return false;
7211 : }
7212 :
7213 21274 : if (check_im1 && wi::gt_p (i, lo, isign))
7214 : {
7215 1300 : if (flag_rounding_math)
7216 : {
7217 344 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7218 344 : real_convert (&rnd, fmt, &raw);
7219 344 : if (!real_identical (&raw, &rnd))
7220 : return false;
7221 : }
7222 : else
7223 956 : real_from_integer (&rnd, fmt, i - 1, isign);
7224 956 : if (real_identical (r, &rnd))
7225 : return false;
7226 : }
7227 :
7228 20586 : if (check_ip1 && wi::lt_p (i, hi, isign))
7229 : {
7230 875 : if (flag_rounding_math)
7231 : {
7232 0 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7233 0 : real_convert (&rnd, fmt, &raw);
7234 0 : if (!real_identical (&raw, &rnd))
7235 : return false;
7236 : }
7237 : else
7238 875 : real_from_integer (&rnd, fmt, i + 1, isign);
7239 875 : if (real_identical (r, &rnd))
7240 0 : return false;
7241 : }
7242 :
7243 : return true;
7244 : }
7245 :
7246 : /* Subroutine of match.pd that optimizes comparisons of a division by
7247 : a nonzero integer constant against an integer constant, i.e.
7248 : X/C1 op C2.
7249 :
7250 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7251 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7252 :
7253 : enum tree_code
7254 1753930 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7255 : tree *hi, bool *neg_overflow)
7256 : {
7257 1753930 : tree prod, tmp, type = TREE_TYPE (c1);
7258 1753930 : signop sign = TYPE_SIGN (type);
7259 1753930 : wi::overflow_type overflow;
7260 :
7261 : /* We have to do this the hard way to detect unsigned overflow.
7262 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7263 1753930 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7264 1753930 : prod = force_fit_type (type, val, -1, overflow);
7265 1753930 : *neg_overflow = false;
7266 :
7267 1753930 : if (sign == UNSIGNED)
7268 : {
7269 1724491 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7270 1724491 : *lo = prod;
7271 :
7272 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7273 1724491 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7274 1724491 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7275 : }
7276 29439 : else if (tree_int_cst_sgn (c1) >= 0)
7277 : {
7278 28040 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7279 28040 : switch (tree_int_cst_sgn (c2))
7280 : {
7281 4914 : case -1:
7282 4914 : *neg_overflow = true;
7283 4914 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7284 4914 : *hi = prod;
7285 4914 : break;
7286 :
7287 14699 : case 0:
7288 14699 : *lo = fold_negate_const (tmp, type);
7289 14699 : *hi = tmp;
7290 14699 : break;
7291 :
7292 8427 : case 1:
7293 8427 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7294 8427 : *lo = prod;
7295 8427 : break;
7296 :
7297 0 : default:
7298 0 : gcc_unreachable ();
7299 : }
7300 : }
7301 : else
7302 : {
7303 : /* A negative divisor reverses the relational operators. */
7304 1399 : code = swap_tree_comparison (code);
7305 :
7306 1399 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7307 1399 : switch (tree_int_cst_sgn (c2))
7308 : {
7309 134 : case -1:
7310 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7311 134 : *lo = prod;
7312 134 : break;
7313 :
7314 167 : case 0:
7315 167 : *hi = fold_negate_const (tmp, type);
7316 167 : *lo = tmp;
7317 167 : break;
7318 :
7319 1098 : case 1:
7320 1098 : *neg_overflow = true;
7321 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7322 1098 : *hi = prod;
7323 1098 : break;
7324 :
7325 0 : default:
7326 0 : gcc_unreachable ();
7327 : }
7328 : }
7329 :
7330 1753930 : if (code != EQ_EXPR && code != NE_EXPR)
7331 : return code;
7332 :
7333 16958 : if (TREE_OVERFLOW (*lo)
7334 16958 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7335 745 : *lo = NULL_TREE;
7336 16958 : if (TREE_OVERFLOW (*hi)
7337 16958 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7338 95 : *hi = NULL_TREE;
7339 :
7340 : return code;
7341 1753930 : }
7342 :
7343 : /* Test whether it is preferable to swap two operands, ARG0 and
7344 : ARG1, for example because ARG0 is an integer constant and ARG1
7345 : isn't. */
7346 :
7347 : bool
7348 1634407647 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7349 : {
7350 1634407647 : if (CONSTANT_CLASS_P (arg1))
7351 : return false;
7352 533063616 : if (CONSTANT_CLASS_P (arg0))
7353 : return true;
7354 :
7355 491475040 : STRIP_NOPS (arg0);
7356 491475040 : STRIP_NOPS (arg1);
7357 :
7358 491475040 : if (TREE_CONSTANT (arg1))
7359 : return false;
7360 473548130 : if (TREE_CONSTANT (arg0))
7361 : return true;
7362 :
7363 : /* Put addresses in arg1. */
7364 472774840 : if (TREE_CODE (arg1) == ADDR_EXPR)
7365 : return false;
7366 453840080 : if (TREE_CODE (arg0) == ADDR_EXPR)
7367 : return true;
7368 :
7369 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7370 : for commutative and comparison operators. Ensuring a canonical
7371 : form allows the optimizers to find additional redundancies without
7372 : having to explicitly check for both orderings. */
7373 453440732 : if (TREE_CODE (arg0) == SSA_NAME
7374 342950960 : && TREE_CODE (arg1) == SSA_NAME
7375 790435611 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7376 : return true;
7377 :
7378 : /* Put SSA_NAMEs last. */
7379 430785468 : if (TREE_CODE (arg1) == SSA_NAME)
7380 : return false;
7381 101224455 : if (TREE_CODE (arg0) == SSA_NAME)
7382 : return true;
7383 :
7384 : /* Put variables last. */
7385 95268374 : if (DECL_P (arg1))
7386 : return false;
7387 51610511 : if (DECL_P (arg0))
7388 6013676 : return true;
7389 :
7390 : return false;
7391 : }
7392 :
7393 :
7394 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7395 : means A >= Y && A != MAX, but in this case we know that
7396 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7397 :
7398 : static tree
7399 24156228 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7400 : {
7401 24156228 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7402 :
7403 24156228 : if (TREE_CODE (bound) == LT_EXPR)
7404 5126377 : a = TREE_OPERAND (bound, 0);
7405 19029851 : else if (TREE_CODE (bound) == GT_EXPR)
7406 2782624 : a = TREE_OPERAND (bound, 1);
7407 : else
7408 : return NULL_TREE;
7409 :
7410 7909001 : typea = TREE_TYPE (a);
7411 7909001 : if (!INTEGRAL_TYPE_P (typea)
7412 484194 : && !POINTER_TYPE_P (typea))
7413 : return NULL_TREE;
7414 :
7415 7730618 : if (TREE_CODE (ineq) == LT_EXPR)
7416 : {
7417 1502168 : a1 = TREE_OPERAND (ineq, 1);
7418 1502168 : y = TREE_OPERAND (ineq, 0);
7419 : }
7420 6228450 : else if (TREE_CODE (ineq) == GT_EXPR)
7421 : {
7422 1172414 : a1 = TREE_OPERAND (ineq, 0);
7423 1172414 : y = TREE_OPERAND (ineq, 1);
7424 : }
7425 : else
7426 : return NULL_TREE;
7427 :
7428 2674582 : if (TREE_TYPE (a1) != typea)
7429 : return NULL_TREE;
7430 :
7431 1901199 : if (POINTER_TYPE_P (typea))
7432 : {
7433 : /* Convert the pointer types into integer before taking the difference. */
7434 11630 : tree ta = fold_convert_loc (loc, ssizetype, a);
7435 11630 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7436 11630 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7437 : }
7438 : else
7439 1889569 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7440 :
7441 1901199 : if (!diff || !integer_onep (diff))
7442 : return NULL_TREE;
7443 :
7444 8897 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7445 : }
7446 :
7447 : /* Fold a sum or difference of at least one multiplication.
7448 : Returns the folded tree or NULL if no simplification could be made. */
7449 :
7450 : static tree
7451 9181895 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7452 : tree arg0, tree arg1)
7453 : {
7454 9181895 : tree arg00, arg01, arg10, arg11;
7455 9181895 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7456 :
7457 : /* (A * C) +- (B * C) -> (A+-B) * C.
7458 : (A * C) +- A -> A * (C+-1).
7459 : We are most concerned about the case where C is a constant,
7460 : but other combinations show up during loop reduction. Since
7461 : it is not difficult, try all four possibilities. */
7462 :
7463 9181895 : if (TREE_CODE (arg0) == MULT_EXPR)
7464 : {
7465 8189105 : arg00 = TREE_OPERAND (arg0, 0);
7466 8189105 : arg01 = TREE_OPERAND (arg0, 1);
7467 : }
7468 992790 : else if (TREE_CODE (arg0) == INTEGER_CST)
7469 : {
7470 69170 : arg00 = build_one_cst (type);
7471 69170 : arg01 = arg0;
7472 : }
7473 : else
7474 : {
7475 : /* We cannot generate constant 1 for fract. */
7476 923620 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7477 : return NULL_TREE;
7478 923620 : arg00 = arg0;
7479 923620 : arg01 = build_one_cst (type);
7480 : }
7481 9181895 : if (TREE_CODE (arg1) == MULT_EXPR)
7482 : {
7483 2388938 : arg10 = TREE_OPERAND (arg1, 0);
7484 2388938 : arg11 = TREE_OPERAND (arg1, 1);
7485 : }
7486 6792957 : else if (TREE_CODE (arg1) == INTEGER_CST)
7487 : {
7488 3515378 : arg10 = build_one_cst (type);
7489 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7490 : the purpose of this canonicalization. */
7491 6800017 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7492 234027 : && negate_expr_p (arg1)
7493 3746117 : && code == PLUS_EXPR)
7494 : {
7495 230739 : arg11 = negate_expr (arg1);
7496 230739 : code = MINUS_EXPR;
7497 : }
7498 : else
7499 : arg11 = arg1;
7500 : }
7501 : else
7502 : {
7503 : /* We cannot generate constant 1 for fract. */
7504 3277579 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7505 : return NULL_TREE;
7506 3277579 : arg10 = arg1;
7507 3277579 : arg11 = build_one_cst (type);
7508 : }
7509 9181895 : same = NULL_TREE;
7510 :
7511 : /* Prefer factoring a common non-constant. */
7512 9181895 : if (operand_equal_p (arg00, arg10, 0))
7513 : same = arg00, alt0 = arg01, alt1 = arg11;
7514 9178117 : else if (operand_equal_p (arg01, arg11, 0))
7515 : same = arg01, alt0 = arg00, alt1 = arg10;
7516 9072332 : else if (operand_equal_p (arg00, arg11, 0))
7517 : same = arg00, alt0 = arg01, alt1 = arg10;
7518 9072270 : else if (operand_equal_p (arg01, arg10, 0))
7519 : same = arg01, alt0 = arg00, alt1 = arg11;
7520 :
7521 : /* No identical multiplicands; see if we can find a common
7522 : power-of-two factor in non-power-of-two multiplies. This
7523 : can help in multi-dimensional array access. */
7524 9066915 : else if (TREE_CODE (arg01) == INTEGER_CST
7525 7961078 : && TREE_CODE (arg11) == INTEGER_CST)
7526 : {
7527 7734690 : wide_int int01 = wi::to_wide (arg01);
7528 7734690 : wide_int int11 = wi::to_wide (arg11);
7529 7734690 : bool swap = false;
7530 7734690 : tree maybe_same;
7531 :
7532 : /* Move min of absolute values to int11. */
7533 7734692 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7534 : {
7535 3525552 : std::swap (int01, int11);
7536 3525552 : std::swap (arg00, arg10);
7537 3525552 : maybe_same = arg01;
7538 3525552 : swap = true;
7539 : }
7540 : else
7541 : maybe_same = arg11;
7542 :
7543 7734690 : wide_int factor = wi::abs (int11);
7544 7734690 : if (wi::gtu_p (factor, 1u)
7545 4219033 : && wi::exact_log2 (factor) != -1
7546 12041586 : && (int01 & (factor - 1)) == 0
7547 : /* The remainder should not be a constant, otherwise we
7548 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7549 : increased the number of multiplications necessary. */
7550 9191213 : && TREE_CODE (arg10) != INTEGER_CST)
7551 : {
7552 2453310 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7553 1226655 : wide_int_to_tree (TREE_TYPE (arg00),
7554 1226655 : wi::sdiv_trunc (int01,
7555 : int11)));
7556 1226655 : alt1 = arg10;
7557 1226655 : same = maybe_same;
7558 1226655 : if (swap)
7559 1101988 : std::swap (alt0, alt1);
7560 : }
7561 7734692 : }
7562 :
7563 7849670 : if (!same)
7564 : return NULL_TREE;
7565 :
7566 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7567 1341635 : || TYPE_OVERFLOW_WRAPS (type)
7568 : /* We are neither factoring zero nor minus one. */
7569 1460346 : || TREE_CODE (same) == INTEGER_CST)
7570 1330021 : return fold_build2_loc (loc, MULT_EXPR, type,
7571 : fold_build2_loc (loc, code, type,
7572 : fold_convert_loc (loc, type, alt0),
7573 : fold_convert_loc (loc, type, alt1)),
7574 1330021 : fold_convert_loc (loc, type, same));
7575 :
7576 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7577 : same may be minus one and thus the multiplication may overflow. Perform
7578 : the sum operation in an unsigned type. */
7579 11614 : tree utype = unsigned_type_for (type);
7580 11614 : tree tem = fold_build2_loc (loc, code, utype,
7581 : fold_convert_loc (loc, utype, alt0),
7582 : fold_convert_loc (loc, utype, alt1));
7583 : /* If the sum evaluated to a constant that is not -INF the multiplication
7584 : cannot overflow. */
7585 23228 : if (TREE_CODE (tem) == INTEGER_CST
7586 14851 : && (wi::to_wide (tem)
7587 18088 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7588 3224 : return fold_build2_loc (loc, MULT_EXPR, type,
7589 3224 : fold_convert (type, tem), same);
7590 :
7591 : /* Do not resort to unsigned multiplication because
7592 : we lose the no-overflow property of the expression. */
7593 : return NULL_TREE;
7594 : }
7595 :
7596 :
7597 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7598 : into the buffer PTR of length LEN bytes.
7599 : Return the number of bytes placed in the buffer, or zero
7600 : upon failure. */
7601 :
7602 : int
7603 55610734 : native_encode_wide_int (tree type, const wide_int_ref &val,
7604 : unsigned char *ptr, int len, int off)
7605 : {
7606 55610734 : int total_bytes;
7607 55610734 : if (BITINT_TYPE_P (type))
7608 : {
7609 18628 : struct bitint_info info;
7610 18628 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7611 18628 : gcc_assert (ok);
7612 18628 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7613 18628 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7614 : {
7615 17376 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7616 : /* More work is needed when adding _BitInt support to PDP endian
7617 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7618 : match target endianity here. */
7619 17376 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7620 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7621 : || (GET_MODE_SIZE (limb_mode)
7622 : >= UNITS_PER_WORD)));
7623 : }
7624 : else
7625 2504 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7626 : }
7627 : else
7628 111184212 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7629 55610734 : int byte, offset, word, words;
7630 55610734 : unsigned char value;
7631 :
7632 55610734 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7633 : return 0;
7634 55610242 : if (off == -1)
7635 53588531 : off = 0;
7636 :
7637 55610242 : if (ptr == NULL)
7638 : /* Dry run. */
7639 2858276 : return MIN (len, total_bytes - off);
7640 :
7641 : words = total_bytes / UNITS_PER_WORD;
7642 :
7643 257021046 : for (byte = 0; byte < total_bytes; byte++)
7644 : {
7645 204269080 : int bitpos = byte * BITS_PER_UNIT;
7646 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7647 : number of bytes. */
7648 204269080 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7649 :
7650 204269080 : if (total_bytes > UNITS_PER_WORD)
7651 : {
7652 204269080 : word = byte / UNITS_PER_WORD;
7653 204269080 : if (WORDS_BIG_ENDIAN)
7654 : word = (words - 1) - word;
7655 204269080 : offset = word * UNITS_PER_WORD;
7656 204269080 : if (BYTES_BIG_ENDIAN)
7657 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7658 : else
7659 204269080 : offset += byte % UNITS_PER_WORD;
7660 : }
7661 : else
7662 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7663 204269080 : if (offset >= off && offset - off < len)
7664 202862032 : ptr[offset - off] = value;
7665 : }
7666 52751966 : return MIN (len, total_bytes - off);
7667 : }
7668 :
7669 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7670 : specified by EXPR into the buffer PTR of length LEN bytes.
7671 : Return the number of bytes placed in the buffer, or zero
7672 : upon failure. */
7673 :
7674 : static int
7675 55610734 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7676 : {
7677 55610734 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7678 55610734 : ptr, len, off);
7679 : }
7680 :
7681 :
7682 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7683 : specified by EXPR into the buffer PTR of length LEN bytes.
7684 : Return the number of bytes placed in the buffer, or zero
7685 : upon failure. */
7686 :
7687 : static int
7688 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7689 : {
7690 0 : tree type = TREE_TYPE (expr);
7691 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7692 0 : int total_bytes = GET_MODE_SIZE (mode);
7693 0 : FIXED_VALUE_TYPE value;
7694 0 : tree i_value, i_type;
7695 :
7696 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7697 : return 0;
7698 :
7699 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7700 :
7701 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7702 : return 0;
7703 :
7704 0 : value = TREE_FIXED_CST (expr);
7705 0 : i_value = double_int_to_tree (i_type, value.data);
7706 :
7707 0 : return native_encode_int (i_value, ptr, len, off);
7708 : }
7709 :
7710 :
7711 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7712 : specified by EXPR into the buffer PTR of length LEN bytes.
7713 : Return the number of bytes placed in the buffer, or zero
7714 : upon failure. */
7715 :
7716 : int
7717 857316 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7718 : unsigned char *ptr, int len, int off)
7719 : {
7720 857316 : int total_bytes = GET_MODE_SIZE (mode);
7721 857316 : int byte, offset, word, words, bitpos;
7722 857316 : unsigned char value;
7723 :
7724 : /* There are always 32 bits in each long, no matter the size of
7725 : the hosts long. We handle floating point representations with
7726 : up to 192 bits. */
7727 857316 : long tmp[6];
7728 :
7729 857316 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7730 : return 0;
7731 854988 : if (off == -1)
7732 746285 : off = 0;
7733 :
7734 854988 : if (ptr == NULL)
7735 : /* Dry run. */
7736 141244 : return MIN (len, total_bytes - off);
7737 :
7738 713744 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7739 :
7740 713744 : real_to_target (tmp, val, mode);
7741 :
7742 6868338 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7743 6154594 : bitpos += BITS_PER_UNIT)
7744 : {
7745 6154594 : byte = (bitpos / BITS_PER_UNIT) & 3;
7746 6154594 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7747 :
7748 6154594 : if (UNITS_PER_WORD < 4)
7749 : {
7750 : word = byte / UNITS_PER_WORD;
7751 : if (WORDS_BIG_ENDIAN)
7752 : word = (words - 1) - word;
7753 : offset = word * UNITS_PER_WORD;
7754 : if (BYTES_BIG_ENDIAN)
7755 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7756 : else
7757 : offset += byte % UNITS_PER_WORD;
7758 : }
7759 : else
7760 : {
7761 6154594 : offset = byte;
7762 6154594 : if (BYTES_BIG_ENDIAN)
7763 : {
7764 : /* Reverse bytes within each long, or within the entire float
7765 : if it's smaller than a long (for HFmode). */
7766 : offset = MIN (3, total_bytes - 1) - offset;
7767 : gcc_assert (offset >= 0);
7768 : }
7769 : }
7770 6154594 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7771 6154594 : if (offset >= off
7772 6151354 : && offset - off < len)
7773 6129694 : ptr[offset - off] = value;
7774 : }
7775 713744 : return MIN (len, total_bytes - off);
7776 : }
7777 :
7778 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7779 : specified by EXPR into the buffer PTR of length LEN bytes.
7780 : Return the number of bytes placed in the buffer, or zero
7781 : upon failure. */
7782 :
7783 : static int
7784 10378 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7785 : {
7786 10378 : int rsize, isize;
7787 10378 : tree part;
7788 :
7789 10378 : part = TREE_REALPART (expr);
7790 10378 : rsize = native_encode_expr (part, ptr, len, off);
7791 10378 : if (off == -1 && rsize == 0)
7792 : return 0;
7793 10378 : part = TREE_IMAGPART (expr);
7794 10378 : if (off != -1)
7795 20759 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7796 10378 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7797 : len - rsize, off);
7798 10378 : if (off == -1 && isize != rsize)
7799 : return 0;
7800 10378 : return rsize + isize;
7801 : }
7802 :
7803 : /* Like native_encode_vector, but only encode the first COUNT elements.
7804 : The other arguments are as for native_encode_vector. */
7805 :
7806 : static int
7807 1084011 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7808 : int off, unsigned HOST_WIDE_INT count)
7809 : {
7810 1084011 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7811 2168022 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7812 1085053 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7813 : {
7814 : /* This is the only case in which elements can be smaller than a byte.
7815 : Element 0 is always in the lsb of the containing byte. */
7816 964 : unsigned int elt_bits = TYPE_PRECISION (itype);
7817 964 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7818 964 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7819 : return 0;
7820 :
7821 964 : if (off == -1)
7822 964 : off = 0;
7823 :
7824 : /* Zero the buffer and then set bits later where necessary. */
7825 964 : int extract_bytes = MIN (len, total_bytes - off);
7826 964 : if (ptr)
7827 964 : memset (ptr, 0, extract_bytes);
7828 :
7829 964 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7830 964 : unsigned int first_elt = off * elts_per_byte;
7831 964 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7832 964 : unsigned int elt_mask = (1 << elt_bits) - 1;
7833 17645 : for (unsigned int i = 0; i < extract_elts; ++i)
7834 : {
7835 16681 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7836 16681 : if (TREE_CODE (elt) != INTEGER_CST)
7837 : return 0;
7838 :
7839 16681 : if (ptr && integer_nonzerop (elt))
7840 : {
7841 8488 : unsigned int bit = i * elt_bits;
7842 8488 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7843 : }
7844 : }
7845 : return extract_bytes;
7846 : }
7847 :
7848 1083047 : int offset = 0;
7849 1083047 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7850 4351246 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7851 : {
7852 3873199 : if (off >= size)
7853 : {
7854 23846 : off -= size;
7855 23846 : continue;
7856 : }
7857 3849353 : tree elem = VECTOR_CST_ELT (expr, i);
7858 3849353 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7859 : len - offset, off);
7860 3849353 : if ((off == -1 && res != size) || res == 0)
7861 : return 0;
7862 3848824 : offset += res;
7863 3848824 : if (offset >= len)
7864 604471 : return (off == -1 && i < count - 1) ? 0 : offset;
7865 3244353 : if (off != -1)
7866 447147 : off = 0;
7867 : }
7868 : return offset;
7869 : }
7870 :
7871 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7872 : specified by EXPR into the buffer PTR of length LEN bytes.
7873 : Return the number of bytes placed in the buffer, or zero
7874 : upon failure. */
7875 :
7876 : static int
7877 926685 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7878 : {
7879 926685 : unsigned HOST_WIDE_INT count;
7880 926685 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7881 : return 0;
7882 926685 : return native_encode_vector_part (expr, ptr, len, off, count);
7883 : }
7884 :
7885 :
7886 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7887 : specified by EXPR into the buffer PTR of length LEN bytes.
7888 : Return the number of bytes placed in the buffer, or zero
7889 : upon failure. */
7890 :
7891 : static int
7892 150782 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7893 : {
7894 150782 : tree type = TREE_TYPE (expr);
7895 :
7896 : /* Wide-char strings are encoded in target byte-order so native
7897 : encoding them is trivial. */
7898 150782 : if (BITS_PER_UNIT != CHAR_BIT
7899 150782 : || TREE_CODE (type) != ARRAY_TYPE
7900 150782 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7901 301564 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7902 : return 0;
7903 :
7904 150782 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7905 150782 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7906 : return 0;
7907 149916 : if (off == -1)
7908 65790 : off = 0;
7909 149916 : len = MIN (total_bytes - off, len);
7910 149916 : if (ptr == NULL)
7911 : /* Dry run. */;
7912 : else
7913 : {
7914 149916 : int written = 0;
7915 149916 : if (off < TREE_STRING_LENGTH (expr))
7916 : {
7917 149642 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7918 149642 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7919 : }
7920 149916 : memset (ptr + written, 0, len - written);
7921 : }
7922 : return len;
7923 : }
7924 :
7925 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7926 : specified by EXPR into the buffer PTR of length LEN bytes.
7927 : Return the number of bytes placed in the buffer, or zero
7928 : upon failure. */
7929 :
7930 : static int
7931 49732 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7932 : {
7933 : /* We are only concerned with zero-initialization constructors here. That's
7934 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7935 : deal with. For more general handling of constructors, there is
7936 : native_encode_initializer. */
7937 49732 : if (CONSTRUCTOR_NELTS (expr))
7938 : return 0;
7939 :
7940 : /* Wide-char strings are encoded in target byte-order so native
7941 : encoding them is trivial. */
7942 92936 : if (BITS_PER_UNIT != CHAR_BIT
7943 46468 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7944 : return 0;
7945 :
7946 46468 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7947 46468 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7948 : return 0;
7949 46468 : if (off == -1)
7950 0 : off = 0;
7951 46468 : len = MIN (total_bytes - off, len);
7952 46468 : if (ptr == NULL)
7953 : /* Dry run. */;
7954 : else
7955 46468 : memset (ptr, 0, len);
7956 : return len;
7957 : }
7958 :
7959 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7960 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7961 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7962 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7963 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7964 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7965 : Return the number of bytes placed in the buffer, or zero upon failure. */
7966 :
7967 : int
7968 72064656 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7969 : {
7970 : /* We don't support starting at negative offset and -1 is special. */
7971 72064656 : if (off < -1)
7972 : return 0;
7973 :
7974 72064644 : switch (TREE_CODE (expr))
7975 : {
7976 55608488 : case INTEGER_CST:
7977 55608488 : return native_encode_int (expr, ptr, len, off);
7978 :
7979 857316 : case REAL_CST:
7980 857316 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7981 1714632 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7982 :
7983 0 : case FIXED_CST:
7984 0 : return native_encode_fixed (expr, ptr, len, off);
7985 :
7986 10378 : case COMPLEX_CST:
7987 10378 : return native_encode_complex (expr, ptr, len, off);
7988 :
7989 926685 : case VECTOR_CST:
7990 926685 : return native_encode_vector (expr, ptr, len, off);
7991 :
7992 150782 : case STRING_CST:
7993 150782 : return native_encode_string (expr, ptr, len, off);
7994 :
7995 49732 : case CONSTRUCTOR:
7996 49732 : return native_encode_constructor (expr, ptr, len, off);
7997 :
7998 : default:
7999 : return 0;
8000 : }
8001 : }
8002 :
8003 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8004 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8005 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8006 : machine modes, we can't just use build_nonstandard_integer_type. */
8007 :
8008 : tree
8009 541 : find_bitfield_repr_type (int fieldsize, int len)
8010 : {
8011 541 : machine_mode mode;
8012 1063 : for (int pass = 0; pass < 2; pass++)
8013 : {
8014 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
8015 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
8016 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8017 7286 : && known_eq (GET_MODE_PRECISION (mode),
8018 : GET_MODE_BITSIZE (mode))
8019 11274 : && known_le (GET_MODE_SIZE (mode), len))
8020 : {
8021 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
8022 280 : if (ret && TYPE_MODE (ret) == mode)
8023 : return ret;
8024 : }
8025 : }
8026 :
8027 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
8028 261 : if (int_n_enabled_p[i]
8029 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
8030 261 : && int_n_trees[i].unsigned_type)
8031 : {
8032 261 : tree ret = int_n_trees[i].unsigned_type;
8033 261 : mode = TYPE_MODE (ret);
8034 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8035 522 : && known_eq (GET_MODE_PRECISION (mode),
8036 : GET_MODE_BITSIZE (mode))
8037 783 : && known_le (GET_MODE_SIZE (mode), len))
8038 : return ret;
8039 : }
8040 :
8041 : return NULL_TREE;
8042 : }
8043 :
8044 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8045 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8046 : to be non-NULL and OFF zero), then in addition to filling the
8047 : bytes pointed by PTR with the value also clear any bits pointed
8048 : by MASK that are known to be initialized, keep them as is for
8049 : e.g. uninitialized padding bits or uninitialized fields. */
8050 :
8051 : int
8052 47384251 : native_encode_initializer (tree init, unsigned char *ptr, int len,
8053 : int off, unsigned char *mask)
8054 : {
8055 47384251 : int r;
8056 :
8057 : /* We don't support starting at negative offset and -1 is special. */
8058 47384251 : if (off < -1 || init == NULL_TREE)
8059 : return 0;
8060 :
8061 47384251 : gcc_assert (mask == NULL || (off == 0 && ptr));
8062 :
8063 47384251 : STRIP_NOPS (init);
8064 47384251 : switch (TREE_CODE (init))
8065 : {
8066 0 : case VIEW_CONVERT_EXPR:
8067 0 : case NON_LVALUE_EXPR:
8068 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8069 0 : mask);
8070 45227559 : default:
8071 45227559 : r = native_encode_expr (init, ptr, len, off);
8072 45227559 : if (mask)
8073 7135 : memset (mask, 0, r);
8074 : return r;
8075 2156692 : case CONSTRUCTOR:
8076 2156692 : tree type = TREE_TYPE (init);
8077 2156692 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8078 2156692 : if (total_bytes < 0)
8079 : return 0;
8080 2156692 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
8081 : return 0;
8082 2156616 : int o = off == -1 ? 0 : off;
8083 2156616 : if (TREE_CODE (type) == ARRAY_TYPE)
8084 : {
8085 283708 : tree min_index;
8086 283708 : unsigned HOST_WIDE_INT cnt;
8087 283708 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8088 283708 : constructor_elt *ce;
8089 :
8090 283708 : if (!TYPE_DOMAIN (type)
8091 283708 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8092 : return 0;
8093 :
8094 283708 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
8095 283708 : if (fieldsize <= 0)
8096 : return 0;
8097 :
8098 283708 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8099 283708 : if (ptr)
8100 283708 : memset (ptr, '\0', MIN (total_bytes - off, len));
8101 :
8102 283708 : for (cnt = 0; ; cnt++)
8103 : {
8104 44884666 : tree val = NULL_TREE, index = NULL_TREE;
8105 44884666 : HOST_WIDE_INT pos = curpos, count = 0;
8106 44884666 : bool full = false;
8107 44884666 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8108 : {
8109 44781769 : val = ce->value;
8110 44781769 : index = ce->index;
8111 : }
8112 102897 : else if (mask == NULL
8113 618 : || CONSTRUCTOR_NO_CLEARING (init)
8114 103455 : || curpos >= total_bytes)
8115 : break;
8116 : else
8117 : pos = total_bytes;
8118 :
8119 44781769 : if (index && TREE_CODE (index) == RANGE_EXPR)
8120 : {
8121 54 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8122 54 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8123 0 : return 0;
8124 54 : offset_int wpos
8125 54 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8126 108 : - wi::to_offset (min_index),
8127 54 : TYPE_PRECISION (sizetype));
8128 54 : wpos *= fieldsize;
8129 54 : if (!wi::fits_shwi_p (pos))
8130 : return 0;
8131 54 : pos = wpos.to_shwi ();
8132 54 : offset_int wcount
8133 54 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8134 108 : - wi::to_offset (TREE_OPERAND (index, 0)),
8135 54 : TYPE_PRECISION (sizetype));
8136 54 : if (!wi::fits_shwi_p (wcount))
8137 : return 0;
8138 54 : count = wcount.to_shwi ();
8139 54 : }
8140 44267493 : else if (index)
8141 : {
8142 44267493 : if (TREE_CODE (index) != INTEGER_CST)
8143 0 : return 0;
8144 44267493 : offset_int wpos
8145 44267493 : = wi::sext (wi::to_offset (index)
8146 88534986 : - wi::to_offset (min_index),
8147 44267493 : TYPE_PRECISION (sizetype));
8148 44267493 : wpos *= fieldsize;
8149 44267493 : if (!wi::fits_shwi_p (wpos))
8150 : return 0;
8151 44267493 : pos = wpos.to_shwi ();
8152 : }
8153 :
8154 44783030 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8155 : {
8156 72 : if (valueinit == -1)
8157 : {
8158 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8159 144 : r = native_encode_initializer (zero, ptr + curpos,
8160 : fieldsize, 0,
8161 72 : mask + curpos);
8162 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8163 2 : ggc_free (zero);
8164 72 : if (!r)
8165 : return 0;
8166 72 : valueinit = curpos;
8167 72 : curpos += fieldsize;
8168 : }
8169 102 : while (curpos != pos)
8170 : {
8171 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8172 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8173 30 : curpos += fieldsize;
8174 : }
8175 : }
8176 :
8177 44781841 : curpos = pos;
8178 44781841 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8179 : {
8180 527 : if (count)
8181 : return 0;
8182 527 : if (off == -1
8183 527 : || (curpos >= off
8184 0 : && (curpos + RAW_DATA_LENGTH (val)
8185 0 : <= (HOST_WIDE_INT) off + len)))
8186 : {
8187 527 : if (ptr)
8188 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8189 527 : RAW_DATA_LENGTH (val));
8190 527 : if (mask)
8191 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8192 : }
8193 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8194 0 : && curpos < (HOST_WIDE_INT) off + len)
8195 : {
8196 : /* Partial overlap. */
8197 0 : unsigned char *p = NULL;
8198 0 : int no = 0;
8199 0 : int l;
8200 0 : gcc_assert (mask == NULL);
8201 0 : if (curpos >= off)
8202 : {
8203 0 : if (ptr)
8204 0 : p = ptr + curpos - off;
8205 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8206 : RAW_DATA_LENGTH (val));
8207 : }
8208 : else
8209 : {
8210 0 : p = ptr;
8211 0 : no = off - curpos;
8212 0 : l = len;
8213 : }
8214 0 : if (p)
8215 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8216 : }
8217 527 : curpos += RAW_DATA_LENGTH (val);
8218 527 : val = NULL_TREE;
8219 : }
8220 527 : if (val)
8221 44859336 : do
8222 : {
8223 44859336 : if (off == -1
8224 610010 : || (curpos >= off
8225 203586 : && (curpos + fieldsize
8226 203586 : <= (HOST_WIDE_INT) off + len)))
8227 : {
8228 44422525 : if (full)
8229 : {
8230 78094 : if (ptr)
8231 78094 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8232 : fieldsize);
8233 78094 : if (mask)
8234 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8235 : }
8236 88863178 : else if (!native_encode_initializer (val,
8237 : ptr
8238 44344431 : ? ptr + curpos - o
8239 : : NULL,
8240 : fieldsize,
8241 : off == -1 ? -1
8242 : : 0,
8243 : mask
8244 1117 : ? mask + curpos
8245 : : NULL))
8246 : return 0;
8247 : else
8248 : {
8249 : full = true;
8250 : pos = curpos;
8251 : }
8252 : }
8253 436811 : else if (curpos + fieldsize > off
8254 32650 : && curpos < (HOST_WIDE_INT) off + len)
8255 : {
8256 : /* Partial overlap. */
8257 8139 : unsigned char *p = NULL;
8258 8139 : int no = 0;
8259 8139 : int l;
8260 8139 : gcc_assert (mask == NULL);
8261 8139 : if (curpos >= off)
8262 : {
8263 5876 : if (ptr)
8264 5876 : p = ptr + curpos - off;
8265 5876 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8266 : fieldsize);
8267 : }
8268 : else
8269 : {
8270 2263 : p = ptr;
8271 2263 : no = off - curpos;
8272 2263 : l = len;
8273 : }
8274 8139 : if (!native_encode_initializer (val, p, l, no, NULL))
8275 : return 0;
8276 : }
8277 44678453 : curpos += fieldsize;
8278 : }
8279 44678453 : while (count-- != 0);
8280 44600958 : }
8281 102825 : return MIN (total_bytes - off, len);
8282 : }
8283 1872908 : else if (TREE_CODE (type) == RECORD_TYPE
8284 1872908 : || TREE_CODE (type) == UNION_TYPE)
8285 : {
8286 1872908 : unsigned HOST_WIDE_INT cnt;
8287 1872908 : constructor_elt *ce;
8288 1872908 : tree fld_base = TYPE_FIELDS (type);
8289 1872908 : tree to_free = NULL_TREE;
8290 :
8291 1872908 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8292 1872908 : if (ptr != NULL)
8293 1872908 : memset (ptr, '\0', MIN (total_bytes - o, len));
8294 1872908 : for (cnt = 0; ; cnt++)
8295 : {
8296 2240938 : tree val = NULL_TREE, field = NULL_TREE;
8297 2240938 : HOST_WIDE_INT pos = 0, fieldsize;
8298 2240938 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8299 :
8300 2240938 : if (to_free)
8301 : {
8302 0 : ggc_free (to_free);
8303 0 : to_free = NULL_TREE;
8304 : }
8305 :
8306 2240938 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8307 : {
8308 395268 : val = ce->value;
8309 395268 : field = ce->index;
8310 395268 : if (field == NULL_TREE)
8311 : return 0;
8312 :
8313 395268 : pos = int_byte_position (field);
8314 395268 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8315 1501 : continue;
8316 : }
8317 1845670 : else if (mask == NULL
8318 1845670 : || CONSTRUCTOR_NO_CLEARING (init))
8319 : break;
8320 : else
8321 : pos = total_bytes;
8322 :
8323 407792 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8324 : {
8325 : tree fld;
8326 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8327 : {
8328 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8329 44565 : continue;
8330 2083 : if (fld == field)
8331 : break;
8332 528 : if (DECL_PADDING_P (fld))
8333 87 : continue;
8334 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8335 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8336 : return 0;
8337 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8338 382 : continue;
8339 : break;
8340 : }
8341 1614 : if (fld == NULL_TREE)
8342 : {
8343 895 : if (ce == NULL)
8344 : break;
8345 : return 0;
8346 : }
8347 1614 : fld_base = DECL_CHAIN (fld);
8348 1614 : if (fld != field)
8349 : {
8350 59 : cnt--;
8351 59 : field = fld;
8352 59 : pos = int_byte_position (field);
8353 59 : val = build_zero_cst (TREE_TYPE (fld));
8354 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8355 0 : to_free = val;
8356 : }
8357 : }
8358 :
8359 393826 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8360 15158 : && TYPE_DOMAIN (TREE_TYPE (field))
8361 408984 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8362 : {
8363 81 : if (mask || off != -1)
8364 : return 0;
8365 81 : if (val == NULL_TREE)
8366 0 : continue;
8367 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8368 : return 0;
8369 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8370 81 : if (fieldsize < 0
8371 81 : || (int) fieldsize != fieldsize
8372 81 : || (pos + fieldsize) > INT_MAX)
8373 : return 0;
8374 81 : if (pos + fieldsize > total_bytes)
8375 : {
8376 81 : if (ptr != NULL && total_bytes < len)
8377 81 : memset (ptr + total_bytes, '\0',
8378 81 : MIN (pos + fieldsize, len) - total_bytes);
8379 : total_bytes = pos + fieldsize;
8380 : }
8381 : }
8382 : else
8383 : {
8384 393745 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8385 393745 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8386 : return 0;
8387 393745 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8388 : }
8389 393826 : if (fieldsize == 0)
8390 1 : continue;
8391 :
8392 : /* Prepare to deal with integral bit-fields and filter out other
8393 : bit-fields that do not start and end on a byte boundary. */
8394 393825 : if (DECL_BIT_FIELD (field))
8395 : {
8396 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8397 : return 0;
8398 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8399 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8400 : {
8401 2711 : bpos %= BITS_PER_UNIT;
8402 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8403 2711 : epos = fieldsize % BITS_PER_UNIT;
8404 2711 : fieldsize += BITS_PER_UNIT - 1;
8405 2711 : fieldsize /= BITS_PER_UNIT;
8406 : }
8407 0 : else if (bpos % BITS_PER_UNIT
8408 0 : || DECL_SIZE (field) == NULL_TREE
8409 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8410 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8411 : return 0;
8412 : }
8413 :
8414 393825 : if (off != -1 && pos + fieldsize <= off)
8415 3199 : continue;
8416 :
8417 390626 : if (val == NULL_TREE)
8418 0 : continue;
8419 :
8420 390626 : if (DECL_BIT_FIELD (field)
8421 390626 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8422 : {
8423 : /* FIXME: Handle PDP endian. */
8424 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8425 261 : return 0;
8426 :
8427 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8428 6 : val = TREE_OPERAND (val, 0);
8429 2507 : if (TREE_CODE (val) != INTEGER_CST)
8430 : return 0;
8431 :
8432 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8433 2507 : tree repr_type = NULL_TREE;
8434 2507 : HOST_WIDE_INT rpos = 0;
8435 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8436 : {
8437 1978 : rpos = int_byte_position (repr);
8438 1978 : repr_type = TREE_TYPE (repr);
8439 : }
8440 : else
8441 : {
8442 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8443 529 : if (repr_type == NULL_TREE)
8444 : return 0;
8445 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8446 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8447 268 : if (pos + repr_size <= o + len)
8448 : rpos = pos;
8449 : else
8450 : {
8451 14 : rpos = o + len - repr_size;
8452 14 : gcc_assert (rpos <= pos);
8453 : }
8454 : }
8455 :
8456 2246 : if (rpos > pos)
8457 : return 0;
8458 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8459 2246 : int diff = (TYPE_PRECISION (repr_type)
8460 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8461 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8462 2246 : if (!BYTES_BIG_ENDIAN)
8463 2246 : w = wi::lshift (w, bitoff);
8464 : else
8465 : w = wi::lshift (w, diff - bitoff);
8466 2246 : val = wide_int_to_tree (repr_type, w);
8467 :
8468 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8469 : / BITS_PER_UNIT + 1];
8470 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8471 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8472 0 : return 0;
8473 :
8474 2246 : if (ptr == NULL)
8475 0 : continue;
8476 :
8477 : /* If the bitfield does not start at byte boundary, handle
8478 : the partial byte at the start. */
8479 2246 : if (bpos
8480 1351 : && (off == -1 || (pos >= off && len >= 1)))
8481 : {
8482 1276 : if (!BYTES_BIG_ENDIAN)
8483 : {
8484 1276 : int msk = (1 << bpos) - 1;
8485 1276 : buf[pos - rpos] &= ~msk;
8486 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8487 1276 : if (mask)
8488 : {
8489 147 : if (fieldsize > 1 || epos == 0)
8490 129 : mask[pos] &= msk;
8491 : else
8492 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8493 : }
8494 : }
8495 : else
8496 : {
8497 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8498 : buf[pos - rpos] &= msk;
8499 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8500 : if (mask)
8501 : {
8502 : if (fieldsize > 1 || epos == 0)
8503 : mask[pos] &= ~msk;
8504 : else
8505 : mask[pos] &= (~msk
8506 : | ((1 << (BITS_PER_UNIT - epos))
8507 : - 1));
8508 : }
8509 : }
8510 : }
8511 : /* If the bitfield does not end at byte boundary, handle
8512 : the partial byte at the end. */
8513 2246 : if (epos
8514 1724 : && (off == -1
8515 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8516 : {
8517 1621 : if (!BYTES_BIG_ENDIAN)
8518 : {
8519 1621 : int msk = (1 << epos) - 1;
8520 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8521 1621 : buf[pos - rpos + fieldsize - 1]
8522 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8523 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8524 156 : mask[pos + fieldsize - 1] &= ~msk;
8525 : }
8526 : else
8527 : {
8528 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8529 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8530 : buf[pos - rpos + fieldsize - 1]
8531 : |= ptr[pos + fieldsize - 1 - o] & msk;
8532 : if (mask && (fieldsize > 1 || bpos == 0))
8533 : mask[pos + fieldsize - 1] &= msk;
8534 : }
8535 : }
8536 2246 : if (off == -1
8537 1301 : || (pos >= off
8538 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8539 : {
8540 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8541 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8542 75 : memset (mask + pos + (bpos != 0), 0,
8543 75 : fieldsize - (bpos != 0) - (epos != 0));
8544 : }
8545 : else
8546 : {
8547 : /* Partial overlap. */
8548 191 : HOST_WIDE_INT fsz = fieldsize;
8549 191 : gcc_assert (mask == NULL);
8550 191 : if (pos < off)
8551 : {
8552 89 : fsz -= (off - pos);
8553 89 : pos = off;
8554 : }
8555 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8556 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8557 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8558 : }
8559 2246 : continue;
8560 2246 : }
8561 :
8562 388119 : if (off == -1
8563 28592 : || (pos >= off
8564 27760 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8565 : {
8566 379746 : int fldsize = fieldsize;
8567 20219 : if (off == -1)
8568 : {
8569 359527 : tree fld = DECL_CHAIN (field);
8570 5704785 : while (fld)
8571 : {
8572 5363993 : if (TREE_CODE (fld) == FIELD_DECL)
8573 : break;
8574 5345258 : fld = DECL_CHAIN (fld);
8575 : }
8576 359527 : if (fld == NULL_TREE)
8577 340792 : fldsize = len - pos;
8578 : }
8579 411823 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8580 : : NULL,
8581 : fldsize,
8582 : off == -1 ? -1 : 0,
8583 11858 : mask ? mask + pos : NULL);
8584 379746 : if (!r)
8585 : return 0;
8586 360060 : if (off == -1
8587 347083 : && fldsize != fieldsize
8588 1149 : && r > fieldsize
8589 822 : && pos + r > total_bytes)
8590 368030 : total_bytes = pos + r;
8591 : }
8592 : else
8593 : {
8594 : /* Partial overlap. */
8595 8373 : unsigned char *p = NULL;
8596 8373 : int no = 0;
8597 8373 : int l;
8598 8373 : gcc_assert (mask == NULL);
8599 8373 : if (pos >= off)
8600 : {
8601 7541 : if (ptr)
8602 7541 : p = ptr + pos - off;
8603 7541 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8604 : fieldsize);
8605 : }
8606 : else
8607 : {
8608 832 : p = ptr;
8609 832 : no = off - pos;
8610 832 : l = len;
8611 : }
8612 8373 : if (!native_encode_initializer (val, p, l, no, NULL))
8613 : return 0;
8614 : }
8615 368030 : }
8616 1845611 : return MIN (total_bytes - off, len);
8617 : }
8618 : return 0;
8619 : }
8620 : }
8621 :
8622 :
8623 : /* Subroutine of native_interpret_expr. Interpret the contents of
8624 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8625 : If the buffer cannot be interpreted, return NULL_TREE. */
8626 :
8627 : static tree
8628 2947678 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8629 : {
8630 2947678 : int total_bytes;
8631 2947678 : if (BITINT_TYPE_P (type))
8632 : {
8633 79 : struct bitint_info info;
8634 79 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8635 79 : gcc_assert (ok);
8636 79 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8637 79 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8638 : {
8639 79 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8640 : /* More work is needed when adding _BitInt support to PDP endian
8641 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8642 : match target endianity here. */
8643 79 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8644 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8645 : || (GET_MODE_SIZE (limb_mode)
8646 : >= UNITS_PER_WORD)));
8647 : }
8648 : else
8649 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8650 : }
8651 : else
8652 5895198 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8653 :
8654 2947678 : if (total_bytes > len)
8655 : return NULL_TREE;
8656 :
8657 2947438 : wide_int result = wi::from_buffer (ptr, total_bytes);
8658 :
8659 2947438 : return wide_int_to_tree (type, result);
8660 2947438 : }
8661 :
8662 :
8663 : /* Subroutine of native_interpret_expr. Interpret the contents of
8664 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8665 : If the buffer cannot be interpreted, return NULL_TREE. */
8666 :
8667 : static tree
8668 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8669 : {
8670 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8671 0 : int total_bytes = GET_MODE_SIZE (mode);
8672 0 : double_int result;
8673 0 : FIXED_VALUE_TYPE fixed_value;
8674 :
8675 0 : if (total_bytes > len
8676 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8677 : return NULL_TREE;
8678 :
8679 0 : result = double_int::from_buffer (ptr, total_bytes);
8680 0 : fixed_value = fixed_from_double_int (result, mode);
8681 :
8682 0 : return build_fixed (type, fixed_value);
8683 : }
8684 :
8685 :
8686 : /* Subroutine of native_interpret_expr. Interpret the contents of
8687 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8688 : If the buffer cannot be interpreted, return NULL_TREE. */
8689 :
8690 : tree
8691 38164 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8692 : {
8693 38164 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8694 38164 : int total_bytes = GET_MODE_SIZE (mode);
8695 38164 : unsigned char value;
8696 : /* There are always 32 bits in each long, no matter the size of
8697 : the hosts long. We handle floating point representations with
8698 : up to 192 bits. */
8699 38164 : REAL_VALUE_TYPE r;
8700 38164 : long tmp[6];
8701 :
8702 38164 : if (total_bytes > len || total_bytes > 24)
8703 : return NULL_TREE;
8704 38103 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8705 :
8706 38103 : memset (tmp, 0, sizeof (tmp));
8707 276825 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8708 238722 : bitpos += BITS_PER_UNIT)
8709 : {
8710 : /* Both OFFSET and BYTE index within a long;
8711 : bitpos indexes the whole float. */
8712 238722 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8713 238722 : if (UNITS_PER_WORD < 4)
8714 : {
8715 : int word = byte / UNITS_PER_WORD;
8716 : if (WORDS_BIG_ENDIAN)
8717 : word = (words - 1) - word;
8718 : offset = word * UNITS_PER_WORD;
8719 : if (BYTES_BIG_ENDIAN)
8720 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8721 : else
8722 : offset += byte % UNITS_PER_WORD;
8723 : }
8724 : else
8725 : {
8726 238722 : offset = byte;
8727 238722 : if (BYTES_BIG_ENDIAN)
8728 : {
8729 : /* Reverse bytes within each long, or within the entire float
8730 : if it's smaller than a long (for HFmode). */
8731 : offset = MIN (3, total_bytes - 1) - offset;
8732 : gcc_assert (offset >= 0);
8733 : }
8734 : }
8735 238722 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8736 :
8737 238722 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8738 : }
8739 :
8740 38103 : real_from_target (&r, tmp, mode);
8741 38103 : return build_real (type, r);
8742 : }
8743 :
8744 :
8745 : /* Subroutine of native_interpret_expr. Interpret the contents of
8746 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8747 : If the buffer cannot be interpreted, return NULL_TREE. */
8748 :
8749 : static tree
8750 1598 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8751 : {
8752 1598 : tree etype, rpart, ipart;
8753 1598 : int size;
8754 :
8755 1598 : etype = TREE_TYPE (type);
8756 1598 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8757 1598 : if (size * 2 > len)
8758 : return NULL_TREE;
8759 1565 : rpart = native_interpret_expr (etype, ptr, size);
8760 1565 : if (!rpart)
8761 : return NULL_TREE;
8762 1384 : ipart = native_interpret_expr (etype, ptr+size, size);
8763 1384 : if (!ipart)
8764 : return NULL_TREE;
8765 1384 : return build_complex (type, rpart, ipart);
8766 : }
8767 :
8768 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8769 : which contains LEN bytes. The vector is known to be encodable using
8770 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8771 :
8772 : Return the vector on success, otherwise return null. */
8773 :
8774 : static tree
8775 240725 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8776 : unsigned int len, unsigned int npatterns,
8777 : unsigned int nelts_per_pattern)
8778 : {
8779 240725 : tree elt_type = TREE_TYPE (type);
8780 240725 : if (VECTOR_BOOLEAN_TYPE_P (type)
8781 240740 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8782 : {
8783 : /* This is the only case in which elements can be smaller than a byte.
8784 : Element 0 is always in the lsb of the containing byte. */
8785 5 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8786 5 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8787 : return NULL_TREE;
8788 :
8789 5 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8790 76 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8791 : {
8792 66 : unsigned int bit_index = i * elt_bits;
8793 66 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8794 66 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8795 132 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8796 116 : ? build_all_ones_cst (elt_type)
8797 50 : : build_zero_cst (elt_type));
8798 : }
8799 5 : return builder.build ();
8800 5 : }
8801 :
8802 240720 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8803 240720 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8804 : return NULL_TREE;
8805 :
8806 240720 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8807 1197495 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8808 : {
8809 716189 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8810 716189 : if (!elt)
8811 134 : return NULL_TREE;
8812 716055 : builder.quick_push (elt);
8813 716055 : bytes += elt_bytes;
8814 : }
8815 240586 : return builder.build ();
8816 240720 : }
8817 :
8818 : /* Subroutine of native_interpret_expr. Interpret the contents of
8819 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8820 : If the buffer cannot be interpreted, return NULL_TREE. */
8821 :
8822 : static tree
8823 83401 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8824 : {
8825 83401 : unsigned HOST_WIDE_INT size;
8826 :
8827 83401 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8828 83401 : || size > len)
8829 2 : return NULL_TREE;
8830 :
8831 83399 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8832 83399 : return native_interpret_vector_part (type, ptr, len, count, 1);
8833 : }
8834 :
8835 :
8836 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8837 : the buffer PTR of length LEN as a constant of type TYPE. For
8838 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8839 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8840 : return NULL_TREE. */
8841 :
8842 : tree
8843 3227733 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8844 : {
8845 3227733 : switch (TREE_CODE (type))
8846 : {
8847 2947678 : case INTEGER_TYPE:
8848 2947678 : case ENUMERAL_TYPE:
8849 2947678 : case BOOLEAN_TYPE:
8850 2947678 : case POINTER_TYPE:
8851 2947678 : case REFERENCE_TYPE:
8852 2947678 : case OFFSET_TYPE:
8853 2947678 : case BITINT_TYPE:
8854 2947678 : return native_interpret_int (type, ptr, len);
8855 :
8856 36577 : case REAL_TYPE:
8857 36577 : if (tree ret = native_interpret_real (type, ptr, len))
8858 : {
8859 : /* For floating point values in composite modes, punt if this
8860 : folding doesn't preserve bit representation. As the mode doesn't
8861 : have fixed precision while GCC pretends it does, there could be
8862 : valid values that GCC can't really represent accurately.
8863 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8864 : bit combinationations which GCC doesn't preserve. */
8865 36516 : unsigned char buf[24 * 2];
8866 36516 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8867 36516 : int total_bytes = GET_MODE_SIZE (mode);
8868 36516 : memcpy (buf + 24, ptr, total_bytes);
8869 36516 : clear_type_padding_in_mask (type, buf + 24);
8870 36516 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8871 36516 : || memcmp (buf + 24, buf, total_bytes) != 0)
8872 488 : return NULL_TREE;
8873 : return ret;
8874 : }
8875 : return NULL_TREE;
8876 :
8877 0 : case FIXED_POINT_TYPE:
8878 0 : return native_interpret_fixed (type, ptr, len);
8879 :
8880 1598 : case COMPLEX_TYPE:
8881 1598 : return native_interpret_complex (type, ptr, len);
8882 :
8883 83401 : case VECTOR_TYPE:
8884 83401 : return native_interpret_vector (type, ptr, len);
8885 :
8886 : default:
8887 : return NULL_TREE;
8888 : }
8889 : }
8890 :
8891 : /* Returns true if we can interpret the contents of a native encoding
8892 : as TYPE. */
8893 :
8894 : bool
8895 603551 : can_native_interpret_type_p (tree type)
8896 : {
8897 603551 : switch (TREE_CODE (type))
8898 : {
8899 : case INTEGER_TYPE:
8900 : case ENUMERAL_TYPE:
8901 : case BOOLEAN_TYPE:
8902 : case POINTER_TYPE:
8903 : case REFERENCE_TYPE:
8904 : case FIXED_POINT_TYPE:
8905 : case REAL_TYPE:
8906 : case COMPLEX_TYPE:
8907 : case VECTOR_TYPE:
8908 : case OFFSET_TYPE:
8909 : return true;
8910 41709 : default:
8911 41709 : return false;
8912 : }
8913 : }
8914 :
8915 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8916 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8917 :
8918 : tree
8919 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8920 : int len)
8921 : {
8922 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8923 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8924 : {
8925 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8926 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8927 : return NULL_TREE;
8928 :
8929 197 : HOST_WIDE_INT cnt = 0;
8930 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8931 : {
8932 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8933 : return NULL_TREE;
8934 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8935 : }
8936 197 : if (eltsz == 0)
8937 0 : cnt = 0;
8938 197 : HOST_WIDE_INT pos = 0;
8939 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8940 : {
8941 439 : tree v = NULL_TREE;
8942 439 : if (pos >= len || pos + eltsz > len)
8943 9631 : return NULL_TREE;
8944 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8945 : {
8946 367 : v = native_interpret_expr (TREE_TYPE (type),
8947 367 : ptr + off + pos, eltsz);
8948 367 : if (v == NULL_TREE)
8949 : return NULL_TREE;
8950 : }
8951 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8952 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8953 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8954 : eltsz);
8955 72 : if (v == NULL_TREE)
8956 : return NULL_TREE;
8957 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8958 : }
8959 197 : return build_constructor (type, elts);
8960 : }
8961 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8962 : return NULL_TREE;
8963 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8964 : {
8965 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8966 796932 : || is_empty_type (TREE_TYPE (field)))
8967 761072 : continue;
8968 13494 : tree fld = field;
8969 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8970 13494 : int diff = 0;
8971 13494 : tree v = NULL_TREE;
8972 13494 : if (DECL_BIT_FIELD (field))
8973 : {
8974 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8975 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8976 : {
8977 168 : poly_int64 bitoffset;
8978 168 : poly_uint64 field_offset, fld_offset;
8979 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8980 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8981 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8982 : else
8983 : bitoffset = 0;
8984 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8985 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8986 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8987 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8988 168 : if (!bitoffset.is_constant (&bitoff)
8989 168 : || bitoff < 0
8990 168 : || bitoff > diff)
8991 0 : return NULL_TREE;
8992 : }
8993 : else
8994 : {
8995 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8996 : return NULL_TREE;
8997 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8998 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8999 12 : bpos %= BITS_PER_UNIT;
9000 12 : fieldsize += bpos;
9001 12 : fieldsize += BITS_PER_UNIT - 1;
9002 12 : fieldsize /= BITS_PER_UNIT;
9003 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
9004 12 : if (repr_type == NULL_TREE)
9005 : return NULL_TREE;
9006 12 : sz = int_size_in_bytes (repr_type);
9007 12 : if (sz < 0 || sz > len)
9008 : return NULL_TREE;
9009 12 : pos = int_byte_position (field);
9010 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
9011 : return NULL_TREE;
9012 12 : HOST_WIDE_INT rpos;
9013 12 : if (pos + sz <= len)
9014 : rpos = pos;
9015 : else
9016 : {
9017 0 : rpos = len - sz;
9018 0 : gcc_assert (rpos <= pos);
9019 : }
9020 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
9021 12 : pos = rpos;
9022 12 : diff = (TYPE_PRECISION (repr_type)
9023 12 : - TYPE_PRECISION (TREE_TYPE (field)));
9024 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
9025 12 : if (v == NULL_TREE)
9026 : return NULL_TREE;
9027 : fld = NULL_TREE;
9028 : }
9029 : }
9030 :
9031 168 : if (fld)
9032 : {
9033 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
9034 13482 : if (sz < 0 || sz > len)
9035 : return NULL_TREE;
9036 13482 : tree byte_pos = byte_position (fld);
9037 13482 : if (!tree_fits_shwi_p (byte_pos))
9038 : return NULL_TREE;
9039 13482 : pos = tree_to_shwi (byte_pos);
9040 13482 : if (pos < 0 || pos > len || pos + sz > len)
9041 : return NULL_TREE;
9042 : }
9043 13482 : if (fld == NULL_TREE)
9044 : /* Already handled above. */;
9045 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
9046 : {
9047 6262 : v = native_interpret_expr (TREE_TYPE (fld),
9048 6262 : ptr + off + pos, sz);
9049 6262 : if (v == NULL_TREE)
9050 : return NULL_TREE;
9051 : }
9052 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
9053 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
9054 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
9055 7232 : if (v == NULL_TREE)
9056 : return NULL_TREE;
9057 13494 : if (fld != field)
9058 : {
9059 180 : if (TREE_CODE (v) != INTEGER_CST)
9060 : return NULL_TREE;
9061 :
9062 : /* FIXME: Figure out how to handle PDP endian bitfields. */
9063 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9064 : return NULL_TREE;
9065 180 : if (!BYTES_BIG_ENDIAN)
9066 180 : v = wide_int_to_tree (TREE_TYPE (field),
9067 360 : wi::lrshift (wi::to_wide (v), bitoff));
9068 : else
9069 : v = wide_int_to_tree (TREE_TYPE (field),
9070 : wi::lrshift (wi::to_wide (v),
9071 : diff - bitoff));
9072 : }
9073 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
9074 : }
9075 9434 : return build_constructor (type, elts);
9076 : }
9077 :
9078 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
9079 : or extracted constant positions and/or sizes aren't byte aligned. */
9080 :
9081 : /* Shift left 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 = 01111111|10000000
9086 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9087 :
9088 : void
9089 30608 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9090 : unsigned int amnt)
9091 : {
9092 30608 : if (amnt == 0)
9093 : return;
9094 :
9095 17809 : unsigned char carry_over = 0U;
9096 17809 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9097 17809 : unsigned char clear_mask = (~0U) << amnt;
9098 :
9099 107278 : for (unsigned int i = 0; i < sz; i++)
9100 : {
9101 89469 : unsigned prev_carry_over = carry_over;
9102 89469 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9103 :
9104 89469 : ptr[i] <<= amnt;
9105 89469 : if (i != 0)
9106 : {
9107 71660 : ptr[i] &= clear_mask;
9108 71660 : ptr[i] |= prev_carry_over;
9109 : }
9110 : }
9111 : }
9112 :
9113 : /* Like shift_bytes_in_array_left but for big-endian.
9114 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9115 : bits between adjacent elements. AMNT should be within
9116 : [0, BITS_PER_UNIT).
9117 : Example, AMNT = 2:
9118 : 00011111|11100000 >> 2 = 00000111|11111000
9119 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9120 :
9121 : void
9122 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9123 : unsigned int amnt)
9124 : {
9125 8 : if (amnt == 0)
9126 : return;
9127 :
9128 4 : unsigned char carry_over = 0U;
9129 4 : unsigned char carry_mask = ~(~0U << amnt);
9130 :
9131 12 : for (unsigned int i = 0; i < sz; i++)
9132 : {
9133 8 : unsigned prev_carry_over = carry_over;
9134 8 : carry_over = ptr[i] & carry_mask;
9135 :
9136 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9137 8 : ptr[i] >>= amnt;
9138 8 : ptr[i] |= prev_carry_over;
9139 : }
9140 : }
9141 :
9142 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9143 : directly on the VECTOR_CST encoding, in a way that works for variable-
9144 : length vectors. Return the resulting VECTOR_CST on success or null
9145 : on failure. */
9146 :
9147 : static tree
9148 166727 : fold_view_convert_vector_encoding (tree type, tree expr)
9149 : {
9150 166727 : tree expr_type = TREE_TYPE (expr);
9151 166727 : poly_uint64 type_bits, expr_bits;
9152 166727 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9153 166727 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9154 : return NULL_TREE;
9155 :
9156 166727 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9157 166727 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9158 166727 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9159 166727 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9160 :
9161 : /* We can only preserve the semantics of a stepped pattern if the new
9162 : vector element is an integer of the same size. */
9163 166727 : if (VECTOR_CST_STEPPED_P (expr)
9164 166727 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9165 : return NULL_TREE;
9166 :
9167 : /* The number of bits needed to encode one element from every pattern
9168 : of the original vector. */
9169 157326 : unsigned int expr_sequence_bits
9170 157326 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9171 :
9172 : /* The number of bits needed to encode one element from every pattern
9173 : of the result. */
9174 157326 : unsigned int type_sequence_bits
9175 157326 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9176 :
9177 : /* Don't try to read more bytes than are available, which can happen
9178 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9179 : The general VIEW_CONVERT handling can cope with that case, so there's
9180 : no point complicating things here. */
9181 157326 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9182 157326 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9183 : BITS_PER_UNIT);
9184 157326 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9185 157326 : if (known_gt (buffer_bits, expr_bits))
9186 : return NULL_TREE;
9187 :
9188 : /* Get enough bytes of EXPR to form the new encoding. */
9189 157326 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9190 157326 : buffer.quick_grow (buffer_bytes);
9191 157326 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9192 157326 : buffer_bits / expr_elt_bits)
9193 : != (int) buffer_bytes)
9194 : return NULL_TREE;
9195 :
9196 : /* Re-encode the bytes as TYPE. */
9197 157326 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9198 314652 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9199 157326 : type_npatterns, nelts_per_pattern);
9200 157326 : }
9201 :
9202 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9203 : TYPE at compile-time. If we're unable to perform the conversion
9204 : return NULL_TREE. */
9205 :
9206 : static tree
9207 13194930 : fold_view_convert_expr (tree type, tree expr)
9208 : {
9209 13194930 : unsigned char buffer[128];
9210 13194930 : unsigned char *buf;
9211 13194930 : int len;
9212 13194930 : HOST_WIDE_INT l;
9213 :
9214 : /* Check that the host and target are sane. */
9215 13194930 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9216 : return NULL_TREE;
9217 :
9218 13194930 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9219 166727 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9220 : return res;
9221 :
9222 13037671 : l = int_size_in_bytes (type);
9223 13037671 : if (l > (int) sizeof (buffer)
9224 13037671 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9225 : {
9226 0 : buf = XALLOCAVEC (unsigned char, l);
9227 0 : len = l;
9228 : }
9229 : else
9230 : {
9231 : buf = buffer;
9232 : len = sizeof (buffer);
9233 : }
9234 13037671 : len = native_encode_expr (expr, buf, len);
9235 13037671 : if (len == 0)
9236 : return NULL_TREE;
9237 :
9238 1885330 : return native_interpret_expr (type, buf, len);
9239 : }
9240 :
9241 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9242 : to avoid confusing the gimplify process. */
9243 :
9244 : tree
9245 594127026 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9246 : {
9247 : /* The size of the object is not relevant when talking about its address. */
9248 594127026 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9249 0 : t = TREE_OPERAND (t, 0);
9250 :
9251 594127026 : if (INDIRECT_REF_P (t))
9252 : {
9253 62123369 : t = TREE_OPERAND (t, 0);
9254 :
9255 62123369 : if (TREE_TYPE (t) != ptrtype)
9256 39842498 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9257 : }
9258 532003657 : else if (TREE_CODE (t) == MEM_REF
9259 532003657 : && integer_zerop (TREE_OPERAND (t, 1)))
9260 : {
9261 1700776 : t = TREE_OPERAND (t, 0);
9262 :
9263 1700776 : if (TREE_TYPE (t) != ptrtype)
9264 1116945 : t = fold_convert_loc (loc, ptrtype, t);
9265 : }
9266 530302881 : else if (TREE_CODE (t) == MEM_REF
9267 530302881 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9268 662 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9269 : TREE_OPERAND (t, 0),
9270 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9271 530302219 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9272 : {
9273 31388563 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9274 :
9275 31388563 : if (TREE_TYPE (t) != ptrtype)
9276 16310 : t = fold_convert_loc (loc, ptrtype, t);
9277 : }
9278 : else
9279 498913656 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9280 :
9281 : return t;
9282 : }
9283 :
9284 : /* Build an expression for the address of T. */
9285 :
9286 : tree
9287 512310478 : build_fold_addr_expr_loc (location_t loc, tree t)
9288 : {
9289 512310478 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9290 :
9291 512310478 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9292 : }
9293 :
9294 : /* Fold a unary expression of code CODE and type TYPE with operand
9295 : OP0. Return the folded expression if folding is successful.
9296 : Otherwise, return NULL_TREE. */
9297 :
9298 : tree
9299 2088904288 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9300 : {
9301 2088904288 : tree tem;
9302 2088904288 : tree arg0;
9303 2088904288 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9304 :
9305 2088904288 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9306 : && TREE_CODE_LENGTH (code) == 1);
9307 :
9308 2088904288 : arg0 = op0;
9309 2088904288 : if (arg0)
9310 : {
9311 2088890991 : if (CONVERT_EXPR_CODE_P (code)
9312 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9313 : {
9314 : /* Don't use STRIP_NOPS, because signedness of argument type
9315 : matters. */
9316 1191870411 : STRIP_SIGN_NOPS (arg0);
9317 : }
9318 : else
9319 : {
9320 : /* Strip any conversions that don't change the mode. This
9321 : is safe for every expression, except for a comparison
9322 : expression because its signedness is derived from its
9323 : operands.
9324 :
9325 : Note that this is done as an internal manipulation within
9326 : the constant folder, in order to find the simplest
9327 : representation of the arguments so that their form can be
9328 : studied. In any cases, the appropriate type conversions
9329 : should be put back in the tree that will get out of the
9330 : constant folder. */
9331 897020580 : STRIP_NOPS (arg0);
9332 : }
9333 :
9334 2088890991 : if (CONSTANT_CLASS_P (arg0))
9335 : {
9336 327743243 : tree tem = const_unop (code, type, arg0);
9337 327743243 : if (tem)
9338 : {
9339 287692212 : if (TREE_TYPE (tem) != type)
9340 9880 : tem = fold_convert_loc (loc, type, tem);
9341 : return tem;
9342 : }
9343 : }
9344 : }
9345 :
9346 1801212076 : tem = generic_simplify (loc, code, type, op0);
9347 1801212076 : if (tem)
9348 : return tem;
9349 :
9350 1356055186 : if (TREE_CODE_CLASS (code) == tcc_unary)
9351 : {
9352 772048885 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9353 1075445 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9354 : fold_build1_loc (loc, code, type,
9355 1075445 : fold_convert_loc (loc, TREE_TYPE (op0),
9356 2150890 : TREE_OPERAND (arg0, 1))));
9357 770973440 : else if (TREE_CODE (arg0) == COND_EXPR)
9358 : {
9359 551614 : tree arg01 = TREE_OPERAND (arg0, 1);
9360 551614 : tree arg02 = TREE_OPERAND (arg0, 2);
9361 551614 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9362 547432 : arg01 = fold_build1_loc (loc, code, type,
9363 : fold_convert_loc (loc,
9364 547432 : TREE_TYPE (op0), arg01));
9365 551614 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9366 541262 : arg02 = fold_build1_loc (loc, code, type,
9367 : fold_convert_loc (loc,
9368 541262 : TREE_TYPE (op0), arg02));
9369 551614 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9370 : arg01, arg02);
9371 :
9372 : /* If this was a conversion, and all we did was to move into
9373 : inside the COND_EXPR, bring it back out. But leave it if
9374 : it is a conversion from integer to integer and the
9375 : result precision is no wider than a word since such a
9376 : conversion is cheap and may be optimized away by combine,
9377 : while it couldn't if it were outside the COND_EXPR. Then return
9378 : so we don't get into an infinite recursion loop taking the
9379 : conversion out and then back in. */
9380 :
9381 551614 : if ((CONVERT_EXPR_CODE_P (code)
9382 10314 : || code == NON_LVALUE_EXPR)
9383 541319 : && TREE_CODE (tem) == COND_EXPR
9384 521625 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9385 457752 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9386 291739 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9387 291527 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9388 291527 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9389 291527 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9390 864462 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9391 22136 : && (INTEGRAL_TYPE_P
9392 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9393 22096 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9394 21983 : || flag_syntax_only))
9395 268769 : tem = build1_loc (loc, code, type,
9396 : build3 (COND_EXPR,
9397 268769 : TREE_TYPE (TREE_OPERAND
9398 : (TREE_OPERAND (tem, 1), 0)),
9399 268769 : TREE_OPERAND (tem, 0),
9400 268769 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9401 268769 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9402 : 0)));
9403 : return tem;
9404 : }
9405 : }
9406 :
9407 1354428127 : switch (code)
9408 : {
9409 42877044 : case NON_LVALUE_EXPR:
9410 42877044 : if (!maybe_lvalue_p (op0))
9411 31156521 : return fold_convert_loc (loc, type, op0);
9412 : return NULL_TREE;
9413 :
9414 717025279 : CASE_CONVERT:
9415 717025279 : case FLOAT_EXPR:
9416 717025279 : case FIX_TRUNC_EXPR:
9417 717025279 : if (COMPARISON_CLASS_P (op0))
9418 : {
9419 : /* If we have (type) (a CMP b) and type is an integral type, return
9420 : new expression involving the new type. Canonicalize
9421 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9422 : non-integral type.
9423 : Do not fold the result as that would not simplify further, also
9424 : folding again results in recursions. */
9425 750723 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9426 167157 : return build2_loc (loc, TREE_CODE (op0), type,
9427 167157 : TREE_OPERAND (op0, 0),
9428 334314 : TREE_OPERAND (op0, 1));
9429 583566 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9430 7685 : && TREE_CODE (type) != VECTOR_TYPE)
9431 7685 : return build3_loc (loc, COND_EXPR, type, op0,
9432 : constant_boolean_node (true, type),
9433 7685 : constant_boolean_node (false, type));
9434 : }
9435 :
9436 : /* Handle (T *)&A.B.C for A being of type T and B and C
9437 : living at offset zero. This occurs frequently in
9438 : C++ upcasting and then accessing the base. */
9439 716850437 : if (TREE_CODE (op0) == ADDR_EXPR
9440 251893647 : && POINTER_TYPE_P (type)
9441 961815268 : && handled_component_p (TREE_OPERAND (op0, 0)))
9442 : {
9443 53915653 : poly_int64 bitsize, bitpos;
9444 53915653 : tree offset;
9445 53915653 : machine_mode mode;
9446 53915653 : int unsignedp, reversep, volatilep;
9447 53915653 : tree base
9448 53915653 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9449 : &offset, &mode, &unsignedp, &reversep,
9450 : &volatilep);
9451 : /* If the reference was to a (constant) zero offset, we can use
9452 : the address of the base if it has the same base type
9453 : as the result type and the pointer type is unqualified. */
9454 53915653 : if (!offset
9455 53769788 : && known_eq (bitpos, 0)
9456 37088402 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9457 37088402 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9458 107685441 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9459 12907 : return fold_convert_loc (loc, type,
9460 12907 : build_fold_addr_expr_loc (loc, base));
9461 : }
9462 :
9463 716837530 : if (TREE_CODE (op0) == MODIFY_EXPR
9464 289741 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9465 : /* Detect assigning a bitfield. */
9466 716839583 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9467 116 : && DECL_BIT_FIELD
9468 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9469 : {
9470 : /* Don't leave an assignment inside a conversion
9471 : unless assigning a bitfield. */
9472 2005 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9473 : /* First do the assignment, then return converted constant. */
9474 2005 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9475 2005 : suppress_warning (tem /* What warning? */);
9476 2005 : TREE_USED (tem) = 1;
9477 2005 : return tem;
9478 : }
9479 :
9480 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9481 : constants (if x has signed type, the sign bit cannot be set
9482 : in c). This folds extension into the BIT_AND_EXPR.
9483 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9484 : very likely don't have maximal range for their precision and this
9485 : transformation effectively doesn't preserve non-maximal ranges. */
9486 716835525 : if (TREE_CODE (type) == INTEGER_TYPE
9487 249702944 : && TREE_CODE (op0) == BIT_AND_EXPR
9488 717445581 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9489 : {
9490 218861 : tree and_expr = op0;
9491 218861 : tree and0 = TREE_OPERAND (and_expr, 0);
9492 218861 : tree and1 = TREE_OPERAND (and_expr, 1);
9493 218861 : int change = 0;
9494 :
9495 218861 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9496 218861 : || (TYPE_PRECISION (type)
9497 59315 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9498 : change = 1;
9499 18013 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9500 : <= HOST_BITS_PER_WIDE_INT
9501 18013 : && tree_fits_uhwi_p (and1))
9502 : {
9503 16807 : unsigned HOST_WIDE_INT cst;
9504 :
9505 16807 : cst = tree_to_uhwi (and1);
9506 33614 : cst &= HOST_WIDE_INT_M1U
9507 16807 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9508 16807 : change = (cst == 0);
9509 16807 : if (change
9510 16807 : && !flag_syntax_only
9511 33614 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9512 : == ZERO_EXTEND))
9513 : {
9514 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9515 : and0 = fold_convert_loc (loc, uns, and0);
9516 : and1 = fold_convert_loc (loc, uns, and1);
9517 : }
9518 : }
9519 16807 : if (change)
9520 : {
9521 217655 : tree and1_type = TREE_TYPE (and1);
9522 217655 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9523 : TYPE_PRECISION (type));
9524 217655 : tem = force_fit_type (type,
9525 217655 : wide_int::from (wi::to_wide (and1), prec,
9526 217655 : TYPE_SIGN (and1_type)),
9527 217655 : 0, TREE_OVERFLOW (and1));
9528 217655 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9529 217655 : fold_convert_loc (loc, type, and0), tem);
9530 : }
9531 : }
9532 :
9533 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9534 : cast (T1)X will fold away. We assume that this happens when X itself
9535 : is a cast. */
9536 716617870 : if (POINTER_TYPE_P (type)
9537 432046476 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9538 750455697 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9539 : {
9540 31254829 : tree arg00 = TREE_OPERAND (arg0, 0);
9541 31254829 : tree arg01 = TREE_OPERAND (arg0, 1);
9542 :
9543 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9544 : when the pointed type needs higher alignment than
9545 : the p+ first operand's pointed type. */
9546 31254829 : if (!in_gimple_form
9547 31233862 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9548 31256043 : && (min_align_of_type (TREE_TYPE (type))
9549 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9550 : return NULL_TREE;
9551 :
9552 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9553 : when type is a reference type and arg00's type is not,
9554 : because arg00 could be validly nullptr and if arg01 doesn't return,
9555 : we don't want false positive binding of reference to nullptr. */
9556 31254762 : if (TREE_CODE (type) == REFERENCE_TYPE
9557 17568582 : && !in_gimple_form
9558 17568561 : && sanitize_flags_p (SANITIZE_NULL)
9559 31255193 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9560 : return NULL_TREE;
9561 :
9562 31254331 : arg00 = fold_convert_loc (loc, type, arg00);
9563 31254331 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9564 : }
9565 :
9566 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9567 : of the same precision, and X is an integer type not narrower than
9568 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9569 685363041 : if (INTEGRAL_TYPE_P (type)
9570 255913724 : && TREE_CODE (op0) == BIT_NOT_EXPR
9571 578763 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9572 578763 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9573 685744228 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9574 : {
9575 378772 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9576 452117 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9577 452115 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9578 313901 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9579 313901 : fold_convert_loc (loc, type, tem));
9580 : }
9581 :
9582 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9583 : type of X and Y (integer types only). */
9584 685049140 : if (INTEGRAL_TYPE_P (type)
9585 255599823 : && TREE_CODE (op0) == MULT_EXPR
9586 9239200 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9587 9218470 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9588 685108992 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9589 20945 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9590 : {
9591 : /* Be careful not to introduce new overflows. */
9592 59798 : tree mult_type;
9593 59798 : if (TYPE_OVERFLOW_WRAPS (type))
9594 : mult_type = type;
9595 : else
9596 2098 : mult_type = unsigned_type_for (type);
9597 :
9598 59798 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9599 : {
9600 119596 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9601 : fold_convert_loc (loc, mult_type,
9602 59798 : TREE_OPERAND (op0, 0)),
9603 : fold_convert_loc (loc, mult_type,
9604 59798 : TREE_OPERAND (op0, 1)));
9605 59798 : return fold_convert_loc (loc, type, tem);
9606 : }
9607 : }
9608 :
9609 : return NULL_TREE;
9610 :
9611 239910010 : case VIEW_CONVERT_EXPR:
9612 239910010 : if (TREE_CODE (op0) == MEM_REF)
9613 : {
9614 2756 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9615 22 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9616 2756 : tem = fold_build2_loc (loc, MEM_REF, type,
9617 2756 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9618 2756 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9619 2756 : return tem;
9620 : }
9621 :
9622 : return NULL_TREE;
9623 :
9624 4238870 : case NEGATE_EXPR:
9625 4238870 : tem = fold_negate_expr (loc, arg0);
9626 4238870 : if (tem)
9627 1692 : return fold_convert_loc (loc, type, tem);
9628 : return NULL_TREE;
9629 :
9630 3449391 : case ABS_EXPR:
9631 : /* Convert fabs((double)float) into (double)fabsf(float). */
9632 3449391 : if (TREE_CODE (arg0) == NOP_EXPR
9633 23799 : && TREE_CODE (type) == REAL_TYPE)
9634 : {
9635 23745 : tree targ0 = strip_float_extensions (arg0);
9636 23745 : if (targ0 != arg0)
9637 23541 : return fold_convert_loc (loc, type,
9638 : fold_build1_loc (loc, ABS_EXPR,
9639 23541 : TREE_TYPE (targ0),
9640 23541 : targ0));
9641 : }
9642 : return NULL_TREE;
9643 :
9644 2825201 : case BIT_NOT_EXPR:
9645 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9646 2825201 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9647 2826893 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9648 : fold_convert_loc (loc, type,
9649 1692 : TREE_OPERAND (arg0, 0)))))
9650 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9651 : fold_convert_loc (loc, type,
9652 28 : TREE_OPERAND (arg0, 1)));
9653 2825187 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9654 2826865 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9655 : fold_convert_loc (loc, type,
9656 1678 : TREE_OPERAND (arg0, 1)))))
9657 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9658 : fold_convert_loc (loc, type,
9659 46 : TREE_OPERAND (arg0, 0)), tem);
9660 :
9661 : return NULL_TREE;
9662 :
9663 51126077 : case TRUTH_NOT_EXPR:
9664 : /* Note that the operand of this must be an int
9665 : and its values must be 0 or 1.
9666 : ("true" is a fixed value perhaps depending on the language,
9667 : but we don't handle values other than 1 correctly yet.) */
9668 51126077 : tem = fold_truth_not_expr (loc, arg0);
9669 51126077 : if (!tem)
9670 : return NULL_TREE;
9671 34587254 : return fold_convert_loc (loc, type, tem);
9672 :
9673 69463788 : case INDIRECT_REF:
9674 : /* Fold *&X to X if X is an lvalue. */
9675 69463788 : if (TREE_CODE (op0) == ADDR_EXPR)
9676 : {
9677 8075 : tree op00 = TREE_OPERAND (op0, 0);
9678 8075 : if ((VAR_P (op00)
9679 : || TREE_CODE (op00) == PARM_DECL
9680 : || TREE_CODE (op00) == RESULT_DECL)
9681 6929 : && !TREE_READONLY (op00))
9682 6812 : return op00;
9683 : }
9684 : return NULL_TREE;
9685 :
9686 : default:
9687 : return NULL_TREE;
9688 : } /* switch (code) */
9689 : }
9690 :
9691 :
9692 : /* If the operation was a conversion do _not_ mark a resulting constant
9693 : with TREE_OVERFLOW if the original constant was not. These conversions
9694 : have implementation defined behavior and retaining the TREE_OVERFLOW
9695 : flag here would confuse later passes such as VRP. */
9696 : tree
9697 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9698 : tree type, tree op0)
9699 : {
9700 0 : tree res = fold_unary_loc (loc, code, type, op0);
9701 0 : if (res
9702 0 : && TREE_CODE (res) == INTEGER_CST
9703 0 : && TREE_CODE (op0) == INTEGER_CST
9704 0 : && CONVERT_EXPR_CODE_P (code))
9705 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9706 :
9707 0 : return res;
9708 : }
9709 :
9710 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9711 : operands OP0 and OP1. LOC is the location of the resulting expression.
9712 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9713 : Return the folded expression if folding is successful. Otherwise,
9714 : return NULL_TREE. */
9715 : static tree
9716 25731658 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9717 : tree arg0, tree arg1, tree op0, tree op1)
9718 : {
9719 25731658 : tree tem;
9720 :
9721 : /* We only do these simplifications if we are optimizing. */
9722 25731658 : if (!optimize)
9723 : return NULL_TREE;
9724 :
9725 : /* Check for things like (A || B) && (A || C). We can convert this
9726 : to A || (B && C). Note that either operator can be any of the four
9727 : truth and/or operations and the transformation will still be
9728 : valid. Also note that we only care about order for the
9729 : ANDIF and ORIF operators. If B contains side effects, this
9730 : might change the truth-value of A. */
9731 25236268 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9732 5894604 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9733 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9734 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9735 5894604 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9736 25298869 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9737 : {
9738 62087 : tree a00 = TREE_OPERAND (arg0, 0);
9739 62087 : tree a01 = TREE_OPERAND (arg0, 1);
9740 62087 : tree a10 = TREE_OPERAND (arg1, 0);
9741 62087 : tree a11 = TREE_OPERAND (arg1, 1);
9742 124174 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9743 62087 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9744 62087 : && (code == TRUTH_AND_EXPR
9745 23102 : || code == TRUTH_OR_EXPR));
9746 :
9747 62087 : if (operand_equal_p (a00, a10, 0))
9748 837 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9749 837 : fold_build2_loc (loc, code, type, a01, a11));
9750 61250 : else if (commutative && operand_equal_p (a00, a11, 0))
9751 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9752 0 : fold_build2_loc (loc, code, type, a01, a10));
9753 61250 : else if (commutative && operand_equal_p (a01, a10, 0))
9754 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9755 0 : fold_build2_loc (loc, code, type, a00, a11));
9756 :
9757 : /* This case if tricky because we must either have commutative
9758 : operators or else A10 must not have side-effects. */
9759 :
9760 61197 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9761 121919 : && operand_equal_p (a01, a11, 0))
9762 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9763 : fold_build2_loc (loc, code, type, a00, a10),
9764 43 : a01);
9765 : }
9766 :
9767 : /* See if we can build a range comparison. */
9768 25235388 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9769 : return tem;
9770 :
9771 24073053 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9772 24071077 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9773 : {
9774 40058 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9775 40058 : if (tem)
9776 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9777 : }
9778 :
9779 24073040 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9780 24061977 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9781 : {
9782 89571 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9783 89571 : if (tem)
9784 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9785 : }
9786 :
9787 : /* Check for the possibility of merging component references. If our
9788 : lhs is another similar operation, try to merge its rhs with our
9789 : rhs. Then try to merge our lhs and rhs. */
9790 24072949 : if (TREE_CODE (arg0) == code
9791 24916530 : && (tem = fold_truth_andor_1 (loc, code, type,
9792 843581 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9793 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9794 :
9795 24072864 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9796 : return tem;
9797 :
9798 24032805 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9799 24032805 : if (param_logical_op_non_short_circuit != -1)
9800 7729 : logical_op_non_short_circuit
9801 7729 : = param_logical_op_non_short_circuit;
9802 24032805 : if (logical_op_non_short_circuit
9803 24028895 : && !sanitize_coverage_p ()
9804 24032805 : && (code == TRUTH_AND_EXPR
9805 24028892 : || code == TRUTH_ANDIF_EXPR
9806 11095448 : || code == TRUTH_OR_EXPR
9807 11095448 : || code == TRUTH_ORIF_EXPR))
9808 : {
9809 24028892 : enum tree_code ncode, icode;
9810 :
9811 24028892 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9812 24028892 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9813 11095448 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9814 :
9815 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9816 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9817 : We don't want to pack more than two leafs to a non-IF AND/OR
9818 : expression.
9819 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9820 : equal to IF-CODE, then we don't want to add right-hand operand.
9821 : If the inner right-hand side of left-hand operand has
9822 : side-effects, or isn't simple, then we can't add to it,
9823 : as otherwise we might destroy if-sequence. */
9824 24028892 : if (TREE_CODE (arg0) == icode
9825 834625 : && simple_condition_p (arg1)
9826 : /* Needed for sequence points to handle trappings, and
9827 : side-effects. */
9828 24076703 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9829 : {
9830 40904 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9831 : arg1);
9832 40904 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9833 40904 : tem);
9834 : }
9835 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9836 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9837 23987988 : else if (TREE_CODE (arg1) == icode
9838 6566 : && simple_condition_p (arg0)
9839 : /* Needed for sequence points to handle trappings, and
9840 : side-effects. */
9841 23988953 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9842 : {
9843 39 : tem = fold_build2_loc (loc, ncode, type,
9844 39 : arg0, TREE_OPERAND (arg1, 0));
9845 39 : return fold_build2_loc (loc, icode, type, tem,
9846 78 : TREE_OPERAND (arg1, 1));
9847 : }
9848 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9849 : into (A OR B).
9850 : For sequence point consistency, we need to check for trapping,
9851 : and side-effects. */
9852 5317038 : else if (code == icode && simple_condition_p (arg0)
9853 24872502 : && simple_condition_p (arg1))
9854 439907 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9855 : }
9856 :
9857 : return NULL_TREE;
9858 : }
9859 :
9860 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9861 : by changing CODE to reduce the magnitude of constants involved in
9862 : ARG0 of the comparison.
9863 : Returns a canonicalized comparison tree if a simplification was
9864 : possible, otherwise returns NULL_TREE. */
9865 :
9866 : static tree
9867 181225430 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9868 : tree arg0, tree arg1)
9869 : {
9870 181225430 : enum tree_code code0 = TREE_CODE (arg0);
9871 181225430 : tree t, cst0 = NULL_TREE;
9872 181225430 : int sgn0;
9873 :
9874 : /* Match A +- CST code arg1. We can change this only if overflow
9875 : is undefined. */
9876 181225430 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9877 138436218 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9878 : /* In principle pointers also have undefined overflow behavior,
9879 : but that causes problems elsewhere. */
9880 68299363 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9881 68299363 : && (code0 == MINUS_EXPR
9882 68299363 : || code0 == PLUS_EXPR)
9883 2648544 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9884 : return NULL_TREE;
9885 :
9886 : /* Identify the constant in arg0 and its sign. */
9887 2179751 : cst0 = TREE_OPERAND (arg0, 1);
9888 2179751 : sgn0 = tree_int_cst_sgn (cst0);
9889 :
9890 : /* Overflowed constants and zero will cause problems. */
9891 2179751 : if (integer_zerop (cst0)
9892 2179751 : || TREE_OVERFLOW (cst0))
9893 : return NULL_TREE;
9894 :
9895 : /* See if we can reduce the magnitude of the constant in
9896 : arg0 by changing the comparison code. */
9897 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9898 2179751 : if (code == LT_EXPR
9899 1282924 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9900 : code = LE_EXPR;
9901 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9902 1993824 : else if (code == GT_EXPR
9903 599690 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9904 : code = GE_EXPR;
9905 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9906 1803802 : else if (code == LE_EXPR
9907 682795 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9908 : code = LT_EXPR;
9909 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9910 1560968 : else if (code == GE_EXPR
9911 488195 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9912 : code = GT_EXPR;
9913 : else
9914 : return NULL_TREE;
9915 :
9916 : /* Now build the constant reduced in magnitude. But not if that
9917 : would produce one outside of its types range. */
9918 1647700 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9919 1647700 : && ((sgn0 == 1
9920 432718 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9921 432718 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9922 823850 : || (sgn0 == -1
9923 391132 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9924 391132 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9925 : return NULL_TREE;
9926 :
9927 1256568 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9928 823850 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9929 823850 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9930 823850 : t = fold_convert (TREE_TYPE (arg1), t);
9931 :
9932 823850 : return fold_build2_loc (loc, code, type, t, arg1);
9933 : }
9934 :
9935 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9936 : overflow further. Try to decrease the magnitude of constants involved
9937 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9938 : and put sole constants at the second argument position.
9939 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9940 :
9941 : static tree
9942 91002614 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9943 : tree arg0, tree arg1)
9944 : {
9945 91002614 : tree t;
9946 :
9947 : /* Try canonicalization by simplifying arg0. */
9948 91002614 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9949 91002614 : if (t)
9950 : return t;
9951 :
9952 : /* Try canonicalization by simplifying arg1 using the swapped
9953 : comparison. */
9954 90222816 : code = swap_tree_comparison (code);
9955 90222816 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9956 90222816 : return t;
9957 : }
9958 :
9959 : /* Return a positive integer when the symbol DECL is known to have
9960 : a nonzero address, zero when it's known not to (e.g., it's a weak
9961 : symbol), and a negative integer when the symbol is not yet in the
9962 : symbol table and so whether or not its address is zero is unknown.
9963 : For function local objects always return positive integer. */
9964 : static int
9965 12432618 : maybe_nonzero_address (tree decl)
9966 : {
9967 12432618 : if (!DECL_P (decl))
9968 : return -1;
9969 :
9970 : /* Normally, don't do anything for variables and functions before symtab is
9971 : built; it is quite possible that DECL will be declared weak later.
9972 : But if folding_initializer, we need a constant answer now, so create
9973 : the symtab entry and prevent later weak declaration. */
9974 10201091 : if (decl_in_symtab_p (decl))
9975 : {
9976 4445796 : if (struct symtab_node *symbol
9977 4445796 : = (folding_initializer
9978 4445796 : ? symtab_node::get_create (decl)
9979 4428471 : : symtab_node::get (decl)))
9980 4426757 : return symbol->nonzero_address ();
9981 : }
9982 5755295 : else if (folding_cxx_constexpr)
9983 : /* Anything that doesn't go in the symtab has non-zero address. */
9984 : return 1;
9985 :
9986 : /* Function local objects are never NULL. */
9987 5618274 : if (DECL_CONTEXT (decl)
9988 5600717 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9989 11215524 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9990 5515488 : return 1;
9991 :
9992 : return -1;
9993 : }
9994 :
9995 : /* Subroutine of fold_binary. This routine performs all of the
9996 : transformations that are common to the equality/inequality
9997 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9998 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9999 : fold_binary should call fold_binary. Fold a comparison with
10000 : tree code CODE and type TYPE with operands OP0 and OP1. Return
10001 : the folded comparison or NULL_TREE. */
10002 :
10003 : static tree
10004 91073452 : fold_comparison (location_t loc, enum tree_code code, tree type,
10005 : tree op0, tree op1)
10006 : {
10007 91073452 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
10008 91073452 : tree arg0, arg1, tem;
10009 :
10010 91073452 : arg0 = op0;
10011 91073452 : arg1 = op1;
10012 :
10013 91073452 : STRIP_SIGN_NOPS (arg0);
10014 91073452 : STRIP_SIGN_NOPS (arg1);
10015 :
10016 : /* For comparisons of pointers we can decompose it to a compile time
10017 : comparison of the base objects and the offsets into the object.
10018 : This requires at least one operand being an ADDR_EXPR or a
10019 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10020 168687681 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
10021 91290767 : && (TREE_CODE (arg0) == ADDR_EXPR
10022 13624126 : || TREE_CODE (arg1) == ADDR_EXPR
10023 12098739 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10024 11330737 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
10025 : {
10026 2355843 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
10027 2355843 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
10028 2355843 : machine_mode mode;
10029 2355843 : int volatilep, reversep, unsignedp;
10030 2355843 : bool indirect_base0 = false, indirect_base1 = false;
10031 :
10032 : /* Get base and offset for the access. Strip ADDR_EXPR for
10033 : get_inner_reference, but put it back by stripping INDIRECT_REF
10034 : off the base object if possible. indirect_baseN will be true
10035 : if baseN is not an address but refers to the object itself. */
10036 2355843 : base0 = arg0;
10037 2355843 : if (TREE_CODE (arg0) == ADDR_EXPR)
10038 : {
10039 52412 : base0
10040 52412 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10041 : &bitsize, &bitpos0, &offset0, &mode,
10042 : &unsignedp, &reversep, &volatilep);
10043 52412 : if (INDIRECT_REF_P (base0))
10044 1979 : base0 = TREE_OPERAND (base0, 0);
10045 : else
10046 : indirect_base0 = true;
10047 : }
10048 2303431 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10049 : {
10050 829693 : base0 = TREE_OPERAND (arg0, 0);
10051 829693 : STRIP_SIGN_NOPS (base0);
10052 829693 : if (TREE_CODE (base0) == ADDR_EXPR)
10053 : {
10054 27435 : base0
10055 27435 : = get_inner_reference (TREE_OPERAND (base0, 0),
10056 : &bitsize, &bitpos0, &offset0, &mode,
10057 : &unsignedp, &reversep, &volatilep);
10058 27435 : if (INDIRECT_REF_P (base0))
10059 20 : base0 = TREE_OPERAND (base0, 0);
10060 : else
10061 : indirect_base0 = true;
10062 : }
10063 829693 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10064 829693 : offset0 = TREE_OPERAND (arg0, 1);
10065 : else
10066 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10067 : TREE_OPERAND (arg0, 1));
10068 829693 : if (poly_int_tree_p (offset0))
10069 : {
10070 663418 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10071 663418 : TYPE_PRECISION (sizetype));
10072 663418 : tem <<= LOG2_BITS_PER_UNIT;
10073 663418 : tem += bitpos0;
10074 663418 : if (tem.to_shwi (&bitpos0))
10075 663418 : offset0 = NULL_TREE;
10076 : }
10077 : }
10078 :
10079 2355843 : base1 = arg1;
10080 2355843 : if (TREE_CODE (arg1) == ADDR_EXPR)
10081 : {
10082 1554623 : base1
10083 1554623 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10084 : &bitsize, &bitpos1, &offset1, &mode,
10085 : &unsignedp, &reversep, &volatilep);
10086 1554623 : if (INDIRECT_REF_P (base1))
10087 69522 : base1 = TREE_OPERAND (base1, 0);
10088 : else
10089 : indirect_base1 = true;
10090 : }
10091 801220 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10092 : {
10093 85089 : base1 = TREE_OPERAND (arg1, 0);
10094 85089 : STRIP_SIGN_NOPS (base1);
10095 85089 : if (TREE_CODE (base1) == ADDR_EXPR)
10096 : {
10097 11604 : base1
10098 11604 : = get_inner_reference (TREE_OPERAND (base1, 0),
10099 : &bitsize, &bitpos1, &offset1, &mode,
10100 : &unsignedp, &reversep, &volatilep);
10101 11604 : if (INDIRECT_REF_P (base1))
10102 0 : base1 = TREE_OPERAND (base1, 0);
10103 : else
10104 : indirect_base1 = true;
10105 : }
10106 85089 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10107 85065 : offset1 = TREE_OPERAND (arg1, 1);
10108 : else
10109 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10110 : TREE_OPERAND (arg1, 1));
10111 85089 : if (poly_int_tree_p (offset1))
10112 : {
10113 74329 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10114 74329 : TYPE_PRECISION (sizetype));
10115 74329 : tem <<= LOG2_BITS_PER_UNIT;
10116 74329 : tem += bitpos1;
10117 74329 : if (tem.to_shwi (&bitpos1))
10118 74329 : offset1 = NULL_TREE;
10119 : }
10120 : }
10121 :
10122 : /* If we have equivalent bases we might be able to simplify. */
10123 2355843 : if (indirect_base0 == indirect_base1
10124 3172904 : && operand_equal_p (base0, base1,
10125 : indirect_base0 ? OEP_ADDRESS_OF : 0))
10126 : {
10127 : /* We can fold this expression to a constant if the non-constant
10128 : offset parts are equal. */
10129 17090 : if ((offset0 == offset1
10130 6745 : || (offset0 && offset1
10131 2742 : && operand_equal_p (offset0, offset1, 0)))
10132 17090 : && (equality_code
10133 10301 : || (indirect_base0
10134 6392 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10135 3909 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10136 : {
10137 10290 : switch (code)
10138 : {
10139 35 : case EQ_EXPR:
10140 35 : if (known_eq (bitpos0, bitpos1))
10141 47777 : return constant_boolean_node (true, type);
10142 21 : if (known_ne (bitpos0, bitpos1))
10143 21 : return constant_boolean_node (false, type);
10144 : break;
10145 9 : case NE_EXPR:
10146 9 : if (known_ne (bitpos0, bitpos1))
10147 7 : return constant_boolean_node (true, type);
10148 2 : if (known_eq (bitpos0, bitpos1))
10149 2 : return constant_boolean_node (false, type);
10150 : break;
10151 2335 : case LT_EXPR:
10152 2335 : if (known_lt (bitpos0, bitpos1))
10153 2191 : return constant_boolean_node (true, type);
10154 144 : if (known_ge (bitpos0, bitpos1))
10155 144 : return constant_boolean_node (false, type);
10156 : break;
10157 1853 : case LE_EXPR:
10158 1853 : if (known_le (bitpos0, bitpos1))
10159 171 : return constant_boolean_node (true, type);
10160 1682 : if (known_gt (bitpos0, bitpos1))
10161 1682 : return constant_boolean_node (false, type);
10162 : break;
10163 3667 : case GE_EXPR:
10164 3667 : if (known_ge (bitpos0, bitpos1))
10165 1655 : return constant_boolean_node (true, type);
10166 2012 : if (known_lt (bitpos0, bitpos1))
10167 2012 : return constant_boolean_node (false, type);
10168 : break;
10169 2391 : case GT_EXPR:
10170 2391 : if (known_gt (bitpos0, bitpos1))
10171 2332 : return constant_boolean_node (true, type);
10172 59 : if (known_le (bitpos0, bitpos1))
10173 59 : return constant_boolean_node (false, type);
10174 : break;
10175 : default:;
10176 : }
10177 : }
10178 : /* We can simplify the comparison to a comparison of the variable
10179 : offset parts if the constant offset parts are equal.
10180 : Be careful to use signed sizetype here because otherwise we
10181 : mess with array offsets in the wrong way. This is possible
10182 : because pointer arithmetic is restricted to retain within an
10183 : object and overflow on pointer differences is undefined as of
10184 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10185 6800 : else if (known_eq (bitpos0, bitpos1)
10186 6800 : && (equality_code
10187 5313 : || (indirect_base0
10188 285 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10189 5028 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10190 : {
10191 : /* By converting to signed sizetype we cover middle-end pointer
10192 : arithmetic which operates on unsigned pointer types of size
10193 : type size and ARRAY_REF offsets which are properly sign or
10194 : zero extended from their type in case it is narrower than
10195 : sizetype. */
10196 5422 : if (offset0 == NULL_TREE)
10197 0 : offset0 = build_int_cst (ssizetype, 0);
10198 : else
10199 5422 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10200 5422 : if (offset1 == NULL_TREE)
10201 2712 : offset1 = build_int_cst (ssizetype, 0);
10202 : else
10203 2710 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10204 :
10205 5422 : return fold_build2_loc (loc, code, type, offset0, offset1);
10206 : }
10207 : }
10208 : /* For equal offsets we can simplify to a comparison of the
10209 : base addresses. */
10210 2338753 : else if (known_eq (bitpos0, bitpos1)
10211 54680 : && (indirect_base0
10212 997104 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10213 15299 : && (indirect_base1
10214 189201 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10215 2564326 : && ((offset0 == offset1)
10216 4413 : || (offset0 && offset1
10217 4143 : && operand_equal_p (offset0, offset1, 0))))
10218 : {
10219 31960 : if (indirect_base0)
10220 2830 : base0 = build_fold_addr_expr_loc (loc, base0);
10221 31960 : if (indirect_base1)
10222 4429 : base1 = build_fold_addr_expr_loc (loc, base1);
10223 31960 : return fold_build2_loc (loc, code, type, base0, base1);
10224 : }
10225 : /* Comparison between an ordinary (non-weak) symbol and a null
10226 : pointer can be eliminated since such symbols must have a non
10227 : null address. In C, relational expressions between pointers
10228 : to objects and null pointers are undefined. The results
10229 : below follow the C++ rules with the additional property that
10230 : every object pointer compares greater than a null pointer.
10231 : */
10232 2306793 : else if (((DECL_P (base0)
10233 253188 : && maybe_nonzero_address (base0) > 0
10234 : /* Avoid folding references to struct members at offset 0 to
10235 : prevent tests like '&ptr->firstmember == 0' from getting
10236 : eliminated. When ptr is null, although the -> expression
10237 : is strictly speaking invalid, GCC retains it as a matter
10238 : of QoI. See PR c/44555. */
10239 238267 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10240 2291263 : || CONSTANT_CLASS_P (base0))
10241 20314 : && indirect_base0
10242 : /* The caller guarantees that when one of the arguments is
10243 : constant (i.e., null in this case) it is second. */
10244 2324557 : && integer_zerop (arg1))
10245 : {
10246 105 : switch (code)
10247 : {
10248 24 : case EQ_EXPR:
10249 24 : case LE_EXPR:
10250 24 : case LT_EXPR:
10251 24 : return constant_boolean_node (false, type);
10252 81 : case GE_EXPR:
10253 81 : case GT_EXPR:
10254 81 : case NE_EXPR:
10255 81 : return constant_boolean_node (true, type);
10256 0 : default:
10257 0 : gcc_unreachable ();
10258 : }
10259 : }
10260 : }
10261 :
10262 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10263 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10264 : the resulting offset is smaller in absolute value than the
10265 : original one and has the same sign. */
10266 179267883 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10267 139261778 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10268 34562645 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10269 2385316 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10270 1970923 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10271 1970923 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10272 160832074 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10273 173072 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10274 : {
10275 173072 : tree const1 = TREE_OPERAND (arg0, 1);
10276 173072 : tree const2 = TREE_OPERAND (arg1, 1);
10277 173072 : tree variable1 = TREE_OPERAND (arg0, 0);
10278 173072 : tree variable2 = TREE_OPERAND (arg1, 0);
10279 173072 : tree cst;
10280 :
10281 : /* Put the constant on the side where it doesn't overflow and is
10282 : of lower absolute value and of same sign than before. */
10283 173073 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10284 : ? MINUS_EXPR : PLUS_EXPR,
10285 : const2, const1);
10286 173072 : if (!TREE_OVERFLOW (cst)
10287 173048 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10288 196133 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10289 5985 : return fold_build2_loc (loc, code, type,
10290 : variable1,
10291 5985 : fold_build2_loc (loc, TREE_CODE (arg1),
10292 5985 : TREE_TYPE (arg1),
10293 5985 : variable2, cst));
10294 :
10295 167088 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10296 : ? MINUS_EXPR : PLUS_EXPR,
10297 : const1, const2);
10298 167087 : if (!TREE_OVERFLOW (cst)
10299 167063 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10300 184163 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10301 17076 : return fold_build2_loc (loc, code, type,
10302 17076 : fold_build2_loc (loc, TREE_CODE (arg0),
10303 17076 : TREE_TYPE (arg0),
10304 : variable1, cst),
10305 17076 : variable2);
10306 : }
10307 :
10308 91002614 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10309 91002614 : if (tem)
10310 : return tem;
10311 :
10312 : /* If we are comparing an expression that just has comparisons
10313 : of two integer values, arithmetic expressions of those comparisons,
10314 : and constants, we can simplify it. There are only three cases
10315 : to check: the two values can either be equal, the first can be
10316 : greater, or the second can be greater. Fold the expression for
10317 : those three values. Since each value must be 0 or 1, we have
10318 : eight possibilities, each of which corresponds to the constant 0
10319 : or 1 or one of the six possible comparisons.
10320 :
10321 : This handles common cases like (a > b) == 0 but also handles
10322 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10323 : occur in macroized code. */
10324 :
10325 90178764 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10326 : {
10327 55101992 : tree cval1 = 0, cval2 = 0;
10328 :
10329 55101992 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10330 : /* Don't handle degenerate cases here; they should already
10331 : have been handled anyway. */
10332 617674 : && cval1 != 0 && cval2 != 0
10333 616382 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10334 616382 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10335 616376 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10336 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10337 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10338 55102046 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10339 54 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10340 : {
10341 54 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10342 54 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10343 :
10344 : /* We can't just pass T to eval_subst in case cval1 or cval2
10345 : was the same as ARG1. */
10346 :
10347 54 : tree high_result
10348 54 : = fold_build2_loc (loc, code, type,
10349 : eval_subst (loc, arg0, cval1, maxval,
10350 : cval2, minval),
10351 : arg1);
10352 54 : tree equal_result
10353 54 : = fold_build2_loc (loc, code, type,
10354 : eval_subst (loc, arg0, cval1, maxval,
10355 : cval2, maxval),
10356 : arg1);
10357 54 : tree low_result
10358 54 : = fold_build2_loc (loc, code, type,
10359 : eval_subst (loc, arg0, cval1, minval,
10360 : cval2, maxval),
10361 : arg1);
10362 :
10363 : /* All three of these results should be 0 or 1. Confirm they are.
10364 : Then use those values to select the proper code to use. */
10365 :
10366 54 : if (TREE_CODE (high_result) == INTEGER_CST
10367 54 : && TREE_CODE (equal_result) == INTEGER_CST
10368 44 : && TREE_CODE (low_result) == INTEGER_CST)
10369 : {
10370 : /* Make a 3-bit mask with the high-order bit being the
10371 : value for `>', the next for '=', and the low for '<'. */
10372 44 : switch ((integer_onep (high_result) * 4)
10373 44 : + (integer_onep (equal_result) * 2)
10374 44 : + integer_onep (low_result))
10375 : {
10376 21 : case 0:
10377 : /* Always false. */
10378 44 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10379 : case 1:
10380 : code = LT_EXPR;
10381 : break;
10382 2 : case 2:
10383 2 : code = EQ_EXPR;
10384 2 : break;
10385 0 : case 3:
10386 0 : code = LE_EXPR;
10387 0 : break;
10388 0 : case 4:
10389 0 : code = GT_EXPR;
10390 0 : break;
10391 5 : case 5:
10392 5 : code = NE_EXPR;
10393 5 : break;
10394 0 : case 6:
10395 0 : code = GE_EXPR;
10396 0 : break;
10397 16 : case 7:
10398 : /* Always true. */
10399 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10400 : }
10401 :
10402 7 : return fold_build2_loc (loc, code, type, cval1, cval2);
10403 : }
10404 : }
10405 : }
10406 :
10407 : return NULL_TREE;
10408 : }
10409 :
10410 :
10411 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10412 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10413 : argument EXPR represents the expression "z" of type TYPE. */
10414 :
10415 : static tree
10416 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10417 : {
10418 2 : tree itype = TREE_TYPE (type);
10419 2 : tree rpart, ipart, tem;
10420 :
10421 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10422 : {
10423 0 : rpart = TREE_OPERAND (expr, 0);
10424 0 : ipart = TREE_OPERAND (expr, 1);
10425 : }
10426 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10427 : {
10428 0 : rpart = TREE_REALPART (expr);
10429 0 : ipart = TREE_IMAGPART (expr);
10430 : }
10431 : else
10432 : {
10433 2 : expr = save_expr (expr);
10434 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10435 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10436 : }
10437 :
10438 2 : rpart = save_expr (rpart);
10439 2 : ipart = save_expr (ipart);
10440 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10441 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10442 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10443 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10444 2 : build_zero_cst (itype));
10445 : }
10446 :
10447 :
10448 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10449 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10450 : true if successful. */
10451 :
10452 : static bool
10453 31632 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10454 : {
10455 31632 : unsigned HOST_WIDE_INT i, nunits;
10456 :
10457 31632 : if (TREE_CODE (arg) == VECTOR_CST
10458 31632 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10459 : {
10460 2200 : for (i = 0; i < nunits; ++i)
10461 1734 : elts[i] = VECTOR_CST_ELT (arg, i);
10462 : }
10463 31166 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10464 : {
10465 : constructor_elt *elt;
10466 :
10467 56665 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10468 46255 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10469 31632 : return false;
10470 : else
10471 20132 : elts[i] = elt->value;
10472 : }
10473 : else
10474 : return false;
10475 5891 : for (; i < nelts; i++)
10476 764 : elts[i]
10477 382 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10478 : return true;
10479 : }
10480 :
10481 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10482 : mask for VLA vec_perm folding.
10483 : REASON if specified, will contain the reason why SEL is not suitable.
10484 : Used only for debugging and unit-testing. */
10485 :
10486 : static bool
10487 30832 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10488 : const vec_perm_indices &sel,
10489 : const char **reason = NULL)
10490 : {
10491 30832 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10492 30832 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10493 :
10494 61664 : if (!(pow2p_hwi (sel_npatterns)
10495 30832 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10496 30832 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10497 : {
10498 0 : if (reason)
10499 0 : *reason = "npatterns is not power of 2";
10500 : return false;
10501 : }
10502 :
10503 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10504 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10505 30832 : poly_uint64 esel;
10506 30832 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10507 : {
10508 0 : if (reason)
10509 0 : *reason = "sel.length is not multiple of sel_npatterns";
10510 : return false;
10511 : }
10512 :
10513 30832 : if (sel_nelts_per_pattern < 3)
10514 : return true;
10515 :
10516 6366 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10517 : {
10518 4745 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10519 4745 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10520 4745 : HOST_WIDE_INT step;
10521 4745 : if (!poly_int64 (a2 - a1).is_constant (&step))
10522 : {
10523 : if (reason)
10524 : *reason = "step is not constant";
10525 30832 : return false;
10526 : }
10527 : // FIXME: Punt on step < 0 for now, revisit later.
10528 4745 : if (step < 0)
10529 : return false;
10530 4681 : if (step == 0)
10531 0 : continue;
10532 :
10533 4681 : if (!pow2p_hwi (step))
10534 : {
10535 0 : if (reason)
10536 0 : *reason = "step is not power of 2";
10537 : return false;
10538 : }
10539 :
10540 : /* Ensure that stepped sequence of the pattern selects elements
10541 : only from the same input vector. */
10542 4681 : uint64_t q1, qe;
10543 4681 : poly_uint64 r1, re;
10544 4681 : poly_uint64 ae = a1 + (esel - 2) * step;
10545 4681 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10546 :
10547 4681 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10548 4681 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10549 : && q1 == qe))
10550 : {
10551 445 : if (reason)
10552 0 : *reason = "crossed input vectors";
10553 : return false;
10554 : }
10555 :
10556 : /* Ensure that the stepped sequence always selects from the same
10557 : input pattern. */
10558 4236 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10559 4236 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10560 :
10561 4236 : if (!multiple_p (step, arg_npatterns))
10562 : {
10563 628 : if (reason)
10564 0 : *reason = "step is not multiple of npatterns";
10565 : return false;
10566 : }
10567 :
10568 : /* If a1 chooses base element from arg, ensure that it's a natural
10569 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10570 : to preserve arg's encoding. */
10571 :
10572 3608 : if (maybe_lt (r1, arg_npatterns))
10573 : {
10574 44 : unsigned HOST_WIDE_INT index;
10575 44 : if (!r1.is_constant (&index))
10576 30832 : return false;
10577 :
10578 44 : tree arg_elem0 = vector_cst_elt (arg, index);
10579 44 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10580 44 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10581 :
10582 44 : tree step1, step2;
10583 44 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10584 44 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10585 88 : || !operand_equal_p (step1, step2, 0))
10586 : {
10587 2 : if (reason)
10588 0 : *reason = "not a natural stepped sequence";
10589 : return false;
10590 : }
10591 : }
10592 : }
10593 :
10594 : return true;
10595 : }
10596 :
10597 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10598 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10599 : REASON has same purpose as described in
10600 : valid_mask_for_fold_vec_perm_cst_p. */
10601 :
10602 : static tree
10603 30832 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10604 : const char **reason = NULL)
10605 : {
10606 30832 : unsigned res_npatterns, res_nelts_per_pattern;
10607 30832 : unsigned HOST_WIDE_INT res_nelts;
10608 :
10609 : /* First try to implement the fold in a VLA-friendly way.
10610 :
10611 : (1) If the selector is simply a duplication of N elements, the
10612 : result is likewise a duplication of N elements.
10613 :
10614 : (2) If the selector is N elements followed by a duplication
10615 : of N elements, the result is too.
10616 :
10617 : (3) If the selector is N elements followed by an interleaving
10618 : of N linear series, the situation is more complex.
10619 :
10620 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10621 : can handle this case. If we can, then each of the N linear
10622 : series either (a) selects the same element each time or
10623 : (b) selects a linear series from one of the input patterns.
10624 :
10625 : If (b) holds for one of the linear series, the result
10626 : will contain a linear series, and so the result will have
10627 : the same shape as the selector. If (a) holds for all of
10628 : the linear series, the result will be the same as (2) above.
10629 :
10630 : (b) can only hold if one of the input patterns has a
10631 : stepped encoding. */
10632 :
10633 30832 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10634 : {
10635 29693 : res_npatterns = sel.encoding ().npatterns ();
10636 29693 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10637 29693 : if (res_nelts_per_pattern == 3
10638 1621 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10639 30769 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10640 : res_nelts_per_pattern = 2;
10641 29693 : res_nelts = res_npatterns * res_nelts_per_pattern;
10642 : }
10643 1139 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10644 : {
10645 1139 : res_npatterns = res_nelts;
10646 1139 : res_nelts_per_pattern = 1;
10647 : }
10648 : else
10649 : return NULL_TREE;
10650 :
10651 30832 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10652 185612 : for (unsigned i = 0; i < res_nelts; i++)
10653 : {
10654 123948 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10655 123948 : uint64_t q;
10656 123948 : poly_uint64 r;
10657 123948 : unsigned HOST_WIDE_INT index;
10658 :
10659 : /* Punt if sel[i] /trunc_div len cannot be determined,
10660 : because the input vector to be chosen will depend on
10661 : runtime vector length.
10662 : For example if len == 4 + 4x, and sel[i] == 4,
10663 : If len at runtime equals 4, we choose arg1[0].
10664 : For any other value of len > 4 at runtime, we choose arg0[4].
10665 : which makes the element choice dependent on runtime vector length. */
10666 123948 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10667 : {
10668 : if (reason)
10669 : *reason = "cannot divide selector element by arg len";
10670 : return NULL_TREE;
10671 : }
10672 :
10673 : /* sel[i] % len will give the index of element in the chosen input
10674 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10675 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10676 123948 : if (!r.is_constant (&index))
10677 : {
10678 : if (reason)
10679 : *reason = "remainder is not constant";
10680 : return NULL_TREE;
10681 : }
10682 :
10683 123948 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10684 123948 : tree elem = vector_cst_elt (arg, index);
10685 123948 : out_elts.quick_push (elem);
10686 : }
10687 :
10688 30832 : return out_elts.build ();
10689 30832 : }
10690 :
10691 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10692 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10693 : NULL_TREE otherwise. */
10694 :
10695 : tree
10696 70272 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10697 : {
10698 70272 : unsigned int i;
10699 70272 : unsigned HOST_WIDE_INT nelts;
10700 :
10701 70272 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10702 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10703 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10704 :
10705 70272 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10706 70272 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10707 : return NULL_TREE;
10708 :
10709 59693 : if (TREE_CODE (arg0) == VECTOR_CST
10710 31106 : && TREE_CODE (arg1) == VECTOR_CST)
10711 30832 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10712 :
10713 : /* For fall back case, we want to ensure we have VLS vectors
10714 : with equal length. */
10715 28861 : if (!sel.length ().is_constant (&nelts)
10716 28861 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10717 0 : return NULL_TREE;
10718 :
10719 28861 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10720 28861 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10721 28861 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10722 : return NULL_TREE;
10723 :
10724 2738 : vec<constructor_elt, va_gc> *v;
10725 2738 : vec_alloc (v, nelts);
10726 16468 : for (i = 0; i < nelts; i++)
10727 : {
10728 10992 : HOST_WIDE_INT index;
10729 10992 : if (!sel[i].is_constant (&index))
10730 : return NULL_TREE;
10731 10992 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10732 : }
10733 2738 : return build_constructor (type, v);
10734 : }
10735 :
10736 : /* Try to fold a pointer difference of type TYPE two address expressions of
10737 : array references AREF0 and AREF1 using location LOC. Return a
10738 : simplified expression for the difference or NULL_TREE. */
10739 :
10740 : static tree
10741 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10742 : tree aref0, tree aref1,
10743 : bool use_pointer_diff)
10744 : {
10745 39 : tree base0 = TREE_OPERAND (aref0, 0);
10746 39 : tree base1 = TREE_OPERAND (aref1, 0);
10747 39 : tree base_offset = build_int_cst (type, 0);
10748 :
10749 : /* If the bases are array references as well, recurse. If the bases
10750 : are pointer indirections compute the difference of the pointers.
10751 : If the bases are equal, we are set. */
10752 39 : if ((TREE_CODE (base0) == ARRAY_REF
10753 1 : && TREE_CODE (base1) == ARRAY_REF
10754 1 : && (base_offset
10755 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10756 : use_pointer_diff)))
10757 38 : || (INDIRECT_REF_P (base0)
10758 7 : && INDIRECT_REF_P (base1)
10759 7 : && (base_offset
10760 : = use_pointer_diff
10761 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10762 1 : TREE_OPERAND (base0, 0),
10763 1 : TREE_OPERAND (base1, 0))
10764 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10765 6 : fold_convert (type,
10766 : TREE_OPERAND (base0, 0)),
10767 6 : fold_convert (type,
10768 : TREE_OPERAND (base1, 0)))))
10769 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10770 : {
10771 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10772 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10773 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10774 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10775 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10776 : base_offset,
10777 : fold_build2_loc (loc, MULT_EXPR, type,
10778 15 : diff, esz));
10779 : }
10780 : return NULL_TREE;
10781 : }
10782 :
10783 : /* If the real or vector real constant CST of type TYPE has an exact
10784 : inverse, return it, else return NULL. */
10785 :
10786 : tree
10787 1177233 : exact_inverse (tree type, tree cst)
10788 : {
10789 1177233 : REAL_VALUE_TYPE r;
10790 1177233 : tree unit_type;
10791 1177233 : machine_mode mode;
10792 :
10793 1177233 : switch (TREE_CODE (cst))
10794 : {
10795 1176558 : case REAL_CST:
10796 1176558 : r = TREE_REAL_CST (cst);
10797 :
10798 1176558 : if (exact_real_inverse (TYPE_MODE (type), &r))
10799 331435 : return build_real (type, r);
10800 :
10801 : return NULL_TREE;
10802 :
10803 675 : case VECTOR_CST:
10804 675 : {
10805 675 : unit_type = TREE_TYPE (type);
10806 675 : mode = TYPE_MODE (unit_type);
10807 :
10808 675 : tree_vector_builder elts;
10809 675 : if (!elts.new_unary_operation (type, cst, false))
10810 : return NULL_TREE;
10811 675 : unsigned int count = elts.encoded_nelts ();
10812 735 : for (unsigned int i = 0; i < count; ++i)
10813 : {
10814 675 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10815 675 : if (!exact_real_inverse (mode, &r))
10816 : return NULL_TREE;
10817 60 : elts.quick_push (build_real (unit_type, r));
10818 : }
10819 :
10820 60 : return elts.build ();
10821 675 : }
10822 :
10823 : default:
10824 : return NULL_TREE;
10825 : }
10826 : }
10827 :
10828 : /* Mask out the tz least significant bits of X of type TYPE where
10829 : tz is the number of trailing zeroes in Y. */
10830 : static wide_int
10831 161592 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10832 : {
10833 161592 : int tz = wi::ctz (y);
10834 161592 : if (tz > 0)
10835 6985 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10836 154607 : return x;
10837 : }
10838 :
10839 : /* Return true when T is an address and is known to be nonzero.
10840 : For floating point we further ensure that T is not denormal.
10841 : Similar logic is present in nonzero_address in rtlanal.h. */
10842 :
10843 : bool
10844 151153234 : tree_expr_nonzero_p (tree t)
10845 : {
10846 151509106 : tree type = TREE_TYPE (t);
10847 151509106 : enum tree_code code;
10848 :
10849 : /* Doing something useful for floating point would need more work. */
10850 151509106 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10851 : return false;
10852 :
10853 151381239 : code = TREE_CODE (t);
10854 151381239 : switch (TREE_CODE_CLASS (code))
10855 : {
10856 984391 : case tcc_unary:
10857 984391 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10858 2911629 : case tcc_binary:
10859 2911629 : case tcc_comparison:
10860 2911629 : return tree_binary_nonzero_p (code, type,
10861 2911629 : TREE_OPERAND (t, 0),
10862 5823258 : TREE_OPERAND (t, 1));
10863 13227944 : case tcc_constant:
10864 13227944 : case tcc_declaration:
10865 13227944 : case tcc_reference:
10866 13227944 : return tree_single_nonzero_p (t);
10867 :
10868 134257275 : default:
10869 134257275 : break;
10870 : }
10871 :
10872 134257275 : switch (code)
10873 : {
10874 615956 : case TRUTH_NOT_EXPR:
10875 615956 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10876 :
10877 71080 : case TRUTH_AND_EXPR:
10878 71080 : case TRUTH_OR_EXPR:
10879 71080 : case TRUTH_XOR_EXPR:
10880 71080 : return tree_binary_nonzero_p (code, type,
10881 71080 : TREE_OPERAND (t, 0),
10882 142160 : TREE_OPERAND (t, 1));
10883 :
10884 130077971 : case COND_EXPR:
10885 130077971 : case CONSTRUCTOR:
10886 130077971 : case OBJ_TYPE_REF:
10887 130077971 : case ADDR_EXPR:
10888 130077971 : case WITH_SIZE_EXPR:
10889 130077971 : case SSA_NAME:
10890 130077971 : return tree_single_nonzero_p (t);
10891 :
10892 85036 : case COMPOUND_EXPR:
10893 85036 : case MODIFY_EXPR:
10894 85036 : case BIND_EXPR:
10895 85036 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10896 :
10897 270836 : case SAVE_EXPR:
10898 270836 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10899 :
10900 3079943 : case CALL_EXPR:
10901 3079943 : {
10902 3079943 : tree fndecl = get_callee_fndecl (t);
10903 3079943 : if (!fndecl) return false;
10904 3077961 : if (flag_delete_null_pointer_checks && !flag_check_new
10905 3077961 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10906 3078679 : && !TREE_NOTHROW (fndecl))
10907 : return true;
10908 3078679 : if (flag_delete_null_pointer_checks
10909 6156640 : && lookup_attribute ("returns_nonnull",
10910 3077961 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10911 : return true;
10912 3078671 : return alloca_call_p (t);
10913 : }
10914 :
10915 : default:
10916 : break;
10917 : }
10918 : return false;
10919 : }
10920 :
10921 : /* Return true if T is known not to be equal to an integer W.
10922 : If STMT is specified, the check is if T on STMT is not equal
10923 : to W. */
10924 :
10925 : bool
10926 104096103 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10927 : {
10928 104096103 : int_range_max vr;
10929 104096103 : switch (TREE_CODE (t))
10930 : {
10931 2464739 : case INTEGER_CST:
10932 2464739 : return wi::to_wide (t) != w;
10933 :
10934 101193368 : case SSA_NAME:
10935 101193368 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10936 : return false;
10937 :
10938 202386736 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10939 101193368 : if (!vr.undefined_p () && !vr.contains_p (w))
10940 : return true;
10941 : /* If T has some known zero bits and W has any of those bits set,
10942 : then T is known not to be equal to W. */
10943 101050662 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10944 202100888 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10945 1 : return true;
10946 : return false;
10947 :
10948 : default:
10949 : return false;
10950 : }
10951 104096103 : }
10952 :
10953 : /* Fold a binary expression of code CODE and type TYPE with operands
10954 : OP0 and OP1. LOC is the location of the resulting expression.
10955 : Return the folded expression if folding is successful. Otherwise,
10956 : return NULL_TREE. */
10957 :
10958 : tree
10959 975376913 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10960 : tree op0, tree op1)
10961 : {
10962 975376913 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10963 975376913 : tree arg0, arg1, tem;
10964 975376913 : tree t1 = NULL_TREE;
10965 975376913 : unsigned int prec;
10966 :
10967 975376913 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10968 : && TREE_CODE_LENGTH (code) == 2
10969 : && op0 != NULL_TREE
10970 : && op1 != NULL_TREE);
10971 :
10972 975376913 : arg0 = op0;
10973 975376913 : arg1 = op1;
10974 :
10975 : /* Strip any conversions that don't change the mode. This is
10976 : safe for every expression, except for a comparison expression
10977 : because its signedness is derived from its operands. So, in
10978 : the latter case, only strip conversions that don't change the
10979 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10980 : preserved.
10981 :
10982 : Note that this is done as an internal manipulation within the
10983 : constant folder, in order to find the simplest representation
10984 : of the arguments so that their form can be studied. In any
10985 : cases, the appropriate type conversions should be put back in
10986 : the tree that will get out of the constant folder. */
10987 :
10988 975376913 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10989 : {
10990 214821499 : STRIP_SIGN_NOPS (arg0);
10991 214821499 : STRIP_SIGN_NOPS (arg1);
10992 : }
10993 : else
10994 : {
10995 760555414 : STRIP_NOPS (arg0);
10996 760555414 : STRIP_NOPS (arg1);
10997 : }
10998 :
10999 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11000 : constant but we can't do arithmetic on them. */
11001 975376913 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11002 : {
11003 274647829 : tem = const_binop (code, type, arg0, arg1);
11004 274647829 : if (tem != NULL_TREE)
11005 : {
11006 272116580 : if (TREE_TYPE (tem) != type)
11007 4219909 : tem = fold_convert_loc (loc, type, tem);
11008 : return tem;
11009 : }
11010 : }
11011 :
11012 : /* If this is a commutative operation, and ARG0 is a constant, move it
11013 : to ARG1 to reduce the number of tests below. */
11014 703260333 : if (commutative_tree_code (code)
11015 703260333 : && tree_swap_operands_p (arg0, arg1))
11016 33552584 : return fold_build2_loc (loc, code, type, op1, op0);
11017 :
11018 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11019 : to ARG1 to reduce the number of tests below. */
11020 669707749 : if (kind == tcc_comparison
11021 669707749 : && tree_swap_operands_p (arg0, arg1))
11022 8480080 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11023 :
11024 661227669 : tem = generic_simplify (loc, code, type, op0, op1);
11025 661227669 : if (tem)
11026 : return tem;
11027 :
11028 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11029 :
11030 : First check for cases where an arithmetic operation is applied to a
11031 : compound, conditional, or comparison operation. Push the arithmetic
11032 : operation inside the compound or conditional to see if any folding
11033 : can then be done. Convert comparison to conditional for this purpose.
11034 : The also optimizes non-constant cases that used to be done in
11035 : expand_expr.
11036 :
11037 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11038 : one of the operands is a comparison and the other is a comparison, a
11039 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11040 : code below would make the expression more complex. Change it to a
11041 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11042 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11043 :
11044 558390312 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11045 : || code == EQ_EXPR || code == NE_EXPR)
11046 58957269 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11047 58366144 : && ((truth_value_p (TREE_CODE (arg0))
11048 1250678 : && (truth_value_p (TREE_CODE (arg1))
11049 930250 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11050 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11051 58045700 : || (truth_value_p (TREE_CODE (arg1))
11052 7193 : && (truth_value_p (TREE_CODE (arg0))
11053 7193 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11054 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11055 : {
11056 400779 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11057 80321 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11058 : : TRUTH_XOR_EXPR,
11059 : boolean_type_node,
11060 : fold_convert_loc (loc, boolean_type_node, arg0),
11061 : fold_convert_loc (loc, boolean_type_node, arg1));
11062 :
11063 320458 : if (code == EQ_EXPR)
11064 72866 : tem = invert_truthvalue_loc (loc, tem);
11065 :
11066 320458 : return fold_convert_loc (loc, type, tem);
11067 : }
11068 :
11069 558069854 : if (TREE_CODE_CLASS (code) == tcc_binary
11070 305294103 : || TREE_CODE_CLASS (code) == tcc_comparison)
11071 : {
11072 350279257 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11073 : {
11074 82990 : tem = fold_build2_loc (loc, code, type,
11075 82990 : fold_convert_loc (loc, TREE_TYPE (op0),
11076 82990 : TREE_OPERAND (arg0, 1)), op1);
11077 82990 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11078 82990 : tem);
11079 : }
11080 350196267 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11081 : {
11082 3171 : tem = fold_build2_loc (loc, code, type, op0,
11083 3171 : fold_convert_loc (loc, TREE_TYPE (op1),
11084 3171 : TREE_OPERAND (arg1, 1)));
11085 3171 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11086 3171 : tem);
11087 : }
11088 :
11089 350193096 : if (TREE_CODE (arg0) == COND_EXPR
11090 349802071 : || TREE_CODE (arg0) == VEC_COND_EXPR
11091 349798721 : || COMPARISON_CLASS_P (arg0))
11092 : {
11093 737803 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11094 : arg0, arg1,
11095 : /*cond_first_p=*/1);
11096 737803 : if (tem != NULL_TREE)
11097 : return tem;
11098 : }
11099 :
11100 349708283 : if (TREE_CODE (arg1) == COND_EXPR
11101 349496333 : || TREE_CODE (arg1) == VEC_COND_EXPR
11102 349495883 : || COMPARISON_CLASS_P (arg1))
11103 : {
11104 225379 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11105 : arg1, arg0,
11106 : /*cond_first_p=*/0);
11107 225379 : if (tem != NULL_TREE)
11108 : return tem;
11109 : }
11110 : }
11111 :
11112 557495272 : switch (code)
11113 : {
11114 65048209 : case MEM_REF:
11115 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11116 65048209 : if (TREE_CODE (arg0) == ADDR_EXPR
11117 65048209 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11118 : {
11119 825448 : tree iref = TREE_OPERAND (arg0, 0);
11120 825448 : return fold_build2 (MEM_REF, type,
11121 : TREE_OPERAND (iref, 0),
11122 : int_const_binop (PLUS_EXPR, arg1,
11123 : TREE_OPERAND (iref, 1)));
11124 : }
11125 :
11126 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11127 64222761 : if (TREE_CODE (arg0) == ADDR_EXPR
11128 64222761 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11129 : {
11130 6211314 : tree base;
11131 6211314 : poly_int64 coffset;
11132 6211314 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11133 : &coffset);
11134 6211314 : if (!base)
11135 : return NULL_TREE;
11136 6207183 : return fold_build2 (MEM_REF, type,
11137 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11138 : int_const_binop (PLUS_EXPR, arg1,
11139 : size_int (coffset)));
11140 : }
11141 :
11142 : return NULL_TREE;
11143 :
11144 74616774 : case POINTER_PLUS_EXPR:
11145 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11146 149233132 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11147 149224247 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11148 32743 : return fold_convert_loc (loc, type,
11149 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11150 : fold_convert_loc (loc, sizetype,
11151 : arg1),
11152 : fold_convert_loc (loc, sizetype,
11153 32743 : arg0)));
11154 :
11155 : return NULL_TREE;
11156 :
11157 63907379 : case PLUS_EXPR:
11158 63907379 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11159 : {
11160 : /* X + (X / CST) * -CST is X % CST. */
11161 52397803 : if (TREE_CODE (arg1) == MULT_EXPR
11162 2342731 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11163 52404082 : && operand_equal_p (arg0,
11164 6279 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11165 : {
11166 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11167 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11168 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11169 : cst1, cst0);
11170 204 : if (sum && integer_zerop (sum))
11171 204 : return fold_convert_loc (loc, type,
11172 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11173 204 : TREE_TYPE (arg0), arg0,
11174 204 : cst0));
11175 : }
11176 : }
11177 :
11178 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11179 : one. Make sure the type is not saturating and has the signedness of
11180 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11181 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11182 63907175 : if ((TREE_CODE (arg0) == MULT_EXPR
11183 52021166 : || TREE_CODE (arg1) == MULT_EXPR)
11184 13218828 : && !TYPE_SATURATING (type)
11185 13218828 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11186 12816992 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11187 76035416 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11188 : {
11189 8846133 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11190 8846133 : if (tem)
11191 : return tem;
11192 : }
11193 :
11194 62624229 : if (! FLOAT_TYPE_P (type))
11195 : {
11196 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11197 : (plus (plus (mult) (mult)) (foo)) so that we can
11198 : take advantage of the factoring cases below. */
11199 284891 : if (ANY_INTEGRAL_TYPE_P (type)
11200 51117017 : && TYPE_OVERFLOW_WRAPS (type)
11201 51117017 : && (((TREE_CODE (arg0) == PLUS_EXPR
11202 32032119 : || TREE_CODE (arg0) == MINUS_EXPR)
11203 3470025 : && TREE_CODE (arg1) == MULT_EXPR)
11204 31527633 : || ((TREE_CODE (arg1) == PLUS_EXPR
11205 31527633 : || TREE_CODE (arg1) == MINUS_EXPR)
11206 434478 : && TREE_CODE (arg0) == MULT_EXPR)))
11207 : {
11208 552138 : tree parg0, parg1, parg, marg;
11209 552138 : enum tree_code pcode;
11210 :
11211 552138 : if (TREE_CODE (arg1) == MULT_EXPR)
11212 : parg = arg0, marg = arg1;
11213 : else
11214 47652 : parg = arg1, marg = arg0;
11215 552138 : pcode = TREE_CODE (parg);
11216 552138 : parg0 = TREE_OPERAND (parg, 0);
11217 552138 : parg1 = TREE_OPERAND (parg, 1);
11218 552138 : STRIP_NOPS (parg0);
11219 552138 : STRIP_NOPS (parg1);
11220 :
11221 552138 : if (TREE_CODE (parg0) == MULT_EXPR
11222 271474 : && TREE_CODE (parg1) != MULT_EXPR)
11223 232709 : return fold_build2_loc (loc, pcode, type,
11224 : fold_build2_loc (loc, PLUS_EXPR, type,
11225 : fold_convert_loc (loc, type,
11226 : parg0),
11227 : fold_convert_loc (loc, type,
11228 : marg)),
11229 232709 : fold_convert_loc (loc, type, parg1));
11230 319429 : if (TREE_CODE (parg0) != MULT_EXPR
11231 280664 : && TREE_CODE (parg1) == MULT_EXPR)
11232 97803 : return
11233 97803 : fold_build2_loc (loc, PLUS_EXPR, type,
11234 : fold_convert_loc (loc, type, parg0),
11235 : fold_build2_loc (loc, pcode, type,
11236 : fold_convert_loc (loc, type, marg),
11237 : fold_convert_loc (loc, type,
11238 97803 : parg1)));
11239 : }
11240 : }
11241 : else
11242 : {
11243 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11244 : to __complex__ ( x, y ). This is not the same for SNaNs or
11245 : if signed zeros are involved. */
11246 11507212 : if (!HONOR_SNANS (arg0)
11247 11505503 : && !HONOR_SIGNED_ZEROS (arg0)
11248 11529143 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11249 : {
11250 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11251 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11252 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11253 3086 : bool arg0rz = false, arg0iz = false;
11254 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11255 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11256 : {
11257 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11258 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11259 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11260 : {
11261 22 : tree rp = arg1r ? arg1r
11262 0 : : build1 (REALPART_EXPR, rtype, arg1);
11263 22 : tree ip = arg0i ? arg0i
11264 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11265 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11266 : }
11267 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11268 : {
11269 53 : tree rp = arg0r ? arg0r
11270 0 : : build1 (REALPART_EXPR, rtype, arg0);
11271 53 : tree ip = arg1i ? arg1i
11272 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11273 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11274 : }
11275 : }
11276 : }
11277 :
11278 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11279 : We associate floats only if the user has specified
11280 : -fassociative-math. */
11281 11507137 : if (flag_associative_math
11282 21463 : && TREE_CODE (arg1) == PLUS_EXPR
11283 38 : && TREE_CODE (arg0) != MULT_EXPR)
11284 : {
11285 23 : tree tree10 = TREE_OPERAND (arg1, 0);
11286 23 : tree tree11 = TREE_OPERAND (arg1, 1);
11287 23 : if (TREE_CODE (tree11) == MULT_EXPR
11288 5 : && TREE_CODE (tree10) == MULT_EXPR)
11289 : {
11290 1 : tree tree0;
11291 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11292 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11293 : }
11294 : }
11295 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11296 : We associate floats only if the user has specified
11297 : -fassociative-math. */
11298 11507136 : if (flag_associative_math
11299 21462 : && TREE_CODE (arg0) == PLUS_EXPR
11300 1294 : && TREE_CODE (arg1) != MULT_EXPR)
11301 : {
11302 866 : tree tree00 = TREE_OPERAND (arg0, 0);
11303 866 : tree tree01 = TREE_OPERAND (arg0, 1);
11304 866 : if (TREE_CODE (tree01) == MULT_EXPR
11305 49 : && TREE_CODE (tree00) == MULT_EXPR)
11306 : {
11307 9 : tree tree0;
11308 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11309 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11310 : }
11311 : }
11312 : }
11313 :
11314 11506270 : bit_rotate:
11315 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11316 : is a rotate of A by C1 bits. */
11317 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11318 : is a rotate of A by B bits.
11319 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11320 : though in this case CODE must be | and not + or ^, otherwise
11321 : it doesn't return A when B is 0. */
11322 65184023 : {
11323 65184023 : enum tree_code code0, code1;
11324 65184023 : tree rtype;
11325 65184023 : code0 = TREE_CODE (arg0);
11326 65184023 : code1 = TREE_CODE (arg1);
11327 75550 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11328 65167543 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11329 39967 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11330 39967 : TREE_OPERAND (arg1, 0), 0)
11331 37198 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11332 37198 : TYPE_UNSIGNED (rtype))
11333 : /* Only create rotates in complete modes. Other cases are not
11334 : expanded properly. */
11335 65211123 : && (element_precision (rtype)
11336 54200 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11337 : {
11338 27027 : tree tree01, tree11;
11339 27027 : tree orig_tree01, orig_tree11;
11340 27027 : enum tree_code code01, code11;
11341 :
11342 27027 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11343 27027 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11344 27027 : STRIP_NOPS (tree01);
11345 27027 : STRIP_NOPS (tree11);
11346 27027 : code01 = TREE_CODE (tree01);
11347 27027 : code11 = TREE_CODE (tree11);
11348 27027 : if (code11 != MINUS_EXPR
11349 26339 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11350 : {
11351 1462 : std::swap (code0, code1);
11352 1462 : std::swap (code01, code11);
11353 1462 : std::swap (tree01, tree11);
11354 1462 : std::swap (orig_tree01, orig_tree11);
11355 : }
11356 54054 : if (code01 == INTEGER_CST
11357 3250 : && code11 == INTEGER_CST
11358 30276 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11359 30276 : == element_precision (rtype)))
11360 : {
11361 6026 : tem = build2_loc (loc, LROTATE_EXPR,
11362 3013 : rtype, TREE_OPERAND (arg0, 0),
11363 : code0 == LSHIFT_EXPR
11364 : ? orig_tree01 : orig_tree11);
11365 3013 : return fold_convert_loc (loc, type, tem);
11366 : }
11367 24014 : else if (code11 == MINUS_EXPR)
11368 : {
11369 943 : tree tree110, tree111;
11370 943 : tree110 = TREE_OPERAND (tree11, 0);
11371 943 : tree111 = TREE_OPERAND (tree11, 1);
11372 943 : STRIP_NOPS (tree110);
11373 943 : STRIP_NOPS (tree111);
11374 943 : if (TREE_CODE (tree110) == INTEGER_CST
11375 932 : && compare_tree_int (tree110,
11376 932 : element_precision (rtype)) == 0
11377 1859 : && operand_equal_p (tree01, tree111, 0))
11378 : {
11379 780 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11380 : ? LROTATE_EXPR : RROTATE_EXPR),
11381 560 : rtype, TREE_OPERAND (arg0, 0),
11382 : orig_tree01);
11383 560 : return fold_convert_loc (loc, type, tem);
11384 : }
11385 : }
11386 23071 : else if (code == BIT_IOR_EXPR
11387 21957 : && code11 == BIT_AND_EXPR
11388 44857 : && pow2p_hwi (element_precision (rtype)))
11389 : {
11390 21786 : tree tree110, tree111;
11391 21786 : tree110 = TREE_OPERAND (tree11, 0);
11392 21786 : tree111 = TREE_OPERAND (tree11, 1);
11393 21786 : STRIP_NOPS (tree110);
11394 21786 : STRIP_NOPS (tree111);
11395 21786 : if (TREE_CODE (tree110) == NEGATE_EXPR
11396 21331 : && TREE_CODE (tree111) == INTEGER_CST
11397 21331 : && compare_tree_int (tree111,
11398 21331 : element_precision (rtype) - 1) == 0
11399 43103 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11400 : {
11401 31847 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11402 : ? LROTATE_EXPR : RROTATE_EXPR),
11403 21255 : rtype, TREE_OPERAND (arg0, 0),
11404 : orig_tree01);
11405 21255 : return fold_convert_loc (loc, type, tem);
11406 : }
11407 : }
11408 : }
11409 : }
11410 :
11411 158605842 : associate:
11412 : /* In most languages, can't associate operations on floats through
11413 : parentheses. Rather than remember where the parentheses were, we
11414 : don't associate floats at all, unless the user has specified
11415 : -fassociative-math.
11416 : And, we need to make sure type is not saturating. */
11417 :
11418 158605842 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11419 117799234 : && !TYPE_SATURATING (type)
11420 276405076 : && !TYPE_OVERFLOW_SANITIZED (type))
11421 : {
11422 117766938 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11423 117766938 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11424 117766938 : tree atype = type;
11425 117766938 : bool ok = true;
11426 :
11427 : /* Split both trees into variables, constants, and literals. Then
11428 : associate each group together, the constants with literals,
11429 : then the result with variables. This increases the chances of
11430 : literals being recombined later and of generating relocatable
11431 : expressions for the sum of a constant and literal. */
11432 117766938 : var0 = split_tree (arg0, type, code,
11433 : &minus_var0, &con0, &minus_con0,
11434 : &lit0, &minus_lit0, 0);
11435 117766938 : var1 = split_tree (arg1, type, code,
11436 : &minus_var1, &con1, &minus_con1,
11437 : &lit1, &minus_lit1, code == MINUS_EXPR);
11438 :
11439 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11440 117766938 : if (code == MINUS_EXPR)
11441 13050578 : code = PLUS_EXPR;
11442 :
11443 : /* With undefined overflow prefer doing association in a type
11444 : which wraps on overflow, if that is one of the operand types. */
11445 117766707 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11446 234278974 : && !TYPE_OVERFLOW_WRAPS (type))
11447 : {
11448 62820956 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11449 62176680 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11450 853683 : atype = TREE_TYPE (arg0);
11451 61067637 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11452 60831664 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11453 234249 : atype = TREE_TYPE (arg1);
11454 31663074 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11455 : }
11456 :
11457 : /* With undefined overflow we can only associate constants with one
11458 : variable, and constants whose association doesn't overflow. */
11459 117766707 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11460 234278974 : && !TYPE_OVERFLOW_WRAPS (atype))
11461 : {
11462 30575142 : if ((var0 && var1) || (minus_var0 && minus_var1))
11463 : {
11464 : /* ??? If split_tree would handle NEGATE_EXPR we could
11465 : simply reject these cases and the allowed cases would
11466 : be the var0/minus_var1 ones. */
11467 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11468 5669990 : tree tmp1 = var1 ? var1 : minus_var1;
11469 5669990 : bool one_neg = false;
11470 :
11471 5669990 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11472 : {
11473 752 : tmp0 = TREE_OPERAND (tmp0, 0);
11474 752 : one_neg = !one_neg;
11475 : }
11476 5041630 : if (CONVERT_EXPR_P (tmp0)
11477 653982 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11478 6323017 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11479 653027 : <= TYPE_PRECISION (atype)))
11480 639864 : tmp0 = TREE_OPERAND (tmp0, 0);
11481 5669990 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11482 : {
11483 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11484 170 : one_neg = !one_neg;
11485 : }
11486 5343626 : if (CONVERT_EXPR_P (tmp1)
11487 394403 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11488 6064337 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11489 394347 : <= TYPE_PRECISION (atype)))
11490 377114 : tmp1 = TREE_OPERAND (tmp1, 0);
11491 : /* The only case we can still associate with two variables
11492 : is if they cancel out. */
11493 5669990 : if (!one_neg
11494 5669990 : || !operand_equal_p (tmp0, tmp1, 0))
11495 : ok = false;
11496 : }
11497 24524020 : else if ((var0 && minus_var1
11498 4180477 : && ! operand_equal_p (var0, minus_var1, 0))
11499 45248696 : || (minus_var0 && var1
11500 11602 : && ! operand_equal_p (minus_var0, var1, 0)))
11501 : ok = false;
11502 : }
11503 :
11504 : /* Only do something if we found more than two objects. Otherwise,
11505 : nothing has changed and we risk infinite recursion. */
11506 : if (ok
11507 107904950 : && ((var0 != 0) + (var1 != 0)
11508 107904950 : + (minus_var0 != 0) + (minus_var1 != 0)
11509 107904950 : + (con0 != 0) + (con1 != 0)
11510 107904950 : + (minus_con0 != 0) + (minus_con1 != 0)
11511 107904950 : + (lit0 != 0) + (lit1 != 0)
11512 107904950 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11513 : {
11514 2081804 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11515 4163608 : int minus_var0_origin
11516 2081804 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11517 2081804 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11518 4163608 : int minus_con0_origin
11519 2081804 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11520 2081804 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11521 4163608 : int minus_lit0_origin
11522 2081804 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11523 2081804 : var0 = associate_trees (loc, var0, var1, code, atype);
11524 2081804 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11525 : code, atype);
11526 2081804 : con0 = associate_trees (loc, con0, con1, code, atype);
11527 2081804 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11528 : code, atype);
11529 2081804 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11530 2081804 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11531 : code, atype);
11532 :
11533 2081804 : if (minus_var0 && var0)
11534 : {
11535 1364312 : var0_origin |= minus_var0_origin;
11536 1364312 : var0 = associate_trees (loc, var0, minus_var0,
11537 : MINUS_EXPR, atype);
11538 1364312 : minus_var0 = 0;
11539 1364312 : minus_var0_origin = 0;
11540 : }
11541 2081804 : if (minus_con0 && con0)
11542 : {
11543 2722 : con0_origin |= minus_con0_origin;
11544 2722 : con0 = associate_trees (loc, con0, minus_con0,
11545 : MINUS_EXPR, atype);
11546 2722 : minus_con0 = 0;
11547 2722 : minus_con0_origin = 0;
11548 : }
11549 :
11550 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11551 : greater than the positive part. Otherwise, the multiplicative
11552 : folding code (i.e extract_muldiv) may be fooled in case
11553 : unsigned constants are subtracted, like in the following
11554 : example: ((X*2 + 4) - 8U)/2. */
11555 2081804 : if (minus_lit0 && lit0)
11556 : {
11557 231663 : if (TREE_CODE (lit0) == INTEGER_CST
11558 231663 : && TREE_CODE (minus_lit0) == INTEGER_CST
11559 231663 : && tree_int_cst_lt (lit0, minus_lit0)
11560 : /* But avoid ending up with only negated parts. */
11561 294810 : && (var0 || con0))
11562 : {
11563 58448 : minus_lit0_origin |= lit0_origin;
11564 58448 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11565 : MINUS_EXPR, atype);
11566 58448 : lit0 = 0;
11567 58448 : lit0_origin = 0;
11568 : }
11569 : else
11570 : {
11571 173215 : lit0_origin |= minus_lit0_origin;
11572 173215 : lit0 = associate_trees (loc, lit0, minus_lit0,
11573 : MINUS_EXPR, atype);
11574 173215 : minus_lit0 = 0;
11575 173215 : minus_lit0_origin = 0;
11576 : }
11577 : }
11578 :
11579 : /* Don't introduce overflows through reassociation. */
11580 1374987 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11581 3456753 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11582 2081804 : return NULL_TREE;
11583 :
11584 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11585 2081766 : con0_origin |= lit0_origin;
11586 2081766 : con0 = associate_trees (loc, con0, lit0, code, atype);
11587 2081766 : minus_con0_origin |= minus_lit0_origin;
11588 2081766 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11589 : code, atype);
11590 :
11591 : /* Eliminate minus_con0. */
11592 2081766 : if (minus_con0)
11593 : {
11594 712043 : if (con0)
11595 : {
11596 15440 : con0_origin |= minus_con0_origin;
11597 15440 : con0 = associate_trees (loc, con0, minus_con0,
11598 : MINUS_EXPR, atype);
11599 : }
11600 696603 : else if (var0)
11601 : {
11602 696603 : var0_origin |= minus_con0_origin;
11603 696603 : var0 = associate_trees (loc, var0, minus_con0,
11604 : MINUS_EXPR, atype);
11605 : }
11606 : else
11607 0 : gcc_unreachable ();
11608 : }
11609 :
11610 : /* Eliminate minus_var0. */
11611 2081766 : if (minus_var0)
11612 : {
11613 362952 : if (con0)
11614 : {
11615 362952 : con0_origin |= minus_var0_origin;
11616 362952 : con0 = associate_trees (loc, con0, minus_var0,
11617 : MINUS_EXPR, atype);
11618 : }
11619 : else
11620 0 : gcc_unreachable ();
11621 : }
11622 :
11623 : /* Reassociate only if there has been any actual association
11624 : between subtrees from op0 and subtrees from op1 in at
11625 : least one of the operands, otherwise we risk infinite
11626 : recursion. See PR114084. */
11627 2081766 : if (var0_origin != 3 && con0_origin != 3)
11628 : return NULL_TREE;
11629 :
11630 2080085 : return
11631 2080085 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11632 2080085 : code, atype));
11633 : }
11634 : }
11635 :
11636 : return NULL_TREE;
11637 :
11638 23849499 : case POINTER_DIFF_EXPR:
11639 23849499 : case MINUS_EXPR:
11640 : /* Fold &a[i] - &a[j] to i-j. */
11641 23849499 : if (TREE_CODE (arg0) == ADDR_EXPR
11642 42358 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11643 6036 : && TREE_CODE (arg1) == ADDR_EXPR
11644 23850107 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11645 : {
11646 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11647 38 : TREE_OPERAND (arg0, 0),
11648 38 : TREE_OPERAND (arg1, 0),
11649 : code
11650 : == POINTER_DIFF_EXPR);
11651 38 : if (tem)
11652 : return tem;
11653 : }
11654 :
11655 : /* Further transformations are not for pointers. */
11656 23849485 : if (code == POINTER_DIFF_EXPR)
11657 : return NULL_TREE;
11658 :
11659 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11660 21096951 : if (TREE_CODE (arg0) == NEGATE_EXPR
11661 147331 : && negate_expr_p (op1)
11662 : /* If arg0 is e.g. unsigned int and type is int, then this could
11663 : introduce UB, because if A is INT_MIN at runtime, the original
11664 : expression can be well defined while the latter is not.
11665 : See PR83269. */
11666 21097799 : && !(ANY_INTEGRAL_TYPE_P (type)
11667 848 : && TYPE_OVERFLOW_UNDEFINED (type)
11668 836 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11669 836 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11670 841 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11671 : fold_convert_loc (loc, type,
11672 1682 : TREE_OPERAND (arg0, 0)));
11673 :
11674 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11675 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11676 : signed zeros are involved. */
11677 21096110 : if (!HONOR_SNANS (arg0)
11678 21094815 : && !HONOR_SIGNED_ZEROS (arg0)
11679 34989199 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11680 : {
11681 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11682 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11683 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11684 53 : bool arg0rz = false, arg0iz = false;
11685 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11686 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11687 : {
11688 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11689 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11690 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11691 : {
11692 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11693 : arg1r ? arg1r
11694 0 : : build1 (REALPART_EXPR, rtype, arg1));
11695 9 : tree ip = arg0i ? arg0i
11696 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11697 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11698 : }
11699 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11700 : {
11701 15 : tree rp = arg0r ? arg0r
11702 0 : : build1 (REALPART_EXPR, rtype, arg0);
11703 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11704 : arg1i ? arg1i
11705 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11706 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11707 : }
11708 : }
11709 : }
11710 :
11711 : /* A - B -> A + (-B) if B is easily negatable. */
11712 21096086 : if (negate_expr_p (op1)
11713 785632 : && ! TYPE_OVERFLOW_SANITIZED (type)
11714 21879180 : && ((FLOAT_TYPE_P (type)
11715 : /* Avoid this transformation if B is a positive REAL_CST. */
11716 65 : && (TREE_CODE (op1) != REAL_CST
11717 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11718 783029 : || INTEGRAL_TYPE_P (type)))
11719 782889 : return fold_build2_loc (loc, PLUS_EXPR, type,
11720 : fold_convert_loc (loc, type, arg0),
11721 782889 : negate_expr (op1));
11722 :
11723 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11724 : one. Make sure the type is not saturating and has the signedness of
11725 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11726 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11727 20313197 : if ((TREE_CODE (arg0) == MULT_EXPR
11728 19024209 : || TREE_CODE (arg1) == MULT_EXPR)
11729 2599360 : && !TYPE_SATURATING (type)
11730 2599360 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11731 2467536 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11732 22728555 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11733 : {
11734 335762 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11735 335762 : if (tem)
11736 : return tem;
11737 : }
11738 :
11739 20262898 : goto associate;
11740 :
11741 66802717 : case MULT_EXPR:
11742 66802717 : if (! FLOAT_TYPE_P (type))
11743 : {
11744 : /* Transform x * -C into -x * C if x is easily negatable. */
11745 44655442 : if (TREE_CODE (op1) == INTEGER_CST
11746 41537692 : && tree_int_cst_sgn (op1) == -1
11747 207727 : && negate_expr_p (op0)
11748 340 : && negate_expr_p (op1)
11749 324 : && (tem = negate_expr (op1)) != op1
11750 44655766 : && ! TREE_OVERFLOW (tem))
11751 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11752 : fold_convert_loc (loc, type,
11753 324 : negate_expr (op0)), tem);
11754 :
11755 44655118 : if (TREE_CODE (arg1) == INTEGER_CST
11756 44655118 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11757 : {
11758 660444 : return fold_convert_loc (loc, type, tem);
11759 : }
11760 :
11761 : /* Optimize z * conj(z) for integer complex numbers. */
11762 43994674 : if (TREE_CODE (arg0) == CONJ_EXPR
11763 43994674 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11764 1 : return fold_mult_zconjz (loc, type, arg1);
11765 43994673 : if (TREE_CODE (arg1) == CONJ_EXPR
11766 43994673 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11767 0 : return fold_mult_zconjz (loc, type, arg0);
11768 : }
11769 : else
11770 : {
11771 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11772 : This is not the same for NaNs or if signed zeros are
11773 : involved. */
11774 22147275 : if (!HONOR_NANS (arg0)
11775 33380 : && !HONOR_SIGNED_ZEROS (arg0)
11776 33043 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11777 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11778 22147500 : && real_zerop (TREE_REALPART (arg1)))
11779 : {
11780 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11781 218 : if (real_onep (TREE_IMAGPART (arg1)))
11782 : {
11783 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11784 63 : arg0 = save_expr (arg0);
11785 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11786 : rtype, arg0);
11787 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11788 : rtype, arg0);
11789 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11790 : negate_expr (iarg0),
11791 208 : rarg0);
11792 : }
11793 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11794 : {
11795 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11796 0 : arg0 = save_expr (arg0);
11797 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11798 : rtype, arg0);
11799 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11800 : rtype, arg0);
11801 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11802 : iarg0,
11803 10 : negate_expr (rarg0));
11804 : }
11805 : }
11806 :
11807 : /* Optimize z * conj(z) for floating point complex numbers.
11808 : Guarded by flag_unsafe_math_optimizations as non-finite
11809 : imaginary components don't produce scalar results. */
11810 22147057 : if (flag_unsafe_math_optimizations
11811 32866 : && TREE_CODE (arg0) == CONJ_EXPR
11812 22147059 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11813 1 : return fold_mult_zconjz (loc, type, arg1);
11814 22147056 : if (flag_unsafe_math_optimizations
11815 32865 : && TREE_CODE (arg1) == CONJ_EXPR
11816 22147060 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11817 0 : return fold_mult_zconjz (loc, type, arg0);
11818 : }
11819 66141729 : goto associate;
11820 :
11821 1929028 : case BIT_IOR_EXPR:
11822 : /* Canonicalize (X & C1) | C2. */
11823 1929028 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11824 91008 : && TREE_CODE (arg1) == INTEGER_CST
11825 1973778 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11826 : {
11827 44742 : int width = TYPE_PRECISION (type), w;
11828 44742 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11829 44742 : wide_int c2 = wi::to_wide (arg1);
11830 :
11831 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11832 44742 : if ((c1 & c2) == c1)
11833 0 : return omit_one_operand_loc (loc, type, arg1,
11834 0 : TREE_OPERAND (arg0, 0));
11835 :
11836 44742 : wide_int msk = wi::mask (width, false,
11837 44742 : TYPE_PRECISION (TREE_TYPE (arg1)));
11838 :
11839 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11840 44742 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11841 : {
11842 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11843 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11844 : }
11845 :
11846 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11847 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11848 : mode which allows further optimizations. */
11849 44736 : c1 &= msk;
11850 44736 : c2 &= msk;
11851 44736 : wide_int c3 = wi::bit_and_not (c1, c2);
11852 185262 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11853 : {
11854 96032 : wide_int mask = wi::mask (w, false,
11855 96032 : TYPE_PRECISION (type));
11856 192064 : if (((c1 | c2) & mask) == mask
11857 192064 : && wi::bit_and_not (c1, mask) == 0)
11858 : {
11859 242 : c3 = mask;
11860 242 : break;
11861 : }
11862 96032 : }
11863 :
11864 44736 : if (c3 != c1)
11865 : {
11866 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11867 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11868 558 : wide_int_to_tree (type, c3));
11869 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11870 : }
11871 45864 : }
11872 :
11873 : /* See if this can be simplified into a rotate first. If that
11874 : is unsuccessful continue in the association code. */
11875 1928464 : goto bit_rotate;
11876 :
11877 961927 : case BIT_XOR_EXPR:
11878 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11879 961927 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11880 3755 : && INTEGRAL_TYPE_P (type)
11881 3150 : && integer_onep (TREE_OPERAND (arg0, 1))
11882 963170 : && integer_onep (arg1))
11883 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11884 0 : build_zero_cst (TREE_TYPE (arg0)));
11885 :
11886 : /* See if this can be simplified into a rotate first. If that
11887 : is unsuccessful continue in the association code. */
11888 961927 : goto bit_rotate;
11889 :
11890 6607361 : case BIT_AND_EXPR:
11891 : /* Fold !X & 1 as X == 0. */
11892 6607361 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11893 6607361 : && integer_onep (arg1))
11894 : {
11895 0 : tem = TREE_OPERAND (arg0, 0);
11896 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11897 0 : build_zero_cst (TREE_TYPE (tem)));
11898 : }
11899 :
11900 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11901 : multiple of 1 << CST. */
11902 6607361 : if (TREE_CODE (arg1) == INTEGER_CST)
11903 : {
11904 4766011 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11905 4766011 : wide_int ncst1 = -cst1;
11906 4766011 : if ((cst1 & ncst1) == ncst1
11907 4922458 : && multiple_of_p (type, arg0,
11908 4922458 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11909 462 : return fold_convert_loc (loc, type, arg0);
11910 4766011 : }
11911 :
11912 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11913 : bits from CST2. */
11914 6606899 : if (TREE_CODE (arg1) == INTEGER_CST
11915 4765549 : && TREE_CODE (arg0) == MULT_EXPR
11916 6768553 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11917 : {
11918 161592 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11919 161592 : wide_int masked
11920 161592 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11921 :
11922 161592 : if (masked == 0)
11923 5661 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11924 5661 : arg0, arg1);
11925 155931 : else if (masked != warg1)
11926 : {
11927 : /* Avoid the transform if arg1 is a mask of some
11928 : mode which allows further optimizations. */
11929 632 : int pop = wi::popcount (warg1);
11930 654 : if (!(pop >= BITS_PER_UNIT
11931 50 : && pow2p_hwi (pop)
11932 676 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11933 1220 : return fold_build2_loc (loc, code, type, op0,
11934 1220 : wide_int_to_tree (type, masked));
11935 : }
11936 161592 : }
11937 :
11938 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11939 4759278 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11940 6814146 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11941 : {
11942 116328 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11943 :
11944 116328 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11945 116328 : if (mask == -1)
11946 2351 : return
11947 2351 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11948 116328 : }
11949 :
11950 6598277 : goto associate;
11951 :
11952 6081729 : case RDIV_EXPR:
11953 : /* Don't touch a floating-point divide by zero unless the mode
11954 : of the constant can represent infinity. */
11955 6081729 : if (TREE_CODE (arg1) == REAL_CST
11956 3061985 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11957 6081729 : && real_zerop (arg1))
11958 0 : return NULL_TREE;
11959 :
11960 : /* (-A) / (-B) -> A / B */
11961 6081729 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11962 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11963 3 : TREE_OPERAND (arg0, 0),
11964 3 : negate_expr (arg1));
11965 6081726 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11966 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11967 : negate_expr (arg0),
11968 0 : TREE_OPERAND (arg1, 0));
11969 : return NULL_TREE;
11970 :
11971 2216887 : case TRUNC_DIV_EXPR:
11972 : /* Fall through */
11973 :
11974 2216887 : case FLOOR_DIV_EXPR:
11975 : /* Simplify A / (B << N) where A and B are positive and B is
11976 : a power of 2, to A >> (N + log2(B)). */
11977 2216887 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11978 2216887 : && (TYPE_UNSIGNED (type)
11979 9 : || tree_expr_nonnegative_p (op0)))
11980 : {
11981 17 : tree sval = TREE_OPERAND (arg1, 0);
11982 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11983 : {
11984 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11985 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11986 16 : wi::exact_log2 (wi::to_wide (sval)));
11987 :
11988 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11989 : sh_cnt, pow2);
11990 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11991 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11992 : }
11993 : }
11994 :
11995 : /* Fall through */
11996 :
11997 3620394 : case ROUND_DIV_EXPR:
11998 3620394 : case CEIL_DIV_EXPR:
11999 3620394 : case EXACT_DIV_EXPR:
12000 3620394 : if (integer_zerop (arg1))
12001 : return NULL_TREE;
12002 :
12003 : /* Convert -A / -B to A / B when the type is signed and overflow is
12004 : undefined. */
12005 3617360 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12006 1005334 : && TREE_CODE (op0) == NEGATE_EXPR
12007 3617422 : && negate_expr_p (op1))
12008 60 : return fold_build2_loc (loc, code, type,
12009 : fold_convert_loc (loc, type,
12010 30 : TREE_OPERAND (arg0, 0)),
12011 30 : negate_expr (op1));
12012 3617330 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12013 1005304 : && TREE_CODE (arg1) == NEGATE_EXPR
12014 3617574 : && negate_expr_p (op0))
12015 36 : return fold_build2_loc (loc, code, type,
12016 : negate_expr (op0),
12017 : fold_convert_loc (loc, type,
12018 72 : TREE_OPERAND (arg1, 0)));
12019 :
12020 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12021 : operation, EXACT_DIV_EXPR.
12022 :
12023 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12024 : At one time others generated faster code, it's not clear if they do
12025 : after the last round to changes to the DIV code in expmed.cc. */
12026 3617294 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12027 3617294 : && multiple_of_p (type, arg0, arg1))
12028 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12029 : fold_convert (type, arg0),
12030 0 : fold_convert (type, arg1));
12031 :
12032 3617294 : if (TREE_CODE (arg1) == INTEGER_CST
12033 3617294 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12034 9494 : return fold_convert_loc (loc, type, tem);
12035 :
12036 : return NULL_TREE;
12037 :
12038 891791 : case CEIL_MOD_EXPR:
12039 891791 : case FLOOR_MOD_EXPR:
12040 891791 : case ROUND_MOD_EXPR:
12041 891791 : case TRUNC_MOD_EXPR:
12042 891791 : if (TREE_CODE (arg1) == INTEGER_CST
12043 891791 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12044 0 : return fold_convert_loc (loc, type, tem);
12045 :
12046 : return NULL_TREE;
12047 :
12048 2257623 : case LROTATE_EXPR:
12049 2257623 : case RROTATE_EXPR:
12050 2257623 : case RSHIFT_EXPR:
12051 2257623 : case LSHIFT_EXPR:
12052 : /* Since negative shift count is not well-defined,
12053 : don't try to compute it in the compiler. */
12054 2257623 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12055 : return NULL_TREE;
12056 :
12057 2256582 : prec = element_precision (type);
12058 :
12059 : /* If we have a rotate of a bit operation with the rotate count and
12060 : the second operand of the bit operation both constant,
12061 : permute the two operations. */
12062 2807 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12063 2242 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12064 2242 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12065 2242 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12066 2256582 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12067 : {
12068 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12069 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12070 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12071 : fold_build2_loc (loc, code, type,
12072 : arg00, arg1),
12073 : fold_build2_loc (loc, code, type,
12074 0 : arg01, arg1));
12075 : }
12076 :
12077 : return NULL_TREE;
12078 :
12079 443743 : case MIN_EXPR:
12080 443743 : case MAX_EXPR:
12081 443743 : goto associate;
12082 :
12083 6684248 : case TRUTH_ANDIF_EXPR:
12084 : /* Note that the operands of this must be ints
12085 : and their values must be 0 or 1.
12086 : ("true" is a fixed value perhaps depending on the language.) */
12087 : /* If first arg is constant zero, return it. */
12088 6684248 : if (integer_zerop (arg0))
12089 1700054 : return fold_convert_loc (loc, type, arg0);
12090 : /* FALLTHRU */
12091 15849850 : case TRUTH_AND_EXPR:
12092 : /* If either arg is constant true, drop it. */
12093 15849850 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12094 1497785 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12095 823570 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12096 : /* Preserve sequence points. */
12097 15130201 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12098 751071 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12099 : /* If second arg is constant zero, result is zero, but first arg
12100 : must be evaluated. */
12101 13600994 : if (integer_zerop (arg1))
12102 45434 : return omit_one_operand_loc (loc, type, arg1, arg0);
12103 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12104 : case will be handled here. */
12105 13555560 : if (integer_zerop (arg0))
12106 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12107 :
12108 : /* !X && X is always false. */
12109 13555560 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12110 13555560 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12111 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12112 : /* X && !X is always false. */
12113 13555560 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12114 13555560 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12115 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12116 :
12117 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12118 : means A >= Y && A != MAX, but in this case we know that
12119 : A < X <= MAX. */
12120 :
12121 13555560 : if (!TREE_SIDE_EFFECTS (arg0)
12122 13555560 : && !TREE_SIDE_EFFECTS (arg1))
12123 : {
12124 12078331 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12125 12078331 : if (tem && !operand_equal_p (tem, arg0, 0))
12126 434 : return fold_convert (type,
12127 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12128 : tem, arg1));
12129 :
12130 12077897 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12131 12077897 : if (tem && !operand_equal_p (tem, arg1, 0))
12132 8463 : return fold_convert (type,
12133 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12134 : arg0, tem));
12135 : }
12136 :
12137 13546663 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12138 : != NULL_TREE)
12139 : return tem;
12140 :
12141 : return NULL_TREE;
12142 :
12143 3506968 : case TRUTH_ORIF_EXPR:
12144 : /* Note that the operands of this must be ints
12145 : and their values must be 0 or true.
12146 : ("true" is a fixed value perhaps depending on the language.) */
12147 : /* If first arg is constant true, return it. */
12148 3506968 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12149 145810 : return fold_convert_loc (loc, type, arg0);
12150 : /* FALLTHRU */
12151 12878753 : case TRUTH_OR_EXPR:
12152 : /* If either arg is constant zero, drop it. */
12153 12878753 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12154 250082 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12155 454966 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12156 : /* Preserve sequence points. */
12157 13037323 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12158 397348 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12159 : /* If second arg is constant true, result is true, but we must
12160 : evaluate first arg. */
12161 12231323 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12162 46314 : return omit_one_operand_loc (loc, type, arg1, arg0);
12163 : /* Likewise for first arg, but note this only occurs here for
12164 : TRUTH_OR_EXPR. */
12165 12185009 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12166 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12167 :
12168 : /* !X || X is always true. */
12169 12185009 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12170 12185009 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12171 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12172 : /* X || !X is always true. */
12173 12185009 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12174 12185009 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12175 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12176 :
12177 : /* (X && !Y) || (!X && Y) is X ^ Y */
12178 12185008 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12179 1654 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12180 : {
12181 660 : tree a0, a1, l0, l1, n0, n1;
12182 :
12183 660 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12184 660 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12185 :
12186 660 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12187 660 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12188 :
12189 660 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12190 660 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12191 :
12192 660 : if ((operand_equal_p (n0, a0, 0)
12193 18 : && operand_equal_p (n1, a1, 0))
12194 668 : || (operand_equal_p (n0, a1, 0)
12195 3 : && operand_equal_p (n1, a0, 0)))
12196 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12197 : }
12198 :
12199 12184995 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12200 : != NULL_TREE)
12201 : return tem;
12202 :
12203 : return NULL_TREE;
12204 :
12205 79189 : case TRUTH_XOR_EXPR:
12206 : /* If the second arg is constant zero, drop it. */
12207 79189 : if (integer_zerop (arg1))
12208 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12209 : /* If the second arg is constant true, this is a logical inversion. */
12210 79189 : if (integer_onep (arg1))
12211 : {
12212 0 : tem = invert_truthvalue_loc (loc, arg0);
12213 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12214 : }
12215 : /* Identical arguments cancel to zero. */
12216 79189 : if (operand_equal_p (arg0, arg1, 0))
12217 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12218 :
12219 : /* !X ^ X is always true. */
12220 79189 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12221 79189 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12222 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12223 :
12224 : /* X ^ !X is always true. */
12225 79189 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12226 79189 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12227 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12228 :
12229 : return NULL_TREE;
12230 :
12231 49663953 : case EQ_EXPR:
12232 49663953 : case NE_EXPR:
12233 49663953 : STRIP_NOPS (arg0);
12234 49663953 : STRIP_NOPS (arg1);
12235 :
12236 49663953 : tem = fold_comparison (loc, code, type, op0, op1);
12237 49663953 : if (tem != NULL_TREE)
12238 : return tem;
12239 :
12240 : /* bool_var != 1 becomes !bool_var. */
12241 50825107 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12242 49703259 : && code == NE_EXPR)
12243 39951 : return fold_convert_loc (loc, type,
12244 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12245 79902 : TREE_TYPE (arg0), arg0));
12246 :
12247 : /* bool_var == 0 becomes !bool_var. */
12248 50745205 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12249 50556818 : && code == EQ_EXPR)
12250 187634 : return fold_convert_loc (loc, type,
12251 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12252 375268 : TREE_TYPE (arg0), arg0));
12253 :
12254 : /* !exp != 0 becomes !exp */
12255 616521 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12256 50046272 : && code == NE_EXPR)
12257 607475 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12258 :
12259 : /* If this is an EQ or NE comparison with zero and ARG0 is
12260 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12261 : two operations, but the latter can be done in one less insn
12262 : on machines that have only two-operand insns or on which a
12263 : constant cannot be the first operand. */
12264 48823019 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12265 48823019 : && integer_zerop (arg1))
12266 : {
12267 1519137 : tree arg00 = TREE_OPERAND (arg0, 0);
12268 1519137 : tree arg01 = TREE_OPERAND (arg0, 1);
12269 1519137 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12270 1519137 : && integer_onep (TREE_OPERAND (arg00, 0)))
12271 : {
12272 4327 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12273 4327 : arg01, TREE_OPERAND (arg00, 1));
12274 4327 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12275 4327 : build_one_cst (TREE_TYPE (arg0)));
12276 4327 : return fold_build2_loc (loc, code, type,
12277 4327 : fold_convert_loc (loc, TREE_TYPE (arg1),
12278 4327 : tem), arg1);
12279 : }
12280 1514810 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12281 1514810 : && integer_onep (TREE_OPERAND (arg01, 0)))
12282 : {
12283 426 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12284 426 : arg00, TREE_OPERAND (arg01, 1));
12285 426 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12286 426 : build_one_cst (TREE_TYPE (arg0)));
12287 426 : return fold_build2_loc (loc, code, type,
12288 426 : fold_convert_loc (loc, TREE_TYPE (arg1),
12289 426 : tem), arg1);
12290 : }
12291 : }
12292 :
12293 : /* If this is a comparison of a field, we may be able to simplify it. */
12294 48818266 : if ((TREE_CODE (arg0) == COMPONENT_REF
12295 48818266 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12296 : /* Handle the constant case even without -O
12297 : to make sure the warnings are given. */
12298 5016322 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12299 : {
12300 4698951 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12301 4698951 : if (t1)
12302 : return t1;
12303 : }
12304 :
12305 : /* Optimize comparisons of strlen vs zero to a compare of the
12306 : first character of the string vs zero. To wit,
12307 : strlen(ptr) == 0 => *ptr == 0
12308 : strlen(ptr) != 0 => *ptr != 0
12309 : Other cases should reduce to one of these two (or a constant)
12310 : due to the return value of strlen being unsigned. */
12311 48023099 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12312 : {
12313 3032878 : tree fndecl = get_callee_fndecl (arg0);
12314 :
12315 3032878 : if (fndecl
12316 3031783 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12317 550 : && call_expr_nargs (arg0) == 1
12318 3033428 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12319 : == POINTER_TYPE))
12320 : {
12321 550 : tree ptrtype
12322 550 : = build_pointer_type (build_qualified_type (char_type_node,
12323 : TYPE_QUAL_CONST));
12324 1100 : tree ptr = fold_convert_loc (loc, ptrtype,
12325 550 : CALL_EXPR_ARG (arg0, 0));
12326 550 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12327 550 : return fold_build2_loc (loc, code, type, iref,
12328 550 : build_int_cst (TREE_TYPE (iref), 0));
12329 : }
12330 : }
12331 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12332 : (X & C) == 0 when C is a single bit. */
12333 48022549 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12334 1688348 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12335 861 : && integer_zerop (arg1)
12336 48023011 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12337 : {
12338 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12339 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12340 140 : TREE_OPERAND (arg0, 1));
12341 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12342 : type, tem,
12343 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12344 140 : arg1));
12345 : }
12346 :
12347 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12348 : constant C is a power of two, i.e. a single bit. */
12349 48022409 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12350 3941 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12351 0 : && integer_zerop (arg1)
12352 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12353 48022409 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12354 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12355 : {
12356 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12357 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12358 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12359 : }
12360 :
12361 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12362 : when is C is a power of two, i.e. a single bit. */
12363 48022409 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12364 1688208 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12365 37235 : && integer_zerop (arg1)
12366 37235 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12367 48056789 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12368 34380 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12369 : {
12370 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12371 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12372 0 : arg000, TREE_OPERAND (arg0, 1));
12373 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12374 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12375 : }
12376 :
12377 48022409 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12378 3941 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12379 : {
12380 74 : tree arg00 = TREE_OPERAND (arg0, 0);
12381 74 : tree arg01 = TREE_OPERAND (arg0, 1);
12382 74 : tree arg10 = TREE_OPERAND (arg1, 0);
12383 74 : tree arg11 = TREE_OPERAND (arg1, 1);
12384 74 : tree itype = TREE_TYPE (arg0);
12385 :
12386 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12387 : operand_equal_p guarantees no side-effects so we don't need
12388 : to use omit_one_operand on Z. */
12389 74 : if (operand_equal_p (arg01, arg11, 0))
12390 8 : return fold_build2_loc (loc, code, type, arg00,
12391 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12392 8 : arg10));
12393 66 : if (operand_equal_p (arg01, arg10, 0))
12394 0 : return fold_build2_loc (loc, code, type, arg00,
12395 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12396 0 : arg11));
12397 66 : if (operand_equal_p (arg00, arg11, 0))
12398 0 : return fold_build2_loc (loc, code, type, arg01,
12399 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12400 0 : arg10));
12401 66 : if (operand_equal_p (arg00, arg10, 0))
12402 0 : return fold_build2_loc (loc, code, type, arg01,
12403 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12404 0 : arg11));
12405 :
12406 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12407 66 : if (TREE_CODE (arg01) == INTEGER_CST
12408 8 : && TREE_CODE (arg11) == INTEGER_CST)
12409 : {
12410 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12411 : fold_convert_loc (loc, itype, arg11));
12412 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12413 8 : return fold_build2_loc (loc, code, type, tem,
12414 8 : fold_convert_loc (loc, itype, arg10));
12415 : }
12416 : }
12417 :
12418 : /* Attempt to simplify equality/inequality comparisons of complex
12419 : values. Only lower the comparison if the result is known or
12420 : can be simplified to a single scalar comparison. */
12421 48022393 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12422 48019830 : || TREE_CODE (arg0) == COMPLEX_CST)
12423 2563 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12424 2365 : || TREE_CODE (arg1) == COMPLEX_CST))
12425 : {
12426 1750 : tree real0, imag0, real1, imag1;
12427 1750 : tree rcond, icond;
12428 :
12429 1750 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12430 : {
12431 1750 : real0 = TREE_OPERAND (arg0, 0);
12432 1750 : imag0 = TREE_OPERAND (arg0, 1);
12433 : }
12434 : else
12435 : {
12436 0 : real0 = TREE_REALPART (arg0);
12437 0 : imag0 = TREE_IMAGPART (arg0);
12438 : }
12439 :
12440 1750 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12441 : {
12442 198 : real1 = TREE_OPERAND (arg1, 0);
12443 198 : imag1 = TREE_OPERAND (arg1, 1);
12444 : }
12445 : else
12446 : {
12447 1552 : real1 = TREE_REALPART (arg1);
12448 1552 : imag1 = TREE_IMAGPART (arg1);
12449 : }
12450 :
12451 1750 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12452 1750 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12453 : {
12454 11 : if (integer_zerop (rcond))
12455 : {
12456 11 : if (code == EQ_EXPR)
12457 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12458 0 : imag0, imag1);
12459 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12460 : }
12461 : else
12462 : {
12463 0 : if (code == NE_EXPR)
12464 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12465 0 : imag0, imag1);
12466 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12467 : }
12468 : }
12469 :
12470 1739 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12471 1739 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12472 : {
12473 9 : if (integer_zerop (icond))
12474 : {
12475 7 : if (code == EQ_EXPR)
12476 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12477 1 : real0, real1);
12478 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12479 : }
12480 : else
12481 : {
12482 2 : if (code == NE_EXPR)
12483 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12484 1 : real0, real1);
12485 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12486 : }
12487 : }
12488 : }
12489 :
12490 : return NULL_TREE;
12491 :
12492 41409499 : case LT_EXPR:
12493 41409499 : case GT_EXPR:
12494 41409499 : case LE_EXPR:
12495 41409499 : case GE_EXPR:
12496 41409499 : tem = fold_comparison (loc, code, type, op0, op1);
12497 41409499 : if (tem != NULL_TREE)
12498 : return tem;
12499 :
12500 : /* Transform comparisons of the form X +- C CMP X. */
12501 40520641 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12502 4798909 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12503 51400 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12504 40520657 : && !HONOR_SNANS (arg0))
12505 : {
12506 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12507 14 : enum tree_code code0 = TREE_CODE (arg0);
12508 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12509 :
12510 : /* (X - c) > X becomes false. */
12511 14 : if (code == GT_EXPR
12512 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12513 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12514 4 : return constant_boolean_node (0, type);
12515 :
12516 : /* Likewise (X + c) < X becomes false. */
12517 10 : if (code == LT_EXPR
12518 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12519 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12520 3 : return constant_boolean_node (0, type);
12521 :
12522 : /* Convert (X - c) <= X to true. */
12523 7 : if (!HONOR_NANS (arg1)
12524 6 : && code == LE_EXPR
12525 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12526 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12527 4 : return constant_boolean_node (1, type);
12528 :
12529 : /* Convert (X + c) >= X to true. */
12530 3 : if (!HONOR_NANS (arg1)
12531 2 : && code == GE_EXPR
12532 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12533 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12534 2 : return constant_boolean_node (1, type);
12535 : }
12536 :
12537 : /* If we are comparing an ABS_EXPR with a constant, we can
12538 : convert all the cases into explicit comparisons, but they may
12539 : well not be faster than doing the ABS and one comparison.
12540 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12541 : and a comparison, and is probably faster. */
12542 40520628 : if (code == LE_EXPR
12543 7767782 : && TREE_CODE (arg1) == INTEGER_CST
12544 5528361 : && TREE_CODE (arg0) == ABS_EXPR
12545 484 : && ! TREE_SIDE_EFFECTS (arg0)
12546 484 : && (tem = negate_expr (arg1)) != 0
12547 484 : && TREE_CODE (tem) == INTEGER_CST
12548 40521112 : && !TREE_OVERFLOW (tem))
12549 968 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12550 : build2 (GE_EXPR, type,
12551 484 : TREE_OPERAND (arg0, 0), tem),
12552 : build2 (LE_EXPR, type,
12553 968 : TREE_OPERAND (arg0, 0), arg1));
12554 :
12555 : /* Convert ABS_EXPR<x> >= 0 to true. */
12556 40520144 : if (code == GE_EXPR
12557 4221362 : && (integer_zerop (arg1)
12558 3081771 : || (! HONOR_NANS (arg0)
12559 2407233 : && real_zerop (arg1)))
12560 41659966 : && tree_expr_nonnegative_p (arg0))
12561 5180 : return omit_one_operand_loc (loc, type,
12562 : constant_boolean_node (true, type),
12563 5180 : arg0);
12564 :
12565 : /* Convert ABS_EXPR<x> < 0 to false. */
12566 40514964 : if (code == LT_EXPR
12567 13416540 : && (integer_zerop (arg1) || real_zerop (arg1))
12568 43764036 : && tree_expr_nonnegative_p (arg0))
12569 41958 : return omit_one_operand_loc (loc, type,
12570 : constant_boolean_node (false, type),
12571 41958 : arg0);
12572 :
12573 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12574 : and similarly for >= into !=. */
12575 40473006 : if ((code == LT_EXPR || code == GE_EXPR)
12576 17590764 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12577 5546962 : && TREE_CODE (arg1) == LSHIFT_EXPR
12578 40474530 : && integer_onep (TREE_OPERAND (arg1, 0)))
12579 4048 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12580 1352 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12581 1352 : TREE_OPERAND (arg1, 1)),
12582 2704 : build_zero_cst (TREE_TYPE (arg0)));
12583 :
12584 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12585 : otherwise Y might be >= # of bits in X's type and thus e.g.
12586 : (unsigned char) (1 << Y) for Y 15 might be 0.
12587 : If the cast is widening, then 1 << Y should have unsigned type,
12588 : otherwise if Y is number of bits in the signed shift type minus 1,
12589 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12590 : 31 might be 0xffffffff80000000. */
12591 40471654 : if ((code == LT_EXPR || code == GE_EXPR)
12592 17589412 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12593 5733964 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12594 11878994 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12595 4059254 : && CONVERT_EXPR_P (arg1)
12596 1127554 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12597 42 : && (element_precision (TREE_TYPE (arg1))
12598 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12599 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12600 14 : || (element_precision (TREE_TYPE (arg1))
12601 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12602 40471661 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12603 : {
12604 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12605 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12606 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12607 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12608 14 : build_zero_cst (TREE_TYPE (arg0)));
12609 : }
12610 :
12611 : return NULL_TREE;
12612 :
12613 5987414 : case UNORDERED_EXPR:
12614 5987414 : case ORDERED_EXPR:
12615 5987414 : case UNLT_EXPR:
12616 5987414 : case UNLE_EXPR:
12617 5987414 : case UNGT_EXPR:
12618 5987414 : case UNGE_EXPR:
12619 5987414 : case UNEQ_EXPR:
12620 5987414 : case LTGT_EXPR:
12621 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12622 5987414 : {
12623 5987414 : tree targ0 = strip_float_extensions (arg0);
12624 5987414 : tree targ1 = strip_float_extensions (arg1);
12625 5987414 : tree newtype = TREE_TYPE (targ0);
12626 :
12627 5987414 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12628 1215 : newtype = TREE_TYPE (targ1);
12629 :
12630 5987414 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12631 5987414 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12632 328 : return fold_build2_loc (loc, code, type,
12633 : fold_convert_loc (loc, newtype, targ0),
12634 328 : fold_convert_loc (loc, newtype, targ1));
12635 : }
12636 :
12637 : return NULL_TREE;
12638 :
12639 8649362 : case COMPOUND_EXPR:
12640 : /* When pedantic, a compound expression can be neither an lvalue
12641 : nor an integer constant expression. */
12642 8649362 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12643 : return NULL_TREE;
12644 : /* Don't let (0, 0) be null pointer constant. */
12645 517641 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12646 517641 : : fold_convert_loc (loc, type, arg1);
12647 : return tem;
12648 :
12649 : default:
12650 : return NULL_TREE;
12651 : } /* switch (code) */
12652 : }
12653 :
12654 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12655 : ((A & N) + B) & M -> (A + B) & M
12656 : Similarly if (N & M) == 0,
12657 : ((A | N) + B) & M -> (A + B) & M
12658 : and for - instead of + (or unary - instead of +)
12659 : and/or ^ instead of |.
12660 : If B is constant and (B & M) == 0, fold into A & M.
12661 :
12662 : This function is a helper for match.pd patterns. Return non-NULL
12663 : type in which the simplified operation should be performed only
12664 : if any optimization is possible.
12665 :
12666 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12667 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12668 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12669 : +/-. */
12670 : tree
12671 1252552 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12672 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12673 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12674 : tree *pmop)
12675 : {
12676 1252552 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12677 1252552 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12678 1252552 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12679 2505104 : if (~cst1 == 0
12680 3753300 : || (cst1 & (cst1 + 1)) != 0
12681 1038096 : || !INTEGRAL_TYPE_P (type)
12682 1038096 : || (!TYPE_OVERFLOW_WRAPS (type)
12683 44891 : && TREE_CODE (type) != INTEGER_TYPE)
12684 4579118 : || (wi::max_value (type) & cst1) != cst1)
12685 : return NULL_TREE;
12686 :
12687 1038096 : enum tree_code codes[2] = { code00, code01 };
12688 1038096 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12689 1038096 : int which = 0;
12690 1038096 : wide_int cst0;
12691 :
12692 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12693 : arg1 (M) is == (1LL << cst) - 1.
12694 : Store C into PMOP[0] and D into PMOP[1]. */
12695 1038096 : pmop[0] = arg00;
12696 1038096 : pmop[1] = arg01;
12697 1038096 : which = code != NEGATE_EXPR;
12698 :
12699 3113370 : for (; which >= 0; which--)
12700 2075274 : switch (codes[which])
12701 : {
12702 21234 : case BIT_AND_EXPR:
12703 21234 : case BIT_IOR_EXPR:
12704 21234 : case BIT_XOR_EXPR:
12705 21234 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12706 21234 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12707 21234 : if (codes[which] == BIT_AND_EXPR)
12708 : {
12709 21122 : if (cst0 != cst1)
12710 : break;
12711 : }
12712 112 : else if (cst0 != 0)
12713 : break;
12714 : /* If C or D is of the form (A & N) where
12715 : (N & M) == M, or of the form (A | N) or
12716 : (A ^ N) where (N & M) == 0, replace it with A. */
12717 19695 : pmop[which] = arg0xx[2 * which];
12718 19695 : break;
12719 2054040 : case ERROR_MARK:
12720 2054040 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12721 : break;
12722 : /* If C or D is a N where (N & M) == 0, it can be
12723 : omitted (replaced with 0). */
12724 880906 : if ((code == PLUS_EXPR
12725 208018 : || (code == MINUS_EXPR && which == 0))
12726 648471 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12727 139127 : pmop[which] = build_int_cst (type, 0);
12728 : /* Similarly, with C - N where (-N & M) == 0. */
12729 880906 : if (code == MINUS_EXPR
12730 440453 : && which == 1
12731 641167 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12732 192683 : pmop[which] = build_int_cst (type, 0);
12733 : break;
12734 0 : default:
12735 0 : gcc_unreachable ();
12736 : }
12737 :
12738 : /* Only build anything new if we optimized one or both arguments above. */
12739 1038096 : if (pmop[0] == arg00 && pmop[1] == arg01)
12740 : return NULL_TREE;
12741 :
12742 350912 : if (TYPE_OVERFLOW_WRAPS (type))
12743 : return type;
12744 : else
12745 3026 : return unsigned_type_for (type);
12746 1038096 : }
12747 :
12748 : /* Used by contains_label_[p1]. */
12749 :
12750 : struct contains_label_data
12751 : {
12752 : hash_set<tree> *pset;
12753 : bool inside_switch_p;
12754 : };
12755 :
12756 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12757 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12758 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12759 :
12760 : static tree
12761 4605323 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12762 : {
12763 4605323 : contains_label_data *d = (contains_label_data *) data;
12764 4605323 : switch (TREE_CODE (*tp))
12765 : {
12766 : case LABEL_EXPR:
12767 : return *tp;
12768 :
12769 0 : case CASE_LABEL_EXPR:
12770 0 : if (!d->inside_switch_p)
12771 : return *tp;
12772 : return NULL_TREE;
12773 :
12774 0 : case SWITCH_EXPR:
12775 0 : if (!d->inside_switch_p)
12776 : {
12777 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12778 0 : return *tp;
12779 0 : d->inside_switch_p = true;
12780 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12781 0 : return *tp;
12782 0 : d->inside_switch_p = false;
12783 0 : *walk_subtrees = 0;
12784 : }
12785 : return NULL_TREE;
12786 :
12787 6662 : case GOTO_EXPR:
12788 6662 : *walk_subtrees = 0;
12789 6662 : return NULL_TREE;
12790 :
12791 : default:
12792 : return NULL_TREE;
12793 : }
12794 : }
12795 :
12796 : /* Return whether the sub-tree ST contains a label which is accessible from
12797 : outside the sub-tree. */
12798 :
12799 : static bool
12800 329109 : contains_label_p (tree st)
12801 : {
12802 329109 : hash_set<tree> pset;
12803 329109 : contains_label_data data = { &pset, false };
12804 329109 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12805 329109 : }
12806 :
12807 : /* Fold a ternary expression of code CODE and type TYPE with operands
12808 : OP0, OP1, and OP2. Return the folded expression if folding is
12809 : successful. Otherwise, return NULL_TREE. */
12810 :
12811 : tree
12812 47653662 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12813 : tree op0, tree op1, tree op2)
12814 : {
12815 47653662 : tree tem;
12816 47653662 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12817 47653662 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12818 :
12819 47653662 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12820 : && TREE_CODE_LENGTH (code) == 3);
12821 :
12822 : /* If this is a commutative operation, and OP0 is a constant, move it
12823 : to OP1 to reduce the number of tests below. */
12824 47653662 : if (commutative_ternary_tree_code (code)
12825 47653662 : && tree_swap_operands_p (op0, op1))
12826 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12827 :
12828 47653629 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12829 47653629 : if (tem)
12830 : return tem;
12831 :
12832 : /* Strip any conversions that don't change the mode. This is safe
12833 : for every expression, except for a comparison expression because
12834 : its signedness is derived from its operands. So, in the latter
12835 : case, only strip conversions that don't change the signedness.
12836 :
12837 : Note that this is done as an internal manipulation within the
12838 : constant folder, in order to find the simplest representation of
12839 : the arguments so that their form can be studied. In any cases,
12840 : the appropriate type conversions should be put back in the tree
12841 : that will get out of the constant folder. */
12842 46569641 : if (op0)
12843 : {
12844 46501098 : arg0 = op0;
12845 46501098 : STRIP_NOPS (arg0);
12846 : }
12847 :
12848 46569641 : if (op1)
12849 : {
12850 46569641 : arg1 = op1;
12851 46569641 : STRIP_NOPS (arg1);
12852 : }
12853 :
12854 46569641 : if (op2)
12855 : {
12856 15061127 : arg2 = op2;
12857 15061127 : STRIP_NOPS (arg2);
12858 : }
12859 :
12860 46569641 : switch (code)
12861 : {
12862 31508015 : case COMPONENT_REF:
12863 31508015 : if (TREE_CODE (arg0) == CONSTRUCTOR
12864 31508015 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12865 : {
12866 : unsigned HOST_WIDE_INT idx;
12867 : tree field, value;
12868 886 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12869 681 : if (field == arg1)
12870 : return value;
12871 : }
12872 : return NULL_TREE;
12873 :
12874 12596975 : case COND_EXPR:
12875 12596975 : case VEC_COND_EXPR:
12876 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12877 : so all simple results must be passed through pedantic_non_lvalue. */
12878 12596975 : if (TREE_CODE (arg0) == INTEGER_CST)
12879 : {
12880 463929 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12881 463929 : tem = integer_zerop (arg0) ? op2 : op1;
12882 : /* Only optimize constant conditions when the selected branch
12883 : has the same type as the COND_EXPR. This avoids optimizing
12884 : away "c ? x : throw", where the throw has a void type.
12885 : Avoid throwing away that operand which contains label. */
12886 463929 : if ((!TREE_SIDE_EFFECTS (unused_op)
12887 329109 : || !contains_label_p (unused_op))
12888 788196 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12889 378920 : || VOID_TYPE_P (type)))
12890 449865 : return protected_set_expr_location_unshare (tem, loc);
12891 : return NULL_TREE;
12892 : }
12893 12133046 : else if (TREE_CODE (arg0) == VECTOR_CST)
12894 : {
12895 11895 : unsigned HOST_WIDE_INT nelts;
12896 11895 : if ((TREE_CODE (arg1) == VECTOR_CST
12897 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12898 2854 : && (TREE_CODE (arg2) == VECTOR_CST
12899 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12900 23790 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12901 : {
12902 2854 : vec_perm_builder sel (nelts, nelts, 1);
12903 27688 : for (unsigned int i = 0; i < nelts; i++)
12904 : {
12905 24834 : tree val = VECTOR_CST_ELT (arg0, i);
12906 24834 : if (integer_all_onesp (val))
12907 12198 : sel.quick_push (i);
12908 12636 : else if (integer_zerop (val))
12909 12636 : sel.quick_push (nelts + i);
12910 : else /* Currently unreachable. */
12911 2166 : return NULL_TREE;
12912 : }
12913 2854 : vec_perm_indices indices (sel, 2, nelts);
12914 2854 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12915 2854 : if (t != NULL_TREE)
12916 2166 : return t;
12917 5020 : }
12918 : }
12919 :
12920 : /* If we have A op B ? A : C, we may be able to convert this to a
12921 : simpler expression, depending on the operation and the values
12922 : of B and C. Signed zeros prevent all of these transformations,
12923 : for reasons given above each one.
12924 :
12925 : Also try swapping the arguments and inverting the conditional. */
12926 12130880 : if (COMPARISON_CLASS_P (arg0)
12927 9979520 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12928 12270766 : && !HONOR_SIGNED_ZEROS (op1))
12929 : {
12930 129047 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12931 129047 : TREE_OPERAND (arg0, 0),
12932 129047 : TREE_OPERAND (arg0, 1),
12933 : op1, op2);
12934 129047 : if (tem)
12935 : return tem;
12936 : }
12937 :
12938 12123950 : if (COMPARISON_CLASS_P (arg0)
12939 9972590 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12940 12602779 : && !HONOR_SIGNED_ZEROS (op2))
12941 : {
12942 392388 : enum tree_code comp_code = TREE_CODE (arg0);
12943 392388 : tree arg00 = TREE_OPERAND (arg0, 0);
12944 392388 : tree arg01 = TREE_OPERAND (arg0, 1);
12945 392388 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12946 392388 : if (comp_code != ERROR_MARK)
12947 392387 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12948 : arg00,
12949 : arg01,
12950 : op2, op1);
12951 392388 : if (tem)
12952 : return tem;
12953 : }
12954 :
12955 : /* If the second operand is simpler than the third, swap them
12956 : since that produces better jump optimization results. */
12957 11849412 : if (truth_value_p (TREE_CODE (arg0))
12958 11849412 : && tree_swap_operands_p (op1, op2))
12959 : {
12960 2071862 : location_t loc0 = expr_location_or (arg0, loc);
12961 : /* See if this can be inverted. If it can't, possibly because
12962 : it was a floating-point inequality comparison, don't do
12963 : anything. */
12964 2071862 : tem = fold_invert_truthvalue (loc0, arg0);
12965 2071862 : if (tem)
12966 1325368 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12967 : }
12968 :
12969 : /* Convert A ? 1 : 0 to simply A. */
12970 10524044 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12971 10071787 : : (integer_onep (op1)
12972 412763 : && !VECTOR_TYPE_P (type)))
12973 691677 : && integer_zerop (op2)
12974 : /* If we try to convert OP0 to our type, the
12975 : call to fold will try to move the conversion inside
12976 : a COND, which will recurse. In that case, the COND_EXPR
12977 : is probably the best choice, so leave it alone. */
12978 11653830 : && type == TREE_TYPE (arg0))
12979 32622 : return protected_set_expr_location_unshare (arg0, loc);
12980 :
12981 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12982 : over COND_EXPR in cases such as floating point comparisons. */
12983 10491422 : if (integer_zerop (op1)
12984 389075 : && code == COND_EXPR
12985 365143 : && integer_onep (op2)
12986 32608 : && !VECTOR_TYPE_P (type)
12987 10524030 : && truth_value_p (TREE_CODE (arg0)))
12988 31061 : return fold_convert_loc (loc, type,
12989 31061 : invert_truthvalue_loc (loc, arg0));
12990 :
12991 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12992 10460361 : if (TREE_CODE (arg0) == LT_EXPR
12993 1368044 : && integer_zerop (TREE_OPERAND (arg0, 1))
12994 38150 : && integer_zerop (op2)
12995 10461727 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12996 : {
12997 : /* sign_bit_p looks through both zero and sign extensions,
12998 : but for this optimization only sign extensions are
12999 : usable. */
13000 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13001 56 : while (tem != tem2)
13002 : {
13003 0 : if (TREE_CODE (tem2) != NOP_EXPR
13004 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13005 : {
13006 : tem = NULL_TREE;
13007 : break;
13008 : }
13009 0 : tem2 = TREE_OPERAND (tem2, 0);
13010 : }
13011 : /* sign_bit_p only checks ARG1 bits within A's precision.
13012 : If <sign bit of A> has wider type than A, bits outside
13013 : of A's precision in <sign bit of A> need to be checked.
13014 : If they are all 0, this optimization needs to be done
13015 : in unsigned A's type, if they are all 1 in signed A's type,
13016 : otherwise this can't be done. */
13017 56 : if (tem
13018 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13019 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13020 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13021 56 : < TYPE_PRECISION (type))
13022 : {
13023 56 : int inner_width, outer_width;
13024 56 : tree tem_type;
13025 :
13026 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13027 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13028 56 : if (outer_width > TYPE_PRECISION (type))
13029 0 : outer_width = TYPE_PRECISION (type);
13030 :
13031 56 : wide_int mask = wi::shifted_mask
13032 56 : (inner_width, outer_width - inner_width, false,
13033 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13034 :
13035 56 : wide_int common = mask & wi::to_wide (arg1);
13036 56 : if (common == mask)
13037 : {
13038 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13039 28 : tem = fold_convert_loc (loc, tem_type, tem);
13040 : }
13041 28 : else if (common == 0)
13042 : {
13043 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13044 0 : tem = fold_convert_loc (loc, tem_type, tem);
13045 : }
13046 : else
13047 : tem = NULL;
13048 56 : }
13049 :
13050 56 : if (tem)
13051 28 : return
13052 56 : fold_convert_loc (loc, type,
13053 : fold_build2_loc (loc, BIT_AND_EXPR,
13054 28 : TREE_TYPE (tem), tem,
13055 : fold_convert_loc (loc,
13056 28 : TREE_TYPE (tem),
13057 28 : arg1)));
13058 : }
13059 :
13060 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13061 : already handled above. */
13062 10460333 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13063 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13064 3 : && integer_zerop (op2)
13065 10460333 : && integer_pow2p (arg1))
13066 : {
13067 0 : tree tem = TREE_OPERAND (arg0, 0);
13068 0 : STRIP_NOPS (tem);
13069 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13070 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13071 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13072 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13073 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13074 : fold_convert_loc (loc, type,
13075 0 : TREE_OPERAND (tem, 0)),
13076 0 : op1);
13077 : }
13078 :
13079 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13080 : is probably obsolete because the first operand should be a
13081 : truth value (that's why we have the two cases above), but let's
13082 : leave it in until we can confirm this for all front-ends. */
13083 10460333 : if (integer_zerop (op2)
13084 2069100 : && TREE_CODE (arg0) == NE_EXPR
13085 540750 : && integer_zerop (TREE_OPERAND (arg0, 1))
13086 291093 : && integer_pow2p (arg1)
13087 32268 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13088 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13089 : arg1, OEP_ONLY_CONST)
13090 : /* operand_equal_p compares just value, not precision, so e.g.
13091 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13092 : second operand 32-bit -128, which is not a power of two (or vice
13093 : versa. */
13094 10460333 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13095 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13096 :
13097 : /* Disable the transformations below for vectors, since
13098 : fold_binary_op_with_conditional_arg may undo them immediately,
13099 : yielding an infinite loop. */
13100 10460333 : if (code == VEC_COND_EXPR)
13101 : return NULL_TREE;
13102 :
13103 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13104 10008076 : if (integer_zerop (op2)
13105 1713569 : && truth_value_p (TREE_CODE (arg0))
13106 1520420 : && truth_value_p (TREE_CODE (arg1))
13107 10041905 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13108 33829 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13109 : : TRUTH_ANDIF_EXPR,
13110 33829 : type, fold_convert_loc (loc, type, arg0), op1);
13111 :
13112 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13113 9974247 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13114 455811 : && truth_value_p (TREE_CODE (arg0))
13115 308262 : && truth_value_p (TREE_CODE (arg1))
13116 10011822 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13117 : {
13118 37575 : location_t loc0 = expr_location_or (arg0, loc);
13119 : /* Only perform transformation if ARG0 is easily inverted. */
13120 37575 : tem = fold_invert_truthvalue (loc0, arg0);
13121 37575 : if (tem)
13122 37263 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13123 : ? BIT_IOR_EXPR
13124 : : TRUTH_ORIF_EXPR,
13125 : type, fold_convert_loc (loc, type, tem),
13126 37263 : op1);
13127 : }
13128 :
13129 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13130 9936984 : if (integer_zerop (arg1)
13131 334159 : && truth_value_p (TREE_CODE (arg0))
13132 83760 : && truth_value_p (TREE_CODE (op2))
13133 9937012 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13134 : {
13135 28 : location_t loc0 = expr_location_or (arg0, loc);
13136 : /* Only perform transformation if ARG0 is easily inverted. */
13137 28 : tem = fold_invert_truthvalue (loc0, arg0);
13138 28 : if (tem)
13139 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13140 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13141 : type, fold_convert_loc (loc, type, tem),
13142 0 : op2);
13143 : }
13144 :
13145 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13146 9936984 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13147 380141 : && truth_value_p (TREE_CODE (arg0))
13148 271008 : && truth_value_p (TREE_CODE (op2))
13149 9937170 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13150 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13151 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13152 186 : type, fold_convert_loc (loc, type, arg0), op2);
13153 :
13154 : return NULL_TREE;
13155 :
13156 0 : case CALL_EXPR:
13157 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13158 : of fold_ternary on them. */
13159 0 : gcc_unreachable ();
13160 :
13161 901535 : case BIT_FIELD_REF:
13162 901535 : if (TREE_CODE (arg0) == VECTOR_CST
13163 80974 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13164 42815 : || (VECTOR_TYPE_P (type)
13165 41997 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13166 80128 : && tree_fits_uhwi_p (op1)
13167 981663 : && tree_fits_uhwi_p (op2))
13168 : {
13169 80128 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13170 80128 : unsigned HOST_WIDE_INT width
13171 80128 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13172 80128 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13173 80128 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13174 80128 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13175 :
13176 80128 : if (n != 0
13177 80128 : && (idx % width) == 0
13178 80128 : && (n % width) == 0
13179 160256 : && known_le ((idx + n) / width,
13180 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13181 : {
13182 80128 : idx = idx / width;
13183 80128 : n = n / width;
13184 :
13185 80128 : if (TREE_CODE (arg0) == VECTOR_CST)
13186 : {
13187 80128 : if (n == 1)
13188 : {
13189 38163 : tem = VECTOR_CST_ELT (arg0, idx);
13190 38163 : if (VECTOR_TYPE_P (type))
13191 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13192 : return tem;
13193 : }
13194 :
13195 41965 : tree_vector_builder vals (type, n, 1);
13196 227570 : for (unsigned i = 0; i < n; ++i)
13197 143640 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13198 41965 : return vals.build ();
13199 41965 : }
13200 : }
13201 : }
13202 :
13203 : /* On constants we can use native encode/interpret to constant
13204 : fold (nearly) all BIT_FIELD_REFs. */
13205 821407 : if (CONSTANT_CLASS_P (arg0)
13206 1651 : && can_native_interpret_type_p (type)
13207 : && BITS_PER_UNIT == 8
13208 1651 : && tree_fits_uhwi_p (op1)
13209 823058 : && tree_fits_uhwi_p (op2))
13210 : {
13211 1651 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13212 1651 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13213 : /* Limit us to a reasonable amount of work. To relax the
13214 : other limitations we need bit-shifting of the buffer
13215 : and rounding up the size. */
13216 1651 : if (bitpos % BITS_PER_UNIT == 0
13217 1651 : && bitsize % BITS_PER_UNIT == 0
13218 1651 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13219 : {
13220 1651 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13221 1651 : unsigned HOST_WIDE_INT len
13222 1651 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13223 1651 : bitpos / BITS_PER_UNIT);
13224 1651 : if (len > 0
13225 1651 : && len * BITS_PER_UNIT >= bitsize)
13226 : {
13227 1651 : tree v = native_interpret_expr (type, b,
13228 : bitsize / BITS_PER_UNIT);
13229 1651 : if (v)
13230 1597 : return v;
13231 : }
13232 : }
13233 : }
13234 :
13235 : return NULL_TREE;
13236 :
13237 788483 : case VEC_PERM_EXPR:
13238 : /* Perform constant folding of BIT_INSERT_EXPR. */
13239 788483 : if (TREE_CODE (arg2) == VECTOR_CST
13240 776905 : && TREE_CODE (op0) == VECTOR_CST
13241 16567 : && TREE_CODE (op1) == VECTOR_CST)
13242 : {
13243 : /* Build a vector of integers from the tree mask. */
13244 3939 : vec_perm_builder builder;
13245 3939 : if (!tree_to_vec_perm_builder (&builder, arg2))
13246 : return NULL_TREE;
13247 :
13248 : /* Create a vec_perm_indices for the integer vector. */
13249 3939 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13250 3939 : bool single_arg = (op0 == op1);
13251 7878 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13252 3939 : return fold_vec_perm (type, op0, op1, sel);
13253 7878 : }
13254 : return NULL_TREE;
13255 :
13256 15971 : case BIT_INSERT_EXPR:
13257 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13258 15971 : if (TREE_CODE (arg0) == INTEGER_CST
13259 14 : && TREE_CODE (arg1) == INTEGER_CST)
13260 : {
13261 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13262 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13263 2 : if (BYTES_BIG_ENDIAN)
13264 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13265 2 : wide_int tem = (wi::to_wide (arg0)
13266 4 : & wi::shifted_mask (bitpos, bitsize, true,
13267 4 : TYPE_PRECISION (type)));
13268 2 : wide_int tem2
13269 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13270 2 : bitsize), bitpos);
13271 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13272 2 : }
13273 15969 : else if (TREE_CODE (arg0) == VECTOR_CST
13274 903 : && CONSTANT_CLASS_P (arg1)
13275 16268 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13276 299 : TREE_TYPE (arg1)))
13277 : {
13278 299 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13279 299 : unsigned HOST_WIDE_INT elsize
13280 299 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13281 299 : if (bitpos % elsize == 0)
13282 : {
13283 299 : unsigned k = bitpos / elsize;
13284 299 : unsigned HOST_WIDE_INT nelts;
13285 299 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13286 47653662 : return arg0;
13287 292 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13288 : {
13289 292 : tree_vector_builder elts (type, nelts, 1);
13290 292 : elts.quick_grow (nelts);
13291 1608 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13292 1024 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13293 292 : return elts.build ();
13294 292 : }
13295 : }
13296 : }
13297 : return NULL_TREE;
13298 :
13299 : default:
13300 : return NULL_TREE;
13301 : } /* switch (code) */
13302 : }
13303 :
13304 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13305 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13306 : constructor element index of the value returned. If the element is
13307 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13308 : the index of the element after the ACCESS_INDEX position (which
13309 : may be outside of the CTOR array). */
13310 :
13311 : tree
13312 690996 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13313 : unsigned *ctor_idx)
13314 : {
13315 690996 : tree index_type = NULL_TREE;
13316 690996 : signop index_sgn = UNSIGNED;
13317 690996 : offset_int low_bound = 0;
13318 :
13319 690996 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13320 : {
13321 690996 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13322 690996 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13323 : {
13324 : /* Static constructors for variably sized objects makes no sense. */
13325 690996 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13326 690996 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13327 : /* ??? When it is obvious that the range is signed, treat it so. */
13328 690996 : if (TYPE_UNSIGNED (index_type)
13329 348933 : && TYPE_MAX_VALUE (domain_type)
13330 1039898 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13331 348902 : TYPE_MIN_VALUE (domain_type)))
13332 : {
13333 0 : index_sgn = SIGNED;
13334 0 : low_bound
13335 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13336 : SIGNED);
13337 : }
13338 : else
13339 : {
13340 690996 : index_sgn = TYPE_SIGN (index_type);
13341 690996 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13342 : }
13343 : }
13344 : }
13345 :
13346 690996 : if (index_type)
13347 690996 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13348 : index_sgn);
13349 :
13350 690996 : offset_int index = low_bound;
13351 690996 : if (index_type)
13352 690996 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13353 :
13354 690996 : offset_int max_index = index;
13355 690996 : unsigned cnt;
13356 690996 : tree cfield, cval;
13357 690996 : bool first_p = true;
13358 :
13359 14316267 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13360 : {
13361 : /* Array constructor might explicitly set index, or specify a range,
13362 : or leave index NULL meaning that it is next index after previous
13363 : one. */
13364 14315204 : if (cfield)
13365 : {
13366 6300465 : if (TREE_CODE (cfield) == INTEGER_CST)
13367 12599770 : max_index = index
13368 6299885 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13369 : else
13370 : {
13371 580 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13372 580 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13373 : index_sgn);
13374 580 : max_index
13375 580 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13376 : index_sgn);
13377 580 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13378 : }
13379 : }
13380 8014739 : else if (!first_p)
13381 : {
13382 7818775 : index = max_index + 1;
13383 7818775 : if (index_type)
13384 7818775 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13385 7818775 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13386 7818775 : max_index = index;
13387 : }
13388 : else
13389 : first_p = false;
13390 :
13391 14315204 : if (TREE_CODE (cval) == RAW_DATA_CST)
13392 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13393 :
13394 : /* Do we have match? */
13395 14315204 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13396 : {
13397 14314916 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13398 : {
13399 689817 : if (ctor_idx)
13400 689817 : *ctor_idx = cnt;
13401 : return cval;
13402 : }
13403 : }
13404 288 : else if (in_gimple_form)
13405 : /* We're past the element we search for. Note during parsing
13406 : the elements might not be sorted.
13407 : ??? We should use a binary search and a flag on the
13408 : CONSTRUCTOR as to whether elements are sorted in declaration
13409 : order. */
13410 : break;
13411 : }
13412 1179 : if (ctor_idx)
13413 1179 : *ctor_idx = cnt;
13414 : return NULL_TREE;
13415 : }
13416 :
13417 : /* Perform constant folding and related simplification of EXPR.
13418 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13419 : and application of the associative law.
13420 : NOP_EXPR conversions may be removed freely (as long as we
13421 : are careful not to change the type of the overall expression).
13422 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13423 : but we can constant-fold them if they have constant operands. */
13424 :
13425 : #ifdef ENABLE_FOLD_CHECKING
13426 : # define fold(x) fold_1 (x)
13427 : static tree fold_1 (tree);
13428 : static
13429 : #endif
13430 : tree
13431 1385661697 : fold (tree expr)
13432 : {
13433 1385835513 : const tree t = expr;
13434 1385835513 : enum tree_code code = TREE_CODE (t);
13435 1385835513 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13436 1385835513 : tree tem;
13437 1385835513 : location_t loc = EXPR_LOCATION (expr);
13438 :
13439 : /* Return right away if a constant. */
13440 1385835513 : if (kind == tcc_constant)
13441 : return t;
13442 :
13443 : /* CALL_EXPR-like objects with variable numbers of operands are
13444 : treated specially. */
13445 1281805487 : if (kind == tcc_vl_exp)
13446 : {
13447 181579367 : if (code == CALL_EXPR)
13448 : {
13449 181578838 : tem = fold_call_expr (loc, expr, false);
13450 181578838 : return tem ? tem : expr;
13451 : }
13452 : return expr;
13453 : }
13454 :
13455 1100226120 : if (IS_EXPR_CODE_CLASS (kind))
13456 : {
13457 1098018670 : tree type = TREE_TYPE (t);
13458 1098018670 : tree op0, op1, op2;
13459 :
13460 1098018670 : switch (TREE_CODE_LENGTH (code))
13461 : {
13462 996975361 : case 1:
13463 996975361 : op0 = TREE_OPERAND (t, 0);
13464 996975361 : tem = fold_unary_loc (loc, code, type, op0);
13465 996975361 : return tem ? tem : expr;
13466 91799741 : case 2:
13467 91799741 : op0 = TREE_OPERAND (t, 0);
13468 91799741 : op1 = TREE_OPERAND (t, 1);
13469 91799741 : tem = fold_binary_loc (loc, code, type, op0, op1);
13470 91799741 : return tem ? tem : expr;
13471 4441150 : case 3:
13472 4441150 : op0 = TREE_OPERAND (t, 0);
13473 4441150 : op1 = TREE_OPERAND (t, 1);
13474 4441150 : op2 = TREE_OPERAND (t, 2);
13475 4441150 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13476 4441150 : return tem ? tem : expr;
13477 : default:
13478 : break;
13479 : }
13480 : }
13481 :
13482 7009868 : switch (code)
13483 : {
13484 4697968 : case ARRAY_REF:
13485 4697968 : {
13486 4697968 : tree op0 = TREE_OPERAND (t, 0);
13487 4697968 : tree op1 = TREE_OPERAND (t, 1);
13488 :
13489 4697968 : if (TREE_CODE (op1) == INTEGER_CST
13490 2949739 : && TREE_CODE (op0) == CONSTRUCTOR
13491 4699423 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13492 : {
13493 1455 : unsigned int idx;
13494 1455 : tree val
13495 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13496 : &idx);
13497 1455 : if (val)
13498 : {
13499 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13500 : return val;
13501 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13502 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13503 : != INTEGER_CST))
13504 : return t;
13505 2 : offset_int o
13506 2 : = (wi::to_offset (op1)
13507 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13508 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13509 2 : return build_int_cst (TREE_TYPE (val),
13510 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13511 : }
13512 : }
13513 :
13514 : return t;
13515 : }
13516 :
13517 : /* Return a VECTOR_CST if possible. */
13518 206653 : case CONSTRUCTOR:
13519 206653 : {
13520 206653 : tree type = TREE_TYPE (t);
13521 206653 : if (TREE_CODE (type) != VECTOR_TYPE)
13522 : return t;
13523 :
13524 : unsigned i;
13525 : tree val;
13526 364046 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13527 314936 : if (! CONSTANT_CLASS_P (val))
13528 : return t;
13529 :
13530 49110 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13531 : }
13532 :
13533 173816 : case CONST_DECL:
13534 173816 : return fold (DECL_INITIAL (t));
13535 :
13536 : default:
13537 : return t;
13538 : } /* switch (code) */
13539 : }
13540 :
13541 : #ifdef ENABLE_FOLD_CHECKING
13542 : #undef fold
13543 :
13544 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13545 : hash_table<nofree_ptr_hash<const tree_node> > *);
13546 : static void fold_check_failed (const_tree, const_tree);
13547 : void print_fold_checksum (const_tree);
13548 :
13549 : /* When --enable-checking=fold, compute a digest of expr before
13550 : and after actual fold call to see if fold did not accidentally
13551 : change original expr. */
13552 :
13553 : tree
13554 : fold (tree expr)
13555 : {
13556 : tree ret;
13557 : struct md5_ctx ctx;
13558 : unsigned char checksum_before[16], checksum_after[16];
13559 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13560 :
13561 : md5_init_ctx (&ctx);
13562 : fold_checksum_tree (expr, &ctx, &ht);
13563 : md5_finish_ctx (&ctx, checksum_before);
13564 : ht.empty ();
13565 :
13566 : ret = fold_1 (expr);
13567 :
13568 : md5_init_ctx (&ctx);
13569 : fold_checksum_tree (expr, &ctx, &ht);
13570 : md5_finish_ctx (&ctx, checksum_after);
13571 :
13572 : if (memcmp (checksum_before, checksum_after, 16))
13573 : fold_check_failed (expr, ret);
13574 :
13575 : return ret;
13576 : }
13577 :
13578 : void
13579 : print_fold_checksum (const_tree expr)
13580 : {
13581 : struct md5_ctx ctx;
13582 : unsigned char checksum[16], cnt;
13583 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13584 :
13585 : md5_init_ctx (&ctx);
13586 : fold_checksum_tree (expr, &ctx, &ht);
13587 : md5_finish_ctx (&ctx, checksum);
13588 : for (cnt = 0; cnt < 16; ++cnt)
13589 : fprintf (stderr, "%02x", checksum[cnt]);
13590 : putc ('\n', stderr);
13591 : }
13592 :
13593 : static void
13594 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13595 : {
13596 : internal_error ("fold check: original tree changed by fold");
13597 : }
13598 :
13599 : static void
13600 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13601 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13602 : {
13603 : const tree_node **slot;
13604 : enum tree_code code;
13605 : union tree_node *buf;
13606 : int i, len;
13607 :
13608 : recursive_label:
13609 : if (expr == NULL)
13610 : return;
13611 : slot = ht->find_slot (expr, INSERT);
13612 : if (*slot != NULL)
13613 : return;
13614 : *slot = expr;
13615 : code = TREE_CODE (expr);
13616 : if (TREE_CODE_CLASS (code) == tcc_declaration
13617 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13618 : {
13619 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13620 : size_t sz = tree_size (expr);
13621 : buf = XALLOCAVAR (union tree_node, sz);
13622 : memcpy ((char *) buf, expr, sz);
13623 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13624 : buf->decl_with_vis.symtab_node = NULL;
13625 : buf->base.nowarning_flag = 0;
13626 : expr = (tree) buf;
13627 : }
13628 : else if (TREE_CODE_CLASS (code) == tcc_type
13629 : && (TYPE_POINTER_TO (expr)
13630 : || TYPE_REFERENCE_TO (expr)
13631 : || TYPE_CACHED_VALUES_P (expr)
13632 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13633 : || TYPE_NEXT_VARIANT (expr)
13634 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13635 : {
13636 : /* Allow these fields to be modified. */
13637 : tree tmp;
13638 : size_t sz = tree_size (expr);
13639 : buf = XALLOCAVAR (union tree_node, sz);
13640 : memcpy ((char *) buf, expr, sz);
13641 : expr = tmp = (tree) buf;
13642 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13643 : TYPE_POINTER_TO (tmp) = NULL;
13644 : TYPE_REFERENCE_TO (tmp) = NULL;
13645 : TYPE_NEXT_VARIANT (tmp) = NULL;
13646 : TYPE_ALIAS_SET (tmp) = -1;
13647 : if (TYPE_CACHED_VALUES_P (tmp))
13648 : {
13649 : TYPE_CACHED_VALUES_P (tmp) = 0;
13650 : TYPE_CACHED_VALUES (tmp) = NULL;
13651 : }
13652 : }
13653 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13654 : {
13655 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13656 : that and change builtins.cc etc. instead - see PR89543. */
13657 : size_t sz = tree_size (expr);
13658 : buf = XALLOCAVAR (union tree_node, sz);
13659 : memcpy ((char *) buf, expr, sz);
13660 : buf->base.nowarning_flag = 0;
13661 : expr = (tree) buf;
13662 : }
13663 : md5_process_bytes (expr, tree_size (expr), ctx);
13664 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13665 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13666 : if (TREE_CODE_CLASS (code) != tcc_type
13667 : && TREE_CODE_CLASS (code) != tcc_declaration
13668 : && code != TREE_LIST
13669 : && code != SSA_NAME
13670 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13671 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13672 : switch (TREE_CODE_CLASS (code))
13673 : {
13674 : case tcc_constant:
13675 : switch (code)
13676 : {
13677 : case STRING_CST:
13678 : md5_process_bytes (TREE_STRING_POINTER (expr),
13679 : TREE_STRING_LENGTH (expr), ctx);
13680 : break;
13681 : case COMPLEX_CST:
13682 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13683 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13684 : break;
13685 : case VECTOR_CST:
13686 : len = vector_cst_encoded_nelts (expr);
13687 : for (i = 0; i < len; ++i)
13688 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13689 : break;
13690 : default:
13691 : break;
13692 : }
13693 : break;
13694 : case tcc_exceptional:
13695 : switch (code)
13696 : {
13697 : case TREE_LIST:
13698 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13699 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13700 : expr = TREE_CHAIN (expr);
13701 : goto recursive_label;
13702 : break;
13703 : case TREE_VEC:
13704 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13705 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13706 : break;
13707 : default:
13708 : break;
13709 : }
13710 : break;
13711 : case tcc_expression:
13712 : case tcc_reference:
13713 : case tcc_comparison:
13714 : case tcc_unary:
13715 : case tcc_binary:
13716 : case tcc_statement:
13717 : case tcc_vl_exp:
13718 : len = TREE_OPERAND_LENGTH (expr);
13719 : for (i = 0; i < len; ++i)
13720 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13721 : break;
13722 : case tcc_declaration:
13723 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13724 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13725 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13726 : {
13727 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13728 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13729 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13730 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13731 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13732 : }
13733 :
13734 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13735 : {
13736 : if (TREE_CODE (expr) == FUNCTION_DECL)
13737 : {
13738 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13739 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13740 : }
13741 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13742 : }
13743 : break;
13744 : case tcc_type:
13745 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13746 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13747 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13748 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13749 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13750 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13751 : if (INTEGRAL_TYPE_P (expr)
13752 : || SCALAR_FLOAT_TYPE_P (expr))
13753 : {
13754 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13755 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13756 : }
13757 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13758 : if (RECORD_OR_UNION_TYPE_P (expr))
13759 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13760 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13761 : break;
13762 : default:
13763 : break;
13764 : }
13765 : }
13766 :
13767 : /* Helper function for outputting the checksum of a tree T. When
13768 : debugging with gdb, you can "define mynext" to be "next" followed
13769 : by "call debug_fold_checksum (op0)", then just trace down till the
13770 : outputs differ. */
13771 :
13772 : DEBUG_FUNCTION void
13773 : debug_fold_checksum (const_tree t)
13774 : {
13775 : int i;
13776 : unsigned char checksum[16];
13777 : struct md5_ctx ctx;
13778 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13779 :
13780 : md5_init_ctx (&ctx);
13781 : fold_checksum_tree (t, &ctx, &ht);
13782 : md5_finish_ctx (&ctx, checksum);
13783 : ht.empty ();
13784 :
13785 : for (i = 0; i < 16; i++)
13786 : fprintf (stderr, "%d ", checksum[i]);
13787 :
13788 : fprintf (stderr, "\n");
13789 : }
13790 :
13791 : #endif
13792 :
13793 : /* Fold a unary tree expression with code CODE of type TYPE with an
13794 : operand OP0. LOC is the location of the resulting expression.
13795 : Return a folded expression if successful. Otherwise, return a tree
13796 : expression with code CODE of type TYPE with an operand OP0. */
13797 :
13798 : tree
13799 1064286931 : fold_build1_loc (location_t loc,
13800 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13801 : {
13802 1064286931 : tree tem;
13803 : #ifdef ENABLE_FOLD_CHECKING
13804 : unsigned char checksum_before[16], checksum_after[16];
13805 : struct md5_ctx ctx;
13806 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13807 :
13808 : md5_init_ctx (&ctx);
13809 : fold_checksum_tree (op0, &ctx, &ht);
13810 : md5_finish_ctx (&ctx, checksum_before);
13811 : ht.empty ();
13812 : #endif
13813 :
13814 1064286931 : tem = fold_unary_loc (loc, code, type, op0);
13815 1064286931 : if (!tem)
13816 543139446 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13817 :
13818 : #ifdef ENABLE_FOLD_CHECKING
13819 : md5_init_ctx (&ctx);
13820 : fold_checksum_tree (op0, &ctx, &ht);
13821 : md5_finish_ctx (&ctx, checksum_after);
13822 :
13823 : if (memcmp (checksum_before, checksum_after, 16))
13824 : fold_check_failed (op0, tem);
13825 : #endif
13826 1064286931 : return tem;
13827 : }
13828 :
13829 : /* Fold a binary tree expression with code CODE of type TYPE with
13830 : operands OP0 and OP1. LOC is the location of the resulting
13831 : expression. Return a folded expression if successful. Otherwise,
13832 : return a tree expression with code CODE of type TYPE with operands
13833 : OP0 and OP1. */
13834 :
13835 : tree
13836 685399194 : fold_build2_loc (location_t loc,
13837 : enum tree_code code, tree type, tree op0, tree op1
13838 : MEM_STAT_DECL)
13839 : {
13840 685399194 : tree tem;
13841 : #ifdef ENABLE_FOLD_CHECKING
13842 : unsigned char checksum_before_op0[16],
13843 : checksum_before_op1[16],
13844 : checksum_after_op0[16],
13845 : checksum_after_op1[16];
13846 : struct md5_ctx ctx;
13847 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13848 :
13849 : md5_init_ctx (&ctx);
13850 : fold_checksum_tree (op0, &ctx, &ht);
13851 : md5_finish_ctx (&ctx, checksum_before_op0);
13852 : ht.empty ();
13853 :
13854 : md5_init_ctx (&ctx);
13855 : fold_checksum_tree (op1, &ctx, &ht);
13856 : md5_finish_ctx (&ctx, checksum_before_op1);
13857 : ht.empty ();
13858 : #endif
13859 :
13860 685399194 : tem = fold_binary_loc (loc, code, type, op0, op1);
13861 685399194 : if (!tem)
13862 384751907 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13863 :
13864 : #ifdef ENABLE_FOLD_CHECKING
13865 : md5_init_ctx (&ctx);
13866 : fold_checksum_tree (op0, &ctx, &ht);
13867 : md5_finish_ctx (&ctx, checksum_after_op0);
13868 : ht.empty ();
13869 :
13870 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13871 : fold_check_failed (op0, tem);
13872 :
13873 : md5_init_ctx (&ctx);
13874 : fold_checksum_tree (op1, &ctx, &ht);
13875 : md5_finish_ctx (&ctx, checksum_after_op1);
13876 :
13877 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13878 : fold_check_failed (op1, tem);
13879 : #endif
13880 685399194 : return tem;
13881 : }
13882 :
13883 : /* Fold a ternary tree expression with code CODE of type TYPE with
13884 : operands OP0, OP1, and OP2. Return a folded expression if
13885 : successful. Otherwise, return a tree expression with code CODE of
13886 : type TYPE with operands OP0, OP1, and OP2. */
13887 :
13888 : tree
13889 41049477 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13890 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13891 : {
13892 41049477 : tree tem;
13893 : #ifdef ENABLE_FOLD_CHECKING
13894 : unsigned char checksum_before_op0[16],
13895 : checksum_before_op1[16],
13896 : checksum_before_op2[16],
13897 : checksum_after_op0[16],
13898 : checksum_after_op1[16],
13899 : checksum_after_op2[16];
13900 : struct md5_ctx ctx;
13901 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13902 :
13903 : md5_init_ctx (&ctx);
13904 : fold_checksum_tree (op0, &ctx, &ht);
13905 : md5_finish_ctx (&ctx, checksum_before_op0);
13906 : ht.empty ();
13907 :
13908 : md5_init_ctx (&ctx);
13909 : fold_checksum_tree (op1, &ctx, &ht);
13910 : md5_finish_ctx (&ctx, checksum_before_op1);
13911 : ht.empty ();
13912 :
13913 : md5_init_ctx (&ctx);
13914 : fold_checksum_tree (op2, &ctx, &ht);
13915 : md5_finish_ctx (&ctx, checksum_before_op2);
13916 : ht.empty ();
13917 : #endif
13918 :
13919 41049477 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13920 41049477 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13921 41049477 : if (!tem)
13922 38168366 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13923 :
13924 : #ifdef ENABLE_FOLD_CHECKING
13925 : md5_init_ctx (&ctx);
13926 : fold_checksum_tree (op0, &ctx, &ht);
13927 : md5_finish_ctx (&ctx, checksum_after_op0);
13928 : ht.empty ();
13929 :
13930 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13931 : fold_check_failed (op0, tem);
13932 :
13933 : md5_init_ctx (&ctx);
13934 : fold_checksum_tree (op1, &ctx, &ht);
13935 : md5_finish_ctx (&ctx, checksum_after_op1);
13936 : ht.empty ();
13937 :
13938 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13939 : fold_check_failed (op1, tem);
13940 :
13941 : md5_init_ctx (&ctx);
13942 : fold_checksum_tree (op2, &ctx, &ht);
13943 : md5_finish_ctx (&ctx, checksum_after_op2);
13944 :
13945 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13946 : fold_check_failed (op2, tem);
13947 : #endif
13948 41049477 : return tem;
13949 : }
13950 :
13951 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13952 : arguments in ARGARRAY, and a null static chain.
13953 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13954 : of type TYPE from the given operands as constructed by build_call_array. */
13955 :
13956 : tree
13957 57833141 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13958 : int nargs, tree *argarray)
13959 : {
13960 57833141 : tree tem;
13961 : #ifdef ENABLE_FOLD_CHECKING
13962 : unsigned char checksum_before_fn[16],
13963 : checksum_before_arglist[16],
13964 : checksum_after_fn[16],
13965 : checksum_after_arglist[16];
13966 : struct md5_ctx ctx;
13967 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13968 : int i;
13969 :
13970 : md5_init_ctx (&ctx);
13971 : fold_checksum_tree (fn, &ctx, &ht);
13972 : md5_finish_ctx (&ctx, checksum_before_fn);
13973 : ht.empty ();
13974 :
13975 : md5_init_ctx (&ctx);
13976 : for (i = 0; i < nargs; i++)
13977 : fold_checksum_tree (argarray[i], &ctx, &ht);
13978 : md5_finish_ctx (&ctx, checksum_before_arglist);
13979 : ht.empty ();
13980 : #endif
13981 :
13982 57833141 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13983 57833141 : if (!tem)
13984 55707950 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13985 :
13986 : #ifdef ENABLE_FOLD_CHECKING
13987 : md5_init_ctx (&ctx);
13988 : fold_checksum_tree (fn, &ctx, &ht);
13989 : md5_finish_ctx (&ctx, checksum_after_fn);
13990 : ht.empty ();
13991 :
13992 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13993 : fold_check_failed (fn, tem);
13994 :
13995 : md5_init_ctx (&ctx);
13996 : for (i = 0; i < nargs; i++)
13997 : fold_checksum_tree (argarray[i], &ctx, &ht);
13998 : md5_finish_ctx (&ctx, checksum_after_arglist);
13999 :
14000 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14001 : fold_check_failed (NULL_TREE, tem);
14002 : #endif
14003 57833141 : return tem;
14004 : }
14005 :
14006 : /* Perform constant folding and related simplification of initializer
14007 : expression EXPR. These behave identically to "fold_buildN" but ignore
14008 : potential run-time traps and exceptions that fold must preserve. */
14009 :
14010 : #define START_FOLD_INIT \
14011 : int saved_signaling_nans = flag_signaling_nans;\
14012 : int saved_trapping_math = flag_trapping_math;\
14013 : int saved_rounding_math = flag_rounding_math;\
14014 : int saved_trapv = flag_trapv;\
14015 : int saved_folding_initializer = folding_initializer;\
14016 : flag_signaling_nans = 0;\
14017 : flag_trapping_math = 0;\
14018 : flag_rounding_math = 0;\
14019 : flag_trapv = 0;\
14020 : folding_initializer = 1;
14021 :
14022 : #define END_FOLD_INIT \
14023 : flag_signaling_nans = saved_signaling_nans;\
14024 : flag_trapping_math = saved_trapping_math;\
14025 : flag_rounding_math = saved_rounding_math;\
14026 : flag_trapv = saved_trapv;\
14027 : folding_initializer = saved_folding_initializer;
14028 :
14029 : tree
14030 549444 : fold_init (tree expr)
14031 : {
14032 549444 : tree result;
14033 549444 : START_FOLD_INIT;
14034 :
14035 549444 : result = fold (expr);
14036 :
14037 549444 : END_FOLD_INIT;
14038 549444 : return result;
14039 : }
14040 :
14041 : tree
14042 2990436 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14043 : tree type, tree op)
14044 : {
14045 2990436 : tree result;
14046 2990436 : START_FOLD_INIT;
14047 :
14048 2990436 : result = fold_build1_loc (loc, code, type, op);
14049 :
14050 2990436 : END_FOLD_INIT;
14051 2990436 : return result;
14052 : }
14053 :
14054 : tree
14055 50593 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14056 : tree type, tree op0, tree op1)
14057 : {
14058 50593 : tree result;
14059 50593 : START_FOLD_INIT;
14060 :
14061 50593 : result = fold_build2_loc (loc, code, type, op0, op1);
14062 :
14063 50593 : END_FOLD_INIT;
14064 50593 : return result;
14065 : }
14066 :
14067 : tree
14068 3484 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14069 : int nargs, tree *argarray)
14070 : {
14071 3484 : tree result;
14072 3484 : START_FOLD_INIT;
14073 :
14074 3484 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14075 :
14076 3484 : END_FOLD_INIT;
14077 3484 : return result;
14078 : }
14079 :
14080 : tree
14081 68495197 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14082 : tree lhs, tree rhs)
14083 : {
14084 68495197 : tree result;
14085 68495197 : START_FOLD_INIT;
14086 :
14087 68495197 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14088 :
14089 68495197 : END_FOLD_INIT;
14090 68495197 : return result;
14091 : }
14092 :
14093 : #undef START_FOLD_INIT
14094 : #undef END_FOLD_INIT
14095 :
14096 : /* Determine if first argument is a multiple of second argument. Return
14097 : false if it is not, or we cannot easily determined it to be.
14098 :
14099 : An example of the sort of thing we care about (at this point; this routine
14100 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14101 : fold cases do now) is discovering that
14102 :
14103 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14104 :
14105 : is a multiple of
14106 :
14107 : SAVE_EXPR (J * 8)
14108 :
14109 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14110 :
14111 : This code also handles discovering that
14112 :
14113 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14114 :
14115 : is a multiple of 8 so we don't have to worry about dealing with a
14116 : possible remainder.
14117 :
14118 : Note that we *look* inside a SAVE_EXPR only to determine how it was
14119 : calculated; it is not safe for fold to do much of anything else with the
14120 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14121 : at run time. For example, the latter example above *cannot* be implemented
14122 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14123 : evaluation time of the original SAVE_EXPR is not necessarily the same at
14124 : the time the new expression is evaluated. The only optimization of this
14125 : sort that would be valid is changing
14126 :
14127 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14128 :
14129 : divided by 8 to
14130 :
14131 : SAVE_EXPR (I) * SAVE_EXPR (J)
14132 :
14133 : (where the same SAVE_EXPR (J) is used in the original and the
14134 : transformed version).
14135 :
14136 : NOWRAP specifies whether all outer operations in TYPE should
14137 : be considered not wrapping. Any type conversion within TOP acts
14138 : as a barrier and we will fall back to NOWRAP being false.
14139 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14140 : as not wrapping even though they are generally using unsigned arithmetic. */
14141 :
14142 : bool
14143 1583261 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14144 : {
14145 1636507 : gimple *stmt;
14146 1636507 : tree op1, op2;
14147 :
14148 1636507 : if (operand_equal_p (top, bottom, 0))
14149 : return true;
14150 :
14151 1123392 : if (TREE_CODE (type) != INTEGER_TYPE)
14152 : return false;
14153 :
14154 1123373 : switch (TREE_CODE (top))
14155 : {
14156 730 : case BIT_AND_EXPR:
14157 : /* Bitwise and provides a power of two multiple. If the mask is
14158 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14159 730 : if (!integer_pow2p (bottom))
14160 : return false;
14161 730 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14162 730 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14163 :
14164 415049 : case MULT_EXPR:
14165 : /* If the multiplication can wrap we cannot recurse further unless
14166 : the bottom is a power of two which is where wrapping does not
14167 : matter. */
14168 415049 : if (!nowrap
14169 14498 : && !TYPE_OVERFLOW_UNDEFINED (type)
14170 419348 : && !integer_pow2p (bottom))
14171 : return false;
14172 414553 : if (TREE_CODE (bottom) == INTEGER_CST)
14173 : {
14174 412833 : op1 = TREE_OPERAND (top, 0);
14175 412833 : op2 = TREE_OPERAND (top, 1);
14176 412833 : if (TREE_CODE (op1) == INTEGER_CST)
14177 0 : std::swap (op1, op2);
14178 412833 : if (TREE_CODE (op2) == INTEGER_CST)
14179 : {
14180 402487 : if (multiple_of_p (type, op2, bottom, nowrap))
14181 : return true;
14182 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14183 3308 : if (multiple_of_p (type, bottom, op2, nowrap))
14184 : {
14185 1909 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14186 1909 : wi::to_widest (op2));
14187 1909 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14188 : {
14189 1909 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14190 1909 : return multiple_of_p (type, op1, op2, nowrap);
14191 : }
14192 1909 : }
14193 : return multiple_of_p (type, op1, bottom, nowrap);
14194 : }
14195 : }
14196 12066 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14197 12066 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14198 :
14199 400 : case LSHIFT_EXPR:
14200 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14201 400 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14202 : {
14203 400 : op1 = TREE_OPERAND (top, 1);
14204 400 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14205 : {
14206 400 : wide_int mul_op
14207 400 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14208 800 : return multiple_of_p (type,
14209 800 : wide_int_to_tree (type, mul_op), bottom,
14210 : nowrap);
14211 400 : }
14212 : }
14213 : return false;
14214 :
14215 230717 : case MINUS_EXPR:
14216 230717 : case PLUS_EXPR:
14217 : /* If the addition or subtraction can wrap we cannot recurse further
14218 : unless bottom is a power of two which is where wrapping does not
14219 : matter. */
14220 230717 : if (!nowrap
14221 179061 : && !TYPE_OVERFLOW_UNDEFINED (type)
14222 408326 : && !integer_pow2p (bottom))
14223 : return false;
14224 :
14225 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14226 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14227 : but 0xfffffffd is not. */
14228 200619 : op1 = TREE_OPERAND (top, 1);
14229 200619 : if (TREE_CODE (top) == PLUS_EXPR
14230 194510 : && nowrap
14231 45633 : && TYPE_UNSIGNED (type)
14232 245516 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14233 28374 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14234 :
14235 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14236 : precisely, so be conservative here checking if both op0 and op1
14237 : are multiple of bottom. Note we check the second operand first
14238 : since it's usually simpler. */
14239 200619 : return (multiple_of_p (type, op1, bottom, nowrap)
14240 200619 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14241 :
14242 147781 : CASE_CONVERT:
14243 : /* Can't handle conversions from non-integral or wider integral type. */
14244 147781 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14245 147781 : || (TYPE_PRECISION (type)
14246 38929 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14247 : return false;
14248 : /* NOWRAP only extends to operations in the outermost type so
14249 : make sure to strip it off here. */
14250 38666 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14251 77332 : TREE_OPERAND (top, 0), bottom, false);
14252 :
14253 13181 : case SAVE_EXPR:
14254 13181 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14255 :
14256 86 : case COND_EXPR:
14257 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14258 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14259 :
14260 142945 : case INTEGER_CST:
14261 142945 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14262 : return false;
14263 140202 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14264 : SIGNED);
14265 :
14266 61148 : case SSA_NAME:
14267 61148 : if (TREE_CODE (bottom) == INTEGER_CST
14268 57996 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14269 119144 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14270 : {
14271 23802 : enum tree_code code = gimple_assign_rhs_code (stmt);
14272 :
14273 : /* Check for special cases to see if top is defined as multiple
14274 : of bottom:
14275 :
14276 : top = (X & ~(bottom - 1) ; bottom is power of 2
14277 :
14278 : or
14279 :
14280 : Y = X % bottom
14281 : top = X - Y. */
14282 23802 : if (code == BIT_AND_EXPR
14283 620 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14284 620 : && TREE_CODE (op2) == INTEGER_CST
14285 512 : && integer_pow2p (bottom)
14286 24314 : && wi::multiple_of_p (wi::to_widest (op2),
14287 512 : wi::to_widest (bottom), SIGNED))
14288 503 : return true;
14289 :
14290 23299 : op1 = gimple_assign_rhs1 (stmt);
14291 23299 : if (code == MINUS_EXPR
14292 2012 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14293 2012 : && TREE_CODE (op2) == SSA_NAME
14294 2012 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14295 2012 : && gimple_code (stmt) == GIMPLE_ASSIGN
14296 1650 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14297 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14298 23363 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14299 : return true;
14300 : }
14301 :
14302 : /* fall through */
14303 :
14304 : default:
14305 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14306 : return multiple_p (wi::to_poly_widest (top),
14307 : wi::to_poly_widest (bottom));
14308 :
14309 : return false;
14310 : }
14311 : }
14312 :
14313 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14314 : This function returns true for integer expressions, and returns
14315 : false if uncertain. */
14316 :
14317 : bool
14318 643595 : tree_expr_finite_p (const_tree x)
14319 : {
14320 643599 : machine_mode mode = element_mode (x);
14321 643599 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14322 : return true;
14323 643297 : switch (TREE_CODE (x))
14324 : {
14325 760 : case REAL_CST:
14326 760 : return real_isfinite (TREE_REAL_CST_PTR (x));
14327 0 : case COMPLEX_CST:
14328 0 : return tree_expr_finite_p (TREE_REALPART (x))
14329 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14330 : case FLOAT_EXPR:
14331 : return true;
14332 4 : case ABS_EXPR:
14333 4 : case CONVERT_EXPR:
14334 4 : case NON_LVALUE_EXPR:
14335 4 : case NEGATE_EXPR:
14336 4 : case SAVE_EXPR:
14337 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14338 0 : case MIN_EXPR:
14339 0 : case MAX_EXPR:
14340 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14341 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14342 0 : case COND_EXPR:
14343 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14344 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14345 38 : case CALL_EXPR:
14346 38 : switch (get_call_combined_fn (x))
14347 : {
14348 0 : CASE_CFN_FABS:
14349 0 : CASE_CFN_FABS_FN:
14350 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14351 0 : CASE_CFN_FMAX:
14352 0 : CASE_CFN_FMAX_FN:
14353 0 : CASE_CFN_FMIN:
14354 0 : CASE_CFN_FMIN_FN:
14355 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14356 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14357 : default:
14358 : return false;
14359 : }
14360 :
14361 : default:
14362 : return false;
14363 : }
14364 : }
14365 :
14366 : /* Return true if expression X evaluates to an infinity.
14367 : This function returns false for integer expressions. */
14368 :
14369 : bool
14370 1334944 : tree_expr_infinite_p (const_tree x)
14371 : {
14372 1335394 : if (!HONOR_INFINITIES (x))
14373 : return false;
14374 1335149 : switch (TREE_CODE (x))
14375 : {
14376 0 : case REAL_CST:
14377 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14378 450 : case ABS_EXPR:
14379 450 : case NEGATE_EXPR:
14380 450 : case NON_LVALUE_EXPR:
14381 450 : case SAVE_EXPR:
14382 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14383 0 : case COND_EXPR:
14384 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14385 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14386 : default:
14387 : return false;
14388 : }
14389 : }
14390 :
14391 : /* Return true if expression X could evaluate to an infinity.
14392 : This function returns false for integer expressions, and returns
14393 : true if uncertain. */
14394 :
14395 : bool
14396 811613 : tree_expr_maybe_infinite_p (const_tree x)
14397 : {
14398 811621 : if (!HONOR_INFINITIES (x))
14399 : return false;
14400 811142 : switch (TREE_CODE (x))
14401 : {
14402 365 : case REAL_CST:
14403 365 : return real_isinf (TREE_REAL_CST_PTR (x));
14404 : case FLOAT_EXPR:
14405 : return false;
14406 8 : case ABS_EXPR:
14407 8 : case NEGATE_EXPR:
14408 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14409 1 : case COND_EXPR:
14410 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14411 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14412 : default:
14413 : return true;
14414 : }
14415 : }
14416 :
14417 : /* Return true if expression X evaluates to a signaling NaN.
14418 : This function returns false for integer expressions. */
14419 :
14420 : bool
14421 421 : tree_expr_signaling_nan_p (const_tree x)
14422 : {
14423 421 : if (!HONOR_SNANS (x))
14424 : return false;
14425 116 : switch (TREE_CODE (x))
14426 : {
14427 116 : case REAL_CST:
14428 116 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14429 0 : case NON_LVALUE_EXPR:
14430 0 : case SAVE_EXPR:
14431 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14432 0 : case COND_EXPR:
14433 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14434 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14435 : default:
14436 : return false;
14437 : }
14438 : }
14439 :
14440 : /* Return true if expression X could evaluate to a signaling NaN.
14441 : This function returns false for integer expressions, and returns
14442 : true if uncertain. */
14443 :
14444 : bool
14445 723970 : tree_expr_maybe_signaling_nan_p (const_tree x)
14446 : {
14447 723970 : if (!HONOR_SNANS (x))
14448 : return false;
14449 5580 : switch (TREE_CODE (x))
14450 : {
14451 2138 : case REAL_CST:
14452 2138 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14453 : case PLUS_EXPR:
14454 : case MINUS_EXPR:
14455 : case MULT_EXPR:
14456 : case RDIV_EXPR:
14457 : case MIN_EXPR:
14458 : case MAX_EXPR:
14459 : case FIX_TRUNC_EXPR:
14460 : case FLOAT_EXPR:
14461 : return false;
14462 0 : case ABS_EXPR:
14463 0 : case CONVERT_EXPR:
14464 0 : case NEGATE_EXPR:
14465 0 : case NON_LVALUE_EXPR:
14466 0 : case SAVE_EXPR:
14467 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14468 0 : case COND_EXPR:
14469 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14470 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14471 0 : case CALL_EXPR:
14472 0 : switch (get_call_combined_fn (x))
14473 : {
14474 0 : CASE_CFN_FABS:
14475 0 : CASE_CFN_FABS_FN:
14476 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14477 0 : CASE_CFN_COPYSIGN:
14478 0 : CASE_CFN_COPYSIGN_FN:
14479 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14480 : CASE_CFN_ATAN:
14481 : CASE_CFN_ATAN_FN:
14482 : CASE_CFN_CEIL:
14483 : CASE_CFN_CEIL_FN:
14484 : CASE_CFN_COS:
14485 : CASE_CFN_COS_FN:
14486 : CASE_CFN_ERF:
14487 : CASE_CFN_ERF_FN:
14488 : CASE_CFN_EXP:
14489 : CASE_CFN_EXP_FN:
14490 : CASE_CFN_FLOOR:
14491 : CASE_CFN_FLOOR_FN:
14492 : CASE_CFN_FMAX:
14493 : CASE_CFN_FMAX_FN:
14494 : CASE_CFN_FMIN:
14495 : CASE_CFN_FMIN_FN:
14496 : CASE_CFN_FMOD:
14497 : CASE_CFN_FMOD_FN:
14498 : CASE_CFN_HYPOT:
14499 : CASE_CFN_HYPOT_FN:
14500 : CASE_CFN_MODF:
14501 : CASE_CFN_MODF_FN:
14502 : CASE_CFN_POW:
14503 : CASE_CFN_POW_FN:
14504 : CASE_CFN_ROUND:
14505 : CASE_CFN_ROUND_FN:
14506 : CASE_CFN_SIN:
14507 : CASE_CFN_SIN_FN:
14508 : CASE_CFN_SQRT:
14509 : CASE_CFN_SQRT_FN:
14510 : CASE_CFN_TAN:
14511 : CASE_CFN_TAN_FN:
14512 : CASE_CFN_TRUNC:
14513 : CASE_CFN_TRUNC_FN:
14514 : return false;
14515 : default:
14516 : return true;
14517 : }
14518 : default:
14519 : return true;
14520 : }
14521 : }
14522 :
14523 : /* Return true if expression X evaluates to a NaN.
14524 : This function returns false for integer expressions. */
14525 :
14526 : bool
14527 3937264 : tree_expr_nan_p (const_tree x)
14528 : {
14529 4292086 : if (!HONOR_NANS (x))
14530 : return false;
14531 4291736 : switch (TREE_CODE (x))
14532 : {
14533 3806 : case REAL_CST:
14534 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14535 354822 : case NON_LVALUE_EXPR:
14536 354822 : case SAVE_EXPR:
14537 354822 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14538 956 : case COND_EXPR:
14539 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14540 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14541 : default:
14542 : return false;
14543 : }
14544 : }
14545 :
14546 : /* Return true if expression X could evaluate to a NaN.
14547 : This function returns false for integer expressions, and returns
14548 : true if uncertain. */
14549 :
14550 : bool
14551 5157903 : tree_expr_maybe_nan_p (const_tree x)
14552 : {
14553 7548226 : if (!HONOR_NANS (x))
14554 : return false;
14555 7464993 : switch (TREE_CODE (x))
14556 : {
14557 3633 : case REAL_CST:
14558 3633 : return real_isnan (TREE_REAL_CST_PTR (x));
14559 : case FLOAT_EXPR:
14560 : return false;
14561 14832 : case PLUS_EXPR:
14562 14832 : case MINUS_EXPR:
14563 14832 : case MULT_EXPR:
14564 14832 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14565 14832 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14566 2390323 : case ABS_EXPR:
14567 2390323 : case CONVERT_EXPR:
14568 2390323 : case NEGATE_EXPR:
14569 2390323 : case NON_LVALUE_EXPR:
14570 2390323 : case SAVE_EXPR:
14571 2390323 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14572 176 : case MIN_EXPR:
14573 176 : case MAX_EXPR:
14574 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14575 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14576 603 : case COND_EXPR:
14577 603 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14578 603 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14579 1085 : case CALL_EXPR:
14580 1085 : switch (get_call_combined_fn (x))
14581 : {
14582 0 : CASE_CFN_FABS:
14583 0 : CASE_CFN_FABS_FN:
14584 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14585 108 : CASE_CFN_FMAX:
14586 108 : CASE_CFN_FMAX_FN:
14587 108 : CASE_CFN_FMIN:
14588 108 : CASE_CFN_FMIN_FN:
14589 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14590 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14591 : default:
14592 : return true;
14593 : }
14594 : default:
14595 : return true;
14596 : }
14597 : }
14598 :
14599 : /* Return true if expression X could evaluate to -0.0.
14600 : This function returns true if uncertain. */
14601 :
14602 : bool
14603 595855 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14604 : {
14605 595855 : if (!HONOR_SIGNED_ZEROS (x))
14606 : return false;
14607 595848 : switch (TREE_CODE (x))
14608 : {
14609 0 : case REAL_CST:
14610 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14611 : case INTEGER_CST:
14612 : case FLOAT_EXPR:
14613 : case ABS_EXPR:
14614 : return false;
14615 0 : case NON_LVALUE_EXPR:
14616 0 : case SAVE_EXPR:
14617 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14618 1 : case COND_EXPR:
14619 1 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14620 1 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14621 2 : case CALL_EXPR:
14622 2 : switch (get_call_combined_fn (x))
14623 : {
14624 : CASE_CFN_FABS:
14625 : CASE_CFN_FABS_FN:
14626 : return false;
14627 : default:
14628 : break;
14629 : }
14630 : default:
14631 : break;
14632 : }
14633 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14634 : * but currently those predicates require tree and not const_tree. */
14635 : return true;
14636 : }
14637 :
14638 : #define tree_expr_nonnegative_p(X, Y) \
14639 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14640 :
14641 : #define RECURSE(X) \
14642 : ((tree_expr_nonnegative_p) (X, depth + 1))
14643 :
14644 : /* Return true if CODE or TYPE is known to be non-negative. */
14645 :
14646 : static bool
14647 26717630 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14648 : {
14649 26717630 : if (!VECTOR_TYPE_P (type)
14650 26679445 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14651 53396810 : && truth_value_p (code))
14652 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14653 : have a signed:1 type (where the value is -1 and 0). */
14654 : return true;
14655 : return false;
14656 : }
14657 :
14658 : /* Return true if (CODE OP0) is known to be non-negative.
14659 : DEPTH is the current nesting depth of the query. */
14660 :
14661 : bool
14662 2439885 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14663 : {
14664 2439885 : if (TYPE_UNSIGNED (type))
14665 : return true;
14666 :
14667 1984237 : switch (code)
14668 : {
14669 283740 : case ABS_EXPR:
14670 : /* We can't return 1 if flag_wrapv is set because
14671 : ABS_EXPR<INT_MIN> = INT_MIN. */
14672 283740 : if (!ANY_INTEGRAL_TYPE_P (type))
14673 : return true;
14674 1176 : if (TYPE_OVERFLOW_UNDEFINED (type))
14675 : return true;
14676 : break;
14677 :
14678 74880 : case NON_LVALUE_EXPR:
14679 74880 : case FLOAT_EXPR:
14680 74880 : case FIX_TRUNC_EXPR:
14681 74880 : return RECURSE (op0);
14682 :
14683 1587119 : CASE_CONVERT:
14684 1587119 : {
14685 1587119 : tree inner_type = TREE_TYPE (op0);
14686 1587119 : tree outer_type = type;
14687 :
14688 1587119 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14689 : {
14690 405118 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14691 405118 : return RECURSE (op0);
14692 0 : if (INTEGRAL_TYPE_P (inner_type))
14693 : {
14694 0 : if (TYPE_UNSIGNED (inner_type))
14695 : return true;
14696 0 : return RECURSE (op0);
14697 : }
14698 : }
14699 1182001 : else if (INTEGRAL_TYPE_P (outer_type))
14700 : {
14701 1181924 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14702 0 : return RECURSE (op0);
14703 1181924 : if (INTEGRAL_TYPE_P (inner_type))
14704 1175576 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14705 1175576 : && TYPE_UNSIGNED (inner_type);
14706 : }
14707 : }
14708 : break;
14709 :
14710 38498 : default:
14711 38498 : return tree_simple_nonnegative_warnv_p (code, type);
14712 : }
14713 :
14714 : /* We don't know sign of `t', so be conservative and return false. */
14715 : return false;
14716 : }
14717 :
14718 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14719 : DEPTH is the current nesting depth of the query. */
14720 :
14721 : bool
14722 4781024 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14723 : tree op1, int depth)
14724 : {
14725 4781024 : if (TYPE_UNSIGNED (type))
14726 : return true;
14727 :
14728 4472597 : switch (code)
14729 : {
14730 1086721 : case POINTER_PLUS_EXPR:
14731 1086721 : case PLUS_EXPR:
14732 1086721 : if (FLOAT_TYPE_P (type))
14733 48180 : return RECURSE (op0) && RECURSE (op1);
14734 :
14735 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14736 : both unsigned and at least 2 bits shorter than the result. */
14737 1038541 : if (TREE_CODE (type) == INTEGER_TYPE
14738 1032457 : && TREE_CODE (op0) == NOP_EXPR
14739 17706 : && TREE_CODE (op1) == NOP_EXPR)
14740 : {
14741 205 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14742 205 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14743 205 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14744 306 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14745 : {
14746 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14747 95 : TYPE_PRECISION (inner2)) + 1;
14748 95 : return prec < TYPE_PRECISION (type);
14749 : }
14750 : }
14751 : break;
14752 :
14753 160931 : case MULT_EXPR:
14754 160931 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14755 : {
14756 : /* x * x is always non-negative for floating point x
14757 : or without overflow. */
14758 145025 : if (operand_equal_p (op0, op1, 0)
14759 145025 : || (RECURSE (op0) && RECURSE (op1)))
14760 : return true;
14761 : }
14762 :
14763 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14764 : both unsigned and their total bits is shorter than the result. */
14765 158792 : if (TREE_CODE (type) == INTEGER_TYPE
14766 88815 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14767 161 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14768 : {
14769 147 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14770 147 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14771 147 : : TREE_TYPE (op0);
14772 147 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14773 147 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14774 147 : : TREE_TYPE (op1);
14775 :
14776 147 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14777 147 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14778 :
14779 147 : if (TREE_CODE (op0) == INTEGER_CST)
14780 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14781 :
14782 147 : if (TREE_CODE (op1) == INTEGER_CST)
14783 70 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14784 :
14785 147 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14786 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14787 : {
14788 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14789 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14790 0 : : TYPE_PRECISION (inner0);
14791 :
14792 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14793 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14794 0 : : TYPE_PRECISION (inner1);
14795 :
14796 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14797 : }
14798 : }
14799 : return false;
14800 :
14801 9851 : case BIT_AND_EXPR:
14802 9851 : return RECURSE (op0) || RECURSE (op1);
14803 :
14804 35321 : case MAX_EXPR:
14805 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14806 : things. */
14807 35321 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14808 76 : return RECURSE (op0) && RECURSE (op1);
14809 35245 : return RECURSE (op0) || RECURSE (op1);
14810 :
14811 147974 : case BIT_IOR_EXPR:
14812 147974 : case BIT_XOR_EXPR:
14813 147974 : case MIN_EXPR:
14814 147974 : case RDIV_EXPR:
14815 147974 : case TRUNC_DIV_EXPR:
14816 147974 : case CEIL_DIV_EXPR:
14817 147974 : case FLOOR_DIV_EXPR:
14818 147974 : case ROUND_DIV_EXPR:
14819 147974 : return RECURSE (op0) && RECURSE (op1);
14820 :
14821 54167 : case TRUNC_MOD_EXPR:
14822 54167 : return RECURSE (op0);
14823 :
14824 252 : case FLOOR_MOD_EXPR:
14825 252 : return RECURSE (op1);
14826 :
14827 2977380 : case CEIL_MOD_EXPR:
14828 2977380 : case ROUND_MOD_EXPR:
14829 2977380 : default:
14830 2977380 : return tree_simple_nonnegative_warnv_p (code, type);
14831 : }
14832 :
14833 : /* We don't know sign of `t', so be conservative and return false. */
14834 : return false;
14835 : }
14836 :
14837 : /* Return true if T is known to be non-negative.
14838 : DEPTH is the current nesting depth of the query. */
14839 :
14840 : bool
14841 25572040 : tree_single_nonnegative_p (tree t, int depth)
14842 : {
14843 25572040 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14844 : return true;
14845 :
14846 21572280 : switch (TREE_CODE (t))
14847 : {
14848 2448927 : case INTEGER_CST:
14849 2448927 : return tree_int_cst_sgn (t) >= 0;
14850 :
14851 922841 : case REAL_CST:
14852 922841 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14853 :
14854 0 : case FIXED_CST:
14855 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14856 :
14857 928 : case COND_EXPR:
14858 928 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14859 :
14860 8690624 : case SSA_NAME:
14861 : /* For integral types, query the range if possible. */
14862 8690624 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14863 : {
14864 7267741 : int_range_max r;
14865 14535482 : get_range_query (cfun)->range_of_expr (r, t);
14866 7267741 : if (!r.undefined_p () && !r.varying_p())
14867 : {
14868 1764884 : if (r.nonnegative_p ())
14869 : return true;
14870 1164703 : if (r.nonpositive_p () && !range_includes_zero_p (r))
14871 : return false;
14872 : }
14873 7267741 : }
14874 : /* Limit the depth of recursion to avoid quadratic behavior.
14875 : This is expected to catch almost all occurrences in practice.
14876 : If this code misses important cases that unbounded recursion
14877 : would not, passes that need this information could be revised
14878 : to provide it through dataflow propagation. */
14879 8086692 : return (!name_registered_for_update_p (t)
14880 8086691 : && depth < param_max_ssa_name_query_depth
14881 16045737 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14882 :
14883 9508960 : default:
14884 9508960 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14885 : }
14886 : }
14887 :
14888 : /* Return true if T is known to be non-negative.
14889 : DEPTH is the current nesting depth of the query. */
14890 :
14891 : bool
14892 14234000 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14893 : int depth)
14894 : {
14895 14234000 : switch (fn)
14896 : {
14897 : CASE_CFN_ACOS:
14898 : CASE_CFN_ACOS_FN:
14899 : CASE_CFN_ACOSH:
14900 : CASE_CFN_ACOSH_FN:
14901 : CASE_CFN_ACOSPI:
14902 : CASE_CFN_ACOSPI_FN:
14903 : CASE_CFN_CABS:
14904 : CASE_CFN_CABS_FN:
14905 : CASE_CFN_COSH:
14906 : CASE_CFN_COSH_FN:
14907 : CASE_CFN_ERFC:
14908 : CASE_CFN_ERFC_FN:
14909 : CASE_CFN_EXP:
14910 : CASE_CFN_EXP_FN:
14911 : CASE_CFN_EXP10:
14912 : CASE_CFN_EXP2:
14913 : CASE_CFN_EXP2_FN:
14914 : CASE_CFN_FABS:
14915 : CASE_CFN_FABS_FN:
14916 : CASE_CFN_FDIM:
14917 : CASE_CFN_FDIM_FN:
14918 : CASE_CFN_HYPOT:
14919 : CASE_CFN_HYPOT_FN:
14920 : CASE_CFN_POW10:
14921 : CASE_CFN_FFS:
14922 : CASE_CFN_PARITY:
14923 : CASE_CFN_POPCOUNT:
14924 : CASE_CFN_CLRSB:
14925 : CASE_CFN_BSWAP:
14926 : CASE_CFN_BITREVERSE:
14927 : case CFN_CLMUL:
14928 : /* Always true. */
14929 : return true;
14930 :
14931 176 : CASE_CFN_CLZ:
14932 176 : CASE_CFN_CTZ:
14933 176 : if (arg1)
14934 2 : return RECURSE (arg1);
14935 : return true;
14936 :
14937 847 : CASE_CFN_SQRT:
14938 847 : CASE_CFN_SQRT_FN:
14939 : /* sqrt(-0.0) is -0.0. */
14940 847 : if (!HONOR_SIGNED_ZEROS (type))
14941 : return true;
14942 815 : return RECURSE (arg0);
14943 :
14944 23708 : CASE_CFN_ASINH:
14945 23708 : CASE_CFN_ASINH_FN:
14946 23708 : CASE_CFN_ASINPI:
14947 23708 : CASE_CFN_ASINPI_FN:
14948 23708 : CASE_CFN_ATAN:
14949 23708 : CASE_CFN_ATAN_FN:
14950 23708 : CASE_CFN_ATANH:
14951 23708 : CASE_CFN_ATANH_FN:
14952 23708 : CASE_CFN_ATANPI:
14953 23708 : CASE_CFN_ATANPI_FN:
14954 23708 : CASE_CFN_CBRT:
14955 23708 : CASE_CFN_CBRT_FN:
14956 23708 : CASE_CFN_CEIL:
14957 23708 : CASE_CFN_CEIL_FN:
14958 23708 : CASE_CFN_ERF:
14959 23708 : CASE_CFN_ERF_FN:
14960 23708 : CASE_CFN_EXPM1:
14961 23708 : CASE_CFN_EXPM1_FN:
14962 23708 : CASE_CFN_FLOOR:
14963 23708 : CASE_CFN_FLOOR_FN:
14964 23708 : CASE_CFN_FMOD:
14965 23708 : CASE_CFN_FMOD_FN:
14966 23708 : CASE_CFN_FREXP:
14967 23708 : CASE_CFN_FREXP_FN:
14968 23708 : CASE_CFN_ICEIL:
14969 23708 : CASE_CFN_IFLOOR:
14970 23708 : CASE_CFN_IRINT:
14971 23708 : CASE_CFN_IROUND:
14972 23708 : CASE_CFN_LCEIL:
14973 23708 : CASE_CFN_LDEXP:
14974 23708 : CASE_CFN_LFLOOR:
14975 23708 : CASE_CFN_LLCEIL:
14976 23708 : CASE_CFN_LLFLOOR:
14977 23708 : CASE_CFN_LLRINT:
14978 23708 : CASE_CFN_LLRINT_FN:
14979 23708 : CASE_CFN_LLROUND:
14980 23708 : CASE_CFN_LLROUND_FN:
14981 23708 : CASE_CFN_LRINT:
14982 23708 : CASE_CFN_LRINT_FN:
14983 23708 : CASE_CFN_LROUND:
14984 23708 : CASE_CFN_LROUND_FN:
14985 23708 : CASE_CFN_MODF:
14986 23708 : CASE_CFN_MODF_FN:
14987 23708 : CASE_CFN_NEARBYINT:
14988 23708 : CASE_CFN_NEARBYINT_FN:
14989 23708 : CASE_CFN_RINT:
14990 23708 : CASE_CFN_RINT_FN:
14991 23708 : CASE_CFN_ROUND:
14992 23708 : CASE_CFN_ROUND_FN:
14993 23708 : CASE_CFN_ROUNDEVEN:
14994 23708 : CASE_CFN_ROUNDEVEN_FN:
14995 23708 : CASE_CFN_SCALB:
14996 23708 : CASE_CFN_SCALBLN:
14997 23708 : CASE_CFN_SCALBLN_FN:
14998 23708 : CASE_CFN_SCALBN:
14999 23708 : CASE_CFN_SCALBN_FN:
15000 23708 : CASE_CFN_SIGNBIT:
15001 23708 : CASE_CFN_SIGNIFICAND:
15002 23708 : CASE_CFN_SINH:
15003 23708 : CASE_CFN_SINH_FN:
15004 23708 : CASE_CFN_TANH:
15005 23708 : CASE_CFN_TANH_FN:
15006 23708 : CASE_CFN_TRUNC:
15007 23708 : CASE_CFN_TRUNC_FN:
15008 : /* True if the 1st argument is nonnegative. */
15009 23708 : return RECURSE (arg0);
15010 :
15011 1329 : CASE_CFN_FMAX:
15012 1329 : CASE_CFN_FMAX_FN:
15013 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15014 : things. In the presence of sNaNs, we're only guaranteed to be
15015 : non-negative if both operands are non-negative. In the presence
15016 : of qNaNs, we're non-negative if either operand is non-negative
15017 : and can't be a qNaN, or if both operands are non-negative. */
15018 1329 : if (tree_expr_maybe_signaling_nan_p (arg0)
15019 1329 : || tree_expr_maybe_signaling_nan_p (arg1))
15020 136 : return RECURSE (arg0) && RECURSE (arg1);
15021 1193 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15022 332 : || RECURSE (arg1))
15023 861 : : (RECURSE (arg1)
15024 861 : && !tree_expr_maybe_nan_p (arg1));
15025 :
15026 910 : CASE_CFN_FMIN:
15027 910 : CASE_CFN_FMIN_FN:
15028 : /* True if the 1st AND 2nd arguments are nonnegative. */
15029 910 : return RECURSE (arg0) && RECURSE (arg1);
15030 :
15031 825 : CASE_CFN_COPYSIGN:
15032 825 : CASE_CFN_COPYSIGN_FN:
15033 : /* True if the 2nd argument is nonnegative. */
15034 825 : return RECURSE (arg1);
15035 :
15036 2315 : CASE_CFN_POWI:
15037 : /* True if the 1st argument is nonnegative or the second
15038 : argument is an even integer. */
15039 2315 : if (TREE_CODE (arg1) == INTEGER_CST
15040 2315 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15041 : return true;
15042 2232 : return RECURSE (arg0);
15043 :
15044 5533 : CASE_CFN_POW:
15045 5533 : CASE_CFN_POW_FN:
15046 : /* True if the 1st argument is nonnegative or the second
15047 : argument is an even integer valued real. */
15048 5533 : if (TREE_CODE (arg1) == REAL_CST)
15049 : {
15050 2254 : REAL_VALUE_TYPE c;
15051 2254 : HOST_WIDE_INT n;
15052 :
15053 2254 : c = TREE_REAL_CST (arg1);
15054 2254 : n = real_to_integer (&c);
15055 2254 : if ((n & 1) == 0)
15056 : {
15057 1615 : REAL_VALUE_TYPE cint;
15058 1615 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15059 1615 : if (real_identical (&c, &cint))
15060 568 : return true;
15061 : }
15062 : }
15063 4965 : return RECURSE (arg0);
15064 :
15065 14189962 : default:
15066 14189962 : break;
15067 : }
15068 14189962 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15069 : }
15070 :
15071 : /* Return true if T is known to be non-negative.
15072 : DEPTH is the current nesting depth of the query. */
15073 :
15074 : static bool
15075 1720545 : tree_invalid_nonnegative_p (tree t, int depth)
15076 : {
15077 1720545 : enum tree_code code = TREE_CODE (t);
15078 1720545 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15079 : return true;
15080 :
15081 1307399 : switch (code)
15082 : {
15083 269 : case TARGET_EXPR:
15084 269 : {
15085 269 : tree temp = TARGET_EXPR_SLOT (t);
15086 269 : t = TARGET_EXPR_INITIAL (t);
15087 :
15088 : /* If the initializer is non-void, then it's a normal expression
15089 : that will be assigned to the slot. */
15090 269 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15091 66 : return RECURSE (t);
15092 :
15093 : /* Otherwise, the initializer sets the slot in some way. One common
15094 : way is an assignment statement at the end of the initializer. */
15095 406 : while (1)
15096 : {
15097 406 : if (TREE_CODE (t) == BIND_EXPR)
15098 203 : t = expr_last (BIND_EXPR_BODY (t));
15099 203 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15100 203 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15101 0 : t = expr_last (TREE_OPERAND (t, 0));
15102 203 : else if (TREE_CODE (t) == STATEMENT_LIST)
15103 0 : t = expr_last (t);
15104 : else
15105 : break;
15106 : }
15107 203 : if (TREE_CODE (t) == MODIFY_EXPR
15108 203 : && TREE_OPERAND (t, 0) == temp)
15109 203 : return RECURSE (TREE_OPERAND (t, 1));
15110 :
15111 : return false;
15112 : }
15113 :
15114 635265 : case CALL_EXPR:
15115 635265 : {
15116 635265 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15117 635265 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15118 :
15119 635265 : return tree_call_nonnegative_p (TREE_TYPE (t),
15120 : get_call_combined_fn (t),
15121 : arg0,
15122 : arg1,
15123 635265 : depth);
15124 : }
15125 3390 : case COMPOUND_EXPR:
15126 3390 : case MODIFY_EXPR:
15127 3390 : return RECURSE (TREE_OPERAND (t, 1));
15128 :
15129 15 : case BIND_EXPR:
15130 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15131 :
15132 665630 : case SAVE_EXPR:
15133 665630 : return RECURSE (TREE_OPERAND (t, 0));
15134 :
15135 2830 : default:
15136 2830 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15137 : }
15138 : }
15139 :
15140 : #undef RECURSE
15141 : #undef tree_expr_nonnegative_p
15142 :
15143 : /* Return true if T is known to be non-negative.
15144 : DEPTH is the current nesting depth of the query. */
15145 :
15146 : bool
15147 27065025 : tree_expr_nonnegative_p (tree t, int depth)
15148 : {
15149 27065025 : enum tree_code code;
15150 27065025 : if (error_operand_p (t))
15151 : return false;
15152 :
15153 27065024 : code = TREE_CODE (t);
15154 27065024 : switch (TREE_CODE_CLASS (code))
15155 : {
15156 1378342 : case tcc_binary:
15157 1378342 : case tcc_comparison:
15158 1378342 : return tree_binary_nonnegative_p (TREE_CODE (t),
15159 1378342 : TREE_TYPE (t),
15160 1378342 : TREE_OPERAND (t, 0),
15161 1378342 : TREE_OPERAND (t, 1),
15162 1378342 : depth);
15163 :
15164 1971218 : case tcc_unary:
15165 1971218 : return tree_unary_nonnegative_p (TREE_CODE (t),
15166 1971218 : TREE_TYPE (t),
15167 1971218 : TREE_OPERAND (t, 0),
15168 1971218 : depth);
15169 :
15170 13499179 : case tcc_constant:
15171 13499179 : case tcc_declaration:
15172 13499179 : case tcc_reference:
15173 13499179 : return tree_single_nonnegative_p (t, depth);
15174 :
15175 10216285 : default:
15176 10216285 : break;
15177 : }
15178 :
15179 10216285 : switch (code)
15180 : {
15181 7 : case TRUTH_AND_EXPR:
15182 7 : case TRUTH_OR_EXPR:
15183 7 : case TRUTH_XOR_EXPR:
15184 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15185 7 : TREE_TYPE (t),
15186 7 : TREE_OPERAND (t, 0),
15187 7 : TREE_OPERAND (t, 1),
15188 7 : depth);
15189 72 : case TRUTH_NOT_EXPR:
15190 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15191 72 : TREE_TYPE (t),
15192 72 : TREE_OPERAND (t, 0),
15193 72 : depth);
15194 :
15195 8495661 : case COND_EXPR:
15196 8495661 : case CONSTRUCTOR:
15197 8495661 : case OBJ_TYPE_REF:
15198 8495661 : case ADDR_EXPR:
15199 8495661 : case WITH_SIZE_EXPR:
15200 8495661 : case SSA_NAME:
15201 8495661 : return tree_single_nonnegative_p (t, depth);
15202 :
15203 1720545 : default:
15204 1720545 : return tree_invalid_nonnegative_p (t, depth);
15205 : }
15206 : }
15207 :
15208 :
15209 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15210 : For floating point we further ensure that T is not denormal.
15211 : Similar logic is present in nonzero_address in rtlanal.h. */
15212 :
15213 : bool
15214 1600347 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15215 : {
15216 1600347 : switch (code)
15217 : {
15218 1 : case ABS_EXPR:
15219 1 : return tree_expr_nonzero_p (op0);
15220 :
15221 924457 : case NOP_EXPR:
15222 924457 : {
15223 924457 : tree inner_type = TREE_TYPE (op0);
15224 924457 : tree outer_type = type;
15225 :
15226 924457 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15227 924457 : && tree_expr_nonzero_p (op0));
15228 : }
15229 28194 : break;
15230 :
15231 28194 : case NON_LVALUE_EXPR:
15232 28194 : return tree_expr_nonzero_p (op0);
15233 :
15234 : default:
15235 : break;
15236 : }
15237 :
15238 : return false;
15239 : }
15240 :
15241 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15242 : For floating point we further ensure that T is not denormal.
15243 : Similar logic is present in nonzero_address in rtlanal.h. */
15244 :
15245 : bool
15246 2982709 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15247 : {
15248 2982709 : switch (code)
15249 : {
15250 472331 : case POINTER_PLUS_EXPR:
15251 472331 : case PLUS_EXPR:
15252 472331 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15253 : {
15254 : /* With the presence of negative values it is hard
15255 : to say something. */
15256 115080 : if (!tree_expr_nonnegative_p (op0)
15257 115080 : || !tree_expr_nonnegative_p (op1))
15258 : return false;
15259 : /* One of operands must be positive and the other non-negative. */
15260 3526 : return (tree_expr_nonzero_p (op0)
15261 3526 : || tree_expr_nonzero_p (op1));
15262 : }
15263 : break;
15264 :
15265 31168 : case MULT_EXPR:
15266 31168 : if (TYPE_OVERFLOW_UNDEFINED (type))
15267 : {
15268 556 : if (tree_expr_nonzero_p (op0)
15269 556 : && tree_expr_nonzero_p (op1))
15270 : return true;
15271 : }
15272 : break;
15273 :
15274 : case MIN_EXPR:
15275 : break;
15276 :
15277 43 : case MAX_EXPR:
15278 43 : if (tree_expr_nonzero_p (op0))
15279 : {
15280 :
15281 : /* When both operands are nonzero, then MAX must be too. */
15282 0 : if (tree_expr_nonzero_p (op1))
15283 : return true;
15284 :
15285 : /* MAX where operand 0 is positive is positive. */
15286 0 : return tree_expr_nonnegative_p (op0);
15287 : }
15288 : /* MAX where operand 1 is positive is positive. */
15289 43 : else if (tree_expr_nonzero_p (op1)
15290 43 : && tree_expr_nonnegative_p (op1))
15291 : return true;
15292 : break;
15293 :
15294 270003 : case BIT_IOR_EXPR:
15295 270003 : return (tree_expr_nonzero_p (op1)
15296 270003 : || tree_expr_nonzero_p (op0));
15297 :
15298 : default:
15299 : break;
15300 : }
15301 :
15302 : return false;
15303 : }
15304 :
15305 : /* Return true when T is an address and is known to be nonzero.
15306 : For floating point we further ensure that T is not denormal.
15307 : Similar logic is present in nonzero_address in rtlanal.h. */
15308 :
15309 : bool
15310 155141665 : tree_single_nonzero_p (tree t)
15311 : {
15312 155141665 : switch (TREE_CODE (t))
15313 : {
15314 1149486 : case INTEGER_CST:
15315 1149486 : return !integer_zerop (t);
15316 :
15317 12179430 : case ADDR_EXPR:
15318 12179430 : {
15319 12179430 : tree base = TREE_OPERAND (t, 0);
15320 :
15321 12179430 : if (!DECL_P (base))
15322 6007838 : base = get_base_address (base);
15323 :
15324 12179430 : if (base && TREE_CODE (base) == TARGET_EXPR)
15325 844 : base = TARGET_EXPR_SLOT (base);
15326 :
15327 844 : if (!base)
15328 : return false;
15329 :
15330 : /* For objects in symbol table check if we know they are non-zero.
15331 : Don't do anything for variables and functions before symtab is built;
15332 : it is quite possible that they will be declared weak later. */
15333 12179430 : int nonzero_addr = maybe_nonzero_address (base);
15334 12179430 : if (nonzero_addr >= 0)
15335 9858474 : return nonzero_addr;
15336 :
15337 : /* Constants are never weak. */
15338 2320956 : if (CONSTANT_CLASS_P (base))
15339 2226348 : return true;
15340 :
15341 : return false;
15342 : }
15343 :
15344 38114 : case COND_EXPR:
15345 38114 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15346 38114 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15347 : return true;
15348 : break;
15349 :
15350 129695968 : case SSA_NAME:
15351 129695968 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15352 : break;
15353 101037924 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15354 :
15355 : default:
15356 : break;
15357 : }
15358 : return false;
15359 : }
15360 :
15361 : #define integer_valued_real_p(X) \
15362 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15363 :
15364 : #define RECURSE(X) \
15365 : ((integer_valued_real_p) (X, depth + 1))
15366 :
15367 : /* Return true if the floating point result of (CODE OP0) has an
15368 : integer value. We also allow +Inf, -Inf and NaN to be considered
15369 : integer values. Return false for signaling NaN.
15370 :
15371 : DEPTH is the current nesting depth of the query. */
15372 :
15373 : bool
15374 15211 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15375 : {
15376 15211 : switch (code)
15377 : {
15378 : case FLOAT_EXPR:
15379 : return true;
15380 :
15381 1400 : case ABS_EXPR:
15382 1400 : return RECURSE (op0);
15383 :
15384 9859 : CASE_CONVERT:
15385 9859 : {
15386 9859 : tree type = TREE_TYPE (op0);
15387 9859 : if (TREE_CODE (type) == INTEGER_TYPE)
15388 : return true;
15389 9859 : if (SCALAR_FLOAT_TYPE_P (type))
15390 9859 : return RECURSE (op0);
15391 : break;
15392 : }
15393 :
15394 : default:
15395 : break;
15396 : }
15397 : return false;
15398 : }
15399 :
15400 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15401 : integer value. We also allow +Inf, -Inf and NaN to be considered
15402 : integer values. Return false for signaling NaN.
15403 :
15404 : DEPTH is the current nesting depth of the query. */
15405 :
15406 : bool
15407 13863 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15408 : {
15409 13863 : switch (code)
15410 : {
15411 8070 : case PLUS_EXPR:
15412 8070 : case MINUS_EXPR:
15413 8070 : case MULT_EXPR:
15414 8070 : case MIN_EXPR:
15415 8070 : case MAX_EXPR:
15416 8070 : return RECURSE (op0) && RECURSE (op1);
15417 :
15418 : default:
15419 : break;
15420 : }
15421 : return false;
15422 : }
15423 :
15424 : /* Return true if the floating point result of calling FNDECL with arguments
15425 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15426 : considered integer values. Return false for signaling NaN. If FNDECL
15427 : takes fewer than 2 arguments, the remaining ARGn are null.
15428 :
15429 : DEPTH is the current nesting depth of the query. */
15430 :
15431 : bool
15432 1125 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15433 : {
15434 1125 : switch (fn)
15435 : {
15436 : CASE_CFN_CEIL:
15437 : CASE_CFN_CEIL_FN:
15438 : CASE_CFN_FLOOR:
15439 : CASE_CFN_FLOOR_FN:
15440 : CASE_CFN_NEARBYINT:
15441 : CASE_CFN_NEARBYINT_FN:
15442 : CASE_CFN_RINT:
15443 : CASE_CFN_RINT_FN:
15444 : CASE_CFN_ROUND:
15445 : CASE_CFN_ROUND_FN:
15446 : CASE_CFN_ROUNDEVEN:
15447 : CASE_CFN_ROUNDEVEN_FN:
15448 : CASE_CFN_TRUNC:
15449 : CASE_CFN_TRUNC_FN:
15450 : return true;
15451 :
15452 336 : CASE_CFN_FMIN:
15453 336 : CASE_CFN_FMIN_FN:
15454 336 : CASE_CFN_FMAX:
15455 336 : CASE_CFN_FMAX_FN:
15456 336 : return RECURSE (arg0) && RECURSE (arg1);
15457 :
15458 : default:
15459 : break;
15460 : }
15461 : return false;
15462 : }
15463 :
15464 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15465 : has an integer value. We also allow +Inf, -Inf and NaN to be
15466 : considered integer values. Return false for signaling NaN.
15467 :
15468 : DEPTH is the current nesting depth of the query. */
15469 :
15470 : bool
15471 130468 : integer_valued_real_single_p (tree t, int depth)
15472 : {
15473 130468 : switch (TREE_CODE (t))
15474 : {
15475 2593 : case REAL_CST:
15476 2593 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15477 :
15478 0 : case COND_EXPR:
15479 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15480 :
15481 91392 : case SSA_NAME:
15482 : /* Limit the depth of recursion to avoid quadratic behavior.
15483 : This is expected to catch almost all occurrences in practice.
15484 : If this code misses important cases that unbounded recursion
15485 : would not, passes that need this information could be revised
15486 : to provide it through dataflow propagation. */
15487 91392 : return (!name_registered_for_update_p (t)
15488 91392 : && depth < param_max_ssa_name_query_depth
15489 181969 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15490 : depth));
15491 :
15492 : default:
15493 : break;
15494 : }
15495 : return false;
15496 : }
15497 :
15498 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15499 : has an integer value. We also allow +Inf, -Inf and NaN to be
15500 : considered integer values. Return false for signaling NaN.
15501 :
15502 : DEPTH is the current nesting depth of the query. */
15503 :
15504 : static bool
15505 0 : integer_valued_real_invalid_p (tree t, int depth)
15506 : {
15507 0 : switch (TREE_CODE (t))
15508 : {
15509 0 : case COMPOUND_EXPR:
15510 0 : case MODIFY_EXPR:
15511 0 : case BIND_EXPR:
15512 0 : return RECURSE (TREE_OPERAND (t, 1));
15513 :
15514 0 : case SAVE_EXPR:
15515 0 : return RECURSE (TREE_OPERAND (t, 0));
15516 :
15517 : default:
15518 : break;
15519 : }
15520 : return false;
15521 : }
15522 :
15523 : #undef RECURSE
15524 : #undef integer_valued_real_p
15525 :
15526 : /* Return true if the floating point expression T has an integer value.
15527 : We also allow +Inf, -Inf and NaN to be considered integer values.
15528 : Return false for signaling NaN.
15529 :
15530 : DEPTH is the current nesting depth of the query. */
15531 :
15532 : bool
15533 98530 : integer_valued_real_p (tree t, int depth)
15534 : {
15535 98530 : if (t == error_mark_node)
15536 : return false;
15537 :
15538 98530 : STRIP_ANY_LOCATION_WRAPPER (t);
15539 :
15540 98530 : tree_code code = TREE_CODE (t);
15541 98530 : switch (TREE_CODE_CLASS (code))
15542 : {
15543 0 : case tcc_binary:
15544 0 : case tcc_comparison:
15545 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15546 0 : TREE_OPERAND (t, 1), depth);
15547 :
15548 0 : case tcc_unary:
15549 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15550 :
15551 8779 : case tcc_constant:
15552 8779 : case tcc_declaration:
15553 8779 : case tcc_reference:
15554 8779 : return integer_valued_real_single_p (t, depth);
15555 :
15556 89751 : default:
15557 89751 : break;
15558 : }
15559 :
15560 89751 : switch (code)
15561 : {
15562 89751 : case COND_EXPR:
15563 89751 : case SSA_NAME:
15564 89751 : return integer_valued_real_single_p (t, depth);
15565 :
15566 0 : case CALL_EXPR:
15567 0 : {
15568 0 : tree arg0 = (call_expr_nargs (t) > 0
15569 0 : ? CALL_EXPR_ARG (t, 0)
15570 0 : : NULL_TREE);
15571 0 : tree arg1 = (call_expr_nargs (t) > 1
15572 0 : ? CALL_EXPR_ARG (t, 1)
15573 0 : : NULL_TREE);
15574 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15575 0 : arg0, arg1, depth);
15576 : }
15577 :
15578 0 : default:
15579 0 : return integer_valued_real_invalid_p (t, depth);
15580 : }
15581 : }
15582 :
15583 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15584 : attempt to fold the expression to a constant without modifying TYPE,
15585 : OP0 or OP1.
15586 :
15587 : If the expression could be simplified to a constant, then return
15588 : the constant. If the expression would not be simplified to a
15589 : constant, then return NULL_TREE. */
15590 :
15591 : tree
15592 16177327 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15593 : {
15594 16177327 : tree tem = fold_binary (code, type, op0, op1);
15595 16177327 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15596 : }
15597 :
15598 : /* Given the components of a unary expression CODE, TYPE and OP0,
15599 : attempt to fold the expression to a constant without modifying
15600 : TYPE or OP0.
15601 :
15602 : If the expression could be simplified to a constant, then return
15603 : the constant. If the expression would not be simplified to a
15604 : constant, then return NULL_TREE. */
15605 :
15606 : tree
15607 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15608 : {
15609 0 : tree tem = fold_unary (code, type, op0);
15610 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15611 : }
15612 :
15613 : /* If EXP represents referencing an element in a constant string
15614 : (either via pointer arithmetic or array indexing), return the
15615 : tree representing the value accessed, otherwise return NULL. */
15616 :
15617 : tree
15618 218626098 : fold_read_from_constant_string (tree exp)
15619 : {
15620 218626098 : if ((INDIRECT_REF_P (exp)
15621 218626079 : || TREE_CODE (exp) == ARRAY_REF)
15622 232238029 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15623 : {
15624 10360287 : tree exp1 = TREE_OPERAND (exp, 0);
15625 10360287 : tree index;
15626 10360287 : tree string;
15627 10360287 : location_t loc = EXPR_LOCATION (exp);
15628 :
15629 10360287 : if (INDIRECT_REF_P (exp))
15630 0 : string = string_constant (exp1, &index, NULL, NULL);
15631 : else
15632 : {
15633 10360287 : tree low_bound = array_ref_low_bound (exp);
15634 10360287 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15635 :
15636 : /* Optimize the special-case of a zero lower bound.
15637 :
15638 : We convert the low_bound to sizetype to avoid some problems
15639 : with constant folding. (E.g. suppose the lower bound is 1,
15640 : and its mode is QI. Without the conversion,l (ARRAY
15641 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15642 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15643 10360287 : if (! integer_zerop (low_bound))
15644 158711 : index = size_diffop_loc (loc, index,
15645 : fold_convert_loc (loc, sizetype, low_bound));
15646 :
15647 : string = exp1;
15648 : }
15649 :
15650 10360287 : scalar_int_mode char_mode;
15651 10360287 : if (string
15652 10360287 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15653 10360287 : && TREE_CODE (string) == STRING_CST
15654 263755 : && tree_fits_uhwi_p (index)
15655 259843 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15656 10619968 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15657 : &char_mode)
15658 20720574 : && GET_MODE_SIZE (char_mode) == 1)
15659 516814 : return build_int_cst_type (TREE_TYPE (exp),
15660 258407 : (TREE_STRING_POINTER (string)
15661 258407 : [TREE_INT_CST_LOW (index)]));
15662 : }
15663 : return NULL;
15664 : }
15665 :
15666 : /* Folds a read from vector element at IDX of vector ARG. */
15667 :
15668 : tree
15669 9683 : fold_read_from_vector (tree arg, poly_uint64 idx)
15670 : {
15671 9683 : unsigned HOST_WIDE_INT i;
15672 9683 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15673 9683 : && known_ge (idx, 0u)
15674 9683 : && idx.is_constant (&i))
15675 : {
15676 9683 : if (TREE_CODE (arg) == VECTOR_CST)
15677 2020 : return VECTOR_CST_ELT (arg, i);
15678 7663 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15679 : {
15680 2561 : if (CONSTRUCTOR_NELTS (arg)
15681 2521 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15682 : return NULL_TREE;
15683 1723 : if (i >= CONSTRUCTOR_NELTS (arg))
15684 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15685 1683 : return CONSTRUCTOR_ELT (arg, i)->value;
15686 : }
15687 : }
15688 : return NULL_TREE;
15689 : }
15690 :
15691 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15692 : an integer constant, real, or fixed-point constant.
15693 :
15694 : TYPE is the type of the result. */
15695 :
15696 : static tree
15697 33187891 : fold_negate_const (tree arg0, tree type)
15698 : {
15699 33187891 : tree t = NULL_TREE;
15700 :
15701 33187891 : switch (TREE_CODE (arg0))
15702 : {
15703 2055153 : case REAL_CST:
15704 2055153 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15705 2055153 : break;
15706 :
15707 0 : case FIXED_CST:
15708 0 : {
15709 0 : FIXED_VALUE_TYPE f;
15710 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15711 0 : &(TREE_FIXED_CST (arg0)), NULL,
15712 0 : TYPE_SATURATING (type));
15713 0 : t = build_fixed (type, f);
15714 : /* Propagate overflow flags. */
15715 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15716 0 : TREE_OVERFLOW (t) = 1;
15717 0 : break;
15718 : }
15719 :
15720 31132738 : default:
15721 31132738 : if (poly_int_tree_p (arg0))
15722 : {
15723 31132738 : wi::overflow_type overflow;
15724 31132738 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15725 31132738 : t = force_fit_type (type, res, 1,
15726 31132738 : (overflow && ! TYPE_UNSIGNED (type))
15727 31122734 : || TREE_OVERFLOW (arg0));
15728 31132738 : break;
15729 31132738 : }
15730 :
15731 0 : gcc_unreachable ();
15732 : }
15733 :
15734 33187891 : return t;
15735 : }
15736 :
15737 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15738 : an integer constant or real constant.
15739 :
15740 : TYPE is the type of the result. */
15741 :
15742 : tree
15743 36149 : fold_abs_const (tree arg0, tree type)
15744 : {
15745 36149 : tree t = NULL_TREE;
15746 :
15747 36149 : switch (TREE_CODE (arg0))
15748 : {
15749 6619 : case INTEGER_CST:
15750 6619 : {
15751 : /* If the value is unsigned or non-negative, then the absolute value
15752 : is the same as the ordinary value. */
15753 6619 : wide_int val = wi::to_wide (arg0);
15754 6619 : wi::overflow_type overflow = wi::OVF_NONE;
15755 6619 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15756 : ;
15757 :
15758 : /* If the value is negative, then the absolute value is
15759 : its negation. */
15760 : else
15761 3015 : val = wi::neg (val, &overflow);
15762 :
15763 : /* Force to the destination type, set TREE_OVERFLOW for signed
15764 : TYPE only. */
15765 6619 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15766 6619 : }
15767 6619 : break;
15768 :
15769 29530 : case REAL_CST:
15770 29530 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15771 7505 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15772 : else
15773 : t = arg0;
15774 : break;
15775 :
15776 0 : default:
15777 0 : gcc_unreachable ();
15778 : }
15779 :
15780 36149 : return t;
15781 : }
15782 :
15783 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15784 : constant. TYPE is the type of the result. */
15785 :
15786 : static tree
15787 2389043 : fold_not_const (const_tree arg0, tree type)
15788 : {
15789 2389043 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15790 :
15791 2389043 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15792 : }
15793 :
15794 : /* Given CODE, a relational operator, the target type, TYPE and two
15795 : constant operands OP0 and OP1, return the result of the
15796 : relational operation. If the result is not a compile time
15797 : constant, then return NULL_TREE. */
15798 :
15799 : static tree
15800 83985932 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15801 : {
15802 83985932 : int result, invert;
15803 :
15804 : /* From here on, the only cases we handle are when the result is
15805 : known to be a constant. */
15806 :
15807 83985932 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15808 : {
15809 1225267 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15810 1225267 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15811 :
15812 : /* Handle the cases where either operand is a NaN. */
15813 1225267 : if (real_isnan (c0) || real_isnan (c1))
15814 : {
15815 13705 : switch (code)
15816 : {
15817 : case EQ_EXPR:
15818 : case ORDERED_EXPR:
15819 : result = 0;
15820 : break;
15821 :
15822 : case NE_EXPR:
15823 : case UNORDERED_EXPR:
15824 : case UNLT_EXPR:
15825 : case UNLE_EXPR:
15826 : case UNGT_EXPR:
15827 : case UNGE_EXPR:
15828 : case UNEQ_EXPR:
15829 6812 : result = 1;
15830 : break;
15831 :
15832 6941 : case LT_EXPR:
15833 6941 : case LE_EXPR:
15834 6941 : case GT_EXPR:
15835 6941 : case GE_EXPR:
15836 6941 : case LTGT_EXPR:
15837 6941 : if (flag_trapping_math)
15838 : return NULL_TREE;
15839 : result = 0;
15840 : break;
15841 :
15842 0 : default:
15843 0 : gcc_unreachable ();
15844 : }
15845 :
15846 6812 : return constant_boolean_node (result, type);
15847 : }
15848 :
15849 1211562 : return constant_boolean_node (real_compare (code, c0, c1), type);
15850 : }
15851 :
15852 82760665 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15853 : {
15854 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15855 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15856 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15857 : }
15858 :
15859 : /* Handle equality/inequality of complex constants. */
15860 82760665 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15861 : {
15862 58558 : tree rcond = fold_relational_const (code, type,
15863 29279 : TREE_REALPART (op0),
15864 29279 : TREE_REALPART (op1));
15865 117116 : tree icond = fold_relational_const (code, type,
15866 29279 : TREE_IMAGPART (op0),
15867 29279 : TREE_IMAGPART (op1));
15868 29279 : if (code == EQ_EXPR)
15869 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15870 28977 : else if (code == NE_EXPR)
15871 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15872 : else
15873 : return NULL_TREE;
15874 : }
15875 :
15876 82731386 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15877 : {
15878 22646 : if (!VECTOR_TYPE_P (type))
15879 : {
15880 : /* Have vector comparison with scalar boolean result. */
15881 154 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15882 : && known_eq (VECTOR_CST_NELTS (op0),
15883 : VECTOR_CST_NELTS (op1)));
15884 154 : unsigned HOST_WIDE_INT nunits;
15885 154 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15886 : return NULL_TREE;
15887 437 : for (unsigned i = 0; i < nunits; i++)
15888 : {
15889 384 : tree elem0 = VECTOR_CST_ELT (op0, i);
15890 384 : tree elem1 = VECTOR_CST_ELT (op1, i);
15891 384 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15892 384 : if (tmp == NULL_TREE)
15893 : return NULL_TREE;
15894 384 : if (integer_zerop (tmp))
15895 101 : return constant_boolean_node (code == NE_EXPR, type);
15896 : }
15897 53 : return constant_boolean_node (code == EQ_EXPR, type);
15898 : }
15899 22492 : tree_vector_builder elts;
15900 22492 : if (!elts.new_binary_operation (type, op0, op1, false))
15901 : return NULL_TREE;
15902 22492 : unsigned int count = elts.encoded_nelts ();
15903 78525 : for (unsigned i = 0; i < count; i++)
15904 : {
15905 56033 : tree elem_type = TREE_TYPE (type);
15906 56033 : tree elem0 = VECTOR_CST_ELT (op0, i);
15907 56033 : tree elem1 = VECTOR_CST_ELT (op1, i);
15908 :
15909 56033 : tree tem = fold_relational_const (code, elem_type,
15910 : elem0, elem1);
15911 :
15912 56033 : if (tem == NULL_TREE)
15913 : return NULL_TREE;
15914 :
15915 56033 : elts.quick_push (build_int_cst (elem_type,
15916 90495 : integer_zerop (tem) ? 0 : -1));
15917 : }
15918 :
15919 22492 : return elts.build ();
15920 22492 : }
15921 :
15922 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15923 :
15924 : To compute GT, swap the arguments and do LT.
15925 : To compute GE, do LT and invert the result.
15926 : To compute LE, swap the arguments, do LT and invert the result.
15927 : To compute NE, do EQ and invert the result.
15928 :
15929 : Therefore, the code below must handle only EQ and LT. */
15930 :
15931 82708740 : if (code == LE_EXPR || code == GT_EXPR)
15932 : {
15933 14927258 : std::swap (op0, op1);
15934 14927258 : code = swap_tree_comparison (code);
15935 : }
15936 :
15937 : /* Note that it is safe to invert for real values here because we
15938 : have already handled the one case that it matters. */
15939 :
15940 82708740 : invert = 0;
15941 82708740 : if (code == NE_EXPR || code == GE_EXPR)
15942 : {
15943 37642949 : invert = 1;
15944 37642949 : code = invert_tree_comparison (code, false);
15945 : }
15946 :
15947 : /* Compute a result for LT or EQ if args permit;
15948 : Otherwise return T. */
15949 82708740 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15950 : {
15951 82683211 : if (code == EQ_EXPR)
15952 40567634 : result = tree_int_cst_equal (op0, op1);
15953 : else
15954 42115577 : result = tree_int_cst_lt (op0, op1);
15955 : }
15956 : else
15957 : return NULL_TREE;
15958 :
15959 82683211 : if (invert)
15960 37641018 : result ^= 1;
15961 82683211 : return constant_boolean_node (result, type);
15962 : }
15963 :
15964 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15965 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15966 : itself. */
15967 :
15968 : tree
15969 136773807 : fold_build_cleanup_point_expr (tree type, tree expr)
15970 : {
15971 : /* If the expression does not have side effects then we don't have to wrap
15972 : it with a cleanup point expression. */
15973 136773807 : if (!TREE_SIDE_EFFECTS (expr))
15974 : return expr;
15975 :
15976 : /* If the expression is a return, check to see if the expression inside the
15977 : return has no side effects or the right hand side of the modify expression
15978 : inside the return. If either don't have side effects set we don't need to
15979 : wrap the expression in a cleanup point expression. Note we don't check the
15980 : left hand side of the modify because it should always be a return decl. */
15981 116806588 : if (TREE_CODE (expr) == RETURN_EXPR)
15982 : {
15983 46034676 : tree op = TREE_OPERAND (expr, 0);
15984 46034676 : if (!op || !TREE_SIDE_EFFECTS (op))
15985 : return expr;
15986 45328088 : op = TREE_OPERAND (op, 1);
15987 45328088 : if (!TREE_SIDE_EFFECTS (op))
15988 : return expr;
15989 : }
15990 :
15991 92426768 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15992 : }
15993 :
15994 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15995 : of an indirection through OP0, or NULL_TREE if no simplification is
15996 : possible. */
15997 :
15998 : tree
15999 22691621 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16000 : {
16001 22691621 : tree sub = op0;
16002 22691621 : tree subtype;
16003 22691621 : poly_uint64 const_op01;
16004 :
16005 22691621 : STRIP_NOPS (sub);
16006 22691621 : subtype = TREE_TYPE (sub);
16007 22691621 : if (!POINTER_TYPE_P (subtype)
16008 22691621 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16009 : return NULL_TREE;
16010 :
16011 22527652 : if (TREE_CODE (sub) == ADDR_EXPR)
16012 : {
16013 5013545 : tree op = TREE_OPERAND (sub, 0);
16014 5013545 : tree optype = TREE_TYPE (op);
16015 :
16016 : /* *&CONST_DECL -> to the value of the const decl. */
16017 5013545 : if (TREE_CODE (op) == CONST_DECL)
16018 3417 : return DECL_INITIAL (op);
16019 : /* *&p => p; make sure to handle *&"str"[cst] here. */
16020 5010128 : if (type == optype)
16021 : {
16022 3796189 : tree fop = fold_read_from_constant_string (op);
16023 3796189 : if (fop)
16024 : return fop;
16025 : else
16026 3749707 : return op;
16027 : }
16028 : /* *(foo *)&fooarray => fooarray[0] */
16029 1213939 : else if (TREE_CODE (optype) == ARRAY_TYPE
16030 14103 : && type == TREE_TYPE (optype)
16031 1226829 : && (!in_gimple_form
16032 3157 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16033 : {
16034 12890 : tree type_domain = TYPE_DOMAIN (optype);
16035 12890 : tree min_val = size_zero_node;
16036 12890 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16037 12851 : min_val = TYPE_MIN_VALUE (type_domain);
16038 12890 : if (in_gimple_form
16039 3157 : && TREE_CODE (min_val) != INTEGER_CST)
16040 : return NULL_TREE;
16041 12890 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16042 12890 : NULL_TREE, NULL_TREE);
16043 : }
16044 : /* *(foo *)&complexfoo => __real__ complexfoo */
16045 1201049 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16046 1201049 : && type == TREE_TYPE (optype))
16047 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16048 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16049 1201049 : else if (VECTOR_TYPE_P (optype)
16050 1201049 : && type == TREE_TYPE (optype))
16051 : {
16052 70 : tree part_width = TYPE_SIZE (type);
16053 70 : tree index = bitsize_int (0);
16054 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16055 70 : index);
16056 : }
16057 : }
16058 :
16059 18715086 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16060 18715086 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16061 : {
16062 259731 : tree op00 = TREE_OPERAND (sub, 0);
16063 259731 : tree op01 = TREE_OPERAND (sub, 1);
16064 :
16065 259731 : STRIP_NOPS (op00);
16066 259731 : if (TREE_CODE (op00) == ADDR_EXPR)
16067 : {
16068 1763 : tree op00type;
16069 1763 : op00 = TREE_OPERAND (op00, 0);
16070 1763 : op00type = TREE_TYPE (op00);
16071 :
16072 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16073 1763 : if (VECTOR_TYPE_P (op00type)
16074 240 : && type == TREE_TYPE (op00type)
16075 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16076 : but we want to treat offsets with MSB set as negative.
16077 : For the code below negative offsets are invalid and
16078 : TYPE_SIZE of the element is something unsigned, so
16079 : check whether op01 fits into poly_int64, which implies
16080 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16081 : then just use poly_uint64 because we want to treat the
16082 : value as unsigned. */
16083 1956 : && tree_fits_poly_int64_p (op01))
16084 : {
16085 179 : tree part_width = TYPE_SIZE (type);
16086 179 : poly_uint64 max_offset
16087 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16088 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16089 179 : if (known_lt (const_op01, max_offset))
16090 : {
16091 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16092 179 : return fold_build3_loc (loc,
16093 : BIT_FIELD_REF, type, op00,
16094 179 : part_width, index);
16095 : }
16096 : }
16097 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16098 1584 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16099 1584 : && type == TREE_TYPE (op00type))
16100 : {
16101 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16102 : const_op01))
16103 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16104 : }
16105 : /* ((foo *)&fooarray)[1] => fooarray[1] */
16106 1584 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16107 1584 : && type == TREE_TYPE (op00type))
16108 : {
16109 719 : tree type_domain = TYPE_DOMAIN (op00type);
16110 719 : tree min_val = size_zero_node;
16111 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16112 718 : min_val = TYPE_MIN_VALUE (type_domain);
16113 719 : poly_uint64 type_size, index;
16114 719 : if (poly_int_tree_p (min_val)
16115 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16116 719 : && multiple_p (const_op01, type_size, &index))
16117 : {
16118 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16119 719 : op01 = wide_int_to_tree (sizetype, off);
16120 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16121 : NULL_TREE, NULL_TREE);
16122 : }
16123 : }
16124 : }
16125 : }
16126 :
16127 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16128 18714188 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16129 699425 : && type == TREE_TYPE (TREE_TYPE (subtype))
16130 18717616 : && (!in_gimple_form
16131 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16132 : {
16133 3427 : tree type_domain;
16134 3427 : tree min_val = size_zero_node;
16135 3427 : sub = build_fold_indirect_ref_loc (loc, sub);
16136 3427 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16137 3427 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16138 3427 : min_val = TYPE_MIN_VALUE (type_domain);
16139 3427 : if (in_gimple_form
16140 11 : && TREE_CODE (min_val) != INTEGER_CST)
16141 : return NULL_TREE;
16142 3427 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16143 3427 : NULL_TREE);
16144 : }
16145 :
16146 : return NULL_TREE;
16147 : }
16148 :
16149 : /* Builds an expression for an indirection through T, simplifying some
16150 : cases. */
16151 :
16152 : tree
16153 11834082 : build_fold_indirect_ref_loc (location_t loc, tree t)
16154 : {
16155 11834082 : tree type = TREE_TYPE (TREE_TYPE (t));
16156 11834082 : tree sub = fold_indirect_ref_1 (loc, type, t);
16157 :
16158 11834082 : if (sub)
16159 : return sub;
16160 :
16161 8041665 : return build1_loc (loc, INDIRECT_REF, type, t);
16162 : }
16163 :
16164 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16165 :
16166 : tree
16167 10466202 : fold_indirect_ref_loc (location_t loc, tree t)
16168 : {
16169 10466202 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16170 :
16171 10466202 : if (sub)
16172 : return sub;
16173 : else
16174 10444386 : return t;
16175 : }
16176 :
16177 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16178 : whose result is ignored. The type of the returned tree need not be
16179 : the same as the original expression. */
16180 :
16181 : tree
16182 135658 : fold_ignored_result (tree t)
16183 : {
16184 135658 : if (!TREE_SIDE_EFFECTS (t))
16185 17791 : return integer_zero_node;
16186 :
16187 157040 : for (;;)
16188 157040 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16189 : {
16190 3835 : case tcc_unary:
16191 3835 : t = TREE_OPERAND (t, 0);
16192 3835 : break;
16193 :
16194 5097 : case tcc_binary:
16195 5097 : case tcc_comparison:
16196 5097 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16197 3207 : t = TREE_OPERAND (t, 0);
16198 1890 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16199 17 : t = TREE_OPERAND (t, 1);
16200 : else
16201 : return t;
16202 : break;
16203 :
16204 100409 : case tcc_expression:
16205 100409 : switch (TREE_CODE (t))
16206 : {
16207 32115 : case COMPOUND_EXPR:
16208 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16209 : return t;
16210 31820 : t = TREE_OPERAND (t, 0);
16211 31820 : break;
16212 :
16213 382 : case COND_EXPR:
16214 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16215 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16216 : return t;
16217 294 : t = TREE_OPERAND (t, 0);
16218 294 : break;
16219 :
16220 : default:
16221 : return t;
16222 : }
16223 : break;
16224 :
16225 : default:
16226 : return t;
16227 : }
16228 : }
16229 :
16230 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16231 :
16232 : tree
16233 3153254100 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16234 : {
16235 3153254100 : tree div = NULL_TREE;
16236 :
16237 3153254100 : if (divisor == 1)
16238 : return value;
16239 :
16240 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16241 : have to do anything. Only do this when we are not given a const,
16242 : because in that case, this check is more expensive than just
16243 : doing it. */
16244 1959034151 : if (TREE_CODE (value) != INTEGER_CST)
16245 : {
16246 387626 : div = build_int_cst (TREE_TYPE (value), divisor);
16247 :
16248 387626 : if (multiple_of_p (TREE_TYPE (value), value, div))
16249 : return value;
16250 : }
16251 :
16252 : /* If divisor is a power of two, simplify this to bit manipulation. */
16253 1958648437 : if (pow2_or_zerop (divisor))
16254 : {
16255 1958648437 : if (TREE_CODE (value) == INTEGER_CST)
16256 : {
16257 1958646525 : wide_int val = wi::to_wide (value);
16258 1958646525 : bool overflow_p;
16259 :
16260 1958646525 : if ((val & (divisor - 1)) == 0)
16261 : return value;
16262 :
16263 4064459 : overflow_p = TREE_OVERFLOW (value);
16264 4064459 : val += divisor - 1;
16265 4064459 : val &= (int) -divisor;
16266 4064459 : if (val == 0)
16267 4 : overflow_p = true;
16268 :
16269 4064459 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16270 1958646525 : }
16271 : else
16272 : {
16273 1912 : tree t;
16274 :
16275 1912 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16276 1912 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16277 1912 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16278 1912 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16279 : }
16280 : }
16281 : else
16282 : {
16283 0 : if (!div)
16284 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16285 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16286 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16287 : }
16288 :
16289 : return value;
16290 : }
16291 :
16292 : /* Likewise, but round down. */
16293 :
16294 : tree
16295 21380532 : round_down_loc (location_t loc, tree value, int divisor)
16296 : {
16297 21380532 : tree div = NULL_TREE;
16298 :
16299 21380532 : gcc_assert (divisor > 0);
16300 21380532 : if (divisor == 1)
16301 : return value;
16302 :
16303 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16304 : have to do anything. Only do this when we are not given a const,
16305 : because in that case, this check is more expensive than just
16306 : doing it. */
16307 21380532 : if (TREE_CODE (value) != INTEGER_CST)
16308 : {
16309 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16310 :
16311 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16312 : return value;
16313 : }
16314 :
16315 : /* If divisor is a power of two, simplify this to bit manipulation. */
16316 21380532 : if (pow2_or_zerop (divisor))
16317 : {
16318 21380532 : tree t;
16319 :
16320 21380532 : t = build_int_cst (TREE_TYPE (value), -divisor);
16321 21380532 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16322 : }
16323 : else
16324 : {
16325 0 : if (!div)
16326 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16327 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16328 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16329 : }
16330 :
16331 : return value;
16332 : }
16333 :
16334 : /* Returns the pointer to the base of the object addressed by EXP and
16335 : extracts the information about the offset of the access, storing it
16336 : to PBITPOS and POFFSET. */
16337 :
16338 : static tree
16339 25280138 : split_address_to_core_and_offset (tree exp,
16340 : poly_int64 *pbitpos, tree *poffset)
16341 : {
16342 25280138 : tree core;
16343 25280138 : machine_mode mode;
16344 25280138 : int unsignedp, reversep, volatilep;
16345 25280138 : poly_int64 bitsize;
16346 25280138 : location_t loc = EXPR_LOCATION (exp);
16347 :
16348 25280138 : STRIP_NOPS (exp);
16349 :
16350 25280138 : if (TREE_CODE (exp) == SSA_NAME)
16351 15855013 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16352 19749647 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16353 58844 : exp = gimple_assign_rhs1 (def);
16354 :
16355 25280138 : if (TREE_CODE (exp) == ADDR_EXPR)
16356 : {
16357 2030362 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16358 : poffset, &mode, &unsignedp, &reversep,
16359 : &volatilep);
16360 : /* If we are left with MEM[a + CST] strip that and add it to the
16361 : pbitpos and return a. */
16362 2030362 : if (TREE_CODE (core) == MEM_REF)
16363 : {
16364 57570 : poly_offset_int tem;
16365 57570 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16366 57570 : tem <<= LOG2_BITS_PER_UNIT;
16367 57570 : tem += *pbitpos;
16368 57570 : if (tem.to_shwi (pbitpos))
16369 57382 : return TREE_OPERAND (core, 0);
16370 : }
16371 1972980 : core = build_fold_addr_expr_loc (loc, core);
16372 : }
16373 23249776 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16374 : {
16375 578938 : core = TREE_OPERAND (exp, 0);
16376 578938 : STRIP_NOPS (core);
16377 578938 : *pbitpos = 0;
16378 578938 : *poffset = TREE_OPERAND (exp, 1);
16379 578938 : if (poly_int_tree_p (*poffset))
16380 : {
16381 559909 : poly_offset_int tem
16382 559909 : = wi::sext (wi::to_poly_offset (*poffset),
16383 559909 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16384 559909 : tem <<= LOG2_BITS_PER_UNIT;
16385 559909 : if (tem.to_shwi (pbitpos))
16386 559909 : *poffset = NULL_TREE;
16387 : }
16388 : }
16389 : else
16390 : {
16391 22670838 : core = exp;
16392 22670838 : *pbitpos = 0;
16393 22670838 : *poffset = NULL_TREE;
16394 : }
16395 :
16396 : return core;
16397 : }
16398 :
16399 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16400 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16401 :
16402 : bool
16403 12640069 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16404 : {
16405 12640069 : tree core1, core2;
16406 12640069 : poly_int64 bitpos1, bitpos2;
16407 12640069 : tree toffset1, toffset2, tdiff, type;
16408 :
16409 12640069 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16410 12640069 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16411 :
16412 12640069 : poly_int64 bytepos1, bytepos2;
16413 12640069 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16414 25280138 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16415 25280138 : || !operand_equal_p (core1, core2, 0))
16416 : return false;
16417 :
16418 692207 : if (toffset1 && toffset2)
16419 : {
16420 149 : type = TREE_TYPE (toffset1);
16421 149 : if (type != TREE_TYPE (toffset2))
16422 0 : toffset2 = fold_convert (type, toffset2);
16423 :
16424 149 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16425 149 : if (!cst_and_fits_in_hwi (tdiff))
16426 : return false;
16427 :
16428 53 : *diff = int_cst_value (tdiff);
16429 : }
16430 692058 : else if (toffset1 || toffset2)
16431 : {
16432 : /* If only one of the offsets is non-constant, the difference cannot
16433 : be a constant. */
16434 : return false;
16435 : }
16436 : else
16437 673463 : *diff = 0;
16438 :
16439 673516 : *diff += bytepos1 - bytepos2;
16440 673516 : return true;
16441 : }
16442 :
16443 : /* Return OFF converted to a pointer offset type suitable as offset for
16444 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16445 : tree
16446 53587197 : convert_to_ptrofftype_loc (location_t loc, tree off)
16447 : {
16448 53587197 : if (ptrofftype_p (TREE_TYPE (off)))
16449 : return off;
16450 6233176 : return fold_convert_loc (loc, sizetype, off);
16451 : }
16452 :
16453 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16454 : tree
16455 47443295 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16456 : {
16457 47443295 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16458 47443295 : ptr, convert_to_ptrofftype_loc (loc, off));
16459 : }
16460 :
16461 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16462 : tree
16463 167212 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16464 : {
16465 167212 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16466 167212 : ptr, size_int (off));
16467 : }
16468 :
16469 : /* Return a pointer to a NUL-terminated string containing the sequence
16470 : of bytes corresponding to the representation of the object referred to
16471 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16472 : to an initialized constant array plus some constant offset).
16473 : Set *STRSIZE the number of bytes in the constant sequence including
16474 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16475 : where A is the array that stores the constant sequence that SRC points
16476 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16477 : need not point to a string or even an array of characters but may point
16478 : to an object of any type. */
16479 :
16480 : const char *
16481 12708241 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16482 : {
16483 : /* The offset into the array A storing the string, and A's byte size. */
16484 12708241 : tree offset_node;
16485 12708241 : tree mem_size;
16486 :
16487 12708241 : if (strsize)
16488 4672035 : *strsize = 0;
16489 :
16490 12708241 : if (strsize)
16491 4672035 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16492 : else
16493 8036206 : src = string_constant (src, &offset_node, &mem_size, NULL);
16494 12708241 : if (!src)
16495 : return NULL;
16496 :
16497 2844495 : unsigned HOST_WIDE_INT offset = 0;
16498 2844495 : if (offset_node != NULL_TREE)
16499 : {
16500 2844495 : if (!tree_fits_uhwi_p (offset_node))
16501 : return NULL;
16502 : else
16503 2841345 : offset = tree_to_uhwi (offset_node);
16504 : }
16505 :
16506 2841345 : if (!tree_fits_uhwi_p (mem_size))
16507 : return NULL;
16508 :
16509 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16510 : is stored in and equal to sizeof A. INIT_BYTES is the number
16511 : of bytes in the constant sequence used to initialize the array,
16512 : including any embedded NULs as well as the terminating NUL (for
16513 : strings), but not including any trailing zeros/NULs past
16514 : the terminating one appended implicitly to a string literal to
16515 : zero out the remainder of the array it's stored in. For example,
16516 : given:
16517 : const char a[7] = "abc\0d";
16518 : n = strlen (a + 1);
16519 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16520 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16521 : is equal to strlen (A) + 1. */
16522 2841345 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16523 2841345 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16524 2841345 : const char *string = TREE_STRING_POINTER (src);
16525 :
16526 : /* Ideally this would turn into a gcc_checking_assert over time. */
16527 2841345 : if (init_bytes > array_size)
16528 : init_bytes = array_size;
16529 :
16530 2841345 : if (init_bytes == 0 || offset >= array_size)
16531 : return NULL;
16532 :
16533 2840078 : if (strsize)
16534 : {
16535 : /* Compute and store the number of characters from the beginning
16536 : of the substring at OFFSET to the end, including the terminating
16537 : nul. Offsets past the initial length refer to null strings. */
16538 1442218 : if (offset < init_bytes)
16539 1442218 : *strsize = init_bytes - offset;
16540 : else
16541 0 : *strsize = 1;
16542 : }
16543 : else
16544 : {
16545 1397860 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16546 : /* Support only properly NUL-terminated single byte strings. */
16547 1397860 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16548 : return NULL;
16549 1393014 : if (string[init_bytes - 1] != '\0')
16550 : return NULL;
16551 : }
16552 :
16553 2809135 : return offset < init_bytes ? string + offset : "";
16554 : }
16555 :
16556 : /* Return a pointer to a NUL-terminated string corresponding to
16557 : the expression STR referencing a constant string, possibly
16558 : involving a constant offset. Return null if STR either doesn't
16559 : reference a constant string or if it involves a nonconstant
16560 : offset. */
16561 :
16562 : const char *
16563 8036206 : c_getstr (tree str)
16564 : {
16565 8036206 : return getbyterep (str, NULL);
16566 : }
16567 :
16568 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16569 : may be nonzero, with precision PREC, the precision of T's type. */
16570 :
16571 : static wide_int
16572 260985494 : tree_nonzero_bits (const_tree t, unsigned prec)
16573 : {
16574 260985494 : switch (TREE_CODE (t))
16575 : {
16576 8843461 : case INTEGER_CST:
16577 8843461 : return wi::to_wide (t);
16578 147043017 : case SSA_NAME:
16579 147043017 : return get_nonzero_bits (t);
16580 287560 : case NON_LVALUE_EXPR:
16581 287560 : case SAVE_EXPR:
16582 287560 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16583 541783 : case BIT_AND_EXPR:
16584 1083566 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16585 1625349 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16586 33772 : case BIT_IOR_EXPR:
16587 33772 : case BIT_XOR_EXPR:
16588 67544 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16589 101316 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16590 75275 : case COND_EXPR:
16591 150550 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16592 225825 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16593 52902910 : CASE_CONVERT:
16594 52902910 : if (TREE_TYPE (t) != error_mark_node
16595 52902910 : && !error_operand_p (TREE_OPERAND (t, 0)))
16596 : {
16597 52902909 : tree op0 = TREE_OPERAND (t, 0);
16598 52902909 : tree inner_type = TREE_TYPE (op0);
16599 52902909 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16600 105805818 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16601 105805818 : prec, TYPE_SIGN (inner_type));
16602 : }
16603 : break;
16604 14299163 : case PLUS_EXPR:
16605 14299163 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16606 : {
16607 14299163 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16608 14299163 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16609 14299163 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16610 546715 : return wi::bit_or (nzbits1, nzbits2);
16611 14299163 : }
16612 : break;
16613 170893 : case LSHIFT_EXPR:
16614 170893 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16615 170893 : && TREE_TYPE (t) != error_mark_node)
16616 : {
16617 98712 : tree type = TREE_TYPE (t);
16618 98712 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16619 98712 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16620 98712 : return wi::neg_p (arg1)
16621 197424 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16622 98712 : : wi::lshift (nzbits, arg1);
16623 98712 : }
16624 : break;
16625 165972 : case RSHIFT_EXPR:
16626 165972 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16627 165972 : && TREE_TYPE (t) != error_mark_node)
16628 : {
16629 164024 : tree type = TREE_TYPE (t);
16630 164024 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16631 164024 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16632 164024 : return wi::neg_p (arg1)
16633 328048 : ? wi::lshift (nzbits, -arg1)
16634 164024 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16635 164024 : }
16636 : break;
16637 : default:
16638 : break;
16639 : }
16640 :
16641 50448266 : return wi::shwi (-1, prec);
16642 : }
16643 :
16644 : /* Given a tree T, compute which bits in T may be nonzero. */
16645 :
16646 : wide_int
16647 177632303 : tree_nonzero_bits (const_tree t)
16648 : {
16649 177632303 : if (error_operand_p (t))
16650 0 : return wi::shwi (-1, 64);
16651 177632303 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16652 : }
16653 :
16654 : /* Helper function for address compare simplifications in match.pd.
16655 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16656 : TYPE is the type of comparison operands.
16657 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16658 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16659 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16660 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16661 : and 2 if unknown. */
16662 :
16663 : int
16664 5501967 : address_compare (tree_code code, tree type, tree op0, tree op1,
16665 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16666 : bool generic)
16667 : {
16668 5501967 : if (TREE_CODE (op0) == SSA_NAME)
16669 32450 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16670 5501967 : if (TREE_CODE (op1) == SSA_NAME)
16671 4603 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16672 5501967 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16673 5501967 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16674 5501967 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16675 5501967 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16676 5501967 : if (base0 && TREE_CODE (base0) == MEM_REF)
16677 : {
16678 30559 : off0 += mem_ref_offset (base0).force_shwi ();
16679 30559 : base0 = TREE_OPERAND (base0, 0);
16680 : }
16681 5501967 : if (base1 && TREE_CODE (base1) == MEM_REF)
16682 : {
16683 3772 : off1 += mem_ref_offset (base1).force_shwi ();
16684 3772 : base1 = TREE_OPERAND (base1, 0);
16685 : }
16686 5501967 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16687 : return 2;
16688 :
16689 5488895 : int equal = 2;
16690 : /* Punt in GENERIC on variables with value expressions;
16691 : the value expressions might point to fields/elements
16692 : of other vars etc. */
16693 5488895 : if (generic
16694 5488895 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16695 5354560 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16696 : return 2;
16697 5488238 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16698 : {
16699 1429384 : symtab_node *node0 = symtab_node::get_create (base0);
16700 1429384 : symtab_node *node1 = symtab_node::get_create (base1);
16701 1429384 : equal = node0->equal_address_to (node1);
16702 : }
16703 4058854 : else if ((DECL_P (base0)
16704 240705 : || TREE_CODE (base0) == SSA_NAME
16705 211265 : || TREE_CODE (base0) == STRING_CST)
16706 4058656 : && (DECL_P (base1)
16707 214937 : || TREE_CODE (base1) == SSA_NAME
16708 211391 : || TREE_CODE (base1) == STRING_CST))
16709 4058641 : equal = (base0 == base1);
16710 : /* Assume different STRING_CSTs with the same content will be
16711 : merged. */
16712 5488025 : if (equal == 0
16713 77570 : && TREE_CODE (base0) == STRING_CST
16714 17344 : && TREE_CODE (base1) == STRING_CST
16715 17133 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16716 5488025 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16717 6190 : TREE_STRING_LENGTH (base0)) == 0)
16718 : equal = 1;
16719 5483805 : if (equal == 1)
16720 : {
16721 5390251 : if (code == EQ_EXPR
16722 5390251 : || code == NE_EXPR
16723 : /* If the offsets are equal we can ignore overflow. */
16724 204364 : || known_eq (off0, off1)
16725 300762 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16726 : /* Or if we compare using pointers to decls or strings. */
16727 5540632 : || (POINTER_TYPE_P (type)
16728 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16729 5390251 : return 1;
16730 : return 2;
16731 : }
16732 97987 : if (equal != 0)
16733 : return equal;
16734 73137 : if (code != EQ_EXPR && code != NE_EXPR)
16735 : return 2;
16736 :
16737 : /* At this point we know (or assume) the two pointers point at
16738 : different objects. */
16739 67893 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16740 67893 : off0.is_constant (&ioff0);
16741 67893 : off1.is_constant (&ioff1);
16742 : /* Punt on non-zero offsets from functions. */
16743 67893 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16744 67893 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16745 : return 2;
16746 : /* Or if the bases are neither decls nor string literals. */
16747 67893 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16748 : return 2;
16749 39189 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16750 : return 2;
16751 : /* For initializers, assume addresses of different functions are
16752 : different. */
16753 39189 : if (folding_initializer
16754 13949 : && TREE_CODE (base0) == FUNCTION_DECL
16755 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16756 : return 0;
16757 :
16758 : /* Compute whether one address points to the start of one
16759 : object and another one to the end of another one. */
16760 39169 : poly_int64 size0 = 0, size1 = 0;
16761 39169 : if (TREE_CODE (base0) == STRING_CST)
16762 : {
16763 12727 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16764 : equal = 2;
16765 : else
16766 : size0 = TREE_STRING_LENGTH (base0);
16767 : }
16768 26442 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16769 : size0 = 1;
16770 : else
16771 : {
16772 26122 : tree sz0 = DECL_SIZE_UNIT (base0);
16773 26122 : if (!tree_fits_poly_int64_p (sz0))
16774 : equal = 2;
16775 : else
16776 26122 : size0 = tree_to_poly_int64 (sz0);
16777 : }
16778 39169 : if (TREE_CODE (base1) == STRING_CST)
16779 : {
16780 12826 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16781 : equal = 2;
16782 : else
16783 : size1 = TREE_STRING_LENGTH (base1);
16784 : }
16785 26343 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16786 : size1 = 1;
16787 : else
16788 : {
16789 26027 : tree sz1 = DECL_SIZE_UNIT (base1);
16790 26027 : if (!tree_fits_poly_int64_p (sz1))
16791 : equal = 2;
16792 : else
16793 26027 : size1 = tree_to_poly_int64 (sz1);
16794 : }
16795 39169 : if (equal == 0)
16796 : {
16797 : /* If one offset is pointing (or could be) to the beginning of one
16798 : object and the other is pointing to one past the last byte of the
16799 : other object, punt. */
16800 39157 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16801 : equal = 2;
16802 38964 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16803 : equal = 2;
16804 : /* If both offsets are the same, there are some cases we know that are
16805 : ok. Either if we know they aren't zero, or if we know both sizes
16806 : are no zero. */
16807 : if (equal == 2
16808 343 : && known_eq (off0, off1)
16809 22 : && (known_ne (off0, 0)
16810 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16811 : equal = 0;
16812 : }
16813 :
16814 : /* At this point, equal is 2 if either one or both pointers are out of
16815 : bounds of their object, or one points to start of its object and the
16816 : other points to end of its object. This is unspecified behavior
16817 : e.g. in C++. Otherwise equal is 0. */
16818 39169 : if (folding_cxx_constexpr && equal)
16819 : return equal;
16820 :
16821 : /* When both pointers point to string literals, even when equal is 0,
16822 : due to tail merging of string literals the pointers might be the same. */
16823 39106 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16824 : {
16825 12682 : if (ioff0 < 0
16826 12682 : || ioff1 < 0
16827 12682 : || ioff0 > TREE_STRING_LENGTH (base0)
16828 25352 : || ioff1 > TREE_STRING_LENGTH (base1))
16829 : return 2;
16830 :
16831 : /* If the bytes in the string literals starting at the pointers
16832 : differ, the pointers need to be different. */
16833 12670 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16834 12670 : TREE_STRING_POINTER (base1) + ioff1,
16835 12670 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16836 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16837 : {
16838 3764 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16839 3764 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16840 3764 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16841 : ioffmin) == 0)
16842 : /* If even the bytes in the string literal before the
16843 : pointers are the same, the string literals could be
16844 : tail merged. */
16845 : return 2;
16846 : }
16847 8918 : return 0;
16848 : }
16849 :
16850 26424 : if (folding_cxx_constexpr)
16851 : return 0;
16852 :
16853 : /* If this is a pointer comparison, ignore for now even
16854 : valid equalities where one pointer is the offset zero
16855 : of one object and the other to one past end of another one. */
16856 12599 : if (!INTEGRAL_TYPE_P (type))
16857 : return 0;
16858 :
16859 : /* Assume that string literals can't be adjacent to variables
16860 : (automatic or global). */
16861 389 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16862 : return 0;
16863 :
16864 : /* Assume that automatic variables can't be adjacent to global
16865 : variables. */
16866 363 : if (is_global_var (base0) != is_global_var (base1))
16867 2 : return 0;
16868 :
16869 : return equal;
16870 : }
16871 :
16872 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16873 : tree
16874 68 : ctor_single_nonzero_element (const_tree t)
16875 : {
16876 68 : unsigned HOST_WIDE_INT idx;
16877 68 : constructor_elt *ce;
16878 68 : tree elt = NULL_TREE;
16879 :
16880 68 : if (TREE_CODE (t) != CONSTRUCTOR)
16881 : return NULL_TREE;
16882 145 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16883 142 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16884 : {
16885 133 : if (elt)
16886 : return NULL_TREE;
16887 68 : elt = ce->value;
16888 : }
16889 : return elt;
16890 : }
16891 :
16892 : #if CHECKING_P
16893 :
16894 : namespace selftest {
16895 :
16896 : /* Helper functions for writing tests of folding trees. */
16897 :
16898 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16899 :
16900 : static void
16901 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16902 : tree constant)
16903 : {
16904 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16905 16 : }
16906 :
16907 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16908 : wrapping WRAPPED_EXPR. */
16909 :
16910 : static void
16911 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16912 : tree wrapped_expr)
16913 : {
16914 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16915 12 : ASSERT_NE (wrapped_expr, result);
16916 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16917 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16918 12 : }
16919 :
16920 : /* Verify that various arithmetic binary operations are folded
16921 : correctly. */
16922 :
16923 : static void
16924 4 : test_arithmetic_folding ()
16925 : {
16926 4 : tree type = integer_type_node;
16927 4 : tree x = create_tmp_var_raw (type, "x");
16928 4 : tree zero = build_zero_cst (type);
16929 4 : tree one = build_int_cst (type, 1);
16930 :
16931 : /* Addition. */
16932 : /* 1 <-- (0 + 1) */
16933 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16934 : one);
16935 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16936 : one);
16937 :
16938 : /* (nonlvalue)x <-- (x + 0) */
16939 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16940 : x);
16941 :
16942 : /* Subtraction. */
16943 : /* 0 <-- (x - x) */
16944 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16945 : zero);
16946 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16947 : x);
16948 :
16949 : /* Multiplication. */
16950 : /* 0 <-- (x * 0) */
16951 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16952 : zero);
16953 :
16954 : /* (nonlvalue)x <-- (x * 1) */
16955 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16956 : x);
16957 4 : }
16958 :
16959 : namespace test_operand_equality {
16960 :
16961 : /* Verify structural equality. */
16962 :
16963 : /* Execute fold_vec_perm_cst unit tests. */
16964 :
16965 : static void
16966 4 : test ()
16967 : {
16968 4 : tree stype = integer_type_node;
16969 4 : tree utype = unsigned_type_node;
16970 4 : tree x = create_tmp_var_raw (stype, "x");
16971 4 : tree y = create_tmp_var_raw (stype, "y");
16972 4 : tree z = create_tmp_var_raw (stype, "z");
16973 4 : tree four = build_int_cst (stype, 4);
16974 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16975 4 : tree rhs1 = fold_convert (stype,
16976 : fold_build2 (PLUS_EXPR, utype,
16977 : fold_convert (utype, x),
16978 : fold_convert (utype, y)));
16979 :
16980 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16981 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16982 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16983 :
16984 : /* (int)(unsigned) x == x. */
16985 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16986 : build1 (NOP_EXPR, utype, x));
16987 4 : tree rhs2 = x;
16988 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16989 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16990 :
16991 : /* (unsigned x) + (unsigned y) == x + y. */
16992 4 : tree lhs3 = lhs1;
16993 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16994 : fold_convert (utype, x),
16995 : fold_convert (utype, y));
16996 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16997 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16998 :
16999 : /* (unsigned x) / (unsigned y) == x / y. */
17000 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17001 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17002 : fold_convert (utype, x),
17003 : fold_convert (utype, y));
17004 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17005 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17006 :
17007 : /* (long x) / 4 == (long)(x / 4). */
17008 4 : tree lstype = long_long_integer_type_node;
17009 4 : tree lfour = build_int_cst (lstype, 4);
17010 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17011 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17012 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17013 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17014 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17015 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17016 :
17017 : /* (unsigned x) / 4 == x / 4. */
17018 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17019 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17020 : fold_convert (utype, x),
17021 : fold_convert (utype, four));
17022 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17023 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17024 :
17025 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17026 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17027 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17028 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17029 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17030 :
17031 : /* (unsigned x) + 4 == x + 4. */
17032 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17033 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17034 : fold_convert (utype, x),
17035 : fold_convert (utype, four));
17036 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17037 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17038 :
17039 : /* (unsigned x) + 4 == 4 + x. */
17040 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17041 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17042 : fold_convert (utype, x),
17043 : fold_convert (utype, four));
17044 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17045 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17046 :
17047 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17048 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17049 : fold_build2 (MULT_EXPR, stype,
17050 : fold_build2 (PLUS_EXPR, stype, four, x),
17051 : y),
17052 : z);
17053 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17054 : fold_build2 (PLUS_EXPR, utype,
17055 : fold_convert (utype, x),
17056 : fold_convert (utype, four)),
17057 : fold_convert (utype, y));
17058 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17059 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17060 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17061 4 : }
17062 : }
17063 :
17064 : namespace test_fold_vec_perm_cst {
17065 :
17066 : /* Build a VECTOR_CST corresponding to VMODE, and has
17067 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17068 : Fill it with randomized elements, using rand() % THRESHOLD. */
17069 :
17070 : static tree
17071 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17072 : unsigned nelts_per_pattern,
17073 : int step = 0, bool natural_stepped = false,
17074 : int threshold = 100)
17075 : {
17076 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17077 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17078 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17079 :
17080 : // Fill a0 for each pattern
17081 0 : for (unsigned i = 0; i < npatterns; i++)
17082 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17083 :
17084 0 : if (nelts_per_pattern == 1)
17085 0 : return builder.build ();
17086 :
17087 : // Fill a1 for each pattern
17088 0 : for (unsigned i = 0; i < npatterns; i++)
17089 : {
17090 0 : tree a1;
17091 0 : if (natural_stepped)
17092 : {
17093 0 : tree a0 = builder[i];
17094 0 : wide_int a0_val = wi::to_wide (a0);
17095 0 : wide_int a1_val = a0_val + step;
17096 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17097 0 : }
17098 : else
17099 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17100 0 : builder.quick_push (a1);
17101 : }
17102 0 : if (nelts_per_pattern == 2)
17103 0 : return builder.build ();
17104 :
17105 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17106 : {
17107 0 : tree prev_elem = builder[i - npatterns];
17108 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17109 0 : wide_int val = prev_elem_val + step;
17110 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17111 0 : }
17112 :
17113 0 : return builder.build ();
17114 0 : }
17115 :
17116 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17117 : when result is VLA. */
17118 :
17119 : static void
17120 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17121 : tree res, tree *expected_res)
17122 : {
17123 : /* Actual npatterns and encoded_elts in res may be less than expected due
17124 : to canonicalization. */
17125 0 : ASSERT_TRUE (res != NULL_TREE);
17126 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17127 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17128 :
17129 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17130 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17131 0 : }
17132 :
17133 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17134 : when the result is VLS. */
17135 :
17136 : static void
17137 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17138 : {
17139 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17140 0 : for (unsigned i = 0; i < expected_nelts; i++)
17141 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17142 0 : }
17143 :
17144 : /* Helper routine to push multiple elements into BUILDER. */
17145 : template<unsigned N>
17146 0 : static void builder_push_elems (vec_perm_builder& builder,
17147 : poly_uint64 (&elems)[N])
17148 : {
17149 0 : for (unsigned i = 0; i < N; i++)
17150 0 : builder.quick_push (elems[i]);
17151 0 : }
17152 :
17153 : #define ARG0(index) vector_cst_elt (arg0, index)
17154 : #define ARG1(index) vector_cst_elt (arg1, index)
17155 :
17156 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17157 :
17158 : static void
17159 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17160 : {
17161 0 : for (int i = 0; i < 10; i++)
17162 : {
17163 : /* Case 1:
17164 : sel = { 0, 4, 1, 5, ... }
17165 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17166 0 : {
17167 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17168 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17169 :
17170 0 : tree inner_type
17171 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17172 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17173 :
17174 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17175 0 : vec_perm_builder builder (res_len, 4, 1);
17176 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17177 0 : builder_push_elems (builder, mask_elems);
17178 :
17179 0 : vec_perm_indices sel (builder, 2, 4);
17180 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17181 :
17182 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17183 0 : validate_res (4, 1, res, expected_res);
17184 0 : }
17185 :
17186 : /* Case 2: Same as case 1, but contains an out of bounds access which
17187 : should wrap around.
17188 : sel = {0, 8, 4, 12, ...} (4, 1)
17189 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17190 0 : {
17191 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17192 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17193 :
17194 0 : tree inner_type
17195 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17196 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17197 :
17198 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17199 0 : vec_perm_builder builder (res_len, 4, 1);
17200 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17201 0 : builder_push_elems (builder, mask_elems);
17202 :
17203 0 : vec_perm_indices sel (builder, 2, 4);
17204 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17205 :
17206 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17207 0 : validate_res (4, 1, res, expected_res);
17208 0 : }
17209 : }
17210 0 : }
17211 :
17212 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17213 :
17214 : static void
17215 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17216 : {
17217 0 : for (int i = 0; i < 10; i++)
17218 : {
17219 : /* Case 1:
17220 : sel = { 0, 1, 2, 3}
17221 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17222 0 : {
17223 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17224 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17225 :
17226 0 : tree inner_type
17227 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17228 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17229 :
17230 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17231 0 : vec_perm_builder builder (res_len, 4, 1);
17232 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17233 0 : builder_push_elems (builder, mask_elems);
17234 :
17235 0 : vec_perm_indices sel (builder, 2,
17236 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17237 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17238 :
17239 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17240 0 : validate_res_vls (res, expected_res, 4);
17241 0 : }
17242 :
17243 : /* Case 2: Same as Case 1, but crossing input vector.
17244 : sel = {0, 2, 4, 6}
17245 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17246 : Since we cannot determine, which vector to choose from during
17247 : compile time, should return NULL_TREE. */
17248 0 : {
17249 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17250 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17251 :
17252 0 : tree inner_type
17253 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17254 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17255 :
17256 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17257 0 : vec_perm_builder builder (res_len, 4, 1);
17258 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17259 0 : builder_push_elems (builder, mask_elems);
17260 :
17261 0 : vec_perm_indices sel (builder, 2,
17262 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17263 0 : const char *reason;
17264 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17265 :
17266 0 : ASSERT_TRUE (res == NULL_TREE);
17267 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17268 0 : }
17269 : }
17270 0 : }
17271 :
17272 : /* Test all input vectors. */
17273 :
17274 : static void
17275 0 : test_all_nunits (machine_mode vmode)
17276 : {
17277 : /* Test with 10 different inputs. */
17278 0 : for (int i = 0; i < 10; i++)
17279 : {
17280 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17281 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17282 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17283 :
17284 : /* Case 1: mask = {0, ...} // (1, 1)
17285 : res = { arg0[0], ... } // (1, 1) */
17286 0 : {
17287 0 : vec_perm_builder builder (len, 1, 1);
17288 0 : builder.quick_push (0);
17289 0 : vec_perm_indices sel (builder, 2, len);
17290 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17291 0 : tree expected_res[] = { ARG0(0) };
17292 0 : validate_res (1, 1, res, expected_res);
17293 0 : }
17294 :
17295 : /* Case 2: mask = {len, ...} // (1, 1)
17296 : res = { arg1[0], ... } // (1, 1) */
17297 0 : {
17298 0 : vec_perm_builder builder (len, 1, 1);
17299 0 : builder.quick_push (len);
17300 0 : vec_perm_indices sel (builder, 2, len);
17301 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17302 :
17303 0 : tree expected_res[] = { ARG1(0) };
17304 0 : validate_res (1, 1, res, expected_res);
17305 0 : }
17306 : }
17307 0 : }
17308 :
17309 : /* Test all vectors which contain at-least 2 elements. */
17310 :
17311 : static void
17312 0 : test_nunits_min_2 (machine_mode vmode)
17313 : {
17314 0 : for (int i = 0; i < 10; i++)
17315 : {
17316 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17317 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17318 0 : {
17319 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17320 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17321 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17322 :
17323 0 : vec_perm_builder builder (len, 2, 1);
17324 0 : poly_uint64 mask_elems[] = { 0, len };
17325 0 : builder_push_elems (builder, mask_elems);
17326 :
17327 0 : vec_perm_indices sel (builder, 2, len);
17328 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17329 :
17330 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17331 0 : validate_res (2, 1, res, expected_res);
17332 0 : }
17333 :
17334 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17335 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17336 0 : {
17337 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17338 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17339 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17340 :
17341 0 : vec_perm_builder builder (len, 2, 2);
17342 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17343 0 : builder_push_elems (builder, mask_elems);
17344 :
17345 0 : vec_perm_indices sel (builder, 2, len);
17346 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17347 :
17348 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17349 0 : validate_res (2, 2, res, expected_res);
17350 0 : }
17351 :
17352 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17353 : Test that the stepped sequence of the pattern selects from
17354 : same input pattern. Since input vectors have npatterns = 2,
17355 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17356 : in input vector. So return NULL_TREE. */
17357 0 : {
17358 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17359 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17360 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17361 :
17362 0 : vec_perm_builder builder (len, 1, 3);
17363 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17364 0 : builder_push_elems (builder, mask_elems);
17365 :
17366 0 : vec_perm_indices sel (builder, 2, len);
17367 0 : const char *reason;
17368 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17369 : &reason);
17370 0 : ASSERT_TRUE (res == NULL_TREE);
17371 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17372 0 : }
17373 :
17374 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17375 : Test that stepped sequence of the pattern selects from arg0.
17376 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17377 0 : {
17378 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17379 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17380 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17381 :
17382 0 : vec_perm_builder builder (len, 1, 3);
17383 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17384 0 : builder_push_elems (builder, mask_elems);
17385 :
17386 0 : vec_perm_indices sel (builder, 2, len);
17387 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17388 :
17389 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17390 0 : validate_res (1, 3, res, expected_res);
17391 0 : }
17392 :
17393 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17394 : In this case ensure that arg has a natural stepped sequence
17395 : to preserve arg's encoding.
17396 :
17397 : As a concrete example, consider:
17398 : arg0: { -16, -9, -10, ... } // (1, 3)
17399 : arg1: { -12, -5, -6, ... } // (1, 3)
17400 : sel = { 0, len, len + 1, ... } // (1, 3)
17401 :
17402 : This will create res with following encoding:
17403 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17404 : = { -16, -12, -5, ... }
17405 :
17406 : The step in above encoding would be: (-5) - (-12) = 7
17407 : And hence res[3] would be computed as -5 + 7 = 2.
17408 : instead of arg1[2], ie, -6.
17409 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17410 : for this case. */
17411 0 : {
17412 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17413 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17414 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17415 :
17416 0 : vec_perm_builder builder (len, 1, 3);
17417 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17418 0 : builder_push_elems (builder, mask_elems);
17419 :
17420 0 : vec_perm_indices sel (builder, 2, len);
17421 0 : const char *reason;
17422 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17423 : stepped pattern, even if we didn't explicitly tell it to. So folding
17424 : may not always fail, but if it does, ensure that's because arg1 does
17425 : not have a natural stepped sequence (and not due to other reason) */
17426 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17427 0 : if (res == NULL_TREE)
17428 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17429 0 : }
17430 :
17431 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17432 : sequence and thus folding should be valid for this case. */
17433 0 : {
17434 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17435 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17436 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17437 :
17438 0 : vec_perm_builder builder (len, 1, 3);
17439 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17440 0 : builder_push_elems (builder, mask_elems);
17441 :
17442 0 : vec_perm_indices sel (builder, 2, len);
17443 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17444 :
17445 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17446 0 : validate_res (1, 3, res, expected_res);
17447 0 : }
17448 :
17449 : /* Case 8: Same as aarch64/sve/slp_3.c:
17450 : arg0, arg1 are dup vectors.
17451 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17452 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17453 :
17454 : In this case, since the input vectors are dup, only the first two
17455 : elements per pattern in sel are considered significant. */
17456 0 : {
17457 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17458 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17459 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17460 :
17461 0 : vec_perm_builder builder (len, 2, 3);
17462 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17463 0 : builder_push_elems (builder, mask_elems);
17464 :
17465 0 : vec_perm_indices sel (builder, 2, len);
17466 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17467 :
17468 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17469 0 : validate_res (2, 1, res, expected_res);
17470 0 : }
17471 : }
17472 0 : }
17473 :
17474 : /* Test all vectors which contain at-least 4 elements. */
17475 :
17476 : static void
17477 0 : test_nunits_min_4 (machine_mode vmode)
17478 : {
17479 0 : for (int i = 0; i < 10; i++)
17480 : {
17481 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17482 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17483 0 : {
17484 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17485 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17486 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17487 :
17488 0 : vec_perm_builder builder (len, 4, 1);
17489 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17490 0 : builder_push_elems (builder, mask_elems);
17491 :
17492 0 : vec_perm_indices sel (builder, 2, len);
17493 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17494 :
17495 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17496 0 : validate_res (4, 1, res, expected_res);
17497 0 : }
17498 :
17499 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17500 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17501 0 : {
17502 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17503 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17504 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17505 :
17506 0 : vec_perm_builder builder (arg0_len, 1, 3);
17507 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17508 0 : builder_push_elems (builder, mask_elems);
17509 :
17510 0 : vec_perm_indices sel (builder, 2, arg0_len);
17511 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17512 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17513 0 : validate_res (1, 3, res, expected_res);
17514 0 : }
17515 :
17516 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17517 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17518 0 : {
17519 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17520 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17521 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17522 :
17523 0 : vec_perm_builder builder (len, 1, 3);
17524 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17525 0 : builder_push_elems (builder, mask_elems);
17526 :
17527 0 : vec_perm_indices sel (builder, 2, len);
17528 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17529 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17530 0 : validate_res (1, 3, res, expected_res);
17531 0 : }
17532 :
17533 : /* Case 4:
17534 : sel = { len, 0, 2, ... } // (1, 3)
17535 : This should return NULL because we cross the input vectors.
17536 : Because,
17537 : Let's assume len = C + Cx
17538 : a1 = 0
17539 : S = 2
17540 : esel = arg0_len / sel_npatterns = C + Cx
17541 : ae = 0 + (esel - 2) * S
17542 : = 0 + (C + Cx - 2) * 2
17543 : = 2(C-2) + 2Cx
17544 :
17545 : For C >= 4:
17546 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17547 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17548 : Since q1 != qe, we cross input vectors.
17549 : So return NULL_TREE. */
17550 0 : {
17551 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17552 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17553 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17554 :
17555 0 : vec_perm_builder builder (arg0_len, 1, 3);
17556 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17557 0 : builder_push_elems (builder, mask_elems);
17558 :
17559 0 : vec_perm_indices sel (builder, 2, arg0_len);
17560 0 : const char *reason;
17561 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17562 0 : ASSERT_TRUE (res == NULL_TREE);
17563 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17564 0 : }
17565 :
17566 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17567 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17568 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17569 :
17570 : Note that fold_vec_perm_cst will set
17571 : res_npatterns = max(4, max(4, 2)) = 4
17572 : However after canonicalizing, we will end up with shape (2, 2). */
17573 0 : {
17574 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17575 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17576 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17577 :
17578 0 : vec_perm_builder builder (len, 2, 2);
17579 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17580 0 : builder_push_elems (builder, mask_elems);
17581 :
17582 0 : vec_perm_indices sel (builder, 2, len);
17583 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17584 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17585 0 : validate_res (2, 2, res, expected_res);
17586 0 : }
17587 :
17588 : /* Case 6: Test combination in sel, where one pattern is dup and other
17589 : is stepped sequence.
17590 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17591 : res = { arg0[0], arg0[0], arg0[0],
17592 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17593 0 : {
17594 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17595 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17596 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17597 :
17598 0 : vec_perm_builder builder (len, 2, 3);
17599 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17600 0 : builder_push_elems (builder, mask_elems);
17601 :
17602 0 : vec_perm_indices sel (builder, 2, len);
17603 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17604 :
17605 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17606 0 : ARG0(1), ARG0(0), ARG0(2) };
17607 0 : validate_res (2, 3, res, expected_res);
17608 0 : }
17609 :
17610 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17611 : when arg0, arg1 and sel have different number of patterns.
17612 : arg0 is of shape (1, 1)
17613 : arg1 is of shape (4, 1)
17614 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17615 :
17616 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17617 : However,
17618 : step = (len+2) - (len+1) = 1
17619 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17620 : Since step is not a multiple of arg_npatterns,
17621 : valid_mask_for_fold_vec_perm_cst should return false,
17622 : and thus fold_vec_perm_cst should return NULL_TREE. */
17623 0 : {
17624 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17625 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17626 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17627 :
17628 0 : vec_perm_builder builder (len, 2, 3);
17629 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17630 0 : builder_push_elems (builder, mask_elems);
17631 :
17632 0 : vec_perm_indices sel (builder, 2, len);
17633 0 : const char *reason;
17634 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17635 :
17636 0 : ASSERT_TRUE (res == NULL_TREE);
17637 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17638 0 : }
17639 :
17640 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17641 : check that the result is not a stepped sequence either, even
17642 : if sel has a stepped sequence. */
17643 0 : {
17644 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17645 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17646 :
17647 0 : vec_perm_builder builder (len, 1, 3);
17648 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17649 0 : builder_push_elems (builder, mask_elems);
17650 :
17651 0 : vec_perm_indices sel (builder, 1, len);
17652 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17653 :
17654 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17655 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17656 0 : }
17657 :
17658 : /* Case 9: If sel doesn't contain a stepped sequence,
17659 : check that the result has same encoding as sel, irrespective
17660 : of shape of input vectors. */
17661 0 : {
17662 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17663 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17664 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17665 :
17666 0 : vec_perm_builder builder (len, 1, 2);
17667 0 : poly_uint64 mask_elems[] = { 0, len };
17668 0 : builder_push_elems (builder, mask_elems);
17669 :
17670 0 : vec_perm_indices sel (builder, 2, len);
17671 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17672 :
17673 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17674 0 : validate_res (sel.encoding ().npatterns (),
17675 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17676 0 : }
17677 : }
17678 0 : }
17679 :
17680 : /* Test all vectors which contain at-least 8 elements. */
17681 :
17682 : static void
17683 0 : test_nunits_min_8 (machine_mode vmode)
17684 : {
17685 0 : for (int i = 0; i < 10; i++)
17686 : {
17687 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17688 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17689 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17690 : arg0[2], arg0[0], arg0[3], arg1[0],
17691 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17692 0 : {
17693 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17694 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17695 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17696 :
17697 0 : vec_perm_builder builder(len, 4, 3);
17698 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17699 0 : 4, 0, 5, len };
17700 0 : builder_push_elems (builder, mask_elems);
17701 :
17702 0 : vec_perm_indices sel (builder, 2, len);
17703 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17704 :
17705 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17706 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17707 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17708 0 : validate_res (4, 3, res, expected_res);
17709 0 : }
17710 : }
17711 0 : }
17712 :
17713 : /* Test vectors for which nunits[0] <= 4. */
17714 :
17715 : static void
17716 0 : test_nunits_max_4 (machine_mode vmode)
17717 : {
17718 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17719 : This should return NULL_TREE because the index 4 may choose
17720 : from either arg0 or arg1 depending on vector length. */
17721 0 : {
17722 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17723 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17724 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17725 :
17726 0 : vec_perm_builder builder (len, 1, 2);
17727 0 : poly_uint64 mask_elems[] = {0, 4};
17728 0 : builder_push_elems (builder, mask_elems);
17729 :
17730 0 : vec_perm_indices sel (builder, 2, len);
17731 0 : const char *reason;
17732 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17733 0 : ASSERT_TRUE (res == NULL_TREE);
17734 0 : ASSERT_TRUE (reason != NULL);
17735 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17736 0 : }
17737 0 : }
17738 :
17739 : #undef ARG0
17740 : #undef ARG1
17741 :
17742 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17743 :
17744 : static bool
17745 0 : is_simple_vla_size (poly_uint64 size)
17746 : {
17747 124 : if (size.is_constant ()
17748 : || !pow2p_hwi (size.coeffs[0]))
17749 0 : return false;
17750 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17751 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17752 : return false;
17753 : return true;
17754 : }
17755 :
17756 : /* Execute fold_vec_perm_cst unit tests. */
17757 :
17758 : static void
17759 4 : test ()
17760 : {
17761 4 : machine_mode vnx4si_mode = E_VOIDmode;
17762 4 : machine_mode v4si_mode = E_VOIDmode;
17763 :
17764 4 : machine_mode vmode;
17765 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17766 : {
17767 : /* Obtain modes corresponding to VNx4SI and V4SI,
17768 : to call mixed mode tests below.
17769 : FIXME: Is there a better way to do this ? */
17770 124 : if (GET_MODE_INNER (vmode) == SImode)
17771 : {
17772 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17773 124 : if (is_simple_vla_size (nunits)
17774 : && nunits.coeffs[0] == 4)
17775 : vnx4si_mode = vmode;
17776 124 : else if (known_eq (nunits, poly_uint64 (4)))
17777 124 : v4si_mode = vmode;
17778 : }
17779 :
17780 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17781 : || !targetm.vector_mode_supported_p (vmode))
17782 124 : continue;
17783 :
17784 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17785 : test_all_nunits (vmode);
17786 : if (nunits.coeffs[0] >= 2)
17787 : test_nunits_min_2 (vmode);
17788 : if (nunits.coeffs[0] >= 4)
17789 : test_nunits_min_4 (vmode);
17790 : if (nunits.coeffs[0] >= 8)
17791 : test_nunits_min_8 (vmode);
17792 :
17793 : if (nunits.coeffs[0] <= 4)
17794 : test_nunits_max_4 (vmode);
17795 : }
17796 :
17797 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17798 : && targetm.vector_mode_supported_p (vnx4si_mode)
17799 : && targetm.vector_mode_supported_p (v4si_mode))
17800 : {
17801 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17802 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17803 : }
17804 4 : }
17805 : } // end of test_fold_vec_perm_cst namespace
17806 :
17807 : /* Verify that various binary operations on vectors are folded
17808 : correctly. */
17809 :
17810 : static void
17811 4 : test_vector_folding ()
17812 : {
17813 4 : tree inner_type = integer_type_node;
17814 4 : tree type = build_vector_type (inner_type, 4);
17815 4 : tree zero = build_zero_cst (type);
17816 4 : tree one = build_one_cst (type);
17817 4 : tree index = build_index_vector (type, 0, 1);
17818 :
17819 : /* Verify equality tests that return a scalar boolean result. */
17820 4 : tree res_type = boolean_type_node;
17821 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17822 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17823 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17824 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17825 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17826 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17827 : index, one)));
17828 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17829 : index, index)));
17830 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17831 : index, index)));
17832 4 : }
17833 :
17834 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17835 :
17836 : static void
17837 4 : test_vec_duplicate_folding ()
17838 : {
17839 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17840 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17841 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17842 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17843 :
17844 4 : tree type = build_vector_type (ssizetype, nunits);
17845 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17846 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17847 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17848 4 : }
17849 :
17850 : /* Verify folding of VEC_UNPACK_{LO,HI}_EXPRs. */
17851 :
17852 : static void
17853 4 : test_vec_unpack_folding ()
17854 : {
17855 4 : machine_mode vmode;
17856 0 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17857 : {
17858 0 : poly_uint64 in_nelts = GET_MODE_NUNITS (vmode);
17859 0 : if (!test_fold_vec_perm_cst::is_simple_vla_size (in_nelts)
17860 : || in_nelts.coeffs[0] < 2
17861 : || !targetm.vector_mode_supported_p (vmode))
17862 0 : continue;
17863 :
17864 : unsigned int in_bits = GET_MODE_UNIT_BITSIZE (vmode);
17865 : if (in_bits >= HOST_BITS_PER_WIDE_INT)
17866 : continue;
17867 :
17868 : unsigned int min_out_nelts = in_nelts.coeffs[0] / 2;
17869 : tree in_inner_type = lang_hooks.types.type_for_mode
17870 : (GET_MODE_INNER (vmode), 1);
17871 : tree out_inner_type = build_nonstandard_integer_type (in_bits * 2, 1);
17872 : tree in_type = build_vector_type_for_mode (in_inner_type, vmode);
17873 : poly_uint64 out_nelts = exact_div (in_nelts, 2);
17874 : tree out_type = build_vector_type (out_inner_type, out_nelts);
17875 :
17876 : tree_vector_builder builder (in_type, min_out_nelts, 2);
17877 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17878 : builder.quick_push (build_int_cst (in_inner_type, i + 1));
17879 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17880 : builder.quick_push (build_int_cst (in_inner_type, 77));
17881 : tree arg = builder.build ();
17882 :
17883 : /* Check integer unpacking with a duplicated scalable high half. */
17884 : tree lo = const_unop (VEC_UNPACK_LO_EXPR, out_type, arg);
17885 : ASSERT_TRUE (lo != NULL_TREE);
17886 : tree hi = const_unop (VEC_UNPACK_HI_EXPR, out_type, arg);
17887 : ASSERT_TRUE (hi != NULL_TREE);
17888 :
17889 : tree prefix = (!BYTES_BIG_ENDIAN ? lo : hi);
17890 : tree dup = (!BYTES_BIG_ENDIAN ? hi : lo);
17891 :
17892 : ASSERT_EQ (VECTOR_CST, TREE_CODE (prefix));
17893 : ASSERT_EQ (VECTOR_CST, TREE_CODE (dup));
17894 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17895 : VECTOR_CST_NELTS (prefix));
17896 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17897 : VECTOR_CST_NELTS (dup));
17898 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (prefix));
17899 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (prefix));
17900 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (dup));
17901 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (dup));
17902 :
17903 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17904 : {
17905 : tree elt = build_int_cst (out_inner_type, i + 1);
17906 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (prefix, i),
17907 : elt, 0));
17908 : tree fill = build_int_cst (out_inner_type, 77);
17909 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17910 : (prefix, min_out_nelts + i),
17911 : fill, 0));
17912 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (dup, i),
17913 : fill, 0));
17914 : }
17915 :
17916 : /* Check int-to-float unpacking with the same scalable shape. */
17917 : tree float_out_type = build_vector_type (float_type_node, out_nelts);
17918 : tree float_lo = const_unop (VEC_UNPACK_FLOAT_LO_EXPR, float_out_type,
17919 : arg);
17920 : ASSERT_TRUE (float_lo != NULL_TREE);
17921 : tree float_hi = const_unop (VEC_UNPACK_FLOAT_HI_EXPR, float_out_type,
17922 : arg);
17923 : ASSERT_TRUE (float_hi != NULL_TREE);
17924 :
17925 : tree float_prefix = (!BYTES_BIG_ENDIAN ? float_lo : float_hi);
17926 : tree float_dup = (!BYTES_BIG_ENDIAN ? float_hi : float_lo);
17927 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_prefix));
17928 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_dup));
17929 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (float_prefix));
17930 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (float_prefix));
17931 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (float_dup));
17932 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (float_dup));
17933 :
17934 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17935 : {
17936 : tree elt = fold_convert_const (FLOAT_EXPR, float_type_node,
17937 : build_int_cst (in_inner_type, i + 1));
17938 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_prefix, i),
17939 : elt, 0));
17940 : tree fill = fold_convert_const (FLOAT_EXPR, float_type_node,
17941 : build_int_cst (in_inner_type, 77));
17942 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17943 : (float_prefix, min_out_nelts + i),
17944 : fill, 0));
17945 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_dup, i),
17946 : fill, 0));
17947 : }
17948 :
17949 : /* Check float-to-int unpacking with the same scalable shape. */
17950 : tree float_in_type = build_vector_type (float_type_node, in_nelts);
17951 : tree_vector_builder fix_builder (float_in_type, min_out_nelts, 2);
17952 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17953 : fix_builder.quick_push
17954 : (build_real_from_int_cst (float_type_node,
17955 : build_int_cst (integer_type_node, i + 1)));
17956 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17957 : fix_builder.quick_push
17958 : (build_real_from_int_cst (float_type_node,
17959 : build_int_cst (integer_type_node, 77)));
17960 : tree fix_arg = fix_builder.build ();
17961 : tree fix_out_type = build_vector_type (integer_type_node, out_nelts);
17962 : tree fix_lo = const_unop (VEC_UNPACK_FIX_TRUNC_LO_EXPR, fix_out_type,
17963 : fix_arg);
17964 : ASSERT_TRUE (fix_lo != NULL_TREE);
17965 : tree fix_hi = const_unop (VEC_UNPACK_FIX_TRUNC_HI_EXPR, fix_out_type,
17966 : fix_arg);
17967 : ASSERT_TRUE (fix_hi != NULL_TREE);
17968 :
17969 : tree fix_prefix = (!BYTES_BIG_ENDIAN ? fix_lo : fix_hi);
17970 : tree fix_dup = (!BYTES_BIG_ENDIAN ? fix_hi : fix_lo);
17971 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_prefix));
17972 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_dup));
17973 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (fix_prefix));
17974 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (fix_prefix));
17975 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (fix_dup));
17976 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (fix_dup));
17977 :
17978 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17979 : {
17980 : tree elt = build_int_cst (integer_type_node, i + 1);
17981 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_prefix, i),
17982 : elt, 0));
17983 : tree fill = build_int_cst (integer_type_node, 77);
17984 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17985 : (fix_prefix, min_out_nelts + i),
17986 : fill, 0));
17987 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_dup, i),
17988 : fill, 0));
17989 : }
17990 :
17991 : /* Reject scalable constants whose high half is not duplicated. */
17992 : tree_vector_builder varied (in_type, in_nelts.coeffs[0], 2);
17993 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
17994 : varied.quick_push (build_int_cst (in_inner_type, i + 1));
17995 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
17996 : varied.quick_push (build_int_cst (in_inner_type, 100 + i));
17997 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
17998 : varied.build ()));
17999 :
18000 : /* Reject stepped scalable constants. */
18001 : tree_vector_builder stepped (in_type, 1, 3);
18002 : stepped.quick_push (build_int_cst (in_inner_type, 1));
18003 : stepped.quick_push (build_int_cst (in_inner_type, 2));
18004 : stepped.quick_push (build_int_cst (in_inner_type, 3));
18005 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
18006 : stepped.build ()));
18007 :
18008 : return;
18009 : }
18010 : }
18011 :
18012 : /* Run all of the selftests within this file. */
18013 :
18014 : void
18015 4 : fold_const_cc_tests ()
18016 : {
18017 4 : test_arithmetic_folding ();
18018 4 : test_vector_folding ();
18019 4 : test_vec_duplicate_folding ();
18020 4 : test_vec_unpack_folding ();
18021 4 : test_fold_vec_perm_cst::test ();
18022 4 : test_operand_equality::test ();
18023 4 : }
18024 :
18025 : } // namespace selftest
18026 :
18027 : #endif /* CHECKING_P */
|