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 372 : operator ~(comparison_code cmp)
132 : {
133 372 : unsigned char newcmp = cmp;
134 372 : newcmp = ~newcmp & 0xF;
135 372 : return (comparison_code)newcmp;
136 : }
137 :
138 : // Implements bitwise ior on comparison_code.
139 : comparison_code
140 12911 : operator |(comparison_code cmp0, comparison_code cmp1)
141 : {
142 12911 : unsigned char newcmp = ((unsigned char)cmp0) | cmp1;
143 12911 : newcmp = newcmp & 0xF;
144 12911 : return (comparison_code)newcmp;
145 : }
146 :
147 : // Implements bitwise and on comparison_code.
148 : comparison_code
149 65919 : operator &(comparison_code cmp0, comparison_code cmp1)
150 : {
151 65919 : unsigned char newcmp = ((unsigned char)cmp0) & cmp1;
152 65919 : newcmp = newcmp & 0xF;
153 65919 : return (comparison_code)newcmp;
154 : }
155 :
156 : // Implements bitwise xor on comparison_code.
157 : comparison_code
158 1409 : operator ^(comparison_code cmp0, comparison_code cmp1)
159 : {
160 1409 : unsigned char newcmp = ((unsigned char)cmp0) ^ cmp1;
161 1409 : newcmp = newcmp & 0xF;
162 1409 : 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 142363 : minmax_from_comparison (tree_code cmp, tree exp0,
200 : const widest_int &cst1,
201 : const widest_int &cst2)
202 : {
203 142363 : 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 142344 : if (cst1 == cst2 - 1)
211 : {
212 : /* X <= Y - 1 equals to X < Y. */
213 85248 : if (cmp == LE_EXPR)
214 : return MIN_EXPR;
215 : /* X > Y - 1 equals to X >= Y. */
216 84787 : 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 72731 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
220 : {
221 20733 : int_range_max r;
222 41466 : get_range_query (cfun)->range_of_expr (r, exp0);
223 20733 : if (r.undefined_p ())
224 0 : r.set_varying (TREE_TYPE (exp0));
225 :
226 20733 : widest_int min = widest_int::from (r.lower_bound (),
227 41466 : TYPE_SIGN (TREE_TYPE (exp0)));
228 20733 : if (min == cst1)
229 502 : return MAX_EXPR;
230 20733 : }
231 : }
232 129325 : if (cst1 == cst2 + 1)
233 : {
234 : /* X < Y + 1 equals to X <= Y. */
235 1299 : if (cmp == LT_EXPR)
236 : return MIN_EXPR;
237 : /* X >= Y + 1 equals to X > Y. */
238 1271 : 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 1108 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
242 : {
243 700 : int_range_max r;
244 1400 : get_range_query (cfun)->range_of_expr (r, exp0);
245 700 : if (r.undefined_p ())
246 0 : r.set_varying (TREE_TYPE (exp0));
247 :
248 700 : widest_int max = widest_int::from (r.upper_bound (),
249 1400 : TYPE_SIGN (TREE_TYPE (exp0)));
250 700 : if (max == cst1)
251 194 : return MIN_EXPR;
252 700 : }
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 175298 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
262 : {
263 175298 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
264 : return ERROR_MARK;
265 :
266 175287 : if (!operand_equal_p (exp0, exp2))
267 : return ERROR_MARK;
268 :
269 175287 : if (operand_equal_p (exp1, exp3))
270 : {
271 32851 : if (cmp == LT_EXPR || cmp == LE_EXPR)
272 : return MIN_EXPR;
273 30639 : if (cmp == GT_EXPR || cmp == GE_EXPR)
274 : return MAX_EXPR;
275 : }
276 142550 : if (TREE_CODE (exp3) == INTEGER_CST
277 142070 : && TREE_CODE (exp1) == INTEGER_CST)
278 141614 : 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 3066300 : expr_location_or (tree t, location_t loc)
287 : {
288 934786 : location_t tloc = EXPR_LOCATION (t);
289 3049853 : 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 9458579 : protected_set_expr_location_unshare (tree x, location_t loc)
297 : {
298 9458579 : if (CAN_HAVE_LOCATION_P (x)
299 8404264 : && EXPR_LOCATION (x) != loc
300 2683324 : && !(TREE_CODE (x) == SAVE_EXPR
301 1341877 : || TREE_CODE (x) == TARGET_EXPR
302 : || TREE_CODE (x) == BIND_EXPR))
303 : {
304 1341090 : x = copy_node (x);
305 1341090 : SET_EXPR_LOCATION (x, loc);
306 : }
307 9458579 : 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 2183736 : negate_mathfn_p (combined_fn fn)
315 : {
316 2183736 : 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 2179542 : default:
386 2179542 : break;
387 : }
388 2179542 : return false;
389 : }
390 :
391 : /* Check whether we may negate an integer constant T without causing
392 : overflow. */
393 :
394 : bool
395 2854833 : may_negate_without_overflow_p (const_tree t)
396 : {
397 2854833 : tree type;
398 :
399 2854833 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
400 :
401 2854833 : type = TREE_TYPE (t);
402 2854833 : if (TYPE_UNSIGNED (type))
403 : return false;
404 :
405 2854833 : 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 28351731 : negate_expr_p (tree t)
413 : {
414 28513703 : tree type;
415 :
416 28513703 : if (t == 0)
417 : return false;
418 :
419 28513703 : type = TREE_TYPE (t);
420 :
421 28513703 : STRIP_SIGN_NOPS (t);
422 28513703 : switch (TREE_CODE (t))
423 : {
424 1707075 : case INTEGER_CST:
425 1707075 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
426 : return true;
427 :
428 : /* Check that -CST will not overflow type. */
429 381050 : 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 1288 : case NEGATE_EXPR:
438 1288 : return !TYPE_OVERFLOW_SANITIZED (type);
439 :
440 1299306 : case REAL_CST:
441 : /* We want to canonicalize to positive real constants. Pretend
442 : that only negative ones can be easily negated. */
443 1299306 : 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 1609622 : case PLUS_EXPR:
471 1609622 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
472 1609616 : || HONOR_SIGNED_ZEROS (type)
473 2930689 : || (ANY_INTEGRAL_TYPE_P (type)
474 1320859 : && ! TYPE_OVERFLOW_WRAPS (type)))
475 : return false;
476 : /* -(A + B) -> (-B) - A. */
477 847517 : if (negate_expr_p (TREE_OPERAND (t, 1)))
478 : return true;
479 : /* -(A + B) -> (-A) - B. */
480 151386 : return negate_expr_p (TREE_OPERAND (t, 0));
481 :
482 262543 : case MINUS_EXPR:
483 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
484 262543 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
485 262543 : && !HONOR_SIGNED_ZEROS (type)
486 350763 : && (! ANY_INTEGRAL_TYPE_P (type)
487 87997 : || TYPE_OVERFLOW_WRAPS (type));
488 :
489 2372311 : case MULT_EXPR:
490 2372311 : 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 4115040 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
495 154962 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
496 2210167 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
497 0 : && (wi::popcount
498 2057520 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
499 152647 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
500 128636 : && (wi::popcount
501 2186156 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
502 : break;
503 :
504 : /* Fall through. */
505 :
506 2336292 : case RDIV_EXPR:
507 2336292 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
508 2336291 : return negate_expr_p (TREE_OPERAND (t, 1))
509 2336291 : || negate_expr_p (TREE_OPERAND (t, 0));
510 : break;
511 :
512 2602 : case TRUNC_DIV_EXPR:
513 2602 : case ROUND_DIV_EXPR:
514 2602 : case EXACT_DIV_EXPR:
515 2602 : 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 553 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
520 553 : && 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 5199166 : case NOP_EXPR:
533 : /* Negate -((double)float) as (double)(-float). */
534 5199166 : if (SCALAR_FLOAT_TYPE_P (type))
535 : {
536 10335 : tree tem = strip_float_extensions (t);
537 10335 : if (tem != t)
538 : return negate_expr_p (tem);
539 : }
540 : break;
541 :
542 1089589 : case CALL_EXPR:
543 : /* Negate -f(x) as f(-x). */
544 1089589 : if (negate_mathfn_p (get_call_combined_fn (t)))
545 67 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
546 : break;
547 :
548 13120 : case RSHIFT_EXPR:
549 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
550 13120 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
551 : {
552 12975 : tree op1 = TREE_OPERAND (t, 1);
553 12975 : 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 39565537 : fold_negate_expr_1 (location_t loc, tree t)
571 : {
572 39565537 : tree type = TREE_TYPE (t);
573 39565537 : tree tem;
574 :
575 39565537 : 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 30244528 : case INTEGER_CST:
585 30244528 : tem = fold_negate_const (t, type);
586 30244528 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
587 9957 : || (ANY_INTEGRAL_TYPE_P (type)
588 9957 : && !TYPE_OVERFLOW_TRAPS (type)
589 9957 : && TYPE_OVERFLOW_WRAPS (type))
590 30254162 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
591 30244416 : return tem;
592 : break;
593 :
594 2035267 : case POLY_INT_CST:
595 2035267 : case REAL_CST:
596 2035267 : case FIXED_CST:
597 2035267 : tem = fold_negate_const (t, type);
598 2035267 : return tem;
599 :
600 63179 : case COMPLEX_CST:
601 63179 : {
602 63179 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
603 63179 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
604 63179 : if (rpart && ipart)
605 63179 : return build_complex (type, rpart, ipart);
606 : }
607 : break;
608 :
609 41700 : case VECTOR_CST:
610 41700 : {
611 41700 : tree_vector_builder elts;
612 41700 : elts.new_unary_operation (type, t, true);
613 41700 : unsigned int count = elts.encoded_nelts ();
614 100247 : for (unsigned int i = 0; i < count; ++i)
615 : {
616 58547 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
617 58547 : if (elt == NULL_TREE)
618 0 : return NULL_TREE;
619 58547 : elts.quick_push (elt);
620 : }
621 :
622 41700 : return elts.build ();
623 41700 : }
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 1227 : case NEGATE_EXPR:
639 1227 : if (!TYPE_OVERFLOW_SANITIZED (type))
640 1201 : return TREE_OPERAND (t, 0);
641 : break;
642 :
643 760014 : case PLUS_EXPR:
644 760014 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
645 760014 : && !HONOR_SIGNED_ZEROS (type))
646 : {
647 : /* -(A + B) -> (-B) - A. */
648 759952 : if (negate_expr_p (TREE_OPERAND (t, 1)))
649 : {
650 700135 : tem = negate_expr (TREE_OPERAND (t, 1));
651 700135 : return fold_build2_loc (loc, MINUS_EXPR, type,
652 1400270 : tem, TREE_OPERAND (t, 0));
653 : }
654 :
655 : /* -(A + B) -> (-A) - B. */
656 59817 : if (negate_expr_p (TREE_OPERAND (t, 0)))
657 : {
658 1653 : tem = negate_expr (TREE_OPERAND (t, 0));
659 1653 : return fold_build2_loc (loc, MINUS_EXPR, type,
660 3306 : tem, TREE_OPERAND (t, 1));
661 : }
662 : }
663 : break;
664 :
665 158957 : case MINUS_EXPR:
666 : /* - (A - B) -> B - A */
667 158957 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
668 158957 : && !HONOR_SIGNED_ZEROS (type))
669 81768 : return fold_build2_loc (loc, MINUS_EXPR, type,
670 163536 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
671 : break;
672 :
673 270164 : case MULT_EXPR:
674 270164 : if (TYPE_UNSIGNED (type))
675 : break;
676 :
677 : /* Fall through. */
678 :
679 32918 : case RDIV_EXPR:
680 32918 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
681 : {
682 32918 : tem = TREE_OPERAND (t, 1);
683 32918 : if (negate_expr_p (tem))
684 59542 : return fold_build2_loc (loc, TREE_CODE (t), type,
685 59542 : TREE_OPERAND (t, 0), negate_expr (tem));
686 3147 : tem = TREE_OPERAND (t, 0);
687 3147 : if (negate_expr_p (tem))
688 48 : return fold_build2_loc (loc, TREE_CODE (t), type,
689 96 : negate_expr (tem), TREE_OPERAND (t, 1));
690 : }
691 : break;
692 :
693 2081 : case TRUNC_DIV_EXPR:
694 2081 : case ROUND_DIV_EXPR:
695 2081 : case EXACT_DIV_EXPR:
696 2081 : 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 731 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
701 731 : && negate_expr_p (TREE_OPERAND (t, 0)))
702 331 : return fold_build2_loc (loc, TREE_CODE (t), type,
703 331 : negate_expr (TREE_OPERAND (t, 0)),
704 662 : 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 2399389 : case NOP_EXPR:
719 : /* Convert -((double)float) into (double)(-float). */
720 2399389 : if (SCALAR_FLOAT_TYPE_P (type))
721 : {
722 10889 : tem = strip_float_extensions (t);
723 10889 : if (tem != t && negate_expr_p (tem))
724 0 : return fold_convert_loc (loc, type, negate_expr (tem));
725 : }
726 : break;
727 :
728 296417 : case CALL_EXPR:
729 : /* Negate -f(x) as f(-x). */
730 296417 : if (negate_mathfn_p (get_call_combined_fn (t))
731 297706 : && 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 12831 : case RSHIFT_EXPR:
742 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
743 12831 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
744 : {
745 12813 : tree op1 = TREE_OPERAND (t, 1);
746 12813 : if (wi::to_wide (op1) == element_precision (type) - 1)
747 : {
748 12470 : tree ntype = TYPE_UNSIGNED (type)
749 12470 : ? signed_type_for (type)
750 72 : : unsigned_type_for (type);
751 12470 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
752 12470 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
753 12470 : 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 39565537 : fold_negate_expr (location_t loc, tree t)
769 : {
770 39565537 : tree type = TREE_TYPE (t);
771 39565537 : STRIP_SIGN_NOPS (t);
772 39565537 : tree tem = fold_negate_expr_1 (loc, t);
773 39565537 : if (tem == NULL_TREE)
774 : return NULL_TREE;
775 33213680 : 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 3989887 : negate_expr (tree t)
784 : {
785 3989887 : tree type, tem;
786 3989887 : location_t loc;
787 :
788 3989887 : if (t == NULL_TREE)
789 : return NULL_TREE;
790 :
791 3989887 : loc = EXPR_LOCATION (t);
792 3989887 : type = TREE_TYPE (t);
793 3989887 : STRIP_SIGN_NOPS (t);
794 :
795 3989887 : tem = fold_negate_expr (loc, t);
796 3989887 : if (!tem)
797 1958883 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
798 3989887 : 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 229899804 : 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 229899804 : tree var = 0;
830 229899804 : *minus_varp = 0;
831 229899804 : *conp = 0;
832 229899804 : *minus_conp = 0;
833 229899804 : *litp = 0;
834 229899804 : *minus_litp = 0;
835 :
836 : /* Strip any conversions that don't change the machine mode or signedness. */
837 229899804 : STRIP_SIGN_NOPS (in);
838 :
839 229899804 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
840 147982274 : || TREE_CODE (in) == FIXED_CST)
841 81917530 : *litp = in;
842 147982274 : else if (TREE_CODE (in) == code
843 147982274 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
844 143324099 : && ! 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 143324099 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
850 61447035 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
851 141589615 : || (code == MINUS_EXPR
852 24001898 : && (TREE_CODE (in) == PLUS_EXPR
853 22067292 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
854 : {
855 8774189 : tree op0 = TREE_OPERAND (in, 0);
856 8774189 : tree op1 = TREE_OPERAND (in, 1);
857 8774189 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
858 8774189 : 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 8774189 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
862 8544688 : || TREE_CODE (op0) == FIXED_CST)
863 229501 : *litp = op0, op0 = 0;
864 8544688 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
865 5668001 : || TREE_CODE (op1) == FIXED_CST)
866 2876687 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
867 :
868 8774189 : if (op0 != 0 && TREE_CONSTANT (op0))
869 13782 : *conp = op0, op0 = 0;
870 8760407 : else if (op1 != 0 && TREE_CONSTANT (op1))
871 52165 : *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 8774189 : if (op0 != 0 && op1 != 0)
876 : var = in;
877 3165674 : else if (op0 != 0)
878 : var = op0;
879 : else
880 243283 : var = op1, neg_var_p = neg1_p;
881 :
882 : /* Now do any needed negations. */
883 8774189 : if (neg_litp_p)
884 28901 : *minus_litp = *litp, *litp = 0;
885 8774189 : if (neg_conp_p && *conp)
886 11431 : *minus_conp = *conp, *conp = 0;
887 8774189 : if (neg_var_p && var)
888 233502 : *minus_varp = var, var = 0;
889 : }
890 139208085 : else if (TREE_CONSTANT (in))
891 829970 : *conp = in;
892 138378115 : else if (TREE_CODE (in) == BIT_NOT_EXPR
893 570819 : && code == PLUS_EXPR)
894 : {
895 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
896 : when IN is constant. */
897 410104 : *litp = build_minus_one_cst (type);
898 410104 : *minus_varp = TREE_OPERAND (in, 0);
899 : }
900 : else
901 : var = in;
902 :
903 229899804 : if (negate_p)
904 : {
905 13249829 : if (*litp)
906 1314489 : *minus_litp = *litp, *litp = 0;
907 11935340 : else if (*minus_litp)
908 174 : *litp = *minus_litp, *minus_litp = 0;
909 13249829 : if (*conp)
910 46512 : *minus_conp = *conp, *conp = 0;
911 13203317 : else if (*minus_conp)
912 0 : *conp = *minus_conp, *minus_conp = 0;
913 13249829 : if (var)
914 13190804 : *minus_varp = var, var = 0;
915 59025 : else if (*minus_varp)
916 889 : var = *minus_varp, *minus_varp = 0;
917 : }
918 :
919 229899804 : if (*litp
920 229899804 : && TREE_OVERFLOW_P (*litp))
921 20705 : *litp = drop_tree_overflow (*litp);
922 229899804 : if (*minus_litp
923 229899804 : && TREE_OVERFLOW_P (*minus_litp))
924 24 : *minus_litp = drop_tree_overflow (*minus_litp);
925 :
926 229899804 : 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 21959957 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
936 : {
937 21959957 : if (t1 == 0)
938 : {
939 13943568 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
940 : return t2;
941 : }
942 8016389 : 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 4474436 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
949 3536721 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
950 3469512 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
951 : {
952 1741332 : if (code == PLUS_EXPR)
953 : {
954 977584 : if (TREE_CODE (t1) == NEGATE_EXPR)
955 89 : return build2_loc (loc, MINUS_EXPR, type,
956 : fold_convert_loc (loc, type, t2),
957 : fold_convert_loc (loc, type,
958 178 : TREE_OPERAND (t1, 0)));
959 977495 : 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 977494 : else if (integer_zerop (t2))
965 38820 : return fold_convert_loc (loc, type, t1);
966 : }
967 763748 : else if (code == MINUS_EXPR)
968 : {
969 739613 : if (integer_zerop (t2))
970 0 : return fold_convert_loc (loc, type, t1);
971 : }
972 :
973 1702422 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
974 1702422 : fold_convert_loc (loc, type, t2));
975 : }
976 :
977 2733104 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
978 2733104 : 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 2725833260 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
986 : {
987 2725833260 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
988 : return false;
989 2725833260 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
990 : return false;
991 :
992 2725833260 : switch (code)
993 : {
994 : case LSHIFT_EXPR:
995 : case RSHIFT_EXPR:
996 : case LROTATE_EXPR:
997 : case RROTATE_EXPR:
998 : return true;
999 :
1000 2725833260 : default:
1001 2725833260 : break;
1002 : }
1003 :
1004 2725833260 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1005 2725833260 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1006 5451666520 : && 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 1531585673 : 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 1531585673 : wide_int tmp;
1019 1531585673 : *overflow = wi::OVF_NONE;
1020 1531585673 : switch (code)
1021 : {
1022 3749115 : case BIT_IOR_EXPR:
1023 3749115 : res = wi::bit_or (arg1, arg2);
1024 3749115 : break;
1025 :
1026 100959 : case BIT_XOR_EXPR:
1027 100959 : res = wi::bit_xor (arg1, arg2);
1028 100959 : break;
1029 :
1030 23616675 : case BIT_AND_EXPR:
1031 23616675 : res = wi::bit_and (arg1, arg2);
1032 23616675 : break;
1033 :
1034 15054526 : case LSHIFT_EXPR:
1035 15054526 : if (wi::neg_p (arg2))
1036 : return false;
1037 15024139 : res = wi::lshift (arg1, arg2);
1038 15024139 : break;
1039 :
1040 7660261 : case RSHIFT_EXPR:
1041 7660261 : 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 7660065 : res = wi::rshift (arg1, arg2, sign);
1047 7660065 : 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 261154018 : case PLUS_EXPR:
1069 261154018 : res = wi::add (arg1, arg2, sign, overflow);
1070 261154018 : break;
1071 :
1072 76240985 : case MINUS_EXPR:
1073 76240985 : res = wi::sub (arg1, arg2, sign, overflow);
1074 76240985 : break;
1075 :
1076 438109539 : case MULT_EXPR:
1077 438109539 : res = wi::mul (arg1, arg2, sign, overflow);
1078 438109539 : break;
1079 :
1080 6320 : case MULT_HIGHPART_EXPR:
1081 6320 : res = wi::mul_high (arg1, arg2, sign);
1082 6320 : break;
1083 :
1084 361196140 : case TRUNC_DIV_EXPR:
1085 361196140 : case EXACT_DIV_EXPR:
1086 361196140 : if (arg2 == 0)
1087 : return false;
1088 361190201 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1089 361190201 : break;
1090 :
1091 81597827 : case FLOOR_DIV_EXPR:
1092 81597827 : if (arg2 == 0)
1093 : return false;
1094 81597827 : res = wi::div_floor (arg1, arg2, sign, overflow);
1095 81597827 : break;
1096 :
1097 88578313 : case CEIL_DIV_EXPR:
1098 88578313 : if (arg2 == 0)
1099 : return false;
1100 88578313 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1101 88578313 : 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 1379185 : case TRUNC_MOD_EXPR:
1110 1379185 : if (arg2 == 0)
1111 : return false;
1112 1378068 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1113 1378068 : break;
1114 :
1115 69531035 : case FLOOR_MOD_EXPR:
1116 69531035 : if (arg2 == 0)
1117 : return false;
1118 69531035 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1119 69531035 : 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 48887 : case MIN_EXPR:
1134 48887 : res = wi::min (arg1, arg2, sign);
1135 48887 : break;
1136 :
1137 103559863 : case MAX_EXPR:
1138 103559863 : res = wi::max (arg1, arg2, sign);
1139 103559863 : break;
1140 :
1141 : default:
1142 : return false;
1143 : }
1144 : return true;
1145 1531585673 : }
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 1531585673 : 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 1531585673 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1176 :
1177 1531585673 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1178 : {
1179 1531585673 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1180 1531585673 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1181 1531585673 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1182 : return NULL_TREE;
1183 1531547907 : res = wi_res;
1184 1531547907 : return true;
1185 1531586097 : }
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 1531585673 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1250 : int overflowable)
1251 : {
1252 1531585673 : poly_wide_int poly_res;
1253 1531585673 : tree type = TREE_TYPE (arg1);
1254 1531585673 : signop sign = TYPE_SIGN (type);
1255 1531585673 : wi::overflow_type overflow = wi::OVF_NONE;
1256 :
1257 1531585673 : if (!poly_int_tree_p (arg1)
1258 1531585673 : || !poly_int_tree_p (arg2)
1259 3063171346 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1260 : return NULL_TREE;
1261 1531547907 : return force_fit_type (type, poly_res, overflowable,
1262 1531547907 : (((sign == SIGNED || overflowable == -1)
1263 1531547907 : && overflow)
1264 1531547907 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1265 1531585673 : }
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 205291 : 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 4111 : default:
1284 4111 : 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 475863 : 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 475863 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1297 : {
1298 392045 : if (integer_zerop (other_op))
1299 : {
1300 29070 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1301 : return op;
1302 27842 : else if (code == BIT_AND_EXPR)
1303 4237 : 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 283071 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1316 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1317 : {
1318 208035 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1319 482345 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1320 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1321 : {
1322 199274 : tree type = TREE_TYPE (arg1);
1323 199274 : bool step_ok_p;
1324 199274 : if (VECTOR_CST_STEPPED_P (arg1)
1325 199274 : && 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 2744 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1335 196530 : 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 55748 : step_ok_p = distributes_over_addition_p (code, 1);
1344 : else
1345 : /* Similarly in reverse. */
1346 140782 : step_ok_p = distributes_over_addition_p (code, 2);
1347 199274 : tree_vector_builder elts;
1348 199274 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1349 : return NULL_TREE;
1350 199274 : unsigned int count = elts.encoded_nelts ();
1351 768328 : for (unsigned int i = 0; i < count; ++i)
1352 : {
1353 569373 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1354 569373 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1355 :
1356 569373 : 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 569373 : if (elt == NULL_TREE)
1361 319 : return NULL_TREE;
1362 569054 : elts.quick_push (elt);
1363 : }
1364 :
1365 198955 : return elts.build ();
1366 199274 : }
1367 :
1368 83797 : if (TREE_CODE (arg1) == VECTOR_CST
1369 8761 : && TREE_CODE (arg2) == INTEGER_CST)
1370 : {
1371 8761 : tree type = TREE_TYPE (arg1);
1372 8761 : bool step_ok_p = distributes_over_addition_p (code, 1);
1373 8761 : tree_vector_builder elts;
1374 8761 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1375 : return NULL_TREE;
1376 8761 : unsigned int count = elts.encoded_nelts ();
1377 37745 : for (unsigned int i = 0; i < count; ++i)
1378 : {
1379 29071 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1380 :
1381 29071 : 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 29071 : if (elt == NULL_TREE)
1386 87 : return NULL_TREE;
1387 28984 : elts.quick_push (elt);
1388 : }
1389 :
1390 8674 : return elts.build ();
1391 8761 : }
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 227396610 : const_binop (enum tree_code code, tree arg1, tree arg2)
1402 : {
1403 : /* Sanity check for the recursive cases. */
1404 227396610 : if (!arg1 || !arg2)
1405 : return NULL_TREE;
1406 :
1407 227395346 : STRIP_NOPS (arg1);
1408 227395346 : STRIP_NOPS (arg2);
1409 :
1410 227395346 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1411 : {
1412 221556141 : if (code == POINTER_PLUS_EXPR)
1413 103843 : return int_const_binop (PLUS_EXPR,
1414 207686 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1415 :
1416 221452298 : return int_const_binop (code, arg1, arg2);
1417 : }
1418 :
1419 5839205 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1420 : {
1421 5539518 : machine_mode mode;
1422 5539518 : REAL_VALUE_TYPE d1;
1423 5539518 : REAL_VALUE_TYPE d2;
1424 5539518 : REAL_VALUE_TYPE value;
1425 5539518 : REAL_VALUE_TYPE result;
1426 5539518 : bool inexact;
1427 5539518 : tree t, type;
1428 :
1429 : /* The following codes are handled by real_arithmetic. */
1430 5539518 : switch (code)
1431 : {
1432 5539518 : case PLUS_EXPR:
1433 5539518 : case MINUS_EXPR:
1434 5539518 : case MULT_EXPR:
1435 5539518 : case RDIV_EXPR:
1436 5539518 : case MIN_EXPR:
1437 5539518 : case MAX_EXPR:
1438 5539518 : break;
1439 :
1440 : default:
1441 : return NULL_TREE;
1442 : }
1443 :
1444 5539518 : d1 = TREE_REAL_CST (arg1);
1445 5539518 : d2 = TREE_REAL_CST (arg2);
1446 :
1447 5539518 : type = TREE_TYPE (arg1);
1448 5539518 : mode = TYPE_MODE (type);
1449 :
1450 : /* Don't perform operation if we honor signaling NaNs and
1451 : either operand is a signaling NaN. */
1452 5539518 : if (HONOR_SNANS (mode)
1453 5539518 : && (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 5539485 : if (code == RDIV_EXPR
1460 2405983 : && real_equal (&d2, &dconst0)
1461 5550165 : && (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 5531717 : 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 5531371 : 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 5531311 : inexact = real_arithmetic (&value, code, &d1, &d2);
1484 5531311 : 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 5531311 : if (flag_trapping_math
1491 21476290 : && MODE_HAS_NANS (mode)
1492 5510621 : && REAL_VALUE_ISNAN (result)
1493 2583 : && !REAL_VALUE_ISNAN (d1)
1494 5533894 : && !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 5528728 : if (flag_trapping_math
1500 21466300 : && MODE_HAS_INFINITIES (mode)
1501 5508038 : && REAL_VALUE_ISINF (result)
1502 7846 : && !REAL_VALUE_ISINF (d1)
1503 5535906 : && !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 5523833 : if ((flag_rounding_math
1513 37516774 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1514 5523833 : && (inexact
1515 2649 : || !real_identical (&result, &value)
1516 2185 : || (real_iszero (&value) && HONOR_SIGNED_ZEROS (mode))))
1517 : return NULL_TREE;
1518 :
1519 5520976 : t = build_real (type, result);
1520 :
1521 5520976 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1522 5520976 : return t;
1523 : }
1524 :
1525 299687 : 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 299687 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1574 : {
1575 11309 : tree type = TREE_TYPE (arg1);
1576 11309 : tree r1 = TREE_REALPART (arg1);
1577 11309 : tree i1 = TREE_IMAGPART (arg1);
1578 11309 : tree r2 = TREE_REALPART (arg2);
1579 11309 : tree i2 = TREE_IMAGPART (arg2);
1580 11309 : tree real, imag;
1581 :
1582 11309 : switch (code)
1583 : {
1584 5344 : case PLUS_EXPR:
1585 5344 : case MINUS_EXPR:
1586 5344 : real = const_binop (code, r1, r2);
1587 5344 : imag = const_binop (code, i1, i2);
1588 5344 : break;
1589 :
1590 4005 : case MULT_EXPR:
1591 4005 : if (COMPLEX_FLOAT_TYPE_P (type))
1592 2853 : return do_mpc_arg2 (arg1, arg2, type,
1593 : /* do_nonfinite= */ folding_initializer,
1594 2853 : 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 6750 : if (real && imag)
1699 6592 : return build_complex (type, real, imag);
1700 : }
1701 :
1702 288536 : tree simplified;
1703 288536 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1704 : return simplified;
1705 :
1706 287909 : if (commutative_tree_code (code)
1707 287909 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1708 : return simplified;
1709 :
1710 283071 : 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 304009362 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1718 : {
1719 304009362 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1720 84346672 : 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 219662690 : 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 270077 : case COMPLEX_EXPR:
1733 270077 : if ((TREE_CODE (arg1) == REAL_CST
1734 259505 : && TREE_CODE (arg2) == REAL_CST)
1735 10577 : || (TREE_CODE (arg1) == INTEGER_CST
1736 10569 : && TREE_CODE (arg2) == INTEGER_CST))
1737 270069 : return build_complex (type, arg1, arg2);
1738 : return NULL_TREE;
1739 :
1740 180297 : case POINTER_DIFF_EXPR:
1741 180297 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1742 : {
1743 359946 : poly_offset_int res = (wi::to_poly_offset (arg1)
1744 179973 : - wi::to_poly_offset (arg2));
1745 179973 : return force_fit_type (type, res, 1,
1746 179973 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1747 : }
1748 : return NULL_TREE;
1749 :
1750 15605 : case VEC_PACK_TRUNC_EXPR:
1751 15605 : case VEC_PACK_FIX_TRUNC_EXPR:
1752 15605 : case VEC_PACK_FLOAT_EXPR:
1753 15605 : {
1754 15605 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1755 :
1756 15605 : if (TREE_CODE (arg1) != VECTOR_CST
1757 15605 : || TREE_CODE (arg2) != VECTOR_CST)
1758 : return NULL_TREE;
1759 :
1760 15605 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1761 : return NULL_TREE;
1762 :
1763 15605 : out_nelts = in_nelts * 2;
1764 15605 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1765 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1766 :
1767 15605 : tree_vector_builder elts (type, out_nelts, 1);
1768 209994 : for (i = 0; i < out_nelts; i++)
1769 : {
1770 178796 : tree elt = (i < in_nelts
1771 178796 : ? VECTOR_CST_ELT (arg1, i)
1772 89392 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1773 179840 : 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 178796 : TREE_TYPE (type), elt);
1778 178796 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1779 12 : return NULL_TREE;
1780 178784 : elts.quick_push (elt);
1781 : }
1782 :
1783 15593 : return elts.build ();
1784 15605 : }
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 219196480 : default:;
1832 : }
1833 :
1834 219196480 : if (TREE_CODE_CLASS (code) != tcc_binary)
1835 : return NULL_TREE;
1836 :
1837 : /* Make sure type and arg0 have the same saturating flag. */
1838 216706897 : gcc_checking_assert (TYPE_SATURATING (type)
1839 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1840 :
1841 216706897 : 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 379372261 : 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 379372261 : if (TREE_CODE (arg0) == REAL_CST
1853 11066939 : && HONOR_SNANS (arg0)
1854 17271 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1855 4747 : && code != NEGATE_EXPR
1856 4747 : && code != ABS_EXPR
1857 379376973 : && code != ABSU_EXPR)
1858 : return NULL_TREE;
1859 :
1860 379367549 : switch (code)
1861 : {
1862 285085967 : CASE_CONVERT:
1863 285085967 : case FLOAT_EXPR:
1864 285085967 : case FIX_TRUNC_EXPR:
1865 285085967 : case FIXED_CONVERT_EXPR:
1866 285085967 : 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 13226431 : case VIEW_CONVERT_EXPR:
1878 13226431 : return fold_view_convert_expr (type, arg0);
1879 :
1880 31141920 : case NEGATE_EXPR:
1881 31141920 : {
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 31141920 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1886 31141920 : if (tem && CONSTANT_CLASS_P (tem))
1887 30996011 : return tem;
1888 : break;
1889 : }
1890 :
1891 40973 : case ABS_EXPR:
1892 40973 : case ABSU_EXPR:
1893 40973 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1894 36272 : 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 2417005 : case BIT_NOT_EXPR:
1907 2417005 : if (TREE_CODE (arg0) == INTEGER_CST)
1908 2409823 : return fold_not_const (arg0, type);
1909 7182 : 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 7182 : 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 11627025 : case TRUTH_NOT_EXPR:
1934 11627025 : if (TREE_CODE (arg0) == INTEGER_CST)
1935 11259172 : return constant_boolean_node (integer_zerop (arg0), type);
1936 : break;
1937 :
1938 180346 : case REALPART_EXPR:
1939 180346 : if (TREE_CODE (arg0) == COMPLEX_CST)
1940 180145 : return fold_convert (type, TREE_REALPART (arg0));
1941 : break;
1942 :
1943 186078 : case IMAGPART_EXPR:
1944 186078 : if (TREE_CODE (arg0) == COMPLEX_CST)
1945 185890 : return fold_convert (type, TREE_IMAGPART (arg0));
1946 : break;
1947 :
1948 19336 : case VEC_UNPACK_LO_EXPR:
1949 19336 : case VEC_UNPACK_HI_EXPR:
1950 19336 : case VEC_UNPACK_FLOAT_LO_EXPR:
1951 19336 : case VEC_UNPACK_FLOAT_HI_EXPR:
1952 19336 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1953 19336 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1954 19336 : {
1955 19336 : poly_uint64 out_nelts = TYPE_VECTOR_SUBPARTS (type);
1956 19336 : unsigned HOST_WIDE_INT npatterns, nelts_per_pattern, i, offset = 0;
1957 19336 : enum tree_code subcode;
1958 :
1959 19336 : if (TREE_CODE (arg0) != VECTOR_CST)
1960 : return NULL_TREE;
1961 :
1962 19336 : gcc_assert (known_eq (VECTOR_CST_NELTS (arg0), out_nelts * 2));
1963 :
1964 19336 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1965 : subcode = NOP_EXPR;
1966 8110 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1967 8110 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1968 : subcode = FLOAT_EXPR;
1969 : else
1970 4 : subcode = FIX_TRUNC_EXPR;
1971 :
1972 38672 : bool low_p = (code == VEC_UNPACK_LO_EXPR
1973 19336 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1974 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR);
1975 :
1976 19336 : npatterns = VECTOR_CST_NPATTERNS (arg0);
1977 19336 : nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (arg0);
1978 25571 : 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 19336 : if ((!BYTES_BIG_ENDIAN) ^ low_p)
1999 : {
2000 9660 : offset = npatterns;
2001 9660 : nelts_per_pattern = 1;
2002 : }
2003 :
2004 19336 : tree_vector_builder elts (type, npatterns, nelts_per_pattern);
2005 117354 : for (i = 0; i < npatterns * nelts_per_pattern; i++)
2006 : {
2007 78682 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2008 78682 : VECTOR_CST_ELT (arg0, i + offset));
2009 78682 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2010 0 : return NULL_TREE;
2011 78682 : elts.quick_push (elt);
2012 : }
2013 :
2014 19336 : return elts.build ();
2015 19336 : }
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 3643696084 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2034 : {
2035 3643696084 : 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 2687546336 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2045 : {
2046 2687546336 : tree type = TREE_TYPE (arg0);
2047 :
2048 2687546336 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2049 : return error_mark_node;
2050 :
2051 2687546336 : 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 2687546336 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2056 : {
2057 : /* And some specific cases even faster than that. */
2058 2661103691 : if (code == PLUS_EXPR)
2059 : {
2060 1225248258 : if (integer_zerop (arg0)
2061 1225248258 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2062 : return arg1;
2063 316328146 : if (integer_zerop (arg1)
2064 316328146 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2065 : return arg0;
2066 : }
2067 1435855433 : else if (code == MINUS_EXPR)
2068 : {
2069 122147446 : if (integer_zerop (arg1)
2070 122147446 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2071 : return arg0;
2072 : }
2073 1313707987 : else if (code == MULT_EXPR)
2074 : {
2075 619208368 : if (integer_onep (arg0)
2076 619208368 : && !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 1287258854 : tree res = int_const_binop (code, arg0, arg1, -1);
2084 1287258854 : if (res != NULL_TREE)
2085 : return res;
2086 : }
2087 :
2088 26442645 : 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 38286924 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2097 : {
2098 38286924 : tree type = TREE_TYPE (arg0);
2099 38286924 : tree ctype;
2100 :
2101 38286924 : 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 38286924 : if (!TYPE_UNSIGNED (type))
2106 10285797 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2107 :
2108 28001127 : if (type == sizetype)
2109 28001127 : 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 28001127 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2119 17836 : return size_binop_loc (loc, MINUS_EXPR,
2120 : fold_convert_loc (loc, ctype, arg0),
2121 17836 : 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 27983291 : if (tree_int_cst_equal (arg0, arg1))
2128 24712135 : return build_int_cst (ctype, 0);
2129 3271156 : else if (tree_int_cst_lt (arg1, arg0))
2130 2151442 : return fold_convert_loc (loc, ctype,
2131 2151442 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2132 : else
2133 1119714 : 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 1458988317 : 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 1458988317 : tree arg1_type = TREE_TYPE (arg1);
2150 1458988317 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2151 1458988317 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2152 1458988317 : TYPE_SIGN (arg1_type)),
2153 : overflowable,
2154 1458988317 : 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 58458 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2162 : {
2163 58458 : bool overflow = false;
2164 58458 : 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 58458 : wide_int val;
2176 58458 : REAL_VALUE_TYPE r;
2177 58458 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2178 :
2179 58458 : switch (code)
2180 : {
2181 58458 : case FIX_TRUNC_EXPR:
2182 58458 : real_trunc (&r, VOIDmode, &x);
2183 58458 : 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 58458 : 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 58458 : if (! overflow)
2200 : {
2201 54820 : tree lt = TYPE_MIN_VALUE (type);
2202 54820 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2203 54820 : if (real_less (&r, &l))
2204 : {
2205 2020 : overflow = true;
2206 2020 : val = wi::to_wide (lt);
2207 : }
2208 : }
2209 :
2210 58458 : if (! overflow)
2211 : {
2212 52800 : tree ut = TYPE_MAX_VALUE (type);
2213 52800 : if (ut)
2214 : {
2215 52800 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2216 52800 : if (real_less (&u, &r))
2217 : {
2218 1924 : overflow = true;
2219 1924 : val = wi::to_wide (ut);
2220 : }
2221 : }
2222 : }
2223 :
2224 58458 : if (! overflow)
2225 50878 : 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 58458 : if (!flag_trapping_math || !overflow)
2235 51135 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2236 : else
2237 : t = NULL_TREE;
2238 :
2239 58458 : return t;
2240 58458 : }
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 2244157 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2295 : {
2296 2244157 : REAL_VALUE_TYPE value;
2297 2244157 : 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 2244157 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2303 : {
2304 99348 : t = build_real (type, TREE_REAL_CST (arg1));
2305 99348 : 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 2144809 : if (HONOR_SNANS (arg1)
2311 2146691 : && 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 2144809 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2317 2145465 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2318 509 : return NULL_TREE;
2319 :
2320 2144300 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2321 2144300 : 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 2144300 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2328 2282278 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2329 0 : TREE_OVERFLOW (t) = 1;
2330 2144300 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2331 2277958 : && !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 10718085 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2336 0 : && REAL_VALUE_ISINF (value)
2337 2144300 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2338 0 : TREE_OVERFLOW (t) = 1;
2339 : else
2340 2144300 : 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 1523614713 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2437 : {
2438 1523614713 : tree arg_type = TREE_TYPE (arg1);
2439 1523614713 : 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 1512216189 : 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 1512216189 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2453 : || TREE_CODE (type) == OFFSET_TYPE)
2454 : {
2455 1480848481 : if (TREE_CODE (arg1) == INTEGER_CST)
2456 1458988317 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2457 21860164 : else if (TREE_CODE (arg1) == REAL_CST)
2458 58458 : return fold_convert_const_int_from_real (code, type, arg1);
2459 21801706 : 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 31328717 : if (TREE_CODE (arg1) == INTEGER_CST)
2465 : {
2466 24071419 : 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 24071419 : 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 7257298 : else if (TREE_CODE (arg1) == REAL_CST)
2480 2244157 : return fold_convert_const_real_from_real (type, arg1);
2481 5013141 : 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 5163 : if (TREE_CODE (arg1) == VECTOR_CST
2496 5163 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2497 : {
2498 5163 : tree elttype = TREE_TYPE (type);
2499 5163 : 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 5163 : bool step_ok_p
2503 5163 : = (INTEGRAL_TYPE_P (elttype)
2504 369 : && INTEGRAL_TYPE_P (arg1_elttype)
2505 5468 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2506 5163 : tree_vector_builder v;
2507 5163 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2508 : return NULL_TREE;
2509 5163 : unsigned int len = v.encoded_nelts ();
2510 29990 : for (unsigned int i = 0; i < len; ++i)
2511 : {
2512 24827 : tree elt = VECTOR_CST_ELT (arg1, i);
2513 24827 : tree cvt = fold_convert_const (code, elttype, elt);
2514 24827 : if (cvt == NULL_TREE)
2515 0 : return NULL_TREE;
2516 24827 : v.quick_push (cvt);
2517 : }
2518 5163 : return v.build ();
2519 5163 : }
2520 : }
2521 12191 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2522 12191 : 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 17590 : build_zero_vector (tree type)
2530 : {
2531 17590 : tree t;
2532 :
2533 17590 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2534 17590 : 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 4408 : fold_convertible_p (const_tree type, const_tree arg)
2541 : {
2542 4408 : const_tree orig = TREE_TYPE (arg);
2543 :
2544 4408 : if (type == orig)
2545 : return true;
2546 :
2547 4408 : if (TREE_CODE (arg) == ERROR_MARK
2548 4408 : || TREE_CODE (type) == ERROR_MARK
2549 4408 : || TREE_CODE (orig) == ERROR_MARK)
2550 : return false;
2551 :
2552 4408 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2553 : return true;
2554 :
2555 4408 : switch (TREE_CODE (type))
2556 : {
2557 3840 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2558 3840 : case POINTER_TYPE: case REFERENCE_TYPE:
2559 3840 : case OFFSET_TYPE:
2560 3840 : return (INTEGRAL_TYPE_P (orig)
2561 396 : || (POINTER_TYPE_P (orig)
2562 261 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2563 3975 : || TREE_CODE (orig) == OFFSET_TYPE);
2564 :
2565 132 : case REAL_TYPE:
2566 132 : case FIXED_POINT_TYPE:
2567 132 : case VOID_TYPE:
2568 132 : 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 2252562475 : fold_convert_loc (location_t loc, tree type, tree arg)
2586 : {
2587 2252562475 : tree orig = TREE_TYPE (arg);
2588 2252562475 : tree tem;
2589 :
2590 2252562475 : if (type == orig)
2591 : return arg;
2592 :
2593 1512587063 : if (TREE_CODE (arg) == ERROR_MARK
2594 1512586021 : || TREE_CODE (type) == ERROR_MARK
2595 1512586020 : || TREE_CODE (orig) == ERROR_MARK)
2596 1043 : return error_mark_node;
2597 :
2598 1512586020 : switch (TREE_CODE (type))
2599 : {
2600 122598980 : case POINTER_TYPE:
2601 122598980 : case REFERENCE_TYPE:
2602 : /* Handle conversions between pointers to different address spaces. */
2603 122598980 : if (POINTER_TYPE_P (orig)
2604 122598980 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2605 103298663 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2606 129 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2607 : /* fall through */
2608 :
2609 1479883249 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2610 1479883249 : case OFFSET_TYPE: case BITINT_TYPE:
2611 1479883249 : if (TREE_CODE (arg) == INTEGER_CST)
2612 : {
2613 1238047467 : tem = fold_convert_const (NOP_EXPR, type, arg);
2614 1238047467 : if (tem != NULL_TREE)
2615 : return tem;
2616 : }
2617 241835782 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2618 2586 : || TREE_CODE (orig) == OFFSET_TYPE)
2619 241835782 : 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 560889 : case REAL_TYPE:
2629 560889 : if (TREE_CODE (arg) == INTEGER_CST)
2630 : {
2631 58240 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2632 58240 : if (tem != NULL_TREE)
2633 : return tem;
2634 : }
2635 502649 : else if (TREE_CODE (arg) == REAL_CST)
2636 : {
2637 122064 : tem = fold_convert_const (NOP_EXPR, type, arg);
2638 122064 : if (tem != NULL_TREE)
2639 : return tem;
2640 : }
2641 380585 : 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 380587 : switch (TREE_CODE (orig))
2649 : {
2650 753 : case INTEGER_TYPE: case BITINT_TYPE:
2651 753 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2652 753 : case POINTER_TYPE: case REFERENCE_TYPE:
2653 753 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2654 :
2655 379834 : case REAL_TYPE:
2656 379834 : 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 2281 : case COMPLEX_TYPE:
2697 2281 : switch (TREE_CODE (orig))
2698 : {
2699 596 : case INTEGER_TYPE: case BITINT_TYPE:
2700 596 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2701 596 : case POINTER_TYPE: case REFERENCE_TYPE:
2702 596 : case REAL_TYPE:
2703 596 : case FIXED_POINT_TYPE:
2704 1192 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2705 596 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2706 596 : fold_convert_loc (loc, TREE_TYPE (type),
2707 596 : 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 32040786 : case VECTOR_TYPE:
2734 32040786 : if (integer_zerop (arg))
2735 17590 : return build_zero_vector (type);
2736 32023196 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2737 32023196 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2738 : || VECTOR_TYPE_P (orig));
2739 32023196 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2740 :
2741 94835 : case VOID_TYPE:
2742 94835 : tem = fold_ignored_result (arg);
2743 94835 : 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 3834 : default:
2750 3834 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2751 3834 : 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 53723911 : maybe_lvalue_p (const_tree x)
2764 : {
2765 : /* We only need to wrap lvalue tree codes. */
2766 53723911 : 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 39453749 : default:
2800 : /* Assume the worst for front-end tree codes. */
2801 39453749 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2802 : break;
2803 : return false;
2804 : }
2805 :
2806 14305661 : return true;
2807 : }
2808 :
2809 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2810 :
2811 : tree
2812 44406585 : 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 44406585 : if (in_gimple_form)
2817 : return x;
2818 :
2819 10854788 : if (! maybe_lvalue_p (x))
2820 : return x;
2821 2582864 : 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 135082214 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2831 : {
2832 135082214 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2833 1072711 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2834 : return ERROR_MARK;
2835 :
2836 134261052 : switch (code)
2837 : {
2838 : case EQ_EXPR:
2839 : return NE_EXPR;
2840 59292812 : case NE_EXPR:
2841 59292812 : return EQ_EXPR;
2842 12526004 : case GT_EXPR:
2843 12526004 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2844 18745907 : case GE_EXPR:
2845 18745907 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2846 8480129 : case LT_EXPR:
2847 8480129 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2848 8444296 : case LE_EXPR:
2849 8444296 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2850 285 : case LTGT_EXPR:
2851 285 : return UNEQ_EXPR;
2852 306 : case UNEQ_EXPR:
2853 306 : 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 223396 : case ORDERED_EXPR:
2863 223396 : return UNORDERED_EXPR;
2864 57019 : case UNORDERED_EXPR:
2865 57019 : 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 163327142 : swap_tree_comparison (enum tree_code code)
2876 : {
2877 163327142 : 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 39778884 : case GT_EXPR:
2887 39778884 : return LT_EXPR;
2888 11350071 : case GE_EXPR:
2889 11350071 : return LE_EXPR;
2890 22410903 : case LT_EXPR:
2891 22410903 : return GT_EXPR;
2892 17509899 : case LE_EXPR:
2893 17509899 : return GE_EXPR;
2894 252582 : case UNGT_EXPR:
2895 252582 : return UNLT_EXPR;
2896 20843 : case UNGE_EXPR:
2897 20843 : return UNLE_EXPR;
2898 375056 : case UNLT_EXPR:
2899 375056 : return UNGT_EXPR;
2900 111879 : case UNLE_EXPR:
2901 111879 : 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 92058 : comparison_to_compcode (enum tree_code code)
2914 : {
2915 92058 : 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 18888 : compcode_to_comparison (enum comparison_code code)
2956 : {
2957 18888 : 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 1783271 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2996 : {
2997 1783271 : return (COMPARISON_CLASS_P (cond1)
2998 1688166 : && COMPARISON_CLASS_P (cond2)
2999 1676854 : && (invert_tree_comparison
3000 1676854 : (TREE_CODE (cond1),
3001 3353708 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3002 68971 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3003 68971 : TREE_OPERAND (cond2, 0), 0)
3004 1804516 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3005 21245 : 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 46029 : 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 46029 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3022 46029 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3023 46029 : int compcode;
3024 46029 : *res = NULL_TREE;
3025 46029 : if (TREE_CODE (inner_truth_type) == VECTOR_TYPE)
3026 199 : inner_truth_type = TREE_TYPE (inner_truth_type);
3027 46029 : bool signed_bool = false;
3028 46029 : if (TYPE_SIGN (inner_truth_type) == SIGNED
3029 46029 : && (TREE_CODE (inner_truth_type) == BOOLEAN_TYPE
3030 2148 : || TYPE_PRECISION (inner_truth_type) == 1))
3031 : signed_bool = true;
3032 :
3033 46029 : switch (code)
3034 : {
3035 31678 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3036 31678 : case BIT_AND_EXPR:
3037 31678 : compcode = lcompcode & rcompcode;
3038 31678 : break;
3039 :
3040 12906 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3041 12906 : case BIT_IOR_EXPR:
3042 12906 : compcode = lcompcode | rcompcode;
3043 12906 : break;
3044 :
3045 1073 : case BIT_XOR_EXPR:
3046 1073 : case NE_EXPR:
3047 1073 : compcode = lcompcode ^ rcompcode;
3048 1073 : 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 27 : case LT_EXPR:
3057 27 : if (signed_bool)
3058 1 : compcode = lcompcode & ~rcompcode;
3059 : else
3060 26 : compcode = ~lcompcode & rcompcode;
3061 : break;
3062 :
3063 : //`bool0 > bool1` is `bool0 & !bool1` for unsigned
3064 : //`bool0 > bool1` is `!bool0 & bool1` for signed
3065 4 : case GT_EXPR:
3066 4 : if (signed_bool)
3067 0 : compcode = ~lcompcode & rcompcode;
3068 : else
3069 4 : compcode = lcompcode & ~rcompcode;
3070 : break;
3071 :
3072 : // `bool0 <= bool1` as !bool0 | bool1 for unsigned
3073 : // `bool0 <= bool1` as bool0 | !bool1 for signed
3074 4 : case LE_EXPR:
3075 4 : if (signed_bool)
3076 3 : compcode = lcompcode | ~rcompcode;
3077 : else
3078 1 : compcode = ~lcompcode | rcompcode;
3079 : break;
3080 :
3081 : //`bool0 >= bool1` is `bool0 | !bool1` for unsigned
3082 : //`bool0 >= bool1` is `bool0 | !bool1` for signed
3083 1 : case GE_EXPR:
3084 1 : if (signed_bool)
3085 0 : compcode = ~lcompcode | rcompcode;
3086 : else
3087 1 : compcode = lcompcode | ~rcompcode;
3088 : break;
3089 :
3090 : default:
3091 : return ERROR_MARK;
3092 : }
3093 :
3094 46029 : 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 27992 : compcode &= ~COMPCODE_UNORD;
3099 27992 : if (compcode == COMPCODE_LTGT)
3100 : compcode = COMPCODE_NE;
3101 26918 : else if (compcode == COMPCODE_ORD)
3102 1355 : compcode = COMPCODE_TRUE;
3103 : }
3104 18037 : 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 30396 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3149 : {
3150 11508 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3151 11508 : return INTEGER_CST;
3152 : }
3153 : else
3154 18888 : 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 28062 : 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 28062 : bool honor_nans = HONOR_NANS (ll_arg);
3171 28062 : tree_code rescode;
3172 28062 : tree res;
3173 28062 : rescode = combine_comparisons (code, lcode, rcode,
3174 : truth_type, inner_truth_type,
3175 : honor_nans, &res);
3176 28062 : if (rescode == ERROR_MARK)
3177 : return NULL_TREE;
3178 25693 : if (rescode == INTEGER_CST)
3179 11023 : return res;
3180 :
3181 14670 : 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 7786786290 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3242 : unsigned int flags)
3243 : {
3244 7786786290 : 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 7787503103 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3254 : tree type1, const_tree arg1,
3255 : unsigned int flags)
3256 : {
3257 7787503103 : bool r;
3258 7787503103 : if (verify_hash_value (arg0, arg1, flags, &r))
3259 3306686731 : return r;
3260 :
3261 4480816372 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3262 4480816372 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3263 :
3264 : /* If either is ERROR_MARK, they aren't equal. */
3265 4480816372 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3266 4480815670 : || type0 == error_mark_node
3267 4480815668 : || 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 4480815667 : if (!type0 || !type1)
3273 : return false;
3274 :
3275 : /* Bitwise identity makes no sense if the values have different layouts. */
3276 4480810513 : if ((flags & OEP_BITWISE)
3277 4480810513 : && !tree_nop_conversion_p (type0, type1))
3278 : return false;
3279 :
3280 : /* We cannot consider pointers to different address space equal. */
3281 4480810513 : if (POINTER_TYPE_P (type0)
3282 665304674 : && POINTER_TYPE_P (type1)
3283 5051047438 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3284 570236925 : != 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 4480810250 : 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 699869782 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3294 699869782 : return tree_int_cst_equal (arg0, arg1);
3295 : }
3296 :
3297 3780940468 : if ((flags & OEP_ASSUME_WRAPV)
3298 2036298 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3299 : {
3300 769870 : const_tree t_arg0 = arg0;
3301 769870 : const_tree t_arg1 = arg1;
3302 769870 : STRIP_NOPS (arg0);
3303 769870 : STRIP_NOPS (arg1);
3304 : /* Only recurse if the conversion was one that was valid to strip. */
3305 769870 : if (t_arg0 != arg0 || t_arg1 != arg1)
3306 716813 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3307 : }
3308 :
3309 3780223655 : 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 3362866814 : bool enforce_signedness = true;
3315 3362866814 : if (flags & OEP_ASSUME_WRAPV)
3316 : {
3317 1220235 : 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 3362866814 : 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 3362866814 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3347 3362866814 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3348 151025021 : && 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 3053342028 : if (element_precision (type0) != element_precision (type1))
3354 : return false;
3355 :
3356 2889303088 : STRIP_NOPS (arg0);
3357 2889303088 : STRIP_NOPS (arg1);
3358 :
3359 2889303088 : type0 = TREE_TYPE (arg0);
3360 2889303088 : 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 3306659929 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3377 1341297075 : && COMPARISON_CLASS_P (arg0)
3378 6927692 : && COMPARISON_CLASS_P (arg1))
3379 : {
3380 5178874 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3381 :
3382 5178874 : if (TREE_CODE (arg0) == swap_code)
3383 2213921 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3384 2213921 : TREE_OPERAND (arg1, 1), flags)
3385 2233671 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3386 19750 : TREE_OPERAND (arg1, 0), flags);
3387 : }
3388 :
3389 3304446008 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3390 : {
3391 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3392 1339083154 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3393 : ;
3394 1339019645 : 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 50177509 : if (TREE_CODE (arg0) == MEM_REF
3400 8170789 : && DECL_P (arg1)
3401 5762728 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3402 1361794 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3403 50907861 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3404 : return true;
3405 49952587 : else if (TREE_CODE (arg1) == MEM_REF
3406 31177806 : && DECL_P (arg0)
3407 11273673 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3408 2371772 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3409 50553799 : && 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 1965426363 : if (TREE_CODE (type0) == ERROR_MARK
3420 1965426363 : || TREE_CODE (type1) == ERROR_MARK
3421 3930852726 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3422 26753120 : && !(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 374787754 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3435 2336198981 : && (TREE_CODE (arg0) == SAVE_EXPR
3436 374762089 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3437 334536194 : || (! 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 1627512127 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3443 28492990 : 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 3787456 : case REAL_CST:
3453 3787456 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3454 : return true;
3455 :
3456 2755531 : 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 15332 : if (real_zerop (arg0) && real_zerop (arg1))
3461 : return true;
3462 : }
3463 : return false;
3464 :
3465 833982 : case VECTOR_CST:
3466 833982 : {
3467 833982 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3468 833982 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3469 : return false;
3470 :
3471 811631 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3472 811631 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3473 : return false;
3474 :
3475 774760 : unsigned int count = vector_cst_encoded_nelts (arg0);
3476 1720610 : for (unsigned int i = 0; i < count; ++i)
3477 1786558 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3478 893279 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3479 : return false;
3480 : return true;
3481 : }
3482 :
3483 17911 : case COMPLEX_CST:
3484 17911 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3485 : flags)
3486 17911 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3487 : flags));
3488 :
3489 1178699 : case STRING_CST:
3490 1178699 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3491 1178699 : && ! memcmp (TREE_STRING_POINTER (arg0),
3492 678095 : TREE_STRING_POINTER (arg1),
3493 678095 : 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 20524467 : case ADDR_EXPR:
3502 20524467 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3503 20524467 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3504 : flags | OEP_ADDRESS_OF
3505 20524467 : | OEP_MATCH_SIDE_EFFECTS);
3506 184272 : case CONSTRUCTOR:
3507 184272 : {
3508 : /* In GIMPLE empty constructors are allowed in initializers of
3509 : aggregates. */
3510 184272 : 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 370692405 : 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 1600985189 : 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 1600985189 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3584 : {
3585 7862599 : case tcc_unary:
3586 : /* Two conversions are equal only if signedness and modes match. */
3587 7862599 : switch (TREE_CODE (arg0))
3588 : {
3589 7494653 : CASE_CONVERT:
3590 7494653 : case FIX_TRUNC_EXPR:
3591 7494653 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3592 : return false;
3593 : break;
3594 : default:
3595 : break;
3596 : }
3597 :
3598 7862578 : return OP_SAME_WITH_NULL (0);
3599 :
3600 :
3601 23763652 : case tcc_comparison:
3602 23763652 : case tcc_binary:
3603 23763652 : if (OP_SAME (0) && OP_SAME (1))
3604 : return true;
3605 :
3606 : /* For commutative ops, allow the other order. */
3607 17634007 : return (commutative_tree_code (TREE_CODE (arg0))
3608 13480348 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3609 13480348 : TREE_OPERAND (arg1, 1), flags)
3610 17869039 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3611 235032 : TREE_OPERAND (arg1, 0), flags));
3612 :
3613 887329707 : 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 887329707 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3618 887329707 : && (TREE_SIDE_EFFECTS (arg0)
3619 816152787 : || TREE_SIDE_EFFECTS (arg1)))
3620 : return false;
3621 :
3622 886707404 : switch (TREE_CODE (arg0))
3623 : {
3624 5054570 : case INDIRECT_REF:
3625 5054570 : if (!(flags & OEP_ADDRESS_OF))
3626 : {
3627 5032131 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3628 : return false;
3629 : /* Verify that the access types are compatible. */
3630 5025528 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3631 : return false;
3632 : }
3633 4980720 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3634 4980720 : return OP_SAME (0);
3635 :
3636 576150 : case IMAGPART_EXPR:
3637 : /* Require the same offset. */
3638 576150 : if (!operand_equal_p (TYPE_SIZE (type0),
3639 576150 : TYPE_SIZE (type1),
3640 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3641 : return false;
3642 :
3643 : /* Fallthru. */
3644 2313342 : case REALPART_EXPR:
3645 2313342 : case VIEW_CONVERT_EXPR:
3646 2313342 : return OP_SAME (0);
3647 :
3648 88261006 : case TARGET_MEM_REF:
3649 88261006 : case MEM_REF:
3650 88261006 : if (!(flags & OEP_ADDRESS_OF))
3651 : {
3652 : /* Require equal access sizes */
3653 17423300 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3654 17423300 : && (!TYPE_SIZE (type0)
3655 1305373 : || !TYPE_SIZE (type1)
3656 1298548 : || !operand_equal_p (TYPE_SIZE (type0),
3657 1298548 : TYPE_SIZE (type1),
3658 : flags)))
3659 : return false;
3660 : /* Verify that access happens in similar types. */
3661 16112823 : if (!types_compatible_p (type0, type1))
3662 : return false;
3663 : /* Verify that accesses are TBAA compatible. */
3664 15767123 : if (!alias_ptr_types_compatible_p
3665 15767123 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3666 15767123 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3667 14873960 : || (MR_DEPENDENCE_CLIQUE (arg0)
3668 14873960 : != MR_DEPENDENCE_CLIQUE (arg1))
3669 28883409 : || (MR_DEPENDENCE_BASE (arg0)
3670 13116286 : != MR_DEPENDENCE_BASE (arg1)))
3671 : return false;
3672 : /* Verify that alignment is compatible. */
3673 12561149 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3674 : return false;
3675 : }
3676 83178243 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3677 142190267 : return (OP_SAME (0) && OP_SAME (1)
3678 : /* TARGET_MEM_REF require equal extra operands. */
3679 108645876 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3680 538577 : || (OP_SAME_WITH_NULL (2)
3681 277792 : && OP_SAME_WITH_NULL (3)
3682 272764 : && OP_SAME_WITH_NULL (4))));
3683 :
3684 37181335 : case ARRAY_REF:
3685 37181335 : case ARRAY_RANGE_REF:
3686 37181335 : if (!OP_SAME (0))
3687 : return false;
3688 31625788 : 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 31625788 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3692 31625788 : TREE_OPERAND (arg1, 1))
3693 28479795 : || OP_SAME (1))
3694 6324910 : && OP_SAME_WITH_NULL (2)
3695 6323590 : && 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 41111833 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3699 3161795 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3700 2426 : || (operand_equal_p (array_ref_low_bound
3701 2426 : (const_cast<tree> (arg0)),
3702 : array_ref_low_bound
3703 2426 : (const_cast<tree> (arg1)),
3704 : flags)
3705 2426 : && operand_equal_p (array_ref_element_size
3706 2426 : (const_cast<tree> (arg0)),
3707 : array_ref_element_size
3708 2426 : (const_cast<tree> (arg1)),
3709 : flags))));
3710 :
3711 753256846 : 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 753256846 : if (!OP_SAME_WITH_NULL (0))
3715 : return false;
3716 62376204 : {
3717 62376204 : 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 62376204 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3723 62376204 : if (!OP_SAME (1))
3724 : {
3725 36628911 : if (compare_address
3726 648436 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3727 : {
3728 646387 : tree field0 = TREE_OPERAND (arg0, 1);
3729 646387 : tree field1 = TREE_OPERAND (arg1, 1);
3730 :
3731 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3732 646387 : if (TREE_CODE (field0) != FIELD_DECL
3733 646387 : || TREE_CODE (field1) != FIELD_DECL)
3734 : return false;
3735 :
3736 646387 : if (!DECL_FIELD_OFFSET (field0)
3737 646387 : || !DECL_FIELD_OFFSET (field1))
3738 3 : return field0 == field1;
3739 :
3740 646384 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3741 646384 : DECL_FIELD_OFFSET (field1), flags)
3742 839632 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3743 193248 : DECL_FIELD_BIT_OFFSET (field1),
3744 : flags))
3745 : return false;
3746 : }
3747 : else
3748 : return false;
3749 : }
3750 : }
3751 25782161 : return OP_SAME_WITH_NULL (2);
3752 :
3753 640113 : case BIT_FIELD_REF:
3754 640113 : if (!OP_SAME (0))
3755 : return false;
3756 378955 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3757 378955 : return OP_SAME (1) && OP_SAME (2);
3758 :
3759 : default:
3760 : return false;
3761 : }
3762 :
3763 61202839 : case tcc_expression:
3764 61202839 : switch (TREE_CODE (arg0))
3765 : {
3766 55458408 : case ADDR_EXPR:
3767 : /* Be sure we pass right ADDRESS_OF flag. */
3768 55458408 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3769 55458408 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3770 55458408 : TREE_OPERAND (arg1, 0),
3771 55458408 : flags | OEP_ADDRESS_OF);
3772 :
3773 611238 : case TRUTH_NOT_EXPR:
3774 611238 : return OP_SAME (0);
3775 :
3776 79505 : case TRUTH_ANDIF_EXPR:
3777 79505 : case TRUTH_ORIF_EXPR:
3778 79505 : 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 47680 : case TRUTH_AND_EXPR:
3788 47680 : case TRUTH_OR_EXPR:
3789 47680 : case TRUTH_XOR_EXPR:
3790 47680 : if (OP_SAME (0) && OP_SAME (1))
3791 : return true;
3792 :
3793 : /* Otherwise take into account this is a commutative operation. */
3794 47662 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3795 47662 : TREE_OPERAND (arg1, 1), flags)
3796 47665 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3797 3 : TREE_OPERAND (arg1, 0), flags));
3798 :
3799 216240 : case COND_EXPR:
3800 216240 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3801 : return false;
3802 170314 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3803 170314 : 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 38569 : case MODIFY_EXPR:
3820 38569 : case INIT_EXPR:
3821 38569 : case COMPOUND_EXPR:
3822 38569 : case PREDECREMENT_EXPR:
3823 38569 : case PREINCREMENT_EXPR:
3824 38569 : case POSTDECREMENT_EXPR:
3825 38569 : case POSTINCREMENT_EXPR:
3826 38569 : if (flags & OEP_LEXICOGRAPHIC)
3827 165 : return OP_SAME (0) && OP_SAME (1);
3828 : return false;
3829 :
3830 445736 : case CLEANUP_POINT_EXPR:
3831 445736 : case EXPR_STMT:
3832 445736 : case SAVE_EXPR:
3833 445736 : if (flags & OEP_LEXICOGRAPHIC)
3834 208 : return OP_SAME (0);
3835 : return false;
3836 :
3837 80868 : case OBJ_TYPE_REF:
3838 : /* Virtual table reference. */
3839 161736 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3840 80868 : OBJ_TYPE_REF_EXPR (arg1), flags))
3841 : return false;
3842 15966 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3843 15966 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3844 15966 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3845 : return false;
3846 15966 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3847 15966 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3848 : return false;
3849 15966 : if (virtual_method_call_p (arg0))
3850 : {
3851 15966 : if (!virtual_method_call_p (arg1))
3852 : return false;
3853 15966 : return types_same_for_odr (obj_type_ref_class (arg0),
3854 31932 : 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 3762742 : case tcc_vl_exp:
3866 3762742 : switch (TREE_CODE (arg0))
3867 : {
3868 3762742 : case CALL_EXPR:
3869 3762742 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3870 3762742 : != (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 3762414 : 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 24 : 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 3762390 : 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 2196159 : {
3892 2196159 : unsigned int cef = call_expr_flags (arg0);
3893 2196159 : if (flags & OEP_PURE_SAME)
3894 0 : cef &= ECF_CONST | ECF_PURE;
3895 : else
3896 2196159 : cef &= ECF_CONST;
3897 2196159 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3898 : return false;
3899 : }
3900 :
3901 : /* Now see if all the arguments are the same. */
3902 35328 : {
3903 35328 : const_call_expr_arg_iterator iter0, iter1;
3904 35328 : const_tree a0, a1;
3905 70656 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3906 35328 : a1 = first_const_call_expr_arg (arg1, &iter1);
3907 43468 : a0 && a1;
3908 8140 : a0 = next_const_call_expr_arg (&iter0),
3909 8140 : a1 = next_const_call_expr_arg (&iter1))
3910 36834 : 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 222882349 : case tcc_declaration:
3922 : /* Consider __builtin_sqrt equal to sqrt. */
3923 222882349 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3924 7112310 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3925 273186 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3926 6490405 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3927 273186 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3928 :
3929 216391944 : if (DECL_P (arg0)
3930 216391944 : && (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 391561721 : case tcc_exceptional:
3945 391561721 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3946 : {
3947 19675 : 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 19675 : 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 976 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3963 1952 : TYPE_VECTOR_SUBPARTS (type1)))
3964 : return false;
3965 :
3966 976 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3967 976 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3968 976 : unsigned int len = vec_safe_length (v0);
3969 :
3970 1952 : if (len != vec_safe_length (v1))
3971 : return false;
3972 :
3973 4360 : for (unsigned int i = 0; i < len; i++)
3974 : {
3975 3789 : constructor_elt *c0 = &(*v0)[i];
3976 3789 : constructor_elt *c1 = &(*v1)[i];
3977 :
3978 3789 : 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 3414 : || (c0->index
3983 2714 : && (TREE_CODE (c0->index) != INTEGER_CST
3984 2714 : || compare_tree_int (c0->index, i)))
3985 7203 : || (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 391542046 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3993 3353 : && (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 2619382 : case tcc_statement:
4016 2619382 : 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 2986088608 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
4043 : unsigned int flags)
4044 : {
4045 2986088608 : int i;
4046 2986088608 : enum tree_code code;
4047 2986088608 : enum tree_code_class tclass;
4048 :
4049 2986088608 : if (t == NULL_TREE || t == error_mark_node)
4050 : {
4051 79505927 : hstate.merge_hash (0);
4052 79505927 : return;
4053 : }
4054 :
4055 2906582681 : STRIP_ANY_LOCATION_WRAPPER (t);
4056 :
4057 2906582681 : if (!(flags & OEP_ADDRESS_OF))
4058 2648791826 : STRIP_NOPS (t);
4059 :
4060 2906582681 : code = TREE_CODE (t);
4061 :
4062 2906582681 : 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 872099777 : case INTEGER_CST:
4070 872099777 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4071 1762250131 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4072 890150354 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4073 : return;
4074 14813694 : case REAL_CST:
4075 14813694 : {
4076 14813694 : unsigned int val2;
4077 14813694 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4078 : val2 = rvc_zero;
4079 : else
4080 14593953 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4081 14813694 : hstate.merge_hash (val2);
4082 14813694 : 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 12720559 : case STRING_CST:
4091 12720559 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4092 12720559 : TREE_STRING_LENGTH (t));
4093 12720559 : 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 212392 : case COMPLEX_CST:
4099 212392 : hash_operand (TREE_REALPART (t), hstate, flags);
4100 212392 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4101 212392 : return;
4102 3134095 : case VECTOR_CST:
4103 3134095 : {
4104 3134095 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4105 3134095 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4106 3134095 : unsigned int count = vector_cst_encoded_nelts (t);
4107 13319119 : for (unsigned int i = 0; i < count; ++i)
4108 7050929 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4109 : return;
4110 : }
4111 869547100 : case SSA_NAME:
4112 : /* We can just compare by pointer. */
4113 869547100 : hstate.add_hwi (SSA_NAME_VERSION (t));
4114 869547100 : 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 260942 : for (; t; t = TREE_CHAIN (t))
4128 130471 : hash_operand (TREE_VALUE (t), hstate, flags);
4129 : return;
4130 4996701 : case CONSTRUCTOR:
4131 4996701 : {
4132 4996701 : unsigned HOST_WIDE_INT idx;
4133 4996701 : tree field, value;
4134 4996701 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4135 4996701 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4136 20016022 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4137 : {
4138 : /* In GIMPLE the indexes can be either NULL or matching i. */
4139 15019321 : if (field == NULL_TREE)
4140 1131233 : field = bitsize_int (idx);
4141 15019321 : if (TREE_CODE (field) == FIELD_DECL)
4142 : {
4143 9981223 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4144 9981223 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4145 : }
4146 : else
4147 5038098 : hash_operand (field, hstate, flags);
4148 15019321 : 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 21291754 : 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 21291754 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4174 21291754 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4175 : {
4176 7193581 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4177 7193581 : code = TREE_CODE (t);
4178 : }
4179 : /* FALL THROUGH */
4180 1128926562 : default:
4181 1128926562 : 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 1128926562 : tclass = TREE_CODE_CLASS (code);
4188 :
4189 1128926562 : if (tclass == tcc_declaration)
4190 : {
4191 : /* DECL's have a unique ID */
4192 786476692 : hstate.add_hwi (DECL_UID (t));
4193 : }
4194 342449870 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4195 : {
4196 : /* For comparisons that can be swapped, use the lower
4197 : tree code. */
4198 154258 : enum tree_code ccode = swap_tree_comparison (code);
4199 154258 : if (code < ccode)
4200 69403 : ccode = code;
4201 154258 : hstate.add_object (ccode);
4202 154258 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4203 154258 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4204 : }
4205 342295612 : else if (CONVERT_EXPR_CODE_P (code))
4206 : {
4207 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4208 : operand_equal_p. */
4209 6899305 : enum tree_code ccode = NOP_EXPR;
4210 6899305 : 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 6899305 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4217 6899305 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4218 : }
4219 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4220 335396307 : else if (code == MEM_REF
4221 80827307 : && (flags & OEP_ADDRESS_OF) != 0
4222 71258309 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4223 14192134 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4224 349380212 : && integer_zerop (TREE_OPERAND (t, 1)))
4225 6503906 : 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 328892401 : else if (!IS_EXPR_CODE_CLASS (tclass))
4230 384 : gcc_assert (flags & OEP_HASH_CHECK);
4231 : else
4232 : {
4233 328892017 : unsigned int sflags = flags;
4234 :
4235 328892017 : hstate.add_object (code);
4236 :
4237 328892017 : switch (code)
4238 : {
4239 131273281 : case ADDR_EXPR:
4240 131273281 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4241 131273281 : flags |= OEP_ADDRESS_OF;
4242 131273281 : sflags = flags;
4243 131273281 : break;
4244 :
4245 79292186 : case INDIRECT_REF:
4246 79292186 : case MEM_REF:
4247 79292186 : case TARGET_MEM_REF:
4248 79292186 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4249 79292186 : sflags = flags;
4250 79292186 : break;
4251 :
4252 77805742 : case COMPONENT_REF:
4253 77805742 : if (sflags & OEP_ADDRESS_OF)
4254 : {
4255 37992706 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4256 37992706 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4257 : hstate, flags & ~OEP_ADDRESS_OF);
4258 37992706 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4259 : hstate, flags & ~OEP_ADDRESS_OF);
4260 37992706 : return;
4261 : }
4262 : break;
4263 15999280 : case ARRAY_REF:
4264 15999280 : case ARRAY_RANGE_REF:
4265 15999280 : case BIT_FIELD_REF:
4266 15999280 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4267 15999280 : break;
4268 :
4269 8822 : case COND_EXPR:
4270 8822 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4271 8822 : 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 45620 : case CALL_EXPR:
4286 45620 : 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 290167 : case OBJ_TYPE_REF:
4298 : /* Virtual table reference. */
4299 290167 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4300 290167 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4301 290167 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4302 290167 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4303 290167 : if (!virtual_method_call_p (t))
4304 : return;
4305 290146 : if (tree c = obj_type_ref_class (t))
4306 : {
4307 290146 : 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 290146 : if (TREE_CODE (c) == TYPE_DECL
4311 290146 : && 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 290609072 : 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 290609072 : 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 17788398 : inchash::hash one, two;
4338 17788398 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4339 17788398 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4340 17788398 : hstate.add_commutative (one, two);
4341 : }
4342 : else
4343 764501004 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4344 710540052 : hash_operand (TREE_OPERAND (t, i), hstate,
4345 : i == 0 ? flags : sflags);
4346 : }
4347 : return;
4348 : }
4349 : }
4350 :
4351 : bool
4352 7791906944 : 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 7791906944 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4359 : {
4360 3308590937 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4361 : {
4362 475984897 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4363 : {
4364 87215682 : inchash::hash hstate0 (0), hstate1 (0);
4365 87215682 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4366 87215682 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4367 87215682 : hashval_t h0 = hstate0.end ();
4368 87215682 : hashval_t h1 = hstate1.end ();
4369 87215682 : gcc_assert (h0 == h1);
4370 : }
4371 475984897 : *ret = true;
4372 : }
4373 : else
4374 2832606040 : *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 3306696312 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4390 : {
4391 3306696312 : 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 2105464985 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4404 : {
4405 2105464985 : default_compare_instance.hash_operand (t, hstate, flags);
4406 2105464985 : }
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 20608711 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4415 : {
4416 20608711 : if (operand_equal_p (arg0, arg1, 0))
4417 : return true;
4418 :
4419 39408012 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4420 33665830 : || ! 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 6345464 : tree op0 = arg0;
4427 6345464 : tree op1 = arg1;
4428 6345464 : STRIP_NOPS (op0);
4429 6345464 : STRIP_NOPS (op1);
4430 6345464 : 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 5255625 : if (CONVERT_EXPR_P (arg1)
4436 900169 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4437 900115 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4438 900115 : < TYPE_PRECISION (TREE_TYPE (arg1))
4439 6463212 : && 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 60944144 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4456 : {
4457 65015828 : enum tree_code code = TREE_CODE (arg);
4458 65015828 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4459 :
4460 : /* We can handle some of the tcc_expression cases here. */
4461 65015828 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4462 : tclass = tcc_unary;
4463 64398406 : else if (tclass == tcc_expression
4464 696511 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4465 696511 : || code == COMPOUND_EXPR))
4466 : tclass = tcc_binary;
4467 :
4468 64387467 : switch (tclass)
4469 : {
4470 4071684 : case tcc_unary:
4471 4071684 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4472 :
4473 5406800 : case tcc_binary:
4474 5406800 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4475 5406800 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4476 :
4477 : case tcc_constant:
4478 : return true;
4479 :
4480 685572 : case tcc_expression:
4481 685572 : if (code == COND_EXPR)
4482 700 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4483 700 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4484 764 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4485 : return false;
4486 :
4487 622234 : 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 622234 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4495 622234 : TREE_OPERAND (arg, 1), 0))
4496 : return false;
4497 :
4498 622234 : if (*cval1 == 0)
4499 620022 : *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 620141 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4510 : ;
4511 620141 : else if (*cval2 == 0)
4512 620022 : *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 311682 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4619 : {
4620 311682 : 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 311682 : 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 311682 : if (TREE_SIDE_EFFECTS (omitted))
4629 11942 : return build2_loc (loc, COMPOUND_EXPR, type,
4630 11942 : fold_ignored_result (omitted), t);
4631 :
4632 299740 : 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 6157 : omit_two_operands_loc (location_t loc, tree type, tree result,
4646 : tree omitted1, tree omitted2)
4647 : {
4648 6157 : tree t = fold_convert_loc (loc, type, result);
4649 :
4650 6157 : if (TREE_SIDE_EFFECTS (omitted2))
4651 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4652 6157 : if (TREE_SIDE_EFFECTS (omitted1))
4653 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4654 :
4655 6157 : 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 51281910 : fold_truth_not_expr (location_t loc, tree arg)
4668 : {
4669 51281910 : tree type = TREE_TYPE (arg);
4670 51281910 : enum tree_code code = TREE_CODE (arg);
4671 51281910 : 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 51281910 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4678 : {
4679 39218095 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4680 32553089 : if (FLOAT_TYPE_P (op_type)
4681 6675321 : && flag_trapping_math
4682 6644211 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4683 45822866 : && code != NE_EXPR && code != EQ_EXPR)
4684 : return NULL_TREE;
4685 :
4686 33310528 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4687 33310528 : if (code == ERROR_MARK)
4688 : return NULL_TREE;
4689 :
4690 33310528 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4691 33310528 : TREE_OPERAND (arg, 1));
4692 33310528 : copy_warning (ret, arg);
4693 33310528 : return ret;
4694 : }
4695 :
4696 12063815 : switch (code)
4697 : {
4698 0 : case INTEGER_CST:
4699 0 : return constant_boolean_node (integer_zerop (arg), type);
4700 :
4701 42815 : case TRUTH_AND_EXPR:
4702 42815 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4703 42815 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4704 85630 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4705 42815 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4706 85630 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4707 :
4708 2665 : case TRUTH_OR_EXPR:
4709 2665 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4710 2665 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4711 5330 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4712 2665 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4713 5330 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4714 :
4715 72918 : 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 72918 : 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 72730 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4726 72730 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4727 145460 : TREE_OPERAND (arg, 1));
4728 :
4729 394454 : case TRUTH_ANDIF_EXPR:
4730 394454 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4731 394454 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4732 788908 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4733 394454 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4734 788908 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4735 :
4736 16952 : case TRUTH_ORIF_EXPR:
4737 16952 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4738 16952 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4739 33904 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4740 16952 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4741 33904 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4742 :
4743 796965 : case TRUTH_NOT_EXPR:
4744 796965 : return TREE_OPERAND (arg, 0);
4745 :
4746 9762 : case COND_EXPR:
4747 9762 : {
4748 9762 : tree arg1 = TREE_OPERAND (arg, 1);
4749 9762 : tree arg2 = TREE_OPERAND (arg, 2);
4750 :
4751 9762 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4752 9762 : 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 9762 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4758 9762 : VOID_TYPE_P (TREE_TYPE (arg1))
4759 9762 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4760 9762 : VOID_TYPE_P (TREE_TYPE (arg2))
4761 19521 : ? 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 81412 : CASE_CONVERT:
4775 81412 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4776 81348 : 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 491 : case BIT_AND_EXPR:
4786 491 : 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 435 : case CLEANUP_POINT_EXPR:
4794 435 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4795 435 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4796 870 : 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 2131514 : fold_invert_truthvalue (location_t loc, tree arg)
4810 : {
4811 2131514 : tree type = TREE_TYPE (arg);
4812 4263004 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4813 : ? BIT_NOT_EXPR
4814 : : TRUTH_NOT_EXPR,
4815 2131514 : 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 43597885 : invert_truthvalue_loc (location_t loc, tree arg)
4824 : {
4825 43597885 : if (TREE_CODE (arg) == ERROR_MARK)
4826 : return arg;
4827 :
4828 43597885 : tree type = TREE_TYPE (arg);
4829 87195770 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4830 : ? BIT_NOT_EXPR
4831 : : TRUTH_NOT_EXPR,
4832 43597885 : 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 837272 : 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 837272 : tree result, bftype;
4847 :
4848 : /* Attempt not to lose the access path if possible. */
4849 837272 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4850 : {
4851 833251 : tree ninner = TREE_OPERAND (orig_inner, 0);
4852 833251 : machine_mode nmode;
4853 833251 : poly_int64 nbitsize, nbitpos;
4854 833251 : tree noffset;
4855 833251 : int nunsignedp, nreversep, nvolatilep = 0;
4856 833251 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4857 : &noffset, &nmode, &nunsignedp,
4858 : &nreversep, &nvolatilep);
4859 833251 : if (base == inner
4860 833124 : && noffset == NULL_TREE
4861 833124 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4862 833114 : && !reversep
4863 833031 : && !nreversep
4864 1666282 : && !nvolatilep)
4865 : {
4866 833031 : inner = ninner;
4867 833251 : bitpos -= nbitpos;
4868 : }
4869 : }
4870 :
4871 837272 : alias_set_type iset = get_alias_set (orig_inner);
4872 837272 : 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 837272 : if (known_eq (bitpos, 0) && !reversep)
4878 : {
4879 13196 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4880 26392 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4881 12984 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4882 216 : && tree_fits_shwi_p (size)
4883 13412 : && tree_to_shwi (size) == bitsize)
4884 193 : return fold_convert_loc (loc, type, inner);
4885 : }
4886 :
4887 837079 : bftype = type;
4888 837079 : if (TYPE_PRECISION (bftype) != bitsize
4889 837079 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4890 646 : bftype = build_nonstandard_integer_type (bitsize, 0);
4891 :
4892 837079 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4893 837079 : bitsize_int (bitsize), bitsize_int (bitpos));
4894 837079 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4895 :
4896 837079 : if (bftype != type)
4897 646 : 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 4703979 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4924 : tree compare_type, tree lhs, tree rhs)
4925 : {
4926 4703979 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4927 4703979 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4928 4703979 : tree type = TREE_TYPE (lhs);
4929 4703979 : tree unsigned_type;
4930 4703979 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4931 4703979 : machine_mode lmode, rmode;
4932 4703979 : scalar_int_mode nmode;
4933 4703979 : int lunsignedp, runsignedp;
4934 4703979 : int lreversep, rreversep;
4935 4703979 : int lvolatilep = 0, rvolatilep = 0;
4936 4703979 : tree linner, rinner = NULL_TREE;
4937 4703979 : tree mask;
4938 4703979 : 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 4703979 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4946 : &lunsignedp, &lreversep, &lvolatilep);
4947 4703979 : if (linner == lhs
4948 4703979 : || !known_size_p (plbitsize)
4949 4703979 : || !plbitsize.is_constant (&lbitsize)
4950 4703979 : || !plbitpos.is_constant (&lbitpos)
4951 9407958 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4952 798325 : || offset != 0
4953 798300 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4954 5502279 : || lvolatilep)
4955 : return 0;
4956 :
4957 798240 : if (const_p)
4958 760375 : 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 37865 : rinner
4964 37865 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4965 : &runsignedp, &rreversep, &rvolatilep);
4966 :
4967 37865 : if (rinner == rhs
4968 37821 : || maybe_ne (lbitpos, rbitpos)
4969 37787 : || maybe_ne (lbitsize, rbitsize)
4970 37787 : || lunsignedp != runsignedp
4971 37787 : || lreversep != rreversep
4972 37787 : || offset != 0
4973 37787 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4974 75652 : || rvolatilep)
4975 : return 0;
4976 : }
4977 :
4978 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4979 798162 : poly_uint64 bitstart = 0;
4980 798162 : poly_uint64 bitend = 0;
4981 798162 : if (TREE_CODE (lhs) == COMPONENT_REF)
4982 : {
4983 798162 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4984 798162 : 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 1596324 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4991 760375 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4992 37787 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4993 : TYPE_ALIGN (TREE_TYPE (rinner))),
4994 798162 : 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 796159 : 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 796159 : nbitsize = GET_MODE_BITSIZE (nmode);
5005 796159 : nbitpos = lbitpos & ~ (nbitsize - 1);
5006 796159 : lbitpos -= nbitpos;
5007 796159 : if (nbitsize == lbitsize)
5008 : return 0;
5009 :
5010 796159 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5011 65 : lbitpos = nbitsize - lbitsize - lbitpos;
5012 :
5013 : /* Make the mask to be used against the extracted field. */
5014 796159 : mask = build_int_cst_type (unsigned_type, -1);
5015 796159 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
5016 796159 : mask = const_binop (RSHIFT_EXPR, mask,
5017 796159 : size_int (nbitsize - lbitsize - lbitpos));
5018 :
5019 796159 : if (! const_p)
5020 : {
5021 36232 : if (nbitpos < 0)
5022 : return 0;
5023 :
5024 : /* If not comparing with constant, just rework the comparison
5025 : and return. */
5026 36232 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5027 36232 : nbitsize, nbitpos, 1, lreversep);
5028 36232 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
5029 36232 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
5030 36232 : nbitsize, nbitpos, 1, rreversep);
5031 36232 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
5032 36232 : 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 759927 : if (lunsignedp)
5045 : {
5046 758751 : 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 1176 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5056 1176 : 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 1176 : }
5063 :
5064 759927 : if (nbitpos < 0)
5065 : return 0;
5066 :
5067 : /* Single-bit compares should always be against zero. */
5068 759927 : 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 759927 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5078 759927 : nbitsize, nbitpos, 1, lreversep);
5079 :
5080 759927 : rhs = const_binop (BIT_AND_EXPR,
5081 : const_binop (LSHIFT_EXPR,
5082 : fold_convert_loc (loc, unsigned_type, rhs),
5083 759927 : size_int (lbitpos)),
5084 : mask);
5085 :
5086 759927 : lhs = build2_loc (loc, code, compare_type,
5087 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5088 759927 : 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 2905 : sign_bit_p (tree exp, const_tree val)
5099 : {
5100 2905 : int width;
5101 2905 : tree t;
5102 :
5103 : /* Tree EXP must have an integral type. */
5104 2905 : t = TREE_TYPE (exp);
5105 2905 : if (! INTEGRAL_TYPE_P (t))
5106 : return NULL_TREE;
5107 :
5108 : /* Tree VAL must be an integer constant. */
5109 2559 : if (TREE_CODE (val) != INTEGER_CST
5110 2559 : || TREE_OVERFLOW (val))
5111 : return NULL_TREE;
5112 :
5113 2188 : width = TYPE_PRECISION (t);
5114 2188 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5115 : return exp;
5116 :
5117 : /* Handle extension from a narrower type. */
5118 1317 : if (TREE_CODE (exp) == NOP_EXPR
5119 1317 : && 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 66890902 : simple_operand_p (const_tree exp)
5130 : {
5131 : /* Strip any conversions that don't change the machine mode. */
5132 66890902 : STRIP_NOPS (exp);
5133 :
5134 66890902 : return (CONSTANT_CLASS_P (exp)
5135 46485711 : || TREE_CODE (exp) == SSA_NAME
5136 83063480 : || (DECL_P (exp)
5137 5593084 : && ! TREE_ADDRESSABLE (exp)
5138 5505154 : && ! TREE_THIS_VOLATILE (exp)
5139 5505154 : && ! 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 5503481 : && ! TREE_PUBLIC (exp)
5144 5482660 : && ! DECL_EXTERNAL (exp)
5145 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5146 5482660 : && ! ((VAR_P (exp)
5147 : || TREE_CODE (exp) == PARM_DECL
5148 : || TREE_CODE (exp) == RESULT_DECL)
5149 5482660 : && 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 5482071 : && (! 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 5482071 : && (! 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 7088424 : simple_condition_p (tree exp)
5165 : {
5166 7181326 : enum tree_code code;
5167 :
5168 7181326 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5169 : return false;
5170 :
5171 2290837 : while (CONVERT_EXPR_P (exp))
5172 34532 : exp = TREE_OPERAND (exp, 0);
5173 :
5174 2256305 : code = TREE_CODE (exp);
5175 :
5176 2256305 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5177 1762722 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5178 1762722 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5179 :
5180 493583 : if (code == TRUTH_NOT_EXPR)
5181 92902 : return simple_condition_p (TREE_OPERAND (exp, 0));
5182 :
5183 400681 : 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 23228135 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5224 : tree arg1, int upper1_p)
5225 : {
5226 23228135 : tree tem;
5227 23228135 : int result;
5228 23228135 : 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 23228135 : if (arg0 != 0 && arg1 != 0)
5236 : {
5237 12256247 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5238 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5239 12256247 : STRIP_NOPS (tem);
5240 12256247 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5241 : }
5242 :
5243 10971888 : 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 10962249 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5253 10962249 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5254 10962249 : switch (code)
5255 : {
5256 5166232 : case EQ_EXPR:
5257 5166232 : result = sgn0 == sgn1;
5258 5166232 : break;
5259 0 : case NE_EXPR:
5260 0 : result = sgn0 != sgn1;
5261 0 : break;
5262 364032 : case LT_EXPR:
5263 364032 : result = sgn0 < sgn1;
5264 364032 : break;
5265 2502420 : case LE_EXPR:
5266 2502420 : result = sgn0 <= sgn1;
5267 2502420 : break;
5268 2929565 : case GT_EXPR:
5269 2929565 : result = sgn0 > sgn1;
5270 2929565 : break;
5271 0 : case GE_EXPR:
5272 0 : result = sgn0 >= sgn1;
5273 0 : break;
5274 0 : default:
5275 0 : gcc_unreachable ();
5276 : }
5277 :
5278 10962249 : 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 61553784 : 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 61553784 : tree arg0_type = TREE_TYPE (arg0);
5290 61553784 : tree n_low, n_high, low = *p_low, high = *p_high;
5291 61553784 : int in_p = *p_in_p, n_in_p;
5292 :
5293 61553784 : switch (code)
5294 : {
5295 1740919 : case TRUTH_NOT_EXPR:
5296 : /* We can only do something if the range is testing for zero. */
5297 1740919 : if (low == NULL_TREE || high == NULL_TREE
5298 1740919 : || ! integer_zerop (low) || ! integer_zerop (high))
5299 : return NULL_TREE;
5300 1740919 : *p_in_p = ! in_p;
5301 1740919 : return arg0;
5302 :
5303 47884098 : case EQ_EXPR: case NE_EXPR:
5304 47884098 : 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 47884098 : if (low == NULL_TREE || high == NULL_TREE
5311 47884098 : || ! integer_zerop (low) || ! integer_zerop (high)
5312 95768101 : || TREE_CODE (arg1) != INTEGER_CST)
5313 : return NULL_TREE;
5314 :
5315 30111760 : switch (code)
5316 : {
5317 : case NE_EXPR: /* - [c, c] */
5318 : low = high = arg1;
5319 : break;
5320 8143403 : case EQ_EXPR: /* + [c, c] */
5321 8143403 : in_p = ! in_p, low = high = arg1;
5322 8143403 : break;
5323 2245375 : case GT_EXPR: /* - [-, c] */
5324 2245375 : low = 0, high = arg1;
5325 2245375 : break;
5326 724752 : case GE_EXPR: /* + [c, -] */
5327 724752 : in_p = ! in_p, low = arg1, high = 0;
5328 724752 : break;
5329 5848542 : case LT_EXPR: /* - [c, -] */
5330 5848542 : low = arg1, high = 0;
5331 5848542 : break;
5332 4407903 : case LE_EXPR: /* + [-, c] */
5333 4407903 : in_p = ! in_p, low = 0, high = arg1;
5334 4407903 : 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 30111760 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5345 : {
5346 1910134 : 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 1910121 : 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 1910121 : if (high == 0 && low && ! integer_zerop (low))
5358 : {
5359 845715 : in_p = ! in_p;
5360 845715 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5361 845715 : build_int_cst (TREE_TYPE (low), 1), 0);
5362 845715 : low = build_int_cst (arg0_type, 0);
5363 : }
5364 : }
5365 :
5366 30111747 : *p_low = low;
5367 30111747 : *p_high = high;
5368 30111747 : *p_in_p = in_p;
5369 30111747 : return arg0;
5370 :
5371 329 : 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 329 : if (!TYPE_UNSIGNED (arg0_type)
5375 329 : && !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 329 : n_low = range_binop (MINUS_EXPR, exp_type,
5385 : build_int_cst (exp_type, 0),
5386 : 0, high, 1);
5387 329 : n_high = range_binop (MINUS_EXPR, exp_type,
5388 : build_int_cst (exp_type, 0),
5389 : 0, low, 0);
5390 329 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5391 : return NULL_TREE;
5392 311 : 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 883652 : case PLUS_EXPR:
5400 883652 : case MINUS_EXPR:
5401 883652 : 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 675813 : if (!TYPE_UNSIGNED (arg0_type)
5407 675813 : && !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 976403 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5415 : arg0_type, low, 0, arg1, 0);
5416 489738 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5417 : arg0_type, high, 1, arg1, 0);
5418 485723 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5419 975449 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5420 : return NULL_TREE;
5421 :
5422 490037 : 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 490037 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5426 : {
5427 141920 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5428 141920 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5429 141920 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5430 141920 : 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 141920 : if (tree_int_cst_equal (n_low, low)
5437 141920 : && tree_int_cst_equal (n_high, high))
5438 : low = high = 0;
5439 : else
5440 141920 : in_p = ! in_p;
5441 : }
5442 : else
5443 348117 : low = n_low, high = n_high;
5444 :
5445 490037 : *p_low = low;
5446 490037 : *p_high = high;
5447 490037 : *p_in_p = in_p;
5448 490037 : return arg0;
5449 :
5450 2651852 : CASE_CONVERT:
5451 2651852 : case NON_LVALUE_EXPR:
5452 2651852 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5453 : return NULL_TREE;
5454 :
5455 1207587 : if (! INTEGRAL_TYPE_P (arg0_type)
5456 1172415 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5457 1079747 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5458 : return NULL_TREE;
5459 :
5460 1061079 : n_low = low, n_high = high;
5461 :
5462 1061079 : if (n_low != 0)
5463 863657 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5464 :
5465 1061079 : if (n_high != 0)
5466 995867 : 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 1061079 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5478 : {
5479 287933 : tree high_positive;
5480 287933 : tree equiv_type;
5481 : /* For fixed-point modes, we need to pass the saturating flag
5482 : as the 2nd parameter. */
5483 287933 : 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 287933 : else if (BITINT_TYPE_P (arg0_type))
5488 : equiv_type = arg0_type;
5489 : else
5490 287918 : equiv_type
5491 287918 : = 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 287933 : high_positive
5497 287933 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5498 0 : : TYPE_MAX_VALUE (arg0_type);
5499 :
5500 287933 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5501 266560 : 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 287933 : if (low != 0)
5510 : {
5511 96160 : 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 96160 : 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 191773 : 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 191773 : 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 773146 : else if (TYPE_UNSIGNED (exp_type)
5544 754441 : && !TYPE_UNSIGNED (arg0_type)
5545 385220 : && low
5546 1158366 : && !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 1061079 : *p_low = n_low;
5560 1061079 : *p_high = n_high;
5561 1061079 : *p_in_p = in_p;
5562 1061079 : 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 50912774 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5577 : {
5578 50912774 : enum tree_code code;
5579 50912774 : tree arg0, arg1 = NULL_TREE;
5580 50912774 : tree exp_type, nexp;
5581 50912774 : int in_p;
5582 50912774 : tree low, high;
5583 50912774 : 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 50912774 : in_p = 0;
5592 50912774 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5593 :
5594 81231894 : while (1)
5595 : {
5596 81231894 : code = TREE_CODE (exp);
5597 81231894 : exp_type = TREE_TYPE (exp);
5598 81231894 : arg0 = NULL_TREE;
5599 :
5600 81231894 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5601 : {
5602 56726157 : if (TREE_OPERAND_LENGTH (exp) > 0)
5603 56726157 : arg0 = TREE_OPERAND (exp, 0);
5604 56726157 : if (TREE_CODE_CLASS (code) == tcc_binary
5605 53603493 : || TREE_CODE_CLASS (code) == tcc_comparison
5606 66528184 : || (TREE_CODE_CLASS (code) == tcc_expression
5607 2928275 : && TREE_OPERAND_LENGTH (exp) > 1))
5608 48094350 : arg1 = TREE_OPERAND (exp, 1);
5609 : }
5610 56726157 : if (arg0 == NULL_TREE)
5611 : break;
5612 :
5613 56726143 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5614 : &high, &in_p);
5615 56726143 : 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 50912774 : if (TREE_CODE (exp) == INTEGER_CST)
5622 : {
5623 31594 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5624 : exp, 0, low, 0))
5625 31594 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5626 : exp, 1, high, 1)));
5627 31594 : low = high = 0;
5628 31594 : exp = 0;
5629 : }
5630 :
5631 50912774 : *pin_p = in_p, *plow = low, *phigh = high;
5632 50912774 : 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 114 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5643 : tree *value)
5644 : {
5645 114 : if (TREE_CODE (low) != INTEGER_CST
5646 114 : || TREE_CODE (high) != INTEGER_CST)
5647 : return false;
5648 :
5649 114 : unsigned prec = TYPE_PRECISION (type);
5650 114 : wide_int lo = wi::to_wide (low, prec);
5651 114 : wide_int hi = wi::to_wide (high, prec);
5652 :
5653 114 : wide_int end_mask = lo ^ hi;
5654 228 : if ((end_mask & (end_mask + 1)) != 0
5655 224 : || (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 200 : }
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 587607 : 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 587607 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5678 0 : etype = TREE_TYPE (etype);
5679 587607 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5680 60586 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5681 :
5682 587607 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5683 : {
5684 403193 : 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 403193 : utype = unsigned_type_for (etype);
5689 403193 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5690 403193 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5691 403193 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5692 403193 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5693 :
5694 403193 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5695 : minv, 1, maxv, 1)))
5696 : etype = utype;
5697 : else
5698 113 : return NULL_TREE;
5699 : }
5700 184414 : 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 1373 : 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 1603711 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5716 : tree low, tree high)
5717 : {
5718 2796092 : 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 2796092 : if (targetm.have_canonicalize_funcptr_for_compare ()
5723 0 : && POINTER_TYPE_P (etype)
5724 2796092 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5725 : return NULL_TREE;
5726 :
5727 2796092 : if (! in_p)
5728 : {
5729 310634 : value = build_range_check (loc, type, exp, 1, low, high);
5730 310634 : if (value != 0)
5731 310634 : return invert_truthvalue_loc (loc, value);
5732 :
5733 : return 0;
5734 : }
5735 :
5736 2485458 : if (low == 0 && high == 0)
5737 121315 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5738 :
5739 2364143 : if (low == 0)
5740 778403 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5741 778403 : fold_convert_loc (loc, etype, high));
5742 :
5743 1585740 : if (high == 0)
5744 73603 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5745 73603 : fold_convert_loc (loc, etype, low));
5746 :
5747 1512137 : if (operand_equal_p (low, high, 0))
5748 319502 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5749 319502 : fold_convert_loc (loc, etype, low));
5750 :
5751 1192635 : if (TREE_CODE (exp) == BIT_AND_EXPR
5752 1192635 : && 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 1192549 : if (integer_zerop (low))
5759 : {
5760 697584 : if (! TYPE_UNSIGNED (etype))
5761 : {
5762 183919 : etype = unsigned_type_for (etype);
5763 183919 : high = fold_convert_loc (loc, etype, high);
5764 183919 : 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 494965 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5771 : {
5772 128659 : int prec = TYPE_PRECISION (etype);
5773 :
5774 128659 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5775 : {
5776 138 : if (TYPE_UNSIGNED (etype))
5777 : {
5778 138 : tree signed_etype = signed_type_for (etype);
5779 138 : 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 138 : exp = fold_convert_loc (loc, etype, exp);
5785 : }
5786 138 : 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 494827 : etype = range_check_type (etype);
5794 494827 : if (etype == NULL_TREE)
5795 : return NULL_TREE;
5796 :
5797 494797 : high = fold_convert_loc (loc, etype, high);
5798 494797 : low = fold_convert_loc (loc, etype, low);
5799 494797 : exp = fold_convert_loc (loc, etype, exp);
5800 :
5801 494797 : value = const_binop (MINUS_EXPR, high, low);
5802 :
5803 494797 : if (value != 0 && !TREE_OVERFLOW (value))
5804 494797 : 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 176500 : range_predecessor (tree val)
5815 : {
5816 176500 : tree type = TREE_TYPE (val);
5817 :
5818 176500 : if (INTEGRAL_TYPE_P (type)
5819 176500 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5820 : return 0;
5821 : else
5822 176500 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5823 176500 : 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 1677211 : range_successor (tree val)
5830 : {
5831 1677211 : tree type = TREE_TYPE (val);
5832 :
5833 1677211 : if (INTEGRAL_TYPE_P (type)
5834 1677211 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5835 : return 0;
5836 : else
5837 1677198 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5838 1677198 : 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 3641463 : 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 3641463 : bool no_overlap;
5849 3641463 : int subset;
5850 3641463 : int temp;
5851 3641463 : tree tem;
5852 3641463 : int in_p;
5853 3641463 : tree low, high;
5854 3641463 : int lowequal = ((low0 == 0 && low1 == 0)
5855 3641463 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5856 3641463 : low0, 0, low1, 0)));
5857 3641463 : int highequal = ((high0 == 0 && high1 == 0)
5858 3641463 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5859 3641463 : 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 3641463 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5864 : low0, 0, low1, 0))
5865 3641463 : || (lowequal
5866 572499 : && 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 3641463 : if (low1
5877 3641463 : && high1
5878 1033214 : && TREE_CODE (low1) == INTEGER_CST
5879 1033214 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5880 127509 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5881 170218 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5882 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5883 4632277 : && operand_equal_p (low1, high1, 0))
5884 : {
5885 560324 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5886 560324 : && 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 448676 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5892 448676 : && 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 3436529 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5901 : high0, 1, low1, 0));
5902 3436529 : 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 3436529 : 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 1558219 : if (no_overlap)
5913 : in_p = 0, low = high = 0;
5914 1556013 : else if (subset)
5915 : in_p = 1, low = low1, high = high1;
5916 : else
5917 1436656 : in_p = 1, low = low1, high = high0;
5918 : }
5919 :
5920 1878310 : 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 324879 : if (no_overlap)
5931 : in_p = 1, low = low0, high = high0;
5932 318646 : else if (lowequal && highequal)
5933 : in_p = 0, low = high = 0;
5934 317792 : else if (subset && lowequal)
5935 : {
5936 242468 : low = range_successor (high1);
5937 242468 : high = high0;
5938 242468 : in_p = 1;
5939 242468 : 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 75324 : else if (! subset || highequal)
5947 : {
5948 54393 : low = low0;
5949 54393 : high = range_predecessor (low1);
5950 54393 : in_p = 1;
5951 54393 : 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 1553431 : 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 1193072 : if (no_overlap)
5968 : in_p = 1, low = low1, high = high1;
5969 1185369 : else if (subset || highequal)
5970 : in_p = 0, low = high = 0;
5971 : else
5972 : {
5973 1078843 : low = range_successor (high0);
5974 1078843 : high = high1;
5975 1078843 : in_p = 1;
5976 1078843 : 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 360359 : if (no_overlap)
5993 : {
5994 261879 : 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 210599 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
6002 209473 : switch (TREE_CODE (TREE_TYPE (low0)))
6003 : {
6004 51181 : case ENUMERAL_TYPE:
6005 51181 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
6006 : GET_MODE_BITSIZE
6007 102362 : (TYPE_MODE (TREE_TYPE (low0)))))
6008 : break;
6009 : /* FALLTHROUGH */
6010 209272 : case INTEGER_TYPE:
6011 209272 : if (tree_int_cst_equal (low0,
6012 209272 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
6013 210599 : 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 210599 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
6026 210453 : switch (TREE_CODE (TREE_TYPE (high1)))
6027 : {
6028 51189 : case ENUMERAL_TYPE:
6029 51189 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
6030 : GET_MODE_BITSIZE
6031 102378 : (TYPE_MODE (TREE_TYPE (high1)))))
6032 : break;
6033 : /* FALLTHROUGH */
6034 210252 : case INTEGER_TYPE:
6035 210252 : if (tree_int_cst_equal (high1,
6036 210252 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
6037 210599 : 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 210599 : if (low0 == 0 && high1 == 0)
6056 : {
6057 735 : low = range_successor (high0);
6058 735 : high = range_predecessor (low1);
6059 735 : if (low == 0 || high == 0)
6060 : return 0;
6061 :
6062 : in_p = 1;
6063 : }
6064 : else
6065 : return 0;
6066 : }
6067 : }
6068 98480 : else if (subset)
6069 : in_p = 0, low = low0, high = high0;
6070 : else
6071 11998 : in_p = 0, low = low0, high = high1;
6072 : }
6073 :
6074 3205721 : *pin_p = in_p, *plow = low, *phigh = high;
6075 3205721 : 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 531338 : 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 531338 : tree tem;
6094 :
6095 531338 : STRIP_NOPS (arg1);
6096 531338 : STRIP_NOPS (arg2);
6097 :
6098 : /* If we have A op 0 ? A : -A, consider applying the following
6099 : transformations:
6100 :
6101 : A == 0? A : -A same as -A
6102 : A != 0? A : -A same as A
6103 : A >= 0? A : -A same as abs (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 :
6108 : None of these transformations work for modes with signed
6109 : zeros. If A is +/-0, the first two transformations will
6110 : change the sign of the result (from +0 to -0, or vice
6111 : versa). The last four will fix the sign of the result,
6112 : even though the original expressions could be positive or
6113 : negative, depending on the sign of A.
6114 :
6115 : Note that all these transformations are correct if A is
6116 : NaN, since the two alternatives (A and -A) are also NaNs. */
6117 531338 : if (!HONOR_SIGNED_ZEROS (type)
6118 1062680 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6119 531338 : ? real_zerop (arg01)
6120 530852 : : integer_zerop (arg01))
6121 1432318 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6122 1048 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6123 : /* In the case that A is of the form X-Y, '-A' (arg2) may
6124 : have already been folded to Y-X, check for that. */
6125 368818 : || (TREE_CODE (arg1) == MINUS_EXPR
6126 1711 : && TREE_CODE (arg2) == MINUS_EXPR
6127 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6128 0 : TREE_OPERAND (arg2, 1), 0)
6129 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6130 0 : TREE_OPERAND (arg2, 0), 0))))
6131 824 : switch (comp_code)
6132 : {
6133 7 : case EQ_EXPR:
6134 7 : case UNEQ_EXPR:
6135 : /* Reuse the negate/minus expression for overflow reasons. */
6136 7 : return fold_convert_loc (loc, type, arg2);
6137 0 : case NE_EXPR:
6138 0 : case LTGT_EXPR:
6139 0 : return fold_convert_loc (loc, type, arg1);
6140 0 : case UNGE_EXPR:
6141 0 : case UNGT_EXPR:
6142 0 : if (flag_trapping_math)
6143 : break;
6144 : /* Fall through. */
6145 801 : case GE_EXPR:
6146 801 : case GT_EXPR:
6147 801 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6148 : break;
6149 785 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6150 785 : return fold_convert_loc (loc, type, tem);
6151 0 : case UNLE_EXPR:
6152 0 : case UNLT_EXPR:
6153 0 : if (flag_trapping_math)
6154 : break;
6155 : /* FALLTHRU */
6156 16 : case LE_EXPR:
6157 16 : case LT_EXPR:
6158 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6159 : break;
6160 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6161 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6162 : {
6163 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6164 : is not, invokes UB both in abs and in the negation of it.
6165 : So, use ABSU_EXPR instead. */
6166 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6167 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6168 16 : tem = negate_expr (tem);
6169 16 : return fold_convert_loc (loc, type, tem);
6170 : }
6171 : else
6172 : {
6173 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6174 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6175 : }
6176 0 : default:
6177 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6178 : break;
6179 : }
6180 :
6181 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6182 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6183 : both transformations are correct when A is NaN: A != 0
6184 : is then true, and A == 0 is false. */
6185 :
6186 530530 : if (!HONOR_SIGNED_ZEROS (type)
6187 530530 : && integer_zerop (arg01) && integer_zerop (arg2))
6188 : {
6189 282849 : if (comp_code == NE_EXPR)
6190 147 : return fold_convert_loc (loc, type, arg1);
6191 282702 : else if (comp_code == EQ_EXPR)
6192 0 : return build_zero_cst (type);
6193 : }
6194 :
6195 : /* Try some transformations of A op B ? A : B.
6196 :
6197 : A == B? A : B same as B
6198 : A != B? A : B same as A
6199 : A >= B? A : B same as max (A, B)
6200 : A > B? A : B same as max (B, A)
6201 : A <= B? A : B same as min (A, B)
6202 : A < B? A : B same as min (B, A)
6203 :
6204 : As above, these transformations don't work in the presence
6205 : of signed zeros. For example, if A and B are zeros of
6206 : opposite sign, the first two transformations will change
6207 : the sign of the result. In the last four, the original
6208 : expressions give different results for (A=+0, B=-0) and
6209 : (A=-0, B=+0), but the transformed expressions do not.
6210 :
6211 : The first two transformations are correct if either A or B
6212 : is a NaN. In the first transformation, the condition will
6213 : be false, and B will indeed be chosen. In the case of the
6214 : second transformation, the condition A != B will be true,
6215 : and A will be chosen.
6216 :
6217 : The conversions to max() and min() are not correct if B is
6218 : a number and A is not. The conditions in the original
6219 : expressions will be false, so all four give B. The min()
6220 : and max() versions would give a NaN instead. */
6221 530383 : if (!HONOR_SIGNED_ZEROS (type)
6222 530383 : && operand_equal_for_comparison_p (arg01, arg2)
6223 : /* Avoid these transformations if the COND_EXPR may be used
6224 : as an lvalue in the C++ front-end. PR c++/19199. */
6225 822233 : && (in_gimple_form
6226 19059 : || VECTOR_TYPE_P (type)
6227 18986 : || (! lang_GNU_CXX ()
6228 16415 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6229 2571 : || ! maybe_lvalue_p (arg1)
6230 2539 : || ! maybe_lvalue_p (arg2)))
6231 : {
6232 290089 : tree comp_op0 = arg00;
6233 290089 : tree comp_op1 = arg01;
6234 290089 : tree comp_type = TREE_TYPE (comp_op0);
6235 :
6236 290089 : switch (comp_code)
6237 : {
6238 0 : case EQ_EXPR:
6239 0 : return fold_convert_loc (loc, type, arg2);
6240 1 : case NE_EXPR:
6241 1 : return fold_convert_loc (loc, type, arg1);
6242 6201 : case LE_EXPR:
6243 6201 : case LT_EXPR:
6244 6201 : case UNLE_EXPR:
6245 6201 : case UNLT_EXPR:
6246 : /* In C++ a ?: expression can be an lvalue, so put the
6247 : operand which will be used if they are equal first
6248 : so that we can convert this back to the
6249 : corresponding COND_EXPR. */
6250 6201 : if (!HONOR_NANS (arg1))
6251 : {
6252 6201 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6253 6201 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6254 6201 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6255 6201 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6256 4811 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6257 : comp_op1, comp_op0);
6258 6201 : return fold_convert_loc (loc, type, tem);
6259 : }
6260 : break;
6261 283887 : case GE_EXPR:
6262 283887 : case GT_EXPR:
6263 283887 : case UNGE_EXPR:
6264 283887 : case UNGT_EXPR:
6265 283887 : if (!HONOR_NANS (arg1))
6266 : {
6267 283885 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6268 283885 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6269 283885 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6270 283885 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6271 3969 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6272 : comp_op1, comp_op0);
6273 283885 : return fold_convert_loc (loc, type, tem);
6274 : }
6275 : break;
6276 0 : case UNEQ_EXPR:
6277 0 : if (!HONOR_NANS (arg1))
6278 0 : return fold_convert_loc (loc, type, arg2);
6279 : break;
6280 0 : case LTGT_EXPR:
6281 0 : if (!HONOR_NANS (arg1))
6282 0 : return fold_convert_loc (loc, type, arg1);
6283 : break;
6284 0 : default:
6285 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6286 : break;
6287 : }
6288 : }
6289 :
6290 : return NULL_TREE;
6291 : }
6292 :
6293 :
6294 :
6295 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6296 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6297 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6298 : false) >= 2)
6299 : #endif
6300 :
6301 : /* EXP is some logical combination of boolean tests. See if we can
6302 : merge it into some range test. Return the new tree if so. */
6303 :
6304 : static tree
6305 25455881 : fold_range_test (location_t loc, enum tree_code code, tree type,
6306 : tree op0, tree op1)
6307 : {
6308 25455881 : int or_op = (code == TRUTH_ORIF_EXPR
6309 25455881 : || code == TRUTH_OR_EXPR);
6310 25455881 : int in0_p, in1_p, in_p;
6311 25455881 : tree low0, low1, low, high0, high1, high;
6312 25455881 : tree tem, lhs, rhs;
6313 :
6314 25455881 : if (!INTEGRAL_TYPE_P (type))
6315 : return 0;
6316 :
6317 25455881 : lhs = make_range (op0, &in0_p, &low0, &high0);
6318 : /* If op0 is known true or false and this is a short-circuiting
6319 : operation we must not merge with op1 since that makes side-effects
6320 : unconditional. So special-case this. */
6321 25455881 : if (!lhs
6322 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6323 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6324 : return op0;
6325 25455879 : rhs = make_range (op1, &in1_p, &low1, &high1);
6326 :
6327 : /* If this is an OR operation, invert both sides; we will invert
6328 : again at the end. */
6329 25455879 : if (or_op)
6330 11985594 : in0_p = ! in0_p, in1_p = ! in1_p;
6331 :
6332 : /* If both expressions are the same, if we can merge the ranges, and we
6333 : can build the range test, return it or it inverted. If one of the
6334 : ranges is always true or always false, consider it to be the same
6335 : expression as the other. */
6336 25424289 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6337 1195872 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6338 : in1_p, low1, high1)
6339 26450840 : && (tem = (build_range_check (loc, type,
6340 : lhs != 0 ? lhs
6341 0 : : rhs != 0 ? rhs : integer_zero_node,
6342 : in_p, low, high))) != 0)
6343 : {
6344 994931 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6345 : }
6346 :
6347 : /* On machines where the branch cost is expensive, if this is a
6348 : short-circuited branch and the underlying object on both sides
6349 : is the same, make a non-short-circuit operation. */
6350 24460948 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6351 24460948 : if (param_logical_op_non_short_circuit != -1)
6352 7827 : logical_op_non_short_circuit
6353 7827 : = param_logical_op_non_short_circuit;
6354 24460948 : if (logical_op_non_short_circuit
6355 24457007 : && !sanitize_coverage_p ()
6356 24457004 : && lhs != 0 && rhs != 0
6357 24456565 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6358 29989339 : && operand_equal_p (lhs, rhs, 0))
6359 : {
6360 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6361 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6362 : which cases we can't do this. */
6363 171096 : if (simple_operand_p (lhs))
6364 69844 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6365 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6366 35445 : type, op0, op1);
6367 :
6368 135651 : else if (!lang_hooks.decls.global_bindings_p ()
6369 135651 : && !CONTAINS_PLACEHOLDER_P (lhs))
6370 : {
6371 134972 : tree common = save_expr (lhs);
6372 :
6373 248290 : if ((lhs = build_range_check (loc, type, common,
6374 113318 : or_op ? ! in0_p : in0_p,
6375 : low0, high0)) != 0
6376 248290 : && (rhs = build_range_check (loc, type, common,
6377 113318 : or_op ? ! in1_p : in1_p,
6378 : low1, high1)) != 0)
6379 : {
6380 248290 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6381 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6382 134972 : type, lhs, rhs);
6383 : }
6384 : }
6385 : }
6386 :
6387 : return 0;
6388 : }
6389 :
6390 : /* For an expression that has the form
6391 : (A && B) || ~B
6392 : or
6393 : (A || B) && ~B,
6394 : we can drop one of the inner expressions and simplify to
6395 : A || ~B
6396 : or
6397 : A && ~B
6398 : LOC is the location of the resulting expression. OP is the inner
6399 : logical operation; the left-hand side in the examples above, while CMPOP
6400 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6401 : removing a condition that guards another, as in
6402 : (A != NULL && A->...) || A == NULL
6403 : which we must not transform. If RHS_ONLY is true, only eliminate the
6404 : right-most operand of the inner logical operation. */
6405 :
6406 : static tree
6407 130165 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6408 : bool rhs_only)
6409 : {
6410 130165 : enum tree_code code = TREE_CODE (cmpop);
6411 130165 : enum tree_code truthop_code = TREE_CODE (op);
6412 130165 : tree lhs = TREE_OPERAND (op, 0);
6413 130165 : tree rhs = TREE_OPERAND (op, 1);
6414 130165 : tree orig_lhs = lhs, orig_rhs = rhs;
6415 130165 : enum tree_code rhs_code = TREE_CODE (rhs);
6416 130165 : enum tree_code lhs_code = TREE_CODE (lhs);
6417 130165 : enum tree_code inv_code;
6418 :
6419 130165 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6420 : return NULL_TREE;
6421 :
6422 116523 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6423 : return NULL_TREE;
6424 :
6425 38964 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6426 :
6427 38964 : if (rhs_code == truthop_code)
6428 : {
6429 37 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6430 37 : if (newrhs != NULL_TREE)
6431 : {
6432 0 : rhs = newrhs;
6433 0 : rhs_code = TREE_CODE (rhs);
6434 : }
6435 : }
6436 38964 : if (lhs_code == truthop_code && !rhs_only)
6437 : {
6438 484 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6439 484 : if (newlhs != NULL_TREE)
6440 : {
6441 0 : lhs = newlhs;
6442 0 : lhs_code = TREE_CODE (lhs);
6443 : }
6444 : }
6445 :
6446 38964 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6447 38964 : if (inv_code == rhs_code
6448 932 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6449 39000 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6450 : return lhs;
6451 38951 : if (!rhs_only && inv_code == lhs_code
6452 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6453 39043 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6454 : return rhs;
6455 38860 : if (rhs != orig_rhs || lhs != orig_lhs)
6456 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6457 0 : lhs, rhs);
6458 : return NULL_TREE;
6459 : }
6460 :
6461 : /* Find ways of folding logical expressions of LHS and RHS:
6462 : Try to merge two comparisons to the same innermost item.
6463 : Look for range tests like "ch >= '0' && ch <= '9'".
6464 : Look for combinations of simple terms on machines with expensive branches
6465 : and evaluate the RHS unconditionally.
6466 :
6467 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6468 : function and the one above.
6469 :
6470 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6471 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6472 :
6473 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6474 : two operands.
6475 :
6476 : We return the simplified tree or 0 if no optimization is possible. */
6477 :
6478 : static tree
6479 25133868 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6480 : tree lhs, tree rhs)
6481 : {
6482 : /* If this is the "or" of two comparisons, we can do something if
6483 : the comparisons are NE_EXPR. If this is the "and", we can do something
6484 : if the comparisons are EQ_EXPR. I.e.,
6485 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6486 :
6487 : WANTED_CODE is this operation code. For single bit fields, we can
6488 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6489 : comparison for one-bit fields. */
6490 :
6491 25133868 : enum tree_code lcode, rcode;
6492 25133868 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6493 25133868 : tree result;
6494 :
6495 : /* Start by getting the comparison codes. Fail if anything is volatile.
6496 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6497 : it were surrounded with a NE_EXPR. */
6498 :
6499 25133868 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6500 : return 0;
6501 :
6502 22245877 : lcode = TREE_CODE (lhs);
6503 22245877 : rcode = TREE_CODE (rhs);
6504 :
6505 22245877 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6506 : {
6507 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6508 0 : build_int_cst (TREE_TYPE (lhs), 0));
6509 0 : lcode = NE_EXPR;
6510 : }
6511 :
6512 22245877 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6513 : {
6514 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6515 0 : build_int_cst (TREE_TYPE (rhs), 0));
6516 0 : rcode = NE_EXPR;
6517 : }
6518 :
6519 22245877 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6520 19868630 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6521 : return 0;
6522 :
6523 18752434 : ll_arg = TREE_OPERAND (lhs, 0);
6524 18752434 : lr_arg = TREE_OPERAND (lhs, 1);
6525 18752434 : rl_arg = TREE_OPERAND (rhs, 0);
6526 18752434 : rr_arg = TREE_OPERAND (rhs, 1);
6527 :
6528 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6529 18752434 : if (simple_operand_p (ll_arg)
6530 18752434 : && simple_operand_p (lr_arg))
6531 : {
6532 15194276 : if (operand_equal_p (ll_arg, rl_arg, 0)
6533 15194276 : && operand_equal_p (lr_arg, rr_arg, 0))
6534 : {
6535 21537 : result = combine_comparisons (loc, code, lcode, rcode,
6536 21537 : truth_type, TREE_TYPE (lhs),
6537 : ll_arg, lr_arg);
6538 21537 : if (result)
6539 : return result;
6540 : }
6541 15172739 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6542 15172739 : && operand_equal_p (lr_arg, rl_arg, 0))
6543 : {
6544 241 : result = combine_comparisons (loc, code, lcode,
6545 : swap_tree_comparison (rcode),
6546 241 : truth_type, TREE_TYPE (lhs),
6547 : ll_arg, lr_arg);
6548 241 : if (result)
6549 : return result;
6550 : }
6551 : }
6552 :
6553 8866567 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6554 18731176 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6555 :
6556 : /* If the RHS can be evaluated unconditionally and its operands are
6557 : simple, it wins to evaluate the RHS unconditionally on machines
6558 : with expensive branches. In this case, this isn't a comparison
6559 : that can be merged. */
6560 :
6561 18731176 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6562 : false) >= 2
6563 18731077 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6564 17690280 : && simple_operand_p (rl_arg)
6565 28923945 : && simple_operand_p (rr_arg))
6566 : {
6567 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6568 11439069 : if (code == TRUTH_OR_EXPR
6569 1558028 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6570 629581 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6571 20350 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6572 11456796 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6573 34830 : return build2_loc (loc, NE_EXPR, truth_type,
6574 17415 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6575 : ll_arg, rl_arg),
6576 17415 : build_int_cst (TREE_TYPE (ll_arg), 0));
6577 :
6578 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6579 11421654 : if (code == TRUTH_AND_EXPR
6580 1750230 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6581 826595 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6582 8277 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6583 11423489 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6584 3234 : return build2_loc (loc, EQ_EXPR, truth_type,
6585 1617 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6586 : ll_arg, rl_arg),
6587 1617 : build_int_cst (TREE_TYPE (ll_arg), 0));
6588 : }
6589 :
6590 : return 0;
6591 : }
6592 :
6593 : /* T is an integer expression that is being multiplied, divided, or taken a
6594 : modulus (CODE says which and what kind of divide or modulus) by a
6595 : constant C. See if we can eliminate that operation by folding it with
6596 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6597 : should be used for the computation if wider than our type.
6598 :
6599 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6600 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6601 : expression would not overflow or that overflow is undefined for the type
6602 : in the language in question.
6603 :
6604 : If we return a non-null expression, it is an equivalent form of the
6605 : original computation, but need not be in the original type. */
6606 :
6607 : static tree
6608 93965404 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6609 : {
6610 : /* To avoid exponential search depth, refuse to allow recursion past
6611 : three levels. Beyond that (1) it's highly unlikely that we'll find
6612 : something interesting and (2) we've probably processed it before
6613 : when we built the inner expression. */
6614 :
6615 93965404 : static int depth;
6616 93965404 : tree ret;
6617 :
6618 93965404 : if (depth > 3)
6619 : return NULL;
6620 :
6621 90338673 : depth++;
6622 90338673 : ret = extract_muldiv_1 (t, c, code, wide_type);
6623 90338673 : depth--;
6624 :
6625 90338673 : return ret;
6626 : }
6627 :
6628 : static tree
6629 90338673 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6630 : {
6631 90338673 : tree type = TREE_TYPE (t);
6632 90338673 : enum tree_code tcode = TREE_CODE (t);
6633 90338673 : tree ctype = type;
6634 90338673 : if (wide_type)
6635 : {
6636 29079436 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6637 : {
6638 177 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6639 90338673 : ctype = wide_type;
6640 : }
6641 29079259 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6642 58158518 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6643 90338673 : ctype = wide_type;
6644 : }
6645 90338673 : tree t1, t2;
6646 90338673 : bool same_p = tcode == code;
6647 90338673 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6648 :
6649 : /* Don't deal with constants of zero here; they confuse the code below. */
6650 90338673 : if (integer_zerop (c))
6651 : return NULL_TREE;
6652 :
6653 90320384 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6654 35241729 : op0 = TREE_OPERAND (t, 0);
6655 :
6656 90320384 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6657 12096121 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6658 :
6659 : /* Note that we need not handle conditional operations here since fold
6660 : already handles those cases. So just do arithmetic here. */
6661 90320384 : switch (tcode)
6662 : {
6663 4335388 : case INTEGER_CST:
6664 : /* For a constant, we can always simplify if we are a multiply
6665 : or (for divide and modulus) if it is a multiple of our constant. */
6666 4335388 : if (code == MULT_EXPR
6667 5577935 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6668 1242547 : TYPE_SIGN (type)))
6669 : {
6670 3523255 : tree tem = const_binop (code, fold_convert (ctype, t),
6671 : fold_convert (ctype, c));
6672 : /* If the multiplication overflowed, we lost information on it.
6673 : See PR68142 and PR69845. */
6674 3523255 : if (TREE_OVERFLOW (tem))
6675 : return NULL_TREE;
6676 3519732 : return tem;
6677 : }
6678 : break;
6679 :
6680 34640608 : CASE_CONVERT: case NON_LVALUE_EXPR:
6681 34640608 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6682 : break;
6683 : /* If op0 is an expression ... */
6684 33367849 : if ((COMPARISON_CLASS_P (op0)
6685 : || UNARY_CLASS_P (op0)
6686 33367849 : || BINARY_CLASS_P (op0)
6687 30317902 : || VL_EXP_CLASS_P (op0)
6688 30256718 : || EXPRESSION_CLASS_P (op0))
6689 : /* ... and has wrapping overflow, and its type is smaller
6690 : than ctype, then we cannot pass through as widening. */
6691 33522518 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6692 1241690 : && (TYPE_PRECISION (ctype)
6693 1241690 : > TYPE_PRECISION (TREE_TYPE (op0))))
6694 : /* ... or this is a truncation (t is narrower than op0),
6695 : then we cannot pass through this narrowing. */
6696 2641831 : || (TYPE_PRECISION (type)
6697 2641831 : < TYPE_PRECISION (TREE_TYPE (op0)))
6698 : /* ... or signedness changes for division or modulus,
6699 : then we cannot pass through this conversion. */
6700 2607294 : || (code != MULT_EXPR
6701 133387 : && (TYPE_UNSIGNED (ctype)
6702 133387 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6703 : /* ... or has undefined overflow while the converted to
6704 : type has not, we cannot do the operation in the inner type
6705 : as that would introduce undefined overflow. */
6706 2500593 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6707 1845746 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6708 : break;
6709 :
6710 : /* Pass the constant down and see if we can make a simplification. If
6711 : we can, replace this expression with the inner simplification for
6712 : possible later conversion to our or some other type. */
6713 30885192 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6714 30885192 : && TREE_CODE (t2) == INTEGER_CST
6715 30885192 : && !TREE_OVERFLOW (t2)
6716 63013334 : && (t1 = extract_muldiv (op0, t2, code,
6717 : code == MULT_EXPR ? ctype : NULL_TREE))
6718 : != 0)
6719 : return t1;
6720 : break;
6721 :
6722 189 : case ABS_EXPR:
6723 : /* If widening the type changes it from signed to unsigned, then we
6724 : must avoid building ABS_EXPR itself as unsigned. */
6725 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6726 : {
6727 0 : tree cstype = (*signed_type_for) (ctype);
6728 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6729 : {
6730 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6731 0 : return fold_convert (ctype, t1);
6732 : }
6733 : break;
6734 : }
6735 : /* If the constant is negative, we cannot simplify this. */
6736 189 : if (tree_int_cst_sgn (c) == -1)
6737 : break;
6738 : /* FALLTHROUGH */
6739 46965 : case NEGATE_EXPR:
6740 : /* For division and modulus, type can't be unsigned, as e.g.
6741 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6742 : For signed types, even with wrapping overflow, this is fine. */
6743 46965 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6744 : break;
6745 45217 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6746 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6747 : break;
6748 :
6749 1525 : case MIN_EXPR: case MAX_EXPR:
6750 : /* If widening the type changes the signedness, then we can't perform
6751 : this optimization as that changes the result. */
6752 1525 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6753 : break;
6754 :
6755 : /* Punt for multiplication altogether.
6756 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6757 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6758 : 0U, the latter is 2U.
6759 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6760 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6761 : well defined 0, the latter invokes UB.
6762 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6763 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6764 : well defined 25, the latter invokes UB. */
6765 1525 : if (code == MULT_EXPR)
6766 : break;
6767 : /* For division/modulo, punt on c being -1 for MAX, as
6768 : MAX (INT_MIN, 0) / -1 is not equivalent to
6769 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6770 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6771 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6772 : transformation won't make it worse. */
6773 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6774 : break;
6775 :
6776 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6777 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6778 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6779 : {
6780 0 : if (tree_int_cst_sgn (c) < 0)
6781 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6782 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6783 : fold_convert (ctype, t2));
6784 : }
6785 : break;
6786 :
6787 1299 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6788 : /* If the second operand is constant, this is a multiplication
6789 : or floor division, by a power of two, so we can treat it that
6790 : way unless the multiplier or divisor overflows. Signed
6791 : left-shift overflow is implementation-defined rather than
6792 : undefined in C90, so do not convert signed left shift into
6793 : multiplication. */
6794 1299 : if (TREE_CODE (op1) == INTEGER_CST
6795 1275 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6796 : /* const_binop may not detect overflow correctly,
6797 : so check for it explicitly here. */
6798 1157 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6799 1308 : wi::to_wide (op1))
6800 1148 : && (t1 = fold_convert (ctype,
6801 : const_binop (LSHIFT_EXPR, size_one_node,
6802 : op1))) != 0
6803 2447 : && !TREE_OVERFLOW (t1))
6804 2094 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6805 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6806 : ctype,
6807 : fold_convert (ctype, op0),
6808 : t1),
6809 1148 : c, code, wide_type);
6810 : break;
6811 :
6812 8390898 : case PLUS_EXPR: case MINUS_EXPR:
6813 : /* See if we can eliminate the operation on both sides. If we can, we
6814 : can return a new PLUS or MINUS. If we can't, the only remaining
6815 : cases where we can do anything are if the second operand is a
6816 : constant. */
6817 8390898 : t1 = extract_muldiv (op0, c, code, wide_type);
6818 8390898 : t2 = extract_muldiv (op1, c, code, wide_type);
6819 800843 : if (t1 != 0 && t2 != 0
6820 273461 : && TYPE_OVERFLOW_WRAPS (ctype)
6821 8654222 : && (code == MULT_EXPR
6822 : /* If not multiplication, we can only do this if both operands
6823 : are divisible by c. */
6824 0 : || (multiple_of_p (ctype, op0, c)
6825 0 : && multiple_of_p (ctype, op1, c))))
6826 : {
6827 263324 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6828 : fold_convert (ctype, t2));
6829 : }
6830 :
6831 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6832 : This simplifies the logic below. */
6833 8127574 : if (tcode == MINUS_EXPR)
6834 : {
6835 2204350 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6836 : /* If OP1 was not easily negatable, the constant may be OP0. */
6837 2204350 : if (TREE_CODE (op0) == INTEGER_CST)
6838 : {
6839 381901 : std::swap (op0, op1);
6840 381901 : std::swap (t1, t2);
6841 : }
6842 : }
6843 :
6844 8127574 : if (TREE_CODE (op1) != INTEGER_CST)
6845 : break;
6846 :
6847 : /* If either OP1 or C are negative, this optimization is not safe for
6848 : some of the division and remainder types while for others we need
6849 : to change the code. */
6850 3756190 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6851 : {
6852 193279 : if (code == CEIL_DIV_EXPR)
6853 : code = FLOOR_DIV_EXPR;
6854 193277 : else if (code == FLOOR_DIV_EXPR)
6855 : code = CEIL_DIV_EXPR;
6856 192840 : else if (code != MULT_EXPR
6857 192840 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6858 : break;
6859 : }
6860 :
6861 : /* If it's a multiply or a division/modulus operation of a multiple
6862 : of our constant, do the operation and verify it doesn't overflow. */
6863 3750648 : if (code == MULT_EXPR
6864 4988413 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6865 1237765 : TYPE_SIGN (type)))
6866 : {
6867 2938954 : op1 = const_binop (code, fold_convert (ctype, op1),
6868 : fold_convert (ctype, c));
6869 : /* We allow the constant to overflow with wrapping semantics. */
6870 2938954 : if (op1 == 0
6871 2938954 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6872 : break;
6873 : }
6874 : else
6875 : break;
6876 :
6877 : /* If we have an unsigned type, we cannot widen the operation since it
6878 : will change the result if the original computation overflowed. */
6879 2935433 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6880 : break;
6881 :
6882 : /* The last case is if we are a multiply. In that case, we can
6883 : apply the distributive law to commute the multiply and addition
6884 : if the multiplication of the constants doesn't overflow
6885 : and overflow is defined. With undefined overflow
6886 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6887 : But fold_plusminus_mult_expr would factor back any power-of-two
6888 : value so do not distribute in the first place in this case. */
6889 2935433 : if (code == MULT_EXPR
6890 2510121 : && TYPE_OVERFLOW_WRAPS (ctype)
6891 5083722 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6892 612694 : return fold_build2 (tcode, ctype,
6893 : fold_build2 (code, ctype,
6894 : fold_convert (ctype, op0),
6895 : fold_convert (ctype, c)),
6896 : op1);
6897 :
6898 : break;
6899 :
6900 2323291 : case MULT_EXPR:
6901 : /* We have a special case here if we are doing something like
6902 : (C * 8) % 4 since we know that's zero. */
6903 2323291 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6904 2323291 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6905 : /* If the multiplication can overflow we cannot optimize this. */
6906 11168 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6907 402 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6908 2334459 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6909 357 : TYPE_SIGN (type)))
6910 : {
6911 8 : return omit_one_operand (type, integer_zero_node, op0);
6912 : }
6913 :
6914 : /* ... fall through ... */
6915 :
6916 2606013 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6917 2606013 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6918 : /* If we can extract our operation from the LHS, do so and return a
6919 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6920 : do something only if the second operand is a constant. */
6921 2606013 : if (same_p
6922 2184552 : && TYPE_OVERFLOW_WRAPS (ctype)
6923 4611729 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6924 67617 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6925 : fold_convert (ctype, op1));
6926 2538396 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6927 2107554 : && TYPE_OVERFLOW_WRAPS (ctype)
6928 4467162 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6929 894496 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6930 : fold_convert (ctype, t1));
6931 1643900 : else if (TREE_CODE (op1) != INTEGER_CST)
6932 : return 0;
6933 :
6934 : /* If these are the same operation types, we can associate them
6935 : assuming no overflow. */
6936 621522 : if (tcode == code)
6937 : {
6938 200660 : bool overflow_p = false;
6939 200660 : wi::overflow_type overflow_mul;
6940 200660 : signop sign = TYPE_SIGN (ctype);
6941 200660 : unsigned prec = TYPE_PRECISION (ctype);
6942 401320 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6943 200660 : wi::to_wide (c, prec),
6944 200660 : sign, &overflow_mul);
6945 200660 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6946 200660 : if (overflow_mul
6947 1549 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6948 : overflow_p = true;
6949 200593 : if (!overflow_p)
6950 200593 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6951 : wide_int_to_tree (ctype, mul));
6952 200660 : }
6953 :
6954 : /* If these operations "cancel" each other, we have the main
6955 : optimizations of this pass, which occur when either constant is a
6956 : multiple of the other, in which case we replace this with either an
6957 : operation or CODE or TCODE.
6958 :
6959 : If we have an unsigned type, we cannot do this since it will change
6960 : the result if the original computation overflowed. */
6961 420929 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6962 100614 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6963 521500 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6964 100266 : || (tcode == MULT_EXPR
6965 100266 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6966 878 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6967 874 : && code != MULT_EXPR)))
6968 : {
6969 1173 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6970 1173 : TYPE_SIGN (type)))
6971 : {
6972 124 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6973 : fold_convert (ctype,
6974 : const_binop (TRUNC_DIV_EXPR,
6975 : op1, c)));
6976 : }
6977 1049 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6978 1049 : TYPE_SIGN (type)))
6979 : {
6980 305 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6981 : fold_convert (ctype,
6982 : const_binop (TRUNC_DIV_EXPR,
6983 : c, op1)));
6984 : }
6985 : }
6986 : break;
6987 :
6988 : default:
6989 : break;
6990 : }
6991 :
6992 : return 0;
6993 : }
6994 :
6995 : /* Return a node which has the indicated constant VALUE (either 0 or
6996 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6997 : and is of the indicated TYPE. */
6998 :
6999 : tree
7000 120829574 : constant_boolean_node (bool value, tree type)
7001 : {
7002 120829574 : if (type == integer_type_node)
7003 20659500 : return value ? integer_one_node : integer_zero_node;
7004 100170074 : else if (type == boolean_type_node)
7005 95728018 : return value ? boolean_true_node : boolean_false_node;
7006 4442056 : else if (VECTOR_TYPE_P (type))
7007 1951 : return build_vector_from_val (type,
7008 1951 : build_int_cst (TREE_TYPE (type),
7009 2692 : value ? -1 : 0));
7010 : else
7011 4440105 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
7012 : }
7013 :
7014 :
7015 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7016 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7017 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7018 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7019 : COND is the first argument to CODE; otherwise (as in the example
7020 : given here), it is the second argument. TYPE is the type of the
7021 : original expression. Return NULL_TREE if no simplification is
7022 : possible. */
7023 :
7024 : static tree
7025 977587 : fold_binary_op_with_conditional_arg (location_t loc,
7026 : enum tree_code code,
7027 : tree type, tree op0, tree op1,
7028 : tree cond, tree arg, int cond_first_p)
7029 : {
7030 977587 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7031 977587 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7032 977587 : tree test, true_value, false_value;
7033 977587 : tree lhs = NULL_TREE;
7034 977587 : tree rhs = NULL_TREE;
7035 977587 : enum tree_code cond_code = COND_EXPR;
7036 :
7037 : /* Do not move possibly trapping operations into the conditional as this
7038 : pessimizes code and causes gimplification issues when applied late. */
7039 997321 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7040 977587 : ANY_INTEGRAL_TYPE_P (type)
7041 786413 : && TYPE_OVERFLOW_TRAPS (type), op1))
7042 : return NULL_TREE;
7043 :
7044 957677 : if (TREE_CODE (cond) == COND_EXPR
7045 362213 : || TREE_CODE (cond) == VEC_COND_EXPR)
7046 : {
7047 : /* Do not sink an operation with a scalar result, such as an
7048 : equality comparison of two vectors, into the arms of a
7049 : VEC_COND_EXPR. */
7050 599304 : if (TREE_CODE (cond) == VEC_COND_EXPR && !VECTOR_TYPE_P (type))
7051 : return NULL_TREE;
7052 599291 : test = TREE_OPERAND (cond, 0);
7053 599291 : true_value = TREE_OPERAND (cond, 1);
7054 599291 : false_value = TREE_OPERAND (cond, 2);
7055 : /* If this operand throws an expression, then it does not make
7056 : sense to try to perform a logical or arithmetic operation
7057 : involving it. */
7058 599291 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7059 7463 : lhs = true_value;
7060 599291 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7061 6 : rhs = false_value;
7062 : }
7063 358373 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7064 358259 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7065 : {
7066 356311 : tree testtype = TREE_TYPE (cond);
7067 356311 : test = cond;
7068 356311 : true_value = constant_boolean_node (true, testtype);
7069 356311 : false_value = constant_boolean_node (false, testtype);
7070 : }
7071 : else
7072 : /* Detect the case of mixing vector and scalar types - bail out. */
7073 : return NULL_TREE;
7074 :
7075 955602 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7076 3941 : cond_code = VEC_COND_EXPR;
7077 :
7078 : /* This transformation is only worthwhile if we don't have to wrap ARG
7079 : in a SAVE_EXPR and the operation can be simplified without recursing
7080 : on at least one of the branches once its pushed inside the COND_EXPR. */
7081 955602 : if (!TREE_CONSTANT (arg)
7082 955602 : && (TREE_SIDE_EFFECTS (arg)
7083 459951 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7084 455010 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7085 : return NULL_TREE;
7086 :
7087 512208 : arg = fold_convert_loc (loc, arg_type, arg);
7088 512208 : if (lhs == 0)
7089 : {
7090 506177 : true_value = fold_convert_loc (loc, cond_type, true_value);
7091 506177 : if (cond_first_p)
7092 500532 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7093 : else
7094 5645 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7095 : }
7096 512208 : if (rhs == 0)
7097 : {
7098 512202 : false_value = fold_convert_loc (loc, cond_type, false_value);
7099 512202 : if (cond_first_p)
7100 505996 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7101 : else
7102 6206 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7103 : }
7104 :
7105 : /* Check that we have simplified at least one of the branches. */
7106 512208 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7107 : return NULL_TREE;
7108 :
7109 491257 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7110 : }
7111 :
7112 :
7113 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7114 :
7115 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7116 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7117 : if ARG - ZERO_ARG is the same as X.
7118 :
7119 : If ARG is NULL, check for any value of type TYPE.
7120 :
7121 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7122 : and finite. The problematic cases are when X is zero, and its mode
7123 : has signed zeros. In the case of rounding towards -infinity,
7124 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7125 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7126 :
7127 : bool
7128 640206 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7129 : const_tree zero_arg, int negate)
7130 : {
7131 640206 : if (!real_zerop (zero_arg))
7132 : return false;
7133 :
7134 : /* Don't allow the fold with -fsignaling-nans. */
7135 639405 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7136 : return false;
7137 :
7138 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7139 636242 : if (!HONOR_SIGNED_ZEROS (type))
7140 : return true;
7141 :
7142 : /* There is no case that is safe for all rounding modes. */
7143 618897 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7144 : return false;
7145 :
7146 : /* In a vector or complex, we would need to check the sign of all zeros. */
7147 617534 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7148 2939 : zero_arg = uniform_vector_p (zero_arg);
7149 617534 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7150 : return false;
7151 :
7152 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7153 616186 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7154 222 : negate = !negate;
7155 :
7156 : /* The mode has signed zeros, and we have to honor their sign.
7157 : In this situation, there are only two cases we can return true for.
7158 : (i) X - 0 is the same as X with default rounding.
7159 : (ii) X + 0 is X when X can't possibly be -0.0. */
7160 616186 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7161 : }
7162 :
7163 : /* Subroutine of match.pd that determines if it is safe to optimize
7164 : a floating point comparison of an integer value, known to be between
7165 : LO and HI, using comparison operator CMP, against the real constant
7166 : R in floating point type FMT, as the same integer comparison against
7167 : the integer constant I, with sign ISIGN.
7168 :
7169 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7170 : because the floating point representations of the neighboring integers
7171 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7172 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7173 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7174 : due to truncation/rounding.
7175 : */
7176 : bool
7177 21532 : fold_cmp_float_cst_p (const wide_int &lo, const wide_int &hi,
7178 : enum tree_code cmp,
7179 : const REAL_VALUE_TYPE *r, format_helper fmt,
7180 : const wide_int &i, signop isign)
7181 : {
7182 21532 : REAL_VALUE_TYPE raw;
7183 21532 : REAL_VALUE_TYPE rnd;
7184 21532 : bool check_im1 = true;
7185 21532 : bool check_ip1 = true;
7186 :
7187 21532 : switch (cmp)
7188 : {
7189 : case EQ_EXPR:
7190 : case NE_EXPR:
7191 : /* Check both i-1 and i+1. */
7192 : break;
7193 :
7194 19767 : case LT_EXPR:
7195 19767 : case GE_EXPR:
7196 : /* Only check i-1. */
7197 19767 : check_ip1 = false;
7198 19767 : break;
7199 :
7200 442 : case LE_EXPR:
7201 442 : case GT_EXPR:
7202 : /* Only check i+1. */
7203 442 : check_im1 = false;
7204 442 : break;
7205 :
7206 : default:
7207 : return false;
7208 : }
7209 :
7210 21532 : if (flag_rounding_math && i != 0)
7211 : {
7212 602 : real_from_integer (&raw, VOIDmode, i, isign);
7213 602 : if (!real_identical (r, &raw))
7214 : return false;
7215 : }
7216 :
7217 21532 : if (check_im1 && wi::gt_p (i, lo, isign))
7218 : {
7219 1472 : if (flag_rounding_math)
7220 : {
7221 516 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7222 516 : real_convert (&rnd, fmt, &raw);
7223 516 : if (!real_identical (&raw, &rnd))
7224 : return false;
7225 : }
7226 : else
7227 956 : real_from_integer (&rnd, fmt, i - 1, isign);
7228 956 : if (real_identical (r, &rnd))
7229 : return false;
7230 : }
7231 :
7232 20672 : if (check_ip1 && wi::lt_p (i, hi, isign))
7233 : {
7234 969 : if (flag_rounding_math)
7235 : {
7236 86 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7237 86 : real_convert (&rnd, fmt, &raw);
7238 86 : if (!real_identical (&raw, &rnd))
7239 : return false;
7240 : }
7241 : else
7242 883 : real_from_integer (&rnd, fmt, i + 1, isign);
7243 883 : if (real_identical (r, &rnd))
7244 0 : return false;
7245 : }
7246 :
7247 : return true;
7248 : }
7249 :
7250 : /* Subroutine of match.pd that optimizes comparisons of a division by
7251 : a nonzero integer constant against an integer constant, i.e.
7252 : X/C1 op C2.
7253 :
7254 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7255 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7256 :
7257 : enum tree_code
7258 1782338 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7259 : tree *hi, bool *neg_overflow)
7260 : {
7261 1782338 : tree prod, tmp, type = TREE_TYPE (c1);
7262 1782338 : signop sign = TYPE_SIGN (type);
7263 1782338 : wi::overflow_type overflow;
7264 :
7265 : /* We have to do this the hard way to detect unsigned overflow.
7266 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7267 1782338 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7268 1782338 : prod = force_fit_type (type, val, -1, overflow);
7269 1782338 : *neg_overflow = false;
7270 :
7271 1782338 : if (sign == UNSIGNED)
7272 : {
7273 1753504 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7274 1753504 : *lo = prod;
7275 :
7276 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7277 1753504 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7278 1753504 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7279 : }
7280 28834 : else if (tree_int_cst_sgn (c1) >= 0)
7281 : {
7282 27437 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7283 27437 : switch (tree_int_cst_sgn (c2))
7284 : {
7285 4914 : case -1:
7286 4914 : *neg_overflow = true;
7287 4914 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7288 4914 : *hi = prod;
7289 4914 : break;
7290 :
7291 15136 : case 0:
7292 15136 : *lo = fold_negate_const (tmp, type);
7293 15136 : *hi = tmp;
7294 15136 : break;
7295 :
7296 7387 : case 1:
7297 7387 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7298 7387 : *lo = prod;
7299 7387 : break;
7300 :
7301 0 : default:
7302 0 : gcc_unreachable ();
7303 : }
7304 : }
7305 : else
7306 : {
7307 : /* A negative divisor reverses the relational operators. */
7308 1397 : code = swap_tree_comparison (code);
7309 :
7310 1397 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7311 1397 : switch (tree_int_cst_sgn (c2))
7312 : {
7313 132 : case -1:
7314 132 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7315 132 : *lo = prod;
7316 132 : break;
7317 :
7318 167 : case 0:
7319 167 : *hi = fold_negate_const (tmp, type);
7320 167 : *lo = tmp;
7321 167 : break;
7322 :
7323 1098 : case 1:
7324 1098 : *neg_overflow = true;
7325 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7326 1098 : *hi = prod;
7327 1098 : break;
7328 :
7329 0 : default:
7330 0 : gcc_unreachable ();
7331 : }
7332 : }
7333 :
7334 1782338 : if (code != EQ_EXPR && code != NE_EXPR)
7335 : return code;
7336 :
7337 15901 : if (TREE_OVERFLOW (*lo)
7338 15901 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7339 738 : *lo = NULL_TREE;
7340 15901 : if (TREE_OVERFLOW (*hi)
7341 15901 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7342 95 : *hi = NULL_TREE;
7343 :
7344 : return code;
7345 1782338 : }
7346 :
7347 : /* Test whether it is preferable to swap two operands, ARG0 and
7348 : ARG1, for example because ARG0 is an integer constant and ARG1
7349 : isn't. */
7350 :
7351 : bool
7352 1640105418 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7353 : {
7354 1640105418 : if (CONSTANT_CLASS_P (arg1))
7355 : return false;
7356 536107416 : if (CONSTANT_CLASS_P (arg0))
7357 : return true;
7358 :
7359 497434579 : STRIP_NOPS (arg0);
7360 497434579 : STRIP_NOPS (arg1);
7361 :
7362 497434579 : if (TREE_CONSTANT (arg1))
7363 : return false;
7364 479399085 : if (TREE_CONSTANT (arg0))
7365 : return true;
7366 :
7367 : /* Put addresses in arg1. */
7368 478624111 : if (TREE_CODE (arg1) == ADDR_EXPR)
7369 : return false;
7370 459772682 : if (TREE_CODE (arg0) == ADDR_EXPR)
7371 : return true;
7372 :
7373 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7374 : for commutative and comparison operators. Ensuring a canonical
7375 : form allows the optimizers to find additional redundancies without
7376 : having to explicitly check for both orderings. */
7377 459377570 : if (TREE_CODE (arg0) == SSA_NAME
7378 348564500 : && TREE_CODE (arg1) == SSA_NAME
7379 801895320 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7380 : return true;
7381 :
7382 : /* Put SSA_NAMEs last. */
7383 436235330 : if (TREE_CODE (arg1) == SSA_NAME)
7384 : return false;
7385 101492329 : if (TREE_CODE (arg0) == SSA_NAME)
7386 : return true;
7387 :
7388 : /* Put variables last. */
7389 95445579 : if (DECL_P (arg1))
7390 : return false;
7391 51753182 : if (DECL_P (arg0))
7392 6013931 : return true;
7393 :
7394 : return false;
7395 : }
7396 :
7397 :
7398 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7399 : means A >= Y && A != MAX, but in this case we know that
7400 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7401 :
7402 : static tree
7403 24380697 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7404 : {
7405 24380697 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7406 :
7407 24380697 : if (TREE_CODE (bound) == LT_EXPR)
7408 5207582 : a = TREE_OPERAND (bound, 0);
7409 19173115 : else if (TREE_CODE (bound) == GT_EXPR)
7410 2843528 : a = TREE_OPERAND (bound, 1);
7411 : else
7412 : return NULL_TREE;
7413 :
7414 8051110 : typea = TREE_TYPE (a);
7415 8051110 : if (!INTEGRAL_TYPE_P (typea)
7416 483750 : && !POINTER_TYPE_P (typea))
7417 : return NULL_TREE;
7418 :
7419 7872781 : if (TREE_CODE (ineq) == LT_EXPR)
7420 : {
7421 1560817 : a1 = TREE_OPERAND (ineq, 1);
7422 1560817 : y = TREE_OPERAND (ineq, 0);
7423 : }
7424 6311964 : else if (TREE_CODE (ineq) == GT_EXPR)
7425 : {
7426 1221546 : a1 = TREE_OPERAND (ineq, 0);
7427 1221546 : y = TREE_OPERAND (ineq, 1);
7428 : }
7429 : else
7430 : return NULL_TREE;
7431 :
7432 2782363 : if (TREE_TYPE (a1) != typea)
7433 : return NULL_TREE;
7434 :
7435 1959254 : if (POINTER_TYPE_P (typea))
7436 : {
7437 : /* Convert the pointer types into integer before taking the difference. */
7438 11646 : tree ta = fold_convert_loc (loc, ssizetype, a);
7439 11646 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7440 11646 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7441 : }
7442 : else
7443 1947608 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7444 :
7445 1959254 : if (!diff || !integer_onep (diff))
7446 : return NULL_TREE;
7447 :
7448 8940 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7449 : }
7450 :
7451 : /* Fold a sum or difference of at least one multiplication.
7452 : Returns the folded tree or NULL if no simplification could be made. */
7453 :
7454 : static tree
7455 8928504 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7456 : tree arg0, tree arg1)
7457 : {
7458 8928504 : tree arg00, arg01, arg10, arg11;
7459 8928504 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7460 :
7461 : /* (A * C) +- (B * C) -> (A+-B) * C.
7462 : (A * C) +- A -> A * (C+-1).
7463 : We are most concerned about the case where C is a constant,
7464 : but other combinations show up during loop reduction. Since
7465 : it is not difficult, try all four possibilities. */
7466 :
7467 8928504 : if (TREE_CODE (arg0) == MULT_EXPR)
7468 : {
7469 7956239 : arg00 = TREE_OPERAND (arg0, 0);
7470 7956239 : arg01 = TREE_OPERAND (arg0, 1);
7471 : }
7472 972265 : else if (TREE_CODE (arg0) == INTEGER_CST)
7473 : {
7474 76747 : arg00 = build_one_cst (type);
7475 76747 : arg01 = arg0;
7476 : }
7477 : else
7478 : {
7479 : /* We cannot generate constant 1 for fract. */
7480 895518 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7481 : return NULL_TREE;
7482 895518 : arg00 = arg0;
7483 895518 : arg01 = build_one_cst (type);
7484 : }
7485 8928504 : if (TREE_CODE (arg1) == MULT_EXPR)
7486 : {
7487 2315541 : arg10 = TREE_OPERAND (arg1, 0);
7488 2315541 : arg11 = TREE_OPERAND (arg1, 1);
7489 : }
7490 6612963 : else if (TREE_CODE (arg1) == INTEGER_CST)
7491 : {
7492 3320202 : arg10 = build_one_cst (type);
7493 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7494 : the purpose of this canonicalization. */
7495 6408144 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7496 235548 : && negate_expr_p (arg1)
7497 3552462 : && code == PLUS_EXPR)
7498 : {
7499 232260 : arg11 = negate_expr (arg1);
7500 232260 : code = MINUS_EXPR;
7501 : }
7502 : else
7503 : arg11 = arg1;
7504 : }
7505 : else
7506 : {
7507 : /* We cannot generate constant 1 for fract. */
7508 3292761 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7509 : return NULL_TREE;
7510 3292761 : arg10 = arg1;
7511 3292761 : arg11 = build_one_cst (type);
7512 : }
7513 8928504 : same = NULL_TREE;
7514 :
7515 : /* Prefer factoring a common non-constant. */
7516 8928504 : if (operand_equal_p (arg00, arg10, 0))
7517 : same = arg00, alt0 = arg01, alt1 = arg11;
7518 8924471 : else if (operand_equal_p (arg01, arg11, 0))
7519 : same = arg01, alt0 = arg00, alt1 = arg10;
7520 8818050 : else if (operand_equal_p (arg00, arg11, 0))
7521 : same = arg00, alt0 = arg01, alt1 = arg10;
7522 8817988 : else if (operand_equal_p (arg01, arg10, 0))
7523 : same = arg01, alt0 = arg00, alt1 = arg11;
7524 :
7525 : /* No identical multiplicands; see if we can find a common
7526 : power-of-two factor in non-power-of-two multiplies. This
7527 : can help in multi-dimensional array access. */
7528 8812631 : else if (TREE_CODE (arg01) == INTEGER_CST
7529 7708653 : && TREE_CODE (arg11) == INTEGER_CST)
7530 : {
7531 7484018 : wide_int int01 = wi::to_wide (arg01);
7532 7484018 : wide_int int11 = wi::to_wide (arg11);
7533 7484018 : bool swap = false;
7534 7484018 : tree maybe_same;
7535 :
7536 : /* Move min of absolute values to int11. */
7537 7484020 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7538 : {
7539 3373219 : std::swap (int01, int11);
7540 3373219 : std::swap (arg00, arg10);
7541 3373219 : maybe_same = arg01;
7542 3373219 : swap = true;
7543 : }
7544 : else
7545 : maybe_same = arg11;
7546 :
7547 7484018 : wide_int factor = wi::abs (int11);
7548 7484018 : if (wi::gtu_p (factor, 1u)
7549 3975398 : && wi::exact_log2 (factor) != -1
7550 11493112 : && (int01 & (factor - 1)) == 0
7551 : /* The remainder should not be a constant, otherwise we
7552 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7553 : increased the number of multiplications necessary. */
7554 8870659 : && TREE_CODE (arg10) != INTEGER_CST)
7555 : {
7556 2338006 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7557 1169003 : wide_int_to_tree (TREE_TYPE (arg00),
7558 1169003 : wi::sdiv_trunc (int01,
7559 : int11)));
7560 1169003 : alt1 = arg10;
7561 1169003 : same = maybe_same;
7562 1169003 : if (swap)
7563 1044857 : std::swap (alt0, alt1);
7564 : }
7565 7484020 : }
7566 :
7567 7599891 : if (!same)
7568 : return NULL_TREE;
7569 :
7570 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7571 1284876 : || TYPE_OVERFLOW_WRAPS (type)
7572 : /* We are neither factoring zero nor minus one. */
7573 1405353 : || TREE_CODE (same) == INTEGER_CST)
7574 1272285 : return fold_build2_loc (loc, MULT_EXPR, type,
7575 : fold_build2_loc (loc, code, type,
7576 : fold_convert_loc (loc, type, alt0),
7577 : fold_convert_loc (loc, type, alt1)),
7578 1272285 : fold_convert_loc (loc, type, same));
7579 :
7580 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7581 : same may be minus one and thus the multiplication may overflow. Perform
7582 : the sum operation in an unsigned type. */
7583 12591 : tree utype = unsigned_type_for (type);
7584 12591 : tree tem = fold_build2_loc (loc, code, utype,
7585 : fold_convert_loc (loc, utype, alt0),
7586 : fold_convert_loc (loc, utype, alt1));
7587 : /* If the sum evaluated to a constant that is not -INF the multiplication
7588 : cannot overflow. */
7589 25182 : if (TREE_CODE (tem) == INTEGER_CST
7590 16029 : && (wi::to_wide (tem)
7591 19467 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7592 3425 : return fold_build2_loc (loc, MULT_EXPR, type,
7593 3425 : fold_convert (type, tem), same);
7594 :
7595 : /* Do not resort to unsigned multiplication because
7596 : we lose the no-overflow property of the expression. */
7597 : return NULL_TREE;
7598 : }
7599 :
7600 :
7601 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7602 : into the buffer PTR of length LEN bytes.
7603 : Return the number of bytes placed in the buffer, or zero
7604 : upon failure. */
7605 :
7606 : int
7607 55687780 : native_encode_wide_int (tree type, const wide_int_ref &val,
7608 : unsigned char *ptr, int len, int off)
7609 : {
7610 55687780 : int total_bytes;
7611 55687780 : if (BITINT_TYPE_P (type))
7612 : {
7613 19210 : struct bitint_info info;
7614 19210 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7615 19210 : gcc_assert (ok);
7616 19210 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7617 19210 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7618 : {
7619 17941 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7620 : /* More work is needed when adding _BitInt support to PDP endian
7621 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7622 : match target endianity here. */
7623 17941 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7624 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7625 : || (GET_MODE_SIZE (limb_mode)
7626 : >= UNITS_PER_WORD)));
7627 : }
7628 : else
7629 2538 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7630 : }
7631 : else
7632 111337140 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7633 55687780 : int byte, offset, word, words;
7634 55687780 : unsigned char value;
7635 :
7636 55687780 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7637 : return 0;
7638 55687206 : if (off == -1)
7639 53615746 : off = 0;
7640 :
7641 55687206 : if (ptr == NULL)
7642 : /* Dry run. */
7643 2877944 : return MIN (len, total_bytes - off);
7644 :
7645 : words = total_bytes / UNITS_PER_WORD;
7646 :
7647 257351287 : for (byte = 0; byte < total_bytes; byte++)
7648 : {
7649 204542025 : int bitpos = byte * BITS_PER_UNIT;
7650 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7651 : number of bytes. */
7652 204542025 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7653 :
7654 204542025 : if (total_bytes > UNITS_PER_WORD)
7655 : {
7656 204542025 : word = byte / UNITS_PER_WORD;
7657 204542025 : if (WORDS_BIG_ENDIAN)
7658 : word = (words - 1) - word;
7659 204542025 : offset = word * UNITS_PER_WORD;
7660 204542025 : if (BYTES_BIG_ENDIAN)
7661 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7662 : else
7663 204542025 : offset += byte % UNITS_PER_WORD;
7664 : }
7665 : else
7666 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7667 204542025 : if (offset >= off && offset - off < len)
7668 203104072 : ptr[offset - off] = value;
7669 : }
7670 52809262 : return MIN (len, total_bytes - off);
7671 : }
7672 :
7673 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7674 : specified by EXPR into the buffer PTR of length LEN bytes.
7675 : Return the number of bytes placed in the buffer, or zero
7676 : upon failure. */
7677 :
7678 : static int
7679 55687780 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7680 : {
7681 55687780 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7682 55687780 : ptr, len, off);
7683 : }
7684 :
7685 :
7686 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7687 : specified by EXPR into the buffer PTR of length LEN bytes.
7688 : Return the number of bytes placed in the buffer, or zero
7689 : upon failure. */
7690 :
7691 : static int
7692 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7693 : {
7694 0 : tree type = TREE_TYPE (expr);
7695 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7696 0 : int total_bytes = GET_MODE_SIZE (mode);
7697 0 : FIXED_VALUE_TYPE value;
7698 0 : tree i_value, i_type;
7699 :
7700 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7701 : return 0;
7702 :
7703 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7704 :
7705 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7706 : return 0;
7707 :
7708 0 : value = TREE_FIXED_CST (expr);
7709 0 : i_value = double_int_to_tree (i_type, value.data);
7710 :
7711 0 : return native_encode_int (i_value, ptr, len, off);
7712 : }
7713 :
7714 :
7715 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7716 : specified by EXPR into the buffer PTR of length LEN bytes.
7717 : Return the number of bytes placed in the buffer, or zero
7718 : upon failure. */
7719 :
7720 : int
7721 882969 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7722 : unsigned char *ptr, int len, int off)
7723 : {
7724 882969 : int total_bytes = GET_MODE_SIZE (mode);
7725 882969 : int byte, offset, word, words, bitpos;
7726 882969 : unsigned char value;
7727 :
7728 : /* There are always 32 bits in each long, no matter the size of
7729 : the hosts long. We handle floating point representations with
7730 : up to 192 bits. */
7731 882969 : long tmp[6];
7732 :
7733 882969 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7734 : return 0;
7735 879533 : if (off == -1)
7736 751535 : off = 0;
7737 :
7738 879533 : if (ptr == NULL)
7739 : /* Dry run. */
7740 142126 : return MIN (len, total_bytes - off);
7741 :
7742 737407 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7743 :
7744 737407 : real_to_target (tmp, val, mode);
7745 :
7746 7001925 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7747 6264518 : bitpos += BITS_PER_UNIT)
7748 : {
7749 6264518 : byte = (bitpos / BITS_PER_UNIT) & 3;
7750 6264518 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7751 :
7752 6264518 : if (UNITS_PER_WORD < 4)
7753 : {
7754 : word = byte / UNITS_PER_WORD;
7755 : if (WORDS_BIG_ENDIAN)
7756 : word = (words - 1) - word;
7757 : offset = word * UNITS_PER_WORD;
7758 : if (BYTES_BIG_ENDIAN)
7759 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7760 : else
7761 : offset += byte % UNITS_PER_WORD;
7762 : }
7763 : else
7764 : {
7765 6264518 : offset = byte;
7766 6264518 : if (BYTES_BIG_ENDIAN)
7767 : {
7768 : /* Reverse bytes within each long, or within the entire float
7769 : if it's smaller than a long (for HFmode). */
7770 : offset = MIN (3, total_bytes - 1) - offset;
7771 : gcc_assert (offset >= 0);
7772 : }
7773 : }
7774 6264518 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7775 6264518 : if (offset >= off
7776 6261278 : && offset - off < len)
7777 6232986 : ptr[offset - off] = value;
7778 : }
7779 737407 : return MIN (len, total_bytes - off);
7780 : }
7781 :
7782 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7783 : specified by EXPR into the buffer PTR of length LEN bytes.
7784 : Return the number of bytes placed in the buffer, or zero
7785 : upon failure. */
7786 :
7787 : static int
7788 12641 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7789 : {
7790 12641 : int rsize, isize;
7791 12641 : tree part;
7792 :
7793 12641 : part = TREE_REALPART (expr);
7794 12641 : rsize = native_encode_expr (part, ptr, len, off);
7795 12641 : if (off == -1 && rsize == 0)
7796 : return 0;
7797 12641 : part = TREE_IMAGPART (expr);
7798 12641 : if (off != -1)
7799 26313 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7800 12641 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7801 : len - rsize, off);
7802 12641 : if (off == -1 && isize != rsize)
7803 : return 0;
7804 12641 : return rsize + isize;
7805 : }
7806 :
7807 : /* Like native_encode_vector, but only encode the first COUNT elements.
7808 : The other arguments are as for native_encode_vector. */
7809 :
7810 : static int
7811 1094450 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7812 : int off, unsigned HOST_WIDE_INT count)
7813 : {
7814 1094450 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7815 2188900 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7816 1095492 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7817 : {
7818 : /* This is the only case in which elements can be smaller than a byte.
7819 : Element 0 is always in the lsb of the containing byte. */
7820 964 : unsigned int elt_bits = TYPE_PRECISION (itype);
7821 964 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7822 964 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7823 : return 0;
7824 :
7825 964 : if (off == -1)
7826 964 : off = 0;
7827 :
7828 : /* Zero the buffer and then set bits later where necessary. */
7829 964 : int extract_bytes = MIN (len, total_bytes - off);
7830 964 : if (ptr)
7831 964 : memset (ptr, 0, extract_bytes);
7832 :
7833 964 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7834 964 : unsigned int first_elt = off * elts_per_byte;
7835 964 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7836 964 : unsigned int elt_mask = (1 << elt_bits) - 1;
7837 17645 : for (unsigned int i = 0; i < extract_elts; ++i)
7838 : {
7839 16681 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7840 16681 : if (TREE_CODE (elt) != INTEGER_CST)
7841 : return 0;
7842 :
7843 16681 : if (ptr && integer_nonzerop (elt))
7844 : {
7845 8488 : unsigned int bit = i * elt_bits;
7846 8488 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7847 : }
7848 : }
7849 : return extract_bytes;
7850 : }
7851 :
7852 1093486 : int offset = 0;
7853 1093486 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7854 4389343 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7855 : {
7856 3904995 : if (off >= size)
7857 : {
7858 25217 : off -= size;
7859 25217 : continue;
7860 : }
7861 3879778 : tree elem = VECTOR_CST_ELT (expr, i);
7862 3879778 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7863 : len - offset, off);
7864 3879778 : if ((off == -1 && res != size) || res == 0)
7865 : return 0;
7866 3879249 : offset += res;
7867 3879249 : if (offset >= len)
7868 608609 : return (off == -1 && i < count - 1) ? 0 : offset;
7869 3270640 : if (off != -1)
7870 460631 : off = 0;
7871 : }
7872 : return offset;
7873 : }
7874 :
7875 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7876 : specified by EXPR into the buffer PTR of length LEN bytes.
7877 : Return the number of bytes placed in the buffer, or zero
7878 : upon failure. */
7879 :
7880 : static int
7881 936566 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7882 : {
7883 936566 : unsigned HOST_WIDE_INT count;
7884 936566 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7885 : return 0;
7886 936566 : return native_encode_vector_part (expr, ptr, len, off, count);
7887 : }
7888 :
7889 :
7890 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7891 : specified by EXPR into the buffer PTR of length LEN bytes.
7892 : Return the number of bytes placed in the buffer, or zero
7893 : upon failure. */
7894 :
7895 : static int
7896 255133 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7897 : {
7898 255133 : tree type = TREE_TYPE (expr);
7899 :
7900 : /* Wide-char strings are encoded in target byte-order so native
7901 : encoding them is trivial. */
7902 255133 : if (BITS_PER_UNIT != CHAR_BIT
7903 255133 : || TREE_CODE (type) != ARRAY_TYPE
7904 255133 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7905 510266 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7906 : return 0;
7907 :
7908 255133 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7909 255133 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7910 : return 0;
7911 254260 : if (off == -1)
7912 65723 : off = 0;
7913 254260 : len = MIN (total_bytes - off, len);
7914 254260 : if (ptr == NULL)
7915 : /* Dry run. */;
7916 : else
7917 : {
7918 254260 : int written = 0;
7919 254260 : if (off < TREE_STRING_LENGTH (expr))
7920 : {
7921 253986 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7922 253986 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7923 : }
7924 254260 : memset (ptr + written, 0, len - written);
7925 : }
7926 : return len;
7927 : }
7928 :
7929 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7930 : specified by EXPR into the buffer PTR of length LEN bytes.
7931 : Return the number of bytes placed in the buffer, or zero
7932 : upon failure. */
7933 :
7934 : static int
7935 50689 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7936 : {
7937 : /* We are only concerned with zero-initialization constructors here. That's
7938 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7939 : deal with. For more general handling of constructors, there is
7940 : native_encode_initializer. */
7941 50689 : if (CONSTRUCTOR_NELTS (expr))
7942 : return 0;
7943 :
7944 : /* Wide-char strings are encoded in target byte-order so native
7945 : encoding them is trivial. */
7946 94708 : if (BITS_PER_UNIT != CHAR_BIT
7947 47354 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7948 : return 0;
7949 :
7950 47354 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7951 47354 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7952 : return 0;
7953 47354 : if (off == -1)
7954 0 : off = 0;
7955 47354 : len = MIN (total_bytes - off, len);
7956 47354 : if (ptr == NULL)
7957 : /* Dry run. */;
7958 : else
7959 47354 : memset (ptr, 0, len);
7960 : return len;
7961 : }
7962 :
7963 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7964 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7965 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7966 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7967 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7968 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7969 : Return the number of bytes placed in the buffer, or zero upon failure. */
7970 :
7971 : int
7972 72305530 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7973 : {
7974 : /* We don't support starting at negative offset and -1 is special. */
7975 72305530 : if (off < -1)
7976 : return 0;
7977 :
7978 72305518 : switch (TREE_CODE (expr))
7979 : {
7980 55685531 : case INTEGER_CST:
7981 55685531 : return native_encode_int (expr, ptr, len, off);
7982 :
7983 882969 : case REAL_CST:
7984 882969 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7985 1765938 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7986 :
7987 0 : case FIXED_CST:
7988 0 : return native_encode_fixed (expr, ptr, len, off);
7989 :
7990 12641 : case COMPLEX_CST:
7991 12641 : return native_encode_complex (expr, ptr, len, off);
7992 :
7993 936566 : case VECTOR_CST:
7994 936566 : return native_encode_vector (expr, ptr, len, off);
7995 :
7996 255133 : case STRING_CST:
7997 255133 : return native_encode_string (expr, ptr, len, off);
7998 :
7999 50689 : case CONSTRUCTOR:
8000 50689 : return native_encode_constructor (expr, ptr, len, off);
8001 :
8002 : default:
8003 : return 0;
8004 : }
8005 : }
8006 :
8007 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8008 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8009 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8010 : machine modes, we can't just use build_nonstandard_integer_type. */
8011 :
8012 : tree
8013 547 : find_bitfield_repr_type (int fieldsize, int len)
8014 : {
8015 547 : machine_mode mode;
8016 1075 : for (int pass = 0; pass < 2; pass++)
8017 : {
8018 811 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
8019 4561 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
8020 8066 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8021 7370 : && known_eq (GET_MODE_PRECISION (mode),
8022 : GET_MODE_BITSIZE (mode))
8023 11403 : && known_le (GET_MODE_SIZE (mode), len))
8024 : {
8025 283 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
8026 283 : if (ret && TYPE_MODE (ret) == mode)
8027 : return ret;
8028 : }
8029 : }
8030 :
8031 528 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
8032 264 : if (int_n_enabled_p[i]
8033 264 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
8034 264 : && int_n_trees[i].unsigned_type)
8035 : {
8036 264 : tree ret = int_n_trees[i].unsigned_type;
8037 264 : mode = TYPE_MODE (ret);
8038 528 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8039 528 : && known_eq (GET_MODE_PRECISION (mode),
8040 : GET_MODE_BITSIZE (mode))
8041 792 : && known_le (GET_MODE_SIZE (mode), len))
8042 : return ret;
8043 : }
8044 :
8045 : return NULL_TREE;
8046 : }
8047 :
8048 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8049 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8050 : to be non-NULL and OFF zero), then in addition to filling the
8051 : bytes pointed by PTR with the value also clear any bits pointed
8052 : by MASK that are known to be initialized, keep them as is for
8053 : e.g. uninitialized padding bits or uninitialized fields. */
8054 :
8055 : int
8056 47367275 : native_encode_initializer (tree init, unsigned char *ptr, int len,
8057 : int off, unsigned char *mask)
8058 : {
8059 47367275 : int r;
8060 :
8061 : /* We don't support starting at negative offset and -1 is special. */
8062 47367275 : if (off < -1 || init == NULL_TREE)
8063 : return 0;
8064 :
8065 47367275 : gcc_assert (mask == NULL || (off == 0 && ptr));
8066 :
8067 47367275 : STRIP_NOPS (init);
8068 47367275 : switch (TREE_CODE (init))
8069 : {
8070 0 : case VIEW_CONVERT_EXPR:
8071 0 : case NON_LVALUE_EXPR:
8072 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8073 0 : mask);
8074 45199487 : default:
8075 45199487 : r = native_encode_expr (init, ptr, len, off);
8076 45199487 : if (mask)
8077 7135 : memset (mask, 0, r);
8078 : return r;
8079 2167788 : case CONSTRUCTOR:
8080 2167788 : tree type = TREE_TYPE (init);
8081 2167788 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8082 2167788 : if (total_bytes < 0)
8083 : return 0;
8084 2167788 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
8085 : return 0;
8086 2167712 : int o = off == -1 ? 0 : off;
8087 2167712 : if (TREE_CODE (type) == ARRAY_TYPE)
8088 : {
8089 293925 : tree min_index;
8090 293925 : unsigned HOST_WIDE_INT cnt;
8091 293925 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8092 293925 : constructor_elt *ce;
8093 :
8094 293925 : if (!TYPE_DOMAIN (type)
8095 293925 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8096 : return 0;
8097 :
8098 293925 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
8099 293925 : if (fieldsize <= 0)
8100 : return 0;
8101 :
8102 293925 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8103 293925 : if (ptr)
8104 293925 : memset (ptr, '\0', MIN (total_bytes - off, len));
8105 :
8106 293925 : for (cnt = 0; ; cnt++)
8107 : {
8108 44867620 : tree val = NULL_TREE, index = NULL_TREE;
8109 44867620 : HOST_WIDE_INT pos = curpos, count = 0;
8110 44867620 : bool full = false;
8111 44867620 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8112 : {
8113 44764517 : val = ce->value;
8114 44764517 : index = ce->index;
8115 : }
8116 103103 : else if (mask == NULL
8117 618 : || CONSTRUCTOR_NO_CLEARING (init)
8118 103661 : || curpos >= total_bytes)
8119 : break;
8120 : else
8121 : pos = total_bytes;
8122 :
8123 44764517 : if (index && TREE_CODE (index) == RANGE_EXPR)
8124 : {
8125 54 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8126 54 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8127 0 : return 0;
8128 54 : offset_int wpos
8129 54 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8130 108 : - wi::to_offset (min_index),
8131 54 : TYPE_PRECISION (sizetype));
8132 54 : wpos *= fieldsize;
8133 54 : if (!wi::fits_shwi_p (pos))
8134 : return 0;
8135 54 : pos = wpos.to_shwi ();
8136 54 : offset_int wcount
8137 54 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8138 108 : - wi::to_offset (TREE_OPERAND (index, 0)),
8139 54 : TYPE_PRECISION (sizetype));
8140 54 : if (!wi::fits_shwi_p (wcount))
8141 : return 0;
8142 54 : count = wcount.to_shwi ();
8143 54 : }
8144 44231077 : else if (index)
8145 : {
8146 44231077 : if (TREE_CODE (index) != INTEGER_CST)
8147 0 : return 0;
8148 44231077 : offset_int wpos
8149 44231077 : = wi::sext (wi::to_offset (index)
8150 88462154 : - wi::to_offset (min_index),
8151 44231077 : TYPE_PRECISION (sizetype));
8152 44231077 : wpos *= fieldsize;
8153 44231077 : if (!wi::fits_shwi_p (wpos))
8154 : return 0;
8155 44231077 : pos = wpos.to_shwi ();
8156 : }
8157 :
8158 44765778 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8159 : {
8160 72 : if (valueinit == -1)
8161 : {
8162 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8163 144 : r = native_encode_initializer (zero, ptr + curpos,
8164 : fieldsize, 0,
8165 72 : mask + curpos);
8166 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8167 2 : ggc_free (zero);
8168 72 : if (!r)
8169 : return 0;
8170 72 : valueinit = curpos;
8171 72 : curpos += fieldsize;
8172 : }
8173 102 : while (curpos != pos)
8174 : {
8175 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8176 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8177 30 : curpos += fieldsize;
8178 : }
8179 : }
8180 :
8181 44764589 : curpos = pos;
8182 44764589 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8183 : {
8184 527 : if (count)
8185 : return 0;
8186 527 : if (off == -1
8187 527 : || (curpos >= off
8188 0 : && (curpos + RAW_DATA_LENGTH (val)
8189 0 : <= (HOST_WIDE_INT) off + len)))
8190 : {
8191 527 : if (ptr)
8192 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8193 527 : RAW_DATA_LENGTH (val));
8194 527 : if (mask)
8195 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8196 : }
8197 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8198 0 : && curpos < (HOST_WIDE_INT) off + len)
8199 : {
8200 : /* Partial overlap. */
8201 0 : unsigned char *p = NULL;
8202 0 : int no = 0;
8203 0 : int l;
8204 0 : gcc_assert (mask == NULL);
8205 0 : if (curpos >= off)
8206 : {
8207 0 : if (ptr)
8208 0 : p = ptr + curpos - off;
8209 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8210 : RAW_DATA_LENGTH (val));
8211 : }
8212 : else
8213 : {
8214 0 : p = ptr;
8215 0 : no = off - curpos;
8216 0 : l = len;
8217 : }
8218 0 : if (p)
8219 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8220 : }
8221 527 : curpos += RAW_DATA_LENGTH (val);
8222 527 : val = NULL_TREE;
8223 : }
8224 527 : if (val)
8225 44842084 : do
8226 : {
8227 44842084 : if (off == -1
8228 629204 : || (curpos >= off
8229 213619 : && (curpos + fieldsize
8230 213619 : <= (HOST_WIDE_INT) off + len)))
8231 : {
8232 44396112 : if (full)
8233 : {
8234 78094 : if (ptr)
8235 78094 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8236 : fieldsize);
8237 78094 : if (mask)
8238 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8239 : }
8240 88820385 : else if (!native_encode_initializer (val,
8241 : ptr
8242 44318018 : ? ptr + curpos - o
8243 : : NULL,
8244 : fieldsize,
8245 : off == -1 ? -1
8246 : : 0,
8247 : mask
8248 1117 : ? mask + curpos
8249 : : NULL))
8250 : return 0;
8251 : else
8252 : {
8253 : full = true;
8254 : pos = curpos;
8255 : }
8256 : }
8257 445972 : else if (curpos + fieldsize > off
8258 32650 : && curpos < (HOST_WIDE_INT) off + len)
8259 : {
8260 : /* Partial overlap. */
8261 8139 : unsigned char *p = NULL;
8262 8139 : int no = 0;
8263 8139 : int l;
8264 8139 : gcc_assert (mask == NULL);
8265 8139 : if (curpos >= off)
8266 : {
8267 5876 : if (ptr)
8268 5876 : p = ptr + curpos - off;
8269 5876 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8270 : fieldsize);
8271 : }
8272 : else
8273 : {
8274 2263 : p = ptr;
8275 2263 : no = off - curpos;
8276 2263 : l = len;
8277 : }
8278 8139 : if (!native_encode_initializer (val, p, l, no, NULL))
8279 : return 0;
8280 : }
8281 44651190 : curpos += fieldsize;
8282 : }
8283 44651190 : while (count-- != 0);
8284 44573695 : }
8285 103031 : return MIN (total_bytes - off, len);
8286 : }
8287 1873787 : else if (TREE_CODE (type) == RECORD_TYPE
8288 1873787 : || TREE_CODE (type) == UNION_TYPE)
8289 : {
8290 1873787 : unsigned HOST_WIDE_INT cnt;
8291 1873787 : constructor_elt *ce;
8292 1873787 : tree fld_base = TYPE_FIELDS (type);
8293 1873787 : tree to_free = NULL_TREE;
8294 :
8295 1873787 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8296 1873787 : if (ptr != NULL)
8297 1873787 : memset (ptr, '\0', MIN (total_bytes - o, len));
8298 1873787 : for (cnt = 0; ; cnt++)
8299 : {
8300 2241454 : tree val = NULL_TREE, field = NULL_TREE;
8301 2241454 : HOST_WIDE_INT pos = 0, fieldsize;
8302 2241454 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8303 :
8304 2241454 : if (to_free)
8305 : {
8306 0 : ggc_free (to_free);
8307 0 : to_free = NULL_TREE;
8308 : }
8309 :
8310 2241454 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8311 : {
8312 394911 : val = ce->value;
8313 394911 : field = ce->index;
8314 394911 : if (field == NULL_TREE)
8315 : return 0;
8316 :
8317 394911 : pos = int_byte_position (field);
8318 394911 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8319 1501 : continue;
8320 : }
8321 1846543 : else if (mask == NULL
8322 1846543 : || CONSTRUCTOR_NO_CLEARING (init))
8323 : break;
8324 : else
8325 : pos = total_bytes;
8326 :
8327 407435 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8328 : {
8329 : tree fld;
8330 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8331 : {
8332 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8333 44565 : continue;
8334 2083 : if (fld == field)
8335 : break;
8336 528 : if (DECL_PADDING_P (fld))
8337 87 : continue;
8338 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8339 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8340 : return 0;
8341 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8342 382 : continue;
8343 : break;
8344 : }
8345 1614 : if (fld == NULL_TREE)
8346 : {
8347 895 : if (ce == NULL)
8348 : break;
8349 : return 0;
8350 : }
8351 1614 : fld_base = DECL_CHAIN (fld);
8352 1614 : if (fld != field)
8353 : {
8354 59 : cnt--;
8355 59 : field = fld;
8356 59 : pos = int_byte_position (field);
8357 59 : val = build_zero_cst (TREE_TYPE (fld));
8358 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8359 0 : to_free = val;
8360 : }
8361 : }
8362 :
8363 393469 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8364 15152 : && TYPE_DOMAIN (TREE_TYPE (field))
8365 408621 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8366 : {
8367 81 : if (mask || off != -1)
8368 : return 0;
8369 81 : if (val == NULL_TREE)
8370 0 : continue;
8371 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8372 : return 0;
8373 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8374 81 : if (fieldsize < 0
8375 81 : || (int) fieldsize != fieldsize
8376 81 : || (pos + fieldsize) > INT_MAX)
8377 : return 0;
8378 81 : if (pos + fieldsize > total_bytes)
8379 : {
8380 81 : if (ptr != NULL && total_bytes < len)
8381 81 : memset (ptr + total_bytes, '\0',
8382 81 : MIN (pos + fieldsize, len) - total_bytes);
8383 : total_bytes = pos + fieldsize;
8384 : }
8385 : }
8386 : else
8387 : {
8388 393388 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8389 393388 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8390 : return 0;
8391 393388 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8392 : }
8393 393469 : if (fieldsize == 0)
8394 1 : continue;
8395 :
8396 : /* Prepare to deal with integral bit-fields and filter out other
8397 : bit-fields that do not start and end on a byte boundary. */
8398 393468 : if (DECL_BIT_FIELD (field))
8399 : {
8400 2717 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8401 : return 0;
8402 2717 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8403 2717 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8404 : {
8405 2717 : bpos %= BITS_PER_UNIT;
8406 2717 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8407 2717 : epos = fieldsize % BITS_PER_UNIT;
8408 2717 : fieldsize += BITS_PER_UNIT - 1;
8409 2717 : fieldsize /= BITS_PER_UNIT;
8410 : }
8411 0 : else if (bpos % BITS_PER_UNIT
8412 0 : || DECL_SIZE (field) == NULL_TREE
8413 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8414 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8415 : return 0;
8416 : }
8417 :
8418 393468 : if (off != -1 && pos + fieldsize <= off)
8419 3199 : continue;
8420 :
8421 390269 : if (val == NULL_TREE)
8422 0 : continue;
8423 :
8424 390269 : if (DECL_BIT_FIELD (field)
8425 390269 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8426 : {
8427 : /* FIXME: Handle PDP endian. */
8428 2513 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8429 264 : return 0;
8430 :
8431 2513 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8432 6 : val = TREE_OPERAND (val, 0);
8433 2513 : if (TREE_CODE (val) != INTEGER_CST)
8434 : return 0;
8435 :
8436 2513 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8437 2513 : tree repr_type = NULL_TREE;
8438 2513 : HOST_WIDE_INT rpos = 0;
8439 2513 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8440 : {
8441 1978 : rpos = int_byte_position (repr);
8442 1978 : repr_type = TREE_TYPE (repr);
8443 : }
8444 : else
8445 : {
8446 535 : repr_type = find_bitfield_repr_type (fieldsize, len);
8447 535 : if (repr_type == NULL_TREE)
8448 : return 0;
8449 271 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8450 271 : gcc_assert (repr_size > 0 && repr_size <= len);
8451 271 : if (pos + repr_size <= o + len)
8452 : rpos = pos;
8453 : else
8454 : {
8455 14 : rpos = o + len - repr_size;
8456 14 : gcc_assert (rpos <= pos);
8457 : }
8458 : }
8459 :
8460 2249 : if (rpos > pos)
8461 : return 0;
8462 2249 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8463 2249 : int diff = (TYPE_PRECISION (repr_type)
8464 2249 : - TYPE_PRECISION (TREE_TYPE (field)));
8465 2249 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8466 2249 : if (!BYTES_BIG_ENDIAN)
8467 2249 : w = wi::lshift (w, bitoff);
8468 : else
8469 : w = wi::lshift (w, diff - bitoff);
8470 2249 : val = wide_int_to_tree (repr_type, w);
8471 :
8472 2249 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8473 : / BITS_PER_UNIT + 1];
8474 2249 : int l = native_encode_int (val, buf, sizeof buf, 0);
8475 2249 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8476 0 : return 0;
8477 :
8478 2249 : if (ptr == NULL)
8479 0 : continue;
8480 :
8481 : /* If the bitfield does not start at byte boundary, handle
8482 : the partial byte at the start. */
8483 2249 : if (bpos
8484 1351 : && (off == -1 || (pos >= off && len >= 1)))
8485 : {
8486 1276 : if (!BYTES_BIG_ENDIAN)
8487 : {
8488 1276 : int msk = (1 << bpos) - 1;
8489 1276 : buf[pos - rpos] &= ~msk;
8490 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8491 1276 : if (mask)
8492 : {
8493 147 : if (fieldsize > 1 || epos == 0)
8494 129 : mask[pos] &= msk;
8495 : else
8496 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8497 : }
8498 : }
8499 : else
8500 : {
8501 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8502 : buf[pos - rpos] &= msk;
8503 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8504 : if (mask)
8505 : {
8506 : if (fieldsize > 1 || epos == 0)
8507 : mask[pos] &= ~msk;
8508 : else
8509 : mask[pos] &= (~msk
8510 : | ((1 << (BITS_PER_UNIT - epos))
8511 : - 1));
8512 : }
8513 : }
8514 : }
8515 : /* If the bitfield does not end at byte boundary, handle
8516 : the partial byte at the end. */
8517 2249 : if (epos
8518 1727 : && (off == -1
8519 1007 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8520 : {
8521 1624 : if (!BYTES_BIG_ENDIAN)
8522 : {
8523 1624 : int msk = (1 << epos) - 1;
8524 1624 : buf[pos - rpos + fieldsize - 1] &= msk;
8525 1624 : buf[pos - rpos + fieldsize - 1]
8526 1624 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8527 1624 : if (mask && (fieldsize > 1 || bpos == 0))
8528 156 : mask[pos + fieldsize - 1] &= ~msk;
8529 : }
8530 : else
8531 : {
8532 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8533 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8534 : buf[pos - rpos + fieldsize - 1]
8535 : |= ptr[pos + fieldsize - 1 - o] & msk;
8536 : if (mask && (fieldsize > 1 || bpos == 0))
8537 : mask[pos + fieldsize - 1] &= msk;
8538 : }
8539 : }
8540 2249 : if (off == -1
8541 1304 : || (pos >= off
8542 1215 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8543 : {
8544 2058 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8545 2058 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8546 75 : memset (mask + pos + (bpos != 0), 0,
8547 75 : fieldsize - (bpos != 0) - (epos != 0));
8548 : }
8549 : else
8550 : {
8551 : /* Partial overlap. */
8552 191 : HOST_WIDE_INT fsz = fieldsize;
8553 191 : gcc_assert (mask == NULL);
8554 191 : if (pos < off)
8555 : {
8556 89 : fsz -= (off - pos);
8557 89 : pos = off;
8558 : }
8559 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8560 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8561 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8562 : }
8563 2249 : continue;
8564 2249 : }
8565 :
8566 387756 : if (off == -1
8567 28694 : || (pos >= off
8568 27862 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8569 : {
8570 379383 : int fldsize = fieldsize;
8571 20321 : if (off == -1)
8572 : {
8573 359062 : tree fld = DECL_CHAIN (field);
8574 5697446 : while (fld)
8575 : {
8576 5357194 : if (TREE_CODE (fld) == FIELD_DECL)
8577 : break;
8578 5338384 : fld = DECL_CHAIN (fld);
8579 : }
8580 359062 : if (fld == NULL_TREE)
8581 340252 : fldsize = len - pos;
8582 : }
8583 411562 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8584 : : NULL,
8585 : fldsize,
8586 : off == -1 ? -1 : 0,
8587 11858 : mask ? mask + pos : NULL);
8588 379383 : if (!r)
8589 : return 0;
8590 359694 : if (off == -1
8591 346615 : && fldsize != fieldsize
8592 1149 : && r > fieldsize
8593 822 : && pos + r > total_bytes)
8594 367667 : total_bytes = pos + r;
8595 : }
8596 : else
8597 : {
8598 : /* Partial overlap. */
8599 8373 : unsigned char *p = NULL;
8600 8373 : int no = 0;
8601 8373 : int l;
8602 8373 : gcc_assert (mask == NULL);
8603 8373 : if (pos >= off)
8604 : {
8605 7541 : if (ptr)
8606 7541 : p = ptr + pos - off;
8607 7541 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8608 : fieldsize);
8609 : }
8610 : else
8611 : {
8612 832 : p = ptr;
8613 832 : no = off - pos;
8614 832 : l = len;
8615 : }
8616 8373 : if (!native_encode_initializer (val, p, l, no, NULL))
8617 : return 0;
8618 : }
8619 367667 : }
8620 1846484 : return MIN (total_bytes - off, len);
8621 : }
8622 : return 0;
8623 : }
8624 : }
8625 :
8626 :
8627 : /* Subroutine of native_interpret_expr. Interpret the contents of
8628 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8629 : If the buffer cannot be interpreted, return NULL_TREE. */
8630 :
8631 : static tree
8632 3098398 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8633 : {
8634 3098398 : int total_bytes;
8635 3098398 : if (BITINT_TYPE_P (type))
8636 : {
8637 79 : struct bitint_info info;
8638 79 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8639 79 : gcc_assert (ok);
8640 79 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8641 79 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8642 : {
8643 79 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8644 : /* More work is needed when adding _BitInt support to PDP endian
8645 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8646 : match target endianity here. */
8647 79 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8648 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8649 : || (GET_MODE_SIZE (limb_mode)
8650 : >= UNITS_PER_WORD)));
8651 : }
8652 : else
8653 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8654 : }
8655 : else
8656 6196638 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8657 :
8658 3098398 : if (total_bytes > len)
8659 : return NULL_TREE;
8660 :
8661 3097999 : wide_int result = wi::from_buffer (ptr, total_bytes);
8662 :
8663 3097999 : return wide_int_to_tree (type, result);
8664 3097999 : }
8665 :
8666 :
8667 : /* Subroutine of native_interpret_expr. Interpret the contents of
8668 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8669 : If the buffer cannot be interpreted, return NULL_TREE. */
8670 :
8671 : static tree
8672 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8673 : {
8674 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8675 0 : int total_bytes = GET_MODE_SIZE (mode);
8676 0 : double_int result;
8677 0 : FIXED_VALUE_TYPE fixed_value;
8678 :
8679 0 : if (total_bytes > len
8680 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8681 : return NULL_TREE;
8682 :
8683 0 : result = double_int::from_buffer (ptr, total_bytes);
8684 0 : fixed_value = fixed_from_double_int (result, mode);
8685 :
8686 0 : return build_fixed (type, fixed_value);
8687 : }
8688 :
8689 :
8690 : /* Subroutine of native_interpret_expr. Interpret the contents of
8691 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8692 : If the buffer cannot be interpreted, return NULL_TREE. */
8693 :
8694 : tree
8695 47202 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8696 : {
8697 47202 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8698 47202 : int total_bytes = GET_MODE_SIZE (mode);
8699 47202 : unsigned char value;
8700 : /* There are always 32 bits in each long, no matter the size of
8701 : the hosts long. We handle floating point representations with
8702 : up to 192 bits. */
8703 47202 : REAL_VALUE_TYPE r;
8704 47202 : long tmp[6];
8705 :
8706 47202 : if (total_bytes > len || total_bytes > 24)
8707 : return NULL_TREE;
8708 47141 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8709 :
8710 47141 : memset (tmp, 0, sizeof (tmp));
8711 336387 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8712 289246 : bitpos += BITS_PER_UNIT)
8713 : {
8714 : /* Both OFFSET and BYTE index within a long;
8715 : bitpos indexes the whole float. */
8716 289246 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8717 289246 : if (UNITS_PER_WORD < 4)
8718 : {
8719 : int word = byte / UNITS_PER_WORD;
8720 : if (WORDS_BIG_ENDIAN)
8721 : word = (words - 1) - word;
8722 : offset = word * UNITS_PER_WORD;
8723 : if (BYTES_BIG_ENDIAN)
8724 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8725 : else
8726 : offset += byte % UNITS_PER_WORD;
8727 : }
8728 : else
8729 : {
8730 289246 : offset = byte;
8731 289246 : if (BYTES_BIG_ENDIAN)
8732 : {
8733 : /* Reverse bytes within each long, or within the entire float
8734 : if it's smaller than a long (for HFmode). */
8735 : offset = MIN (3, total_bytes - 1) - offset;
8736 : gcc_assert (offset >= 0);
8737 : }
8738 : }
8739 289246 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8740 :
8741 289246 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8742 : }
8743 :
8744 47141 : real_from_target (&r, tmp, mode);
8745 47141 : return build_real (type, r);
8746 : }
8747 :
8748 :
8749 : /* Subroutine of native_interpret_expr. Interpret the contents of
8750 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8751 : If the buffer cannot be interpreted, return NULL_TREE. */
8752 :
8753 : static tree
8754 1662 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8755 : {
8756 1662 : tree etype, rpart, ipart;
8757 1662 : int size;
8758 :
8759 1662 : etype = TREE_TYPE (type);
8760 1662 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8761 1662 : if (size * 2 > len)
8762 : return NULL_TREE;
8763 1625 : rpart = native_interpret_expr (etype, ptr, size);
8764 1625 : if (!rpart)
8765 : return NULL_TREE;
8766 1444 : ipart = native_interpret_expr (etype, ptr+size, size);
8767 1444 : if (!ipart)
8768 : return NULL_TREE;
8769 1444 : return build_complex (type, rpart, ipart);
8770 : }
8771 :
8772 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8773 : which contains LEN bytes. The vector is known to be encodable using
8774 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8775 :
8776 : Return the vector on success, otherwise return null. */
8777 :
8778 : static tree
8779 244627 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8780 : unsigned int len, unsigned int npatterns,
8781 : unsigned int nelts_per_pattern)
8782 : {
8783 244627 : tree elt_type = TREE_TYPE (type);
8784 244627 : if (VECTOR_BOOLEAN_TYPE_P (type)
8785 244642 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8786 : {
8787 : /* This is the only case in which elements can be smaller than a byte.
8788 : Element 0 is always in the lsb of the containing byte. */
8789 5 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8790 5 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8791 : return NULL_TREE;
8792 :
8793 5 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8794 76 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8795 : {
8796 66 : unsigned int bit_index = i * elt_bits;
8797 66 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8798 66 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8799 132 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8800 116 : ? build_all_ones_cst (elt_type)
8801 50 : : build_zero_cst (elt_type));
8802 : }
8803 5 : return builder.build ();
8804 5 : }
8805 :
8806 244622 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8807 244622 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8808 : return NULL_TREE;
8809 :
8810 244622 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8811 1222004 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8812 : {
8813 732894 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8814 732894 : if (!elt)
8815 134 : return NULL_TREE;
8816 732760 : builder.quick_push (elt);
8817 732760 : bytes += elt_bytes;
8818 : }
8819 244488 : return builder.build ();
8820 244622 : }
8821 :
8822 : /* Subroutine of native_interpret_expr. Interpret the contents of
8823 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8824 : If the buffer cannot be interpreted, return NULL_TREE. */
8825 :
8826 : static tree
8827 86745 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8828 : {
8829 86745 : unsigned HOST_WIDE_INT size;
8830 :
8831 86745 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8832 86745 : || size > len)
8833 2 : return NULL_TREE;
8834 :
8835 86743 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8836 86743 : return native_interpret_vector_part (type, ptr, len, count, 1);
8837 : }
8838 :
8839 :
8840 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8841 : the buffer PTR of length LEN as a constant of type TYPE. For
8842 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8843 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8844 : return NULL_TREE. */
8845 :
8846 : tree
8847 3391406 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8848 : {
8849 3391406 : switch (TREE_CODE (type))
8850 : {
8851 3098398 : case INTEGER_TYPE:
8852 3098398 : case ENUMERAL_TYPE:
8853 3098398 : case BOOLEAN_TYPE:
8854 3098398 : case POINTER_TYPE:
8855 3098398 : case REFERENCE_TYPE:
8856 3098398 : case OFFSET_TYPE:
8857 3098398 : case BITINT_TYPE:
8858 3098398 : return native_interpret_int (type, ptr, len);
8859 :
8860 45615 : case REAL_TYPE:
8861 45615 : if (tree ret = native_interpret_real (type, ptr, len))
8862 : {
8863 : /* For floating point values in composite modes, punt if this
8864 : folding doesn't preserve bit representation. As the mode doesn't
8865 : have fixed precision while GCC pretends it does, there could be
8866 : valid values that GCC can't really represent accurately.
8867 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8868 : bit combinationations which GCC doesn't preserve. */
8869 45554 : unsigned char buf[24 * 2];
8870 45554 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8871 45554 : int total_bytes = GET_MODE_SIZE (mode);
8872 45554 : memcpy (buf + 24, ptr, total_bytes);
8873 45554 : clear_type_padding_in_mask (type, buf + 24);
8874 45554 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8875 45554 : || memcmp (buf + 24, buf, total_bytes) != 0)
8876 488 : return NULL_TREE;
8877 : return ret;
8878 : }
8879 : return NULL_TREE;
8880 :
8881 0 : case FIXED_POINT_TYPE:
8882 0 : return native_interpret_fixed (type, ptr, len);
8883 :
8884 1662 : case COMPLEX_TYPE:
8885 1662 : return native_interpret_complex (type, ptr, len);
8886 :
8887 86745 : case VECTOR_TYPE:
8888 86745 : return native_interpret_vector (type, ptr, len);
8889 :
8890 : default:
8891 : return NULL_TREE;
8892 : }
8893 : }
8894 :
8895 : /* Returns true if we can interpret the contents of a native encoding
8896 : as TYPE. */
8897 :
8898 : bool
8899 724941 : can_native_interpret_type_p (tree type)
8900 : {
8901 724941 : switch (TREE_CODE (type))
8902 : {
8903 : case INTEGER_TYPE:
8904 : case ENUMERAL_TYPE:
8905 : case BOOLEAN_TYPE:
8906 : case POINTER_TYPE:
8907 : case REFERENCE_TYPE:
8908 : case FIXED_POINT_TYPE:
8909 : case REAL_TYPE:
8910 : case COMPLEX_TYPE:
8911 : case VECTOR_TYPE:
8912 : case OFFSET_TYPE:
8913 : return true;
8914 42308 : default:
8915 42308 : return false;
8916 : }
8917 : }
8918 :
8919 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8920 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8921 :
8922 : tree
8923 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8924 : int len)
8925 : {
8926 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8927 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8928 : {
8929 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8930 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8931 : return NULL_TREE;
8932 :
8933 197 : HOST_WIDE_INT cnt = 0;
8934 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8935 : {
8936 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8937 : return NULL_TREE;
8938 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8939 : }
8940 197 : if (eltsz == 0)
8941 0 : cnt = 0;
8942 197 : HOST_WIDE_INT pos = 0;
8943 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8944 : {
8945 439 : tree v = NULL_TREE;
8946 439 : if (pos >= len || pos + eltsz > len)
8947 9631 : return NULL_TREE;
8948 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8949 : {
8950 367 : v = native_interpret_expr (TREE_TYPE (type),
8951 367 : ptr + off + pos, eltsz);
8952 367 : if (v == NULL_TREE)
8953 : return NULL_TREE;
8954 : }
8955 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8956 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8957 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8958 : eltsz);
8959 72 : if (v == NULL_TREE)
8960 : return NULL_TREE;
8961 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8962 : }
8963 197 : return build_constructor (type, elts);
8964 : }
8965 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8966 : return NULL_TREE;
8967 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8968 : {
8969 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8970 796932 : || is_empty_type (TREE_TYPE (field)))
8971 761072 : continue;
8972 13494 : tree fld = field;
8973 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8974 13494 : int diff = 0;
8975 13494 : tree v = NULL_TREE;
8976 13494 : if (DECL_BIT_FIELD (field))
8977 : {
8978 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8979 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8980 : {
8981 168 : poly_int64 bitoffset;
8982 168 : poly_uint64 field_offset, fld_offset;
8983 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8984 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8985 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8986 : else
8987 : bitoffset = 0;
8988 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8989 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8990 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8991 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8992 168 : if (!bitoffset.is_constant (&bitoff)
8993 168 : || bitoff < 0
8994 168 : || bitoff > diff)
8995 0 : return NULL_TREE;
8996 : }
8997 : else
8998 : {
8999 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
9000 : return NULL_TREE;
9001 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
9002 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
9003 12 : bpos %= BITS_PER_UNIT;
9004 12 : fieldsize += bpos;
9005 12 : fieldsize += BITS_PER_UNIT - 1;
9006 12 : fieldsize /= BITS_PER_UNIT;
9007 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
9008 12 : if (repr_type == NULL_TREE)
9009 : return NULL_TREE;
9010 12 : sz = int_size_in_bytes (repr_type);
9011 12 : if (sz < 0 || sz > len)
9012 : return NULL_TREE;
9013 12 : pos = int_byte_position (field);
9014 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
9015 : return NULL_TREE;
9016 12 : HOST_WIDE_INT rpos;
9017 12 : if (pos + sz <= len)
9018 : rpos = pos;
9019 : else
9020 : {
9021 0 : rpos = len - sz;
9022 0 : gcc_assert (rpos <= pos);
9023 : }
9024 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
9025 12 : pos = rpos;
9026 12 : diff = (TYPE_PRECISION (repr_type)
9027 12 : - TYPE_PRECISION (TREE_TYPE (field)));
9028 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
9029 12 : if (v == NULL_TREE)
9030 : return NULL_TREE;
9031 : fld = NULL_TREE;
9032 : }
9033 : }
9034 :
9035 168 : if (fld)
9036 : {
9037 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
9038 13482 : if (sz < 0 || sz > len)
9039 : return NULL_TREE;
9040 13482 : tree byte_pos = byte_position (fld);
9041 13482 : if (!tree_fits_shwi_p (byte_pos))
9042 : return NULL_TREE;
9043 13482 : pos = tree_to_shwi (byte_pos);
9044 13482 : if (pos < 0 || pos > len || pos + sz > len)
9045 : return NULL_TREE;
9046 : }
9047 13482 : if (fld == NULL_TREE)
9048 : /* Already handled above. */;
9049 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
9050 : {
9051 6262 : v = native_interpret_expr (TREE_TYPE (fld),
9052 6262 : ptr + off + pos, sz);
9053 6262 : if (v == NULL_TREE)
9054 : return NULL_TREE;
9055 : }
9056 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
9057 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
9058 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
9059 7232 : if (v == NULL_TREE)
9060 : return NULL_TREE;
9061 13494 : if (fld != field)
9062 : {
9063 180 : if (TREE_CODE (v) != INTEGER_CST)
9064 : return NULL_TREE;
9065 :
9066 : /* FIXME: Figure out how to handle PDP endian bitfields. */
9067 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9068 : return NULL_TREE;
9069 180 : if (!BYTES_BIG_ENDIAN)
9070 180 : v = wide_int_to_tree (TREE_TYPE (field),
9071 360 : wi::lrshift (wi::to_wide (v), bitoff));
9072 : else
9073 : v = wide_int_to_tree (TREE_TYPE (field),
9074 : wi::lrshift (wi::to_wide (v),
9075 : diff - bitoff));
9076 : }
9077 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
9078 : }
9079 9434 : return build_constructor (type, elts);
9080 : }
9081 :
9082 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
9083 : or extracted constant positions and/or sizes aren't byte aligned. */
9084 :
9085 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9086 : bits between adjacent elements. AMNT should be within
9087 : [0, BITS_PER_UNIT).
9088 : Example, AMNT = 2:
9089 : 00011111|11100000 << 2 = 01111111|10000000
9090 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9091 :
9092 : void
9093 30623 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9094 : unsigned int amnt)
9095 : {
9096 30623 : if (amnt == 0)
9097 : return;
9098 :
9099 17744 : unsigned char carry_over = 0U;
9100 17744 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9101 17744 : unsigned char clear_mask = (~0U) << amnt;
9102 :
9103 107250 : for (unsigned int i = 0; i < sz; i++)
9104 : {
9105 89506 : unsigned prev_carry_over = carry_over;
9106 89506 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9107 :
9108 89506 : ptr[i] <<= amnt;
9109 89506 : if (i != 0)
9110 : {
9111 71762 : ptr[i] &= clear_mask;
9112 71762 : ptr[i] |= prev_carry_over;
9113 : }
9114 : }
9115 : }
9116 :
9117 : /* Like shift_bytes_in_array_left but for big-endian.
9118 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9119 : bits between adjacent elements. AMNT should be within
9120 : [0, BITS_PER_UNIT).
9121 : Example, AMNT = 2:
9122 : 00011111|11100000 >> 2 = 00000111|11111000
9123 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9124 :
9125 : void
9126 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9127 : unsigned int amnt)
9128 : {
9129 8 : if (amnt == 0)
9130 : return;
9131 :
9132 4 : unsigned char carry_over = 0U;
9133 4 : unsigned char carry_mask = ~(~0U << amnt);
9134 :
9135 12 : for (unsigned int i = 0; i < sz; i++)
9136 : {
9137 8 : unsigned prev_carry_over = carry_over;
9138 8 : carry_over = ptr[i] & carry_mask;
9139 :
9140 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9141 8 : ptr[i] >>= amnt;
9142 8 : ptr[i] |= prev_carry_over;
9143 : }
9144 : }
9145 :
9146 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9147 : directly on the VECTOR_CST encoding, in a way that works for variable-
9148 : length vectors. Return the resulting VECTOR_CST on success or null
9149 : on failure. */
9150 :
9151 : static tree
9152 167596 : fold_view_convert_vector_encoding (tree type, tree expr)
9153 : {
9154 167596 : tree expr_type = TREE_TYPE (expr);
9155 167596 : poly_uint64 type_bits, expr_bits;
9156 167596 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9157 167596 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9158 : return NULL_TREE;
9159 :
9160 167596 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9161 167596 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9162 :
9163 : /* Vectors with container padding (such as AVX512 2-lane or 4-lane masks
9164 : where TYPE_SIZE is 8 bits but only 2 or 4 bits are used) cannot be
9165 : represented safely by operating directly on the vector encoding.
9166 : Let the general VIEW_CONVERT handling cope with that case. */
9167 167596 : if (VECTOR_BOOLEAN_TYPE_P (type)
9168 167606 : && maybe_ne (type_bits, type_units * TYPE_PRECISION (TREE_TYPE (type))))
9169 : return NULL_TREE;
9170 167596 : if (VECTOR_BOOLEAN_TYPE_P (expr_type)
9171 167639 : && maybe_ne (expr_bits, expr_units * TYPE_PRECISION (TREE_TYPE (expr_type))))
9172 : return NULL_TREE;
9173 :
9174 167596 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9175 167596 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9176 :
9177 : /* We can only preserve the semantics of a stepped pattern if the new
9178 : vector element is an integer of the same size. */
9179 167596 : if (VECTOR_CST_STEPPED_P (expr)
9180 167596 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9181 : return NULL_TREE;
9182 :
9183 : /* The number of bits needed to encode one element from every pattern
9184 : of the original vector. */
9185 157884 : unsigned int expr_sequence_bits
9186 157884 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9187 :
9188 : /* The number of bits needed to encode one element from every pattern
9189 : of the result. */
9190 157884 : unsigned int type_sequence_bits
9191 157884 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9192 :
9193 : /* Don't try to read more bytes than are available, which can happen
9194 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9195 : The general VIEW_CONVERT handling can cope with that case, so there's
9196 : no point complicating things here. */
9197 157884 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9198 157884 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9199 : BITS_PER_UNIT);
9200 157884 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9201 157884 : if (known_gt (buffer_bits, expr_bits))
9202 : return NULL_TREE;
9203 :
9204 : /* Get enough bytes of EXPR to form the new encoding. */
9205 157884 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9206 157884 : buffer.quick_grow (buffer_bytes);
9207 157884 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9208 157884 : buffer_bits / expr_elt_bits)
9209 : != (int) buffer_bytes)
9210 : return NULL_TREE;
9211 :
9212 : /* Re-encode the bytes as TYPE. */
9213 157884 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9214 315768 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9215 157884 : type_npatterns, nelts_per_pattern);
9216 157884 : }
9217 :
9218 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9219 : TYPE at compile-time. If we're unable to perform the conversion
9220 : return NULL_TREE. */
9221 :
9222 : static tree
9223 13226431 : fold_view_convert_expr (tree type, tree expr)
9224 : {
9225 13226431 : unsigned char buffer[128];
9226 13226431 : unsigned char *buf;
9227 13226431 : int len;
9228 13226431 : HOST_WIDE_INT l;
9229 :
9230 : /* Check that the host and target are sane. */
9231 13226431 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9232 : return NULL_TREE;
9233 :
9234 13226431 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9235 167596 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9236 : return res;
9237 :
9238 13068614 : l = int_size_in_bytes (type);
9239 13068614 : if (l > (int) sizeof (buffer)
9240 13068614 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9241 : {
9242 0 : buf = XALLOCAVEC (unsigned char, l);
9243 0 : len = l;
9244 : }
9245 : else
9246 : {
9247 : buf = buffer;
9248 : len = sizeof (buffer);
9249 : }
9250 13068614 : len = native_encode_expr (expr, buf, len);
9251 13068614 : if (len == 0)
9252 : return NULL_TREE;
9253 :
9254 1917616 : return native_interpret_expr (type, buf, len);
9255 : }
9256 :
9257 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9258 : to avoid confusing the gimplify process. */
9259 :
9260 : tree
9261 570720281 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9262 : {
9263 : /* The size of the object is not relevant when talking about its address. */
9264 570720281 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9265 0 : t = TREE_OPERAND (t, 0);
9266 :
9267 570720281 : if (INDIRECT_REF_P (t))
9268 : {
9269 62091599 : t = TREE_OPERAND (t, 0);
9270 :
9271 62091599 : if (TREE_TYPE (t) != ptrtype)
9272 39831204 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9273 : }
9274 508628682 : else if (TREE_CODE (t) == MEM_REF
9275 508628682 : && integer_zerop (TREE_OPERAND (t, 1)))
9276 : {
9277 1685141 : t = TREE_OPERAND (t, 0);
9278 :
9279 1685141 : if (TREE_TYPE (t) != ptrtype)
9280 1106102 : t = fold_convert_loc (loc, ptrtype, t);
9281 : }
9282 506943541 : else if (TREE_CODE (t) == MEM_REF
9283 506943541 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9284 662 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9285 : TREE_OPERAND (t, 0),
9286 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9287 506942879 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9288 : {
9289 31390753 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9290 :
9291 31390753 : if (TREE_TYPE (t) != ptrtype)
9292 17831 : t = fold_convert_loc (loc, ptrtype, t);
9293 : }
9294 : else
9295 475552126 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9296 :
9297 : return t;
9298 : }
9299 :
9300 : /* Build an expression for the address of T. */
9301 :
9302 : tree
9303 488899670 : build_fold_addr_expr_loc (location_t loc, tree t)
9304 : {
9305 488899670 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9306 :
9307 488899670 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9308 : }
9309 :
9310 : /* Fold a unary expression of code CODE and type TYPE with operand
9311 : OP0. Return the folded expression if folding is successful.
9312 : Otherwise, return NULL_TREE. */
9313 :
9314 : tree
9315 2087680907 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9316 : {
9317 2087680907 : tree tem;
9318 2087680907 : tree arg0;
9319 2087680907 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9320 :
9321 2087680907 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9322 : && TREE_CODE_LENGTH (code) == 1);
9323 :
9324 2087680907 : arg0 = op0;
9325 2087680907 : if (arg0)
9326 : {
9327 2087667601 : if (CONVERT_EXPR_CODE_P (code)
9328 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9329 : {
9330 : /* Don't use STRIP_NOPS, because signedness of argument type
9331 : matters. */
9332 1190098385 : STRIP_SIGN_NOPS (arg0);
9333 : }
9334 : else
9335 : {
9336 : /* Strip any conversions that don't change the mode. This
9337 : is safe for every expression, except for a comparison
9338 : expression because its signedness is derived from its
9339 : operands.
9340 :
9341 : Note that this is done as an internal manipulation within
9342 : the constant folder, in order to find the simplest
9343 : representation of the arguments so that their form can be
9344 : studied. In any cases, the appropriate type conversions
9345 : should be put back in the tree that will get out of the
9346 : constant folder. */
9347 897569216 : STRIP_NOPS (arg0);
9348 : }
9349 :
9350 2087667601 : if (CONSTANT_CLASS_P (arg0))
9351 : {
9352 328041195 : tree tem = const_unop (code, type, arg0);
9353 328041195 : if (tem)
9354 : {
9355 287995436 : if (TREE_TYPE (tem) != type)
9356 9872 : tem = fold_convert_loc (loc, type, tem);
9357 : return tem;
9358 : }
9359 : }
9360 : }
9361 :
9362 1799685471 : tem = generic_simplify (loc, code, type, op0);
9363 1799685471 : if (tem)
9364 : return tem;
9365 :
9366 1354360735 : if (TREE_CODE_CLASS (code) == tcc_unary)
9367 : {
9368 769870138 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9369 1074504 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9370 : fold_build1_loc (loc, code, type,
9371 1074504 : fold_convert_loc (loc, TREE_TYPE (op0),
9372 2149008 : TREE_OPERAND (arg0, 1))));
9373 768795634 : else if (TREE_CODE (arg0) == COND_EXPR)
9374 : {
9375 552566 : tree arg01 = TREE_OPERAND (arg0, 1);
9376 552566 : tree arg02 = TREE_OPERAND (arg0, 2);
9377 552566 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9378 548384 : arg01 = fold_build1_loc (loc, code, type,
9379 : fold_convert_loc (loc,
9380 548384 : TREE_TYPE (op0), arg01));
9381 552566 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9382 542222 : arg02 = fold_build1_loc (loc, code, type,
9383 : fold_convert_loc (loc,
9384 542222 : TREE_TYPE (op0), arg02));
9385 552566 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9386 : arg01, arg02);
9387 :
9388 : /* If this was a conversion, and all we did was to move into
9389 : inside the COND_EXPR, bring it back out. But leave it if
9390 : it is a conversion from integer to integer and the
9391 : result precision is no wider than a word since such a
9392 : conversion is cheap and may be optimized away by combine,
9393 : while it couldn't if it were outside the COND_EXPR. Then return
9394 : so we don't get into an infinite recursion loop taking the
9395 : conversion out and then back in. */
9396 :
9397 552566 : if ((CONVERT_EXPR_CODE_P (code)
9398 10346 : || code == NON_LVALUE_EXPR)
9399 542239 : && TREE_CODE (tem) == COND_EXPR
9400 522474 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9401 458572 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9402 292098 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9403 291886 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9404 291886 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9405 291886 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9406 865806 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9407 22161 : && (INTEGRAL_TYPE_P
9408 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9409 22121 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9410 22008 : || flag_syntax_only))
9411 269111 : tem = build1_loc (loc, code, type,
9412 : build3 (COND_EXPR,
9413 269111 : TREE_TYPE (TREE_OPERAND
9414 : (TREE_OPERAND (tem, 1), 0)),
9415 269111 : TREE_OPERAND (tem, 0),
9416 269111 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9417 269111 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9418 : 0)));
9419 : return tem;
9420 : }
9421 : }
9422 :
9423 1352733665 : switch (code)
9424 : {
9425 42864013 : case NON_LVALUE_EXPR:
9426 42864013 : if (!maybe_lvalue_p (op0))
9427 31145516 : return fold_convert_loc (loc, type, op0);
9428 : return NULL_TREE;
9429 :
9430 714791276 : CASE_CONVERT:
9431 714791276 : case FLOAT_EXPR:
9432 714791276 : case FIX_TRUNC_EXPR:
9433 714791276 : if (COMPARISON_CLASS_P (op0))
9434 : {
9435 : /* If we have (type) (a CMP b) and type is an integral type, return
9436 : new expression involving the new type. Canonicalize
9437 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9438 : non-integral type.
9439 : Do not fold the result as that would not simplify further, also
9440 : folding again results in recursions. */
9441 755553 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9442 167454 : return build2_loc (loc, TREE_CODE (op0), type,
9443 167454 : TREE_OPERAND (op0, 0),
9444 334908 : TREE_OPERAND (op0, 1));
9445 588099 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9446 7719 : && TREE_CODE (type) != VECTOR_TYPE)
9447 7719 : return build3_loc (loc, COND_EXPR, type, op0,
9448 : constant_boolean_node (true, type),
9449 7719 : constant_boolean_node (false, type));
9450 : }
9451 :
9452 : /* Handle (T *)&A.B.C for A being of type T and B and C
9453 : living at offset zero. This occurs frequently in
9454 : C++ upcasting and then accessing the base. */
9455 714616103 : if (TREE_CODE (op0) == ADDR_EXPR
9456 251995388 : && POINTER_TYPE_P (type)
9457 959594960 : && handled_component_p (TREE_OPERAND (op0, 0)))
9458 : {
9459 53914147 : poly_int64 bitsize, bitpos;
9460 53914147 : tree offset;
9461 53914147 : machine_mode mode;
9462 53914147 : int unsignedp, reversep, volatilep;
9463 53914147 : tree base
9464 53914147 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9465 : &offset, &mode, &unsignedp, &reversep,
9466 : &volatilep);
9467 : /* If the reference was to a (constant) zero offset, we can use
9468 : the address of the base if it has the same base type
9469 : as the result type and the pointer type is unqualified. */
9470 53914147 : if (!offset
9471 53767238 : && known_eq (bitpos, 0)
9472 37085692 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9473 37085692 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9474 107681385 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9475 12913 : return fold_convert_loc (loc, type,
9476 12913 : build_fold_addr_expr_loc (loc, base));
9477 : }
9478 :
9479 714603190 : if (TREE_CODE (op0) == MODIFY_EXPR
9480 289685 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9481 : /* Detect assigning a bitfield. */
9482 714605245 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9483 116 : && DECL_BIT_FIELD
9484 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9485 : {
9486 : /* Don't leave an assignment inside a conversion
9487 : unless assigning a bitfield. */
9488 2007 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9489 : /* First do the assignment, then return converted constant. */
9490 2007 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9491 2007 : suppress_warning (tem /* What warning? */);
9492 2007 : TREE_USED (tem) = 1;
9493 2007 : return tem;
9494 : }
9495 :
9496 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9497 : constants (if x has signed type, the sign bit cannot be set
9498 : in c). This folds extension into the BIT_AND_EXPR.
9499 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9500 : very likely don't have maximal range for their precision and this
9501 : transformation effectively doesn't preserve non-maximal ranges. */
9502 714601183 : if (TREE_CODE (type) == INTEGER_TYPE
9503 247498739 : && TREE_CODE (op0) == BIT_AND_EXPR
9504 715210934 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9505 : {
9506 219187 : tree and_expr = op0;
9507 219187 : tree and0 = TREE_OPERAND (and_expr, 0);
9508 219187 : tree and1 = TREE_OPERAND (and_expr, 1);
9509 219187 : int change = 0;
9510 :
9511 219187 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9512 219187 : || (TYPE_PRECISION (type)
9513 59437 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9514 : change = 1;
9515 18049 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9516 : <= HOST_BITS_PER_WIDE_INT
9517 18049 : && tree_fits_uhwi_p (and1))
9518 : {
9519 16843 : unsigned HOST_WIDE_INT cst;
9520 :
9521 16843 : cst = tree_to_uhwi (and1);
9522 33686 : cst &= HOST_WIDE_INT_M1U
9523 16843 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9524 16843 : change = (cst == 0);
9525 16843 : if (change
9526 16843 : && !flag_syntax_only
9527 33686 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9528 : == ZERO_EXTEND))
9529 : {
9530 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9531 : and0 = fold_convert_loc (loc, uns, and0);
9532 : and1 = fold_convert_loc (loc, uns, and1);
9533 : }
9534 : }
9535 16843 : if (change)
9536 : {
9537 217981 : tree and1_type = TREE_TYPE (and1);
9538 217981 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9539 : TYPE_PRECISION (type));
9540 217981 : tem = force_fit_type (type,
9541 217981 : wide_int::from (wi::to_wide (and1), prec,
9542 217981 : TYPE_SIGN (and1_type)),
9543 217981 : 0, TREE_OVERFLOW (and1));
9544 217981 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9545 217981 : fold_convert_loc (loc, type, and0), tem);
9546 : }
9547 : }
9548 :
9549 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9550 : cast (T1)X will fold away. We assume that this happens when X itself
9551 : is a cast. */
9552 714383202 : if (POINTER_TYPE_P (type)
9553 432024339 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9554 748221997 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9555 : {
9556 31256032 : tree arg00 = TREE_OPERAND (arg0, 0);
9557 31256032 : tree arg01 = TREE_OPERAND (arg0, 1);
9558 :
9559 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9560 : when the pointed type needs higher alignment than
9561 : the p+ first operand's pointed type. */
9562 31256032 : if (!in_gimple_form
9563 31237674 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9564 31257250 : && (min_align_of_type (TREE_TYPE (type))
9565 609 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9566 : return NULL_TREE;
9567 :
9568 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9569 : when type is a reference type and arg00's type is not,
9570 : because arg00 could be validly nullptr and if arg01 doesn't return,
9571 : we don't want false positive binding of reference to nullptr. */
9572 31255965 : if (TREE_CODE (type) == REFERENCE_TYPE
9573 17569184 : && !in_gimple_form
9574 17569163 : && sanitize_flags_p (SANITIZE_NULL)
9575 31256396 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9576 : return NULL_TREE;
9577 :
9578 31255534 : arg00 = fold_convert_loc (loc, type, arg00);
9579 31255534 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9580 : }
9581 :
9582 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9583 : of the same precision, and X is an integer type not narrower than
9584 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9585 683127170 : if (INTEGRAL_TYPE_P (type)
9586 253716084 : && TREE_CODE (op0) == BIT_NOT_EXPR
9587 578331 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9588 578331 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9589 683508069 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9590 : {
9591 378493 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9592 451902 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9593 451900 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9594 313648 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9595 313648 : fold_convert_loc (loc, type, tem));
9596 : }
9597 :
9598 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9599 : type of X and Y (integer types only). */
9600 682813522 : if (INTEGRAL_TYPE_P (type)
9601 253402436 : && TREE_CODE (op0) == MULT_EXPR
9602 9326890 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9603 9306192 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9604 682876437 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9605 22325 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9606 : {
9607 : /* Be careful not to introduce new overflows. */
9608 62861 : tree mult_type;
9609 62861 : if (TYPE_OVERFLOW_WRAPS (type))
9610 : mult_type = type;
9611 : else
9612 2098 : mult_type = unsigned_type_for (type);
9613 :
9614 62861 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9615 : {
9616 125722 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9617 : fold_convert_loc (loc, mult_type,
9618 62861 : TREE_OPERAND (op0, 0)),
9619 : fold_convert_loc (loc, mult_type,
9620 62861 : TREE_OPERAND (op0, 1)));
9621 62861 : return fold_convert_loc (loc, type, tem);
9622 : }
9623 : }
9624 :
9625 : return NULL_TREE;
9626 :
9627 240177687 : case VIEW_CONVERT_EXPR:
9628 240177687 : if (TREE_CODE (op0) == MEM_REF)
9629 : {
9630 2749 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9631 21 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9632 2749 : tem = fold_build2_loc (loc, MEM_REF, type,
9633 2749 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9634 2749 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9635 2749 : return tem;
9636 : }
9637 :
9638 : return NULL_TREE;
9639 :
9640 4248764 : case NEGATE_EXPR:
9641 4248764 : tem = fold_negate_expr (loc, arg0);
9642 4248764 : if (tem)
9643 1683 : return fold_convert_loc (loc, type, tem);
9644 : return NULL_TREE;
9645 :
9646 3455309 : case ABS_EXPR:
9647 : /* Convert fabs((double)float) into (double)fabsf(float). */
9648 3455309 : if (TREE_CODE (arg0) == NOP_EXPR
9649 23943 : && TREE_CODE (type) == REAL_TYPE)
9650 : {
9651 23889 : tree targ0 = strip_float_extensions (arg0);
9652 23889 : if (targ0 != arg0)
9653 23685 : return fold_convert_loc (loc, type,
9654 : fold_build1_loc (loc, ABS_EXPR,
9655 23685 : TREE_TYPE (targ0),
9656 23685 : targ0));
9657 : }
9658 : return NULL_TREE;
9659 :
9660 2877659 : case BIT_NOT_EXPR:
9661 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9662 2877659 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9663 2879351 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9664 : fold_convert_loc (loc, type,
9665 1692 : TREE_OPERAND (arg0, 0)))))
9666 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9667 : fold_convert_loc (loc, type,
9668 28 : TREE_OPERAND (arg0, 1)));
9669 2877645 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9670 2879323 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9671 : fold_convert_loc (loc, type,
9672 1678 : TREE_OPERAND (arg0, 1)))))
9673 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9674 : fold_convert_loc (loc, type,
9675 46 : TREE_OPERAND (arg0, 0)), tem);
9676 :
9677 : return NULL_TREE;
9678 :
9679 51281910 : case TRUTH_NOT_EXPR:
9680 : /* Note that the operand of this must be an int
9681 : and its values must be 0 or 1.
9682 : ("true" is a fixed value perhaps depending on the language,
9683 : but we don't handle values other than 1 correctly yet.) */
9684 51281910 : tem = fold_truth_not_expr (loc, arg0);
9685 51281910 : if (!tem)
9686 : return NULL_TREE;
9687 34729899 : return fold_convert_loc (loc, type, tem);
9688 :
9689 69418442 : case INDIRECT_REF:
9690 : /* Fold *&X to X if X is an lvalue. */
9691 69418442 : if (TREE_CODE (op0) == ADDR_EXPR)
9692 : {
9693 8765 : tree op00 = TREE_OPERAND (op0, 0);
9694 8765 : if ((VAR_P (op00)
9695 : || TREE_CODE (op00) == PARM_DECL
9696 : || TREE_CODE (op00) == RESULT_DECL)
9697 7619 : && !TREE_READONLY (op00))
9698 7502 : return op00;
9699 : }
9700 : return NULL_TREE;
9701 :
9702 : default:
9703 : return NULL_TREE;
9704 : } /* switch (code) */
9705 : }
9706 :
9707 :
9708 : /* If the operation was a conversion do _not_ mark a resulting constant
9709 : with TREE_OVERFLOW if the original constant was not. These conversions
9710 : have implementation defined behavior and retaining the TREE_OVERFLOW
9711 : flag here would confuse later passes such as VRP. */
9712 : tree
9713 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9714 : tree type, tree op0)
9715 : {
9716 0 : tree res = fold_unary_loc (loc, code, type, op0);
9717 0 : if (res
9718 0 : && TREE_CODE (res) == INTEGER_CST
9719 0 : && TREE_CODE (op0) == INTEGER_CST
9720 0 : && CONVERT_EXPR_CODE_P (code))
9721 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9722 :
9723 0 : return res;
9724 : }
9725 :
9726 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9727 : operands OP0 and OP1. LOC is the location of the resulting expression.
9728 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9729 : Return the folded expression if folding is successful. Otherwise,
9730 : return NULL_TREE. */
9731 : static tree
9732 25956325 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9733 : tree arg0, tree arg1, tree op0, tree op1)
9734 : {
9735 25956325 : tree tem;
9736 :
9737 : /* We only do these simplifications if we are optimizing. */
9738 25956325 : if (!optimize)
9739 : return NULL_TREE;
9740 :
9741 : /* Check for things like (A || B) && (A || C). We can convert this
9742 : to A || (B && C). Note that either operator can be any of the four
9743 : truth and/or operations and the transformation will still be
9744 : valid. Also note that we only care about order for the
9745 : ANDIF and ORIF operators. If B contains side effects, this
9746 : might change the truth-value of A. */
9747 25456761 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9748 5929997 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9749 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9750 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9751 5929997 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9752 25519352 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9753 : {
9754 62076 : tree a00 = TREE_OPERAND (arg0, 0);
9755 62076 : tree a01 = TREE_OPERAND (arg0, 1);
9756 62076 : tree a10 = TREE_OPERAND (arg1, 0);
9757 62076 : tree a11 = TREE_OPERAND (arg1, 1);
9758 124152 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9759 62076 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9760 62076 : && (code == TRUTH_AND_EXPR
9761 23095 : || code == TRUTH_OR_EXPR));
9762 :
9763 62076 : if (operand_equal_p (a00, a10, 0))
9764 837 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9765 837 : fold_build2_loc (loc, code, type, a01, a11));
9766 61239 : else if (commutative && operand_equal_p (a00, a11, 0))
9767 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9768 0 : fold_build2_loc (loc, code, type, a01, a10));
9769 61239 : else if (commutative && operand_equal_p (a01, a10, 0))
9770 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9771 0 : fold_build2_loc (loc, code, type, a00, a11));
9772 :
9773 : /* This case if tricky because we must either have commutative
9774 : operators or else A10 must not have side-effects. */
9775 :
9776 61186 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9777 121897 : && operand_equal_p (a01, a11, 0))
9778 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9779 : fold_build2_loc (loc, code, type, a00, a10),
9780 43 : a01);
9781 : }
9782 :
9783 : /* See if we can build a range comparison. */
9784 25455881 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9785 : return tem;
9786 :
9787 24290531 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9788 24288561 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9789 : {
9790 40055 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9791 40055 : if (tem)
9792 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9793 : }
9794 :
9795 24290518 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9796 24279448 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9797 : {
9798 89589 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9799 89589 : if (tem)
9800 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9801 : }
9802 :
9803 : /* Check for the possibility of merging component references. If our
9804 : lhs is another similar operation, try to merge its rhs with our
9805 : rhs. Then try to merge our lhs and rhs. */
9806 24290427 : if (TREE_CODE (arg0) == code
9807 25133953 : && (tem = fold_truth_andor_1 (loc, code, type,
9808 843526 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9809 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9810 :
9811 24290342 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9812 : return tem;
9813 :
9814 24250137 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9815 24250137 : if (param_logical_op_non_short_circuit != -1)
9816 7729 : logical_op_non_short_circuit
9817 7729 : = param_logical_op_non_short_circuit;
9818 24250137 : if (logical_op_non_short_circuit
9819 24246227 : && !sanitize_coverage_p ()
9820 24250137 : && (code == TRUTH_AND_EXPR
9821 24246224 : || code == TRUTH_ANDIF_EXPR
9822 11204766 : || code == TRUTH_OR_EXPR
9823 11204766 : || code == TRUTH_ORIF_EXPR))
9824 : {
9825 24246224 : enum tree_code ncode, icode;
9826 :
9827 24246224 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9828 24246224 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9829 11204766 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9830 :
9831 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9832 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9833 : We don't want to pack more than two leafs to a non-IF AND/OR
9834 : expression.
9835 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9836 : equal to IF-CODE, then we don't want to add right-hand operand.
9837 : If the inner right-hand side of left-hand operand has
9838 : side-effects, or isn't simple, then we can't add to it,
9839 : as otherwise we might destroy if-sequence. */
9840 24246224 : if (TREE_CODE (arg0) == icode
9841 834468 : && simple_condition_p (arg1)
9842 : /* Needed for sequence points to handle trappings, and
9843 : side-effects. */
9844 24294076 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9845 : {
9846 40869 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9847 : arg1);
9848 40869 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9849 40869 : tem);
9850 : }
9851 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9852 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9853 24205355 : else if (TREE_CODE (arg1) == icode
9854 6566 : && simple_condition_p (arg0)
9855 : /* Needed for sequence points to handle trappings, and
9856 : side-effects. */
9857 24206320 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9858 : {
9859 39 : tem = fold_build2_loc (loc, ncode, type,
9860 39 : arg0, TREE_OPERAND (arg1, 0));
9861 39 : return fold_build2_loc (loc, icode, type, tem,
9862 78 : TREE_OPERAND (arg1, 1));
9863 : }
9864 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9865 : into (A OR B).
9866 : For sequence point consistency, we need to check for trapping,
9867 : and side-effects. */
9868 5314003 : else if (code == icode && simple_condition_p (arg0)
9869 25089276 : && simple_condition_p (arg1))
9870 439730 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9871 : }
9872 :
9873 : return NULL_TREE;
9874 : }
9875 :
9876 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9877 : by changing CODE to reduce the magnitude of constants involved in
9878 : ARG0 of the comparison.
9879 : Returns a canonicalized comparison tree if a simplification was
9880 : possible, otherwise returns NULL_TREE. */
9881 :
9882 : static tree
9883 182429553 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9884 : tree arg0, tree arg1)
9885 : {
9886 182429553 : enum tree_code code0 = TREE_CODE (arg0);
9887 182429553 : tree t, cst0 = NULL_TREE;
9888 182429553 : int sgn0;
9889 :
9890 : /* Match A +- CST code arg1. We can change this only if overflow
9891 : is undefined. */
9892 182429553 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9893 139609107 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9894 : /* In principle pointers also have undefined overflow behavior,
9895 : but that causes problems elsewhere. */
9896 68870278 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9897 68870278 : && (code0 == MINUS_EXPR
9898 68870278 : || code0 == PLUS_EXPR)
9899 2678365 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9900 : return NULL_TREE;
9901 :
9902 : /* Identify the constant in arg0 and its sign. */
9903 2204374 : cst0 = TREE_OPERAND (arg0, 1);
9904 2204374 : sgn0 = tree_int_cst_sgn (cst0);
9905 :
9906 : /* Overflowed constants and zero will cause problems. */
9907 2204374 : if (integer_zerop (cst0)
9908 2204374 : || TREE_OVERFLOW (cst0))
9909 : return NULL_TREE;
9910 :
9911 : /* See if we can reduce the magnitude of the constant in
9912 : arg0 by changing the comparison code. */
9913 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9914 2204374 : if (code == LT_EXPR
9915 1302480 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9916 : code = LE_EXPR;
9917 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9918 2017663 : else if (code == GT_EXPR
9919 612568 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9920 : code = GE_EXPR;
9921 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9922 1821372 : else if (code == LE_EXPR
9923 686235 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9924 : code = LT_EXPR;
9925 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9926 1576857 : else if (code == GE_EXPR
9927 490773 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9928 : code = GT_EXPR;
9929 : else
9930 : return NULL_TREE;
9931 :
9932 : /* Now build the constant reduced in magnitude. But not if that
9933 : would produce one outside of its types range. */
9934 1665592 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9935 1665592 : && ((sgn0 == 1
9936 440668 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9937 440668 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9938 832796 : || (sgn0 == -1
9939 392128 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9940 392128 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9941 : return NULL_TREE;
9942 :
9943 1273464 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9944 832796 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9945 832796 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9946 832796 : t = fold_convert (TREE_TYPE (arg1), t);
9947 :
9948 832796 : return fold_build2_loc (loc, code, type, t, arg1);
9949 : }
9950 :
9951 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9952 : overflow further. Try to decrease the magnitude of constants involved
9953 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9954 : and put sole constants at the second argument position.
9955 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9956 :
9957 : static tree
9958 91609071 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9959 : tree arg0, tree arg1)
9960 : {
9961 91609071 : tree t;
9962 :
9963 : /* Try canonicalization by simplifying arg0. */
9964 91609071 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9965 91609071 : if (t)
9966 : return t;
9967 :
9968 : /* Try canonicalization by simplifying arg1 using the swapped
9969 : comparison. */
9970 90820482 : code = swap_tree_comparison (code);
9971 90820482 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9972 90820482 : return t;
9973 : }
9974 :
9975 : /* Return a positive integer when the symbol DECL is known to have
9976 : a nonzero address, zero when it's known not to (e.g., it's a weak
9977 : symbol), and a negative integer when the symbol is not yet in the
9978 : symbol table and so whether or not its address is zero is unknown.
9979 : For function local objects always return positive integer. */
9980 : static int
9981 12309556 : maybe_nonzero_address (tree decl)
9982 : {
9983 12309556 : if (!DECL_P (decl))
9984 : return -1;
9985 :
9986 : /* Normally, don't do anything for variables and functions before symtab is
9987 : built; it is quite possible that DECL will be declared weak later.
9988 : But if folding_initializer, we need a constant answer now, so create
9989 : the symtab entry and prevent later weak declaration. */
9990 10082856 : if (decl_in_symtab_p (decl))
9991 : {
9992 4400382 : if (struct symtab_node *symbol
9993 4400382 : = (folding_initializer
9994 4400382 : ? symtab_node::get_create (decl)
9995 4383057 : : symtab_node::get (decl)))
9996 4381351 : return symbol->nonzero_address ();
9997 : }
9998 5682474 : else if (folding_cxx_constexpr)
9999 : /* Anything that doesn't go in the symtab has non-zero address. */
10000 : return 1;
10001 :
10002 : /* Function local objects are never NULL. */
10003 5545441 : if (DECL_CONTEXT (decl)
10004 5527888 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
10005 11069866 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
10006 5448979 : return 1;
10007 :
10008 : return -1;
10009 : }
10010 :
10011 : /* Subroutine of fold_binary. This routine performs all of the
10012 : transformations that are common to the equality/inequality
10013 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
10014 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10015 : fold_binary should call fold_binary. Fold a comparison with
10016 : tree code CODE and type TYPE with operands OP0 and OP1. Return
10017 : the folded comparison or NULL_TREE. */
10018 :
10019 : static tree
10020 91680230 : fold_comparison (location_t loc, enum tree_code code, tree type,
10021 : tree op0, tree op1)
10022 : {
10023 91680230 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
10024 91680230 : tree arg0, arg1, tem;
10025 :
10026 91680230 : arg0 = op0;
10027 91680230 : arg1 = op1;
10028 :
10029 91680230 : STRIP_SIGN_NOPS (arg0);
10030 91680230 : STRIP_SIGN_NOPS (arg1);
10031 :
10032 : /* For comparisons of pointers we can decompose it to a compile time
10033 : comparison of the base objects and the offsets into the object.
10034 : This requires at least one operand being an ADDR_EXPR or a
10035 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10036 169887217 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
10037 91897227 : && (TREE_CODE (arg0) == ADDR_EXPR
10038 13638935 : || TREE_CODE (arg1) == ADDR_EXPR
10039 12118896 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10040 11353120 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
10041 : {
10042 2347158 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
10043 2347158 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
10044 2347158 : machine_mode mode;
10045 2347158 : int volatilep, reversep, unsignedp;
10046 2347158 : bool indirect_base0 = false, indirect_base1 = false;
10047 :
10048 : /* Get base and offset for the access. Strip ADDR_EXPR for
10049 : get_inner_reference, but put it back by stripping INDIRECT_REF
10050 : off the base object if possible. indirect_baseN will be true
10051 : if baseN is not an address but refers to the object itself. */
10052 2347158 : base0 = arg0;
10053 2347158 : if (TREE_CODE (arg0) == ADDR_EXPR)
10054 : {
10055 51305 : base0
10056 51305 : = get_inner_reference (TREE_OPERAND (arg0, 0),
10057 : &bitsize, &bitpos0, &offset0, &mode,
10058 : &unsignedp, &reversep, &volatilep);
10059 51305 : if (INDIRECT_REF_P (base0))
10060 1980 : base0 = TREE_OPERAND (base0, 0);
10061 : else
10062 : indirect_base0 = true;
10063 : }
10064 2295853 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10065 : {
10066 827435 : base0 = TREE_OPERAND (arg0, 0);
10067 827435 : STRIP_SIGN_NOPS (base0);
10068 827435 : if (TREE_CODE (base0) == ADDR_EXPR)
10069 : {
10070 27460 : base0
10071 27460 : = get_inner_reference (TREE_OPERAND (base0, 0),
10072 : &bitsize, &bitpos0, &offset0, &mode,
10073 : &unsignedp, &reversep, &volatilep);
10074 27460 : if (INDIRECT_REF_P (base0))
10075 20 : base0 = TREE_OPERAND (base0, 0);
10076 : else
10077 : indirect_base0 = true;
10078 : }
10079 827435 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10080 827435 : offset0 = TREE_OPERAND (arg0, 1);
10081 : else
10082 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10083 : TREE_OPERAND (arg0, 1));
10084 827435 : if (poly_int_tree_p (offset0))
10085 : {
10086 661224 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10087 661224 : TYPE_PRECISION (sizetype));
10088 661224 : tem <<= LOG2_BITS_PER_UNIT;
10089 661224 : tem += bitpos0;
10090 661224 : if (tem.to_shwi (&bitpos0))
10091 661224 : offset0 = NULL_TREE;
10092 : }
10093 : }
10094 :
10095 2347158 : base1 = arg1;
10096 2347158 : if (TREE_CODE (arg1) == ADDR_EXPR)
10097 : {
10098 1549942 : base1
10099 1549942 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10100 : &bitsize, &bitpos1, &offset1, &mode,
10101 : &unsignedp, &reversep, &volatilep);
10102 1549942 : if (INDIRECT_REF_P (base1))
10103 69383 : base1 = TREE_OPERAND (base1, 0);
10104 : else
10105 : indirect_base1 = true;
10106 : }
10107 797216 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10108 : {
10109 84995 : base1 = TREE_OPERAND (arg1, 0);
10110 84995 : STRIP_SIGN_NOPS (base1);
10111 84995 : if (TREE_CODE (base1) == ADDR_EXPR)
10112 : {
10113 11616 : base1
10114 11616 : = get_inner_reference (TREE_OPERAND (base1, 0),
10115 : &bitsize, &bitpos1, &offset1, &mode,
10116 : &unsignedp, &reversep, &volatilep);
10117 11616 : if (INDIRECT_REF_P (base1))
10118 0 : base1 = TREE_OPERAND (base1, 0);
10119 : else
10120 : indirect_base1 = true;
10121 : }
10122 84995 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10123 84971 : offset1 = TREE_OPERAND (arg1, 1);
10124 : else
10125 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10126 : TREE_OPERAND (arg1, 1));
10127 84995 : if (poly_int_tree_p (offset1))
10128 : {
10129 74204 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10130 74204 : TYPE_PRECISION (sizetype));
10131 74204 : tem <<= LOG2_BITS_PER_UNIT;
10132 74204 : tem += bitpos1;
10133 74204 : if (tem.to_shwi (&bitpos1))
10134 74204 : offset1 = NULL_TREE;
10135 : }
10136 : }
10137 :
10138 : /* If we have equivalent bases we might be able to simplify. */
10139 2347158 : if (indirect_base0 == indirect_base1
10140 3161815 : && operand_equal_p (base0, base1,
10141 : indirect_base0 ? OEP_ADDRESS_OF : 0))
10142 : {
10143 : /* We can fold this expression to a constant if the non-constant
10144 : offset parts are equal. */
10145 17084 : if ((offset0 == offset1
10146 6749 : || (offset0 && offset1
10147 2742 : && operand_equal_p (offset0, offset1, 0)))
10148 17084 : && (equality_code
10149 10291 : || (indirect_base0
10150 6392 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10151 3899 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10152 : {
10153 10280 : switch (code)
10154 : {
10155 35 : case EQ_EXPR:
10156 35 : if (known_eq (bitpos0, bitpos1))
10157 47602 : return constant_boolean_node (true, type);
10158 21 : if (known_ne (bitpos0, bitpos1))
10159 21 : return constant_boolean_node (false, type);
10160 : break;
10161 9 : case NE_EXPR:
10162 9 : if (known_ne (bitpos0, bitpos1))
10163 7 : return constant_boolean_node (true, type);
10164 2 : if (known_eq (bitpos0, bitpos1))
10165 2 : return constant_boolean_node (false, type);
10166 : break;
10167 2331 : case LT_EXPR:
10168 2331 : if (known_lt (bitpos0, bitpos1))
10169 2187 : return constant_boolean_node (true, type);
10170 144 : if (known_ge (bitpos0, bitpos1))
10171 144 : return constant_boolean_node (false, type);
10172 : break;
10173 1854 : case LE_EXPR:
10174 1854 : if (known_le (bitpos0, bitpos1))
10175 171 : return constant_boolean_node (true, type);
10176 1683 : if (known_gt (bitpos0, bitpos1))
10177 1683 : return constant_boolean_node (false, type);
10178 : break;
10179 3660 : case GE_EXPR:
10180 3660 : if (known_ge (bitpos0, bitpos1))
10181 1656 : return constant_boolean_node (true, type);
10182 2004 : if (known_lt (bitpos0, bitpos1))
10183 2004 : return constant_boolean_node (false, type);
10184 : break;
10185 2391 : case GT_EXPR:
10186 2391 : if (known_gt (bitpos0, bitpos1))
10187 2332 : return constant_boolean_node (true, type);
10188 59 : if (known_le (bitpos0, bitpos1))
10189 59 : return constant_boolean_node (false, type);
10190 : break;
10191 : default:;
10192 : }
10193 : }
10194 : /* We can simplify the comparison to a comparison of the variable
10195 : offset parts if the constant offset parts are equal.
10196 : Be careful to use signed sizetype here because otherwise we
10197 : mess with array offsets in the wrong way. This is possible
10198 : because pointer arithmetic is restricted to retain within an
10199 : object and overflow on pointer differences is undefined as of
10200 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10201 6804 : else if (known_eq (bitpos0, bitpos1)
10202 6804 : && (equality_code
10203 5315 : || (indirect_base0
10204 285 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10205 5030 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10206 : {
10207 : /* By converting to signed sizetype we cover middle-end pointer
10208 : arithmetic which operates on unsigned pointer types of size
10209 : type size and ARRAY_REF offsets which are properly sign or
10210 : zero extended from their type in case it is narrower than
10211 : sizetype. */
10212 5424 : if (offset0 == NULL_TREE)
10213 0 : offset0 = build_int_cst (ssizetype, 0);
10214 : else
10215 5424 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10216 5424 : if (offset1 == NULL_TREE)
10217 2714 : offset1 = build_int_cst (ssizetype, 0);
10218 : else
10219 2710 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10220 :
10221 5424 : return fold_build2_loc (loc, code, type, offset0, offset1);
10222 : }
10223 : }
10224 : /* For equal offsets we can simplify to a comparison of the
10225 : base addresses. */
10226 2330074 : else if (known_eq (bitpos0, bitpos1)
10227 53585 : && (indirect_base0
10228 996033 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10229 15219 : && (indirect_base1
10230 188966 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10231 2555319 : && ((offset0 == offset1)
10232 4487 : || (offset0 && offset1
10233 4217 : && operand_equal_p (offset0, offset1, 0))))
10234 : {
10235 31793 : if (indirect_base0)
10236 2747 : base0 = build_fold_addr_expr_loc (loc, base0);
10237 31793 : if (indirect_base1)
10238 4350 : base1 = build_fold_addr_expr_loc (loc, base1);
10239 31793 : return fold_build2_loc (loc, code, type, base0, base1);
10240 : }
10241 : /* Comparison between an ordinary (non-weak) symbol and a null
10242 : pointer can be eliminated since such symbols must have a non
10243 : null address. In C, relational expressions between pointers
10244 : to objects and null pointers are undefined. The results
10245 : below follow the C++ rules with the additional property that
10246 : every object pointer compares greater than a null pointer.
10247 : */
10248 2298281 : else if (((DECL_P (base0)
10249 252021 : && maybe_nonzero_address (base0) > 0
10250 : /* Avoid folding references to struct members at offset 0 to
10251 : prevent tests like '&ptr->firstmember == 0' from getting
10252 : eliminated. When ptr is null, although the -> expression
10253 : is strictly speaking invalid, GCC retains it as a matter
10254 : of QoI. See PR c/44555. */
10255 238905 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10256 2282785 : || CONSTANT_CLASS_P (base0))
10257 20278 : && indirect_base0
10258 : /* The caller guarantees that when one of the arguments is
10259 : constant (i.e., null in this case) it is second. */
10260 2316045 : && integer_zerop (arg1))
10261 : {
10262 105 : switch (code)
10263 : {
10264 24 : case EQ_EXPR:
10265 24 : case LE_EXPR:
10266 24 : case LT_EXPR:
10267 24 : return constant_boolean_node (false, type);
10268 81 : case GE_EXPR:
10269 81 : case GT_EXPR:
10270 81 : case NE_EXPR:
10271 81 : return constant_boolean_node (true, type);
10272 0 : default:
10273 0 : gcc_unreachable ();
10274 : }
10275 : }
10276 : }
10277 :
10278 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10279 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10280 : the resulting offset is smaller in absolute value than the
10281 : original one and has the same sign. */
10282 180485938 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10283 140444450 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10284 34852994 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10285 2414159 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10286 1994634 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10287 1994634 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10288 162030734 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10289 173580 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10290 : {
10291 173580 : tree const1 = TREE_OPERAND (arg0, 1);
10292 173580 : tree const2 = TREE_OPERAND (arg1, 1);
10293 173580 : tree variable1 = TREE_OPERAND (arg0, 0);
10294 173580 : tree variable2 = TREE_OPERAND (arg1, 0);
10295 173580 : tree cst;
10296 :
10297 : /* Put the constant on the side where it doesn't overflow and is
10298 : of lower absolute value and of same sign than before. */
10299 173581 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10300 : ? MINUS_EXPR : PLUS_EXPR,
10301 : const2, const1);
10302 173580 : if (!TREE_OVERFLOW (cst)
10303 173556 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10304 197137 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10305 6164 : return fold_build2_loc (loc, code, type,
10306 : variable1,
10307 6164 : fold_build2_loc (loc, TREE_CODE (arg1),
10308 6164 : TREE_TYPE (arg1),
10309 6164 : variable2, cst));
10310 :
10311 167417 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10312 : ? MINUS_EXPR : PLUS_EXPR,
10313 : const1, const2);
10314 167416 : if (!TREE_OVERFLOW (cst)
10315 167392 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10316 184809 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10317 17393 : return fold_build2_loc (loc, code, type,
10318 17393 : fold_build2_loc (loc, TREE_CODE (arg0),
10319 17393 : TREE_TYPE (arg0),
10320 : variable1, cst),
10321 17393 : variable2);
10322 : }
10323 :
10324 91609071 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10325 91609071 : if (tem)
10326 : return tem;
10327 :
10328 : /* If we are comparing an expression that just has comparisons
10329 : of two integer values, arithmetic expressions of those comparisons,
10330 : and constants, we can simplify it. There are only three cases
10331 : to check: the two values can either be equal, the first can be
10332 : greater, or the second can be greater. Fold the expression for
10333 : those three values. Since each value must be 0 or 1, we have
10334 : eight possibilities, each of which corresponds to the constant 0
10335 : or 1 or one of the six possible comparisons.
10336 :
10337 : This handles common cases like (a > b) == 0 but also handles
10338 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10339 : occur in macroized code. */
10340 :
10341 90776275 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10342 : {
10343 55463606 : tree cval1 = 0, cval2 = 0;
10344 :
10345 55463606 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10346 : /* Don't handle degenerate cases here; they should already
10347 : have been handled anyway. */
10348 618312 : && cval1 != 0 && cval2 != 0
10349 617018 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10350 617018 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10351 617012 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10352 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10353 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10354 55463660 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10355 54 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10356 : {
10357 54 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10358 54 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10359 :
10360 : /* We can't just pass T to eval_subst in case cval1 or cval2
10361 : was the same as ARG1. */
10362 :
10363 54 : tree high_result
10364 54 : = fold_build2_loc (loc, code, type,
10365 : eval_subst (loc, arg0, cval1, maxval,
10366 : cval2, minval),
10367 : arg1);
10368 54 : tree equal_result
10369 54 : = fold_build2_loc (loc, code, type,
10370 : eval_subst (loc, arg0, cval1, maxval,
10371 : cval2, maxval),
10372 : arg1);
10373 54 : tree low_result
10374 54 : = fold_build2_loc (loc, code, type,
10375 : eval_subst (loc, arg0, cval1, minval,
10376 : cval2, maxval),
10377 : arg1);
10378 :
10379 : /* All three of these results should be 0 or 1. Confirm they are.
10380 : Then use those values to select the proper code to use. */
10381 :
10382 54 : if (TREE_CODE (high_result) == INTEGER_CST
10383 54 : && TREE_CODE (equal_result) == INTEGER_CST
10384 44 : && TREE_CODE (low_result) == INTEGER_CST)
10385 : {
10386 : /* Make a 3-bit mask with the high-order bit being the
10387 : value for `>', the next for '=', and the low for '<'. */
10388 44 : switch ((integer_onep (high_result) * 4)
10389 44 : + (integer_onep (equal_result) * 2)
10390 44 : + integer_onep (low_result))
10391 : {
10392 21 : case 0:
10393 : /* Always false. */
10394 44 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10395 : case 1:
10396 : code = LT_EXPR;
10397 : break;
10398 2 : case 2:
10399 2 : code = EQ_EXPR;
10400 2 : break;
10401 0 : case 3:
10402 0 : code = LE_EXPR;
10403 0 : break;
10404 0 : case 4:
10405 0 : code = GT_EXPR;
10406 0 : break;
10407 5 : case 5:
10408 5 : code = NE_EXPR;
10409 5 : break;
10410 0 : case 6:
10411 0 : code = GE_EXPR;
10412 0 : break;
10413 16 : case 7:
10414 : /* Always true. */
10415 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10416 : }
10417 :
10418 7 : return fold_build2_loc (loc, code, type, cval1, cval2);
10419 : }
10420 : }
10421 : }
10422 :
10423 : return NULL_TREE;
10424 : }
10425 :
10426 :
10427 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10428 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10429 : argument EXPR represents the expression "z" of type TYPE. */
10430 :
10431 : static tree
10432 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10433 : {
10434 2 : tree itype = TREE_TYPE (type);
10435 2 : tree rpart, ipart, tem;
10436 :
10437 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10438 : {
10439 0 : rpart = TREE_OPERAND (expr, 0);
10440 0 : ipart = TREE_OPERAND (expr, 1);
10441 : }
10442 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10443 : {
10444 0 : rpart = TREE_REALPART (expr);
10445 0 : ipart = TREE_IMAGPART (expr);
10446 : }
10447 : else
10448 : {
10449 2 : expr = save_expr (expr);
10450 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10451 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10452 : }
10453 :
10454 2 : rpart = save_expr (rpart);
10455 2 : ipart = save_expr (ipart);
10456 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10457 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10458 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10459 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10460 2 : build_zero_cst (itype));
10461 : }
10462 :
10463 :
10464 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10465 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10466 : true if successful. */
10467 :
10468 : static bool
10469 32010 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10470 : {
10471 32010 : unsigned HOST_WIDE_INT i, nunits;
10472 :
10473 32010 : if (TREE_CODE (arg) == VECTOR_CST
10474 32010 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10475 : {
10476 2200 : for (i = 0; i < nunits; ++i)
10477 1734 : elts[i] = VECTOR_CST_ELT (arg, i);
10478 : }
10479 31544 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10480 : {
10481 : constructor_elt *elt;
10482 :
10483 60001 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10484 48451 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10485 32010 : return false;
10486 : else
10487 22520 : elts[i] = elt->value;
10488 : }
10489 : else
10490 : return false;
10491 6461 : for (; i < nelts; i++)
10492 764 : elts[i]
10493 382 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10494 : return true;
10495 : }
10496 :
10497 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10498 : mask for VLA vec_perm folding.
10499 : REASON if specified, will contain the reason why SEL is not suitable.
10500 : Used only for debugging and unit-testing. */
10501 :
10502 : static bool
10503 30854 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10504 : const vec_perm_indices &sel,
10505 : const char **reason = NULL)
10506 : {
10507 30854 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10508 30854 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10509 :
10510 61708 : if (!(pow2p_hwi (sel_npatterns)
10511 30854 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10512 30854 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10513 : {
10514 0 : if (reason)
10515 0 : *reason = "npatterns is not power of 2";
10516 : return false;
10517 : }
10518 :
10519 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10520 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10521 30854 : poly_uint64 esel;
10522 30854 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10523 : {
10524 0 : if (reason)
10525 0 : *reason = "sel.length is not multiple of sel_npatterns";
10526 : return false;
10527 : }
10528 :
10529 30854 : if (sel_nelts_per_pattern < 3)
10530 : return true;
10531 :
10532 6397 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10533 : {
10534 4776 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10535 4776 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10536 4776 : HOST_WIDE_INT step;
10537 4776 : if (!poly_int64 (a2 - a1).is_constant (&step))
10538 : {
10539 : if (reason)
10540 : *reason = "step is not constant";
10541 30854 : return false;
10542 : }
10543 : // FIXME: Punt on step < 0 for now, revisit later.
10544 4776 : if (step < 0)
10545 : return false;
10546 4681 : if (step == 0)
10547 0 : continue;
10548 :
10549 4681 : if (!pow2p_hwi (step))
10550 : {
10551 0 : if (reason)
10552 0 : *reason = "step is not power of 2";
10553 : return false;
10554 : }
10555 :
10556 : /* Ensure that stepped sequence of the pattern selects elements
10557 : only from the same input vector. */
10558 4681 : uint64_t q1, qe;
10559 4681 : poly_uint64 r1, re;
10560 4681 : poly_uint64 ae = a1 + (esel - 2) * step;
10561 4681 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10562 :
10563 4681 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10564 4681 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10565 : && q1 == qe))
10566 : {
10567 445 : if (reason)
10568 0 : *reason = "crossed input vectors";
10569 : return false;
10570 : }
10571 :
10572 : /* Ensure that the stepped sequence always selects from the same
10573 : input pattern. */
10574 4236 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10575 4236 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10576 :
10577 4236 : if (!multiple_p (step, arg_npatterns))
10578 : {
10579 628 : if (reason)
10580 0 : *reason = "step is not multiple of npatterns";
10581 : return false;
10582 : }
10583 :
10584 : /* If a1 chooses base element from arg, ensure that it's a natural
10585 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10586 : to preserve arg's encoding. */
10587 :
10588 3608 : if (maybe_lt (r1, arg_npatterns))
10589 : {
10590 44 : unsigned HOST_WIDE_INT index;
10591 44 : if (!r1.is_constant (&index))
10592 30854 : return false;
10593 :
10594 44 : tree arg_elem0 = vector_cst_elt (arg, index);
10595 44 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10596 44 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10597 :
10598 44 : tree step1, step2;
10599 44 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10600 44 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10601 88 : || !operand_equal_p (step1, step2, 0))
10602 : {
10603 2 : if (reason)
10604 0 : *reason = "not a natural stepped sequence";
10605 : return false;
10606 : }
10607 : }
10608 : }
10609 :
10610 : return true;
10611 : }
10612 :
10613 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10614 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10615 : REASON has same purpose as described in
10616 : valid_mask_for_fold_vec_perm_cst_p. */
10617 :
10618 : static tree
10619 30854 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10620 : const char **reason = NULL)
10621 : {
10622 30854 : unsigned res_npatterns, res_nelts_per_pattern;
10623 30854 : unsigned HOST_WIDE_INT res_nelts;
10624 :
10625 : /* First try to implement the fold in a VLA-friendly way.
10626 :
10627 : (1) If the selector is simply a duplication of N elements, the
10628 : result is likewise a duplication of N elements.
10629 :
10630 : (2) If the selector is N elements followed by a duplication
10631 : of N elements, the result is too.
10632 :
10633 : (3) If the selector is N elements followed by an interleaving
10634 : of N linear series, the situation is more complex.
10635 :
10636 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10637 : can handle this case. If we can, then each of the N linear
10638 : series either (a) selects the same element each time or
10639 : (b) selects a linear series from one of the input patterns.
10640 :
10641 : If (b) holds for one of the linear series, the result
10642 : will contain a linear series, and so the result will have
10643 : the same shape as the selector. If (a) holds for all of
10644 : the linear series, the result will be the same as (2) above.
10645 :
10646 : (b) can only hold if one of the input patterns has a
10647 : stepped encoding. */
10648 :
10649 30854 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10650 : {
10651 29684 : res_npatterns = sel.encoding ().npatterns ();
10652 29684 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10653 29684 : if (res_nelts_per_pattern == 3
10654 1621 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10655 30760 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10656 : res_nelts_per_pattern = 2;
10657 29684 : res_nelts = res_npatterns * res_nelts_per_pattern;
10658 : }
10659 1170 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10660 : {
10661 1170 : res_npatterns = res_nelts;
10662 1170 : res_nelts_per_pattern = 1;
10663 : }
10664 : else
10665 : return NULL_TREE;
10666 :
10667 30854 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10668 185744 : for (unsigned i = 0; i < res_nelts; i++)
10669 : {
10670 124036 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10671 124036 : uint64_t q;
10672 124036 : poly_uint64 r;
10673 124036 : unsigned HOST_WIDE_INT index;
10674 :
10675 : /* Punt if sel[i] /trunc_div len cannot be determined,
10676 : because the input vector to be chosen will depend on
10677 : runtime vector length.
10678 : For example if len == 4 + 4x, and sel[i] == 4,
10679 : If len at runtime equals 4, we choose arg1[0].
10680 : For any other value of len > 4 at runtime, we choose arg0[4].
10681 : which makes the element choice dependent on runtime vector length. */
10682 124036 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10683 : {
10684 : if (reason)
10685 : *reason = "cannot divide selector element by arg len";
10686 : return NULL_TREE;
10687 : }
10688 :
10689 : /* sel[i] % len will give the index of element in the chosen input
10690 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10691 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10692 124036 : if (!r.is_constant (&index))
10693 : {
10694 : if (reason)
10695 : *reason = "remainder is not constant";
10696 : return NULL_TREE;
10697 : }
10698 :
10699 124036 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10700 124036 : tree elem = vector_cst_elt (arg, index);
10701 124036 : out_elts.quick_push (elem);
10702 : }
10703 :
10704 30854 : return out_elts.build ();
10705 30854 : }
10706 :
10707 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10708 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10709 : NULL_TREE otherwise. */
10710 :
10711 : tree
10712 70417 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10713 : {
10714 70417 : unsigned int i;
10715 70417 : unsigned HOST_WIDE_INT nelts;
10716 :
10717 70417 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10718 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10719 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10720 :
10721 70417 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10722 70417 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10723 : return NULL_TREE;
10724 :
10725 59808 : if (TREE_CODE (arg0) == VECTOR_CST
10726 31128 : && TREE_CODE (arg1) == VECTOR_CST)
10727 30854 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10728 :
10729 : /* For fall back case, we want to ensure we have VLS vectors
10730 : with equal length. */
10731 28954 : if (!sel.length ().is_constant (&nelts)
10732 28954 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10733 0 : return NULL_TREE;
10734 :
10735 28954 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10736 28954 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10737 28954 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10738 : return NULL_TREE;
10739 :
10740 3023 : vec<constructor_elt, va_gc> *v;
10741 3023 : vec_alloc (v, nelts);
10742 18232 : for (i = 0; i < nelts; i++)
10743 : {
10744 12186 : HOST_WIDE_INT index;
10745 12186 : if (!sel[i].is_constant (&index))
10746 : return NULL_TREE;
10747 12186 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10748 : }
10749 3023 : return build_constructor (type, v);
10750 : }
10751 :
10752 : /* Try to fold a pointer difference of type TYPE two address expressions of
10753 : array references AREF0 and AREF1 using location LOC. Return a
10754 : simplified expression for the difference or NULL_TREE. */
10755 :
10756 : static tree
10757 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10758 : tree aref0, tree aref1,
10759 : bool use_pointer_diff)
10760 : {
10761 39 : tree base0 = TREE_OPERAND (aref0, 0);
10762 39 : tree base1 = TREE_OPERAND (aref1, 0);
10763 39 : tree base_offset = build_int_cst (type, 0);
10764 :
10765 : /* If the bases are array references as well, recurse. If the bases
10766 : are pointer indirections compute the difference of the pointers.
10767 : If the bases are equal, we are set. */
10768 39 : if ((TREE_CODE (base0) == ARRAY_REF
10769 1 : && TREE_CODE (base1) == ARRAY_REF
10770 1 : && (base_offset
10771 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10772 : use_pointer_diff)))
10773 38 : || (INDIRECT_REF_P (base0)
10774 7 : && INDIRECT_REF_P (base1)
10775 7 : && (base_offset
10776 : = use_pointer_diff
10777 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10778 1 : TREE_OPERAND (base0, 0),
10779 1 : TREE_OPERAND (base1, 0))
10780 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10781 6 : fold_convert (type,
10782 : TREE_OPERAND (base0, 0)),
10783 6 : fold_convert (type,
10784 : TREE_OPERAND (base1, 0)))))
10785 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10786 : {
10787 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10788 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10789 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10790 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10791 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10792 : base_offset,
10793 : fold_build2_loc (loc, MULT_EXPR, type,
10794 15 : diff, esz));
10795 : }
10796 : return NULL_TREE;
10797 : }
10798 :
10799 : /* If the real or vector real constant CST of type TYPE has an exact
10800 : inverse, return it, else return NULL. */
10801 :
10802 : tree
10803 1176544 : exact_inverse (tree type, tree cst)
10804 : {
10805 1176544 : REAL_VALUE_TYPE r;
10806 1176544 : tree unit_type;
10807 1176544 : machine_mode mode;
10808 :
10809 1176544 : switch (TREE_CODE (cst))
10810 : {
10811 1175865 : case REAL_CST:
10812 1175865 : r = TREE_REAL_CST (cst);
10813 :
10814 1175865 : if (exact_real_inverse (TYPE_MODE (type), &r))
10815 331233 : return build_real (type, r);
10816 :
10817 : return NULL_TREE;
10818 :
10819 679 : case VECTOR_CST:
10820 679 : {
10821 679 : unit_type = TREE_TYPE (type);
10822 679 : mode = TYPE_MODE (unit_type);
10823 :
10824 679 : tree_vector_builder elts;
10825 679 : if (!elts.new_unary_operation (type, cst, false))
10826 : return NULL_TREE;
10827 679 : unsigned int count = elts.encoded_nelts ();
10828 739 : for (unsigned int i = 0; i < count; ++i)
10829 : {
10830 679 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10831 679 : if (!exact_real_inverse (mode, &r))
10832 : return NULL_TREE;
10833 60 : elts.quick_push (build_real (unit_type, r));
10834 : }
10835 :
10836 60 : return elts.build ();
10837 679 : }
10838 :
10839 : default:
10840 : return NULL_TREE;
10841 : }
10842 : }
10843 :
10844 : /* Mask out the tz least significant bits of X of type TYPE where
10845 : tz is the number of trailing zeroes in Y. */
10846 : static wide_int
10847 160672 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10848 : {
10849 160672 : int tz = wi::ctz (y);
10850 160672 : if (tz > 0)
10851 7007 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10852 153665 : return x;
10853 : }
10854 :
10855 : /* Return true when T is an address and is known to be nonzero.
10856 : For floating point we further ensure that T is not denormal.
10857 : Similar logic is present in nonzero_address in rtlanal.h. */
10858 :
10859 : bool
10860 153694407 : tree_expr_nonzero_p (tree t)
10861 : {
10862 154050506 : tree type = TREE_TYPE (t);
10863 154050506 : enum tree_code code;
10864 :
10865 : /* Doing something useful for floating point would need more work. */
10866 154050506 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10867 : return false;
10868 :
10869 153915755 : code = TREE_CODE (t);
10870 153915755 : switch (TREE_CODE_CLASS (code))
10871 : {
10872 988750 : case tcc_unary:
10873 988750 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10874 2931360 : case tcc_binary:
10875 2931360 : case tcc_comparison:
10876 2931360 : return tree_binary_nonzero_p (code, type,
10877 2931360 : TREE_OPERAND (t, 0),
10878 5862720 : TREE_OPERAND (t, 1));
10879 13251336 : case tcc_constant:
10880 13251336 : case tcc_declaration:
10881 13251336 : case tcc_reference:
10882 13251336 : return tree_single_nonzero_p (t);
10883 :
10884 136744309 : default:
10885 136744309 : break;
10886 : }
10887 :
10888 136744309 : switch (code)
10889 : {
10890 616592 : case TRUTH_NOT_EXPR:
10891 616592 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10892 :
10893 71117 : case TRUTH_AND_EXPR:
10894 71117 : case TRUTH_OR_EXPR:
10895 71117 : case TRUTH_XOR_EXPR:
10896 71117 : return tree_binary_nonzero_p (code, type,
10897 71117 : TREE_OPERAND (t, 0),
10898 142234 : TREE_OPERAND (t, 1));
10899 :
10900 132564118 : case COND_EXPR:
10901 132564118 : case CONSTRUCTOR:
10902 132564118 : case OBJ_TYPE_REF:
10903 132564118 : case ADDR_EXPR:
10904 132564118 : case WITH_SIZE_EXPR:
10905 132564118 : case SSA_NAME:
10906 132564118 : return tree_single_nonzero_p (t);
10907 :
10908 85029 : case COMPOUND_EXPR:
10909 85029 : case MODIFY_EXPR:
10910 85029 : case BIND_EXPR:
10911 85029 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10912 :
10913 271070 : case SAVE_EXPR:
10914 271070 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10915 :
10916 3079662 : case CALL_EXPR:
10917 3079662 : {
10918 3079662 : tree fndecl = get_callee_fndecl (t);
10919 3079662 : if (!fndecl) return false;
10920 3077659 : if (flag_delete_null_pointer_checks && !flag_check_new
10921 3077659 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10922 3078377 : && !TREE_NOTHROW (fndecl))
10923 : return true;
10924 3078377 : if (flag_delete_null_pointer_checks
10925 6156036 : && lookup_attribute ("returns_nonnull",
10926 3077659 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10927 : return true;
10928 3078369 : return alloca_call_p (t);
10929 : }
10930 :
10931 : default:
10932 : break;
10933 : }
10934 : return false;
10935 : }
10936 :
10937 : /* Return true if T is known not to be equal to an integer W.
10938 : If STMT is specified, the check is if T on STMT is not equal
10939 : to W. */
10940 :
10941 : bool
10942 105910018 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10943 : {
10944 105910018 : int_range_max vr;
10945 105910018 : switch (TREE_CODE (t))
10946 : {
10947 2481736 : case INTEGER_CST:
10948 2481736 : return wi::to_wide (t) != w;
10949 :
10950 102991039 : case SSA_NAME:
10951 102991039 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10952 : return false;
10953 :
10954 205982078 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10955 102991039 : if (!vr.undefined_p () && !vr.contains_p (w))
10956 : return true;
10957 : /* If T has some known zero bits and W has any of those bits set,
10958 : then T is known not to be equal to W. */
10959 102852672 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10960 205704108 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10961 1 : return true;
10962 : return false;
10963 :
10964 : default:
10965 : return false;
10966 : }
10967 105910018 : }
10968 :
10969 : /* Fold a binary expression of code CODE and type TYPE with operands
10970 : OP0 and OP1. LOC is the location of the resulting expression.
10971 : Return the folded expression if folding is successful. Otherwise,
10972 : return NULL_TREE. */
10973 :
10974 : tree
10975 969558582 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10976 : tree op0, tree op1)
10977 : {
10978 969558582 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10979 969558582 : tree arg0, arg1, tem;
10980 969558582 : tree t1 = NULL_TREE;
10981 969558582 : unsigned int prec;
10982 :
10983 969558582 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10984 : && TREE_CODE_LENGTH (code) == 2
10985 : && op0 != NULL_TREE
10986 : && op1 != NULL_TREE);
10987 :
10988 969558582 : arg0 = op0;
10989 969558582 : arg1 = op1;
10990 :
10991 : /* Strip any conversions that don't change the mode. This is
10992 : safe for every expression, except for a comparison expression
10993 : because its signedness is derived from its operands. So, in
10994 : the latter case, only strip conversions that don't change the
10995 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10996 : preserved.
10997 :
10998 : Note that this is done as an internal manipulation within the
10999 : constant folder, in order to find the simplest representation
11000 : of the arguments so that their form can be studied. In any
11001 : cases, the appropriate type conversions should be put back in
11002 : the tree that will get out of the constant folder. */
11003 :
11004 969558582 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11005 : {
11006 216306408 : STRIP_SIGN_NOPS (arg0);
11007 216306408 : STRIP_SIGN_NOPS (arg1);
11008 : }
11009 : else
11010 : {
11011 753252174 : STRIP_NOPS (arg0);
11012 753252174 : STRIP_NOPS (arg1);
11013 : }
11014 :
11015 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11016 : constant but we can't do arithmetic on them. */
11017 969558582 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11018 : {
11019 276206076 : tem = const_binop (code, type, arg0, arg1);
11020 276206076 : if (tem != NULL_TREE)
11021 : {
11022 273674546 : if (TREE_TYPE (tem) != type)
11023 4220004 : tem = fold_convert_loc (loc, type, tem);
11024 : return tem;
11025 : }
11026 : }
11027 :
11028 : /* If this is a commutative operation, and ARG0 is a constant, move it
11029 : to ARG1 to reduce the number of tests below. */
11030 695884036 : if (commutative_tree_code (code)
11031 695884036 : && tree_swap_operands_p (arg0, arg1))
11032 30413253 : return fold_build2_loc (loc, code, type, op1, op0);
11033 :
11034 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11035 : to ARG1 to reduce the number of tests below. */
11036 665470783 : if (kind == tcc_comparison
11037 665470783 : && tree_swap_operands_p (arg0, arg1))
11038 8645786 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11039 :
11040 656824997 : tem = generic_simplify (loc, code, type, op0, op1);
11041 656824997 : if (tem)
11042 : return tem;
11043 :
11044 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11045 :
11046 : First check for cases where an arithmetic operation is applied to a
11047 : compound, conditional, or comparison operation. Push the arithmetic
11048 : operation inside the compound or conditional to see if any folding
11049 : can then be done. Convert comparison to conditional for this purpose.
11050 : The also optimizes non-constant cases that used to be done in
11051 : expand_expr.
11052 :
11053 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11054 : one of the operands is a comparison and the other is a comparison, a
11055 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11056 : code below would make the expression more complex. Change it to a
11057 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11058 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11059 :
11060 556584984 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11061 : || code == EQ_EXPR || code == NE_EXPR)
11062 59147852 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11063 58555265 : && ((truth_value_p (TREE_CODE (arg0))
11064 1253340 : && (truth_value_p (TREE_CODE (arg1))
11065 932759 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11066 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11067 58234668 : || (truth_value_p (TREE_CODE (arg1))
11068 7252 : && (truth_value_p (TREE_CODE (arg0))
11069 7252 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11070 216 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11071 : {
11072 400944 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11073 80326 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11074 : : TRUTH_XOR_EXPR,
11075 : boolean_type_node,
11076 : fold_convert_loc (loc, boolean_type_node, arg0),
11077 : fold_convert_loc (loc, boolean_type_node, arg1));
11078 :
11079 320618 : if (code == EQ_EXPR)
11080 72902 : tem = invert_truthvalue_loc (loc, tem);
11081 :
11082 320618 : return fold_convert_loc (loc, type, tem);
11083 : }
11084 :
11085 556264366 : if (TREE_CODE_CLASS (code) == tcc_binary
11086 306275617 : || TREE_CODE_CLASS (code) == tcc_comparison)
11087 : {
11088 348106453 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11089 : {
11090 83007 : tem = fold_build2_loc (loc, code, type,
11091 83007 : fold_convert_loc (loc, TREE_TYPE (op0),
11092 83007 : TREE_OPERAND (arg0, 1)), op1);
11093 83007 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11094 83007 : tem);
11095 : }
11096 348023446 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11097 : {
11098 3221 : tem = fold_build2_loc (loc, code, type, op0,
11099 3221 : fold_convert_loc (loc, TREE_TYPE (op1),
11100 3221 : TREE_OPERAND (arg1, 1)));
11101 3221 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11102 3221 : tem);
11103 : }
11104 :
11105 348020225 : if (TREE_CODE (arg0) == COND_EXPR
11106 347617929 : || TREE_CODE (arg0) == VEC_COND_EXPR
11107 347614532 : || COMPARISON_CLASS_P (arg0))
11108 : {
11109 751186 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11110 : arg0, arg1,
11111 : /*cond_first_p=*/1);
11112 751186 : if (tem != NULL_TREE)
11113 : return tem;
11114 : }
11115 :
11116 347532572 : if (TREE_CODE (arg1) == COND_EXPR
11117 347319660 : || TREE_CODE (arg1) == VEC_COND_EXPR
11118 347319210 : || COMPARISON_CLASS_P (arg1))
11119 : {
11120 226401 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11121 : arg1, arg0,
11122 : /*cond_first_p=*/0);
11123 226401 : if (tem != NULL_TREE)
11124 : return tem;
11125 : }
11126 : }
11127 :
11128 555686881 : switch (code)
11129 : {
11130 65148201 : case MEM_REF:
11131 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11132 65148201 : if (TREE_CODE (arg0) == ADDR_EXPR
11133 65148201 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11134 : {
11135 828830 : tree iref = TREE_OPERAND (arg0, 0);
11136 828830 : return fold_build2 (MEM_REF, type,
11137 : TREE_OPERAND (iref, 0),
11138 : int_const_binop (PLUS_EXPR, arg1,
11139 : TREE_OPERAND (iref, 1)));
11140 : }
11141 :
11142 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11143 64319371 : if (TREE_CODE (arg0) == ADDR_EXPR
11144 64319371 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11145 : {
11146 6211443 : tree base;
11147 6211443 : poly_int64 coffset;
11148 6211443 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11149 : &coffset);
11150 6211443 : if (!base)
11151 : return NULL_TREE;
11152 6207313 : return fold_build2 (MEM_REF, type,
11153 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11154 : int_const_binop (PLUS_EXPR, arg1,
11155 : size_int (coffset)));
11156 : }
11157 :
11158 : return NULL_TREE;
11159 :
11160 74658716 : case POINTER_PLUS_EXPR:
11161 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11162 149317016 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11163 149308095 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11164 32672 : return fold_convert_loc (loc, type,
11165 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11166 : fold_convert_loc (loc, sizetype,
11167 : arg1),
11168 : fold_convert_loc (loc, sizetype,
11169 32672 : arg0)));
11170 :
11171 : return NULL_TREE;
11172 :
11173 63995507 : case PLUS_EXPR:
11174 63995507 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11175 : {
11176 : /* X + (X / CST) * -CST is X % CST. */
11177 52490372 : if (TREE_CODE (arg1) == MULT_EXPR
11178 2247031 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11179 52496661 : && operand_equal_p (arg0,
11180 6289 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11181 : {
11182 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11183 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11184 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11185 : cst1, cst0);
11186 204 : if (sum && integer_zerop (sum))
11187 204 : return fold_convert_loc (loc, type,
11188 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11189 204 : TREE_TYPE (arg0), arg0,
11190 204 : cst0));
11191 : }
11192 : }
11193 :
11194 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11195 : one. Make sure the type is not saturating and has the signedness of
11196 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11197 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11198 63995303 : if ((TREE_CODE (arg0) == MULT_EXPR
11199 52356009 : || TREE_CODE (arg1) == MULT_EXPR)
11200 12937248 : && !TYPE_SATURATING (type)
11201 12937248 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11202 12543458 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11203 75856120 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11204 : {
11205 8580060 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11206 8580060 : if (tem)
11207 : return tem;
11208 : }
11209 :
11210 62770871 : if (! FLOAT_TYPE_P (type))
11211 : {
11212 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11213 : (plus (plus (mult) (mult)) (foo)) so that we can
11214 : take advantage of the factoring cases below. */
11215 285282 : if (ANY_INTEGRAL_TYPE_P (type)
11216 51268100 : && TYPE_OVERFLOW_WRAPS (type)
11217 51268100 : && (((TREE_CODE (arg0) == PLUS_EXPR
11218 32020667 : || TREE_CODE (arg0) == MINUS_EXPR)
11219 3440850 : && TREE_CODE (arg1) == MULT_EXPR)
11220 31538602 : || ((TREE_CODE (arg1) == PLUS_EXPR
11221 31538602 : || TREE_CODE (arg1) == MINUS_EXPR)
11222 426349 : && TREE_CODE (arg0) == MULT_EXPR)))
11223 : {
11224 527957 : tree parg0, parg1, parg, marg;
11225 527957 : enum tree_code pcode;
11226 :
11227 527957 : if (TREE_CODE (arg1) == MULT_EXPR)
11228 : parg = arg0, marg = arg1;
11229 : else
11230 45892 : parg = arg1, marg = arg0;
11231 527957 : pcode = TREE_CODE (parg);
11232 527957 : parg0 = TREE_OPERAND (parg, 0);
11233 527957 : parg1 = TREE_OPERAND (parg, 1);
11234 527957 : STRIP_NOPS (parg0);
11235 527957 : STRIP_NOPS (parg1);
11236 :
11237 527957 : if (TREE_CODE (parg0) == MULT_EXPR
11238 262653 : && TREE_CODE (parg1) != MULT_EXPR)
11239 225020 : return fold_build2_loc (loc, pcode, type,
11240 : fold_build2_loc (loc, PLUS_EXPR, type,
11241 : fold_convert_loc (loc, type,
11242 : parg0),
11243 : fold_convert_loc (loc, type,
11244 : marg)),
11245 225020 : fold_convert_loc (loc, type, parg1));
11246 302937 : if (TREE_CODE (parg0) != MULT_EXPR
11247 265304 : && TREE_CODE (parg1) == MULT_EXPR)
11248 91446 : return
11249 91446 : fold_build2_loc (loc, PLUS_EXPR, type,
11250 : fold_convert_loc (loc, type, parg0),
11251 : fold_build2_loc (loc, pcode, type,
11252 : fold_convert_loc (loc, type, marg),
11253 : fold_convert_loc (loc, type,
11254 91446 : parg1)));
11255 : }
11256 : }
11257 : else
11258 : {
11259 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11260 : to __complex__ ( x, y ). This is not the same for SNaNs or
11261 : if signed zeros are involved. */
11262 11502771 : if (!HONOR_SNANS (arg0)
11263 11501062 : && !HONOR_SIGNED_ZEROS (arg0)
11264 11524728 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11265 : {
11266 3090 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11267 3090 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11268 3090 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11269 3090 : bool arg0rz = false, arg0iz = false;
11270 132 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11271 3198 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11272 : {
11273 90 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11274 90 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11275 90 : if (arg0rz && arg1i && real_zerop (arg1i))
11276 : {
11277 22 : tree rp = arg1r ? arg1r
11278 0 : : build1 (REALPART_EXPR, rtype, arg1);
11279 22 : tree ip = arg0i ? arg0i
11280 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11281 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11282 : }
11283 68 : else if (arg0iz && arg1r && real_zerop (arg1r))
11284 : {
11285 57 : tree rp = arg0r ? arg0r
11286 0 : : build1 (REALPART_EXPR, rtype, arg0);
11287 57 : tree ip = arg1i ? arg1i
11288 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11289 57 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11290 : }
11291 : }
11292 : }
11293 :
11294 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11295 : We associate floats only if the user has specified
11296 : -fassociative-math. */
11297 11502692 : if (flag_associative_math
11298 21485 : && TREE_CODE (arg1) == PLUS_EXPR
11299 38 : && TREE_CODE (arg0) != MULT_EXPR)
11300 : {
11301 23 : tree tree10 = TREE_OPERAND (arg1, 0);
11302 23 : tree tree11 = TREE_OPERAND (arg1, 1);
11303 23 : if (TREE_CODE (tree11) == MULT_EXPR
11304 5 : && TREE_CODE (tree10) == MULT_EXPR)
11305 : {
11306 1 : tree tree0;
11307 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11308 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11309 : }
11310 : }
11311 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11312 : We associate floats only if the user has specified
11313 : -fassociative-math. */
11314 11502691 : if (flag_associative_math
11315 21484 : && TREE_CODE (arg0) == PLUS_EXPR
11316 1294 : && TREE_CODE (arg1) != MULT_EXPR)
11317 : {
11318 866 : tree tree00 = TREE_OPERAND (arg0, 0);
11319 866 : tree tree01 = TREE_OPERAND (arg0, 1);
11320 866 : if (TREE_CODE (tree01) == MULT_EXPR
11321 49 : && TREE_CODE (tree00) == MULT_EXPR)
11322 : {
11323 9 : tree tree0;
11324 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11325 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11326 : }
11327 : }
11328 : }
11329 :
11330 11501825 : bit_rotate:
11331 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11332 : is a rotate of A by C1 bits. */
11333 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11334 : is a rotate of A by B bits.
11335 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11336 : though in this case CODE must be | and not + or ^, otherwise
11337 : it doesn't return A when B is 0. */
11338 65346187 : {
11339 65346187 : enum tree_code code0, code1;
11340 65346187 : tree rtype;
11341 65346187 : code0 = TREE_CODE (arg0);
11342 65346187 : code1 = TREE_CODE (arg1);
11343 75686 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11344 65329697 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11345 39991 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11346 39991 : TREE_OPERAND (arg1, 0), 0)
11347 37218 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11348 37218 : TYPE_UNSIGNED (rtype))
11349 : /* Only create rotates in complete modes. Other cases are not
11350 : expanded properly. */
11351 65373307 : && (element_precision (rtype)
11352 54240 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11353 : {
11354 27047 : tree tree01, tree11;
11355 27047 : tree orig_tree01, orig_tree11;
11356 27047 : enum tree_code code01, code11;
11357 :
11358 27047 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11359 27047 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11360 27047 : STRIP_NOPS (tree01);
11361 27047 : STRIP_NOPS (tree11);
11362 27047 : code01 = TREE_CODE (tree01);
11363 27047 : code11 = TREE_CODE (tree11);
11364 27047 : if (code11 != MINUS_EXPR
11365 26359 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11366 : {
11367 1462 : std::swap (code0, code1);
11368 1462 : std::swap (code01, code11);
11369 1462 : std::swap (tree01, tree11);
11370 1462 : std::swap (orig_tree01, orig_tree11);
11371 : }
11372 54094 : if (code01 == INTEGER_CST
11373 3250 : && code11 == INTEGER_CST
11374 30296 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11375 30296 : == element_precision (rtype)))
11376 : {
11377 6026 : tem = build2_loc (loc, LROTATE_EXPR,
11378 3013 : rtype, TREE_OPERAND (arg0, 0),
11379 : code0 == LSHIFT_EXPR
11380 : ? orig_tree01 : orig_tree11);
11381 3013 : return fold_convert_loc (loc, type, tem);
11382 : }
11383 24034 : else if (code11 == MINUS_EXPR)
11384 : {
11385 943 : tree tree110, tree111;
11386 943 : tree110 = TREE_OPERAND (tree11, 0);
11387 943 : tree111 = TREE_OPERAND (tree11, 1);
11388 943 : STRIP_NOPS (tree110);
11389 943 : STRIP_NOPS (tree111);
11390 943 : if (TREE_CODE (tree110) == INTEGER_CST
11391 932 : && compare_tree_int (tree110,
11392 932 : element_precision (rtype)) == 0
11393 1859 : && operand_equal_p (tree01, tree111, 0))
11394 : {
11395 780 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11396 : ? LROTATE_EXPR : RROTATE_EXPR),
11397 560 : rtype, TREE_OPERAND (arg0, 0),
11398 : orig_tree01);
11399 560 : return fold_convert_loc (loc, type, tem);
11400 : }
11401 : }
11402 23091 : else if (code == BIT_IOR_EXPR
11403 21977 : && code11 == BIT_AND_EXPR
11404 44897 : && pow2p_hwi (element_precision (rtype)))
11405 : {
11406 21806 : tree tree110, tree111;
11407 21806 : tree110 = TREE_OPERAND (tree11, 0);
11408 21806 : tree111 = TREE_OPERAND (tree11, 1);
11409 21806 : STRIP_NOPS (tree110);
11410 21806 : STRIP_NOPS (tree111);
11411 21806 : if (TREE_CODE (tree110) == NEGATE_EXPR
11412 21351 : && TREE_CODE (tree111) == INTEGER_CST
11413 21351 : && compare_tree_int (tree111,
11414 21351 : element_precision (rtype) - 1) == 0
11415 43143 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11416 : {
11417 31877 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11418 : ? LROTATE_EXPR : RROTATE_EXPR),
11419 21275 : rtype, TREE_OPERAND (arg0, 0),
11420 : orig_tree01);
11421 21275 : return fold_convert_loc (loc, type, tem);
11422 : }
11423 : }
11424 : }
11425 : }
11426 :
11427 155772330 : associate:
11428 : /* In most languages, can't associate operations on floats through
11429 : parentheses. Rather than remember where the parentheses were, we
11430 : don't associate floats at all, unless the user has specified
11431 : -fassociative-math.
11432 : And, we need to make sure type is not saturating. */
11433 :
11434 155772330 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11435 114982795 : && !TYPE_SATURATING (type)
11436 270755125 : && !TYPE_OVERFLOW_SANITIZED (type))
11437 : {
11438 114949902 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11439 114949902 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11440 114949902 : tree atype = type;
11441 114949902 : bool ok = true;
11442 :
11443 : /* Split both trees into variables, constants, and literals. Then
11444 : associate each group together, the constants with literals,
11445 : then the result with variables. This increases the chances of
11446 : literals being recombined later and of generating relocatable
11447 : expressions for the sum of a constant and literal. */
11448 114949902 : var0 = split_tree (arg0, type, code,
11449 : &minus_var0, &con0, &minus_con0,
11450 : &lit0, &minus_lit0, 0);
11451 114949902 : var1 = split_tree (arg1, type, code,
11452 : &minus_var1, &con1, &minus_con1,
11453 : &lit1, &minus_lit1, code == MINUS_EXPR);
11454 :
11455 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11456 114949902 : if (code == MINUS_EXPR)
11457 13249829 : code = PLUS_EXPR;
11458 :
11459 : /* With undefined overflow prefer doing association in a type
11460 : which wraps on overflow, if that is one of the operand types. */
11461 114949671 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11462 228643319 : && !TYPE_OVERFLOW_WRAPS (type))
11463 : {
11464 63363108 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11465 62718145 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11466 878796 : atype = TREE_TYPE (arg0);
11467 61559639 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11468 61322912 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11469 236006 : atype = TREE_TYPE (arg1);
11470 31933645 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11471 : }
11472 :
11473 : /* With undefined overflow we can only associate constants with one
11474 : variable, and constants whose association doesn't overflow. */
11475 114949671 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11476 228643319 : && !TYPE_OVERFLOW_WRAPS (atype))
11477 : {
11478 30818843 : if ((var0 && var1) || (minus_var0 && minus_var1))
11479 : {
11480 : /* ??? If split_tree would handle NEGATE_EXPR we could
11481 : simply reject these cases and the allowed cases would
11482 : be the var0/minus_var1 ones. */
11483 1449 : tree tmp0 = var0 ? var0 : minus_var0;
11484 5703467 : tree tmp1 = var1 ? var1 : minus_var1;
11485 5703467 : bool one_neg = false;
11486 :
11487 5703467 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11488 : {
11489 752 : tmp0 = TREE_OPERAND (tmp0, 0);
11490 752 : one_neg = !one_neg;
11491 : }
11492 5070144 : if (CONVERT_EXPR_P (tmp0)
11493 658931 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11494 6361443 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11495 657976 : <= TYPE_PRECISION (atype)))
11496 644745 : tmp0 = TREE_OPERAND (tmp0, 0);
11497 5703467 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11498 : {
11499 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11500 170 : one_neg = !one_neg;
11501 : }
11502 5375108 : if (CONVERT_EXPR_P (tmp1)
11503 396311 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11504 6099722 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11505 396255 : <= TYPE_PRECISION (atype)))
11506 378997 : tmp1 = TREE_OPERAND (tmp1, 0);
11507 : /* The only case we can still associate with two variables
11508 : is if they cancel out. */
11509 5703467 : if (!one_neg
11510 5703467 : || !operand_equal_p (tmp0, tmp1, 0))
11511 : ok = false;
11512 : }
11513 24732580 : else if ((var0 && minus_var1
11514 4208185 : && ! operand_equal_p (var0, minus_var1, 0))
11515 45639772 : || (minus_var0 && var1
11516 11789 : && ! operand_equal_p (minus_var0, var1, 0)))
11517 : ok = false;
11518 : }
11519 :
11520 : /* Only do something if we found more than two objects. Otherwise,
11521 : nothing has changed and we risk infinite recursion. */
11522 : if (ok
11523 105026542 : && ((var0 != 0) + (var1 != 0)
11524 105026542 : + (minus_var0 != 0) + (minus_var1 != 0)
11525 105026542 : + (con0 != 0) + (con1 != 0)
11526 105026542 : + (minus_con0 != 0) + (minus_con1 != 0)
11527 105026542 : + (lit0 != 0) + (lit1 != 0)
11528 105026542 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11529 : {
11530 2135395 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11531 4270790 : int minus_var0_origin
11532 2135395 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11533 2135395 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11534 4270790 : int minus_con0_origin
11535 2135395 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11536 2135395 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11537 4270790 : int minus_lit0_origin
11538 2135395 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11539 2135395 : var0 = associate_trees (loc, var0, var1, code, atype);
11540 2135395 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11541 : code, atype);
11542 2135395 : con0 = associate_trees (loc, con0, con1, code, atype);
11543 2135395 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11544 : code, atype);
11545 2135395 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11546 2135395 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11547 : code, atype);
11548 :
11549 2135395 : if (minus_var0 && var0)
11550 : {
11551 1399102 : var0_origin |= minus_var0_origin;
11552 1399102 : var0 = associate_trees (loc, var0, minus_var0,
11553 : MINUS_EXPR, atype);
11554 1399102 : minus_var0 = 0;
11555 1399102 : minus_var0_origin = 0;
11556 : }
11557 2135395 : if (minus_con0 && con0)
11558 : {
11559 2742 : con0_origin |= minus_con0_origin;
11560 2742 : con0 = associate_trees (loc, con0, minus_con0,
11561 : MINUS_EXPR, atype);
11562 2742 : minus_con0 = 0;
11563 2742 : minus_con0_origin = 0;
11564 : }
11565 :
11566 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11567 : greater than the positive part. Otherwise, the multiplicative
11568 : folding code (i.e extract_muldiv) may be fooled in case
11569 : unsigned constants are subtracted, like in the following
11570 : example: ((X*2 + 4) - 8U)/2. */
11571 2135395 : if (minus_lit0 && lit0)
11572 : {
11573 242780 : if (TREE_CODE (lit0) == INTEGER_CST
11574 242780 : && TREE_CODE (minus_lit0) == INTEGER_CST
11575 242780 : && tree_int_cst_lt (lit0, minus_lit0)
11576 : /* But avoid ending up with only negated parts. */
11577 306092 : && (var0 || con0))
11578 : {
11579 58674 : minus_lit0_origin |= lit0_origin;
11580 58674 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11581 : MINUS_EXPR, atype);
11582 58674 : lit0 = 0;
11583 58674 : lit0_origin = 0;
11584 : }
11585 : else
11586 : {
11587 184106 : lit0_origin |= minus_lit0_origin;
11588 184106 : lit0 = associate_trees (loc, lit0, minus_lit0,
11589 : MINUS_EXPR, atype);
11590 184106 : minus_lit0 = 0;
11591 184106 : minus_lit0_origin = 0;
11592 : }
11593 : }
11594 :
11595 : /* Don't introduce overflows through reassociation. */
11596 1423350 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11597 3558707 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11598 2135395 : return NULL_TREE;
11599 :
11600 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11601 2135357 : con0_origin |= lit0_origin;
11602 2135357 : con0 = associate_trees (loc, con0, lit0, code, atype);
11603 2135357 : minus_con0_origin |= minus_lit0_origin;
11604 2135357 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11605 : code, atype);
11606 :
11607 : /* Eliminate minus_con0. */
11608 2135357 : if (minus_con0)
11609 : {
11610 717246 : if (con0)
11611 : {
11612 15493 : con0_origin |= minus_con0_origin;
11613 15493 : con0 = associate_trees (loc, con0, minus_con0,
11614 : MINUS_EXPR, atype);
11615 : }
11616 701753 : else if (var0)
11617 : {
11618 701753 : var0_origin |= minus_con0_origin;
11619 701753 : var0 = associate_trees (loc, var0, minus_con0,
11620 : MINUS_EXPR, atype);
11621 : }
11622 : else
11623 0 : gcc_unreachable ();
11624 : }
11625 :
11626 : /* Eliminate minus_var0. */
11627 2135357 : if (minus_var0)
11628 : {
11629 381356 : if (con0)
11630 : {
11631 381356 : con0_origin |= minus_var0_origin;
11632 381356 : con0 = associate_trees (loc, con0, minus_var0,
11633 : MINUS_EXPR, atype);
11634 : }
11635 : else
11636 0 : gcc_unreachable ();
11637 : }
11638 :
11639 : /* Reassociate only if there has been any actual association
11640 : between subtrees from op0 and subtrees from op1 in at
11641 : least one of the operands, otherwise we risk infinite
11642 : recursion. See PR114084. */
11643 2135357 : if (var0_origin != 3 && con0_origin != 3)
11644 : return NULL_TREE;
11645 :
11646 2133647 : return
11647 2133647 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11648 2133647 : code, atype));
11649 : }
11650 : }
11651 :
11652 : return NULL_TREE;
11653 :
11654 24079765 : case POINTER_DIFF_EXPR:
11655 24079765 : case MINUS_EXPR:
11656 : /* Fold &a[i] - &a[j] to i-j. */
11657 24079765 : if (TREE_CODE (arg0) == ADDR_EXPR
11658 43363 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11659 6036 : && TREE_CODE (arg1) == ADDR_EXPR
11660 24080373 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11661 : {
11662 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11663 38 : TREE_OPERAND (arg0, 0),
11664 38 : TREE_OPERAND (arg1, 0),
11665 : code
11666 : == POINTER_DIFF_EXPR);
11667 38 : if (tem)
11668 : return tem;
11669 : }
11670 :
11671 : /* Further transformations are not for pointers. */
11672 24079751 : if (code == POINTER_DIFF_EXPR)
11673 : return NULL_TREE;
11674 :
11675 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11676 21328935 : if (TREE_CODE (arg0) == NEGATE_EXPR
11677 147445 : && negate_expr_p (op1)
11678 : /* If arg0 is e.g. unsigned int and type is int, then this could
11679 : introduce UB, because if A is INT_MIN at runtime, the original
11680 : expression can be well defined while the latter is not.
11681 : See PR83269. */
11682 21329794 : && !(ANY_INTEGRAL_TYPE_P (type)
11683 859 : && TYPE_OVERFLOW_UNDEFINED (type)
11684 847 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11685 847 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11686 852 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11687 : fold_convert_loc (loc, type,
11688 1704 : TREE_OPERAND (arg0, 0)));
11689 :
11690 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11691 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11692 : signed zeros are involved. */
11693 21328083 : if (!HONOR_SNANS (arg0)
11694 21326788 : && !HONOR_SIGNED_ZEROS (arg0)
11695 35456271 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11696 : {
11697 66 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11698 66 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11699 66 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11700 66 : bool arg0rz = false, arg0iz = false;
11701 36 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11702 93 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11703 : {
11704 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11705 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11706 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11707 : {
11708 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11709 : arg1r ? arg1r
11710 0 : : build1 (REALPART_EXPR, rtype, arg1));
11711 9 : tree ip = arg0i ? arg0i
11712 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11713 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11714 : }
11715 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11716 : {
11717 15 : tree rp = arg0r ? arg0r
11718 0 : : build1 (REALPART_EXPR, rtype, arg0);
11719 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11720 : arg1i ? arg1i
11721 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11722 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11723 : }
11724 : }
11725 : }
11726 :
11727 : /* A - B -> A + (-B) if B is easily negatable. */
11728 21328059 : if (negate_expr_p (op1)
11729 820173 : && ! TYPE_OVERFLOW_SANITIZED (type)
11730 22145694 : && ((FLOAT_TYPE_P (type)
11731 : /* Avoid this transformation if B is a positive REAL_CST. */
11732 65 : && (TREE_CODE (op1) != REAL_CST
11733 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11734 817570 : || INTEGRAL_TYPE_P (type)))
11735 817430 : return fold_build2_loc (loc, PLUS_EXPR, type,
11736 : fold_convert_loc (loc, type, arg0),
11737 817430 : negate_expr (op1));
11738 :
11739 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11740 : one. Make sure the type is not saturating and has the signedness of
11741 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11742 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11743 20510629 : if ((TREE_CODE (arg0) == MULT_EXPR
11744 19217289 : || TREE_CODE (arg1) == MULT_EXPR)
11745 2613559 : && !TYPE_SATURATING (type)
11746 2613559 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11747 2480167 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11748 22937723 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11749 : {
11750 348444 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11751 348444 : if (tem)
11752 : return tem;
11753 : }
11754 :
11755 20459351 : goto associate;
11756 :
11757 63595722 : case MULT_EXPR:
11758 63595722 : if (! FLOAT_TYPE_P (type))
11759 : {
11760 : /* Transform x * -C into -x * C if x is easily negatable. */
11761 41457907 : if (TREE_CODE (op1) == INTEGER_CST
11762 38329111 : && tree_int_cst_sgn (op1) == -1
11763 210453 : && negate_expr_p (op0)
11764 340 : && negate_expr_p (op1)
11765 324 : && (tem = negate_expr (op1)) != op1
11766 41458231 : && ! TREE_OVERFLOW (tem))
11767 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11768 : fold_convert_loc (loc, type,
11769 324 : negate_expr (op0)), tem);
11770 :
11771 41457583 : if (TREE_CODE (arg1) == INTEGER_CST
11772 41457583 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11773 : {
11774 663567 : return fold_convert_loc (loc, type, tem);
11775 : }
11776 :
11777 : /* Optimize z * conj(z) for integer complex numbers. */
11778 40794016 : if (TREE_CODE (arg0) == CONJ_EXPR
11779 40794016 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11780 1 : return fold_mult_zconjz (loc, type, arg1);
11781 40794015 : if (TREE_CODE (arg1) == CONJ_EXPR
11782 40794015 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11783 0 : return fold_mult_zconjz (loc, type, arg0);
11784 : }
11785 : else
11786 : {
11787 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11788 : This is not the same for NaNs or if signed zeros are
11789 : involved. */
11790 22137815 : if (!HONOR_NANS (arg0)
11791 33418 : && !HONOR_SIGNED_ZEROS (arg0)
11792 33081 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11793 3647 : && TREE_CODE (arg1) == COMPLEX_CST
11794 22138044 : && real_zerop (TREE_REALPART (arg1)))
11795 : {
11796 222 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11797 222 : if (real_onep (TREE_IMAGPART (arg1)))
11798 : {
11799 212 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11800 63 : arg0 = save_expr (arg0);
11801 212 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11802 : rtype, arg0);
11803 212 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11804 : rtype, arg0);
11805 212 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11806 : negate_expr (iarg0),
11807 212 : rarg0);
11808 : }
11809 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11810 : {
11811 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11812 0 : arg0 = save_expr (arg0);
11813 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11814 : rtype, arg0);
11815 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11816 : rtype, arg0);
11817 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11818 : iarg0,
11819 10 : negate_expr (rarg0));
11820 : }
11821 : }
11822 :
11823 : /* Optimize z * conj(z) for floating point complex numbers.
11824 : Guarded by flag_unsafe_math_optimizations as non-finite
11825 : imaginary components don't produce scalar results. */
11826 22137593 : if (flag_unsafe_math_optimizations
11827 32900 : && TREE_CODE (arg0) == CONJ_EXPR
11828 22137595 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11829 1 : return fold_mult_zconjz (loc, type, arg1);
11830 22137592 : if (flag_unsafe_math_optimizations
11831 32899 : && TREE_CODE (arg1) == CONJ_EXPR
11832 22137596 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11833 0 : return fold_mult_zconjz (loc, type, arg0);
11834 : }
11835 62931607 : goto associate;
11836 :
11837 1931327 : case BIT_IOR_EXPR:
11838 : /* Canonicalize (X & C1) | C2. */
11839 1931327 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11840 91400 : && TREE_CODE (arg1) == INTEGER_CST
11841 1976293 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11842 : {
11843 44958 : int width = TYPE_PRECISION (type), w;
11844 44958 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11845 44958 : wide_int c2 = wi::to_wide (arg1);
11846 :
11847 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11848 44958 : if ((c1 & c2) == c1)
11849 0 : return omit_one_operand_loc (loc, type, arg1,
11850 0 : TREE_OPERAND (arg0, 0));
11851 :
11852 44958 : wide_int msk = wi::mask (width, false,
11853 44958 : TYPE_PRECISION (TREE_TYPE (arg1)));
11854 :
11855 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11856 44958 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11857 : {
11858 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11859 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11860 : }
11861 :
11862 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11863 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11864 : mode which allows further optimizations. */
11865 44952 : c1 &= msk;
11866 44952 : c2 &= msk;
11867 44952 : wide_int c3 = wi::bit_and_not (c1, c2);
11868 186150 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11869 : {
11870 96488 : wide_int mask = wi::mask (w, false,
11871 96488 : TYPE_PRECISION (type));
11872 192976 : if (((c1 | c2) & mask) == mask
11873 192976 : && wi::bit_and_not (c1, mask) == 0)
11874 : {
11875 242 : c3 = mask;
11876 242 : break;
11877 : }
11878 96488 : }
11879 :
11880 44952 : if (c3 != c1)
11881 : {
11882 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11883 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11884 558 : wide_int_to_tree (type, c3));
11885 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11886 : }
11887 46080 : }
11888 :
11889 : /* See if this can be simplified into a rotate first. If that
11890 : is unsuccessful continue in the association code. */
11891 1930763 : goto bit_rotate;
11892 :
11893 961108 : case BIT_XOR_EXPR:
11894 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11895 961108 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11896 3775 : && INTEGRAL_TYPE_P (type)
11897 3167 : && integer_onep (TREE_OPERAND (arg0, 1))
11898 962351 : && integer_onep (arg1))
11899 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11900 0 : build_zero_cst (TREE_TYPE (arg0)));
11901 :
11902 : /* See if this can be simplified into a rotate first. If that
11903 : is unsuccessful continue in the association code. */
11904 961108 : goto bit_rotate;
11905 :
11906 6615949 : case BIT_AND_EXPR:
11907 : /* Fold !X & 1 as X == 0. */
11908 6615949 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11909 6615949 : && integer_onep (arg1))
11910 : {
11911 0 : tem = TREE_OPERAND (arg0, 0);
11912 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11913 0 : build_zero_cst (TREE_TYPE (tem)));
11914 : }
11915 :
11916 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11917 : multiple of 1 << CST. */
11918 6615949 : if (TREE_CODE (arg1) == INTEGER_CST)
11919 : {
11920 4771113 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11921 4771113 : wide_int ncst1 = -cst1;
11922 4771113 : if ((cst1 & ncst1) == ncst1
11923 4928037 : && multiple_of_p (type, arg0,
11924 4928037 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11925 462 : return fold_convert_loc (loc, type, arg0);
11926 4771113 : }
11927 :
11928 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11929 : bits from CST2. */
11930 6615487 : if (TREE_CODE (arg1) == INTEGER_CST
11931 4770651 : && TREE_CODE (arg0) == MULT_EXPR
11932 6776221 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11933 : {
11934 160672 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11935 160672 : wide_int masked
11936 160672 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11937 :
11938 160672 : if (masked == 0)
11939 5683 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11940 5683 : arg0, arg1);
11941 154989 : else if (masked != warg1)
11942 : {
11943 : /* Avoid the transform if arg1 is a mask of some
11944 : mode which allows further optimizations. */
11945 632 : int pop = wi::popcount (warg1);
11946 654 : if (!(pop >= BITS_PER_UNIT
11947 50 : && pow2p_hwi (pop)
11948 676 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11949 1220 : return fold_build2_loc (loc, code, type, op0,
11950 1220 : wide_int_to_tree (type, masked));
11951 : }
11952 160672 : }
11953 :
11954 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11955 4764358 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11956 6823721 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11957 : {
11958 116758 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11959 :
11960 116758 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11961 116758 : if (mask == -1)
11962 2423 : return
11963 2423 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11964 116758 : }
11965 :
11966 6606771 : goto associate;
11967 :
11968 6079356 : case RDIV_EXPR:
11969 : /* Don't touch a floating-point divide by zero unless the mode
11970 : of the constant can represent infinity. */
11971 6079356 : if (TREE_CODE (arg1) == REAL_CST
11972 3060573 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11973 6079356 : && real_zerop (arg1))
11974 0 : return NULL_TREE;
11975 :
11976 : /* (-A) / (-B) -> A / B */
11977 6079356 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11978 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11979 3 : TREE_OPERAND (arg0, 0),
11980 3 : negate_expr (arg1));
11981 6079353 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11982 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11983 : negate_expr (arg0),
11984 0 : TREE_OPERAND (arg1, 0));
11985 : return NULL_TREE;
11986 :
11987 2240852 : case TRUNC_DIV_EXPR:
11988 : /* Fall through */
11989 :
11990 2240852 : case FLOOR_DIV_EXPR:
11991 : /* Simplify A / (B << N) where A and B are positive and B is
11992 : a power of 2, to A >> (N + log2(B)). */
11993 2240852 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11994 2240852 : && (TYPE_UNSIGNED (type)
11995 9 : || tree_expr_nonnegative_p (op0)))
11996 : {
11997 17 : tree sval = TREE_OPERAND (arg1, 0);
11998 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11999 : {
12000 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
12001 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12002 16 : wi::exact_log2 (wi::to_wide (sval)));
12003 :
12004 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12005 : sh_cnt, pow2);
12006 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
12007 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
12008 : }
12009 : }
12010 :
12011 : /* Fall through */
12012 :
12013 3645131 : case ROUND_DIV_EXPR:
12014 3645131 : case CEIL_DIV_EXPR:
12015 3645131 : case EXACT_DIV_EXPR:
12016 3645131 : if (integer_zerop (arg1))
12017 : return NULL_TREE;
12018 :
12019 : /* Convert -A / -B to A / B when the type is signed and overflow is
12020 : undefined. */
12021 3642096 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12022 1005613 : && TREE_CODE (op0) == NEGATE_EXPR
12023 3642158 : && negate_expr_p (op1))
12024 60 : return fold_build2_loc (loc, code, type,
12025 : fold_convert_loc (loc, type,
12026 30 : TREE_OPERAND (arg0, 0)),
12027 30 : negate_expr (op1));
12028 3642066 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12029 1005583 : && TREE_CODE (arg1) == NEGATE_EXPR
12030 3642310 : && negate_expr_p (op0))
12031 36 : return fold_build2_loc (loc, code, type,
12032 : negate_expr (op0),
12033 : fold_convert_loc (loc, type,
12034 72 : TREE_OPERAND (arg1, 0)));
12035 :
12036 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12037 : operation, EXACT_DIV_EXPR.
12038 :
12039 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12040 : At one time others generated faster code, it's not clear if they do
12041 : after the last round to changes to the DIV code in expmed.cc. */
12042 3642030 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12043 3642030 : && multiple_of_p (type, arg0, arg1))
12044 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12045 : fold_convert (type, arg0),
12046 0 : fold_convert (type, arg1));
12047 :
12048 3642030 : if (TREE_CODE (arg1) == INTEGER_CST
12049 3642030 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12050 9486 : return fold_convert_loc (loc, type, tem);
12051 :
12052 : return NULL_TREE;
12053 :
12054 902513 : case CEIL_MOD_EXPR:
12055 902513 : case FLOOR_MOD_EXPR:
12056 902513 : case ROUND_MOD_EXPR:
12057 902513 : case TRUNC_MOD_EXPR:
12058 902513 : if (TREE_CODE (arg1) == INTEGER_CST
12059 902513 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
12060 0 : return fold_convert_loc (loc, type, tem);
12061 :
12062 : return NULL_TREE;
12063 :
12064 2261924 : case LROTATE_EXPR:
12065 2261924 : case RROTATE_EXPR:
12066 2261924 : case RSHIFT_EXPR:
12067 2261924 : case LSHIFT_EXPR:
12068 : /* Since negative shift count is not well-defined,
12069 : don't try to compute it in the compiler. */
12070 2261924 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12071 : return NULL_TREE;
12072 :
12073 2260882 : prec = element_precision (type);
12074 :
12075 : /* If we have a rotate of a bit operation with the rotate count and
12076 : the second operand of the bit operation both constant,
12077 : permute the two operations. */
12078 2801 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12079 2236 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12080 2236 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12081 2236 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12082 2260882 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12083 : {
12084 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12085 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12086 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12087 : fold_build2_loc (loc, code, type,
12088 : arg00, arg1),
12089 : fold_build2_loc (loc, code, type,
12090 0 : arg01, arg1));
12091 : }
12092 :
12093 : return NULL_TREE;
12094 :
12095 453262 : case MIN_EXPR:
12096 453262 : case MAX_EXPR:
12097 453262 : goto associate;
12098 :
12099 6681837 : case TRUTH_ANDIF_EXPR:
12100 : /* Note that the operands of this must be ints
12101 : and their values must be 0 or 1.
12102 : ("true" is a fixed value perhaps depending on the language.) */
12103 : /* If first arg is constant zero, return it. */
12104 6681837 : if (integer_zerop (arg0))
12105 1699159 : return fold_convert_loc (loc, type, arg0);
12106 : /* FALLTHRU */
12107 15964817 : case TRUTH_AND_EXPR:
12108 : /* If either arg is constant true, drop it. */
12109 15964817 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12110 1498094 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12111 826710 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12112 : /* Preserve sequence points. */
12113 15247875 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12114 754099 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12115 : /* If second arg is constant zero, result is zero, but first arg
12116 : must be evaluated. */
12117 13712624 : if (integer_zerop (arg1))
12118 45558 : return omit_one_operand_loc (loc, type, arg1, arg0);
12119 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12120 : case will be handled here. */
12121 13667066 : if (integer_zerop (arg0))
12122 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12123 :
12124 : /* !X && X is always false. */
12125 13667066 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12126 13667066 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12127 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12128 : /* X && !X is always false. */
12129 13667066 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12130 13667066 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12131 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12132 :
12133 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12134 : means A >= Y && A != MAX, but in this case we know that
12135 : A < X <= MAX. */
12136 :
12137 13667066 : if (!TREE_SIDE_EFFECTS (arg0)
12138 13667066 : && !TREE_SIDE_EFFECTS (arg1))
12139 : {
12140 12190570 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12141 12190570 : if (tem && !operand_equal_p (tem, arg0, 0))
12142 443 : return fold_convert (type,
12143 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12144 : tem, arg1));
12145 :
12146 12190127 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12147 12190127 : if (tem && !operand_equal_p (tem, arg1, 0))
12148 8497 : return fold_convert (type,
12149 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12150 : arg0, tem));
12151 : }
12152 :
12153 13658126 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12154 : != NULL_TREE)
12155 : return tem;
12156 :
12157 : return NULL_TREE;
12158 :
12159 3508260 : case TRUTH_ORIF_EXPR:
12160 : /* Note that the operands of this must be ints
12161 : and their values must be 0 or true.
12162 : ("true" is a fixed value perhaps depending on the language.) */
12163 : /* If first arg is constant true, return it. */
12164 3508260 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12165 145802 : return fold_convert_loc (loc, type, arg0);
12166 : /* FALLTHRU */
12167 12994992 : case TRUTH_OR_EXPR:
12168 : /* If either arg is constant zero, drop it. */
12169 12994992 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12170 250491 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12171 457592 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12172 : /* Preserve sequence points. */
12173 13156018 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12174 400213 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12175 : /* If second arg is constant true, result is true, but we must
12176 : evaluate first arg. */
12177 12344288 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12178 46075 : return omit_one_operand_loc (loc, type, arg1, arg0);
12179 : /* Likewise for first arg, but note this only occurs here for
12180 : TRUTH_OR_EXPR. */
12181 12298213 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12182 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12183 :
12184 : /* !X || X is always true. */
12185 12298213 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12186 12298213 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12187 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12188 : /* X || !X is always true. */
12189 12298213 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12190 12298213 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12191 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12192 :
12193 : /* (X && !Y) || (!X && Y) is X ^ Y */
12194 12298212 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12195 1654 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12196 : {
12197 660 : tree a0, a1, l0, l1, n0, n1;
12198 :
12199 660 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12200 660 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12201 :
12202 660 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12203 660 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12204 :
12205 660 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12206 660 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12207 :
12208 660 : if ((operand_equal_p (n0, a0, 0)
12209 18 : && operand_equal_p (n1, a1, 0))
12210 668 : || (operand_equal_p (n0, a1, 0)
12211 3 : && operand_equal_p (n1, a0, 0)))
12212 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12213 : }
12214 :
12215 12298199 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12216 : != NULL_TREE)
12217 : return tem;
12218 :
12219 : return NULL_TREE;
12220 :
12221 79198 : case TRUTH_XOR_EXPR:
12222 : /* If the second arg is constant zero, drop it. */
12223 79198 : if (integer_zerop (arg1))
12224 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12225 : /* If the second arg is constant true, this is a logical inversion. */
12226 79198 : if (integer_onep (arg1))
12227 : {
12228 0 : tem = invert_truthvalue_loc (loc, arg0);
12229 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12230 : }
12231 : /* Identical arguments cancel to zero. */
12232 79198 : if (operand_equal_p (arg0, arg1, 0))
12233 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12234 :
12235 : /* !X ^ X is always true. */
12236 79198 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12237 79198 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12238 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12239 :
12240 : /* X ^ !X is always true. */
12241 79198 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12242 79198 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12243 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12244 :
12245 : return NULL_TREE;
12246 :
12247 49841493 : case EQ_EXPR:
12248 49841493 : case NE_EXPR:
12249 49841493 : STRIP_NOPS (arg0);
12250 49841493 : STRIP_NOPS (arg1);
12251 :
12252 49841493 : tem = fold_comparison (loc, code, type, op0, op1);
12253 49841493 : if (tem != NULL_TREE)
12254 : return tem;
12255 :
12256 : /* bool_var != 1 becomes !bool_var. */
12257 51004995 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12258 49882255 : && code == NE_EXPR)
12259 40855 : return fold_convert_loc (loc, type,
12260 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12261 81710 : TREE_TYPE (arg0), arg0));
12262 :
12263 : /* bool_var == 0 becomes !bool_var. */
12264 50923285 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12265 50734491 : && code == EQ_EXPR)
12266 187554 : return fold_convert_loc (loc, type,
12267 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12268 375108 : TREE_TYPE (arg0), arg0));
12269 :
12270 : /* !exp != 0 becomes !exp */
12271 617157 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12272 50223708 : && code == NE_EXPR)
12273 608063 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12274 :
12275 : /* If this is an EQ or NE comparison with zero and ARG0 is
12276 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12277 : two operations, but the latter can be done in one less insn
12278 : on machines that have only two-operand insns or on which a
12279 : constant cannot be the first operand. */
12280 48999231 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12281 48999231 : && integer_zerop (arg1))
12282 : {
12283 1521627 : tree arg00 = TREE_OPERAND (arg0, 0);
12284 1521627 : tree arg01 = TREE_OPERAND (arg0, 1);
12285 1521627 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12286 1521627 : && integer_onep (TREE_OPERAND (arg00, 0)))
12287 : {
12288 4365 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12289 4365 : arg01, TREE_OPERAND (arg00, 1));
12290 4365 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12291 4365 : build_one_cst (TREE_TYPE (arg0)));
12292 4365 : return fold_build2_loc (loc, code, type,
12293 4365 : fold_convert_loc (loc, TREE_TYPE (arg1),
12294 4365 : tem), arg1);
12295 : }
12296 1517262 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12297 1517262 : && integer_onep (TREE_OPERAND (arg01, 0)))
12298 : {
12299 426 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12300 426 : arg00, TREE_OPERAND (arg01, 1));
12301 426 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12302 426 : build_one_cst (TREE_TYPE (arg0)));
12303 426 : return fold_build2_loc (loc, code, type,
12304 426 : fold_convert_loc (loc, TREE_TYPE (arg1),
12305 426 : tem), arg1);
12306 : }
12307 : }
12308 :
12309 : /* If this is a comparison of a field, we may be able to simplify it. */
12310 48994440 : if ((TREE_CODE (arg0) == COMPONENT_REF
12311 48994440 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12312 : /* Handle the constant case even without -O
12313 : to make sure the warnings are given. */
12314 5022224 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12315 : {
12316 4703979 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12317 4703979 : if (t1)
12318 : return t1;
12319 : }
12320 :
12321 : /* Optimize comparisons of strlen vs zero to a compare of the
12322 : first character of the string vs zero. To wit,
12323 : strlen(ptr) == 0 => *ptr == 0
12324 : strlen(ptr) != 0 => *ptr != 0
12325 : Other cases should reduce to one of these two (or a constant)
12326 : due to the return value of strlen being unsigned. */
12327 48198281 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12328 : {
12329 3032614 : tree fndecl = get_callee_fndecl (arg0);
12330 :
12331 3032614 : if (fndecl
12332 3031498 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12333 550 : && call_expr_nargs (arg0) == 1
12334 3033164 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12335 : == POINTER_TYPE))
12336 : {
12337 550 : tree ptrtype
12338 550 : = build_pointer_type (build_qualified_type (char_type_node,
12339 : TYPE_QUAL_CONST));
12340 1100 : tree ptr = fold_convert_loc (loc, ptrtype,
12341 550 : CALL_EXPR_ARG (arg0, 0));
12342 550 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12343 550 : return fold_build2_loc (loc, code, type, iref,
12344 550 : build_int_cst (TREE_TYPE (iref), 0));
12345 : }
12346 : }
12347 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12348 : (X & C) == 0 when C is a single bit. */
12349 48197731 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12350 1691483 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12351 861 : && integer_zerop (arg1)
12352 48198193 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12353 : {
12354 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12355 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12356 140 : TREE_OPERAND (arg0, 1));
12357 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12358 : type, tem,
12359 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12360 140 : arg1));
12361 : }
12362 :
12363 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12364 : constant C is a power of two, i.e. a single bit. */
12365 48197591 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12366 4069 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12367 0 : && integer_zerop (arg1)
12368 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12369 48197591 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12370 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12371 : {
12372 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12373 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12374 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12375 : }
12376 :
12377 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12378 : when is C is a power of two, i.e. a single bit. */
12379 48197591 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12380 1691343 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12381 37148 : && integer_zerop (arg1)
12382 37148 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12383 48231884 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12384 34293 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12385 : {
12386 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12387 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12388 0 : arg000, TREE_OPERAND (arg0, 1));
12389 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12390 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12391 : }
12392 :
12393 48197591 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12394 4069 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12395 : {
12396 74 : tree arg00 = TREE_OPERAND (arg0, 0);
12397 74 : tree arg01 = TREE_OPERAND (arg0, 1);
12398 74 : tree arg10 = TREE_OPERAND (arg1, 0);
12399 74 : tree arg11 = TREE_OPERAND (arg1, 1);
12400 74 : tree itype = TREE_TYPE (arg0);
12401 :
12402 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12403 : operand_equal_p guarantees no side-effects so we don't need
12404 : to use omit_one_operand on Z. */
12405 74 : if (operand_equal_p (arg01, arg11, 0))
12406 8 : return fold_build2_loc (loc, code, type, arg00,
12407 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12408 8 : arg10));
12409 66 : if (operand_equal_p (arg01, arg10, 0))
12410 0 : return fold_build2_loc (loc, code, type, arg00,
12411 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12412 0 : arg11));
12413 66 : if (operand_equal_p (arg00, arg11, 0))
12414 0 : return fold_build2_loc (loc, code, type, arg01,
12415 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12416 0 : arg10));
12417 66 : if (operand_equal_p (arg00, arg10, 0))
12418 0 : return fold_build2_loc (loc, code, type, arg01,
12419 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12420 0 : arg11));
12421 :
12422 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12423 66 : if (TREE_CODE (arg01) == INTEGER_CST
12424 8 : && TREE_CODE (arg11) == INTEGER_CST)
12425 : {
12426 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12427 : fold_convert_loc (loc, itype, arg11));
12428 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12429 8 : return fold_build2_loc (loc, code, type, tem,
12430 8 : fold_convert_loc (loc, itype, arg10));
12431 : }
12432 : }
12433 :
12434 : /* Attempt to simplify equality/inequality comparisons of complex
12435 : values. Only lower the comparison if the result is known or
12436 : can be simplified to a single scalar comparison. */
12437 48197575 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12438 48195012 : || TREE_CODE (arg0) == COMPLEX_CST)
12439 2563 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12440 2365 : || TREE_CODE (arg1) == COMPLEX_CST))
12441 : {
12442 1750 : tree real0, imag0, real1, imag1;
12443 1750 : tree rcond, icond;
12444 :
12445 1750 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12446 : {
12447 1750 : real0 = TREE_OPERAND (arg0, 0);
12448 1750 : imag0 = TREE_OPERAND (arg0, 1);
12449 : }
12450 : else
12451 : {
12452 0 : real0 = TREE_REALPART (arg0);
12453 0 : imag0 = TREE_IMAGPART (arg0);
12454 : }
12455 :
12456 1750 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12457 : {
12458 198 : real1 = TREE_OPERAND (arg1, 0);
12459 198 : imag1 = TREE_OPERAND (arg1, 1);
12460 : }
12461 : else
12462 : {
12463 1552 : real1 = TREE_REALPART (arg1);
12464 1552 : imag1 = TREE_IMAGPART (arg1);
12465 : }
12466 :
12467 1750 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12468 1750 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12469 : {
12470 11 : if (integer_zerop (rcond))
12471 : {
12472 11 : if (code == EQ_EXPR)
12473 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12474 0 : imag0, imag1);
12475 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12476 : }
12477 : else
12478 : {
12479 0 : if (code == NE_EXPR)
12480 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12481 0 : imag0, imag1);
12482 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12483 : }
12484 : }
12485 :
12486 1739 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12487 1739 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12488 : {
12489 9 : if (integer_zerop (icond))
12490 : {
12491 7 : if (code == EQ_EXPR)
12492 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12493 1 : real0, real1);
12494 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12495 : }
12496 : else
12497 : {
12498 2 : if (code == NE_EXPR)
12499 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12500 1 : real0, real1);
12501 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12502 : }
12503 : }
12504 : }
12505 :
12506 : return NULL_TREE;
12507 :
12508 41838737 : case LT_EXPR:
12509 41838737 : case GT_EXPR:
12510 41838737 : case LE_EXPR:
12511 41838737 : case GE_EXPR:
12512 41838737 : tem = fold_comparison (loc, code, type, op0, op1);
12513 41838737 : if (tem != NULL_TREE)
12514 : return tem;
12515 :
12516 : /* Transform comparisons of the form X +- C CMP X. */
12517 40940528 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12518 4882686 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12519 52789 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12520 40940544 : && !HONOR_SNANS (arg0))
12521 : {
12522 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12523 14 : enum tree_code code0 = TREE_CODE (arg0);
12524 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12525 :
12526 : /* (X - c) > X becomes false. */
12527 14 : if (code == GT_EXPR
12528 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12529 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12530 4 : return constant_boolean_node (0, type);
12531 :
12532 : /* Likewise (X + c) < X becomes false. */
12533 10 : if (code == LT_EXPR
12534 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12535 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12536 3 : return constant_boolean_node (0, type);
12537 :
12538 : /* Convert (X - c) <= X to true. */
12539 7 : if (!HONOR_NANS (arg1)
12540 6 : && code == LE_EXPR
12541 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12542 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12543 4 : return constant_boolean_node (1, type);
12544 :
12545 : /* Convert (X + c) >= X to true. */
12546 3 : if (!HONOR_NANS (arg1)
12547 2 : && code == GE_EXPR
12548 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12549 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12550 2 : return constant_boolean_node (1, type);
12551 : }
12552 :
12553 : /* If we are comparing an ABS_EXPR with a constant, we can
12554 : convert all the cases into explicit comparisons, but they may
12555 : well not be faster than doing the ABS and one comparison.
12556 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12557 : and a comparison, and is probably faster. */
12558 40940515 : if (code == LE_EXPR
12559 7858220 : && TREE_CODE (arg1) == INTEGER_CST
12560 5599839 : && TREE_CODE (arg0) == ABS_EXPR
12561 484 : && ! TREE_SIDE_EFFECTS (arg0)
12562 484 : && (tem = negate_expr (arg1)) != 0
12563 484 : && TREE_CODE (tem) == INTEGER_CST
12564 40940999 : && !TREE_OVERFLOW (tem))
12565 968 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12566 : build2 (GE_EXPR, type,
12567 484 : TREE_OPERAND (arg0, 0), tem),
12568 : build2 (LE_EXPR, type,
12569 968 : TREE_OPERAND (arg0, 0), arg1));
12570 :
12571 : /* Convert ABS_EXPR<x> >= 0 to true. */
12572 40940031 : if (code == GE_EXPR
12573 4278947 : && (integer_zerop (arg1)
12574 3133871 : || (! HONOR_NANS (arg0)
12575 2459390 : && real_zerop (arg1)))
12576 42085338 : && tree_expr_nonnegative_p (arg0))
12577 5235 : return omit_one_operand_loc (loc, type,
12578 : constant_boolean_node (true, type),
12579 5235 : arg0);
12580 :
12581 : /* Convert ABS_EXPR<x> < 0 to false. */
12582 40934796 : if (code == LT_EXPR
12583 13516557 : && (integer_zerop (arg1) || real_zerop (arg1))
12584 44203846 : && tree_expr_nonnegative_p (arg0))
12585 41853 : return omit_one_operand_loc (loc, type,
12586 : constant_boolean_node (false, type),
12587 41853 : arg0);
12588 :
12589 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12590 : and similarly for >= into !=. */
12591 40892943 : if ((code == LT_EXPR || code == GE_EXPR)
12592 17748416 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12593 5648039 : && TREE_CODE (arg1) == LSHIFT_EXPR
12594 40894467 : && integer_onep (TREE_OPERAND (arg1, 0)))
12595 4048 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12596 1352 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12597 1352 : TREE_OPERAND (arg1, 1)),
12598 2704 : build_zero_cst (TREE_TYPE (arg0)));
12599 :
12600 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12601 : otherwise Y might be >= # of bits in X's type and thus e.g.
12602 : (unsigned char) (1 << Y) for Y 15 might be 0.
12603 : If the cast is widening, then 1 << Y should have unsigned type,
12604 : otherwise if Y is number of bits in the signed shift type minus 1,
12605 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12606 : 31 might be 0xffffffff80000000. */
12607 40891591 : if ((code == LT_EXPR || code == GE_EXPR)
12608 17747064 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12609 5731621 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12610 12039013 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12611 4161816 : && CONVERT_EXPR_P (arg1)
12612 1126827 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12613 42 : && (element_precision (TREE_TYPE (arg1))
12614 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12615 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12616 14 : || (element_precision (TREE_TYPE (arg1))
12617 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12618 40891598 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12619 : {
12620 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12621 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12622 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12623 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12624 14 : build_zero_cst (TREE_TYPE (arg0)));
12625 : }
12626 :
12627 : return NULL_TREE;
12628 :
12629 5992814 : case UNORDERED_EXPR:
12630 5992814 : case ORDERED_EXPR:
12631 5992814 : case UNLT_EXPR:
12632 5992814 : case UNLE_EXPR:
12633 5992814 : case UNGT_EXPR:
12634 5992814 : case UNGE_EXPR:
12635 5992814 : case UNEQ_EXPR:
12636 5992814 : case LTGT_EXPR:
12637 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12638 5992814 : {
12639 5992814 : tree targ0 = strip_float_extensions (arg0);
12640 5992814 : tree targ1 = strip_float_extensions (arg1);
12641 5992814 : tree newtype = TREE_TYPE (targ0);
12642 :
12643 5992814 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12644 1215 : newtype = TREE_TYPE (targ1);
12645 :
12646 5992814 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12647 5992814 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12648 328 : return fold_build2_loc (loc, code, type,
12649 : fold_convert_loc (loc, newtype, targ0),
12650 328 : fold_convert_loc (loc, newtype, targ1));
12651 : }
12652 :
12653 : return NULL_TREE;
12654 :
12655 8642941 : case COMPOUND_EXPR:
12656 : /* When pedantic, a compound expression can be neither an lvalue
12657 : nor an integer constant expression. */
12658 8642941 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12659 : return NULL_TREE;
12660 : /* Don't let (0, 0) be null pointer constant. */
12661 517680 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12662 517680 : : fold_convert_loc (loc, type, arg1);
12663 : return tem;
12664 :
12665 : default:
12666 : return NULL_TREE;
12667 : } /* switch (code) */
12668 : }
12669 :
12670 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12671 : ((A & N) + B) & M -> (A + B) & M
12672 : Similarly if (N & M) == 0,
12673 : ((A | N) + B) & M -> (A + B) & M
12674 : and for - instead of + (or unary - instead of +)
12675 : and/or ^ instead of |.
12676 : If B is constant and (B & M) == 0, fold into A & M.
12677 :
12678 : This function is a helper for match.pd patterns. Return non-NULL
12679 : type in which the simplified operation should be performed only
12680 : if any optimization is possible.
12681 :
12682 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12683 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12684 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12685 : +/-. */
12686 : tree
12687 1255129 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12688 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12689 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12690 : tree *pmop)
12691 : {
12692 1255129 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12693 1255129 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12694 1255129 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12695 2510258 : if (~cst1 == 0
12696 3760631 : || (cst1 & (cst1 + 1)) != 0
12697 1042058 : || !INTEGRAL_TYPE_P (type)
12698 1042058 : || (!TYPE_OVERFLOW_WRAPS (type)
12699 48618 : && TREE_CODE (type) != INTEGER_TYPE)
12700 4591996 : || (wi::max_value (type) & cst1) != cst1)
12701 : return NULL_TREE;
12702 :
12703 1042058 : enum tree_code codes[2] = { code00, code01 };
12704 1042058 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12705 1042058 : int which = 0;
12706 1042058 : wide_int cst0;
12707 :
12708 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12709 : arg1 (M) is == (1LL << cst) - 1.
12710 : Store C into PMOP[0] and D into PMOP[1]. */
12711 1042058 : pmop[0] = arg00;
12712 1042058 : pmop[1] = arg01;
12713 1042058 : which = code != NEGATE_EXPR;
12714 :
12715 3124959 : for (; which >= 0; which--)
12716 2082901 : switch (codes[which])
12717 : {
12718 21540 : case BIT_AND_EXPR:
12719 21540 : case BIT_IOR_EXPR:
12720 21540 : case BIT_XOR_EXPR:
12721 21540 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12722 21540 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12723 21540 : if (codes[which] == BIT_AND_EXPR)
12724 : {
12725 21428 : if (cst0 != cst1)
12726 : break;
12727 : }
12728 112 : else if (cst0 != 0)
12729 : break;
12730 : /* If C or D is of the form (A & N) where
12731 : (N & M) == M, or of the form (A | N) or
12732 : (A ^ N) where (N & M) == 0, replace it with A. */
12733 20001 : pmop[which] = arg0xx[2 * which];
12734 20001 : break;
12735 2061361 : case ERROR_MARK:
12736 2061361 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12737 : break;
12738 : /* If C or D is a N where (N & M) == 0, it can be
12739 : omitted (replaced with 0). */
12740 883938 : if ((code == PLUS_EXPR
12741 206842 : || (code == MINUS_EXPR && which == 0))
12742 648811 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12743 141069 : pmop[which] = build_int_cst (type, 0);
12744 : /* Similarly, with C - N where (-N & M) == 0. */
12745 883938 : if (code == MINUS_EXPR
12746 441969 : && which == 1
12747 641426 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12748 191425 : pmop[which] = build_int_cst (type, 0);
12749 : break;
12750 0 : default:
12751 0 : gcc_unreachable ();
12752 : }
12753 :
12754 : /* Only build anything new if we optimized one or both arguments above. */
12755 1042058 : if (pmop[0] == arg00 && pmop[1] == arg01)
12756 : return NULL_TREE;
12757 :
12758 351893 : if (TYPE_OVERFLOW_WRAPS (type))
12759 : return type;
12760 : else
12761 3327 : return unsigned_type_for (type);
12762 1042058 : }
12763 :
12764 : /* Used by contains_label_[p1]. */
12765 :
12766 : struct contains_label_data
12767 : {
12768 : hash_set<tree> *pset;
12769 : bool inside_switch_p;
12770 : };
12771 :
12772 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12773 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12774 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12775 :
12776 : static tree
12777 4614424 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12778 : {
12779 4614424 : contains_label_data *d = (contains_label_data *) data;
12780 4614424 : switch (TREE_CODE (*tp))
12781 : {
12782 : case LABEL_EXPR:
12783 : return *tp;
12784 :
12785 0 : case CASE_LABEL_EXPR:
12786 0 : if (!d->inside_switch_p)
12787 : return *tp;
12788 : return NULL_TREE;
12789 :
12790 0 : case SWITCH_EXPR:
12791 0 : if (!d->inside_switch_p)
12792 : {
12793 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12794 0 : return *tp;
12795 0 : d->inside_switch_p = true;
12796 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12797 0 : return *tp;
12798 0 : d->inside_switch_p = false;
12799 0 : *walk_subtrees = 0;
12800 : }
12801 : return NULL_TREE;
12802 :
12803 6671 : case GOTO_EXPR:
12804 6671 : *walk_subtrees = 0;
12805 6671 : return NULL_TREE;
12806 :
12807 : default:
12808 : return NULL_TREE;
12809 : }
12810 : }
12811 :
12812 : /* Return whether the sub-tree ST contains a label which is accessible from
12813 : outside the sub-tree. */
12814 :
12815 : static bool
12816 329604 : contains_label_p (tree st)
12817 : {
12818 329604 : hash_set<tree> pset;
12819 329604 : contains_label_data data = { &pset, false };
12820 329604 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12821 329604 : }
12822 :
12823 : /* Fold a ternary expression of code CODE and type TYPE with operands
12824 : OP0, OP1, and OP2. Return the folded expression if folding is
12825 : successful. Otherwise, return NULL_TREE. */
12826 :
12827 : tree
12828 47888345 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12829 : tree op0, tree op1, tree op2)
12830 : {
12831 47888345 : tree tem;
12832 47888345 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12833 47888345 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12834 :
12835 47888345 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12836 : && TREE_CODE_LENGTH (code) == 3);
12837 :
12838 : /* If this is a commutative operation, and OP0 is a constant, move it
12839 : to OP1 to reduce the number of tests below. */
12840 47888345 : if (commutative_ternary_tree_code (code)
12841 47888345 : && tree_swap_operands_p (op0, op1))
12842 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12843 :
12844 47888312 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12845 47888312 : if (tem)
12846 : return tem;
12847 :
12848 : /* Strip any conversions that don't change the mode. This is safe
12849 : for every expression, except for a comparison expression because
12850 : its signedness is derived from its operands. So, in the latter
12851 : case, only strip conversions that don't change the signedness.
12852 :
12853 : Note that this is done as an internal manipulation within the
12854 : constant folder, in order to find the simplest representation of
12855 : the arguments so that their form can be studied. In any cases,
12856 : the appropriate type conversions should be put back in the tree
12857 : that will get out of the constant folder. */
12858 46796105 : if (op0)
12859 : {
12860 46726581 : arg0 = op0;
12861 46726581 : STRIP_NOPS (arg0);
12862 : }
12863 :
12864 46796105 : if (op1)
12865 : {
12866 46796105 : arg1 = op1;
12867 46796105 : STRIP_NOPS (arg1);
12868 : }
12869 :
12870 46796105 : if (op2)
12871 : {
12872 15150264 : arg2 = op2;
12873 15150264 : STRIP_NOPS (arg2);
12874 : }
12875 :
12876 46796105 : switch (code)
12877 : {
12878 31645303 : case COMPONENT_REF:
12879 31645303 : if (TREE_CODE (arg0) == CONSTRUCTOR
12880 31645303 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12881 : {
12882 : unsigned HOST_WIDE_INT idx;
12883 : tree field, value;
12884 886 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12885 681 : if (field == arg1)
12886 : return value;
12887 : }
12888 : return NULL_TREE;
12889 :
12890 12666868 : case COND_EXPR:
12891 12666868 : case VEC_COND_EXPR:
12892 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12893 : so all simple results must be passed through pedantic_non_lvalue. */
12894 12666868 : if (TREE_CODE (arg0) == INTEGER_CST)
12895 : {
12896 465032 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12897 465032 : tem = integer_zerop (arg0) ? op2 : op1;
12898 : /* Only optimize constant conditions when the selected branch
12899 : has the same type as the COND_EXPR. This avoids optimizing
12900 : away "c ? x : throw", where the throw has a void type.
12901 : Avoid throwing away that operand which contains label. */
12902 465032 : if ((!TREE_SIDE_EFFECTS (unused_op)
12903 329604 : || !contains_label_p (unused_op))
12904 789794 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12905 379112 : || VOID_TYPE_P (type)))
12906 450966 : return protected_set_expr_location_unshare (tem, loc);
12907 : return NULL_TREE;
12908 : }
12909 12201836 : else if (TREE_CODE (arg0) == VECTOR_CST)
12910 : {
12911 11901 : unsigned HOST_WIDE_INT nelts;
12912 11901 : if ((TREE_CODE (arg1) == VECTOR_CST
12913 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12914 2860 : && (TREE_CODE (arg2) == VECTOR_CST
12915 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12916 23802 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12917 : {
12918 2860 : vec_perm_builder sel (nelts, nelts, 1);
12919 27718 : for (unsigned int i = 0; i < nelts; i++)
12920 : {
12921 24858 : tree val = VECTOR_CST_ELT (arg0, i);
12922 24858 : if (integer_all_onesp (val))
12923 12204 : sel.quick_push (i);
12924 12654 : else if (integer_zerop (val))
12925 12654 : sel.quick_push (nelts + i);
12926 : else /* Currently unreachable. */
12927 2172 : return NULL_TREE;
12928 : }
12929 2860 : vec_perm_indices indices (sel, 2, nelts);
12930 2860 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12931 2860 : if (t != NULL_TREE)
12932 2172 : return t;
12933 5032 : }
12934 : }
12935 :
12936 : /* If we have A op B ? A : C, we may be able to convert this to a
12937 : simpler expression, depending on the operation and the values
12938 : of B and C. Signed zeros prevent all of these transformations,
12939 : for reasons given above each one.
12940 :
12941 : Also try swapping the arguments and inverting the conditional. */
12942 12199664 : if (COMPARISON_CLASS_P (arg0)
12943 10042653 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12944 12340154 : && !HONOR_SIGNED_ZEROS (op1))
12945 : {
12946 129458 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12947 129458 : TREE_OPERAND (arg0, 0),
12948 129458 : TREE_OPERAND (arg0, 1),
12949 : op1, op2);
12950 129458 : if (tem)
12951 : return tem;
12952 : }
12953 :
12954 12192686 : if (COMPARISON_CLASS_P (arg0)
12955 10035675 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12956 12681129 : && !HONOR_SIGNED_ZEROS (op2))
12957 : {
12958 401881 : enum tree_code comp_code = TREE_CODE (arg0);
12959 401881 : tree arg00 = TREE_OPERAND (arg0, 0);
12960 401881 : tree arg01 = TREE_OPERAND (arg0, 1);
12961 401881 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12962 401881 : if (comp_code != ERROR_MARK)
12963 401880 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12964 : arg00,
12965 : arg01,
12966 : op2, op1);
12967 401881 : if (tem)
12968 : return tem;
12969 : }
12970 :
12971 : /* If the second operand is simpler than the third, swap them
12972 : since that produces better jump optimization results. */
12973 11908622 : if (truth_value_p (TREE_CODE (arg0))
12974 11908622 : && tree_swap_operands_p (op1, op2))
12975 : {
12976 2093856 : location_t loc0 = expr_location_or (arg0, loc);
12977 : /* See if this can be inverted. If it can't, possibly because
12978 : it was a floating-point inequality comparison, don't do
12979 : anything. */
12980 2093856 : tem = fold_invert_truthvalue (loc0, arg0);
12981 2093856 : if (tem)
12982 1347167 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12983 : }
12984 :
12985 : /* Convert A ? 1 : 0 to simply A. */
12986 10561455 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12987 10105174 : : (integer_onep (op1)
12988 414456 : && !VECTOR_TYPE_P (type)))
12989 693889 : && integer_zerop (op2)
12990 : /* If we try to convert OP0 to our type, the
12991 : call to fold will try to move the conversion inside
12992 : a COND, which will recurse. In that case, the COND_EXPR
12993 : is probably the best choice, so leave it alone. */
12994 11697570 : && type == TREE_TYPE (arg0))
12995 32653 : return protected_set_expr_location_unshare (arg0, loc);
12996 :
12997 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12998 : over COND_EXPR in cases such as floating point comparisons. */
12999 10528802 : if (integer_zerop (op1)
13000 388053 : && code == COND_EXPR
13001 364121 : && integer_onep (op2)
13002 32595 : && !VECTOR_TYPE_P (type)
13003 10561397 : && truth_value_p (TREE_CODE (arg0)))
13004 31048 : return fold_convert_loc (loc, type,
13005 31048 : invert_truthvalue_loc (loc, arg0));
13006 :
13007 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13008 10497754 : if (TREE_CODE (arg0) == LT_EXPR
13009 1368848 : && integer_zerop (TREE_OPERAND (arg0, 1))
13010 38347 : && integer_zerop (op2)
13011 10499131 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13012 : {
13013 : /* sign_bit_p looks through both zero and sign extensions,
13014 : but for this optimization only sign extensions are
13015 : usable. */
13016 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13017 56 : while (tem != tem2)
13018 : {
13019 0 : if (TREE_CODE (tem2) != NOP_EXPR
13020 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13021 : {
13022 : tem = NULL_TREE;
13023 : break;
13024 : }
13025 0 : tem2 = TREE_OPERAND (tem2, 0);
13026 : }
13027 : /* sign_bit_p only checks ARG1 bits within A's precision.
13028 : If <sign bit of A> has wider type than A, bits outside
13029 : of A's precision in <sign bit of A> need to be checked.
13030 : If they are all 0, this optimization needs to be done
13031 : in unsigned A's type, if they are all 1 in signed A's type,
13032 : otherwise this can't be done. */
13033 56 : if (tem
13034 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13035 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13036 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13037 56 : < TYPE_PRECISION (type))
13038 : {
13039 56 : int inner_width, outer_width;
13040 56 : tree tem_type;
13041 :
13042 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13043 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13044 56 : if (outer_width > TYPE_PRECISION (type))
13045 0 : outer_width = TYPE_PRECISION (type);
13046 :
13047 56 : wide_int mask = wi::shifted_mask
13048 56 : (inner_width, outer_width - inner_width, false,
13049 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13050 :
13051 56 : wide_int common = mask & wi::to_wide (arg1);
13052 56 : if (common == mask)
13053 : {
13054 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13055 28 : tem = fold_convert_loc (loc, tem_type, tem);
13056 : }
13057 28 : else if (common == 0)
13058 : {
13059 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13060 0 : tem = fold_convert_loc (loc, tem_type, tem);
13061 : }
13062 : else
13063 : tem = NULL;
13064 56 : }
13065 :
13066 56 : if (tem)
13067 28 : return
13068 56 : fold_convert_loc (loc, type,
13069 : fold_build2_loc (loc, BIT_AND_EXPR,
13070 28 : TREE_TYPE (tem), tem,
13071 : fold_convert_loc (loc,
13072 28 : TREE_TYPE (tem),
13073 28 : arg1)));
13074 : }
13075 :
13076 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13077 : already handled above. */
13078 10497726 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13079 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13080 3 : && integer_zerop (op2)
13081 10497726 : && integer_pow2p (arg1))
13082 : {
13083 0 : tree tem = TREE_OPERAND (arg0, 0);
13084 0 : STRIP_NOPS (tem);
13085 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13086 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13087 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13088 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13089 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13090 : fold_convert_loc (loc, type,
13091 0 : TREE_OPERAND (tem, 0)),
13092 0 : op1);
13093 : }
13094 :
13095 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13096 : is probably obsolete because the first operand should be a
13097 : truth value (that's why we have the two cases above), but let's
13098 : leave it in until we can confirm this for all front-ends. */
13099 10497726 : if (integer_zerop (op2)
13100 2094077 : && TREE_CODE (arg0) == NE_EXPR
13101 541227 : && integer_zerop (TREE_OPERAND (arg0, 1))
13102 291470 : && integer_pow2p (arg1)
13103 32416 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13104 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13105 : arg1, OEP_ONLY_CONST)
13106 : /* operand_equal_p compares just value, not precision, so e.g.
13107 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13108 : second operand 32-bit -128, which is not a power of two (or vice
13109 : versa. */
13110 10497726 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13111 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13112 :
13113 : /* Disable the transformations below for vectors, since
13114 : fold_binary_op_with_conditional_arg may undo them immediately,
13115 : yielding an infinite loop. */
13116 10497726 : if (code == VEC_COND_EXPR)
13117 : return NULL_TREE;
13118 :
13119 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13120 10041445 : if (integer_zerop (op2)
13121 1737490 : && truth_value_p (TREE_CODE (arg0))
13122 1543483 : && truth_value_p (TREE_CODE (arg1))
13123 10075299 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13124 33854 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13125 : : TRUTH_ANDIF_EXPR,
13126 33854 : type, fold_convert_loc (loc, type, arg0), op1);
13127 :
13128 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13129 10007591 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13130 457264 : && truth_value_p (TREE_CODE (arg0))
13131 309323 : && truth_value_p (TREE_CODE (arg1))
13132 10045221 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13133 : {
13134 37630 : location_t loc0 = expr_location_or (arg0, loc);
13135 : /* Only perform transformation if ARG0 is easily inverted. */
13136 37630 : tem = fold_invert_truthvalue (loc0, arg0);
13137 37630 : if (tem)
13138 37318 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13139 : ? BIT_IOR_EXPR
13140 : : TRUTH_ORIF_EXPR,
13141 : type, fold_convert_loc (loc, type, tem),
13142 37318 : op1);
13143 : }
13144 :
13145 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13146 9970273 : if (integer_zerop (arg1)
13147 333150 : && truth_value_p (TREE_CODE (arg0))
13148 83672 : && truth_value_p (TREE_CODE (op2))
13149 9970301 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13150 : {
13151 28 : location_t loc0 = expr_location_or (arg0, loc);
13152 : /* Only perform transformation if ARG0 is easily inverted. */
13153 28 : tem = fold_invert_truthvalue (loc0, arg0);
13154 28 : if (tem)
13155 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13156 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13157 : type, fold_convert_loc (loc, type, tem),
13158 0 : op2);
13159 : }
13160 :
13161 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13162 9970273 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13163 381803 : && truth_value_p (TREE_CODE (arg0))
13164 272825 : && truth_value_p (TREE_CODE (op2))
13165 9970459 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13166 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13167 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13168 186 : type, fold_convert_loc (loc, type, arg0), op2);
13169 :
13170 : return NULL_TREE;
13171 :
13172 0 : case CALL_EXPR:
13173 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13174 : of fold_ternary on them. */
13175 0 : gcc_unreachable ();
13176 :
13177 910292 : case BIT_FIELD_REF:
13178 910292 : if (TREE_CODE (arg0) == VECTOR_CST
13179 81981 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13180 42817 : || (VECTOR_TYPE_P (type)
13181 41999 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13182 81135 : && tree_fits_uhwi_p (op1)
13183 991427 : && tree_fits_uhwi_p (op2))
13184 : {
13185 81135 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13186 81135 : unsigned HOST_WIDE_INT width
13187 81135 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13188 81135 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13189 81135 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13190 81135 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13191 :
13192 81135 : if (n != 0
13193 81135 : && (idx % width) == 0
13194 81135 : && (n % width) == 0
13195 162270 : && known_le ((idx + n) / width,
13196 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13197 : {
13198 81135 : idx = idx / width;
13199 81135 : n = n / width;
13200 :
13201 81135 : if (TREE_CODE (arg0) == VECTOR_CST)
13202 : {
13203 81135 : if (n == 1)
13204 : {
13205 39168 : tem = VECTOR_CST_ELT (arg0, idx);
13206 39168 : if (VECTOR_TYPE_P (type))
13207 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13208 : return tem;
13209 : }
13210 :
13211 41967 : tree_vector_builder vals (type, n, 1);
13212 227578 : for (unsigned i = 0; i < n; ++i)
13213 143644 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13214 41967 : return vals.build ();
13215 41967 : }
13216 : }
13217 : }
13218 :
13219 : /* On constants we can use native encode/interpret to constant
13220 : fold (nearly) all BIT_FIELD_REFs. */
13221 829157 : if (CONSTANT_CLASS_P (arg0)
13222 1651 : && can_native_interpret_type_p (type)
13223 : && BITS_PER_UNIT == 8
13224 1651 : && tree_fits_uhwi_p (op1)
13225 830808 : && tree_fits_uhwi_p (op2))
13226 : {
13227 1651 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13228 1651 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13229 : /* Limit us to a reasonable amount of work. To relax the
13230 : other limitations we need bit-shifting of the buffer
13231 : and rounding up the size. */
13232 1651 : if (bitpos % BITS_PER_UNIT == 0
13233 1651 : && bitsize % BITS_PER_UNIT == 0
13234 1651 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13235 : {
13236 1651 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13237 1651 : unsigned HOST_WIDE_INT len
13238 1651 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13239 1651 : bitpos / BITS_PER_UNIT);
13240 1651 : if (len > 0
13241 1651 : && len * BITS_PER_UNIT >= bitsize)
13242 : {
13243 1651 : tree v = native_interpret_expr (type, b,
13244 : bitsize / BITS_PER_UNIT);
13245 1651 : if (v)
13246 1597 : return v;
13247 : }
13248 : }
13249 : }
13250 :
13251 : return NULL_TREE;
13252 :
13253 792682 : case VEC_PERM_EXPR:
13254 : /* Perform constant folding of BIT_INSERT_EXPR. */
13255 792682 : if (TREE_CODE (arg2) == VECTOR_CST
13256 781104 : && TREE_CODE (op0) == VECTOR_CST
13257 16727 : && TREE_CODE (op1) == VECTOR_CST)
13258 : {
13259 : /* Build a vector of integers from the tree mask. */
13260 3951 : vec_perm_builder builder;
13261 3951 : if (!tree_to_vec_perm_builder (&builder, arg2))
13262 : return NULL_TREE;
13263 :
13264 : /* Create a vec_perm_indices for the integer vector. */
13265 3951 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13266 3951 : bool single_arg = (op0 == op1);
13267 7902 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13268 3951 : return fold_vec_perm (type, op0, op1, sel);
13269 7902 : }
13270 : return NULL_TREE;
13271 :
13272 13254 : case BIT_INSERT_EXPR:
13273 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13274 13254 : if (TREE_CODE (arg0) == INTEGER_CST
13275 14 : && TREE_CODE (arg1) == INTEGER_CST)
13276 : {
13277 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13278 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13279 2 : if (BYTES_BIG_ENDIAN)
13280 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13281 2 : wide_int tem = (wi::to_wide (arg0)
13282 4 : & wi::shifted_mask (bitpos, bitsize, true,
13283 4 : TYPE_PRECISION (type)));
13284 2 : wide_int tem2
13285 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13286 2 : bitsize), bitpos);
13287 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13288 2 : }
13289 13252 : else if (TREE_CODE (arg0) == VECTOR_CST
13290 909 : && CONSTANT_CLASS_P (arg1)
13291 13551 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13292 299 : TREE_TYPE (arg1)))
13293 : {
13294 299 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13295 299 : unsigned HOST_WIDE_INT elsize
13296 299 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13297 299 : if (bitpos % elsize == 0)
13298 : {
13299 299 : unsigned k = bitpos / elsize;
13300 299 : unsigned HOST_WIDE_INT nelts;
13301 299 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13302 47888345 : return arg0;
13303 292 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13304 : {
13305 292 : tree_vector_builder elts (type, nelts, 1);
13306 292 : elts.quick_grow (nelts);
13307 1608 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13308 1024 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13309 292 : return elts.build ();
13310 292 : }
13311 : }
13312 : }
13313 : return NULL_TREE;
13314 :
13315 : default:
13316 : return NULL_TREE;
13317 : } /* switch (code) */
13318 : }
13319 :
13320 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13321 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13322 : constructor element index of the value returned. If the element is
13323 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13324 : the index of the element after the ACCESS_INDEX position (which
13325 : may be outside of the CTOR array). */
13326 :
13327 : tree
13328 721181 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13329 : unsigned *ctor_idx)
13330 : {
13331 721181 : tree index_type = NULL_TREE;
13332 721181 : signop index_sgn = UNSIGNED;
13333 721181 : offset_int low_bound = 0;
13334 :
13335 721181 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13336 : {
13337 721181 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13338 721181 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13339 : {
13340 : /* Static constructors for variably sized objects makes no sense. */
13341 721181 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13342 721181 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13343 : /* ??? When it is obvious that the range is signed, treat it so. */
13344 721181 : if (TYPE_UNSIGNED (index_type)
13345 367447 : && TYPE_MAX_VALUE (domain_type)
13346 1088597 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13347 367416 : TYPE_MIN_VALUE (domain_type)))
13348 : {
13349 0 : index_sgn = SIGNED;
13350 0 : low_bound
13351 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13352 : SIGNED);
13353 : }
13354 : else
13355 : {
13356 721181 : index_sgn = TYPE_SIGN (index_type);
13357 721181 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13358 : }
13359 : }
13360 : }
13361 :
13362 721181 : if (index_type)
13363 721181 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13364 : index_sgn);
13365 :
13366 721181 : offset_int index = low_bound;
13367 721181 : if (index_type)
13368 721181 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13369 :
13370 721181 : offset_int max_index = index;
13371 721181 : unsigned cnt;
13372 721181 : tree cfield, cval;
13373 721181 : bool first_p = true;
13374 :
13375 17799075 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13376 : {
13377 : /* Array constructor might explicitly set index, or specify a range,
13378 : or leave index NULL meaning that it is next index after previous
13379 : one. */
13380 17798012 : if (cfield)
13381 : {
13382 6333186 : if (TREE_CODE (cfield) == INTEGER_CST)
13383 12665068 : max_index = index
13384 6332534 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13385 : else
13386 : {
13387 652 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13388 652 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13389 : index_sgn);
13390 652 : max_index
13391 652 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13392 : index_sgn);
13393 652 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13394 : }
13395 : }
13396 11464826 : else if (!first_p)
13397 : {
13398 11252204 : index = max_index + 1;
13399 11252204 : if (index_type)
13400 11252204 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13401 11252204 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13402 11252204 : max_index = index;
13403 : }
13404 : else
13405 : first_p = false;
13406 :
13407 17798012 : if (TREE_CODE (cval) == RAW_DATA_CST)
13408 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13409 :
13410 : /* Do we have match? */
13411 17798012 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13412 : {
13413 17797724 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13414 : {
13415 720002 : if (ctor_idx)
13416 720002 : *ctor_idx = cnt;
13417 : return cval;
13418 : }
13419 : }
13420 288 : else if (in_gimple_form)
13421 : /* We're past the element we search for. Note during parsing
13422 : the elements might not be sorted.
13423 : ??? We should use a binary search and a flag on the
13424 : CONSTRUCTOR as to whether elements are sorted in declaration
13425 : order. */
13426 : break;
13427 : }
13428 1179 : if (ctor_idx)
13429 1179 : *ctor_idx = cnt;
13430 : return NULL_TREE;
13431 : }
13432 :
13433 : /* Perform constant folding and related simplification of EXPR.
13434 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13435 : and application of the associative law.
13436 : NOP_EXPR conversions may be removed freely (as long as we
13437 : are careful not to change the type of the overall expression).
13438 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13439 : but we can constant-fold them if they have constant operands. */
13440 :
13441 : #ifdef ENABLE_FOLD_CHECKING
13442 : # define fold(x) fold_1 (x)
13443 : static tree fold_1 (tree);
13444 : static
13445 : #endif
13446 : tree
13447 1388195599 : fold (tree expr)
13448 : {
13449 1388371818 : const tree t = expr;
13450 1388371818 : enum tree_code code = TREE_CODE (t);
13451 1388371818 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13452 1388371818 : tree tem;
13453 1388371818 : location_t loc = EXPR_LOCATION (expr);
13454 :
13455 : /* Return right away if a constant. */
13456 1388371818 : if (kind == tcc_constant)
13457 : return t;
13458 :
13459 : /* CALL_EXPR-like objects with variable numbers of operands are
13460 : treated specially. */
13461 1282158256 : if (kind == tcc_vl_exp)
13462 : {
13463 181574472 : if (code == CALL_EXPR)
13464 : {
13465 181573944 : tem = fold_call_expr (loc, expr, false);
13466 181573944 : return tem ? tem : expr;
13467 : }
13468 : return expr;
13469 : }
13470 :
13471 1100583784 : if (IS_EXPR_CODE_CLASS (kind))
13472 : {
13473 1098317796 : tree type = TREE_TYPE (t);
13474 1098317796 : tree op0, op1, op2;
13475 :
13476 1098317796 : switch (TREE_CODE_LENGTH (code))
13477 : {
13478 997074947 : case 1:
13479 997074947 : op0 = TREE_OPERAND (t, 0);
13480 997074947 : tem = fold_unary_loc (loc, code, type, op0);
13481 997074947 : return tem ? tem : expr;
13482 91971142 : case 2:
13483 91971142 : op0 = TREE_OPERAND (t, 0);
13484 91971142 : op1 = TREE_OPERAND (t, 1);
13485 91971142 : tem = fold_binary_loc (loc, code, type, op0, op1);
13486 91971142 : return tem ? tem : expr;
13487 4460856 : case 3:
13488 4460856 : op0 = TREE_OPERAND (t, 0);
13489 4460856 : op1 = TREE_OPERAND (t, 1);
13490 4460856 : op2 = TREE_OPERAND (t, 2);
13491 4460856 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13492 4460856 : return tem ? tem : expr;
13493 : default:
13494 : break;
13495 : }
13496 : }
13497 :
13498 7076839 : switch (code)
13499 : {
13500 4706398 : case ARRAY_REF:
13501 4706398 : {
13502 4706398 : tree op0 = TREE_OPERAND (t, 0);
13503 4706398 : tree op1 = TREE_OPERAND (t, 1);
13504 :
13505 4706398 : if (TREE_CODE (op1) == INTEGER_CST
13506 2956489 : && TREE_CODE (op0) == CONSTRUCTOR
13507 4707853 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13508 : {
13509 1455 : unsigned int idx;
13510 1455 : tree val
13511 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13512 : &idx);
13513 1455 : if (val)
13514 : {
13515 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13516 : return val;
13517 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13518 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13519 : != INTEGER_CST))
13520 : return t;
13521 2 : offset_int o
13522 2 : = (wi::to_offset (op1)
13523 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13524 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13525 2 : return build_int_cst (TREE_TYPE (val),
13526 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13527 : }
13528 : }
13529 :
13530 : return t;
13531 : }
13532 :
13533 : /* Return a VECTOR_CST if possible. */
13534 206682 : case CONSTRUCTOR:
13535 206682 : {
13536 206682 : tree type = TREE_TYPE (t);
13537 206682 : if (TREE_CODE (type) != VECTOR_TYPE)
13538 : return t;
13539 :
13540 : unsigned i;
13541 : tree val;
13542 364184 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13543 315029 : if (! CONSTANT_CLASS_P (val))
13544 : return t;
13545 :
13546 49155 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13547 : }
13548 :
13549 176219 : case CONST_DECL:
13550 176219 : return fold (DECL_INITIAL (t));
13551 :
13552 : default:
13553 : return t;
13554 : } /* switch (code) */
13555 : }
13556 :
13557 : #ifdef ENABLE_FOLD_CHECKING
13558 : #undef fold
13559 :
13560 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13561 : hash_table<nofree_ptr_hash<const tree_node> > *);
13562 : static void fold_check_failed (const_tree, const_tree);
13563 : void print_fold_checksum (const_tree);
13564 :
13565 : /* When --enable-checking=fold, compute a digest of expr before
13566 : and after actual fold call to see if fold did not accidentally
13567 : change original expr. */
13568 :
13569 : tree
13570 : fold (tree expr)
13571 : {
13572 : tree ret;
13573 : struct md5_ctx ctx;
13574 : unsigned char checksum_before[16], checksum_after[16];
13575 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13576 :
13577 : md5_init_ctx (&ctx);
13578 : fold_checksum_tree (expr, &ctx, &ht);
13579 : md5_finish_ctx (&ctx, checksum_before);
13580 : ht.empty ();
13581 :
13582 : ret = fold_1 (expr);
13583 :
13584 : md5_init_ctx (&ctx);
13585 : fold_checksum_tree (expr, &ctx, &ht);
13586 : md5_finish_ctx (&ctx, checksum_after);
13587 :
13588 : if (memcmp (checksum_before, checksum_after, 16))
13589 : fold_check_failed (expr, ret);
13590 :
13591 : return ret;
13592 : }
13593 :
13594 : void
13595 : print_fold_checksum (const_tree expr)
13596 : {
13597 : struct md5_ctx ctx;
13598 : unsigned char checksum[16], cnt;
13599 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13600 :
13601 : md5_init_ctx (&ctx);
13602 : fold_checksum_tree (expr, &ctx, &ht);
13603 : md5_finish_ctx (&ctx, checksum);
13604 : for (cnt = 0; cnt < 16; ++cnt)
13605 : fprintf (stderr, "%02x", checksum[cnt]);
13606 : putc ('\n', stderr);
13607 : }
13608 :
13609 : static void
13610 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13611 : {
13612 : internal_error ("fold check: original tree changed by fold");
13613 : }
13614 :
13615 : static void
13616 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13617 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13618 : {
13619 : const tree_node **slot;
13620 : enum tree_code code;
13621 : union tree_node *buf;
13622 : int i, len;
13623 :
13624 : recursive_label:
13625 : if (expr == NULL)
13626 : return;
13627 : slot = ht->find_slot (expr, INSERT);
13628 : if (*slot != NULL)
13629 : return;
13630 : *slot = expr;
13631 : code = TREE_CODE (expr);
13632 : if (TREE_CODE_CLASS (code) == tcc_declaration
13633 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13634 : {
13635 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13636 : size_t sz = tree_size (expr);
13637 : buf = XALLOCAVAR (union tree_node, sz);
13638 : memcpy ((char *) buf, expr, sz);
13639 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13640 : buf->decl_with_vis.symtab_node = NULL;
13641 : buf->base.nowarning_flag = 0;
13642 : expr = (tree) buf;
13643 : }
13644 : else if (TREE_CODE_CLASS (code) == tcc_type
13645 : && (TYPE_POINTER_TO (expr)
13646 : || TYPE_REFERENCE_TO (expr)
13647 : || TYPE_CACHED_VALUES_P (expr)
13648 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13649 : || TYPE_NEXT_VARIANT (expr)
13650 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13651 : {
13652 : /* Allow these fields to be modified. */
13653 : tree tmp;
13654 : size_t sz = tree_size (expr);
13655 : buf = XALLOCAVAR (union tree_node, sz);
13656 : memcpy ((char *) buf, expr, sz);
13657 : expr = tmp = (tree) buf;
13658 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13659 : TYPE_POINTER_TO (tmp) = NULL;
13660 : TYPE_REFERENCE_TO (tmp) = NULL;
13661 : TYPE_NEXT_VARIANT (tmp) = NULL;
13662 : TYPE_ALIAS_SET (tmp) = -1;
13663 : if (TYPE_CACHED_VALUES_P (tmp))
13664 : {
13665 : TYPE_CACHED_VALUES_P (tmp) = 0;
13666 : TYPE_CACHED_VALUES (tmp) = NULL;
13667 : }
13668 : }
13669 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13670 : {
13671 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13672 : that and change builtins.cc etc. instead - see PR89543. */
13673 : size_t sz = tree_size (expr);
13674 : buf = XALLOCAVAR (union tree_node, sz);
13675 : memcpy ((char *) buf, expr, sz);
13676 : buf->base.nowarning_flag = 0;
13677 : expr = (tree) buf;
13678 : }
13679 : md5_process_bytes (expr, tree_size (expr), ctx);
13680 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13681 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13682 : if (TREE_CODE_CLASS (code) != tcc_type
13683 : && TREE_CODE_CLASS (code) != tcc_declaration
13684 : && code != TREE_LIST
13685 : && code != SSA_NAME
13686 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13687 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13688 : switch (TREE_CODE_CLASS (code))
13689 : {
13690 : case tcc_constant:
13691 : switch (code)
13692 : {
13693 : case STRING_CST:
13694 : md5_process_bytes (TREE_STRING_POINTER (expr),
13695 : TREE_STRING_LENGTH (expr), ctx);
13696 : break;
13697 : case COMPLEX_CST:
13698 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13699 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13700 : break;
13701 : case VECTOR_CST:
13702 : len = vector_cst_encoded_nelts (expr);
13703 : for (i = 0; i < len; ++i)
13704 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13705 : break;
13706 : default:
13707 : break;
13708 : }
13709 : break;
13710 : case tcc_exceptional:
13711 : switch (code)
13712 : {
13713 : case TREE_LIST:
13714 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13715 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13716 : expr = TREE_CHAIN (expr);
13717 : goto recursive_label;
13718 : break;
13719 : case TREE_VEC:
13720 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13721 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13722 : break;
13723 : default:
13724 : break;
13725 : }
13726 : break;
13727 : case tcc_expression:
13728 : case tcc_reference:
13729 : case tcc_comparison:
13730 : case tcc_unary:
13731 : case tcc_binary:
13732 : case tcc_statement:
13733 : case tcc_vl_exp:
13734 : len = TREE_OPERAND_LENGTH (expr);
13735 : for (i = 0; i < len; ++i)
13736 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13737 : break;
13738 : case tcc_declaration:
13739 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13740 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13741 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13742 : {
13743 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13744 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13745 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13746 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13747 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13748 : }
13749 :
13750 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13751 : {
13752 : if (TREE_CODE (expr) == FUNCTION_DECL)
13753 : {
13754 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13755 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13756 : }
13757 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13758 : }
13759 : break;
13760 : case tcc_type:
13761 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13762 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13763 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13764 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13765 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13766 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13767 : if (INTEGRAL_TYPE_P (expr)
13768 : || SCALAR_FLOAT_TYPE_P (expr))
13769 : {
13770 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13771 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13772 : }
13773 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13774 : if (RECORD_OR_UNION_TYPE_P (expr))
13775 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13776 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13777 : break;
13778 : default:
13779 : break;
13780 : }
13781 : }
13782 :
13783 : /* Helper function for outputting the checksum of a tree T. When
13784 : debugging with gdb, you can "define mynext" to be "next" followed
13785 : by "call debug_fold_checksum (op0)", then just trace down till the
13786 : outputs differ. */
13787 :
13788 : DEBUG_FUNCTION void
13789 : debug_fold_checksum (const_tree t)
13790 : {
13791 : int i;
13792 : unsigned char checksum[16];
13793 : struct md5_ctx ctx;
13794 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13795 :
13796 : md5_init_ctx (&ctx);
13797 : fold_checksum_tree (t, &ctx, &ht);
13798 : md5_finish_ctx (&ctx, checksum);
13799 : ht.empty ();
13800 :
13801 : for (i = 0; i < 16; i++)
13802 : fprintf (stderr, "%d ", checksum[i]);
13803 :
13804 : fprintf (stderr, "\n");
13805 : }
13806 :
13807 : #endif
13808 :
13809 : /* Fold a unary tree expression with code CODE of type TYPE with an
13810 : operand OP0. LOC is the location of the resulting expression.
13811 : Return a folded expression if successful. Otherwise, return a tree
13812 : expression with code CODE of type TYPE with an operand OP0. */
13813 :
13814 : tree
13815 1062868442 : fold_build1_loc (location_t loc,
13816 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13817 : {
13818 1062868442 : tree tem;
13819 : #ifdef ENABLE_FOLD_CHECKING
13820 : unsigned char checksum_before[16], checksum_after[16];
13821 : struct md5_ctx ctx;
13822 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13823 :
13824 : md5_init_ctx (&ctx);
13825 : fold_checksum_tree (op0, &ctx, &ht);
13826 : md5_finish_ctx (&ctx, checksum_before);
13827 : ht.empty ();
13828 : #endif
13829 :
13830 1062868442 : tem = fold_unary_loc (loc, code, type, op0);
13831 1062868442 : if (!tem)
13832 541044506 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13833 :
13834 : #ifdef ENABLE_FOLD_CHECKING
13835 : md5_init_ctx (&ctx);
13836 : fold_checksum_tree (op0, &ctx, &ht);
13837 : md5_finish_ctx (&ctx, checksum_after);
13838 :
13839 : if (memcmp (checksum_before, checksum_after, 16))
13840 : fold_check_failed (op0, tem);
13841 : #endif
13842 1062868442 : return tem;
13843 : }
13844 :
13845 : /* Fold a binary tree expression with code CODE of type TYPE with
13846 : operands OP0 and OP1. LOC is the location of the resulting
13847 : expression. Return a folded expression if successful. Otherwise,
13848 : return a tree expression with code CODE of type TYPE with operands
13849 : OP0 and OP1. */
13850 :
13851 : tree
13852 678391677 : fold_build2_loc (location_t loc,
13853 : enum tree_code code, tree type, tree op0, tree op1
13854 : MEM_STAT_DECL)
13855 : {
13856 678391677 : tree tem;
13857 : #ifdef ENABLE_FOLD_CHECKING
13858 : unsigned char checksum_before_op0[16],
13859 : checksum_before_op1[16],
13860 : checksum_after_op0[16],
13861 : checksum_after_op1[16];
13862 : struct md5_ctx ctx;
13863 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13864 :
13865 : md5_init_ctx (&ctx);
13866 : fold_checksum_tree (op0, &ctx, &ht);
13867 : md5_finish_ctx (&ctx, checksum_before_op0);
13868 : ht.empty ();
13869 :
13870 : md5_init_ctx (&ctx);
13871 : fold_checksum_tree (op1, &ctx, &ht);
13872 : md5_finish_ctx (&ctx, checksum_before_op1);
13873 : ht.empty ();
13874 : #endif
13875 :
13876 678391677 : tem = fold_binary_loc (loc, code, type, op0, op1);
13877 678391677 : if (!tem)
13878 382378900 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13879 :
13880 : #ifdef ENABLE_FOLD_CHECKING
13881 : md5_init_ctx (&ctx);
13882 : fold_checksum_tree (op0, &ctx, &ht);
13883 : md5_finish_ctx (&ctx, checksum_after_op0);
13884 : ht.empty ();
13885 :
13886 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13887 : fold_check_failed (op0, tem);
13888 :
13889 : md5_init_ctx (&ctx);
13890 : fold_checksum_tree (op1, &ctx, &ht);
13891 : md5_finish_ctx (&ctx, checksum_after_op1);
13892 :
13893 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13894 : fold_check_failed (op1, tem);
13895 : #endif
13896 678391677 : return tem;
13897 : }
13898 :
13899 : /* Fold a ternary tree expression with code CODE of type TYPE with
13900 : operands OP0, OP1, and OP2. Return a folded expression if
13901 : successful. Otherwise, return a tree expression with code CODE of
13902 : type TYPE with operands OP0, OP1, and OP2. */
13903 :
13904 : tree
13905 41250734 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13906 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13907 : {
13908 41250734 : tree tem;
13909 : #ifdef ENABLE_FOLD_CHECKING
13910 : unsigned char checksum_before_op0[16],
13911 : checksum_before_op1[16],
13912 : checksum_before_op2[16],
13913 : checksum_after_op0[16],
13914 : checksum_after_op1[16],
13915 : checksum_after_op2[16];
13916 : struct md5_ctx ctx;
13917 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13918 :
13919 : md5_init_ctx (&ctx);
13920 : fold_checksum_tree (op0, &ctx, &ht);
13921 : md5_finish_ctx (&ctx, checksum_before_op0);
13922 : ht.empty ();
13923 :
13924 : md5_init_ctx (&ctx);
13925 : fold_checksum_tree (op1, &ctx, &ht);
13926 : md5_finish_ctx (&ctx, checksum_before_op1);
13927 : ht.empty ();
13928 :
13929 : md5_init_ctx (&ctx);
13930 : fold_checksum_tree (op2, &ctx, &ht);
13931 : md5_finish_ctx (&ctx, checksum_before_op2);
13932 : ht.empty ();
13933 : #endif
13934 :
13935 41250734 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13936 41250734 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13937 41250734 : if (!tem)
13938 38329382 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13939 :
13940 : #ifdef ENABLE_FOLD_CHECKING
13941 : md5_init_ctx (&ctx);
13942 : fold_checksum_tree (op0, &ctx, &ht);
13943 : md5_finish_ctx (&ctx, checksum_after_op0);
13944 : ht.empty ();
13945 :
13946 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13947 : fold_check_failed (op0, tem);
13948 :
13949 : md5_init_ctx (&ctx);
13950 : fold_checksum_tree (op1, &ctx, &ht);
13951 : md5_finish_ctx (&ctx, checksum_after_op1);
13952 : ht.empty ();
13953 :
13954 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13955 : fold_check_failed (op1, tem);
13956 :
13957 : md5_init_ctx (&ctx);
13958 : fold_checksum_tree (op2, &ctx, &ht);
13959 : md5_finish_ctx (&ctx, checksum_after_op2);
13960 :
13961 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13962 : fold_check_failed (op2, tem);
13963 : #endif
13964 41250734 : return tem;
13965 : }
13966 :
13967 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13968 : arguments in ARGARRAY, and a null static chain.
13969 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13970 : of type TYPE from the given operands as constructed by build_call_array. */
13971 :
13972 : tree
13973 57875477 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13974 : int nargs, tree *argarray)
13975 : {
13976 57875477 : tree tem;
13977 : #ifdef ENABLE_FOLD_CHECKING
13978 : unsigned char checksum_before_fn[16],
13979 : checksum_before_arglist[16],
13980 : checksum_after_fn[16],
13981 : checksum_after_arglist[16];
13982 : struct md5_ctx ctx;
13983 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13984 : int i;
13985 :
13986 : md5_init_ctx (&ctx);
13987 : fold_checksum_tree (fn, &ctx, &ht);
13988 : md5_finish_ctx (&ctx, checksum_before_fn);
13989 : ht.empty ();
13990 :
13991 : md5_init_ctx (&ctx);
13992 : for (i = 0; i < nargs; i++)
13993 : fold_checksum_tree (argarray[i], &ctx, &ht);
13994 : md5_finish_ctx (&ctx, checksum_before_arglist);
13995 : ht.empty ();
13996 : #endif
13997 :
13998 57875477 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13999 57875477 : if (!tem)
14000 55747035 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14001 :
14002 : #ifdef ENABLE_FOLD_CHECKING
14003 : md5_init_ctx (&ctx);
14004 : fold_checksum_tree (fn, &ctx, &ht);
14005 : md5_finish_ctx (&ctx, checksum_after_fn);
14006 : ht.empty ();
14007 :
14008 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14009 : fold_check_failed (fn, tem);
14010 :
14011 : md5_init_ctx (&ctx);
14012 : for (i = 0; i < nargs; i++)
14013 : fold_checksum_tree (argarray[i], &ctx, &ht);
14014 : md5_finish_ctx (&ctx, checksum_after_arglist);
14015 :
14016 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14017 : fold_check_failed (NULL_TREE, tem);
14018 : #endif
14019 57875477 : return tem;
14020 : }
14021 :
14022 : /* Perform constant folding and related simplification of initializer
14023 : expression EXPR. These behave identically to "fold_buildN" but ignore
14024 : potential run-time traps and exceptions that fold must preserve. */
14025 :
14026 : #define START_FOLD_INIT \
14027 : int saved_signaling_nans = flag_signaling_nans;\
14028 : int saved_trapping_math = flag_trapping_math;\
14029 : int saved_rounding_math = flag_rounding_math;\
14030 : int saved_trapv = flag_trapv;\
14031 : int saved_folding_initializer = folding_initializer;\
14032 : flag_signaling_nans = 0;\
14033 : flag_trapping_math = 0;\
14034 : flag_rounding_math = 0;\
14035 : flag_trapv = 0;\
14036 : folding_initializer = 1;
14037 :
14038 : #define END_FOLD_INIT \
14039 : flag_signaling_nans = saved_signaling_nans;\
14040 : flag_trapping_math = saved_trapping_math;\
14041 : flag_rounding_math = saved_rounding_math;\
14042 : flag_trapv = saved_trapv;\
14043 : folding_initializer = saved_folding_initializer;
14044 :
14045 : tree
14046 550842 : fold_init (tree expr)
14047 : {
14048 550842 : tree result;
14049 550842 : START_FOLD_INIT;
14050 :
14051 550842 : result = fold (expr);
14052 :
14053 550842 : END_FOLD_INIT;
14054 550842 : return result;
14055 : }
14056 :
14057 : tree
14058 2990676 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14059 : tree type, tree op)
14060 : {
14061 2990676 : tree result;
14062 2990676 : START_FOLD_INIT;
14063 :
14064 2990676 : result = fold_build1_loc (loc, code, type, op);
14065 :
14066 2990676 : END_FOLD_INIT;
14067 2990676 : return result;
14068 : }
14069 :
14070 : tree
14071 50647 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14072 : tree type, tree op0, tree op1)
14073 : {
14074 50647 : tree result;
14075 50647 : START_FOLD_INIT;
14076 :
14077 50647 : result = fold_build2_loc (loc, code, type, op0, op1);
14078 :
14079 50647 : END_FOLD_INIT;
14080 50647 : return result;
14081 : }
14082 :
14083 : tree
14084 3516 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14085 : int nargs, tree *argarray)
14086 : {
14087 3516 : tree result;
14088 3516 : START_FOLD_INIT;
14089 :
14090 3516 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14091 :
14092 3516 : END_FOLD_INIT;
14093 3516 : return result;
14094 : }
14095 :
14096 : tree
14097 68460957 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14098 : tree lhs, tree rhs)
14099 : {
14100 68460957 : tree result;
14101 68460957 : START_FOLD_INIT;
14102 :
14103 68460957 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14104 :
14105 68460957 : END_FOLD_INIT;
14106 68460957 : return result;
14107 : }
14108 :
14109 : #undef START_FOLD_INIT
14110 : #undef END_FOLD_INIT
14111 :
14112 : /* Determine if first argument is a multiple of second argument. Return
14113 : false if it is not, or we cannot easily determined it to be.
14114 :
14115 : An example of the sort of thing we care about (at this point; this routine
14116 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14117 : fold cases do now) is discovering that
14118 :
14119 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14120 :
14121 : is a multiple of
14122 :
14123 : SAVE_EXPR (J * 8)
14124 :
14125 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14126 :
14127 : This code also handles discovering that
14128 :
14129 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14130 :
14131 : is a multiple of 8 so we don't have to worry about dealing with a
14132 : possible remainder.
14133 :
14134 : Note that we *look* inside a SAVE_EXPR only to determine how it was
14135 : calculated; it is not safe for fold to do much of anything else with the
14136 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14137 : at run time. For example, the latter example above *cannot* be implemented
14138 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14139 : evaluation time of the original SAVE_EXPR is not necessarily the same at
14140 : the time the new expression is evaluated. The only optimization of this
14141 : sort that would be valid is changing
14142 :
14143 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14144 :
14145 : divided by 8 to
14146 :
14147 : SAVE_EXPR (I) * SAVE_EXPR (J)
14148 :
14149 : (where the same SAVE_EXPR (J) is used in the original and the
14150 : transformed version).
14151 :
14152 : NOWRAP specifies whether all outer operations in TYPE should
14153 : be considered not wrapping. Any type conversion within TOP acts
14154 : as a barrier and we will fall back to NOWRAP being false.
14155 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14156 : as not wrapping even though they are generally using unsigned arithmetic. */
14157 :
14158 : bool
14159 1606776 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14160 : {
14161 1661490 : gimple *stmt;
14162 1661490 : tree op1, op2;
14163 :
14164 1661490 : if (operand_equal_p (top, bottom, 0))
14165 : return true;
14166 :
14167 1139517 : if (TREE_CODE (type) != INTEGER_TYPE)
14168 : return false;
14169 :
14170 1139498 : switch (TREE_CODE (top))
14171 : {
14172 730 : case BIT_AND_EXPR:
14173 : /* Bitwise and provides a power of two multiple. If the mask is
14174 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14175 730 : if (!integer_pow2p (bottom))
14176 : return false;
14177 730 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14178 730 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14179 :
14180 419895 : case MULT_EXPR:
14181 : /* If the multiplication can wrap we cannot recurse further unless
14182 : the bottom is a power of two which is where wrapping does not
14183 : matter. */
14184 419895 : if (!nowrap
14185 16062 : && !TYPE_OVERFLOW_UNDEFINED (type)
14186 424195 : && !integer_pow2p (bottom))
14187 : return false;
14188 419391 : if (TREE_CODE (bottom) == INTEGER_CST)
14189 : {
14190 417991 : op1 = TREE_OPERAND (top, 0);
14191 417991 : op2 = TREE_OPERAND (top, 1);
14192 417991 : if (TREE_CODE (op1) == INTEGER_CST)
14193 0 : std::swap (op1, op2);
14194 417991 : if (TREE_CODE (op2) == INTEGER_CST)
14195 : {
14196 407567 : if (multiple_of_p (type, op2, bottom, nowrap))
14197 : return true;
14198 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14199 3326 : if (multiple_of_p (type, bottom, op2, nowrap))
14200 : {
14201 1909 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14202 1909 : wi::to_widest (op2));
14203 1909 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14204 : {
14205 1909 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14206 1909 : return multiple_of_p (type, op1, op2, nowrap);
14207 : }
14208 1909 : }
14209 : return multiple_of_p (type, op1, bottom, nowrap);
14210 : }
14211 : }
14212 11824 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14213 11824 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14214 :
14215 400 : case LSHIFT_EXPR:
14216 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14217 400 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14218 : {
14219 400 : op1 = TREE_OPERAND (top, 1);
14220 400 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14221 : {
14222 400 : wide_int mul_op
14223 400 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14224 800 : return multiple_of_p (type,
14225 800 : wide_int_to_tree (type, mul_op), bottom,
14226 : nowrap);
14227 400 : }
14228 : }
14229 : return false;
14230 :
14231 233944 : case MINUS_EXPR:
14232 233944 : case PLUS_EXPR:
14233 : /* If the addition or subtraction can wrap we cannot recurse further
14234 : unless bottom is a power of two which is where wrapping does not
14235 : matter. */
14236 233944 : if (!nowrap
14237 182011 : && !TYPE_OVERFLOW_UNDEFINED (type)
14238 414503 : && !integer_pow2p (bottom))
14239 : return false;
14240 :
14241 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14242 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14243 : but 0xfffffffd is not. */
14244 203424 : op1 = TREE_OPERAND (top, 1);
14245 203424 : if (TREE_CODE (top) == PLUS_EXPR
14246 197400 : && nowrap
14247 45915 : && TYPE_UNSIGNED (type)
14248 248585 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14249 28550 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14250 :
14251 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14252 : precisely, so be conservative here checking if both op0 and op1
14253 : are multiple of bottom. Note we check the second operand first
14254 : since it's usually simpler. */
14255 203424 : return (multiple_of_p (type, op1, bottom, nowrap)
14256 203424 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14257 :
14258 150754 : CASE_CONVERT:
14259 : /* Can't handle conversions from non-integral or wider integral type. */
14260 150754 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14261 150754 : || (TYPE_PRECISION (type)
14262 40282 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14263 : return false;
14264 : /* NOWRAP only extends to operations in the outermost type so
14265 : make sure to strip it off here. */
14266 40020 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14267 80040 : TREE_OPERAND (top, 0), bottom, false);
14268 :
14269 13277 : case SAVE_EXPR:
14270 13277 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14271 :
14272 86 : case COND_EXPR:
14273 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14274 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14275 :
14276 146898 : case INTEGER_CST:
14277 146898 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14278 : return false;
14279 144449 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14280 : SIGNED);
14281 :
14282 61708 : case SSA_NAME:
14283 61708 : if (TREE_CODE (bottom) == INTEGER_CST
14284 58864 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14285 120572 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14286 : {
14287 24219 : enum tree_code code = gimple_assign_rhs_code (stmt);
14288 :
14289 : /* Check for special cases to see if top is defined as multiple
14290 : of bottom:
14291 :
14292 : top = (X & ~(bottom - 1) ; bottom is power of 2
14293 :
14294 : or
14295 :
14296 : Y = X % bottom
14297 : top = X - Y. */
14298 24219 : if (code == BIT_AND_EXPR
14299 584 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14300 584 : && TREE_CODE (op2) == INTEGER_CST
14301 476 : && integer_pow2p (bottom)
14302 24695 : && wi::multiple_of_p (wi::to_widest (op2),
14303 476 : wi::to_widest (bottom), SIGNED))
14304 467 : return true;
14305 :
14306 23752 : op1 = gimple_assign_rhs1 (stmt);
14307 23752 : if (code == MINUS_EXPR
14308 2062 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14309 2062 : && TREE_CODE (op2) == SSA_NAME
14310 2062 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14311 2062 : && gimple_code (stmt) == GIMPLE_ASSIGN
14312 1700 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14313 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14314 23816 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14315 : return true;
14316 : }
14317 :
14318 : /* fall through */
14319 :
14320 : default:
14321 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14322 : return multiple_p (wi::to_poly_widest (top),
14323 : wi::to_poly_widest (bottom));
14324 :
14325 : return false;
14326 : }
14327 : }
14328 :
14329 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14330 : This function returns true for integer expressions, and returns
14331 : false if uncertain. */
14332 :
14333 : bool
14334 645801 : tree_expr_finite_p (const_tree x)
14335 : {
14336 645805 : machine_mode mode = element_mode (x);
14337 645805 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14338 : return true;
14339 645503 : switch (TREE_CODE (x))
14340 : {
14341 760 : case REAL_CST:
14342 760 : return real_isfinite (TREE_REAL_CST_PTR (x));
14343 0 : case COMPLEX_CST:
14344 0 : return tree_expr_finite_p (TREE_REALPART (x))
14345 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14346 : case FLOAT_EXPR:
14347 : return true;
14348 4 : case ABS_EXPR:
14349 4 : case CONVERT_EXPR:
14350 4 : case NON_LVALUE_EXPR:
14351 4 : case NEGATE_EXPR:
14352 4 : case SAVE_EXPR:
14353 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14354 0 : case MIN_EXPR:
14355 0 : case MAX_EXPR:
14356 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14357 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14358 0 : case COND_EXPR:
14359 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14360 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14361 38 : case CALL_EXPR:
14362 38 : switch (get_call_combined_fn (x))
14363 : {
14364 0 : CASE_CFN_FABS:
14365 0 : CASE_CFN_FABS_FN:
14366 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14367 0 : CASE_CFN_FMAX:
14368 0 : CASE_CFN_FMAX_FN:
14369 0 : CASE_CFN_FMIN:
14370 0 : CASE_CFN_FMIN_FN:
14371 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14372 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14373 : default:
14374 : return false;
14375 : }
14376 :
14377 : default:
14378 : return false;
14379 : }
14380 : }
14381 :
14382 : /* Return true if expression X evaluates to an infinity.
14383 : This function returns false for integer expressions. */
14384 :
14385 : bool
14386 1336972 : tree_expr_infinite_p (const_tree x)
14387 : {
14388 1337422 : if (!HONOR_INFINITIES (x))
14389 : return false;
14390 1337177 : switch (TREE_CODE (x))
14391 : {
14392 0 : case REAL_CST:
14393 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14394 450 : case ABS_EXPR:
14395 450 : case NEGATE_EXPR:
14396 450 : case NON_LVALUE_EXPR:
14397 450 : case SAVE_EXPR:
14398 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14399 0 : case COND_EXPR:
14400 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14401 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14402 : default:
14403 : return false;
14404 : }
14405 : }
14406 :
14407 : /* Return true if expression X could evaluate to an infinity.
14408 : This function returns false for integer expressions, and returns
14409 : true if uncertain. */
14410 :
14411 : bool
14412 811609 : tree_expr_maybe_infinite_p (const_tree x)
14413 : {
14414 811617 : if (!HONOR_INFINITIES (x))
14415 : return false;
14416 811138 : switch (TREE_CODE (x))
14417 : {
14418 365 : case REAL_CST:
14419 365 : return real_isinf (TREE_REAL_CST_PTR (x));
14420 : case FLOAT_EXPR:
14421 : return false;
14422 8 : case ABS_EXPR:
14423 8 : case NEGATE_EXPR:
14424 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14425 1 : case COND_EXPR:
14426 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14427 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14428 : default:
14429 : return true;
14430 : }
14431 : }
14432 :
14433 : /* Return true if expression X evaluates to a signaling NaN.
14434 : This function returns false for integer expressions. */
14435 :
14436 : bool
14437 5762 : tree_expr_signaling_nan_p (const_tree x)
14438 : {
14439 5762 : if (!HONOR_SNANS (x))
14440 : return false;
14441 245 : switch (TREE_CODE (x))
14442 : {
14443 245 : case REAL_CST:
14444 245 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14445 0 : case NON_LVALUE_EXPR:
14446 0 : case SAVE_EXPR:
14447 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14448 0 : case COND_EXPR:
14449 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14450 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14451 : default:
14452 : return false;
14453 : }
14454 : }
14455 :
14456 : /* Return true if expression X could evaluate to a signaling NaN.
14457 : This function returns false for integer expressions, and returns
14458 : true if uncertain. */
14459 :
14460 : bool
14461 725919 : tree_expr_maybe_signaling_nan_p (const_tree x)
14462 : {
14463 725919 : if (!HONOR_SNANS (x))
14464 : return false;
14465 5672 : switch (TREE_CODE (x))
14466 : {
14467 2138 : case REAL_CST:
14468 2138 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14469 : case PLUS_EXPR:
14470 : case MINUS_EXPR:
14471 : case MULT_EXPR:
14472 : case RDIV_EXPR:
14473 : case MIN_EXPR:
14474 : case MAX_EXPR:
14475 : case FIX_TRUNC_EXPR:
14476 : case FLOAT_EXPR:
14477 : return false;
14478 0 : case ABS_EXPR:
14479 0 : case CONVERT_EXPR:
14480 0 : case NEGATE_EXPR:
14481 0 : case NON_LVALUE_EXPR:
14482 0 : case SAVE_EXPR:
14483 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14484 0 : case COND_EXPR:
14485 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14486 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14487 0 : case CALL_EXPR:
14488 0 : switch (get_call_combined_fn (x))
14489 : {
14490 0 : CASE_CFN_FABS:
14491 0 : CASE_CFN_FABS_FN:
14492 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14493 0 : CASE_CFN_COPYSIGN:
14494 0 : CASE_CFN_COPYSIGN_FN:
14495 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14496 : CASE_CFN_ATAN:
14497 : CASE_CFN_ATAN_FN:
14498 : CASE_CFN_CEIL:
14499 : CASE_CFN_CEIL_FN:
14500 : CASE_CFN_COS:
14501 : CASE_CFN_COS_FN:
14502 : CASE_CFN_ERF:
14503 : CASE_CFN_ERF_FN:
14504 : CASE_CFN_EXP:
14505 : CASE_CFN_EXP_FN:
14506 : CASE_CFN_FLOOR:
14507 : CASE_CFN_FLOOR_FN:
14508 : CASE_CFN_FMAX:
14509 : CASE_CFN_FMAX_FN:
14510 : CASE_CFN_FMIN:
14511 : CASE_CFN_FMIN_FN:
14512 : CASE_CFN_FMOD:
14513 : CASE_CFN_FMOD_FN:
14514 : CASE_CFN_HYPOT:
14515 : CASE_CFN_HYPOT_FN:
14516 : CASE_CFN_MODF:
14517 : CASE_CFN_MODF_FN:
14518 : CASE_CFN_POW:
14519 : CASE_CFN_POW_FN:
14520 : CASE_CFN_ROUND:
14521 : CASE_CFN_ROUND_FN:
14522 : CASE_CFN_SIN:
14523 : CASE_CFN_SIN_FN:
14524 : CASE_CFN_SQRT:
14525 : CASE_CFN_SQRT_FN:
14526 : CASE_CFN_TAN:
14527 : CASE_CFN_TAN_FN:
14528 : CASE_CFN_TRUNC:
14529 : CASE_CFN_TRUNC_FN:
14530 : return false;
14531 : default:
14532 : return true;
14533 : }
14534 : default:
14535 : return true;
14536 : }
14537 : }
14538 :
14539 : /* Return true if expression X evaluates to a NaN.
14540 : This function returns false for integer expressions. */
14541 :
14542 : bool
14543 3953159 : tree_expr_nan_p (const_tree x)
14544 : {
14545 4308193 : if (!HONOR_NANS (x))
14546 : return false;
14547 4307843 : switch (TREE_CODE (x))
14548 : {
14549 3806 : case REAL_CST:
14550 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14551 355034 : case NON_LVALUE_EXPR:
14552 355034 : case SAVE_EXPR:
14553 355034 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14554 956 : case COND_EXPR:
14555 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14556 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14557 : default:
14558 : return false;
14559 : }
14560 : }
14561 :
14562 : /* Return true if expression X could evaluate to a NaN.
14563 : This function returns false for integer expressions, and returns
14564 : true if uncertain. */
14565 :
14566 : bool
14567 5171886 : tree_expr_maybe_nan_p (const_tree x)
14568 : {
14569 7564807 : if (!HONOR_NANS (x))
14570 : return false;
14571 7480688 : switch (TREE_CODE (x))
14572 : {
14573 3797 : case REAL_CST:
14574 3797 : return real_isnan (TREE_REAL_CST_PTR (x));
14575 : case FLOAT_EXPR:
14576 : return false;
14577 14944 : case PLUS_EXPR:
14578 14944 : case MINUS_EXPR:
14579 14944 : case MULT_EXPR:
14580 14944 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14581 14944 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14582 2392921 : case ABS_EXPR:
14583 2392921 : case CONVERT_EXPR:
14584 2392921 : case NEGATE_EXPR:
14585 2392921 : case NON_LVALUE_EXPR:
14586 2392921 : case SAVE_EXPR:
14587 2392921 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14588 176 : case MIN_EXPR:
14589 176 : case MAX_EXPR:
14590 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14591 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14592 603 : case COND_EXPR:
14593 603 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14594 603 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14595 1094 : case CALL_EXPR:
14596 1094 : switch (get_call_combined_fn (x))
14597 : {
14598 0 : CASE_CFN_FABS:
14599 0 : CASE_CFN_FABS_FN:
14600 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14601 108 : CASE_CFN_FMAX:
14602 108 : CASE_CFN_FMAX_FN:
14603 108 : CASE_CFN_FMIN:
14604 108 : CASE_CFN_FMIN_FN:
14605 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14606 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14607 : default:
14608 : return true;
14609 : }
14610 : default:
14611 : return true;
14612 : }
14613 : }
14614 :
14615 : /* Return true if expression X could evaluate to -0.0.
14616 : This function returns true if uncertain. */
14617 :
14618 : bool
14619 596374 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14620 : {
14621 596374 : if (!HONOR_SIGNED_ZEROS (x))
14622 : return false;
14623 596367 : switch (TREE_CODE (x))
14624 : {
14625 0 : case REAL_CST:
14626 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14627 : case INTEGER_CST:
14628 : case FLOAT_EXPR:
14629 : case ABS_EXPR:
14630 : return false;
14631 0 : case NON_LVALUE_EXPR:
14632 0 : case SAVE_EXPR:
14633 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14634 1 : case COND_EXPR:
14635 1 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14636 1 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14637 2 : case CALL_EXPR:
14638 2 : switch (get_call_combined_fn (x))
14639 : {
14640 : CASE_CFN_FABS:
14641 : CASE_CFN_FABS_FN:
14642 : return false;
14643 : default:
14644 : break;
14645 : }
14646 : default:
14647 : break;
14648 : }
14649 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14650 : * but currently those predicates require tree and not const_tree. */
14651 : return true;
14652 : }
14653 :
14654 : #define tree_expr_nonnegative_p(X, Y) \
14655 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14656 :
14657 : #define RECURSE(X) \
14658 : ((tree_expr_nonnegative_p) (X, depth + 1))
14659 :
14660 : /* Return true if CODE or TYPE is known to be non-negative. */
14661 :
14662 : static bool
14663 26781487 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14664 : {
14665 26781487 : if (!VECTOR_TYPE_P (type)
14666 26743256 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14667 53524478 : && truth_value_p (code))
14668 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14669 : have a signed:1 type (where the value is -1 and 0). */
14670 : return true;
14671 : return false;
14672 : }
14673 :
14674 : /* Return true if (CODE OP0) is known to be non-negative.
14675 : DEPTH is the current nesting depth of the query. */
14676 :
14677 : bool
14678 2459376 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14679 : {
14680 2459376 : if (TYPE_UNSIGNED (type))
14681 : return true;
14682 :
14683 2002675 : switch (code)
14684 : {
14685 283882 : case ABS_EXPR:
14686 : /* We can't return 1 if flag_wrapv is set because
14687 : ABS_EXPR<INT_MIN> = INT_MIN. */
14688 283882 : if (!ANY_INTEGRAL_TYPE_P (type))
14689 : return true;
14690 1172 : if (TYPE_OVERFLOW_UNDEFINED (type))
14691 : return true;
14692 : break;
14693 :
14694 75410 : case NON_LVALUE_EXPR:
14695 75410 : case FLOAT_EXPR:
14696 75410 : case FIX_TRUNC_EXPR:
14697 75410 : return RECURSE (op0);
14698 :
14699 1604640 : CASE_CONVERT:
14700 1604640 : {
14701 1604640 : tree inner_type = TREE_TYPE (op0);
14702 1604640 : tree outer_type = type;
14703 :
14704 1604640 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14705 : {
14706 406135 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14707 406135 : return RECURSE (op0);
14708 0 : if (INTEGRAL_TYPE_P (inner_type))
14709 : {
14710 0 : if (TYPE_UNSIGNED (inner_type))
14711 : return true;
14712 0 : return RECURSE (op0);
14713 : }
14714 : }
14715 1198505 : else if (INTEGRAL_TYPE_P (outer_type))
14716 : {
14717 1198428 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14718 0 : return RECURSE (op0);
14719 1198428 : if (INTEGRAL_TYPE_P (inner_type))
14720 1191789 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14721 1191789 : && TYPE_UNSIGNED (inner_type);
14722 : }
14723 : }
14724 : break;
14725 :
14726 38743 : default:
14727 38743 : return tree_simple_nonnegative_warnv_p (code, type);
14728 : }
14729 :
14730 : /* We don't know sign of `t', so be conservative and return false. */
14731 : return false;
14732 : }
14733 :
14734 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14735 : DEPTH is the current nesting depth of the query. */
14736 :
14737 : bool
14738 4801471 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14739 : tree op1, int depth)
14740 : {
14741 4801471 : if (TYPE_UNSIGNED (type))
14742 : return true;
14743 :
14744 4488549 : switch (code)
14745 : {
14746 1093524 : case POINTER_PLUS_EXPR:
14747 1093524 : case PLUS_EXPR:
14748 1093524 : if (FLOAT_TYPE_P (type))
14749 48259 : return RECURSE (op0) && RECURSE (op1);
14750 :
14751 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14752 : both unsigned and at least 2 bits shorter than the result. */
14753 1045265 : if (TREE_CODE (type) == INTEGER_TYPE
14754 1038860 : && TREE_CODE (op0) == NOP_EXPR
14755 17771 : && TREE_CODE (op1) == NOP_EXPR)
14756 : {
14757 205 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14758 205 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14759 205 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14760 306 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14761 : {
14762 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14763 95 : TYPE_PRECISION (inner2)) + 1;
14764 95 : return prec < TYPE_PRECISION (type);
14765 : }
14766 : }
14767 : break;
14768 :
14769 164947 : case MULT_EXPR:
14770 164947 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14771 : {
14772 : /* x * x is always non-negative for floating point x
14773 : or without overflow. */
14774 149038 : if (operand_equal_p (op0, op1, 0)
14775 149038 : || (RECURSE (op0) && RECURSE (op1)))
14776 : return true;
14777 : }
14778 :
14779 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14780 : both unsigned and their total bits is shorter than the result. */
14781 162666 : if (TREE_CODE (type) == INTEGER_TYPE
14782 92450 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14783 168 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14784 : {
14785 154 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14786 154 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14787 154 : : TREE_TYPE (op0);
14788 154 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14789 154 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14790 154 : : TREE_TYPE (op1);
14791 :
14792 154 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14793 154 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14794 :
14795 154 : if (TREE_CODE (op0) == INTEGER_CST)
14796 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14797 :
14798 154 : if (TREE_CODE (op1) == INTEGER_CST)
14799 77 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14800 :
14801 154 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14802 14 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14803 : {
14804 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14805 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14806 0 : : TYPE_PRECISION (inner0);
14807 :
14808 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14809 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14810 0 : : TYPE_PRECISION (inner1);
14811 :
14812 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14813 : }
14814 : }
14815 : return false;
14816 :
14817 10113 : case BIT_AND_EXPR:
14818 10113 : return RECURSE (op0) || RECURSE (op1);
14819 :
14820 35764 : case MAX_EXPR:
14821 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14822 : things. */
14823 35764 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14824 76 : return RECURSE (op0) && RECURSE (op1);
14825 35688 : return RECURSE (op0) || RECURSE (op1);
14826 :
14827 150000 : case BIT_IOR_EXPR:
14828 150000 : case BIT_XOR_EXPR:
14829 150000 : case MIN_EXPR:
14830 150000 : case RDIV_EXPR:
14831 150000 : case TRUNC_DIV_EXPR:
14832 150000 : case CEIL_DIV_EXPR:
14833 150000 : case FLOOR_DIV_EXPR:
14834 150000 : case ROUND_DIV_EXPR:
14835 150000 : return RECURSE (op0) && RECURSE (op1);
14836 :
14837 54317 : case TRUNC_MOD_EXPR:
14838 54317 : return RECURSE (op0);
14839 :
14840 252 : case FLOOR_MOD_EXPR:
14841 252 : return RECURSE (op1);
14842 :
14843 2979632 : case CEIL_MOD_EXPR:
14844 2979632 : case ROUND_MOD_EXPR:
14845 2979632 : default:
14846 2979632 : return tree_simple_nonnegative_warnv_p (code, type);
14847 : }
14848 :
14849 : /* We don't know sign of `t', so be conservative and return false. */
14850 : return false;
14851 : }
14852 :
14853 : /* Return true if T is known to be non-negative.
14854 : DEPTH is the current nesting depth of the query. */
14855 :
14856 : bool
14857 25752434 : tree_single_nonnegative_p (tree t, int depth)
14858 : {
14859 25752434 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14860 : return true;
14861 :
14862 21724792 : switch (TREE_CODE (t))
14863 : {
14864 2479759 : case INTEGER_CST:
14865 2479759 : return tree_int_cst_sgn (t) >= 0;
14866 :
14867 925171 : case REAL_CST:
14868 925171 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14869 :
14870 0 : case FIXED_CST:
14871 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14872 :
14873 937 : case COND_EXPR:
14874 937 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14875 :
14876 8769659 : case SSA_NAME:
14877 : /* For integral types, query the range if possible. */
14878 8769659 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14879 : {
14880 7340682 : int_range_max r;
14881 14681364 : get_range_query (cfun)->range_of_expr (r, t);
14882 7340682 : if (!r.undefined_p () && !r.varying_p())
14883 : {
14884 1774418 : if (r.nonnegative_p ())
14885 : return true;
14886 1165457 : if (r.nonpositive_p () && !range_includes_zero_p (r))
14887 : return false;
14888 : }
14889 7340682 : }
14890 : /* Limit the depth of recursion to avoid quadratic behavior.
14891 : This is expected to catch almost all occurrences in practice.
14892 : If this code misses important cases that unbounded recursion
14893 : would not, passes that need this information could be revised
14894 : to provide it through dataflow propagation. */
14895 8156903 : return (!name_registered_for_update_p (t)
14896 8156902 : && depth < param_max_ssa_name_query_depth
14897 16184744 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14898 :
14899 9549266 : default:
14900 9549266 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14901 : }
14902 : }
14903 :
14904 : /* Return true if T is known to be non-negative.
14905 : DEPTH is the current nesting depth of the query. */
14906 :
14907 : bool
14908 14256408 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14909 : int depth)
14910 : {
14911 14256408 : switch (fn)
14912 : {
14913 : CASE_CFN_ACOS:
14914 : CASE_CFN_ACOS_FN:
14915 : CASE_CFN_ACOSH:
14916 : CASE_CFN_ACOSH_FN:
14917 : CASE_CFN_ACOSPI:
14918 : CASE_CFN_ACOSPI_FN:
14919 : CASE_CFN_CABS:
14920 : CASE_CFN_CABS_FN:
14921 : CASE_CFN_COSH:
14922 : CASE_CFN_COSH_FN:
14923 : CASE_CFN_ERFC:
14924 : CASE_CFN_ERFC_FN:
14925 : CASE_CFN_EXP:
14926 : CASE_CFN_EXP_FN:
14927 : CASE_CFN_EXP10:
14928 : CASE_CFN_EXP2:
14929 : CASE_CFN_EXP2_FN:
14930 : CASE_CFN_FABS:
14931 : CASE_CFN_FABS_FN:
14932 : CASE_CFN_FDIM:
14933 : CASE_CFN_FDIM_FN:
14934 : CASE_CFN_HYPOT:
14935 : CASE_CFN_HYPOT_FN:
14936 : CASE_CFN_POW10:
14937 : CASE_CFN_FFS:
14938 : CASE_CFN_PARITY:
14939 : CASE_CFN_POPCOUNT:
14940 : CASE_CFN_CLRSB:
14941 : CASE_CFN_BSWAP:
14942 : CASE_CFN_BITREVERSE:
14943 : case CFN_CLMUL:
14944 : case CFN_BITCOMPRESS:
14945 : case CFN_BITEXPAND:
14946 : /* Always true. */
14947 : return true;
14948 :
14949 176 : CASE_CFN_CLZ:
14950 176 : CASE_CFN_CTZ:
14951 176 : if (arg1)
14952 2 : return RECURSE (arg1);
14953 : return true;
14954 :
14955 847 : CASE_CFN_SQRT:
14956 847 : CASE_CFN_SQRT_FN:
14957 : /* sqrt(-0.0) is -0.0. */
14958 847 : if (!HONOR_SIGNED_ZEROS (type))
14959 : return true;
14960 815 : return RECURSE (arg0);
14961 :
14962 23807 : CASE_CFN_ASINH:
14963 23807 : CASE_CFN_ASINH_FN:
14964 23807 : CASE_CFN_ASINPI:
14965 23807 : CASE_CFN_ASINPI_FN:
14966 23807 : CASE_CFN_ATAN:
14967 23807 : CASE_CFN_ATAN_FN:
14968 23807 : CASE_CFN_ATANH:
14969 23807 : CASE_CFN_ATANH_FN:
14970 23807 : CASE_CFN_ATANPI:
14971 23807 : CASE_CFN_ATANPI_FN:
14972 23807 : CASE_CFN_CBRT:
14973 23807 : CASE_CFN_CBRT_FN:
14974 23807 : CASE_CFN_CEIL:
14975 23807 : CASE_CFN_CEIL_FN:
14976 23807 : CASE_CFN_ERF:
14977 23807 : CASE_CFN_ERF_FN:
14978 23807 : CASE_CFN_EXPM1:
14979 23807 : CASE_CFN_EXPM1_FN:
14980 23807 : CASE_CFN_FLOOR:
14981 23807 : CASE_CFN_FLOOR_FN:
14982 23807 : CASE_CFN_FMOD:
14983 23807 : CASE_CFN_FMOD_FN:
14984 23807 : CASE_CFN_FREXP:
14985 23807 : CASE_CFN_FREXP_FN:
14986 23807 : CASE_CFN_ICEIL:
14987 23807 : CASE_CFN_IFLOOR:
14988 23807 : CASE_CFN_IRINT:
14989 23807 : CASE_CFN_IROUND:
14990 23807 : CASE_CFN_LCEIL:
14991 23807 : CASE_CFN_LDEXP:
14992 23807 : CASE_CFN_LFLOOR:
14993 23807 : CASE_CFN_LLCEIL:
14994 23807 : CASE_CFN_LLFLOOR:
14995 23807 : CASE_CFN_LLRINT:
14996 23807 : CASE_CFN_LLRINT_FN:
14997 23807 : CASE_CFN_LLROUND:
14998 23807 : CASE_CFN_LLROUND_FN:
14999 23807 : CASE_CFN_LRINT:
15000 23807 : CASE_CFN_LRINT_FN:
15001 23807 : CASE_CFN_LROUND:
15002 23807 : CASE_CFN_LROUND_FN:
15003 23807 : CASE_CFN_MODF:
15004 23807 : CASE_CFN_MODF_FN:
15005 23807 : CASE_CFN_NEARBYINT:
15006 23807 : CASE_CFN_NEARBYINT_FN:
15007 23807 : CASE_CFN_RINT:
15008 23807 : CASE_CFN_RINT_FN:
15009 23807 : CASE_CFN_ROUND:
15010 23807 : CASE_CFN_ROUND_FN:
15011 23807 : CASE_CFN_ROUNDEVEN:
15012 23807 : CASE_CFN_ROUNDEVEN_FN:
15013 23807 : CASE_CFN_SCALB:
15014 23807 : CASE_CFN_SCALBLN:
15015 23807 : CASE_CFN_SCALBLN_FN:
15016 23807 : CASE_CFN_SCALBN:
15017 23807 : CASE_CFN_SCALBN_FN:
15018 23807 : CASE_CFN_SIGNBIT:
15019 23807 : CASE_CFN_SIGNIFICAND:
15020 23807 : CASE_CFN_SINH:
15021 23807 : CASE_CFN_SINH_FN:
15022 23807 : CASE_CFN_TANH:
15023 23807 : CASE_CFN_TANH_FN:
15024 23807 : CASE_CFN_TRUNC:
15025 23807 : CASE_CFN_TRUNC_FN:
15026 : /* True if the 1st argument is nonnegative. */
15027 23807 : return RECURSE (arg0);
15028 :
15029 1329 : CASE_CFN_FMAX:
15030 1329 : CASE_CFN_FMAX_FN:
15031 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15032 : things. In the presence of sNaNs, we're only guaranteed to be
15033 : non-negative if both operands are non-negative. In the presence
15034 : of qNaNs, we're non-negative if either operand is non-negative
15035 : and can't be a qNaN, or if both operands are non-negative. */
15036 1329 : if (tree_expr_maybe_signaling_nan_p (arg0)
15037 1329 : || tree_expr_maybe_signaling_nan_p (arg1))
15038 136 : return RECURSE (arg0) && RECURSE (arg1);
15039 1193 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15040 332 : || RECURSE (arg1))
15041 861 : : (RECURSE (arg1)
15042 861 : && !tree_expr_maybe_nan_p (arg1));
15043 :
15044 910 : CASE_CFN_FMIN:
15045 910 : CASE_CFN_FMIN_FN:
15046 : /* True if the 1st AND 2nd arguments are nonnegative. */
15047 910 : return RECURSE (arg0) && RECURSE (arg1);
15048 :
15049 963 : CASE_CFN_COPYSIGN:
15050 963 : CASE_CFN_COPYSIGN_FN:
15051 : /* True if the 2nd argument is nonnegative. */
15052 963 : return RECURSE (arg1);
15053 :
15054 2317 : CASE_CFN_POWI:
15055 : /* True if the 1st argument is nonnegative or the second
15056 : argument is an even integer. */
15057 2317 : if (TREE_CODE (arg1) == INTEGER_CST
15058 2317 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15059 : return true;
15060 2233 : return RECURSE (arg0);
15061 :
15062 5584 : CASE_CFN_POW:
15063 5584 : CASE_CFN_POW_FN:
15064 : /* True if the 1st argument is nonnegative or the second
15065 : argument is an even integer valued real. */
15066 5584 : if (TREE_CODE (arg1) == REAL_CST)
15067 : {
15068 2299 : REAL_VALUE_TYPE c;
15069 2299 : HOST_WIDE_INT n;
15070 :
15071 2299 : c = TREE_REAL_CST (arg1);
15072 2299 : n = real_to_integer (&c);
15073 2299 : if ((n & 1) == 0)
15074 : {
15075 1656 : REAL_VALUE_TYPE cint;
15076 1656 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15077 1656 : if (real_identical (&c, &cint))
15078 609 : return true;
15079 : }
15080 : }
15081 4975 : return RECURSE (arg0);
15082 :
15083 14210972 : default:
15084 14210972 : break;
15085 : }
15086 14210972 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15087 : }
15088 :
15089 : /* Return true if T is known to be non-negative.
15090 : DEPTH is the current nesting depth of the query. */
15091 :
15092 : static bool
15093 1719405 : tree_invalid_nonnegative_p (tree t, int depth)
15094 : {
15095 1719405 : enum tree_code code = TREE_CODE (t);
15096 1719405 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15097 : return true;
15098 :
15099 1306659 : switch (code)
15100 : {
15101 269 : case TARGET_EXPR:
15102 269 : {
15103 269 : tree temp = TARGET_EXPR_SLOT (t);
15104 269 : t = TARGET_EXPR_INITIAL (t);
15105 :
15106 : /* If the initializer is non-void, then it's a normal expression
15107 : that will be assigned to the slot. */
15108 269 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15109 66 : return RECURSE (t);
15110 :
15111 : /* Otherwise, the initializer sets the slot in some way. One common
15112 : way is an assignment statement at the end of the initializer. */
15113 406 : while (1)
15114 : {
15115 406 : if (TREE_CODE (t) == BIND_EXPR)
15116 203 : t = expr_last (BIND_EXPR_BODY (t));
15117 203 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15118 203 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15119 0 : t = expr_last (TREE_OPERAND (t, 0));
15120 203 : else if (TREE_CODE (t) == STATEMENT_LIST)
15121 0 : t = expr_last (t);
15122 : else
15123 : break;
15124 : }
15125 203 : if (TREE_CODE (t) == MODIFY_EXPR
15126 203 : && TREE_OPERAND (t, 0) == temp)
15127 203 : return RECURSE (TREE_OPERAND (t, 1));
15128 :
15129 : return false;
15130 : }
15131 :
15132 634692 : case CALL_EXPR:
15133 634692 : {
15134 634692 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15135 634692 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15136 :
15137 634692 : return tree_call_nonnegative_p (TREE_TYPE (t),
15138 : get_call_combined_fn (t),
15139 : arg0,
15140 : arg1,
15141 634692 : depth);
15142 : }
15143 3392 : case COMPOUND_EXPR:
15144 3392 : case MODIFY_EXPR:
15145 3392 : return RECURSE (TREE_OPERAND (t, 1));
15146 :
15147 15 : case BIND_EXPR:
15148 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15149 :
15150 665417 : case SAVE_EXPR:
15151 665417 : return RECURSE (TREE_OPERAND (t, 0));
15152 :
15153 2874 : default:
15154 2874 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15155 : }
15156 : }
15157 :
15158 : #undef RECURSE
15159 : #undef tree_expr_nonnegative_p
15160 :
15161 : /* Return true if T is known to be non-negative.
15162 : DEPTH is the current nesting depth of the query. */
15163 :
15164 : bool
15165 27218290 : tree_expr_nonnegative_p (tree t, int depth)
15166 : {
15167 27218290 : enum tree_code code;
15168 27218290 : if (error_operand_p (t))
15169 : return false;
15170 :
15171 27218289 : code = TREE_CODE (t);
15172 27218289 : switch (TREE_CODE_CLASS (code))
15173 : {
15174 1385840 : case tcc_binary:
15175 1385840 : case tcc_comparison:
15176 1385840 : return tree_binary_nonnegative_p (TREE_CODE (t),
15177 1385840 : TREE_TYPE (t),
15178 1385840 : TREE_OPERAND (t, 0),
15179 1385840 : TREE_OPERAND (t, 1),
15180 1385840 : depth);
15181 :
15182 1985542 : case tcc_unary:
15183 1985542 : return tree_unary_nonnegative_p (TREE_CODE (t),
15184 1985542 : TREE_TYPE (t),
15185 1985542 : TREE_OPERAND (t, 0),
15186 1985542 : depth);
15187 :
15188 13545771 : case tcc_constant:
15189 13545771 : case tcc_declaration:
15190 13545771 : case tcc_reference:
15191 13545771 : return tree_single_nonnegative_p (t, depth);
15192 :
15193 10301136 : default:
15194 10301136 : break;
15195 : }
15196 :
15197 10301136 : switch (code)
15198 : {
15199 7 : case TRUTH_AND_EXPR:
15200 7 : case TRUTH_OR_EXPR:
15201 7 : case TRUTH_XOR_EXPR:
15202 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15203 7 : TREE_TYPE (t),
15204 7 : TREE_OPERAND (t, 0),
15205 7 : TREE_OPERAND (t, 1),
15206 7 : depth);
15207 96 : case TRUTH_NOT_EXPR:
15208 96 : return tree_unary_nonnegative_p (TREE_CODE (t),
15209 96 : TREE_TYPE (t),
15210 96 : TREE_OPERAND (t, 0),
15211 96 : depth);
15212 :
15213 8581628 : case COND_EXPR:
15214 8581628 : case CONSTRUCTOR:
15215 8581628 : case OBJ_TYPE_REF:
15216 8581628 : case ADDR_EXPR:
15217 8581628 : case WITH_SIZE_EXPR:
15218 8581628 : case SSA_NAME:
15219 8581628 : return tree_single_nonnegative_p (t, depth);
15220 :
15221 1719405 : default:
15222 1719405 : return tree_invalid_nonnegative_p (t, depth);
15223 : }
15224 : }
15225 :
15226 :
15227 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15228 : For floating point we further ensure that T is not denormal.
15229 : Similar logic is present in nonzero_address in rtlanal.h. */
15230 :
15231 : bool
15232 1605342 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15233 : {
15234 1605342 : switch (code)
15235 : {
15236 1 : case ABS_EXPR:
15237 1 : return tree_expr_nonzero_p (op0);
15238 :
15239 928797 : case NOP_EXPR:
15240 928797 : {
15241 928797 : tree inner_type = TREE_TYPE (op0);
15242 928797 : tree outer_type = type;
15243 :
15244 928797 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15245 928797 : && tree_expr_nonzero_p (op0));
15246 : }
15247 28194 : break;
15248 :
15249 28194 : case NON_LVALUE_EXPR:
15250 28194 : return tree_expr_nonzero_p (op0);
15251 :
15252 : default:
15253 : break;
15254 : }
15255 :
15256 : return false;
15257 : }
15258 :
15259 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15260 : For floating point we further ensure that T is not denormal.
15261 : Similar logic is present in nonzero_address in rtlanal.h. */
15262 :
15263 : bool
15264 3002477 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15265 : {
15266 3002477 : switch (code)
15267 : {
15268 482825 : case POINTER_PLUS_EXPR:
15269 482825 : case PLUS_EXPR:
15270 482825 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15271 : {
15272 : /* With the presence of negative values it is hard
15273 : to say something. */
15274 118439 : if (!tree_expr_nonnegative_p (op0)
15275 118439 : || !tree_expr_nonnegative_p (op1))
15276 : return false;
15277 : /* One of operands must be positive and the other non-negative. */
15278 3603 : return (tree_expr_nonzero_p (op0)
15279 3603 : || tree_expr_nonzero_p (op1));
15280 : }
15281 : break;
15282 :
15283 31312 : case MULT_EXPR:
15284 31312 : if (TYPE_OVERFLOW_UNDEFINED (type))
15285 : {
15286 556 : if (tree_expr_nonzero_p (op0)
15287 556 : && tree_expr_nonzero_p (op1))
15288 : return true;
15289 : }
15290 : break;
15291 :
15292 : case MIN_EXPR:
15293 : break;
15294 :
15295 43 : case MAX_EXPR:
15296 43 : if (tree_expr_nonzero_p (op0))
15297 : {
15298 :
15299 : /* When both operands are nonzero, then MAX must be too. */
15300 0 : if (tree_expr_nonzero_p (op1))
15301 : return true;
15302 :
15303 : /* MAX where operand 0 is positive is positive. */
15304 0 : return tree_expr_nonnegative_p (op0);
15305 : }
15306 : /* MAX where operand 1 is positive is positive. */
15307 43 : else if (tree_expr_nonzero_p (op1)
15308 43 : && tree_expr_nonnegative_p (op1))
15309 : return true;
15310 : break;
15311 :
15312 271447 : case BIT_IOR_EXPR:
15313 271447 : return (tree_expr_nonzero_p (op1)
15314 271447 : || tree_expr_nonzero_p (op0));
15315 :
15316 : default:
15317 : break;
15318 : }
15319 :
15320 : return false;
15321 : }
15322 :
15323 : /* Return true when T is an address and is known to be nonzero.
15324 : For floating point we further ensure that T is not denormal.
15325 : Similar logic is present in nonzero_address in rtlanal.h. */
15326 :
15327 : bool
15328 157539151 : tree_single_nonzero_p (tree t)
15329 : {
15330 157539151 : switch (TREE_CODE (t))
15331 : {
15332 1149659 : case INTEGER_CST:
15333 1149659 : return !integer_zerop (t);
15334 :
15335 12057535 : case ADDR_EXPR:
15336 12057535 : {
15337 12057535 : tree base = TREE_OPERAND (t, 0);
15338 :
15339 12057535 : if (!DECL_P (base))
15340 5922833 : base = get_base_address (base);
15341 :
15342 12057535 : if (base && TREE_CODE (base) == TARGET_EXPR)
15343 844 : base = TARGET_EXPR_SLOT (base);
15344 :
15345 844 : if (!base)
15346 : return false;
15347 :
15348 : /* For objects in symbol table check if we know they are non-zero.
15349 : Don't do anything for variables and functions before symtab is built;
15350 : it is quite possible that they will be declared weak later. */
15351 12057535 : int nonzero_addr = maybe_nonzero_address (base);
15352 12057535 : if (nonzero_addr >= 0)
15353 9745925 : return nonzero_addr;
15354 :
15355 : /* Constants are never weak. */
15356 2311610 : if (CONSTANT_CLASS_P (base))
15357 2221365 : return true;
15358 :
15359 : return false;
15360 : }
15361 :
15362 38148 : case COND_EXPR:
15363 38148 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15364 38148 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15365 : return true;
15366 : break;
15367 :
15368 132191923 : case SSA_NAME:
15369 132191923 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15370 : break;
15371 102843432 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15372 :
15373 : default:
15374 : break;
15375 : }
15376 : return false;
15377 : }
15378 :
15379 : #define integer_valued_real_p(X) \
15380 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15381 :
15382 : #define RECURSE(X) \
15383 : ((integer_valued_real_p) (X, depth + 1))
15384 :
15385 : /* Return true if the floating point result of (CODE OP0) has an
15386 : integer value. We also allow +Inf, -Inf and NaN to be considered
15387 : integer values. Return false for signaling NaN.
15388 :
15389 : DEPTH is the current nesting depth of the query. */
15390 :
15391 : bool
15392 15227 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15393 : {
15394 15227 : switch (code)
15395 : {
15396 : case FLOAT_EXPR:
15397 : return true;
15398 :
15399 1450 : case ABS_EXPR:
15400 1450 : return RECURSE (op0);
15401 :
15402 9859 : CASE_CONVERT:
15403 9859 : {
15404 9859 : tree type = TREE_TYPE (op0);
15405 9859 : if (TREE_CODE (type) == INTEGER_TYPE)
15406 : return true;
15407 9859 : if (SCALAR_FLOAT_TYPE_P (type))
15408 9859 : return RECURSE (op0);
15409 : break;
15410 : }
15411 :
15412 : default:
15413 : break;
15414 : }
15415 : return false;
15416 : }
15417 :
15418 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15419 : integer value. We also allow +Inf, -Inf and NaN to be considered
15420 : integer values. Return false for signaling NaN.
15421 :
15422 : DEPTH is the current nesting depth of the query. */
15423 :
15424 : bool
15425 13784 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15426 : {
15427 13784 : switch (code)
15428 : {
15429 8036 : case PLUS_EXPR:
15430 8036 : case MINUS_EXPR:
15431 8036 : case MULT_EXPR:
15432 8036 : case MIN_EXPR:
15433 8036 : case MAX_EXPR:
15434 8036 : return RECURSE (op0) && RECURSE (op1);
15435 :
15436 : default:
15437 : break;
15438 : }
15439 : return false;
15440 : }
15441 :
15442 : /* Return true if the floating point result of calling FNDECL with arguments
15443 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15444 : considered integer values. Return false for signaling NaN. If FNDECL
15445 : takes fewer than 2 arguments, the remaining ARGn are null.
15446 :
15447 : DEPTH is the current nesting depth of the query. */
15448 :
15449 : bool
15450 1125 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15451 : {
15452 1125 : switch (fn)
15453 : {
15454 : CASE_CFN_CEIL:
15455 : CASE_CFN_CEIL_FN:
15456 : CASE_CFN_FLOOR:
15457 : CASE_CFN_FLOOR_FN:
15458 : CASE_CFN_NEARBYINT:
15459 : CASE_CFN_NEARBYINT_FN:
15460 : CASE_CFN_RINT:
15461 : CASE_CFN_RINT_FN:
15462 : CASE_CFN_ROUND:
15463 : CASE_CFN_ROUND_FN:
15464 : CASE_CFN_ROUNDEVEN:
15465 : CASE_CFN_ROUNDEVEN_FN:
15466 : CASE_CFN_TRUNC:
15467 : CASE_CFN_TRUNC_FN:
15468 : return true;
15469 :
15470 336 : CASE_CFN_FMIN:
15471 336 : CASE_CFN_FMIN_FN:
15472 336 : CASE_CFN_FMAX:
15473 336 : CASE_CFN_FMAX_FN:
15474 336 : return RECURSE (arg0) && RECURSE (arg1);
15475 :
15476 : default:
15477 : break;
15478 : }
15479 : return false;
15480 : }
15481 :
15482 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15483 : has an integer value. We also allow +Inf, -Inf and NaN to be
15484 : considered integer values. Return false for signaling NaN.
15485 :
15486 : DEPTH is the current nesting depth of the query. */
15487 :
15488 : bool
15489 130721 : integer_valued_real_single_p (tree t, int depth)
15490 : {
15491 130721 : switch (TREE_CODE (t))
15492 : {
15493 2750 : case REAL_CST:
15494 2750 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15495 :
15496 0 : case COND_EXPR:
15497 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15498 :
15499 91490 : case SSA_NAME:
15500 : /* Limit the depth of recursion to avoid quadratic behavior.
15501 : This is expected to catch almost all occurrences in practice.
15502 : If this code misses important cases that unbounded recursion
15503 : would not, passes that need this information could be revised
15504 : to provide it through dataflow propagation. */
15505 91490 : return (!name_registered_for_update_p (t)
15506 91490 : && depth < param_max_ssa_name_query_depth
15507 182165 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15508 : depth));
15509 :
15510 : default:
15511 : break;
15512 : }
15513 : return false;
15514 : }
15515 :
15516 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15517 : has an integer value. We also allow +Inf, -Inf and NaN to be
15518 : considered integer values. Return false for signaling NaN.
15519 :
15520 : DEPTH is the current nesting depth of the query. */
15521 :
15522 : static bool
15523 0 : integer_valued_real_invalid_p (tree t, int depth)
15524 : {
15525 0 : switch (TREE_CODE (t))
15526 : {
15527 0 : case COMPOUND_EXPR:
15528 0 : case MODIFY_EXPR:
15529 0 : case BIND_EXPR:
15530 0 : return RECURSE (TREE_OPERAND (t, 1));
15531 :
15532 0 : case SAVE_EXPR:
15533 0 : return RECURSE (TREE_OPERAND (t, 0));
15534 :
15535 : default:
15536 : break;
15537 : }
15538 : return false;
15539 : }
15540 :
15541 : #undef RECURSE
15542 : #undef integer_valued_real_p
15543 :
15544 : /* Return true if the floating point expression T has an integer value.
15545 : We also allow +Inf, -Inf and NaN to be considered integer values.
15546 : Return false for signaling NaN.
15547 :
15548 : DEPTH is the current nesting depth of the query. */
15549 :
15550 : bool
15551 98600 : integer_valued_real_p (tree t, int depth)
15552 : {
15553 98600 : if (t == error_mark_node)
15554 : return false;
15555 :
15556 98600 : STRIP_ANY_LOCATION_WRAPPER (t);
15557 :
15558 98600 : tree_code code = TREE_CODE (t);
15559 98600 : switch (TREE_CODE_CLASS (code))
15560 : {
15561 0 : case tcc_binary:
15562 0 : case tcc_comparison:
15563 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15564 0 : TREE_OPERAND (t, 1), depth);
15565 :
15566 0 : case tcc_unary:
15567 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15568 :
15569 8781 : case tcc_constant:
15570 8781 : case tcc_declaration:
15571 8781 : case tcc_reference:
15572 8781 : return integer_valued_real_single_p (t, depth);
15573 :
15574 89819 : default:
15575 89819 : break;
15576 : }
15577 :
15578 89819 : switch (code)
15579 : {
15580 89819 : case COND_EXPR:
15581 89819 : case SSA_NAME:
15582 89819 : return integer_valued_real_single_p (t, depth);
15583 :
15584 0 : case CALL_EXPR:
15585 0 : {
15586 0 : tree arg0 = (call_expr_nargs (t) > 0
15587 0 : ? CALL_EXPR_ARG (t, 0)
15588 0 : : NULL_TREE);
15589 0 : tree arg1 = (call_expr_nargs (t) > 1
15590 0 : ? CALL_EXPR_ARG (t, 1)
15591 0 : : NULL_TREE);
15592 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15593 0 : arg0, arg1, depth);
15594 : }
15595 :
15596 0 : default:
15597 0 : return integer_valued_real_invalid_p (t, depth);
15598 : }
15599 : }
15600 :
15601 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15602 : attempt to fold the expression to a constant without modifying TYPE,
15603 : OP0 or OP1.
15604 :
15605 : If the expression could be simplified to a constant, then return
15606 : the constant. If the expression would not be simplified to a
15607 : constant, then return NULL_TREE. */
15608 :
15609 : tree
15610 16327908 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15611 : {
15612 16327908 : tree tem = fold_binary (code, type, op0, op1);
15613 16327908 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15614 : }
15615 :
15616 : /* Given the components of a unary expression CODE, TYPE and OP0,
15617 : attempt to fold the expression to a constant without modifying
15618 : TYPE or OP0.
15619 :
15620 : If the expression could be simplified to a constant, then return
15621 : the constant. If the expression would not be simplified to a
15622 : constant, then return NULL_TREE. */
15623 :
15624 : tree
15625 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15626 : {
15627 0 : tree tem = fold_unary (code, type, op0);
15628 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15629 : }
15630 :
15631 : /* If EXP represents referencing an element in a constant string
15632 : (either via pointer arithmetic or array indexing), return the
15633 : tree representing the value accessed, otherwise return NULL. */
15634 :
15635 : tree
15636 219191855 : fold_read_from_constant_string (tree exp)
15637 : {
15638 219191855 : if ((INDIRECT_REF_P (exp)
15639 219191836 : || TREE_CODE (exp) == ARRAY_REF)
15640 232982703 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15641 : {
15642 10523772 : tree exp1 = TREE_OPERAND (exp, 0);
15643 10523772 : tree index;
15644 10523772 : tree string;
15645 10523772 : location_t loc = EXPR_LOCATION (exp);
15646 :
15647 10523772 : if (INDIRECT_REF_P (exp))
15648 0 : string = string_constant (exp1, &index, NULL, NULL);
15649 : else
15650 : {
15651 10523772 : tree low_bound = array_ref_low_bound (exp);
15652 10523772 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15653 :
15654 : /* Optimize the special-case of a zero lower bound.
15655 :
15656 : We convert the low_bound to sizetype to avoid some problems
15657 : with constant folding. (E.g. suppose the lower bound is 1,
15658 : and its mode is QI. Without the conversion,l (ARRAY
15659 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15660 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15661 10523772 : if (! integer_zerop (low_bound))
15662 166222 : index = size_diffop_loc (loc, index,
15663 : fold_convert_loc (loc, sizetype, low_bound));
15664 :
15665 : string = exp1;
15666 : }
15667 :
15668 10523772 : scalar_int_mode char_mode;
15669 10523772 : if (string
15670 10523772 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15671 10523772 : && TREE_CODE (string) == STRING_CST
15672 264239 : && tree_fits_uhwi_p (index)
15673 260327 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15674 10783937 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15675 : &char_mode)
15676 21047544 : && GET_MODE_SIZE (char_mode) == 1)
15677 517782 : return build_int_cst_type (TREE_TYPE (exp),
15678 258891 : (TREE_STRING_POINTER (string)
15679 258891 : [TREE_INT_CST_LOW (index)]));
15680 : }
15681 : return NULL;
15682 : }
15683 :
15684 : /* Folds a read from vector element at IDX of vector ARG. */
15685 :
15686 : tree
15687 9654 : fold_read_from_vector (tree arg, poly_uint64 idx)
15688 : {
15689 9654 : unsigned HOST_WIDE_INT i;
15690 9654 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15691 9654 : && known_ge (idx, 0u)
15692 9654 : && idx.is_constant (&i))
15693 : {
15694 9654 : if (TREE_CODE (arg) == VECTOR_CST)
15695 2008 : return VECTOR_CST_ELT (arg, i);
15696 7646 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15697 : {
15698 2548 : if (CONSTRUCTOR_NELTS (arg)
15699 2508 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15700 : return NULL_TREE;
15701 1710 : if (i >= CONSTRUCTOR_NELTS (arg))
15702 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15703 1670 : return CONSTRUCTOR_ELT (arg, i)->value;
15704 : }
15705 : }
15706 : return NULL_TREE;
15707 : }
15708 :
15709 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15710 : an integer constant, real, or fixed-point constant.
15711 :
15712 : TYPE is the type of the result. */
15713 :
15714 : static tree
15715 32320611 : fold_negate_const (tree arg0, tree type)
15716 : {
15717 32320611 : tree t = NULL_TREE;
15718 :
15719 32320611 : switch (TREE_CODE (arg0))
15720 : {
15721 2060768 : case REAL_CST:
15722 2060768 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15723 2060768 : break;
15724 :
15725 0 : case FIXED_CST:
15726 0 : {
15727 0 : FIXED_VALUE_TYPE f;
15728 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15729 0 : &(TREE_FIXED_CST (arg0)), NULL,
15730 0 : TYPE_SATURATING (type));
15731 0 : t = build_fixed (type, f);
15732 : /* Propagate overflow flags. */
15733 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15734 0 : TREE_OVERFLOW (t) = 1;
15735 0 : break;
15736 : }
15737 :
15738 30259843 : default:
15739 30259843 : if (poly_int_tree_p (arg0))
15740 : {
15741 30259843 : wi::overflow_type overflow;
15742 30259843 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15743 30259843 : t = force_fit_type (type, res, 1,
15744 30259843 : (overflow && ! TYPE_UNSIGNED (type))
15745 30249849 : || TREE_OVERFLOW (arg0));
15746 30259843 : break;
15747 30259843 : }
15748 :
15749 0 : gcc_unreachable ();
15750 : }
15751 :
15752 32320611 : return t;
15753 : }
15754 :
15755 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15756 : an integer constant or real constant.
15757 :
15758 : TYPE is the type of the result. */
15759 :
15760 : tree
15761 36780 : fold_abs_const (tree arg0, tree type)
15762 : {
15763 36780 : tree t = NULL_TREE;
15764 :
15765 36780 : switch (TREE_CODE (arg0))
15766 : {
15767 6664 : case INTEGER_CST:
15768 6664 : {
15769 : /* If the value is unsigned or non-negative, then the absolute value
15770 : is the same as the ordinary value. */
15771 6664 : wide_int val = wi::to_wide (arg0);
15772 6664 : wi::overflow_type overflow = wi::OVF_NONE;
15773 6664 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15774 : ;
15775 :
15776 : /* If the value is negative, then the absolute value is
15777 : its negation. */
15778 : else
15779 3063 : val = wi::neg (val, &overflow);
15780 :
15781 : /* Force to the destination type, set TREE_OVERFLOW for signed
15782 : TYPE only. */
15783 6664 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15784 6664 : }
15785 6664 : break;
15786 :
15787 30116 : case REAL_CST:
15788 30116 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15789 7686 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15790 : else
15791 : t = arg0;
15792 : break;
15793 :
15794 0 : default:
15795 0 : gcc_unreachable ();
15796 : }
15797 :
15798 36780 : return t;
15799 : }
15800 :
15801 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15802 : constant. TYPE is the type of the result. */
15803 :
15804 : static tree
15805 2409823 : fold_not_const (const_tree arg0, tree type)
15806 : {
15807 2409823 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15808 :
15809 2409823 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15810 : }
15811 :
15812 : /* Given CODE, a relational operator, the target type, TYPE and two
15813 : constant operands OP0 and OP1, return the result of the
15814 : relational operation. If the result is not a compile time
15815 : constant, then return NULL_TREE. */
15816 :
15817 : static tree
15818 84462479 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15819 : {
15820 84462479 : int result, invert;
15821 :
15822 : /* From here on, the only cases we handle are when the result is
15823 : known to be a constant. */
15824 :
15825 84462479 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15826 : {
15827 1230314 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15828 1230314 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15829 :
15830 : /* Handle the cases where either operand is a NaN. */
15831 1230314 : if (real_isnan (c0) || real_isnan (c1))
15832 : {
15833 14009 : switch (code)
15834 : {
15835 : case EQ_EXPR:
15836 : case ORDERED_EXPR:
15837 : result = 0;
15838 : break;
15839 :
15840 : case NE_EXPR:
15841 : case UNORDERED_EXPR:
15842 : case UNLT_EXPR:
15843 : case UNLE_EXPR:
15844 : case UNGT_EXPR:
15845 : case UNGE_EXPR:
15846 : case UNEQ_EXPR:
15847 6812 : result = 1;
15848 : break;
15849 :
15850 7245 : case LT_EXPR:
15851 7245 : case LE_EXPR:
15852 7245 : case GT_EXPR:
15853 7245 : case GE_EXPR:
15854 7245 : case LTGT_EXPR:
15855 7245 : if (flag_trapping_math)
15856 : return NULL_TREE;
15857 : result = 0;
15858 : break;
15859 :
15860 0 : default:
15861 0 : gcc_unreachable ();
15862 : }
15863 :
15864 6812 : return constant_boolean_node (result, type);
15865 : }
15866 :
15867 1216305 : return constant_boolean_node (real_compare (code, c0, c1), type);
15868 : }
15869 :
15870 83232165 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15871 : {
15872 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15873 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15874 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15875 : }
15876 :
15877 : /* Handle equality/inequality of complex constants. */
15878 83232165 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15879 : {
15880 58894 : tree rcond = fold_relational_const (code, type,
15881 29447 : TREE_REALPART (op0),
15882 29447 : TREE_REALPART (op1));
15883 117788 : tree icond = fold_relational_const (code, type,
15884 29447 : TREE_IMAGPART (op0),
15885 29447 : TREE_IMAGPART (op1));
15886 29447 : if (code == EQ_EXPR)
15887 371 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15888 29076 : else if (code == NE_EXPR)
15889 29076 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15890 : else
15891 : return NULL_TREE;
15892 : }
15893 :
15894 83202718 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15895 : {
15896 22789 : if (!VECTOR_TYPE_P (type))
15897 : {
15898 : /* Have vector comparison with scalar boolean result. */
15899 156 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15900 : && known_eq (VECTOR_CST_NELTS (op0),
15901 : VECTOR_CST_NELTS (op1)));
15902 156 : unsigned HOST_WIDE_INT nunits;
15903 156 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15904 : return NULL_TREE;
15905 445 : for (unsigned i = 0; i < nunits; i++)
15906 : {
15907 388 : tree elem0 = VECTOR_CST_ELT (op0, i);
15908 388 : tree elem1 = VECTOR_CST_ELT (op1, i);
15909 388 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15910 388 : if (tmp == NULL_TREE)
15911 : return NULL_TREE;
15912 388 : if (integer_zerop (tmp))
15913 99 : return constant_boolean_node (code == NE_EXPR, type);
15914 : }
15915 57 : return constant_boolean_node (code == EQ_EXPR, type);
15916 : }
15917 22633 : tree_vector_builder elts;
15918 22633 : if (!elts.new_binary_operation (type, op0, op1, false))
15919 : return NULL_TREE;
15920 22633 : unsigned int count = elts.encoded_nelts ();
15921 79158 : for (unsigned i = 0; i < count; i++)
15922 : {
15923 56525 : tree elem_type = TREE_TYPE (type);
15924 56525 : tree elem0 = VECTOR_CST_ELT (op0, i);
15925 56525 : tree elem1 = VECTOR_CST_ELT (op1, i);
15926 :
15927 56525 : tree tem = fold_relational_const (code, elem_type,
15928 : elem0, elem1);
15929 :
15930 56525 : if (tem == NULL_TREE)
15931 : return NULL_TREE;
15932 :
15933 56525 : elts.quick_push (build_int_cst (elem_type,
15934 91024 : integer_zerop (tem) ? 0 : -1));
15935 : }
15936 :
15937 22633 : return elts.build ();
15938 22633 : }
15939 :
15940 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15941 :
15942 : To compute GT, swap the arguments and do LT.
15943 : To compute GE, do LT and invert the result.
15944 : To compute LE, swap the arguments, do LT and invert the result.
15945 : To compute NE, do EQ and invert the result.
15946 :
15947 : Therefore, the code below must handle only EQ and LT. */
15948 :
15949 83179929 : if (code == LE_EXPR || code == GT_EXPR)
15950 : {
15951 15033852 : std::swap (op0, op1);
15952 15033852 : code = swap_tree_comparison (code);
15953 : }
15954 :
15955 : /* Note that it is safe to invert for real values here because we
15956 : have already handled the one case that it matters. */
15957 :
15958 83179929 : invert = 0;
15959 83179929 : if (code == NE_EXPR || code == GE_EXPR)
15960 : {
15961 37944279 : invert = 1;
15962 37944279 : code = invert_tree_comparison (code, false);
15963 : }
15964 :
15965 : /* Compute a result for LT or EQ if args permit;
15966 : Otherwise return T. */
15967 83179929 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15968 : {
15969 83154615 : if (code == EQ_EXPR)
15970 40849588 : result = tree_int_cst_equal (op0, op1);
15971 : else
15972 42305027 : result = tree_int_cst_lt (op0, op1);
15973 : }
15974 : else
15975 : return NULL_TREE;
15976 :
15977 83154615 : if (invert)
15978 37942261 : result ^= 1;
15979 83154615 : return constant_boolean_node (result, type);
15980 : }
15981 :
15982 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15983 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15984 : itself. */
15985 :
15986 : tree
15987 136725952 : fold_build_cleanup_point_expr (tree type, tree expr)
15988 : {
15989 : /* If the expression does not have side effects then we don't have to wrap
15990 : it with a cleanup point expression. */
15991 136725952 : if (!TREE_SIDE_EFFECTS (expr))
15992 : return expr;
15993 :
15994 : /* If the expression is a return, check to see if the expression inside the
15995 : return has no side effects or the right hand side of the modify expression
15996 : inside the return. If either don't have side effects set we don't need to
15997 : wrap the expression in a cleanup point expression. Note we don't check the
15998 : left hand side of the modify because it should always be a return decl. */
15999 116753716 : if (TREE_CODE (expr) == RETURN_EXPR)
16000 : {
16001 46008414 : tree op = TREE_OPERAND (expr, 0);
16002 46008414 : if (!op || !TREE_SIDE_EFFECTS (op))
16003 : return expr;
16004 45301617 : op = TREE_OPERAND (op, 1);
16005 45301617 : if (!TREE_SIDE_EFFECTS (op))
16006 : return expr;
16007 : }
16008 :
16009 92389851 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16010 : }
16011 :
16012 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
16013 : of an indirection through OP0, or NULL_TREE if no simplification is
16014 : possible. */
16015 :
16016 : tree
16017 22740516 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16018 : {
16019 22740516 : tree sub = op0;
16020 22740516 : tree subtype;
16021 22740516 : poly_uint64 const_op01;
16022 :
16023 22740516 : STRIP_NOPS (sub);
16024 22740516 : subtype = TREE_TYPE (sub);
16025 22740516 : if (!POINTER_TYPE_P (subtype)
16026 22740516 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16027 : return NULL_TREE;
16028 :
16029 22568114 : if (TREE_CODE (sub) == ADDR_EXPR)
16030 : {
16031 5017589 : tree op = TREE_OPERAND (sub, 0);
16032 5017589 : tree optype = TREE_TYPE (op);
16033 :
16034 : /* *&CONST_DECL -> to the value of the const decl. */
16035 5017589 : if (TREE_CODE (op) == CONST_DECL)
16036 3726 : return DECL_INITIAL (op);
16037 : /* *&p => p; make sure to handle *&"str"[cst] here. */
16038 5013863 : if (type == optype)
16039 : {
16040 3800382 : tree fop = fold_read_from_constant_string (op);
16041 3800382 : if (fop)
16042 : return fop;
16043 : else
16044 3753736 : return op;
16045 : }
16046 : /* *(foo *)&fooarray => fooarray[0] */
16047 1213481 : else if (TREE_CODE (optype) == ARRAY_TYPE
16048 14463 : && type == TREE_TYPE (optype)
16049 1226731 : && (!in_gimple_form
16050 3147 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16051 : {
16052 13250 : tree type_domain = TYPE_DOMAIN (optype);
16053 13250 : tree min_val = size_zero_node;
16054 13250 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16055 13211 : min_val = TYPE_MIN_VALUE (type_domain);
16056 13250 : if (in_gimple_form
16057 3147 : && TREE_CODE (min_val) != INTEGER_CST)
16058 : return NULL_TREE;
16059 13250 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16060 13250 : NULL_TREE, NULL_TREE);
16061 : }
16062 : /* *(foo *)&complexfoo => __real__ complexfoo */
16063 1200231 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16064 1200231 : && type == TREE_TYPE (optype))
16065 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16066 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16067 1200231 : else if (VECTOR_TYPE_P (optype)
16068 1200231 : && type == TREE_TYPE (optype))
16069 : {
16070 70 : tree part_width = TYPE_SIZE (type);
16071 70 : tree index = bitsize_int (0);
16072 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16073 70 : index);
16074 : }
16075 : }
16076 :
16077 18750686 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16078 18750686 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16079 : {
16080 260500 : tree op00 = TREE_OPERAND (sub, 0);
16081 260500 : tree op01 = TREE_OPERAND (sub, 1);
16082 :
16083 260500 : STRIP_NOPS (op00);
16084 260500 : if (TREE_CODE (op00) == ADDR_EXPR)
16085 : {
16086 1763 : tree op00type;
16087 1763 : op00 = TREE_OPERAND (op00, 0);
16088 1763 : op00type = TREE_TYPE (op00);
16089 :
16090 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16091 1763 : if (VECTOR_TYPE_P (op00type)
16092 240 : && type == TREE_TYPE (op00type)
16093 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16094 : but we want to treat offsets with MSB set as negative.
16095 : For the code below negative offsets are invalid and
16096 : TYPE_SIZE of the element is something unsigned, so
16097 : check whether op01 fits into poly_int64, which implies
16098 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16099 : then just use poly_uint64 because we want to treat the
16100 : value as unsigned. */
16101 1956 : && tree_fits_poly_int64_p (op01))
16102 : {
16103 179 : tree part_width = TYPE_SIZE (type);
16104 179 : poly_uint64 max_offset
16105 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16106 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16107 179 : if (known_lt (const_op01, max_offset))
16108 : {
16109 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16110 179 : return fold_build3_loc (loc,
16111 : BIT_FIELD_REF, type, op00,
16112 179 : part_width, index);
16113 : }
16114 : }
16115 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16116 1584 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16117 1584 : && type == TREE_TYPE (op00type))
16118 : {
16119 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16120 : const_op01))
16121 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16122 : }
16123 : /* ((foo *)&fooarray)[1] => fooarray[1] */
16124 1584 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16125 1584 : && type == TREE_TYPE (op00type))
16126 : {
16127 719 : tree type_domain = TYPE_DOMAIN (op00type);
16128 719 : tree min_val = size_zero_node;
16129 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16130 718 : min_val = TYPE_MIN_VALUE (type_domain);
16131 719 : poly_uint64 type_size, index;
16132 719 : if (poly_int_tree_p (min_val)
16133 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16134 719 : && multiple_p (const_op01, type_size, &index))
16135 : {
16136 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16137 719 : op01 = wide_int_to_tree (sizetype, off);
16138 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16139 : NULL_TREE, NULL_TREE);
16140 : }
16141 : }
16142 : }
16143 : }
16144 :
16145 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16146 18749788 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16147 709979 : && type == TREE_TYPE (TREE_TYPE (subtype))
16148 18753234 : && (!in_gimple_form
16149 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16150 : {
16151 3445 : tree type_domain;
16152 3445 : tree min_val = size_zero_node;
16153 3445 : sub = build_fold_indirect_ref_loc (loc, sub);
16154 3445 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16155 3445 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16156 3445 : min_val = TYPE_MIN_VALUE (type_domain);
16157 3445 : if (in_gimple_form
16158 11 : && TREE_CODE (min_val) != INTEGER_CST)
16159 : return NULL_TREE;
16160 3445 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16161 3445 : NULL_TREE);
16162 : }
16163 :
16164 : return NULL_TREE;
16165 : }
16166 :
16167 : /* Builds an expression for an indirection through T, simplifying some
16168 : cases. */
16169 :
16170 : tree
16171 11853227 : build_fold_indirect_ref_loc (location_t loc, tree t)
16172 : {
16173 11853227 : tree type = TREE_TYPE (TREE_TYPE (t));
16174 11853227 : tree sub = fold_indirect_ref_1 (loc, type, t);
16175 :
16176 11853227 : if (sub)
16177 : return sub;
16178 :
16179 8055920 : return build1_loc (loc, INDIRECT_REF, type, t);
16180 : }
16181 :
16182 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16183 :
16184 : tree
16185 10505667 : fold_indirect_ref_loc (location_t loc, tree t)
16186 : {
16187 10505667 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16188 :
16189 10505667 : if (sub)
16190 : return sub;
16191 : else
16192 10483851 : return t;
16193 : }
16194 :
16195 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16196 : whose result is ignored. The type of the returned tree need not be
16197 : the same as the original expression. */
16198 :
16199 : tree
16200 119381 : fold_ignored_result (tree t)
16201 : {
16202 119381 : if (!TREE_SIDE_EFFECTS (t))
16203 302 : return integer_zero_node;
16204 :
16205 158297 : for (;;)
16206 158297 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16207 : {
16208 3863 : case tcc_unary:
16209 3863 : t = TREE_OPERAND (t, 0);
16210 3863 : break;
16211 :
16212 5114 : case tcc_binary:
16213 5114 : case tcc_comparison:
16214 5114 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16215 3224 : t = TREE_OPERAND (t, 0);
16216 1890 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16217 17 : t = TREE_OPERAND (t, 1);
16218 : else
16219 : return t;
16220 : break;
16221 :
16222 100445 : case tcc_expression:
16223 100445 : switch (TREE_CODE (t))
16224 : {
16225 32115 : case COMPOUND_EXPR:
16226 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16227 : return t;
16228 31820 : t = TREE_OPERAND (t, 0);
16229 31820 : break;
16230 :
16231 382 : case COND_EXPR:
16232 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16233 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16234 : return t;
16235 294 : t = TREE_OPERAND (t, 0);
16236 294 : break;
16237 :
16238 : default:
16239 : return t;
16240 : }
16241 : break;
16242 :
16243 : default:
16244 : return t;
16245 : }
16246 : }
16247 :
16248 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16249 :
16250 : tree
16251 3164149262 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16252 : {
16253 3164149262 : tree div = NULL_TREE;
16254 :
16255 3164149262 : if (divisor == 1)
16256 : return value;
16257 :
16258 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16259 : have to do anything. Only do this when we are not given a const,
16260 : because in that case, this check is more expensive than just
16261 : doing it. */
16262 1965384619 : if (TREE_CODE (value) != INTEGER_CST)
16263 : {
16264 391132 : div = build_int_cst (TREE_TYPE (value), divisor);
16265 :
16266 391132 : if (multiple_of_p (TREE_TYPE (value), value, div))
16267 : return value;
16268 : }
16269 :
16270 : /* If divisor is a power of two, simplify this to bit manipulation. */
16271 1964995399 : if (pow2_or_zerop (divisor))
16272 : {
16273 1964995399 : if (TREE_CODE (value) == INTEGER_CST)
16274 : {
16275 1964993487 : wide_int val = wi::to_wide (value);
16276 1964993487 : bool overflow_p;
16277 :
16278 1964993487 : if ((val & (divisor - 1)) == 0)
16279 : return value;
16280 :
16281 4067724 : overflow_p = TREE_OVERFLOW (value);
16282 4067724 : val += divisor - 1;
16283 4067724 : val &= (int) -divisor;
16284 4067724 : if (val == 0)
16285 4 : overflow_p = true;
16286 :
16287 4067724 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16288 1964993487 : }
16289 : else
16290 : {
16291 1912 : tree t;
16292 :
16293 1912 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16294 1912 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16295 1912 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16296 1912 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16297 : }
16298 : }
16299 : else
16300 : {
16301 0 : if (!div)
16302 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16303 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16304 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16305 : }
16306 :
16307 : return value;
16308 : }
16309 :
16310 : /* Likewise, but round down. */
16311 :
16312 : tree
16313 21368656 : round_down_loc (location_t loc, tree value, int divisor)
16314 : {
16315 21368656 : tree div = NULL_TREE;
16316 :
16317 21368656 : gcc_assert (divisor > 0);
16318 21368656 : if (divisor == 1)
16319 : return value;
16320 :
16321 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16322 : have to do anything. Only do this when we are not given a const,
16323 : because in that case, this check is more expensive than just
16324 : doing it. */
16325 21368656 : if (TREE_CODE (value) != INTEGER_CST)
16326 : {
16327 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16328 :
16329 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16330 : return value;
16331 : }
16332 :
16333 : /* If divisor is a power of two, simplify this to bit manipulation. */
16334 21368656 : if (pow2_or_zerop (divisor))
16335 : {
16336 21368656 : tree t;
16337 :
16338 21368656 : t = build_int_cst (TREE_TYPE (value), -divisor);
16339 21368656 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16340 : }
16341 : else
16342 : {
16343 0 : if (!div)
16344 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16345 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16346 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16347 : }
16348 :
16349 : return value;
16350 : }
16351 :
16352 : /* Returns the pointer to the base of the object addressed by EXP and
16353 : extracts the information about the offset of the access, storing it
16354 : to PBITPOS and POFFSET. */
16355 :
16356 : static tree
16357 25214758 : split_address_to_core_and_offset (tree exp,
16358 : poly_int64 *pbitpos, tree *poffset)
16359 : {
16360 25214758 : tree core;
16361 25214758 : machine_mode mode;
16362 25214758 : int unsignedp, reversep, volatilep;
16363 25214758 : poly_int64 bitsize;
16364 25214758 : location_t loc = EXPR_LOCATION (exp);
16365 :
16366 25214758 : STRIP_NOPS (exp);
16367 :
16368 25214758 : if (TREE_CODE (exp) == SSA_NAME)
16369 15787892 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16370 19666897 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16371 58860 : exp = gimple_assign_rhs1 (def);
16372 :
16373 25214758 : if (TREE_CODE (exp) == ADDR_EXPR)
16374 : {
16375 2027843 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16376 : poffset, &mode, &unsignedp, &reversep,
16377 : &volatilep);
16378 : /* If we are left with MEM[a + CST] strip that and add it to the
16379 : pbitpos and return a. */
16380 2027843 : if (TREE_CODE (core) == MEM_REF)
16381 : {
16382 57599 : poly_offset_int tem;
16383 57599 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16384 57599 : tem <<= LOG2_BITS_PER_UNIT;
16385 57599 : tem += *pbitpos;
16386 57599 : if (tem.to_shwi (pbitpos))
16387 57411 : return TREE_OPERAND (core, 0);
16388 : }
16389 1970432 : core = build_fold_addr_expr_loc (loc, core);
16390 : }
16391 23186915 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16392 : {
16393 576991 : core = TREE_OPERAND (exp, 0);
16394 576991 : STRIP_NOPS (core);
16395 576991 : *pbitpos = 0;
16396 576991 : *poffset = TREE_OPERAND (exp, 1);
16397 576991 : if (poly_int_tree_p (*poffset))
16398 : {
16399 557953 : poly_offset_int tem
16400 557953 : = wi::sext (wi::to_poly_offset (*poffset),
16401 557953 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16402 557953 : tem <<= LOG2_BITS_PER_UNIT;
16403 557953 : if (tem.to_shwi (pbitpos))
16404 557953 : *poffset = NULL_TREE;
16405 : }
16406 : }
16407 : else
16408 : {
16409 22609924 : core = exp;
16410 22609924 : *pbitpos = 0;
16411 22609924 : *poffset = NULL_TREE;
16412 : }
16413 :
16414 : return core;
16415 : }
16416 :
16417 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16418 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16419 :
16420 : bool
16421 12607379 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16422 : {
16423 12607379 : tree core1, core2;
16424 12607379 : poly_int64 bitpos1, bitpos2;
16425 12607379 : tree toffset1, toffset2, tdiff, type;
16426 :
16427 12607379 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16428 12607379 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16429 :
16430 12607379 : poly_int64 bytepos1, bytepos2;
16431 12607379 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16432 25214758 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16433 25214758 : || !operand_equal_p (core1, core2, 0))
16434 : return false;
16435 :
16436 690616 : if (toffset1 && toffset2)
16437 : {
16438 149 : type = TREE_TYPE (toffset1);
16439 149 : if (type != TREE_TYPE (toffset2))
16440 0 : toffset2 = fold_convert (type, toffset2);
16441 :
16442 149 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16443 149 : if (!cst_and_fits_in_hwi (tdiff))
16444 : return false;
16445 :
16446 53 : *diff = int_cst_value (tdiff);
16447 : }
16448 690467 : else if (toffset1 || toffset2)
16449 : {
16450 : /* If only one of the offsets is non-constant, the difference cannot
16451 : be a constant. */
16452 : return false;
16453 : }
16454 : else
16455 671813 : *diff = 0;
16456 :
16457 671866 : *diff += bytepos1 - bytepos2;
16458 671866 : return true;
16459 : }
16460 :
16461 : /* Return OFF converted to a pointer offset type suitable as offset for
16462 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16463 : tree
16464 53648774 : convert_to_ptrofftype_loc (location_t loc, tree off)
16465 : {
16466 53648774 : if (ptrofftype_p (TREE_TYPE (off)))
16467 : return off;
16468 6234264 : return fold_convert_loc (loc, sizetype, off);
16469 : }
16470 :
16471 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16472 : tree
16473 47500585 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16474 : {
16475 47500585 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16476 47500585 : ptr, convert_to_ptrofftype_loc (loc, off));
16477 : }
16478 :
16479 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16480 : tree
16481 168281 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16482 : {
16483 168281 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16484 168281 : ptr, size_int (off));
16485 : }
16486 :
16487 : /* Return a pointer to a NUL-terminated string containing the sequence
16488 : of bytes corresponding to the representation of the object referred to
16489 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16490 : to an initialized constant array plus some constant offset).
16491 : Set *STRSIZE the number of bytes in the constant sequence including
16492 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16493 : where A is the array that stores the constant sequence that SRC points
16494 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16495 : need not point to a string or even an array of characters but may point
16496 : to an object of any type. */
16497 :
16498 : const char *
16499 12720490 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16500 : {
16501 : /* The offset into the array A storing the string, and A's byte size. */
16502 12720490 : tree offset_node;
16503 12720490 : tree mem_size;
16504 :
16505 12720490 : if (strsize)
16506 4680074 : *strsize = 0;
16507 :
16508 12720490 : if (strsize)
16509 4680074 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16510 : else
16511 8040416 : src = string_constant (src, &offset_node, &mem_size, NULL);
16512 12720490 : if (!src)
16513 : return NULL;
16514 :
16515 2846236 : unsigned HOST_WIDE_INT offset = 0;
16516 2846236 : if (offset_node != NULL_TREE)
16517 : {
16518 2846236 : if (!tree_fits_uhwi_p (offset_node))
16519 : return NULL;
16520 : else
16521 2843086 : offset = tree_to_uhwi (offset_node);
16522 : }
16523 :
16524 2843086 : if (!tree_fits_uhwi_p (mem_size))
16525 : return NULL;
16526 :
16527 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16528 : is stored in and equal to sizeof A. INIT_BYTES is the number
16529 : of bytes in the constant sequence used to initialize the array,
16530 : including any embedded NULs as well as the terminating NUL (for
16531 : strings), but not including any trailing zeros/NULs past
16532 : the terminating one appended implicitly to a string literal to
16533 : zero out the remainder of the array it's stored in. For example,
16534 : given:
16535 : const char a[7] = "abc\0d";
16536 : n = strlen (a + 1);
16537 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16538 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16539 : is equal to strlen (A) + 1. */
16540 2843086 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16541 2843086 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16542 2843086 : const char *string = TREE_STRING_POINTER (src);
16543 :
16544 : /* Ideally this would turn into a gcc_checking_assert over time. */
16545 2843086 : if (init_bytes > array_size)
16546 : init_bytes = array_size;
16547 :
16548 2843086 : if (init_bytes == 0 || offset >= array_size)
16549 : return NULL;
16550 :
16551 2841819 : if (strsize)
16552 : {
16553 : /* Compute and store the number of characters from the beginning
16554 : of the substring at OFFSET to the end, including the terminating
16555 : nul. Offsets past the initial length refer to null strings. */
16556 1442725 : if (offset < init_bytes)
16557 1442725 : *strsize = init_bytes - offset;
16558 : else
16559 0 : *strsize = 1;
16560 : }
16561 : else
16562 : {
16563 1399094 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16564 : /* Support only properly NUL-terminated single byte strings. */
16565 1399094 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16566 : return NULL;
16567 1394223 : if (string[init_bytes - 1] != '\0')
16568 : return NULL;
16569 : }
16570 :
16571 2810730 : return offset < init_bytes ? string + offset : "";
16572 : }
16573 :
16574 : /* Return a pointer to a NUL-terminated string corresponding to
16575 : the expression STR referencing a constant string, possibly
16576 : involving a constant offset. Return null if STR either doesn't
16577 : reference a constant string or if it involves a nonconstant
16578 : offset. */
16579 :
16580 : const char *
16581 8040416 : c_getstr (tree str)
16582 : {
16583 8040416 : return getbyterep (str, NULL);
16584 : }
16585 :
16586 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16587 : may be nonzero, with precision PREC, the precision of T's type. */
16588 :
16589 : static wide_int
16590 251385865 : tree_nonzero_bits (const_tree t, unsigned prec)
16591 : {
16592 251385865 : switch (TREE_CODE (t))
16593 : {
16594 8913434 : case INTEGER_CST:
16595 8913434 : return wi::to_wide (t);
16596 140858547 : case SSA_NAME:
16597 140858547 : return get_nonzero_bits (t);
16598 294546 : case NON_LVALUE_EXPR:
16599 294546 : case SAVE_EXPR:
16600 294546 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16601 541399 : case BIT_AND_EXPR:
16602 1082798 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16603 1624197 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16604 33833 : case BIT_IOR_EXPR:
16605 33833 : case BIT_XOR_EXPR:
16606 67666 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16607 101499 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16608 75149 : case COND_EXPR:
16609 150298 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16610 225447 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16611 49393923 : CASE_CONVERT:
16612 49393923 : if (TREE_TYPE (t) != error_mark_node
16613 49393923 : && !error_operand_p (TREE_OPERAND (t, 0)))
16614 : {
16615 49393922 : tree op0 = TREE_OPERAND (t, 0);
16616 49393922 : tree inner_type = TREE_TYPE (op0);
16617 49393922 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16618 98787844 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16619 98787844 : prec, TYPE_SIGN (inner_type));
16620 : }
16621 : break;
16622 14304333 : case PLUS_EXPR:
16623 14304333 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16624 : {
16625 14304333 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16626 14304333 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16627 14304334 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16628 543312 : return wi::bit_or (nzbits1, nzbits2);
16629 14304334 : }
16630 : break;
16631 170893 : case LSHIFT_EXPR:
16632 170893 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16633 170893 : && TREE_TYPE (t) != error_mark_node)
16634 : {
16635 98712 : tree type = TREE_TYPE (t);
16636 98712 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16637 98712 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16638 98712 : return wi::neg_p (arg1)
16639 197424 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16640 98712 : : wi::lshift (nzbits, arg1);
16641 98712 : }
16642 : break;
16643 166328 : case RSHIFT_EXPR:
16644 166328 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16645 166328 : && TREE_TYPE (t) != error_mark_node)
16646 : {
16647 164380 : tree type = TREE_TYPE (t);
16648 164380 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16649 164380 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16650 164380 : return wi::neg_p (arg1)
16651 328760 : ? wi::lshift (nzbits, -arg1)
16652 164380 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16653 164380 : }
16654 : break;
16655 : default:
16656 : break;
16657 : }
16658 :
16659 50468631 : return wi::shwi (-1, prec);
16660 : }
16661 :
16662 : /* Given a tree T, compute which bits in T may be nonzero. */
16663 :
16664 : wide_int
16665 171524877 : tree_nonzero_bits (const_tree t)
16666 : {
16667 171524877 : if (error_operand_p (t))
16668 0 : return wi::shwi (-1, 64);
16669 171524877 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16670 : }
16671 :
16672 : /* Helper function for address compare simplifications in match.pd.
16673 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16674 : TYPE is the type of comparison operands.
16675 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16676 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16677 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16678 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16679 : and 2 if unknown. */
16680 :
16681 : int
16682 5500716 : address_compare (tree_code code, tree type, tree op0, tree op1,
16683 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16684 : bool generic)
16685 : {
16686 5500716 : if (TREE_CODE (op0) == SSA_NAME)
16687 31976 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16688 5500716 : if (TREE_CODE (op1) == SSA_NAME)
16689 4658 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16690 5500716 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16691 5500716 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16692 5500716 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16693 5500716 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16694 5500716 : if (base0 && TREE_CODE (base0) == MEM_REF)
16695 : {
16696 30004 : off0 += mem_ref_offset (base0).force_shwi ();
16697 30004 : base0 = TREE_OPERAND (base0, 0);
16698 : }
16699 5500716 : if (base1 && TREE_CODE (base1) == MEM_REF)
16700 : {
16701 3865 : off1 += mem_ref_offset (base1).force_shwi ();
16702 3865 : base1 = TREE_OPERAND (base1, 0);
16703 : }
16704 5500716 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16705 : return 2;
16706 :
16707 5487624 : int equal = 2;
16708 : /* Punt in GENERIC on variables with value expressions;
16709 : the value expressions might point to fields/elements
16710 : of other vars etc. */
16711 5487624 : if (generic
16712 5487624 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16713 5353718 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16714 : return 2;
16715 5486967 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16716 : {
16717 1430334 : symtab_node *node0 = symtab_node::get_create (base0);
16718 1430334 : symtab_node *node1 = symtab_node::get_create (base1);
16719 1430334 : equal = node0->equal_address_to (node1);
16720 : }
16721 4056633 : else if ((DECL_P (base0)
16722 240182 : || TREE_CODE (base0) == SSA_NAME
16723 211297 : || TREE_CODE (base0) == STRING_CST)
16724 4056435 : && (DECL_P (base1)
16725 215064 : || TREE_CODE (base1) == SSA_NAME
16726 211435 : || TREE_CODE (base1) == STRING_CST))
16727 4056420 : equal = (base0 == base1);
16728 : /* Assume different STRING_CSTs with the same content will be
16729 : merged. */
16730 5486754 : if (equal == 0
16731 77140 : && TREE_CODE (base0) == STRING_CST
16732 17380 : && TREE_CODE (base1) == STRING_CST
16733 17175 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16734 5486754 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16735 6150 : TREE_STRING_LENGTH (base0)) == 0)
16736 : equal = 1;
16737 5482553 : if (equal == 1)
16738 : {
16739 5388647 : if (code == EQ_EXPR
16740 5388647 : || code == NE_EXPR
16741 : /* If the offsets are equal we can ignore overflow. */
16742 204462 : || known_eq (off0, off1)
16743 300958 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16744 : /* Or if we compare using pointers to decls or strings. */
16745 5539126 : || (POINTER_TYPE_P (type)
16746 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16747 5388647 : return 1;
16748 : return 2;
16749 : }
16750 98320 : if (equal != 0)
16751 : return equal;
16752 72726 : if (code != EQ_EXPR && code != NE_EXPR)
16753 : return 2;
16754 :
16755 : /* At this point we know (or assume) the two pointers point at
16756 : different objects. */
16757 67427 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16758 67427 : off0.is_constant (&ioff0);
16759 67427 : off1.is_constant (&ioff1);
16760 : /* Punt on non-zero offsets from functions. */
16761 67427 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16762 67427 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16763 : return 2;
16764 : /* Or if the bases are neither decls nor string literals. */
16765 67427 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16766 : return 2;
16767 39277 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16768 : return 2;
16769 : /* For initializers, assume addresses of different functions are
16770 : different. */
16771 39277 : if (folding_initializer
16772 13961 : && TREE_CODE (base0) == FUNCTION_DECL
16773 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16774 : return 0;
16775 :
16776 : /* Compute whether one address points to the start of one
16777 : object and another one to the end of another one. */
16778 39257 : poly_int64 size0 = 0, size1 = 0;
16779 39257 : if (TREE_CODE (base0) == STRING_CST)
16780 : {
16781 12788 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16782 : equal = 2;
16783 : else
16784 : size0 = TREE_STRING_LENGTH (base0);
16785 : }
16786 26469 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16787 : size0 = 1;
16788 : else
16789 : {
16790 26149 : tree sz0 = DECL_SIZE_UNIT (base0);
16791 26149 : if (!tree_fits_poly_int64_p (sz0))
16792 : equal = 2;
16793 : else
16794 26149 : size0 = tree_to_poly_int64 (sz0);
16795 : }
16796 39257 : if (TREE_CODE (base1) == STRING_CST)
16797 : {
16798 12887 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16799 : equal = 2;
16800 : else
16801 : size1 = TREE_STRING_LENGTH (base1);
16802 : }
16803 26370 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16804 : size1 = 1;
16805 : else
16806 : {
16807 26054 : tree sz1 = DECL_SIZE_UNIT (base1);
16808 26054 : if (!tree_fits_poly_int64_p (sz1))
16809 : equal = 2;
16810 : else
16811 26054 : size1 = tree_to_poly_int64 (sz1);
16812 : }
16813 39257 : if (equal == 0)
16814 : {
16815 : /* If one offset is pointing (or could be) to the beginning of one
16816 : object and the other is pointing to one past the last byte of the
16817 : other object, punt. */
16818 39245 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16819 : equal = 2;
16820 39052 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16821 : equal = 2;
16822 : /* If both offsets are the same, there are some cases we know that are
16823 : ok. Either if we know they aren't zero, or if we know both sizes
16824 : are no zero. */
16825 : if (equal == 2
16826 343 : && known_eq (off0, off1)
16827 22 : && (known_ne (off0, 0)
16828 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16829 : equal = 0;
16830 : }
16831 :
16832 : /* At this point, equal is 2 if either one or both pointers are out of
16833 : bounds of their object, or one points to start of its object and the
16834 : other points to end of its object. This is unspecified behavior
16835 : e.g. in C++. Otherwise equal is 0. */
16836 39257 : if (folding_cxx_constexpr && equal)
16837 : return equal;
16838 :
16839 : /* When both pointers point to string literals, even when equal is 0,
16840 : due to tail merging of string literals the pointers might be the same. */
16841 39194 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16842 : {
16843 12743 : if (ioff0 < 0
16844 12743 : || ioff1 < 0
16845 12743 : || ioff0 > TREE_STRING_LENGTH (base0)
16846 25474 : || ioff1 > TREE_STRING_LENGTH (base1))
16847 : return 2;
16848 :
16849 : /* If the bytes in the string literals starting at the pointers
16850 : differ, the pointers need to be different. */
16851 12731 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16852 12731 : TREE_STRING_POINTER (base1) + ioff1,
16853 12731 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16854 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16855 : {
16856 3761 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16857 3761 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16858 3761 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16859 : ioffmin) == 0)
16860 : /* If even the bytes in the string literal before the
16861 : pointers are the same, the string literals could be
16862 : tail merged. */
16863 : return 2;
16864 : }
16865 8982 : return 0;
16866 : }
16867 :
16868 26451 : if (folding_cxx_constexpr)
16869 : return 0;
16870 :
16871 : /* If this is a pointer comparison, ignore for now even
16872 : valid equalities where one pointer is the offset zero
16873 : of one object and the other to one past end of another one. */
16874 12614 : if (!INTEGRAL_TYPE_P (type))
16875 : return 0;
16876 :
16877 : /* Assume that string literals can't be adjacent to variables
16878 : (automatic or global). */
16879 387 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16880 : return 0;
16881 :
16882 : /* Assume that automatic variables can't be adjacent to global
16883 : variables. */
16884 361 : if (is_global_var (base0) != is_global_var (base1))
16885 2 : return 0;
16886 :
16887 : return equal;
16888 : }
16889 :
16890 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16891 : tree
16892 86 : ctor_single_nonzero_element (const_tree t)
16893 : {
16894 86 : unsigned HOST_WIDE_INT idx;
16895 86 : constructor_elt *ce;
16896 86 : tree elt = NULL_TREE;
16897 :
16898 86 : if (TREE_CODE (t) != CONSTRUCTOR)
16899 : return NULL_TREE;
16900 181 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16901 178 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16902 : {
16903 169 : if (elt)
16904 : return NULL_TREE;
16905 86 : elt = ce->value;
16906 : }
16907 : return elt;
16908 : }
16909 :
16910 : #if CHECKING_P
16911 :
16912 : namespace selftest {
16913 :
16914 : /* Helper functions for writing tests of folding trees. */
16915 :
16916 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16917 :
16918 : static void
16919 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16920 : tree constant)
16921 : {
16922 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16923 16 : }
16924 :
16925 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16926 : wrapping WRAPPED_EXPR. */
16927 :
16928 : static void
16929 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16930 : tree wrapped_expr)
16931 : {
16932 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16933 12 : ASSERT_NE (wrapped_expr, result);
16934 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16935 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16936 12 : }
16937 :
16938 : /* Verify that various arithmetic binary operations are folded
16939 : correctly. */
16940 :
16941 : static void
16942 4 : test_arithmetic_folding ()
16943 : {
16944 4 : tree type = integer_type_node;
16945 4 : tree x = create_tmp_var_raw (type, "x");
16946 4 : tree zero = build_zero_cst (type);
16947 4 : tree one = build_int_cst (type, 1);
16948 :
16949 : /* Addition. */
16950 : /* 1 <-- (0 + 1) */
16951 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16952 : one);
16953 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16954 : one);
16955 :
16956 : /* (nonlvalue)x <-- (x + 0) */
16957 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16958 : x);
16959 :
16960 : /* Subtraction. */
16961 : /* 0 <-- (x - x) */
16962 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16963 : zero);
16964 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16965 : x);
16966 :
16967 : /* Multiplication. */
16968 : /* 0 <-- (x * 0) */
16969 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16970 : zero);
16971 :
16972 : /* (nonlvalue)x <-- (x * 1) */
16973 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16974 : x);
16975 4 : }
16976 :
16977 : namespace test_operand_equality {
16978 :
16979 : /* Verify structural equality. */
16980 :
16981 : /* Execute fold_vec_perm_cst unit tests. */
16982 :
16983 : static void
16984 4 : test ()
16985 : {
16986 4 : tree stype = integer_type_node;
16987 4 : tree utype = unsigned_type_node;
16988 4 : tree x = create_tmp_var_raw (stype, "x");
16989 4 : tree y = create_tmp_var_raw (stype, "y");
16990 4 : tree z = create_tmp_var_raw (stype, "z");
16991 4 : tree four = build_int_cst (stype, 4);
16992 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16993 4 : tree rhs1 = fold_convert (stype,
16994 : fold_build2 (PLUS_EXPR, utype,
16995 : fold_convert (utype, x),
16996 : fold_convert (utype, y)));
16997 :
16998 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16999 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17000 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17001 :
17002 : /* (int)(unsigned) x == x. */
17003 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17004 : build1 (NOP_EXPR, utype, x));
17005 4 : tree rhs2 = x;
17006 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17007 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17008 :
17009 : /* (unsigned x) + (unsigned y) == x + y. */
17010 4 : tree lhs3 = lhs1;
17011 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17012 : fold_convert (utype, x),
17013 : fold_convert (utype, y));
17014 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17015 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17016 :
17017 : /* (unsigned x) / (unsigned y) == x / y. */
17018 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17019 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17020 : fold_convert (utype, x),
17021 : fold_convert (utype, y));
17022 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17023 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17024 :
17025 : /* (long x) / 4 == (long)(x / 4). */
17026 4 : tree lstype = long_long_integer_type_node;
17027 4 : tree lfour = build_int_cst (lstype, 4);
17028 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17029 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17030 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17031 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17032 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17033 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17034 :
17035 : /* (unsigned x) / 4 == x / 4. */
17036 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17037 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17038 : fold_convert (utype, x),
17039 : fold_convert (utype, four));
17040 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17041 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17042 :
17043 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17044 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17045 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17046 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17047 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17048 :
17049 : /* (unsigned x) + 4 == x + 4. */
17050 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17051 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17052 : fold_convert (utype, x),
17053 : fold_convert (utype, four));
17054 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17055 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17056 :
17057 : /* (unsigned x) + 4 == 4 + x. */
17058 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17059 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17060 : fold_convert (utype, x),
17061 : fold_convert (utype, four));
17062 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17063 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17064 :
17065 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17066 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17067 : fold_build2 (MULT_EXPR, stype,
17068 : fold_build2 (PLUS_EXPR, stype, four, x),
17069 : y),
17070 : z);
17071 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17072 : fold_build2 (PLUS_EXPR, utype,
17073 : fold_convert (utype, x),
17074 : fold_convert (utype, four)),
17075 : fold_convert (utype, y));
17076 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17077 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17078 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17079 4 : }
17080 : }
17081 :
17082 : namespace test_fold_vec_perm_cst {
17083 :
17084 : /* Build a VECTOR_CST corresponding to VMODE, and has
17085 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17086 : Fill it with randomized elements, using rand() % THRESHOLD. */
17087 :
17088 : static tree
17089 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17090 : unsigned nelts_per_pattern,
17091 : int step = 0, bool natural_stepped = false,
17092 : int threshold = 100)
17093 : {
17094 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17095 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17096 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17097 :
17098 : // Fill a0 for each pattern
17099 0 : for (unsigned i = 0; i < npatterns; i++)
17100 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17101 :
17102 0 : if (nelts_per_pattern == 1)
17103 0 : return builder.build ();
17104 :
17105 : // Fill a1 for each pattern
17106 0 : for (unsigned i = 0; i < npatterns; i++)
17107 : {
17108 0 : tree a1;
17109 0 : if (natural_stepped)
17110 : {
17111 0 : tree a0 = builder[i];
17112 0 : wide_int a0_val = wi::to_wide (a0);
17113 0 : wide_int a1_val = a0_val + step;
17114 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17115 0 : }
17116 : else
17117 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17118 0 : builder.quick_push (a1);
17119 : }
17120 0 : if (nelts_per_pattern == 2)
17121 0 : return builder.build ();
17122 :
17123 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17124 : {
17125 0 : tree prev_elem = builder[i - npatterns];
17126 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17127 0 : wide_int val = prev_elem_val + step;
17128 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17129 0 : }
17130 :
17131 0 : return builder.build ();
17132 0 : }
17133 :
17134 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17135 : when result is VLA. */
17136 :
17137 : static void
17138 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17139 : tree res, tree *expected_res)
17140 : {
17141 : /* Actual npatterns and encoded_elts in res may be less than expected due
17142 : to canonicalization. */
17143 0 : ASSERT_TRUE (res != NULL_TREE);
17144 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17145 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17146 :
17147 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17148 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17149 0 : }
17150 :
17151 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17152 : when the result is VLS. */
17153 :
17154 : static void
17155 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17156 : {
17157 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17158 0 : for (unsigned i = 0; i < expected_nelts; i++)
17159 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17160 0 : }
17161 :
17162 : /* Helper routine to push multiple elements into BUILDER. */
17163 : template<unsigned N>
17164 0 : static void builder_push_elems (vec_perm_builder& builder,
17165 : poly_uint64 (&elems)[N])
17166 : {
17167 0 : for (unsigned i = 0; i < N; i++)
17168 0 : builder.quick_push (elems[i]);
17169 0 : }
17170 :
17171 : #define ARG0(index) vector_cst_elt (arg0, index)
17172 : #define ARG1(index) vector_cst_elt (arg1, index)
17173 :
17174 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17175 :
17176 : static void
17177 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17178 : {
17179 0 : for (int i = 0; i < 10; i++)
17180 : {
17181 : /* Case 1:
17182 : sel = { 0, 4, 1, 5, ... }
17183 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17184 0 : {
17185 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17186 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17187 :
17188 0 : tree inner_type
17189 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17190 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17191 :
17192 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17193 0 : vec_perm_builder builder (res_len, 4, 1);
17194 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17195 0 : builder_push_elems (builder, mask_elems);
17196 :
17197 0 : vec_perm_indices sel (builder, 2, 4);
17198 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17199 :
17200 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17201 0 : validate_res (4, 1, res, expected_res);
17202 0 : }
17203 :
17204 : /* Case 2: Same as case 1, but contains an out of bounds access which
17205 : should wrap around.
17206 : sel = {0, 8, 4, 12, ...} (4, 1)
17207 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17208 0 : {
17209 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17210 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17211 :
17212 0 : tree inner_type
17213 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17214 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17215 :
17216 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17217 0 : vec_perm_builder builder (res_len, 4, 1);
17218 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17219 0 : builder_push_elems (builder, mask_elems);
17220 :
17221 0 : vec_perm_indices sel (builder, 2, 4);
17222 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17223 :
17224 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17225 0 : validate_res (4, 1, res, expected_res);
17226 0 : }
17227 : }
17228 0 : }
17229 :
17230 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17231 :
17232 : static void
17233 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17234 : {
17235 0 : for (int i = 0; i < 10; i++)
17236 : {
17237 : /* Case 1:
17238 : sel = { 0, 1, 2, 3}
17239 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17240 0 : {
17241 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17242 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17243 :
17244 0 : tree inner_type
17245 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17246 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17247 :
17248 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17249 0 : vec_perm_builder builder (res_len, 4, 1);
17250 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17251 0 : builder_push_elems (builder, mask_elems);
17252 :
17253 0 : vec_perm_indices sel (builder, 2,
17254 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17255 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17256 :
17257 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17258 0 : validate_res_vls (res, expected_res, 4);
17259 0 : }
17260 :
17261 : /* Case 2: Same as Case 1, but crossing input vector.
17262 : sel = {0, 2, 4, 6}
17263 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17264 : Since we cannot determine, which vector to choose from during
17265 : compile time, should return NULL_TREE. */
17266 0 : {
17267 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17268 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17269 :
17270 0 : tree inner_type
17271 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17272 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17273 :
17274 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17275 0 : vec_perm_builder builder (res_len, 4, 1);
17276 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17277 0 : builder_push_elems (builder, mask_elems);
17278 :
17279 0 : vec_perm_indices sel (builder, 2,
17280 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17281 0 : const char *reason;
17282 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17283 :
17284 0 : ASSERT_TRUE (res == NULL_TREE);
17285 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17286 0 : }
17287 : }
17288 0 : }
17289 :
17290 : /* Test all input vectors. */
17291 :
17292 : static void
17293 0 : test_all_nunits (machine_mode vmode)
17294 : {
17295 : /* Test with 10 different inputs. */
17296 0 : for (int i = 0; i < 10; i++)
17297 : {
17298 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17299 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17300 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17301 :
17302 : /* Case 1: mask = {0, ...} // (1, 1)
17303 : res = { arg0[0], ... } // (1, 1) */
17304 0 : {
17305 0 : vec_perm_builder builder (len, 1, 1);
17306 0 : builder.quick_push (0);
17307 0 : vec_perm_indices sel (builder, 2, len);
17308 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17309 0 : tree expected_res[] = { ARG0(0) };
17310 0 : validate_res (1, 1, res, expected_res);
17311 0 : }
17312 :
17313 : /* Case 2: mask = {len, ...} // (1, 1)
17314 : res = { arg1[0], ... } // (1, 1) */
17315 0 : {
17316 0 : vec_perm_builder builder (len, 1, 1);
17317 0 : builder.quick_push (len);
17318 0 : vec_perm_indices sel (builder, 2, len);
17319 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17320 :
17321 0 : tree expected_res[] = { ARG1(0) };
17322 0 : validate_res (1, 1, res, expected_res);
17323 0 : }
17324 : }
17325 0 : }
17326 :
17327 : /* Test all vectors which contain at-least 2 elements. */
17328 :
17329 : static void
17330 0 : test_nunits_min_2 (machine_mode vmode)
17331 : {
17332 0 : for (int i = 0; i < 10; i++)
17333 : {
17334 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17335 : res = { arg0[0], arg1[0], ... } // (2, 1) */
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, 1);
17342 0 : poly_uint64 mask_elems[] = { 0, len };
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) };
17349 0 : validate_res (2, 1, res, expected_res);
17350 0 : }
17351 :
17352 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17353 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17354 0 : {
17355 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17356 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17357 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17358 :
17359 0 : vec_perm_builder builder (len, 2, 2);
17360 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17361 0 : builder_push_elems (builder, mask_elems);
17362 :
17363 0 : vec_perm_indices sel (builder, 2, len);
17364 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17365 :
17366 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17367 0 : validate_res (2, 2, res, expected_res);
17368 0 : }
17369 :
17370 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17371 : Test that the stepped sequence of the pattern selects from
17372 : same input pattern. Since input vectors have npatterns = 2,
17373 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17374 : in input vector. So return NULL_TREE. */
17375 0 : {
17376 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17377 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17378 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17379 :
17380 0 : vec_perm_builder builder (len, 1, 3);
17381 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17382 0 : builder_push_elems (builder, mask_elems);
17383 :
17384 0 : vec_perm_indices sel (builder, 2, len);
17385 0 : const char *reason;
17386 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17387 : &reason);
17388 0 : ASSERT_TRUE (res == NULL_TREE);
17389 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17390 0 : }
17391 :
17392 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17393 : Test that stepped sequence of the pattern selects from arg0.
17394 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17395 0 : {
17396 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17397 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17398 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17399 :
17400 0 : vec_perm_builder builder (len, 1, 3);
17401 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17402 0 : builder_push_elems (builder, mask_elems);
17403 :
17404 0 : vec_perm_indices sel (builder, 2, len);
17405 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17406 :
17407 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17408 0 : validate_res (1, 3, res, expected_res);
17409 0 : }
17410 :
17411 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17412 : In this case ensure that arg has a natural stepped sequence
17413 : to preserve arg's encoding.
17414 :
17415 : As a concrete example, consider:
17416 : arg0: { -16, -9, -10, ... } // (1, 3)
17417 : arg1: { -12, -5, -6, ... } // (1, 3)
17418 : sel = { 0, len, len + 1, ... } // (1, 3)
17419 :
17420 : This will create res with following encoding:
17421 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17422 : = { -16, -12, -5, ... }
17423 :
17424 : The step in above encoding would be: (-5) - (-12) = 7
17425 : And hence res[3] would be computed as -5 + 7 = 2.
17426 : instead of arg1[2], ie, -6.
17427 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17428 : for this case. */
17429 0 : {
17430 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17431 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17432 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17433 :
17434 0 : vec_perm_builder builder (len, 1, 3);
17435 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17436 0 : builder_push_elems (builder, mask_elems);
17437 :
17438 0 : vec_perm_indices sel (builder, 2, len);
17439 0 : const char *reason;
17440 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17441 : stepped pattern, even if we didn't explicitly tell it to. So folding
17442 : may not always fail, but if it does, ensure that's because arg1 does
17443 : not have a natural stepped sequence (and not due to other reason) */
17444 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17445 0 : if (res == NULL_TREE)
17446 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17447 0 : }
17448 :
17449 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17450 : sequence and thus folding should be valid for this case. */
17451 0 : {
17452 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17453 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17454 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17455 :
17456 0 : vec_perm_builder builder (len, 1, 3);
17457 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17458 0 : builder_push_elems (builder, mask_elems);
17459 :
17460 0 : vec_perm_indices sel (builder, 2, len);
17461 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17462 :
17463 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17464 0 : validate_res (1, 3, res, expected_res);
17465 0 : }
17466 :
17467 : /* Case 8: Same as aarch64/sve/slp_3.c:
17468 : arg0, arg1 are dup vectors.
17469 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17470 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17471 :
17472 : In this case, since the input vectors are dup, only the first two
17473 : elements per pattern in sel are considered significant. */
17474 0 : {
17475 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17476 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17477 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17478 :
17479 0 : vec_perm_builder builder (len, 2, 3);
17480 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17481 0 : builder_push_elems (builder, mask_elems);
17482 :
17483 0 : vec_perm_indices sel (builder, 2, len);
17484 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17485 :
17486 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17487 0 : validate_res (2, 1, res, expected_res);
17488 0 : }
17489 : }
17490 0 : }
17491 :
17492 : /* Test all vectors which contain at-least 4 elements. */
17493 :
17494 : static void
17495 0 : test_nunits_min_4 (machine_mode vmode)
17496 : {
17497 0 : for (int i = 0; i < 10; i++)
17498 : {
17499 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17500 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17501 0 : {
17502 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17503 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17504 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17505 :
17506 0 : vec_perm_builder builder (len, 4, 1);
17507 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17508 0 : builder_push_elems (builder, mask_elems);
17509 :
17510 0 : vec_perm_indices sel (builder, 2, len);
17511 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17512 :
17513 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17514 0 : validate_res (4, 1, res, expected_res);
17515 0 : }
17516 :
17517 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17518 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17519 0 : {
17520 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17521 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17522 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17523 :
17524 0 : vec_perm_builder builder (arg0_len, 1, 3);
17525 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17526 0 : builder_push_elems (builder, mask_elems);
17527 :
17528 0 : vec_perm_indices sel (builder, 2, arg0_len);
17529 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17530 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17531 0 : validate_res (1, 3, res, expected_res);
17532 0 : }
17533 :
17534 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17535 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17536 0 : {
17537 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17538 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17539 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17540 :
17541 0 : vec_perm_builder builder (len, 1, 3);
17542 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17543 0 : builder_push_elems (builder, mask_elems);
17544 :
17545 0 : vec_perm_indices sel (builder, 2, len);
17546 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17547 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17548 0 : validate_res (1, 3, res, expected_res);
17549 0 : }
17550 :
17551 : /* Case 4:
17552 : sel = { len, 0, 2, ... } // (1, 3)
17553 : This should return NULL because we cross the input vectors.
17554 : Because,
17555 : Let's assume len = C + Cx
17556 : a1 = 0
17557 : S = 2
17558 : esel = arg0_len / sel_npatterns = C + Cx
17559 : ae = 0 + (esel - 2) * S
17560 : = 0 + (C + Cx - 2) * 2
17561 : = 2(C-2) + 2Cx
17562 :
17563 : For C >= 4:
17564 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17565 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17566 : Since q1 != qe, we cross input vectors.
17567 : So return NULL_TREE. */
17568 0 : {
17569 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17570 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17571 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17572 :
17573 0 : vec_perm_builder builder (arg0_len, 1, 3);
17574 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17575 0 : builder_push_elems (builder, mask_elems);
17576 :
17577 0 : vec_perm_indices sel (builder, 2, arg0_len);
17578 0 : const char *reason;
17579 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17580 0 : ASSERT_TRUE (res == NULL_TREE);
17581 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17582 0 : }
17583 :
17584 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17585 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17586 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17587 :
17588 : Note that fold_vec_perm_cst will set
17589 : res_npatterns = max(4, max(4, 2)) = 4
17590 : However after canonicalizing, we will end up with shape (2, 2). */
17591 0 : {
17592 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17593 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17594 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17595 :
17596 0 : vec_perm_builder builder (len, 2, 2);
17597 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17598 0 : builder_push_elems (builder, mask_elems);
17599 :
17600 0 : vec_perm_indices sel (builder, 2, len);
17601 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17602 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17603 0 : validate_res (2, 2, res, expected_res);
17604 0 : }
17605 :
17606 : /* Case 6: Test combination in sel, where one pattern is dup and other
17607 : is stepped sequence.
17608 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17609 : res = { arg0[0], arg0[0], arg0[0],
17610 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17611 0 : {
17612 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17613 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17614 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17615 :
17616 0 : vec_perm_builder builder (len, 2, 3);
17617 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17618 0 : builder_push_elems (builder, mask_elems);
17619 :
17620 0 : vec_perm_indices sel (builder, 2, len);
17621 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17622 :
17623 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17624 0 : ARG0(1), ARG0(0), ARG0(2) };
17625 0 : validate_res (2, 3, res, expected_res);
17626 0 : }
17627 :
17628 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17629 : when arg0, arg1 and sel have different number of patterns.
17630 : arg0 is of shape (1, 1)
17631 : arg1 is of shape (4, 1)
17632 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17633 :
17634 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17635 : However,
17636 : step = (len+2) - (len+1) = 1
17637 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17638 : Since step is not a multiple of arg_npatterns,
17639 : valid_mask_for_fold_vec_perm_cst should return false,
17640 : and thus fold_vec_perm_cst should return NULL_TREE. */
17641 0 : {
17642 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17643 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17644 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17645 :
17646 0 : vec_perm_builder builder (len, 2, 3);
17647 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17648 0 : builder_push_elems (builder, mask_elems);
17649 :
17650 0 : vec_perm_indices sel (builder, 2, len);
17651 0 : const char *reason;
17652 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17653 :
17654 0 : ASSERT_TRUE (res == NULL_TREE);
17655 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17656 0 : }
17657 :
17658 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17659 : check that the result is not a stepped sequence either, even
17660 : if sel has a stepped sequence. */
17661 0 : {
17662 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17663 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17664 :
17665 0 : vec_perm_builder builder (len, 1, 3);
17666 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17667 0 : builder_push_elems (builder, mask_elems);
17668 :
17669 0 : vec_perm_indices sel (builder, 1, len);
17670 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17671 :
17672 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17673 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17674 0 : }
17675 :
17676 : /* Case 9: If sel doesn't contain a stepped sequence,
17677 : check that the result has same encoding as sel, irrespective
17678 : of shape of input vectors. */
17679 0 : {
17680 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17681 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17682 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17683 :
17684 0 : vec_perm_builder builder (len, 1, 2);
17685 0 : poly_uint64 mask_elems[] = { 0, len };
17686 0 : builder_push_elems (builder, mask_elems);
17687 :
17688 0 : vec_perm_indices sel (builder, 2, len);
17689 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17690 :
17691 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17692 0 : validate_res (sel.encoding ().npatterns (),
17693 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17694 0 : }
17695 : }
17696 0 : }
17697 :
17698 : /* Test all vectors which contain at-least 8 elements. */
17699 :
17700 : static void
17701 0 : test_nunits_min_8 (machine_mode vmode)
17702 : {
17703 0 : for (int i = 0; i < 10; i++)
17704 : {
17705 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17706 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17707 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17708 : arg0[2], arg0[0], arg0[3], arg1[0],
17709 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17710 0 : {
17711 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17712 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17713 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17714 :
17715 0 : vec_perm_builder builder(len, 4, 3);
17716 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17717 0 : 4, 0, 5, len };
17718 0 : builder_push_elems (builder, mask_elems);
17719 :
17720 0 : vec_perm_indices sel (builder, 2, len);
17721 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17722 :
17723 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17724 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17725 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17726 0 : validate_res (4, 3, res, expected_res);
17727 0 : }
17728 : }
17729 0 : }
17730 :
17731 : /* Test vectors for which nunits[0] <= 4. */
17732 :
17733 : static void
17734 0 : test_nunits_max_4 (machine_mode vmode)
17735 : {
17736 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17737 : This should return NULL_TREE because the index 4 may choose
17738 : from either arg0 or arg1 depending on vector length. */
17739 0 : {
17740 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17741 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17742 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17743 :
17744 0 : vec_perm_builder builder (len, 1, 2);
17745 0 : poly_uint64 mask_elems[] = {0, 4};
17746 0 : builder_push_elems (builder, mask_elems);
17747 :
17748 0 : vec_perm_indices sel (builder, 2, len);
17749 0 : const char *reason;
17750 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17751 0 : ASSERT_TRUE (res == NULL_TREE);
17752 0 : ASSERT_TRUE (reason != NULL);
17753 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17754 0 : }
17755 0 : }
17756 :
17757 : #undef ARG0
17758 : #undef ARG1
17759 :
17760 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17761 :
17762 : static bool
17763 0 : is_simple_vla_size (poly_uint64 size)
17764 : {
17765 124 : if (size.is_constant ()
17766 : || !pow2p_hwi (size.coeffs[0]))
17767 0 : return false;
17768 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17769 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17770 : return false;
17771 : return true;
17772 : }
17773 :
17774 : /* Execute fold_vec_perm_cst unit tests. */
17775 :
17776 : static void
17777 4 : test ()
17778 : {
17779 4 : machine_mode vnx4si_mode = E_VOIDmode;
17780 4 : machine_mode v4si_mode = E_VOIDmode;
17781 :
17782 4 : machine_mode vmode;
17783 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17784 : {
17785 : /* Obtain modes corresponding to VNx4SI and V4SI,
17786 : to call mixed mode tests below.
17787 : FIXME: Is there a better way to do this ? */
17788 124 : if (GET_MODE_INNER (vmode) == SImode)
17789 : {
17790 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17791 124 : if (is_simple_vla_size (nunits)
17792 : && nunits.coeffs[0] == 4)
17793 : vnx4si_mode = vmode;
17794 124 : else if (known_eq (nunits, poly_uint64 (4)))
17795 124 : v4si_mode = vmode;
17796 : }
17797 :
17798 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17799 : || !targetm.vector_mode_supported_p (vmode))
17800 124 : continue;
17801 :
17802 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17803 : test_all_nunits (vmode);
17804 : if (nunits.coeffs[0] >= 2)
17805 : test_nunits_min_2 (vmode);
17806 : if (nunits.coeffs[0] >= 4)
17807 : test_nunits_min_4 (vmode);
17808 : if (nunits.coeffs[0] >= 8)
17809 : test_nunits_min_8 (vmode);
17810 :
17811 : if (nunits.coeffs[0] <= 4)
17812 : test_nunits_max_4 (vmode);
17813 : }
17814 :
17815 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17816 : && targetm.vector_mode_supported_p (vnx4si_mode)
17817 : && targetm.vector_mode_supported_p (v4si_mode))
17818 : {
17819 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17820 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17821 : }
17822 4 : }
17823 : } // end of test_fold_vec_perm_cst namespace
17824 :
17825 : /* Verify that various binary operations on vectors are folded
17826 : correctly. */
17827 :
17828 : static void
17829 4 : test_vector_folding ()
17830 : {
17831 4 : tree inner_type = integer_type_node;
17832 4 : tree type = build_vector_type (inner_type, 4);
17833 4 : tree zero = build_zero_cst (type);
17834 4 : tree one = build_one_cst (type);
17835 4 : tree index = build_index_vector (type, 0, 1);
17836 :
17837 : /* Verify equality tests that return a scalar boolean result. */
17838 4 : tree res_type = boolean_type_node;
17839 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17840 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17841 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17842 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17843 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17844 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17845 : index, one)));
17846 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17847 : index, index)));
17848 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17849 : index, index)));
17850 4 : }
17851 :
17852 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17853 :
17854 : static void
17855 4 : test_vec_duplicate_folding ()
17856 : {
17857 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17858 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17859 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17860 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17861 :
17862 4 : tree type = build_vector_type (ssizetype, nunits);
17863 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17864 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17865 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17866 4 : }
17867 :
17868 : /* Verify folding of VEC_UNPACK_{LO,HI}_EXPRs. */
17869 :
17870 : static void
17871 4 : test_vec_unpack_folding ()
17872 : {
17873 4 : machine_mode vmode;
17874 0 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17875 : {
17876 0 : poly_uint64 in_nelts = GET_MODE_NUNITS (vmode);
17877 0 : if (!test_fold_vec_perm_cst::is_simple_vla_size (in_nelts)
17878 : || in_nelts.coeffs[0] < 2
17879 : || !targetm.vector_mode_supported_p (vmode))
17880 0 : continue;
17881 :
17882 : unsigned int in_bits = GET_MODE_UNIT_BITSIZE (vmode);
17883 : if (in_bits >= HOST_BITS_PER_WIDE_INT)
17884 : continue;
17885 :
17886 : unsigned int min_out_nelts = in_nelts.coeffs[0] / 2;
17887 : tree in_inner_type = lang_hooks.types.type_for_mode
17888 : (GET_MODE_INNER (vmode), 1);
17889 : tree out_inner_type = build_nonstandard_integer_type (in_bits * 2, 1);
17890 : tree in_type = build_vector_type_for_mode (in_inner_type, vmode);
17891 : poly_uint64 out_nelts = exact_div (in_nelts, 2);
17892 : tree out_type = build_vector_type (out_inner_type, out_nelts);
17893 :
17894 : tree_vector_builder builder (in_type, min_out_nelts, 2);
17895 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17896 : builder.quick_push (build_int_cst (in_inner_type, i + 1));
17897 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17898 : builder.quick_push (build_int_cst (in_inner_type, 77));
17899 : tree arg = builder.build ();
17900 :
17901 : /* Check integer unpacking with a duplicated scalable high half. */
17902 : tree lo = const_unop (VEC_UNPACK_LO_EXPR, out_type, arg);
17903 : ASSERT_TRUE (lo != NULL_TREE);
17904 : tree hi = const_unop (VEC_UNPACK_HI_EXPR, out_type, arg);
17905 : ASSERT_TRUE (hi != NULL_TREE);
17906 :
17907 : tree prefix = (!BYTES_BIG_ENDIAN ? lo : hi);
17908 : tree dup = (!BYTES_BIG_ENDIAN ? hi : lo);
17909 :
17910 : ASSERT_EQ (VECTOR_CST, TREE_CODE (prefix));
17911 : ASSERT_EQ (VECTOR_CST, TREE_CODE (dup));
17912 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17913 : VECTOR_CST_NELTS (prefix));
17914 : ASSERT_KNOWN_EQ (TYPE_VECTOR_SUBPARTS (out_type),
17915 : VECTOR_CST_NELTS (dup));
17916 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (prefix));
17917 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (prefix));
17918 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (dup));
17919 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (dup));
17920 :
17921 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17922 : {
17923 : tree elt = build_int_cst (out_inner_type, i + 1);
17924 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (prefix, i),
17925 : elt, 0));
17926 : tree fill = build_int_cst (out_inner_type, 77);
17927 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17928 : (prefix, min_out_nelts + i),
17929 : fill, 0));
17930 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (dup, i),
17931 : fill, 0));
17932 : }
17933 :
17934 : /* Check int-to-float unpacking with the same scalable shape. */
17935 : tree float_out_type = build_vector_type (float_type_node, out_nelts);
17936 : tree float_lo = const_unop (VEC_UNPACK_FLOAT_LO_EXPR, float_out_type,
17937 : arg);
17938 : ASSERT_TRUE (float_lo != NULL_TREE);
17939 : tree float_hi = const_unop (VEC_UNPACK_FLOAT_HI_EXPR, float_out_type,
17940 : arg);
17941 : ASSERT_TRUE (float_hi != NULL_TREE);
17942 :
17943 : tree float_prefix = (!BYTES_BIG_ENDIAN ? float_lo : float_hi);
17944 : tree float_dup = (!BYTES_BIG_ENDIAN ? float_hi : float_lo);
17945 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_prefix));
17946 : ASSERT_EQ (VECTOR_CST, TREE_CODE (float_dup));
17947 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (float_prefix));
17948 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (float_prefix));
17949 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (float_dup));
17950 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (float_dup));
17951 :
17952 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17953 : {
17954 : tree elt = fold_convert_const (FLOAT_EXPR, float_type_node,
17955 : build_int_cst (in_inner_type, i + 1));
17956 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_prefix, i),
17957 : elt, 0));
17958 : tree fill = fold_convert_const (FLOAT_EXPR, float_type_node,
17959 : build_int_cst (in_inner_type, 77));
17960 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
17961 : (float_prefix, min_out_nelts + i),
17962 : fill, 0));
17963 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (float_dup, i),
17964 : fill, 0));
17965 : }
17966 :
17967 : /* Check float-to-int unpacking with the same scalable shape. */
17968 : tree float_in_type = build_vector_type (float_type_node, in_nelts);
17969 : tree_vector_builder fix_builder (float_in_type, min_out_nelts, 2);
17970 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17971 : fix_builder.quick_push
17972 : (build_real_from_int_cst (float_type_node,
17973 : build_int_cst (integer_type_node, i + 1)));
17974 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17975 : fix_builder.quick_push
17976 : (build_real_from_int_cst (float_type_node,
17977 : build_int_cst (integer_type_node, 77)));
17978 : tree fix_arg = fix_builder.build ();
17979 : tree fix_out_type = build_vector_type (integer_type_node, out_nelts);
17980 : tree fix_lo = const_unop (VEC_UNPACK_FIX_TRUNC_LO_EXPR, fix_out_type,
17981 : fix_arg);
17982 : ASSERT_TRUE (fix_lo != NULL_TREE);
17983 : tree fix_hi = const_unop (VEC_UNPACK_FIX_TRUNC_HI_EXPR, fix_out_type,
17984 : fix_arg);
17985 : ASSERT_TRUE (fix_hi != NULL_TREE);
17986 :
17987 : tree fix_prefix = (!BYTES_BIG_ENDIAN ? fix_lo : fix_hi);
17988 : tree fix_dup = (!BYTES_BIG_ENDIAN ? fix_hi : fix_lo);
17989 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_prefix));
17990 : ASSERT_EQ (VECTOR_CST, TREE_CODE (fix_dup));
17991 : ASSERT_EQ (min_out_nelts, VECTOR_CST_NPATTERNS (fix_prefix));
17992 : ASSERT_EQ (2, VECTOR_CST_NELTS_PER_PATTERN (fix_prefix));
17993 : ASSERT_EQ (1, VECTOR_CST_NPATTERNS (fix_dup));
17994 : ASSERT_EQ (1, VECTOR_CST_NELTS_PER_PATTERN (fix_dup));
17995 :
17996 : for (unsigned int i = 0; i < min_out_nelts; ++i)
17997 : {
17998 : tree elt = build_int_cst (integer_type_node, i + 1);
17999 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_prefix, i),
18000 : elt, 0));
18001 : tree fill = build_int_cst (integer_type_node, 77);
18002 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT
18003 : (fix_prefix, min_out_nelts + i),
18004 : fill, 0));
18005 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (fix_dup, i),
18006 : fill, 0));
18007 : }
18008 :
18009 : /* Reject scalable constants whose high half is not duplicated. */
18010 : tree_vector_builder varied (in_type, in_nelts.coeffs[0], 2);
18011 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
18012 : varied.quick_push (build_int_cst (in_inner_type, i + 1));
18013 : for (unsigned int i = 0; i < in_nelts.coeffs[0]; ++i)
18014 : varied.quick_push (build_int_cst (in_inner_type, 100 + i));
18015 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
18016 : varied.build ()));
18017 :
18018 : /* Reject stepped scalable constants. */
18019 : tree_vector_builder stepped (in_type, 1, 3);
18020 : stepped.quick_push (build_int_cst (in_inner_type, 1));
18021 : stepped.quick_push (build_int_cst (in_inner_type, 2));
18022 : stepped.quick_push (build_int_cst (in_inner_type, 3));
18023 : ASSERT_EQ (NULL_TREE, const_unop (VEC_UNPACK_LO_EXPR, out_type,
18024 : stepped.build ()));
18025 :
18026 : return;
18027 : }
18028 : }
18029 :
18030 : /* Run all of the selftests within this file. */
18031 :
18032 : void
18033 4 : fold_const_cc_tests ()
18034 : {
18035 4 : test_arithmetic_folding ();
18036 4 : test_vector_folding ();
18037 4 : test_vec_duplicate_folding ();
18038 4 : test_vec_unpack_folding ();
18039 4 : test_fold_vec_perm_cst::test ();
18040 4 : test_operand_equality::test ();
18041 4 : }
18042 :
18043 : } // namespace selftest
18044 :
18045 : #endif /* CHECKING_P */
|