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 349 : operator ~(comparison_code cmp)
132 : {
133 349 : unsigned char newcmp = cmp;
134 349 : newcmp = ~newcmp & 0xF;
135 349 : return (comparison_code)newcmp;
136 : }
137 :
138 : // Implements bitwise ior on comparison_code.
139 : comparison_code
140 23146 : operator |(comparison_code cmp0, comparison_code cmp1)
141 : {
142 23146 : unsigned char newcmp = ((unsigned char)cmp0) | cmp1;
143 23146 : newcmp = newcmp & 0xF;
144 23146 : return (comparison_code)newcmp;
145 : }
146 :
147 : // Implements bitwise and on comparison_code.
148 : comparison_code
149 64828 : operator &(comparison_code cmp0, comparison_code cmp1)
150 : {
151 64828 : unsigned char newcmp = ((unsigned char)cmp0) & cmp1;
152 64828 : newcmp = newcmp & 0xF;
153 64828 : return (comparison_code)newcmp;
154 : }
155 :
156 : // Implements bitwise xor on comparison_code.
157 : comparison_code
158 1189 : operator ^(comparison_code cmp0, comparison_code cmp1)
159 : {
160 1189 : unsigned char newcmp = ((unsigned char)cmp0) ^ cmp1;
161 1189 : newcmp = newcmp & 0xF;
162 1189 : 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 144160 : minmax_from_comparison (tree_code cmp, tree exp0,
200 : const widest_int cst1,
201 : const widest_int cst2)
202 : {
203 144160 : if (cst1 == cst2)
204 : {
205 125 : if (cmp == LE_EXPR || cmp == LT_EXPR)
206 : return MIN_EXPR;
207 106 : if (cmp == GT_EXPR || cmp == GE_EXPR)
208 : return MAX_EXPR;
209 : }
210 144141 : if (cst1 == cst2 - 1)
211 : {
212 : /* X <= Y - 1 equals to X < Y. */
213 87288 : if (cmp == LE_EXPR)
214 : return MIN_EXPR;
215 : /* X > Y - 1 equals to X >= Y. */
216 86837 : 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 74208 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
220 : {
221 20655 : int_range_max r;
222 41310 : get_range_query (cfun)->range_of_expr (r, exp0);
223 20655 : if (r.undefined_p ())
224 0 : r.set_varying (TREE_TYPE (exp0));
225 :
226 20655 : widest_int min = widest_int::from (r.lower_bound (),
227 41310 : TYPE_SIGN (TREE_TYPE (exp0)));
228 20655 : if (min == cst1)
229 497 : return MAX_EXPR;
230 20655 : }
231 : }
232 130564 : if (cst1 == cst2 + 1)
233 : {
234 : /* X < Y + 1 equals to X <= Y. */
235 1282 : if (cmp == LT_EXPR)
236 : return MIN_EXPR;
237 : /* X >= Y + 1 equals to X > Y. */
238 1254 : 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 1092 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
242 : {
243 684 : int_range_max r;
244 1368 : get_range_query (cfun)->range_of_expr (r, exp0);
245 684 : if (r.undefined_p ())
246 0 : r.set_varying (TREE_TYPE (exp0));
247 :
248 684 : widest_int max = widest_int::from (r.upper_bound (),
249 1368 : TYPE_SIGN (TREE_TYPE (exp0)));
250 684 : if (max == cst1)
251 188 : return MIN_EXPR;
252 684 : }
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 177912 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
262 : {
263 177912 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
264 : return ERROR_MARK;
265 :
266 177901 : if (!operand_equal_p (exp0, exp2))
267 : return ERROR_MARK;
268 :
269 177901 : if (operand_equal_p (exp1, exp3))
270 : {
271 33666 : if (cmp == LT_EXPR || cmp == LE_EXPR)
272 : return MIN_EXPR;
273 31492 : if (cmp == GT_EXPR || cmp == GE_EXPR)
274 : return MAX_EXPR;
275 : }
276 144341 : if (TREE_CODE (exp3) == INTEGER_CST
277 143867 : && TREE_CODE (exp1) == INTEGER_CST)
278 143411 : 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 3068980 : expr_location_or (tree t, location_t loc)
287 : {
288 959736 : location_t tloc = EXPR_LOCATION (t);
289 3052627 : 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 9439069 : protected_set_expr_location_unshare (tree x, location_t loc)
297 : {
298 9439069 : if (CAN_HAVE_LOCATION_P (x)
299 8393222 : && EXPR_LOCATION (x) != loc
300 2678342 : && !(TREE_CODE (x) == SAVE_EXPR
301 1339386 : || TREE_CODE (x) == TARGET_EXPR
302 : || TREE_CODE (x) == BIND_EXPR))
303 : {
304 1338620 : x = copy_node (x);
305 1338620 : SET_EXPR_LOCATION (x, loc);
306 : }
307 9439069 : 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 2190930 : negate_mathfn_p (combined_fn fn)
315 : {
316 2190930 : 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 2186849 : default:
386 2186849 : break;
387 : }
388 2186849 : return false;
389 : }
390 :
391 : /* Check whether we may negate an integer constant T without causing
392 : overflow. */
393 :
394 : bool
395 3118977 : may_negate_without_overflow_p (const_tree t)
396 : {
397 3118977 : tree type;
398 :
399 3118977 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
400 :
401 3118977 : type = TREE_TYPE (t);
402 3118977 : if (TYPE_UNSIGNED (type))
403 : return false;
404 :
405 3118977 : 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 27981525 : negate_expr_p (tree t)
413 : {
414 28139337 : tree type;
415 :
416 28139337 : if (t == 0)
417 : return false;
418 :
419 28139337 : type = TREE_TYPE (t);
420 :
421 28139337 : STRIP_SIGN_NOPS (t);
422 28139337 : switch (TREE_CODE (t))
423 : {
424 1620263 : case INTEGER_CST:
425 1620263 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
426 : return true;
427 :
428 : /* Check that -CST will not overflow type. */
429 379582 : return may_negate_without_overflow_p (t);
430 539 : case BIT_NOT_EXPR:
431 539 : return (INTEGRAL_TYPE_P (type)
432 539 : && TYPE_OVERFLOW_WRAPS (type));
433 :
434 : case FIXED_CST:
435 : return true;
436 :
437 1297 : case NEGATE_EXPR:
438 1297 : return !TYPE_OVERFLOW_SANITIZED (type);
439 :
440 1307104 : case REAL_CST:
441 : /* We want to canonicalize to positive real constants. Pretend
442 : that only negative ones can be easily negated. */
443 1307104 : 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 1543271 : case PLUS_EXPR:
471 1543271 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
472 1543265 : || HONOR_SIGNED_ZEROS (type)
473 2795947 : || (ANY_INTEGRAL_TYPE_P (type)
474 1252462 : && ! TYPE_OVERFLOW_WRAPS (type)))
475 : return false;
476 : /* -(A + B) -> (-B) - A. */
477 801158 : if (negate_expr_p (TREE_OPERAND (t, 1)))
478 : return true;
479 : /* -(A + B) -> (-A) - B. */
480 147396 : return negate_expr_p (TREE_OPERAND (t, 0));
481 :
482 263056 : case MINUS_EXPR:
483 : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
484 263056 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
485 263056 : && !HONOR_SIGNED_ZEROS (type)
486 350601 : && (! ANY_INTEGRAL_TYPE_P (type)
487 87322 : || TYPE_OVERFLOW_WRAPS (type));
488 :
489 2374491 : case MULT_EXPR:
490 2374491 : 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 4137608 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
495 153262 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
496 2219766 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
497 0 : && (wi::popcount
498 2068804 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
499 150962 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
500 127802 : && (wi::popcount
501 2196606 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
502 : break;
503 :
504 : /* Fall through. */
505 :
506 2351492 : case RDIV_EXPR:
507 2351492 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
508 2351491 : return negate_expr_p (TREE_OPERAND (t, 1))
509 2351491 : || negate_expr_p (TREE_OPERAND (t, 0));
510 : break;
511 :
512 2508 : case TRUNC_DIV_EXPR:
513 2508 : case ROUND_DIV_EXPR:
514 2508 : case EXACT_DIV_EXPR:
515 2508 : 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 550 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
520 550 : && 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 766 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
526 383 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
527 681 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
528 288 : && ! integer_onep (TREE_OPERAND (t, 1))))
529 373 : return negate_expr_p (TREE_OPERAND (t, 1));
530 : break;
531 :
532 5118851 : case NOP_EXPR:
533 : /* Negate -((double)float) as (double)(-float). */
534 5118851 : if (SCALAR_FLOAT_TYPE_P (type))
535 : {
536 10166 : tree tem = strip_float_extensions (t);
537 10166 : if (tem != t)
538 : return negate_expr_p (tem);
539 : }
540 : break;
541 :
542 1101490 : case CALL_EXPR:
543 : /* Negate -f(x) as f(-x). */
544 1101490 : if (negate_mathfn_p (get_call_combined_fn (t)))
545 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
546 : break;
547 :
548 12811 : case RSHIFT_EXPR:
549 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
550 12811 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
551 : {
552 12666 : tree op1 = TREE_OPERAND (t, 1);
553 12666 : 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 40242419 : fold_negate_expr_1 (location_t loc, tree t)
571 : {
572 40242419 : tree type = TREE_TYPE (t);
573 40242419 : tree tem;
574 :
575 40242419 : 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 30943761 : case INTEGER_CST:
585 30943761 : tem = fold_negate_const (t, type);
586 30943761 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
587 9794 : || (ANY_INTEGRAL_TYPE_P (type)
588 9794 : && !TYPE_OVERFLOW_TRAPS (type)
589 9794 : && TYPE_OVERFLOW_WRAPS (type))
590 30953080 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
591 30943649 : return tem;
592 : break;
593 :
594 2039697 : case POLY_INT_CST:
595 2039697 : case REAL_CST:
596 2039697 : case FIXED_CST:
597 2039697 : tem = fold_negate_const (t, type);
598 2039697 : return tem;
599 :
600 66208 : case COMPLEX_CST:
601 66208 : {
602 66208 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
603 66208 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
604 66208 : if (rpart && ipart)
605 66208 : return build_complex (type, rpart, ipart);
606 : }
607 : break;
608 :
609 51056 : case VECTOR_CST:
610 51056 : {
611 51056 : tree_vector_builder elts;
612 51056 : elts.new_unary_operation (type, t, true);
613 51056 : unsigned int count = elts.encoded_nelts ();
614 125051 : for (unsigned int i = 0; i < count; ++i)
615 : {
616 73995 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
617 73995 : if (elt == NULL_TREE)
618 0 : return NULL_TREE;
619 73995 : elts.quick_push (elt);
620 : }
621 :
622 51056 : return elts.build ();
623 51056 : }
624 :
625 78 : case COMPLEX_EXPR:
626 78 : if (negate_expr_p (t))
627 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
628 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
629 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
630 : break;
631 :
632 21 : case CONJ_EXPR:
633 21 : if (negate_expr_p (t))
634 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
635 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
636 : break;
637 :
638 1236 : case NEGATE_EXPR:
639 1236 : if (!TYPE_OVERFLOW_SANITIZED (type))
640 1210 : return TREE_OPERAND (t, 0);
641 : break;
642 :
643 716013 : case PLUS_EXPR:
644 716013 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
645 716013 : && !HONOR_SIGNED_ZEROS (type))
646 : {
647 : /* -(A + B) -> (-B) - A. */
648 715903 : if (negate_expr_p (TREE_OPERAND (t, 1)))
649 : {
650 657788 : tem = negate_expr (TREE_OPERAND (t, 1));
651 657788 : return fold_build2_loc (loc, MINUS_EXPR, type,
652 1315576 : tem, TREE_OPERAND (t, 0));
653 : }
654 :
655 : /* -(A + B) -> (-A) - B. */
656 58115 : if (negate_expr_p (TREE_OPERAND (t, 0)))
657 : {
658 919 : tem = negate_expr (TREE_OPERAND (t, 0));
659 919 : return fold_build2_loc (loc, MINUS_EXPR, type,
660 1838 : tem, TREE_OPERAND (t, 1));
661 : }
662 : }
663 : break;
664 :
665 160337 : case MINUS_EXPR:
666 : /* - (A - B) -> B - A */
667 160337 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
668 160337 : && !HONOR_SIGNED_ZEROS (type))
669 82464 : return fold_build2_loc (loc, MINUS_EXPR, type,
670 164928 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
671 : break;
672 :
673 268131 : case MULT_EXPR:
674 268131 : if (TYPE_UNSIGNED (type))
675 : break;
676 :
677 : /* Fall through. */
678 :
679 32694 : case RDIV_EXPR:
680 32694 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
681 : {
682 32694 : tem = TREE_OPERAND (t, 1);
683 32694 : if (negate_expr_p (tem))
684 59140 : return fold_build2_loc (loc, TREE_CODE (t), type,
685 59140 : TREE_OPERAND (t, 0), negate_expr (tem));
686 3124 : tem = TREE_OPERAND (t, 0);
687 3124 : if (negate_expr_p (tem))
688 57 : return fold_build2_loc (loc, TREE_CODE (t), type,
689 114 : negate_expr (tem), TREE_OPERAND (t, 1));
690 : }
691 : break;
692 :
693 2020 : case TRUNC_DIV_EXPR:
694 2020 : case ROUND_DIV_EXPR:
695 2020 : case EXACT_DIV_EXPR:
696 2020 : 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 728 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
701 728 : && negate_expr_p (TREE_OPERAND (t, 0)))
702 325 : return fold_build2_loc (loc, TREE_CODE (t), type,
703 325 : negate_expr (TREE_OPERAND (t, 0)),
704 650 : 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 806 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
709 403 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
710 319 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
711 296 : && ! integer_onep (TREE_OPERAND (t, 1))))
712 783 : && negate_expr_p (TREE_OPERAND (t, 1)))
713 748 : return fold_build2_loc (loc, TREE_CODE (t), type,
714 374 : TREE_OPERAND (t, 0),
715 748 : negate_expr (TREE_OPERAND (t, 1)));
716 : break;
717 :
718 2410409 : case NOP_EXPR:
719 : /* Convert -((double)float) into (double)(-float). */
720 2410409 : if (SCALAR_FLOAT_TYPE_P (type))
721 : {
722 10958 : tem = strip_float_extensions (t);
723 10958 : if (tem != t && negate_expr_p (tem))
724 0 : return fold_convert_loc (loc, type, negate_expr (tem));
725 : }
726 : break;
727 :
728 298296 : case CALL_EXPR:
729 : /* Negate -f(x) as f(-x). */
730 298296 : if (negate_mathfn_p (get_call_combined_fn (t))
731 299585 : && 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 12555 : case RSHIFT_EXPR:
742 : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
743 12555 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
744 : {
745 12537 : tree op1 = TREE_OPERAND (t, 1);
746 12537 : if (wi::to_wide (op1) == element_precision (type) - 1)
747 : {
748 12209 : tree ntype = TYPE_UNSIGNED (type)
749 12209 : ? signed_type_for (type)
750 72 : : unsigned_type_for (type);
751 12209 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
752 12209 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
753 12209 : 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 40242419 : fold_negate_expr (location_t loc, tree t)
769 : {
770 40242419 : tree type = TREE_TYPE (t);
771 40242419 : STRIP_SIGN_NOPS (t);
772 40242419 : tree tem = fold_negate_expr_1 (loc, t);
773 40242419 : if (tem == NULL_TREE)
774 : return NULL_TREE;
775 33886896 : 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 3904605 : negate_expr (tree t)
784 : {
785 3904605 : tree type, tem;
786 3904605 : location_t loc;
787 :
788 3904605 : if (t == NULL_TREE)
789 : return NULL_TREE;
790 :
791 3904605 : loc = EXPR_LOCATION (t);
792 3904605 : type = TREE_TYPE (t);
793 3904605 : STRIP_SIGN_NOPS (t);
794 :
795 3904605 : tem = fold_negate_expr (loc, t);
796 3904605 : if (!tem)
797 1956948 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
798 3904605 : 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 233966638 : 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 233966638 : tree var = 0;
830 233966638 : *minus_varp = 0;
831 233966638 : *conp = 0;
832 233966638 : *minus_conp = 0;
833 233966638 : *litp = 0;
834 233966638 : *minus_litp = 0;
835 :
836 : /* Strip any conversions that don't change the machine mode or signedness. */
837 233966638 : STRIP_SIGN_NOPS (in);
838 :
839 233966638 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
840 149647429 : || TREE_CODE (in) == FIXED_CST)
841 84319209 : *litp = in;
842 149647429 : else if (TREE_CODE (in) == code
843 149647429 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
844 145003490 : && ! 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 145003490 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
850 60636921 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
851 143300931 : || (code == MINUS_EXPR
852 23459924 : && (TREE_CODE (in) == PLUS_EXPR
853 21605048 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
854 : {
855 8671173 : tree op0 = TREE_OPERAND (in, 0);
856 8671173 : tree op1 = TREE_OPERAND (in, 1);
857 8671173 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
858 8671173 : 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 8671173 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
862 8450299 : || TREE_CODE (op0) == FIXED_CST)
863 220874 : *litp = op0, op0 = 0;
864 8450299 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
865 5651133 : || TREE_CODE (op1) == FIXED_CST)
866 2799166 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
867 :
868 8671173 : if (op0 != 0 && TREE_CONSTANT (op0))
869 13618 : *conp = op0, op0 = 0;
870 8657555 : else if (op1 != 0 && TREE_CONSTANT (op1))
871 51231 : *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 8671173 : if (op0 != 0 && op1 != 0)
876 : var = in;
877 3078624 : else if (op0 != 0)
878 : var = op0;
879 : else
880 234492 : var = op1, neg_var_p = neg1_p;
881 :
882 : /* Now do any needed negations. */
883 8671173 : if (neg_litp_p)
884 28710 : *minus_litp = *litp, *litp = 0;
885 8671173 : if (neg_conp_p && *conp)
886 11396 : *minus_conp = *conp, *conp = 0;
887 8671173 : if (neg_var_p && var)
888 224954 : *minus_varp = var, var = 0;
889 : }
890 140976256 : else if (TREE_CONSTANT (in))
891 811934 : *conp = in;
892 140164322 : else if (TREE_CODE (in) == BIT_NOT_EXPR
893 549111 : && code == PLUS_EXPR)
894 : {
895 : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
896 : when IN is constant. */
897 387970 : *litp = build_minus_one_cst (type);
898 387970 : *minus_varp = TREE_OPERAND (in, 0);
899 : }
900 : else
901 : var = in;
902 :
903 233966638 : if (negate_p)
904 : {
905 12941435 : if (*litp)
906 1274131 : *minus_litp = *litp, *litp = 0;
907 11667304 : else if (*minus_litp)
908 174 : *litp = *minus_litp, *minus_litp = 0;
909 12941435 : if (*conp)
910 46232 : *minus_conp = *conp, *conp = 0;
911 12895203 : else if (*minus_conp)
912 0 : *conp = *minus_conp, *minus_conp = 0;
913 12941435 : if (var)
914 12882729 : *minus_varp = var, var = 0;
915 58706 : else if (*minus_varp)
916 883 : var = *minus_varp, *minus_varp = 0;
917 : }
918 :
919 233966638 : if (*litp
920 233966638 : && TREE_OVERFLOW_P (*litp))
921 20470 : *litp = drop_tree_overflow (*litp);
922 233966638 : if (*minus_litp
923 233966638 : && TREE_OVERFLOW_P (*minus_litp))
924 24 : *minus_litp = drop_tree_overflow (*minus_litp);
925 :
926 233966638 : 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 21327016 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
936 : {
937 21327016 : if (t1 == 0)
938 : {
939 13544194 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
940 : return t2;
941 : }
942 7782822 : 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 4347206 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
949 3424089 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
950 3357746 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
951 : {
952 1691592 : if (code == PLUS_EXPR)
953 : {
954 943576 : if (TREE_CODE (t1) == NEGATE_EXPR)
955 54 : return build2_loc (loc, MINUS_EXPR, type,
956 : fold_convert_loc (loc, type, t2),
957 : fold_convert_loc (loc, type,
958 108 : TREE_OPERAND (t1, 0)));
959 943522 : 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 943521 : else if (integer_zerop (t2))
965 37899 : return fold_convert_loc (loc, type, t1);
966 : }
967 748016 : else if (code == MINUS_EXPR)
968 : {
969 724515 : if (integer_zerop (t2))
970 0 : return fold_convert_loc (loc, type, t1);
971 : }
972 :
973 1653638 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
974 1653638 : fold_convert_loc (loc, type, t2));
975 : }
976 :
977 2655614 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
978 2655614 : 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 2775022350 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
986 : {
987 2775022350 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
988 : return false;
989 2775022350 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
990 : return false;
991 :
992 2775022350 : switch (code)
993 : {
994 : case LSHIFT_EXPR:
995 : case RSHIFT_EXPR:
996 : case LROTATE_EXPR:
997 : case RROTATE_EXPR:
998 : return true;
999 :
1000 2775022350 : default:
1001 2775022350 : break;
1002 : }
1003 :
1004 2775022350 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1005 2775022350 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1006 5550044700 : && 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 1543378921 : 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 1543378921 : wide_int tmp;
1019 1543378921 : *overflow = wi::OVF_NONE;
1020 1543378921 : switch (code)
1021 : {
1022 3728868 : case BIT_IOR_EXPR:
1023 3728868 : res = wi::bit_or (arg1, arg2);
1024 3728868 : break;
1025 :
1026 98788 : case BIT_XOR_EXPR:
1027 98788 : res = wi::bit_xor (arg1, arg2);
1028 98788 : break;
1029 :
1030 24045969 : case BIT_AND_EXPR:
1031 24045969 : res = wi::bit_and (arg1, arg2);
1032 24045969 : break;
1033 :
1034 15058060 : case LSHIFT_EXPR:
1035 15058060 : if (wi::neg_p (arg2))
1036 : return false;
1037 15027692 : res = wi::lshift (arg1, arg2);
1038 15027692 : break;
1039 :
1040 7532243 : case RSHIFT_EXPR:
1041 7532243 : 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 7532047 : res = wi::rshift (arg1, arg2, sign);
1047 7532047 : 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 261421986 : case PLUS_EXPR:
1069 261421986 : res = wi::add (arg1, arg2, sign, overflow);
1070 261421986 : break;
1071 :
1072 75737892 : case MINUS_EXPR:
1073 75737892 : res = wi::sub (arg1, arg2, sign, overflow);
1074 75737892 : break;
1075 :
1076 442802077 : case MULT_EXPR:
1077 442802077 : res = wi::mul (arg1, arg2, sign, overflow);
1078 442802077 : break;
1079 :
1080 5544 : case MULT_HIGHPART_EXPR:
1081 5544 : res = wi::mul_high (arg1, arg2, sign);
1082 5544 : break;
1083 :
1084 365234249 : case TRUNC_DIV_EXPR:
1085 365234249 : case EXACT_DIV_EXPR:
1086 365234249 : if (arg2 == 0)
1087 : return false;
1088 365228302 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1089 365228302 : break;
1090 :
1091 82156196 : case FLOOR_DIV_EXPR:
1092 82156196 : if (arg2 == 0)
1093 : return false;
1094 82156196 : res = wi::div_floor (arg1, arg2, sign, overflow);
1095 82156196 : break;
1096 :
1097 88114567 : case CEIL_DIV_EXPR:
1098 88114567 : if (arg2 == 0)
1099 : return false;
1100 88114567 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1101 88114567 : 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 1380917 : case TRUNC_MOD_EXPR:
1110 1380917 : if (arg2 == 0)
1111 : return false;
1112 1379811 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1113 1379811 : break;
1114 :
1115 70500703 : case FLOOR_MOD_EXPR:
1116 70500703 : if (arg2 == 0)
1117 : return false;
1118 70500703 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1119 70500703 : 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 49063 : case MIN_EXPR:
1134 49063 : res = wi::min (arg1, arg2, sign);
1135 49063 : break;
1136 :
1137 105509774 : case MAX_EXPR:
1138 105509774 : res = wi::max (arg1, arg2, sign);
1139 105509774 : break;
1140 :
1141 : default:
1142 : return false;
1143 : }
1144 : return true;
1145 1543378921 : }
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 1543378921 : 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 1543378921 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1176 :
1177 1543378921 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1178 : {
1179 1543378921 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1180 1543378921 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1181 1543378921 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1182 : return NULL_TREE;
1183 1543341177 : res = wi_res;
1184 1543341177 : return true;
1185 1543379175 : }
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 1543378921 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1250 : int overflowable)
1251 : {
1252 1543378921 : poly_wide_int poly_res;
1253 1543378921 : tree type = TREE_TYPE (arg1);
1254 1543378921 : signop sign = TYPE_SIGN (type);
1255 1543378921 : wi::overflow_type overflow = wi::OVF_NONE;
1256 :
1257 1543378921 : if (!poly_int_tree_p (arg1)
1258 1543378921 : || !poly_int_tree_p (arg2)
1259 3086757842 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1260 : return NULL_TREE;
1261 1543341177 : return force_fit_type (type, poly_res, overflowable,
1262 1543341177 : (((sign == SIGNED || overflowable == -1)
1263 1543341177 : && overflow)
1264 1543341177 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1265 1543378921 : }
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 191107 : 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 3960 : default:
1284 3960 : 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 449154 : 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 449154 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1297 : {
1298 364660 : if (integer_zerop (other_op))
1299 : {
1300 27630 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1301 : return op;
1302 26474 : else if (code == BIT_AND_EXPR)
1303 4235 : 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 269913 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1316 : tree (*elt_const_binop) (enum tree_code, tree, tree))
1317 : {
1318 193825 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1319 455353 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1320 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1321 : {
1322 185440 : tree type = TREE_TYPE (arg1);
1323 185440 : bool step_ok_p;
1324 185440 : if (VECTOR_CST_STEPPED_P (arg1)
1325 185440 : && 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 2718 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1335 182722 : 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 50747 : step_ok_p = distributes_over_addition_p (code, 1);
1344 : else
1345 : /* Similarly in reverse. */
1346 131975 : step_ok_p = distributes_over_addition_p (code, 2);
1347 185440 : tree_vector_builder elts;
1348 185440 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1349 : return NULL_TREE;
1350 185440 : unsigned int count = elts.encoded_nelts ();
1351 718177 : for (unsigned int i = 0; i < count; ++i)
1352 : {
1353 533056 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1354 533056 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1355 :
1356 533056 : 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 533056 : if (elt == NULL_TREE)
1361 319 : return NULL_TREE;
1362 532737 : elts.quick_push (elt);
1363 : }
1364 :
1365 185121 : return elts.build ();
1366 185440 : }
1367 :
1368 84473 : if (TREE_CODE (arg1) == VECTOR_CST
1369 8385 : && TREE_CODE (arg2) == INTEGER_CST)
1370 : {
1371 8385 : tree type = TREE_TYPE (arg1);
1372 8385 : bool step_ok_p = distributes_over_addition_p (code, 1);
1373 8385 : tree_vector_builder elts;
1374 8385 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1375 : return NULL_TREE;
1376 8385 : unsigned int count = elts.encoded_nelts ();
1377 35664 : for (unsigned int i = 0; i < count; ++i)
1378 : {
1379 27366 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1380 :
1381 27366 : 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 27366 : if (elt == NULL_TREE)
1386 87 : return NULL_TREE;
1387 27279 : elts.quick_push (elt);
1388 : }
1389 :
1390 8298 : return elts.build ();
1391 8385 : }
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 225112011 : const_binop (enum tree_code code, tree arg1, tree arg2)
1402 : {
1403 : /* Sanity check for the recursive cases. */
1404 225112011 : if (!arg1 || !arg2)
1405 : return NULL_TREE;
1406 :
1407 225110747 : STRIP_NOPS (arg1);
1408 225110747 : STRIP_NOPS (arg2);
1409 :
1410 225110747 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1411 : {
1412 219266799 : if (code == POINTER_PLUS_EXPR)
1413 106758 : return int_const_binop (PLUS_EXPR,
1414 213516 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1415 :
1416 219160041 : return int_const_binop (code, arg1, arg2);
1417 : }
1418 :
1419 5843948 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1420 : {
1421 5557554 : machine_mode mode;
1422 5557554 : REAL_VALUE_TYPE d1;
1423 5557554 : REAL_VALUE_TYPE d2;
1424 5557554 : REAL_VALUE_TYPE value;
1425 5557554 : REAL_VALUE_TYPE result;
1426 5557554 : bool inexact;
1427 5557554 : tree t, type;
1428 :
1429 : /* The following codes are handled by real_arithmetic. */
1430 5557554 : switch (code)
1431 : {
1432 5557554 : case PLUS_EXPR:
1433 5557554 : case MINUS_EXPR:
1434 5557554 : case MULT_EXPR:
1435 5557554 : case RDIV_EXPR:
1436 5557554 : case MIN_EXPR:
1437 5557554 : case MAX_EXPR:
1438 5557554 : break;
1439 :
1440 : default:
1441 : return NULL_TREE;
1442 : }
1443 :
1444 5557554 : d1 = TREE_REAL_CST (arg1);
1445 5557554 : d2 = TREE_REAL_CST (arg2);
1446 :
1447 5557554 : type = TREE_TYPE (arg1);
1448 5557554 : mode = TYPE_MODE (type);
1449 :
1450 : /* Don't perform operation if we honor signaling NaNs and
1451 : either operand is a signaling NaN. */
1452 5557554 : if (HONOR_SNANS (mode)
1453 5557554 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1454 6967 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1455 : return NULL_TREE;
1456 :
1457 : /* Don't perform operation if it would raise a division
1458 : by zero exception. */
1459 5557521 : if (code == RDIV_EXPR
1460 2422815 : && real_equal (&d2, &dconst0)
1461 5568242 : && (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 5549708 : 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 5549362 : else if (REAL_VALUE_ISNAN (d2))
1475 : {
1476 : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1477 : is off. */
1478 61 : d2.signalling = 0;
1479 61 : t = build_real (type, d2);
1480 61 : return t;
1481 : }
1482 :
1483 5549301 : inexact = real_arithmetic (&value, code, &d1, &d2);
1484 5549301 : 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 5549301 : if (flag_trapping_math
1491 21550894 : && MODE_HAS_NANS (mode)
1492 5529272 : && REAL_VALUE_ISNAN (result)
1493 2583 : && !REAL_VALUE_ISNAN (d1)
1494 5551884 : && !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 5546718 : if (flag_trapping_math
1500 21540904 : && MODE_HAS_INFINITIES (mode)
1501 5526689 : && REAL_VALUE_ISINF (result)
1502 7846 : && !REAL_VALUE_ISINF (d1)
1503 5553896 : && !REAL_VALUE_ISINF (d2))
1504 4895 : return NULL_TREE;
1505 :
1506 : /* Don't constant fold this floating point operation if the
1507 : result may dependent upon the run-time rounding mode and
1508 : flag_rounding_math is set, or if GCC's software emulation
1509 : is unable to accurately represent the result. */
1510 5541823 : if ((flag_rounding_math
1511 37648031 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1512 5541823 : && (inexact || !real_identical (&result, &value)))
1513 : return NULL_TREE;
1514 :
1515 5540716 : t = build_real (type, result);
1516 :
1517 5540716 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1518 5540716 : return t;
1519 : }
1520 :
1521 286394 : if (TREE_CODE (arg1) == FIXED_CST)
1522 : {
1523 0 : FIXED_VALUE_TYPE f1;
1524 0 : FIXED_VALUE_TYPE f2;
1525 0 : FIXED_VALUE_TYPE result;
1526 0 : tree t, type;
1527 0 : bool sat_p;
1528 0 : bool overflow_p;
1529 :
1530 : /* The following codes are handled by fixed_arithmetic. */
1531 0 : switch (code)
1532 : {
1533 0 : case PLUS_EXPR:
1534 0 : case MINUS_EXPR:
1535 0 : case MULT_EXPR:
1536 0 : case TRUNC_DIV_EXPR:
1537 0 : if (TREE_CODE (arg2) != FIXED_CST)
1538 : return NULL_TREE;
1539 0 : f2 = TREE_FIXED_CST (arg2);
1540 0 : break;
1541 :
1542 0 : case LSHIFT_EXPR:
1543 0 : case RSHIFT_EXPR:
1544 0 : {
1545 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1546 0 : return NULL_TREE;
1547 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1548 0 : f2.data.high = w2.elt (1);
1549 0 : f2.data.low = w2.ulow ();
1550 0 : f2.mode = SImode;
1551 : }
1552 0 : break;
1553 :
1554 : default:
1555 : return NULL_TREE;
1556 : }
1557 :
1558 0 : f1 = TREE_FIXED_CST (arg1);
1559 0 : type = TREE_TYPE (arg1);
1560 0 : sat_p = TYPE_SATURATING (type);
1561 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1562 0 : t = build_fixed (type, result);
1563 : /* Propagate overflow flags. */
1564 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1565 0 : TREE_OVERFLOW (t) = 1;
1566 : return t;
1567 : }
1568 :
1569 286394 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1570 : {
1571 11248 : tree type = TREE_TYPE (arg1);
1572 11248 : tree r1 = TREE_REALPART (arg1);
1573 11248 : tree i1 = TREE_IMAGPART (arg1);
1574 11248 : tree r2 = TREE_REALPART (arg2);
1575 11248 : tree i2 = TREE_IMAGPART (arg2);
1576 11248 : tree real, imag;
1577 :
1578 11248 : switch (code)
1579 : {
1580 5319 : case PLUS_EXPR:
1581 5319 : case MINUS_EXPR:
1582 5319 : real = const_binop (code, r1, r2);
1583 5319 : imag = const_binop (code, i1, i2);
1584 5319 : break;
1585 :
1586 3971 : case MULT_EXPR:
1587 3971 : if (COMPLEX_FLOAT_TYPE_P (type))
1588 2819 : return do_mpc_arg2 (arg1, arg2, type,
1589 : /* do_nonfinite= */ folding_initializer,
1590 2819 : mpc_mul);
1591 :
1592 1152 : real = const_binop (MINUS_EXPR,
1593 : const_binop (MULT_EXPR, r1, r2),
1594 : const_binop (MULT_EXPR, i1, i2));
1595 1152 : imag = const_binop (PLUS_EXPR,
1596 : const_binop (MULT_EXPR, r1, i2),
1597 : const_binop (MULT_EXPR, i1, r2));
1598 1152 : break;
1599 :
1600 1704 : case RDIV_EXPR:
1601 1704 : if (COMPLEX_FLOAT_TYPE_P (type))
1602 1704 : return do_mpc_arg2 (arg1, arg2, type,
1603 : /* do_nonfinite= */ folding_initializer,
1604 1704 : mpc_div);
1605 : /* Fallthru. */
1606 254 : case TRUNC_DIV_EXPR:
1607 254 : case CEIL_DIV_EXPR:
1608 254 : case FLOOR_DIV_EXPR:
1609 254 : case ROUND_DIV_EXPR:
1610 254 : if (flag_complex_method == 0)
1611 : {
1612 : /* Keep this algorithm in sync with
1613 : tree-complex.cc:expand_complex_div_straight().
1614 :
1615 : Expand complex division to scalars, straightforward algorithm.
1616 : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1617 : t = br*br + bi*bi
1618 : */
1619 0 : tree magsquared
1620 0 : = const_binop (PLUS_EXPR,
1621 : const_binop (MULT_EXPR, r2, r2),
1622 : const_binop (MULT_EXPR, i2, i2));
1623 0 : tree t1
1624 0 : = const_binop (PLUS_EXPR,
1625 : const_binop (MULT_EXPR, r1, r2),
1626 : const_binop (MULT_EXPR, i1, i2));
1627 0 : tree t2
1628 0 : = const_binop (MINUS_EXPR,
1629 : const_binop (MULT_EXPR, i1, r2),
1630 : const_binop (MULT_EXPR, r1, i2));
1631 :
1632 0 : real = const_binop (code, t1, magsquared);
1633 0 : imag = const_binop (code, t2, magsquared);
1634 : }
1635 : else
1636 : {
1637 : /* Keep this algorithm in sync with
1638 : tree-complex.cc:expand_complex_div_wide().
1639 :
1640 : Expand complex division to scalars, modified algorithm to minimize
1641 : overflow with wide input ranges. */
1642 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1643 : fold_abs_const (r2, TREE_TYPE (type)),
1644 : fold_abs_const (i2, TREE_TYPE (type)));
1645 :
1646 254 : if (integer_nonzerop (compare))
1647 : {
1648 : /* In the TRUE branch, we compute
1649 : ratio = br/bi;
1650 : div = (br * ratio) + bi;
1651 : tr = (ar * ratio) + ai;
1652 : ti = (ai * ratio) - ar;
1653 : tr = tr / div;
1654 : ti = ti / div; */
1655 48 : tree ratio = const_binop (code, r2, i2);
1656 48 : tree div = const_binop (PLUS_EXPR, i2,
1657 : const_binop (MULT_EXPR, r2, ratio));
1658 48 : real = const_binop (MULT_EXPR, r1, ratio);
1659 48 : real = const_binop (PLUS_EXPR, real, i1);
1660 48 : real = const_binop (code, real, div);
1661 :
1662 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1663 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1664 48 : imag = const_binop (code, imag, div);
1665 : }
1666 : else
1667 : {
1668 : /* In the FALSE branch, we compute
1669 : ratio = d/c;
1670 : divisor = (d * ratio) + c;
1671 : tr = (b * ratio) + a;
1672 : ti = b - (a * ratio);
1673 : tr = tr / div;
1674 : ti = ti / div; */
1675 206 : tree ratio = const_binop (code, i2, r2);
1676 206 : tree div = const_binop (PLUS_EXPR, r2,
1677 : const_binop (MULT_EXPR, i2, ratio));
1678 :
1679 206 : real = const_binop (MULT_EXPR, i1, ratio);
1680 206 : real = const_binop (PLUS_EXPR, real, r1);
1681 206 : real = const_binop (code, real, div);
1682 :
1683 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1684 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1685 206 : imag = const_binop (code, imag, div);
1686 : }
1687 : }
1688 : break;
1689 :
1690 : default:
1691 : return NULL_TREE;
1692 : }
1693 :
1694 6725 : if (real && imag)
1695 6567 : return build_complex (type, real, imag);
1696 : }
1697 :
1698 275304 : tree simplified;
1699 275304 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1700 : return simplified;
1701 :
1702 274727 : if (commutative_tree_code (code)
1703 274727 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1704 : return simplified;
1705 :
1706 269913 : return vector_const_binop (code, arg1, arg2, const_binop);
1707 : }
1708 :
1709 : /* Overload that adds a TYPE parameter to be able to dispatch
1710 : to fold_relational_const. */
1711 :
1712 : tree
1713 301040967 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1714 : {
1715 301040967 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1716 83646034 : return fold_relational_const (code, type, arg1, arg2);
1717 :
1718 : /* ??? Until we make the const_binop worker take the type of the
1719 : result as argument put those cases that need it here. */
1720 217394933 : switch (code)
1721 : {
1722 18 : case VEC_SERIES_EXPR:
1723 18 : if (CONSTANT_CLASS_P (arg1)
1724 18 : && CONSTANT_CLASS_P (arg2))
1725 18 : return build_vec_series (type, arg1, arg2);
1726 : return NULL_TREE;
1727 :
1728 269965 : case COMPLEX_EXPR:
1729 269965 : if ((TREE_CODE (arg1) == REAL_CST
1730 259408 : && TREE_CODE (arg2) == REAL_CST)
1731 10562 : || (TREE_CODE (arg1) == INTEGER_CST
1732 10554 : && TREE_CODE (arg2) == INTEGER_CST))
1733 269957 : return build_complex (type, arg1, arg2);
1734 : return NULL_TREE;
1735 :
1736 180675 : case POINTER_DIFF_EXPR:
1737 180675 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1738 : {
1739 360698 : poly_offset_int res = (wi::to_poly_offset (arg1)
1740 180349 : - wi::to_poly_offset (arg2));
1741 180349 : return force_fit_type (type, res, 1,
1742 180349 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1743 : }
1744 : return NULL_TREE;
1745 :
1746 14989 : case VEC_PACK_TRUNC_EXPR:
1747 14989 : case VEC_PACK_FIX_TRUNC_EXPR:
1748 14989 : case VEC_PACK_FLOAT_EXPR:
1749 14989 : {
1750 14989 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1751 :
1752 14989 : if (TREE_CODE (arg1) != VECTOR_CST
1753 14989 : || TREE_CODE (arg2) != VECTOR_CST)
1754 : return NULL_TREE;
1755 :
1756 14989 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1757 : return NULL_TREE;
1758 :
1759 14989 : out_nelts = in_nelts * 2;
1760 14989 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1761 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1762 :
1763 14989 : tree_vector_builder elts (type, out_nelts, 1);
1764 202402 : for (i = 0; i < out_nelts; i++)
1765 : {
1766 172436 : tree elt = (i < in_nelts
1767 172436 : ? VECTOR_CST_ELT (arg1, i)
1768 86212 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1769 173480 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1770 : ? NOP_EXPR
1771 : : code == VEC_PACK_FLOAT_EXPR
1772 1044 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1773 172436 : TREE_TYPE (type), elt);
1774 172436 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1775 12 : return NULL_TREE;
1776 172424 : elts.quick_push (elt);
1777 : }
1778 :
1779 14977 : return elts.build ();
1780 14989 : }
1781 :
1782 206 : case VEC_WIDEN_MULT_LO_EXPR:
1783 206 : case VEC_WIDEN_MULT_HI_EXPR:
1784 206 : case VEC_WIDEN_MULT_EVEN_EXPR:
1785 206 : case VEC_WIDEN_MULT_ODD_EXPR:
1786 206 : {
1787 206 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1788 :
1789 206 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1790 : return NULL_TREE;
1791 :
1792 206 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1793 : return NULL_TREE;
1794 206 : out_nelts = in_nelts / 2;
1795 206 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1796 : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1797 :
1798 206 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1799 : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1800 : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1801 : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1802 : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1803 : scale = 1, ofs = 0;
1804 : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1805 206 : scale = 1, ofs = 1;
1806 :
1807 206 : tree_vector_builder elts (type, out_nelts, 1);
1808 944 : for (out = 0; out < out_nelts; out++)
1809 : {
1810 532 : unsigned int in = (out << scale) + ofs;
1811 532 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1812 : VECTOR_CST_ELT (arg1, in));
1813 532 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1814 : VECTOR_CST_ELT (arg2, in));
1815 :
1816 532 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1817 0 : return NULL_TREE;
1818 532 : tree elt = const_binop (MULT_EXPR, t1, t2);
1819 532 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1820 : return NULL_TREE;
1821 532 : elts.quick_push (elt);
1822 : }
1823 :
1824 206 : return elts.build ();
1825 206 : }
1826 :
1827 216929080 : default:;
1828 : }
1829 :
1830 216929080 : if (TREE_CODE_CLASS (code) != tcc_binary)
1831 : return NULL_TREE;
1832 :
1833 : /* Make sure type and arg0 have the same saturating flag. */
1834 214428300 : gcc_checking_assert (TYPE_SATURATING (type)
1835 : == TYPE_SATURATING (TREE_TYPE (arg1)));
1836 :
1837 214428300 : return const_binop (code, arg1, arg2);
1838 : }
1839 :
1840 : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1841 : Return zero if computing the constants is not possible. */
1842 :
1843 : tree
1844 382102376 : const_unop (enum tree_code code, tree type, tree arg0)
1845 : {
1846 : /* Don't perform the operation, other than NEGATE and ABS, if
1847 : flag_signaling_nans is on and the operand is a signaling NaN. */
1848 382102376 : if (TREE_CODE (arg0) == REAL_CST
1849 11145839 : && HONOR_SNANS (arg0)
1850 17121 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1851 4740 : && code != NEGATE_EXPR
1852 4740 : && code != ABS_EXPR
1853 382107081 : && code != ABSU_EXPR)
1854 : return NULL_TREE;
1855 :
1856 382097671 : switch (code)
1857 : {
1858 286731784 : CASE_CONVERT:
1859 286731784 : case FLOAT_EXPR:
1860 286731784 : case FIX_TRUNC_EXPR:
1861 286731784 : case FIXED_CONVERT_EXPR:
1862 286731784 : return fold_convert_const (code, type, arg0);
1863 :
1864 0 : case ADDR_SPACE_CONVERT_EXPR:
1865 : /* If the source address is 0, and the source address space
1866 : cannot have a valid object at 0, fold to dest type null. */
1867 0 : if (integer_zerop (arg0)
1868 0 : && !(targetm.addr_space.zero_address_valid
1869 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1870 0 : return fold_convert_const (code, type, arg0);
1871 : break;
1872 :
1873 13289758 : case VIEW_CONVERT_EXPR:
1874 13289758 : return fold_view_convert_expr (type, arg0);
1875 :
1876 31877976 : case NEGATE_EXPR:
1877 31877976 : {
1878 : /* Can't call fold_negate_const directly here as that doesn't
1879 : handle all cases and we might not be able to negate some
1880 : constants. */
1881 31877976 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1882 31877976 : if (tem && CONSTANT_CLASS_P (tem))
1883 31731071 : return tem;
1884 : break;
1885 : }
1886 :
1887 40233 : case ABS_EXPR:
1888 40233 : case ABSU_EXPR:
1889 40233 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1890 35532 : return fold_abs_const (arg0, type);
1891 : break;
1892 :
1893 25512 : case CONJ_EXPR:
1894 25512 : if (TREE_CODE (arg0) == COMPLEX_CST)
1895 : {
1896 25509 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1897 25509 : TREE_TYPE (type));
1898 25509 : return build_complex (type, TREE_REALPART (arg0), ipart);
1899 : }
1900 : break;
1901 :
1902 2355163 : case BIT_NOT_EXPR:
1903 2355163 : if (TREE_CODE (arg0) == INTEGER_CST)
1904 2347925 : return fold_not_const (arg0, type);
1905 7238 : else if (POLY_INT_CST_P (arg0))
1906 : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1907 : /* Perform BIT_NOT_EXPR on each element individually. */
1908 7238 : else if (TREE_CODE (arg0) == VECTOR_CST)
1909 : {
1910 6604 : tree elem;
1911 :
1912 : /* This can cope with stepped encodings because ~x == -1 - x. */
1913 6604 : tree_vector_builder elements;
1914 6604 : elements.new_unary_operation (type, arg0, true);
1915 6604 : unsigned int i, count = elements.encoded_nelts ();
1916 25350 : for (i = 0; i < count; ++i)
1917 : {
1918 18746 : elem = VECTOR_CST_ELT (arg0, i);
1919 18746 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1920 18746 : if (elem == NULL_TREE)
1921 : break;
1922 18746 : elements.quick_push (elem);
1923 : }
1924 6604 : if (i == count)
1925 6604 : return elements.build ();
1926 6604 : }
1927 : break;
1928 :
1929 11703290 : case TRUTH_NOT_EXPR:
1930 11703290 : if (TREE_CODE (arg0) == INTEGER_CST)
1931 11335375 : return constant_boolean_node (integer_zerop (arg0), type);
1932 : break;
1933 :
1934 179901 : case REALPART_EXPR:
1935 179901 : if (TREE_CODE (arg0) == COMPLEX_CST)
1936 179700 : return fold_convert (type, TREE_REALPART (arg0));
1937 : break;
1938 :
1939 185698 : case IMAGPART_EXPR:
1940 185698 : if (TREE_CODE (arg0) == COMPLEX_CST)
1941 185510 : return fold_convert (type, TREE_IMAGPART (arg0));
1942 : break;
1943 :
1944 19028 : case VEC_UNPACK_LO_EXPR:
1945 19028 : case VEC_UNPACK_HI_EXPR:
1946 19028 : case VEC_UNPACK_FLOAT_LO_EXPR:
1947 19028 : case VEC_UNPACK_FLOAT_HI_EXPR:
1948 19028 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1949 19028 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1950 19028 : {
1951 19028 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1952 19028 : enum tree_code subcode;
1953 :
1954 19028 : if (TREE_CODE (arg0) != VECTOR_CST)
1955 : return NULL_TREE;
1956 :
1957 19028 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1958 : return NULL_TREE;
1959 19028 : out_nelts = in_nelts / 2;
1960 19028 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1961 :
1962 19028 : unsigned int offset = 0;
1963 19028 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1964 19028 : || code == VEC_UNPACK_FLOAT_LO_EXPR
1965 : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1966 9506 : offset = out_nelts;
1967 :
1968 19028 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1969 : subcode = NOP_EXPR;
1970 7914 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1971 7914 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
1972 : subcode = FLOAT_EXPR;
1973 : else
1974 4 : subcode = FIX_TRUNC_EXPR;
1975 :
1976 19028 : tree_vector_builder elts (type, out_nelts, 1);
1977 119642 : for (i = 0; i < out_nelts; i++)
1978 : {
1979 81586 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1980 81586 : VECTOR_CST_ELT (arg0, i + offset));
1981 81586 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1982 0 : return NULL_TREE;
1983 81586 : elts.quick_push (elt);
1984 : }
1985 :
1986 19028 : return elts.build ();
1987 19028 : }
1988 :
1989 4 : case VEC_DUPLICATE_EXPR:
1990 4 : if (CONSTANT_CLASS_P (arg0))
1991 4 : return build_vector_from_val (type, arg0);
1992 : return NULL_TREE;
1993 :
1994 : default:
1995 : break;
1996 : }
1997 :
1998 : return NULL_TREE;
1999 : }
2000 :
2001 : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2002 : indicates which particular sizetype to create. */
2003 :
2004 : tree
2005 3653137713 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2006 : {
2007 3653137713 : return build_int_cst (sizetype_tab[(int) kind], number);
2008 : }
2009 :
2010 : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2011 : is a tree code. The type of the result is taken from the operands.
2012 : Both must be equivalent integer types, ala int_binop_types_match_p.
2013 : If the operands are constant, so is the result. */
2014 :
2015 : tree
2016 2735955716 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2017 : {
2018 2735955716 : tree type = TREE_TYPE (arg0);
2019 :
2020 2735955716 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2021 : return error_mark_node;
2022 :
2023 2735955716 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2024 : TREE_TYPE (arg1)));
2025 :
2026 : /* Handle the special case of two poly_int constants faster. */
2027 2735955716 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2028 : {
2029 : /* And some specific cases even faster than that. */
2030 2703113088 : if (code == PLUS_EXPR)
2031 : {
2032 1249867914 : if (integer_zerop (arg0)
2033 1249867914 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2034 : return arg1;
2035 319175497 : if (integer_zerop (arg1)
2036 319175497 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2037 : return arg0;
2038 : }
2039 1453245174 : else if (code == MINUS_EXPR)
2040 : {
2041 123525242 : if (integer_zerop (arg1)
2042 123525242 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2043 : return arg0;
2044 : }
2045 1329719932 : else if (code == MULT_EXPR)
2046 : {
2047 627161647 : if (integer_onep (arg0)
2048 627161647 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2049 : return arg1;
2050 : }
2051 :
2052 : /* Handle general case of two integer constants. For sizetype
2053 : constant calculations we always want to know about overflow,
2054 : even in the unsigned case. */
2055 1301477816 : tree res = int_const_binop (code, arg0, arg1, -1);
2056 1301477816 : if (res != NULL_TREE)
2057 : return res;
2058 : }
2059 :
2060 32842628 : return fold_build2_loc (loc, code, type, arg0, arg1);
2061 : }
2062 :
2063 : /* Given two values, either both of sizetype or both of bitsizetype,
2064 : compute the difference between the two values. Return the value
2065 : in signed type corresponding to the type of the operands. */
2066 :
2067 : tree
2068 39066634 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2069 : {
2070 39066634 : tree type = TREE_TYPE (arg0);
2071 39066634 : tree ctype;
2072 :
2073 39066634 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2074 : TREE_TYPE (arg1)));
2075 :
2076 : /* If the type is already signed, just do the simple thing. */
2077 39066634 : if (!TYPE_UNSIGNED (type))
2078 10494305 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2079 :
2080 28572329 : if (type == sizetype)
2081 28572329 : ctype = ssizetype;
2082 0 : else if (type == bitsizetype)
2083 0 : ctype = sbitsizetype;
2084 : else
2085 0 : ctype = signed_type_for (type);
2086 :
2087 : /* If either operand is not a constant, do the conversions to the signed
2088 : type and subtract. The hardware will do the right thing with any
2089 : overflow in the subtraction. */
2090 28572329 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091 17598 : return size_binop_loc (loc, MINUS_EXPR,
2092 : fold_convert_loc (loc, ctype, arg0),
2093 17598 : fold_convert_loc (loc, ctype, arg1));
2094 :
2095 : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2096 : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2097 : overflow) and negate (which can't either). Special-case a result
2098 : of zero while we're here. */
2099 28554731 : if (tree_int_cst_equal (arg0, arg1))
2100 25258592 : return build_int_cst (ctype, 0);
2101 3296139 : else if (tree_int_cst_lt (arg1, arg0))
2102 2158399 : return fold_convert_loc (loc, ctype,
2103 2158399 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2104 : else
2105 1137740 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2106 : fold_convert_loc (loc, ctype,
2107 : size_binop_loc (loc,
2108 : MINUS_EXPR,
2109 : arg1, arg0)));
2110 : }
2111 :
2112 : /* Convert integer constant ARG1 to TYPE, which is an integral or offset
2113 : or pointer type. */
2114 :
2115 : tree
2116 1474207207 : int_const_convert (tree type, const_tree arg1, int overflowable)
2117 : {
2118 : /* Given an integer constant, make new constant with new type,
2119 : appropriately sign-extended or truncated. Use widest_int
2120 : so that any extension is done according ARG1's type. */
2121 1474207207 : tree arg1_type = TREE_TYPE (arg1);
2122 1474207207 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2123 1474207207 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2124 1474207207 : TYPE_SIGN (arg1_type)),
2125 : overflowable,
2126 1474207207 : TREE_OVERFLOW (arg1));
2127 : }
2128 :
2129 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2130 : to an integer type. */
2131 :
2132 : static tree
2133 58671 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2134 : {
2135 58671 : bool overflow = false;
2136 58671 : tree t;
2137 :
2138 : /* The following code implements the floating point to integer
2139 : conversion rules required by the Java Language Specification,
2140 : that IEEE NaNs are mapped to zero and values that overflow
2141 : the target precision saturate, i.e. values greater than
2142 : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2143 : are mapped to INT_MIN. These semantics are allowed by the
2144 : C and C++ standards that simply state that the behavior of
2145 : FP-to-integer conversion is unspecified upon overflow. */
2146 :
2147 58671 : wide_int val;
2148 58671 : REAL_VALUE_TYPE r;
2149 58671 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2150 :
2151 58671 : switch (code)
2152 : {
2153 58671 : case FIX_TRUNC_EXPR:
2154 58671 : real_trunc (&r, VOIDmode, &x);
2155 58671 : break;
2156 :
2157 0 : default:
2158 0 : gcc_unreachable ();
2159 : }
2160 :
2161 : /* If R is NaN, return zero and show we have an overflow. */
2162 58671 : if (REAL_VALUE_ISNAN (r))
2163 : {
2164 3638 : overflow = true;
2165 3638 : val = wi::zero (TYPE_PRECISION (type));
2166 : }
2167 :
2168 : /* See if R is less than the lower bound or greater than the
2169 : upper bound. */
2170 :
2171 58671 : if (! overflow)
2172 : {
2173 55033 : tree lt = TYPE_MIN_VALUE (type);
2174 55033 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2175 55033 : if (real_less (&r, &l))
2176 : {
2177 1974 : overflow = true;
2178 1974 : val = wi::to_wide (lt);
2179 : }
2180 : }
2181 :
2182 58671 : if (! overflow)
2183 : {
2184 53059 : tree ut = TYPE_MAX_VALUE (type);
2185 53059 : if (ut)
2186 : {
2187 53059 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2188 53059 : if (real_less (&u, &r))
2189 : {
2190 1921 : overflow = true;
2191 1921 : val = wi::to_wide (ut);
2192 : }
2193 : }
2194 : }
2195 :
2196 58671 : if (! overflow)
2197 51140 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2198 :
2199 : /* According to IEEE standard, for conversions from floating point to
2200 : integer. When a NaN or infinite operand cannot be represented in the
2201 : destination format and this cannot otherwise be indicated, the invalid
2202 : operation exception shall be signaled. When a numeric operand would
2203 : convert to an integer outside the range of the destination format, the
2204 : invalid operation exception shall be signaled if this situation cannot
2205 : otherwise be indicated. */
2206 58671 : if (!flag_trapping_math || !overflow)
2207 51394 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2208 : else
2209 : t = NULL_TREE;
2210 :
2211 58671 : return t;
2212 58671 : }
2213 :
2214 : /* A subroutine of fold_convert_const handling conversions of a
2215 : FIXED_CST to an integer type. */
2216 :
2217 : static tree
2218 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2219 : {
2220 0 : tree t;
2221 0 : double_int temp, temp_trunc;
2222 0 : scalar_mode mode;
2223 :
2224 : /* Right shift FIXED_CST to temp by fbit. */
2225 0 : temp = TREE_FIXED_CST (arg1).data;
2226 0 : mode = TREE_FIXED_CST (arg1).mode;
2227 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2228 : {
2229 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2230 : HOST_BITS_PER_DOUBLE_INT,
2231 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2232 :
2233 : /* Left shift temp to temp_trunc by fbit. */
2234 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2235 : HOST_BITS_PER_DOUBLE_INT,
2236 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2237 : }
2238 : else
2239 : {
2240 0 : temp = double_int_zero;
2241 0 : temp_trunc = double_int_zero;
2242 : }
2243 :
2244 : /* If FIXED_CST is negative, we need to round the value toward 0.
2245 : By checking if the fractional bits are not zero to add 1 to temp. */
2246 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2247 0 : && temp_trunc.is_negative ()
2248 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2249 0 : temp += double_int_one;
2250 :
2251 : /* Given a fixed-point constant, make new constant with new type,
2252 : appropriately sign-extended or truncated. */
2253 0 : t = force_fit_type (type, temp, -1,
2254 0 : (temp.is_negative ()
2255 0 : && (TYPE_UNSIGNED (type)
2256 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2257 0 : | TREE_OVERFLOW (arg1));
2258 :
2259 0 : return t;
2260 : }
2261 :
2262 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2263 : to another floating point type. */
2264 :
2265 : static tree
2266 2257117 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2267 : {
2268 2257117 : REAL_VALUE_TYPE value;
2269 2257117 : tree t;
2270 :
2271 : /* If the underlying modes are the same, simply treat it as
2272 : copy and rebuild with TREE_REAL_CST information and the
2273 : given type. */
2274 2257117 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2275 : {
2276 99302 : t = build_real (type, TREE_REAL_CST (arg1));
2277 99302 : return t;
2278 : }
2279 :
2280 : /* Don't perform the operation if flag_signaling_nans is on
2281 : and the operand is a signaling NaN. */
2282 2157815 : if (HONOR_SNANS (arg1)
2283 2159697 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2284 : return NULL_TREE;
2285 :
2286 : /* With flag_rounding_math we should respect the current rounding mode
2287 : unless the conversion is exact. */
2288 2157815 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2289 2158471 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2290 509 : return NULL_TREE;
2291 :
2292 2157306 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2293 2157306 : t = build_real (type, value);
2294 :
2295 : /* If converting an infinity or NAN to a representation that doesn't
2296 : have one, set the overflow bit so that we can produce some kind of
2297 : error message at the appropriate point if necessary. It's not the
2298 : most user-friendly message, but it's better than nothing. */
2299 2157306 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2300 2298341 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2301 0 : TREE_OVERFLOW (t) = 1;
2302 2157306 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2303 2294066 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2304 0 : TREE_OVERFLOW (t) = 1;
2305 : /* Regular overflow, conversion produced an infinity in a mode that
2306 : can't represent them. */
2307 10783203 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2308 0 : && REAL_VALUE_ISINF (value)
2309 2157306 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2310 0 : TREE_OVERFLOW (t) = 1;
2311 : else
2312 2157306 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2313 : return t;
2314 : }
2315 :
2316 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2317 : to a floating point type. */
2318 :
2319 : static tree
2320 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2321 : {
2322 0 : REAL_VALUE_TYPE value;
2323 0 : tree t;
2324 :
2325 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2326 0 : &TREE_FIXED_CST (arg1));
2327 0 : t = build_real (type, value);
2328 :
2329 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2330 0 : return t;
2331 : }
2332 :
2333 : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2334 : to another fixed-point type. */
2335 :
2336 : static tree
2337 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2338 : {
2339 0 : FIXED_VALUE_TYPE value;
2340 0 : tree t;
2341 0 : bool overflow_p;
2342 :
2343 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2344 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2345 0 : t = build_fixed (type, value);
2346 :
2347 : /* Propagate overflow flags. */
2348 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2349 0 : TREE_OVERFLOW (t) = 1;
2350 0 : return t;
2351 : }
2352 :
2353 : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2354 : to a fixed-point type. */
2355 :
2356 : static tree
2357 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2358 : {
2359 0 : FIXED_VALUE_TYPE value;
2360 0 : tree t;
2361 0 : bool overflow_p;
2362 0 : double_int di;
2363 :
2364 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2365 :
2366 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2367 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2368 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2369 : else
2370 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2371 :
2372 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2373 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2374 0 : TYPE_SATURATING (type));
2375 0 : t = build_fixed (type, value);
2376 :
2377 : /* Propagate overflow flags. */
2378 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2379 0 : TREE_OVERFLOW (t) = 1;
2380 0 : return t;
2381 : }
2382 :
2383 : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2384 : to a fixed-point type. */
2385 :
2386 : static tree
2387 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2388 : {
2389 0 : FIXED_VALUE_TYPE value;
2390 0 : tree t;
2391 0 : bool overflow_p;
2392 :
2393 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2394 0 : &TREE_REAL_CST (arg1),
2395 0 : TYPE_SATURATING (type));
2396 0 : t = build_fixed (type, value);
2397 :
2398 : /* Propagate overflow flags. */
2399 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2400 0 : TREE_OVERFLOW (t) = 1;
2401 0 : return t;
2402 : }
2403 :
2404 : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2405 : type TYPE. If no simplification can be done return NULL_TREE. */
2406 :
2407 : static tree
2408 1539391438 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2409 : {
2410 1539391438 : tree arg_type = TREE_TYPE (arg1);
2411 1539391438 : if (arg_type == type)
2412 : return arg1;
2413 :
2414 : /* We can't widen types, since the runtime value could overflow the
2415 : original type before being extended to the new type. */
2416 1527900025 : if (POLY_INT_CST_P (arg1)
2417 : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2418 : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2419 : return build_poly_int_cst (type,
2420 : poly_wide_int::from (poly_int_cst_value (arg1),
2421 : TYPE_PRECISION (type),
2422 : TYPE_SIGN (arg_type)));
2423 :
2424 1527900025 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2425 : || TREE_CODE (type) == OFFSET_TYPE)
2426 : {
2427 1496315415 : if (TREE_CODE (arg1) == INTEGER_CST)
2428 1474207207 : return int_const_convert (type, arg1, !POINTER_TYPE_P (arg_type));
2429 22108208 : else if (TREE_CODE (arg1) == REAL_CST)
2430 58671 : return fold_convert_const_int_from_real (code, type, arg1);
2431 22049537 : else if (TREE_CODE (arg1) == FIXED_CST)
2432 0 : return fold_convert_const_int_from_fixed (type, arg1);
2433 : }
2434 : else if (SCALAR_FLOAT_TYPE_P (type))
2435 : {
2436 31527465 : if (TREE_CODE (arg1) == INTEGER_CST)
2437 : {
2438 24230236 : tree res = build_real_from_int_cst (type, arg1);
2439 : /* Avoid the folding if flag_rounding_math is on and the
2440 : conversion is not exact. */
2441 24230236 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2442 : {
2443 2914 : bool fail = false;
2444 5828 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2445 2914 : TYPE_PRECISION (TREE_TYPE (arg1)));
2446 2914 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2447 1755 : return NULL_TREE;
2448 2914 : }
2449 : return res;
2450 : }
2451 7297229 : else if (TREE_CODE (arg1) == REAL_CST)
2452 2257117 : return fold_convert_const_real_from_real (type, arg1);
2453 5040112 : else if (TREE_CODE (arg1) == FIXED_CST)
2454 0 : return fold_convert_const_real_from_fixed (type, arg1);
2455 : }
2456 : else if (FIXED_POINT_TYPE_P (type))
2457 : {
2458 0 : if (TREE_CODE (arg1) == FIXED_CST)
2459 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2460 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2461 0 : return fold_convert_const_fixed_from_int (type, arg1);
2462 0 : else if (TREE_CODE (arg1) == REAL_CST)
2463 0 : return fold_convert_const_fixed_from_real (type, arg1);
2464 : }
2465 : else if (VECTOR_TYPE_P (type))
2466 : {
2467 4818 : if (TREE_CODE (arg1) == VECTOR_CST
2468 4818 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2469 : {
2470 4818 : tree elttype = TREE_TYPE (type);
2471 4818 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2472 : /* We can't handle steps directly when extending, since the
2473 : values need to wrap at the original precision first. */
2474 4818 : bool step_ok_p
2475 4818 : = (INTEGRAL_TYPE_P (elttype)
2476 329 : && INTEGRAL_TYPE_P (arg1_elttype)
2477 5087 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2478 4818 : tree_vector_builder v;
2479 4818 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2480 : return NULL_TREE;
2481 4818 : unsigned int len = v.encoded_nelts ();
2482 28262 : for (unsigned int i = 0; i < len; ++i)
2483 : {
2484 23444 : tree elt = VECTOR_CST_ELT (arg1, i);
2485 23444 : tree cvt = fold_convert_const (code, elttype, elt);
2486 23444 : if (cvt == NULL_TREE)
2487 0 : return NULL_TREE;
2488 23444 : v.quick_push (cvt);
2489 : }
2490 4818 : return v.build ();
2491 4818 : }
2492 : }
2493 12257 : else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
2494 12257 : return build_zero_cst (type);
2495 : return NULL_TREE;
2496 : }
2497 :
2498 : /* Construct a vector of zero elements of vector type TYPE. */
2499 :
2500 : static tree
2501 17487 : build_zero_vector (tree type)
2502 : {
2503 17487 : tree t;
2504 :
2505 17487 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2506 17487 : return build_vector_from_val (type, t);
2507 : }
2508 :
2509 : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2510 :
2511 : bool
2512 4400 : fold_convertible_p (const_tree type, const_tree arg)
2513 : {
2514 4400 : const_tree orig = TREE_TYPE (arg);
2515 :
2516 4400 : if (type == orig)
2517 : return true;
2518 :
2519 4400 : if (TREE_CODE (arg) == ERROR_MARK
2520 4400 : || TREE_CODE (type) == ERROR_MARK
2521 4400 : || TREE_CODE (orig) == ERROR_MARK)
2522 : return false;
2523 :
2524 4400 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2525 : return true;
2526 :
2527 4400 : switch (TREE_CODE (type))
2528 : {
2529 3820 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2530 3820 : case POINTER_TYPE: case REFERENCE_TYPE:
2531 3820 : case OFFSET_TYPE:
2532 3820 : return (INTEGRAL_TYPE_P (orig)
2533 386 : || (POINTER_TYPE_P (orig)
2534 251 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2535 3955 : || TREE_CODE (orig) == OFFSET_TYPE);
2536 :
2537 130 : case REAL_TYPE:
2538 130 : case FIXED_POINT_TYPE:
2539 130 : case VOID_TYPE:
2540 130 : return TREE_CODE (type) == TREE_CODE (orig);
2541 :
2542 209 : case VECTOR_TYPE:
2543 209 : return (VECTOR_TYPE_P (orig)
2544 322 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2545 : TYPE_VECTOR_SUBPARTS (orig))
2546 226 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2547 :
2548 : default:
2549 : return false;
2550 : }
2551 : }
2552 :
2553 : /* Convert expression ARG to type TYPE. Used by the middle-end for
2554 : simple conversions in preference to calling the front-end's convert. */
2555 :
2556 : tree
2557 2265705032 : fold_convert_loc (location_t loc, tree type, tree arg)
2558 : {
2559 2265705032 : tree orig = TREE_TYPE (arg);
2560 2265705032 : tree tem;
2561 :
2562 2265705032 : if (type == orig)
2563 : return arg;
2564 :
2565 1527620627 : if (TREE_CODE (arg) == ERROR_MARK
2566 1527619587 : || TREE_CODE (type) == ERROR_MARK
2567 1527619586 : || TREE_CODE (orig) == ERROR_MARK)
2568 1041 : return error_mark_node;
2569 :
2570 1527619586 : switch (TREE_CODE (type))
2571 : {
2572 122865446 : case POINTER_TYPE:
2573 122865446 : case REFERENCE_TYPE:
2574 : /* Handle conversions between pointers to different address spaces. */
2575 122865446 : if (POINTER_TYPE_P (orig)
2576 122865446 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2577 103595352 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2578 124 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2579 : /* fall through */
2580 :
2581 1495094536 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2582 1495094536 : case OFFSET_TYPE: case BITINT_TYPE:
2583 1495094536 : if (TREE_CODE (arg) == INTEGER_CST)
2584 : {
2585 1252183773 : tem = fold_convert_const (NOP_EXPR, type, arg);
2586 1252183773 : if (tem != NULL_TREE)
2587 : return tem;
2588 : }
2589 242910763 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2590 2548 : || TREE_CODE (orig) == OFFSET_TYPE)
2591 242910763 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2592 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2593 0 : return fold_convert_loc (loc, type,
2594 : fold_build1_loc (loc, REALPART_EXPR,
2595 0 : TREE_TYPE (orig), arg));
2596 0 : gcc_assert (VECTOR_TYPE_P (orig)
2597 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2598 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2599 :
2600 558209 : case REAL_TYPE:
2601 558209 : if (TREE_CODE (arg) == INTEGER_CST)
2602 : {
2603 58065 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2604 58065 : if (tem != NULL_TREE)
2605 : return tem;
2606 : }
2607 500144 : else if (TREE_CODE (arg) == REAL_CST)
2608 : {
2609 121799 : tem = fold_convert_const (NOP_EXPR, type, arg);
2610 121799 : if (tem != NULL_TREE)
2611 : return tem;
2612 : }
2613 378345 : else if (TREE_CODE (arg) == FIXED_CST)
2614 : {
2615 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2616 0 : if (tem != NULL_TREE)
2617 : return tem;
2618 : }
2619 :
2620 378347 : switch (TREE_CODE (orig))
2621 : {
2622 721 : case INTEGER_TYPE: case BITINT_TYPE:
2623 721 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2624 721 : case POINTER_TYPE: case REFERENCE_TYPE:
2625 721 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2626 :
2627 377626 : case REAL_TYPE:
2628 377626 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2629 :
2630 0 : case FIXED_POINT_TYPE:
2631 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2632 :
2633 0 : case COMPLEX_TYPE:
2634 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2635 0 : return fold_convert_loc (loc, type, tem);
2636 :
2637 0 : default:
2638 0 : gcc_unreachable ();
2639 : }
2640 :
2641 0 : case FIXED_POINT_TYPE:
2642 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2643 0 : || TREE_CODE (arg) == REAL_CST)
2644 : {
2645 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2646 0 : if (tem != NULL_TREE)
2647 0 : goto fold_convert_exit;
2648 : }
2649 :
2650 0 : switch (TREE_CODE (orig))
2651 : {
2652 0 : case FIXED_POINT_TYPE:
2653 0 : case INTEGER_TYPE:
2654 0 : case ENUMERAL_TYPE:
2655 0 : case BOOLEAN_TYPE:
2656 0 : case REAL_TYPE:
2657 0 : case BITINT_TYPE:
2658 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2659 :
2660 0 : case COMPLEX_TYPE:
2661 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2662 0 : return fold_convert_loc (loc, type, tem);
2663 :
2664 0 : default:
2665 0 : gcc_unreachable ();
2666 : }
2667 :
2668 2269 : case COMPLEX_TYPE:
2669 2269 : switch (TREE_CODE (orig))
2670 : {
2671 584 : case INTEGER_TYPE: case BITINT_TYPE:
2672 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2673 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2674 584 : case REAL_TYPE:
2675 584 : case FIXED_POINT_TYPE:
2676 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2677 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2678 584 : fold_convert_loc (loc, TREE_TYPE (type),
2679 584 : integer_zero_node));
2680 1685 : case COMPLEX_TYPE:
2681 1685 : {
2682 1685 : tree rpart, ipart;
2683 :
2684 1685 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2685 : {
2686 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2687 1534 : TREE_OPERAND (arg, 0));
2688 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2689 1534 : TREE_OPERAND (arg, 1));
2690 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2691 : }
2692 :
2693 151 : arg = save_expr (arg);
2694 151 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2695 151 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2696 151 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2697 151 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2698 151 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2699 : }
2700 :
2701 0 : default:
2702 0 : gcc_unreachable ();
2703 : }
2704 :
2705 31849363 : case VECTOR_TYPE:
2706 31849363 : if (integer_zerop (arg))
2707 17487 : return build_zero_vector (type);
2708 31831876 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2709 31831876 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2710 : || VECTOR_TYPE_P (orig));
2711 31831876 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2712 :
2713 111290 : case VOID_TYPE:
2714 111290 : tem = fold_ignored_result (arg);
2715 111290 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2716 :
2717 63 : case NULLPTR_TYPE:
2718 63 : if (integer_zerop (arg))
2719 17 : return build_zero_cst (type);
2720 : /* FALLTHRU */
2721 3778 : default:
2722 3778 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2723 3778 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2724 0 : gcc_unreachable ();
2725 : }
2726 0 : fold_convert_exit:
2727 0 : tem = protected_set_expr_location_unshare (tem, loc);
2728 0 : return tem;
2729 : }
2730 :
2731 : /* Return false if expr can be assumed not to be an lvalue, true
2732 : otherwise. */
2733 :
2734 : static bool
2735 54181525 : maybe_lvalue_p (const_tree x)
2736 : {
2737 : /* We only need to wrap lvalue tree codes. */
2738 54181525 : switch (TREE_CODE (x))
2739 : {
2740 : case VAR_DECL:
2741 : case PARM_DECL:
2742 : case RESULT_DECL:
2743 : case LABEL_DECL:
2744 : case FUNCTION_DECL:
2745 : case SSA_NAME:
2746 : case COMPOUND_LITERAL_EXPR:
2747 :
2748 : case COMPONENT_REF:
2749 : case MEM_REF:
2750 : case INDIRECT_REF:
2751 : case ARRAY_REF:
2752 : case ARRAY_RANGE_REF:
2753 : case BIT_FIELD_REF:
2754 : case OBJ_TYPE_REF:
2755 :
2756 : case REALPART_EXPR:
2757 : case IMAGPART_EXPR:
2758 : case PREINCREMENT_EXPR:
2759 : case PREDECREMENT_EXPR:
2760 : case SAVE_EXPR:
2761 : case TRY_CATCH_EXPR:
2762 : case WITH_CLEANUP_EXPR:
2763 : case COMPOUND_EXPR:
2764 : case MODIFY_EXPR:
2765 : case TARGET_EXPR:
2766 : case COND_EXPR:
2767 : case BIND_EXPR:
2768 : case VIEW_CONVERT_EXPR:
2769 : break;
2770 :
2771 39835747 : default:
2772 : /* Assume the worst for front-end tree codes. */
2773 39835747 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2774 : break;
2775 : return false;
2776 : }
2777 :
2778 14380799 : return true;
2779 : }
2780 :
2781 : /* Return an expr equal to X but certainly not valid as an lvalue. */
2782 :
2783 : tree
2784 47053169 : non_lvalue_loc (location_t loc, tree x)
2785 : {
2786 : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2787 : us. */
2788 47053169 : if (in_gimple_form)
2789 : return x;
2790 :
2791 10918197 : if (! maybe_lvalue_p (x))
2792 : return x;
2793 2596706 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2794 : }
2795 :
2796 : /* Given a tree comparison code, return the code that is the logical inverse.
2797 : It is generally not safe to do this for floating-point comparisons, except
2798 : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2799 : ERROR_MARK in this case. */
2800 :
2801 : enum tree_code
2802 132369894 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2803 : {
2804 132369894 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2805 1054034 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2806 : return ERROR_MARK;
2807 :
2808 131567374 : switch (code)
2809 : {
2810 : case EQ_EXPR:
2811 : return NE_EXPR;
2812 57979479 : case NE_EXPR:
2813 57979479 : return EQ_EXPR;
2814 12364814 : case GT_EXPR:
2815 12364814 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2816 18107638 : case GE_EXPR:
2817 18107638 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2818 8157217 : case LT_EXPR:
2819 8157217 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2820 8171959 : case LE_EXPR:
2821 8171959 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2822 261 : case LTGT_EXPR:
2823 261 : return UNEQ_EXPR;
2824 292 : case UNEQ_EXPR:
2825 292 : return LTGT_EXPR;
2826 : case UNGT_EXPR:
2827 : return LE_EXPR;
2828 : case UNGE_EXPR:
2829 : return LT_EXPR;
2830 : case UNLT_EXPR:
2831 : return GE_EXPR;
2832 : case UNLE_EXPR:
2833 : return GT_EXPR;
2834 223336 : case ORDERED_EXPR:
2835 223336 : return UNORDERED_EXPR;
2836 57036 : case UNORDERED_EXPR:
2837 57036 : return ORDERED_EXPR;
2838 0 : default:
2839 0 : gcc_unreachable ();
2840 : }
2841 : }
2842 :
2843 : /* Similar, but return the comparison that results if the operands are
2844 : swapped. This is safe for floating-point. */
2845 :
2846 : enum tree_code
2847 162001299 : swap_tree_comparison (enum tree_code code)
2848 : {
2849 162001299 : switch (code)
2850 : {
2851 : case EQ_EXPR:
2852 : case NE_EXPR:
2853 : case ORDERED_EXPR:
2854 : case UNORDERED_EXPR:
2855 : case LTGT_EXPR:
2856 : case UNEQ_EXPR:
2857 : return code;
2858 39539245 : case GT_EXPR:
2859 39539245 : return LT_EXPR;
2860 11226112 : case GE_EXPR:
2861 11226112 : return LE_EXPR;
2862 22139140 : case LT_EXPR:
2863 22139140 : return GT_EXPR;
2864 16967996 : case LE_EXPR:
2865 16967996 : return GE_EXPR;
2866 253165 : case UNGT_EXPR:
2867 253165 : return UNLT_EXPR;
2868 20705 : case UNGE_EXPR:
2869 20705 : return UNLE_EXPR;
2870 376226 : case UNLT_EXPR:
2871 376226 : return UNGT_EXPR;
2872 112056 : case UNLE_EXPR:
2873 112056 : return UNGE_EXPR;
2874 0 : default:
2875 0 : gcc_unreachable ();
2876 : }
2877 : }
2878 :
2879 :
2880 : /* Convert a comparison tree code from an enum tree_code representation
2881 : into a compcode bit-based encoding. This function is the inverse of
2882 : compcode_to_comparison. */
2883 :
2884 : static enum comparison_code
2885 111986 : comparison_to_compcode (enum tree_code code)
2886 : {
2887 111986 : switch (code)
2888 : {
2889 : case LT_EXPR:
2890 : return COMPCODE_LT;
2891 : case EQ_EXPR:
2892 : return COMPCODE_EQ;
2893 : case LE_EXPR:
2894 : return COMPCODE_LE;
2895 : case GT_EXPR:
2896 : return COMPCODE_GT;
2897 : case NE_EXPR:
2898 : return COMPCODE_NE;
2899 : case GE_EXPR:
2900 : return COMPCODE_GE;
2901 : case ORDERED_EXPR:
2902 : return COMPCODE_ORD;
2903 : case UNORDERED_EXPR:
2904 : return COMPCODE_UNORD;
2905 : case UNLT_EXPR:
2906 : return COMPCODE_UNLT;
2907 : case UNEQ_EXPR:
2908 : return COMPCODE_UNEQ;
2909 : case UNLE_EXPR:
2910 : return COMPCODE_UNLE;
2911 : case UNGT_EXPR:
2912 : return COMPCODE_UNGT;
2913 : case LTGT_EXPR:
2914 : return COMPCODE_LTGT;
2915 : case UNGE_EXPR:
2916 : return COMPCODE_UNGE;
2917 0 : default:
2918 0 : gcc_unreachable ();
2919 : }
2920 : }
2921 :
2922 : /* Convert a compcode bit-based encoding of a comparison operator back
2923 : to GCC's enum tree_code representation. This function is the
2924 : inverse of comparison_to_compcode. */
2925 :
2926 : static enum tree_code
2927 24952 : compcode_to_comparison (enum comparison_code code)
2928 : {
2929 24952 : switch (code)
2930 : {
2931 : case COMPCODE_LT:
2932 : return LT_EXPR;
2933 : case COMPCODE_EQ:
2934 : return EQ_EXPR;
2935 : case COMPCODE_LE:
2936 : return LE_EXPR;
2937 : case COMPCODE_GT:
2938 : return GT_EXPR;
2939 : case COMPCODE_NE:
2940 : return NE_EXPR;
2941 : case COMPCODE_GE:
2942 : return GE_EXPR;
2943 : case COMPCODE_ORD:
2944 : return ORDERED_EXPR;
2945 : case COMPCODE_UNORD:
2946 : return UNORDERED_EXPR;
2947 : case COMPCODE_UNLT:
2948 : return UNLT_EXPR;
2949 : case COMPCODE_UNEQ:
2950 : return UNEQ_EXPR;
2951 : case COMPCODE_UNLE:
2952 : return UNLE_EXPR;
2953 : case COMPCODE_UNGT:
2954 : return UNGT_EXPR;
2955 : case COMPCODE_LTGT:
2956 : return LTGT_EXPR;
2957 : case COMPCODE_UNGE:
2958 : return UNGE_EXPR;
2959 0 : default:
2960 0 : gcc_unreachable ();
2961 : }
2962 : }
2963 :
2964 : /* Return true if COND1 tests the opposite condition of COND2. */
2965 :
2966 : bool
2967 1795279 : inverse_conditions_p (const_tree cond1, const_tree cond2)
2968 : {
2969 1795279 : return (COMPARISON_CLASS_P (cond1)
2970 1702212 : && COMPARISON_CLASS_P (cond2)
2971 1690832 : && (invert_tree_comparison
2972 1690832 : (TREE_CODE (cond1),
2973 3381664 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2974 70544 : && operand_equal_p (TREE_OPERAND (cond1, 0),
2975 70544 : TREE_OPERAND (cond2, 0), 0)
2976 1817749 : && operand_equal_p (TREE_OPERAND (cond1, 1),
2977 22470 : TREE_OPERAND (cond2, 1), 0));
2978 : }
2979 :
2980 : /* Return a code for the comparison which is the combination of
2981 : doing the AND or OR (depending on CODE) of the two operations LCODE
2982 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2983 : the possibility of trapping if the mode has NaNs, and return ERROR_MARK
2984 : if this makes the transformation invalid. If the resulting code is
2985 : INTEGER_CST, then *RES will be set to a non-NULL CONSTANT. */
2986 :
2987 : enum tree_code
2988 55993 : combine_comparisons (enum tree_code code, enum tree_code lcode,
2989 : enum tree_code rcode, tree truth_type,
2990 : bool honor_nans, tree *res)
2991 : {
2992 55993 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
2993 55993 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
2994 55993 : int compcode;
2995 55993 : *res = NULL_TREE;
2996 :
2997 55993 : switch (code)
2998 : {
2999 31635 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3000 31635 : case BIT_AND_EXPR:
3001 31635 : compcode = lcompcode & rcompcode;
3002 31635 : break;
3003 :
3004 23146 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3005 23146 : case BIT_IOR_EXPR:
3006 23146 : compcode = lcompcode | rcompcode;
3007 23146 : break;
3008 :
3009 863 : case BIT_XOR_EXPR:
3010 863 : case NE_EXPR:
3011 863 : compcode = lcompcode ^ rcompcode;
3012 863 : break;
3013 :
3014 326 : case EQ_EXPR:
3015 326 : compcode = ~(lcompcode ^ rcompcode);
3016 326 : break;
3017 :
3018 : //`bool0 < bool1` is `!bool0 & bool1`
3019 23 : case LT_EXPR:
3020 23 : compcode = ~lcompcode & rcompcode;
3021 23 : break;
3022 :
3023 : //`bool0 > bool1` is `bool0 & !bool1`
3024 0 : case GT_EXPR:
3025 0 : compcode = lcompcode & ~rcompcode;
3026 0 : break;
3027 :
3028 : // `bool0 <= bool1` as !bool0 | bool1
3029 0 : case LE_EXPR:
3030 0 : compcode = ~lcompcode | rcompcode;
3031 0 : break;
3032 :
3033 : //`bool0 >= bool1` is `bool0 | !bool1`
3034 0 : case GE_EXPR:
3035 0 : compcode = lcompcode | ~rcompcode;
3036 0 : break;
3037 :
3038 : default:
3039 : return ERROR_MARK;
3040 : }
3041 :
3042 55993 : if (!honor_nans)
3043 : {
3044 : /* Eliminate unordered comparisons, as well as LTGT and ORD
3045 : which are not used unless the mode has NaNs. */
3046 38510 : compcode &= ~COMPCODE_UNORD;
3047 38510 : if (compcode == COMPCODE_LTGT)
3048 : compcode = COMPCODE_NE;
3049 35269 : else if (compcode == COMPCODE_ORD)
3050 5960 : compcode = COMPCODE_TRUE;
3051 : }
3052 17483 : else if (flag_trapping_math)
3053 : {
3054 : /* Check that the original operation and the optimized ones will trap
3055 : under the same condition. */
3056 16585 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3057 15283 : && (lcompcode != COMPCODE_EQ)
3058 16585 : && (lcompcode != COMPCODE_ORD);
3059 16585 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3060 15310 : && (rcompcode != COMPCODE_EQ)
3061 16585 : && (rcompcode != COMPCODE_ORD);
3062 33170 : bool trap = (compcode & COMPCODE_UNORD) == 0
3063 15769 : && (compcode != COMPCODE_EQ)
3064 16585 : && (compcode != COMPCODE_ORD);
3065 :
3066 : /* In a short-circuited boolean expression the LHS might be
3067 : such that the RHS, if evaluated, will never trap. For
3068 : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3069 : if neither x nor y is NaN. (This is a mixed blessing: for
3070 : example, the expression above will never trap, hence
3071 : optimizing it to x < y would be invalid). */
3072 756 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3073 16866 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3074 : rtrap = false;
3075 :
3076 : /* Allow combining of `a != b && a < b` since NAN will cause != to be
3077 : always true, and `a < b` will cause a trap. This is trap neutral. */
3078 16585 : if (code == TRUTH_ANDIF_EXPR && lcompcode == COMPCODE_NE && rtrap && trap)
3079 : ;
3080 : /* Likewise of `a == b || a < b` for the same reason. */
3081 16489 : else if (code == TRUTH_ORIF_EXPR && lcompcode == COMPCODE_EQ && rtrap && trap)
3082 : ;
3083 : /* If the comparison was short-circuited, and only the RHS
3084 : trapped, we may now generate a spurious trap. */
3085 16421 : else if (rtrap && !ltrap
3086 0 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3087 : return ERROR_MARK;
3088 :
3089 : /* If we changed the conditions that cause a trap, we lose. */
3090 16421 : else if ((ltrap || rtrap) != trap)
3091 : return ERROR_MARK;
3092 : }
3093 :
3094 40893 : if (compcode == COMPCODE_TRUE || compcode == COMPCODE_FALSE)
3095 : {
3096 15941 : *res = constant_boolean_node (compcode == COMPCODE_TRUE, truth_type);
3097 15941 : return INTEGER_CST;
3098 : }
3099 : else
3100 24952 : return compcode_to_comparison ((enum comparison_code) compcode);
3101 : }
3102 :
3103 : /* Return a tree for the comparison which is the combination of
3104 : doing the AND or OR (depending on CODE) of the two operations LCODE
3105 : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3106 : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3107 : if this makes the transformation invalid. */
3108 :
3109 : tree
3110 39783 : combine_comparisons (location_t loc,
3111 : enum tree_code code, enum tree_code lcode,
3112 : enum tree_code rcode, tree truth_type,
3113 : tree ll_arg, tree lr_arg)
3114 : {
3115 39783 : bool honor_nans = HONOR_NANS (ll_arg);
3116 39783 : tree_code rescode;
3117 39783 : tree res;
3118 39783 : rescode = combine_comparisons (code, lcode, rcode, truth_type,
3119 : honor_nans, &res);
3120 39783 : if (rescode == ERROR_MARK)
3121 : return NULL_TREE;
3122 36251 : if (rescode == INTEGER_CST)
3123 15493 : return res;
3124 :
3125 20758 : return fold_build2_loc (loc, rescode, truth_type, ll_arg, lr_arg);
3126 : }
3127 :
3128 : /* Return nonzero if two operands (typically of the same tree node)
3129 : are necessarily equal. FLAGS modifies behavior as follows:
3130 :
3131 : If OEP_ONLY_CONST is set, only return nonzero for constants.
3132 : This function tests whether the operands are indistinguishable;
3133 : it does not test whether they are equal using C's == operation.
3134 : The distinction is important for IEEE floating point, because
3135 : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3136 : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3137 :
3138 : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3139 : even though it may hold multiple values during a function.
3140 : This is because a GCC tree node guarantees that nothing else is
3141 : executed between the evaluation of its "operands" (which may often
3142 : be evaluated in arbitrary order). Hence if the operands themselves
3143 : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3144 : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3145 : unset means assuming isochronic (or instantaneous) tree equivalence.
3146 : Unless comparing arbitrary expression trees, such as from different
3147 : statements, this flag can usually be left unset.
3148 :
3149 : If OEP_PURE_SAME is set, then pure functions with identical arguments
3150 : are considered the same. It is used when the caller has other ways
3151 : to ensure that global memory is unchanged in between.
3152 :
3153 : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3154 : not values of expressions.
3155 :
3156 : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3157 : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3158 :
3159 : If OEP_BITWISE is set, then require the values to be bitwise identical
3160 : rather than simply numerically equal. Do not take advantage of things
3161 : like math-related flags or undefined behavior; only return true for
3162 : values that are provably bitwise identical in all circumstances.
3163 :
3164 : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3165 : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3166 : rather than just numerically equivalent. The compared expressions must
3167 : however perform the same operations but may do intermediate computations in
3168 : differing signs. Because this comparison ignores any possible UB it cannot
3169 : be used blindly without ensuring that the context you are using it in itself
3170 : doesn't guarantee that there will be no UB. Conditional expressions are
3171 : excluded from this relaxation.
3172 :
3173 : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3174 : differing hashes even for cases where operand_compare::operand_equal_p
3175 : compares equal.
3176 :
3177 : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3178 : any operand with side effect. This is unnecessarily conservative in the
3179 : case we know that arg0 and arg1 are in disjoint code paths (such as in
3180 : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3181 : addresses with TREE_CONSTANT flag set so we know that &var == &var
3182 : even if var is volatile. */
3183 :
3184 : bool
3185 7360122009 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3186 : unsigned int flags)
3187 : {
3188 7360122009 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3189 : }
3190 :
3191 : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3192 : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3193 : expression is being compared under for equality. This means that they can
3194 : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3195 :
3196 : bool
3197 7360864008 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3198 : tree type1, const_tree arg1,
3199 : unsigned int flags)
3200 : {
3201 7360864008 : bool r;
3202 7360864008 : if (verify_hash_value (arg0, arg1, flags, &r))
3203 3100328132 : return r;
3204 :
3205 4260535876 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3206 4260535876 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3207 :
3208 : /* If either is ERROR_MARK, they aren't equal. */
3209 4260535876 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3210 4260535229 : || type0 == error_mark_node
3211 4260535227 : || type1 == error_mark_node)
3212 : return false;
3213 :
3214 : /* Similar, if either does not have a type (like a template id),
3215 : they aren't equal. */
3216 4260535226 : if (!type0 || !type1)
3217 : return false;
3218 :
3219 : /* Bitwise identity makes no sense if the values have different layouts. */
3220 4260530072 : if ((flags & OEP_BITWISE)
3221 4260530072 : && !tree_nop_conversion_p (type0, type1))
3222 : return false;
3223 :
3224 : /* We cannot consider pointers to different address space equal. */
3225 4260530072 : if (POINTER_TYPE_P (type0)
3226 661701318 : && POINTER_TYPE_P (type1)
3227 4828897052 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3228 568366980 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3229 : return false;
3230 :
3231 : /* Check equality of integer constants before bailing out due to
3232 : precision differences. */
3233 4260529825 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3234 : {
3235 : /* Address of INTEGER_CST is not defined; check that we did not forget
3236 : to drop the OEP_ADDRESS_OF flags. */
3237 669580517 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3238 669580517 : return tree_int_cst_equal (arg0, arg1);
3239 : }
3240 :
3241 3590949308 : if ((flags & OEP_ASSUME_WRAPV)
3242 2102118 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3243 : {
3244 794046 : const_tree t_arg0 = arg0;
3245 794046 : const_tree t_arg1 = arg1;
3246 794046 : STRIP_NOPS (arg0);
3247 794046 : STRIP_NOPS (arg1);
3248 : /* Only recurse if the conversion was one that was valid to strip. */
3249 794046 : if (t_arg0 != arg0 || t_arg1 != arg1)
3250 741999 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3251 : }
3252 :
3253 3590207309 : if (!(flags & OEP_ADDRESS_OF))
3254 : {
3255 : /* Check if we are checking an operation where the two's compliment
3256 : bitwise representation of the result is not the same between signed and
3257 : unsigned arithmetic. */
3258 3185334439 : bool enforce_signedness = true;
3259 3185334439 : if (flags & OEP_ASSUME_WRAPV)
3260 : {
3261 1265016 : switch (TREE_CODE (arg0))
3262 : {
3263 : case PLUS_EXPR:
3264 : case MINUS_EXPR:
3265 : case MULT_EXPR:
3266 : case BIT_IOR_EXPR:
3267 : case BIT_XOR_EXPR:
3268 : case BIT_AND_EXPR:
3269 : case BIT_NOT_EXPR:
3270 : case ABS_EXPR:
3271 : CASE_CONVERT:
3272 : case SSA_NAME:
3273 : case INTEGER_CST:
3274 : case VAR_DECL:
3275 : case PARM_DECL:
3276 : case RESULT_DECL:
3277 3185334439 : enforce_signedness = false;
3278 : break;
3279 :
3280 : default:
3281 : break;
3282 : }
3283 : }
3284 :
3285 : /* If both types don't have the same signedness, then we can't consider
3286 : them equal. We must check this before the STRIP_NOPS calls
3287 : because they may change the signedness of the arguments. As pointers
3288 : strictly don't have a signedness, require either two pointers or
3289 : two non-pointers as well. */
3290 3185334439 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3291 3185334439 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3292 147080698 : && enforce_signedness))
3293 : return false;
3294 :
3295 : /* If both types don't have the same precision, then it is not safe
3296 : to strip NOPs. */
3297 2881573521 : if (element_precision (type0) != element_precision (type1))
3298 : return false;
3299 :
3300 2727908131 : STRIP_NOPS (arg0);
3301 2727908131 : STRIP_NOPS (arg1);
3302 :
3303 2727908131 : type0 = TREE_TYPE (arg0);
3304 2727908131 : type1 = TREE_TYPE (arg1);
3305 : }
3306 : #if 0
3307 : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3308 : sanity check once the issue is solved. */
3309 : else
3310 : /* Addresses of conversions and SSA_NAMEs (and many other things)
3311 : are not defined. Check that we did not forget to drop the
3312 : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3313 : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3314 : && TREE_CODE (arg0) != SSA_NAME);
3315 : #endif
3316 :
3317 : /* In case both args are comparisons but with different comparison
3318 : code, try to swap the comparison operands of one arg to produce
3319 : a match and compare that variant. */
3320 3132781001 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3321 1262526233 : && COMPARISON_CLASS_P (arg0)
3322 6850344 : && COMPARISON_CLASS_P (arg1))
3323 : {
3324 5113943 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3325 :
3326 5113943 : if (TREE_CODE (arg0) == swap_code)
3327 2174855 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3328 2174855 : TREE_OPERAND (arg1, 1), flags)
3329 2194610 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3330 19755 : TREE_OPERAND (arg1, 0), flags);
3331 : }
3332 :
3333 3130606146 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3334 : {
3335 : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3336 1260351378 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3337 : ;
3338 1260286722 : else if (flags & OEP_ADDRESS_OF)
3339 : {
3340 : /* If we are interested in comparing addresses ignore
3341 : MEM_REF wrappings of the base that can appear just for
3342 : TBAA reasons. */
3343 49807241 : if (TREE_CODE (arg0) == MEM_REF
3344 8204668 : && DECL_P (arg1)
3345 5792336 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3346 1369116 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3347 50544326 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3348 : return true;
3349 49585137 : else if (TREE_CODE (arg1) == MEM_REF
3350 30938694 : && DECL_P (arg0)
3351 11316251 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3352 2361264 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3353 50183892 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3354 : return true;
3355 : return false;
3356 : }
3357 : else
3358 : return false;
3359 : }
3360 :
3361 : /* When not checking addresses, this is needed for conversions and for
3362 : COMPONENT_REF. Might as well play it safe and always test this. */
3363 1870319424 : if (TREE_CODE (type0) == ERROR_MARK
3364 1870319424 : || TREE_CODE (type1) == ERROR_MARK
3365 3740638848 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3366 26379554 : && !(flags & OEP_ADDRESS_OF)))
3367 : return false;
3368 :
3369 : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3370 : We don't care about side effects in that case because the SAVE_EXPR
3371 : takes care of that for us. In all other cases, two expressions are
3372 : equal if they have no side effects. If we have two identical
3373 : expressions with side effects that should be treated the same due
3374 : to the only side effects being identical SAVE_EXPR's, that will
3375 : be detected in the recursive calls below.
3376 : If we are taking an invariant address of two identical objects
3377 : they are necessarily equal as well. */
3378 332183171 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3379 2198569577 : && (TREE_CODE (arg0) == SAVE_EXPR
3380 332157526 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3381 292395656 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3382 : return true;
3383 :
3384 : /* Next handle constant cases, those for which we can return 1 even
3385 : if ONLY_CONST is set. */
3386 1534359403 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3387 26256796 : switch (TREE_CODE (arg0))
3388 : {
3389 151 : case INTEGER_CST:
3390 151 : return tree_int_cst_equal (arg0, arg1);
3391 :
3392 0 : case FIXED_CST:
3393 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3394 : TREE_FIXED_CST (arg1));
3395 :
3396 3796488 : case REAL_CST:
3397 3796488 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3398 : return true;
3399 :
3400 2755270 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3401 : {
3402 : /* If we do not distinguish between signed and unsigned zero,
3403 : consider them equal. */
3404 15334 : if (real_zerop (arg0) && real_zerop (arg1))
3405 : return true;
3406 : }
3407 : return false;
3408 :
3409 1019257 : case VECTOR_CST:
3410 1019257 : {
3411 1019257 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3412 1019257 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3413 : return false;
3414 :
3415 997505 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3416 997505 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3417 : return false;
3418 :
3419 962229 : unsigned int count = vector_cst_encoded_nelts (arg0);
3420 2306748 : for (unsigned int i = 0; i < count; ++i)
3421 2194370 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3422 1097185 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3423 : return false;
3424 : return true;
3425 : }
3426 :
3427 14139 : case COMPLEX_CST:
3428 14139 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3429 : flags)
3430 14139 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3431 : flags));
3432 :
3433 1022666 : case STRING_CST:
3434 1022666 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3435 1022666 : && ! memcmp (TREE_STRING_POINTER (arg0),
3436 583272 : TREE_STRING_POINTER (arg1),
3437 583272 : TREE_STRING_LENGTH (arg0)));
3438 :
3439 0 : case RAW_DATA_CST:
3440 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3441 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3442 0 : RAW_DATA_POINTER (arg1),
3443 0 : RAW_DATA_LENGTH (arg0)));
3444 :
3445 19264542 : case ADDR_EXPR:
3446 19264542 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3447 19264542 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3448 : flags | OEP_ADDRESS_OF
3449 19264542 : | OEP_MATCH_SIDE_EFFECTS);
3450 183990 : case CONSTRUCTOR:
3451 183990 : {
3452 : /* In GIMPLE empty constructors are allowed in initializers of
3453 : aggregates. */
3454 183990 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3455 : return true;
3456 :
3457 : /* See sem_variable::equals in ipa-icf for a similar approach. */
3458 138145 : if (TREE_CODE (type0) != TREE_CODE (type1))
3459 : return false;
3460 138145 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3461 : {
3462 : /* For arrays, check that the sizes all match. */
3463 264 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3464 264 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3465 264 : || siz0 < 0
3466 528 : || siz0 != int_size_in_bytes (type1))
3467 : return false;
3468 : }
3469 137881 : else if (!types_compatible_p (type0, type1))
3470 : return false;
3471 :
3472 138145 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3473 138145 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3474 414435 : if (vec_safe_length (v0) != vec_safe_length (v1))
3475 : return false;
3476 :
3477 : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3478 : of the CONSTRUCTOR referenced indirectly. */
3479 138145 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3480 :
3481 368631843 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3482 : {
3483 206349 : constructor_elt *c0 = &(*v0)[idx];
3484 206349 : constructor_elt *c1 = &(*v1)[idx];
3485 :
3486 : /* Check that the values are the same... */
3487 206349 : if (c0->value != c1->value
3488 206349 : && !operand_equal_p (c0->value, c1->value, flags))
3489 : return false;
3490 :
3491 : /* ... and that they apply to the same field! */
3492 117785 : if (c0->index != c1->index
3493 117785 : && (TREE_CODE (type0) == ARRAY_TYPE
3494 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3495 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3496 0 : DECL_FIELD_OFFSET (c1->index),
3497 : flags)
3498 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3499 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3500 : flags)))
3501 : return false;
3502 : }
3503 :
3504 : return true;
3505 : }
3506 :
3507 : default:
3508 : break;
3509 : }
3510 :
3511 : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3512 : two instances of undefined behavior will give identical results. */
3513 1509058170 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3514 : return false;
3515 :
3516 : /* Define macros to test an operand from arg0 and arg1 for equality and a
3517 : variant that allows null and views null as being different from any
3518 : non-null value. In the latter case, if either is null, the both
3519 : must be; otherwise, do the normal comparison. */
3520 : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3521 : TREE_OPERAND (arg1, N), flags)
3522 :
3523 : #define OP_SAME_WITH_NULL(N) \
3524 : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3525 : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3526 :
3527 1509058170 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3528 : {
3529 8429155 : case tcc_unary:
3530 : /* Two conversions are equal only if signedness and modes match. */
3531 8429155 : switch (TREE_CODE (arg0))
3532 : {
3533 8058772 : CASE_CONVERT:
3534 8058772 : case FIX_TRUNC_EXPR:
3535 8058772 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3536 : return false;
3537 : break;
3538 : default:
3539 : break;
3540 : }
3541 :
3542 8429134 : return OP_SAME_WITH_NULL (0);
3543 :
3544 :
3545 23098934 : case tcc_comparison:
3546 23098934 : case tcc_binary:
3547 23098934 : if (OP_SAME (0) && OP_SAME (1))
3548 : return true;
3549 :
3550 : /* For commutative ops, allow the other order. */
3551 17043908 : return (commutative_tree_code (TREE_CODE (arg0))
3552 12930171 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3553 12930171 : TREE_OPERAND (arg1, 1), flags)
3554 17277358 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3555 233450 : TREE_OPERAND (arg1, 0), flags));
3556 :
3557 880559355 : case tcc_reference:
3558 : /* If either of the pointer (or reference) expressions we are
3559 : dereferencing contain a side effect, these cannot be equal,
3560 : but their addresses can be. */
3561 880559355 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3562 880559355 : && (TREE_SIDE_EFFECTS (arg0)
3563 812371246 : || TREE_SIDE_EFFECTS (arg1)))
3564 : return false;
3565 :
3566 879988988 : switch (TREE_CODE (arg0))
3567 : {
3568 5598254 : case INDIRECT_REF:
3569 5598254 : if (!(flags & OEP_ADDRESS_OF))
3570 : {
3571 5575699 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3572 : return false;
3573 : /* Verify that the access types are compatible. */
3574 5569374 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3575 : return false;
3576 : }
3577 5523461 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3578 5523461 : return OP_SAME (0);
3579 :
3580 576104 : case IMAGPART_EXPR:
3581 : /* Require the same offset. */
3582 576104 : if (!operand_equal_p (TYPE_SIZE (type0),
3583 576104 : TYPE_SIZE (type1),
3584 : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3585 : return false;
3586 :
3587 : /* Fallthru. */
3588 2310760 : case REALPART_EXPR:
3589 2310760 : case VIEW_CONVERT_EXPR:
3590 2310760 : return OP_SAME (0);
3591 :
3592 87317360 : case TARGET_MEM_REF:
3593 87317360 : case MEM_REF:
3594 87317360 : if (!(flags & OEP_ADDRESS_OF))
3595 : {
3596 : /* Require equal access sizes */
3597 17258744 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3598 17258744 : && (!TYPE_SIZE (type0)
3599 1271163 : || !TYPE_SIZE (type1)
3600 1264890 : || !operand_equal_p (TYPE_SIZE (type0),
3601 1264890 : TYPE_SIZE (type1),
3602 : flags)))
3603 : return false;
3604 : /* Verify that access happens in similar types. */
3605 15982987 : if (!types_compatible_p (type0, type1))
3606 : return false;
3607 : /* Verify that accesses are TBAA compatible. */
3608 15644184 : if (!alias_ptr_types_compatible_p
3609 15644184 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3610 15644184 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3611 14763126 : || (MR_DEPENDENCE_CLIQUE (arg0)
3612 14763126 : != MR_DEPENDENCE_CLIQUE (arg1))
3613 28595303 : || (MR_DEPENDENCE_BASE (arg0)
3614 12951119 : != MR_DEPENDENCE_BASE (arg1)))
3615 : return false;
3616 : /* Verify that alignment is compatible. */
3617 12424609 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3618 : return false;
3619 : }
3620 82267795 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3621 140740110 : return (OP_SAME (0) && OP_SAME (1)
3622 : /* TARGET_MEM_REF require equal extra operands. */
3623 107497668 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3624 539715 : || (OP_SAME_WITH_NULL (2)
3625 280402 : && OP_SAME_WITH_NULL (3)
3626 274762 : && OP_SAME_WITH_NULL (4))));
3627 :
3628 35919769 : case ARRAY_REF:
3629 35919769 : case ARRAY_RANGE_REF:
3630 35919769 : if (!OP_SAME (0))
3631 : return false;
3632 31119334 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3633 : /* Compare the array index by value if it is constant first as we
3634 : may have different types but same value here. */
3635 31119334 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3636 31119334 : TREE_OPERAND (arg1, 1))
3637 28045366 : || OP_SAME (1))
3638 6177994 : && OP_SAME_WITH_NULL (2)
3639 6176674 : && OP_SAME_WITH_NULL (3)
3640 : /* Compare low bound and element size as with OEP_ADDRESS_OF
3641 : we have to account for the offset of the ref. */
3642 40385005 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3643 3088337 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3644 2649 : || (operand_equal_p (array_ref_low_bound
3645 2649 : (const_cast<tree> (arg0)),
3646 : array_ref_low_bound
3647 2649 : (const_cast<tree> (arg1)),
3648 : flags)
3649 2649 : && operand_equal_p (array_ref_element_size
3650 2649 : (const_cast<tree> (arg0)),
3651 : array_ref_element_size
3652 2649 : (const_cast<tree> (arg1)),
3653 : flags))));
3654 :
3655 748207306 : case COMPONENT_REF:
3656 : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3657 : may be NULL when we're called to compare MEM_EXPRs. */
3658 748207306 : if (!OP_SAME_WITH_NULL (0))
3659 : return false;
3660 58817754 : {
3661 58817754 : bool compare_address = flags & OEP_ADDRESS_OF;
3662 :
3663 : /* Most of time we only need to compare FIELD_DECLs for equality.
3664 : However when determining address look into actual offsets.
3665 : These may match for unions and unshared record types. */
3666 58817754 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3667 58817754 : if (!OP_SAME (1))
3668 : {
3669 34032611 : if (compare_address
3670 651813 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3671 : {
3672 649182 : tree field0 = TREE_OPERAND (arg0, 1);
3673 649182 : tree field1 = TREE_OPERAND (arg1, 1);
3674 :
3675 : /* Non-FIELD_DECL operands can appear in C++ templates. */
3676 649182 : if (TREE_CODE (field0) != FIELD_DECL
3677 649182 : || TREE_CODE (field1) != FIELD_DECL)
3678 : return false;
3679 :
3680 649182 : if (!DECL_FIELD_OFFSET (field0)
3681 649182 : || !DECL_FIELD_OFFSET (field1))
3682 3 : return field0 == field1;
3683 :
3684 649179 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3685 649179 : DECL_FIELD_OFFSET (field1), flags)
3686 840483 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3687 191304 : DECL_FIELD_BIT_OFFSET (field1),
3688 : flags))
3689 : return false;
3690 : }
3691 : else
3692 : return false;
3693 : }
3694 : }
3695 24819369 : return OP_SAME_WITH_NULL (2);
3696 :
3697 635347 : case BIT_FIELD_REF:
3698 635347 : if (!OP_SAME (0))
3699 : return false;
3700 369754 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3701 369754 : return OP_SAME (1) && OP_SAME (2);
3702 :
3703 : default:
3704 : return false;
3705 : }
3706 :
3707 59945334 : case tcc_expression:
3708 59945334 : switch (TREE_CODE (arg0))
3709 : {
3710 54569723 : case ADDR_EXPR:
3711 : /* Be sure we pass right ADDRESS_OF flag. */
3712 54569723 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3713 54569723 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3714 54569723 : TREE_OPERAND (arg1, 0),
3715 54569723 : flags | OEP_ADDRESS_OF);
3716 :
3717 613503 : case TRUTH_NOT_EXPR:
3718 613503 : return OP_SAME (0);
3719 :
3720 79787 : case TRUTH_ANDIF_EXPR:
3721 79787 : case TRUTH_ORIF_EXPR:
3722 79787 : return OP_SAME (0) && OP_SAME (1);
3723 :
3724 0 : case WIDEN_MULT_PLUS_EXPR:
3725 0 : case WIDEN_MULT_MINUS_EXPR:
3726 0 : if (!OP_SAME (2))
3727 : return false;
3728 : /* The multiplication operands are commutative. */
3729 : /* FALLTHRU */
3730 :
3731 47877 : case TRUTH_AND_EXPR:
3732 47877 : case TRUTH_OR_EXPR:
3733 47877 : case TRUTH_XOR_EXPR:
3734 47877 : if (OP_SAME (0) && OP_SAME (1))
3735 : return true;
3736 :
3737 : /* Otherwise take into account this is a commutative operation. */
3738 47859 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3739 47859 : TREE_OPERAND (arg1, 1), flags)
3740 47862 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3741 3 : TREE_OPERAND (arg1, 0), flags));
3742 :
3743 216313 : case COND_EXPR:
3744 216313 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3745 : return false;
3746 170485 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3747 170485 : return OP_SAME (0);
3748 :
3749 4 : case BIT_INSERT_EXPR:
3750 : /* BIT_INSERT_EXPR has an implicit operand as the type precision
3751 : of op1. Need to check to make sure they are the same. */
3752 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3753 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3754 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3755 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3756 : return false;
3757 : /* FALLTHRU */
3758 :
3759 371 : case VEC_COND_EXPR:
3760 371 : case DOT_PROD_EXPR:
3761 371 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3762 :
3763 38576 : case MODIFY_EXPR:
3764 38576 : case INIT_EXPR:
3765 38576 : case COMPOUND_EXPR:
3766 38576 : case PREDECREMENT_EXPR:
3767 38576 : case PREINCREMENT_EXPR:
3768 38576 : case POSTDECREMENT_EXPR:
3769 38576 : case POSTINCREMENT_EXPR:
3770 38576 : if (flags & OEP_LEXICOGRAPHIC)
3771 165 : return OP_SAME (0) && OP_SAME (1);
3772 : return false;
3773 :
3774 319115 : case CLEANUP_POINT_EXPR:
3775 319115 : case EXPR_STMT:
3776 319115 : case SAVE_EXPR:
3777 319115 : if (flags & OEP_LEXICOGRAPHIC)
3778 208 : return OP_SAME (0);
3779 : return false;
3780 :
3781 81647 : case OBJ_TYPE_REF:
3782 : /* Virtual table reference. */
3783 163294 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3784 81647 : OBJ_TYPE_REF_EXPR (arg1), flags))
3785 : return false;
3786 15896 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3787 15896 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3788 15896 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3789 : return false;
3790 15896 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3791 15896 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3792 : return false;
3793 15896 : if (virtual_method_call_p (arg0))
3794 : {
3795 15896 : if (!virtual_method_call_p (arg1))
3796 : return false;
3797 15896 : return types_same_for_odr (obj_type_ref_class (arg0),
3798 31792 : obj_type_ref_class (arg1));
3799 : }
3800 : return false;
3801 :
3802 661 : case OMP_ARRAY_SECTION:
3803 661 : return OP_SAME (0) && OP_SAME_WITH_NULL (1) && OP_SAME_WITH_NULL (2);
3804 :
3805 : default:
3806 : return false;
3807 : }
3808 :
3809 3804235 : case tcc_vl_exp:
3810 3804235 : switch (TREE_CODE (arg0))
3811 : {
3812 3804235 : case CALL_EXPR:
3813 3804235 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3814 3804235 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3815 : /* If not both CALL_EXPRs are either internal or normal function
3816 : functions, then they are not equal. */
3817 : return false;
3818 3804235 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3819 : {
3820 : /* If the CALL_EXPRs call different internal functions, then they
3821 : are not equal. */
3822 14 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3823 : return false;
3824 : }
3825 : else
3826 : {
3827 : /* If the CALL_EXPRs call different functions, then they are not
3828 : equal. */
3829 3804221 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3830 : flags))
3831 : return false;
3832 : }
3833 :
3834 : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3835 2224426 : {
3836 2224426 : unsigned int cef = call_expr_flags (arg0);
3837 2224426 : if (flags & OEP_PURE_SAME)
3838 0 : cef &= ECF_CONST | ECF_PURE;
3839 : else
3840 2224426 : cef &= ECF_CONST;
3841 2224426 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3842 : return false;
3843 : }
3844 :
3845 : /* Now see if all the arguments are the same. */
3846 34741 : {
3847 34741 : const_call_expr_arg_iterator iter0, iter1;
3848 34741 : const_tree a0, a1;
3849 69482 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3850 34741 : a1 = first_const_call_expr_arg (arg1, &iter1);
3851 42874 : a0 && a1;
3852 8133 : a0 = next_const_call_expr_arg (&iter0),
3853 8133 : a1 = next_const_call_expr_arg (&iter1))
3854 36240 : if (! operand_equal_p (a0, a1, flags))
3855 : return false;
3856 :
3857 : /* If we get here and both argument lists are exhausted
3858 : then the CALL_EXPRs are equal. */
3859 6634 : return ! (a0 || a1);
3860 : }
3861 : default:
3862 : return false;
3863 : }
3864 :
3865 172079523 : case tcc_declaration:
3866 : /* Consider __builtin_sqrt equal to sqrt. */
3867 172079523 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3868 7048564 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3869 270512 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3870 6436846 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3871 270512 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3872 :
3873 165642677 : if (DECL_P (arg0)
3874 165642677 : && (flags & OEP_DECL_NAME)
3875 35 : && (flags & OEP_LEXICOGRAPHIC))
3876 : {
3877 : /* Consider decls with the same name equal. The caller needs
3878 : to make sure they refer to the same entity (such as a function
3879 : formal parameter). */
3880 35 : tree a0name = DECL_NAME (arg0);
3881 35 : tree a1name = DECL_NAME (arg1);
3882 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3883 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3884 35 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3885 : }
3886 : return false;
3887 :
3888 358499465 : case tcc_exceptional:
3889 358499465 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3890 : {
3891 19803 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3892 : return false;
3893 :
3894 : /* In GIMPLE constructors are used only to build vectors from
3895 : elements. Individual elements in the constructor must be
3896 : indexed in increasing order and form an initial sequence.
3897 :
3898 : We make no effort to compare nonconstant ones in GENERIC. */
3899 19803 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3900 : return false;
3901 :
3902 : /* Be sure that vectors constructed have the same representation.
3903 : We only tested element precision and modes to match.
3904 : Vectors may be BLKmode and thus also check that the number of
3905 : parts match. */
3906 1048 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3907 2096 : TYPE_VECTOR_SUBPARTS (type1)))
3908 : return false;
3909 :
3910 1048 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3911 1048 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3912 1048 : unsigned int len = vec_safe_length (v0);
3913 :
3914 2096 : if (len != vec_safe_length (v1))
3915 : return false;
3916 :
3917 4618 : for (unsigned int i = 0; i < len; i++)
3918 : {
3919 3938 : constructor_elt *c0 = &(*v0)[i];
3920 3938 : constructor_elt *c1 = &(*v1)[i];
3921 :
3922 3938 : if (!operand_equal_p (c0->value, c1->value, flags)
3923 : /* In GIMPLE the indexes can be either NULL or matching i.
3924 : Double check this so we won't get false
3925 : positives for GENERIC. */
3926 3600 : || (c0->index
3927 2684 : && (TREE_CODE (c0->index) != INTEGER_CST
3928 2684 : || compare_tree_int (c0->index, i)))
3929 7538 : || (c1->index
3930 2684 : && (TREE_CODE (c1->index) != INTEGER_CST
3931 2684 : || compare_tree_int (c1->index, i))))
3932 : return false;
3933 : }
3934 : return true;
3935 : }
3936 358479662 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3937 3301 : && (flags & OEP_LEXICOGRAPHIC))
3938 : {
3939 : /* Compare the STATEMENT_LISTs. */
3940 16 : tree_stmt_iterator tsi1, tsi2;
3941 16 : tree body1 = const_cast<tree> (arg0);
3942 16 : tree body2 = const_cast<tree> (arg1);
3943 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3944 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3945 : {
3946 : /* The lists don't have the same number of statements. */
3947 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3948 : return false;
3949 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3950 : return true;
3951 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3952 : flags & (OEP_LEXICOGRAPHIC
3953 : | OEP_NO_HASH_CHECK)))
3954 : return false;
3955 : }
3956 : }
3957 : return false;
3958 :
3959 2641971 : case tcc_statement:
3960 2641971 : switch (TREE_CODE (arg0))
3961 : {
3962 52 : case RETURN_EXPR:
3963 52 : if (flags & OEP_LEXICOGRAPHIC)
3964 52 : return OP_SAME_WITH_NULL (0);
3965 : return false;
3966 4 : case DEBUG_BEGIN_STMT:
3967 4 : if (flags & OEP_LEXICOGRAPHIC)
3968 : return true;
3969 : return false;
3970 : default:
3971 : return false;
3972 : }
3973 :
3974 : default:
3975 : return false;
3976 : }
3977 :
3978 : #undef OP_SAME
3979 : #undef OP_SAME_WITH_NULL
3980 : }
3981 :
3982 : /* Generate a hash value for an expression. This can be used iteratively
3983 : by passing a previous result as the HSTATE argument. */
3984 :
3985 : void
3986 3174750871 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3987 : unsigned int flags)
3988 : {
3989 3174750871 : int i;
3990 3174750871 : enum tree_code code;
3991 3174750871 : enum tree_code_class tclass;
3992 :
3993 3174750871 : if (t == NULL_TREE || t == error_mark_node)
3994 : {
3995 78213935 : hstate.merge_hash (0);
3996 78213935 : return;
3997 : }
3998 :
3999 3096536936 : STRIP_ANY_LOCATION_WRAPPER (t);
4000 :
4001 3096536936 : if (!(flags & OEP_ADDRESS_OF))
4002 2838488136 : STRIP_NOPS (t);
4003 :
4004 3096536936 : code = TREE_CODE (t);
4005 :
4006 3096536936 : switch (code)
4007 : {
4008 : /* Alas, constants aren't shared, so we can't rely on pointer
4009 : identity. */
4010 843 : case VOID_CST:
4011 843 : hstate.merge_hash (0);
4012 843 : return;
4013 887156353 : case INTEGER_CST:
4014 887156353 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4015 1793113959 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4016 905957606 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4017 : return;
4018 15807270 : case REAL_CST:
4019 15807270 : {
4020 15807270 : unsigned int val2;
4021 15807270 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4022 : val2 = rvc_zero;
4023 : else
4024 15586107 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4025 15807270 : hstate.merge_hash (val2);
4026 15807270 : return;
4027 : }
4028 0 : case FIXED_CST:
4029 0 : {
4030 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4031 0 : hstate.merge_hash (val2);
4032 0 : return;
4033 : }
4034 12749865 : case STRING_CST:
4035 12749865 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4036 12749865 : TREE_STRING_LENGTH (t));
4037 12749865 : return;
4038 209 : case RAW_DATA_CST:
4039 209 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4040 209 : RAW_DATA_LENGTH (t));
4041 209 : return;
4042 211380 : case COMPLEX_CST:
4043 211380 : hash_operand (TREE_REALPART (t), hstate, flags);
4044 211380 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4045 211380 : return;
4046 3463913 : case VECTOR_CST:
4047 3463913 : {
4048 3463913 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4049 3463913 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4050 3463913 : unsigned int count = vector_cst_encoded_nelts (t);
4051 14285048 : for (unsigned int i = 0; i < count; ++i)
4052 7357222 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4053 : return;
4054 : }
4055 898605717 : case SSA_NAME:
4056 : /* We can just compare by pointer. */
4057 898605717 : hstate.add_hwi (SSA_NAME_VERSION (t));
4058 898605717 : return;
4059 : case PLACEHOLDER_EXPR:
4060 : /* The node itself doesn't matter. */
4061 : return;
4062 : case BLOCK:
4063 : case OMP_CLAUSE:
4064 : case OMP_NEXT_VARIANT:
4065 : case OMP_TARGET_DEVICE_MATCHES:
4066 : /* Ignore. */
4067 : return;
4068 : case TREE_LIST:
4069 : /* A list of expressions, for a CALL_EXPR or as the elements of a
4070 : VECTOR_CST. */
4071 257618 : for (; t; t = TREE_CHAIN (t))
4072 128809 : hash_operand (TREE_VALUE (t), hstate, flags);
4073 : return;
4074 4967786 : case CONSTRUCTOR:
4075 4967786 : {
4076 4967786 : unsigned HOST_WIDE_INT idx;
4077 4967786 : tree field, value;
4078 4967786 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4079 4967786 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4080 19960355 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4081 : {
4082 : /* In GIMPLE the indexes can be either NULL or matching i. */
4083 14992569 : if (field == NULL_TREE)
4084 1117450 : field = bitsize_int (idx);
4085 14992569 : if (TREE_CODE (field) == FIELD_DECL)
4086 : {
4087 9972293 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4088 9972293 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4089 : }
4090 : else
4091 5020276 : hash_operand (field, hstate, flags);
4092 14992569 : hash_operand (value, hstate, flags);
4093 : }
4094 : return;
4095 : }
4096 182 : case STATEMENT_LIST:
4097 182 : {
4098 182 : tree_stmt_iterator i;
4099 182 : for (i = tsi_start (const_cast<tree> (t));
4100 550 : !tsi_end_p (i); tsi_next (&i))
4101 368 : hash_operand (tsi_stmt (i), hstate, flags);
4102 182 : return;
4103 : }
4104 : case TREE_VEC:
4105 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4106 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4107 : return;
4108 4 : case IDENTIFIER_NODE:
4109 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4110 4 : return;
4111 21483430 : case FUNCTION_DECL:
4112 : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4113 : Otherwise nodes that compare equal according to operand_equal_p might
4114 : get different hash codes. However, don't do this for machine specific
4115 : or front end builtins, since the function code is overloaded in those
4116 : cases. */
4117 21483430 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4118 21483430 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4119 : {
4120 7199673 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4121 7199673 : code = TREE_CODE (t);
4122 : }
4123 : /* FALL THROUGH */
4124 1273444478 : default:
4125 1273444478 : if (POLY_INT_CST_P (t))
4126 : {
4127 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4128 : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4129 : return;
4130 : }
4131 1273444478 : tclass = TREE_CODE_CLASS (code);
4132 :
4133 1273444478 : if (tclass == tcc_declaration)
4134 : {
4135 : /* DECL's have a unique ID */
4136 932602841 : hstate.add_hwi (DECL_UID (t));
4137 : }
4138 340841637 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4139 : {
4140 : /* For comparisons that can be swapped, use the lower
4141 : tree code. */
4142 149205 : enum tree_code ccode = swap_tree_comparison (code);
4143 149205 : if (code < ccode)
4144 66763 : ccode = code;
4145 149205 : hstate.add_object (ccode);
4146 149205 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4147 149205 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4148 : }
4149 340692432 : else if (CONVERT_EXPR_CODE_P (code))
4150 : {
4151 : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4152 : operand_equal_p. */
4153 6598521 : enum tree_code ccode = NOP_EXPR;
4154 6598521 : hstate.add_object (ccode);
4155 :
4156 : /* Don't hash the type, that can lead to having nodes which
4157 : compare equal according to operand_equal_p, but which
4158 : have different hash codes. Make sure to include signedness
4159 : in the hash computation. */
4160 6598521 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4161 6598521 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4162 : }
4163 : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4164 334093911 : else if (code == MEM_REF
4165 80535570 : && (flags & OEP_ADDRESS_OF) != 0
4166 70922409 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4167 14333361 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4168 348210118 : && integer_zerop (TREE_OPERAND (t, 1)))
4169 6303240 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4170 : hstate, flags);
4171 : /* Don't ICE on FE specific trees, or their arguments etc.
4172 : during operand_equal_p hash verification. */
4173 327790671 : else if (!IS_EXPR_CODE_CLASS (tclass))
4174 384 : gcc_assert (flags & OEP_HASH_CHECK);
4175 : else
4176 : {
4177 327790287 : unsigned int sflags = flags;
4178 :
4179 327790287 : hstate.add_object (code);
4180 :
4181 327790287 : switch (code)
4182 : {
4183 131504150 : case ADDR_EXPR:
4184 131504150 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4185 131504150 : flags |= OEP_ADDRESS_OF;
4186 131504150 : sflags = flags;
4187 131504150 : break;
4188 :
4189 79200361 : case INDIRECT_REF:
4190 79200361 : case MEM_REF:
4191 79200361 : case TARGET_MEM_REF:
4192 79200361 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4193 79200361 : sflags = flags;
4194 79200361 : break;
4195 :
4196 77231418 : case COMPONENT_REF:
4197 77231418 : if (sflags & OEP_ADDRESS_OF)
4198 : {
4199 39166410 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4200 39166410 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4201 : hstate, flags & ~OEP_ADDRESS_OF);
4202 39166410 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4203 : hstate, flags & ~OEP_ADDRESS_OF);
4204 39166410 : return;
4205 : }
4206 : break;
4207 15673576 : case ARRAY_REF:
4208 15673576 : case ARRAY_RANGE_REF:
4209 15673576 : case BIT_FIELD_REF:
4210 15673576 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4211 15673576 : break;
4212 :
4213 8482 : case COND_EXPR:
4214 8482 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4215 8482 : break;
4216 :
4217 0 : case WIDEN_MULT_PLUS_EXPR:
4218 0 : case WIDEN_MULT_MINUS_EXPR:
4219 0 : {
4220 : /* The multiplication operands are commutative. */
4221 0 : inchash::hash one, two;
4222 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4223 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4224 0 : hstate.add_commutative (one, two);
4225 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4226 0 : return;
4227 : }
4228 :
4229 43747 : case CALL_EXPR:
4230 43747 : if (CALL_EXPR_FN (t) == NULL_TREE)
4231 14 : hstate.add_int (CALL_EXPR_IFN (t));
4232 : break;
4233 :
4234 72 : case TARGET_EXPR:
4235 : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4236 : Usually different TARGET_EXPRs just should use
4237 : different temporaries in their slots. */
4238 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4239 72 : return;
4240 :
4241 293948 : case OBJ_TYPE_REF:
4242 : /* Virtual table reference. */
4243 293948 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4244 293948 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4245 293948 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4246 293948 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4247 293948 : if (!virtual_method_call_p (t))
4248 : return;
4249 293927 : if (tree c = obj_type_ref_class (t))
4250 : {
4251 293927 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4252 : /* We compute mangled names only when free_lang_data is run.
4253 : In that case we can hash precisely. */
4254 293927 : if (TREE_CODE (c) == TYPE_DECL
4255 293927 : && DECL_ASSEMBLER_NAME_SET_P (c))
4256 7309 : hstate.add_object
4257 7309 : (IDENTIFIER_HASH_VALUE
4258 : (DECL_ASSEMBLER_NAME (c)));
4259 : }
4260 : return;
4261 : default:
4262 : break;
4263 : }
4264 :
4265 : /* Don't hash the type, that can lead to having nodes which
4266 : compare equal according to operand_equal_p, but which
4267 : have different hash codes. */
4268 288329857 : if (code == NON_LVALUE_EXPR)
4269 : {
4270 : /* Make sure to include signness in the hash computation. */
4271 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4272 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4273 : }
4274 :
4275 288329857 : else if (commutative_tree_code (code))
4276 : {
4277 : /* It's a commutative expression. We want to hash it the same
4278 : however it appears. We do this by first hashing both operands
4279 : and then rehashing based on the order of their independent
4280 : hashes. */
4281 17534904 : inchash::hash one, two;
4282 17534904 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4283 17534904 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4284 17534904 : hstate.add_commutative (one, two);
4285 : }
4286 : else
4287 755756561 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4288 699128525 : hash_operand (TREE_OPERAND (t, i), hstate,
4289 : i == 0 ? flags : sflags);
4290 : }
4291 : return;
4292 : }
4293 : }
4294 :
4295 : bool
4296 7364884986 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4297 : unsigned int flags, bool *ret)
4298 : {
4299 : /* When checking and unless comparing DECL names, verify that if
4300 : the outermost operand_equal_p call returns non-zero then ARG0
4301 : and ARG1 have the same hash value. */
4302 7364884986 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4303 : {
4304 3102068383 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4305 : {
4306 477264973 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4307 : {
4308 85004665 : inchash::hash hstate0 (0), hstate1 (0);
4309 85004665 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4310 85004665 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4311 85004665 : hashval_t h0 = hstate0.end ();
4312 85004665 : hashval_t h1 = hstate1.end ();
4313 85004665 : gcc_assert (h0 == h1);
4314 : }
4315 477264973 : *ret = true;
4316 : }
4317 : else
4318 2624803410 : *ret = false;
4319 :
4320 : return true;
4321 : }
4322 :
4323 : return false;
4324 : }
4325 :
4326 :
4327 : static operand_compare default_compare_instance;
4328 :
4329 : /* Convenience wrapper around operand_compare class because usually we do
4330 : not need to play with the valueizer. */
4331 :
4332 : bool
4333 3100337196 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4334 : {
4335 3100337196 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4336 : }
4337 :
4338 : namespace inchash
4339 : {
4340 :
4341 : /* Generate a hash value for an expression. This can be used iteratively
4342 : by passing a previous result as the HSTATE argument.
4343 :
4344 : This function is intended to produce the same hash for expressions which
4345 : would compare equal using operand_equal_p. */
4346 : void
4347 2302412586 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4348 : {
4349 2302412586 : default_compare_instance.hash_operand (t, hstate, flags);
4350 2302412586 : }
4351 :
4352 : }
4353 :
4354 : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4355 : with a different signedness or a narrower precision. */
4356 :
4357 : static bool
4358 20551680 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4359 : {
4360 20551680 : if (operand_equal_p (arg0, arg1, 0))
4361 : return true;
4362 :
4363 39320562 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4364 33567451 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4365 : return false;
4366 :
4367 : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4368 : and see if the inner values are the same. This removes any
4369 : signedness comparison, which doesn't matter here. */
4370 6239858 : tree op0 = arg0;
4371 6239858 : tree op1 = arg1;
4372 6239858 : STRIP_NOPS (op0);
4373 6239858 : STRIP_NOPS (op1);
4374 6239858 : if (operand_equal_p (op0, op1, 0))
4375 : return true;
4376 :
4377 : /* Discard a single widening conversion from ARG1 and see if the inner
4378 : value is the same as ARG0. */
4379 5158596 : if (CONVERT_EXPR_P (arg1)
4380 903201 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4381 903147 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4382 903147 : < TYPE_PRECISION (TREE_TYPE (arg1))
4383 6367837 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4384 : return true;
4385 :
4386 : return false;
4387 : }
4388 :
4389 : /* See if ARG is an expression that is either a comparison or is performing
4390 : arithmetic on comparisons. The comparisons must only be comparing
4391 : two different values, which will be stored in *CVAL1 and *CVAL2; if
4392 : they are nonzero it means that some operands have already been found.
4393 : No variables may be used anywhere else in the expression except in the
4394 : comparisons.
4395 :
4396 : If this is true, return 1. Otherwise, return zero. */
4397 :
4398 : static bool
4399 60687384 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4400 : {
4401 64715879 : enum tree_code code = TREE_CODE (arg);
4402 64715879 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4403 :
4404 : /* We can handle some of the tcc_expression cases here. */
4405 64715879 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4406 : tclass = tcc_unary;
4407 64097757 : else if (tclass == tcc_expression
4408 698901 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4409 698901 : || code == COMPOUND_EXPR))
4410 : tclass = tcc_binary;
4411 :
4412 64086848 : switch (tclass)
4413 : {
4414 4028495 : case tcc_unary:
4415 4028495 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4416 :
4417 5371562 : case tcc_binary:
4418 5371562 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4419 5371562 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4420 :
4421 : case tcc_constant:
4422 : return true;
4423 :
4424 687992 : case tcc_expression:
4425 687992 : if (code == COND_EXPR)
4426 692 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4427 692 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4428 756 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4429 : return false;
4430 :
4431 622866 : case tcc_comparison:
4432 : /* First see if we can handle the first operand, then the second. For
4433 : the second operand, we know *CVAL1 can't be zero. It must be that
4434 : one side of the comparison is each of the values; test for the
4435 : case where this isn't true by failing if the two operands
4436 : are the same. */
4437 :
4438 622866 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4439 622866 : TREE_OPERAND (arg, 1), 0))
4440 : return false;
4441 :
4442 622866 : if (*cval1 == 0)
4443 620684 : *cval1 = TREE_OPERAND (arg, 0);
4444 2182 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4445 : ;
4446 2063 : else if (*cval2 == 0)
4447 0 : *cval2 = TREE_OPERAND (arg, 0);
4448 2063 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4449 : ;
4450 : else
4451 : return false;
4452 :
4453 620803 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4454 : ;
4455 620803 : else if (*cval2 == 0)
4456 620684 : *cval2 = TREE_OPERAND (arg, 1);
4457 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4458 : ;
4459 : else
4460 : return false;
4461 :
4462 : return true;
4463 :
4464 : default:
4465 : return false;
4466 : }
4467 : }
4468 :
4469 : /* ARG is a tree that is known to contain just arithmetic operations and
4470 : comparisons. Evaluate the operations in the tree substituting NEW0 for
4471 : any occurrence of OLD0 as an operand of a comparison and likewise for
4472 : NEW1 and OLD1. */
4473 :
4474 : static tree
4475 663 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4476 : tree old1, tree new1)
4477 : {
4478 663 : tree type = TREE_TYPE (arg);
4479 663 : enum tree_code code = TREE_CODE (arg);
4480 663 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4481 :
4482 : /* We can handle some of the tcc_expression cases here. */
4483 663 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4484 : tclass = tcc_unary;
4485 663 : else if (tclass == tcc_expression
4486 30 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4487 : tclass = tcc_binary;
4488 :
4489 642 : switch (tclass)
4490 : {
4491 168 : case tcc_unary:
4492 168 : return fold_build1_loc (loc, code, type,
4493 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4494 168 : old0, new0, old1, new1));
4495 :
4496 153 : case tcc_binary:
4497 306 : return fold_build2_loc (loc, code, type,
4498 153 : eval_subst (loc, TREE_OPERAND (arg, 0),
4499 : old0, new0, old1, new1),
4500 153 : eval_subst (loc, TREE_OPERAND (arg, 1),
4501 153 : old0, new0, old1, new1));
4502 :
4503 9 : case tcc_expression:
4504 9 : switch (code)
4505 : {
4506 0 : case SAVE_EXPR:
4507 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4508 0 : old1, new1);
4509 :
4510 0 : case COMPOUND_EXPR:
4511 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4512 0 : old1, new1);
4513 :
4514 9 : case COND_EXPR:
4515 27 : return fold_build3_loc (loc, code, type,
4516 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4517 : old0, new0, old1, new1),
4518 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4519 : old0, new0, old1, new1),
4520 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4521 9 : old0, new0, old1, new1));
4522 : default:
4523 : break;
4524 : }
4525 : /* Fall through - ??? */
4526 :
4527 168 : case tcc_comparison:
4528 168 : {
4529 168 : tree arg0 = TREE_OPERAND (arg, 0);
4530 168 : tree arg1 = TREE_OPERAND (arg, 1);
4531 :
4532 : /* We need to check both for exact equality and tree equality. The
4533 : former will be true if the operand has a side-effect. In that
4534 : case, we know the operand occurred exactly once. */
4535 :
4536 168 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4537 : arg0 = new0;
4538 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4539 : arg0 = new1;
4540 :
4541 168 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4542 : arg1 = new0;
4543 168 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4544 : arg1 = new1;
4545 :
4546 168 : return fold_build2_loc (loc, code, type, arg0, arg1);
4547 : }
4548 :
4549 : default:
4550 : return arg;
4551 : }
4552 : }
4553 :
4554 : /* Return a tree for the case when the result of an expression is RESULT
4555 : converted to TYPE and OMITTED was previously an operand of the expression
4556 : but is now not needed (e.g., we folded OMITTED * 0).
4557 :
4558 : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4559 : the conversion of RESULT to TYPE. */
4560 :
4561 : tree
4562 317604 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4563 : {
4564 317604 : tree t = fold_convert_loc (loc, type, result);
4565 :
4566 : /* If the resulting operand is an empty statement, just return the omitted
4567 : statement casted to void. */
4568 317604 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4569 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4570 0 : fold_ignored_result (omitted));
4571 :
4572 317604 : if (TREE_SIDE_EFFECTS (omitted))
4573 12052 : return build2_loc (loc, COMPOUND_EXPR, type,
4574 12052 : fold_ignored_result (omitted), t);
4575 :
4576 305552 : return non_lvalue_loc (loc, t);
4577 : }
4578 :
4579 : /* Return a tree for the case when the result of an expression is RESULT
4580 : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4581 : of the expression but are now not needed.
4582 :
4583 : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4584 : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4585 : evaluated before OMITTED2. Otherwise, if neither has side effects,
4586 : just do the conversion of RESULT to TYPE. */
4587 :
4588 : tree
4589 5848 : omit_two_operands_loc (location_t loc, tree type, tree result,
4590 : tree omitted1, tree omitted2)
4591 : {
4592 5848 : tree t = fold_convert_loc (loc, type, result);
4593 :
4594 5848 : if (TREE_SIDE_EFFECTS (omitted2))
4595 69 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4596 5848 : if (TREE_SIDE_EFFECTS (omitted1))
4597 176 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4598 :
4599 5848 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4600 : }
4601 :
4602 :
4603 : /* Return a simplified tree node for the truth-negation of ARG. This
4604 : never alters ARG itself. We assume that ARG is an operation that
4605 : returns a truth value (0 or 1).
4606 :
4607 : FIXME: one would think we would fold the result, but it causes
4608 : problems with the dominator optimizer. */
4609 :
4610 : static tree
4611 51301687 : fold_truth_not_expr (location_t loc, tree arg)
4612 : {
4613 51301687 : tree type = TREE_TYPE (arg);
4614 51301687 : enum tree_code code = TREE_CODE (arg);
4615 51301687 : location_t loc1, loc2;
4616 :
4617 : /* If this is a comparison, we can simply invert it, except for
4618 : floating-point non-equality comparisons, in which case we just
4619 : enclose a TRUTH_NOT_EXPR around what we have. */
4620 :
4621 51301687 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4622 : {
4623 39193309 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4624 32523733 : if (FLOAT_TYPE_P (op_type)
4625 6679663 : && flag_trapping_math
4626 6648786 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4627 45802004 : && code != NE_EXPR && code != EQ_EXPR)
4628 : return NULL_TREE;
4629 :
4630 33282137 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4631 33282137 : if (code == ERROR_MARK)
4632 : return NULL_TREE;
4633 :
4634 33282137 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4635 33282137 : TREE_OPERAND (arg, 1));
4636 33282137 : copy_warning (ret, arg);
4637 33282137 : return ret;
4638 : }
4639 :
4640 12108378 : switch (code)
4641 : {
4642 0 : case INTEGER_CST:
4643 0 : return constant_boolean_node (integer_zerop (arg), type);
4644 :
4645 51779 : case TRUTH_AND_EXPR:
4646 51779 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4647 51779 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4648 103558 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4649 51779 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4650 103558 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4651 :
4652 2632 : case TRUTH_OR_EXPR:
4653 2632 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4654 2632 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4655 5264 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4656 2632 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4657 5264 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4658 :
4659 72972 : case TRUTH_XOR_EXPR:
4660 : /* Here we can invert either operand. We invert the first operand
4661 : unless the second operand is a TRUTH_NOT_EXPR in which case our
4662 : result is the XOR of the first operand with the inside of the
4663 : negation of the second operand. */
4664 :
4665 72972 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4666 188 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4667 376 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4668 : else
4669 72784 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4670 72784 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4671 145568 : TREE_OPERAND (arg, 1));
4672 :
4673 398040 : case TRUTH_ANDIF_EXPR:
4674 398040 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4675 398040 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4676 796080 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4677 398040 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4678 796080 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4679 :
4680 16953 : case TRUTH_ORIF_EXPR:
4681 16953 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4682 16953 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4683 33906 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4684 16953 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4685 33906 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4686 :
4687 773753 : case TRUTH_NOT_EXPR:
4688 773753 : return TREE_OPERAND (arg, 0);
4689 :
4690 9750 : case COND_EXPR:
4691 9750 : {
4692 9750 : tree arg1 = TREE_OPERAND (arg, 1);
4693 9750 : tree arg2 = TREE_OPERAND (arg, 2);
4694 :
4695 9750 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4696 9750 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4697 :
4698 : /* A COND_EXPR may have a throw as one operand, which
4699 : then has void type. Just leave void operands
4700 : as they are. */
4701 9750 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4702 9750 : VOID_TYPE_P (TREE_TYPE (arg1))
4703 9750 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4704 9750 : VOID_TYPE_P (TREE_TYPE (arg2))
4705 19497 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4706 : }
4707 :
4708 991 : case COMPOUND_EXPR:
4709 991 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4710 1982 : return build2_loc (loc, COMPOUND_EXPR, type,
4711 991 : TREE_OPERAND (arg, 0),
4712 1982 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4713 :
4714 0 : case NON_LVALUE_EXPR:
4715 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4716 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4717 :
4718 75128 : CASE_CONVERT:
4719 75128 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4720 75064 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4721 :
4722 : /* fall through */
4723 :
4724 64 : case FLOAT_EXPR:
4725 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4726 64 : return build1_loc (loc, TREE_CODE (arg), type,
4727 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4728 :
4729 492 : case BIT_AND_EXPR:
4730 492 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4731 : return NULL_TREE;
4732 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4733 :
4734 2 : case SAVE_EXPR:
4735 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4736 :
4737 373 : case CLEANUP_POINT_EXPR:
4738 373 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4739 373 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4740 746 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4741 :
4742 : default:
4743 : return NULL_TREE;
4744 : }
4745 : }
4746 :
4747 : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4748 : assume that ARG is an operation that returns a truth value (0 or 1
4749 : for scalars, 0 or -1 for vectors). Return the folded expression if
4750 : folding is successful. Otherwise, return NULL_TREE. */
4751 :
4752 : static tree
4753 2109244 : fold_invert_truthvalue (location_t loc, tree arg)
4754 : {
4755 2109244 : tree type = TREE_TYPE (arg);
4756 4218464 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4757 : ? BIT_NOT_EXPR
4758 : : TRUTH_NOT_EXPR,
4759 2109244 : type, arg);
4760 : }
4761 :
4762 : /* Return a simplified tree node for the truth-negation of ARG. This
4763 : never alters ARG itself. We assume that ARG is an operation that
4764 : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4765 :
4766 : tree
4767 43633684 : invert_truthvalue_loc (location_t loc, tree arg)
4768 : {
4769 43633684 : if (TREE_CODE (arg) == ERROR_MARK)
4770 : return arg;
4771 :
4772 43633684 : tree type = TREE_TYPE (arg);
4773 87267368 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4774 : ? BIT_NOT_EXPR
4775 : : TRUTH_NOT_EXPR,
4776 43633684 : type, arg);
4777 : }
4778 :
4779 : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4780 : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4781 : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4782 : is the original memory reference used to preserve the alias set of
4783 : the access. */
4784 :
4785 : tree
4786 844033 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4787 : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4788 : int unsignedp, int reversep)
4789 : {
4790 844033 : tree result, bftype;
4791 :
4792 : /* Attempt not to lose the access path if possible. */
4793 844033 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4794 : {
4795 840241 : tree ninner = TREE_OPERAND (orig_inner, 0);
4796 840241 : machine_mode nmode;
4797 840241 : poly_int64 nbitsize, nbitpos;
4798 840241 : tree noffset;
4799 840241 : int nunsignedp, nreversep, nvolatilep = 0;
4800 840241 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4801 : &noffset, &nmode, &nunsignedp,
4802 : &nreversep, &nvolatilep);
4803 840241 : if (base == inner
4804 840116 : && noffset == NULL_TREE
4805 840116 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4806 840109 : && !reversep
4807 840030 : && !nreversep
4808 1680271 : && !nvolatilep)
4809 : {
4810 840030 : inner = ninner;
4811 840241 : bitpos -= nbitpos;
4812 : }
4813 : }
4814 :
4815 844033 : alias_set_type iset = get_alias_set (orig_inner);
4816 844033 : if (iset == 0 && get_alias_set (inner) != iset)
4817 234 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4818 : build_fold_addr_expr (inner),
4819 : build_int_cst (ptr_type_node, 0));
4820 :
4821 844033 : if (known_eq (bitpos, 0) && !reversep)
4822 : {
4823 12783 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4824 25566 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4825 12621 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4826 166 : && tree_fits_shwi_p (size)
4827 12949 : && tree_to_shwi (size) == bitsize)
4828 143 : return fold_convert_loc (loc, type, inner);
4829 : }
4830 :
4831 843890 : bftype = type;
4832 843890 : if (TYPE_PRECISION (bftype) != bitsize
4833 843890 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4834 501 : bftype = build_nonstandard_integer_type (bitsize, 0);
4835 :
4836 843890 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4837 843890 : bitsize_int (bitsize), bitsize_int (bitpos));
4838 843890 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4839 :
4840 843890 : if (bftype != type)
4841 501 : result = fold_convert_loc (loc, type, result);
4842 :
4843 : return result;
4844 : }
4845 :
4846 : /* Optimize a bit-field compare.
4847 :
4848 : There are two cases: First is a compare against a constant and the
4849 : second is a comparison of two items where the fields are at the same
4850 : bit position relative to the start of a chunk (byte, halfword, word)
4851 : large enough to contain it. In these cases we can avoid the shift
4852 : implicit in bitfield extractions.
4853 :
4854 : For constants, we emit a compare of the shifted constant with the
4855 : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4856 : compared. For two fields at the same position, we do the ANDs with the
4857 : similar mask and compare the result of the ANDs.
4858 :
4859 : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4860 : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4861 : are the left and right operands of the comparison, respectively.
4862 :
4863 : If the optimization described above can be done, we return the resulting
4864 : tree. Otherwise we return zero. */
4865 :
4866 : static tree
4867 4738961 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4868 : tree compare_type, tree lhs, tree rhs)
4869 : {
4870 4738961 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4871 4738961 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4872 4738961 : tree type = TREE_TYPE (lhs);
4873 4738961 : tree unsigned_type;
4874 4738961 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4875 4738961 : machine_mode lmode, rmode;
4876 4738961 : scalar_int_mode nmode;
4877 4738961 : int lunsignedp, runsignedp;
4878 4738961 : int lreversep, rreversep;
4879 4738961 : int lvolatilep = 0, rvolatilep = 0;
4880 4738961 : tree linner, rinner = NULL_TREE;
4881 4738961 : tree mask;
4882 4738961 : tree offset;
4883 :
4884 : /* Get all the information about the extractions being done. If the bit size
4885 : is the same as the size of the underlying object, we aren't doing an
4886 : extraction at all and so can do nothing. We also don't want to
4887 : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4888 : then will no longer be able to replace it. */
4889 4738961 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4890 : &lunsignedp, &lreversep, &lvolatilep);
4891 4738961 : if (linner == lhs
4892 4738961 : || !known_size_p (plbitsize)
4893 4738961 : || !plbitsize.is_constant (&lbitsize)
4894 4738961 : || !plbitpos.is_constant (&lbitpos)
4895 9477922 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4896 804118 : || offset != 0
4897 804093 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4898 5543054 : || lvolatilep)
4899 : return 0;
4900 :
4901 804033 : if (const_p)
4902 764959 : rreversep = lreversep;
4903 : else
4904 : {
4905 : /* If this is not a constant, we can only do something if bit positions,
4906 : sizes, signedness and storage order are the same. */
4907 39074 : rinner
4908 39074 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4909 : &runsignedp, &rreversep, &rvolatilep);
4910 :
4911 39074 : if (rinner == rhs
4912 39030 : || maybe_ne (lbitpos, rbitpos)
4913 38996 : || maybe_ne (lbitsize, rbitsize)
4914 38996 : || lunsignedp != runsignedp
4915 38996 : || lreversep != rreversep
4916 38996 : || offset != 0
4917 38996 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4918 78070 : || rvolatilep)
4919 : return 0;
4920 : }
4921 :
4922 : /* Honor the C++ memory model and mimic what RTL expansion does. */
4923 803955 : poly_uint64 bitstart = 0;
4924 803955 : poly_uint64 bitend = 0;
4925 803955 : if (TREE_CODE (lhs) == COMPONENT_REF)
4926 : {
4927 803955 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4928 803955 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4929 : return 0;
4930 : }
4931 :
4932 : /* See if we can find a mode to refer to this field. We should be able to,
4933 : but fail if we can't. */
4934 1607910 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4935 764959 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4936 38996 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4937 : TYPE_ALIGN (TREE_TYPE (rinner))),
4938 803955 : BITS_PER_WORD, false, &nmode))
4939 : return 0;
4940 :
4941 : /* Set signed and unsigned types of the precision of this mode for the
4942 : shifts below. */
4943 801952 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4944 :
4945 : /* Compute the bit position and size for the new reference and our offset
4946 : within it. If the new reference is the same size as the original, we
4947 : won't optimize anything, so return zero. */
4948 801952 : nbitsize = GET_MODE_BITSIZE (nmode);
4949 801952 : nbitpos = lbitpos & ~ (nbitsize - 1);
4950 801952 : lbitpos -= nbitpos;
4951 801952 : if (nbitsize == lbitsize)
4952 : return 0;
4953 :
4954 801952 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4955 61 : lbitpos = nbitsize - lbitsize - lbitpos;
4956 :
4957 : /* Make the mask to be used against the extracted field. */
4958 801952 : mask = build_int_cst_type (unsigned_type, -1);
4959 801952 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4960 801952 : mask = const_binop (RSHIFT_EXPR, mask,
4961 801952 : size_int (nbitsize - lbitsize - lbitpos));
4962 :
4963 801952 : if (! const_p)
4964 : {
4965 37441 : if (nbitpos < 0)
4966 : return 0;
4967 :
4968 : /* If not comparing with constant, just rework the comparison
4969 : and return. */
4970 37441 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4971 37441 : nbitsize, nbitpos, 1, lreversep);
4972 37441 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4973 37441 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4974 37441 : nbitsize, nbitpos, 1, rreversep);
4975 37441 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4976 37441 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4977 : }
4978 :
4979 : /* Otherwise, we are handling the constant case. See if the constant is too
4980 : big for the field. Warn and return a tree for 0 (false) if so. We do
4981 : this not only for its own sake, but to avoid having to test for this
4982 : error case below. If we didn't, we might generate wrong code.
4983 :
4984 : For unsigned fields, the constant shifted right by the field length should
4985 : be all zero. For signed fields, the high-order bits should agree with
4986 : the sign bit. */
4987 :
4988 764511 : if (lunsignedp)
4989 : {
4990 763339 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4991 : {
4992 0 : warning (0, "comparison is always %d due to width of bit-field",
4993 : code == NE_EXPR);
4994 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4995 : }
4996 : }
4997 : else
4998 : {
4999 1172 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
5000 1172 : if (tem != 0 && tem != -1)
5001 : {
5002 0 : warning (0, "comparison is always %d due to width of bit-field",
5003 : code == NE_EXPR);
5004 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5005 : }
5006 1172 : }
5007 :
5008 764511 : if (nbitpos < 0)
5009 : return 0;
5010 :
5011 : /* Single-bit compares should always be against zero. */
5012 764511 : if (lbitsize == 1 && ! integer_zerop (rhs))
5013 : {
5014 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5015 175 : rhs = build_int_cst (type, 0);
5016 : }
5017 :
5018 : /* Make a new bitfield reference, shift the constant over the
5019 : appropriate number of bits and mask it with the computed mask
5020 : (in case this was a signed field). If we changed it, make a new one. */
5021 764511 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5022 764511 : nbitsize, nbitpos, 1, lreversep);
5023 :
5024 764511 : rhs = const_binop (BIT_AND_EXPR,
5025 : const_binop (LSHIFT_EXPR,
5026 : fold_convert_loc (loc, unsigned_type, rhs),
5027 764511 : size_int (lbitpos)),
5028 : mask);
5029 :
5030 764511 : lhs = build2_loc (loc, code, compare_type,
5031 : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5032 764511 : return lhs;
5033 : }
5034 :
5035 : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5036 : represents the sign bit of EXP's type. If EXP represents a sign
5037 : or zero extension, also test VAL against the unextended type.
5038 : The return value is the (sub)expression whose sign bit is VAL,
5039 : or NULL_TREE otherwise. */
5040 :
5041 : tree
5042 2602 : sign_bit_p (tree exp, const_tree val)
5043 : {
5044 2602 : int width;
5045 2602 : tree t;
5046 :
5047 : /* Tree EXP must have an integral type. */
5048 2602 : t = TREE_TYPE (exp);
5049 2602 : if (! INTEGRAL_TYPE_P (t))
5050 : return NULL_TREE;
5051 :
5052 : /* Tree VAL must be an integer constant. */
5053 2256 : if (TREE_CODE (val) != INTEGER_CST
5054 2256 : || TREE_OVERFLOW (val))
5055 : return NULL_TREE;
5056 :
5057 1894 : width = TYPE_PRECISION (t);
5058 1894 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5059 : return exp;
5060 :
5061 : /* Handle extension from a narrower type. */
5062 1257 : if (TREE_CODE (exp) == NOP_EXPR
5063 1257 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5064 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5065 :
5066 : return NULL_TREE;
5067 : }
5068 :
5069 : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5070 : operand is simple enough to be evaluated unconditionally. */
5071 :
5072 : static bool
5073 66130544 : simple_operand_p (const_tree exp)
5074 : {
5075 : /* Strip any conversions that don't change the machine mode. */
5076 66130544 : STRIP_NOPS (exp);
5077 :
5078 66130544 : return (CONSTANT_CLASS_P (exp)
5079 45929810 : || TREE_CODE (exp) == SSA_NAME
5080 82409852 : || (DECL_P (exp)
5081 5673591 : && ! TREE_ADDRESSABLE (exp)
5082 5585788 : && ! TREE_THIS_VOLATILE (exp)
5083 5585788 : && ! DECL_NONLOCAL (exp)
5084 : /* Don't regard global variables as simple. They may be
5085 : allocated in ways unknown to the compiler (shared memory,
5086 : #pragma weak, etc). */
5087 5584135 : && ! TREE_PUBLIC (exp)
5088 5563380 : && ! DECL_EXTERNAL (exp)
5089 : /* DECL_VALUE_EXPR will expand to something non-simple. */
5090 5563380 : && ! ((VAR_P (exp)
5091 : || TREE_CODE (exp) == PARM_DECL
5092 : || TREE_CODE (exp) == RESULT_DECL)
5093 5563380 : && DECL_HAS_VALUE_EXPR_P (exp))
5094 : /* Weakrefs are not safe to be read, since they can be NULL.
5095 : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5096 : have DECL_WEAK flag set. */
5097 5562791 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5098 : /* Loading a static variable is unduly expensive, but global
5099 : registers aren't expensive. */
5100 5562791 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5101 : }
5102 :
5103 : /* Determine if an operand is simple enough to be evaluated unconditionally.
5104 : In addition to simple_operand_p, we assume that comparisons, conversions,
5105 : and logic-not operations are simple, if their operands are simple, too. */
5106 :
5107 : bool
5108 7163709 : simple_condition_p (tree exp)
5109 : {
5110 7258368 : enum tree_code code;
5111 :
5112 7258368 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5113 : return false;
5114 :
5115 2330079 : while (CONVERT_EXPR_P (exp))
5116 35683 : exp = TREE_OPERAND (exp, 0);
5117 :
5118 2294396 : code = TREE_CODE (exp);
5119 :
5120 2294396 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5121 1792359 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5122 1792359 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5123 :
5124 502037 : if (code == TRUTH_NOT_EXPR)
5125 94659 : return simple_condition_p (TREE_OPERAND (exp, 0));
5126 :
5127 407378 : return simple_operand_p (exp);
5128 : }
5129 :
5130 :
5131 : /* The following functions are subroutines to fold_range_test and allow it to
5132 : try to change a logical combination of comparisons into a range test.
5133 :
5134 : For example, both
5135 : X == 2 || X == 3 || X == 4 || X == 5
5136 : and
5137 : X >= 2 && X <= 5
5138 : are converted to
5139 : (unsigned) (X - 2) <= 3
5140 :
5141 : We describe each set of comparisons as being either inside or outside
5142 : a range, using a variable named like IN_P, and then describe the
5143 : range with a lower and upper bound. If one of the bounds is omitted,
5144 : it represents either the highest or lowest value of the type.
5145 :
5146 : In the comments below, we represent a range by two numbers in brackets
5147 : preceded by a "+" to designate being inside that range, or a "-" to
5148 : designate being outside that range, so the condition can be inverted by
5149 : flipping the prefix. An omitted bound is represented by a "-". For
5150 : example, "- [-, 10]" means being outside the range starting at the lowest
5151 : possible value and ending at 10, in other words, being greater than 10.
5152 : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5153 : always false.
5154 :
5155 : We set up things so that the missing bounds are handled in a consistent
5156 : manner so neither a missing bound nor "true" and "false" need to be
5157 : handled using a special case. */
5158 :
5159 : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5160 : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5161 : and UPPER1_P are nonzero if the respective argument is an upper bound
5162 : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5163 : must be specified for a comparison. ARG1 will be converted to ARG0's
5164 : type if both are specified. */
5165 :
5166 : static tree
5167 23293523 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5168 : tree arg1, int upper1_p)
5169 : {
5170 23293523 : tree tem;
5171 23293523 : int result;
5172 23293523 : int sgn0, sgn1;
5173 :
5174 : /* If neither arg represents infinity, do the normal operation.
5175 : Else, if not a comparison, return infinity. Else handle the special
5176 : comparison rules. Note that most of the cases below won't occur, but
5177 : are handled for consistency. */
5178 :
5179 23293523 : if (arg0 != 0 && arg1 != 0)
5180 : {
5181 12235959 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5182 : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5183 12235959 : STRIP_NOPS (tem);
5184 12235959 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5185 : }
5186 :
5187 11057564 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5188 : return 0;
5189 :
5190 : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5191 : for neither. In real maths, we cannot assume open ended ranges are
5192 : the same. But, this is computer arithmetic, where numbers are finite.
5193 : We can therefore make the transformation of any unbounded range with
5194 : the value Z, Z being greater than any representable number. This permits
5195 : us to treat unbounded ranges as equal. */
5196 11048423 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5197 11048423 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5198 11048423 : switch (code)
5199 : {
5200 5208763 : case EQ_EXPR:
5201 5208763 : result = sgn0 == sgn1;
5202 5208763 : break;
5203 0 : case NE_EXPR:
5204 0 : result = sgn0 != sgn1;
5205 0 : break;
5206 365795 : case LT_EXPR:
5207 365795 : result = sgn0 < sgn1;
5208 365795 : break;
5209 2537518 : case LE_EXPR:
5210 2537518 : result = sgn0 <= sgn1;
5211 2537518 : break;
5212 2936347 : case GT_EXPR:
5213 2936347 : result = sgn0 > sgn1;
5214 2936347 : break;
5215 0 : case GE_EXPR:
5216 0 : result = sgn0 >= sgn1;
5217 0 : break;
5218 0 : default:
5219 0 : gcc_unreachable ();
5220 : }
5221 :
5222 11048423 : return constant_boolean_node (result, type);
5223 : }
5224 :
5225 : /* Helper routine for make_range. Perform one step for it, return
5226 : new expression if the loop should continue or NULL_TREE if it should
5227 : stop. */
5228 :
5229 : tree
5230 61225550 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5231 : tree exp_type, tree *p_low, tree *p_high, int *p_in_p)
5232 : {
5233 61225550 : tree arg0_type = TREE_TYPE (arg0);
5234 61225550 : tree n_low, n_high, low = *p_low, high = *p_high;
5235 61225550 : int in_p = *p_in_p, n_in_p;
5236 :
5237 61225550 : switch (code)
5238 : {
5239 1719585 : case TRUTH_NOT_EXPR:
5240 : /* We can only do something if the range is testing for zero. */
5241 1719585 : if (low == NULL_TREE || high == NULL_TREE
5242 1719585 : || ! integer_zerop (low) || ! integer_zerop (high))
5243 : return NULL_TREE;
5244 1719585 : *p_in_p = ! in_p;
5245 1719585 : return arg0;
5246 :
5247 47470674 : case EQ_EXPR: case NE_EXPR:
5248 47470674 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5249 : /* We can only do something if the range is testing for zero
5250 : and if the second operand is an integer constant. Note that
5251 : saying something is "in" the range we make is done by
5252 : complementing IN_P since it will set in the initial case of
5253 : being not equal to zero; "out" is leaving it alone. */
5254 47470674 : if (low == NULL_TREE || high == NULL_TREE
5255 47470674 : || ! integer_zerop (low) || ! integer_zerop (high)
5256 94941260 : || TREE_CODE (arg1) != INTEGER_CST)
5257 : return NULL_TREE;
5258 :
5259 30003042 : switch (code)
5260 : {
5261 : case NE_EXPR: /* - [c, c] */
5262 : low = high = arg1;
5263 : break;
5264 8203581 : case EQ_EXPR: /* + [c, c] */
5265 8203581 : in_p = ! in_p, low = high = arg1;
5266 8203581 : break;
5267 2234971 : case GT_EXPR: /* - [-, c] */
5268 2234971 : low = 0, high = arg1;
5269 2234971 : break;
5270 750479 : case GE_EXPR: /* + [c, -] */
5271 750479 : in_p = ! in_p, low = arg1, high = 0;
5272 750479 : break;
5273 5738503 : case LT_EXPR: /* - [c, -] */
5274 5738503 : low = arg1, high = 0;
5275 5738503 : break;
5276 4395922 : case LE_EXPR: /* + [-, c] */
5277 4395922 : in_p = ! in_p, low = 0, high = arg1;
5278 4395922 : break;
5279 : default:
5280 : gcc_unreachable ();
5281 : }
5282 :
5283 : /* If this is an unsigned comparison, we also know that EXP is
5284 : greater than or equal to zero. We base the range tests we make
5285 : on that fact, so we record it here so we can parse existing
5286 : range tests. We test arg0_type since often the return type
5287 : of, e.g. EQ_EXPR, is boolean. */
5288 30003042 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5289 : {
5290 1934038 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5291 : in_p, low, high, 1,
5292 : build_int_cst (arg0_type, 0),
5293 : NULL_TREE))
5294 : return NULL_TREE;
5295 :
5296 1934029 : in_p = n_in_p, low = n_low, high = n_high;
5297 :
5298 : /* If the high bound is missing, but we have a nonzero low
5299 : bound, reverse the range so it goes from zero to the low bound
5300 : minus 1. */
5301 1934029 : if (high == 0 && low && ! integer_zerop (low))
5302 : {
5303 859450 : in_p = ! in_p;
5304 859450 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5305 859450 : build_int_cst (TREE_TYPE (low), 1), 0);
5306 859450 : low = build_int_cst (arg0_type, 0);
5307 : }
5308 : }
5309 :
5310 30003033 : *p_low = low;
5311 30003033 : *p_high = high;
5312 30003033 : *p_in_p = in_p;
5313 30003033 : return arg0;
5314 :
5315 332 : case NEGATE_EXPR:
5316 : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5317 : low and high are non-NULL, then normalize will DTRT. */
5318 332 : if (!TYPE_UNSIGNED (arg0_type)
5319 332 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5320 : {
5321 93 : if (low == NULL_TREE)
5322 18 : low = TYPE_MIN_VALUE (arg0_type);
5323 93 : if (high == NULL_TREE)
5324 39 : high = TYPE_MAX_VALUE (arg0_type);
5325 : }
5326 :
5327 : /* (-x) IN [a,b] -> x in [-b, -a] */
5328 332 : n_low = range_binop (MINUS_EXPR, exp_type,
5329 : build_int_cst (exp_type, 0),
5330 : 0, high, 1);
5331 332 : n_high = range_binop (MINUS_EXPR, exp_type,
5332 : build_int_cst (exp_type, 0),
5333 : 0, low, 0);
5334 332 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5335 : return NULL_TREE;
5336 314 : goto normalize;
5337 :
5338 24 : case BIT_NOT_EXPR:
5339 : /* ~ X -> -X - 1 */
5340 24 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5341 : build_int_cst (exp_type, 1));
5342 :
5343 873755 : case PLUS_EXPR:
5344 873755 : case MINUS_EXPR:
5345 873755 : if (TREE_CODE (arg1) != INTEGER_CST)
5346 : return NULL_TREE;
5347 :
5348 : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5349 : move a constant to the other side. */
5350 669077 : if (!TYPE_UNSIGNED (arg0_type)
5351 669077 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5352 : return NULL_TREE;
5353 :
5354 : /* If EXP is signed, any overflow in the computation is undefined,
5355 : so we don't worry about it so long as our computations on
5356 : the bounds don't overflow. For unsigned, overflow is defined
5357 : and this is exactly the right thing. */
5358 957785 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5359 : arg0_type, low, 0, arg1, 0);
5360 480385 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5361 : arg0_type, high, 1, arg1, 0);
5362 476807 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5363 957180 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5364 : return NULL_TREE;
5365 :
5366 480687 : normalize:
5367 : /* Check for an unsigned range which has wrapped around the maximum
5368 : value thus making n_high < n_low, and normalize it. */
5369 480687 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5370 : {
5371 131901 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5372 131901 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5373 131901 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5374 131901 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5375 :
5376 : /* If the range is of the form +/- [ x+1, x ], we won't
5377 : be able to normalize it. But then, it represents the
5378 : whole range or the empty set, so make it
5379 : +/- [ -, - ]. */
5380 131901 : if (tree_int_cst_equal (n_low, low)
5381 131901 : && tree_int_cst_equal (n_high, high))
5382 : low = high = 0;
5383 : else
5384 131901 : in_p = ! in_p;
5385 : }
5386 : else
5387 348786 : low = n_low, high = n_high;
5388 :
5389 480687 : *p_low = low;
5390 480687 : *p_high = high;
5391 480687 : *p_in_p = in_p;
5392 480687 : return arg0;
5393 :
5394 2589682 : CASE_CONVERT:
5395 2589682 : case NON_LVALUE_EXPR:
5396 2589682 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5397 : return NULL_TREE;
5398 :
5399 1179322 : if (! INTEGRAL_TYPE_P (arg0_type)
5400 1144023 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5401 1041664 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5402 : return NULL_TREE;
5403 :
5404 1023271 : n_low = low, n_high = high;
5405 :
5406 1023271 : if (n_low != 0)
5407 852946 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5408 :
5409 1023271 : if (n_high != 0)
5410 959626 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5411 :
5412 : /* If we're converting arg0 from an unsigned type, to exp,
5413 : a signed type, we will be doing the comparison as unsigned.
5414 : The tests above have already verified that LOW and HIGH
5415 : are both positive.
5416 :
5417 : So we have to ensure that we will handle large unsigned
5418 : values the same way that the current signed bounds treat
5419 : negative values. */
5420 :
5421 1023271 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5422 : {
5423 259114 : tree high_positive;
5424 259114 : tree equiv_type;
5425 : /* For fixed-point modes, we need to pass the saturating flag
5426 : as the 2nd parameter. */
5427 259114 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5428 0 : equiv_type
5429 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5430 0 : TYPE_SATURATING (arg0_type));
5431 259114 : else if (BITINT_TYPE_P (arg0_type))
5432 : equiv_type = arg0_type;
5433 : else
5434 259099 : equiv_type
5435 259099 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5436 :
5437 : /* A range without an upper bound is, naturally, unbounded.
5438 : Since convert would have cropped a very large value, use
5439 : the max value for the destination type. */
5440 259114 : high_positive
5441 259114 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5442 0 : : TYPE_MAX_VALUE (arg0_type);
5443 :
5444 259114 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5445 237898 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5446 : fold_convert_loc (loc, arg0_type,
5447 : high_positive),
5448 : build_int_cst (arg0_type, 1));
5449 :
5450 : /* If the low bound is specified, "and" the range with the
5451 : range for which the original unsigned value will be
5452 : positive. */
5453 259114 : if (low != 0)
5454 : {
5455 94418 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5456 : 1, fold_convert_loc (loc, arg0_type,
5457 : integer_zero_node),
5458 : high_positive))
5459 : return NULL_TREE;
5460 :
5461 94418 : in_p = (n_in_p == in_p);
5462 : }
5463 : else
5464 : {
5465 : /* Otherwise, "or" the range with the range of the input
5466 : that will be interpreted as negative. */
5467 164696 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5468 : 1, fold_convert_loc (loc, arg0_type,
5469 : integer_zero_node),
5470 : high_positive))
5471 : return NULL_TREE;
5472 :
5473 164696 : in_p = (in_p != n_in_p);
5474 : }
5475 : }
5476 :
5477 : /* Otherwise, if we are converting arg0 from signed type, to exp,
5478 : an unsigned type, we will do the comparison as signed. If
5479 : high is non-NULL, we punt above if it doesn't fit in the signed
5480 : type, so if we get through here, +[-, high] or +[low, high] are
5481 : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5482 : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5483 : high is not, the +[low, -] range is equivalent to union of
5484 : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5485 : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5486 : low being 0, which should be treated as [-, -]. */
5487 764157 : else if (TYPE_UNSIGNED (exp_type)
5488 745815 : && !TYPE_UNSIGNED (arg0_type)
5489 389732 : && low
5490 1153889 : && !high)
5491 : {
5492 12 : if (integer_zerop (low))
5493 12 : n_low = NULL_TREE;
5494 : else
5495 : {
5496 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5497 : n_low, build_int_cst (arg0_type, -1));
5498 0 : n_low = build_zero_cst (arg0_type);
5499 0 : in_p = !in_p;
5500 : }
5501 : }
5502 :
5503 1023271 : *p_low = n_low;
5504 1023271 : *p_high = n_high;
5505 1023271 : *p_in_p = in_p;
5506 1023271 : return arg0;
5507 :
5508 : default:
5509 : return NULL_TREE;
5510 : }
5511 : }
5512 :
5513 : /* Given EXP, a logical expression, set the range it is testing into
5514 : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5515 : actually being tested. *PLOW and *PHIGH will be made of the same
5516 : type as the returned expression. If EXP is not a comparison, we
5517 : will most likely not be returning a useful value and range. */
5518 :
5519 : tree
5520 50485438 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
5521 : {
5522 50485438 : enum tree_code code;
5523 50485438 : tree arg0, arg1 = NULL_TREE;
5524 50485438 : tree exp_type, nexp;
5525 50485438 : int in_p;
5526 50485438 : tree low, high;
5527 50485438 : location_t loc = EXPR_LOCATION (exp);
5528 :
5529 : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5530 : and see if we can refine the range. Some of the cases below may not
5531 : happen, but it doesn't seem worth worrying about this. We "continue"
5532 : the outer loop when we've changed something; otherwise we "break"
5533 : the switch, which will "break" the while. */
5534 :
5535 50485438 : in_p = 0;
5536 50485438 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5537 :
5538 80632455 : while (1)
5539 : {
5540 80632455 : code = TREE_CODE (exp);
5541 80632455 : exp_type = TREE_TYPE (exp);
5542 80632455 : arg0 = NULL_TREE;
5543 :
5544 80632455 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5545 : {
5546 56401684 : if (TREE_OPERAND_LENGTH (exp) > 0)
5547 56401684 : arg0 = TREE_OPERAND (exp, 0);
5548 56401684 : if (TREE_CODE_CLASS (code) == tcc_binary
5549 53166905 : || TREE_CODE_CLASS (code) == tcc_comparison
5550 66178464 : || (TREE_CODE_CLASS (code) == tcc_expression
5551 2913165 : && TREE_OPERAND_LENGTH (exp) > 1))
5552 47802011 : arg1 = TREE_OPERAND (exp, 1);
5553 : }
5554 56401684 : if (arg0 == NULL_TREE)
5555 : break;
5556 :
5557 56401670 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5558 : &high, &in_p);
5559 56401670 : if (nexp == NULL_TREE)
5560 : break;
5561 : exp = nexp;
5562 : }
5563 :
5564 : /* If EXP is a constant, we can evaluate whether this is true or false. */
5565 50485438 : if (TREE_CODE (exp) == INTEGER_CST)
5566 : {
5567 31696 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5568 : exp, 0, low, 0))
5569 31696 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5570 : exp, 1, high, 1)));
5571 31696 : low = high = 0;
5572 31696 : exp = 0;
5573 : }
5574 :
5575 50485438 : *pin_p = in_p, *plow = low, *phigh = high;
5576 50485438 : return exp;
5577 : }
5578 :
5579 : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5580 : a bitwise check i.e. when
5581 : LOW == 0xXX...X00...0
5582 : HIGH == 0xXX...X11...1
5583 : Return corresponding mask in MASK and stem in VALUE. */
5584 :
5585 : static bool
5586 112 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5587 : tree *value)
5588 : {
5589 112 : if (TREE_CODE (low) != INTEGER_CST
5590 112 : || TREE_CODE (high) != INTEGER_CST)
5591 : return false;
5592 :
5593 112 : unsigned prec = TYPE_PRECISION (type);
5594 112 : wide_int lo = wi::to_wide (low, prec);
5595 112 : wide_int hi = wi::to_wide (high, prec);
5596 :
5597 112 : wide_int end_mask = lo ^ hi;
5598 224 : if ((end_mask & (end_mask + 1)) != 0
5599 222 : || (lo & end_mask) != 0)
5600 : return false;
5601 :
5602 86 : wide_int stem_mask = ~end_mask;
5603 86 : wide_int stem = lo & stem_mask;
5604 86 : if (stem != (hi & stem_mask))
5605 : return false;
5606 :
5607 86 : *mask = wide_int_to_tree (type, stem_mask);
5608 86 : *value = wide_int_to_tree (type, stem);
5609 :
5610 86 : return true;
5611 198 : }
5612 :
5613 : /* Helper routine for build_range_check and match.pd. Return the type to
5614 : perform the check or NULL if it shouldn't be optimized. */
5615 :
5616 : tree
5617 593398 : range_check_type (tree etype)
5618 : {
5619 : /* First make sure that arithmetics in this type is valid, then make sure
5620 : that it wraps around. */
5621 593398 : if (TREE_CODE (etype) == ENUMERAL_TYPE && BITINT_TYPE_P (etype))
5622 0 : etype = TREE_TYPE (etype);
5623 593398 : else if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5624 61290 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5625 :
5626 593398 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5627 : {
5628 408379 : tree utype, minv, maxv;
5629 :
5630 : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5631 : for the type in question, as we rely on this here. */
5632 408379 : utype = unsigned_type_for (etype);
5633 408379 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5634 408379 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5635 408379 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5636 408379 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5637 :
5638 408379 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5639 : minv, 1, maxv, 1)))
5640 : etype = utype;
5641 : else
5642 102 : return NULL_TREE;
5643 : }
5644 185019 : else if (POINTER_TYPE_P (etype)
5645 : || TREE_CODE (etype) == OFFSET_TYPE
5646 : /* Right now all BITINT_TYPEs satisfy
5647 : (unsigned) max + 1 == (unsigned) min, so no need to verify
5648 : that like for INTEGER_TYPEs. */
5649 : || TREE_CODE (etype) == BITINT_TYPE)
5650 1364 : etype = unsigned_type_for (etype);
5651 : return etype;
5652 : }
5653 :
5654 : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5655 : type, TYPE, return an expression to test if EXP is in (or out of, depending
5656 : on IN_P) the range. Return 0 if the test couldn't be created. */
5657 :
5658 : tree
5659 1628834 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5660 : tree low, tree high)
5661 : {
5662 2840234 : tree etype = TREE_TYPE (exp), mask, value;
5663 :
5664 : /* Disable this optimization for function pointer expressions
5665 : on targets that require function pointer canonicalization. */
5666 2840234 : if (targetm.have_canonicalize_funcptr_for_compare ()
5667 0 : && POINTER_TYPE_P (etype)
5668 2840234 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5669 : return NULL_TREE;
5670 :
5671 2840234 : if (! in_p)
5672 : {
5673 316310 : value = build_range_check (loc, type, exp, 1, low, high);
5674 316310 : if (value != 0)
5675 316310 : return invert_truthvalue_loc (loc, value);
5676 :
5677 : return 0;
5678 : }
5679 :
5680 2523924 : if (low == 0 && high == 0)
5681 125150 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5682 :
5683 2398774 : if (low == 0)
5684 789435 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5685 789435 : fold_convert_loc (loc, etype, high));
5686 :
5687 1609339 : if (high == 0)
5688 75150 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5689 75150 : fold_convert_loc (loc, etype, low));
5690 :
5691 1534189 : if (operand_equal_p (low, high, 0))
5692 322549 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5693 322549 : fold_convert_loc (loc, etype, low));
5694 :
5695 1211640 : if (TREE_CODE (exp) == BIT_AND_EXPR
5696 1211640 : && maskable_range_p (low, high, etype, &mask, &value))
5697 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5698 : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5699 : exp, mask),
5700 86 : value);
5701 :
5702 1211554 : if (integer_zerop (low))
5703 : {
5704 708230 : if (! TYPE_UNSIGNED (etype))
5705 : {
5706 186739 : etype = unsigned_type_for (etype);
5707 186739 : high = fold_convert_loc (loc, etype, high);
5708 186739 : exp = fold_convert_loc (loc, etype, exp);
5709 : }
5710 : return build_range_check (loc, type, exp, 1, 0, high);
5711 : }
5712 :
5713 : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5714 503324 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5715 : {
5716 130097 : int prec = TYPE_PRECISION (etype);
5717 :
5718 130097 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5719 : {
5720 124 : if (TYPE_UNSIGNED (etype))
5721 : {
5722 118 : tree signed_etype = signed_type_for (etype);
5723 118 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5724 0 : etype
5725 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5726 : else
5727 : etype = signed_etype;
5728 118 : exp = fold_convert_loc (loc, etype, exp);
5729 : }
5730 124 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5731 : build_int_cst (etype, 0));
5732 : }
5733 : }
5734 :
5735 : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5736 : This requires wrap-around arithmetics for the type of the expression. */
5737 503200 : etype = range_check_type (etype);
5738 503200 : if (etype == NULL_TREE)
5739 : return NULL_TREE;
5740 :
5741 503170 : high = fold_convert_loc (loc, etype, high);
5742 503170 : low = fold_convert_loc (loc, etype, low);
5743 503170 : exp = fold_convert_loc (loc, etype, exp);
5744 :
5745 503170 : value = const_binop (MINUS_EXPR, high, low);
5746 :
5747 503170 : if (value != 0 && !TREE_OVERFLOW (value))
5748 503170 : return build_range_check (loc, type,
5749 : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5750 : 1, build_int_cst (etype, 0), value);
5751 :
5752 : return 0;
5753 : }
5754 :
5755 : /* Return the predecessor of VAL in its type, handling the infinite case. */
5756 :
5757 : static tree
5758 177796 : range_predecessor (tree val)
5759 : {
5760 177796 : tree type = TREE_TYPE (val);
5761 :
5762 177796 : if (INTEGRAL_TYPE_P (type)
5763 177796 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5764 : return 0;
5765 : else
5766 177796 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5767 177796 : build_int_cst (TREE_TYPE (val), 1), 0);
5768 : }
5769 :
5770 : /* Return the successor of VAL in its type, handling the infinite case. */
5771 :
5772 : static tree
5773 1669839 : range_successor (tree val)
5774 : {
5775 1669839 : tree type = TREE_TYPE (val);
5776 :
5777 1669839 : if (INTEGRAL_TYPE_P (type)
5778 1669839 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5779 : return 0;
5780 : else
5781 1669830 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5782 1669830 : build_int_cst (TREE_TYPE (val), 1), 0);
5783 : }
5784 :
5785 : /* Given two ranges, see if we can merge them into one. Return 1 if we
5786 : can, 0 if we can't. Set the output range into the specified parameters. */
5787 :
5788 : bool
5789 3659165 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5790 : tree high0, int in1_p, tree low1, tree high1)
5791 : {
5792 3659165 : bool no_overlap;
5793 3659165 : int subset;
5794 3659165 : int temp;
5795 3659165 : tree tem;
5796 3659165 : int in_p;
5797 3659165 : tree low, high;
5798 3659165 : int lowequal = ((low0 == 0 && low1 == 0)
5799 3659165 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5800 3659165 : low0, 0, low1, 0)));
5801 3659165 : int highequal = ((high0 == 0 && high1 == 0)
5802 3659165 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5803 3659165 : high0, 1, high1, 1)));
5804 :
5805 : /* Make range 0 be the range that starts first, or ends last if they
5806 : start at the same value. Swap them if it isn't. */
5807 3659165 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5808 : low0, 0, low1, 0))
5809 3659165 : || (lowequal
5810 578241 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5811 : high1, 1, high0, 1))))
5812 : {
5813 : temp = in0_p, in0_p = in1_p, in1_p = temp;
5814 : tem = low0, low0 = low1, low1 = tem;
5815 : tem = high0, high0 = high1, high1 = tem;
5816 : }
5817 :
5818 : /* If the second range is != high1 where high1 is the type maximum of
5819 : the type, try first merging with < high1 range. */
5820 3659165 : if (low1
5821 3659165 : && high1
5822 1013449 : && TREE_CODE (low1) == INTEGER_CST
5823 1013449 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5824 128140 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5825 172010 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5826 : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5827 4630479 : && operand_equal_p (low1, high1, 0))
5828 : {
5829 566442 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5830 566442 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5831 : !in1_p, NULL_TREE, range_predecessor (low1)))
5832 : return true;
5833 : /* Similarly for the second range != low1 where low1 is the type minimum
5834 : of the type, try first merging with > low1 range. */
5835 453449 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5836 453449 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5837 : !in1_p, range_successor (low1), NULL_TREE))
5838 : return true;
5839 : }
5840 :
5841 : /* Now flag two cases, whether the ranges are disjoint or whether the
5842 : second range is totally subsumed in the first. Note that the tests
5843 : below are simplified by the ones above. */
5844 3450393 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5845 : high0, 1, low1, 0));
5846 3450393 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5847 : high1, 1, high0, 1));
5848 :
5849 : /* We now have four cases, depending on whether we are including or
5850 : excluding the two ranges. */
5851 3450393 : if (in0_p && in1_p)
5852 : {
5853 : /* If they don't overlap, the result is false. If the second range
5854 : is a subset it is the result. Otherwise, the range is from the start
5855 : of the second to the end of the first. */
5856 1576480 : if (no_overlap)
5857 : in_p = 0, low = high = 0;
5858 1574398 : else if (subset)
5859 : in_p = 1, low = low1, high = high1;
5860 : else
5861 1455868 : in_p = 1, low = low1, high = high0;
5862 : }
5863 :
5864 1873913 : else if (in0_p && ! in1_p)
5865 : {
5866 : /* If they don't overlap, the result is the first range. If they are
5867 : equal, the result is false. If the second range is a subset of the
5868 : first, and the ranges begin at the same place, we go from just after
5869 : the end of the second range to the end of the first. If the second
5870 : range is not a subset of the first, or if it is a subset and both
5871 : ranges end at the same place, the range starts at the start of the
5872 : first range and ends just before the second range.
5873 : Otherwise, we can't describe this as a single range. */
5874 326563 : if (no_overlap)
5875 : in_p = 1, low = low0, high = high0;
5876 320852 : else if (lowequal && highequal)
5877 : in_p = 0, low = high = 0;
5878 319970 : else if (subset && lowequal)
5879 : {
5880 243932 : low = range_successor (high1);
5881 243932 : high = high0;
5882 243932 : in_p = 1;
5883 243932 : if (low == 0)
5884 : {
5885 : /* We are in the weird situation where high0 > high1 but
5886 : high1 has no successor. Punt. */
5887 : return 0;
5888 : }
5889 : }
5890 76038 : else if (! subset || highequal)
5891 : {
5892 55204 : low = low0;
5893 55204 : high = range_predecessor (low1);
5894 55204 : in_p = 1;
5895 55204 : if (high == 0)
5896 : {
5897 : /* low0 < low1 but low1 has no predecessor. Punt. */
5898 : return 0;
5899 : }
5900 : }
5901 : else
5902 : return 0;
5903 : }
5904 :
5905 1547350 : else if (! in0_p && in1_p)
5906 : {
5907 : /* If they don't overlap, the result is the second range. If the second
5908 : is a subset of the first, the result is false. Otherwise,
5909 : the range starts just after the first range and ends at the
5910 : end of the second. */
5911 1184729 : if (no_overlap)
5912 : in_p = 1, low = low1, high = high1;
5913 1176519 : else if (subset || highequal)
5914 : in_p = 0, low = high = 0;
5915 : else
5916 : {
5917 1066132 : low = range_successor (high0);
5918 1066132 : high = high1;
5919 1066132 : in_p = 1;
5920 1066132 : if (low == 0)
5921 : {
5922 : /* high1 > high0 but high0 has no successor. Punt. */
5923 : return 0;
5924 : }
5925 : }
5926 : }
5927 :
5928 : else
5929 : {
5930 : /* The case where we are excluding both ranges. Here the complex case
5931 : is if they don't overlap. In that case, the only time we have a
5932 : range is if they are adjacent. If the second is a subset of the
5933 : first, the result is the first. Otherwise, the range to exclude
5934 : starts at the beginning of the first range and ends at the end of the
5935 : second. */
5936 362621 : if (no_overlap)
5937 : {
5938 263671 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5939 : range_successor (high0),
5940 : 1, low1, 0)))
5941 : in_p = 0, low = low0, high = high1;
5942 : else
5943 : {
5944 : /* Canonicalize - [min, x] into - [-, x]. */
5945 212086 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5946 210924 : switch (TREE_CODE (TREE_TYPE (low0)))
5947 : {
5948 51484 : case ENUMERAL_TYPE:
5949 51484 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5950 : GET_MODE_BITSIZE
5951 102968 : (TYPE_MODE (TREE_TYPE (low0)))))
5952 : break;
5953 : /* FALLTHROUGH */
5954 210723 : case INTEGER_TYPE:
5955 210723 : if (tree_int_cst_equal (low0,
5956 210723 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5957 212086 : low0 = 0;
5958 : break;
5959 201 : case POINTER_TYPE:
5960 201 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5961 201 : && integer_zerop (low0))
5962 : low0 = 0;
5963 : break;
5964 : default:
5965 : break;
5966 : }
5967 :
5968 : /* Canonicalize - [x, max] into - [x, -]. */
5969 212086 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5970 211899 : switch (TREE_CODE (TREE_TYPE (high1)))
5971 : {
5972 51492 : case ENUMERAL_TYPE:
5973 51492 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5974 : GET_MODE_BITSIZE
5975 102984 : (TYPE_MODE (TREE_TYPE (high1)))))
5976 : break;
5977 : /* FALLTHROUGH */
5978 211698 : case INTEGER_TYPE:
5979 211698 : if (tree_int_cst_equal (high1,
5980 211698 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5981 212086 : high1 = 0;
5982 : break;
5983 201 : case POINTER_TYPE:
5984 201 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5985 402 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5986 : high1, 1,
5987 201 : build_int_cst (TREE_TYPE (high1), 1),
5988 : 1)))
5989 133 : high1 = 0;
5990 : break;
5991 : default:
5992 : break;
5993 : }
5994 :
5995 : /* The ranges might be also adjacent between the maximum and
5996 : minimum values of the given type. For
5997 : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5998 : return + [x + 1, y - 1]. */
5999 212086 : if (low0 == 0 && high1 == 0)
6000 : {
6001 325 : low = range_successor (high0);
6002 325 : high = range_predecessor (low1);
6003 325 : if (low == 0 || high == 0)
6004 : return 0;
6005 :
6006 : in_p = 1;
6007 : }
6008 : else
6009 : return 0;
6010 : }
6011 : }
6012 98950 : else if (subset)
6013 : in_p = 0, low = low0, high = high0;
6014 : else
6015 12060 : in_p = 0, low = low0, high = high1;
6016 : }
6017 :
6018 3217789 : *pin_p = in_p, *plow = low, *phigh = high;
6019 3217789 : return 1;
6020 : }
6021 :
6022 :
6023 : /* Subroutine of fold, looking inside expressions of the form
6024 : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6025 : are the three operands of the COND_EXPR. This function is
6026 : being used also to optimize A op B ? C : A, by reversing the
6027 : comparison first.
6028 :
6029 : Return a folded expression whose code is not a COND_EXPR
6030 : anymore, or NULL_TREE if no folding opportunity is found. */
6031 :
6032 : static tree
6033 517601 : fold_cond_expr_with_comparison (location_t loc, tree type,
6034 : enum tree_code comp_code,
6035 : tree arg00, tree arg01, tree arg1, tree arg2)
6036 : {
6037 517601 : tree arg1_type = TREE_TYPE (arg1);
6038 517601 : tree tem;
6039 :
6040 517601 : STRIP_NOPS (arg1);
6041 517601 : STRIP_NOPS (arg2);
6042 :
6043 : /* If we have A op 0 ? A : -A, consider applying the following
6044 : transformations:
6045 :
6046 : A == 0? A : -A same as -A
6047 : A != 0? A : -A same as A
6048 : A >= 0? A : -A same as abs (A)
6049 : A > 0? A : -A same as abs (A)
6050 : A <= 0? A : -A same as -abs (A)
6051 : A < 0? A : -A same as -abs (A)
6052 :
6053 : None of these transformations work for modes with signed
6054 : zeros. If A is +/-0, the first two transformations will
6055 : change the sign of the result (from +0 to -0, or vice
6056 : versa). The last four will fix the sign of the result,
6057 : even though the original expressions could be positive or
6058 : negative, depending on the sign of A.
6059 :
6060 : Note that all these transformations are correct if A is
6061 : NaN, since the two alternatives (A and -A) are also NaNs. */
6062 517601 : if (!HONOR_SIGNED_ZEROS (type)
6063 1035212 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6064 517601 : ? real_zerop (arg01)
6065 516509 : : integer_zerop (arg01))
6066 1387615 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6067 1647 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6068 : /* In the case that A is of the form X-Y, '-A' (arg2) may
6069 : have already been folded to Y-X, check for that. */
6070 350990 : || (TREE_CODE (arg1) == MINUS_EXPR
6071 1711 : && TREE_CODE (arg2) == MINUS_EXPR
6072 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6073 0 : TREE_OPERAND (arg2, 1), 0)
6074 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6075 0 : TREE_OPERAND (arg2, 0), 0))))
6076 1423 : switch (comp_code)
6077 : {
6078 0 : case EQ_EXPR:
6079 0 : case UNEQ_EXPR:
6080 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6081 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6082 0 : case NE_EXPR:
6083 0 : case LTGT_EXPR:
6084 0 : return fold_convert_loc (loc, type, arg1);
6085 0 : case UNGE_EXPR:
6086 0 : case UNGT_EXPR:
6087 0 : if (flag_trapping_math)
6088 : break;
6089 : /* Fall through. */
6090 1407 : case GE_EXPR:
6091 1407 : case GT_EXPR:
6092 1407 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6093 : break;
6094 1391 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6095 1391 : return fold_convert_loc (loc, type, tem);
6096 0 : case UNLE_EXPR:
6097 0 : case UNLT_EXPR:
6098 0 : if (flag_trapping_math)
6099 : break;
6100 : /* FALLTHRU */
6101 16 : case LE_EXPR:
6102 16 : case LT_EXPR:
6103 16 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6104 : break;
6105 32 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6106 32 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6107 : {
6108 : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6109 : is not, invokes UB both in abs and in the negation of it.
6110 : So, use ABSU_EXPR instead. */
6111 16 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6112 16 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6113 16 : tem = negate_expr (tem);
6114 16 : return fold_convert_loc (loc, type, tem);
6115 : }
6116 : else
6117 : {
6118 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6119 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6120 : }
6121 0 : default:
6122 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6123 : break;
6124 : }
6125 :
6126 : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6127 : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6128 : both transformations are correct when A is NaN: A != 0
6129 : is then true, and A == 0 is false. */
6130 :
6131 516194 : if (!HONOR_SIGNED_ZEROS (type)
6132 516194 : && integer_zerop (arg01) && integer_zerop (arg2))
6133 : {
6134 265699 : if (comp_code == NE_EXPR)
6135 147 : return fold_convert_loc (loc, type, arg1);
6136 265552 : else if (comp_code == EQ_EXPR)
6137 0 : return build_zero_cst (type);
6138 : }
6139 :
6140 : /* Try some transformations of A op B ? A : B.
6141 :
6142 : A == B? A : B same as B
6143 : A != B? A : B same as A
6144 : A >= B? A : B same as max (A, B)
6145 : A > B? A : B same as max (B, A)
6146 : A <= B? A : B same as min (A, B)
6147 : A < B? A : B same as min (B, A)
6148 :
6149 : As above, these transformations don't work in the presence
6150 : of signed zeros. For example, if A and B are zeros of
6151 : opposite sign, the first two transformations will change
6152 : the sign of the result. In the last four, the original
6153 : expressions give different results for (A=+0, B=-0) and
6154 : (A=-0, B=+0), but the transformed expressions do not.
6155 :
6156 : The first two transformations are correct if either A or B
6157 : is a NaN. In the first transformation, the condition will
6158 : be false, and B will indeed be chosen. In the case of the
6159 : second transformation, the condition A != B will be true,
6160 : and A will be chosen.
6161 :
6162 : The conversions to max() and min() are not correct if B is
6163 : a number and A is not. The conditions in the original
6164 : expressions will be false, so all four give B. The min()
6165 : and max() versions would give a NaN instead. */
6166 516047 : if (!HONOR_SIGNED_ZEROS (type)
6167 516047 : && operand_equal_for_comparison_p (arg01, arg2)
6168 : /* Avoid these transformations if the COND_EXPR may be used
6169 : as an lvalue in the C++ front-end. PR c++/19199. */
6170 790562 : && (in_gimple_form
6171 17928 : || VECTOR_TYPE_P (type)
6172 17855 : || (! lang_GNU_CXX ()
6173 15372 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6174 2483 : || ! maybe_lvalue_p (arg1)
6175 2462 : || ! maybe_lvalue_p (arg2)))
6176 : {
6177 272786 : tree comp_op0 = arg00;
6178 272786 : tree comp_op1 = arg01;
6179 272786 : tree comp_type = TREE_TYPE (comp_op0);
6180 :
6181 272786 : switch (comp_code)
6182 : {
6183 0 : case EQ_EXPR:
6184 0 : return fold_convert_loc (loc, type, arg2);
6185 1 : case NE_EXPR:
6186 1 : return fold_convert_loc (loc, type, arg1);
6187 6113 : case LE_EXPR:
6188 6113 : case LT_EXPR:
6189 6113 : case UNLE_EXPR:
6190 6113 : case UNLT_EXPR:
6191 : /* In C++ a ?: expression can be an lvalue, so put the
6192 : operand which will be used if they are equal first
6193 : so that we can convert this back to the
6194 : corresponding COND_EXPR. */
6195 6113 : if (!HONOR_NANS (arg1))
6196 : {
6197 6113 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6198 6113 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6199 6113 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6200 6113 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6201 4748 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6202 : comp_op1, comp_op0);
6203 6113 : return fold_convert_loc (loc, type, tem);
6204 : }
6205 : break;
6206 266672 : case GE_EXPR:
6207 266672 : case GT_EXPR:
6208 266672 : case UNGE_EXPR:
6209 266672 : case UNGT_EXPR:
6210 266672 : if (!HONOR_NANS (arg1))
6211 : {
6212 266670 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6213 266670 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6214 266670 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6215 266670 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6216 3721 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6217 : comp_op1, comp_op0);
6218 266670 : return fold_convert_loc (loc, type, tem);
6219 : }
6220 : break;
6221 0 : case UNEQ_EXPR:
6222 0 : if (!HONOR_NANS (arg1))
6223 0 : return fold_convert_loc (loc, type, arg2);
6224 : break;
6225 0 : case LTGT_EXPR:
6226 0 : if (!HONOR_NANS (arg1))
6227 0 : return fold_convert_loc (loc, type, arg1);
6228 : break;
6229 0 : default:
6230 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6231 : break;
6232 : }
6233 : }
6234 :
6235 : return NULL_TREE;
6236 : }
6237 :
6238 :
6239 :
6240 : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6241 : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6242 : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6243 : false) >= 2)
6244 : #endif
6245 :
6246 : /* EXP is some logical combination of boolean tests. See if we can
6247 : merge it into some range test. Return the new tree if so. */
6248 :
6249 : static tree
6250 25242213 : fold_range_test (location_t loc, enum tree_code code, tree type,
6251 : tree op0, tree op1)
6252 : {
6253 25242213 : int or_op = (code == TRUTH_ORIF_EXPR
6254 25242213 : || code == TRUTH_OR_EXPR);
6255 25242213 : int in0_p, in1_p, in_p;
6256 25242213 : tree low0, low1, low, high0, high1, high;
6257 25242213 : tree tem, lhs, rhs;
6258 :
6259 25242213 : if (!INTEGRAL_TYPE_P (type))
6260 : return 0;
6261 :
6262 25242213 : lhs = make_range (op0, &in0_p, &low0, &high0);
6263 : /* If op0 is known true or false and this is a short-circuiting
6264 : operation we must not merge with op1 since that makes side-effects
6265 : unconditional. So special-case this. */
6266 25242213 : if (!lhs
6267 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6268 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6269 : return op0;
6270 25242211 : rhs = make_range (op1, &in1_p, &low1, &high1);
6271 :
6272 : /* If this is an OR operation, invert both sides; we will invert
6273 : again at the end. */
6274 25242211 : if (or_op)
6275 11865401 : in0_p = ! in0_p, in1_p = ! in1_p;
6276 :
6277 : /* If both expressions are the same, if we can merge the ranges, and we
6278 : can build the range test, return it or it inverted. If one of the
6279 : ranges is always true or always false, consider it to be the same
6280 : expression as the other. */
6281 25210519 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6282 1212277 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6283 : in1_p, low1, high1)
6284 26252596 : && (tem = (build_range_check (loc, type,
6285 : lhs != 0 ? lhs
6286 0 : : rhs != 0 ? rhs : integer_zero_node,
6287 : in_p, low, high))) != 0)
6288 : {
6289 1010355 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6290 : }
6291 :
6292 : /* On machines where the branch cost is expensive, if this is a
6293 : short-circuited branch and the underlying object on both sides
6294 : is the same, make a non-short-circuit operation. */
6295 24231856 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6296 24231856 : if (param_logical_op_non_short_circuit != -1)
6297 7865 : logical_op_non_short_circuit
6298 7865 : = param_logical_op_non_short_circuit;
6299 24231856 : if (logical_op_non_short_circuit
6300 24227893 : && !sanitize_coverage_p ()
6301 24227890 : && lhs != 0 && rhs != 0
6302 24227451 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6303 29818785 : && operand_equal_p (lhs, rhs, 0))
6304 : {
6305 : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6306 : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6307 : which cases we can't do this. */
6308 171835 : if (simple_operand_p (lhs))
6309 69576 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6310 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6311 35308 : type, op0, op1);
6312 :
6313 136527 : else if (!lang_hooks.decls.global_bindings_p ()
6314 136527 : && !CONTAINS_PLACEHOLDER_P (lhs))
6315 : {
6316 135874 : tree common = save_expr (lhs);
6317 :
6318 250018 : if ((lhs = build_range_check (loc, type, common,
6319 114144 : or_op ? ! in0_p : in0_p,
6320 : low0, high0)) != 0
6321 250018 : && (rhs = build_range_check (loc, type, common,
6322 114144 : or_op ? ! in1_p : in1_p,
6323 : low1, high1)) != 0)
6324 : {
6325 250018 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6326 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6327 135874 : type, lhs, rhs);
6328 : }
6329 : }
6330 : }
6331 :
6332 : return 0;
6333 : }
6334 :
6335 : /* For an expression that has the form
6336 : (A && B) || ~B
6337 : or
6338 : (A || B) && ~B,
6339 : we can drop one of the inner expressions and simplify to
6340 : A || ~B
6341 : or
6342 : A && ~B
6343 : LOC is the location of the resulting expression. OP is the inner
6344 : logical operation; the left-hand side in the examples above, while CMPOP
6345 : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6346 : removing a condition that guards another, as in
6347 : (A != NULL && A->...) || A == NULL
6348 : which we must not transform. If RHS_ONLY is true, only eliminate the
6349 : right-most operand of the inner logical operation. */
6350 :
6351 : static tree
6352 130422 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6353 : bool rhs_only)
6354 : {
6355 130422 : enum tree_code code = TREE_CODE (cmpop);
6356 130422 : enum tree_code truthop_code = TREE_CODE (op);
6357 130422 : tree lhs = TREE_OPERAND (op, 0);
6358 130422 : tree rhs = TREE_OPERAND (op, 1);
6359 130422 : tree orig_lhs = lhs, orig_rhs = rhs;
6360 130422 : enum tree_code rhs_code = TREE_CODE (rhs);
6361 130422 : enum tree_code lhs_code = TREE_CODE (lhs);
6362 130422 : enum tree_code inv_code;
6363 :
6364 130422 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6365 : return NULL_TREE;
6366 :
6367 116869 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6368 : return NULL_TREE;
6369 :
6370 38994 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6371 :
6372 38994 : if (rhs_code == truthop_code)
6373 : {
6374 37 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6375 37 : if (newrhs != NULL_TREE)
6376 : {
6377 0 : rhs = newrhs;
6378 0 : rhs_code = TREE_CODE (rhs);
6379 : }
6380 : }
6381 38994 : if (lhs_code == truthop_code && !rhs_only)
6382 : {
6383 480 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6384 480 : if (newlhs != NULL_TREE)
6385 : {
6386 0 : lhs = newlhs;
6387 0 : lhs_code = TREE_CODE (lhs);
6388 : }
6389 : }
6390 :
6391 38994 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6392 38994 : if (inv_code == rhs_code
6393 932 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6394 39030 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6395 : return lhs;
6396 38981 : if (!rhs_only && inv_code == lhs_code
6397 604 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6398 39073 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6399 : return rhs;
6400 38890 : if (rhs != orig_rhs || lhs != orig_lhs)
6401 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6402 0 : lhs, rhs);
6403 : return NULL_TREE;
6404 : }
6405 :
6406 : /* Find ways of folding logical expressions of LHS and RHS:
6407 : Try to merge two comparisons to the same innermost item.
6408 : Look for range tests like "ch >= '0' && ch <= '9'".
6409 : Look for combinations of simple terms on machines with expensive branches
6410 : and evaluate the RHS unconditionally.
6411 :
6412 : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6413 : function and the one above.
6414 :
6415 : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6416 : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6417 :
6418 : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6419 : two operands.
6420 :
6421 : We return the simplified tree or 0 if no optimization is possible. */
6422 :
6423 : static tree
6424 24910570 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6425 : tree lhs, tree rhs)
6426 : {
6427 : /* If this is the "or" of two comparisons, we can do something if
6428 : the comparisons are NE_EXPR. If this is the "and", we can do something
6429 : if the comparisons are EQ_EXPR. I.e.,
6430 : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6431 :
6432 : WANTED_CODE is this operation code. For single bit fields, we can
6433 : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6434 : comparison for one-bit fields. */
6435 :
6436 24910570 : enum tree_code lcode, rcode;
6437 24910570 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6438 24910570 : tree result;
6439 :
6440 : /* Start by getting the comparison codes. Fail if anything is volatile.
6441 : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6442 : it were surrounded with a NE_EXPR. */
6443 :
6444 24910570 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6445 : return 0;
6446 :
6447 21997318 : lcode = TREE_CODE (lhs);
6448 21997318 : rcode = TREE_CODE (rhs);
6449 :
6450 21997318 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6451 : {
6452 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6453 0 : build_int_cst (TREE_TYPE (lhs), 0));
6454 0 : lcode = NE_EXPR;
6455 : }
6456 :
6457 21997318 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6458 : {
6459 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6460 0 : build_int_cst (TREE_TYPE (rhs), 0));
6461 0 : rcode = NE_EXPR;
6462 : }
6463 :
6464 21997318 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6465 19650905 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6466 : return 0;
6467 :
6468 18556445 : ll_arg = TREE_OPERAND (lhs, 0);
6469 18556445 : lr_arg = TREE_OPERAND (lhs, 1);
6470 18556445 : rl_arg = TREE_OPERAND (rhs, 0);
6471 18556445 : rr_arg = TREE_OPERAND (rhs, 1);
6472 :
6473 : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6474 18556445 : if (simple_operand_p (ll_arg)
6475 18556445 : && simple_operand_p (lr_arg))
6476 : {
6477 15042799 : if (operand_equal_p (ll_arg, rl_arg, 0)
6478 15042799 : && operand_equal_p (lr_arg, rr_arg, 0))
6479 : {
6480 21098 : result = combine_comparisons (loc, code, lcode, rcode,
6481 : truth_type, ll_arg, lr_arg);
6482 21098 : if (result)
6483 : return result;
6484 : }
6485 15021701 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6486 15021701 : && operand_equal_p (lr_arg, rl_arg, 0))
6487 : {
6488 252 : result = combine_comparisons (loc, code, lcode,
6489 : swap_tree_comparison (rcode),
6490 : truth_type, ll_arg, lr_arg);
6491 252 : if (result)
6492 : return result;
6493 : }
6494 : }
6495 :
6496 8757373 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6497 18535533 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6498 :
6499 : /* If the RHS can be evaluated unconditionally and its operands are
6500 : simple, it wins to evaluate the RHS unconditionally on machines
6501 : with expensive branches. In this case, this isn't a comparison
6502 : that can be merged. */
6503 :
6504 18535533 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6505 : false) >= 2
6506 18535430 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6507 17484703 : && simple_operand_p (rl_arg)
6508 28523122 : && simple_operand_p (rr_arg))
6509 : {
6510 : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6511 11180734 : if (code == TRUTH_OR_EXPR
6512 1518823 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6513 622275 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6514 21042 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6515 11198360 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6516 34636 : return build2_loc (loc, NE_EXPR, truth_type,
6517 17318 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6518 : ll_arg, rl_arg),
6519 17318 : build_int_cst (TREE_TYPE (ll_arg), 0));
6520 :
6521 : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6522 11163416 : if (code == TRUTH_AND_EXPR
6523 1717873 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6524 826033 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6525 8230 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6526 11165042 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6527 2810 : return build2_loc (loc, EQ_EXPR, truth_type,
6528 1405 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6529 : ll_arg, rl_arg),
6530 1405 : build_int_cst (TREE_TYPE (ll_arg), 0));
6531 : }
6532 :
6533 : return 0;
6534 : }
6535 :
6536 : /* T is an integer expression that is being multiplied, divided, or taken a
6537 : modulus (CODE says which and what kind of divide or modulus) by a
6538 : constant C. See if we can eliminate that operation by folding it with
6539 : other operations already in T. WIDE_TYPE, if non-null, is a type that
6540 : should be used for the computation if wider than our type.
6541 :
6542 : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6543 : (X * 2) + (Y * 4). We must, however, be assured that either the original
6544 : expression would not overflow or that overflow is undefined for the type
6545 : in the language in question.
6546 :
6547 : If we return a non-null expression, it is an equivalent form of the
6548 : original computation, but need not be in the original type. */
6549 :
6550 : static tree
6551 100180231 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
6552 : {
6553 : /* To avoid exponential search depth, refuse to allow recursion past
6554 : three levels. Beyond that (1) it's highly unlikely that we'll find
6555 : something interesting and (2) we've probably processed it before
6556 : when we built the inner expression. */
6557 :
6558 100180231 : static int depth;
6559 100180231 : tree ret;
6560 :
6561 100180231 : if (depth > 3)
6562 : return NULL;
6563 :
6564 96491350 : depth++;
6565 96491350 : ret = extract_muldiv_1 (t, c, code, wide_type);
6566 96491350 : depth--;
6567 :
6568 96491350 : return ret;
6569 : }
6570 :
6571 : static tree
6572 96491350 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
6573 : {
6574 96491350 : tree type = TREE_TYPE (t);
6575 96491350 : enum tree_code tcode = TREE_CODE (t);
6576 96491350 : tree ctype = type;
6577 96491350 : if (wide_type)
6578 : {
6579 32120421 : if (BITINT_TYPE_P (type) || BITINT_TYPE_P (wide_type))
6580 : {
6581 142 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6582 96491350 : ctype = wide_type;
6583 : }
6584 32120279 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6585 64240558 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6586 96491350 : ctype = wide_type;
6587 : }
6588 96491350 : tree t1, t2;
6589 96491350 : bool same_p = tcode == code;
6590 96491350 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6591 :
6592 : /* Don't deal with constants of zero here; they confuse the code below. */
6593 96491350 : if (integer_zerop (c))
6594 : return NULL_TREE;
6595 :
6596 96475840 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6597 38296641 : op0 = TREE_OPERAND (t, 0);
6598 :
6599 96475840 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6600 12179234 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6601 :
6602 : /* Note that we need not handle conditional operations here since fold
6603 : already handles those cases. So just do arithmetic here. */
6604 96475840 : switch (tcode)
6605 : {
6606 4348279 : case INTEGER_CST:
6607 : /* For a constant, we can always simplify if we are a multiply
6608 : or (for divide and modulus) if it is a multiple of our constant. */
6609 4348279 : if (code == MULT_EXPR
6610 5579300 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6611 1231021 : TYPE_SIGN (type)))
6612 : {
6613 3550247 : tree tem = const_binop (code, fold_convert (ctype, t),
6614 : fold_convert (ctype, c));
6615 : /* If the multiplication overflowed, we lost information on it.
6616 : See PR68142 and PR69845. */
6617 3550247 : if (TREE_OVERFLOW (tem))
6618 : return NULL_TREE;
6619 3546724 : return tem;
6620 : }
6621 : break;
6622 :
6623 37739604 : CASE_CONVERT: case NON_LVALUE_EXPR:
6624 37739604 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6625 : break;
6626 : /* If op0 is an expression ... */
6627 36437347 : if ((COMPARISON_CLASS_P (op0)
6628 : || UNARY_CLASS_P (op0)
6629 36437347 : || BINARY_CLASS_P (op0)
6630 33399970 : || VL_EXP_CLASS_P (op0)
6631 33338316 : || EXPRESSION_CLASS_P (op0))
6632 : /* ... and has wrapping overflow, and its type is smaller
6633 : than ctype, then we cannot pass through as widening. */
6634 36588529 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6635 1235003 : && (TYPE_PRECISION (ctype)
6636 1235003 : > TYPE_PRECISION (TREE_TYPE (op0))))
6637 : /* ... or this is a truncation (t is narrower than op0),
6638 : then we cannot pass through this narrowing. */
6639 2632193 : || (TYPE_PRECISION (type)
6640 2632193 : < TYPE_PRECISION (TREE_TYPE (op0)))
6641 : /* ... or signedness changes for division or modulus,
6642 : then we cannot pass through this conversion. */
6643 2603256 : || (code != MULT_EXPR
6644 124986 : && (TYPE_UNSIGNED (ctype)
6645 124986 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6646 : /* ... or has undefined overflow while the converted to
6647 : type has not, we cannot do the operation in the inner type
6648 : as that would introduce undefined overflow. */
6649 2504926 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6650 1847803 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6651 : break;
6652 :
6653 : /* Pass the constant down and see if we can make a simplification. If
6654 : we can, replace this expression with the inner simplification for
6655 : possible later conversion to our or some other type. */
6656 33976548 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6657 33976548 : && TREE_CODE (t2) == INTEGER_CST
6658 33976548 : && !TREE_OVERFLOW (t2)
6659 69193330 : && (t1 = extract_muldiv (op0, t2, code,
6660 : code == MULT_EXPR ? ctype : NULL_TREE))
6661 : != 0)
6662 : return t1;
6663 : break;
6664 :
6665 189 : case ABS_EXPR:
6666 : /* If widening the type changes it from signed to unsigned, then we
6667 : must avoid building ABS_EXPR itself as unsigned. */
6668 189 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6669 : {
6670 0 : tree cstype = (*signed_type_for) (ctype);
6671 0 : if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
6672 : {
6673 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6674 0 : return fold_convert (ctype, t1);
6675 : }
6676 : break;
6677 : }
6678 : /* If the constant is negative, we cannot simplify this. */
6679 189 : if (tree_int_cst_sgn (c) == -1)
6680 : break;
6681 : /* FALLTHROUGH */
6682 47150 : case NEGATE_EXPR:
6683 : /* For division and modulus, type can't be unsigned, as e.g.
6684 : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6685 : For signed types, even with wrapping overflow, this is fine. */
6686 47150 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6687 : break;
6688 45386 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6689 1 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6690 : break;
6691 :
6692 798 : case MIN_EXPR: case MAX_EXPR:
6693 : /* If widening the type changes the signedness, then we can't perform
6694 : this optimization as that changes the result. */
6695 798 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6696 : break;
6697 :
6698 : /* Punt for multiplication altogether.
6699 : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6700 : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6701 : 0U, the latter is 2U.
6702 : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6703 : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6704 : well defined 0, the latter invokes UB.
6705 : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6706 : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6707 : well defined 25, the latter invokes UB. */
6708 798 : if (code == MULT_EXPR)
6709 : break;
6710 : /* For division/modulo, punt on c being -1 for MAX, as
6711 : MAX (INT_MIN, 0) / -1 is not equivalent to
6712 : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6713 : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6714 : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6715 : transformation won't make it worse. */
6716 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6717 : break;
6718 :
6719 : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6720 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
6721 8 : && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
6722 : {
6723 0 : if (tree_int_cst_sgn (c) < 0)
6724 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6725 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6726 : fold_convert (ctype, t2));
6727 : }
6728 : break;
6729 :
6730 1371 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6731 : /* If the second operand is constant, this is a multiplication
6732 : or floor division, by a power of two, so we can treat it that
6733 : way unless the multiplier or divisor overflows. Signed
6734 : left-shift overflow is implementation-defined rather than
6735 : undefined in C90, so do not convert signed left shift into
6736 : multiplication. */
6737 1371 : if (TREE_CODE (op1) == INTEGER_CST
6738 1355 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6739 : /* const_binop may not detect overflow correctly,
6740 : so check for it explicitly here. */
6741 1237 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6742 1380 : wi::to_wide (op1))
6743 1228 : && (t1 = fold_convert (ctype,
6744 : const_binop (LSHIFT_EXPR, size_one_node,
6745 : op1))) != 0
6746 2599 : && !TREE_OVERFLOW (t1))
6747 2254 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6748 : ? MULT_EXPR : FLOOR_DIV_EXPR,
6749 : ctype,
6750 : fold_convert (ctype, op0),
6751 : t1),
6752 1228 : c, code, wide_type);
6753 : break;
6754 :
6755 8406773 : case PLUS_EXPR: case MINUS_EXPR:
6756 : /* See if we can eliminate the operation on both sides. If we can, we
6757 : can return a new PLUS or MINUS. If we can't, the only remaining
6758 : cases where we can do anything are if the second operand is a
6759 : constant. */
6760 8406773 : t1 = extract_muldiv (op0, c, code, wide_type);
6761 8406773 : t2 = extract_muldiv (op1, c, code, wide_type);
6762 820097 : if (t1 != 0 && t2 != 0
6763 284087 : && TYPE_OVERFLOW_WRAPS (ctype)
6764 8681579 : && (code == MULT_EXPR
6765 : /* If not multiplication, we can only do this if both operands
6766 : are divisible by c. */
6767 0 : || (multiple_of_p (ctype, op0, c)
6768 0 : && multiple_of_p (ctype, op1, c))))
6769 : {
6770 274806 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6771 : fold_convert (ctype, t2));
6772 : }
6773 :
6774 : /* If this was a subtraction, negate OP1 and set it to be an addition.
6775 : This simplifies the logic below. */
6776 8131967 : if (tcode == MINUS_EXPR)
6777 : {
6778 2206166 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6779 : /* If OP1 was not easily negatable, the constant may be OP0. */
6780 2206166 : if (TREE_CODE (op0) == INTEGER_CST)
6781 : {
6782 367816 : std::swap (op0, op1);
6783 367816 : std::swap (t1, t2);
6784 : }
6785 : }
6786 :
6787 8131967 : if (TREE_CODE (op1) != INTEGER_CST)
6788 : break;
6789 :
6790 : /* If either OP1 or C are negative, this optimization is not safe for
6791 : some of the division and remainder types while for others we need
6792 : to change the code. */
6793 3717612 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6794 : {
6795 176674 : if (code == CEIL_DIV_EXPR)
6796 : code = FLOOR_DIV_EXPR;
6797 176672 : else if (code == FLOOR_DIV_EXPR)
6798 : code = CEIL_DIV_EXPR;
6799 176235 : else if (code != MULT_EXPR
6800 176235 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6801 : break;
6802 : }
6803 :
6804 : /* If it's a multiply or a division/modulus operation of a multiple
6805 : of our constant, do the operation and verify it doesn't overflow. */
6806 3712100 : if (code == MULT_EXPR
6807 4938369 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6808 1226269 : TYPE_SIGN (type)))
6809 : {
6810 2914834 : op1 = const_binop (code, fold_convert (ctype, op1),
6811 : fold_convert (ctype, c));
6812 : /* We allow the constant to overflow with wrapping semantics. */
6813 2914834 : if (op1 == 0
6814 2914834 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6815 : break;
6816 : }
6817 : else
6818 : break;
6819 :
6820 : /* If we have an unsigned type, we cannot widen the operation since it
6821 : will change the result if the original computation overflowed. */
6822 2911313 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6823 : break;
6824 :
6825 : /* The last case is if we are a multiply. In that case, we can
6826 : apply the distributive law to commute the multiply and addition
6827 : if the multiplication of the constants doesn't overflow
6828 : and overflow is defined. With undefined overflow
6829 : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6830 : But fold_plusminus_mult_expr would factor back any power-of-two
6831 : value so do not distribute in the first place in this case. */
6832 2911313 : if (code == MULT_EXPR
6833 2483069 : && TYPE_OVERFLOW_WRAPS (ctype)
6834 5059256 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6835 599830 : return fold_build2 (tcode, ctype,
6836 : fold_build2 (code, ctype,
6837 : fold_convert (ctype, op0),
6838 : fold_convert (ctype, c)),
6839 : op1);
6840 :
6841 : break;
6842 :
6843 2371420 : case MULT_EXPR:
6844 : /* We have a special case here if we are doing something like
6845 : (C * 8) % 4 since we know that's zero. */
6846 2371420 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6847 2371420 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6848 : /* If the multiplication can overflow we cannot optimize this. */
6849 10816 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6850 338 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6851 2382236 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6852 293 : TYPE_SIGN (type)))
6853 : {
6854 8 : return omit_one_operand (type, integer_zero_node, op0);
6855 : }
6856 :
6857 : /* ... fall through ... */
6858 :
6859 2660940 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6860 2660940 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6861 : /* If we can extract our operation from the LHS, do so and return a
6862 : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6863 : do something only if the second operand is a constant. */
6864 2660940 : if (same_p
6865 2238636 : && TYPE_OVERFLOW_WRAPS (ctype)
6866 4727480 : && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
6867 66822 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6868 : fold_convert (ctype, op1));
6869 2594118 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6870 2162364 : && TYPE_OVERFLOW_WRAPS (ctype)
6871 4584434 : && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
6872 943659 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6873 : fold_convert (ctype, t1));
6874 1650459 : else if (TREE_CODE (op1) != INTEGER_CST)
6875 : return 0;
6876 :
6877 : /* If these are the same operation types, we can associate them
6878 : assuming no overflow. */
6879 623272 : if (tcode == code)
6880 : {
6881 201529 : bool overflow_p = false;
6882 201529 : wi::overflow_type overflow_mul;
6883 201529 : signop sign = TYPE_SIGN (ctype);
6884 201529 : unsigned prec = TYPE_PRECISION (ctype);
6885 403058 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6886 201529 : wi::to_wide (c, prec),
6887 201529 : sign, &overflow_mul);
6888 201529 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6889 201529 : if (overflow_mul
6890 1549 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6891 : overflow_p = true;
6892 201462 : if (!overflow_p)
6893 201462 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6894 : wide_int_to_tree (ctype, mul));
6895 201529 : }
6896 :
6897 : /* If these operations "cancel" each other, we have the main
6898 : optimizations of this pass, which occur when either constant is a
6899 : multiple of the other, in which case we replace this with either an
6900 : operation or CODE or TCODE.
6901 :
6902 : If we have an unsigned type, we cannot do this since it will change
6903 : the result if the original computation overflowed. */
6904 421810 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6905 101079 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6906 522846 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6907 100732 : || (tcode == MULT_EXPR
6908 100732 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6909 850 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6910 846 : && code != MULT_EXPR)))
6911 : {
6912 1144 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6913 1144 : TYPE_SIGN (type)))
6914 : {
6915 124 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6916 : fold_convert (ctype,
6917 : const_binop (TRUNC_DIV_EXPR,
6918 : op1, c)));
6919 : }
6920 1020 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6921 1020 : TYPE_SIGN (type)))
6922 : {
6923 304 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6924 : fold_convert (ctype,
6925 : const_binop (TRUNC_DIV_EXPR,
6926 : c, op1)));
6927 : }
6928 : }
6929 : break;
6930 :
6931 : default:
6932 : break;
6933 : }
6934 :
6935 : return 0;
6936 : }
6937 :
6938 : /* Return a node which has the indicated constant VALUE (either 0 or
6939 : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6940 : and is of the indicated TYPE. */
6941 :
6942 : tree
6943 120167724 : constant_boolean_node (bool value, tree type)
6944 : {
6945 120167724 : if (type == integer_type_node)
6946 20756439 : return value ? integer_one_node : integer_zero_node;
6947 99411285 : else if (type == boolean_type_node)
6948 94945988 : return value ? boolean_true_node : boolean_false_node;
6949 4465297 : else if (VECTOR_TYPE_P (type))
6950 1995 : return build_vector_from_val (type,
6951 1995 : build_int_cst (TREE_TYPE (type),
6952 2720 : value ? -1 : 0));
6953 : else
6954 4463302 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6955 : }
6956 :
6957 :
6958 : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6959 : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6960 : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6961 : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6962 : COND is the first argument to CODE; otherwise (as in the example
6963 : given here), it is the second argument. TYPE is the type of the
6964 : original expression. Return NULL_TREE if no simplification is
6965 : possible. */
6966 :
6967 : static tree
6968 970878 : fold_binary_op_with_conditional_arg (location_t loc,
6969 : enum tree_code code,
6970 : tree type, tree op0, tree op1,
6971 : tree cond, tree arg, int cond_first_p)
6972 : {
6973 970878 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6974 970878 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6975 970878 : tree test, true_value, false_value;
6976 970878 : tree lhs = NULL_TREE;
6977 970878 : tree rhs = NULL_TREE;
6978 970878 : enum tree_code cond_code = COND_EXPR;
6979 :
6980 : /* Do not move possibly trapping operations into the conditional as this
6981 : pessimizes code and causes gimplification issues when applied late. */
6982 990734 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6983 970878 : ANY_INTEGRAL_TYPE_P (type)
6984 776797 : && TYPE_OVERFLOW_TRAPS (type), op1))
6985 : return NULL_TREE;
6986 :
6987 950848 : if (TREE_CODE (cond) == COND_EXPR
6988 358231 : || TREE_CODE (cond) == VEC_COND_EXPR)
6989 : {
6990 596410 : test = TREE_OPERAND (cond, 0);
6991 596410 : true_value = TREE_OPERAND (cond, 1);
6992 596410 : false_value = TREE_OPERAND (cond, 2);
6993 : /* If this operand throws an expression, then it does not make
6994 : sense to try to perform a logical or arithmetic operation
6995 : involving it. */
6996 596410 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
6997 7463 : lhs = true_value;
6998 596410 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
6999 6 : rhs = false_value;
7000 : }
7001 354438 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7002 354332 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7003 : {
7004 352369 : tree testtype = TREE_TYPE (cond);
7005 352369 : test = cond;
7006 352369 : true_value = constant_boolean_node (true, testtype);
7007 352369 : false_value = constant_boolean_node (false, testtype);
7008 : }
7009 : else
7010 : /* Detect the case of mixing vector and scalar types - bail out. */
7011 : return NULL_TREE;
7012 :
7013 948779 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7014 3899 : cond_code = VEC_COND_EXPR;
7015 :
7016 : /* This transformation is only worthwhile if we don't have to wrap ARG
7017 : in a SAVE_EXPR and the operation can be simplified without recursing
7018 : on at least one of the branches once its pushed inside the COND_EXPR. */
7019 948779 : if (!TREE_CONSTANT (arg)
7020 948779 : && (TREE_SIDE_EFFECTS (arg)
7021 446544 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7022 441844 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7023 : return NULL_TREE;
7024 :
7025 518712 : arg = fold_convert_loc (loc, arg_type, arg);
7026 518712 : if (lhs == 0)
7027 : {
7028 512681 : true_value = fold_convert_loc (loc, cond_type, true_value);
7029 512681 : if (cond_first_p)
7030 502427 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7031 : else
7032 10254 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7033 : }
7034 518712 : if (rhs == 0)
7035 : {
7036 518706 : false_value = fold_convert_loc (loc, cond_type, false_value);
7037 518706 : if (cond_first_p)
7038 507891 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7039 : else
7040 10815 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7041 : }
7042 :
7043 : /* Check that we have simplified at least one of the branches. */
7044 518712 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7045 : return NULL_TREE;
7046 :
7047 497962 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7048 : }
7049 :
7050 :
7051 : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7052 :
7053 : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7054 : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7055 : if ARG - ZERO_ARG is the same as X.
7056 :
7057 : If ARG is NULL, check for any value of type TYPE.
7058 :
7059 : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7060 : and finite. The problematic cases are when X is zero, and its mode
7061 : has signed zeros. In the case of rounding towards -infinity,
7062 : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7063 : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7064 :
7065 : bool
7066 637089 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7067 : const_tree zero_arg, int negate)
7068 : {
7069 637089 : if (!real_zerop (zero_arg))
7070 : return false;
7071 :
7072 : /* Don't allow the fold with -fsignaling-nans. */
7073 636297 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7074 : return false;
7075 :
7076 : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7077 633095 : if (!HONOR_SIGNED_ZEROS (type))
7078 : return true;
7079 :
7080 : /* There is no case that is safe for all rounding modes. */
7081 616052 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7082 : return false;
7083 :
7084 : /* In a vector or complex, we would need to check the sign of all zeros. */
7085 615389 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7086 2849 : zero_arg = uniform_vector_p (zero_arg);
7087 615389 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7088 : return false;
7089 :
7090 : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7091 614085 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7092 210 : negate = !negate;
7093 :
7094 : /* The mode has signed zeros, and we have to honor their sign.
7095 : In this situation, there are only two cases we can return true for.
7096 : (i) X - 0 is the same as X with default rounding.
7097 : (ii) X + 0 is X when X can't possibly be -0.0. */
7098 614085 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7099 : }
7100 :
7101 : /* Subroutine of match.pd that determines if it is safe to optimize
7102 : a floating point comparison of an integer value, known to be between
7103 : LO and HI, using comparison operator CMP, against the real constant
7104 : R in floating point type FMT, as the same integer comparison against
7105 : the integer constant I, with sign ISIGN.
7106 :
7107 : For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
7108 : because the floating point representations of the neighboring integers
7109 : (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
7110 : 3.0f respectively. On the other hand (float)x == 16777220.0f can't
7111 : be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
7112 : due to truncation/rounding.
7113 : */
7114 : bool
7115 21398 : fold_cmp_float_cst_p (wide_int lo, wide_int hi, enum tree_code cmp,
7116 : const REAL_VALUE_TYPE *r, format_helper fmt,
7117 : wide_int i, signop isign)
7118 : {
7119 21398 : REAL_VALUE_TYPE raw;
7120 21398 : REAL_VALUE_TYPE rnd;
7121 21398 : bool check_im1 = true;
7122 21398 : bool check_ip1 = true;
7123 :
7124 21398 : switch (cmp)
7125 : {
7126 : case EQ_EXPR:
7127 : case NE_EXPR:
7128 : /* Check both i-1 and i+1. */
7129 : break;
7130 :
7131 19813 : case LT_EXPR:
7132 19813 : case GE_EXPR:
7133 : /* Only check i-1. */
7134 19813 : check_ip1 = false;
7135 19813 : break;
7136 :
7137 356 : case LE_EXPR:
7138 356 : case GT_EXPR:
7139 : /* Only check i+1. */
7140 356 : check_im1 = false;
7141 356 : break;
7142 :
7143 : default:
7144 : return false;
7145 : }
7146 :
7147 21398 : if (flag_rounding_math && i != 0)
7148 : {
7149 344 : real_from_integer (&raw, VOIDmode, i, isign);
7150 344 : if (!real_identical (r, &raw))
7151 : return false;
7152 : }
7153 :
7154 21398 : if (check_im1 && wi::gt_p (i, lo, isign))
7155 : {
7156 1300 : if (flag_rounding_math)
7157 : {
7158 344 : real_from_integer (&raw, VOIDmode, i - 1, isign);
7159 344 : real_convert (&rnd, fmt, &raw);
7160 344 : if (!real_identical (&raw, &rnd))
7161 : return false;
7162 : }
7163 : else
7164 956 : real_from_integer (&rnd, fmt, i - 1, isign);
7165 956 : if (real_identical (r, &rnd))
7166 : return false;
7167 : }
7168 :
7169 20710 : if (check_ip1 && wi::lt_p (i, hi, isign))
7170 : {
7171 875 : if (flag_rounding_math)
7172 : {
7173 0 : real_from_integer (&raw, VOIDmode, i + 1, isign);
7174 0 : real_convert (&rnd, fmt, &raw);
7175 0 : if (!real_identical (&raw, &rnd))
7176 : return false;
7177 : }
7178 : else
7179 875 : real_from_integer (&rnd, fmt, i + 1, isign);
7180 875 : if (real_identical (r, &rnd))
7181 0 : return false;
7182 : }
7183 :
7184 : return true;
7185 : }
7186 :
7187 : /* Subroutine of match.pd that optimizes comparisons of a division by
7188 : a nonzero integer constant against an integer constant, i.e.
7189 : X/C1 op C2.
7190 :
7191 : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7192 : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7193 :
7194 : enum tree_code
7195 1721906 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7196 : tree *hi, bool *neg_overflow)
7197 : {
7198 1721906 : tree prod, tmp, type = TREE_TYPE (c1);
7199 1721906 : signop sign = TYPE_SIGN (type);
7200 1721906 : wi::overflow_type overflow;
7201 :
7202 : /* We have to do this the hard way to detect unsigned overflow.
7203 : prod = int_const_binop (MULT_EXPR, c1, c2); */
7204 1721906 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7205 1721906 : prod = force_fit_type (type, val, -1, overflow);
7206 1721906 : *neg_overflow = false;
7207 :
7208 1721906 : if (sign == UNSIGNED)
7209 : {
7210 1692131 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7211 1692131 : *lo = prod;
7212 :
7213 : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7214 1692131 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7215 1692131 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7216 : }
7217 29775 : else if (tree_int_cst_sgn (c1) >= 0)
7218 : {
7219 28376 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7220 28376 : switch (tree_int_cst_sgn (c2))
7221 : {
7222 4914 : case -1:
7223 4914 : *neg_overflow = true;
7224 4914 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7225 4914 : *hi = prod;
7226 4914 : break;
7227 :
7228 14860 : case 0:
7229 14860 : *lo = fold_negate_const (tmp, type);
7230 14860 : *hi = tmp;
7231 14860 : break;
7232 :
7233 8602 : case 1:
7234 8602 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7235 8602 : *lo = prod;
7236 8602 : break;
7237 :
7238 0 : default:
7239 0 : gcc_unreachable ();
7240 : }
7241 : }
7242 : else
7243 : {
7244 : /* A negative divisor reverses the relational operators. */
7245 1399 : code = swap_tree_comparison (code);
7246 :
7247 1399 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7248 1399 : switch (tree_int_cst_sgn (c2))
7249 : {
7250 134 : case -1:
7251 134 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7252 134 : *lo = prod;
7253 134 : break;
7254 :
7255 167 : case 0:
7256 167 : *hi = fold_negate_const (tmp, type);
7257 167 : *lo = tmp;
7258 167 : break;
7259 :
7260 1098 : case 1:
7261 1098 : *neg_overflow = true;
7262 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7263 1098 : *hi = prod;
7264 1098 : break;
7265 :
7266 0 : default:
7267 0 : gcc_unreachable ();
7268 : }
7269 : }
7270 :
7271 1721906 : if (code != EQ_EXPR && code != NE_EXPR)
7272 : return code;
7273 :
7274 16763 : if (TREE_OVERFLOW (*lo)
7275 16763 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7276 733 : *lo = NULL_TREE;
7277 16763 : if (TREE_OVERFLOW (*hi)
7278 16763 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7279 95 : *hi = NULL_TREE;
7280 :
7281 : return code;
7282 1721906 : }
7283 :
7284 : /* Test whether it is preferable to swap two operands, ARG0 and
7285 : ARG1, for example because ARG0 is an integer constant and ARG1
7286 : isn't. */
7287 :
7288 : bool
7289 1648654371 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7290 : {
7291 1648654371 : if (CONSTANT_CLASS_P (arg1))
7292 : return false;
7293 539542262 : if (CONSTANT_CLASS_P (arg0))
7294 : return true;
7295 :
7296 498116250 : STRIP_NOPS (arg0);
7297 498116250 : STRIP_NOPS (arg1);
7298 :
7299 498116250 : if (TREE_CONSTANT (arg1))
7300 : return false;
7301 480090258 : if (TREE_CONSTANT (arg0))
7302 : return true;
7303 :
7304 : /* Put addresses in arg1. */
7305 479308386 : if (TREE_CODE (arg1) == ADDR_EXPR)
7306 : return false;
7307 459660856 : if (TREE_CODE (arg0) == ADDR_EXPR)
7308 : return true;
7309 :
7310 : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7311 : for commutative and comparison operators. Ensuring a canonical
7312 : form allows the optimizers to find additional redundancies without
7313 : having to explicitly check for both orderings. */
7314 459263385 : if (TREE_CODE (arg0) == SSA_NAME
7315 348473236 : && TREE_CODE (arg1) == SSA_NAME
7316 801836065 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7317 : return true;
7318 :
7319 : /* Put SSA_NAMEs last. */
7320 435972849 : if (TREE_CODE (arg1) == SSA_NAME)
7321 : return false;
7322 101603298 : if (TREE_CODE (arg0) == SSA_NAME)
7323 : return true;
7324 :
7325 : /* Put variables last. */
7326 95702742 : if (DECL_P (arg1))
7327 : return false;
7328 51780062 : if (DECL_P (arg0))
7329 6058124 : return true;
7330 :
7331 : return false;
7332 : }
7333 :
7334 :
7335 : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7336 : means A >= Y && A != MAX, but in this case we know that
7337 : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7338 :
7339 : static tree
7340 24166409 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7341 : {
7342 24166409 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7343 :
7344 24166409 : if (TREE_CODE (bound) == LT_EXPR)
7345 4992423 : a = TREE_OPERAND (bound, 0);
7346 19173986 : else if (TREE_CODE (bound) == GT_EXPR)
7347 2821421 : a = TREE_OPERAND (bound, 1);
7348 : else
7349 : return NULL_TREE;
7350 :
7351 7813844 : typea = TREE_TYPE (a);
7352 7813844 : if (!INTEGRAL_TYPE_P (typea)
7353 495973 : && !POINTER_TYPE_P (typea))
7354 : return NULL_TREE;
7355 :
7356 7634423 : if (TREE_CODE (ineq) == LT_EXPR)
7357 : {
7358 1474845 : a1 = TREE_OPERAND (ineq, 1);
7359 1474845 : y = TREE_OPERAND (ineq, 0);
7360 : }
7361 6159578 : else if (TREE_CODE (ineq) == GT_EXPR)
7362 : {
7363 1174987 : a1 = TREE_OPERAND (ineq, 0);
7364 1174987 : y = TREE_OPERAND (ineq, 1);
7365 : }
7366 : else
7367 : return NULL_TREE;
7368 :
7369 2649832 : if (TREE_TYPE (a1) != typea)
7370 : return NULL_TREE;
7371 :
7372 1886911 : if (POINTER_TYPE_P (typea))
7373 : {
7374 : /* Convert the pointer types into integer before taking the difference. */
7375 11596 : tree ta = fold_convert_loc (loc, ssizetype, a);
7376 11596 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7377 11596 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7378 : }
7379 : else
7380 1875315 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7381 :
7382 1886911 : if (!diff || !integer_onep (diff))
7383 : return NULL_TREE;
7384 :
7385 8901 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7386 : }
7387 :
7388 : /* Fold a sum or difference of at least one multiplication.
7389 : Returns the folded tree or NULL if no simplification could be made. */
7390 :
7391 : static tree
7392 9187510 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7393 : tree arg0, tree arg1)
7394 : {
7395 9187510 : tree arg00, arg01, arg10, arg11;
7396 9187510 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7397 :
7398 : /* (A * C) +- (B * C) -> (A+-B) * C.
7399 : (A * C) +- A -> A * (C+-1).
7400 : We are most concerned about the case where C is a constant,
7401 : but other combinations show up during loop reduction. Since
7402 : it is not difficult, try all four possibilities. */
7403 :
7404 9187510 : if (TREE_CODE (arg0) == MULT_EXPR)
7405 : {
7406 8186631 : arg00 = TREE_OPERAND (arg0, 0);
7407 8186631 : arg01 = TREE_OPERAND (arg0, 1);
7408 : }
7409 1000879 : else if (TREE_CODE (arg0) == INTEGER_CST)
7410 : {
7411 69108 : arg00 = build_one_cst (type);
7412 69108 : arg01 = arg0;
7413 : }
7414 : else
7415 : {
7416 : /* We cannot generate constant 1 for fract. */
7417 931771 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7418 : return NULL_TREE;
7419 931771 : arg00 = arg0;
7420 931771 : arg01 = build_one_cst (type);
7421 : }
7422 9187510 : if (TREE_CODE (arg1) == MULT_EXPR)
7423 : {
7424 2413412 : arg10 = TREE_OPERAND (arg1, 0);
7425 2413412 : arg11 = TREE_OPERAND (arg1, 1);
7426 : }
7427 6774098 : else if (TREE_CODE (arg1) == INTEGER_CST)
7428 : {
7429 3518757 : arg10 = build_one_cst (type);
7430 : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7431 : the purpose of this canonicalization. */
7432 6806519 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7433 234283 : && negate_expr_p (arg1)
7434 3749752 : && code == PLUS_EXPR)
7435 : {
7436 230995 : arg11 = negate_expr (arg1);
7437 230995 : code = MINUS_EXPR;
7438 : }
7439 : else
7440 : arg11 = arg1;
7441 : }
7442 : else
7443 : {
7444 : /* We cannot generate constant 1 for fract. */
7445 3255341 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7446 : return NULL_TREE;
7447 3255341 : arg10 = arg1;
7448 3255341 : arg11 = build_one_cst (type);
7449 : }
7450 9187510 : same = NULL_TREE;
7451 :
7452 : /* Prefer factoring a common non-constant. */
7453 9187510 : if (operand_equal_p (arg00, arg10, 0))
7454 : same = arg00, alt0 = arg01, alt1 = arg11;
7455 9183763 : else if (operand_equal_p (arg01, arg11, 0))
7456 : same = arg01, alt0 = arg00, alt1 = arg10;
7457 9076522 : else if (operand_equal_p (arg00, arg11, 0))
7458 : same = arg00, alt0 = arg01, alt1 = arg10;
7459 9076460 : else if (operand_equal_p (arg01, arg10, 0))
7460 : same = arg01, alt0 = arg00, alt1 = arg11;
7461 :
7462 : /* No identical multiplicands; see if we can find a common
7463 : power-of-two factor in non-power-of-two multiplies. This
7464 : can help in multi-dimensional array access. */
7465 9071104 : else if (TREE_CODE (arg01) == INTEGER_CST
7466 7970124 : && TREE_CODE (arg11) == INTEGER_CST)
7467 : {
7468 7743371 : wide_int int01 = wi::to_wide (arg01);
7469 7743371 : wide_int int11 = wi::to_wide (arg11);
7470 7743371 : bool swap = false;
7471 7743371 : tree maybe_same;
7472 :
7473 : /* Move min of absolute values to int11. */
7474 7743373 : if (wi::ltu_p (wi::abs (int01), wi::abs (int11)))
7475 : {
7476 3543764 : std::swap (int01, int11);
7477 3543764 : std::swap (arg00, arg10);
7478 3543764 : maybe_same = arg01;
7479 3543764 : swap = true;
7480 : }
7481 : else
7482 : maybe_same = arg11;
7483 :
7484 7743371 : wide_int factor = wi::abs (int11);
7485 7743371 : if (wi::gtu_p (factor, 1u)
7486 4234932 : && wi::exact_log2 (factor) != -1
7487 12059773 : && (int01 & (factor - 1)) == 0
7488 : /* The remainder should not be a constant, otherwise we
7489 : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7490 : increased the number of multiplications necessary. */
7491 9205529 : && TREE_CODE (arg10) != INTEGER_CST)
7492 : {
7493 2463838 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7494 1231919 : wide_int_to_tree (TREE_TYPE (arg00),
7495 1231919 : wi::sdiv_trunc (int01,
7496 : int11)));
7497 1231919 : alt1 = arg10;
7498 1231919 : same = maybe_same;
7499 1231919 : if (swap)
7500 1106957 : std::swap (alt0, alt1);
7501 : }
7502 7743373 : }
7503 :
7504 7859777 : if (!same)
7505 : return NULL_TREE;
7506 :
7507 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7508 1348325 : || TYPE_OVERFLOW_WRAPS (type)
7509 : /* We are neither factoring zero nor minus one. */
7510 1467012 : || TREE_CODE (same) == INTEGER_CST)
7511 1336703 : return fold_build2_loc (loc, MULT_EXPR, type,
7512 : fold_build2_loc (loc, code, type,
7513 : fold_convert_loc (loc, type, alt0),
7514 : fold_convert_loc (loc, type, alt1)),
7515 1336703 : fold_convert_loc (loc, type, same));
7516 :
7517 : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7518 : same may be minus one and thus the multiplication may overflow. Perform
7519 : the sum operation in an unsigned type. */
7520 11622 : tree utype = unsigned_type_for (type);
7521 11622 : tree tem = fold_build2_loc (loc, code, utype,
7522 : fold_convert_loc (loc, utype, alt0),
7523 : fold_convert_loc (loc, utype, alt1));
7524 : /* If the sum evaluated to a constant that is not -INF the multiplication
7525 : cannot overflow. */
7526 23244 : if (TREE_CODE (tem) == INTEGER_CST
7527 14860 : && (wi::to_wide (tem)
7528 18098 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7529 3225 : return fold_build2_loc (loc, MULT_EXPR, type,
7530 3225 : fold_convert (type, tem), same);
7531 :
7532 : /* Do not resort to unsigned multiplication because
7533 : we lose the no-overflow property of the expression. */
7534 : return NULL_TREE;
7535 : }
7536 :
7537 :
7538 : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7539 : into the buffer PTR of length LEN bytes.
7540 : Return the number of bytes placed in the buffer, or zero
7541 : upon failure. */
7542 :
7543 : int
7544 57090962 : native_encode_wide_int (tree type, const wide_int_ref &val,
7545 : unsigned char *ptr, int len, int off)
7546 : {
7547 57090962 : int total_bytes;
7548 57090962 : if (BITINT_TYPE_P (type))
7549 : {
7550 17430 : struct bitint_info info;
7551 17430 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7552 17430 : gcc_assert (ok);
7553 17430 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7554 17430 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7555 : {
7556 17083 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7557 : /* More work is needed when adding _BitInt support to PDP endian
7558 : if limb is smaller than word, or if _BitInt limb ordering doesn't
7559 : match target endianity here. */
7560 17083 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7561 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7562 : || (GET_MODE_SIZE (limb_mode)
7563 : >= UNITS_PER_WORD)));
7564 : }
7565 : else
7566 694 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7567 : }
7568 : else
7569 114147064 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7570 57090962 : int byte, offset, word, words;
7571 57090962 : unsigned char value;
7572 :
7573 57090962 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7574 : return 0;
7575 57090474 : if (off == -1)
7576 56204497 : off = 0;
7577 :
7578 57090474 : if (ptr == NULL)
7579 : /* Dry run. */
7580 2845562 : return MIN (len, total_bytes - off);
7581 :
7582 : words = total_bytes / UNITS_PER_WORD;
7583 :
7584 260533769 : for (byte = 0; byte < total_bytes; byte++)
7585 : {
7586 206288857 : int bitpos = byte * BITS_PER_UNIT;
7587 : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7588 : number of bytes. */
7589 206288857 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7590 :
7591 206288857 : if (total_bytes > UNITS_PER_WORD)
7592 : {
7593 206288857 : word = byte / UNITS_PER_WORD;
7594 206288857 : if (WORDS_BIG_ENDIAN)
7595 : word = (words - 1) - word;
7596 206288857 : offset = word * UNITS_PER_WORD;
7597 206288857 : if (BYTES_BIG_ENDIAN)
7598 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7599 : else
7600 206288857 : offset += byte % UNITS_PER_WORD;
7601 : }
7602 : else
7603 : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7604 206288857 : if (offset >= off && offset - off < len)
7605 204962542 : ptr[offset - off] = value;
7606 : }
7607 54244912 : return MIN (len, total_bytes - off);
7608 : }
7609 :
7610 : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7611 : specified by EXPR into the buffer PTR of length LEN bytes.
7612 : Return the number of bytes placed in the buffer, or zero
7613 : upon failure. */
7614 :
7615 : static int
7616 57090962 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7617 : {
7618 57090962 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7619 57090962 : ptr, len, off);
7620 : }
7621 :
7622 :
7623 : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7624 : specified by EXPR into the buffer PTR of length LEN bytes.
7625 : Return the number of bytes placed in the buffer, or zero
7626 : upon failure. */
7627 :
7628 : static int
7629 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7630 : {
7631 0 : tree type = TREE_TYPE (expr);
7632 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7633 0 : int total_bytes = GET_MODE_SIZE (mode);
7634 0 : FIXED_VALUE_TYPE value;
7635 0 : tree i_value, i_type;
7636 :
7637 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7638 : return 0;
7639 :
7640 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7641 :
7642 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7643 : return 0;
7644 :
7645 0 : value = TREE_FIXED_CST (expr);
7646 0 : i_value = double_int_to_tree (i_type, value.data);
7647 :
7648 0 : return native_encode_int (i_value, ptr, len, off);
7649 : }
7650 :
7651 :
7652 : /* Subroutine of native_encode_expr. Encode the REAL_CST
7653 : specified by EXPR into the buffer PTR of length LEN bytes.
7654 : Return the number of bytes placed in the buffer, or zero
7655 : upon failure. */
7656 :
7657 : int
7658 865914 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7659 : unsigned char *ptr, int len, int off)
7660 : {
7661 865914 : int total_bytes = GET_MODE_SIZE (mode);
7662 865914 : int byte, offset, word, words, bitpos;
7663 865914 : unsigned char value;
7664 :
7665 : /* There are always 32 bits in each long, no matter the size of
7666 : the hosts long. We handle floating point representations with
7667 : up to 192 bits. */
7668 865914 : long tmp[6];
7669 :
7670 865914 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7671 : return 0;
7672 863614 : if (off == -1)
7673 755664 : off = 0;
7674 :
7675 863614 : if (ptr == NULL)
7676 : /* Dry run. */
7677 138689 : return MIN (len, total_bytes - off);
7678 :
7679 724925 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7680 :
7681 724925 : real_to_target (tmp, val, mode);
7682 :
7683 7015219 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7684 6290294 : bitpos += BITS_PER_UNIT)
7685 : {
7686 6290294 : byte = (bitpos / BITS_PER_UNIT) & 3;
7687 6290294 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7688 :
7689 6290294 : if (UNITS_PER_WORD < 4)
7690 : {
7691 : word = byte / UNITS_PER_WORD;
7692 : if (WORDS_BIG_ENDIAN)
7693 : word = (words - 1) - word;
7694 : offset = word * UNITS_PER_WORD;
7695 : if (BYTES_BIG_ENDIAN)
7696 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7697 : else
7698 : offset += byte % UNITS_PER_WORD;
7699 : }
7700 : else
7701 : {
7702 6290294 : offset = byte;
7703 6290294 : if (BYTES_BIG_ENDIAN)
7704 : {
7705 : /* Reverse bytes within each long, or within the entire float
7706 : if it's smaller than a long (for HFmode). */
7707 : offset = MIN (3, total_bytes - 1) - offset;
7708 : gcc_assert (offset >= 0);
7709 : }
7710 : }
7711 6290294 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7712 6290294 : if (offset >= off
7713 6287054 : && offset - off < len)
7714 6265394 : ptr[offset - off] = value;
7715 : }
7716 724925 : return MIN (len, total_bytes - off);
7717 : }
7718 :
7719 : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7720 : specified by EXPR into the buffer PTR of length LEN bytes.
7721 : Return the number of bytes placed in the buffer, or zero
7722 : upon failure. */
7723 :
7724 : static int
7725 10215 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7726 : {
7727 10215 : int rsize, isize;
7728 10215 : tree part;
7729 :
7730 10215 : part = TREE_REALPART (expr);
7731 10215 : rsize = native_encode_expr (part, ptr, len, off);
7732 10215 : if (off == -1 && rsize == 0)
7733 : return 0;
7734 10215 : part = TREE_IMAGPART (expr);
7735 10215 : if (off != -1)
7736 20421 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7737 10215 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7738 : len - rsize, off);
7739 10215 : if (off == -1 && isize != rsize)
7740 : return 0;
7741 10215 : return rsize + isize;
7742 : }
7743 :
7744 : /* Like native_encode_vector, but only encode the first COUNT elements.
7745 : The other arguments are as for native_encode_vector. */
7746 :
7747 : static int
7748 1077476 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7749 : int off, unsigned HOST_WIDE_INT count)
7750 : {
7751 1077476 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7752 2154952 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7753 1078510 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7754 : {
7755 : /* This is the only case in which elements can be smaller than a byte.
7756 : Element 0 is always in the lsb of the containing byte. */
7757 956 : unsigned int elt_bits = TYPE_PRECISION (itype);
7758 956 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7759 956 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7760 : return 0;
7761 :
7762 956 : if (off == -1)
7763 956 : off = 0;
7764 :
7765 : /* Zero the buffer and then set bits later where necessary. */
7766 956 : int extract_bytes = MIN (len, total_bytes - off);
7767 956 : if (ptr)
7768 956 : memset (ptr, 0, extract_bytes);
7769 :
7770 956 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7771 956 : unsigned int first_elt = off * elts_per_byte;
7772 956 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7773 956 : unsigned int elt_mask = (1 << elt_bits) - 1;
7774 17477 : for (unsigned int i = 0; i < extract_elts; ++i)
7775 : {
7776 16521 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7777 16521 : if (TREE_CODE (elt) != INTEGER_CST)
7778 : return 0;
7779 :
7780 16521 : if (ptr && integer_nonzerop (elt))
7781 : {
7782 8488 : unsigned int bit = i * elt_bits;
7783 8488 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7784 : }
7785 : }
7786 : return extract_bytes;
7787 : }
7788 :
7789 1076520 : int offset = 0;
7790 1076520 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7791 4311515 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7792 : {
7793 3836477 : if (off >= size)
7794 : {
7795 24102 : off -= size;
7796 24102 : continue;
7797 : }
7798 3812375 : tree elem = VECTOR_CST_ELT (expr, i);
7799 3812375 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7800 : len - offset, off);
7801 3812375 : if ((off == -1 && res != size) || res == 0)
7802 : return 0;
7803 3811846 : offset += res;
7804 3811846 : if (offset >= len)
7805 600953 : return (off == -1 && i < count - 1) ? 0 : offset;
7806 3210893 : if (off != -1)
7807 439614 : off = 0;
7808 : }
7809 : return offset;
7810 : }
7811 :
7812 : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7813 : specified by EXPR into the buffer PTR of length LEN bytes.
7814 : Return the number of bytes placed in the buffer, or zero
7815 : upon failure. */
7816 :
7817 : static int
7818 921918 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7819 : {
7820 921918 : unsigned HOST_WIDE_INT count;
7821 921918 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7822 : return 0;
7823 921918 : return native_encode_vector_part (expr, ptr, len, off, count);
7824 : }
7825 :
7826 :
7827 : /* Subroutine of native_encode_expr. Encode the STRING_CST
7828 : specified by EXPR into the buffer PTR of length LEN bytes.
7829 : Return the number of bytes placed in the buffer, or zero
7830 : upon failure. */
7831 :
7832 : static int
7833 143245 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7834 : {
7835 143245 : tree type = TREE_TYPE (expr);
7836 :
7837 : /* Wide-char strings are encoded in target byte-order so native
7838 : encoding them is trivial. */
7839 143245 : if (BITS_PER_UNIT != CHAR_BIT
7840 143245 : || TREE_CODE (type) != ARRAY_TYPE
7841 143245 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7842 286490 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7843 : return 0;
7844 :
7845 143245 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7846 143245 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7847 : return 0;
7848 142379 : if (off == -1)
7849 56373 : off = 0;
7850 142379 : len = MIN (total_bytes - off, len);
7851 142379 : if (ptr == NULL)
7852 : /* Dry run. */;
7853 : else
7854 : {
7855 142379 : int written = 0;
7856 142379 : if (off < TREE_STRING_LENGTH (expr))
7857 : {
7858 141886 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7859 141886 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7860 : }
7861 142379 : memset (ptr + written, 0, len - written);
7862 : }
7863 : return len;
7864 : }
7865 :
7866 : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7867 : specified by EXPR into the buffer PTR of length LEN bytes.
7868 : Return the number of bytes placed in the buffer, or zero
7869 : upon failure. */
7870 :
7871 : static int
7872 49275 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7873 : {
7874 : /* We are only concerned with zero-initialization constructors here. That's
7875 : all we expect to see in GIMPLE, so that's all native_encode_expr should
7876 : deal with. For more general handling of constructors, there is
7877 : native_encode_initializer. */
7878 49275 : if (CONSTRUCTOR_NELTS (expr))
7879 : return 0;
7880 :
7881 : /* Wide-char strings are encoded in target byte-order so native
7882 : encoding them is trivial. */
7883 92162 : if (BITS_PER_UNIT != CHAR_BIT
7884 46081 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7885 : return 0;
7886 :
7887 46081 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7888 46081 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7889 : return 0;
7890 46081 : if (off == -1)
7891 0 : off = 0;
7892 46081 : len = MIN (total_bytes - off, len);
7893 46081 : if (ptr == NULL)
7894 : /* Dry run. */;
7895 : else
7896 46081 : memset (ptr, 0, len);
7897 : return len;
7898 : }
7899 :
7900 : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7901 : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7902 : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7903 : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7904 : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7905 : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7906 : Return the number of bytes placed in the buffer, or zero upon failure. */
7907 :
7908 : int
7909 73660272 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7910 : {
7911 : /* We don't support starting at negative offset and -1 is special. */
7912 73660272 : if (off < -1)
7913 : return 0;
7914 :
7915 73660260 : switch (TREE_CODE (expr))
7916 : {
7917 57088716 : case INTEGER_CST:
7918 57088716 : return native_encode_int (expr, ptr, len, off);
7919 :
7920 865914 : case REAL_CST:
7921 865914 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7922 1731828 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7923 :
7924 0 : case FIXED_CST:
7925 0 : return native_encode_fixed (expr, ptr, len, off);
7926 :
7927 10215 : case COMPLEX_CST:
7928 10215 : return native_encode_complex (expr, ptr, len, off);
7929 :
7930 921918 : case VECTOR_CST:
7931 921918 : return native_encode_vector (expr, ptr, len, off);
7932 :
7933 143245 : case STRING_CST:
7934 143245 : return native_encode_string (expr, ptr, len, off);
7935 :
7936 49275 : case CONSTRUCTOR:
7937 49275 : return native_encode_constructor (expr, ptr, len, off);
7938 :
7939 : default:
7940 : return 0;
7941 : }
7942 : }
7943 :
7944 : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7945 : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7946 : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7947 : machine modes, we can't just use build_nonstandard_integer_type. */
7948 :
7949 : tree
7950 541 : find_bitfield_repr_type (int fieldsize, int len)
7951 : {
7952 541 : machine_mode mode;
7953 1063 : for (int pass = 0; pass < 2; pass++)
7954 : {
7955 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7956 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7957 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7958 7286 : && known_eq (GET_MODE_PRECISION (mode),
7959 : GET_MODE_BITSIZE (mode))
7960 11274 : && known_le (GET_MODE_SIZE (mode), len))
7961 : {
7962 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7963 280 : if (ret && TYPE_MODE (ret) == mode)
7964 : return ret;
7965 : }
7966 : }
7967 :
7968 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7969 261 : if (int_n_enabled_p[i]
7970 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7971 261 : && int_n_trees[i].unsigned_type)
7972 : {
7973 261 : tree ret = int_n_trees[i].unsigned_type;
7974 261 : mode = TYPE_MODE (ret);
7975 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7976 522 : && known_eq (GET_MODE_PRECISION (mode),
7977 : GET_MODE_BITSIZE (mode))
7978 783 : && known_le (GET_MODE_SIZE (mode), len))
7979 : return ret;
7980 : }
7981 :
7982 : return NULL_TREE;
7983 : }
7984 :
7985 : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7986 : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7987 : to be non-NULL and OFF zero), then in addition to filling the
7988 : bytes pointed by PTR with the value also clear any bits pointed
7989 : by MASK that are known to be initialized, keep them as is for
7990 : e.g. uninitialized padding bits or uninitialized fields. */
7991 :
7992 : int
7993 50140485 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7994 : int off, unsigned char *mask)
7995 : {
7996 50140485 : int r;
7997 :
7998 : /* We don't support starting at negative offset and -1 is special. */
7999 50140485 : if (off < -1 || init == NULL_TREE)
8000 : return 0;
8001 :
8002 50140485 : gcc_assert (mask == NULL || (off == 0 && ptr));
8003 :
8004 50140485 : STRIP_NOPS (init);
8005 50140485 : switch (TREE_CODE (init))
8006 : {
8007 0 : case VIEW_CONVERT_EXPR:
8008 0 : case NON_LVALUE_EXPR:
8009 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8010 0 : mask);
8011 47928066 : default:
8012 47928066 : r = native_encode_expr (init, ptr, len, off);
8013 47928066 : if (mask)
8014 7135 : memset (mask, 0, r);
8015 : return r;
8016 2212419 : case CONSTRUCTOR:
8017 2212419 : tree type = TREE_TYPE (init);
8018 2212419 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8019 2212419 : if (total_bytes < 0)
8020 : return 0;
8021 2212419 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
8022 : return 0;
8023 2212343 : int o = off == -1 ? 0 : off;
8024 2212343 : if (TREE_CODE (type) == ARRAY_TYPE)
8025 : {
8026 301918 : tree min_index;
8027 301918 : unsigned HOST_WIDE_INT cnt;
8028 301918 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8029 301918 : constructor_elt *ce;
8030 :
8031 301918 : if (!TYPE_DOMAIN (type)
8032 301918 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8033 : return 0;
8034 :
8035 301918 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
8036 301918 : if (fieldsize <= 0)
8037 : return 0;
8038 :
8039 301918 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8040 301918 : if (ptr)
8041 301918 : memset (ptr, '\0', MIN (total_bytes - off, len));
8042 :
8043 301918 : for (cnt = 0; ; cnt++)
8044 : {
8045 47595482 : tree val = NULL_TREE, index = NULL_TREE;
8046 47595482 : HOST_WIDE_INT pos = curpos, count = 0;
8047 47595482 : bool full = false;
8048 47595482 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8049 : {
8050 47483022 : val = ce->value;
8051 47483022 : index = ce->index;
8052 : }
8053 112460 : else if (mask == NULL
8054 618 : || CONSTRUCTOR_NO_CLEARING (init)
8055 113018 : || curpos >= total_bytes)
8056 : break;
8057 : else
8058 : pos = total_bytes;
8059 :
8060 47483022 : if (index && TREE_CODE (index) == RANGE_EXPR)
8061 : {
8062 18 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8063 18 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8064 0 : return 0;
8065 18 : offset_int wpos
8066 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8067 36 : - wi::to_offset (min_index),
8068 18 : TYPE_PRECISION (sizetype));
8069 18 : wpos *= fieldsize;
8070 18 : if (!wi::fits_shwi_p (pos))
8071 : return 0;
8072 18 : pos = wpos.to_shwi ();
8073 18 : offset_int wcount
8074 18 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8075 36 : - wi::to_offset (TREE_OPERAND (index, 0)),
8076 18 : TYPE_PRECISION (sizetype));
8077 18 : if (!wi::fits_shwi_p (wcount))
8078 : return 0;
8079 18 : count = wcount.to_shwi ();
8080 18 : }
8081 46948751 : else if (index)
8082 : {
8083 46948751 : if (TREE_CODE (index) != INTEGER_CST)
8084 0 : return 0;
8085 46948751 : offset_int wpos
8086 46948751 : = wi::sext (wi::to_offset (index)
8087 93897502 : - wi::to_offset (min_index),
8088 46948751 : TYPE_PRECISION (sizetype));
8089 46948751 : wpos *= fieldsize;
8090 46948751 : if (!wi::fits_shwi_p (wpos))
8091 : return 0;
8092 46948751 : pos = wpos.to_shwi ();
8093 : }
8094 :
8095 47484283 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8096 : {
8097 72 : if (valueinit == -1)
8098 : {
8099 72 : tree zero = build_zero_cst (TREE_TYPE (type));
8100 144 : r = native_encode_initializer (zero, ptr + curpos,
8101 : fieldsize, 0,
8102 72 : mask + curpos);
8103 72 : if (TREE_CODE (zero) == CONSTRUCTOR)
8104 2 : ggc_free (zero);
8105 72 : if (!r)
8106 : return 0;
8107 72 : valueinit = curpos;
8108 72 : curpos += fieldsize;
8109 : }
8110 102 : while (curpos != pos)
8111 : {
8112 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8113 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8114 30 : curpos += fieldsize;
8115 : }
8116 : }
8117 :
8118 47483094 : curpos = pos;
8119 47483094 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8120 : {
8121 527 : if (count)
8122 : return 0;
8123 527 : if (off == -1
8124 527 : || (curpos >= off
8125 0 : && (curpos + RAW_DATA_LENGTH (val)
8126 0 : <= (HOST_WIDE_INT) off + len)))
8127 : {
8128 527 : if (ptr)
8129 527 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8130 527 : RAW_DATA_LENGTH (val));
8131 527 : if (mask)
8132 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8133 : }
8134 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8135 0 : && curpos < (HOST_WIDE_INT) off + len)
8136 : {
8137 : /* Partial overlap. */
8138 0 : unsigned char *p = NULL;
8139 0 : int no = 0;
8140 0 : int l;
8141 0 : gcc_assert (mask == NULL);
8142 0 : if (curpos >= off)
8143 : {
8144 0 : if (ptr)
8145 0 : p = ptr + curpos - off;
8146 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8147 : RAW_DATA_LENGTH (val));
8148 : }
8149 : else
8150 : {
8151 0 : p = ptr;
8152 0 : no = off - curpos;
8153 0 : l = len;
8154 : }
8155 0 : if (p)
8156 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8157 : }
8158 527 : curpos += RAW_DATA_LENGTH (val);
8159 527 : val = NULL_TREE;
8160 : }
8161 527 : if (val)
8162 47560537 : do
8163 : {
8164 47560537 : if (off == -1
8165 630025 : || (curpos >= off
8166 212243 : && (curpos + fieldsize
8167 212243 : <= (HOST_WIDE_INT) off + len)))
8168 : {
8169 47112381 : if (full)
8170 : {
8171 78042 : if (ptr)
8172 78042 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8173 : fieldsize);
8174 78042 : if (mask)
8175 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8176 : }
8177 94251664 : else if (!native_encode_initializer (val,
8178 : ptr
8179 47034339 : ? ptr + curpos - o
8180 : : NULL,
8181 : fieldsize,
8182 : off == -1 ? -1
8183 : : 0,
8184 : mask
8185 1117 : ? mask + curpos
8186 : : NULL))
8187 : return 0;
8188 : else
8189 : {
8190 : full = true;
8191 : pos = curpos;
8192 : }
8193 : }
8194 448156 : else if (curpos + fieldsize > off
8195 32634 : && curpos < (HOST_WIDE_INT) off + len)
8196 : {
8197 : /* Partial overlap. */
8198 8135 : unsigned char *p = NULL;
8199 8135 : int no = 0;
8200 8135 : int l;
8201 8135 : gcc_assert (mask == NULL);
8202 8135 : if (curpos >= off)
8203 : {
8204 5875 : if (ptr)
8205 5875 : p = ptr + curpos - off;
8206 5875 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8207 : fieldsize);
8208 : }
8209 : else
8210 : {
8211 2260 : p = ptr;
8212 2260 : no = off - curpos;
8213 2260 : l = len;
8214 : }
8215 8135 : if (!native_encode_initializer (val, p, l, no, NULL))
8216 : return 0;
8217 : }
8218 47371007 : curpos += fieldsize;
8219 : }
8220 47371007 : while (count-- != 0);
8221 47293564 : }
8222 112388 : return MIN (total_bytes - off, len);
8223 : }
8224 1910425 : else if (TREE_CODE (type) == RECORD_TYPE
8225 1910425 : || TREE_CODE (type) == UNION_TYPE)
8226 : {
8227 1910425 : unsigned HOST_WIDE_INT cnt;
8228 1910425 : constructor_elt *ce;
8229 1910425 : tree fld_base = TYPE_FIELDS (type);
8230 1910425 : tree to_free = NULL_TREE;
8231 :
8232 1910425 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8233 1910425 : if (ptr != NULL)
8234 1910425 : memset (ptr, '\0', MIN (total_bytes - o, len));
8235 1910425 : for (cnt = 0; ; cnt++)
8236 : {
8237 2284513 : tree val = NULL_TREE, field = NULL_TREE;
8238 2284513 : HOST_WIDE_INT pos = 0, fieldsize;
8239 2284513 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8240 :
8241 2284513 : if (to_free)
8242 : {
8243 0 : ggc_free (to_free);
8244 0 : to_free = NULL_TREE;
8245 : }
8246 :
8247 2284513 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8248 : {
8249 401356 : val = ce->value;
8250 401356 : field = ce->index;
8251 401356 : if (field == NULL_TREE)
8252 : return 0;
8253 :
8254 401356 : pos = int_byte_position (field);
8255 401356 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8256 1496 : continue;
8257 : }
8258 1883157 : else if (mask == NULL
8259 1883157 : || CONSTRUCTOR_NO_CLEARING (init))
8260 : break;
8261 : else
8262 : pos = total_bytes;
8263 :
8264 413885 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8265 : {
8266 : tree fld;
8267 47543 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8268 : {
8269 46648 : if (TREE_CODE (fld) != FIELD_DECL)
8270 44565 : continue;
8271 2083 : if (fld == field)
8272 : break;
8273 528 : if (DECL_PADDING_P (fld))
8274 87 : continue;
8275 441 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8276 441 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8277 : return 0;
8278 441 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8279 382 : continue;
8280 : break;
8281 : }
8282 1614 : if (fld == NULL_TREE)
8283 : {
8284 895 : if (ce == NULL)
8285 : break;
8286 : return 0;
8287 : }
8288 1614 : fld_base = DECL_CHAIN (fld);
8289 1614 : if (fld != field)
8290 : {
8291 59 : cnt--;
8292 59 : field = fld;
8293 59 : pos = int_byte_position (field);
8294 59 : val = build_zero_cst (TREE_TYPE (fld));
8295 59 : if (TREE_CODE (val) == CONSTRUCTOR)
8296 0 : to_free = val;
8297 : }
8298 : }
8299 :
8300 399919 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8301 15151 : && TYPE_DOMAIN (TREE_TYPE (field))
8302 415070 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8303 : {
8304 81 : if (mask || off != -1)
8305 : return 0;
8306 81 : if (val == NULL_TREE)
8307 0 : continue;
8308 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8309 : return 0;
8310 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8311 81 : if (fieldsize < 0
8312 81 : || (int) fieldsize != fieldsize
8313 81 : || (pos + fieldsize) > INT_MAX)
8314 : return 0;
8315 81 : if (pos + fieldsize > total_bytes)
8316 : {
8317 81 : if (ptr != NULL && total_bytes < len)
8318 81 : memset (ptr + total_bytes, '\0',
8319 81 : MIN (pos + fieldsize, len) - total_bytes);
8320 : total_bytes = pos + fieldsize;
8321 : }
8322 : }
8323 : else
8324 : {
8325 399838 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8326 399838 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8327 : return 0;
8328 399838 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8329 : }
8330 399919 : if (fieldsize == 0)
8331 1 : continue;
8332 :
8333 : /* Prepare to deal with integral bit-fields and filter out other
8334 : bit-fields that do not start and end on a byte boundary. */
8335 399918 : if (DECL_BIT_FIELD (field))
8336 : {
8337 2711 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8338 : return 0;
8339 2711 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8340 2711 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8341 : {
8342 2711 : bpos %= BITS_PER_UNIT;
8343 2711 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8344 2711 : epos = fieldsize % BITS_PER_UNIT;
8345 2711 : fieldsize += BITS_PER_UNIT - 1;
8346 2711 : fieldsize /= BITS_PER_UNIT;
8347 : }
8348 0 : else if (bpos % BITS_PER_UNIT
8349 0 : || DECL_SIZE (field) == NULL_TREE
8350 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8351 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8352 : return 0;
8353 : }
8354 :
8355 399918 : if (off != -1 && pos + fieldsize <= off)
8356 3195 : continue;
8357 :
8358 396723 : if (val == NULL_TREE)
8359 0 : continue;
8360 :
8361 396723 : if (DECL_BIT_FIELD (field)
8362 396723 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8363 : {
8364 : /* FIXME: Handle PDP endian. */
8365 2507 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8366 261 : return 0;
8367 :
8368 2507 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8369 6 : val = TREE_OPERAND (val, 0);
8370 2507 : if (TREE_CODE (val) != INTEGER_CST)
8371 : return 0;
8372 :
8373 2507 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8374 2507 : tree repr_type = NULL_TREE;
8375 2507 : HOST_WIDE_INT rpos = 0;
8376 2507 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8377 : {
8378 1978 : rpos = int_byte_position (repr);
8379 1978 : repr_type = TREE_TYPE (repr);
8380 : }
8381 : else
8382 : {
8383 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8384 529 : if (repr_type == NULL_TREE)
8385 : return 0;
8386 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8387 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8388 268 : if (pos + repr_size <= o + len)
8389 : rpos = pos;
8390 : else
8391 : {
8392 14 : rpos = o + len - repr_size;
8393 14 : gcc_assert (rpos <= pos);
8394 : }
8395 : }
8396 :
8397 2246 : if (rpos > pos)
8398 : return 0;
8399 2246 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8400 2246 : int diff = (TYPE_PRECISION (repr_type)
8401 2246 : - TYPE_PRECISION (TREE_TYPE (field)));
8402 2246 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8403 2246 : if (!BYTES_BIG_ENDIAN)
8404 2246 : w = wi::lshift (w, bitoff);
8405 : else
8406 : w = wi::lshift (w, diff - bitoff);
8407 2246 : val = wide_int_to_tree (repr_type, w);
8408 :
8409 2246 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8410 : / BITS_PER_UNIT + 1];
8411 2246 : int l = native_encode_int (val, buf, sizeof buf, 0);
8412 2246 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8413 0 : return 0;
8414 :
8415 2246 : if (ptr == NULL)
8416 0 : continue;
8417 :
8418 : /* If the bitfield does not start at byte boundary, handle
8419 : the partial byte at the start. */
8420 2246 : if (bpos
8421 1351 : && (off == -1 || (pos >= off && len >= 1)))
8422 : {
8423 1276 : if (!BYTES_BIG_ENDIAN)
8424 : {
8425 1276 : int msk = (1 << bpos) - 1;
8426 1276 : buf[pos - rpos] &= ~msk;
8427 1276 : buf[pos - rpos] |= ptr[pos - o] & msk;
8428 1276 : if (mask)
8429 : {
8430 147 : if (fieldsize > 1 || epos == 0)
8431 129 : mask[pos] &= msk;
8432 : else
8433 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8434 : }
8435 : }
8436 : else
8437 : {
8438 : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8439 : buf[pos - rpos] &= msk;
8440 : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8441 : if (mask)
8442 : {
8443 : if (fieldsize > 1 || epos == 0)
8444 : mask[pos] &= ~msk;
8445 : else
8446 : mask[pos] &= (~msk
8447 : | ((1 << (BITS_PER_UNIT - epos))
8448 : - 1));
8449 : }
8450 : }
8451 : }
8452 : /* If the bitfield does not end at byte boundary, handle
8453 : the partial byte at the end. */
8454 2246 : if (epos
8455 1724 : && (off == -1
8456 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8457 : {
8458 1621 : if (!BYTES_BIG_ENDIAN)
8459 : {
8460 1621 : int msk = (1 << epos) - 1;
8461 1621 : buf[pos - rpos + fieldsize - 1] &= msk;
8462 1621 : buf[pos - rpos + fieldsize - 1]
8463 1621 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8464 1621 : if (mask && (fieldsize > 1 || bpos == 0))
8465 156 : mask[pos + fieldsize - 1] &= ~msk;
8466 : }
8467 : else
8468 : {
8469 : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8470 : buf[pos - rpos + fieldsize - 1] &= ~msk;
8471 : buf[pos - rpos + fieldsize - 1]
8472 : |= ptr[pos + fieldsize - 1 - o] & msk;
8473 : if (mask && (fieldsize > 1 || bpos == 0))
8474 : mask[pos + fieldsize - 1] &= msk;
8475 : }
8476 : }
8477 2246 : if (off == -1
8478 1301 : || (pos >= off
8479 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8480 : {
8481 2055 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8482 2055 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8483 75 : memset (mask + pos + (bpos != 0), 0,
8484 75 : fieldsize - (bpos != 0) - (epos != 0));
8485 : }
8486 : else
8487 : {
8488 : /* Partial overlap. */
8489 191 : HOST_WIDE_INT fsz = fieldsize;
8490 191 : gcc_assert (mask == NULL);
8491 191 : if (pos < off)
8492 : {
8493 89 : fsz -= (off - pos);
8494 89 : pos = off;
8495 : }
8496 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8497 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8498 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8499 : }
8500 2246 : continue;
8501 2246 : }
8502 :
8503 394216 : if (off == -1
8504 28584 : || (pos >= off
8505 27754 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8506 : {
8507 385847 : int fldsize = fieldsize;
8508 20215 : if (off == -1)
8509 : {
8510 365632 : tree fld = DECL_CHAIN (field);
8511 5776603 : while (fld)
8512 : {
8513 5429707 : if (TREE_CODE (fld) == FIELD_DECL)
8514 : break;
8515 5410971 : fld = DECL_CHAIN (fld);
8516 : }
8517 365632 : if (fld == NULL_TREE)
8518 346896 : fldsize = len - pos;
8519 : }
8520 417920 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8521 : : NULL,
8522 : fldsize,
8523 : off == -1 ? -1 : 0,
8524 11858 : mask ? mask + pos : NULL);
8525 385847 : if (!r)
8526 : return 0;
8527 366129 : if (off == -1
8528 353154 : && fldsize != fieldsize
8529 1149 : && r > fieldsize
8530 822 : && pos + r > total_bytes)
8531 374088 : total_bytes = pos + r;
8532 : }
8533 : else
8534 : {
8535 : /* Partial overlap. */
8536 8369 : unsigned char *p = NULL;
8537 8369 : int no = 0;
8538 8369 : int l;
8539 8369 : gcc_assert (mask == NULL);
8540 8369 : if (pos >= off)
8541 : {
8542 7539 : if (ptr)
8543 7539 : p = ptr + pos - off;
8544 7539 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8545 : fieldsize);
8546 : }
8547 : else
8548 : {
8549 830 : p = ptr;
8550 830 : no = off - pos;
8551 830 : l = len;
8552 : }
8553 8369 : if (!native_encode_initializer (val, p, l, no, NULL))
8554 : return 0;
8555 : }
8556 374088 : }
8557 1883098 : return MIN (total_bytes - off, len);
8558 : }
8559 : return 0;
8560 : }
8561 : }
8562 :
8563 :
8564 : /* Subroutine of native_interpret_expr. Interpret the contents of
8565 : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8566 : If the buffer cannot be interpreted, return NULL_TREE. */
8567 :
8568 : static tree
8569 2872935 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8570 : {
8571 2872935 : int total_bytes;
8572 2872935 : if (BITINT_TYPE_P (type))
8573 : {
8574 31 : struct bitint_info info;
8575 31 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8576 31 : gcc_assert (ok);
8577 31 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8578 31 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8579 : {
8580 31 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8581 : /* More work is needed when adding _BitInt support to PDP endian
8582 : if limb is smaller than word, or if _BitInt limb ordering doesn't
8583 : match target endianity here. */
8584 31 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8585 : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8586 : || (GET_MODE_SIZE (limb_mode)
8587 : >= UNITS_PER_WORD)));
8588 : }
8589 : else
8590 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8591 : }
8592 : else
8593 5745808 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8594 :
8595 2872935 : if (total_bytes > len)
8596 : return NULL_TREE;
8597 :
8598 2872693 : wide_int result = wi::from_buffer (ptr, total_bytes);
8599 :
8600 2872693 : return wide_int_to_tree (type, result);
8601 2872693 : }
8602 :
8603 :
8604 : /* Subroutine of native_interpret_expr. Interpret the contents of
8605 : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8606 : If the buffer cannot be interpreted, return NULL_TREE. */
8607 :
8608 : static tree
8609 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8610 : {
8611 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8612 0 : int total_bytes = GET_MODE_SIZE (mode);
8613 0 : double_int result;
8614 0 : FIXED_VALUE_TYPE fixed_value;
8615 :
8616 0 : if (total_bytes > len
8617 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8618 : return NULL_TREE;
8619 :
8620 0 : result = double_int::from_buffer (ptr, total_bytes);
8621 0 : fixed_value = fixed_from_double_int (result, mode);
8622 :
8623 0 : return build_fixed (type, fixed_value);
8624 : }
8625 :
8626 :
8627 : /* Subroutine of native_interpret_expr. Interpret the contents of
8628 : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8629 : If the buffer cannot be interpreted, return NULL_TREE. */
8630 :
8631 : tree
8632 38097 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8633 : {
8634 38097 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8635 38097 : int total_bytes = GET_MODE_SIZE (mode);
8636 38097 : unsigned char value;
8637 : /* There are always 32 bits in each long, no matter the size of
8638 : the hosts long. We handle floating point representations with
8639 : up to 192 bits. */
8640 38097 : REAL_VALUE_TYPE r;
8641 38097 : long tmp[6];
8642 :
8643 38097 : if (total_bytes > len || total_bytes > 24)
8644 : return NULL_TREE;
8645 38036 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8646 :
8647 38036 : memset (tmp, 0, sizeof (tmp));
8648 276430 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8649 238394 : bitpos += BITS_PER_UNIT)
8650 : {
8651 : /* Both OFFSET and BYTE index within a long;
8652 : bitpos indexes the whole float. */
8653 238394 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8654 238394 : if (UNITS_PER_WORD < 4)
8655 : {
8656 : int word = byte / UNITS_PER_WORD;
8657 : if (WORDS_BIG_ENDIAN)
8658 : word = (words - 1) - word;
8659 : offset = word * UNITS_PER_WORD;
8660 : if (BYTES_BIG_ENDIAN)
8661 : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8662 : else
8663 : offset += byte % UNITS_PER_WORD;
8664 : }
8665 : else
8666 : {
8667 238394 : offset = byte;
8668 238394 : if (BYTES_BIG_ENDIAN)
8669 : {
8670 : /* Reverse bytes within each long, or within the entire float
8671 : if it's smaller than a long (for HFmode). */
8672 : offset = MIN (3, total_bytes - 1) - offset;
8673 : gcc_assert (offset >= 0);
8674 : }
8675 : }
8676 238394 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8677 :
8678 238394 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8679 : }
8680 :
8681 38036 : real_from_target (&r, tmp, mode);
8682 38036 : return build_real (type, r);
8683 : }
8684 :
8685 :
8686 : /* Subroutine of native_interpret_expr. Interpret the contents of
8687 : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8688 : If the buffer cannot be interpreted, return NULL_TREE. */
8689 :
8690 : static tree
8691 1596 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8692 : {
8693 1596 : tree etype, rpart, ipart;
8694 1596 : int size;
8695 :
8696 1596 : etype = TREE_TYPE (type);
8697 1596 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8698 1596 : if (size * 2 > len)
8699 : return NULL_TREE;
8700 1563 : rpart = native_interpret_expr (etype, ptr, size);
8701 1563 : if (!rpart)
8702 : return NULL_TREE;
8703 1382 : ipart = native_interpret_expr (etype, ptr+size, size);
8704 1382 : if (!ipart)
8705 : return NULL_TREE;
8706 1382 : return build_complex (type, rpart, ipart);
8707 : }
8708 :
8709 : /* Read a vector of type TYPE from the target memory image given by BYTES,
8710 : which contains LEN bytes. The vector is known to be encodable using
8711 : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8712 :
8713 : Return the vector on success, otherwise return null. */
8714 :
8715 : static tree
8716 238396 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8717 : unsigned int len, unsigned int npatterns,
8718 : unsigned int nelts_per_pattern)
8719 : {
8720 238396 : tree elt_type = TREE_TYPE (type);
8721 238396 : if (VECTOR_BOOLEAN_TYPE_P (type)
8722 238400 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8723 : {
8724 : /* This is the only case in which elements can be smaller than a byte.
8725 : Element 0 is always in the lsb of the containing byte. */
8726 2 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8727 2 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8728 : return NULL_TREE;
8729 :
8730 2 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8731 22 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8732 : {
8733 18 : unsigned int bit_index = i * elt_bits;
8734 18 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8735 18 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8736 36 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8737 20 : ? build_all_ones_cst (elt_type)
8738 2 : : build_zero_cst (elt_type));
8739 : }
8740 2 : return builder.build ();
8741 2 : }
8742 :
8743 238394 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8744 238394 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8745 : return NULL_TREE;
8746 :
8747 238394 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8748 1188304 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8749 : {
8750 711650 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8751 711650 : if (!elt)
8752 134 : return NULL_TREE;
8753 711516 : builder.quick_push (elt);
8754 711516 : bytes += elt_bytes;
8755 : }
8756 238260 : return builder.build ();
8757 238394 : }
8758 :
8759 : /* Subroutine of native_interpret_expr. Interpret the contents of
8760 : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8761 : If the buffer cannot be interpreted, return NULL_TREE. */
8762 :
8763 : static tree
8764 82840 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8765 : {
8766 82840 : unsigned HOST_WIDE_INT size;
8767 :
8768 82840 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8769 82840 : || size > len)
8770 2 : return NULL_TREE;
8771 :
8772 82838 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8773 82838 : return native_interpret_vector_part (type, ptr, len, count, 1);
8774 : }
8775 :
8776 :
8777 : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8778 : the buffer PTR of length LEN as a constant of type TYPE. For
8779 : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8780 : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8781 : return NULL_TREE. */
8782 :
8783 : tree
8784 3152303 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8785 : {
8786 3152303 : switch (TREE_CODE (type))
8787 : {
8788 2872935 : case INTEGER_TYPE:
8789 2872935 : case ENUMERAL_TYPE:
8790 2872935 : case BOOLEAN_TYPE:
8791 2872935 : case POINTER_TYPE:
8792 2872935 : case REFERENCE_TYPE:
8793 2872935 : case OFFSET_TYPE:
8794 2872935 : case BITINT_TYPE:
8795 2872935 : return native_interpret_int (type, ptr, len);
8796 :
8797 36510 : case REAL_TYPE:
8798 36510 : if (tree ret = native_interpret_real (type, ptr, len))
8799 : {
8800 : /* For floating point values in composite modes, punt if this
8801 : folding doesn't preserve bit representation. As the mode doesn't
8802 : have fixed precision while GCC pretends it does, there could be
8803 : valid values that GCC can't really represent accurately.
8804 : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8805 : bit combinationations which GCC doesn't preserve. */
8806 36449 : unsigned char buf[24 * 2];
8807 36449 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8808 36449 : int total_bytes = GET_MODE_SIZE (mode);
8809 36449 : memcpy (buf + 24, ptr, total_bytes);
8810 36449 : clear_type_padding_in_mask (type, buf + 24);
8811 36449 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8812 36449 : || memcmp (buf + 24, buf, total_bytes) != 0)
8813 488 : return NULL_TREE;
8814 : return ret;
8815 : }
8816 : return NULL_TREE;
8817 :
8818 0 : case FIXED_POINT_TYPE:
8819 0 : return native_interpret_fixed (type, ptr, len);
8820 :
8821 1596 : case COMPLEX_TYPE:
8822 1596 : return native_interpret_complex (type, ptr, len);
8823 :
8824 82840 : case VECTOR_TYPE:
8825 82840 : return native_interpret_vector (type, ptr, len);
8826 :
8827 : default:
8828 : return NULL_TREE;
8829 : }
8830 : }
8831 :
8832 : /* Returns true if we can interpret the contents of a native encoding
8833 : as TYPE. */
8834 :
8835 : bool
8836 340984 : can_native_interpret_type_p (tree type)
8837 : {
8838 340984 : switch (TREE_CODE (type))
8839 : {
8840 : case INTEGER_TYPE:
8841 : case ENUMERAL_TYPE:
8842 : case BOOLEAN_TYPE:
8843 : case POINTER_TYPE:
8844 : case REFERENCE_TYPE:
8845 : case FIXED_POINT_TYPE:
8846 : case REAL_TYPE:
8847 : case COMPLEX_TYPE:
8848 : case VECTOR_TYPE:
8849 : case OFFSET_TYPE:
8850 : return true;
8851 38531 : default:
8852 38531 : return false;
8853 : }
8854 : }
8855 :
8856 : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8857 : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8858 :
8859 : tree
8860 9631 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8861 : int len)
8862 : {
8863 9631 : vec<constructor_elt, va_gc> *elts = NULL;
8864 9631 : if (TREE_CODE (type) == ARRAY_TYPE)
8865 : {
8866 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8867 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8868 : return NULL_TREE;
8869 :
8870 197 : HOST_WIDE_INT cnt = 0;
8871 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8872 : {
8873 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8874 : return NULL_TREE;
8875 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8876 : }
8877 197 : if (eltsz == 0)
8878 0 : cnt = 0;
8879 197 : HOST_WIDE_INT pos = 0;
8880 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8881 : {
8882 439 : tree v = NULL_TREE;
8883 439 : if (pos >= len || pos + eltsz > len)
8884 9631 : return NULL_TREE;
8885 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8886 : {
8887 367 : v = native_interpret_expr (TREE_TYPE (type),
8888 367 : ptr + off + pos, eltsz);
8889 367 : if (v == NULL_TREE)
8890 : return NULL_TREE;
8891 : }
8892 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8893 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8894 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8895 : eltsz);
8896 72 : if (v == NULL_TREE)
8897 : return NULL_TREE;
8898 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8899 : }
8900 197 : return build_constructor (type, elts);
8901 : }
8902 9434 : if (TREE_CODE (type) != RECORD_TYPE)
8903 : return NULL_TREE;
8904 784000 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8905 : {
8906 22366 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8907 796932 : || is_empty_type (TREE_TYPE (field)))
8908 761072 : continue;
8909 13494 : tree fld = field;
8910 13494 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8911 13494 : int diff = 0;
8912 13494 : tree v = NULL_TREE;
8913 13494 : if (DECL_BIT_FIELD (field))
8914 : {
8915 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8916 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8917 : {
8918 168 : poly_int64 bitoffset;
8919 168 : poly_uint64 field_offset, fld_offset;
8920 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8921 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8922 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8923 : else
8924 : bitoffset = 0;
8925 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8926 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8927 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8928 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8929 168 : if (!bitoffset.is_constant (&bitoff)
8930 168 : || bitoff < 0
8931 168 : || bitoff > diff)
8932 0 : return NULL_TREE;
8933 : }
8934 : else
8935 : {
8936 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8937 : return NULL_TREE;
8938 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8939 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8940 12 : bpos %= BITS_PER_UNIT;
8941 12 : fieldsize += bpos;
8942 12 : fieldsize += BITS_PER_UNIT - 1;
8943 12 : fieldsize /= BITS_PER_UNIT;
8944 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8945 12 : if (repr_type == NULL_TREE)
8946 : return NULL_TREE;
8947 12 : sz = int_size_in_bytes (repr_type);
8948 12 : if (sz < 0 || sz > len)
8949 : return NULL_TREE;
8950 12 : pos = int_byte_position (field);
8951 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8952 : return NULL_TREE;
8953 12 : HOST_WIDE_INT rpos;
8954 12 : if (pos + sz <= len)
8955 : rpos = pos;
8956 : else
8957 : {
8958 0 : rpos = len - sz;
8959 0 : gcc_assert (rpos <= pos);
8960 : }
8961 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8962 12 : pos = rpos;
8963 12 : diff = (TYPE_PRECISION (repr_type)
8964 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8965 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8966 12 : if (v == NULL_TREE)
8967 : return NULL_TREE;
8968 : fld = NULL_TREE;
8969 : }
8970 : }
8971 :
8972 168 : if (fld)
8973 : {
8974 13482 : sz = int_size_in_bytes (TREE_TYPE (fld));
8975 13482 : if (sz < 0 || sz > len)
8976 : return NULL_TREE;
8977 13482 : tree byte_pos = byte_position (fld);
8978 13482 : if (!tree_fits_shwi_p (byte_pos))
8979 : return NULL_TREE;
8980 13482 : pos = tree_to_shwi (byte_pos);
8981 13482 : if (pos < 0 || pos > len || pos + sz > len)
8982 : return NULL_TREE;
8983 : }
8984 13482 : if (fld == NULL_TREE)
8985 : /* Already handled above. */;
8986 13482 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8987 : {
8988 6262 : v = native_interpret_expr (TREE_TYPE (fld),
8989 6262 : ptr + off + pos, sz);
8990 6262 : if (v == NULL_TREE)
8991 : return NULL_TREE;
8992 : }
8993 7220 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8994 7220 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8995 7220 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8996 7232 : if (v == NULL_TREE)
8997 : return NULL_TREE;
8998 13494 : if (fld != field)
8999 : {
9000 180 : if (TREE_CODE (v) != INTEGER_CST)
9001 : return NULL_TREE;
9002 :
9003 : /* FIXME: Figure out how to handle PDP endian bitfields. */
9004 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9005 : return NULL_TREE;
9006 180 : if (!BYTES_BIG_ENDIAN)
9007 180 : v = wide_int_to_tree (TREE_TYPE (field),
9008 360 : wi::lrshift (wi::to_wide (v), bitoff));
9009 : else
9010 : v = wide_int_to_tree (TREE_TYPE (field),
9011 : wi::lrshift (wi::to_wide (v),
9012 : diff - bitoff));
9013 : }
9014 13494 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
9015 : }
9016 9434 : return build_constructor (type, elts);
9017 : }
9018 :
9019 : /* Routines for manipulation of native_encode_expr encoded data if the encoded
9020 : or extracted constant positions and/or sizes aren't byte aligned. */
9021 :
9022 : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9023 : bits between adjacent elements. AMNT should be within
9024 : [0, BITS_PER_UNIT).
9025 : Example, AMNT = 2:
9026 : 00011111|11100000 << 2 = 01111111|10000000
9027 : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9028 :
9029 : void
9030 29850 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9031 : unsigned int amnt)
9032 : {
9033 29850 : if (amnt == 0)
9034 : return;
9035 :
9036 17306 : unsigned char carry_over = 0U;
9037 17306 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9038 17306 : unsigned char clear_mask = (~0U) << amnt;
9039 :
9040 102359 : for (unsigned int i = 0; i < sz; i++)
9041 : {
9042 85053 : unsigned prev_carry_over = carry_over;
9043 85053 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9044 :
9045 85053 : ptr[i] <<= amnt;
9046 85053 : if (i != 0)
9047 : {
9048 67747 : ptr[i] &= clear_mask;
9049 67747 : ptr[i] |= prev_carry_over;
9050 : }
9051 : }
9052 : }
9053 :
9054 : /* Like shift_bytes_in_array_left but for big-endian.
9055 : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9056 : bits between adjacent elements. AMNT should be within
9057 : [0, BITS_PER_UNIT).
9058 : Example, AMNT = 2:
9059 : 00011111|11100000 >> 2 = 00000111|11111000
9060 : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9061 :
9062 : void
9063 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9064 : unsigned int amnt)
9065 : {
9066 8 : if (amnt == 0)
9067 : return;
9068 :
9069 4 : unsigned char carry_over = 0U;
9070 4 : unsigned char carry_mask = ~(~0U << amnt);
9071 :
9072 12 : for (unsigned int i = 0; i < sz; i++)
9073 : {
9074 8 : unsigned prev_carry_over = carry_over;
9075 8 : carry_over = ptr[i] & carry_mask;
9076 :
9077 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9078 8 : ptr[i] >>= amnt;
9079 8 : ptr[i] |= prev_carry_over;
9080 : }
9081 : }
9082 :
9083 : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9084 : directly on the VECTOR_CST encoding, in a way that works for variable-
9085 : length vectors. Return the resulting VECTOR_CST on success or null
9086 : on failure. */
9087 :
9088 : static tree
9089 164517 : fold_view_convert_vector_encoding (tree type, tree expr)
9090 : {
9091 164517 : tree expr_type = TREE_TYPE (expr);
9092 164517 : poly_uint64 type_bits, expr_bits;
9093 164517 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9094 164517 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9095 : return NULL_TREE;
9096 :
9097 164517 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9098 164517 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9099 164517 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9100 164517 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9101 :
9102 : /* We can only preserve the semantics of a stepped pattern if the new
9103 : vector element is an integer of the same size. */
9104 164517 : if (VECTOR_CST_STEPPED_P (expr)
9105 164517 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9106 : return NULL_TREE;
9107 :
9108 : /* The number of bits needed to encode one element from every pattern
9109 : of the original vector. */
9110 155558 : unsigned int expr_sequence_bits
9111 155558 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9112 :
9113 : /* The number of bits needed to encode one element from every pattern
9114 : of the result. */
9115 155558 : unsigned int type_sequence_bits
9116 155558 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9117 :
9118 : /* Don't try to read more bytes than are available, which can happen
9119 : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9120 : The general VIEW_CONVERT handling can cope with that case, so there's
9121 : no point complicating things here. */
9122 155558 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9123 155558 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9124 : BITS_PER_UNIT);
9125 155558 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9126 155558 : if (known_gt (buffer_bits, expr_bits))
9127 : return NULL_TREE;
9128 :
9129 : /* Get enough bytes of EXPR to form the new encoding. */
9130 155558 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9131 155558 : buffer.quick_grow (buffer_bytes);
9132 155558 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9133 155558 : buffer_bits / expr_elt_bits)
9134 : != (int) buffer_bytes)
9135 : return NULL_TREE;
9136 :
9137 : /* Re-encode the bytes as TYPE. */
9138 155558 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9139 311116 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9140 155558 : type_npatterns, nelts_per_pattern);
9141 155558 : }
9142 :
9143 : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9144 : TYPE at compile-time. If we're unable to perform the conversion
9145 : return NULL_TREE. */
9146 :
9147 : static tree
9148 13289758 : fold_view_convert_expr (tree type, tree expr)
9149 : {
9150 13289758 : unsigned char buffer[128];
9151 13289758 : unsigned char *buf;
9152 13289758 : int len;
9153 13289758 : HOST_WIDE_INT l;
9154 :
9155 : /* Check that the host and target are sane. */
9156 13289758 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9157 : return NULL_TREE;
9158 :
9159 13289758 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9160 164517 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9161 : return res;
9162 :
9163 13134267 : l = int_size_in_bytes (type);
9164 13134267 : if (l > (int) sizeof (buffer)
9165 13134267 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9166 : {
9167 0 : buf = XALLOCAVEC (unsigned char, l);
9168 0 : len = l;
9169 : }
9170 : else
9171 : {
9172 : buf = buffer;
9173 : len = sizeof (buffer);
9174 : }
9175 13134267 : len = native_encode_expr (expr, buf, len);
9176 13134267 : if (len == 0)
9177 : return NULL_TREE;
9178 :
9179 1847676 : return native_interpret_expr (type, buf, len);
9180 : }
9181 :
9182 : /* Build an expression for the address of T. Folds away INDIRECT_REF
9183 : to avoid confusing the gimplify process. */
9184 :
9185 : tree
9186 595068045 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9187 : {
9188 : /* The size of the object is not relevant when talking about its address. */
9189 595068045 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9190 0 : t = TREE_OPERAND (t, 0);
9191 :
9192 595068045 : if (INDIRECT_REF_P (t))
9193 : {
9194 62743491 : t = TREE_OPERAND (t, 0);
9195 :
9196 62743491 : if (TREE_TYPE (t) != ptrtype)
9197 40151108 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9198 : }
9199 532324554 : else if (TREE_CODE (t) == MEM_REF
9200 532324554 : && integer_zerop (TREE_OPERAND (t, 1)))
9201 : {
9202 1680570 : t = TREE_OPERAND (t, 0);
9203 :
9204 1680570 : if (TREE_TYPE (t) != ptrtype)
9205 1102838 : t = fold_convert_loc (loc, ptrtype, t);
9206 : }
9207 530643984 : else if (TREE_CODE (t) == MEM_REF
9208 530643984 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9209 662 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9210 : TREE_OPERAND (t, 0),
9211 : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9212 530643322 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9213 : {
9214 31482084 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9215 :
9216 31482084 : if (TREE_TYPE (t) != ptrtype)
9217 16364 : t = fold_convert_loc (loc, ptrtype, t);
9218 : }
9219 : else
9220 499161238 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9221 :
9222 : return t;
9223 : }
9224 :
9225 : /* Build an expression for the address of T. */
9226 :
9227 : tree
9228 512918675 : build_fold_addr_expr_loc (location_t loc, tree t)
9229 : {
9230 512918675 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9231 :
9232 512918675 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9233 : }
9234 :
9235 : /* Fold a unary expression of code CODE and type TYPE with operand
9236 : OP0. Return the folded expression if folding is successful.
9237 : Otherwise, return NULL_TREE. */
9238 :
9239 : tree
9240 2096700937 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9241 : {
9242 2096700937 : tree tem;
9243 2096700937 : tree arg0;
9244 2096700937 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9245 :
9246 2096700937 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9247 : && TREE_CODE_LENGTH (code) == 1);
9248 :
9249 2096700937 : arg0 = op0;
9250 2096700937 : if (arg0)
9251 : {
9252 2096687655 : if (CONVERT_EXPR_CODE_P (code)
9253 : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9254 : {
9255 : /* Don't use STRIP_NOPS, because signedness of argument type
9256 : matters. */
9257 1196393590 : STRIP_SIGN_NOPS (arg0);
9258 : }
9259 : else
9260 : {
9261 : /* Strip any conversions that don't change the mode. This
9262 : is safe for every expression, except for a comparison
9263 : expression because its signedness is derived from its
9264 : operands.
9265 :
9266 : Note that this is done as an internal manipulation within
9267 : the constant folder, in order to find the simplest
9268 : representation of the arguments so that their form can be
9269 : studied. In any cases, the appropriate type conversions
9270 : should be put back in the tree that will get out of the
9271 : constant folder. */
9272 900294065 : STRIP_NOPS (arg0);
9273 : }
9274 :
9275 2096687655 : if (CONSTANT_CLASS_P (arg0))
9276 : {
9277 329532143 : tree tem = const_unop (code, type, arg0);
9278 329532143 : if (tem)
9279 : {
9280 289157668 : if (TREE_TYPE (tem) != type)
9281 9943 : tem = fold_convert_loc (loc, type, tem);
9282 : return tem;
9283 : }
9284 : }
9285 : }
9286 :
9287 1807543269 : tem = generic_simplify (loc, code, type, op0);
9288 1807543269 : if (tem)
9289 : return tem;
9290 :
9291 1359203924 : if (TREE_CODE_CLASS (code) == tcc_unary)
9292 : {
9293 774225656 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9294 1087741 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9295 : fold_build1_loc (loc, code, type,
9296 1087741 : fold_convert_loc (loc, TREE_TYPE (op0),
9297 2175482 : TREE_OPERAND (arg0, 1))));
9298 773137915 : else if (TREE_CODE (arg0) == COND_EXPR)
9299 : {
9300 558957 : tree arg01 = TREE_OPERAND (arg0, 1);
9301 558957 : tree arg02 = TREE_OPERAND (arg0, 2);
9302 558957 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9303 554775 : arg01 = fold_build1_loc (loc, code, type,
9304 : fold_convert_loc (loc,
9305 554775 : TREE_TYPE (op0), arg01));
9306 558957 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9307 548564 : arg02 = fold_build1_loc (loc, code, type,
9308 : fold_convert_loc (loc,
9309 548564 : TREE_TYPE (op0), arg02));
9310 558957 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9311 : arg01, arg02);
9312 :
9313 : /* If this was a conversion, and all we did was to move into
9314 : inside the COND_EXPR, bring it back out. But leave it if
9315 : it is a conversion from integer to integer and the
9316 : result precision is no wider than a word since such a
9317 : conversion is cheap and may be optimized away by combine,
9318 : while it couldn't if it were outside the COND_EXPR. Then return
9319 : so we don't get into an infinite recursion loop taking the
9320 : conversion out and then back in. */
9321 :
9322 558957 : if ((CONVERT_EXPR_CODE_P (code)
9323 10350 : || code == NON_LVALUE_EXPR)
9324 548626 : && TREE_CODE (tem) == COND_EXPR
9325 528708 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9326 465139 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9327 292611 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9328 292399 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9329 292399 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9330 292399 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9331 872353 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9332 21868 : && (INTEGRAL_TYPE_P
9333 : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9334 21828 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9335 21715 : || flag_syntax_only))
9336 269853 : tem = build1_loc (loc, code, type,
9337 : build3 (COND_EXPR,
9338 269853 : TREE_TYPE (TREE_OPERAND
9339 : (TREE_OPERAND (tem, 1), 0)),
9340 269853 : TREE_OPERAND (tem, 0),
9341 269853 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9342 269853 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9343 : 0)));
9344 : return tem;
9345 : }
9346 : }
9347 :
9348 1357557226 : switch (code)
9349 : {
9350 43258383 : case NON_LVALUE_EXPR:
9351 43258383 : if (!maybe_lvalue_p (op0))
9352 31478481 : return fold_convert_loc (loc, type, op0);
9353 : return NULL_TREE;
9354 :
9355 718802265 : CASE_CONVERT:
9356 718802265 : case FLOAT_EXPR:
9357 718802265 : case FIX_TRUNC_EXPR:
9358 718802265 : if (COMPARISON_CLASS_P (op0))
9359 : {
9360 : /* If we have (type) (a CMP b) and type is an integral type, return
9361 : new expression involving the new type. Canonicalize
9362 : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9363 : non-integral type.
9364 : Do not fold the result as that would not simplify further, also
9365 : folding again results in recursions. */
9366 746956 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9367 166441 : return build2_loc (loc, TREE_CODE (op0), type,
9368 166441 : TREE_OPERAND (op0, 0),
9369 332882 : TREE_OPERAND (op0, 1));
9370 580515 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9371 7653 : && TREE_CODE (type) != VECTOR_TYPE)
9372 7653 : return build3_loc (loc, COND_EXPR, type, op0,
9373 : constant_boolean_node (true, type),
9374 7653 : constant_boolean_node (false, type));
9375 : }
9376 :
9377 : /* Handle (T *)&A.B.C for A being of type T and B and C
9378 : living at offset zero. This occurs frequently in
9379 : C++ upcasting and then accessing the base. */
9380 718628171 : if (TREE_CODE (op0) == ADDR_EXPR
9381 252806316 : && POINTER_TYPE_P (type)
9382 964558835 : && handled_component_p (TREE_OPERAND (op0, 0)))
9383 : {
9384 54269266 : poly_int64 bitsize, bitpos;
9385 54269266 : tree offset;
9386 54269266 : machine_mode mode;
9387 54269266 : int unsignedp, reversep, volatilep;
9388 54269266 : tree base
9389 54269266 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9390 : &offset, &mode, &unsignedp, &reversep,
9391 : &volatilep);
9392 : /* If the reference was to a (constant) zero offset, we can use
9393 : the address of the base if it has the same base type
9394 : as the result type and the pointer type is unqualified. */
9395 54269266 : if (!offset
9396 54123325 : && known_eq (bitpos, 0)
9397 37340730 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9398 37340730 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9399 54282317 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9400 12850 : return fold_convert_loc (loc, type,
9401 12850 : build_fold_addr_expr_loc (loc, base));
9402 : }
9403 :
9404 718615321 : if (TREE_CODE (op0) == MODIFY_EXPR
9405 293344 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9406 : /* Detect assigning a bitfield. */
9407 718617324 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9408 118 : && DECL_BIT_FIELD
9409 : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9410 : {
9411 : /* Don't leave an assignment inside a conversion
9412 : unless assigning a bitfield. */
9413 1955 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9414 : /* First do the assignment, then return converted constant. */
9415 1955 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9416 1955 : suppress_warning (tem /* What warning? */);
9417 1955 : TREE_USED (tem) = 1;
9418 1955 : return tem;
9419 : }
9420 :
9421 : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9422 : constants (if x has signed type, the sign bit cannot be set
9423 : in c). This folds extension into the BIT_AND_EXPR.
9424 : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9425 : very likely don't have maximal range for their precision and this
9426 : transformation effectively doesn't preserve non-maximal ranges. */
9427 718613366 : if (TREE_CODE (type) == INTEGER_TYPE
9428 248768526 : && TREE_CODE (op0) == BIT_AND_EXPR
9429 719230204 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9430 : {
9431 217917 : tree and_expr = op0;
9432 217917 : tree and0 = TREE_OPERAND (and_expr, 0);
9433 217917 : tree and1 = TREE_OPERAND (and_expr, 1);
9434 217917 : int change = 0;
9435 :
9436 217917 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9437 217917 : || (TYPE_PRECISION (type)
9438 59323 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9439 : change = 1;
9440 18092 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9441 : <= HOST_BITS_PER_WIDE_INT
9442 18092 : && tree_fits_uhwi_p (and1))
9443 : {
9444 16978 : unsigned HOST_WIDE_INT cst;
9445 :
9446 16978 : cst = tree_to_uhwi (and1);
9447 33956 : cst &= HOST_WIDE_INT_M1U
9448 16978 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9449 16978 : change = (cst == 0);
9450 16978 : if (change
9451 16978 : && !flag_syntax_only
9452 33956 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9453 : == ZERO_EXTEND))
9454 : {
9455 : tree uns = unsigned_type_for (TREE_TYPE (and0));
9456 : and0 = fold_convert_loc (loc, uns, and0);
9457 : and1 = fold_convert_loc (loc, uns, and1);
9458 : }
9459 : }
9460 16978 : if (change)
9461 : {
9462 216803 : tree and1_type = TREE_TYPE (and1);
9463 216803 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9464 : TYPE_PRECISION (type));
9465 216803 : tem = force_fit_type (type,
9466 216803 : wide_int::from (wi::to_wide (and1), prec,
9467 216803 : TYPE_SIGN (and1_type)),
9468 216803 : 0, TREE_OVERFLOW (and1));
9469 216803 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9470 216803 : fold_convert_loc (loc, type, and0), tem);
9471 : }
9472 : }
9473 :
9474 : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9475 : cast (T1)X will fold away. We assume that this happens when X itself
9476 : is a cast. */
9477 718396563 : if (POINTER_TYPE_P (type)
9478 434577600 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9479 752258668 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9480 : {
9481 31239882 : tree arg00 = TREE_OPERAND (arg0, 0);
9482 31239882 : tree arg01 = TREE_OPERAND (arg0, 1);
9483 :
9484 : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9485 : when the pointed type needs higher alignment than
9486 : the p+ first operand's pointed type. */
9487 31239882 : if (!in_gimple_form
9488 31219361 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9489 31241096 : && (min_align_of_type (TREE_TYPE (type))
9490 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9491 : return NULL_TREE;
9492 :
9493 : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9494 : when type is a reference type and arg00's type is not,
9495 : because arg00 could be validly nullptr and if arg01 doesn't return,
9496 : we don't want false positive binding of reference to nullptr. */
9497 31239815 : if (TREE_CODE (type) == REFERENCE_TYPE
9498 17581855 : && !in_gimple_form
9499 17581835 : && sanitize_flags_p (SANITIZE_NULL)
9500 31240246 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9501 : return NULL_TREE;
9502 :
9503 31239384 : arg00 = fold_convert_loc (loc, type, arg00);
9504 31239384 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9505 : }
9506 :
9507 : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9508 : of the same precision, and X is an integer type not narrower than
9509 : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9510 687156681 : if (INTEGRAL_TYPE_P (type)
9511 255025185 : && TREE_CODE (op0) == BIT_NOT_EXPR
9512 585463 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9513 585463 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9514 687542553 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9515 : {
9516 383389 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9517 457066 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9518 457064 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9519 318262 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9520 318262 : fold_convert_loc (loc, type, tem));
9521 : }
9522 :
9523 : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9524 : type of X and Y (integer types only). */
9525 686838419 : if (INTEGRAL_TYPE_P (type)
9526 254706923 : && TREE_CODE (op0) == MULT_EXPR
9527 9244402 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9528 9223454 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9529 686898042 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9530 21025 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9531 : {
9532 : /* Be careful not to introduce new overflows. */
9533 59569 : tree mult_type;
9534 59569 : if (TYPE_OVERFLOW_WRAPS (type))
9535 : mult_type = type;
9536 : else
9537 2100 : mult_type = unsigned_type_for (type);
9538 :
9539 59569 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9540 : {
9541 119138 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9542 : fold_convert_loc (loc, mult_type,
9543 59569 : TREE_OPERAND (op0, 0)),
9544 : fold_convert_loc (loc, mult_type,
9545 59569 : TREE_OPERAND (op0, 1)));
9546 59569 : return fold_convert_loc (loc, type, tem);
9547 : }
9548 : }
9549 :
9550 : return NULL_TREE;
9551 :
9552 238772526 : case VIEW_CONVERT_EXPR:
9553 238772526 : if (TREE_CODE (op0) == MEM_REF)
9554 : {
9555 2691 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9556 18 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9557 2691 : tem = fold_build2_loc (loc, MEM_REF, type,
9558 2691 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9559 2691 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9560 2691 : return tem;
9561 : }
9562 :
9563 : return NULL_TREE;
9564 :
9565 4253366 : case NEGATE_EXPR:
9566 4253366 : tem = fold_negate_expr (loc, arg0);
9567 4253366 : if (tem)
9568 1680 : return fold_convert_loc (loc, type, tem);
9569 : return NULL_TREE;
9570 :
9571 3466399 : case ABS_EXPR:
9572 : /* Convert fabs((double)float) into (double)fabsf(float). */
9573 3466399 : if (TREE_CODE (arg0) == NOP_EXPR
9574 23511 : && TREE_CODE (type) == REAL_TYPE)
9575 : {
9576 23457 : tree targ0 = strip_float_extensions (arg0);
9577 23457 : if (targ0 != arg0)
9578 23253 : return fold_convert_loc (loc, type,
9579 : fold_build1_loc (loc, ABS_EXPR,
9580 23253 : TREE_TYPE (targ0),
9581 23253 : targ0));
9582 : }
9583 : return NULL_TREE;
9584 :
9585 2792482 : case BIT_NOT_EXPR:
9586 : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9587 2792482 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9588 2794174 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9589 : fold_convert_loc (loc, type,
9590 1692 : TREE_OPERAND (arg0, 0)))))
9591 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9592 : fold_convert_loc (loc, type,
9593 28 : TREE_OPERAND (arg0, 1)));
9594 2792468 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9595 2794146 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9596 : fold_convert_loc (loc, type,
9597 1678 : TREE_OPERAND (arg0, 1)))))
9598 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9599 : fold_convert_loc (loc, type,
9600 46 : TREE_OPERAND (arg0, 0)), tem);
9601 :
9602 : return NULL_TREE;
9603 :
9604 51301687 : case TRUTH_NOT_EXPR:
9605 : /* Note that the operand of this must be an int
9606 : and its values must be 0 or 1.
9607 : ("true" is a fixed value perhaps depending on the language,
9608 : but we don't handle values other than 1 correctly yet.) */
9609 51301687 : tem = fold_truth_not_expr (loc, arg0);
9610 51301687 : if (!tem)
9611 : return NULL_TREE;
9612 34684510 : return fold_convert_loc (loc, type, tem);
9613 :
9614 70261340 : case INDIRECT_REF:
9615 : /* Fold *&X to X if X is an lvalue. */
9616 70261340 : if (TREE_CODE (op0) == ADDR_EXPR)
9617 : {
9618 7881 : tree op00 = TREE_OPERAND (op0, 0);
9619 7881 : if ((VAR_P (op00)
9620 : || TREE_CODE (op00) == PARM_DECL
9621 : || TREE_CODE (op00) == RESULT_DECL)
9622 6735 : && !TREE_READONLY (op00))
9623 6618 : return op00;
9624 : }
9625 : return NULL_TREE;
9626 :
9627 : default:
9628 : return NULL_TREE;
9629 : } /* switch (code) */
9630 : }
9631 :
9632 :
9633 : /* If the operation was a conversion do _not_ mark a resulting constant
9634 : with TREE_OVERFLOW if the original constant was not. These conversions
9635 : have implementation defined behavior and retaining the TREE_OVERFLOW
9636 : flag here would confuse later passes such as VRP. */
9637 : tree
9638 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9639 : tree type, tree op0)
9640 : {
9641 0 : tree res = fold_unary_loc (loc, code, type, op0);
9642 0 : if (res
9643 0 : && TREE_CODE (res) == INTEGER_CST
9644 0 : && TREE_CODE (op0) == INTEGER_CST
9645 0 : && CONVERT_EXPR_CODE_P (code))
9646 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9647 :
9648 0 : return res;
9649 : }
9650 :
9651 : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9652 : operands OP0 and OP1. LOC is the location of the resulting expression.
9653 : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9654 : Return the folded expression if folding is successful. Otherwise,
9655 : return NULL_TREE. */
9656 : static tree
9657 25735295 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9658 : tree arg0, tree arg1, tree op0, tree op1)
9659 : {
9660 25735295 : tree tem;
9661 :
9662 : /* We only do these simplifications if we are optimizing. */
9663 25735295 : if (!optimize)
9664 : return NULL_TREE;
9665 :
9666 : /* Check for things like (A || B) && (A || C). We can convert this
9667 : to A || (B && C). Note that either operator can be any of the four
9668 : truth and/or operations and the transformation will still be
9669 : valid. Also note that we only care about order for the
9670 : ANDIF and ORIF operators. If B contains side effects, this
9671 : might change the truth-value of A. */
9672 25243105 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9673 5970123 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9674 : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9675 : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9676 5970123 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9677 25305968 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9678 : {
9679 62359 : tree a00 = TREE_OPERAND (arg0, 0);
9680 62359 : tree a01 = TREE_OPERAND (arg0, 1);
9681 62359 : tree a10 = TREE_OPERAND (arg1, 0);
9682 62359 : tree a11 = TREE_OPERAND (arg1, 1);
9683 124718 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9684 62359 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9685 62359 : && (code == TRUTH_AND_EXPR
9686 23223 : || code == TRUTH_OR_EXPR));
9687 :
9688 62359 : if (operand_equal_p (a00, a10, 0))
9689 849 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9690 849 : fold_build2_loc (loc, code, type, a01, a11));
9691 61510 : else if (commutative && operand_equal_p (a00, a11, 0))
9692 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9693 0 : fold_build2_loc (loc, code, type, a01, a10));
9694 61510 : else if (commutative && operand_equal_p (a01, a10, 0))
9695 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9696 0 : fold_build2_loc (loc, code, type, a00, a11));
9697 :
9698 : /* This case if tricky because we must either have commutative
9699 : operators or else A10 must not have side-effects. */
9700 :
9701 61464 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9702 122458 : && operand_equal_p (a01, a11, 0))
9703 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9704 : fold_build2_loc (loc, code, type, a00, a10),
9705 43 : a01);
9706 : }
9707 :
9708 : /* See if we can build a range comparison. */
9709 25242213 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9710 : return tem;
9711 :
9712 24060674 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9713 24058664 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9714 : {
9715 40175 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9716 40175 : if (tem)
9717 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9718 : }
9719 :
9720 24060661 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9721 24049548 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9722 : {
9723 89730 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9724 89730 : if (tem)
9725 91 : return fold_build2_loc (loc, code, type, arg0, tem);
9726 : }
9727 :
9728 : /* Check for the possibility of merging component references. If our
9729 : lhs is another similar operation, try to merge its rhs with our
9730 : rhs. Then try to merge our lhs and rhs. */
9731 24060570 : if (TREE_CODE (arg0) == code
9732 24910655 : && (tem = fold_truth_andor_1 (loc, code, type,
9733 850085 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9734 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9735 :
9736 24060485 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9737 : return tem;
9738 :
9739 24020935 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9740 24020935 : if (param_logical_op_non_short_circuit != -1)
9741 7776 : logical_op_non_short_circuit
9742 7776 : = param_logical_op_non_short_circuit;
9743 24020935 : if (logical_op_non_short_circuit
9744 24017003 : && !sanitize_coverage_p ()
9745 24020935 : && (code == TRUTH_AND_EXPR
9746 24017000 : || code == TRUTH_ANDIF_EXPR
9747 11072493 : || code == TRUTH_OR_EXPR
9748 11072493 : || code == TRUTH_ORIF_EXPR))
9749 : {
9750 24017000 : enum tree_code ncode, icode;
9751 :
9752 24017000 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9753 24017000 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9754 11072493 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9755 :
9756 : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9757 : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9758 : We don't want to pack more than two leafs to a non-IF AND/OR
9759 : expression.
9760 : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9761 : equal to IF-CODE, then we don't want to add right-hand operand.
9762 : If the inner right-hand side of left-hand operand has
9763 : side-effects, or isn't simple, then we can't add to it,
9764 : as otherwise we might destroy if-sequence. */
9765 24017000 : if (TREE_CODE (arg0) == icode
9766 841033 : && simple_condition_p (arg1)
9767 : /* Needed for sequence points to handle trappings, and
9768 : side-effects. */
9769 24066054 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9770 : {
9771 42187 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9772 : arg1);
9773 42187 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9774 42187 : tem);
9775 : }
9776 : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9777 : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9778 23974813 : else if (TREE_CODE (arg1) == icode
9779 6358 : && simple_condition_p (arg0)
9780 : /* Needed for sequence points to handle trappings, and
9781 : side-effects. */
9782 23975770 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9783 : {
9784 36 : tem = fold_build2_loc (loc, ncode, type,
9785 36 : arg0, TREE_OPERAND (arg1, 0));
9786 36 : return fold_build2_loc (loc, icode, type, tem,
9787 72 : TREE_OPERAND (arg1, 1));
9788 : }
9789 : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9790 : into (A OR B).
9791 : For sequence point consistency, we need to check for trapping,
9792 : and side-effects. */
9793 5370537 : else if (code == icode && simple_condition_p (arg0)
9794 24870016 : && simple_condition_p (arg1))
9795 445401 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9796 : }
9797 :
9798 : return NULL_TREE;
9799 : }
9800 :
9801 : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9802 : by changing CODE to reduce the magnitude of constants involved in
9803 : ARG0 of the comparison.
9804 : Returns a canonicalized comparison tree if a simplification was
9805 : possible, otherwise returns NULL_TREE. */
9806 :
9807 : static tree
9808 181873107 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9809 : tree arg0, tree arg1)
9810 : {
9811 181873107 : enum tree_code code0 = TREE_CODE (arg0);
9812 181873107 : tree t, cst0 = NULL_TREE;
9813 181873107 : int sgn0;
9814 :
9815 : /* Match A +- CST code arg1. We can change this only if overflow
9816 : is undefined. */
9817 181873107 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9818 138509677 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9819 : /* In principle pointers also have undefined overflow behavior,
9820 : but that causes problems elsewhere. */
9821 68129688 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9822 68129688 : && (code0 == MINUS_EXPR
9823 68129688 : || code0 == PLUS_EXPR)
9824 2594097 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9825 : return NULL_TREE;
9826 :
9827 : /* Identify the constant in arg0 and its sign. */
9828 2128627 : cst0 = TREE_OPERAND (arg0, 1);
9829 2128627 : sgn0 = tree_int_cst_sgn (cst0);
9830 :
9831 : /* Overflowed constants and zero will cause problems. */
9832 2128627 : if (integer_zerop (cst0)
9833 2128627 : || TREE_OVERFLOW (cst0))
9834 : return NULL_TREE;
9835 :
9836 : /* See if we can reduce the magnitude of the constant in
9837 : arg0 by changing the comparison code. */
9838 : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9839 2128627 : if (code == LT_EXPR
9840 1259857 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9841 : code = LE_EXPR;
9842 : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9843 1949281 : else if (code == GT_EXPR
9844 582293 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9845 : code = GE_EXPR;
9846 : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9847 1764747 : else if (code == LE_EXPR
9848 677712 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9849 : code = LT_EXPR;
9850 : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9851 1524409 : else if (code == GE_EXPR
9852 481238 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9853 : code = GT_EXPR;
9854 : else
9855 : return NULL_TREE;
9856 :
9857 : /* Now build the constant reduced in magnitude. But not if that
9858 : would produce one outside of its types range. */
9859 1614992 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9860 1614992 : && ((sgn0 == 1
9861 424734 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9862 424734 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9863 807496 : || (sgn0 == -1
9864 382762 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9865 382762 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9866 : return NULL_TREE;
9867 :
9868 1232230 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9869 807496 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9870 807496 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9871 807496 : t = fold_convert (TREE_TYPE (arg1), t);
9872 :
9873 807496 : return fold_build2_loc (loc, code, type, t, arg1);
9874 : }
9875 :
9876 : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9877 : overflow further. Try to decrease the magnitude of constants involved
9878 : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9879 : and put sole constants at the second argument position.
9880 : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9881 :
9882 : static tree
9883 91318419 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9884 : tree arg0, tree arg1)
9885 : {
9886 91318419 : tree t;
9887 :
9888 : /* Try canonicalization by simplifying arg0. */
9889 91318419 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1);
9890 91318419 : if (t)
9891 : return t;
9892 :
9893 : /* Try canonicalization by simplifying arg1 using the swapped
9894 : comparison. */
9895 90554688 : code = swap_tree_comparison (code);
9896 90554688 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0);
9897 90554688 : return t;
9898 : }
9899 :
9900 : /* Return a positive integer when the symbol DECL is known to have
9901 : a nonzero address, zero when it's known not to (e.g., it's a weak
9902 : symbol), and a negative integer when the symbol is not yet in the
9903 : symbol table and so whether or not its address is zero is unknown.
9904 : For function local objects always return positive integer. */
9905 : static int
9906 12602356 : maybe_nonzero_address (tree decl)
9907 : {
9908 12602356 : if (!DECL_P (decl))
9909 : return -1;
9910 :
9911 : /* Normally, don't do anything for variables and functions before symtab is
9912 : built; it is quite possible that DECL will be declared weak later.
9913 : But if folding_initializer, we need a constant answer now, so create
9914 : the symtab entry and prevent later weak declaration. */
9915 10376922 : if (decl_in_symtab_p (decl))
9916 : {
9917 4551686 : if (struct symtab_node *symbol
9918 4551686 : = (folding_initializer
9919 4551686 : ? symtab_node::get_create (decl)
9920 4534371 : : symtab_node::get (decl)))
9921 4532673 : return symbol->nonzero_address ();
9922 : }
9923 5825236 : else if (folding_cxx_constexpr)
9924 : /* Anything that doesn't go in the symtab has non-zero address. */
9925 : return 1;
9926 :
9927 : /* Function local objects are never NULL. */
9928 5687595 : if (DECL_CONTEXT (decl)
9929 5670042 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9930 11354174 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9931 5585593 : return 1;
9932 :
9933 : return -1;
9934 : }
9935 :
9936 : /* Subroutine of fold_binary. This routine performs all of the
9937 : transformations that are common to the equality/inequality
9938 : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9939 : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9940 : fold_binary should call fold_binary. Fold a comparison with
9941 : tree code CODE and type TYPE with operands OP0 and OP1. Return
9942 : the folded comparison or NULL_TREE. */
9943 :
9944 : static tree
9945 91390412 : fold_comparison (location_t loc, enum tree_code code, tree type,
9946 : tree op0, tree op1)
9947 : {
9948 91390412 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9949 91390412 : tree arg0, arg1, tem;
9950 :
9951 91390412 : arg0 = op0;
9952 91390412 : arg1 = op1;
9953 :
9954 91390412 : STRIP_SIGN_NOPS (arg0);
9955 91390412 : STRIP_SIGN_NOPS (arg1);
9956 :
9957 : /* For comparisons of pointers we can decompose it to a compile time
9958 : comparison of the base objects and the offsets into the object.
9959 : This requires at least one operand being an ADDR_EXPR or a
9960 : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9961 169069003 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9962 91611774 : && (TREE_CODE (arg0) == ADDR_EXPR
9963 13879276 : || TREE_CODE (arg1) == ADDR_EXPR
9964 12302763 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9965 11511183 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9966 : {
9967 2432124 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9968 2432124 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9969 2432124 : machine_mode mode;
9970 2432124 : int volatilep, reversep, unsignedp;
9971 2432124 : bool indirect_base0 = false, indirect_base1 = false;
9972 :
9973 : /* Get base and offset for the access. Strip ADDR_EXPR for
9974 : get_inner_reference, but put it back by stripping INDIRECT_REF
9975 : off the base object if possible. indirect_baseN will be true
9976 : if baseN is not an address but refers to the object itself. */
9977 2432124 : base0 = arg0;
9978 2432124 : if (TREE_CODE (arg0) == ADDR_EXPR)
9979 : {
9980 53907 : base0
9981 53907 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9982 : &bitsize, &bitpos0, &offset0, &mode,
9983 : &unsignedp, &reversep, &volatilep);
9984 53907 : if (INDIRECT_REF_P (base0))
9985 2286 : base0 = TREE_OPERAND (base0, 0);
9986 : else
9987 : indirect_base0 = true;
9988 : }
9989 2378217 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9990 : {
9991 853493 : base0 = TREE_OPERAND (arg0, 0);
9992 853493 : STRIP_SIGN_NOPS (base0);
9993 853493 : if (TREE_CODE (base0) == ADDR_EXPR)
9994 : {
9995 27608 : base0
9996 27608 : = get_inner_reference (TREE_OPERAND (base0, 0),
9997 : &bitsize, &bitpos0, &offset0, &mode,
9998 : &unsignedp, &reversep, &volatilep);
9999 27608 : if (INDIRECT_REF_P (base0))
10000 20 : base0 = TREE_OPERAND (base0, 0);
10001 : else
10002 : indirect_base0 = true;
10003 : }
10004 853493 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10005 853493 : offset0 = TREE_OPERAND (arg0, 1);
10006 : else
10007 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10008 : TREE_OPERAND (arg0, 1));
10009 853493 : if (poly_int_tree_p (offset0))
10010 : {
10011 686683 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10012 686683 : TYPE_PRECISION (sizetype));
10013 686683 : tem <<= LOG2_BITS_PER_UNIT;
10014 686683 : tem += bitpos0;
10015 686683 : if (tem.to_shwi (&bitpos0))
10016 686683 : offset0 = NULL_TREE;
10017 : }
10018 : }
10019 :
10020 2432124 : base1 = arg1;
10021 2432124 : if (TREE_CODE (arg1) == ADDR_EXPR)
10022 : {
10023 1606410 : base1
10024 1606410 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10025 : &bitsize, &bitpos1, &offset1, &mode,
10026 : &unsignedp, &reversep, &volatilep);
10027 1606410 : if (INDIRECT_REF_P (base1))
10028 70794 : base1 = TREE_OPERAND (base1, 0);
10029 : else
10030 : indirect_base1 = true;
10031 : }
10032 825714 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10033 : {
10034 91913 : base1 = TREE_OPERAND (arg1, 0);
10035 91913 : STRIP_SIGN_NOPS (base1);
10036 91913 : if (TREE_CODE (base1) == ADDR_EXPR)
10037 : {
10038 11304 : base1
10039 11304 : = get_inner_reference (TREE_OPERAND (base1, 0),
10040 : &bitsize, &bitpos1, &offset1, &mode,
10041 : &unsignedp, &reversep, &volatilep);
10042 11304 : if (INDIRECT_REF_P (base1))
10043 0 : base1 = TREE_OPERAND (base1, 0);
10044 : else
10045 : indirect_base1 = true;
10046 : }
10047 91913 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10048 91889 : offset1 = TREE_OPERAND (arg1, 1);
10049 : else
10050 24 : offset1 = size_binop (PLUS_EXPR, offset1,
10051 : TREE_OPERAND (arg1, 1));
10052 91913 : if (poly_int_tree_p (offset1))
10053 : {
10054 81424 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10055 81424 : TYPE_PRECISION (sizetype));
10056 81424 : tem <<= LOG2_BITS_PER_UNIT;
10057 81424 : tem += bitpos1;
10058 81424 : if (tem.to_shwi (&bitpos1))
10059 81424 : offset1 = NULL_TREE;
10060 : }
10061 : }
10062 :
10063 : /* If we have equivalent bases we might be able to simplify. */
10064 2432124 : if (indirect_base0 == indirect_base1
10065 3274656 : && operand_equal_p (base0, base1,
10066 : indirect_base0 ? OEP_ADDRESS_OF : 0))
10067 : {
10068 : /* We can fold this expression to a constant if the non-constant
10069 : offset parts are equal. */
10070 17011 : if ((offset0 == offset1
10071 6682 : || (offset0 && offset1
10072 2736 : && operand_equal_p (offset0, offset1, 0)))
10073 17011 : && (equality_code
10074 10285 : || (indirect_base0
10075 6450 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10076 3835 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10077 : {
10078 10280 : switch (code)
10079 : {
10080 35 : case EQ_EXPR:
10081 35 : if (known_eq (bitpos0, bitpos1))
10082 49437 : return constant_boolean_node (true, type);
10083 21 : if (known_ne (bitpos0, bitpos1))
10084 21 : return constant_boolean_node (false, type);
10085 : break;
10086 9 : case NE_EXPR:
10087 9 : if (known_ne (bitpos0, bitpos1))
10088 7 : return constant_boolean_node (true, type);
10089 2 : if (known_eq (bitpos0, bitpos1))
10090 2 : return constant_boolean_node (false, type);
10091 : break;
10092 2407 : case LT_EXPR:
10093 2407 : if (known_lt (bitpos0, bitpos1))
10094 2263 : return constant_boolean_node (true, type);
10095 144 : if (known_ge (bitpos0, bitpos1))
10096 144 : return constant_boolean_node (false, type);
10097 : break;
10098 1756 : case LE_EXPR:
10099 1756 : if (known_le (bitpos0, bitpos1))
10100 182 : return constant_boolean_node (true, type);
10101 1574 : if (known_gt (bitpos0, bitpos1))
10102 1574 : return constant_boolean_node (false, type);
10103 : break;
10104 3663 : case GE_EXPR:
10105 3663 : if (known_ge (bitpos0, bitpos1))
10106 1464 : return constant_boolean_node (true, type);
10107 2199 : if (known_lt (bitpos0, bitpos1))
10108 2199 : return constant_boolean_node (false, type);
10109 : break;
10110 2410 : case GT_EXPR:
10111 2410 : if (known_gt (bitpos0, bitpos1))
10112 2351 : return constant_boolean_node (true, type);
10113 59 : if (known_le (bitpos0, bitpos1))
10114 59 : return constant_boolean_node (false, type);
10115 : break;
10116 : default:;
10117 : }
10118 : }
10119 : /* We can simplify the comparison to a comparison of the variable
10120 : offset parts if the constant offset parts are equal.
10121 : Be careful to use signed sizetype here because otherwise we
10122 : mess with array offsets in the wrong way. This is possible
10123 : because pointer arithmetic is restricted to retain within an
10124 : object and overflow on pointer differences is undefined as of
10125 : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10126 6731 : else if (known_eq (bitpos0, bitpos1)
10127 6731 : && (equality_code
10128 5253 : || (indirect_base0
10129 285 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10130 4968 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10131 : {
10132 : /* By converting to signed sizetype we cover middle-end pointer
10133 : arithmetic which operates on unsigned pointer types of size
10134 : type size and ARRAY_REF offsets which are properly sign or
10135 : zero extended from their type in case it is narrower than
10136 : sizetype. */
10137 5362 : if (offset0 == NULL_TREE)
10138 0 : offset0 = build_int_cst (ssizetype, 0);
10139 : else
10140 5362 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10141 5362 : if (offset1 == NULL_TREE)
10142 2658 : offset1 = build_int_cst (ssizetype, 0);
10143 : else
10144 2704 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10145 :
10146 5362 : return fold_build2_loc (loc, code, type, offset0, offset1);
10147 : }
10148 : }
10149 : /* For equal offsets we can simplify to a comparison of the
10150 : base addresses. */
10151 2415113 : else if (known_eq (bitpos0, bitpos1)
10152 55847 : && (indirect_base0
10153 1008726 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10154 16036 : && (indirect_base1
10155 192233 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10156 2645418 : && ((offset0 == offset1)
10157 4399 : || (offset0 && offset1
10158 4129 : && operand_equal_p (offset0, offset1, 0))))
10159 : {
10160 33674 : if (indirect_base0)
10161 3779 : base0 = build_fold_addr_expr_loc (loc, base0);
10162 33674 : if (indirect_base1)
10163 5416 : base1 = build_fold_addr_expr_loc (loc, base1);
10164 33674 : return fold_build2_loc (loc, code, type, base0, base1);
10165 : }
10166 : /* Comparison between an ordinary (non-weak) symbol and a null
10167 : pointer can be eliminated since such symbols must have a non
10168 : null address. In C, relational expressions between pointers
10169 : to objects and null pointers are undefined. The results
10170 : below follow the C++ rules with the additional property that
10171 : every object pointer compares greater than a null pointer.
10172 : */
10173 2381439 : else if (((DECL_P (base0)
10174 256809 : && maybe_nonzero_address (base0) > 0
10175 : /* Avoid folding references to struct members at offset 0 to
10176 : prevent tests like '&ptr->firstmember == 0' from getting
10177 : eliminated. When ptr is null, although the -> expression
10178 : is strictly speaking invalid, GCC retains it as a matter
10179 : of QoI. See PR c/44555. */
10180 241908 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10181 2365403 : || CONSTANT_CLASS_P (base0))
10182 20837 : && indirect_base0
10183 : /* The caller guarantees that when one of the arguments is
10184 : constant (i.e., null in this case) it is second. */
10185 2399250 : && integer_zerop (arg1))
10186 : {
10187 121 : switch (code)
10188 : {
10189 24 : case EQ_EXPR:
10190 24 : case LE_EXPR:
10191 24 : case LT_EXPR:
10192 24 : return constant_boolean_node (false, type);
10193 97 : case GE_EXPR:
10194 97 : case GT_EXPR:
10195 97 : case NE_EXPR:
10196 97 : return constant_boolean_node (true, type);
10197 0 : default:
10198 0 : gcc_unreachable ();
10199 : }
10200 : }
10201 : }
10202 :
10203 : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10204 : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10205 : the resulting offset is smaller in absolute value than the
10206 : original one and has the same sign. */
10207 179909094 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10208 139318188 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10209 34469269 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10210 2339468 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10211 1928355 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10212 1928355 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10213 161164021 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10214 161539 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10215 : {
10216 161539 : tree const1 = TREE_OPERAND (arg0, 1);
10217 161539 : tree const2 = TREE_OPERAND (arg1, 1);
10218 161539 : tree variable1 = TREE_OPERAND (arg0, 0);
10219 161539 : tree variable2 = TREE_OPERAND (arg1, 0);
10220 161539 : tree cst;
10221 :
10222 : /* Put the constant on the side where it doesn't overflow and is
10223 : of lower absolute value and of same sign than before. */
10224 161540 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10225 : ? MINUS_EXPR : PLUS_EXPR,
10226 : const2, const1);
10227 161539 : if (!TREE_OVERFLOW (cst)
10228 161515 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10229 184095 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10230 5778 : return fold_build2_loc (loc, code, type,
10231 : variable1,
10232 5778 : fold_build2_loc (loc, TREE_CODE (arg1),
10233 5778 : TREE_TYPE (arg1),
10234 5778 : variable2, cst));
10235 :
10236 155762 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10237 : ? MINUS_EXPR : PLUS_EXPR,
10238 : const1, const2);
10239 155761 : if (!TREE_OVERFLOW (cst)
10240 155737 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10241 172539 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10242 16778 : return fold_build2_loc (loc, code, type,
10243 16778 : fold_build2_loc (loc, TREE_CODE (arg0),
10244 16778 : TREE_TYPE (arg0),
10245 : variable1, cst),
10246 16778 : variable2);
10247 : }
10248 :
10249 91318419 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10250 91318419 : if (tem)
10251 : return tem;
10252 :
10253 : /* If we are comparing an expression that just has comparisons
10254 : of two integer values, arithmetic expressions of those comparisons,
10255 : and constants, we can simplify it. There are only three cases
10256 : to check: the two values can either be equal, the first can be
10257 : greater, or the second can be greater. Fold the expression for
10258 : those three values. Since each value must be 0 or 1, we have
10259 : eight possibilities, each of which corresponds to the constant 0
10260 : or 1 or one of the six possible comparisons.
10261 :
10262 : This handles common cases like (a > b) == 0 but also handles
10263 : expressions like ((x > y) - (y > x)) > 0, which supposedly
10264 : occur in macroized code. */
10265 :
10266 90510923 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10267 : {
10268 55240360 : tree cval1 = 0, cval2 = 0;
10269 :
10270 55240360 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10271 : /* Don't handle degenerate cases here; they should already
10272 : have been handled anyway. */
10273 619012 : && cval1 != 0 && cval2 != 0
10274 617718 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10275 617718 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10276 617712 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10277 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10278 54 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10279 55240414 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10280 54 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10281 : {
10282 54 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10283 54 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10284 :
10285 : /* We can't just pass T to eval_subst in case cval1 or cval2
10286 : was the same as ARG1. */
10287 :
10288 54 : tree high_result
10289 54 : = fold_build2_loc (loc, code, type,
10290 : eval_subst (loc, arg0, cval1, maxval,
10291 : cval2, minval),
10292 : arg1);
10293 54 : tree equal_result
10294 54 : = fold_build2_loc (loc, code, type,
10295 : eval_subst (loc, arg0, cval1, maxval,
10296 : cval2, maxval),
10297 : arg1);
10298 54 : tree low_result
10299 54 : = fold_build2_loc (loc, code, type,
10300 : eval_subst (loc, arg0, cval1, minval,
10301 : cval2, maxval),
10302 : arg1);
10303 :
10304 : /* All three of these results should be 0 or 1. Confirm they are.
10305 : Then use those values to select the proper code to use. */
10306 :
10307 54 : if (TREE_CODE (high_result) == INTEGER_CST
10308 54 : && TREE_CODE (equal_result) == INTEGER_CST
10309 44 : && TREE_CODE (low_result) == INTEGER_CST)
10310 : {
10311 : /* Make a 3-bit mask with the high-order bit being the
10312 : value for `>', the next for '=', and the low for '<'. */
10313 44 : switch ((integer_onep (high_result) * 4)
10314 44 : + (integer_onep (equal_result) * 2)
10315 44 : + integer_onep (low_result))
10316 : {
10317 21 : case 0:
10318 : /* Always false. */
10319 44 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10320 : case 1:
10321 : code = LT_EXPR;
10322 : break;
10323 2 : case 2:
10324 2 : code = EQ_EXPR;
10325 2 : break;
10326 0 : case 3:
10327 0 : code = LE_EXPR;
10328 0 : break;
10329 0 : case 4:
10330 0 : code = GT_EXPR;
10331 0 : break;
10332 5 : case 5:
10333 5 : code = NE_EXPR;
10334 5 : break;
10335 0 : case 6:
10336 0 : code = GE_EXPR;
10337 0 : break;
10338 16 : case 7:
10339 : /* Always true. */
10340 16 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10341 : }
10342 :
10343 7 : return fold_build2_loc (loc, code, type, cval1, cval2);
10344 : }
10345 : }
10346 : }
10347 :
10348 : return NULL_TREE;
10349 : }
10350 :
10351 :
10352 : /* Subroutine of fold_binary. Optimize complex multiplications of the
10353 : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10354 : argument EXPR represents the expression "z" of type TYPE. */
10355 :
10356 : static tree
10357 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10358 : {
10359 2 : tree itype = TREE_TYPE (type);
10360 2 : tree rpart, ipart, tem;
10361 :
10362 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10363 : {
10364 0 : rpart = TREE_OPERAND (expr, 0);
10365 0 : ipart = TREE_OPERAND (expr, 1);
10366 : }
10367 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10368 : {
10369 0 : rpart = TREE_REALPART (expr);
10370 0 : ipart = TREE_IMAGPART (expr);
10371 : }
10372 : else
10373 : {
10374 2 : expr = save_expr (expr);
10375 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10376 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10377 : }
10378 :
10379 2 : rpart = save_expr (rpart);
10380 2 : ipart = save_expr (ipart);
10381 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10382 : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10383 : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10384 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10385 2 : build_zero_cst (itype));
10386 : }
10387 :
10388 :
10389 : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10390 : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10391 : true if successful. */
10392 :
10393 : static bool
10394 31533 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10395 : {
10396 31533 : unsigned HOST_WIDE_INT i, nunits;
10397 :
10398 31533 : if (TREE_CODE (arg) == VECTOR_CST
10399 31533 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10400 : {
10401 2160 : for (i = 0; i < nunits; ++i)
10402 1702 : elts[i] = VECTOR_CST_ELT (arg, i);
10403 : }
10404 31075 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10405 : {
10406 : constructor_elt *elt;
10407 :
10408 56580 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10409 46060 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10410 31533 : return false;
10411 : else
10412 20136 : elts[i] = elt->value;
10413 : }
10414 : else
10415 : return false;
10416 6383 : for (; i < nelts; i++)
10417 1548 : elts[i]
10418 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10419 : return true;
10420 : }
10421 :
10422 : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10423 : mask for VLA vec_perm folding.
10424 : REASON if specified, will contain the reason why SEL is not suitable.
10425 : Used only for debugging and unit-testing. */
10426 :
10427 : static bool
10428 30537 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10429 : const vec_perm_indices &sel,
10430 : const char **reason = NULL)
10431 : {
10432 30537 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10433 30537 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10434 :
10435 61074 : if (!(pow2p_hwi (sel_npatterns)
10436 30537 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10437 30537 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10438 : {
10439 0 : if (reason)
10440 0 : *reason = "npatterns is not power of 2";
10441 : return false;
10442 : }
10443 :
10444 : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10445 : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10446 30537 : poly_uint64 esel;
10447 30537 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10448 : {
10449 0 : if (reason)
10450 0 : *reason = "sel.length is not multiple of sel_npatterns";
10451 : return false;
10452 : }
10453 :
10454 30537 : if (sel_nelts_per_pattern < 3)
10455 : return true;
10456 :
10457 6056 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10458 : {
10459 4560 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10460 4560 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10461 4560 : HOST_WIDE_INT step;
10462 4560 : if (!poly_int64 (a2 - a1).is_constant (&step))
10463 : {
10464 : if (reason)
10465 : *reason = "step is not constant";
10466 30537 : return false;
10467 : }
10468 : // FIXME: Punt on step < 0 for now, revisit later.
10469 4560 : if (step < 0)
10470 : return false;
10471 4504 : if (step == 0)
10472 0 : continue;
10473 :
10474 4504 : if (!pow2p_hwi (step))
10475 : {
10476 0 : if (reason)
10477 0 : *reason = "step is not power of 2";
10478 : return false;
10479 : }
10480 :
10481 : /* Ensure that stepped sequence of the pattern selects elements
10482 : only from the same input vector. */
10483 4504 : uint64_t q1, qe;
10484 4504 : poly_uint64 r1, re;
10485 4504 : poly_uint64 ae = a1 + (esel - 2) * step;
10486 4504 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10487 :
10488 4504 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10489 4504 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10490 : && q1 == qe))
10491 : {
10492 433 : if (reason)
10493 0 : *reason = "crossed input vectors";
10494 : return false;
10495 : }
10496 :
10497 : /* Ensure that the stepped sequence always selects from the same
10498 : input pattern. */
10499 4071 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10500 4071 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10501 :
10502 4071 : if (!multiple_p (step, arg_npatterns))
10503 : {
10504 628 : if (reason)
10505 0 : *reason = "step is not multiple of npatterns";
10506 : return false;
10507 : }
10508 :
10509 : /* If a1 chooses base element from arg, ensure that it's a natural
10510 : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10511 : to preserve arg's encoding. */
10512 :
10513 3443 : if (maybe_lt (r1, arg_npatterns))
10514 : {
10515 44 : unsigned HOST_WIDE_INT index;
10516 44 : if (!r1.is_constant (&index))
10517 30537 : return false;
10518 :
10519 44 : tree arg_elem0 = vector_cst_elt (arg, index);
10520 44 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10521 44 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10522 :
10523 44 : tree step1, step2;
10524 44 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10525 44 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10526 88 : || !operand_equal_p (step1, step2, 0))
10527 : {
10528 2 : if (reason)
10529 0 : *reason = "not a natural stepped sequence";
10530 : return false;
10531 : }
10532 : }
10533 : }
10534 :
10535 : return true;
10536 : }
10537 :
10538 : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10539 : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10540 : REASON has same purpose as described in
10541 : valid_mask_for_fold_vec_perm_cst_p. */
10542 :
10543 : static tree
10544 30537 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10545 : const char **reason = NULL)
10546 : {
10547 30537 : unsigned res_npatterns, res_nelts_per_pattern;
10548 30537 : unsigned HOST_WIDE_INT res_nelts;
10549 :
10550 : /* First try to implement the fold in a VLA-friendly way.
10551 :
10552 : (1) If the selector is simply a duplication of N elements, the
10553 : result is likewise a duplication of N elements.
10554 :
10555 : (2) If the selector is N elements followed by a duplication
10556 : of N elements, the result is too.
10557 :
10558 : (3) If the selector is N elements followed by an interleaving
10559 : of N linear series, the situation is more complex.
10560 :
10561 : valid_mask_for_fold_vec_perm_cst_p detects whether we
10562 : can handle this case. If we can, then each of the N linear
10563 : series either (a) selects the same element each time or
10564 : (b) selects a linear series from one of the input patterns.
10565 :
10566 : If (b) holds for one of the linear series, the result
10567 : will contain a linear series, and so the result will have
10568 : the same shape as the selector. If (a) holds for all of
10569 : the linear series, the result will be the same as (2) above.
10570 :
10571 : (b) can only hold if one of the input patterns has a
10572 : stepped encoding. */
10573 :
10574 30537 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10575 : {
10576 29418 : res_npatterns = sel.encoding ().npatterns ();
10577 29418 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10578 29418 : if (res_nelts_per_pattern == 3
10579 1496 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10580 30377 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10581 : res_nelts_per_pattern = 2;
10582 29418 : res_nelts = res_npatterns * res_nelts_per_pattern;
10583 : }
10584 1119 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10585 : {
10586 1119 : res_npatterns = res_nelts;
10587 1119 : res_nelts_per_pattern = 1;
10588 : }
10589 : else
10590 : return NULL_TREE;
10591 :
10592 30537 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10593 184012 : for (unsigned i = 0; i < res_nelts; i++)
10594 : {
10595 122938 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10596 122938 : uint64_t q;
10597 122938 : poly_uint64 r;
10598 122938 : unsigned HOST_WIDE_INT index;
10599 :
10600 : /* Punt if sel[i] /trunc_div len cannot be determined,
10601 : because the input vector to be chosen will depend on
10602 : runtime vector length.
10603 : For example if len == 4 + 4x, and sel[i] == 4,
10604 : If len at runtime equals 4, we choose arg1[0].
10605 : For any other value of len > 4 at runtime, we choose arg0[4].
10606 : which makes the element choice dependent on runtime vector length. */
10607 122938 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10608 : {
10609 : if (reason)
10610 : *reason = "cannot divide selector element by arg len";
10611 : return NULL_TREE;
10612 : }
10613 :
10614 : /* sel[i] % len will give the index of element in the chosen input
10615 : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10616 : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10617 122938 : if (!r.is_constant (&index))
10618 : {
10619 : if (reason)
10620 : *reason = "remainder is not constant";
10621 : return NULL_TREE;
10622 : }
10623 :
10624 122938 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10625 122938 : tree elem = vector_cst_elt (arg, index);
10626 122938 : out_elts.quick_push (elem);
10627 : }
10628 :
10629 30537 : return out_elts.build ();
10630 30537 : }
10631 :
10632 : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10633 : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10634 : NULL_TREE otherwise. */
10635 :
10636 : tree
10637 69828 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10638 : {
10639 69828 : unsigned int i;
10640 69828 : unsigned HOST_WIDE_INT nelts;
10641 :
10642 69828 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10643 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10644 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10645 :
10646 69828 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10647 69828 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10648 : return NULL_TREE;
10649 :
10650 59249 : if (TREE_CODE (arg0) == VECTOR_CST
10651 30811 : && TREE_CODE (arg1) == VECTOR_CST)
10652 30537 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10653 :
10654 : /* For fall back case, we want to ensure we have VLS vectors
10655 : with equal length. */
10656 28712 : if (!sel.length ().is_constant (&nelts)
10657 28712 : || !known_eq (sel.length (), TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
10658 0 : return NULL_TREE;
10659 :
10660 28712 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10661 28712 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10662 28712 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10663 : return NULL_TREE;
10664 :
10665 2788 : vec<constructor_elt, va_gc> *v;
10666 2788 : vec_alloc (v, nelts);
10667 16750 : for (i = 0; i < nelts; i++)
10668 : {
10669 11174 : HOST_WIDE_INT index;
10670 11174 : if (!sel[i].is_constant (&index))
10671 : return NULL_TREE;
10672 11174 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10673 : }
10674 2788 : return build_constructor (type, v);
10675 : }
10676 :
10677 : /* Try to fold a pointer difference of type TYPE two address expressions of
10678 : array references AREF0 and AREF1 using location LOC. Return a
10679 : simplified expression for the difference or NULL_TREE. */
10680 :
10681 : static tree
10682 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10683 : tree aref0, tree aref1,
10684 : bool use_pointer_diff)
10685 : {
10686 39 : tree base0 = TREE_OPERAND (aref0, 0);
10687 39 : tree base1 = TREE_OPERAND (aref1, 0);
10688 39 : tree base_offset = build_int_cst (type, 0);
10689 :
10690 : /* If the bases are array references as well, recurse. If the bases
10691 : are pointer indirections compute the difference of the pointers.
10692 : If the bases are equal, we are set. */
10693 39 : if ((TREE_CODE (base0) == ARRAY_REF
10694 1 : && TREE_CODE (base1) == ARRAY_REF
10695 1 : && (base_offset
10696 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10697 : use_pointer_diff)))
10698 38 : || (INDIRECT_REF_P (base0)
10699 7 : && INDIRECT_REF_P (base1)
10700 7 : && (base_offset
10701 : = use_pointer_diff
10702 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10703 1 : TREE_OPERAND (base0, 0),
10704 1 : TREE_OPERAND (base1, 0))
10705 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10706 6 : fold_convert (type,
10707 : TREE_OPERAND (base0, 0)),
10708 6 : fold_convert (type,
10709 : TREE_OPERAND (base1, 0)))))
10710 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10711 : {
10712 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10713 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10714 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10715 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10716 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10717 : base_offset,
10718 : fold_build2_loc (loc, MULT_EXPR, type,
10719 15 : diff, esz));
10720 : }
10721 : return NULL_TREE;
10722 : }
10723 :
10724 : /* If the real or vector real constant CST of type TYPE has an exact
10725 : inverse, return it, else return NULL. */
10726 :
10727 : tree
10728 1164075 : exact_inverse (tree type, tree cst)
10729 : {
10730 1164075 : REAL_VALUE_TYPE r;
10731 1164075 : tree unit_type;
10732 1164075 : machine_mode mode;
10733 :
10734 1164075 : switch (TREE_CODE (cst))
10735 : {
10736 1163458 : case REAL_CST:
10737 1163458 : r = TREE_REAL_CST (cst);
10738 :
10739 1163458 : if (exact_real_inverse (TYPE_MODE (type), &r))
10740 333527 : return build_real (type, r);
10741 :
10742 : return NULL_TREE;
10743 :
10744 617 : case VECTOR_CST:
10745 617 : {
10746 617 : unit_type = TREE_TYPE (type);
10747 617 : mode = TYPE_MODE (unit_type);
10748 :
10749 617 : tree_vector_builder elts;
10750 617 : if (!elts.new_unary_operation (type, cst, false))
10751 : return NULL_TREE;
10752 617 : unsigned int count = elts.encoded_nelts ();
10753 677 : for (unsigned int i = 0; i < count; ++i)
10754 : {
10755 617 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10756 617 : if (!exact_real_inverse (mode, &r))
10757 : return NULL_TREE;
10758 60 : elts.quick_push (build_real (unit_type, r));
10759 : }
10760 :
10761 60 : return elts.build ();
10762 617 : }
10763 :
10764 : default:
10765 : return NULL_TREE;
10766 : }
10767 : }
10768 :
10769 : /* Mask out the tz least significant bits of X of type TYPE where
10770 : tz is the number of trailing zeroes in Y. */
10771 : static wide_int
10772 168547 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10773 : {
10774 168547 : int tz = wi::ctz (y);
10775 168547 : if (tz > 0)
10776 6710 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10777 161837 : return x;
10778 : }
10779 :
10780 : /* Return true when T is an address and is known to be nonzero.
10781 : For floating point we further ensure that T is not denormal.
10782 : Similar logic is present in nonzero_address in rtlanal.h. */
10783 :
10784 : bool
10785 151129045 : tree_expr_nonzero_p (tree t)
10786 : {
10787 151485554 : tree type = TREE_TYPE (t);
10788 151485554 : enum tree_code code;
10789 :
10790 : /* Doing something useful for floating point would need more work. */
10791 151485554 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10792 : return false;
10793 :
10794 151359581 : code = TREE_CODE (t);
10795 151359581 : switch (TREE_CODE_CLASS (code))
10796 : {
10797 984272 : case tcc_unary:
10798 984272 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10799 2921124 : case tcc_binary:
10800 2921124 : case tcc_comparison:
10801 2921124 : return tree_binary_nonzero_p (code, type,
10802 2921124 : TREE_OPERAND (t, 0),
10803 5842248 : TREE_OPERAND (t, 1));
10804 13321610 : case tcc_constant:
10805 13321610 : case tcc_declaration:
10806 13321610 : case tcc_reference:
10807 13321610 : return tree_single_nonzero_p (t);
10808 :
10809 134132575 : default:
10810 134132575 : break;
10811 : }
10812 :
10813 134132575 : switch (code)
10814 : {
10815 617292 : case TRUTH_NOT_EXPR:
10816 617292 : return tree_unary_nonzero_p (code, type, TREE_OPERAND (t, 0));
10817 :
10818 71300 : case TRUTH_AND_EXPR:
10819 71300 : case TRUTH_OR_EXPR:
10820 71300 : case TRUTH_XOR_EXPR:
10821 71300 : return tree_binary_nonzero_p (code, type,
10822 71300 : TREE_OPERAND (t, 0),
10823 142600 : TREE_OPERAND (t, 1));
10824 :
10825 129921081 : case COND_EXPR:
10826 129921081 : case CONSTRUCTOR:
10827 129921081 : case OBJ_TYPE_REF:
10828 129921081 : case ADDR_EXPR:
10829 129921081 : case WITH_SIZE_EXPR:
10830 129921081 : case SSA_NAME:
10831 129921081 : return tree_single_nonzero_p (t);
10832 :
10833 85041 : case COMPOUND_EXPR:
10834 85041 : case MODIFY_EXPR:
10835 85041 : case BIND_EXPR:
10836 85041 : return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10837 :
10838 271468 : case SAVE_EXPR:
10839 271468 : return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10840 :
10841 3110033 : case CALL_EXPR:
10842 3110033 : {
10843 3110033 : tree fndecl = get_callee_fndecl (t);
10844 3110033 : if (!fndecl) return false;
10845 3108054 : if (flag_delete_null_pointer_checks && !flag_check_new
10846 3108054 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10847 3108772 : && !TREE_NOTHROW (fndecl))
10848 : return true;
10849 3108772 : if (flag_delete_null_pointer_checks
10850 6216826 : && lookup_attribute ("returns_nonnull",
10851 3108054 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10852 : return true;
10853 3108764 : return alloca_call_p (t);
10854 : }
10855 :
10856 : default:
10857 : break;
10858 : }
10859 : return false;
10860 : }
10861 :
10862 : /* Return true if T is known not to be equal to an integer W.
10863 : If STMT is specified, the check is if T on STMT is not equal
10864 : to W. */
10865 :
10866 : bool
10867 103912988 : expr_not_equal_to (tree t, const wide_int &w, gimple *stmt /* = NULL */)
10868 : {
10869 103912988 : int_range_max vr;
10870 103912988 : switch (TREE_CODE (t))
10871 : {
10872 2447974 : case INTEGER_CST:
10873 2447974 : return wi::to_wide (t) != w;
10874 :
10875 101022869 : case SSA_NAME:
10876 101022869 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10877 : return false;
10878 :
10879 202045738 : get_range_query (cfun)->range_of_expr (vr, t, stmt);
10880 101022869 : if (!vr.undefined_p () && !vr.contains_p (w))
10881 : return true;
10882 : /* If T has some known zero bits and W has any of those bits set,
10883 : then T is known not to be equal to W. */
10884 100885115 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10885 201769798 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10886 1 : return true;
10887 : return false;
10888 :
10889 : default:
10890 : return false;
10891 : }
10892 103912988 : }
10893 :
10894 : /* Fold a binary expression of code CODE and type TYPE with operands
10895 : OP0 and OP1. LOC is the location of the resulting expression.
10896 : Return the folded expression if folding is successful. Otherwise,
10897 : return NULL_TREE. */
10898 :
10899 : tree
10900 974329958 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10901 : tree op0, tree op1)
10902 : {
10903 974329958 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10904 974329958 : tree arg0, arg1, tem;
10905 974329958 : tree t1 = NULL_TREE;
10906 974329958 : unsigned int prec;
10907 :
10908 974329958 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10909 : && TREE_CODE_LENGTH (code) == 2
10910 : && op0 != NULL_TREE
10911 : && op1 != NULL_TREE);
10912 :
10913 974329958 : arg0 = op0;
10914 974329958 : arg1 = op1;
10915 :
10916 : /* Strip any conversions that don't change the mode. This is
10917 : safe for every expression, except for a comparison expression
10918 : because its signedness is derived from its operands. So, in
10919 : the latter case, only strip conversions that don't change the
10920 : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10921 : preserved.
10922 :
10923 : Note that this is done as an internal manipulation within the
10924 : constant folder, in order to find the simplest representation
10925 : of the arguments so that their form can be studied. In any
10926 : cases, the appropriate type conversions should be put back in
10927 : the tree that will get out of the constant folder. */
10928 :
10929 974329958 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10930 : {
10931 214637022 : STRIP_SIGN_NOPS (arg0);
10932 214637022 : STRIP_SIGN_NOPS (arg1);
10933 : }
10934 : else
10935 : {
10936 759692936 : STRIP_NOPS (arg0);
10937 759692936 : STRIP_NOPS (arg1);
10938 : }
10939 :
10940 : /* Note that TREE_CONSTANT isn't enough: static var addresses are
10941 : constant but we can't do arithmetic on them. */
10942 974329958 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10943 : {
10944 273811328 : tem = const_binop (code, type, arg0, arg1);
10945 273811328 : if (tem != NULL_TREE)
10946 : {
10947 271269559 : if (TREE_TYPE (tem) != type)
10948 4387510 : tem = fold_convert_loc (loc, type, tem);
10949 : return tem;
10950 : }
10951 : }
10952 :
10953 : /* If this is a commutative operation, and ARG0 is a constant, move it
10954 : to ARG1 to reduce the number of tests below. */
10955 703060399 : if (commutative_tree_code (code)
10956 703060399 : && tree_swap_operands_p (arg0, arg1))
10957 33352569 : return fold_build2_loc (loc, code, type, op1, op0);
10958 :
10959 : /* Likewise if this is a comparison, and ARG0 is a constant, move it
10960 : to ARG1 to reduce the number of tests below. */
10961 669707830 : if (kind == tcc_comparison
10962 669707830 : && tree_swap_operands_p (arg0, arg1))
10963 8422803 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10964 :
10965 661285027 : tem = generic_simplify (loc, code, type, op0, op1);
10966 661285027 : if (tem)
10967 : return tem;
10968 :
10969 : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10970 :
10971 : First check for cases where an arithmetic operation is applied to a
10972 : compound, conditional, or comparison operation. Push the arithmetic
10973 : operation inside the compound or conditional to see if any folding
10974 : can then be done. Convert comparison to conditional for this purpose.
10975 : The also optimizes non-constant cases that used to be done in
10976 : expand_expr.
10977 :
10978 : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10979 : one of the operands is a comparison and the other is a comparison, a
10980 : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10981 : code below would make the expression more complex. Change it to a
10982 : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10983 : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10984 :
10985 559260744 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10986 : || code == EQ_EXPR || code == NE_EXPR)
10987 59207382 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10988 58619137 : && ((truth_value_p (TREE_CODE (arg0))
10989 1251076 : && (truth_value_p (TREE_CODE (arg1))
10990 930023 : || (TREE_CODE (arg1) == BIT_AND_EXPR
10991 46 : && integer_onep (TREE_OPERAND (arg1, 1)))))
10992 58298068 : || (truth_value_p (TREE_CODE (arg1))
10993 6966 : && (truth_value_p (TREE_CODE (arg0))
10994 6966 : || (TREE_CODE (arg0) == BIT_AND_EXPR
10995 209 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
10996 : {
10997 401122 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10998 80039 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10999 : : TRUTH_XOR_EXPR,
11000 : boolean_type_node,
11001 : fold_convert_loc (loc, boolean_type_node, arg0),
11002 : fold_convert_loc (loc, boolean_type_node, arg1));
11003 :
11004 321083 : if (code == EQ_EXPR)
11005 72956 : tem = invert_truthvalue_loc (loc, tem);
11006 :
11007 321083 : return fold_convert_loc (loc, type, tem);
11008 : }
11009 :
11010 558939661 : if (TREE_CODE_CLASS (code) == tcc_binary
11011 306471180 : || TREE_CODE_CLASS (code) == tcc_comparison)
11012 : {
11013 350300602 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11014 : {
11015 82869 : tem = fold_build2_loc (loc, code, type,
11016 82869 : fold_convert_loc (loc, TREE_TYPE (op0),
11017 82869 : TREE_OPERAND (arg0, 1)), op1);
11018 82869 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11019 82869 : tem);
11020 : }
11021 350217733 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11022 : {
11023 3161 : tem = fold_build2_loc (loc, code, type, op0,
11024 3161 : fold_convert_loc (loc, TREE_TYPE (op1),
11025 3161 : TREE_OPERAND (arg1, 1)));
11026 3161 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11027 3161 : tem);
11028 : }
11029 :
11030 350214572 : if (TREE_CODE (arg0) == COND_EXPR
11031 349819459 : || TREE_CODE (arg0) == VEC_COND_EXPR
11032 349816129 : || COMPARISON_CLASS_P (arg0))
11033 : {
11034 740118 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11035 : arg0, arg1,
11036 : /*cond_first_p=*/1);
11037 740118 : if (tem != NULL_TREE)
11038 : return tem;
11039 : }
11040 :
11041 349724832 : if (TREE_CODE (arg1) == COND_EXPR
11042 349507464 : || TREE_CODE (arg1) == VEC_COND_EXPR
11043 349506994 : || COMPARISON_CLASS_P (arg1))
11044 : {
11045 230760 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11046 : arg1, arg0,
11047 : /*cond_first_p=*/0);
11048 230760 : if (tem != NULL_TREE)
11049 : return tem;
11050 : }
11051 : }
11052 :
11053 558355669 : switch (code)
11054 : {
11055 64992581 : case MEM_REF:
11056 : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11057 64992581 : if (TREE_CODE (arg0) == ADDR_EXPR
11058 64992581 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11059 : {
11060 839380 : tree iref = TREE_OPERAND (arg0, 0);
11061 839380 : return fold_build2 (MEM_REF, type,
11062 : TREE_OPERAND (iref, 0),
11063 : int_const_binop (PLUS_EXPR, arg1,
11064 : TREE_OPERAND (iref, 1)));
11065 : }
11066 :
11067 : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11068 64153201 : if (TREE_CODE (arg0) == ADDR_EXPR
11069 64153201 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11070 : {
11071 6241594 : tree base;
11072 6241594 : poly_int64 coffset;
11073 6241594 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11074 : &coffset);
11075 6241594 : if (!base)
11076 : return NULL_TREE;
11077 6237560 : return fold_build2 (MEM_REF, type,
11078 : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11079 : int_const_binop (PLUS_EXPR, arg1,
11080 : size_int (coffset)));
11081 : }
11082 :
11083 : return NULL_TREE;
11084 :
11085 74782196 : case POINTER_PLUS_EXPR:
11086 : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11087 149563976 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11088 149555133 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11089 33153 : return fold_convert_loc (loc, type,
11090 : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11091 : fold_convert_loc (loc, sizetype,
11092 : arg1),
11093 : fold_convert_loc (loc, sizetype,
11094 33153 : arg0)));
11095 :
11096 : return NULL_TREE;
11097 :
11098 63461897 : case PLUS_EXPR:
11099 63461897 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11100 : {
11101 : /* X + (X / CST) * -CST is X % CST. */
11102 51885045 : if (TREE_CODE (arg1) == MULT_EXPR
11103 2355027 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11104 51891320 : && operand_equal_p (arg0,
11105 6275 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11106 : {
11107 204 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11108 204 : tree cst1 = TREE_OPERAND (arg1, 1);
11109 204 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11110 : cst1, cst0);
11111 204 : if (sum && integer_zerop (sum))
11112 204 : return fold_convert_loc (loc, type,
11113 : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11114 204 : TREE_TYPE (arg0), arg0,
11115 204 : cst0));
11116 : }
11117 : }
11118 :
11119 : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11120 : one. Make sure the type is not saturating and has the signedness of
11121 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11122 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11123 63461693 : if ((TREE_CODE (arg0) == MULT_EXPR
11124 51569456 : || TREE_CODE (arg1) == MULT_EXPR)
11125 13230863 : && !TYPE_SATURATING (type)
11126 13230863 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11127 12840740 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11128 75613254 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11129 : {
11130 8849331 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11131 8849331 : if (tem)
11132 : return tem;
11133 : }
11134 :
11135 62172357 : if (! FLOAT_TYPE_P (type))
11136 : {
11137 : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11138 : (plus (plus (mult) (mult)) (foo)) so that we can
11139 : take advantage of the factoring cases below. */
11140 283521 : if (ANY_INTEGRAL_TYPE_P (type)
11141 50597869 : && TYPE_OVERFLOW_WRAPS (type)
11142 50597869 : && (((TREE_CODE (arg0) == PLUS_EXPR
11143 31831878 : || TREE_CODE (arg0) == MINUS_EXPR)
11144 3447471 : && TREE_CODE (arg1) == MULT_EXPR)
11145 31326462 : || ((TREE_CODE (arg1) == PLUS_EXPR
11146 31326462 : || TREE_CODE (arg1) == MINUS_EXPR)
11147 428238 : && TREE_CODE (arg0) == MULT_EXPR)))
11148 : {
11149 551667 : tree parg0, parg1, parg, marg;
11150 551667 : enum tree_code pcode;
11151 :
11152 551667 : if (TREE_CODE (arg1) == MULT_EXPR)
11153 : parg = arg0, marg = arg1;
11154 : else
11155 46251 : parg = arg1, marg = arg0;
11156 551667 : pcode = TREE_CODE (parg);
11157 551667 : parg0 = TREE_OPERAND (parg, 0);
11158 551667 : parg1 = TREE_OPERAND (parg, 1);
11159 551667 : STRIP_NOPS (parg0);
11160 551667 : STRIP_NOPS (parg1);
11161 :
11162 551667 : if (TREE_CODE (parg0) == MULT_EXPR
11163 271688 : && TREE_CODE (parg1) != MULT_EXPR)
11164 232558 : return fold_build2_loc (loc, pcode, type,
11165 : fold_build2_loc (loc, PLUS_EXPR, type,
11166 : fold_convert_loc (loc, type,
11167 : parg0),
11168 : fold_convert_loc (loc, type,
11169 : marg)),
11170 232558 : fold_convert_loc (loc, type, parg1));
11171 319109 : if (TREE_CODE (parg0) != MULT_EXPR
11172 279979 : && TREE_CODE (parg1) == MULT_EXPR)
11173 99442 : return
11174 99442 : fold_build2_loc (loc, PLUS_EXPR, type,
11175 : fold_convert_loc (loc, type, parg0),
11176 : fold_build2_loc (loc, pcode, type,
11177 : fold_convert_loc (loc, type, marg),
11178 : fold_convert_loc (loc, type,
11179 99442 : parg1)));
11180 : }
11181 : }
11182 : else
11183 : {
11184 : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11185 : to __complex__ ( x, y ). This is not the same for SNaNs or
11186 : if signed zeros are involved. */
11187 11574488 : if (!HONOR_SNANS (arg0)
11188 11572828 : && !HONOR_SIGNED_ZEROS (arg0)
11189 11596049 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11190 : {
11191 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11192 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11193 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11194 3086 : bool arg0rz = false, arg0iz = false;
11195 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11196 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11197 : {
11198 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11199 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11200 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11201 : {
11202 22 : tree rp = arg1r ? arg1r
11203 0 : : build1 (REALPART_EXPR, rtype, arg1);
11204 22 : tree ip = arg0i ? arg0i
11205 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11206 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11207 : }
11208 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11209 : {
11210 53 : tree rp = arg0r ? arg0r
11211 0 : : build1 (REALPART_EXPR, rtype, arg0);
11212 53 : tree ip = arg1i ? arg1i
11213 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11214 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11215 : }
11216 : }
11217 : }
11218 :
11219 : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11220 : We associate floats only if the user has specified
11221 : -fassociative-math. */
11222 11574413 : if (flag_associative_math
11223 21429 : && TREE_CODE (arg1) == PLUS_EXPR
11224 38 : && TREE_CODE (arg0) != MULT_EXPR)
11225 : {
11226 23 : tree tree10 = TREE_OPERAND (arg1, 0);
11227 23 : tree tree11 = TREE_OPERAND (arg1, 1);
11228 23 : if (TREE_CODE (tree11) == MULT_EXPR
11229 5 : && TREE_CODE (tree10) == MULT_EXPR)
11230 : {
11231 1 : tree tree0;
11232 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11233 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11234 : }
11235 : }
11236 : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11237 : We associate floats only if the user has specified
11238 : -fassociative-math. */
11239 11574412 : if (flag_associative_math
11240 21428 : && TREE_CODE (arg0) == PLUS_EXPR
11241 1292 : && TREE_CODE (arg1) != MULT_EXPR)
11242 : {
11243 864 : tree tree00 = TREE_OPERAND (arg0, 0);
11244 864 : tree tree01 = TREE_OPERAND (arg0, 1);
11245 864 : if (TREE_CODE (tree01) == MULT_EXPR
11246 49 : && TREE_CODE (tree00) == MULT_EXPR)
11247 : {
11248 9 : tree tree0;
11249 9 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11250 9 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11251 : }
11252 : }
11253 : }
11254 :
11255 11573548 : bit_rotate:
11256 : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11257 : is a rotate of A by C1 bits. */
11258 : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11259 : is a rotate of A by B bits.
11260 : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11261 : though in this case CODE must be | and not + or ^, otherwise
11262 : it doesn't return A when B is 0. */
11263 64752927 : {
11264 64752927 : enum tree_code code0, code1;
11265 64752927 : tree rtype;
11266 64752927 : code0 = TREE_CODE (arg0);
11267 64752927 : code1 = TREE_CODE (arg1);
11268 74941 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11269 64736544 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11270 39771 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11271 39771 : TREE_OPERAND (arg1, 0), 0)
11272 37002 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11273 37002 : TYPE_UNSIGNED (rtype))
11274 : /* Only create rotates in complete modes. Other cases are not
11275 : expanded properly. */
11276 64779831 : && (element_precision (rtype)
11277 53808 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11278 : {
11279 26831 : tree tree01, tree11;
11280 26831 : tree orig_tree01, orig_tree11;
11281 26831 : enum tree_code code01, code11;
11282 :
11283 26831 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11284 26831 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11285 26831 : STRIP_NOPS (tree01);
11286 26831 : STRIP_NOPS (tree11);
11287 26831 : code01 = TREE_CODE (tree01);
11288 26831 : code11 = TREE_CODE (tree11);
11289 26831 : if (code11 != MINUS_EXPR
11290 26145 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11291 : {
11292 1462 : std::swap (code0, code1);
11293 1462 : std::swap (code01, code11);
11294 1462 : std::swap (tree01, tree11);
11295 1462 : std::swap (orig_tree01, orig_tree11);
11296 : }
11297 53662 : if (code01 == INTEGER_CST
11298 3152 : && code11 == INTEGER_CST
11299 29982 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11300 29982 : == element_precision (rtype)))
11301 : {
11302 6022 : tem = build2_loc (loc, LROTATE_EXPR,
11303 3011 : rtype, TREE_OPERAND (arg0, 0),
11304 : code0 == LSHIFT_EXPR
11305 : ? orig_tree01 : orig_tree11);
11306 3011 : return fold_convert_loc (loc, type, tem);
11307 : }
11308 23820 : else if (code11 == MINUS_EXPR)
11309 : {
11310 941 : tree tree110, tree111;
11311 941 : tree110 = TREE_OPERAND (tree11, 0);
11312 941 : tree111 = TREE_OPERAND (tree11, 1);
11313 941 : STRIP_NOPS (tree110);
11314 941 : STRIP_NOPS (tree111);
11315 941 : if (TREE_CODE (tree110) == INTEGER_CST
11316 930 : && compare_tree_int (tree110,
11317 930 : element_precision (rtype)) == 0
11318 1855 : && operand_equal_p (tree01, tree111, 0))
11319 : {
11320 777 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11321 : ? LROTATE_EXPR : RROTATE_EXPR),
11322 558 : rtype, TREE_OPERAND (arg0, 0),
11323 : orig_tree01);
11324 558 : return fold_convert_loc (loc, type, tem);
11325 : }
11326 : }
11327 22879 : else if (code == BIT_IOR_EXPR
11328 21765 : && code11 == BIT_AND_EXPR
11329 44569 : && pow2p_hwi (element_precision (rtype)))
11330 : {
11331 21690 : tree tree110, tree111;
11332 21690 : tree110 = TREE_OPERAND (tree11, 0);
11333 21690 : tree111 = TREE_OPERAND (tree11, 1);
11334 21690 : STRIP_NOPS (tree110);
11335 21690 : STRIP_NOPS (tree111);
11336 21690 : if (TREE_CODE (tree110) == NEGATE_EXPR
11337 21235 : && TREE_CODE (tree111) == INTEGER_CST
11338 21235 : && compare_tree_int (tree111,
11339 21235 : element_precision (rtype) - 1) == 0
11340 42911 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11341 : {
11342 31703 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11343 : ? LROTATE_EXPR : RROTATE_EXPR),
11344 21159 : rtype, TREE_OPERAND (arg0, 0),
11345 : orig_tree01);
11346 21159 : return fold_convert_loc (loc, type, tem);
11347 : }
11348 : }
11349 : }
11350 : }
11351 :
11352 158071233 : associate:
11353 : /* In most languages, can't associate operations on floats through
11354 : parentheses. Rather than remember where the parentheses were, we
11355 : don't associate floats at all, unless the user has specified
11356 : -fassociative-math.
11357 : And, we need to make sure type is not saturating. */
11358 :
11359 158071233 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11360 117015619 : && !TYPE_SATURATING (type)
11361 275086852 : && !TYPE_OVERFLOW_SANITIZED (type))
11362 : {
11363 116983319 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11364 116983319 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11365 116983319 : tree atype = type;
11366 116983319 : bool ok = true;
11367 :
11368 : /* Split both trees into variables, constants, and literals. Then
11369 : associate each group together, the constants with literals,
11370 : then the result with variables. This increases the chances of
11371 : literals being recombined later and of generating relocatable
11372 : expressions for the sum of a constant and literal. */
11373 116983319 : var0 = split_tree (arg0, type, code,
11374 : &minus_var0, &con0, &minus_con0,
11375 : &lit0, &minus_lit0, 0);
11376 116983319 : var1 = split_tree (arg1, type, code,
11377 : &minus_var1, &con1, &minus_con1,
11378 : &lit1, &minus_lit1, code == MINUS_EXPR);
11379 :
11380 : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11381 116983319 : if (code == MINUS_EXPR)
11382 12941435 : code = PLUS_EXPR;
11383 :
11384 : /* With undefined overflow prefer doing association in a type
11385 : which wraps on overflow, if that is one of the operand types. */
11386 116983088 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11387 232717688 : && !TYPE_OVERFLOW_WRAPS (type))
11388 : {
11389 61974860 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11390 61324722 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11391 854379 : atype = TREE_TYPE (arg0);
11392 60219855 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11393 59985172 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11394 246403 : atype = TREE_TYPE (arg1);
11395 31245685 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11396 : }
11397 :
11398 : /* With undefined overflow we can only associate constants with one
11399 : variable, and constants whose association doesn't overflow. */
11400 116983088 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11401 232717688 : && !TYPE_OVERFLOW_WRAPS (atype))
11402 : {
11403 30144903 : if ((var0 && var1) || (minus_var0 && minus_var1))
11404 : {
11405 : /* ??? If split_tree would handle NEGATE_EXPR we could
11406 : simply reject these cases and the allowed cases would
11407 : be the var0/minus_var1 ones. */
11408 1237 : tree tmp0 = var0 ? var0 : minus_var0;
11409 5618108 : tree tmp1 = var1 ? var1 : minus_var1;
11410 5618108 : bool one_neg = false;
11411 :
11412 5618108 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11413 : {
11414 742 : tmp0 = TREE_OPERAND (tmp0, 0);
11415 742 : one_neg = !one_neg;
11416 : }
11417 4994076 : if (CONVERT_EXPR_P (tmp0)
11418 649987 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11419 6267144 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11420 649036 : <= TYPE_PRECISION (atype)))
11421 636563 : tmp0 = TREE_OPERAND (tmp0, 0);
11422 5618108 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11423 : {
11424 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11425 170 : one_neg = !one_neg;
11426 : }
11427 5291719 : if (CONVERT_EXPR_P (tmp1)
11428 395921 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11429 6013969 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11430 395861 : <= TYPE_PRECISION (atype)))
11431 378782 : tmp1 = TREE_OPERAND (tmp1, 0);
11432 : /* The only case we can still associate with two variables
11433 : is if they cancel out. */
11434 5618108 : if (!one_neg
11435 5618108 : || !operand_equal_p (tmp0, tmp1, 0))
11436 : ok = false;
11437 : }
11438 24145173 : else if ((var0 && minus_var1
11439 4091448 : && ! operand_equal_p (var0, minus_var1, 0))
11440 44580521 : || (minus_var0 && var1
11441 11599 : && ! operand_equal_p (minus_var0, var1, 0)))
11442 : ok = false;
11443 : }
11444 :
11445 : /* Only do something if we found more than two objects. Otherwise,
11446 : nothing has changed and we risk infinite recursion. */
11447 : if (ok
11448 107262241 : && ((var0 != 0) + (var1 != 0)
11449 107262241 : + (minus_var0 != 0) + (minus_var1 != 0)
11450 107262241 : + (con0 != 0) + (con1 != 0)
11451 107262241 : + (minus_con0 != 0) + (minus_con1 != 0)
11452 107262241 : + (lit0 != 0) + (lit1 != 0)
11453 107262241 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11454 : {
11455 2074283 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11456 4148566 : int minus_var0_origin
11457 2074283 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11458 2074283 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11459 4148566 : int minus_con0_origin
11460 2074283 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11461 2074283 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11462 4148566 : int minus_lit0_origin
11463 2074283 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11464 2074283 : var0 = associate_trees (loc, var0, var1, code, atype);
11465 2074283 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11466 : code, atype);
11467 2074283 : con0 = associate_trees (loc, con0, con1, code, atype);
11468 2074283 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11469 : code, atype);
11470 2074283 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11471 2074283 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11472 : code, atype);
11473 :
11474 2074283 : if (minus_var0 && var0)
11475 : {
11476 1353755 : var0_origin |= minus_var0_origin;
11477 1353755 : var0 = associate_trees (loc, var0, minus_var0,
11478 : MINUS_EXPR, atype);
11479 1353755 : minus_var0 = 0;
11480 1353755 : minus_var0_origin = 0;
11481 : }
11482 2074283 : if (minus_con0 && con0)
11483 : {
11484 2705 : con0_origin |= minus_con0_origin;
11485 2705 : con0 = associate_trees (loc, con0, minus_con0,
11486 : MINUS_EXPR, atype);
11487 2705 : minus_con0 = 0;
11488 2705 : minus_con0_origin = 0;
11489 : }
11490 :
11491 : /* Preserve the MINUS_EXPR if the negative part of the literal is
11492 : greater than the positive part. Otherwise, the multiplicative
11493 : folding code (i.e extract_muldiv) may be fooled in case
11494 : unsigned constants are subtracted, like in the following
11495 : example: ((X*2 + 4) - 8U)/2. */
11496 2074283 : if (minus_lit0 && lit0)
11497 : {
11498 232970 : if (TREE_CODE (lit0) == INTEGER_CST
11499 232970 : && TREE_CODE (minus_lit0) == INTEGER_CST
11500 232970 : && tree_int_cst_lt (lit0, minus_lit0)
11501 : /* But avoid ending up with only negated parts. */
11502 291035 : && (var0 || con0))
11503 : {
11504 53451 : minus_lit0_origin |= lit0_origin;
11505 53451 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11506 : MINUS_EXPR, atype);
11507 53451 : lit0 = 0;
11508 53451 : lit0_origin = 0;
11509 : }
11510 : else
11511 : {
11512 179519 : lit0_origin |= minus_lit0_origin;
11513 179519 : lit0 = associate_trees (loc, lit0, minus_lit0,
11514 : MINUS_EXPR, atype);
11515 179519 : minus_lit0 = 0;
11516 179519 : minus_lit0_origin = 0;
11517 : }
11518 : }
11519 :
11520 : /* Don't introduce overflows through reassociation. */
11521 1376654 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11522 3450899 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11523 2074283 : return NULL_TREE;
11524 :
11525 : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11526 2074245 : con0_origin |= lit0_origin;
11527 2074245 : con0 = associate_trees (loc, con0, lit0, code, atype);
11528 2074245 : minus_con0_origin |= minus_lit0_origin;
11529 2074245 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11530 : code, atype);
11531 :
11532 : /* Eliminate minus_con0. */
11533 2074245 : if (minus_con0)
11534 : {
11535 702655 : if (con0)
11536 : {
11537 15456 : con0_origin |= minus_con0_origin;
11538 15456 : con0 = associate_trees (loc, con0, minus_con0,
11539 : MINUS_EXPR, atype);
11540 : }
11541 687199 : else if (var0)
11542 : {
11543 687199 : var0_origin |= minus_con0_origin;
11544 687199 : var0 = associate_trees (loc, var0, minus_con0,
11545 : MINUS_EXPR, atype);
11546 : }
11547 : else
11548 0 : gcc_unreachable ();
11549 : }
11550 :
11551 : /* Eliminate minus_var0. */
11552 2074245 : if (minus_var0)
11553 : {
11554 368055 : if (con0)
11555 : {
11556 368055 : con0_origin |= minus_var0_origin;
11557 368055 : con0 = associate_trees (loc, con0, minus_var0,
11558 : MINUS_EXPR, atype);
11559 : }
11560 : else
11561 0 : gcc_unreachable ();
11562 : }
11563 :
11564 : /* Reassociate only if there has been any actual association
11565 : between subtrees from op0 and subtrees from op1 in at
11566 : least one of the operands, otherwise we risk infinite
11567 : recursion. See PR114084. */
11568 2074245 : if (var0_origin != 3 && con0_origin != 3)
11569 : return NULL_TREE;
11570 :
11571 2072688 : return
11572 2072688 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11573 2072688 : code, atype));
11574 : }
11575 : }
11576 :
11577 : return NULL_TREE;
11578 :
11579 23815279 : case POINTER_DIFF_EXPR:
11580 23815279 : case MINUS_EXPR:
11581 : /* Fold &a[i] - &a[j] to i-j. */
11582 23815279 : if (TREE_CODE (arg0) == ADDR_EXPR
11583 42905 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11584 6036 : && TREE_CODE (arg1) == ADDR_EXPR
11585 23815887 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11586 : {
11587 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11588 38 : TREE_OPERAND (arg0, 0),
11589 38 : TREE_OPERAND (arg1, 0),
11590 : code
11591 : == POINTER_DIFF_EXPR);
11592 38 : if (tem)
11593 : return tem;
11594 : }
11595 :
11596 : /* Further transformations are not for pointers. */
11597 23815265 : if (code == POINTER_DIFF_EXPR)
11598 : return NULL_TREE;
11599 :
11600 : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11601 21024176 : if (TREE_CODE (arg0) == NEGATE_EXPR
11602 147372 : && negate_expr_p (op1)
11603 : /* If arg0 is e.g. unsigned int and type is int, then this could
11604 : introduce UB, because if A is INT_MIN at runtime, the original
11605 : expression can be well defined while the latter is not.
11606 : See PR83269. */
11607 21025023 : && !(ANY_INTEGRAL_TYPE_P (type)
11608 847 : && TYPE_OVERFLOW_UNDEFINED (type)
11609 835 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11610 835 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11611 840 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11612 : fold_convert_loc (loc, type,
11613 1680 : TREE_OPERAND (arg0, 0)));
11614 :
11615 : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11616 : __complex__ ( x, -y ). This is not the same for SNaNs or if
11617 : signed zeros are involved. */
11618 21023336 : if (!HONOR_SNANS (arg0)
11619 21022185 : && !HONOR_SIGNED_ZEROS (arg0)
11620 34799020 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11621 : {
11622 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11623 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11624 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11625 53 : bool arg0rz = false, arg0iz = false;
11626 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11627 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11628 : {
11629 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11630 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11631 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11632 : {
11633 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11634 : arg1r ? arg1r
11635 0 : : build1 (REALPART_EXPR, rtype, arg1));
11636 9 : tree ip = arg0i ? arg0i
11637 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11638 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11639 : }
11640 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11641 : {
11642 15 : tree rp = arg0r ? arg0r
11643 0 : : build1 (REALPART_EXPR, rtype, arg0);
11644 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11645 : arg1i ? arg1i
11646 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11647 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11648 : }
11649 : }
11650 : }
11651 :
11652 : /* A - B -> A + (-B) if B is easily negatable. */
11653 21023312 : if (negate_expr_p (op1)
11654 777653 : && ! TYPE_OVERFLOW_SANITIZED (type)
11655 21798438 : && ((FLOAT_TYPE_P (type)
11656 : /* Avoid this transformation if B is a positive REAL_CST. */
11657 65 : && (TREE_CODE (op1) != REAL_CST
11658 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11659 775061 : || INTEGRAL_TYPE_P (type)))
11660 774921 : return fold_build2_loc (loc, PLUS_EXPR, type,
11661 : fold_convert_loc (loc, type, arg0),
11662 774921 : negate_expr (op1));
11663 :
11664 : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11665 : one. Make sure the type is not saturating and has the signedness of
11666 : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11667 : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11668 20248391 : if ((TREE_CODE (arg0) == MULT_EXPR
11669 18952417 : || TREE_CODE (arg1) == MULT_EXPR)
11670 2614803 : && !TYPE_SATURATING (type)
11671 2614803 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11672 2483050 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11673 22679336 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11674 : {
11675 338179 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11676 338179 : if (tem)
11677 : return tem;
11678 : }
11679 :
11680 20197799 : goto associate;
11681 :
11682 66716533 : case MULT_EXPR:
11683 66716533 : if (! FLOAT_TYPE_P (type))
11684 : {
11685 : /* Transform x * -C into -x * C if x is easily negatable. */
11686 44431235 : if (TREE_CODE (op1) == INTEGER_CST
11687 41335500 : && tree_int_cst_sgn (op1) == -1
11688 207181 : && negate_expr_p (op0)
11689 340 : && negate_expr_p (op1)
11690 324 : && (tem = negate_expr (op1)) != op1
11691 44431559 : && ! TREE_OVERFLOW (tem))
11692 324 : return fold_build2_loc (loc, MULT_EXPR, type,
11693 : fold_convert_loc (loc, type,
11694 324 : negate_expr (op0)), tem);
11695 :
11696 44430911 : if (TREE_CODE (arg1) == INTEGER_CST
11697 44430911 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11698 : {
11699 652015 : return fold_convert_loc (loc, type, tem);
11700 : }
11701 :
11702 : /* Optimize z * conj(z) for integer complex numbers. */
11703 43778896 : if (TREE_CODE (arg0) == CONJ_EXPR
11704 43778896 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11705 1 : return fold_mult_zconjz (loc, type, arg1);
11706 43778895 : if (TREE_CODE (arg1) == CONJ_EXPR
11707 43778895 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11708 0 : return fold_mult_zconjz (loc, type, arg0);
11709 : }
11710 : else
11711 : {
11712 : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11713 : This is not the same for NaNs or if signed zeros are
11714 : involved. */
11715 22285298 : if (!HONOR_NANS (arg0)
11716 33310 : && !HONOR_SIGNED_ZEROS (arg0)
11717 33009 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11718 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11719 22285523 : && real_zerop (TREE_REALPART (arg1)))
11720 : {
11721 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11722 218 : if (real_onep (TREE_IMAGPART (arg1)))
11723 : {
11724 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11725 63 : arg0 = save_expr (arg0);
11726 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11727 : rtype, arg0);
11728 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11729 : rtype, arg0);
11730 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11731 : negate_expr (iarg0),
11732 208 : rarg0);
11733 : }
11734 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11735 : {
11736 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11737 0 : arg0 = save_expr (arg0);
11738 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11739 : rtype, arg0);
11740 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11741 : rtype, arg0);
11742 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11743 : iarg0,
11744 10 : negate_expr (rarg0));
11745 : }
11746 : }
11747 :
11748 : /* Optimize z * conj(z) for floating point complex numbers.
11749 : Guarded by flag_unsafe_math_optimizations as non-finite
11750 : imaginary components don't produce scalar results. */
11751 22285080 : if (flag_unsafe_math_optimizations
11752 32832 : && TREE_CODE (arg0) == CONJ_EXPR
11753 22285082 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11754 1 : return fold_mult_zconjz (loc, type, arg1);
11755 22285079 : if (flag_unsafe_math_optimizations
11756 32831 : && TREE_CODE (arg1) == CONJ_EXPR
11757 22285083 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11758 0 : return fold_mult_zconjz (loc, type, arg0);
11759 : }
11760 66063974 : goto associate;
11761 :
11762 1935105 : case BIT_IOR_EXPR:
11763 : /* Canonicalize (X & C1) | C2. */
11764 1935105 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11765 89982 : && TREE_CODE (arg1) == INTEGER_CST
11766 1979435 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11767 : {
11768 44322 : int width = TYPE_PRECISION (type), w;
11769 44322 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11770 44322 : wide_int c2 = wi::to_wide (arg1);
11771 :
11772 : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11773 44322 : if ((c1 & c2) == c1)
11774 0 : return omit_one_operand_loc (loc, type, arg1,
11775 0 : TREE_OPERAND (arg0, 0));
11776 :
11777 44322 : wide_int msk = wi::mask (width, false,
11778 44322 : TYPE_PRECISION (TREE_TYPE (arg1)));
11779 :
11780 : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11781 44322 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11782 : {
11783 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11784 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11785 : }
11786 :
11787 : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11788 : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11789 : mode which allows further optimizations. */
11790 44316 : c1 &= msk;
11791 44316 : c2 &= msk;
11792 44316 : wide_int c3 = wi::bit_and_not (c1, c2);
11793 183526 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11794 : {
11795 95136 : wide_int mask = wi::mask (w, false,
11796 95136 : TYPE_PRECISION (type));
11797 190272 : if (((c1 | c2) & mask) == mask
11798 190272 : && wi::bit_and_not (c1, mask) == 0)
11799 : {
11800 242 : c3 = mask;
11801 242 : break;
11802 : }
11803 95136 : }
11804 :
11805 44316 : if (c3 != c1)
11806 : {
11807 558 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11808 1116 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11809 558 : wide_int_to_tree (type, c3));
11810 558 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11811 : }
11812 45444 : }
11813 :
11814 : /* See if this can be simplified into a rotate first. If that
11815 : is unsuccessful continue in the association code. */
11816 1934541 : goto bit_rotate;
11817 :
11818 978114 : case BIT_XOR_EXPR:
11819 : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11820 978114 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11821 3703 : && INTEGRAL_TYPE_P (type)
11822 3098 : && integer_onep (TREE_OPERAND (arg0, 1))
11823 979357 : && integer_onep (arg1))
11824 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11825 0 : build_zero_cst (TREE_TYPE (arg0)));
11826 :
11827 : /* See if this can be simplified into a rotate first. If that
11828 : is unsuccessful continue in the association code. */
11829 978114 : goto bit_rotate;
11830 :
11831 6650039 : case BIT_AND_EXPR:
11832 : /* Fold !X & 1 as X == 0. */
11833 6650039 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11834 6650039 : && integer_onep (arg1))
11835 : {
11836 0 : tem = TREE_OPERAND (arg0, 0);
11837 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11838 0 : build_zero_cst (TREE_TYPE (tem)));
11839 : }
11840 :
11841 : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11842 : multiple of 1 << CST. */
11843 6650039 : if (TREE_CODE (arg1) == INTEGER_CST)
11844 : {
11845 4795625 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11846 4795625 : wide_int ncst1 = -cst1;
11847 4795625 : if ((cst1 & ncst1) == ncst1
11848 4952117 : && multiple_of_p (type, arg0,
11849 4952117 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11850 467 : return fold_convert_loc (loc, type, arg0);
11851 4795625 : }
11852 :
11853 : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11854 : bits from CST2. */
11855 6649572 : if (TREE_CODE (arg1) == INTEGER_CST
11856 4795158 : && TREE_CODE (arg0) == MULT_EXPR
11857 6818181 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11858 : {
11859 168547 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11860 168547 : wide_int masked
11861 168547 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11862 :
11863 168547 : if (masked == 0)
11864 5374 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11865 5374 : arg0, arg1);
11866 163173 : else if (masked != warg1)
11867 : {
11868 : /* Avoid the transform if arg1 is a mask of some
11869 : mode which allows further optimizations. */
11870 638 : int pop = wi::popcount (warg1);
11871 660 : if (!(pop >= BITS_PER_UNIT
11872 50 : && pow2p_hwi (pop)
11873 682 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11874 1232 : return fold_build2_loc (loc, code, type, op0,
11875 1232 : wide_int_to_tree (type, masked));
11876 : }
11877 168547 : }
11878 :
11879 : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11880 4789168 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11881 6855801 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11882 : {
11883 114757 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11884 :
11885 114757 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11886 114757 : if (mask == -1)
11887 2402 : return
11888 2402 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11889 114757 : }
11890 :
11891 6641180 : goto associate;
11892 :
11893 6114708 : case RDIV_EXPR:
11894 : /* Don't touch a floating-point divide by zero unless the mode
11895 : of the constant can represent infinity. */
11896 6114708 : if (TREE_CODE (arg1) == REAL_CST
11897 3064457 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11898 6114708 : && real_zerop (arg1))
11899 0 : return NULL_TREE;
11900 :
11901 : /* (-A) / (-B) -> A / B */
11902 6114708 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11903 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11904 3 : TREE_OPERAND (arg0, 0),
11905 3 : negate_expr (arg1));
11906 6114705 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11907 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11908 : negate_expr (arg0),
11909 0 : TREE_OPERAND (arg1, 0));
11910 : return NULL_TREE;
11911 :
11912 2178737 : case TRUNC_DIV_EXPR:
11913 : /* Fall through */
11914 :
11915 2178737 : case FLOOR_DIV_EXPR:
11916 : /* Simplify A / (B << N) where A and B are positive and B is
11917 : a power of 2, to A >> (N + log2(B)). */
11918 2178737 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11919 2178737 : && (TYPE_UNSIGNED (type)
11920 9 : || tree_expr_nonnegative_p (op0)))
11921 : {
11922 17 : tree sval = TREE_OPERAND (arg1, 0);
11923 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11924 : {
11925 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
11926 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11927 16 : wi::exact_log2 (wi::to_wide (sval)));
11928 :
11929 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11930 : sh_cnt, pow2);
11931 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
11932 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
11933 : }
11934 : }
11935 :
11936 : /* Fall through */
11937 :
11938 3607548 : case ROUND_DIV_EXPR:
11939 3607548 : case CEIL_DIV_EXPR:
11940 3607548 : case EXACT_DIV_EXPR:
11941 3607548 : if (integer_zerop (arg1))
11942 : return NULL_TREE;
11943 :
11944 : /* Convert -A / -B to A / B when the type is signed and overflow is
11945 : undefined. */
11946 3604510 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11947 1020218 : && TREE_CODE (op0) == NEGATE_EXPR
11948 3604572 : && negate_expr_p (op1))
11949 60 : return fold_build2_loc (loc, code, type,
11950 : fold_convert_loc (loc, type,
11951 30 : TREE_OPERAND (arg0, 0)),
11952 30 : negate_expr (op1));
11953 3604480 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11954 1020188 : && TREE_CODE (arg1) == NEGATE_EXPR
11955 3604724 : && negate_expr_p (op0))
11956 36 : return fold_build2_loc (loc, code, type,
11957 : negate_expr (op0),
11958 : fold_convert_loc (loc, type,
11959 72 : TREE_OPERAND (arg1, 0)));
11960 :
11961 : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11962 : operation, EXACT_DIV_EXPR.
11963 :
11964 : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11965 : At one time others generated faster code, it's not clear if they do
11966 : after the last round to changes to the DIV code in expmed.cc. */
11967 3604444 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11968 3604444 : && multiple_of_p (type, arg0, arg1))
11969 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11970 : fold_convert (type, arg0),
11971 0 : fold_convert (type, arg1));
11972 :
11973 3604444 : if (TREE_CODE (arg1) == INTEGER_CST
11974 3604444 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11975 9555 : return fold_convert_loc (loc, type, tem);
11976 :
11977 : return NULL_TREE;
11978 :
11979 897131 : case CEIL_MOD_EXPR:
11980 897131 : case FLOOR_MOD_EXPR:
11981 897131 : case ROUND_MOD_EXPR:
11982 897131 : case TRUNC_MOD_EXPR:
11983 897131 : if (TREE_CODE (arg1) == INTEGER_CST
11984 897131 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE)) != 0)
11985 0 : return fold_convert_loc (loc, type, tem);
11986 :
11987 : return NULL_TREE;
11988 :
11989 2256552 : case LROTATE_EXPR:
11990 2256552 : case RROTATE_EXPR:
11991 2256552 : case RSHIFT_EXPR:
11992 2256552 : case LSHIFT_EXPR:
11993 : /* Since negative shift count is not well-defined,
11994 : don't try to compute it in the compiler. */
11995 2256552 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11996 : return NULL_TREE;
11997 :
11998 2255511 : prec = element_precision (type);
11999 :
12000 : /* If we have a rotate of a bit operation with the rotate count and
12001 : the second operand of the bit operation both constant,
12002 : permute the two operations. */
12003 2779 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12004 2214 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12005 2214 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12006 2214 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12007 2255511 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12008 : {
12009 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12010 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12011 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12012 : fold_build2_loc (loc, code, type,
12013 : arg00, arg1),
12014 : fold_build2_loc (loc, code, type,
12015 0 : arg01, arg1));
12016 : }
12017 :
12018 : return NULL_TREE;
12019 :
12020 440081 : case MIN_EXPR:
12021 440081 : case MAX_EXPR:
12022 440081 : goto associate;
12023 :
12024 6743709 : case TRUTH_ANDIF_EXPR:
12025 : /* Note that the operands of this must be ints
12026 : and their values must be 0 or 1.
12027 : ("true" is a fixed value perhaps depending on the language.) */
12028 : /* If first arg is constant zero, return it. */
12029 6743709 : if (integer_zerop (arg0))
12030 1708746 : return fold_convert_loc (loc, type, arg0);
12031 : /* FALLTHRU */
12032 15890885 : case TRUTH_AND_EXPR:
12033 : /* If either arg is constant true, drop it. */
12034 15890885 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12035 1511992 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12036 834963 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12037 : /* Preserve sequence points. */
12038 15168339 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12039 762273 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12040 : /* If second arg is constant zero, result is zero, but first arg
12041 : must be evaluated. */
12042 13616620 : if (integer_zerop (arg1))
12043 45517 : return omit_one_operand_loc (loc, type, arg1, arg0);
12044 : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12045 : case will be handled here. */
12046 13571103 : if (integer_zerop (arg0))
12047 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12048 :
12049 : /* !X && X is always false. */
12050 13571103 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12051 13571103 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12052 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12053 : /* X && !X is always false. */
12054 13571103 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12055 13571103 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12056 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12057 :
12058 : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12059 : means A >= Y && A != MAX, but in this case we know that
12060 : A < X <= MAX. */
12061 :
12062 13571103 : if (!TREE_SIDE_EFFECTS (arg0)
12063 13571103 : && !TREE_SIDE_EFFECTS (arg1))
12064 : {
12065 12083421 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12066 12083421 : if (tem && !operand_equal_p (tem, arg0, 0))
12067 433 : return fold_convert (type,
12068 : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12069 : tem, arg1));
12070 :
12071 12082988 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12072 12082988 : if (tem && !operand_equal_p (tem, arg1, 0))
12073 8468 : return fold_convert (type,
12074 : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12075 : arg0, tem));
12076 : }
12077 :
12078 13562202 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12079 : != NULL_TREE)
12080 : return tem;
12081 :
12082 : return NULL_TREE;
12083 :
12084 3534288 : case TRUTH_ORIF_EXPR:
12085 : /* Note that the operands of this must be ints
12086 : and their values must be 0 or true.
12087 : ("true" is a fixed value perhaps depending on the language.) */
12088 : /* If first arg is constant true, return it. */
12089 3534288 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12090 146466 : return fold_convert_loc (loc, type, arg0);
12091 : /* FALLTHRU */
12092 12865278 : case TRUTH_OR_EXPR:
12093 : /* If either arg is constant zero, drop it. */
12094 12865278 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12095 250023 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12096 453426 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12097 : /* Preserve sequence points. */
12098 13022464 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12099 395931 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12100 : /* If second arg is constant true, result is true, but we must
12101 : evaluate first arg. */
12102 12219324 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12103 46217 : return omit_one_operand_loc (loc, type, arg1, arg0);
12104 : /* Likewise for first arg, but note this only occurs here for
12105 : TRUTH_OR_EXPR. */
12106 12173107 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12107 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12108 :
12109 : /* !X || X is always true. */
12110 12173107 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12111 12173107 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12112 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12113 : /* X || !X is always true. */
12114 12173107 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12115 12173107 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12116 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12117 :
12118 : /* (X && !Y) || (!X && Y) is X ^ Y */
12119 12173106 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12120 1652 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12121 : {
12122 668 : tree a0, a1, l0, l1, n0, n1;
12123 :
12124 668 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12125 668 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12126 :
12127 668 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12128 668 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12129 :
12130 668 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12131 668 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12132 :
12133 668 : if ((operand_equal_p (n0, a0, 0)
12134 18 : && operand_equal_p (n1, a1, 0))
12135 676 : || (operand_equal_p (n0, a1, 0)
12136 3 : && operand_equal_p (n1, a0, 0)))
12137 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12138 : }
12139 :
12140 12173093 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12141 : != NULL_TREE)
12142 : return tem;
12143 :
12144 : return NULL_TREE;
12145 :
12146 78833 : case TRUTH_XOR_EXPR:
12147 : /* If the second arg is constant zero, drop it. */
12148 78833 : if (integer_zerop (arg1))
12149 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12150 : /* If the second arg is constant true, this is a logical inversion. */
12151 78833 : if (integer_onep (arg1))
12152 : {
12153 0 : tem = invert_truthvalue_loc (loc, arg0);
12154 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12155 : }
12156 : /* Identical arguments cancel to zero. */
12157 78833 : if (operand_equal_p (arg0, arg1, 0))
12158 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12159 :
12160 : /* !X ^ X is always true. */
12161 78833 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12162 78833 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12163 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12164 :
12165 : /* X ^ !X is always true. */
12166 78833 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12167 78833 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12168 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12169 :
12170 : return NULL_TREE;
12171 :
12172 49865254 : case EQ_EXPR:
12173 49865254 : case NE_EXPR:
12174 49865254 : STRIP_NOPS (arg0);
12175 49865254 : STRIP_NOPS (arg1);
12176 :
12177 49865254 : tem = fold_comparison (loc, code, type, op0, op1);
12178 49865254 : if (tem != NULL_TREE)
12179 : return tem;
12180 :
12181 : /* bool_var != 1 becomes !bool_var. */
12182 51025915 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12183 49902999 : && code == NE_EXPR)
12184 39640 : return fold_convert_loc (loc, type,
12185 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12186 79280 : TREE_TYPE (arg0), arg0));
12187 :
12188 : /* bool_var == 0 becomes !bool_var. */
12189 50946635 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12190 50761595 : && code == EQ_EXPR)
12191 196948 : return fold_convert_loc (loc, type,
12192 : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12193 393896 : TREE_TYPE (arg0), arg0));
12194 :
12195 : /* !exp != 0 becomes !exp */
12196 617825 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12197 50238812 : && code == NE_EXPR)
12198 608811 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12199 :
12200 : /* If this is an EQ or NE comparison with zero and ARG0 is
12201 : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12202 : two operations, but the latter can be done in one less insn
12203 : on machines that have only two-operand insns or on which a
12204 : constant cannot be the first operand. */
12205 49012887 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12206 49012887 : && integer_zerop (arg1))
12207 : {
12208 1532951 : tree arg00 = TREE_OPERAND (arg0, 0);
12209 1532951 : tree arg01 = TREE_OPERAND (arg0, 1);
12210 1532951 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12211 1532951 : && integer_onep (TREE_OPERAND (arg00, 0)))
12212 : {
12213 4325 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12214 4325 : arg01, TREE_OPERAND (arg00, 1));
12215 4325 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12216 4325 : build_one_cst (TREE_TYPE (arg0)));
12217 4325 : return fold_build2_loc (loc, code, type,
12218 4325 : fold_convert_loc (loc, TREE_TYPE (arg1),
12219 4325 : tem), arg1);
12220 : }
12221 1528626 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12222 1528626 : && integer_onep (TREE_OPERAND (arg01, 0)))
12223 : {
12224 425 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12225 425 : arg00, TREE_OPERAND (arg01, 1));
12226 425 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12227 425 : build_one_cst (TREE_TYPE (arg0)));
12228 425 : return fold_build2_loc (loc, code, type,
12229 425 : fold_convert_loc (loc, TREE_TYPE (arg1),
12230 425 : tem), arg1);
12231 : }
12232 : }
12233 :
12234 : /* If this is a comparison of a field, we may be able to simplify it. */
12235 49008137 : if ((TREE_CODE (arg0) == COMPONENT_REF
12236 49008137 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12237 : /* Handle the constant case even without -O
12238 : to make sure the warnings are given. */
12239 5055658 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12240 : {
12241 4738961 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12242 4738961 : if (t1)
12243 : return t1;
12244 : }
12245 :
12246 : /* Optimize comparisons of strlen vs zero to a compare of the
12247 : first character of the string vs zero. To wit,
12248 : strlen(ptr) == 0 => *ptr == 0
12249 : strlen(ptr) != 0 => *ptr != 0
12250 : Other cases should reduce to one of these two (or a constant)
12251 : due to the return value of strlen being unsigned. */
12252 48206185 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12253 : {
12254 3062948 : tree fndecl = get_callee_fndecl (arg0);
12255 :
12256 3062948 : if (fndecl
12257 3061856 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12258 550 : && call_expr_nargs (arg0) == 1
12259 3063498 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12260 : == POINTER_TYPE))
12261 : {
12262 550 : tree ptrtype
12263 550 : = build_pointer_type (build_qualified_type (char_type_node,
12264 : TYPE_QUAL_CONST));
12265 1100 : tree ptr = fold_convert_loc (loc, ptrtype,
12266 550 : CALL_EXPR_ARG (arg0, 0));
12267 550 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12268 550 : return fold_build2_loc (loc, code, type, iref,
12269 550 : build_int_cst (TREE_TYPE (iref), 0));
12270 : }
12271 : }
12272 : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12273 : (X & C) == 0 when C is a single bit. */
12274 48205635 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12275 1699053 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12276 861 : && integer_zerop (arg1)
12277 48206097 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12278 : {
12279 140 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12280 140 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12281 140 : TREE_OPERAND (arg0, 1));
12282 280 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12283 : type, tem,
12284 140 : fold_convert_loc (loc, TREE_TYPE (arg0),
12285 140 : arg1));
12286 : }
12287 :
12288 : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12289 : constant C is a power of two, i.e. a single bit. */
12290 48205495 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12291 3919 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12292 0 : && integer_zerop (arg1)
12293 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12294 48205495 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12295 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12296 : {
12297 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12298 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12299 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12300 : }
12301 :
12302 : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12303 : when is C is a power of two, i.e. a single bit. */
12304 48205495 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12305 1698913 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12306 38247 : && integer_zerop (arg1)
12307 38247 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12308 48240926 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12309 35431 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12310 : {
12311 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12312 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12313 0 : arg000, TREE_OPERAND (arg0, 1));
12314 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12315 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12316 : }
12317 :
12318 48205495 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12319 3919 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12320 : {
12321 74 : tree arg00 = TREE_OPERAND (arg0, 0);
12322 74 : tree arg01 = TREE_OPERAND (arg0, 1);
12323 74 : tree arg10 = TREE_OPERAND (arg1, 0);
12324 74 : tree arg11 = TREE_OPERAND (arg1, 1);
12325 74 : tree itype = TREE_TYPE (arg0);
12326 :
12327 : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12328 : operand_equal_p guarantees no side-effects so we don't need
12329 : to use omit_one_operand on Z. */
12330 74 : if (operand_equal_p (arg01, arg11, 0))
12331 8 : return fold_build2_loc (loc, code, type, arg00,
12332 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12333 8 : arg10));
12334 66 : if (operand_equal_p (arg01, arg10, 0))
12335 0 : return fold_build2_loc (loc, code, type, arg00,
12336 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12337 0 : arg11));
12338 66 : if (operand_equal_p (arg00, arg11, 0))
12339 0 : return fold_build2_loc (loc, code, type, arg01,
12340 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12341 0 : arg10));
12342 66 : if (operand_equal_p (arg00, arg10, 0))
12343 0 : return fold_build2_loc (loc, code, type, arg01,
12344 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12345 0 : arg11));
12346 :
12347 : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12348 66 : if (TREE_CODE (arg01) == INTEGER_CST
12349 8 : && TREE_CODE (arg11) == INTEGER_CST)
12350 : {
12351 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12352 : fold_convert_loc (loc, itype, arg11));
12353 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12354 8 : return fold_build2_loc (loc, code, type, tem,
12355 8 : fold_convert_loc (loc, itype, arg10));
12356 : }
12357 : }
12358 :
12359 : /* Attempt to simplify equality/inequality comparisons of complex
12360 : values. Only lower the comparison if the result is known or
12361 : can be simplified to a single scalar comparison. */
12362 48205479 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12363 48202916 : || TREE_CODE (arg0) == COMPLEX_CST)
12364 2563 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12365 2365 : || TREE_CODE (arg1) == COMPLEX_CST))
12366 : {
12367 1750 : tree real0, imag0, real1, imag1;
12368 1750 : tree rcond, icond;
12369 :
12370 1750 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12371 : {
12372 1750 : real0 = TREE_OPERAND (arg0, 0);
12373 1750 : imag0 = TREE_OPERAND (arg0, 1);
12374 : }
12375 : else
12376 : {
12377 0 : real0 = TREE_REALPART (arg0);
12378 0 : imag0 = TREE_IMAGPART (arg0);
12379 : }
12380 :
12381 1750 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12382 : {
12383 198 : real1 = TREE_OPERAND (arg1, 0);
12384 198 : imag1 = TREE_OPERAND (arg1, 1);
12385 : }
12386 : else
12387 : {
12388 1552 : real1 = TREE_REALPART (arg1);
12389 1552 : imag1 = TREE_IMAGPART (arg1);
12390 : }
12391 :
12392 1750 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12393 1750 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12394 : {
12395 11 : if (integer_zerop (rcond))
12396 : {
12397 11 : if (code == EQ_EXPR)
12398 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12399 0 : imag0, imag1);
12400 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12401 : }
12402 : else
12403 : {
12404 0 : if (code == NE_EXPR)
12405 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12406 0 : imag0, imag1);
12407 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12408 : }
12409 : }
12410 :
12411 1739 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12412 1739 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12413 : {
12414 9 : if (integer_zerop (icond))
12415 : {
12416 7 : if (code == EQ_EXPR)
12417 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12418 1 : real0, real1);
12419 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12420 : }
12421 : else
12422 : {
12423 2 : if (code == NE_EXPR)
12424 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12425 1 : real0, real1);
12426 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12427 : }
12428 : }
12429 : }
12430 :
12431 : return NULL_TREE;
12432 :
12433 41525158 : case LT_EXPR:
12434 41525158 : case GT_EXPR:
12435 41525158 : case LE_EXPR:
12436 41525158 : case GE_EXPR:
12437 41525158 : tem = fold_comparison (loc, code, type, op0, op1);
12438 41525158 : if (tem != NULL_TREE)
12439 : return tem;
12440 :
12441 : /* Transform comparisons of the form X +- C CMP X. */
12442 40652593 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12443 4782246 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12444 51405 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12445 40652609 : && !HONOR_SNANS (arg0))
12446 : {
12447 14 : tree arg01 = TREE_OPERAND (arg0, 1);
12448 14 : enum tree_code code0 = TREE_CODE (arg0);
12449 14 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12450 :
12451 : /* (X - c) > X becomes false. */
12452 14 : if (code == GT_EXPR
12453 4 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12454 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12455 4 : return constant_boolean_node (0, type);
12456 :
12457 : /* Likewise (X + c) < X becomes false. */
12458 10 : if (code == LT_EXPR
12459 3 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12460 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12461 3 : return constant_boolean_node (0, type);
12462 :
12463 : /* Convert (X - c) <= X to true. */
12464 7 : if (!HONOR_NANS (arg1)
12465 6 : && code == LE_EXPR
12466 11 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12467 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12468 4 : return constant_boolean_node (1, type);
12469 :
12470 : /* Convert (X + c) >= X to true. */
12471 3 : if (!HONOR_NANS (arg1)
12472 2 : && code == GE_EXPR
12473 5 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12474 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12475 2 : return constant_boolean_node (1, type);
12476 : }
12477 :
12478 : /* If we are comparing an ABS_EXPR with a constant, we can
12479 : convert all the cases into explicit comparisons, but they may
12480 : well not be faster than doing the ABS and one comparison.
12481 : But ABS (X) <= C is a range comparison, which becomes a subtraction
12482 : and a comparison, and is probably faster. */
12483 40652580 : if (code == LE_EXPR
12484 7739399 : && TREE_CODE (arg1) == INTEGER_CST
12485 5511058 : && TREE_CODE (arg0) == ABS_EXPR
12486 484 : && ! TREE_SIDE_EFFECTS (arg0)
12487 484 : && (tem = negate_expr (arg1)) != 0
12488 484 : && TREE_CODE (tem) == INTEGER_CST
12489 40653064 : && !TREE_OVERFLOW (tem))
12490 968 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12491 : build2 (GE_EXPR, type,
12492 484 : TREE_OPERAND (arg0, 0), tem),
12493 : build2 (LE_EXPR, type,
12494 968 : TREE_OPERAND (arg0, 0), arg1));
12495 :
12496 : /* Convert ABS_EXPR<x> >= 0 to true. */
12497 40652096 : if (code == GE_EXPR
12498 4250001 : && (integer_zerop (arg1)
12499 3103935 : || (! HONOR_NANS (arg0)
12500 2427120 : && real_zerop (arg1)))
12501 41798393 : && tree_expr_nonnegative_p (arg0))
12502 5254 : return omit_one_operand_loc (loc, type,
12503 : constant_boolean_node (true, type),
12504 5254 : arg0);
12505 :
12506 : /* Convert ABS_EXPR<x> < 0 to false. */
12507 40646842 : if (code == LT_EXPR
12508 13499992 : && (integer_zerop (arg1) || real_zerop (arg1))
12509 43926539 : && tree_expr_nonnegative_p (arg0))
12510 43859 : return omit_one_operand_loc (loc, type,
12511 : constant_boolean_node (false, type),
12512 43859 : arg0);
12513 :
12514 : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12515 : and similarly for >= into !=. */
12516 40602983 : if ((code == LT_EXPR || code == GE_EXPR)
12517 17700880 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12518 5603504 : && TREE_CODE (arg1) == LSHIFT_EXPR
12519 40604510 : && integer_onep (TREE_OPERAND (arg1, 0)))
12520 4054 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12521 1355 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12522 1355 : TREE_OPERAND (arg1, 1)),
12523 2710 : build_zero_cst (TREE_TYPE (arg0)));
12524 :
12525 : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12526 : otherwise Y might be >= # of bits in X's type and thus e.g.
12527 : (unsigned char) (1 << Y) for Y 15 might be 0.
12528 : If the cast is widening, then 1 << Y should have unsigned type,
12529 : otherwise if Y is number of bits in the signed shift type minus 1,
12530 : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12531 : 31 might be 0xffffffff80000000. */
12532 40601628 : if ((code == LT_EXPR || code == GE_EXPR)
12533 17699525 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12534 5784542 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12535 11938553 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12536 4084422 : && CONVERT_EXPR_P (arg1)
12537 1134723 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12538 42 : && (element_precision (TREE_TYPE (arg1))
12539 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12540 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12541 14 : || (element_precision (TREE_TYPE (arg1))
12542 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12543 40601635 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12544 : {
12545 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12546 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12547 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12548 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12549 14 : build_zero_cst (TREE_TYPE (arg0)));
12550 : }
12551 :
12552 : return NULL_TREE;
12553 :
12554 5999699 : case UNORDERED_EXPR:
12555 5999699 : case ORDERED_EXPR:
12556 5999699 : case UNLT_EXPR:
12557 5999699 : case UNLE_EXPR:
12558 5999699 : case UNGT_EXPR:
12559 5999699 : case UNGE_EXPR:
12560 5999699 : case UNEQ_EXPR:
12561 5999699 : case LTGT_EXPR:
12562 : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12563 5999699 : {
12564 5999699 : tree targ0 = strip_float_extensions (arg0);
12565 5999699 : tree targ1 = strip_float_extensions (arg1);
12566 5999699 : tree newtype = TREE_TYPE (targ0);
12567 :
12568 5999699 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12569 1283 : newtype = TREE_TYPE (targ1);
12570 :
12571 5999699 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12572 5999699 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12573 328 : return fold_build2_loc (loc, code, type,
12574 : fold_convert_loc (loc, newtype, targ0),
12575 328 : fold_convert_loc (loc, newtype, targ1));
12576 : }
12577 :
12578 : return NULL_TREE;
12579 :
12580 8728979 : case COMPOUND_EXPR:
12581 : /* When pedantic, a compound expression can be neither an lvalue
12582 : nor an integer constant expression. */
12583 8728979 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12584 : return NULL_TREE;
12585 : /* Don't let (0, 0) be null pointer constant. */
12586 518110 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12587 518110 : : fold_convert_loc (loc, type, arg1);
12588 : return tem;
12589 :
12590 : default:
12591 : return NULL_TREE;
12592 : } /* switch (code) */
12593 : }
12594 :
12595 : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12596 : ((A & N) + B) & M -> (A + B) & M
12597 : Similarly if (N & M) == 0,
12598 : ((A | N) + B) & M -> (A + B) & M
12599 : and for - instead of + (or unary - instead of +)
12600 : and/or ^ instead of |.
12601 : If B is constant and (B & M) == 0, fold into A & M.
12602 :
12603 : This function is a helper for match.pd patterns. Return non-NULL
12604 : type in which the simplified operation should be performed only
12605 : if any optimization is possible.
12606 :
12607 : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12608 : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12609 : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12610 : +/-. */
12611 : tree
12612 1265583 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12613 : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12614 : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12615 : tree *pmop)
12616 : {
12617 1265583 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12618 1265583 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12619 1265583 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12620 2531166 : if (~cst1 == 0
12621 3792411 : || (cst1 & (cst1 + 1)) != 0
12622 1053237 : || !INTEGRAL_TYPE_P (type)
12623 1053237 : || (!TYPE_OVERFLOW_WRAPS (type)
12624 44689 : && TREE_CODE (type) != INTEGER_TYPE)
12625 4635471 : || (wi::max_value (type) & cst1) != cst1)
12626 : return NULL_TREE;
12627 :
12628 1053237 : enum tree_code codes[2] = { code00, code01 };
12629 1053237 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12630 1053237 : int which = 0;
12631 1053237 : wide_int cst0;
12632 :
12633 : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12634 : arg1 (M) is == (1LL << cst) - 1.
12635 : Store C into PMOP[0] and D into PMOP[1]. */
12636 1053237 : pmop[0] = arg00;
12637 1053237 : pmop[1] = arg01;
12638 1053237 : which = code != NEGATE_EXPR;
12639 :
12640 3158793 : for (; which >= 0; which--)
12641 2105556 : switch (codes[which])
12642 : {
12643 21219 : case BIT_AND_EXPR:
12644 21219 : case BIT_IOR_EXPR:
12645 21219 : case BIT_XOR_EXPR:
12646 21219 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12647 21219 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12648 21219 : if (codes[which] == BIT_AND_EXPR)
12649 : {
12650 21107 : if (cst0 != cst1)
12651 : break;
12652 : }
12653 112 : else if (cst0 != 0)
12654 : break;
12655 : /* If C or D is of the form (A & N) where
12656 : (N & M) == M, or of the form (A | N) or
12657 : (A ^ N) where (N & M) == 0, replace it with A. */
12658 19680 : pmop[which] = arg0xx[2 * which];
12659 19680 : break;
12660 2084337 : case ERROR_MARK:
12661 2084337 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12662 : break;
12663 : /* If C or D is a N where (N & M) == 0, it can be
12664 : omitted (replaced with 0). */
12665 888452 : if ((code == PLUS_EXPR
12666 216167 : || (code == MINUS_EXPR && which == 0))
12667 660393 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12668 135907 : pmop[which] = build_int_cst (type, 0);
12669 : /* Similarly, with C - N where (-N & M) == 0. */
12670 888452 : if (code == MINUS_EXPR
12671 444226 : && which == 1
12672 653075 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12673 200808 : pmop[which] = build_int_cst (type, 0);
12674 : break;
12675 0 : default:
12676 0 : gcc_unreachable ();
12677 : }
12678 :
12679 : /* Only build anything new if we optimized one or both arguments above. */
12680 1053237 : if (pmop[0] == arg00 && pmop[1] == arg01)
12681 : return NULL_TREE;
12682 :
12683 355814 : if (TYPE_OVERFLOW_WRAPS (type))
12684 : return type;
12685 : else
12686 3010 : return unsigned_type_for (type);
12687 1053237 : }
12688 :
12689 : /* Used by contains_label_[p1]. */
12690 :
12691 : struct contains_label_data
12692 : {
12693 : hash_set<tree> *pset;
12694 : bool inside_switch_p;
12695 : };
12696 :
12697 : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12698 : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12699 : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12700 :
12701 : static tree
12702 4610423 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12703 : {
12704 4610423 : contains_label_data *d = (contains_label_data *) data;
12705 4610423 : switch (TREE_CODE (*tp))
12706 : {
12707 : case LABEL_EXPR:
12708 : return *tp;
12709 :
12710 0 : case CASE_LABEL_EXPR:
12711 0 : if (!d->inside_switch_p)
12712 : return *tp;
12713 : return NULL_TREE;
12714 :
12715 0 : case SWITCH_EXPR:
12716 0 : if (!d->inside_switch_p)
12717 : {
12718 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12719 0 : return *tp;
12720 0 : d->inside_switch_p = true;
12721 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12722 0 : return *tp;
12723 0 : d->inside_switch_p = false;
12724 0 : *walk_subtrees = 0;
12725 : }
12726 : return NULL_TREE;
12727 :
12728 6627 : case GOTO_EXPR:
12729 6627 : *walk_subtrees = 0;
12730 6627 : return NULL_TREE;
12731 :
12732 : default:
12733 : return NULL_TREE;
12734 : }
12735 : }
12736 :
12737 : /* Return whether the sub-tree ST contains a label which is accessible from
12738 : outside the sub-tree. */
12739 :
12740 : static bool
12741 330159 : contains_label_p (tree st)
12742 : {
12743 330159 : hash_set<tree> pset;
12744 330159 : contains_label_data data = { &pset, false };
12745 330159 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12746 330159 : }
12747 :
12748 : /* Fold a ternary expression of code CODE and type TYPE with operands
12749 : OP0, OP1, and OP2. Return the folded expression if folding is
12750 : successful. Otherwise, return NULL_TREE. */
12751 :
12752 : tree
12753 47723896 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12754 : tree op0, tree op1, tree op2)
12755 : {
12756 47723896 : tree tem;
12757 47723896 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12758 47723896 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12759 :
12760 47723896 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12761 : && TREE_CODE_LENGTH (code) == 3);
12762 :
12763 : /* If this is a commutative operation, and OP0 is a constant, move it
12764 : to OP1 to reduce the number of tests below. */
12765 47723896 : if (commutative_ternary_tree_code (code)
12766 47723896 : && tree_swap_operands_p (op0, op1))
12767 33 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12768 :
12769 47723863 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12770 47723863 : if (tem)
12771 : return tem;
12772 :
12773 : /* Strip any conversions that don't change the mode. This is safe
12774 : for every expression, except for a comparison expression because
12775 : its signedness is derived from its operands. So, in the latter
12776 : case, only strip conversions that don't change the signedness.
12777 :
12778 : Note that this is done as an internal manipulation within the
12779 : constant folder, in order to find the simplest representation of
12780 : the arguments so that their form can be studied. In any cases,
12781 : the appropriate type conversions should be put back in the tree
12782 : that will get out of the constant folder. */
12783 46629597 : if (op0)
12784 : {
12785 46562043 : arg0 = op0;
12786 46562043 : STRIP_NOPS (arg0);
12787 : }
12788 :
12789 46629597 : if (op1)
12790 : {
12791 46629597 : arg1 = op1;
12792 46629597 : STRIP_NOPS (arg1);
12793 : }
12794 :
12795 46629597 : if (op2)
12796 : {
12797 15156736 : arg2 = op2;
12798 15156736 : STRIP_NOPS (arg2);
12799 : }
12800 :
12801 46629597 : switch (code)
12802 : {
12803 31472379 : case COMPONENT_REF:
12804 31472379 : if (TREE_CODE (arg0) == CONSTRUCTOR
12805 31472379 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12806 : {
12807 : unsigned HOST_WIDE_INT idx;
12808 : tree field, value;
12809 886 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12810 681 : if (field == arg1)
12811 : return value;
12812 : }
12813 : return NULL_TREE;
12814 :
12815 12649869 : case COND_EXPR:
12816 12649869 : case VEC_COND_EXPR:
12817 : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12818 : so all simple results must be passed through pedantic_non_lvalue. */
12819 12649869 : if (TREE_CODE (arg0) == INTEGER_CST)
12820 : {
12821 463989 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12822 463989 : tem = integer_zerop (arg0) ? op2 : op1;
12823 : /* Only optimize constant conditions when the selected branch
12824 : has the same type as the COND_EXPR. This avoids optimizing
12825 : away "c ? x : throw", where the throw has a void type.
12826 : Avoid throwing away that operand which contains label. */
12827 463989 : if ((!TREE_SIDE_EFFECTS (unused_op)
12828 330159 : || !contains_label_p (unused_op))
12829 789309 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12830 379432 : || VOID_TYPE_P (type)))
12831 449944 : return protected_set_expr_location_unshare (tem, loc);
12832 : return NULL_TREE;
12833 : }
12834 12185880 : else if (TREE_CODE (arg0) == VECTOR_CST)
12835 : {
12836 11742 : unsigned HOST_WIDE_INT nelts;
12837 11742 : if ((TREE_CODE (arg1) == VECTOR_CST
12838 9041 : || TREE_CODE (arg1) == CONSTRUCTOR)
12839 2701 : && (TREE_CODE (arg2) == VECTOR_CST
12840 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12841 23484 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12842 : {
12843 2701 : vec_perm_builder sel (nelts, nelts, 1);
12844 26711 : for (unsigned int i = 0; i < nelts; i++)
12845 : {
12846 24010 : tree val = VECTOR_CST_ELT (arg0, i);
12847 24010 : if (integer_all_onesp (val))
12848 11884 : sel.quick_push (i);
12849 12126 : else if (integer_zerop (val))
12850 12126 : sel.quick_push (nelts + i);
12851 : else /* Currently unreachable. */
12852 2013 : return NULL_TREE;
12853 : }
12854 2701 : vec_perm_indices indices (sel, 2, nelts);
12855 2701 : tree t = fold_vec_perm (type, arg1, arg2, indices);
12856 2701 : if (t != NULL_TREE)
12857 2013 : return t;
12858 4714 : }
12859 : }
12860 :
12861 : /* If we have A op B ? A : C, we may be able to convert this to a
12862 : simpler expression, depending on the operation and the values
12863 : of B and C. Signed zeros prevent all of these transformations,
12864 : for reasons given above each one.
12865 :
12866 : Also try swapping the arguments and inverting the conditional. */
12867 12183867 : if (COMPARISON_CLASS_P (arg0)
12868 10021246 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12869 12325337 : && !HONOR_SIGNED_ZEROS (op1))
12870 : {
12871 130679 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12872 130679 : TREE_OPERAND (arg0, 0),
12873 130679 : TREE_OPERAND (arg0, 1),
12874 : op1, op2);
12875 130679 : if (tem)
12876 : return tem;
12877 : }
12878 :
12879 12177008 : if (COMPARISON_CLASS_P (arg0)
12880 10014387 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12881 12650519 : && !HONOR_SIGNED_ZEROS (op2))
12882 : {
12883 386922 : enum tree_code comp_code = TREE_CODE (arg0);
12884 386922 : tree arg00 = TREE_OPERAND (arg0, 0);
12885 386922 : tree arg01 = TREE_OPERAND (arg0, 1);
12886 386922 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12887 386922 : if (comp_code != ERROR_MARK)
12888 386922 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12889 : arg00,
12890 : arg01,
12891 : op2, op1);
12892 386922 : if (tem)
12893 : return tem;
12894 : }
12895 :
12896 : /* If the second operand is simpler than the third, swap them
12897 : since that produces better jump optimization results. */
12898 11909529 : if (truth_value_p (TREE_CODE (arg0))
12899 11909529 : && tree_swap_operands_p (op1, op2))
12900 : {
12901 2071914 : location_t loc0 = expr_location_or (arg0, loc);
12902 : /* See if this can be inverted. If it can't, possibly because
12903 : it was a floating-point inequality comparison, don't do
12904 : anything. */
12905 2071914 : tem = fold_invert_truthvalue (loc0, arg0);
12906 2071914 : if (tem)
12907 1322192 : return fold_build3_loc (loc, code, type, tem, op2, op1);
12908 : }
12909 :
12910 : /* Convert A ? 1 : 0 to simply A. */
12911 10587337 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12912 10135174 : : (integer_onep (op1)
12913 414451 : && !VECTOR_TYPE_P (type)))
12914 693055 : && integer_zerop (op2)
12915 : /* If we try to convert OP0 to our type, the
12916 : call to fold will try to move the conversion inside
12917 : a COND, which will recurse. In that case, the COND_EXPR
12918 : is probably the best choice, so leave it alone. */
12919 11718692 : && type == TREE_TYPE (arg0))
12920 32860 : return protected_set_expr_location_unshare (arg0, loc);
12921 :
12922 : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12923 : over COND_EXPR in cases such as floating point comparisons. */
12924 10554477 : if (integer_zerop (op1)
12925 394361 : && code == COND_EXPR
12926 370331 : && integer_onep (op2)
12927 32291 : && !VECTOR_TYPE_P (type)
12928 10586768 : && truth_value_p (TREE_CODE (arg0)))
12929 30736 : return fold_convert_loc (loc, type,
12930 30736 : invert_truthvalue_loc (loc, arg0));
12931 :
12932 : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12933 10523741 : if (TREE_CODE (arg0) == LT_EXPR
12934 1380949 : && integer_zerop (TREE_OPERAND (arg0, 1))
12935 38199 : && integer_zerop (op2)
12936 10525104 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12937 : {
12938 : /* sign_bit_p looks through both zero and sign extensions,
12939 : but for this optimization only sign extensions are
12940 : usable. */
12941 56 : tree tem2 = TREE_OPERAND (arg0, 0);
12942 56 : while (tem != tem2)
12943 : {
12944 0 : if (TREE_CODE (tem2) != NOP_EXPR
12945 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12946 : {
12947 : tem = NULL_TREE;
12948 : break;
12949 : }
12950 0 : tem2 = TREE_OPERAND (tem2, 0);
12951 : }
12952 : /* sign_bit_p only checks ARG1 bits within A's precision.
12953 : If <sign bit of A> has wider type than A, bits outside
12954 : of A's precision in <sign bit of A> need to be checked.
12955 : If they are all 0, this optimization needs to be done
12956 : in unsigned A's type, if they are all 1 in signed A's type,
12957 : otherwise this can't be done. */
12958 56 : if (tem
12959 56 : && TYPE_PRECISION (TREE_TYPE (tem))
12960 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
12961 112 : && TYPE_PRECISION (TREE_TYPE (tem))
12962 56 : < TYPE_PRECISION (type))
12963 : {
12964 56 : int inner_width, outer_width;
12965 56 : tree tem_type;
12966 :
12967 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12968 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12969 56 : if (outer_width > TYPE_PRECISION (type))
12970 0 : outer_width = TYPE_PRECISION (type);
12971 :
12972 56 : wide_int mask = wi::shifted_mask
12973 56 : (inner_width, outer_width - inner_width, false,
12974 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
12975 :
12976 56 : wide_int common = mask & wi::to_wide (arg1);
12977 56 : if (common == mask)
12978 : {
12979 28 : tem_type = signed_type_for (TREE_TYPE (tem));
12980 28 : tem = fold_convert_loc (loc, tem_type, tem);
12981 : }
12982 28 : else if (common == 0)
12983 : {
12984 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
12985 0 : tem = fold_convert_loc (loc, tem_type, tem);
12986 : }
12987 : else
12988 : tem = NULL;
12989 56 : }
12990 :
12991 56 : if (tem)
12992 28 : return
12993 56 : fold_convert_loc (loc, type,
12994 : fold_build2_loc (loc, BIT_AND_EXPR,
12995 28 : TREE_TYPE (tem), tem,
12996 : fold_convert_loc (loc,
12997 28 : TREE_TYPE (tem),
12998 28 : arg1)));
12999 : }
13000 :
13001 : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13002 : already handled above. */
13003 10523713 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13004 347 : && integer_onep (TREE_OPERAND (arg0, 1))
13005 3 : && integer_zerop (op2)
13006 10523713 : && integer_pow2p (arg1))
13007 : {
13008 0 : tree tem = TREE_OPERAND (arg0, 0);
13009 0 : STRIP_NOPS (tem);
13010 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13011 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13012 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13013 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13014 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13015 : fold_convert_loc (loc, type,
13016 0 : TREE_OPERAND (tem, 0)),
13017 0 : op1);
13018 : }
13019 :
13020 : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13021 : is probably obsolete because the first operand should be a
13022 : truth value (that's why we have the two cases above), but let's
13023 : leave it in until we can confirm this for all front-ends. */
13024 10523713 : if (integer_zerop (op2)
13025 2067162 : && TREE_CODE (arg0) == NE_EXPR
13026 549287 : && integer_zerop (TREE_OPERAND (arg0, 1))
13027 297775 : && integer_pow2p (arg1)
13028 32835 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13029 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13030 : arg1, OEP_ONLY_CONST)
13031 : /* operand_equal_p compares just value, not precision, so e.g.
13032 : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13033 : second operand 32-bit -128, which is not a power of two (or vice
13034 : versa. */
13035 10523713 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13036 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13037 :
13038 : /* Disable the transformations below for vectors, since
13039 : fold_binary_op_with_conditional_arg may undo them immediately,
13040 : yielding an infinite loop. */
13041 10523713 : if (code == VEC_COND_EXPR)
13042 : return NULL_TREE;
13043 :
13044 : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13045 10071550 : if (integer_zerop (op2)
13046 1711047 : && truth_value_p (TREE_CODE (arg0))
13047 1517793 : && truth_value_p (TREE_CODE (arg1))
13048 10105007 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13049 33457 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13050 : : TRUTH_ANDIF_EXPR,
13051 33457 : type, fold_convert_loc (loc, type, arg0), op1);
13052 :
13053 : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13054 10038093 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13055 455291 : && truth_value_p (TREE_CODE (arg0))
13056 307902 : && truth_value_p (TREE_CODE (arg1))
13057 10075395 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13058 : {
13059 37302 : location_t loc0 = expr_location_or (arg0, loc);
13060 : /* Only perform transformation if ARG0 is easily inverted. */
13061 37302 : tem = fold_invert_truthvalue (loc0, arg0);
13062 37302 : if (tem)
13063 37038 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13064 : ? BIT_IOR_EXPR
13065 : : TRUTH_ORIF_EXPR,
13066 : type, fold_convert_loc (loc, type, tem),
13067 37038 : op1);
13068 : }
13069 :
13070 : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13071 10001055 : if (integer_zerop (arg1)
13072 339672 : && truth_value_p (TREE_CODE (arg0))
13073 84364 : && truth_value_p (TREE_CODE (op2))
13074 10001083 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13075 : {
13076 28 : location_t loc0 = expr_location_or (arg0, loc);
13077 : /* Only perform transformation if ARG0 is easily inverted. */
13078 28 : tem = fold_invert_truthvalue (loc0, arg0);
13079 28 : if (tem)
13080 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13081 : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13082 : type, fold_convert_loc (loc, type, tem),
13083 0 : op2);
13084 : }
13085 :
13086 : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13087 10001055 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13088 381591 : && truth_value_p (TREE_CODE (arg0))
13089 272411 : && truth_value_p (TREE_CODE (op2))
13090 10001241 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13091 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13092 : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13093 186 : type, fold_convert_loc (loc, type, arg0), op2);
13094 :
13095 : return NULL_TREE;
13096 :
13097 0 : case CALL_EXPR:
13098 : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13099 : of fold_ternary on them. */
13100 0 : gcc_unreachable ();
13101 :
13102 954838 : case BIT_FIELD_REF:
13103 954838 : if (TREE_CODE (arg0) == VECTOR_CST
13104 80194 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13105 42749 : || (VECTOR_TYPE_P (type)
13106 41873 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13107 79282 : && tree_fits_uhwi_p (op1)
13108 1034120 : && tree_fits_uhwi_p (op2))
13109 : {
13110 79282 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13111 79282 : unsigned HOST_WIDE_INT width
13112 79282 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13113 79282 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13114 79282 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13115 79282 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13116 :
13117 79282 : if (n != 0
13118 79282 : && (idx % width) == 0
13119 79282 : && (n % width) == 0
13120 158564 : && known_le ((idx + n) / width,
13121 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13122 : {
13123 79282 : idx = idx / width;
13124 79282 : n = n / width;
13125 :
13126 79282 : if (TREE_CODE (arg0) == VECTOR_CST)
13127 : {
13128 79282 : if (n == 1)
13129 : {
13130 37449 : tem = VECTOR_CST_ELT (arg0, idx);
13131 37449 : if (VECTOR_TYPE_P (type))
13132 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13133 : return tem;
13134 : }
13135 :
13136 41833 : tree_vector_builder vals (type, n, 1);
13137 227042 : for (unsigned i = 0; i < n; ++i)
13138 143376 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13139 41833 : return vals.build ();
13140 41833 : }
13141 : }
13142 : }
13143 :
13144 : /* On constants we can use native encode/interpret to constant
13145 : fold (nearly) all BIT_FIELD_REFs. */
13146 875556 : if (CONSTANT_CLASS_P (arg0)
13147 1713 : && can_native_interpret_type_p (type)
13148 : && BITS_PER_UNIT == 8
13149 1713 : && tree_fits_uhwi_p (op1)
13150 877269 : && tree_fits_uhwi_p (op2))
13151 : {
13152 1713 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13153 1713 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13154 : /* Limit us to a reasonable amount of work. To relax the
13155 : other limitations we need bit-shifting of the buffer
13156 : and rounding up the size. */
13157 1713 : if (bitpos % BITS_PER_UNIT == 0
13158 1713 : && bitsize % BITS_PER_UNIT == 0
13159 1713 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13160 : {
13161 1713 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13162 1713 : unsigned HOST_WIDE_INT len
13163 1713 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13164 1713 : bitpos / BITS_PER_UNIT);
13165 1713 : if (len > 0
13166 1713 : && len * BITS_PER_UNIT >= bitsize)
13167 : {
13168 1713 : tree v = native_interpret_expr (type, b,
13169 : bitsize / BITS_PER_UNIT);
13170 1713 : if (v)
13171 1659 : return v;
13172 : }
13173 : }
13174 : }
13175 :
13176 : return NULL_TREE;
13177 :
13178 788789 : case VEC_PERM_EXPR:
13179 : /* Perform constant folding of BIT_INSERT_EXPR. */
13180 788789 : if (TREE_CODE (arg2) == VECTOR_CST
13181 777315 : && TREE_CODE (op0) == VECTOR_CST
13182 16476 : && TREE_CODE (op1) == VECTOR_CST)
13183 : {
13184 : /* Build a vector of integers from the tree mask. */
13185 3939 : vec_perm_builder builder;
13186 3939 : if (!tree_to_vec_perm_builder (&builder, arg2))
13187 : return NULL_TREE;
13188 :
13189 : /* Create a vec_perm_indices for the integer vector. */
13190 3939 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13191 3939 : bool single_arg = (op0 == op1);
13192 7878 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13193 3939 : return fold_vec_perm (type, op0, op1, sel);
13194 7878 : }
13195 : return NULL_TREE;
13196 :
13197 16179 : case BIT_INSERT_EXPR:
13198 : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13199 16179 : if (TREE_CODE (arg0) == INTEGER_CST
13200 14 : && TREE_CODE (arg1) == INTEGER_CST)
13201 : {
13202 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13203 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13204 2 : if (BYTES_BIG_ENDIAN)
13205 : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13206 2 : wide_int tem = (wi::to_wide (arg0)
13207 4 : & wi::shifted_mask (bitpos, bitsize, true,
13208 4 : TYPE_PRECISION (type)));
13209 2 : wide_int tem2
13210 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13211 2 : bitsize), bitpos);
13212 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13213 2 : }
13214 16177 : else if (TREE_CODE (arg0) == VECTOR_CST
13215 903 : && CONSTANT_CLASS_P (arg1)
13216 16476 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13217 299 : TREE_TYPE (arg1)))
13218 : {
13219 299 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13220 299 : unsigned HOST_WIDE_INT elsize
13221 299 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13222 299 : if (bitpos % elsize == 0)
13223 : {
13224 299 : unsigned k = bitpos / elsize;
13225 299 : unsigned HOST_WIDE_INT nelts;
13226 299 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13227 47723896 : return arg0;
13228 292 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13229 : {
13230 292 : tree_vector_builder elts (type, nelts, 1);
13231 292 : elts.quick_grow (nelts);
13232 1608 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13233 1024 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13234 292 : return elts.build ();
13235 292 : }
13236 : }
13237 : }
13238 : return NULL_TREE;
13239 :
13240 : default:
13241 : return NULL_TREE;
13242 : } /* switch (code) */
13243 : }
13244 :
13245 : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13246 : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13247 : constructor element index of the value returned. If the element is
13248 : not found NULL_TREE is returned and *CTOR_IDX is updated to
13249 : the index of the element after the ACCESS_INDEX position (which
13250 : may be outside of the CTOR array). */
13251 :
13252 : tree
13253 694525 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13254 : unsigned *ctor_idx)
13255 : {
13256 694525 : tree index_type = NULL_TREE;
13257 694525 : signop index_sgn = UNSIGNED;
13258 694525 : offset_int low_bound = 0;
13259 :
13260 694525 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13261 : {
13262 694525 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13263 694525 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13264 : {
13265 : /* Static constructors for variably sized objects makes no sense. */
13266 694525 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13267 694525 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13268 : /* ??? When it is obvious that the range is signed, treat it so. */
13269 694525 : if (TYPE_UNSIGNED (index_type)
13270 354581 : && TYPE_MAX_VALUE (domain_type)
13271 1049075 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13272 354550 : TYPE_MIN_VALUE (domain_type)))
13273 : {
13274 0 : index_sgn = SIGNED;
13275 0 : low_bound
13276 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13277 : SIGNED);
13278 : }
13279 : else
13280 : {
13281 694525 : index_sgn = TYPE_SIGN (index_type);
13282 694525 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13283 : }
13284 : }
13285 : }
13286 :
13287 694525 : if (index_type)
13288 694525 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13289 : index_sgn);
13290 :
13291 694525 : offset_int index = low_bound;
13292 694525 : if (index_type)
13293 694525 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13294 :
13295 694525 : offset_int max_index = index;
13296 694525 : unsigned cnt;
13297 694525 : tree cfield, cval;
13298 694525 : bool first_p = true;
13299 :
13300 13933156 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13301 : {
13302 : /* Array constructor might explicitly set index, or specify a range,
13303 : or leave index NULL meaning that it is next index after previous
13304 : one. */
13305 13931989 : if (cfield)
13306 : {
13307 5897509 : if (TREE_CODE (cfield) == INTEGER_CST)
13308 11793328 : max_index = index
13309 5896664 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13310 : else
13311 : {
13312 845 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13313 845 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13314 : index_sgn);
13315 845 : max_index
13316 845 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13317 : index_sgn);
13318 845 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13319 : }
13320 : }
13321 8034480 : else if (!first_p)
13322 : {
13323 7829912 : index = max_index + 1;
13324 7829912 : if (index_type)
13325 7829912 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13326 7829912 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13327 7829912 : max_index = index;
13328 : }
13329 : else
13330 : first_p = false;
13331 :
13332 13931989 : if (TREE_CODE (cval) == RAW_DATA_CST)
13333 2629 : max_index += RAW_DATA_LENGTH (cval) - 1;
13334 :
13335 : /* Do we have match? */
13336 13931989 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13337 : {
13338 13931701 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13339 : {
13340 693242 : if (ctor_idx)
13341 693242 : *ctor_idx = cnt;
13342 : return cval;
13343 : }
13344 : }
13345 288 : else if (in_gimple_form)
13346 : /* We're past the element we search for. Note during parsing
13347 : the elements might not be sorted.
13348 : ??? We should use a binary search and a flag on the
13349 : CONSTRUCTOR as to whether elements are sorted in declaration
13350 : order. */
13351 : break;
13352 : }
13353 1283 : if (ctor_idx)
13354 1283 : *ctor_idx = cnt;
13355 : return NULL_TREE;
13356 : }
13357 :
13358 : /* Perform constant folding and related simplification of EXPR.
13359 : The related simplifications include x*1 => x, x*0 => 0, etc.,
13360 : and application of the associative law.
13361 : NOP_EXPR conversions may be removed freely (as long as we
13362 : are careful not to change the type of the overall expression).
13363 : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13364 : but we can constant-fold them if they have constant operands. */
13365 :
13366 : #ifdef ENABLE_FOLD_CHECKING
13367 : # define fold(x) fold_1 (x)
13368 : static tree fold_1 (tree);
13369 : static
13370 : #endif
13371 : tree
13372 1391615169 : fold (tree expr)
13373 : {
13374 1391788101 : const tree t = expr;
13375 1391788101 : enum tree_code code = TREE_CODE (t);
13376 1391788101 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13377 1391788101 : tree tem;
13378 1391788101 : location_t loc = EXPR_LOCATION (expr);
13379 :
13380 : /* Return right away if a constant. */
13381 1391788101 : if (kind == tcc_constant)
13382 : return t;
13383 :
13384 : /* CALL_EXPR-like objects with variable numbers of operands are
13385 : treated specially. */
13386 1288670106 : if (kind == tcc_vl_exp)
13387 : {
13388 182647582 : if (code == CALL_EXPR)
13389 : {
13390 182647053 : tem = fold_call_expr (loc, expr, false);
13391 182647053 : return tem ? tem : expr;
13392 : }
13393 : return expr;
13394 : }
13395 :
13396 1106022524 : if (IS_EXPR_CODE_CLASS (kind))
13397 : {
13398 1103831299 : tree type = TREE_TYPE (t);
13399 1103831299 : tree op0, op1, op2;
13400 :
13401 1103831299 : switch (TREE_CODE_LENGTH (code))
13402 : {
13403 1002328268 : case 1:
13404 1002328268 : op0 = TREE_OPERAND (t, 0);
13405 1002328268 : tem = fold_unary_loc (loc, code, type, op0);
13406 1002328268 : return tem ? tem : expr;
13407 92275230 : case 2:
13408 92275230 : op0 = TREE_OPERAND (t, 0);
13409 92275230 : op1 = TREE_OPERAND (t, 1);
13410 92275230 : tem = fold_binary_loc (loc, code, type, op0, op1);
13411 92275230 : return tem ? tem : expr;
13412 4435553 : case 3:
13413 4435553 : op0 = TREE_OPERAND (t, 0);
13414 4435553 : op1 = TREE_OPERAND (t, 1);
13415 4435553 : op2 = TREE_OPERAND (t, 2);
13416 4435553 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13417 4435553 : return tem ? tem : expr;
13418 : default:
13419 : break;
13420 : }
13421 : }
13422 :
13423 6983473 : switch (code)
13424 : {
13425 4688268 : case ARRAY_REF:
13426 4688268 : {
13427 4688268 : tree op0 = TREE_OPERAND (t, 0);
13428 4688268 : tree op1 = TREE_OPERAND (t, 1);
13429 :
13430 4688268 : if (TREE_CODE (op1) == INTEGER_CST
13431 2934429 : && TREE_CODE (op0) == CONSTRUCTOR
13432 4689723 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13433 : {
13434 1455 : unsigned int idx;
13435 1455 : tree val
13436 1455 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13437 : &idx);
13438 1455 : if (val)
13439 : {
13440 1455 : if (TREE_CODE (val) != RAW_DATA_CST)
13441 : return val;
13442 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13443 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13444 : != INTEGER_CST))
13445 : return t;
13446 2 : offset_int o
13447 2 : = (wi::to_offset (op1)
13448 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13449 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13450 2 : return build_int_cst (TREE_TYPE (val),
13451 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13452 : }
13453 : }
13454 :
13455 : return t;
13456 : }
13457 :
13458 : /* Return a VECTOR_CST if possible. */
13459 206850 : case CONSTRUCTOR:
13460 206850 : {
13461 206850 : tree type = TREE_TYPE (t);
13462 206850 : if (TREE_CODE (type) != VECTOR_TYPE)
13463 : return t;
13464 :
13465 : unsigned i;
13466 : tree val;
13467 364028 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13468 314921 : if (! CONSTANT_CLASS_P (val))
13469 : return t;
13470 :
13471 49107 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13472 : }
13473 :
13474 172932 : case CONST_DECL:
13475 172932 : return fold (DECL_INITIAL (t));
13476 :
13477 : default:
13478 : return t;
13479 : } /* switch (code) */
13480 : }
13481 :
13482 : #ifdef ENABLE_FOLD_CHECKING
13483 : #undef fold
13484 :
13485 : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13486 : hash_table<nofree_ptr_hash<const tree_node> > *);
13487 : static void fold_check_failed (const_tree, const_tree);
13488 : void print_fold_checksum (const_tree);
13489 :
13490 : /* When --enable-checking=fold, compute a digest of expr before
13491 : and after actual fold call to see if fold did not accidentally
13492 : change original expr. */
13493 :
13494 : tree
13495 : fold (tree expr)
13496 : {
13497 : tree ret;
13498 : struct md5_ctx ctx;
13499 : unsigned char checksum_before[16], checksum_after[16];
13500 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13501 :
13502 : md5_init_ctx (&ctx);
13503 : fold_checksum_tree (expr, &ctx, &ht);
13504 : md5_finish_ctx (&ctx, checksum_before);
13505 : ht.empty ();
13506 :
13507 : ret = fold_1 (expr);
13508 :
13509 : md5_init_ctx (&ctx);
13510 : fold_checksum_tree (expr, &ctx, &ht);
13511 : md5_finish_ctx (&ctx, checksum_after);
13512 :
13513 : if (memcmp (checksum_before, checksum_after, 16))
13514 : fold_check_failed (expr, ret);
13515 :
13516 : return ret;
13517 : }
13518 :
13519 : void
13520 : print_fold_checksum (const_tree expr)
13521 : {
13522 : struct md5_ctx ctx;
13523 : unsigned char checksum[16], cnt;
13524 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13525 :
13526 : md5_init_ctx (&ctx);
13527 : fold_checksum_tree (expr, &ctx, &ht);
13528 : md5_finish_ctx (&ctx, checksum);
13529 : for (cnt = 0; cnt < 16; ++cnt)
13530 : fprintf (stderr, "%02x", checksum[cnt]);
13531 : putc ('\n', stderr);
13532 : }
13533 :
13534 : static void
13535 : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13536 : {
13537 : internal_error ("fold check: original tree changed by fold");
13538 : }
13539 :
13540 : static void
13541 : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13542 : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13543 : {
13544 : const tree_node **slot;
13545 : enum tree_code code;
13546 : union tree_node *buf;
13547 : int i, len;
13548 :
13549 : recursive_label:
13550 : if (expr == NULL)
13551 : return;
13552 : slot = ht->find_slot (expr, INSERT);
13553 : if (*slot != NULL)
13554 : return;
13555 : *slot = expr;
13556 : code = TREE_CODE (expr);
13557 : if (TREE_CODE_CLASS (code) == tcc_declaration
13558 : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13559 : {
13560 : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13561 : size_t sz = tree_size (expr);
13562 : buf = XALLOCAVAR (union tree_node, sz);
13563 : memcpy ((char *) buf, expr, sz);
13564 : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13565 : buf->decl_with_vis.symtab_node = NULL;
13566 : buf->base.nowarning_flag = 0;
13567 : expr = (tree) buf;
13568 : }
13569 : else if (TREE_CODE_CLASS (code) == tcc_type
13570 : && (TYPE_POINTER_TO (expr)
13571 : || TYPE_REFERENCE_TO (expr)
13572 : || TYPE_CACHED_VALUES_P (expr)
13573 : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13574 : || TYPE_NEXT_VARIANT (expr)
13575 : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13576 : {
13577 : /* Allow these fields to be modified. */
13578 : tree tmp;
13579 : size_t sz = tree_size (expr);
13580 : buf = XALLOCAVAR (union tree_node, sz);
13581 : memcpy ((char *) buf, expr, sz);
13582 : expr = tmp = (tree) buf;
13583 : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13584 : TYPE_POINTER_TO (tmp) = NULL;
13585 : TYPE_REFERENCE_TO (tmp) = NULL;
13586 : TYPE_NEXT_VARIANT (tmp) = NULL;
13587 : TYPE_ALIAS_SET (tmp) = -1;
13588 : if (TYPE_CACHED_VALUES_P (tmp))
13589 : {
13590 : TYPE_CACHED_VALUES_P (tmp) = 0;
13591 : TYPE_CACHED_VALUES (tmp) = NULL;
13592 : }
13593 : }
13594 : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13595 : {
13596 : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13597 : that and change builtins.cc etc. instead - see PR89543. */
13598 : size_t sz = tree_size (expr);
13599 : buf = XALLOCAVAR (union tree_node, sz);
13600 : memcpy ((char *) buf, expr, sz);
13601 : buf->base.nowarning_flag = 0;
13602 : expr = (tree) buf;
13603 : }
13604 : md5_process_bytes (expr, tree_size (expr), ctx);
13605 : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13606 : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13607 : if (TREE_CODE_CLASS (code) != tcc_type
13608 : && TREE_CODE_CLASS (code) != tcc_declaration
13609 : && code != TREE_LIST
13610 : && code != SSA_NAME
13611 : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13612 : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13613 : switch (TREE_CODE_CLASS (code))
13614 : {
13615 : case tcc_constant:
13616 : switch (code)
13617 : {
13618 : case STRING_CST:
13619 : md5_process_bytes (TREE_STRING_POINTER (expr),
13620 : TREE_STRING_LENGTH (expr), ctx);
13621 : break;
13622 : case COMPLEX_CST:
13623 : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13624 : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13625 : break;
13626 : case VECTOR_CST:
13627 : len = vector_cst_encoded_nelts (expr);
13628 : for (i = 0; i < len; ++i)
13629 : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13630 : break;
13631 : default:
13632 : break;
13633 : }
13634 : break;
13635 : case tcc_exceptional:
13636 : switch (code)
13637 : {
13638 : case TREE_LIST:
13639 : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13640 : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13641 : expr = TREE_CHAIN (expr);
13642 : goto recursive_label;
13643 : break;
13644 : case TREE_VEC:
13645 : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13646 : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13647 : break;
13648 : default:
13649 : break;
13650 : }
13651 : break;
13652 : case tcc_expression:
13653 : case tcc_reference:
13654 : case tcc_comparison:
13655 : case tcc_unary:
13656 : case tcc_binary:
13657 : case tcc_statement:
13658 : case tcc_vl_exp:
13659 : len = TREE_OPERAND_LENGTH (expr);
13660 : for (i = 0; i < len; ++i)
13661 : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13662 : break;
13663 : case tcc_declaration:
13664 : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13665 : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13666 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13667 : {
13668 : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13669 : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13670 : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13671 : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13672 : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13673 : }
13674 :
13675 : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13676 : {
13677 : if (TREE_CODE (expr) == FUNCTION_DECL)
13678 : {
13679 : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13680 : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13681 : }
13682 : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13683 : }
13684 : break;
13685 : case tcc_type:
13686 : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13687 : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13688 : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13689 : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13690 : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13691 : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13692 : if (INTEGRAL_TYPE_P (expr)
13693 : || SCALAR_FLOAT_TYPE_P (expr))
13694 : {
13695 : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13696 : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13697 : }
13698 : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13699 : if (RECORD_OR_UNION_TYPE_P (expr))
13700 : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13701 : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13702 : break;
13703 : default:
13704 : break;
13705 : }
13706 : }
13707 :
13708 : /* Helper function for outputting the checksum of a tree T. When
13709 : debugging with gdb, you can "define mynext" to be "next" followed
13710 : by "call debug_fold_checksum (op0)", then just trace down till the
13711 : outputs differ. */
13712 :
13713 : DEBUG_FUNCTION void
13714 : debug_fold_checksum (const_tree t)
13715 : {
13716 : int i;
13717 : unsigned char checksum[16];
13718 : struct md5_ctx ctx;
13719 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13720 :
13721 : md5_init_ctx (&ctx);
13722 : fold_checksum_tree (t, &ctx, &ht);
13723 : md5_finish_ctx (&ctx, checksum);
13724 : ht.empty ();
13725 :
13726 : for (i = 0; i < 16; i++)
13727 : fprintf (stderr, "%d ", checksum[i]);
13728 :
13729 : fprintf (stderr, "\n");
13730 : }
13731 :
13732 : #endif
13733 :
13734 : /* Fold a unary tree expression with code CODE of type TYPE with an
13735 : operand OP0. LOC is the location of the resulting expression.
13736 : Return a folded expression if successful. Otherwise, return a tree
13737 : expression with code CODE of type TYPE with an operand OP0. */
13738 :
13739 : tree
13740 1066659936 : fold_build1_loc (location_t loc,
13741 : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13742 : {
13743 1066659936 : tree tem;
13744 : #ifdef ENABLE_FOLD_CHECKING
13745 : unsigned char checksum_before[16], checksum_after[16];
13746 : struct md5_ctx ctx;
13747 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13748 :
13749 : md5_init_ctx (&ctx);
13750 : fold_checksum_tree (op0, &ctx, &ht);
13751 : md5_finish_ctx (&ctx, checksum_before);
13752 : ht.empty ();
13753 : #endif
13754 :
13755 1066659936 : tem = fold_unary_loc (loc, code, type, op0);
13756 1066659936 : if (!tem)
13757 543376632 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13758 :
13759 : #ifdef ENABLE_FOLD_CHECKING
13760 : md5_init_ctx (&ctx);
13761 : fold_checksum_tree (op0, &ctx, &ht);
13762 : md5_finish_ctx (&ctx, checksum_after);
13763 :
13764 : if (memcmp (checksum_before, checksum_after, 16))
13765 : fold_check_failed (op0, tem);
13766 : #endif
13767 1066659936 : return tem;
13768 : }
13769 :
13770 : /* Fold a binary tree expression with code CODE of type TYPE with
13771 : operands OP0 and OP1. LOC is the location of the resulting
13772 : expression. Return a folded expression if successful. Otherwise,
13773 : return a tree expression with code CODE of type TYPE with operands
13774 : OP0 and OP1. */
13775 :
13776 : tree
13777 684300249 : fold_build2_loc (location_t loc,
13778 : enum tree_code code, tree type, tree op0, tree op1
13779 : MEM_STAT_DECL)
13780 : {
13781 684300249 : tree tem;
13782 : #ifdef ENABLE_FOLD_CHECKING
13783 : unsigned char checksum_before_op0[16],
13784 : checksum_before_op1[16],
13785 : checksum_after_op0[16],
13786 : checksum_after_op1[16];
13787 : struct md5_ctx ctx;
13788 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13789 :
13790 : md5_init_ctx (&ctx);
13791 : fold_checksum_tree (op0, &ctx, &ht);
13792 : md5_finish_ctx (&ctx, checksum_before_op0);
13793 : ht.empty ();
13794 :
13795 : md5_init_ctx (&ctx);
13796 : fold_checksum_tree (op1, &ctx, &ht);
13797 : md5_finish_ctx (&ctx, checksum_before_op1);
13798 : ht.empty ();
13799 : #endif
13800 :
13801 684300249 : tem = fold_binary_loc (loc, code, type, op0, op1);
13802 684300249 : if (!tem)
13803 385198319 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13804 :
13805 : #ifdef ENABLE_FOLD_CHECKING
13806 : md5_init_ctx (&ctx);
13807 : fold_checksum_tree (op0, &ctx, &ht);
13808 : md5_finish_ctx (&ctx, checksum_after_op0);
13809 : ht.empty ();
13810 :
13811 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13812 : fold_check_failed (op0, tem);
13813 :
13814 : md5_init_ctx (&ctx);
13815 : fold_checksum_tree (op1, &ctx, &ht);
13816 : md5_finish_ctx (&ctx, checksum_after_op1);
13817 :
13818 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13819 : fold_check_failed (op1, tem);
13820 : #endif
13821 684300249 : return tem;
13822 : }
13823 :
13824 : /* Fold a ternary tree expression with code CODE of type TYPE with
13825 : operands OP0, OP1, and OP2. Return a folded expression if
13826 : successful. Otherwise, return a tree expression with code CODE of
13827 : type TYPE with operands OP0, OP1, and OP2. */
13828 :
13829 : tree
13830 41059875 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13831 : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13832 : {
13833 41059875 : tree tem;
13834 : #ifdef ENABLE_FOLD_CHECKING
13835 : unsigned char checksum_before_op0[16],
13836 : checksum_before_op1[16],
13837 : checksum_before_op2[16],
13838 : checksum_after_op0[16],
13839 : checksum_after_op1[16],
13840 : checksum_after_op2[16];
13841 : struct md5_ctx ctx;
13842 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13843 :
13844 : md5_init_ctx (&ctx);
13845 : fold_checksum_tree (op0, &ctx, &ht);
13846 : md5_finish_ctx (&ctx, checksum_before_op0);
13847 : ht.empty ();
13848 :
13849 : md5_init_ctx (&ctx);
13850 : fold_checksum_tree (op1, &ctx, &ht);
13851 : md5_finish_ctx (&ctx, checksum_before_op1);
13852 : ht.empty ();
13853 :
13854 : md5_init_ctx (&ctx);
13855 : fold_checksum_tree (op2, &ctx, &ht);
13856 : md5_finish_ctx (&ctx, checksum_before_op2);
13857 : ht.empty ();
13858 : #endif
13859 :
13860 41059875 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13861 41059875 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13862 41059875 : if (!tem)
13863 38176314 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13864 :
13865 : #ifdef ENABLE_FOLD_CHECKING
13866 : md5_init_ctx (&ctx);
13867 : fold_checksum_tree (op0, &ctx, &ht);
13868 : md5_finish_ctx (&ctx, checksum_after_op0);
13869 : ht.empty ();
13870 :
13871 : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13872 : fold_check_failed (op0, tem);
13873 :
13874 : md5_init_ctx (&ctx);
13875 : fold_checksum_tree (op1, &ctx, &ht);
13876 : md5_finish_ctx (&ctx, checksum_after_op1);
13877 : ht.empty ();
13878 :
13879 : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13880 : fold_check_failed (op1, tem);
13881 :
13882 : md5_init_ctx (&ctx);
13883 : fold_checksum_tree (op2, &ctx, &ht);
13884 : md5_finish_ctx (&ctx, checksum_after_op2);
13885 :
13886 : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13887 : fold_check_failed (op2, tem);
13888 : #endif
13889 41059875 : return tem;
13890 : }
13891 :
13892 : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13893 : arguments in ARGARRAY, and a null static chain.
13894 : Return a folded expression if successful. Otherwise, return a CALL_EXPR
13895 : of type TYPE from the given operands as constructed by build_call_array. */
13896 :
13897 : tree
13898 57949626 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
13899 : int nargs, tree *argarray)
13900 : {
13901 57949626 : tree tem;
13902 : #ifdef ENABLE_FOLD_CHECKING
13903 : unsigned char checksum_before_fn[16],
13904 : checksum_before_arglist[16],
13905 : checksum_after_fn[16],
13906 : checksum_after_arglist[16];
13907 : struct md5_ctx ctx;
13908 : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13909 : int i;
13910 :
13911 : md5_init_ctx (&ctx);
13912 : fold_checksum_tree (fn, &ctx, &ht);
13913 : md5_finish_ctx (&ctx, checksum_before_fn);
13914 : ht.empty ();
13915 :
13916 : md5_init_ctx (&ctx);
13917 : for (i = 0; i < nargs; i++)
13918 : fold_checksum_tree (argarray[i], &ctx, &ht);
13919 : md5_finish_ctx (&ctx, checksum_before_arglist);
13920 : ht.empty ();
13921 : #endif
13922 :
13923 57949626 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13924 57949626 : if (!tem)
13925 55823120 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13926 :
13927 : #ifdef ENABLE_FOLD_CHECKING
13928 : md5_init_ctx (&ctx);
13929 : fold_checksum_tree (fn, &ctx, &ht);
13930 : md5_finish_ctx (&ctx, checksum_after_fn);
13931 : ht.empty ();
13932 :
13933 : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13934 : fold_check_failed (fn, tem);
13935 :
13936 : md5_init_ctx (&ctx);
13937 : for (i = 0; i < nargs; i++)
13938 : fold_checksum_tree (argarray[i], &ctx, &ht);
13939 : md5_finish_ctx (&ctx, checksum_after_arglist);
13940 :
13941 : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13942 : fold_check_failed (NULL_TREE, tem);
13943 : #endif
13944 57949626 : return tem;
13945 : }
13946 :
13947 : /* Perform constant folding and related simplification of initializer
13948 : expression EXPR. These behave identically to "fold_buildN" but ignore
13949 : potential run-time traps and exceptions that fold must preserve. */
13950 :
13951 : #define START_FOLD_INIT \
13952 : int saved_signaling_nans = flag_signaling_nans;\
13953 : int saved_trapping_math = flag_trapping_math;\
13954 : int saved_rounding_math = flag_rounding_math;\
13955 : int saved_trapv = flag_trapv;\
13956 : int saved_folding_initializer = folding_initializer;\
13957 : flag_signaling_nans = 0;\
13958 : flag_trapping_math = 0;\
13959 : flag_rounding_math = 0;\
13960 : flag_trapv = 0;\
13961 : folding_initializer = 1;
13962 :
13963 : #define END_FOLD_INIT \
13964 : flag_signaling_nans = saved_signaling_nans;\
13965 : flag_trapping_math = saved_trapping_math;\
13966 : flag_rounding_math = saved_rounding_math;\
13967 : flag_trapv = saved_trapv;\
13968 : folding_initializer = saved_folding_initializer;
13969 :
13970 : tree
13971 544556 : fold_init (tree expr)
13972 : {
13973 544556 : tree result;
13974 544556 : START_FOLD_INIT;
13975 :
13976 544556 : result = fold (expr);
13977 :
13978 544556 : END_FOLD_INIT;
13979 544556 : return result;
13980 : }
13981 :
13982 : tree
13983 2989313 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
13984 : tree type, tree op)
13985 : {
13986 2989313 : tree result;
13987 2989313 : START_FOLD_INIT;
13988 :
13989 2989313 : result = fold_build1_loc (loc, code, type, op);
13990 :
13991 2989313 : END_FOLD_INIT;
13992 2989313 : return result;
13993 : }
13994 :
13995 : tree
13996 50460 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
13997 : tree type, tree op0, tree op1)
13998 : {
13999 50460 : tree result;
14000 50460 : START_FOLD_INIT;
14001 :
14002 50460 : result = fold_build2_loc (loc, code, type, op0, op1);
14003 :
14004 50460 : END_FOLD_INIT;
14005 50460 : return result;
14006 : }
14007 :
14008 : tree
14009 3462 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14010 : int nargs, tree *argarray)
14011 : {
14012 3462 : tree result;
14013 3462 : START_FOLD_INIT;
14014 :
14015 3462 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14016 :
14017 3462 : END_FOLD_INIT;
14018 3462 : return result;
14019 : }
14020 :
14021 : tree
14022 68365797 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14023 : tree lhs, tree rhs)
14024 : {
14025 68365797 : tree result;
14026 68365797 : START_FOLD_INIT;
14027 :
14028 68365797 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14029 :
14030 68365797 : END_FOLD_INIT;
14031 68365797 : return result;
14032 : }
14033 :
14034 : #undef START_FOLD_INIT
14035 : #undef END_FOLD_INIT
14036 :
14037 : /* Determine if first argument is a multiple of second argument. Return
14038 : false if it is not, or we cannot easily determined it to be.
14039 :
14040 : An example of the sort of thing we care about (at this point; this routine
14041 : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14042 : fold cases do now) is discovering that
14043 :
14044 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14045 :
14046 : is a multiple of
14047 :
14048 : SAVE_EXPR (J * 8)
14049 :
14050 : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14051 :
14052 : This code also handles discovering that
14053 :
14054 : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14055 :
14056 : is a multiple of 8 so we don't have to worry about dealing with a
14057 : possible remainder.
14058 :
14059 : Note that we *look* inside a SAVE_EXPR only to determine how it was
14060 : calculated; it is not safe for fold to do much of anything else with the
14061 : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14062 : at run time. For example, the latter example above *cannot* be implemented
14063 : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14064 : evaluation time of the original SAVE_EXPR is not necessarily the same at
14065 : the time the new expression is evaluated. The only optimization of this
14066 : sort that would be valid is changing
14067 :
14068 : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14069 :
14070 : divided by 8 to
14071 :
14072 : SAVE_EXPR (I) * SAVE_EXPR (J)
14073 :
14074 : (where the same SAVE_EXPR (J) is used in the original and the
14075 : transformed version).
14076 :
14077 : NOWRAP specifies whether all outer operations in TYPE should
14078 : be considered not wrapping. Any type conversion within TOP acts
14079 : as a barrier and we will fall back to NOWRAP being false.
14080 : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14081 : as not wrapping even though they are generally using unsigned arithmetic. */
14082 :
14083 : bool
14084 1560011 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14085 : {
14086 1613441 : gimple *stmt;
14087 1613441 : tree op1, op2;
14088 :
14089 1613441 : if (operand_equal_p (top, bottom, 0))
14090 : return true;
14091 :
14092 1109876 : if (TREE_CODE (type) != INTEGER_TYPE)
14093 : return false;
14094 :
14095 1109857 : switch (TREE_CODE (top))
14096 : {
14097 702 : case BIT_AND_EXPR:
14098 : /* Bitwise and provides a power of two multiple. If the mask is
14099 : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14100 702 : if (!integer_pow2p (bottom))
14101 : return false;
14102 702 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14103 702 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14104 :
14105 406859 : case MULT_EXPR:
14106 : /* If the multiplication can wrap we cannot recurse further unless
14107 : the bottom is a power of two which is where wrapping does not
14108 : matter. */
14109 406859 : if (!nowrap
14110 15526 : && !TYPE_OVERFLOW_UNDEFINED (type)
14111 411925 : && !integer_pow2p (bottom))
14112 : return false;
14113 406386 : if (TREE_CODE (bottom) == INTEGER_CST)
14114 : {
14115 404678 : op1 = TREE_OPERAND (top, 0);
14116 404678 : op2 = TREE_OPERAND (top, 1);
14117 404678 : if (TREE_CODE (op1) == INTEGER_CST)
14118 0 : std::swap (op1, op2);
14119 404678 : if (TREE_CODE (op2) == INTEGER_CST)
14120 : {
14121 394346 : if (multiple_of_p (type, op2, bottom, nowrap))
14122 : return true;
14123 : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14124 3308 : if (multiple_of_p (type, bottom, op2, nowrap))
14125 : {
14126 1909 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14127 1909 : wi::to_widest (op2));
14128 1909 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14129 : {
14130 1909 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14131 1909 : return multiple_of_p (type, op1, op2, nowrap);
14132 : }
14133 1909 : }
14134 : return multiple_of_p (type, op1, bottom, nowrap);
14135 : }
14136 : }
14137 12040 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14138 12040 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14139 :
14140 403 : case LSHIFT_EXPR:
14141 : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14142 403 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14143 : {
14144 403 : op1 = TREE_OPERAND (top, 1);
14145 403 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14146 : {
14147 403 : wide_int mul_op
14148 403 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14149 806 : return multiple_of_p (type,
14150 806 : wide_int_to_tree (type, mul_op), bottom,
14151 : nowrap);
14152 403 : }
14153 : }
14154 : return false;
14155 :
14156 227486 : case MINUS_EXPR:
14157 227486 : case PLUS_EXPR:
14158 : /* If the addition or subtraction can wrap we cannot recurse further
14159 : unless bottom is a power of two which is where wrapping does not
14160 : matter. */
14161 227486 : if (!nowrap
14162 176104 : && !TYPE_OVERFLOW_UNDEFINED (type)
14163 402146 : && !integer_pow2p (bottom))
14164 : return false;
14165 :
14166 : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14167 : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14168 : but 0xfffffffd is not. */
14169 198264 : op1 = TREE_OPERAND (top, 1);
14170 198264 : if (TREE_CODE (top) == PLUS_EXPR
14171 191781 : && nowrap
14172 44990 : && TYPE_UNSIGNED (type)
14173 242518 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14174 27915 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14175 :
14176 : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14177 : precisely, so be conservative here checking if both op0 and op1
14178 : are multiple of bottom. Note we check the second operand first
14179 : since it's usually simpler. */
14180 198264 : return (multiple_of_p (type, op1, bottom, nowrap)
14181 198264 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14182 :
14183 146406 : CASE_CONVERT:
14184 : /* Can't handle conversions from non-integral or wider integral type. */
14185 146406 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14186 146406 : || (TYPE_PRECISION (type)
14187 39122 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14188 : return false;
14189 : /* NOWRAP only extends to operations in the outermost type so
14190 : make sure to strip it off here. */
14191 38864 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14192 77728 : TREE_OPERAND (top, 0), bottom, false);
14193 :
14194 13167 : case SAVE_EXPR:
14195 13167 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14196 :
14197 86 : case COND_EXPR:
14198 86 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14199 86 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14200 :
14201 142190 : case INTEGER_CST:
14202 142190 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14203 : return false;
14204 139471 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14205 : SIGNED);
14206 :
14207 60909 : case SSA_NAME:
14208 60909 : if (TREE_CODE (bottom) == INTEGER_CST
14209 57694 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14210 118603 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14211 : {
14212 23986 : enum tree_code code = gimple_assign_rhs_code (stmt);
14213 :
14214 : /* Check for special cases to see if top is defined as multiple
14215 : of bottom:
14216 :
14217 : top = (X & ~(bottom - 1) ; bottom is power of 2
14218 :
14219 : or
14220 :
14221 : Y = X % bottom
14222 : top = X - Y. */
14223 23986 : if (code == BIT_AND_EXPR
14224 574 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14225 574 : && TREE_CODE (op2) == INTEGER_CST
14226 466 : && integer_pow2p (bottom)
14227 24452 : && wi::multiple_of_p (wi::to_widest (op2),
14228 466 : wi::to_widest (bottom), SIGNED))
14229 457 : return true;
14230 :
14231 23529 : op1 = gimple_assign_rhs1 (stmt);
14232 23529 : if (code == MINUS_EXPR
14233 1874 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14234 1874 : && TREE_CODE (op2) == SSA_NAME
14235 1874 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14236 1874 : && gimple_code (stmt) == GIMPLE_ASSIGN
14237 1514 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14238 64 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14239 23593 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14240 : return true;
14241 : }
14242 :
14243 : /* fall through */
14244 :
14245 : default:
14246 : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14247 : return multiple_p (wi::to_poly_widest (top),
14248 : wi::to_poly_widest (bottom));
14249 :
14250 : return false;
14251 : }
14252 : }
14253 :
14254 : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14255 : This function returns true for integer expressions, and returns
14256 : false if uncertain. */
14257 :
14258 : bool
14259 640843 : tree_expr_finite_p (const_tree x)
14260 : {
14261 640847 : machine_mode mode = element_mode (x);
14262 640847 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14263 : return true;
14264 640545 : switch (TREE_CODE (x))
14265 : {
14266 760 : case REAL_CST:
14267 760 : return real_isfinite (TREE_REAL_CST_PTR (x));
14268 0 : case COMPLEX_CST:
14269 0 : return tree_expr_finite_p (TREE_REALPART (x))
14270 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14271 : case FLOAT_EXPR:
14272 : return true;
14273 4 : case ABS_EXPR:
14274 4 : case CONVERT_EXPR:
14275 4 : case NON_LVALUE_EXPR:
14276 4 : case NEGATE_EXPR:
14277 4 : case SAVE_EXPR:
14278 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14279 0 : case MIN_EXPR:
14280 0 : case MAX_EXPR:
14281 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14282 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14283 0 : case COND_EXPR:
14284 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14285 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14286 38 : case CALL_EXPR:
14287 38 : switch (get_call_combined_fn (x))
14288 : {
14289 0 : CASE_CFN_FABS:
14290 0 : CASE_CFN_FABS_FN:
14291 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14292 0 : CASE_CFN_FMAX:
14293 0 : CASE_CFN_FMAX_FN:
14294 0 : CASE_CFN_FMIN:
14295 0 : CASE_CFN_FMIN_FN:
14296 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14297 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14298 : default:
14299 : return false;
14300 : }
14301 :
14302 : default:
14303 : return false;
14304 : }
14305 : }
14306 :
14307 : /* Return true if expression X evaluates to an infinity.
14308 : This function returns false for integer expressions. */
14309 :
14310 : bool
14311 1335980 : tree_expr_infinite_p (const_tree x)
14312 : {
14313 1336430 : if (!HONOR_INFINITIES (x))
14314 : return false;
14315 1336185 : switch (TREE_CODE (x))
14316 : {
14317 0 : case REAL_CST:
14318 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14319 450 : case ABS_EXPR:
14320 450 : case NEGATE_EXPR:
14321 450 : case NON_LVALUE_EXPR:
14322 450 : case SAVE_EXPR:
14323 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14324 0 : case COND_EXPR:
14325 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14326 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14327 : default:
14328 : return false;
14329 : }
14330 : }
14331 :
14332 : /* Return true if expression X could evaluate to an infinity.
14333 : This function returns false for integer expressions, and returns
14334 : true if uncertain. */
14335 :
14336 : bool
14337 815661 : tree_expr_maybe_infinite_p (const_tree x)
14338 : {
14339 815669 : if (!HONOR_INFINITIES (x))
14340 : return false;
14341 815213 : switch (TREE_CODE (x))
14342 : {
14343 301 : case REAL_CST:
14344 301 : return real_isinf (TREE_REAL_CST_PTR (x));
14345 : case FLOAT_EXPR:
14346 : return false;
14347 8 : case ABS_EXPR:
14348 8 : case NEGATE_EXPR:
14349 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14350 1 : case COND_EXPR:
14351 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14352 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14353 : default:
14354 : return true;
14355 : }
14356 : }
14357 :
14358 : /* Return true if expression X evaluates to a signaling NaN.
14359 : This function returns false for integer expressions. */
14360 :
14361 : bool
14362 429 : tree_expr_signaling_nan_p (const_tree x)
14363 : {
14364 429 : if (!HONOR_SNANS (x))
14365 : return false;
14366 124 : switch (TREE_CODE (x))
14367 : {
14368 124 : case REAL_CST:
14369 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14370 0 : case NON_LVALUE_EXPR:
14371 0 : case SAVE_EXPR:
14372 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14373 0 : case COND_EXPR:
14374 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14375 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14376 : default:
14377 : return false;
14378 : }
14379 : }
14380 :
14381 : /* Return true if expression X could evaluate to a signaling NaN.
14382 : This function returns false for integer expressions, and returns
14383 : true if uncertain. */
14384 :
14385 : bool
14386 721161 : tree_expr_maybe_signaling_nan_p (const_tree x)
14387 : {
14388 721161 : if (!HONOR_SNANS (x))
14389 : return false;
14390 4892 : switch (TREE_CODE (x))
14391 : {
14392 1452 : case REAL_CST:
14393 1452 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14394 : case FLOAT_EXPR:
14395 : return false;
14396 0 : case ABS_EXPR:
14397 0 : case CONVERT_EXPR:
14398 0 : case NEGATE_EXPR:
14399 0 : case NON_LVALUE_EXPR:
14400 0 : case SAVE_EXPR:
14401 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14402 0 : case MIN_EXPR:
14403 0 : case MAX_EXPR:
14404 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14405 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14406 0 : case COND_EXPR:
14407 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14408 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14409 0 : case CALL_EXPR:
14410 0 : switch (get_call_combined_fn (x))
14411 : {
14412 0 : CASE_CFN_FABS:
14413 0 : CASE_CFN_FABS_FN:
14414 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14415 0 : CASE_CFN_FMAX:
14416 0 : CASE_CFN_FMAX_FN:
14417 0 : CASE_CFN_FMIN:
14418 0 : CASE_CFN_FMIN_FN:
14419 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14420 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14421 : default:
14422 : return true;
14423 : }
14424 : default:
14425 : return true;
14426 : }
14427 : }
14428 :
14429 : /* Return true if expression X evaluates to a NaN.
14430 : This function returns false for integer expressions. */
14431 :
14432 : bool
14433 3939686 : tree_expr_nan_p (const_tree x)
14434 : {
14435 4295688 : if (!HONOR_NANS (x))
14436 : return false;
14437 4295338 : switch (TREE_CODE (x))
14438 : {
14439 3806 : case REAL_CST:
14440 3806 : return real_isnan (TREE_REAL_CST_PTR (x));
14441 356002 : case NON_LVALUE_EXPR:
14442 356002 : case SAVE_EXPR:
14443 356002 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14444 956 : case COND_EXPR:
14445 956 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14446 956 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14447 : default:
14448 : return false;
14449 : }
14450 : }
14451 :
14452 : /* Return true if expression X could evaluate to a NaN.
14453 : This function returns false for integer expressions, and returns
14454 : true if uncertain. */
14455 :
14456 : bool
14457 5192174 : tree_expr_maybe_nan_p (const_tree x)
14458 : {
14459 7586987 : if (!HONOR_NANS (x))
14460 : return false;
14461 7479647 : switch (TREE_CODE (x))
14462 : {
14463 3454 : case REAL_CST:
14464 3454 : return real_isnan (TREE_REAL_CST_PTR (x));
14465 : case FLOAT_EXPR:
14466 : return false;
14467 14832 : case PLUS_EXPR:
14468 14832 : case MINUS_EXPR:
14469 14832 : case MULT_EXPR:
14470 14832 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14471 14832 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14472 2394813 : case ABS_EXPR:
14473 2394813 : case CONVERT_EXPR:
14474 2394813 : case NEGATE_EXPR:
14475 2394813 : case NON_LVALUE_EXPR:
14476 2394813 : case SAVE_EXPR:
14477 2394813 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14478 176 : case MIN_EXPR:
14479 176 : case MAX_EXPR:
14480 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14481 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14482 603 : case COND_EXPR:
14483 603 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14484 603 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14485 1085 : case CALL_EXPR:
14486 1085 : switch (get_call_combined_fn (x))
14487 : {
14488 0 : CASE_CFN_FABS:
14489 0 : CASE_CFN_FABS_FN:
14490 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14491 108 : CASE_CFN_FMAX:
14492 108 : CASE_CFN_FMAX_FN:
14493 108 : CASE_CFN_FMIN:
14494 108 : CASE_CFN_FMIN_FN:
14495 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14496 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14497 : default:
14498 : return true;
14499 : }
14500 : default:
14501 : return true;
14502 : }
14503 : }
14504 :
14505 : /* Return true if expression X could evaluate to -0.0.
14506 : This function returns true if uncertain. */
14507 :
14508 : bool
14509 594266 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14510 : {
14511 594266 : if (!HONOR_SIGNED_ZEROS (x))
14512 : return false;
14513 594266 : switch (TREE_CODE (x))
14514 : {
14515 0 : case REAL_CST:
14516 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14517 : case INTEGER_CST:
14518 : case FLOAT_EXPR:
14519 : case ABS_EXPR:
14520 : return false;
14521 0 : case NON_LVALUE_EXPR:
14522 0 : case SAVE_EXPR:
14523 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14524 0 : case COND_EXPR:
14525 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14526 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14527 2 : case CALL_EXPR:
14528 2 : switch (get_call_combined_fn (x))
14529 : {
14530 : CASE_CFN_FABS:
14531 : CASE_CFN_FABS_FN:
14532 : return false;
14533 : default:
14534 : break;
14535 : }
14536 : default:
14537 : break;
14538 : }
14539 : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14540 : * but currently those predicates require tree and not const_tree. */
14541 : return true;
14542 : }
14543 :
14544 : #define tree_expr_nonnegative_p(X, Y) \
14545 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14546 :
14547 : #define RECURSE(X) \
14548 : ((tree_expr_nonnegative_p) (X, depth + 1))
14549 :
14550 : /* Return true if CODE or TYPE is known to be non-negative. */
14551 :
14552 : static bool
14553 26864968 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14554 : {
14555 26864968 : if (!VECTOR_TYPE_P (type)
14556 26828043 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14557 53692731 : && truth_value_p (code))
14558 : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14559 : have a signed:1 type (where the value is -1 and 0). */
14560 : return true;
14561 : return false;
14562 : }
14563 :
14564 : /* Return true if (CODE OP0) is known to be non-negative.
14565 : DEPTH is the current nesting depth of the query. */
14566 :
14567 : bool
14568 2434006 : tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth)
14569 : {
14570 2434006 : if (TYPE_UNSIGNED (type))
14571 : return true;
14572 :
14573 1973808 : switch (code)
14574 : {
14575 284908 : case ABS_EXPR:
14576 : /* We can't return 1 if flag_wrapv is set because
14577 : ABS_EXPR<INT_MIN> = INT_MIN. */
14578 284908 : if (!ANY_INTEGRAL_TYPE_P (type))
14579 : return true;
14580 1172 : if (TYPE_OVERFLOW_UNDEFINED (type))
14581 : return true;
14582 : break;
14583 :
14584 74843 : case NON_LVALUE_EXPR:
14585 74843 : case FLOAT_EXPR:
14586 74843 : case FIX_TRUNC_EXPR:
14587 74843 : return RECURSE (op0);
14588 :
14589 1574399 : CASE_CONVERT:
14590 1574399 : {
14591 1574399 : tree inner_type = TREE_TYPE (op0);
14592 1574399 : tree outer_type = type;
14593 :
14594 1574399 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14595 : {
14596 408362 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14597 408362 : return RECURSE (op0);
14598 0 : if (INTEGRAL_TYPE_P (inner_type))
14599 : {
14600 0 : if (TYPE_UNSIGNED (inner_type))
14601 : return true;
14602 0 : return RECURSE (op0);
14603 : }
14604 : }
14605 1166037 : else if (INTEGRAL_TYPE_P (outer_type))
14606 : {
14607 1165960 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14608 0 : return RECURSE (op0);
14609 1165960 : if (INTEGRAL_TYPE_P (inner_type))
14610 1159678 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14611 1159678 : && TYPE_UNSIGNED (inner_type);
14612 : }
14613 : }
14614 : break;
14615 :
14616 39658 : default:
14617 39658 : return tree_simple_nonnegative_warnv_p (code, type);
14618 : }
14619 :
14620 : /* We don't know sign of `t', so be conservative and return false. */
14621 : return false;
14622 : }
14623 :
14624 : /* Return true if (CODE OP0 OP1) is known to be non-negative.
14625 : DEPTH is the current nesting depth of the query. */
14626 :
14627 : bool
14628 4857002 : tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0,
14629 : tree op1, int depth)
14630 : {
14631 4857002 : if (TYPE_UNSIGNED (type))
14632 : return true;
14633 :
14634 4553125 : switch (code)
14635 : {
14636 1107101 : case POINTER_PLUS_EXPR:
14637 1107101 : case PLUS_EXPR:
14638 1107101 : if (FLOAT_TYPE_P (type))
14639 48458 : return RECURSE (op0) && RECURSE (op1);
14640 :
14641 : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14642 : both unsigned and at least 2 bits shorter than the result. */
14643 1058643 : if (TREE_CODE (type) == INTEGER_TYPE
14644 1052615 : && TREE_CODE (op0) == NOP_EXPR
14645 16227 : && TREE_CODE (op1) == NOP_EXPR)
14646 : {
14647 201 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14648 201 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14649 201 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14650 302 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14651 : {
14652 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14653 95 : TYPE_PRECISION (inner2)) + 1;
14654 95 : return prec < TYPE_PRECISION (type);
14655 : }
14656 : }
14657 : break;
14658 :
14659 163919 : case MULT_EXPR:
14660 163919 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14661 : {
14662 : /* x * x is always non-negative for floating point x
14663 : or without overflow. */
14664 148248 : if (operand_equal_p (op0, op1, 0)
14665 148248 : || (RECURSE (op0) && RECURSE (op1)))
14666 : return true;
14667 : }
14668 :
14669 : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14670 : both unsigned and their total bits is shorter than the result. */
14671 161768 : if (TREE_CODE (type) == INTEGER_TYPE
14672 89415 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14673 161 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14674 : {
14675 147 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14676 147 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14677 147 : : TREE_TYPE (op0);
14678 147 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14679 147 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14680 147 : : TREE_TYPE (op1);
14681 :
14682 147 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14683 147 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14684 :
14685 147 : if (TREE_CODE (op0) == INTEGER_CST)
14686 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14687 :
14688 147 : if (TREE_CODE (op1) == INTEGER_CST)
14689 70 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14690 :
14691 147 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14692 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14693 : {
14694 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14695 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14696 0 : : TYPE_PRECISION (inner0);
14697 :
14698 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14699 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14700 0 : : TYPE_PRECISION (inner1);
14701 :
14702 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14703 : }
14704 : }
14705 : return false;
14706 :
14707 9568 : case BIT_AND_EXPR:
14708 9568 : return RECURSE (op0) || RECURSE (op1);
14709 :
14710 47686 : case MAX_EXPR:
14711 : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14712 : things. */
14713 47686 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14714 76 : return RECURSE (op0) && RECURSE (op1);
14715 47610 : return RECURSE (op0) || RECURSE (op1);
14716 :
14717 156393 : case BIT_IOR_EXPR:
14718 156393 : case BIT_XOR_EXPR:
14719 156393 : case MIN_EXPR:
14720 156393 : case RDIV_EXPR:
14721 156393 : case TRUNC_DIV_EXPR:
14722 156393 : case CEIL_DIV_EXPR:
14723 156393 : case FLOOR_DIV_EXPR:
14724 156393 : case ROUND_DIV_EXPR:
14725 156393 : return RECURSE (op0) && RECURSE (op1);
14726 :
14727 54078 : case TRUNC_MOD_EXPR:
14728 54078 : return RECURSE (op0);
14729 :
14730 230 : case FLOOR_MOD_EXPR:
14731 230 : return RECURSE (op1);
14732 :
14733 3014150 : case CEIL_MOD_EXPR:
14734 3014150 : case ROUND_MOD_EXPR:
14735 3014150 : default:
14736 3014150 : return tree_simple_nonnegative_warnv_p (code, type);
14737 : }
14738 :
14739 : /* We don't know sign of `t', so be conservative and return false. */
14740 : return false;
14741 : }
14742 :
14743 : /* Return true if T is known to be non-negative.
14744 : DEPTH is the current nesting depth of the query. */
14745 :
14746 : bool
14747 25912101 : tree_single_nonnegative_p (tree t, int depth)
14748 : {
14749 25912101 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14750 : return true;
14751 :
14752 21849015 : switch (TREE_CODE (t))
14753 : {
14754 2454241 : case INTEGER_CST:
14755 2454241 : return tree_int_cst_sgn (t) >= 0;
14756 :
14757 930018 : case REAL_CST:
14758 930018 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14759 :
14760 0 : case FIXED_CST:
14761 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14762 :
14763 928 : case COND_EXPR:
14764 928 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14765 :
14766 8848977 : case SSA_NAME:
14767 : /* For integral types, query the range if possible. */
14768 8848977 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14769 : {
14770 7364481 : int_range_max r;
14771 14728962 : get_range_query (cfun)->range_of_expr (r, t);
14772 7364481 : if (!r.undefined_p () && !r.varying_p())
14773 : {
14774 1772692 : if (r.nonnegative_p ())
14775 : return true;
14776 1173604 : if (r.nonpositive_p () && !range_includes_zero_p (r))
14777 : return false;
14778 : }
14779 7364481 : }
14780 : /* Limit the depth of recursion to avoid quadratic behavior.
14781 : This is expected to catch almost all occurrences in practice.
14782 : If this code misses important cases that unbounded recursion
14783 : would not, passes that need this information could be revised
14784 : to provide it through dataflow propagation. */
14785 8246230 : return (!name_registered_for_update_p (t)
14786 8246229 : && depth < param_max_ssa_name_query_depth
14787 16355073 : && gimple_stmt_nonnegative_p (SSA_NAME_DEF_STMT (t), depth));
14788 :
14789 9614851 : default:
14790 9614851 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14791 : }
14792 : }
14793 :
14794 : /* Return true if T is known to be non-negative.
14795 : DEPTH is the current nesting depth of the query. */
14796 :
14797 : bool
14798 14236277 : tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1,
14799 : int depth)
14800 : {
14801 14236277 : switch (fn)
14802 : {
14803 : CASE_CFN_ACOS:
14804 : CASE_CFN_ACOS_FN:
14805 : CASE_CFN_ACOSH:
14806 : CASE_CFN_ACOSH_FN:
14807 : CASE_CFN_ACOSPI:
14808 : CASE_CFN_ACOSPI_FN:
14809 : CASE_CFN_CABS:
14810 : CASE_CFN_CABS_FN:
14811 : CASE_CFN_COSH:
14812 : CASE_CFN_COSH_FN:
14813 : CASE_CFN_ERFC:
14814 : CASE_CFN_ERFC_FN:
14815 : CASE_CFN_EXP:
14816 : CASE_CFN_EXP_FN:
14817 : CASE_CFN_EXP10:
14818 : CASE_CFN_EXP2:
14819 : CASE_CFN_EXP2_FN:
14820 : CASE_CFN_FABS:
14821 : CASE_CFN_FABS_FN:
14822 : CASE_CFN_FDIM:
14823 : CASE_CFN_FDIM_FN:
14824 : CASE_CFN_HYPOT:
14825 : CASE_CFN_HYPOT_FN:
14826 : CASE_CFN_POW10:
14827 : CASE_CFN_FFS:
14828 : CASE_CFN_PARITY:
14829 : CASE_CFN_POPCOUNT:
14830 : CASE_CFN_CLRSB:
14831 : CASE_CFN_BSWAP:
14832 : CASE_CFN_BITREVERSE:
14833 : /* Always true. */
14834 : return true;
14835 :
14836 176 : CASE_CFN_CLZ:
14837 176 : CASE_CFN_CTZ:
14838 176 : if (arg1)
14839 2 : return RECURSE (arg1);
14840 : return true;
14841 :
14842 969 : CASE_CFN_SQRT:
14843 969 : CASE_CFN_SQRT_FN:
14844 : /* sqrt(-0.0) is -0.0. */
14845 969 : if (!HONOR_SIGNED_ZEROS (type))
14846 : return true;
14847 937 : return RECURSE (arg0);
14848 :
14849 23109 : CASE_CFN_ASINH:
14850 23109 : CASE_CFN_ASINH_FN:
14851 23109 : CASE_CFN_ASINPI:
14852 23109 : CASE_CFN_ASINPI_FN:
14853 23109 : CASE_CFN_ATAN:
14854 23109 : CASE_CFN_ATAN_FN:
14855 23109 : CASE_CFN_ATANH:
14856 23109 : CASE_CFN_ATANH_FN:
14857 23109 : CASE_CFN_ATANPI:
14858 23109 : CASE_CFN_ATANPI_FN:
14859 23109 : CASE_CFN_CBRT:
14860 23109 : CASE_CFN_CBRT_FN:
14861 23109 : CASE_CFN_CEIL:
14862 23109 : CASE_CFN_CEIL_FN:
14863 23109 : CASE_CFN_ERF:
14864 23109 : CASE_CFN_ERF_FN:
14865 23109 : CASE_CFN_EXPM1:
14866 23109 : CASE_CFN_EXPM1_FN:
14867 23109 : CASE_CFN_FLOOR:
14868 23109 : CASE_CFN_FLOOR_FN:
14869 23109 : CASE_CFN_FMOD:
14870 23109 : CASE_CFN_FMOD_FN:
14871 23109 : CASE_CFN_FREXP:
14872 23109 : CASE_CFN_FREXP_FN:
14873 23109 : CASE_CFN_ICEIL:
14874 23109 : CASE_CFN_IFLOOR:
14875 23109 : CASE_CFN_IRINT:
14876 23109 : CASE_CFN_IROUND:
14877 23109 : CASE_CFN_LCEIL:
14878 23109 : CASE_CFN_LDEXP:
14879 23109 : CASE_CFN_LFLOOR:
14880 23109 : CASE_CFN_LLCEIL:
14881 23109 : CASE_CFN_LLFLOOR:
14882 23109 : CASE_CFN_LLRINT:
14883 23109 : CASE_CFN_LLRINT_FN:
14884 23109 : CASE_CFN_LLROUND:
14885 23109 : CASE_CFN_LLROUND_FN:
14886 23109 : CASE_CFN_LRINT:
14887 23109 : CASE_CFN_LRINT_FN:
14888 23109 : CASE_CFN_LROUND:
14889 23109 : CASE_CFN_LROUND_FN:
14890 23109 : CASE_CFN_MODF:
14891 23109 : CASE_CFN_MODF_FN:
14892 23109 : CASE_CFN_NEARBYINT:
14893 23109 : CASE_CFN_NEARBYINT_FN:
14894 23109 : CASE_CFN_RINT:
14895 23109 : CASE_CFN_RINT_FN:
14896 23109 : CASE_CFN_ROUND:
14897 23109 : CASE_CFN_ROUND_FN:
14898 23109 : CASE_CFN_ROUNDEVEN:
14899 23109 : CASE_CFN_ROUNDEVEN_FN:
14900 23109 : CASE_CFN_SCALB:
14901 23109 : CASE_CFN_SCALBLN:
14902 23109 : CASE_CFN_SCALBLN_FN:
14903 23109 : CASE_CFN_SCALBN:
14904 23109 : CASE_CFN_SCALBN_FN:
14905 23109 : CASE_CFN_SIGNBIT:
14906 23109 : CASE_CFN_SIGNIFICAND:
14907 23109 : CASE_CFN_SINH:
14908 23109 : CASE_CFN_SINH_FN:
14909 23109 : CASE_CFN_TANH:
14910 23109 : CASE_CFN_TANH_FN:
14911 23109 : CASE_CFN_TRUNC:
14912 23109 : CASE_CFN_TRUNC_FN:
14913 : /* True if the 1st argument is nonnegative. */
14914 23109 : return RECURSE (arg0);
14915 :
14916 1319 : CASE_CFN_FMAX:
14917 1319 : CASE_CFN_FMAX_FN:
14918 : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14919 : things. In the presence of sNaNs, we're only guaranteed to be
14920 : non-negative if both operands are non-negative. In the presence
14921 : of qNaNs, we're non-negative if either operand is non-negative
14922 : and can't be a qNaN, or if both operands are non-negative. */
14923 1319 : if (tree_expr_maybe_signaling_nan_p (arg0)
14924 1319 : || tree_expr_maybe_signaling_nan_p (arg1))
14925 136 : return RECURSE (arg0) && RECURSE (arg1);
14926 1183 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14927 332 : || RECURSE (arg1))
14928 851 : : (RECURSE (arg1)
14929 851 : && !tree_expr_maybe_nan_p (arg1));
14930 :
14931 910 : CASE_CFN_FMIN:
14932 910 : CASE_CFN_FMIN_FN:
14933 : /* True if the 1st AND 2nd arguments are nonnegative. */
14934 910 : return RECURSE (arg0) && RECURSE (arg1);
14935 :
14936 807 : CASE_CFN_COPYSIGN:
14937 807 : CASE_CFN_COPYSIGN_FN:
14938 : /* True if the 2nd argument is nonnegative. */
14939 807 : return RECURSE (arg1);
14940 :
14941 2312 : CASE_CFN_POWI:
14942 : /* True if the 1st argument is nonnegative or the second
14943 : argument is an even integer. */
14944 2312 : if (TREE_CODE (arg1) == INTEGER_CST
14945 2312 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14946 : return true;
14947 2229 : return RECURSE (arg0);
14948 :
14949 4927 : CASE_CFN_POW:
14950 4927 : CASE_CFN_POW_FN:
14951 : /* True if the 1st argument is nonnegative or the second
14952 : argument is an even integer valued real. */
14953 4927 : if (TREE_CODE (arg1) == REAL_CST)
14954 : {
14955 2202 : REAL_VALUE_TYPE c;
14956 2202 : HOST_WIDE_INT n;
14957 :
14958 2202 : c = TREE_REAL_CST (arg1);
14959 2202 : n = real_to_integer (&c);
14960 2202 : if ((n & 1) == 0)
14961 : {
14962 1573 : REAL_VALUE_TYPE cint;
14963 1573 : real_from_integer (&cint, VOIDmode, n, SIGNED);
14964 1573 : if (real_identical (&c, &cint))
14965 568 : return true;
14966 : }
14967 : }
14968 4359 : return RECURSE (arg0);
14969 :
14970 14193535 : default:
14971 14193535 : break;
14972 : }
14973 14193535 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14974 : }
14975 :
14976 : /* Return true if T is known to be non-negative.
14977 : DEPTH is the current nesting depth of the query. */
14978 :
14979 : static bool
14980 1738639 : tree_invalid_nonnegative_p (tree t, int depth)
14981 : {
14982 1738639 : enum tree_code code = TREE_CODE (t);
14983 1738639 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14984 : return true;
14985 :
14986 1321180 : switch (code)
14987 : {
14988 268 : case TARGET_EXPR:
14989 268 : {
14990 268 : tree temp = TARGET_EXPR_SLOT (t);
14991 268 : t = TARGET_EXPR_INITIAL (t);
14992 :
14993 : /* If the initializer is non-void, then it's a normal expression
14994 : that will be assigned to the slot. */
14995 268 : if (!VOID_TYPE_P (TREE_TYPE (t)))
14996 66 : return RECURSE (t);
14997 :
14998 : /* Otherwise, the initializer sets the slot in some way. One common
14999 : way is an assignment statement at the end of the initializer. */
15000 404 : while (1)
15001 : {
15002 404 : if (TREE_CODE (t) == BIND_EXPR)
15003 202 : t = expr_last (BIND_EXPR_BODY (t));
15004 202 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15005 202 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15006 0 : t = expr_last (TREE_OPERAND (t, 0));
15007 202 : else if (TREE_CODE (t) == STATEMENT_LIST)
15008 0 : t = expr_last (t);
15009 : else
15010 : break;
15011 : }
15012 202 : if (TREE_CODE (t) == MODIFY_EXPR
15013 202 : && TREE_OPERAND (t, 0) == temp)
15014 202 : return RECURSE (TREE_OPERAND (t, 1));
15015 :
15016 : return false;
15017 : }
15018 :
15019 643528 : case CALL_EXPR:
15020 643528 : {
15021 643528 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15022 643528 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15023 :
15024 643528 : return tree_call_nonnegative_p (TREE_TYPE (t),
15025 : get_call_combined_fn (t),
15026 : arg0,
15027 : arg1,
15028 643528 : depth);
15029 : }
15030 3387 : case COMPOUND_EXPR:
15031 3387 : case MODIFY_EXPR:
15032 3387 : return RECURSE (TREE_OPERAND (t, 1));
15033 :
15034 15 : case BIND_EXPR:
15035 15 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15036 :
15037 671208 : case SAVE_EXPR:
15038 671208 : return RECURSE (TREE_OPERAND (t, 0));
15039 :
15040 2774 : default:
15041 2774 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15042 : }
15043 : }
15044 :
15045 : #undef RECURSE
15046 : #undef tree_expr_nonnegative_p
15047 :
15048 : /* Return true if T is known to be non-negative.
15049 : DEPTH is the current nesting depth of the query. */
15050 :
15051 : bool
15052 27356656 : tree_expr_nonnegative_p (tree t, int depth)
15053 : {
15054 27356656 : enum tree_code code;
15055 27356656 : if (error_operand_p (t))
15056 : return false;
15057 :
15058 27356655 : code = TREE_CODE (t);
15059 27356655 : switch (TREE_CODE_CLASS (code))
15060 : {
15061 1376489 : case tcc_binary:
15062 1376489 : case tcc_comparison:
15063 1376489 : return tree_binary_nonnegative_p (TREE_CODE (t),
15064 1376489 : TREE_TYPE (t),
15065 1376489 : TREE_OPERAND (t, 0),
15066 1376489 : TREE_OPERAND (t, 1),
15067 1376489 : depth);
15068 :
15069 1978850 : case tcc_unary:
15070 1978850 : return tree_unary_nonnegative_p (TREE_CODE (t),
15071 1978850 : TREE_TYPE (t),
15072 1978850 : TREE_OPERAND (t, 0),
15073 1978850 : depth);
15074 :
15075 13564964 : case tcc_constant:
15076 13564964 : case tcc_declaration:
15077 13564964 : case tcc_reference:
15078 13564964 : return tree_single_nonnegative_p (t, depth);
15079 :
15080 10436352 : default:
15081 10436352 : break;
15082 : }
15083 :
15084 10436352 : switch (code)
15085 : {
15086 7 : case TRUTH_AND_EXPR:
15087 7 : case TRUTH_OR_EXPR:
15088 7 : case TRUTH_XOR_EXPR:
15089 7 : return tree_binary_nonnegative_p (TREE_CODE (t),
15090 7 : TREE_TYPE (t),
15091 7 : TREE_OPERAND (t, 0),
15092 7 : TREE_OPERAND (t, 1),
15093 7 : depth);
15094 72 : case TRUTH_NOT_EXPR:
15095 72 : return tree_unary_nonnegative_p (TREE_CODE (t),
15096 72 : TREE_TYPE (t),
15097 72 : TREE_OPERAND (t, 0),
15098 72 : depth);
15099 :
15100 8697634 : case COND_EXPR:
15101 8697634 : case CONSTRUCTOR:
15102 8697634 : case OBJ_TYPE_REF:
15103 8697634 : case ADDR_EXPR:
15104 8697634 : case WITH_SIZE_EXPR:
15105 8697634 : case SSA_NAME:
15106 8697634 : return tree_single_nonnegative_p (t, depth);
15107 :
15108 1738639 : default:
15109 1738639 : return tree_invalid_nonnegative_p (t, depth);
15110 : }
15111 : }
15112 :
15113 :
15114 : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15115 : For floating point we further ensure that T is not denormal.
15116 : Similar logic is present in nonzero_address in rtlanal.h. */
15117 :
15118 : bool
15119 1601564 : tree_unary_nonzero_p (enum tree_code code, tree type, tree op0)
15120 : {
15121 1601564 : switch (code)
15122 : {
15123 1 : case ABS_EXPR:
15124 1 : return tree_expr_nonzero_p (op0);
15125 :
15126 924561 : case NOP_EXPR:
15127 924561 : {
15128 924561 : tree inner_type = TREE_TYPE (op0);
15129 924561 : tree outer_type = type;
15130 :
15131 924561 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15132 924561 : && tree_expr_nonzero_p (op0));
15133 : }
15134 28119 : break;
15135 :
15136 28119 : case NON_LVALUE_EXPR:
15137 28119 : return tree_expr_nonzero_p (op0);
15138 :
15139 : default:
15140 : break;
15141 : }
15142 :
15143 : return false;
15144 : }
15145 :
15146 : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15147 : For floating point we further ensure that T is not denormal.
15148 : Similar logic is present in nonzero_address in rtlanal.h. */
15149 :
15150 : bool
15151 2992424 : tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1)
15152 : {
15153 2992424 : switch (code)
15154 : {
15155 469060 : case POINTER_PLUS_EXPR:
15156 469060 : case PLUS_EXPR:
15157 469060 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15158 : {
15159 : /* With the presence of negative values it is hard
15160 : to say something. */
15161 110652 : if (!tree_expr_nonnegative_p (op0)
15162 110652 : || !tree_expr_nonnegative_p (op1))
15163 : return false;
15164 : /* One of operands must be positive and the other non-negative. */
15165 3519 : return (tree_expr_nonzero_p (op0)
15166 3519 : || tree_expr_nonzero_p (op1));
15167 : }
15168 : break;
15169 :
15170 31962 : case MULT_EXPR:
15171 31962 : if (TYPE_OVERFLOW_UNDEFINED (type))
15172 : {
15173 559 : if (tree_expr_nonzero_p (op0)
15174 559 : && tree_expr_nonzero_p (op1))
15175 : return true;
15176 : }
15177 : break;
15178 :
15179 : case MIN_EXPR:
15180 : break;
15181 :
15182 43 : case MAX_EXPR:
15183 43 : if (tree_expr_nonzero_p (op0))
15184 : {
15185 :
15186 : /* When both operands are nonzero, then MAX must be too. */
15187 0 : if (tree_expr_nonzero_p (op1))
15188 : return true;
15189 :
15190 : /* MAX where operand 0 is positive is positive. */
15191 0 : return tree_expr_nonnegative_p (op0);
15192 : }
15193 : /* MAX where operand 1 is positive is positive. */
15194 43 : else if (tree_expr_nonzero_p (op1)
15195 43 : && tree_expr_nonnegative_p (op1))
15196 : return true;
15197 : break;
15198 :
15199 269509 : case BIT_IOR_EXPR:
15200 269509 : return (tree_expr_nonzero_p (op1)
15201 269509 : || tree_expr_nonzero_p (op0));
15202 :
15203 : default:
15204 : break;
15205 : }
15206 :
15207 : return false;
15208 : }
15209 :
15210 : /* Return true when T is an address and is known to be nonzero.
15211 : For floating point we further ensure that T is not denormal.
15212 : Similar logic is present in nonzero_address in rtlanal.h. */
15213 :
15214 : bool
15215 155362206 : tree_single_nonzero_p (tree t)
15216 : {
15217 155362206 : switch (TREE_CODE (t))
15218 : {
15219 1150386 : case INTEGER_CST:
15220 1150386 : return !integer_zerop (t);
15221 :
15222 12345547 : case ADDR_EXPR:
15223 12345547 : {
15224 12345547 : tree base = TREE_OPERAND (t, 0);
15225 :
15226 12345547 : if (!DECL_P (base))
15227 6119819 : base = get_base_address (base);
15228 :
15229 12345547 : if (base && TREE_CODE (base) == TARGET_EXPR)
15230 844 : base = TARGET_EXPR_SLOT (base);
15231 :
15232 844 : if (!base)
15233 : return false;
15234 :
15235 : /* For objects in symbol table check if we know they are non-zero.
15236 : Don't do anything for variables and functions before symtab is built;
15237 : it is quite possible that they will be declared weak later. */
15238 12345547 : int nonzero_addr = maybe_nonzero_address (base);
15239 12345547 : if (nonzero_addr >= 0)
15240 10031448 : return nonzero_addr;
15241 :
15242 : /* Constants are never weak. */
15243 2314099 : if (CONSTANT_CLASS_P (base))
15244 2219376 : return true;
15245 :
15246 : return false;
15247 : }
15248 :
15249 38266 : case COND_EXPR:
15250 38266 : if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
15251 38266 : && tree_expr_nonzero_p (TREE_OPERAND (t, 2)))
15252 : return true;
15253 : break;
15254 :
15255 129656536 : case SSA_NAME:
15256 129656536 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15257 : break;
15258 100879194 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15259 :
15260 : default:
15261 : break;
15262 : }
15263 : return false;
15264 : }
15265 :
15266 : #define integer_valued_real_p(X) \
15267 : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15268 :
15269 : #define RECURSE(X) \
15270 : ((integer_valued_real_p) (X, depth + 1))
15271 :
15272 : /* Return true if the floating point result of (CODE OP0) has an
15273 : integer value. We also allow +Inf, -Inf and NaN to be considered
15274 : integer values. Return false for signaling NaN.
15275 :
15276 : DEPTH is the current nesting depth of the query. */
15277 :
15278 : bool
15279 15209 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15280 : {
15281 15209 : switch (code)
15282 : {
15283 : case FLOAT_EXPR:
15284 : return true;
15285 :
15286 1400 : case ABS_EXPR:
15287 1400 : return RECURSE (op0);
15288 :
15289 9859 : CASE_CONVERT:
15290 9859 : {
15291 9859 : tree type = TREE_TYPE (op0);
15292 9859 : if (TREE_CODE (type) == INTEGER_TYPE)
15293 : return true;
15294 9859 : if (SCALAR_FLOAT_TYPE_P (type))
15295 9859 : return RECURSE (op0);
15296 : break;
15297 : }
15298 :
15299 : default:
15300 : break;
15301 : }
15302 : return false;
15303 : }
15304 :
15305 : /* Return true if the floating point result of (CODE OP0 OP1) has an
15306 : integer value. We also allow +Inf, -Inf and NaN to be considered
15307 : integer values. Return false for signaling NaN.
15308 :
15309 : DEPTH is the current nesting depth of the query. */
15310 :
15311 : bool
15312 13863 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15313 : {
15314 13863 : switch (code)
15315 : {
15316 8070 : case PLUS_EXPR:
15317 8070 : case MINUS_EXPR:
15318 8070 : case MULT_EXPR:
15319 8070 : case MIN_EXPR:
15320 8070 : case MAX_EXPR:
15321 8070 : return RECURSE (op0) && RECURSE (op1);
15322 :
15323 : default:
15324 : break;
15325 : }
15326 : return false;
15327 : }
15328 :
15329 : /* Return true if the floating point result of calling FNDECL with arguments
15330 : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15331 : considered integer values. Return false for signaling NaN. If FNDECL
15332 : takes fewer than 2 arguments, the remaining ARGn are null.
15333 :
15334 : DEPTH is the current nesting depth of the query. */
15335 :
15336 : bool
15337 1157 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15338 : {
15339 1157 : switch (fn)
15340 : {
15341 : CASE_CFN_CEIL:
15342 : CASE_CFN_CEIL_FN:
15343 : CASE_CFN_FLOOR:
15344 : CASE_CFN_FLOOR_FN:
15345 : CASE_CFN_NEARBYINT:
15346 : CASE_CFN_NEARBYINT_FN:
15347 : CASE_CFN_RINT:
15348 : CASE_CFN_RINT_FN:
15349 : CASE_CFN_ROUND:
15350 : CASE_CFN_ROUND_FN:
15351 : CASE_CFN_ROUNDEVEN:
15352 : CASE_CFN_ROUNDEVEN_FN:
15353 : CASE_CFN_TRUNC:
15354 : CASE_CFN_TRUNC_FN:
15355 : return true;
15356 :
15357 336 : CASE_CFN_FMIN:
15358 336 : CASE_CFN_FMIN_FN:
15359 336 : CASE_CFN_FMAX:
15360 336 : CASE_CFN_FMAX_FN:
15361 336 : return RECURSE (arg0) && RECURSE (arg1);
15362 :
15363 : default:
15364 : break;
15365 : }
15366 : return false;
15367 : }
15368 :
15369 : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15370 : has an integer value. We also allow +Inf, -Inf and NaN to be
15371 : considered integer values. Return false for signaling NaN.
15372 :
15373 : DEPTH is the current nesting depth of the query. */
15374 :
15375 : bool
15376 130270 : integer_valued_real_single_p (tree t, int depth)
15377 : {
15378 130270 : switch (TREE_CODE (t))
15379 : {
15380 2347 : case REAL_CST:
15381 2347 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15382 :
15383 0 : case COND_EXPR:
15384 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15385 :
15386 91424 : case SSA_NAME:
15387 : /* Limit the depth of recursion to avoid quadratic behavior.
15388 : This is expected to catch almost all occurrences in practice.
15389 : If this code misses important cases that unbounded recursion
15390 : would not, passes that need this information could be revised
15391 : to provide it through dataflow propagation. */
15392 91424 : return (!name_registered_for_update_p (t)
15393 91424 : && depth < param_max_ssa_name_query_depth
15394 182033 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15395 : depth));
15396 :
15397 : default:
15398 : break;
15399 : }
15400 : return false;
15401 : }
15402 :
15403 : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15404 : has an integer value. We also allow +Inf, -Inf and NaN to be
15405 : considered integer values. Return false for signaling NaN.
15406 :
15407 : DEPTH is the current nesting depth of the query. */
15408 :
15409 : static bool
15410 0 : integer_valued_real_invalid_p (tree t, int depth)
15411 : {
15412 0 : switch (TREE_CODE (t))
15413 : {
15414 0 : case COMPOUND_EXPR:
15415 0 : case MODIFY_EXPR:
15416 0 : case BIND_EXPR:
15417 0 : return RECURSE (TREE_OPERAND (t, 1));
15418 :
15419 0 : case SAVE_EXPR:
15420 0 : return RECURSE (TREE_OPERAND (t, 0));
15421 :
15422 : default:
15423 : break;
15424 : }
15425 : return false;
15426 : }
15427 :
15428 : #undef RECURSE
15429 : #undef integer_valued_real_p
15430 :
15431 : /* Return true if the floating point expression T has an integer value.
15432 : We also allow +Inf, -Inf and NaN to be considered integer values.
15433 : Return false for signaling NaN.
15434 :
15435 : DEPTH is the current nesting depth of the query. */
15436 :
15437 : bool
15438 98330 : integer_valued_real_p (tree t, int depth)
15439 : {
15440 98330 : if (t == error_mark_node)
15441 : return false;
15442 :
15443 98330 : STRIP_ANY_LOCATION_WRAPPER (t);
15444 :
15445 98330 : tree_code code = TREE_CODE (t);
15446 98330 : switch (TREE_CODE_CLASS (code))
15447 : {
15448 0 : case tcc_binary:
15449 0 : case tcc_comparison:
15450 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15451 0 : TREE_OPERAND (t, 1), depth);
15452 :
15453 0 : case tcc_unary:
15454 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15455 :
15456 8533 : case tcc_constant:
15457 8533 : case tcc_declaration:
15458 8533 : case tcc_reference:
15459 8533 : return integer_valued_real_single_p (t, depth);
15460 :
15461 89797 : default:
15462 89797 : break;
15463 : }
15464 :
15465 89797 : switch (code)
15466 : {
15467 89797 : case COND_EXPR:
15468 89797 : case SSA_NAME:
15469 89797 : return integer_valued_real_single_p (t, depth);
15470 :
15471 0 : case CALL_EXPR:
15472 0 : {
15473 0 : tree arg0 = (call_expr_nargs (t) > 0
15474 0 : ? CALL_EXPR_ARG (t, 0)
15475 0 : : NULL_TREE);
15476 0 : tree arg1 = (call_expr_nargs (t) > 1
15477 0 : ? CALL_EXPR_ARG (t, 1)
15478 0 : : NULL_TREE);
15479 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15480 0 : arg0, arg1, depth);
15481 : }
15482 :
15483 0 : default:
15484 0 : return integer_valued_real_invalid_p (t, depth);
15485 : }
15486 : }
15487 :
15488 : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15489 : attempt to fold the expression to a constant without modifying TYPE,
15490 : OP0 or OP1.
15491 :
15492 : If the expression could be simplified to a constant, then return
15493 : the constant. If the expression would not be simplified to a
15494 : constant, then return NULL_TREE. */
15495 :
15496 : tree
15497 16032072 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15498 : {
15499 16032072 : tree tem = fold_binary (code, type, op0, op1);
15500 16032072 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15501 : }
15502 :
15503 : /* Given the components of a unary expression CODE, TYPE and OP0,
15504 : attempt to fold the expression to a constant without modifying
15505 : TYPE or OP0.
15506 :
15507 : If the expression could be simplified to a constant, then return
15508 : the constant. If the expression would not be simplified to a
15509 : constant, then return NULL_TREE. */
15510 :
15511 : tree
15512 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15513 : {
15514 0 : tree tem = fold_unary (code, type, op0);
15515 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15516 : }
15517 :
15518 : /* If EXP represents referencing an element in a constant string
15519 : (either via pointer arithmetic or array indexing), return the
15520 : tree representing the value accessed, otherwise return NULL. */
15521 :
15522 : tree
15523 218877868 : fold_read_from_constant_string (tree exp)
15524 : {
15525 218877868 : if ((INDIRECT_REF_P (exp)
15526 218877849 : || TREE_CODE (exp) == ARRAY_REF)
15527 232314225 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15528 : {
15529 10202991 : tree exp1 = TREE_OPERAND (exp, 0);
15530 10202991 : tree index;
15531 10202991 : tree string;
15532 10202991 : location_t loc = EXPR_LOCATION (exp);
15533 :
15534 10202991 : if (INDIRECT_REF_P (exp))
15535 0 : string = string_constant (exp1, &index, NULL, NULL);
15536 : else
15537 : {
15538 10202991 : tree low_bound = array_ref_low_bound (exp);
15539 10202991 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15540 :
15541 : /* Optimize the special-case of a zero lower bound.
15542 :
15543 : We convert the low_bound to sizetype to avoid some problems
15544 : with constant folding. (E.g. suppose the lower bound is 1,
15545 : and its mode is QI. Without the conversion,l (ARRAY
15546 : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15547 : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15548 10202991 : if (! integer_zerop (low_bound))
15549 156286 : index = size_diffop_loc (loc, index,
15550 : fold_convert_loc (loc, sizetype, low_bound));
15551 :
15552 : string = exp1;
15553 : }
15554 :
15555 10202991 : scalar_int_mode char_mode;
15556 10202991 : if (string
15557 10202991 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15558 10202991 : && TREE_CODE (string) == STRING_CST
15559 260932 : && tree_fits_uhwi_p (index)
15560 257020 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15561 10459809 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15562 : &char_mode)
15563 20405982 : && GET_MODE_SIZE (char_mode) == 1)
15564 511184 : return build_int_cst_type (TREE_TYPE (exp),
15565 255592 : (TREE_STRING_POINTER (string)
15566 255592 : [TREE_INT_CST_LOW (index)]));
15567 : }
15568 : return NULL;
15569 : }
15570 :
15571 : /* Folds a read from vector element at IDX of vector ARG. */
15572 :
15573 : tree
15574 9971 : fold_read_from_vector (tree arg, poly_uint64 idx)
15575 : {
15576 9971 : unsigned HOST_WIDE_INT i;
15577 9971 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15578 9971 : && known_ge (idx, 0u)
15579 9971 : && idx.is_constant (&i))
15580 : {
15581 9971 : if (TREE_CODE (arg) == VECTOR_CST)
15582 2120 : return VECTOR_CST_ELT (arg, i);
15583 7851 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15584 : {
15585 2680 : if (CONSTRUCTOR_NELTS (arg)
15586 2640 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15587 : return NULL_TREE;
15588 1842 : if (i >= CONSTRUCTOR_NELTS (arg))
15589 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15590 1802 : return CONSTRUCTOR_ELT (arg, i)->value;
15591 : }
15592 : }
15593 : return NULL_TREE;
15594 : }
15595 :
15596 : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15597 : an integer constant, real, or fixed-point constant.
15598 :
15599 : TYPE is the type of the result. */
15600 :
15601 : static tree
15602 33023994 : fold_negate_const (tree arg0, tree type)
15603 : {
15604 33023994 : tree t = NULL_TREE;
15605 :
15606 33023994 : switch (TREE_CODE (arg0))
15607 : {
15608 2065194 : case REAL_CST:
15609 2065194 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15610 2065194 : break;
15611 :
15612 0 : case FIXED_CST:
15613 0 : {
15614 0 : FIXED_VALUE_TYPE f;
15615 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15616 0 : &(TREE_FIXED_CST (arg0)), NULL,
15617 0 : TYPE_SATURATING (type));
15618 0 : t = build_fixed (type, f);
15619 : /* Propagate overflow flags. */
15620 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15621 0 : TREE_OVERFLOW (t) = 1;
15622 0 : break;
15623 : }
15624 :
15625 30958800 : default:
15626 30958800 : if (poly_int_tree_p (arg0))
15627 : {
15628 30958800 : wi::overflow_type overflow;
15629 30958800 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15630 30958800 : t = force_fit_type (type, res, 1,
15631 30958800 : (overflow && ! TYPE_UNSIGNED (type))
15632 30948969 : || TREE_OVERFLOW (arg0));
15633 30958800 : break;
15634 30958800 : }
15635 :
15636 0 : gcc_unreachable ();
15637 : }
15638 :
15639 33023994 : return t;
15640 : }
15641 :
15642 : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15643 : an integer constant or real constant.
15644 :
15645 : TYPE is the type of the result. */
15646 :
15647 : tree
15648 36040 : fold_abs_const (tree arg0, tree type)
15649 : {
15650 36040 : tree t = NULL_TREE;
15651 :
15652 36040 : switch (TREE_CODE (arg0))
15653 : {
15654 6536 : case INTEGER_CST:
15655 6536 : {
15656 : /* If the value is unsigned or non-negative, then the absolute value
15657 : is the same as the ordinary value. */
15658 6536 : wide_int val = wi::to_wide (arg0);
15659 6536 : wi::overflow_type overflow = wi::OVF_NONE;
15660 6536 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15661 : ;
15662 :
15663 : /* If the value is negative, then the absolute value is
15664 : its negation. */
15665 : else
15666 3015 : val = wi::neg (val, &overflow);
15667 :
15668 : /* Force to the destination type, set TREE_OVERFLOW for signed
15669 : TYPE only. */
15670 6536 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15671 6536 : }
15672 6536 : break;
15673 :
15674 29504 : case REAL_CST:
15675 29504 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15676 7515 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15677 : else
15678 : t = arg0;
15679 : break;
15680 :
15681 0 : default:
15682 0 : gcc_unreachable ();
15683 : }
15684 :
15685 36040 : return t;
15686 : }
15687 :
15688 : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15689 : constant. TYPE is the type of the result. */
15690 :
15691 : static tree
15692 2347925 : fold_not_const (const_tree arg0, tree type)
15693 : {
15694 2347925 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15695 :
15696 2347925 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15697 : }
15698 :
15699 : /* Given CODE, a relational operator, the target type, TYPE and two
15700 : constant operands OP0 and OP1, return the result of the
15701 : relational operation. If the result is not a compile time
15702 : constant, then return NULL_TREE. */
15703 :
15704 : static tree
15705 83760678 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15706 : {
15707 83760678 : int result, invert;
15708 :
15709 : /* From here on, the only cases we handle are when the result is
15710 : known to be a constant. */
15711 :
15712 83760678 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15713 : {
15714 1224621 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15715 1224621 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15716 :
15717 : /* Handle the cases where either operand is a NaN. */
15718 1224621 : if (real_isnan (c0) || real_isnan (c1))
15719 : {
15720 13727 : switch (code)
15721 : {
15722 : case EQ_EXPR:
15723 : case ORDERED_EXPR:
15724 : result = 0;
15725 : break;
15726 :
15727 : case NE_EXPR:
15728 : case UNORDERED_EXPR:
15729 : case UNLT_EXPR:
15730 : case UNLE_EXPR:
15731 : case UNGT_EXPR:
15732 : case UNGE_EXPR:
15733 : case UNEQ_EXPR:
15734 6805 : result = 1;
15735 : break;
15736 :
15737 6965 : case LT_EXPR:
15738 6965 : case LE_EXPR:
15739 6965 : case GT_EXPR:
15740 6965 : case GE_EXPR:
15741 6965 : case LTGT_EXPR:
15742 6965 : if (flag_trapping_math)
15743 : return NULL_TREE;
15744 : result = 0;
15745 : break;
15746 :
15747 0 : default:
15748 0 : gcc_unreachable ();
15749 : }
15750 :
15751 6805 : return constant_boolean_node (result, type);
15752 : }
15753 :
15754 1210894 : return constant_boolean_node (real_compare (code, c0, c1), type);
15755 : }
15756 :
15757 82536057 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15758 : {
15759 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15760 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15761 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
15762 : }
15763 :
15764 : /* Handle equality/inequality of complex constants. */
15765 82536057 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15766 : {
15767 58558 : tree rcond = fold_relational_const (code, type,
15768 29279 : TREE_REALPART (op0),
15769 29279 : TREE_REALPART (op1));
15770 117116 : tree icond = fold_relational_const (code, type,
15771 29279 : TREE_IMAGPART (op0),
15772 29279 : TREE_IMAGPART (op1));
15773 29279 : if (code == EQ_EXPR)
15774 302 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15775 28977 : else if (code == NE_EXPR)
15776 28977 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15777 : else
15778 : return NULL_TREE;
15779 : }
15780 :
15781 82506778 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15782 : {
15783 22563 : if (!VECTOR_TYPE_P (type))
15784 : {
15785 : /* Have vector comparison with scalar boolean result. */
15786 198 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15787 : && known_eq (VECTOR_CST_NELTS (op0),
15788 : VECTOR_CST_NELTS (op1)));
15789 198 : unsigned HOST_WIDE_INT nunits;
15790 198 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15791 : return NULL_TREE;
15792 657 : for (unsigned i = 0; i < nunits; i++)
15793 : {
15794 560 : tree elem0 = VECTOR_CST_ELT (op0, i);
15795 560 : tree elem1 = VECTOR_CST_ELT (op1, i);
15796 560 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15797 560 : if (tmp == NULL_TREE)
15798 : return NULL_TREE;
15799 560 : if (integer_zerop (tmp))
15800 101 : return constant_boolean_node (code == NE_EXPR, type);
15801 : }
15802 97 : return constant_boolean_node (code == EQ_EXPR, type);
15803 : }
15804 22365 : tree_vector_builder elts;
15805 22365 : if (!elts.new_binary_operation (type, op0, op1, false))
15806 : return NULL_TREE;
15807 22365 : unsigned int count = elts.encoded_nelts ();
15808 77891 : for (unsigned i = 0; i < count; i++)
15809 : {
15810 55526 : tree elem_type = TREE_TYPE (type);
15811 55526 : tree elem0 = VECTOR_CST_ELT (op0, i);
15812 55526 : tree elem1 = VECTOR_CST_ELT (op1, i);
15813 :
15814 55526 : tree tem = fold_relational_const (code, elem_type,
15815 : elem0, elem1);
15816 :
15817 55526 : if (tem == NULL_TREE)
15818 : return NULL_TREE;
15819 :
15820 55526 : elts.quick_push (build_int_cst (elem_type,
15821 89845 : integer_zerop (tem) ? 0 : -1));
15822 : }
15823 :
15824 22365 : return elts.build ();
15825 22365 : }
15826 :
15827 : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15828 :
15829 : To compute GT, swap the arguments and do LT.
15830 : To compute GE, do LT and invert the result.
15831 : To compute LE, swap the arguments, do LT and invert the result.
15832 : To compute NE, do EQ and invert the result.
15833 :
15834 : Therefore, the code below must handle only EQ and LT. */
15835 :
15836 82484215 : if (code == LE_EXPR || code == GT_EXPR)
15837 : {
15838 14650151 : std::swap (op0, op1);
15839 14650151 : code = swap_tree_comparison (code);
15840 : }
15841 :
15842 : /* Note that it is safe to invert for real values here because we
15843 : have already handled the one case that it matters. */
15844 :
15845 82484215 : invert = 0;
15846 82484215 : if (code == NE_EXPR || code == GE_EXPR)
15847 : {
15848 37136105 : invert = 1;
15849 37136105 : code = invert_tree_comparison (code, false);
15850 : }
15851 :
15852 : /* Compute a result for LT or EQ if args permit;
15853 : Otherwise return T. */
15854 82484215 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15855 : {
15856 82456131 : if (code == EQ_EXPR)
15857 40431448 : result = tree_int_cst_equal (op0, op1);
15858 : else
15859 42024683 : result = tree_int_cst_lt (op0, op1);
15860 : }
15861 : else
15862 : return NULL_TREE;
15863 :
15864 82456131 : if (invert)
15865 37134243 : result ^= 1;
15866 82456131 : return constant_boolean_node (result, type);
15867 : }
15868 :
15869 : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15870 : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15871 : itself. */
15872 :
15873 : tree
15874 137852430 : fold_build_cleanup_point_expr (tree type, tree expr)
15875 : {
15876 : /* If the expression does not have side effects then we don't have to wrap
15877 : it with a cleanup point expression. */
15878 137852430 : if (!TREE_SIDE_EFFECTS (expr))
15879 : return expr;
15880 :
15881 : /* If the expression is a return, check to see if the expression inside the
15882 : return has no side effects or the right hand side of the modify expression
15883 : inside the return. If either don't have side effects set we don't need to
15884 : wrap the expression in a cleanup point expression. Note we don't check the
15885 : left hand side of the modify because it should always be a return decl. */
15886 117959476 : if (TREE_CODE (expr) == RETURN_EXPR)
15887 : {
15888 46477620 : tree op = TREE_OPERAND (expr, 0);
15889 46477620 : if (!op || !TREE_SIDE_EFFECTS (op))
15890 : return expr;
15891 45766260 : op = TREE_OPERAND (op, 1);
15892 45766260 : if (!TREE_SIDE_EFFECTS (op))
15893 : return expr;
15894 : }
15895 :
15896 93226722 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
15897 : }
15898 :
15899 : /* Given a pointer value OP0 and a type TYPE, return a simplified version
15900 : of an indirection through OP0, or NULL_TREE if no simplification is
15901 : possible. */
15902 :
15903 : tree
15904 22681404 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15905 : {
15906 22681404 : tree sub = op0;
15907 22681404 : tree subtype;
15908 22681404 : poly_uint64 const_op01;
15909 :
15910 22681404 : STRIP_NOPS (sub);
15911 22681404 : subtype = TREE_TYPE (sub);
15912 22681404 : if (!POINTER_TYPE_P (subtype)
15913 22681404 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
15914 : return NULL_TREE;
15915 :
15916 22523662 : if (TREE_CODE (sub) == ADDR_EXPR)
15917 : {
15918 5054923 : tree op = TREE_OPERAND (sub, 0);
15919 5054923 : tree optype = TREE_TYPE (op);
15920 :
15921 : /* *&CONST_DECL -> to the value of the const decl. */
15922 5054923 : if (TREE_CODE (op) == CONST_DECL)
15923 3280 : return DECL_INITIAL (op);
15924 : /* *&p => p; make sure to handle *&"str"[cst] here. */
15925 5051643 : if (type == optype)
15926 : {
15927 3824881 : tree fop = fold_read_from_constant_string (op);
15928 3824881 : if (fop)
15929 : return fop;
15930 : else
15931 3778820 : return op;
15932 : }
15933 : /* *(foo *)&fooarray => fooarray[0] */
15934 1226762 : else if (TREE_CODE (optype) == ARRAY_TYPE
15935 13889 : && type == TREE_TYPE (optype)
15936 1239451 : && (!in_gimple_form
15937 3106 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15938 : {
15939 12689 : tree type_domain = TYPE_DOMAIN (optype);
15940 12689 : tree min_val = size_zero_node;
15941 12689 : if (type_domain && TYPE_MIN_VALUE (type_domain))
15942 12650 : min_val = TYPE_MIN_VALUE (type_domain);
15943 12689 : if (in_gimple_form
15944 3106 : && TREE_CODE (min_val) != INTEGER_CST)
15945 : return NULL_TREE;
15946 12689 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
15947 12689 : NULL_TREE, NULL_TREE);
15948 : }
15949 : /* *(foo *)&complexfoo => __real__ complexfoo */
15950 1214073 : else if (TREE_CODE (optype) == COMPLEX_TYPE
15951 1214073 : && type == TREE_TYPE (optype))
15952 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
15953 : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15954 1214073 : else if (VECTOR_TYPE_P (optype)
15955 1214073 : && type == TREE_TYPE (optype))
15956 : {
15957 70 : tree part_width = TYPE_SIZE (type);
15958 70 : tree index = bitsize_int (0);
15959 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
15960 70 : index);
15961 : }
15962 : }
15963 :
15964 18682742 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15965 18682742 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
15966 : {
15967 261339 : tree op00 = TREE_OPERAND (sub, 0);
15968 261339 : tree op01 = TREE_OPERAND (sub, 1);
15969 :
15970 261339 : STRIP_NOPS (op00);
15971 261339 : if (TREE_CODE (op00) == ADDR_EXPR)
15972 : {
15973 1741 : tree op00type;
15974 1741 : op00 = TREE_OPERAND (op00, 0);
15975 1741 : op00type = TREE_TYPE (op00);
15976 :
15977 : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15978 1741 : if (VECTOR_TYPE_P (op00type)
15979 240 : && type == TREE_TYPE (op00type)
15980 : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
15981 : but we want to treat offsets with MSB set as negative.
15982 : For the code below negative offsets are invalid and
15983 : TYPE_SIZE of the element is something unsigned, so
15984 : check whether op01 fits into poly_int64, which implies
15985 : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
15986 : then just use poly_uint64 because we want to treat the
15987 : value as unsigned. */
15988 1934 : && tree_fits_poly_int64_p (op01))
15989 : {
15990 179 : tree part_width = TYPE_SIZE (type);
15991 179 : poly_uint64 max_offset
15992 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
15993 179 : * TYPE_VECTOR_SUBPARTS (op00type));
15994 179 : if (known_lt (const_op01, max_offset))
15995 : {
15996 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
15997 179 : return fold_build3_loc (loc,
15998 : BIT_FIELD_REF, type, op00,
15999 179 : part_width, index);
16000 : }
16001 : }
16002 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16003 1562 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16004 1562 : && type == TREE_TYPE (op00type))
16005 : {
16006 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16007 : const_op01))
16008 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16009 : }
16010 : /* ((foo *)&fooarray)[1] => fooarray[1] */
16011 1562 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16012 1562 : && type == TREE_TYPE (op00type))
16013 : {
16014 719 : tree type_domain = TYPE_DOMAIN (op00type);
16015 719 : tree min_val = size_zero_node;
16016 719 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16017 718 : min_val = TYPE_MIN_VALUE (type_domain);
16018 719 : poly_uint64 type_size, index;
16019 719 : if (poly_int_tree_p (min_val)
16020 719 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16021 719 : && multiple_p (const_op01, type_size, &index))
16022 : {
16023 719 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16024 719 : op01 = wide_int_to_tree (sizetype, off);
16025 719 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16026 : NULL_TREE, NULL_TREE);
16027 : }
16028 : }
16029 : }
16030 : }
16031 :
16032 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16033 18681844 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16034 681085 : && type == TREE_TYPE (TREE_TYPE (subtype))
16035 18685047 : && (!in_gimple_form
16036 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16037 : {
16038 3202 : tree type_domain;
16039 3202 : tree min_val = size_zero_node;
16040 3202 : sub = build_fold_indirect_ref_loc (loc, sub);
16041 3202 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16042 3202 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16043 3202 : min_val = TYPE_MIN_VALUE (type_domain);
16044 3202 : if (in_gimple_form
16045 11 : && TREE_CODE (min_val) != INTEGER_CST)
16046 : return NULL_TREE;
16047 3202 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16048 3202 : NULL_TREE);
16049 : }
16050 :
16051 : return NULL_TREE;
16052 : }
16053 :
16054 : /* Builds an expression for an indirection through T, simplifying some
16055 : cases. */
16056 :
16057 : tree
16058 11887114 : build_fold_indirect_ref_loc (location_t loc, tree t)
16059 : {
16060 11887114 : tree type = TREE_TYPE (TREE_TYPE (t));
16061 11887114 : tree sub = fold_indirect_ref_1 (loc, type, t);
16062 :
16063 11887114 : if (sub)
16064 : return sub;
16065 :
16066 8065974 : return build1_loc (loc, INDIRECT_REF, type, t);
16067 : }
16068 :
16069 : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16070 :
16071 : tree
16072 10415209 : fold_indirect_ref_loc (location_t loc, tree t)
16073 : {
16074 10415209 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16075 :
16076 10415209 : if (sub)
16077 : return sub;
16078 : else
16079 10393938 : return t;
16080 : }
16081 :
16082 : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16083 : whose result is ignored. The type of the returned tree need not be
16084 : the same as the original expression. */
16085 :
16086 : tree
16087 135919 : fold_ignored_result (tree t)
16088 : {
16089 135919 : if (!TREE_SIDE_EFFECTS (t))
16090 18075 : return integer_zero_node;
16091 :
16092 157040 : for (;;)
16093 157040 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16094 : {
16095 3835 : case tcc_unary:
16096 3835 : t = TREE_OPERAND (t, 0);
16097 3835 : break;
16098 :
16099 5119 : case tcc_binary:
16100 5119 : case tcc_comparison:
16101 5119 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16102 3204 : t = TREE_OPERAND (t, 0);
16103 1915 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16104 43 : t = TREE_OPERAND (t, 1);
16105 : else
16106 : return t;
16107 : break;
16108 :
16109 100476 : case tcc_expression:
16110 100476 : switch (TREE_CODE (t))
16111 : {
16112 32115 : case COMPOUND_EXPR:
16113 32115 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16114 : return t;
16115 31820 : t = TREE_OPERAND (t, 0);
16116 31820 : break;
16117 :
16118 382 : case COND_EXPR:
16119 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16120 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16121 : return t;
16122 294 : t = TREE_OPERAND (t, 0);
16123 294 : break;
16124 :
16125 : default:
16126 : return t;
16127 : }
16128 : break;
16129 :
16130 : default:
16131 : return t;
16132 : }
16133 : }
16134 :
16135 : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16136 :
16137 : tree
16138 3173887506 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16139 : {
16140 3173887506 : tree div = NULL_TREE;
16141 :
16142 3173887506 : if (divisor == 1)
16143 : return value;
16144 :
16145 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16146 : have to do anything. Only do this when we are not given a const,
16147 : because in that case, this check is more expensive than just
16148 : doing it. */
16149 1971569842 : if (TREE_CODE (value) != INTEGER_CST)
16150 : {
16151 378426 : div = build_int_cst (TREE_TYPE (value), divisor);
16152 :
16153 378426 : if (multiple_of_p (TREE_TYPE (value), value, div))
16154 : return value;
16155 : }
16156 :
16157 : /* If divisor is a power of two, simplify this to bit manipulation. */
16158 1971193328 : if (pow2_or_zerop (divisor))
16159 : {
16160 1971193328 : if (TREE_CODE (value) == INTEGER_CST)
16161 : {
16162 1971191416 : wide_int val = wi::to_wide (value);
16163 1971191416 : bool overflow_p;
16164 :
16165 1971191416 : if ((val & (divisor - 1)) == 0)
16166 : return value;
16167 :
16168 4108319 : overflow_p = TREE_OVERFLOW (value);
16169 4108319 : val += divisor - 1;
16170 4108319 : val &= (int) -divisor;
16171 4108319 : if (val == 0)
16172 4 : overflow_p = true;
16173 :
16174 4108319 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16175 1971191416 : }
16176 : else
16177 : {
16178 1912 : tree t;
16179 :
16180 1912 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16181 1912 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16182 1912 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16183 1912 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16184 : }
16185 : }
16186 : else
16187 : {
16188 0 : if (!div)
16189 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16190 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16191 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16192 : }
16193 :
16194 : return value;
16195 : }
16196 :
16197 : /* Likewise, but round down. */
16198 :
16199 : tree
16200 21866036 : round_down_loc (location_t loc, tree value, int divisor)
16201 : {
16202 21866036 : tree div = NULL_TREE;
16203 :
16204 21866036 : gcc_assert (divisor > 0);
16205 21866036 : if (divisor == 1)
16206 : return value;
16207 :
16208 : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16209 : have to do anything. Only do this when we are not given a const,
16210 : because in that case, this check is more expensive than just
16211 : doing it. */
16212 21866036 : if (TREE_CODE (value) != INTEGER_CST)
16213 : {
16214 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16215 :
16216 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16217 : return value;
16218 : }
16219 :
16220 : /* If divisor is a power of two, simplify this to bit manipulation. */
16221 21866036 : if (pow2_or_zerop (divisor))
16222 : {
16223 21866036 : tree t;
16224 :
16225 21866036 : t = build_int_cst (TREE_TYPE (value), -divisor);
16226 21866036 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16227 : }
16228 : else
16229 : {
16230 0 : if (!div)
16231 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16232 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16233 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16234 : }
16235 :
16236 : return value;
16237 : }
16238 :
16239 : /* Returns the pointer to the base of the object addressed by EXP and
16240 : extracts the information about the offset of the access, storing it
16241 : to PBITPOS and POFFSET. */
16242 :
16243 : static tree
16244 25862394 : split_address_to_core_and_offset (tree exp,
16245 : poly_int64 *pbitpos, tree *poffset)
16246 : {
16247 25862394 : tree core;
16248 25862394 : machine_mode mode;
16249 25862394 : int unsignedp, reversep, volatilep;
16250 25862394 : poly_int64 bitsize;
16251 25862394 : location_t loc = EXPR_LOCATION (exp);
16252 :
16253 25862394 : STRIP_NOPS (exp);
16254 :
16255 25862394 : if (TREE_CODE (exp) == SSA_NAME)
16256 16243337 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16257 19993155 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16258 49467 : exp = gimple_assign_rhs1 (def);
16259 :
16260 25862394 : if (TREE_CODE (exp) == ADDR_EXPR)
16261 : {
16262 1663788 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16263 : poffset, &mode, &unsignedp, &reversep,
16264 : &volatilep);
16265 : /* If we are left with MEM[a + CST] strip that and add it to the
16266 : pbitpos and return a. */
16267 1663788 : if (TREE_CODE (core) == MEM_REF)
16268 : {
16269 48220 : poly_offset_int tem;
16270 48220 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
16271 48220 : tem <<= LOG2_BITS_PER_UNIT;
16272 48220 : tem += *pbitpos;
16273 48220 : if (tem.to_shwi (pbitpos))
16274 48032 : return TREE_OPERAND (core, 0);
16275 : }
16276 1615756 : core = build_fold_addr_expr_loc (loc, core);
16277 : }
16278 24198606 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16279 : {
16280 1071611 : core = TREE_OPERAND (exp, 0);
16281 1071611 : STRIP_NOPS (core);
16282 1071611 : *pbitpos = 0;
16283 1071611 : *poffset = TREE_OPERAND (exp, 1);
16284 1071611 : if (poly_int_tree_p (*poffset))
16285 : {
16286 1053124 : poly_offset_int tem
16287 1053124 : = wi::sext (wi::to_poly_offset (*poffset),
16288 1053124 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16289 1053124 : tem <<= LOG2_BITS_PER_UNIT;
16290 1053124 : if (tem.to_shwi (pbitpos))
16291 1053124 : *poffset = NULL_TREE;
16292 : }
16293 : }
16294 : else
16295 : {
16296 23126995 : core = exp;
16297 23126995 : *pbitpos = 0;
16298 23126995 : *poffset = NULL_TREE;
16299 : }
16300 :
16301 : return core;
16302 : }
16303 :
16304 : /* Returns true if addresses of E1 and E2 differ by a constant, false
16305 : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16306 :
16307 : bool
16308 12931197 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16309 : {
16310 12931197 : tree core1, core2;
16311 12931197 : poly_int64 bitpos1, bitpos2;
16312 12931197 : tree toffset1, toffset2, tdiff, type;
16313 :
16314 12931197 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16315 12931197 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16316 :
16317 12931197 : poly_int64 bytepos1, bytepos2;
16318 12931197 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16319 25862394 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16320 25862394 : || !operand_equal_p (core1, core2, 0))
16321 : return false;
16322 :
16323 702003 : if (toffset1 && toffset2)
16324 : {
16325 149 : type = TREE_TYPE (toffset1);
16326 149 : if (type != TREE_TYPE (toffset2))
16327 0 : toffset2 = fold_convert (type, toffset2);
16328 :
16329 149 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16330 149 : if (!cst_and_fits_in_hwi (tdiff))
16331 : return false;
16332 :
16333 53 : *diff = int_cst_value (tdiff);
16334 : }
16335 701854 : else if (toffset1 || toffset2)
16336 : {
16337 : /* If only one of the offsets is non-constant, the difference cannot
16338 : be a constant. */
16339 : return false;
16340 : }
16341 : else
16342 683194 : *diff = 0;
16343 :
16344 683247 : *diff += bytepos1 - bytepos2;
16345 683247 : return true;
16346 : }
16347 :
16348 : /* Return OFF converted to a pointer offset type suitable as offset for
16349 : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16350 : tree
16351 53604568 : convert_to_ptrofftype_loc (location_t loc, tree off)
16352 : {
16353 53604568 : if (ptrofftype_p (TREE_TYPE (off)))
16354 : return off;
16355 6254506 : return fold_convert_loc (loc, sizetype, off);
16356 : }
16357 :
16358 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16359 : tree
16360 47454266 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16361 : {
16362 47454266 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16363 47454266 : ptr, convert_to_ptrofftype_loc (loc, off));
16364 : }
16365 :
16366 : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16367 : tree
16368 166336 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16369 : {
16370 166336 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16371 166336 : ptr, size_int (off));
16372 : }
16373 :
16374 : /* Return a pointer to a NUL-terminated string containing the sequence
16375 : of bytes corresponding to the representation of the object referred to
16376 : by SRC (or a subsequence of such bytes within it if SRC is a reference
16377 : to an initialized constant array plus some constant offset).
16378 : Set *STRSIZE the number of bytes in the constant sequence including
16379 : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16380 : where A is the array that stores the constant sequence that SRC points
16381 : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16382 : need not point to a string or even an array of characters but may point
16383 : to an object of any type. */
16384 :
16385 : const char *
16386 12776795 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16387 : {
16388 : /* The offset into the array A storing the string, and A's byte size. */
16389 12776795 : tree offset_node;
16390 12776795 : tree mem_size;
16391 :
16392 12776795 : if (strsize)
16393 4685252 : *strsize = 0;
16394 :
16395 12776795 : if (strsize)
16396 4685252 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16397 : else
16398 8091543 : src = string_constant (src, &offset_node, &mem_size, NULL);
16399 12776795 : if (!src)
16400 : return NULL;
16401 :
16402 2855792 : unsigned HOST_WIDE_INT offset = 0;
16403 2855792 : if (offset_node != NULL_TREE)
16404 : {
16405 2855792 : if (!tree_fits_uhwi_p (offset_node))
16406 : return NULL;
16407 : else
16408 2852642 : offset = tree_to_uhwi (offset_node);
16409 : }
16410 :
16411 2852642 : if (!tree_fits_uhwi_p (mem_size))
16412 : return NULL;
16413 :
16414 : /* ARRAY_SIZE is the byte size of the array the constant sequence
16415 : is stored in and equal to sizeof A. INIT_BYTES is the number
16416 : of bytes in the constant sequence used to initialize the array,
16417 : including any embedded NULs as well as the terminating NUL (for
16418 : strings), but not including any trailing zeros/NULs past
16419 : the terminating one appended implicitly to a string literal to
16420 : zero out the remainder of the array it's stored in. For example,
16421 : given:
16422 : const char a[7] = "abc\0d";
16423 : n = strlen (a + 1);
16424 : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16425 : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16426 : is equal to strlen (A) + 1. */
16427 2852642 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16428 2852642 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16429 2852642 : const char *string = TREE_STRING_POINTER (src);
16430 :
16431 : /* Ideally this would turn into a gcc_checking_assert over time. */
16432 2852642 : if (init_bytes > array_size)
16433 : init_bytes = array_size;
16434 :
16435 2852642 : if (init_bytes == 0 || offset >= array_size)
16436 : return NULL;
16437 :
16438 2851375 : if (strsize)
16439 : {
16440 : /* Compute and store the number of characters from the beginning
16441 : of the substring at OFFSET to the end, including the terminating
16442 : nul. Offsets past the initial length refer to null strings. */
16443 1443684 : if (offset < init_bytes)
16444 1443684 : *strsize = init_bytes - offset;
16445 : else
16446 0 : *strsize = 1;
16447 : }
16448 : else
16449 : {
16450 1407691 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16451 : /* Support only properly NUL-terminated single byte strings. */
16452 1407691 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16453 : return NULL;
16454 1402843 : if (string[init_bytes - 1] != '\0')
16455 : return NULL;
16456 : }
16457 :
16458 2820566 : return offset < init_bytes ? string + offset : "";
16459 : }
16460 :
16461 : /* Return a pointer to a NUL-terminated string corresponding to
16462 : the expression STR referencing a constant string, possibly
16463 : involving a constant offset. Return null if STR either doesn't
16464 : reference a constant string or if it involves a nonconstant
16465 : offset. */
16466 :
16467 : const char *
16468 8091543 : c_getstr (tree str)
16469 : {
16470 8091543 : return getbyterep (str, NULL);
16471 : }
16472 :
16473 : /* Helper for tree_nonzero_bits. Given a tree T, compute which bits in T
16474 : may be nonzero, with precision PREC, the precision of T's type. */
16475 :
16476 : static wide_int
16477 258953236 : tree_nonzero_bits (const_tree t, unsigned prec)
16478 : {
16479 258953236 : switch (TREE_CODE (t))
16480 : {
16481 8785663 : case INTEGER_CST:
16482 8785663 : return wi::to_wide (t);
16483 145186665 : case SSA_NAME:
16484 145186665 : return get_nonzero_bits (t);
16485 263615 : case NON_LVALUE_EXPR:
16486 263615 : case SAVE_EXPR:
16487 263615 : return tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16488 520526 : case BIT_AND_EXPR:
16489 1041052 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16490 1561578 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16491 19600 : case BIT_IOR_EXPR:
16492 19600 : case BIT_XOR_EXPR:
16493 39200 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0), prec),
16494 58800 : tree_nonzero_bits (TREE_OPERAND (t, 1), prec));
16495 73122 : case COND_EXPR:
16496 146244 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1), prec),
16497 219366 : tree_nonzero_bits (TREE_OPERAND (t, 2), prec));
16498 52810555 : CASE_CONVERT:
16499 52810555 : if (TREE_TYPE (t) != error_mark_node
16500 52810555 : && !error_operand_p (TREE_OPERAND (t, 0)))
16501 : {
16502 52810554 : tree op0 = TREE_OPERAND (t, 0);
16503 52810554 : tree inner_type = TREE_TYPE (op0);
16504 52810554 : unsigned inner_prec = TYPE_PRECISION (inner_type);
16505 105621108 : return wide_int::from (tree_nonzero_bits (op0, inner_prec),
16506 105621108 : prec, TYPE_SIGN (inner_type));
16507 : }
16508 : break;
16509 14227180 : case PLUS_EXPR:
16510 14227180 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16511 : {
16512 14227180 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16513 14227180 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1), prec);
16514 14227180 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16515 547979 : return wi::bit_or (nzbits1, nzbits2);
16516 14227180 : }
16517 : break;
16518 170586 : case LSHIFT_EXPR:
16519 170586 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16520 170586 : && TREE_TYPE (t) != error_mark_node)
16521 : {
16522 98664 : tree type = TREE_TYPE (t);
16523 98664 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16524 98664 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16525 98664 : return wi::neg_p (arg1)
16526 197328 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16527 98664 : : wi::lshift (nzbits, arg1);
16528 98664 : }
16529 : break;
16530 162719 : case RSHIFT_EXPR:
16531 162719 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
16532 162719 : && TREE_TYPE (t) != error_mark_node)
16533 : {
16534 160775 : tree type = TREE_TYPE (t);
16535 160775 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0), prec);
16536 160775 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), prec);
16537 160775 : return wi::neg_p (arg1)
16538 321550 : ? wi::lshift (nzbits, -arg1)
16539 160775 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16540 160775 : }
16541 : break;
16542 : default:
16543 : break;
16544 : }
16545 :
16546 50486073 : return wi::shwi (-1, prec);
16547 : }
16548 :
16549 : /* Given a tree T, compute which bits in T may be nonzero. */
16550 :
16551 : wide_int
16552 175938772 : tree_nonzero_bits (const_tree t)
16553 : {
16554 175938772 : if (error_operand_p (t))
16555 0 : return wi::shwi (-1, 64);
16556 175938772 : return tree_nonzero_bits (t, TYPE_PRECISION (TREE_TYPE (t)));
16557 : }
16558 :
16559 : /* Helper function for address compare simplifications in match.pd.
16560 : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16561 : TYPE is the type of comparison operands.
16562 : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16563 : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16564 : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16565 : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16566 : and 2 if unknown. */
16567 :
16568 : int
16569 5466695 : address_compare (tree_code code, tree type, tree op0, tree op1,
16570 : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16571 : bool generic)
16572 : {
16573 5466695 : if (TREE_CODE (op0) == SSA_NAME)
16574 37048 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16575 5466695 : if (TREE_CODE (op1) == SSA_NAME)
16576 4580 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16577 5466695 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16578 5466695 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16579 5466695 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16580 5466695 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16581 5466695 : if (base0 && TREE_CODE (base0) == MEM_REF)
16582 : {
16583 36409 : off0 += mem_ref_offset (base0).force_shwi ();
16584 36409 : base0 = TREE_OPERAND (base0, 0);
16585 : }
16586 5466695 : if (base1 && TREE_CODE (base1) == MEM_REF)
16587 : {
16588 3746 : off1 += mem_ref_offset (base1).force_shwi ();
16589 3746 : base1 = TREE_OPERAND (base1, 0);
16590 : }
16591 5466695 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16592 : return 2;
16593 :
16594 5454208 : int equal = 2;
16595 : /* Punt in GENERIC on variables with value expressions;
16596 : the value expressions might point to fields/elements
16597 : of other vars etc. */
16598 5454208 : if (generic
16599 5454208 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16600 5315934 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16601 : return 2;
16602 5453551 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16603 : {
16604 1428996 : symtab_node *node0 = symtab_node::get_create (base0);
16605 1428996 : symtab_node *node1 = symtab_node::get_create (base1);
16606 1428996 : equal = node0->equal_address_to (node1);
16607 : }
16608 4024555 : else if ((DECL_P (base0)
16609 246164 : || TREE_CODE (base0) == SSA_NAME
16610 210874 : || TREE_CODE (base0) == STRING_CST)
16611 4024357 : && (DECL_P (base1)
16612 214568 : || TREE_CODE (base1) == SSA_NAME
16613 211048 : || TREE_CODE (base1) == STRING_CST))
16614 4024342 : equal = (base0 == base1);
16615 : /* Assume different STRING_CSTs with the same content will be
16616 : merged. */
16617 5453338 : if (equal == 0
16618 83593 : && TREE_CODE (base0) == STRING_CST
16619 17440 : && TREE_CODE (base1) == STRING_CST
16620 17265 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16621 5453338 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16622 6190 : TREE_STRING_LENGTH (base0)) == 0)
16623 : equal = 1;
16624 5449118 : if (equal == 1)
16625 : {
16626 5349564 : if (code == EQ_EXPR
16627 5349564 : || code == NE_EXPR
16628 : /* If the offsets are equal we can ignore overflow. */
16629 149803 : || known_eq (off0, off1)
16630 299398 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16631 : /* Or if we compare using pointers to decls or strings. */
16632 5499263 : || (POINTER_TYPE_P (type)
16633 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16634 5349564 : return 1;
16635 : return 2;
16636 : }
16637 103987 : if (equal != 0)
16638 : return equal;
16639 79160 : if (code != EQ_EXPR && code != NE_EXPR)
16640 : return 2;
16641 :
16642 : /* At this point we know (or assume) the two pointers point at
16643 : different objects. */
16644 73881 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16645 73881 : off0.is_constant (&ioff0);
16646 73881 : off1.is_constant (&ioff1);
16647 : /* Punt on non-zero offsets from functions. */
16648 73881 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16649 73881 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16650 : return 2;
16651 : /* Or if the bases are neither decls nor string literals. */
16652 73881 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16653 : return 2;
16654 39327 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16655 : return 2;
16656 : /* For initializers, assume addresses of different functions are
16657 : different. */
16658 39327 : if (folding_initializer
16659 13913 : && TREE_CODE (base0) == FUNCTION_DECL
16660 20 : && TREE_CODE (base1) == FUNCTION_DECL)
16661 : return 0;
16662 :
16663 : /* Compute whether one address points to the start of one
16664 : object and another one to the end of another one. */
16665 39307 : poly_int64 size0 = 0, size1 = 0;
16666 39307 : if (TREE_CODE (base0) == STRING_CST)
16667 : {
16668 12853 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16669 : equal = 2;
16670 : else
16671 : size0 = TREE_STRING_LENGTH (base0);
16672 : }
16673 26454 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16674 : size0 = 1;
16675 : else
16676 : {
16677 26134 : tree sz0 = DECL_SIZE_UNIT (base0);
16678 26134 : if (!tree_fits_poly_int64_p (sz0))
16679 : equal = 2;
16680 : else
16681 26134 : size0 = tree_to_poly_int64 (sz0);
16682 : }
16683 39307 : if (TREE_CODE (base1) == STRING_CST)
16684 : {
16685 12958 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16686 : equal = 2;
16687 : else
16688 : size1 = TREE_STRING_LENGTH (base1);
16689 : }
16690 26349 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16691 : size1 = 1;
16692 : else
16693 : {
16694 26033 : tree sz1 = DECL_SIZE_UNIT (base1);
16695 26033 : if (!tree_fits_poly_int64_p (sz1))
16696 : equal = 2;
16697 : else
16698 26033 : size1 = tree_to_poly_int64 (sz1);
16699 : }
16700 39307 : if (equal == 0)
16701 : {
16702 : /* If one offset is pointing (or could be) to the beginning of one
16703 : object and the other is pointing to one past the last byte of the
16704 : other object, punt. */
16705 39295 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16706 : equal = 2;
16707 39158 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16708 : equal = 2;
16709 : /* If both offsets are the same, there are some cases we know that are
16710 : ok. Either if we know they aren't zero, or if we know both sizes
16711 : are no zero. */
16712 : if (equal == 2
16713 273 : && known_eq (off0, off1)
16714 22 : && (known_ne (off0, 0)
16715 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16716 : equal = 0;
16717 : }
16718 :
16719 : /* At this point, equal is 2 if either one or both pointers are out of
16720 : bounds of their object, or one points to start of its object and the
16721 : other points to end of its object. This is unspecified behavior
16722 : e.g. in C++. Otherwise equal is 0. */
16723 39307 : if (folding_cxx_constexpr && equal)
16724 : return equal;
16725 :
16726 : /* When both pointers point to string literals, even when equal is 0,
16727 : due to tail merging of string literals the pointers might be the same. */
16728 39244 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16729 : {
16730 12814 : if (ioff0 < 0
16731 12814 : || ioff1 < 0
16732 12814 : || ioff0 > TREE_STRING_LENGTH (base0)
16733 25616 : || ioff1 > TREE_STRING_LENGTH (base1))
16734 : return 2;
16735 :
16736 : /* If the bytes in the string literals starting at the pointers
16737 : differ, the pointers need to be different. */
16738 12802 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16739 12802 : TREE_STRING_POINTER (base1) + ioff1,
16740 12802 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16741 : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16742 : {
16743 3908 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16744 3908 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16745 3908 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16746 : ioffmin) == 0)
16747 : /* If even the bytes in the string literal before the
16748 : pointers are the same, the string literals could be
16749 : tail merged. */
16750 : return 2;
16751 : }
16752 8906 : return 0;
16753 : }
16754 :
16755 26430 : if (folding_cxx_constexpr)
16756 : return 0;
16757 :
16758 : /* If this is a pointer comparison, ignore for now even
16759 : valid equalities where one pointer is the offset zero
16760 : of one object and the other to one past end of another one. */
16761 12641 : if (!INTEGRAL_TYPE_P (type))
16762 : return 0;
16763 :
16764 : /* Assume that string literals can't be adjacent to variables
16765 : (automatic or global). */
16766 309 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16767 : return 0;
16768 :
16769 : /* Assume that automatic variables can't be adjacent to global
16770 : variables. */
16771 289 : if (is_global_var (base0) != is_global_var (base1))
16772 2 : return 0;
16773 :
16774 : return equal;
16775 : }
16776 :
16777 : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16778 : tree
16779 54 : ctor_single_nonzero_element (const_tree t)
16780 : {
16781 54 : unsigned HOST_WIDE_INT idx;
16782 54 : constructor_elt *ce;
16783 54 : tree elt = NULL_TREE;
16784 :
16785 54 : if (TREE_CODE (t) != CONSTRUCTOR)
16786 : return NULL_TREE;
16787 117 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16788 114 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16789 : {
16790 105 : if (elt)
16791 : return NULL_TREE;
16792 54 : elt = ce->value;
16793 : }
16794 : return elt;
16795 : }
16796 :
16797 : #if CHECKING_P
16798 :
16799 : namespace selftest {
16800 :
16801 : /* Helper functions for writing tests of folding trees. */
16802 :
16803 : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16804 :
16805 : static void
16806 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16807 : tree constant)
16808 : {
16809 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16810 16 : }
16811 :
16812 : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16813 : wrapping WRAPPED_EXPR. */
16814 :
16815 : static void
16816 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16817 : tree wrapped_expr)
16818 : {
16819 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16820 12 : ASSERT_NE (wrapped_expr, result);
16821 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16822 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16823 12 : }
16824 :
16825 : /* Verify that various arithmetic binary operations are folded
16826 : correctly. */
16827 :
16828 : static void
16829 4 : test_arithmetic_folding ()
16830 : {
16831 4 : tree type = integer_type_node;
16832 4 : tree x = create_tmp_var_raw (type, "x");
16833 4 : tree zero = build_zero_cst (type);
16834 4 : tree one = build_int_cst (type, 1);
16835 :
16836 : /* Addition. */
16837 : /* 1 <-- (0 + 1) */
16838 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16839 : one);
16840 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16841 : one);
16842 :
16843 : /* (nonlvalue)x <-- (x + 0) */
16844 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16845 : x);
16846 :
16847 : /* Subtraction. */
16848 : /* 0 <-- (x - x) */
16849 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
16850 : zero);
16851 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16852 : x);
16853 :
16854 : /* Multiplication. */
16855 : /* 0 <-- (x * 0) */
16856 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
16857 : zero);
16858 :
16859 : /* (nonlvalue)x <-- (x * 1) */
16860 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16861 : x);
16862 4 : }
16863 :
16864 : namespace test_operand_equality {
16865 :
16866 : /* Verify structural equality. */
16867 :
16868 : /* Execute fold_vec_perm_cst unit tests. */
16869 :
16870 : static void
16871 4 : test ()
16872 : {
16873 4 : tree stype = integer_type_node;
16874 4 : tree utype = unsigned_type_node;
16875 4 : tree x = create_tmp_var_raw (stype, "x");
16876 4 : tree y = create_tmp_var_raw (stype, "y");
16877 4 : tree z = create_tmp_var_raw (stype, "z");
16878 4 : tree four = build_int_cst (stype, 4);
16879 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
16880 4 : tree rhs1 = fold_convert (stype,
16881 : fold_build2 (PLUS_EXPR, utype,
16882 : fold_convert (utype, x),
16883 : fold_convert (utype, y)));
16884 :
16885 : /* (int)((unsigned x) + (unsigned y)) == x + y. */
16886 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
16887 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
16888 :
16889 : /* (int)(unsigned) x == x. */
16890 4 : tree lhs2 = build1 (NOP_EXPR, stype,
16891 : build1 (NOP_EXPR, utype, x));
16892 4 : tree rhs2 = x;
16893 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
16894 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
16895 :
16896 : /* (unsigned x) + (unsigned y) == x + y. */
16897 4 : tree lhs3 = lhs1;
16898 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
16899 : fold_convert (utype, x),
16900 : fold_convert (utype, y));
16901 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
16902 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
16903 :
16904 : /* (unsigned x) / (unsigned y) == x / y. */
16905 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
16906 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
16907 : fold_convert (utype, x),
16908 : fold_convert (utype, y));
16909 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
16910 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
16911 :
16912 : /* (long x) / 4 == (long)(x / 4). */
16913 4 : tree lstype = long_long_integer_type_node;
16914 4 : tree lfour = build_int_cst (lstype, 4);
16915 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
16916 : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
16917 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
16918 : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
16919 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
16920 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
16921 :
16922 : /* (unsigned x) / 4 == x / 4. */
16923 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
16924 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
16925 : fold_convert (utype, x),
16926 : fold_convert (utype, four));
16927 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
16928 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
16929 :
16930 : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
16931 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
16932 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
16933 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
16934 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
16935 :
16936 : /* (unsigned x) + 4 == x + 4. */
16937 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
16938 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
16939 : fold_convert (utype, x),
16940 : fold_convert (utype, four));
16941 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
16942 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
16943 :
16944 : /* (unsigned x) + 4 == 4 + x. */
16945 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
16946 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
16947 : fold_convert (utype, x),
16948 : fold_convert (utype, four));
16949 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
16950 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
16951 :
16952 : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
16953 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
16954 : fold_build2 (MULT_EXPR, stype,
16955 : fold_build2 (PLUS_EXPR, stype, four, x),
16956 : y),
16957 : z);
16958 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
16959 : fold_build2 (PLUS_EXPR, utype,
16960 : fold_convert (utype, x),
16961 : fold_convert (utype, four)),
16962 : fold_convert (utype, y));
16963 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
16964 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
16965 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
16966 4 : }
16967 : }
16968 :
16969 : namespace test_fold_vec_perm_cst {
16970 :
16971 : /* Build a VECTOR_CST corresponding to VMODE, and has
16972 : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
16973 : Fill it with randomized elements, using rand() % THRESHOLD. */
16974 :
16975 : static tree
16976 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
16977 : unsigned nelts_per_pattern,
16978 : int step = 0, bool natural_stepped = false,
16979 : int threshold = 100)
16980 : {
16981 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
16982 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
16983 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
16984 :
16985 : // Fill a0 for each pattern
16986 0 : for (unsigned i = 0; i < npatterns; i++)
16987 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
16988 :
16989 0 : if (nelts_per_pattern == 1)
16990 0 : return builder.build ();
16991 :
16992 : // Fill a1 for each pattern
16993 0 : for (unsigned i = 0; i < npatterns; i++)
16994 : {
16995 0 : tree a1;
16996 0 : if (natural_stepped)
16997 : {
16998 0 : tree a0 = builder[i];
16999 0 : wide_int a0_val = wi::to_wide (a0);
17000 0 : wide_int a1_val = a0_val + step;
17001 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17002 0 : }
17003 : else
17004 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17005 0 : builder.quick_push (a1);
17006 : }
17007 0 : if (nelts_per_pattern == 2)
17008 0 : return builder.build ();
17009 :
17010 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17011 : {
17012 0 : tree prev_elem = builder[i - npatterns];
17013 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17014 0 : wide_int val = prev_elem_val + step;
17015 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17016 0 : }
17017 :
17018 0 : return builder.build ();
17019 0 : }
17020 :
17021 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17022 : when result is VLA. */
17023 :
17024 : static void
17025 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17026 : tree res, tree *expected_res)
17027 : {
17028 : /* Actual npatterns and encoded_elts in res may be less than expected due
17029 : to canonicalization. */
17030 0 : ASSERT_TRUE (res != NULL_TREE);
17031 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17032 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17033 :
17034 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17035 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17036 0 : }
17037 :
17038 : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17039 : when the result is VLS. */
17040 :
17041 : static void
17042 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17043 : {
17044 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17045 0 : for (unsigned i = 0; i < expected_nelts; i++)
17046 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17047 0 : }
17048 :
17049 : /* Helper routine to push multiple elements into BUILDER. */
17050 : template<unsigned N>
17051 0 : static void builder_push_elems (vec_perm_builder& builder,
17052 : poly_uint64 (&elems)[N])
17053 : {
17054 0 : for (unsigned i = 0; i < N; i++)
17055 0 : builder.quick_push (elems[i]);
17056 0 : }
17057 :
17058 : #define ARG0(index) vector_cst_elt (arg0, index)
17059 : #define ARG1(index) vector_cst_elt (arg1, index)
17060 :
17061 : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17062 :
17063 : static void
17064 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17065 : {
17066 0 : for (int i = 0; i < 10; i++)
17067 : {
17068 : /* Case 1:
17069 : sel = { 0, 4, 1, 5, ... }
17070 : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17071 0 : {
17072 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17073 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17074 :
17075 0 : tree inner_type
17076 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17077 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17078 :
17079 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17080 0 : vec_perm_builder builder (res_len, 4, 1);
17081 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17082 0 : builder_push_elems (builder, mask_elems);
17083 :
17084 0 : vec_perm_indices sel (builder, 2, 4);
17085 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17086 :
17087 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17088 0 : validate_res (4, 1, res, expected_res);
17089 0 : }
17090 :
17091 : /* Case 2: Same as case 1, but contains an out of bounds access which
17092 : should wrap around.
17093 : sel = {0, 8, 4, 12, ...} (4, 1)
17094 : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17095 0 : {
17096 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17097 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17098 :
17099 0 : tree inner_type
17100 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17101 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17102 :
17103 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17104 0 : vec_perm_builder builder (res_len, 4, 1);
17105 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17106 0 : builder_push_elems (builder, mask_elems);
17107 :
17108 0 : vec_perm_indices sel (builder, 2, 4);
17109 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17110 :
17111 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17112 0 : validate_res (4, 1, res, expected_res);
17113 0 : }
17114 : }
17115 0 : }
17116 :
17117 : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17118 :
17119 : static void
17120 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17121 : {
17122 0 : for (int i = 0; i < 10; i++)
17123 : {
17124 : /* Case 1:
17125 : sel = { 0, 1, 2, 3}
17126 : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17127 0 : {
17128 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17129 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17130 :
17131 0 : tree inner_type
17132 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17133 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17134 :
17135 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17136 0 : vec_perm_builder builder (res_len, 4, 1);
17137 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17138 0 : builder_push_elems (builder, mask_elems);
17139 :
17140 0 : vec_perm_indices sel (builder, 2,
17141 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17142 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17143 :
17144 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17145 0 : validate_res_vls (res, expected_res, 4);
17146 0 : }
17147 :
17148 : /* Case 2: Same as Case 1, but crossing input vector.
17149 : sel = {0, 2, 4, 6}
17150 : In this case,the index 4 is ambiguous since len = 4 + 4x.
17151 : Since we cannot determine, which vector to choose from during
17152 : compile time, should return NULL_TREE. */
17153 0 : {
17154 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17155 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17156 :
17157 0 : tree inner_type
17158 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17159 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17160 :
17161 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17162 0 : vec_perm_builder builder (res_len, 4, 1);
17163 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17164 0 : builder_push_elems (builder, mask_elems);
17165 :
17166 0 : vec_perm_indices sel (builder, 2,
17167 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)));
17168 0 : const char *reason;
17169 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17170 :
17171 0 : ASSERT_TRUE (res == NULL_TREE);
17172 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17173 0 : }
17174 : }
17175 0 : }
17176 :
17177 : /* Test all input vectors. */
17178 :
17179 : static void
17180 0 : test_all_nunits (machine_mode vmode)
17181 : {
17182 : /* Test with 10 different inputs. */
17183 0 : for (int i = 0; i < 10; i++)
17184 : {
17185 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17186 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17187 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17188 :
17189 : /* Case 1: mask = {0, ...} // (1, 1)
17190 : res = { arg0[0], ... } // (1, 1) */
17191 0 : {
17192 0 : vec_perm_builder builder (len, 1, 1);
17193 0 : builder.quick_push (0);
17194 0 : vec_perm_indices sel (builder, 2, len);
17195 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17196 0 : tree expected_res[] = { ARG0(0) };
17197 0 : validate_res (1, 1, res, expected_res);
17198 0 : }
17199 :
17200 : /* Case 2: mask = {len, ...} // (1, 1)
17201 : res = { arg1[0], ... } // (1, 1) */
17202 0 : {
17203 0 : vec_perm_builder builder (len, 1, 1);
17204 0 : builder.quick_push (len);
17205 0 : vec_perm_indices sel (builder, 2, len);
17206 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17207 :
17208 0 : tree expected_res[] = { ARG1(0) };
17209 0 : validate_res (1, 1, res, expected_res);
17210 0 : }
17211 : }
17212 0 : }
17213 :
17214 : /* Test all vectors which contain at-least 2 elements. */
17215 :
17216 : static void
17217 0 : test_nunits_min_2 (machine_mode vmode)
17218 : {
17219 0 : for (int i = 0; i < 10; i++)
17220 : {
17221 : /* Case 1: mask = { 0, len, ... } // (2, 1)
17222 : res = { arg0[0], arg1[0], ... } // (2, 1) */
17223 0 : {
17224 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17225 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17226 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17227 :
17228 0 : vec_perm_builder builder (len, 2, 1);
17229 0 : poly_uint64 mask_elems[] = { 0, len };
17230 0 : builder_push_elems (builder, mask_elems);
17231 :
17232 0 : vec_perm_indices sel (builder, 2, len);
17233 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17234 :
17235 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17236 0 : validate_res (2, 1, res, expected_res);
17237 0 : }
17238 :
17239 : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17240 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17241 0 : {
17242 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17243 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17244 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17245 :
17246 0 : vec_perm_builder builder (len, 2, 2);
17247 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17248 0 : builder_push_elems (builder, mask_elems);
17249 :
17250 0 : vec_perm_indices sel (builder, 2, len);
17251 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17252 :
17253 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17254 0 : validate_res (2, 2, res, expected_res);
17255 0 : }
17256 :
17257 : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17258 : Test that the stepped sequence of the pattern selects from
17259 : same input pattern. Since input vectors have npatterns = 2,
17260 : and step (a2 - a1) = 1, step is not a multiple of npatterns
17261 : in input vector. So return NULL_TREE. */
17262 0 : {
17263 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17264 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17265 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17266 :
17267 0 : vec_perm_builder builder (len, 1, 3);
17268 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17269 0 : builder_push_elems (builder, mask_elems);
17270 :
17271 0 : vec_perm_indices sel (builder, 2, len);
17272 0 : const char *reason;
17273 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17274 : &reason);
17275 0 : ASSERT_TRUE (res == NULL_TREE);
17276 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17277 0 : }
17278 :
17279 : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17280 : Test that stepped sequence of the pattern selects from arg0.
17281 : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17282 0 : {
17283 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17284 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17285 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17286 :
17287 0 : vec_perm_builder builder (len, 1, 3);
17288 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17289 0 : builder_push_elems (builder, mask_elems);
17290 :
17291 0 : vec_perm_indices sel (builder, 2, len);
17292 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17293 :
17294 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17295 0 : validate_res (1, 3, res, expected_res);
17296 0 : }
17297 :
17298 : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17299 : In this case ensure that arg has a natural stepped sequence
17300 : to preserve arg's encoding.
17301 :
17302 : As a concrete example, consider:
17303 : arg0: { -16, -9, -10, ... } // (1, 3)
17304 : arg1: { -12, -5, -6, ... } // (1, 3)
17305 : sel = { 0, len, len + 1, ... } // (1, 3)
17306 :
17307 : This will create res with following encoding:
17308 : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17309 : = { -16, -12, -5, ... }
17310 :
17311 : The step in above encoding would be: (-5) - (-12) = 7
17312 : And hence res[3] would be computed as -5 + 7 = 2.
17313 : instead of arg1[2], ie, -6.
17314 : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17315 : for this case. */
17316 0 : {
17317 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17318 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17319 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17320 :
17321 0 : vec_perm_builder builder (len, 1, 3);
17322 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17323 0 : builder_push_elems (builder, mask_elems);
17324 :
17325 0 : vec_perm_indices sel (builder, 2, len);
17326 0 : const char *reason;
17327 : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17328 : stepped pattern, even if we didn't explicitly tell it to. So folding
17329 : may not always fail, but if it does, ensure that's because arg1 does
17330 : not have a natural stepped sequence (and not due to other reason) */
17331 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17332 0 : if (res == NULL_TREE)
17333 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17334 0 : }
17335 :
17336 : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17337 : sequence and thus folding should be valid for this case. */
17338 0 : {
17339 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17340 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17341 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17342 :
17343 0 : vec_perm_builder builder (len, 1, 3);
17344 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17345 0 : builder_push_elems (builder, mask_elems);
17346 :
17347 0 : vec_perm_indices sel (builder, 2, len);
17348 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17349 :
17350 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17351 0 : validate_res (1, 3, res, expected_res);
17352 0 : }
17353 :
17354 : /* Case 8: Same as aarch64/sve/slp_3.c:
17355 : arg0, arg1 are dup vectors.
17356 : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17357 : So res = { arg0[0], arg1[0], ... } // (2, 1)
17358 :
17359 : In this case, since the input vectors are dup, only the first two
17360 : elements per pattern in sel are considered significant. */
17361 0 : {
17362 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17363 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17364 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17365 :
17366 0 : vec_perm_builder builder (len, 2, 3);
17367 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17368 0 : builder_push_elems (builder, mask_elems);
17369 :
17370 0 : vec_perm_indices sel (builder, 2, len);
17371 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17372 :
17373 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17374 0 : validate_res (2, 1, res, expected_res);
17375 0 : }
17376 : }
17377 0 : }
17378 :
17379 : /* Test all vectors which contain at-least 4 elements. */
17380 :
17381 : static void
17382 0 : test_nunits_min_4 (machine_mode vmode)
17383 : {
17384 0 : for (int i = 0; i < 10; i++)
17385 : {
17386 : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17387 : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17388 0 : {
17389 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17390 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17391 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17392 :
17393 0 : vec_perm_builder builder (len, 4, 1);
17394 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17395 0 : builder_push_elems (builder, mask_elems);
17396 :
17397 0 : vec_perm_indices sel (builder, 2, len);
17398 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17399 :
17400 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17401 0 : validate_res (4, 1, res, expected_res);
17402 0 : }
17403 :
17404 : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17405 : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17406 0 : {
17407 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17408 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17409 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17410 :
17411 0 : vec_perm_builder builder (arg0_len, 1, 3);
17412 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17413 0 : builder_push_elems (builder, mask_elems);
17414 :
17415 0 : vec_perm_indices sel (builder, 2, arg0_len);
17416 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17417 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17418 0 : validate_res (1, 3, res, expected_res);
17419 0 : }
17420 :
17421 : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17422 : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17423 0 : {
17424 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17425 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17426 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17427 :
17428 0 : vec_perm_builder builder (len, 1, 3);
17429 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17430 0 : builder_push_elems (builder, mask_elems);
17431 :
17432 0 : vec_perm_indices sel (builder, 2, len);
17433 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17434 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17435 0 : validate_res (1, 3, res, expected_res);
17436 0 : }
17437 :
17438 : /* Case 4:
17439 : sel = { len, 0, 2, ... } // (1, 3)
17440 : This should return NULL because we cross the input vectors.
17441 : Because,
17442 : Let's assume len = C + Cx
17443 : a1 = 0
17444 : S = 2
17445 : esel = arg0_len / sel_npatterns = C + Cx
17446 : ae = 0 + (esel - 2) * S
17447 : = 0 + (C + Cx - 2) * 2
17448 : = 2(C-2) + 2Cx
17449 :
17450 : For C >= 4:
17451 : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17452 : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17453 : Since q1 != qe, we cross input vectors.
17454 : So return NULL_TREE. */
17455 0 : {
17456 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17457 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17458 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17459 :
17460 0 : vec_perm_builder builder (arg0_len, 1, 3);
17461 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17462 0 : builder_push_elems (builder, mask_elems);
17463 :
17464 0 : vec_perm_indices sel (builder, 2, arg0_len);
17465 0 : const char *reason;
17466 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17467 0 : ASSERT_TRUE (res == NULL_TREE);
17468 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17469 0 : }
17470 :
17471 : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17472 : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17473 : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17474 :
17475 : Note that fold_vec_perm_cst will set
17476 : res_npatterns = max(4, max(4, 2)) = 4
17477 : However after canonicalizing, we will end up with shape (2, 2). */
17478 0 : {
17479 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17480 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17481 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17482 :
17483 0 : vec_perm_builder builder (len, 2, 2);
17484 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17485 0 : builder_push_elems (builder, mask_elems);
17486 :
17487 0 : vec_perm_indices sel (builder, 2, len);
17488 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17489 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17490 0 : validate_res (2, 2, res, expected_res);
17491 0 : }
17492 :
17493 : /* Case 6: Test combination in sel, where one pattern is dup and other
17494 : is stepped sequence.
17495 : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17496 : res = { arg0[0], arg0[0], arg0[0],
17497 : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17498 0 : {
17499 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17500 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17501 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17502 :
17503 0 : vec_perm_builder builder (len, 2, 3);
17504 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17505 0 : builder_push_elems (builder, mask_elems);
17506 :
17507 0 : vec_perm_indices sel (builder, 2, len);
17508 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17509 :
17510 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17511 0 : ARG0(1), ARG0(0), ARG0(2) };
17512 0 : validate_res (2, 3, res, expected_res);
17513 0 : }
17514 :
17515 : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17516 : when arg0, arg1 and sel have different number of patterns.
17517 : arg0 is of shape (1, 1)
17518 : arg1 is of shape (4, 1)
17519 : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17520 :
17521 : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17522 : However,
17523 : step = (len+2) - (len+1) = 1
17524 : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17525 : Since step is not a multiple of arg_npatterns,
17526 : valid_mask_for_fold_vec_perm_cst should return false,
17527 : and thus fold_vec_perm_cst should return NULL_TREE. */
17528 0 : {
17529 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17530 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17531 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17532 :
17533 0 : vec_perm_builder builder (len, 2, 3);
17534 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17535 0 : builder_push_elems (builder, mask_elems);
17536 :
17537 0 : vec_perm_indices sel (builder, 2, len);
17538 0 : const char *reason;
17539 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17540 :
17541 0 : ASSERT_TRUE (res == NULL_TREE);
17542 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17543 0 : }
17544 :
17545 : /* Case 8: PR111754: When input vector is not a stepped sequence,
17546 : check that the result is not a stepped sequence either, even
17547 : if sel has a stepped sequence. */
17548 0 : {
17549 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17550 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17551 :
17552 0 : vec_perm_builder builder (len, 1, 3);
17553 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17554 0 : builder_push_elems (builder, mask_elems);
17555 :
17556 0 : vec_perm_indices sel (builder, 1, len);
17557 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17558 :
17559 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17560 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17561 0 : }
17562 :
17563 : /* Case 9: If sel doesn't contain a stepped sequence,
17564 : check that the result has same encoding as sel, irrespective
17565 : of shape of input vectors. */
17566 0 : {
17567 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17568 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17569 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17570 :
17571 0 : vec_perm_builder builder (len, 1, 2);
17572 0 : poly_uint64 mask_elems[] = { 0, len };
17573 0 : builder_push_elems (builder, mask_elems);
17574 :
17575 0 : vec_perm_indices sel (builder, 2, len);
17576 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17577 :
17578 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17579 0 : validate_res (sel.encoding ().npatterns (),
17580 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17581 0 : }
17582 : }
17583 0 : }
17584 :
17585 : /* Test all vectors which contain at-least 8 elements. */
17586 :
17587 : static void
17588 0 : test_nunits_min_8 (machine_mode vmode)
17589 : {
17590 0 : for (int i = 0; i < 10; i++)
17591 : {
17592 : /* Case 1: sel_npatterns (4) > input npatterns (2)
17593 : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17594 : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17595 : arg0[2], arg0[0], arg0[3], arg1[0],
17596 : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17597 0 : {
17598 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17599 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17600 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17601 :
17602 0 : vec_perm_builder builder(len, 4, 3);
17603 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17604 0 : 4, 0, 5, len };
17605 0 : builder_push_elems (builder, mask_elems);
17606 :
17607 0 : vec_perm_indices sel (builder, 2, len);
17608 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17609 :
17610 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17611 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17612 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17613 0 : validate_res (4, 3, res, expected_res);
17614 0 : }
17615 : }
17616 0 : }
17617 :
17618 : /* Test vectors for which nunits[0] <= 4. */
17619 :
17620 : static void
17621 0 : test_nunits_max_4 (machine_mode vmode)
17622 : {
17623 : /* Case 1: mask = {0, 4, ...} // (1, 2)
17624 : This should return NULL_TREE because the index 4 may choose
17625 : from either arg0 or arg1 depending on vector length. */
17626 0 : {
17627 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17628 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17629 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17630 :
17631 0 : vec_perm_builder builder (len, 1, 2);
17632 0 : poly_uint64 mask_elems[] = {0, 4};
17633 0 : builder_push_elems (builder, mask_elems);
17634 :
17635 0 : vec_perm_indices sel (builder, 2, len);
17636 0 : const char *reason;
17637 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17638 0 : ASSERT_TRUE (res == NULL_TREE);
17639 0 : ASSERT_TRUE (reason != NULL);
17640 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17641 0 : }
17642 0 : }
17643 :
17644 : #undef ARG0
17645 : #undef ARG1
17646 :
17647 : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17648 :
17649 : static bool
17650 0 : is_simple_vla_size (poly_uint64 size)
17651 : {
17652 124 : if (size.is_constant ()
17653 : || !pow2p_hwi (size.coeffs[0]))
17654 0 : return false;
17655 : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17656 : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17657 : return false;
17658 : return true;
17659 : }
17660 :
17661 : /* Execute fold_vec_perm_cst unit tests. */
17662 :
17663 : static void
17664 4 : test ()
17665 : {
17666 4 : machine_mode vnx4si_mode = E_VOIDmode;
17667 4 : machine_mode v4si_mode = E_VOIDmode;
17668 :
17669 4 : machine_mode vmode;
17670 128 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17671 : {
17672 : /* Obtain modes corresponding to VNx4SI and V4SI,
17673 : to call mixed mode tests below.
17674 : FIXME: Is there a better way to do this ? */
17675 124 : if (GET_MODE_INNER (vmode) == SImode)
17676 : {
17677 124 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17678 124 : if (is_simple_vla_size (nunits)
17679 : && nunits.coeffs[0] == 4)
17680 : vnx4si_mode = vmode;
17681 124 : else if (known_eq (nunits, poly_uint64 (4)))
17682 124 : v4si_mode = vmode;
17683 : }
17684 :
17685 124 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17686 : || !targetm.vector_mode_supported_p (vmode))
17687 124 : continue;
17688 :
17689 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17690 : test_all_nunits (vmode);
17691 : if (nunits.coeffs[0] >= 2)
17692 : test_nunits_min_2 (vmode);
17693 : if (nunits.coeffs[0] >= 4)
17694 : test_nunits_min_4 (vmode);
17695 : if (nunits.coeffs[0] >= 8)
17696 : test_nunits_min_8 (vmode);
17697 :
17698 : if (nunits.coeffs[0] <= 4)
17699 : test_nunits_max_4 (vmode);
17700 : }
17701 :
17702 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17703 : && targetm.vector_mode_supported_p (vnx4si_mode)
17704 : && targetm.vector_mode_supported_p (v4si_mode))
17705 : {
17706 : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17707 : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17708 : }
17709 4 : }
17710 : } // end of test_fold_vec_perm_cst namespace
17711 :
17712 : /* Verify that various binary operations on vectors are folded
17713 : correctly. */
17714 :
17715 : static void
17716 4 : test_vector_folding ()
17717 : {
17718 4 : tree inner_type = integer_type_node;
17719 4 : tree type = build_vector_type (inner_type, 4);
17720 4 : tree zero = build_zero_cst (type);
17721 4 : tree one = build_one_cst (type);
17722 4 : tree index = build_index_vector (type, 0, 1);
17723 :
17724 : /* Verify equality tests that return a scalar boolean result. */
17725 4 : tree res_type = boolean_type_node;
17726 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17727 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17728 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17729 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17730 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17731 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17732 : index, one)));
17733 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17734 : index, index)));
17735 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17736 : index, index)));
17737 4 : }
17738 :
17739 : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17740 :
17741 : static void
17742 4 : test_vec_duplicate_folding ()
17743 : {
17744 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17745 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17746 : /* This will be 1 if VEC_MODE isn't a vector mode. */
17747 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17748 :
17749 4 : tree type = build_vector_type (ssizetype, nunits);
17750 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17751 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17752 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17753 4 : }
17754 :
17755 : /* Run all of the selftests within this file. */
17756 :
17757 : void
17758 4 : fold_const_cc_tests ()
17759 : {
17760 4 : test_arithmetic_folding ();
17761 4 : test_vector_folding ();
17762 4 : test_vec_duplicate_folding ();
17763 4 : test_fold_vec_perm_cst::test ();
17764 4 : test_operand_equality::test ();
17765 4 : }
17766 :
17767 : } // namespace selftest
17768 :
17769 : #endif /* CHECKING_P */
|