Branch data Line data Source code
1 : : /* Fold a constant sub-tree into a single node for C-compiler
2 : : Copyright (C) 1987-2025 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_wide and size_binop.
29 : :
30 : : fold takes a tree as argument and returns a simplified tree.
31 : :
32 : : size_binop takes a tree code for an arithmetic operation
33 : : and two operands that are trees, and produces a tree for the
34 : : result, assuming the type comes from `sizetype'.
35 : :
36 : : size_int takes an integer value, and creates a tree constant
37 : : with type from `sizetype'.
38 : :
39 : : Note: Since the folders get called on non-gimple code as well as
40 : : gimple code, we need to handle GIMPLE tuples as well as their
41 : : corresponding tree equivalents. */
42 : :
43 : : #define INCLUDE_ALGORITHM
44 : : #include "config.h"
45 : : #include "system.h"
46 : : #include "coretypes.h"
47 : : #include "backend.h"
48 : : #include "target.h"
49 : : #include "rtl.h"
50 : : #include "tree.h"
51 : : #include "gimple.h"
52 : : #include "predict.h"
53 : : #include "memmodel.h"
54 : : #include "tm_p.h"
55 : : #include "tree-ssa-operands.h"
56 : : #include "optabs-query.h"
57 : : #include "cgraph.h"
58 : : #include "diagnostic-core.h"
59 : : #include "flags.h"
60 : : #include "alias.h"
61 : : #include "fold-const.h"
62 : : #include "fold-const-call.h"
63 : : #include "stor-layout.h"
64 : : #include "calls.h"
65 : : #include "tree-iterator.h"
66 : : #include "expr.h"
67 : : #include "intl.h"
68 : : #include "langhooks.h"
69 : : #include "tree-eh.h"
70 : : #include "gimplify.h"
71 : : #include "tree-dfa.h"
72 : : #include "builtins.h"
73 : : #include "generic-match.h"
74 : : #include "gimple-iterator.h"
75 : : #include "gimple-fold.h"
76 : : #include "tree-into-ssa.h"
77 : : #include "md5.h"
78 : : #include "case-cfn-macros.h"
79 : : #include "stringpool.h"
80 : : #include "tree-vrp.h"
81 : : #include "tree-ssanames.h"
82 : : #include "selftest.h"
83 : : #include "stringpool.h"
84 : : #include "attribs.h"
85 : : #include "tree-vector-builder.h"
86 : : #include "vec-perm-indices.h"
87 : : #include "asan.h"
88 : : #include "gimple-range.h"
89 : :
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 : : /* The following constants represent a bit based encoding of GCC's
103 : : comparison operators. This encoding simplifies transformations
104 : : on relational comparison operators, such as AND and OR. */
105 : : enum comparison_code {
106 : : COMPCODE_FALSE = 0,
107 : : COMPCODE_LT = 1,
108 : : COMPCODE_EQ = 2,
109 : : COMPCODE_LE = 3,
110 : : COMPCODE_GT = 4,
111 : : COMPCODE_LTGT = 5,
112 : : COMPCODE_GE = 6,
113 : : COMPCODE_ORD = 7,
114 : : COMPCODE_UNORD = 8,
115 : : COMPCODE_UNLT = 9,
116 : : COMPCODE_UNEQ = 10,
117 : : COMPCODE_UNLE = 11,
118 : : COMPCODE_UNGT = 12,
119 : : COMPCODE_NE = 13,
120 : : COMPCODE_UNGE = 14,
121 : : COMPCODE_TRUE = 15
122 : : };
123 : :
124 : : static bool negate_expr_p (tree);
125 : : static tree negate_expr (tree);
126 : : static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
127 : : static enum comparison_code comparison_to_compcode (enum tree_code);
128 : : static enum tree_code compcode_to_comparison (enum comparison_code);
129 : : static bool twoval_comparison_p (tree, tree *, tree *);
130 : : static tree eval_subst (location_t, tree, tree, tree, tree, tree);
131 : : static tree optimize_bit_field_compare (location_t, enum tree_code,
132 : : tree, tree, tree);
133 : : static bool simple_operand_p (const_tree);
134 : : static tree range_binop (enum tree_code, tree, tree, int, tree, int);
135 : : static tree range_predecessor (tree);
136 : : static tree range_successor (tree);
137 : : static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
138 : : static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
139 : : tree, tree, tree, tree);
140 : : static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
141 : : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
142 : : static tree fold_binary_op_with_conditional_arg (location_t,
143 : : enum tree_code, tree,
144 : : tree, tree,
145 : : tree, tree, int);
146 : : static tree fold_negate_const (tree, tree);
147 : : static tree fold_not_const (const_tree, tree);
148 : : static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 : : static tree fold_convert_const (enum tree_code, tree, tree);
150 : : static tree fold_view_convert_expr (tree, tree);
151 : : static tree fold_negate_expr (location_t, tree);
152 : :
153 : : /* This is a helper function to detect min/max for some operands of COND_EXPR.
154 : : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
155 : : tree_code
156 : 138869 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
157 : : {
158 : 138869 : enum tree_code code = ERROR_MARK;
159 : :
160 : 138869 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
161 : 11 : return ERROR_MARK;
162 : :
163 : 138858 : if (!operand_equal_p (exp0, exp2))
164 : : return ERROR_MARK;
165 : :
166 : 138858 : if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST)
167 : : {
168 : 136210 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
169 : : {
170 : : /* X <= Y - 1 equals to X < Y. */
171 : 77637 : if (cmp == LE_EXPR)
172 : : code = LT_EXPR;
173 : : /* X > Y - 1 equals to X >= Y. */
174 : 77274 : if (cmp == GT_EXPR)
175 : : code = GE_EXPR;
176 : : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
177 : 67866 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
178 : : {
179 : 16965 : int_range_max r;
180 : 33930 : get_range_query (cfun)->range_of_expr (r, exp0);
181 : 16965 : if (r.undefined_p ())
182 : 0 : r.set_varying (TREE_TYPE (exp0));
183 : :
184 : 16965 : widest_int min = widest_int::from (r.lower_bound (),
185 : 33930 : TYPE_SIGN (TREE_TYPE (exp0)));
186 : 16965 : if (min == wi::to_widest (exp1))
187 : 736 : code = MAX_EXPR;
188 : 16965 : }
189 : : }
190 : 136210 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
191 : : {
192 : : /* X < Y + 1 equals to X <= Y. */
193 : 1007 : if (cmp == LT_EXPR)
194 : : code = LE_EXPR;
195 : : /* X >= Y + 1 equals to X > Y. */
196 : 979 : if (cmp == GE_EXPR)
197 : : code = GT_EXPR;
198 : : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
199 : 882 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
200 : : {
201 : 495 : int_range_max r;
202 : 990 : get_range_query (cfun)->range_of_expr (r, exp0);
203 : 495 : if (r.undefined_p ())
204 : 0 : r.set_varying (TREE_TYPE (exp0));
205 : :
206 : 495 : widest_int max = widest_int::from (r.upper_bound (),
207 : 990 : TYPE_SIGN (TREE_TYPE (exp0)));
208 : 495 : if (max == wi::to_widest (exp1))
209 : 43 : code = MIN_EXPR;
210 : 495 : }
211 : : }
212 : : }
213 : 136210 : if (code != ERROR_MARK
214 : 138858 : || operand_equal_p (exp1, exp3))
215 : : {
216 : 24270 : if (cmp == LT_EXPR || cmp == LE_EXPR)
217 : : code = MIN_EXPR;
218 : 21890 : if (cmp == GT_EXPR || cmp == GE_EXPR)
219 : 20988 : code = MAX_EXPR;
220 : : }
221 : : return code;
222 : : }
223 : :
224 : : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
225 : : Otherwise, return LOC. */
226 : :
227 : : static location_t
228 : 2371822 : expr_location_or (tree t, location_t loc)
229 : : {
230 : 658861 : location_t tloc = EXPR_LOCATION (t);
231 : 2357450 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
232 : : }
233 : :
234 : : /* Similar to protected_set_expr_location, but never modify x in place,
235 : : if location can and needs to be set, unshare it. */
236 : :
237 : : tree
238 : 4539053 : protected_set_expr_location_unshare (tree x, location_t loc)
239 : : {
240 : 4539053 : if (CAN_HAVE_LOCATION_P (x)
241 : 4025410 : && EXPR_LOCATION (x) != loc
242 : 1817941 : && !(TREE_CODE (x) == SAVE_EXPR
243 : 909191 : || TREE_CODE (x) == TARGET_EXPR
244 : : || TREE_CODE (x) == BIND_EXPR))
245 : : {
246 : 908438 : x = copy_node (x);
247 : 908438 : SET_EXPR_LOCATION (x, loc);
248 : : }
249 : 4539053 : return x;
250 : : }
251 : :
252 : : /* If ARG2 divides ARG1 with zero remainder, carries out the exact
253 : : division and returns the quotient. Otherwise returns
254 : : NULL_TREE. */
255 : :
256 : : tree
257 : 0 : div_if_zero_remainder (const_tree arg1, const_tree arg2)
258 : : {
259 : 0 : widest_int quo;
260 : :
261 : 0 : if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
262 : : SIGNED, &quo))
263 : 0 : return wide_int_to_tree (TREE_TYPE (arg1), quo);
264 : :
265 : : return NULL_TREE;
266 : 0 : }
267 : :
268 : : /* This is nonzero if we should defer warnings about undefined
269 : : overflow. This facility exists because these warnings are a
270 : : special case. The code to estimate loop iterations does not want
271 : : to issue any warnings, since it works with expressions which do not
272 : : occur in user code. Various bits of cleanup code call fold(), but
273 : : only use the result if it has certain characteristics (e.g., is a
274 : : constant); that code only wants to issue a warning if the result is
275 : : used. */
276 : :
277 : : static int fold_deferring_overflow_warnings;
278 : :
279 : : /* If a warning about undefined overflow is deferred, this is the
280 : : warning. Note that this may cause us to turn two warnings into
281 : : one, but that is fine since it is sufficient to only give one
282 : : warning per expression. */
283 : :
284 : : static const char* fold_deferred_overflow_warning;
285 : :
286 : : /* If a warning about undefined overflow is deferred, this is the
287 : : level at which the warning should be emitted. */
288 : :
289 : : static enum warn_strict_overflow_code fold_deferred_overflow_code;
290 : :
291 : : /* Start deferring overflow warnings. We could use a stack here to
292 : : permit nested calls, but at present it is not necessary. */
293 : :
294 : : void
295 : 1182035999 : fold_defer_overflow_warnings (void)
296 : : {
297 : 1182035999 : ++fold_deferring_overflow_warnings;
298 : 1182035999 : }
299 : :
300 : : /* Stop deferring overflow warnings. If there is a pending warning,
301 : : and ISSUE is true, then issue the warning if appropriate. STMT is
302 : : the statement with which the warning should be associated (used for
303 : : location information); STMT may be NULL. CODE is the level of the
304 : : warning--a warn_strict_overflow_code value. This function will use
305 : : the smaller of CODE and the deferred code when deciding whether to
306 : : issue the warning. CODE may be zero to mean to always use the
307 : : deferred code. */
308 : :
309 : : void
310 : 1182035999 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
311 : : {
312 : 1182035999 : const char *warnmsg;
313 : 1182035999 : location_t locus;
314 : :
315 : 1182035999 : gcc_assert (fold_deferring_overflow_warnings > 0);
316 : 1182035999 : --fold_deferring_overflow_warnings;
317 : 1182035999 : if (fold_deferring_overflow_warnings > 0)
318 : : {
319 : 8795795 : if (fold_deferred_overflow_warning != NULL
320 : 1788220 : && code != 0
321 : 0 : && code < (int) fold_deferred_overflow_code)
322 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
323 : 8795795 : return;
324 : : }
325 : :
326 : 1173240204 : warnmsg = fold_deferred_overflow_warning;
327 : 1173240204 : fold_deferred_overflow_warning = NULL;
328 : :
329 : 1173240204 : if (!issue || warnmsg == NULL)
330 : : return;
331 : :
332 : 10874 : if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
333 : : return;
334 : :
335 : : /* Use the smallest code level when deciding to issue the
336 : : warning. */
337 : 10874 : if (code == 0 || code > (int) fold_deferred_overflow_code)
338 : 10874 : code = fold_deferred_overflow_code;
339 : :
340 : 10874 : if (!issue_strict_overflow_warning (code))
341 : : return;
342 : :
343 : 0 : if (stmt == NULL)
344 : : locus = input_location;
345 : : else
346 : 0 : locus = gimple_location (stmt);
347 : 0 : warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
348 : : }
349 : :
350 : : /* Stop deferring overflow warnings, ignoring any deferred
351 : : warnings. */
352 : :
353 : : void
354 : 181127495 : fold_undefer_and_ignore_overflow_warnings (void)
355 : : {
356 : 181127495 : fold_undefer_overflow_warnings (false, NULL, 0);
357 : 181127495 : }
358 : :
359 : : /* Whether we are deferring overflow warnings. */
360 : :
361 : : bool
362 : 316021393 : fold_deferring_overflow_warnings_p (void)
363 : : {
364 : 316021393 : return fold_deferring_overflow_warnings > 0;
365 : : }
366 : :
367 : : /* This is called when we fold something based on the fact that signed
368 : : overflow is undefined. */
369 : :
370 : : void
371 : 1763097 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
372 : : {
373 : 1763097 : if (fold_deferring_overflow_warnings > 0)
374 : : {
375 : 1697630 : if (fold_deferred_overflow_warning == NULL
376 : 751534 : || wc < fold_deferred_overflow_code)
377 : : {
378 : 968946 : fold_deferred_overflow_warning = gmsgid;
379 : 968946 : fold_deferred_overflow_code = wc;
380 : : }
381 : : }
382 : 65467 : else if (issue_strict_overflow_warning (wc))
383 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
384 : 1763097 : }
385 : :
386 : : /* Return true if the built-in mathematical function specified by CODE
387 : : is odd, i.e. -f(x) == f(-x). */
388 : :
389 : : bool
390 : 2006939 : negate_mathfn_p (combined_fn fn)
391 : : {
392 : 2006939 : switch (fn)
393 : : {
394 : : CASE_CFN_ASIN:
395 : : CASE_CFN_ASIN_FN:
396 : : CASE_CFN_ASINH:
397 : : CASE_CFN_ASINH_FN:
398 : : CASE_CFN_ATAN:
399 : : CASE_CFN_ATAN_FN:
400 : : CASE_CFN_ATANH:
401 : : CASE_CFN_ATANH_FN:
402 : : CASE_CFN_CASIN:
403 : : CASE_CFN_CASIN_FN:
404 : : CASE_CFN_CASINH:
405 : : CASE_CFN_CASINH_FN:
406 : : CASE_CFN_CATAN:
407 : : CASE_CFN_CATAN_FN:
408 : : CASE_CFN_CATANH:
409 : : CASE_CFN_CATANH_FN:
410 : : CASE_CFN_CBRT:
411 : : CASE_CFN_CBRT_FN:
412 : : CASE_CFN_CPROJ:
413 : : CASE_CFN_CPROJ_FN:
414 : : CASE_CFN_CSIN:
415 : : CASE_CFN_CSIN_FN:
416 : : CASE_CFN_CSINH:
417 : : CASE_CFN_CSINH_FN:
418 : : CASE_CFN_CTAN:
419 : : CASE_CFN_CTAN_FN:
420 : : CASE_CFN_CTANH:
421 : : CASE_CFN_CTANH_FN:
422 : : CASE_CFN_ERF:
423 : : CASE_CFN_ERF_FN:
424 : : CASE_CFN_LLROUND:
425 : : CASE_CFN_LLROUND_FN:
426 : : CASE_CFN_LROUND:
427 : : CASE_CFN_LROUND_FN:
428 : : CASE_CFN_ROUND:
429 : : CASE_CFN_ROUNDEVEN:
430 : : CASE_CFN_ROUNDEVEN_FN:
431 : : CASE_CFN_SIN:
432 : : CASE_CFN_SIN_FN:
433 : : CASE_CFN_SINH:
434 : : CASE_CFN_SINH_FN:
435 : : CASE_CFN_TAN:
436 : : CASE_CFN_TAN_FN:
437 : : CASE_CFN_TANH:
438 : : CASE_CFN_TANH_FN:
439 : : CASE_CFN_TRUNC:
440 : : CASE_CFN_TRUNC_FN:
441 : : return true;
442 : :
443 : 390 : CASE_CFN_LLRINT:
444 : 390 : CASE_CFN_LLRINT_FN:
445 : 390 : CASE_CFN_LRINT:
446 : 390 : CASE_CFN_LRINT_FN:
447 : 390 : CASE_CFN_NEARBYINT:
448 : 390 : CASE_CFN_NEARBYINT_FN:
449 : 390 : CASE_CFN_RINT:
450 : 390 : CASE_CFN_RINT_FN:
451 : 390 : return !flag_rounding_math;
452 : :
453 : 2002957 : default:
454 : 2002957 : break;
455 : : }
456 : 2002957 : return false;
457 : : }
458 : :
459 : : /* Check whether we may negate an integer constant T without causing
460 : : overflow. */
461 : :
462 : : bool
463 : 3025320 : may_negate_without_overflow_p (const_tree t)
464 : : {
465 : 3025320 : tree type;
466 : :
467 : 3025320 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
468 : :
469 : 3025320 : type = TREE_TYPE (t);
470 : 3025320 : if (TYPE_UNSIGNED (type))
471 : : return false;
472 : :
473 : 3025320 : return !wi::only_sign_bit_p (wi::to_wide (t));
474 : : }
475 : :
476 : : /* Determine whether an expression T can be cheaply negated using
477 : : the function negate_expr without introducing undefined overflow. */
478 : :
479 : : static bool
480 : 26218588 : negate_expr_p (tree t)
481 : : {
482 : 26370433 : tree type;
483 : :
484 : 26370433 : if (t == 0)
485 : : return false;
486 : :
487 : 26370433 : type = TREE_TYPE (t);
488 : :
489 : 26370433 : STRIP_SIGN_NOPS (t);
490 : 26370433 : switch (TREE_CODE (t))
491 : : {
492 : 1515730 : case INTEGER_CST:
493 : 1515730 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
494 : : return true;
495 : :
496 : : /* Check that -CST will not overflow type. */
497 : 365113 : return may_negate_without_overflow_p (t);
498 : 515 : case BIT_NOT_EXPR:
499 : 515 : return (INTEGRAL_TYPE_P (type)
500 : 515 : && TYPE_OVERFLOW_WRAPS (type));
501 : :
502 : : case FIXED_CST:
503 : : return true;
504 : :
505 : 1308 : case NEGATE_EXPR:
506 : 1308 : return !TYPE_OVERFLOW_SANITIZED (type);
507 : :
508 : 1323405 : case REAL_CST:
509 : : /* We want to canonicalize to positive real constants. Pretend
510 : : that only negative ones can be easily negated. */
511 : 1323405 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
512 : :
513 : 454 : case COMPLEX_CST:
514 : 454 : return negate_expr_p (TREE_REALPART (t))
515 : 572 : && negate_expr_p (TREE_IMAGPART (t));
516 : :
517 : 97 : case VECTOR_CST:
518 : 97 : {
519 : 97 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
520 : : return true;
521 : :
522 : : /* Steps don't prevent negation. */
523 : 97 : unsigned int count = vector_cst_encoded_nelts (t);
524 : 194 : for (unsigned int i = 0; i < count; ++i)
525 : 97 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
526 : : return false;
527 : :
528 : : return true;
529 : : }
530 : :
531 : 705 : case COMPLEX_EXPR:
532 : 705 : return negate_expr_p (TREE_OPERAND (t, 0))
533 : 705 : && negate_expr_p (TREE_OPERAND (t, 1));
534 : :
535 : 33 : case CONJ_EXPR:
536 : 33 : return negate_expr_p (TREE_OPERAND (t, 0));
537 : :
538 : 1462929 : case PLUS_EXPR:
539 : 1462929 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
540 : 1462923 : || HONOR_SIGNED_ZEROS (type)
541 : 2630599 : || (ANY_INTEGRAL_TYPE_P (type)
542 : 1167484 : && ! TYPE_OVERFLOW_WRAPS (type)))
543 : 716175 : return false;
544 : : /* -(A + B) -> (-B) - A. */
545 : 746754 : if (negate_expr_p (TREE_OPERAND (t, 1)))
546 : : return true;
547 : : /* -(A + B) -> (-A) - B. */
548 : 138237 : return negate_expr_p (TREE_OPERAND (t, 0));
549 : :
550 : 249715 : case MINUS_EXPR:
551 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
552 : 249715 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
553 : 249715 : && !HONOR_SIGNED_ZEROS (type)
554 : 321234 : && (! ANY_INTEGRAL_TYPE_P (type)
555 : 71296 : || TYPE_OVERFLOW_WRAPS (type));
556 : :
557 : 2390055 : case MULT_EXPR:
558 : 2390055 : if (TYPE_UNSIGNED (type))
559 : : break;
560 : : /* INT_MIN/n * n doesn't overflow while negating one operand it does
561 : : if n is a (negative) power of two. */
562 : 4181936 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
563 : 145537 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
564 : 2234184 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
565 : 0 : && (wi::popcount
566 : 2090968 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
567 : 143216 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
568 : 120568 : && (wi::popcount
569 : 4279856 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
570 : : break;
571 : :
572 : : /* Fall through. */
573 : :
574 : 2380910 : case RDIV_EXPR:
575 : 2380910 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
576 : 2380909 : return negate_expr_p (TREE_OPERAND (t, 1))
577 : 2380909 : || negate_expr_p (TREE_OPERAND (t, 0));
578 : : break;
579 : :
580 : 2211 : case TRUNC_DIV_EXPR:
581 : 2211 : case ROUND_DIV_EXPR:
582 : 2211 : case EXACT_DIV_EXPR:
583 : 2211 : if (TYPE_UNSIGNED (type))
584 : : break;
585 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
586 : : B is not 1 we change the sign of the result. */
587 : 489 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
588 : 489 : && negate_expr_p (TREE_OPERAND (t, 0)))
589 : : return true;
590 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
591 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
592 : : and actually traps on some architectures. */
593 : 648 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
594 : 324 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
595 : 563 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
596 : 230 : && ! integer_onep (TREE_OPERAND (t, 1))))
597 : 315 : return negate_expr_p (TREE_OPERAND (t, 1));
598 : : break;
599 : :
600 : 4540173 : case NOP_EXPR:
601 : : /* Negate -((double)float) as (double)(-float). */
602 : 4540173 : if (SCALAR_FLOAT_TYPE_P (type))
603 : : {
604 : 13408 : tree tem = strip_float_extensions (t);
605 : 13408 : if (tem != t)
606 : : return negate_expr_p (tem);
607 : : }
608 : : break;
609 : :
610 : 997740 : case CALL_EXPR:
611 : : /* Negate -f(x) as f(-x). */
612 : 997740 : if (negate_mathfn_p (get_call_combined_fn (t)))
613 : 63 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
614 : : break;
615 : :
616 : 643 : case RSHIFT_EXPR:
617 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
618 : 643 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
619 : : {
620 : 498 : tree op1 = TREE_OPERAND (t, 1);
621 : 498 : if (wi::to_wide (op1) == element_precision (type) - 1)
622 : : return true;
623 : : }
624 : : break;
625 : :
626 : : default:
627 : : break;
628 : : }
629 : : return false;
630 : : }
631 : :
632 : : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
633 : : simplification is possible.
634 : : If negate_expr_p would return true for T, NULL_TREE will never be
635 : : returned. */
636 : :
637 : : static tree
638 : 36559760 : fold_negate_expr_1 (location_t loc, tree t)
639 : : {
640 : 36559760 : tree type = TREE_TYPE (t);
641 : 36559760 : tree tem;
642 : :
643 : 36559760 : switch (TREE_CODE (t))
644 : : {
645 : : /* Convert - (~A) to A + 1. */
646 : 138 : case BIT_NOT_EXPR:
647 : 138 : if (INTEGRAL_TYPE_P (type))
648 : 138 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
649 : 138 : build_one_cst (type));
650 : : break;
651 : :
652 : 28276639 : case INTEGER_CST:
653 : 28276639 : tem = fold_negate_const (t, type);
654 : 28276639 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
655 : 8791 : || (ANY_INTEGRAL_TYPE_P (type)
656 : 8791 : && !TYPE_OVERFLOW_TRAPS (type)
657 : 8791 : && TYPE_OVERFLOW_WRAPS (type))
658 : 28284737 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
659 : : return tem;
660 : : break;
661 : :
662 : 2016844 : case POLY_INT_CST:
663 : 2016844 : case REAL_CST:
664 : 2016844 : case FIXED_CST:
665 : 2016844 : tem = fold_negate_const (t, type);
666 : 2016844 : return tem;
667 : :
668 : 66138 : case COMPLEX_CST:
669 : 66138 : {
670 : 66138 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
671 : 66138 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
672 : 66138 : if (rpart && ipart)
673 : 66138 : return build_complex (type, rpart, ipart);
674 : : }
675 : : break;
676 : :
677 : 49889 : case VECTOR_CST:
678 : 49889 : {
679 : 49889 : tree_vector_builder elts;
680 : 49889 : elts.new_unary_operation (type, t, true);
681 : 49889 : unsigned int count = elts.encoded_nelts ();
682 : 121042 : for (unsigned int i = 0; i < count; ++i)
683 : : {
684 : 71153 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
685 : 71153 : if (elt == NULL_TREE)
686 : 0 : return NULL_TREE;
687 : 71153 : elts.quick_push (elt);
688 : : }
689 : :
690 : 49889 : return elts.build ();
691 : 49889 : }
692 : :
693 : 78 : case COMPLEX_EXPR:
694 : 78 : if (negate_expr_p (t))
695 : 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
696 : 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
697 : 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
698 : : break;
699 : :
700 : 21 : case CONJ_EXPR:
701 : 21 : if (negate_expr_p (t))
702 : 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
703 : 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
704 : : break;
705 : :
706 : 1234 : case NEGATE_EXPR:
707 : 1234 : if (!TYPE_OVERFLOW_SANITIZED (type))
708 : 1221 : return TREE_OPERAND (t, 0);
709 : : break;
710 : :
711 : 663289 : case PLUS_EXPR:
712 : 663289 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
713 : 663289 : && !HONOR_SIGNED_ZEROS (type))
714 : : {
715 : : /* -(A + B) -> (-B) - A. */
716 : 663179 : if (negate_expr_p (TREE_OPERAND (t, 1)))
717 : : {
718 : 612128 : tem = negate_expr (TREE_OPERAND (t, 1));
719 : 612128 : return fold_build2_loc (loc, MINUS_EXPR, type,
720 : 1224256 : tem, TREE_OPERAND (t, 0));
721 : : }
722 : :
723 : : /* -(A + B) -> (-A) - B. */
724 : 51051 : if (negate_expr_p (TREE_OPERAND (t, 0)))
725 : : {
726 : 944 : tem = negate_expr (TREE_OPERAND (t, 0));
727 : 944 : return fold_build2_loc (loc, MINUS_EXPR, type,
728 : 1888 : tem, TREE_OPERAND (t, 1));
729 : : }
730 : : }
731 : : break;
732 : :
733 : 145573 : case MINUS_EXPR:
734 : : /* - (A - B) -> B - A */
735 : 145573 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
736 : 145573 : && !HONOR_SIGNED_ZEROS (type))
737 : 66462 : return fold_build2_loc (loc, MINUS_EXPR, type,
738 : 132924 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
739 : : break;
740 : :
741 : 116934 : case MULT_EXPR:
742 : 116934 : if (TYPE_UNSIGNED (type))
743 : : break;
744 : :
745 : : /* Fall through. */
746 : :
747 : 26603 : case RDIV_EXPR:
748 : 26603 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
749 : : {
750 : 26603 : tem = TREE_OPERAND (t, 1);
751 : 26603 : if (negate_expr_p (tem))
752 : 46340 : return fold_build2_loc (loc, TREE_CODE (t), type,
753 : 46340 : TREE_OPERAND (t, 0), negate_expr (tem));
754 : 3433 : tem = TREE_OPERAND (t, 0);
755 : 3433 : if (negate_expr_p (tem))
756 : 68 : return fold_build2_loc (loc, TREE_CODE (t), type,
757 : 136 : negate_expr (tem), TREE_OPERAND (t, 1));
758 : : }
759 : : break;
760 : :
761 : 1736 : case TRUNC_DIV_EXPR:
762 : 1736 : case ROUND_DIV_EXPR:
763 : 1736 : case EXACT_DIV_EXPR:
764 : 1736 : if (TYPE_UNSIGNED (type))
765 : : break;
766 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
767 : : B is not 1 we change the sign of the result. */
768 : 666 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
769 : 666 : && negate_expr_p (TREE_OPERAND (t, 0)))
770 : 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
771 : 323 : negate_expr (TREE_OPERAND (t, 0)),
772 : 646 : TREE_OPERAND (t, 1));
773 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
774 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
775 : : and actually traps on some architectures. */
776 : 686 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
777 : 343 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
778 : 259 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
779 : 236 : && ! integer_onep (TREE_OPERAND (t, 1))))
780 : 663 : && negate_expr_p (TREE_OPERAND (t, 1)))
781 : 628 : return fold_build2_loc (loc, TREE_CODE (t), type,
782 : 314 : TREE_OPERAND (t, 0),
783 : 628 : negate_expr (TREE_OPERAND (t, 1)));
784 : : break;
785 : :
786 : 1757741 : case NOP_EXPR:
787 : : /* Convert -((double)float) into (double)(-float). */
788 : 1757741 : if (SCALAR_FLOAT_TYPE_P (type))
789 : : {
790 : 11118 : tem = strip_float_extensions (t);
791 : 11118 : if (tem != t && negate_expr_p (tem))
792 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
793 : : }
794 : : break;
795 : :
796 : 299809 : case CALL_EXPR:
797 : : /* Negate -f(x) as f(-x). */
798 : 299809 : if (negate_mathfn_p (get_call_combined_fn (t))
799 : 301098 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
800 : : {
801 : 1191 : tree fndecl, arg;
802 : :
803 : 1191 : fndecl = get_callee_fndecl (t);
804 : 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
805 : 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
806 : : }
807 : : break;
808 : :
809 : 354 : case RSHIFT_EXPR:
810 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
811 : 354 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
812 : : {
813 : 336 : tree op1 = TREE_OPERAND (t, 1);
814 : 336 : if (wi::to_wide (op1) == element_precision (type) - 1)
815 : : {
816 : 72 : tree ntype = TYPE_UNSIGNED (type)
817 : 72 : ? signed_type_for (type)
818 : 72 : : unsigned_type_for (type);
819 : 72 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
820 : 72 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
821 : 72 : return fold_convert_loc (loc, type, temp);
822 : : }
823 : : }
824 : : break;
825 : :
826 : : default:
827 : : break;
828 : : }
829 : :
830 : : return NULL_TREE;
831 : : }
832 : :
833 : : /* A wrapper for fold_negate_expr_1. */
834 : :
835 : : static tree
836 : 36559760 : fold_negate_expr (location_t loc, tree t)
837 : : {
838 : 36559760 : tree type = TREE_TYPE (t);
839 : 36559760 : STRIP_SIGN_NOPS (t);
840 : 36559760 : tree tem = fold_negate_expr_1 (loc, t);
841 : 36559760 : if (tem == NULL_TREE)
842 : : return NULL_TREE;
843 : 31115470 : return fold_convert_loc (loc, type, tem);
844 : : }
845 : :
846 : : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
847 : : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
848 : : return NULL_TREE. */
849 : :
850 : : static tree
851 : 3479947 : negate_expr (tree t)
852 : : {
853 : 3479947 : tree type, tem;
854 : 3479947 : location_t loc;
855 : :
856 : 3479947 : if (t == NULL_TREE)
857 : : return NULL_TREE;
858 : :
859 : 3479947 : loc = EXPR_LOCATION (t);
860 : 3479947 : type = TREE_TYPE (t);
861 : 3479947 : STRIP_SIGN_NOPS (t);
862 : :
863 : 3479947 : tem = fold_negate_expr (loc, t);
864 : 3479947 : if (!tem)
865 : 1698448 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
866 : 3479947 : return fold_convert_loc (loc, type, tem);
867 : : }
868 : :
869 : : /* Split a tree IN into a constant, literal and variable parts that could be
870 : : combined with CODE to make IN. "constant" means an expression with
871 : : TREE_CONSTANT but that isn't an actual constant. CODE must be a
872 : : commutative arithmetic operation. Store the constant part into *CONP,
873 : : the literal in *LITP and return the variable part. If a part isn't
874 : : present, set it to null. If the tree does not decompose in this way,
875 : : return the entire tree as the variable part and the other parts as null.
876 : :
877 : : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
878 : : case, we negate an operand that was subtracted. Except if it is a
879 : : literal for which we use *MINUS_LITP instead.
880 : :
881 : : If NEGATE_P is true, we are negating all of IN, again except a literal
882 : : for which we use *MINUS_LITP instead. If a variable part is of pointer
883 : : type, it is negated after converting to TYPE. This prevents us from
884 : : generating illegal MINUS pointer expression. LOC is the location of
885 : : the converted variable part.
886 : :
887 : : If IN is itself a literal or constant, return it as appropriate.
888 : :
889 : : Note that we do not guarantee that any of the three values will be the
890 : : same type as IN, but they will have the same signedness and mode. */
891 : :
892 : : static tree
893 : 213591488 : split_tree (tree in, tree type, enum tree_code code,
894 : : tree *minus_varp, tree *conp, tree *minus_conp,
895 : : tree *litp, tree *minus_litp, int negate_p)
896 : : {
897 : 213591488 : tree var = 0;
898 : 213591488 : *minus_varp = 0;
899 : 213591488 : *conp = 0;
900 : 213591488 : *minus_conp = 0;
901 : 213591488 : *litp = 0;
902 : 213591488 : *minus_litp = 0;
903 : :
904 : : /* Strip any conversions that don't change the machine mode or signedness. */
905 : 213591488 : STRIP_SIGN_NOPS (in);
906 : :
907 : 213591488 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
908 : 135522064 : || TREE_CODE (in) == FIXED_CST)
909 : 78069424 : *litp = in;
910 : 135522064 : else if (TREE_CODE (in) == code
911 : 135522064 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
912 : 131289931 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
913 : : /* We can associate addition and subtraction together (even
914 : : though the C standard doesn't say so) for integers because
915 : : the value is not affected. For reals, the value might be
916 : : affected, so we can't. */
917 : 131289931 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
918 : 55118504 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
919 : 129700013 : || (code == MINUS_EXPR
920 : 20486586 : && (TREE_CODE (in) == PLUS_EXPR
921 : 18816668 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
922 : : {
923 : 7901074 : tree op0 = TREE_OPERAND (in, 0);
924 : 7901074 : tree op1 = TREE_OPERAND (in, 1);
925 : 7901074 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
926 : 7901074 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
927 : :
928 : : /* First see if either of the operands is a literal, then a constant. */
929 : 7901074 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
930 : 7679155 : || TREE_CODE (op0) == FIXED_CST)
931 : 221919 : *litp = op0, op0 = 0;
932 : 7679155 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
933 : 5075387 : || TREE_CODE (op1) == FIXED_CST)
934 : 2603768 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
935 : :
936 : 7901074 : if (op0 != 0 && TREE_CONSTANT (op0))
937 : 14833 : *conp = op0, op0 = 0;
938 : 7886241 : else if (op1 != 0 && TREE_CONSTANT (op1))
939 : 44700 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
940 : :
941 : : /* If we haven't dealt with either operand, this is not a case we can
942 : : decompose. Otherwise, VAR is either of the ones remaining, if any. */
943 : 7901074 : if (op0 != 0 && op1 != 0)
944 : : var = in;
945 : 2878042 : else if (op0 != 0)
946 : : var = op0;
947 : : else
948 : 236752 : var = op1, neg_var_p = neg1_p;
949 : :
950 : : /* Now do any needed negations. */
951 : 7901074 : if (neg_litp_p)
952 : 24377 : *minus_litp = *litp, *litp = 0;
953 : 7901074 : if (neg_conp_p && *conp)
954 : 3288 : *minus_conp = *conp, *conp = 0;
955 : 7901074 : if (neg_var_p && var)
956 : 226217 : *minus_varp = var, var = 0;
957 : : }
958 : 127620990 : else if (TREE_CONSTANT (in))
959 : 769480 : *conp = in;
960 : 126851510 : else if (TREE_CODE (in) == BIT_NOT_EXPR
961 : 443465 : && code == PLUS_EXPR)
962 : : {
963 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
964 : : when IN is constant. */
965 : 354928 : *litp = build_minus_one_cst (type);
966 : 354928 : *minus_varp = TREE_OPERAND (in, 0);
967 : : }
968 : : else
969 : : var = in;
970 : :
971 : 213591488 : if (negate_p)
972 : : {
973 : 11307227 : if (*litp)
974 : 1170415 : *minus_litp = *litp, *litp = 0;
975 : 10136812 : else if (*minus_litp)
976 : 162 : *litp = *minus_litp, *minus_litp = 0;
977 : 11307227 : if (*conp)
978 : 21490 : *minus_conp = *conp, *conp = 0;
979 : 11285737 : else if (*minus_conp)
980 : 0 : *conp = *minus_conp, *minus_conp = 0;
981 : 11307227 : if (var)
982 : 11273555 : *minus_varp = var, var = 0;
983 : 33672 : else if (*minus_varp)
984 : 757 : var = *minus_varp, *minus_varp = 0;
985 : : }
986 : :
987 : 213591488 : if (*litp
988 : 213591488 : && TREE_OVERFLOW_P (*litp))
989 : 21635 : *litp = drop_tree_overflow (*litp);
990 : 213591488 : if (*minus_litp
991 : 213591488 : && TREE_OVERFLOW_P (*minus_litp))
992 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
993 : :
994 : 213591488 : return var;
995 : : }
996 : :
997 : : /* Re-associate trees split by the above function. T1 and T2 are
998 : : either expressions to associate or null. Return the new
999 : : expression, if any. LOC is the location of the new expression. If
1000 : : we build an operation, do it in TYPE and with CODE. */
1001 : :
1002 : : static tree
1003 : 19704381 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1004 : : {
1005 : 19704381 : if (t1 == 0)
1006 : : {
1007 : 12515297 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1008 : : return t2;
1009 : : }
1010 : 7189084 : else if (t2 == 0)
1011 : : return t1;
1012 : :
1013 : : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1014 : : try to fold this since we will have infinite recursion. But do
1015 : : deal with any NEGATE_EXPRs. */
1016 : 4011865 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1017 : 3171062 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1018 : 3109643 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1019 : : {
1020 : 1557313 : if (code == PLUS_EXPR)
1021 : : {
1022 : 874920 : if (TREE_CODE (t1) == NEGATE_EXPR)
1023 : 54 : return build2_loc (loc, MINUS_EXPR, type,
1024 : : fold_convert_loc (loc, type, t2),
1025 : : fold_convert_loc (loc, type,
1026 : 108 : TREE_OPERAND (t1, 0)));
1027 : 874866 : else if (TREE_CODE (t2) == NEGATE_EXPR)
1028 : 1 : return build2_loc (loc, MINUS_EXPR, type,
1029 : : fold_convert_loc (loc, type, t1),
1030 : : fold_convert_loc (loc, type,
1031 : 2 : TREE_OPERAND (t2, 0)));
1032 : 874865 : else if (integer_zerop (t2))
1033 : 33002 : return fold_convert_loc (loc, type, t1);
1034 : : }
1035 : 682393 : else if (code == MINUS_EXPR)
1036 : : {
1037 : 661311 : if (integer_zerop (t2))
1038 : 0 : return fold_convert_loc (loc, type, t1);
1039 : : }
1040 : :
1041 : 1524256 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1042 : 1524256 : fold_convert_loc (loc, type, t2));
1043 : : }
1044 : :
1045 : 2454552 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1046 : 2454552 : fold_convert_loc (loc, type, t2));
1047 : : }
1048 : :
1049 : : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1050 : : for use in int_const_binop, size_binop and size_diffop. */
1051 : :
1052 : : static bool
1053 : 2093239115 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1054 : : {
1055 : 2093239115 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1056 : : return false;
1057 : 2093239115 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1058 : : return false;
1059 : :
1060 : 2093239115 : switch (code)
1061 : : {
1062 : : case LSHIFT_EXPR:
1063 : : case RSHIFT_EXPR:
1064 : : case LROTATE_EXPR:
1065 : : case RROTATE_EXPR:
1066 : : return true;
1067 : :
1068 : 2093239115 : default:
1069 : 2093239115 : break;
1070 : : }
1071 : :
1072 : 2093239115 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1073 : 2093239115 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1074 : 4186478230 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1075 : : }
1076 : :
1077 : : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1078 : : a new constant in RES. Return FALSE if we don't know how to
1079 : : evaluate CODE at compile-time. */
1080 : :
1081 : : bool
1082 : 1192564994 : wide_int_binop (wide_int &res,
1083 : : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1084 : : signop sign, wi::overflow_type *overflow)
1085 : : {
1086 : 1192564994 : wide_int tmp;
1087 : 1192564994 : *overflow = wi::OVF_NONE;
1088 : 1192564994 : switch (code)
1089 : : {
1090 : 1930506 : case BIT_IOR_EXPR:
1091 : 1930506 : res = wi::bit_or (arg1, arg2);
1092 : 1930506 : break;
1093 : :
1094 : 80123 : case BIT_XOR_EXPR:
1095 : 80123 : res = wi::bit_xor (arg1, arg2);
1096 : 80123 : break;
1097 : :
1098 : 18041126 : case BIT_AND_EXPR:
1099 : 18041126 : res = wi::bit_and (arg1, arg2);
1100 : 18041126 : break;
1101 : :
1102 : 12385421 : case LSHIFT_EXPR:
1103 : 12385421 : if (wi::neg_p (arg2))
1104 : : return false;
1105 : 12355109 : res = wi::lshift (arg1, arg2);
1106 : 12355109 : break;
1107 : :
1108 : 7600996 : case RSHIFT_EXPR:
1109 : 7600996 : if (wi::neg_p (arg2))
1110 : : return false;
1111 : : /* It's unclear from the C standard whether shifts can overflow.
1112 : : The following code ignores overflow; perhaps a C standard
1113 : : interpretation ruling is needed. */
1114 : 7600798 : res = wi::rshift (arg1, arg2, sign);
1115 : 7600798 : break;
1116 : :
1117 : 1146 : case RROTATE_EXPR:
1118 : 1146 : case LROTATE_EXPR:
1119 : 1146 : if (wi::neg_p (arg2))
1120 : : {
1121 : 14 : tmp = -arg2;
1122 : 14 : if (code == RROTATE_EXPR)
1123 : : code = LROTATE_EXPR;
1124 : : else
1125 : : code = RROTATE_EXPR;
1126 : : }
1127 : : else
1128 : 1132 : tmp = arg2;
1129 : :
1130 : 1132 : if (code == RROTATE_EXPR)
1131 : 964 : res = wi::rrotate (arg1, tmp);
1132 : : else
1133 : 182 : res = wi::lrotate (arg1, tmp);
1134 : : break;
1135 : :
1136 : 201388594 : case PLUS_EXPR:
1137 : 201388594 : res = wi::add (arg1, arg2, sign, overflow);
1138 : 201388594 : break;
1139 : :
1140 : 69222547 : case MINUS_EXPR:
1141 : 69222547 : res = wi::sub (arg1, arg2, sign, overflow);
1142 : 69222547 : break;
1143 : :
1144 : 314801426 : case MULT_EXPR:
1145 : 314801426 : res = wi::mul (arg1, arg2, sign, overflow);
1146 : 314801426 : break;
1147 : :
1148 : 4776 : case MULT_HIGHPART_EXPR:
1149 : 4776 : res = wi::mul_high (arg1, arg2, sign);
1150 : 4776 : break;
1151 : :
1152 : 280442553 : case TRUNC_DIV_EXPR:
1153 : 280442553 : case EXACT_DIV_EXPR:
1154 : 280442553 : if (arg2 == 0)
1155 : : return false;
1156 : 280436610 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1157 : 280436610 : break;
1158 : :
1159 : 64503321 : case FLOOR_DIV_EXPR:
1160 : 64503321 : if (arg2 == 0)
1161 : : return false;
1162 : 64503321 : res = wi::div_floor (arg1, arg2, sign, overflow);
1163 : 64503321 : break;
1164 : :
1165 : 95171629 : case CEIL_DIV_EXPR:
1166 : 95171629 : if (arg2 == 0)
1167 : : return false;
1168 : 95171629 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1169 : 95171629 : break;
1170 : :
1171 : 0 : case ROUND_DIV_EXPR:
1172 : 0 : if (arg2 == 0)
1173 : : return false;
1174 : 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1175 : 0 : break;
1176 : :
1177 : 639825 : case TRUNC_MOD_EXPR:
1178 : 639825 : if (arg2 == 0)
1179 : : return false;
1180 : 638751 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1181 : 638751 : break;
1182 : :
1183 : 53379988 : case FLOOR_MOD_EXPR:
1184 : 53379988 : if (arg2 == 0)
1185 : : return false;
1186 : 53379988 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1187 : 53379988 : break;
1188 : :
1189 : 178 : case CEIL_MOD_EXPR:
1190 : 178 : if (arg2 == 0)
1191 : : return false;
1192 : 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1193 : 178 : break;
1194 : :
1195 : 0 : case ROUND_MOD_EXPR:
1196 : 0 : if (arg2 == 0)
1197 : : return false;
1198 : 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1199 : 0 : break;
1200 : :
1201 : 27224 : case MIN_EXPR:
1202 : 27224 : res = wi::min (arg1, arg2, sign);
1203 : 27224 : break;
1204 : :
1205 : 72943488 : case MAX_EXPR:
1206 : 72943488 : res = wi::max (arg1, arg2, sign);
1207 : 72943488 : break;
1208 : :
1209 : : default:
1210 : : return false;
1211 : : }
1212 : : return true;
1213 : 1192564994 : }
1214 : :
1215 : : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1216 : : min value to RES. */
1217 : : bool
1218 : 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1219 : : {
1220 : 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1221 : : {
1222 : 0 : res = wi::to_poly_wide (arg1);
1223 : 0 : return true;
1224 : : }
1225 : 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1226 : : {
1227 : 0 : res = wi::to_poly_wide (arg2);
1228 : 0 : return true;
1229 : : }
1230 : :
1231 : : return false;
1232 : : }
1233 : :
1234 : : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1235 : : produce a new constant in RES. Return FALSE if we don't know how
1236 : : to evaluate CODE at compile-time. */
1237 : :
1238 : : bool
1239 : 1192564994 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1240 : : const_tree arg1, const_tree arg2,
1241 : : signop sign, wi::overflow_type *overflow)
1242 : : {
1243 : 1192564994 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1244 : :
1245 : 1192564994 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1246 : : {
1247 : 1192564994 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1248 : 1192564994 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1249 : 1192564994 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1250 : : return NULL_TREE;
1251 : 1192527340 : res = wi_res;
1252 : 1192527340 : return true;
1253 : 1192565213 : }
1254 : :
1255 : : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1256 : :
1257 : : switch (code)
1258 : : {
1259 : : case PLUS_EXPR:
1260 : : res = wi::add (wi::to_poly_wide (arg1),
1261 : : wi::to_poly_wide (arg2), sign, overflow);
1262 : : break;
1263 : :
1264 : : case MINUS_EXPR:
1265 : : res = wi::sub (wi::to_poly_wide (arg1),
1266 : : wi::to_poly_wide (arg2), sign, overflow);
1267 : : break;
1268 : :
1269 : : case MULT_EXPR:
1270 : : if (TREE_CODE (arg2) == INTEGER_CST)
1271 : : res = wi::mul (wi::to_poly_wide (arg1),
1272 : : wi::to_wide (arg2), sign, overflow);
1273 : : else if (TREE_CODE (arg1) == INTEGER_CST)
1274 : : res = wi::mul (wi::to_poly_wide (arg2),
1275 : : wi::to_wide (arg1), sign, overflow);
1276 : : else
1277 : : return NULL_TREE;
1278 : : break;
1279 : :
1280 : : case LSHIFT_EXPR:
1281 : : if (TREE_CODE (arg2) == INTEGER_CST)
1282 : : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1283 : : else
1284 : : return false;
1285 : : break;
1286 : :
1287 : : case BIT_AND_EXPR:
1288 : : if (TREE_CODE (arg2) != INTEGER_CST
1289 : : || !can_and_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1290 : : &res))
1291 : : return false;
1292 : : break;
1293 : :
1294 : : case BIT_IOR_EXPR:
1295 : : if (TREE_CODE (arg2) != INTEGER_CST
1296 : : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1297 : : &res))
1298 : : return false;
1299 : : break;
1300 : :
1301 : : case MIN_EXPR:
1302 : : if (!can_min_p (arg1, arg2, res))
1303 : : return false;
1304 : : break;
1305 : :
1306 : : default:
1307 : : return false;
1308 : : }
1309 : : return true;
1310 : : }
1311 : :
1312 : : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1313 : : produce a new constant. Return NULL_TREE if we don't know how to
1314 : : evaluate CODE at compile-time. */
1315 : :
1316 : : tree
1317 : 1192564994 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1318 : : int overflowable)
1319 : : {
1320 : 1192564994 : poly_wide_int poly_res;
1321 : 1192564994 : tree type = TREE_TYPE (arg1);
1322 : 1192564994 : signop sign = TYPE_SIGN (type);
1323 : 1192564994 : wi::overflow_type overflow = wi::OVF_NONE;
1324 : :
1325 : 1192564994 : if (!poly_int_tree_p (arg1)
1326 : 1192564994 : || !poly_int_tree_p (arg2)
1327 : 2385129988 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1328 : 37654 : return NULL_TREE;
1329 : 1192527340 : return force_fit_type (type, poly_res, overflowable,
1330 : 1192527340 : (((sign == SIGNED || overflowable == -1)
1331 : 1192527340 : && overflow)
1332 : 1192527340 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1333 : 1192564994 : }
1334 : :
1335 : : /* Return true if binary operation OP distributes over addition in operand
1336 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
1337 : :
1338 : : static bool
1339 : 174127 : distributes_over_addition_p (tree_code op, int opno)
1340 : : {
1341 : 0 : switch (op)
1342 : : {
1343 : : case PLUS_EXPR:
1344 : : case MINUS_EXPR:
1345 : : case MULT_EXPR:
1346 : : return true;
1347 : :
1348 : 0 : case LSHIFT_EXPR:
1349 : 0 : return opno == 1;
1350 : :
1351 : 3388 : default:
1352 : 3388 : return false;
1353 : : }
1354 : : }
1355 : :
1356 : : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1357 : : is the other operand. Try to use the value of OP to simplify the
1358 : : operation in one step, without having to process individual elements. */
1359 : : static tree
1360 : 428038 : simplify_const_binop (tree_code code, tree op, tree other_op,
1361 : : int index ATTRIBUTE_UNUSED)
1362 : : {
1363 : : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1364 : 428038 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1365 : : {
1366 : 336776 : if (integer_zerop (other_op))
1367 : : {
1368 : 23861 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1369 : : return op;
1370 : 23065 : else if (code == BIT_AND_EXPR)
1371 : : return other_op;
1372 : : }
1373 : : }
1374 : :
1375 : : return NULL_TREE;
1376 : : }
1377 : :
1378 : : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1379 : : be an elementwise vector operation, try to fold the operation to a
1380 : : constant vector, using ELT_CONST_BINOP to fold each element. Return
1381 : : the folded value on success, otherwise return null. */
1382 : : tree
1383 : 261382 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1384 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1385 : : {
1386 : 176232 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1387 : 431523 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1388 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1389 : : {
1390 : 170141 : tree type = TREE_TYPE (arg1);
1391 : 170141 : bool step_ok_p;
1392 : 170141 : if (VECTOR_CST_STEPPED_P (arg1)
1393 : 170141 : && VECTOR_CST_STEPPED_P (arg2))
1394 : : /* We can operate directly on the encoding if:
1395 : :
1396 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1397 : : implies
1398 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1399 : :
1400 : : Addition and subtraction are the supported operators
1401 : : for which this is true. */
1402 : 2105 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1403 : 168036 : else if (VECTOR_CST_STEPPED_P (arg1))
1404 : : /* We can operate directly on stepped encodings if:
1405 : :
1406 : : a3 - a2 == a2 - a1
1407 : : implies:
1408 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1409 : :
1410 : : which is true if (x -> x op c) distributes over addition. */
1411 : 49525 : step_ok_p = distributes_over_addition_p (code, 1);
1412 : : else
1413 : : /* Similarly in reverse. */
1414 : 118511 : step_ok_p = distributes_over_addition_p (code, 2);
1415 : 170141 : tree_vector_builder elts;
1416 : 170141 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1417 : : return NULL_TREE;
1418 : 170141 : unsigned int count = elts.encoded_nelts ();
1419 : 635507 : for (unsigned int i = 0; i < count; ++i)
1420 : : {
1421 : 465697 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1422 : 465697 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1423 : :
1424 : 465697 : tree elt = elt_const_binop (code, elem1, elem2);
1425 : :
1426 : : /* It is possible that const_binop cannot handle the given
1427 : : code and return NULL_TREE */
1428 : 465697 : if (elt == NULL_TREE)
1429 : 331 : return NULL_TREE;
1430 : 465366 : elts.quick_push (elt);
1431 : : }
1432 : :
1433 : 169810 : return elts.build ();
1434 : 170141 : }
1435 : :
1436 : 91241 : if (TREE_CODE (arg1) == VECTOR_CST
1437 : 6091 : && TREE_CODE (arg2) == INTEGER_CST)
1438 : : {
1439 : 6091 : tree type = TREE_TYPE (arg1);
1440 : 6091 : bool step_ok_p = distributes_over_addition_p (code, 1);
1441 : 6091 : tree_vector_builder elts;
1442 : 6091 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1443 : : return NULL_TREE;
1444 : 6091 : unsigned int count = elts.encoded_nelts ();
1445 : 30348 : for (unsigned int i = 0; i < count; ++i)
1446 : : {
1447 : 24344 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1448 : :
1449 : 24344 : tree elt = elt_const_binop (code, elem1, arg2);
1450 : :
1451 : : /* It is possible that const_binop cannot handle the given
1452 : : code and return NULL_TREE. */
1453 : 24344 : if (elt == NULL_TREE)
1454 : 87 : return NULL_TREE;
1455 : 24257 : elts.quick_push (elt);
1456 : : }
1457 : :
1458 : 6004 : return elts.build ();
1459 : 6091 : }
1460 : : return NULL_TREE;
1461 : : }
1462 : :
1463 : : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1464 : : constant. We assume ARG1 and ARG2 have the same data type, or at least
1465 : : are the same kind of constant and the same machine mode. Return zero if
1466 : : combining the constants is not allowed in the current operating mode. */
1467 : :
1468 : : static tree
1469 : 168588690 : const_binop (enum tree_code code, tree arg1, tree arg2)
1470 : : {
1471 : : /* Sanity check for the recursive cases. */
1472 : 168588690 : if (!arg1 || !arg2)
1473 : : return NULL_TREE;
1474 : :
1475 : 168587426 : STRIP_NOPS (arg1);
1476 : 168587426 : STRIP_NOPS (arg2);
1477 : :
1478 : 168587426 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1479 : : {
1480 : 162893473 : if (code == POINTER_PLUS_EXPR)
1481 : 101898 : return int_const_binop (PLUS_EXPR,
1482 : 203796 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1483 : :
1484 : 162791575 : return int_const_binop (code, arg1, arg2);
1485 : : }
1486 : :
1487 : 5693953 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1488 : : {
1489 : 5417021 : machine_mode mode;
1490 : 5417021 : REAL_VALUE_TYPE d1;
1491 : 5417021 : REAL_VALUE_TYPE d2;
1492 : 5417021 : REAL_VALUE_TYPE value;
1493 : 5417021 : REAL_VALUE_TYPE result;
1494 : 5417021 : bool inexact;
1495 : 5417021 : tree t, type;
1496 : :
1497 : : /* The following codes are handled by real_arithmetic. */
1498 : 5417021 : switch (code)
1499 : : {
1500 : 5417021 : case PLUS_EXPR:
1501 : 5417021 : case MINUS_EXPR:
1502 : 5417021 : case MULT_EXPR:
1503 : 5417021 : case RDIV_EXPR:
1504 : 5417021 : case MIN_EXPR:
1505 : 5417021 : case MAX_EXPR:
1506 : 5417021 : break;
1507 : :
1508 : : default:
1509 : : return NULL_TREE;
1510 : : }
1511 : :
1512 : 5417021 : d1 = TREE_REAL_CST (arg1);
1513 : 5417021 : d2 = TREE_REAL_CST (arg2);
1514 : :
1515 : 5417021 : type = TREE_TYPE (arg1);
1516 : 5417021 : mode = TYPE_MODE (type);
1517 : :
1518 : : /* Don't perform operation if we honor signaling NaNs and
1519 : : either operand is a signaling NaN. */
1520 : 5417021 : if (HONOR_SNANS (mode)
1521 : 5417021 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1522 : 3625 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1523 : 33 : return NULL_TREE;
1524 : :
1525 : : /* Don't perform operation if it would raise a division
1526 : : by zero exception. */
1527 : 5416988 : if (code == RDIV_EXPR
1528 : 2416602 : && real_equal (&d2, &dconst0)
1529 : 5427521 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1530 : 7245 : return NULL_TREE;
1531 : :
1532 : : /* If either operand is a NaN, just return it. Otherwise, set up
1533 : : for floating-point trap; we return an overflow. */
1534 : 5409743 : if (REAL_VALUE_ISNAN (d1))
1535 : : {
1536 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1537 : : is off. */
1538 : 239 : d1.signalling = 0;
1539 : 239 : t = build_real (type, d1);
1540 : 239 : return t;
1541 : : }
1542 : 5409504 : else if (REAL_VALUE_ISNAN (d2))
1543 : : {
1544 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1545 : : is off. */
1546 : 61 : d2.signalling = 0;
1547 : 61 : t = build_real (type, d2);
1548 : 61 : return t;
1549 : : }
1550 : :
1551 : 5409443 : inexact = real_arithmetic (&value, code, &d1, &d2);
1552 : 5409443 : real_convert (&result, mode, &value);
1553 : :
1554 : : /* Don't constant fold this floating point operation if
1555 : : both operands are not NaN but the result is NaN, and
1556 : : flag_trapping_math. Such operations should raise an
1557 : : invalid operation exception. */
1558 : 5409443 : if (flag_trapping_math
1559 : 21004806 : && MODE_HAS_NANS (mode)
1560 : 5392747 : && REAL_VALUE_ISNAN (result)
1561 : 2498 : && !REAL_VALUE_ISNAN (d1)
1562 : 5411941 : && !REAL_VALUE_ISNAN (d2))
1563 : 2498 : return NULL_TREE;
1564 : :
1565 : : /* Don't constant fold this floating point operation if
1566 : : the result has overflowed and flag_trapping_math. */
1567 : 5406945 : if (flag_trapping_math
1568 : 20995156 : && MODE_HAS_INFINITIES (mode)
1569 : 5390249 : && REAL_VALUE_ISINF (result)
1570 : 7497 : && !REAL_VALUE_ISINF (d1)
1571 : 5413859 : && !REAL_VALUE_ISINF (d2))
1572 : 4631 : return NULL_TREE;
1573 : :
1574 : : /* Don't constant fold this floating point operation if the
1575 : : result may dependent upon the run-time rounding mode and
1576 : : flag_rounding_math is set, or if GCC's software emulation
1577 : : is unable to accurately represent the result. */
1578 : 5402314 : if ((flag_rounding_math
1579 : 36670385 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1580 : 5402314 : && (inexact || !real_identical (&result, &value)))
1581 : 1107 : return NULL_TREE;
1582 : :
1583 : 5401207 : t = build_real (type, result);
1584 : :
1585 : 5401207 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1586 : 5401207 : return t;
1587 : : }
1588 : :
1589 : 276932 : if (TREE_CODE (arg1) == FIXED_CST)
1590 : : {
1591 : 0 : FIXED_VALUE_TYPE f1;
1592 : 0 : FIXED_VALUE_TYPE f2;
1593 : 0 : FIXED_VALUE_TYPE result;
1594 : 0 : tree t, type;
1595 : 0 : bool sat_p;
1596 : 0 : bool overflow_p;
1597 : :
1598 : : /* The following codes are handled by fixed_arithmetic. */
1599 : 0 : switch (code)
1600 : : {
1601 : 0 : case PLUS_EXPR:
1602 : 0 : case MINUS_EXPR:
1603 : 0 : case MULT_EXPR:
1604 : 0 : case TRUNC_DIV_EXPR:
1605 : 0 : if (TREE_CODE (arg2) != FIXED_CST)
1606 : : return NULL_TREE;
1607 : 0 : f2 = TREE_FIXED_CST (arg2);
1608 : 0 : break;
1609 : :
1610 : 0 : case LSHIFT_EXPR:
1611 : 0 : case RSHIFT_EXPR:
1612 : 0 : {
1613 : 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1614 : 0 : return NULL_TREE;
1615 : 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1616 : 0 : f2.data.high = w2.elt (1);
1617 : 0 : f2.data.low = w2.ulow ();
1618 : 0 : f2.mode = SImode;
1619 : : }
1620 : 0 : break;
1621 : :
1622 : : default:
1623 : : return NULL_TREE;
1624 : : }
1625 : :
1626 : 0 : f1 = TREE_FIXED_CST (arg1);
1627 : 0 : type = TREE_TYPE (arg1);
1628 : 0 : sat_p = TYPE_SATURATING (type);
1629 : 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1630 : 0 : t = build_fixed (type, result);
1631 : : /* Propagate overflow flags. */
1632 : 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1633 : 0 : TREE_OVERFLOW (t) = 1;
1634 : 0 : return t;
1635 : : }
1636 : :
1637 : 276932 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1638 : : {
1639 : 11239 : tree type = TREE_TYPE (arg1);
1640 : 11239 : tree r1 = TREE_REALPART (arg1);
1641 : 11239 : tree i1 = TREE_IMAGPART (arg1);
1642 : 11239 : tree r2 = TREE_REALPART (arg2);
1643 : 11239 : tree i2 = TREE_IMAGPART (arg2);
1644 : 11239 : tree real, imag;
1645 : :
1646 : 11239 : switch (code)
1647 : : {
1648 : 5355 : case PLUS_EXPR:
1649 : 5355 : case MINUS_EXPR:
1650 : 5355 : real = const_binop (code, r1, r2);
1651 : 5355 : imag = const_binop (code, i1, i2);
1652 : 5355 : break;
1653 : :
1654 : 3927 : case MULT_EXPR:
1655 : 3927 : if (COMPLEX_FLOAT_TYPE_P (type))
1656 : 2775 : return do_mpc_arg2 (arg1, arg2, type,
1657 : : /* do_nonfinite= */ folding_initializer,
1658 : 2775 : mpc_mul);
1659 : :
1660 : 1152 : real = const_binop (MINUS_EXPR,
1661 : : const_binop (MULT_EXPR, r1, r2),
1662 : : const_binop (MULT_EXPR, i1, i2));
1663 : 1152 : imag = const_binop (PLUS_EXPR,
1664 : : const_binop (MULT_EXPR, r1, i2),
1665 : : const_binop (MULT_EXPR, i1, r2));
1666 : 1152 : break;
1667 : :
1668 : 1703 : case RDIV_EXPR:
1669 : 1703 : if (COMPLEX_FLOAT_TYPE_P (type))
1670 : 1703 : return do_mpc_arg2 (arg1, arg2, type,
1671 : : /* do_nonfinite= */ folding_initializer,
1672 : 1703 : mpc_div);
1673 : : /* Fallthru. */
1674 : 254 : case TRUNC_DIV_EXPR:
1675 : 254 : case CEIL_DIV_EXPR:
1676 : 254 : case FLOOR_DIV_EXPR:
1677 : 254 : case ROUND_DIV_EXPR:
1678 : 254 : if (flag_complex_method == 0)
1679 : : {
1680 : : /* Keep this algorithm in sync with
1681 : : tree-complex.cc:expand_complex_div_straight().
1682 : :
1683 : : Expand complex division to scalars, straightforward algorithm.
1684 : : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1685 : : t = br*br + bi*bi
1686 : : */
1687 : 0 : tree magsquared
1688 : 0 : = const_binop (PLUS_EXPR,
1689 : : const_binop (MULT_EXPR, r2, r2),
1690 : : const_binop (MULT_EXPR, i2, i2));
1691 : 0 : tree t1
1692 : 0 : = const_binop (PLUS_EXPR,
1693 : : const_binop (MULT_EXPR, r1, r2),
1694 : : const_binop (MULT_EXPR, i1, i2));
1695 : 0 : tree t2
1696 : 0 : = const_binop (MINUS_EXPR,
1697 : : const_binop (MULT_EXPR, i1, r2),
1698 : : const_binop (MULT_EXPR, r1, i2));
1699 : :
1700 : 0 : real = const_binop (code, t1, magsquared);
1701 : 0 : imag = const_binop (code, t2, magsquared);
1702 : : }
1703 : : else
1704 : : {
1705 : : /* Keep this algorithm in sync with
1706 : : tree-complex.cc:expand_complex_div_wide().
1707 : :
1708 : : Expand complex division to scalars, modified algorithm to minimize
1709 : : overflow with wide input ranges. */
1710 : 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1711 : : fold_abs_const (r2, TREE_TYPE (type)),
1712 : : fold_abs_const (i2, TREE_TYPE (type)));
1713 : :
1714 : 254 : if (integer_nonzerop (compare))
1715 : : {
1716 : : /* In the TRUE branch, we compute
1717 : : ratio = br/bi;
1718 : : div = (br * ratio) + bi;
1719 : : tr = (ar * ratio) + ai;
1720 : : ti = (ai * ratio) - ar;
1721 : : tr = tr / div;
1722 : : ti = ti / div; */
1723 : 48 : tree ratio = const_binop (code, r2, i2);
1724 : 48 : tree div = const_binop (PLUS_EXPR, i2,
1725 : : const_binop (MULT_EXPR, r2, ratio));
1726 : 48 : real = const_binop (MULT_EXPR, r1, ratio);
1727 : 48 : real = const_binop (PLUS_EXPR, real, i1);
1728 : 48 : real = const_binop (code, real, div);
1729 : :
1730 : 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1731 : 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1732 : 48 : imag = const_binop (code, imag, div);
1733 : : }
1734 : : else
1735 : : {
1736 : : /* In the FALSE branch, we compute
1737 : : ratio = d/c;
1738 : : divisor = (d * ratio) + c;
1739 : : tr = (b * ratio) + a;
1740 : : ti = b - (a * ratio);
1741 : : tr = tr / div;
1742 : : ti = ti / div; */
1743 : 206 : tree ratio = const_binop (code, i2, r2);
1744 : 206 : tree div = const_binop (PLUS_EXPR, r2,
1745 : : const_binop (MULT_EXPR, i2, ratio));
1746 : :
1747 : 206 : real = const_binop (MULT_EXPR, i1, ratio);
1748 : 206 : real = const_binop (PLUS_EXPR, real, r1);
1749 : 206 : real = const_binop (code, real, div);
1750 : :
1751 : 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1752 : 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1753 : 206 : imag = const_binop (code, imag, div);
1754 : : }
1755 : : }
1756 : : break;
1757 : :
1758 : : default:
1759 : : return NULL_TREE;
1760 : : }
1761 : :
1762 : 6761 : if (real && imag)
1763 : 6603 : return build_complex (type, real, imag);
1764 : : }
1765 : :
1766 : 265851 : tree simplified;
1767 : 265851 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1768 : : return simplified;
1769 : :
1770 : 265594 : if (commutative_tree_code (code)
1771 : 265594 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1772 : : return simplified;
1773 : :
1774 : 261382 : return vector_const_binop (code, arg1, arg2, const_binop);
1775 : : }
1776 : :
1777 : : /* Overload that adds a TYPE parameter to be able to dispatch
1778 : : to fold_relational_const. */
1779 : :
1780 : : tree
1781 : 217151889 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1782 : : {
1783 : 217151889 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1784 : 55460702 : return fold_relational_const (code, type, arg1, arg2);
1785 : :
1786 : : /* ??? Until we make the const_binop worker take the type of the
1787 : : result as argument put those cases that need it here. */
1788 : 161691187 : switch (code)
1789 : : {
1790 : 18 : case VEC_SERIES_EXPR:
1791 : 18 : if (CONSTANT_CLASS_P (arg1)
1792 : 18 : && CONSTANT_CLASS_P (arg2))
1793 : 18 : return build_vec_series (type, arg1, arg2);
1794 : : return NULL_TREE;
1795 : :
1796 : 267669 : case COMPLEX_EXPR:
1797 : 267669 : if ((TREE_CODE (arg1) == REAL_CST
1798 : 257166 : && TREE_CODE (arg2) == REAL_CST)
1799 : 10504 : || (TREE_CODE (arg1) == INTEGER_CST
1800 : 10503 : && TREE_CODE (arg2) == INTEGER_CST))
1801 : 267668 : return build_complex (type, arg1, arg2);
1802 : : return NULL_TREE;
1803 : :
1804 : 10419 : case POINTER_DIFF_EXPR:
1805 : 10419 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1806 : : {
1807 : 19654 : poly_offset_int res = (wi::to_poly_offset (arg1)
1808 : 9827 : - wi::to_poly_offset (arg2));
1809 : 9827 : return force_fit_type (type, res, 1,
1810 : 9827 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1811 : : }
1812 : : return NULL_TREE;
1813 : :
1814 : 11936 : case VEC_PACK_TRUNC_EXPR:
1815 : 11936 : case VEC_PACK_FIX_TRUNC_EXPR:
1816 : 11936 : case VEC_PACK_FLOAT_EXPR:
1817 : 11936 : {
1818 : 11936 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1819 : :
1820 : 11936 : if (TREE_CODE (arg1) != VECTOR_CST
1821 : 11936 : || TREE_CODE (arg2) != VECTOR_CST)
1822 : : return NULL_TREE;
1823 : :
1824 : 11936 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1825 : : return NULL_TREE;
1826 : :
1827 : 11936 : out_nelts = in_nelts * 2;
1828 : 11936 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1829 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1830 : :
1831 : 11936 : tree_vector_builder elts (type, out_nelts, 1);
1832 : 147156 : for (i = 0; i < out_nelts; i++)
1833 : : {
1834 : 135232 : tree elt = (i < in_nelts
1835 : 135232 : ? VECTOR_CST_ELT (arg1, i)
1836 : 67610 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1837 : 136100 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1838 : : ? NOP_EXPR
1839 : : : code == VEC_PACK_FLOAT_EXPR
1840 : 868 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1841 : 135232 : TREE_TYPE (type), elt);
1842 : 135232 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1843 : 12 : return NULL_TREE;
1844 : 135220 : elts.quick_push (elt);
1845 : : }
1846 : :
1847 : 11924 : return elts.build ();
1848 : 11936 : }
1849 : :
1850 : 186 : case VEC_WIDEN_MULT_LO_EXPR:
1851 : 186 : case VEC_WIDEN_MULT_HI_EXPR:
1852 : 186 : case VEC_WIDEN_MULT_EVEN_EXPR:
1853 : 186 : case VEC_WIDEN_MULT_ODD_EXPR:
1854 : 186 : {
1855 : 186 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1856 : :
1857 : 186 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1858 : : return NULL_TREE;
1859 : :
1860 : 186 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1861 : : return NULL_TREE;
1862 : 186 : out_nelts = in_nelts / 2;
1863 : 186 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1864 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1865 : :
1866 : 186 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1867 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1868 : : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1869 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1870 : : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1871 : : scale = 1, ofs = 0;
1872 : : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1873 : 186 : scale = 1, ofs = 1;
1874 : :
1875 : 186 : tree_vector_builder elts (type, out_nelts, 1);
1876 : 646 : for (out = 0; out < out_nelts; out++)
1877 : : {
1878 : 460 : unsigned int in = (out << scale) + ofs;
1879 : 460 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1880 : : VECTOR_CST_ELT (arg1, in));
1881 : 460 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1882 : : VECTOR_CST_ELT (arg2, in));
1883 : :
1884 : 460 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1885 : 0 : return NULL_TREE;
1886 : 460 : tree elt = const_binop (MULT_EXPR, t1, t2);
1887 : 460 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1888 : : return NULL_TREE;
1889 : 460 : elts.quick_push (elt);
1890 : : }
1891 : :
1892 : 186 : return elts.build ();
1893 : 186 : }
1894 : :
1895 : 161400959 : default:;
1896 : : }
1897 : :
1898 : 161400959 : if (TREE_CODE_CLASS (code) != tcc_binary)
1899 : : return NULL_TREE;
1900 : :
1901 : : /* Make sure type and arg0 have the same saturating flag. */
1902 : 158741173 : gcc_checking_assert (TYPE_SATURATING (type)
1903 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1904 : :
1905 : 158741173 : return const_binop (code, arg1, arg2);
1906 : : }
1907 : :
1908 : : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1909 : : Return zero if computing the constants is not possible. */
1910 : :
1911 : : tree
1912 : 268524232 : const_unop (enum tree_code code, tree type, tree arg0)
1913 : : {
1914 : : /* Don't perform the operation, other than NEGATE and ABS, if
1915 : : flag_signaling_nans is on and the operand is a signaling NaN. */
1916 : 268524232 : if (TREE_CODE (arg0) == REAL_CST
1917 : 10643933 : && HONOR_SNANS (arg0)
1918 : 7345 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1919 : 454 : && code != NEGATE_EXPR
1920 : 454 : && code != ABS_EXPR
1921 : 268524651 : && code != ABSU_EXPR)
1922 : : return NULL_TREE;
1923 : :
1924 : 268523813 : switch (code)
1925 : : {
1926 : 186350681 : CASE_CONVERT:
1927 : 186350681 : case FLOAT_EXPR:
1928 : 186350681 : case FIX_TRUNC_EXPR:
1929 : 186350681 : case FIXED_CONVERT_EXPR:
1930 : 186350681 : return fold_convert_const (code, type, arg0);
1931 : :
1932 : 0 : case ADDR_SPACE_CONVERT_EXPR:
1933 : : /* If the source address is 0, and the source address space
1934 : : cannot have a valid object at 0, fold to dest type null. */
1935 : 0 : if (integer_zerop (arg0)
1936 : 0 : && !(targetm.addr_space.zero_address_valid
1937 : 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1938 : 0 : return fold_convert_const (code, type, arg0);
1939 : : break;
1940 : :
1941 : 11012387 : case VIEW_CONVERT_EXPR:
1942 : 11012387 : return fold_view_convert_expr (type, arg0);
1943 : :
1944 : 29277684 : case NEGATE_EXPR:
1945 : 29277684 : {
1946 : : /* Can't call fold_negate_const directly here as that doesn't
1947 : : handle all cases and we might not be able to negate some
1948 : : constants. */
1949 : 29277684 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1950 : 29277684 : if (tem && CONSTANT_CLASS_P (tem))
1951 : : return tem;
1952 : : break;
1953 : : }
1954 : :
1955 : 35600 : case ABS_EXPR:
1956 : 35600 : case ABSU_EXPR:
1957 : 35600 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1958 : 35317 : return fold_abs_const (arg0, type);
1959 : : break;
1960 : :
1961 : 24552 : case CONJ_EXPR:
1962 : 24552 : if (TREE_CODE (arg0) == COMPLEX_CST)
1963 : : {
1964 : 24549 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1965 : 24549 : TREE_TYPE (type));
1966 : 24549 : return build_complex (type, TREE_REALPART (arg0), ipart);
1967 : : }
1968 : : break;
1969 : :
1970 : 2327194 : case BIT_NOT_EXPR:
1971 : 2327194 : if (TREE_CODE (arg0) == INTEGER_CST)
1972 : 2326091 : return fold_not_const (arg0, type);
1973 : 1103 : else if (POLY_INT_CST_P (arg0))
1974 : : return wide_int_to_tree (type, ~poly_int_cst_value (arg0));
1975 : : /* Perform BIT_NOT_EXPR on each element individually. */
1976 : 1103 : else if (TREE_CODE (arg0) == VECTOR_CST)
1977 : : {
1978 : 497 : tree elem;
1979 : :
1980 : : /* This can cope with stepped encodings because ~x == -1 - x. */
1981 : 497 : tree_vector_builder elements;
1982 : 497 : elements.new_unary_operation (type, arg0, true);
1983 : 497 : unsigned int i, count = elements.encoded_nelts ();
1984 : 2076 : for (i = 0; i < count; ++i)
1985 : : {
1986 : 1579 : elem = VECTOR_CST_ELT (arg0, i);
1987 : 1579 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1988 : 1579 : if (elem == NULL_TREE)
1989 : : break;
1990 : 1579 : elements.quick_push (elem);
1991 : : }
1992 : 497 : if (i == count)
1993 : 497 : return elements.build ();
1994 : 497 : }
1995 : : break;
1996 : :
1997 : 7859704 : case TRUTH_NOT_EXPR:
1998 : 7859704 : if (TREE_CODE (arg0) == INTEGER_CST)
1999 : 7544452 : return constant_boolean_node (integer_zerop (arg0), type);
2000 : : break;
2001 : :
2002 : 178995 : case REALPART_EXPR:
2003 : 178995 : if (TREE_CODE (arg0) == COMPLEX_CST)
2004 : 178794 : return fold_convert (type, TREE_REALPART (arg0));
2005 : : break;
2006 : :
2007 : 182853 : case IMAGPART_EXPR:
2008 : 182853 : if (TREE_CODE (arg0) == COMPLEX_CST)
2009 : 182665 : return fold_convert (type, TREE_IMAGPART (arg0));
2010 : : break;
2011 : :
2012 : 12274 : case VEC_UNPACK_LO_EXPR:
2013 : 12274 : case VEC_UNPACK_HI_EXPR:
2014 : 12274 : case VEC_UNPACK_FLOAT_LO_EXPR:
2015 : 12274 : case VEC_UNPACK_FLOAT_HI_EXPR:
2016 : 12274 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2017 : 12274 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2018 : 12274 : {
2019 : 12274 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2020 : 12274 : enum tree_code subcode;
2021 : :
2022 : 12274 : if (TREE_CODE (arg0) != VECTOR_CST)
2023 : : return NULL_TREE;
2024 : :
2025 : 12274 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2026 : : return NULL_TREE;
2027 : 12274 : out_nelts = in_nelts / 2;
2028 : 12274 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2029 : :
2030 : 12274 : unsigned int offset = 0;
2031 : 12274 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2032 : 12274 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2033 : : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2034 : 6128 : offset = out_nelts;
2035 : :
2036 : 12274 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2037 : : subcode = NOP_EXPR;
2038 : 7416 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2039 : 7416 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
2040 : : subcode = FLOAT_EXPR;
2041 : : else
2042 : 4 : subcode = FIX_TRUNC_EXPR;
2043 : :
2044 : 12274 : tree_vector_builder elts (type, out_nelts, 1);
2045 : 58280 : for (i = 0; i < out_nelts; i++)
2046 : : {
2047 : 46006 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2048 : 46006 : VECTOR_CST_ELT (arg0, i + offset));
2049 : 46006 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2050 : 0 : return NULL_TREE;
2051 : 46006 : elts.quick_push (elt);
2052 : : }
2053 : :
2054 : 12274 : return elts.build ();
2055 : 12274 : }
2056 : :
2057 : 4 : case VEC_DUPLICATE_EXPR:
2058 : 4 : if (CONSTANT_CLASS_P (arg0))
2059 : 4 : return build_vector_from_val (type, arg0);
2060 : : return NULL_TREE;
2061 : :
2062 : : default:
2063 : : break;
2064 : : }
2065 : :
2066 : : return NULL_TREE;
2067 : : }
2068 : :
2069 : : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2070 : : indicates which particular sizetype to create. */
2071 : :
2072 : : tree
2073 : 3058031992 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2074 : : {
2075 : 3058031992 : return build_int_cst (sizetype_tab[(int) kind], number);
2076 : : }
2077 : :
2078 : : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2079 : : is a tree code. The type of the result is taken from the operands.
2080 : : Both must be equivalent integer types, ala int_binop_types_match_p.
2081 : : If the operands are constant, so is the result. */
2082 : :
2083 : : tree
2084 : 2060950940 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2085 : : {
2086 : 2060950940 : tree type = TREE_TYPE (arg0);
2087 : :
2088 : 2060950940 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2089 : : return error_mark_node;
2090 : :
2091 : 2060950940 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2092 : : TREE_TYPE (arg1)));
2093 : :
2094 : : /* Handle the special case of two poly_int constants faster. */
2095 : 2060950940 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2096 : : {
2097 : : /* And some specific cases even faster than that. */
2098 : 2029085849 : if (code == PLUS_EXPR)
2099 : : {
2100 : 914931274 : if (integer_zerop (arg0)
2101 : 914931274 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2102 : : return arg1;
2103 : 259856165 : if (integer_zerop (arg1)
2104 : 259856165 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2105 : : return arg0;
2106 : : }
2107 : 1114154575 : else if (code == MINUS_EXPR)
2108 : : {
2109 : 102270775 : if (integer_zerop (arg1)
2110 : 102270775 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2111 : : return arg0;
2112 : : }
2113 : 1011883800 : else if (code == MULT_EXPR)
2114 : : {
2115 : 453528527 : if (integer_onep (arg0)
2116 : 453528527 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2117 : : return arg1;
2118 : : }
2119 : :
2120 : : /* Handle general case of two integer constants. For sizetype
2121 : : constant calculations we always want to know about overflow,
2122 : : even in the unsigned case. */
2123 : 1013942841 : tree res = int_const_binop (code, arg0, arg1, -1);
2124 : 1013942841 : if (res != NULL_TREE)
2125 : : return res;
2126 : : }
2127 : :
2128 : 31865091 : return fold_build2_loc (loc, code, type, arg0, arg1);
2129 : : }
2130 : :
2131 : : /* Given two values, either both of sizetype or both of bitsizetype,
2132 : : compute the difference between the two values. Return the value
2133 : : in signed type corresponding to the type of the operands. */
2134 : :
2135 : : tree
2136 : 32288175 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2137 : : {
2138 : 32288175 : tree type = TREE_TYPE (arg0);
2139 : 32288175 : tree ctype;
2140 : :
2141 : 32288175 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2142 : : TREE_TYPE (arg1)));
2143 : :
2144 : : /* If the type is already signed, just do the simple thing. */
2145 : 32288175 : if (!TYPE_UNSIGNED (type))
2146 : 9506489 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2147 : :
2148 : 22781686 : if (type == sizetype)
2149 : 22781686 : ctype = ssizetype;
2150 : 0 : else if (type == bitsizetype)
2151 : 0 : ctype = sbitsizetype;
2152 : : else
2153 : 0 : ctype = signed_type_for (type);
2154 : :
2155 : : /* If either operand is not a constant, do the conversions to the signed
2156 : : type and subtract. The hardware will do the right thing with any
2157 : : overflow in the subtraction. */
2158 : 22781686 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2159 : 11789 : return size_binop_loc (loc, MINUS_EXPR,
2160 : : fold_convert_loc (loc, ctype, arg0),
2161 : 11789 : fold_convert_loc (loc, ctype, arg1));
2162 : :
2163 : : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2164 : : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2165 : : overflow) and negate (which can't either). Special-case a result
2166 : : of zero while we're here. */
2167 : 22769897 : if (tree_int_cst_equal (arg0, arg1))
2168 : 19534979 : return build_int_cst (ctype, 0);
2169 : 3234918 : else if (tree_int_cst_lt (arg1, arg0))
2170 : 2098629 : return fold_convert_loc (loc, ctype,
2171 : 2098629 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2172 : : else
2173 : 1136289 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2174 : : fold_convert_loc (loc, ctype,
2175 : : size_binop_loc (loc,
2176 : : MINUS_EXPR,
2177 : : arg1, arg0)));
2178 : : }
2179 : :
2180 : : /* A subroutine of fold_convert_const handling conversions of an
2181 : : INTEGER_CST to another integer type. */
2182 : :
2183 : : static tree
2184 : 1060338643 : fold_convert_const_int_from_int (tree type, const_tree arg1)
2185 : : {
2186 : : /* Given an integer constant, make new constant with new type,
2187 : : appropriately sign-extended or truncated. Use widest_int
2188 : : so that any extension is done according ARG1's type. */
2189 : 1060338643 : tree arg1_type = TREE_TYPE (arg1);
2190 : 1060338643 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2191 : 1060338643 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2192 : 1060338643 : TYPE_SIGN (arg1_type)),
2193 : 1060338643 : !POINTER_TYPE_P (TREE_TYPE (arg1)),
2194 : 1060338643 : TREE_OVERFLOW (arg1));
2195 : : }
2196 : :
2197 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2198 : : to an integer type. */
2199 : :
2200 : : static tree
2201 : 36017 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2202 : : {
2203 : 36017 : bool overflow = false;
2204 : 36017 : tree t;
2205 : :
2206 : : /* The following code implements the floating point to integer
2207 : : conversion rules required by the Java Language Specification,
2208 : : that IEEE NaNs are mapped to zero and values that overflow
2209 : : the target precision saturate, i.e. values greater than
2210 : : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2211 : : are mapped to INT_MIN. These semantics are allowed by the
2212 : : C and C++ standards that simply state that the behavior of
2213 : : FP-to-integer conversion is unspecified upon overflow. */
2214 : :
2215 : 36017 : wide_int val;
2216 : 36017 : REAL_VALUE_TYPE r;
2217 : 36017 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2218 : :
2219 : 36017 : switch (code)
2220 : : {
2221 : 36017 : case FIX_TRUNC_EXPR:
2222 : 36017 : real_trunc (&r, VOIDmode, &x);
2223 : 36017 : break;
2224 : :
2225 : 0 : default:
2226 : 0 : gcc_unreachable ();
2227 : : }
2228 : :
2229 : : /* If R is NaN, return zero and show we have an overflow. */
2230 : 36017 : if (REAL_VALUE_ISNAN (r))
2231 : : {
2232 : 3638 : overflow = true;
2233 : 3638 : val = wi::zero (TYPE_PRECISION (type));
2234 : : }
2235 : :
2236 : : /* See if R is less than the lower bound or greater than the
2237 : : upper bound. */
2238 : :
2239 : 36017 : if (! overflow)
2240 : : {
2241 : 32379 : tree lt = TYPE_MIN_VALUE (type);
2242 : 32379 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2243 : 32379 : if (real_less (&r, &l))
2244 : : {
2245 : 1974 : overflow = true;
2246 : 1974 : val = wi::to_wide (lt);
2247 : : }
2248 : : }
2249 : :
2250 : 36017 : if (! overflow)
2251 : : {
2252 : 30405 : tree ut = TYPE_MAX_VALUE (type);
2253 : 30405 : if (ut)
2254 : : {
2255 : 30405 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2256 : 30405 : if (real_less (&u, &r))
2257 : : {
2258 : 1921 : overflow = true;
2259 : 1921 : val = wi::to_wide (ut);
2260 : : }
2261 : : }
2262 : : }
2263 : :
2264 : 36017 : if (! overflow)
2265 : 28486 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2266 : :
2267 : : /* According to IEEE standard, for conversions from floating point to
2268 : : integer. When a NaN or infinite operand cannot be represented in the
2269 : : destination format and this cannot otherwise be indicated, the invalid
2270 : : operation exception shall be signaled. When a numeric operand would
2271 : : convert to an integer outside the range of the destination format, the
2272 : : invalid operation exception shall be signaled if this situation cannot
2273 : : otherwise be indicated. */
2274 : 36017 : if (!flag_trapping_math || !overflow)
2275 : 28740 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2276 : : else
2277 : : t = NULL_TREE;
2278 : :
2279 : 36017 : return t;
2280 : 36017 : }
2281 : :
2282 : : /* A subroutine of fold_convert_const handling conversions of a
2283 : : FIXED_CST to an integer type. */
2284 : :
2285 : : static tree
2286 : 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2287 : : {
2288 : 0 : tree t;
2289 : 0 : double_int temp, temp_trunc;
2290 : 0 : scalar_mode mode;
2291 : :
2292 : : /* Right shift FIXED_CST to temp by fbit. */
2293 : 0 : temp = TREE_FIXED_CST (arg1).data;
2294 : 0 : mode = TREE_FIXED_CST (arg1).mode;
2295 : 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2296 : : {
2297 : 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2298 : : HOST_BITS_PER_DOUBLE_INT,
2299 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2300 : :
2301 : : /* Left shift temp to temp_trunc by fbit. */
2302 : 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2303 : : HOST_BITS_PER_DOUBLE_INT,
2304 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2305 : : }
2306 : : else
2307 : : {
2308 : 0 : temp = double_int_zero;
2309 : 0 : temp_trunc = double_int_zero;
2310 : : }
2311 : :
2312 : : /* If FIXED_CST is negative, we need to round the value toward 0.
2313 : : By checking if the fractional bits are not zero to add 1 to temp. */
2314 : 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2315 : 0 : && temp_trunc.is_negative ()
2316 : 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2317 : 0 : temp += double_int_one;
2318 : :
2319 : : /* Given a fixed-point constant, make new constant with new type,
2320 : : appropriately sign-extended or truncated. */
2321 : 0 : t = force_fit_type (type, temp, -1,
2322 : 0 : (temp.is_negative ()
2323 : 0 : && (TYPE_UNSIGNED (type)
2324 : 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2325 : 0 : | TREE_OVERFLOW (arg1));
2326 : :
2327 : 0 : return t;
2328 : : }
2329 : :
2330 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2331 : : to another floating point type. */
2332 : :
2333 : : static tree
2334 : 1945294 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2335 : : {
2336 : 1945294 : REAL_VALUE_TYPE value;
2337 : 1945294 : tree t;
2338 : :
2339 : : /* If the underlying modes are the same, simply treat it as
2340 : : copy and rebuild with TREE_REAL_CST information and the
2341 : : given type. */
2342 : 1945294 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2343 : : {
2344 : 97578 : t = build_real (type, TREE_REAL_CST (arg1));
2345 : 97578 : return t;
2346 : : }
2347 : :
2348 : : /* Don't perform the operation if flag_signaling_nans is on
2349 : : and the operand is a signaling NaN. */
2350 : 1847716 : if (HONOR_SNANS (arg1)
2351 : 1848076 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2352 : : return NULL_TREE;
2353 : :
2354 : : /* With flag_rounding_math we should respect the current rounding mode
2355 : : unless the conversion is exact. */
2356 : 1847716 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2357 : 1848372 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2358 : 509 : return NULL_TREE;
2359 : :
2360 : 1847207 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2361 : 1847207 : t = build_real (type, value);
2362 : :
2363 : : /* If converting an infinity or NAN to a representation that doesn't
2364 : : have one, set the overflow bit so that we can produce some kind of
2365 : : error message at the appropriate point if necessary. It's not the
2366 : : most user-friendly message, but it's better than nothing. */
2367 : 1847207 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2368 : 1957006 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2369 : 0 : TREE_OVERFLOW (t) = 1;
2370 : 1847207 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2371 : 1952119 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2372 : 0 : TREE_OVERFLOW (t) = 1;
2373 : : /* Regular overflow, conversion produced an infinity in a mode that
2374 : : can't represent them. */
2375 : 9232624 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2376 : 0 : && REAL_VALUE_ISINF (value)
2377 : 1847207 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2378 : 0 : TREE_OVERFLOW (t) = 1;
2379 : : else
2380 : 1847207 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2381 : : return t;
2382 : : }
2383 : :
2384 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2385 : : to a floating point type. */
2386 : :
2387 : : static tree
2388 : 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2389 : : {
2390 : 0 : REAL_VALUE_TYPE value;
2391 : 0 : tree t;
2392 : :
2393 : 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2394 : 0 : &TREE_FIXED_CST (arg1));
2395 : 0 : t = build_real (type, value);
2396 : :
2397 : 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2398 : 0 : return t;
2399 : : }
2400 : :
2401 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2402 : : to another fixed-point type. */
2403 : :
2404 : : static tree
2405 : 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2406 : : {
2407 : 0 : FIXED_VALUE_TYPE value;
2408 : 0 : tree t;
2409 : 0 : bool overflow_p;
2410 : :
2411 : 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2412 : 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2413 : 0 : t = build_fixed (type, value);
2414 : :
2415 : : /* Propagate overflow flags. */
2416 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2417 : 0 : TREE_OVERFLOW (t) = 1;
2418 : 0 : return t;
2419 : : }
2420 : :
2421 : : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2422 : : to a fixed-point type. */
2423 : :
2424 : : static tree
2425 : 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2426 : : {
2427 : 0 : FIXED_VALUE_TYPE value;
2428 : 0 : tree t;
2429 : 0 : bool overflow_p;
2430 : 0 : double_int di;
2431 : :
2432 : 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2433 : :
2434 : 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2435 : 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2436 : 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2437 : : else
2438 : 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2439 : :
2440 : 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2441 : 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2442 : 0 : TYPE_SATURATING (type));
2443 : 0 : t = build_fixed (type, value);
2444 : :
2445 : : /* Propagate overflow flags. */
2446 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2447 : 0 : TREE_OVERFLOW (t) = 1;
2448 : 0 : return t;
2449 : : }
2450 : :
2451 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2452 : : to a fixed-point type. */
2453 : :
2454 : : static tree
2455 : 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2456 : : {
2457 : 0 : FIXED_VALUE_TYPE value;
2458 : 0 : tree t;
2459 : 0 : bool overflow_p;
2460 : :
2461 : 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2462 : 0 : &TREE_REAL_CST (arg1),
2463 : 0 : TYPE_SATURATING (type));
2464 : 0 : t = build_fixed (type, value);
2465 : :
2466 : : /* Propagate overflow flags. */
2467 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2468 : 0 : TREE_OVERFLOW (t) = 1;
2469 : 0 : return t;
2470 : : }
2471 : :
2472 : : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2473 : : type TYPE. If no simplification can be done return NULL_TREE. */
2474 : :
2475 : : static tree
2476 : 1118554026 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2477 : : {
2478 : 1118554026 : tree arg_type = TREE_TYPE (arg1);
2479 : 1118554026 : if (arg_type == type)
2480 : : return arg1;
2481 : :
2482 : : /* We can't widen types, since the runtime value could overflow the
2483 : : original type before being extended to the new type. */
2484 : 1107852933 : if (POLY_INT_CST_P (arg1)
2485 : : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2486 : : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2487 : : return build_poly_int_cst (type,
2488 : : poly_wide_int::from (poly_int_cst_value (arg1),
2489 : : TYPE_PRECISION (type),
2490 : : TYPE_SIGN (arg_type)));
2491 : :
2492 : 1107852933 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2493 : : || TREE_CODE (type) == OFFSET_TYPE)
2494 : : {
2495 : 1076785397 : if (TREE_CODE (arg1) == INTEGER_CST)
2496 : 1060338643 : return fold_convert_const_int_from_int (type, arg1);
2497 : 16446754 : else if (TREE_CODE (arg1) == REAL_CST)
2498 : 36017 : return fold_convert_const_int_from_real (code, type, arg1);
2499 : 16410737 : else if (TREE_CODE (arg1) == FIXED_CST)
2500 : 0 : return fold_convert_const_int_from_fixed (type, arg1);
2501 : : }
2502 : : else if (SCALAR_FLOAT_TYPE_P (type))
2503 : : {
2504 : 31012805 : if (TREE_CODE (arg1) == INTEGER_CST)
2505 : : {
2506 : 23876799 : tree res = build_real_from_int_cst (type, arg1);
2507 : : /* Avoid the folding if flag_rounding_math is on and the
2508 : : conversion is not exact. */
2509 : 23876799 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2510 : : {
2511 : 2878 : bool fail = false;
2512 : 5756 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2513 : 2878 : TYPE_PRECISION (TREE_TYPE (arg1)));
2514 : 2878 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2515 : 1722 : return NULL_TREE;
2516 : 2878 : }
2517 : 23875077 : return res;
2518 : : }
2519 : 7136006 : else if (TREE_CODE (arg1) == REAL_CST)
2520 : 1945294 : return fold_convert_const_real_from_real (type, arg1);
2521 : 5190712 : else if (TREE_CODE (arg1) == FIXED_CST)
2522 : 0 : return fold_convert_const_real_from_fixed (type, arg1);
2523 : : }
2524 : : else if (FIXED_POINT_TYPE_P (type))
2525 : : {
2526 : 0 : if (TREE_CODE (arg1) == FIXED_CST)
2527 : 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2528 : 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2529 : 0 : return fold_convert_const_fixed_from_int (type, arg1);
2530 : 0 : else if (TREE_CODE (arg1) == REAL_CST)
2531 : 0 : return fold_convert_const_fixed_from_real (type, arg1);
2532 : : }
2533 : : else if (VECTOR_TYPE_P (type))
2534 : : {
2535 : 7155 : if (TREE_CODE (arg1) == VECTOR_CST
2536 : 7155 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2537 : : {
2538 : 7155 : tree elttype = TREE_TYPE (type);
2539 : 7155 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2540 : : /* We can't handle steps directly when extending, since the
2541 : : values need to wrap at the original precision first. */
2542 : 7155 : bool step_ok_p
2543 : 7155 : = (INTEGRAL_TYPE_P (elttype)
2544 : 284 : && INTEGRAL_TYPE_P (arg1_elttype)
2545 : 7381 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2546 : 7155 : tree_vector_builder v;
2547 : 7155 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2548 : : return NULL_TREE;
2549 : 7155 : unsigned int len = v.encoded_nelts ();
2550 : 40408 : for (unsigned int i = 0; i < len; ++i)
2551 : : {
2552 : 33253 : tree elt = VECTOR_CST_ELT (arg1, i);
2553 : 33253 : tree cvt = fold_convert_const (code, elttype, elt);
2554 : 33253 : if (cvt == NULL_TREE)
2555 : 0 : return NULL_TREE;
2556 : 33253 : v.quick_push (cvt);
2557 : : }
2558 : 7155 : return v.build ();
2559 : 7155 : }
2560 : : }
2561 : : return NULL_TREE;
2562 : : }
2563 : :
2564 : : /* Construct a vector of zero elements of vector type TYPE. */
2565 : :
2566 : : static tree
2567 : 16735 : build_zero_vector (tree type)
2568 : : {
2569 : 16735 : tree t;
2570 : :
2571 : 16735 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2572 : 16735 : return build_vector_from_val (type, t);
2573 : : }
2574 : :
2575 : : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2576 : :
2577 : : bool
2578 : 2289 : fold_convertible_p (const_tree type, const_tree arg)
2579 : : {
2580 : 2289 : const_tree orig = TREE_TYPE (arg);
2581 : :
2582 : 2289 : if (type == orig)
2583 : : return true;
2584 : :
2585 : 2289 : if (TREE_CODE (arg) == ERROR_MARK
2586 : 2289 : || TREE_CODE (type) == ERROR_MARK
2587 : 2289 : || TREE_CODE (orig) == ERROR_MARK)
2588 : : return false;
2589 : :
2590 : 2289 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2591 : : return true;
2592 : :
2593 : 2289 : switch (TREE_CODE (type))
2594 : : {
2595 : 865 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2596 : 865 : case POINTER_TYPE: case REFERENCE_TYPE:
2597 : 865 : case OFFSET_TYPE:
2598 : 865 : return (INTEGRAL_TYPE_P (orig)
2599 : 240 : || (POINTER_TYPE_P (orig)
2600 : 105 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2601 : 1000 : || TREE_CODE (orig) == OFFSET_TYPE);
2602 : :
2603 : 42 : case REAL_TYPE:
2604 : 42 : case FIXED_POINT_TYPE:
2605 : 42 : case VOID_TYPE:
2606 : 42 : return TREE_CODE (type) == TREE_CODE (orig);
2607 : :
2608 : 201 : case VECTOR_TYPE:
2609 : 201 : return (VECTOR_TYPE_P (orig)
2610 : 306 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2611 : : TYPE_VECTOR_SUBPARTS (orig))
2612 : 210 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2613 : :
2614 : : default:
2615 : : return false;
2616 : : }
2617 : : }
2618 : :
2619 : : /* Convert expression ARG to type TYPE. Used by the middle-end for
2620 : : simple conversions in preference to calling the front-end's convert. */
2621 : :
2622 : : tree
2623 : 1781685291 : fold_convert_loc (location_t loc, tree type, tree arg)
2624 : : {
2625 : 1781685291 : tree orig = TREE_TYPE (arg);
2626 : 1781685291 : tree tem;
2627 : :
2628 : 1781685291 : if (type == orig)
2629 : : return arg;
2630 : :
2631 : 1135155374 : if (TREE_CODE (arg) == ERROR_MARK
2632 : 1135154363 : || TREE_CODE (type) == ERROR_MARK
2633 : 1135154362 : || TREE_CODE (orig) == ERROR_MARK)
2634 : 1012 : return error_mark_node;
2635 : :
2636 : 1135154362 : switch (TREE_CODE (type))
2637 : : {
2638 : 54530571 : case POINTER_TYPE:
2639 : 54530571 : case REFERENCE_TYPE:
2640 : : /* Handle conversions between pointers to different address spaces. */
2641 : 54530571 : if (POINTER_TYPE_P (orig)
2642 : 54530571 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2643 : 44322732 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2644 : 125 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2645 : : /* fall through */
2646 : :
2647 : 1104522270 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2648 : 1104522270 : case OFFSET_TYPE: case BITINT_TYPE:
2649 : 1104522270 : if (TREE_CODE (arg) == INTEGER_CST)
2650 : : {
2651 : 931796705 : tem = fold_convert_const (NOP_EXPR, type, arg);
2652 : 931796705 : if (tem != NULL_TREE)
2653 : : return tem;
2654 : : }
2655 : 172725565 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2656 : 3026 : || TREE_CODE (orig) == OFFSET_TYPE)
2657 : 172725565 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2658 : 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2659 : 0 : return fold_convert_loc (loc, type,
2660 : : fold_build1_loc (loc, REALPART_EXPR,
2661 : 0 : TREE_TYPE (orig), arg));
2662 : 0 : gcc_assert (VECTOR_TYPE_P (orig)
2663 : : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2664 : 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2665 : :
2666 : 537550 : case REAL_TYPE:
2667 : 537550 : if (TREE_CODE (arg) == INTEGER_CST)
2668 : : {
2669 : 57263 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2670 : 57263 : if (tem != NULL_TREE)
2671 : : return tem;
2672 : : }
2673 : 480287 : else if (TREE_CODE (arg) == REAL_CST)
2674 : : {
2675 : 117231 : tem = fold_convert_const (NOP_EXPR, type, arg);
2676 : 117231 : if (tem != NULL_TREE)
2677 : : return tem;
2678 : : }
2679 : 363056 : else if (TREE_CODE (arg) == FIXED_CST)
2680 : : {
2681 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2682 : 0 : if (tem != NULL_TREE)
2683 : : return tem;
2684 : : }
2685 : :
2686 : 363058 : switch (TREE_CODE (orig))
2687 : : {
2688 : 642 : case INTEGER_TYPE: case BITINT_TYPE:
2689 : 642 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2690 : 642 : case POINTER_TYPE: case REFERENCE_TYPE:
2691 : 642 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2692 : :
2693 : 362416 : case REAL_TYPE:
2694 : 362416 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2695 : :
2696 : 0 : case FIXED_POINT_TYPE:
2697 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2698 : :
2699 : 0 : case COMPLEX_TYPE:
2700 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2701 : 0 : return fold_convert_loc (loc, type, tem);
2702 : :
2703 : 0 : default:
2704 : 0 : gcc_unreachable ();
2705 : : }
2706 : :
2707 : 0 : case FIXED_POINT_TYPE:
2708 : 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2709 : 0 : || TREE_CODE (arg) == REAL_CST)
2710 : : {
2711 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2712 : 0 : if (tem != NULL_TREE)
2713 : 0 : goto fold_convert_exit;
2714 : : }
2715 : :
2716 : 0 : switch (TREE_CODE (orig))
2717 : : {
2718 : 0 : case FIXED_POINT_TYPE:
2719 : 0 : case INTEGER_TYPE:
2720 : 0 : case ENUMERAL_TYPE:
2721 : 0 : case BOOLEAN_TYPE:
2722 : 0 : case REAL_TYPE:
2723 : 0 : case BITINT_TYPE:
2724 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2725 : :
2726 : 0 : case COMPLEX_TYPE:
2727 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2728 : 0 : return fold_convert_loc (loc, type, tem);
2729 : :
2730 : 0 : default:
2731 : 0 : gcc_unreachable ();
2732 : : }
2733 : :
2734 : 2254 : case COMPLEX_TYPE:
2735 : 2254 : switch (TREE_CODE (orig))
2736 : : {
2737 : 584 : case INTEGER_TYPE: case BITINT_TYPE:
2738 : 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2739 : 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2740 : 584 : case REAL_TYPE:
2741 : 584 : case FIXED_POINT_TYPE:
2742 : 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2743 : 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2744 : 584 : fold_convert_loc (loc, TREE_TYPE (type),
2745 : 584 : integer_zero_node));
2746 : 1670 : case COMPLEX_TYPE:
2747 : 1670 : {
2748 : 1670 : tree rpart, ipart;
2749 : :
2750 : 1670 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2751 : : {
2752 : 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2753 : 1534 : TREE_OPERAND (arg, 0));
2754 : 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2755 : 1534 : TREE_OPERAND (arg, 1));
2756 : 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2757 : : }
2758 : :
2759 : 136 : arg = save_expr (arg);
2760 : 136 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2761 : 136 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2762 : 136 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2763 : 136 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2764 : 136 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2765 : : }
2766 : :
2767 : 0 : default:
2768 : 0 : gcc_unreachable ();
2769 : : }
2770 : :
2771 : 29978852 : case VECTOR_TYPE:
2772 : 29978852 : if (integer_zerop (arg))
2773 : 16735 : return build_zero_vector (type);
2774 : 29962117 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2775 : 29962117 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2776 : : || VECTOR_TYPE_P (orig));
2777 : 29962117 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2778 : :
2779 : 110055 : case VOID_TYPE:
2780 : 110055 : tem = fold_ignored_result (arg);
2781 : 110055 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2782 : :
2783 : 3256 : default:
2784 : 3256 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2785 : 3256 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2786 : 0 : gcc_unreachable ();
2787 : : }
2788 : 0 : fold_convert_exit:
2789 : 0 : tem = protected_set_expr_location_unshare (tem, loc);
2790 : 0 : return tem;
2791 : : }
2792 : :
2793 : : /* Return false if expr can be assumed not to be an lvalue, true
2794 : : otherwise. */
2795 : :
2796 : : static bool
2797 : 47686516 : maybe_lvalue_p (const_tree x)
2798 : : {
2799 : : /* We only need to wrap lvalue tree codes. */
2800 : 47686516 : switch (TREE_CODE (x))
2801 : : {
2802 : : case VAR_DECL:
2803 : : case PARM_DECL:
2804 : : case RESULT_DECL:
2805 : : case LABEL_DECL:
2806 : : case FUNCTION_DECL:
2807 : : case SSA_NAME:
2808 : : case COMPOUND_LITERAL_EXPR:
2809 : :
2810 : : case COMPONENT_REF:
2811 : : case MEM_REF:
2812 : : case INDIRECT_REF:
2813 : : case ARRAY_REF:
2814 : : case ARRAY_RANGE_REF:
2815 : : case BIT_FIELD_REF:
2816 : : case OBJ_TYPE_REF:
2817 : :
2818 : : case REALPART_EXPR:
2819 : : case IMAGPART_EXPR:
2820 : : case PREINCREMENT_EXPR:
2821 : : case PREDECREMENT_EXPR:
2822 : : case SAVE_EXPR:
2823 : : case TRY_CATCH_EXPR:
2824 : : case WITH_CLEANUP_EXPR:
2825 : : case COMPOUND_EXPR:
2826 : : case MODIFY_EXPR:
2827 : : case TARGET_EXPR:
2828 : : case COND_EXPR:
2829 : : case BIND_EXPR:
2830 : : case VIEW_CONVERT_EXPR:
2831 : : break;
2832 : :
2833 : 35889235 : default:
2834 : : /* Assume the worst for front-end tree codes. */
2835 : 35889235 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2836 : : break;
2837 : : return false;
2838 : : }
2839 : :
2840 : 11853530 : return true;
2841 : : }
2842 : :
2843 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2844 : :
2845 : : tree
2846 : 44545861 : non_lvalue_loc (location_t loc, tree x)
2847 : : {
2848 : : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2849 : : us. */
2850 : 44545861 : if (in_gimple_form)
2851 : : return x;
2852 : :
2853 : 9349479 : if (! maybe_lvalue_p (x))
2854 : : return x;
2855 : 2016195 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2856 : : }
2857 : :
2858 : : /* Given a tree comparison code, return the code that is the logical inverse.
2859 : : It is generally not safe to do this for floating-point comparisons, except
2860 : : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2861 : : ERROR_MARK in this case. */
2862 : :
2863 : : enum tree_code
2864 : 115542728 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2865 : : {
2866 : 115542728 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2867 : 1076355 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2868 : : return ERROR_MARK;
2869 : :
2870 : 114729568 : switch (code)
2871 : : {
2872 : : case EQ_EXPR:
2873 : : return NE_EXPR;
2874 : 51242254 : case NE_EXPR:
2875 : 51242254 : return EQ_EXPR;
2876 : 11369393 : case GT_EXPR:
2877 : 11369393 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2878 : 12840598 : case GE_EXPR:
2879 : 12840598 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2880 : 7151135 : case LT_EXPR:
2881 : 7151135 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2882 : 7427557 : case LE_EXPR:
2883 : 7427557 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2884 : 255 : case LTGT_EXPR:
2885 : 255 : return UNEQ_EXPR;
2886 : 303 : case UNEQ_EXPR:
2887 : 303 : return LTGT_EXPR;
2888 : : case UNGT_EXPR:
2889 : : return LE_EXPR;
2890 : : case UNGE_EXPR:
2891 : : return LT_EXPR;
2892 : : case UNLT_EXPR:
2893 : : return GE_EXPR;
2894 : : case UNLE_EXPR:
2895 : : return GT_EXPR;
2896 : 237083 : case ORDERED_EXPR:
2897 : 237083 : return UNORDERED_EXPR;
2898 : 53702 : case UNORDERED_EXPR:
2899 : 53702 : return ORDERED_EXPR;
2900 : 0 : default:
2901 : 0 : gcc_unreachable ();
2902 : : }
2903 : : }
2904 : :
2905 : : /* Similar, but return the comparison that results if the operands are
2906 : : swapped. This is safe for floating-point. */
2907 : :
2908 : : enum tree_code
2909 : 146195647 : swap_tree_comparison (enum tree_code code)
2910 : : {
2911 : 146195647 : switch (code)
2912 : : {
2913 : : case EQ_EXPR:
2914 : : case NE_EXPR:
2915 : : case ORDERED_EXPR:
2916 : : case UNORDERED_EXPR:
2917 : : case LTGT_EXPR:
2918 : : case UNEQ_EXPR:
2919 : : return code;
2920 : 34208973 : case GT_EXPR:
2921 : 34208973 : return LT_EXPR;
2922 : 10438079 : case GE_EXPR:
2923 : 10438079 : return LE_EXPR;
2924 : 20271545 : case LT_EXPR:
2925 : 20271545 : return GT_EXPR;
2926 : 14494224 : case LE_EXPR:
2927 : 14494224 : return GE_EXPR;
2928 : 269562 : case UNGT_EXPR:
2929 : 269562 : return UNLT_EXPR;
2930 : 19551 : case UNGE_EXPR:
2931 : 19551 : return UNLE_EXPR;
2932 : 402915 : case UNLT_EXPR:
2933 : 402915 : return UNGT_EXPR;
2934 : 131505 : case UNLE_EXPR:
2935 : 131505 : return UNGE_EXPR;
2936 : 0 : default:
2937 : 0 : gcc_unreachable ();
2938 : : }
2939 : : }
2940 : :
2941 : :
2942 : : /* Convert a comparison tree code from an enum tree_code representation
2943 : : into a compcode bit-based encoding. This function is the inverse of
2944 : : compcode_to_comparison. */
2945 : :
2946 : : static enum comparison_code
2947 : 54336 : comparison_to_compcode (enum tree_code code)
2948 : : {
2949 : 54336 : switch (code)
2950 : : {
2951 : : case LT_EXPR:
2952 : : return COMPCODE_LT;
2953 : : case EQ_EXPR:
2954 : : return COMPCODE_EQ;
2955 : : case LE_EXPR:
2956 : : return COMPCODE_LE;
2957 : : case GT_EXPR:
2958 : : return COMPCODE_GT;
2959 : : case NE_EXPR:
2960 : : return COMPCODE_NE;
2961 : : case GE_EXPR:
2962 : : return COMPCODE_GE;
2963 : : case ORDERED_EXPR:
2964 : : return COMPCODE_ORD;
2965 : : case UNORDERED_EXPR:
2966 : : return COMPCODE_UNORD;
2967 : : case UNLT_EXPR:
2968 : : return COMPCODE_UNLT;
2969 : : case UNEQ_EXPR:
2970 : : return COMPCODE_UNEQ;
2971 : : case UNLE_EXPR:
2972 : : return COMPCODE_UNLE;
2973 : : case UNGT_EXPR:
2974 : : return COMPCODE_UNGT;
2975 : : case LTGT_EXPR:
2976 : : return COMPCODE_LTGT;
2977 : : case UNGE_EXPR:
2978 : : return COMPCODE_UNGE;
2979 : 0 : default:
2980 : 0 : gcc_unreachable ();
2981 : : }
2982 : : }
2983 : :
2984 : : /* Convert a compcode bit-based encoding of a comparison operator back
2985 : : to GCC's enum tree_code representation. This function is the
2986 : : inverse of comparison_to_compcode. */
2987 : :
2988 : : static enum tree_code
2989 : 13373 : compcode_to_comparison (enum comparison_code code)
2990 : : {
2991 : 13373 : switch (code)
2992 : : {
2993 : : case COMPCODE_LT:
2994 : : return LT_EXPR;
2995 : : case COMPCODE_EQ:
2996 : : return EQ_EXPR;
2997 : : case COMPCODE_LE:
2998 : : return LE_EXPR;
2999 : : case COMPCODE_GT:
3000 : : return GT_EXPR;
3001 : : case COMPCODE_NE:
3002 : : return NE_EXPR;
3003 : : case COMPCODE_GE:
3004 : : return GE_EXPR;
3005 : : case COMPCODE_ORD:
3006 : : return ORDERED_EXPR;
3007 : : case COMPCODE_UNORD:
3008 : : return UNORDERED_EXPR;
3009 : : case COMPCODE_UNLT:
3010 : : return UNLT_EXPR;
3011 : : case COMPCODE_UNEQ:
3012 : : return UNEQ_EXPR;
3013 : : case COMPCODE_UNLE:
3014 : : return UNLE_EXPR;
3015 : : case COMPCODE_UNGT:
3016 : : return UNGT_EXPR;
3017 : : case COMPCODE_LTGT:
3018 : : return LTGT_EXPR;
3019 : : case COMPCODE_UNGE:
3020 : : return UNGE_EXPR;
3021 : 0 : default:
3022 : 0 : gcc_unreachable ();
3023 : : }
3024 : : }
3025 : :
3026 : : /* Return true if COND1 tests the opposite condition of COND2. */
3027 : :
3028 : : bool
3029 : 1320624 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3030 : : {
3031 : 1320624 : return (COMPARISON_CLASS_P (cond1)
3032 : 1232111 : && COMPARISON_CLASS_P (cond2)
3033 : 1227874 : && (invert_tree_comparison
3034 : 1227874 : (TREE_CODE (cond1),
3035 : 2455748 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3036 : 71696 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3037 : 71696 : TREE_OPERAND (cond2, 0), 0)
3038 : 1343528 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3039 : 22904 : TREE_OPERAND (cond2, 1), 0));
3040 : : }
3041 : :
3042 : : /* Return a tree for the comparison which is the combination of
3043 : : doing the AND or OR (depending on CODE) of the two operations LCODE
3044 : : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3045 : : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3046 : : if this makes the transformation invalid. */
3047 : :
3048 : : tree
3049 : 27168 : combine_comparisons (location_t loc,
3050 : : enum tree_code code, enum tree_code lcode,
3051 : : enum tree_code rcode, tree truth_type,
3052 : : tree ll_arg, tree lr_arg)
3053 : : {
3054 : 27168 : bool honor_nans = HONOR_NANS (ll_arg);
3055 : 27168 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3056 : 27168 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3057 : 27168 : int compcode;
3058 : :
3059 : 27168 : switch (code)
3060 : : {
3061 : 17804 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3062 : 17804 : compcode = lcompcode & rcompcode;
3063 : 17804 : break;
3064 : :
3065 : 9364 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3066 : 9364 : compcode = lcompcode | rcompcode;
3067 : 9364 : break;
3068 : :
3069 : : default:
3070 : : return NULL_TREE;
3071 : : }
3072 : :
3073 : 27168 : if (!honor_nans)
3074 : : {
3075 : : /* Eliminate unordered comparisons, as well as LTGT and ORD
3076 : : which are not used unless the mode has NaNs. */
3077 : 22093 : compcode &= ~COMPCODE_UNORD;
3078 : 22093 : if (compcode == COMPCODE_LTGT)
3079 : : compcode = COMPCODE_NE;
3080 : 21109 : else if (compcode == COMPCODE_ORD)
3081 : : compcode = COMPCODE_TRUE;
3082 : : }
3083 : 5075 : else if (flag_trapping_math)
3084 : : {
3085 : : /* Check that the original operation and the optimized ones will trap
3086 : : under the same condition. */
3087 : 8308 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3088 : 3518 : && (lcompcode != COMPCODE_EQ)
3089 : 4154 : && (lcompcode != COMPCODE_ORD);
3090 : 8308 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3091 : 3666 : && (rcompcode != COMPCODE_EQ)
3092 : 4154 : && (rcompcode != COMPCODE_ORD);
3093 : 8308 : bool trap = (compcode & COMPCODE_UNORD) == 0
3094 : 3731 : && (compcode != COMPCODE_EQ)
3095 : 4154 : && (compcode != COMPCODE_ORD);
3096 : :
3097 : : /* In a short-circuited boolean expression the LHS might be
3098 : : such that the RHS, if evaluated, will never trap. For
3099 : : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3100 : : if neither x nor y is NaN. (This is a mixed blessing: for
3101 : : example, the expression above will never trap, hence
3102 : : optimizing it to x < y would be invalid). */
3103 : 4154 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3104 : 3753 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3105 : 4154 : rtrap = false;
3106 : :
3107 : : /* If the comparison was short-circuited, and only the RHS
3108 : : trapped, we may now generate a spurious trap. */
3109 : 4154 : if (rtrap && !ltrap
3110 : 118 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3111 : : return NULL_TREE;
3112 : :
3113 : : /* If we changed the conditions that cause a trap, we lose. */
3114 : 4036 : if ((ltrap || rtrap) != trap)
3115 : : return NULL_TREE;
3116 : : }
3117 : :
3118 : 1642 : if (compcode == COMPCODE_TRUE)
3119 : 1223 : return constant_boolean_node (true, truth_type);
3120 : 22512 : else if (compcode == COMPCODE_FALSE)
3121 : 9139 : return constant_boolean_node (false, truth_type);
3122 : : else
3123 : : {
3124 : 13373 : enum tree_code tcode;
3125 : :
3126 : 13373 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3127 : 13373 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3128 : : }
3129 : : }
3130 : :
3131 : : /* Return nonzero if two operands (typically of the same tree node)
3132 : : are necessarily equal. FLAGS modifies behavior as follows:
3133 : :
3134 : : If OEP_ONLY_CONST is set, only return nonzero for constants.
3135 : : This function tests whether the operands are indistinguishable;
3136 : : it does not test whether they are equal using C's == operation.
3137 : : The distinction is important for IEEE floating point, because
3138 : : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3139 : : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3140 : :
3141 : : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3142 : : even though it may hold multiple values during a function.
3143 : : This is because a GCC tree node guarantees that nothing else is
3144 : : executed between the evaluation of its "operands" (which may often
3145 : : be evaluated in arbitrary order). Hence if the operands themselves
3146 : : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3147 : : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3148 : : unset means assuming isochronic (or instantaneous) tree equivalence.
3149 : : Unless comparing arbitrary expression trees, such as from different
3150 : : statements, this flag can usually be left unset.
3151 : :
3152 : : If OEP_PURE_SAME is set, then pure functions with identical arguments
3153 : : are considered the same. It is used when the caller has other ways
3154 : : to ensure that global memory is unchanged in between.
3155 : :
3156 : : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3157 : : not values of expressions.
3158 : :
3159 : : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3160 : : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3161 : :
3162 : : If OEP_BITWISE is set, then require the values to be bitwise identical
3163 : : rather than simply numerically equal. Do not take advantage of things
3164 : : like math-related flags or undefined behavior; only return true for
3165 : : values that are provably bitwise identical in all circumstances.
3166 : :
3167 : : If OEP_ASSUME_WRAPV is set, then require the values to be bitwise identical
3168 : : under two's compliment arithmetic (ignoring any possible Undefined Behaviour)
3169 : : rather than just numerically equivalent. The compared expressions must
3170 : : however perform the same operations but may do intermediate computations in
3171 : : differing signs. Because this comparison ignores any possible UB it cannot
3172 : : be used blindly without ensuring that the context you are using it in itself
3173 : : doesn't guarantee that there will be no UB. Conditional expressions are
3174 : : excluded from this relaxation.
3175 : :
3176 : : When OEP_ASSUME_WRAPV is used operand_compare::hash_operand may return
3177 : : differing hashes even for cases where operand_compare::operand_equal_p
3178 : : compares equal.
3179 : :
3180 : : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3181 : : any operand with side effect. This is unnecesarily conservative in the
3182 : : case we know that arg0 and arg1 are in disjoint code paths (such as in
3183 : : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3184 : : addresses with TREE_CONSTANT flag set so we know that &var == &var
3185 : : even if var is volatile. */
3186 : :
3187 : : bool
3188 : 6893012791 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3189 : : unsigned int flags)
3190 : : {
3191 : 6893012791 : return operand_equal_p (TREE_TYPE (arg0), arg0, TREE_TYPE (arg1), arg1, flags);
3192 : : }
3193 : :
3194 : : /* The same as operand_equal_p however the type of ARG0 and ARG1 are assumed to
3195 : : be the TYPE0 and TYPE1 respectively. TYPE0 and TYPE1 represent the type the
3196 : : expression is being compared under for equality. This means that they can
3197 : : differ from the actual TREE_TYPE (..) value of ARG0 and ARG1. */
3198 : :
3199 : : bool
3200 : 6893738105 : operand_compare::operand_equal_p (tree type0, const_tree arg0,
3201 : : tree type1, const_tree arg1,
3202 : : unsigned int flags)
3203 : : {
3204 : 6893738105 : bool r;
3205 : 6893738105 : if (verify_hash_value (arg0, arg1, flags, &r))
3206 : 2896029223 : return r;
3207 : :
3208 : 3997708882 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3209 : 3997708882 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3210 : :
3211 : : /* If either is ERROR_MARK, they aren't equal. */
3212 : 3997708882 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3213 : 3997708099 : || type0 == error_mark_node
3214 : 3997708099 : || type1 == error_mark_node)
3215 : : return false;
3216 : :
3217 : : /* Similar, if either does not have a type (like a template id),
3218 : : they aren't equal. */
3219 : 3997708099 : if (!type0 || !type1)
3220 : : return false;
3221 : :
3222 : : /* Bitwise identity makes no sense if the values have different layouts. */
3223 : 3997705595 : if ((flags & OEP_BITWISE)
3224 : 3997705595 : && !tree_nop_conversion_p (type0, type1))
3225 : : return false;
3226 : :
3227 : : /* We cannot consider pointers to different address space equal. */
3228 : 3997705595 : if (POINTER_TYPE_P (type0)
3229 : 581960254 : && POINTER_TYPE_P (type1)
3230 : 4490320004 : && (TYPE_ADDR_SPACE (TREE_TYPE (type0))
3231 : 492614409 : != TYPE_ADDR_SPACE (TREE_TYPE (type1))))
3232 : : return false;
3233 : :
3234 : : /* Check equality of integer constants before bailing out due to
3235 : : precision differences. */
3236 : 3997705396 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3237 : : {
3238 : : /* Address of INTEGER_CST is not defined; check that we did not forget
3239 : : to drop the OEP_ADDRESS_OF flags. */
3240 : 630168849 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3241 : 630168849 : return tree_int_cst_equal (arg0, arg1);
3242 : : }
3243 : :
3244 : 3367536547 : if ((flags & OEP_ASSUME_WRAPV)
3245 : 2054476 : && (CONVERT_EXPR_P (arg0) || CONVERT_EXPR_P (arg1)))
3246 : : {
3247 : 778463 : const_tree t_arg0 = arg0;
3248 : 778463 : const_tree t_arg1 = arg1;
3249 : 778463 : STRIP_NOPS (arg0);
3250 : 778463 : STRIP_NOPS (arg1);
3251 : : /* Only recurse if the conversion was one that was valid to strip. */
3252 : 778463 : if (t_arg0 != arg0 || t_arg1 != arg1)
3253 : 725314 : return operand_equal_p (type0, arg0, type1, arg1, flags);
3254 : : }
3255 : :
3256 : 3366811233 : if (!(flags & OEP_ADDRESS_OF))
3257 : : {
3258 : : /* Check if we are checking an operation where the two's compliment
3259 : : bitwise representation of the result is not the same between signed and
3260 : : unsigned arithmetic. */
3261 : 2990411500 : bool enforce_signedness = true;
3262 : 2990411500 : if (flags & OEP_ASSUME_WRAPV)
3263 : : {
3264 : 1244789 : switch (TREE_CODE (arg0))
3265 : : {
3266 : : case PLUS_EXPR:
3267 : : case MINUS_EXPR:
3268 : : case MULT_EXPR:
3269 : : case BIT_IOR_EXPR:
3270 : : case BIT_XOR_EXPR:
3271 : : case BIT_AND_EXPR:
3272 : : case BIT_NOT_EXPR:
3273 : : case ABS_EXPR:
3274 : : CASE_CONVERT:
3275 : : case SSA_NAME:
3276 : : case INTEGER_CST:
3277 : : case VAR_DECL:
3278 : : case PARM_DECL:
3279 : : case RESULT_DECL:
3280 : 2990411500 : enforce_signedness = false;
3281 : : break;
3282 : :
3283 : : default:
3284 : : break;
3285 : : }
3286 : : }
3287 : :
3288 : : /* If both types don't have the same signedness, then we can't consider
3289 : : them equal. We must check this before the STRIP_NOPS calls
3290 : : because they may change the signedness of the arguments. As pointers
3291 : : strictly don't have a signedness, require either two pointers or
3292 : : two non-pointers as well. */
3293 : 2990411500 : if (POINTER_TYPE_P (type0) != POINTER_TYPE_P (type1)
3294 : 2990411500 : || (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1)
3295 : 136781252 : && enforce_signedness))
3296 : : return false;
3297 : :
3298 : : /* If both types don't have the same precision, then it is not safe
3299 : : to strip NOPs. */
3300 : 2704718252 : if (element_precision (type0) != element_precision (type1))
3301 : : return false;
3302 : :
3303 : 2559285419 : STRIP_NOPS (arg0);
3304 : 2559285419 : STRIP_NOPS (arg1);
3305 : :
3306 : 2559285419 : type0 = TREE_TYPE (arg0);
3307 : 2559285419 : type1 = TREE_TYPE (arg1);
3308 : : }
3309 : : #if 0
3310 : : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3311 : : sanity check once the issue is solved. */
3312 : : else
3313 : : /* Addresses of conversions and SSA_NAMEs (and many other things)
3314 : : are not defined. Check that we did not forget to drop the
3315 : : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3316 : : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3317 : : && TREE_CODE (arg0) != SSA_NAME);
3318 : : #endif
3319 : :
3320 : : /* In case both args are comparisons but with different comparison
3321 : : code, try to swap the comparison operands of one arg to produce
3322 : : a match and compare that variant. */
3323 : 2935685152 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3324 : 1190464764 : && COMPARISON_CLASS_P (arg0)
3325 : 6031806 : && COMPARISON_CLASS_P (arg1))
3326 : : {
3327 : 4456581 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3328 : :
3329 : 4456581 : if (TREE_CODE (arg0) == swap_code)
3330 : 1963526 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3331 : 1963526 : TREE_OPERAND (arg1, 1), flags)
3332 : 1983803 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3333 : 20277 : TREE_OPERAND (arg1, 0), flags);
3334 : : }
3335 : :
3336 : 2933721626 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3337 : : {
3338 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3339 : 1188501238 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3340 : : ;
3341 : 1188483541 : else if (flags & OEP_ADDRESS_OF)
3342 : : {
3343 : : /* If we are interested in comparing addresses ignore
3344 : : MEM_REF wrappings of the base that can appear just for
3345 : : TBAA reasons. */
3346 : 47584483 : if (TREE_CODE (arg0) == MEM_REF
3347 : 7409259 : && DECL_P (arg1)
3348 : 5226460 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3349 : 1353788 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3350 : 48088669 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3351 : : return true;
3352 : 47356788 : else if (TREE_CODE (arg1) == MEM_REF
3353 : 29858788 : && DECL_P (arg0)
3354 : 10978649 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3355 : 2559073 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3356 : 48036088 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3357 : : return true;
3358 : 46848025 : return false;
3359 : : }
3360 : : else
3361 : : return false;
3362 : : }
3363 : :
3364 : : /* When not checking adddresses, this is needed for conversions and for
3365 : : COMPONENT_REF. Might as well play it safe and always test this. */
3366 : 1745238085 : if (TREE_CODE (type0) == ERROR_MARK
3367 : 1745238085 : || TREE_CODE (type1) == ERROR_MARK
3368 : 3490476170 : || (TYPE_MODE (type0) != TYPE_MODE (type1)
3369 : 24436931 : && !(flags & OEP_ADDRESS_OF)))
3370 : 3591073 : return false;
3371 : :
3372 : : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3373 : : We don't care about side effects in that case because the SAVE_EXPR
3374 : : takes care of that for us. In all other cases, two expressions are
3375 : : equal if they have no side effects. If we have two identical
3376 : : expressions with side effects that should be treated the same due
3377 : : to the only side effects being identical SAVE_EXPR's, that will
3378 : : be detected in the recursive calls below.
3379 : : If we are taking an invariant address of two identical objects
3380 : : they are necessarily equal as well. */
3381 : 308380894 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3382 : 2050027728 : && (TREE_CODE (arg0) == SAVE_EXPR
3383 : 308368180 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3384 : 274873057 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3385 : : return true;
3386 : :
3387 : : /* Next handle constant cases, those for which we can return 1 even
3388 : : if ONLY_CONST is set. */
3389 : 1433408526 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3390 : 18972863 : switch (TREE_CODE (arg0))
3391 : : {
3392 : 153 : case INTEGER_CST:
3393 : 153 : return tree_int_cst_equal (arg0, arg1);
3394 : :
3395 : 0 : case FIXED_CST:
3396 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3397 : : TREE_FIXED_CST (arg1));
3398 : :
3399 : 3632070 : case REAL_CST:
3400 : 3632070 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3401 : : return true;
3402 : :
3403 : 2619136 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3404 : : {
3405 : : /* If we do not distinguish between signed and unsigned zero,
3406 : : consider them equal. */
3407 : 13996 : if (real_zerop (arg0) && real_zerop (arg1))
3408 : : return true;
3409 : : }
3410 : 2619131 : return false;
3411 : :
3412 : 721002 : case VECTOR_CST:
3413 : 721002 : {
3414 : 721002 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3415 : 721002 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3416 : : return false;
3417 : :
3418 : 709467 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3419 : 709467 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3420 : : return false;
3421 : :
3422 : 684463 : unsigned int count = vector_cst_encoded_nelts (arg0);
3423 : 943454 : for (unsigned int i = 0; i < count; ++i)
3424 : 1456878 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3425 : 728439 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3426 : : return false;
3427 : : return true;
3428 : : }
3429 : :
3430 : 12762 : case COMPLEX_CST:
3431 : 12762 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3432 : : flags)
3433 : 12762 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3434 : : flags));
3435 : :
3436 : 1148103 : case STRING_CST:
3437 : 1148103 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3438 : 1148103 : && ! memcmp (TREE_STRING_POINTER (arg0),
3439 : 739712 : TREE_STRING_POINTER (arg1),
3440 : 739712 : TREE_STRING_LENGTH (arg0)));
3441 : :
3442 : 0 : case RAW_DATA_CST:
3443 : 0 : return (RAW_DATA_LENGTH (arg0) == RAW_DATA_LENGTH (arg1)
3444 : 0 : && ! memcmp (RAW_DATA_POINTER (arg0),
3445 : 0 : RAW_DATA_POINTER (arg1),
3446 : 0 : RAW_DATA_LENGTH (arg0)));
3447 : :
3448 : 12558374 : case ADDR_EXPR:
3449 : 12558374 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3450 : 12558374 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3451 : : flags | OEP_ADDRESS_OF
3452 : 12558374 : | OEP_MATCH_SIDE_EFFECTS);
3453 : 173226 : case CONSTRUCTOR:
3454 : 173226 : {
3455 : : /* In GIMPLE empty constructors are allowed in initializers of
3456 : : aggregates. */
3457 : 173226 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3458 : : return true;
3459 : :
3460 : : /* See sem_variable::equals in ipa-icf for a similar approach. */
3461 : 135319 : if (TREE_CODE (type0) != TREE_CODE (type1))
3462 : : return false;
3463 : 135319 : else if (TREE_CODE (type0) == ARRAY_TYPE)
3464 : : {
3465 : : /* For arrays, check that the sizes all match. */
3466 : 14 : const HOST_WIDE_INT siz0 = int_size_in_bytes (type0);
3467 : 14 : if (TYPE_MODE (type0) != TYPE_MODE (type1)
3468 : 14 : || siz0 < 0
3469 : 28 : || siz0 != int_size_in_bytes (type1))
3470 : 0 : return false;
3471 : : }
3472 : 135305 : else if (!types_compatible_p (type0, type1))
3473 : : return false;
3474 : :
3475 : 135319 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3476 : 135319 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3477 : 405957 : if (vec_safe_length (v0) != vec_safe_length (v1))
3478 : : return false;
3479 : :
3480 : : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3481 : : of the CONSTRUCTOR referenced indirectly. */
3482 : 135319 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3483 : :
3484 : 343386972 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3485 : : {
3486 : 193257 : constructor_elt *c0 = &(*v0)[idx];
3487 : 193257 : constructor_elt *c1 = &(*v1)[idx];
3488 : :
3489 : : /* Check that the values are the same... */
3490 : 193257 : if (c0->value != c1->value
3491 : 193257 : && !operand_equal_p (c0->value, c1->value, flags))
3492 : : return false;
3493 : :
3494 : : /* ... and that they apply to the same field! */
3495 : 107223 : if (c0->index != c1->index
3496 : 107223 : && (TREE_CODE (type0) == ARRAY_TYPE
3497 : 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3498 : 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3499 : 0 : DECL_FIELD_OFFSET (c1->index),
3500 : : flags)
3501 : 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3502 : 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3503 : : flags)))
3504 : 0 : return false;
3505 : : }
3506 : :
3507 : : return true;
3508 : : }
3509 : :
3510 : : default:
3511 : : break;
3512 : : }
3513 : :
3514 : : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3515 : : two instances of undefined behavior will give identical results. */
3516 : 1415162836 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3517 : : return false;
3518 : :
3519 : : /* Define macros to test an operand from arg0 and arg1 for equality and a
3520 : : variant that allows null and views null as being different from any
3521 : : non-null value. In the latter case, if either is null, the both
3522 : : must be; otherwise, do the normal comparison. */
3523 : : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3524 : : TREE_OPERAND (arg1, N), flags)
3525 : :
3526 : : #define OP_SAME_WITH_NULL(N) \
3527 : : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3528 : : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3529 : :
3530 : 1415162836 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3531 : : {
3532 : 7022956 : case tcc_unary:
3533 : : /* Two conversions are equal only if signedness and modes match. */
3534 : 7022956 : switch (TREE_CODE (arg0))
3535 : : {
3536 : 6658218 : CASE_CONVERT:
3537 : 6658218 : case FIX_TRUNC_EXPR:
3538 : 6658218 : if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
3539 : : return false;
3540 : : break;
3541 : : default:
3542 : : break;
3543 : : }
3544 : :
3545 : 7022935 : return OP_SAME_WITH_NULL (0);
3546 : :
3547 : :
3548 : 21371775 : case tcc_comparison:
3549 : 21371775 : case tcc_binary:
3550 : 21371775 : if (OP_SAME (0) && OP_SAME (1))
3551 : : return true;
3552 : :
3553 : : /* For commutative ops, allow the other order. */
3554 : 15835445 : return (commutative_tree_code (TREE_CODE (arg0))
3555 : 12091195 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3556 : 12091195 : TREE_OPERAND (arg1, 1), flags)
3557 : 16020590 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3558 : 185145 : TREE_OPERAND (arg1, 0), flags));
3559 : :
3560 : 859846514 : case tcc_reference:
3561 : : /* If either of the pointer (or reference) expressions we are
3562 : : dereferencing contain a side effect, these cannot be equal,
3563 : : but their addresses can be. */
3564 : 859846514 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3565 : 859846514 : && (TREE_SIDE_EFFECTS (arg0)
3566 : 799275250 : || TREE_SIDE_EFFECTS (arg1)))
3567 : : return false;
3568 : :
3569 : 859632152 : switch (TREE_CODE (arg0))
3570 : : {
3571 : 3844607 : case INDIRECT_REF:
3572 : 3844607 : if (!(flags & OEP_ADDRESS_OF))
3573 : : {
3574 : 3822221 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3575 : : return false;
3576 : : /* Verify that the access types are compatible. */
3577 : 3820812 : if (TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
3578 : : return false;
3579 : : }
3580 : 3781305 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3581 : 3781305 : return OP_SAME (0);
3582 : :
3583 : 655806 : case IMAGPART_EXPR:
3584 : : /* Require the same offset. */
3585 : 655806 : if (!operand_equal_p (TYPE_SIZE (type0),
3586 : 655806 : TYPE_SIZE (type1),
3587 : : flags & ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV)))
3588 : : return false;
3589 : :
3590 : : /* Fallthru. */
3591 : 2410574 : case REALPART_EXPR:
3592 : 2410574 : case VIEW_CONVERT_EXPR:
3593 : 2410574 : return OP_SAME (0);
3594 : :
3595 : 75569665 : case TARGET_MEM_REF:
3596 : 75569665 : case MEM_REF:
3597 : 75569665 : if (!(flags & OEP_ADDRESS_OF))
3598 : : {
3599 : : /* Require equal access sizes */
3600 : 16113065 : if (TYPE_SIZE (type0) != TYPE_SIZE (type1)
3601 : 16113065 : && (!TYPE_SIZE (type0)
3602 : 1057048 : || !TYPE_SIZE (type1)
3603 : 1052525 : || !operand_equal_p (TYPE_SIZE (type0),
3604 : 1052525 : TYPE_SIZE (type1),
3605 : : flags)))
3606 : 1057227 : return false;
3607 : : /* Verify that access happens in similar types. */
3608 : 15055838 : if (!types_compatible_p (type0, type1))
3609 : : return false;
3610 : : /* Verify that accesses are TBAA compatible. */
3611 : 14714709 : if (!alias_ptr_types_compatible_p
3612 : 14714709 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3613 : 14714709 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3614 : 13807627 : || (MR_DEPENDENCE_CLIQUE (arg0)
3615 : 13807627 : != MR_DEPENDENCE_CLIQUE (arg1))
3616 : 26465455 : || (MR_DEPENDENCE_BASE (arg0)
3617 : 11750746 : != MR_DEPENDENCE_BASE (arg1)))
3618 : : return false;
3619 : : /* Verify that alignment is compatible. */
3620 : 11319536 : if (TYPE_ALIGN (type0) != TYPE_ALIGN (type1))
3621 : : return false;
3622 : : }
3623 : 70624744 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3624 : 120568031 : return (OP_SAME (0) && OP_SAME (1)
3625 : : /* TARGET_MEM_REF require equal extra operands. */
3626 : 95069815 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3627 : 538636 : || (OP_SAME_WITH_NULL (2)
3628 : 270776 : && OP_SAME_WITH_NULL (3)
3629 : 265822 : && OP_SAME_WITH_NULL (4))));
3630 : :
3631 : 33389184 : case ARRAY_REF:
3632 : 33389184 : case ARRAY_RANGE_REF:
3633 : 33389184 : if (!OP_SAME (0))
3634 : : return false;
3635 : 28664331 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3636 : : /* Compare the array index by value if it is constant first as we
3637 : : may have different types but same value here. */
3638 : 28664331 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3639 : 28664331 : TREE_OPERAND (arg1, 1))
3640 : 25759979 : || OP_SAME (1))
3641 : 5836222 : && OP_SAME_WITH_NULL (2)
3642 : 5836124 : && OP_SAME_WITH_NULL (3)
3643 : : /* Compare low bound and element size as with OEP_ADDRESS_OF
3644 : : we have to account for the offset of the ref. */
3645 : 37418566 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3646 : 2918062 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3647 : 2759 : || (operand_equal_p (array_ref_low_bound
3648 : 2759 : (CONST_CAST_TREE (arg0)),
3649 : : array_ref_low_bound
3650 : 2759 : (CONST_CAST_TREE (arg1)), flags)
3651 : 2759 : && operand_equal_p (array_ref_element_size
3652 : 2759 : (CONST_CAST_TREE (arg0)),
3653 : : array_ref_element_size
3654 : 2759 : (CONST_CAST_TREE (arg1)),
3655 : : flags))));
3656 : :
3657 : 743942274 : case COMPONENT_REF:
3658 : : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3659 : : may be NULL when we're called to compare MEM_EXPRs. */
3660 : 743942274 : if (!OP_SAME_WITH_NULL (0))
3661 : : return false;
3662 : 56028733 : {
3663 : 56028733 : bool compare_address = flags & OEP_ADDRESS_OF;
3664 : :
3665 : : /* Most of time we only need to compare FIELD_DECLs for equality.
3666 : : However when determining address look into actual offsets.
3667 : : These may match for unions and unshared record types. */
3668 : 56028733 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3669 : 56028733 : if (!OP_SAME (1))
3670 : : {
3671 : 32667317 : if (compare_address
3672 : 601720 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3673 : : {
3674 : 601717 : tree field0 = TREE_OPERAND (arg0, 1);
3675 : 601717 : tree field1 = TREE_OPERAND (arg1, 1);
3676 : :
3677 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3678 : 601717 : if (TREE_CODE (field0) != FIELD_DECL
3679 : 601717 : || TREE_CODE (field1) != FIELD_DECL)
3680 : : return false;
3681 : :
3682 : 601717 : if (!DECL_FIELD_OFFSET (field0)
3683 : 601717 : || !DECL_FIELD_OFFSET (field1))
3684 : 3 : return field0 == field1;
3685 : :
3686 : 601714 : if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
3687 : 601714 : DECL_FIELD_OFFSET (field1), flags)
3688 : 792170 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3689 : 190456 : DECL_FIELD_BIT_OFFSET (field1),
3690 : : flags))
3691 : 563848 : return false;
3692 : : }
3693 : : else
3694 : : return false;
3695 : : }
3696 : : }
3697 : 23399282 : return OP_SAME_WITH_NULL (2);
3698 : :
3699 : 475800 : case BIT_FIELD_REF:
3700 : 475800 : if (!OP_SAME (0))
3701 : : return false;
3702 : 370796 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3703 : 370796 : return OP_SAME (1) && OP_SAME (2);
3704 : :
3705 : : default:
3706 : : return false;
3707 : : }
3708 : :
3709 : 48289633 : case tcc_expression:
3710 : 48289633 : switch (TREE_CODE (arg0))
3711 : : {
3712 : 43402221 : case ADDR_EXPR:
3713 : : /* Be sure we pass right ADDRESS_OF flag. */
3714 : 43402221 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3715 : 43402221 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3716 : 43402221 : TREE_OPERAND (arg1, 0),
3717 : 43402221 : flags | OEP_ADDRESS_OF);
3718 : :
3719 : 657727 : case TRUTH_NOT_EXPR:
3720 : 657727 : return OP_SAME (0);
3721 : :
3722 : 39481 : case TRUTH_ANDIF_EXPR:
3723 : 39481 : case TRUTH_ORIF_EXPR:
3724 : 39481 : return OP_SAME (0) && OP_SAME (1);
3725 : :
3726 : 0 : case WIDEN_MULT_PLUS_EXPR:
3727 : 0 : case WIDEN_MULT_MINUS_EXPR:
3728 : 0 : if (!OP_SAME (2))
3729 : : return false;
3730 : : /* The multiplcation operands are commutative. */
3731 : : /* FALLTHRU */
3732 : :
3733 : 19163 : case TRUTH_AND_EXPR:
3734 : 19163 : case TRUTH_OR_EXPR:
3735 : 19163 : case TRUTH_XOR_EXPR:
3736 : 19163 : if (OP_SAME (0) && OP_SAME (1))
3737 : : return true;
3738 : :
3739 : : /* Otherwise take into account this is a commutative operation. */
3740 : 19145 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3741 : 19145 : TREE_OPERAND (arg1, 1), flags)
3742 : 19148 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3743 : 3 : TREE_OPERAND (arg1, 0), flags));
3744 : :
3745 : 117089 : case COND_EXPR:
3746 : 117089 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3747 : 47862 : return false;
3748 : 69227 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3749 : 69227 : return OP_SAME (0);
3750 : :
3751 : 4 : case BIT_INSERT_EXPR:
3752 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
3753 : : of op1. Need to check to make sure they are the same. */
3754 : 4 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3755 : 1 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3756 : 5 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3757 : 1 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3758 : : return false;
3759 : : /* FALLTHRU */
3760 : :
3761 : 191 : case VEC_COND_EXPR:
3762 : 191 : case DOT_PROD_EXPR:
3763 : 191 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3764 : :
3765 : 23103 : case MODIFY_EXPR:
3766 : 23103 : case INIT_EXPR:
3767 : 23103 : case COMPOUND_EXPR:
3768 : 23103 : case PREDECREMENT_EXPR:
3769 : 23103 : case PREINCREMENT_EXPR:
3770 : 23103 : case POSTDECREMENT_EXPR:
3771 : 23103 : case POSTINCREMENT_EXPR:
3772 : 23103 : if (flags & OEP_LEXICOGRAPHIC)
3773 : 163 : return OP_SAME (0) && OP_SAME (1);
3774 : : return false;
3775 : :
3776 : 86777 : case CLEANUP_POINT_EXPR:
3777 : 86777 : case EXPR_STMT:
3778 : 86777 : case SAVE_EXPR:
3779 : 86777 : if (flags & OEP_LEXICOGRAPHIC)
3780 : 208 : return OP_SAME (0);
3781 : : return false;
3782 : :
3783 : 101706 : case OBJ_TYPE_REF:
3784 : : /* Virtual table reference. */
3785 : 203412 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3786 : 101706 : OBJ_TYPE_REF_EXPR (arg1), flags))
3787 : : return false;
3788 : 881 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
3789 : 881 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3790 : 881 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3791 : : return false;
3792 : 881 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3793 : 881 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3794 : : return false;
3795 : 881 : if (virtual_method_call_p (arg0))
3796 : : {
3797 : 881 : if (!virtual_method_call_p (arg1))
3798 : : return false;
3799 : 881 : return types_same_for_odr (obj_type_ref_class (arg0),
3800 : 1762 : obj_type_ref_class (arg1));
3801 : : }
3802 : : return false;
3803 : :
3804 : : default:
3805 : : return false;
3806 : : }
3807 : :
3808 : 2816729 : case tcc_vl_exp:
3809 : 2816729 : switch (TREE_CODE (arg0))
3810 : : {
3811 : 2816729 : case CALL_EXPR:
3812 : 2816729 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3813 : 2816729 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3814 : : /* If not both CALL_EXPRs are either internal or normal function
3815 : : functions, then they are not equal. */
3816 : : return false;
3817 : 2816729 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3818 : : {
3819 : : /* If the CALL_EXPRs call different internal functions, then they
3820 : : are not equal. */
3821 : 4 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3822 : : return false;
3823 : : }
3824 : : else
3825 : : {
3826 : : /* If the CALL_EXPRs call different functions, then they are not
3827 : : equal. */
3828 : 2816725 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3829 : : flags))
3830 : : return false;
3831 : : }
3832 : :
3833 : : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3834 : 1831426 : {
3835 : 1831426 : unsigned int cef = call_expr_flags (arg0);
3836 : 1831426 : if (flags & OEP_PURE_SAME)
3837 : 0 : cef &= ECF_CONST | ECF_PURE;
3838 : : else
3839 : 1831426 : cef &= ECF_CONST;
3840 : 1831426 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3841 : : return false;
3842 : : }
3843 : :
3844 : : /* Now see if all the arguments are the same. */
3845 : 32547 : {
3846 : 32547 : const_call_expr_arg_iterator iter0, iter1;
3847 : 32547 : const_tree a0, a1;
3848 : 65094 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3849 : 32547 : a1 = first_const_call_expr_arg (arg1, &iter1);
3850 : 40652 : a0 && a1;
3851 : 8105 : a0 = next_const_call_expr_arg (&iter0),
3852 : 8105 : a1 = next_const_call_expr_arg (&iter1))
3853 : 34039 : if (! operand_equal_p (a0, a1, flags))
3854 : : return false;
3855 : :
3856 : : /* If we get here and both argument lists are exhausted
3857 : : then the CALL_EXPRs are equal. */
3858 : 6613 : return ! (a0 || a1);
3859 : : }
3860 : : default:
3861 : : return false;
3862 : : }
3863 : :
3864 : 160820126 : case tcc_declaration:
3865 : : /* Consider __builtin_sqrt equal to sqrt. */
3866 : 160820126 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3867 : 6350234 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3868 : 334321 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3869 : 5697969 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3870 : 334321 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3871 : :
3872 : 155122157 : if (DECL_P (arg0)
3873 : 155122157 : && (flags & OEP_DECL_NAME)
3874 : 35 : && (flags & OEP_LEXICOGRAPHIC))
3875 : : {
3876 : : /* Consider decls with the same name equal. The caller needs
3877 : : to make sure they refer to the same entity (such as a function
3878 : : formal parameter). */
3879 : 35 : tree a0name = DECL_NAME (arg0);
3880 : 35 : tree a1name = DECL_NAME (arg1);
3881 : 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3882 : 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3883 : 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3884 : : }
3885 : : return false;
3886 : :
3887 : 312822941 : case tcc_exceptional:
3888 : 312822941 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3889 : : {
3890 : 19389 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3891 : : return false;
3892 : :
3893 : : /* In GIMPLE constructors are used only to build vectors from
3894 : : elements. Individual elements in the constructor must be
3895 : : indexed in increasing order and form an initial sequence.
3896 : :
3897 : : We make no effort to compare nonconstant ones in GENERIC. */
3898 : 19389 : if (!VECTOR_TYPE_P (type0) || !VECTOR_TYPE_P (type1))
3899 : : return false;
3900 : :
3901 : : /* Be sure that vectors constructed have the same representation.
3902 : : We only tested element precision and modes to match.
3903 : : Vectors may be BLKmode and thus also check that the number of
3904 : : parts match. */
3905 : 543 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
3906 : 1086 : TYPE_VECTOR_SUBPARTS (type1)))
3907 : : return false;
3908 : :
3909 : 543 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3910 : 543 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3911 : 543 : unsigned int len = vec_safe_length (v0);
3912 : :
3913 : 1086 : if (len != vec_safe_length (v1))
3914 : : return false;
3915 : :
3916 : 3377 : for (unsigned int i = 0; i < len; i++)
3917 : : {
3918 : 3004 : constructor_elt *c0 = &(*v0)[i];
3919 : 3004 : constructor_elt *c1 = &(*v1)[i];
3920 : :
3921 : 3004 : if (!operand_equal_p (c0->value, c1->value, flags)
3922 : : /* In GIMPLE the indexes can be either NULL or matching i.
3923 : : Double check this so we won't get false
3924 : : positives for GENERIC. */
3925 : 2834 : || (c0->index
3926 : 2588 : && (TREE_CODE (c0->index) != INTEGER_CST
3927 : 2588 : || compare_tree_int (c0->index, i)))
3928 : 5838 : || (c1->index
3929 : 2588 : && (TREE_CODE (c1->index) != INTEGER_CST
3930 : 2588 : || compare_tree_int (c1->index, i))))
3931 : 170 : return false;
3932 : : }
3933 : : return true;
3934 : : }
3935 : 312803552 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3936 : 2981 : && (flags & OEP_LEXICOGRAPHIC))
3937 : : {
3938 : : /* Compare the STATEMENT_LISTs. */
3939 : 16 : tree_stmt_iterator tsi1, tsi2;
3940 : 16 : tree body1 = CONST_CAST_TREE (arg0);
3941 : 16 : tree body2 = CONST_CAST_TREE (arg1);
3942 : 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3943 : 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3944 : : {
3945 : : /* The lists don't have the same number of statements. */
3946 : 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3947 : : return false;
3948 : 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3949 : : return true;
3950 : 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3951 : : flags & (OEP_LEXICOGRAPHIC
3952 : : | OEP_NO_HASH_CHECK)))
3953 : : return false;
3954 : : }
3955 : : }
3956 : : return false;
3957 : :
3958 : 2171977 : case tcc_statement:
3959 : 2171977 : switch (TREE_CODE (arg0))
3960 : : {
3961 : 52 : case RETURN_EXPR:
3962 : 52 : if (flags & OEP_LEXICOGRAPHIC)
3963 : 52 : return OP_SAME_WITH_NULL (0);
3964 : : return false;
3965 : 4 : case DEBUG_BEGIN_STMT:
3966 : 4 : if (flags & OEP_LEXICOGRAPHIC)
3967 : : return true;
3968 : : return false;
3969 : : default:
3970 : : return false;
3971 : : }
3972 : :
3973 : : default:
3974 : : return false;
3975 : : }
3976 : :
3977 : : #undef OP_SAME
3978 : : #undef OP_SAME_WITH_NULL
3979 : : }
3980 : :
3981 : : /* Generate a hash value for an expression. This can be used iteratively
3982 : : by passing a previous result as the HSTATE argument. */
3983 : :
3984 : : void
3985 : 2667143545 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3986 : : unsigned int flags)
3987 : : {
3988 : 2667143545 : int i;
3989 : 2667143545 : enum tree_code code;
3990 : 2667143545 : enum tree_code_class tclass;
3991 : :
3992 : 2667143545 : if (t == NULL_TREE || t == error_mark_node)
3993 : : {
3994 : 73486895 : hstate.merge_hash (0);
3995 : 73486895 : return;
3996 : : }
3997 : :
3998 : 2593656650 : STRIP_ANY_LOCATION_WRAPPER (t);
3999 : :
4000 : 2593656650 : if (!(flags & OEP_ADDRESS_OF))
4001 : 2346168657 : STRIP_NOPS (t);
4002 : :
4003 : 2593656650 : code = TREE_CODE (t);
4004 : :
4005 : 2593656650 : switch (code)
4006 : : {
4007 : : /* Alas, constants aren't shared, so we can't rely on pointer
4008 : : identity. */
4009 : 731 : case VOID_CST:
4010 : 731 : hstate.merge_hash (0);
4011 : 731 : return;
4012 : 779559706 : case INTEGER_CST:
4013 : 779559706 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4014 : 1583910988 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
4015 : 804351282 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
4016 : : return;
4017 : 15364596 : case REAL_CST:
4018 : 15364596 : {
4019 : 15364596 : unsigned int val2;
4020 : 15364596 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
4021 : : val2 = rvc_zero;
4022 : : else
4023 : 15145473 : val2 = real_hash (TREE_REAL_CST_PTR (t));
4024 : 15364596 : hstate.merge_hash (val2);
4025 : 15364596 : return;
4026 : : }
4027 : 0 : case FIXED_CST:
4028 : 0 : {
4029 : 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
4030 : 0 : hstate.merge_hash (val2);
4031 : 0 : return;
4032 : : }
4033 : 10336224 : case STRING_CST:
4034 : 10336224 : hstate.add ((const void *) TREE_STRING_POINTER (t),
4035 : 10336224 : TREE_STRING_LENGTH (t));
4036 : 10336224 : return;
4037 : 188 : case RAW_DATA_CST:
4038 : 188 : hstate.add ((const void *) RAW_DATA_POINTER (t),
4039 : 188 : RAW_DATA_LENGTH (t));
4040 : 188 : return;
4041 : 208565 : case COMPLEX_CST:
4042 : 208565 : hash_operand (TREE_REALPART (t), hstate, flags);
4043 : 208565 : hash_operand (TREE_IMAGPART (t), hstate, flags);
4044 : 208565 : return;
4045 : 2868256 : case VECTOR_CST:
4046 : 2868256 : {
4047 : 2868256 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
4048 : 2868256 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
4049 : 2868256 : unsigned int count = vector_cst_encoded_nelts (t);
4050 : 8898493 : for (unsigned int i = 0; i < count; ++i)
4051 : 6030237 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
4052 : : return;
4053 : : }
4054 : 864607497 : case SSA_NAME:
4055 : : /* We can just compare by pointer. */
4056 : 864607497 : hstate.add_hwi (SSA_NAME_VERSION (t));
4057 : 864607497 : return;
4058 : : case PLACEHOLDER_EXPR:
4059 : : /* The node itself doesn't matter. */
4060 : : return;
4061 : : case BLOCK:
4062 : : case OMP_CLAUSE:
4063 : : case OMP_NEXT_VARIANT:
4064 : : case OMP_TARGET_DEVICE_MATCHES:
4065 : : /* Ignore. */
4066 : : return;
4067 : : case TREE_LIST:
4068 : : /* A list of expressions, for a CALL_EXPR or as the elements of a
4069 : : VECTOR_CST. */
4070 : 271436 : for (; t; t = TREE_CHAIN (t))
4071 : 135718 : hash_operand (TREE_VALUE (t), hstate, flags);
4072 : : return;
4073 : 4873715 : case CONSTRUCTOR:
4074 : 4873715 : {
4075 : 4873715 : unsigned HOST_WIDE_INT idx;
4076 : 4873715 : tree field, value;
4077 : 4873715 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4078 : 4873715 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
4079 : 19811543 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
4080 : : {
4081 : : /* In GIMPLE the indexes can be either NULL or matching i. */
4082 : 14937828 : if (field == NULL_TREE)
4083 : 1038594 : field = bitsize_int (idx);
4084 : 14937828 : if (TREE_CODE (field) == FIELD_DECL)
4085 : : {
4086 : 9907145 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4087 : 9907145 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4088 : : }
4089 : : else
4090 : 5030683 : hash_operand (field, hstate, flags);
4091 : 14937828 : hash_operand (value, hstate, flags);
4092 : : }
4093 : : return;
4094 : : }
4095 : 182 : case STATEMENT_LIST:
4096 : 182 : {
4097 : 182 : tree_stmt_iterator i;
4098 : 182 : for (i = tsi_start (CONST_CAST_TREE (t));
4099 : 550 : !tsi_end_p (i); tsi_next (&i))
4100 : 368 : hash_operand (tsi_stmt (i), hstate, flags);
4101 : 182 : return;
4102 : : }
4103 : : case TREE_VEC:
4104 : 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4105 : 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4106 : : return;
4107 : 4 : case IDENTIFIER_NODE:
4108 : 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4109 : 4 : return;
4110 : 20003905 : case FUNCTION_DECL:
4111 : : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4112 : : Otherwise nodes that compare equal according to operand_equal_p might
4113 : : get different hash codes. However, don't do this for machine specific
4114 : : or front end builtins, since the function code is overloaded in those
4115 : : cases. */
4116 : 20003905 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4117 : 20003905 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4118 : : {
4119 : 7034546 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4120 : 7034546 : code = TREE_CODE (t);
4121 : : }
4122 : : /* FALL THROUGH */
4123 : 915701144 : default:
4124 : 915701144 : if (POLY_INT_CST_P (t))
4125 : : {
4126 : : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4127 : : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4128 : : return;
4129 : : }
4130 : 915701144 : tclass = TREE_CODE_CLASS (code);
4131 : :
4132 : 915701144 : if (tclass == tcc_declaration)
4133 : : {
4134 : : /* DECL's have a unique ID */
4135 : 591536997 : hstate.add_hwi (DECL_UID (t));
4136 : : }
4137 : 324164147 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4138 : : {
4139 : : /* For comparisons that can be swapped, use the lower
4140 : : tree code. */
4141 : 149695 : enum tree_code ccode = swap_tree_comparison (code);
4142 : 149695 : if (code < ccode)
4143 : 66556 : ccode = code;
4144 : 149695 : hstate.add_object (ccode);
4145 : 149695 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4146 : 149695 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4147 : : }
4148 : 324014452 : else if (CONVERT_EXPR_CODE_P (code))
4149 : : {
4150 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4151 : : operand_equal_p. */
4152 : 5169011 : enum tree_code ccode = NOP_EXPR;
4153 : 5169011 : hstate.add_object (ccode);
4154 : :
4155 : : /* Don't hash the type, that can lead to having nodes which
4156 : : compare equal according to operand_equal_p, but which
4157 : : have different hash codes. Make sure to include signedness
4158 : : in the hash computation. */
4159 : 5169011 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4160 : 5169011 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4161 : : }
4162 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4163 : 318845441 : else if (code == MEM_REF
4164 : 78649111 : && (flags & OEP_ADDRESS_OF) != 0
4165 : 69514051 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4166 : 13763041 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4167 : 332359690 : && integer_zerop (TREE_OPERAND (t, 1)))
4168 : 6315210 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4169 : : hstate, flags);
4170 : : /* Don't ICE on FE specific trees, or their arguments etc.
4171 : : during operand_equal_p hash verification. */
4172 : 312530231 : else if (!IS_EXPR_CODE_CLASS (tclass))
4173 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4174 : : else
4175 : : {
4176 : 312529979 : unsigned int sflags = flags;
4177 : :
4178 : 312529979 : hstate.add_object (code);
4179 : :
4180 : 312529979 : switch (code)
4181 : : {
4182 : 124978553 : case ADDR_EXPR:
4183 : 124978553 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4184 : 124978553 : flags |= OEP_ADDRESS_OF;
4185 : 124978553 : sflags = flags;
4186 : 124978553 : break;
4187 : :
4188 : 76664271 : case INDIRECT_REF:
4189 : 76664271 : case MEM_REF:
4190 : 76664271 : case TARGET_MEM_REF:
4191 : 76664271 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4192 : 76664271 : sflags = flags;
4193 : 76664271 : break;
4194 : :
4195 : 73371855 : case COMPONENT_REF:
4196 : 73371855 : if (sflags & OEP_ADDRESS_OF)
4197 : : {
4198 : 36756181 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4199 : 36756181 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4200 : : hstate, flags & ~OEP_ADDRESS_OF);
4201 : 36756181 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4202 : : hstate, flags & ~OEP_ADDRESS_OF);
4203 : 36756181 : return;
4204 : : }
4205 : : break;
4206 : 14778015 : case ARRAY_REF:
4207 : 14778015 : case ARRAY_RANGE_REF:
4208 : 14778015 : case BIT_FIELD_REF:
4209 : 14778015 : sflags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4210 : 14778015 : break;
4211 : :
4212 : 8411 : case COND_EXPR:
4213 : 8411 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4214 : 8411 : break;
4215 : :
4216 : 0 : case WIDEN_MULT_PLUS_EXPR:
4217 : 0 : case WIDEN_MULT_MINUS_EXPR:
4218 : 0 : {
4219 : : /* The multiplication operands are commutative. */
4220 : 0 : inchash::hash one, two;
4221 : 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4222 : 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4223 : 0 : hstate.add_commutative (one, two);
4224 : 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4225 : 0 : return;
4226 : : }
4227 : :
4228 : 53382 : case CALL_EXPR:
4229 : 53382 : if (CALL_EXPR_FN (t) == NULL_TREE)
4230 : 2 : hstate.add_int (CALL_EXPR_IFN (t));
4231 : : break;
4232 : :
4233 : 72 : case TARGET_EXPR:
4234 : : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4235 : : Usually different TARGET_EXPRs just should use
4236 : : different temporaries in their slots. */
4237 : 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4238 : 72 : return;
4239 : :
4240 : 214324 : case OBJ_TYPE_REF:
4241 : : /* Virtual table reference. */
4242 : 214324 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4243 : 214324 : flags &= ~(OEP_ADDRESS_OF | OEP_ASSUME_WRAPV);
4244 : 214324 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4245 : 214324 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4246 : 214324 : if (!virtual_method_call_p (t))
4247 : : return;
4248 : 214309 : if (tree c = obj_type_ref_class (t))
4249 : : {
4250 : 214309 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4251 : : /* We compute mangled names only when free_lang_data is run.
4252 : : In that case we can hash precisely. */
4253 : 214309 : if (TREE_CODE (c) == TYPE_DECL
4254 : 214309 : && DECL_ASSEMBLER_NAME_SET_P (c))
4255 : 662 : hstate.add_object
4256 : 662 : (IDENTIFIER_HASH_VALUE
4257 : : (DECL_ASSEMBLER_NAME (c)));
4258 : : }
4259 : 214309 : return;
4260 : : default:
4261 : : break;
4262 : : }
4263 : :
4264 : : /* Don't hash the type, that can lead to having nodes which
4265 : : compare equal according to operand_equal_p, but which
4266 : : have different hash codes. */
4267 : 275559402 : if (code == NON_LVALUE_EXPR)
4268 : : {
4269 : : /* Make sure to include signness in the hash computation. */
4270 : 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4271 : 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4272 : : }
4273 : :
4274 : 275559402 : else if (commutative_tree_code (code))
4275 : : {
4276 : : /* It's a commutative expression. We want to hash it the same
4277 : : however it appears. We do this by first hashing both operands
4278 : : and then rehashing based on the order of their independent
4279 : : hashes. */
4280 : 16622620 : inchash::hash one, two;
4281 : 16622620 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4282 : 16622620 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4283 : 16622620 : hstate.add_commutative (one, two);
4284 : : }
4285 : : else
4286 : 723449471 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4287 : 670088890 : hash_operand (TREE_OPERAND (t, i), hstate,
4288 : : i == 0 ? flags : sflags);
4289 : : }
4290 : : return;
4291 : : }
4292 : : }
4293 : :
4294 : : bool
4295 : 6899071285 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4296 : : unsigned int flags, bool *ret)
4297 : : {
4298 : : /* When checking and unless comparing DECL names, verify that if
4299 : : the outermost operand_equal_p call returns non-zero then ARG0
4300 : : and ARG1 have the same hash value. */
4301 : 6899071285 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4302 : : {
4303 : 2898371096 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4304 : : {
4305 : 433987213 : if (arg0 != arg1 && !(flags & (OEP_DECL_NAME | OEP_ASSUME_WRAPV)))
4306 : : {
4307 : 81164441 : inchash::hash hstate0 (0), hstate1 (0);
4308 : 81164441 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4309 : 81164441 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4310 : 81164441 : hashval_t h0 = hstate0.end ();
4311 : 81164441 : hashval_t h1 = hstate1.end ();
4312 : 81164441 : gcc_assert (h0 == h1);
4313 : : }
4314 : 433987213 : *ret = true;
4315 : : }
4316 : : else
4317 : 2464383883 : *ret = false;
4318 : :
4319 : 2898371096 : return true;
4320 : : }
4321 : :
4322 : : return false;
4323 : : }
4324 : :
4325 : :
4326 : : static operand_compare default_compare_instance;
4327 : :
4328 : : /* Conveinece wrapper around operand_compare class because usually we do
4329 : : not need to play with the valueizer. */
4330 : :
4331 : : bool
4332 : 2896038060 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4333 : : {
4334 : 2896038060 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4335 : : }
4336 : :
4337 : : namespace inchash
4338 : : {
4339 : :
4340 : : /* Generate a hash value for an expression. This can be used iteratively
4341 : : by passing a previous result as the HSTATE argument.
4342 : :
4343 : : This function is intended to produce the same hash for expressions which
4344 : : would compare equal using operand_equal_p. */
4345 : : void
4346 : 1836093482 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4347 : : {
4348 : 1836093482 : default_compare_instance.hash_operand (t, hstate, flags);
4349 : 1836093482 : }
4350 : :
4351 : : }
4352 : :
4353 : : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4354 : : with a different signedness or a narrower precision. */
4355 : :
4356 : : static bool
4357 : 17430332 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4358 : : {
4359 : 17430332 : if (operand_equal_p (arg0, arg1, 0))
4360 : : return true;
4361 : :
4362 : 33282994 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4363 : 28320243 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4364 : : return false;
4365 : :
4366 : : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4367 : : and see if the inner values are the same. This removes any
4368 : : signedness comparison, which doesn't matter here. */
4369 : 5606549 : tree op0 = arg0;
4370 : 5606549 : tree op1 = arg1;
4371 : 5606549 : STRIP_NOPS (op0);
4372 : 5606549 : STRIP_NOPS (op1);
4373 : 5606549 : if (operand_equal_p (op0, op1, 0))
4374 : : return true;
4375 : :
4376 : : /* Discard a single widening conversion from ARG1 and see if the inner
4377 : : value is the same as ARG0. */
4378 : 4663919 : if (CONVERT_EXPR_P (arg1)
4379 : 758889 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4380 : 758843 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4381 : 758843 : < TYPE_PRECISION (TREE_TYPE (arg1))
4382 : 5718708 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4383 : : return true;
4384 : :
4385 : : return false;
4386 : : }
4387 : :
4388 : : /* See if ARG is an expression that is either a comparison or is performing
4389 : : arithmetic on comparisons. The comparisons must only be comparing
4390 : : two different values, which will be stored in *CVAL1 and *CVAL2; if
4391 : : they are nonzero it means that some operands have already been found.
4392 : : No variables may be used anywhere else in the expression except in the
4393 : : comparisons.
4394 : :
4395 : : If this is true, return 1. Otherwise, return zero. */
4396 : :
4397 : : static bool
4398 : 54404277 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4399 : : {
4400 : 58010189 : enum tree_code code = TREE_CODE (arg);
4401 : 58010189 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4402 : :
4403 : : /* We can handle some of the tcc_expression cases here. */
4404 : 58010189 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4405 : : tclass = tcc_unary;
4406 : 57431635 : else if (tclass == tcc_expression
4407 : 575748 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4408 : 575748 : || code == COMPOUND_EXPR))
4409 : : tclass = tcc_binary;
4410 : :
4411 : 57420880 : switch (tclass)
4412 : : {
4413 : 3605912 : case tcc_unary:
4414 : 3605912 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4415 : :
4416 : 5031829 : case tcc_binary:
4417 : 5031829 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4418 : 5031829 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4419 : :
4420 : : case tcc_constant:
4421 : : return true;
4422 : :
4423 : 564993 : case tcc_expression:
4424 : 564993 : if (code == COND_EXPR)
4425 : 765 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4426 : 765 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4427 : 829 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4428 : : return false;
4429 : :
4430 : 583126 : case tcc_comparison:
4431 : : /* First see if we can handle the first operand, then the second. For
4432 : : the second operand, we know *CVAL1 can't be zero. It must be that
4433 : : one side of the comparison is each of the values; test for the
4434 : : case where this isn't true by failing if the two operands
4435 : : are the same. */
4436 : :
4437 : 583126 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4438 : 583126 : TREE_OPERAND (arg, 1), 0))
4439 : : return false;
4440 : :
4441 : 583126 : if (*cval1 == 0)
4442 : 581074 : *cval1 = TREE_OPERAND (arg, 0);
4443 : 2052 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4444 : : ;
4445 : 1933 : else if (*cval2 == 0)
4446 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4447 : 1933 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4448 : : ;
4449 : : else
4450 : : return false;
4451 : :
4452 : 581193 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4453 : : ;
4454 : 581193 : else if (*cval2 == 0)
4455 : 581074 : *cval2 = TREE_OPERAND (arg, 1);
4456 : 119 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4457 : : ;
4458 : : else
4459 : : return false;
4460 : :
4461 : : return true;
4462 : :
4463 : : default:
4464 : : return false;
4465 : : }
4466 : : }
4467 : :
4468 : : /* ARG is a tree that is known to contain just arithmetic operations and
4469 : : comparisons. Evaluate the operations in the tree substituting NEW0 for
4470 : : any occurrence of OLD0 as an operand of a comparison and likewise for
4471 : : NEW1 and OLD1. */
4472 : :
4473 : : static tree
4474 : 702 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4475 : : tree old1, tree new1)
4476 : : {
4477 : 702 : tree type = TREE_TYPE (arg);
4478 : 702 : enum tree_code code = TREE_CODE (arg);
4479 : 702 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4480 : :
4481 : : /* We can handle some of the tcc_expression cases here. */
4482 : 702 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4483 : : tclass = tcc_unary;
4484 : 702 : else if (tclass == tcc_expression
4485 : 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4486 : : tclass = tcc_binary;
4487 : :
4488 : 693 : switch (tclass)
4489 : : {
4490 : 165 : case tcc_unary:
4491 : 165 : return fold_build1_loc (loc, code, type,
4492 : 165 : eval_subst (loc, TREE_OPERAND (arg, 0),
4493 : 165 : old0, new0, old1, new1));
4494 : :
4495 : 168 : case tcc_binary:
4496 : 336 : return fold_build2_loc (loc, code, type,
4497 : 168 : eval_subst (loc, TREE_OPERAND (arg, 0),
4498 : : old0, new0, old1, new1),
4499 : 168 : eval_subst (loc, TREE_OPERAND (arg, 1),
4500 : 168 : old0, new0, old1, new1));
4501 : :
4502 : 9 : case tcc_expression:
4503 : 9 : switch (code)
4504 : : {
4505 : 0 : case SAVE_EXPR:
4506 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4507 : 0 : old1, new1);
4508 : :
4509 : 0 : case COMPOUND_EXPR:
4510 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4511 : 0 : old1, new1);
4512 : :
4513 : 9 : case COND_EXPR:
4514 : 27 : return fold_build3_loc (loc, code, type,
4515 : 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4516 : : old0, new0, old1, new1),
4517 : 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4518 : : old0, new0, old1, new1),
4519 : 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4520 : 9 : old0, new0, old1, new1));
4521 : : default:
4522 : : break;
4523 : : }
4524 : : /* Fall through - ??? */
4525 : :
4526 : 180 : case tcc_comparison:
4527 : 180 : {
4528 : 180 : tree arg0 = TREE_OPERAND (arg, 0);
4529 : 180 : tree arg1 = TREE_OPERAND (arg, 1);
4530 : :
4531 : : /* We need to check both for exact equality and tree equality. The
4532 : : former will be true if the operand has a side-effect. In that
4533 : : case, we know the operand occurred exactly once. */
4534 : :
4535 : 180 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4536 : : arg0 = new0;
4537 : 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4538 : : arg0 = new1;
4539 : :
4540 : 180 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4541 : : arg1 = new0;
4542 : 180 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4543 : : arg1 = new1;
4544 : :
4545 : 180 : return fold_build2_loc (loc, code, type, arg0, arg1);
4546 : : }
4547 : :
4548 : : default:
4549 : : return arg;
4550 : : }
4551 : : }
4552 : :
4553 : : /* Return a tree for the case when the result of an expression is RESULT
4554 : : converted to TYPE and OMITTED was previously an operand of the expression
4555 : : but is now not needed (e.g., we folded OMITTED * 0).
4556 : :
4557 : : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4558 : : the conversion of RESULT to TYPE. */
4559 : :
4560 : : tree
4561 : 287864 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4562 : : {
4563 : 287864 : tree t = fold_convert_loc (loc, type, result);
4564 : :
4565 : : /* If the resulting operand is an empty statement, just return the omitted
4566 : : statement casted to void. */
4567 : 287864 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4568 : 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4569 : 0 : fold_ignored_result (omitted));
4570 : :
4571 : 287864 : if (TREE_SIDE_EFFECTS (omitted))
4572 : 13121 : return build2_loc (loc, COMPOUND_EXPR, type,
4573 : 13121 : fold_ignored_result (omitted), t);
4574 : :
4575 : 274743 : return non_lvalue_loc (loc, t);
4576 : : }
4577 : :
4578 : : /* Return a tree for the case when the result of an expression is RESULT
4579 : : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4580 : : of the expression but are now not needed.
4581 : :
4582 : : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4583 : : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4584 : : evaluated before OMITTED2. Otherwise, if neither has side effects,
4585 : : just do the conversion of RESULT to TYPE. */
4586 : :
4587 : : tree
4588 : 7105 : omit_two_operands_loc (location_t loc, tree type, tree result,
4589 : : tree omitted1, tree omitted2)
4590 : : {
4591 : 7105 : tree t = fold_convert_loc (loc, type, result);
4592 : :
4593 : 7105 : if (TREE_SIDE_EFFECTS (omitted2))
4594 : 68 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4595 : 7105 : if (TREE_SIDE_EFFECTS (omitted1))
4596 : 175 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4597 : :
4598 : 7105 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4599 : : }
4600 : :
4601 : :
4602 : : /* Return a simplified tree node for the truth-negation of ARG. This
4603 : : never alters ARG itself. We assume that ARG is an operation that
4604 : : returns a truth value (0 or 1).
4605 : :
4606 : : FIXME: one would think we would fold the result, but it causes
4607 : : problems with the dominator optimizer. */
4608 : :
4609 : : static tree
4610 : 48113181 : fold_truth_not_expr (location_t loc, tree arg)
4611 : : {
4612 : 48113181 : tree type = TREE_TYPE (arg);
4613 : 48113181 : enum tree_code code = TREE_CODE (arg);
4614 : 48113181 : location_t loc1, loc2;
4615 : :
4616 : : /* If this is a comparison, we can simply invert it, except for
4617 : : floating-point non-equality comparisons, in which case we just
4618 : : enclose a TRUTH_NOT_EXPR around what we have. */
4619 : :
4620 : 48113181 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4621 : : {
4622 : 37182709 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4623 : 31377233 : if (FLOAT_TYPE_P (op_type)
4624 : 5815563 : && flag_trapping_math
4625 : 5785112 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4626 : 42928369 : && code != NE_EXPR && code != EQ_EXPR)
4627 : : return NULL_TREE;
4628 : :
4629 : 32125900 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4630 : 32125900 : if (code == ERROR_MARK)
4631 : : return NULL_TREE;
4632 : :
4633 : 32125900 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4634 : 32125900 : TREE_OPERAND (arg, 1));
4635 : 32125900 : copy_warning (ret, arg);
4636 : 32125900 : return ret;
4637 : : }
4638 : :
4639 : 10930472 : switch (code)
4640 : : {
4641 : 0 : case INTEGER_CST:
4642 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4643 : :
4644 : 50793 : case TRUTH_AND_EXPR:
4645 : 50793 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4646 : 50793 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4647 : 101586 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4648 : 50793 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4649 : 101586 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4650 : :
4651 : 2448 : case TRUTH_OR_EXPR:
4652 : 2448 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4653 : 2448 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4654 : 4896 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4655 : 2448 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4656 : 4896 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4657 : :
4658 : 32252 : case TRUTH_XOR_EXPR:
4659 : : /* Here we can invert either operand. We invert the first operand
4660 : : unless the second operand is a TRUTH_NOT_EXPR in which case our
4661 : : result is the XOR of the first operand with the inside of the
4662 : : negation of the second operand. */
4663 : :
4664 : 32252 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4665 : 7 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4666 : 14 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4667 : : else
4668 : 32245 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4669 : 32245 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4670 : 64490 : TREE_OPERAND (arg, 1));
4671 : :
4672 : 249289 : case TRUTH_ANDIF_EXPR:
4673 : 249289 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4674 : 249289 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4675 : 498578 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4676 : 249289 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4677 : 498578 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4678 : :
4679 : 18825 : case TRUTH_ORIF_EXPR:
4680 : 18825 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4681 : 18825 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4682 : 37650 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4683 : 18825 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4684 : 37650 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4685 : :
4686 : 799505 : case TRUTH_NOT_EXPR:
4687 : 799505 : return TREE_OPERAND (arg, 0);
4688 : :
4689 : 7936 : case COND_EXPR:
4690 : 7936 : {
4691 : 7936 : tree arg1 = TREE_OPERAND (arg, 1);
4692 : 7936 : tree arg2 = TREE_OPERAND (arg, 2);
4693 : :
4694 : 7936 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4695 : 7936 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4696 : :
4697 : : /* A COND_EXPR may have a throw as one operand, which
4698 : : then has void type. Just leave void operands
4699 : : as they are. */
4700 : 7936 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4701 : 7936 : VOID_TYPE_P (TREE_TYPE (arg1))
4702 : 7936 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4703 : 7936 : VOID_TYPE_P (TREE_TYPE (arg2))
4704 : 15869 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4705 : : }
4706 : :
4707 : 140 : case COMPOUND_EXPR:
4708 : 140 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4709 : 280 : return build2_loc (loc, COMPOUND_EXPR, type,
4710 : 140 : TREE_OPERAND (arg, 0),
4711 : 280 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4712 : :
4713 : 0 : case NON_LVALUE_EXPR:
4714 : 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4715 : 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4716 : :
4717 : 70470 : CASE_CONVERT:
4718 : 70470 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4719 : 70406 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4720 : :
4721 : : /* fall through */
4722 : :
4723 : 64 : case FLOAT_EXPR:
4724 : 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4725 : 64 : return build1_loc (loc, TREE_CODE (arg), type,
4726 : 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4727 : :
4728 : 461 : case BIT_AND_EXPR:
4729 : 461 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4730 : : return NULL_TREE;
4731 : 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4732 : :
4733 : 2 : case SAVE_EXPR:
4734 : 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4735 : :
4736 : 75 : case CLEANUP_POINT_EXPR:
4737 : 75 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4738 : 75 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4739 : 150 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4740 : :
4741 : : default:
4742 : : return NULL_TREE;
4743 : : }
4744 : : }
4745 : :
4746 : : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4747 : : assume that ARG is an operation that returns a truth value (0 or 1
4748 : : for scalars, 0 or -1 for vectors). Return the folded expression if
4749 : : folding is successful. Otherwise, return NULL_TREE. */
4750 : :
4751 : : static tree
4752 : 1712961 : fold_invert_truthvalue (location_t loc, tree arg)
4753 : : {
4754 : 1712961 : tree type = TREE_TYPE (arg);
4755 : 3425898 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4756 : : ? BIT_NOT_EXPR
4757 : : : TRUTH_NOT_EXPR,
4758 : 1712961 : type, arg);
4759 : : }
4760 : :
4761 : : /* Return a simplified tree node for the truth-negation of ARG. This
4762 : : never alters ARG itself. We assume that ARG is an operation that
4763 : : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4764 : :
4765 : : tree
4766 : 40967935 : invert_truthvalue_loc (location_t loc, tree arg)
4767 : : {
4768 : 40967935 : if (TREE_CODE (arg) == ERROR_MARK)
4769 : : return arg;
4770 : :
4771 : 40967935 : tree type = TREE_TYPE (arg);
4772 : 81935870 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4773 : : ? BIT_NOT_EXPR
4774 : : : TRUTH_NOT_EXPR,
4775 : 40967935 : type, arg);
4776 : : }
4777 : :
4778 : : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4779 : : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4780 : : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4781 : : is the original memory reference used to preserve the alias set of
4782 : : the access. */
4783 : :
4784 : : tree
4785 : 757585 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4786 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4787 : : int unsignedp, int reversep)
4788 : : {
4789 : 757585 : tree result, bftype;
4790 : :
4791 : : /* Attempt not to lose the access path if possible. */
4792 : 757585 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4793 : : {
4794 : 753981 : tree ninner = TREE_OPERAND (orig_inner, 0);
4795 : 753981 : machine_mode nmode;
4796 : 753981 : poly_int64 nbitsize, nbitpos;
4797 : 753981 : tree noffset;
4798 : 753981 : int nunsignedp, nreversep, nvolatilep = 0;
4799 : 753981 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4800 : : &noffset, &nmode, &nunsignedp,
4801 : : &nreversep, &nvolatilep);
4802 : 753981 : if (base == inner
4803 : 753849 : && noffset == NULL_TREE
4804 : 753849 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4805 : 753825 : && !reversep
4806 : 753753 : && !nreversep
4807 : 1507734 : && !nvolatilep)
4808 : : {
4809 : 753753 : inner = ninner;
4810 : 753981 : bitpos -= nbitpos;
4811 : : }
4812 : : }
4813 : :
4814 : 757585 : alias_set_type iset = get_alias_set (orig_inner);
4815 : 757585 : if (iset == 0 && get_alias_set (inner) != iset)
4816 : 209 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4817 : : build_fold_addr_expr (inner),
4818 : : build_int_cst (ptr_type_node, 0));
4819 : :
4820 : 757585 : if (known_eq (bitpos, 0) && !reversep)
4821 : : {
4822 : 17519 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4823 : 35038 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4824 : 17321 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4825 : 202 : && tree_fits_shwi_p (size)
4826 : 17721 : && tree_to_shwi (size) == bitsize)
4827 : 179 : return fold_convert_loc (loc, type, inner);
4828 : : }
4829 : :
4830 : 757406 : bftype = type;
4831 : 757406 : if (TYPE_PRECISION (bftype) != bitsize
4832 : 757406 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4833 : 365 : bftype = build_nonstandard_integer_type (bitsize, 0);
4834 : :
4835 : 757406 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4836 : 757406 : bitsize_int (bitsize), bitsize_int (bitpos));
4837 : 757406 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4838 : :
4839 : 757406 : if (bftype != type)
4840 : 365 : result = fold_convert_loc (loc, type, result);
4841 : :
4842 : : return result;
4843 : : }
4844 : :
4845 : : /* Optimize a bit-field compare.
4846 : :
4847 : : There are two cases: First is a compare against a constant and the
4848 : : second is a comparison of two items where the fields are at the same
4849 : : bit position relative to the start of a chunk (byte, halfword, word)
4850 : : large enough to contain it. In these cases we can avoid the shift
4851 : : implicit in bitfield extractions.
4852 : :
4853 : : For constants, we emit a compare of the shifted constant with the
4854 : : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4855 : : compared. For two fields at the same position, we do the ANDs with the
4856 : : similar mask and compare the result of the ANDs.
4857 : :
4858 : : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4859 : : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4860 : : are the left and right operands of the comparison, respectively.
4861 : :
4862 : : If the optimization described above can be done, we return the resulting
4863 : : tree. Otherwise we return zero. */
4864 : :
4865 : : static tree
4866 : 4022465 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4867 : : tree compare_type, tree lhs, tree rhs)
4868 : : {
4869 : 4022465 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4870 : 4022465 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4871 : 4022465 : tree type = TREE_TYPE (lhs);
4872 : 4022465 : tree unsigned_type;
4873 : 4022465 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4874 : 4022465 : machine_mode lmode, rmode;
4875 : 4022465 : scalar_int_mode nmode;
4876 : 4022465 : int lunsignedp, runsignedp;
4877 : 4022465 : int lreversep, rreversep;
4878 : 4022465 : int lvolatilep = 0, rvolatilep = 0;
4879 : 4022465 : tree linner, rinner = NULL_TREE;
4880 : 4022465 : tree mask;
4881 : 4022465 : tree offset;
4882 : :
4883 : : /* Get all the information about the extractions being done. If the bit size
4884 : : is the same as the size of the underlying object, we aren't doing an
4885 : : extraction at all and so can do nothing. We also don't want to
4886 : : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4887 : : then will no longer be able to replace it. */
4888 : 4022465 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4889 : : &lunsignedp, &lreversep, &lvolatilep);
4890 : 4022465 : if (linner == lhs
4891 : 4022465 : || !known_size_p (plbitsize)
4892 : 4022465 : || !plbitsize.is_constant (&lbitsize)
4893 : 4022465 : || !plbitpos.is_constant (&lbitpos)
4894 : 8044930 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4895 : 756285 : || offset != 0
4896 : 756260 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4897 : 4778725 : || lvolatilep)
4898 : 3266265 : return 0;
4899 : :
4900 : 756200 : if (const_p)
4901 : 742381 : rreversep = lreversep;
4902 : : else
4903 : : {
4904 : : /* If this is not a constant, we can only do something if bit positions,
4905 : : sizes, signedness and storage order are the same. */
4906 : 13819 : rinner
4907 : 13819 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4908 : : &runsignedp, &rreversep, &rvolatilep);
4909 : :
4910 : 13819 : if (rinner == rhs
4911 : 13655 : || maybe_ne (lbitpos, rbitpos)
4912 : 13621 : || maybe_ne (lbitsize, rbitsize)
4913 : 13621 : || lunsignedp != runsignedp
4914 : 13621 : || lreversep != rreversep
4915 : 13621 : || offset != 0
4916 : 13621 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4917 : 27440 : || rvolatilep)
4918 : : return 0;
4919 : : }
4920 : :
4921 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4922 : 756002 : poly_uint64 bitstart = 0;
4923 : 756002 : poly_uint64 bitend = 0;
4924 : 756002 : if (TREE_CODE (lhs) == COMPONENT_REF)
4925 : : {
4926 : 756002 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4927 : 756002 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4928 : : return 0;
4929 : : }
4930 : :
4931 : : /* See if we can find a mode to refer to this field. We should be able to,
4932 : : but fail if we can't. */
4933 : 1512004 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4934 : 742381 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4935 : 13621 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4936 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4937 : 756002 : BITS_PER_WORD, false, &nmode))
4938 : : return 0;
4939 : :
4940 : : /* Set signed and unsigned types of the precision of this mode for the
4941 : : shifts below. */
4942 : 754001 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4943 : :
4944 : : /* Compute the bit position and size for the new reference and our offset
4945 : : within it. If the new reference is the same size as the original, we
4946 : : won't optimize anything, so return zero. */
4947 : 754001 : nbitsize = GET_MODE_BITSIZE (nmode);
4948 : 754001 : nbitpos = lbitpos & ~ (nbitsize - 1);
4949 : 754001 : lbitpos -= nbitpos;
4950 : 754001 : if (nbitsize == lbitsize)
4951 : : return 0;
4952 : :
4953 : 742449 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4954 : 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4955 : :
4956 : : /* Make the mask to be used against the extracted field. */
4957 : 742449 : mask = build_int_cst_type (unsigned_type, -1);
4958 : 742449 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4959 : 742449 : mask = const_binop (RSHIFT_EXPR, mask,
4960 : 742449 : size_int (nbitsize - lbitsize - lbitpos));
4961 : :
4962 : 742449 : if (! const_p)
4963 : : {
4964 : 10786 : if (nbitpos < 0)
4965 : : return 0;
4966 : :
4967 : : /* If not comparing with constant, just rework the comparison
4968 : : and return. */
4969 : 10786 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4970 : 10786 : nbitsize, nbitpos, 1, lreversep);
4971 : 10786 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4972 : 10786 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4973 : 10786 : nbitsize, nbitpos, 1, rreversep);
4974 : 10786 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4975 : 10786 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4976 : : }
4977 : :
4978 : : /* Otherwise, we are handling the constant case. See if the constant is too
4979 : : big for the field. Warn and return a tree for 0 (false) if so. We do
4980 : : this not only for its own sake, but to avoid having to test for this
4981 : : error case below. If we didn't, we might generate wrong code.
4982 : :
4983 : : For unsigned fields, the constant shifted right by the field length should
4984 : : be all zero. For signed fields, the high-order bits should agree with
4985 : : the sign bit. */
4986 : :
4987 : 731663 : if (lunsignedp)
4988 : : {
4989 : 730544 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4990 : : {
4991 : 0 : warning (0, "comparison is always %d due to width of bit-field",
4992 : : code == NE_EXPR);
4993 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4994 : : }
4995 : : }
4996 : : else
4997 : : {
4998 : 1119 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4999 : 1119 : if (tem != 0 && tem != -1)
5000 : : {
5001 : 0 : warning (0, "comparison is always %d due to width of bit-field",
5002 : : code == NE_EXPR);
5003 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
5004 : : }
5005 : 1119 : }
5006 : :
5007 : 731663 : if (nbitpos < 0)
5008 : : return 0;
5009 : :
5010 : : /* Single-bit compares should always be against zero. */
5011 : 731663 : if (lbitsize == 1 && ! integer_zerop (rhs))
5012 : : {
5013 : 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
5014 : 175 : rhs = build_int_cst (type, 0);
5015 : : }
5016 : :
5017 : : /* Make a new bitfield reference, shift the constant over the
5018 : : appropriate number of bits and mask it with the computed mask
5019 : : (in case this was a signed field). If we changed it, make a new one. */
5020 : 731663 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
5021 : 731663 : nbitsize, nbitpos, 1, lreversep);
5022 : :
5023 : 731663 : rhs = const_binop (BIT_AND_EXPR,
5024 : : const_binop (LSHIFT_EXPR,
5025 : : fold_convert_loc (loc, unsigned_type, rhs),
5026 : 731663 : size_int (lbitpos)),
5027 : : mask);
5028 : :
5029 : 731663 : lhs = build2_loc (loc, code, compare_type,
5030 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
5031 : 731663 : return lhs;
5032 : : }
5033 : :
5034 : : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5035 : : represents the sign bit of EXP's type. If EXP represents a sign
5036 : : or zero extension, also test VAL against the unextended type.
5037 : : The return value is the (sub)expression whose sign bit is VAL,
5038 : : or NULL_TREE otherwise. */
5039 : :
5040 : : tree
5041 : 2150 : sign_bit_p (tree exp, const_tree val)
5042 : : {
5043 : 2150 : int width;
5044 : 2150 : tree t;
5045 : :
5046 : : /* Tree EXP must have an integral type. */
5047 : 2150 : t = TREE_TYPE (exp);
5048 : 2150 : if (! INTEGRAL_TYPE_P (t))
5049 : : return NULL_TREE;
5050 : :
5051 : : /* Tree VAL must be an integer constant. */
5052 : 1818 : if (TREE_CODE (val) != INTEGER_CST
5053 : 1818 : || TREE_OVERFLOW (val))
5054 : : return NULL_TREE;
5055 : :
5056 : 1444 : width = TYPE_PRECISION (t);
5057 : 1444 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5058 : : return exp;
5059 : :
5060 : : /* Handle extension from a narrower type. */
5061 : 807 : if (TREE_CODE (exp) == NOP_EXPR
5062 : 807 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5063 : 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5064 : :
5065 : : return NULL_TREE;
5066 : : }
5067 : :
5068 : : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5069 : : operand is simple enough to be evaluated unconditionally. */
5070 : :
5071 : : static bool
5072 : 62600568 : simple_operand_p (const_tree exp)
5073 : : {
5074 : : /* Strip any conversions that don't change the machine mode. */
5075 : 62600568 : STRIP_NOPS (exp);
5076 : :
5077 : 62600568 : return (CONSTANT_CLASS_P (exp)
5078 : 43323482 : || TREE_CODE (exp) == SSA_NAME
5079 : 76818677 : || (DECL_P (exp)
5080 : 4506077 : && ! TREE_ADDRESSABLE (exp)
5081 : 4417464 : && ! TREE_THIS_VOLATILE (exp)
5082 : 4417464 : && ! DECL_NONLOCAL (exp)
5083 : : /* Don't regard global variables as simple. They may be
5084 : : allocated in ways unknown to the compiler (shared memory,
5085 : : #pragma weak, etc). */
5086 : 4416012 : && ! TREE_PUBLIC (exp)
5087 : 4395398 : && ! DECL_EXTERNAL (exp)
5088 : : /* DECL_VALUE_EXPR will expand to something non-simple. */
5089 : 4395398 : && ! ((VAR_P (exp)
5090 : : || TREE_CODE (exp) == PARM_DECL
5091 : : || TREE_CODE (exp) == RESULT_DECL)
5092 : 4395398 : && DECL_HAS_VALUE_EXPR_P (exp))
5093 : : /* Weakrefs are not safe to be read, since they can be NULL.
5094 : : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5095 : : have DECL_WEAK flag set. */
5096 : 4394807 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5097 : : /* Loading a static variable is unduly expensive, but global
5098 : : registers aren't expensive. */
5099 : 4394807 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5100 : : }
5101 : :
5102 : : /* Determine if an operand is simple enough to be evaluated unconditionally.
5103 : : In addition to simple_operand_p, we assume that comparisons, conversions,
5104 : : and logic-not operations are simple, if their operands are simple, too. */
5105 : :
5106 : : bool
5107 : 5815778 : simple_condition_p (tree exp)
5108 : : {
5109 : 5884772 : enum tree_code code;
5110 : :
5111 : 5884772 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5112 : 4207981 : return false;
5113 : :
5114 : 1685859 : while (CONVERT_EXPR_P (exp))
5115 : 9068 : exp = TREE_OPERAND (exp, 0);
5116 : :
5117 : 1676791 : code = TREE_CODE (exp);
5118 : :
5119 : 1676791 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5120 : 1279775 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5121 : 1279775 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5122 : :
5123 : 397016 : if (code == TRUTH_NOT_EXPR)
5124 : 68994 : return simple_condition_p (TREE_OPERAND (exp, 0));
5125 : :
5126 : 328022 : return simple_operand_p (exp);
5127 : : }
5128 : :
5129 : :
5130 : : /* The following functions are subroutines to fold_range_test and allow it to
5131 : : try to change a logical combination of comparisons into a range test.
5132 : :
5133 : : For example, both
5134 : : X == 2 || X == 3 || X == 4 || X == 5
5135 : : and
5136 : : X >= 2 && X <= 5
5137 : : are converted to
5138 : : (unsigned) (X - 2) <= 3
5139 : :
5140 : : We describe each set of comparisons as being either inside or outside
5141 : : a range, using a variable named like IN_P, and then describe the
5142 : : range with a lower and upper bound. If one of the bounds is omitted,
5143 : : it represents either the highest or lowest value of the type.
5144 : :
5145 : : In the comments below, we represent a range by two numbers in brackets
5146 : : preceded by a "+" to designate being inside that range, or a "-" to
5147 : : designate being outside that range, so the condition can be inverted by
5148 : : flipping the prefix. An omitted bound is represented by a "-". For
5149 : : example, "- [-, 10]" means being outside the range starting at the lowest
5150 : : possible value and ending at 10, in other words, being greater than 10.
5151 : : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5152 : : always false.
5153 : :
5154 : : We set up things so that the missing bounds are handled in a consistent
5155 : : manner so neither a missing bound nor "true" and "false" need to be
5156 : : handled using a special case. */
5157 : :
5158 : : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5159 : : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5160 : : and UPPER1_P are nonzero if the respective argument is an upper bound
5161 : : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5162 : : must be specified for a comparison. ARG1 will be converted to ARG0's
5163 : : type if both are specified. */
5164 : :
5165 : : static tree
5166 : 22114962 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5167 : : tree arg1, int upper1_p)
5168 : : {
5169 : 22114962 : tree tem;
5170 : 22114962 : int result;
5171 : 22114962 : int sgn0, sgn1;
5172 : :
5173 : : /* If neither arg represents infinity, do the normal operation.
5174 : : Else, if not a comparison, return infinity. Else handle the special
5175 : : comparison rules. Note that most of the cases below won't occur, but
5176 : : are handled for consistency. */
5177 : :
5178 : 22114962 : if (arg0 != 0 && arg1 != 0)
5179 : : {
5180 : 11372161 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5181 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5182 : 11372161 : STRIP_NOPS (tem);
5183 : 11372161 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5184 : : }
5185 : :
5186 : 10742801 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5187 : : return 0;
5188 : :
5189 : : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5190 : : for neither. In real maths, we cannot assume open ended ranges are
5191 : : the same. But, this is computer arithmetic, where numbers are finite.
5192 : : We can therefore make the transformation of any unbounded range with
5193 : : the value Z, Z being greater than any representable number. This permits
5194 : : us to treat unbounded ranges as equal. */
5195 : 10734272 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5196 : 10734272 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5197 : 10734272 : switch (code)
5198 : : {
5199 : 5011039 : case EQ_EXPR:
5200 : 5011039 : result = sgn0 == sgn1;
5201 : 5011039 : break;
5202 : 0 : case NE_EXPR:
5203 : 0 : result = sgn0 != sgn1;
5204 : 0 : break;
5205 : 398934 : case LT_EXPR:
5206 : 398934 : result = sgn0 < sgn1;
5207 : 398934 : break;
5208 : 2470861 : case LE_EXPR:
5209 : 2470861 : result = sgn0 <= sgn1;
5210 : 2470861 : break;
5211 : 2853438 : case GT_EXPR:
5212 : 2853438 : result = sgn0 > sgn1;
5213 : 2853438 : break;
5214 : 0 : case GE_EXPR:
5215 : 0 : result = sgn0 >= sgn1;
5216 : 0 : break;
5217 : 0 : default:
5218 : 0 : gcc_unreachable ();
5219 : : }
5220 : :
5221 : 10734272 : return constant_boolean_node (result, type);
5222 : : }
5223 : :
5224 : : /* Helper routine for make_range. Perform one step for it, return
5225 : : new expression if the loop should continue or NULL_TREE if it should
5226 : : stop. */
5227 : :
5228 : : tree
5229 : 57247408 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5230 : : tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5231 : : bool *strict_overflow_p)
5232 : : {
5233 : 57247408 : tree arg0_type = TREE_TYPE (arg0);
5234 : 57247408 : tree n_low, n_high, low = *p_low, high = *p_high;
5235 : 57247408 : int in_p = *p_in_p, n_in_p;
5236 : :
5237 : 57247408 : switch (code)
5238 : : {
5239 : 1631279 : case TRUTH_NOT_EXPR:
5240 : : /* We can only do something if the range is testing for zero. */
5241 : 1631279 : if (low == NULL_TREE || high == NULL_TREE
5242 : 1631279 : || ! integer_zerop (low) || ! integer_zerop (high))
5243 : 0 : return NULL_TREE;
5244 : 1631279 : *p_in_p = ! in_p;
5245 : 1631279 : return arg0;
5246 : :
5247 : 45273757 : case EQ_EXPR: case NE_EXPR:
5248 : 45273757 : 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 : 45273757 : if (low == NULL_TREE || high == NULL_TREE
5255 : 45273757 : || ! integer_zerop (low) || ! integer_zerop (high)
5256 : 90547427 : || TREE_CODE (arg1) != INTEGER_CST)
5257 : 16735154 : return NULL_TREE;
5258 : :
5259 : 28538603 : switch (code)
5260 : : {
5261 : : case NE_EXPR: /* - [c, c] */
5262 : : low = high = arg1;
5263 : : break;
5264 : 7727986 : case EQ_EXPR: /* + [c, c] */
5265 : 7727986 : in_p = ! in_p, low = high = arg1;
5266 : 7727986 : break;
5267 : 2173342 : case GT_EXPR: /* - [-, c] */
5268 : 2173342 : low = 0, high = arg1;
5269 : 2173342 : break;
5270 : 916780 : case GE_EXPR: /* + [c, -] */
5271 : 916780 : in_p = ! in_p, low = arg1, high = 0;
5272 : 916780 : break;
5273 : 5364887 : case LT_EXPR: /* - [c, -] */
5274 : 5364887 : low = arg1, high = 0;
5275 : 5364887 : break;
5276 : 4369515 : case LE_EXPR: /* + [-, c] */
5277 : 4369515 : in_p = ! in_p, low = 0, high = arg1;
5278 : 4369515 : break;
5279 : 0 : default:
5280 : 0 : 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 : 28538603 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5289 : : {
5290 : 1926178 : 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 : 1926169 : 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 : 1926169 : if (high == 0 && low && ! integer_zerop (low))
5302 : : {
5303 : 881172 : in_p = ! in_p;
5304 : 881172 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5305 : 881172 : build_int_cst (TREE_TYPE (low), 1), 0);
5306 : 881172 : low = build_int_cst (arg0_type, 0);
5307 : : }
5308 : : }
5309 : :
5310 : 28538594 : *p_low = low;
5311 : 28538594 : *p_high = high;
5312 : 28538594 : *p_in_p = in_p;
5313 : 28538594 : return arg0;
5314 : :
5315 : 208 : 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 : 208 : if (!TYPE_UNSIGNED (arg0_type)
5319 : 208 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5320 : : {
5321 : 95 : if (low == NULL_TREE)
5322 : 12 : low = TYPE_MIN_VALUE (arg0_type);
5323 : 95 : if (high == NULL_TREE)
5324 : 47 : high = TYPE_MAX_VALUE (arg0_type);
5325 : : }
5326 : :
5327 : : /* (-x) IN [a,b] -> x in [-b, -a] */
5328 : 208 : n_low = range_binop (MINUS_EXPR, exp_type,
5329 : : build_int_cst (exp_type, 0),
5330 : : 0, high, 1);
5331 : 208 : n_high = range_binop (MINUS_EXPR, exp_type,
5332 : : build_int_cst (exp_type, 0),
5333 : : 0, low, 0);
5334 : 208 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5335 : : return NULL_TREE;
5336 : 196 : goto normalize;
5337 : :
5338 : 12 : case BIT_NOT_EXPR:
5339 : : /* ~ X -> -X - 1 */
5340 : 12 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5341 : : build_int_cst (exp_type, 1));
5342 : :
5343 : 799376 : case PLUS_EXPR:
5344 : 799376 : case MINUS_EXPR:
5345 : 799376 : 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 : 661506 : if (!TYPE_UNSIGNED (arg0_type)
5351 : 661506 : && !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 : 874742 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5359 : : arg0_type, low, 0, arg1, 0);
5360 : 438516 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5361 : : arg0_type, high, 1, arg1, 0);
5362 : 435508 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5363 : 874012 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5364 : : return NULL_TREE;
5365 : :
5366 : 438504 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5367 : 20909 : *strict_overflow_p = true;
5368 : :
5369 : 0 : normalize:
5370 : : /* Check for an unsigned range which has wrapped around the maximum
5371 : : value thus making n_high < n_low, and normalize it. */
5372 : 438700 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5373 : : {
5374 : 159161 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5375 : 159161 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5376 : 159161 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5377 : 159161 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5378 : :
5379 : : /* If the range is of the form +/- [ x+1, x ], we won't
5380 : : be able to normalize it. But then, it represents the
5381 : : whole range or the empty set, so make it
5382 : : +/- [ -, - ]. */
5383 : 159161 : if (tree_int_cst_equal (n_low, low)
5384 : 159161 : && tree_int_cst_equal (n_high, high))
5385 : : low = high = 0;
5386 : : else
5387 : 159161 : in_p = ! in_p;
5388 : : }
5389 : : else
5390 : 279539 : low = n_low, high = n_high;
5391 : :
5392 : 438700 : *p_low = low;
5393 : 438700 : *p_high = high;
5394 : 438700 : *p_in_p = in_p;
5395 : 438700 : return arg0;
5396 : :
5397 : 2465024 : CASE_CONVERT:
5398 : 2465024 : case NON_LVALUE_EXPR:
5399 : 2465024 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5400 : : return NULL_TREE;
5401 : :
5402 : 1070347 : if (! INTEGRAL_TYPE_P (arg0_type)
5403 : 1040851 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5404 : 926690 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5405 : : return NULL_TREE;
5406 : :
5407 : 904018 : n_low = low, n_high = high;
5408 : :
5409 : 904018 : if (n_low != 0)
5410 : 797558 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5411 : :
5412 : 904018 : if (n_high != 0)
5413 : 819555 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5414 : :
5415 : : /* If we're converting arg0 from an unsigned type, to exp,
5416 : : a signed type, we will be doing the comparison as unsigned.
5417 : : The tests above have already verified that LOW and HIGH
5418 : : are both positive.
5419 : :
5420 : : So we have to ensure that we will handle large unsigned
5421 : : values the same way that the current signed bounds treat
5422 : : negative values. */
5423 : :
5424 : 904018 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5425 : : {
5426 : 215597 : tree high_positive;
5427 : 215597 : tree equiv_type;
5428 : : /* For fixed-point modes, we need to pass the saturating flag
5429 : : as the 2nd parameter. */
5430 : 215597 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5431 : 0 : equiv_type
5432 : 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5433 : 0 : TYPE_SATURATING (arg0_type));
5434 : 215597 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5435 : : equiv_type = arg0_type;
5436 : : else
5437 : 215589 : equiv_type
5438 : 215589 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5439 : :
5440 : : /* A range without an upper bound is, naturally, unbounded.
5441 : : Since convert would have cropped a very large value, use
5442 : : the max value for the destination type. */
5443 : 215597 : high_positive
5444 : 215597 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5445 : 0 : : TYPE_MAX_VALUE (arg0_type);
5446 : :
5447 : 215597 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5448 : 194956 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5449 : : fold_convert_loc (loc, arg0_type,
5450 : : high_positive),
5451 : : build_int_cst (arg0_type, 1));
5452 : :
5453 : : /* If the low bound is specified, "and" the range with the
5454 : : range for which the original unsigned value will be
5455 : : positive. */
5456 : 215597 : if (low != 0)
5457 : : {
5458 : 114380 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5459 : : 1, fold_convert_loc (loc, arg0_type,
5460 : : integer_zero_node),
5461 : : high_positive))
5462 : : return NULL_TREE;
5463 : :
5464 : 114380 : in_p = (n_in_p == in_p);
5465 : : }
5466 : : else
5467 : : {
5468 : : /* Otherwise, "or" the range with the range of the input
5469 : : that will be interpreted as negative. */
5470 : 101217 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5471 : : 1, fold_convert_loc (loc, arg0_type,
5472 : : integer_zero_node),
5473 : : high_positive))
5474 : : return NULL_TREE;
5475 : :
5476 : 101217 : in_p = (in_p != n_in_p);
5477 : : }
5478 : : }
5479 : :
5480 : : /* Otherwise, if we are converting arg0 from signed type, to exp,
5481 : : an unsigned type, we will do the comparison as signed. If
5482 : : high is non-NULL, we punt above if it doesn't fit in the signed
5483 : : type, so if we get through here, +[-, high] or +[low, high] are
5484 : : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5485 : : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5486 : : high is not, the +[low, -] range is equivalent to union of
5487 : : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5488 : : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5489 : : low being 0, which should be treated as [-, -]. */
5490 : 688421 : else if (TYPE_UNSIGNED (exp_type)
5491 : 669722 : && !TYPE_UNSIGNED (arg0_type)
5492 : 383495 : && low
5493 : 1071916 : && !high)
5494 : : {
5495 : 12 : if (integer_zerop (low))
5496 : 12 : n_low = NULL_TREE;
5497 : : else
5498 : : {
5499 : 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5500 : : n_low, build_int_cst (arg0_type, -1));
5501 : 0 : n_low = build_zero_cst (arg0_type);
5502 : 0 : in_p = !in_p;
5503 : : }
5504 : : }
5505 : :
5506 : 904018 : *p_low = n_low;
5507 : 904018 : *p_high = n_high;
5508 : 904018 : *p_in_p = in_p;
5509 : 904018 : return arg0;
5510 : :
5511 : : default:
5512 : : return NULL_TREE;
5513 : : }
5514 : : }
5515 : :
5516 : : /* Given EXP, a logical expression, set the range it is testing into
5517 : : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5518 : : actually being tested. *PLOW and *PHIGH will be made of the same
5519 : : type as the returned expression. If EXP is not a comparison, we
5520 : : will most likely not be returning a useful value and range. Set
5521 : : *STRICT_OVERFLOW_P to true if the return value is only valid
5522 : : because signed overflow is undefined; otherwise, do not change
5523 : : *STRICT_OVERFLOW_P. */
5524 : :
5525 : : tree
5526 : 47599524 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5527 : : bool *strict_overflow_p)
5528 : : {
5529 : 47599524 : enum tree_code code;
5530 : 47599524 : tree arg0, arg1 = NULL_TREE;
5531 : 47599524 : tree exp_type, nexp;
5532 : 47599524 : int in_p;
5533 : 47599524 : tree low, high;
5534 : 47599524 : location_t loc = EXPR_LOCATION (exp);
5535 : :
5536 : : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5537 : : and see if we can refine the range. Some of the cases below may not
5538 : : happen, but it doesn't seem worth worrying about this. We "continue"
5539 : : the outer loop when we've changed something; otherwise we "break"
5540 : : the switch, which will "break" the while. */
5541 : :
5542 : 47599524 : in_p = 0;
5543 : 47599524 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5544 : :
5545 : 76184124 : while (1)
5546 : : {
5547 : 76184124 : code = TREE_CODE (exp);
5548 : 76184124 : exp_type = TREE_TYPE (exp);
5549 : 76184124 : arg0 = NULL_TREE;
5550 : :
5551 : 76184124 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5552 : : {
5553 : 52625607 : if (TREE_OPERAND_LENGTH (exp) > 0)
5554 : 52625607 : arg0 = TREE_OPERAND (exp, 0);
5555 : 52625607 : if (TREE_CODE_CLASS (code) == tcc_binary
5556 : 49444359 : || TREE_CODE_CLASS (code) == tcc_comparison
5557 : 60720951 : || (TREE_CODE_CLASS (code) == tcc_expression
5558 : 2634371 : && TREE_OPERAND_LENGTH (exp) > 1))
5559 : 45521366 : arg1 = TREE_OPERAND (exp, 1);
5560 : : }
5561 : 52625607 : if (arg0 == NULL_TREE)
5562 : : break;
5563 : :
5564 : 52625593 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5565 : : &high, &in_p, strict_overflow_p);
5566 : 52625593 : if (nexp == NULL_TREE)
5567 : : break;
5568 : : exp = nexp;
5569 : : }
5570 : :
5571 : : /* If EXP is a constant, we can evaluate whether this is true or false. */
5572 : 47599524 : if (TREE_CODE (exp) == INTEGER_CST)
5573 : : {
5574 : 33812 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5575 : : exp, 0, low, 0))
5576 : 33812 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5577 : : exp, 1, high, 1)));
5578 : 33812 : low = high = 0;
5579 : 33812 : exp = 0;
5580 : : }
5581 : :
5582 : 47599524 : *pin_p = in_p, *plow = low, *phigh = high;
5583 : 47599524 : return exp;
5584 : : }
5585 : :
5586 : : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5587 : : a bitwise check i.e. when
5588 : : LOW == 0xXX...X00...0
5589 : : HIGH == 0xXX...X11...1
5590 : : Return corresponding mask in MASK and stem in VALUE. */
5591 : :
5592 : : static bool
5593 : 131 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5594 : : tree *value)
5595 : : {
5596 : 131 : if (TREE_CODE (low) != INTEGER_CST
5597 : 131 : || TREE_CODE (high) != INTEGER_CST)
5598 : : return false;
5599 : :
5600 : 131 : unsigned prec = TYPE_PRECISION (type);
5601 : 131 : wide_int lo = wi::to_wide (low, prec);
5602 : 131 : wide_int hi = wi::to_wide (high, prec);
5603 : :
5604 : 131 : wide_int end_mask = lo ^ hi;
5605 : 262 : if ((end_mask & (end_mask + 1)) != 0
5606 : 241 : || (lo & end_mask) != 0)
5607 : : return false;
5608 : :
5609 : 86 : wide_int stem_mask = ~end_mask;
5610 : 86 : wide_int stem = lo & stem_mask;
5611 : 86 : if (stem != (hi & stem_mask))
5612 : : return false;
5613 : :
5614 : 86 : *mask = wide_int_to_tree (type, stem_mask);
5615 : 86 : *value = wide_int_to_tree (type, stem);
5616 : :
5617 : 86 : return true;
5618 : 217 : }
5619 : :
5620 : : /* Helper routine for build_range_check and match.pd. Return the type to
5621 : : perform the check or NULL if it shouldn't be optimized. */
5622 : :
5623 : : tree
5624 : 549470 : range_check_type (tree etype)
5625 : : {
5626 : : /* First make sure that arithmetics in this type is valid, then make sure
5627 : : that it wraps around. */
5628 : 549470 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5629 : 62678 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5630 : :
5631 : 549470 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5632 : : {
5633 : 403723 : tree utype, minv, maxv;
5634 : :
5635 : : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5636 : : for the type in question, as we rely on this here. */
5637 : 403723 : utype = unsigned_type_for (etype);
5638 : 403723 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5639 : 403723 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5640 : 403723 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5641 : 403723 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5642 : :
5643 : 403723 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5644 : : minv, 1, maxv, 1)))
5645 : : etype = utype;
5646 : : else
5647 : 0 : return NULL_TREE;
5648 : : }
5649 : 145747 : else if (POINTER_TYPE_P (etype)
5650 : : || TREE_CODE (etype) == OFFSET_TYPE
5651 : : /* Right now all BITINT_TYPEs satisfy
5652 : : (unsigned) max + 1 == (unsigned) min, so no need to verify
5653 : : that like for INTEGER_TYPEs. */
5654 : : || TREE_CODE (etype) == BITINT_TYPE)
5655 : 1357 : etype = unsigned_type_for (etype);
5656 : : return etype;
5657 : : }
5658 : :
5659 : : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5660 : : type, TYPE, return an expression to test if EXP is in (or out of, depending
5661 : : on IN_P) the range. Return 0 if the test couldn't be created. */
5662 : :
5663 : : tree
5664 : 1445214 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5665 : : tree low, tree high)
5666 : : {
5667 : 2503764 : tree etype = TREE_TYPE (exp), mask, value;
5668 : :
5669 : : /* Disable this optimization for function pointer expressions
5670 : : on targets that require function pointer canonicalization. */
5671 : 2503764 : if (targetm.have_canonicalize_funcptr_for_compare ()
5672 : 0 : && POINTER_TYPE_P (etype)
5673 : 2503764 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5674 : : return NULL_TREE;
5675 : :
5676 : 2503764 : if (! in_p)
5677 : : {
5678 : 347427 : value = build_range_check (loc, type, exp, 1, low, high);
5679 : 347427 : if (value != 0)
5680 : 347427 : return invert_truthvalue_loc (loc, value);
5681 : :
5682 : : return 0;
5683 : : }
5684 : :
5685 : 2156337 : if (low == 0 && high == 0)
5686 : 136863 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5687 : :
5688 : 2019474 : if (low == 0)
5689 : 697297 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5690 : 697297 : fold_convert_loc (loc, etype, high));
5691 : :
5692 : 1322177 : if (high == 0)
5693 : 76753 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5694 : 76753 : fold_convert_loc (loc, etype, low));
5695 : :
5696 : 1245424 : if (operand_equal_p (low, high, 0))
5697 : 186678 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5698 : 186678 : fold_convert_loc (loc, etype, low));
5699 : :
5700 : 1058746 : if (TREE_CODE (exp) == BIT_AND_EXPR
5701 : 1058746 : && maskable_range_p (low, high, etype, &mask, &value))
5702 : 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5703 : : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5704 : : exp, mask),
5705 : 86 : value);
5706 : :
5707 : 1058660 : if (integer_zerop (low))
5708 : : {
5709 : 602377 : if (! TYPE_UNSIGNED (etype))
5710 : : {
5711 : 126651 : etype = unsigned_type_for (etype);
5712 : 126651 : high = fold_convert_loc (loc, etype, high);
5713 : 126651 : exp = fold_convert_loc (loc, etype, exp);
5714 : : }
5715 : 602377 : return build_range_check (loc, type, exp, 1, 0, high);
5716 : : }
5717 : :
5718 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5719 : 456283 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5720 : : {
5721 : 106219 : int prec = TYPE_PRECISION (etype);
5722 : :
5723 : 106219 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5724 : : {
5725 : 110 : if (TYPE_UNSIGNED (etype))
5726 : : {
5727 : 104 : tree signed_etype = signed_type_for (etype);
5728 : 104 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5729 : 0 : etype
5730 : 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5731 : : else
5732 : : etype = signed_etype;
5733 : 104 : exp = fold_convert_loc (loc, etype, exp);
5734 : : }
5735 : 110 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5736 : : build_int_cst (etype, 0));
5737 : : }
5738 : : }
5739 : :
5740 : : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5741 : : This requires wrap-around arithmetics for the type of the expression. */
5742 : 456173 : etype = range_check_type (etype);
5743 : 456173 : if (etype == NULL_TREE)
5744 : : return NULL_TREE;
5745 : :
5746 : 456173 : high = fold_convert_loc (loc, etype, high);
5747 : 456173 : low = fold_convert_loc (loc, etype, low);
5748 : 456173 : exp = fold_convert_loc (loc, etype, exp);
5749 : :
5750 : 456173 : value = const_binop (MINUS_EXPR, high, low);
5751 : :
5752 : 456173 : if (value != 0 && !TREE_OVERFLOW (value))
5753 : 456173 : return build_range_check (loc, type,
5754 : : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5755 : : 1, build_int_cst (etype, 0), value);
5756 : :
5757 : : return 0;
5758 : : }
5759 : :
5760 : : /* Return the predecessor of VAL in its type, handling the infinite case. */
5761 : :
5762 : : static tree
5763 : 164503 : range_predecessor (tree val)
5764 : : {
5765 : 164503 : tree type = TREE_TYPE (val);
5766 : :
5767 : 164503 : if (INTEGRAL_TYPE_P (type)
5768 : 164503 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5769 : : return 0;
5770 : : else
5771 : 164503 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5772 : 164503 : build_int_cst (TREE_TYPE (val), 1), 0);
5773 : : }
5774 : :
5775 : : /* Return the successor of VAL in its type, handling the infinite case. */
5776 : :
5777 : : static tree
5778 : 1539744 : range_successor (tree val)
5779 : : {
5780 : 1539744 : tree type = TREE_TYPE (val);
5781 : :
5782 : 1539744 : if (INTEGRAL_TYPE_P (type)
5783 : 1539744 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5784 : : return 0;
5785 : : else
5786 : 1539735 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5787 : 1539735 : build_int_cst (TREE_TYPE (val), 1), 0);
5788 : : }
5789 : :
5790 : : /* Given two ranges, see if we can merge them into one. Return 1 if we
5791 : : can, 0 if we can't. Set the output range into the specified parameters. */
5792 : :
5793 : : bool
5794 : 3447445 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5795 : : tree high0, int in1_p, tree low1, tree high1)
5796 : : {
5797 : 3447445 : bool no_overlap;
5798 : 3447445 : int subset;
5799 : 3447445 : int temp;
5800 : 3447445 : tree tem;
5801 : 3447445 : int in_p;
5802 : 3447445 : tree low, high;
5803 : 3447445 : int lowequal = ((low0 == 0 && low1 == 0)
5804 : 3447445 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5805 : 3447445 : low0, 0, low1, 0)));
5806 : 3447445 : int highequal = ((high0 == 0 && high1 == 0)
5807 : 3447445 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5808 : 3447445 : high0, 1, high1, 1)));
5809 : :
5810 : : /* Make range 0 be the range that starts first, or ends last if they
5811 : : start at the same value. Swap them if it isn't. */
5812 : 3447445 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5813 : : low0, 0, low1, 0))
5814 : 3447445 : || (lowequal
5815 : 564739 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5816 : : high1, 1, high0, 1))))
5817 : : {
5818 : : temp = in0_p, in0_p = in1_p, in1_p = temp;
5819 : : tem = low0, low0 = low1, low1 = tem;
5820 : : tem = high0, high0 = high1, high1 = tem;
5821 : : }
5822 : :
5823 : : /* If the second range is != high1 where high1 is the type maximum of
5824 : : the type, try first merging with < high1 range. */
5825 : 3447445 : if (low1
5826 : 3447445 : && high1
5827 : 883275 : && TREE_CODE (low1) == INTEGER_CST
5828 : 883275 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5829 : 143959 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5830 : 200270 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5831 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5832 : 4286896 : && operand_equal_p (low1, high1, 0))
5833 : : {
5834 : 488301 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5835 : 488301 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5836 : : !in1_p, NULL_TREE, range_predecessor (low1)))
5837 : : return true;
5838 : : /* Similarly for the second range != low1 where low1 is the type minimum
5839 : : of the type, try first merging with > low1 range. */
5840 : 392929 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5841 : 392929 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5842 : : !in1_p, range_successor (low1), NULL_TREE))
5843 : : return true;
5844 : : }
5845 : :
5846 : : /* Now flag two cases, whether the ranges are disjoint or whether the
5847 : : second range is totally subsumed in the first. Note that the tests
5848 : : below are simplified by the ones above. */
5849 : 3286559 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5850 : : high0, 1, low1, 0));
5851 : 3286559 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5852 : : high1, 1, high0, 1));
5853 : :
5854 : : /* We now have four cases, depending on whether we are including or
5855 : : excluding the two ranges. */
5856 : 3286559 : if (in0_p && in1_p)
5857 : : {
5858 : : /* If they don't overlap, the result is false. If the second range
5859 : : is a subset it is the result. Otherwise, the range is from the start
5860 : : of the second to the end of the first. */
5861 : 1483894 : if (no_overlap)
5862 : : in_p = 0, low = high = 0;
5863 : 1481625 : else if (subset)
5864 : : in_p = 1, low = low1, high = high1;
5865 : : else
5866 : 1339939 : in_p = 1, low = low1, high = high0;
5867 : : }
5868 : :
5869 : 1802665 : else if (in0_p && ! in1_p)
5870 : : {
5871 : : /* If they don't overlap, the result is the first range. If they are
5872 : : equal, the result is false. If the second range is a subset of the
5873 : : first, and the ranges begin at the same place, we go from just after
5874 : : the end of the second range to the end of the first. If the second
5875 : : range is not a subset of the first, or if it is a subset and both
5876 : : ranges end at the same place, the range starts at the start of the
5877 : : first range and ends just before the second range.
5878 : : Otherwise, we can't describe this as a single range. */
5879 : 309253 : if (no_overlap)
5880 : : in_p = 1, low = low0, high = high0;
5881 : 303685 : else if (lowequal && highequal)
5882 : : in_p = 0, low = high = 0;
5883 : 302658 : else if (subset && lowequal)
5884 : : {
5885 : 220808 : low = range_successor (high1);
5886 : 220808 : high = high0;
5887 : 220808 : in_p = 1;
5888 : 220808 : if (low == 0)
5889 : : {
5890 : : /* We are in the weird situation where high0 > high1 but
5891 : : high1 has no successor. Punt. */
5892 : : return 0;
5893 : : }
5894 : : }
5895 : 81850 : else if (! subset || highequal)
5896 : : {
5897 : 56445 : low = low0;
5898 : 56445 : high = range_predecessor (low1);
5899 : 56445 : in_p = 1;
5900 : 56445 : if (high == 0)
5901 : : {
5902 : : /* low0 < low1 but low1 has no predecessor. Punt. */
5903 : : return 0;
5904 : : }
5905 : : }
5906 : : else
5907 : : return 0;
5908 : : }
5909 : :
5910 : 1493412 : else if (! in0_p && in1_p)
5911 : : {
5912 : : /* If they don't overlap, the result is the second range. If the second
5913 : : is a subset of the first, the result is false. Otherwise,
5914 : : the range starts just after the first range and ends at the
5915 : : end of the second. */
5916 : 1152081 : if (no_overlap)
5917 : : in_p = 1, low = low1, high = high1;
5918 : 1144699 : else if (subset || highequal)
5919 : : in_p = 0, low = high = 0;
5920 : : else
5921 : : {
5922 : 1026325 : low = range_successor (high0);
5923 : 1026325 : high = high1;
5924 : 1026325 : in_p = 1;
5925 : 1026325 : if (low == 0)
5926 : : {
5927 : : /* high1 > high0 but high0 has no successor. Punt. */
5928 : : return 0;
5929 : : }
5930 : : }
5931 : : }
5932 : :
5933 : : else
5934 : : {
5935 : : /* The case where we are excluding both ranges. Here the complex case
5936 : : is if they don't overlap. In that case, the only time we have a
5937 : : range is if they are adjacent. If the second is a subset of the
5938 : : first, the result is the first. Otherwise, the range to exclude
5939 : : starts at the beginning of the first range and ends at the end of the
5940 : : second. */
5941 : 341331 : if (no_overlap)
5942 : : {
5943 : 226556 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5944 : : range_successor (high0),
5945 : : 1, low1, 0)))
5946 : : in_p = 0, low = low0, high = high1;
5947 : : else
5948 : : {
5949 : : /* Canonicalize - [min, x] into - [-, x]. */
5950 : 173390 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5951 : 172115 : switch (TREE_CODE (TREE_TYPE (low0)))
5952 : : {
5953 : 64672 : case ENUMERAL_TYPE:
5954 : 64672 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5955 : : GET_MODE_BITSIZE
5956 : 129344 : (TYPE_MODE (TREE_TYPE (low0)))))
5957 : : break;
5958 : : /* FALLTHROUGH */
5959 : 171922 : case INTEGER_TYPE:
5960 : 171922 : if (tree_int_cst_equal (low0,
5961 : 171922 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
5962 : 7062 : low0 = 0;
5963 : : break;
5964 : 193 : case POINTER_TYPE:
5965 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
5966 : 193 : && integer_zerop (low0))
5967 : : low0 = 0;
5968 : : break;
5969 : : default:
5970 : : break;
5971 : : }
5972 : :
5973 : : /* Canonicalize - [x, max] into - [x, -]. */
5974 : 173390 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
5975 : 173184 : switch (TREE_CODE (TREE_TYPE (high1)))
5976 : : {
5977 : 64680 : case ENUMERAL_TYPE:
5978 : 64680 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5979 : : GET_MODE_BITSIZE
5980 : 129360 : (TYPE_MODE (TREE_TYPE (high1)))))
5981 : : break;
5982 : : /* FALLTHROUGH */
5983 : 172991 : case INTEGER_TYPE:
5984 : 172991 : if (tree_int_cst_equal (high1,
5985 : 172991 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
5986 : 12485 : high1 = 0;
5987 : : break;
5988 : 193 : case POINTER_TYPE:
5989 : 193 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
5990 : 386 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5991 : : high1, 1,
5992 : 193 : build_int_cst (TREE_TYPE (high1), 1),
5993 : : 1)))
5994 : 133 : high1 = 0;
5995 : : break;
5996 : : default:
5997 : : break;
5998 : : }
5999 : :
6000 : : /* The ranges might be also adjacent between the maximum and
6001 : : minimum values of the given type. For
6002 : : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6003 : : return + [x + 1, y - 1]. */
6004 : 173390 : if (low0 == 0 && high1 == 0)
6005 : : {
6006 : 541 : low = range_successor (high0);
6007 : 541 : high = range_predecessor (low1);
6008 : 541 : if (low == 0 || high == 0)
6009 : : return 0;
6010 : :
6011 : : in_p = 1;
6012 : : }
6013 : : else
6014 : : return 0;
6015 : : }
6016 : : }
6017 : 114775 : else if (subset)
6018 : : in_p = 0, low = low0, high = high0;
6019 : : else
6020 : 15495 : in_p = 0, low = low0, high = high1;
6021 : : }
6022 : :
6023 : 3088296 : *pin_p = in_p, *plow = low, *phigh = high;
6024 : 3088296 : return 1;
6025 : : }
6026 : :
6027 : :
6028 : : /* Subroutine of fold, looking inside expressions of the form
6029 : : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6030 : : are the three operands of the COND_EXPR. This function is
6031 : : being used also to optimize A op B ? C : A, by reversing the
6032 : : comparison first.
6033 : :
6034 : : Return a folded expression whose code is not a COND_EXPR
6035 : : anymore, or NULL_TREE if no folding opportunity is found. */
6036 : :
6037 : : static tree
6038 : 438970 : fold_cond_expr_with_comparison (location_t loc, tree type,
6039 : : enum tree_code comp_code,
6040 : : tree arg00, tree arg01, tree arg1, tree arg2)
6041 : : {
6042 : 438970 : tree arg1_type = TREE_TYPE (arg1);
6043 : 438970 : tree tem;
6044 : :
6045 : 438970 : STRIP_NOPS (arg1);
6046 : 438970 : STRIP_NOPS (arg2);
6047 : :
6048 : : /* If we have A op 0 ? A : -A, consider applying the following
6049 : : transformations:
6050 : :
6051 : : A == 0? A : -A same as -A
6052 : : A != 0? A : -A same as A
6053 : : A >= 0? A : -A same as abs (A)
6054 : : A > 0? A : -A same as abs (A)
6055 : : A <= 0? A : -A same as -abs (A)
6056 : : A < 0? A : -A same as -abs (A)
6057 : :
6058 : : None of these transformations work for modes with signed
6059 : : zeros. If A is +/-0, the first two transformations will
6060 : : change the sign of the result (from +0 to -0, or vice
6061 : : versa). The last four will fix the sign of the result,
6062 : : even though the original expressions could be positive or
6063 : : negative, depending on the sign of A.
6064 : :
6065 : : Note that all these transformations are correct if A is
6066 : : NaN, since the two alternatives (A and -A) are also NaNs. */
6067 : 438970 : if (!HONOR_SIGNED_ZEROS (type)
6068 : 877950 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6069 : 438970 : ? real_zerop (arg01)
6070 : 437910 : : integer_zerop (arg01))
6071 : 1163449 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6072 : 1486 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6073 : : /* In the case that A is of the form X-Y, '-A' (arg2) may
6074 : : have already been folded to Y-X, check for that. */
6075 : 284247 : || (TREE_CODE (arg1) == MINUS_EXPR
6076 : 1718 : && TREE_CODE (arg2) == MINUS_EXPR
6077 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6078 : 0 : TREE_OPERAND (arg2, 1), 0)
6079 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6080 : 0 : TREE_OPERAND (arg2, 0), 0))))
6081 : 1262 : switch (comp_code)
6082 : : {
6083 : 0 : case EQ_EXPR:
6084 : 0 : case UNEQ_EXPR:
6085 : 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6086 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6087 : 0 : case NE_EXPR:
6088 : 0 : case LTGT_EXPR:
6089 : 0 : return fold_convert_loc (loc, type, arg1);
6090 : 0 : case UNGE_EXPR:
6091 : 0 : case UNGT_EXPR:
6092 : 0 : if (flag_trapping_math)
6093 : : break;
6094 : : /* Fall through. */
6095 : 1262 : case GE_EXPR:
6096 : 1262 : case GT_EXPR:
6097 : 1262 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6098 : : break;
6099 : 1246 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6100 : 1246 : return fold_convert_loc (loc, type, tem);
6101 : 0 : case UNLE_EXPR:
6102 : 0 : case UNLT_EXPR:
6103 : 0 : if (flag_trapping_math)
6104 : : break;
6105 : : /* FALLTHRU */
6106 : 0 : case LE_EXPR:
6107 : 0 : case LT_EXPR:
6108 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6109 : : break;
6110 : 0 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6111 : 0 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6112 : : {
6113 : : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6114 : : is not, invokes UB both in abs and in the negation of it.
6115 : : So, use ABSU_EXPR instead. */
6116 : 0 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6117 : 0 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6118 : 0 : tem = negate_expr (tem);
6119 : 0 : return fold_convert_loc (loc, type, tem);
6120 : : }
6121 : : else
6122 : : {
6123 : 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6124 : 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6125 : : }
6126 : 0 : default:
6127 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6128 : : break;
6129 : : }
6130 : :
6131 : : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6132 : : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6133 : : both transformations are correct when A is NaN: A != 0
6134 : : is then true, and A == 0 is false. */
6135 : :
6136 : 437724 : if (!HONOR_SIGNED_ZEROS (type)
6137 : 437724 : && integer_zerop (arg01) && integer_zerop (arg2))
6138 : : {
6139 : 237840 : if (comp_code == NE_EXPR)
6140 : 145 : return fold_convert_loc (loc, type, arg1);
6141 : 237695 : else if (comp_code == EQ_EXPR)
6142 : 0 : return build_zero_cst (type);
6143 : : }
6144 : :
6145 : : /* Try some transformations of A op B ? A : B.
6146 : :
6147 : : A == B? A : B same as B
6148 : : A != B? A : B same as A
6149 : : A >= B? A : B same as max (A, B)
6150 : : A > B? A : B same as max (B, A)
6151 : : A <= B? A : B same as min (A, B)
6152 : : A < B? A : B same as min (B, A)
6153 : :
6154 : : As above, these transformations don't work in the presence
6155 : : of signed zeros. For example, if A and B are zeros of
6156 : : opposite sign, the first two transformations will change
6157 : : the sign of the result. In the last four, the original
6158 : : expressions give different results for (A=+0, B=-0) and
6159 : : (A=-0, B=+0), but the transformed expressions do not.
6160 : :
6161 : : The first two transformations are correct if either A or B
6162 : : is a NaN. In the first transformation, the condition will
6163 : : be false, and B will indeed be chosen. In the case of the
6164 : : second transformation, the condition A != B will be true,
6165 : : and A will be chosen.
6166 : :
6167 : : The conversions to max() and min() are not correct if B is
6168 : : a number and A is not. The conditions in the original
6169 : : expressions will be false, so all four give B. The min()
6170 : : and max() versions would give a NaN instead. */
6171 : 437579 : if (!HONOR_SIGNED_ZEROS (type)
6172 : 437579 : && operand_equal_for_comparison_p (arg01, arg2)
6173 : : /* Avoid these transformations if the COND_EXPR may be used
6174 : : as an lvalue in the C++ front-end. PR c++/19199. */
6175 : 683764 : && (in_gimple_form
6176 : 16123 : || VECTOR_TYPE_P (type)
6177 : 16061 : || (! lang_GNU_CXX ()
6178 : 13481 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6179 : 2580 : || ! maybe_lvalue_p (arg1)
6180 : 2559 : || ! maybe_lvalue_p (arg2)))
6181 : : {
6182 : 244534 : tree comp_op0 = arg00;
6183 : 244534 : tree comp_op1 = arg01;
6184 : 244534 : tree comp_type = TREE_TYPE (comp_op0);
6185 : :
6186 : 244534 : switch (comp_code)
6187 : : {
6188 : 0 : case EQ_EXPR:
6189 : 0 : return fold_convert_loc (loc, type, arg2);
6190 : 1 : case NE_EXPR:
6191 : 1 : return fold_convert_loc (loc, type, arg1);
6192 : 5858 : case LE_EXPR:
6193 : 5858 : case LT_EXPR:
6194 : 5858 : case UNLE_EXPR:
6195 : 5858 : case UNLT_EXPR:
6196 : : /* In C++ a ?: expression can be an lvalue, so put the
6197 : : operand which will be used if they are equal first
6198 : : so that we can convert this back to the
6199 : : corresponding COND_EXPR. */
6200 : 5858 : if (!HONOR_NANS (arg1))
6201 : : {
6202 : 5858 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6203 : 5858 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6204 : 5858 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6205 : 5858 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6206 : 4499 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6207 : : comp_op1, comp_op0);
6208 : 5858 : return fold_convert_loc (loc, type, tem);
6209 : : }
6210 : : break;
6211 : 238675 : case GE_EXPR:
6212 : 238675 : case GT_EXPR:
6213 : 238675 : case UNGE_EXPR:
6214 : 238675 : case UNGT_EXPR:
6215 : 238675 : if (!HONOR_NANS (arg1))
6216 : : {
6217 : 238673 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6218 : 238673 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6219 : 238673 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6220 : 238673 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6221 : 3434 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6222 : : comp_op1, comp_op0);
6223 : 238673 : return fold_convert_loc (loc, type, tem);
6224 : : }
6225 : : break;
6226 : 0 : case UNEQ_EXPR:
6227 : 0 : if (!HONOR_NANS (arg1))
6228 : 0 : return fold_convert_loc (loc, type, arg2);
6229 : : break;
6230 : 0 : case LTGT_EXPR:
6231 : 0 : if (!HONOR_NANS (arg1))
6232 : 0 : return fold_convert_loc (loc, type, arg1);
6233 : : break;
6234 : 0 : default:
6235 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6236 : : break;
6237 : : }
6238 : : }
6239 : :
6240 : : return NULL_TREE;
6241 : : }
6242 : :
6243 : :
6244 : :
6245 : : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6246 : : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6247 : : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6248 : : false) >= 2)
6249 : : #endif
6250 : :
6251 : : /* EXP is some logical combination of boolean tests. See if we can
6252 : : merge it into some range test. Return the new tree if so. */
6253 : :
6254 : : static tree
6255 : 23799256 : fold_range_test (location_t loc, enum tree_code code, tree type,
6256 : : tree op0, tree op1)
6257 : : {
6258 : 23799256 : int or_op = (code == TRUTH_ORIF_EXPR
6259 : 23799256 : || code == TRUTH_OR_EXPR);
6260 : 23799256 : int in0_p, in1_p, in_p;
6261 : 23799256 : tree low0, low1, low, high0, high1, high;
6262 : 23799256 : bool strict_overflow_p = false;
6263 : 23799256 : tree tem, lhs, rhs;
6264 : 23799256 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6265 : : "when simplifying range test");
6266 : :
6267 : 23799256 : if (!INTEGRAL_TYPE_P (type))
6268 : : return 0;
6269 : :
6270 : 23799256 : lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6271 : : /* If op0 is known true or false and this is a short-circuiting
6272 : : operation we must not merge with op1 since that makes side-effects
6273 : : unconditional. So special-case this. */
6274 : 23799256 : if (!lhs
6275 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6276 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6277 : : return op0;
6278 : 23799254 : rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6279 : :
6280 : : /* If this is an OR operation, invert both sides; we will invert
6281 : : again at the end. */
6282 : 23799254 : if (or_op)
6283 : 11555957 : in0_p = ! in0_p, in1_p = ! in1_p;
6284 : :
6285 : : /* If both expressions are the same, if we can merge the ranges, and we
6286 : : can build the range test, return it or it inverted. If one of the
6287 : : ranges is always true or always false, consider it to be the same
6288 : : expression as the other. */
6289 : 23765446 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6290 : 1100476 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6291 : : in1_p, low1, high1)
6292 : 24736137 : && (tem = (build_range_check (loc, type,
6293 : : lhs != 0 ? lhs
6294 : 0 : : rhs != 0 ? rhs : integer_zero_node,
6295 : : in_p, low, high))) != 0)
6296 : : {
6297 : 936883 : if (strict_overflow_p)
6298 : 259 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6299 : 936883 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6300 : : }
6301 : :
6302 : : /* On machines where the branch cost is expensive, if this is a
6303 : : short-circuited branch and the underlying object on both sides
6304 : : is the same, make a non-short-circuit operation. */
6305 : 22862371 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6306 : 22862371 : if (param_logical_op_non_short_circuit != -1)
6307 : 7777 : logical_op_non_short_circuit
6308 : 7777 : = param_logical_op_non_short_circuit;
6309 : 22862371 : if (logical_op_non_short_circuit
6310 : 22858474 : && !sanitize_coverage_p ()
6311 : 22858471 : && lhs != 0 && rhs != 0
6312 : 22858292 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6313 : 27403717 : && operand_equal_p (lhs, rhs, 0))
6314 : : {
6315 : : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6316 : : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6317 : : which cases we can't do this. */
6318 : 131630 : if (simple_operand_p (lhs))
6319 : 125197 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6320 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6321 : 63116 : type, op0, op1);
6322 : :
6323 : 68514 : else if (!lang_hooks.decls.global_bindings_p ()
6324 : 68514 : && !CONTAINS_PLACEHOLDER_P (lhs))
6325 : : {
6326 : 67861 : tree common = save_expr (lhs);
6327 : :
6328 : 111114 : if ((lhs = build_range_check (loc, type, common,
6329 : 43253 : or_op ? ! in0_p : in0_p,
6330 : : low0, high0)) != 0
6331 : 111114 : && (rhs = build_range_check (loc, type, common,
6332 : 43253 : or_op ? ! in1_p : in1_p,
6333 : : low1, high1)) != 0)
6334 : : {
6335 : 67861 : if (strict_overflow_p)
6336 : 0 : fold_overflow_warning (warnmsg,
6337 : : WARN_STRICT_OVERFLOW_COMPARISON);
6338 : 111114 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6339 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6340 : 67861 : type, lhs, rhs);
6341 : : }
6342 : : }
6343 : : }
6344 : :
6345 : : return 0;
6346 : : }
6347 : :
6348 : : /* For an expression that has the form
6349 : : (A && B) || ~B
6350 : : or
6351 : : (A || B) && ~B,
6352 : : we can drop one of the inner expressions and simplify to
6353 : : A || ~B
6354 : : or
6355 : : A && ~B
6356 : : LOC is the location of the resulting expression. OP is the inner
6357 : : logical operation; the left-hand side in the examples above, while CMPOP
6358 : : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6359 : : removing a condition that guards another, as in
6360 : : (A != NULL && A->...) || A == NULL
6361 : : which we must not transform. If RHS_ONLY is true, only eliminate the
6362 : : right-most operand of the inner logical operation. */
6363 : :
6364 : : static tree
6365 : 135916 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6366 : : bool rhs_only)
6367 : : {
6368 : 135916 : enum tree_code code = TREE_CODE (cmpop);
6369 : 135916 : enum tree_code truthop_code = TREE_CODE (op);
6370 : 135916 : tree lhs = TREE_OPERAND (op, 0);
6371 : 135916 : tree rhs = TREE_OPERAND (op, 1);
6372 : 135916 : tree orig_lhs = lhs, orig_rhs = rhs;
6373 : 135916 : enum tree_code rhs_code = TREE_CODE (rhs);
6374 : 135916 : enum tree_code lhs_code = TREE_CODE (lhs);
6375 : 135916 : enum tree_code inv_code;
6376 : :
6377 : 135916 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6378 : : return NULL_TREE;
6379 : :
6380 : 91676 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6381 : : return NULL_TREE;
6382 : :
6383 : 54103 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6384 : :
6385 : 54103 : if (rhs_code == truthop_code)
6386 : : {
6387 : 29 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6388 : 29 : if (newrhs != NULL_TREE)
6389 : : {
6390 : 0 : rhs = newrhs;
6391 : 0 : rhs_code = TREE_CODE (rhs);
6392 : : }
6393 : : }
6394 : 54103 : if (lhs_code == truthop_code && !rhs_only)
6395 : : {
6396 : 460 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6397 : 460 : if (newlhs != NULL_TREE)
6398 : : {
6399 : 0 : lhs = newlhs;
6400 : 0 : lhs_code = TREE_CODE (lhs);
6401 : : }
6402 : : }
6403 : :
6404 : 54103 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6405 : 54103 : if (inv_code == rhs_code
6406 : 701 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6407 : 54139 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6408 : : return lhs;
6409 : 54090 : if (!rhs_only && inv_code == lhs_code
6410 : 615 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6411 : 54167 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6412 : : return rhs;
6413 : 54014 : if (rhs != orig_rhs || lhs != orig_lhs)
6414 : 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6415 : 0 : lhs, rhs);
6416 : : return NULL_TREE;
6417 : : }
6418 : :
6419 : : /* Find ways of folding logical expressions of LHS and RHS:
6420 : : Try to merge two comparisons to the same innermost item.
6421 : : Look for range tests like "ch >= '0' && ch <= '9'".
6422 : : Look for combinations of simple terms on machines with expensive branches
6423 : : and evaluate the RHS unconditionally.
6424 : :
6425 : : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6426 : : function and the one above.
6427 : :
6428 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6429 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6430 : :
6431 : : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6432 : : two operands.
6433 : :
6434 : : We return the simplified tree or 0 if no optimization is possible. */
6435 : :
6436 : : static tree
6437 : 23444719 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6438 : : tree lhs, tree rhs)
6439 : : {
6440 : : /* If this is the "or" of two comparisons, we can do something if
6441 : : the comparisons are NE_EXPR. If this is the "and", we can do something
6442 : : if the comparisons are EQ_EXPR. I.e.,
6443 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6444 : :
6445 : : WANTED_CODE is this operation code. For single bit fields, we can
6446 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6447 : : comparison for one-bit fields. */
6448 : :
6449 : 23444719 : enum tree_code lcode, rcode;
6450 : 23444719 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6451 : 23444719 : tree result;
6452 : :
6453 : : /* Start by getting the comparison codes. Fail if anything is volatile.
6454 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6455 : : it were surrounded with a NE_EXPR. */
6456 : :
6457 : 23444719 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6458 : : return 0;
6459 : :
6460 : 21182438 : lcode = TREE_CODE (lhs);
6461 : 21182438 : rcode = TREE_CODE (rhs);
6462 : :
6463 : 21182438 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6464 : : {
6465 : 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6466 : 0 : build_int_cst (TREE_TYPE (lhs), 0));
6467 : 0 : lcode = NE_EXPR;
6468 : : }
6469 : :
6470 : 21182438 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6471 : : {
6472 : 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6473 : 0 : build_int_cst (TREE_TYPE (rhs), 0));
6474 : 0 : rcode = NE_EXPR;
6475 : : }
6476 : :
6477 : 21182438 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6478 : 18890540 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6479 : : return 0;
6480 : :
6481 : 17738630 : ll_arg = TREE_OPERAND (lhs, 0);
6482 : 17738630 : lr_arg = TREE_OPERAND (lhs, 1);
6483 : 17738630 : rl_arg = TREE_OPERAND (rhs, 0);
6484 : 17738630 : rr_arg = TREE_OPERAND (rhs, 1);
6485 : :
6486 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6487 : 17738630 : if (simple_operand_p (ll_arg)
6488 : 17738630 : && simple_operand_p (lr_arg))
6489 : : {
6490 : 14449374 : if (operand_equal_p (ll_arg, rl_arg, 0)
6491 : 14449374 : && operand_equal_p (lr_arg, rr_arg, 0))
6492 : : {
6493 : 20034 : result = combine_comparisons (loc, code, lcode, rcode,
6494 : : truth_type, ll_arg, lr_arg);
6495 : 20034 : if (result)
6496 : : return result;
6497 : : }
6498 : 14429340 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6499 : 14429340 : && operand_equal_p (lr_arg, rl_arg, 0))
6500 : : {
6501 : 286 : result = combine_comparisons (loc, code, lcode,
6502 : : swap_tree_comparison (rcode),
6503 : : truth_type, ll_arg, lr_arg);
6504 : 286 : if (result)
6505 : : return result;
6506 : : }
6507 : : }
6508 : :
6509 : 8553869 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6510 : 17718895 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6511 : :
6512 : : /* If the RHS can be evaluated unconditionally and its operands are
6513 : : simple, it wins to evaluate the RHS unconditionally on machines
6514 : : with expensive branches. In this case, this isn't a comparison
6515 : : that can be merged. */
6516 : :
6517 : 17718895 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6518 : : false) >= 2
6519 : 17718786 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6520 : 16725425 : && simple_operand_p (rl_arg)
6521 : 27342944 : && simple_operand_p (rr_arg))
6522 : : {
6523 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6524 : 10715145 : if (code == TRUTH_OR_EXPR
6525 : 1477679 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6526 : 582765 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6527 : 23537 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6528 : 10734431 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6529 : 38040 : return build2_loc (loc, NE_EXPR, truth_type,
6530 : 19020 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6531 : : ll_arg, rl_arg),
6532 : 19020 : build_int_cst (TREE_TYPE (ll_arg), 0));
6533 : :
6534 : : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6535 : 10696125 : if (code == TRUTH_AND_EXPR
6536 : 1626061 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6537 : 731011 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6538 : 5086 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6539 : 10697741 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6540 : 2766 : return build2_loc (loc, EQ_EXPR, truth_type,
6541 : 1383 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6542 : : ll_arg, rl_arg),
6543 : 1383 : build_int_cst (TREE_TYPE (ll_arg), 0));
6544 : : }
6545 : :
6546 : : return 0;
6547 : : }
6548 : :
6549 : : /* T is an integer expression that is being multiplied, divided, or taken a
6550 : : modulus (CODE says which and what kind of divide or modulus) by a
6551 : : constant C. See if we can eliminate that operation by folding it with
6552 : : other operations already in T. WIDE_TYPE, if non-null, is a type that
6553 : : should be used for the computation if wider than our type.
6554 : :
6555 : : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6556 : : (X * 2) + (Y * 4). We must, however, be assured that either the original
6557 : : expression would not overflow or that overflow is undefined for the type
6558 : : in the language in question.
6559 : :
6560 : : If we return a non-null expression, it is an equivalent form of the
6561 : : original computation, but need not be in the original type.
6562 : :
6563 : : We set *STRICT_OVERFLOW_P to true if the return values depends on
6564 : : signed overflow being undefined. Otherwise we do not change
6565 : : *STRICT_OVERFLOW_P. */
6566 : :
6567 : : static tree
6568 : 92466194 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6569 : : bool *strict_overflow_p)
6570 : : {
6571 : : /* To avoid exponential search depth, refuse to allow recursion past
6572 : : three levels. Beyond that (1) it's highly unlikely that we'll find
6573 : : something interesting and (2) we've probably processed it before
6574 : : when we built the inner expression. */
6575 : :
6576 : 92466194 : static int depth;
6577 : 92466194 : tree ret;
6578 : :
6579 : 92466194 : if (depth > 3)
6580 : : return NULL;
6581 : :
6582 : 89201185 : depth++;
6583 : 89201185 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6584 : 89201185 : depth--;
6585 : :
6586 : 89201185 : return ret;
6587 : : }
6588 : :
6589 : : static tree
6590 : 89201185 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6591 : : bool *strict_overflow_p)
6592 : : {
6593 : 89201185 : tree type = TREE_TYPE (t);
6594 : 89201185 : enum tree_code tcode = TREE_CODE (t);
6595 : 89201185 : tree ctype = type;
6596 : 89201185 : if (wide_type)
6597 : : {
6598 : 29992058 : if (TREE_CODE (type) == BITINT_TYPE
6599 : 29991946 : || TREE_CODE (wide_type) == BITINT_TYPE)
6600 : : {
6601 : 112 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
6602 : 8497992 : ctype = wide_type;
6603 : : }
6604 : 29991946 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6605 : 59983892 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6606 : 8497992 : ctype = wide_type;
6607 : : }
6608 : 89201185 : tree t1, t2;
6609 : 89201185 : bool same_p = tcode == code;
6610 : 89201185 : tree op0 = NULL_TREE, op1 = NULL_TREE;
6611 : 89201185 : bool sub_strict_overflow_p;
6612 : :
6613 : : /* Don't deal with constants of zero here; they confuse the code below. */
6614 : 89201185 : if (integer_zerop (c))
6615 : : return NULL_TREE;
6616 : :
6617 : 89175766 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
6618 : 35762385 : op0 = TREE_OPERAND (t, 0);
6619 : :
6620 : 89175766 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
6621 : 10774654 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6622 : :
6623 : : /* Note that we need not handle conditional operations here since fold
6624 : : already handles those cases. So just do arithmetic here. */
6625 : 89175766 : switch (tcode)
6626 : : {
6627 : 4091964 : case INTEGER_CST:
6628 : : /* For a constant, we can always simplify if we are a multiply
6629 : : or (for divide and modulus) if it is a multiple of our constant. */
6630 : 4091964 : if (code == MULT_EXPR
6631 : 5253943 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6632 : 1161979 : TYPE_SIGN (type)))
6633 : : {
6634 : 3306572 : tree tem = const_binop (code, fold_convert (ctype, t),
6635 : : fold_convert (ctype, c));
6636 : : /* If the multiplication overflowed, we lost information on it.
6637 : : See PR68142 and PR69845. */
6638 : 3306572 : if (TREE_OVERFLOW (tem))
6639 : : return NULL_TREE;
6640 : : return tem;
6641 : : }
6642 : : break;
6643 : :
6644 : 35136883 : CASE_CONVERT: case NON_LVALUE_EXPR:
6645 : 35136883 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6646 : : break;
6647 : : /* If op0 is an expression ... */
6648 : 33988738 : if ((COMPARISON_CLASS_P (op0)
6649 : : || UNARY_CLASS_P (op0)
6650 : 33988738 : || BINARY_CLASS_P (op0)
6651 : 31120784 : || VL_EXP_CLASS_P (op0)
6652 : 31091673 : || EXPRESSION_CLASS_P (op0))
6653 : : /* ... and has wrapping overflow, and its type is smaller
6654 : : than ctype, then we cannot pass through as widening. */
6655 : 34107309 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6656 : 1076730 : && (TYPE_PRECISION (ctype)
6657 : 1076730 : > TYPE_PRECISION (TREE_TYPE (op0))))
6658 : : /* ... or this is a truncation (t is narrower than op0),
6659 : : then we cannot pass through this narrowing. */
6660 : 2445692 : || (TYPE_PRECISION (type)
6661 : 2445692 : < TYPE_PRECISION (TREE_TYPE (op0)))
6662 : : /* ... or signedness changes for division or modulus,
6663 : : then we cannot pass through this conversion. */
6664 : 2416412 : || (code != MULT_EXPR
6665 : 109986 : && (TYPE_UNSIGNED (ctype)
6666 : 109986 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
6667 : : /* ... or has undefined overflow while the converted to
6668 : : type has not, we cannot do the operation in the inner type
6669 : : as that would introduce undefined overflow. */
6670 : 2323764 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6671 : 1810389 : && !TYPE_OVERFLOW_UNDEFINED (type))))
6672 : : break;
6673 : :
6674 : : /* Pass the constant down and see if we can make a simplification. If
6675 : : we can, replace this expression with the inner simplification for
6676 : : possible later conversion to our or some other type. */
6677 : 31561734 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6678 : 31561734 : && TREE_CODE (t2) == INTEGER_CST
6679 : 31561734 : && !TREE_OVERFLOW (t2)
6680 : 63940956 : && (t1 = extract_muldiv (op0, t2, code,
6681 : : code == MULT_EXPR ? ctype : NULL_TREE,
6682 : : strict_overflow_p)) != 0)
6683 : : return t1;
6684 : : break;
6685 : :
6686 : 1131 : case ABS_EXPR:
6687 : : /* If widening the type changes it from signed to unsigned, then we
6688 : : must avoid building ABS_EXPR itself as unsigned. */
6689 : 1131 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6690 : : {
6691 : 0 : tree cstype = (*signed_type_for) (ctype);
6692 : 0 : if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6693 : : != 0)
6694 : : {
6695 : 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6696 : 0 : return fold_convert (ctype, t1);
6697 : : }
6698 : : break;
6699 : : }
6700 : : /* If the constant is negative, we cannot simplify this. */
6701 : 1131 : if (tree_int_cst_sgn (c) == -1)
6702 : : break;
6703 : : /* FALLTHROUGH */
6704 : 51430 : case NEGATE_EXPR:
6705 : : /* For division and modulus, type can't be unsigned, as e.g.
6706 : : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6707 : : For signed types, even with wrapping overflow, this is fine. */
6708 : 51430 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6709 : : break;
6710 : 49682 : if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6711 : : != 0)
6712 : 0 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6713 : : break;
6714 : :
6715 : 773 : case MIN_EXPR: case MAX_EXPR:
6716 : : /* If widening the type changes the signedness, then we can't perform
6717 : : this optimization as that changes the result. */
6718 : 773 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6719 : : break;
6720 : :
6721 : : /* Punt for multiplication altogether.
6722 : : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
6723 : : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
6724 : : 0U, the latter is 2U.
6725 : : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
6726 : : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
6727 : : well defined 0, the latter invokes UB.
6728 : : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
6729 : : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
6730 : : well defined 25, the latter invokes UB. */
6731 : 773 : if (code == MULT_EXPR)
6732 : : break;
6733 : : /* For division/modulo, punt on c being -1 for MAX, as
6734 : : MAX (INT_MIN, 0) / -1 is not equivalent to
6735 : : MIN (INT_MIN / -1, 0 / -1), the former is well defined
6736 : : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
6737 : : MIN (INT_MIN, 0) / -1 already invokes UB, so the
6738 : : transformation won't make it worse. */
6739 : 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
6740 : : break;
6741 : :
6742 : : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6743 : 8 : sub_strict_overflow_p = false;
6744 : 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type,
6745 : : &sub_strict_overflow_p)) != 0
6746 : 8 : && (t2 = extract_muldiv (op1, c, code, wide_type,
6747 : : &sub_strict_overflow_p)) != 0)
6748 : : {
6749 : 0 : if (tree_int_cst_sgn (c) < 0)
6750 : 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6751 : 0 : if (sub_strict_overflow_p)
6752 : 0 : *strict_overflow_p = true;
6753 : 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6754 : : fold_convert (ctype, t2));
6755 : : }
6756 : : break;
6757 : :
6758 : 1183 : case LSHIFT_EXPR: case RSHIFT_EXPR:
6759 : : /* If the second operand is constant, this is a multiplication
6760 : : or floor division, by a power of two, so we can treat it that
6761 : : way unless the multiplier or divisor overflows. Signed
6762 : : left-shift overflow is implementation-defined rather than
6763 : : undefined in C90, so do not convert signed left shift into
6764 : : multiplication. */
6765 : 1183 : if (TREE_CODE (op1) == INTEGER_CST
6766 : 1167 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6767 : : /* const_binop may not detect overflow correctly,
6768 : : so check for it explicitly here. */
6769 : 1050 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6770 : 1192 : wi::to_wide (op1))
6771 : 1041 : && (t1 = fold_convert (ctype,
6772 : : const_binop (LSHIFT_EXPR, size_one_node,
6773 : : op1))) != 0
6774 : 2224 : && !TREE_OVERFLOW (t1))
6775 : 1880 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6776 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
6777 : : ctype,
6778 : : fold_convert (ctype, op0),
6779 : : t1),
6780 : 1041 : c, code, wide_type, strict_overflow_p);
6781 : : break;
6782 : :
6783 : 7595266 : case PLUS_EXPR: case MINUS_EXPR:
6784 : : /* See if we can eliminate the operation on both sides. If we can, we
6785 : : can return a new PLUS or MINUS. If we can't, the only remaining
6786 : : cases where we can do anything are if the second operand is a
6787 : : constant. */
6788 : 7595266 : sub_strict_overflow_p = false;
6789 : 7595266 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6790 : 7595266 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6791 : 791722 : if (t1 != 0 && t2 != 0
6792 : 277202 : && TYPE_OVERFLOW_WRAPS (ctype)
6793 : 7863619 : && (code == MULT_EXPR
6794 : : /* If not multiplication, we can only do this if both operands
6795 : : are divisible by c. */
6796 : 0 : || (multiple_of_p (ctype, op0, c)
6797 : 0 : && multiple_of_p (ctype, op1, c))))
6798 : : {
6799 : 268353 : if (sub_strict_overflow_p)
6800 : 0 : *strict_overflow_p = true;
6801 : 268353 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6802 : : fold_convert (ctype, t2));
6803 : : }
6804 : :
6805 : : /* If this was a subtraction, negate OP1 and set it to be an addition.
6806 : : This simplifies the logic below. */
6807 : 7326913 : if (tcode == MINUS_EXPR)
6808 : : {
6809 : 1913801 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
6810 : : /* If OP1 was not easily negatable, the constant may be OP0. */
6811 : 1913801 : if (TREE_CODE (op0) == INTEGER_CST)
6812 : : {
6813 : 350902 : std::swap (op0, op1);
6814 : 350902 : std::swap (t1, t2);
6815 : : }
6816 : : }
6817 : :
6818 : 7326913 : if (TREE_CODE (op1) != INTEGER_CST)
6819 : : break;
6820 : :
6821 : : /* If either OP1 or C are negative, this optimization is not safe for
6822 : : some of the division and remainder types while for others we need
6823 : : to change the code. */
6824 : 3414781 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6825 : : {
6826 : 167421 : if (code == CEIL_DIV_EXPR)
6827 : : code = FLOOR_DIV_EXPR;
6828 : 167419 : else if (code == FLOOR_DIV_EXPR)
6829 : : code = CEIL_DIV_EXPR;
6830 : 167154 : else if (code != MULT_EXPR
6831 : 167154 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6832 : : break;
6833 : : }
6834 : :
6835 : : /* If it's a multiply or a division/modulus operation of a multiple
6836 : : of our constant, do the operation and verify it doesn't overflow. */
6837 : 3410239 : if (code == MULT_EXPR
6838 : 4568264 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6839 : 1158025 : TYPE_SIGN (type)))
6840 : : {
6841 : 2624924 : op1 = const_binop (code, fold_convert (ctype, op1),
6842 : : fold_convert (ctype, c));
6843 : : /* We allow the constant to overflow with wrapping semantics. */
6844 : 2624924 : if (op1 == 0
6845 : 2624924 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6846 : : break;
6847 : : }
6848 : : else
6849 : : break;
6850 : :
6851 : : /* If we have an unsigned type, we cannot widen the operation since it
6852 : : will change the result if the original computation overflowed. */
6853 : 2621612 : if (TYPE_UNSIGNED (ctype) && ctype != type)
6854 : : break;
6855 : :
6856 : : /* The last case is if we are a multiply. In that case, we can
6857 : : apply the distributive law to commute the multiply and addition
6858 : : if the multiplication of the constants doesn't overflow
6859 : : and overflow is defined. With undefined overflow
6860 : : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6861 : : But fold_plusminus_mult_expr would factor back any power-of-two
6862 : : value so do not distribute in the first place in this case. */
6863 : 2621612 : if (code == MULT_EXPR
6864 : 2249489 : && TYPE_OVERFLOW_WRAPS (ctype)
6865 : 4548440 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6866 : 455496 : return fold_build2 (tcode, ctype,
6867 : : fold_build2 (code, ctype,
6868 : : fold_convert (ctype, op0),
6869 : : fold_convert (ctype, c)),
6870 : : op1);
6871 : :
6872 : : break;
6873 : :
6874 : 2139170 : case MULT_EXPR:
6875 : : /* We have a special case here if we are doing something like
6876 : : (C * 8) % 4 since we know that's zero. */
6877 : 2139170 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6878 : 2139170 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6879 : : /* If the multiplication can overflow we cannot optimize this. */
6880 : 10564 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6881 : 326 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6882 : 2149734 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6883 : 281 : TYPE_SIGN (type)))
6884 : : {
6885 : 8 : *strict_overflow_p = true;
6886 : 8 : return omit_one_operand (type, integer_zero_node, op0);
6887 : : }
6888 : :
6889 : : /* ... fall through ... */
6890 : :
6891 : 2320731 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6892 : 2320731 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6893 : : /* If we can extract our operation from the LHS, do so and return a
6894 : : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6895 : : do something only if the second operand is a constant. */
6896 : 2320731 : if (same_p
6897 : 2006320 : && TYPE_OVERFLOW_WRAPS (ctype)
6898 : 4154485 : && (t1 = extract_muldiv (op0, c, code, wide_type,
6899 : : strict_overflow_p)) != 0)
6900 : 58739 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6901 : : fold_convert (ctype, op1));
6902 : 2261992 : else if (tcode == MULT_EXPR && code == MULT_EXPR
6903 : 1945253 : && TYPE_OVERFLOW_WRAPS (ctype)
6904 : 4034727 : && (t1 = extract_muldiv (op1, c, code, wide_type,
6905 : : strict_overflow_p)) != 0)
6906 : 929788 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6907 : : fold_convert (ctype, t1));
6908 : 1332204 : else if (TREE_CODE (op1) != INTEGER_CST)
6909 : : return 0;
6910 : :
6911 : : /* If these are the same operation types, we can associate them
6912 : : assuming no overflow. */
6913 : 505159 : if (tcode == code)
6914 : : {
6915 : 191238 : bool overflow_p = false;
6916 : 191238 : wi::overflow_type overflow_mul;
6917 : 191238 : signop sign = TYPE_SIGN (ctype);
6918 : 191238 : unsigned prec = TYPE_PRECISION (ctype);
6919 : 382476 : wide_int mul = wi::mul (wi::to_wide (op1, prec),
6920 : 191238 : wi::to_wide (c, prec),
6921 : 191238 : sign, &overflow_mul);
6922 : 191238 : overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6923 : 191238 : if (overflow_mul
6924 : 1279 : && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6925 : : overflow_p = true;
6926 : 191171 : if (!overflow_p)
6927 : 191171 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6928 : : wide_int_to_tree (ctype, mul));
6929 : 191238 : }
6930 : :
6931 : : /* If these operations "cancel" each other, we have the main
6932 : : optimizations of this pass, which occur when either constant is a
6933 : : multiple of the other, in which case we replace this with either an
6934 : : operation or CODE or TCODE.
6935 : :
6936 : : If we have an unsigned type, we cannot do this since it will change
6937 : : the result if the original computation overflowed. */
6938 : 313988 : if (TYPE_OVERFLOW_UNDEFINED (ctype)
6939 : 30362 : && !TYPE_OVERFLOW_SANITIZED (ctype)
6940 : 344307 : && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6941 : 30279 : || (tcode == MULT_EXPR
6942 : 30279 : && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6943 : 859 : && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6944 : 855 : && code != MULT_EXPR)))
6945 : : {
6946 : 889 : if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6947 : 889 : TYPE_SIGN (type)))
6948 : : {
6949 : 106 : *strict_overflow_p = true;
6950 : 106 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6951 : : fold_convert (ctype,
6952 : : const_binop (TRUNC_DIV_EXPR,
6953 : : op1, c)));
6954 : : }
6955 : 783 : else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6956 : 783 : TYPE_SIGN (type)))
6957 : : {
6958 : 64 : *strict_overflow_p = true;
6959 : 64 : return fold_build2 (code, ctype, fold_convert (ctype, op0),
6960 : : fold_convert (ctype,
6961 : : const_binop (TRUNC_DIV_EXPR,
6962 : : c, op1)));
6963 : : }
6964 : : }
6965 : : break;
6966 : :
6967 : : default:
6968 : : break;
6969 : : }
6970 : :
6971 : : return 0;
6972 : : }
6973 : :
6974 : : /* Return a node which has the indicated constant VALUE (either 0 or
6975 : : 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6976 : : and is of the indicated TYPE. */
6977 : :
6978 : : tree
6979 : 82964451 : constant_boolean_node (bool value, tree type)
6980 : : {
6981 : 82964451 : if (type == integer_type_node)
6982 : 19445054 : return value ? integer_one_node : integer_zero_node;
6983 : 63519397 : else if (type == boolean_type_node)
6984 : 60326296 : return value ? boolean_true_node : boolean_false_node;
6985 : 3193101 : else if (VECTOR_TYPE_P (type))
6986 : 584 : return build_vector_from_val (type,
6987 : 584 : build_int_cst (TREE_TYPE (type),
6988 : 923 : value ? -1 : 0));
6989 : : else
6990 : 3192517 : return fold_convert (type, value ? integer_one_node : integer_zero_node);
6991 : : }
6992 : :
6993 : :
6994 : : /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6995 : : Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6996 : : CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6997 : : expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6998 : : COND is the first argument to CODE; otherwise (as in the example
6999 : : given here), it is the second argument. TYPE is the type of the
7000 : : original expression. Return NULL_TREE if no simplification is
7001 : : possible. */
7002 : :
7003 : : static tree
7004 : 960381 : fold_binary_op_with_conditional_arg (location_t loc,
7005 : : enum tree_code code,
7006 : : tree type, tree op0, tree op1,
7007 : : tree cond, tree arg, int cond_first_p)
7008 : : {
7009 : 960381 : tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7010 : 960381 : tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7011 : 960381 : tree test, true_value, false_value;
7012 : 960381 : tree lhs = NULL_TREE;
7013 : 960381 : tree rhs = NULL_TREE;
7014 : 960381 : enum tree_code cond_code = COND_EXPR;
7015 : :
7016 : : /* Do not move possibly trapping operations into the conditional as this
7017 : : pessimizes code and causes gimplification issues when applied late. */
7018 : 980534 : if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7019 : 200677 : ANY_INTEGRAL_TYPE_P (type)
7020 : 962967 : && TYPE_OVERFLOW_TRAPS (type), op1))
7021 : : return NULL_TREE;
7022 : :
7023 : 940062 : if (TREE_CODE (cond) == COND_EXPR
7024 : 366543 : || TREE_CODE (cond) == VEC_COND_EXPR)
7025 : : {
7026 : 576072 : test = TREE_OPERAND (cond, 0);
7027 : 576072 : true_value = TREE_OPERAND (cond, 1);
7028 : 576072 : false_value = TREE_OPERAND (cond, 2);
7029 : : /* If this operand throws an expression, then it does not make
7030 : : sense to try to perform a logical or arithmetic operation
7031 : : involving it. */
7032 : 576072 : if (VOID_TYPE_P (TREE_TYPE (true_value)))
7033 : 7463 : lhs = true_value;
7034 : 576072 : if (VOID_TYPE_P (TREE_TYPE (false_value)))
7035 : 6 : rhs = false_value;
7036 : : }
7037 : 363990 : else if (!(TREE_CODE (type) != VECTOR_TYPE
7038 : 363966 : && VECTOR_TYPE_P (TREE_TYPE (cond))))
7039 : : {
7040 : 362425 : tree testtype = TREE_TYPE (cond);
7041 : 362425 : test = cond;
7042 : 362425 : true_value = constant_boolean_node (true, testtype);
7043 : 362425 : false_value = constant_boolean_node (false, testtype);
7044 : : }
7045 : : else
7046 : : /* Detect the case of mixing vector and scalar types - bail out. */
7047 : : return NULL_TREE;
7048 : :
7049 : 938497 : if (VECTOR_TYPE_P (TREE_TYPE (test)))
7050 : 2577 : cond_code = VEC_COND_EXPR;
7051 : :
7052 : : /* This transformation is only worthwhile if we don't have to wrap ARG
7053 : : in a SAVE_EXPR and the operation can be simplified without recursing
7054 : : on at least one of the branches once its pushed inside the COND_EXPR. */
7055 : 938497 : if (!TREE_CONSTANT (arg)
7056 : 938497 : && (TREE_SIDE_EFFECTS (arg)
7057 : 446744 : || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7058 : 442323 : || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7059 : : return NULL_TREE;
7060 : :
7061 : 506499 : arg = fold_convert_loc (loc, arg_type, arg);
7062 : 506499 : if (lhs == 0)
7063 : : {
7064 : 500468 : true_value = fold_convert_loc (loc, cond_type, true_value);
7065 : 500468 : if (cond_first_p)
7066 : 490299 : lhs = fold_build2_loc (loc, code, type, true_value, arg);
7067 : : else
7068 : 10169 : lhs = fold_build2_loc (loc, code, type, arg, true_value);
7069 : : }
7070 : 506499 : if (rhs == 0)
7071 : : {
7072 : 506493 : false_value = fold_convert_loc (loc, cond_type, false_value);
7073 : 506493 : if (cond_first_p)
7074 : 495763 : rhs = fold_build2_loc (loc, code, type, false_value, arg);
7075 : : else
7076 : 10730 : rhs = fold_build2_loc (loc, code, type, arg, false_value);
7077 : : }
7078 : :
7079 : : /* Check that we have simplified at least one of the branches. */
7080 : 506499 : if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7081 : : return NULL_TREE;
7082 : :
7083 : 487644 : return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7084 : : }
7085 : :
7086 : :
7087 : : /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7088 : :
7089 : : If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7090 : : type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7091 : : if ARG - ZERO_ARG is the same as X.
7092 : :
7093 : : If ARG is NULL, check for any value of type TYPE.
7094 : :
7095 : : X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7096 : : and finite. The problematic cases are when X is zero, and its mode
7097 : : has signed zeros. In the case of rounding towards -infinity,
7098 : : X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7099 : : modes, X + 0 is not the same as X because -0 + 0 is 0. */
7100 : :
7101 : : bool
7102 : 645546 : fold_real_zero_addition_p (const_tree type, const_tree arg,
7103 : : const_tree zero_arg, int negate)
7104 : : {
7105 : 645546 : if (!real_zerop (zero_arg))
7106 : : return false;
7107 : :
7108 : : /* Don't allow the fold with -fsignaling-nans. */
7109 : 644916 : if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7110 : : return false;
7111 : :
7112 : : /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7113 : 641578 : if (!HONOR_SIGNED_ZEROS (type))
7114 : : return true;
7115 : :
7116 : : /* There is no case that is safe for all rounding modes. */
7117 : 625145 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7118 : : return false;
7119 : :
7120 : : /* In a vector or complex, we would need to check the sign of all zeros. */
7121 : 624482 : if (TREE_CODE (zero_arg) == VECTOR_CST)
7122 : 1450 : zero_arg = uniform_vector_p (zero_arg);
7123 : 624482 : if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7124 : 1178 : return false;
7125 : :
7126 : : /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7127 : 623304 : if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7128 : 229 : negate = !negate;
7129 : :
7130 : : /* The mode has signed zeros, and we have to honor their sign.
7131 : : In this situation, there are only two cases we can return true for.
7132 : : (i) X - 0 is the same as X with default rounding.
7133 : : (ii) X + 0 is X when X can't possibly be -0.0. */
7134 : 623304 : return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7135 : : }
7136 : :
7137 : : /* Subroutine of match.pd that optimizes comparisons of a division by
7138 : : a nonzero integer constant against an integer constant, i.e.
7139 : : X/C1 op C2.
7140 : :
7141 : : CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7142 : : GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7143 : :
7144 : : enum tree_code
7145 : 1748034 : fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7146 : : tree *hi, bool *neg_overflow)
7147 : : {
7148 : 1748034 : tree prod, tmp, type = TREE_TYPE (c1);
7149 : 1748034 : signop sign = TYPE_SIGN (type);
7150 : 1748034 : wi::overflow_type overflow;
7151 : :
7152 : : /* We have to do this the hard way to detect unsigned overflow.
7153 : : prod = int_const_binop (MULT_EXPR, c1, c2); */
7154 : 1748034 : wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7155 : 1748034 : prod = force_fit_type (type, val, -1, overflow);
7156 : 1748034 : *neg_overflow = false;
7157 : :
7158 : 1748034 : if (sign == UNSIGNED)
7159 : : {
7160 : 1716450 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7161 : 1716450 : *lo = prod;
7162 : :
7163 : : /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7164 : 1716450 : val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7165 : 1716450 : *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7166 : : }
7167 : 31584 : else if (tree_int_cst_sgn (c1) >= 0)
7168 : : {
7169 : 30176 : tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7170 : 30176 : switch (tree_int_cst_sgn (c2))
7171 : : {
7172 : 5454 : case -1:
7173 : 5454 : *neg_overflow = true;
7174 : 5454 : *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7175 : 5454 : *hi = prod;
7176 : 5454 : break;
7177 : :
7178 : 14028 : case 0:
7179 : 14028 : *lo = fold_negate_const (tmp, type);
7180 : 14028 : *hi = tmp;
7181 : 14028 : break;
7182 : :
7183 : 10694 : case 1:
7184 : 10694 : *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7185 : 10694 : *lo = prod;
7186 : 10694 : break;
7187 : :
7188 : 0 : default:
7189 : 0 : gcc_unreachable ();
7190 : : }
7191 : : }
7192 : : else
7193 : : {
7194 : : /* A negative divisor reverses the relational operators. */
7195 : 1408 : code = swap_tree_comparison (code);
7196 : :
7197 : 1408 : tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7198 : 1408 : switch (tree_int_cst_sgn (c2))
7199 : : {
7200 : 132 : case -1:
7201 : 132 : *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7202 : 132 : *lo = prod;
7203 : 132 : break;
7204 : :
7205 : 178 : case 0:
7206 : 178 : *hi = fold_negate_const (tmp, type);
7207 : 178 : *lo = tmp;
7208 : 178 : break;
7209 : :
7210 : 1098 : case 1:
7211 : 1098 : *neg_overflow = true;
7212 : 1098 : *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7213 : 1098 : *hi = prod;
7214 : 1098 : break;
7215 : :
7216 : 0 : default:
7217 : 0 : gcc_unreachable ();
7218 : : }
7219 : : }
7220 : :
7221 : 1748034 : if (code != EQ_EXPR && code != NE_EXPR)
7222 : : return code;
7223 : :
7224 : 18153 : if (TREE_OVERFLOW (*lo)
7225 : 18153 : || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7226 : 727 : *lo = NULL_TREE;
7227 : 18153 : if (TREE_OVERFLOW (*hi)
7228 : 18153 : || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7229 : 92 : *hi = NULL_TREE;
7230 : :
7231 : : return code;
7232 : 1748034 : }
7233 : :
7234 : : /* Test whether it is preferable to swap two operands, ARG0 and
7235 : : ARG1, for example because ARG0 is an integer constant and ARG1
7236 : : isn't. */
7237 : :
7238 : : bool
7239 : 1519839924 : tree_swap_operands_p (const_tree arg0, const_tree arg1)
7240 : : {
7241 : 1519839924 : if (CONSTANT_CLASS_P (arg1))
7242 : : return false;
7243 : 487533602 : if (CONSTANT_CLASS_P (arg0))
7244 : : return true;
7245 : :
7246 : 448065918 : STRIP_NOPS (arg0);
7247 : 448065918 : STRIP_NOPS (arg1);
7248 : :
7249 : 448065918 : if (TREE_CONSTANT (arg1))
7250 : : return false;
7251 : 435704844 : if (TREE_CONSTANT (arg0))
7252 : : return true;
7253 : :
7254 : : /* Put addresses in arg1. */
7255 : 435189839 : if (TREE_CODE (arg1) == ADDR_EXPR)
7256 : : return false;
7257 : 422420719 : if (TREE_CODE (arg0) == ADDR_EXPR)
7258 : : return true;
7259 : :
7260 : : /* It is preferable to swap two SSA_NAME to ensure a canonical form
7261 : : for commutative and comparison operators. Ensuring a canonical
7262 : : form allows the optimizers to find additional redundancies without
7263 : : having to explicitly check for both orderings. */
7264 : 422038690 : if (TREE_CODE (arg0) == SSA_NAME
7265 : 318643987 : && TREE_CODE (arg1) == SSA_NAME
7266 : 734911016 : && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7267 : : return true;
7268 : :
7269 : : /* Put SSA_NAMEs last. */
7270 : 397718564 : if (TREE_CODE (arg1) == SSA_NAME)
7271 : : return false;
7272 : 94909372 : if (TREE_CODE (arg0) == SSA_NAME)
7273 : : return true;
7274 : :
7275 : : /* Put variables last. */
7276 : 89137711 : if (DECL_P (arg1))
7277 : : return false;
7278 : 47624372 : if (DECL_P (arg0))
7279 : : return true;
7280 : :
7281 : : return false;
7282 : : }
7283 : :
7284 : :
7285 : : /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7286 : : means A >= Y && A != MAX, but in this case we know that
7287 : : A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7288 : :
7289 : : static tree
7290 : 22774581 : fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7291 : : {
7292 : 22774581 : tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7293 : :
7294 : 22774581 : if (TREE_CODE (bound) == LT_EXPR)
7295 : 4790279 : a = TREE_OPERAND (bound, 0);
7296 : 17984302 : else if (TREE_CODE (bound) == GT_EXPR)
7297 : 2541459 : a = TREE_OPERAND (bound, 1);
7298 : : else
7299 : : return NULL_TREE;
7300 : :
7301 : 7331738 : typea = TREE_TYPE (a);
7302 : 7331738 : if (!INTEGRAL_TYPE_P (typea)
7303 : 345372 : && !POINTER_TYPE_P (typea))
7304 : : return NULL_TREE;
7305 : :
7306 : 7149422 : if (TREE_CODE (ineq) == LT_EXPR)
7307 : : {
7308 : 1395070 : a1 = TREE_OPERAND (ineq, 1);
7309 : 1395070 : y = TREE_OPERAND (ineq, 0);
7310 : : }
7311 : 5754352 : else if (TREE_CODE (ineq) == GT_EXPR)
7312 : : {
7313 : 1044469 : a1 = TREE_OPERAND (ineq, 0);
7314 : 1044469 : y = TREE_OPERAND (ineq, 1);
7315 : : }
7316 : : else
7317 : : return NULL_TREE;
7318 : :
7319 : 2439539 : if (TREE_TYPE (a1) != typea)
7320 : : return NULL_TREE;
7321 : :
7322 : 1697328 : if (POINTER_TYPE_P (typea))
7323 : : {
7324 : : /* Convert the pointer types into integer before taking the difference. */
7325 : 8132 : tree ta = fold_convert_loc (loc, ssizetype, a);
7326 : 8132 : tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7327 : 8132 : diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7328 : : }
7329 : : else
7330 : 1689196 : diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7331 : :
7332 : 1697328 : if (!diff || !integer_onep (diff))
7333 : 1686863 : return NULL_TREE;
7334 : :
7335 : 10465 : return fold_build2_loc (loc, GE_EXPR, type, a, y);
7336 : : }
7337 : :
7338 : : /* Fold a sum or difference of at least one multiplication.
7339 : : Returns the folded tree or NULL if no simplification could be made. */
7340 : :
7341 : : static tree
7342 : 8457169 : fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7343 : : tree arg0, tree arg1)
7344 : : {
7345 : 8457169 : tree arg00, arg01, arg10, arg11;
7346 : 8457169 : tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7347 : :
7348 : : /* (A * C) +- (B * C) -> (A+-B) * C.
7349 : : (A * C) +- A -> A * (C+-1).
7350 : : We are most concerned about the case where C is a constant,
7351 : : but other combinations show up during loop reduction. Since
7352 : : it is not difficult, try all four possibilities. */
7353 : :
7354 : 8457169 : if (TREE_CODE (arg0) == MULT_EXPR)
7355 : : {
7356 : 7475557 : arg00 = TREE_OPERAND (arg0, 0);
7357 : 7475557 : arg01 = TREE_OPERAND (arg0, 1);
7358 : : }
7359 : 981612 : else if (TREE_CODE (arg0) == INTEGER_CST)
7360 : : {
7361 : 69718 : arg00 = build_one_cst (type);
7362 : 69718 : arg01 = arg0;
7363 : : }
7364 : : else
7365 : : {
7366 : : /* We cannot generate constant 1 for fract. */
7367 : 911894 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7368 : 0 : return NULL_TREE;
7369 : 911894 : arg00 = arg0;
7370 : 911894 : arg01 = build_one_cst (type);
7371 : : }
7372 : 8457169 : if (TREE_CODE (arg1) == MULT_EXPR)
7373 : : {
7374 : 2308682 : arg10 = TREE_OPERAND (arg1, 0);
7375 : 2308682 : arg11 = TREE_OPERAND (arg1, 1);
7376 : : }
7377 : 6148487 : else if (TREE_CODE (arg1) == INTEGER_CST)
7378 : : {
7379 : 3251301 : arg10 = build_one_cst (type);
7380 : : /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7381 : : the purpose of this canonicalization. */
7382 : 6271665 : if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7383 : 234225 : && negate_expr_p (arg1)
7384 : 3482238 : && code == PLUS_EXPR)
7385 : : {
7386 : 230937 : arg11 = negate_expr (arg1);
7387 : 230937 : code = MINUS_EXPR;
7388 : : }
7389 : : else
7390 : : arg11 = arg1;
7391 : : }
7392 : : else
7393 : : {
7394 : : /* We cannot generate constant 1 for fract. */
7395 : 2897186 : if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7396 : 0 : return NULL_TREE;
7397 : 2897186 : arg10 = arg1;
7398 : 2897186 : arg11 = build_one_cst (type);
7399 : : }
7400 : 8457169 : same = NULL_TREE;
7401 : :
7402 : : /* Prefer factoring a common non-constant. */
7403 : 8457169 : if (operand_equal_p (arg00, arg10, 0))
7404 : : same = arg00, alt0 = arg01, alt1 = arg11;
7405 : 8453527 : else if (operand_equal_p (arg01, arg11, 0))
7406 : : same = arg01, alt0 = arg00, alt1 = arg10;
7407 : 8382749 : else if (operand_equal_p (arg00, arg11, 0))
7408 : : same = arg00, alt0 = arg01, alt1 = arg10;
7409 : 8382673 : else if (operand_equal_p (arg01, arg10, 0))
7410 : : same = arg01, alt0 = arg00, alt1 = arg11;
7411 : :
7412 : : /* No identical multiplicands; see if we can find a common
7413 : : power-of-two factor in non-power-of-two multiplies. This
7414 : : can help in multi-dimensional array access. */
7415 : 8381279 : else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7416 : : {
7417 : 7038632 : HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7418 : 7038632 : HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7419 : 7038632 : HOST_WIDE_INT tmp;
7420 : 7038632 : bool swap = false;
7421 : 7038632 : tree maybe_same;
7422 : :
7423 : : /* Move min of absolute values to int11. */
7424 : 7038632 : if (absu_hwi (int01) < absu_hwi (int11))
7425 : : {
7426 : : tmp = int01, int01 = int11, int11 = tmp;
7427 : : alt0 = arg00, arg00 = arg10, arg10 = alt0;
7428 : : maybe_same = arg01;
7429 : : swap = true;
7430 : : }
7431 : : else
7432 : 3570568 : maybe_same = arg11;
7433 : :
7434 : 7038632 : const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7435 : 7038632 : if (factor > 1
7436 : 9266615 : && pow2p_hwi (factor)
7437 : 2031879 : && (int01 & (factor - 1)) == 0
7438 : : /* The remainder should not be a constant, otherwise we
7439 : : end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7440 : : increased the number of multiplications necessary. */
7441 : 8309157 : && TREE_CODE (arg10) != INTEGER_CST)
7442 : : {
7443 : 1146543 : alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7444 : 1146543 : build_int_cst (TREE_TYPE (arg00),
7445 : 1146543 : int01 / int11));
7446 : 1146543 : alt1 = arg10;
7447 : 1146543 : same = maybe_same;
7448 : 1146543 : if (swap)
7449 : 1035988 : maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7450 : : }
7451 : : }
7452 : :
7453 : 1222433 : if (!same)
7454 : 7234736 : return NULL_TREE;
7455 : :
7456 : 7 : if (! ANY_INTEGRAL_TYPE_P (type)
7457 : 1222433 : || TYPE_OVERFLOW_WRAPS (type)
7458 : : /* We are neither factoring zero nor minus one. */
7459 : 1345712 : || TREE_CODE (same) == INTEGER_CST)
7460 : 1211055 : return fold_build2_loc (loc, MULT_EXPR, type,
7461 : : fold_build2_loc (loc, code, type,
7462 : : fold_convert_loc (loc, type, alt0),
7463 : : fold_convert_loc (loc, type, alt1)),
7464 : 1211055 : fold_convert_loc (loc, type, same));
7465 : :
7466 : : /* Same may be zero and thus the operation 'code' may overflow. Likewise
7467 : : same may be minus one and thus the multiplication may overflow. Perform
7468 : : the sum operation in an unsigned type. */
7469 : 11378 : tree utype = unsigned_type_for (type);
7470 : 11378 : tree tem = fold_build2_loc (loc, code, utype,
7471 : : fold_convert_loc (loc, utype, alt0),
7472 : : fold_convert_loc (loc, utype, alt1));
7473 : : /* If the sum evaluated to a constant that is not -INF the multiplication
7474 : : cannot overflow. */
7475 : 22756 : if (TREE_CODE (tem) == INTEGER_CST
7476 : 17898 : && (wi::to_wide (tem)
7477 : 17898 : != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7478 : 3247 : return fold_build2_loc (loc, MULT_EXPR, type,
7479 : 3247 : fold_convert (type, tem), same);
7480 : :
7481 : : /* Do not resort to unsigned multiplication because
7482 : : we lose the no-overflow property of the expression. */
7483 : : return NULL_TREE;
7484 : : }
7485 : :
7486 : :
7487 : : /* Subroutine of native_encode_int. Encode the integer VAL with type TYPE
7488 : : into the buffer PTR of length LEN bytes.
7489 : : Return the number of bytes placed in the buffer, or zero
7490 : : upon failure. */
7491 : :
7492 : : int
7493 : 19702454 : native_encode_wide_int (tree type, const wide_int_ref &val,
7494 : : unsigned char *ptr, int len, int off)
7495 : : {
7496 : 19702454 : int total_bytes;
7497 : 19702454 : if (TREE_CODE (type) == BITINT_TYPE)
7498 : : {
7499 : 17148 : struct bitint_info info;
7500 : 17148 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
7501 : 17148 : gcc_assert (ok);
7502 : 17148 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
7503 : 17148 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
7504 : : {
7505 : 17028 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7506 : : /* More work is needed when adding _BitInt support to PDP endian
7507 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
7508 : : match target endianity here. */
7509 : 17028 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
7510 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
7511 : : || (GET_MODE_SIZE (limb_mode)
7512 : : >= UNITS_PER_WORD)));
7513 : : }
7514 : : else
7515 : 240 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7516 : : }
7517 : : else
7518 : 39370612 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7519 : 19702454 : int byte, offset, word, words;
7520 : 19702454 : unsigned char value;
7521 : :
7522 : 19702454 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7523 : : return 0;
7524 : 19701971 : if (off == -1)
7525 : 18604624 : off = 0;
7526 : :
7527 : 19701971 : if (ptr == NULL)
7528 : : /* Dry run. */
7529 : 2728105 : return MIN (len, total_bytes - off);
7530 : :
7531 : : words = total_bytes / UNITS_PER_WORD;
7532 : :
7533 : 83148045 : for (byte = 0; byte < total_bytes; byte++)
7534 : : {
7535 : 66174179 : int bitpos = byte * BITS_PER_UNIT;
7536 : : /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7537 : : number of bytes. */
7538 : 66174179 : value = wi::extract_uhwi (val, bitpos, BITS_PER_UNIT);
7539 : :
7540 : 66174179 : if (total_bytes > UNITS_PER_WORD)
7541 : : {
7542 : 66174179 : word = byte / UNITS_PER_WORD;
7543 : 66174179 : if (WORDS_BIG_ENDIAN)
7544 : : word = (words - 1) - word;
7545 : 66174179 : offset = word * UNITS_PER_WORD;
7546 : 66174179 : if (BYTES_BIG_ENDIAN)
7547 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7548 : : else
7549 : 66174179 : offset += byte % UNITS_PER_WORD;
7550 : : }
7551 : : else
7552 : : offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7553 : 66174179 : if (offset >= off && offset - off < len)
7554 : 64887852 : ptr[offset - off] = value;
7555 : : }
7556 : 16973866 : return MIN (len, total_bytes - off);
7557 : : }
7558 : :
7559 : : /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7560 : : specified by EXPR into the buffer PTR of length LEN bytes.
7561 : : Return the number of bytes placed in the buffer, or zero
7562 : : upon failure. */
7563 : :
7564 : : static int
7565 : 19702454 : native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7566 : : {
7567 : 19702454 : return native_encode_wide_int (TREE_TYPE (expr), wi::to_widest (expr),
7568 : 19702454 : ptr, len, off);
7569 : : }
7570 : :
7571 : :
7572 : : /* Subroutine of native_encode_expr. Encode the FIXED_CST
7573 : : specified by EXPR into the buffer PTR of length LEN bytes.
7574 : : Return the number of bytes placed in the buffer, or zero
7575 : : upon failure. */
7576 : :
7577 : : static int
7578 : 0 : native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7579 : : {
7580 : 0 : tree type = TREE_TYPE (expr);
7581 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
7582 : 0 : int total_bytes = GET_MODE_SIZE (mode);
7583 : 0 : FIXED_VALUE_TYPE value;
7584 : 0 : tree i_value, i_type;
7585 : :
7586 : 0 : if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7587 : : return 0;
7588 : :
7589 : 0 : i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7590 : :
7591 : 0 : if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7592 : : return 0;
7593 : :
7594 : 0 : value = TREE_FIXED_CST (expr);
7595 : 0 : i_value = double_int_to_tree (i_type, value.data);
7596 : :
7597 : 0 : return native_encode_int (i_value, ptr, len, off);
7598 : : }
7599 : :
7600 : :
7601 : : /* Subroutine of native_encode_expr. Encode the REAL_CST
7602 : : specified by EXPR into the buffer PTR of length LEN bytes.
7603 : : Return the number of bytes placed in the buffer, or zero
7604 : : upon failure. */
7605 : :
7606 : : int
7607 : 567354 : native_encode_real (scalar_float_mode mode, const REAL_VALUE_TYPE *val,
7608 : : unsigned char *ptr, int len, int off)
7609 : : {
7610 : 567354 : int total_bytes = GET_MODE_SIZE (mode);
7611 : 567354 : int byte, offset, word, words, bitpos;
7612 : 567354 : unsigned char value;
7613 : :
7614 : : /* There are always 32 bits in each long, no matter the size of
7615 : : the hosts long. We handle floating point representations with
7616 : : up to 192 bits. */
7617 : 567354 : long tmp[6];
7618 : :
7619 : 567354 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7620 : : return 0;
7621 : 565529 : if (off == -1)
7622 : 434591 : off = 0;
7623 : :
7624 : 565529 : if (ptr == NULL)
7625 : : /* Dry run. */
7626 : 138113 : return MIN (len, total_bytes - off);
7627 : :
7628 : 427416 : words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7629 : :
7630 : 427416 : real_to_target (tmp, val, mode);
7631 : :
7632 : 3490148 : for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7633 : 3062732 : bitpos += BITS_PER_UNIT)
7634 : : {
7635 : 3062732 : byte = (bitpos / BITS_PER_UNIT) & 3;
7636 : 3062732 : value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7637 : :
7638 : 3062732 : if (UNITS_PER_WORD < 4)
7639 : : {
7640 : : word = byte / UNITS_PER_WORD;
7641 : : if (WORDS_BIG_ENDIAN)
7642 : : word = (words - 1) - word;
7643 : : offset = word * UNITS_PER_WORD;
7644 : : if (BYTES_BIG_ENDIAN)
7645 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7646 : : else
7647 : : offset += byte % UNITS_PER_WORD;
7648 : : }
7649 : : else
7650 : : {
7651 : 3062732 : offset = byte;
7652 : 3062732 : if (BYTES_BIG_ENDIAN)
7653 : : {
7654 : : /* Reverse bytes within each long, or within the entire float
7655 : : if it's smaller than a long (for HFmode). */
7656 : : offset = MIN (3, total_bytes - 1) - offset;
7657 : : gcc_assert (offset >= 0);
7658 : : }
7659 : : }
7660 : 3062732 : offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7661 : 3062732 : if (offset >= off
7662 : 3059416 : && offset - off < len)
7663 : 3041924 : ptr[offset - off] = value;
7664 : : }
7665 : 427416 : return MIN (len, total_bytes - off);
7666 : : }
7667 : :
7668 : : /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7669 : : specified by EXPR into the buffer PTR of length LEN bytes.
7670 : : Return the number of bytes placed in the buffer, or zero
7671 : : upon failure. */
7672 : :
7673 : : static int
7674 : 20121 : native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7675 : : {
7676 : 20121 : int rsize, isize;
7677 : 20121 : tree part;
7678 : :
7679 : 20121 : part = TREE_REALPART (expr);
7680 : 20121 : rsize = native_encode_expr (part, ptr, len, off);
7681 : 20121 : if (off == -1 && rsize == 0)
7682 : : return 0;
7683 : 20121 : part = TREE_IMAGPART (expr);
7684 : 20121 : if (off != -1)
7685 : 39924 : off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7686 : 20121 : isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7687 : : len - rsize, off);
7688 : 20121 : if (off == -1 && isize != rsize)
7689 : : return 0;
7690 : 20121 : return rsize + isize;
7691 : : }
7692 : :
7693 : : /* Like native_encode_vector, but only encode the first COUNT elements.
7694 : : The other arguments are as for native_encode_vector. */
7695 : :
7696 : : static int
7697 : 863663 : native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7698 : : int off, unsigned HOST_WIDE_INT count)
7699 : : {
7700 : 863663 : tree itype = TREE_TYPE (TREE_TYPE (expr));
7701 : 1727326 : if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7702 : 863983 : && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7703 : : {
7704 : : /* This is the only case in which elements can be smaller than a byte.
7705 : : Element 0 is always in the lsb of the containing byte. */
7706 : 244 : unsigned int elt_bits = TYPE_PRECISION (itype);
7707 : 244 : int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7708 : 244 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7709 : : return 0;
7710 : :
7711 : 244 : if (off == -1)
7712 : 244 : off = 0;
7713 : :
7714 : : /* Zero the buffer and then set bits later where necessary. */
7715 : 244 : int extract_bytes = MIN (len, total_bytes - off);
7716 : 244 : if (ptr)
7717 : 244 : memset (ptr, 0, extract_bytes);
7718 : :
7719 : 244 : unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7720 : 244 : unsigned int first_elt = off * elts_per_byte;
7721 : 244 : unsigned int extract_elts = extract_bytes * elts_per_byte;
7722 : 244 : unsigned int elt_mask = (1 << elt_bits) - 1;
7723 : 2869 : for (unsigned int i = 0; i < extract_elts; ++i)
7724 : : {
7725 : 2625 : tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7726 : 2625 : if (TREE_CODE (elt) != INTEGER_CST)
7727 : : return 0;
7728 : :
7729 : 2625 : if (ptr && integer_nonzerop (elt))
7730 : : {
7731 : 913 : unsigned int bit = i * elt_bits;
7732 : 913 : ptr[bit / BITS_PER_UNIT] |= elt_mask << (bit % BITS_PER_UNIT);
7733 : : }
7734 : : }
7735 : : return extract_bytes;
7736 : : }
7737 : :
7738 : 863419 : int offset = 0;
7739 : 863419 : int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7740 : 2766749 : for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7741 : : {
7742 : 2433557 : if (off >= size)
7743 : : {
7744 : 23041 : off -= size;
7745 : 23041 : continue;
7746 : : }
7747 : 2410516 : tree elem = VECTOR_CST_ELT (expr, i);
7748 : 2410516 : int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7749 : : len - offset, off);
7750 : 2410516 : if ((off == -1 && res != size) || res == 0)
7751 : : return 0;
7752 : 2409992 : offset += res;
7753 : 2409992 : if (offset >= len)
7754 : 529703 : return (off == -1 && i < count - 1) ? 0 : offset;
7755 : 1880289 : if (off != -1)
7756 : 332653 : off = 0;
7757 : : }
7758 : : return offset;
7759 : : }
7760 : :
7761 : : /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7762 : : specified by EXPR into the buffer PTR of length LEN bytes.
7763 : : Return the number of bytes placed in the buffer, or zero
7764 : : upon failure. */
7765 : :
7766 : : static int
7767 : 760043 : native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7768 : : {
7769 : 760043 : unsigned HOST_WIDE_INT count;
7770 : 760043 : if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7771 : : return 0;
7772 : 760043 : return native_encode_vector_part (expr, ptr, len, off, count);
7773 : : }
7774 : :
7775 : :
7776 : : /* Subroutine of native_encode_expr. Encode the STRING_CST
7777 : : specified by EXPR into the buffer PTR of length LEN bytes.
7778 : : Return the number of bytes placed in the buffer, or zero
7779 : : upon failure. */
7780 : :
7781 : : static int
7782 : 133010 : native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7783 : : {
7784 : 133010 : tree type = TREE_TYPE (expr);
7785 : :
7786 : : /* Wide-char strings are encoded in target byte-order so native
7787 : : encoding them is trivial. */
7788 : 133010 : if (BITS_PER_UNIT != CHAR_BIT
7789 : 133010 : || TREE_CODE (type) != ARRAY_TYPE
7790 : 133010 : || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7791 : 266020 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7792 : : return 0;
7793 : :
7794 : 133010 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7795 : 133010 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7796 : : return 0;
7797 : 132152 : if (off == -1)
7798 : 56458 : off = 0;
7799 : 132152 : len = MIN (total_bytes - off, len);
7800 : 132152 : if (ptr == NULL)
7801 : : /* Dry run. */;
7802 : : else
7803 : : {
7804 : 132152 : int written = 0;
7805 : 132152 : if (off < TREE_STRING_LENGTH (expr))
7806 : : {
7807 : 131671 : written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7808 : 131671 : memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7809 : : }
7810 : 132152 : memset (ptr + written, 0, len - written);
7811 : : }
7812 : : return len;
7813 : : }
7814 : :
7815 : : /* Subroutine of native_encode_expr. Encode the CONSTRUCTOR
7816 : : specified by EXPR into the buffer PTR of length LEN bytes.
7817 : : Return the number of bytes placed in the buffer, or zero
7818 : : upon failure. */
7819 : :
7820 : : static int
7821 : 45811 : native_encode_constructor (const_tree expr, unsigned char *ptr, int len, int off)
7822 : : {
7823 : : /* We are only concerned with zero-initialization constructors here. That's
7824 : : all we expect to see in GIMPLE, so that's all native_encode_expr should
7825 : : deal with. For more general handling of constructors, there is
7826 : : native_encode_initializer. */
7827 : 45811 : if (CONSTRUCTOR_NELTS (expr))
7828 : : return 0;
7829 : :
7830 : : /* Wide-char strings are encoded in target byte-order so native
7831 : : encoding them is trivial. */
7832 : 85936 : if (BITS_PER_UNIT != CHAR_BIT
7833 : 42968 : || !tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (expr))))
7834 : : return 0;
7835 : :
7836 : 42968 : HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7837 : 42968 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7838 : : return 0;
7839 : 42968 : if (off == -1)
7840 : 0 : off = 0;
7841 : 42968 : len = MIN (total_bytes - off, len);
7842 : 42968 : if (ptr == NULL)
7843 : : /* Dry run. */;
7844 : : else
7845 : 42968 : memset (ptr, 0, len);
7846 : : return len;
7847 : : }
7848 : :
7849 : : /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
7850 : : FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
7851 : : the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
7852 : : anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
7853 : : sufficient to encode the entire EXPR, or if OFF is out of bounds.
7854 : : Otherwise, start at byte offset OFF and encode at most LEN bytes.
7855 : : Return the number of bytes placed in the buffer, or zero upon failure. */
7856 : :
7857 : : int
7858 : 33813935 : native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7859 : : {
7860 : : /* We don't support starting at negative offset and -1 is special. */
7861 : 33813935 : if (off < -1)
7862 : : return 0;
7863 : :
7864 : 33813923 : switch (TREE_CODE (expr))
7865 : : {
7866 : 19700268 : case INTEGER_CST:
7867 : 19700268 : return native_encode_int (expr, ptr, len, off);
7868 : :
7869 : 567354 : case REAL_CST:
7870 : 567354 : return native_encode_real (SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (expr)),
7871 : 1134708 : TREE_REAL_CST_PTR (expr), ptr, len, off);
7872 : :
7873 : 0 : case FIXED_CST:
7874 : 0 : return native_encode_fixed (expr, ptr, len, off);
7875 : :
7876 : 20121 : case COMPLEX_CST:
7877 : 20121 : return native_encode_complex (expr, ptr, len, off);
7878 : :
7879 : 760043 : case VECTOR_CST:
7880 : 760043 : return native_encode_vector (expr, ptr, len, off);
7881 : :
7882 : 133010 : case STRING_CST:
7883 : 133010 : return native_encode_string (expr, ptr, len, off);
7884 : :
7885 : 45811 : case CONSTRUCTOR:
7886 : 45811 : return native_encode_constructor (expr, ptr, len, off);
7887 : :
7888 : : default:
7889 : : return 0;
7890 : : }
7891 : : }
7892 : :
7893 : : /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
7894 : : or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
7895 : : of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
7896 : : machine modes, we can't just use build_nonstandard_integer_type. */
7897 : :
7898 : : tree
7899 : 541 : find_bitfield_repr_type (int fieldsize, int len)
7900 : : {
7901 : 541 : machine_mode mode;
7902 : 1063 : for (int pass = 0; pass < 2; pass++)
7903 : : {
7904 : 802 : enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
7905 : 4510 : FOR_EACH_MODE_IN_CLASS (mode, mclass)
7906 : 7976 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7907 : 7286 : && known_eq (GET_MODE_PRECISION (mode),
7908 : : GET_MODE_BITSIZE (mode))
7909 : 11274 : && known_le (GET_MODE_SIZE (mode), len))
7910 : : {
7911 : 280 : tree ret = lang_hooks.types.type_for_mode (mode, 1);
7912 : 280 : if (ret && TYPE_MODE (ret) == mode)
7913 : : return ret;
7914 : : }
7915 : : }
7916 : :
7917 : 522 : for (int i = 0; i < NUM_INT_N_ENTS; i ++)
7918 : 261 : if (int_n_enabled_p[i]
7919 : 261 : && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
7920 : 261 : && int_n_trees[i].unsigned_type)
7921 : : {
7922 : 261 : tree ret = int_n_trees[i].unsigned_type;
7923 : 261 : mode = TYPE_MODE (ret);
7924 : 522 : if (known_ge (GET_MODE_SIZE (mode), fieldsize)
7925 : 522 : && known_eq (GET_MODE_PRECISION (mode),
7926 : : GET_MODE_BITSIZE (mode))
7927 : 783 : && known_le (GET_MODE_SIZE (mode), len))
7928 : : return ret;
7929 : : }
7930 : :
7931 : : return NULL_TREE;
7932 : : }
7933 : :
7934 : : /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
7935 : : NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
7936 : : to be non-NULL and OFF zero), then in addition to filling the
7937 : : bytes pointed by PTR with the value also clear any bits pointed
7938 : : by MASK that are known to be initialized, keep them as is for
7939 : : e.g. uninitialized padding bits or uninitialized fields. */
7940 : :
7941 : : int
7942 : 12357234 : native_encode_initializer (tree init, unsigned char *ptr, int len,
7943 : : int off, unsigned char *mask)
7944 : : {
7945 : 12357234 : int r;
7946 : :
7947 : : /* We don't support starting at negative offset and -1 is special. */
7948 : 12357234 : if (off < -1 || init == NULL_TREE)
7949 : : return 0;
7950 : :
7951 : 12357234 : gcc_assert (mask == NULL || (off == 0 && ptr));
7952 : :
7953 : 12357234 : STRIP_NOPS (init);
7954 : 12357234 : switch (TREE_CODE (init))
7955 : : {
7956 : 0 : case VIEW_CONVERT_EXPR:
7957 : 0 : case NON_LVALUE_EXPR:
7958 : 0 : return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
7959 : 0 : mask);
7960 : 11453069 : default:
7961 : 11453069 : r = native_encode_expr (init, ptr, len, off);
7962 : 11453069 : if (mask)
7963 : 1502 : memset (mask, 0, r);
7964 : : return r;
7965 : 904165 : case CONSTRUCTOR:
7966 : 904165 : tree type = TREE_TYPE (init);
7967 : 904165 : HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
7968 : 904165 : if (total_bytes < 0)
7969 : : return 0;
7970 : 904165 : if ((off == -1 && total_bytes > len) || off >= total_bytes)
7971 : : return 0;
7972 : 904165 : int o = off == -1 ? 0 : off;
7973 : 904165 : if (TREE_CODE (type) == ARRAY_TYPE)
7974 : : {
7975 : 313337 : tree min_index;
7976 : 313337 : unsigned HOST_WIDE_INT cnt;
7977 : 313337 : HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
7978 : 313337 : constructor_elt *ce;
7979 : :
7980 : 313337 : if (!TYPE_DOMAIN (type)
7981 : 313337 : || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
7982 : : return 0;
7983 : :
7984 : 313337 : fieldsize = int_size_in_bytes (TREE_TYPE (type));
7985 : 313337 : if (fieldsize <= 0)
7986 : : return 0;
7987 : :
7988 : 313337 : min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
7989 : 313337 : if (ptr)
7990 : 313337 : memset (ptr, '\0', MIN (total_bytes - off, len));
7991 : :
7992 : 11582078 : for (cnt = 0; ; cnt++)
7993 : : {
7994 : 11895415 : tree val = NULL_TREE, index = NULL_TREE;
7995 : 11895415 : HOST_WIDE_INT pos = curpos, count = 0;
7996 : 11895415 : bool full = false;
7997 : 11895415 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
7998 : : {
7999 : 11849955 : val = ce->value;
8000 : 11849955 : index = ce->index;
8001 : : }
8002 : 45460 : else if (mask == NULL
8003 : 228 : || CONSTRUCTOR_NO_CLEARING (init)
8004 : 45688 : || curpos >= total_bytes)
8005 : : break;
8006 : : else
8007 : : pos = total_bytes;
8008 : :
8009 : 11849955 : if (index && TREE_CODE (index) == RANGE_EXPR)
8010 : : {
8011 : 16 : if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8012 : 16 : || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8013 : 0 : return 0;
8014 : 16 : offset_int wpos
8015 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8016 : 32 : - wi::to_offset (min_index),
8017 : 16 : TYPE_PRECISION (sizetype));
8018 : 16 : wpos *= fieldsize;
8019 : 16 : if (!wi::fits_shwi_p (pos))
8020 : : return 0;
8021 : 16 : pos = wpos.to_shwi ();
8022 : 16 : offset_int wcount
8023 : 16 : = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8024 : 32 : - wi::to_offset (TREE_OPERAND (index, 0)),
8025 : 16 : TYPE_PRECISION (sizetype));
8026 : 16 : if (!wi::fits_shwi_p (wcount))
8027 : : return 0;
8028 : 16 : count = wcount.to_shwi ();
8029 : 16 : }
8030 : 10988426 : else if (index)
8031 : : {
8032 : 10988426 : if (TREE_CODE (index) != INTEGER_CST)
8033 : 0 : return 0;
8034 : 10988426 : offset_int wpos
8035 : 10988426 : = wi::sext (wi::to_offset (index)
8036 : 21976852 : - wi::to_offset (min_index),
8037 : 10988426 : TYPE_PRECISION (sizetype));
8038 : 10988426 : wpos *= fieldsize;
8039 : 10988426 : if (!wi::fits_shwi_p (wpos))
8040 : : return 0;
8041 : 10988426 : pos = wpos.to_shwi ();
8042 : : }
8043 : :
8044 : 11850626 : if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8045 : : {
8046 : 14 : if (valueinit == -1)
8047 : : {
8048 : 14 : tree zero = build_zero_cst (TREE_TYPE (type));
8049 : 28 : r = native_encode_initializer (zero, ptr + curpos,
8050 : : fieldsize, 0,
8051 : 14 : mask + curpos);
8052 : 14 : if (TREE_CODE (zero) == CONSTRUCTOR)
8053 : 0 : ggc_free (zero);
8054 : 14 : if (!r)
8055 : : return 0;
8056 : 14 : valueinit = curpos;
8057 : 14 : curpos += fieldsize;
8058 : : }
8059 : 44 : while (curpos != pos)
8060 : : {
8061 : 30 : memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8062 : 30 : memcpy (mask + curpos, mask + valueinit, fieldsize);
8063 : 30 : curpos += fieldsize;
8064 : : }
8065 : : }
8066 : :
8067 : 11849969 : curpos = pos;
8068 : 11849969 : if (val && TREE_CODE (val) == RAW_DATA_CST)
8069 : : {
8070 : 474 : if (count)
8071 : : return 0;
8072 : 474 : if (off == -1
8073 : 474 : || (curpos >= off
8074 : 0 : && (curpos + RAW_DATA_LENGTH (val)
8075 : 0 : <= (HOST_WIDE_INT) off + len)))
8076 : : {
8077 : 474 : if (ptr)
8078 : 474 : memcpy (ptr + (curpos - o), RAW_DATA_POINTER (val),
8079 : 474 : RAW_DATA_LENGTH (val));
8080 : 474 : if (mask)
8081 : 0 : memset (mask + curpos, 0, RAW_DATA_LENGTH (val));
8082 : : }
8083 : 0 : else if (curpos + RAW_DATA_LENGTH (val) > off
8084 : 0 : && curpos < (HOST_WIDE_INT) off + len)
8085 : : {
8086 : : /* Partial overlap. */
8087 : 0 : unsigned char *p = NULL;
8088 : 0 : int no = 0;
8089 : 0 : int l;
8090 : 0 : gcc_assert (mask == NULL);
8091 : 0 : if (curpos >= off)
8092 : : {
8093 : 0 : if (ptr)
8094 : 0 : p = ptr + curpos - off;
8095 : 0 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8096 : : RAW_DATA_LENGTH (val));
8097 : : }
8098 : : else
8099 : : {
8100 : 0 : p = ptr;
8101 : 0 : no = off - curpos;
8102 : 0 : l = len;
8103 : : }
8104 : 0 : if (p)
8105 : 0 : memcpy (p, RAW_DATA_POINTER (val) + no, l);
8106 : : }
8107 : 474 : curpos += RAW_DATA_LENGTH (val);
8108 : 474 : val = NULL_TREE;
8109 : : }
8110 : 474 : if (val)
8111 : 11927521 : do
8112 : : {
8113 : 11927521 : if (off == -1
8114 : 974506 : || (curpos >= off
8115 : 291382 : && (curpos + fieldsize
8116 : 291382 : <= (HOST_WIDE_INT) off + len)))
8117 : : {
8118 : 11210677 : if (full)
8119 : : {
8120 : 78040 : if (ptr)
8121 : 78040 : memcpy (ptr + (curpos - o), ptr + (pos - o),
8122 : : fieldsize);
8123 : 78040 : if (mask)
8124 : 0 : memcpy (mask + curpos, mask + pos, fieldsize);
8125 : : }
8126 : 22523579 : else if (!native_encode_initializer (val,
8127 : : ptr
8128 : 11132637 : ? ptr + curpos - o
8129 : : : NULL,
8130 : : fieldsize,
8131 : : off == -1 ? -1
8132 : : : 0,
8133 : : mask
8134 : 643 : ? mask + curpos
8135 : : : NULL))
8136 : : return 0;
8137 : : else
8138 : : {
8139 : : full = true;
8140 : : pos = curpos;
8141 : : }
8142 : : }
8143 : 716844 : else if (curpos + fieldsize > off
8144 : 36295 : && curpos < (HOST_WIDE_INT) off + len)
8145 : : {
8146 : : /* Partial overlap. */
8147 : 9312 : unsigned char *p = NULL;
8148 : 9312 : int no = 0;
8149 : 9312 : int l;
8150 : 9312 : gcc_assert (mask == NULL);
8151 : 9312 : if (curpos >= off)
8152 : : {
8153 : 6737 : if (ptr)
8154 : 6737 : p = ptr + curpos - off;
8155 : 6737 : l = MIN ((HOST_WIDE_INT) off + len - curpos,
8156 : : fieldsize);
8157 : : }
8158 : : else
8159 : : {
8160 : 2575 : p = ptr;
8161 : 2575 : no = off - curpos;
8162 : 2575 : l = len;
8163 : : }
8164 : 9312 : if (!native_encode_initializer (val, p, l, no, NULL))
8165 : : return 0;
8166 : : }
8167 : 11659630 : curpos += fieldsize;
8168 : : }
8169 : 11659630 : while (count-- != 0);
8170 : 11582078 : }
8171 : 45446 : return MIN (total_bytes - off, len);
8172 : : }
8173 : 590828 : else if (TREE_CODE (type) == RECORD_TYPE
8174 : 590828 : || TREE_CODE (type) == UNION_TYPE)
8175 : : {
8176 : 590828 : unsigned HOST_WIDE_INT cnt;
8177 : 590828 : constructor_elt *ce;
8178 : 590828 : tree fld_base = TYPE_FIELDS (type);
8179 : 590828 : tree to_free = NULL_TREE;
8180 : :
8181 : 590828 : gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8182 : 590828 : if (ptr != NULL)
8183 : 590828 : memset (ptr, '\0', MIN (total_bytes - o, len));
8184 : 103547 : for (cnt = 0; ; cnt++)
8185 : : {
8186 : 694375 : tree val = NULL_TREE, field = NULL_TREE;
8187 : 694375 : HOST_WIDE_INT pos = 0, fieldsize;
8188 : 694375 : unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8189 : :
8190 : 694375 : if (to_free)
8191 : : {
8192 : 0 : ggc_free (to_free);
8193 : 0 : to_free = NULL_TREE;
8194 : : }
8195 : :
8196 : 694375 : if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8197 : : {
8198 : 126583 : val = ce->value;
8199 : 126583 : field = ce->index;
8200 : 126583 : if (field == NULL_TREE)
8201 : : return 0;
8202 : :
8203 : 126583 : pos = int_byte_position (field);
8204 : 126583 : if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8205 : 1273 : continue;
8206 : : }
8207 : 567792 : else if (mask == NULL
8208 : 567792 : || CONSTRUCTOR_NO_CLEARING (init))
8209 : : break;
8210 : : else
8211 : : pos = total_bytes;
8212 : :
8213 : 127507 : if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8214 : : {
8215 : : tree fld;
8216 : 11729 : for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8217 : : {
8218 : 11230 : if (TREE_CODE (fld) != FIELD_DECL)
8219 : 10077 : continue;
8220 : 1153 : if (fld == field)
8221 : : break;
8222 : 146 : if (DECL_PADDING_P (fld))
8223 : 87 : continue;
8224 : 59 : if (DECL_SIZE_UNIT (fld) == NULL_TREE
8225 : 59 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8226 : : return 0;
8227 : 59 : if (integer_zerop (DECL_SIZE_UNIT (fld)))
8228 : 2 : continue;
8229 : : break;
8230 : : }
8231 : 1563 : if (fld == NULL_TREE)
8232 : : {
8233 : 499 : if (ce == NULL)
8234 : : break;
8235 : : return 0;
8236 : : }
8237 : 1064 : fld_base = DECL_CHAIN (fld);
8238 : 1064 : if (fld != field)
8239 : : {
8240 : 57 : cnt--;
8241 : 57 : field = fld;
8242 : 57 : pos = int_byte_position (field);
8243 : 57 : val = build_zero_cst (TREE_TYPE (fld));
8244 : 57 : if (TREE_CODE (val) == CONSTRUCTOR)
8245 : 0 : to_free = val;
8246 : : }
8247 : : }
8248 : :
8249 : 125367 : if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8250 : 5935 : && TYPE_DOMAIN (TREE_TYPE (field))
8251 : 131302 : && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8252 : : {
8253 : 81 : if (mask || off != -1)
8254 : : return 0;
8255 : 81 : if (val == NULL_TREE)
8256 : 0 : continue;
8257 : 81 : if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8258 : : return 0;
8259 : 81 : fieldsize = int_size_in_bytes (TREE_TYPE (val));
8260 : 81 : if (fieldsize < 0
8261 : 81 : || (int) fieldsize != fieldsize
8262 : 81 : || (pos + fieldsize) > INT_MAX)
8263 : : return 0;
8264 : 81 : if (pos + fieldsize > total_bytes)
8265 : : {
8266 : 81 : if (ptr != NULL && total_bytes < len)
8267 : 81 : memset (ptr + total_bytes, '\0',
8268 : 81 : MIN (pos + fieldsize, len) - total_bytes);
8269 : : total_bytes = pos + fieldsize;
8270 : : }
8271 : : }
8272 : : else
8273 : : {
8274 : 125286 : if (DECL_SIZE_UNIT (field) == NULL_TREE
8275 : 125286 : || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8276 : : return 0;
8277 : 125286 : fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8278 : : }
8279 : 125367 : if (fieldsize == 0)
8280 : 1 : continue;
8281 : :
8282 : : /* Prepare to deal with integral bit-fields and filter out other
8283 : : bit-fields that do not start and end on a byte boundary. */
8284 : 125366 : if (DECL_BIT_FIELD (field))
8285 : : {
8286 : 2651 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8287 : : return 0;
8288 : 2651 : bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8289 : 2651 : if (INTEGRAL_TYPE_P (TREE_TYPE (field)))
8290 : : {
8291 : 2651 : bpos %= BITS_PER_UNIT;
8292 : 2651 : fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos;
8293 : 2651 : epos = fieldsize % BITS_PER_UNIT;
8294 : 2651 : fieldsize += BITS_PER_UNIT - 1;
8295 : 2651 : fieldsize /= BITS_PER_UNIT;
8296 : : }
8297 : 0 : else if (bpos % BITS_PER_UNIT
8298 : 0 : || DECL_SIZE (field) == NULL_TREE
8299 : 0 : || !tree_fits_shwi_p (DECL_SIZE (field))
8300 : 0 : || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT)
8301 : : return 0;
8302 : : }
8303 : :
8304 : 125366 : if (off != -1 && pos + fieldsize <= off)
8305 : 3768 : continue;
8306 : :
8307 : 121598 : if (val == NULL_TREE)
8308 : 0 : continue;
8309 : :
8310 : 121598 : if (DECL_BIT_FIELD (field)
8311 : 121598 : && INTEGRAL_TYPE_P (TREE_TYPE (field)))
8312 : : {
8313 : : /* FIXME: Handle PDP endian. */
8314 : 2447 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8315 : 261 : return 0;
8316 : :
8317 : 2447 : if (TREE_CODE (val) == NON_LVALUE_EXPR)
8318 : 6 : val = TREE_OPERAND (val, 0);
8319 : 2447 : if (TREE_CODE (val) != INTEGER_CST)
8320 : : return 0;
8321 : :
8322 : 2447 : tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8323 : 2447 : tree repr_type = NULL_TREE;
8324 : 2447 : HOST_WIDE_INT rpos = 0;
8325 : 2447 : if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8326 : : {
8327 : 1918 : rpos = int_byte_position (repr);
8328 : 1918 : repr_type = TREE_TYPE (repr);
8329 : : }
8330 : : else
8331 : : {
8332 : 529 : repr_type = find_bitfield_repr_type (fieldsize, len);
8333 : 529 : if (repr_type == NULL_TREE)
8334 : : return 0;
8335 : 268 : HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8336 : 268 : gcc_assert (repr_size > 0 && repr_size <= len);
8337 : 268 : if (pos + repr_size <= o + len)
8338 : : rpos = pos;
8339 : : else
8340 : : {
8341 : 14 : rpos = o + len - repr_size;
8342 : 14 : gcc_assert (rpos <= pos);
8343 : : }
8344 : : }
8345 : :
8346 : 2186 : if (rpos > pos)
8347 : : return 0;
8348 : 2186 : wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8349 : 2186 : int diff = (TYPE_PRECISION (repr_type)
8350 : 2186 : - TYPE_PRECISION (TREE_TYPE (field)));
8351 : 2186 : HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8352 : 2186 : if (!BYTES_BIG_ENDIAN)
8353 : 2186 : w = wi::lshift (w, bitoff);
8354 : : else
8355 : : w = wi::lshift (w, diff - bitoff);
8356 : 2186 : val = wide_int_to_tree (repr_type, w);
8357 : :
8358 : 2186 : unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8359 : : / BITS_PER_UNIT + 1];
8360 : 2186 : int l = native_encode_int (val, buf, sizeof buf, 0);
8361 : 2186 : if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8362 : 0 : return 0;
8363 : :
8364 : 2186 : if (ptr == NULL)
8365 : 0 : continue;
8366 : :
8367 : : /* If the bitfield does not start at byte boundary, handle
8368 : : the partial byte at the start. */
8369 : 2186 : if (bpos
8370 : 1304 : && (off == -1 || (pos >= off && len >= 1)))
8371 : : {
8372 : 1229 : if (!BYTES_BIG_ENDIAN)
8373 : : {
8374 : 1229 : int msk = (1 << bpos) - 1;
8375 : 1229 : buf[pos - rpos] &= ~msk;
8376 : 1229 : buf[pos - rpos] |= ptr[pos - o] & msk;
8377 : 1229 : if (mask)
8378 : : {
8379 : 147 : if (fieldsize > 1 || epos == 0)
8380 : 129 : mask[pos] &= msk;
8381 : : else
8382 : 18 : mask[pos] &= (msk | ~((1 << epos) - 1));
8383 : : }
8384 : : }
8385 : : else
8386 : : {
8387 : : int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8388 : : buf[pos - rpos] &= msk;
8389 : : buf[pos - rpos] |= ptr[pos - o] & ~msk;
8390 : : if (mask)
8391 : : {
8392 : : if (fieldsize > 1 || epos == 0)
8393 : : mask[pos] &= ~msk;
8394 : : else
8395 : : mask[pos] &= (~msk
8396 : : | ((1 << (BITS_PER_UNIT - epos))
8397 : : - 1));
8398 : : }
8399 : : }
8400 : : }
8401 : : /* If the bitfield does not end at byte boundary, handle
8402 : : the partial byte at the end. */
8403 : 2186 : if (epos
8404 : 1679 : && (off == -1
8405 : 1004 : || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8406 : : {
8407 : 1576 : if (!BYTES_BIG_ENDIAN)
8408 : : {
8409 : 1576 : int msk = (1 << epos) - 1;
8410 : 1576 : buf[pos - rpos + fieldsize - 1] &= msk;
8411 : 1576 : buf[pos - rpos + fieldsize - 1]
8412 : 1576 : |= ptr[pos + fieldsize - 1 - o] & ~msk;
8413 : 1576 : if (mask && (fieldsize > 1 || bpos == 0))
8414 : 156 : mask[pos + fieldsize - 1] &= ~msk;
8415 : : }
8416 : : else
8417 : : {
8418 : : int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8419 : : buf[pos - rpos + fieldsize - 1] &= ~msk;
8420 : : buf[pos - rpos + fieldsize - 1]
8421 : : |= ptr[pos + fieldsize - 1 - o] & msk;
8422 : : if (mask && (fieldsize > 1 || bpos == 0))
8423 : : mask[pos + fieldsize - 1] &= msk;
8424 : : }
8425 : : }
8426 : 2186 : if (off == -1
8427 : 1301 : || (pos >= off
8428 : 1212 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8429 : : {
8430 : 1995 : memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8431 : 1995 : if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8432 : 75 : memset (mask + pos + (bpos != 0), 0,
8433 : 75 : fieldsize - (bpos != 0) - (epos != 0));
8434 : : }
8435 : : else
8436 : : {
8437 : : /* Partial overlap. */
8438 : 191 : HOST_WIDE_INT fsz = fieldsize;
8439 : 191 : gcc_assert (mask == NULL);
8440 : 191 : if (pos < off)
8441 : : {
8442 : 89 : fsz -= (off - pos);
8443 : 89 : pos = off;
8444 : : }
8445 : 191 : if (pos + fsz > (HOST_WIDE_INT) off + len)
8446 : 104 : fsz = (HOST_WIDE_INT) off + len - pos;
8447 : 191 : memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8448 : : }
8449 : 2186 : continue;
8450 : 2186 : }
8451 : :
8452 : 119151 : if (off == -1
8453 : 19911 : || (pos >= off
8454 : 19064 : && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8455 : : {
8456 : 109598 : int fldsize = fieldsize;
8457 : 10358 : if (off == -1)
8458 : : {
8459 : 99240 : tree fld = DECL_CHAIN (field);
8460 : 1321541 : while (fld)
8461 : : {
8462 : 1231635 : if (TREE_CODE (fld) == FIELD_DECL)
8463 : : break;
8464 : 1222301 : fld = DECL_CHAIN (fld);
8465 : : }
8466 : 99240 : if (fld == NULL_TREE)
8467 : 89906 : fldsize = len - pos;
8468 : : }
8469 : 120780 : r = native_encode_initializer (val, ptr ? ptr + pos - o
8470 : : : NULL,
8471 : : fldsize,
8472 : : off == -1 ? -1 : 0,
8473 : 824 : mask ? mask + pos : NULL);
8474 : 109598 : if (!r)
8475 : : return 0;
8476 : 95269 : if (off == -1
8477 : 93306 : && fldsize != fieldsize
8478 : 324 : && r > fieldsize
8479 : 54 : && pos + r > total_bytes)
8480 : 103547 : total_bytes = pos + r;
8481 : : }
8482 : : else
8483 : : {
8484 : : /* Partial overlap. */
8485 : 9553 : unsigned char *p = NULL;
8486 : 9553 : int no = 0;
8487 : 9553 : int l;
8488 : 9553 : gcc_assert (mask == NULL);
8489 : 9553 : if (pos >= off)
8490 : : {
8491 : 8706 : if (ptr)
8492 : 8706 : p = ptr + pos - off;
8493 : 8706 : l = MIN ((HOST_WIDE_INT) off + len - pos,
8494 : : fieldsize);
8495 : : }
8496 : : else
8497 : : {
8498 : 847 : p = ptr;
8499 : 847 : no = off - pos;
8500 : 847 : l = len;
8501 : : }
8502 : 9553 : if (!native_encode_initializer (val, p, l, no, NULL))
8503 : : return 0;
8504 : : }
8505 : 103547 : }
8506 : 567735 : return MIN (total_bytes - off, len);
8507 : : }
8508 : : return 0;
8509 : : }
8510 : : }
8511 : :
8512 : :
8513 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8514 : : the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8515 : : If the buffer cannot be interpreted, return NULL_TREE. */
8516 : :
8517 : : static tree
8518 : 2690490 : native_interpret_int (tree type, const unsigned char *ptr, int len)
8519 : : {
8520 : 2690490 : int total_bytes;
8521 : 2690490 : if (TREE_CODE (type) == BITINT_TYPE)
8522 : : {
8523 : 17 : struct bitint_info info;
8524 : 17 : bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
8525 : 17 : gcc_assert (ok);
8526 : 17 : scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
8527 : 17 : if (TYPE_PRECISION (type) > GET_MODE_PRECISION (limb_mode))
8528 : : {
8529 : 17 : total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8530 : : /* More work is needed when adding _BitInt support to PDP endian
8531 : : if limb is smaller than word, or if _BitInt limb ordering doesn't
8532 : : match target endianity here. */
8533 : 17 : gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN
8534 : : && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN
8535 : : || (GET_MODE_SIZE (limb_mode)
8536 : : >= UNITS_PER_WORD)));
8537 : : }
8538 : : else
8539 : 0 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8540 : : }
8541 : : else
8542 : 5380946 : total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8543 : :
8544 : 2690490 : if (total_bytes > len)
8545 : : return NULL_TREE;
8546 : :
8547 : 2690273 : wide_int result = wi::from_buffer (ptr, total_bytes);
8548 : :
8549 : 2690273 : return wide_int_to_tree (type, result);
8550 : 2690273 : }
8551 : :
8552 : :
8553 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8554 : : the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8555 : : If the buffer cannot be interpreted, return NULL_TREE. */
8556 : :
8557 : : static tree
8558 : 0 : native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8559 : : {
8560 : 0 : scalar_mode mode = SCALAR_TYPE_MODE (type);
8561 : 0 : int total_bytes = GET_MODE_SIZE (mode);
8562 : 0 : double_int result;
8563 : 0 : FIXED_VALUE_TYPE fixed_value;
8564 : :
8565 : 0 : if (total_bytes > len
8566 : 0 : || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8567 : : return NULL_TREE;
8568 : :
8569 : 0 : result = double_int::from_buffer (ptr, total_bytes);
8570 : 0 : fixed_value = fixed_from_double_int (result, mode);
8571 : :
8572 : 0 : return build_fixed (type, fixed_value);
8573 : : }
8574 : :
8575 : :
8576 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8577 : : the buffer PTR of length LEN as a REAL_CST of type TYPE.
8578 : : If the buffer cannot be interpreted, return NULL_TREE. */
8579 : :
8580 : : tree
8581 : 30931 : native_interpret_real (tree type, const unsigned char *ptr, int len)
8582 : : {
8583 : 30931 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8584 : 30931 : int total_bytes = GET_MODE_SIZE (mode);
8585 : 30931 : unsigned char value;
8586 : : /* There are always 32 bits in each long, no matter the size of
8587 : : the hosts long. We handle floating point representations with
8588 : : up to 192 bits. */
8589 : 30931 : REAL_VALUE_TYPE r;
8590 : 30931 : long tmp[6];
8591 : :
8592 : 30931 : if (total_bytes > len || total_bytes > 24)
8593 : : return NULL_TREE;
8594 : 30870 : int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8595 : :
8596 : 30870 : memset (tmp, 0, sizeof (tmp));
8597 : 222996 : for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8598 : 192126 : bitpos += BITS_PER_UNIT)
8599 : : {
8600 : : /* Both OFFSET and BYTE index within a long;
8601 : : bitpos indexes the whole float. */
8602 : 192126 : int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8603 : 192126 : if (UNITS_PER_WORD < 4)
8604 : : {
8605 : : int word = byte / UNITS_PER_WORD;
8606 : : if (WORDS_BIG_ENDIAN)
8607 : : word = (words - 1) - word;
8608 : : offset = word * UNITS_PER_WORD;
8609 : : if (BYTES_BIG_ENDIAN)
8610 : : offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8611 : : else
8612 : : offset += byte % UNITS_PER_WORD;
8613 : : }
8614 : : else
8615 : : {
8616 : 192126 : offset = byte;
8617 : 192126 : if (BYTES_BIG_ENDIAN)
8618 : : {
8619 : : /* Reverse bytes within each long, or within the entire float
8620 : : if it's smaller than a long (for HFmode). */
8621 : : offset = MIN (3, total_bytes - 1) - offset;
8622 : : gcc_assert (offset >= 0);
8623 : : }
8624 : : }
8625 : 192126 : value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8626 : :
8627 : 192126 : tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8628 : : }
8629 : :
8630 : 30870 : real_from_target (&r, tmp, mode);
8631 : 30870 : return build_real (type, r);
8632 : : }
8633 : :
8634 : :
8635 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8636 : : the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8637 : : If the buffer cannot be interpreted, return NULL_TREE. */
8638 : :
8639 : : static tree
8640 : 1377 : native_interpret_complex (tree type, const unsigned char *ptr, int len)
8641 : : {
8642 : 1377 : tree etype, rpart, ipart;
8643 : 1377 : int size;
8644 : :
8645 : 1377 : etype = TREE_TYPE (type);
8646 : 1377 : size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8647 : 1377 : if (size * 2 > len)
8648 : : return NULL_TREE;
8649 : 1342 : rpart = native_interpret_expr (etype, ptr, size);
8650 : 1342 : if (!rpart)
8651 : : return NULL_TREE;
8652 : 1341 : ipart = native_interpret_expr (etype, ptr+size, size);
8653 : 1341 : if (!ipart)
8654 : : return NULL_TREE;
8655 : 1341 : return build_complex (type, rpart, ipart);
8656 : : }
8657 : :
8658 : : /* Read a vector of type TYPE from the target memory image given by BYTES,
8659 : : which contains LEN bytes. The vector is known to be encodable using
8660 : : NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8661 : :
8662 : : Return the vector on success, otherwise return null. */
8663 : :
8664 : : static tree
8665 : 175838 : native_interpret_vector_part (tree type, const unsigned char *bytes,
8666 : : unsigned int len, unsigned int npatterns,
8667 : : unsigned int nelts_per_pattern)
8668 : : {
8669 : 175838 : tree elt_type = TREE_TYPE (type);
8670 : 175838 : if (VECTOR_BOOLEAN_TYPE_P (type)
8671 : 175841 : && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8672 : : {
8673 : : /* This is the only case in which elements can be smaller than a byte.
8674 : : Element 0 is always in the lsb of the containing byte. */
8675 : 1 : unsigned int elt_bits = TYPE_PRECISION (elt_type);
8676 : 1 : if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8677 : : return NULL_TREE;
8678 : :
8679 : 1 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8680 : 17 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8681 : : {
8682 : 16 : unsigned int bit_index = i * elt_bits;
8683 : 16 : unsigned int byte_index = bit_index / BITS_PER_UNIT;
8684 : 16 : unsigned int lsb = bit_index % BITS_PER_UNIT;
8685 : 32 : builder.quick_push (bytes[byte_index] & (1 << lsb)
8686 : 17 : ? build_all_ones_cst (elt_type)
8687 : 1 : : build_zero_cst (elt_type));
8688 : : }
8689 : 1 : return builder.build ();
8690 : 1 : }
8691 : :
8692 : 175837 : unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8693 : 175837 : if (elt_bytes * npatterns * nelts_per_pattern > len)
8694 : : return NULL_TREE;
8695 : :
8696 : 175837 : tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8697 : 706528 : for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8698 : : {
8699 : 530729 : tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8700 : 530729 : if (!elt)
8701 : 38 : return NULL_TREE;
8702 : 530691 : builder.quick_push (elt);
8703 : 530691 : bytes += elt_bytes;
8704 : : }
8705 : 175799 : return builder.build ();
8706 : 175837 : }
8707 : :
8708 : : /* Subroutine of native_interpret_expr. Interpret the contents of
8709 : : the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8710 : : If the buffer cannot be interpreted, return NULL_TREE. */
8711 : :
8712 : : static tree
8713 : 72220 : native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8714 : : {
8715 : 72220 : unsigned HOST_WIDE_INT size;
8716 : :
8717 : 72220 : if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (&size)
8718 : 72220 : || size > len)
8719 : 2 : return NULL_TREE;
8720 : :
8721 : 72218 : unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (type).to_constant ();
8722 : 72218 : return native_interpret_vector_part (type, ptr, len, count, 1);
8723 : : }
8724 : :
8725 : :
8726 : : /* Subroutine of fold_view_convert_expr. Interpret the contents of
8727 : : the buffer PTR of length LEN as a constant of type TYPE. For
8728 : : INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8729 : : we return a REAL_CST, etc... If the buffer cannot be interpreted,
8730 : : return NULL_TREE. */
8731 : :
8732 : : tree
8733 : 2848510 : native_interpret_expr (tree type, const unsigned char *ptr, int len)
8734 : : {
8735 : 2848510 : switch (TREE_CODE (type))
8736 : : {
8737 : 2690490 : case INTEGER_TYPE:
8738 : 2690490 : case ENUMERAL_TYPE:
8739 : 2690490 : case BOOLEAN_TYPE:
8740 : 2690490 : case POINTER_TYPE:
8741 : 2690490 : case REFERENCE_TYPE:
8742 : 2690490 : case OFFSET_TYPE:
8743 : 2690490 : case BITINT_TYPE:
8744 : 2690490 : return native_interpret_int (type, ptr, len);
8745 : :
8746 : 29717 : case REAL_TYPE:
8747 : 29717 : if (tree ret = native_interpret_real (type, ptr, len))
8748 : : {
8749 : : /* For floating point values in composite modes, punt if this
8750 : : folding doesn't preserve bit representation. As the mode doesn't
8751 : : have fixed precision while GCC pretends it does, there could be
8752 : : valid values that GCC can't really represent accurately.
8753 : : See PR95450. Even for other modes, e.g. x86 XFmode can have some
8754 : : bit combinationations which GCC doesn't preserve. */
8755 : 29656 : unsigned char buf[24 * 2];
8756 : 29656 : scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8757 : 29656 : int total_bytes = GET_MODE_SIZE (mode);
8758 : 29656 : memcpy (buf + 24, ptr, total_bytes);
8759 : 29656 : clear_type_padding_in_mask (type, buf + 24);
8760 : 29656 : if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8761 : 29656 : || memcmp (buf + 24, buf, total_bytes) != 0)
8762 : 156 : return NULL_TREE;
8763 : : return ret;
8764 : : }
8765 : : return NULL_TREE;
8766 : :
8767 : 0 : case FIXED_POINT_TYPE:
8768 : 0 : return native_interpret_fixed (type, ptr, len);
8769 : :
8770 : 1377 : case COMPLEX_TYPE:
8771 : 1377 : return native_interpret_complex (type, ptr, len);
8772 : :
8773 : 72220 : case VECTOR_TYPE:
8774 : 72220 : return native_interpret_vector (type, ptr, len);
8775 : :
8776 : : default:
8777 : : return NULL_TREE;
8778 : : }
8779 : : }
8780 : :
8781 : : /* Returns true if we can interpret the contents of a native encoding
8782 : : as TYPE. */
8783 : :
8784 : : bool
8785 : 446991 : can_native_interpret_type_p (tree type)
8786 : : {
8787 : 446991 : switch (TREE_CODE (type))
8788 : : {
8789 : : case INTEGER_TYPE:
8790 : : case ENUMERAL_TYPE:
8791 : : case BOOLEAN_TYPE:
8792 : : case POINTER_TYPE:
8793 : : case REFERENCE_TYPE:
8794 : : case FIXED_POINT_TYPE:
8795 : : case REAL_TYPE:
8796 : : case COMPLEX_TYPE:
8797 : : case VECTOR_TYPE:
8798 : : case OFFSET_TYPE:
8799 : : return true;
8800 : 84186 : default:
8801 : 84186 : return false;
8802 : : }
8803 : : }
8804 : :
8805 : : /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8806 : : byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8807 : :
8808 : : tree
8809 : 603 : native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8810 : : int len)
8811 : : {
8812 : 603 : vec<constructor_elt, va_gc> *elts = NULL;
8813 : 603 : if (TREE_CODE (type) == ARRAY_TYPE)
8814 : : {
8815 : 197 : HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8816 : 394 : if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8817 : : return NULL_TREE;
8818 : :
8819 : 197 : HOST_WIDE_INT cnt = 0;
8820 : 197 : if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8821 : : {
8822 : 197 : if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8823 : : return NULL_TREE;
8824 : 197 : cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8825 : : }
8826 : 197 : if (eltsz == 0)
8827 : 0 : cnt = 0;
8828 : 197 : HOST_WIDE_INT pos = 0;
8829 : 636 : for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8830 : : {
8831 : 439 : tree v = NULL_TREE;
8832 : 439 : if (pos >= len || pos + eltsz > len)
8833 : 603 : return NULL_TREE;
8834 : 439 : if (can_native_interpret_type_p (TREE_TYPE (type)))
8835 : : {
8836 : 367 : v = native_interpret_expr (TREE_TYPE (type),
8837 : 367 : ptr + off + pos, eltsz);
8838 : 367 : if (v == NULL_TREE)
8839 : : return NULL_TREE;
8840 : : }
8841 : 72 : else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8842 : 72 : || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8843 : 72 : v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8844 : : eltsz);
8845 : 72 : if (v == NULL_TREE)
8846 : 0 : return NULL_TREE;
8847 : 439 : CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8848 : : }
8849 : 197 : return build_constructor (type, elts);
8850 : : }
8851 : 406 : if (TREE_CODE (type) != RECORD_TYPE)
8852 : : return NULL_TREE;
8853 : 6618 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8854 : : {
8855 : 1138 : if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field)
8856 : 7350 : || is_empty_type (TREE_TYPE (field)))
8857 : 5164 : continue;
8858 : 1048 : tree fld = field;
8859 : 1048 : HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8860 : 1048 : int diff = 0;
8861 : 1048 : tree v = NULL_TREE;
8862 : 1048 : if (DECL_BIT_FIELD (field))
8863 : : {
8864 : 180 : fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8865 : 180 : if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8866 : : {
8867 : 168 : poly_int64 bitoffset;
8868 : 168 : poly_uint64 field_offset, fld_offset;
8869 : 168 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8870 : 336 : && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8871 : 168 : bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8872 : : else
8873 : : bitoffset = 0;
8874 : 168 : bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8875 : 168 : - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8876 : 168 : diff = (TYPE_PRECISION (TREE_TYPE (fld))
8877 : 168 : - TYPE_PRECISION (TREE_TYPE (field)));
8878 : 168 : if (!bitoffset.is_constant (&bitoff)
8879 : 168 : || bitoff < 0
8880 : 168 : || bitoff > diff)
8881 : 0 : return NULL_TREE;
8882 : : }
8883 : : else
8884 : : {
8885 : 12 : if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8886 : : return NULL_TREE;
8887 : 12 : int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8888 : 12 : int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8889 : 12 : bpos %= BITS_PER_UNIT;
8890 : 12 : fieldsize += bpos;
8891 : 12 : fieldsize += BITS_PER_UNIT - 1;
8892 : 12 : fieldsize /= BITS_PER_UNIT;
8893 : 12 : tree repr_type = find_bitfield_repr_type (fieldsize, len);
8894 : 12 : if (repr_type == NULL_TREE)
8895 : : return NULL_TREE;
8896 : 12 : sz = int_size_in_bytes (repr_type);
8897 : 12 : if (sz < 0 || sz > len)
8898 : : return NULL_TREE;
8899 : 12 : pos = int_byte_position (field);
8900 : 12 : if (pos < 0 || pos > len || pos + fieldsize > len)
8901 : : return NULL_TREE;
8902 : 12 : HOST_WIDE_INT rpos;
8903 : 12 : if (pos + sz <= len)
8904 : : rpos = pos;
8905 : : else
8906 : : {
8907 : 0 : rpos = len - sz;
8908 : 0 : gcc_assert (rpos <= pos);
8909 : : }
8910 : 12 : bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
8911 : 12 : pos = rpos;
8912 : 12 : diff = (TYPE_PRECISION (repr_type)
8913 : 12 : - TYPE_PRECISION (TREE_TYPE (field)));
8914 : 12 : v = native_interpret_expr (repr_type, ptr + off + pos, sz);
8915 : 12 : if (v == NULL_TREE)
8916 : : return NULL_TREE;
8917 : : fld = NULL_TREE;
8918 : : }
8919 : : }
8920 : :
8921 : 168 : if (fld)
8922 : : {
8923 : 1036 : sz = int_size_in_bytes (TREE_TYPE (fld));
8924 : 1036 : if (sz < 0 || sz > len)
8925 : : return NULL_TREE;
8926 : 1036 : tree byte_pos = byte_position (fld);
8927 : 1036 : if (!tree_fits_shwi_p (byte_pos))
8928 : : return NULL_TREE;
8929 : 1036 : pos = tree_to_shwi (byte_pos);
8930 : 1036 : if (pos < 0 || pos > len || pos + sz > len)
8931 : : return NULL_TREE;
8932 : : }
8933 : 1036 : if (fld == NULL_TREE)
8934 : : /* Already handled above. */;
8935 : 1036 : else if (can_native_interpret_type_p (TREE_TYPE (fld)))
8936 : : {
8937 : 844 : v = native_interpret_expr (TREE_TYPE (fld),
8938 : 844 : ptr + off + pos, sz);
8939 : 844 : if (v == NULL_TREE)
8940 : : return NULL_TREE;
8941 : : }
8942 : 192 : else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
8943 : 192 : || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
8944 : 192 : v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
8945 : 204 : if (v == NULL_TREE)
8946 : : return NULL_TREE;
8947 : 1048 : if (fld != field)
8948 : : {
8949 : 180 : if (TREE_CODE (v) != INTEGER_CST)
8950 : : return NULL_TREE;
8951 : :
8952 : : /* FIXME: Figure out how to handle PDP endian bitfields. */
8953 : 180 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8954 : : return NULL_TREE;
8955 : 180 : if (!BYTES_BIG_ENDIAN)
8956 : 180 : v = wide_int_to_tree (TREE_TYPE (field),
8957 : 360 : wi::lrshift (wi::to_wide (v), bitoff));
8958 : : else
8959 : : v = wide_int_to_tree (TREE_TYPE (field),
8960 : : wi::lrshift (wi::to_wide (v),
8961 : : diff - bitoff));
8962 : : }
8963 : 1048 : CONSTRUCTOR_APPEND_ELT (elts, field, v);
8964 : : }
8965 : 406 : return build_constructor (type, elts);
8966 : : }
8967 : :
8968 : : /* Routines for manipulation of native_encode_expr encoded data if the encoded
8969 : : or extracted constant positions and/or sizes aren't byte aligned. */
8970 : :
8971 : : /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
8972 : : bits between adjacent elements. AMNT should be within
8973 : : [0, BITS_PER_UNIT).
8974 : : Example, AMNT = 2:
8975 : : 00011111|11100000 << 2 = 01111111|10000000
8976 : : PTR[1] | PTR[0] PTR[1] | PTR[0]. */
8977 : :
8978 : : void
8979 : 37612 : shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
8980 : : unsigned int amnt)
8981 : : {
8982 : 37612 : if (amnt == 0)
8983 : : return;
8984 : :
8985 : 22324 : unsigned char carry_over = 0U;
8986 : 22324 : unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
8987 : 22324 : unsigned char clear_mask = (~0U) << amnt;
8988 : :
8989 : 118585 : for (unsigned int i = 0; i < sz; i++)
8990 : : {
8991 : 96261 : unsigned prev_carry_over = carry_over;
8992 : 96261 : carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
8993 : :
8994 : 96261 : ptr[i] <<= amnt;
8995 : 96261 : if (i != 0)
8996 : : {
8997 : 73937 : ptr[i] &= clear_mask;
8998 : 73937 : ptr[i] |= prev_carry_over;
8999 : : }
9000 : : }
9001 : : }
9002 : :
9003 : : /* Like shift_bytes_in_array_left but for big-endian.
9004 : : Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9005 : : bits between adjacent elements. AMNT should be within
9006 : : [0, BITS_PER_UNIT).
9007 : : Example, AMNT = 2:
9008 : : 00011111|11100000 >> 2 = 00000111|11111000
9009 : : PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9010 : :
9011 : : void
9012 : 8 : shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9013 : : unsigned int amnt)
9014 : : {
9015 : 8 : if (amnt == 0)
9016 : : return;
9017 : :
9018 : 4 : unsigned char carry_over = 0U;
9019 : 4 : unsigned char carry_mask = ~(~0U << amnt);
9020 : :
9021 : 12 : for (unsigned int i = 0; i < sz; i++)
9022 : : {
9023 : 8 : unsigned prev_carry_over = carry_over;
9024 : 8 : carry_over = ptr[i] & carry_mask;
9025 : :
9026 : 8 : carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9027 : 8 : ptr[i] >>= amnt;
9028 : 8 : ptr[i] |= prev_carry_over;
9029 : : }
9030 : : }
9031 : :
9032 : : /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9033 : : directly on the VECTOR_CST encoding, in a way that works for variable-
9034 : : length vectors. Return the resulting VECTOR_CST on success or null
9035 : : on failure. */
9036 : :
9037 : : static tree
9038 : 109706 : fold_view_convert_vector_encoding (tree type, tree expr)
9039 : : {
9040 : 109706 : tree expr_type = TREE_TYPE (expr);
9041 : 109706 : poly_uint64 type_bits, expr_bits;
9042 : 109706 : if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9043 : 109706 : || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9044 : 0 : return NULL_TREE;
9045 : :
9046 : 109706 : poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9047 : 109706 : poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9048 : 109706 : unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9049 : 109706 : unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9050 : :
9051 : : /* We can only preserve the semantics of a stepped pattern if the new
9052 : : vector element is an integer of the same size. */
9053 : 109706 : if (VECTOR_CST_STEPPED_P (expr)
9054 : 109706 : && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9055 : : return NULL_TREE;
9056 : :
9057 : : /* The number of bits needed to encode one element from every pattern
9058 : : of the original vector. */
9059 : 103620 : unsigned int expr_sequence_bits
9060 : 103620 : = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9061 : :
9062 : : /* The number of bits needed to encode one element from every pattern
9063 : : of the result. */
9064 : 103620 : unsigned int type_sequence_bits
9065 : 103620 : = least_common_multiple (expr_sequence_bits, type_elt_bits);
9066 : :
9067 : : /* Don't try to read more bytes than are available, which can happen
9068 : : for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9069 : : The general VIEW_CONVERT handling can cope with that case, so there's
9070 : : no point complicating things here. */
9071 : 103620 : unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9072 : 103620 : unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9073 : : BITS_PER_UNIT);
9074 : 103620 : unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9075 : 103620 : if (known_gt (buffer_bits, expr_bits))
9076 : : return NULL_TREE;
9077 : :
9078 : : /* Get enough bytes of EXPR to form the new encoding. */
9079 : 103620 : auto_vec<unsigned char, 128> buffer (buffer_bytes);
9080 : 103620 : buffer.quick_grow (buffer_bytes);
9081 : 103620 : if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9082 : 103620 : buffer_bits / expr_elt_bits)
9083 : : != (int) buffer_bytes)
9084 : : return NULL_TREE;
9085 : :
9086 : : /* Reencode the bytes as TYPE. */
9087 : 103620 : unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9088 : 207240 : return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9089 : 103620 : type_npatterns, nelts_per_pattern);
9090 : 103620 : }
9091 : :
9092 : : /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9093 : : TYPE at compile-time. If we're unable to perform the conversion
9094 : : return NULL_TREE. */
9095 : :
9096 : : static tree
9097 : 11012387 : fold_view_convert_expr (tree type, tree expr)
9098 : : {
9099 : 11012387 : unsigned char buffer[128];
9100 : 11012387 : unsigned char *buf;
9101 : 11012387 : int len;
9102 : 11012387 : HOST_WIDE_INT l;
9103 : :
9104 : : /* Check that the host and target are sane. */
9105 : 11012387 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9106 : : return NULL_TREE;
9107 : :
9108 : 11012387 : if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9109 : 109706 : if (tree res = fold_view_convert_vector_encoding (type, expr))
9110 : : return res;
9111 : :
9112 : 10908786 : l = int_size_in_bytes (type);
9113 : 10908786 : if (l > (int) sizeof (buffer)
9114 : 10908786 : && l <= WIDE_INT_MAX_PRECISION / BITS_PER_UNIT)
9115 : : {
9116 : 0 : buf = XALLOCAVEC (unsigned char, l);
9117 : 0 : len = l;
9118 : : }
9119 : : else
9120 : : {
9121 : : buf = buffer;
9122 : : len = sizeof (buffer);
9123 : : }
9124 : 10908786 : len = native_encode_expr (expr, buf, len);
9125 : 10908786 : if (len == 0)
9126 : : return NULL_TREE;
9127 : :
9128 : 1678074 : return native_interpret_expr (type, buf, len);
9129 : : }
9130 : :
9131 : : /* Build an expression for the address of T. Folds away INDIRECT_REF
9132 : : to avoid confusing the gimplify process. */
9133 : :
9134 : : tree
9135 : 410403475 : build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9136 : : {
9137 : : /* The size of the object is not relevant when talking about its address. */
9138 : 410403475 : if (TREE_CODE (t) == WITH_SIZE_EXPR)
9139 : 0 : t = TREE_OPERAND (t, 0);
9140 : :
9141 : 410403475 : if (INDIRECT_REF_P (t))
9142 : : {
9143 : 47099043 : t = TREE_OPERAND (t, 0);
9144 : :
9145 : 47099043 : if (TREE_TYPE (t) != ptrtype)
9146 : 29981311 : t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9147 : : }
9148 : 363304432 : else if (TREE_CODE (t) == MEM_REF
9149 : 363304432 : && integer_zerop (TREE_OPERAND (t, 1)))
9150 : : {
9151 : 1641707 : t = TREE_OPERAND (t, 0);
9152 : :
9153 : 1641707 : if (TREE_TYPE (t) != ptrtype)
9154 : 1062486 : t = fold_convert_loc (loc, ptrtype, t);
9155 : : }
9156 : 361662725 : else if (TREE_CODE (t) == MEM_REF
9157 : 361662725 : && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9158 : 658 : return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9159 : : TREE_OPERAND (t, 0),
9160 : : convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9161 : 361662067 : else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9162 : : {
9163 : 25029139 : t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9164 : :
9165 : 25029139 : if (TREE_TYPE (t) != ptrtype)
9166 : 13621 : t = fold_convert_loc (loc, ptrtype, t);
9167 : : }
9168 : : else
9169 : 336632928 : t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9170 : :
9171 : : return t;
9172 : : }
9173 : :
9174 : : /* Build an expression for the address of T. */
9175 : :
9176 : : tree
9177 : 380754076 : build_fold_addr_expr_loc (location_t loc, tree t)
9178 : : {
9179 : 380754076 : tree ptrtype = build_pointer_type (TREE_TYPE (t));
9180 : :
9181 : 380754076 : return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9182 : : }
9183 : :
9184 : : /* Fold a unary expression of code CODE and type TYPE with operand
9185 : : OP0. Return the folded expression if folding is successful.
9186 : : Otherwise, return NULL_TREE. */
9187 : :
9188 : : tree
9189 : 1587978772 : fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9190 : : {
9191 : 1587978772 : tree tem;
9192 : 1587978772 : tree arg0;
9193 : 1587978772 : enum tree_code_class kind = TREE_CODE_CLASS (code);
9194 : :
9195 : 1587978772 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
9196 : : && TREE_CODE_LENGTH (code) == 1);
9197 : :
9198 : 1587978772 : arg0 = op0;
9199 : 1587978772 : if (arg0)
9200 : : {
9201 : 1587965634 : if (CONVERT_EXPR_CODE_P (code)
9202 : : || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9203 : : {
9204 : : /* Don't use STRIP_NOPS, because signedness of argument type
9205 : : matters. */
9206 : 778752855 : STRIP_SIGN_NOPS (arg0);
9207 : : }
9208 : : else
9209 : : {
9210 : : /* Strip any conversions that don't change the mode. This
9211 : : is safe for every expression, except for a comparison
9212 : : expression because its signedness is derived from its
9213 : : operands.
9214 : :
9215 : : Note that this is done as an internal manipulation within
9216 : : the constant folder, in order to find the simplest
9217 : : representation of the arguments so that their form can be
9218 : : studied. In any cases, the appropriate type conversions
9219 : : should be put back in the tree that will get out of the
9220 : : constant folder. */
9221 : 809212779 : STRIP_NOPS (arg0);
9222 : : }
9223 : :
9224 : 1587965634 : if (CONSTANT_CLASS_P (arg0))
9225 : : {
9226 : 227172279 : tree tem = const_unop (code, type, arg0);
9227 : 227172279 : if (tem)
9228 : : {
9229 : 192478910 : if (TREE_TYPE (tem) != type)
9230 : 73799 : tem = fold_convert_loc (loc, type, tem);
9231 : 192478910 : return tem;
9232 : : }
9233 : : }
9234 : : }
9235 : :
9236 : 1395499862 : tem = generic_simplify (loc, code, type, op0);
9237 : 1395499862 : if (tem)
9238 : : return tem;
9239 : :
9240 : 1058208805 : if (TREE_CODE_CLASS (code) == tcc_unary)
9241 : : {
9242 : 532350139 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
9243 : 1004332 : return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9244 : : fold_build1_loc (loc, code, type,
9245 : 1004332 : fold_convert_loc (loc, TREE_TYPE (op0),
9246 : 2008664 : TREE_OPERAND (arg0, 1))));
9247 : 531345807 : else if (TREE_CODE (arg0) == COND_EXPR)
9248 : : {
9249 : 408270 : tree arg01 = TREE_OPERAND (arg0, 1);
9250 : 408270 : tree arg02 = TREE_OPERAND (arg0, 2);
9251 : 408270 : if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9252 : 404088 : arg01 = fold_build1_loc (loc, code, type,
9253 : : fold_convert_loc (loc,
9254 : 404088 : TREE_TYPE (op0), arg01));
9255 : 408270 : if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9256 : 405069 : arg02 = fold_build1_loc (loc, code, type,
9257 : : fold_convert_loc (loc,
9258 : 405069 : TREE_TYPE (op0), arg02));
9259 : 408270 : tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9260 : : arg01, arg02);
9261 : :
9262 : : /* If this was a conversion, and all we did was to move into
9263 : : inside the COND_EXPR, bring it back out. But leave it if
9264 : : it is a conversion from integer to integer and the
9265 : : result precision is no wider than a word since such a
9266 : : conversion is cheap and may be optimized away by combine,
9267 : : while it couldn't if it were outside the COND_EXPR. Then return
9268 : : so we don't get into an infinite recursion loop taking the
9269 : : conversion out and then back in. */
9270 : :
9271 : 408270 : if ((CONVERT_EXPR_CODE_P (code)
9272 : 10004 : || code == NON_LVALUE_EXPR)
9273 : 398285 : && TREE_CODE (tem) == COND_EXPR
9274 : 384593 : && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9275 : 355337 : && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9276 : 167291 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9277 : 167079 : && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9278 : 167079 : && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9279 : 167079 : == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9280 : 580568 : && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9281 : 6051 : && (INTEGRAL_TYPE_P
9282 : : (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9283 : 6011 : && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9284 : 5986 : || flag_syntax_only))
9285 : 160301 : tem = build1_loc (loc, code, type,
9286 : : build3 (COND_EXPR,
9287 : 160301 : TREE_TYPE (TREE_OPERAND
9288 : : (TREE_OPERAND (tem, 1), 0)),
9289 : 160301 : TREE_OPERAND (tem, 0),
9290 : 160301 : TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9291 : 160301 : TREE_OPERAND (TREE_OPERAND (tem, 2),
9292 : : 0)));
9293 : 408270 : return tem;
9294 : : }
9295 : : }
9296 : :
9297 : 1056796203 : switch (code)
9298 : : {
9299 : 38331898 : case NON_LVALUE_EXPR:
9300 : 38331898 : if (!maybe_lvalue_p (op0))
9301 : 28498773 : return fold_convert_loc (loc, type, op0);
9302 : : return NULL_TREE;
9303 : :
9304 : 483748003 : CASE_CONVERT:
9305 : 483748003 : case FLOAT_EXPR:
9306 : 483748003 : case FIX_TRUNC_EXPR:
9307 : 483748003 : if (COMPARISON_CLASS_P (op0))
9308 : : {
9309 : : /* If we have (type) (a CMP b) and type is an integral type, return
9310 : : new expression involving the new type. Canonicalize
9311 : : (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9312 : : non-integral type.
9313 : : Do not fold the result as that would not simplify further, also
9314 : : folding again results in recursions. */
9315 : 373629 : if (TREE_CODE (type) == BOOLEAN_TYPE)
9316 : 76404 : return build2_loc (loc, TREE_CODE (op0), type,
9317 : 76404 : TREE_OPERAND (op0, 0),
9318 : 152808 : TREE_OPERAND (op0, 1));
9319 : 297225 : else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9320 : 5931 : && TREE_CODE (type) != VECTOR_TYPE)
9321 : 5931 : return build3_loc (loc, COND_EXPR, type, op0,
9322 : : constant_boolean_node (true, type),
9323 : 5931 : constant_boolean_node (false, type));
9324 : : }
9325 : :
9326 : : /* Handle (T *)&A.B.C for A being of type T and B and C
9327 : : living at offset zero. This occurs frequently in
9328 : : C++ upcasting and then accessing the base. */
9329 : 483665668 : if (TREE_CODE (op0) == ADDR_EXPR
9330 : 105323523 : && POINTER_TYPE_P (type)
9331 : 582454453 : && handled_component_p (TREE_OPERAND (op0, 0)))
9332 : : {
9333 : 17218001 : poly_int64 bitsize, bitpos;
9334 : 17218001 : tree offset;
9335 : 17218001 : machine_mode mode;
9336 : 17218001 : int unsignedp, reversep, volatilep;
9337 : 17218001 : tree base
9338 : 17218001 : = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9339 : : &offset, &mode, &unsignedp, &reversep,
9340 : : &volatilep);
9341 : : /* If the reference was to a (constant) zero offset, we can use
9342 : : the address of the base if it has the same base type
9343 : : as the result type and the pointer type is unqualified. */
9344 : 17218001 : if (!offset
9345 : 17109289 : && known_eq (bitpos, 0)
9346 : 11808632 : && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9347 : 11808632 : == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9348 : 17229940 : && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9349 : 11738 : return fold_convert_loc (loc, type,
9350 : 11738 : build_fold_addr_expr_loc (loc, base));
9351 : : }
9352 : :
9353 : 483653930 : if (TREE_CODE (op0) == MODIFY_EXPR
9354 : 256525 : && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9355 : : /* Detect assigning a bitfield. */
9356 : 483655929 : && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9357 : 109 : && DECL_BIT_FIELD
9358 : : (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9359 : : {
9360 : : /* Don't leave an assignment inside a conversion
9361 : : unless assigning a bitfield. */
9362 : 1951 : tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9363 : : /* First do the assignment, then return converted constant. */
9364 : 1951 : tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9365 : 1951 : suppress_warning (tem /* What warning? */);
9366 : 1951 : TREE_USED (tem) = 1;
9367 : 1951 : return tem;
9368 : : }
9369 : :
9370 : : /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9371 : : constants (if x has signed type, the sign bit cannot be set
9372 : : in c). This folds extension into the BIT_AND_EXPR.
9373 : : ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9374 : : very likely don't have maximal range for their precision and this
9375 : : transformation effectively doesn't preserve non-maximal ranges. */
9376 : 483651979 : if (TREE_CODE (type) == INTEGER_TYPE
9377 : 217044382 : && TREE_CODE (op0) == BIT_AND_EXPR
9378 : 484156983 : && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9379 : : {
9380 : 269489 : tree and_expr = op0;
9381 : 269489 : tree and0 = TREE_OPERAND (and_expr, 0);
9382 : 269489 : tree and1 = TREE_OPERAND (and_expr, 1);
9383 : 269489 : int change = 0;
9384 : :
9385 : 269489 : if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9386 : 269489 : || (TYPE_PRECISION (type)
9387 : 128066 : <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9388 : : change = 1;
9389 : 76403 : else if (TYPE_PRECISION (TREE_TYPE (and1))
9390 : : <= HOST_BITS_PER_WIDE_INT
9391 : 76403 : && tree_fits_uhwi_p (and1))
9392 : : {
9393 : 75301 : unsigned HOST_WIDE_INT cst;
9394 : :
9395 : 75301 : cst = tree_to_uhwi (and1);
9396 : 150602 : cst &= HOST_WIDE_INT_M1U
9397 : 75301 : << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9398 : 75301 : change = (cst == 0);
9399 : 75301 : if (change
9400 : 75301 : && !flag_syntax_only
9401 : 147767 : && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9402 : : == ZERO_EXTEND))
9403 : : {
9404 : : tree uns = unsigned_type_for (TREE_TYPE (and0));
9405 : : and0 = fold_convert_loc (loc, uns, and0);
9406 : : and1 = fold_convert_loc (loc, uns, and1);
9407 : : }
9408 : : }
9409 : 75301 : if (change)
9410 : : {
9411 : 268387 : tree and1_type = TREE_TYPE (and1);
9412 : 268387 : unsigned prec = MAX (TYPE_PRECISION (and1_type),
9413 : : TYPE_PRECISION (type));
9414 : 268387 : tem = force_fit_type (type,
9415 : 268387 : wide_int::from (wi::to_wide (and1), prec,
9416 : 268387 : TYPE_SIGN (and1_type)),
9417 : 268387 : 0, TREE_OVERFLOW (and1));
9418 : 268387 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
9419 : 268387 : fold_convert_loc (loc, type, and0), tem);
9420 : : }
9421 : : }
9422 : :
9423 : : /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9424 : : cast (T1)X will fold away. We assume that this happens when X itself
9425 : : is a cast. */
9426 : 483383592 : if (POINTER_TYPE_P (type)
9427 : 231217634 : && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9428 : 487646742 : && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9429 : : {
9430 : 2050704 : tree arg00 = TREE_OPERAND (arg0, 0);
9431 : 2050704 : tree arg01 = TREE_OPERAND (arg0, 1);
9432 : :
9433 : : /* If -fsanitize=alignment, avoid this optimization in GENERIC
9434 : : when the pointed type needs higher alignment than
9435 : : the p+ first operand's pointed type. */
9436 : 2050704 : if (!in_gimple_form
9437 : 2037575 : && sanitize_flags_p (SANITIZE_ALIGNMENT)
9438 : 2051918 : && (min_align_of_type (TREE_TYPE (type))
9439 : 607 : > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9440 : : return NULL_TREE;
9441 : :
9442 : : /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9443 : : when type is a reference type and arg00's type is not,
9444 : : because arg00 could be validly nullptr and if arg01 doesn't return,
9445 : : we don't want false positive binding of reference to nullptr. */
9446 : 2050637 : if (TREE_CODE (type) == REFERENCE_TYPE
9447 : 1488991 : && !in_gimple_form
9448 : 1488974 : && sanitize_flags_p (SANITIZE_NULL)
9449 : 2051068 : && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9450 : : return NULL_TREE;
9451 : :
9452 : 2050206 : arg00 = fold_convert_loc (loc, type, arg00);
9453 : 2050206 : return fold_build_pointer_plus_loc (loc, arg00, arg01);
9454 : : }
9455 : :
9456 : : /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9457 : : of the same precision, and X is an integer type not narrower than
9458 : : types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9459 : 481332888 : if (INTEGRAL_TYPE_P (type)
9460 : 222570865 : && TREE_CODE (op0) == BIT_NOT_EXPR
9461 : 510034 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9462 : 510034 : && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9463 : 481646570 : && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9464 : : {
9465 : 312052 : tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9466 : 379314 : if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9467 : 379312 : && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9468 : 251945 : return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9469 : 251945 : fold_convert_loc (loc, type, tem));
9470 : : }
9471 : :
9472 : : /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9473 : : type of X and Y (integer types only). */
9474 : 481080943 : if (INTEGRAL_TYPE_P (type)
9475 : 222318920 : && TREE_CODE (op0) == MULT_EXPR
9476 : 7629162 : && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9477 : 7608160 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9478 : 481141095 : && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9479 : 13718 : || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9480 : : {
9481 : : /* Be careful not to introduce new overflows. */
9482 : 60112 : tree mult_type;
9483 : 60112 : if (TYPE_OVERFLOW_WRAPS (type))
9484 : : mult_type = type;
9485 : : else
9486 : 1967 : mult_type = unsigned_type_for (type);
9487 : :
9488 : 60112 : if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9489 : : {
9490 : 120224 : tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9491 : : fold_convert_loc (loc, mult_type,
9492 : 60112 : TREE_OPERAND (op0, 0)),
9493 : : fold_convert_loc (loc, mult_type,
9494 : 60112 : TREE_OPERAND (op0, 1)));
9495 : 60112 : return fold_convert_loc (loc, type, tem);
9496 : : }
9497 : : }
9498 : :
9499 : : return NULL_TREE;
9500 : :
9501 : 224140767 : case VIEW_CONVERT_EXPR:
9502 : 224140767 : if (TREE_CODE (op0) == MEM_REF)
9503 : : {
9504 : 1 : if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9505 : 1 : type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9506 : 1 : tem = fold_build2_loc (loc, MEM_REF, type,
9507 : 1 : TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9508 : 1 : REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9509 : 1 : return tem;
9510 : : }
9511 : :
9512 : : return NULL_TREE;
9513 : :
9514 : 3598639 : case NEGATE_EXPR:
9515 : 3598639 : tem = fold_negate_expr (loc, arg0);
9516 : 3598639 : if (tem)
9517 : 1552 : return fold_convert_loc (loc, type, tem);
9518 : : return NULL_TREE;
9519 : :
9520 : 2643142 : case ABS_EXPR:
9521 : : /* Convert fabs((double)float) into (double)fabsf(float). */
9522 : 2643142 : if (TREE_CODE (arg0) == NOP_EXPR
9523 : 21823 : && TREE_CODE (type) == REAL_TYPE)
9524 : : {
9525 : 21787 : tree targ0 = strip_float_extensions (arg0);
9526 : 21787 : if (targ0 != arg0)
9527 : 21583 : return fold_convert_loc (loc, type,
9528 : : fold_build1_loc (loc, ABS_EXPR,
9529 : 21583 : TREE_TYPE (targ0),
9530 : 21583 : targ0));
9531 : : }
9532 : : return NULL_TREE;
9533 : :
9534 : 2610153 : case BIT_NOT_EXPR:
9535 : : /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9536 : 2610153 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
9537 : 2611838 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9538 : : fold_convert_loc (loc, type,
9539 : 1685 : TREE_OPERAND (arg0, 0)))))
9540 : 14 : return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9541 : : fold_convert_loc (loc, type,
9542 : 28 : TREE_OPERAND (arg0, 1)));
9543 : 2610139 : else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9544 : 2611810 : && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9545 : : fold_convert_loc (loc, type,
9546 : 1671 : TREE_OPERAND (arg0, 1)))))
9547 : 23 : return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9548 : : fold_convert_loc (loc, type,
9549 : 46 : TREE_OPERAND (arg0, 0)), tem);
9550 : :
9551 : : return NULL_TREE;
9552 : :
9553 : 48113181 : case TRUTH_NOT_EXPR:
9554 : : /* Note that the operand of this must be an int
9555 : : and its values must be 0 or 1.
9556 : : ("true" is a fixed value perhaps depending on the language,
9557 : : but we don't handle values other than 1 correctly yet.) */
9558 : 48113181 : tem = fold_truth_not_expr (loc, arg0);
9559 : 48113181 : if (!tem)
9560 : : return NULL_TREE;
9561 : 33357635 : return fold_convert_loc (loc, type, tem);
9562 : :
9563 : 62553891 : case INDIRECT_REF:
9564 : : /* Fold *&X to X if X is an lvalue. */
9565 : 62553891 : if (TREE_CODE (op0) == ADDR_EXPR)
9566 : : {
9567 : 6321 : tree op00 = TREE_OPERAND (op0, 0);
9568 : 6321 : if ((VAR_P (op00)
9569 : : || TREE_CODE (op00) == PARM_DECL
9570 : : || TREE_CODE (op00) == RESULT_DECL)
9571 : 5135 : && !TREE_READONLY (op00))
9572 : : return op00;
9573 : : }
9574 : : return NULL_TREE;
9575 : :
9576 : : default:
9577 : : return NULL_TREE;
9578 : : } /* switch (code) */
9579 : : }
9580 : :
9581 : :
9582 : : /* If the operation was a conversion do _not_ mark a resulting constant
9583 : : with TREE_OVERFLOW if the original constant was not. These conversions
9584 : : have implementation defined behavior and retaining the TREE_OVERFLOW
9585 : : flag here would confuse later passes such as VRP. */
9586 : : tree
9587 : 0 : fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9588 : : tree type, tree op0)
9589 : : {
9590 : 0 : tree res = fold_unary_loc (loc, code, type, op0);
9591 : 0 : if (res
9592 : 0 : && TREE_CODE (res) == INTEGER_CST
9593 : 0 : && TREE_CODE (op0) == INTEGER_CST
9594 : 0 : && CONVERT_EXPR_CODE_P (code))
9595 : 0 : TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9596 : :
9597 : 0 : return res;
9598 : : }
9599 : :
9600 : : /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9601 : : operands OP0 and OP1. LOC is the location of the resulting expression.
9602 : : ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9603 : : Return the folded expression if folding is successful. Otherwise,
9604 : : return NULL_TREE. */
9605 : : static tree
9606 : 24070001 : fold_truth_andor (location_t loc, enum tree_code code, tree type,
9607 : : tree arg0, tree arg1, tree op0, tree op1)
9608 : : {
9609 : 24070001 : tree tem;
9610 : :
9611 : : /* We only do these simplifications if we are optimizing. */
9612 : 24070001 : if (!optimize)
9613 : : return NULL_TREE;
9614 : :
9615 : : /* Check for things like (A || B) && (A || C). We can convert this
9616 : : to A || (B && C). Note that either operator can be any of the four
9617 : : truth and/or operations and the transformation will still be
9618 : : valid. Also note that we only care about order for the
9619 : : ANDIF and ORIF operators. If B contains side effects, this
9620 : : might change the truth-value of A. */
9621 : 23799690 : if (TREE_CODE (arg0) == TREE_CODE (arg1)
9622 : 5253810 : && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9623 : : || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9624 : : || TREE_CODE (arg0) == TRUTH_AND_EXPR
9625 : 5253810 : || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9626 : 23827693 : && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9627 : : {
9628 : 27485 : tree a00 = TREE_OPERAND (arg0, 0);
9629 : 27485 : tree a01 = TREE_OPERAND (arg0, 1);
9630 : 27485 : tree a10 = TREE_OPERAND (arg1, 0);
9631 : 27485 : tree a11 = TREE_OPERAND (arg1, 1);
9632 : 54970 : bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9633 : 27485 : || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9634 : 27485 : && (code == TRUTH_AND_EXPR
9635 : 8951 : || code == TRUTH_OR_EXPR));
9636 : :
9637 : 27485 : if (operand_equal_p (a00, a10, 0))
9638 : 391 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9639 : 391 : fold_build2_loc (loc, code, type, a01, a11));
9640 : 27094 : else if (commutative && operand_equal_p (a00, a11, 0))
9641 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9642 : 0 : fold_build2_loc (loc, code, type, a01, a10));
9643 : 27094 : else if (commutative && operand_equal_p (a01, a10, 0))
9644 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9645 : 0 : fold_build2_loc (loc, code, type, a00, a11));
9646 : :
9647 : : /* This case if tricky because we must either have commutative
9648 : : operators or else A10 must not have side-effects. */
9649 : :
9650 : 27055 : else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9651 : 53621 : && operand_equal_p (a01, a11, 0))
9652 : 43 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
9653 : : fold_build2_loc (loc, code, type, a00, a10),
9654 : 43 : a01);
9655 : : }
9656 : :
9657 : : /* See if we can build a range comparison. */
9658 : 23799256 : if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9659 : : return tem;
9660 : :
9661 : 22731394 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9662 : 22729410 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9663 : : {
9664 : 20061 : tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9665 : 20061 : if (tem)
9666 : 13 : return fold_build2_loc (loc, code, type, tem, arg1);
9667 : : }
9668 : :
9669 : 22731381 : if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9670 : 22723993 : || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9671 : : {
9672 : 115366 : tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9673 : 115366 : if (tem)
9674 : 76 : return fold_build2_loc (loc, code, type, arg0, tem);
9675 : : }
9676 : :
9677 : : /* Check for the possibility of merging component references. If our
9678 : : lhs is another similar operation, try to merge its rhs with our
9679 : : rhs. Then try to merge our lhs and rhs. */
9680 : 22731305 : if (TREE_CODE (arg0) == code
9681 : 23444804 : && (tem = fold_truth_andor_1 (loc, code, type,
9682 : 713499 : TREE_OPERAND (arg0, 1), arg1)) != 0)
9683 : 85 : return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9684 : :
9685 : 22731220 : if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9686 : : return tem;
9687 : :
9688 : 22691167 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9689 : 22691167 : if (param_logical_op_non_short_circuit != -1)
9690 : 7694 : logical_op_non_short_circuit
9691 : 7694 : = param_logical_op_non_short_circuit;
9692 : 22691167 : if (logical_op_non_short_circuit
9693 : 22687298 : && !sanitize_coverage_p ()
9694 : 22691167 : && (code == TRUTH_AND_EXPR
9695 : 22687295 : || code == TRUTH_ANDIF_EXPR
9696 : 10829167 : || code == TRUTH_OR_EXPR
9697 : 10829167 : || code == TRUTH_ORIF_EXPR))
9698 : : {
9699 : 22687295 : enum tree_code ncode, icode;
9700 : :
9701 : 22687295 : ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9702 : 22687295 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9703 : 11858128 : icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9704 : :
9705 : : /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9706 : : or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9707 : : We don't want to pack more than two leafs to a non-IF AND/OR
9708 : : expression.
9709 : : If tree-code of left-hand operand isn't an AND/OR-IF code and not
9710 : : equal to IF-CODE, then we don't want to add right-hand operand.
9711 : : If the inner right-hand side of left-hand operand has
9712 : : side-effects, or isn't simple, then we can't add to it,
9713 : : as otherwise we might destroy if-sequence. */
9714 : 22687295 : if (TREE_CODE (arg0) == icode
9715 : 705047 : && simple_condition_p (arg1)
9716 : : /* Needed for sequence points to handle trappings, and
9717 : : side-effects. */
9718 : 22735060 : && simple_condition_p (TREE_OPERAND (arg0, 1)))
9719 : : {
9720 : 40993 : tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9721 : : arg1);
9722 : 40993 : return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9723 : 40993 : tem);
9724 : : }
9725 : : /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9726 : : or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9727 : 22646302 : else if (TREE_CODE (arg1) == icode
9728 : 2448 : && simple_condition_p (arg0)
9729 : : /* Needed for sequence points to handle trappings, and
9730 : : side-effects. */
9731 : 22646906 : && simple_condition_p (TREE_OPERAND (arg1, 0)))
9732 : : {
9733 : 21 : tem = fold_build2_loc (loc, ncode, type,
9734 : 21 : arg0, TREE_OPERAND (arg1, 0));
9735 : 21 : return fold_build2_loc (loc, icode, type, tem,
9736 : 42 : TREE_OPERAND (arg1, 1));
9737 : : }
9738 : : /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9739 : : into (A OR B).
9740 : : For sequence point consistancy, we need to check for trapping,
9741 : : and side-effects. */
9742 : 4366101 : else if (code == icode && simple_condition_p (arg0)
9743 : 23339582 : && simple_condition_p (arg1))
9744 : 365507 : return fold_build2_loc (loc, ncode, type, arg0, arg1);
9745 : : }
9746 : :
9747 : : return NULL_TREE;
9748 : : }
9749 : :
9750 : : /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9751 : : by changing CODE to reduce the magnitude of constants involved in
9752 : : ARG0 of the comparison.
9753 : : Returns a canonicalized comparison tree if a simplification was
9754 : : possible, otherwise returns NULL_TREE.
9755 : : Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9756 : : valid if signed overflow is undefined. */
9757 : :
9758 : : static tree
9759 : 163773006 : maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9760 : : tree arg0, tree arg1,
9761 : : bool *strict_overflow_p)
9762 : : {
9763 : 163773006 : enum tree_code code0 = TREE_CODE (arg0);
9764 : 163773006 : tree t, cst0 = NULL_TREE;
9765 : 163773006 : int sgn0;
9766 : :
9767 : : /* Match A +- CST code arg1. We can change this only if overflow
9768 : : is undefined. */
9769 : 163773006 : if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9770 : 124816645 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9771 : : /* In principle pointers also have undefined overflow behavior,
9772 : : but that causes problems elsewhere. */
9773 : 61409831 : && !POINTER_TYPE_P (TREE_TYPE (arg0))
9774 : 61409831 : && (code0 == MINUS_EXPR
9775 : 61409831 : || code0 == PLUS_EXPR)
9776 : 2572198 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9777 : : return NULL_TREE;
9778 : :
9779 : : /* Identify the constant in arg0 and its sign. */
9780 : 2116987 : cst0 = TREE_OPERAND (arg0, 1);
9781 : 2116987 : sgn0 = tree_int_cst_sgn (cst0);
9782 : :
9783 : : /* Overflowed constants and zero will cause problems. */
9784 : 2116987 : if (integer_zerop (cst0)
9785 : 2116987 : || TREE_OVERFLOW (cst0))
9786 : : return NULL_TREE;
9787 : :
9788 : : /* See if we can reduce the magnitude of the constant in
9789 : : arg0 by changing the comparison code. */
9790 : : /* A - CST < arg1 -> A - CST-1 <= arg1. */
9791 : 2116987 : if (code == LT_EXPR
9792 : 1195032 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9793 : : code = LE_EXPR;
9794 : : /* A + CST > arg1 -> A + CST-1 >= arg1. */
9795 : 1918504 : else if (code == GT_EXPR
9796 : 549736 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9797 : : code = GE_EXPR;
9798 : : /* A + CST <= arg1 -> A + CST-1 < arg1. */
9799 : 1749158 : else if (code == LE_EXPR
9800 : 634917 : && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9801 : : code = LT_EXPR;
9802 : : /* A - CST >= arg1 -> A - CST-1 > arg1. */
9803 : 1532608 : else if (code == GE_EXPR
9804 : 500328 : && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9805 : : code = GT_EXPR;
9806 : : else
9807 : : return NULL_TREE;
9808 : 805947 : *strict_overflow_p = true;
9809 : :
9810 : : /* Now build the constant reduced in magnitude. But not if that
9811 : : would produce one outside of its types range. */
9812 : 1611894 : if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9813 : 1611894 : && ((sgn0 == 1
9814 : 385758 : && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9815 : 385758 : && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9816 : 805947 : || (sgn0 == -1
9817 : 420189 : && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9818 : 420189 : && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9819 : 0 : return NULL_TREE;
9820 : :
9821 : 1191705 : t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9822 : 805947 : cst0, build_int_cst (TREE_TYPE (cst0), 1));
9823 : 805947 : t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9824 : 805947 : t = fold_convert (TREE_TYPE (arg1), t);
9825 : :
9826 : 805947 : return fold_build2_loc (loc, code, type, t, arg1);
9827 : : }
9828 : :
9829 : : /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9830 : : overflow further. Try to decrease the magnitude of constants involved
9831 : : by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9832 : : and put sole constants at the second argument position.
9833 : : Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9834 : :
9835 : : static tree
9836 : 82269050 : maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9837 : : tree arg0, tree arg1)
9838 : : {
9839 : 82269050 : tree t;
9840 : 82269050 : bool strict_overflow_p;
9841 : 82269050 : const char * const warnmsg = G_("assuming signed overflow does not occur "
9842 : : "when reducing constant in comparison");
9843 : :
9844 : : /* Try canonicalization by simplifying arg0. */
9845 : 82269050 : strict_overflow_p = false;
9846 : 82269050 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9847 : : &strict_overflow_p);
9848 : 82269050 : if (t)
9849 : : {
9850 : 765094 : if (strict_overflow_p)
9851 : 765094 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9852 : 765094 : return t;
9853 : : }
9854 : :
9855 : : /* Try canonicalization by simplifying arg1 using the swapped
9856 : : comparison. */
9857 : 81503956 : code = swap_tree_comparison (code);
9858 : 81503956 : strict_overflow_p = false;
9859 : 81503956 : t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9860 : : &strict_overflow_p);
9861 : 81503956 : if (t && strict_overflow_p)
9862 : 40853 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9863 : : return t;
9864 : : }
9865 : :
9866 : : /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9867 : : space. This is used to avoid issuing overflow warnings for
9868 : : expressions like &p->x which cannot wrap. */
9869 : :
9870 : : static bool
9871 : 17913 : pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9872 : : {
9873 : 17913 : if (!POINTER_TYPE_P (TREE_TYPE (base)))
9874 : : return true;
9875 : :
9876 : 10605 : if (maybe_lt (bitpos, 0))
9877 : : return true;
9878 : :
9879 : : poly_wide_int wi_offset;
9880 : 9569 : int precision = TYPE_PRECISION (TREE_TYPE (base));
9881 : 9569 : if (offset == NULL_TREE)
9882 : 4697 : wi_offset = wi::zero (precision);
9883 : 4872 : else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9884 : : return true;
9885 : : else
9886 : 0 : wi_offset = wi::to_poly_wide (offset);
9887 : :
9888 : 4697 : wi::overflow_type overflow;
9889 : 4697 : poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9890 : 4697 : precision);
9891 : 4697 : poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9892 : 4697 : if (overflow)
9893 : : return true;
9894 : :
9895 : 4697 : poly_uint64 total_hwi, size;
9896 : 4697 : if (!total.to_uhwi (&total_hwi)
9897 : 4697 : || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9898 : : &size)
9899 : 9300 : || known_eq (size, 0U))
9900 : 94 : return true;
9901 : :
9902 : 4603 : if (known_le (total_hwi, size))
9903 : : return false;
9904 : :
9905 : : /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9906 : : array. */
9907 : 1136 : if (TREE_CODE (base) == ADDR_EXPR
9908 : 0 : && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
9909 : : &size)
9910 : 0 : && maybe_ne (size, 0U)
9911 : 1136 : && known_le (total_hwi, size))
9912 : : return false;
9913 : :
9914 : : return true;
9915 : 9569 : }
9916 : :
9917 : : /* Return a positive integer when the symbol DECL is known to have
9918 : : a nonzero address, zero when it's known not to (e.g., it's a weak
9919 : : symbol), and a negative integer when the symbol is not yet in the
9920 : : symbol table and so whether or not its address is zero is unknown.
9921 : : For function local objects always return positive integer. */
9922 : : static int
9923 : 11120111 : maybe_nonzero_address (tree decl)
9924 : : {
9925 : 11120111 : if (!DECL_P (decl))
9926 : : return -1;
9927 : :
9928 : : /* Normally, don't do anything for variables and functions before symtab is
9929 : : built; it is quite possible that DECL will be declared weak later.
9930 : : But if folding_initializer, we need a constant answer now, so create
9931 : : the symtab entry and prevent later weak declaration. */
9932 : 8876566 : if (decl_in_symtab_p (decl))
9933 : : {
9934 : 3787116 : if (struct symtab_node *symbol
9935 : 3787116 : = (folding_initializer
9936 : 3787116 : ? symtab_node::get_create (decl)
9937 : 3770920 : : symtab_node::get (decl)))
9938 : 3768336 : return symbol->nonzero_address ();
9939 : : }
9940 : 5089450 : else if (folding_cxx_constexpr)
9941 : : /* Anything that doesn't go in the symtab has non-zero address. */
9942 : : return 1;
9943 : :
9944 : : /* Function local objects are never NULL. */
9945 : 5102637 : if (DECL_CONTEXT (decl)
9946 : 5086843 : && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
9947 : 10186168 : && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))
9948 : : return 1;
9949 : :
9950 : : return -1;
9951 : : }
9952 : :
9953 : : /* Subroutine of fold_binary. This routine performs all of the
9954 : : transformations that are common to the equality/inequality
9955 : : operators (EQ_EXPR and NE_EXPR) and the ordering operators
9956 : : (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9957 : : fold_binary should call fold_binary. Fold a comparison with
9958 : : tree code CODE and type TYPE with operands OP0 and OP1. Return
9959 : : the folded comparison or NULL_TREE. */
9960 : :
9961 : : static tree
9962 : 82339662 : fold_comparison (location_t loc, enum tree_code code, tree type,
9963 : : tree op0, tree op1)
9964 : : {
9965 : 82339662 : const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
9966 : 82339662 : tree arg0, arg1, tem;
9967 : :
9968 : 82339662 : arg0 = op0;
9969 : 82339662 : arg1 = op1;
9970 : :
9971 : 82339662 : STRIP_SIGN_NOPS (arg0);
9972 : 82339662 : STRIP_SIGN_NOPS (arg1);
9973 : :
9974 : : /* For comparisons of pointers we can decompose it to a compile time
9975 : : comparison of the base objects and the offsets into the object.
9976 : : This requires at least one operand being an ADDR_EXPR or a
9977 : : POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9978 : 152775947 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
9979 : 82567091 : && (TREE_CODE (arg0) == ADDR_EXPR
9980 : 12083811 : || TREE_CODE (arg1) == ADDR_EXPR
9981 : 10732626 : || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9982 : 10068407 : || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9983 : : {
9984 : 2070434 : tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9985 : 2070434 : poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
9986 : 2070434 : machine_mode mode;
9987 : 2070434 : int volatilep, reversep, unsignedp;
9988 : 2070434 : bool indirect_base0 = false, indirect_base1 = false;
9989 : :
9990 : : /* Get base and offset for the access. Strip ADDR_EXPR for
9991 : : get_inner_reference, but put it back by stripping INDIRECT_REF
9992 : : off the base object if possible. indirect_baseN will be true
9993 : : if baseN is not an address but refers to the object itself. */
9994 : 2070434 : base0 = arg0;
9995 : 2070434 : if (TREE_CODE (arg0) == ADDR_EXPR)
9996 : : {
9997 : 46995 : base0
9998 : 46995 : = get_inner_reference (TREE_OPERAND (arg0, 0),
9999 : : &bitsize, &bitpos0, &offset0, &mode,
10000 : : &unsignedp, &reversep, &volatilep);
10001 : 46995 : if (INDIRECT_REF_P (base0))
10002 : 1725 : base0 = TREE_OPERAND (base0, 0);
10003 : : else
10004 : : indirect_base0 = true;
10005 : : }
10006 : 2023439 : else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10007 : : {
10008 : 729845 : base0 = TREE_OPERAND (arg0, 0);
10009 : 729845 : STRIP_SIGN_NOPS (base0);
10010 : 729845 : if (TREE_CODE (base0) == ADDR_EXPR)
10011 : : {
10012 : 41114 : base0
10013 : 41114 : = get_inner_reference (TREE_OPERAND (base0, 0),
10014 : : &bitsize, &bitpos0, &offset0, &mode,
10015 : : &unsignedp, &reversep, &volatilep);
10016 : 41114 : if (INDIRECT_REF_P (base0))
10017 : 14 : base0 = TREE_OPERAND (base0, 0);
10018 : : else
10019 : : indirect_base0 = true;
10020 : : }
10021 : 729845 : if (offset0 == NULL_TREE || integer_zerop (offset0))
10022 : 729845 : offset0 = TREE_OPERAND (arg0, 1);
10023 : : else
10024 : 0 : offset0 = size_binop (PLUS_EXPR, offset0,
10025 : : TREE_OPERAND (arg0, 1));
10026 : 729845 : if (poly_int_tree_p (offset0))
10027 : : {
10028 : 618062 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10029 : 618062 : TYPE_PRECISION (sizetype));
10030 : 618062 : tem <<= LOG2_BITS_PER_UNIT;
10031 : 618062 : tem += bitpos0;
10032 : 618062 : if (tem.to_shwi (&bitpos0))
10033 : 618059 : offset0 = NULL_TREE;
10034 : : }
10035 : : }
10036 : :
10037 : 2070434 : base1 = arg1;
10038 : 2070434 : if (TREE_CODE (arg1) == ADDR_EXPR)
10039 : : {
10040 : 1376613 : base1
10041 : 1376613 : = get_inner_reference (TREE_OPERAND (arg1, 0),
10042 : : &bitsize, &bitpos1, &offset1, &mode,
10043 : : &unsignedp, &reversep, &volatilep);
10044 : 1376613 : if (INDIRECT_REF_P (base1))
10045 : 67644 : base1 = TREE_OPERAND (base1, 0);
10046 : : else
10047 : : indirect_base1 = true;
10048 : : }
10049 : 693821 : else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10050 : : {
10051 : 72613 : base1 = TREE_OPERAND (arg1, 0);
10052 : 72613 : STRIP_SIGN_NOPS (base1);
10053 : 72613 : if (TREE_CODE (base1) == ADDR_EXPR)
10054 : : {
10055 : 9555 : base1
10056 : 9555 : = get_inner_reference (TREE_OPERAND (base1, 0),
10057 : : &bitsize, &bitpos1, &offset1, &mode,
10058 : : &unsignedp, &reversep, &volatilep);
10059 : 9555 : if (INDIRECT_REF_P (base1))
10060 : 0 : base1 = TREE_OPERAND (base1, 0);
10061 : : else
10062 : : indirect_base1 = true;
10063 : : }
10064 : 72613 : if (offset1 == NULL_TREE || integer_zerop (offset1))
10065 : 72597 : offset1 = TREE_OPERAND (arg1, 1);
10066 : : else
10067 : 16 : offset1 = size_binop (PLUS_EXPR, offset1,
10068 : : TREE_OPERAND (arg1, 1));
10069 : 72613 : if (poly_int_tree_p (offset1))
10070 : : {
10071 : 61690 : poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10072 : 61690 : TYPE_PRECISION (sizetype));
10073 : 61690 : tem <<= LOG2_BITS_PER_UNIT;
10074 : 61690 : tem += bitpos1;
10075 : 61690 : if (tem.to_shwi (&bitpos1))
10076 : 61690 : offset1 = NULL_TREE;
10077 : : }
10078 : : }
10079 : :
10080 : : /* If we have equivalent bases we might be able to simplify. */
10081 : 2070434 : if (indirect_base0 == indirect_base1
10082 : 2771757 : && operand_equal_p (base0, base1,
10083 : : indirect_base0 ? OEP_ADDRESS_OF : 0))
10084 : : {
10085 : : /* We can fold this expression to a constant if the non-constant
10086 : : offset parts are equal. */
10087 : 21191 : if ((offset0 == offset1
10088 : 6854 : || (offset0 && offset1
10089 : 2521 : && operand_equal_p (offset0, offset1, 0)))
10090 : 21191 : && (equality_code
10091 : 10861 : || (indirect_base0
10092 : 7065 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10093 : 3796 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10094 : : {
10095 : 14299 : if (!equality_code
10096 : 10823 : && maybe_ne (bitpos0, bitpos1)
10097 : 25101 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10098 : 1975 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10099 : 9310 : fold_overflow_warning (("assuming pointer wraparound does not "
10100 : : "occur when comparing P +- C1 with "
10101 : : "P +- C2"),
10102 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
10103 : :
10104 : 14299 : switch (code)
10105 : : {
10106 : 56 : case EQ_EXPR:
10107 : 56 : if (known_eq (bitpos0, bitpos1))
10108 : 48982 : return constant_boolean_node (true, type);
10109 : 21 : if (known_ne (bitpos0, bitpos1))
10110 : 21 : return constant_boolean_node (false, type);
10111 : : break;
10112 : 3420 : case NE_EXPR:
10113 : 3420 : if (known_ne (bitpos0, bitpos1))
10114 : 3415 : return constant_boolean_node (true, type);
10115 : 5 : if (known_eq (bitpos0, bitpos1))
10116 : 5 : return constant_boolean_node (false, type);
10117 : : break;
10118 : 2466 : case LT_EXPR:
10119 : 2466 : if (known_lt (bitpos0, bitpos1))
10120 : 2318 : return constant_boolean_node (true, type);
10121 : 148 : if (known_ge (bitpos0, bitpos1))
10122 : 148 : return constant_boolean_node (false, type);
10123 : : break;
10124 : 2163 : case LE_EXPR:
10125 : 2163 : if (known_le (bitpos0, bitpos1))
10126 : 275 : return constant_boolean_node (true, type);
10127 : 1888 : if (known_gt (bitpos0, bitpos1))
10128 : 1888 : return constant_boolean_node (false, type);
10129 : : break;
10130 : 4418 : case GE_EXPR:
10131 : 4418 : if (known_ge (bitpos0, bitpos1))
10132 : 1938 : return constant_boolean_node (true, type);
10133 : 2480 : if (known_lt (bitpos0, bitpos1))
10134 : 2480 : return constant_boolean_node (false, type);
10135 : : break;
10136 : 1776 : case GT_EXPR:
10137 : 1776 : if (known_gt (bitpos0, bitpos1))
10138 : 1723 : return constant_boolean_node (true, type);
10139 : 53 : if (known_le (bitpos0, bitpos1))
10140 : 53 : return constant_boolean_node (false, type);
10141 : : break;
10142 : : default:;
10143 : : }
10144 : : }
10145 : : /* We can simplify the comparison to a comparison of the variable
10146 : : offset parts if the constant offset parts are equal.
10147 : : Be careful to use signed sizetype here because otherwise we
10148 : : mess with array offsets in the wrong way. This is possible
10149 : : because pointer arithmetic is restricted to retain within an
10150 : : object and overflow on pointer differences is undefined as of
10151 : : 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10152 : 6892 : else if (known_eq (bitpos0, bitpos1)
10153 : 6892 : && (equality_code
10154 : 5136 : || (indirect_base0
10155 : 264 : && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10156 : 4872 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10157 : : {
10158 : : /* By converting to signed sizetype we cover middle-end pointer
10159 : : arithmetic which operates on unsigned pointer types of size
10160 : : type size and ARRAY_REF offsets which are properly sign or
10161 : : zero extended from their type in case it is narrower than
10162 : : sizetype. */
10163 : 5250 : if (offset0 == NULL_TREE)
10164 : 0 : offset0 = build_int_cst (ssizetype, 0);
10165 : : else
10166 : 5250 : offset0 = fold_convert_loc (loc, ssizetype, offset0);
10167 : 5250 : if (offset1 == NULL_TREE)
10168 : 2761 : offset1 = build_int_cst (ssizetype, 0);
10169 : : else
10170 : 2489 : offset1 = fold_convert_loc (loc, ssizetype, offset1);
10171 : :
10172 : 5250 : if (!equality_code
10173 : 5250 : && (pointer_may_wrap_p (base0, offset0, bitpos0)
10174 : 0 : || pointer_may_wrap_p (base1, offset1, bitpos1)))
10175 : 5136 : fold_overflow_warning (("assuming pointer wraparound does not "
10176 : : "occur when comparing P +- C1 with "
10177 : : "P +- C2"),
10178 : : WARN_STRICT_OVERFLOW_COMPARISON);
10179 : :
10180 : 5250 : return fold_build2_loc (loc, code, type, offset0, offset1);
10181 : : }
10182 : : }
10183 : : /* For equal offsets we can simplify to a comparison of the
10184 : : base addresses. */
10185 : 2049243 : else if (known_eq (bitpos0, bitpos1)
10186 : 49352 : && (indirect_base0
10187 : 784090 : ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10188 : 11718 : && (indirect_base1
10189 : 131943 : ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10190 : 2215547 : && ((offset0 == offset1)
10191 : 5311 : || (offset0 && offset1
10192 : 5110 : && operand_equal_p (offset0, offset1, 0))))
10193 : : {
10194 : 29370 : if (indirect_base0)
10195 : 1156 : base0 = build_fold_addr_expr_loc (loc, base0);
10196 : 29370 : if (indirect_base1)
10197 : 3292 : base1 = build_fold_addr_expr_loc (loc, base1);
10198 : 29370 : return fold_build2_loc (loc, code, type, base0, base1);
10199 : : }
10200 : : /* Comparison between an ordinary (non-weak) symbol and a null
10201 : : pointer can be eliminated since such symbols must have a non
10202 : : null address. In C, relational expressions between pointers
10203 : : to objects and null pointers are undefined. The results
10204 : : below follow the C++ rules with the additional property that
10205 : : every object pointer compares greater than a null pointer.
10206 : : */
10207 : 2019873 : else if (((DECL_P (base0)
10208 : 203857 : && maybe_nonzero_address (base0) > 0
10209 : : /* Avoid folding references to struct members at offset 0 to
10210 : : prevent tests like '&ptr->firstmember == 0' from getting
10211 : : eliminated. When ptr is null, although the -> expression
10212 : : is strictly speaking invalid, GCC retains it as a matter
10213 : : of QoI. See PR c/44555. */
10214 : 190964 : && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10215 : 1993175 : || CONSTANT_CLASS_P (base0))
10216 : 31366 : && indirect_base0
10217 : : /* The caller guarantees that when one of the arguments is
10218 : : constant (i.e., null in this case) it is second. */
10219 : 2048655 : && integer_zerop (arg1))
10220 : : {
10221 : 63 : switch (code)
10222 : : {
10223 : 24 : case EQ_EXPR:
10224 : 24 : case LE_EXPR:
10225 : 24 : case LT_EXPR:
10226 : 24 : return constant_boolean_node (false, type);
10227 : 39 : case GE_EXPR:
10228 : 39 : case GT_EXPR:
10229 : 39 : case NE_EXPR:
10230 : 39 : return constant_boolean_node (true, type);
10231 : 0 : default:
10232 : 0 : gcc_unreachable ();
10233 : : }
10234 : : }
10235 : : }
10236 : :
10237 : : /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10238 : : X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10239 : : the resulting offset is smaller in absolute value than the
10240 : : original one and has the same sign. */
10241 : 161769913 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10242 : 125625006 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10243 : 31109095 : && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10244 : 2316029 : && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10245 : 1914725 : && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10246 : 1914725 : && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10247 : 145270980 : && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10248 : 165488 : && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10249 : : {
10250 : 165488 : tree const1 = TREE_OPERAND (arg0, 1);
10251 : 165488 : tree const2 = TREE_OPERAND (arg1, 1);
10252 : 165488 : tree variable1 = TREE_OPERAND (arg0, 0);
10253 : 165488 : tree variable2 = TREE_OPERAND (arg1, 0);
10254 : 165488 : tree cst;
10255 : 165488 : const char * const warnmsg = G_("assuming signed overflow does not "
10256 : : "occur when combining constants around "
10257 : : "a comparison");
10258 : :
10259 : : /* Put the constant on the side where it doesn't overflow and is
10260 : : of lower absolute value and of same sign than before. */
10261 : 165489 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10262 : : ? MINUS_EXPR : PLUS_EXPR,
10263 : : const2, const1);
10264 : 165488 : if (!TREE_OVERFLOW (cst)
10265 : 165472 : && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10266 : 187118 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10267 : : {
10268 : 5479 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10269 : 5479 : return fold_build2_loc (loc, code, type,
10270 : : variable1,
10271 : 5479 : fold_build2_loc (loc, TREE_CODE (arg1),
10272 : 5479 : TREE_TYPE (arg1),
10273 : 5479 : variable2, cst));
10274 : : }
10275 : :
10276 : 160010 : cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10277 : : ? MINUS_EXPR : PLUS_EXPR,
10278 : : const1, const2);
10279 : 160009 : if (!TREE_OVERFLOW (cst)
10280 : 159993 : && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10281 : 176160 : && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10282 : : {
10283 : 16151 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10284 : 16151 : return fold_build2_loc (loc, code, type,
10285 : 16151 : fold_build2_loc (loc, TREE_CODE (arg0),
10286 : 16151 : TREE_TYPE (arg0),
10287 : : variable1, cst),
10288 : 16151 : variable2);
10289 : : }
10290 : : }
10291 : :
10292 : 82269050 : tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10293 : 82269050 : if (tem)
10294 : : return tem;
10295 : :
10296 : : /* If we are comparing an expression that just has comparisons
10297 : : of two integer values, arithmetic expressions of those comparisons,
10298 : : and constants, we can simplify it. There are only three cases
10299 : : to check: the two values can either be equal, the first can be
10300 : : greater, or the second can be greater. Fold the expression for
10301 : : those three values. Since each value must be 0 or 1, we have
10302 : : eight possibilities, each of which corresponds to the constant 0
10303 : : or 1 or one of the six possible comparisons.
10304 : :
10305 : : This handles common cases like (a > b) == 0 but also handles
10306 : : expressions like ((x > y) - (y > x)) > 0, which supposedly
10307 : : occur in macroized code. */
10308 : :
10309 : 81463103 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10310 : : {
10311 : 49301685 : tree cval1 = 0, cval2 = 0;
10312 : :
10313 : 49301685 : if (twoval_comparison_p (arg0, &cval1, &cval2)
10314 : : /* Don't handle degenerate cases here; they should already
10315 : : have been handled anyway. */
10316 : 579340 : && cval1 != 0 && cval2 != 0
10317 : 578169 : && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10318 : 578169 : && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10319 : 578163 : && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10320 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10321 : 58 : && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10322 : 49301743 : && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10323 : 58 : TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10324 : : {
10325 : 58 : tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10326 : 58 : tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10327 : :
10328 : : /* We can't just pass T to eval_subst in case cval1 or cval2
10329 : : was the same as ARG1. */
10330 : :
10331 : 58 : tree high_result
10332 : 58 : = fold_build2_loc (loc, code, type,
10333 : : eval_subst (loc, arg0, cval1, maxval,
10334 : : cval2, minval),
10335 : : arg1);
10336 : 58 : tree equal_result
10337 : 58 : = fold_build2_loc (loc, code, type,
10338 : : eval_subst (loc, arg0, cval1, maxval,
10339 : : cval2, maxval),
10340 : : arg1);
10341 : 58 : tree low_result
10342 : 58 : = fold_build2_loc (loc, code, type,
10343 : : eval_subst (loc, arg0, cval1, minval,
10344 : : cval2, maxval),
10345 : : arg1);
10346 : :
10347 : : /* All three of these results should be 0 or 1. Confirm they are.
10348 : : Then use those values to select the proper code to use. */
10349 : :
10350 : 58 : if (TREE_CODE (high_result) == INTEGER_CST
10351 : 49 : && TREE_CODE (equal_result) == INTEGER_CST
10352 : 39 : && TREE_CODE (low_result) == INTEGER_CST)
10353 : : {
10354 : : /* Make a 3-bit mask with the high-order bit being the
10355 : : value for `>', the next for '=', and the low for '<'. */
10356 : 39 : switch ((integer_onep (high_result) * 4)
10357 : 39 : + (integer_onep (equal_result) * 2)
10358 : 39 : + integer_onep (low_result))
10359 : : {
10360 : 21 : case 0:
10361 : : /* Always false. */
10362 : 39 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10363 : : case 1:
10364 : : code = LT_EXPR;
10365 : : break;
10366 : 2 : case 2:
10367 : 2 : code = EQ_EXPR;
10368 : 2 : break;
10369 : 0 : case 3:
10370 : 0 : code = LE_EXPR;
10371 : 0 : break;
10372 : 0 : case 4:
10373 : 0 : code = GT_EXPR;
10374 : 0 : break;
10375 : 1 : case 5:
10376 : 1 : code = NE_EXPR;
10377 : 1 : break;
10378 : 0 : case 6:
10379 : 0 : code = GE_EXPR;
10380 : 0 : break;
10381 : 15 : case 7:
10382 : : /* Always true. */
10383 : 15 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10384 : : }
10385 : :
10386 : 3 : return fold_build2_loc (loc, code, type, cval1, cval2);
10387 : : }
10388 : : }
10389 : : }
10390 : :
10391 : : return NULL_TREE;
10392 : : }
10393 : :
10394 : :
10395 : : /* Subroutine of fold_binary. Optimize complex multiplications of the
10396 : : form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10397 : : argument EXPR represents the expression "z" of type TYPE. */
10398 : :
10399 : : static tree
10400 : 2 : fold_mult_zconjz (location_t loc, tree type, tree expr)
10401 : : {
10402 : 2 : tree itype = TREE_TYPE (type);
10403 : 2 : tree rpart, ipart, tem;
10404 : :
10405 : 2 : if (TREE_CODE (expr) == COMPLEX_EXPR)
10406 : : {
10407 : 0 : rpart = TREE_OPERAND (expr, 0);
10408 : 0 : ipart = TREE_OPERAND (expr, 1);
10409 : : }
10410 : 2 : else if (TREE_CODE (expr) == COMPLEX_CST)
10411 : : {
10412 : 0 : rpart = TREE_REALPART (expr);
10413 : 0 : ipart = TREE_IMAGPART (expr);
10414 : : }
10415 : : else
10416 : : {
10417 : 2 : expr = save_expr (expr);
10418 : 2 : rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10419 : 2 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10420 : : }
10421 : :
10422 : 2 : rpart = save_expr (rpart);
10423 : 2 : ipart = save_expr (ipart);
10424 : 2 : tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10425 : : fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10426 : : fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10427 : 2 : return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10428 : 2 : build_zero_cst (itype));
10429 : : }
10430 : :
10431 : :
10432 : : /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10433 : : CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10434 : : true if successful. */
10435 : :
10436 : : static bool
10437 : 11420 : vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10438 : : {
10439 : 11420 : unsigned HOST_WIDE_INT i, nunits;
10440 : :
10441 : 11420 : if (TREE_CODE (arg) == VECTOR_CST
10442 : 11420 : && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10443 : : {
10444 : 2456 : for (i = 0; i < nunits; ++i)
10445 : 1930 : elts[i] = VECTOR_CST_ELT (arg, i);
10446 : : }
10447 : 10894 : else if (TREE_CODE (arg) == CONSTRUCTOR)
10448 : : {
10449 : : constructor_elt *elt;
10450 : :
10451 : 35594 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10452 : 29935 : if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10453 : 5235 : return false;
10454 : : else
10455 : 24700 : elts[i] = elt->value;
10456 : : }
10457 : : else
10458 : : return false;
10459 : 6959 : for (; i < nelts; i++)
10460 : 1548 : elts[i]
10461 : 774 : = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10462 : : return true;
10463 : : }
10464 : :
10465 : : /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable
10466 : : mask for VLA vec_perm folding.
10467 : : REASON if specified, will contain the reason why SEL is not suitable.
10468 : : Used only for debugging and unit-testing. */
10469 : :
10470 : : static bool
10471 : 7238 : valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1,
10472 : : const vec_perm_indices &sel,
10473 : : const char **reason = NULL)
10474 : : {
10475 : 7238 : unsigned sel_npatterns = sel.encoding ().npatterns ();
10476 : 7238 : unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10477 : :
10478 : 14476 : if (!(pow2p_hwi (sel_npatterns)
10479 : 7238 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0))
10480 : 7238 : && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1))))
10481 : : {
10482 : 0 : if (reason)
10483 : 0 : *reason = "npatterns is not power of 2";
10484 : 0 : return false;
10485 : : }
10486 : :
10487 : : /* We want to avoid cases where sel.length is not a multiple of npatterns.
10488 : : For eg: sel.length = 2 + 2x, and sel npatterns = 4. */
10489 : 7238 : poly_uint64 esel;
10490 : 7238 : if (!multiple_p (sel.length (), sel_npatterns, &esel))
10491 : : {
10492 : 0 : if (reason)
10493 : 0 : *reason = "sel.length is not multiple of sel_npatterns";
10494 : 0 : return false;
10495 : : }
10496 : :
10497 : 7238 : if (sel_nelts_per_pattern < 3)
10498 : : return true;
10499 : :
10500 : 4665 : for (unsigned pattern = 0; pattern < sel_npatterns; pattern++)
10501 : : {
10502 : 3692 : poly_uint64 a1 = sel[pattern + sel_npatterns];
10503 : 3692 : poly_uint64 a2 = sel[pattern + 2 * sel_npatterns];
10504 : 3692 : HOST_WIDE_INT step;
10505 : 3692 : if (!poly_int64 (a2 - a1).is_constant (&step))
10506 : : {
10507 : : if (reason)
10508 : : *reason = "step is not constant";
10509 : 933 : return false;
10510 : : }
10511 : : // FIXME: Punt on step < 0 for now, revisit later.
10512 : 3692 : if (step < 0)
10513 : : return false;
10514 : 3628 : if (step == 0)
10515 : 0 : continue;
10516 : :
10517 : 3628 : if (!pow2p_hwi (step))
10518 : : {
10519 : 0 : if (reason)
10520 : 0 : *reason = "step is not power of 2";
10521 : 0 : return false;
10522 : : }
10523 : :
10524 : : /* Ensure that stepped sequence of the pattern selects elements
10525 : : only from the same input vector. */
10526 : 3628 : uint64_t q1, qe;
10527 : 3628 : poly_uint64 r1, re;
10528 : 3628 : poly_uint64 ae = a1 + (esel - 2) * step;
10529 : 3628 : poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10530 : :
10531 : 3628 : if (!(can_div_trunc_p (a1, arg_len, &q1, &r1)
10532 : 3628 : && can_div_trunc_p (ae, arg_len, &qe, &re)
10533 : : && q1 == qe))
10534 : : {
10535 : 331 : if (reason)
10536 : 0 : *reason = "crossed input vectors";
10537 : 331 : return false;
10538 : : }
10539 : :
10540 : : /* Ensure that the stepped sequence always selects from the same
10541 : : input pattern. */
10542 : 3297 : tree arg = ((q1 & 1) == 0) ? arg0 : arg1;
10543 : 3297 : unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg);
10544 : :
10545 : 3297 : if (!multiple_p (step, arg_npatterns))
10546 : : {
10547 : 536 : if (reason)
10548 : 0 : *reason = "step is not multiple of npatterns";
10549 : 536 : return false;
10550 : : }
10551 : :
10552 : : /* If a1 chooses base element from arg, ensure that it's a natural
10553 : : stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0])
10554 : : to preserve arg's encoding. */
10555 : :
10556 : 2761 : if (maybe_lt (r1, arg_npatterns))
10557 : : {
10558 : 2 : unsigned HOST_WIDE_INT index;
10559 : 2 : if (!r1.is_constant (&index))
10560 : 2 : return false;
10561 : :
10562 : 2 : tree arg_elem0 = vector_cst_elt (arg, index);
10563 : 2 : tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns);
10564 : 2 : tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2);
10565 : :
10566 : 2 : tree step1, step2;
10567 : 2 : if (!(step1 = const_binop (MINUS_EXPR, arg_elem1, arg_elem0))
10568 : 2 : || !(step2 = const_binop (MINUS_EXPR, arg_elem2, arg_elem1))
10569 : 4 : || !operand_equal_p (step1, step2, 0))
10570 : : {
10571 : 2 : if (reason)
10572 : 0 : *reason = "not a natural stepped sequence";
10573 : 2 : return false;
10574 : : }
10575 : : }
10576 : : }
10577 : :
10578 : : return true;
10579 : : }
10580 : :
10581 : : /* Try to fold permutation of ARG0 and ARG1 with SEL selector when
10582 : : the input vectors are VECTOR_CST. Return NULL_TREE otherwise.
10583 : : REASON has same purpose as described in
10584 : : valid_mask_for_fold_vec_perm_cst_p. */
10585 : :
10586 : : static tree
10587 : 7238 : fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel,
10588 : : const char **reason = NULL)
10589 : : {
10590 : 7238 : unsigned res_npatterns, res_nelts_per_pattern;
10591 : 7238 : unsigned HOST_WIDE_INT res_nelts;
10592 : :
10593 : : /* First try to implement the fold in a VLA-friendly way.
10594 : :
10595 : : (1) If the selector is simply a duplication of N elements, the
10596 : : result is likewise a duplication of N elements.
10597 : :
10598 : : (2) If the selector is N elements followed by a duplication
10599 : : of N elements, the result is too.
10600 : :
10601 : : (3) If the selector is N elements followed by an interleaving
10602 : : of N linear series, the situation is more complex.
10603 : :
10604 : : valid_mask_for_fold_vec_perm_cst_p detects whether we
10605 : : can handle this case. If we can, then each of the N linear
10606 : : series either (a) selects the same element each time or
10607 : : (b) selects a linear series from one of the input patterns.
10608 : :
10609 : : If (b) holds for one of the linear series, the result
10610 : : will contain a linear series, and so the result will have
10611 : : the same shape as the selector. If (a) holds for all of
10612 : : the linear series, the result will be the same as (2) above.
10613 : :
10614 : : (b) can only hold if one of the input patterns has a
10615 : : stepped encoding. */
10616 : :
10617 : 7238 : if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason))
10618 : : {
10619 : 6305 : res_npatterns = sel.encoding ().npatterns ();
10620 : 6305 : res_nelts_per_pattern = sel.encoding ().nelts_per_pattern ();
10621 : 6305 : if (res_nelts_per_pattern == 3
10622 : 973 : && VECTOR_CST_NELTS_PER_PATTERN (arg0) < 3
10623 : 6811 : && VECTOR_CST_NELTS_PER_PATTERN (arg1) < 3)
10624 : : res_nelts_per_pattern = 2;
10625 : 6305 : res_nelts = res_npatterns * res_nelts_per_pattern;
10626 : : }
10627 : 933 : else if (TYPE_VECTOR_SUBPARTS (type).is_constant (&res_nelts))
10628 : : {
10629 : 933 : res_npatterns = res_nelts;
10630 : 933 : res_nelts_per_pattern = 1;
10631 : : }
10632 : : else
10633 : : return NULL_TREE;
10634 : :
10635 : 7238 : tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern);
10636 : 45979 : for (unsigned i = 0; i < res_nelts; i++)
10637 : : {
10638 : 38741 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
10639 : 38741 : uint64_t q;
10640 : 38741 : poly_uint64 r;
10641 : 38741 : unsigned HOST_WIDE_INT index;
10642 : :
10643 : : /* Punt if sel[i] /trunc_div len cannot be determined,
10644 : : because the input vector to be chosen will depend on
10645 : : runtime vector length.
10646 : : For example if len == 4 + 4x, and sel[i] == 4,
10647 : : If len at runtime equals 4, we choose arg1[0].
10648 : : For any other value of len > 4 at runtime, we choose arg0[4].
10649 : : which makes the element choice dependent on runtime vector length. */
10650 : 38741 : if (!can_div_trunc_p (sel[i], len, &q, &r))
10651 : : {
10652 : : if (reason)
10653 : : *reason = "cannot divide selector element by arg len";
10654 : : return NULL_TREE;
10655 : : }
10656 : :
10657 : : /* sel[i] % len will give the index of element in the chosen input
10658 : : vector. For example if sel[i] == 5 + 4x and len == 4 + 4x,
10659 : : we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */
10660 : 38741 : if (!r.is_constant (&index))
10661 : : {
10662 : : if (reason)
10663 : : *reason = "remainder is not constant";
10664 : : return NULL_TREE;
10665 : : }
10666 : :
10667 : 38741 : tree arg = ((q & 1) == 0) ? arg0 : arg1;
10668 : 38741 : tree elem = vector_cst_elt (arg, index);
10669 : 38741 : out_elts.quick_push (elem);
10670 : : }
10671 : :
10672 : 7238 : return out_elts.build ();
10673 : 7238 : }
10674 : :
10675 : : /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10676 : : selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10677 : : NULL_TREE otherwise. */
10678 : :
10679 : : tree
10680 : 25403 : fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10681 : : {
10682 : 25403 : unsigned int i;
10683 : 25403 : unsigned HOST_WIDE_INT nelts;
10684 : :
10685 : 25403 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ())
10686 : : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
10687 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))));
10688 : :
10689 : 25403 : if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10690 : 25403 : || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10691 : : return NULL_TREE;
10692 : :
10693 : 15538 : if (TREE_CODE (arg0) == VECTOR_CST
10694 : 7591 : && TREE_CODE (arg1) == VECTOR_CST)
10695 : 7238 : return fold_vec_perm_cst (type, arg0, arg1, sel);
10696 : :
10697 : : /* For fall back case, we want to ensure we have VLS vectors
10698 : : with equal length. */
10699 : 8300 : if (!sel.length ().is_constant (&nelts))
10700 : : return NULL_TREE;
10701 : :
10702 : 8300 : gcc_assert (known_eq (sel.length (),
10703 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))));
10704 : 8300 : tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10705 : 8300 : if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10706 : 8300 : || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10707 : 5235 : return NULL_TREE;
10708 : :
10709 : 3065 : vec<constructor_elt, va_gc> *v;
10710 : 3065 : vec_alloc (v, nelts);
10711 : 16547 : for (i = 0; i < nelts; i++)
10712 : : {
10713 : 13482 : HOST_WIDE_INT index;
10714 : 13482 : if (!sel[i].is_constant (&index))
10715 : : return NULL_TREE;
10716 : 13482 : CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]);
10717 : : }
10718 : 3065 : return build_constructor (type, v);
10719 : : }
10720 : :
10721 : : /* Try to fold a pointer difference of type TYPE two address expressions of
10722 : : array references AREF0 and AREF1 using location LOC. Return a
10723 : : simplified expression for the difference or NULL_TREE. */
10724 : :
10725 : : static tree
10726 : 39 : fold_addr_of_array_ref_difference (location_t loc, tree type,
10727 : : tree aref0, tree aref1,
10728 : : bool use_pointer_diff)
10729 : : {
10730 : 39 : tree base0 = TREE_OPERAND (aref0, 0);
10731 : 39 : tree base1 = TREE_OPERAND (aref1, 0);
10732 : 39 : tree base_offset = build_int_cst (type, 0);
10733 : :
10734 : : /* If the bases are array references as well, recurse. If the bases
10735 : : are pointer indirections compute the difference of the pointers.
10736 : : If the bases are equal, we are set. */
10737 : 39 : if ((TREE_CODE (base0) == ARRAY_REF
10738 : 1 : && TREE_CODE (base1) == ARRAY_REF
10739 : 1 : && (base_offset
10740 : 1 : = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10741 : : use_pointer_diff)))
10742 : 38 : || (INDIRECT_REF_P (base0)
10743 : 7 : && INDIRECT_REF_P (base1)
10744 : 7 : && (base_offset
10745 : : = use_pointer_diff
10746 : 8 : ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10747 : 1 : TREE_OPERAND (base0, 0),
10748 : 1 : TREE_OPERAND (base1, 0))
10749 : 12 : : fold_binary_loc (loc, MINUS_EXPR, type,
10750 : 6 : fold_convert (type,
10751 : : TREE_OPERAND (base0, 0)),
10752 : 6 : fold_convert (type,
10753 : : TREE_OPERAND (base1, 0)))))
10754 : 70 : || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10755 : : {
10756 : 15 : tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10757 : 15 : tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10758 : 15 : tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10759 : 15 : tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10760 : 15 : return fold_build2_loc (loc, PLUS_EXPR, type,
10761 : : base_offset,
10762 : : fold_build2_loc (loc, MULT_EXPR, type,
10763 : 15 : diff, esz));
10764 : : }
10765 : : return NULL_TREE;
10766 : : }
10767 : :
10768 : : /* If the real or vector real constant CST of type TYPE has an exact
10769 : : inverse, return it, else return NULL. */
10770 : :
10771 : : tree
10772 : 1198688 : exact_inverse (tree type, tree cst)
10773 : : {
10774 : 1198688 : REAL_VALUE_TYPE r;
10775 : 1198688 : tree unit_type;
10776 : 1198688 : machine_mode mode;
10777 : :
10778 : 1198688 : switch (TREE_CODE (cst))
10779 : : {
10780 : 1198163 : case REAL_CST:
10781 : 1198163 : r = TREE_REAL_CST (cst);
10782 : :
10783 : 1198163 : if (exact_real_inverse (TYPE_MODE (type), &r))
10784 : 338802 : return build_real (type, r);
10785 : :
10786 : : return NULL_TREE;
10787 : :
10788 : 525 : case VECTOR_CST:
10789 : 525 : {
10790 : 525 : unit_type = TREE_TYPE (type);
10791 : 525 : mode = TYPE_MODE (unit_type);
10792 : :
10793 : 525 : tree_vector_builder elts;
10794 : 525 : if (!elts.new_unary_operation (type, cst, false))
10795 : : return NULL_TREE;
10796 : 525 : unsigned int count = elts.encoded_nelts ();
10797 : 585 : for (unsigned int i = 0; i < count; ++i)
10798 : : {
10799 : 525 : r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10800 : 525 : if (!exact_real_inverse (mode, &r))
10801 : : return NULL_TREE;
10802 : 60 : elts.quick_push (build_real (unit_type, r));
10803 : : }
10804 : :
10805 : 60 : return elts.build ();
10806 : 525 : }
10807 : :
10808 : : default:
10809 : : return NULL_TREE;
10810 : : }
10811 : : }
10812 : :
10813 : : /* Mask out the tz least significant bits of X of type TYPE where
10814 : : tz is the number of trailing zeroes in Y. */
10815 : : static wide_int
10816 : 104849 : mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10817 : : {
10818 : 104849 : int tz = wi::ctz (y);
10819 : 104849 : if (tz > 0)
10820 : 8014 : return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10821 : 96835 : return x;
10822 : : }
10823 : :
10824 : : /* Return true when T is an address and is known to be nonzero.
10825 : : For floating point we further ensure that T is not denormal.
10826 : : Similar logic is present in nonzero_address in rtlanal.h.
10827 : :
10828 : : If the return value is based on the assumption that signed overflow
10829 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10830 : : change *STRICT_OVERFLOW_P. */
10831 : :
10832 : : static bool
10833 : 142681447 : tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10834 : : {
10835 : 142977597 : tree type = TREE_TYPE (t);
10836 : 142977597 : enum tree_code code;
10837 : :
10838 : : /* Doing something useful for floating point would need more work. */
10839 : 142977597 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10840 : : return false;
10841 : :
10842 : 142882972 : code = TREE_CODE (t);
10843 : 142882972 : switch (TREE_CODE_CLASS (code))
10844 : : {
10845 : 800860 : case tcc_unary:
10846 : 800860 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10847 : 800860 : strict_overflow_p);
10848 : 2857546 : case tcc_binary:
10849 : 2857546 : case tcc_comparison:
10850 : 2857546 : return tree_binary_nonzero_warnv_p (code, type,
10851 : 2857546 : TREE_OPERAND (t, 0),
10852 : 2857546 : TREE_OPERAND (t, 1),
10853 : 2857546 : strict_overflow_p);
10854 : 11736854 : case tcc_constant:
10855 : 11736854 : case tcc_declaration:
10856 : 11736854 : case tcc_reference:
10857 : 11736854 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10858 : :
10859 : 127487712 : default:
10860 : 127487712 : break;
10861 : : }
10862 : :
10863 : 127487712 : switch (code)
10864 : : {
10865 : 577722 : case TRUTH_NOT_EXPR:
10866 : 577722 : return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10867 : 577722 : strict_overflow_p);
10868 : :
10869 : 76144 : case TRUTH_AND_EXPR:
10870 : 76144 : case TRUTH_OR_EXPR:
10871 : 76144 : case TRUTH_XOR_EXPR:
10872 : 76144 : return tree_binary_nonzero_warnv_p (code, type,
10873 : 76144 : TREE_OPERAND (t, 0),
10874 : 76144 : TREE_OPERAND (t, 1),
10875 : 76144 : strict_overflow_p);
10876 : :
10877 : 123944203 : case COND_EXPR:
10878 : 123944203 : case CONSTRUCTOR:
10879 : 123944203 : case OBJ_TYPE_REF:
10880 : 123944203 : case ADDR_EXPR:
10881 : 123944203 : case WITH_SIZE_EXPR:
10882 : 123944203 : case SSA_NAME:
10883 : 123944203 : return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10884 : :
10885 : 80720 : case COMPOUND_EXPR:
10886 : 80720 : case MODIFY_EXPR:
10887 : 80720 : case BIND_EXPR:
10888 : 80720 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10889 : 80720 : strict_overflow_p);
10890 : :
10891 : 215430 : case SAVE_EXPR:
10892 : 215430 : return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10893 : 215430 : strict_overflow_p);
10894 : :
10895 : 2512383 : case CALL_EXPR:
10896 : 2512383 : {
10897 : 2512383 : tree fndecl = get_callee_fndecl (t);
10898 : 2512383 : if (!fndecl) return false;
10899 : 2510677 : if (flag_delete_null_pointer_checks && !flag_check_new
10900 : 2510668 : && DECL_IS_OPERATOR_NEW_P (fndecl)
10901 : 2511401 : && !TREE_NOTHROW (fndecl))
10902 : : return true;
10903 : 2511311 : if (flag_delete_null_pointer_checks
10904 : 5021952 : && lookup_attribute ("returns_nonnull",
10905 : 2510641 : TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10906 : : return true;
10907 : 2511303 : return alloca_call_p (t);
10908 : : }
10909 : :
10910 : : default:
10911 : : break;
10912 : : }
10913 : : return false;
10914 : : }
10915 : :
10916 : : /* Return true when T is an address and is known to be nonzero.
10917 : : Handle warnings about undefined signed overflow. */
10918 : :
10919 : : bool
10920 : 141597680 : tree_expr_nonzero_p (tree t)
10921 : : {
10922 : 141597680 : bool ret, strict_overflow_p;
10923 : :
10924 : 141597680 : strict_overflow_p = false;
10925 : 141597680 : ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10926 : 141597680 : if (strict_overflow_p)
10927 : 0 : fold_overflow_warning (("assuming signed overflow does not occur when "
10928 : : "determining that expression is always "
10929 : : "non-zero"),
10930 : : WARN_STRICT_OVERFLOW_MISC);
10931 : 141597680 : return ret;
10932 : : }
10933 : :
10934 : : /* Return true if T is known not to be equal to an integer W. */
10935 : :
10936 : : bool
10937 : 97896366 : expr_not_equal_to (tree t, const wide_int &w)
10938 : : {
10939 : 97896366 : int_range_max vr;
10940 : 97896366 : switch (TREE_CODE (t))
10941 : : {
10942 : 1078970 : case INTEGER_CST:
10943 : 1078970 : return wi::to_wide (t) != w;
10944 : :
10945 : 96816341 : case SSA_NAME:
10946 : 96816341 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10947 : : return false;
10948 : :
10949 : 193632682 : get_range_query (cfun)->range_of_expr (vr, t);
10950 : 96816341 : if (!vr.undefined_p () && !vr.contains_p (w))
10951 : : return true;
10952 : : /* If T has some known zero bits and W has any of those bits set,
10953 : : then T is known not to be equal to W. */
10954 : 96713862 : if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10955 : 193427348 : TYPE_PRECISION (TREE_TYPE (t))), 0))
10956 : : return true;
10957 : : return false;
10958 : :
10959 : : default:
10960 : : return false;
10961 : : }
10962 : 97896366 : }
10963 : :
10964 : : /* Fold a binary expression of code CODE and type TYPE with operands
10965 : : OP0 and OP1. LOC is the location of the resulting expression.
10966 : : Return the folded expression if folding is successful. Otherwise,
10967 : : return NULL_TREE. */
10968 : :
10969 : : tree
10970 : 780451514 : fold_binary_loc (location_t loc, enum tree_code code, tree type,
10971 : : tree op0, tree op1)
10972 : : {
10973 : 780451514 : enum tree_code_class kind = TREE_CODE_CLASS (code);
10974 : 780451514 : tree arg0, arg1, tem;
10975 : 780451514 : tree t1 = NULL_TREE;
10976 : 780451514 : bool strict_overflow_p;
10977 : 780451514 : unsigned int prec;
10978 : :
10979 : 780451514 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
10980 : : && TREE_CODE_LENGTH (code) == 2
10981 : : && op0 != NULL_TREE
10982 : : && op1 != NULL_TREE);
10983 : :
10984 : 780451514 : arg0 = op0;
10985 : 780451514 : arg1 = op1;
10986 : :
10987 : : /* Strip any conversions that don't change the mode. This is
10988 : : safe for every expression, except for a comparison expression
10989 : : because its signedness is derived from its operands. So, in
10990 : : the latter case, only strip conversions that don't change the
10991 : : signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10992 : : preserved.
10993 : :
10994 : : Note that this is done as an internal manipulation within the
10995 : : constant folder, in order to find the simplest representation
10996 : : of the arguments so that their form can be studied. In any
10997 : : cases, the appropriate type conversions should be put back in
10998 : : the tree that will get out of the constant folder. */
10999 : :
11000 : 780451514 : if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
11001 : : {
11002 : 169661300 : STRIP_SIGN_NOPS (arg0);
11003 : 169661300 : STRIP_SIGN_NOPS (arg1);
11004 : : }
11005 : : else
11006 : : {
11007 : 610790214 : STRIP_NOPS (arg0);
11008 : 610790214 : STRIP_NOPS (arg1);
11009 : : }
11010 : :
11011 : : /* Note that TREE_CONSTANT isn't enough: static var addresses are
11012 : : constant but we can't do arithmetic on them. */
11013 : 780451514 : if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
11014 : : {
11015 : 192067669 : tem = const_binop (code, type, arg0, arg1);
11016 : 192067669 : if (tem != NULL_TREE)
11017 : : {
11018 : 189367344 : if (TREE_TYPE (tem) != type)
11019 : 1786184 : tem = fold_convert_loc (loc, type, tem);
11020 : 189367344 : return tem;
11021 : : }
11022 : : }
11023 : :
11024 : : /* If this is a commutative operation, and ARG0 is a constant, move it
11025 : : to ARG1 to reduce the number of tests below. */
11026 : 591084170 : if (commutative_tree_code (code)
11027 : 591084170 : && tree_swap_operands_p (arg0, arg1))
11028 : 32239344 : return fold_build2_loc (loc, code, type, op1, op0);
11029 : :
11030 : : /* Likewise if this is a comparison, and ARG0 is a constant, move it
11031 : : to ARG1 to reduce the number of tests below. */
11032 : 558844826 : if (kind == tcc_comparison
11033 : 558844826 : && tree_swap_operands_p (arg0, arg1))
11034 : 7405725 : return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
11035 : :
11036 : 551439101 : tem = generic_simplify (loc, code, type, op0, op1);
11037 : 551439101 : if (tem)
11038 : : return tem;
11039 : :
11040 : : /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
11041 : :
11042 : : First check for cases where an arithmetic operation is applied to a
11043 : : compound, conditional, or comparison operation. Push the arithmetic
11044 : : operation inside the compound or conditional to see if any folding
11045 : : can then be done. Convert comparison to conditional for this purpose.
11046 : : The also optimizes non-constant cases that used to be done in
11047 : : expand_expr.
11048 : :
11049 : : Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
11050 : : one of the operands is a comparison and the other is a comparison, a
11051 : : BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
11052 : : code below would make the expression more complex. Change it to a
11053 : : TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
11054 : : TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
11055 : :
11056 : 465695281 : if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
11057 : : || code == EQ_EXPR || code == NE_EXPR)
11058 : 53598739 : && !VECTOR_TYPE_P (TREE_TYPE (arg0))
11059 : 53044304 : && ((truth_value_p (TREE_CODE (arg0))
11060 : 1188276 : && (truth_value_p (TREE_CODE (arg1))
11061 : 892267 : || (TREE_CODE (arg1) == BIT_AND_EXPR
11062 : 40 : && integer_onep (TREE_OPERAND (arg1, 1)))))
11063 : 52748279 : || (truth_value_p (TREE_CODE (arg1))
11064 : 6547 : && (truth_value_p (TREE_CODE (arg0))
11065 : 6547 : || (TREE_CODE (arg0) == BIT_AND_EXPR
11066 : 167 : && integer_onep (TREE_OPERAND (arg0, 1)))))))
11067 : : {
11068 : 334370 : tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
11069 : 38331 : : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
11070 : : : TRUTH_XOR_EXPR,
11071 : : boolean_type_node,
11072 : : fold_convert_loc (loc, boolean_type_node, arg0),
11073 : : fold_convert_loc (loc, boolean_type_node, arg1));
11074 : :
11075 : 296039 : if (code == EQ_EXPR)
11076 : 32238 : tem = invert_truthvalue_loc (loc, tem);
11077 : :
11078 : 296039 : return fold_convert_loc (loc, type, tem);
11079 : : }
11080 : :
11081 : 465399242 : if (TREE_CODE_CLASS (code) == tcc_binary
11082 : 268586860 : || TREE_CODE_CLASS (code) == tcc_comparison)
11083 : : {
11084 : 285042807 : if (TREE_CODE (arg0) == COMPOUND_EXPR)
11085 : : {
11086 : 79838 : tem = fold_build2_loc (loc, code, type,
11087 : 79838 : fold_convert_loc (loc, TREE_TYPE (op0),
11088 : 79838 : TREE_OPERAND (arg0, 1)), op1);
11089 : 79838 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
11090 : 79838 : tem);
11091 : : }
11092 : 284962969 : if (TREE_CODE (arg1) == COMPOUND_EXPR)
11093 : : {
11094 : 3132 : tem = fold_build2_loc (loc, code, type, op0,
11095 : 3132 : fold_convert_loc (loc, TREE_TYPE (op1),
11096 : 3132 : TREE_OPERAND (arg1, 1)));
11097 : 3132 : return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
11098 : 3132 : tem);
11099 : : }
11100 : :
11101 : 284959837 : if (TREE_CODE (arg0) == COND_EXPR
11102 : 284589990 : || TREE_CODE (arg0) == VEC_COND_EXPR
11103 : 284587755 : || COMPARISON_CLASS_P (arg0))
11104 : : {
11105 : 726187 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11106 : : arg0, arg1,
11107 : : /*cond_first_p=*/1);
11108 : 726187 : if (tem != NULL_TREE)
11109 : : return tem;
11110 : : }
11111 : :
11112 : 284480443 : if (TREE_CODE (arg1) == COND_EXPR
11113 : 284256621 : || TREE_CODE (arg1) == VEC_COND_EXPR
11114 : 284256296 : || COMPARISON_CLASS_P (arg1))
11115 : : {
11116 : 234194 : tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11117 : : arg1, arg0,
11118 : : /*cond_first_p=*/0);
11119 : 234194 : if (tem != NULL_TREE)
11120 : : return tem;
11121 : : }
11122 : : }
11123 : :
11124 : 464828628 : switch (code)
11125 : : {
11126 : 51128771 : case MEM_REF:
11127 : : /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11128 : 51128771 : if (TREE_CODE (arg0) == ADDR_EXPR
11129 : 51128771 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11130 : : {
11131 : 887001 : tree iref = TREE_OPERAND (arg0, 0);
11132 : 887001 : return fold_build2 (MEM_REF, type,
11133 : : TREE_OPERAND (iref, 0),
11134 : : int_const_binop (PLUS_EXPR, arg1,
11135 : : TREE_OPERAND (iref, 1)));
11136 : : }
11137 : :
11138 : : /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11139 : 50241770 : if (TREE_CODE (arg0) == ADDR_EXPR
11140 : 50241770 : && handled_component_p (TREE_OPERAND (arg0, 0)))
11141 : : {
11142 : 2303083 : tree base;
11143 : 2303083 : poly_int64 coffset;
11144 : 2303083 : base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11145 : : &coffset);
11146 : 2303083 : if (!base)
11147 : : return NULL_TREE;
11148 : 2299566 : return fold_build2 (MEM_REF, type,
11149 : : build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11150 : : int_const_binop (PLUS_EXPR, arg1,
11151 : : size_int (coffset)));
11152 : : }
11153 : :
11154 : : return NULL_TREE;
11155 : :
11156 : 30141184 : case POINTER_PLUS_EXPR:
11157 : : /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11158 : 60281948 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11159 : 60272618 : && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11160 : 35100 : return fold_convert_loc (loc, type,
11161 : : fold_build2_loc (loc, PLUS_EXPR, sizetype,
11162 : : fold_convert_loc (loc, sizetype,
11163 : : arg1),
11164 : : fold_convert_loc (loc, sizetype,
11165 : 35100 : arg0)));
11166 : :
11167 : : return NULL_TREE;
11168 : :
11169 : 59026131 : case PLUS_EXPR:
11170 : 59026131 : if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11171 : : {
11172 : : /* X + (X / CST) * -CST is X % CST. */
11173 : 47342433 : if (TREE_CODE (arg1) == MULT_EXPR
11174 : 2240991 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11175 : 47348558 : && operand_equal_p (arg0,
11176 : 6125 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11177 : : {
11178 : 172 : tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11179 : 172 : tree cst1 = TREE_OPERAND (arg1, 1);
11180 : 172 : tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11181 : : cst1, cst0);
11182 : 172 : if (sum && integer_zerop (sum))
11183 : 172 : return fold_convert_loc (loc, type,
11184 : : fold_build2_loc (loc, TRUNC_MOD_EXPR,
11185 : 172 : TREE_TYPE (arg0), arg0,
11186 : 172 : cst0));
11187 : : }
11188 : : }
11189 : :
11190 : : /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11191 : : one. Make sure the type is not saturating and has the signedness of
11192 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11193 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11194 : 59025959 : if ((TREE_CODE (arg0) == MULT_EXPR
11195 : 47898696 : || TREE_CODE (arg1) == MULT_EXPR)
11196 : 12453606 : && !TYPE_SATURATING (type)
11197 : 12453606 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11198 : 12074097 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11199 : 70456171 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11200 : : {
11201 : 8128857 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11202 : 8128857 : if (tem)
11203 : : return tem;
11204 : : }
11205 : :
11206 : 57858022 : if (! FLOAT_TYPE_P (type))
11207 : : {
11208 : : /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11209 : : (plus (plus (mult) (mult)) (foo)) so that we can
11210 : : take advantage of the factoring cases below. */
11211 : 272976 : if (ANY_INTEGRAL_TYPE_P (type)
11212 : 46176672 : && TYPE_OVERFLOW_WRAPS (type)
11213 : 46176672 : && (((TREE_CODE (arg0) == PLUS_EXPR
11214 : 28786794 : || TREE_CODE (arg0) == MINUS_EXPR)
11215 : 3251805 : && TREE_CODE (arg1) == MULT_EXPR)
11216 : 28293840 : || ((TREE_CODE (arg1) == PLUS_EXPR
11217 : 28293840 : || TREE_CODE (arg1) == MINUS_EXPR)
11218 : 401077 : && TREE_CODE (arg0) == MULT_EXPR)))
11219 : : {
11220 : 539622 : tree parg0, parg1, parg, marg;
11221 : 539622 : enum tree_code pcode;
11222 : :
11223 : 539622 : if (TREE_CODE (arg1) == MULT_EXPR)
11224 : : parg = arg0, marg = arg1;
11225 : : else
11226 : 46668 : parg = arg1, marg = arg0;
11227 : 539622 : pcode = TREE_CODE (parg);
11228 : 539622 : parg0 = TREE_OPERAND (parg, 0);
11229 : 539622 : parg1 = TREE_OPERAND (parg, 1);
11230 : 539622 : STRIP_NOPS (parg0);
11231 : 539622 : STRIP_NOPS (parg1);
11232 : :
11233 : 539622 : if (TREE_CODE (parg0) == MULT_EXPR
11234 : 244674 : && TREE_CODE (parg1) != MULT_EXPR)
11235 : 216553 : return fold_build2_loc (loc, pcode, type,
11236 : : fold_build2_loc (loc, PLUS_EXPR, type,
11237 : : fold_convert_loc (loc, type,
11238 : : parg0),
11239 : : fold_convert_loc (loc, type,
11240 : : marg)),
11241 : 216553 : fold_convert_loc (loc, type, parg1));
11242 : 323069 : if (TREE_CODE (parg0) != MULT_EXPR
11243 : 294948 : && TREE_CODE (parg1) == MULT_EXPR)
11244 : 101573 : return
11245 : 101573 : fold_build2_loc (loc, PLUS_EXPR, type,
11246 : : fold_convert_loc (loc, type, parg0),
11247 : : fold_build2_loc (loc, pcode, type,
11248 : : fold_convert_loc (loc, type, marg),
11249 : : fold_convert_loc (loc, type,
11250 : 101573 : parg1)));
11251 : : }
11252 : : }
11253 : : else
11254 : : {
11255 : : /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11256 : : to __complex__ ( x, y ). This is not the same for SNaNs or
11257 : : if signed zeros are involved. */
11258 : 11681350 : if (!HONOR_SNANS (arg0)
11259 : 11680186 : && !HONOR_SIGNED_ZEROS (arg0)
11260 : 11699914 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11261 : : {
11262 : 3086 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11263 : 3086 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11264 : 3086 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11265 : 3086 : bool arg0rz = false, arg0iz = false;
11266 : 128 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11267 : 3190 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11268 : : {
11269 : 86 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11270 : 86 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11271 : 86 : if (arg0rz && arg1i && real_zerop (arg1i))
11272 : : {
11273 : 22 : tree rp = arg1r ? arg1r
11274 : 0 : : build1 (REALPART_EXPR, rtype, arg1);
11275 : 22 : tree ip = arg0i ? arg0i
11276 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11277 : 22 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11278 : : }
11279 : 64 : else if (arg0iz && arg1r && real_zerop (arg1r))
11280 : : {
11281 : 53 : tree rp = arg0r ? arg0r
11282 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11283 : 53 : tree ip = arg1i ? arg1i
11284 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1);
11285 : 53 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11286 : : }
11287 : : }
11288 : : }
11289 : :
11290 : : /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11291 : : We associate floats only if the user has specified
11292 : : -fassociative-math. */
11293 : 11681275 : if (flag_associative_math
11294 : 18467 : && TREE_CODE (arg1) == PLUS_EXPR
11295 : 38 : && TREE_CODE (arg0) != MULT_EXPR)
11296 : : {
11297 : 21 : tree tree10 = TREE_OPERAND (arg1, 0);
11298 : 21 : tree tree11 = TREE_OPERAND (arg1, 1);
11299 : 21 : if (TREE_CODE (tree11) == MULT_EXPR
11300 : 5 : && TREE_CODE (tree10) == MULT_EXPR)
11301 : : {
11302 : 1 : tree tree0;
11303 : 1 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11304 : 1 : return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11305 : : }
11306 : : }
11307 : : /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11308 : : We associate floats only if the user has specified
11309 : : -fassociative-math. */
11310 : 11681274 : if (flag_associative_math
11311 : 18466 : && TREE_CODE (arg0) == PLUS_EXPR
11312 : 1183 : && TREE_CODE (arg1) != MULT_EXPR)
11313 : : {
11314 : 796 : tree tree00 = TREE_OPERAND (arg0, 0);
11315 : 796 : tree tree01 = TREE_OPERAND (arg0, 1);
11316 : 796 : if (TREE_CODE (tree01) == MULT_EXPR
11317 : 51 : && TREE_CODE (tree00) == MULT_EXPR)
11318 : : {
11319 : 11 : tree tree0;
11320 : 11 : tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11321 : 11 : return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11322 : : }
11323 : : }
11324 : : }
11325 : :
11326 : 11680478 : bit_rotate:
11327 : : /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11328 : : is a rotate of A by C1 bits. */
11329 : : /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11330 : : is a rotate of A by B bits.
11331 : : Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11332 : : though in this case CODE must be | and not + or ^, otherwise
11333 : : it doesn't return A when B is 0. */
11334 : 59982650 : {
11335 : 59982650 : enum tree_code code0, code1;
11336 : 59982650 : tree rtype;
11337 : 59982650 : code0 = TREE_CODE (arg0);
11338 : 59982650 : code1 = TREE_CODE (arg1);
11339 : 52858 : if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11340 : 59966679 : || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11341 : 38940 : && operand_equal_p (TREE_OPERAND (arg0, 0),
11342 : 38940 : TREE_OPERAND (arg1, 0), 0)
11343 : 36257 : && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11344 : 36257 : TYPE_UNSIGNED (rtype))
11345 : : /* Only create rotates in complete modes. Other cases are not
11346 : : expanded properly. */
11347 : 60008763 : && (element_precision (rtype)
11348 : 52226 : == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11349 : : {
11350 : 26044 : tree tree01, tree11;
11351 : 26044 : tree orig_tree01, orig_tree11;
11352 : 26044 : enum tree_code code01, code11;
11353 : :
11354 : 26044 : tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11355 : 26044 : tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11356 : 26044 : STRIP_NOPS (tree01);
11357 : 26044 : STRIP_NOPS (tree11);
11358 : 26044 : code01 = TREE_CODE (tree01);
11359 : 26044 : code11 = TREE_CODE (tree11);
11360 : 26044 : if (code11 != MINUS_EXPR
11361 : 25360 : && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11362 : : {
11363 : 1478 : std::swap (code0, code1);
11364 : 1478 : std::swap (code01, code11);
11365 : 1478 : std::swap (tree01, tree11);
11366 : 1478 : std::swap (orig_tree01, orig_tree11);
11367 : : }
11368 : 52088 : if (code01 == INTEGER_CST
11369 : 3151 : && code11 == INTEGER_CST
11370 : 32344 : && (wi::to_widest (tree01) + wi::to_widest (tree11)
11371 : 32344 : == element_precision (rtype)))
11372 : : {
11373 : 6020 : tem = build2_loc (loc, LROTATE_EXPR,
11374 : 3010 : rtype, TREE_OPERAND (arg0, 0),
11375 : : code0 == LSHIFT_EXPR
11376 : : ? orig_tree01 : orig_tree11);
11377 : 3010 : return fold_convert_loc (loc, type, tem);
11378 : : }
11379 : 23034 : else if (code11 == MINUS_EXPR)
11380 : : {
11381 : 939 : tree tree110, tree111;
11382 : 939 : tree110 = TREE_OPERAND (tree11, 0);
11383 : 939 : tree111 = TREE_OPERAND (tree11, 1);
11384 : 939 : STRIP_NOPS (tree110);
11385 : 939 : STRIP_NOPS (tree111);
11386 : 939 : if (TREE_CODE (tree110) == INTEGER_CST
11387 : 928 : && compare_tree_int (tree110,
11388 : 928 : element_precision (rtype)) == 0
11389 : 1851 : && operand_equal_p (tree01, tree111, 0))
11390 : : {
11391 : 775 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11392 : : ? LROTATE_EXPR : RROTATE_EXPR),
11393 : 556 : rtype, TREE_OPERAND (arg0, 0),
11394 : : orig_tree01);
11395 : 556 : return fold_convert_loc (loc, type, tem);
11396 : : }
11397 : : }
11398 : 22095 : else if (code == BIT_IOR_EXPR
11399 : 20981 : && code11 == BIT_AND_EXPR
11400 : 43001 : && pow2p_hwi (element_precision (rtype)))
11401 : : {
11402 : 20906 : tree tree110, tree111;
11403 : 20906 : tree110 = TREE_OPERAND (tree11, 0);
11404 : 20906 : tree111 = TREE_OPERAND (tree11, 1);
11405 : 20906 : STRIP_NOPS (tree110);
11406 : 20906 : STRIP_NOPS (tree111);
11407 : 20906 : if (TREE_CODE (tree110) == NEGATE_EXPR
11408 : 20419 : && TREE_CODE (tree111) == INTEGER_CST
11409 : 20419 : && compare_tree_int (tree111,
11410 : 20419 : element_precision (rtype) - 1) == 0
11411 : 41311 : && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11412 : : {
11413 : 30455 : tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11414 : : ? LROTATE_EXPR : RROTATE_EXPR),
11415 : 20327 : rtype, TREE_OPERAND (arg0, 0),
11416 : : orig_tree01);
11417 : 20327 : return fold_convert_loc (loc, type, tem);
11418 : : }
11419 : : }
11420 : : }
11421 : : }
11422 : :
11423 : 148226744 : associate:
11424 : : /* In most languages, can't associate operations on floats through
11425 : : parentheses. Rather than remember where the parentheses were, we
11426 : : don't associate floats at all, unless the user has specified
11427 : : -fassociative-math.
11428 : : And, we need to make sure type is not saturating. */
11429 : :
11430 : 148226744 : if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11431 : 106823784 : && !TYPE_SATURATING (type)
11432 : 255050528 : && !TYPE_OVERFLOW_SANITIZED (type))
11433 : : {
11434 : 106795744 : tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11435 : 106795744 : tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11436 : 106795744 : tree atype = type;
11437 : 106795744 : bool ok = true;
11438 : :
11439 : : /* Split both trees into variables, constants, and literals. Then
11440 : : associate each group together, the constants with literals,
11441 : : then the result with variables. This increases the chances of
11442 : : literals being recombined later and of generating relocatable
11443 : : expressions for the sum of a constant and literal. */
11444 : 106795744 : var0 = split_tree (arg0, type, code,
11445 : : &minus_var0, &con0, &minus_con0,
11446 : : &lit0, &minus_lit0, 0);
11447 : 106795744 : var1 = split_tree (arg1, type, code,
11448 : : &minus_var1, &con1, &minus_con1,
11449 : : &lit1, &minus_lit1, code == MINUS_EXPR);
11450 : :
11451 : : /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11452 : 106795744 : if (code == MINUS_EXPR)
11453 : 11307227 : code = PLUS_EXPR;
11454 : :
11455 : : /* With undefined overflow prefer doing association in a type
11456 : : which wraps on overflow, if that is one of the operand types. */
11457 : 106795513 : if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11458 : 212388773 : && !TYPE_OVERFLOW_WRAPS (type))
11459 : : {
11460 : 58119071 : if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11461 : 57476995 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11462 : 745551 : atype = TREE_TYPE (arg0);
11463 : 56631313 : else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11464 : 56422255 : && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11465 : 210864 : atype = TREE_TYPE (arg1);
11466 : 29323137 : gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11467 : : }
11468 : :
11469 : : /* With undefined overflow we can only associate constants with one
11470 : : variable, and constants whose association doesn't overflow. */
11471 : 106795513 : if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11472 : 212388773 : && !TYPE_OVERFLOW_WRAPS (atype))
11473 : : {
11474 : 28366722 : if ((var0 && var1) || (minus_var0 && minus_var1))
11475 : : {
11476 : : /* ??? If split_tree would handle NEGATE_EXPR we could
11477 : : simply reject these cases and the allowed cases would
11478 : : be the var0/minus_var1 ones. */
11479 : 1240 : tree tmp0 = var0 ? var0 : minus_var0;
11480 : 5285042 : tree tmp1 = var1 ? var1 : minus_var1;
11481 : 5285042 : bool one_neg = false;
11482 : :
11483 : 5285042 : if (TREE_CODE (tmp0) == NEGATE_EXPR)
11484 : : {
11485 : 1661 : tmp0 = TREE_OPERAND (tmp0, 0);
11486 : 1661 : one_neg = !one_neg;
11487 : : }
11488 : 4763626 : if (CONVERT_EXPR_P (tmp0)
11489 : 536745 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11490 : 5821444 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11491 : 536402 : <= TYPE_PRECISION (atype)))
11492 : 524216 : tmp0 = TREE_OPERAND (tmp0, 0);
11493 : 5285042 : if (TREE_CODE (tmp1) == NEGATE_EXPR)
11494 : : {
11495 : 170 : tmp1 = TREE_OPERAND (tmp1, 0);
11496 : 170 : one_neg = !one_neg;
11497 : : }
11498 : 4979122 : if (CONVERT_EXPR_P (tmp1)
11499 : 328286 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11500 : 5613218 : && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11501 : 328176 : <= TYPE_PRECISION (atype)))
11502 : 318416 : tmp1 = TREE_OPERAND (tmp1, 0);
11503 : : /* The only case we can still associate with two variables
11504 : : is if they cancel out. */
11505 : 5285042 : if (!one_neg
11506 : 5285042 : || !operand_equal_p (tmp0, tmp1, 0))
11507 : : ok = false;
11508 : : }
11509 : 22702704 : else if ((var0 && minus_var1
11510 : 3708827 : && ! operand_equal_p (var0, minus_var1, 0))
11511 : 42075558 : || (minus_var0 && var1
11512 : 11307 : && ! operand_equal_p (minus_var0, var1, 0)))
11513 : : ok = false;
11514 : : }
11515 : :
11516 : : /* Only do something if we found more than two objects. Otherwise,
11517 : : nothing has changed and we risk infinite recursion. */
11518 : : if (ok
11519 : 97790640 : && ((var0 != 0) + (var1 != 0)
11520 : 97790640 : + (minus_var0 != 0) + (minus_var1 != 0)
11521 : 97790640 : + (con0 != 0) + (con1 != 0)
11522 : 97790640 : + (minus_con0 != 0) + (minus_con1 != 0)
11523 : 97790640 : + (lit0 != 0) + (lit1 != 0)
11524 : 97790640 : + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11525 : : {
11526 : 1916556 : int var0_origin = (var0 != 0) + 2 * (var1 != 0);
11527 : 3833112 : int minus_var0_origin
11528 : 1916556 : = (minus_var0 != 0) + 2 * (minus_var1 != 0);
11529 : 1916556 : int con0_origin = (con0 != 0) + 2 * (con1 != 0);
11530 : 3833112 : int minus_con0_origin
11531 : 1916556 : = (minus_con0 != 0) + 2 * (minus_con1 != 0);
11532 : 1916556 : int lit0_origin = (lit0 != 0) + 2 * (lit1 != 0);
11533 : 3833112 : int minus_lit0_origin
11534 : 1916556 : = (minus_lit0 != 0) + 2 * (minus_lit1 != 0);
11535 : 1916556 : var0 = associate_trees (loc, var0, var1, code, atype);
11536 : 1916556 : minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11537 : : code, atype);
11538 : 1916556 : con0 = associate_trees (loc, con0, con1, code, atype);
11539 : 1916556 : minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11540 : : code, atype);
11541 : 1916556 : lit0 = associate_trees (loc, lit0, lit1, code, atype);
11542 : 1916556 : minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11543 : : code, atype);
11544 : :
11545 : 1916556 : if (minus_var0 && var0)
11546 : : {
11547 : 1260340 : var0_origin |= minus_var0_origin;
11548 : 1260340 : var0 = associate_trees (loc, var0, minus_var0,
11549 : : MINUS_EXPR, atype);
11550 : 1260340 : minus_var0 = 0;
11551 : 1260340 : minus_var0_origin = 0;
11552 : : }
11553 : 1916556 : if (minus_con0 && con0)
11554 : : {
11555 : 3610 : con0_origin |= minus_con0_origin;
11556 : 3610 : con0 = associate_trees (loc, con0, minus_con0,
11557 : : MINUS_EXPR, atype);
11558 : 3610 : minus_con0 = 0;
11559 : 3610 : minus_con0_origin = 0;
11560 : : }
11561 : :
11562 : : /* Preserve the MINUS_EXPR if the negative part of the literal is
11563 : : greater than the positive part. Otherwise, the multiplicative
11564 : : folding code (i.e extract_muldiv) may be fooled in case
11565 : : unsigned constants are subtracted, like in the following
11566 : : example: ((X*2 + 4) - 8U)/2. */
11567 : 1916556 : if (minus_lit0 && lit0)
11568 : : {
11569 : 217079 : if (TREE_CODE (lit0) == INTEGER_CST
11570 : 217079 : && TREE_CODE (minus_lit0) == INTEGER_CST
11571 : 217079 : && tree_int_cst_lt (lit0, minus_lit0)
11572 : : /* But avoid ending up with only negated parts. */
11573 : 273296 : && (var0 || con0))
11574 : : {
11575 : 51791 : minus_lit0_origin |= lit0_origin;
11576 : 51791 : minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11577 : : MINUS_EXPR, atype);
11578 : 51791 : lit0 = 0;
11579 : 51791 : lit0_origin = 0;
11580 : : }
11581 : : else
11582 : : {
11583 : 165288 : lit0_origin |= minus_lit0_origin;
11584 : 165288 : lit0 = associate_trees (loc, lit0, minus_lit0,
11585 : : MINUS_EXPR, atype);
11586 : 165288 : minus_lit0 = 0;
11587 : 165288 : minus_lit0_origin = 0;
11588 : : }
11589 : : }
11590 : :
11591 : : /* Don't introduce overflows through reassociation. */
11592 : 1280321 : if ((lit0 && TREE_OVERFLOW_P (lit0))
11593 : 3196836 : || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11594 : 1916556 : return NULL_TREE;
11595 : :
11596 : : /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11597 : 1916515 : con0_origin |= lit0_origin;
11598 : 1916515 : con0 = associate_trees (loc, con0, lit0, code, atype);
11599 : 1916515 : minus_con0_origin |= minus_lit0_origin;
11600 : 1916515 : minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11601 : : code, atype);
11602 : :
11603 : : /* Eliminate minus_con0. */
11604 : 1916515 : if (minus_con0)
11605 : : {
11606 : 633623 : if (con0)
11607 : : {
11608 : 8379 : con0_origin |= minus_con0_origin;
11609 : 8379 : con0 = associate_trees (loc, con0, minus_con0,
11610 : : MINUS_EXPR, atype);
11611 : : }
11612 : 625244 : else if (var0)
11613 : : {
11614 : 625244 : var0_origin |= minus_con0_origin;
11615 : 625244 : var0 = associate_trees (loc, var0, minus_con0,
11616 : : MINUS_EXPR, atype);
11617 : : }
11618 : : else
11619 : 0 : gcc_unreachable ();
11620 : : }
11621 : :
11622 : : /* Eliminate minus_var0. */
11623 : 1916515 : if (minus_var0)
11624 : : {
11625 : 342550 : if (con0)
11626 : : {
11627 : 342550 : con0_origin |= minus_var0_origin;
11628 : 342550 : con0 = associate_trees (loc, con0, minus_var0,
11629 : : MINUS_EXPR, atype);
11630 : : }
11631 : : else
11632 : 0 : gcc_unreachable ();
11633 : : }
11634 : :
11635 : : /* Reassociate only if there has been any actual association
11636 : : between subtrees from op0 and subtrees from op1 in at
11637 : : least one of the operands, otherwise we risk infinite
11638 : : recursion. See PR114084. */
11639 : 1916515 : if (var0_origin != 3 && con0_origin != 3)
11640 : : return NULL_TREE;
11641 : :
11642 : 1914813 : return
11643 : 1914813 : fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11644 : 1914813 : code, atype));
11645 : : }
11646 : : }
11647 : :
11648 : : return NULL_TREE;
11649 : :
11650 : 21880817 : case POINTER_DIFF_EXPR:
11651 : 21880817 : case MINUS_EXPR:
11652 : : /* Fold &a[i] - &a[j] to i-j. */
11653 : 21880817 : if (TREE_CODE (arg0) == ADDR_EXPR
11654 : 51279 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11655 : 5953 : && TREE_CODE (arg1) == ADDR_EXPR
11656 : 21881361 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11657 : : {
11658 : 38 : tree tem = fold_addr_of_array_ref_difference (loc, type,
11659 : 38 : TREE_OPERAND (arg0, 0),
11660 : 38 : TREE_OPERAND (arg1, 0),
11661 : : code
11662 : : == POINTER_DIFF_EXPR);
11663 : 38 : if (tem)
11664 : : return tem;
11665 : : }
11666 : :
11667 : : /* Further transformations are not for pointers. */
11668 : 21880803 : if (code == POINTER_DIFF_EXPR)
11669 : : return NULL_TREE;
11670 : :
11671 : : /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11672 : 19326883 : if (TREE_CODE (arg0) == NEGATE_EXPR
11673 : 147279 : && negate_expr_p (op1)
11674 : : /* If arg0 is e.g. unsigned int and type is int, then this could
11675 : : introduce UB, because if A is INT_MIN at runtime, the original
11676 : : expression can be well defined while the latter is not.
11677 : : See PR83269. */
11678 : 19327700 : && !(ANY_INTEGRAL_TYPE_P (type)
11679 : 817 : && TYPE_OVERFLOW_UNDEFINED (type)
11680 : 805 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11681 : 805 : && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11682 : 810 : return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11683 : : fold_convert_loc (loc, type,
11684 : 1620 : TREE_OPERAND (arg0, 0)));
11685 : :
11686 : : /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11687 : : __complex__ ( x, -y ). This is not the same for SNaNs or if
11688 : : signed zeros are involved. */
11689 : 19326073 : if (!HONOR_SNANS (arg0)
11690 : 19325209 : && !HONOR_SIGNED_ZEROS (arg0)
11691 : 31381818 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11692 : : {
11693 : 53 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11694 : 53 : tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11695 : 53 : tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11696 : 53 : bool arg0rz = false, arg0iz = false;
11697 : 25 : if ((arg0r && (arg0rz = real_zerop (arg0r)))
11698 : 69 : || (arg0i && (arg0iz = real_zerop (arg0i))))
11699 : : {
11700 : 25 : tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11701 : 25 : tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11702 : 25 : if (arg0rz && arg1i && real_zerop (arg1i))
11703 : : {
11704 : 9 : tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11705 : : arg1r ? arg1r
11706 : 0 : : build1 (REALPART_EXPR, rtype, arg1));
11707 : 9 : tree ip = arg0i ? arg0i
11708 : 0 : : build1 (IMAGPART_EXPR, rtype, arg0);
11709 : 9 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11710 : : }
11711 : 16 : else if (arg0iz && arg1r && real_zerop (arg1r))
11712 : : {
11713 : 15 : tree rp = arg0r ? arg0r
11714 : 0 : : build1 (REALPART_EXPR, rtype, arg0);
11715 : 15 : tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11716 : : arg1i ? arg1i
11717 : 0 : : build1 (IMAGPART_EXPR, rtype, arg1));
11718 : 15 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11719 : : }
11720 : : }
11721 : : }
11722 : :
11723 : : /* A - B -> A + (-B) if B is easily negatable. */
11724 : 19326049 : if (negate_expr_p (op1)
11725 : 697262 : && ! TYPE_OVERFLOW_SANITIZED (type)
11726 : 20020849 : && ((FLOAT_TYPE_P (type)
11727 : : /* Avoid this transformation if B is a positive REAL_CST. */
11728 : 65 : && (TREE_CODE (op1) != REAL_CST
11729 : 0 : || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11730 : 694735 : || INTEGRAL_TYPE_P (type)))
11731 : 694619 : return fold_build2_loc (loc, PLUS_EXPR, type,
11732 : : fold_convert_loc (loc, type, arg0),
11733 : 694619 : negate_expr (op1));
11734 : :
11735 : : /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11736 : : one. Make sure the type is not saturating and has the signedness of
11737 : : the stripped operands, as fold_plusminus_mult_expr will re-associate.
11738 : : ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11739 : 18631430 : if ((TREE_CODE (arg0) == MULT_EXPR
11740 : 17346595 : || TREE_CODE (arg1) == MULT_EXPR)
11741 : 2633502 : && !TYPE_SATURATING (type)
11742 : 2633502 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11743 : 2508220 : && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11744 : 21084998 : && (!FLOAT_TYPE_P (type) || flag_associative_math))
11745 : : {
11746 : 328312 : tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11747 : 328312 : if (tem)
11748 : : return tem;
11749 : : }
11750 : :
11751 : 18585065 : goto associate;
11752 : :
11753 : 63747414 : case MULT_EXPR:
11754 : 63747414 : if (! FLOAT_TYPE_P (type))
11755 : : {
11756 : : /* Transform x * -C into -x * C if x is easily negatable. */
11757 : 41247844 : if (TREE_CODE (op1) == INTEGER_CST
11758 : 38594244 : && tree_int_cst_sgn (op1) == -1
11759 : 194844 : && negate_expr_p (op0)
11760 : 336 : && negate_expr_p (op1)
11761 : 320 : && (tem = negate_expr (op1)) != op1
11762 : 41248164 : && ! TREE_OVERFLOW (tem))
11763 : 320 : return fold_build2_loc (loc, MULT_EXPR, type,
11764 : : fold_convert_loc (loc, type,
11765 : 320 : negate_expr (op0)), tem);
11766 : :
11767 : 41247524 : strict_overflow_p = false;
11768 : 41247524 : if (TREE_CODE (arg1) == INTEGER_CST
11769 : 41247524 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11770 : : &strict_overflow_p)) != 0)
11771 : : {
11772 : 501379 : if (strict_overflow_p)
11773 : 10 : fold_overflow_warning (("assuming signed overflow does not "
11774 : : "occur when simplifying "
11775 : : "multiplication"),
11776 : : WARN_STRICT_OVERFLOW_MISC);
11777 : 501379 : return fold_convert_loc (loc, type, tem);
11778 : : }
11779 : :
11780 : : /* Optimize z * conj(z) for integer complex numbers. */
11781 : 40746145 : if (TREE_CODE (arg0) == CONJ_EXPR
11782 : 40746145 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11783 : 1 : return fold_mult_zconjz (loc, type, arg1);
11784 : 40746144 : if (TREE_CODE (arg1) == CONJ_EXPR
11785 : 40746144 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11786 : 0 : return fold_mult_zconjz (loc, type, arg0);
11787 : : }
11788 : : else
11789 : : {
11790 : : /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11791 : : This is not the same for NaNs or if signed zeros are
11792 : : involved. */
11793 : 22499570 : if (!HONOR_NANS (arg0)
11794 : 32715 : && !HONOR_SIGNED_ZEROS (arg0)
11795 : 32415 : && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11796 : 3637 : && TREE_CODE (arg1) == COMPLEX_CST
11797 : 22499795 : && real_zerop (TREE_REALPART (arg1)))
11798 : : {
11799 : 218 : tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11800 : 218 : if (real_onep (TREE_IMAGPART (arg1)))
11801 : : {
11802 : 208 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11803 : 63 : arg0 = save_expr (arg0);
11804 : 208 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11805 : : rtype, arg0);
11806 : 208 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11807 : : rtype, arg0);
11808 : 208 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11809 : : negate_expr (iarg0),
11810 : 208 : rarg0);
11811 : : }
11812 : 10 : else if (real_minus_onep (TREE_IMAGPART (arg1)))
11813 : : {
11814 : 10 : if (TREE_CODE (arg0) != COMPLEX_EXPR)
11815 : 0 : arg0 = save_expr (arg0);
11816 : 10 : tree iarg0 = fold_build1_loc (loc, IMAGPART_EXPR,
11817 : : rtype, arg0);
11818 : 10 : tree rarg0 = fold_build1_loc (loc, REALPART_EXPR,
11819 : : rtype, arg0);
11820 : 10 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
11821 : : iarg0,
11822 : 10 : negate_expr (rarg0));
11823 : : }
11824 : : }
11825 : :
11826 : : /* Optimize z * conj(z) for floating point complex numbers.
11827 : : Guarded by flag_unsafe_math_optimizations as non-finite
11828 : : imaginary components don't produce scalar results. */
11829 : 22499352 : if (flag_unsafe_math_optimizations
11830 : 32244 : && TREE_CODE (arg0) == CONJ_EXPR
11831 : 22499354 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11832 : 1 : return fold_mult_zconjz (loc, type, arg1);
11833 : 22499351 : if (flag_unsafe_math_optimizations
11834 : 32243 : && TREE_CODE (arg1) == CONJ_EXPR
11835 : 22499355 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11836 : 0 : return fold_mult_zconjz (loc, type, arg0);
11837 : : }
11838 : 63245495 : goto associate;
11839 : :
11840 : 1782400 : case BIT_IOR_EXPR:
11841 : : /* Canonicalize (X & C1) | C2. */
11842 : 1782400 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11843 : 126771 : && TREE_CODE (arg1) == INTEGER_CST
11844 : 1864642 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11845 : : {
11846 : 82234 : int width = TYPE_PRECISION (type), w;
11847 : 82234 : wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11848 : 82234 : wide_int c2 = wi::to_wide (arg1);
11849 : :
11850 : : /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11851 : 82234 : if ((c1 & c2) == c1)
11852 : 0 : return omit_one_operand_loc (loc, type, arg1,
11853 : 0 : TREE_OPERAND (arg0, 0));
11854 : :
11855 : 82234 : wide_int msk = wi::mask (width, false,
11856 : 82234 : TYPE_PRECISION (TREE_TYPE (arg1)));
11857 : :
11858 : : /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11859 : 82234 : if (wi::bit_and_not (msk, c1 | c2) == 0)
11860 : : {
11861 : 6 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11862 : 6 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11863 : : }
11864 : :
11865 : : /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11866 : : unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11867 : : mode which allows further optimizations. */
11868 : 82228 : c1 &= msk;
11869 : 82228 : c2 &= msk;
11870 : 82228 : wide_int c3 = wi::bit_and_not (c1, c2);
11871 : 254571 : for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11872 : : {
11873 : 172585 : wide_int mask = wi::mask (w, false,
11874 : 172585 : TYPE_PRECISION (type));
11875 : 345170 : if (((c1 | c2) & mask) == mask
11876 : 345170 : && wi::bit_and_not (c1, mask) == 0)
11877 : : {
11878 : 242 : c3 = mask;
11879 : 242 : break;
11880 : : }
11881 : 172585 : }
11882 : :
11883 : 82228 : if (c3 != c1)
11884 : : {
11885 : 562 : tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11886 : 1124 : tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11887 : 562 : wide_int_to_tree (type, c3));
11888 : 562 : return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11889 : : }
11890 : 83364 : }
11891 : :
11892 : : /* See if this can be simplified into a rotate first. If that
11893 : : is unsuccessful continue in the association code. */
11894 : 1781832 : goto bit_rotate;
11895 : :
11896 : 661009 : case BIT_XOR_EXPR:
11897 : : /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11898 : 661009 : if (TREE_CODE (arg0) == BIT_AND_EXPR
11899 : 2420 : && INTEGRAL_TYPE_P (type)
11900 : 1815 : && integer_onep (TREE_OPERAND (arg0, 1))
11901 : 661012 : && integer_onep (arg1))
11902 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11903 : 0 : build_zero_cst (TREE_TYPE (arg0)));
11904 : :
11905 : : /* See if this can be simplified into a rotate first. If that
11906 : : is unsuccessful continue in the association code. */
11907 : 661009 : goto bit_rotate;
11908 : :
11909 : 6015416 : case BIT_AND_EXPR:
11910 : : /* Fold !X & 1 as X == 0. */
11911 : 6015416 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11912 : 6015416 : && integer_onep (arg1))
11913 : : {
11914 : 0 : tem = TREE_OPERAND (arg0, 0);
11915 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, tem,
11916 : 0 : build_zero_cst (TREE_TYPE (tem)));
11917 : : }
11918 : :
11919 : : /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11920 : : multiple of 1 << CST. */
11921 : 6015416 : if (TREE_CODE (arg1) == INTEGER_CST)
11922 : : {
11923 : 4427615 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11924 : 4427615 : wide_int ncst1 = -cst1;
11925 : 4427615 : if ((cst1 & ncst1) == ncst1
11926 : 4606917 : && multiple_of_p (type, arg0,
11927 : 4606917 : wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11928 : 466 : return fold_convert_loc (loc, type, arg0);
11929 : 4427615 : }
11930 : :
11931 : : /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11932 : : bits from CST2. */
11933 : 6014950 : if (TREE_CODE (arg1) == INTEGER_CST
11934 : 4427149 : && TREE_CODE (arg0) == MULT_EXPR
11935 : 6119829 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11936 : : {
11937 : 104849 : wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11938 : 104849 : wide_int masked
11939 : 104849 : = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11940 : :
11941 : 104849 : if (masked == 0)
11942 : 6650 : return omit_two_operands_loc (loc, type, build_zero_cst (type),
11943 : 6650 : arg0, arg1);
11944 : 98199 : else if (masked != warg1)
11945 : : {
11946 : : /* Avoid the transform if arg1 is a mask of some
11947 : : mode which allows further optimizations. */
11948 : 653 : int pop = wi::popcount (warg1);
11949 : 677 : if (!(pop >= BITS_PER_UNIT
11950 : 50 : && pow2p_hwi (pop)
11951 : 701 : && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11952 : 1258 : return fold_build2_loc (loc, code, type, op0,
11953 : 1258 : wide_int_to_tree (type, masked));
11954 : : }
11955 : 104849 : }
11956 : :
11957 : : /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11958 : 4419870 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11959 : 6341898 : && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11960 : : {
11961 : 175914 : prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11962 : :
11963 : 175914 : wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11964 : 175914 : if (mask == -1)
11965 : 665 : return
11966 : 665 : fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11967 : 175914 : }
11968 : :
11969 : 6007006 : goto associate;
11970 : :
11971 : 6239956 : case RDIV_EXPR:
11972 : : /* Don't touch a floating-point divide by zero unless the mode
11973 : : of the constant can represent infinity. */
11974 : 6239956 : if (TREE_CODE (arg1) == REAL_CST
11975 : 3149789 : && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11976 : 6239956 : && real_zerop (arg1))
11977 : 0 : return NULL_TREE;
11978 : :
11979 : : /* (-A) / (-B) -> A / B */
11980 : 6239956 : if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11981 : 6 : return fold_build2_loc (loc, RDIV_EXPR, type,
11982 : 3 : TREE_OPERAND (arg0, 0),
11983 : 3 : negate_expr (arg1));
11984 : 6239953 : if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11985 : 0 : return fold_build2_loc (loc, RDIV_EXPR, type,
11986 : : negate_expr (arg0),
11987 : 0 : TREE_OPERAND (arg1, 0));
11988 : : return NULL_TREE;
11989 : :
11990 : 2081394 : case TRUNC_DIV_EXPR:
11991 : : /* Fall through */
11992 : :
11993 : 2081394 : case FLOOR_DIV_EXPR:
11994 : : /* Simplify A / (B << N) where A and B are positive and B is
11995 : : a power of 2, to A >> (N + log2(B)). */
11996 : 2081394 : strict_overflow_p = false;
11997 : 2081394 : if (TREE_CODE (arg1) == LSHIFT_EXPR
11998 : 2081394 : && (TYPE_UNSIGNED (type)
11999 : 8 : || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12000 : : {
12001 : 17 : tree sval = TREE_OPERAND (arg1, 0);
12002 : 17 : if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12003 : : {
12004 : 16 : tree sh_cnt = TREE_OPERAND (arg1, 1);
12005 : 16 : tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12006 : 16 : wi::exact_log2 (wi::to_wide (sval)));
12007 : :
12008 : 16 : if (strict_overflow_p)
12009 : 0 : fold_overflow_warning (("assuming signed overflow does not "
12010 : : "occur when simplifying A / (B << N)"),
12011 : : WARN_STRICT_OVERFLOW_MISC);
12012 : :
12013 : 16 : sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12014 : : sh_cnt, pow2);
12015 : 16 : return fold_build2_loc (loc, RSHIFT_EXPR, type,
12016 : 16 : fold_convert_loc (loc, type, arg0), sh_cnt);
12017 : : }
12018 : : }
12019 : :
12020 : : /* Fall through */
12021 : :
12022 : 3282251 : case ROUND_DIV_EXPR:
12023 : 3282251 : case CEIL_DIV_EXPR:
12024 : 3282251 : case EXACT_DIV_EXPR:
12025 : 3282251 : if (integer_zerop (arg1))
12026 : : return NULL_TREE;
12027 : :
12028 : : /* Convert -A / -B to A / B when the type is signed and overflow is
12029 : : undefined. */
12030 : 3279237 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12031 : 819128 : && TREE_CODE (op0) == NEGATE_EXPR
12032 : 3279299 : && negate_expr_p (op1))
12033 : : {
12034 : 30 : if (ANY_INTEGRAL_TYPE_P (type))
12035 : 30 : fold_overflow_warning (("assuming signed overflow does not occur "
12036 : : "when distributing negation across "
12037 : : "division"),
12038 : : WARN_STRICT_OVERFLOW_MISC);
12039 : 60 : return fold_build2_loc (loc, code, type,
12040 : : fold_convert_loc (loc, type,
12041 : 30 : TREE_OPERAND (arg0, 0)),
12042 : 30 : negate_expr (op1));
12043 : : }
12044 : 3279207 : if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12045 : 819098 : && TREE_CODE (arg1) == NEGATE_EXPR
12046 : 3279451 : && negate_expr_p (op0))
12047 : : {
12048 : 36 : if (ANY_INTEGRAL_TYPE_P (type))
12049 : 36 : fold_overflow_warning (("assuming signed overflow does not occur "
12050 : : "when distributing negation across "
12051 : : "division"),
12052 : : WARN_STRICT_OVERFLOW_MISC);
12053 : 36 : return fold_build2_loc (loc, code, type,
12054 : : negate_expr (op0),
12055 : : fold_convert_loc (loc, type,
12056 : 72 : TREE_OPERAND (arg1, 0)));
12057 : : }
12058 : :
12059 : : /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12060 : : operation, EXACT_DIV_EXPR.
12061 : :
12062 : : Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12063 : : At one time others generated faster code, it's not clear if they do
12064 : : after the last round to changes to the DIV code in expmed.cc. */
12065 : 3279171 : if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12066 : 3279171 : && multiple_of_p (type, arg0, arg1))
12067 : 0 : return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
12068 : : fold_convert (type, arg0),
12069 : 0 : fold_convert (type, arg1));
12070 : :
12071 : 3279171 : strict_overflow_p = false;
12072 : 3279171 : if (TREE_CODE (arg1) == INTEGER_CST
12073 : 3279171 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12074 : : &strict_overflow_p)) != 0)
12075 : : {
12076 : 2435 : if (strict_overflow_p)
12077 : 157 : fold_overflow_warning (("assuming signed overflow does not occur "
12078 : : "when simplifying division"),
12079 : : WARN_STRICT_OVERFLOW_MISC);
12080 : 2435 : return fold_convert_loc (loc, type, tem);
12081 : : }
12082 : :
12083 : : return NULL_TREE;
12084 : :
12085 : 613802 : case CEIL_MOD_EXPR:
12086 : 613802 : case FLOOR_MOD_EXPR:
12087 : 613802 : case ROUND_MOD_EXPR:
12088 : 613802 : case TRUNC_MOD_EXPR:
12089 : 613802 : strict_overflow_p = false;
12090 : 613802 : if (TREE_CODE (arg1) == INTEGER_CST
12091 : 613802 : && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12092 : : &strict_overflow_p)) != 0)
12093 : : {
12094 : 0 : if (strict_overflow_p)
12095 : 0 : fold_overflow_warning (("assuming signed overflow does not occur "
12096 : : "when simplifying modulus"),
12097 : : WARN_STRICT_OVERFLOW_MISC);
12098 : 0 : return fold_convert_loc (loc, type, tem);
12099 : : }
12100 : :
12101 : : return NULL_TREE;
12102 : :
12103 : 2050095 : case LROTATE_EXPR:
12104 : 2050095 : case RROTATE_EXPR:
12105 : 2050095 : case RSHIFT_EXPR:
12106 : 2050095 : case LSHIFT_EXPR:
12107 : : /* Since negative shift count is not well-defined,
12108 : : don't try to compute it in the compiler. */
12109 : 2050095 : if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12110 : : return NULL_TREE;
12111 : :
12112 : 2049138 : prec = element_precision (type);
12113 : :
12114 : : /* If we have a rotate of a bit operation with the rotate count and
12115 : : the second operand of the bit operation both constant,
12116 : : permute the two operations. */
12117 : 2451 : if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12118 : 1897 : && (TREE_CODE (arg0) == BIT_AND_EXPR
12119 : 1897 : || TREE_CODE (arg0) == BIT_IOR_EXPR
12120 : 1897 : || TREE_CODE (arg0) == BIT_XOR_EXPR)
12121 : 2049138 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12122 : : {
12123 : 0 : tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12124 : 0 : tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12125 : 0 : return fold_build2_loc (loc, TREE_CODE (arg0), type,
12126 : : fold_build2_loc (loc, code, type,
12127 : : arg00, arg1),
12128 : : fold_build2_loc (loc, code, type,
12129 : 0 : arg01, arg1));
12130 : : }
12131 : :
12132 : : return NULL_TREE;
12133 : :
12134 : 430421 : case MIN_EXPR:
12135 : 430421 : case MAX_EXPR:
12136 : 430421 : goto associate;
12137 : :
12138 : 5664177 : case TRUTH_ANDIF_EXPR:
12139 : : /* Note that the operands of this must be ints
12140 : : and their values must be 0 or 1.
12141 : : ("true" is a fixed value perhaps depending on the language.) */
12142 : : /* If first arg is constant zero, return it. */
12143 : 5664177 : if (integer_zerop (arg0))
12144 : 1129778 : return fold_convert_loc (loc, type, arg0);
12145 : : /* FALLTHRU */
12146 : 15177743 : case TRUTH_AND_EXPR:
12147 : : /* If either arg is constant true, drop it. */
12148 : 15177743 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12149 : 2033898 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12150 : 806316 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12151 : : /* Preserve sequence points. */
12152 : 13904157 : && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12153 : 732951 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12154 : : /* If second arg is constant zero, result is zero, but first arg
12155 : : must be evaluated. */
12156 : 12410894 : if (integer_zerop (arg1))
12157 : 46004 : return omit_one_operand_loc (loc, type, arg1, arg0);
12158 : : /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12159 : : case will be handled here. */
12160 : 12364890 : if (integer_zerop (arg0))
12161 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12162 : :
12163 : : /* !X && X is always false. */
12164 : 12364890 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12165 : 12364890 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12166 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12167 : : /* X && !X is always false. */
12168 : 12364890 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12169 : 12364890 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12170 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12171 : :
12172 : : /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12173 : : means A >= Y && A != MAX, but in this case we know that
12174 : : A < X <= MAX. */
12175 : :
12176 : 12364890 : if (!TREE_SIDE_EFFECTS (arg0)
12177 : 12364890 : && !TREE_SIDE_EFFECTS (arg1))
12178 : : {
12179 : 11387525 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12180 : 11387525 : if (tem && !operand_equal_p (tem, arg0, 0))
12181 : 469 : return fold_convert (type,
12182 : : fold_build2_loc (loc, code, TREE_TYPE (arg1),
12183 : : tem, arg1));
12184 : :
12185 : 11387056 : tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12186 : 11387056 : if (tem && !operand_equal_p (tem, arg1, 0))
12187 : 9996 : return fold_convert (type,
12188 : : fold_build2_loc (loc, code, TREE_TYPE (arg0),
12189 : : arg0, tem));
12190 : : }
12191 : :
12192 : 12354425 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12193 : : != NULL_TREE)
12194 : : return tem;
12195 : :
12196 : : return NULL_TREE;
12197 : :
12198 : 3065074 : case TRUTH_ORIF_EXPR:
12199 : : /* Note that the operands of this must be ints
12200 : : and their values must be 0 or true.
12201 : : ("true" is a fixed value perhaps depending on the language.) */
12202 : : /* If first arg is constant true, return it. */
12203 : 3065074 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12204 : 124064 : return fold_convert_loc (loc, type, arg0);
12205 : : /* FALLTHRU */
12206 : 12337988 : case TRUTH_OR_EXPR:
12207 : : /* If either arg is constant zero, drop it. */
12208 : 12337988 : if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12209 : 152323 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12210 : 480942 : if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12211 : : /* Preserve sequence points. */
12212 : 12615662 : && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12213 : 419130 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12214 : : /* If second arg is constant true, result is true, but we must
12215 : : evaluate first arg. */
12216 : 11766535 : if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12217 : 50945 : return omit_one_operand_loc (loc, type, arg1, arg0);
12218 : : /* Likewise for first arg, but note this only occurs here for
12219 : : TRUTH_OR_EXPR. */
12220 : 11715590 : if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12221 : 0 : return omit_one_operand_loc (loc, type, arg0, arg1);
12222 : :
12223 : : /* !X || X is always true. */
12224 : 11715590 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12225 : 11715590 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12226 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12227 : : /* X || !X is always true. */
12228 : 11715590 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12229 : 11715590 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12230 : 1 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12231 : :
12232 : : /* (X && !Y) || (!X && Y) is X ^ Y */
12233 : 11715589 : if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12234 : 1601 : && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12235 : : {
12236 : 659 : tree a0, a1, l0, l1, n0, n1;
12237 : :
12238 : 659 : a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12239 : 659 : a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12240 : :
12241 : 659 : l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12242 : 659 : l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12243 : :
12244 : 659 : n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12245 : 659 : n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12246 : :
12247 : 659 : if ((operand_equal_p (n0, a0, 0)
12248 : 18 : && operand_equal_p (n1, a1, 0))
12249 : 667 : || (operand_equal_p (n0, a1, 0)
12250 : 3 : && operand_equal_p (n1, a0, 0)))
12251 : 13 : return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12252 : : }
12253 : :
12254 : 11715576 : if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12255 : : != NULL_TREE)
12256 : : return tem;
12257 : :
12258 : : return NULL_TREE;
12259 : :
12260 : 37879 : case TRUTH_XOR_EXPR:
12261 : : /* If the second arg is constant zero, drop it. */
12262 : 37879 : if (integer_zerop (arg1))
12263 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12264 : : /* If the second arg is constant true, this is a logical inversion. */
12265 : 37879 : if (integer_onep (arg1))
12266 : : {
12267 : 0 : tem = invert_truthvalue_loc (loc, arg0);
12268 : 0 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12269 : : }
12270 : : /* Identical arguments cancel to zero. */
12271 : 37879 : if (operand_equal_p (arg0, arg1, 0))
12272 : 0 : return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12273 : :
12274 : : /* !X ^ X is always true. */
12275 : 37879 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12276 : 37879 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12277 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12278 : :
12279 : : /* X ^ !X is always true. */
12280 : 37879 : if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12281 : 37879 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12282 : 0 : return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12283 : :
12284 : : return NULL_TREE;
12285 : :
12286 : 45074678 : case EQ_EXPR:
12287 : 45074678 : case NE_EXPR:
12288 : 45074678 : STRIP_NOPS (arg0);
12289 : 45074678 : STRIP_NOPS (arg1);
12290 : :
12291 : 45074678 : tem = fold_comparison (loc, code, type, op0, op1);
12292 : 45074678 : if (tem != NULL_TREE)
12293 : : return tem;
12294 : :
12295 : : /* bool_var != 1 becomes !bool_var. */
12296 : 46162953 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12297 : 45108751 : && code == NE_EXPR)
12298 : 37497 : return fold_convert_loc (loc, type,
12299 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12300 : 74994 : TREE_TYPE (arg0), arg0));
12301 : :
12302 : : /* bool_var == 0 becomes !bool_var. */
12303 : 46087959 : if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12304 : 45911826 : && code == EQ_EXPR)
12305 : 200713 : return fold_convert_loc (loc, type,
12306 : : fold_build1_loc (loc, TRUTH_NOT_EXPR,
12307 : 401426 : TREE_TYPE (arg0), arg0));
12308 : :
12309 : : /* !exp != 0 becomes !exp */
12310 : 578274 : if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12311 : 45406303 : && code == NE_EXPR)
12312 : 573410 : return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12313 : :
12314 : : /* If this is an EQ or NE comparison with zero and ARG0 is
12315 : : (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12316 : : two operations, but the latter can be done in one less insn
12317 : : on machines that have only two-operand insns or on which a
12318 : : constant cannot be the first operand. */
12319 : 44255332 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12320 : 44255332 : && integer_zerop (arg1))
12321 : : {
12322 : 1473289 : tree arg00 = TREE_OPERAND (arg0, 0);
12323 : 1473289 : tree arg01 = TREE_OPERAND (arg0, 1);
12324 : 1473289 : if (TREE_CODE (arg00) == LSHIFT_EXPR
12325 : 1473289 : && integer_onep (TREE_OPERAND (arg00, 0)))
12326 : : {
12327 : 4714 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12328 : 4714 : arg01, TREE_OPERAND (arg00, 1));
12329 : 4714 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12330 : 4714 : build_one_cst (TREE_TYPE (arg0)));
12331 : 4714 : return fold_build2_loc (loc, code, type,
12332 : 4714 : fold_convert_loc (loc, TREE_TYPE (arg1),
12333 : 4714 : tem), arg1);
12334 : : }
12335 : 1468575 : else if (TREE_CODE (arg01) == LSHIFT_EXPR
12336 : 1468575 : && integer_onep (TREE_OPERAND (arg01, 0)))
12337 : : {
12338 : 820 : tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12339 : 820 : arg00, TREE_OPERAND (arg01, 1));
12340 : 820 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12341 : 820 : build_one_cst (TREE_TYPE (arg0)));
12342 : 820 : return fold_build2_loc (loc, code, type,
12343 : 820 : fold_convert_loc (loc, TREE_TYPE (arg1),
12344 : 820 : tem), arg1);
12345 : : }
12346 : : }
12347 : :
12348 : : /* If this is a comparison of a field, we may be able to simplify it. */
12349 : 44249798 : if ((TREE_CODE (arg0) == COMPONENT_REF
12350 : 44249798 : || TREE_CODE (arg0) == BIT_FIELD_REF)
12351 : : /* Handle the constant case even without -O
12352 : : to make sure the warnings are given. */
12353 : 4312289 : && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12354 : : {
12355 : 4022465 : t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12356 : 4022465 : if (t1)
12357 : : return t1;
12358 : : }
12359 : :
12360 : : /* Optimize comparisons of strlen vs zero to a compare of the
12361 : : first character of the string vs zero. To wit,
12362 : : strlen(ptr) == 0 => *ptr == 0
12363 : : strlen(ptr) != 0 => *ptr != 0
12364 : : Other cases should reduce to one of these two (or a constant)
12365 : : due to the return value of strlen being unsigned. */
12366 : 43507349 : if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12367 : : {
12368 : 2474530 : tree fndecl = get_callee_fndecl (arg0);
12369 : :
12370 : 2474530 : if (fndecl
12371 : 2473642 : && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12372 : 537 : && call_expr_nargs (arg0) == 1
12373 : 2475067 : && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12374 : : == POINTER_TYPE))
12375 : : {
12376 : 537 : tree ptrtype
12377 : 537 : = build_pointer_type (build_qualified_type (char_type_node,
12378 : : TYPE_QUAL_CONST));
12379 : 1074 : tree ptr = fold_convert_loc (loc, ptrtype,
12380 : 537 : CALL_EXPR_ARG (arg0, 0));
12381 : 537 : tree iref = build_fold_indirect_ref_loc (loc, ptr);
12382 : 537 : return fold_build2_loc (loc, code, type, iref,
12383 : 537 : build_int_cst (TREE_TYPE (iref), 0));
12384 : : }
12385 : : }
12386 : :
12387 : : /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12388 : : of X. Similarly fold (X >> C) == 0 into X >= 0. */
12389 : 43506812 : if (TREE_CODE (arg0) == RSHIFT_EXPR
12390 : 37934 : && integer_zerop (arg1)
12391 : 43518816 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12392 : : {
12393 : 9936 : tree arg00 = TREE_OPERAND (arg0, 0);
12394 : 9936 : tree arg01 = TREE_OPERAND (arg0, 1);
12395 : 9936 : tree itype = TREE_TYPE (arg00);
12396 : 9936 : if (wi::to_wide (arg01) == element_precision (itype) - 1)
12397 : : {
12398 : 910 : if (TYPE_UNSIGNED (itype))
12399 : : {
12400 : 829 : itype = signed_type_for (itype);
12401 : 829 : arg00 = fold_convert_loc (loc, itype, arg00);
12402 : : }
12403 : 1789 : return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12404 : 910 : type, arg00, build_zero_cst (itype));
12405 : : }
12406 : : }
12407 : :
12408 : : /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12409 : : (X & C) == 0 when C is a single bit. */
12410 : 43505902 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12411 : 1664381 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12412 : 963 : && integer_zerop (arg1)
12413 : 43506467 : && integer_pow2p (TREE_OPERAND (arg0, 1)))
12414 : : {
12415 : 248 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12416 : 248 : TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12417 : 248 : TREE_OPERAND (arg0, 1));
12418 : 388 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12419 : : type, tem,
12420 : 248 : fold_convert_loc (loc, TREE_TYPE (arg0),
12421 : 248 : arg1));
12422 : : }
12423 : :
12424 : : /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12425 : : constant C is a power of two, i.e. a single bit. */
12426 : 43505654 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12427 : 4570 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12428 : 0 : && integer_zerop (arg1)
12429 : 0 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12430 : 43505654 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12431 : 0 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12432 : : {
12433 : 0 : tree arg00 = TREE_OPERAND (arg0, 0);
12434 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12435 : 0 : arg00, build_int_cst (TREE_TYPE (arg00), 0));
12436 : : }
12437 : :
12438 : : /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12439 : : when is C is a power of two, i.e. a single bit. */
12440 : 43505654 : if (TREE_CODE (arg0) == BIT_AND_EXPR
12441 : 1664133 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12442 : 11422 : && integer_zerop (arg1)
12443 : 11422 : && integer_pow2p (TREE_OPERAND (arg0, 1))
12444 : 43514426 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12445 : 8772 : TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12446 : : {
12447 : 0 : tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12448 : 0 : tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12449 : 0 : arg000, TREE_OPERAND (arg0, 1));
12450 : 0 : return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12451 : 0 : tem, build_int_cst (TREE_TYPE (tem), 0));
12452 : : }
12453 : :
12454 : 43505654 : if (TREE_CODE (arg0) == BIT_XOR_EXPR
12455 : 4570 : && TREE_CODE (arg1) == BIT_XOR_EXPR)
12456 : : {
12457 : 482 : tree arg00 = TREE_OPERAND (arg0, 0);
12458 : 482 : tree arg01 = TREE_OPERAND (arg0, 1);
12459 : 482 : tree arg10 = TREE_OPERAND (arg1, 0);
12460 : 482 : tree arg11 = TREE_OPERAND (arg1, 1);
12461 : 482 : tree itype = TREE_TYPE (arg0);
12462 : :
12463 : : /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12464 : : operand_equal_p guarantees no side-effects so we don't need
12465 : : to use omit_one_operand on Z. */
12466 : 482 : if (operand_equal_p (arg01, arg11, 0))
12467 : 8 : return fold_build2_loc (loc, code, type, arg00,
12468 : 8 : fold_convert_loc (loc, TREE_TYPE (arg00),
12469 : 8 : arg10));
12470 : 474 : if (operand_equal_p (arg01, arg10, 0))
12471 : 0 : return fold_build2_loc (loc, code, type, arg00,
12472 : 0 : fold_convert_loc (loc, TREE_TYPE (arg00),
12473 : 0 : arg11));
12474 : 474 : if (operand_equal_p (arg00, arg11, 0))
12475 : 0 : return fold_build2_loc (loc, code, type, arg01,
12476 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12477 : 0 : arg10));
12478 : 474 : if (operand_equal_p (arg00, arg10, 0))
12479 : 0 : return fold_build2_loc (loc, code, type, arg01,
12480 : 0 : fold_convert_loc (loc, TREE_TYPE (arg01),
12481 : 0 : arg11));
12482 : :
12483 : : /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12484 : 474 : if (TREE_CODE (arg01) == INTEGER_CST
12485 : 8 : && TREE_CODE (arg11) == INTEGER_CST)
12486 : : {
12487 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12488 : : fold_convert_loc (loc, itype, arg11));
12489 : 8 : tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12490 : 8 : return fold_build2_loc (loc, code, type, tem,
12491 : 8 : fold_convert_loc (loc, itype, arg10));
12492 : : }
12493 : : }
12494 : :
12495 : : /* Attempt to simplify equality/inequality comparisons of complex
12496 : : values. Only lower the comparison if the result is known or
12497 : : can be simplified to a single scalar comparison. */
12498 : 43505638 : if ((TREE_CODE (arg0) == COMPLEX_EXPR
12499 : 43503111 : || TREE_CODE (arg0) == COMPLEX_CST)
12500 : 2527 : && (TREE_CODE (arg1) == COMPLEX_EXPR
12501 : 2335 : || TREE_CODE (arg1) == COMPLEX_CST))
12502 : : {
12503 : 1726 : tree real0, imag0, real1, imag1;
12504 : 1726 : tree rcond, icond;
12505 : :
12506 : 1726 : if (TREE_CODE (arg0) == COMPLEX_EXPR)
12507 : : {
12508 : 1726 : real0 = TREE_OPERAND (arg0, 0);
12509 : 1726 : imag0 = TREE_OPERAND (arg0, 1);
12510 : : }
12511 : : else
12512 : : {
12513 : 0 : real0 = TREE_REALPART (arg0);
12514 : 0 : imag0 = TREE_IMAGPART (arg0);
12515 : : }
12516 : :
12517 : 1726 : if (TREE_CODE (arg1) == COMPLEX_EXPR)
12518 : : {
12519 : 192 : real1 = TREE_OPERAND (arg1, 0);
12520 : 192 : imag1 = TREE_OPERAND (arg1, 1);
12521 : : }
12522 : : else
12523 : : {
12524 : 1534 : real1 = TREE_REALPART (arg1);
12525 : 1534 : imag1 = TREE_IMAGPART (arg1);
12526 : : }
12527 : :
12528 : 1726 : rcond = fold_binary_loc (loc, code, type, real0, real1);
12529 : 1726 : if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12530 : : {
12531 : 11 : if (integer_zerop (rcond))
12532 : : {
12533 : 11 : if (code == EQ_EXPR)
12534 : 0 : return omit_two_operands_loc (loc, type, boolean_false_node,
12535 : 0 : imag0, imag1);
12536 : 11 : return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12537 : : }
12538 : : else
12539 : : {
12540 : 0 : if (code == NE_EXPR)
12541 : 0 : return omit_two_operands_loc (loc, type, boolean_true_node,
12542 : 0 : imag0, imag1);
12543 : 0 : return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12544 : : }
12545 : : }
12546 : :
12547 : 1715 : icond = fold_binary_loc (loc, code, type, imag0, imag1);
12548 : 1715 : if (icond && TREE_CODE (icond) == INTEGER_CST)
12549 : : {
12550 : 9 : if (integer_zerop (icond))
12551 : : {
12552 : 7 : if (code == EQ_EXPR)
12553 : 1 : return omit_two_operands_loc (loc, type, boolean_false_node,
12554 : 1 : real0, real1);
12555 : 6 : return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12556 : : }
12557 : : else
12558 : : {
12559 : 2 : if (code == NE_EXPR)
12560 : 1 : return omit_two_operands_loc (loc, type, boolean_true_node,
12561 : 1 : real0, real1);
12562 : 1 : return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12563 : : }
12564 : : }
12565 : : }
12566 : :
12567 : : return NULL_TREE;
12568 : :
12569 : 37264984 : case LT_EXPR:
12570 : 37264984 : case GT_EXPR:
12571 : 37264984 : case LE_EXPR:
12572 : 37264984 : case GE_EXPR:
12573 : 37264984 : tem = fold_comparison (loc, code, type, op0, op1);
12574 : 37264984 : if (tem != NULL_TREE)
12575 : : return tem;
12576 : :
12577 : : /* Transform comparisons of the form X +- C CMP X. */
12578 : 36396112 : if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12579 : 4561879 : && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12580 : 48807 : && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12581 : 36396130 : && !HONOR_SNANS (arg0))
12582 : : {
12583 : 16 : tree arg01 = TREE_OPERAND (arg0, 1);
12584 : 16 : enum tree_code code0 = TREE_CODE (arg0);
12585 : 16 : int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12586 : :
12587 : : /* (X - c) > X becomes false. */
12588 : 16 : if (code == GT_EXPR
12589 : 7 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12590 : 3 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12591 : 4 : return constant_boolean_node (0, type);
12592 : :
12593 : : /* Likewise (X + c) < X becomes false. */
12594 : 12 : if (code == LT_EXPR
12595 : 2 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12596 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12597 : 2 : return constant_boolean_node (0, type);
12598 : :
12599 : : /* Convert (X - c) <= X to true. */
12600 : 10 : if (!HONOR_NANS (arg1)
12601 : 6 : && code == LE_EXPR
12602 : 14 : && ((code0 == MINUS_EXPR && is_positive >= 0)
12603 : 0 : || (code0 == PLUS_EXPR && is_positive <= 0)))
12604 : 4 : return constant_boolean_node (1, type);
12605 : :
12606 : : /* Convert (X + c) >= X to true. */
12607 : 6 : if (!HONOR_NANS (arg1)
12608 : 2 : && code == GE_EXPR
12609 : 8 : && ((code0 == PLUS_EXPR && is_positive >= 0)
12610 : 0 : || (code0 == MINUS_EXPR && is_positive <= 0)))
12611 : 2 : return constant_boolean_node (1, type);
12612 : : }
12613 : :
12614 : : /* If we are comparing an ABS_EXPR with a constant, we can
12615 : : convert all the cases into explicit comparisons, but they may
12616 : : well not be faster than doing the ABS and one comparison.
12617 : : But ABS (X) <= C is a range comparison, which becomes a subtraction
12618 : : and a comparison, and is probably faster. */
12619 : 36396100 : if (code == LE_EXPR
12620 : 6976093 : && TREE_CODE (arg1) == INTEGER_CST
12621 : 4907894 : && TREE_CODE (arg0) == ABS_EXPR
12622 : 703 : && ! TREE_SIDE_EFFECTS (arg0)
12623 : 703 : && (tem = negate_expr (arg1)) != 0
12624 : 703 : && TREE_CODE (tem) == INTEGER_CST
12625 : 36396803 : && !TREE_OVERFLOW (tem))
12626 : 1406 : return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12627 : : build2 (GE_EXPR, type,
12628 : 703 : TREE_OPERAND (arg0, 0), tem),
12629 : : build2 (LE_EXPR, type,
12630 : 1406 : TREE_OPERAND (arg0, 0), arg1));
12631 : :
12632 : : /* Convert ABS_EXPR<x> >= 0 to true. */
12633 : 36395397 : strict_overflow_p = false;
12634 : 36395397 : if (code == GE_EXPR
12635 : 3853725 : && (integer_zerop (arg1)
12636 : 2974666 : || (! HONOR_NANS (arg0)
12637 : 2359207 : && real_zerop (arg1)))
12638 : 37274669 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12639 : : {
12640 : 1174 : if (strict_overflow_p)
12641 : 6 : fold_overflow_warning (("assuming signed overflow does not occur "
12642 : : "when simplifying comparison of "
12643 : : "absolute value and zero"),
12644 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12645 : 1174 : return omit_one_operand_loc (loc, type,
12646 : : constant_boolean_node (true, type),
12647 : 1174 : arg0);
12648 : : }
12649 : :
12650 : : /* Convert ABS_EXPR<x> < 0 to false. */
12651 : 36394223 : strict_overflow_p = false;
12652 : 36394223 : if (code == LT_EXPR
12653 : 12241940 : && (integer_zerop (arg1) || real_zerop (arg1))
12654 : 39264782 : && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12655 : : {
12656 : 3069 : if (strict_overflow_p)
12657 : 193 : fold_overflow_warning (("assuming signed overflow does not occur "
12658 : : "when simplifying comparison of "
12659 : : "absolute value and zero"),
12660 : : WARN_STRICT_OVERFLOW_CONDITIONAL);
12661 : 3069 : return omit_one_operand_loc (loc, type,
12662 : : constant_boolean_node (false, type),
12663 : 3069 : arg0);
12664 : : }
12665 : :
12666 : : /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12667 : : and similarly for >= into !=. */
12668 : 36391154 : if ((code == LT_EXPR || code == GE_EXPR)
12669 : 16091422 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12670 : 4913893 : && TREE_CODE (arg1) == LSHIFT_EXPR
12671 : 36392679 : && integer_onep (TREE_OPERAND (arg1, 0)))
12672 : 4070 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12673 : 1361 : build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12674 : 1361 : TREE_OPERAND (arg1, 1)),
12675 : 2722 : build_zero_cst (TREE_TYPE (arg0)));
12676 : :
12677 : : /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12678 : : otherwise Y might be >= # of bits in X's type and thus e.g.
12679 : : (unsigned char) (1 << Y) for Y 15 might be 0.
12680 : : If the cast is widening, then 1 << Y should have unsigned type,
12681 : : otherwise if Y is number of bits in the signed shift type minus 1,
12682 : : we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12683 : : 31 might be 0xffffffff80000000. */
12684 : 36389793 : if ((code == LT_EXPR || code == GE_EXPR)
12685 : 16090061 : && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12686 : 5442697 : || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12687 : 10669268 : && TYPE_UNSIGNED (TREE_TYPE (arg0))
12688 : 3650746 : && CONVERT_EXPR_P (arg1)
12689 : 1058505 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12690 : 42 : && (element_precision (TREE_TYPE (arg1))
12691 : 21 : >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12692 : 14 : && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12693 : 14 : || (element_precision (TREE_TYPE (arg1))
12694 : 7 : == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12695 : 36389800 : && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12696 : : {
12697 : 7 : tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12698 : 7 : TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12699 : 21 : return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12700 : 7 : fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12701 : 14 : build_zero_cst (TREE_TYPE (arg0)));
12702 : : }
12703 : :
12704 : : return NULL_TREE;
12705 : :
12706 : 5454983 : case UNORDERED_EXPR:
12707 : 5454983 : case ORDERED_EXPR:
12708 : 5454983 : case UNLT_EXPR:
12709 : 5454983 : case UNLE_EXPR:
12710 : 5454983 : case UNGT_EXPR:
12711 : 5454983 : case UNGE_EXPR:
12712 : 5454983 : case UNEQ_EXPR:
12713 : 5454983 : case LTGT_EXPR:
12714 : : /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12715 : 5454983 : {
12716 : 5454983 : tree targ0 = strip_float_extensions (arg0);
12717 : 5454983 : tree targ1 = strip_float_extensions (arg1);
12718 : 5454983 : tree newtype = TREE_TYPE (targ0);
12719 : :
12720 : 5454983 : if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype))
12721 : 1289 : newtype = TREE_TYPE (targ1);
12722 : :
12723 : 5454983 : if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))
12724 : 5454983 : && (!VECTOR_TYPE_P (type) || is_truth_type_for (newtype, type)))
12725 : 328 : return fold_build2_loc (loc, code, type,
12726 : : fold_convert_loc (loc, newtype, targ0),
12727 : 328 : fold_convert_loc (loc, newtype, targ1));
12728 : : }
12729 : :
12730 : : return NULL_TREE;
12731 : :
12732 : 5512096 : case COMPOUND_EXPR:
12733 : : /* When pedantic, a compound expression can be neither an lvalue
12734 : : nor an integer constant expression. */
12735 : 5512096 : if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12736 : : return NULL_TREE;
12737 : : /* Don't let (0, 0) be null pointer constant. */
12738 : 443161 : tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12739 : 443161 : : fold_convert_loc (loc, type, arg1);
12740 : : return tem;
12741 : :
12742 : : default:
12743 : : return NULL_TREE;
12744 : : } /* switch (code) */
12745 : : }
12746 : :
12747 : : /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12748 : : ((A & N) + B) & M -> (A + B) & M
12749 : : Similarly if (N & M) == 0,
12750 : : ((A | N) + B) & M -> (A + B) & M
12751 : : and for - instead of + (or unary - instead of +)
12752 : : and/or ^ instead of |.
12753 : : If B is constant and (B & M) == 0, fold into A & M.
12754 : :
12755 : : This function is a helper for match.pd patterns. Return non-NULL
12756 : : type in which the simplified operation should be performed only
12757 : : if any optimization is possible.
12758 : :
12759 : : ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12760 : : then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12761 : : Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12762 : : +/-. */
12763 : : tree
12764 : 1288855 : fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12765 : : tree arg00, enum tree_code code00, tree arg000, tree arg001,
12766 : : tree arg01, enum tree_code code01, tree arg010, tree arg011,
12767 : : tree *pmop)
12768 : : {
12769 : 1288855 : gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12770 : 1288855 : gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12771 : 1288855 : wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12772 : 2577710 : if (~cst1 == 0
12773 : 3862203 : || (cst1 & (cst1 + 1)) != 0
12774 : 1078807 : || !INTEGRAL_TYPE_P (type)
12775 : 1078807 : || (!TYPE_OVERFLOW_WRAPS (type)
12776 : 40434 : && TREE_CODE (type) != INTEGER_TYPE)
12777 : 4733143 : || (wi::max_value (type) & cst1) != cst1)
12778 : : return NULL_TREE;
12779 : :
12780 : 1078807 : enum tree_code codes[2] = { code00, code01 };
12781 : 1078807 : tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12782 : 1078807 : int which = 0;
12783 : 1078807 : wide_int cst0;
12784 : :
12785 : : /* Now we know that arg0 is (C + D) or (C - D) or -C and
12786 : : arg1 (M) is == (1LL << cst) - 1.
12787 : : Store C into PMOP[0] and D into PMOP[1]. */
12788 : 1078807 : pmop[0] = arg00;
12789 : 1078807 : pmop[1] = arg01;
12790 : 1078807 : which = code != NEGATE_EXPR;
12791 : :
12792 : 3235525 : for (; which >= 0; which--)
12793 : 2156718 : switch (codes[which])
12794 : : {
12795 : 20723 : case BIT_AND_EXPR:
12796 : 20723 : case BIT_IOR_EXPR:
12797 : 20723 : case BIT_XOR_EXPR:
12798 : 20723 : gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12799 : 20723 : cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12800 : 20723 : if (codes[which] == BIT_AND_EXPR)
12801 : : {
12802 : 20609 : if (cst0 != cst1)
12803 : : break;
12804 : : }
12805 : 114 : else if (cst0 != 0)
12806 : : break;
12807 : : /* If C or D is of the form (A & N) where
12808 : : (N & M) == M, or of the form (A | N) or
12809 : : (A ^ N) where (N & M) == 0, replace it with A. */
12810 : 19182 : pmop[which] = arg0xx[2 * which];
12811 : 19182 : break;
12812 : 2135995 : case ERROR_MARK:
12813 : 2135995 : if (TREE_CODE (pmop[which]) != INTEGER_CST)
12814 : : break;
12815 : : /* If C or D is a N where (N & M) == 0, it can be
12816 : : omitted (replaced with 0). */
12817 : 915012 : if ((code == PLUS_EXPR
12818 : 198846 : || (code == MINUS_EXPR && which == 0))
12819 : 656352 : && (cst1 & wi::to_wide (pmop[which])) == 0)
12820 : 135904 : pmop[which] = build_int_cst (type, 0);
12821 : : /* Similarly, with C - N where (-N & M) == 0. */
12822 : 915012 : if (code == MINUS_EXPR
12823 : 457506 : && which == 1
12824 : 648914 : && (cst1 & -wi::to_wide (pmop[which])) == 0)
12825 : 182030 : pmop[which] = build_int_cst (type, 0);
12826 : : break;
12827 : 0 : default:
12828 : 0 : gcc_unreachable ();
12829 : : }
12830 : :
12831 : : /* Only build anything new if we optimized one or both arguments above. */
12832 : 1078807 : if (pmop[0] == arg00 && pmop[1] == arg01)
12833 : : return NULL_TREE;
12834 : :
12835 : 336453 : if (TYPE_OVERFLOW_WRAPS (type))
12836 : : return type;
12837 : : else
12838 : 2256 : return unsigned_type_for (type);
12839 : 1078807 : }
12840 : :
12841 : : /* Used by contains_label_[p1]. */
12842 : :
12843 : : struct contains_label_data
12844 : : {
12845 : : hash_set<tree> *pset;
12846 : : bool inside_switch_p;
12847 : : };
12848 : :
12849 : : /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12850 : : a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12851 : : return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12852 : :
12853 : : static tree
12854 : 2371683 : contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12855 : : {
12856 : 2371683 : contains_label_data *d = (contains_label_data *) data;
12857 : 2371683 : switch (TREE_CODE (*tp))
12858 : : {
12859 : : case LABEL_EXPR:
12860 : : return *tp;
12861 : :
12862 : 0 : case CASE_LABEL_EXPR:
12863 : 0 : if (!d->inside_switch_p)
12864 : : return *tp;
12865 : : return NULL_TREE;
12866 : :
12867 : 0 : case SWITCH_EXPR:
12868 : 0 : if (!d->inside_switch_p)
12869 : : {
12870 : 0 : if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12871 : 0 : return *tp;
12872 : 0 : d->inside_switch_p = true;
12873 : 0 : if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12874 : 0 : return *tp;
12875 : 0 : d->inside_switch_p = false;
12876 : 0 : *walk_subtrees = 0;
12877 : : }
12878 : : return NULL_TREE;
12879 : :
12880 : 5896 : case GOTO_EXPR:
12881 : 5896 : *walk_subtrees = 0;
12882 : 5896 : return NULL_TREE;
12883 : :
12884 : : default:
12885 : : return NULL_TREE;
12886 : : }
12887 : : }
12888 : :
12889 : : /* Return whether the sub-tree ST contains a label which is accessible from
12890 : : outside the sub-tree. */
12891 : :
12892 : : static bool
12893 : 210958 : contains_label_p (tree st)
12894 : : {
12895 : 210958 : hash_set<tree> pset;
12896 : 210958 : contains_label_data data = { &pset, false };
12897 : 210958 : return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12898 : 210958 : }
12899 : :
12900 : : /* Fold a ternary expression of code CODE and type TYPE with operands
12901 : : OP0, OP1, and OP2. Return the folded expression if folding is
12902 : : successful. Otherwise, return NULL_TREE. */
12903 : :
12904 : : tree
12905 : 26551284 : fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12906 : : tree op0, tree op1, tree op2)
12907 : : {
12908 : 26551284 : tree tem;
12909 : 26551284 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12910 : 26551284 : enum tree_code_class kind = TREE_CODE_CLASS (code);
12911 : :
12912 : 26551284 : gcc_assert (IS_EXPR_CODE_CLASS (kind)
12913 : : && TREE_CODE_LENGTH (code) == 3);
12914 : :
12915 : : /* If this is a commutative operation, and OP0 is a constant, move it
12916 : : to OP1 to reduce the number of tests below. */
12917 : 26551284 : if (commutative_ternary_tree_code (code)
12918 : 26551284 : && tree_swap_operands_p (op0, op1))
12919 : 41 : return fold_build3_loc (loc, code, type, op1, op0, op2);
12920 : :
12921 : 26551243 : tem = generic_simplify (loc, code, type, op0, op1, op2);
12922 : 26551243 : if (tem)
12923 : : return tem;
12924 : :
12925 : : /* Strip any conversions that don't change the mode. This is safe
12926 : : for every expression, except for a comparison expression because
12927 : : its signedness is derived from its operands. So, in the latter
12928 : : case, only strip conversions that don't change the signedness.
12929 : :
12930 : : Note that this is done as an internal manipulation within the
12931 : : constant folder, in order to find the simplest representation of
12932 : : the arguments so that their form can be studied. In any cases,
12933 : : the appropriate type conversions should be put back in the tree
12934 : : that will get out of the constant folder. */
12935 : 25650706 : if (op0)
12936 : : {
12937 : 25586633 : arg0 = op0;
12938 : 25586633 : STRIP_NOPS (arg0);
12939 : : }
12940 : :
12941 : 25650706 : if (op1)
12942 : : {
12943 : 25650706 : arg1 = op1;
12944 : 25650706 : STRIP_NOPS (arg1);
12945 : : }
12946 : :
12947 : 25650706 : if (op2)
12948 : : {
12949 : 12512362 : arg2 = op2;
12950 : 12512362 : STRIP_NOPS (arg2);
12951 : : }
12952 : :
12953 : 25650706 : switch (code)
12954 : : {
12955 : 13137873 : case COMPONENT_REF:
12956 : 13137873 : if (TREE_CODE (arg0) == CONSTRUCTOR
12957 : 13137873 : && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12958 : : {
12959 : : unsigned HOST_WIDE_INT idx;
12960 : : tree field, value;
12961 : 862 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12962 : 671 : if (field == arg1)
12963 : : return value;
12964 : : }
12965 : : return NULL_TREE;
12966 : :
12967 : 10473532 : case COND_EXPR:
12968 : 10473532 : case VEC_COND_EXPR:
12969 : : /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12970 : : so all simple results must be passed through pedantic_non_lvalue. */
12971 : 10473532 : if (TREE_CODE (arg0) == INTEGER_CST)
12972 : : {
12973 : 338868 : tree unused_op = integer_zerop (arg0) ? op1 : op2;
12974 : 338868 : tem = integer_zerop (arg0) ? op2 : op1;
12975 : : /* Only optimize constant conditions when the selected branch
12976 : : has the same type as the COND_EXPR. This avoids optimizing
12977 : : away "c ? x : throw", where the throw has a void type.
12978 : : Avoid throwing away that operand which contains label. */
12979 : 338868 : if ((!TREE_SIDE_EFFECTS (unused_op)
12980 : 210958 : || !contains_label_p (unused_op))
12981 : 545476 : && (! VOID_TYPE_P (TREE_TYPE (tem))
12982 : 258554 : || VOID_TYPE_P (type)))
12983 : 325320 : return protected_set_expr_location_unshare (tem, loc);
12984 : 13548 : return NULL_TREE;
12985 : : }
12986 : 10134664 : else if (TREE_CODE (arg0) == VECTOR_CST)
12987 : : {
12988 : 1526 : unsigned HOST_WIDE_INT nelts;
12989 : 1526 : if ((TREE_CODE (arg1) == VECTOR_CST
12990 : 267 : || TREE_CODE (arg1) == CONSTRUCTOR)
12991 : 1259 : && (TREE_CODE (arg2) == VECTOR_CST
12992 : 0 : || TREE_CODE (arg2) == CONSTRUCTOR)
12993 : 3052 : && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12994 : : {
12995 : 1259 : vec_perm_builder sel (nelts, nelts, 1);
12996 : 13603 : for (unsigned int i = 0; i < nelts; i++)
12997 : : {
12998 : 12344 : tree val = VECTOR_CST_ELT (arg0, i);
12999 : 12344 : if (integer_all_onesp (val))
13000 : 6635 : sel.quick_push (i);
13001 : 5709 : else if (integer_zerop (val))
13002 : 5709 : sel.quick_push (nelts + i);
13003 : : else /* Currently unreachable. */
13004 : 1227 : return NULL_TREE;
13005 : : }
13006 : 1259 : vec_perm_indices indices (sel, 2, nelts);
13007 : 1259 : tree t = fold_vec_perm (type, arg1, arg2, indices);
13008 : 1259 : if (t != NULL_TREE)
13009 : 1227 : return t;
13010 : 2486 : }
13011 : : }
13012 : :
13013 : : /* If we have A op B ? A : C, we may be able to convert this to a
13014 : : simpler expression, depending on the operation and the values
13015 : : of B and C. Signed zeros prevent all of these transformations,
13016 : : for reasons given above each one.
13017 : :
13018 : : Also try swapping the arguments and inverting the conditional. */
13019 : 10133437 : if (COMPARISON_CLASS_P (arg0)
13020 : 8499617 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
13021 : 10252610 : && !HONOR_SIGNED_ZEROS (op1))
13022 : : {
13023 : 108800 : tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
13024 : 108800 : TREE_OPERAND (arg0, 0),
13025 : 108800 : TREE_OPERAND (arg0, 1),
13026 : : op1, op2);
13027 : 108800 : if (tem)
13028 : : return tem;
13029 : : }
13030 : :
13031 : 10126956 : if (COMPARISON_CLASS_P (arg0)
13032 : 8493136 : && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
13033 : 10547019 : && !HONOR_SIGNED_ZEROS (op2))
13034 : : {
13035 : 330170 : enum tree_code comp_code = TREE_CODE (arg0);
13036 : 330170 : tree arg00 = TREE_OPERAND (arg0, 0);
13037 : 330170 : tree arg01 = TREE_OPERAND (arg0, 1);
13038 : 330170 : comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
13039 : 330170 : if (comp_code != ERROR_MARK)
13040 : 330170 : tem = fold_cond_expr_with_comparison (loc, type, comp_code,
13041 : : arg00,
13042 : : arg01,
13043 : : op2, op1);
13044 : 330170 : if (tem)
13045 : : return tem;
13046 : : }
13047 : :
13048 : : /* If the second operand is simpler than the third, swap them
13049 : : since that produces better jump optimization results. */
13050 : 9887514 : if (truth_value_p (TREE_CODE (arg0))
13051 : 9887514 : && tree_swap_operands_p (op1, op2))
13052 : : {
13053 : 1678743 : location_t loc0 = expr_location_or (arg0, loc);
13054 : : /* See if this can be inverted. If it can't, possibly because
13055 : : it was a floating-point inequality comparison, don't do
13056 : : anything. */
13057 : 1678743 : tem = fold_invert_truthvalue (loc0, arg0);
13058 : 1678743 : if (tem)
13059 : 1115342 : return fold_build3_loc (loc, code, type, tem, op2, op1);
13060 : : }
13061 : :
13062 : : /* Convert A ? 1 : 0 to simply A. */
13063 : 8772172 : if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13064 : 8464847 : : (integer_onep (op1)
13065 : 383990 : && !VECTOR_TYPE_P (type)))
13066 : 580486 : && integer_zerop (op2)
13067 : : /* If we try to convert OP0 to our type, the
13068 : : call to fold will try to move the conversion inside
13069 : : a COND, which will recurse. In that case, the COND_EXPR
13070 : : is probably the best choice, so leave it alone. */
13071 : 9648322 : && type == TREE_TYPE (arg0))
13072 : 30164 : return protected_set_expr_location_unshare (arg0, loc);
13073 : :
13074 : : /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13075 : : over COND_EXPR in cases such as floating point comparisons. */
13076 : 8742008 : if (integer_zerop (op1)
13077 : 247444 : && code == COND_EXPR
13078 : 245721 : && integer_onep (op2)
13079 : 33247 : && !VECTOR_TYPE_P (type)
13080 : 8775255 : && truth_value_p (TREE_CODE (arg0)))
13081 : 31481 : return fold_convert_loc (loc, type,
13082 : 31481 : invert_truthvalue_loc (loc, arg0));
13083 : :
13084 : : /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13085 : 8710527 : if (TREE_CODE (arg0) == LT_EXPR
13086 : 968111 : && integer_zerop (TREE_OPERAND (arg0, 1))
13087 : 15650 : && integer_zerop (op2)
13088 : 8711448 : && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13089 : : {
13090 : : /* sign_bit_p looks through both zero and sign extensions,
13091 : : but for this optimization only sign extensions are
13092 : : usable. */
13093 : 56 : tree tem2 = TREE_OPERAND (arg0, 0);
13094 : 56 : while (tem != tem2)
13095 : : {
13096 : 0 : if (TREE_CODE (tem2) != NOP_EXPR
13097 : 0 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13098 : : {
13099 : : tem = NULL_TREE;
13100 : : break;
13101 : : }
13102 : 0 : tem2 = TREE_OPERAND (tem2, 0);
13103 : : }
13104 : : /* sign_bit_p only checks ARG1 bits within A's precision.
13105 : : If <sign bit of A> has wider type than A, bits outside
13106 : : of A's precision in <sign bit of A> need to be checked.
13107 : : If they are all 0, this optimization needs to be done
13108 : : in unsigned A's type, if they are all 1 in signed A's type,
13109 : : otherwise this can't be done. */
13110 : 56 : if (tem
13111 : 56 : && TYPE_PRECISION (TREE_TYPE (tem))
13112 : 56 : < TYPE_PRECISION (TREE_TYPE (arg1))
13113 : 112 : && TYPE_PRECISION (TREE_TYPE (tem))
13114 : 56 : < TYPE_PRECISION (type))
13115 : : {
13116 : 56 : int inner_width, outer_width;
13117 : 56 : tree tem_type;
13118 : :
13119 : 56 : inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13120 : 56 : outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13121 : 56 : if (outer_width > TYPE_PRECISION (type))
13122 : 0 : outer_width = TYPE_PRECISION (type);
13123 : :
13124 : 56 : wide_int mask = wi::shifted_mask
13125 : 56 : (inner_width, outer_width - inner_width, false,
13126 : 56 : TYPE_PRECISION (TREE_TYPE (arg1)));
13127 : :
13128 : 56 : wide_int common = mask & wi::to_wide (arg1);
13129 : 56 : if (common == mask)
13130 : : {
13131 : 28 : tem_type = signed_type_for (TREE_TYPE (tem));
13132 : 28 : tem = fold_convert_loc (loc, tem_type, tem);
13133 : : }
13134 : 28 : else if (common == 0)
13135 : : {
13136 : 0 : tem_type = unsigned_type_for (TREE_TYPE (tem));
13137 : 0 : tem = fold_convert_loc (loc, tem_type, tem);
13138 : : }
13139 : : else
13140 : : tem = NULL;
13141 : 56 : }
13142 : :
13143 : 56 : if (tem)
13144 : 28 : return
13145 : 56 : fold_convert_loc (loc, type,
13146 : : fold_build2_loc (loc, BIT_AND_EXPR,
13147 : 28 : TREE_TYPE (tem), tem,
13148 : : fold_convert_loc (loc,
13149 : 28 : TREE_TYPE (tem),
13150 : 28 : arg1)));
13151 : : }
13152 : :
13153 : : /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13154 : : already handled above. */
13155 : 8710499 : if (TREE_CODE (arg0) == BIT_AND_EXPR
13156 : 325 : && integer_onep (TREE_OPERAND (arg0, 1))
13157 : 3 : && integer_zerop (op2)
13158 : 8710499 : && integer_pow2p (arg1))
13159 : : {
13160 : 0 : tree tem = TREE_OPERAND (arg0, 0);
13161 : 0 : STRIP_NOPS (tem);
13162 : 0 : if (TREE_CODE (tem) == RSHIFT_EXPR
13163 : 0 : && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13164 : 0 : && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13165 : 0 : == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13166 : 0 : return fold_build2_loc (loc, BIT_AND_EXPR, type,
13167 : : fold_convert_loc (loc, type,
13168 : 0 : TREE_OPERAND (tem, 0)),
13169 : 0 : op1);
13170 : : }
13171 : :
13172 : : /* A & N ? N : 0 is simply A & N if N is a power of two. This
13173 : : is probably obsolete because the first operand should be a
13174 : : truth value (that's why we have the two cases above), but let's
13175 : : leave it in until we can confirm this for all front-ends. */
13176 : 8710499 : if (integer_zerop (op2)
13177 : 1874527 : && TREE_CODE (arg0) == NE_EXPR
13178 : 508765 : && integer_zerop (TREE_OPERAND (arg0, 1))
13179 : 278816 : && integer_pow2p (arg1)
13180 : 31040 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13181 : 91 : && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13182 : : arg1, OEP_ONLY_CONST)
13183 : : /* operand_equal_p compares just value, not precision, so e.g.
13184 : : arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13185 : : second operand 32-bit -128, which is not a power of two (or vice
13186 : : versa. */
13187 : 8710499 : && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13188 : 0 : return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13189 : :
13190 : : /* Disable the transformations below for vectors, since
13191 : : fold_binary_op_with_conditional_arg may undo them immediately,
13192 : : yielding an infinite loop. */
13193 : 8710499 : if (code == VEC_COND_EXPR)
13194 : : return NULL_TREE;
13195 : :
13196 : : /* Convert A ? B : 0 into A && B if A and B are truth values. */
13197 : 8403174 : if (integer_zerop (op2)
13198 : 1618777 : && truth_value_p (TREE_CODE (arg0))
13199 : 1501296 : && truth_value_p (TREE_CODE (arg1))
13200 : 8433676 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13201 : 30502 : return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13202 : : : TRUTH_ANDIF_EXPR,
13203 : 30502 : type, fold_convert_loc (loc, type, arg0), op1);
13204 : :
13205 : : /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13206 : 8372672 : if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13207 : 433283 : && truth_value_p (TREE_CODE (arg0))
13208 : 299435 : && truth_value_p (TREE_CODE (arg1))
13209 : 8406862 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13210 : : {
13211 : 34190 : location_t loc0 = expr_location_or (arg0, loc);
13212 : : /* Only perform transformation if ARG0 is easily inverted. */
13213 : 34190 : tem = fold_invert_truthvalue (loc0, arg0);
13214 : 34190 : if (tem)
13215 : 33926 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13216 : : ? BIT_IOR_EXPR
13217 : : : TRUTH_ORIF_EXPR,
13218 : : type, fold_convert_loc (loc, type, tem),
13219 : 33926 : op1);
13220 : : }
13221 : :
13222 : : /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13223 : 8338746 : if (integer_zerop (arg1)
13224 : 214319 : && truth_value_p (TREE_CODE (arg0))
13225 : 47812 : && truth_value_p (TREE_CODE (op2))
13226 : 8338774 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13227 : : {
13228 : 28 : location_t loc0 = expr_location_or (arg0, loc);
13229 : : /* Only perform transformation if ARG0 is easily inverted. */
13230 : 28 : tem = fold_invert_truthvalue (loc0, arg0);
13231 : 28 : if (tem)
13232 : 0 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13233 : : ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13234 : : type, fold_convert_loc (loc, type, tem),
13235 : 0 : op2);
13236 : : }
13237 : :
13238 : : /* Convert A ? 1 : B into A || B if A and B are truth values. */
13239 : 8338746 : if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13240 : 353826 : && truth_value_p (TREE_CODE (arg0))
13241 : 275105 : && truth_value_p (TREE_CODE (op2))
13242 : 8338932 : && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13243 : 186 : return fold_build2_loc (loc, code == VEC_COND_EXPR
13244 : : ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13245 : 186 : type, fold_convert_loc (loc, type, arg0), op2);
13246 : :
13247 : : return NULL_TREE;
13248 : :
13249 : 0 : case CALL_EXPR:
13250 : : /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13251 : : of fold_ternary on them. */
13252 : 0 : gcc_unreachable ();
13253 : :
13254 : 637384 : case BIT_FIELD_REF:
13255 : 637384 : if (TREE_CODE (arg0) == VECTOR_CST
13256 : 28119 : && (type == TREE_TYPE (TREE_TYPE (arg0))
13257 : 1780 : || (VECTOR_TYPE_P (type)
13258 : 1084 : && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13259 : 27403 : && tree_fits_uhwi_p (op1)
13260 : 664787 : && tree_fits_uhwi_p (op2))
13261 : : {
13262 : 27403 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13263 : 27403 : unsigned HOST_WIDE_INT width
13264 : 27403 : = (TREE_CODE (eltype) == BOOLEAN_TYPE
13265 : 27403 : ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13266 : 27403 : unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13267 : 27403 : unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13268 : :
13269 : 27403 : if (n != 0
13270 : 27403 : && (idx % width) == 0
13271 : 27403 : && (n % width) == 0
13272 : 54806 : && known_le ((idx + n) / width,
13273 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13274 : : {
13275 : 27403 : idx = idx / width;
13276 : 27403 : n = n / width;
13277 : :
13278 : 27403 : if (TREE_CODE (arg0) == VECTOR_CST)
13279 : : {
13280 : 27403 : if (n == 1)
13281 : : {
13282 : 26343 : tem = VECTOR_CST_ELT (arg0, idx);
13283 : 26343 : if (VECTOR_TYPE_P (type))
13284 : 4 : tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13285 : 26343 : return tem;
13286 : : }
13287 : :
13288 : 1060 : tree_vector_builder vals (type, n, 1);
13289 : 6548 : for (unsigned i = 0; i < n; ++i)
13290 : 5488 : vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13291 : 1060 : return vals.build ();
13292 : 1060 : }
13293 : : }
13294 : : }
13295 : :
13296 : : /* On constants we can use native encode/interpret to constant
13297 : : fold (nearly) all BIT_FIELD_REFs. */
13298 : 609981 : if (CONSTANT_CLASS_P (arg0)
13299 : 1509 : && can_native_interpret_type_p (type)
13300 : : && BITS_PER_UNIT == 8
13301 : 1509 : && tree_fits_uhwi_p (op1)
13302 : 611490 : && tree_fits_uhwi_p (op2))
13303 : : {
13304 : 1509 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13305 : 1509 : unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13306 : : /* Limit us to a reasonable amount of work. To relax the
13307 : : other limitations we need bit-shifting of the buffer
13308 : : and rounding up the size. */
13309 : 1509 : if (bitpos % BITS_PER_UNIT == 0
13310 : 1509 : && bitsize % BITS_PER_UNIT == 0
13311 : 1509 : && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13312 : : {
13313 : 1509 : unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13314 : 1509 : unsigned HOST_WIDE_INT len
13315 : 1509 : = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13316 : 1509 : bitpos / BITS_PER_UNIT);
13317 : 1509 : if (len > 0
13318 : 1509 : && len * BITS_PER_UNIT >= bitsize)
13319 : : {
13320 : 1509 : tree v = native_interpret_expr (type, b,
13321 : : bitsize / BITS_PER_UNIT);
13322 : 1509 : if (v)
13323 : 1503 : return v;
13324 : : }
13325 : : }
13326 : : }
13327 : :
13328 : : return NULL_TREE;
13329 : :
13330 : 690201 : case VEC_PERM_EXPR:
13331 : : /* Perform constant folding of BIT_INSERT_EXPR. */
13332 : 690201 : if (TREE_CODE (arg2) == VECTOR_CST
13333 : 678972 : && TREE_CODE (op0) == VECTOR_CST
13334 : 15208 : && TREE_CODE (op1) == VECTOR_CST)
13335 : : {
13336 : : /* Build a vector of integers from the tree mask. */
13337 : 3915 : vec_perm_builder builder;
13338 : 3915 : if (!tree_to_vec_perm_builder (&builder, arg2))
13339 : : return NULL_TREE;
13340 : :
13341 : : /* Create a vec_perm_indices for the integer vector. */
13342 : 3915 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13343 : 3915 : bool single_arg = (op0 == op1);
13344 : 7830 : vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13345 : 3915 : return fold_vec_perm (type, op0, op1, sel);
13346 : 7830 : }
13347 : : return NULL_TREE;
13348 : :
13349 : 13629 : case BIT_INSERT_EXPR:
13350 : : /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13351 : 13629 : if (TREE_CODE (arg0) == INTEGER_CST
13352 : 14 : && TREE_CODE (arg1) == INTEGER_CST)
13353 : : {
13354 : 2 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13355 : 2 : unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13356 : 2 : if (BYTES_BIG_ENDIAN)
13357 : : bitpos = TYPE_PRECISION (type) - bitpos - bitsize;
13358 : 2 : wide_int tem = (wi::to_wide (arg0)
13359 : 4 : & wi::shifted_mask (bitpos, bitsize, true,
13360 : 4 : TYPE_PRECISION (type)));
13361 : 2 : wide_int tem2
13362 : 4 : = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13363 : 2 : bitsize), bitpos);
13364 : 2 : return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13365 : 2 : }
13366 : 13627 : else if (TREE_CODE (arg0) == VECTOR_CST
13367 : 906 : && CONSTANT_CLASS_P (arg1)
13368 : 13929 : && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13369 : 302 : TREE_TYPE (arg1)))
13370 : : {
13371 : 302 : unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13372 : 302 : unsigned HOST_WIDE_INT elsize
13373 : 302 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13374 : 302 : if (bitpos % elsize == 0)
13375 : : {
13376 : 302 : unsigned k = bitpos / elsize;
13377 : 302 : unsigned HOST_WIDE_INT nelts;
13378 : 302 : if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13379 : 26551284 : return arg0;
13380 : 290 : else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13381 : : {
13382 : 290 : tree_vector_builder elts (type, nelts, 1);
13383 : 290 : elts.quick_grow (nelts);
13384 : 1306 : for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13385 : 1016 : elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13386 : 290 : return elts.build ();
13387 : 290 : }
13388 : : }
13389 : : }
13390 : : return NULL_TREE;
13391 : :
13392 : : default:
13393 : : return NULL_TREE;
13394 : : } /* switch (code) */
13395 : : }
13396 : :
13397 : : /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13398 : : of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13399 : : constructor element index of the value returned. If the element is
13400 : : not found NULL_TREE is returned and *CTOR_IDX is updated to
13401 : : the index of the element after the ACCESS_INDEX position (which
13402 : : may be outside of the CTOR array). */
13403 : :
13404 : : tree
13405 : 766042 : get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13406 : : unsigned *ctor_idx)
13407 : : {
13408 : 766042 : tree index_type = NULL_TREE;
13409 : 766042 : signop index_sgn = UNSIGNED;
13410 : 766042 : offset_int low_bound = 0;
13411 : :
13412 : 766042 : if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13413 : : {
13414 : 766042 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13415 : 766042 : if (domain_type && TYPE_MIN_VALUE (domain_type))
13416 : : {
13417 : : /* Static constructors for variably sized objects makes no sense. */
13418 : 766042 : gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13419 : 766042 : index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13420 : : /* ??? When it is obvious that the range is signed, treat it so. */
13421 : 766042 : if (TYPE_UNSIGNED (index_type)
13422 : 419351 : && TYPE_MAX_VALUE (domain_type)
13423 : 1185359 : && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13424 : 419317 : TYPE_MIN_VALUE (domain_type)))
13425 : : {
13426 : 0 : index_sgn = SIGNED;
13427 : 0 : low_bound
13428 : 0 : = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13429 : : SIGNED);
13430 : : }
13431 : : else
13432 : : {
13433 : 766042 : index_sgn = TYPE_SIGN (index_type);
13434 : 766042 : low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13435 : : }
13436 : : }
13437 : : }
13438 : :
13439 : 766042 : if (index_type)
13440 : 766042 : access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13441 : : index_sgn);
13442 : :
13443 : 766042 : offset_int index = low_bound;
13444 : 766042 : if (index_type)
13445 : 766042 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13446 : :
13447 : 766042 : offset_int max_index = index;
13448 : 766042 : unsigned cnt;
13449 : 766042 : tree cfield, cval;
13450 : 766042 : bool first_p = true;
13451 : :
13452 : 14784593 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13453 : : {
13454 : : /* Array constructor might explicitly set index, or specify a range,
13455 : : or leave index NULL meaning that it is next index after previous
13456 : : one. */
13457 : 14783117 : if (cfield)
13458 : : {
13459 : 6396614 : if (TREE_CODE (cfield) == INTEGER_CST)
13460 : 12791784 : max_index = index
13461 : 6395892 : = offset_int::from (wi::to_wide (cfield), index_sgn);
13462 : : else
13463 : : {
13464 : 722 : gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13465 : 722 : index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13466 : : index_sgn);
13467 : 722 : max_index
13468 : 722 : = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13469 : : index_sgn);
13470 : 722 : gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13471 : : }
13472 : : }
13473 : 8386503 : else if (!first_p)
13474 : : {
13475 : 8076123 : index = max_index + 1;
13476 : 8076123 : if (index_type)
13477 : 8076123 : index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13478 : 8076123 : gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13479 : 8076123 : max_index = index;
13480 : : }
13481 : : else
13482 : : first_p = false;
13483 : :
13484 : 14783117 : if (TREE_CODE (cval) == RAW_DATA_CST)
13485 : 1841 : max_index += RAW_DATA_LENGTH (cval) - 1;
13486 : :
13487 : : /* Do we have match? */
13488 : 14783117 : if (wi::cmp (access_index, index, index_sgn) >= 0)
13489 : : {
13490 : 14782826 : if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13491 : : {
13492 : 764447 : if (ctor_idx)
13493 : 764447 : *ctor_idx = cnt;
13494 : 764447 : return cval;
13495 : : }
13496 : : }
13497 : 291 : else if (in_gimple_form)
13498 : : /* We're past the element we search for. Note during parsing
13499 : : the elements might not be sorted.
13500 : : ??? We should use a binary search and a flag on the
13501 : : CONSTRUCTOR as to whether elements are sorted in declaration
13502 : : order. */
13503 : : break;
13504 : : }
13505 : 1595 : if (ctor_idx)
13506 : 1595 : *ctor_idx = cnt;
13507 : : return NULL_TREE;
13508 : : }
13509 : :
13510 : : /* Perform constant folding and related simplification of EXPR.
13511 : : The related simplifications include x*1 => x, x*0 => 0, etc.,
13512 : : and application of the associative law.
13513 : : NOP_EXPR conversions may be removed freely (as long as we
13514 : : are careful not to change the type of the overall expression).
13515 : : We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13516 : : but we can constant-fold them if they have constant operands. */
13517 : :
13518 : : #ifdef ENABLE_FOLD_CHECKING
13519 : : # define fold(x) fold_1 (x)
13520 : : static tree fold_1 (tree);
13521 : : static
13522 : : #endif
13523 : : tree
13524 : 1230592918 : fold (tree expr)
13525 : : {
13526 : 1230707781 : const tree t = expr;
13527 : 1230707781 : enum tree_code code = TREE_CODE (t);
13528 : 1230707781 : enum tree_code_class kind = TREE_CODE_CLASS (code);
13529 : 1230707781 : tree tem;
13530 : 1230707781 : location_t loc = EXPR_LOCATION (expr);
13531 : :
13532 : : /* Return right away if a constant. */
13533 : 1230707781 : if (kind == tcc_constant)
13534 : : return t;
13535 : :
13536 : : /* CALL_EXPR-like objects with variable numbers of operands are
13537 : : treated specially. */
13538 : 1117518600 : if (kind == tcc_vl_exp)
13539 : : {
13540 : 159053873 : if (code == CALL_EXPR)
13541 : : {
13542 : 159053331 : tem = fold_call_expr (loc, expr, false);
13543 : 315554448 : return tem ? tem : expr;
13544 : : }
13545 : : return expr;
13546 : : }
13547 : :
13548 : 958464727 : if (IS_EXPR_CODE_CLASS (kind))
13549 : : {
13550 : 956631394 : tree type = TREE_TYPE (t);
13551 : 956631394 : tree op0, op1, op2;
13552 : :
13553 : 956631394 : switch (TREE_CODE_LENGTH (code))
13554 : : {
13555 : 864640695 : case 1:
13556 : 864640695 : op0 = TREE_OPERAND (t, 0);
13557 : 864640695 : tem = fold_unary_loc (loc, code, type, op0);
13558 : 1477533917 : return tem ? tem : expr;
13559 : 83367670 : case 2:
13560 : 83367670 : op0 = TREE_OPERAND (t, 0);
13561 : 83367670 : op1 = TREE_OPERAND (t, 1);
13562 : 83367670 : tem = fold_binary_loc (loc, code, type, op0, op1);
13563 : 158587019 : return tem ? tem : expr;
13564 : 4114876 : case 3:
13565 : 4114876 : op0 = TREE_OPERAND (t, 0);
13566 : 4114876 : op1 = TREE_OPERAND (t, 1);
13567 : 4114876 : op2 = TREE_OPERAND (t, 2);
13568 : 4114876 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13569 : 8061502 : return tem ? tem : expr;
13570 : : default:
13571 : : break;
13572 : : }
13573 : : }
13574 : :
13575 : 6341486 : switch (code)
13576 : : {
13577 : 4405404 : case ARRAY_REF:
13578 : 4405404 : {
13579 : 4405404 : tree op0 = TREE_OPERAND (t, 0);
13580 : 4405404 : tree op1 = TREE_OPERAND (t, 1);
13581 : :
13582 : 4405404 : if (TREE_CODE (op1) == INTEGER_CST
13583 : 2864532 : && TREE_CODE (op0) == CONSTRUCTOR
13584 : 4405411 : && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13585 : : {
13586 : 7 : unsigned int idx;
13587 : 7 : tree val
13588 : 7 : = get_array_ctor_element_at_index (op0, wi::to_offset (op1),
13589 : : &idx);
13590 : 7 : if (val)
13591 : : {
13592 : 7 : if (TREE_CODE (val) != RAW_DATA_CST)
13593 : : return val;
13594 : 2 : if (CONSTRUCTOR_ELT (op0, idx)->index == NULL_TREE
13595 : 2 : || (TREE_CODE (CONSTRUCTOR_ELT (op0, idx)->index)
13596 : : != INTEGER_CST))
13597 : : return t;
13598 : 2 : offset_int o
13599 : 2 : = (wi::to_offset (op1)
13600 : 2 : - wi::to_offset (CONSTRUCTOR_ELT (op0, idx)->index));
13601 : 2 : gcc_checking_assert (o < RAW_DATA_LENGTH (val));
13602 : 2 : return build_int_cst (TREE_TYPE (val),
13603 : 2 : RAW_DATA_UCHAR_ELT (val, o.to_uhwi ()));
13604 : : }
13605 : : }
13606 : :
13607 : : return t;
13608 : : }
13609 : :
13610 : : /* Return a VECTOR_CST if possible. */
13611 : 106030 : case CONSTRUCTOR:
13612 : 106030 : {
13613 : 106030 : tree type = TREE_TYPE (t);
13614 : 106030 : if (TREE_CODE (type) != VECTOR_TYPE)
13615 : : return t;
13616 : :
13617 : : unsigned i;
13618 : : tree val;
13619 : 248088 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13620 : 214633 : if (! CONSTANT_CLASS_P (val))
13621 : : return t;
13622 : :
13623 : 33455 : return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13624 : : }
13625 : :
13626 : 114863 : case CONST_DECL:
13627 : 114863 : return fold (DECL_INITIAL (t));
13628 : :
13629 : : default:
13630 : : return t;
13631 : : } /* switch (code) */
13632 : : }
13633 : :
13634 : : #ifdef ENABLE_FOLD_CHECKING
13635 : : #undef fold
13636 : :
13637 : : static void fold_checksum_tree (const_tree, struct md5_ctx *,
13638 : : hash_table<nofree_ptr_hash<const tree_node> > *);
13639 : : static void fold_check_failed (const_tree, const_tree);
13640 : : void print_fold_checksum (const_tree);
13641 : :
13642 : : /* When --enable-checking=fold, compute a digest of expr before
13643 : : and after actual fold call to see if fold did not accidentally
13644 : : change original expr. */
13645 : :
13646 : : tree
13647 : : fold (tree expr)
13648 : : {
13649 : : tree ret;
13650 : : struct md5_ctx ctx;
13651 : : unsigned char checksum_before[16], checksum_after[16];
13652 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13653 : :
13654 : : md5_init_ctx (&ctx);
13655 : : fold_checksum_tree (expr, &ctx, &ht);
13656 : : md5_finish_ctx (&ctx, checksum_before);
13657 : : ht.empty ();
13658 : :
13659 : : ret = fold_1 (expr);
13660 : :
13661 : : md5_init_ctx (&ctx);
13662 : : fold_checksum_tree (expr, &ctx, &ht);
13663 : : md5_finish_ctx (&ctx, checksum_after);
13664 : :
13665 : : if (memcmp (checksum_before, checksum_after, 16))
13666 : : fold_check_failed (expr, ret);
13667 : :
13668 : : return ret;
13669 : : }
13670 : :
13671 : : void
13672 : : print_fold_checksum (const_tree expr)
13673 : : {
13674 : : struct md5_ctx ctx;
13675 : : unsigned char checksum[16], cnt;
13676 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13677 : :
13678 : : md5_init_ctx (&ctx);
13679 : : fold_checksum_tree (expr, &ctx, &ht);
13680 : : md5_finish_ctx (&ctx, checksum);
13681 : : for (cnt = 0; cnt < 16; ++cnt)
13682 : : fprintf (stderr, "%02x", checksum[cnt]);
13683 : : putc ('\n', stderr);
13684 : : }
13685 : :
13686 : : static void
13687 : : fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13688 : : {
13689 : : internal_error ("fold check: original tree changed by fold");
13690 : : }
13691 : :
13692 : : static void
13693 : : fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13694 : : hash_table<nofree_ptr_hash <const tree_node> > *ht)
13695 : : {
13696 : : const tree_node **slot;
13697 : : enum tree_code code;
13698 : : union tree_node *buf;
13699 : : int i, len;
13700 : :
13701 : : recursive_label:
13702 : : if (expr == NULL)
13703 : : return;
13704 : : slot = ht->find_slot (expr, INSERT);
13705 : : if (*slot != NULL)
13706 : : return;
13707 : : *slot = expr;
13708 : : code = TREE_CODE (expr);
13709 : : if (TREE_CODE_CLASS (code) == tcc_declaration
13710 : : && HAS_DECL_ASSEMBLER_NAME_P (expr))
13711 : : {
13712 : : /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13713 : : size_t sz = tree_size (expr);
13714 : : buf = XALLOCAVAR (union tree_node, sz);
13715 : : memcpy ((char *) buf, expr, sz);
13716 : : SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13717 : : buf->decl_with_vis.symtab_node = NULL;
13718 : : buf->base.nowarning_flag = 0;
13719 : : expr = (tree) buf;
13720 : : }
13721 : : else if (TREE_CODE_CLASS (code) == tcc_type
13722 : : && (TYPE_POINTER_TO (expr)
13723 : : || TYPE_REFERENCE_TO (expr)
13724 : : || TYPE_CACHED_VALUES_P (expr)
13725 : : || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13726 : : || TYPE_NEXT_VARIANT (expr)
13727 : : || TYPE_ALIAS_SET_KNOWN_P (expr)))
13728 : : {
13729 : : /* Allow these fields to be modified. */
13730 : : tree tmp;
13731 : : size_t sz = tree_size (expr);
13732 : : buf = XALLOCAVAR (union tree_node, sz);
13733 : : memcpy ((char *) buf, expr, sz);
13734 : : expr = tmp = (tree) buf;
13735 : : TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13736 : : TYPE_POINTER_TO (tmp) = NULL;
13737 : : TYPE_REFERENCE_TO (tmp) = NULL;
13738 : : TYPE_NEXT_VARIANT (tmp) = NULL;
13739 : : TYPE_ALIAS_SET (tmp) = -1;
13740 : : if (TYPE_CACHED_VALUES_P (tmp))
13741 : : {
13742 : : TYPE_CACHED_VALUES_P (tmp) = 0;
13743 : : TYPE_CACHED_VALUES (tmp) = NULL;
13744 : : }
13745 : : }
13746 : : else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13747 : : {
13748 : : /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13749 : : that and change builtins.cc etc. instead - see PR89543. */
13750 : : size_t sz = tree_size (expr);
13751 : : buf = XALLOCAVAR (union tree_node, sz);
13752 : : memcpy ((char *) buf, expr, sz);
13753 : : buf->base.nowarning_flag = 0;
13754 : : expr = (tree) buf;
13755 : : }
13756 : : md5_process_bytes (expr, tree_size (expr), ctx);
13757 : : if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13758 : : fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13759 : : if (TREE_CODE_CLASS (code) != tcc_type
13760 : : && TREE_CODE_CLASS (code) != tcc_declaration
13761 : : && code != TREE_LIST
13762 : : && code != SSA_NAME
13763 : : && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13764 : : fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13765 : : switch (TREE_CODE_CLASS (code))
13766 : : {
13767 : : case tcc_constant:
13768 : : switch (code)
13769 : : {
13770 : : case STRING_CST:
13771 : : md5_process_bytes (TREE_STRING_POINTER (expr),
13772 : : TREE_STRING_LENGTH (expr), ctx);
13773 : : break;
13774 : : case COMPLEX_CST:
13775 : : fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13776 : : fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13777 : : break;
13778 : : case VECTOR_CST:
13779 : : len = vector_cst_encoded_nelts (expr);
13780 : : for (i = 0; i < len; ++i)
13781 : : fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13782 : : break;
13783 : : default:
13784 : : break;
13785 : : }
13786 : : break;
13787 : : case tcc_exceptional:
13788 : : switch (code)
13789 : : {
13790 : : case TREE_LIST:
13791 : : fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13792 : : fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13793 : : expr = TREE_CHAIN (expr);
13794 : : goto recursive_label;
13795 : : break;
13796 : : case TREE_VEC:
13797 : : for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13798 : : fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13799 : : break;
13800 : : default:
13801 : : break;
13802 : : }
13803 : : break;
13804 : : case tcc_expression:
13805 : : case tcc_reference:
13806 : : case tcc_comparison:
13807 : : case tcc_unary:
13808 : : case tcc_binary:
13809 : : case tcc_statement:
13810 : : case tcc_vl_exp:
13811 : : len = TREE_OPERAND_LENGTH (expr);
13812 : : for (i = 0; i < len; ++i)
13813 : : fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13814 : : break;
13815 : : case tcc_declaration:
13816 : : fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13817 : : fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13818 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13819 : : {
13820 : : fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13821 : : fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13822 : : fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13823 : : fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13824 : : fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13825 : : }
13826 : :
13827 : : if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13828 : : {
13829 : : if (TREE_CODE (expr) == FUNCTION_DECL)
13830 : : {
13831 : : fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13832 : : fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13833 : : }
13834 : : fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13835 : : }
13836 : : break;
13837 : : case tcc_type:
13838 : : if (TREE_CODE (expr) == ENUMERAL_TYPE)
13839 : : fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13840 : : fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13841 : : fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13842 : : fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13843 : : fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13844 : : if (INTEGRAL_TYPE_P (expr)
13845 : : || SCALAR_FLOAT_TYPE_P (expr))
13846 : : {
13847 : : fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13848 : : fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13849 : : }
13850 : : fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13851 : : if (RECORD_OR_UNION_TYPE_P (expr))
13852 : : fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13853 : : fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13854 : : break;
13855 : : default:
13856 : : break;
13857 : : }
13858 : : }
13859 : :
13860 : : /* Helper function for outputting the checksum of a tree T. When
13861 : : debugging with gdb, you can "define mynext" to be "next" followed
13862 : : by "call debug_fold_checksum (op0)", then just trace down till the
13863 : : outputs differ. */
13864 : :
13865 : : DEBUG_FUNCTION void
13866 : : debug_fold_checksum (const_tree t)
13867 : : {
13868 : : int i;
13869 : : unsigned char checksum[16];
13870 : : struct md5_ctx ctx;
13871 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13872 : :
13873 : : md5_init_ctx (&ctx);
13874 : : fold_checksum_tree (t, &ctx, &ht);
13875 : : md5_finish_ctx (&ctx, checksum);
13876 : : ht.empty ();
13877 : :
13878 : : for (i = 0; i < 16; i++)
13879 : : fprintf (stderr, "%d ", checksum[i]);
13880 : :
13881 : : fprintf (stderr, "\n");
13882 : : }
13883 : :
13884 : : #endif
13885 : :
13886 : : /* Fold a unary tree expression with code CODE of type TYPE with an
13887 : : operand OP0. LOC is the location of the resulting expression.
13888 : : Return a folded expression if successful. Otherwise, return a tree
13889 : : expression with code CODE of type TYPE with an operand OP0. */
13890 : :
13891 : : tree
13892 : 699869127 : fold_build1_loc (location_t loc,
13893 : : enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13894 : : {
13895 : 699869127 : tree tem;
13896 : : #ifdef ENABLE_FOLD_CHECKING
13897 : : unsigned char checksum_before[16], checksum_after[16];
13898 : : struct md5_ctx ctx;
13899 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13900 : :
13901 : : md5_init_ctx (&ctx);
13902 : : fold_checksum_tree (op0, &ctx, &ht);
13903 : : md5_finish_ctx (&ctx, checksum_before);
13904 : : ht.empty ();
13905 : : #endif
13906 : :
13907 : 699869127 : tem = fold_unary_loc (loc, code, type, op0);
13908 : 699869127 : if (!tem)
13909 : 375760808 : tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13910 : :
13911 : : #ifdef ENABLE_FOLD_CHECKING
13912 : : md5_init_ctx (&ctx);
13913 : : fold_checksum_tree (op0, &ctx, &ht);
13914 : : md5_finish_ctx (&ctx, checksum_after);
13915 : :
13916 : : if (memcmp (checksum_before, checksum_after, 16))
13917 : : fold_check_failed (op0, tem);
13918 : : #endif
13919 : 699869127 : return tem;
13920 : : }
13921 : :
13922 : : /* Fold a binary tree expression with code CODE of type TYPE with
13923 : : operands OP0 and OP1. LOC is the location of the resulting
13924 : : expression. Return a folded expression if successful. Otherwise,
13925 : : return a tree expression with code CODE of type TYPE with operands
13926 : : OP0 and OP1. */
13927 : :
13928 : : tree
13929 : 559855376 : fold_build2_loc (location_t loc,
13930 : : enum tree_code code, tree type, tree op0, tree op1
13931 : : MEM_STAT_DECL)
13932 : : {
13933 : 559855376 : tree tem;
13934 : : #ifdef ENABLE_FOLD_CHECKING
13935 : : unsigned char checksum_before_op0[16],
13936 : : checksum_before_op1[16],
13937 : : checksum_after_op0[16],
13938 : : checksum_after_op1[16];
13939 : : struct md5_ctx ctx;
13940 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13941 : :
13942 : : md5_init_ctx (&ctx);
13943 : : fold_checksum_tree (op0, &ctx, &ht);
13944 : : md5_finish_ctx (&ctx, checksum_before_op0);
13945 : : ht.empty ();
13946 : :
13947 : : md5_init_ctx (&ctx);
13948 : : fold_checksum_tree (op1, &ctx, &ht);
13949 : : md5_finish_ctx (&ctx, checksum_before_op1);
13950 : : ht.empty ();
13951 : : #endif
13952 : :
13953 : 559855376 : tem = fold_binary_loc (loc, code, type, op0, op1);
13954 : 559855376 : if (!tem)
13955 : 311653038 : tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13956 : :
13957 : : #ifdef ENABLE_FOLD_CHECKING
13958 : : md5_init_ctx (&ctx);
13959 : : fold_checksum_tree (op0, &ctx, &ht);
13960 : : md5_finish_ctx (&ctx, checksum_after_op0);
13961 : : ht.empty ();
13962 : :
13963 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13964 : : fold_check_failed (op0, tem);
13965 : :
13966 : : md5_init_ctx (&ctx);
13967 : : fold_checksum_tree (op1, &ctx, &ht);
13968 : : md5_finish_ctx (&ctx, checksum_after_op1);
13969 : :
13970 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13971 : : fold_check_failed (op1, tem);
13972 : : #endif
13973 : 559855376 : return tem;
13974 : : }
13975 : :
13976 : : /* Fold a ternary tree expression with code CODE of type TYPE with
13977 : : operands OP0, OP1, and OP2. Return a folded expression if
13978 : : successful. Otherwise, return a tree expression with code CODE of
13979 : : type TYPE with operands OP0, OP1, and OP2. */
13980 : :
13981 : : tree
13982 : 20847014 : fold_build3_loc (location_t loc, enum tree_code code, tree type,
13983 : : tree op0, tree op1, tree op2 MEM_STAT_DECL)
13984 : : {
13985 : 20847014 : tree tem;
13986 : : #ifdef ENABLE_FOLD_CHECKING
13987 : : unsigned char checksum_before_op0[16],
13988 : : checksum_before_op1[16],
13989 : : checksum_before_op2[16],
13990 : : checksum_after_op0[16],
13991 : : checksum_after_op1[16],
13992 : : checksum_after_op2[16];
13993 : : struct md5_ctx ctx;
13994 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13995 : :
13996 : : md5_init_ctx (&ctx);
13997 : : fold_checksum_tree (op0, &ctx, &ht);
13998 : : md5_finish_ctx (&ctx, checksum_before_op0);
13999 : : ht.empty ();
14000 : :
14001 : : md5_init_ctx (&ctx);
14002 : : fold_checksum_tree (op1, &ctx, &ht);
14003 : : md5_finish_ctx (&ctx, checksum_before_op1);
14004 : : ht.empty ();
14005 : :
14006 : : md5_init_ctx (&ctx);
14007 : : fold_checksum_tree (op2, &ctx, &ht);
14008 : : md5_finish_ctx (&ctx, checksum_before_op2);
14009 : : ht.empty ();
14010 : : #endif
14011 : :
14012 : 20847014 : gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14013 : 20847014 : tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14014 : 20847014 : if (!tem)
14015 : 18318210 : tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14016 : :
14017 : : #ifdef ENABLE_FOLD_CHECKING
14018 : : md5_init_ctx (&ctx);
14019 : : fold_checksum_tree (op0, &ctx, &ht);
14020 : : md5_finish_ctx (&ctx, checksum_after_op0);
14021 : : ht.empty ();
14022 : :
14023 : : if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14024 : : fold_check_failed (op0, tem);
14025 : :
14026 : : md5_init_ctx (&ctx);
14027 : : fold_checksum_tree (op1, &ctx, &ht);
14028 : : md5_finish_ctx (&ctx, checksum_after_op1);
14029 : : ht.empty ();
14030 : :
14031 : : if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14032 : : fold_check_failed (op1, tem);
14033 : :
14034 : : md5_init_ctx (&ctx);
14035 : : fold_checksum_tree (op2, &ctx, &ht);
14036 : : md5_finish_ctx (&ctx, checksum_after_op2);
14037 : :
14038 : : if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14039 : : fold_check_failed (op2, tem);
14040 : : #endif
14041 : 20847014 : return tem;
14042 : : }
14043 : :
14044 : : /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14045 : : arguments in ARGARRAY, and a null static chain.
14046 : : Return a folded expression if successful. Otherwise, return a CALL_EXPR
14047 : : of type TYPE from the given operands as constructed by build_call_array. */
14048 : :
14049 : : tree
14050 : 49035004 : fold_build_call_array_loc (location_t loc, tree type, tree fn,
14051 : : int nargs, tree *argarray)
14052 : : {
14053 : 49035004 : tree tem;
14054 : : #ifdef ENABLE_FOLD_CHECKING
14055 : : unsigned char checksum_before_fn[16],
14056 : : checksum_before_arglist[16],
14057 : : checksum_after_fn[16],
14058 : : checksum_after_arglist[16];
14059 : : struct md5_ctx ctx;
14060 : : hash_table<nofree_ptr_hash<const tree_node> > ht (32);
14061 : : int i;
14062 : :
14063 : : md5_init_ctx (&ctx);
14064 : : fold_checksum_tree (fn, &ctx, &ht);
14065 : : md5_finish_ctx (&ctx, checksum_before_fn);
14066 : : ht.empty ();
14067 : :
14068 : : md5_init_ctx (&ctx);
14069 : : for (i = 0; i < nargs; i++)
14070 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14071 : : md5_finish_ctx (&ctx, checksum_before_arglist);
14072 : : ht.empty ();
14073 : : #endif
14074 : :
14075 : 49035004 : tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14076 : 49035004 : if (!tem)
14077 : 47402067 : tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14078 : :
14079 : : #ifdef ENABLE_FOLD_CHECKING
14080 : : md5_init_ctx (&ctx);
14081 : : fold_checksum_tree (fn, &ctx, &ht);
14082 : : md5_finish_ctx (&ctx, checksum_after_fn);
14083 : : ht.empty ();
14084 : :
14085 : : if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14086 : : fold_check_failed (fn, tem);
14087 : :
14088 : : md5_init_ctx (&ctx);
14089 : : for (i = 0; i < nargs; i++)
14090 : : fold_checksum_tree (argarray[i], &ctx, &ht);
14091 : : md5_finish_ctx (&ctx, checksum_after_arglist);
14092 : :
14093 : : if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14094 : : fold_check_failed (NULL_TREE, tem);
14095 : : #endif
14096 : 49035004 : return tem;
14097 : : }
14098 : :
14099 : : /* Perform constant folding and related simplification of initializer
14100 : : expression EXPR. These behave identically to "fold_buildN" but ignore
14101 : : potential run-time traps and exceptions that fold must preserve. */
14102 : :
14103 : : #define START_FOLD_INIT \
14104 : : int saved_signaling_nans = flag_signaling_nans;\
14105 : : int saved_trapping_math = flag_trapping_math;\
14106 : : int saved_rounding_math = flag_rounding_math;\
14107 : : int saved_trapv = flag_trapv;\
14108 : : int saved_folding_initializer = folding_initializer;\
14109 : : flag_signaling_nans = 0;\
14110 : : flag_trapping_math = 0;\
14111 : : flag_rounding_math = 0;\
14112 : : flag_trapv = 0;\
14113 : : folding_initializer = 1;
14114 : :
14115 : : #define END_FOLD_INIT \
14116 : : flag_signaling_nans = saved_signaling_nans;\
14117 : : flag_trapping_math = saved_trapping_math;\
14118 : : flag_rounding_math = saved_rounding_math;\
14119 : : flag_trapv = saved_trapv;\
14120 : : folding_initializer = saved_folding_initializer;
14121 : :
14122 : : tree
14123 : 542486 : fold_init (tree expr)
14124 : : {
14125 : 542486 : tree result;
14126 : 542486 : START_FOLD_INIT;
14127 : :
14128 : 542486 : result = fold (expr);
14129 : :
14130 : 542486 : END_FOLD_INIT;
14131 : 542486 : return result;
14132 : : }
14133 : :
14134 : : tree
14135 : 2988287 : fold_build1_initializer_loc (location_t loc, enum tree_code code,
14136 : : tree type, tree op)
14137 : : {
14138 : 2988287 : tree result;
14139 : 2988287 : START_FOLD_INIT;
14140 : :
14141 : 2988287 : result = fold_build1_loc (loc, code, type, op);
14142 : :
14143 : 2988287 : END_FOLD_INIT;
14144 : 2988287 : return result;
14145 : : }
14146 : :
14147 : : tree
14148 : 50474 : fold_build2_initializer_loc (location_t loc, enum tree_code code,
14149 : : tree type, tree op0, tree op1)
14150 : : {
14151 : 50474 : tree result;
14152 : 50474 : START_FOLD_INIT;
14153 : :
14154 : 50474 : result = fold_build2_loc (loc, code, type, op0, op1);
14155 : :
14156 : 50474 : END_FOLD_INIT;
14157 : 50474 : return result;
14158 : : }
14159 : :
14160 : : tree
14161 : 3488 : fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14162 : : int nargs, tree *argarray)
14163 : : {
14164 : 3488 : tree result;
14165 : 3488 : START_FOLD_INIT;
14166 : :
14167 : 3488 : result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14168 : :
14169 : 3488 : END_FOLD_INIT;
14170 : 3488 : return result;
14171 : : }
14172 : :
14173 : : tree
14174 : 19229278 : fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14175 : : tree lhs, tree rhs)
14176 : : {
14177 : 19229278 : tree result;
14178 : 19229278 : START_FOLD_INIT;
14179 : :
14180 : 19229278 : result = fold_binary_loc (loc, code, type, lhs, rhs);
14181 : :
14182 : 19229278 : END_FOLD_INIT;
14183 : 19229278 : return result;
14184 : : }
14185 : :
14186 : : #undef START_FOLD_INIT
14187 : : #undef END_FOLD_INIT
14188 : :
14189 : : /* Determine if first argument is a multiple of second argument. Return
14190 : : false if it is not, or we cannot easily determined it to be.
14191 : :
14192 : : An example of the sort of thing we care about (at this point; this routine
14193 : : could surely be made more general, and expanded to do what the *_DIV_EXPR's
14194 : : fold cases do now) is discovering that
14195 : :
14196 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14197 : :
14198 : : is a multiple of
14199 : :
14200 : : SAVE_EXPR (J * 8)
14201 : :
14202 : : when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14203 : :
14204 : : This code also handles discovering that
14205 : :
14206 : : SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14207 : :
14208 : : is a multiple of 8 so we don't have to worry about dealing with a
14209 : : possible remainder.
14210 : :
14211 : : Note that we *look* inside a SAVE_EXPR only to determine how it was
14212 : : calculated; it is not safe for fold to do much of anything else with the
14213 : : internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14214 : : at run time. For example, the latter example above *cannot* be implemented
14215 : : as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14216 : : evaluation time of the original SAVE_EXPR is not necessarily the same at
14217 : : the time the new expression is evaluated. The only optimization of this
14218 : : sort that would be valid is changing
14219 : :
14220 : : SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14221 : :
14222 : : divided by 8 to
14223 : :
14224 : : SAVE_EXPR (I) * SAVE_EXPR (J)
14225 : :
14226 : : (where the same SAVE_EXPR (J) is used in the original and the
14227 : : transformed version).
14228 : :
14229 : : NOWRAP specifies whether all outer operations in TYPE should
14230 : : be considered not wrapping. Any type conversion within TOP acts
14231 : : as a barrier and we will fall back to NOWRAP being false.
14232 : : NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14233 : : as not wrapping even though they are generally using unsigned arithmetic. */
14234 : :
14235 : : bool
14236 : 1526686 : multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14237 : : {
14238 : 1526686 : gimple *stmt;
14239 : 1526686 : tree op1, op2;
14240 : :
14241 : 1526686 : if (operand_equal_p (top, bottom, 0))
14242 : : return true;
14243 : :
14244 : 1067583 : if (TREE_CODE (type) != INTEGER_TYPE)
14245 : : return false;
14246 : :
14247 : 1067578 : switch (TREE_CODE (top))
14248 : : {
14249 : 642 : case BIT_AND_EXPR:
14250 : : /* Bitwise and provides a power of two multiple. If the mask is
14251 : : a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14252 : 642 : if (!integer_pow2p (bottom))
14253 : : return false;
14254 : 642 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14255 : 642 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14256 : :
14257 : 360834 : case MULT_EXPR:
14258 : : /* If the multiplication can wrap we cannot recurse further unless
14259 : : the bottom is a power of two which is where wrapping does not
14260 : : matter. */
14261 : 360834 : if (!nowrap
14262 : 13940 : && !TYPE_OVERFLOW_UNDEFINED (type)
14263 : 365586 : && !integer_pow2p (bottom))
14264 : : return false;
14265 : 360414 : if (TREE_CODE (bottom) == INTEGER_CST)
14266 : : {
14267 : 358698 : op1 = TREE_OPERAND (top, 0);
14268 : 358698 : op2 = TREE_OPERAND (top, 1);
14269 : 358698 : if (TREE_CODE (op1) == INTEGER_CST)
14270 : 0 : std::swap (op1, op2);
14271 : 358698 : if (TREE_CODE (op2) == INTEGER_CST)
14272 : : {
14273 : 348790 : if (multiple_of_p (type, op2, bottom, nowrap))
14274 : : return true;
14275 : : /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14276 : 2991 : if (multiple_of_p (type, bottom, op2, nowrap))
14277 : : {
14278 : 1888 : widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14279 : 1888 : wi::to_widest (op2));
14280 : 1888 : if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14281 : : {
14282 : 1888 : op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14283 : 1888 : return multiple_of_p (type, op1, op2, nowrap);
14284 : : }
14285 : 1888 : }
14286 : 1103 : return multiple_of_p (type, op1, bottom, nowrap);
14287 : : }
14288 : : }
14289 : 11624 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14290 : 11624 : || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14291 : :
14292 : 284 : case LSHIFT_EXPR:
14293 : : /* Handle X << CST as X * (1 << CST) and only process the constant. */
14294 : 284 : if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14295 : : {
14296 : 284 : op1 = TREE_OPERAND (top, 1);
14297 : 284 : if (wi::to_widest (op1) < TYPE_PRECISION (type))
14298 : : {
14299 : 284 : wide_int mul_op
14300 : 284 : = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14301 : 568 : return multiple_of_p (type,
14302 : 568 : wide_int_to_tree (type, mul_op), bottom,
14303 : : nowrap);
14304 : 284 : }
14305 : : }
14306 : : return false;
14307 : :
14308 : 211721 : case MINUS_EXPR:
14309 : 211721 : case PLUS_EXPR:
14310 : : /* If the addition or subtraction can wrap we cannot recurse further
14311 : : unless bottom is a power of two which is where wrapping does not
14312 : : matter. */
14313 : 211721 : if (!nowrap
14314 : 170381 : && !TYPE_OVERFLOW_UNDEFINED (type)
14315 : 380673 : && !integer_pow2p (bottom))
14316 : : return false;
14317 : :
14318 : : /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14319 : : unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14320 : : but 0xfffffffd is not. */
14321 : 185715 : op1 = TREE_OPERAND (top, 1);
14322 : 185715 : if (TREE_CODE (top) == PLUS_EXPR
14323 : 180021 : && nowrap
14324 : 35733 : && TYPE_UNSIGNED (type)
14325 : 220893 : && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14326 : 30513 : op1 = fold_build1 (NEGATE_EXPR, type, op1);
14327 : :
14328 : : /* It is impossible to prove if op0 +- op1 is multiple of bottom
14329 : : precisely, so be conservative here checking if both op0 and op1
14330 : : are multiple of bottom. Note we check the second operand first
14331 : : since it's usually simpler. */
14332 : 185715 : return (multiple_of_p (type, op1, bottom, nowrap)
14333 : 185715 : && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14334 : :
14335 : 154227 : CASE_CONVERT:
14336 : : /* Can't handle conversions from non-integral or wider integral type. */
14337 : 154227 : if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14338 : 154227 : || (TYPE_PRECISION (type)
14339 : 53229 : < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14340 : : return false;
14341 : : /* NOWRAP only extends to operations in the outermost type so
14342 : : make sure to strip it off here. */
14343 : 52915 : return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14344 : 105830 : TREE_OPERAND (top, 0), bottom, false);
14345 : :
14346 : 12352 : case SAVE_EXPR:
14347 : 12352 : return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14348 : :
14349 : 0 : case COND_EXPR:
14350 : 0 : return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14351 : 0 : && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14352 : :
14353 : 121895 : case INTEGER_CST:
14354 : 121895 : if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14355 : 2728 : return false;
14356 : 119167 : return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14357 : : SIGNED);
14358 : :
14359 : 64097 : case SSA_NAME:
14360 : 64097 : if (TREE_CODE (bottom) == INTEGER_CST
14361 : 61135 : && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14362 : 125232 : && gimple_code (stmt) == GIMPLE_ASSIGN)
14363 : : {
14364 : 27303 : enum tree_code code = gimple_assign_rhs_code (stmt);
14365 : :
14366 : : /* Check for special cases to see if top is defined as multiple
14367 : : of bottom:
14368 : :
14369 : : top = (X & ~(bottom - 1) ; bottom is power of 2
14370 : :
14371 : : or
14372 : :
14373 : : Y = X % bottom
14374 : : top = X - Y. */
14375 : 27303 : if (code == BIT_AND_EXPR
14376 : 296 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14377 : 296 : && TREE_CODE (op2) == INTEGER_CST
14378 : 188 : && integer_pow2p (bottom)
14379 : 27491 : && wi::multiple_of_p (wi::to_widest (op2),
14380 : 188 : wi::to_widest (bottom), SIGNED))
14381 : 179 : return true;
14382 : :
14383 : 27124 : op1 = gimple_assign_rhs1 (stmt);
14384 : 27124 : if (code == MINUS_EXPR
14385 : 2644 : && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14386 : 2644 : && TREE_CODE (op2) == SSA_NAME
14387 : 2644 : && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14388 : 2644 : && gimple_code (stmt) == GIMPLE_ASSIGN
14389 : 2284 : && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14390 : 62 : && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14391 : 27186 : && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14392 : : return true;
14393 : : }
14394 : :
14395 : : /* fall through */
14396 : :
14397 : : default:
14398 : : if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14399 : : return multiple_p (wi::to_poly_widest (top),
14400 : : wi::to_poly_widest (bottom));
14401 : :
14402 : : return false;
14403 : : }
14404 : : }
14405 : :
14406 : : /* Return true if expression X cannot be (or contain) a NaN or infinity.
14407 : : This function returns true for integer expressions, and returns
14408 : : false if uncertain. */
14409 : :
14410 : : bool
14411 : 568904 : tree_expr_finite_p (const_tree x)
14412 : : {
14413 : 568908 : machine_mode mode = element_mode (x);
14414 : 568908 : if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14415 : : return true;
14416 : 568714 : switch (TREE_CODE (x))
14417 : : {
14418 : 592 : case REAL_CST:
14419 : 592 : return real_isfinite (TREE_REAL_CST_PTR (x));
14420 : 0 : case COMPLEX_CST:
14421 : 0 : return tree_expr_finite_p (TREE_REALPART (x))
14422 : 0 : && tree_expr_finite_p (TREE_IMAGPART (x));
14423 : : case FLOAT_EXPR:
14424 : : return true;
14425 : 4 : case ABS_EXPR:
14426 : 4 : case CONVERT_EXPR:
14427 : 4 : case NON_LVALUE_EXPR:
14428 : 4 : case NEGATE_EXPR:
14429 : 4 : case SAVE_EXPR:
14430 : 4 : return tree_expr_finite_p (TREE_OPERAND (x, 0));
14431 : 0 : case MIN_EXPR:
14432 : 0 : case MAX_EXPR:
14433 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 0))
14434 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 1));
14435 : 0 : case COND_EXPR:
14436 : 0 : return tree_expr_finite_p (TREE_OPERAND (x, 1))
14437 : 0 : && tree_expr_finite_p (TREE_OPERAND (x, 2));
14438 : 38 : case CALL_EXPR:
14439 : 38 : switch (get_call_combined_fn (x))
14440 : : {
14441 : 0 : CASE_CFN_FABS:
14442 : 0 : CASE_CFN_FABS_FN:
14443 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14444 : 0 : CASE_CFN_FMAX:
14445 : 0 : CASE_CFN_FMAX_FN:
14446 : 0 : CASE_CFN_FMIN:
14447 : 0 : CASE_CFN_FMIN_FN:
14448 : 0 : return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14449 : 0 : && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14450 : : default:
14451 : : return false;
14452 : : }
14453 : :
14454 : : default:
14455 : : return false;
14456 : : }
14457 : : }
14458 : :
14459 : : /* Return true if expression X evaluates to an infinity.
14460 : : This function returns false for integer expressions. */
14461 : :
14462 : : bool
14463 : 815226 : tree_expr_infinite_p (const_tree x)
14464 : : {
14465 : 815676 : if (!HONOR_INFINITIES (x))
14466 : : return false;
14467 : 815561 : switch (TREE_CODE (x))
14468 : : {
14469 : 0 : case REAL_CST:
14470 : 0 : return real_isinf (TREE_REAL_CST_PTR (x));
14471 : 450 : case ABS_EXPR:
14472 : 450 : case NEGATE_EXPR:
14473 : 450 : case NON_LVALUE_EXPR:
14474 : 450 : case SAVE_EXPR:
14475 : 450 : return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14476 : 0 : case COND_EXPR:
14477 : 0 : return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14478 : 0 : && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14479 : : default:
14480 : : return false;
14481 : : }
14482 : : }
14483 : :
14484 : : /* Return true if expression X could evaluate to an infinity.
14485 : : This function returns false for integer expressions, and returns
14486 : : true if uncertain. */
14487 : :
14488 : : bool
14489 : 372925 : tree_expr_maybe_infinite_p (const_tree x)
14490 : : {
14491 : 372933 : if (!HONOR_INFINITIES (x))
14492 : : return false;
14493 : 372606 : switch (TREE_CODE (x))
14494 : : {
14495 : 186 : case REAL_CST:
14496 : 186 : return real_isinf (TREE_REAL_CST_PTR (x));
14497 : : case FLOAT_EXPR:
14498 : : return false;
14499 : 8 : case ABS_EXPR:
14500 : 8 : case NEGATE_EXPR:
14501 : 8 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14502 : 1 : case COND_EXPR:
14503 : 1 : return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14504 : 1 : || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14505 : : default:
14506 : : return true;
14507 : : }
14508 : : }
14509 : :
14510 : : /* Return true if expression X evaluates to a signaling NaN.
14511 : : This function returns false for integer expressions. */
14512 : :
14513 : : bool
14514 : 395 : tree_expr_signaling_nan_p (const_tree x)
14515 : : {
14516 : 395 : if (!HONOR_SNANS (x))
14517 : : return false;
14518 : 124 : switch (TREE_CODE (x))
14519 : : {
14520 : 124 : case REAL_CST:
14521 : 124 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14522 : 0 : case NON_LVALUE_EXPR:
14523 : 0 : case SAVE_EXPR:
14524 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14525 : 0 : case COND_EXPR:
14526 : 0 : return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14527 : 0 : && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14528 : : default:
14529 : : return false;
14530 : : }
14531 : : }
14532 : :
14533 : : /* Return true if expression X could evaluate to a signaling NaN.
14534 : : This function returns false for integer expressions, and returns
14535 : : true if uncertain. */
14536 : :
14537 : : bool
14538 : 728026 : tree_expr_maybe_signaling_nan_p (const_tree x)
14539 : : {
14540 : 728026 : if (!HONOR_SNANS (x))
14541 : : return false;
14542 : 5032 : switch (TREE_CODE (x))
14543 : : {
14544 : 1456 : case REAL_CST:
14545 : 1456 : return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14546 : : case FLOAT_EXPR:
14547 : : return false;
14548 : 0 : case ABS_EXPR:
14549 : 0 : case CONVERT_EXPR:
14550 : 0 : case NEGATE_EXPR:
14551 : 0 : case NON_LVALUE_EXPR:
14552 : 0 : case SAVE_EXPR:
14553 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14554 : 0 : case MIN_EXPR:
14555 : 0 : case MAX_EXPR:
14556 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14557 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14558 : 0 : case COND_EXPR:
14559 : 0 : return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14560 : 0 : || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14561 : 0 : case CALL_EXPR:
14562 : 0 : switch (get_call_combined_fn (x))
14563 : : {
14564 : 0 : CASE_CFN_FABS:
14565 : 0 : CASE_CFN_FABS_FN:
14566 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14567 : 0 : CASE_CFN_FMAX:
14568 : 0 : CASE_CFN_FMAX_FN:
14569 : 0 : CASE_CFN_FMIN:
14570 : 0 : CASE_CFN_FMIN_FN:
14571 : 0 : return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14572 : 0 : || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14573 : : default:
14574 : : return true;
14575 : : }
14576 : : default:
14577 : : return true;
14578 : : }
14579 : : }
14580 : :
14581 : : /* Return true if expression X evaluates to a NaN.
14582 : : This function returns false for integer expressions. */
14583 : :
14584 : : bool
14585 : 4004953 : tree_expr_nan_p (const_tree x)
14586 : : {
14587 : 4385535 : if (!HONOR_NANS (x))
14588 : : return false;
14589 : 4385185 : switch (TREE_CODE (x))
14590 : : {
14591 : 3792 : case REAL_CST:
14592 : 3792 : return real_isnan (TREE_REAL_CST_PTR (x));
14593 : 380582 : case NON_LVALUE_EXPR:
14594 : 380582 : case SAVE_EXPR:
14595 : 380582 : return tree_expr_nan_p (TREE_OPERAND (x, 0));
14596 : 900 : case COND_EXPR:
14597 : 900 : return tree_expr_nan_p (TREE_OPERAND (x, 1))
14598 : 900 : && tree_expr_nan_p (TREE_OPERAND (x, 2));
14599 : : default:
14600 : : return false;
14601 : : }
14602 : : }
14603 : :
14604 : : /* Return true if expression X could evaluate to a NaN.
14605 : : This function returns false for integer expressions, and returns
14606 : : true if uncertain. */
14607 : :
14608 : : bool
14609 : 4867500 : tree_expr_maybe_nan_p (const_tree x)
14610 : : {
14611 : 6815665 : if (!HONOR_NANS (x))
14612 : : return false;
14613 : 6655681 : switch (TREE_CODE (x))
14614 : : {
14615 : 3282 : case REAL_CST:
14616 : 3282 : return real_isnan (TREE_REAL_CST_PTR (x));
14617 : : case FLOAT_EXPR:
14618 : : return false;
14619 : 13819 : case PLUS_EXPR:
14620 : 13819 : case MINUS_EXPR:
14621 : 13819 : case MULT_EXPR:
14622 : 13819 : return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14623 : 13819 : || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14624 : 1948165 : case ABS_EXPR:
14625 : 1948165 : case CONVERT_EXPR:
14626 : 1948165 : case NEGATE_EXPR:
14627 : 1948165 : case NON_LVALUE_EXPR:
14628 : 1948165 : case SAVE_EXPR:
14629 : 1948165 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14630 : 176 : case MIN_EXPR:
14631 : 176 : case MAX_EXPR:
14632 : 176 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14633 : 176 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14634 : 557 : case COND_EXPR:
14635 : 557 : return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14636 : 557 : || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14637 : 1064 : case CALL_EXPR:
14638 : 1064 : switch (get_call_combined_fn (x))
14639 : : {
14640 : 0 : CASE_CFN_FABS:
14641 : 0 : CASE_CFN_FABS_FN:
14642 : 0 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14643 : 108 : CASE_CFN_FMAX:
14644 : 108 : CASE_CFN_FMAX_FN:
14645 : 108 : CASE_CFN_FMIN:
14646 : 108 : CASE_CFN_FMIN_FN:
14647 : 108 : return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14648 : 108 : || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14649 : : default:
14650 : : return true;
14651 : : }
14652 : : default:
14653 : : return true;
14654 : : }
14655 : : }
14656 : :
14657 : : /* Return true if expression X could evaluate to -0.0.
14658 : : This function returns true if uncertain. */
14659 : :
14660 : : bool
14661 : 603670 : tree_expr_maybe_real_minus_zero_p (const_tree x)
14662 : : {
14663 : 603670 : if (!HONOR_SIGNED_ZEROS (x))
14664 : : return false;
14665 : 603670 : switch (TREE_CODE (x))
14666 : : {
14667 : 0 : case REAL_CST:
14668 : 0 : return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14669 : : case INTEGER_CST:
14670 : : case FLOAT_EXPR:
14671 : : case ABS_EXPR:
14672 : : return false;
14673 : 0 : case NON_LVALUE_EXPR:
14674 : 0 : case SAVE_EXPR:
14675 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14676 : 0 : case COND_EXPR:
14677 : 0 : return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14678 : 0 : || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14679 : 1 : case CALL_EXPR:
14680 : 1 : switch (get_call_combined_fn (x))
14681 : : {
14682 : : CASE_CFN_FABS:
14683 : : CASE_CFN_FABS_FN:
14684 : : return false;
14685 : : default:
14686 : : break;
14687 : : }
14688 : : default:
14689 : : break;
14690 : : }
14691 : : /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14692 : : * but currently those predicates require tree and not const_tree. */
14693 : : return true;
14694 : : }
14695 : :
14696 : : #define tree_expr_nonnegative_warnv_p(X, Y) \
14697 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14698 : :
14699 : : #define RECURSE(X) \
14700 : : ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14701 : :
14702 : : /* Return true if CODE or TYPE is known to be non-negative. */
14703 : :
14704 : : static bool
14705 : 38206354 : tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14706 : : {
14707 : 38206354 : if (!VECTOR_TYPE_P (type)
14708 : 38188457 : && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14709 : 76394068 : && truth_value_p (code))
14710 : : /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14711 : : have a signed:1 type (where the value is -1 and 0). */
14712 : : return true;
14713 : : return false;
14714 : : }
14715 : :
14716 : : /* Return true if (CODE OP0) is known to be non-negative. If the return
14717 : : value is based on the assumption that signed overflow is undefined,
14718 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14719 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14720 : :
14721 : : bool
14722 : 13583475 : tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14723 : : bool *strict_overflow_p, int depth)
14724 : : {
14725 : 13583475 : if (TYPE_UNSIGNED (type))
14726 : : return true;
14727 : :
14728 : 5407967 : switch (code)
14729 : : {
14730 : 346289 : case ABS_EXPR:
14731 : : /* We can't return 1 if flag_wrapv is set because
14732 : : ABS_EXPR<INT_MIN> = INT_MIN. */
14733 : 346289 : if (!ANY_INTEGRAL_TYPE_P (type))
14734 : : return true;
14735 : 43421 : if (TYPE_OVERFLOW_UNDEFINED (type))
14736 : : {
14737 : 42476 : *strict_overflow_p = true;
14738 : 42476 : return true;
14739 : : }
14740 : : break;
14741 : :
14742 : 226049 : case NON_LVALUE_EXPR:
14743 : 226049 : case FLOAT_EXPR:
14744 : 226049 : case FIX_TRUNC_EXPR:
14745 : 226049 : return RECURSE (op0);
14746 : :
14747 : 4736892 : CASE_CONVERT:
14748 : 4736892 : {
14749 : 4736892 : tree inner_type = TREE_TYPE (op0);
14750 : 4736892 : tree outer_type = type;
14751 : :
14752 : 4736892 : if (SCALAR_FLOAT_TYPE_P (outer_type))
14753 : : {
14754 : 298240 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14755 : 298240 : return RECURSE (op0);
14756 : 0 : if (INTEGRAL_TYPE_P (inner_type))
14757 : : {
14758 : 0 : if (TYPE_UNSIGNED (inner_type))
14759 : : return true;
14760 : 0 : return RECURSE (op0);
14761 : : }
14762 : : }
14763 : 4438652 : else if (INTEGRAL_TYPE_P (outer_type))
14764 : : {
14765 : 4438615 : if (SCALAR_FLOAT_TYPE_P (inner_type))
14766 : 0 : return RECURSE (op0);
14767 : 4438615 : if (INTEGRAL_TYPE_P (inner_type))
14768 : 4278650 : return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14769 : 4278650 : && TYPE_UNSIGNED (inner_type);
14770 : : }
14771 : : }
14772 : : break;
14773 : :
14774 : 98737 : default:
14775 : 98737 : return tree_simple_nonnegative_warnv_p (code, type);
14776 : : }
14777 : :
14778 : : /* We don't know sign of `t', so be conservative and return false. */
14779 : : return false;
14780 : : }
14781 : :
14782 : : /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14783 : : value is based on the assumption that signed overflow is undefined,
14784 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14785 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14786 : :
14787 : : bool
14788 : 39465647 : tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14789 : : tree op1, bool *strict_overflow_p,
14790 : : int depth)
14791 : : {
14792 : 39465647 : if (TYPE_UNSIGNED (type))
14793 : : return true;
14794 : :
14795 : 15346257 : switch (code)
14796 : : {
14797 : 5643834 : case POINTER_PLUS_EXPR:
14798 : 5643834 : case PLUS_EXPR:
14799 : 5643834 : if (FLOAT_TYPE_P (type))
14800 : 67989 : return RECURSE (op0) && RECURSE (op1);
14801 : :
14802 : : /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14803 : : both unsigned and at least 2 bits shorter than the result. */
14804 : 5575845 : if (TREE_CODE (type) == INTEGER_TYPE
14805 : 5569755 : && TREE_CODE (op0) == NOP_EXPR
14806 : 7750 : && TREE_CODE (op1) == NOP_EXPR)
14807 : : {
14808 : 205 : tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14809 : 205 : tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14810 : 205 : if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14811 : 308 : && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14812 : : {
14813 : 95 : unsigned int prec = MAX (TYPE_PRECISION (inner1),
14814 : 95 : TYPE_PRECISION (inner2)) + 1;
14815 : 95 : return prec < TYPE_PRECISION (type);
14816 : : }
14817 : : }
14818 : : break;
14819 : :
14820 : 1350085 : case MULT_EXPR:
14821 : 1350085 : if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14822 : : {
14823 : : /* x * x is always non-negative for floating point x
14824 : : or without overflow. */
14825 : 1291572 : if (operand_equal_p (op0, op1, 0)
14826 : 1291572 : || (RECURSE (op0) && RECURSE (op1)))
14827 : : {
14828 : 1234 : if (ANY_INTEGRAL_TYPE_P (type)
14829 : 14361 : && TYPE_OVERFLOW_UNDEFINED (type))
14830 : 13127 : *strict_overflow_p = true;
14831 : 14340 : return true;
14832 : : }
14833 : : }
14834 : :
14835 : : /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14836 : : both unsigned and their total bits is shorter than the result. */
14837 : 1335745 : if (TREE_CODE (type) == INTEGER_TYPE
14838 : 1256759 : && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14839 : 151 : && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14840 : : {
14841 : 115 : tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14842 : 115 : ? TREE_TYPE (TREE_OPERAND (op0, 0))
14843 : 115 : : TREE_TYPE (op0);
14844 : 115 : tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14845 : 115 : ? TREE_TYPE (TREE_OPERAND (op1, 0))
14846 : 115 : : TREE_TYPE (op1);
14847 : :
14848 : 115 : bool unsigned0 = TYPE_UNSIGNED (inner0);
14849 : 115 : bool unsigned1 = TYPE_UNSIGNED (inner1);
14850 : :
14851 : 115 : if (TREE_CODE (op0) == INTEGER_CST)
14852 : 0 : unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14853 : :
14854 : 115 : if (TREE_CODE (op1) == INTEGER_CST)
14855 : 68 : unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14856 : :
14857 : 115 : if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14858 : 7 : && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14859 : : {
14860 : 0 : unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14861 : 0 : ? tree_int_cst_min_precision (op0, UNSIGNED)
14862 : 0 : : TYPE_PRECISION (inner0);
14863 : :
14864 : 0 : unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14865 : 0 : ? tree_int_cst_min_precision (op1, UNSIGNED)
14866 : 0 : : TYPE_PRECISION (inner1);
14867 : :
14868 : 0 : return precision0 + precision1 < TYPE_PRECISION (type);
14869 : : }
14870 : : }
14871 : : return false;
14872 : :
14873 : 95728 : case BIT_AND_EXPR:
14874 : 95728 : return RECURSE (op0) || RECURSE (op1);
14875 : :
14876 : 74135 : case MAX_EXPR:
14877 : : /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14878 : : things. */
14879 : 74135 : if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14880 : 76 : return RECURSE (op0) && RECURSE (op1);
14881 : 74059 : return RECURSE (op0) || RECURSE (op1);
14882 : :
14883 : 760300 : case BIT_IOR_EXPR:
14884 : 760300 : case BIT_XOR_EXPR:
14885 : 760300 : case MIN_EXPR:
14886 : 760300 : case RDIV_EXPR:
14887 : 760300 : case TRUNC_DIV_EXPR:
14888 : 760300 : case CEIL_DIV_EXPR:
14889 : 760300 : case FLOOR_DIV_EXPR:
14890 : 760300 : case ROUND_DIV_EXPR:
14891 : 760300 : return RECURSE (op0) && RECURSE (op1);
14892 : :
14893 : 101727 : case TRUNC_MOD_EXPR:
14894 : 101727 : return RECURSE (op0);
14895 : :
14896 : 254 : case FLOOR_MOD_EXPR:
14897 : 254 : return RECURSE (op1);
14898 : :
14899 : 7320194 : case CEIL_MOD_EXPR:
14900 : 7320194 : case ROUND_MOD_EXPR:
14901 : 7320194 : default:
14902 : 7320194 : return tree_simple_nonnegative_warnv_p (code, type);
14903 : : }
14904 : :
14905 : : /* We don't know sign of `t', so be conservative and return false. */
14906 : : return false;
14907 : : }
14908 : :
14909 : : /* Return true if T is known to be non-negative. If the return
14910 : : value is based on the assumption that signed overflow is undefined,
14911 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14912 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14913 : :
14914 : : bool
14915 : 47524344 : tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14916 : : {
14917 : 47524344 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
14918 : : return true;
14919 : :
14920 : 32004115 : switch (TREE_CODE (t))
14921 : : {
14922 : 3550260 : case INTEGER_CST:
14923 : 3550260 : return tree_int_cst_sgn (t) >= 0;
14924 : :
14925 : 979323 : case REAL_CST:
14926 : 979323 : return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14927 : :
14928 : 0 : case FIXED_CST:
14929 : 0 : return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14930 : :
14931 : 490 : case COND_EXPR:
14932 : 490 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14933 : :
14934 : 17532889 : case SSA_NAME:
14935 : : /* Limit the depth of recursion to avoid quadratic behavior.
14936 : : This is expected to catch almost all occurrences in practice.
14937 : : If this code misses important cases that unbounded recursion
14938 : : would not, passes that need this information could be revised
14939 : : to provide it through dataflow propagation. */
14940 : 17532889 : return (!name_registered_for_update_p (t)
14941 : 17532888 : && depth < param_max_ssa_name_query_depth
14942 : 33686780 : && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14943 : : strict_overflow_p, depth));
14944 : :
14945 : 9941153 : default:
14946 : 9941153 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14947 : : }
14948 : : }
14949 : :
14950 : : /* Return true if T is known to be non-negative. If the return
14951 : : value is based on the assumption that signed overflow is undefined,
14952 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
14953 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14954 : :
14955 : : bool
14956 : 21008803 : tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14957 : : bool *strict_overflow_p, int depth)
14958 : : {
14959 : 21008803 : switch (fn)
14960 : : {
14961 : : CASE_CFN_ACOS:
14962 : : CASE_CFN_ACOS_FN:
14963 : : CASE_CFN_ACOSH:
14964 : : CASE_CFN_ACOSH_FN:
14965 : : CASE_CFN_CABS:
14966 : : CASE_CFN_CABS_FN:
14967 : : CASE_CFN_COSH:
14968 : : CASE_CFN_COSH_FN:
14969 : : CASE_CFN_ERFC:
14970 : : CASE_CFN_ERFC_FN:
14971 : : CASE_CFN_EXP:
14972 : : CASE_CFN_EXP_FN:
14973 : : CASE_CFN_EXP10:
14974 : : CASE_CFN_EXP2:
14975 : : CASE_CFN_EXP2_FN:
14976 : : CASE_CFN_FABS:
14977 : : CASE_CFN_FABS_FN:
14978 : : CASE_CFN_FDIM:
14979 : : CASE_CFN_FDIM_FN:
14980 : : CASE_CFN_HYPOT:
14981 : : CASE_CFN_HYPOT_FN:
14982 : : CASE_CFN_POW10:
14983 : : CASE_CFN_FFS:
14984 : : CASE_CFN_PARITY:
14985 : : CASE_CFN_POPCOUNT:
14986 : : CASE_CFN_CLRSB:
14987 : : case CFN_BUILT_IN_BSWAP16:
14988 : : case CFN_BUILT_IN_BSWAP32:
14989 : : case CFN_BUILT_IN_BSWAP64:
14990 : : case CFN_BUILT_IN_BSWAP128:
14991 : : /* Always true. */
14992 : : return true;
14993 : :
14994 : 1021 : CASE_CFN_CLZ:
14995 : 1021 : CASE_CFN_CTZ:
14996 : 1021 : if (arg1)
14997 : 0 : return RECURSE (arg1);
14998 : : return true;
14999 : :
15000 : 1006 : CASE_CFN_SQRT:
15001 : 1006 : CASE_CFN_SQRT_FN:
15002 : : /* sqrt(-0.0) is -0.0. */
15003 : 1006 : if (!HONOR_SIGNED_ZEROS (type))
15004 : : return true;
15005 : 970 : return RECURSE (arg0);
15006 : :
15007 : 119694 : CASE_CFN_ASINH:
15008 : 119694 : CASE_CFN_ASINH_FN:
15009 : 119694 : CASE_CFN_ATAN:
15010 : 119694 : CASE_CFN_ATAN_FN:
15011 : 119694 : CASE_CFN_ATANH:
15012 : 119694 : CASE_CFN_ATANH_FN:
15013 : 119694 : CASE_CFN_CBRT:
15014 : 119694 : CASE_CFN_CBRT_FN:
15015 : 119694 : CASE_CFN_CEIL:
15016 : 119694 : CASE_CFN_CEIL_FN:
15017 : 119694 : CASE_CFN_ERF:
15018 : 119694 : CASE_CFN_ERF_FN:
15019 : 119694 : CASE_CFN_EXPM1:
15020 : 119694 : CASE_CFN_EXPM1_FN:
15021 : 119694 : CASE_CFN_FLOOR:
15022 : 119694 : CASE_CFN_FLOOR_FN:
15023 : 119694 : CASE_CFN_FMOD:
15024 : 119694 : CASE_CFN_FMOD_FN:
15025 : 119694 : CASE_CFN_FREXP:
15026 : 119694 : CASE_CFN_FREXP_FN:
15027 : 119694 : CASE_CFN_ICEIL:
15028 : 119694 : CASE_CFN_IFLOOR:
15029 : 119694 : CASE_CFN_IRINT:
15030 : 119694 : CASE_CFN_IROUND:
15031 : 119694 : CASE_CFN_LCEIL:
15032 : 119694 : CASE_CFN_LDEXP:
15033 : 119694 : CASE_CFN_LFLOOR:
15034 : 119694 : CASE_CFN_LLCEIL:
15035 : 119694 : CASE_CFN_LLFLOOR:
15036 : 119694 : CASE_CFN_LLRINT:
15037 : 119694 : CASE_CFN_LLRINT_FN:
15038 : 119694 : CASE_CFN_LLROUND:
15039 : 119694 : CASE_CFN_LLROUND_FN:
15040 : 119694 : CASE_CFN_LRINT:
15041 : 119694 : CASE_CFN_LRINT_FN:
15042 : 119694 : CASE_CFN_LROUND:
15043 : 119694 : CASE_CFN_LROUND_FN:
15044 : 119694 : CASE_CFN_MODF:
15045 : 119694 : CASE_CFN_MODF_FN:
15046 : 119694 : CASE_CFN_NEARBYINT:
15047 : 119694 : CASE_CFN_NEARBYINT_FN:
15048 : 119694 : CASE_CFN_RINT:
15049 : 119694 : CASE_CFN_RINT_FN:
15050 : 119694 : CASE_CFN_ROUND:
15051 : 119694 : CASE_CFN_ROUND_FN:
15052 : 119694 : CASE_CFN_ROUNDEVEN:
15053 : 119694 : CASE_CFN_ROUNDEVEN_FN:
15054 : 119694 : CASE_CFN_SCALB:
15055 : 119694 : CASE_CFN_SCALBLN:
15056 : 119694 : CASE_CFN_SCALBLN_FN:
15057 : 119694 : CASE_CFN_SCALBN:
15058 : 119694 : CASE_CFN_SCALBN_FN:
15059 : 119694 : CASE_CFN_SIGNBIT:
15060 : 119694 : CASE_CFN_SIGNIFICAND:
15061 : 119694 : CASE_CFN_SINH:
15062 : 119694 : CASE_CFN_SINH_FN:
15063 : 119694 : CASE_CFN_TANH:
15064 : 119694 : CASE_CFN_TANH_FN:
15065 : 119694 : CASE_CFN_TRUNC:
15066 : 119694 : CASE_CFN_TRUNC_FN:
15067 : : /* True if the 1st argument is nonnegative. */
15068 : 119694 : return RECURSE (arg0);
15069 : :
15070 : 1335 : CASE_CFN_FMAX:
15071 : 1335 : CASE_CFN_FMAX_FN:
15072 : : /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
15073 : : things. In the presence of sNaNs, we're only guaranteed to be
15074 : : non-negative if both operands are non-negative. In the presence
15075 : : of qNaNs, we're non-negative if either operand is non-negative
15076 : : and can't be a qNaN, or if both operands are non-negative. */
15077 : 1335 : if (tree_expr_maybe_signaling_nan_p (arg0)
15078 : 1335 : || tree_expr_maybe_signaling_nan_p (arg1))
15079 : 136 : return RECURSE (arg0) && RECURSE (arg1);
15080 : 1199 : return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
15081 : 332 : || RECURSE (arg1))
15082 : 867 : : (RECURSE (arg1)
15083 : 867 : && !tree_expr_maybe_nan_p (arg1));
15084 : :
15085 : 946 : CASE_CFN_FMIN:
15086 : 946 : CASE_CFN_FMIN_FN:
15087 : : /* True if the 1st AND 2nd arguments are nonnegative. */
15088 : 946 : return RECURSE (arg0) && RECURSE (arg1);
15089 : :
15090 : 827 : CASE_CFN_COPYSIGN:
15091 : 827 : CASE_CFN_COPYSIGN_FN:
15092 : : /* True if the 2nd argument is nonnegative. */
15093 : 827 : return RECURSE (arg1);
15094 : :
15095 : 2336 : CASE_CFN_POWI:
15096 : : /* True if the 1st argument is nonnegative or the second
15097 : : argument is an even integer. */
15098 : 2336 : if (TREE_CODE (arg1) == INTEGER_CST
15099 : 2336 : && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15100 : : return true;
15101 : 2255 : return RECURSE (arg0);
15102 : :
15103 : 4893 : CASE_CFN_POW:
15104 : 4893 : CASE_CFN_POW_FN:
15105 : : /* True if the 1st argument is nonnegative or the second
15106 : : argument is an even integer valued real. */
15107 : 4893 : if (TREE_CODE (arg1) == REAL_CST)
15108 : : {
15109 : 2146 : REAL_VALUE_TYPE c;
15110 : 2146 : HOST_WIDE_INT n;
15111 : :
15112 : 2146 : c = TREE_REAL_CST (arg1);
15113 : 2146 : n = real_to_integer (&c);
15114 : 2146 : if ((n & 1) == 0)
15115 : : {
15116 : 1513 : REAL_VALUE_TYPE cint;
15117 : 1513 : real_from_integer (&cint, VOIDmode, n, SIGNED);
15118 : 1513 : if (real_identical (&c, &cint))
15119 : 502 : return true;
15120 : : }
15121 : : }
15122 : 4391 : return RECURSE (arg0);
15123 : :
15124 : 20844604 : default:
15125 : 20844604 : break;
15126 : : }
15127 : 20844604 : return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
15128 : : }
15129 : :
15130 : : /* Return true if T is known to be non-negative. If the return
15131 : : value is based on the assumption that signed overflow is undefined,
15132 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15133 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15134 : :
15135 : : static bool
15136 : 879167 : tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15137 : : {
15138 : 879167 : enum tree_code code = TREE_CODE (t);
15139 : 879167 : if (TYPE_UNSIGNED (TREE_TYPE (t)))
15140 : : return true;
15141 : :
15142 : 704354 : switch (code)
15143 : : {
15144 : 203 : case TARGET_EXPR:
15145 : 203 : {
15146 : 203 : tree temp = TARGET_EXPR_SLOT (t);
15147 : 203 : t = TARGET_EXPR_INITIAL (t);
15148 : :
15149 : : /* If the initializer is non-void, then it's a normal expression
15150 : : that will be assigned to the slot. */
15151 : 203 : if (!VOID_TYPE_P (TREE_TYPE (t)))
15152 : 0 : return RECURSE (t);
15153 : :
15154 : : /* Otherwise, the initializer sets the slot in some way. One common
15155 : : way is an assignment statement at the end of the initializer. */
15156 : 405 : while (1)
15157 : : {
15158 : 405 : if (TREE_CODE (t) == BIND_EXPR)
15159 : 202 : t = expr_last (BIND_EXPR_BODY (t));
15160 : 203 : else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15161 : 203 : || TREE_CODE (t) == TRY_CATCH_EXPR)
15162 : 0 : t = expr_last (TREE_OPERAND (t, 0));
15163 : 203 : else if (TREE_CODE (t) == STATEMENT_LIST)
15164 : 0 : t = expr_last (t);
15165 : : else
15166 : : break;
15167 : : }
15168 : 203 : if (TREE_CODE (t) == MODIFY_EXPR
15169 : 203 : && TREE_OPERAND (t, 0) == temp)
15170 : 202 : return RECURSE (TREE_OPERAND (t, 1));
15171 : :
15172 : : return false;
15173 : : }
15174 : :
15175 : 318649 : case CALL_EXPR:
15176 : 318649 : {
15177 : 318649 : tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15178 : 318649 : tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15179 : :
15180 : 318649 : return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15181 : : get_call_combined_fn (t),
15182 : : arg0,
15183 : : arg1,
15184 : 318649 : strict_overflow_p, depth);
15185 : : }
15186 : 767 : case COMPOUND_EXPR:
15187 : 767 : case MODIFY_EXPR:
15188 : 767 : return RECURSE (TREE_OPERAND (t, 1));
15189 : :
15190 : 9 : case BIND_EXPR:
15191 : 9 : return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15192 : :
15193 : 383060 : case SAVE_EXPR:
15194 : 383060 : return RECURSE (TREE_OPERAND (t, 0));
15195 : :
15196 : 1666 : default:
15197 : 1666 : return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15198 : : }
15199 : : }
15200 : :
15201 : : #undef RECURSE
15202 : : #undef tree_expr_nonnegative_warnv_p
15203 : :
15204 : : /* Return true if T is known to be non-negative. If the return
15205 : : value is based on the assumption that signed overflow is undefined,
15206 : : set *STRICT_OVERFLOW_P to true; otherwise, don't change
15207 : : *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15208 : :
15209 : : bool
15210 : 24676651 : tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15211 : : {
15212 : 24676651 : enum tree_code code;
15213 : 24676651 : if (t == error_mark_node)
15214 : : return false;
15215 : :
15216 : 24676651 : code = TREE_CODE (t);
15217 : 24676651 : switch (TREE_CODE_CLASS (code))
15218 : : {
15219 : 1030718 : case tcc_binary:
15220 : 1030718 : case tcc_comparison:
15221 : 1030718 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15222 : 1030718 : TREE_TYPE (t),
15223 : 1030718 : TREE_OPERAND (t, 0),
15224 : 1030718 : TREE_OPERAND (t, 1),
15225 : 1030718 : strict_overflow_p, depth);
15226 : :
15227 : 1589018 : case tcc_unary:
15228 : 1589018 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15229 : 1589018 : TREE_TYPE (t),
15230 : 1589018 : TREE_OPERAND (t, 0),
15231 : 1589018 : strict_overflow_p, depth);
15232 : :
15233 : 11489285 : case tcc_constant:
15234 : 11489285 : case tcc_declaration:
15235 : 11489285 : case tcc_reference:
15236 : 11489285 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15237 : :
15238 : 10567630 : default:
15239 : 10567630 : break;
15240 : : }
15241 : :
15242 : 10567630 : switch (code)
15243 : : {
15244 : 7 : case TRUTH_AND_EXPR:
15245 : 7 : case TRUTH_OR_EXPR:
15246 : 7 : case TRUTH_XOR_EXPR:
15247 : 7 : return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15248 : 7 : TREE_TYPE (t),
15249 : 7 : TREE_OPERAND (t, 0),
15250 : 7 : TREE_OPERAND (t, 1),
15251 : 7 : strict_overflow_p, depth);
15252 : 72 : case TRUTH_NOT_EXPR:
15253 : 72 : return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15254 : 72 : TREE_TYPE (t),
15255 : 72 : TREE_OPERAND (t, 0),
15256 : 72 : strict_overflow_p, depth);
15257 : :
15258 : 9688384 : case COND_EXPR:
15259 : 9688384 : case CONSTRUCTOR:
15260 : 9688384 : case OBJ_TYPE_REF:
15261 : 9688384 : case ADDR_EXPR:
15262 : 9688384 : case WITH_SIZE_EXPR:
15263 : 9688384 : case SSA_NAME:
15264 : 9688384 : return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15265 : :
15266 : 879167 : default:
15267 : 879167 : return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15268 : : }
15269 : : }
15270 : :
15271 : : /* Return true if `t' is known to be non-negative. Handle warnings
15272 : : about undefined signed overflow. */
15273 : :
15274 : : bool
15275 : 17047428 : tree_expr_nonnegative_p (tree t)
15276 : : {
15277 : 17047428 : bool ret, strict_overflow_p;
15278 : :
15279 : 17047428 : strict_overflow_p = false;
15280 : 17047428 : ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15281 : 17047428 : if (strict_overflow_p)
15282 : 43092 : fold_overflow_warning (("assuming signed overflow does not occur when "
15283 : : "determining that expression is always "
15284 : : "non-negative"),
15285 : : WARN_STRICT_OVERFLOW_MISC);
15286 : 17047428 : return ret;
15287 : : }
15288 : :
15289 : :
15290 : : /* Return true when (CODE OP0) is an address and is known to be nonzero.
15291 : : For floating point we further ensure that T is not denormal.
15292 : : Similar logic is present in nonzero_address in rtlanal.h.
15293 : :
15294 : : If the return value is based on the assumption that signed overflow
15295 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15296 : : change *STRICT_OVERFLOW_P. */
15297 : :
15298 : : bool
15299 : 1378582 : tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15300 : : bool *strict_overflow_p)
15301 : : {
15302 : 1378582 : switch (code)
15303 : : {
15304 : 1 : case ABS_EXPR:
15305 : 1 : return tree_expr_nonzero_warnv_p (op0,
15306 : 1 : strict_overflow_p);
15307 : :
15308 : 741788 : case NOP_EXPR:
15309 : 741788 : {
15310 : 741788 : tree inner_type = TREE_TYPE (op0);
15311 : 741788 : tree outer_type = type;
15312 : :
15313 : 741788 : return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15314 : 741788 : && tree_expr_nonzero_warnv_p (op0,
15315 : : strict_overflow_p));
15316 : : }
15317 : 28109 : break;
15318 : :
15319 : 28109 : case NON_LVALUE_EXPR:
15320 : 28109 : return tree_expr_nonzero_warnv_p (op0,
15321 : 28109 : strict_overflow_p);
15322 : :
15323 : : default:
15324 : : break;
15325 : : }
15326 : :
15327 : : return false;
15328 : : }
15329 : :
15330 : : /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15331 : : For floating point we further ensure that T is not denormal.
15332 : : Similar logic is present in nonzero_address in rtlanal.h.
15333 : :
15334 : : If the return value is based on the assumption that signed overflow
15335 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15336 : : change *STRICT_OVERFLOW_P. */
15337 : :
15338 : : bool
15339 : 2933690 : tree_binary_nonzero_warnv_p (enum tree_code code,
15340 : : tree type,
15341 : : tree op0,
15342 : : tree op1, bool *strict_overflow_p)
15343 : : {
15344 : 2933690 : bool sub_strict_overflow_p;
15345 : 2933690 : switch (code)
15346 : : {
15347 : 447870 : case POINTER_PLUS_EXPR:
15348 : 447870 : case PLUS_EXPR:
15349 : 447870 : if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15350 : : {
15351 : : /* With the presence of negative values it is hard
15352 : : to say something. */
15353 : 101110 : sub_strict_overflow_p = false;
15354 : 101110 : if (!tree_expr_nonnegative_warnv_p (op0,
15355 : : &sub_strict_overflow_p)
15356 : 101110 : || !tree_expr_nonnegative_warnv_p (op1,
15357 : : &sub_strict_overflow_p))
15358 : 98774 : return false;
15359 : : /* One of operands must be positive and the other non-negative. */
15360 : : /* We don't set *STRICT_OVERFLOW_P here: even if this value
15361 : : overflows, on a twos-complement machine the sum of two
15362 : : nonnegative numbers can never be zero. */
15363 : 2336 : return (tree_expr_nonzero_warnv_p (op0,
15364 : : strict_overflow_p)
15365 : 2336 : || tree_expr_nonzero_warnv_p (op1,
15366 : : strict_overflow_p));
15367 : : }
15368 : : break;
15369 : :
15370 : 19938 : case MULT_EXPR:
15371 : 19938 : if (TYPE_OVERFLOW_UNDEFINED (type))
15372 : : {
15373 : 443 : if (tree_expr_nonzero_warnv_p (op0,
15374 : : strict_overflow_p)
15375 : 443 : && tree_expr_nonzero_warnv_p (op1,
15376 : : strict_overflow_p))
15377 : : {
15378 : 0 : *strict_overflow_p = true;
15379 : 0 : return true;
15380 : : }
15381 : : }
15382 : : break;
15383 : :
15384 : 13503 : case MIN_EXPR:
15385 : 13503 : sub_strict_overflow_p = false;
15386 : 13503 : if (tree_expr_nonzero_warnv_p (op0,
15387 : : &sub_strict_overflow_p)
15388 : 13503 : && tree_expr_nonzero_warnv_p (op1,
15389 : : &sub_strict_overflow_p))
15390 : : {
15391 : 0 : if (sub_strict_overflow_p)
15392 : 0 : *strict_overflow_p = true;
15393 : : }
15394 : : break;
15395 : :
15396 : 44 : case MAX_EXPR:
15397 : 44 : sub_strict_overflow_p = false;
15398 : 44 : if (tree_expr_nonzero_warnv_p (op0,
15399 : : &sub_strict_overflow_p))
15400 : : {
15401 : 0 : if (sub_strict_overflow_p)
15402 : 0 : *strict_overflow_p = true;
15403 : :
15404 : : /* When both operands are nonzero, then MAX must be too. */
15405 : 0 : if (tree_expr_nonzero_warnv_p (op1,
15406 : : strict_overflow_p))
15407 : : return true;
15408 : :
15409 : : /* MAX where operand 0 is positive is positive. */
15410 : 0 : return tree_expr_nonnegative_warnv_p (op0,
15411 : 0 : strict_overflow_p);
15412 : : }
15413 : : /* MAX where operand 1 is positive is positive. */
15414 : 44 : else if (tree_expr_nonzero_warnv_p (op1,
15415 : : &sub_strict_overflow_p)
15416 : 44 : && tree_expr_nonnegative_warnv_p (op1,
15417 : : &sub_strict_overflow_p))
15418 : : {
15419 : 0 : if (sub_strict_overflow_p)
15420 : 0 : *strict_overflow_p = true;
15421 : 0 : return true;
15422 : : }
15423 : : break;
15424 : :
15425 : 277142 : case BIT_IOR_EXPR:
15426 : 277142 : return (tree_expr_nonzero_warnv_p (op1,
15427 : : strict_overflow_p)
15428 : 277142 : || tree_expr_nonzero_warnv_p (op0,
15429 : : strict_overflow_p));
15430 : :
15431 : : default:
15432 : : break;
15433 : : }
15434 : :
15435 : : return false;
15436 : : }
15437 : :
15438 : : /* Return true when T is an address and is known to be nonzero.
15439 : : For floating point we further ensure that T is not denormal.
15440 : : Similar logic is present in nonzero_address in rtlanal.h.
15441 : :
15442 : : If the return value is based on the assumption that signed overflow
15443 : : is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15444 : : change *STRICT_OVERFLOW_P. */
15445 : :
15446 : : bool
15447 : 146467486 : tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15448 : : {
15449 : 146467486 : bool sub_strict_overflow_p;
15450 : 146467486 : switch (TREE_CODE (t))
15451 : : {
15452 : 1131324 : case INTEGER_CST:
15453 : 1131324 : return !integer_zerop (t);
15454 : :
15455 : 10916254 : case ADDR_EXPR:
15456 : 10916254 : {
15457 : 10916254 : tree base = TREE_OPERAND (t, 0);
15458 : :
15459 : 10916254 : if (!DECL_P (base))
15460 : 5668517 : base = get_base_address (base);
15461 : :
15462 : 10916254 : if (base && TREE_CODE (base) == TARGET_EXPR)
15463 : 714 : base = TARGET_EXPR_SLOT (base);
15464 : :
15465 : 714 : if (!base)
15466 : 0 : return false;
15467 : :
15468 : : /* For objects in symbol table check if we know they are non-zero.
15469 : : Don't do anything for variables and functions before symtab is built;
15470 : : it is quite possible that they will be declared weak later. */
15471 : 10916254 : int nonzero_addr = maybe_nonzero_address (base);
15472 : 10916254 : if (nonzero_addr >= 0)
15473 : 8538442 : return nonzero_addr;
15474 : :
15475 : : /* Constants are never weak. */
15476 : 2377812 : if (CONSTANT_CLASS_P (base))
15477 : : return true;
15478 : :
15479 : : return false;
15480 : : }
15481 : :
15482 : 30675 : case COND_EXPR:
15483 : 30675 : sub_strict_overflow_p = false;
15484 : 30675 : if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15485 : : &sub_strict_overflow_p)
15486 : 30675 : && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15487 : : &sub_strict_overflow_p))
15488 : : {
15489 : 1225 : if (sub_strict_overflow_p)
15490 : 0 : *strict_overflow_p = true;
15491 : 1225 : return true;
15492 : : }
15493 : : break;
15494 : :
15495 : 123783656 : case SSA_NAME:
15496 : 123783656 : if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15497 : : break;
15498 : 96755077 : return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15499 : :
15500 : : default:
15501 : : break;
15502 : : }
15503 : : return false;
15504 : : }
15505 : :
15506 : : #define integer_valued_real_p(X) \
15507 : : _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15508 : :
15509 : : #define RECURSE(X) \
15510 : : ((integer_valued_real_p) (X, depth + 1))
15511 : :
15512 : : /* Return true if the floating point result of (CODE OP0) has an
15513 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15514 : : integer values. Return false for signaling NaN.
15515 : :
15516 : : DEPTH is the current nesting depth of the query. */
15517 : :
15518 : : bool
15519 : 15030 : integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15520 : : {
15521 : 15030 : switch (code)
15522 : : {
15523 : : case FLOAT_EXPR:
15524 : : return true;
15525 : :
15526 : 1413 : case ABS_EXPR:
15527 : 1413 : return RECURSE (op0);
15528 : :
15529 : 9926 : CASE_CONVERT:
15530 : 9926 : {
15531 : 9926 : tree type = TREE_TYPE (op0);
15532 : 9926 : if (TREE_CODE (type) == INTEGER_TYPE)
15533 : : return true;
15534 : 9926 : if (SCALAR_FLOAT_TYPE_P (type))
15535 : 9926 : return RECURSE (op0);
15536 : : break;
15537 : : }
15538 : :
15539 : : default:
15540 : : break;
15541 : : }
15542 : : return false;
15543 : : }
15544 : :
15545 : : /* Return true if the floating point result of (CODE OP0 OP1) has an
15546 : : integer value. We also allow +Inf, -Inf and NaN to be considered
15547 : : integer values. Return false for signaling NaN.
15548 : :
15549 : : DEPTH is the current nesting depth of the query. */
15550 : :
15551 : : bool
15552 : 13641 : integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15553 : : {
15554 : 13641 : switch (code)
15555 : : {
15556 : 7159 : case PLUS_EXPR:
15557 : 7159 : case MINUS_EXPR:
15558 : 7159 : case MULT_EXPR:
15559 : 7159 : case MIN_EXPR:
15560 : 7159 : case MAX_EXPR:
15561 : 7159 : return RECURSE (op0) && RECURSE (op1);
15562 : :
15563 : : default:
15564 : : break;
15565 : : }
15566 : : return false;
15567 : : }
15568 : :
15569 : : /* Return true if the floating point result of calling FNDECL with arguments
15570 : : ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15571 : : considered integer values. Return false for signaling NaN. If FNDECL
15572 : : takes fewer than 2 arguments, the remaining ARGn are null.
15573 : :
15574 : : DEPTH is the current nesting depth of the query. */
15575 : :
15576 : : bool
15577 : 874 : integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15578 : : {
15579 : 874 : switch (fn)
15580 : : {
15581 : : CASE_CFN_CEIL:
15582 : : CASE_CFN_CEIL_FN:
15583 : : CASE_CFN_FLOOR:
15584 : : CASE_CFN_FLOOR_FN:
15585 : : CASE_CFN_NEARBYINT:
15586 : : CASE_CFN_NEARBYINT_FN:
15587 : : CASE_CFN_RINT:
15588 : : CASE_CFN_RINT_FN:
15589 : : CASE_CFN_ROUND:
15590 : : CASE_CFN_ROUND_FN:
15591 : : CASE_CFN_ROUNDEVEN:
15592 : : CASE_CFN_ROUNDEVEN_FN:
15593 : : CASE_CFN_TRUNC:
15594 : : CASE_CFN_TRUNC_FN:
15595 : : return true;
15596 : :
15597 : 336 : CASE_CFN_FMIN:
15598 : 336 : CASE_CFN_FMIN_FN:
15599 : 336 : CASE_CFN_FMAX:
15600 : 336 : CASE_CFN_FMAX_FN:
15601 : 336 : return RECURSE (arg0) && RECURSE (arg1);
15602 : :
15603 : : default:
15604 : : break;
15605 : : }
15606 : : return false;
15607 : : }
15608 : :
15609 : : /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15610 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15611 : : considered integer values. Return false for signaling NaN.
15612 : :
15613 : : DEPTH is the current nesting depth of the query. */
15614 : :
15615 : : bool
15616 : 129205 : integer_valued_real_single_p (tree t, int depth)
15617 : : {
15618 : 129205 : switch (TREE_CODE (t))
15619 : : {
15620 : 2226 : case REAL_CST:
15621 : 2226 : return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15622 : :
15623 : 0 : case COND_EXPR:
15624 : 0 : return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15625 : :
15626 : 90542 : case SSA_NAME:
15627 : : /* Limit the depth of recursion to avoid quadratic behavior.
15628 : : This is expected to catch almost all occurrences in practice.
15629 : : If this code misses important cases that unbounded recursion
15630 : : would not, passes that need this information could be revised
15631 : : to provide it through dataflow propagation. */
15632 : 90542 : return (!name_registered_for_update_p (t)
15633 : 90542 : && depth < param_max_ssa_name_query_depth
15634 : 180305 : && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15635 : : depth));
15636 : :
15637 : : default:
15638 : : break;
15639 : : }
15640 : : return false;
15641 : : }
15642 : :
15643 : : /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15644 : : has an integer value. We also allow +Inf, -Inf and NaN to be
15645 : : considered integer values. Return false for signaling NaN.
15646 : :
15647 : : DEPTH is the current nesting depth of the query. */
15648 : :
15649 : : static bool
15650 : 0 : integer_valued_real_invalid_p (tree t, int depth)
15651 : : {
15652 : 0 : switch (TREE_CODE (t))
15653 : : {
15654 : 0 : case COMPOUND_EXPR:
15655 : 0 : case MODIFY_EXPR:
15656 : 0 : case BIND_EXPR:
15657 : 0 : return RECURSE (TREE_OPERAND (t, 1));
15658 : :
15659 : 0 : case SAVE_EXPR:
15660 : 0 : return RECURSE (TREE_OPERAND (t, 0));
15661 : :
15662 : : default:
15663 : : break;
15664 : : }
15665 : : return false;
15666 : : }
15667 : :
15668 : : #undef RECURSE
15669 : : #undef integer_valued_real_p
15670 : :
15671 : : /* Return true if the floating point expression T has an integer value.
15672 : : We also allow +Inf, -Inf and NaN to be considered integer values.
15673 : : Return false for signaling NaN.
15674 : :
15675 : : DEPTH is the current nesting depth of the query. */
15676 : :
15677 : : bool
15678 : 97413 : integer_valued_real_p (tree t, int depth)
15679 : : {
15680 : 97413 : if (t == error_mark_node)
15681 : : return false;
15682 : :
15683 : 97413 : STRIP_ANY_LOCATION_WRAPPER (t);
15684 : :
15685 : 97413 : tree_code code = TREE_CODE (t);
15686 : 97413 : switch (TREE_CODE_CLASS (code))
15687 : : {
15688 : 0 : case tcc_binary:
15689 : 0 : case tcc_comparison:
15690 : 0 : return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15691 : 0 : TREE_OPERAND (t, 1), depth);
15692 : :
15693 : 0 : case tcc_unary:
15694 : 0 : return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15695 : :
15696 : 8338 : case tcc_constant:
15697 : 8338 : case tcc_declaration:
15698 : 8338 : case tcc_reference:
15699 : 8338 : return integer_valued_real_single_p (t, depth);
15700 : :
15701 : 89075 : default:
15702 : 89075 : break;
15703 : : }
15704 : :
15705 : 89075 : switch (code)
15706 : : {
15707 : 89075 : case COND_EXPR:
15708 : 89075 : case SSA_NAME:
15709 : 89075 : return integer_valued_real_single_p (t, depth);
15710 : :
15711 : 0 : case CALL_EXPR:
15712 : 0 : {
15713 : 0 : tree arg0 = (call_expr_nargs (t) > 0
15714 : 0 : ? CALL_EXPR_ARG (t, 0)
15715 : 0 : : NULL_TREE);
15716 : 0 : tree arg1 = (call_expr_nargs (t) > 1
15717 : 0 : ? CALL_EXPR_ARG (t, 1)
15718 : 0 : : NULL_TREE);
15719 : 0 : return integer_valued_real_call_p (get_call_combined_fn (t),
15720 : 0 : arg0, arg1, depth);
15721 : : }
15722 : :
15723 : 0 : default:
15724 : 0 : return integer_valued_real_invalid_p (t, depth);
15725 : : }
15726 : : }
15727 : :
15728 : : /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15729 : : attempt to fold the expression to a constant without modifying TYPE,
15730 : : OP0 or OP1.
15731 : :
15732 : : If the expression could be simplified to a constant, then return
15733 : : the constant. If the expression would not be simplified to a
15734 : : constant, then return NULL_TREE. */
15735 : :
15736 : : tree
15737 : 15293934 : fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15738 : : {
15739 : 15293934 : tree tem = fold_binary (code, type, op0, op1);
15740 : 15293934 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15741 : : }
15742 : :
15743 : : /* Given the components of a unary expression CODE, TYPE and OP0,
15744 : : attempt to fold the expression to a constant without modifying
15745 : : TYPE or OP0.
15746 : :
15747 : : If the expression could be simplified to a constant, then return
15748 : : the constant. If the expression would not be simplified to a
15749 : : constant, then return NULL_TREE. */
15750 : :
15751 : : tree
15752 : 0 : fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15753 : : {
15754 : 0 : tree tem = fold_unary (code, type, op0);
15755 : 0 : return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15756 : : }
15757 : :
15758 : : /* If EXP represents referencing an element in a constant string
15759 : : (either via pointer arithmetic or array indexing), return the
15760 : : tree representing the value accessed, otherwise return NULL. */
15761 : :
15762 : : tree
15763 : 158729381 : fold_read_from_constant_string (tree exp)
15764 : : {
15765 : 158729381 : if ((INDIRECT_REF_P (exp)
15766 : 158729363 : || TREE_CODE (exp) == ARRAY_REF)
15767 : 169748523 : && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15768 : : {
15769 : 7753287 : tree exp1 = TREE_OPERAND (exp, 0);
15770 : 7753287 : tree index;
15771 : 7753287 : tree string;
15772 : 7753287 : location_t loc = EXPR_LOCATION (exp);
15773 : :
15774 : 7753287 : if (INDIRECT_REF_P (exp))
15775 : 0 : string = string_constant (exp1, &index, NULL, NULL);
15776 : : else
15777 : : {
15778 : 7753287 : tree low_bound = array_ref_low_bound (exp);
15779 : 7753287 : index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15780 : :
15781 : : /* Optimize the special-case of a zero lower bound.
15782 : :
15783 : : We convert the low_bound to sizetype to avoid some problems
15784 : : with constant folding. (E.g. suppose the lower bound is 1,
15785 : : and its mode is QI. Without the conversion,l (ARRAY
15786 : : +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15787 : : +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15788 : 7753287 : if (! integer_zerop (low_bound))
15789 : 139781 : index = size_diffop_loc (loc, index,
15790 : : fold_convert_loc (loc, sizetype, low_bound));
15791 : :
15792 : : string = exp1;
15793 : : }
15794 : :
15795 : 7753287 : scalar_int_mode char_mode;
15796 : 7753287 : if (string
15797 : 7753287 : && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15798 : 7753287 : && TREE_CODE (string) == STRING_CST
15799 : 67807 : && tree_fits_uhwi_p (index)
15800 : 59823 : && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15801 : 7814704 : && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15802 : : &char_mode)
15803 : 15506574 : && GET_MODE_SIZE (char_mode) == 1)
15804 : 113198 : return build_int_cst_type (TREE_TYPE (exp),
15805 : 56599 : (TREE_STRING_POINTER (string)
15806 : 56599 : [TREE_INT_CST_LOW (index)]));
15807 : : }
15808 : : return NULL;
15809 : : }
15810 : :
15811 : : /* Folds a read from vector element at IDX of vector ARG. */
15812 : :
15813 : : tree
15814 : 5230 : fold_read_from_vector (tree arg, poly_uint64 idx)
15815 : : {
15816 : 5230 : unsigned HOST_WIDE_INT i;
15817 : 5230 : if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15818 : 5230 : && known_ge (idx, 0u)
15819 : 5230 : && idx.is_constant (&i))
15820 : : {
15821 : 5230 : if (TREE_CODE (arg) == VECTOR_CST)
15822 : 1938 : return VECTOR_CST_ELT (arg, i);
15823 : 3292 : else if (TREE_CODE (arg) == CONSTRUCTOR)
15824 : : {
15825 : 1537 : if (CONSTRUCTOR_NELTS (arg)
15826 : 1497 : && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15827 : : return NULL_TREE;
15828 : 1535 : if (i >= CONSTRUCTOR_NELTS (arg))
15829 : 40 : return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15830 : 1495 : return CONSTRUCTOR_ELT (arg, i)->value;
15831 : : }
15832 : : }
15833 : : return NULL_TREE;
15834 : : }
15835 : :
15836 : : /* Return the tree for neg (ARG0) when ARG0 is known to be either
15837 : : an integer constant, real, or fixed-point constant.
15838 : :
15839 : : TYPE is the type of the result. */
15840 : :
15841 : : static tree
15842 : 30332238 : fold_negate_const (tree arg0, tree type)
15843 : : {
15844 : 30332238 : tree t = NULL_TREE;
15845 : :
15846 : 30332238 : switch (TREE_CODE (arg0))
15847 : : {
15848 : 2041381 : case REAL_CST:
15849 : 2041381 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15850 : 2041381 : break;
15851 : :
15852 : 0 : case FIXED_CST:
15853 : 0 : {
15854 : 0 : FIXED_VALUE_TYPE f;
15855 : 0 : bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15856 : 0 : &(TREE_FIXED_CST (arg0)), NULL,
15857 : 0 : TYPE_SATURATING (type));
15858 : 0 : t = build_fixed (type, f);
15859 : : /* Propagate overflow flags. */
15860 : 0 : if (overflow_p | TREE_OVERFLOW (arg0))
15861 : 0 : TREE_OVERFLOW (t) = 1;
15862 : 0 : break;
15863 : : }
15864 : :
15865 : 28290857 : default:
15866 : 28290857 : if (poly_int_tree_p (arg0))
15867 : : {
15868 : 28290857 : wi::overflow_type overflow;
15869 : 28290857 : poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15870 : 28290857 : t = force_fit_type (type, res, 1,
15871 : 221976 : (overflow && ! TYPE_UNSIGNED (type))
15872 : 28504005 : || TREE_OVERFLOW (arg0));
15873 : 28290857 : break;
15874 : 28290857 : }
15875 : :
15876 : 0 : gcc_unreachable ();
15877 : : }
15878 : :
15879 : 30332238 : return t;
15880 : : }
15881 : :
15882 : : /* Return the tree for abs (ARG0) when ARG0 is known to be either
15883 : : an integer constant or real constant.
15884 : :
15885 : : TYPE is the type of the result. */
15886 : :
15887 : : tree
15888 : 35825 : fold_abs_const (tree arg0, tree type)
15889 : : {
15890 : 35825 : tree t = NULL_TREE;
15891 : :
15892 : 35825 : switch (TREE_CODE (arg0))
15893 : : {
15894 : 8356 : case INTEGER_CST:
15895 : 8356 : {
15896 : : /* If the value is unsigned or non-negative, then the absolute value
15897 : : is the same as the ordinary value. */
15898 : 8356 : wide_int val = wi::to_wide (arg0);
15899 : 8356 : wi::overflow_type overflow = wi::OVF_NONE;
15900 : 8356 : if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15901 : : ;
15902 : :
15903 : : /* If the value is negative, then the absolute value is
15904 : : its negation. */
15905 : : else
15906 : 4826 : val = wi::neg (val, &overflow);
15907 : :
15908 : : /* Force to the destination type, set TREE_OVERFLOW for signed
15909 : : TYPE only. */
15910 : 8356 : t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15911 : 8356 : }
15912 : 8356 : break;
15913 : :
15914 : 27469 : case REAL_CST:
15915 : 27469 : if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15916 : 7322 : t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15917 : : else
15918 : : t = arg0;
15919 : : break;
15920 : :
15921 : 0 : default:
15922 : 0 : gcc_unreachable ();
15923 : : }
15924 : :
15925 : 35825 : return t;
15926 : : }
15927 : :
15928 : : /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15929 : : constant. TYPE is the type of the result. */
15930 : :
15931 : : static tree
15932 : 2326091 : fold_not_const (const_tree arg0, tree type)
15933 : : {
15934 : 2326091 : gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15935 : :
15936 : 2326091 : return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15937 : : }
15938 : :
15939 : : /* Given CODE, a relational operator, the target type, TYPE and two
15940 : : constant operands OP0 and OP1, return the result of the
15941 : : relational operation. If the result is not a compile time
15942 : : constant, then return NULL_TREE. */
15943 : :
15944 : : static tree
15945 : 55537262 : fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15946 : : {
15947 : 55537262 : int result, invert;
15948 : :
15949 : : /* From here on, the only cases we handle are when the result is
15950 : : known to be a constant. */
15951 : :
15952 : 55537262 : if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15953 : : {
15954 : 1107224 : const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15955 : 1107224 : const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15956 : :
15957 : : /* Handle the cases where either operand is a NaN. */
15958 : 1107224 : if (real_isnan (c0) || real_isnan (c1))
15959 : : {
15960 : 14599 : switch (code)
15961 : : {
15962 : : case EQ_EXPR:
15963 : : case ORDERED_EXPR:
15964 : : result = 0;
15965 : : break;
15966 : :
15967 : : case NE_EXPR:
15968 : : case UNORDERED_EXPR:
15969 : : case UNLT_EXPR:
15970 : : case UNLE_EXPR:
15971 : : case UNGT_EXPR:
15972 : : case UNGE_EXPR:
15973 : : case UNEQ_EXPR:
15974 : 6593 : result = 1;
15975 : : break;
15976 : :
15977 : 8028 : case LT_EXPR:
15978 : 8028 : case LE_EXPR:
15979 : 8028 : case GT_EXPR:
15980 : 8028 : case GE_EXPR:
15981 : 8028 : case LTGT_EXPR:
15982 : 8028 : if (flag_trapping_math)
15983 : : return NULL_TREE;
15984 : : result = 0;
15985 : : break;
15986 : :
15987 : 0 : default:
15988 : 0 : gcc_unreachable ();
15989 : : }
15990 : :
15991 : 6593 : return constant_boolean_node (result, type);
15992 : : }
15993 : :
15994 : 1092625 : return constant_boolean_node (real_compare (code, c0, c1), type);
15995 : : }
15996 : :
15997 : 54430038 : if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15998 : : {
15999 : 0 : const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16000 : 0 : const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16001 : 0 : return constant_boolean_node (fixed_compare (code, c0, c1), type);
16002 : : }
16003 : :
16004 : : /* Handle equality/inequality of complex constants. */
16005 : 54430038 : if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16006 : : {
16007 : 58276 : tree rcond = fold_relational_const (code, type,
16008 : 29138 : TREE_REALPART (op0),
16009 : 29138 : TREE_REALPART (op1));
16010 : 116552 : tree icond = fold_relational_const (code, type,
16011 : 29138 : TREE_IMAGPART (op0),
16012 : 29138 : TREE_IMAGPART (op1));
16013 : 29138 : if (code == EQ_EXPR)
16014 : 286 : return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16015 : 28852 : else if (code == NE_EXPR)
16016 : 28852 : return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16017 : : else
16018 : : return NULL_TREE;
16019 : : }
16020 : :
16021 : 54400900 : if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16022 : : {
16023 : 4936 : if (!VECTOR_TYPE_P (type))
16024 : : {
16025 : : /* Have vector comparison with scalar boolean result. */
16026 : 142 : gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
16027 : : && known_eq (VECTOR_CST_NELTS (op0),
16028 : : VECTOR_CST_NELTS (op1)));
16029 : 142 : unsigned HOST_WIDE_INT nunits;
16030 : 142 : if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
16031 : : return NULL_TREE;
16032 : 805 : for (unsigned i = 0; i < nunits; i++)
16033 : : {
16034 : 718 : tree elem0 = VECTOR_CST_ELT (op0, i);
16035 : 718 : tree elem1 = VECTOR_CST_ELT (op1, i);
16036 : 718 : tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
16037 : 718 : if (tmp == NULL_TREE)
16038 : : return NULL_TREE;
16039 : 718 : if (integer_zerop (tmp))
16040 : 55 : return constant_boolean_node (code == NE_EXPR, type);
16041 : : }
16042 : 87 : return constant_boolean_node (code == EQ_EXPR, type);
16043 : : }
16044 : 4794 : tree_vector_builder elts;
16045 : 4794 : if (!elts.new_binary_operation (type, op0, op1, false))
16046 : : return NULL_TREE;
16047 : 4794 : unsigned int count = elts.encoded_nelts ();
16048 : 22360 : for (unsigned i = 0; i < count; i++)
16049 : : {
16050 : 17566 : tree elem_type = TREE_TYPE (type);
16051 : 17566 : tree elem0 = VECTOR_CST_ELT (op0, i);
16052 : 17566 : tree elem1 = VECTOR_CST_ELT (op1, i);
16053 : :
16054 : 17566 : tree tem = fold_relational_const (code, elem_type,
16055 : : elem0, elem1);
16056 : :
16057 : 17566 : if (tem == NULL_TREE)
16058 : : return NULL_TREE;
16059 : :
16060 : 17566 : elts.quick_push (build_int_cst (elem_type,
16061 : 24851 : integer_zerop (tem) ? 0 : -1));
16062 : : }
16063 : :
16064 : 4794 : return elts.build ();
16065 : 4794 : }
16066 : :
16067 : : /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16068 : :
16069 : : To compute GT, swap the arguments and do LT.
16070 : : To compute GE, do LT and invert the result.
16071 : : To compute LE, swap the arguments, do LT and invert the result.
16072 : : To compute NE, do EQ and invert the result.
16073 : :
16074 : : Therefore, the code below must handle only EQ and LT. */
16075 : :
16076 : 54395964 : if (code == LE_EXPR || code == GT_EXPR)
16077 : : {
16078 : 10861683 : std::swap (op0, op1);
16079 : 10861683 : code = swap_tree_comparison (code);
16080 : : }
16081 : :
16082 : : /* Note that it is safe to invert for real values here because we
16083 : : have already handled the one case that it matters. */
16084 : :
16085 : 54395964 : invert = 0;
16086 : 54395964 : if (code == NE_EXPR || code == GE_EXPR)
16087 : : {
16088 : 27191352 : invert = 1;
16089 : 27191352 : code = invert_tree_comparison (code, false);
16090 : : }
16091 : :
16092 : : /* Compute a result for LT or EQ if args permit;
16093 : : Otherwise return T. */
16094 : 54395964 : if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16095 : : {
16096 : 54376155 : if (code == EQ_EXPR)
16097 : 28211710 : result = tree_int_cst_equal (op0, op1);
16098 : : else
16099 : 26164445 : result = tree_int_cst_lt (op0, op1);
16100 : : }
16101 : : else
16102 : : return NULL_TREE;
16103 : :
16104 : 54376155 : if (invert)
16105 : 27189204 : result ^= 1;
16106 : 54376155 : return constant_boolean_node (result, type);
16107 : : }
16108 : :
16109 : : /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16110 : : indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16111 : : itself. */
16112 : :
16113 : : tree
16114 : 115745801 : fold_build_cleanup_point_expr (tree type, tree expr)
16115 : : {
16116 : : /* If the expression does not have side effects then we don't have to wrap
16117 : : it with a cleanup point expression. */
16118 : 115745801 : if (!TREE_SIDE_EFFECTS (expr))
16119 : : return expr;
16120 : :
16121 : : /* If the expression is a return, check to see if the expression inside the
16122 : : return has no side effects or the right hand side of the modify expression
16123 : : inside the return. If either don't have side effects set we don't need to
16124 : : wrap the expression in a cleanup point expression. Note we don't check the
16125 : : left hand side of the modify because it should always be a return decl. */
16126 : 100538694 : if (TREE_CODE (expr) == RETURN_EXPR)
16127 : : {
16128 : 36081914 : tree op = TREE_OPERAND (expr, 0);
16129 : 36081914 : if (!op || !TREE_SIDE_EFFECTS (op))
16130 : : return expr;
16131 : 35419044 : op = TREE_OPERAND (op, 1);
16132 : 35419044 : if (!TREE_SIDE_EFFECTS (op))
16133 : : return expr;
16134 : : }
16135 : :
16136 : 81738698 : return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16137 : : }
16138 : :
16139 : : /* Given a pointer value OP0 and a type TYPE, return a simplified version
16140 : : of an indirection through OP0, or NULL_TREE if no simplification is
16141 : : possible. */
16142 : :
16143 : : tree
16144 : 17778146 : fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16145 : : {
16146 : 17778146 : tree sub = op0;
16147 : 17778146 : tree subtype;
16148 : 17778146 : poly_uint64 const_op01;
16149 : :
16150 : 17778146 : STRIP_NOPS (sub);
16151 : 17778146 : subtype = TREE_TYPE (sub);
16152 : 17778146 : if (!POINTER_TYPE_P (subtype)
16153 : 17778146 : || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16154 : : return NULL_TREE;
16155 : :
16156 : 17630490 : if (TREE_CODE (sub) == ADDR_EXPR)
16157 : : {
16158 : 3178455 : tree op = TREE_OPERAND (sub, 0);
16159 : 3178455 : tree optype = TREE_TYPE (op);
16160 : :
16161 : : /* *&CONST_DECL -> to the value of the const decl. */
16162 : 3178455 : if (TREE_CODE (op) == CONST_DECL)
16163 : 2702 : return DECL_INITIAL (op);
16164 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
16165 : 3175753 : if (type == optype)
16166 : : {
16167 : 2267458 : tree fop = fold_read_from_constant_string (op);
16168 : 2267458 : if (fop)
16169 : : return fop;
16170 : : else
16171 : 2225488 : return op;
16172 : : }
16173 : : /* *(foo *)&fooarray => fooarray[0] */
16174 : 908295 : else if (TREE_CODE (optype) == ARRAY_TYPE
16175 : 11809 : && type == TREE_TYPE (optype)
16176 : 918944 : && (!in_gimple_form
16177 : 1355 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16178 : : {
16179 : 10649 : tree type_domain = TYPE_DOMAIN (optype);
16180 : 10649 : tree min_val = size_zero_node;
16181 : 10649 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16182 : 10611 : min_val = TYPE_MIN_VALUE (type_domain);
16183 : 10649 : if (in_gimple_form
16184 : 1355 : && TREE_CODE (min_val) != INTEGER_CST)
16185 : : return NULL_TREE;
16186 : 10649 : return build4_loc (loc, ARRAY_REF, type, op, min_val,
16187 : 10649 : NULL_TREE, NULL_TREE);
16188 : : }
16189 : : /* *(foo *)&complexfoo => __real__ complexfoo */
16190 : 897646 : else if (TREE_CODE (optype) == COMPLEX_TYPE
16191 : 897646 : && type == TREE_TYPE (optype))
16192 : 0 : return fold_build1_loc (loc, REALPART_EXPR, type, op);
16193 : : /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16194 : 897646 : else if (VECTOR_TYPE_P (optype)
16195 : 897646 : && type == TREE_TYPE (optype))
16196 : : {
16197 : 70 : tree part_width = TYPE_SIZE (type);
16198 : 70 : tree index = bitsize_int (0);
16199 : 70 : return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16200 : 70 : index);
16201 : : }
16202 : : }
16203 : :
16204 : 15349611 : if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16205 : 15349611 : && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16206 : : {
16207 : 268357 : tree op00 = TREE_OPERAND (sub, 0);
16208 : 268357 : tree op01 = TREE_OPERAND (sub, 1);
16209 : :
16210 : 268357 : STRIP_NOPS (op00);
16211 : 268357 : if (TREE_CODE (op00) == ADDR_EXPR)
16212 : : {
16213 : 6138 : tree op00type;
16214 : 6138 : op00 = TREE_OPERAND (op00, 0);
16215 : 6138 : op00type = TREE_TYPE (op00);
16216 : :
16217 : : /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16218 : 6138 : if (VECTOR_TYPE_P (op00type)
16219 : 240 : && type == TREE_TYPE (op00type)
16220 : : /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16221 : : but we want to treat offsets with MSB set as negative.
16222 : : For the code below negative offsets are invalid and
16223 : : TYPE_SIZE of the element is something unsigned, so
16224 : : check whether op01 fits into poly_int64, which implies
16225 : : it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16226 : : then just use poly_uint64 because we want to treat the
16227 : : value as unsigned. */
16228 : 6331 : && tree_fits_poly_int64_p (op01))
16229 : : {
16230 : 179 : tree part_width = TYPE_SIZE (type);
16231 : 179 : poly_uint64 max_offset
16232 : 179 : = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16233 : 179 : * TYPE_VECTOR_SUBPARTS (op00type));
16234 : 179 : if (known_lt (const_op01, max_offset))
16235 : : {
16236 : 179 : tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16237 : 179 : return fold_build3_loc (loc,
16238 : : BIT_FIELD_REF, type, op00,
16239 : 179 : part_width, index);
16240 : : }
16241 : : }
16242 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16243 : 5959 : else if (TREE_CODE (op00type) == COMPLEX_TYPE
16244 : 5959 : && type == TREE_TYPE (op00type))
16245 : : {
16246 : 0 : if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16247 : : const_op01))
16248 : 0 : return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16249 : : }
16250 : : /* ((foo *)&fooarray)[1] => fooarray[1] */
16251 : 5959 : else if (TREE_CODE (op00type) == ARRAY_TYPE
16252 : 5959 : && type == TREE_TYPE (op00type))
16253 : : {
16254 : 4471 : tree type_domain = TYPE_DOMAIN (op00type);
16255 : 4471 : tree min_val = size_zero_node;
16256 : 4471 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16257 : 4470 : min_val = TYPE_MIN_VALUE (type_domain);
16258 : 4471 : poly_uint64 type_size, index;
16259 : 4471 : if (poly_int_tree_p (min_val)
16260 : 4471 : && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16261 : 4471 : && multiple_p (const_op01, type_size, &index))
16262 : : {
16263 : 4471 : poly_offset_int off = index + wi::to_poly_offset (min_val);
16264 : 4471 : op01 = wide_int_to_tree (sizetype, off);
16265 : 4471 : return build4_loc (loc, ARRAY_REF, type, op00, op01,
16266 : : NULL_TREE, NULL_TREE);
16267 : : }
16268 : : }
16269 : : }
16270 : : }
16271 : :
16272 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16273 : 15344961 : if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16274 : 636830 : && type == TREE_TYPE (TREE_TYPE (subtype))
16275 : 15347897 : && (!in_gimple_form
16276 : 12 : || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16277 : : {
16278 : 2935 : tree type_domain;
16279 : 2935 : tree min_val = size_zero_node;
16280 : 2935 : sub = build_fold_indirect_ref_loc (loc, sub);
16281 : 2935 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16282 : 2935 : if (type_domain && TYPE_MIN_VALUE (type_domain))
16283 : 2935 : min_val = TYPE_MIN_VALUE (type_domain);
16284 : 2935 : if (in_gimple_form
16285 : 11 : && TREE_CODE (min_val) != INTEGER_CST)
16286 : : return NULL_TREE;
16287 : 2935 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16288 : 2935 : NULL_TREE);
16289 : : }
16290 : :
16291 : : return NULL_TREE;
16292 : : }
16293 : :
16294 : : /* Builds an expression for an indirection through T, simplifying some
16295 : : cases. */
16296 : :
16297 : : tree
16298 : 7097015 : build_fold_indirect_ref_loc (location_t loc, tree t)
16299 : : {
16300 : 7097015 : tree type = TREE_TYPE (TREE_TYPE (t));
16301 : 7097015 : tree sub = fold_indirect_ref_1 (loc, type, t);
16302 : :
16303 : 7097015 : if (sub)
16304 : : return sub;
16305 : :
16306 : 4833064 : return build1_loc (loc, INDIRECT_REF, type, t);
16307 : : }
16308 : :
16309 : : /* Given an INDIRECT_REF T, return either T or a simplified version. */
16310 : :
16311 : : tree
16312 : 10370841 : fold_indirect_ref_loc (location_t loc, tree t)
16313 : : {
16314 : 10370841 : tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16315 : :
16316 : 10370841 : if (sub)
16317 : : return sub;
16318 : : else
16319 : 10346538 : return t;
16320 : : }
16321 : :
16322 : : /* Strip non-trapping, non-side-effecting tree nodes from an expression
16323 : : whose result is ignored. The type of the returned tree need not be
16324 : : the same as the original expression. */
16325 : :
16326 : : tree
16327 : 133215 : fold_ignored_result (tree t)
16328 : : {
16329 : 133215 : if (!TREE_SIDE_EFFECTS (t))
16330 : 18617 : return integer_zero_node;
16331 : :
16332 : 151931 : for (;;)
16333 : 151931 : switch (TREE_CODE_CLASS (TREE_CODE (t)))
16334 : : {
16335 : 3754 : case tcc_unary:
16336 : 3754 : t = TREE_OPERAND (t, 0);
16337 : 3754 : break;
16338 : :
16339 : 4928 : case tcc_binary:
16340 : 4928 : case tcc_comparison:
16341 : 4928 : if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16342 : 3037 : t = TREE_OPERAND (t, 0);
16343 : 1891 : else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16344 : 19 : t = TREE_OPERAND (t, 1);
16345 : : else
16346 : : return t;
16347 : : break;
16348 : :
16349 : 99188 : case tcc_expression:
16350 : 99188 : switch (TREE_CODE (t))
16351 : : {
16352 : 30506 : case COMPOUND_EXPR:
16353 : 30506 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16354 : : return t;
16355 : 30228 : t = TREE_OPERAND (t, 0);
16356 : 30228 : break;
16357 : :
16358 : 382 : case COND_EXPR:
16359 : 382 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16360 : 382 : || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16361 : : return t;
16362 : 295 : t = TREE_OPERAND (t, 0);
16363 : 295 : break;
16364 : :
16365 : : default:
16366 : : return t;
16367 : : }
16368 : : break;
16369 : :
16370 : : default:
16371 : : return t;
16372 : : }
16373 : : }
16374 : :
16375 : : /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16376 : :
16377 : : tree
16378 : 2642475632 : round_up_loc (location_t loc, tree value, unsigned int divisor)
16379 : : {
16380 : 2642475632 : tree div = NULL_TREE;
16381 : :
16382 : 2642475632 : if (divisor == 1)
16383 : : return value;
16384 : :
16385 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16386 : : have to do anything. Only do this when we are not given a const,
16387 : : because in that case, this check is more expensive than just
16388 : : doing it. */
16389 : 1644749179 : if (TREE_CODE (value) != INTEGER_CST)
16390 : : {
16391 : 334715 : div = build_int_cst (TREE_TYPE (value), divisor);
16392 : :
16393 : 334715 : if (multiple_of_p (TREE_TYPE (value), value, div))
16394 : : return value;
16395 : : }
16396 : :
16397 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16398 : 1644416255 : if (pow2_or_zerop (divisor))
16399 : : {
16400 : 1644416255 : if (TREE_CODE (value) == INTEGER_CST)
16401 : : {
16402 : 1644414464 : wide_int val = wi::to_wide (value);
16403 : 1644414464 : bool overflow_p;
16404 : :
16405 : 1644414464 : if ((val & (divisor - 1)) == 0)
16406 : : return value;
16407 : :
16408 : 3529404 : overflow_p = TREE_OVERFLOW (value);
16409 : 3529404 : val += divisor - 1;
16410 : 3529404 : val &= (int) -divisor;
16411 : 3529404 : if (val == 0)
16412 : 4 : overflow_p = true;
16413 : :
16414 : 3529404 : return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16415 : 1644414464 : }
16416 : : else
16417 : : {
16418 : 1791 : tree t;
16419 : :
16420 : 1791 : t = build_int_cst (TREE_TYPE (value), divisor - 1);
16421 : 1791 : value = size_binop_loc (loc, PLUS_EXPR, value, t);
16422 : 1791 : t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16423 : 1791 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16424 : : }
16425 : : }
16426 : : else
16427 : : {
16428 : 0 : if (!div)
16429 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16430 : 0 : value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16431 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16432 : : }
16433 : :
16434 : : return value;
16435 : : }
16436 : :
16437 : : /* Likewise, but round down. */
16438 : :
16439 : : tree
16440 : 16462219 : round_down_loc (location_t loc, tree value, int divisor)
16441 : : {
16442 : 16462219 : tree div = NULL_TREE;
16443 : :
16444 : 16462219 : gcc_assert (divisor > 0);
16445 : 16462219 : if (divisor == 1)
16446 : : return value;
16447 : :
16448 : : /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16449 : : have to do anything. Only do this when we are not given a const,
16450 : : because in that case, this check is more expensive than just
16451 : : doing it. */
16452 : 16462219 : if (TREE_CODE (value) != INTEGER_CST)
16453 : : {
16454 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16455 : :
16456 : 0 : if (multiple_of_p (TREE_TYPE (value), value, div))
16457 : : return value;
16458 : : }
16459 : :
16460 : : /* If divisor is a power of two, simplify this to bit manipulation. */
16461 : 16462219 : if (pow2_or_zerop (divisor))
16462 : : {
16463 : 16462219 : tree t;
16464 : :
16465 : 16462219 : t = build_int_cst (TREE_TYPE (value), -divisor);
16466 : 16462219 : value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16467 : : }
16468 : : else
16469 : : {
16470 : 0 : if (!div)
16471 : 0 : div = build_int_cst (TREE_TYPE (value), divisor);
16472 : 0 : value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16473 : 0 : value = size_binop_loc (loc, MULT_EXPR, value, div);
16474 : : }
16475 : :
16476 : : return value;
16477 : : }
16478 : :
16479 : : /* Returns the pointer to the base of the object addressed by EXP and
16480 : : extracts the information about the offset of the access, storing it
16481 : : to PBITPOS and POFFSET. */
16482 : :
16483 : : static tree
16484 : 1582562 : split_address_to_core_and_offset (tree exp,
16485 : : poly_int64 *pbitpos, tree *poffset)
16486 : : {
16487 : 1582562 : tree core;
16488 : 1582562 : machine_mode mode;
16489 : 1582562 : int unsignedp, reversep, volatilep;
16490 : 1582562 : poly_int64 bitsize;
16491 : 1582562 : location_t loc = EXPR_LOCATION (exp);
16492 : :
16493 : 1582562 : if (TREE_CODE (exp) == SSA_NAME)
16494 : 513753 : if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16495 : 374866 : if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16496 : 31337 : exp = gimple_assign_rhs1 (def);
16497 : :
16498 : 1582562 : if (TREE_CODE (exp) == ADDR_EXPR)
16499 : : {
16500 : 926734 : core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16501 : : poffset, &mode, &unsignedp, &reversep,
16502 : : &volatilep);
16503 : 926734 : core = build_fold_addr_expr_loc (loc, core);
16504 : : }
16505 : 655828 : else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16506 : : {
16507 : 66796 : core = TREE_OPERAND (exp, 0);
16508 : 66796 : STRIP_NOPS (core);
16509 : 66796 : *pbitpos = 0;
16510 : 66796 : *poffset = TREE_OPERAND (exp, 1);
16511 : 66796 : if (poly_int_tree_p (*poffset))
16512 : : {
16513 : 66706 : poly_offset_int tem
16514 : 66706 : = wi::sext (wi::to_poly_offset (*poffset),
16515 : 66706 : TYPE_PRECISION (TREE_TYPE (*poffset)));
16516 : 66706 : tem <<= LOG2_BITS_PER_UNIT;
16517 : 66706 : if (tem.to_shwi (pbitpos))
16518 : 66706 : *poffset = NULL_TREE;
16519 : : }
16520 : : }
16521 : : else
16522 : : {
16523 : 589032 : core = exp;
16524 : 589032 : *pbitpos = 0;
16525 : 589032 : *poffset = NULL_TREE;
16526 : : }
16527 : :
16528 : 1582562 : return core;
16529 : : }
16530 : :
16531 : : /* Returns true if addresses of E1 and E2 differ by a constant, false
16532 : : otherwise. If they do, E1 - E2 is stored in *DIFF. */
16533 : :
16534 : : bool
16535 : 791281 : ptr_difference_const (tree e1, tree e2, poly_int64 *diff)
16536 : : {
16537 : 791281 : tree core1, core2;
16538 : 791281 : poly_int64 bitpos1, bitpos2;
16539 : 791281 : tree toffset1, toffset2, tdiff, type;
16540 : :
16541 : 791281 : core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16542 : 791281 : core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16543 : :
16544 : 791281 : poly_int64 bytepos1, bytepos2;
16545 : 791281 : if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16546 : 1376211 : || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16547 : 1582562 : || !operand_equal_p (core1, core2, 0))
16548 : 584930 : return false;
16549 : :
16550 : 206351 : if (toffset1 && toffset2)
16551 : : {
16552 : 29 : type = TREE_TYPE (toffset1);
16553 : 29 : if (type != TREE_TYPE (toffset2))
16554 : 0 : toffset2 = fold_convert (type, toffset2);
16555 : :
16556 : 29 : tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16557 : 29 : if (!cst_and_fits_in_hwi (tdiff))
16558 : : return false;
16559 : :
16560 : 15 : *diff = int_cst_value (tdiff);
16561 : : }
16562 : 206322 : else if (toffset1 || toffset2)
16563 : : {
16564 : : /* If only one of the offsets is non-constant, the difference cannot
16565 : : be a constant. */
16566 : : return false;
16567 : : }
16568 : : else
16569 : 188573 : *diff = 0;
16570 : :
16571 : 188588 : *diff += bytepos1 - bytepos2;
16572 : 188588 : return true;
16573 : : }
16574 : :
16575 : : /* Return OFF converted to a pointer offset type suitable as offset for
16576 : : POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16577 : : tree
16578 : 18063017 : convert_to_ptrofftype_loc (location_t loc, tree off)
16579 : : {
16580 : 18063017 : if (ptrofftype_p (TREE_TYPE (off)))
16581 : : return off;
16582 : 2219885 : return fold_convert_loc (loc, sizetype, off);
16583 : : }
16584 : :
16585 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16586 : : tree
16587 : 16074397 : fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16588 : : {
16589 : 16074397 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16590 : 16074397 : ptr, convert_to_ptrofftype_loc (loc, off));
16591 : : }
16592 : :
16593 : : /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16594 : : tree
16595 : 160840 : fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16596 : : {
16597 : 160840 : return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16598 : 160840 : ptr, size_int (off));
16599 : : }
16600 : :
16601 : : /* Return a pointer to a NUL-terminated string containing the sequence
16602 : : of bytes corresponding to the representation of the object referred to
16603 : : by SRC (or a subsequence of such bytes within it if SRC is a reference
16604 : : to an initialized constant array plus some constant offset).
16605 : : Set *STRSIZE the number of bytes in the constant sequence including
16606 : : the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16607 : : where A is the array that stores the constant sequence that SRC points
16608 : : to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16609 : : need not point to a string or even an array of characters but may point
16610 : : to an object of any type. */
16611 : :
16612 : : const char *
16613 : 12515182 : getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16614 : : {
16615 : : /* The offset into the array A storing the string, and A's byte size. */
16616 : 12515182 : tree offset_node;
16617 : 12515182 : tree mem_size;
16618 : :
16619 : 12515182 : if (strsize)
16620 : 4856945 : *strsize = 0;
16621 : :
16622 : 12515182 : if (strsize)
16623 : 4856945 : src = byte_representation (src, &offset_node, &mem_size, NULL);
16624 : : else
16625 : 7658237 : src = string_constant (src, &offset_node, &mem_size, NULL);
16626 : 12515182 : if (!src)
16627 : : return NULL;
16628 : :
16629 : 2810890 : unsigned HOST_WIDE_INT offset = 0;
16630 : 2810890 : if (offset_node != NULL_TREE)
16631 : : {
16632 : 2810890 : if (!tree_fits_uhwi_p (offset_node))
16633 : : return NULL;
16634 : : else
16635 : 2809126 : offset = tree_to_uhwi (offset_node);
16636 : : }
16637 : :
16638 : 2809126 : if (!tree_fits_uhwi_p (mem_size))
16639 : : return NULL;
16640 : :
16641 : : /* ARRAY_SIZE is the byte size of the array the constant sequence
16642 : : is stored in and equal to sizeof A. INIT_BYTES is the number
16643 : : of bytes in the constant sequence used to initialize the array,
16644 : : including any embedded NULs as well as the terminating NUL (for
16645 : : strings), but not including any trailing zeros/NULs past
16646 : : the terminating one appended implicitly to a string literal to
16647 : : zero out the remainder of the array it's stored in. For example,
16648 : : given:
16649 : : const char a[7] = "abc\0d";
16650 : : n = strlen (a + 1);
16651 : : ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16652 : : (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16653 : : is equal to strlen (A) + 1. */
16654 : 2809126 : const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16655 : 2809126 : unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16656 : 2809126 : const char *string = TREE_STRING_POINTER (src);
16657 : :
16658 : : /* Ideally this would turn into a gcc_checking_assert over time. */
16659 : 2809126 : if (init_bytes > array_size)
16660 : : init_bytes = array_size;
16661 : :
16662 : 2809126 : if (init_bytes == 0 || offset >= array_size)
16663 : : return NULL;
16664 : :
16665 : 2807934 : if (strsize)
16666 : : {
16667 : : /* Compute and store the number of characters from the beginning
16668 : : of the substring at OFFSET to the end, including the terminating
16669 : : nul. Offsets past the initial length refer to null strings. */
16670 : 1562643 : if (offset < init_bytes)
16671 : 1562643 : *strsize = init_bytes - offset;
16672 : : else
16673 : 0 : *strsize = 1;
16674 : : }
16675 : : else
16676 : : {
16677 : 1245291 : tree eltype = TREE_TYPE (TREE_TYPE (src));
16678 : : /* Support only properly NUL-terminated single byte strings. */
16679 : 1245291 : if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16680 : : return NULL;
16681 : 1237971 : if (string[init_bytes - 1] != '\0')
16682 : : return NULL;
16683 : : }
16684 : :
16685 : 2779412 : return offset < init_bytes ? string + offset : "";
16686 : : }
16687 : :
16688 : : /* Return a pointer to a NUL-terminated string corresponding to
16689 : : the expression STR referencing a constant string, possibly
16690 : : involving a constant offset. Return null if STR either doesn't
16691 : : reference a constant string or if it involves a nonconstant
16692 : : offset. */
16693 : :
16694 : : const char *
16695 : 7658237 : c_getstr (tree str)
16696 : : {
16697 : 7658237 : return getbyterep (str, NULL);
16698 : : }
16699 : :
16700 : : /* Given a tree T, compute which bits in T may be nonzero. */
16701 : :
16702 : : wide_int
16703 : 234223650 : tree_nonzero_bits (const_tree t)
16704 : : {
16705 : 234223650 : switch (TREE_CODE (t))
16706 : : {
16707 : 8382651 : case INTEGER_CST:
16708 : 8382651 : return wi::to_wide (t);
16709 : 133488612 : case SSA_NAME:
16710 : 133488612 : return get_nonzero_bits (t);
16711 : 241913 : case NON_LVALUE_EXPR:
16712 : 241913 : case SAVE_EXPR:
16713 : 241913 : return tree_nonzero_bits (TREE_OPERAND (t, 0));
16714 : 487335 : case BIT_AND_EXPR:
16715 : 974670 : return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16716 : 1462005 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16717 : 4235 : case BIT_IOR_EXPR:
16718 : 4235 : case BIT_XOR_EXPR:
16719 : 8470 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16720 : 12705 : tree_nonzero_bits (TREE_OPERAND (t, 1)));
16721 : 62245 : case COND_EXPR:
16722 : 124490 : return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16723 : 186735 : tree_nonzero_bits (TREE_OPERAND (t, 2)));
16724 : 48797501 : CASE_CONVERT:
16725 : 97595002 : return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16726 : 48797501 : TYPE_PRECISION (TREE_TYPE (t)),
16727 : 146392503 : TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16728 : 13146367 : case PLUS_EXPR:
16729 : 13146367 : if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16730 : : {
16731 : 13146367 : wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16732 : 13146367 : wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16733 : 13146367 : if (wi::bit_and (nzbits1, nzbits2) == 0)
16734 : 494208 : return wi::bit_or (nzbits1, nzbits2);
16735 : 13146367 : }
16736 : : break;
16737 : 150716 : case LSHIFT_EXPR:
16738 : 150716 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16739 : : {
16740 : 82766 : tree type = TREE_TYPE (t);
16741 : 82766 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16742 : 165532 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16743 : 82766 : TYPE_PRECISION (type));
16744 : 82766 : return wi::neg_p (arg1)
16745 : 165532 : ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16746 : 82766 : : wi::lshift (nzbits, arg1);
16747 : 82766 : }
16748 : : break;
16749 : 152155 : case RSHIFT_EXPR:
16750 : 152155 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16751 : : {
16752 : 150603 : tree type = TREE_TYPE (t);
16753 : 150603 : wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16754 : 301206 : wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16755 : 150603 : TYPE_PRECISION (type));
16756 : 150603 : return wi::neg_p (arg1)
16757 : 301206 : ? wi::lshift (nzbits, -arg1)
16758 : 150603 : : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16759 : 150603 : }
16760 : : break;
16761 : : default:
16762 : : break;
16763 : : }
16764 : :
16765 : 42031581 : return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16766 : : }
16767 : :
16768 : : /* Helper function for address compare simplifications in match.pd.
16769 : : OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16770 : : TYPE is the type of comparison operands.
16771 : : BASE0, BASE1, OFF0 and OFF1 are set by the function.
16772 : : GENERIC is true if GENERIC folding and false for GIMPLE folding.
16773 : : Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16774 : : 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16775 : : and 2 if unknown. */
16776 : :
16777 : : int
16778 : 1190794 : address_compare (tree_code code, tree type, tree op0, tree op1,
16779 : : tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16780 : : bool generic)
16781 : : {
16782 : 1190794 : if (TREE_CODE (op0) == SSA_NAME)
16783 : 19761 : op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16784 : 1190794 : if (TREE_CODE (op1) == SSA_NAME)
16785 : 3976 : op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16786 : 1190794 : gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16787 : 1190794 : gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16788 : 1190794 : base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16789 : 1190794 : base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16790 : 1190794 : if (base0 && TREE_CODE (base0) == MEM_REF)
16791 : : {
16792 : 18979 : off0 += mem_ref_offset (base0).force_shwi ();
16793 : 18979 : base0 = TREE_OPERAND (base0, 0);
16794 : : }
16795 : 1190794 : if (base1 && TREE_CODE (base1) == MEM_REF)
16796 : : {
16797 : 2633 : off1 += mem_ref_offset (base1).force_shwi ();
16798 : 2633 : base1 = TREE_OPERAND (base1, 0);
16799 : : }
16800 : 1190794 : if (base0 == NULL_TREE || base1 == NULL_TREE)
16801 : : return 2;
16802 : :
16803 : 1183289 : int equal = 2;
16804 : : /* Punt in GENERIC on variables with value expressions;
16805 : : the value expressions might point to fields/elements
16806 : : of other vars etc. */
16807 : 1183289 : if (generic
16808 : 1183289 : && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16809 : 1056507 : || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16810 : : return 2;
16811 : 1182726 : else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16812 : : {
16813 : 103846 : symtab_node *node0 = symtab_node::get_create (base0);
16814 : 103846 : symtab_node *node1 = symtab_node::get_create (base1);
16815 : 103846 : equal = node0->equal_address_to (node1);
16816 : : }
16817 : 1078880 : else if ((DECL_P (base0)
16818 : 202166 : || TREE_CODE (base0) == SSA_NAME
16819 : 184054 : || TREE_CODE (base0) == STRING_CST)
16820 : 1078726 : && (DECL_P (base1)
16821 : 186728 : || TREE_CODE (base1) == SSA_NAME
16822 : 184321 : || TREE_CODE (base1) == STRING_CST))
16823 : 1078704 : equal = (base0 == base1);
16824 : : /* Assume different STRING_CSTs with the same content will be
16825 : : merged. */
16826 : 1182550 : if (equal == 0
16827 : 48106 : && TREE_CODE (base0) == STRING_CST
16828 : 16885 : && TREE_CODE (base1) == STRING_CST
16829 : 16856 : && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16830 : 1182550 : && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16831 : 6012 : TREE_STRING_LENGTH (base0)) == 0)
16832 : : equal = 1;
16833 : 1178368 : if (equal == 1)
16834 : : {
16835 : 1113407 : if (code == EQ_EXPR
16836 : 1113407 : || code == NE_EXPR
16837 : : /* If the offsets are equal we can ignore overflow. */
16838 : 70960 : || known_eq (off0, off1)
16839 : 141698 : || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16840 : : /* Or if we compare using pointers to decls or strings. */
16841 : 1184256 : || (POINTER_TYPE_P (type)
16842 : 0 : && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16843 : : return 1;
16844 : : return 2;
16845 : : }
16846 : 69319 : if (equal != 0)
16847 : : return equal;
16848 : 43748 : if (code != EQ_EXPR && code != NE_EXPR)
16849 : : return 2;
16850 : :
16851 : : /* At this point we know (or assume) the two pointers point at
16852 : : different objects. */
16853 : 39615 : HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16854 : 39615 : off0.is_constant (&ioff0);
16855 : 39615 : off1.is_constant (&ioff1);
16856 : : /* Punt on non-zero offsets from functions. */
16857 : 39615 : if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16858 : 39615 : || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16859 : : return 2;
16860 : : /* Or if the bases are neither decls nor string literals. */
16861 : 39615 : if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16862 : : return 2;
16863 : 21974 : if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16864 : : return 2;
16865 : : /* For initializers, assume addresses of different functions are
16866 : : different. */
16867 : 21974 : if (folding_initializer
16868 : 567 : && TREE_CODE (base0) == FUNCTION_DECL
16869 : 14 : && TREE_CODE (base1) == FUNCTION_DECL)
16870 : : return 0;
16871 : :
16872 : : /* Compute whether one address points to the start of one
16873 : : object and another one to the end of another one. */
16874 : 21960 : poly_int64 size0 = 0, size1 = 0;
16875 : 21960 : if (TREE_CODE (base0) == STRING_CST)
16876 : : {
16877 : 12503 : if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16878 : : equal = 2;
16879 : : else
16880 : : size0 = TREE_STRING_LENGTH (base0);
16881 : : }
16882 : 9457 : else if (TREE_CODE (base0) == FUNCTION_DECL)
16883 : : size0 = 1;
16884 : : else
16885 : : {
16886 : 9384 : tree sz0 = DECL_SIZE_UNIT (base0);
16887 : 9384 : if (!tree_fits_poly_int64_p (sz0))
16888 : : equal = 2;
16889 : : else
16890 : 9384 : size0 = tree_to_poly_int64 (sz0);
16891 : : }
16892 : 21960 : if (TREE_CODE (base1) == STRING_CST)
16893 : : {
16894 : 12624 : if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16895 : : equal = 2;
16896 : : else
16897 : : size1 = TREE_STRING_LENGTH (base1);
16898 : : }
16899 : 9336 : else if (TREE_CODE (base1) == FUNCTION_DECL)
16900 : : size1 = 1;
16901 : : else
16902 : : {
16903 : 9267 : tree sz1 = DECL_SIZE_UNIT (base1);
16904 : 9267 : if (!tree_fits_poly_int64_p (sz1))
16905 : : equal = 2;
16906 : : else
16907 : 9267 : size1 = tree_to_poly_int64 (sz1);
16908 : : }
16909 : 21960 : if (equal == 0)
16910 : : {
16911 : : /* If one offset is pointing (or could be) to the beginning of one
16912 : : object and the other is pointing to one past the last byte of the
16913 : : other object, punt. */
16914 : 21948 : if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16915 : : equal = 2;
16916 : 21811 : else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16917 : : equal = 2;
16918 : : /* If both offsets are the same, there are some cases we know that are
16919 : : ok. Either if we know they aren't zero, or if we know both sizes
16920 : : are no zero. */
16921 : : if (equal == 2
16922 : 274 : && known_eq (off0, off1)
16923 : 22 : && (known_ne (off0, 0)
16924 : 22 : || (known_ne (size0, 0) && known_ne (size1, 0))))
16925 : : equal = 0;
16926 : : }
16927 : :
16928 : : /* At this point, equal is 2 if either one or both pointers are out of
16929 : : bounds of their object, or one points to start of its object and the
16930 : : other points to end of its object. This is unspecified behavior
16931 : : e.g. in C++. Otherwise equal is 0. */
16932 : 21960 : if (folding_cxx_constexpr && equal)
16933 : : return equal;
16934 : :
16935 : : /* When both pointers point to string literals, even when equal is 0,
16936 : : due to tail merging of string literals the pointers might be the same. */
16937 : 21897 : if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16938 : : {
16939 : 12480 : if (ioff0 < 0
16940 : 12480 : || ioff1 < 0
16941 : 12480 : || ioff0 > TREE_STRING_LENGTH (base0)
16942 : 24948 : || ioff1 > TREE_STRING_LENGTH (base1))
16943 : : return 2;
16944 : :
16945 : : /* If the bytes in the string literals starting at the pointers
16946 : : differ, the pointers need to be different. */
16947 : 12468 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16948 : 12468 : TREE_STRING_POINTER (base1) + ioff1,
16949 : 12468 : MIN (TREE_STRING_LENGTH (base0) - ioff0,
16950 : : TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16951 : : {
16952 : 3715 : HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16953 : 3715 : if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16954 : 3715 : TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16955 : : ioffmin) == 0)
16956 : : /* If even the bytes in the string literal before the
16957 : : pointers are the same, the string literals could be
16958 : : tail merged. */
16959 : : return 2;
16960 : : }
16961 : : return 0;
16962 : : }
16963 : :
16964 : 9417 : if (folding_cxx_constexpr)
16965 : : return 0;
16966 : :
16967 : : /* If this is a pointer comparison, ignore for now even
16968 : : valid equalities where one pointer is the offset zero
16969 : : of one object and the other to one past end of another one. */
16970 : 8970 : if (!INTEGRAL_TYPE_P (type))
16971 : : return 0;
16972 : :
16973 : : /* Assume that string literals can't be adjacent to variables
16974 : : (automatic or global). */
16975 : 299 : if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16976 : : return 0;
16977 : :
16978 : : /* Assume that automatic variables can't be adjacent to global
16979 : : variables. */
16980 : 295 : if (is_global_var (base0) != is_global_var (base1))
16981 : : return 0;
16982 : :
16983 : : return equal;
16984 : : }
16985 : :
16986 : : /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16987 : : tree
16988 : 44 : ctor_single_nonzero_element (const_tree t)
16989 : : {
16990 : 44 : unsigned HOST_WIDE_INT idx;
16991 : 44 : constructor_elt *ce;
16992 : 44 : tree elt = NULL_TREE;
16993 : :
16994 : 44 : if (TREE_CODE (t) != CONSTRUCTOR)
16995 : : return NULL_TREE;
16996 : 97 : for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16997 : 94 : if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16998 : : {
16999 : 85 : if (elt)
17000 : : return NULL_TREE;
17001 : 44 : elt = ce->value;
17002 : : }
17003 : : return elt;
17004 : : }
17005 : :
17006 : : #if CHECKING_P
17007 : :
17008 : : namespace selftest {
17009 : :
17010 : : /* Helper functions for writing tests of folding trees. */
17011 : :
17012 : : /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
17013 : :
17014 : : static void
17015 : 16 : assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
17016 : : tree constant)
17017 : : {
17018 : 16 : ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
17019 : 16 : }
17020 : :
17021 : : /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
17022 : : wrapping WRAPPED_EXPR. */
17023 : :
17024 : : static void
17025 : 12 : assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
17026 : : tree wrapped_expr)
17027 : : {
17028 : 12 : tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
17029 : 12 : ASSERT_NE (wrapped_expr, result);
17030 : 12 : ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
17031 : 12 : ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
17032 : 12 : }
17033 : :
17034 : : /* Verify that various arithmetic binary operations are folded
17035 : : correctly. */
17036 : :
17037 : : static void
17038 : 4 : test_arithmetic_folding ()
17039 : : {
17040 : 4 : tree type = integer_type_node;
17041 : 4 : tree x = create_tmp_var_raw (type, "x");
17042 : 4 : tree zero = build_zero_cst (type);
17043 : 4 : tree one = build_int_cst (type, 1);
17044 : :
17045 : : /* Addition. */
17046 : : /* 1 <-- (0 + 1) */
17047 : 4 : assert_binop_folds_to_const (zero, PLUS_EXPR, one,
17048 : : one);
17049 : 4 : assert_binop_folds_to_const (one, PLUS_EXPR, zero,
17050 : : one);
17051 : :
17052 : : /* (nonlvalue)x <-- (x + 0) */
17053 : 4 : assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
17054 : : x);
17055 : :
17056 : : /* Subtraction. */
17057 : : /* 0 <-- (x - x) */
17058 : 4 : assert_binop_folds_to_const (x, MINUS_EXPR, x,
17059 : : zero);
17060 : 4 : assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
17061 : : x);
17062 : :
17063 : : /* Multiplication. */
17064 : : /* 0 <-- (x * 0) */
17065 : 4 : assert_binop_folds_to_const (x, MULT_EXPR, zero,
17066 : : zero);
17067 : :
17068 : : /* (nonlvalue)x <-- (x * 1) */
17069 : 4 : assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
17070 : : x);
17071 : 4 : }
17072 : :
17073 : : namespace test_operand_equality {
17074 : :
17075 : : /* Verify structural equality. */
17076 : :
17077 : : /* Execute fold_vec_perm_cst unit tests. */
17078 : :
17079 : : static void
17080 : 4 : test ()
17081 : : {
17082 : 4 : tree stype = integer_type_node;
17083 : 4 : tree utype = unsigned_type_node;
17084 : 4 : tree x = create_tmp_var_raw (stype, "x");
17085 : 4 : tree y = create_tmp_var_raw (stype, "y");
17086 : 4 : tree z = create_tmp_var_raw (stype, "z");
17087 : 4 : tree four = build_int_cst (stype, 4);
17088 : 4 : tree lhs1 = fold_build2 (PLUS_EXPR, stype, x, y);
17089 : 4 : tree rhs1 = fold_convert (stype,
17090 : : fold_build2 (PLUS_EXPR, utype,
17091 : : fold_convert (utype, x),
17092 : : fold_convert (utype, y)));
17093 : :
17094 : : /* (int)((unsigned x) + (unsigned y)) == x + y. */
17095 : 4 : ASSERT_TRUE (operand_equal_p (lhs1, rhs1, OEP_ASSUME_WRAPV));
17096 : 4 : ASSERT_FALSE (operand_equal_p (lhs1, rhs1, 0));
17097 : :
17098 : : /* (int)(unsigned) x == x. */
17099 : 4 : tree lhs2 = build1 (NOP_EXPR, stype,
17100 : : build1 (NOP_EXPR, utype, x));
17101 : 4 : tree rhs2 = x;
17102 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, OEP_ASSUME_WRAPV));
17103 : 4 : ASSERT_TRUE (operand_equal_p (lhs2, rhs2, 0));
17104 : :
17105 : : /* (unsigned x) + (unsigned y) == x + y. */
17106 : 4 : tree lhs3 = lhs1;
17107 : 4 : tree rhs3 = fold_build2 (PLUS_EXPR, utype,
17108 : : fold_convert (utype, x),
17109 : : fold_convert (utype, y));
17110 : 4 : ASSERT_TRUE (operand_equal_p (lhs3, rhs3, OEP_ASSUME_WRAPV));
17111 : 4 : ASSERT_FALSE (operand_equal_p (lhs3, rhs3, 0));
17112 : :
17113 : : /* (unsigned x) / (unsigned y) == x / y. */
17114 : 4 : tree lhs4 = fold_build2 (TRUNC_DIV_EXPR, stype, x, y);;
17115 : 4 : tree rhs4 = fold_build2 (TRUNC_DIV_EXPR, utype,
17116 : : fold_convert (utype, x),
17117 : : fold_convert (utype, y));
17118 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, OEP_ASSUME_WRAPV));
17119 : 4 : ASSERT_FALSE (operand_equal_p (lhs4, rhs4, 0));
17120 : :
17121 : : /* (long x) / 4 == (long)(x / 4). */
17122 : 4 : tree lstype = long_long_integer_type_node;
17123 : 4 : tree lfour = build_int_cst (lstype, 4);
17124 : 4 : tree lhs5 = fold_build2 (TRUNC_DIV_EXPR, lstype,
17125 : : fold_build1 (VIEW_CONVERT_EXPR, lstype, x), lfour);
17126 : 4 : tree rhs5 = fold_build1 (VIEW_CONVERT_EXPR, lstype,
17127 : : fold_build2 (TRUNC_DIV_EXPR, stype, x, four));
17128 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, OEP_ASSUME_WRAPV));
17129 : 4 : ASSERT_FALSE (operand_equal_p (lhs5, rhs5, 0));
17130 : :
17131 : : /* (unsigned x) / 4 == x / 4. */
17132 : 4 : tree lhs6 = fold_build2 (TRUNC_DIV_EXPR, stype, x, four);;
17133 : 4 : tree rhs6 = fold_build2 (TRUNC_DIV_EXPR, utype,
17134 : : fold_convert (utype, x),
17135 : : fold_convert (utype, four));
17136 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, OEP_ASSUME_WRAPV));
17137 : 4 : ASSERT_FALSE (operand_equal_p (lhs6, rhs6, 0));
17138 : :
17139 : : /* a / (int)((unsigned)b - (unsigned)c)) == a / (b - c). */
17140 : 4 : tree lhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, lhs1);
17141 : 4 : tree rhs7 = fold_build2 (TRUNC_DIV_EXPR, stype, x, rhs1);
17142 : 4 : ASSERT_TRUE (operand_equal_p (lhs7, rhs7, OEP_ASSUME_WRAPV));
17143 : 4 : ASSERT_FALSE (operand_equal_p (lhs7, rhs7, 0));
17144 : :
17145 : : /* (unsigned x) + 4 == x + 4. */
17146 : 4 : tree lhs8 = fold_build2 (PLUS_EXPR, stype, x, four);
17147 : 4 : tree rhs8 = fold_build2 (PLUS_EXPR, utype,
17148 : : fold_convert (utype, x),
17149 : : fold_convert (utype, four));
17150 : 4 : ASSERT_TRUE (operand_equal_p (lhs8, rhs8, OEP_ASSUME_WRAPV));
17151 : 4 : ASSERT_FALSE (operand_equal_p (lhs8, rhs8, 0));
17152 : :
17153 : : /* (unsigned x) + 4 == 4 + x. */
17154 : 4 : tree lhs9 = fold_build2 (PLUS_EXPR, stype, four, x);
17155 : 4 : tree rhs9 = fold_build2 (PLUS_EXPR, utype,
17156 : : fold_convert (utype, x),
17157 : : fold_convert (utype, four));
17158 : 4 : ASSERT_TRUE (operand_equal_p (lhs9, rhs9, OEP_ASSUME_WRAPV));
17159 : 4 : ASSERT_FALSE (operand_equal_p (lhs9, rhs9, 0));
17160 : :
17161 : : /* ((unsigned x) + 4) * (unsigned y)) + z == ((4 + x) * y) + z. */
17162 : 4 : tree lhs10 = fold_build2 (PLUS_EXPR, stype,
17163 : : fold_build2 (MULT_EXPR, stype,
17164 : : fold_build2 (PLUS_EXPR, stype, four, x),
17165 : : y),
17166 : : z);
17167 : 4 : tree rhs10 = fold_build2 (MULT_EXPR, utype,
17168 : : fold_build2 (PLUS_EXPR, utype,
17169 : : fold_convert (utype, x),
17170 : : fold_convert (utype, four)),
17171 : : fold_convert (utype, y));
17172 : 4 : rhs10 = fold_build2 (PLUS_EXPR, stype, fold_convert (stype, rhs10), z);
17173 : 4 : ASSERT_TRUE (operand_equal_p (lhs10, rhs10, OEP_ASSUME_WRAPV));
17174 : 4 : ASSERT_FALSE (operand_equal_p (lhs10, rhs10, 0));
17175 : 4 : }
17176 : : }
17177 : :
17178 : : namespace test_fold_vec_perm_cst {
17179 : :
17180 : : /* Build a VECTOR_CST corresponding to VMODE, and has
17181 : : encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP.
17182 : : Fill it with randomized elements, using rand() % THRESHOLD. */
17183 : :
17184 : : static tree
17185 : 0 : build_vec_cst_rand (machine_mode vmode, unsigned npatterns,
17186 : : unsigned nelts_per_pattern,
17187 : : int step = 0, bool natural_stepped = false,
17188 : : int threshold = 100)
17189 : : {
17190 : 0 : tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1);
17191 : 0 : tree vectype = build_vector_type_for_mode (inner_type, vmode);
17192 : 0 : tree_vector_builder builder (vectype, npatterns, nelts_per_pattern);
17193 : :
17194 : : // Fill a0 for each pattern
17195 : 0 : for (unsigned i = 0; i < npatterns; i++)
17196 : 0 : builder.quick_push (build_int_cst (inner_type, rand () % threshold));
17197 : :
17198 : 0 : if (nelts_per_pattern == 1)
17199 : 0 : return builder.build ();
17200 : :
17201 : : // Fill a1 for each pattern
17202 : 0 : for (unsigned i = 0; i < npatterns; i++)
17203 : : {
17204 : 0 : tree a1;
17205 : 0 : if (natural_stepped)
17206 : : {
17207 : 0 : tree a0 = builder[i];
17208 : 0 : wide_int a0_val = wi::to_wide (a0);
17209 : 0 : wide_int a1_val = a0_val + step;
17210 : 0 : a1 = wide_int_to_tree (inner_type, a1_val);
17211 : 0 : }
17212 : : else
17213 : 0 : a1 = build_int_cst (inner_type, rand () % threshold);
17214 : 0 : builder.quick_push (a1);
17215 : : }
17216 : 0 : if (nelts_per_pattern == 2)
17217 : 0 : return builder.build ();
17218 : :
17219 : 0 : for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++)
17220 : : {
17221 : 0 : tree prev_elem = builder[i - npatterns];
17222 : 0 : wide_int prev_elem_val = wi::to_wide (prev_elem);
17223 : 0 : wide_int val = prev_elem_val + step;
17224 : 0 : builder.quick_push (wide_int_to_tree (inner_type, val));
17225 : 0 : }
17226 : :
17227 : 0 : return builder.build ();
17228 : 0 : }
17229 : :
17230 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17231 : : when result is VLA. */
17232 : :
17233 : : static void
17234 : 0 : validate_res (unsigned npatterns, unsigned nelts_per_pattern,
17235 : : tree res, tree *expected_res)
17236 : : {
17237 : : /* Actual npatterns and encoded_elts in res may be less than expected due
17238 : : to canonicalization. */
17239 : 0 : ASSERT_TRUE (res != NULL_TREE);
17240 : 0 : ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns);
17241 : 0 : ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern);
17242 : :
17243 : 0 : for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++)
17244 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17245 : 0 : }
17246 : :
17247 : : /* Validate result of VEC_PERM_EXPR folding for the unit-tests below,
17248 : : when the result is VLS. */
17249 : :
17250 : : static void
17251 : 0 : validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts)
17252 : : {
17253 : 0 : ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts));
17254 : 0 : for (unsigned i = 0; i < expected_nelts; i++)
17255 : 0 : ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0));
17256 : 0 : }
17257 : :
17258 : : /* Helper routine to push multiple elements into BUILDER. */
17259 : : template<unsigned N>
17260 : 0 : static void builder_push_elems (vec_perm_builder& builder,
17261 : : poly_uint64 (&elems)[N])
17262 : : {
17263 : 0 : for (unsigned i = 0; i < N; i++)
17264 : 0 : builder.quick_push (elems[i]);
17265 : 0 : }
17266 : :
17267 : : #define ARG0(index) vector_cst_elt (arg0, index)
17268 : : #define ARG1(index) vector_cst_elt (arg1, index)
17269 : :
17270 : : /* Test cases where result is VNx4SI and input vectors are V4SI. */
17271 : :
17272 : : static void
17273 : 0 : test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode)
17274 : : {
17275 : 0 : for (int i = 0; i < 10; i++)
17276 : : {
17277 : : /* Case 1:
17278 : : sel = { 0, 4, 1, 5, ... }
17279 : : res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */
17280 : 0 : {
17281 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17282 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17283 : :
17284 : 0 : tree inner_type
17285 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17286 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17287 : :
17288 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17289 : 0 : vec_perm_builder builder (res_len, 4, 1);
17290 : 0 : poly_uint64 mask_elems[] = { 0, 4, 1, 5 };
17291 : 0 : builder_push_elems (builder, mask_elems);
17292 : :
17293 : 0 : vec_perm_indices sel (builder, 2, res_len);
17294 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17295 : :
17296 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17297 : 0 : validate_res (4, 1, res, expected_res);
17298 : 0 : }
17299 : :
17300 : : /* Case 2: Same as case 1, but contains an out of bounds access which
17301 : : should wrap around.
17302 : : sel = {0, 8, 4, 12, ...} (4, 1)
17303 : : res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */
17304 : 0 : {
17305 : 0 : tree arg0 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17306 : 0 : tree arg1 = build_vec_cst_rand (v4si_mode, 4, 1, 0);
17307 : :
17308 : 0 : tree inner_type
17309 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1);
17310 : 0 : tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode);
17311 : :
17312 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17313 : 0 : vec_perm_builder builder (res_len, 4, 1);
17314 : 0 : poly_uint64 mask_elems[] = { 0, 8, 4, 12 };
17315 : 0 : builder_push_elems (builder, mask_elems);
17316 : :
17317 : 0 : vec_perm_indices sel (builder, 2, res_len);
17318 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17319 : :
17320 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) };
17321 : 0 : validate_res (4, 1, res, expected_res);
17322 : 0 : }
17323 : : }
17324 : 0 : }
17325 : :
17326 : : /* Test cases where result is V4SI and input vectors are VNx4SI. */
17327 : :
17328 : : static void
17329 : 0 : test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode)
17330 : : {
17331 : 0 : for (int i = 0; i < 10; i++)
17332 : : {
17333 : : /* Case 1:
17334 : : sel = { 0, 1, 2, 3}
17335 : : res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */
17336 : 0 : {
17337 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17338 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17339 : :
17340 : 0 : tree inner_type
17341 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17342 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17343 : :
17344 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17345 : 0 : vec_perm_builder builder (res_len, 4, 1);
17346 : 0 : poly_uint64 mask_elems[] = {0, 1, 2, 3};
17347 : 0 : builder_push_elems (builder, mask_elems);
17348 : :
17349 : 0 : vec_perm_indices sel (builder, 2, res_len);
17350 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel);
17351 : :
17352 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) };
17353 : 0 : validate_res_vls (res, expected_res, 4);
17354 : 0 : }
17355 : :
17356 : : /* Case 2: Same as Case 1, but crossing input vector.
17357 : : sel = {0, 2, 4, 6}
17358 : : In this case,the index 4 is ambiguous since len = 4 + 4x.
17359 : : Since we cannot determine, which vector to choose from during
17360 : : compile time, should return NULL_TREE. */
17361 : 0 : {
17362 : 0 : tree arg0 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17363 : 0 : tree arg1 = build_vec_cst_rand (vnx4si_mode, 4, 1);
17364 : :
17365 : 0 : tree inner_type
17366 : 0 : = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1);
17367 : 0 : tree res_type = build_vector_type_for_mode (inner_type, v4si_mode);
17368 : :
17369 : 0 : poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (res_type);
17370 : 0 : vec_perm_builder builder (res_len, 4, 1);
17371 : 0 : poly_uint64 mask_elems[] = {0, 2, 4, 6};
17372 : 0 : builder_push_elems (builder, mask_elems);
17373 : :
17374 : 0 : vec_perm_indices sel (builder, 2, res_len);
17375 : 0 : const char *reason;
17376 : 0 : tree res = fold_vec_perm_cst (res_type, arg0, arg1, sel, &reason);
17377 : :
17378 : 0 : ASSERT_TRUE (res == NULL_TREE);
17379 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17380 : 0 : }
17381 : : }
17382 : 0 : }
17383 : :
17384 : : /* Test all input vectors. */
17385 : :
17386 : : static void
17387 : 0 : test_all_nunits (machine_mode vmode)
17388 : : {
17389 : : /* Test with 10 different inputs. */
17390 : 0 : for (int i = 0; i < 10; i++)
17391 : : {
17392 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17393 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17394 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17395 : :
17396 : : /* Case 1: mask = {0, ...} // (1, 1)
17397 : : res = { arg0[0], ... } // (1, 1) */
17398 : 0 : {
17399 : 0 : vec_perm_builder builder (len, 1, 1);
17400 : 0 : builder.quick_push (0);
17401 : 0 : vec_perm_indices sel (builder, 2, len);
17402 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17403 : 0 : tree expected_res[] = { ARG0(0) };
17404 : 0 : validate_res (1, 1, res, expected_res);
17405 : 0 : }
17406 : :
17407 : : /* Case 2: mask = {len, ...} // (1, 1)
17408 : : res = { arg1[0], ... } // (1, 1) */
17409 : 0 : {
17410 : 0 : vec_perm_builder builder (len, 1, 1);
17411 : 0 : builder.quick_push (len);
17412 : 0 : vec_perm_indices sel (builder, 2, len);
17413 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17414 : :
17415 : 0 : tree expected_res[] = { ARG1(0) };
17416 : 0 : validate_res (1, 1, res, expected_res);
17417 : 0 : }
17418 : : }
17419 : 0 : }
17420 : :
17421 : : /* Test all vectors which contain at-least 2 elements. */
17422 : :
17423 : : static void
17424 : 0 : test_nunits_min_2 (machine_mode vmode)
17425 : : {
17426 : 0 : for (int i = 0; i < 10; i++)
17427 : : {
17428 : : /* Case 1: mask = { 0, len, ... } // (2, 1)
17429 : : res = { arg0[0], arg1[0], ... } // (2, 1) */
17430 : 0 : {
17431 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17432 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17433 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17434 : :
17435 : 0 : vec_perm_builder builder (len, 2, 1);
17436 : 0 : poly_uint64 mask_elems[] = { 0, len };
17437 : 0 : builder_push_elems (builder, mask_elems);
17438 : :
17439 : 0 : vec_perm_indices sel (builder, 2, len);
17440 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17441 : :
17442 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17443 : 0 : validate_res (2, 1, res, expected_res);
17444 : 0 : }
17445 : :
17446 : : /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2)
17447 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */
17448 : 0 : {
17449 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17450 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17451 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17452 : :
17453 : 0 : vec_perm_builder builder (len, 2, 2);
17454 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17455 : 0 : builder_push_elems (builder, mask_elems);
17456 : :
17457 : 0 : vec_perm_indices sel (builder, 2, len);
17458 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17459 : :
17460 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17461 : 0 : validate_res (2, 2, res, expected_res);
17462 : 0 : }
17463 : :
17464 : : /* Case 4: mask = {0, 0, 1, ...} // (1, 3)
17465 : : Test that the stepped sequence of the pattern selects from
17466 : : same input pattern. Since input vectors have npatterns = 2,
17467 : : and step (a2 - a1) = 1, step is not a multiple of npatterns
17468 : : in input vector. So return NULL_TREE. */
17469 : 0 : {
17470 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 1, true);
17471 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 1);
17472 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17473 : :
17474 : 0 : vec_perm_builder builder (len, 1, 3);
17475 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1 };
17476 : 0 : builder_push_elems (builder, mask_elems);
17477 : :
17478 : 0 : vec_perm_indices sel (builder, 2, len);
17479 : 0 : const char *reason;
17480 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel,
17481 : : &reason);
17482 : 0 : ASSERT_TRUE (res == NULL_TREE);
17483 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17484 : 0 : }
17485 : :
17486 : : /* Case 5: mask = {len, 0, 1, ...} // (1, 3)
17487 : : Test that stepped sequence of the pattern selects from arg0.
17488 : : res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */
17489 : 0 : {
17490 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17491 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17492 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17493 : :
17494 : 0 : vec_perm_builder builder (len, 1, 3);
17495 : 0 : poly_uint64 mask_elems[] = { len, 0, 1 };
17496 : 0 : builder_push_elems (builder, mask_elems);
17497 : :
17498 : 0 : vec_perm_indices sel (builder, 2, len);
17499 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17500 : :
17501 : 0 : tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) };
17502 : 0 : validate_res (1, 3, res, expected_res);
17503 : 0 : }
17504 : :
17505 : : /* Case 6: PR111648 - a1 chooses base element from input vector arg.
17506 : : In this case ensure that arg has a natural stepped sequence
17507 : : to preserve arg's encoding.
17508 : :
17509 : : As a concrete example, consider:
17510 : : arg0: { -16, -9, -10, ... } // (1, 3)
17511 : : arg1: { -12, -5, -6, ... } // (1, 3)
17512 : : sel = { 0, len, len + 1, ... } // (1, 3)
17513 : :
17514 : : This will create res with following encoding:
17515 : : res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3)
17516 : : = { -16, -12, -5, ... }
17517 : :
17518 : : The step in above encoding would be: (-5) - (-12) = 7
17519 : : And hence res[3] would be computed as -5 + 7 = 2.
17520 : : instead of arg1[2], ie, -6.
17521 : : Ensure that valid_mask_for_fold_vec_perm_cst returns false
17522 : : for this case. */
17523 : 0 : {
17524 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17525 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17526 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17527 : :
17528 : 0 : vec_perm_builder builder (len, 1, 3);
17529 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17530 : 0 : builder_push_elems (builder, mask_elems);
17531 : :
17532 : 0 : vec_perm_indices sel (builder, 2, len);
17533 : 0 : const char *reason;
17534 : : /* FIXME: It may happen that build_vec_cst_rand may build a natural
17535 : : stepped pattern, even if we didn't explicitly tell it to. So folding
17536 : : may not always fail, but if it does, ensure that's because arg1 does
17537 : : not have a natural stepped sequence (and not due to other reason) */
17538 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17539 : 0 : if (res == NULL_TREE)
17540 : 0 : ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence"));
17541 : 0 : }
17542 : :
17543 : : /* Case 7: Same as Case 6, except that arg1 contains natural stepped
17544 : : sequence and thus folding should be valid for this case. */
17545 : 0 : {
17546 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17547 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1, true);
17548 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17549 : :
17550 : 0 : vec_perm_builder builder (len, 1, 3);
17551 : 0 : poly_uint64 mask_elems[] = { 0, len, len+1 };
17552 : 0 : builder_push_elems (builder, mask_elems);
17553 : :
17554 : 0 : vec_perm_indices sel (builder, 2, len);
17555 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17556 : :
17557 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) };
17558 : 0 : validate_res (1, 3, res, expected_res);
17559 : 0 : }
17560 : :
17561 : : /* Case 8: Same as aarch64/sve/slp_3.c:
17562 : : arg0, arg1 are dup vectors.
17563 : : sel = { 0, len, 1, len+1, 2, len+2, ... } // (2, 3)
17564 : : So res = { arg0[0], arg1[0], ... } // (2, 1)
17565 : :
17566 : : In this case, since the input vectors are dup, only the first two
17567 : : elements per pattern in sel are considered significant. */
17568 : 0 : {
17569 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17570 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 1);
17571 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17572 : :
17573 : 0 : vec_perm_builder builder (len, 2, 3);
17574 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17575 : 0 : builder_push_elems (builder, mask_elems);
17576 : :
17577 : 0 : vec_perm_indices sel (builder, 2, len);
17578 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17579 : :
17580 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17581 : 0 : validate_res (2, 1, res, expected_res);
17582 : 0 : }
17583 : : }
17584 : 0 : }
17585 : :
17586 : : /* Test all vectors which contain at-least 4 elements. */
17587 : :
17588 : : static void
17589 : 0 : test_nunits_min_4 (machine_mode vmode)
17590 : : {
17591 : 0 : for (int i = 0; i < 10; i++)
17592 : : {
17593 : : /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1)
17594 : : res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */
17595 : 0 : {
17596 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17597 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17598 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17599 : :
17600 : 0 : vec_perm_builder builder (len, 4, 1);
17601 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17602 : 0 : builder_push_elems (builder, mask_elems);
17603 : :
17604 : 0 : vec_perm_indices sel (builder, 2, len);
17605 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17606 : :
17607 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17608 : 0 : validate_res (4, 1, res, expected_res);
17609 : 0 : }
17610 : :
17611 : : /* Case 2: sel = {0, 1, 2, ...} // (1, 3)
17612 : : res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */
17613 : 0 : {
17614 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17615 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17616 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17617 : :
17618 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17619 : 0 : poly_uint64 mask_elems[] = {0, 1, 2};
17620 : 0 : builder_push_elems (builder, mask_elems);
17621 : :
17622 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17623 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17624 : 0 : tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) };
17625 : 0 : validate_res (1, 3, res, expected_res);
17626 : 0 : }
17627 : :
17628 : : /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3)
17629 : : res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */
17630 : 0 : {
17631 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17632 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17633 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17634 : :
17635 : 0 : vec_perm_builder builder (len, 1, 3);
17636 : 0 : poly_uint64 mask_elems[] = {len, len + 1, len + 2};
17637 : 0 : builder_push_elems (builder, mask_elems);
17638 : :
17639 : 0 : vec_perm_indices sel (builder, 2, len);
17640 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17641 : 0 : tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) };
17642 : 0 : validate_res (1, 3, res, expected_res);
17643 : 0 : }
17644 : :
17645 : : /* Case 4:
17646 : : sel = { len, 0, 2, ... } // (1, 3)
17647 : : This should return NULL because we cross the input vectors.
17648 : : Because,
17649 : : Let's assume len = C + Cx
17650 : : a1 = 0
17651 : : S = 2
17652 : : esel = arg0_len / sel_npatterns = C + Cx
17653 : : ae = 0 + (esel - 2) * S
17654 : : = 0 + (C + Cx - 2) * 2
17655 : : = 2(C-2) + 2Cx
17656 : :
17657 : : For C >= 4:
17658 : : Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0
17659 : : Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1
17660 : : Since q1 != qe, we cross input vectors.
17661 : : So return NULL_TREE. */
17662 : 0 : {
17663 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 2);
17664 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 2);
17665 : 0 : poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17666 : :
17667 : 0 : vec_perm_builder builder (arg0_len, 1, 3);
17668 : 0 : poly_uint64 mask_elems[] = { arg0_len, 0, 2 };
17669 : 0 : builder_push_elems (builder, mask_elems);
17670 : :
17671 : 0 : vec_perm_indices sel (builder, 2, arg0_len);
17672 : 0 : const char *reason;
17673 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17674 : 0 : ASSERT_TRUE (res == NULL_TREE);
17675 : 0 : ASSERT_TRUE (!strcmp (reason, "crossed input vectors"));
17676 : 0 : }
17677 : :
17678 : : /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2
17679 : : mask = { 0, len, 1, len + 1, ...} // (2, 2)
17680 : : res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2)
17681 : :
17682 : : Note that fold_vec_perm_cst will set
17683 : : res_npatterns = max(4, max(4, 2)) = 4
17684 : : However after canonicalizing, we will end up with shape (2, 2). */
17685 : 0 : {
17686 : 0 : tree arg0 = build_vec_cst_rand (vmode, 4, 1);
17687 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17688 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17689 : :
17690 : 0 : vec_perm_builder builder (len, 2, 2);
17691 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1 };
17692 : 0 : builder_push_elems (builder, mask_elems);
17693 : :
17694 : 0 : vec_perm_indices sel (builder, 2, len);
17695 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17696 : 0 : tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) };
17697 : 0 : validate_res (2, 2, res, expected_res);
17698 : 0 : }
17699 : :
17700 : : /* Case 6: Test combination in sel, where one pattern is dup and other
17701 : : is stepped sequence.
17702 : : sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3)
17703 : : res = { arg0[0], arg0[0], arg0[0],
17704 : : arg0[1], arg0[0], arg0[2], ... } // (2, 3) */
17705 : 0 : {
17706 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17707 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17708 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17709 : :
17710 : 0 : vec_perm_builder builder (len, 2, 3);
17711 : 0 : poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 };
17712 : 0 : builder_push_elems (builder, mask_elems);
17713 : :
17714 : 0 : vec_perm_indices sel (builder, 2, len);
17715 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17716 : :
17717 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0),
17718 : 0 : ARG0(1), ARG0(0), ARG0(2) };
17719 : 0 : validate_res (2, 3, res, expected_res);
17720 : 0 : }
17721 : :
17722 : : /* Case 7: PR111048: Check that we set arg_npatterns correctly,
17723 : : when arg0, arg1 and sel have different number of patterns.
17724 : : arg0 is of shape (1, 1)
17725 : : arg1 is of shape (4, 1)
17726 : : sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...}
17727 : :
17728 : : In this case the pattern: {len, len+1, len+2, ...} chooses arg1.
17729 : : However,
17730 : : step = (len+2) - (len+1) = 1
17731 : : arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4
17732 : : Since step is not a multiple of arg_npatterns,
17733 : : valid_mask_for_fold_vec_perm_cst should return false,
17734 : : and thus fold_vec_perm_cst should return NULL_TREE. */
17735 : 0 : {
17736 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 1);
17737 : 0 : tree arg1 = build_vec_cst_rand (vmode, 4, 1);
17738 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17739 : :
17740 : 0 : vec_perm_builder builder (len, 2, 3);
17741 : 0 : poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 };
17742 : 0 : builder_push_elems (builder, mask_elems);
17743 : :
17744 : 0 : vec_perm_indices sel (builder, 2, len);
17745 : 0 : const char *reason;
17746 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17747 : :
17748 : 0 : ASSERT_TRUE (res == NULL_TREE);
17749 : 0 : ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns"));
17750 : 0 : }
17751 : :
17752 : : /* Case 8: PR111754: When input vector is not a stepped sequence,
17753 : : check that the result is not a stepped sequence either, even
17754 : : if sel has a stepped sequence. */
17755 : 0 : {
17756 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 2);
17757 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17758 : :
17759 : 0 : vec_perm_builder builder (len, 1, 3);
17760 : 0 : poly_uint64 mask_elems[] = { 0, 1, 2 };
17761 : 0 : builder_push_elems (builder, mask_elems);
17762 : :
17763 : 0 : vec_perm_indices sel (builder, 1, len);
17764 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg0, sel);
17765 : :
17766 : 0 : tree expected_res[] = { ARG0(0), ARG0(1) };
17767 : 0 : validate_res (sel.encoding ().npatterns (), 2, res, expected_res);
17768 : 0 : }
17769 : :
17770 : : /* Case 9: If sel doesn't contain a stepped sequence,
17771 : : check that the result has same encoding as sel, irrespective
17772 : : of shape of input vectors. */
17773 : 0 : {
17774 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17775 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17776 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17777 : :
17778 : 0 : vec_perm_builder builder (len, 1, 2);
17779 : 0 : poly_uint64 mask_elems[] = { 0, len };
17780 : 0 : builder_push_elems (builder, mask_elems);
17781 : :
17782 : 0 : vec_perm_indices sel (builder, 2, len);
17783 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17784 : :
17785 : 0 : tree expected_res[] = { ARG0(0), ARG1(0) };
17786 : 0 : validate_res (sel.encoding ().npatterns (),
17787 : 0 : sel.encoding ().nelts_per_pattern (), res, expected_res);
17788 : 0 : }
17789 : : }
17790 : 0 : }
17791 : :
17792 : : /* Test all vectors which contain at-least 8 elements. */
17793 : :
17794 : : static void
17795 : 0 : test_nunits_min_8 (machine_mode vmode)
17796 : : {
17797 : 0 : for (int i = 0; i < 10; i++)
17798 : : {
17799 : : /* Case 1: sel_npatterns (4) > input npatterns (2)
17800 : : sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3)
17801 : : res: { arg0[0], arg0[0], arg0[0], arg1[0],
17802 : : arg0[2], arg0[0], arg0[3], arg1[0],
17803 : : arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */
17804 : 0 : {
17805 : 0 : tree arg0 = build_vec_cst_rand (vmode, 2, 3, 2);
17806 : 0 : tree arg1 = build_vec_cst_rand (vmode, 2, 3, 2);
17807 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17808 : :
17809 : 0 : vec_perm_builder builder(len, 4, 3);
17810 : 0 : poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len,
17811 : 0 : 4, 0, 5, len };
17812 : 0 : builder_push_elems (builder, mask_elems);
17813 : :
17814 : 0 : vec_perm_indices sel (builder, 2, len);
17815 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel);
17816 : :
17817 : 0 : tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0),
17818 : 0 : ARG0(2), ARG0(0), ARG0(3), ARG1(0),
17819 : 0 : ARG0(4), ARG0(0), ARG0(5), ARG1(0) };
17820 : 0 : validate_res (4, 3, res, expected_res);
17821 : 0 : }
17822 : : }
17823 : 0 : }
17824 : :
17825 : : /* Test vectors for which nunits[0] <= 4. */
17826 : :
17827 : : static void
17828 : 0 : test_nunits_max_4 (machine_mode vmode)
17829 : : {
17830 : : /* Case 1: mask = {0, 4, ...} // (1, 2)
17831 : : This should return NULL_TREE because the index 4 may choose
17832 : : from either arg0 or arg1 depending on vector length. */
17833 : 0 : {
17834 : 0 : tree arg0 = build_vec_cst_rand (vmode, 1, 3, 1);
17835 : 0 : tree arg1 = build_vec_cst_rand (vmode, 1, 3, 1);
17836 : 0 : poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
17837 : :
17838 : 0 : vec_perm_builder builder (len, 1, 2);
17839 : 0 : poly_uint64 mask_elems[] = {0, 4};
17840 : 0 : builder_push_elems (builder, mask_elems);
17841 : :
17842 : 0 : vec_perm_indices sel (builder, 2, len);
17843 : 0 : const char *reason;
17844 : 0 : tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, &reason);
17845 : 0 : ASSERT_TRUE (res == NULL_TREE);
17846 : 0 : ASSERT_TRUE (reason != NULL);
17847 : 0 : ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len"));
17848 : 0 : }
17849 : 0 : }
17850 : :
17851 : : #undef ARG0
17852 : : #undef ARG1
17853 : :
17854 : : /* Return true if SIZE is of the form C + Cx and C is power of 2. */
17855 : :
17856 : : static bool
17857 : 0 : is_simple_vla_size (poly_uint64 size)
17858 : : {
17859 : 128 : if (size.is_constant ()
17860 : : || !pow2p_hwi (size.coeffs[0]))
17861 : 0 : return false;
17862 : : for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i)
17863 : : if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0))
17864 : : return false;
17865 : : return true;
17866 : : }
17867 : :
17868 : : /* Execute fold_vec_perm_cst unit tests. */
17869 : :
17870 : : static void
17871 : 4 : test ()
17872 : : {
17873 : 4 : machine_mode vnx4si_mode = E_VOIDmode;
17874 : 4 : machine_mode v4si_mode = E_VOIDmode;
17875 : :
17876 : 4 : machine_mode vmode;
17877 : 132 : FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT)
17878 : : {
17879 : : /* Obtain modes corresponding to VNx4SI and V4SI,
17880 : : to call mixed mode tests below.
17881 : : FIXME: Is there a better way to do this ? */
17882 : 128 : if (GET_MODE_INNER (vmode) == SImode)
17883 : : {
17884 : 128 : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17885 : 128 : if (is_simple_vla_size (nunits)
17886 : : && nunits.coeffs[0] == 4)
17887 : : vnx4si_mode = vmode;
17888 : 128 : else if (known_eq (nunits, poly_uint64 (4)))
17889 : 128 : v4si_mode = vmode;
17890 : : }
17891 : :
17892 : 128 : if (!is_simple_vla_size (GET_MODE_NUNITS (vmode))
17893 : : || !targetm.vector_mode_supported_p (vmode))
17894 : 128 : continue;
17895 : :
17896 : : poly_uint64 nunits = GET_MODE_NUNITS (vmode);
17897 : : test_all_nunits (vmode);
17898 : : if (nunits.coeffs[0] >= 2)
17899 : : test_nunits_min_2 (vmode);
17900 : : if (nunits.coeffs[0] >= 4)
17901 : : test_nunits_min_4 (vmode);
17902 : : if (nunits.coeffs[0] >= 8)
17903 : : test_nunits_min_8 (vmode);
17904 : :
17905 : : if (nunits.coeffs[0] <= 4)
17906 : : test_nunits_max_4 (vmode);
17907 : : }
17908 : :
17909 : 4 : if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode
17910 : : && targetm.vector_mode_supported_p (vnx4si_mode)
17911 : : && targetm.vector_mode_supported_p (v4si_mode))
17912 : : {
17913 : : test_vnx4si_v4si (vnx4si_mode, v4si_mode);
17914 : : test_v4si_vnx4si (v4si_mode, vnx4si_mode);
17915 : : }
17916 : 4 : }
17917 : : } // end of test_fold_vec_perm_cst namespace
17918 : :
17919 : : /* Verify that various binary operations on vectors are folded
17920 : : correctly. */
17921 : :
17922 : : static void
17923 : 4 : test_vector_folding ()
17924 : : {
17925 : 4 : tree inner_type = integer_type_node;
17926 : 4 : tree type = build_vector_type (inner_type, 4);
17927 : 4 : tree zero = build_zero_cst (type);
17928 : 4 : tree one = build_one_cst (type);
17929 : 4 : tree index = build_index_vector (type, 0, 1);
17930 : :
17931 : : /* Verify equality tests that return a scalar boolean result. */
17932 : 4 : tree res_type = boolean_type_node;
17933 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
17934 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
17935 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
17936 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
17937 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
17938 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17939 : : index, one)));
17940 : 4 : ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
17941 : : index, index)));
17942 : 4 : ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
17943 : : index, index)));
17944 : 4 : }
17945 : :
17946 : : /* Verify folding of VEC_DUPLICATE_EXPRs. */
17947 : :
17948 : : static void
17949 : 4 : test_vec_duplicate_folding ()
17950 : : {
17951 : 4 : scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
17952 : 4 : machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
17953 : : /* This will be 1 if VEC_MODE isn't a vector mode. */
17954 : 8 : poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
17955 : :
17956 : 4 : tree type = build_vector_type (ssizetype, nunits);
17957 : 4 : tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
17958 : 4 : tree dup5_cst = build_vector_from_val (type, ssize_int (5));
17959 : 4 : ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
17960 : 4 : }
17961 : :
17962 : : /* Run all of the selftests within this file. */
17963 : :
17964 : : void
17965 : 4 : fold_const_cc_tests ()
17966 : : {
17967 : 4 : test_arithmetic_folding ();
17968 : 4 : test_vector_folding ();
17969 : 4 : test_vec_duplicate_folding ();
17970 : 4 : test_fold_vec_perm_cst::test ();
17971 : 4 : test_operand_equality::test ();
17972 : 4 : }
17973 : :
17974 : : } // namespace selftest
17975 : :
17976 : : #endif /* CHECKING_P */
|