Branch data Line data Source code
1 : : /* Fold a constant sub-tree into a single node for C-compiler
2 : : Copyright (C) 1987-2024 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 unextend (tree, int, int, tree);
141 : : static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
142 : : static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
143 : : static tree fold_binary_op_with_conditional_arg (location_t,
144 : : enum tree_code, tree,
145 : : tree, tree,
146 : : tree, tree, int);
147 : : static tree fold_negate_const (tree, tree);
148 : : static tree fold_not_const (const_tree, tree);
149 : : static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 : : static tree fold_convert_const (enum tree_code, tree, tree);
151 : : static tree fold_view_convert_expr (tree, tree);
152 : : static tree fold_negate_expr (location_t, tree);
153 : :
154 : : /* This is a helper function to detect min/max for some operands of COND_EXPR.
155 : : The form is "(EXP0 CMP EXP1) ? EXP2 : EXP3". */
156 : : tree_code
157 : 132895 : minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3)
158 : : {
159 : 132895 : enum tree_code code = ERROR_MARK;
160 : :
161 : 132895 : if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0))
162 : 11 : return ERROR_MARK;
163 : :
164 : 132884 : if (!operand_equal_p (exp0, exp2))
165 : : return ERROR_MARK;
166 : :
167 : 132884 : if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST)
168 : : {
169 : 130230 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) - 1))
170 : : {
171 : : /* X <= Y - 1 equals to X < Y. */
172 : 76440 : if (cmp == LE_EXPR)
173 : : code = LT_EXPR;
174 : : /* X > Y - 1 equals to X >= Y. */
175 : 76097 : if (cmp == GT_EXPR)
176 : : code = GE_EXPR;
177 : : /* a != MIN_RANGE<a> ? a : MIN_RANGE<a>+1 -> MAX_EXPR<MIN_RANGE<a>+1, a> */
178 : 67255 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
179 : : {
180 : 16452 : int_range_max r;
181 : 32904 : get_range_query (cfun)->range_of_expr (r, exp0);
182 : 16452 : if (r.undefined_p ())
183 : 1 : r.set_varying (TREE_TYPE (exp0));
184 : :
185 : 16452 : widest_int min = widest_int::from (r.lower_bound (),
186 : 32904 : TYPE_SIGN (TREE_TYPE (exp0)));
187 : 16452 : if (min == wi::to_widest (exp1))
188 : 744 : code = MAX_EXPR;
189 : 16452 : }
190 : : }
191 : 130230 : if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1))
192 : : {
193 : : /* X < Y + 1 equals to X <= Y. */
194 : 970 : if (cmp == LT_EXPR)
195 : : code = LE_EXPR;
196 : : /* X >= Y + 1 equals to X > Y. */
197 : 942 : if (cmp == GE_EXPR)
198 : : code = GT_EXPR;
199 : : /* a != MAX_RANGE<a> ? a : MAX_RANGE<a>-1 -> MIN_EXPR<MIN_RANGE<a>-1, a> */
200 : 847 : if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME)
201 : : {
202 : 481 : int_range_max r;
203 : 962 : get_range_query (cfun)->range_of_expr (r, exp0);
204 : 481 : if (r.undefined_p ())
205 : 0 : r.set_varying (TREE_TYPE (exp0));
206 : :
207 : 481 : widest_int max = widest_int::from (r.upper_bound (),
208 : 962 : TYPE_SIGN (TREE_TYPE (exp0)));
209 : 481 : if (max == wi::to_widest (exp1))
210 : 43 : code = MIN_EXPR;
211 : 481 : }
212 : : }
213 : : }
214 : 130230 : if (code != ERROR_MARK
215 : 132884 : || operand_equal_p (exp1, exp3))
216 : : {
217 : 23399 : if (cmp == LT_EXPR || cmp == LE_EXPR)
218 : 2362 : code = MIN_EXPR;
219 : 23399 : if (cmp == GT_EXPR || cmp == GE_EXPR)
220 : 20139 : code = MAX_EXPR;
221 : : }
222 : : return code;
223 : : }
224 : :
225 : : /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
226 : : Otherwise, return LOC. */
227 : :
228 : : static location_t
229 : 2105598 : expr_location_or (tree t, location_t loc)
230 : : {
231 : 585141 : location_t tloc = EXPR_LOCATION (t);
232 : 2091943 : return tloc == UNKNOWN_LOCATION ? loc : tloc;
233 : : }
234 : :
235 : : /* Similar to protected_set_expr_location, but never modify x in place,
236 : : if location can and needs to be set, unshare it. */
237 : :
238 : : tree
239 : 3622284 : protected_set_expr_location_unshare (tree x, location_t loc)
240 : : {
241 : 3622284 : if (CAN_HAVE_LOCATION_P (x)
242 : 3204582 : && EXPR_LOCATION (x) != loc
243 : 1539312 : && !(TREE_CODE (x) == SAVE_EXPR
244 : 769870 : || TREE_CODE (x) == TARGET_EXPR
245 : : || TREE_CODE (x) == BIND_EXPR))
246 : : {
247 : 769131 : x = copy_node (x);
248 : 769131 : SET_EXPR_LOCATION (x, loc);
249 : : }
250 : 3622284 : return x;
251 : : }
252 : :
253 : : /* If ARG2 divides ARG1 with zero remainder, carries out the exact
254 : : division and returns the quotient. Otherwise returns
255 : : NULL_TREE. */
256 : :
257 : : tree
258 : 0 : div_if_zero_remainder (const_tree arg1, const_tree arg2)
259 : : {
260 : 0 : widest_int quo;
261 : :
262 : 0 : if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
263 : : SIGNED, &quo))
264 : 0 : return wide_int_to_tree (TREE_TYPE (arg1), quo);
265 : :
266 : : return NULL_TREE;
267 : 0 : }
268 : :
269 : : /* This is nonzero if we should defer warnings about undefined
270 : : overflow. This facility exists because these warnings are a
271 : : special case. The code to estimate loop iterations does not want
272 : : to issue any warnings, since it works with expressions which do not
273 : : occur in user code. Various bits of cleanup code call fold(), but
274 : : only use the result if it has certain characteristics (e.g., is a
275 : : constant); that code only wants to issue a warning if the result is
276 : : used. */
277 : :
278 : : static int fold_deferring_overflow_warnings;
279 : :
280 : : /* If a warning about undefined overflow is deferred, this is the
281 : : warning. Note that this may cause us to turn two warnings into
282 : : one, but that is fine since it is sufficient to only give one
283 : : warning per expression. */
284 : :
285 : : static const char* fold_deferred_overflow_warning;
286 : :
287 : : /* If a warning about undefined overflow is deferred, this is the
288 : : level at which the warning should be emitted. */
289 : :
290 : : static enum warn_strict_overflow_code fold_deferred_overflow_code;
291 : :
292 : : /* Start deferring overflow warnings. We could use a stack here to
293 : : permit nested calls, but at present it is not necessary. */
294 : :
295 : : void
296 : 1027391604 : fold_defer_overflow_warnings (void)
297 : : {
298 : 1027391604 : ++fold_deferring_overflow_warnings;
299 : 1027391604 : }
300 : :
301 : : /* Stop deferring overflow warnings. If there is a pending warning,
302 : : and ISSUE is true, then issue the warning if appropriate. STMT is
303 : : the statement with which the warning should be associated (used for
304 : : location information); STMT may be NULL. CODE is the level of the
305 : : warning--a warn_strict_overflow_code value. This function will use
306 : : the smaller of CODE and the deferred code when deciding whether to
307 : : issue the warning. CODE may be zero to mean to always use the
308 : : deferred code. */
309 : :
310 : : void
311 : 1027391604 : fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
312 : : {
313 : 1027391604 : const char *warnmsg;
314 : 1027391604 : location_t locus;
315 : :
316 : 1027391604 : gcc_assert (fold_deferring_overflow_warnings > 0);
317 : 1027391604 : --fold_deferring_overflow_warnings;
318 : 1027391604 : if (fold_deferring_overflow_warnings > 0)
319 : : {
320 : 7768241 : if (fold_deferred_overflow_warning != NULL
321 : 1649540 : && code != 0
322 : 0 : && code < (int) fold_deferred_overflow_code)
323 : 0 : fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
324 : 7768241 : return;
325 : : }
326 : :
327 : 1019623363 : warnmsg = fold_deferred_overflow_warning;
328 : 1019623363 : fold_deferred_overflow_warning = NULL;
329 : :
330 : 1019623363 : if (!issue || warnmsg == NULL)
331 : : return;
332 : :
333 : 10439 : if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
334 : : return;
335 : :
336 : : /* Use the smallest code level when deciding to issue the
337 : : warning. */
338 : 10439 : if (code == 0 || code > (int) fold_deferred_overflow_code)
339 : 10439 : code = fold_deferred_overflow_code;
340 : :
341 : 10439 : if (!issue_strict_overflow_warning (code))
342 : : return;
343 : :
344 : 0 : if (stmt == NULL)
345 : : locus = input_location;
346 : : else
347 : 0 : locus = gimple_location (stmt);
348 : 0 : warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
349 : : }
350 : :
351 : : /* Stop deferring overflow warnings, ignoring any deferred
352 : : warnings. */
353 : :
354 : : void
355 : 164119189 : fold_undefer_and_ignore_overflow_warnings (void)
356 : : {
357 : 164119189 : fold_undefer_overflow_warnings (false, NULL, 0);
358 : 164119189 : }
359 : :
360 : : /* Whether we are deferring overflow warnings. */
361 : :
362 : : bool
363 : 300402904 : fold_deferring_overflow_warnings_p (void)
364 : : {
365 : 300402904 : return fold_deferring_overflow_warnings > 0;
366 : : }
367 : :
368 : : /* This is called when we fold something based on the fact that signed
369 : : overflow is undefined. */
370 : :
371 : : void
372 : 1626574 : fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
373 : : {
374 : 1626574 : if (fold_deferring_overflow_warnings > 0)
375 : : {
376 : 1544145 : if (fold_deferred_overflow_warning == NULL
377 : 702875 : || wc < fold_deferred_overflow_code)
378 : : {
379 : 859552 : fold_deferred_overflow_warning = gmsgid;
380 : 859552 : fold_deferred_overflow_code = wc;
381 : : }
382 : : }
383 : 82429 : else if (issue_strict_overflow_warning (wc))
384 : 7 : warning (OPT_Wstrict_overflow, gmsgid);
385 : 1626574 : }
386 : :
387 : : /* Return true if the built-in mathematical function specified by CODE
388 : : is odd, i.e. -f(x) == f(-x). */
389 : :
390 : : bool
391 : 1841379 : negate_mathfn_p (combined_fn fn)
392 : : {
393 : 1841379 : switch (fn)
394 : : {
395 : : CASE_CFN_ASIN:
396 : : CASE_CFN_ASIN_FN:
397 : : CASE_CFN_ASINH:
398 : : CASE_CFN_ASINH_FN:
399 : : CASE_CFN_ATAN:
400 : : CASE_CFN_ATAN_FN:
401 : : CASE_CFN_ATANH:
402 : : CASE_CFN_ATANH_FN:
403 : : CASE_CFN_CASIN:
404 : : CASE_CFN_CASIN_FN:
405 : : CASE_CFN_CASINH:
406 : : CASE_CFN_CASINH_FN:
407 : : CASE_CFN_CATAN:
408 : : CASE_CFN_CATAN_FN:
409 : : CASE_CFN_CATANH:
410 : : CASE_CFN_CATANH_FN:
411 : : CASE_CFN_CBRT:
412 : : CASE_CFN_CBRT_FN:
413 : : CASE_CFN_CPROJ:
414 : : CASE_CFN_CPROJ_FN:
415 : : CASE_CFN_CSIN:
416 : : CASE_CFN_CSIN_FN:
417 : : CASE_CFN_CSINH:
418 : : CASE_CFN_CSINH_FN:
419 : : CASE_CFN_CTAN:
420 : : CASE_CFN_CTAN_FN:
421 : : CASE_CFN_CTANH:
422 : : CASE_CFN_CTANH_FN:
423 : : CASE_CFN_ERF:
424 : : CASE_CFN_ERF_FN:
425 : : CASE_CFN_LLROUND:
426 : : CASE_CFN_LLROUND_FN:
427 : : CASE_CFN_LROUND:
428 : : CASE_CFN_LROUND_FN:
429 : : CASE_CFN_ROUND:
430 : : CASE_CFN_ROUNDEVEN:
431 : : CASE_CFN_ROUNDEVEN_FN:
432 : : CASE_CFN_SIN:
433 : : CASE_CFN_SIN_FN:
434 : : CASE_CFN_SINH:
435 : : CASE_CFN_SINH_FN:
436 : : CASE_CFN_TAN:
437 : : CASE_CFN_TAN_FN:
438 : : CASE_CFN_TANH:
439 : : CASE_CFN_TANH_FN:
440 : : CASE_CFN_TRUNC:
441 : : CASE_CFN_TRUNC_FN:
442 : : return true;
443 : :
444 : 330 : CASE_CFN_LLRINT:
445 : 330 : CASE_CFN_LLRINT_FN:
446 : 330 : CASE_CFN_LRINT:
447 : 330 : CASE_CFN_LRINT_FN:
448 : 330 : CASE_CFN_NEARBYINT:
449 : 330 : CASE_CFN_NEARBYINT_FN:
450 : 330 : CASE_CFN_RINT:
451 : 330 : CASE_CFN_RINT_FN:
452 : 330 : return !flag_rounding_math;
453 : :
454 : 1837583 : default:
455 : 1837583 : break;
456 : : }
457 : 1837583 : return false;
458 : : }
459 : :
460 : : /* Check whether we may negate an integer constant T without causing
461 : : overflow. */
462 : :
463 : : bool
464 : 2942618 : may_negate_without_overflow_p (const_tree t)
465 : : {
466 : 2942618 : tree type;
467 : :
468 : 2942618 : gcc_assert (TREE_CODE (t) == INTEGER_CST);
469 : :
470 : 2942618 : type = TREE_TYPE (t);
471 : 2942618 : if (TYPE_UNSIGNED (type))
472 : : return false;
473 : :
474 : 2942618 : return !wi::only_sign_bit_p (wi::to_wide (t));
475 : : }
476 : :
477 : : /* Determine whether an expression T can be cheaply negated using
478 : : the function negate_expr without introducing undefined overflow. */
479 : :
480 : : static bool
481 : 24343540 : negate_expr_p (tree t)
482 : : {
483 : 24492168 : tree type;
484 : :
485 : 24492168 : if (t == 0)
486 : : return false;
487 : :
488 : 24492168 : type = TREE_TYPE (t);
489 : :
490 : 24492168 : STRIP_SIGN_NOPS (t);
491 : 24492168 : switch (TREE_CODE (t))
492 : : {
493 : 1380782 : case INTEGER_CST:
494 : 1380782 : if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
495 : : return true;
496 : :
497 : : /* Check that -CST will not overflow type. */
498 : 400243 : return may_negate_without_overflow_p (t);
499 : 503 : case BIT_NOT_EXPR:
500 : 503 : return (INTEGRAL_TYPE_P (type)
501 : 503 : && TYPE_OVERFLOW_WRAPS (type));
502 : :
503 : : case FIXED_CST:
504 : : return true;
505 : :
506 : 1308 : case NEGATE_EXPR:
507 : 1308 : return !TYPE_OVERFLOW_SANITIZED (type);
508 : :
509 : 1295685 : case REAL_CST:
510 : : /* We want to canonicalize to positive real constants. Pretend
511 : : that only negative ones can be easily negated. */
512 : 1295685 : return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
513 : :
514 : 452 : case COMPLEX_CST:
515 : 452 : return negate_expr_p (TREE_REALPART (t))
516 : 568 : && negate_expr_p (TREE_IMAGPART (t));
517 : :
518 : 97 : case VECTOR_CST:
519 : 97 : {
520 : 97 : if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
521 : : return true;
522 : :
523 : : /* Steps don't prevent negation. */
524 : 97 : unsigned int count = vector_cst_encoded_nelts (t);
525 : 194 : for (unsigned int i = 0; i < count; ++i)
526 : 97 : if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
527 : : return false;
528 : :
529 : : return true;
530 : : }
531 : :
532 : 705 : case COMPLEX_EXPR:
533 : 705 : return negate_expr_p (TREE_OPERAND (t, 0))
534 : 705 : && negate_expr_p (TREE_OPERAND (t, 1));
535 : :
536 : 33 : case CONJ_EXPR:
537 : 33 : return negate_expr_p (TREE_OPERAND (t, 0));
538 : :
539 : 1340014 : case PLUS_EXPR:
540 : 1340014 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
541 : 1340008 : || HONOR_SIGNED_ZEROS (type)
542 : 2391594 : || (ANY_INTEGRAL_TYPE_P (type)
543 : 1051394 : && ! TYPE_OVERFLOW_WRAPS (type)))
544 : 681366 : return false;
545 : : /* -(A + B) -> (-B) - A. */
546 : 658648 : if (negate_expr_p (TREE_OPERAND (t, 1)))
547 : : return true;
548 : : /* -(A + B) -> (-A) - B. */
549 : 137137 : return negate_expr_p (TREE_OPERAND (t, 0));
550 : :
551 : 264027 : case MINUS_EXPR:
552 : : /* We can't turn -(A-B) into B-A when we honor signed zeros. */
553 : 264027 : return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
554 : 264027 : && !HONOR_SIGNED_ZEROS (type)
555 : 353918 : && (! ANY_INTEGRAL_TYPE_P (type)
556 : 89668 : || TYPE_OVERFLOW_WRAPS (type));
557 : :
558 : 2372173 : case MULT_EXPR:
559 : 2372173 : if (TYPE_UNSIGNED (type))
560 : : break;
561 : : /* INT_MIN/n * n doesn't overflow while negating one operand it does
562 : : if n is a (negative) power of two. */
563 : 4142592 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
564 : 170351 : && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
565 : 2239288 : && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
566 : 0 : && (wi::popcount
567 : 2071296 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
568 : 167992 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
569 : 150411 : && (wi::popcount
570 : 2372118 : (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
571 : : break;
572 : :
573 : : /* Fall through. */
574 : :
575 : 2324049 : case RDIV_EXPR:
576 : 2324049 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
577 : 2324048 : return negate_expr_p (TREE_OPERAND (t, 1))
578 : 2324048 : || negate_expr_p (TREE_OPERAND (t, 0));
579 : : break;
580 : :
581 : 2433 : case TRUNC_DIV_EXPR:
582 : 2433 : case ROUND_DIV_EXPR:
583 : 2433 : case EXACT_DIV_EXPR:
584 : 2433 : if (TYPE_UNSIGNED (type))
585 : : break;
586 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
587 : : B is not 1 we change the sign of the result. */
588 : 463 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
589 : 463 : && negate_expr_p (TREE_OPERAND (t, 0)))
590 : : return true;
591 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
592 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
593 : : and actually traps on some architectures. */
594 : 596 : if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
595 : 298 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
596 : 511 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
597 : 206 : && ! integer_onep (TREE_OPERAND (t, 1))))
598 : 291 : return negate_expr_p (TREE_OPERAND (t, 1));
599 : : break;
600 : :
601 : 3684358 : case NOP_EXPR:
602 : : /* Negate -((double)float) as (double)(-float). */
603 : 3684358 : if (SCALAR_FLOAT_TYPE_P (type))
604 : : {
605 : 11319 : tree tem = strip_float_extensions (t);
606 : 11319 : if (tem != t)
607 : : return negate_expr_p (tem);
608 : : }
609 : : break;
610 : :
611 : 914593 : case CALL_EXPR:
612 : : /* Negate -f(x) as f(-x). */
613 : 914593 : if (negate_mathfn_p (get_call_combined_fn (t)))
614 : 59 : return negate_expr_p (CALL_EXPR_ARG (t, 0));
615 : : break;
616 : :
617 : 637 : case RSHIFT_EXPR:
618 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
619 : 637 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
620 : : {
621 : 492 : tree op1 = TREE_OPERAND (t, 1);
622 : 492 : if (wi::to_wide (op1) == element_precision (type) - 1)
623 : : return true;
624 : : }
625 : : break;
626 : :
627 : : default:
628 : : break;
629 : : }
630 : : return false;
631 : : }
632 : :
633 : : /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
634 : : simplification is possible.
635 : : If negate_expr_p would return true for T, NULL_TREE will never be
636 : : returned. */
637 : :
638 : : static tree
639 : 34199123 : fold_negate_expr_1 (location_t loc, tree t)
640 : : {
641 : 34199123 : tree type = TREE_TYPE (t);
642 : 34199123 : tree tem;
643 : :
644 : 34199123 : switch (TREE_CODE (t))
645 : : {
646 : : /* Convert - (~A) to A + 1. */
647 : 136 : case BIT_NOT_EXPR:
648 : 136 : if (INTEGRAL_TYPE_P (type))
649 : 136 : return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
650 : 136 : build_one_cst (type));
651 : : break;
652 : :
653 : 26608733 : case INTEGER_CST:
654 : 26608733 : tem = fold_negate_const (t, type);
655 : 26608733 : if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
656 : 6355 : || (ANY_INTEGRAL_TYPE_P (type)
657 : 6355 : && !TYPE_OVERFLOW_TRAPS (type)
658 : 6355 : && TYPE_OVERFLOW_WRAPS (type))
659 : 26614395 : || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
660 : : return tem;
661 : : break;
662 : :
663 : 1946003 : case POLY_INT_CST:
664 : 1946003 : case REAL_CST:
665 : 1946003 : case FIXED_CST:
666 : 1946003 : tem = fold_negate_const (t, type);
667 : 1946003 : return tem;
668 : :
669 : 66122 : case COMPLEX_CST:
670 : 66122 : {
671 : 66122 : tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
672 : 66122 : tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
673 : 66122 : if (rpart && ipart)
674 : 66122 : return build_complex (type, rpart, ipart);
675 : : }
676 : : break;
677 : :
678 : 38935 : case VECTOR_CST:
679 : 38935 : {
680 : 38935 : tree_vector_builder elts;
681 : 38935 : elts.new_unary_operation (type, t, true);
682 : 38935 : unsigned int count = elts.encoded_nelts ();
683 : 92973 : for (unsigned int i = 0; i < count; ++i)
684 : : {
685 : 54038 : tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
686 : 54038 : if (elt == NULL_TREE)
687 : 0 : return NULL_TREE;
688 : 54038 : elts.quick_push (elt);
689 : : }
690 : :
691 : 38935 : return elts.build ();
692 : 38935 : }
693 : :
694 : 78 : case COMPLEX_EXPR:
695 : 78 : if (negate_expr_p (t))
696 : 40 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
697 : 20 : fold_negate_expr (loc, TREE_OPERAND (t, 0)),
698 : 40 : fold_negate_expr (loc, TREE_OPERAND (t, 1)));
699 : : break;
700 : :
701 : 21 : case CONJ_EXPR:
702 : 21 : if (negate_expr_p (t))
703 : 21 : return fold_build1_loc (loc, CONJ_EXPR, type,
704 : 42 : fold_negate_expr (loc, TREE_OPERAND (t, 0)));
705 : : break;
706 : :
707 : 1234 : case NEGATE_EXPR:
708 : 1234 : if (!TYPE_OVERFLOW_SANITIZED (type))
709 : 1221 : return TREE_OPERAND (t, 0);
710 : : break;
711 : :
712 : 575619 : case PLUS_EXPR:
713 : 575619 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
714 : 575619 : && !HONOR_SIGNED_ZEROS (type))
715 : : {
716 : : /* -(A + B) -> (-B) - A. */
717 : 575509 : if (negate_expr_p (TREE_OPERAND (t, 1)))
718 : : {
719 : 525082 : tem = negate_expr (TREE_OPERAND (t, 1));
720 : 525082 : return fold_build2_loc (loc, MINUS_EXPR, type,
721 : 1050164 : tem, TREE_OPERAND (t, 0));
722 : : }
723 : :
724 : : /* -(A + B) -> (-A) - B. */
725 : 50427 : if (negate_expr_p (TREE_OPERAND (t, 0)))
726 : : {
727 : 973 : tem = negate_expr (TREE_OPERAND (t, 0));
728 : 973 : return fold_build2_loc (loc, MINUS_EXPR, type,
729 : 1946 : tem, TREE_OPERAND (t, 1));
730 : : }
731 : : }
732 : : break;
733 : :
734 : 141424 : case MINUS_EXPR:
735 : : /* - (A - B) -> B - A */
736 : 141424 : if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
737 : 141424 : && !HONOR_SIGNED_ZEROS (type))
738 : 64125 : return fold_build2_loc (loc, MINUS_EXPR, type,
739 : 128250 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
740 : : break;
741 : :
742 : 164379 : case MULT_EXPR:
743 : 164379 : if (TYPE_UNSIGNED (type))
744 : : break;
745 : :
746 : : /* Fall through. */
747 : :
748 : 73668 : case RDIV_EXPR:
749 : 73668 : if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
750 : : {
751 : 73668 : tem = TREE_OPERAND (t, 1);
752 : 73668 : if (negate_expr_p (tem))
753 : 140538 : return fold_build2_loc (loc, TREE_CODE (t), type,
754 : 140538 : TREE_OPERAND (t, 0), negate_expr (tem));
755 : 3399 : tem = TREE_OPERAND (t, 0);
756 : 3399 : if (negate_expr_p (tem))
757 : 68 : return fold_build2_loc (loc, TREE_CODE (t), type,
758 : 136 : negate_expr (tem), TREE_OPERAND (t, 1));
759 : : }
760 : : break;
761 : :
762 : 1653 : case TRUNC_DIV_EXPR:
763 : 1653 : case ROUND_DIV_EXPR:
764 : 1653 : case EXACT_DIV_EXPR:
765 : 1653 : if (TYPE_UNSIGNED (type))
766 : : break;
767 : : /* In general we can't negate A in A / B, because if A is INT_MIN and
768 : : B is not 1 we change the sign of the result. */
769 : 642 : if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
770 : 642 : && negate_expr_p (TREE_OPERAND (t, 0)))
771 : 323 : return fold_build2_loc (loc, TREE_CODE (t), type,
772 : 323 : negate_expr (TREE_OPERAND (t, 0)),
773 : 646 : TREE_OPERAND (t, 1));
774 : : /* In general we can't negate B in A / B, because if A is INT_MIN and
775 : : B is 1, we may turn this into INT_MIN / -1 which is undefined
776 : : and actually traps on some architectures. */
777 : 638 : if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
778 : 319 : || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
779 : 235 : || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
780 : 212 : && ! integer_onep (TREE_OPERAND (t, 1))))
781 : 615 : && negate_expr_p (TREE_OPERAND (t, 1)))
782 : 580 : return fold_build2_loc (loc, TREE_CODE (t), type,
783 : 290 : TREE_OPERAND (t, 0),
784 : 580 : negate_expr (TREE_OPERAND (t, 1)));
785 : : break;
786 : :
787 : 1422229 : case NOP_EXPR:
788 : : /* Convert -((double)float) into (double)(-float). */
789 : 1422229 : if (SCALAR_FLOAT_TYPE_P (type))
790 : : {
791 : 10901 : tem = strip_float_extensions (t);
792 : 10901 : if (tem != t && negate_expr_p (tem))
793 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
794 : : }
795 : : break;
796 : :
797 : 292810 : case CALL_EXPR:
798 : : /* Negate -f(x) as f(-x). */
799 : 292810 : if (negate_mathfn_p (get_call_combined_fn (t))
800 : 294097 : && negate_expr_p (CALL_EXPR_ARG (t, 0)))
801 : : {
802 : 1191 : tree fndecl, arg;
803 : :
804 : 1191 : fndecl = get_callee_fndecl (t);
805 : 1191 : arg = negate_expr (CALL_EXPR_ARG (t, 0));
806 : 1191 : return build_call_expr_loc (loc, fndecl, 1, arg);
807 : : }
808 : : break;
809 : :
810 : 250 : case RSHIFT_EXPR:
811 : : /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
812 : 250 : if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
813 : : {
814 : 231 : tree op1 = TREE_OPERAND (t, 1);
815 : 231 : if (wi::to_wide (op1) == element_precision (type) - 1)
816 : : {
817 : 72 : tree ntype = TYPE_UNSIGNED (type)
818 : 72 : ? signed_type_for (type)
819 : 72 : : unsigned_type_for (type);
820 : 72 : tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
821 : 72 : temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
822 : 72 : return fold_convert_loc (loc, type, temp);
823 : : }
824 : : }
825 : : break;
826 : :
827 : : default:
828 : : break;
829 : : }
830 : :
831 : : return NULL_TREE;
832 : : }
833 : :
834 : : /* A wrapper for fold_negate_expr_1. */
835 : :
836 : : static tree
837 : 34199123 : fold_negate_expr (location_t loc, tree t)
838 : : {
839 : 34199123 : tree type = TREE_TYPE (t);
840 : 34199123 : STRIP_SIGN_NOPS (t);
841 : 34199123 : tree tem = fold_negate_expr_1 (loc, t);
842 : 34199123 : if (tem == NULL_TREE)
843 : : return NULL_TREE;
844 : 29323476 : return fold_convert_loc (loc, type, tem);
845 : : }
846 : :
847 : : /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
848 : : negated in a simpler way. Also allow for T to be NULL_TREE, in which case
849 : : return NULL_TREE. */
850 : :
851 : : static tree
852 : 2936716 : negate_expr (tree t)
853 : : {
854 : 2936716 : tree type, tem;
855 : 2936716 : location_t loc;
856 : :
857 : 2936716 : if (t == NULL_TREE)
858 : : return NULL_TREE;
859 : :
860 : 2936716 : loc = EXPR_LOCATION (t);
861 : 2936716 : type = TREE_TYPE (t);
862 : 2936716 : STRIP_SIGN_NOPS (t);
863 : :
864 : 2936716 : tem = fold_negate_expr (loc, t);
865 : 2936716 : if (!tem)
866 : 1291016 : tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
867 : 2936716 : return fold_convert_loc (loc, type, tem);
868 : : }
869 : :
870 : : /* Split a tree IN into a constant, literal and variable parts that could be
871 : : combined with CODE to make IN. "constant" means an expression with
872 : : TREE_CONSTANT but that isn't an actual constant. CODE must be a
873 : : commutative arithmetic operation. Store the constant part into *CONP,
874 : : the literal in *LITP and return the variable part. If a part isn't
875 : : present, set it to null. If the tree does not decompose in this way,
876 : : return the entire tree as the variable part and the other parts as null.
877 : :
878 : : If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
879 : : case, we negate an operand that was subtracted. Except if it is a
880 : : literal for which we use *MINUS_LITP instead.
881 : :
882 : : If NEGATE_P is true, we are negating all of IN, again except a literal
883 : : for which we use *MINUS_LITP instead. If a variable part is of pointer
884 : : type, it is negated after converting to TYPE. This prevents us from
885 : : generating illegal MINUS pointer expression. LOC is the location of
886 : : the converted variable part.
887 : :
888 : : If IN is itself a literal or constant, return it as appropriate.
889 : :
890 : : Note that we do not guarantee that any of the three values will be the
891 : : same type as IN, but they will have the same signedness and mode. */
892 : :
893 : : static tree
894 : 186172970 : split_tree (tree in, tree type, enum tree_code code,
895 : : tree *minus_varp, tree *conp, tree *minus_conp,
896 : : tree *litp, tree *minus_litp, int negate_p)
897 : : {
898 : 186172970 : tree var = 0;
899 : 186172970 : *minus_varp = 0;
900 : 186172970 : *conp = 0;
901 : 186172970 : *minus_conp = 0;
902 : 186172970 : *litp = 0;
903 : 186172970 : *minus_litp = 0;
904 : :
905 : : /* Strip any conversions that don't change the machine mode or signedness. */
906 : 186172970 : STRIP_SIGN_NOPS (in);
907 : :
908 : 186172970 : if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
909 : 119484952 : || TREE_CODE (in) == FIXED_CST)
910 : 66688018 : *litp = in;
911 : 119484952 : else if (TREE_CODE (in) == code
912 : 119484952 : || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
913 : 115758524 : && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
914 : : /* We can associate addition and subtraction together (even
915 : : though the C standard doesn't say so) for integers because
916 : : the value is not affected. For reals, the value might be
917 : : affected, so we can't. */
918 : 115758524 : && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
919 : 48805179 : || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
920 : 114330933 : || (code == MINUS_EXPR
921 : 18209408 : && (TREE_CODE (in) == PLUS_EXPR
922 : 16745505 : || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
923 : : {
924 : 6917895 : tree op0 = TREE_OPERAND (in, 0);
925 : 6917895 : tree op1 = TREE_OPERAND (in, 1);
926 : 6917895 : bool neg1_p = TREE_CODE (in) == MINUS_EXPR;
927 : 6917895 : bool neg_litp_p = false, neg_conp_p = false, neg_var_p = false;
928 : :
929 : : /* First see if either of the operands is a literal, then a constant. */
930 : 6917895 : if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
931 : 6719710 : || TREE_CODE (op0) == FIXED_CST)
932 : 198185 : *litp = op0, op0 = 0;
933 : 6719710 : else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
934 : 4435327 : || TREE_CODE (op1) == FIXED_CST)
935 : 2284383 : *litp = op1, neg_litp_p = neg1_p, op1 = 0;
936 : :
937 : 6917895 : if (op0 != 0 && TREE_CONSTANT (op0))
938 : 13563 : *conp = op0, op0 = 0;
939 : 6904332 : else if (op1 != 0 && TREE_CONSTANT (op1))
940 : 33974 : *conp = op1, neg_conp_p = neg1_p, op1 = 0;
941 : :
942 : : /* If we haven't dealt with either operand, this is not a case we can
943 : : decompose. Otherwise, VAR is either of the ones remaining, if any. */
944 : 6917895 : if (op0 != 0 && op1 != 0)
945 : : var = in;
946 : 2518296 : else if (op0 != 0)
947 : : var = op0;
948 : : else
949 : 211748 : var = op1, neg_var_p = neg1_p;
950 : :
951 : : /* Now do any needed negations. */
952 : 6917895 : if (neg_litp_p)
953 : 21395 : *minus_litp = *litp, *litp = 0;
954 : 6917895 : if (neg_conp_p && *conp)
955 : 5629 : *minus_conp = *conp, *conp = 0;
956 : 6917895 : if (neg_var_p && var)
957 : 199557 : *minus_varp = var, var = 0;
958 : : }
959 : 112567057 : else if (TREE_CONSTANT (in))
960 : 519596 : *conp = in;
961 : 112047461 : else if (TREE_CODE (in) == BIT_NOT_EXPR
962 : 385149 : && code == PLUS_EXPR)
963 : : {
964 : : /* -1 - X is folded to ~X, undo that here. Do _not_ do this
965 : : when IN is constant. */
966 : 299896 : *litp = build_minus_one_cst (type);
967 : 299896 : *minus_varp = TREE_OPERAND (in, 0);
968 : : }
969 : : else
970 : : var = in;
971 : :
972 : 186172970 : if (negate_p)
973 : : {
974 : 9933518 : if (*litp)
975 : 956413 : *minus_litp = *litp, *litp = 0;
976 : 8977105 : else if (*minus_litp)
977 : 149 : *litp = *minus_litp, *minus_litp = 0;
978 : 9933518 : if (*conp)
979 : 25342 : *minus_conp = *conp, *conp = 0;
980 : 9908176 : else if (*minus_conp)
981 : 0 : *conp = *minus_conp, *minus_conp = 0;
982 : 9933518 : if (var)
983 : 9896514 : *minus_varp = var, var = 0;
984 : 37004 : else if (*minus_varp)
985 : 1293 : var = *minus_varp, *minus_varp = 0;
986 : : }
987 : :
988 : 186172970 : if (*litp
989 : 186172970 : && TREE_OVERFLOW_P (*litp))
990 : 12889 : *litp = drop_tree_overflow (*litp);
991 : 186172970 : if (*minus_litp
992 : 186172970 : && TREE_OVERFLOW_P (*minus_litp))
993 : 0 : *minus_litp = drop_tree_overflow (*minus_litp);
994 : :
995 : 186172970 : return var;
996 : : }
997 : :
998 : : /* Re-associate trees split by the above function. T1 and T2 are
999 : : either expressions to associate or null. Return the new
1000 : : expression, if any. LOC is the location of the new expression. If
1001 : : we build an operation, do it in TYPE and with CODE. */
1002 : :
1003 : : static tree
1004 : 16425632 : associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1005 : : {
1006 : 16425632 : if (t1 == 0)
1007 : : {
1008 : 10413072 : gcc_assert (t2 == 0 || code != MINUS_EXPR);
1009 : : return t2;
1010 : : }
1011 : 6012560 : else if (t2 == 0)
1012 : : return t1;
1013 : :
1014 : : /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1015 : : try to fold this since we will have infinite recursion. But do
1016 : : deal with any NEGATE_EXPRs. */
1017 : 3351021 : if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1018 : 2653598 : || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
1019 : 2606212 : || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1020 : : {
1021 : 1311616 : if (code == PLUS_EXPR)
1022 : : {
1023 : 752204 : if (TREE_CODE (t1) == NEGATE_EXPR)
1024 : 46 : return build2_loc (loc, MINUS_EXPR, type,
1025 : : fold_convert_loc (loc, type, t2),
1026 : : fold_convert_loc (loc, type,
1027 : 92 : TREE_OPERAND (t1, 0)));
1028 : 752158 : else if (TREE_CODE (t2) == NEGATE_EXPR)
1029 : 0 : return build2_loc (loc, MINUS_EXPR, type,
1030 : : fold_convert_loc (loc, type, t1),
1031 : : fold_convert_loc (loc, type,
1032 : 0 : TREE_OPERAND (t2, 0)));
1033 : 752158 : else if (integer_zerop (t2))
1034 : 24340 : return fold_convert_loc (loc, type, t1);
1035 : : }
1036 : 559412 : else if (code == MINUS_EXPR)
1037 : : {
1038 : 538969 : if (integer_zerop (t2))
1039 : 0 : return fold_convert_loc (loc, type, t1);
1040 : : }
1041 : :
1042 : 1287230 : return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1043 : 1287230 : fold_convert_loc (loc, type, t2));
1044 : : }
1045 : :
1046 : 2039405 : return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1047 : 2039405 : fold_convert_loc (loc, type, t2));
1048 : : }
1049 : :
1050 : : /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1051 : : for use in int_const_binop, size_binop and size_diffop. */
1052 : :
1053 : : static bool
1054 : 2003469457 : int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1055 : : {
1056 : 2003469457 : if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
1057 : : return false;
1058 : 2003469457 : if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
1059 : : return false;
1060 : :
1061 : 2003469457 : switch (code)
1062 : : {
1063 : : case LSHIFT_EXPR:
1064 : : case RSHIFT_EXPR:
1065 : : case LROTATE_EXPR:
1066 : : case RROTATE_EXPR:
1067 : : return true;
1068 : :
1069 : 2003469457 : default:
1070 : 2003469457 : break;
1071 : : }
1072 : :
1073 : 2003469457 : return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1074 : 2003469457 : && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1075 : 4006938914 : && TYPE_MODE (type1) == TYPE_MODE (type2);
1076 : : }
1077 : :
1078 : : /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1079 : : a new constant in RES. Return FALSE if we don't know how to
1080 : : evaluate CODE at compile-time. */
1081 : :
1082 : : bool
1083 : 1316529280 : wide_int_binop (wide_int &res,
1084 : : enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1085 : : signop sign, wi::overflow_type *overflow)
1086 : : {
1087 : 1316529280 : wide_int tmp;
1088 : 1316529280 : *overflow = wi::OVF_NONE;
1089 : 1316529280 : switch (code)
1090 : : {
1091 : 2049268 : case BIT_IOR_EXPR:
1092 : 2049268 : res = wi::bit_or (arg1, arg2);
1093 : 2049268 : break;
1094 : :
1095 : 74893 : case BIT_XOR_EXPR:
1096 : 74893 : res = wi::bit_xor (arg1, arg2);
1097 : 74893 : break;
1098 : :
1099 : 17911474 : case BIT_AND_EXPR:
1100 : 17911474 : res = wi::bit_and (arg1, arg2);
1101 : 17911474 : break;
1102 : :
1103 : 12147748 : case LSHIFT_EXPR:
1104 : 12147748 : if (wi::neg_p (arg2))
1105 : : return false;
1106 : 12117556 : res = wi::lshift (arg1, arg2);
1107 : 12117556 : break;
1108 : :
1109 : 7479773 : case RSHIFT_EXPR:
1110 : 7479773 : if (wi::neg_p (arg2))
1111 : : return false;
1112 : : /* It's unclear from the C standard whether shifts can overflow.
1113 : : The following code ignores overflow; perhaps a C standard
1114 : : interpretation ruling is needed. */
1115 : 7479514 : res = wi::rshift (arg1, arg2, sign);
1116 : 7479514 : break;
1117 : :
1118 : 935 : case RROTATE_EXPR:
1119 : 935 : case LROTATE_EXPR:
1120 : 935 : if (wi::neg_p (arg2))
1121 : : {
1122 : 14 : tmp = -arg2;
1123 : 14 : if (code == RROTATE_EXPR)
1124 : : code = LROTATE_EXPR;
1125 : : else
1126 : : code = RROTATE_EXPR;
1127 : : }
1128 : : else
1129 : 921 : tmp = arg2;
1130 : :
1131 : 921 : if (code == RROTATE_EXPR)
1132 : 856 : res = wi::rrotate (arg1, tmp);
1133 : : else
1134 : 79 : res = wi::lrotate (arg1, tmp);
1135 : : break;
1136 : :
1137 : 184442305 : case PLUS_EXPR:
1138 : 184442305 : res = wi::add (arg1, arg2, sign, overflow);
1139 : 184442305 : break;
1140 : :
1141 : 65625948 : case MINUS_EXPR:
1142 : 65625948 : res = wi::sub (arg1, arg2, sign, overflow);
1143 : 65625948 : break;
1144 : :
1145 : 475968749 : case MULT_EXPR:
1146 : 475968749 : res = wi::mul (arg1, arg2, sign, overflow);
1147 : 475968749 : break;
1148 : :
1149 : 4484 : case MULT_HIGHPART_EXPR:
1150 : 4484 : res = wi::mul_high (arg1, arg2, sign);
1151 : 4484 : break;
1152 : :
1153 : 268058032 : case TRUNC_DIV_EXPR:
1154 : 268058032 : case EXACT_DIV_EXPR:
1155 : 268058032 : if (arg2 == 0)
1156 : : return false;
1157 : 268052349 : res = wi::div_trunc (arg1, arg2, sign, overflow);
1158 : 268052349 : break;
1159 : :
1160 : 61357523 : case FLOOR_DIV_EXPR:
1161 : 61357523 : if (arg2 == 0)
1162 : : return false;
1163 : 61357523 : res = wi::div_floor (arg1, arg2, sign, overflow);
1164 : 61357523 : break;
1165 : :
1166 : 98052995 : case CEIL_DIV_EXPR:
1167 : 98052995 : if (arg2 == 0)
1168 : : return false;
1169 : 98052995 : res = wi::div_ceil (arg1, arg2, sign, overflow);
1170 : 98052995 : break;
1171 : :
1172 : 0 : case ROUND_DIV_EXPR:
1173 : 0 : if (arg2 == 0)
1174 : : return false;
1175 : 0 : res = wi::div_round (arg1, arg2, sign, overflow);
1176 : 0 : break;
1177 : :
1178 : 593819 : case TRUNC_MOD_EXPR:
1179 : 593819 : if (arg2 == 0)
1180 : : return false;
1181 : 592716 : res = wi::mod_trunc (arg1, arg2, sign, overflow);
1182 : 592716 : break;
1183 : :
1184 : 51829048 : case FLOOR_MOD_EXPR:
1185 : 51829048 : if (arg2 == 0)
1186 : : return false;
1187 : 51829048 : res = wi::mod_floor (arg1, arg2, sign, overflow);
1188 : 51829048 : break;
1189 : :
1190 : 178 : case CEIL_MOD_EXPR:
1191 : 178 : if (arg2 == 0)
1192 : : return false;
1193 : 178 : res = wi::mod_ceil (arg1, arg2, sign, overflow);
1194 : 178 : break;
1195 : :
1196 : 0 : case ROUND_MOD_EXPR:
1197 : 0 : if (arg2 == 0)
1198 : : return false;
1199 : 0 : res = wi::mod_round (arg1, arg2, sign, overflow);
1200 : 0 : break;
1201 : :
1202 : 26078 : case MIN_EXPR:
1203 : 26078 : res = wi::min (arg1, arg2, sign);
1204 : 26078 : break;
1205 : :
1206 : 70905878 : case MAX_EXPR:
1207 : 70905878 : res = wi::max (arg1, arg2, sign);
1208 : 70905878 : break;
1209 : :
1210 : : default:
1211 : : return false;
1212 : : }
1213 : : return true;
1214 : 1316529280 : }
1215 : :
1216 : : /* Returns true if we know who is smaller or equal, ARG1 or ARG2, and set the
1217 : : min value to RES. */
1218 : : bool
1219 : 0 : can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res)
1220 : : {
1221 : 0 : if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2)))
1222 : : {
1223 : 0 : res = wi::to_poly_wide (arg1);
1224 : 0 : return true;
1225 : : }
1226 : 0 : else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1)))
1227 : : {
1228 : 0 : res = wi::to_poly_wide (arg2);
1229 : 0 : return true;
1230 : : }
1231 : :
1232 : : return false;
1233 : : }
1234 : :
1235 : : /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1236 : : produce a new constant in RES. Return FALSE if we don't know how
1237 : : to evaluate CODE at compile-time. */
1238 : :
1239 : : bool
1240 : 1316529280 : poly_int_binop (poly_wide_int &res, enum tree_code code,
1241 : : const_tree arg1, const_tree arg2,
1242 : : signop sign, wi::overflow_type *overflow)
1243 : : {
1244 : 1316529280 : gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1245 : :
1246 : 1316529280 : if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1247 : : {
1248 : 1316529280 : wide_int warg1 = wi::to_wide (arg1), wi_res;
1249 : 1316529280 : wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (TREE_TYPE (arg1)));
1250 : 1316529280 : if (!wide_int_binop (wi_res, code, warg1, warg2, sign, overflow))
1251 : : return NULL_TREE;
1252 : 1316491891 : res = wi_res;
1253 : 1316491891 : return true;
1254 : 1316529448 : }
1255 : :
1256 : : gcc_assert (NUM_POLY_INT_COEFFS != 1);
1257 : :
1258 : : switch (code)
1259 : : {
1260 : : case PLUS_EXPR:
1261 : : res = wi::add (wi::to_poly_wide (arg1),
1262 : : wi::to_poly_wide (arg2), sign, overflow);
1263 : : break;
1264 : :
1265 : : case MINUS_EXPR:
1266 : : res = wi::sub (wi::to_poly_wide (arg1),
1267 : : wi::to_poly_wide (arg2), sign, overflow);
1268 : : break;
1269 : :
1270 : : case MULT_EXPR:
1271 : : if (TREE_CODE (arg2) == INTEGER_CST)
1272 : : res = wi::mul (wi::to_poly_wide (arg1),
1273 : : wi::to_wide (arg2), sign, overflow);
1274 : : else if (TREE_CODE (arg1) == INTEGER_CST)
1275 : : res = wi::mul (wi::to_poly_wide (arg2),
1276 : : wi::to_wide (arg1), sign, overflow);
1277 : : else
1278 : : return NULL_TREE;
1279 : : break;
1280 : :
1281 : : case LSHIFT_EXPR:
1282 : : if (TREE_CODE (arg2) == INTEGER_CST)
1283 : : res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1284 : : else
1285 : : return false;
1286 : : break;
1287 : :
1288 : : case BIT_IOR_EXPR:
1289 : : if (TREE_CODE (arg2) != INTEGER_CST
1290 : : || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1291 : : &res))
1292 : : return false;
1293 : : break;
1294 : :
1295 : : case MIN_EXPR:
1296 : : if (!can_min_p (arg1, arg2, res))
1297 : : return false;
1298 : : break;
1299 : :
1300 : : default:
1301 : : return false;
1302 : : }
1303 : : return true;
1304 : : }
1305 : :
1306 : : /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1307 : : produce a new constant. Return NULL_TREE if we don't know how to
1308 : : evaluate CODE at compile-time. */
1309 : :
1310 : : tree
1311 : 1316529280 : int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1312 : : int overflowable)
1313 : : {
1314 : 1316529280 : poly_wide_int poly_res;
1315 : 1316529280 : tree type = TREE_TYPE (arg1);
1316 : 1316529280 : signop sign = TYPE_SIGN (type);
1317 : 1316529280 : wi::overflow_type overflow = wi::OVF_NONE;
1318 : :
1319 : 1316529280 : if (!poly_int_tree_p (arg1)
1320 : 1316529280 : || !poly_int_tree_p (arg2)
1321 : 2633058560 : || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1322 : 37389 : return NULL_TREE;
1323 : 1316491891 : return force_fit_type (type, poly_res, overflowable,
1324 : 1316491891 : (((sign == SIGNED || overflowable == -1)
1325 : 1316491891 : && overflow)
1326 : 1316491891 : | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1327 : 1316529280 : }
1328 : :
1329 : : /* Return true if binary operation OP distributes over addition in operand
1330 : : OPNO, with the other operand being held constant. OPNO counts from 1. */
1331 : :
1332 : : static bool
1333 : 138815 : distributes_over_addition_p (tree_code op, int opno)
1334 : : {
1335 : 0 : switch (op)
1336 : : {
1337 : : case PLUS_EXPR:
1338 : : case MINUS_EXPR:
1339 : : case MULT_EXPR:
1340 : : return true;
1341 : :
1342 : 0 : case LSHIFT_EXPR:
1343 : 0 : return opno == 1;
1344 : :
1345 : 3210 : default:
1346 : 3210 : return false;
1347 : : }
1348 : : }
1349 : :
1350 : : /* OP is the INDEXth operand to CODE (counting from zero) and OTHER_OP
1351 : : is the other operand. Try to use the value of OP to simplify the
1352 : : operation in one step, without having to process individual elements. */
1353 : : static tree
1354 : 332788 : simplify_const_binop (tree_code code, tree op, tree other_op,
1355 : : int index ATTRIBUTE_UNUSED)
1356 : : {
1357 : : /* AND, IOR as well as XOR with a zerop can be simplified directly. */
1358 : 332788 : if (TREE_CODE (op) == VECTOR_CST && TREE_CODE (other_op) == VECTOR_CST)
1359 : : {
1360 : 267159 : if (integer_zerop (other_op))
1361 : : {
1362 : 19094 : if (code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1363 : : return op;
1364 : 18581 : else if (code == BIT_AND_EXPR)
1365 : : return other_op;
1366 : : }
1367 : : }
1368 : :
1369 : : return NULL_TREE;
1370 : : }
1371 : :
1372 : : /* If ARG1 and ARG2 are constants, and if performing CODE on them would
1373 : : be an elementwise vector operation, try to fold the operation to a
1374 : : constant vector, using ELT_CONST_BINOP to fold each element. Return
1375 : : the folded value on success, otherwise return null. */
1376 : : tree
1377 : 200318 : vector_const_binop (tree_code code, tree arg1, tree arg2,
1378 : : tree (*elt_const_binop) (enum tree_code, tree, tree))
1379 : : {
1380 : 140785 : if (TREE_CODE (arg1) == VECTOR_CST && TREE_CODE (arg2) == VECTOR_CST
1381 : 335028 : && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1382 : : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1383 : : {
1384 : 134710 : tree type = TREE_TYPE (arg1);
1385 : 134710 : bool step_ok_p;
1386 : 134710 : if (VECTOR_CST_STEPPED_P (arg1)
1387 : 134710 : && VECTOR_CST_STEPPED_P (arg2))
1388 : : /* We can operate directly on the encoding if:
1389 : :
1390 : : a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1391 : : implies
1392 : : (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1393 : :
1394 : : Addition and subtraction are the supported operators
1395 : : for which this is true. */
1396 : 1970 : step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1397 : 132740 : else if (VECTOR_CST_STEPPED_P (arg1))
1398 : : /* We can operate directly on stepped encodings if:
1399 : :
1400 : : a3 - a2 == a2 - a1
1401 : : implies:
1402 : : (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1403 : :
1404 : : which is true if (x -> x op c) distributes over addition. */
1405 : 44590 : step_ok_p = distributes_over_addition_p (code, 1);
1406 : : else
1407 : : /* Similarly in reverse. */
1408 : 88150 : step_ok_p = distributes_over_addition_p (code, 2);
1409 : 134710 : tree_vector_builder elts;
1410 : 134710 : if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1411 : : return NULL_TREE;
1412 : 134710 : unsigned int count = elts.encoded_nelts ();
1413 : 519675 : for (unsigned int i = 0; i < count; ++i)
1414 : : {
1415 : 385292 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1416 : 385292 : tree elem2 = VECTOR_CST_ELT (arg2, i);
1417 : :
1418 : 385292 : tree elt = elt_const_binop (code, elem1, elem2);
1419 : :
1420 : : /* It is possible that const_binop cannot handle the given
1421 : : code and return NULL_TREE */
1422 : 385292 : if (elt == NULL_TREE)
1423 : 327 : return NULL_TREE;
1424 : 384965 : elts.quick_push (elt);
1425 : : }
1426 : :
1427 : 134383 : return elts.build ();
1428 : 134710 : }
1429 : :
1430 : 65608 : if (TREE_CODE (arg1) == VECTOR_CST
1431 : 6075 : && TREE_CODE (arg2) == INTEGER_CST)
1432 : : {
1433 : 6075 : tree type = TREE_TYPE (arg1);
1434 : 6075 : bool step_ok_p = distributes_over_addition_p (code, 1);
1435 : 6075 : tree_vector_builder elts;
1436 : 6075 : if (!elts.new_unary_operation (type, arg1, step_ok_p))
1437 : : return NULL_TREE;
1438 : 6075 : unsigned int count = elts.encoded_nelts ();
1439 : 30095 : for (unsigned int i = 0; i < count; ++i)
1440 : : {
1441 : 24107 : tree elem1 = VECTOR_CST_ELT (arg1, i);
1442 : :
1443 : 24107 : tree elt = elt_const_binop (code, elem1, arg2);
1444 : :
1445 : : /* It is possible that const_binop cannot handle the given
1446 : : code and return NULL_TREE. */
1447 : 24107 : if (elt == NULL_TREE)
1448 : 87 : return NULL_TREE;
1449 : 24020 : elts.quick_push (elt);
1450 : : }
1451 : :
1452 : 5988 : return elts.build ();
1453 : 6075 : }
1454 : : return NULL_TREE;
1455 : : }
1456 : :
1457 : : /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1458 : : constant. We assume ARG1 and ARG2 have the same data type, or at least
1459 : : are the same kind of constant and the same machine mode. Return zero if
1460 : : combining the constants is not allowed in the current operating mode. */
1461 : :
1462 : : static tree
1463 : 155904462 : const_binop (enum tree_code code, tree arg1, tree arg2)
1464 : : {
1465 : : /* Sanity check for the recursive cases. */
1466 : 155904462 : if (!arg1 || !arg2)
1467 : : return NULL_TREE;
1468 : :
1469 : 155903198 : STRIP_NOPS (arg1);
1470 : 155903198 : STRIP_NOPS (arg2);
1471 : :
1472 : 155903198 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1473 : : {
1474 : 150960730 : if (code == POINTER_PLUS_EXPR)
1475 : 97435 : return int_const_binop (PLUS_EXPR,
1476 : 194870 : arg1, fold_convert (TREE_TYPE (arg1), arg2));
1477 : :
1478 : 150863295 : return int_const_binop (code, arg1, arg2);
1479 : : }
1480 : :
1481 : 4942468 : if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1482 : : {
1483 : 4726884 : machine_mode mode;
1484 : 4726884 : REAL_VALUE_TYPE d1;
1485 : 4726884 : REAL_VALUE_TYPE d2;
1486 : 4726884 : REAL_VALUE_TYPE value;
1487 : 4726884 : REAL_VALUE_TYPE result;
1488 : 4726884 : bool inexact;
1489 : 4726884 : tree t, type;
1490 : :
1491 : : /* The following codes are handled by real_arithmetic. */
1492 : 4726884 : switch (code)
1493 : : {
1494 : 4726884 : case PLUS_EXPR:
1495 : 4726884 : case MINUS_EXPR:
1496 : 4726884 : case MULT_EXPR:
1497 : 4726884 : case RDIV_EXPR:
1498 : 4726884 : case MIN_EXPR:
1499 : 4726884 : case MAX_EXPR:
1500 : 4726884 : break;
1501 : :
1502 : : default:
1503 : : return NULL_TREE;
1504 : : }
1505 : :
1506 : 4726884 : d1 = TREE_REAL_CST (arg1);
1507 : 4726884 : d2 = TREE_REAL_CST (arg2);
1508 : :
1509 : 4726884 : type = TREE_TYPE (arg1);
1510 : 4726884 : mode = TYPE_MODE (type);
1511 : :
1512 : : /* Don't perform operation if we honor signaling NaNs and
1513 : : either operand is a signaling NaN. */
1514 : 4726884 : if (HONOR_SNANS (mode)
1515 : 4726884 : && (REAL_VALUE_ISSIGNALING_NAN (d1)
1516 : 3676 : || REAL_VALUE_ISSIGNALING_NAN (d2)))
1517 : 33 : return NULL_TREE;
1518 : :
1519 : : /* Don't perform operation if it would raise a division
1520 : : by zero exception. */
1521 : 4726851 : if (code == RDIV_EXPR
1522 : 2341019 : && real_equal (&d2, &dconst0)
1523 : 4737852 : && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1524 : 7729 : return NULL_TREE;
1525 : :
1526 : : /* If either operand is a NaN, just return it. Otherwise, set up
1527 : : for floating-point trap; we return an overflow. */
1528 : 4719122 : if (REAL_VALUE_ISNAN (d1))
1529 : : {
1530 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1531 : : is off. */
1532 : 225 : d1.signalling = 0;
1533 : 225 : t = build_real (type, d1);
1534 : 225 : return t;
1535 : : }
1536 : 4718897 : else if (REAL_VALUE_ISNAN (d2))
1537 : : {
1538 : : /* Make resulting NaN value to be qNaN when flag_signaling_nans
1539 : : is off. */
1540 : 62 : d2.signalling = 0;
1541 : 62 : t = build_real (type, d2);
1542 : 62 : return t;
1543 : : }
1544 : :
1545 : 4718835 : inexact = real_arithmetic (&value, code, &d1, &d2);
1546 : 4718835 : real_convert (&result, mode, &value);
1547 : :
1548 : : /* Don't constant fold this floating point operation if
1549 : : both operands are not NaN but the result is NaN, and
1550 : : flag_trapping_math. Such operations should raise an
1551 : : invalid operation exception. */
1552 : 4718835 : if (flag_trapping_math
1553 : 18440700 : && MODE_HAS_NANS (mode)
1554 : 4701439 : && REAL_VALUE_ISNAN (result)
1555 : 2432 : && !REAL_VALUE_ISNAN (d1)
1556 : 4721267 : && !REAL_VALUE_ISNAN (d2))
1557 : 2432 : return NULL_TREE;
1558 : :
1559 : : /* Don't constant fold this floating point operation if
1560 : : the result has overflowed and flag_trapping_math. */
1561 : 4716403 : if (flag_trapping_math
1562 : 18431278 : && MODE_HAS_INFINITIES (mode)
1563 : 4699007 : && REAL_VALUE_ISINF (result)
1564 : 7157 : && !REAL_VALUE_ISINF (d1)
1565 : 4722964 : && !REAL_VALUE_ISINF (d2))
1566 : 4278 : return NULL_TREE;
1567 : :
1568 : : /* Don't constant fold this floating point operation if the
1569 : : result may dependent upon the run-time rounding mode and
1570 : : flag_rounding_math is set, or if GCC's software emulation
1571 : : is unable to accurately represent the result. */
1572 : 4712125 : if ((flag_rounding_math
1573 : 32240201 : || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1574 : 4712125 : && (inexact || !real_identical (&result, &value)))
1575 : 1171 : return NULL_TREE;
1576 : :
1577 : 4710954 : t = build_real (type, result);
1578 : :
1579 : 4710954 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1580 : 4710954 : return t;
1581 : : }
1582 : :
1583 : 215584 : if (TREE_CODE (arg1) == FIXED_CST)
1584 : : {
1585 : 0 : FIXED_VALUE_TYPE f1;
1586 : 0 : FIXED_VALUE_TYPE f2;
1587 : 0 : FIXED_VALUE_TYPE result;
1588 : 0 : tree t, type;
1589 : 0 : bool sat_p;
1590 : 0 : bool overflow_p;
1591 : :
1592 : : /* The following codes are handled by fixed_arithmetic. */
1593 : 0 : switch (code)
1594 : : {
1595 : 0 : case PLUS_EXPR:
1596 : 0 : case MINUS_EXPR:
1597 : 0 : case MULT_EXPR:
1598 : 0 : case TRUNC_DIV_EXPR:
1599 : 0 : if (TREE_CODE (arg2) != FIXED_CST)
1600 : : return NULL_TREE;
1601 : 0 : f2 = TREE_FIXED_CST (arg2);
1602 : 0 : break;
1603 : :
1604 : 0 : case LSHIFT_EXPR:
1605 : 0 : case RSHIFT_EXPR:
1606 : 0 : {
1607 : 0 : if (TREE_CODE (arg2) != INTEGER_CST)
1608 : 0 : return NULL_TREE;
1609 : 0 : wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1610 : 0 : f2.data.high = w2.elt (1);
1611 : 0 : f2.data.low = w2.ulow ();
1612 : 0 : f2.mode = SImode;
1613 : : }
1614 : 0 : break;
1615 : :
1616 : : default:
1617 : : return NULL_TREE;
1618 : : }
1619 : :
1620 : 0 : f1 = TREE_FIXED_CST (arg1);
1621 : 0 : type = TREE_TYPE (arg1);
1622 : 0 : sat_p = TYPE_SATURATING (type);
1623 : 0 : overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1624 : 0 : t = build_fixed (type, result);
1625 : : /* Propagate overflow flags. */
1626 : 0 : if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1627 : 0 : TREE_OVERFLOW (t) = 1;
1628 : 0 : return t;
1629 : : }
1630 : :
1631 : 215584 : if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1632 : : {
1633 : 11016 : tree type = TREE_TYPE (arg1);
1634 : 11016 : tree r1 = TREE_REALPART (arg1);
1635 : 11016 : tree i1 = TREE_IMAGPART (arg1);
1636 : 11016 : tree r2 = TREE_REALPART (arg2);
1637 : 11016 : tree i2 = TREE_IMAGPART (arg2);
1638 : 11016 : tree real, imag;
1639 : :
1640 : 11016 : switch (code)
1641 : : {
1642 : 5176 : case PLUS_EXPR:
1643 : 5176 : case MINUS_EXPR:
1644 : 5176 : real = const_binop (code, r1, r2);
1645 : 5176 : imag = const_binop (code, i1, i2);
1646 : 5176 : break;
1647 : :
1648 : 3889 : case MULT_EXPR:
1649 : 3889 : if (COMPLEX_FLOAT_TYPE_P (type))
1650 : 2737 : return do_mpc_arg2 (arg1, arg2, type,
1651 : : /* do_nonfinite= */ folding_initializer,
1652 : 2737 : mpc_mul);
1653 : :
1654 : 1152 : real = const_binop (MINUS_EXPR,
1655 : : const_binop (MULT_EXPR, r1, r2),
1656 : : const_binop (MULT_EXPR, i1, i2));
1657 : 1152 : imag = const_binop (PLUS_EXPR,
1658 : : const_binop (MULT_EXPR, r1, i2),
1659 : : const_binop (MULT_EXPR, i1, r2));
1660 : 1152 : break;
1661 : :
1662 : 1697 : case RDIV_EXPR:
1663 : 1697 : if (COMPLEX_FLOAT_TYPE_P (type))
1664 : 1697 : return do_mpc_arg2 (arg1, arg2, type,
1665 : : /* do_nonfinite= */ folding_initializer,
1666 : 1697 : mpc_div);
1667 : : /* Fallthru. */
1668 : 254 : case TRUNC_DIV_EXPR:
1669 : 254 : case CEIL_DIV_EXPR:
1670 : 254 : case FLOOR_DIV_EXPR:
1671 : 254 : case ROUND_DIV_EXPR:
1672 : 254 : if (flag_complex_method == 0)
1673 : : {
1674 : : /* Keep this algorithm in sync with
1675 : : tree-complex.cc:expand_complex_div_straight().
1676 : :
1677 : : Expand complex division to scalars, straightforward algorithm.
1678 : : a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1679 : : t = br*br + bi*bi
1680 : : */
1681 : 0 : tree magsquared
1682 : 0 : = const_binop (PLUS_EXPR,
1683 : : const_binop (MULT_EXPR, r2, r2),
1684 : : const_binop (MULT_EXPR, i2, i2));
1685 : 0 : tree t1
1686 : 0 : = const_binop (PLUS_EXPR,
1687 : : const_binop (MULT_EXPR, r1, r2),
1688 : : const_binop (MULT_EXPR, i1, i2));
1689 : 0 : tree t2
1690 : 0 : = const_binop (MINUS_EXPR,
1691 : : const_binop (MULT_EXPR, i1, r2),
1692 : : const_binop (MULT_EXPR, r1, i2));
1693 : :
1694 : 0 : real = const_binop (code, t1, magsquared);
1695 : 0 : imag = const_binop (code, t2, magsquared);
1696 : : }
1697 : : else
1698 : : {
1699 : : /* Keep this algorithm in sync with
1700 : : tree-complex.cc:expand_complex_div_wide().
1701 : :
1702 : : Expand complex division to scalars, modified algorithm to minimize
1703 : : overflow with wide input ranges. */
1704 : 254 : tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1705 : : fold_abs_const (r2, TREE_TYPE (type)),
1706 : : fold_abs_const (i2, TREE_TYPE (type)));
1707 : :
1708 : 254 : if (integer_nonzerop (compare))
1709 : : {
1710 : : /* In the TRUE branch, we compute
1711 : : ratio = br/bi;
1712 : : div = (br * ratio) + bi;
1713 : : tr = (ar * ratio) + ai;
1714 : : ti = (ai * ratio) - ar;
1715 : : tr = tr / div;
1716 : : ti = ti / div; */
1717 : 48 : tree ratio = const_binop (code, r2, i2);
1718 : 48 : tree div = const_binop (PLUS_EXPR, i2,
1719 : : const_binop (MULT_EXPR, r2, ratio));
1720 : 48 : real = const_binop (MULT_EXPR, r1, ratio);
1721 : 48 : real = const_binop (PLUS_EXPR, real, i1);
1722 : 48 : real = const_binop (code, real, div);
1723 : :
1724 : 48 : imag = const_binop (MULT_EXPR, i1, ratio);
1725 : 48 : imag = const_binop (MINUS_EXPR, imag, r1);
1726 : 48 : imag = const_binop (code, imag, div);
1727 : : }
1728 : : else
1729 : : {
1730 : : /* In the FALSE branch, we compute
1731 : : ratio = d/c;
1732 : : divisor = (d * ratio) + c;
1733 : : tr = (b * ratio) + a;
1734 : : ti = b - (a * ratio);
1735 : : tr = tr / div;
1736 : : ti = ti / div; */
1737 : 206 : tree ratio = const_binop (code, i2, r2);
1738 : 206 : tree div = const_binop (PLUS_EXPR, r2,
1739 : : const_binop (MULT_EXPR, i2, ratio));
1740 : :
1741 : 206 : real = const_binop (MULT_EXPR, i1, ratio);
1742 : 206 : real = const_binop (PLUS_EXPR, real, r1);
1743 : 206 : real = const_binop (code, real, div);
1744 : :
1745 : 206 : imag = const_binop (MULT_EXPR, r1, ratio);
1746 : 206 : imag = const_binop (MINUS_EXPR, i1, imag);
1747 : 206 : imag = const_binop (code, imag, div);
1748 : : }
1749 : : }
1750 : : break;
1751 : :
1752 : : default:
1753 : : return NULL_TREE;
1754 : : }
1755 : :
1756 : 6582 : if (real && imag)
1757 : 6424 : return build_complex (type, real, imag);
1758 : : }
1759 : :
1760 : 204726 : tree simplified;
1761 : 204726 : if ((simplified = simplify_const_binop (code, arg1, arg2, 0)))
1762 : : return simplified;
1763 : :
1764 : 204512 : if (commutative_tree_code (code)
1765 : 204512 : && (simplified = simplify_const_binop (code, arg2, arg1, 1)))
1766 : : return simplified;
1767 : :
1768 : 200318 : return vector_const_binop (code, arg1, arg2, const_binop);
1769 : : }
1770 : :
1771 : : /* Overload that adds a TYPE parameter to be able to dispatch
1772 : : to fold_relational_const. */
1773 : :
1774 : : tree
1775 : 200481738 : const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1776 : : {
1777 : 200481738 : if (TREE_CODE_CLASS (code) == tcc_comparison)
1778 : 51345757 : return fold_relational_const (code, type, arg1, arg2);
1779 : :
1780 : : /* ??? Until we make the const_binop worker take the type of the
1781 : : result as argument put those cases that need it here. */
1782 : 149135981 : switch (code)
1783 : : {
1784 : 18 : case VEC_SERIES_EXPR:
1785 : 18 : if (CONSTANT_CLASS_P (arg1)
1786 : 18 : && CONSTANT_CLASS_P (arg2))
1787 : 18 : return build_vec_series (type, arg1, arg2);
1788 : : return NULL_TREE;
1789 : :
1790 : 282553 : case COMPLEX_EXPR:
1791 : 282553 : if ((TREE_CODE (arg1) == REAL_CST
1792 : 271966 : && TREE_CODE (arg2) == REAL_CST)
1793 : 10588 : || (TREE_CODE (arg1) == INTEGER_CST
1794 : 10587 : && TREE_CODE (arg2) == INTEGER_CST))
1795 : 282552 : return build_complex (type, arg1, arg2);
1796 : : return NULL_TREE;
1797 : :
1798 : 6033 : case POINTER_DIFF_EXPR:
1799 : 6033 : if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1800 : : {
1801 : 11434 : poly_offset_int res = (wi::to_poly_offset (arg1)
1802 : 5717 : - wi::to_poly_offset (arg2));
1803 : 5717 : return force_fit_type (type, res, 1,
1804 : 5717 : TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1805 : : }
1806 : : return NULL_TREE;
1807 : :
1808 : 8707 : case VEC_PACK_TRUNC_EXPR:
1809 : 8707 : case VEC_PACK_FIX_TRUNC_EXPR:
1810 : 8707 : case VEC_PACK_FLOAT_EXPR:
1811 : 8707 : {
1812 : 8707 : unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1813 : :
1814 : 8707 : if (TREE_CODE (arg1) != VECTOR_CST
1815 : 8707 : || TREE_CODE (arg2) != VECTOR_CST)
1816 : : return NULL_TREE;
1817 : :
1818 : 8707 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1819 : : return NULL_TREE;
1820 : :
1821 : 8707 : out_nelts = in_nelts * 2;
1822 : 8707 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1823 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1824 : :
1825 : 8707 : tree_vector_builder elts (type, out_nelts, 1);
1826 : 116527 : for (i = 0; i < out_nelts; i++)
1827 : : {
1828 : 107838 : tree elt = (i < in_nelts
1829 : 107838 : ? VECTOR_CST_ELT (arg1, i)
1830 : 53910 : : VECTOR_CST_ELT (arg2, i - in_nelts));
1831 : 108572 : elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1832 : : ? NOP_EXPR
1833 : : : code == VEC_PACK_FLOAT_EXPR
1834 : 734 : ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1835 : 107838 : TREE_TYPE (type), elt);
1836 : 107838 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1837 : 18 : return NULL_TREE;
1838 : 107820 : elts.quick_push (elt);
1839 : : }
1840 : :
1841 : 8689 : return elts.build ();
1842 : 8707 : }
1843 : :
1844 : 198 : case VEC_WIDEN_MULT_LO_EXPR:
1845 : 198 : case VEC_WIDEN_MULT_HI_EXPR:
1846 : 198 : case VEC_WIDEN_MULT_EVEN_EXPR:
1847 : 198 : case VEC_WIDEN_MULT_ODD_EXPR:
1848 : 198 : {
1849 : 198 : unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1850 : :
1851 : 198 : if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1852 : : return NULL_TREE;
1853 : :
1854 : 198 : if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1855 : : return NULL_TREE;
1856 : 198 : out_nelts = in_nelts / 2;
1857 : 198 : gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1858 : : && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1859 : :
1860 : 198 : if (code == VEC_WIDEN_MULT_LO_EXPR)
1861 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1862 : : else if (code == VEC_WIDEN_MULT_HI_EXPR)
1863 : : scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1864 : : else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1865 : : scale = 1, ofs = 0;
1866 : : else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1867 : 198 : scale = 1, ofs = 1;
1868 : :
1869 : 198 : tree_vector_builder elts (type, out_nelts, 1);
1870 : 682 : for (out = 0; out < out_nelts; out++)
1871 : : {
1872 : 484 : unsigned int in = (out << scale) + ofs;
1873 : 484 : tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1874 : : VECTOR_CST_ELT (arg1, in));
1875 : 484 : tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1876 : : VECTOR_CST_ELT (arg2, in));
1877 : :
1878 : 484 : if (t1 == NULL_TREE || t2 == NULL_TREE)
1879 : 0 : return NULL_TREE;
1880 : 484 : tree elt = const_binop (MULT_EXPR, t1, t2);
1881 : 484 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1882 : : return NULL_TREE;
1883 : 484 : elts.quick_push (elt);
1884 : : }
1885 : :
1886 : 198 : return elts.build ();
1887 : 198 : }
1888 : :
1889 : 148838472 : default:;
1890 : : }
1891 : :
1892 : 148838472 : if (TREE_CODE_CLASS (code) != tcc_binary)
1893 : : return NULL_TREE;
1894 : :
1895 : : /* Make sure type and arg0 have the same saturating flag. */
1896 : 146410222 : gcc_checking_assert (TYPE_SATURATING (type)
1897 : : == TYPE_SATURATING (TREE_TYPE (arg1)));
1898 : :
1899 : 146410222 : return const_binop (code, arg1, arg2);
1900 : : }
1901 : :
1902 : : /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1903 : : Return zero if computing the constants is not possible. */
1904 : :
1905 : : tree
1906 : 268257844 : const_unop (enum tree_code code, tree type, tree arg0)
1907 : : {
1908 : : /* Don't perform the operation, other than NEGATE and ABS, if
1909 : : flag_signaling_nans is on and the operand is a signaling NaN. */
1910 : 268257844 : if (TREE_CODE (arg0) == REAL_CST
1911 : 10601508 : && HONOR_SNANS (arg0)
1912 : 7191 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1913 : 454 : && code != NEGATE_EXPR
1914 : 454 : && code != ABS_EXPR
1915 : 268258263 : && code != ABSU_EXPR)
1916 : : return NULL_TREE;
1917 : :
1918 : 268257425 : switch (code)
1919 : : {
1920 : 183594275 : CASE_CONVERT:
1921 : 183594275 : case FLOAT_EXPR:
1922 : 183594275 : case FIX_TRUNC_EXPR:
1923 : 183594275 : case FIXED_CONVERT_EXPR:
1924 : 183594275 : return fold_convert_const (code, type, arg0);
1925 : :
1926 : 0 : case ADDR_SPACE_CONVERT_EXPR:
1927 : : /* If the source address is 0, and the source address space
1928 : : cannot have a valid object at 0, fold to dest type null. */
1929 : 0 : if (integer_zerop (arg0)
1930 : 0 : && !(targetm.addr_space.zero_address_valid
1931 : 0 : (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1932 : 0 : return fold_convert_const (code, type, arg0);
1933 : : break;
1934 : :
1935 : 11755013 : case VIEW_CONVERT_EXPR:
1936 : 11755013 : return fold_view_convert_expr (type, arg0);
1937 : :
1938 : 27635175 : case NEGATE_EXPR:
1939 : 27635175 : {
1940 : : /* Can't call fold_negate_const directly here as that doesn't
1941 : : handle all cases and we might not be able to negate some
1942 : : constants. */
1943 : 27635175 : tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1944 : 27635175 : if (tem && CONSTANT_CLASS_P (tem))
1945 : : return tem;
1946 : : break;
1947 : : }
1948 : :
1949 : 31354 : case ABS_EXPR:
1950 : 31354 : case ABSU_EXPR:
1951 : 31354 : if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1952 : 31083 : return fold_abs_const (arg0, type);
1953 : : break;
1954 : :
1955 : 24552 : case CONJ_EXPR:
1956 : 24552 : if (TREE_CODE (arg0) == COMPLEX_CST)
1957 : : {
1958 : 24549 : tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1959 : 24549 : TREE_TYPE (type));
1960 : 24549 : return build_complex (type, TREE_REALPART (arg0), ipart);
1961 : : }
1962 : : break;
1963 : :
1964 : 2218655 : case BIT_NOT_EXPR:
1965 : 2218655 : if (TREE_CODE (arg0) == INTEGER_CST)
1966 : 2217763 : return fold_not_const (arg0, type);
1967 : 892 : else if (POLY_INT_CST_P (arg0))
1968 : : return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1969 : : /* Perform BIT_NOT_EXPR on each element individually. */
1970 : 892 : else if (TREE_CODE (arg0) == VECTOR_CST)
1971 : : {
1972 : 288 : tree elem;
1973 : :
1974 : : /* This can cope with stepped encodings because ~x == -1 - x. */
1975 : 288 : tree_vector_builder elements;
1976 : 288 : elements.new_unary_operation (type, arg0, true);
1977 : 288 : unsigned int i, count = elements.encoded_nelts ();
1978 : 1622 : for (i = 0; i < count; ++i)
1979 : : {
1980 : 1334 : elem = VECTOR_CST_ELT (arg0, i);
1981 : 1334 : elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1982 : 1334 : if (elem == NULL_TREE)
1983 : : break;
1984 : 1334 : elements.quick_push (elem);
1985 : : }
1986 : 288 : if (i == count)
1987 : 288 : return elements.build ();
1988 : 288 : }
1989 : : break;
1990 : :
1991 : 8640685 : case TRUTH_NOT_EXPR:
1992 : 8640685 : if (TREE_CODE (arg0) == INTEGER_CST)
1993 : 8346333 : return constant_boolean_node (integer_zerop (arg0), type);
1994 : : break;
1995 : :
1996 : 194033 : case REALPART_EXPR:
1997 : 194033 : if (TREE_CODE (arg0) == COMPLEX_CST)
1998 : 193841 : return fold_convert (type, TREE_REALPART (arg0));
1999 : : break;
2000 : :
2001 : 197872 : case IMAGPART_EXPR:
2002 : 197872 : if (TREE_CODE (arg0) == COMPLEX_CST)
2003 : 197693 : return fold_convert (type, TREE_IMAGPART (arg0));
2004 : : break;
2005 : :
2006 : 8842 : case VEC_UNPACK_LO_EXPR:
2007 : 8842 : case VEC_UNPACK_HI_EXPR:
2008 : 8842 : case VEC_UNPACK_FLOAT_LO_EXPR:
2009 : 8842 : case VEC_UNPACK_FLOAT_HI_EXPR:
2010 : 8842 : case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
2011 : 8842 : case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
2012 : 8842 : {
2013 : 8842 : unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
2014 : 8842 : enum tree_code subcode;
2015 : :
2016 : 8842 : if (TREE_CODE (arg0) != VECTOR_CST)
2017 : : return NULL_TREE;
2018 : :
2019 : 8842 : if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
2020 : : return NULL_TREE;
2021 : 8842 : out_nelts = in_nelts / 2;
2022 : 8842 : gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
2023 : :
2024 : 8842 : unsigned int offset = 0;
2025 : 8842 : if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
2026 : 8842 : || code == VEC_UNPACK_FLOAT_LO_EXPR
2027 : : || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
2028 : 4419 : offset = out_nelts;
2029 : :
2030 : 8842 : if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
2031 : : subcode = NOP_EXPR;
2032 : 4760 : else if (code == VEC_UNPACK_FLOAT_LO_EXPR
2033 : 4760 : || code == VEC_UNPACK_FLOAT_HI_EXPR)
2034 : : subcode = FLOAT_EXPR;
2035 : : else
2036 : 0 : subcode = FIX_TRUNC_EXPR;
2037 : :
2038 : 8842 : tree_vector_builder elts (type, out_nelts, 1);
2039 : 44194 : for (i = 0; i < out_nelts; i++)
2040 : : {
2041 : 35352 : tree elt = fold_convert_const (subcode, TREE_TYPE (type),
2042 : 35352 : VECTOR_CST_ELT (arg0, i + offset));
2043 : 35352 : if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
2044 : 0 : return NULL_TREE;
2045 : 35352 : elts.quick_push (elt);
2046 : : }
2047 : :
2048 : 8842 : return elts.build ();
2049 : 8842 : }
2050 : :
2051 : 4 : case VEC_DUPLICATE_EXPR:
2052 : 4 : if (CONSTANT_CLASS_P (arg0))
2053 : 4 : return build_vector_from_val (type, arg0);
2054 : : return NULL_TREE;
2055 : :
2056 : : default:
2057 : : break;
2058 : : }
2059 : :
2060 : : return NULL_TREE;
2061 : : }
2062 : :
2063 : : /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
2064 : : indicates which particular sizetype to create. */
2065 : :
2066 : : tree
2067 : 2984575292 : size_int_kind (poly_int64 number, enum size_type_kind kind)
2068 : : {
2069 : 2984575292 : return build_int_cst (sizetype_tab[(int) kind], number);
2070 : : }
2071 : :
2072 : : /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2073 : : is a tree code. The type of the result is taken from the operands.
2074 : : Both must be equivalent integer types, ala int_binop_types_match_p.
2075 : : If the operands are constant, so is the result. */
2076 : :
2077 : : tree
2078 : 1971478504 : size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2079 : : {
2080 : 1971478504 : tree type = TREE_TYPE (arg0);
2081 : :
2082 : 1971478504 : if (arg0 == error_mark_node || arg1 == error_mark_node)
2083 : : return error_mark_node;
2084 : :
2085 : 1971478504 : gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2086 : : TREE_TYPE (arg1)));
2087 : :
2088 : : /* Handle the special case of two poly_int constants faster. */
2089 : 1971478504 : if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
2090 : : {
2091 : : /* And some specific cases even faster than that. */
2092 : 1948496074 : if (code == PLUS_EXPR)
2093 : : {
2094 : 885529799 : if (integer_zerop (arg0)
2095 : 885529799 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2096 : : return arg1;
2097 : 242144392 : if (integer_zerop (arg1)
2098 : 242144392 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2099 : : return arg0;
2100 : : }
2101 : 1062966275 : else if (code == MINUS_EXPR)
2102 : : {
2103 : 93449989 : if (integer_zerop (arg1)
2104 : 93449989 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
2105 : : return arg0;
2106 : : }
2107 : 969516286 : else if (code == MULT_EXPR)
2108 : : {
2109 : 425289729 : if (integer_onep (arg0)
2110 : 425289729 : && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
2111 : : return arg1;
2112 : : }
2113 : :
2114 : : /* Handle general case of two integer constants. For sizetype
2115 : : constant calculations we always want to know about overflow,
2116 : : even in the unsigned case. */
2117 : 969815765 : tree res = int_const_binop (code, arg0, arg1, -1);
2118 : 969815765 : if (res != NULL_TREE)
2119 : : return res;
2120 : : }
2121 : :
2122 : 22982430 : return fold_build2_loc (loc, code, type, arg0, arg1);
2123 : : }
2124 : :
2125 : : /* Given two values, either both of sizetype or both of bitsizetype,
2126 : : compute the difference between the two values. Return the value
2127 : : in signed type corresponding to the type of the operands. */
2128 : :
2129 : : tree
2130 : 31990953 : size_diffop_loc (location_t loc, tree arg0, tree arg1)
2131 : : {
2132 : 31990953 : tree type = TREE_TYPE (arg0);
2133 : 31990953 : tree ctype;
2134 : :
2135 : 31990953 : gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2136 : : TREE_TYPE (arg1)));
2137 : :
2138 : : /* If the type is already signed, just do the simple thing. */
2139 : 31990953 : if (!TYPE_UNSIGNED (type))
2140 : 9307394 : return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2141 : :
2142 : 22683559 : if (type == sizetype)
2143 : 22683559 : ctype = ssizetype;
2144 : 0 : else if (type == bitsizetype)
2145 : 0 : ctype = sbitsizetype;
2146 : : else
2147 : 0 : ctype = signed_type_for (type);
2148 : :
2149 : : /* If either operand is not a constant, do the conversions to the signed
2150 : : type and subtract. The hardware will do the right thing with any
2151 : : overflow in the subtraction. */
2152 : 22683559 : if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2153 : 8421 : return size_binop_loc (loc, MINUS_EXPR,
2154 : : fold_convert_loc (loc, ctype, arg0),
2155 : 8421 : fold_convert_loc (loc, ctype, arg1));
2156 : :
2157 : : /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2158 : : Otherwise, subtract the other way, convert to CTYPE (we know that can't
2159 : : overflow) and negate (which can't either). Special-case a result
2160 : : of zero while we're here. */
2161 : 22675138 : if (tree_int_cst_equal (arg0, arg1))
2162 : 19464422 : return build_int_cst (ctype, 0);
2163 : 3210716 : else if (tree_int_cst_lt (arg1, arg0))
2164 : 2083146 : return fold_convert_loc (loc, ctype,
2165 : 2083146 : size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2166 : : else
2167 : 1127570 : return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2168 : : fold_convert_loc (loc, ctype,
2169 : : size_binop_loc (loc,
2170 : : MINUS_EXPR,
2171 : : arg1, arg0)));
2172 : : }
2173 : :
2174 : : /* A subroutine of fold_convert_const handling conversions of an
2175 : : INTEGER_CST to another integer type. */
2176 : :
2177 : : static tree
2178 : 1101520819 : fold_convert_const_int_from_int (tree type, const_tree arg1)
2179 : : {
2180 : : /* Given an integer constant, make new constant with new type,
2181 : : appropriately sign-extended or truncated. Use widest_int
2182 : : so that any extension is done according ARG1's type. */
2183 : 1101520819 : tree arg1_type = TREE_TYPE (arg1);
2184 : 1101520819 : unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type));
2185 : 1101520819 : return force_fit_type (type, wide_int::from (wi::to_wide (arg1), prec,
2186 : 1101520819 : TYPE_SIGN (arg1_type)),
2187 : 1101520819 : !POINTER_TYPE_P (TREE_TYPE (arg1)),
2188 : 1101520819 : TREE_OVERFLOW (arg1));
2189 : : }
2190 : :
2191 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2192 : : to an integer type. */
2193 : :
2194 : : static tree
2195 : 34920 : fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2196 : : {
2197 : 34920 : bool overflow = false;
2198 : 34920 : tree t;
2199 : :
2200 : : /* The following code implements the floating point to integer
2201 : : conversion rules required by the Java Language Specification,
2202 : : that IEEE NaNs are mapped to zero and values that overflow
2203 : : the target precision saturate, i.e. values greater than
2204 : : INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2205 : : are mapped to INT_MIN. These semantics are allowed by the
2206 : : C and C++ standards that simply state that the behavior of
2207 : : FP-to-integer conversion is unspecified upon overflow. */
2208 : :
2209 : 34920 : wide_int val;
2210 : 34920 : REAL_VALUE_TYPE r;
2211 : 34920 : REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2212 : :
2213 : 34920 : switch (code)
2214 : : {
2215 : 34920 : case FIX_TRUNC_EXPR:
2216 : 34920 : real_trunc (&r, VOIDmode, &x);
2217 : 34920 : break;
2218 : :
2219 : 0 : default:
2220 : 0 : gcc_unreachable ();
2221 : : }
2222 : :
2223 : : /* If R is NaN, return zero and show we have an overflow. */
2224 : 34920 : if (REAL_VALUE_ISNAN (r))
2225 : : {
2226 : 3638 : overflow = true;
2227 : 3638 : val = wi::zero (TYPE_PRECISION (type));
2228 : : }
2229 : :
2230 : : /* See if R is less than the lower bound or greater than the
2231 : : upper bound. */
2232 : :
2233 : 34920 : if (! overflow)
2234 : : {
2235 : 31282 : tree lt = TYPE_MIN_VALUE (type);
2236 : 31282 : REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2237 : 31282 : if (real_less (&r, &l))
2238 : : {
2239 : 1974 : overflow = true;
2240 : 1974 : val = wi::to_wide (lt);
2241 : : }
2242 : : }
2243 : :
2244 : 34920 : if (! overflow)
2245 : : {
2246 : 29308 : tree ut = TYPE_MAX_VALUE (type);
2247 : 29308 : if (ut)
2248 : : {
2249 : 29308 : REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2250 : 29308 : if (real_less (&u, &r))
2251 : : {
2252 : 1927 : overflow = true;
2253 : 1927 : val = wi::to_wide (ut);
2254 : : }
2255 : : }
2256 : : }
2257 : :
2258 : 34920 : if (! overflow)
2259 : 27381 : val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2260 : :
2261 : : /* According to IEEE standard, for conversions from floating point to
2262 : : integer. When a NaN or infinite operand cannot be represented in the
2263 : : destination format and this cannot otherwise be indicated, the invalid
2264 : : operation exception shall be signaled. When a numeric operand would
2265 : : convert to an integer outside the range of the destination format, the
2266 : : invalid operation exception shall be signaled if this situation cannot
2267 : : otherwise be indicated. */
2268 : 34920 : if (!flag_trapping_math || !overflow)
2269 : 27637 : t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2270 : : else
2271 : : t = NULL_TREE;
2272 : :
2273 : 34920 : return t;
2274 : 34920 : }
2275 : :
2276 : : /* A subroutine of fold_convert_const handling conversions of a
2277 : : FIXED_CST to an integer type. */
2278 : :
2279 : : static tree
2280 : 0 : fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2281 : : {
2282 : 0 : tree t;
2283 : 0 : double_int temp, temp_trunc;
2284 : 0 : scalar_mode mode;
2285 : :
2286 : : /* Right shift FIXED_CST to temp by fbit. */
2287 : 0 : temp = TREE_FIXED_CST (arg1).data;
2288 : 0 : mode = TREE_FIXED_CST (arg1).mode;
2289 : 0 : if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2290 : : {
2291 : 0 : temp = temp.rshift (GET_MODE_FBIT (mode),
2292 : : HOST_BITS_PER_DOUBLE_INT,
2293 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2294 : :
2295 : : /* Left shift temp to temp_trunc by fbit. */
2296 : 0 : temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2297 : : HOST_BITS_PER_DOUBLE_INT,
2298 : 0 : SIGNED_FIXED_POINT_MODE_P (mode));
2299 : : }
2300 : : else
2301 : : {
2302 : 0 : temp = double_int_zero;
2303 : 0 : temp_trunc = double_int_zero;
2304 : : }
2305 : :
2306 : : /* If FIXED_CST is negative, we need to round the value toward 0.
2307 : : By checking if the fractional bits are not zero to add 1 to temp. */
2308 : 0 : if (SIGNED_FIXED_POINT_MODE_P (mode)
2309 : 0 : && temp_trunc.is_negative ()
2310 : 0 : && TREE_FIXED_CST (arg1).data != temp_trunc)
2311 : 0 : temp += double_int_one;
2312 : :
2313 : : /* Given a fixed-point constant, make new constant with new type,
2314 : : appropriately sign-extended or truncated. */
2315 : 0 : t = force_fit_type (type, temp, -1,
2316 : 0 : (temp.is_negative ()
2317 : 0 : && (TYPE_UNSIGNED (type)
2318 : 0 : < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2319 : 0 : | TREE_OVERFLOW (arg1));
2320 : :
2321 : 0 : return t;
2322 : : }
2323 : :
2324 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2325 : : to another floating point type. */
2326 : :
2327 : : static tree
2328 : 1953844 : fold_convert_const_real_from_real (tree type, const_tree arg1)
2329 : : {
2330 : 1953844 : REAL_VALUE_TYPE value;
2331 : 1953844 : tree t;
2332 : :
2333 : : /* If the underlying modes are the same, simply treat it as
2334 : : copy and rebuild with TREE_REAL_CST information and the
2335 : : given type. */
2336 : 1953844 : if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2337 : : {
2338 : 93218 : t = build_real (type, TREE_REAL_CST (arg1));
2339 : 93218 : return t;
2340 : : }
2341 : :
2342 : : /* Don't perform the operation if flag_signaling_nans is on
2343 : : and the operand is a signaling NaN. */
2344 : 1860626 : if (HONOR_SNANS (arg1)
2345 : 1860974 : && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2346 : : return NULL_TREE;
2347 : :
2348 : : /* With flag_rounding_math we should respect the current rounding mode
2349 : : unless the conversion is exact. */
2350 : 1860626 : if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2351 : 1861270 : && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2352 : 509 : return NULL_TREE;
2353 : :
2354 : 1860117 : real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2355 : 1860117 : t = build_real (type, value);
2356 : :
2357 : : /* If converting an infinity or NAN to a representation that doesn't
2358 : : have one, set the overflow bit so that we can produce some kind of
2359 : : error message at the appropriate point if necessary. It's not the
2360 : : most user-friendly message, but it's better than nothing. */
2361 : 1860117 : if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2362 : 1967520 : && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2363 : 0 : TREE_OVERFLOW (t) = 1;
2364 : 1860117 : else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2365 : 1961806 : && !MODE_HAS_NANS (TYPE_MODE (type)))
2366 : 0 : TREE_OVERFLOW (t) = 1;
2367 : : /* Regular overflow, conversion produced an infinity in a mode that
2368 : : can't represent them. */
2369 : 9297117 : else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2370 : 0 : && REAL_VALUE_ISINF (value)
2371 : 1860117 : && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2372 : 0 : TREE_OVERFLOW (t) = 1;
2373 : : else
2374 : 1860117 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2375 : : return t;
2376 : : }
2377 : :
2378 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2379 : : to a floating point type. */
2380 : :
2381 : : static tree
2382 : 0 : fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2383 : : {
2384 : 0 : REAL_VALUE_TYPE value;
2385 : 0 : tree t;
2386 : :
2387 : 0 : real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2388 : 0 : &TREE_FIXED_CST (arg1));
2389 : 0 : t = build_real (type, value);
2390 : :
2391 : 0 : TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2392 : 0 : return t;
2393 : : }
2394 : :
2395 : : /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2396 : : to another fixed-point type. */
2397 : :
2398 : : static tree
2399 : 0 : fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2400 : : {
2401 : 0 : FIXED_VALUE_TYPE value;
2402 : 0 : tree t;
2403 : 0 : bool overflow_p;
2404 : :
2405 : 0 : overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2406 : 0 : &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2407 : 0 : t = build_fixed (type, value);
2408 : :
2409 : : /* Propagate overflow flags. */
2410 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2411 : 0 : TREE_OVERFLOW (t) = 1;
2412 : 0 : return t;
2413 : : }
2414 : :
2415 : : /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2416 : : to a fixed-point type. */
2417 : :
2418 : : static tree
2419 : 0 : fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2420 : : {
2421 : 0 : FIXED_VALUE_TYPE value;
2422 : 0 : tree t;
2423 : 0 : bool overflow_p;
2424 : 0 : double_int di;
2425 : :
2426 : 0 : gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2427 : :
2428 : 0 : di.low = TREE_INT_CST_ELT (arg1, 0);
2429 : 0 : if (TREE_INT_CST_NUNITS (arg1) == 1)
2430 : 0 : di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2431 : : else
2432 : 0 : di.high = TREE_INT_CST_ELT (arg1, 1);
2433 : :
2434 : 0 : overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2435 : 0 : TYPE_UNSIGNED (TREE_TYPE (arg1)),
2436 : 0 : TYPE_SATURATING (type));
2437 : 0 : t = build_fixed (type, value);
2438 : :
2439 : : /* Propagate overflow flags. */
2440 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2441 : 0 : TREE_OVERFLOW (t) = 1;
2442 : 0 : return t;
2443 : : }
2444 : :
2445 : : /* A subroutine of fold_convert_const handling conversions a REAL_CST
2446 : : to a fixed-point type. */
2447 : :
2448 : : static tree
2449 : 0 : fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2450 : : {
2451 : 0 : FIXED_VALUE_TYPE value;
2452 : 0 : tree t;
2453 : 0 : bool overflow_p;
2454 : :
2455 : 0 : overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2456 : 0 : &TREE_REAL_CST (arg1),
2457 : 0 : TYPE_SATURATING (type));
2458 : 0 : t = build_fixed (type, value);
2459 : :
2460 : : /* Propagate overflow flags. */
2461 : 0 : if (overflow_p | TREE_OVERFLOW (arg1))
2462 : 0 : TREE_OVERFLOW (t) = 1;
2463 : 0 : return t;
2464 : : }
2465 : :
2466 : : /* Attempt to fold type conversion operation CODE of expression ARG1 to
2467 : : type TYPE. If no simplification can be done return NULL_TREE. */
2468 : :
2469 : : static tree
2470 : 1159184153 : fold_convert_const (enum tree_code code, tree type, tree arg1)
2471 : : {
2472 : 1159184153 : tree arg_type = TREE_TYPE (arg1);
2473 : 1159184153 : if (arg_type == type)
2474 : : return arg1;
2475 : :
2476 : : /* We can't widen types, since the runtime value could overflow the
2477 : : original type before being extended to the new type. */
2478 : 1148685597 : if (POLY_INT_CST_P (arg1)
2479 : : && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2480 : : && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2481 : : return build_poly_int_cst (type,
2482 : : poly_wide_int::from (poly_int_cst_value (arg1),
2483 : : TYPE_PRECISION (type),
2484 : : TYPE_SIGN (arg_type)));
2485 : :
2486 : 1148685597 : if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2487 : : || TREE_CODE (type) == OFFSET_TYPE)
2488 : : {
2489 : 1117769484 : if (TREE_CODE (arg1) == INTEGER_CST)
2490 : 1101520819 : return fold_convert_const_int_from_int (type, arg1);
2491 : 16248665 : else if (TREE_CODE (arg1) == REAL_CST)
2492 : 34920 : return fold_convert_const_int_from_real (code, type, arg1);
2493 : 16213745 : else if (TREE_CODE (arg1) == FIXED_CST)
2494 : 0 : return fold_convert_const_int_from_fixed (type, arg1);
2495 : : }
2496 : : else if (SCALAR_FLOAT_TYPE_P (type))
2497 : : {
2498 : 30873962 : if (TREE_CODE (arg1) == INTEGER_CST)
2499 : : {
2500 : 23943741 : tree res = build_real_from_int_cst (type, arg1);
2501 : : /* Avoid the folding if flag_rounding_math is on and the
2502 : : conversion is not exact. */
2503 : 23943741 : if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2504 : : {
2505 : 2498 : bool fail = false;
2506 : 4996 : wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2507 : 2498 : TYPE_PRECISION (TREE_TYPE (arg1)));
2508 : 2498 : if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2509 : 1474 : return NULL_TREE;
2510 : 2498 : }
2511 : 23942267 : return res;
2512 : : }
2513 : 6930221 : else if (TREE_CODE (arg1) == REAL_CST)
2514 : 1953844 : return fold_convert_const_real_from_real (type, arg1);
2515 : 4976377 : else if (TREE_CODE (arg1) == FIXED_CST)
2516 : 0 : return fold_convert_const_real_from_fixed (type, arg1);
2517 : : }
2518 : : else if (FIXED_POINT_TYPE_P (type))
2519 : : {
2520 : 0 : if (TREE_CODE (arg1) == FIXED_CST)
2521 : 0 : return fold_convert_const_fixed_from_fixed (type, arg1);
2522 : 0 : else if (TREE_CODE (arg1) == INTEGER_CST)
2523 : 0 : return fold_convert_const_fixed_from_int (type, arg1);
2524 : 0 : else if (TREE_CODE (arg1) == REAL_CST)
2525 : 0 : return fold_convert_const_fixed_from_real (type, arg1);
2526 : : }
2527 : : else if (VECTOR_TYPE_P (type))
2528 : : {
2529 : 3866 : if (TREE_CODE (arg1) == VECTOR_CST
2530 : 3866 : && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2531 : : {
2532 : 3866 : tree elttype = TREE_TYPE (type);
2533 : 3866 : tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2534 : : /* We can't handle steps directly when extending, since the
2535 : : values need to wrap at the original precision first. */
2536 : 3866 : bool step_ok_p
2537 : 3866 : = (INTEGRAL_TYPE_P (elttype)
2538 : 254 : && INTEGRAL_TYPE_P (arg1_elttype)
2539 : 4076 : && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2540 : 3866 : tree_vector_builder v;
2541 : 3866 : if (!v.new_unary_operation (type, arg1, step_ok_p))
2542 : : return NULL_TREE;
2543 : 3866 : unsigned int len = v.encoded_nelts ();
2544 : 25238 : for (unsigned int i = 0; i < len; ++i)
2545 : : {
2546 : 21372 : tree elt = VECTOR_CST_ELT (arg1, i);
2547 : 21372 : tree cvt = fold_convert_const (code, elttype, elt);
2548 : 21372 : if (cvt == NULL_TREE)
2549 : 0 : return NULL_TREE;
2550 : 21372 : v.quick_push (cvt);
2551 : : }
2552 : 3866 : return v.build ();
2553 : 3866 : }
2554 : : }
2555 : : return NULL_TREE;
2556 : : }
2557 : :
2558 : : /* Construct a vector of zero elements of vector type TYPE. */
2559 : :
2560 : : static tree
2561 : 5668 : build_zero_vector (tree type)
2562 : : {
2563 : 5668 : tree t;
2564 : :
2565 : 5668 : t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2566 : 5668 : return build_vector_from_val (type, t);
2567 : : }
2568 : :
2569 : : /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2570 : :
2571 : : bool
2572 : 172879 : fold_convertible_p (const_tree type, const_tree arg)
2573 : : {
2574 : 172879 : const_tree orig = TREE_TYPE (arg);
2575 : :
2576 : 172879 : if (type == orig)
2577 : : return true;
2578 : :
2579 : 172879 : if (TREE_CODE (arg) == ERROR_MARK
2580 : 172879 : || TREE_CODE (type) == ERROR_MARK
2581 : 172879 : || TREE_CODE (orig) == ERROR_MARK)
2582 : : return false;
2583 : :
2584 : 172879 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2585 : : return true;
2586 : :
2587 : 172879 : switch (TREE_CODE (type))
2588 : : {
2589 : 172518 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2590 : 172518 : case POINTER_TYPE: case REFERENCE_TYPE:
2591 : 172518 : case OFFSET_TYPE:
2592 : 172518 : return (INTEGRAL_TYPE_P (orig)
2593 : 237 : || (POINTER_TYPE_P (orig)
2594 : 102 : && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2595 : 172653 : || TREE_CODE (orig) == OFFSET_TYPE);
2596 : :
2597 : 42 : case REAL_TYPE:
2598 : 42 : case FIXED_POINT_TYPE:
2599 : 42 : case VOID_TYPE:
2600 : 42 : return TREE_CODE (type) == TREE_CODE (orig);
2601 : :
2602 : 201 : case VECTOR_TYPE:
2603 : 201 : return (VECTOR_TYPE_P (orig)
2604 : 306 : && known_eq (TYPE_VECTOR_SUBPARTS (type),
2605 : : TYPE_VECTOR_SUBPARTS (orig))
2606 : 210 : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2607 : :
2608 : : default:
2609 : : return false;
2610 : : }
2611 : : }
2612 : :
2613 : : /* Convert expression ARG to type TYPE. Used by the middle-end for
2614 : : simple conversions in preference to calling the front-end's convert. */
2615 : :
2616 : : tree
2617 : 1723872195 : fold_convert_loc (location_t loc, tree type, tree arg)
2618 : : {
2619 : 1723872195 : tree orig = TREE_TYPE (arg);
2620 : 1723872195 : tree tem;
2621 : :
2622 : 1723872195 : if (type == orig)
2623 : : return arg;
2624 : :
2625 : 1152764831 : if (TREE_CODE (arg) == ERROR_MARK
2626 : 1152763879 : || TREE_CODE (type) == ERROR_MARK
2627 : 1152763878 : || TREE_CODE (orig) == ERROR_MARK)
2628 : 953 : return error_mark_node;
2629 : :
2630 : 1152763878 : switch (TREE_CODE (type))
2631 : : {
2632 : 48778286 : case POINTER_TYPE:
2633 : 48778286 : case REFERENCE_TYPE:
2634 : : /* Handle conversions between pointers to different address spaces. */
2635 : 48778286 : if (POINTER_TYPE_P (orig)
2636 : 48778286 : && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2637 : 42414587 : != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2638 : 108 : return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2639 : : /* fall through */
2640 : :
2641 : 1122234556 : case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2642 : 1122234556 : case OFFSET_TYPE: case BITINT_TYPE:
2643 : 1122234556 : if (TREE_CODE (arg) == INTEGER_CST)
2644 : : {
2645 : 975246009 : tem = fold_convert_const (NOP_EXPR, type, arg);
2646 : 975246009 : if (tem != NULL_TREE)
2647 : : return tem;
2648 : : }
2649 : 146988547 : if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2650 : 2942 : || TREE_CODE (orig) == OFFSET_TYPE)
2651 : 146988547 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2652 : 0 : if (TREE_CODE (orig) == COMPLEX_TYPE)
2653 : 0 : return fold_convert_loc (loc, type,
2654 : : fold_build1_loc (loc, REALPART_EXPR,
2655 : 0 : TREE_TYPE (orig), arg));
2656 : 0 : gcc_assert (VECTOR_TYPE_P (orig)
2657 : : && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2658 : 0 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2659 : :
2660 : 509163 : case REAL_TYPE:
2661 : 509163 : if (TREE_CODE (arg) == INTEGER_CST)
2662 : : {
2663 : 56783 : tem = fold_convert_const (FLOAT_EXPR, type, arg);
2664 : 56783 : if (tem != NULL_TREE)
2665 : : return tem;
2666 : : }
2667 : 452380 : else if (TREE_CODE (arg) == REAL_CST)
2668 : : {
2669 : 115888 : tem = fold_convert_const (NOP_EXPR, type, arg);
2670 : 115888 : if (tem != NULL_TREE)
2671 : : return tem;
2672 : : }
2673 : 336492 : else if (TREE_CODE (arg) == FIXED_CST)
2674 : : {
2675 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2676 : 0 : if (tem != NULL_TREE)
2677 : : return tem;
2678 : : }
2679 : :
2680 : 336494 : switch (TREE_CODE (orig))
2681 : : {
2682 : 625 : case INTEGER_TYPE: case BITINT_TYPE:
2683 : 625 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2684 : 625 : case POINTER_TYPE: case REFERENCE_TYPE:
2685 : 625 : return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2686 : :
2687 : 335869 : case REAL_TYPE:
2688 : 335869 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2689 : :
2690 : 0 : case FIXED_POINT_TYPE:
2691 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2692 : :
2693 : 0 : case COMPLEX_TYPE:
2694 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2695 : 0 : return fold_convert_loc (loc, type, tem);
2696 : :
2697 : 0 : default:
2698 : 0 : gcc_unreachable ();
2699 : : }
2700 : :
2701 : 0 : case FIXED_POINT_TYPE:
2702 : 0 : if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2703 : 0 : || TREE_CODE (arg) == REAL_CST)
2704 : : {
2705 : 0 : tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2706 : 0 : if (tem != NULL_TREE)
2707 : 0 : goto fold_convert_exit;
2708 : : }
2709 : :
2710 : 0 : switch (TREE_CODE (orig))
2711 : : {
2712 : 0 : case FIXED_POINT_TYPE:
2713 : 0 : case INTEGER_TYPE:
2714 : 0 : case ENUMERAL_TYPE:
2715 : 0 : case BOOLEAN_TYPE:
2716 : 0 : case REAL_TYPE:
2717 : 0 : case BITINT_TYPE:
2718 : 0 : return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2719 : :
2720 : 0 : case COMPLEX_TYPE:
2721 : 0 : tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2722 : 0 : return fold_convert_loc (loc, type, tem);
2723 : :
2724 : 0 : default:
2725 : 0 : gcc_unreachable ();
2726 : : }
2727 : :
2728 : 2222 : case COMPLEX_TYPE:
2729 : 2222 : switch (TREE_CODE (orig))
2730 : : {
2731 : 584 : case INTEGER_TYPE: case BITINT_TYPE:
2732 : 584 : case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2733 : 584 : case POINTER_TYPE: case REFERENCE_TYPE:
2734 : 584 : case REAL_TYPE:
2735 : 584 : case FIXED_POINT_TYPE:
2736 : 1168 : return fold_build2_loc (loc, COMPLEX_EXPR, type,
2737 : 584 : fold_convert_loc (loc, TREE_TYPE (type), arg),
2738 : 584 : fold_convert_loc (loc, TREE_TYPE (type),
2739 : 584 : integer_zero_node));
2740 : 1638 : case COMPLEX_TYPE:
2741 : 1638 : {
2742 : 1638 : tree rpart, ipart;
2743 : :
2744 : 1638 : if (TREE_CODE (arg) == COMPLEX_EXPR)
2745 : : {
2746 : 1534 : rpart = fold_convert_loc (loc, TREE_TYPE (type),
2747 : 1534 : TREE_OPERAND (arg, 0));
2748 : 1534 : ipart = fold_convert_loc (loc, TREE_TYPE (type),
2749 : 1534 : TREE_OPERAND (arg, 1));
2750 : 1534 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2751 : : }
2752 : :
2753 : 104 : arg = save_expr (arg);
2754 : 104 : rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2755 : 104 : ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2756 : 104 : rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2757 : 104 : ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2758 : 104 : return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2759 : : }
2760 : :
2761 : 0 : default:
2762 : 0 : gcc_unreachable ();
2763 : : }
2764 : :
2765 : 29905883 : case VECTOR_TYPE:
2766 : 29905883 : if (integer_zerop (arg))
2767 : 5668 : return build_zero_vector (type);
2768 : 29900215 : gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2769 : 29900215 : gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2770 : : || VECTOR_TYPE_P (orig));
2771 : 29900215 : return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2772 : :
2773 : 108821 : case VOID_TYPE:
2774 : 108821 : tem = fold_ignored_result (arg);
2775 : 108821 : return fold_build1_loc (loc, NOP_EXPR, type, tem);
2776 : :
2777 : 3125 : default:
2778 : 3125 : if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2779 : 3125 : return fold_build1_loc (loc, NOP_EXPR, type, arg);
2780 : 0 : gcc_unreachable ();
2781 : : }
2782 : 0 : fold_convert_exit:
2783 : 0 : tem = protected_set_expr_location_unshare (tem, loc);
2784 : 0 : return tem;
2785 : : }
2786 : :
2787 : : /* Return false if expr can be assumed not to be an lvalue, true
2788 : : otherwise. */
2789 : :
2790 : : static bool
2791 : 50289725 : maybe_lvalue_p (const_tree x)
2792 : : {
2793 : : /* We only need to wrap lvalue tree codes. */
2794 : 50289725 : switch (TREE_CODE (x))
2795 : : {
2796 : : case VAR_DECL:
2797 : : case PARM_DECL:
2798 : : case RESULT_DECL:
2799 : : case LABEL_DECL:
2800 : : case FUNCTION_DECL:
2801 : : case SSA_NAME:
2802 : : case COMPOUND_LITERAL_EXPR:
2803 : :
2804 : : case COMPONENT_REF:
2805 : : case MEM_REF:
2806 : : case INDIRECT_REF:
2807 : : case ARRAY_REF:
2808 : : case ARRAY_RANGE_REF:
2809 : : case BIT_FIELD_REF:
2810 : : case OBJ_TYPE_REF:
2811 : :
2812 : : case REALPART_EXPR:
2813 : : case IMAGPART_EXPR:
2814 : : case PREINCREMENT_EXPR:
2815 : : case PREDECREMENT_EXPR:
2816 : : case SAVE_EXPR:
2817 : : case TRY_CATCH_EXPR:
2818 : : case WITH_CLEANUP_EXPR:
2819 : : case COMPOUND_EXPR:
2820 : : case MODIFY_EXPR:
2821 : : case TARGET_EXPR:
2822 : : case COND_EXPR:
2823 : : case BIND_EXPR:
2824 : : case VIEW_CONVERT_EXPR:
2825 : : break;
2826 : :
2827 : 38420978 : default:
2828 : : /* Assume the worst for front-end tree codes. */
2829 : 38420978 : if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2830 : : break;
2831 : : return false;
2832 : : }
2833 : :
2834 : 11946143 : return true;
2835 : : }
2836 : :
2837 : : /* Return an expr equal to X but certainly not valid as an lvalue. */
2838 : :
2839 : : tree
2840 : 34590790 : non_lvalue_loc (location_t loc, tree x)
2841 : : {
2842 : : /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2843 : : us. */
2844 : 34590790 : if (in_gimple_form)
2845 : : return x;
2846 : :
2847 : 8698842 : if (! maybe_lvalue_p (x))
2848 : : return x;
2849 : 1902106 : return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2850 : : }
2851 : :
2852 : : /* Given a tree comparison code, return the code that is the logical inverse.
2853 : : It is generally not safe to do this for floating-point comparisons, except
2854 : : for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2855 : : ERROR_MARK in this case. */
2856 : :
2857 : : enum tree_code
2858 : 101071151 : invert_tree_comparison (enum tree_code code, bool honor_nans)
2859 : : {
2860 : 101071151 : if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2861 : 968769 : && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2862 : : return ERROR_MARK;
2863 : :
2864 : 100354073 : switch (code)
2865 : : {
2866 : : case EQ_EXPR:
2867 : : return NE_EXPR;
2868 : 45650597 : case NE_EXPR:
2869 : 45650597 : return EQ_EXPR;
2870 : 8958194 : case GT_EXPR:
2871 : 8958194 : return honor_nans ? UNLE_EXPR : LE_EXPR;
2872 : 11542153 : case GE_EXPR:
2873 : 11542153 : return honor_nans ? UNLT_EXPR : LT_EXPR;
2874 : 6124263 : case LT_EXPR:
2875 : 6124263 : return honor_nans ? UNGE_EXPR : GE_EXPR;
2876 : 6721384 : case LE_EXPR:
2877 : 6721384 : return honor_nans ? UNGT_EXPR : GT_EXPR;
2878 : 252 : case LTGT_EXPR:
2879 : 252 : return UNEQ_EXPR;
2880 : 289 : case UNEQ_EXPR:
2881 : 289 : return LTGT_EXPR;
2882 : : case UNGT_EXPR:
2883 : : return LE_EXPR;
2884 : : case UNGE_EXPR:
2885 : : return LT_EXPR;
2886 : : case UNLT_EXPR:
2887 : : return GE_EXPR;
2888 : : case UNLE_EXPR:
2889 : : return GT_EXPR;
2890 : 219619 : case ORDERED_EXPR:
2891 : 219619 : return UNORDERED_EXPR;
2892 : 52902 : case UNORDERED_EXPR:
2893 : 52902 : return ORDERED_EXPR;
2894 : 0 : default:
2895 : 0 : gcc_unreachable ();
2896 : : }
2897 : : }
2898 : :
2899 : : /* Similar, but return the comparison that results if the operands are
2900 : : swapped. This is safe for floating-point. */
2901 : :
2902 : : enum tree_code
2903 : 138807615 : swap_tree_comparison (enum tree_code code)
2904 : : {
2905 : 138807615 : switch (code)
2906 : : {
2907 : : case EQ_EXPR:
2908 : : case NE_EXPR:
2909 : : case ORDERED_EXPR:
2910 : : case UNORDERED_EXPR:
2911 : : case LTGT_EXPR:
2912 : : case UNEQ_EXPR:
2913 : : return code;
2914 : 32034812 : case GT_EXPR:
2915 : 32034812 : return LT_EXPR;
2916 : 9894204 : case GE_EXPR:
2917 : 9894204 : return LE_EXPR;
2918 : 19240350 : case LT_EXPR:
2919 : 19240350 : return GT_EXPR;
2920 : 14642162 : case LE_EXPR:
2921 : 14642162 : return GE_EXPR;
2922 : 253274 : case UNGT_EXPR:
2923 : 253274 : return UNLT_EXPR;
2924 : 18667 : case UNGE_EXPR:
2925 : 18667 : return UNLE_EXPR;
2926 : 380217 : case UNLT_EXPR:
2927 : 380217 : return UNGT_EXPR;
2928 : 127607 : case UNLE_EXPR:
2929 : 127607 : return UNGE_EXPR;
2930 : 0 : default:
2931 : 0 : gcc_unreachable ();
2932 : : }
2933 : : }
2934 : :
2935 : :
2936 : : /* Convert a comparison tree code from an enum tree_code representation
2937 : : into a compcode bit-based encoding. This function is the inverse of
2938 : : compcode_to_comparison. */
2939 : :
2940 : : static enum comparison_code
2941 : 51540 : comparison_to_compcode (enum tree_code code)
2942 : : {
2943 : 51540 : switch (code)
2944 : : {
2945 : : case LT_EXPR:
2946 : : return COMPCODE_LT;
2947 : : case EQ_EXPR:
2948 : : return COMPCODE_EQ;
2949 : : case LE_EXPR:
2950 : : return COMPCODE_LE;
2951 : : case GT_EXPR:
2952 : : return COMPCODE_GT;
2953 : : case NE_EXPR:
2954 : : return COMPCODE_NE;
2955 : : case GE_EXPR:
2956 : : return COMPCODE_GE;
2957 : : case ORDERED_EXPR:
2958 : : return COMPCODE_ORD;
2959 : : case UNORDERED_EXPR:
2960 : : return COMPCODE_UNORD;
2961 : : case UNLT_EXPR:
2962 : : return COMPCODE_UNLT;
2963 : : case UNEQ_EXPR:
2964 : : return COMPCODE_UNEQ;
2965 : : case UNLE_EXPR:
2966 : : return COMPCODE_UNLE;
2967 : : case UNGT_EXPR:
2968 : : return COMPCODE_UNGT;
2969 : : case LTGT_EXPR:
2970 : : return COMPCODE_LTGT;
2971 : : case UNGE_EXPR:
2972 : : return COMPCODE_UNGE;
2973 : 0 : default:
2974 : 0 : gcc_unreachable ();
2975 : : }
2976 : : }
2977 : :
2978 : : /* Convert a compcode bit-based encoding of a comparison operator back
2979 : : to GCC's enum tree_code representation. This function is the
2980 : : inverse of comparison_to_compcode. */
2981 : :
2982 : : static enum tree_code
2983 : 11964 : compcode_to_comparison (enum comparison_code code)
2984 : : {
2985 : 11964 : switch (code)
2986 : : {
2987 : : case COMPCODE_LT:
2988 : : return LT_EXPR;
2989 : : case COMPCODE_EQ:
2990 : : return EQ_EXPR;
2991 : : case COMPCODE_LE:
2992 : : return LE_EXPR;
2993 : : case COMPCODE_GT:
2994 : : return GT_EXPR;
2995 : : case COMPCODE_NE:
2996 : : return NE_EXPR;
2997 : : case COMPCODE_GE:
2998 : : return GE_EXPR;
2999 : : case COMPCODE_ORD:
3000 : : return ORDERED_EXPR;
3001 : : case COMPCODE_UNORD:
3002 : : return UNORDERED_EXPR;
3003 : : case COMPCODE_UNLT:
3004 : : return UNLT_EXPR;
3005 : : case COMPCODE_UNEQ:
3006 : : return UNEQ_EXPR;
3007 : : case COMPCODE_UNLE:
3008 : : return UNLE_EXPR;
3009 : : case COMPCODE_UNGT:
3010 : : return UNGT_EXPR;
3011 : : case COMPCODE_LTGT:
3012 : : return LTGT_EXPR;
3013 : : case COMPCODE_UNGE:
3014 : : return UNGE_EXPR;
3015 : 0 : default:
3016 : 0 : gcc_unreachable ();
3017 : : }
3018 : : }
3019 : :
3020 : : /* Return true if COND1 tests the opposite condition of COND2. */
3021 : :
3022 : : bool
3023 : 1183138 : inverse_conditions_p (const_tree cond1, const_tree cond2)
3024 : : {
3025 : 1183138 : return (COMPARISON_CLASS_P (cond1)
3026 : 1110379 : && COMPARISON_CLASS_P (cond2)
3027 : 1106444 : && (invert_tree_comparison
3028 : 1106444 : (TREE_CODE (cond1),
3029 : 2212888 : HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
3030 : 68912 : && operand_equal_p (TREE_OPERAND (cond1, 0),
3031 : 68912 : TREE_OPERAND (cond2, 0), 0)
3032 : 1204518 : && operand_equal_p (TREE_OPERAND (cond1, 1),
3033 : 21380 : TREE_OPERAND (cond2, 1), 0));
3034 : : }
3035 : :
3036 : : /* Return a tree for the comparison which is the combination of
3037 : : doing the AND or OR (depending on CODE) of the two operations LCODE
3038 : : and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3039 : : the possibility of trapping if the mode has NaNs, and return NULL_TREE
3040 : : if this makes the transformation invalid. */
3041 : :
3042 : : tree
3043 : 25770 : combine_comparisons (location_t loc,
3044 : : enum tree_code code, enum tree_code lcode,
3045 : : enum tree_code rcode, tree truth_type,
3046 : : tree ll_arg, tree lr_arg)
3047 : : {
3048 : 25770 : bool honor_nans = HONOR_NANS (ll_arg);
3049 : 25770 : enum comparison_code lcompcode = comparison_to_compcode (lcode);
3050 : 25770 : enum comparison_code rcompcode = comparison_to_compcode (rcode);
3051 : 25770 : int compcode;
3052 : :
3053 : 25770 : switch (code)
3054 : : {
3055 : 17380 : case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3056 : 17380 : compcode = lcompcode & rcompcode;
3057 : 17380 : break;
3058 : :
3059 : 8390 : case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3060 : 8390 : compcode = lcompcode | rcompcode;
3061 : 8390 : break;
3062 : :
3063 : : default:
3064 : : return NULL_TREE;
3065 : : }
3066 : :
3067 : 25770 : if (!honor_nans)
3068 : : {
3069 : : /* Eliminate unordered comparisons, as well as LTGT and ORD
3070 : : which are not used unless the mode has NaNs. */
3071 : 20719 : compcode &= ~COMPCODE_UNORD;
3072 : 20719 : if (compcode == COMPCODE_LTGT)
3073 : : compcode = COMPCODE_NE;
3074 : 20188 : else if (compcode == COMPCODE_ORD)
3075 : : compcode = COMPCODE_TRUE;
3076 : : }
3077 : 5051 : else if (flag_trapping_math)
3078 : : {
3079 : : /* Check that the original operation and the optimized ones will trap
3080 : : under the same condition. */
3081 : 8260 : bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3082 : 3494 : && (lcompcode != COMPCODE_EQ)
3083 : 4130 : && (lcompcode != COMPCODE_ORD);
3084 : 8260 : bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3085 : 3642 : && (rcompcode != COMPCODE_EQ)
3086 : 4130 : && (rcompcode != COMPCODE_ORD);
3087 : 8260 : bool trap = (compcode & COMPCODE_UNORD) == 0
3088 : 3707 : && (compcode != COMPCODE_EQ)
3089 : 4130 : && (compcode != COMPCODE_ORD);
3090 : :
3091 : : /* In a short-circuited boolean expression the LHS might be
3092 : : such that the RHS, if evaluated, will never trap. For
3093 : : example, in ORD (x, y) && (x < y), we evaluate the RHS only
3094 : : if neither x nor y is NaN. (This is a mixed blessing: for
3095 : : example, the expression above will never trap, hence
3096 : : optimizing it to x < y would be invalid). */
3097 : 4130 : if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3098 : 3729 : || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3099 : 4130 : rtrap = false;
3100 : :
3101 : : /* If the comparison was short-circuited, and only the RHS
3102 : : trapped, we may now generate a spurious trap. */
3103 : 4130 : if (rtrap && !ltrap
3104 : 118 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3105 : : return NULL_TREE;
3106 : :
3107 : : /* If we changed the conditions that cause a trap, we lose. */
3108 : 4012 : if ((ltrap || rtrap) != trap)
3109 : : return NULL_TREE;
3110 : : }
3111 : :
3112 : 20560 : if (compcode == COMPCODE_TRUE)
3113 : 1302 : return constant_boolean_node (true, truth_type);
3114 : 21063 : else if (compcode == COMPCODE_FALSE)
3115 : 9099 : return constant_boolean_node (false, truth_type);
3116 : : else
3117 : : {
3118 : 11964 : enum tree_code tcode;
3119 : :
3120 : 11964 : tcode = compcode_to_comparison ((enum comparison_code) compcode);
3121 : 11964 : return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3122 : : }
3123 : : }
3124 : :
3125 : : /* Return nonzero if two operands (typically of the same tree node)
3126 : : are necessarily equal. FLAGS modifies behavior as follows:
3127 : :
3128 : : If OEP_ONLY_CONST is set, only return nonzero for constants.
3129 : : This function tests whether the operands are indistinguishable;
3130 : : it does not test whether they are equal using C's == operation.
3131 : : The distinction is important for IEEE floating point, because
3132 : : (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3133 : : (2) two NaNs may be indistinguishable, but NaN!=NaN.
3134 : :
3135 : : If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3136 : : even though it may hold multiple values during a function.
3137 : : This is because a GCC tree node guarantees that nothing else is
3138 : : executed between the evaluation of its "operands" (which may often
3139 : : be evaluated in arbitrary order). Hence if the operands themselves
3140 : : don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3141 : : same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3142 : : unset means assuming isochronic (or instantaneous) tree equivalence.
3143 : : Unless comparing arbitrary expression trees, such as from different
3144 : : statements, this flag can usually be left unset.
3145 : :
3146 : : If OEP_PURE_SAME is set, then pure functions with identical arguments
3147 : : are considered the same. It is used when the caller has other ways
3148 : : to ensure that global memory is unchanged in between.
3149 : :
3150 : : If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
3151 : : not values of expressions.
3152 : :
3153 : : If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3154 : : such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3155 : :
3156 : : If OEP_BITWISE is set, then require the values to be bitwise identical
3157 : : rather than simply numerically equal. Do not take advantage of things
3158 : : like math-related flags or undefined behavior; only return true for
3159 : : values that are provably bitwise identical in all circumstances.
3160 : :
3161 : : Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3162 : : any operand with side effect. This is unnecesarily conservative in the
3163 : : case we know that arg0 and arg1 are in disjoint code paths (such as in
3164 : : ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3165 : : addresses with TREE_CONSTANT flag set so we know that &var == &var
3166 : : even if var is volatile. */
3167 : :
3168 : : bool
3169 : 5932433419 : operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3170 : : unsigned int flags)
3171 : : {
3172 : 5932433419 : bool r;
3173 : 5932433419 : if (verify_hash_value (arg0, arg1, flags, &r))
3174 : 2426396904 : return r;
3175 : :
3176 : 3506036515 : STRIP_ANY_LOCATION_WRAPPER (arg0);
3177 : 3506036515 : STRIP_ANY_LOCATION_WRAPPER (arg1);
3178 : :
3179 : : /* If either is ERROR_MARK, they aren't equal. */
3180 : 3506036321 : if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3181 : 3506035789 : || TREE_TYPE (arg0) == error_mark_node
3182 : 7012072304 : || TREE_TYPE (arg1) == error_mark_node)
3183 : : return false;
3184 : :
3185 : : /* Similar, if either does not have a type (like a template id),
3186 : : they aren't equal. */
3187 : 3506035789 : if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
3188 : : return false;
3189 : :
3190 : : /* Bitwise identity makes no sense if the values have different layouts. */
3191 : 3506033911 : if ((flags & OEP_BITWISE)
3192 : 3506033911 : && !tree_nop_conversion_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3193 : : return false;
3194 : :
3195 : : /* We cannot consider pointers to different address space equal. */
3196 : 6525253342 : if (POINTER_TYPE_P (TREE_TYPE (arg0))
3197 : 501069631 : && POINTER_TYPE_P (TREE_TYPE (arg1))
3198 : 3916488523 : && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3199 : 410454612 : != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3200 : : return false;
3201 : :
3202 : : /* Check equality of integer constants before bailing out due to
3203 : : precision differences. */
3204 : 3506033695 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3205 : : {
3206 : : /* Address of INTEGER_CST is not defined; check that we did not forget
3207 : : to drop the OEP_ADDRESS_OF flags. */
3208 : 567745949 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3209 : 567745949 : return tree_int_cst_equal (arg0, arg1);
3210 : : }
3211 : :
3212 : 2938287746 : if (!(flags & OEP_ADDRESS_OF))
3213 : : {
3214 : : /* If both types don't have the same signedness, then we can't consider
3215 : : them equal. We must check this before the STRIP_NOPS calls
3216 : : because they may change the signedness of the arguments. As pointers
3217 : : strictly don't have a signedness, require either two pointers or
3218 : : two non-pointers as well. */
3219 : 2614091494 : if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3220 : 4984278966 : || POINTER_TYPE_P (TREE_TYPE (arg0))
3221 : 2370187472 : != POINTER_TYPE_P (TREE_TYPE (arg1)))
3222 : : return false;
3223 : :
3224 : : /* If both types don't have the same precision, then it is not safe
3225 : : to strip NOPs. */
3226 : 4679981280 : if (element_precision (TREE_TYPE (arg0))
3227 : 2339990640 : != element_precision (TREE_TYPE (arg1)))
3228 : : return false;
3229 : :
3230 : 2197147867 : STRIP_NOPS (arg0);
3231 : 2197147867 : STRIP_NOPS (arg1);
3232 : : }
3233 : : #if 0
3234 : : /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3235 : : sanity check once the issue is solved. */
3236 : : else
3237 : : /* Addresses of conversions and SSA_NAMEs (and many other things)
3238 : : are not defined. Check that we did not forget to drop the
3239 : : OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3240 : : gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3241 : : && TREE_CODE (arg0) != SSA_NAME);
3242 : : #endif
3243 : :
3244 : : /* In case both args are comparisons but with different comparison
3245 : : code, try to swap the comparison operands of one arg to produce
3246 : : a match and compare that variant. */
3247 : 2521344119 : if (TREE_CODE (arg0) != TREE_CODE (arg1)
3248 : 876407563 : && COMPARISON_CLASS_P (arg0)
3249 : 5508689 : && COMPARISON_CLASS_P (arg1))
3250 : : {
3251 : 4260647 : enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3252 : :
3253 : 4260647 : if (TREE_CODE (arg0) == swap_code)
3254 : 1977909 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3255 : 1977909 : TREE_OPERAND (arg1, 1), flags)
3256 : 1994629 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3257 : 16720 : TREE_OPERAND (arg1, 0), flags);
3258 : : }
3259 : :
3260 : 2519366210 : if (TREE_CODE (arg0) != TREE_CODE (arg1))
3261 : : {
3262 : : /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3263 : 874429654 : if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3264 : : ;
3265 : 874413829 : else if (flags & OEP_ADDRESS_OF)
3266 : : {
3267 : : /* If we are interested in comparing addresses ignore
3268 : : MEM_REF wrappings of the base that can appear just for
3269 : : TBAA reasons. */
3270 : 29949215 : if (TREE_CODE (arg0) == MEM_REF
3271 : 5836176 : && DECL_P (arg1)
3272 : 4091547 : && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3273 : 772254 : && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3274 : 30301327 : && integer_zerop (TREE_OPERAND (arg0, 1)))
3275 : : return true;
3276 : 29772525 : else if (TREE_CODE (arg1) == MEM_REF
3277 : 15573803 : && DECL_P (arg0)
3278 : 9095230 : && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3279 : 1606407 : && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3280 : 30219515 : && integer_zerop (TREE_OPERAND (arg1, 1)))
3281 : : return true;
3282 : 29464253 : return false;
3283 : : }
3284 : : else
3285 : : return false;
3286 : : }
3287 : :
3288 : : /* When not checking adddresses, this is needed for conversions and for
3289 : : COMPONENT_REF. Might as well play it safe and always test this. */
3290 : 1644952381 : if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3291 : 1644952381 : || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3292 : 3289904762 : || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3293 : 20022932 : && !(flags & OEP_ADDRESS_OF)))
3294 : 3565896 : return false;
3295 : :
3296 : : /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3297 : : We don't care about side effects in that case because the SAVE_EXPR
3298 : : takes care of that for us. In all other cases, two expressions are
3299 : : equal if they have no side effects. If we have two identical
3300 : : expressions with side effects that should be treated the same due
3301 : : to the only side effects being identical SAVE_EXPR's, that will
3302 : : be detected in the recursive calls below.
3303 : : If we are taking an invariant address of two identical objects
3304 : : they are necessarily equal as well. */
3305 : 280053997 : if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3306 : 1921440330 : && (TREE_CODE (arg0) == SAVE_EXPR
3307 : 280041311 : || (flags & OEP_MATCH_SIDE_EFFECTS)
3308 : 251044658 : || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3309 : : return true;
3310 : :
3311 : : /* Next handle constant cases, those for which we can return 1 even
3312 : : if ONLY_CONST is set. */
3313 : 1361465687 : if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3314 : 17819092 : switch (TREE_CODE (arg0))
3315 : : {
3316 : 153 : case INTEGER_CST:
3317 : 153 : return tree_int_cst_equal (arg0, arg1);
3318 : :
3319 : 0 : case FIXED_CST:
3320 : 0 : return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3321 : : TREE_FIXED_CST (arg1));
3322 : :
3323 : 3560833 : case REAL_CST:
3324 : 3560833 : if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3325 : : return true;
3326 : :
3327 : 2569593 : if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3328 : : {
3329 : : /* If we do not distinguish between signed and unsigned zero,
3330 : : consider them equal. */
3331 : 14615 : if (real_zerop (arg0) && real_zerop (arg1))
3332 : : return true;
3333 : : }
3334 : 2569589 : return false;
3335 : :
3336 : 599192 : case VECTOR_CST:
3337 : 599192 : {
3338 : 599192 : if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3339 : 599192 : != VECTOR_CST_LOG2_NPATTERNS (arg1))
3340 : : return false;
3341 : :
3342 : 587993 : if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3343 : 587993 : != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3344 : : return false;
3345 : :
3346 : 566874 : unsigned int count = vector_cst_encoded_nelts (arg0);
3347 : 736311 : for (unsigned int i = 0; i < count; ++i)
3348 : 1217480 : if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3349 : 608740 : VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3350 : : return false;
3351 : : return true;
3352 : : }
3353 : :
3354 : 11830 : case COMPLEX_CST:
3355 : 11830 : return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3356 : : flags)
3357 : 11830 : && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3358 : : flags));
3359 : :
3360 : 1040869 : case STRING_CST:
3361 : 1040869 : return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3362 : 1040869 : && ! memcmp (TREE_STRING_POINTER (arg0),
3363 : 627898 : TREE_STRING_POINTER (arg1),
3364 : 627898 : TREE_STRING_LENGTH (arg0)));
3365 : :
3366 : 11296706 : case ADDR_EXPR:
3367 : 11296706 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3368 : 11296706 : return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3369 : : flags | OEP_ADDRESS_OF
3370 : 11296706 : | OEP_MATCH_SIDE_EFFECTS);
3371 : 176462 : case CONSTRUCTOR:
3372 : 176462 : {
3373 : : /* In GIMPLE empty constructors are allowed in initializers of
3374 : : aggregates. */
3375 : 312641678 : if (!CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1))
3376 : : return true;
3377 : :
3378 : : /* See sem_variable::equals in ipa-icf for a similar approach. */
3379 : 150604 : tree typ0 = TREE_TYPE (arg0);
3380 : 150604 : tree typ1 = TREE_TYPE (arg1);
3381 : :
3382 : 150604 : if (TREE_CODE (typ0) != TREE_CODE (typ1))
3383 : : return false;
3384 : 150604 : else if (TREE_CODE (typ0) == ARRAY_TYPE)
3385 : : {
3386 : : /* For arrays, check that the sizes all match. */
3387 : 2 : const HOST_WIDE_INT siz0 = int_size_in_bytes (typ0);
3388 : 2 : if (TYPE_MODE (typ0) != TYPE_MODE (typ1)
3389 : 2 : || siz0 < 0
3390 : 4 : || siz0 != int_size_in_bytes (typ1))
3391 : 0 : return false;
3392 : : }
3393 : 150602 : else if (!types_compatible_p (typ0, typ1))
3394 : : return false;
3395 : :
3396 : 150604 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3397 : 150604 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3398 : 451812 : if (vec_safe_length (v0) != vec_safe_length (v1))
3399 : : return false;
3400 : :
3401 : : /* Address of CONSTRUCTOR is defined in GENERIC to mean the value
3402 : : of the CONSTRUCTOR referenced indirectly. */
3403 : 150604 : flags &= ~OEP_ADDRESS_OF;
3404 : :
3405 : 515406 : for (unsigned idx = 0; idx < vec_safe_length (v0); ++idx)
3406 : : {
3407 : 208441 : constructor_elt *c0 = &(*v0)[idx];
3408 : 208441 : constructor_elt *c1 = &(*v1)[idx];
3409 : :
3410 : : /* Check that the values are the same... */
3411 : 208441 : if (c0->value != c1->value
3412 : 208441 : && !operand_equal_p (c0->value, c1->value, flags))
3413 : : return false;
3414 : :
3415 : : /* ... and that they apply to the same field! */
3416 : 107099 : if (c0->index != c1->index
3417 : 107099 : && (TREE_CODE (typ0) == ARRAY_TYPE
3418 : 0 : ? !operand_equal_p (c0->index, c1->index, flags)
3419 : 0 : : !operand_equal_p (DECL_FIELD_OFFSET (c0->index),
3420 : 0 : DECL_FIELD_OFFSET (c1->index),
3421 : : flags)
3422 : 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (c0->index),
3423 : 0 : DECL_FIELD_BIT_OFFSET (c1->index),
3424 : : flags)))
3425 : 0 : return false;
3426 : : }
3427 : :
3428 : : return true;
3429 : : }
3430 : :
3431 : : default:
3432 : : break;
3433 : : }
3434 : :
3435 : : /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3436 : : two instances of undefined behavior will give identical results. */
3437 : 1344779642 : if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3438 : : return false;
3439 : :
3440 : : /* Define macros to test an operand from arg0 and arg1 for equality and a
3441 : : variant that allows null and views null as being different from any
3442 : : non-null value. In the latter case, if either is null, the both
3443 : : must be; otherwise, do the normal comparison. */
3444 : : #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3445 : : TREE_OPERAND (arg1, N), flags)
3446 : :
3447 : : #define OP_SAME_WITH_NULL(N) \
3448 : : ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3449 : : ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3450 : :
3451 : 1344779642 : switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3452 : : {
3453 : 6883372 : case tcc_unary:
3454 : : /* Two conversions are equal only if signedness and modes match. */
3455 : 6883372 : switch (TREE_CODE (arg0))
3456 : : {
3457 : 6533203 : CASE_CONVERT:
3458 : 6533203 : case FIX_TRUNC_EXPR:
3459 : 6533203 : if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3460 : 6533203 : != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3461 : : return false;
3462 : : break;
3463 : : default:
3464 : : break;
3465 : : }
3466 : :
3467 : 6883351 : return OP_SAME (0);
3468 : :
3469 : :
3470 : 20280107 : case tcc_comparison:
3471 : 20280107 : case tcc_binary:
3472 : 20280107 : if (OP_SAME (0) && OP_SAME (1))
3473 : : return true;
3474 : :
3475 : : /* For commutative ops, allow the other order. */
3476 : 15266822 : return (commutative_tree_code (TREE_CODE (arg0))
3477 : 10821836 : && operand_equal_p (TREE_OPERAND (arg0, 0),
3478 : 10821836 : TREE_OPERAND (arg1, 1), flags)
3479 : 15457372 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3480 : 190550 : TREE_OPERAND (arg1, 0), flags));
3481 : :
3482 : 858636128 : case tcc_reference:
3483 : : /* If either of the pointer (or reference) expressions we are
3484 : : dereferencing contain a side effect, these cannot be equal,
3485 : : but their addresses can be. */
3486 : 858636128 : if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3487 : 858636128 : && (TREE_SIDE_EFFECTS (arg0)
3488 : 798716268 : || TREE_SIDE_EFFECTS (arg1)))
3489 : : return false;
3490 : :
3491 : 858432465 : switch (TREE_CODE (arg0))
3492 : : {
3493 : 3706505 : case INDIRECT_REF:
3494 : 3706505 : if (!(flags & OEP_ADDRESS_OF))
3495 : : {
3496 : 7369050 : if (TYPE_ALIGN (TREE_TYPE (arg0))
3497 : 3684525 : != TYPE_ALIGN (TREE_TYPE (arg1)))
3498 : : return false;
3499 : : /* Verify that the access types are compatible. */
3500 : 3682699 : if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0))
3501 : 3682699 : != TYPE_MAIN_VARIANT (TREE_TYPE (arg1)))
3502 : : return false;
3503 : : }
3504 : 3651324 : flags &= ~OEP_ADDRESS_OF;
3505 : 3651324 : return OP_SAME (0);
3506 : :
3507 : 645865 : case IMAGPART_EXPR:
3508 : : /* Require the same offset. */
3509 : 645865 : if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3510 : 645865 : TYPE_SIZE (TREE_TYPE (arg1)),
3511 : : flags & ~OEP_ADDRESS_OF))
3512 : : return false;
3513 : :
3514 : : /* Fallthru. */
3515 : 2387537 : case REALPART_EXPR:
3516 : 2387537 : case VIEW_CONVERT_EXPR:
3517 : 2387537 : return OP_SAME (0);
3518 : :
3519 : 58116762 : case TARGET_MEM_REF:
3520 : 58116762 : case MEM_REF:
3521 : 58116762 : if (!(flags & OEP_ADDRESS_OF))
3522 : : {
3523 : : /* Require equal access sizes */
3524 : 14329792 : if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3525 : 14329792 : && (!TYPE_SIZE (TREE_TYPE (arg0))
3526 : 1088230 : || !TYPE_SIZE (TREE_TYPE (arg1))
3527 : 1085299 : || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3528 : 1085299 : TYPE_SIZE (TREE_TYPE (arg1)),
3529 : : flags)))
3530 : 1087043 : return false;
3531 : : /* Verify that access happens in similar types. */
3532 : 13242749 : if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3533 : : return false;
3534 : : /* Verify that accesses are TBAA compatible. */
3535 : 12934141 : if (!alias_ptr_types_compatible_p
3536 : 12934141 : (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3537 : 12934141 : TREE_TYPE (TREE_OPERAND (arg1, 1)))
3538 : 11862617 : || (MR_DEPENDENCE_CLIQUE (arg0)
3539 : 11862617 : != MR_DEPENDENCE_CLIQUE (arg1))
3540 : 22779745 : || (MR_DEPENDENCE_BASE (arg0)
3541 : 9845604 : != MR_DEPENDENCE_BASE (arg1)))
3542 : : return false;
3543 : : /* Verify that alignment is compatible. */
3544 : 19030458 : if (TYPE_ALIGN (TREE_TYPE (arg0))
3545 : 9515229 : != TYPE_ALIGN (TREE_TYPE (arg1)))
3546 : : return false;
3547 : : }
3548 : 53188177 : flags &= ~OEP_ADDRESS_OF;
3549 : 92915520 : return (OP_SAME (0) && OP_SAME (1)
3550 : : /* TARGET_MEM_REF require equal extra operands. */
3551 : 76303593 : && (TREE_CODE (arg0) != TARGET_MEM_REF
3552 : 501555 : || (OP_SAME_WITH_NULL (2)
3553 : 255561 : && OP_SAME_WITH_NULL (3)
3554 : 249327 : && OP_SAME_WITH_NULL (4))));
3555 : :
3556 : 35937306 : case ARRAY_REF:
3557 : 35937306 : case ARRAY_RANGE_REF:
3558 : 35937306 : if (!OP_SAME (0))
3559 : : return false;
3560 : 31392437 : flags &= ~OEP_ADDRESS_OF;
3561 : : /* Compare the array index by value if it is constant first as we
3562 : : may have different types but same value here. */
3563 : 31392437 : return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3564 : 31392437 : TREE_OPERAND (arg1, 1))
3565 : 28658221 : || OP_SAME (1))
3566 : 5490700 : && OP_SAME_WITH_NULL (2)
3567 : 5490520 : && OP_SAME_WITH_NULL (3)
3568 : : /* Compare low bound and element size as with OEP_ADDRESS_OF
3569 : : we have to account for the offset of the ref. */
3570 : 39628307 : && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3571 : 2745260 : == TREE_TYPE (TREE_OPERAND (arg1, 0))
3572 : 2461 : || (operand_equal_p (array_ref_low_bound
3573 : 2461 : (CONST_CAST_TREE (arg0)),
3574 : : array_ref_low_bound
3575 : 2461 : (CONST_CAST_TREE (arg1)), flags)
3576 : 2461 : && operand_equal_p (array_ref_element_size
3577 : 2461 : (CONST_CAST_TREE (arg0)),
3578 : : array_ref_element_size
3579 : 2461 : (CONST_CAST_TREE (arg1)),
3580 : : flags))));
3581 : :
3582 : 757785012 : case COMPONENT_REF:
3583 : : /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3584 : : may be NULL when we're called to compare MEM_EXPRs. */
3585 : 757785012 : if (!OP_SAME_WITH_NULL (0))
3586 : : return false;
3587 : 61135575 : {
3588 : 61135575 : bool compare_address = flags & OEP_ADDRESS_OF;
3589 : :
3590 : : /* Most of time we only need to compare FIELD_DECLs for equality.
3591 : : However when determining address look into actual offsets.
3592 : : These may match for unions and unshared record types. */
3593 : 61135575 : flags &= ~OEP_ADDRESS_OF;
3594 : 61135575 : if (!OP_SAME (1))
3595 : : {
3596 : 34986077 : if (compare_address
3597 : 705031 : && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3598 : : {
3599 : 705028 : tree field0 = TREE_OPERAND (arg0, 1);
3600 : 705028 : tree field1 = TREE_OPERAND (arg1, 1);
3601 : :
3602 : : /* Non-FIELD_DECL operands can appear in C++ templates. */
3603 : 705028 : if (TREE_CODE (field0) != FIELD_DECL
3604 : 705028 : || TREE_CODE (field1) != FIELD_DECL
3605 : 705028 : || !operand_equal_p (DECL_FIELD_OFFSET (field0),
3606 : 705028 : DECL_FIELD_OFFSET (field1), flags)
3607 : 1158538 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3608 : 226755 : DECL_FIELD_BIT_OFFSET (field1),
3609 : : flags))
3610 : 668408 : return false;
3611 : : }
3612 : : else
3613 : : return false;
3614 : : }
3615 : : }
3616 : 26186118 : return OP_SAME_WITH_NULL (2);
3617 : :
3618 : 499343 : case BIT_FIELD_REF:
3619 : 499343 : if (!OP_SAME (0))
3620 : : return false;
3621 : 384009 : flags &= ~OEP_ADDRESS_OF;
3622 : 384009 : return OP_SAME (1) && OP_SAME (2);
3623 : :
3624 : : default:
3625 : : return false;
3626 : : }
3627 : :
3628 : 30318240 : case tcc_expression:
3629 : 30318240 : switch (TREE_CODE (arg0))
3630 : : {
3631 : 29239780 : case ADDR_EXPR:
3632 : : /* Be sure we pass right ADDRESS_OF flag. */
3633 : 29239780 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3634 : 29239780 : return operand_equal_p (TREE_OPERAND (arg0, 0),
3635 : 29239780 : TREE_OPERAND (arg1, 0),
3636 : 29239780 : flags | OEP_ADDRESS_OF);
3637 : :
3638 : 678336 : case TRUTH_NOT_EXPR:
3639 : 678336 : return OP_SAME (0);
3640 : :
3641 : 34605 : case TRUTH_ANDIF_EXPR:
3642 : 34605 : case TRUTH_ORIF_EXPR:
3643 : 34605 : return OP_SAME (0) && OP_SAME (1);
3644 : :
3645 : 0 : case WIDEN_MULT_PLUS_EXPR:
3646 : 0 : case WIDEN_MULT_MINUS_EXPR:
3647 : 0 : if (!OP_SAME (2))
3648 : : return false;
3649 : : /* The multiplcation operands are commutative. */
3650 : : /* FALLTHRU */
3651 : :
3652 : 18029 : case TRUTH_AND_EXPR:
3653 : 18029 : case TRUTH_OR_EXPR:
3654 : 18029 : case TRUTH_XOR_EXPR:
3655 : 18029 : if (OP_SAME (0) && OP_SAME (1))
3656 : : return true;
3657 : :
3658 : : /* Otherwise take into account this is a commutative operation. */
3659 : 18011 : return (operand_equal_p (TREE_OPERAND (arg0, 0),
3660 : 18011 : TREE_OPERAND (arg1, 1), flags)
3661 : 18012 : && operand_equal_p (TREE_OPERAND (arg0, 1),
3662 : 1 : TREE_OPERAND (arg1, 0), flags));
3663 : :
3664 : 104173 : case COND_EXPR:
3665 : 104173 : if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3666 : 45903 : return false;
3667 : 58270 : flags &= ~OEP_ADDRESS_OF;
3668 : 58270 : return OP_SAME (0);
3669 : :
3670 : 3 : case BIT_INSERT_EXPR:
3671 : : /* BIT_INSERT_EXPR has an implict operand as the type precision
3672 : : of op1. Need to check to make sure they are the same. */
3673 : 3 : if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3674 : 0 : && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3675 : 3 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3676 : 0 : != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3677 : : return false;
3678 : : /* FALLTHRU */
3679 : :
3680 : 190 : case VEC_COND_EXPR:
3681 : 190 : case DOT_PROD_EXPR:
3682 : 190 : return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3683 : :
3684 : 16919 : case MODIFY_EXPR:
3685 : 16919 : case INIT_EXPR:
3686 : 16919 : case COMPOUND_EXPR:
3687 : 16919 : case PREDECREMENT_EXPR:
3688 : 16919 : case PREINCREMENT_EXPR:
3689 : 16919 : case POSTDECREMENT_EXPR:
3690 : 16919 : case POSTINCREMENT_EXPR:
3691 : 16919 : if (flags & OEP_LEXICOGRAPHIC)
3692 : 163 : return OP_SAME (0) && OP_SAME (1);
3693 : : return false;
3694 : :
3695 : 84275 : case CLEANUP_POINT_EXPR:
3696 : 84275 : case EXPR_STMT:
3697 : 84275 : case SAVE_EXPR:
3698 : 84275 : if (flags & OEP_LEXICOGRAPHIC)
3699 : 208 : return OP_SAME (0);
3700 : : return false;
3701 : :
3702 : 99229 : case OBJ_TYPE_REF:
3703 : : /* Virtual table reference. */
3704 : 198458 : if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3705 : 99229 : OBJ_TYPE_REF_EXPR (arg1), flags))
3706 : : return false;
3707 : 844 : flags &= ~OEP_ADDRESS_OF;
3708 : 844 : if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3709 : 844 : != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3710 : : return false;
3711 : 844 : if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3712 : 844 : OBJ_TYPE_REF_OBJECT (arg1), flags))
3713 : : return false;
3714 : 844 : if (virtual_method_call_p (arg0))
3715 : : {
3716 : 844 : if (!virtual_method_call_p (arg1))
3717 : : return false;
3718 : 844 : return types_same_for_odr (obj_type_ref_class (arg0),
3719 : 1688 : obj_type_ref_class (arg1));
3720 : : }
3721 : : return false;
3722 : :
3723 : : default:
3724 : : return false;
3725 : : }
3726 : :
3727 : 2714579 : case tcc_vl_exp:
3728 : 2714579 : switch (TREE_CODE (arg0))
3729 : : {
3730 : 2714579 : case CALL_EXPR:
3731 : 2714579 : if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3732 : 2714579 : != (CALL_EXPR_FN (arg1) == NULL_TREE))
3733 : : /* If not both CALL_EXPRs are either internal or normal function
3734 : : functions, then they are not equal. */
3735 : : return false;
3736 : 2714579 : else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3737 : : {
3738 : : /* If the CALL_EXPRs call different internal functions, then they
3739 : : are not equal. */
3740 : 4 : if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3741 : : return false;
3742 : : }
3743 : : else
3744 : : {
3745 : : /* If the CALL_EXPRs call different functions, then they are not
3746 : : equal. */
3747 : 2714575 : if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3748 : : flags))
3749 : : return false;
3750 : : }
3751 : :
3752 : : /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3753 : 1829183 : {
3754 : 1829183 : unsigned int cef = call_expr_flags (arg0);
3755 : 1829183 : if (flags & OEP_PURE_SAME)
3756 : 0 : cef &= ECF_CONST | ECF_PURE;
3757 : : else
3758 : 1829183 : cef &= ECF_CONST;
3759 : 1829183 : if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3760 : : return false;
3761 : : }
3762 : :
3763 : : /* Now see if all the arguments are the same. */
3764 : 87133 : {
3765 : 87133 : const_call_expr_arg_iterator iter0, iter1;
3766 : 87133 : const_tree a0, a1;
3767 : 174266 : for (a0 = first_const_call_expr_arg (arg0, &iter0),
3768 : 87133 : a1 = first_const_call_expr_arg (arg1, &iter1);
3769 : 95211 : a0 && a1;
3770 : 8078 : a0 = next_const_call_expr_arg (&iter0),
3771 : 8078 : a1 = next_const_call_expr_arg (&iter1))
3772 : 84647 : if (! operand_equal_p (a0, a1, flags))
3773 : : return false;
3774 : :
3775 : : /* If we get here and both argument lists are exhausted
3776 : : then the CALL_EXPRs are equal. */
3777 : 10564 : return ! (a0 || a1);
3778 : : }
3779 : : default:
3780 : : return false;
3781 : : }
3782 : :
3783 : 144418979 : case tcc_declaration:
3784 : : /* Consider __builtin_sqrt equal to sqrt. */
3785 : 144418979 : if (TREE_CODE (arg0) == FUNCTION_DECL)
3786 : 6029121 : return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3787 : 329381 : && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3788 : 5429265 : && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3789 : 329381 : == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3790 : :
3791 : 138989714 : if (DECL_P (arg0)
3792 : : && (flags & OEP_DECL_NAME)
3793 : 138989714 : && (flags & OEP_LEXICOGRAPHIC))
3794 : : {
3795 : : /* Consider decls with the same name equal. The caller needs
3796 : : to make sure they refer to the same entity (such as a function
3797 : : formal parameter). */
3798 : 35 : tree a0name = DECL_NAME (arg0);
3799 : 35 : tree a1name = DECL_NAME (arg1);
3800 : 70 : const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3801 : 70 : const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3802 : 60 : return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3803 : : }
3804 : : return false;
3805 : :
3806 : 279446327 : case tcc_exceptional:
3807 : 279446327 : if (TREE_CODE (arg0) == CONSTRUCTOR)
3808 : : {
3809 : 20042 : if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3810 : : return false;
3811 : :
3812 : : /* In GIMPLE constructors are used only to build vectors from
3813 : : elements. Individual elements in the constructor must be
3814 : : indexed in increasing order and form an initial sequence.
3815 : :
3816 : : We make no effort to compare nonconstant ones in GENERIC. */
3817 : 20042 : if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3818 : 20042 : || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3819 : : return false;
3820 : :
3821 : : /* Be sure that vectors constructed have the same representation.
3822 : : We only tested element precision and modes to match.
3823 : : Vectors may be BLKmode and thus also check that the number of
3824 : : parts match. */
3825 : 649 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3826 : 1298 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3827 : : return false;
3828 : :
3829 : 649 : vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3830 : 649 : vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3831 : 649 : unsigned int len = vec_safe_length (v0);
3832 : :
3833 : 1298 : if (len != vec_safe_length (v1))
3834 : : return false;
3835 : :
3836 : 2583 : for (unsigned int i = 0; i < len; i++)
3837 : : {
3838 : 1978 : constructor_elt *c0 = &(*v0)[i];
3839 : 1978 : constructor_elt *c1 = &(*v1)[i];
3840 : :
3841 : 1978 : if (!operand_equal_p (c0->value, c1->value, flags)
3842 : : /* In GIMPLE the indexes can be either NULL or matching i.
3843 : : Double check this so we won't get false
3844 : : positives for GENERIC. */
3845 : 1934 : || (c0->index
3846 : 868 : && (TREE_CODE (c0->index) != INTEGER_CST
3847 : 868 : || compare_tree_int (c0->index, i)))
3848 : 3912 : || (c1->index
3849 : 868 : && (TREE_CODE (c1->index) != INTEGER_CST
3850 : 868 : || compare_tree_int (c1->index, i))))
3851 : 44 : return false;
3852 : : }
3853 : : return true;
3854 : : }
3855 : 279426285 : else if (TREE_CODE (arg0) == STATEMENT_LIST
3856 : 2691 : && (flags & OEP_LEXICOGRAPHIC))
3857 : : {
3858 : : /* Compare the STATEMENT_LISTs. */
3859 : 16 : tree_stmt_iterator tsi1, tsi2;
3860 : 16 : tree body1 = CONST_CAST_TREE (arg0);
3861 : 16 : tree body2 = CONST_CAST_TREE (arg1);
3862 : 56 : for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3863 : 40 : tsi_next (&tsi1), tsi_next (&tsi2))
3864 : : {
3865 : : /* The lists don't have the same number of statements. */
3866 : 56 : if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3867 : : return false;
3868 : 56 : if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3869 : : return true;
3870 : 40 : if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3871 : : flags & (OEP_LEXICOGRAPHIC
3872 : : | OEP_NO_HASH_CHECK)))
3873 : : return false;
3874 : : }
3875 : : }
3876 : : return false;
3877 : :
3878 : 2081711 : case tcc_statement:
3879 : 2081711 : switch (TREE_CODE (arg0))
3880 : : {
3881 : 52 : case RETURN_EXPR:
3882 : 52 : if (flags & OEP_LEXICOGRAPHIC)
3883 : 52 : return OP_SAME_WITH_NULL (0);
3884 : : return false;
3885 : 4 : case DEBUG_BEGIN_STMT:
3886 : 4 : if (flags & OEP_LEXICOGRAPHIC)
3887 : : return true;
3888 : : return false;
3889 : : default:
3890 : : return false;
3891 : : }
3892 : :
3893 : : default:
3894 : : return false;
3895 : : }
3896 : :
3897 : : #undef OP_SAME
3898 : : #undef OP_SAME_WITH_NULL
3899 : : }
3900 : :
3901 : : /* Generate a hash value for an expression. This can be used iteratively
3902 : : by passing a previous result as the HSTATE argument. */
3903 : :
3904 : : void
3905 : 3671373787 : operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3906 : : unsigned int flags)
3907 : : {
3908 : 3671373787 : int i;
3909 : 3671373787 : enum tree_code code;
3910 : 3671373787 : enum tree_code_class tclass;
3911 : :
3912 : 3671373787 : if (t == NULL_TREE || t == error_mark_node)
3913 : : {
3914 : 78216380 : hstate.merge_hash (0);
3915 : 78216380 : return;
3916 : : }
3917 : :
3918 : 3593157407 : STRIP_ANY_LOCATION_WRAPPER (t);
3919 : :
3920 : 3593157407 : if (!(flags & OEP_ADDRESS_OF))
3921 : 3368171577 : STRIP_NOPS (t);
3922 : :
3923 : 3593157407 : code = TREE_CODE (t);
3924 : :
3925 : 3593157407 : switch (code)
3926 : : {
3927 : : /* Alas, constants aren't shared, so we can't rely on pointer
3928 : : identity. */
3929 : 284 : case VOID_CST:
3930 : 284 : hstate.merge_hash (0);
3931 : 284 : return;
3932 : 1632807900 : case INTEGER_CST:
3933 : 1632807900 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3934 : 3295592122 : for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3935 : 1662784222 : hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3936 : : return;
3937 : 15088663 : case REAL_CST:
3938 : 15088663 : {
3939 : 15088663 : unsigned int val2;
3940 : 15088663 : if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3941 : : val2 = rvc_zero;
3942 : : else
3943 : 14742402 : val2 = real_hash (TREE_REAL_CST_PTR (t));
3944 : 15088663 : hstate.merge_hash (val2);
3945 : 15088663 : return;
3946 : : }
3947 : 0 : case FIXED_CST:
3948 : 0 : {
3949 : 0 : unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
3950 : 0 : hstate.merge_hash (val2);
3951 : 0 : return;
3952 : : }
3953 : 8821043 : case STRING_CST:
3954 : 8821043 : hstate.add ((const void *) TREE_STRING_POINTER (t),
3955 : 8821043 : TREE_STRING_LENGTH (t));
3956 : 8821043 : return;
3957 : 208128 : case COMPLEX_CST:
3958 : 208128 : hash_operand (TREE_REALPART (t), hstate, flags);
3959 : 208128 : hash_operand (TREE_IMAGPART (t), hstate, flags);
3960 : 208128 : return;
3961 : 2274881 : case VECTOR_CST:
3962 : 2274881 : {
3963 : 2274881 : hstate.add_int (VECTOR_CST_NPATTERNS (t));
3964 : 2274881 : hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3965 : 2274881 : unsigned int count = vector_cst_encoded_nelts (t);
3966 : 7257824 : for (unsigned int i = 0; i < count; ++i)
3967 : 4982943 : hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3968 : : return;
3969 : : }
3970 : 776302159 : case SSA_NAME:
3971 : : /* We can just compare by pointer. */
3972 : 776302159 : hstate.add_hwi (SSA_NAME_VERSION (t));
3973 : 776302159 : return;
3974 : : case PLACEHOLDER_EXPR:
3975 : : /* The node itself doesn't matter. */
3976 : : return;
3977 : : case BLOCK:
3978 : : case OMP_CLAUSE:
3979 : : /* Ignore. */
3980 : : return;
3981 : : case TREE_LIST:
3982 : : /* A list of expressions, for a CALL_EXPR or as the elements of a
3983 : : VECTOR_CST. */
3984 : 272598 : for (; t; t = TREE_CHAIN (t))
3985 : 136299 : hash_operand (TREE_VALUE (t), hstate, flags);
3986 : : return;
3987 : 4771057 : case CONSTRUCTOR:
3988 : 4771057 : {
3989 : 4771057 : unsigned HOST_WIDE_INT idx;
3990 : 4771057 : tree field, value;
3991 : 4771057 : flags &= ~OEP_ADDRESS_OF;
3992 : 4771057 : hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
3993 : 19402510 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
3994 : : {
3995 : : /* In GIMPLE the indexes can be either NULL or matching i. */
3996 : 14631453 : if (field == NULL_TREE)
3997 : 1187431 : field = bitsize_int (idx);
3998 : 14631453 : if (TREE_CODE (field) == FIELD_DECL)
3999 : : {
4000 : 9899200 : hash_operand (DECL_FIELD_OFFSET (field), hstate, flags);
4001 : 9899200 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, flags);
4002 : : }
4003 : : else
4004 : 4732253 : hash_operand (field, hstate, flags);
4005 : 14631453 : hash_operand (value, hstate, flags);
4006 : : }
4007 : : return;
4008 : : }
4009 : 182 : case STATEMENT_LIST:
4010 : 182 : {
4011 : 182 : tree_stmt_iterator i;
4012 : 182 : for (i = tsi_start (CONST_CAST_TREE (t));
4013 : 550 : !tsi_end_p (i); tsi_next (&i))
4014 : 368 : hash_operand (tsi_stmt (i), hstate, flags);
4015 : 182 : return;
4016 : : }
4017 : : case TREE_VEC:
4018 : 24 : for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
4019 : 12 : hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
4020 : : return;
4021 : 4 : case IDENTIFIER_NODE:
4022 : 4 : hstate.add_object (IDENTIFIER_HASH_VALUE (t));
4023 : 4 : return;
4024 : 43245616 : case FUNCTION_DECL:
4025 : : /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
4026 : : Otherwise nodes that compare equal according to operand_equal_p might
4027 : : get different hash codes. However, don't do this for machine specific
4028 : : or front end builtins, since the function code is overloaded in those
4029 : : cases. */
4030 : 43245616 : if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
4031 : 43245616 : && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
4032 : : {
4033 : 5828248 : t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
4034 : 5828248 : code = TREE_CODE (t);
4035 : : }
4036 : : /* FALL THROUGH */
4037 : 1152746683 : default:
4038 : 1152746683 : if (POLY_INT_CST_P (t))
4039 : : {
4040 : : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
4041 : : hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
4042 : : return;
4043 : : }
4044 : 1152746683 : tclass = TREE_CODE_CLASS (code);
4045 : :
4046 : 1152746683 : if (tclass == tcc_declaration)
4047 : : {
4048 : : /* DECL's have a unique ID */
4049 : 847489214 : hstate.add_hwi (DECL_UID (t));
4050 : : }
4051 : 305257469 : else if (tclass == tcc_comparison && !commutative_tree_code (code))
4052 : : {
4053 : : /* For comparisons that can be swapped, use the lower
4054 : : tree code. */
4055 : 116974 : enum tree_code ccode = swap_tree_comparison (code);
4056 : 116974 : if (code < ccode)
4057 : 47695 : ccode = code;
4058 : 116974 : hstate.add_object (ccode);
4059 : 116974 : hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
4060 : 116974 : hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
4061 : : }
4062 : 305140495 : else if (CONVERT_EXPR_CODE_P (code))
4063 : : {
4064 : : /* NOP_EXPR and CONVERT_EXPR are considered equal by
4065 : : operand_equal_p. */
4066 : 4807762 : enum tree_code ccode = NOP_EXPR;
4067 : 4807762 : hstate.add_object (ccode);
4068 : :
4069 : : /* Don't hash the type, that can lead to having nodes which
4070 : : compare equal according to operand_equal_p, but which
4071 : : have different hash codes. Make sure to include signedness
4072 : : in the hash computation. */
4073 : 4807762 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4074 : 4807762 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4075 : : }
4076 : : /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
4077 : 300332733 : else if (code == MEM_REF
4078 : 70941863 : && (flags & OEP_ADDRESS_OF) != 0
4079 : 63078194 : && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
4080 : 10017497 : && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
4081 : 310214079 : && integer_zerop (TREE_OPERAND (t, 1)))
4082 : 4565685 : hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
4083 : : hstate, flags);
4084 : : /* Don't ICE on FE specific trees, or their arguments etc.
4085 : : during operand_equal_p hash verification. */
4086 : 295767048 : else if (!IS_EXPR_CODE_CLASS (tclass))
4087 : 252 : gcc_assert (flags & OEP_HASH_CHECK);
4088 : : else
4089 : : {
4090 : 295766796 : unsigned int sflags = flags;
4091 : :
4092 : 295766796 : hstate.add_object (code);
4093 : :
4094 : 295766796 : switch (code)
4095 : : {
4096 : 110189450 : case ADDR_EXPR:
4097 : 110189450 : gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
4098 : 110189450 : flags |= OEP_ADDRESS_OF;
4099 : 110189450 : sflags = flags;
4100 : 110189450 : break;
4101 : :
4102 : 70516980 : case INDIRECT_REF:
4103 : 70516980 : case MEM_REF:
4104 : 70516980 : case TARGET_MEM_REF:
4105 : 70516980 : flags &= ~OEP_ADDRESS_OF;
4106 : 70516980 : sflags = flags;
4107 : 70516980 : break;
4108 : :
4109 : 78786732 : case COMPONENT_REF:
4110 : 78786732 : if (sflags & OEP_ADDRESS_OF)
4111 : : {
4112 : 35242222 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4113 : 35242222 : hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
4114 : : hstate, flags & ~OEP_ADDRESS_OF);
4115 : 35242222 : hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
4116 : : hstate, flags & ~OEP_ADDRESS_OF);
4117 : 35242222 : return;
4118 : : }
4119 : : break;
4120 : 14088804 : case ARRAY_REF:
4121 : 14088804 : case ARRAY_RANGE_REF:
4122 : 14088804 : case BIT_FIELD_REF:
4123 : 14088804 : sflags &= ~OEP_ADDRESS_OF;
4124 : 14088804 : break;
4125 : :
4126 : 8392 : case COND_EXPR:
4127 : 8392 : flags &= ~OEP_ADDRESS_OF;
4128 : 8392 : break;
4129 : :
4130 : 0 : case WIDEN_MULT_PLUS_EXPR:
4131 : 0 : case WIDEN_MULT_MINUS_EXPR:
4132 : 0 : {
4133 : : /* The multiplication operands are commutative. */
4134 : 0 : inchash::hash one, two;
4135 : 0 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4136 : 0 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4137 : 0 : hstate.add_commutative (one, two);
4138 : 0 : hash_operand (TREE_OPERAND (t, 2), hstate, flags);
4139 : 0 : return;
4140 : : }
4141 : :
4142 : 56857 : case CALL_EXPR:
4143 : 56857 : if (CALL_EXPR_FN (t) == NULL_TREE)
4144 : 0 : hstate.add_int (CALL_EXPR_IFN (t));
4145 : : break;
4146 : :
4147 : 72 : case TARGET_EXPR:
4148 : : /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
4149 : : Usually different TARGET_EXPRs just should use
4150 : : different temporaries in their slots. */
4151 : 72 : hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
4152 : 72 : return;
4153 : :
4154 : 200822 : case OBJ_TYPE_REF:
4155 : : /* Virtual table reference. */
4156 : 200822 : inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
4157 : 200822 : flags &= ~OEP_ADDRESS_OF;
4158 : 200822 : inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
4159 : 200822 : inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
4160 : 200822 : if (!virtual_method_call_p (t))
4161 : : return;
4162 : 200807 : if (tree c = obj_type_ref_class (t))
4163 : : {
4164 : 200807 : c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
4165 : : /* We compute mangled names only when free_lang_data is run.
4166 : : In that case we can hash precisely. */
4167 : 200807 : if (TREE_CODE (c) == TYPE_DECL
4168 : 200807 : && DECL_ASSEMBLER_NAME_SET_P (c))
4169 : 683 : hstate.add_object
4170 : 683 : (IDENTIFIER_HASH_VALUE
4171 : : (DECL_ASSEMBLER_NAME (c)));
4172 : : }
4173 : 200807 : return;
4174 : : default:
4175 : : break;
4176 : : }
4177 : :
4178 : : /* Don't hash the type, that can lead to having nodes which
4179 : : compare equal according to operand_equal_p, but which
4180 : : have different hash codes. */
4181 : 260323680 : if (code == NON_LVALUE_EXPR)
4182 : : {
4183 : : /* Make sure to include signness in the hash computation. */
4184 : 0 : hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
4185 : 0 : hash_operand (TREE_OPERAND (t, 0), hstate, flags);
4186 : : }
4187 : :
4188 : 260323680 : else if (commutative_tree_code (code))
4189 : : {
4190 : : /* It's a commutative expression. We want to hash it the same
4191 : : however it appears. We do this by first hashing both operands
4192 : : and then rehashing based on the order of their independent
4193 : : hashes. */
4194 : 15898217 : inchash::hash one, two;
4195 : 15898217 : hash_operand (TREE_OPERAND (t, 0), one, flags);
4196 : 15898217 : hash_operand (TREE_OPERAND (t, 1), two, flags);
4197 : 15898217 : hstate.add_commutative (one, two);
4198 : : }
4199 : : else
4200 : 699416313 : for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
4201 : 665556277 : hash_operand (TREE_OPERAND (t, i), hstate,
4202 : : i == 0 ? flags : sflags);
4203 : : }
4204 : : return;
4205 : : }
4206 : : }
4207 : :
4208 : : bool
4209 : 5936802994 : operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
4210 : : unsigned int flags, bool *ret)
4211 : : {
4212 : : /* When checking and unless comparing DECL names, verify that if
4213 : : the outermost operand_equal_p call returns non-zero then ARG0
4214 : : and ARG1 have the same hash value. */
4215 : 5936802994 : if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4216 : : {
4217 : 2428314574 : if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4218 : : {
4219 : 394509176 : if (arg0 != arg1 && !(flags & OEP_DECL_NAME))
4220 : : {
4221 : 79275412 : inchash::hash hstate0 (0), hstate1 (0);
4222 : 79275412 : hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4223 : 79275412 : hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4224 : 79275412 : hashval_t h0 = hstate0.end ();
4225 : 79275412 : hashval_t h1 = hstate1.end ();
4226 : 79275412 : gcc_assert (h0 == h1);
4227 : : }
4228 : 394509176 : *ret = true;
4229 : : }
4230 : : else
4231 : 2033805398 : *ret = false;
4232 : :
4233 : 2428314574 : return true;
4234 : : }
4235 : :
4236 : : return false;
4237 : : }
4238 : :
4239 : :
4240 : : static operand_compare default_compare_instance;
4241 : :
4242 : : /* Conveinece wrapper around operand_compare class because usually we do
4243 : : not need to play with the valueizer. */
4244 : :
4245 : : bool
4246 : 2426403546 : operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4247 : : {
4248 : 2426403546 : return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4249 : : }
4250 : :
4251 : : namespace inchash
4252 : : {
4253 : :
4254 : : /* Generate a hash value for an expression. This can be used iteratively
4255 : : by passing a previous result as the HSTATE argument.
4256 : :
4257 : : This function is intended to produce the same hash for expressions which
4258 : : would compare equal using operand_equal_p. */
4259 : : void
4260 : 2863828961 : add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4261 : : {
4262 : 2863828961 : default_compare_instance.hash_operand (t, hstate, flags);
4263 : 2863828961 : }
4264 : :
4265 : : }
4266 : :
4267 : : /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4268 : : with a different signedness or a narrower precision. */
4269 : :
4270 : : static bool
4271 : 15419866 : operand_equal_for_comparison_p (tree arg0, tree arg1)
4272 : : {
4273 : 15419866 : if (operand_equal_p (arg0, arg1, 0))
4274 : : return true;
4275 : :
4276 : 29302594 : if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4277 : 24656021 : || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4278 : : return false;
4279 : :
4280 : : /* Discard any conversions that don't change the modes of ARG0 and ARG1
4281 : : and see if the inner values are the same. This removes any
4282 : : signedness comparison, which doesn't matter here. */
4283 : 4231056 : tree op0 = arg0;
4284 : 4231056 : tree op1 = arg1;
4285 : 4231056 : STRIP_NOPS (op0);
4286 : 4231056 : STRIP_NOPS (op1);
4287 : 4231056 : if (operand_equal_p (op0, op1, 0))
4288 : : return true;
4289 : :
4290 : : /* Discard a single widening conversion from ARG1 and see if the inner
4291 : : value is the same as ARG0. */
4292 : 3389801 : if (CONVERT_EXPR_P (arg1)
4293 : 657500 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4294 : 657461 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4295 : 657461 : < TYPE_PRECISION (TREE_TYPE (arg1))
4296 : 4319426 : && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4297 : : return true;
4298 : :
4299 : : return false;
4300 : : }
4301 : :
4302 : : /* See if ARG is an expression that is either a comparison or is performing
4303 : : arithmetic on comparisons. The comparisons must only be comparing
4304 : : two different values, which will be stored in *CVAL1 and *CVAL2; if
4305 : : they are nonzero it means that some operands have already been found.
4306 : : No variables may be used anywhere else in the expression except in the
4307 : : comparisons.
4308 : :
4309 : : If this is true, return 1. Otherwise, return zero. */
4310 : :
4311 : : static bool
4312 : 54071034 : twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4313 : : {
4314 : 58038626 : enum tree_code code = TREE_CODE (arg);
4315 : 58038626 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4316 : :
4317 : : /* We can handle some of the tcc_expression cases here. */
4318 : 58038626 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4319 : : tclass = tcc_unary;
4320 : 57495362 : else if (tclass == tcc_expression
4321 : 295150 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4322 : 295150 : || code == COMPOUND_EXPR))
4323 : : tclass = tcc_binary;
4324 : :
4325 : 57484837 : switch (tclass)
4326 : : {
4327 : 3967592 : case tcc_unary:
4328 : 3967592 : return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4329 : :
4330 : 5040260 : case tcc_binary:
4331 : 5040260 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4332 : 5040260 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4333 : :
4334 : : case tcc_constant:
4335 : : return true;
4336 : :
4337 : 284625 : case tcc_expression:
4338 : 284625 : if (code == COND_EXPR)
4339 : 776 : return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4340 : 776 : && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4341 : 840 : && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4342 : : return false;
4343 : :
4344 : 547983 : case tcc_comparison:
4345 : : /* First see if we can handle the first operand, then the second. For
4346 : : the second operand, we know *CVAL1 can't be zero. It must be that
4347 : : one side of the comparison is each of the values; test for the
4348 : : case where this isn't true by failing if the two operands
4349 : : are the same. */
4350 : :
4351 : 547983 : if (operand_equal_p (TREE_OPERAND (arg, 0),
4352 : 547983 : TREE_OPERAND (arg, 1), 0))
4353 : : return false;
4354 : :
4355 : 547983 : if (*cval1 == 0)
4356 : 546002 : *cval1 = TREE_OPERAND (arg, 0);
4357 : 1981 : else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4358 : : ;
4359 : 1874 : else if (*cval2 == 0)
4360 : 0 : *cval2 = TREE_OPERAND (arg, 0);
4361 : 1874 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4362 : : ;
4363 : : else
4364 : : return false;
4365 : :
4366 : 546109 : if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4367 : : ;
4368 : 546109 : else if (*cval2 == 0)
4369 : 546002 : *cval2 = TREE_OPERAND (arg, 1);
4370 : 107 : else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4371 : : ;
4372 : : else
4373 : : return false;
4374 : :
4375 : : return true;
4376 : :
4377 : : default:
4378 : : return false;
4379 : : }
4380 : : }
4381 : :
4382 : : /* ARG is a tree that is known to contain just arithmetic operations and
4383 : : comparisons. Evaluate the operations in the tree substituting NEW0 for
4384 : : any occurrence of OLD0 as an operand of a comparison and likewise for
4385 : : NEW1 and OLD1. */
4386 : :
4387 : : static tree
4388 : 690 : eval_subst (location_t loc, tree arg, tree old0, tree new0,
4389 : : tree old1, tree new1)
4390 : : {
4391 : 690 : tree type = TREE_TYPE (arg);
4392 : 690 : enum tree_code code = TREE_CODE (arg);
4393 : 690 : enum tree_code_class tclass = TREE_CODE_CLASS (code);
4394 : :
4395 : : /* We can handle some of the tcc_expression cases here. */
4396 : 690 : if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4397 : : tclass = tcc_unary;
4398 : 690 : else if (tclass == tcc_expression
4399 : 18 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4400 : : tclass = tcc_binary;
4401 : :
4402 : 681 : switch (tclass)
4403 : : {
4404 : 162 : case tcc_unary:
4405 : 162 : return fold_build1_loc (loc, code, type,
4406 : 162 : eval_subst (loc, TREE_OPERAND (arg, 0),
4407 : 162 : old0, new0, old1, new1));
4408 : :
4409 : 165 : case tcc_binary:
4410 : 330 : return fold_build2_loc (loc, code, type,
4411 : 165 : eval_subst (loc, TREE_OPERAND (arg, 0),
4412 : : old0, new0, old1, new1),
4413 : 165 : eval_subst (loc, TREE_OPERAND (arg, 1),
4414 : 165 : old0, new0, old1, new1));
4415 : :
4416 : 9 : case tcc_expression:
4417 : 9 : switch (code)
4418 : : {
4419 : 0 : case SAVE_EXPR:
4420 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4421 : 0 : old1, new1);
4422 : :
4423 : 0 : case COMPOUND_EXPR:
4424 : 0 : return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4425 : 0 : old1, new1);
4426 : :
4427 : 9 : case COND_EXPR:
4428 : 27 : return fold_build3_loc (loc, code, type,
4429 : 9 : eval_subst (loc, TREE_OPERAND (arg, 0),
4430 : : old0, new0, old1, new1),
4431 : 9 : eval_subst (loc, TREE_OPERAND (arg, 1),
4432 : : old0, new0, old1, new1),
4433 : 9 : eval_subst (loc, TREE_OPERAND (arg, 2),
4434 : 9 : old0, new0, old1, new1));
4435 : : default:
4436 : : break;
4437 : : }
4438 : : /* Fall through - ??? */
4439 : :
4440 : 177 : case tcc_comparison:
4441 : 177 : {
4442 : 177 : tree arg0 = TREE_OPERAND (arg, 0);
4443 : 177 : tree arg1 = TREE_OPERAND (arg, 1);
4444 : :
4445 : : /* We need to check both for exact equality and tree equality. The
4446 : : former will be true if the operand has a side-effect. In that
4447 : : case, we know the operand occurred exactly once. */
4448 : :
4449 : 177 : if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4450 : : arg0 = new0;
4451 : 0 : else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4452 : : arg0 = new1;
4453 : :
4454 : 177 : if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4455 : : arg1 = new0;
4456 : 177 : else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4457 : : arg1 = new1;
4458 : :
4459 : 177 : return fold_build2_loc (loc, code, type, arg0, arg1);
4460 : : }
4461 : :
4462 : : default:
4463 : : return arg;
4464 : : }
4465 : : }
4466 : :
4467 : : /* Return a tree for the case when the result of an expression is RESULT
4468 : : converted to TYPE and OMITTED was previously an operand of the expression
4469 : : but is now not needed (e.g., we folded OMITTED * 0).
4470 : :
4471 : : If OMITTED has side effects, we must evaluate it. Otherwise, just do
4472 : : the conversion of RESULT to TYPE. */
4473 : :
4474 : : tree
4475 : 262589 : omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4476 : : {
4477 : 262589 : tree t = fold_convert_loc (loc, type, result);
4478 : :
4479 : : /* If the resulting operand is an empty statement, just return the omitted
4480 : : statement casted to void. */
4481 : 262589 : if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4482 : 0 : return build1_loc (loc, NOP_EXPR, void_type_node,
4483 : 0 : fold_ignored_result (omitted));
4484 : :
4485 : 262589 : if (TREE_SIDE_EFFECTS (omitted))
4486 : 13041 : return build2_loc (loc, COMPOUND_EXPR, type,
4487 : 13041 : fold_ignored_result (omitted), t);
4488 : :
4489 : 249548 : return non_lvalue_loc (loc, t);
4490 : : }
4491 : :
4492 : : /* Return a tree for the case when the result of an expression is RESULT
4493 : : converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4494 : : of the expression but are now not needed.
4495 : :
4496 : : If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4497 : : If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4498 : : evaluated before OMITTED2. Otherwise, if neither has side effects,
4499 : : just do the conversion of RESULT to TYPE. */
4500 : :
4501 : : tree
4502 : 6966 : omit_two_operands_loc (location_t loc, tree type, tree result,
4503 : : tree omitted1, tree omitted2)
4504 : : {
4505 : 6966 : tree t = fold_convert_loc (loc, type, result);
4506 : :
4507 : 6966 : if (TREE_SIDE_EFFECTS (omitted2))
4508 : 68 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4509 : 6966 : if (TREE_SIDE_EFFECTS (omitted1))
4510 : 175 : t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4511 : :
4512 : 6966 : return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4513 : : }
4514 : :
4515 : :
4516 : : /* Return a simplified tree node for the truth-negation of ARG. This
4517 : : never alters ARG itself. We assume that ARG is an operation that
4518 : : returns a truth value (0 or 1).
4519 : :
4520 : : FIXME: one would think we would fold the result, but it causes
4521 : : problems with the dominator optimizer. */
4522 : :
4523 : : static tree
4524 : 43990073 : fold_truth_not_expr (location_t loc, tree arg)
4525 : : {
4526 : 43990073 : tree type = TREE_TYPE (arg);
4527 : 43990073 : enum tree_code code = TREE_CODE (arg);
4528 : 43990073 : location_t loc1, loc2;
4529 : :
4530 : : /* If this is a comparison, we can simply invert it, except for
4531 : : floating-point non-equality comparisons, in which case we just
4532 : : enclose a TRUTH_NOT_EXPR around what we have. */
4533 : :
4534 : 43990073 : if (TREE_CODE_CLASS (code) == tcc_comparison)
4535 : : {
4536 : 33558568 : tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4537 : 27982619 : if (FLOAT_TYPE_P (op_type)
4538 : 5586046 : && flag_trapping_math
4539 : 5556520 : && code != ORDERED_EXPR && code != UNORDERED_EXPR
4540 : 39075493 : && code != NE_EXPR && code != EQ_EXPR)
4541 : : return NULL_TREE;
4542 : :
4543 : 28715993 : code = invert_tree_comparison (code, HONOR_NANS (op_type));
4544 : 28715993 : if (code == ERROR_MARK)
4545 : : return NULL_TREE;
4546 : :
4547 : 28715993 : tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4548 : 28715993 : TREE_OPERAND (arg, 1));
4549 : 28715993 : copy_warning (ret, arg);
4550 : 28715993 : return ret;
4551 : : }
4552 : :
4553 : 10431505 : switch (code)
4554 : : {
4555 : 0 : case INTEGER_CST:
4556 : 0 : return constant_boolean_node (integer_zerop (arg), type);
4557 : :
4558 : 42921 : case TRUTH_AND_EXPR:
4559 : 42921 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4560 : 42921 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4561 : 85842 : return build2_loc (loc, TRUTH_OR_EXPR, type,
4562 : 42921 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4563 : 85842 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4564 : :
4565 : 2195 : case TRUTH_OR_EXPR:
4566 : 2195 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4567 : 2195 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4568 : 4390 : return build2_loc (loc, TRUTH_AND_EXPR, type,
4569 : 2195 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4570 : 4390 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4571 : :
4572 : 26744 : case TRUTH_XOR_EXPR:
4573 : : /* Here we can invert either operand. We invert the first operand
4574 : : unless the second operand is a TRUTH_NOT_EXPR in which case our
4575 : : result is the XOR of the first operand with the inside of the
4576 : : negation of the second operand. */
4577 : :
4578 : 26744 : if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4579 : 7 : return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4580 : 14 : TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4581 : : else
4582 : 26737 : return build2_loc (loc, TRUTH_XOR_EXPR, type,
4583 : 26737 : invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4584 : 53474 : TREE_OPERAND (arg, 1));
4585 : :
4586 : 221517 : case TRUTH_ANDIF_EXPR:
4587 : 221517 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4588 : 221517 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4589 : 443034 : return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4590 : 221517 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4591 : 443034 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4592 : :
4593 : 17832 : case TRUTH_ORIF_EXPR:
4594 : 17832 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4595 : 17832 : loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4596 : 35664 : return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4597 : 17832 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4598 : 35664 : invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4599 : :
4600 : 749514 : case TRUTH_NOT_EXPR:
4601 : 749514 : return TREE_OPERAND (arg, 0);
4602 : :
4603 : 7951 : case COND_EXPR:
4604 : 7951 : {
4605 : 7951 : tree arg1 = TREE_OPERAND (arg, 1);
4606 : 7951 : tree arg2 = TREE_OPERAND (arg, 2);
4607 : :
4608 : 7951 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4609 : 7951 : loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4610 : :
4611 : : /* A COND_EXPR may have a throw as one operand, which
4612 : : then has void type. Just leave void operands
4613 : : as they are. */
4614 : 7951 : return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4615 : 7951 : VOID_TYPE_P (TREE_TYPE (arg1))
4616 : 7951 : ? arg1 : invert_truthvalue_loc (loc1, arg1),
4617 : 7951 : VOID_TYPE_P (TREE_TYPE (arg2))
4618 : 15899 : ? arg2 : invert_truthvalue_loc (loc2, arg2));
4619 : : }
4620 : :
4621 : 92 : case COMPOUND_EXPR:
4622 : 92 : loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4623 : 184 : return build2_loc (loc, COMPOUND_EXPR, type,
4624 : 92 : TREE_OPERAND (arg, 0),
4625 : 184 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4626 : :
4627 : 0 : case NON_LVALUE_EXPR:
4628 : 0 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4629 : 0 : return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4630 : :
4631 : 68156 : CASE_CONVERT:
4632 : 68156 : if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4633 : 68092 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4634 : :
4635 : : /* fall through */
4636 : :
4637 : 64 : case FLOAT_EXPR:
4638 : 64 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4639 : 64 : return build1_loc (loc, TREE_CODE (arg), type,
4640 : 128 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4641 : :
4642 : 636 : case BIT_AND_EXPR:
4643 : 636 : if (!integer_onep (TREE_OPERAND (arg, 1)))
4644 : : return NULL_TREE;
4645 : 0 : return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4646 : :
4647 : 2 : case SAVE_EXPR:
4648 : 2 : return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4649 : :
4650 : 153 : case CLEANUP_POINT_EXPR:
4651 : 153 : loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4652 : 153 : return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4653 : 306 : invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4654 : :
4655 : : default:
4656 : : return NULL_TREE;
4657 : : }
4658 : : }
4659 : :
4660 : : /* Fold the truth-negation of ARG. This never alters ARG itself. We
4661 : : assume that ARG is an operation that returns a truth value (0 or 1
4662 : : for scalars, 0 or -1 for vectors). Return the folded expression if
4663 : : folding is successful. Otherwise, return NULL_TREE. */
4664 : :
4665 : : static tree
4666 : 1520457 : fold_invert_truthvalue (location_t loc, tree arg)
4667 : : {
4668 : 1520457 : tree type = TREE_TYPE (arg);
4669 : 3040908 : return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4670 : : ? BIT_NOT_EXPR
4671 : : : TRUTH_NOT_EXPR,
4672 : 1520457 : type, arg);
4673 : : }
4674 : :
4675 : : /* Return a simplified tree node for the truth-negation of ARG. This
4676 : : never alters ARG itself. We assume that ARG is an operation that
4677 : : returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4678 : :
4679 : : tree
4680 : 36879141 : invert_truthvalue_loc (location_t loc, tree arg)
4681 : : {
4682 : 36879141 : if (TREE_CODE (arg) == ERROR_MARK)
4683 : : return arg;
4684 : :
4685 : 36879141 : tree type = TREE_TYPE (arg);
4686 : 73758282 : return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4687 : : ? BIT_NOT_EXPR
4688 : : : TRUTH_NOT_EXPR,
4689 : 36879141 : type, arg);
4690 : : }
4691 : :
4692 : : /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4693 : : starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4694 : : and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4695 : : is the original memory reference used to preserve the alias set of
4696 : : the access. */
4697 : :
4698 : : static tree
4699 : 647035 : make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4700 : : HOST_WIDE_INT bitsize, poly_int64 bitpos,
4701 : : int unsignedp, int reversep)
4702 : : {
4703 : 647035 : tree result, bftype;
4704 : :
4705 : : /* Attempt not to lose the access path if possible. */
4706 : 647035 : if (TREE_CODE (orig_inner) == COMPONENT_REF)
4707 : : {
4708 : 609979 : tree ninner = TREE_OPERAND (orig_inner, 0);
4709 : 609979 : machine_mode nmode;
4710 : 609979 : poly_int64 nbitsize, nbitpos;
4711 : 609979 : tree noffset;
4712 : 609979 : int nunsignedp, nreversep, nvolatilep = 0;
4713 : 609979 : tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4714 : : &noffset, &nmode, &nunsignedp,
4715 : : &nreversep, &nvolatilep);
4716 : 609979 : if (base == inner
4717 : 609979 : && noffset == NULL_TREE
4718 : 609979 : && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4719 : 609979 : && !reversep
4720 : 609910 : && !nreversep
4721 : 1219889 : && !nvolatilep)
4722 : : {
4723 : 609910 : inner = ninner;
4724 : 609979 : bitpos -= nbitpos;
4725 : : }
4726 : : }
4727 : :
4728 : 647035 : alias_set_type iset = get_alias_set (orig_inner);
4729 : 647035 : if (iset == 0 && get_alias_set (inner) != iset)
4730 : 151 : inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4731 : : build_fold_addr_expr (inner),
4732 : : build_int_cst (ptr_type_node, 0));
4733 : :
4734 : 647035 : if (known_eq (bitpos, 0) && !reversep)
4735 : : {
4736 : 344078 : tree size = TYPE_SIZE (TREE_TYPE (inner));
4737 : 688156 : if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4738 : 344025 : || POINTER_TYPE_P (TREE_TYPE (inner)))
4739 : 59 : && tree_fits_shwi_p (size)
4740 : 344137 : && tree_to_shwi (size) == bitsize)
4741 : 59 : return fold_convert_loc (loc, type, inner);
4742 : : }
4743 : :
4744 : 646976 : bftype = type;
4745 : 646976 : if (TYPE_PRECISION (bftype) != bitsize
4746 : 646976 : || TYPE_UNSIGNED (bftype) == !unsignedp)
4747 : 992 : bftype = build_nonstandard_integer_type (bitsize, 0);
4748 : :
4749 : 646976 : result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4750 : : bitsize_int (bitsize), bitsize_int (bitpos));
4751 : 646976 : REF_REVERSE_STORAGE_ORDER (result) = reversep;
4752 : :
4753 : 646976 : if (bftype != type)
4754 : 992 : result = fold_convert_loc (loc, type, result);
4755 : :
4756 : : return result;
4757 : : }
4758 : :
4759 : : /* Optimize a bit-field compare.
4760 : :
4761 : : There are two cases: First is a compare against a constant and the
4762 : : second is a comparison of two items where the fields are at the same
4763 : : bit position relative to the start of a chunk (byte, halfword, word)
4764 : : large enough to contain it. In these cases we can avoid the shift
4765 : : implicit in bitfield extractions.
4766 : :
4767 : : For constants, we emit a compare of the shifted constant with the
4768 : : BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4769 : : compared. For two fields at the same position, we do the ANDs with the
4770 : : similar mask and compare the result of the ANDs.
4771 : :
4772 : : CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4773 : : COMPARE_TYPE is the type of the comparison, and LHS and RHS
4774 : : are the left and right operands of the comparison, respectively.
4775 : :
4776 : : If the optimization described above can be done, we return the resulting
4777 : : tree. Otherwise we return zero. */
4778 : :
4779 : : static tree
4780 : 4036145 : optimize_bit_field_compare (location_t loc, enum tree_code code,
4781 : : tree compare_type, tree lhs, tree rhs)
4782 : : {
4783 : 4036145 : poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4784 : 4036145 : HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4785 : 4036145 : tree type = TREE_TYPE (lhs);
4786 : 4036145 : tree unsigned_type;
4787 : 4036145 : int const_p = TREE_CODE (rhs) == INTEGER_CST;
4788 : 4036145 : machine_mode lmode, rmode;
4789 : 4036145 : scalar_int_mode nmode;
4790 : 4036145 : int lunsignedp, runsignedp;
4791 : 4036145 : int lreversep, rreversep;
4792 : 4036145 : int lvolatilep = 0, rvolatilep = 0;
4793 : 4036145 : tree linner, rinner = NULL_TREE;
4794 : 4036145 : tree mask;
4795 : 4036145 : tree offset;
4796 : :
4797 : : /* Get all the information about the extractions being done. If the bit size
4798 : : is the same as the size of the underlying object, we aren't doing an
4799 : : extraction at all and so can do nothing. We also don't want to
4800 : : do anything if the inner expression is a PLACEHOLDER_EXPR since we
4801 : : then will no longer be able to replace it. */
4802 : 4036145 : linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4803 : : &lunsignedp, &lreversep, &lvolatilep);
4804 : 4036145 : if (linner == lhs
4805 : 4036145 : || !known_size_p (plbitsize)
4806 : 4036145 : || !plbitsize.is_constant (&lbitsize)
4807 : 4036145 : || !plbitpos.is_constant (&lbitpos)
4808 : 8072290 : || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4809 : 613085 : || offset != 0
4810 : 613060 : || TREE_CODE (linner) == PLACEHOLDER_EXPR
4811 : 4649205 : || lvolatilep)
4812 : 3423145 : return 0;
4813 : :
4814 : 613000 : if (const_p)
4815 : 600224 : rreversep = lreversep;
4816 : : else
4817 : : {
4818 : : /* If this is not a constant, we can only do something if bit positions,
4819 : : sizes, signedness and storage order are the same. */
4820 : 12776 : rinner
4821 : 12776 : = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4822 : : &runsignedp, &rreversep, &rvolatilep);
4823 : :
4824 : 12776 : if (rinner == rhs
4825 : 12612 : || maybe_ne (lbitpos, rbitpos)
4826 : 12578 : || maybe_ne (lbitsize, rbitsize)
4827 : 12578 : || lunsignedp != runsignedp
4828 : 12578 : || lreversep != rreversep
4829 : 12578 : || offset != 0
4830 : 12578 : || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4831 : 25354 : || rvolatilep)
4832 : : return 0;
4833 : : }
4834 : :
4835 : : /* Honor the C++ memory model and mimic what RTL expansion does. */
4836 : 612802 : poly_uint64 bitstart = 0;
4837 : 612802 : poly_uint64 bitend = 0;
4838 : 612802 : if (TREE_CODE (lhs) == COMPONENT_REF)
4839 : : {
4840 : 612671 : get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4841 : 612671 : if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4842 : : return 0;
4843 : : }
4844 : :
4845 : : /* See if we can find a mode to refer to this field. We should be able to,
4846 : : but fail if we can't. */
4847 : 1225604 : if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4848 : 600224 : const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4849 : 12578 : : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4850 : : TYPE_ALIGN (TREE_TYPE (rinner))),
4851 : 612802 : BITS_PER_WORD, false, &nmode))
4852 : : return 0;
4853 : :
4854 : : /* Set signed and unsigned types of the precision of this mode for the
4855 : : shifts below. */
4856 : 611065 : unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4857 : :
4858 : : /* Compute the bit position and size for the new reference and our offset
4859 : : within it. If the new reference is the same size as the original, we
4860 : : won't optimize anything, so return zero. */
4861 : 611065 : nbitsize = GET_MODE_BITSIZE (nmode);
4862 : 611065 : nbitpos = lbitpos & ~ (nbitsize - 1);
4863 : 611065 : lbitpos -= nbitpos;
4864 : 611065 : if (nbitsize == lbitsize)
4865 : : return 0;
4866 : :
4867 : 599382 : if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4868 : 54 : lbitpos = nbitsize - lbitsize - lbitpos;
4869 : :
4870 : : /* Make the mask to be used against the extracted field. */
4871 : 599382 : mask = build_int_cst_type (unsigned_type, -1);
4872 : 599382 : mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4873 : 599382 : mask = const_binop (RSHIFT_EXPR, mask,
4874 : 599382 : size_int (nbitsize - lbitsize - lbitpos));
4875 : :
4876 : 599382 : if (! const_p)
4877 : : {
4878 : 9740 : if (nbitpos < 0)
4879 : : return 0;
4880 : :
4881 : : /* If not comparing with constant, just rework the comparison
4882 : : and return. */
4883 : 9740 : tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4884 : : nbitsize, nbitpos, 1, lreversep);
4885 : 9740 : t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4886 : 9740 : tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4887 : : nbitsize, nbitpos, 1, rreversep);
4888 : 9740 : t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4889 : 9740 : return fold_build2_loc (loc, code, compare_type, t1, t2);
4890 : : }
4891 : :
4892 : : /* Otherwise, we are handling the constant case. See if the constant is too
4893 : : big for the field. Warn and return a tree for 0 (false) if so. We do
4894 : : this not only for its own sake, but to avoid having to test for this
4895 : : error case below. If we didn't, we might generate wrong code.
4896 : :
4897 : : For unsigned fields, the constant shifted right by the field length should
4898 : : be all zero. For signed fields, the high-order bits should agree with
4899 : : the sign bit. */
4900 : :
4901 : 589642 : if (lunsignedp)
4902 : : {
4903 : 588573 : if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4904 : : {
4905 : 0 : warning (0, "comparison is always %d due to width of bit-field",
4906 : : code == NE_EXPR);
4907 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4908 : : }
4909 : : }
4910 : : else
4911 : : {
4912 : 1069 : wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4913 : 1069 : if (tem != 0 && tem != -1)
4914 : : {
4915 : 0 : warning (0, "comparison is always %d due to width of bit-field",
4916 : : code == NE_EXPR);
4917 : 0 : return constant_boolean_node (code == NE_EXPR, compare_type);
4918 : : }
4919 : 1069 : }
4920 : :
4921 : 589642 : if (nbitpos < 0)
4922 : : return 0;
4923 : :
4924 : : /* Single-bit compares should always be against zero. */
4925 : 589642 : if (lbitsize == 1 && ! integer_zerop (rhs))
4926 : : {
4927 : 175 : code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4928 : 175 : rhs = build_int_cst (type, 0);
4929 : : }
4930 : :
4931 : : /* Make a new bitfield reference, shift the constant over the
4932 : : appropriate number of bits and mask it with the computed mask
4933 : : (in case this was a signed field). If we changed it, make a new one. */
4934 : 589642 : lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4935 : : nbitsize, nbitpos, 1, lreversep);
4936 : :
4937 : 589642 : rhs = const_binop (BIT_AND_EXPR,
4938 : : const_binop (LSHIFT_EXPR,
4939 : : fold_convert_loc (loc, unsigned_type, rhs),
4940 : : size_int (lbitpos)),
4941 : : mask);
4942 : :
4943 : 589642 : lhs = build2_loc (loc, code, compare_type,
4944 : : build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4945 : 589642 : return lhs;
4946 : : }
4947 : :
4948 : : /* Subroutine for fold_truth_andor_1: decode a field reference.
4949 : :
4950 : : If EXP is a comparison reference, we return the innermost reference.
4951 : :
4952 : : *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4953 : : set to the starting bit number.
4954 : :
4955 : : If the innermost field can be completely contained in a mode-sized
4956 : : unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4957 : :
4958 : : *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4959 : : otherwise it is not changed.
4960 : :
4961 : : *PUNSIGNEDP is set to the signedness of the field.
4962 : :
4963 : : *PREVERSEP is set to the storage order of the field.
4964 : :
4965 : : *PMASK is set to the mask used. This is either contained in a
4966 : : BIT_AND_EXPR or derived from the width of the field.
4967 : :
4968 : : *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4969 : :
4970 : : Return 0 if this is not a component reference or is one that we can't
4971 : : do anything with. */
4972 : :
4973 : : static tree
4974 : 11471000 : decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4975 : : HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4976 : : int *punsignedp, int *preversep, int *pvolatilep,
4977 : : tree *pmask, tree *pand_mask)
4978 : : {
4979 : 11471000 : tree exp = *exp_;
4980 : 11471000 : tree outer_type = 0;
4981 : 11471000 : tree and_mask = 0;
4982 : 11471000 : tree mask, inner, offset;
4983 : 11471000 : tree unsigned_type;
4984 : 11471000 : unsigned int precision;
4985 : :
4986 : : /* All the optimizations using this function assume integer fields.
4987 : : There are problems with FP fields since the type_for_size call
4988 : : below can fail for, e.g., XFmode. */
4989 : 11471000 : if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4990 : : return NULL_TREE;
4991 : :
4992 : : /* We are interested in the bare arrangement of bits, so strip everything
4993 : : that doesn't affect the machine mode. However, record the type of the
4994 : : outermost expression if it may matter below. */
4995 : 8570798 : if (CONVERT_EXPR_P (exp)
4996 : 8370197 : || TREE_CODE (exp) == NON_LVALUE_EXPR)
4997 : 200920 : outer_type = TREE_TYPE (exp);
4998 : 8570798 : STRIP_NOPS (exp);
4999 : :
5000 : 8570798 : if (TREE_CODE (exp) == BIT_AND_EXPR)
5001 : : {
5002 : 1039207 : and_mask = TREE_OPERAND (exp, 1);
5003 : 1039207 : exp = TREE_OPERAND (exp, 0);
5004 : 1039207 : STRIP_NOPS (exp); STRIP_NOPS (and_mask);
5005 : 1039207 : if (TREE_CODE (and_mask) != INTEGER_CST)
5006 : : return NULL_TREE;
5007 : : }
5008 : :
5009 : 8568510 : poly_int64 poly_bitsize, poly_bitpos;
5010 : 8568510 : inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
5011 : : pmode, punsignedp, preversep, pvolatilep);
5012 : 8568510 : if ((inner == exp && and_mask == 0)
5013 : 1226363 : || !poly_bitsize.is_constant (pbitsize)
5014 : 1226363 : || !poly_bitpos.is_constant (pbitpos)
5015 : 1226363 : || *pbitsize < 0
5016 : 1226363 : || offset != 0
5017 : 1215372 : || TREE_CODE (inner) == PLACEHOLDER_EXPR
5018 : : /* We eventually want to build a larger reference and need to take
5019 : : the address of this. */
5020 : 1215372 : || (!REFERENCE_CLASS_P (inner) && !DECL_P (inner))
5021 : : /* Reject out-of-bound accesses (PR79731). */
5022 : 8906461 : || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
5023 : 9150 : && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
5024 : 9150 : *pbitpos + *pbitsize) < 0))
5025 : 8230608 : return NULL_TREE;
5026 : :
5027 : 337902 : unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
5028 : 337902 : if (unsigned_type == NULL_TREE)
5029 : : return NULL_TREE;
5030 : :
5031 : 337774 : *exp_ = exp;
5032 : :
5033 : : /* If the number of bits in the reference is the same as the bitsize of
5034 : : the outer type, then the outer type gives the signedness. Otherwise
5035 : : (in case of a small bitfield) the signedness is unchanged. */
5036 : 337774 : if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
5037 : 1649 : *punsignedp = TYPE_UNSIGNED (outer_type);
5038 : :
5039 : : /* Compute the mask to access the bitfield. */
5040 : 337774 : precision = TYPE_PRECISION (unsigned_type);
5041 : :
5042 : 337774 : mask = build_int_cst_type (unsigned_type, -1);
5043 : :
5044 : 337774 : mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
5045 : 337774 : mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
5046 : :
5047 : : /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
5048 : 337774 : if (and_mask != 0)
5049 : 159419 : mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
5050 : : fold_convert_loc (loc, unsigned_type, and_mask), mask);
5051 : :
5052 : 337774 : *pmask = mask;
5053 : 337774 : *pand_mask = and_mask;
5054 : 337774 : return inner;
5055 : : }
5056 : :
5057 : : /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
5058 : : bit positions and MASK is SIGNED. */
5059 : :
5060 : : static bool
5061 : 37913 : all_ones_mask_p (const_tree mask, unsigned int size)
5062 : : {
5063 : 37913 : tree type = TREE_TYPE (mask);
5064 : 37913 : unsigned int precision = TYPE_PRECISION (type);
5065 : :
5066 : : /* If this function returns true when the type of the mask is
5067 : : UNSIGNED, then there will be errors. In particular see
5068 : : gcc.c-torture/execute/990326-1.c. There does not appear to be
5069 : : any documentation paper trail as to why this is so. But the pre
5070 : : wide-int worked with that restriction and it has been preserved
5071 : : here. */
5072 : 37913 : if (size > precision || TYPE_SIGN (type) == UNSIGNED)
5073 : : return false;
5074 : :
5075 : 0 : return wi::mask (size, false, precision) == wi::to_wide (mask);
5076 : : }
5077 : :
5078 : : /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
5079 : : represents the sign bit of EXP's type. If EXP represents a sign
5080 : : or zero extension, also test VAL against the unextended type.
5081 : : The return value is the (sub)expression whose sign bit is VAL,
5082 : : or NULL_TREE otherwise. */
5083 : :
5084 : : tree
5085 : 2486 : sign_bit_p (tree exp, const_tree val)
5086 : : {
5087 : 2486 : int width;
5088 : 2486 : tree t;
5089 : :
5090 : : /* Tree EXP must have an integral type. */
5091 : 2486 : t = TREE_TYPE (exp);
5092 : 2486 : if (! INTEGRAL_TYPE_P (t))
5093 : : return NULL_TREE;
5094 : :
5095 : : /* Tree VAL must be an integer constant. */
5096 : 2162 : if (TREE_CODE (val) != INTEGER_CST
5097 : 2162 : || TREE_OVERFLOW (val))
5098 : : return NULL_TREE;
5099 : :
5100 : 1443 : width = TYPE_PRECISION (t);
5101 : 1443 : if (wi::only_sign_bit_p (wi::to_wide (val), width))
5102 : : return exp;
5103 : :
5104 : : /* Handle extension from a narrower type. */
5105 : 806 : if (TREE_CODE (exp) == NOP_EXPR
5106 : 806 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
5107 : 0 : return sign_bit_p (TREE_OPERAND (exp, 0), val);
5108 : :
5109 : : return NULL_TREE;
5110 : : }
5111 : :
5112 : : /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
5113 : : operand is simple enough to be evaluated unconditionally. */
5114 : :
5115 : : static bool
5116 : 56315834 : simple_operand_p (const_tree exp)
5117 : : {
5118 : : /* Strip any conversions that don't change the machine mode. */
5119 : 56315834 : STRIP_NOPS (exp);
5120 : :
5121 : 56315834 : return (CONSTANT_CLASS_P (exp)
5122 : 38908388 : || TREE_CODE (exp) == SSA_NAME
5123 : 68685367 : || (DECL_P (exp)
5124 : : && ! TREE_ADDRESSABLE (exp)
5125 : 4091597 : && ! TREE_THIS_VOLATILE (exp)
5126 : 4010572 : && ! DECL_NONLOCAL (exp)
5127 : : /* Don't regard global variables as simple. They may be
5128 : : allocated in ways unknown to the compiler (shared memory,
5129 : : #pragma weak, etc). */
5130 : 4009240 : && ! TREE_PUBLIC (exp)
5131 : 3988686 : && ! DECL_EXTERNAL (exp)
5132 : : /* Weakrefs are not safe to be read, since they can be NULL.
5133 : : They are !TREE_PUBLIC && !DECL_EXTERNAL but still
5134 : : have DECL_WEAK flag set. */
5135 : 3988686 : && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
5136 : : /* Loading a static variable is unduly expensive, but global
5137 : : registers aren't expensive. */
5138 : 3988686 : && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
5139 : : }
5140 : :
5141 : : /* Determine if an operand is simple enough to be evaluated unconditionally.
5142 : : In addition to simple_operand_p, we assume that comparisons, conversions,
5143 : : and logic-not operations are simple, if their operands are simple, too. */
5144 : :
5145 : : bool
5146 : 5410956 : simple_condition_p (tree exp)
5147 : : {
5148 : 5475131 : enum tree_code code;
5149 : :
5150 : 5475131 : if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
5151 : 3965381 : return false;
5152 : :
5153 : 1517714 : while (CONVERT_EXPR_P (exp))
5154 : 7964 : exp = TREE_OPERAND (exp, 0);
5155 : :
5156 : 1509750 : code = TREE_CODE (exp);
5157 : :
5158 : 1509750 : if (TREE_CODE_CLASS (code) == tcc_comparison)
5159 : 1151937 : return (simple_operand_p (TREE_OPERAND (exp, 0))
5160 : 1151937 : && simple_operand_p (TREE_OPERAND (exp, 1)));
5161 : :
5162 : 357813 : if (code == TRUTH_NOT_EXPR)
5163 : 64175 : return simple_condition_p (TREE_OPERAND (exp, 0));
5164 : :
5165 : 293638 : return simple_operand_p (exp);
5166 : : }
5167 : :
5168 : :
5169 : : /* The following functions are subroutines to fold_range_test and allow it to
5170 : : try to change a logical combination of comparisons into a range test.
5171 : :
5172 : : For example, both
5173 : : X == 2 || X == 3 || X == 4 || X == 5
5174 : : and
5175 : : X >= 2 && X <= 5
5176 : : are converted to
5177 : : (unsigned) (X - 2) <= 3
5178 : :
5179 : : We describe each set of comparisons as being either inside or outside
5180 : : a range, using a variable named like IN_P, and then describe the
5181 : : range with a lower and upper bound. If one of the bounds is omitted,
5182 : : it represents either the highest or lowest value of the type.
5183 : :
5184 : : In the comments below, we represent a range by two numbers in brackets
5185 : : preceded by a "+" to designate being inside that range, or a "-" to
5186 : : designate being outside that range, so the condition can be inverted by
5187 : : flipping the prefix. An omitted bound is represented by a "-". For
5188 : : example, "- [-, 10]" means being outside the range starting at the lowest
5189 : : possible value and ending at 10, in other words, being greater than 10.
5190 : : The range "+ [-, -]" is always true and hence the range "- [-, -]" is
5191 : : always false.
5192 : :
5193 : : We set up things so that the missing bounds are handled in a consistent
5194 : : manner so neither a missing bound nor "true" and "false" need to be
5195 : : handled using a special case. */
5196 : :
5197 : : /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
5198 : : of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
5199 : : and UPPER1_P are nonzero if the respective argument is an upper bound
5200 : : and zero for a lower. TYPE, if nonzero, is the type of the result; it
5201 : : must be specified for a comparison. ARG1 will be converted to ARG0's
5202 : : type if both are specified. */
5203 : :
5204 : : static tree
5205 : 19980482 : range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
5206 : : tree arg1, int upper1_p)
5207 : : {
5208 : 19980482 : tree tem;
5209 : 19980482 : int result;
5210 : 19980482 : int sgn0, sgn1;
5211 : :
5212 : : /* If neither arg represents infinity, do the normal operation.
5213 : : Else, if not a comparison, return infinity. Else handle the special
5214 : : comparison rules. Note that most of the cases below won't occur, but
5215 : : are handled for consistency. */
5216 : :
5217 : 19980482 : if (arg0 != 0 && arg1 != 0)
5218 : : {
5219 : 11368288 : tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5220 : : arg0, fold_convert (TREE_TYPE (arg0), arg1));
5221 : 11368288 : STRIP_NOPS (tem);
5222 : 11368288 : return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5223 : : }
5224 : :
5225 : 8612194 : if (TREE_CODE_CLASS (code) != tcc_comparison)
5226 : : return 0;
5227 : :
5228 : : /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5229 : : for neither. In real maths, we cannot assume open ended ranges are
5230 : : the same. But, this is computer arithmetic, where numbers are finite.
5231 : : We can therefore make the transformation of any unbounded range with
5232 : : the value Z, Z being greater than any representable number. This permits
5233 : : us to treat unbounded ranges as equal. */
5234 : 8604481 : sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5235 : 8604481 : sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5236 : 8604481 : switch (code)
5237 : : {
5238 : 3594383 : case EQ_EXPR:
5239 : 3594383 : result = sgn0 == sgn1;
5240 : 3594383 : break;
5241 : 0 : case NE_EXPR:
5242 : 0 : result = sgn0 != sgn1;
5243 : 0 : break;
5244 : 744667 : case LT_EXPR:
5245 : 744667 : result = sgn0 < sgn1;
5246 : 744667 : break;
5247 : 1773496 : case LE_EXPR:
5248 : 1773496 : result = sgn0 <= sgn1;
5249 : 1773496 : break;
5250 : 2491935 : case GT_EXPR:
5251 : 2491935 : result = sgn0 > sgn1;
5252 : 2491935 : break;
5253 : 0 : case GE_EXPR:
5254 : 0 : result = sgn0 >= sgn1;
5255 : 0 : break;
5256 : 0 : default:
5257 : 0 : gcc_unreachable ();
5258 : : }
5259 : :
5260 : 8604481 : return constant_boolean_node (result, type);
5261 : : }
5262 : :
5263 : : /* Helper routine for make_range. Perform one step for it, return
5264 : : new expression if the loop should continue or NULL_TREE if it should
5265 : : stop. */
5266 : :
5267 : : tree
5268 : 51802235 : make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5269 : : tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5270 : : bool *strict_overflow_p)
5271 : : {
5272 : 51802235 : tree arg0_type = TREE_TYPE (arg0);
5273 : 51802235 : tree n_low, n_high, low = *p_low, high = *p_high;
5274 : 51802235 : int in_p = *p_in_p, n_in_p;
5275 : :
5276 : 51802235 : switch (code)
5277 : : {
5278 : 1533988 : case TRUTH_NOT_EXPR:
5279 : : /* We can only do something if the range is testing for zero. */
5280 : 1533988 : if (low == NULL_TREE || high == NULL_TREE
5281 : 1533988 : || ! integer_zerop (low) || ! integer_zerop (high))
5282 : 0 : return NULL_TREE;
5283 : 1533988 : *p_in_p = ! in_p;
5284 : 1533988 : return arg0;
5285 : :
5286 : 41277377 : case EQ_EXPR: case NE_EXPR:
5287 : 41277377 : case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5288 : : /* We can only do something if the range is testing for zero
5289 : : and if the second operand is an integer constant. Note that
5290 : : saying something is "in" the range we make is done by
5291 : : complementing IN_P since it will set in the initial case of
5292 : : being not equal to zero; "out" is leaving it alone. */
5293 : 41277377 : if (low == NULL_TREE || high == NULL_TREE
5294 : 41277377 : || ! integer_zerop (low) || ! integer_zerop (high)
5295 : 82554660 : || TREE_CODE (arg1) != INTEGER_CST)
5296 : 15373257 : return NULL_TREE;
5297 : :
5298 : 25904120 : switch (code)
5299 : : {
5300 : : case NE_EXPR: /* - [c, c] */
5301 : : low = high = arg1;
5302 : : break;
5303 : 6508980 : case EQ_EXPR: /* + [c, c] */
5304 : 6508980 : in_p = ! in_p, low = high = arg1;
5305 : 6508980 : break;
5306 : 2141405 : case GT_EXPR: /* - [-, c] */
5307 : 2141405 : low = 0, high = arg1;
5308 : 2141405 : break;
5309 : 781717 : case GE_EXPR: /* + [c, -] */
5310 : 781717 : in_p = ! in_p, low = arg1, high = 0;
5311 : 781717 : break;
5312 : 5063696 : case LT_EXPR: /* - [c, -] */
5313 : 5063696 : low = arg1, high = 0;
5314 : 5063696 : break;
5315 : 4025946 : case LE_EXPR: /* + [-, c] */
5316 : 4025946 : in_p = ! in_p, low = 0, high = arg1;
5317 : 4025946 : break;
5318 : 0 : default:
5319 : 0 : gcc_unreachable ();
5320 : : }
5321 : :
5322 : : /* If this is an unsigned comparison, we also know that EXP is
5323 : : greater than or equal to zero. We base the range tests we make
5324 : : on that fact, so we record it here so we can parse existing
5325 : : range tests. We test arg0_type since often the return type
5326 : : of, e.g. EQ_EXPR, is boolean. */
5327 : 25904120 : if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5328 : : {
5329 : 1288050 : if (! merge_ranges (&n_in_p, &n_low, &n_high,
5330 : : in_p, low, high, 1,
5331 : 1288050 : build_int_cst (arg0_type, 0),
5332 : : NULL_TREE))
5333 : : return NULL_TREE;
5334 : :
5335 : 1288041 : in_p = n_in_p, low = n_low, high = n_high;
5336 : :
5337 : : /* If the high bound is missing, but we have a nonzero low
5338 : : bound, reverse the range so it goes from zero to the low bound
5339 : : minus 1. */
5340 : 1288041 : if (high == 0 && low && ! integer_zerop (low))
5341 : : {
5342 : 629007 : in_p = ! in_p;
5343 : 629007 : high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5344 : 629007 : build_int_cst (TREE_TYPE (low), 1), 0);
5345 : 629007 : low = build_int_cst (arg0_type, 0);
5346 : : }
5347 : : }
5348 : :
5349 : 25904111 : *p_low = low;
5350 : 25904111 : *p_high = high;
5351 : 25904111 : *p_in_p = in_p;
5352 : 25904111 : return arg0;
5353 : :
5354 : 188 : case NEGATE_EXPR:
5355 : : /* If flag_wrapv and ARG0_TYPE is signed, make sure
5356 : : low and high are non-NULL, then normalize will DTRT. */
5357 : 188 : if (!TYPE_UNSIGNED (arg0_type)
5358 : 188 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5359 : : {
5360 : 105 : if (low == NULL_TREE)
5361 : 12 : low = TYPE_MIN_VALUE (arg0_type);
5362 : 105 : if (high == NULL_TREE)
5363 : 47 : high = TYPE_MAX_VALUE (arg0_type);
5364 : : }
5365 : :
5366 : : /* (-x) IN [a,b] -> x in [-b, -a] */
5367 : 188 : n_low = range_binop (MINUS_EXPR, exp_type,
5368 : 188 : build_int_cst (exp_type, 0),
5369 : : 0, high, 1);
5370 : 188 : n_high = range_binop (MINUS_EXPR, exp_type,
5371 : 188 : build_int_cst (exp_type, 0),
5372 : : 0, low, 0);
5373 : 188 : if (n_high != 0 && TREE_OVERFLOW (n_high))
5374 : : return NULL_TREE;
5375 : 176 : goto normalize;
5376 : :
5377 : 0 : case BIT_NOT_EXPR:
5378 : : /* ~ X -> -X - 1 */
5379 : 0 : return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5380 : 0 : build_int_cst (exp_type, 1));
5381 : :
5382 : 700542 : case PLUS_EXPR:
5383 : 700542 : case MINUS_EXPR:
5384 : 700542 : if (TREE_CODE (arg1) != INTEGER_CST)
5385 : : return NULL_TREE;
5386 : :
5387 : : /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5388 : : move a constant to the other side. */
5389 : 580261 : if (!TYPE_UNSIGNED (arg0_type)
5390 : 580261 : && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5391 : : return NULL_TREE;
5392 : :
5393 : : /* If EXP is signed, any overflow in the computation is undefined,
5394 : : so we don't worry about it so long as our computations on
5395 : : the bounds don't overflow. For unsigned, overflow is defined
5396 : : and this is exactly the right thing. */
5397 : 711676 : n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5398 : : arg0_type, low, 0, arg1, 0);
5399 : 356033 : n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5400 : : arg0_type, high, 1, arg1, 0);
5401 : 352998 : if ((n_low != 0 && TREE_OVERFLOW (n_low))
5402 : 709018 : || (n_high != 0 && TREE_OVERFLOW (n_high)))
5403 : : return NULL_TREE;
5404 : :
5405 : 356020 : if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5406 : 13717 : *strict_overflow_p = true;
5407 : :
5408 : 0 : normalize:
5409 : : /* Check for an unsigned range which has wrapped around the maximum
5410 : : value thus making n_high < n_low, and normalize it. */
5411 : 356196 : if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5412 : : {
5413 : 132544 : low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5414 : 132544 : build_int_cst (TREE_TYPE (n_high), 1), 0);
5415 : 132544 : high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5416 : 132544 : build_int_cst (TREE_TYPE (n_low), 1), 0);
5417 : :
5418 : : /* If the range is of the form +/- [ x+1, x ], we won't
5419 : : be able to normalize it. But then, it represents the
5420 : : whole range or the empty set, so make it
5421 : : +/- [ -, - ]. */
5422 : 132544 : if (tree_int_cst_equal (n_low, low)
5423 : 132544 : && tree_int_cst_equal (n_high, high))
5424 : : low = high = 0;
5425 : : else
5426 : 132544 : in_p = ! in_p;
5427 : : }
5428 : : else
5429 : 223652 : low = n_low, high = n_high;
5430 : :
5431 : 356196 : *p_low = low;
5432 : 356196 : *p_high = high;
5433 : 356196 : *p_in_p = in_p;
5434 : 356196 : return arg0;
5435 : :
5436 : 2164567 : CASE_CONVERT:
5437 : 2164567 : case NON_LVALUE_EXPR:
5438 : 2164567 : if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5439 : : return NULL_TREE;
5440 : :
5441 : 843907 : if (! INTEGRAL_TYPE_P (arg0_type)
5442 : 816310 : || (low != 0 && ! int_fits_type_p (low, arg0_type))
5443 : 709795 : || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5444 : : return NULL_TREE;
5445 : :
5446 : 693118 : n_low = low, n_high = high;
5447 : :
5448 : 693118 : if (n_low != 0)
5449 : 596100 : n_low = fold_convert_loc (loc, arg0_type, n_low);
5450 : :
5451 : 693118 : if (n_high != 0)
5452 : 608943 : n_high = fold_convert_loc (loc, arg0_type, n_high);
5453 : :
5454 : : /* If we're converting arg0 from an unsigned type, to exp,
5455 : : a signed type, we will be doing the comparison as unsigned.
5456 : : The tests above have already verified that LOW and HIGH
5457 : : are both positive.
5458 : :
5459 : : So we have to ensure that we will handle large unsigned
5460 : : values the same way that the current signed bounds treat
5461 : : negative values. */
5462 : :
5463 : 693118 : if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5464 : : {
5465 : 206898 : tree high_positive;
5466 : 206898 : tree equiv_type;
5467 : : /* For fixed-point modes, we need to pass the saturating flag
5468 : : as the 2nd parameter. */
5469 : 206898 : if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5470 : 0 : equiv_type
5471 : 0 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5472 : 0 : TYPE_SATURATING (arg0_type));
5473 : 206898 : else if (TREE_CODE (arg0_type) == BITINT_TYPE)
5474 : : equiv_type = arg0_type;
5475 : : else
5476 : 206890 : equiv_type
5477 : 206890 : = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5478 : :
5479 : : /* A range without an upper bound is, naturally, unbounded.
5480 : : Since convert would have cropped a very large value, use
5481 : : the max value for the destination type. */
5482 : 206898 : high_positive
5483 : 206898 : = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5484 : 0 : : TYPE_MAX_VALUE (arg0_type);
5485 : :
5486 : 206898 : if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5487 : 186120 : high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5488 : : fold_convert_loc (loc, arg0_type,
5489 : : high_positive),
5490 : 186120 : build_int_cst (arg0_type, 1));
5491 : :
5492 : : /* If the low bound is specified, "and" the range with the
5493 : : range for which the original unsigned value will be
5494 : : positive. */
5495 : 206898 : if (low != 0)
5496 : : {
5497 : 114787 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5498 : : 1, fold_convert_loc (loc, arg0_type,
5499 : : integer_zero_node),
5500 : : high_positive))
5501 : : return NULL_TREE;
5502 : :
5503 : 114787 : in_p = (n_in_p == in_p);
5504 : : }
5505 : : else
5506 : : {
5507 : : /* Otherwise, "or" the range with the range of the input
5508 : : that will be interpreted as negative. */
5509 : 92111 : if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5510 : : 1, fold_convert_loc (loc, arg0_type,
5511 : : integer_zero_node),
5512 : : high_positive))
5513 : : return NULL_TREE;
5514 : :
5515 : 92111 : in_p = (in_p != n_in_p);
5516 : : }
5517 : : }
5518 : :
5519 : : /* Otherwise, if we are converting arg0 from signed type, to exp,
5520 : : an unsigned type, we will do the comparison as signed. If
5521 : : high is non-NULL, we punt above if it doesn't fit in the signed
5522 : : type, so if we get through here, +[-, high] or +[low, high] are
5523 : : equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5524 : : +[-, -] or -[-, -] are equivalent too. But if low is specified and
5525 : : high is not, the +[low, -] range is equivalent to union of
5526 : : +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5527 : : -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5528 : : low being 0, which should be treated as [-, -]. */
5529 : 486220 : else if (TYPE_UNSIGNED (exp_type)
5530 : 469443 : && !TYPE_UNSIGNED (arg0_type)
5531 : 216713 : && low
5532 : 702933 : && !high)
5533 : : {
5534 : 12 : if (integer_zerop (low))
5535 : 12 : n_low = NULL_TREE;
5536 : : else
5537 : : {
5538 : 0 : n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5539 : 0 : n_low, build_int_cst (arg0_type, -1));
5540 : 0 : n_low = build_zero_cst (arg0_type);
5541 : 0 : in_p = !in_p;
5542 : : }
5543 : : }
5544 : :
5545 : 693118 : *p_low = n_low;
5546 : 693118 : *p_high = n_high;
5547 : 693118 : *p_in_p = in_p;
5548 : 693118 : return arg0;
5549 : :
5550 : : default:
5551 : : return NULL_TREE;
5552 : : }
5553 : : }
5554 : :
5555 : : /* Given EXP, a logical expression, set the range it is testing into
5556 : : variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5557 : : actually being tested. *PLOW and *PHIGH will be made of the same
5558 : : type as the returned expression. If EXP is not a comparison, we
5559 : : will most likely not be returning a useful value and range. Set
5560 : : *STRICT_OVERFLOW_P to true if the return value is only valid
5561 : : because signed overflow is undefined; otherwise, do not change
5562 : : *STRICT_OVERFLOW_P. */
5563 : :
5564 : : tree
5565 : 43533816 : make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5566 : : bool *strict_overflow_p)
5567 : : {
5568 : 43533816 : enum tree_code code;
5569 : 43533816 : tree arg0, arg1 = NULL_TREE;
5570 : 43533816 : tree exp_type, nexp;
5571 : 43533816 : int in_p;
5572 : 43533816 : tree low, high;
5573 : 43533816 : location_t loc = EXPR_LOCATION (exp);
5574 : :
5575 : : /* Start with simply saying "EXP != 0" and then look at the code of EXP
5576 : : and see if we can refine the range. Some of the cases below may not
5577 : : happen, but it doesn't seem worth worrying about this. We "continue"
5578 : : the outer loop when we've changed something; otherwise we "break"
5579 : : the switch, which will "break" the while. */
5580 : :
5581 : 43533816 : in_p = 0;
5582 : 43533816 : low = high = build_int_cst (TREE_TYPE (exp), 0);
5583 : :
5584 : 69405744 : while (1)
5585 : : {
5586 : 69405744 : code = TREE_CODE (exp);
5587 : 69405744 : exp_type = TREE_TYPE (exp);
5588 : 69405744 : arg0 = NULL_TREE;
5589 : :
5590 : 69405744 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5591 : : {
5592 : 47659280 : if (TREE_OPERAND_LENGTH (exp) > 0)
5593 : 47659280 : arg0 = TREE_OPERAND (exp, 0);
5594 : 47659280 : if (TREE_CODE_CLASS (code) == tcc_binary
5595 : 45289964 : || TREE_CODE_CLASS (code) == tcc_comparison
5596 : 55209550 : || (TREE_CODE_CLASS (code) == tcc_expression
5597 : 2468307 : && TREE_OPERAND_LENGTH (exp) > 1))
5598 : 41031455 : arg1 = TREE_OPERAND (exp, 1);
5599 : : }
5600 : 47659280 : if (arg0 == NULL_TREE)
5601 : : break;
5602 : :
5603 : 47659266 : nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5604 : : &high, &in_p, strict_overflow_p);
5605 : 47659266 : if (nexp == NULL_TREE)
5606 : : break;
5607 : : exp = nexp;
5608 : : }
5609 : :
5610 : : /* If EXP is a constant, we can evaluate whether this is true or false. */
5611 : 43533816 : if (TREE_CODE (exp) == INTEGER_CST)
5612 : : {
5613 : 32894 : in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5614 : : exp, 0, low, 0))
5615 : 32894 : && integer_onep (range_binop (LE_EXPR, integer_type_node,
5616 : : exp, 1, high, 1)));
5617 : 32894 : low = high = 0;
5618 : 32894 : exp = 0;
5619 : : }
5620 : :
5621 : 43533816 : *pin_p = in_p, *plow = low, *phigh = high;
5622 : 43533816 : return exp;
5623 : : }
5624 : :
5625 : : /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5626 : : a bitwise check i.e. when
5627 : : LOW == 0xXX...X00...0
5628 : : HIGH == 0xXX...X11...1
5629 : : Return corresponding mask in MASK and stem in VALUE. */
5630 : :
5631 : : static bool
5632 : 131 : maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5633 : : tree *value)
5634 : : {
5635 : 131 : if (TREE_CODE (low) != INTEGER_CST
5636 : 131 : || TREE_CODE (high) != INTEGER_CST)
5637 : : return false;
5638 : :
5639 : 131 : unsigned prec = TYPE_PRECISION (type);
5640 : 131 : wide_int lo = wi::to_wide (low, prec);
5641 : 131 : wide_int hi = wi::to_wide (high, prec);
5642 : :
5643 : 131 : wide_int end_mask = lo ^ hi;
5644 : 262 : if ((end_mask & (end_mask + 1)) != 0
5645 : 241 : || (lo & end_mask) != 0)
5646 : : return false;
5647 : :
5648 : 86 : wide_int stem_mask = ~end_mask;
5649 : 86 : wide_int stem = lo & stem_mask;
5650 : 86 : if (stem != (hi & stem_mask))
5651 : : return false;
5652 : :
5653 : 86 : *mask = wide_int_to_tree (type, stem_mask);
5654 : 86 : *value = wide_int_to_tree (type, stem);
5655 : :
5656 : 86 : return true;
5657 : 217 : }
5658 : :
5659 : : /* Helper routine for build_range_check and match.pd. Return the type to
5660 : : perform the check or NULL if it shouldn't be optimized. */
5661 : :
5662 : : tree
5663 : 867533 : range_check_type (tree etype)
5664 : : {
5665 : : /* First make sure that arithmetics in this type is valid, then make sure
5666 : : that it wraps around. */
5667 : 867533 : if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5668 : 58438 : etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5669 : :
5670 : 867533 : if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5671 : : {
5672 : 746450 : tree utype, minv, maxv;
5673 : :
5674 : : /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5675 : : for the type in question, as we rely on this here. */
5676 : 746450 : utype = unsigned_type_for (etype);
5677 : 746450 : maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5678 : 746450 : maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5679 : 746450 : build_int_cst (TREE_TYPE (maxv), 1), 1);
5680 : 746450 : minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5681 : :
5682 : 746450 : if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5683 : : minv, 1, maxv, 1)))
5684 : : etype = utype;
5685 : : else
5686 : 0 : return NULL_TREE;
5687 : : }
5688 : 121083 : else if (POINTER_TYPE_P (etype)
5689 : : || TREE_CODE (etype) == OFFSET_TYPE
5690 : : /* Right now all BITINT_TYPEs satisfy
5691 : : (unsigned) max + 1 == (unsigned) min, so no need to verify
5692 : : that like for INTEGER_TYPEs. */
5693 : : || TREE_CODE (etype) == BITINT_TYPE)
5694 : 1357 : etype = unsigned_type_for (etype);
5695 : : return etype;
5696 : : }
5697 : :
5698 : : /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5699 : : type, TYPE, return an expression to test if EXP is in (or out of, depending
5700 : : on IN_P) the range. Return 0 if the test couldn't be created. */
5701 : :
5702 : : tree
5703 : 1667300 : build_range_check (location_t loc, tree type, tree exp, int in_p,
5704 : : tree low, tree high)
5705 : : {
5706 : 3318563 : tree etype = TREE_TYPE (exp), mask, value;
5707 : :
5708 : : /* Disable this optimization for function pointer expressions
5709 : : on targets that require function pointer canonicalization. */
5710 : 3318563 : if (targetm.have_canonicalize_funcptr_for_compare ()
5711 : 0 : && POINTER_TYPE_P (etype)
5712 : 3318563 : && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5713 : : return NULL_TREE;
5714 : :
5715 : 3318563 : if (! in_p)
5716 : : {
5717 : 315593 : value = build_range_check (loc, type, exp, 1, low, high);
5718 : 315593 : if (value != 0)
5719 : 315593 : return invert_truthvalue_loc (loc, value);
5720 : :
5721 : : return 0;
5722 : : }
5723 : :
5724 : 3002970 : if (low == 0 && high == 0)
5725 : 127687 : return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5726 : :
5727 : 2875283 : if (low == 0)
5728 : 957179 : return fold_build2_loc (loc, LE_EXPR, type, exp,
5729 : 957179 : fold_convert_loc (loc, etype, high));
5730 : :
5731 : 1918104 : if (high == 0)
5732 : 68217 : return fold_build2_loc (loc, GE_EXPR, type, exp,
5733 : 68217 : fold_convert_loc (loc, etype, low));
5734 : :
5735 : 1849887 : if (operand_equal_p (low, high, 0))
5736 : 198336 : return fold_build2_loc (loc, EQ_EXPR, type, exp,
5737 : 198336 : fold_convert_loc (loc, etype, low));
5738 : :
5739 : 1651551 : if (TREE_CODE (exp) == BIT_AND_EXPR
5740 : 1651551 : && maskable_range_p (low, high, etype, &mask, &value))
5741 : 86 : return fold_build2_loc (loc, EQ_EXPR, type,
5742 : : fold_build2_loc (loc, BIT_AND_EXPR, etype,
5743 : : exp, mask),
5744 : 86 : value);
5745 : :
5746 : 1651465 : if (integer_zerop (low))
5747 : : {
5748 : 870426 : if (! TYPE_UNSIGNED (etype))
5749 : : {
5750 : 81990 : etype = unsigned_type_for (etype);
5751 : 81990 : high = fold_convert_loc (loc, etype, high);
5752 : 81990 : exp = fold_convert_loc (loc, etype, exp);
5753 : : }
5754 : 870426 : return build_range_check (loc, type, exp, 1, 0, high);
5755 : : }
5756 : :
5757 : : /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5758 : 781039 : if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5759 : : {
5760 : 93710 : int prec = TYPE_PRECISION (etype);
5761 : :
5762 : 93710 : if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5763 : : {
5764 : 202 : if (TYPE_UNSIGNED (etype))
5765 : : {
5766 : 196 : tree signed_etype = signed_type_for (etype);
5767 : 196 : if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5768 : 0 : etype
5769 : 0 : = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5770 : : else
5771 : : etype = signed_etype;
5772 : 196 : exp = fold_convert_loc (loc, etype, exp);
5773 : : }
5774 : 202 : return fold_build2_loc (loc, GT_EXPR, type, exp,
5775 : 202 : build_int_cst (etype, 0));
5776 : : }
5777 : : }
5778 : :
5779 : : /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5780 : : This requires wrap-around arithmetics for the type of the expression. */
5781 : 780837 : etype = range_check_type (etype);
5782 : 780837 : if (etype == NULL_TREE)
5783 : : return NULL_TREE;
5784 : :
5785 : 780837 : high = fold_convert_loc (loc, etype, high);
5786 : 780837 : low = fold_convert_loc (loc, etype, low);
5787 : 780837 : exp = fold_convert_loc (loc, etype, exp);
5788 : :
5789 : 780837 : value = const_binop (MINUS_EXPR, high, low);
5790 : :
5791 : 780837 : if (value != 0 && !TREE_OVERFLOW (value))
5792 : 780837 : return build_range_check (loc, type,
5793 : : fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5794 : 780837 : 1, build_int_cst (etype, 0), value);
5795 : :
5796 : : return 0;
5797 : : }
5798 : :
5799 : : /* Return the predecessor of VAL in its type, handling the infinite case. */
5800 : :
5801 : : static tree
5802 : 141990 : range_predecessor (tree val)
5803 : : {
5804 : 141990 : tree type = TREE_TYPE (val);
5805 : :
5806 : 141990 : if (INTEGRAL_TYPE_P (type)
5807 : 141990 : && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5808 : : return 0;
5809 : : else
5810 : 141990 : return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5811 : 141990 : build_int_cst (TREE_TYPE (val), 1), 0);
5812 : : }
5813 : :
5814 : : /* Return the successor of VAL in its type, handling the infinite case. */
5815 : :
5816 : : static tree
5817 : 1562302 : range_successor (tree val)
5818 : : {
5819 : 1562302 : tree type = TREE_TYPE (val);
5820 : :
5821 : 1562302 : if (INTEGRAL_TYPE_P (type)
5822 : 1562302 : && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5823 : : return 0;
5824 : : else
5825 : 1562293 : return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5826 : 1562293 : build_int_cst (TREE_TYPE (val), 1), 0);
5827 : : }
5828 : :
5829 : : /* Given two ranges, see if we can merge them into one. Return 1 if we
5830 : : can, 0 if we can't. Set the output range into the specified parameters. */
5831 : :
5832 : : bool
5833 : 2985444 : merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5834 : : tree high0, int in1_p, tree low1, tree high1)
5835 : : {
5836 : 2985444 : bool no_overlap;
5837 : 2985444 : int subset;
5838 : 2985444 : int temp;
5839 : 2985444 : tree tem;
5840 : 2985444 : int in_p;
5841 : 2985444 : tree low, high;
5842 : 2985444 : int lowequal = ((low0 == 0 && low1 == 0)
5843 : 2985444 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5844 : 2985444 : low0, 0, low1, 0)));
5845 : 2985444 : int highequal = ((high0 == 0 && high1 == 0)
5846 : 2985444 : || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5847 : 2985444 : high0, 1, high1, 1)));
5848 : :
5849 : : /* Make range 0 be the range that starts first, or ends last if they
5850 : : start at the same value. Swap them if it isn't. */
5851 : 2985444 : if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5852 : : low0, 0, low1, 0))
5853 : 2985444 : || (lowequal
5854 : 873878 : && integer_onep (range_binop (GT_EXPR, integer_type_node,
5855 : : high1, 1, high0, 1))))
5856 : : {
5857 : : temp = in0_p, in0_p = in1_p, in1_p = temp;
5858 : : tem = low0, low0 = low1, low1 = tem;
5859 : : tem = high0, high0 = high1, high1 = tem;
5860 : : }
5861 : :
5862 : : /* If the second range is != high1 where high1 is the type maximum of
5863 : : the type, try first merging with < high1 range. */
5864 : 2985444 : if (low1
5865 : 2985444 : && high1
5866 : 768435 : && TREE_CODE (low1) == INTEGER_CST
5867 : 768435 : && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5868 : 125658 : || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5869 : 2722598 : && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5870 : : GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5871 : 3711146 : && operand_equal_p (low1, high1, 0))
5872 : : {
5873 : 428696 : if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5874 : 428696 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5875 : : !in1_p, NULL_TREE, range_predecessor (low1)))
5876 : : return true;
5877 : : /* Similarly for the second range != low1 where low1 is the type minimum
5878 : : of the type, try first merging with > low1 range. */
5879 : 347871 : if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5880 : 347871 : && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5881 : : !in1_p, range_successor (low1), NULL_TREE))
5882 : : return true;
5883 : : }
5884 : :
5885 : : /* Now flag two cases, whether the ranges are disjoint or whether the
5886 : : second range is totally subsumed in the first. Note that the tests
5887 : : below are simplified by the ones above. */
5888 : 2844987 : no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5889 : : high0, 1, low1, 0));
5890 : 2844987 : subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5891 : : high1, 1, high0, 1));
5892 : :
5893 : : /* We now have four cases, depending on whether we are including or
5894 : : excluding the two ranges. */
5895 : 2844987 : if (in0_p && in1_p)
5896 : : {
5897 : : /* If they don't overlap, the result is false. If the second range
5898 : : is a subset it is the result. Otherwise, the range is from the start
5899 : : of the second to the end of the first. */
5900 : 1043600 : if (no_overlap)
5901 : : in_p = 0, low = high = 0;
5902 : 1041539 : else if (subset)
5903 : : in_p = 1, low = low1, high = high1;
5904 : : else
5905 : 911236 : in_p = 1, low = low1, high = high0;
5906 : : }
5907 : :
5908 : 1801387 : else if (in0_p && ! in1_p)
5909 : : {
5910 : : /* If they don't overlap, the result is the first range. If they are
5911 : : equal, the result is false. If the second range is a subset of the
5912 : : first, and the ranges begin at the same place, we go from just after
5913 : : the end of the second range to the end of the first. If the second
5914 : : range is not a subset of the first, or if it is a subset and both
5915 : : ranges end at the same place, the range starts at the start of the
5916 : : first range and ends just before the second range.
5917 : : Otherwise, we can't describe this as a single range. */
5918 : 637250 : if (no_overlap)
5919 : : in_p = 1, low = low0, high = high0;
5920 : 631964 : else if (lowequal && highequal)
5921 : : in_p = 0, low = high = 0;
5922 : 630944 : else if (subset && lowequal)
5923 : : {
5924 : 557984 : low = range_successor (high1);
5925 : 557984 : high = high0;
5926 : 557984 : in_p = 1;
5927 : 557984 : if (low == 0)
5928 : : {
5929 : : /* We are in the weird situation where high0 > high1 but
5930 : : high1 has no successor. Punt. */
5931 : : return 0;
5932 : : }
5933 : : }
5934 : 72960 : else if (! subset || highequal)
5935 : : {
5936 : 53454 : low = low0;
5937 : 53454 : high = range_predecessor (low1);
5938 : 53454 : in_p = 1;
5939 : 53454 : if (high == 0)
5940 : : {
5941 : : /* low0 < low1 but low1 has no predecessor. Punt. */
5942 : : return 0;
5943 : : }
5944 : : }
5945 : : else
5946 : : return 0;
5947 : : }
5948 : :
5949 : 1164137 : else if (! in0_p && in1_p)
5950 : : {
5951 : : /* If they don't overlap, the result is the second range. If the second
5952 : : is a subset of the first, the result is false. Otherwise,
5953 : : the range starts just after the first range and ends at the
5954 : : end of the second. */
5955 : 875470 : if (no_overlap)
5956 : : in_p = 1, low = low1, high = high1;
5957 : 871050 : else if (subset || highequal)
5958 : : in_p = 0, low = high = 0;
5959 : : else
5960 : : {
5961 : 757370 : low = range_successor (high0);
5962 : 757370 : high = high1;
5963 : 757370 : in_p = 1;
5964 : 757370 : if (low == 0)
5965 : : {
5966 : : /* high1 > high0 but high0 has no successor. Punt. */
5967 : : return 0;
5968 : : }
5969 : : }
5970 : : }
5971 : :
5972 : : else
5973 : : {
5974 : : /* The case where we are excluding both ranges. Here the complex case
5975 : : is if they don't overlap. In that case, the only time we have a
5976 : : range is if they are adjacent. If the second is a subset of the
5977 : : first, the result is the first. Otherwise, the range to exclude
5978 : : starts at the beginning of the first range and ends at the end of the
5979 : : second. */
5980 : 288667 : if (no_overlap)
5981 : : {
5982 : 186751 : if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5983 : : range_successor (high0),
5984 : : 1, low1, 0)))
5985 : : in_p = 0, low = low0, high = high1;
5986 : : else
5987 : : {
5988 : : /* Canonicalize - [min, x] into - [-, x]. */
5989 : 145544 : if (low0 && TREE_CODE (low0) == INTEGER_CST)
5990 : 144300 : switch (TREE_CODE (TREE_TYPE (low0)))
5991 : : {
5992 : 50511 : case ENUMERAL_TYPE:
5993 : 50511 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5994 : : GET_MODE_BITSIZE
5995 : 151533 : (TYPE_MODE (TREE_TYPE (low0)))))
5996 : : break;
5997 : : /* FALLTHROUGH */
5998 : 144155 : case INTEGER_TYPE:
5999 : 144155 : if (tree_int_cst_equal (low0,
6000 : 144155 : TYPE_MIN_VALUE (TREE_TYPE (low0))))
6001 : 10975 : low0 = 0;
6002 : : break;
6003 : 145 : case POINTER_TYPE:
6004 : 145 : if (TYPE_UNSIGNED (TREE_TYPE (low0))
6005 : 145 : && integer_zerop (low0))
6006 : : low0 = 0;
6007 : : break;
6008 : : default:
6009 : : break;
6010 : : }
6011 : :
6012 : : /* Canonicalize - [x, max] into - [x, -]. */
6013 : 145544 : if (high1 && TREE_CODE (high1) == INTEGER_CST)
6014 : 145346 : switch (TREE_CODE (TREE_TYPE (high1)))
6015 : : {
6016 : 50519 : case ENUMERAL_TYPE:
6017 : 50519 : if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
6018 : : GET_MODE_BITSIZE
6019 : 151557 : (TYPE_MODE (TREE_TYPE (high1)))))
6020 : : break;
6021 : : /* FALLTHROUGH */
6022 : 145201 : case INTEGER_TYPE:
6023 : 145201 : if (tree_int_cst_equal (high1,
6024 : 145201 : TYPE_MAX_VALUE (TREE_TYPE (high1))))
6025 : 7516 : high1 = 0;
6026 : : break;
6027 : 145 : case POINTER_TYPE:
6028 : 145 : if (TYPE_UNSIGNED (TREE_TYPE (high1))
6029 : 290 : && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
6030 : : high1, 1,
6031 : 157 : build_int_cst (TREE_TYPE (high1), 1),
6032 : : 1)))
6033 : 133 : high1 = 0;
6034 : : break;
6035 : : default:
6036 : : break;
6037 : : }
6038 : :
6039 : : /* The ranges might be also adjacent between the maximum and
6040 : : minimum values of the given type. For
6041 : : - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
6042 : : return + [x + 1, y - 1]. */
6043 : 145544 : if (low0 == 0 && high1 == 0)
6044 : : {
6045 : 565 : low = range_successor (high0);
6046 : 565 : high = range_predecessor (low1);
6047 : 565 : if (low == 0 || high == 0)
6048 : : return 0;
6049 : :
6050 : : in_p = 1;
6051 : : }
6052 : : else
6053 : : return 0;
6054 : : }
6055 : : }
6056 : 101916 : else if (subset)
6057 : : in_p = 0, low = low0, high = high0;
6058 : : else
6059 : 11038 : in_p = 0, low = low0, high = high1;
6060 : : }
6061 : :
6062 : 2680493 : *pin_p = in_p, *plow = low, *phigh = high;
6063 : 2680493 : return 1;
6064 : : }
6065 : :
6066 : :
6067 : : /* Subroutine of fold, looking inside expressions of the form
6068 : : A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
6069 : : are the three operands of the COND_EXPR. This function is
6070 : : being used also to optimize A op B ? C : A, by reversing the
6071 : : comparison first.
6072 : :
6073 : : Return a folded expression whose code is not a COND_EXPR
6074 : : anymore, or NULL_TREE if no folding opportunity is found. */
6075 : :
6076 : : static tree
6077 : 431715 : fold_cond_expr_with_comparison (location_t loc, tree type,
6078 : : enum tree_code comp_code,
6079 : : tree arg00, tree arg01, tree arg1, tree arg2)
6080 : : {
6081 : 431715 : tree arg1_type = TREE_TYPE (arg1);
6082 : 431715 : tree tem;
6083 : :
6084 : 431715 : STRIP_NOPS (arg1);
6085 : 431715 : STRIP_NOPS (arg2);
6086 : :
6087 : : /* If we have A op 0 ? A : -A, consider applying the following
6088 : : transformations:
6089 : :
6090 : : A == 0? A : -A same as -A
6091 : : A != 0? A : -A same as A
6092 : : A >= 0? A : -A same as abs (A)
6093 : : A > 0? A : -A same as abs (A)
6094 : : A <= 0? A : -A same as -abs (A)
6095 : : A < 0? A : -A same as -abs (A)
6096 : :
6097 : : None of these transformations work for modes with signed
6098 : : zeros. If A is +/-0, the first two transformations will
6099 : : change the sign of the result (from +0 to -0, or vice
6100 : : versa). The last four will fix the sign of the result,
6101 : : even though the original expressions could be positive or
6102 : : negative, depending on the sign of A.
6103 : :
6104 : : Note that all these transformations are correct if A is
6105 : : NaN, since the two alternatives (A and -A) are also NaNs. */
6106 : 431715 : if (!HONOR_SIGNED_ZEROS (type)
6107 : 863440 : && (FLOAT_TYPE_P (TREE_TYPE (arg01))
6108 : 431715 : ? real_zerop (arg01)
6109 : 430699 : : integer_zerop (arg01))
6110 : 1142943 : && ((TREE_CODE (arg2) == NEGATE_EXPR
6111 : 1178 : && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
6112 : : /* In the case that A is of the form X-Y, '-A' (arg2) may
6113 : : have already been folded to Y-X, check for that. */
6114 : 278383 : || (TREE_CODE (arg1) == MINUS_EXPR
6115 : 2425 : && TREE_CODE (arg2) == MINUS_EXPR
6116 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 0),
6117 : 0 : TREE_OPERAND (arg2, 1), 0)
6118 : 0 : && operand_equal_p (TREE_OPERAND (arg1, 1),
6119 : 0 : TREE_OPERAND (arg2, 0), 0))))
6120 : 1130 : switch (comp_code)
6121 : : {
6122 : 0 : case EQ_EXPR:
6123 : 0 : case UNEQ_EXPR:
6124 : 0 : tem = fold_convert_loc (loc, arg1_type, arg1);
6125 : 0 : return fold_convert_loc (loc, type, negate_expr (tem));
6126 : 0 : case NE_EXPR:
6127 : 0 : case LTGT_EXPR:
6128 : 0 : return fold_convert_loc (loc, type, arg1);
6129 : 0 : case UNGE_EXPR:
6130 : 0 : case UNGT_EXPR:
6131 : 0 : if (flag_trapping_math)
6132 : : break;
6133 : : /* Fall through. */
6134 : 1130 : case GE_EXPR:
6135 : 1130 : case GT_EXPR:
6136 : 1130 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6137 : : break;
6138 : 1114 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6139 : 1114 : return fold_convert_loc (loc, type, tem);
6140 : 0 : case UNLE_EXPR:
6141 : 0 : case UNLT_EXPR:
6142 : 0 : if (flag_trapping_math)
6143 : : break;
6144 : : /* FALLTHRU */
6145 : 0 : case LE_EXPR:
6146 : 0 : case LT_EXPR:
6147 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
6148 : : break;
6149 : 0 : if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6150 : 0 : && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
6151 : : {
6152 : : /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
6153 : : is not, invokes UB both in abs and in the negation of it.
6154 : : So, use ABSU_EXPR instead. */
6155 : 0 : tree utype = unsigned_type_for (TREE_TYPE (arg1));
6156 : 0 : tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
6157 : 0 : tem = negate_expr (tem);
6158 : 0 : return fold_convert_loc (loc, type, tem);
6159 : : }
6160 : : else
6161 : : {
6162 : 0 : tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
6163 : 0 : return negate_expr (fold_convert_loc (loc, type, tem));
6164 : : }
6165 : 0 : default:
6166 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6167 : : break;
6168 : : }
6169 : :
6170 : : /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
6171 : : A == 0 ? A : 0 is always 0 unless A is -0. Note that
6172 : : both transformations are correct when A is NaN: A != 0
6173 : : is then true, and A == 0 is false. */
6174 : :
6175 : 430601 : if (!HONOR_SIGNED_ZEROS (type)
6176 : 430601 : && integer_zerop (arg01) && integer_zerop (arg2))
6177 : : {
6178 : 234863 : if (comp_code == NE_EXPR)
6179 : 153 : return fold_convert_loc (loc, type, arg1);
6180 : 234710 : else if (comp_code == EQ_EXPR)
6181 : 0 : return build_zero_cst (type);
6182 : : }
6183 : :
6184 : : /* Try some transformations of A op B ? A : B.
6185 : :
6186 : : A == B? A : B same as B
6187 : : A != B? A : B same as A
6188 : : A >= B? A : B same as max (A, B)
6189 : : A > B? A : B same as max (B, A)
6190 : : A <= B? A : B same as min (A, B)
6191 : : A < B? A : B same as min (B, A)
6192 : :
6193 : : As above, these transformations don't work in the presence
6194 : : of signed zeros. For example, if A and B are zeros of
6195 : : opposite sign, the first two transformations will change
6196 : : the sign of the result. In the last four, the original
6197 : : expressions give different results for (A=+0, B=-0) and
6198 : : (A=-0, B=+0), but the transformed expressions do not.
6199 : :
6200 : : The first two transformations are correct if either A or B
6201 : : is a NaN. In the first transformation, the condition will
6202 : : be false, and B will indeed be chosen. In the case of the
6203 : : second transformation, the condition A != B will be true,
6204 : : and A will be chosen.
6205 : :
6206 : : The conversions to max() and min() are not correct if B is
6207 : : a number and A is not. The conditions in the original
6208 : : expressions will be false, so all four give B. The min()
6209 : : and max() versions would give a NaN instead. */
6210 : 430448 : if (!HONOR_SIGNED_ZEROS (type)
6211 : 430448 : && operand_equal_for_comparison_p (arg01, arg2)
6212 : : /* Avoid these transformations if the COND_EXPR may be used
6213 : : as an lvalue in the C++ front-end. PR c++/19199. */
6214 : 673448 : && (in_gimple_form
6215 : 15564 : || VECTOR_TYPE_P (type)
6216 : 15502 : || (! lang_GNU_CXX ()
6217 : 13141 : && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
6218 : 2361 : || ! maybe_lvalue_p (arg1)
6219 : 2342 : || ! maybe_lvalue_p (arg2)))
6220 : : {
6221 : 241398 : tree comp_op0 = arg00;
6222 : 241398 : tree comp_op1 = arg01;
6223 : 241398 : tree comp_type = TREE_TYPE (comp_op0);
6224 : :
6225 : 241398 : switch (comp_code)
6226 : : {
6227 : 0 : case EQ_EXPR:
6228 : 0 : return fold_convert_loc (loc, type, arg2);
6229 : 1 : case NE_EXPR:
6230 : 1 : return fold_convert_loc (loc, type, arg1);
6231 : 5773 : case LE_EXPR:
6232 : 5773 : case LT_EXPR:
6233 : 5773 : case UNLE_EXPR:
6234 : 5773 : case UNLT_EXPR:
6235 : : /* In C++ a ?: expression can be an lvalue, so put the
6236 : : operand which will be used if they are equal first
6237 : : so that we can convert this back to the
6238 : : corresponding COND_EXPR. */
6239 : 5773 : if (!HONOR_NANS (arg1))
6240 : : {
6241 : 5773 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6242 : 5773 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6243 : 11546 : tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6244 : 5773 : ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6245 : 4409 : : fold_build2_loc (loc, MIN_EXPR, comp_type,
6246 : : comp_op1, comp_op0);
6247 : 5773 : return fold_convert_loc (loc, type, tem);
6248 : : }
6249 : : break;
6250 : 235624 : case GE_EXPR:
6251 : 235624 : case GT_EXPR:
6252 : 235624 : case UNGE_EXPR:
6253 : 235624 : case UNGT_EXPR:
6254 : 235624 : if (!HONOR_NANS (arg1))
6255 : : {
6256 : 235622 : comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6257 : 235622 : comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6258 : 471244 : tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6259 : 235622 : ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6260 : 3421 : : fold_build2_loc (loc, MAX_EXPR, comp_type,
6261 : : comp_op1, comp_op0);
6262 : 235622 : return fold_convert_loc (loc, type, tem);
6263 : : }
6264 : : break;
6265 : 0 : case UNEQ_EXPR:
6266 : 0 : if (!HONOR_NANS (arg1))
6267 : 0 : return fold_convert_loc (loc, type, arg2);
6268 : : break;
6269 : 0 : case LTGT_EXPR:
6270 : 0 : if (!HONOR_NANS (arg1))
6271 : 0 : return fold_convert_loc (loc, type, arg1);
6272 : : break;
6273 : 0 : default:
6274 : 0 : gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6275 : : break;
6276 : : }
6277 : : }
6278 : :
6279 : : return NULL_TREE;
6280 : : }
6281 : :
6282 : :
6283 : :
6284 : : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6285 : : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6286 : : (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6287 : : false) >= 2)
6288 : : #endif
6289 : :
6290 : : /* EXP is some logical combination of boolean tests. See if we can
6291 : : merge it into some range test. Return the new tree if so. */
6292 : :
6293 : : static tree
6294 : 21766403 : fold_range_test (location_t loc, enum tree_code code, tree type,
6295 : : tree op0, tree op1)
6296 : : {
6297 : 21766403 : int or_op = (code == TRUTH_ORIF_EXPR
6298 : 21766403 : || code == TRUTH_OR_EXPR);
6299 : 21766403 : int in0_p, in1_p, in_p;
6300 : 21766403 : tree low0, low1, low, high0, high1, high;
6301 : 21766403 : bool strict_overflow_p = false;
6302 : 21766403 : tree tem, lhs, rhs;
6303 : 21766403 : const char * const warnmsg = G_("assuming signed overflow does not occur "
6304 : : "when simplifying range test");
6305 : :
6306 : 21766403 : if (!INTEGRAL_TYPE_P (type))
6307 : : return 0;
6308 : :
6309 : 21766403 : lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6310 : : /* If op0 is known true or false and this is a short-circuiting
6311 : : operation we must not merge with op1 since that makes side-effects
6312 : : unconditional. So special-case this. */
6313 : 21766403 : if (!lhs
6314 : 2 : && ((code == TRUTH_ORIF_EXPR && in0_p)
6315 : 1 : || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6316 : : return op0;
6317 : 21766401 : rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6318 : :
6319 : : /* If this is an OR operation, invert both sides; we will invert
6320 : : again at the end. */
6321 : 21766401 : if (or_op)
6322 : 10785195 : in0_p = ! in0_p, in1_p = ! in1_p;
6323 : :
6324 : : /* If both expressions are the same, if we can merge the ranges, and we
6325 : : can build the range test, return it or it inverted. If one of the
6326 : : ranges is always true or always false, consider it to be the same
6327 : : expression as the other. */
6328 : 21733511 : if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6329 : 1314571 : && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6330 : : in1_p, low1, high1)
6331 : 22943041 : && (tem = (build_range_check (loc, type,
6332 : : lhs != 0 ? lhs
6333 : 0 : : rhs != 0 ? rhs : integer_zero_node,
6334 : : in_p, low, high))) != 0)
6335 : : {
6336 : 1176640 : if (strict_overflow_p)
6337 : 273 : fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6338 : 1176640 : return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6339 : : }
6340 : :
6341 : : /* On machines where the branch cost is expensive, if this is a
6342 : : short-circuited branch and the underlying object on both sides
6343 : : is the same, make a non-short-circuit operation. */
6344 : 20589761 : bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6345 : 20589761 : if (param_logical_op_non_short_circuit != -1)
6346 : 7653 : logical_op_non_short_circuit
6347 : 7653 : = param_logical_op_non_short_circuit;
6348 : 20589761 : if (logical_op_non_short_circuit
6349 : 20585903 : && !sanitize_coverage_p ()
6350 : 20585900 : && lhs != 0 && rhs != 0
6351 : 20585682 : && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6352 : 24863690 : && operand_equal_p (lhs, rhs, 0))
6353 : : {
6354 : : /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6355 : : unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6356 : : which cases we can't do this. */
6357 : 107454 : if (simple_operand_p (lhs))
6358 : 61428 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6359 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6360 : 31219 : type, op0, op1);
6361 : :
6362 : 76235 : else if (!lang_hooks.decls.global_bindings_p ()
6363 : 76235 : && !CONTAINS_PLACEHOLDER_P (lhs))
6364 : : {
6365 : 75578 : tree common = save_expr (lhs);
6366 : :
6367 : 127547 : if ((lhs = build_range_check (loc, type, common,
6368 : 51969 : or_op ? ! in0_p : in0_p,
6369 : : low0, high0)) != 0
6370 : 127547 : && (rhs = build_range_check (loc, type, common,
6371 : 51969 : or_op ? ! in1_p : in1_p,
6372 : : low1, high1)) != 0)
6373 : : {
6374 : 75578 : if (strict_overflow_p)
6375 : 0 : fold_overflow_warning (warnmsg,
6376 : : WARN_STRICT_OVERFLOW_COMPARISON);
6377 : 127547 : return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6378 : : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6379 : 75578 : type, lhs, rhs);
6380 : : }
6381 : : }
6382 : : }
6383 : :
6384 : : return 0;
6385 : : }
6386 : :
6387 : : /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6388 : : bit value. Arrange things so the extra bits will be set to zero if and
6389 : : only if C is signed-extended to its full width. If MASK is nonzero,
6390 : : it is an INTEGER_CST that should be AND'ed with the extra bits. */
6391 : :
6392 : : static tree
6393 : 75078 : unextend (tree c, int p, int unsignedp, tree mask)
6394 : : {
6395 : 75078 : tree type = TREE_TYPE (c);
6396 : 75078 : int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
6397 : 75078 : tree temp;
6398 : :
6399 : 75078 : if (p == modesize || unsignedp)
6400 : : return c;
6401 : :
6402 : : /* We work by getting just the sign bit into the low-order bit, then
6403 : : into the high-order bit, then sign-extend. We then XOR that value
6404 : : with C. */
6405 : 1473 : temp = build_int_cst (TREE_TYPE (c),
6406 : 1473 : wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
6407 : :
6408 : : /* We must use a signed type in order to get an arithmetic right shift.
6409 : : However, we must also avoid introducing accidental overflows, so that
6410 : : a subsequent call to integer_zerop will work. Hence we must
6411 : : do the type conversion here. At this point, the constant is either
6412 : : zero or one, and the conversion to a signed type can never overflow.
6413 : : We could get an overflow if this conversion is done anywhere else. */
6414 : 1473 : if (TYPE_UNSIGNED (type))
6415 : 1473 : temp = fold_convert (signed_type_for (type), temp);
6416 : :
6417 : 1473 : temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
6418 : 1473 : temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
6419 : 1473 : if (mask != 0)
6420 : 163 : temp = const_binop (BIT_AND_EXPR, temp,
6421 : 163 : fold_convert (TREE_TYPE (c), mask));
6422 : : /* If necessary, convert the type back to match the type of C. */
6423 : 1473 : if (TYPE_UNSIGNED (type))
6424 : 1473 : temp = fold_convert (type, temp);
6425 : :
6426 : 1473 : return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
6427 : : }
6428 : :
6429 : : /* For an expression that has the form
6430 : : (A && B) || ~B
6431 : : or
6432 : : (A || B) && ~B,
6433 : : we can drop one of the inner expressions and simplify to
6434 : : A || ~B
6435 : : or
6436 : : A && ~B
6437 : : LOC is the location of the resulting expression. OP is the inner
6438 : : logical operation; the left-hand side in the examples above, while CMPOP
6439 : : is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6440 : : removing a condition that guards another, as in
6441 : : (A != NULL && A->...) || A == NULL
6442 : : which we must not transform. If RHS_ONLY is true, only eliminate the
6443 : : right-most operand of the inner logical operation. */
6444 : :
6445 : : static tree
6446 : 131610 : merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6447 : : bool rhs_only)
6448 : : {
6449 : 131610 : enum tree_code code = TREE_CODE (cmpop);
6450 : 131610 : enum tree_code truthop_code = TREE_CODE (op);
6451 : 131610 : tree lhs = TREE_OPERAND (op, 0);
6452 : 131610 : tree rhs = TREE_OPERAND (op, 1);
6453 : 131610 : tree orig_lhs = lhs, orig_rhs = rhs;
6454 : 131610 : enum tree_code rhs_code = TREE_CODE (rhs);
6455 : 131610 : enum tree_code lhs_code = TREE_CODE (lhs);
6456 : 131610 : enum tree_code inv_code;
6457 : :
6458 : 131610 : if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6459 : : return NULL_TREE;
6460 : :
6461 : 86872 : if (TREE_CODE_CLASS (code) != tcc_comparison)
6462 : : return NULL_TREE;
6463 : :
6464 : 54058 : tree type = TREE_TYPE (TREE_OPERAND (cmpop, 0));
6465 : :
6466 : 54058 : if (rhs_code == truthop_code)
6467 : : {
6468 : 29 : tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6469 : 29 : if (newrhs != NULL_TREE)
6470 : : {
6471 : 0 : rhs = newrhs;
6472 : 0 : rhs_code = TREE_CODE (rhs);
6473 : : }
6474 : : }
6475 : 54058 : if (lhs_code == truthop_code && !rhs_only)
6476 : : {
6477 : 454 : tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6478 : 454 : if (newlhs != NULL_TREE)
6479 : : {
6480 : 0 : lhs = newlhs;
6481 : 0 : lhs_code = TREE_CODE (lhs);
6482 : : }
6483 : : }
6484 : :
6485 : 54058 : inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6486 : 54058 : if (inv_code == rhs_code
6487 : 693 : && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6488 : 54094 : && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6489 : : return lhs;
6490 : 54045 : if (!rhs_only && inv_code == lhs_code
6491 : 609 : && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6492 : 54122 : && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6493 : : return rhs;
6494 : 53969 : if (rhs != orig_rhs || lhs != orig_lhs)
6495 : 0 : return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6496 : 0 : lhs, rhs);
6497 : : return NULL_TREE;
6498 : : }
6499 : :
6500 : : /* Find ways of folding logical expressions of LHS and RHS:
6501 : : Try to merge two comparisons to the same innermost item.
6502 : : Look for range tests like "ch >= '0' && ch <= '9'".
6503 : : Look for combinations of simple terms on machines with expensive branches
6504 : : and evaluate the RHS unconditionally.
6505 : :
6506 : : For example, if we have p->a == 2 && p->b == 4 and we can make an
6507 : : object large enough to span both A and B, we can do this with a comparison
6508 : : against the object ANDed with the a mask.
6509 : :
6510 : : If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6511 : : operations to do this with one comparison.
6512 : :
6513 : : We check for both normal comparisons and the BIT_AND_EXPRs made this by
6514 : : function and the one above.
6515 : :
6516 : : CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6517 : : TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6518 : :
6519 : : TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6520 : : two operands.
6521 : :
6522 : : We return the simplified tree or 0 if no optimization is possible. */
6523 : :
6524 : : static tree
6525 : 21156126 : fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6526 : : tree lhs, tree rhs)
6527 : : {
6528 : : /* If this is the "or" of two comparisons, we can do something if
6529 : : the comparisons are NE_EXPR. If this is the "and", we can do something
6530 : : if the comparisons are EQ_EXPR. I.e.,
6531 : : (a->b == 2 && a->c == 4) can become (a->new == NEW).
6532 : :
6533 : : WANTED_CODE is this operation code. For single bit fields, we can
6534 : : convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6535 : : comparison for one-bit fields. */
6536 : :
6537 : 21156126 : enum tree_code wanted_code;
6538 : 21156126 : enum tree_code lcode, rcode;
6539 : 21156126 : tree ll_arg, lr_arg, rl_arg, rr_arg;
6540 : 21156126 : tree ll_inner, lr_inner, rl_inner, rr_inner;
6541 : 21156126 : HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
6542 : 21156126 : HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
6543 : 21156126 : HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
6544 : 21156126 : HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
6545 : 21156126 : int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
6546 : 21156126 : int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
6547 : 21156126 : machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
6548 : 21156126 : scalar_int_mode lnmode, rnmode;
6549 : 21156126 : tree ll_mask, lr_mask, rl_mask, rr_mask;
6550 : 21156126 : tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
6551 : 21156126 : tree l_const, r_const;
6552 : 21156126 : tree lntype, rntype, result;
6553 : 21156126 : HOST_WIDE_INT first_bit, end_bit;
6554 : 21156126 : int volatilep;
6555 : :
6556 : : /* Start by getting the comparison codes. Fail if anything is volatile.
6557 : : If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6558 : : it were surrounded with a NE_EXPR. */
6559 : :
6560 : 21156126 : if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6561 : : return 0;
6562 : :
6563 : 19049998 : lcode = TREE_CODE (lhs);
6564 : 19049998 : rcode = TREE_CODE (rhs);
6565 : :
6566 : 19049998 : if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6567 : : {
6568 : 0 : lhs = build2 (NE_EXPR, truth_type, lhs,
6569 : 0 : build_int_cst (TREE_TYPE (lhs), 0));
6570 : 0 : lcode = NE_EXPR;
6571 : : }
6572 : :
6573 : 19049998 : if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6574 : : {
6575 : 0 : rhs = build2 (NE_EXPR, truth_type, rhs,
6576 : 0 : build_int_cst (TREE_TYPE (rhs), 0));
6577 : 0 : rcode = NE_EXPR;
6578 : : }
6579 : :
6580 : 19049998 : if (TREE_CODE_CLASS (lcode) != tcc_comparison
6581 : 16925738 : || TREE_CODE_CLASS (rcode) != tcc_comparison)
6582 : : return 0;
6583 : :
6584 : 15895941 : ll_arg = TREE_OPERAND (lhs, 0);
6585 : 15895941 : lr_arg = TREE_OPERAND (lhs, 1);
6586 : 15895941 : rl_arg = TREE_OPERAND (rhs, 0);
6587 : 15895941 : rr_arg = TREE_OPERAND (rhs, 1);
6588 : :
6589 : : /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6590 : 15895941 : if (simple_operand_p (ll_arg)
6591 : 15895941 : && simple_operand_p (lr_arg))
6592 : : {
6593 : 12932648 : if (operand_equal_p (ll_arg, rl_arg, 0)
6594 : 12932648 : && operand_equal_p (lr_arg, rr_arg, 0))
6595 : : {
6596 : 19612 : result = combine_comparisons (loc, code, lcode, rcode,
6597 : : truth_type, ll_arg, lr_arg);
6598 : 19612 : if (result)
6599 : : return result;
6600 : : }
6601 : 12913036 : else if (operand_equal_p (ll_arg, rr_arg, 0)
6602 : 12913036 : && operand_equal_p (lr_arg, rl_arg, 0))
6603 : : {
6604 : 286 : result = combine_comparisons (loc, code, lcode,
6605 : : swap_tree_comparison (rcode),
6606 : : truth_type, ll_arg, lr_arg);
6607 : 286 : if (result)
6608 : : return result;
6609 : : }
6610 : : }
6611 : :
6612 : 31753256 : code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6613 : 15876628 : ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6614 : :
6615 : : /* If the RHS can be evaluated unconditionally and its operands are
6616 : : simple, it wins to evaluate the RHS unconditionally on machines
6617 : : with expensive branches. In this case, this isn't a comparison
6618 : : that can be merged. */
6619 : :
6620 : 15876628 : if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6621 : : false) >= 2
6622 : 15876525 : && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6623 : 14906943 : && simple_operand_p (rl_arg)
6624 : 24861169 : && simple_operand_p (rr_arg))
6625 : : {
6626 : : /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6627 : 9971379 : if (code == TRUTH_OR_EXPR
6628 : 1297789 : && lcode == NE_EXPR && integer_zerop (lr_arg)
6629 : 513445 : && rcode == NE_EXPR && integer_zerop (rr_arg)
6630 : 22626 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6631 : 9990769 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6632 : 38248 : return build2_loc (loc, NE_EXPR, truth_type,
6633 : 19124 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6634 : : ll_arg, rl_arg),
6635 : 38248 : build_int_cst (TREE_TYPE (ll_arg), 0));
6636 : :
6637 : : /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6638 : 9952255 : if (code == TRUTH_AND_EXPR
6639 : 1446561 : && lcode == EQ_EXPR && integer_zerop (lr_arg)
6640 : 646477 : && rcode == EQ_EXPR && integer_zerop (rr_arg)
6641 : 5169 : && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6642 : 9953668 : && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6643 : 2360 : return build2_loc (loc, EQ_EXPR, truth_type,
6644 : 1180 : build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6645 : : ll_arg, rl_arg),
6646 : 2360 : build_int_cst (TREE_TYPE (ll_arg), 0));
6647 : : }
6648 : :
6649 : : /* See if the comparisons can be merged. Then get all the parameters for
6650 : : each side. */
6651 : :
6652 : 15856324 : if ((lcode != EQ_EXPR && lcode != NE_EXPR)
6653 : 8216477 : || (rcode != EQ_EXPR && rcode != NE_EXPR))
6654 : : return 0;
6655 : :
6656 : 2867750 : ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
6657 : 2867750 : volatilep = 0;
6658 : 2867750 : ll_inner = decode_field_reference (loc, &ll_arg,
6659 : : &ll_bitsize, &ll_bitpos, &ll_mode,
6660 : : &ll_unsignedp, &ll_reversep, &volatilep,
6661 : : &ll_mask, &ll_and_mask);
6662 : 2867750 : lr_inner = decode_field_reference (loc, &lr_arg,
6663 : : &lr_bitsize, &lr_bitpos, &lr_mode,
6664 : : &lr_unsignedp, &lr_reversep, &volatilep,
6665 : : &lr_mask, &lr_and_mask);
6666 : 2867750 : rl_inner = decode_field_reference (loc, &rl_arg,
6667 : : &rl_bitsize, &rl_bitpos, &rl_mode,
6668 : : &rl_unsignedp, &rl_reversep, &volatilep,
6669 : : &rl_mask, &rl_and_mask);
6670 : 2867750 : rr_inner = decode_field_reference (loc, &rr_arg,
6671 : : &rr_bitsize, &rr_bitpos, &rr_mode,
6672 : : &rr_unsignedp, &rr_reversep, &volatilep,
6673 : : &rr_mask, &rr_and_mask);
6674 : :
6675 : : /* It must be true that the inner operation on the lhs of each
6676 : : comparison must be the same if we are to be able to do anything.
6677 : : Then see if we have constants. If not, the same must be true for
6678 : : the rhs's. */
6679 : 2867750 : if (volatilep
6680 : 2867750 : || ll_reversep != rl_reversep
6681 : 2867730 : || ll_inner == 0 || rl_inner == 0
6682 : 2942813 : || ! operand_equal_p (ll_inner, rl_inner, 0))
6683 : 2807418 : return 0;
6684 : :
6685 : 60332 : if (TREE_CODE (lr_arg) == INTEGER_CST
6686 : 56003 : && TREE_CODE (rr_arg) == INTEGER_CST)
6687 : : {
6688 : 55341 : l_const = lr_arg, r_const = rr_arg;
6689 : 55341 : lr_reversep = ll_reversep;
6690 : : }
6691 : 4991 : else if (lr_reversep != rr_reversep
6692 : 4991 : || lr_inner == 0 || rr_inner == 0
6693 : 7747 : || ! operand_equal_p (lr_inner, rr_inner, 0))
6694 : 2365 : return 0;
6695 : : else
6696 : : l_const = r_const = 0;
6697 : :
6698 : : /* If either comparison code is not correct for our logical operation,
6699 : : fail. However, we can convert a one-bit comparison against zero into
6700 : : the opposite comparison against that bit being set in the field. */
6701 : :
6702 : 57967 : wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
6703 : 57967 : if (lcode != wanted_code)
6704 : : {
6705 : 2084 : if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
6706 : : {
6707 : : /* Make the left operand unsigned, since we are only interested
6708 : : in the value of one bit. Otherwise we are doing the wrong
6709 : : thing below. */
6710 : 1114 : ll_unsignedp = 1;
6711 : 1114 : l_const = ll_mask;
6712 : : }
6713 : : else
6714 : 970 : return 0;
6715 : : }
6716 : :
6717 : : /* This is analogous to the code for l_const above. */
6718 : 56997 : if (rcode != wanted_code)
6719 : : {
6720 : 2259 : if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
6721 : : {
6722 : 1276 : rl_unsignedp = 1;
6723 : 1276 : r_const = rl_mask;
6724 : : }
6725 : : else
6726 : 983 : return 0;
6727 : : }
6728 : :
6729 : : /* See if we can find a mode that contains both fields being compared on
6730 : : the left. If we can't, fail. Otherwise, update all constants and masks
6731 : : to be relative to a field of that size. */
6732 : 56014 : first_bit = MIN (ll_bitpos, rl_bitpos);
6733 : 56014 : end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
6734 : 56014 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6735 : 57293 : TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
6736 : : volatilep, &lnmode))
6737 : : return 0;
6738 : :
6739 : 37741 : lnbitsize = GET_MODE_BITSIZE (lnmode);
6740 : 37741 : lnbitpos = first_bit & ~ (lnbitsize - 1);
6741 : 37741 : lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
6742 : 37741 : xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
6743 : :
6744 : 37741 : if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6745 : : {
6746 : 6 : xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
6747 : 6 : xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
6748 : : }
6749 : :
6750 : 37741 : ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6751 : : size_int (xll_bitpos));
6752 : 37741 : rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6753 : : size_int (xrl_bitpos));
6754 : 37741 : if (ll_mask == NULL_TREE || rl_mask == NULL_TREE)
6755 : : return 0;
6756 : :
6757 : 37735 : if (l_const)
6758 : : {
6759 : 37539 : l_const = fold_convert_loc (loc, lntype, l_const);
6760 : 37539 : l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6761 : 37539 : l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
6762 : 37539 : if (l_const == NULL_TREE)
6763 : : return 0;
6764 : 37539 : if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6765 : : fold_build1_loc (loc, BIT_NOT_EXPR,
6766 : : lntype, ll_mask))))
6767 : : {
6768 : 0 : warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6769 : :
6770 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6771 : : }
6772 : : }
6773 : 37735 : if (r_const)
6774 : : {
6775 : 37539 : r_const = fold_convert_loc (loc, lntype, r_const);
6776 : 37539 : r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6777 : 37539 : r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
6778 : 37539 : if (r_const == NULL_TREE)
6779 : : return 0;
6780 : 37539 : if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6781 : : fold_build1_loc (loc, BIT_NOT_EXPR,
6782 : : lntype, rl_mask))))
6783 : : {
6784 : 0 : warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6785 : :
6786 : 0 : return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6787 : : }
6788 : : }
6789 : :
6790 : : /* If the right sides are not constant, do the same for it. Also,
6791 : : disallow this optimization if a size, signedness or storage order
6792 : : mismatch occurs between the left and right sides. */
6793 : 37735 : if (l_const == 0)
6794 : : {
6795 : 196 : if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6796 : 196 : || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6797 : 196 : || ll_reversep != lr_reversep
6798 : : /* Make sure the two fields on the right
6799 : : correspond to the left without being swapped. */
6800 : 190 : || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6801 : : return 0;
6802 : :
6803 : 190 : first_bit = MIN (lr_bitpos, rr_bitpos);
6804 : 190 : end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6805 : 190 : if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6806 : 190 : TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6807 : : volatilep, &rnmode))
6808 : : return 0;
6809 : :
6810 : 190 : rnbitsize = GET_MODE_BITSIZE (rnmode);
6811 : 190 : rnbitpos = first_bit & ~ (rnbitsize - 1);
6812 : 190 : rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6813 : 190 : xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6814 : :
6815 : 190 : if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6816 : : {
6817 : 0 : xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6818 : 0 : xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6819 : : }
6820 : :
6821 : 190 : lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6822 : : rntype, lr_mask),
6823 : : size_int (xlr_bitpos));
6824 : 190 : rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6825 : : rntype, rr_mask),
6826 : : size_int (xrr_bitpos));
6827 : 190 : if (lr_mask == NULL_TREE || rr_mask == NULL_TREE)
6828 : : return 0;
6829 : :
6830 : : /* Make a mask that corresponds to both fields being compared.
6831 : : Do this for both items being compared. If the operands are the
6832 : : same size and the bits being compared are in the same position
6833 : : then we can do this by masking both and comparing the masked
6834 : : results. */
6835 : 190 : ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6836 : 190 : lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6837 : 190 : if (lnbitsize == rnbitsize
6838 : 190 : && xll_bitpos == xlr_bitpos
6839 : 154 : && lnbitpos >= 0
6840 : 154 : && rnbitpos >= 0)
6841 : : {
6842 : 154 : lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6843 : : lntype, lnbitsize, lnbitpos,
6844 : 154 : ll_unsignedp || rl_unsignedp, ll_reversep);
6845 : 154 : if (! all_ones_mask_p (ll_mask, lnbitsize))
6846 : 154 : lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6847 : :
6848 : 154 : rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6849 : : rntype, rnbitsize, rnbitpos,
6850 : 154 : lr_unsignedp || rr_unsignedp, lr_reversep);
6851 : 154 : if (! all_ones_mask_p (lr_mask, rnbitsize))
6852 : 154 : rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6853 : :
6854 : 154 : return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6855 : : }
6856 : :
6857 : : /* There is still another way we can do something: If both pairs of
6858 : : fields being compared are adjacent, we may be able to make a wider
6859 : : field containing them both.
6860 : :
6861 : : Note that we still must mask the lhs/rhs expressions. Furthermore,
6862 : : the mask must be shifted to account for the shift done by
6863 : : make_bit_field_ref. */
6864 : 36 : if (((ll_bitsize + ll_bitpos == rl_bitpos
6865 : 18 : && lr_bitsize + lr_bitpos == rr_bitpos)
6866 : 18 : || (ll_bitpos == rl_bitpos + rl_bitsize
6867 : 18 : && lr_bitpos == rr_bitpos + rr_bitsize))
6868 : 36 : && ll_bitpos >= 0
6869 : 36 : && rl_bitpos >= 0
6870 : 36 : && lr_bitpos >= 0
6871 : 36 : && rr_bitpos >= 0)
6872 : : {
6873 : 36 : tree type;
6874 : :
6875 : 54 : lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6876 : : ll_bitsize + rl_bitsize,
6877 : : MIN (ll_bitpos, rl_bitpos),
6878 : : ll_unsignedp, ll_reversep);
6879 : 54 : rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6880 : : lr_bitsize + rr_bitsize,
6881 : : MIN (lr_bitpos, rr_bitpos),
6882 : : lr_unsignedp, lr_reversep);
6883 : :
6884 : 36 : ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6885 : 36 : size_int (MIN (xll_bitpos, xrl_bitpos)));
6886 : 36 : lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6887 : 36 : size_int (MIN (xlr_bitpos, xrr_bitpos)));
6888 : 36 : if (ll_mask == NULL_TREE || lr_mask == NULL_TREE)
6889 : : return 0;
6890 : :
6891 : : /* Convert to the smaller type before masking out unwanted bits. */
6892 : 36 : type = lntype;
6893 : 36 : if (lntype != rntype)
6894 : : {
6895 : 0 : if (lnbitsize > rnbitsize)
6896 : : {
6897 : 0 : lhs = fold_convert_loc (loc, rntype, lhs);
6898 : 0 : ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6899 : 0 : type = rntype;
6900 : : }
6901 : 0 : else if (lnbitsize < rnbitsize)
6902 : : {
6903 : 0 : rhs = fold_convert_loc (loc, lntype, rhs);
6904 : 0 : lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6905 : 0 : type = lntype;
6906 : : }
6907 : : }
6908 : :
6909 : 36 : if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6910 : 36 : lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6911 : :
6912 : 36 : if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6913 : 36 : rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6914 : :
6915 : 36 : return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6916 : : }
6917 : :
6918 : : return 0;
6919 : : }
6920 : :
6921 : : /* Handle the case of comparisons with constants. If there is something in
6922 : : common between the masks, those bits of the constants must be the same.
6923 : : If not, the condition is always false. Test for this to avoid generating
6924 : : incorrect code below. */
6925 : 37539 : result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6926 : 37539 : if (! integer_zerop (result)
6927 : 37545 : && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6928 : 6 : const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6929 : : {
6930 : 0 : if (wanted_code == NE_EXPR)
6931 : : {
6932 : 0 : warning (0, "%<or%> of unmatched not-equal tests is always 1");
6933 : 0 : return constant_boolean_node (true, truth_type);
6934 : : }
6935 : : else
6936 : : {
6937 : 0 : warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6938 : 0 : return constant_boolean_node (false, truth_type);
6939 : : }
6940 : : }
6941 : :
6942 : 37539 : if (lnbitpos < 0)
6943 : : return 0;
6944 : :
6945 : : /* Construct the expression we will return. First get the component
6946 : : reference we will make. Unless the mask is all ones the width of
6947 : : that field, perform the mask operation. Then compare with the
6948 : : merged constant. */
6949 : 37533 : result = make_bit_field_ref (loc, ll_inner, ll_arg,
6950 : : lntype, lnbitsize, lnbitpos,
6951 : 37533 : ll_unsignedp || rl_unsignedp, ll_reversep);
6952 : :
6953 : 37533 : ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6954 : 37533 : if (! all_ones_mask_p (ll_mask, lnbitsize))
6955 : 37533 : result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6956 : :
6957 : 37533 : return build2_loc (loc, wanted_code, truth_type, result,
6958 : 37533 : const_binop (BIT_IOR_EXPR, l_const, r_const));
6959 : : }
6960 : :
6961 : : /* T is an integer expression that is being multiplied, divided, or taken a
6962 : : modulus (CODE says which and what kind of divide or modulus) by a
6963 : : constant C. See if we can eliminate that operation by folding it with
6964 : : other operations already in T. WIDE_TYPE, if non-null, is a type that
6965 : : should be used for the computation if wider than our type.
6966 : :
6967 : : For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6968 : : (X * 2) + (Y * 4). We must, however, be assured that either the original
6969 : : expression would not overflow or that overflow is undefined for the type
6970 : : in the language in question.
6971 : :
6972 : : If we return a non-null expression, it is an equivalent form of the
6973 : : original computation, but need not be in the original type.
6974 : :
6975 : : We set *STRICT_OVERFLOW_P to true if the return values depends on
6976 : : signed overflow being undefined. Otherwise we do not change
6977 : : *STRICT_OVERFLOW_P. */
6978 : :
6979 : : static tree
6980 : 77209815 : extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6981 : : bool *strict_overflow_p)
6982 : : {
6983 : : /* To avoid exponential search depth, refuse to allow recursion past
6984 : : three levels. Beyond that (1) it's highly unlikely that we'll find
6985 : : something interesting and (2) we've probably processed it before
6986 : : when we built the inner expression. */
6987 : :
6988 : 77209815 : static int depth;
6989 : 77209815 : tree ret;
6990 : :
6991 : 77209815 : if (depth > 3)
6992 : : return NULL;
6993 : :
6994 : 74551349 : depth++;
6995 : 74551349 : ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6996 : 74551349 : depth--;
6997 : :
6998 : 74551349 : return ret;
6999 : : }
7000 : :
7001 : : static tree
7002 : 74551349 : extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
7003 : : bool *strict_overflow_p)
7004 : : {
7005 : 74551349 : tree type = TREE_TYPE (t);
7006 : 74551349 : enum tree_code tcode = TREE_CODE (t);
7007 : 74551349 : tree ctype = type;
7008 : 74551349 : if (wide_type)
7009 : : {
7010 : 23886281 : if (TREE_CODE (type) == BITINT_TYPE
7011 : 23886171 : || TREE_CODE (wide_type) == BITINT_TYPE)
7012 : : {
7013 : 110 : if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type))
7014 : 8188490 : ctype = wide_type;
7015 : : }
7016 : 23886171 : else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
7017 : 47772342 : > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
7018 : 8188490 : ctype = wide_type;
7019 : : }
7020 : 74551349 : tree t1, t2;
7021 : 74551349 : bool same_p = tcode == code;
7022 : 74551349 : tree op0 = NULL_TREE, op1 = NULL_TREE;
7023 : 74551349 : bool sub_strict_overflow_p;
7024 : :
7025 : : /* Don't deal with constants of zero here; they confuse the code below. */
7026 : 74551349 : if (integer_zerop (c))
7027 : : return NULL_TREE;
7028 : :
7029 : 74531473 : if (TREE_CODE_CLASS (tcode) == tcc_unary)
7030 : 29177598 : op0 = TREE_OPERAND (t, 0);
7031 : :
7032 : 74531473 : if (TREE_CODE_CLASS (tcode) == tcc_binary)
7033 : 9712851 : op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
7034 : :
7035 : : /* Note that we need not handle conditional operations here since fold
7036 : : already handles those cases. So just do arithmetic here. */
7037 : 74531473 : switch (tcode)
7038 : : {
7039 : 3480308 : case INTEGER_CST:
7040 : : /* For a constant, we can always simplify if we are a multiply
7041 : : or (for divide and modulus) if it is a multiple of our constant. */
7042 : 3480308 : if (code == MULT_EXPR
7043 : 4468225 : || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
7044 : 987917 : TYPE_SIGN (type)))
7045 : : {
7046 : 2791695 : tree tem = const_binop (code, fold_convert (ctype, t),
7047 : : fold_convert (ctype, c));
7048 : : /* If the multiplication overflowed, we lost information on it.
7049 : : See PR68142 and PR69845. */
7050 : 2791695 : if (TREE_OVERFLOW (tem))
7051 : : return NULL_TREE;
7052 : : return tem;
7053 : : }
7054 : : break;
7055 : :
7056 : 28648139 : CASE_CONVERT: case NON_LVALUE_EXPR:
7057 : 28648139 : if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
7058 : : break;
7059 : : /* If op0 is an expression ... */
7060 : 27741758 : if ((COMPARISON_CLASS_P (op0)
7061 : : || UNARY_CLASS_P (op0)
7062 : 27741758 : || BINARY_CLASS_P (op0)
7063 : 24895926 : || VL_EXP_CLASS_P (op0)
7064 : 24860744 : || EXPRESSION_CLASS_P (op0))
7065 : : /* ... and has wrapping overflow, and its type is smaller
7066 : : than ctype, then we cannot pass through as widening. */
7067 : 27850426 : && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
7068 : 1034399 : && (TYPE_PRECISION (ctype)
7069 : 1034399 : > TYPE_PRECISION (TREE_TYPE (op0))))
7070 : : /* ... or this is a truncation (t is narrower than op0),
7071 : : then we cannot pass through this narrowing. */
7072 : 2417533 : || (TYPE_PRECISION (type)
7073 : 2417533 : < TYPE_PRECISION (TREE_TYPE (op0)))
7074 : : /* ... or signedness changes for division or modulus,
7075 : : then we cannot pass through this conversion. */
7076 : 2382225 : || (code != MULT_EXPR
7077 : 97843 : && (TYPE_UNSIGNED (ctype)
7078 : 97843 : != TYPE_UNSIGNED (TREE_TYPE (op0))))
7079 : : /* ... or has undefined overflow while the converted to
7080 : : type has not, we cannot do the operation in the inner type
7081 : : as that would introduce undefined overflow. */
7082 : 2304419 : || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
7083 : 1835021 : && !TYPE_OVERFLOW_UNDEFINED (type))))
7084 : : break;
7085 : :
7086 : : /* Pass the constant down and see if we can make a simplification. If
7087 : : we can, replace this expression with the inner simplification for
7088 : : possible later conversion to our or some other type. */
7089 : 25307720 : if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
7090 : 25307720 : && TREE_CODE (t2) == INTEGER_CST
7091 : 25307720 : && !TREE_OVERFLOW (t2)
7092 : 51330820 : && (t1 = extract_muldiv (op0, t2, code,
7093 : : code == MULT_EXPR ? ctype : NULL_TREE,
7094 : : strict_overflow_p)) != 0)
7095 : : return t1;
7096 : : break;
7097 : :
7098 : 351 : case ABS_EXPR:
7099 : : /* If widening the type changes it from signed to unsigned, then we
7100 : : must avoid building ABS_EXPR itself as unsigned. */
7101 : 351 : if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
7102 : : {
7103 : 0 : tree cstype = (*signed_type_for) (ctype);
7104 : 0 : if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
7105 : : != 0)
7106 : : {
7107 : 0 : t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
7108 : 0 : return fold_convert (ctype, t1);
7109 : : }
7110 : : break;
7111 : : }
7112 : : /* If the constant is negative, we cannot simplify this. */
7113 : 351 : if (tree_int_cst_sgn (c) == -1)
7114 : : break;
7115 : : /* FALLTHROUGH */
7116 : 15589 : case NEGATE_EXPR:
7117 : : /* For division and modulus, type can't be unsigned, as e.g.
7118 : : (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
7119 : : For signed types, even with wrapping overflow, this is fine. */
7120 : 15589 : if (code != MULT_EXPR && TYPE_UNSIGNED (type))
7121 : : break;
7122 : 14321 : if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
7123 : : != 0)
7124 : 0 : return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
7125 : : break;
7126 : :
7127 : 833 : case MIN_EXPR: case MAX_EXPR:
7128 : : /* If widening the type changes the signedness, then we can't perform
7129 : : this optimization as that changes the result. */
7130 : 833 : if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
7131 : : break;
7132 : :
7133 : : /* Punt for multiplication altogether.
7134 : : MAX (1U + INT_MAX, 1U) * 2U is not equivalent to
7135 : : MAX ((1U + INT_MAX) * 2U, 1U * 2U), the former is
7136 : : 0U, the latter is 2U.
7137 : : MAX (INT_MIN / 2, 0) * -2 is not equivalent to
7138 : : MIN (INT_MIN / 2 * -2, 0 * -2), the former is
7139 : : well defined 0, the latter invokes UB.
7140 : : MAX (INT_MIN / 2, 5) * 5 is not equivalent to
7141 : : MAX (INT_MIN / 2 * 5, 5 * 5), the former is
7142 : : well defined 25, the latter invokes UB. */
7143 : 833 : if (code == MULT_EXPR)
7144 : : break;
7145 : : /* For division/modulo, punt on c being -1 for MAX, as
7146 : : MAX (INT_MIN, 0) / -1 is not equivalent to
7147 : : MIN (INT_MIN / -1, 0 / -1), the former is well defined
7148 : : 0, the latter invokes UB (or for -fwrapv is INT_MIN).
7149 : : MIN (INT_MIN, 0) / -1 already invokes UB, so the
7150 : : transformation won't make it worse. */
7151 : 8 : else if (tcode == MAX_EXPR && integer_minus_onep (c))
7152 : : break;
7153 : :
7154 : : /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
7155 : 8 : sub_strict_overflow_p = false;
7156 : 8 : if ((t1 = extract_muldiv (op0, c, code, wide_type,
7157 : : &sub_strict_overflow_p)) != 0
7158 : 8 : && (t2 = extract_muldiv (op1, c, code, wide_type,
7159 : : &sub_strict_overflow_p)) != 0)
7160 : : {
7161 : 0 : if (tree_int_cst_sgn (c) < 0)
7162 : 0 : tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
7163 : 0 : if (sub_strict_overflow_p)
7164 : 0 : *strict_overflow_p = true;
7165 : 0 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
7166 : : fold_convert (ctype, t2));
7167 : : }
7168 : : break;
7169 : :
7170 : 1020 : case LSHIFT_EXPR: case RSHIFT_EXPR:
7171 : : /* If the second operand is constant, this is a multiplication
7172 : : or floor division, by a power of two, so we can treat it that
7173 : : way unless the multiplier or divisor overflows. Signed
7174 : : left-shift overflow is implementation-defined rather than
7175 : : undefined in C90, so do not convert signed left shift into
7176 : : multiplication. */
7177 : 1020 : if (TREE_CODE (op1) == INTEGER_CST
7178 : 1003 : && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
7179 : : /* const_binop may not detect overflow correctly,
7180 : : so check for it explicitly here. */
7181 : 888 : && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
7182 : 1029 : wi::to_wide (op1))
7183 : 879 : && (t1 = fold_convert (ctype,
7184 : : const_binop (LSHIFT_EXPR, size_one_node,
7185 : : op1))) != 0
7186 : 1899 : && !TREE_OVERFLOW (t1))
7187 : 1574 : return extract_muldiv (build2 (tcode == LSHIFT_EXPR
7188 : : ? MULT_EXPR : FLOOR_DIV_EXPR,
7189 : : ctype,
7190 : : fold_convert (ctype, op0),
7191 : : t1),
7192 : 879 : c, code, wide_type, strict_overflow_p);
7193 : : break;
7194 : :
7195 : 6515830 : case PLUS_EXPR: case MINUS_EXPR:
7196 : : /* See if we can eliminate the operation on both sides. If we can, we
7197 : : can return a new PLUS or MINUS. If we can't, the only remaining
7198 : : cases where we can do anything are if the second operand is a
7199 : : constant. */
7200 : 6515830 : sub_strict_overflow_p = false;
7201 : 6515830 : t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
7202 : 6515830 : t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
7203 : 721495 : if (t1 != 0 && t2 != 0
7204 : 249374 : && TYPE_OVERFLOW_WRAPS (ctype)
7205 : 6756452 : && (code == MULT_EXPR
7206 : : /* If not multiplication, we can only do this if both operands
7207 : : are divisible by c. */
7208 : 0 : || (multiple_of_p (ctype, op0, c)
7209 : 0 : && multiple_of_p (ctype, op1, c))))
7210 : : {
7211 : 240622 : if (sub_strict_overflow_p)
7212 : 0 : *strict_overflow_p = true;
7213 : 240622 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
7214 : : fold_convert (ctype, t2));
7215 : : }
7216 : :
7217 : : /* If this was a subtraction, negate OP1 and set it to be an addition.
7218 : : This simplifies the logic below. */
7219 : 6275208 : if (tcode == MINUS_EXPR)
7220 : : {
7221 : 1506001 : tcode = PLUS_EXPR, op1 = negate_expr (op1);
7222 : : /* If OP1 was not easily negatable, the constant may be OP0. */
7223 : 1506001 : if (TREE_CODE (op0) == INTEGER_CST)
7224 : : {
7225 : 246897 : std::swap (op0, op1);
7226 : 246897 : std::swap (t1, t2);
7227 : : }
7228 : : }
7229 : :
7230 : 6275208 : if (TREE_CODE (op1) != INTEGER_CST)
7231 : : break;
7232 : :
7233 : : /* If either OP1 or C are negative, this optimization is not safe for
7234 : : some of the division and remainder types while for others we need
7235 : : to change the code. */
7236 : 2755979 : if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
7237 : : {
7238 : 163902 : if (code == CEIL_DIV_EXPR)
7239 : : code = FLOOR_DIV_EXPR;
7240 : 163900 : else if (code == FLOOR_DIV_EXPR)
7241 : : code = CEIL_DIV_EXPR;
7242 : 163647 : else if (code != MULT_EXPR
7243 : 163647 : && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
7244 : : break;
7245 : : }
7246 : :
7247 : : /* If it's a multiply or a division/modulus operation of a multiple
7248 : : of our constant, do the operation and verify it doesn't overflow. */
7249 : 2752483 : if (code == MULT_EXPR
7250 : 3737588 : || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
7251 : 985105 : TYPE_SIGN (type)))
7252 : : {
7253 : 2062988 : op1 = const_binop (code, fold_convert (ctype, op1),
7254 : : fold_convert (ctype, c));
7255 : : /* We allow the constant to overflow with wrapping semantics. */
7256 : 2062988 : if (op1 == 0
7257 : 2062988 : || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
7258 : : break;
7259 : : }
7260 : : else
7261 : : break;
7262 : :
7263 : : /* If we have an unsigned type, we cannot widen the operation since it
7264 : : will change the result if the original computation overflowed. */
7265 : 2060013 : if (TYPE_UNSIGNED (ctype) && ctype != type)
7266 : : break;
7267 : :
7268 : : /* The last case is if we are a multiply. In that case, we can
7269 : : apply the distributive law to commute the multiply and addition
7270 : : if the multiplication of the constants doesn't overflow
7271 : : and overflow is defined. With undefined overflow
7272 : : op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
7273 : : But fold_plusminus_mult_expr would factor back any power-of-two
7274 : : value so do not distribute in the first place in this case. */
7275 : 2060013 : if (code == MULT_EXPR
7276 : 1764978 : && TYPE_OVERFLOW_WRAPS (ctype)
7277 : 3517500 : && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
7278 : 188576 : return fold_build2 (tcode, ctype,
7279 : : fold_build2 (code, ctype,
7280 : : fold_convert (ctype, op0),
7281 : : fold_convert (ctype, c)),
7282 : : op1);
7283 : :
7284 : : break;
7285 : :
7286 : 2122636 : case MULT_EXPR:
7287 : : /* We have a special case here if we are doing something like
7288 : : (C * 8) % 4 since we know that's zero. */
7289 : 2122636 : if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
7290 : 2122636 : || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
7291 : : /* If the multiplication can overflow we cannot optimize this. */
7292 : 10242 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
7293 : 346 : && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
7294 : 2132878 : && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
7295 : 301 : TYPE_SIGN (type)))
7296 : : {
7297 : 8 : *strict_overflow_p = true;
7298 : 8 : return omit_one_operand (type, integer_zero_node, op0);
7299 : : }
7300 : :
7301 : : /* ... fall through ... */
7302 : :
7303 : 2334985 : case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
7304 : 2334985 : case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
7305 : : /* If we can extract our operation from the LHS, do so and return a
7306 : : new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7307 : : do something only if the second operand is a constant. */
7308 : 2334985 : if (same_p
7309 : 1979232 : && TYPE_OVERFLOW_WRAPS (ctype)
7310 : 4116284 : && (t1 = extract_muldiv (op0, c, code, wide_type,
7311 : : strict_overflow_p)) != 0)
7312 : 46369 : return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
7313 : : fold_convert (ctype, op1));
7314 : 2288616 : else if (tcode == MULT_EXPR && code == MULT_EXPR
7315 : 1930828 : && TYPE_OVERFLOW_WRAPS (ctype)
7316 : 4021559 : && (t1 = extract_muldiv (op1, c, code, wide_type,
7317 : : strict_overflow_p)) != 0)
7318 : 887618 : return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
7319 : : fold_convert (ctype, t1));
7320 : 1400998 : else if (TREE_CODE (op1) != INTEGER_CST)
7321 : : return 0;
7322 : :
7323 : : /* If these are the same operation types, we can associate them
7324 : : assuming no overflow. */
7325 : 565691 : if (tcode == code)
7326 : : {
7327 : |