1 | /* Fold a constant sub-tree into a single node for C-compiler |
2 | Copyright (C) 1987-2023 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 | minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp3) |
158 | { |
159 | enum tree_code code = ERROR_MARK; |
160 | |
161 | if (HONOR_NANS (exp0) || HONOR_SIGNED_ZEROS (exp0)) |
162 | return ERROR_MARK; |
163 | |
164 | if (!operand_equal_p (exp0, exp2)) |
165 | return ERROR_MARK; |
166 | |
167 | if (TREE_CODE (exp3) == INTEGER_CST && TREE_CODE (exp1) == INTEGER_CST) |
168 | { |
169 | if (wi::to_widest (t: exp1) == (wi::to_widest (t: exp3) - 1)) |
170 | { |
171 | /* X <= Y - 1 equals to X < Y. */ |
172 | if (cmp == LE_EXPR) |
173 | code = LT_EXPR; |
174 | /* X > Y - 1 equals to X >= Y. */ |
175 | 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 | if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) |
179 | { |
180 | value_range r; |
181 | get_range_query (cfun)->range_of_expr (r, expr: exp0); |
182 | if (r.undefined_p ()) |
183 | r.set_varying (TREE_TYPE (exp0)); |
184 | |
185 | widest_int min = widest_int::from (x: r.lower_bound (), |
186 | TYPE_SIGN (TREE_TYPE (exp0))); |
187 | if (min == wi::to_widest (t: exp1)) |
188 | code = MAX_EXPR; |
189 | } |
190 | } |
191 | if (wi::to_widest (t: exp1) == (wi::to_widest (t: exp3) + 1)) |
192 | { |
193 | /* X < Y + 1 equals to X <= Y. */ |
194 | if (cmp == LT_EXPR) |
195 | code = LE_EXPR; |
196 | /* X >= Y + 1 equals to X > Y. */ |
197 | 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 | if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) |
201 | { |
202 | value_range r; |
203 | get_range_query (cfun)->range_of_expr (r, expr: exp0); |
204 | if (r.undefined_p ()) |
205 | r.set_varying (TREE_TYPE (exp0)); |
206 | |
207 | widest_int max = widest_int::from (x: r.upper_bound (), |
208 | TYPE_SIGN (TREE_TYPE (exp0))); |
209 | if (max == wi::to_widest (t: exp1)) |
210 | code = MIN_EXPR; |
211 | } |
212 | } |
213 | } |
214 | if (code != ERROR_MARK |
215 | || operand_equal_p (exp1, exp3)) |
216 | { |
217 | if (cmp == LT_EXPR || cmp == LE_EXPR) |
218 | code = MIN_EXPR; |
219 | if (cmp == GT_EXPR || cmp == GE_EXPR) |
220 | 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 | expr_location_or (tree t, location_t loc) |
230 | { |
231 | location_t tloc = EXPR_LOCATION (t); |
232 | 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 | protected_set_expr_location_unshare (tree x, location_t loc) |
240 | { |
241 | if (CAN_HAVE_LOCATION_P (x) |
242 | && EXPR_LOCATION (x) != loc |
243 | && !(TREE_CODE (x) == SAVE_EXPR |
244 | || TREE_CODE (x) == TARGET_EXPR |
245 | || TREE_CODE (x) == BIND_EXPR)) |
246 | { |
247 | x = copy_node (x); |
248 | SET_EXPR_LOCATION (x, loc); |
249 | } |
250 | 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 | div_if_zero_remainder (const_tree arg1, const_tree arg2) |
259 | { |
260 | widest_int quo; |
261 | |
262 | if (wi::multiple_of_p (x: wi::to_widest (t: arg1), y: wi::to_widest (t: arg2), |
263 | sgn: SIGNED, res: &quo)) |
264 | return wide_int_to_tree (TREE_TYPE (arg1), cst: quo); |
265 | |
266 | return NULL_TREE; |
267 | } |
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 | fold_defer_overflow_warnings (void) |
297 | { |
298 | ++fold_deferring_overflow_warnings; |
299 | } |
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 | fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code) |
312 | { |
313 | const char *warnmsg; |
314 | location_t locus; |
315 | |
316 | gcc_assert (fold_deferring_overflow_warnings > 0); |
317 | --fold_deferring_overflow_warnings; |
318 | if (fold_deferring_overflow_warnings > 0) |
319 | { |
320 | if (fold_deferred_overflow_warning != NULL |
321 | && code != 0 |
322 | && code < (int) fold_deferred_overflow_code) |
323 | fold_deferred_overflow_code = (enum warn_strict_overflow_code) code; |
324 | return; |
325 | } |
326 | |
327 | warnmsg = fold_deferred_overflow_warning; |
328 | fold_deferred_overflow_warning = NULL; |
329 | |
330 | if (!issue || warnmsg == NULL) |
331 | return; |
332 | |
333 | 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 | if (code == 0 || code > (int) fold_deferred_overflow_code) |
339 | code = fold_deferred_overflow_code; |
340 | |
341 | if (!issue_strict_overflow_warning (code)) |
342 | return; |
343 | |
344 | if (stmt == NULL) |
345 | locus = input_location; |
346 | else |
347 | locus = gimple_location (g: stmt); |
348 | warning_at (locus, OPT_Wstrict_overflow, "%s" , warnmsg); |
349 | } |
350 | |
351 | /* Stop deferring overflow warnings, ignoring any deferred |
352 | warnings. */ |
353 | |
354 | void |
355 | fold_undefer_and_ignore_overflow_warnings (void) |
356 | { |
357 | fold_undefer_overflow_warnings (issue: false, NULL, code: 0); |
358 | } |
359 | |
360 | /* Whether we are deferring overflow warnings. */ |
361 | |
362 | bool |
363 | fold_deferring_overflow_warnings_p (void) |
364 | { |
365 | 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 | fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc) |
373 | { |
374 | if (fold_deferring_overflow_warnings > 0) |
375 | { |
376 | if (fold_deferred_overflow_warning == NULL |
377 | || wc < fold_deferred_overflow_code) |
378 | { |
379 | fold_deferred_overflow_warning = gmsgid; |
380 | fold_deferred_overflow_code = wc; |
381 | } |
382 | } |
383 | else if (issue_strict_overflow_warning (wc)) |
384 | warning (OPT_Wstrict_overflow, gmsgid); |
385 | } |
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 | negate_mathfn_p (combined_fn fn) |
392 | { |
393 | 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 | CASE_CFN_LLRINT: |
445 | CASE_CFN_LLRINT_FN: |
446 | CASE_CFN_LRINT: |
447 | CASE_CFN_LRINT_FN: |
448 | CASE_CFN_NEARBYINT: |
449 | CASE_CFN_NEARBYINT_FN: |
450 | CASE_CFN_RINT: |
451 | CASE_CFN_RINT_FN: |
452 | return !flag_rounding_math; |
453 | |
454 | default: |
455 | break; |
456 | } |
457 | return false; |
458 | } |
459 | |
460 | /* Check whether we may negate an integer constant T without causing |
461 | overflow. */ |
462 | |
463 | bool |
464 | may_negate_without_overflow_p (const_tree t) |
465 | { |
466 | tree type; |
467 | |
468 | gcc_assert (TREE_CODE (t) == INTEGER_CST); |
469 | |
470 | type = TREE_TYPE (t); |
471 | if (TYPE_UNSIGNED (type)) |
472 | return false; |
473 | |
474 | 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 | negate_expr_p (tree t) |
482 | { |
483 | tree type; |
484 | |
485 | if (t == 0) |
486 | return false; |
487 | |
488 | type = TREE_TYPE (t); |
489 | |
490 | STRIP_SIGN_NOPS (t); |
491 | switch (TREE_CODE (t)) |
492 | { |
493 | case INTEGER_CST: |
494 | if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type)) |
495 | return true; |
496 | |
497 | /* Check that -CST will not overflow type. */ |
498 | return may_negate_without_overflow_p (t); |
499 | case BIT_NOT_EXPR: |
500 | return (INTEGRAL_TYPE_P (type) |
501 | && TYPE_OVERFLOW_WRAPS (type)); |
502 | |
503 | case FIXED_CST: |
504 | return true; |
505 | |
506 | case NEGATE_EXPR: |
507 | return !TYPE_OVERFLOW_SANITIZED (type); |
508 | |
509 | case REAL_CST: |
510 | /* We want to canonicalize to positive real constants. Pretend |
511 | that only negative ones can be easily negated. */ |
512 | return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); |
513 | |
514 | case COMPLEX_CST: |
515 | return negate_expr_p (TREE_REALPART (t)) |
516 | && negate_expr_p (TREE_IMAGPART (t)); |
517 | |
518 | case VECTOR_CST: |
519 | { |
520 | if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type)) |
521 | return true; |
522 | |
523 | /* Steps don't prevent negation. */ |
524 | unsigned int count = vector_cst_encoded_nelts (t); |
525 | for (unsigned int i = 0; i < count; ++i) |
526 | if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i))) |
527 | return false; |
528 | |
529 | return true; |
530 | } |
531 | |
532 | case COMPLEX_EXPR: |
533 | return negate_expr_p (TREE_OPERAND (t, 0)) |
534 | && negate_expr_p (TREE_OPERAND (t, 1)); |
535 | |
536 | case CONJ_EXPR: |
537 | return negate_expr_p (TREE_OPERAND (t, 0)); |
538 | |
539 | case PLUS_EXPR: |
540 | if (HONOR_SIGN_DEPENDENT_ROUNDING (type) |
541 | || HONOR_SIGNED_ZEROS (type) |
542 | || (ANY_INTEGRAL_TYPE_P (type) |
543 | && ! TYPE_OVERFLOW_WRAPS (type))) |
544 | return false; |
545 | /* -(A + B) -> (-B) - A. */ |
546 | if (negate_expr_p (TREE_OPERAND (t, 1))) |
547 | return true; |
548 | /* -(A + B) -> (-A) - B. */ |
549 | return negate_expr_p (TREE_OPERAND (t, 0)); |
550 | |
551 | case MINUS_EXPR: |
552 | /* We can't turn -(A-B) into B-A when we honor signed zeros. */ |
553 | return !HONOR_SIGN_DEPENDENT_ROUNDING (type) |
554 | && !HONOR_SIGNED_ZEROS (type) |
555 | && (! ANY_INTEGRAL_TYPE_P (type) |
556 | || TYPE_OVERFLOW_WRAPS (type)); |
557 | |
558 | case MULT_EXPR: |
559 | 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 | if (INTEGRAL_TYPE_P (TREE_TYPE (t)) |
564 | && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) |
565 | && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST |
566 | && (wi::popcount |
567 | (wi::abs (x: wi::to_wide (TREE_OPERAND (t, 0))))) != 1) |
568 | || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
569 | && (wi::popcount |
570 | (wi::abs (x: wi::to_wide (TREE_OPERAND (t, 1))))) != 1))) |
571 | break; |
572 | |
573 | /* Fall through. */ |
574 | |
575 | case RDIV_EXPR: |
576 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (t)) |
577 | return negate_expr_p (TREE_OPERAND (t, 1)) |
578 | || negate_expr_p (TREE_OPERAND (t, 0)); |
579 | break; |
580 | |
581 | case TRUNC_DIV_EXPR: |
582 | case ROUND_DIV_EXPR: |
583 | case EXACT_DIV_EXPR: |
584 | 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 | if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST |
589 | && 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 | if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t)) |
595 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) |
596 | || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
597 | && ! integer_onep (TREE_OPERAND (t, 1)))) |
598 | return negate_expr_p (TREE_OPERAND (t, 1)); |
599 | break; |
600 | |
601 | case NOP_EXPR: |
602 | /* Negate -((double)float) as (double)(-float). */ |
603 | if (SCALAR_FLOAT_TYPE_P (type)) |
604 | { |
605 | tree tem = strip_float_extensions (t); |
606 | if (tem != t) |
607 | return negate_expr_p (t: tem); |
608 | } |
609 | break; |
610 | |
611 | case CALL_EXPR: |
612 | /* Negate -f(x) as f(-x). */ |
613 | if (negate_mathfn_p (fn: get_call_combined_fn (t))) |
614 | return negate_expr_p (CALL_EXPR_ARG (t, 0)); |
615 | break; |
616 | |
617 | case RSHIFT_EXPR: |
618 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */ |
619 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) |
620 | { |
621 | tree op1 = TREE_OPERAND (t, 1); |
622 | if (wi::to_wide (t: 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 | fold_negate_expr_1 (location_t loc, tree t) |
640 | { |
641 | tree type = TREE_TYPE (t); |
642 | tree tem; |
643 | |
644 | switch (TREE_CODE (t)) |
645 | { |
646 | /* Convert - (~A) to A + 1. */ |
647 | case BIT_NOT_EXPR: |
648 | if (INTEGRAL_TYPE_P (type)) |
649 | return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0), |
650 | build_one_cst (type)); |
651 | break; |
652 | |
653 | case INTEGER_CST: |
654 | tem = fold_negate_const (t, type); |
655 | if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t) |
656 | || (ANY_INTEGRAL_TYPE_P (type) |
657 | && !TYPE_OVERFLOW_TRAPS (type) |
658 | && TYPE_OVERFLOW_WRAPS (type)) |
659 | || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0) |
660 | return tem; |
661 | break; |
662 | |
663 | case POLY_INT_CST: |
664 | case REAL_CST: |
665 | case FIXED_CST: |
666 | tem = fold_negate_const (t, type); |
667 | return tem; |
668 | |
669 | case COMPLEX_CST: |
670 | { |
671 | tree rpart = fold_negate_expr (loc, TREE_REALPART (t)); |
672 | tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t)); |
673 | if (rpart && ipart) |
674 | return build_complex (type, rpart, ipart); |
675 | } |
676 | break; |
677 | |
678 | case VECTOR_CST: |
679 | { |
680 | tree_vector_builder elts; |
681 | elts.new_unary_operation (shape: type, vec: t, allow_stepped_p: true); |
682 | unsigned int count = elts.encoded_nelts (); |
683 | for (unsigned int i = 0; i < count; ++i) |
684 | { |
685 | tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i)); |
686 | if (elt == NULL_TREE) |
687 | return NULL_TREE; |
688 | elts.quick_push (obj: elt); |
689 | } |
690 | |
691 | return elts.build (); |
692 | } |
693 | |
694 | case COMPLEX_EXPR: |
695 | if (negate_expr_p (t)) |
696 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
697 | fold_negate_expr (loc, TREE_OPERAND (t, 0)), |
698 | fold_negate_expr (loc, TREE_OPERAND (t, 1))); |
699 | break; |
700 | |
701 | case CONJ_EXPR: |
702 | if (negate_expr_p (t)) |
703 | return fold_build1_loc (loc, CONJ_EXPR, type, |
704 | fold_negate_expr (loc, TREE_OPERAND (t, 0))); |
705 | break; |
706 | |
707 | case NEGATE_EXPR: |
708 | if (!TYPE_OVERFLOW_SANITIZED (type)) |
709 | return TREE_OPERAND (t, 0); |
710 | break; |
711 | |
712 | case PLUS_EXPR: |
713 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) |
714 | && !HONOR_SIGNED_ZEROS (type)) |
715 | { |
716 | /* -(A + B) -> (-B) - A. */ |
717 | if (negate_expr_p (TREE_OPERAND (t, 1))) |
718 | { |
719 | tem = negate_expr (TREE_OPERAND (t, 1)); |
720 | return fold_build2_loc (loc, MINUS_EXPR, type, |
721 | tem, TREE_OPERAND (t, 0)); |
722 | } |
723 | |
724 | /* -(A + B) -> (-A) - B. */ |
725 | if (negate_expr_p (TREE_OPERAND (t, 0))) |
726 | { |
727 | tem = negate_expr (TREE_OPERAND (t, 0)); |
728 | return fold_build2_loc (loc, MINUS_EXPR, type, |
729 | tem, TREE_OPERAND (t, 1)); |
730 | } |
731 | } |
732 | break; |
733 | |
734 | case MINUS_EXPR: |
735 | /* - (A - B) -> B - A */ |
736 | if (!HONOR_SIGN_DEPENDENT_ROUNDING (type) |
737 | && !HONOR_SIGNED_ZEROS (type)) |
738 | return fold_build2_loc (loc, MINUS_EXPR, type, |
739 | TREE_OPERAND (t, 1), TREE_OPERAND (t, 0)); |
740 | break; |
741 | |
742 | case MULT_EXPR: |
743 | if (TYPE_UNSIGNED (type)) |
744 | break; |
745 | |
746 | /* Fall through. */ |
747 | |
748 | case RDIV_EXPR: |
749 | if (! HONOR_SIGN_DEPENDENT_ROUNDING (type)) |
750 | { |
751 | tem = TREE_OPERAND (t, 1); |
752 | if (negate_expr_p (t: tem)) |
753 | return fold_build2_loc (loc, TREE_CODE (t), type, |
754 | TREE_OPERAND (t, 0), negate_expr (tem)); |
755 | tem = TREE_OPERAND (t, 0); |
756 | if (negate_expr_p (t: tem)) |
757 | return fold_build2_loc (loc, TREE_CODE (t), type, |
758 | negate_expr (tem), TREE_OPERAND (t, 1)); |
759 | } |
760 | break; |
761 | |
762 | case TRUNC_DIV_EXPR: |
763 | case ROUND_DIV_EXPR: |
764 | case EXACT_DIV_EXPR: |
765 | 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 | if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST |
770 | && negate_expr_p (TREE_OPERAND (t, 0))) |
771 | return fold_build2_loc (loc, TREE_CODE (t), type, |
772 | negate_expr (TREE_OPERAND (t, 0)), |
773 | 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 | if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t)) |
778 | || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t)) |
779 | || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
780 | && ! integer_onep (TREE_OPERAND (t, 1)))) |
781 | && negate_expr_p (TREE_OPERAND (t, 1))) |
782 | return fold_build2_loc (loc, TREE_CODE (t), type, |
783 | TREE_OPERAND (t, 0), |
784 | negate_expr (TREE_OPERAND (t, 1))); |
785 | break; |
786 | |
787 | case NOP_EXPR: |
788 | /* Convert -((double)float) into (double)(-float). */ |
789 | if (SCALAR_FLOAT_TYPE_P (type)) |
790 | { |
791 | tem = strip_float_extensions (t); |
792 | if (tem != t && negate_expr_p (t: tem)) |
793 | return fold_convert_loc (loc, type, negate_expr (tem)); |
794 | } |
795 | break; |
796 | |
797 | case CALL_EXPR: |
798 | /* Negate -f(x) as f(-x). */ |
799 | if (negate_mathfn_p (fn: get_call_combined_fn (t)) |
800 | && negate_expr_p (CALL_EXPR_ARG (t, 0))) |
801 | { |
802 | tree fndecl, arg; |
803 | |
804 | fndecl = get_callee_fndecl (t); |
805 | arg = negate_expr (CALL_EXPR_ARG (t, 0)); |
806 | return build_call_expr_loc (loc, fndecl, 1, arg); |
807 | } |
808 | break; |
809 | |
810 | case RSHIFT_EXPR: |
811 | /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */ |
812 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) |
813 | { |
814 | tree op1 = TREE_OPERAND (t, 1); |
815 | if (wi::to_wide (t: op1) == element_precision (type) - 1) |
816 | { |
817 | tree ntype = TYPE_UNSIGNED (type) |
818 | ? signed_type_for (type) |
819 | : unsigned_type_for (type); |
820 | tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0)); |
821 | temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1); |
822 | 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 | fold_negate_expr (location_t loc, tree t) |
838 | { |
839 | tree type = TREE_TYPE (t); |
840 | STRIP_SIGN_NOPS (t); |
841 | tree tem = fold_negate_expr_1 (loc, t); |
842 | if (tem == NULL_TREE) |
843 | return NULL_TREE; |
844 | 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 | negate_expr (tree t) |
853 | { |
854 | tree type, tem; |
855 | location_t loc; |
856 | |
857 | if (t == NULL_TREE) |
858 | return NULL_TREE; |
859 | |
860 | loc = EXPR_LOCATION (t); |
861 | type = TREE_TYPE (t); |
862 | STRIP_SIGN_NOPS (t); |
863 | |
864 | tem = fold_negate_expr (loc, t); |
865 | if (!tem) |
866 | tem = build1_loc (loc, code: NEGATE_EXPR, TREE_TYPE (t), arg1: t); |
867 | 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 | 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 | tree var = 0; |
899 | *minus_varp = 0; |
900 | *conp = 0; |
901 | *minus_conp = 0; |
902 | *litp = 0; |
903 | *minus_litp = 0; |
904 | |
905 | /* Strip any conversions that don't change the machine mode or signedness. */ |
906 | STRIP_SIGN_NOPS (in); |
907 | |
908 | if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST |
909 | || TREE_CODE (in) == FIXED_CST) |
910 | *litp = in; |
911 | else if (TREE_CODE (in) == code |
912 | || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math) |
913 | && ! 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 | && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR) |
919 | || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) |
920 | || (code == MINUS_EXPR |
921 | && (TREE_CODE (in) == PLUS_EXPR |
922 | || TREE_CODE (in) == POINTER_PLUS_EXPR))))) |
923 | { |
924 | tree op0 = TREE_OPERAND (in, 0); |
925 | tree op1 = TREE_OPERAND (in, 1); |
926 | bool neg1_p = TREE_CODE (in) == MINUS_EXPR; |
927 | 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 | if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST |
931 | || TREE_CODE (op0) == FIXED_CST) |
932 | *litp = op0, op0 = 0; |
933 | else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST |
934 | || TREE_CODE (op1) == FIXED_CST) |
935 | *litp = op1, neg_litp_p = neg1_p, op1 = 0; |
936 | |
937 | if (op0 != 0 && TREE_CONSTANT (op0)) |
938 | *conp = op0, op0 = 0; |
939 | else if (op1 != 0 && TREE_CONSTANT (op1)) |
940 | *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 | if (op0 != 0 && op1 != 0) |
945 | var = in; |
946 | else if (op0 != 0) |
947 | var = op0; |
948 | else |
949 | var = op1, neg_var_p = neg1_p; |
950 | |
951 | /* Now do any needed negations. */ |
952 | if (neg_litp_p) |
953 | *minus_litp = *litp, *litp = 0; |
954 | if (neg_conp_p && *conp) |
955 | *minus_conp = *conp, *conp = 0; |
956 | if (neg_var_p && var) |
957 | *minus_varp = var, var = 0; |
958 | } |
959 | else if (TREE_CONSTANT (in)) |
960 | *conp = in; |
961 | else if (TREE_CODE (in) == BIT_NOT_EXPR |
962 | && code == PLUS_EXPR) |
963 | { |
964 | /* -1 - X is folded to ~X, undo that here. Do _not_ do this |
965 | when IN is constant. */ |
966 | *litp = build_minus_one_cst (type); |
967 | *minus_varp = TREE_OPERAND (in, 0); |
968 | } |
969 | else |
970 | var = in; |
971 | |
972 | if (negate_p) |
973 | { |
974 | if (*litp) |
975 | *minus_litp = *litp, *litp = 0; |
976 | else if (*minus_litp) |
977 | *litp = *minus_litp, *minus_litp = 0; |
978 | if (*conp) |
979 | *minus_conp = *conp, *conp = 0; |
980 | else if (*minus_conp) |
981 | *conp = *minus_conp, *minus_conp = 0; |
982 | if (var) |
983 | *minus_varp = var, var = 0; |
984 | else if (*minus_varp) |
985 | var = *minus_varp, *minus_varp = 0; |
986 | } |
987 | |
988 | if (*litp |
989 | && TREE_OVERFLOW_P (*litp)) |
990 | *litp = drop_tree_overflow (*litp); |
991 | if (*minus_litp |
992 | && TREE_OVERFLOW_P (*minus_litp)) |
993 | *minus_litp = drop_tree_overflow (*minus_litp); |
994 | |
995 | 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 | associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type) |
1005 | { |
1006 | if (t1 == 0) |
1007 | { |
1008 | gcc_assert (t2 == 0 || code != MINUS_EXPR); |
1009 | return t2; |
1010 | } |
1011 | 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 | if (TREE_CODE (t1) == code || TREE_CODE (t2) == code |
1018 | || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR |
1019 | || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR) |
1020 | { |
1021 | if (code == PLUS_EXPR) |
1022 | { |
1023 | if (TREE_CODE (t1) == NEGATE_EXPR) |
1024 | return build2_loc (loc, code: MINUS_EXPR, type, |
1025 | arg0: fold_convert_loc (loc, type, t2), |
1026 | arg1: fold_convert_loc (loc, type, |
1027 | TREE_OPERAND (t1, 0))); |
1028 | else if (TREE_CODE (t2) == NEGATE_EXPR) |
1029 | return build2_loc (loc, code: MINUS_EXPR, type, |
1030 | arg0: fold_convert_loc (loc, type, t1), |
1031 | arg1: fold_convert_loc (loc, type, |
1032 | TREE_OPERAND (t2, 0))); |
1033 | else if (integer_zerop (t2)) |
1034 | return fold_convert_loc (loc, type, t1); |
1035 | } |
1036 | else if (code == MINUS_EXPR) |
1037 | { |
1038 | if (integer_zerop (t2)) |
1039 | return fold_convert_loc (loc, type, t1); |
1040 | } |
1041 | |
1042 | return build2_loc (loc, code, type, arg0: fold_convert_loc (loc, type, t1), |
1043 | arg1: fold_convert_loc (loc, type, t2)); |
1044 | } |
1045 | |
1046 | return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1), |
1047 | 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 | int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2) |
1055 | { |
1056 | if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1)) |
1057 | return false; |
1058 | if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2)) |
1059 | return false; |
1060 | |
1061 | switch (code) |
1062 | { |
1063 | case LSHIFT_EXPR: |
1064 | case RSHIFT_EXPR: |
1065 | case LROTATE_EXPR: |
1066 | case RROTATE_EXPR: |
1067 | return true; |
1068 | |
1069 | default: |
1070 | break; |
1071 | } |
1072 | |
1073 | return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2) |
1074 | && TYPE_PRECISION (type1) == TYPE_PRECISION (type2) |
1075 | && 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 | 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 | wide_int tmp; |
1088 | *overflow = wi::OVF_NONE; |
1089 | switch (code) |
1090 | { |
1091 | case BIT_IOR_EXPR: |
1092 | res = wi::bit_or (x: arg1, y: arg2); |
1093 | break; |
1094 | |
1095 | case BIT_XOR_EXPR: |
1096 | res = wi::bit_xor (x: arg1, y: arg2); |
1097 | break; |
1098 | |
1099 | case BIT_AND_EXPR: |
1100 | res = wi::bit_and (x: arg1, y: arg2); |
1101 | break; |
1102 | |
1103 | case LSHIFT_EXPR: |
1104 | if (wi::neg_p (x: arg2)) |
1105 | return false; |
1106 | res = wi::lshift (x: arg1, y: arg2); |
1107 | break; |
1108 | |
1109 | case RSHIFT_EXPR: |
1110 | if (wi::neg_p (x: 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 | res = wi::rshift (x: arg1, y: arg2, sgn: sign); |
1116 | break; |
1117 | |
1118 | case RROTATE_EXPR: |
1119 | case LROTATE_EXPR: |
1120 | if (wi::neg_p (x: arg2)) |
1121 | { |
1122 | tmp = -arg2; |
1123 | if (code == RROTATE_EXPR) |
1124 | code = LROTATE_EXPR; |
1125 | else |
1126 | code = RROTATE_EXPR; |
1127 | } |
1128 | else |
1129 | tmp = arg2; |
1130 | |
1131 | if (code == RROTATE_EXPR) |
1132 | res = wi::rrotate (x: arg1, y: tmp); |
1133 | else |
1134 | res = wi::lrotate (x: arg1, y: tmp); |
1135 | break; |
1136 | |
1137 | case PLUS_EXPR: |
1138 | res = wi::add (x: arg1, y: arg2, sgn: sign, overflow); |
1139 | break; |
1140 | |
1141 | case MINUS_EXPR: |
1142 | res = wi::sub (x: arg1, y: arg2, sgn: sign, overflow); |
1143 | break; |
1144 | |
1145 | case MULT_EXPR: |
1146 | res = wi::mul (x: arg1, y: arg2, sgn: sign, overflow); |
1147 | break; |
1148 | |
1149 | case MULT_HIGHPART_EXPR: |
1150 | res = wi::mul_high (x: arg1, y: arg2, sgn: sign); |
1151 | break; |
1152 | |
1153 | case TRUNC_DIV_EXPR: |
1154 | case EXACT_DIV_EXPR: |
1155 | if (arg2 == 0) |
1156 | return false; |
1157 | res = wi::div_trunc (x: arg1, y: arg2, sgn: sign, overflow); |
1158 | break; |
1159 | |
1160 | case FLOOR_DIV_EXPR: |
1161 | if (arg2 == 0) |
1162 | return false; |
1163 | res = wi::div_floor (x: arg1, y: arg2, sgn: sign, overflow); |
1164 | break; |
1165 | |
1166 | case CEIL_DIV_EXPR: |
1167 | if (arg2 == 0) |
1168 | return false; |
1169 | res = wi::div_ceil (x: arg1, y: arg2, sgn: sign, overflow); |
1170 | break; |
1171 | |
1172 | case ROUND_DIV_EXPR: |
1173 | if (arg2 == 0) |
1174 | return false; |
1175 | res = wi::div_round (x: arg1, y: arg2, sgn: sign, overflow); |
1176 | break; |
1177 | |
1178 | case TRUNC_MOD_EXPR: |
1179 | if (arg2 == 0) |
1180 | return false; |
1181 | res = wi::mod_trunc (x: arg1, y: arg2, sgn: sign, overflow); |
1182 | break; |
1183 | |
1184 | case FLOOR_MOD_EXPR: |
1185 | if (arg2 == 0) |
1186 | return false; |
1187 | res = wi::mod_floor (x: arg1, y: arg2, sgn: sign, overflow); |
1188 | break; |
1189 | |
1190 | case CEIL_MOD_EXPR: |
1191 | if (arg2 == 0) |
1192 | return false; |
1193 | res = wi::mod_ceil (x: arg1, y: arg2, sgn: sign, overflow); |
1194 | break; |
1195 | |
1196 | case ROUND_MOD_EXPR: |
1197 | if (arg2 == 0) |
1198 | return false; |
1199 | res = wi::mod_round (x: arg1, y: arg2, sgn: sign, overflow); |
1200 | break; |
1201 | |
1202 | case MIN_EXPR: |
1203 | res = wi::min (x: arg1, y: arg2, sgn: sign); |
1204 | break; |
1205 | |
1206 | case MAX_EXPR: |
1207 | res = wi::max (x: arg1, y: arg2, sgn: sign); |
1208 | break; |
1209 | |
1210 | default: |
1211 | return false; |
1212 | } |
1213 | return true; |
1214 | } |
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 | can_min_p (const_tree arg1, const_tree arg2, poly_wide_int &res) |
1220 | { |
1221 | if (known_le (wi::to_poly_widest (arg1), wi::to_poly_widest (arg2))) |
1222 | { |
1223 | res = wi::to_poly_wide (t: arg1); |
1224 | return true; |
1225 | } |
1226 | else if (known_le (wi::to_poly_widest (arg2), wi::to_poly_widest (arg1))) |
1227 | { |
1228 | res = wi::to_poly_wide (t: arg2); |
1229 | 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 | static bool |
1240 | 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 | gcc_assert (NUM_POLY_INT_COEFFS != 1); |
1245 | gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2)); |
1246 | switch (code) |
1247 | { |
1248 | case PLUS_EXPR: |
1249 | res = wi::add (a: wi::to_poly_wide (t: arg1), |
1250 | b: wi::to_poly_wide (t: arg2), sgn: sign, overflow); |
1251 | break; |
1252 | |
1253 | case MINUS_EXPR: |
1254 | res = wi::sub (a: wi::to_poly_wide (t: arg1), |
1255 | b: wi::to_poly_wide (t: arg2), sgn: sign, overflow); |
1256 | break; |
1257 | |
1258 | case MULT_EXPR: |
1259 | if (TREE_CODE (arg2) == INTEGER_CST) |
1260 | res = wi::mul (a: wi::to_poly_wide (t: arg1), |
1261 | b: wi::to_wide (t: arg2), sgn: sign, overflow); |
1262 | else if (TREE_CODE (arg1) == INTEGER_CST) |
1263 | res = wi::mul (a: wi::to_poly_wide (t: arg2), |
1264 | b: wi::to_wide (t: arg1), sgn: sign, overflow); |
1265 | else |
1266 | return NULL_TREE; |
1267 | break; |
1268 | |
1269 | case LSHIFT_EXPR: |
1270 | if (TREE_CODE (arg2) == INTEGER_CST) |
1271 | res = wi::to_poly_wide (t: arg1) << wi::to_wide (t: arg2); |
1272 | else |
1273 | return false; |
1274 | break; |
1275 | |
1276 | case BIT_IOR_EXPR: |
1277 | if (TREE_CODE (arg2) != INTEGER_CST |
1278 | || !can_ior_p (a: wi::to_poly_wide (t: arg1), b: wi::to_wide (t: arg2), |
1279 | result: &res)) |
1280 | return false; |
1281 | break; |
1282 | |
1283 | case MIN_EXPR: |
1284 | if (!can_min_p (arg1, arg2, res)) |
1285 | return false; |
1286 | break; |
1287 | |
1288 | default: |
1289 | return false; |
1290 | } |
1291 | return true; |
1292 | } |
1293 | |
1294 | /* Combine two integer constants ARG1 and ARG2 under operation CODE to |
1295 | produce a new constant. Return NULL_TREE if we don't know how to |
1296 | evaluate CODE at compile-time. */ |
1297 | |
1298 | tree |
1299 | int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, |
1300 | int overflowable) |
1301 | { |
1302 | poly_wide_int poly_res; |
1303 | tree type = TREE_TYPE (arg1); |
1304 | signop sign = TYPE_SIGN (type); |
1305 | wi::overflow_type overflow = wi::OVF_NONE; |
1306 | |
1307 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST) |
1308 | { |
1309 | wide_int warg1 = wi::to_wide (t: arg1), res; |
1310 | wide_int warg2 = wi::to_wide (t: arg2, TYPE_PRECISION (type)); |
1311 | if (!wide_int_binop (res, code, arg1: warg1, arg2: warg2, sign, overflow: &overflow)) |
1312 | return NULL_TREE; |
1313 | poly_res = res; |
1314 | } |
1315 | else if (!poly_int_tree_p (t: arg1) |
1316 | || !poly_int_tree_p (t: arg2) |
1317 | || !poly_int_binop (res&: poly_res, code, arg1, arg2, sign, overflow: &overflow)) |
1318 | return NULL_TREE; |
1319 | return force_fit_type (type, poly_res, overflowable, |
1320 | (((sign == SIGNED || overflowable == -1) |
1321 | && overflow) |
1322 | | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))); |
1323 | } |
1324 | |
1325 | /* Return true if binary operation OP distributes over addition in operand |
1326 | OPNO, with the other operand being held constant. OPNO counts from 1. */ |
1327 | |
1328 | static bool |
1329 | distributes_over_addition_p (tree_code op, int opno) |
1330 | { |
1331 | switch (op) |
1332 | { |
1333 | case PLUS_EXPR: |
1334 | case MINUS_EXPR: |
1335 | case MULT_EXPR: |
1336 | return true; |
1337 | |
1338 | case LSHIFT_EXPR: |
1339 | return opno == 1; |
1340 | |
1341 | default: |
1342 | return false; |
1343 | } |
1344 | } |
1345 | |
1346 | /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new |
1347 | constant. We assume ARG1 and ARG2 have the same data type, or at least |
1348 | are the same kind of constant and the same machine mode. Return zero if |
1349 | combining the constants is not allowed in the current operating mode. */ |
1350 | |
1351 | static tree |
1352 | const_binop (enum tree_code code, tree arg1, tree arg2) |
1353 | { |
1354 | /* Sanity check for the recursive cases. */ |
1355 | if (!arg1 || !arg2) |
1356 | return NULL_TREE; |
1357 | |
1358 | STRIP_NOPS (arg1); |
1359 | STRIP_NOPS (arg2); |
1360 | |
1361 | if (poly_int_tree_p (t: arg1) && poly_int_tree_p (t: arg2)) |
1362 | { |
1363 | if (code == POINTER_PLUS_EXPR) |
1364 | return int_const_binop (code: PLUS_EXPR, |
1365 | arg1, fold_convert (TREE_TYPE (arg1), arg2)); |
1366 | |
1367 | return int_const_binop (code, arg1, arg2); |
1368 | } |
1369 | |
1370 | if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST) |
1371 | { |
1372 | machine_mode mode; |
1373 | REAL_VALUE_TYPE d1; |
1374 | REAL_VALUE_TYPE d2; |
1375 | REAL_VALUE_TYPE value; |
1376 | REAL_VALUE_TYPE result; |
1377 | bool inexact; |
1378 | tree t, type; |
1379 | |
1380 | /* The following codes are handled by real_arithmetic. */ |
1381 | switch (code) |
1382 | { |
1383 | case PLUS_EXPR: |
1384 | case MINUS_EXPR: |
1385 | case MULT_EXPR: |
1386 | case RDIV_EXPR: |
1387 | case MIN_EXPR: |
1388 | case MAX_EXPR: |
1389 | break; |
1390 | |
1391 | default: |
1392 | return NULL_TREE; |
1393 | } |
1394 | |
1395 | d1 = TREE_REAL_CST (arg1); |
1396 | d2 = TREE_REAL_CST (arg2); |
1397 | |
1398 | type = TREE_TYPE (arg1); |
1399 | mode = TYPE_MODE (type); |
1400 | |
1401 | /* Don't perform operation if we honor signaling NaNs and |
1402 | either operand is a signaling NaN. */ |
1403 | if (HONOR_SNANS (mode) |
1404 | && (REAL_VALUE_ISSIGNALING_NAN (d1) |
1405 | || REAL_VALUE_ISSIGNALING_NAN (d2))) |
1406 | return NULL_TREE; |
1407 | |
1408 | /* Don't perform operation if it would raise a division |
1409 | by zero exception. */ |
1410 | if (code == RDIV_EXPR |
1411 | && real_equal (&d2, &dconst0) |
1412 | && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode))) |
1413 | return NULL_TREE; |
1414 | |
1415 | /* If either operand is a NaN, just return it. Otherwise, set up |
1416 | for floating-point trap; we return an overflow. */ |
1417 | if (REAL_VALUE_ISNAN (d1)) |
1418 | { |
1419 | /* Make resulting NaN value to be qNaN when flag_signaling_nans |
1420 | is off. */ |
1421 | d1.signalling = 0; |
1422 | t = build_real (type, d1); |
1423 | return t; |
1424 | } |
1425 | else if (REAL_VALUE_ISNAN (d2)) |
1426 | { |
1427 | /* Make resulting NaN value to be qNaN when flag_signaling_nans |
1428 | is off. */ |
1429 | d2.signalling = 0; |
1430 | t = build_real (type, d2); |
1431 | return t; |
1432 | } |
1433 | |
1434 | inexact = real_arithmetic (&value, code, &d1, &d2); |
1435 | real_convert (&result, mode, &value); |
1436 | |
1437 | /* Don't constant fold this floating point operation if |
1438 | both operands are not NaN but the result is NaN, and |
1439 | flag_trapping_math. Such operations should raise an |
1440 | invalid operation exception. */ |
1441 | if (flag_trapping_math |
1442 | && MODE_HAS_NANS (mode) |
1443 | && REAL_VALUE_ISNAN (result) |
1444 | && !REAL_VALUE_ISNAN (d1) |
1445 | && !REAL_VALUE_ISNAN (d2)) |
1446 | return NULL_TREE; |
1447 | |
1448 | /* Don't constant fold this floating point operation if |
1449 | the result has overflowed and flag_trapping_math. */ |
1450 | if (flag_trapping_math |
1451 | && MODE_HAS_INFINITIES (mode) |
1452 | && REAL_VALUE_ISINF (result) |
1453 | && !REAL_VALUE_ISINF (d1) |
1454 | && !REAL_VALUE_ISINF (d2)) |
1455 | return NULL_TREE; |
1456 | |
1457 | /* Don't constant fold this floating point operation if the |
1458 | result may dependent upon the run-time rounding mode and |
1459 | flag_rounding_math is set, or if GCC's software emulation |
1460 | is unable to accurately represent the result. */ |
1461 | if ((flag_rounding_math |
1462 | || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations)) |
1463 | && (inexact || !real_identical (&result, &value))) |
1464 | return NULL_TREE; |
1465 | |
1466 | t = build_real (type, result); |
1467 | |
1468 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2); |
1469 | return t; |
1470 | } |
1471 | |
1472 | if (TREE_CODE (arg1) == FIXED_CST) |
1473 | { |
1474 | FIXED_VALUE_TYPE f1; |
1475 | FIXED_VALUE_TYPE f2; |
1476 | FIXED_VALUE_TYPE result; |
1477 | tree t, type; |
1478 | bool sat_p; |
1479 | bool overflow_p; |
1480 | |
1481 | /* The following codes are handled by fixed_arithmetic. */ |
1482 | switch (code) |
1483 | { |
1484 | case PLUS_EXPR: |
1485 | case MINUS_EXPR: |
1486 | case MULT_EXPR: |
1487 | case TRUNC_DIV_EXPR: |
1488 | if (TREE_CODE (arg2) != FIXED_CST) |
1489 | return NULL_TREE; |
1490 | f2 = TREE_FIXED_CST (arg2); |
1491 | break; |
1492 | |
1493 | case LSHIFT_EXPR: |
1494 | case RSHIFT_EXPR: |
1495 | { |
1496 | if (TREE_CODE (arg2) != INTEGER_CST) |
1497 | return NULL_TREE; |
1498 | wi::tree_to_wide_ref w2 = wi::to_wide (t: arg2); |
1499 | f2.data.high = w2.elt (i: 1); |
1500 | f2.data.low = w2.ulow (); |
1501 | f2.mode = SImode; |
1502 | } |
1503 | break; |
1504 | |
1505 | default: |
1506 | return NULL_TREE; |
1507 | } |
1508 | |
1509 | f1 = TREE_FIXED_CST (arg1); |
1510 | type = TREE_TYPE (arg1); |
1511 | sat_p = TYPE_SATURATING (type); |
1512 | overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p); |
1513 | t = build_fixed (type, result); |
1514 | /* Propagate overflow flags. */ |
1515 | if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)) |
1516 | TREE_OVERFLOW (t) = 1; |
1517 | return t; |
1518 | } |
1519 | |
1520 | if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST) |
1521 | { |
1522 | tree type = TREE_TYPE (arg1); |
1523 | tree r1 = TREE_REALPART (arg1); |
1524 | tree i1 = TREE_IMAGPART (arg1); |
1525 | tree r2 = TREE_REALPART (arg2); |
1526 | tree i2 = TREE_IMAGPART (arg2); |
1527 | tree real, imag; |
1528 | |
1529 | switch (code) |
1530 | { |
1531 | case PLUS_EXPR: |
1532 | case MINUS_EXPR: |
1533 | real = const_binop (code, arg1: r1, arg2: r2); |
1534 | imag = const_binop (code, arg1: i1, arg2: i2); |
1535 | break; |
1536 | |
1537 | case MULT_EXPR: |
1538 | if (COMPLEX_FLOAT_TYPE_P (type)) |
1539 | return do_mpc_arg2 (arg1, arg2, type, |
1540 | /* do_nonfinite= */ folding_initializer, |
1541 | mpc_mul); |
1542 | |
1543 | real = const_binop (code: MINUS_EXPR, |
1544 | arg1: const_binop (code: MULT_EXPR, arg1: r1, arg2: r2), |
1545 | arg2: const_binop (code: MULT_EXPR, arg1: i1, arg2: i2)); |
1546 | imag = const_binop (code: PLUS_EXPR, |
1547 | arg1: const_binop (code: MULT_EXPR, arg1: r1, arg2: i2), |
1548 | arg2: const_binop (code: MULT_EXPR, arg1: i1, arg2: r2)); |
1549 | break; |
1550 | |
1551 | case RDIV_EXPR: |
1552 | if (COMPLEX_FLOAT_TYPE_P (type)) |
1553 | return do_mpc_arg2 (arg1, arg2, type, |
1554 | /* do_nonfinite= */ folding_initializer, |
1555 | mpc_div); |
1556 | /* Fallthru. */ |
1557 | case TRUNC_DIV_EXPR: |
1558 | case CEIL_DIV_EXPR: |
1559 | case FLOOR_DIV_EXPR: |
1560 | case ROUND_DIV_EXPR: |
1561 | if (flag_complex_method == 0) |
1562 | { |
1563 | /* Keep this algorithm in sync with |
1564 | tree-complex.cc:expand_complex_div_straight(). |
1565 | |
1566 | Expand complex division to scalars, straightforward algorithm. |
1567 | a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t) |
1568 | t = br*br + bi*bi |
1569 | */ |
1570 | tree magsquared |
1571 | = const_binop (code: PLUS_EXPR, |
1572 | arg1: const_binop (code: MULT_EXPR, arg1: r2, arg2: r2), |
1573 | arg2: const_binop (code: MULT_EXPR, arg1: i2, arg2: i2)); |
1574 | tree t1 |
1575 | = const_binop (code: PLUS_EXPR, |
1576 | arg1: const_binop (code: MULT_EXPR, arg1: r1, arg2: r2), |
1577 | arg2: const_binop (code: MULT_EXPR, arg1: i1, arg2: i2)); |
1578 | tree t2 |
1579 | = const_binop (code: MINUS_EXPR, |
1580 | arg1: const_binop (code: MULT_EXPR, arg1: i1, arg2: r2), |
1581 | arg2: const_binop (code: MULT_EXPR, arg1: r1, arg2: i2)); |
1582 | |
1583 | real = const_binop (code, arg1: t1, arg2: magsquared); |
1584 | imag = const_binop (code, arg1: t2, arg2: magsquared); |
1585 | } |
1586 | else |
1587 | { |
1588 | /* Keep this algorithm in sync with |
1589 | tree-complex.cc:expand_complex_div_wide(). |
1590 | |
1591 | Expand complex division to scalars, modified algorithm to minimize |
1592 | overflow with wide input ranges. */ |
1593 | tree compare = fold_build2 (LT_EXPR, boolean_type_node, |
1594 | fold_abs_const (r2, TREE_TYPE (type)), |
1595 | fold_abs_const (i2, TREE_TYPE (type))); |
1596 | |
1597 | if (integer_nonzerop (compare)) |
1598 | { |
1599 | /* In the TRUE branch, we compute |
1600 | ratio = br/bi; |
1601 | div = (br * ratio) + bi; |
1602 | tr = (ar * ratio) + ai; |
1603 | ti = (ai * ratio) - ar; |
1604 | tr = tr / div; |
1605 | ti = ti / div; */ |
1606 | tree ratio = const_binop (code, arg1: r2, arg2: i2); |
1607 | tree div = const_binop (code: PLUS_EXPR, arg1: i2, |
1608 | arg2: const_binop (code: MULT_EXPR, arg1: r2, arg2: ratio)); |
1609 | real = const_binop (code: MULT_EXPR, arg1: r1, arg2: ratio); |
1610 | real = const_binop (code: PLUS_EXPR, arg1: real, arg2: i1); |
1611 | real = const_binop (code, arg1: real, arg2: div); |
1612 | |
1613 | imag = const_binop (code: MULT_EXPR, arg1: i1, arg2: ratio); |
1614 | imag = const_binop (code: MINUS_EXPR, arg1: imag, arg2: r1); |
1615 | imag = const_binop (code, arg1: imag, arg2: div); |
1616 | } |
1617 | else |
1618 | { |
1619 | /* In the FALSE branch, we compute |
1620 | ratio = d/c; |
1621 | divisor = (d * ratio) + c; |
1622 | tr = (b * ratio) + a; |
1623 | ti = b - (a * ratio); |
1624 | tr = tr / div; |
1625 | ti = ti / div; */ |
1626 | tree ratio = const_binop (code, arg1: i2, arg2: r2); |
1627 | tree div = const_binop (code: PLUS_EXPR, arg1: r2, |
1628 | arg2: const_binop (code: MULT_EXPR, arg1: i2, arg2: ratio)); |
1629 | |
1630 | real = const_binop (code: MULT_EXPR, arg1: i1, arg2: ratio); |
1631 | real = const_binop (code: PLUS_EXPR, arg1: real, arg2: r1); |
1632 | real = const_binop (code, arg1: real, arg2: div); |
1633 | |
1634 | imag = const_binop (code: MULT_EXPR, arg1: r1, arg2: ratio); |
1635 | imag = const_binop (code: MINUS_EXPR, arg1: i1, arg2: imag); |
1636 | imag = const_binop (code, arg1: imag, arg2: div); |
1637 | } |
1638 | } |
1639 | break; |
1640 | |
1641 | default: |
1642 | return NULL_TREE; |
1643 | } |
1644 | |
1645 | if (real && imag) |
1646 | return build_complex (type, real, imag); |
1647 | } |
1648 | |
1649 | if (TREE_CODE (arg1) == VECTOR_CST |
1650 | && TREE_CODE (arg2) == VECTOR_CST |
1651 | && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), |
1652 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)))) |
1653 | { |
1654 | tree type = TREE_TYPE (arg1); |
1655 | bool step_ok_p; |
1656 | if (VECTOR_CST_STEPPED_P (arg1) |
1657 | && VECTOR_CST_STEPPED_P (arg2)) |
1658 | /* We can operate directly on the encoding if: |
1659 | |
1660 | a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1 |
1661 | implies |
1662 | (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1) |
1663 | |
1664 | Addition and subtraction are the supported operators |
1665 | for which this is true. */ |
1666 | step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR); |
1667 | else if (VECTOR_CST_STEPPED_P (arg1)) |
1668 | /* We can operate directly on stepped encodings if: |
1669 | |
1670 | a3 - a2 == a2 - a1 |
1671 | implies: |
1672 | (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c) |
1673 | |
1674 | which is true if (x -> x op c) distributes over addition. */ |
1675 | step_ok_p = distributes_over_addition_p (op: code, opno: 1); |
1676 | else |
1677 | /* Similarly in reverse. */ |
1678 | step_ok_p = distributes_over_addition_p (op: code, opno: 2); |
1679 | tree_vector_builder elts; |
1680 | if (!elts.new_binary_operation (shape: type, vec1: arg1, vec2: arg2, allow_stepped_p: step_ok_p)) |
1681 | return NULL_TREE; |
1682 | unsigned int count = elts.encoded_nelts (); |
1683 | for (unsigned int i = 0; i < count; ++i) |
1684 | { |
1685 | tree elem1 = VECTOR_CST_ELT (arg1, i); |
1686 | tree elem2 = VECTOR_CST_ELT (arg2, i); |
1687 | |
1688 | tree elt = const_binop (code, arg1: elem1, arg2: elem2); |
1689 | |
1690 | /* It is possible that const_binop cannot handle the given |
1691 | code and return NULL_TREE */ |
1692 | if (elt == NULL_TREE) |
1693 | return NULL_TREE; |
1694 | elts.quick_push (obj: elt); |
1695 | } |
1696 | |
1697 | return elts.build (); |
1698 | } |
1699 | |
1700 | /* Shifts allow a scalar offset for a vector. */ |
1701 | if (TREE_CODE (arg1) == VECTOR_CST |
1702 | && TREE_CODE (arg2) == INTEGER_CST) |
1703 | { |
1704 | tree type = TREE_TYPE (arg1); |
1705 | bool step_ok_p = distributes_over_addition_p (op: code, opno: 1); |
1706 | tree_vector_builder elts; |
1707 | if (!elts.new_unary_operation (shape: type, vec: arg1, allow_stepped_p: step_ok_p)) |
1708 | return NULL_TREE; |
1709 | unsigned int count = elts.encoded_nelts (); |
1710 | for (unsigned int i = 0; i < count; ++i) |
1711 | { |
1712 | tree elem1 = VECTOR_CST_ELT (arg1, i); |
1713 | |
1714 | tree elt = const_binop (code, arg1: elem1, arg2); |
1715 | |
1716 | /* It is possible that const_binop cannot handle the given |
1717 | code and return NULL_TREE. */ |
1718 | if (elt == NULL_TREE) |
1719 | return NULL_TREE; |
1720 | elts.quick_push (obj: elt); |
1721 | } |
1722 | |
1723 | return elts.build (); |
1724 | } |
1725 | return NULL_TREE; |
1726 | } |
1727 | |
1728 | /* Overload that adds a TYPE parameter to be able to dispatch |
1729 | to fold_relational_const. */ |
1730 | |
1731 | tree |
1732 | const_binop (enum tree_code code, tree type, tree arg1, tree arg2) |
1733 | { |
1734 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
1735 | return fold_relational_const (code, type, arg1, arg2); |
1736 | |
1737 | /* ??? Until we make the const_binop worker take the type of the |
1738 | result as argument put those cases that need it here. */ |
1739 | switch (code) |
1740 | { |
1741 | case VEC_SERIES_EXPR: |
1742 | if (CONSTANT_CLASS_P (arg1) |
1743 | && CONSTANT_CLASS_P (arg2)) |
1744 | return build_vec_series (type, arg1, arg2); |
1745 | return NULL_TREE; |
1746 | |
1747 | case COMPLEX_EXPR: |
1748 | if ((TREE_CODE (arg1) == REAL_CST |
1749 | && TREE_CODE (arg2) == REAL_CST) |
1750 | || (TREE_CODE (arg1) == INTEGER_CST |
1751 | && TREE_CODE (arg2) == INTEGER_CST)) |
1752 | return build_complex (type, arg1, arg2); |
1753 | return NULL_TREE; |
1754 | |
1755 | case POINTER_DIFF_EXPR: |
1756 | if (poly_int_tree_p (t: arg1) && poly_int_tree_p (t: arg2)) |
1757 | { |
1758 | poly_offset_int res = (wi::to_poly_offset (t: arg1) |
1759 | - wi::to_poly_offset (t: arg2)); |
1760 | return force_fit_type (type, res, 1, |
1761 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)); |
1762 | } |
1763 | return NULL_TREE; |
1764 | |
1765 | case VEC_PACK_TRUNC_EXPR: |
1766 | case VEC_PACK_FIX_TRUNC_EXPR: |
1767 | case VEC_PACK_FLOAT_EXPR: |
1768 | { |
1769 | unsigned int HOST_WIDE_INT out_nelts, in_nelts, i; |
1770 | |
1771 | if (TREE_CODE (arg1) != VECTOR_CST |
1772 | || TREE_CODE (arg2) != VECTOR_CST) |
1773 | return NULL_TREE; |
1774 | |
1775 | if (!VECTOR_CST_NELTS (arg1).is_constant (const_value: &in_nelts)) |
1776 | return NULL_TREE; |
1777 | |
1778 | out_nelts = in_nelts * 2; |
1779 | gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2)) |
1780 | && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type))); |
1781 | |
1782 | tree_vector_builder elts (type, out_nelts, 1); |
1783 | for (i = 0; i < out_nelts; i++) |
1784 | { |
1785 | tree elt = (i < in_nelts |
1786 | ? VECTOR_CST_ELT (arg1, i) |
1787 | : VECTOR_CST_ELT (arg2, i - in_nelts)); |
1788 | elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR |
1789 | ? NOP_EXPR |
1790 | : code == VEC_PACK_FLOAT_EXPR |
1791 | ? FLOAT_EXPR : FIX_TRUNC_EXPR, |
1792 | TREE_TYPE (type), elt); |
1793 | if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt)) |
1794 | return NULL_TREE; |
1795 | elts.quick_push (obj: elt); |
1796 | } |
1797 | |
1798 | return elts.build (); |
1799 | } |
1800 | |
1801 | case VEC_WIDEN_MULT_LO_EXPR: |
1802 | case VEC_WIDEN_MULT_HI_EXPR: |
1803 | case VEC_WIDEN_MULT_EVEN_EXPR: |
1804 | case VEC_WIDEN_MULT_ODD_EXPR: |
1805 | { |
1806 | unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale; |
1807 | |
1808 | if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST) |
1809 | return NULL_TREE; |
1810 | |
1811 | if (!VECTOR_CST_NELTS (arg1).is_constant (const_value: &in_nelts)) |
1812 | return NULL_TREE; |
1813 | out_nelts = in_nelts / 2; |
1814 | gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2)) |
1815 | && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type))); |
1816 | |
1817 | if (code == VEC_WIDEN_MULT_LO_EXPR) |
1818 | scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0; |
1819 | else if (code == VEC_WIDEN_MULT_HI_EXPR) |
1820 | scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts; |
1821 | else if (code == VEC_WIDEN_MULT_EVEN_EXPR) |
1822 | scale = 1, ofs = 0; |
1823 | else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */ |
1824 | scale = 1, ofs = 1; |
1825 | |
1826 | tree_vector_builder elts (type, out_nelts, 1); |
1827 | for (out = 0; out < out_nelts; out++) |
1828 | { |
1829 | unsigned int in = (out << scale) + ofs; |
1830 | tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), |
1831 | VECTOR_CST_ELT (arg1, in)); |
1832 | tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), |
1833 | VECTOR_CST_ELT (arg2, in)); |
1834 | |
1835 | if (t1 == NULL_TREE || t2 == NULL_TREE) |
1836 | return NULL_TREE; |
1837 | tree elt = const_binop (code: MULT_EXPR, arg1: t1, arg2: t2); |
1838 | if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt)) |
1839 | return NULL_TREE; |
1840 | elts.quick_push (obj: elt); |
1841 | } |
1842 | |
1843 | return elts.build (); |
1844 | } |
1845 | |
1846 | default:; |
1847 | } |
1848 | |
1849 | if (TREE_CODE_CLASS (code) != tcc_binary) |
1850 | return NULL_TREE; |
1851 | |
1852 | /* Make sure type and arg0 have the same saturating flag. */ |
1853 | gcc_checking_assert (TYPE_SATURATING (type) |
1854 | == TYPE_SATURATING (TREE_TYPE (arg1))); |
1855 | |
1856 | return const_binop (code, arg1, arg2); |
1857 | } |
1858 | |
1859 | /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant. |
1860 | Return zero if computing the constants is not possible. */ |
1861 | |
1862 | tree |
1863 | const_unop (enum tree_code code, tree type, tree arg0) |
1864 | { |
1865 | /* Don't perform the operation, other than NEGATE and ABS, if |
1866 | flag_signaling_nans is on and the operand is a signaling NaN. */ |
1867 | if (TREE_CODE (arg0) == REAL_CST |
1868 | && HONOR_SNANS (arg0) |
1869 | && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0)) |
1870 | && code != NEGATE_EXPR |
1871 | && code != ABS_EXPR |
1872 | && code != ABSU_EXPR) |
1873 | return NULL_TREE; |
1874 | |
1875 | switch (code) |
1876 | { |
1877 | CASE_CONVERT: |
1878 | case FLOAT_EXPR: |
1879 | case FIX_TRUNC_EXPR: |
1880 | case FIXED_CONVERT_EXPR: |
1881 | return fold_convert_const (code, type, arg0); |
1882 | |
1883 | case ADDR_SPACE_CONVERT_EXPR: |
1884 | /* If the source address is 0, and the source address space |
1885 | cannot have a valid object at 0, fold to dest type null. */ |
1886 | if (integer_zerop (arg0) |
1887 | && !(targetm.addr_space.zero_address_valid |
1888 | (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))))) |
1889 | return fold_convert_const (code, type, arg0); |
1890 | break; |
1891 | |
1892 | case VIEW_CONVERT_EXPR: |
1893 | return fold_view_convert_expr (type, arg0); |
1894 | |
1895 | case NEGATE_EXPR: |
1896 | { |
1897 | /* Can't call fold_negate_const directly here as that doesn't |
1898 | handle all cases and we might not be able to negate some |
1899 | constants. */ |
1900 | tree tem = fold_negate_expr (UNKNOWN_LOCATION, t: arg0); |
1901 | if (tem && CONSTANT_CLASS_P (tem)) |
1902 | return tem; |
1903 | break; |
1904 | } |
1905 | |
1906 | case ABS_EXPR: |
1907 | case ABSU_EXPR: |
1908 | if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST) |
1909 | return fold_abs_const (arg0, type); |
1910 | break; |
1911 | |
1912 | case CONJ_EXPR: |
1913 | if (TREE_CODE (arg0) == COMPLEX_CST) |
1914 | { |
1915 | tree ipart = fold_negate_const (TREE_IMAGPART (arg0), |
1916 | TREE_TYPE (type)); |
1917 | return build_complex (type, TREE_REALPART (arg0), ipart); |
1918 | } |
1919 | break; |
1920 | |
1921 | case BIT_NOT_EXPR: |
1922 | if (TREE_CODE (arg0) == INTEGER_CST) |
1923 | return fold_not_const (arg0, type); |
1924 | else if (POLY_INT_CST_P (arg0)) |
1925 | return wide_int_to_tree (type, cst: -poly_int_cst_value (x: arg0)); |
1926 | /* Perform BIT_NOT_EXPR on each element individually. */ |
1927 | else if (TREE_CODE (arg0) == VECTOR_CST) |
1928 | { |
1929 | tree elem; |
1930 | |
1931 | /* This can cope with stepped encodings because ~x == -1 - x. */ |
1932 | tree_vector_builder elements; |
1933 | elements.new_unary_operation (shape: type, vec: arg0, allow_stepped_p: true); |
1934 | unsigned int i, count = elements.encoded_nelts (); |
1935 | for (i = 0; i < count; ++i) |
1936 | { |
1937 | elem = VECTOR_CST_ELT (arg0, i); |
1938 | elem = const_unop (code: BIT_NOT_EXPR, TREE_TYPE (type), arg0: elem); |
1939 | if (elem == NULL_TREE) |
1940 | break; |
1941 | elements.quick_push (obj: elem); |
1942 | } |
1943 | if (i == count) |
1944 | return elements.build (); |
1945 | } |
1946 | break; |
1947 | |
1948 | case TRUTH_NOT_EXPR: |
1949 | if (TREE_CODE (arg0) == INTEGER_CST) |
1950 | return constant_boolean_node (integer_zerop (arg0), type); |
1951 | break; |
1952 | |
1953 | case REALPART_EXPR: |
1954 | if (TREE_CODE (arg0) == COMPLEX_CST) |
1955 | return fold_convert (type, TREE_REALPART (arg0)); |
1956 | break; |
1957 | |
1958 | case IMAGPART_EXPR: |
1959 | if (TREE_CODE (arg0) == COMPLEX_CST) |
1960 | return fold_convert (type, TREE_IMAGPART (arg0)); |
1961 | break; |
1962 | |
1963 | case VEC_UNPACK_LO_EXPR: |
1964 | case VEC_UNPACK_HI_EXPR: |
1965 | case VEC_UNPACK_FLOAT_LO_EXPR: |
1966 | case VEC_UNPACK_FLOAT_HI_EXPR: |
1967 | case VEC_UNPACK_FIX_TRUNC_LO_EXPR: |
1968 | case VEC_UNPACK_FIX_TRUNC_HI_EXPR: |
1969 | { |
1970 | unsigned HOST_WIDE_INT out_nelts, in_nelts, i; |
1971 | enum tree_code subcode; |
1972 | |
1973 | if (TREE_CODE (arg0) != VECTOR_CST) |
1974 | return NULL_TREE; |
1975 | |
1976 | if (!VECTOR_CST_NELTS (arg0).is_constant (const_value: &in_nelts)) |
1977 | return NULL_TREE; |
1978 | out_nelts = in_nelts / 2; |
1979 | gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type))); |
1980 | |
1981 | unsigned int offset = 0; |
1982 | if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR |
1983 | || code == VEC_UNPACK_FLOAT_LO_EXPR |
1984 | || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR)) |
1985 | offset = out_nelts; |
1986 | |
1987 | if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR) |
1988 | subcode = NOP_EXPR; |
1989 | else if (code == VEC_UNPACK_FLOAT_LO_EXPR |
1990 | || code == VEC_UNPACK_FLOAT_HI_EXPR) |
1991 | subcode = FLOAT_EXPR; |
1992 | else |
1993 | subcode = FIX_TRUNC_EXPR; |
1994 | |
1995 | tree_vector_builder elts (type, out_nelts, 1); |
1996 | for (i = 0; i < out_nelts; i++) |
1997 | { |
1998 | tree elt = fold_convert_const (subcode, TREE_TYPE (type), |
1999 | VECTOR_CST_ELT (arg0, i + offset)); |
2000 | if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt)) |
2001 | return NULL_TREE; |
2002 | elts.quick_push (obj: elt); |
2003 | } |
2004 | |
2005 | return elts.build (); |
2006 | } |
2007 | |
2008 | case VEC_DUPLICATE_EXPR: |
2009 | if (CONSTANT_CLASS_P (arg0)) |
2010 | return build_vector_from_val (type, arg0); |
2011 | return NULL_TREE; |
2012 | |
2013 | default: |
2014 | break; |
2015 | } |
2016 | |
2017 | return NULL_TREE; |
2018 | } |
2019 | |
2020 | /* Create a sizetype INT_CST node with NUMBER sign extended. KIND |
2021 | indicates which particular sizetype to create. */ |
2022 | |
2023 | tree |
2024 | size_int_kind (poly_int64 number, enum size_type_kind kind) |
2025 | { |
2026 | return build_int_cst (sizetype_tab[(int) kind], number); |
2027 | } |
2028 | |
2029 | /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE |
2030 | is a tree code. The type of the result is taken from the operands. |
2031 | Both must be equivalent integer types, ala int_binop_types_match_p. |
2032 | If the operands are constant, so is the result. */ |
2033 | |
2034 | tree |
2035 | size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1) |
2036 | { |
2037 | tree type = TREE_TYPE (arg0); |
2038 | |
2039 | if (arg0 == error_mark_node || arg1 == error_mark_node) |
2040 | return error_mark_node; |
2041 | |
2042 | gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0), |
2043 | TREE_TYPE (arg1))); |
2044 | |
2045 | /* Handle the special case of two poly_int constants faster. */ |
2046 | if (poly_int_tree_p (t: arg0) && poly_int_tree_p (t: arg1)) |
2047 | { |
2048 | /* And some specific cases even faster than that. */ |
2049 | if (code == PLUS_EXPR) |
2050 | { |
2051 | if (integer_zerop (arg0) |
2052 | && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0))) |
2053 | return arg1; |
2054 | if (integer_zerop (arg1) |
2055 | && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1))) |
2056 | return arg0; |
2057 | } |
2058 | else if (code == MINUS_EXPR) |
2059 | { |
2060 | if (integer_zerop (arg1) |
2061 | && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1))) |
2062 | return arg0; |
2063 | } |
2064 | else if (code == MULT_EXPR) |
2065 | { |
2066 | if (integer_onep (arg0) |
2067 | && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0))) |
2068 | return arg1; |
2069 | } |
2070 | |
2071 | /* Handle general case of two integer constants. For sizetype |
2072 | constant calculations we always want to know about overflow, |
2073 | even in the unsigned case. */ |
2074 | tree res = int_const_binop (code, arg1: arg0, arg2: arg1, overflowable: -1); |
2075 | if (res != NULL_TREE) |
2076 | return res; |
2077 | } |
2078 | |
2079 | return fold_build2_loc (loc, code, type, arg0, arg1); |
2080 | } |
2081 | |
2082 | /* Given two values, either both of sizetype or both of bitsizetype, |
2083 | compute the difference between the two values. Return the value |
2084 | in signed type corresponding to the type of the operands. */ |
2085 | |
2086 | tree |
2087 | size_diffop_loc (location_t loc, tree arg0, tree arg1) |
2088 | { |
2089 | tree type = TREE_TYPE (arg0); |
2090 | tree ctype; |
2091 | |
2092 | gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0), |
2093 | TREE_TYPE (arg1))); |
2094 | |
2095 | /* If the type is already signed, just do the simple thing. */ |
2096 | if (!TYPE_UNSIGNED (type)) |
2097 | return size_binop_loc (loc, code: MINUS_EXPR, arg0, arg1); |
2098 | |
2099 | if (type == sizetype) |
2100 | ctype = ssizetype; |
2101 | else if (type == bitsizetype) |
2102 | ctype = sbitsizetype; |
2103 | else |
2104 | ctype = signed_type_for (type); |
2105 | |
2106 | /* If either operand is not a constant, do the conversions to the signed |
2107 | type and subtract. The hardware will do the right thing with any |
2108 | overflow in the subtraction. */ |
2109 | if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST) |
2110 | return size_binop_loc (loc, code: MINUS_EXPR, |
2111 | arg0: fold_convert_loc (loc, ctype, arg0), |
2112 | arg1: fold_convert_loc (loc, ctype, arg1)); |
2113 | |
2114 | /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE. |
2115 | Otherwise, subtract the other way, convert to CTYPE (we know that can't |
2116 | overflow) and negate (which can't either). Special-case a result |
2117 | of zero while we're here. */ |
2118 | if (tree_int_cst_equal (arg0, arg1)) |
2119 | return build_int_cst (ctype, 0); |
2120 | else if (tree_int_cst_lt (t1: arg1, t2: arg0)) |
2121 | return fold_convert_loc (loc, ctype, |
2122 | size_binop_loc (loc, code: MINUS_EXPR, arg0, arg1)); |
2123 | else |
2124 | return size_binop_loc (loc, code: MINUS_EXPR, arg0: build_int_cst (ctype, 0), |
2125 | arg1: fold_convert_loc (loc, ctype, |
2126 | size_binop_loc (loc, |
2127 | code: MINUS_EXPR, |
2128 | arg0: arg1, arg1: arg0))); |
2129 | } |
2130 | |
2131 | /* A subroutine of fold_convert_const handling conversions of an |
2132 | INTEGER_CST to another integer type. */ |
2133 | |
2134 | static tree |
2135 | fold_convert_const_int_from_int (tree type, const_tree arg1) |
2136 | { |
2137 | /* Given an integer constant, make new constant with new type, |
2138 | appropriately sign-extended or truncated. Use widest_int |
2139 | so that any extension is done according ARG1's type. */ |
2140 | tree arg1_type = TREE_TYPE (arg1); |
2141 | unsigned prec = MAX (TYPE_PRECISION (arg1_type), TYPE_PRECISION (type)); |
2142 | return force_fit_type (type, wide_int::from (x: wi::to_wide (t: arg1), precision: prec, |
2143 | TYPE_SIGN (arg1_type)), |
2144 | !POINTER_TYPE_P (TREE_TYPE (arg1)), |
2145 | TREE_OVERFLOW (arg1)); |
2146 | } |
2147 | |
2148 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
2149 | to an integer type. */ |
2150 | |
2151 | static tree |
2152 | fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1) |
2153 | { |
2154 | bool overflow = false; |
2155 | tree t; |
2156 | |
2157 | /* The following code implements the floating point to integer |
2158 | conversion rules required by the Java Language Specification, |
2159 | that IEEE NaNs are mapped to zero and values that overflow |
2160 | the target precision saturate, i.e. values greater than |
2161 | INT_MAX are mapped to INT_MAX, and values less than INT_MIN |
2162 | are mapped to INT_MIN. These semantics are allowed by the |
2163 | C and C++ standards that simply state that the behavior of |
2164 | FP-to-integer conversion is unspecified upon overflow. */ |
2165 | |
2166 | wide_int val; |
2167 | REAL_VALUE_TYPE r; |
2168 | REAL_VALUE_TYPE x = TREE_REAL_CST (arg1); |
2169 | |
2170 | switch (code) |
2171 | { |
2172 | case FIX_TRUNC_EXPR: |
2173 | real_trunc (&r, VOIDmode, &x); |
2174 | break; |
2175 | |
2176 | default: |
2177 | gcc_unreachable (); |
2178 | } |
2179 | |
2180 | /* If R is NaN, return zero and show we have an overflow. */ |
2181 | if (REAL_VALUE_ISNAN (r)) |
2182 | { |
2183 | overflow = true; |
2184 | val = wi::zero (TYPE_PRECISION (type)); |
2185 | } |
2186 | |
2187 | /* See if R is less than the lower bound or greater than the |
2188 | upper bound. */ |
2189 | |
2190 | if (! overflow) |
2191 | { |
2192 | tree lt = TYPE_MIN_VALUE (type); |
2193 | REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt); |
2194 | if (real_less (&r, &l)) |
2195 | { |
2196 | overflow = true; |
2197 | val = wi::to_wide (t: lt); |
2198 | } |
2199 | } |
2200 | |
2201 | if (! overflow) |
2202 | { |
2203 | tree ut = TYPE_MAX_VALUE (type); |
2204 | if (ut) |
2205 | { |
2206 | REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut); |
2207 | if (real_less (&u, &r)) |
2208 | { |
2209 | overflow = true; |
2210 | val = wi::to_wide (t: ut); |
2211 | } |
2212 | } |
2213 | } |
2214 | |
2215 | if (! overflow) |
2216 | val = real_to_integer (&r, &overflow, TYPE_PRECISION (type)); |
2217 | |
2218 | t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1)); |
2219 | return t; |
2220 | } |
2221 | |
2222 | /* A subroutine of fold_convert_const handling conversions of a |
2223 | FIXED_CST to an integer type. */ |
2224 | |
2225 | static tree |
2226 | fold_convert_const_int_from_fixed (tree type, const_tree arg1) |
2227 | { |
2228 | tree t; |
2229 | double_int temp, temp_trunc; |
2230 | scalar_mode mode; |
2231 | |
2232 | /* Right shift FIXED_CST to temp by fbit. */ |
2233 | temp = TREE_FIXED_CST (arg1).data; |
2234 | mode = TREE_FIXED_CST (arg1).mode; |
2235 | if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT) |
2236 | { |
2237 | temp = temp.rshift (GET_MODE_FBIT (mode), |
2238 | HOST_BITS_PER_DOUBLE_INT, |
2239 | SIGNED_FIXED_POINT_MODE_P (mode)); |
2240 | |
2241 | /* Left shift temp to temp_trunc by fbit. */ |
2242 | temp_trunc = temp.lshift (GET_MODE_FBIT (mode), |
2243 | HOST_BITS_PER_DOUBLE_INT, |
2244 | SIGNED_FIXED_POINT_MODE_P (mode)); |
2245 | } |
2246 | else |
2247 | { |
2248 | temp = double_int_zero; |
2249 | temp_trunc = double_int_zero; |
2250 | } |
2251 | |
2252 | /* If FIXED_CST is negative, we need to round the value toward 0. |
2253 | By checking if the fractional bits are not zero to add 1 to temp. */ |
2254 | if (SIGNED_FIXED_POINT_MODE_P (mode) |
2255 | && temp_trunc.is_negative () |
2256 | && TREE_FIXED_CST (arg1).data != temp_trunc) |
2257 | temp += double_int_one; |
2258 | |
2259 | /* Given a fixed-point constant, make new constant with new type, |
2260 | appropriately sign-extended or truncated. */ |
2261 | t = force_fit_type (type, temp, -1, |
2262 | (temp.is_negative () |
2263 | && (TYPE_UNSIGNED (type) |
2264 | < TYPE_UNSIGNED (TREE_TYPE (arg1)))) |
2265 | | TREE_OVERFLOW (arg1)); |
2266 | |
2267 | return t; |
2268 | } |
2269 | |
2270 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
2271 | to another floating point type. */ |
2272 | |
2273 | static tree |
2274 | fold_convert_const_real_from_real (tree type, const_tree arg1) |
2275 | { |
2276 | REAL_VALUE_TYPE value; |
2277 | tree t; |
2278 | |
2279 | /* If the underlying modes are the same, simply treat it as |
2280 | copy and rebuild with TREE_REAL_CST information and the |
2281 | given type. */ |
2282 | if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1))) |
2283 | { |
2284 | t = build_real (type, TREE_REAL_CST (arg1)); |
2285 | return t; |
2286 | } |
2287 | |
2288 | /* Don't perform the operation if flag_signaling_nans is on |
2289 | and the operand is a signaling NaN. */ |
2290 | if (HONOR_SNANS (arg1) |
2291 | && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1))) |
2292 | return NULL_TREE; |
2293 | |
2294 | /* With flag_rounding_math we should respect the current rounding mode |
2295 | unless the conversion is exact. */ |
2296 | if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1) |
2297 | && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1))) |
2298 | return NULL_TREE; |
2299 | |
2300 | real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1)); |
2301 | t = build_real (type, value); |
2302 | |
2303 | /* If converting an infinity or NAN to a representation that doesn't |
2304 | have one, set the overflow bit so that we can produce some kind of |
2305 | error message at the appropriate point if necessary. It's not the |
2306 | most user-friendly message, but it's better than nothing. */ |
2307 | if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1)) |
2308 | && !MODE_HAS_INFINITIES (TYPE_MODE (type))) |
2309 | TREE_OVERFLOW (t) = 1; |
2310 | else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)) |
2311 | && !MODE_HAS_NANS (TYPE_MODE (type))) |
2312 | TREE_OVERFLOW (t) = 1; |
2313 | /* Regular overflow, conversion produced an infinity in a mode that |
2314 | can't represent them. */ |
2315 | else if (!MODE_HAS_INFINITIES (TYPE_MODE (type)) |
2316 | && REAL_VALUE_ISINF (value) |
2317 | && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1))) |
2318 | TREE_OVERFLOW (t) = 1; |
2319 | else |
2320 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); |
2321 | return t; |
2322 | } |
2323 | |
2324 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST |
2325 | to a floating point type. */ |
2326 | |
2327 | static tree |
2328 | fold_convert_const_real_from_fixed (tree type, const_tree arg1) |
2329 | { |
2330 | REAL_VALUE_TYPE value; |
2331 | tree t; |
2332 | |
2333 | real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type), |
2334 | &TREE_FIXED_CST (arg1)); |
2335 | t = build_real (type, value); |
2336 | |
2337 | TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1); |
2338 | return t; |
2339 | } |
2340 | |
2341 | /* A subroutine of fold_convert_const handling conversions a FIXED_CST |
2342 | to another fixed-point type. */ |
2343 | |
2344 | static tree |
2345 | fold_convert_const_fixed_from_fixed (tree type, const_tree arg1) |
2346 | { |
2347 | FIXED_VALUE_TYPE value; |
2348 | tree t; |
2349 | bool overflow_p; |
2350 | |
2351 | overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type), |
2352 | &TREE_FIXED_CST (arg1), TYPE_SATURATING (type)); |
2353 | t = build_fixed (type, value); |
2354 | |
2355 | /* Propagate overflow flags. */ |
2356 | if (overflow_p | TREE_OVERFLOW (arg1)) |
2357 | TREE_OVERFLOW (t) = 1; |
2358 | return t; |
2359 | } |
2360 | |
2361 | /* A subroutine of fold_convert_const handling conversions an INTEGER_CST |
2362 | to a fixed-point type. */ |
2363 | |
2364 | static tree |
2365 | fold_convert_const_fixed_from_int (tree type, const_tree arg1) |
2366 | { |
2367 | FIXED_VALUE_TYPE value; |
2368 | tree t; |
2369 | bool overflow_p; |
2370 | double_int di; |
2371 | |
2372 | gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2); |
2373 | |
2374 | di.low = TREE_INT_CST_ELT (arg1, 0); |
2375 | if (TREE_INT_CST_NUNITS (arg1) == 1) |
2376 | di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0; |
2377 | else |
2378 | di.high = TREE_INT_CST_ELT (arg1, 1); |
2379 | |
2380 | overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di, |
2381 | TYPE_UNSIGNED (TREE_TYPE (arg1)), |
2382 | TYPE_SATURATING (type)); |
2383 | t = build_fixed (type, value); |
2384 | |
2385 | /* Propagate overflow flags. */ |
2386 | if (overflow_p | TREE_OVERFLOW (arg1)) |
2387 | TREE_OVERFLOW (t) = 1; |
2388 | return t; |
2389 | } |
2390 | |
2391 | /* A subroutine of fold_convert_const handling conversions a REAL_CST |
2392 | to a fixed-point type. */ |
2393 | |
2394 | static tree |
2395 | fold_convert_const_fixed_from_real (tree type, const_tree arg1) |
2396 | { |
2397 | FIXED_VALUE_TYPE value; |
2398 | tree t; |
2399 | bool overflow_p; |
2400 | |
2401 | overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type), |
2402 | &TREE_REAL_CST (arg1), |
2403 | TYPE_SATURATING (type)); |
2404 | t = build_fixed (type, value); |
2405 | |
2406 | /* Propagate overflow flags. */ |
2407 | if (overflow_p | TREE_OVERFLOW (arg1)) |
2408 | TREE_OVERFLOW (t) = 1; |
2409 | return t; |
2410 | } |
2411 | |
2412 | /* Attempt to fold type conversion operation CODE of expression ARG1 to |
2413 | type TYPE. If no simplification can be done return NULL_TREE. */ |
2414 | |
2415 | static tree |
2416 | fold_convert_const (enum tree_code code, tree type, tree arg1) |
2417 | { |
2418 | tree arg_type = TREE_TYPE (arg1); |
2419 | if (arg_type == type) |
2420 | return arg1; |
2421 | |
2422 | /* We can't widen types, since the runtime value could overflow the |
2423 | original type before being extended to the new type. */ |
2424 | if (POLY_INT_CST_P (arg1) |
2425 | && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)) |
2426 | && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type)) |
2427 | return build_poly_int_cst (type, |
2428 | poly_wide_int::from (a: poly_int_cst_value (x: arg1), |
2429 | TYPE_PRECISION (type), |
2430 | TYPE_SIGN (arg_type))); |
2431 | |
2432 | if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type) |
2433 | || TREE_CODE (type) == OFFSET_TYPE) |
2434 | { |
2435 | if (TREE_CODE (arg1) == INTEGER_CST) |
2436 | return fold_convert_const_int_from_int (type, arg1); |
2437 | else if (TREE_CODE (arg1) == REAL_CST) |
2438 | return fold_convert_const_int_from_real (code, type, arg1); |
2439 | else if (TREE_CODE (arg1) == FIXED_CST) |
2440 | return fold_convert_const_int_from_fixed (type, arg1); |
2441 | } |
2442 | else if (SCALAR_FLOAT_TYPE_P (type)) |
2443 | { |
2444 | if (TREE_CODE (arg1) == INTEGER_CST) |
2445 | { |
2446 | tree res = build_real_from_int_cst (type, arg1); |
2447 | /* Avoid the folding if flag_rounding_math is on and the |
2448 | conversion is not exact. */ |
2449 | if (HONOR_SIGN_DEPENDENT_ROUNDING (type)) |
2450 | { |
2451 | bool fail = false; |
2452 | wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail, |
2453 | TYPE_PRECISION (TREE_TYPE (arg1))); |
2454 | if (fail || wi::ne_p (x: w, y: wi::to_wide (t: arg1))) |
2455 | return NULL_TREE; |
2456 | } |
2457 | return res; |
2458 | } |
2459 | else if (TREE_CODE (arg1) == REAL_CST) |
2460 | return fold_convert_const_real_from_real (type, arg1); |
2461 | else if (TREE_CODE (arg1) == FIXED_CST) |
2462 | return fold_convert_const_real_from_fixed (type, arg1); |
2463 | } |
2464 | else if (FIXED_POINT_TYPE_P (type)) |
2465 | { |
2466 | if (TREE_CODE (arg1) == FIXED_CST) |
2467 | return fold_convert_const_fixed_from_fixed (type, arg1); |
2468 | else if (TREE_CODE (arg1) == INTEGER_CST) |
2469 | return fold_convert_const_fixed_from_int (type, arg1); |
2470 | else if (TREE_CODE (arg1) == REAL_CST) |
2471 | return fold_convert_const_fixed_from_real (type, arg1); |
2472 | } |
2473 | else if (VECTOR_TYPE_P (type)) |
2474 | { |
2475 | if (TREE_CODE (arg1) == VECTOR_CST |
2476 | && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1))) |
2477 | { |
2478 | tree elttype = TREE_TYPE (type); |
2479 | tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1)); |
2480 | /* We can't handle steps directly when extending, since the |
2481 | values need to wrap at the original precision first. */ |
2482 | bool step_ok_p |
2483 | = (INTEGRAL_TYPE_P (elttype) |
2484 | && INTEGRAL_TYPE_P (arg1_elttype) |
2485 | && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype)); |
2486 | tree_vector_builder v; |
2487 | if (!v.new_unary_operation (shape: type, vec: arg1, allow_stepped_p: step_ok_p)) |
2488 | return NULL_TREE; |
2489 | unsigned int len = v.encoded_nelts (); |
2490 | for (unsigned int i = 0; i < len; ++i) |
2491 | { |
2492 | tree elt = VECTOR_CST_ELT (arg1, i); |
2493 | tree cvt = fold_convert_const (code, type: elttype, arg1: elt); |
2494 | if (cvt == NULL_TREE) |
2495 | return NULL_TREE; |
2496 | v.quick_push (obj: cvt); |
2497 | } |
2498 | return v.build (); |
2499 | } |
2500 | } |
2501 | return NULL_TREE; |
2502 | } |
2503 | |
2504 | /* Construct a vector of zero elements of vector type TYPE. */ |
2505 | |
2506 | static tree |
2507 | build_zero_vector (tree type) |
2508 | { |
2509 | tree t; |
2510 | |
2511 | t = fold_convert_const (code: NOP_EXPR, TREE_TYPE (type), integer_zero_node); |
2512 | return build_vector_from_val (type, t); |
2513 | } |
2514 | |
2515 | /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */ |
2516 | |
2517 | bool |
2518 | fold_convertible_p (const_tree type, const_tree arg) |
2519 | { |
2520 | const_tree orig = TREE_TYPE (arg); |
2521 | |
2522 | if (type == orig) |
2523 | return true; |
2524 | |
2525 | if (TREE_CODE (arg) == ERROR_MARK |
2526 | || TREE_CODE (type) == ERROR_MARK |
2527 | || TREE_CODE (orig) == ERROR_MARK) |
2528 | return false; |
2529 | |
2530 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) |
2531 | return true; |
2532 | |
2533 | switch (TREE_CODE (type)) |
2534 | { |
2535 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: |
2536 | case POINTER_TYPE: case REFERENCE_TYPE: |
2537 | case OFFSET_TYPE: |
2538 | return (INTEGRAL_TYPE_P (orig) |
2539 | || (POINTER_TYPE_P (orig) |
2540 | && TYPE_PRECISION (type) <= TYPE_PRECISION (orig)) |
2541 | || TREE_CODE (orig) == OFFSET_TYPE); |
2542 | |
2543 | case REAL_TYPE: |
2544 | case FIXED_POINT_TYPE: |
2545 | case VOID_TYPE: |
2546 | return TREE_CODE (type) == TREE_CODE (orig); |
2547 | |
2548 | case VECTOR_TYPE: |
2549 | return (VECTOR_TYPE_P (orig) |
2550 | && known_eq (TYPE_VECTOR_SUBPARTS (type), |
2551 | TYPE_VECTOR_SUBPARTS (orig)) |
2552 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); |
2553 | |
2554 | default: |
2555 | return false; |
2556 | } |
2557 | } |
2558 | |
2559 | /* Convert expression ARG to type TYPE. Used by the middle-end for |
2560 | simple conversions in preference to calling the front-end's convert. */ |
2561 | |
2562 | tree |
2563 | fold_convert_loc (location_t loc, tree type, tree arg) |
2564 | { |
2565 | tree orig = TREE_TYPE (arg); |
2566 | tree tem; |
2567 | |
2568 | if (type == orig) |
2569 | return arg; |
2570 | |
2571 | if (TREE_CODE (arg) == ERROR_MARK |
2572 | || TREE_CODE (type) == ERROR_MARK |
2573 | || TREE_CODE (orig) == ERROR_MARK) |
2574 | return error_mark_node; |
2575 | |
2576 | switch (TREE_CODE (type)) |
2577 | { |
2578 | case POINTER_TYPE: |
2579 | case REFERENCE_TYPE: |
2580 | /* Handle conversions between pointers to different address spaces. */ |
2581 | if (POINTER_TYPE_P (orig) |
2582 | && (TYPE_ADDR_SPACE (TREE_TYPE (type)) |
2583 | != TYPE_ADDR_SPACE (TREE_TYPE (orig)))) |
2584 | return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg); |
2585 | /* fall through */ |
2586 | |
2587 | case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE: |
2588 | case OFFSET_TYPE: case BITINT_TYPE: |
2589 | if (TREE_CODE (arg) == INTEGER_CST) |
2590 | { |
2591 | tem = fold_convert_const (code: NOP_EXPR, type, arg1: arg); |
2592 | if (tem != NULL_TREE) |
2593 | return tem; |
2594 | } |
2595 | if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) |
2596 | || TREE_CODE (orig) == OFFSET_TYPE) |
2597 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
2598 | if (TREE_CODE (orig) == COMPLEX_TYPE) |
2599 | return fold_convert_loc (loc, type, |
2600 | arg: fold_build1_loc (loc, REALPART_EXPR, |
2601 | TREE_TYPE (orig), arg)); |
2602 | gcc_assert (VECTOR_TYPE_P (orig) |
2603 | && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); |
2604 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg); |
2605 | |
2606 | case REAL_TYPE: |
2607 | if (TREE_CODE (arg) == INTEGER_CST) |
2608 | { |
2609 | tem = fold_convert_const (code: FLOAT_EXPR, type, arg1: arg); |
2610 | if (tem != NULL_TREE) |
2611 | return tem; |
2612 | } |
2613 | else if (TREE_CODE (arg) == REAL_CST) |
2614 | { |
2615 | tem = fold_convert_const (code: NOP_EXPR, type, arg1: arg); |
2616 | if (tem != NULL_TREE) |
2617 | return tem; |
2618 | } |
2619 | else if (TREE_CODE (arg) == FIXED_CST) |
2620 | { |
2621 | tem = fold_convert_const (code: FIXED_CONVERT_EXPR, type, arg1: arg); |
2622 | if (tem != NULL_TREE) |
2623 | return tem; |
2624 | } |
2625 | |
2626 | switch (TREE_CODE (orig)) |
2627 | { |
2628 | case INTEGER_TYPE: case BITINT_TYPE: |
2629 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
2630 | case POINTER_TYPE: case REFERENCE_TYPE: |
2631 | return fold_build1_loc (loc, FLOAT_EXPR, type, arg); |
2632 | |
2633 | case REAL_TYPE: |
2634 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
2635 | |
2636 | case FIXED_POINT_TYPE: |
2637 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
2638 | |
2639 | case COMPLEX_TYPE: |
2640 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2641 | return fold_convert_loc (loc, type, arg: tem); |
2642 | |
2643 | default: |
2644 | gcc_unreachable (); |
2645 | } |
2646 | |
2647 | case FIXED_POINT_TYPE: |
2648 | if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST |
2649 | || TREE_CODE (arg) == REAL_CST) |
2650 | { |
2651 | tem = fold_convert_const (code: FIXED_CONVERT_EXPR, type, arg1: arg); |
2652 | if (tem != NULL_TREE) |
2653 | goto fold_convert_exit; |
2654 | } |
2655 | |
2656 | switch (TREE_CODE (orig)) |
2657 | { |
2658 | case FIXED_POINT_TYPE: |
2659 | case INTEGER_TYPE: |
2660 | case ENUMERAL_TYPE: |
2661 | case BOOLEAN_TYPE: |
2662 | case REAL_TYPE: |
2663 | case BITINT_TYPE: |
2664 | return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg); |
2665 | |
2666 | case COMPLEX_TYPE: |
2667 | tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2668 | return fold_convert_loc (loc, type, arg: tem); |
2669 | |
2670 | default: |
2671 | gcc_unreachable (); |
2672 | } |
2673 | |
2674 | case COMPLEX_TYPE: |
2675 | switch (TREE_CODE (orig)) |
2676 | { |
2677 | case INTEGER_TYPE: case BITINT_TYPE: |
2678 | case BOOLEAN_TYPE: case ENUMERAL_TYPE: |
2679 | case POINTER_TYPE: case REFERENCE_TYPE: |
2680 | case REAL_TYPE: |
2681 | case FIXED_POINT_TYPE: |
2682 | return fold_build2_loc (loc, COMPLEX_EXPR, type, |
2683 | fold_convert_loc (loc, TREE_TYPE (type), arg), |
2684 | fold_convert_loc (loc, TREE_TYPE (type), |
2685 | integer_zero_node)); |
2686 | case COMPLEX_TYPE: |
2687 | { |
2688 | tree rpart, ipart; |
2689 | |
2690 | if (TREE_CODE (arg) == COMPLEX_EXPR) |
2691 | { |
2692 | rpart = fold_convert_loc (loc, TREE_TYPE (type), |
2693 | TREE_OPERAND (arg, 0)); |
2694 | ipart = fold_convert_loc (loc, TREE_TYPE (type), |
2695 | TREE_OPERAND (arg, 1)); |
2696 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); |
2697 | } |
2698 | |
2699 | arg = save_expr (arg); |
2700 | rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg); |
2701 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg); |
2702 | rpart = fold_convert_loc (loc, TREE_TYPE (type), arg: rpart); |
2703 | ipart = fold_convert_loc (loc, TREE_TYPE (type), arg: ipart); |
2704 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart); |
2705 | } |
2706 | |
2707 | default: |
2708 | gcc_unreachable (); |
2709 | } |
2710 | |
2711 | case VECTOR_TYPE: |
2712 | if (integer_zerop (arg)) |
2713 | return build_zero_vector (type); |
2714 | gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig))); |
2715 | gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig) |
2716 | || VECTOR_TYPE_P (orig)); |
2717 | return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg); |
2718 | |
2719 | case VOID_TYPE: |
2720 | tem = fold_ignored_result (arg); |
2721 | return fold_build1_loc (loc, NOP_EXPR, type, tem); |
2722 | |
2723 | default: |
2724 | if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)) |
2725 | return fold_build1_loc (loc, NOP_EXPR, type, arg); |
2726 | gcc_unreachable (); |
2727 | } |
2728 | fold_convert_exit: |
2729 | tem = protected_set_expr_location_unshare (x: tem, loc); |
2730 | return tem; |
2731 | } |
2732 | |
2733 | /* Return false if expr can be assumed not to be an lvalue, true |
2734 | otherwise. */ |
2735 | |
2736 | static bool |
2737 | maybe_lvalue_p (const_tree x) |
2738 | { |
2739 | /* We only need to wrap lvalue tree codes. */ |
2740 | switch (TREE_CODE (x)) |
2741 | { |
2742 | case VAR_DECL: |
2743 | case PARM_DECL: |
2744 | case RESULT_DECL: |
2745 | case LABEL_DECL: |
2746 | case FUNCTION_DECL: |
2747 | case SSA_NAME: |
2748 | case COMPOUND_LITERAL_EXPR: |
2749 | |
2750 | case COMPONENT_REF: |
2751 | case MEM_REF: |
2752 | case INDIRECT_REF: |
2753 | case ARRAY_REF: |
2754 | case ARRAY_RANGE_REF: |
2755 | case BIT_FIELD_REF: |
2756 | case OBJ_TYPE_REF: |
2757 | |
2758 | case REALPART_EXPR: |
2759 | case IMAGPART_EXPR: |
2760 | case PREINCREMENT_EXPR: |
2761 | case PREDECREMENT_EXPR: |
2762 | case SAVE_EXPR: |
2763 | case TRY_CATCH_EXPR: |
2764 | case WITH_CLEANUP_EXPR: |
2765 | case COMPOUND_EXPR: |
2766 | case MODIFY_EXPR: |
2767 | case TARGET_EXPR: |
2768 | case COND_EXPR: |
2769 | case BIND_EXPR: |
2770 | case VIEW_CONVERT_EXPR: |
2771 | break; |
2772 | |
2773 | default: |
2774 | /* Assume the worst for front-end tree codes. */ |
2775 | if ((int)TREE_CODE (x) >= NUM_TREE_CODES) |
2776 | break; |
2777 | return false; |
2778 | } |
2779 | |
2780 | return true; |
2781 | } |
2782 | |
2783 | /* Return an expr equal to X but certainly not valid as an lvalue. */ |
2784 | |
2785 | tree |
2786 | non_lvalue_loc (location_t loc, tree x) |
2787 | { |
2788 | /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to |
2789 | us. */ |
2790 | if (in_gimple_form) |
2791 | return x; |
2792 | |
2793 | if (! maybe_lvalue_p (x)) |
2794 | return x; |
2795 | return build1_loc (loc, code: NON_LVALUE_EXPR, TREE_TYPE (x), arg1: x); |
2796 | } |
2797 | |
2798 | /* Given a tree comparison code, return the code that is the logical inverse. |
2799 | It is generally not safe to do this for floating-point comparisons, except |
2800 | for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return |
2801 | ERROR_MARK in this case. */ |
2802 | |
2803 | enum tree_code |
2804 | invert_tree_comparison (enum tree_code code, bool honor_nans) |
2805 | { |
2806 | if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR |
2807 | && code != ORDERED_EXPR && code != UNORDERED_EXPR) |
2808 | return ERROR_MARK; |
2809 | |
2810 | switch (code) |
2811 | { |
2812 | case EQ_EXPR: |
2813 | return NE_EXPR; |
2814 | case NE_EXPR: |
2815 | return EQ_EXPR; |
2816 | case GT_EXPR: |
2817 | return honor_nans ? UNLE_EXPR : LE_EXPR; |
2818 | case GE_EXPR: |
2819 | return honor_nans ? UNLT_EXPR : LT_EXPR; |
2820 | case LT_EXPR: |
2821 | return honor_nans ? UNGE_EXPR : GE_EXPR; |
2822 | case LE_EXPR: |
2823 | return honor_nans ? UNGT_EXPR : GT_EXPR; |
2824 | case LTGT_EXPR: |
2825 | return UNEQ_EXPR; |
2826 | case UNEQ_EXPR: |
2827 | return LTGT_EXPR; |
2828 | case UNGT_EXPR: |
2829 | return LE_EXPR; |
2830 | case UNGE_EXPR: |
2831 | return LT_EXPR; |
2832 | case UNLT_EXPR: |
2833 | return GE_EXPR; |
2834 | case UNLE_EXPR: |
2835 | return GT_EXPR; |
2836 | case ORDERED_EXPR: |
2837 | return UNORDERED_EXPR; |
2838 | case UNORDERED_EXPR: |
2839 | return ORDERED_EXPR; |
2840 | default: |
2841 | gcc_unreachable (); |
2842 | } |
2843 | } |
2844 | |
2845 | /* Similar, but return the comparison that results if the operands are |
2846 | swapped. This is safe for floating-point. */ |
2847 | |
2848 | enum tree_code |
2849 | swap_tree_comparison (enum tree_code code) |
2850 | { |
2851 | switch (code) |
2852 | { |
2853 | case EQ_EXPR: |
2854 | case NE_EXPR: |
2855 | case ORDERED_EXPR: |
2856 | case UNORDERED_EXPR: |
2857 | case LTGT_EXPR: |
2858 | case UNEQ_EXPR: |
2859 | return code; |
2860 | case GT_EXPR: |
2861 | return LT_EXPR; |
2862 | case GE_EXPR: |
2863 | return LE_EXPR; |
2864 | case LT_EXPR: |
2865 | return GT_EXPR; |
2866 | case LE_EXPR: |
2867 | return GE_EXPR; |
2868 | case UNGT_EXPR: |
2869 | return UNLT_EXPR; |
2870 | case UNGE_EXPR: |
2871 | return UNLE_EXPR; |
2872 | case UNLT_EXPR: |
2873 | return UNGT_EXPR; |
2874 | case UNLE_EXPR: |
2875 | return UNGE_EXPR; |
2876 | default: |
2877 | gcc_unreachable (); |
2878 | } |
2879 | } |
2880 | |
2881 | |
2882 | /* Convert a comparison tree code from an enum tree_code representation |
2883 | into a compcode bit-based encoding. This function is the inverse of |
2884 | compcode_to_comparison. */ |
2885 | |
2886 | static enum comparison_code |
2887 | comparison_to_compcode (enum tree_code code) |
2888 | { |
2889 | switch (code) |
2890 | { |
2891 | case LT_EXPR: |
2892 | return COMPCODE_LT; |
2893 | case EQ_EXPR: |
2894 | return COMPCODE_EQ; |
2895 | case LE_EXPR: |
2896 | return COMPCODE_LE; |
2897 | case GT_EXPR: |
2898 | return COMPCODE_GT; |
2899 | case NE_EXPR: |
2900 | return COMPCODE_NE; |
2901 | case GE_EXPR: |
2902 | return COMPCODE_GE; |
2903 | case ORDERED_EXPR: |
2904 | return COMPCODE_ORD; |
2905 | case UNORDERED_EXPR: |
2906 | return COMPCODE_UNORD; |
2907 | case UNLT_EXPR: |
2908 | return COMPCODE_UNLT; |
2909 | case UNEQ_EXPR: |
2910 | return COMPCODE_UNEQ; |
2911 | case UNLE_EXPR: |
2912 | return COMPCODE_UNLE; |
2913 | case UNGT_EXPR: |
2914 | return COMPCODE_UNGT; |
2915 | case LTGT_EXPR: |
2916 | return COMPCODE_LTGT; |
2917 | case UNGE_EXPR: |
2918 | return COMPCODE_UNGE; |
2919 | default: |
2920 | gcc_unreachable (); |
2921 | } |
2922 | } |
2923 | |
2924 | /* Convert a compcode bit-based encoding of a comparison operator back |
2925 | to GCC's enum tree_code representation. This function is the |
2926 | inverse of comparison_to_compcode. */ |
2927 | |
2928 | static enum tree_code |
2929 | compcode_to_comparison (enum comparison_code code) |
2930 | { |
2931 | switch (code) |
2932 | { |
2933 | case COMPCODE_LT: |
2934 | return LT_EXPR; |
2935 | case COMPCODE_EQ: |
2936 | return EQ_EXPR; |
2937 | case COMPCODE_LE: |
2938 | return LE_EXPR; |
2939 | case COMPCODE_GT: |
2940 | return GT_EXPR; |
2941 | case COMPCODE_NE: |
2942 | return NE_EXPR; |
2943 | case COMPCODE_GE: |
2944 | return GE_EXPR; |
2945 | case COMPCODE_ORD: |
2946 | return ORDERED_EXPR; |
2947 | case COMPCODE_UNORD: |
2948 | return UNORDERED_EXPR; |
2949 | case COMPCODE_UNLT: |
2950 | return UNLT_EXPR; |
2951 | case COMPCODE_UNEQ: |
2952 | return UNEQ_EXPR; |
2953 | case COMPCODE_UNLE: |
2954 | return UNLE_EXPR; |
2955 | case COMPCODE_UNGT: |
2956 | return UNGT_EXPR; |
2957 | case COMPCODE_LTGT: |
2958 | return LTGT_EXPR; |
2959 | case COMPCODE_UNGE: |
2960 | return UNGE_EXPR; |
2961 | default: |
2962 | gcc_unreachable (); |
2963 | } |
2964 | } |
2965 | |
2966 | /* Return true if COND1 tests the opposite condition of COND2. */ |
2967 | |
2968 | bool |
2969 | inverse_conditions_p (const_tree cond1, const_tree cond2) |
2970 | { |
2971 | return (COMPARISON_CLASS_P (cond1) |
2972 | && COMPARISON_CLASS_P (cond2) |
2973 | && (invert_tree_comparison |
2974 | (TREE_CODE (cond1), |
2975 | honor_nans: HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2)) |
2976 | && operand_equal_p (TREE_OPERAND (cond1, 0), |
2977 | TREE_OPERAND (cond2, 0), flags: 0) |
2978 | && operand_equal_p (TREE_OPERAND (cond1, 1), |
2979 | TREE_OPERAND (cond2, 1), flags: 0)); |
2980 | } |
2981 | |
2982 | /* Return a tree for the comparison which is the combination of |
2983 | doing the AND or OR (depending on CODE) of the two operations LCODE |
2984 | and RCODE on the identical operands LL_ARG and LR_ARG. Take into account |
2985 | the possibility of trapping if the mode has NaNs, and return NULL_TREE |
2986 | if this makes the transformation invalid. */ |
2987 | |
2988 | tree |
2989 | combine_comparisons (location_t loc, |
2990 | enum tree_code code, enum tree_code lcode, |
2991 | enum tree_code rcode, tree truth_type, |
2992 | tree ll_arg, tree lr_arg) |
2993 | { |
2994 | bool honor_nans = HONOR_NANS (ll_arg); |
2995 | enum comparison_code lcompcode = comparison_to_compcode (code: lcode); |
2996 | enum comparison_code rcompcode = comparison_to_compcode (code: rcode); |
2997 | int compcode; |
2998 | |
2999 | switch (code) |
3000 | { |
3001 | case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR: |
3002 | compcode = lcompcode & rcompcode; |
3003 | break; |
3004 | |
3005 | case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR: |
3006 | compcode = lcompcode | rcompcode; |
3007 | break; |
3008 | |
3009 | default: |
3010 | return NULL_TREE; |
3011 | } |
3012 | |
3013 | if (!honor_nans) |
3014 | { |
3015 | /* Eliminate unordered comparisons, as well as LTGT and ORD |
3016 | which are not used unless the mode has NaNs. */ |
3017 | compcode &= ~COMPCODE_UNORD; |
3018 | if (compcode == COMPCODE_LTGT) |
3019 | compcode = COMPCODE_NE; |
3020 | else if (compcode == COMPCODE_ORD) |
3021 | compcode = COMPCODE_TRUE; |
3022 | } |
3023 | else if (flag_trapping_math) |
3024 | { |
3025 | /* Check that the original operation and the optimized ones will trap |
3026 | under the same condition. */ |
3027 | bool ltrap = (lcompcode & COMPCODE_UNORD) == 0 |
3028 | && (lcompcode != COMPCODE_EQ) |
3029 | && (lcompcode != COMPCODE_ORD); |
3030 | bool rtrap = (rcompcode & COMPCODE_UNORD) == 0 |
3031 | && (rcompcode != COMPCODE_EQ) |
3032 | && (rcompcode != COMPCODE_ORD); |
3033 | bool trap = (compcode & COMPCODE_UNORD) == 0 |
3034 | && (compcode != COMPCODE_EQ) |
3035 | && (compcode != COMPCODE_ORD); |
3036 | |
3037 | /* In a short-circuited boolean expression the LHS might be |
3038 | such that the RHS, if evaluated, will never trap. For |
3039 | example, in ORD (x, y) && (x < y), we evaluate the RHS only |
3040 | if neither x nor y is NaN. (This is a mixed blessing: for |
3041 | example, the expression above will never trap, hence |
3042 | optimizing it to x < y would be invalid). */ |
3043 | if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD)) |
3044 | || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD))) |
3045 | rtrap = false; |
3046 | |
3047 | /* If the comparison was short-circuited, and only the RHS |
3048 | trapped, we may now generate a spurious trap. */ |
3049 | if (rtrap && !ltrap |
3050 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) |
3051 | return NULL_TREE; |
3052 | |
3053 | /* If we changed the conditions that cause a trap, we lose. */ |
3054 | if ((ltrap || rtrap) != trap) |
3055 | return NULL_TREE; |
3056 | } |
3057 | |
3058 | if (compcode == COMPCODE_TRUE) |
3059 | return constant_boolean_node (true, truth_type); |
3060 | else if (compcode == COMPCODE_FALSE) |
3061 | return constant_boolean_node (false, truth_type); |
3062 | else |
3063 | { |
3064 | enum tree_code tcode; |
3065 | |
3066 | tcode = compcode_to_comparison (code: (enum comparison_code) compcode); |
3067 | return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg); |
3068 | } |
3069 | } |
3070 | |
3071 | /* Return nonzero if two operands (typically of the same tree node) |
3072 | are necessarily equal. FLAGS modifies behavior as follows: |
3073 | |
3074 | If OEP_ONLY_CONST is set, only return nonzero for constants. |
3075 | This function tests whether the operands are indistinguishable; |
3076 | it does not test whether they are equal using C's == operation. |
3077 | The distinction is important for IEEE floating point, because |
3078 | (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and |
3079 | (2) two NaNs may be indistinguishable, but NaN!=NaN. |
3080 | |
3081 | If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself |
3082 | even though it may hold multiple values during a function. |
3083 | This is because a GCC tree node guarantees that nothing else is |
3084 | executed between the evaluation of its "operands" (which may often |
3085 | be evaluated in arbitrary order). Hence if the operands themselves |
3086 | don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the |
3087 | same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST |
3088 | unset means assuming isochronic (or instantaneous) tree equivalence. |
3089 | Unless comparing arbitrary expression trees, such as from different |
3090 | statements, this flag can usually be left unset. |
3091 | |
3092 | If OEP_PURE_SAME is set, then pure functions with identical arguments |
3093 | are considered the same. It is used when the caller has other ways |
3094 | to ensure that global memory is unchanged in between. |
3095 | |
3096 | If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects, |
3097 | not values of expressions. |
3098 | |
3099 | If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects |
3100 | such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs. |
3101 | |
3102 | If OEP_BITWISE is set, then require the values to be bitwise identical |
3103 | rather than simply numerically equal. Do not take advantage of things |
3104 | like math-related flags or undefined behavior; only return true for |
3105 | values that are provably bitwise identical in all circumstances. |
3106 | |
3107 | Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on |
3108 | any operand with side effect. This is unnecesarily conservative in the |
3109 | case we know that arg0 and arg1 are in disjoint code paths (such as in |
3110 | ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing |
3111 | addresses with TREE_CONSTANT flag set so we know that &var == &var |
3112 | even if var is volatile. */ |
3113 | |
3114 | bool |
3115 | operand_compare::operand_equal_p (const_tree arg0, const_tree arg1, |
3116 | unsigned int flags) |
3117 | { |
3118 | bool r; |
3119 | if (verify_hash_value (arg0, arg1, flags, ret: &r)) |
3120 | return r; |
3121 | |
3122 | STRIP_ANY_LOCATION_WRAPPER (arg0); |
3123 | STRIP_ANY_LOCATION_WRAPPER (arg1); |
3124 | |
3125 | /* If either is ERROR_MARK, they aren't equal. */ |
3126 | if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK |
3127 | || TREE_TYPE (arg0) == error_mark_node |
3128 | || TREE_TYPE (arg1) == error_mark_node) |
3129 | return false; |
3130 | |
3131 | /* Similar, if either does not have a type (like a template id), |
3132 | they aren't equal. */ |
3133 | if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1)) |
3134 | return false; |
3135 | |
3136 | /* Bitwise identity makes no sense if the values have different layouts. */ |
3137 | if ((flags & OEP_BITWISE) |
3138 | && !tree_nop_conversion_p (TREE_TYPE (arg0), TREE_TYPE (arg1))) |
3139 | return false; |
3140 | |
3141 | /* We cannot consider pointers to different address space equal. */ |
3142 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) |
3143 | && POINTER_TYPE_P (TREE_TYPE (arg1)) |
3144 | && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))) |
3145 | != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1))))) |
3146 | return false; |
3147 | |
3148 | /* Check equality of integer constants before bailing out due to |
3149 | precision differences. */ |
3150 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) |
3151 | { |
3152 | /* Address of INTEGER_CST is not defined; check that we did not forget |
3153 | to drop the OEP_ADDRESS_OF flags. */ |
3154 | gcc_checking_assert (!(flags & OEP_ADDRESS_OF)); |
3155 | return tree_int_cst_equal (arg0, arg1); |
3156 | } |
3157 | |
3158 | if (!(flags & OEP_ADDRESS_OF)) |
3159 | { |
3160 | /* If both types don't have the same signedness, then we can't consider |
3161 | them equal. We must check this before the STRIP_NOPS calls |
3162 | because they may change the signedness of the arguments. As pointers |
3163 | strictly don't have a signedness, require either two pointers or |
3164 | two non-pointers as well. */ |
3165 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)) |
3166 | || POINTER_TYPE_P (TREE_TYPE (arg0)) |
3167 | != POINTER_TYPE_P (TREE_TYPE (arg1))) |
3168 | return false; |
3169 | |
3170 | /* If both types don't have the same precision, then it is not safe |
3171 | to strip NOPs. */ |
3172 | if (element_precision (TREE_TYPE (arg0)) |
3173 | != element_precision (TREE_TYPE (arg1))) |
3174 | return false; |
3175 | |
3176 | STRIP_NOPS (arg0); |
3177 | STRIP_NOPS (arg1); |
3178 | } |
3179 | #if 0 |
3180 | /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the |
3181 | sanity check once the issue is solved. */ |
3182 | else |
3183 | /* Addresses of conversions and SSA_NAMEs (and many other things) |
3184 | are not defined. Check that we did not forget to drop the |
3185 | OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */ |
3186 | gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1) |
3187 | && TREE_CODE (arg0) != SSA_NAME); |
3188 | #endif |
3189 | |
3190 | /* In case both args are comparisons but with different comparison |
3191 | code, try to swap the comparison operands of one arg to produce |
3192 | a match and compare that variant. */ |
3193 | if (TREE_CODE (arg0) != TREE_CODE (arg1) |
3194 | && COMPARISON_CLASS_P (arg0) |
3195 | && COMPARISON_CLASS_P (arg1)) |
3196 | { |
3197 | enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1)); |
3198 | |
3199 | if (TREE_CODE (arg0) == swap_code) |
3200 | return operand_equal_p (TREE_OPERAND (arg0, 0), |
3201 | TREE_OPERAND (arg1, 1), flags) |
3202 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
3203 | TREE_OPERAND (arg1, 0), flags); |
3204 | } |
3205 | |
3206 | if (TREE_CODE (arg0) != TREE_CODE (arg1)) |
3207 | { |
3208 | /* NOP_EXPR and CONVERT_EXPR are considered equal. */ |
3209 | if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)) |
3210 | ; |
3211 | else if (flags & OEP_ADDRESS_OF) |
3212 | { |
3213 | /* If we are interested in comparing addresses ignore |
3214 | MEM_REF wrappings of the base that can appear just for |
3215 | TBAA reasons. */ |
3216 | if (TREE_CODE (arg0) == MEM_REF |
3217 | && DECL_P (arg1) |
3218 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR |
3219 | && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1 |
3220 | && integer_zerop (TREE_OPERAND (arg0, 1))) |
3221 | return true; |
3222 | else if (TREE_CODE (arg1) == MEM_REF |
3223 | && DECL_P (arg0) |
3224 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR |
3225 | && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0 |
3226 | && integer_zerop (TREE_OPERAND (arg1, 1))) |
3227 | return true; |
3228 | return false; |
3229 | } |
3230 | else |
3231 | return false; |
3232 | } |
3233 | |
3234 | /* When not checking adddresses, this is needed for conversions and for |
3235 | COMPONENT_REF. Might as well play it safe and always test this. */ |
3236 | if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK |
3237 | || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK |
3238 | || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)) |
3239 | && !(flags & OEP_ADDRESS_OF))) |
3240 | return false; |
3241 | |
3242 | /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal. |
3243 | We don't care about side effects in that case because the SAVE_EXPR |
3244 | takes care of that for us. In all other cases, two expressions are |
3245 | equal if they have no side effects. If we have two identical |
3246 | expressions with side effects that should be treated the same due |
3247 | to the only side effects being identical SAVE_EXPR's, that will |
3248 | be detected in the recursive calls below. |
3249 | If we are taking an invariant address of two identical objects |
3250 | they are necessarily equal as well. */ |
3251 | if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST) |
3252 | && (TREE_CODE (arg0) == SAVE_EXPR |
3253 | || (flags & OEP_MATCH_SIDE_EFFECTS) |
3254 | || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1)))) |
3255 | return true; |
3256 | |
3257 | /* Next handle constant cases, those for which we can return 1 even |
3258 | if ONLY_CONST is set. */ |
3259 | if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)) |
3260 | switch (TREE_CODE (arg0)) |
3261 | { |
3262 | case INTEGER_CST: |
3263 | return tree_int_cst_equal (arg0, arg1); |
3264 | |
3265 | case FIXED_CST: |
3266 | return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0), |
3267 | TREE_FIXED_CST (arg1)); |
3268 | |
3269 | case REAL_CST: |
3270 | if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1))) |
3271 | return true; |
3272 | |
3273 | if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0)) |
3274 | { |
3275 | /* If we do not distinguish between signed and unsigned zero, |
3276 | consider them equal. */ |
3277 | if (real_zerop (arg0) && real_zerop (arg1)) |
3278 | return true; |
3279 | } |
3280 | return false; |
3281 | |
3282 | case VECTOR_CST: |
3283 | { |
3284 | if (VECTOR_CST_LOG2_NPATTERNS (arg0) |
3285 | != VECTOR_CST_LOG2_NPATTERNS (arg1)) |
3286 | return false; |
3287 | |
3288 | if (VECTOR_CST_NELTS_PER_PATTERN (arg0) |
3289 | != VECTOR_CST_NELTS_PER_PATTERN (arg1)) |
3290 | return false; |
3291 | |
3292 | unsigned int count = vector_cst_encoded_nelts (t: arg0); |
3293 | for (unsigned int i = 0; i < count; ++i) |
3294 | if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i), |
3295 | VECTOR_CST_ENCODED_ELT (arg1, i), flags)) |
3296 | return false; |
3297 | return true; |
3298 | } |
3299 | |
3300 | case COMPLEX_CST: |
3301 | return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1), |
3302 | flags) |
3303 | && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1), |
3304 | flags)); |
3305 | |
3306 | case STRING_CST: |
3307 | return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1) |
3308 | && ! memcmp (TREE_STRING_POINTER (arg0), |
3309 | TREE_STRING_POINTER (arg1), |
3310 | TREE_STRING_LENGTH (arg0))); |
3311 | |
3312 | case ADDR_EXPR: |
3313 | gcc_checking_assert (!(flags & OEP_ADDRESS_OF)); |
3314 | return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), |
3315 | flags: flags | OEP_ADDRESS_OF |
3316 | | OEP_MATCH_SIDE_EFFECTS); |
3317 | case CONSTRUCTOR: |
3318 | /* In GIMPLE empty constructors are allowed in initializers of |
3319 | aggregates. */ |
3320 | return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1); |
3321 | default: |
3322 | break; |
3323 | } |
3324 | |
3325 | /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that |
3326 | two instances of undefined behavior will give identical results. */ |
3327 | if (flags & (OEP_ONLY_CONST | OEP_BITWISE)) |
3328 | return false; |
3329 | |
3330 | /* Define macros to test an operand from arg0 and arg1 for equality and a |
3331 | variant that allows null and views null as being different from any |
3332 | non-null value. In the latter case, if either is null, the both |
3333 | must be; otherwise, do the normal comparison. */ |
3334 | #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \ |
3335 | TREE_OPERAND (arg1, N), flags) |
3336 | |
3337 | #define OP_SAME_WITH_NULL(N) \ |
3338 | ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \ |
3339 | ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N)) |
3340 | |
3341 | switch (TREE_CODE_CLASS (TREE_CODE (arg0))) |
3342 | { |
3343 | case tcc_unary: |
3344 | /* Two conversions are equal only if signedness and modes match. */ |
3345 | switch (TREE_CODE (arg0)) |
3346 | { |
3347 | CASE_CONVERT: |
3348 | case FIX_TRUNC_EXPR: |
3349 | if (TYPE_UNSIGNED (TREE_TYPE (arg0)) |
3350 | != TYPE_UNSIGNED (TREE_TYPE (arg1))) |
3351 | return false; |
3352 | break; |
3353 | default: |
3354 | break; |
3355 | } |
3356 | |
3357 | return OP_SAME (0); |
3358 | |
3359 | |
3360 | case tcc_comparison: |
3361 | case tcc_binary: |
3362 | if (OP_SAME (0) && OP_SAME (1)) |
3363 | return true; |
3364 | |
3365 | /* For commutative ops, allow the other order. */ |
3366 | return (commutative_tree_code (TREE_CODE (arg0)) |
3367 | && operand_equal_p (TREE_OPERAND (arg0, 0), |
3368 | TREE_OPERAND (arg1, 1), flags) |
3369 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
3370 | TREE_OPERAND (arg1, 0), flags)); |
3371 | |
3372 | case tcc_reference: |
3373 | /* If either of the pointer (or reference) expressions we are |
3374 | dereferencing contain a side effect, these cannot be equal, |
3375 | but their addresses can be. */ |
3376 | if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0 |
3377 | && (TREE_SIDE_EFFECTS (arg0) |
3378 | || TREE_SIDE_EFFECTS (arg1))) |
3379 | return false; |
3380 | |
3381 | switch (TREE_CODE (arg0)) |
3382 | { |
3383 | case INDIRECT_REF: |
3384 | if (!(flags & OEP_ADDRESS_OF)) |
3385 | { |
3386 | if (TYPE_ALIGN (TREE_TYPE (arg0)) |
3387 | != TYPE_ALIGN (TREE_TYPE (arg1))) |
3388 | return false; |
3389 | /* Verify that the access types are compatible. */ |
3390 | if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0)) |
3391 | != TYPE_MAIN_VARIANT (TREE_TYPE (arg1))) |
3392 | return false; |
3393 | } |
3394 | flags &= ~OEP_ADDRESS_OF; |
3395 | return OP_SAME (0); |
3396 | |
3397 | case IMAGPART_EXPR: |
3398 | /* Require the same offset. */ |
3399 | if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)), |
3400 | TYPE_SIZE (TREE_TYPE (arg1)), |
3401 | flags: flags & ~OEP_ADDRESS_OF)) |
3402 | return false; |
3403 | |
3404 | /* Fallthru. */ |
3405 | case REALPART_EXPR: |
3406 | case VIEW_CONVERT_EXPR: |
3407 | return OP_SAME (0); |
3408 | |
3409 | case TARGET_MEM_REF: |
3410 | case MEM_REF: |
3411 | if (!(flags & OEP_ADDRESS_OF)) |
3412 | { |
3413 | /* Require equal access sizes */ |
3414 | if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1)) |
3415 | && (!TYPE_SIZE (TREE_TYPE (arg0)) |
3416 | || !TYPE_SIZE (TREE_TYPE (arg1)) |
3417 | || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)), |
3418 | TYPE_SIZE (TREE_TYPE (arg1)), |
3419 | flags))) |
3420 | return false; |
3421 | /* Verify that access happens in similar types. */ |
3422 | if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))) |
3423 | return false; |
3424 | /* Verify that accesses are TBAA compatible. */ |
3425 | if (!alias_ptr_types_compatible_p |
3426 | (TREE_TYPE (TREE_OPERAND (arg0, 1)), |
3427 | TREE_TYPE (TREE_OPERAND (arg1, 1))) |
3428 | || (MR_DEPENDENCE_CLIQUE (arg0) |
3429 | != MR_DEPENDENCE_CLIQUE (arg1)) |
3430 | || (MR_DEPENDENCE_BASE (arg0) |
3431 | != MR_DEPENDENCE_BASE (arg1))) |
3432 | return false; |
3433 | /* Verify that alignment is compatible. */ |
3434 | if (TYPE_ALIGN (TREE_TYPE (arg0)) |
3435 | != TYPE_ALIGN (TREE_TYPE (arg1))) |
3436 | return false; |
3437 | } |
3438 | flags &= ~OEP_ADDRESS_OF; |
3439 | return (OP_SAME (0) && OP_SAME (1) |
3440 | /* TARGET_MEM_REF require equal extra operands. */ |
3441 | && (TREE_CODE (arg0) != TARGET_MEM_REF |
3442 | || (OP_SAME_WITH_NULL (2) |
3443 | && OP_SAME_WITH_NULL (3) |
3444 | && OP_SAME_WITH_NULL (4)))); |
3445 | |
3446 | case ARRAY_REF: |
3447 | case ARRAY_RANGE_REF: |
3448 | if (!OP_SAME (0)) |
3449 | return false; |
3450 | flags &= ~OEP_ADDRESS_OF; |
3451 | /* Compare the array index by value if it is constant first as we |
3452 | may have different types but same value here. */ |
3453 | return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1), |
3454 | TREE_OPERAND (arg1, 1)) |
3455 | || OP_SAME (1)) |
3456 | && OP_SAME_WITH_NULL (2) |
3457 | && OP_SAME_WITH_NULL (3) |
3458 | /* Compare low bound and element size as with OEP_ADDRESS_OF |
3459 | we have to account for the offset of the ref. */ |
3460 | && (TREE_TYPE (TREE_OPERAND (arg0, 0)) |
3461 | == TREE_TYPE (TREE_OPERAND (arg1, 0)) |
3462 | || (operand_equal_p (arg0: array_ref_low_bound |
3463 | (CONST_CAST_TREE (arg0)), |
3464 | arg1: array_ref_low_bound |
3465 | (CONST_CAST_TREE (arg1)), flags) |
3466 | && operand_equal_p (arg0: array_ref_element_size |
3467 | (CONST_CAST_TREE (arg0)), |
3468 | arg1: array_ref_element_size |
3469 | (CONST_CAST_TREE (arg1)), |
3470 | flags)))); |
3471 | |
3472 | case COMPONENT_REF: |
3473 | /* Handle operand 2 the same as for ARRAY_REF. Operand 0 |
3474 | may be NULL when we're called to compare MEM_EXPRs. */ |
3475 | if (!OP_SAME_WITH_NULL (0)) |
3476 | return false; |
3477 | { |
3478 | bool compare_address = flags & OEP_ADDRESS_OF; |
3479 | |
3480 | /* Most of time we only need to compare FIELD_DECLs for equality. |
3481 | However when determining address look into actual offsets. |
3482 | These may match for unions and unshared record types. */ |
3483 | flags &= ~OEP_ADDRESS_OF; |
3484 | if (!OP_SAME (1)) |
3485 | { |
3486 | if (compare_address |
3487 | && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0) |
3488 | { |
3489 | tree field0 = TREE_OPERAND (arg0, 1); |
3490 | tree field1 = TREE_OPERAND (arg1, 1); |
3491 | |
3492 | /* Non-FIELD_DECL operands can appear in C++ templates. */ |
3493 | if (TREE_CODE (field0) != FIELD_DECL |
3494 | || TREE_CODE (field1) != FIELD_DECL |
3495 | || !operand_equal_p (DECL_FIELD_OFFSET (field0), |
3496 | DECL_FIELD_OFFSET (field1), flags) |
3497 | || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0), |
3498 | DECL_FIELD_BIT_OFFSET (field1), |
3499 | flags)) |
3500 | return false; |
3501 | } |
3502 | else |
3503 | return false; |
3504 | } |
3505 | } |
3506 | return OP_SAME_WITH_NULL (2); |
3507 | |
3508 | case BIT_FIELD_REF: |
3509 | if (!OP_SAME (0)) |
3510 | return false; |
3511 | flags &= ~OEP_ADDRESS_OF; |
3512 | return OP_SAME (1) && OP_SAME (2); |
3513 | |
3514 | default: |
3515 | return false; |
3516 | } |
3517 | |
3518 | case tcc_expression: |
3519 | switch (TREE_CODE (arg0)) |
3520 | { |
3521 | case ADDR_EXPR: |
3522 | /* Be sure we pass right ADDRESS_OF flag. */ |
3523 | gcc_checking_assert (!(flags & OEP_ADDRESS_OF)); |
3524 | return operand_equal_p (TREE_OPERAND (arg0, 0), |
3525 | TREE_OPERAND (arg1, 0), |
3526 | flags: flags | OEP_ADDRESS_OF); |
3527 | |
3528 | case TRUTH_NOT_EXPR: |
3529 | return OP_SAME (0); |
3530 | |
3531 | case TRUTH_ANDIF_EXPR: |
3532 | case TRUTH_ORIF_EXPR: |
3533 | return OP_SAME (0) && OP_SAME (1); |
3534 | |
3535 | case WIDEN_MULT_PLUS_EXPR: |
3536 | case WIDEN_MULT_MINUS_EXPR: |
3537 | if (!OP_SAME (2)) |
3538 | return false; |
3539 | /* The multiplcation operands are commutative. */ |
3540 | /* FALLTHRU */ |
3541 | |
3542 | case TRUTH_AND_EXPR: |
3543 | case TRUTH_OR_EXPR: |
3544 | case TRUTH_XOR_EXPR: |
3545 | if (OP_SAME (0) && OP_SAME (1)) |
3546 | return true; |
3547 | |
3548 | /* Otherwise take into account this is a commutative operation. */ |
3549 | return (operand_equal_p (TREE_OPERAND (arg0, 0), |
3550 | TREE_OPERAND (arg1, 1), flags) |
3551 | && operand_equal_p (TREE_OPERAND (arg0, 1), |
3552 | TREE_OPERAND (arg1, 0), flags)); |
3553 | |
3554 | case COND_EXPR: |
3555 | if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2)) |
3556 | return false; |
3557 | flags &= ~OEP_ADDRESS_OF; |
3558 | return OP_SAME (0); |
3559 | |
3560 | case BIT_INSERT_EXPR: |
3561 | /* BIT_INSERT_EXPR has an implict operand as the type precision |
3562 | of op1. Need to check to make sure they are the same. */ |
3563 | if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
3564 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST |
3565 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1))) |
3566 | != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1)))) |
3567 | return false; |
3568 | /* FALLTHRU */ |
3569 | |
3570 | case VEC_COND_EXPR: |
3571 | case DOT_PROD_EXPR: |
3572 | return OP_SAME (0) && OP_SAME (1) && OP_SAME (2); |
3573 | |
3574 | case MODIFY_EXPR: |
3575 | case INIT_EXPR: |
3576 | case COMPOUND_EXPR: |
3577 | case PREDECREMENT_EXPR: |
3578 | case PREINCREMENT_EXPR: |
3579 | case POSTDECREMENT_EXPR: |
3580 | case POSTINCREMENT_EXPR: |
3581 | if (flags & OEP_LEXICOGRAPHIC) |
3582 | return OP_SAME (0) && OP_SAME (1); |
3583 | return false; |
3584 | |
3585 | case CLEANUP_POINT_EXPR: |
3586 | case EXPR_STMT: |
3587 | case SAVE_EXPR: |
3588 | if (flags & OEP_LEXICOGRAPHIC) |
3589 | return OP_SAME (0); |
3590 | return false; |
3591 | |
3592 | case OBJ_TYPE_REF: |
3593 | /* Virtual table reference. */ |
3594 | if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0), |
3595 | OBJ_TYPE_REF_EXPR (arg1), flags)) |
3596 | return false; |
3597 | flags &= ~OEP_ADDRESS_OF; |
3598 | if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0)) |
3599 | != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1))) |
3600 | return false; |
3601 | if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0), |
3602 | OBJ_TYPE_REF_OBJECT (arg1), flags)) |
3603 | return false; |
3604 | if (virtual_method_call_p (arg0)) |
3605 | { |
3606 | if (!virtual_method_call_p (arg1)) |
3607 | return false; |
3608 | return types_same_for_odr (type1: obj_type_ref_class (ref: arg0), |
3609 | type2: obj_type_ref_class (ref: arg1)); |
3610 | } |
3611 | return false; |
3612 | |
3613 | default: |
3614 | return false; |
3615 | } |
3616 | |
3617 | case tcc_vl_exp: |
3618 | switch (TREE_CODE (arg0)) |
3619 | { |
3620 | case CALL_EXPR: |
3621 | if ((CALL_EXPR_FN (arg0) == NULL_TREE) |
3622 | != (CALL_EXPR_FN (arg1) == NULL_TREE)) |
3623 | /* If not both CALL_EXPRs are either internal or normal function |
3624 | functions, then they are not equal. */ |
3625 | return false; |
3626 | else if (CALL_EXPR_FN (arg0) == NULL_TREE) |
3627 | { |
3628 | /* If the CALL_EXPRs call different internal functions, then they |
3629 | are not equal. */ |
3630 | if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1)) |
3631 | return false; |
3632 | } |
3633 | else |
3634 | { |
3635 | /* If the CALL_EXPRs call different functions, then they are not |
3636 | equal. */ |
3637 | if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1), |
3638 | flags)) |
3639 | return false; |
3640 | } |
3641 | |
3642 | /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */ |
3643 | { |
3644 | unsigned int cef = call_expr_flags (arg0); |
3645 | if (flags & OEP_PURE_SAME) |
3646 | cef &= ECF_CONST | ECF_PURE; |
3647 | else |
3648 | cef &= ECF_CONST; |
3649 | if (!cef && !(flags & OEP_LEXICOGRAPHIC)) |
3650 | return false; |
3651 | } |
3652 | |
3653 | /* Now see if all the arguments are the same. */ |
3654 | { |
3655 | const_call_expr_arg_iterator iter0, iter1; |
3656 | const_tree a0, a1; |
3657 | for (a0 = first_const_call_expr_arg (exp: arg0, iter: &iter0), |
3658 | a1 = first_const_call_expr_arg (exp: arg1, iter: &iter1); |
3659 | a0 && a1; |
3660 | a0 = next_const_call_expr_arg (iter: &iter0), |
3661 | a1 = next_const_call_expr_arg (iter: &iter1)) |
3662 | if (! operand_equal_p (arg0: a0, arg1: a1, flags)) |
3663 | return false; |
3664 | |
3665 | /* If we get here and both argument lists are exhausted |
3666 | then the CALL_EXPRs are equal. */ |
3667 | return ! (a0 || a1); |
3668 | } |
3669 | default: |
3670 | return false; |
3671 | } |
3672 | |
3673 | case tcc_declaration: |
3674 | /* Consider __builtin_sqrt equal to sqrt. */ |
3675 | if (TREE_CODE (arg0) == FUNCTION_DECL) |
3676 | return (fndecl_built_in_p (node: arg0) && fndecl_built_in_p (node: arg1) |
3677 | && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1) |
3678 | && (DECL_UNCHECKED_FUNCTION_CODE (arg0) |
3679 | == DECL_UNCHECKED_FUNCTION_CODE (arg1))); |
3680 | |
3681 | if (DECL_P (arg0) |
3682 | && (flags & OEP_DECL_NAME) |
3683 | && (flags & OEP_LEXICOGRAPHIC)) |
3684 | { |
3685 | /* Consider decls with the same name equal. The caller needs |
3686 | to make sure they refer to the same entity (such as a function |
3687 | formal parameter). */ |
3688 | tree a0name = DECL_NAME (arg0); |
3689 | tree a1name = DECL_NAME (arg1); |
3690 | const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL; |
3691 | const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL; |
3692 | return a0ns && a1ns && strcmp (s1: a0ns, s2: a1ns) == 0; |
3693 | } |
3694 | return false; |
3695 | |
3696 | case tcc_exceptional: |
3697 | if (TREE_CODE (arg0) == CONSTRUCTOR) |
3698 | { |
3699 | if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1)) |
3700 | return false; |
3701 | |
3702 | /* In GIMPLE constructors are used only to build vectors from |
3703 | elements. Individual elements in the constructor must be |
3704 | indexed in increasing order and form an initial sequence. |
3705 | |
3706 | We make no effort to compare constructors in generic. |
3707 | (see sem_variable::equals in ipa-icf which can do so for |
3708 | constants). */ |
3709 | if (!VECTOR_TYPE_P (TREE_TYPE (arg0)) |
3710 | || !VECTOR_TYPE_P (TREE_TYPE (arg1))) |
3711 | return false; |
3712 | |
3713 | /* Be sure that vectors constructed have the same representation. |
3714 | We only tested element precision and modes to match. |
3715 | Vectors may be BLKmode and thus also check that the number of |
3716 | parts match. */ |
3717 | if (maybe_ne (a: TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), |
3718 | b: TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))) |
3719 | return false; |
3720 | |
3721 | vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0); |
3722 | vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1); |
3723 | unsigned int len = vec_safe_length (v: v0); |
3724 | |
3725 | if (len != vec_safe_length (v: v1)) |
3726 | return false; |
3727 | |
3728 | for (unsigned int i = 0; i < len; i++) |
3729 | { |
3730 | constructor_elt *c0 = &(*v0)[i]; |
3731 | constructor_elt *c1 = &(*v1)[i]; |
3732 | |
3733 | if (!operand_equal_p (arg0: c0->value, arg1: c1->value, flags) |
3734 | /* In GIMPLE the indexes can be either NULL or matching i. |
3735 | Double check this so we won't get false |
3736 | positives for GENERIC. */ |
3737 | || (c0->index |
3738 | && (TREE_CODE (c0->index) != INTEGER_CST |
3739 | || compare_tree_int (c0->index, i))) |
3740 | || (c1->index |
3741 | && (TREE_CODE (c1->index) != INTEGER_CST |
3742 | || compare_tree_int (c1->index, i)))) |
3743 | return false; |
3744 | } |
3745 | return true; |
3746 | } |
3747 | else if (TREE_CODE (arg0) == STATEMENT_LIST |
3748 | && (flags & OEP_LEXICOGRAPHIC)) |
3749 | { |
3750 | /* Compare the STATEMENT_LISTs. */ |
3751 | tree_stmt_iterator tsi1, tsi2; |
3752 | tree body1 = CONST_CAST_TREE (arg0); |
3753 | tree body2 = CONST_CAST_TREE (arg1); |
3754 | for (tsi1 = tsi_start (t: body1), tsi2 = tsi_start (t: body2); ; |
3755 | tsi_next (i: &tsi1), tsi_next (i: &tsi2)) |
3756 | { |
3757 | /* The lists don't have the same number of statements. */ |
3758 | if (tsi_end_p (i: tsi1) ^ tsi_end_p (i: tsi2)) |
3759 | return false; |
3760 | if (tsi_end_p (i: tsi1) && tsi_end_p (i: tsi2)) |
3761 | return true; |
3762 | if (!operand_equal_p (arg0: tsi_stmt (i: tsi1), arg1: tsi_stmt (i: tsi2), |
3763 | flags: flags & (OEP_LEXICOGRAPHIC |
3764 | | OEP_NO_HASH_CHECK))) |
3765 | return false; |
3766 | } |
3767 | } |
3768 | return false; |
3769 | |
3770 | case tcc_statement: |
3771 | switch (TREE_CODE (arg0)) |
3772 | { |
3773 | case RETURN_EXPR: |
3774 | if (flags & OEP_LEXICOGRAPHIC) |
3775 | return OP_SAME_WITH_NULL (0); |
3776 | return false; |
3777 | case DEBUG_BEGIN_STMT: |
3778 | if (flags & OEP_LEXICOGRAPHIC) |
3779 | return true; |
3780 | return false; |
3781 | default: |
3782 | return false; |
3783 | } |
3784 | |
3785 | default: |
3786 | return false; |
3787 | } |
3788 | |
3789 | #undef OP_SAME |
3790 | #undef OP_SAME_WITH_NULL |
3791 | } |
3792 | |
3793 | /* Generate a hash value for an expression. This can be used iteratively |
3794 | by passing a previous result as the HSTATE argument. */ |
3795 | |
3796 | void |
3797 | operand_compare::hash_operand (const_tree t, inchash::hash &hstate, |
3798 | unsigned int flags) |
3799 | { |
3800 | int i; |
3801 | enum tree_code code; |
3802 | enum tree_code_class tclass; |
3803 | |
3804 | if (t == NULL_TREE || t == error_mark_node) |
3805 | { |
3806 | hstate.merge_hash (other: 0); |
3807 | return; |
3808 | } |
3809 | |
3810 | STRIP_ANY_LOCATION_WRAPPER (t); |
3811 | |
3812 | if (!(flags & OEP_ADDRESS_OF)) |
3813 | STRIP_NOPS (t); |
3814 | |
3815 | code = TREE_CODE (t); |
3816 | |
3817 | switch (code) |
3818 | { |
3819 | /* Alas, constants aren't shared, so we can't rely on pointer |
3820 | identity. */ |
3821 | case VOID_CST: |
3822 | hstate.merge_hash (other: 0); |
3823 | return; |
3824 | case INTEGER_CST: |
3825 | gcc_checking_assert (!(flags & OEP_ADDRESS_OF)); |
3826 | for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++) |
3827 | hstate.add_hwi (TREE_INT_CST_ELT (t, i)); |
3828 | return; |
3829 | case REAL_CST: |
3830 | { |
3831 | unsigned int val2; |
3832 | if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t)) |
3833 | val2 = rvc_zero; |
3834 | else |
3835 | val2 = real_hash (TREE_REAL_CST_PTR (t)); |
3836 | hstate.merge_hash (other: val2); |
3837 | return; |
3838 | } |
3839 | case FIXED_CST: |
3840 | { |
3841 | unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t)); |
3842 | hstate.merge_hash (other: val2); |
3843 | return; |
3844 | } |
3845 | case STRING_CST: |
3846 | hstate.add (data: (const void *) TREE_STRING_POINTER (t), |
3847 | TREE_STRING_LENGTH (t)); |
3848 | return; |
3849 | case COMPLEX_CST: |
3850 | hash_operand (TREE_REALPART (t), hstate, flags); |
3851 | hash_operand (TREE_IMAGPART (t), hstate, flags); |
3852 | return; |
3853 | case VECTOR_CST: |
3854 | { |
3855 | hstate.add_int (VECTOR_CST_NPATTERNS (t)); |
3856 | hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t)); |
3857 | unsigned int count = vector_cst_encoded_nelts (t); |
3858 | for (unsigned int i = 0; i < count; ++i) |
3859 | hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags); |
3860 | return; |
3861 | } |
3862 | case SSA_NAME: |
3863 | /* We can just compare by pointer. */ |
3864 | hstate.add_hwi (SSA_NAME_VERSION (t)); |
3865 | return; |
3866 | case PLACEHOLDER_EXPR: |
3867 | /* The node itself doesn't matter. */ |
3868 | return; |
3869 | case BLOCK: |
3870 | case OMP_CLAUSE: |
3871 | /* Ignore. */ |
3872 | return; |
3873 | case TREE_LIST: |
3874 | /* A list of expressions, for a CALL_EXPR or as the elements of a |
3875 | VECTOR_CST. */ |
3876 | for (; t; t = TREE_CHAIN (t)) |
3877 | hash_operand (TREE_VALUE (t), hstate, flags); |
3878 | return; |
3879 | case CONSTRUCTOR: |
3880 | { |
3881 | unsigned HOST_WIDE_INT idx; |
3882 | tree field, value; |
3883 | flags &= ~OEP_ADDRESS_OF; |
3884 | hstate.add_int (CONSTRUCTOR_NO_CLEARING (t)); |
3885 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value) |
3886 | { |
3887 | /* In GIMPLE the indexes can be either NULL or matching i. */ |
3888 | if (field == NULL_TREE) |
3889 | field = bitsize_int (idx); |
3890 | hash_operand (t: field, hstate, flags); |
3891 | hash_operand (t: value, hstate, flags); |
3892 | } |
3893 | return; |
3894 | } |
3895 | case STATEMENT_LIST: |
3896 | { |
3897 | tree_stmt_iterator i; |
3898 | for (i = tsi_start (CONST_CAST_TREE (t)); |
3899 | !tsi_end_p (i); tsi_next (i: &i)) |
3900 | hash_operand (t: tsi_stmt (i), hstate, flags); |
3901 | return; |
3902 | } |
3903 | case TREE_VEC: |
3904 | for (i = 0; i < TREE_VEC_LENGTH (t); ++i) |
3905 | hash_operand (TREE_VEC_ELT (t, i), hstate, flags); |
3906 | return; |
3907 | case IDENTIFIER_NODE: |
3908 | hstate.add_object (IDENTIFIER_HASH_VALUE (t)); |
3909 | return; |
3910 | case FUNCTION_DECL: |
3911 | /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form. |
3912 | Otherwise nodes that compare equal according to operand_equal_p might |
3913 | get different hash codes. However, don't do this for machine specific |
3914 | or front end builtins, since the function code is overloaded in those |
3915 | cases. */ |
3916 | if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL |
3917 | && builtin_decl_explicit_p (fncode: DECL_FUNCTION_CODE (decl: t))) |
3918 | { |
3919 | t = builtin_decl_explicit (fncode: DECL_FUNCTION_CODE (decl: t)); |
3920 | code = TREE_CODE (t); |
3921 | } |
3922 | /* FALL THROUGH */ |
3923 | default: |
3924 | if (POLY_INT_CST_P (t)) |
3925 | { |
3926 | for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i) |
3927 | hstate.add_wide_int (x: wi::to_wide (POLY_INT_CST_COEFF (t, i))); |
3928 | return; |
3929 | } |
3930 | tclass = TREE_CODE_CLASS (code); |
3931 | |
3932 | if (tclass == tcc_declaration) |
3933 | { |
3934 | /* DECL's have a unique ID */ |
3935 | hstate.add_hwi (DECL_UID (t)); |
3936 | } |
3937 | else if (tclass == tcc_comparison && !commutative_tree_code (code)) |
3938 | { |
3939 | /* For comparisons that can be swapped, use the lower |
3940 | tree code. */ |
3941 | enum tree_code ccode = swap_tree_comparison (code); |
3942 | if (code < ccode) |
3943 | ccode = code; |
3944 | hstate.add_object (obj&: ccode); |
3945 | hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags); |
3946 | hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags); |
3947 | } |
3948 | else if (CONVERT_EXPR_CODE_P (code)) |
3949 | { |
3950 | /* NOP_EXPR and CONVERT_EXPR are considered equal by |
3951 | operand_equal_p. */ |
3952 | enum tree_code ccode = NOP_EXPR; |
3953 | hstate.add_object (obj&: ccode); |
3954 | |
3955 | /* Don't hash the type, that can lead to having nodes which |
3956 | compare equal according to operand_equal_p, but which |
3957 | have different hash codes. Make sure to include signedness |
3958 | in the hash computation. */ |
3959 | hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t))); |
3960 | hash_operand (TREE_OPERAND (t, 0), hstate, flags); |
3961 | } |
3962 | /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */ |
3963 | else if (code == MEM_REF |
3964 | && (flags & OEP_ADDRESS_OF) != 0 |
3965 | && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR |
3966 | && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) |
3967 | && integer_zerop (TREE_OPERAND (t, 1))) |
3968 | hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0), |
3969 | hstate, flags); |
3970 | /* Don't ICE on FE specific trees, or their arguments etc. |
3971 | during operand_equal_p hash verification. */ |
3972 | else if (!IS_EXPR_CODE_CLASS (tclass)) |
3973 | gcc_assert (flags & OEP_HASH_CHECK); |
3974 | else |
3975 | { |
3976 | unsigned int sflags = flags; |
3977 | |
3978 | hstate.add_object (obj&: code); |
3979 | |
3980 | switch (code) |
3981 | { |
3982 | case ADDR_EXPR: |
3983 | gcc_checking_assert (!(flags & OEP_ADDRESS_OF)); |
3984 | flags |= OEP_ADDRESS_OF; |
3985 | sflags = flags; |
3986 | break; |
3987 | |
3988 | case INDIRECT_REF: |
3989 | case MEM_REF: |
3990 | case TARGET_MEM_REF: |
3991 | flags &= ~OEP_ADDRESS_OF; |
3992 | sflags = flags; |
3993 | break; |
3994 | |
3995 | case COMPONENT_REF: |
3996 | if (sflags & OEP_ADDRESS_OF) |
3997 | { |
3998 | hash_operand (TREE_OPERAND (t, 0), hstate, flags); |
3999 | hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)), |
4000 | hstate, flags: flags & ~OEP_ADDRESS_OF); |
4001 | hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)), |
4002 | hstate, flags: flags & ~OEP_ADDRESS_OF); |
4003 | return; |
4004 | } |
4005 | break; |
4006 | case ARRAY_REF: |
4007 | case ARRAY_RANGE_REF: |
4008 | case BIT_FIELD_REF: |
4009 | sflags &= ~OEP_ADDRESS_OF; |
4010 | break; |
4011 | |
4012 | case COND_EXPR: |
4013 | flags &= ~OEP_ADDRESS_OF; |
4014 | break; |
4015 | |
4016 | case WIDEN_MULT_PLUS_EXPR: |
4017 | case WIDEN_MULT_MINUS_EXPR: |
4018 | { |
4019 | /* The multiplication operands are commutative. */ |
4020 | inchash::hash one, two; |
4021 | hash_operand (TREE_OPERAND (t, 0), hstate&: one, flags); |
4022 | hash_operand (TREE_OPERAND (t, 1), hstate&: two, flags); |
4023 | hstate.add_commutative (a&: one, b&: two); |
4024 | hash_operand (TREE_OPERAND (t, 2), hstate&: two, flags); |
4025 | return; |
4026 | } |
4027 | |
4028 | case CALL_EXPR: |
4029 | if (CALL_EXPR_FN (t) == NULL_TREE) |
4030 | hstate.add_int (CALL_EXPR_IFN (t)); |
4031 | break; |
4032 | |
4033 | case TARGET_EXPR: |
4034 | /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT. |
4035 | Usually different TARGET_EXPRs just should use |
4036 | different temporaries in their slots. */ |
4037 | hash_operand (TARGET_EXPR_SLOT (t), hstate, flags); |
4038 | return; |
4039 | |
4040 | case OBJ_TYPE_REF: |
4041 | /* Virtual table reference. */ |
4042 | inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags); |
4043 | flags &= ~OEP_ADDRESS_OF; |
4044 | inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags); |
4045 | inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags); |
4046 | if (!virtual_method_call_p (t)) |
4047 | return; |
4048 | if (tree c = obj_type_ref_class (ref: t)) |
4049 | { |
4050 | c = TYPE_NAME (TYPE_MAIN_VARIANT (c)); |
4051 | /* We compute mangled names only when free_lang_data is run. |
4052 | In that case we can hash precisely. */ |
4053 | if (TREE_CODE (c) == TYPE_DECL |
4054 | && DECL_ASSEMBLER_NAME_SET_P (c)) |
4055 | hstate.add_object |
4056 | (IDENTIFIER_HASH_VALUE |
4057 | (DECL_ASSEMBLER_NAME (c))); |
4058 | } |
4059 | return; |
4060 | default: |
4061 | break; |
4062 | } |
4063 | |
4064 | /* Don't hash the type, that can lead to having nodes which |
4065 | compare equal according to operand_equal_p, but which |
4066 | have different hash codes. */ |
4067 | if (code == NON_LVALUE_EXPR) |
4068 | { |
4069 | /* Make sure to include signness in the hash computation. */ |
4070 | hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t))); |
4071 | hash_operand (TREE_OPERAND (t, 0), hstate, flags); |
4072 | } |
4073 | |
4074 | else if (commutative_tree_code (code)) |
4075 | { |
4076 | /* It's a commutative expression. We want to hash it the same |
4077 | however it appears. We do this by first hashing both operands |
4078 | and then rehashing based on the order of their independent |
4079 | hashes. */ |
4080 | inchash::hash one, two; |
4081 | hash_operand (TREE_OPERAND (t, 0), hstate&: one, flags); |
4082 | hash_operand (TREE_OPERAND (t, 1), hstate&: two, flags); |
4083 | hstate.add_commutative (a&: one, b&: two); |
4084 | } |
4085 | else |
4086 | for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i) |
4087 | hash_operand (TREE_OPERAND (t, i), hstate, |
4088 | flags: i == 0 ? flags : sflags); |
4089 | } |
4090 | return; |
4091 | } |
4092 | } |
4093 | |
4094 | bool |
4095 | operand_compare::verify_hash_value (const_tree arg0, const_tree arg1, |
4096 | unsigned int flags, bool *ret) |
4097 | { |
4098 | /* When checking and unless comparing DECL names, verify that if |
4099 | the outermost operand_equal_p call returns non-zero then ARG0 |
4100 | and ARG1 have the same hash value. */ |
4101 | if (flag_checking && !(flags & OEP_NO_HASH_CHECK)) |
4102 | { |
4103 | if (operand_equal_p (arg0, arg1, flags: flags | OEP_NO_HASH_CHECK)) |
4104 | { |
4105 | if (arg0 != arg1 && !(flags & OEP_DECL_NAME)) |
4106 | { |
4107 | inchash::hash hstate0 (0), hstate1 (0); |
4108 | hash_operand (t: arg0, hstate&: hstate0, flags: flags | OEP_HASH_CHECK); |
4109 | hash_operand (t: arg1, hstate&: hstate1, flags: flags | OEP_HASH_CHECK); |
4110 | hashval_t h0 = hstate0.end (); |
4111 | hashval_t h1 = hstate1.end (); |
4112 | gcc_assert (h0 == h1); |
4113 | } |
4114 | *ret = true; |
4115 | } |
4116 | else |
4117 | *ret = false; |
4118 | |
4119 | return true; |
4120 | } |
4121 | |
4122 | return false; |
4123 | } |
4124 | |
4125 | |
4126 | static operand_compare default_compare_instance; |
4127 | |
4128 | /* Conveinece wrapper around operand_compare class because usually we do |
4129 | not need to play with the valueizer. */ |
4130 | |
4131 | bool |
4132 | operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags) |
4133 | { |
4134 | return default_compare_instance.operand_equal_p (arg0, arg1, flags); |
4135 | } |
4136 | |
4137 | namespace inchash |
4138 | { |
4139 | |
4140 | /* Generate a hash value for an expression. This can be used iteratively |
4141 | by passing a previous result as the HSTATE argument. |
4142 | |
4143 | This function is intended to produce the same hash for expressions which |
4144 | would compare equal using operand_equal_p. */ |
4145 | void |
4146 | add_expr (const_tree t, inchash::hash &hstate, unsigned int flags) |
4147 | { |
4148 | default_compare_instance.hash_operand (t, hstate, flags); |
4149 | } |
4150 | |
4151 | } |
4152 | |
4153 | /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1 |
4154 | with a different signedness or a narrower precision. */ |
4155 | |
4156 | static bool |
4157 | operand_equal_for_comparison_p (tree arg0, tree arg1) |
4158 | { |
4159 | if (operand_equal_p (arg0, arg1, flags: 0)) |
4160 | return true; |
4161 | |
4162 | if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
4163 | || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1))) |
4164 | return false; |
4165 | |
4166 | /* Discard any conversions that don't change the modes of ARG0 and ARG1 |
4167 | and see if the inner values are the same. This removes any |
4168 | signedness comparison, which doesn't matter here. */ |
4169 | tree op0 = arg0; |
4170 | tree op1 = arg1; |
4171 | STRIP_NOPS (op0); |
4172 | STRIP_NOPS (op1); |
4173 | if (operand_equal_p (arg0: op0, arg1: op1, flags: 0)) |
4174 | return true; |
4175 | |
4176 | /* Discard a single widening conversion from ARG1 and see if the inner |
4177 | value is the same as ARG0. */ |
4178 | if (CONVERT_EXPR_P (arg1) |
4179 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0))) |
4180 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))) |
4181 | < TYPE_PRECISION (TREE_TYPE (arg1)) |
4182 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
4183 | return true; |
4184 | |
4185 | return false; |
4186 | } |
4187 | |
4188 | /* See if ARG is an expression that is either a comparison or is performing |
4189 | arithmetic on comparisons. The comparisons must only be comparing |
4190 | two different values, which will be stored in *CVAL1 and *CVAL2; if |
4191 | they are nonzero it means that some operands have already been found. |
4192 | No variables may be used anywhere else in the expression except in the |
4193 | comparisons. |
4194 | |
4195 | If this is true, return 1. Otherwise, return zero. */ |
4196 | |
4197 | static bool |
4198 | twoval_comparison_p (tree arg, tree *cval1, tree *cval2) |
4199 | { |
4200 | enum tree_code code = TREE_CODE (arg); |
4201 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
4202 | |
4203 | /* We can handle some of the tcc_expression cases here. */ |
4204 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
4205 | tclass = tcc_unary; |
4206 | else if (tclass == tcc_expression |
4207 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR |
4208 | || code == COMPOUND_EXPR)) |
4209 | tclass = tcc_binary; |
4210 | |
4211 | switch (tclass) |
4212 | { |
4213 | case tcc_unary: |
4214 | return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2); |
4215 | |
4216 | case tcc_binary: |
4217 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2) |
4218 | && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)); |
4219 | |
4220 | case tcc_constant: |
4221 | return true; |
4222 | |
4223 | case tcc_expression: |
4224 | if (code == COND_EXPR) |
4225 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2) |
4226 | && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2) |
4227 | && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2)); |
4228 | return false; |
4229 | |
4230 | case tcc_comparison: |
4231 | /* First see if we can handle the first operand, then the second. For |
4232 | the second operand, we know *CVAL1 can't be zero. It must be that |
4233 | one side of the comparison is each of the values; test for the |
4234 | case where this isn't true by failing if the two operands |
4235 | are the same. */ |
4236 | |
4237 | if (operand_equal_p (TREE_OPERAND (arg, 0), |
4238 | TREE_OPERAND (arg, 1), flags: 0)) |
4239 | return false; |
4240 | |
4241 | if (*cval1 == 0) |
4242 | *cval1 = TREE_OPERAND (arg, 0); |
4243 | else if (operand_equal_p (arg0: *cval1, TREE_OPERAND (arg, 0), flags: 0)) |
4244 | ; |
4245 | else if (*cval2 == 0) |
4246 | *cval2 = TREE_OPERAND (arg, 0); |
4247 | else if (operand_equal_p (arg0: *cval2, TREE_OPERAND (arg, 0), flags: 0)) |
4248 | ; |
4249 | else |
4250 | return false; |
4251 | |
4252 | if (operand_equal_p (arg0: *cval1, TREE_OPERAND (arg, 1), flags: 0)) |
4253 | ; |
4254 | else if (*cval2 == 0) |
4255 | *cval2 = TREE_OPERAND (arg, 1); |
4256 | else if (operand_equal_p (arg0: *cval2, TREE_OPERAND (arg, 1), flags: 0)) |
4257 | ; |
4258 | else |
4259 | return false; |
4260 | |
4261 | return true; |
4262 | |
4263 | default: |
4264 | return false; |
4265 | } |
4266 | } |
4267 | |
4268 | /* ARG is a tree that is known to contain just arithmetic operations and |
4269 | comparisons. Evaluate the operations in the tree substituting NEW0 for |
4270 | any occurrence of OLD0 as an operand of a comparison and likewise for |
4271 | NEW1 and OLD1. */ |
4272 | |
4273 | static tree |
4274 | eval_subst (location_t loc, tree arg, tree old0, tree new0, |
4275 | tree old1, tree new1) |
4276 | { |
4277 | tree type = TREE_TYPE (arg); |
4278 | enum tree_code code = TREE_CODE (arg); |
4279 | enum tree_code_class tclass = TREE_CODE_CLASS (code); |
4280 | |
4281 | /* We can handle some of the tcc_expression cases here. */ |
4282 | if (tclass == tcc_expression && code == TRUTH_NOT_EXPR) |
4283 | tclass = tcc_unary; |
4284 | else if (tclass == tcc_expression |
4285 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) |
4286 | tclass = tcc_binary; |
4287 | |
4288 | switch (tclass) |
4289 | { |
4290 | case tcc_unary: |
4291 | return fold_build1_loc (loc, code, type, |
4292 | eval_subst (loc, TREE_OPERAND (arg, 0), |
4293 | old0, new0, old1, new1)); |
4294 | |
4295 | case tcc_binary: |
4296 | return fold_build2_loc (loc, code, type, |
4297 | eval_subst (loc, TREE_OPERAND (arg, 0), |
4298 | old0, new0, old1, new1), |
4299 | eval_subst (loc, TREE_OPERAND (arg, 1), |
4300 | old0, new0, old1, new1)); |
4301 | |
4302 | case tcc_expression: |
4303 | switch (code) |
4304 | { |
4305 | case SAVE_EXPR: |
4306 | return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0, |
4307 | old1, new1); |
4308 | |
4309 | case COMPOUND_EXPR: |
4310 | return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0, |
4311 | old1, new1); |
4312 | |
4313 | case COND_EXPR: |
4314 | return fold_build3_loc (loc, code, type, |
4315 | eval_subst (loc, TREE_OPERAND (arg, 0), |
4316 | old0, new0, old1, new1), |
4317 | eval_subst (loc, TREE_OPERAND (arg, 1), |
4318 | old0, new0, old1, new1), |
4319 | eval_subst (loc, TREE_OPERAND (arg, 2), |
4320 | old0, new0, old1, new1)); |
4321 | default: |
4322 | break; |
4323 | } |
4324 | /* Fall through - ??? */ |
4325 | |
4326 | case tcc_comparison: |
4327 | { |
4328 | tree arg0 = TREE_OPERAND (arg, 0); |
4329 | tree arg1 = TREE_OPERAND (arg, 1); |
4330 | |
4331 | /* We need to check both for exact equality and tree equality. The |
4332 | former will be true if the operand has a side-effect. In that |
4333 | case, we know the operand occurred exactly once. */ |
4334 | |
4335 | if (arg0 == old0 || operand_equal_p (arg0, arg1: old0, flags: 0)) |
4336 | arg0 = new0; |
4337 | else if (arg0 == old1 || operand_equal_p (arg0, arg1: old1, flags: 0)) |
4338 | arg0 = new1; |
4339 | |
4340 | if (arg1 == old0 || operand_equal_p (arg0: arg1, arg1: old0, flags: 0)) |
4341 | arg1 = new0; |
4342 | else if (arg1 == old1 || operand_equal_p (arg0: arg1, arg1: old1, flags: 0)) |
4343 | arg1 = new1; |
4344 | |
4345 | return fold_build2_loc (loc, code, type, arg0, arg1); |
4346 | } |
4347 | |
4348 | default: |
4349 | return arg; |
4350 | } |
4351 | } |
4352 | |
4353 | /* Return a tree for the case when the result of an expression is RESULT |
4354 | converted to TYPE and OMITTED was previously an operand of the expression |
4355 | but is now not needed (e.g., we folded OMITTED * 0). |
4356 | |
4357 | If OMITTED has side effects, we must evaluate it. Otherwise, just do |
4358 | the conversion of RESULT to TYPE. */ |
4359 | |
4360 | tree |
4361 | omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted) |
4362 | { |
4363 | tree t = fold_convert_loc (loc, type, arg: result); |
4364 | |
4365 | /* If the resulting operand is an empty statement, just return the omitted |
4366 | statement casted to void. */ |
4367 | if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted)) |
4368 | return build1_loc (loc, code: NOP_EXPR, void_type_node, |
4369 | arg1: fold_ignored_result (omitted)); |
4370 | |
4371 | if (TREE_SIDE_EFFECTS (omitted)) |
4372 | return build2_loc (loc, code: COMPOUND_EXPR, type, |
4373 | arg0: fold_ignored_result (omitted), arg1: t); |
4374 | |
4375 | return non_lvalue_loc (loc, x: t); |
4376 | } |
4377 | |
4378 | /* Return a tree for the case when the result of an expression is RESULT |
4379 | converted to TYPE and OMITTED1 and OMITTED2 were previously operands |
4380 | of the expression but are now not needed. |
4381 | |
4382 | If OMITTED1 or OMITTED2 has side effects, they must be evaluated. |
4383 | If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is |
4384 | evaluated before OMITTED2. Otherwise, if neither has side effects, |
4385 | just do the conversion of RESULT to TYPE. */ |
4386 | |
4387 | tree |
4388 | omit_two_operands_loc (location_t loc, tree type, tree result, |
4389 | tree omitted1, tree omitted2) |
4390 | { |
4391 | tree t = fold_convert_loc (loc, type, arg: result); |
4392 | |
4393 | if (TREE_SIDE_EFFECTS (omitted2)) |
4394 | t = build2_loc (loc, code: COMPOUND_EXPR, type, arg0: omitted2, arg1: t); |
4395 | if (TREE_SIDE_EFFECTS (omitted1)) |
4396 | t = build2_loc (loc, code: COMPOUND_EXPR, type, arg0: omitted1, arg1: t); |
4397 | |
4398 | return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, x: t) : t; |
4399 | } |
4400 | |
4401 | |
4402 | /* Return a simplified tree node for the truth-negation of ARG. This |
4403 | never alters ARG itself. We assume that ARG is an operation that |
4404 | returns a truth value (0 or 1). |
4405 | |
4406 | FIXME: one would think we would fold the result, but it causes |
4407 | problems with the dominator optimizer. */ |
4408 | |
4409 | static tree |
4410 | fold_truth_not_expr (location_t loc, tree arg) |
4411 | { |
4412 | tree type = TREE_TYPE (arg); |
4413 | enum tree_code code = TREE_CODE (arg); |
4414 | location_t loc1, loc2; |
4415 | |
4416 | /* If this is a comparison, we can simply invert it, except for |
4417 | floating-point non-equality comparisons, in which case we just |
4418 | enclose a TRUTH_NOT_EXPR around what we have. */ |
4419 | |
4420 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
4421 | { |
4422 | tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0)); |
4423 | if (FLOAT_TYPE_P (op_type) |
4424 | && flag_trapping_math |
4425 | && code != ORDERED_EXPR && code != UNORDERED_EXPR |
4426 | && code != NE_EXPR && code != EQ_EXPR) |
4427 | return NULL_TREE; |
4428 | |
4429 | code = invert_tree_comparison (code, honor_nans: HONOR_NANS (op_type)); |
4430 | if (code == ERROR_MARK) |
4431 | return NULL_TREE; |
4432 | |
4433 | tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0), |
4434 | TREE_OPERAND (arg, 1)); |
4435 | copy_warning (ret, arg); |
4436 | return ret; |
4437 | } |
4438 | |
4439 | switch (code) |
4440 | { |
4441 | case INTEGER_CST: |
4442 | return constant_boolean_node (integer_zerop (arg), type); |
4443 | |
4444 | case TRUTH_AND_EXPR: |
4445 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4446 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4447 | return build2_loc (loc, code: TRUTH_OR_EXPR, type, |
4448 | arg0: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
4449 | arg1: invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); |
4450 | |
4451 | case TRUTH_OR_EXPR: |
4452 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4453 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4454 | return build2_loc (loc, code: TRUTH_AND_EXPR, type, |
4455 | arg0: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
4456 | arg1: invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); |
4457 | |
4458 | case TRUTH_XOR_EXPR: |
4459 | /* Here we can invert either operand. We invert the first operand |
4460 | unless the second operand is a TRUTH_NOT_EXPR in which case our |
4461 | result is the XOR of the first operand with the inside of the |
4462 | negation of the second operand. */ |
4463 | |
4464 | if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR) |
4465 | return build2_loc (loc, code: TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0), |
4466 | TREE_OPERAND (TREE_OPERAND (arg, 1), 0)); |
4467 | else |
4468 | return build2_loc (loc, code: TRUTH_XOR_EXPR, type, |
4469 | arg0: invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)), |
4470 | TREE_OPERAND (arg, 1)); |
4471 | |
4472 | case TRUTH_ANDIF_EXPR: |
4473 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4474 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4475 | return build2_loc (loc, code: TRUTH_ORIF_EXPR, type, |
4476 | arg0: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
4477 | arg1: invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); |
4478 | |
4479 | case TRUTH_ORIF_EXPR: |
4480 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4481 | loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4482 | return build2_loc (loc, code: TRUTH_ANDIF_EXPR, type, |
4483 | arg0: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)), |
4484 | arg1: invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1))); |
4485 | |
4486 | case TRUTH_NOT_EXPR: |
4487 | return TREE_OPERAND (arg, 0); |
4488 | |
4489 | case COND_EXPR: |
4490 | { |
4491 | tree arg1 = TREE_OPERAND (arg, 1); |
4492 | tree arg2 = TREE_OPERAND (arg, 2); |
4493 | |
4494 | loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4495 | loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc); |
4496 | |
4497 | /* A COND_EXPR may have a throw as one operand, which |
4498 | then has void type. Just leave void operands |
4499 | as they are. */ |
4500 | return build3_loc (loc, code: COND_EXPR, type, TREE_OPERAND (arg, 0), |
4501 | VOID_TYPE_P (TREE_TYPE (arg1)) |
4502 | ? arg1 : invert_truthvalue_loc (loc1, arg1), |
4503 | VOID_TYPE_P (TREE_TYPE (arg2)) |
4504 | ? arg2 : invert_truthvalue_loc (loc2, arg2)); |
4505 | } |
4506 | |
4507 | case COMPOUND_EXPR: |
4508 | loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc); |
4509 | return build2_loc (loc, code: COMPOUND_EXPR, type, |
4510 | TREE_OPERAND (arg, 0), |
4511 | arg1: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1))); |
4512 | |
4513 | case NON_LVALUE_EXPR: |
4514 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4515 | return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)); |
4516 | |
4517 | CASE_CONVERT: |
4518 | if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE) |
4519 | return build1_loc (loc, code: TRUTH_NOT_EXPR, type, arg1: arg); |
4520 | |
4521 | /* fall through */ |
4522 | |
4523 | case FLOAT_EXPR: |
4524 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4525 | return build1_loc (loc, TREE_CODE (arg), type, |
4526 | arg1: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); |
4527 | |
4528 | case BIT_AND_EXPR: |
4529 | if (!integer_onep (TREE_OPERAND (arg, 1))) |
4530 | return NULL_TREE; |
4531 | return build2_loc (loc, code: EQ_EXPR, type, arg0: arg, arg1: build_int_cst (type, 0)); |
4532 | |
4533 | case SAVE_EXPR: |
4534 | return build1_loc (loc, code: TRUTH_NOT_EXPR, type, arg1: arg); |
4535 | |
4536 | case CLEANUP_POINT_EXPR: |
4537 | loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc); |
4538 | return build1_loc (loc, code: CLEANUP_POINT_EXPR, type, |
4539 | arg1: invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0))); |
4540 | |
4541 | default: |
4542 | return NULL_TREE; |
4543 | } |
4544 | } |
4545 | |
4546 | /* Fold the truth-negation of ARG. This never alters ARG itself. We |
4547 | assume that ARG is an operation that returns a truth value (0 or 1 |
4548 | for scalars, 0 or -1 for vectors). Return the folded expression if |
4549 | folding is successful. Otherwise, return NULL_TREE. */ |
4550 | |
4551 | static tree |
4552 | fold_invert_truthvalue (location_t loc, tree arg) |
4553 | { |
4554 | tree type = TREE_TYPE (arg); |
4555 | return fold_unary_loc (loc, VECTOR_TYPE_P (type) |
4556 | ? BIT_NOT_EXPR |
4557 | : TRUTH_NOT_EXPR, |
4558 | type, arg); |
4559 | } |
4560 | |
4561 | /* Return a simplified tree node for the truth-negation of ARG. This |
4562 | never alters ARG itself. We assume that ARG is an operation that |
4563 | returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */ |
4564 | |
4565 | tree |
4566 | invert_truthvalue_loc (location_t loc, tree arg) |
4567 | { |
4568 | if (TREE_CODE (arg) == ERROR_MARK) |
4569 | return arg; |
4570 | |
4571 | tree type = TREE_TYPE (arg); |
4572 | return fold_build1_loc (loc, VECTOR_TYPE_P (type) |
4573 | ? BIT_NOT_EXPR |
4574 | : TRUTH_NOT_EXPR, |
4575 | type, arg); |
4576 | } |
4577 | |
4578 | /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER |
4579 | starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero |
4580 | and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER |
4581 | is the original memory reference used to preserve the alias set of |
4582 | the access. */ |
4583 | |
4584 | static tree |
4585 | make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type, |
4586 | HOST_WIDE_INT bitsize, poly_int64 bitpos, |
4587 | int unsignedp, int reversep) |
4588 | { |
4589 | tree result, bftype; |
4590 | |
4591 | /* Attempt not to lose the access path if possible. */ |
4592 | if (TREE_CODE (orig_inner) == COMPONENT_REF) |
4593 | { |
4594 | tree ninner = TREE_OPERAND (orig_inner, 0); |
4595 | machine_mode nmode; |
4596 | poly_int64 nbitsize, nbitpos; |
4597 | tree noffset; |
4598 | int nunsignedp, nreversep, nvolatilep = 0; |
4599 | tree base = get_inner_reference (ninner, &nbitsize, &nbitpos, |
4600 | &noffset, &nmode, &nunsignedp, |
4601 | &nreversep, &nvolatilep); |
4602 | if (base == inner |
4603 | && noffset == NULL_TREE |
4604 | && known_subrange_p (pos1: bitpos, size1: bitsize, pos2: nbitpos, size2: nbitsize) |
4605 | && !reversep |
4606 | && !nreversep |
4607 | && !nvolatilep) |
4608 | { |
4609 | inner = ninner; |
4610 | bitpos -= nbitpos; |
4611 | } |
4612 | } |
4613 | |
4614 | alias_set_type iset = get_alias_set (orig_inner); |
4615 | if (iset == 0 && get_alias_set (inner) != iset) |
4616 | inner = fold_build2 (MEM_REF, TREE_TYPE (inner), |
4617 | build_fold_addr_expr (inner), |
4618 | build_int_cst (ptr_type_node, 0)); |
4619 | |
4620 | if (known_eq (bitpos, 0) && !reversep) |
4621 | { |
4622 | tree size = TYPE_SIZE (TREE_TYPE (inner)); |
4623 | if ((INTEGRAL_TYPE_P (TREE_TYPE (inner)) |
4624 | || POINTER_TYPE_P (TREE_TYPE (inner))) |
4625 | && tree_fits_shwi_p (size) |
4626 | && tree_to_shwi (size) == bitsize) |
4627 | return fold_convert_loc (loc, type, arg: inner); |
4628 | } |
4629 | |
4630 | bftype = type; |
4631 | if (TYPE_PRECISION (bftype) != bitsize |
4632 | || TYPE_UNSIGNED (bftype) == !unsignedp) |
4633 | bftype = build_nonstandard_integer_type (bitsize, 0); |
4634 | |
4635 | result = build3_loc (loc, code: BIT_FIELD_REF, type: bftype, arg0: inner, |
4636 | bitsize_int (bitsize), bitsize_int (bitpos)); |
4637 | REF_REVERSE_STORAGE_ORDER (result) = reversep; |
4638 | |
4639 | if (bftype != type) |
4640 | result = fold_convert_loc (loc, type, arg: result); |
4641 | |
4642 | return result; |
4643 | } |
4644 | |
4645 | /* Optimize a bit-field compare. |
4646 | |
4647 | There are two cases: First is a compare against a constant and the |
4648 | second is a comparison of two items where the fields are at the same |
4649 | bit position relative to the start of a chunk (byte, halfword, word) |
4650 | large enough to contain it. In these cases we can avoid the shift |
4651 | implicit in bitfield extractions. |
4652 | |
4653 | For constants, we emit a compare of the shifted constant with the |
4654 | BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being |
4655 | compared. For two fields at the same position, we do the ANDs with the |
4656 | similar mask and compare the result of the ANDs. |
4657 | |
4658 | CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR. |
4659 | COMPARE_TYPE is the type of the comparison, and LHS and RHS |
4660 | are the left and right operands of the comparison, respectively. |
4661 | |
4662 | If the optimization described above can be done, we return the resulting |
4663 | tree. Otherwise we return zero. */ |
4664 | |
4665 | static tree |
4666 | optimize_bit_field_compare (location_t loc, enum tree_code code, |
4667 | tree compare_type, tree lhs, tree rhs) |
4668 | { |
4669 | poly_int64 plbitpos, plbitsize, rbitpos, rbitsize; |
4670 | HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize; |
4671 | tree type = TREE_TYPE (lhs); |
4672 | tree unsigned_type; |
4673 | int const_p = TREE_CODE (rhs) == INTEGER_CST; |
4674 | machine_mode lmode, rmode; |
4675 | scalar_int_mode nmode; |
4676 | int lunsignedp, runsignedp; |
4677 | int lreversep, rreversep; |
4678 | int lvolatilep = 0, rvolatilep = 0; |
4679 | tree linner, rinner = NULL_TREE; |
4680 | tree mask; |
4681 | tree offset; |
4682 | |
4683 | /* Get all the information about the extractions being done. If the bit size |
4684 | is the same as the size of the underlying object, we aren't doing an |
4685 | extraction at all and so can do nothing. We also don't want to |
4686 | do anything if the inner expression is a PLACEHOLDER_EXPR since we |
4687 | then will no longer be able to replace it. */ |
4688 | linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode, |
4689 | &lunsignedp, &lreversep, &lvolatilep); |
4690 | if (linner == lhs |
4691 | || !known_size_p (a: plbitsize) |
4692 | || !plbitsize.is_constant (const_value: &lbitsize) |
4693 | || !plbitpos.is_constant (const_value: &lbitpos) |
4694 | || known_eq (lbitsize, GET_MODE_BITSIZE (lmode)) |
4695 | || offset != 0 |
4696 | || TREE_CODE (linner) == PLACEHOLDER_EXPR |
4697 | || lvolatilep) |
4698 | return 0; |
4699 | |
4700 | if (const_p) |
4701 | rreversep = lreversep; |
4702 | else |
4703 | { |
4704 | /* If this is not a constant, we can only do something if bit positions, |
4705 | sizes, signedness and storage order are the same. */ |
4706 | rinner |
4707 | = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode, |
4708 | &runsignedp, &rreversep, &rvolatilep); |
4709 | |
4710 | if (rinner == rhs |
4711 | || maybe_ne (a: lbitpos, b: rbitpos) |
4712 | || maybe_ne (a: lbitsize, b: rbitsize) |
4713 | || lunsignedp != runsignedp |
4714 | || lreversep != rreversep |
4715 | || offset != 0 |
4716 | || TREE_CODE (rinner) == PLACEHOLDER_EXPR |
4717 | || rvolatilep) |
4718 | return 0; |
4719 | } |
4720 | |
4721 | /* Honor the C++ memory model and mimic what RTL expansion does. */ |
4722 | poly_uint64 bitstart = 0; |
4723 | poly_uint64 bitend = 0; |
4724 | if (TREE_CODE (lhs) == COMPONENT_REF) |
4725 | { |
4726 | get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset); |
4727 | if (!plbitpos.is_constant (const_value: &lbitpos) || offset != NULL_TREE) |
4728 | return 0; |
4729 | } |
4730 | |
4731 | /* See if we can find a mode to refer to this field. We should be able to, |
4732 | but fail if we can't. */ |
4733 | if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend, |
4734 | const_p ? TYPE_ALIGN (TREE_TYPE (linner)) |
4735 | : MIN (TYPE_ALIGN (TREE_TYPE (linner)), |
4736 | TYPE_ALIGN (TREE_TYPE (rinner))), |
4737 | BITS_PER_WORD, false, &nmode)) |
4738 | return 0; |
4739 | |
4740 | /* Set signed and unsigned types of the precision of this mode for the |
4741 | shifts below. */ |
4742 | unsigned_type = lang_hooks.types.type_for_mode (nmode, 1); |
4743 | |
4744 | /* Compute the bit position and size for the new reference and our offset |
4745 | within it. If the new reference is the same size as the original, we |
4746 | won't optimize anything, so return zero. */ |
4747 | nbitsize = GET_MODE_BITSIZE (mode: nmode); |
4748 | nbitpos = lbitpos & ~ (nbitsize - 1); |
4749 | lbitpos -= nbitpos; |
4750 | if (nbitsize == lbitsize) |
4751 | return 0; |
4752 | |
4753 | if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN) |
4754 | lbitpos = nbitsize - lbitsize - lbitpos; |
4755 | |
4756 | /* Make the mask to be used against the extracted field. */ |
4757 | mask = build_int_cst_type (unsigned_type, -1); |
4758 | mask = const_binop (code: LSHIFT_EXPR, arg1: mask, size_int (nbitsize - lbitsize)); |
4759 | mask = const_binop (code: RSHIFT_EXPR, arg1: mask, |
4760 | size_int (nbitsize - lbitsize - lbitpos)); |
4761 | |
4762 | if (! const_p) |
4763 | { |
4764 | if (nbitpos < 0) |
4765 | return 0; |
4766 | |
4767 | /* If not comparing with constant, just rework the comparison |
4768 | and return. */ |
4769 | tree t1 = make_bit_field_ref (loc, inner: linner, orig_inner: lhs, type: unsigned_type, |
4770 | bitsize: nbitsize, bitpos: nbitpos, unsignedp: 1, reversep: lreversep); |
4771 | t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask); |
4772 | tree t2 = make_bit_field_ref (loc, inner: rinner, orig_inner: rhs, type: unsigned_type, |
4773 | bitsize: nbitsize, bitpos: nbitpos, unsignedp: 1, reversep: rreversep); |
4774 | t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask); |
4775 | return fold_build2_loc (loc, code, compare_type, t1, t2); |
4776 | } |
4777 | |
4778 | /* Otherwise, we are handling the constant case. See if the constant is too |
4779 | big for the field. Warn and return a tree for 0 (false) if so. We do |
4780 | this not only for its own sake, but to avoid having to test for this |
4781 | error case below. If we didn't, we might generate wrong code. |
4782 | |
4783 | For unsigned fields, the constant shifted right by the field length should |
4784 | be all zero. For signed fields, the high-order bits should agree with |
4785 | the sign bit. */ |
4786 | |
4787 | if (lunsignedp) |
4788 | { |
4789 | if (wi::lrshift (x: wi::to_wide (t: rhs), y: lbitsize) != 0) |
4790 | { |
4791 | warning (0, "comparison is always %d due to width of bit-field" , |
4792 | code == NE_EXPR); |
4793 | return constant_boolean_node (code == NE_EXPR, compare_type); |
4794 | } |
4795 | } |
4796 | else |
4797 | { |
4798 | wide_int tem = wi::arshift (x: wi::to_wide (t: rhs), y: lbitsize - 1); |
4799 | if (tem != 0 && tem != -1) |
4800 | { |
4801 | warning (0, "comparison is always %d due to width of bit-field" , |
4802 | code == NE_EXPR); |
4803 | return constant_boolean_node (code == NE_EXPR, compare_type); |
4804 | } |
4805 | } |
4806 | |
4807 | if (nbitpos < 0) |
4808 | return 0; |
4809 | |
4810 | /* Single-bit compares should always be against zero. */ |
4811 | if (lbitsize == 1 && ! integer_zerop (rhs)) |
4812 | { |
4813 | code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR; |
4814 | rhs = build_int_cst (type, 0); |
4815 | } |
4816 | |
4817 | /* Make a new bitfield reference, shift the constant over the |
4818 | appropriate number of bits and mask it with the computed mask |
4819 | (in case this was a signed field). If we changed it, make a new one. */ |
4820 | lhs = make_bit_field_ref (loc, inner: linner, orig_inner: lhs, type: unsigned_type, |
4821 | bitsize: nbitsize, bitpos: nbitpos, unsignedp: 1, reversep: lreversep); |
4822 | |
4823 | rhs = const_binop (code: BIT_AND_EXPR, |
4824 | arg1: const_binop (code: LSHIFT_EXPR, |
4825 | arg1: fold_convert_loc (loc, type: unsigned_type, arg: rhs), |
4826 | size_int (lbitpos)), |
4827 | arg2: mask); |
4828 | |
4829 | lhs = build2_loc (loc, code, type: compare_type, |
4830 | arg0: build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), arg1: rhs); |
4831 | return lhs; |
4832 | } |
4833 | |
4834 | /* Subroutine for fold_truth_andor_1: decode a field reference. |
4835 | |
4836 | If EXP is a comparison reference, we return the innermost reference. |
4837 | |
4838 | *PBITSIZE is set to the number of bits in the reference, *PBITPOS is |
4839 | set to the starting bit number. |
4840 | |
4841 | If the innermost field can be completely contained in a mode-sized |
4842 | unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode. |
4843 | |
4844 | *PVOLATILEP is set to 1 if the any expression encountered is volatile; |
4845 | otherwise it is not changed. |
4846 | |
4847 | *PUNSIGNEDP is set to the signedness of the field. |
4848 | |
4849 | *PREVERSEP is set to the storage order of the field. |
4850 | |
4851 | *PMASK is set to the mask used. This is either contained in a |
4852 | BIT_AND_EXPR or derived from the width of the field. |
4853 | |
4854 | *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any. |
4855 | |
4856 | Return 0 if this is not a component reference or is one that we can't |
4857 | do anything with. */ |
4858 | |
4859 | static tree |
4860 | decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize, |
4861 | HOST_WIDE_INT *pbitpos, machine_mode *pmode, |
4862 | int *punsignedp, int *preversep, int *pvolatilep, |
4863 | tree *pmask, tree *pand_mask) |
4864 | { |
4865 | tree exp = *exp_; |
4866 | tree outer_type = 0; |
4867 | tree and_mask = 0; |
4868 | tree mask, inner, offset; |
4869 | tree unsigned_type; |
4870 | unsigned int precision; |
4871 | |
4872 | /* All the optimizations using this function assume integer fields. |
4873 | There are problems with FP fields since the type_for_size call |
4874 | below can fail for, e.g., XFmode. */ |
4875 | if (! INTEGRAL_TYPE_P (TREE_TYPE (exp))) |
4876 | return NULL_TREE; |
4877 | |
4878 | /* We are interested in the bare arrangement of bits, so strip everything |
4879 | that doesn't affect the machine mode. However, record the type of the |
4880 | outermost expression if it may matter below. */ |
4881 | if (CONVERT_EXPR_P (exp) |
4882 | || TREE_CODE (exp) == NON_LVALUE_EXPR) |
4883 | outer_type = TREE_TYPE (exp); |
4884 | STRIP_NOPS (exp); |
4885 | |
4886 | if (TREE_CODE (exp) == BIT_AND_EXPR) |
4887 | { |
4888 | and_mask = TREE_OPERAND (exp, 1); |
4889 | exp = TREE_OPERAND (exp, 0); |
4890 | STRIP_NOPS (exp); STRIP_NOPS (and_mask); |
4891 | if (TREE_CODE (and_mask) != INTEGER_CST) |
4892 | return NULL_TREE; |
4893 | } |
4894 | |
4895 | poly_int64 poly_bitsize, poly_bitpos; |
4896 | inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset, |
4897 | pmode, punsignedp, preversep, pvolatilep); |
4898 | if ((inner == exp && and_mask == 0) |
4899 | || !poly_bitsize.is_constant (const_value: pbitsize) |
4900 | || !poly_bitpos.is_constant (const_value: pbitpos) |
4901 | || *pbitsize < 0 |
4902 | || offset != 0 |
4903 | || TREE_CODE (inner) == PLACEHOLDER_EXPR |
4904 | /* Reject out-of-bound accesses (PR79731). */ |
4905 | || (! AGGREGATE_TYPE_P (TREE_TYPE (inner)) |
4906 | && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)), |
4907 | *pbitpos + *pbitsize) < 0)) |
4908 | return NULL_TREE; |
4909 | |
4910 | unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1); |
4911 | if (unsigned_type == NULL_TREE) |
4912 | return NULL_TREE; |
4913 | |
4914 | *exp_ = exp; |
4915 | |
4916 | /* If the number of bits in the reference is the same as the bitsize of |
4917 | the outer type, then the outer type gives the signedness. Otherwise |
4918 | (in case of a small bitfield) the signedness is unchanged. */ |
4919 | if (outer_type && *pbitsize == TYPE_PRECISION (outer_type)) |
4920 | *punsignedp = TYPE_UNSIGNED (outer_type); |
4921 | |
4922 | /* Compute the mask to access the bitfield. */ |
4923 | precision = TYPE_PRECISION (unsigned_type); |
4924 | |
4925 | mask = build_int_cst_type (unsigned_type, -1); |
4926 | |
4927 | mask = const_binop (code: LSHIFT_EXPR, arg1: mask, size_int (precision - *pbitsize)); |
4928 | mask = const_binop (code: RSHIFT_EXPR, arg1: mask, size_int (precision - *pbitsize)); |
4929 | |
4930 | /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */ |
4931 | if (and_mask != 0) |
4932 | mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, |
4933 | fold_convert_loc (loc, type: unsigned_type, arg: and_mask), mask); |
4934 | |
4935 | *pmask = mask; |
4936 | *pand_mask = and_mask; |
4937 | return inner; |
4938 | } |
4939 | |
4940 | /* Return nonzero if MASK represents a mask of SIZE ones in the low-order |
4941 | bit positions and MASK is SIGNED. */ |
4942 | |
4943 | static bool |
4944 | all_ones_mask_p (const_tree mask, unsigned int size) |
4945 | { |
4946 | tree type = TREE_TYPE (mask); |
4947 | unsigned int precision = TYPE_PRECISION (type); |
4948 | |
4949 | /* If this function returns true when the type of the mask is |
4950 | UNSIGNED, then there will be errors. In particular see |
4951 | gcc.c-torture/execute/990326-1.c. There does not appear to be |
4952 | any documentation paper trail as to why this is so. But the pre |
4953 | wide-int worked with that restriction and it has been preserved |
4954 | here. */ |
4955 | if (size > precision || TYPE_SIGN (type) == UNSIGNED) |
4956 | return false; |
4957 | |
4958 | return wi::mask (width: size, negate_p: false, precision) == wi::to_wide (t: mask); |
4959 | } |
4960 | |
4961 | /* Subroutine for fold: determine if VAL is the INTEGER_CONST that |
4962 | represents the sign bit of EXP's type. If EXP represents a sign |
4963 | or zero extension, also test VAL against the unextended type. |
4964 | The return value is the (sub)expression whose sign bit is VAL, |
4965 | or NULL_TREE otherwise. */ |
4966 | |
4967 | tree |
4968 | sign_bit_p (tree exp, const_tree val) |
4969 | { |
4970 | int width; |
4971 | tree t; |
4972 | |
4973 | /* Tree EXP must have an integral type. */ |
4974 | t = TREE_TYPE (exp); |
4975 | if (! INTEGRAL_TYPE_P (t)) |
4976 | return NULL_TREE; |
4977 | |
4978 | /* Tree VAL must be an integer constant. */ |
4979 | if (TREE_CODE (val) != INTEGER_CST |
4980 | || TREE_OVERFLOW (val)) |
4981 | return NULL_TREE; |
4982 | |
4983 | width = TYPE_PRECISION (t); |
4984 | if (wi::only_sign_bit_p (wi::to_wide (t: val), width)) |
4985 | return exp; |
4986 | |
4987 | /* Handle extension from a narrower type. */ |
4988 | if (TREE_CODE (exp) == NOP_EXPR |
4989 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width) |
4990 | return sign_bit_p (TREE_OPERAND (exp, 0), val); |
4991 | |
4992 | return NULL_TREE; |
4993 | } |
4994 | |
4995 | /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an |
4996 | operand is simple enough to be evaluated unconditionally. */ |
4997 | |
4998 | static bool |
4999 | simple_operand_p (const_tree exp) |
5000 | { |
5001 | /* Strip any conversions that don't change the machine mode. */ |
5002 | STRIP_NOPS (exp); |
5003 | |
5004 | return (CONSTANT_CLASS_P (exp) |
5005 | || TREE_CODE (exp) == SSA_NAME |
5006 | || (DECL_P (exp) |
5007 | && ! TREE_ADDRESSABLE (exp) |
5008 | && ! TREE_THIS_VOLATILE (exp) |
5009 | && ! DECL_NONLOCAL (exp) |
5010 | /* Don't regard global variables as simple. They may be |
5011 | allocated in ways unknown to the compiler (shared memory, |
5012 | #pragma weak, etc). */ |
5013 | && ! TREE_PUBLIC (exp) |
5014 | && ! DECL_EXTERNAL (exp) |
5015 | /* Weakrefs are not safe to be read, since they can be NULL. |
5016 | They are !TREE_PUBLIC && !DECL_EXTERNAL but still |
5017 | have DECL_WEAK flag set. */ |
5018 | && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp)) |
5019 | /* Loading a static variable is unduly expensive, but global |
5020 | registers aren't expensive. */ |
5021 | && (! TREE_STATIC (exp) || DECL_REGISTER (exp)))); |
5022 | } |
5023 | |
5024 | /* Determine if an operand is simple enough to be evaluated unconditionally. |
5025 | In addition to simple_operand_p, we assume that comparisons, conversions, |
5026 | and logic-not operations are simple, if their operands are simple, too. */ |
5027 | |
5028 | bool |
5029 | simple_condition_p (tree exp) |
5030 | { |
5031 | enum tree_code code; |
5032 | |
5033 | if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (expr: exp)) |
5034 | return false; |
5035 | |
5036 | while (CONVERT_EXPR_P (exp)) |
5037 | exp = TREE_OPERAND (exp, 0); |
5038 | |
5039 | code = TREE_CODE (exp); |
5040 | |
5041 | if (TREE_CODE_CLASS (code) == tcc_comparison) |
5042 | return (simple_operand_p (TREE_OPERAND (exp, 0)) |
5043 | && simple_operand_p (TREE_OPERAND (exp, 1))); |
5044 | |
5045 | if (code == TRUTH_NOT_EXPR) |
5046 | return simple_condition_p (TREE_OPERAND (exp, 0)); |
5047 | |
5048 | return simple_operand_p (exp); |
5049 | } |
5050 | |
5051 | |
5052 | /* The following functions are subroutines to fold_range_test and allow it to |
5053 | try to change a logical combination of comparisons into a range test. |
5054 | |
5055 | For example, both |
5056 | X == 2 || X == 3 || X == 4 || X == 5 |
5057 | and |
5058 | X >= 2 && X <= 5 |
5059 | are converted to |
5060 | (unsigned) (X - 2) <= 3 |
5061 | |
5062 | We describe each set of comparisons as being either inside or outside |
5063 | a range, using a variable named like IN_P, and then describe the |
5064 | range with a lower and upper bound. If one of the bounds is omitted, |
5065 | it represents either the highest or lowest value of the type. |
5066 | |
5067 | In the comments below, we represent a range by two numbers in brackets |
5068 | preceded by a "+" to designate being inside that range, or a "-" to |
5069 | designate being outside that range, so the condition can be inverted by |
5070 | flipping the prefix. An omitted bound is represented by a "-". For |
5071 | example, "- [-, 10]" means being outside the range starting at the lowest |
5072 | possible value and ending at 10, in other words, being greater than 10. |
5073 | The range "+ [-, -]" is always true and hence the range "- [-, -]" is |
5074 | always false. |
5075 | |
5076 | We set up things so that the missing bounds are handled in a consistent |
5077 | manner so neither a missing bound nor "true" and "false" need to be |
5078 | handled using a special case. */ |
5079 | |
5080 | /* Return the result of applying CODE to ARG0 and ARG1, but handle the case |
5081 | of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P |
5082 | and UPPER1_P are nonzero if the respective argument is an upper bound |
5083 | and zero for a lower. TYPE, if nonzero, is the type of the result; it |
5084 | must be specified for a comparison. ARG1 will be converted to ARG0's |
5085 | type if both are specified. */ |
5086 | |
5087 | static tree |
5088 | range_binop (enum tree_code code, tree type, tree arg0, int upper0_p, |
5089 | tree arg1, int upper1_p) |
5090 | { |
5091 | tree tem; |
5092 | int result; |
5093 | int sgn0, sgn1; |
5094 | |
5095 | /* If neither arg represents infinity, do the normal operation. |
5096 | Else, if not a comparison, return infinity. Else handle the special |
5097 | comparison rules. Note that most of the cases below won't occur, but |
5098 | are handled for consistency. */ |
5099 | |
5100 | if (arg0 != 0 && arg1 != 0) |
5101 | { |
5102 | tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0), |
5103 | arg0, fold_convert (TREE_TYPE (arg0), arg1)); |
5104 | STRIP_NOPS (tem); |
5105 | return TREE_CODE (tem) == INTEGER_CST ? tem : 0; |
5106 | } |
5107 | |
5108 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
5109 | return 0; |
5110 | |
5111 | /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0 |
5112 | for neither. In real maths, we cannot assume open ended ranges are |
5113 | the same. But, this is computer arithmetic, where numbers are finite. |
5114 | We can therefore make the transformation of any unbounded range with |
5115 | the value Z, Z being greater than any representable number. This permits |
5116 | us to treat unbounded ranges as equal. */ |
5117 | sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1); |
5118 | sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1); |
5119 | switch (code) |
5120 | { |
5121 | case EQ_EXPR: |
5122 | result = sgn0 == sgn1; |
5123 | break; |
5124 | case NE_EXPR: |
5125 | result = sgn0 != sgn1; |
5126 | break; |
5127 | case LT_EXPR: |
5128 | result = sgn0 < sgn1; |
5129 | break; |
5130 | case LE_EXPR: |
5131 | result = sgn0 <= sgn1; |
5132 | break; |
5133 | case GT_EXPR: |
5134 | result = sgn0 > sgn1; |
5135 | break; |
5136 | case GE_EXPR: |
5137 | result = sgn0 >= sgn1; |
5138 | break; |
5139 | default: |
5140 | gcc_unreachable (); |
5141 | } |
5142 | |
5143 | return constant_boolean_node (result, type); |
5144 | } |
5145 | |
5146 | /* Helper routine for make_range. Perform one step for it, return |
5147 | new expression if the loop should continue or NULL_TREE if it should |
5148 | stop. */ |
5149 | |
5150 | tree |
5151 | make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1, |
5152 | tree exp_type, tree *p_low, tree *p_high, int *p_in_p, |
5153 | bool *strict_overflow_p) |
5154 | { |
5155 | tree arg0_type = TREE_TYPE (arg0); |
5156 | tree n_low, n_high, low = *p_low, high = *p_high; |
5157 | int in_p = *p_in_p, n_in_p; |
5158 | |
5159 | switch (code) |
5160 | { |
5161 | case TRUTH_NOT_EXPR: |
5162 | /* We can only do something if the range is testing for zero. */ |
5163 | if (low == NULL_TREE || high == NULL_TREE |
5164 | || ! integer_zerop (low) || ! integer_zerop (high)) |
5165 | return NULL_TREE; |
5166 | *p_in_p = ! in_p; |
5167 | return arg0; |
5168 | |
5169 | case EQ_EXPR: case NE_EXPR: |
5170 | case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR: |
5171 | /* We can only do something if the range is testing for zero |
5172 | and if the second operand is an integer constant. Note that |
5173 | saying something is "in" the range we make is done by |
5174 | complementing IN_P since it will set in the initial case of |
5175 | being not equal to zero; "out" is leaving it alone. */ |
5176 | if (low == NULL_TREE || high == NULL_TREE |
5177 | || ! integer_zerop (low) || ! integer_zerop (high) |
5178 | || TREE_CODE (arg1) != INTEGER_CST) |
5179 | return NULL_TREE; |
5180 | |
5181 | switch (code) |
5182 | { |
5183 | case NE_EXPR: /* - [c, c] */ |
5184 | low = high = arg1; |
5185 | break; |
5186 | case EQ_EXPR: /* + [c, c] */ |
5187 | in_p = ! in_p, low = high = arg1; |
5188 | break; |
5189 | case GT_EXPR: /* - [-, c] */ |
5190 | low = 0, high = arg1; |
5191 | break; |
5192 | case GE_EXPR: /* + [c, -] */ |
5193 | in_p = ! in_p, low = arg1, high = 0; |
5194 | break; |
5195 | case LT_EXPR: /* - [c, -] */ |
5196 | low = arg1, high = 0; |
5197 | break; |
5198 | case LE_EXPR: /* + [-, c] */ |
5199 | in_p = ! in_p, low = 0, high = arg1; |
5200 | break; |
5201 | default: |
5202 | gcc_unreachable (); |
5203 | } |
5204 | |
5205 | /* If this is an unsigned comparison, we also know that EXP is |
5206 | greater than or equal to zero. We base the range tests we make |
5207 | on that fact, so we record it here so we can parse existing |
5208 | range tests. We test arg0_type since often the return type |
5209 | of, e.g. EQ_EXPR, is boolean. */ |
5210 | if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0)) |
5211 | { |
5212 | if (! merge_ranges (&n_in_p, &n_low, &n_high, |
5213 | in_p, low, high, 1, |
5214 | build_int_cst (arg0_type, 0), |
5215 | NULL_TREE)) |
5216 | return NULL_TREE; |
5217 | |
5218 | in_p = n_in_p, low = n_low, high = n_high; |
5219 | |
5220 | /* If the high bound is missing, but we have a nonzero low |
5221 | bound, reverse the range so it goes from zero to the low bound |
5222 | minus 1. */ |
5223 | if (high == 0 && low && ! integer_zerop (low)) |
5224 | { |
5225 | in_p = ! in_p; |
5226 | high = range_binop (code: MINUS_EXPR, NULL_TREE, arg0: low, upper0_p: 0, |
5227 | arg1: build_int_cst (TREE_TYPE (low), 1), upper1_p: 0); |
5228 | low = build_int_cst (arg0_type, 0); |
5229 | } |
5230 | } |
5231 | |
5232 | *p_low = low; |
5233 | *p_high = high; |
5234 | *p_in_p = in_p; |
5235 | return arg0; |
5236 | |
5237 | case NEGATE_EXPR: |
5238 | /* If flag_wrapv and ARG0_TYPE is signed, make sure |
5239 | low and high are non-NULL, then normalize will DTRT. */ |
5240 | if (!TYPE_UNSIGNED (arg0_type) |
5241 | && !TYPE_OVERFLOW_UNDEFINED (arg0_type)) |
5242 | { |
5243 | if (low == NULL_TREE) |
5244 | low = TYPE_MIN_VALUE (arg0_type); |
5245 | if (high == NULL_TREE) |
5246 | high = TYPE_MAX_VALUE (arg0_type); |
5247 | } |
5248 | |
5249 | /* (-x) IN [a,b] -> x in [-b, -a] */ |
5250 | n_low = range_binop (code: MINUS_EXPR, type: exp_type, |
5251 | arg0: build_int_cst (exp_type, 0), |
5252 | upper0_p: 0, arg1: high, upper1_p: 1); |
5253 | n_high = range_binop (code: MINUS_EXPR, type: exp_type, |
5254 | arg0: build_int_cst (exp_type, 0), |
5255 | upper0_p: 0, arg1: low, upper1_p: 0); |
5256 | if (n_high != 0 && TREE_OVERFLOW (n_high)) |
5257 | return NULL_TREE; |
5258 | goto normalize; |
5259 | |
5260 | case BIT_NOT_EXPR: |
5261 | /* ~ X -> -X - 1 */ |
5262 | return build2_loc (loc, code: MINUS_EXPR, type: exp_type, arg0: negate_expr (t: arg0), |
5263 | arg1: build_int_cst (exp_type, 1)); |
5264 | |
5265 | case PLUS_EXPR: |
5266 | case MINUS_EXPR: |
5267 | if (TREE_CODE (arg1) != INTEGER_CST) |
5268 | return NULL_TREE; |
5269 | |
5270 | /* If flag_wrapv and ARG0_TYPE is signed, then we cannot |
5271 | move a constant to the other side. */ |
5272 | if (!TYPE_UNSIGNED (arg0_type) |
5273 | && !TYPE_OVERFLOW_UNDEFINED (arg0_type)) |
5274 | return NULL_TREE; |
5275 | |
5276 | /* If EXP is signed, any overflow in the computation is undefined, |
5277 | so we don't worry about it so long as our computations on |
5278 | the bounds don't overflow. For unsigned, overflow is defined |
5279 | and this is exactly the right thing. */ |
5280 | n_low = range_binop (code: code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, |
5281 | type: arg0_type, arg0: low, upper0_p: 0, arg1, upper1_p: 0); |
5282 | n_high = range_binop (code: code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR, |
5283 | type: arg0_type, arg0: high, upper0_p: 1, arg1, upper1_p: 0); |
5284 | if ((n_low != 0 && TREE_OVERFLOW (n_low)) |
5285 | || (n_high != 0 && TREE_OVERFLOW (n_high))) |
5286 | return NULL_TREE; |
5287 | |
5288 | if (TYPE_OVERFLOW_UNDEFINED (arg0_type)) |
5289 | *strict_overflow_p = true; |
5290 | |
5291 | normalize: |
5292 | /* Check for an unsigned range which has wrapped around the maximum |
5293 | value thus making n_high < n_low, and normalize it. */ |
5294 | if (n_low && n_high && tree_int_cst_lt (t1: n_high, t2: n_low)) |
5295 | { |
5296 | low = range_binop (code: PLUS_EXPR, type: arg0_type, arg0: n_high, upper0_p: 0, |
5297 | arg1: build_int_cst (TREE_TYPE (n_high), 1), upper1_p: 0); |
5298 | high = range_binop (code: MINUS_EXPR, type: arg0_type, arg0: n_low, upper0_p: 0, |
5299 | arg1: build_int_cst (TREE_TYPE (n_low), 1), upper1_p: 0); |
5300 | |
5301 | /* If the range is of the form +/- [ x+1, x ], we won't |
5302 | be able to normalize it. But then, it represents the |
5303 | whole range or the empty set, so make it |
5304 | +/- [ -, - ]. */ |
5305 | if (tree_int_cst_equal (n_low, low) |
5306 | && tree_int_cst_equal (n_high, high)) |
5307 | low = high = 0; |
5308 | else |
5309 | in_p = ! in_p; |
5310 | } |
5311 | else |
5312 | low = n_low, high = n_high; |
5313 | |
5314 | *p_low = low; |
5315 | *p_high = high; |
5316 | *p_in_p = in_p; |
5317 | return arg0; |
5318 | |
5319 | CASE_CONVERT: |
5320 | case NON_LVALUE_EXPR: |
5321 | if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type)) |
5322 | return NULL_TREE; |
5323 | |
5324 | if (! INTEGRAL_TYPE_P (arg0_type) |
5325 | || (low != 0 && ! int_fits_type_p (low, arg0_type)) |
5326 | || (high != 0 && ! int_fits_type_p (high, arg0_type))) |
5327 | return NULL_TREE; |
5328 | |
5329 | n_low = low, n_high = high; |
5330 | |
5331 | if (n_low != 0) |
5332 | n_low = fold_convert_loc (loc, type: arg0_type, arg: n_low); |
5333 | |
5334 | if (n_high != 0) |
5335 | n_high = fold_convert_loc (loc, type: arg0_type, arg: n_high); |
5336 | |
5337 | /* If we're converting arg0 from an unsigned type, to exp, |
5338 | a signed type, we will be doing the comparison as unsigned. |
5339 | The tests above have already verified that LOW and HIGH |
5340 | are both positive. |
5341 | |
5342 | So we have to ensure that we will handle large unsigned |
5343 | values the same way that the current signed bounds treat |
5344 | negative values. */ |
5345 | |
5346 | if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type)) |
5347 | { |
5348 | tree high_positive; |
5349 | tree equiv_type; |
5350 | /* For fixed-point modes, we need to pass the saturating flag |
5351 | as the 2nd parameter. */ |
5352 | if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type))) |
5353 | equiv_type |
5354 | = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), |
5355 | TYPE_SATURATING (arg0_type)); |
5356 | else if (TREE_CODE (arg0_type) == BITINT_TYPE) |
5357 | equiv_type = arg0_type; |
5358 | else |
5359 | equiv_type |
5360 | = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1); |
5361 | |
5362 | /* A range without an upper bound is, naturally, unbounded. |
5363 | Since convert would have cropped a very large value, use |
5364 | the max value for the destination type. */ |
5365 | high_positive |
5366 | = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type) |
5367 | : TYPE_MAX_VALUE (arg0_type); |
5368 | |
5369 | if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type)) |
5370 | high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type, |
5371 | fold_convert_loc (loc, type: arg0_type, |
5372 | arg: high_positive), |
5373 | build_int_cst (arg0_type, 1)); |
5374 | |
5375 | /* If the low bound is specified, "and" the range with the |
5376 | range for which the original unsigned value will be |
5377 | positive. */ |
5378 | if (low != 0) |
5379 | { |
5380 | if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high, |
5381 | 1, fold_convert_loc (loc, type: arg0_type, |
5382 | integer_zero_node), |
5383 | high_positive)) |
5384 | return NULL_TREE; |
5385 | |
5386 | in_p = (n_in_p == in_p); |
5387 | } |
5388 | else |
5389 | { |
5390 | /* Otherwise, "or" the range with the range of the input |
5391 | that will be interpreted as negative. */ |
5392 | if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high, |
5393 | 1, fold_convert_loc (loc, type: arg0_type, |
5394 | integer_zero_node), |
5395 | high_positive)) |
5396 | return NULL_TREE; |
5397 | |
5398 | in_p = (in_p != n_in_p); |
5399 | } |
5400 | } |
5401 | |
5402 | /* Otherwise, if we are converting arg0 from signed type, to exp, |
5403 | an unsigned type, we will do the comparison as signed. If |
5404 | high is non-NULL, we punt above if it doesn't fit in the signed |
5405 | type, so if we get through here, +[-, high] or +[low, high] are |
5406 | equivalent to +[-, n_high] or +[n_low, n_high]. Similarly, |
5407 | +[-, -] or -[-, -] are equivalent too. But if low is specified and |
5408 | high is not, the +[low, -] range is equivalent to union of |
5409 | +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to |
5410 | -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for |
5411 | low being 0, which should be treated as [-, -]. */ |
5412 | else if (TYPE_UNSIGNED (exp_type) |
5413 | && !TYPE_UNSIGNED (arg0_type) |
5414 | && low |
5415 | && !high) |
5416 | { |
5417 | if (integer_zerop (low)) |
5418 | n_low = NULL_TREE; |
5419 | else |
5420 | { |
5421 | n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type, |
5422 | n_low, build_int_cst (arg0_type, -1)); |
5423 | n_low = build_zero_cst (arg0_type); |
5424 | in_p = !in_p; |
5425 | } |
5426 | } |
5427 | |
5428 | *p_low = n_low; |
5429 | *p_high = n_high; |
5430 | *p_in_p = in_p; |
5431 | return arg0; |
5432 | |
5433 | default: |
5434 | return NULL_TREE; |
5435 | } |
5436 | } |
5437 | |
5438 | /* Given EXP, a logical expression, set the range it is testing into |
5439 | variables denoted by PIN_P, PLOW, and PHIGH. Return the expression |
5440 | actually being tested. *PLOW and *PHIGH will be made of the same |
5441 | type as the returned expression. If EXP is not a comparison, we |
5442 | will most likely not be returning a useful value and range. Set |
5443 | *STRICT_OVERFLOW_P to true if the return value is only valid |
5444 | because signed overflow is undefined; otherwise, do not change |
5445 | *STRICT_OVERFLOW_P. */ |
5446 | |
5447 | tree |
5448 | make_range (tree exp, int *pin_p, tree *plow, tree *phigh, |
5449 | bool *strict_overflow_p) |
5450 | { |
5451 | enum tree_code code; |
5452 | tree arg0, arg1 = NULL_TREE; |
5453 | tree exp_type, nexp; |
5454 | int in_p; |
5455 | tree low, high; |
5456 | location_t loc = EXPR_LOCATION (exp); |
5457 | |
5458 | /* Start with simply saying "EXP != 0" and then look at the code of EXP |
5459 | and see if we can refine the range. Some of the cases below may not |
5460 | happen, but it doesn't seem worth worrying about this. We "continue" |
5461 | the outer loop when we've changed something; otherwise we "break" |
5462 | the switch, which will "break" the while. */ |
5463 | |
5464 | in_p = 0; |
5465 | low = high = build_int_cst (TREE_TYPE (exp), 0); |
5466 | |
5467 | while (1) |
5468 | { |
5469 | code = TREE_CODE (exp); |
5470 | exp_type = TREE_TYPE (exp); |
5471 | arg0 = NULL_TREE; |
5472 | |
5473 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))) |
5474 | { |
5475 | if (TREE_OPERAND_LENGTH (exp) > 0) |
5476 | arg0 = TREE_OPERAND (exp, 0); |
5477 | if (TREE_CODE_CLASS (code) == tcc_binary |
5478 | || TREE_CODE_CLASS (code) == tcc_comparison |
5479 | || (TREE_CODE_CLASS (code) == tcc_expression |
5480 | && TREE_OPERAND_LENGTH (exp) > 1)) |
5481 | arg1 = TREE_OPERAND (exp, 1); |
5482 | } |
5483 | if (arg0 == NULL_TREE) |
5484 | break; |
5485 | |
5486 | nexp = make_range_step (loc, code, arg0, arg1, exp_type, p_low: &low, |
5487 | p_high: &high, p_in_p: &in_p, strict_overflow_p); |
5488 | if (nexp == NULL_TREE) |
5489 | break; |
5490 | exp = nexp; |
5491 | } |
5492 | |
5493 | /* If EXP is a constant, we can evaluate whether this is true or false. */ |
5494 | if (TREE_CODE (exp) == INTEGER_CST) |
5495 | { |
5496 | in_p = in_p == (integer_onep (range_binop (code: GE_EXPR, integer_type_node, |
5497 | arg0: exp, upper0_p: 0, arg1: low, upper1_p: 0)) |
5498 | && integer_onep (range_binop (code: LE_EXPR, integer_type_node, |
5499 | arg0: exp, upper0_p: 1, arg1: high, upper1_p: 1))); |
5500 | low = high = 0; |
5501 | exp = 0; |
5502 | } |
5503 | |
5504 | *pin_p = in_p, *plow = low, *phigh = high; |
5505 | return exp; |
5506 | } |
5507 | |
5508 | /* Returns TRUE if [LOW, HIGH] range check can be optimized to |
5509 | a bitwise check i.e. when |
5510 | LOW == 0xXX...X00...0 |
5511 | HIGH == 0xXX...X11...1 |
5512 | Return corresponding mask in MASK and stem in VALUE. */ |
5513 | |
5514 | static bool |
5515 | maskable_range_p (const_tree low, const_tree high, tree type, tree *mask, |
5516 | tree *value) |
5517 | { |
5518 | if (TREE_CODE (low) != INTEGER_CST |
5519 | || TREE_CODE (high) != INTEGER_CST) |
5520 | return false; |
5521 | |
5522 | unsigned prec = TYPE_PRECISION (type); |
5523 | wide_int lo = wi::to_wide (t: low, prec); |
5524 | wide_int hi = wi::to_wide (t: high, prec); |
5525 | |
5526 | wide_int end_mask = lo ^ hi; |
5527 | if ((end_mask & (end_mask + 1)) != 0 |
5528 | || (lo & end_mask) != 0) |
5529 | return false; |
5530 | |
5531 | wide_int stem_mask = ~end_mask; |
5532 | wide_int stem = lo & stem_mask; |
5533 | if (stem != (hi & stem_mask)) |
5534 | return false; |
5535 | |
5536 | *mask = wide_int_to_tree (type, cst: stem_mask); |
5537 | *value = wide_int_to_tree (type, cst: stem); |
5538 | |
5539 | return true; |
5540 | } |
5541 | |
5542 | /* Helper routine for build_range_check and match.pd. Return the type to |
5543 | perform the check or NULL if it shouldn't be optimized. */ |
5544 | |
5545 | tree |
5546 | range_check_type (tree etype) |
5547 | { |
5548 | /* First make sure that arithmetics in this type is valid, then make sure |
5549 | that it wraps around. */ |
5550 | if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE) |
5551 | etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1); |
5552 | |
5553 | if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype)) |
5554 | { |
5555 | tree utype, minv, maxv; |
5556 | |
5557 | /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN |
5558 | for the type in question, as we rely on this here. */ |
5559 | utype = unsigned_type_for (etype); |
5560 | maxv = fold_convert (utype, TYPE_MAX_VALUE (etype)); |
5561 | maxv = range_binop (code: PLUS_EXPR, NULL_TREE, arg0: maxv, upper0_p: 1, |
5562 | arg1: build_int_cst (TREE_TYPE (maxv), 1), upper1_p: 1); |
5563 | minv = fold_convert (utype, TYPE_MIN_VALUE (etype)); |
5564 | |
5565 | if (integer_zerop (range_binop (code: NE_EXPR, integer_type_node, |
5566 | arg0: minv, upper0_p: 1, arg1: maxv, upper1_p: 1))) |
5567 | etype = utype; |
5568 | else |
5569 | return NULL_TREE; |
5570 | } |
5571 | else if (POINTER_TYPE_P (etype) |
5572 | || TREE_CODE (etype) == OFFSET_TYPE |
5573 | /* Right now all BITINT_TYPEs satisfy |
5574 | (unsigned) max + 1 == (unsigned) min, so no need to verify |
5575 | that like for INTEGER_TYPEs. */ |
5576 | || TREE_CODE (etype) == BITINT_TYPE) |
5577 | etype = unsigned_type_for (etype); |
5578 | return etype; |
5579 | } |
5580 | |
5581 | /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result |
5582 | type, TYPE, return an expression to test if EXP is in (or out of, depending |
5583 | on IN_P) the range. Return 0 if the test couldn't be created. */ |
5584 | |
5585 | tree |
5586 | build_range_check (location_t loc, tree type, tree exp, int in_p, |
5587 | tree low, tree high) |
5588 | { |
5589 | tree etype = TREE_TYPE (exp), mask, value; |
5590 | |
5591 | /* Disable this optimization for function pointer expressions |
5592 | on targets that require function pointer canonicalization. */ |
5593 | if (targetm.have_canonicalize_funcptr_for_compare () |
5594 | && POINTER_TYPE_P (etype) |
5595 | && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype))) |
5596 | return NULL_TREE; |
5597 | |
5598 | if (! in_p) |
5599 | { |
5600 | value = build_range_check (loc, type, exp, in_p: 1, low, high); |
5601 | if (value != 0) |
5602 | return invert_truthvalue_loc (loc, arg: value); |
5603 | |
5604 | return 0; |
5605 | } |
5606 | |
5607 | if (low == 0 && high == 0) |
5608 | return omit_one_operand_loc (loc, type, result: build_int_cst (type, 1), omitted: exp); |
5609 | |
5610 | if (low == 0) |
5611 | return fold_build2_loc (loc, LE_EXPR, type, exp, |
5612 | fold_convert_loc (loc, type: etype, arg: high)); |
5613 | |
5614 | if (high == 0) |
5615 | return fold_build2_loc (loc, GE_EXPR, type, exp, |
5616 | fold_convert_loc (loc, type: etype, arg: low)); |
5617 | |
5618 | if (operand_equal_p (arg0: low, arg1: high, flags: 0)) |
5619 | return fold_build2_loc (loc, EQ_EXPR, type, exp, |
5620 | fold_convert_loc (loc, type: etype, arg: low)); |
5621 | |
5622 | if (TREE_CODE (exp) == BIT_AND_EXPR |
5623 | && maskable_range_p (low, high, type: etype, mask: &mask, value: &value)) |
5624 | return fold_build2_loc (loc, EQ_EXPR, type, |
5625 | fold_build2_loc (loc, BIT_AND_EXPR, etype, |
5626 | exp, mask), |
5627 | value); |
5628 | |
5629 | if (integer_zerop (low)) |
5630 | { |
5631 | if (! TYPE_UNSIGNED (etype)) |
5632 | { |
5633 | etype = unsigned_type_for (etype); |
5634 | high = fold_convert_loc (loc, type: etype, arg: high); |
5635 | exp = fold_convert_loc (loc, type: etype, arg: exp); |
5636 | } |
5637 | return build_range_check (loc, type, exp, in_p: 1, low: 0, high); |
5638 | } |
5639 | |
5640 | /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */ |
5641 | if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST) |
5642 | { |
5643 | int prec = TYPE_PRECISION (etype); |
5644 | |
5645 | if (wi::mask <widest_int> (width: prec - 1, negate_p: false) == wi::to_widest (t: high)) |
5646 | { |
5647 | if (TYPE_UNSIGNED (etype)) |
5648 | { |
5649 | tree signed_etype = signed_type_for (etype); |
5650 | if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype)) |
5651 | etype |
5652 | = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0); |
5653 | else |
5654 | etype = signed_etype; |
5655 | exp = fold_convert_loc (loc, type: etype, arg: exp); |
5656 | } |
5657 | return fold_build2_loc (loc, GT_EXPR, type, exp, |
5658 | build_int_cst (etype, 0)); |
5659 | } |
5660 | } |
5661 | |
5662 | /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low). |
5663 | This requires wrap-around arithmetics for the type of the expression. */ |
5664 | etype = range_check_type (etype); |
5665 | if (etype == NULL_TREE) |
5666 | return NULL_TREE; |
5667 | |
5668 | high = fold_convert_loc (loc, type: etype, arg: high); |
5669 | low = fold_convert_loc (loc, type: etype, arg: low); |
5670 | exp = fold_convert_loc (loc, type: etype, arg: exp); |
5671 | |
5672 | value = const_binop (code: MINUS_EXPR, arg1: high, arg2: low); |
5673 | |
5674 | if (value != 0 && !TREE_OVERFLOW (value)) |
5675 | return build_range_check (loc, type, |
5676 | exp: fold_build2_loc (loc, MINUS_EXPR, etype, exp, low), |
5677 | in_p: 1, low: build_int_cst (etype, 0), high: value); |
5678 | |
5679 | return 0; |
5680 | } |
5681 | |
5682 | /* Return the predecessor of VAL in its type, handling the infinite case. */ |
5683 | |
5684 | static tree |
5685 | range_predecessor (tree val) |
5686 | { |
5687 | tree type = TREE_TYPE (val); |
5688 | |
5689 | if (INTEGRAL_TYPE_P (type) |
5690 | && operand_equal_p (arg0: val, TYPE_MIN_VALUE (type), flags: 0)) |
5691 | return 0; |
5692 | else |
5693 | return range_binop (code: MINUS_EXPR, NULL_TREE, arg0: val, upper0_p: 0, |
5694 | arg1: build_int_cst (TREE_TYPE (val), 1), upper1_p: 0); |
5695 | } |
5696 | |
5697 | /* Return the successor of VAL in its type, handling the infinite case. */ |
5698 | |
5699 | static tree |
5700 | range_successor (tree val) |
5701 | { |
5702 | tree type = TREE_TYPE (val); |
5703 | |
5704 | if (INTEGRAL_TYPE_P (type) |
5705 | && operand_equal_p (arg0: val, TYPE_MAX_VALUE (type), flags: 0)) |
5706 | return 0; |
5707 | else |
5708 | return range_binop (code: PLUS_EXPR, NULL_TREE, arg0: val, upper0_p: 0, |
5709 | arg1: build_int_cst (TREE_TYPE (val), 1), upper1_p: 0); |
5710 | } |
5711 | |
5712 | /* Given two ranges, see if we can merge them into one. Return 1 if we |
5713 | can, 0 if we can't. Set the output range into the specified parameters. */ |
5714 | |
5715 | bool |
5716 | merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0, |
5717 | tree high0, int in1_p, tree low1, tree high1) |
5718 | { |
5719 | bool no_overlap; |
5720 | int subset; |
5721 | int temp; |
5722 | tree tem; |
5723 | int in_p; |
5724 | tree low, high; |
5725 | int lowequal = ((low0 == 0 && low1 == 0) |
5726 | || integer_onep (range_binop (code: EQ_EXPR, integer_type_node, |
5727 | arg0: low0, upper0_p: 0, arg1: low1, upper1_p: 0))); |
5728 | int highequal = ((high0 == 0 && high1 == 0) |
5729 | || integer_onep (range_binop (code: EQ_EXPR, integer_type_node, |
5730 | arg0: high0, upper0_p: 1, arg1: high1, upper1_p: 1))); |
5731 | |
5732 | /* Make range 0 be the range that starts first, or ends last if they |
5733 | start at the same value. Swap them if it isn't. */ |
5734 | if (integer_onep (range_binop (code: GT_EXPR, integer_type_node, |
5735 | arg0: low0, upper0_p: 0, arg1: low1, upper1_p: 0)) |
5736 | || (lowequal |
5737 | && integer_onep (range_binop (code: GT_EXPR, integer_type_node, |
5738 | arg0: high1, upper0_p: 1, arg1: high0, upper1_p: 1)))) |
5739 | { |
5740 | temp = in0_p, in0_p = in1_p, in1_p = temp; |
5741 | tem = low0, low0 = low1, low1 = tem; |
5742 | tem = high0, high0 = high1, high1 = tem; |
5743 | } |
5744 | |
5745 | /* If the second range is != high1 where high1 is the type maximum of |
5746 | the type, try first merging with < high1 range. */ |
5747 | if (low1 |
5748 | && high1 |
5749 | && TREE_CODE (low1) == INTEGER_CST |
5750 | && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE |
5751 | || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE |
5752 | && known_eq (TYPE_PRECISION (TREE_TYPE (low1)), |
5753 | GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1)))))) |
5754 | && operand_equal_p (arg0: low1, arg1: high1, flags: 0)) |
5755 | { |
5756 | if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1))) |
5757 | && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, |
5758 | in1_p: !in1_p, NULL_TREE, high1: range_predecessor (val: low1))) |
5759 | return true; |
5760 | /* Similarly for the second range != low1 where low1 is the type minimum |
5761 | of the type, try first merging with > low1 range. */ |
5762 | if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1))) |
5763 | && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, |
5764 | in1_p: !in1_p, low1: range_successor (val: low1), NULL_TREE)) |
5765 | return true; |
5766 | } |
5767 | |
5768 | /* Now flag two cases, whether the ranges are disjoint or whether the |
5769 | second range is totally subsumed in the first. Note that the tests |
5770 | below are simplified by the ones above. */ |
5771 | no_overlap = integer_onep (range_binop (code: LT_EXPR, integer_type_node, |
5772 | arg0: high0, upper0_p: 1, arg1: low1, upper1_p: 0)); |
5773 | subset = integer_onep (range_binop (code: LE_EXPR, integer_type_node, |
5774 | arg0: high1, upper0_p: 1, arg1: high0, upper1_p: 1)); |
5775 | |
5776 | /* We now have four cases, depending on whether we are including or |
5777 | excluding the two ranges. */ |
5778 | if (in0_p && in1_p) |
5779 | { |
5780 | /* If they don't overlap, the result is false. If the second range |
5781 | is a subset it is the result. Otherwise, the range is from the start |
5782 | of the second to the end of the first. */ |
5783 | if (no_overlap) |
5784 | in_p = 0, low = high = 0; |
5785 | else if (subset) |
5786 | in_p = 1, low = low1, high = high1; |
5787 | else |
5788 | in_p = 1, low = low1, high = high0; |
5789 | } |
5790 | |
5791 | else if (in0_p && ! in1_p) |
5792 | { |
5793 | /* If they don't overlap, the result is the first range. If they are |
5794 | equal, the result is false. If the second range is a subset of the |
5795 | first, and the ranges begin at the same place, we go from just after |
5796 | the end of the second range to the end of the first. If the second |
5797 | range is not a subset of the first, or if it is a subset and both |
5798 | ranges end at the same place, the range starts at the start of the |
5799 | first range and ends just before the second range. |
5800 | Otherwise, we can't describe this as a single range. */ |
5801 | if (no_overlap) |
5802 | in_p = 1, low = low0, high = high0; |
5803 | else if (lowequal && highequal) |
5804 | in_p = 0, low = high = 0; |
5805 | else if (subset && lowequal) |
5806 | { |
5807 | low = range_successor (val: high1); |
5808 | high = high0; |
5809 | in_p = 1; |
5810 | if (low == 0) |
5811 | { |
5812 | /* We are in the weird situation where high0 > high1 but |
5813 | high1 has no successor. Punt. */ |
5814 | return 0; |
5815 | } |
5816 | } |
5817 | else if (! subset || highequal) |
5818 | { |
5819 | low = low0; |
5820 | high = range_predecessor (val: low1); |
5821 | in_p = 1; |
5822 | if (high == 0) |
5823 | { |
5824 | /* low0 < low1 but low1 has no predecessor. Punt. */ |
5825 | return 0; |
5826 | } |
5827 | } |
5828 | else |
5829 | return 0; |
5830 | } |
5831 | |
5832 | else if (! in0_p && in1_p) |
5833 | { |
5834 | /* If they don't overlap, the result is the second range. If the second |
5835 | is a subset of the first, the result is false. Otherwise, |
5836 | the range starts just after the first range and ends at the |
5837 | end of the second. */ |
5838 | if (no_overlap) |
5839 | in_p = 1, low = low1, high = high1; |
5840 | else if (subset || highequal) |
5841 | in_p = 0, low = high = 0; |
5842 | else |
5843 | { |
5844 | low = range_successor (val: high0); |
5845 | high = high1; |
5846 | in_p = 1; |
5847 | if (low == 0) |
5848 | { |
5849 | /* high1 > high0 but high0 has no successor. Punt. */ |
5850 | return 0; |
5851 | } |
5852 | } |
5853 | } |
5854 | |
5855 | else |
5856 | { |
5857 | /* The case where we are excluding both ranges. Here the complex case |
5858 | is if they don't overlap. In that case, the only time we have a |
5859 | range is if they are adjacent. If the second is a subset of the |
5860 | first, the result is the first. Otherwise, the range to exclude |
5861 | starts at the beginning of the first range and ends at the end of the |
5862 | second. */ |
5863 | if (no_overlap) |
5864 | { |
5865 | if (integer_onep (range_binop (code: EQ_EXPR, integer_type_node, |
5866 | arg0: range_successor (val: high0), |
5867 | upper0_p: 1, arg1: low1, upper1_p: 0))) |
5868 | in_p = 0, low = low0, high = high1; |
5869 | else |
5870 | { |
5871 | /* Canonicalize - [min, x] into - [-, x]. */ |
5872 | if (low0 && TREE_CODE (low0) == INTEGER_CST) |
5873 | switch (TREE_CODE (TREE_TYPE (low0))) |
5874 | { |
5875 | case ENUMERAL_TYPE: |
5876 | if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)), |
5877 | b: GET_MODE_BITSIZE |
5878 | (TYPE_MODE (TREE_TYPE (low0))))) |
5879 | break; |
5880 | /* FALLTHROUGH */ |
5881 | case INTEGER_TYPE: |
5882 | if (tree_int_cst_equal (low0, |
5883 | TYPE_MIN_VALUE (TREE_TYPE (low0)))) |
5884 | low0 = 0; |
5885 | break; |
5886 | case POINTER_TYPE: |
5887 | if (TYPE_UNSIGNED (TREE_TYPE (low0)) |
5888 | && integer_zerop (low0)) |
5889 | low0 = 0; |
5890 | break; |
5891 | default: |
5892 | break; |
5893 | } |
5894 | |
5895 | /* Canonicalize - [x, max] into - [x, -]. */ |
5896 | if (high1 && TREE_CODE (high1) == INTEGER_CST) |
5897 | switch (TREE_CODE (TREE_TYPE (high1))) |
5898 | { |
5899 | case ENUMERAL_TYPE: |
5900 | if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)), |
5901 | b: GET_MODE_BITSIZE |
5902 | (TYPE_MODE (TREE_TYPE (high1))))) |
5903 | break; |
5904 | /* FALLTHROUGH */ |
5905 | case INTEGER_TYPE: |
5906 | if (tree_int_cst_equal (high1, |
5907 | TYPE_MAX_VALUE (TREE_TYPE (high1)))) |
5908 | high1 = 0; |
5909 | break; |
5910 | case POINTER_TYPE: |
5911 | if (TYPE_UNSIGNED (TREE_TYPE (high1)) |
5912 | && integer_zerop (range_binop (code: PLUS_EXPR, NULL_TREE, |
5913 | arg0: high1, upper0_p: 1, |
5914 | arg1: build_int_cst (TREE_TYPE (high1), 1), |
5915 | upper1_p: 1))) |
5916 | high1 = 0; |
5917 | break; |
5918 | default: |
5919 | break; |
5920 | } |
5921 | |
5922 | /* The ranges might be also adjacent between the maximum and |
5923 | minimum values of the given type. For |
5924 | - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y |
5925 | return + [x + 1, y - 1]. */ |
5926 | if (low0 == 0 && high1 == 0) |
5927 | { |
5928 | low = range_successor (val: high0); |
5929 | high = range_predecessor (val: low1); |
5930 | if (low == 0 || high == 0) |
5931 | return 0; |
5932 | |
5933 | in_p = 1; |
5934 | } |
5935 | else |
5936 | return 0; |
5937 | } |
5938 | } |
5939 | else if (subset) |
5940 | in_p = 0, low = low0, high = high0; |
5941 | else |
5942 | in_p = 0, low = low0, high = high1; |
5943 | } |
5944 | |
5945 | *pin_p = in_p, *plow = low, *phigh = high; |
5946 | return 1; |
5947 | } |
5948 | |
5949 | |
5950 | /* Subroutine of fold, looking inside expressions of the form |
5951 | A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2 |
5952 | are the three operands of the COND_EXPR. This function is |
5953 | being used also to optimize A op B ? C : A, by reversing the |
5954 | comparison first. |
5955 | |
5956 | Return a folded expression whose code is not a COND_EXPR |
5957 | anymore, or NULL_TREE if no folding opportunity is found. */ |
5958 | |
5959 | static tree |
5960 | fold_cond_expr_with_comparison (location_t loc, tree type, |
5961 | enum tree_code comp_code, |
5962 | tree arg00, tree arg01, tree arg1, tree arg2) |
5963 | { |
5964 | tree arg1_type = TREE_TYPE (arg1); |
5965 | tree tem; |
5966 | |
5967 | STRIP_NOPS (arg1); |
5968 | STRIP_NOPS (arg2); |
5969 | |
5970 | /* If we have A op 0 ? A : -A, consider applying the following |
5971 | transformations: |
5972 | |
5973 | A == 0? A : -A same as -A |
5974 | A != 0? A : -A same as A |
5975 | A >= 0? A : -A same as abs (A) |
5976 | A > 0? A : -A same as abs (A) |
5977 | A <= 0? A : -A same as -abs (A) |
5978 | A < 0? A : -A same as -abs (A) |
5979 | |
5980 | None of these transformations work for modes with signed |
5981 | zeros. If A is +/-0, the first two transformations will |
5982 | change the sign of the result (from +0 to -0, or vice |
5983 | versa). The last four will fix the sign of the result, |
5984 | even though the original expressions could be positive or |
5985 | negative, depending on the sign of A. |
5986 | |
5987 | Note that all these transformations are correct if A is |
5988 | NaN, since the two alternatives (A and -A) are also NaNs. */ |
5989 | if (!HONOR_SIGNED_ZEROS (type) |
5990 | && (FLOAT_TYPE_P (TREE_TYPE (arg01)) |
5991 | ? real_zerop (arg01) |
5992 | : integer_zerop (arg01)) |
5993 | && ((TREE_CODE (arg2) == NEGATE_EXPR |
5994 | && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, flags: 0)) |
5995 | /* In the case that A is of the form X-Y, '-A' (arg2) may |
5996 | have already been folded to Y-X, check for that. */ |
5997 | || (TREE_CODE (arg1) == MINUS_EXPR |
5998 | && TREE_CODE (arg2) == MINUS_EXPR |
5999 | && operand_equal_p (TREE_OPERAND (arg1, 0), |
6000 | TREE_OPERAND (arg2, 1), flags: 0) |
6001 | && operand_equal_p (TREE_OPERAND (arg1, 1), |
6002 | TREE_OPERAND (arg2, 0), flags: 0)))) |
6003 | switch (comp_code) |
6004 | { |
6005 | case EQ_EXPR: |
6006 | case UNEQ_EXPR: |
6007 | tem = fold_convert_loc (loc, type: arg1_type, arg: arg1); |
6008 | return fold_convert_loc (loc, type, arg: negate_expr (t: tem)); |
6009 | case NE_EXPR: |
6010 | case LTGT_EXPR: |
6011 | return fold_convert_loc (loc, type, arg: arg1); |
6012 | case UNGE_EXPR: |
6013 | case UNGT_EXPR: |
6014 | if (flag_trapping_math) |
6015 | break; |
6016 | /* Fall through. */ |
6017 | case GE_EXPR: |
6018 | case GT_EXPR: |
6019 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
6020 | break; |
6021 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
6022 | return fold_convert_loc (loc, type, arg: tem); |
6023 | case UNLE_EXPR: |
6024 | case UNLT_EXPR: |
6025 | if (flag_trapping_math) |
6026 | break; |
6027 | /* FALLTHRU */ |
6028 | case LE_EXPR: |
6029 | case LT_EXPR: |
6030 | if (TYPE_UNSIGNED (TREE_TYPE (arg1))) |
6031 | break; |
6032 | if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1)) |
6033 | && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))) |
6034 | { |
6035 | /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN) |
6036 | is not, invokes UB both in abs and in the negation of it. |
6037 | So, use ABSU_EXPR instead. */ |
6038 | tree utype = unsigned_type_for (TREE_TYPE (arg1)); |
6039 | tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1); |
6040 | tem = negate_expr (t: tem); |
6041 | return fold_convert_loc (loc, type, arg: tem); |
6042 | } |
6043 | else |
6044 | { |
6045 | tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1); |
6046 | return negate_expr (t: fold_convert_loc (loc, type, arg: tem)); |
6047 | } |
6048 | default: |
6049 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
6050 | break; |
6051 | } |
6052 | |
6053 | /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise |
6054 | A == 0 ? A : 0 is always 0 unless A is -0. Note that |
6055 | both transformations are correct when A is NaN: A != 0 |
6056 | is then true, and A == 0 is false. */ |
6057 | |
6058 | if (!HONOR_SIGNED_ZEROS (type) |
6059 | && integer_zerop (arg01) && integer_zerop (arg2)) |
6060 | { |
6061 | if (comp_code == NE_EXPR) |
6062 | return fold_convert_loc (loc, type, arg: arg1); |
6063 | else if (comp_code == EQ_EXPR) |
6064 | return build_zero_cst (type); |
6065 | } |
6066 | |
6067 | /* Try some transformations of A op B ? A : B. |
6068 | |
6069 | A == B? A : B same as B |
6070 | A != B? A : B same as A |
6071 | A >= B? A : B same as max (A, B) |
6072 | A > B? A : B same as max (B, A) |
6073 | A <= B? A : B same as min (A, B) |
6074 | A < B? A : B same as min (B, A) |
6075 | |
6076 | As above, these transformations don't work in the presence |
6077 | of signed zeros. For example, if A and B are zeros of |
6078 | opposite sign, the first two transformations will change |
6079 | the sign of the result. In the last four, the original |
6080 | expressions give different results for (A=+0, B=-0) and |
6081 | (A=-0, B=+0), but the transformed expressions do not. |
6082 | |
6083 | The first two transformations are correct if either A or B |
6084 | is a NaN. In the first transformation, the condition will |
6085 | be false, and B will indeed be chosen. In the case of the |
6086 | second transformation, the condition A != B will be true, |
6087 | and A will be chosen. |
6088 | |
6089 | The conversions to max() and min() are not correct if B is |
6090 | a number and A is not. The conditions in the original |
6091 | expressions will be false, so all four give B. The min() |
6092 | and max() versions would give a NaN instead. */ |
6093 | if (!HONOR_SIGNED_ZEROS (type) |
6094 | && operand_equal_for_comparison_p (arg0: arg01, arg1: arg2) |
6095 | /* Avoid these transformations if the COND_EXPR may be used |
6096 | as an lvalue in the C++ front-end. PR c++/19199. */ |
6097 | && (in_gimple_form |
6098 | || VECTOR_TYPE_P (type) |
6099 | || (! lang_GNU_CXX () |
6100 | && strcmp (s1: lang_hooks.name, s2: "GNU Objective-C++" ) != 0) |
6101 | || ! maybe_lvalue_p (x: arg1) |
6102 | || ! maybe_lvalue_p (x: arg2))) |
6103 | { |
6104 | tree comp_op0 = arg00; |
6105 | tree comp_op1 = arg01; |
6106 | tree comp_type = TREE_TYPE (comp_op0); |
6107 | |
6108 | switch (comp_code) |
6109 | { |
6110 | case EQ_EXPR: |
6111 | return fold_convert_loc (loc, type, arg: arg2); |
6112 | case NE_EXPR: |
6113 | return fold_convert_loc (loc, type, arg: arg1); |
6114 | case LE_EXPR: |
6115 | case LT_EXPR: |
6116 | case UNLE_EXPR: |
6117 | case UNLT_EXPR: |
6118 | /* In C++ a ?: expression can be an lvalue, so put the |
6119 | operand which will be used if they are equal first |
6120 | so that we can convert this back to the |
6121 | corresponding COND_EXPR. */ |
6122 | if (!HONOR_NANS (arg1)) |
6123 | { |
6124 | comp_op0 = fold_convert_loc (loc, type: comp_type, arg: comp_op0); |
6125 | comp_op1 = fold_convert_loc (loc, type: comp_type, arg: comp_op1); |
6126 | tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR) |
6127 | ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1) |
6128 | : fold_build2_loc (loc, MIN_EXPR, comp_type, |
6129 | comp_op1, comp_op0); |
6130 | return fold_convert_loc (loc, type, arg: tem); |
6131 | } |
6132 | break; |
6133 | case GE_EXPR: |
6134 | case GT_EXPR: |
6135 | case UNGE_EXPR: |
6136 | case UNGT_EXPR: |
6137 | if (!HONOR_NANS (arg1)) |
6138 | { |
6139 | comp_op0 = fold_convert_loc (loc, type: comp_type, arg: comp_op0); |
6140 | comp_op1 = fold_convert_loc (loc, type: comp_type, arg: comp_op1); |
6141 | tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR) |
6142 | ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1) |
6143 | : fold_build2_loc (loc, MAX_EXPR, comp_type, |
6144 | comp_op1, comp_op0); |
6145 | return fold_convert_loc (loc, type, arg: tem); |
6146 | } |
6147 | break; |
6148 | case UNEQ_EXPR: |
6149 | if (!HONOR_NANS (arg1)) |
6150 | return fold_convert_loc (loc, type, arg: arg2); |
6151 | break; |
6152 | case LTGT_EXPR: |
6153 | if (!HONOR_NANS (arg1)) |
6154 | return fold_convert_loc (loc, type, arg: arg1); |
6155 | break; |
6156 | default: |
6157 | gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison); |
6158 | break; |
6159 | } |
6160 | } |
6161 | |
6162 | return NULL_TREE; |
6163 | } |
6164 | |
6165 | |
6166 | |
6167 | #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT |
6168 | #define LOGICAL_OP_NON_SHORT_CIRCUIT \ |
6169 | (BRANCH_COST (optimize_function_for_speed_p (cfun), \ |
6170 | false) >= 2) |
6171 | #endif |
6172 | |
6173 | /* EXP is some logical combination of boolean tests. See if we can |
6174 | merge it into some range test. Return the new tree if so. */ |
6175 | |
6176 | static tree |
6177 | fold_range_test (location_t loc, enum tree_code code, tree type, |
6178 | tree op0, tree op1) |
6179 | { |
6180 | int or_op = (code == TRUTH_ORIF_EXPR |
6181 | || code == TRUTH_OR_EXPR); |
6182 | int in0_p, in1_p, in_p; |
6183 | tree low0, low1, low, high0, high1, high; |
6184 | bool strict_overflow_p = false; |
6185 | tree tem, lhs, rhs; |
6186 | const char * const warnmsg = G_("assuming signed overflow does not occur " |
6187 | "when simplifying range test" ); |
6188 | |
6189 | if (!INTEGRAL_TYPE_P (type)) |
6190 | return 0; |
6191 | |
6192 | lhs = make_range (exp: op0, pin_p: &in0_p, plow: &low0, phigh: &high0, strict_overflow_p: &strict_overflow_p); |
6193 | /* If op0 is known true or false and this is a short-circuiting |
6194 | operation we must not merge with op1 since that makes side-effects |
6195 | unconditional. So special-case this. */ |
6196 | if (!lhs |
6197 | && ((code == TRUTH_ORIF_EXPR && in0_p) |
6198 | || (code == TRUTH_ANDIF_EXPR && !in0_p))) |
6199 | return op0; |
6200 | rhs = make_range (exp: op1, pin_p: &in1_p, plow: &low1, phigh: &high1, strict_overflow_p: &strict_overflow_p); |
6201 | |
6202 | /* If this is an OR operation, invert both sides; we will invert |
6203 | again at the end. */ |
6204 | if (or_op) |
6205 | in0_p = ! in0_p, in1_p = ! in1_p; |
6206 | |
6207 | /* If both expressions are the same, if we can merge the ranges, and we |
6208 | can build the range test, return it or it inverted. If one of the |
6209 | ranges is always true or always false, consider it to be the same |
6210 | expression as the other. */ |
6211 | if ((lhs == 0 || rhs == 0 || operand_equal_p (arg0: lhs, arg1: rhs, flags: 0)) |
6212 | && merge_ranges (pin_p: &in_p, plow: &low, phigh: &high, in0_p, low0, high0, |
6213 | in1_p, low1, high1) |
6214 | && (tem = (build_range_check (loc, type, |
6215 | exp: lhs != 0 ? lhs |
6216 | : rhs != 0 ? rhs : integer_zero_node, |
6217 | in_p, low, high))) != 0) |
6218 | { |
6219 | if (strict_overflow_p) |
6220 | fold_overflow_warning (gmsgid: warnmsg, wc: WARN_STRICT_OVERFLOW_COMPARISON); |
6221 | return or_op ? invert_truthvalue_loc (loc, arg: tem) : tem; |
6222 | } |
6223 | |
6224 | /* On machines where the branch cost is expensive, if this is a |
6225 | short-circuited branch and the underlying object on both sides |
6226 | is the same, make a non-short-circuit operation. */ |
6227 | bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT; |
6228 | if (param_logical_op_non_short_circuit != -1) |
6229 | logical_op_non_short_circuit |
6230 | = param_logical_op_non_short_circuit; |
6231 | if (logical_op_non_short_circuit |
6232 | && !sanitize_coverage_p () |
6233 | && lhs != 0 && rhs != 0 |
6234 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) |
6235 | && operand_equal_p (arg0: lhs, arg1: rhs, flags: 0)) |
6236 | { |
6237 | /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR |
6238 | unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in |
6239 | which cases we can't do this. */ |
6240 | if (simple_operand_p (exp: lhs)) |
6241 | return build2_loc (loc, code: code == TRUTH_ANDIF_EXPR |
6242 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, |
6243 | type, arg0: op0, arg1: op1); |
6244 | |
6245 | else if (!lang_hooks.decls.global_bindings_p () |
6246 | && !CONTAINS_PLACEHOLDER_P (lhs)) |
6247 | { |
6248 | tree common = save_expr (lhs); |
6249 | |
6250 | if ((lhs = build_range_check (loc, type, exp: common, |
6251 | in_p: or_op ? ! in0_p : in0_p, |
6252 | low: low0, high: high0)) != 0 |
6253 | && (rhs = build_range_check (loc, type, exp: common, |
6254 | in_p: or_op ? ! in1_p : in1_p, |
6255 | low: low1, high: high1)) != 0) |
6256 | { |
6257 | if (strict_overflow_p) |
6258 | fold_overflow_warning (gmsgid: warnmsg, |
6259 | wc: WARN_STRICT_OVERFLOW_COMPARISON); |
6260 | return build2_loc (loc, code: code == TRUTH_ANDIF_EXPR |
6261 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR, |
6262 | type, arg0: lhs, arg1: rhs); |
6263 | } |
6264 | } |
6265 | } |
6266 | |
6267 | return 0; |
6268 | } |
6269 | |
6270 | /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P |
6271 | bit value. Arrange things so the extra bits will be set to zero if and |
6272 | only if C is signed-extended to its full width. If MASK is nonzero, |
6273 | it is an INTEGER_CST that should be AND'ed with the extra bits. */ |
6274 | |
6275 | static tree |
6276 | unextend (tree c, int p, int unsignedp, tree mask) |
6277 | { |
6278 | tree type = TREE_TYPE (c); |
6279 | int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type)); |
6280 | tree temp; |
6281 | |
6282 | if (p == modesize || unsignedp) |
6283 | return c; |
6284 | |
6285 | /* We work by getting just the sign bit into the low-order bit, then |
6286 | into the high-order bit, then sign-extend. We then XOR that value |
6287 | with C. */ |
6288 | temp = build_int_cst (TREE_TYPE (c), |
6289 | wi::extract_uhwi (x: wi::to_wide (t: c), bitpos: p - 1, width: 1)); |
6290 | |
6291 | /* We must use a signed type in order to get an arithmetic right shift. |
6292 | However, we must also avoid introducing accidental overflows, so that |
6293 | a subsequent call to integer_zerop will work. Hence we must |
6294 | do the type conversion here. At this point, the constant is either |
6295 | zero or one, and the conversion to a signed type can never overflow. |
6296 | We could get an overflow if this conversion is done anywhere else. */ |
6297 | if (TYPE_UNSIGNED (type)) |
6298 | temp = fold_convert (signed_type_for (type), temp); |
6299 | |
6300 | temp = const_binop (code: LSHIFT_EXPR, arg1: temp, size_int (modesize - 1)); |
6301 | temp = const_binop (code: RSHIFT_EXPR, arg1: temp, size_int (modesize - p - 1)); |
6302 | if (mask != 0) |
6303 | temp = const_binop (code: BIT_AND_EXPR, arg1: temp, |
6304 | fold_convert (TREE_TYPE (c), mask)); |
6305 | /* If necessary, convert the type back to match the type of C. */ |
6306 | if (TYPE_UNSIGNED (type)) |
6307 | temp = fold_convert (type, temp); |
6308 | |
6309 | return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp)); |
6310 | } |
6311 | |
6312 | /* For an expression that has the form |
6313 | (A && B) || ~B |
6314 | or |
6315 | (A || B) && ~B, |
6316 | we can drop one of the inner expressions and simplify to |
6317 | A || ~B |
6318 | or |
6319 | A && ~B |
6320 | LOC is the location of the resulting expression. OP is the inner |
6321 | logical operation; the left-hand side in the examples above, while CMPOP |
6322 | is the right-hand side. RHS_ONLY is used to prevent us from accidentally |
6323 | removing a condition that guards another, as in |
6324 | (A != NULL && A->...) || A == NULL |
6325 | which we must not transform. If RHS_ONLY is true, only eliminate the |
6326 | right-most operand of the inner logical operation. */ |
6327 | |
6328 | static tree |
6329 | merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop, |
6330 | bool rhs_only) |
6331 | { |
6332 | tree type = TREE_TYPE (cmpop); |
6333 | enum tree_code code = TREE_CODE (cmpop); |
6334 | enum tree_code truthop_code = TREE_CODE (op); |
6335 | tree lhs = TREE_OPERAND (op, 0); |
6336 | tree rhs = TREE_OPERAND (op, 1); |
6337 | tree orig_lhs = lhs, orig_rhs = rhs; |
6338 | enum tree_code rhs_code = TREE_CODE (rhs); |
6339 | enum tree_code lhs_code = TREE_CODE (lhs); |
6340 | enum tree_code inv_code; |
6341 | |
6342 | if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop)) |
6343 | return NULL_TREE; |
6344 | |
6345 | if (TREE_CODE_CLASS (code) != tcc_comparison) |
6346 | return NULL_TREE; |
6347 | |
6348 | if (rhs_code == truthop_code) |
6349 | { |
6350 | tree newrhs = merge_truthop_with_opposite_arm (loc, op: rhs, cmpop, rhs_only); |
6351 | if (newrhs != NULL_TREE) |
6352 | { |
6353 | rhs = newrhs; |
6354 | rhs_code = TREE_CODE (rhs); |
6355 | } |
6356 | } |
6357 | if (lhs_code == truthop_code && !rhs_only) |
6358 | { |
6359 | tree newlhs = merge_truthop_with_opposite_arm (loc, op: lhs, cmpop, rhs_only: false); |
6360 | if (newlhs != NULL_TREE) |
6361 | { |
6362 | lhs = newlhs; |
6363 | lhs_code = TREE_CODE (lhs); |
6364 | } |
6365 | } |
6366 | |
6367 | inv_code = invert_tree_comparison (code, honor_nans: HONOR_NANS (type)); |
6368 | if (inv_code == rhs_code |
6369 | && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), flags: 0) |
6370 | && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), flags: 0)) |
6371 | return lhs; |
6372 | if (!rhs_only && inv_code == lhs_code |
6373 | && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), flags: 0) |
6374 | && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), flags: 0)) |
6375 | return rhs; |
6376 | if (rhs != orig_rhs || lhs != orig_lhs) |
6377 | return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop), |
6378 | lhs, rhs); |
6379 | return NULL_TREE; |
6380 | } |
6381 | |
6382 | /* Find ways of folding logical expressions of LHS and RHS: |
6383 | Try to merge two comparisons to the same innermost item. |
6384 | Look for range tests like "ch >= '0' && ch <= '9'". |
6385 | Look for combinations of simple terms on machines with expensive branches |
6386 | and evaluate the RHS unconditionally. |
6387 | |
6388 | For example, if we have p->a == 2 && p->b == 4 and we can make an |
6389 | object large enough to span both A and B, we can do this with a comparison |
6390 | against the object ANDed with the a mask. |
6391 | |
6392 | If we have p->a == q->a && p->b == q->b, we may be able to use bit masking |
6393 | operations to do this with one comparison. |
6394 | |
6395 | We check for both normal comparisons and the BIT_AND_EXPRs made this by |
6396 | function and the one above. |
6397 | |
6398 | CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR, |
6399 | TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR. |
6400 | |
6401 | TRUTH_TYPE is the type of the logical operand and LHS and RHS are its |
6402 | two operands. |
6403 | |
6404 | We return the simplified tree or 0 if no optimization is possible. */ |
6405 | |
6406 | static tree |
6407 | fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type, |
6408 | tree lhs, tree rhs) |
6409 | { |
6410 | /* If this is the "or" of two comparisons, we can do something if |
6411 | the comparisons are NE_EXPR. If this is the "and", we can do something |
6412 | if the comparisons are EQ_EXPR. I.e., |
6413 | (a->b == 2 && a->c == 4) can become (a->new == NEW). |
6414 | |
6415 | WANTED_CODE is this operation code. For single bit fields, we can |
6416 | convert EQ_EXPR to NE_EXPR so we need not reject the "wrong" |
6417 | comparison for one-bit fields. */ |
6418 | |
6419 | enum tree_code wanted_code; |
6420 | enum tree_code lcode, rcode; |
6421 | tree ll_arg, lr_arg, rl_arg, rr_arg; |
6422 | tree ll_inner, lr_inner, rl_inner, rr_inner; |
6423 | HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos; |
6424 | HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos; |
6425 | HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos; |
6426 | HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos; |
6427 | int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp; |
6428 | int ll_reversep, lr_reversep, rl_reversep, rr_reversep; |
6429 | machine_mode ll_mode, lr_mode, rl_mode, rr_mode; |
6430 | scalar_int_mode lnmode, rnmode; |
6431 | tree ll_mask, lr_mask, rl_mask, rr_mask; |
6432 | tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask; |
6433 | tree l_const, r_const; |
6434 | tree lntype, rntype, result; |
6435 | HOST_WIDE_INT first_bit, end_bit; |
6436 | int volatilep; |
6437 | |
6438 | /* Start by getting the comparison codes. Fail if anything is volatile. |
6439 | If one operand is a BIT_AND_EXPR with the constant one, treat it as if |
6440 | it were surrounded with a NE_EXPR. */ |
6441 | |
6442 | if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs)) |
6443 | return 0; |
6444 | |
6445 | lcode = TREE_CODE (lhs); |
6446 | rcode = TREE_CODE (rhs); |
6447 | |
6448 | if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1))) |
6449 | { |
6450 | lhs = build2 (NE_EXPR, truth_type, lhs, |
6451 | build_int_cst (TREE_TYPE (lhs), 0)); |
6452 | lcode = NE_EXPR; |
6453 | } |
6454 | |
6455 | if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1))) |
6456 | { |
6457 | rhs = build2 (NE_EXPR, truth_type, rhs, |
6458 | build_int_cst (TREE_TYPE (rhs), 0)); |
6459 | rcode = NE_EXPR; |
6460 | } |
6461 | |
6462 | if (TREE_CODE_CLASS (lcode) != tcc_comparison |
6463 | || TREE_CODE_CLASS (rcode) != tcc_comparison) |
6464 | return 0; |
6465 | |
6466 | ll_arg = TREE_OPERAND (lhs, 0); |
6467 | lr_arg = TREE_OPERAND (lhs, 1); |
6468 | rl_arg = TREE_OPERAND (rhs, 0); |
6469 | rr_arg = TREE_OPERAND (rhs, 1); |
6470 | |
6471 | /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */ |
6472 | if (simple_operand_p (exp: ll_arg) |
6473 | && simple_operand_p (exp: lr_arg)) |
6474 | { |
6475 | if (operand_equal_p (arg0: ll_arg, arg1: rl_arg, flags: 0) |
6476 | && operand_equal_p (arg0: lr_arg, arg1: rr_arg, flags: 0)) |
6477 | { |
6478 | result = combine_comparisons (loc, code, lcode, rcode, |
6479 | truth_type, ll_arg, lr_arg); |
6480 | if (result) |
6481 | return result; |
6482 | } |
6483 | else if (operand_equal_p (arg0: ll_arg, arg1: rr_arg, flags: 0) |
6484 | && operand_equal_p (arg0: lr_arg, arg1: rl_arg, flags: 0)) |
6485 | { |
6486 | result = combine_comparisons (loc, code, lcode, |
6487 | rcode: swap_tree_comparison (code: rcode), |
6488 | truth_type, ll_arg, lr_arg); |
6489 | if (result) |
6490 | return result; |
6491 | } |
6492 | } |
6493 | |
6494 | code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) |
6495 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR); |
6496 | |
6497 | /* If the RHS can be evaluated unconditionally and its operands are |
6498 | simple, it wins to evaluate the RHS unconditionally on machines |
6499 | with expensive branches. In this case, this isn't a comparison |
6500 | that can be merged. */ |
6501 | |
6502 | if (BRANCH_COST (optimize_function_for_speed_p (cfun), |
6503 | false) >= 2 |
6504 | && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg)) |
6505 | && simple_operand_p (exp: rl_arg) |
6506 | && simple_operand_p (exp: rr_arg)) |
6507 | { |
6508 | /* Convert (a != 0) || (b != 0) into (a | b) != 0. */ |
6509 | if (code == TRUTH_OR_EXPR |
6510 | && lcode == NE_EXPR && integer_zerop (lr_arg) |
6511 | && rcode == NE_EXPR && integer_zerop (rr_arg) |
6512 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
6513 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) |
6514 | return build2_loc (loc, code: NE_EXPR, type: truth_type, |
6515 | arg0: build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), |
6516 | ll_arg, rl_arg), |
6517 | arg1: build_int_cst (TREE_TYPE (ll_arg), 0)); |
6518 | |
6519 | /* Convert (a == 0) && (b == 0) into (a | b) == 0. */ |
6520 | if (code == TRUTH_AND_EXPR |
6521 | && lcode == EQ_EXPR && integer_zerop (lr_arg) |
6522 | && rcode == EQ_EXPR && integer_zerop (rr_arg) |
6523 | && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg) |
6524 | && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg))) |
6525 | return build2_loc (loc, code: EQ_EXPR, type: truth_type, |
6526 | arg0: build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg), |
6527 | ll_arg, rl_arg), |
6528 | arg1: build_int_cst (TREE_TYPE (ll_arg), 0)); |
6529 | } |
6530 | |
6531 | /* See if the comparisons can be merged. Then get all the parameters for |
6532 | each side. */ |
6533 | |
6534 | if ((lcode != EQ_EXPR && lcode != NE_EXPR) |
6535 | || (rcode != EQ_EXPR && rcode != NE_EXPR)) |
6536 | return 0; |
6537 | |
6538 | ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0; |
6539 | volatilep = 0; |
6540 | ll_inner = decode_field_reference (loc, exp_: &ll_arg, |
6541 | pbitsize: &ll_bitsize, pbitpos: &ll_bitpos, pmode: &ll_mode, |
6542 | punsignedp: &ll_unsignedp, preversep: &ll_reversep, pvolatilep: &volatilep, |
6543 | pmask: &ll_mask, pand_mask: &ll_and_mask); |
6544 | lr_inner = decode_field_reference (loc, exp_: &lr_arg, |
6545 | pbitsize: &lr_bitsize, pbitpos: &lr_bitpos, pmode: &lr_mode, |
6546 | punsignedp: &lr_unsignedp, preversep: &lr_reversep, pvolatilep: &volatilep, |
6547 | pmask: &lr_mask, pand_mask: &lr_and_mask); |
6548 | rl_inner = decode_field_reference (loc, exp_: &rl_arg, |
6549 | pbitsize: &rl_bitsize, pbitpos: &rl_bitpos, pmode: &rl_mode, |
6550 | punsignedp: &rl_unsignedp, preversep: &rl_reversep, pvolatilep: &volatilep, |
6551 | pmask: &rl_mask, pand_mask: &rl_and_mask); |
6552 | rr_inner = decode_field_reference (loc, exp_: &rr_arg, |
6553 | pbitsize: &rr_bitsize, pbitpos: &rr_bitpos, pmode: &rr_mode, |
6554 | punsignedp: &rr_unsignedp, preversep: &rr_reversep, pvolatilep: &volatilep, |
6555 | pmask: &rr_mask, pand_mask: &rr_and_mask); |
6556 | |
6557 | /* It must be true that the inner operation on the lhs of each |
6558 | comparison must be the same if we are to be able to do anything. |
6559 | Then see if we have constants. If not, the same must be true for |
6560 | the rhs's. */ |
6561 | if (volatilep |
6562 | || ll_reversep != rl_reversep |
6563 | || ll_inner == 0 || rl_inner == 0 |
6564 | || ! operand_equal_p (arg0: ll_inner, arg1: rl_inner, flags: 0)) |
6565 | return 0; |
6566 | |
6567 | if (TREE_CODE (lr_arg) == INTEGER_CST |
6568 | && TREE_CODE (rr_arg) == INTEGER_CST) |
6569 | { |
6570 | l_const = lr_arg, r_const = rr_arg; |
6571 | lr_reversep = ll_reversep; |
6572 | } |
6573 | else if (lr_reversep != rr_reversep |
6574 | || lr_inner == 0 || rr_inner == 0 |
6575 | || ! operand_equal_p (arg0: lr_inner, arg1: rr_inner, flags: 0)) |
6576 | return 0; |
6577 | else |
6578 | l_const = r_const = 0; |
6579 | |
6580 | /* If either comparison code is not correct for our logical operation, |
6581 | fail. However, we can convert a one-bit comparison against zero into |
6582 | the opposite comparison against that bit being set in the field. */ |
6583 | |
6584 | wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR); |
6585 | if (lcode != wanted_code) |
6586 | { |
6587 | if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask)) |
6588 | { |
6589 | /* Make the left operand unsigned, since we are only interested |
6590 | in the value of one bit. Otherwise we are doing the wrong |
6591 | thing below. */ |
6592 | ll_unsignedp = 1; |
6593 | l_const = ll_mask; |
6594 | } |
6595 | else |
6596 | return 0; |
6597 | } |
6598 | |
6599 | /* This is analogous to the code for l_const above. */ |
6600 | if (rcode != wanted_code) |
6601 | { |
6602 | if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask)) |
6603 | { |
6604 | rl_unsignedp = 1; |
6605 | r_const = rl_mask; |
6606 | } |
6607 | else |
6608 | return 0; |
6609 | } |
6610 | |
6611 | /* See if we can find a mode that contains both fields being compared on |
6612 | the left. If we can't, fail. Otherwise, update all constants and masks |
6613 | to be relative to a field of that size. */ |
6614 | first_bit = MIN (ll_bitpos, rl_bitpos); |
6615 | end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize); |
6616 | if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0, |
6617 | TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD, |
6618 | volatilep, &lnmode)) |
6619 | return 0; |
6620 | |
6621 | lnbitsize = GET_MODE_BITSIZE (mode: lnmode); |
6622 | lnbitpos = first_bit & ~ (lnbitsize - 1); |
6623 | lntype = lang_hooks.types.type_for_size (lnbitsize, 1); |
6624 | xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos; |
6625 | |
6626 | if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN) |
6627 | { |
6628 | xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize; |
6629 | xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize; |
6630 | } |
6631 | |
6632 | ll_mask = const_binop (code: LSHIFT_EXPR, arg1: fold_convert_loc (loc, type: lntype, arg: ll_mask), |
6633 | size_int (xll_bitpos)); |
6634 | rl_mask = const_binop (code: LSHIFT_EXPR, arg1: fold_convert_loc (loc, type: lntype, arg: rl_mask), |
6635 | size_int (xrl_bitpos)); |
6636 | if (ll_mask == NULL_TREE || rl_mask == NULL_TREE) |
6637 | return 0; |
6638 | |
6639 | if (l_const) |
6640 | { |
6641 | l_const = fold_convert_loc (loc, type: lntype, arg: l_const); |
6642 | l_const = unextend (c: l_const, p: ll_bitsize, unsignedp: ll_unsignedp, mask: ll_and_mask); |
6643 | l_const = const_binop (code: LSHIFT_EXPR, arg1: l_const, size_int (xll_bitpos)); |
6644 | if (l_const == NULL_TREE) |
6645 | return 0; |
6646 | if (! integer_zerop (const_binop (code: BIT_AND_EXPR, arg1: l_const, |
6647 | arg2: fold_build1_loc (loc, BIT_NOT_EXPR, |
6648 | lntype, ll_mask)))) |
6649 | { |
6650 | warning (0, "comparison is always %d" , wanted_code == NE_EXPR); |
6651 | |
6652 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
6653 | } |
6654 | } |
6655 | if (r_const) |
6656 | { |
6657 | r_const = fold_convert_loc (loc, type: lntype, arg: r_const); |
6658 | r_const = unextend (c: r_const, p: rl_bitsize, unsignedp: rl_unsignedp, mask: rl_and_mask); |
6659 | r_const = const_binop (code: LSHIFT_EXPR, arg1: r_const, size_int (xrl_bitpos)); |
6660 | if (r_const == NULL_TREE) |
6661 | return 0; |
6662 | if (! integer_zerop (const_binop (code: BIT_AND_EXPR, arg1: r_const, |
6663 | arg2: fold_build1_loc (loc, BIT_NOT_EXPR, |
6664 | lntype, rl_mask)))) |
6665 | { |
6666 | warning (0, "comparison is always %d" , wanted_code == NE_EXPR); |
6667 | |
6668 | return constant_boolean_node (wanted_code == NE_EXPR, truth_type); |
6669 | } |
6670 | } |
6671 | |
6672 | /* If the right sides are not constant, do the same for it. Also, |
6673 | disallow this optimization if a size, signedness or storage order |
6674 | mismatch occurs between the left and right sides. */ |
6675 | if (l_const == 0) |
6676 | { |
6677 | if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize |
6678 | || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp |
6679 | || ll_reversep != lr_reversep |
6680 | /* Make sure the two fields on the right |
6681 | correspond to the left without being swapped. */ |
6682 | || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos) |
6683 | return 0; |
6684 | |
6685 | first_bit = MIN (lr_bitpos, rr_bitpos); |
6686 | end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize); |
6687 | if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0, |
6688 | TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD, |
6689 | volatilep, &rnmode)) |
6690 | return 0; |
6691 | |
6692 | rnbitsize = GET_MODE_BITSIZE (mode: rnmode); |
6693 | rnbitpos = first_bit & ~ (rnbitsize - 1); |
6694 | rntype = lang_hooks.types.type_for_size (rnbitsize, 1); |
6695 | xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos; |
6696 | |
6697 | if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN) |
6698 | { |
6699 | xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize; |
6700 | xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize; |
6701 | } |
6702 | |
6703 | lr_mask = const_binop (code: LSHIFT_EXPR, arg1: fold_convert_loc (loc, |
6704 | type: rntype, arg: lr_mask), |
6705 | size_int (xlr_bitpos)); |
6706 | rr_mask = const_binop (code: LSHIFT_EXPR, arg1: fold_convert_loc (loc, |
6707 | type: rntype, arg: rr_mask), |
6708 | size_int (xrr_bitpos)); |
6709 | if (lr_mask == NULL_TREE || rr_mask == NULL_TREE) |
6710 | return 0; |
6711 | |
6712 | /* Make a mask that corresponds to both fields being compared. |
6713 | Do this for both items being compared. If the operands are the |
6714 | same size and the bits being compared are in the same position |
6715 | then we can do this by masking both and comparing the masked |
6716 | results. */ |
6717 | ll_mask = const_binop (code: BIT_IOR_EXPR, arg1: ll_mask, arg2: rl_mask); |
6718 | lr_mask = const_binop (code: BIT_IOR_EXPR, arg1: lr_mask, arg2: rr_mask); |
6719 | if (lnbitsize == rnbitsize |
6720 | && xll_bitpos == xlr_bitpos |
6721 | && lnbitpos >= 0 |
6722 | && rnbitpos >= 0) |
6723 | { |
6724 | lhs = make_bit_field_ref (loc, inner: ll_inner, orig_inner: ll_arg, |
6725 | type: lntype, bitsize: lnbitsize, bitpos: lnbitpos, |
6726 | unsignedp: ll_unsignedp || rl_unsignedp, reversep: ll_reversep); |
6727 | if (! all_ones_mask_p (mask: ll_mask, size: lnbitsize)) |
6728 | lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask); |
6729 | |
6730 | rhs = make_bit_field_ref (loc, inner: lr_inner, orig_inner: lr_arg, |
6731 | type: rntype, bitsize: rnbitsize, bitpos: rnbitpos, |
6732 | unsignedp: lr_unsignedp || rr_unsignedp, reversep: lr_reversep); |
6733 | if (! all_ones_mask_p (mask: lr_mask, size: rnbitsize)) |
6734 | rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask); |
6735 | |
6736 | return build2_loc (loc, code: wanted_code, type: truth_type, arg0: lhs, arg1: rhs); |
6737 | } |
6738 | |
6739 | /* There is still another way we can do something: If both pairs of |
6740 | fields being compared are adjacent, we may be able to make a wider |
6741 | field containing them both. |
6742 | |
6743 | Note that we still must mask the lhs/rhs expressions. Furthermore, |
6744 | the mask must be shifted to account for the shift done by |
6745 | make_bit_field_ref. */ |
6746 | if (((ll_bitsize + ll_bitpos == rl_bitpos |
6747 | && lr_bitsize + lr_bitpos == rr_bitpos) |
6748 | || (ll_bitpos == rl_bitpos + rl_bitsize |
6749 | && lr_bitpos == rr_bitpos + rr_bitsize)) |
6750 | && ll_bitpos >= 0 |
6751 | && rl_bitpos >= 0 |
6752 | && lr_bitpos >= 0 |
6753 | && rr_bitpos >= 0) |
6754 | { |
6755 | tree type; |
6756 | |
6757 | lhs = make_bit_field_ref (loc, inner: ll_inner, orig_inner: ll_arg, type: lntype, |
6758 | bitsize: ll_bitsize + rl_bitsize, |
6759 | MIN (ll_bitpos, rl_bitpos), |
6760 | unsignedp: ll_unsignedp, reversep: ll_reversep); |
6761 | rhs = make_bit_field_ref (loc, inner: lr_inner, orig_inner: lr_arg, type: rntype, |
6762 | bitsize: lr_bitsize + rr_bitsize, |
6763 | MIN (lr_bitpos, rr_bitpos), |
6764 | unsignedp: lr_unsignedp, reversep: lr_reversep); |
6765 | |
6766 | ll_mask = const_binop (code: RSHIFT_EXPR, arg1: ll_mask, |
6767 | size_int (MIN (xll_bitpos, xrl_bitpos))); |
6768 | lr_mask = const_binop (code: RSHIFT_EXPR, arg1: lr_mask, |
6769 | size_int (MIN (xlr_bitpos, xrr_bitpos))); |
6770 | if (ll_mask == NULL_TREE || lr_mask == NULL_TREE) |
6771 | return 0; |
6772 | |
6773 | /* Convert to the smaller type before masking out unwanted bits. */ |
6774 | type = lntype; |
6775 | if (lntype != rntype) |
6776 | { |
6777 | if (lnbitsize > rnbitsize) |
6778 | { |
6779 | lhs = fold_convert_loc (loc, type: rntype, arg: lhs); |
6780 | ll_mask = fold_convert_loc (loc, type: rntype, arg: ll_mask); |
6781 | type = rntype; |
6782 | } |
6783 | else if (lnbitsize < rnbitsize) |
6784 | { |
6785 | rhs = fold_convert_loc (loc, type: lntype, arg: rhs); |
6786 | lr_mask = fold_convert_loc (loc, type: lntype, arg: lr_mask); |
6787 | type = lntype; |
6788 | } |
6789 | } |
6790 | |
6791 | if (! all_ones_mask_p (mask: ll_mask, size: ll_bitsize + rl_bitsize)) |
6792 | lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask); |
6793 | |
6794 | if (! all_ones_mask_p (mask: lr_mask, size: lr_bitsize + rr_bitsize)) |
6795 | rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask); |
6796 | |
6797 | return build2_loc (loc, code: wanted_code, type: truth_type, arg0: lhs, arg1: rhs); |
6798 | } |
6799 | |
6800 | return 0; |
6801 | } |
6802 | |
6803 | /* Handle the case of comparisons with constants. If there is something in |
6804 | common between the masks, those bits of the constants must be the same. |
6805 | If not, the condition is always false. Test for this to avoid generating |
6806 | incorrect code below. */ |
6807 | result = const_binop (code: BIT_AND_EXPR, arg1: ll_mask, arg2: rl_mask); |
6808 | if (! integer_zerop (result) |
6809 | && simple_cst_equal (const_binop (code: BIT_AND_EXPR, arg1: result, arg2: l_const), |
6810 | const_binop (code: BIT_AND_EXPR, arg1: result, arg2: r_const)) != 1) |
6811 | { |
6812 | if (wanted_code == NE_EXPR) |
6813 | { |
6814 | warning (0, "%<or%> of unmatched not-equal tests is always 1" ); |
6815 | return constant_boolean_node (true, truth_type); |
6816 | } |
6817 | else |
6818 | { |
6819 | warning (0, "%<and%> of mutually exclusive equal-tests is always 0" ); |
6820 | return constant_boolean_node (false, truth_type); |
6821 | } |
6822 | } |
6823 | |
6824 | if (lnbitpos < 0) |
6825 | return 0; |
6826 | |
6827 | /* Construct the expression we will return. First get the component |
6828 | reference we will make. Unless the mask is all ones the width of |
6829 | that field, perform the mask operation. Then compare with the |
6830 | merged constant. */ |
6831 | result = make_bit_field_ref (loc, inner: ll_inner, orig_inner: ll_arg, |
6832 | type: lntype, bitsize: lnbitsize, bitpos: lnbitpos, |
6833 | unsignedp: ll_unsignedp || rl_unsignedp, reversep: ll_reversep); |
6834 | |
6835 | ll_mask = const_binop (code: BIT_IOR_EXPR, arg1: ll_mask, arg2: rl_mask); |
6836 | if (! all_ones_mask_p (mask: ll_mask, size: lnbitsize)) |
6837 | result = build2_loc (loc, code: BIT_AND_EXPR, type: lntype, arg0: result, arg1: ll_mask); |
6838 | |
6839 | return build2_loc (loc, code: wanted_code, type: truth_type, arg0: result, |
6840 | arg1: const_binop (code: BIT_IOR_EXPR, arg1: l_const, arg2: r_const)); |
6841 | } |
6842 | |
6843 | /* T is an integer expression that is being multiplied, divided, or taken a |
6844 | modulus (CODE says which and what kind of divide or modulus) by a |
6845 | constant C. See if we can eliminate that operation by folding it with |
6846 | other operations already in T. WIDE_TYPE, if non-null, is a type that |
6847 | should be used for the computation if wider than our type. |
6848 | |
6849 | For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return |
6850 | (X * 2) + (Y * 4). We must, however, be assured that either the original |
6851 | expression would not overflow or that overflow is undefined for the type |
6852 | in the language in question. |
6853 | |
6854 | If we return a non-null expression, it is an equivalent form of the |
6855 | original computation, but need not be in the original type. |
6856 | |
6857 | We set *STRICT_OVERFLOW_P to true if the return values depends on |
6858 | signed overflow being undefined. Otherwise we do not change |
6859 | *STRICT_OVERFLOW_P. */ |
6860 | |
6861 | static tree |
6862 | (tree t, tree c, enum tree_code code, tree wide_type, |
6863 | bool *strict_overflow_p) |
6864 | { |
6865 | /* To avoid exponential search depth, refuse to allow recursion past |
6866 | three levels. Beyond that (1) it's highly unlikely that we'll find |
6867 | something interesting and (2) we've probably processed it before |
6868 | when we built the inner expression. */ |
6869 | |
6870 | static int depth; |
6871 | tree ret; |
6872 | |
6873 | if (depth > 3) |
6874 | return NULL; |
6875 | |
6876 | depth++; |
6877 | ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p); |
6878 | depth--; |
6879 | |
6880 | return ret; |
6881 | } |
6882 | |
6883 | static tree |
6884 | (tree t, tree c, enum tree_code code, tree wide_type, |
6885 | bool *strict_overflow_p) |
6886 | { |
6887 | tree type = TREE_TYPE (t); |
6888 | enum tree_code tcode = TREE_CODE (t); |
6889 | tree ctype = type; |
6890 | if (wide_type) |
6891 | { |
6892 | if (TREE_CODE (type) == BITINT_TYPE |
6893 | || TREE_CODE (wide_type) == BITINT_TYPE) |
6894 | { |
6895 | if (TYPE_PRECISION (wide_type) > TYPE_PRECISION (type)) |
6896 | ctype = wide_type; |
6897 | } |
6898 | else if (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type)) |
6899 | > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type))) |
6900 | ctype = wide_type; |
6901 | } |
6902 | tree t1, t2; |
6903 | bool same_p = tcode == code; |
6904 | tree op0 = NULL_TREE, op1 = NULL_TREE; |
6905 | bool sub_strict_overflow_p; |
6906 | |
6907 | /* Don't deal with constants of zero here; they confuse the code below. */ |
6908 | if (integer_zerop (c)) |
6909 | return NULL_TREE; |
6910 | |
6911 | if (TREE_CODE_CLASS (tcode) == tcc_unary) |
6912 | op0 = TREE_OPERAND (t, 0); |
6913 | |
6914 | if (TREE_CODE_CLASS (tcode) == tcc_binary) |
6915 | op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1); |
6916 | |
6917 | /* Note that we need not handle conditional operations here since fold |
6918 | already handles those cases. So just do arithmetic here. */ |
6919 | switch (tcode) |
6920 | { |
6921 | case INTEGER_CST: |
6922 | /* For a constant, we can always simplify if we are a multiply |
6923 | or (for divide and modulus) if it is a multiple of our constant. */ |
6924 | if (code == MULT_EXPR |
6925 | || wi::multiple_of_p (x: wi::to_wide (t), y: wi::to_wide (t: c), |
6926 | TYPE_SIGN (type))) |
6927 | { |
6928 | tree tem = const_binop (code, fold_convert (ctype, t), |
6929 | fold_convert (ctype, c)); |
6930 | /* If the multiplication overflowed, we lost information on it. |
6931 | See PR68142 and PR69845. */ |
6932 | if (TREE_OVERFLOW (tem)) |
6933 | return NULL_TREE; |
6934 | return tem; |
6935 | } |
6936 | break; |
6937 | |
6938 | CASE_CONVERT: case NON_LVALUE_EXPR: |
6939 | if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))) |
6940 | break; |
6941 | /* If op0 is an expression ... */ |
6942 | if ((COMPARISON_CLASS_P (op0) |
6943 | || UNARY_CLASS_P (op0) |
6944 | || BINARY_CLASS_P (op0) |
6945 | || VL_EXP_CLASS_P (op0) |
6946 | || EXPRESSION_CLASS_P (op0)) |
6947 | /* ... and has wrapping overflow, and its type is smaller |
6948 | than ctype, then we cannot pass through as widening. */ |
6949 | && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)) |
6950 | && (TYPE_PRECISION (ctype) |
6951 | > TYPE_PRECISION (TREE_TYPE (op0)))) |
6952 | /* ... or this is a truncation (t is narrower than op0), |
6953 | then we cannot pass through this narrowing. */ |
6954 | || (TYPE_PRECISION (type) |
6955 | < TYPE_PRECISION (TREE_TYPE (op0))) |
6956 | /* ... or signedness changes for division or modulus, |
6957 | then we cannot pass through this conversion. */ |
6958 | || (code != MULT_EXPR |
6959 | && (TYPE_UNSIGNED (ctype) |
6960 | != TYPE_UNSIGNED (TREE_TYPE (op0)))) |
6961 | /* ... or has undefined overflow while the converted to |
6962 | type has not, we cannot do the operation in the inner type |
6963 | as that would introduce undefined overflow. */ |
6964 | || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)) |
6965 | && !TYPE_OVERFLOW_UNDEFINED (type)))) |
6966 | break; |
6967 | |
6968 | /* Pass the constant down and see if we can make a simplification. If |
6969 | we can, replace this expression with the inner simplification for |
6970 | possible later conversion to our or some other type. */ |
6971 | if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0 |
6972 | && TREE_CODE (t2) == INTEGER_CST |
6973 | && !TREE_OVERFLOW (t2) |
6974 | && (t1 = extract_muldiv (t: op0, c: t2, code, |
6975 | wide_type: code == MULT_EXPR ? ctype : NULL_TREE, |
6976 | strict_overflow_p)) != 0) |
6977 | return t1; |
6978 | break; |
6979 | |
6980 | case ABS_EXPR: |
6981 | /* If widening the type changes it from signed to unsigned, then we |
6982 | must avoid building ABS_EXPR itself as unsigned. */ |
6983 | if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type)) |
6984 | { |
6985 | tree cstype = (*signed_type_for) (ctype); |
6986 | if ((t1 = extract_muldiv (t: op0, c, code, wide_type: cstype, strict_overflow_p)) |
6987 | != 0) |
6988 | { |
6989 | t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1)); |
6990 | return fold_convert (ctype, t1); |
6991 | } |
6992 | break; |
6993 | } |
6994 | /* If the constant is negative, we cannot simplify this. */ |
6995 | if (tree_int_cst_sgn (c) == -1) |
6996 | break; |
6997 | /* FALLTHROUGH */ |
6998 | case NEGATE_EXPR: |
6999 | /* For division and modulus, type can't be unsigned, as e.g. |
7000 | (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2. |
7001 | For signed types, even with wrapping overflow, this is fine. */ |
7002 | if (code != MULT_EXPR && TYPE_UNSIGNED (type)) |
7003 | break; |
7004 | if ((t1 = extract_muldiv (t: op0, c, code, wide_type, strict_overflow_p)) |
7005 | != 0) |
7006 | return fold_build1 (tcode, ctype, fold_convert (ctype, t1)); |
7007 | break; |
7008 | |
7009 | case MIN_EXPR: case MAX_EXPR: |
7010 | /* If widening the type changes the signedness, then we can't perform |
7011 | this optimization as that changes the result. */ |
7012 | if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type)) |
7013 | break; |
7014 | |
7015 | /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */ |
7016 | sub_strict_overflow_p = false; |
7017 | if ((t1 = extract_muldiv (t: op0, c, code, wide_type, |
7018 | strict_overflow_p: &sub_strict_overflow_p)) != 0 |
7019 | && (t2 = extract_muldiv (t: op1, c, code, wide_type, |
7020 | strict_overflow_p: &sub_strict_overflow_p)) != 0) |
7021 | { |
7022 | if (tree_int_cst_sgn (c) < 0) |
7023 | tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR); |
7024 | if (sub_strict_overflow_p) |
7025 | *strict_overflow_p = true; |
7026 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
7027 | fold_convert (ctype, t2)); |
7028 | } |
7029 | break; |
7030 | |
7031 | case LSHIFT_EXPR: case RSHIFT_EXPR: |
7032 | /* If the second operand is constant, this is a multiplication |
7033 | or floor division, by a power of two, so we can treat it that |
7034 | way unless the multiplier or divisor overflows. Signed |
7035 | left-shift overflow is implementation-defined rather than |
7036 | undefined in C90, so do not convert signed left shift into |
7037 | multiplication. */ |
7038 | if (TREE_CODE (op1) == INTEGER_CST |
7039 | && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0))) |
7040 | /* const_binop may not detect overflow correctly, |
7041 | so check for it explicitly here. */ |
7042 | && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), |
7043 | y: wi::to_wide (t: op1)) |
7044 | && (t1 = fold_convert (ctype, |
7045 | const_binop (LSHIFT_EXPR, size_one_node, |
7046 | op1))) != 0 |
7047 | && !TREE_OVERFLOW (t1)) |
7048 | return extract_muldiv (t: build2 (tcode == LSHIFT_EXPR |
7049 | ? MULT_EXPR : FLOOR_DIV_EXPR, |
7050 | ctype, |
7051 | fold_convert (ctype, op0), |
7052 | t1), |
7053 | c, code, wide_type, strict_overflow_p); |
7054 | break; |
7055 | |
7056 | case PLUS_EXPR: case MINUS_EXPR: |
7057 | /* See if we can eliminate the operation on both sides. If we can, we |
7058 | can return a new PLUS or MINUS. If we can't, the only remaining |
7059 | cases where we can do anything are if the second operand is a |
7060 | constant. */ |
7061 | sub_strict_overflow_p = false; |
7062 | t1 = extract_muldiv (t: op0, c, code, wide_type, strict_overflow_p: &sub_strict_overflow_p); |
7063 | t2 = extract_muldiv (t: op1, c, code, wide_type, strict_overflow_p: &sub_strict_overflow_p); |
7064 | if (t1 != 0 && t2 != 0 |
7065 | && TYPE_OVERFLOW_WRAPS (ctype) |
7066 | && (code == MULT_EXPR |
7067 | /* If not multiplication, we can only do this if both operands |
7068 | are divisible by c. */ |
7069 | || (multiple_of_p (ctype, op0, c) |
7070 | && multiple_of_p (ctype, op1, c)))) |
7071 | { |
7072 | if (sub_strict_overflow_p) |
7073 | *strict_overflow_p = true; |
7074 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
7075 | fold_convert (ctype, t2)); |
7076 | } |
7077 | |
7078 | /* If this was a subtraction, negate OP1 and set it to be an addition. |
7079 | This simplifies the logic below. */ |
7080 | if (tcode == MINUS_EXPR) |
7081 | { |
7082 | tcode = PLUS_EXPR, op1 = negate_expr (t: op1); |
7083 | /* If OP1 was not easily negatable, the constant may be OP0. */ |
7084 | if (TREE_CODE (op0) == INTEGER_CST) |
7085 | { |
7086 | std::swap (a&: op0, b&: op1); |
7087 | std::swap (a&: t1, b&: t2); |
7088 | } |
7089 | } |
7090 | |
7091 | if (TREE_CODE (op1) != INTEGER_CST) |
7092 | break; |
7093 | |
7094 | /* If either OP1 or C are negative, this optimization is not safe for |
7095 | some of the division and remainder types while for others we need |
7096 | to change the code. */ |
7097 | if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0) |
7098 | { |
7099 | if (code == CEIL_DIV_EXPR) |
7100 | code = FLOOR_DIV_EXPR; |
7101 | else if (code == FLOOR_DIV_EXPR) |
7102 | code = CEIL_DIV_EXPR; |
7103 | else if (code != MULT_EXPR |
7104 | && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR) |
7105 | break; |
7106 | } |
7107 | |
7108 | /* If it's a multiply or a division/modulus operation of a multiple |
7109 | of our constant, do the operation and verify it doesn't overflow. */ |
7110 | if (code == MULT_EXPR |
7111 | || wi::multiple_of_p (x: wi::to_wide (t: op1), y: wi::to_wide (t: c), |
7112 | TYPE_SIGN (type))) |
7113 | { |
7114 | op1 = const_binop (code, fold_convert (ctype, op1), |
7115 | fold_convert (ctype, c)); |
7116 | /* We allow the constant to overflow with wrapping semantics. */ |
7117 | if (op1 == 0 |
7118 | || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype))) |
7119 | break; |
7120 | } |
7121 | else |
7122 | break; |
7123 | |
7124 | /* If we have an unsigned type, we cannot widen the operation since it |
7125 | will change the result if the original computation overflowed. */ |
7126 | if (TYPE_UNSIGNED (ctype) && ctype != type) |
7127 | break; |
7128 | |
7129 | /* The last case is if we are a multiply. In that case, we can |
7130 | apply the distributive law to commute the multiply and addition |
7131 | if the multiplication of the constants doesn't overflow |
7132 | and overflow is defined. With undefined overflow |
7133 | op0 * c might overflow, while (op0 + orig_op1) * c doesn't. |
7134 | But fold_plusminus_mult_expr would factor back any power-of-two |
7135 | value so do not distribute in the first place in this case. */ |
7136 | if (code == MULT_EXPR |
7137 | && TYPE_OVERFLOW_WRAPS (ctype) |
7138 | && !(tree_fits_shwi_p (c) && pow2p_hwi (x: absu_hwi (x: tree_to_shwi (c))))) |
7139 | return fold_build2 (tcode, ctype, |
7140 | fold_build2 (code, ctype, |
7141 | fold_convert (ctype, op0), |
7142 | fold_convert (ctype, c)), |
7143 | op1); |
7144 | |
7145 | break; |
7146 | |
7147 | case MULT_EXPR: |
7148 | /* We have a special case here if we are doing something like |
7149 | (C * 8) % 4 since we know that's zero. */ |
7150 | if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR |
7151 | || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR) |
7152 | /* If the multiplication can overflow we cannot optimize this. */ |
7153 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)) |
7154 | && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST |
7155 | && wi::multiple_of_p (x: wi::to_wide (t: op1), y: wi::to_wide (t: c), |
7156 | TYPE_SIGN (type))) |
7157 | { |
7158 | *strict_overflow_p = true; |
7159 | return omit_one_operand (type, integer_zero_node, op0); |
7160 | } |
7161 | |
7162 | /* ... fall through ... */ |
7163 | |
7164 | case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR: |
7165 | case ROUND_DIV_EXPR: case EXACT_DIV_EXPR: |
7166 | /* If we can extract our operation from the LHS, do so and return a |
7167 | new operation. Likewise for the RHS from a MULT_EXPR. Otherwise, |
7168 | do something only if the second operand is a constant. */ |
7169 | if (same_p |
7170 | && TYPE_OVERFLOW_WRAPS (ctype) |
7171 | && (t1 = extract_muldiv (t: op0, c, code, wide_type, |
7172 | strict_overflow_p)) != 0) |
7173 | return fold_build2 (tcode, ctype, fold_convert (ctype, t1), |
7174 | fold_convert (ctype, op1)); |
7175 | else if (tcode == MULT_EXPR && code == MULT_EXPR |
7176 | && TYPE_OVERFLOW_WRAPS (ctype) |
7177 | && (t1 = extract_muldiv (t: op1, c, code, wide_type, |
7178 | strict_overflow_p)) != 0) |
7179 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), |
7180 | fold_convert (ctype, t1)); |
7181 | else if (TREE_CODE (op1) != INTEGER_CST) |
7182 | return 0; |
7183 | |
7184 | /* If these are the same operation types, we can associate them |
7185 | assuming no overflow. */ |
7186 | if (tcode == code) |
7187 | { |
7188 | bool overflow_p = false; |
7189 | wi::overflow_type overflow_mul; |
7190 | signop sign = TYPE_SIGN (ctype); |
7191 | unsigned prec = TYPE_PRECISION (ctype); |
7192 | wide_int mul = wi::mul (x: wi::to_wide (t: op1, prec), |
7193 | y: wi::to_wide (t: c, prec), |
7194 | sgn: sign, overflow: &overflow_mul); |
7195 | overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1); |
7196 | if (overflow_mul |
7197 | && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED)) |
7198 | overflow_p = true; |
7199 | if (!overflow_p) |
7200 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), |
7201 | wide_int_to_tree (ctype, mul)); |
7202 | } |
7203 | |
7204 | /* If these operations "cancel" each other, we have the main |
7205 | optimizations of this pass, which occur when either constant is a |
7206 | multiple of the other, in which case we replace this with either an |
7207 | operation or CODE or TCODE. |
7208 | |
7209 | If we have an unsigned type, we cannot do this since it will change |
7210 | the result if the original computation overflowed. */ |
7211 | if (TYPE_OVERFLOW_UNDEFINED (ctype) |
7212 | && !TYPE_OVERFLOW_SANITIZED (ctype) |
7213 | && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR) |
7214 | || (tcode == MULT_EXPR |
7215 | && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR |
7216 | && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR |
7217 | && code != MULT_EXPR))) |
7218 | { |
7219 | if (wi::multiple_of_p (x: wi::to_wide (t: op1), y: wi::to_wide (t: c), |
7220 | TYPE_SIGN (type))) |
7221 | { |
7222 | *strict_overflow_p = true; |
7223 | return fold_build2 (tcode, ctype, fold_convert (ctype, op0), |
7224 | fold_convert (ctype, |
7225 | const_binop (TRUNC_DIV_EXPR, |
7226 | op1, c))); |
7227 | } |
7228 | else if (wi::multiple_of_p (x: wi::to_wide (t: c), y: wi::to_wide (t: op1), |
7229 | TYPE_SIGN (type))) |
7230 | { |
7231 | *strict_overflow_p = true; |
7232 | return fold_build2 (code, ctype, fold_convert (ctype, op0), |
7233 | fold_convert (ctype, |
7234 | const_binop (TRUNC_DIV_EXPR, |
7235 | c, op1))); |
7236 | } |
7237 | } |
7238 | break; |
7239 | |
7240 | default: |
7241 | break; |
7242 | } |
7243 | |
7244 | return 0; |
7245 | } |
7246 | |
7247 | /* Return a node which has the indicated constant VALUE (either 0 or |
7248 | 1 for scalars or {-1,-1,..} or {0,0,...} for vectors), |
7249 | and is of the indicated TYPE. */ |
7250 | |
7251 | tree |
7252 | constant_boolean_node (bool value, tree type) |
7253 | { |
7254 | if (type == integer_type_node) |
7255 | return value ? integer_one_node : integer_zero_node; |
7256 | else if (type == boolean_type_node) |
7257 | return value ? boolean_true_node : boolean_false_node; |
7258 | else if (VECTOR_TYPE_P (type)) |
7259 | return build_vector_from_val (type, |
7260 | build_int_cst (TREE_TYPE (type), |
7261 | value ? -1 : 0)); |
7262 | else |
7263 | return fold_convert (type, value ? integer_one_node : integer_zero_node); |
7264 | } |
7265 | |
7266 | |
7267 | /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'. |
7268 | Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here |
7269 | CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)' |
7270 | expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the |
7271 | COND is the first argument to CODE; otherwise (as in the example |
7272 | given here), it is the second argument. TYPE is the type of the |
7273 | original expression. Return NULL_TREE if no simplification is |
7274 | possible. */ |
7275 | |
7276 | static tree |
7277 | fold_binary_op_with_conditional_arg (location_t loc, |
7278 | enum tree_code code, |
7279 | tree type, tree op0, tree op1, |
7280 | tree cond, tree arg, int cond_first_p) |
7281 | { |
7282 | tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1); |
7283 | tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0); |
7284 | tree test, true_value, false_value; |
7285 | tree lhs = NULL_TREE; |
7286 | tree rhs = NULL_TREE; |
7287 | enum tree_code cond_code = COND_EXPR; |
7288 | |
7289 | /* Do not move possibly trapping operations into the conditional as this |
7290 | pessimizes code and causes gimplification issues when applied late. */ |
7291 | if (operation_could_trap_p (code, FLOAT_TYPE_P (type), |
7292 | ANY_INTEGRAL_TYPE_P (type) |
7293 | && TYPE_OVERFLOW_TRAPS (type), op1)) |
7294 | return NULL_TREE; |
7295 | |
7296 | if (TREE_CODE (cond) == COND_EXPR |
7297 | || TREE_CODE (cond) == VEC_COND_EXPR) |
7298 | { |
7299 | test = TREE_OPERAND (cond, 0); |
7300 | true_value = TREE_OPERAND (cond, 1); |
7301 | false_value = TREE_OPERAND (cond, 2); |
7302 | /* If this operand throws an expression, then it does not make |
7303 | sense to try to perform a logical or arithmetic operation |
7304 | involving it. */ |
7305 | if (VOID_TYPE_P (TREE_TYPE (true_value))) |
7306 | lhs = true_value; |
7307 | if (VOID_TYPE_P (TREE_TYPE (false_value))) |
7308 | rhs = false_value; |
7309 | } |
7310 | else if (!(TREE_CODE (type) != VECTOR_TYPE |
7311 | && VECTOR_TYPE_P (TREE_TYPE (cond)))) |
7312 | { |
7313 | tree testtype = TREE_TYPE (cond); |
7314 | test = cond; |
7315 | true_value = constant_boolean_node (value: true, type: testtype); |
7316 | false_value = constant_boolean_node (value: false, type: testtype); |
7317 | } |
7318 | else |
7319 | /* Detect the case of mixing vector and scalar types - bail out. */ |
7320 | return NULL_TREE; |
7321 | |
7322 | if (VECTOR_TYPE_P (TREE_TYPE (test))) |
7323 | cond_code = VEC_COND_EXPR; |
7324 | |
7325 | /* This transformation is only worthwhile if we don't have to wrap ARG |
7326 | in a SAVE_EXPR and the operation can be simplified without recursing |
7327 | on at least one of the branches once its pushed inside the COND_EXPR. */ |
7328 | if (!TREE_CONSTANT (arg) |
7329 | && (TREE_SIDE_EFFECTS (arg) |
7330 | || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR |
7331 | || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value))) |
7332 | return NULL_TREE; |
7333 | |
7334 | arg = fold_convert_loc (loc, type: arg_type, arg); |
7335 | if (lhs == 0) |
7336 | { |
7337 | true_value = fold_convert_loc (loc, type: cond_type, arg: true_value); |
7338 | if (cond_first_p) |
7339 | lhs = fold_build2_loc (loc, code, type, true_value, arg); |
7340 | else |
7341 | lhs = fold_build2_loc (loc, code, type, arg, true_value); |
7342 | } |
7343 | if (rhs == 0) |
7344 | { |
7345 | false_value = fold_convert_loc (loc, type: cond_type, arg: false_value); |
7346 | if (cond_first_p) |
7347 | rhs = fold_build2_loc (loc, code, type, false_value, arg); |
7348 | else |
7349 | rhs = fold_build2_loc (loc, code, type, arg, false_value); |
7350 | } |
7351 | |
7352 | /* Check that we have simplified at least one of the branches. */ |
7353 | if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs)) |
7354 | return NULL_TREE; |
7355 | |
7356 | return fold_build3_loc (loc, cond_code, type, test, lhs, rhs); |
7357 | } |
7358 | |
7359 | |
7360 | /* Subroutine of fold() that checks for the addition of ARG +/- 0.0. |
7361 | |
7362 | If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of |
7363 | type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true |
7364 | if ARG - ZERO_ARG is the same as X. |
7365 | |
7366 | If ARG is NULL, check for any value of type TYPE. |
7367 | |
7368 | X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero |
7369 | and finite. The problematic cases are when X is zero, and its mode |
7370 | has signed zeros. In the case of rounding towards -infinity, |
7371 | X - 0 is not the same as X because 0 - 0 is -0. In other rounding |
7372 | modes, X + 0 is not the same as X because -0 + 0 is 0. */ |
7373 | |
7374 | bool |
7375 | fold_real_zero_addition_p (const_tree type, const_tree arg, |
7376 | const_tree zero_arg, int negate) |
7377 | { |
7378 | if (!real_zerop (zero_arg)) |
7379 | return false; |
7380 | |
7381 | /* Don't allow the fold with -fsignaling-nans. */ |
7382 | if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type)) |
7383 | return false; |
7384 | |
7385 | /* Allow the fold if zeros aren't signed, or their sign isn't important. */ |
7386 | if (!HONOR_SIGNED_ZEROS (type)) |
7387 | return true; |
7388 | |
7389 | /* There is no case that is safe for all rounding modes. */ |
7390 | if (HONOR_SIGN_DEPENDENT_ROUNDING (type)) |
7391 | return false; |
7392 | |
7393 | /* In a vector or complex, we would need to check the sign of all zeros. */ |
7394 | if (TREE_CODE (zero_arg) == VECTOR_CST) |
7395 | zero_arg = uniform_vector_p (zero_arg); |
7396 | if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST) |
7397 | return false; |
7398 | |
7399 | /* Treat x + -0 as x - 0 and x - -0 as x + 0. */ |
7400 | if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg))) |
7401 | negate = !negate; |
7402 | |
7403 | /* The mode has signed zeros, and we have to honor their sign. |
7404 | In this situation, there are only two cases we can return true for. |
7405 | (i) X - 0 is the same as X with default rounding. |
7406 | (ii) X + 0 is X when X can't possibly be -0.0. */ |
7407 | return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg)); |
7408 | } |
7409 | |
7410 | /* Subroutine of match.pd that optimizes comparisons of a division by |
7411 | a nonzero integer constant against an integer constant, i.e. |
7412 | X/C1 op C2. |
7413 | |
7414 | CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, |
7415 | GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */ |
7416 | |
7417 | enum tree_code |
7418 | fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo, |
7419 | tree *hi, bool *neg_overflow) |
7420 | { |
7421 | tree prod, tmp, type = TREE_TYPE (c1); |
7422 | signop sign = TYPE_SIGN (type); |
7423 | wi::overflow_type overflow; |
7424 | |
7425 | /* We have to do this the hard way to detect unsigned overflow. |
7426 | prod = int_const_binop (MULT_EXPR, c1, c2); */ |
7427 | wide_int val = wi::mul (x: wi::to_wide (t: c1), y: wi::to_wide (t: c2), sgn: sign, overflow: &overflow); |
7428 | prod = force_fit_type (type, val, -1, overflow); |
7429 | *neg_overflow = false; |
7430 | |
7431 | if (sign == UNSIGNED) |
7432 | { |
7433 | tmp = int_const_binop (code: MINUS_EXPR, arg1: c1, arg2: build_int_cst (type, 1)); |
7434 | *lo = prod; |
7435 | |
7436 | /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */ |
7437 | val = wi::add (x: wi::to_wide (t: prod), y: wi::to_wide (t: tmp), sgn: sign, overflow: &overflow); |
7438 | *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod)); |
7439 | } |
7440 | else if (tree_int_cst_sgn (c1) >= 0) |
7441 | { |
7442 | tmp = int_const_binop (code: MINUS_EXPR, arg1: c1, arg2: build_int_cst (type, 1)); |
7443 | switch (tree_int_cst_sgn (c2)) |
7444 | { |
7445 | case -1: |
7446 | *neg_overflow = true; |
7447 | *lo = int_const_binop (code: MINUS_EXPR, arg1: prod, arg2: tmp); |
7448 | *hi = prod; |
7449 | break; |
7450 | |
7451 | case 0: |
7452 | *lo = fold_negate_const (tmp, type); |
7453 | *hi = tmp; |
7454 | break; |
7455 | |
7456 | case 1: |
7457 | *hi = int_const_binop (code: PLUS_EXPR, arg1: prod, arg2: tmp); |
7458 | *lo = prod; |
7459 | break; |
7460 | |
7461 | default: |
7462 | gcc_unreachable (); |
7463 | } |
7464 | } |
7465 | else |
7466 | { |
7467 | /* A negative divisor reverses the relational operators. */ |
7468 | code = swap_tree_comparison (code); |
7469 | |
7470 | tmp = int_const_binop (code: PLUS_EXPR, arg1: c1, arg2: build_int_cst (type, 1)); |
7471 | switch (tree_int_cst_sgn (c2)) |
7472 | { |
7473 | case -1: |
7474 | *hi = int_const_binop (code: MINUS_EXPR, arg1: prod, arg2: tmp); |
7475 | *lo = prod; |
7476 | break; |
7477 | |
7478 | case 0: |
7479 | *hi = fold_negate_const (tmp, type); |
7480 | *lo = tmp; |
7481 | break; |
7482 | |
7483 | case 1: |
7484 | *neg_overflow = true; |
7485 | *lo = int_const_binop (code: PLUS_EXPR, arg1: prod, arg2: tmp); |
7486 | *hi = prod; |
7487 | break; |
7488 | |
7489 | default: |
7490 | gcc_unreachable (); |
7491 | } |
7492 | } |
7493 | |
7494 | if (code != EQ_EXPR && code != NE_EXPR) |
7495 | return code; |
7496 | |
7497 | if (TREE_OVERFLOW (*lo) |
7498 | || operand_equal_p (arg0: *lo, TYPE_MIN_VALUE (type), flags: 0)) |
7499 | *lo = NULL_TREE; |
7500 | if (TREE_OVERFLOW (*hi) |
7501 | || operand_equal_p (arg0: *hi, TYPE_MAX_VALUE (type), flags: 0)) |
7502 | *hi = NULL_TREE; |
7503 | |
7504 | return code; |
7505 | } |
7506 | |
7507 | /* Test whether it is preferable to swap two operands, ARG0 and |
7508 | ARG1, for example because ARG0 is an integer constant and ARG1 |
7509 | isn't. */ |
7510 | |
7511 | bool |
7512 | tree_swap_operands_p (const_tree arg0, const_tree arg1) |
7513 | { |
7514 | if (CONSTANT_CLASS_P (arg1)) |
7515 | return false; |
7516 | if (CONSTANT_CLASS_P (arg0)) |
7517 | return true; |
7518 | |
7519 | STRIP_NOPS (arg0); |
7520 | STRIP_NOPS (arg1); |
7521 | |
7522 | if (TREE_CONSTANT (arg1)) |
7523 | return false; |
7524 | if (TREE_CONSTANT (arg0)) |
7525 | return true; |
7526 | |
7527 | /* It is preferable to swap two SSA_NAME to ensure a canonical form |
7528 | for commutative and comparison operators. Ensuring a canonical |
7529 | form allows the optimizers to find additional redundancies without |
7530 | having to explicitly check for both orderings. */ |
7531 | if (TREE_CODE (arg0) == SSA_NAME |
7532 | && TREE_CODE (arg1) == SSA_NAME |
7533 | && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1)) |
7534 | return true; |
7535 | |
7536 | /* Put SSA_NAMEs last. */ |
7537 | if (TREE_CODE (arg1) == SSA_NAME) |
7538 | return false; |
7539 | if (TREE_CODE (arg0) == SSA_NAME) |
7540 | return true; |
7541 | |
7542 | /* Put variables last. */ |
7543 | if (DECL_P (arg1)) |
7544 | return false; |
7545 | if (DECL_P (arg0)) |
7546 | return true; |
7547 | |
7548 | return false; |
7549 | } |
7550 | |
7551 | |
7552 | /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y |
7553 | means A >= Y && A != MAX, but in this case we know that |
7554 | A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */ |
7555 | |
7556 | static tree |
7557 | fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound) |
7558 | { |
7559 | tree a, typea, type = TREE_TYPE (bound), a1, diff, y; |
7560 | |
7561 | if (TREE_CODE (bound) == LT_EXPR) |
7562 | a = TREE_OPERAND (bound, 0); |
7563 | else if (TREE_CODE (bound) == GT_EXPR) |
7564 | a = TREE_OPERAND (bound, 1); |
7565 | else |
7566 | return NULL_TREE; |
7567 | |
7568 | typea = TREE_TYPE (a); |
7569 | if (!INTEGRAL_TYPE_P (typea) |
7570 | && !POINTER_TYPE_P (typea)) |
7571 | return NULL_TREE; |
7572 | |
7573 | if (TREE_CODE (ineq) == LT_EXPR) |
7574 | { |
7575 | a1 = TREE_OPERAND (ineq, 1); |
7576 | y = TREE_OPERAND (ineq, 0); |
7577 | } |
7578 | else if (TREE_CODE (ineq) == GT_EXPR) |
7579 | { |
7580 | a1 = TREE_OPERAND (ineq, 0); |
7581 | y = TREE_OPERAND (ineq, 1); |
7582 | } |
7583 | else |
7584 | return NULL_TREE; |
7585 | |
7586 | if (TREE_TYPE (a1) != typea) |
7587 | return NULL_TREE; |
7588 | |
7589 | if (POINTER_TYPE_P (typea)) |
7590 | { |
7591 | /* Convert the pointer types into integer before taking the difference. */ |
7592 | tree ta = fold_convert_loc (loc, ssizetype, arg: a); |
7593 | tree ta1 = fold_convert_loc (loc, ssizetype, arg: a1); |
7594 | diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta); |
7595 | } |
7596 | else |
7597 | diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a); |
7598 | |
7599 | if (!diff || !integer_onep (diff)) |
7600 | return NULL_TREE; |
7601 | |
7602 | return fold_build2_loc (loc, GE_EXPR, type, a, y); |
7603 | } |
7604 | |
7605 | /* Fold a sum or difference of at least one multiplication. |
7606 | Returns the folded tree or NULL if no simplification could be made. */ |
7607 | |
7608 | static tree |
7609 | fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type, |
7610 | tree arg0, tree arg1) |
7611 | { |
7612 | tree arg00, arg01, arg10, arg11; |
7613 | tree alt0 = NULL_TREE, alt1 = NULL_TREE, same; |
7614 | |
7615 | /* (A * C) +- (B * C) -> (A+-B) * C. |
7616 | (A * C) +- A -> A * (C+-1). |
7617 | We are most concerned about the case where C is a constant, |
7618 | but other combinations show up during loop reduction. Since |
7619 | it is not difficult, try all four possibilities. */ |
7620 | |
7621 | if (TREE_CODE (arg0) == MULT_EXPR) |
7622 | { |
7623 | arg00 = TREE_OPERAND (arg0, 0); |
7624 | arg01 = TREE_OPERAND (arg0, 1); |
7625 | } |
7626 | else if (TREE_CODE (arg0) == INTEGER_CST) |
7627 | { |
7628 | arg00 = build_one_cst (type); |
7629 | arg01 = arg0; |
7630 | } |
7631 | else |
7632 | { |
7633 | /* We cannot generate constant 1 for fract. */ |
7634 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) |
7635 | return NULL_TREE; |
7636 | arg00 = arg0; |
7637 | arg01 = build_one_cst (type); |
7638 | } |
7639 | if (TREE_CODE (arg1) == MULT_EXPR) |
7640 | { |
7641 | arg10 = TREE_OPERAND (arg1, 0); |
7642 | arg11 = TREE_OPERAND (arg1, 1); |
7643 | } |
7644 | else if (TREE_CODE (arg1) == INTEGER_CST) |
7645 | { |
7646 | arg10 = build_one_cst (type); |
7647 | /* As we canonicalize A - 2 to A + -2 get rid of that sign for |
7648 | the purpose of this canonicalization. */ |
7649 | if (wi::neg_p (x: wi::to_wide (t: arg1), TYPE_SIGN (TREE_TYPE (arg1))) |
7650 | && negate_expr_p (t: arg1) |
7651 | && code == PLUS_EXPR) |
7652 | { |
7653 | arg11 = negate_expr (t: arg1); |
7654 | code = MINUS_EXPR; |
7655 | } |
7656 | else |
7657 | arg11 = arg1; |
7658 | } |
7659 | else |
7660 | { |
7661 | /* We cannot generate constant 1 for fract. */ |
7662 | if (ALL_FRACT_MODE_P (TYPE_MODE (type))) |
7663 | return NULL_TREE; |
7664 | arg10 = arg1; |
7665 | arg11 = build_one_cst (type); |
7666 | } |
7667 | same = NULL_TREE; |
7668 | |
7669 | /* Prefer factoring a common non-constant. */ |
7670 | if (operand_equal_p (arg0: arg00, arg1: arg10, flags: 0)) |
7671 | same = arg00, alt0 = arg01, alt1 = arg11; |
7672 | else if (operand_equal_p (arg0: arg01, arg1: arg11, flags: 0)) |
7673 | same = arg01, alt0 = arg00, alt1 = arg10; |
7674 | else if (operand_equal_p (arg0: arg00, arg1: arg11, flags: 0)) |
7675 | same = arg00, alt0 = arg01, alt1 = arg10; |
7676 | else if (operand_equal_p (arg0: arg01, arg1: arg10, flags: 0)) |
7677 | same = arg01, alt0 = arg00, alt1 = arg11; |
7678 | |
7679 | /* No identical multiplicands; see if we can find a common |
7680 | power-of-two factor in non-power-of-two multiplies. This |
7681 | can help in multi-dimensional array access. */ |
7682 | else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11)) |
7683 | { |
7684 | HOST_WIDE_INT int01 = tree_to_shwi (arg01); |
7685 | HOST_WIDE_INT int11 = tree_to_shwi (arg11); |
7686 | HOST_WIDE_INT tmp; |
7687 | bool swap = false; |
7688 | tree maybe_same; |
7689 | |
7690 | /* Move min of absolute values to int11. */ |
7691 | if (absu_hwi (x: int01) < absu_hwi (x: int11)) |
7692 | { |
7693 | tmp = int01, int01 = int11, int11 = tmp; |
7694 | alt0 = arg00, arg00 = arg10, arg10 = alt0; |
7695 | maybe_same = arg01; |
7696 | swap = true; |
7697 | } |
7698 | else |
7699 | maybe_same = arg11; |
7700 | |
7701 | const unsigned HOST_WIDE_INT factor = absu_hwi (x: int11); |
7702 | if (factor > 1 |
7703 | && pow2p_hwi (x: factor) |
7704 | && (int01 & (factor - 1)) == 0 |
7705 | /* The remainder should not be a constant, otherwise we |
7706 | end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has |
7707 | increased the number of multiplications necessary. */ |
7708 | && TREE_CODE (arg10) != INTEGER_CST) |
7709 | { |
7710 | alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00, |
7711 | build_int_cst (TREE_TYPE (arg00), |
7712 | int01 / int11)); |
7713 | alt1 = arg10; |
7714 | same = maybe_same; |
7715 | if (swap) |
7716 | maybe_same = alt0, alt0 = alt1, alt1 = maybe_same; |
7717 | } |
7718 | } |
7719 | |
7720 | if (!same) |
7721 | return NULL_TREE; |
7722 | |
7723 | if (! ANY_INTEGRAL_TYPE_P (type) |
7724 | || TYPE_OVERFLOW_WRAPS (type) |
7725 | /* We are neither factoring zero nor minus one. */ |
7726 | || TREE_CODE (same) == INTEGER_CST) |
7727 | return fold_build2_loc (loc, MULT_EXPR, type, |
7728 | fold_build2_loc (loc, code, type, |
7729 | fold_convert_loc (loc, type, arg: alt0), |
7730 | fold_convert_loc (loc, type, arg: alt1)), |
7731 | fold_convert_loc (loc, type, arg: same)); |
7732 | |
7733 | /* Same may be zero and thus the operation 'code' may overflow. Likewise |
7734 | same may be minus one and thus the multiplication may overflow. Perform |
7735 | the sum operation in an unsigned type. */ |
7736 | tree utype = unsigned_type_for (type); |
7737 | tree tem = fold_build2_loc (loc, code, utype, |
7738 | fold_convert_loc (loc, type: utype, arg: alt0), |
7739 | fold_convert_loc (loc, type: utype, arg: alt1)); |
7740 | /* If the sum evaluated to a constant that is not -INF the multiplication |
7741 | cannot overflow. */ |
7742 | if (TREE_CODE (tem) == INTEGER_CST |
7743 | && (wi::to_wide (t: tem) |
7744 | != wi::min_value (TYPE_PRECISION (utype), SIGNED))) |
7745 | return fold_build2_loc (loc, MULT_EXPR, type, |
7746 | fold_convert (type, tem), same); |
7747 | |
7748 | /* Do not resort to unsigned multiplication because |
7749 | we lose the no-overflow property of the expression. */ |
7750 | return NULL_TREE; |
7751 | } |
7752 | |
7753 | /* Subroutine of native_encode_expr. Encode the INTEGER_CST |
7754 | specified by EXPR into the buffer PTR of length LEN bytes. |
7755 | Return the number of bytes placed in the buffer, or zero |
7756 | upon failure. */ |
7757 | |
7758 | static int |
7759 | native_encode_int (const_tree expr, unsigned char *ptr, int len, int off) |
7760 | { |
7761 | tree type = TREE_TYPE (expr); |
7762 | int total_bytes; |
7763 | if (TREE_CODE (type) == BITINT_TYPE) |
7764 | { |
7765 | struct bitint_info info; |
7766 | bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info); |
7767 | gcc_assert (ok); |
7768 | scalar_int_mode limb_mode = as_a <scalar_int_mode> (m: info.limb_mode); |
7769 | if (TYPE_PRECISION (type) > GET_MODE_PRECISION (mode: limb_mode)) |
7770 | { |
7771 | total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
7772 | /* More work is needed when adding _BitInt support to PDP endian |
7773 | if limb is smaller than word, or if _BitInt limb ordering doesn't |
7774 | match target endianity here. */ |
7775 | gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN |
7776 | && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN |
7777 | || (GET_MODE_SIZE (limb_mode) |
7778 | >= UNITS_PER_WORD))); |
7779 | } |
7780 | else |
7781 | total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)); |
7782 | } |
7783 | else |
7784 | total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)); |
7785 | int byte, offset, word, words; |
7786 | unsigned char value; |
7787 | |
7788 | if ((off == -1 && total_bytes > len) || off >= total_bytes) |
7789 | return 0; |
7790 | if (off == -1) |
7791 | off = 0; |
7792 | |
7793 | if (ptr == NULL) |
7794 | /* Dry run. */ |
7795 | return MIN (len, total_bytes - off); |
7796 | |
7797 | words = total_bytes / UNITS_PER_WORD; |
7798 | |
7799 | for (byte = 0; byte < total_bytes; byte++) |
7800 | { |
7801 | int bitpos = byte * BITS_PER_UNIT; |
7802 | /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole |
7803 | number of bytes. */ |
7804 | value = wi::extract_uhwi (x: wi::to_widest (t: expr), bitpos, BITS_PER_UNIT); |
7805 | |
7806 | if (total_bytes > UNITS_PER_WORD) |
7807 | { |
7808 | word = byte / UNITS_PER_WORD; |
7809 | if (WORDS_BIG_ENDIAN) |
7810 | word = (words - 1) - word; |
7811 | offset = word * UNITS_PER_WORD; |
7812 | if (BYTES_BIG_ENDIAN) |
7813 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); |
7814 | else |
7815 | offset += byte % UNITS_PER_WORD; |
7816 | } |
7817 | else |
7818 | offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte; |
7819 | if (offset >= off && offset - off < len) |
7820 | ptr[offset - off] = value; |
7821 | } |
7822 | return MIN (len, total_bytes - off); |
7823 | } |
7824 | |
7825 | |
7826 | /* Subroutine of native_encode_expr. Encode the FIXED_CST |
7827 | specified by EXPR into the buffer PTR of length LEN bytes. |
7828 | Return the number of bytes placed in the buffer, or zero |
7829 | upon failure. */ |
7830 | |
7831 | static int |
7832 | native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off) |
7833 | { |
7834 | tree type = TREE_TYPE (expr); |
7835 | scalar_mode mode = SCALAR_TYPE_MODE (type); |
7836 | int total_bytes = GET_MODE_SIZE (mode); |
7837 | FIXED_VALUE_TYPE value; |
7838 | tree i_value, i_type; |
7839 | |
7840 | if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) |
7841 | return 0; |
7842 | |
7843 | i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1); |
7844 | |
7845 | if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes) |
7846 | return 0; |
7847 | |
7848 | value = TREE_FIXED_CST (expr); |
7849 | i_value = double_int_to_tree (i_type, value.data); |
7850 | |
7851 | return native_encode_int (expr: i_value, ptr, len, off); |
7852 | } |
7853 | |
7854 | |
7855 | /* Subroutine of native_encode_expr. Encode the REAL_CST |
7856 | specified by EXPR into the buffer PTR of length LEN bytes. |
7857 | Return the number of bytes placed in the buffer, or zero |
7858 | upon failure. */ |
7859 | |
7860 | static int |
7861 | native_encode_real (const_tree expr, unsigned char *ptr, int len, int off) |
7862 | { |
7863 | tree type = TREE_TYPE (expr); |
7864 | int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type)); |
7865 | int byte, offset, word, words, bitpos; |
7866 | unsigned char value; |
7867 | |
7868 | /* There are always 32 bits in each long, no matter the size of |
7869 | the hosts long. We handle floating point representations with |
7870 | up to 192 bits. */ |
7871 | long tmp[6]; |
7872 | |
7873 | if ((off == -1 && total_bytes > len) || off >= total_bytes) |
7874 | return 0; |
7875 | if (off == -1) |
7876 | off = 0; |
7877 | |
7878 | if (ptr == NULL) |
7879 | /* Dry run. */ |
7880 | return MIN (len, total_bytes - off); |
7881 | |
7882 | words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
7883 | |
7884 | real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type)); |
7885 | |
7886 | for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
7887 | bitpos += BITS_PER_UNIT) |
7888 | { |
7889 | byte = (bitpos / BITS_PER_UNIT) & 3; |
7890 | value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31)); |
7891 | |
7892 | if (UNITS_PER_WORD < 4) |
7893 | { |
7894 | word = byte / UNITS_PER_WORD; |
7895 | if (WORDS_BIG_ENDIAN) |
7896 | word = (words - 1) - word; |
7897 | offset = word * UNITS_PER_WORD; |
7898 | if (BYTES_BIG_ENDIAN) |
7899 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); |
7900 | else |
7901 | offset += byte % UNITS_PER_WORD; |
7902 | } |
7903 | else |
7904 | { |
7905 | offset = byte; |
7906 | if (BYTES_BIG_ENDIAN) |
7907 | { |
7908 | /* Reverse bytes within each long, or within the entire float |
7909 | if it's smaller than a long (for HFmode). */ |
7910 | offset = MIN (3, total_bytes - 1) - offset; |
7911 | gcc_assert (offset >= 0); |
7912 | } |
7913 | } |
7914 | offset = offset + ((bitpos / BITS_PER_UNIT) & ~3); |
7915 | if (offset >= off |
7916 | && offset - off < len) |
7917 | ptr[offset - off] = value; |
7918 | } |
7919 | return MIN (len, total_bytes - off); |
7920 | } |
7921 | |
7922 | /* Subroutine of native_encode_expr. Encode the COMPLEX_CST |
7923 | specified by EXPR into the buffer PTR of length LEN bytes. |
7924 | Return the number of bytes placed in the buffer, or zero |
7925 | upon failure. */ |
7926 | |
7927 | static int |
7928 | native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off) |
7929 | { |
7930 | int rsize, isize; |
7931 | tree part; |
7932 | |
7933 | part = TREE_REALPART (expr); |
7934 | rsize = native_encode_expr (part, ptr, len, off); |
7935 | if (off == -1 && rsize == 0) |
7936 | return 0; |
7937 | part = TREE_IMAGPART (expr); |
7938 | if (off != -1) |
7939 | off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part)))); |
7940 | isize = native_encode_expr (part, ptr ? ptr + rsize : NULL, |
7941 | len - rsize, off); |
7942 | if (off == -1 && isize != rsize) |
7943 | return 0; |
7944 | return rsize + isize; |
7945 | } |
7946 | |
7947 | /* Like native_encode_vector, but only encode the first COUNT elements. |
7948 | The other arguments are as for native_encode_vector. */ |
7949 | |
7950 | static int |
7951 | native_encode_vector_part (const_tree expr, unsigned char *ptr, int len, |
7952 | int off, unsigned HOST_WIDE_INT count) |
7953 | { |
7954 | tree itype = TREE_TYPE (TREE_TYPE (expr)); |
7955 | if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr)) |
7956 | && TYPE_PRECISION (itype) <= BITS_PER_UNIT) |
7957 | { |
7958 | /* This is the only case in which elements can be smaller than a byte. |
7959 | Element 0 is always in the lsb of the containing byte. */ |
7960 | unsigned int elt_bits = TYPE_PRECISION (itype); |
7961 | int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT); |
7962 | if ((off == -1 && total_bytes > len) || off >= total_bytes) |
7963 | return 0; |
7964 | |
7965 | if (off == -1) |
7966 | off = 0; |
7967 | |
7968 | /* Zero the buffer and then set bits later where necessary. */ |
7969 | int = MIN (len, total_bytes - off); |
7970 | if (ptr) |
7971 | memset (s: ptr, c: 0, n: extract_bytes); |
7972 | |
7973 | unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits; |
7974 | unsigned int first_elt = off * elts_per_byte; |
7975 | unsigned int = extract_bytes * elts_per_byte; |
7976 | for (unsigned int i = 0; i < extract_elts; ++i) |
7977 | { |
7978 | tree elt = VECTOR_CST_ELT (expr, first_elt + i); |
7979 | if (TREE_CODE (elt) != INTEGER_CST) |
7980 | return 0; |
7981 | |
7982 | if (ptr && wi::extract_uhwi (x: wi::to_wide (t: elt), bitpos: 0, width: 1)) |
7983 | { |
7984 | unsigned int bit = i * elt_bits; |
7985 | ptr[bit / BITS_PER_UNIT] |= 1 << (bit % BITS_PER_UNIT); |
7986 | } |
7987 | } |
7988 | return extract_bytes; |
7989 | } |
7990 | |
7991 | int offset = 0; |
7992 | int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype)); |
7993 | for (unsigned HOST_WIDE_INT i = 0; i < count; i++) |
7994 | { |
7995 | if (off >= size) |
7996 | { |
7997 | off -= size; |
7998 | continue; |
7999 | } |
8000 | tree elem = VECTOR_CST_ELT (expr, i); |
8001 | int res = native_encode_expr (elem, ptr ? ptr + offset : NULL, |
8002 | len - offset, off); |
8003 | if ((off == -1 && res != size) || res == 0) |
8004 | return 0; |
8005 | offset += res; |
8006 | if (offset >= len) |
8007 | return (off == -1 && i < count - 1) ? 0 : offset; |
8008 | if (off != -1) |
8009 | off = 0; |
8010 | } |
8011 | return offset; |
8012 | } |
8013 | |
8014 | /* Subroutine of native_encode_expr. Encode the VECTOR_CST |
8015 | specified by EXPR into the buffer PTR of length LEN bytes. |
8016 | Return the number of bytes placed in the buffer, or zero |
8017 | upon failure. */ |
8018 | |
8019 | static int |
8020 | native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off) |
8021 | { |
8022 | unsigned HOST_WIDE_INT count; |
8023 | if (!VECTOR_CST_NELTS (expr).is_constant (const_value: &count)) |
8024 | return 0; |
8025 | return native_encode_vector_part (expr, ptr, len, off, count); |
8026 | } |
8027 | |
8028 | |
8029 | /* Subroutine of native_encode_expr. Encode the STRING_CST |
8030 | specified by EXPR into the buffer PTR of length LEN bytes. |
8031 | Return the number of bytes placed in the buffer, or zero |
8032 | upon failure. */ |
8033 | |
8034 | static int |
8035 | native_encode_string (const_tree expr, unsigned char *ptr, int len, int off) |
8036 | { |
8037 | tree type = TREE_TYPE (expr); |
8038 | |
8039 | /* Wide-char strings are encoded in target byte-order so native |
8040 | encoding them is trivial. */ |
8041 | if (BITS_PER_UNIT != CHAR_BIT |
8042 | || TREE_CODE (type) != ARRAY_TYPE |
8043 | || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE |
8044 | || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type))) |
8045 | return 0; |
8046 | |
8047 | HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr))); |
8048 | if ((off == -1 && total_bytes > len) || off >= total_bytes) |
8049 | return 0; |
8050 | if (off == -1) |
8051 | off = 0; |
8052 | len = MIN (total_bytes - off, len); |
8053 | if (ptr == NULL) |
8054 | /* Dry run. */; |
8055 | else |
8056 | { |
8057 | int written = 0; |
8058 | if (off < TREE_STRING_LENGTH (expr)) |
8059 | { |
8060 | written = MIN (len, TREE_STRING_LENGTH (expr) - off); |
8061 | memcpy (dest: ptr, TREE_STRING_POINTER (expr) + off, n: written); |
8062 | } |
8063 | memset (s: ptr + written, c: 0, n: len - written); |
8064 | } |
8065 | return len; |
8066 | } |
8067 | |
8068 | |
8069 | /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST, |
8070 | FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into |
8071 | the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store |
8072 | anything, just do a dry run. Fail either if OFF is -1 and LEN isn't |
8073 | sufficient to encode the entire EXPR, or if OFF is out of bounds. |
8074 | Otherwise, start at byte offset OFF and encode at most LEN bytes. |
8075 | Return the number of bytes placed in the buffer, or zero upon failure. */ |
8076 | |
8077 | int |
8078 | native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off) |
8079 | { |
8080 | /* We don't support starting at negative offset and -1 is special. */ |
8081 | if (off < -1) |
8082 | return 0; |
8083 | |
8084 | switch (TREE_CODE (expr)) |
8085 | { |
8086 | case INTEGER_CST: |
8087 | return native_encode_int (expr, ptr, len, off); |
8088 | |
8089 | case REAL_CST: |
8090 | return native_encode_real (expr, ptr, len, off); |
8091 | |
8092 | case FIXED_CST: |
8093 | return native_encode_fixed (expr, ptr, len, off); |
8094 | |
8095 | case COMPLEX_CST: |
8096 | return native_encode_complex (expr, ptr, len, off); |
8097 | |
8098 | case VECTOR_CST: |
8099 | return native_encode_vector (expr, ptr, len, off); |
8100 | |
8101 | case STRING_CST: |
8102 | return native_encode_string (expr, ptr, len, off); |
8103 | |
8104 | default: |
8105 | return 0; |
8106 | } |
8107 | } |
8108 | |
8109 | /* Try to find a type whose byte size is smaller or equal to LEN bytes larger |
8110 | or equal to FIELDSIZE bytes, with underlying mode precision/size multiple |
8111 | of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of |
8112 | machine modes, we can't just use build_nonstandard_integer_type. */ |
8113 | |
8114 | tree |
8115 | find_bitfield_repr_type (int fieldsize, int len) |
8116 | { |
8117 | machine_mode mode; |
8118 | for (int pass = 0; pass < 2; pass++) |
8119 | { |
8120 | enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT; |
8121 | FOR_EACH_MODE_IN_CLASS (mode, mclass) |
8122 | if (known_ge (GET_MODE_SIZE (mode), fieldsize) |
8123 | && known_eq (GET_MODE_PRECISION (mode), |
8124 | GET_MODE_BITSIZE (mode)) |
8125 | && known_le (GET_MODE_SIZE (mode), len)) |
8126 | { |
8127 | tree ret = lang_hooks.types.type_for_mode (mode, 1); |
8128 | if (ret && TYPE_MODE (ret) == mode) |
8129 | return ret; |
8130 | } |
8131 | } |
8132 | |
8133 | for (int i = 0; i < NUM_INT_N_ENTS; i ++) |
8134 | if (int_n_enabled_p[i] |
8135 | && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize) |
8136 | && int_n_trees[i].unsigned_type) |
8137 | { |
8138 | tree ret = int_n_trees[i].unsigned_type; |
8139 | mode = TYPE_MODE (ret); |
8140 | if (known_ge (GET_MODE_SIZE (mode), fieldsize) |
8141 | && known_eq (GET_MODE_PRECISION (mode), |
8142 | GET_MODE_BITSIZE (mode)) |
8143 | && known_le (GET_MODE_SIZE (mode), len)) |
8144 | return ret; |
8145 | } |
8146 | |
8147 | return NULL_TREE; |
8148 | } |
8149 | |
8150 | /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs, |
8151 | NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has |
8152 | to be non-NULL and OFF zero), then in addition to filling the |
8153 | bytes pointed by PTR with the value also clear any bits pointed |
8154 | by MASK that are known to be initialized, keep them as is for |
8155 | e.g. uninitialized padding bits or uninitialized fields. */ |
8156 | |
8157 | int |
8158 | native_encode_initializer (tree init, unsigned char *ptr, int len, |
8159 | int off, unsigned char *mask) |
8160 | { |
8161 | int r; |
8162 | |
8163 | /* We don't support starting at negative offset and -1 is special. */ |
8164 | if (off < -1 || init == NULL_TREE) |
8165 | return 0; |
8166 | |
8167 | gcc_assert (mask == NULL || (off == 0 && ptr)); |
8168 | |
8169 | STRIP_NOPS (init); |
8170 | switch (TREE_CODE (init)) |
8171 | { |
8172 | case VIEW_CONVERT_EXPR: |
8173 | case NON_LVALUE_EXPR: |
8174 | return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off, |
8175 | mask); |
8176 | default: |
8177 | r = native_encode_expr (expr: init, ptr, len, off); |
8178 | if (mask) |
8179 | memset (s: mask, c: 0, n: r); |
8180 | return r; |
8181 | case CONSTRUCTOR: |
8182 | tree type = TREE_TYPE (init); |
8183 | HOST_WIDE_INT total_bytes = int_size_in_bytes (type); |
8184 | if (total_bytes < 0) |
8185 | return 0; |
8186 | if ((off == -1 && total_bytes > len) || off >= total_bytes) |
8187 | return 0; |
8188 | int o = off == -1 ? 0 : off; |
8189 | if (TREE_CODE (type) == ARRAY_TYPE) |
8190 | { |
8191 | tree min_index; |
8192 | unsigned HOST_WIDE_INT cnt; |
8193 | HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1; |
8194 | constructor_elt *ce; |
8195 | |
8196 | if (!TYPE_DOMAIN (type) |
8197 | || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST) |
8198 | return 0; |
8199 | |
8200 | fieldsize = int_size_in_bytes (TREE_TYPE (type)); |
8201 | if (fieldsize <= 0) |
8202 | return 0; |
8203 | |
8204 | min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type)); |
8205 | if (ptr) |
8206 | memset (s: ptr, c: '\0', MIN (total_bytes - off, len)); |
8207 | |
8208 | for (cnt = 0; ; cnt++) |
8209 | { |
8210 | tree val = NULL_TREE, index = NULL_TREE; |
8211 | HOST_WIDE_INT pos = curpos, count = 0; |
8212 | bool full = false; |
8213 | if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), ix: cnt, ptr: &ce)) |
8214 | { |
8215 | val = ce->value; |
8216 | index = ce->index; |
8217 | } |
8218 | else if (mask == NULL |
8219 | || CONSTRUCTOR_NO_CLEARING (init) |
8220 | || curpos >= total_bytes) |
8221 | break; |
8222 | else |
8223 | pos = total_bytes; |
8224 | |
8225 | if (index && TREE_CODE (index) == RANGE_EXPR) |
8226 | { |
8227 | if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST |
8228 | || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST) |
8229 | return 0; |
8230 | offset_int wpos |
8231 | = wi::sext (x: wi::to_offset (TREE_OPERAND (index, 0)) |
8232 | - wi::to_offset (t: min_index), |
8233 | TYPE_PRECISION (sizetype)); |
8234 | wpos *= fieldsize; |
8235 | if (!wi::fits_shwi_p (x: pos)) |
8236 | return 0; |
8237 | pos = wpos.to_shwi (); |
8238 | offset_int wcount |
8239 | = wi::sext (x: wi::to_offset (TREE_OPERAND (index, 1)) |
8240 | - wi::to_offset (TREE_OPERAND (index, 0)), |
8241 | TYPE_PRECISION (sizetype)); |
8242 | if (!wi::fits_shwi_p (x: wcount)) |
8243 | return 0; |
8244 | count = wcount.to_shwi (); |
8245 | } |
8246 | else if (index) |
8247 | { |
8248 | if (TREE_CODE (index) != INTEGER_CST) |
8249 | return 0; |
8250 | offset_int wpos |
8251 | = wi::sext (x: wi::to_offset (t: index) |
8252 | - wi::to_offset (t: min_index), |
8253 | TYPE_PRECISION (sizetype)); |
8254 | wpos *= fieldsize; |
8255 | if (!wi::fits_shwi_p (x: wpos)) |
8256 | return 0; |
8257 | pos = wpos.to_shwi (); |
8258 | } |
8259 | |
8260 | if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos) |
8261 | { |
8262 | if (valueinit == -1) |
8263 | { |
8264 | tree zero = build_zero_cst (TREE_TYPE (type)); |
8265 | r = native_encode_initializer (init: zero, ptr: ptr + curpos, |
8266 | len: fieldsize, off: 0, |
8267 | mask: mask + curpos); |
8268 | if (TREE_CODE (zero) == CONSTRUCTOR) |
8269 | ggc_free (zero); |
8270 | if (!r) |
8271 | return 0; |
8272 | valueinit = curpos; |
8273 | curpos += fieldsize; |
8274 | } |
8275 | while (curpos != pos) |
8276 | { |
8277 | memcpy (dest: ptr + curpos, src: ptr + valueinit, n: fieldsize); |
8278 | memcpy (dest: mask + curpos, src: mask + valueinit, n: fieldsize); |
8279 | curpos += fieldsize; |
8280 | } |
8281 | } |
8282 | |
8283 | curpos = pos; |
8284 | if (val) |
8285 | do |
8286 | { |
8287 | if (off == -1 |
8288 | || (curpos >= off |
8289 | && (curpos + fieldsize |
8290 | <= (HOST_WIDE_INT) off + len))) |
8291 | { |
8292 | if (full) |
8293 | { |
8294 | if (ptr) |
8295 | memcpy (dest: ptr + (curpos - o), src: ptr + (pos - o), |
8296 | n: fieldsize); |
8297 | if (mask) |
8298 | memcpy (dest: mask + curpos, src: mask + pos, n: fieldsize); |
8299 | } |
8300 | else if (!native_encode_initializer (init: val, |
8301 | ptr: ptr |
8302 | ? ptr + curpos - o |
8303 | : NULL, |
8304 | len: fieldsize, |
8305 | off: off == -1 ? -1 |
8306 | : 0, |
8307 | mask: mask |
8308 | ? mask + curpos |
8309 | : NULL)) |
8310 | return 0; |
8311 | else |
8312 | { |
8313 | full = true; |
8314 | pos = curpos; |
8315 | } |
8316 | } |
8317 | else if (curpos + fieldsize > off |
8318 | && curpos < (HOST_WIDE_INT) off + len) |
8319 | { |
8320 | /* Partial overlap. */ |
8321 | unsigned char *p = NULL; |
8322 | int no = 0; |
8323 | int l; |
8324 | gcc_assert (mask == NULL); |
8325 | if (curpos >= off) |
8326 | { |
8327 | if (ptr) |
8328 | p = ptr + curpos - off; |
8329 | l = MIN ((HOST_WIDE_INT) off + len - curpos, |
8330 | fieldsize); |
8331 | } |
8332 | else |
8333 | { |
8334 | p = ptr; |
8335 | no = off - curpos; |
8336 | l = len; |
8337 | } |
8338 | if (!native_encode_initializer (init: val, ptr: p, len: l, off: no, NULL)) |
8339 | return 0; |
8340 | } |
8341 | curpos += fieldsize; |
8342 | } |
8343 | while (count-- != 0); |
8344 | } |
8345 | return MIN (total_bytes - off, len); |
8346 | } |
8347 | else if (TREE_CODE (type) == RECORD_TYPE |
8348 | || TREE_CODE (type) == UNION_TYPE) |
8349 | { |
8350 | unsigned HOST_WIDE_INT cnt; |
8351 | constructor_elt *ce; |
8352 | tree fld_base = TYPE_FIELDS (type); |
8353 | tree to_free = NULL_TREE; |
8354 | |
8355 | gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL); |
8356 | if (ptr != NULL) |
8357 | memset (s: ptr, c: '\0', MIN (total_bytes - o, len)); |
8358 | for (cnt = 0; ; cnt++) |
8359 | { |
8360 | tree val = NULL_TREE, field = NULL_TREE; |
8361 | HOST_WIDE_INT pos = 0, fieldsize; |
8362 | unsigned HOST_WIDE_INT bpos = 0, epos = 0; |
8363 | |
8364 | if (to_free) |
8365 | { |
8366 | ggc_free (to_free); |
8367 | to_free = NULL_TREE; |
8368 | } |
8369 | |
8370 | if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), ix: cnt, ptr: &ce)) |
8371 | { |
8372 | val = ce->value; |
8373 | field = ce->index; |
8374 | if (field == NULL_TREE) |
8375 | return 0; |
8376 | |
8377 | pos = int_byte_position (field); |
8378 | if (off != -1 && (HOST_WIDE_INT) off + len <= pos) |
8379 | continue; |
8380 | } |
8381 | else if (mask == NULL |
8382 | || CONSTRUCTOR_NO_CLEARING (init)) |
8383 | break; |
8384 | else |
8385 | pos = total_bytes; |
8386 | |
8387 | if (mask && !CONSTRUCTOR_NO_CLEARING (init)) |
8388 | { |
8389 | tree fld; |
8390 | for (fld = fld_base; fld; fld = DECL_CHAIN (fld)) |
8391 | { |
8392 | if (TREE_CODE (fld) != FIELD_DECL) |
8393 | continue; |
8394 | if (fld == field) |
8395 | break; |
8396 | if (DECL_PADDING_P (fld)) |
8397 | continue; |
8398 | if (DECL_SIZE_UNIT (fld) == NULL_TREE |
8399 | || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld))) |
8400 | return 0; |
8401 | if (integer_zerop (DECL_SIZE_UNIT (fld))) |
8402 | continue; |
8403 | break; |
8404 | } |
8405 | if (fld == NULL_TREE) |
8406 | { |
8407 | if (ce == NULL) |
8408 | break; |
8409 | return 0; |
8410 | } |
8411 | fld_base = DECL_CHAIN (fld); |
8412 | if (fld != field) |
8413 | { |
8414 | cnt--; |
8415 | field = fld; |
8416 | pos = int_byte_position (field); |
8417 | val = build_zero_cst (TREE_TYPE (fld)); |
8418 | if (TREE_CODE (val) == CONSTRUCTOR) |
8419 | to_free = val; |
8420 | } |
8421 | } |
8422 | |
8423 | if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE |
8424 | && TYPE_DOMAIN (TREE_TYPE (field)) |
8425 | && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field)))) |
8426 | { |
8427 | if (mask || off != -1) |
8428 | return 0; |
8429 | if (val == NULL_TREE) |
8430 | continue; |
8431 | if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE) |
8432 | return 0; |
8433 | fieldsize = int_size_in_bytes (TREE_TYPE (val)); |
8434 | if (fieldsize < 0 |
8435 | || (int) fieldsize != fieldsize |
8436 | || (pos + fieldsize) > INT_MAX) |
8437 | return 0; |
8438 | if (pos + fieldsize > total_bytes) |
8439 | { |
8440 | if (ptr != NULL && total_bytes < len) |
8441 | memset (s: ptr + total_bytes, c: '\0', |
8442 | MIN (pos + fieldsize, len) - total_bytes); |
8443 | total_bytes = pos + fieldsize; |
8444 | } |
8445 | } |
8446 | else |
8447 | { |
8448 | if (DECL_SIZE_UNIT (field) == NULL_TREE |
8449 | || !tree_fits_shwi_p (DECL_SIZE_UNIT (field))) |
8450 | return 0; |
8451 | fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field)); |
8452 | } |
8453 | if (fieldsize == 0) |
8454 | continue; |
8455 | |
8456 | /* Prepare to deal with integral bit-fields and filter out other |
8457 | bit-fields that do not start and end on a byte boundary. */ |
8458 | if (DECL_BIT_FIELD (field)) |
8459 | { |
8460 | if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field))) |
8461 | return 0; |
8462 | bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); |
8463 | if (INTEGRAL_TYPE_P (TREE_TYPE (field))) |
8464 | { |
8465 | bpos %= BITS_PER_UNIT; |
8466 | fieldsize = TYPE_PRECISION (TREE_TYPE (field)) + bpos; |
8467 | epos = fieldsize % BITS_PER_UNIT; |
8468 | fieldsize += BITS_PER_UNIT - 1; |
8469 | fieldsize /= BITS_PER_UNIT; |
8470 | } |
8471 | else if (bpos % BITS_PER_UNIT |
8472 | || DECL_SIZE (field) == NULL_TREE |
8473 | || !tree_fits_shwi_p (DECL_SIZE (field)) |
8474 | || tree_to_shwi (DECL_SIZE (field)) % BITS_PER_UNIT) |
8475 | return 0; |
8476 | } |
8477 | |
8478 | if (off != -1 && pos + fieldsize <= off) |
8479 | continue; |
8480 | |
8481 | if (val == NULL_TREE) |
8482 | continue; |
8483 | |
8484 | if (DECL_BIT_FIELD (field) |
8485 | && INTEGRAL_TYPE_P (TREE_TYPE (field))) |
8486 | { |
8487 | /* FIXME: Handle PDP endian. */ |
8488 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
8489 | return 0; |
8490 | |
8491 | if (TREE_CODE (val) != INTEGER_CST) |
8492 | return 0; |
8493 | |
8494 | tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field); |
8495 | tree repr_type = NULL_TREE; |
8496 | HOST_WIDE_INT rpos = 0; |
8497 | if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr))) |
8498 | { |
8499 | rpos = int_byte_position (repr); |
8500 | repr_type = TREE_TYPE (repr); |
8501 | } |
8502 | else |
8503 | { |
8504 | repr_type = find_bitfield_repr_type (fieldsize, len); |
8505 | if (repr_type == NULL_TREE) |
8506 | return 0; |
8507 | HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type); |
8508 | gcc_assert (repr_size > 0 && repr_size <= len); |
8509 | if (pos + repr_size <= o + len) |
8510 | rpos = pos; |
8511 | else |
8512 | { |
8513 | rpos = o + len - repr_size; |
8514 | gcc_assert (rpos <= pos); |
8515 | } |
8516 | } |
8517 | |
8518 | if (rpos > pos) |
8519 | return 0; |
8520 | wide_int w = wi::to_wide (t: val, TYPE_PRECISION (repr_type)); |
8521 | int diff = (TYPE_PRECISION (repr_type) |
8522 | - TYPE_PRECISION (TREE_TYPE (field))); |
8523 | HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos; |
8524 | if (!BYTES_BIG_ENDIAN) |
8525 | w = wi::lshift (x: w, y: bitoff); |
8526 | else |
8527 | w = wi::lshift (x: w, y: diff - bitoff); |
8528 | val = wide_int_to_tree (type: repr_type, cst: w); |
8529 | |
8530 | unsigned char buf[MAX_BITSIZE_MODE_ANY_INT |
8531 | / BITS_PER_UNIT + 1]; |
8532 | int l = native_encode_int (expr: val, ptr: buf, len: sizeof buf, off: 0); |
8533 | if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type)) |
8534 | return 0; |
8535 | |
8536 | if (ptr == NULL) |
8537 | continue; |
8538 | |
8539 | /* If the bitfield does not start at byte boundary, handle |
8540 | the partial byte at the start. */ |
8541 | if (bpos |
8542 | && (off == -1 || (pos >= off && len >= 1))) |
8543 | { |
8544 | if (!BYTES_BIG_ENDIAN) |
8545 | { |
8546 | int msk = (1 << bpos) - 1; |
8547 | buf[pos - rpos] &= ~msk; |
8548 | buf[pos - rpos] |= ptr[pos - o] & msk; |
8549 | if (mask) |
8550 | { |
8551 | if (fieldsize > 1 || epos == 0) |
8552 | mask[pos] &= msk; |
8553 | else |
8554 | mask[pos] &= (msk | ~((1 << epos) - 1)); |
8555 | } |
8556 | } |
8557 | else |
8558 | { |
8559 | int msk = (1 << (BITS_PER_UNIT - bpos)) - 1; |
8560 | buf[pos - rpos] &= msk; |
8561 | buf[pos - rpos] |= ptr[pos - o] & ~msk; |
8562 | if (mask) |
8563 | { |
8564 | if (fieldsize > 1 || epos == 0) |
8565 | mask[pos] &= ~msk; |
8566 | else |
8567 | mask[pos] &= (~msk |
8568 | | ((1 << (BITS_PER_UNIT - epos)) |
8569 | - 1)); |
8570 | } |
8571 | } |
8572 | } |
8573 | /* If the bitfield does not end at byte boundary, handle |
8574 | the partial byte at the end. */ |
8575 | if (epos |
8576 | && (off == -1 |
8577 | || pos + fieldsize <= (HOST_WIDE_INT) off + len)) |
8578 | { |
8579 | if (!BYTES_BIG_ENDIAN) |
8580 | { |
8581 | int msk = (1 << epos) - 1; |
8582 | buf[pos - rpos + fieldsize - 1] &= msk; |
8583 | buf[pos - rpos + fieldsize - 1] |
8584 | |= ptr[pos + fieldsize - 1 - o] & ~msk; |
8585 | if (mask && (fieldsize > 1 || bpos == 0)) |
8586 | mask[pos + fieldsize - 1] &= ~msk; |
8587 | } |
8588 | else |
8589 | { |
8590 | int msk = (1 << (BITS_PER_UNIT - epos)) - 1; |
8591 | buf[pos - rpos + fieldsize - 1] &= ~msk; |
8592 | buf[pos - rpos + fieldsize - 1] |
8593 | |= ptr[pos + fieldsize - 1 - o] & msk; |
8594 | if (mask && (fieldsize > 1 || bpos == 0)) |
8595 | mask[pos + fieldsize - 1] &= msk; |
8596 | } |
8597 | } |
8598 | if (off == -1 |
8599 | || (pos >= off |
8600 | && (pos + fieldsize <= (HOST_WIDE_INT) off + len))) |
8601 | { |
8602 | memcpy (dest: ptr + pos - o, src: buf + (pos - rpos), n: fieldsize); |
8603 | if (mask && (fieldsize > (bpos != 0) + (epos != 0))) |
8604 | memset (s: mask + pos + (bpos != 0), c: 0, |
8605 | n: fieldsize - (bpos != 0) - (epos != 0)); |
8606 | } |
8607 | else |
8608 | { |
8609 | /* Partial overlap. */ |
8610 | HOST_WIDE_INT fsz = fieldsize; |
8611 | gcc_assert (mask == NULL); |
8612 | if (pos < off) |
8613 | { |
8614 | fsz -= (off - pos); |
8615 | pos = off; |
8616 | } |
8617 | if (pos + fsz > (HOST_WIDE_INT) off + len) |
8618 | fsz = (HOST_WIDE_INT) off + len - pos; |
8619 | memcpy (dest: ptr + pos - off, src: buf + (pos - rpos), n: fsz); |
8620 | } |
8621 | continue; |
8622 | } |
8623 | |
8624 | if (off == -1 |
8625 | || (pos >= off |
8626 | && (pos + fieldsize <= (HOST_WIDE_INT) off + len))) |
8627 | { |
8628 | int fldsize = fieldsize; |
8629 | if (off == -1) |
8630 | { |
8631 | tree fld = DECL_CHAIN (field); |
8632 | while (fld) |
8633 | { |
8634 | if (TREE_CODE (fld) == FIELD_DECL) |
8635 | break; |
8636 | fld = DECL_CHAIN (fld); |
8637 | } |
8638 | if (fld == NULL_TREE) |
8639 | fldsize = len - pos; |
8640 | } |
8641 | r = native_encode_initializer (init: val, ptr: ptr ? ptr + pos - o |
8642 | : NULL, |
8643 | len: fldsize, |
8644 | off: off == -1 ? -1 : 0, |
8645 | mask: mask ? mask + pos : NULL); |
8646 | if (!r) |
8647 | return 0; |
8648 | if (off == -1 |
8649 | && fldsize != fieldsize |
8650 | && r > fieldsize |
8651 | && pos + r > total_bytes) |
8652 | total_bytes = pos + r; |
8653 | } |
8654 | else |
8655 | { |
8656 | /* Partial overlap. */ |
8657 | unsigned char *p = NULL; |
8658 | int no = 0; |
8659 | int l; |
8660 | gcc_assert (mask == NULL); |
8661 | if (pos >= off) |
8662 | { |
8663 | if (ptr) |
8664 | p = ptr + pos - off; |
8665 | l = MIN ((HOST_WIDE_INT) off + len - pos, |
8666 | fieldsize); |
8667 | } |
8668 | else |
8669 | { |
8670 | p = ptr; |
8671 | no = off - pos; |
8672 | l = len; |
8673 | } |
8674 | if (!native_encode_initializer (init: val, ptr: p, len: l, off: no, NULL)) |
8675 | return 0; |
8676 | } |
8677 | } |
8678 | return MIN (total_bytes - off, len); |
8679 | } |
8680 | return 0; |
8681 | } |
8682 | } |
8683 | |
8684 | |
8685 | /* Subroutine of native_interpret_expr. Interpret the contents of |
8686 | the buffer PTR of length LEN as an INTEGER_CST of type TYPE. |
8687 | If the buffer cannot be interpreted, return NULL_TREE. */ |
8688 | |
8689 | static tree |
8690 | native_interpret_int (tree type, const unsigned char *ptr, int len) |
8691 | { |
8692 | int total_bytes; |
8693 | if (TREE_CODE (type) == BITINT_TYPE) |
8694 | { |
8695 | struct bitint_info info; |
8696 | bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info); |
8697 | gcc_assert (ok); |
8698 | scalar_int_mode limb_mode = as_a <scalar_int_mode> (m: info.limb_mode); |
8699 | if (TYPE_PRECISION (type) > GET_MODE_PRECISION (mode: limb_mode)) |
8700 | { |
8701 | total_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (type)); |
8702 | /* More work is needed when adding _BitInt support to PDP endian |
8703 | if limb is smaller than word, or if _BitInt limb ordering doesn't |
8704 | match target endianity here. */ |
8705 | gcc_checking_assert (info.big_endian == WORDS_BIG_ENDIAN |
8706 | && (BYTES_BIG_ENDIAN == WORDS_BIG_ENDIAN |
8707 | || (GET_MODE_SIZE (limb_mode) |
8708 | >= UNITS_PER_WORD))); |
8709 | } |
8710 | else |
8711 | total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)); |
8712 | } |
8713 | else |
8714 | total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)); |
8715 | |
8716 | if (total_bytes > len |
8717 | || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) |
8718 | return NULL_TREE; |
8719 | |
8720 | wide_int result = wi::from_buffer (ptr, total_bytes); |
8721 | |
8722 | return wide_int_to_tree (type, cst: result); |
8723 | } |
8724 | |
8725 | |
8726 | /* Subroutine of native_interpret_expr. Interpret the contents of |
8727 | the buffer PTR of length LEN as a FIXED_CST of type TYPE. |
8728 | If the buffer cannot be interpreted, return NULL_TREE. */ |
8729 | |
8730 | static tree |
8731 | native_interpret_fixed (tree type, const unsigned char *ptr, int len) |
8732 | { |
8733 | scalar_mode mode = SCALAR_TYPE_MODE (type); |
8734 | int total_bytes = GET_MODE_SIZE (mode); |
8735 | double_int result; |
8736 | FIXED_VALUE_TYPE fixed_value; |
8737 | |
8738 | if (total_bytes > len |
8739 | || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT) |
8740 | return NULL_TREE; |
8741 | |
8742 | result = double_int::from_buffer (buffer: ptr, len: total_bytes); |
8743 | fixed_value = fixed_from_double_int (result, mode); |
8744 | |
8745 | return build_fixed (type, fixed_value); |
8746 | } |
8747 | |
8748 | |
8749 | /* Subroutine of native_interpret_expr. Interpret the contents of |
8750 | the buffer PTR of length LEN as a REAL_CST of type TYPE. |
8751 | If the buffer cannot be interpreted, return NULL_TREE. */ |
8752 | |
8753 | tree |
8754 | native_interpret_real (tree type, const unsigned char *ptr, int len) |
8755 | { |
8756 | scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type); |
8757 | int total_bytes = GET_MODE_SIZE (mode); |
8758 | unsigned char value; |
8759 | /* There are always 32 bits in each long, no matter the size of |
8760 | the hosts long. We handle floating point representations with |
8761 | up to 192 bits. */ |
8762 | REAL_VALUE_TYPE r; |
8763 | long tmp[6]; |
8764 | |
8765 | if (total_bytes > len || total_bytes > 24) |
8766 | return NULL_TREE; |
8767 | int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD; |
8768 | |
8769 | memset (s: tmp, c: 0, n: sizeof (tmp)); |
8770 | for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT; |
8771 | bitpos += BITS_PER_UNIT) |
8772 | { |
8773 | /* Both OFFSET and BYTE index within a long; |
8774 | bitpos indexes the whole float. */ |
8775 | int offset, byte = (bitpos / BITS_PER_UNIT) & 3; |
8776 | if (UNITS_PER_WORD < 4) |
8777 | { |
8778 | int word = byte / UNITS_PER_WORD; |
8779 | if (WORDS_BIG_ENDIAN) |
8780 | word = (words - 1) - word; |
8781 | offset = word * UNITS_PER_WORD; |
8782 | if (BYTES_BIG_ENDIAN) |
8783 | offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD); |
8784 | else |
8785 | offset += byte % UNITS_PER_WORD; |
8786 | } |
8787 | else |
8788 | { |
8789 | offset = byte; |
8790 | if (BYTES_BIG_ENDIAN) |
8791 | { |
8792 | /* Reverse bytes within each long, or within the entire float |
8793 | if it's smaller than a long (for HFmode). */ |
8794 | offset = MIN (3, total_bytes - 1) - offset; |
8795 | gcc_assert (offset >= 0); |
8796 | } |
8797 | } |
8798 | value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)]; |
8799 | |
8800 | tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31); |
8801 | } |
8802 | |
8803 | real_from_target (&r, tmp, mode); |
8804 | return build_real (type, r); |
8805 | } |
8806 | |
8807 | |
8808 | /* Subroutine of native_interpret_expr. Interpret the contents of |
8809 | the buffer PTR of length LEN as a COMPLEX_CST of type TYPE. |
8810 | If the buffer cannot be interpreted, return NULL_TREE. */ |
8811 | |
8812 | static tree |
8813 | native_interpret_complex (tree type, const unsigned char *ptr, int len) |
8814 | { |
8815 | tree etype, rpart, ipart; |
8816 | int size; |
8817 | |
8818 | etype = TREE_TYPE (type); |
8819 | size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype)); |
8820 | if (size * 2 > len) |
8821 | return NULL_TREE; |
8822 | rpart = native_interpret_expr (etype, ptr, size); |
8823 | if (!rpart) |
8824 | return NULL_TREE; |
8825 | ipart = native_interpret_expr (etype, ptr+size, size); |
8826 | if (!ipart) |
8827 | return NULL_TREE; |
8828 | return build_complex (type, rpart, ipart); |
8829 | } |
8830 | |
8831 | /* Read a vector of type TYPE from the target memory image given by BYTES, |
8832 | which contains LEN bytes. The vector is known to be encodable using |
8833 | NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each. |
8834 | |
8835 | Return the vector on success, otherwise return null. */ |
8836 | |
8837 | static tree |
8838 | native_interpret_vector_part (tree type, const unsigned char *bytes, |
8839 | unsigned int len, unsigned int npatterns, |
8840 | unsigned int nelts_per_pattern) |
8841 | { |
8842 | tree elt_type = TREE_TYPE (type); |
8843 | if (VECTOR_BOOLEAN_TYPE_P (type) |
8844 | && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT) |
8845 | { |
8846 | /* This is the only case in which elements can be smaller than a byte. |
8847 | Element 0 is always in the lsb of the containing byte. */ |
8848 | unsigned int elt_bits = TYPE_PRECISION (elt_type); |
8849 | if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT) |
8850 | return NULL_TREE; |
8851 | |
8852 | tree_vector_builder builder (type, npatterns, nelts_per_pattern); |
8853 | for (unsigned int i = 0; i < builder.encoded_nelts (); ++i) |
8854 | { |
8855 | unsigned int bit_index = i * elt_bits; |
8856 | unsigned int byte_index = bit_index / BITS_PER_UNIT; |
8857 | unsigned int lsb = bit_index % BITS_PER_UNIT; |
8858 | builder.quick_push (obj: bytes[byte_index] & (1 << lsb) |
8859 | ? build_all_ones_cst (elt_type) |
8860 | : build_zero_cst (elt_type)); |
8861 | } |
8862 | return builder.build (); |
8863 | } |
8864 | |
8865 | unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type)); |
8866 | if (elt_bytes * npatterns * nelts_per_pattern > len) |
8867 | return NULL_TREE; |
8868 | |
8869 | tree_vector_builder builder (type, npatterns, nelts_per_pattern); |
8870 | for (unsigned int i = 0; i < builder.encoded_nelts (); ++i) |
8871 | { |
8872 | tree elt = native_interpret_expr (elt_type, bytes, elt_bytes); |
8873 | if (!elt) |
8874 | return NULL_TREE; |
8875 | builder.quick_push (obj: elt); |
8876 | bytes += elt_bytes; |
8877 | } |
8878 | return builder.build (); |
8879 | } |
8880 | |
8881 | /* Subroutine of native_interpret_expr. Interpret the contents of |
8882 | the buffer PTR of length LEN as a VECTOR_CST of type TYPE. |
8883 | If the buffer cannot be interpreted, return NULL_TREE. */ |
8884 | |
8885 | static tree |
8886 | native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len) |
8887 | { |
8888 | unsigned HOST_WIDE_INT size; |
8889 | |
8890 | if (!tree_to_poly_uint64 (TYPE_SIZE_UNIT (type)).is_constant (const_value: &size) |
8891 | || size > len) |
8892 | return NULL_TREE; |
8893 | |
8894 | unsigned HOST_WIDE_INT count = TYPE_VECTOR_SUBPARTS (node: type).to_constant (); |
8895 | return native_interpret_vector_part (type, bytes: ptr, len, npatterns: count, nelts_per_pattern: 1); |
8896 | } |
8897 | |
8898 | |
8899 | /* Subroutine of fold_view_convert_expr. Interpret the contents of |
8900 | the buffer PTR of length LEN as a constant of type TYPE. For |
8901 | INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P |
8902 | we return a REAL_CST, etc... If the buffer cannot be interpreted, |
8903 | return NULL_TREE. */ |
8904 | |
8905 | tree |
8906 | native_interpret_expr (tree type, const unsigned char *ptr, int len) |
8907 | { |
8908 | switch (TREE_CODE (type)) |
8909 | { |
8910 | case INTEGER_TYPE: |
8911 | case ENUMERAL_TYPE: |
8912 | case BOOLEAN_TYPE: |
8913 | case POINTER_TYPE: |
8914 | case REFERENCE_TYPE: |
8915 | case OFFSET_TYPE: |
8916 | case BITINT_TYPE: |
8917 | return native_interpret_int (type, ptr, len); |
8918 | |
8919 | case REAL_TYPE: |
8920 | if (tree ret = native_interpret_real (type, ptr, len)) |
8921 | { |
8922 | /* For floating point values in composite modes, punt if this |
8923 | folding doesn't preserve bit representation. As the mode doesn't |
8924 | have fixed precision while GCC pretends it does, there could be |
8925 | valid values that GCC can't really represent accurately. |
8926 | See PR95450. Even for other modes, e.g. x86 XFmode can have some |
8927 | bit combinationations which GCC doesn't preserve. */ |
8928 | unsigned char buf[24 * 2]; |
8929 | scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type); |
8930 | int total_bytes = GET_MODE_SIZE (mode); |
8931 | memcpy (dest: buf + 24, src: ptr, n: total_bytes); |
8932 | clear_type_padding_in_mask (type, buf + 24); |
8933 | if (native_encode_expr (expr: ret, ptr: buf, len: total_bytes, off: 0) != total_bytes |
8934 | || memcmp (s1: buf + 24, s2: buf, n: total_bytes) != 0) |
8935 | return NULL_TREE; |
8936 | return ret; |
8937 | } |
8938 | return NULL_TREE; |
8939 | |
8940 | case FIXED_POINT_TYPE: |
8941 | return native_interpret_fixed (type, ptr, len); |
8942 | |
8943 | case COMPLEX_TYPE: |
8944 | return native_interpret_complex (type, ptr, len); |
8945 | |
8946 | case VECTOR_TYPE: |
8947 | return native_interpret_vector (type, ptr, len); |
8948 | |
8949 | default: |
8950 | return NULL_TREE; |
8951 | } |
8952 | } |
8953 | |
8954 | /* Returns true if we can interpret the contents of a native encoding |
8955 | as TYPE. */ |
8956 | |
8957 | bool |
8958 | can_native_interpret_type_p (tree type) |
8959 | { |
8960 | switch (TREE_CODE (type)) |
8961 | { |
8962 | case INTEGER_TYPE: |
8963 | case ENUMERAL_TYPE: |
8964 | case BOOLEAN_TYPE: |
8965 | case POINTER_TYPE: |
8966 | case REFERENCE_TYPE: |
8967 | case FIXED_POINT_TYPE: |
8968 | case REAL_TYPE: |
8969 | case COMPLEX_TYPE: |
8970 | case VECTOR_TYPE: |
8971 | case OFFSET_TYPE: |
8972 | return true; |
8973 | default: |
8974 | return false; |
8975 | } |
8976 | } |
8977 | |
8978 | /* Attempt to interpret aggregate of TYPE from bytes encoded in target |
8979 | byte order at PTR + OFF with LEN bytes. Does not handle unions. */ |
8980 | |
8981 | tree |
8982 | native_interpret_aggregate (tree type, const unsigned char *ptr, int off, |
8983 | int len) |
8984 | { |
8985 | vec<constructor_elt, va_gc> *elts = NULL; |
8986 | if (TREE_CODE (type) == ARRAY_TYPE) |
8987 | { |
8988 | HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type)); |
8989 | if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE) |
8990 | return NULL_TREE; |
8991 | |
8992 | HOST_WIDE_INT cnt = 0; |
8993 | if (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) |
8994 | { |
8995 | if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))) |
8996 | return NULL_TREE; |
8997 | cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1; |
8998 | } |
8999 | if (eltsz == 0) |
9000 | cnt = 0; |
9001 | HOST_WIDE_INT pos = 0; |
9002 | for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz) |
9003 | { |
9004 | tree v = NULL_TREE; |
9005 | if (pos >= len || pos + eltsz > len) |
9006 | return NULL_TREE; |
9007 | if (can_native_interpret_type_p (TREE_TYPE (type))) |
9008 | { |
9009 | v = native_interpret_expr (TREE_TYPE (type), |
9010 | ptr: ptr + off + pos, len: eltsz); |
9011 | if (v == NULL_TREE) |
9012 | return NULL_TREE; |
9013 | } |
9014 | else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE |
9015 | || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE) |
9016 | v = native_interpret_aggregate (TREE_TYPE (type), ptr, off: off + pos, |
9017 | len: eltsz); |
9018 | if (v == NULL_TREE) |
9019 | return NULL_TREE; |
9020 | CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v); |
9021 | } |
9022 | return build_constructor (type, elts); |
9023 | } |
9024 | if (TREE_CODE (type) != RECORD_TYPE) |
9025 | return NULL_TREE; |
9026 | for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) |
9027 | { |
9028 | if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field) |
9029 | || is_empty_type (TREE_TYPE (field))) |
9030 | continue; |
9031 | tree fld = field; |
9032 | HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0; |
9033 | int diff = 0; |
9034 | tree v = NULL_TREE; |
9035 | if (DECL_BIT_FIELD (field)) |
9036 | { |
9037 | fld = DECL_BIT_FIELD_REPRESENTATIVE (field); |
9038 | if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld))) |
9039 | { |
9040 | poly_int64 bitoffset; |
9041 | poly_uint64 field_offset, fld_offset; |
9042 | if (poly_int_tree_p (DECL_FIELD_OFFSET (field), value: &field_offset) |
9043 | && poly_int_tree_p (DECL_FIELD_OFFSET (fld), value: &fld_offset)) |
9044 | bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT; |
9045 | else |
9046 | bitoffset = 0; |
9047 | bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)) |
9048 | - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld))); |
9049 | diff = (TYPE_PRECISION (TREE_TYPE (fld)) |
9050 | - TYPE_PRECISION (TREE_TYPE (field))); |
9051 | if (!bitoffset.is_constant (const_value: &bitoff) |
9052 | || bitoff < 0 |
9053 | || bitoff > diff) |
9054 | return NULL_TREE; |
9055 | } |
9056 | else |
9057 | { |
9058 | if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field))) |
9059 | return NULL_TREE; |
9060 | int fieldsize = TYPE_PRECISION (TREE_TYPE (field)); |
9061 | int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field)); |
9062 | bpos %= BITS_PER_UNIT; |
9063 | fieldsize += bpos; |
9064 | fieldsize += BITS_PER_UNIT - 1; |
9065 | fieldsize /= BITS_PER_UNIT; |
9066 | tree repr_type = find_bitfield_repr_type (fieldsize, len); |
9067 | if (repr_type == NULL_TREE) |
9068 | return NULL_TREE; |
9069 | sz = int_size_in_bytes (repr_type); |
9070 | if (sz < 0 || sz > len) |
9071 | return NULL_TREE; |
9072 | pos = int_byte_position (field); |
9073 | if (pos < 0 || pos > len || pos + fieldsize > len) |
9074 | return NULL_TREE; |
9075 | HOST_WIDE_INT rpos; |
9076 | if (pos + sz <= len) |
9077 | rpos = pos; |
9078 | else |
9079 | { |
9080 | rpos = len - sz; |
9081 | gcc_assert (rpos <= pos); |
9082 | } |
9083 | bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos; |
9084 | pos = rpos; |
9085 | diff = (TYPE_PRECISION (repr_type) |
9086 | - TYPE_PRECISION (TREE_TYPE (field))); |
9087 | v = native_interpret_expr (type: repr_type, ptr: ptr + off + pos, len: sz); |
9088 | if (v == NULL_TREE) |
9089 | return NULL_TREE; |
9090 | fld = NULL_TREE; |
9091 | } |
9092 | } |
9093 | |
9094 | if (fld) |
9095 | { |
9096 | sz = int_size_in_bytes (TREE_TYPE (fld)); |
9097 | if (sz < 0 || sz > len) |
9098 | return NULL_TREE; |
9099 | tree byte_pos = byte_position (fld); |
9100 | if (!tree_fits_shwi_p (byte_pos)) |
9101 | return NULL_TREE; |
9102 | pos = tree_to_shwi (byte_pos); |
9103 | if (pos < 0 || pos > len || pos + sz > len) |
9104 | return NULL_TREE; |
9105 | } |
9106 | if (fld == NULL_TREE) |
9107 | /* Already handled above. */; |
9108 | else if (can_native_interpret_type_p (TREE_TYPE (fld))) |
9109 | { |
9110 | v = native_interpret_expr (TREE_TYPE (fld), |
9111 | ptr: ptr + off + pos, len: sz); |
9112 | if (v == NULL_TREE) |
9113 | return NULL_TREE; |
9114 | } |
9115 | else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE |
9116 | || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE) |
9117 | v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off: off + pos, len: sz); |
9118 | if (v == NULL_TREE) |
9119 | return NULL_TREE; |
9120 | if (fld != field) |
9121 | { |
9122 | if (TREE_CODE (v) != INTEGER_CST) |
9123 | return NULL_TREE; |
9124 | |
9125 | /* FIXME: Figure out how to handle PDP endian bitfields. */ |
9126 | if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN) |
9127 | return NULL_TREE; |
9128 | if (!BYTES_BIG_ENDIAN) |
9129 | v = wide_int_to_tree (TREE_TYPE (field), |
9130 | cst: wi::lrshift (x: wi::to_wide (t: v), y: bitoff)); |
9131 | else |
9132 | v = wide_int_to_tree (TREE_TYPE (field), |
9133 | cst: wi::lrshift (x: wi::to_wide (t: v), |
9134 | y: diff - bitoff)); |
9135 | } |
9136 | CONSTRUCTOR_APPEND_ELT (elts, field, v); |
9137 | } |
9138 | return build_constructor (type, elts); |
9139 | } |
9140 | |
9141 | /* Routines for manipulation of native_encode_expr encoded data if the encoded |
9142 | or extracted constant positions and/or sizes aren't byte aligned. */ |
9143 | |
9144 | /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the |
9145 | bits between adjacent elements. AMNT should be within |
9146 | [0, BITS_PER_UNIT). |
9147 | Example, AMNT = 2: |
9148 | 00011111|11100000 << 2 = 01111111|10000000 |
9149 | PTR[1] | PTR[0] PTR[1] | PTR[0]. */ |
9150 | |
9151 | void |
9152 | shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz, |
9153 | unsigned int amnt) |
9154 | { |
9155 | if (amnt == 0) |
9156 | return; |
9157 | |
9158 | unsigned char carry_over = 0U; |
9159 | unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt); |
9160 | unsigned char clear_mask = (~0U) << amnt; |
9161 | |
9162 | for (unsigned int i = 0; i < sz; i++) |
9163 | { |
9164 | unsigned prev_carry_over = carry_over; |
9165 | carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt); |
9166 | |
9167 | ptr[i] <<= amnt; |
9168 | if (i != 0) |
9169 | { |
9170 | ptr[i] &= clear_mask; |
9171 | ptr[i] |= prev_carry_over; |
9172 | } |
9173 | } |
9174 | } |
9175 | |
9176 | /* Like shift_bytes_in_array_left but for big-endian. |
9177 | Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the |
9178 | bits between adjacent elements. AMNT should be within |
9179 | [0, BITS_PER_UNIT). |
9180 | Example, AMNT = 2: |
9181 | 00011111|11100000 >> 2 = 00000111|11111000 |
9182 | PTR[0] | PTR[1] PTR[0] | PTR[1]. */ |
9183 | |
9184 | void |
9185 | shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz, |
9186 | unsigned int amnt) |
9187 | { |
9188 | if (amnt == 0) |
9189 | return; |
9190 | |
9191 | unsigned char carry_over = 0U; |
9192 | unsigned char carry_mask = ~(~0U << amnt); |
9193 | |
9194 | for (unsigned int i = 0; i < sz; i++) |
9195 | { |
9196 | unsigned prev_carry_over = carry_over; |
9197 | carry_over = ptr[i] & carry_mask; |
9198 | |
9199 | carry_over <<= (unsigned char) BITS_PER_UNIT - amnt; |
9200 | ptr[i] >>= amnt; |
9201 | ptr[i] |= prev_carry_over; |
9202 | } |
9203 | } |
9204 | |
9205 | /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating |
9206 | directly on the VECTOR_CST encoding, in a way that works for variable- |
9207 | length vectors. Return the resulting VECTOR_CST on success or null |
9208 | on failure. */ |
9209 | |
9210 | static tree |
9211 | fold_view_convert_vector_encoding (tree type, tree expr) |
9212 | { |
9213 | tree expr_type = TREE_TYPE (expr); |
9214 | poly_uint64 type_bits, expr_bits; |
9215 | if (!poly_int_tree_p (TYPE_SIZE (type), value: &type_bits) |
9216 | || !poly_int_tree_p (TYPE_SIZE (expr_type), value: &expr_bits)) |
9217 | return NULL_TREE; |
9218 | |
9219 | poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (node: type); |
9220 | poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (node: expr_type); |
9221 | unsigned int type_elt_bits = vector_element_size (type_bits, type_units); |
9222 | unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units); |
9223 | |
9224 | /* We can only preserve the semantics of a stepped pattern if the new |
9225 | vector element is an integer of the same size. */ |
9226 | if (VECTOR_CST_STEPPED_P (expr) |
9227 | && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits)) |
9228 | return NULL_TREE; |
9229 | |
9230 | /* The number of bits needed to encode one element from every pattern |
9231 | of the original vector. */ |
9232 | unsigned int expr_sequence_bits |
9233 | = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits; |
9234 | |
9235 | /* The number of bits needed to encode one element from every pattern |
9236 | of the result. */ |
9237 | unsigned int type_sequence_bits |
9238 | = least_common_multiple (expr_sequence_bits, type_elt_bits); |
9239 | |
9240 | /* Don't try to read more bytes than are available, which can happen |
9241 | for constant-sized vectors if TYPE has larger elements than EXPR_TYPE. |
9242 | The general VIEW_CONVERT handling can cope with that case, so there's |
9243 | no point complicating things here. */ |
9244 | unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr); |
9245 | unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits, |
9246 | BITS_PER_UNIT); |
9247 | unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT; |
9248 | if (known_gt (buffer_bits, expr_bits)) |
9249 | return NULL_TREE; |
9250 | |
9251 | /* Get enough bytes of EXPR to form the new encoding. */ |
9252 | auto_vec<unsigned char, 128> buffer (buffer_bytes); |
9253 | buffer.quick_grow (len: buffer_bytes); |
9254 | if (native_encode_vector_part (expr, ptr: buffer.address (), len: buffer_bytes, off: 0, |
9255 | count: buffer_bits / expr_elt_bits) |
9256 | != (int) buffer_bytes) |
9257 | return NULL_TREE; |
9258 | |
9259 | /* Reencode the bytes as TYPE. */ |
9260 | unsigned int type_npatterns = type_sequence_bits / type_elt_bits; |
9261 | return native_interpret_vector_part (type, bytes: &buffer[0], len: buffer.length (), |
9262 | npatterns: type_npatterns, nelts_per_pattern); |
9263 | } |
9264 | |
9265 | /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type |
9266 | TYPE at compile-time. If we're unable to perform the conversion |
9267 | return NULL_TREE. */ |
9268 | |
9269 | static tree |
9270 | fold_view_convert_expr (tree type, tree expr) |
9271 | { |
9272 | /* We support up to 1024-bit values (for GCN/RISC-V V128QImode). */ |
9273 | unsigned char buffer[128]; |
9274 | int len; |
9275 | |
9276 | /* Check that the host and target are sane. */ |
9277 | if (CHAR_BIT != 8 || BITS_PER_UNIT != 8) |
9278 | return NULL_TREE; |
9279 | |
9280 | if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST) |
9281 | if (tree res = fold_view_convert_vector_encoding (type, expr)) |
9282 | return res; |
9283 | |
9284 | len = native_encode_expr (expr, ptr: buffer, len: sizeof (buffer)); |
9285 | if (len == 0) |
9286 | return NULL_TREE; |
9287 | |
9288 | return native_interpret_expr (type, ptr: buffer, len); |
9289 | } |
9290 | |
9291 | /* Build an expression for the address of T. Folds away INDIRECT_REF |
9292 | to avoid confusing the gimplify process. */ |
9293 | |
9294 | tree |
9295 | build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype) |
9296 | { |
9297 | /* The size of the object is not relevant when talking about its address. */ |
9298 | if (TREE_CODE (t) == WITH_SIZE_EXPR) |
9299 | t = TREE_OPERAND (t, 0); |
9300 | |
9301 | if (INDIRECT_REF_P (t)) |
9302 | { |
9303 | t = TREE_OPERAND (t, 0); |
9304 | |
9305 | if (TREE_TYPE (t) != ptrtype) |
9306 | t = build1_loc (loc, code: NOP_EXPR, type: ptrtype, arg1: t); |
9307 | } |
9308 | else if (TREE_CODE (t) == MEM_REF |
9309 | && integer_zerop (TREE_OPERAND (t, 1))) |
9310 | { |
9311 | t = TREE_OPERAND (t, 0); |
9312 | |
9313 | if (TREE_TYPE (t) != ptrtype) |
9314 | t = fold_convert_loc (loc, type: ptrtype, arg: t); |
9315 | } |
9316 | else if (TREE_CODE (t) == MEM_REF |
9317 | && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST) |
9318 | return fold_binary (POINTER_PLUS_EXPR, ptrtype, |
9319 | TREE_OPERAND (t, 0), |
9320 | convert_to_ptrofftype (TREE_OPERAND (t, 1))); |
9321 | else if (TREE_CODE (t) == VIEW_CONVERT_EXPR) |
9322 | { |
9323 | t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0)); |
9324 | |
9325 | if (TREE_TYPE (t) != ptrtype) |
9326 | t = fold_convert_loc (loc, type: ptrtype, arg: t); |
9327 | } |
9328 | else |
9329 | t = build1_loc (loc, code: ADDR_EXPR, type: ptrtype, arg1: t); |
9330 | |
9331 | return t; |
9332 | } |
9333 | |
9334 | /* Build an expression for the address of T. */ |
9335 | |
9336 | tree |
9337 | build_fold_addr_expr_loc (location_t loc, tree t) |
9338 | { |
9339 | tree ptrtype = build_pointer_type (TREE_TYPE (t)); |
9340 | |
9341 | return build_fold_addr_expr_with_type_loc (loc, t, ptrtype); |
9342 | } |
9343 | |
9344 | /* Fold a unary expression of code CODE and type TYPE with operand |
9345 | OP0. Return the folded expression if folding is successful. |
9346 | Otherwise, return NULL_TREE. */ |
9347 | |
9348 | tree |
9349 | fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0) |
9350 | { |
9351 | tree tem; |
9352 | tree arg0; |
9353 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
9354 | |
9355 | gcc_assert (IS_EXPR_CODE_CLASS (kind) |
9356 | && TREE_CODE_LENGTH (code) == 1); |
9357 | |
9358 | arg0 = op0; |
9359 | if (arg0) |
9360 | { |
9361 | if (CONVERT_EXPR_CODE_P (code) |
9362 | || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR) |
9363 | { |
9364 | /* Don't use STRIP_NOPS, because signedness of argument type |
9365 | matters. */ |
9366 | STRIP_SIGN_NOPS (arg0); |
9367 | } |
9368 | else |
9369 | { |
9370 | /* Strip any conversions that don't change the mode. This |
9371 | is safe for every expression, except for a comparison |
9372 | expression because its signedness is derived from its |
9373 | operands. |
9374 | |
9375 | Note that this is done as an internal manipulation within |
9376 | the constant folder, in order to find the simplest |
9377 | representation of the arguments so that their form can be |
9378 | studied. In any cases, the appropriate type conversions |
9379 | should be put back in the tree that will get out of the |
9380 | constant folder. */ |
9381 | STRIP_NOPS (arg0); |
9382 | } |
9383 | |
9384 | if (CONSTANT_CLASS_P (arg0)) |
9385 | { |
9386 | tree tem = const_unop (code, type, arg0); |
9387 | if (tem) |
9388 | { |
9389 | if (TREE_TYPE (tem) != type) |
9390 | tem = fold_convert_loc (loc, type, arg: tem); |
9391 | return tem; |
9392 | } |
9393 | } |
9394 | } |
9395 | |
9396 | tem = generic_simplify (loc, code, type, op0); |
9397 | if (tem) |
9398 | return tem; |
9399 | |
9400 | if (TREE_CODE_CLASS (code) == tcc_unary) |
9401 | { |
9402 | if (TREE_CODE (arg0) == COMPOUND_EXPR) |
9403 | return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), |
9404 | fold_build1_loc (loc, code, type, |
9405 | fold_convert_loc (loc, TREE_TYPE (op0), |
9406 | TREE_OPERAND (arg0, 1)))); |
9407 | else if (TREE_CODE (arg0) == COND_EXPR) |
9408 | { |
9409 | tree arg01 = TREE_OPERAND (arg0, 1); |
9410 | tree arg02 = TREE_OPERAND (arg0, 2); |
9411 | if (! VOID_TYPE_P (TREE_TYPE (arg01))) |
9412 | arg01 = fold_build1_loc (loc, code, type, |
9413 | fold_convert_loc (loc, |
9414 | TREE_TYPE (op0), arg: arg01)); |
9415 | if (! VOID_TYPE_P (TREE_TYPE (arg02))) |
9416 | arg02 = fold_build1_loc (loc, code, type, |
9417 | fold_convert_loc (loc, |
9418 | TREE_TYPE (op0), arg: arg02)); |
9419 | tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0), |
9420 | arg01, arg02); |
9421 | |
9422 | /* If this was a conversion, and all we did was to move into |
9423 | inside the COND_EXPR, bring it back out. But leave it if |
9424 | it is a conversion from integer to integer and the |
9425 | result precision is no wider than a word since such a |
9426 | conversion is cheap and may be optimized away by combine, |
9427 | while it couldn't if it were outside the COND_EXPR. Then return |
9428 | so we don't get into an infinite recursion loop taking the |
9429 | conversion out and then back in. */ |
9430 | |
9431 | if ((CONVERT_EXPR_CODE_P (code) |
9432 | || code == NON_LVALUE_EXPR) |
9433 | && TREE_CODE (tem) == COND_EXPR |
9434 | && TREE_CODE (TREE_OPERAND (tem, 1)) == code |
9435 | && TREE_CODE (TREE_OPERAND (tem, 2)) == code |
9436 | && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1))) |
9437 | && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2))) |
9438 | && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)) |
9439 | == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0))) |
9440 | && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
9441 | && (INTEGRAL_TYPE_P |
9442 | (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0)))) |
9443 | && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD) |
9444 | || flag_syntax_only)) |
9445 | tem = build1_loc (loc, code, type, |
9446 | arg1: build3 (COND_EXPR, |
9447 | TREE_TYPE (TREE_OPERAND |
9448 | (TREE_OPERAND (tem, 1), 0)), |
9449 | TREE_OPERAND (tem, 0), |
9450 | TREE_OPERAND (TREE_OPERAND (tem, 1), 0), |
9451 | TREE_OPERAND (TREE_OPERAND (tem, 2), |
9452 | 0))); |
9453 | return tem; |
9454 | } |
9455 | } |
9456 | |
9457 | switch (code) |
9458 | { |
9459 | case NON_LVALUE_EXPR: |
9460 | if (!maybe_lvalue_p (x: op0)) |
9461 | return fold_convert_loc (loc, type, arg: op0); |
9462 | return NULL_TREE; |
9463 | |
9464 | CASE_CONVERT: |
9465 | case FLOAT_EXPR: |
9466 | case FIX_TRUNC_EXPR: |
9467 | if (COMPARISON_CLASS_P (op0)) |
9468 | { |
9469 | /* If we have (type) (a CMP b) and type is an integral type, return |
9470 | new expression involving the new type. Canonicalize |
9471 | (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for |
9472 | non-integral type. |
9473 | Do not fold the result as that would not simplify further, also |
9474 | folding again results in recursions. */ |
9475 | if (TREE_CODE (type) == BOOLEAN_TYPE) |
9476 | return build2_loc (loc, TREE_CODE (op0), type, |
9477 | TREE_OPERAND (op0, 0), |
9478 | TREE_OPERAND (op0, 1)); |
9479 | else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type) |
9480 | && TREE_CODE (type) != VECTOR_TYPE) |
9481 | return build3_loc (loc, code: COND_EXPR, type, arg0: op0, |
9482 | arg1: constant_boolean_node (value: true, type), |
9483 | arg2: constant_boolean_node (value: false, type)); |
9484 | } |
9485 | |
9486 | /* Handle (T *)&A.B.C for A being of type T and B and C |
9487 | living at offset zero. This occurs frequently in |
9488 | C++ upcasting and then accessing the base. */ |
9489 | if (TREE_CODE (op0) == ADDR_EXPR |
9490 | && POINTER_TYPE_P (type) |
9491 | && handled_component_p (TREE_OPERAND (op0, 0))) |
9492 | { |
9493 | poly_int64 bitsize, bitpos; |
9494 | tree offset; |
9495 | machine_mode mode; |
9496 | int unsignedp, reversep, volatilep; |
9497 | tree base |
9498 | = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos, |
9499 | &offset, &mode, &unsignedp, &reversep, |
9500 | &volatilep); |
9501 | /* If the reference was to a (constant) zero offset, we can use |
9502 | the address of the base if it has the same base type |
9503 | as the result type and the pointer type is unqualified. */ |
9504 | if (!offset |
9505 | && known_eq (bitpos, 0) |
9506 | && (TYPE_MAIN_VARIANT (TREE_TYPE (type)) |
9507 | == TYPE_MAIN_VARIANT (TREE_TYPE (base))) |
9508 | && TYPE_QUALS (type) == TYPE_UNQUALIFIED) |
9509 | return fold_convert_loc (loc, type, |
9510 | arg: build_fold_addr_expr_loc (loc, t: base)); |
9511 | } |
9512 | |
9513 | if (TREE_CODE (op0) == MODIFY_EXPR |
9514 | && TREE_CONSTANT (TREE_OPERAND (op0, 1)) |
9515 | /* Detect assigning a bitfield. */ |
9516 | && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF |
9517 | && DECL_BIT_FIELD |
9518 | (TREE_OPERAND (TREE_OPERAND (op0, 0), 1)))) |
9519 | { |
9520 | /* Don't leave an assignment inside a conversion |
9521 | unless assigning a bitfield. */ |
9522 | tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1)); |
9523 | /* First do the assignment, then return converted constant. */ |
9524 | tem = build2_loc (loc, code: COMPOUND_EXPR, TREE_TYPE (tem), arg0: op0, arg1: tem); |
9525 | suppress_warning (tem /* What warning? */); |
9526 | TREE_USED (tem) = 1; |
9527 | return tem; |
9528 | } |
9529 | |
9530 | /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer |
9531 | constants (if x has signed type, the sign bit cannot be set |
9532 | in c). This folds extension into the BIT_AND_EXPR. |
9533 | ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they |
9534 | very likely don't have maximal range for their precision and this |
9535 | transformation effectively doesn't preserve non-maximal ranges. */ |
9536 | if (TREE_CODE (type) == INTEGER_TYPE |
9537 | && TREE_CODE (op0) == BIT_AND_EXPR |
9538 | && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST) |
9539 | { |
9540 | tree and_expr = op0; |
9541 | tree and0 = TREE_OPERAND (and_expr, 0); |
9542 | tree and1 = TREE_OPERAND (and_expr, 1); |
9543 | int change = 0; |
9544 | |
9545 | if (TYPE_UNSIGNED (TREE_TYPE (and_expr)) |
9546 | || (TYPE_PRECISION (type) |
9547 | <= TYPE_PRECISION (TREE_TYPE (and_expr)))) |
9548 | change = 1; |
9549 | else if (TYPE_PRECISION (TREE_TYPE (and1)) |
9550 | <= HOST_BITS_PER_WIDE_INT |
9551 | && tree_fits_uhwi_p (and1)) |
9552 | { |
9553 | unsigned HOST_WIDE_INT cst; |
9554 | |
9555 | cst = tree_to_uhwi (and1); |
9556 | cst &= HOST_WIDE_INT_M1U |
9557 | << (TYPE_PRECISION (TREE_TYPE (and1)) - 1); |
9558 | change = (cst == 0); |
9559 | if (change |
9560 | && !flag_syntax_only |
9561 | && (load_extend_op (TYPE_MODE (TREE_TYPE (and0))) |
9562 | == ZERO_EXTEND)) |
9563 | { |
9564 | tree uns = unsigned_type_for (TREE_TYPE (and0)); |
9565 | and0 = fold_convert_loc (loc, type: uns, arg: and0); |
9566 | and1 = fold_convert_loc (loc, type: uns, arg: and1); |
9567 | } |
9568 | } |
9569 | if (change) |
9570 | { |
9571 | tree and1_type = TREE_TYPE (and1); |
9572 | unsigned prec = MAX (TYPE_PRECISION (and1_type), |
9573 | TYPE_PRECISION (type)); |
9574 | tem = force_fit_type (type, |
9575 | wide_int::from (x: wi::to_wide (t: and1), precision: prec, |
9576 | TYPE_SIGN (and1_type)), |
9577 | 0, TREE_OVERFLOW (and1)); |
9578 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
9579 | fold_convert_loc (loc, type, arg: and0), tem); |
9580 | } |
9581 | } |
9582 | |
9583 | /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new |
9584 | cast (T1)X will fold away. We assume that this happens when X itself |
9585 | is a cast. */ |
9586 | if (POINTER_TYPE_P (type) |
9587 | && TREE_CODE (arg0) == POINTER_PLUS_EXPR |
9588 | && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0))) |
9589 | { |
9590 | tree arg00 = TREE_OPERAND (arg0, 0); |
9591 | tree arg01 = TREE_OPERAND (arg0, 1); |
9592 | |
9593 | /* If -fsanitize=alignment, avoid this optimization in GENERIC |
9594 | when the pointed type needs higher alignment than |
9595 | the p+ first operand's pointed type. */ |
9596 | if (!in_gimple_form |
9597 | && sanitize_flags_p (flag: SANITIZE_ALIGNMENT) |
9598 | && (min_align_of_type (TREE_TYPE (type)) |
9599 | > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00))))) |
9600 | return NULL_TREE; |
9601 | |
9602 | /* Similarly, avoid this optimization in GENERIC for -fsanitize=null |
9603 | when type is a reference type and arg00's type is not, |
9604 | because arg00 could be validly nullptr and if arg01 doesn't return, |
9605 | we don't want false positive binding of reference to nullptr. */ |
9606 | if (TREE_CODE (type) == REFERENCE_TYPE |
9607 | && !in_gimple_form |
9608 | && sanitize_flags_p (flag: SANITIZE_NULL) |
9609 | && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE) |
9610 | return NULL_TREE; |
9611 | |
9612 | arg00 = fold_convert_loc (loc, type, arg: arg00); |
9613 | return fold_build_pointer_plus_loc (loc, ptr: arg00, off: arg01); |
9614 | } |
9615 | |
9616 | /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types |
9617 | of the same precision, and X is an integer type not narrower than |
9618 | types T1 or T2, i.e. the cast (T2)X isn't an extension. */ |
9619 | if (INTEGRAL_TYPE_P (type) |
9620 | && TREE_CODE (op0) == BIT_NOT_EXPR |
9621 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) |
9622 | && CONVERT_EXPR_P (TREE_OPERAND (op0, 0)) |
9623 | && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))) |
9624 | { |
9625 | tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0); |
9626 | if (INTEGRAL_TYPE_P (TREE_TYPE (tem)) |
9627 | && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem))) |
9628 | return fold_build1_loc (loc, BIT_NOT_EXPR, type, |
9629 | fold_convert_loc (loc, type, arg: tem)); |
9630 | } |
9631 | |
9632 | /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the |
9633 | type of X and Y (integer types only). */ |
9634 | if (INTEGRAL_TYPE_P (type) |
9635 | && TREE_CODE (op0) == MULT_EXPR |
9636 | && INTEGRAL_TYPE_P (TREE_TYPE (op0)) |
9637 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)) |
9638 | && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)) |
9639 | || !sanitize_flags_p (flag: SANITIZE_SI_OVERFLOW))) |
9640 | { |
9641 | /* Be careful not to introduce new overflows. */ |
9642 | tree mult_type; |
9643 | if (TYPE_OVERFLOW_WRAPS (type)) |
9644 | mult_type = type; |
9645 | else |
9646 | mult_type = unsigned_type_for (type); |
9647 | |
9648 | if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0))) |
9649 | { |
9650 | tem = fold_build2_loc (loc, MULT_EXPR, mult_type, |
9651 | fold_convert_loc (loc, type: mult_type, |
9652 | TREE_OPERAND (op0, 0)), |
9653 | fold_convert_loc (loc, type: mult_type, |
9654 | TREE_OPERAND (op0, 1))); |
9655 | return fold_convert_loc (loc, type, arg: tem); |
9656 | } |
9657 | } |
9658 | |
9659 | return NULL_TREE; |
9660 | |
9661 | case VIEW_CONVERT_EXPR: |
9662 | if (TREE_CODE (op0) == MEM_REF) |
9663 | { |
9664 | if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type)) |
9665 | type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0))); |
9666 | tem = fold_build2_loc (loc, MEM_REF, type, |
9667 | TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1)); |
9668 | REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0); |
9669 | return tem; |
9670 | } |
9671 | |
9672 | return NULL_TREE; |
9673 | |
9674 | case NEGATE_EXPR: |
9675 | tem = fold_negate_expr (loc, t: arg0); |
9676 | if (tem) |
9677 | return fold_convert_loc (loc, type, arg: tem); |
9678 | return NULL_TREE; |
9679 | |
9680 | case ABS_EXPR: |
9681 | /* Convert fabs((double)float) into (double)fabsf(float). */ |
9682 | if (TREE_CODE (arg0) == NOP_EXPR |
9683 | && TREE_CODE (type) == REAL_TYPE) |
9684 | { |
9685 | tree targ0 = strip_float_extensions (arg0); |
9686 | if (targ0 != arg0) |
9687 | return fold_convert_loc (loc, type, |
9688 | arg: fold_build1_loc (loc, ABS_EXPR, |
9689 | TREE_TYPE (targ0), |
9690 | targ0)); |
9691 | } |
9692 | return NULL_TREE; |
9693 | |
9694 | case BIT_NOT_EXPR: |
9695 | /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ |
9696 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
9697 | && (tem = fold_unary_loc (loc, code: BIT_NOT_EXPR, type, |
9698 | op0: fold_convert_loc (loc, type, |
9699 | TREE_OPERAND (arg0, 0))))) |
9700 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem, |
9701 | fold_convert_loc (loc, type, |
9702 | TREE_OPERAND (arg0, 1))); |
9703 | else if (TREE_CODE (arg0) == BIT_XOR_EXPR |
9704 | && (tem = fold_unary_loc (loc, code: BIT_NOT_EXPR, type, |
9705 | op0: fold_convert_loc (loc, type, |
9706 | TREE_OPERAND (arg0, 1))))) |
9707 | return fold_build2_loc (loc, BIT_XOR_EXPR, type, |
9708 | fold_convert_loc (loc, type, |
9709 | TREE_OPERAND (arg0, 0)), tem); |
9710 | |
9711 | return NULL_TREE; |
9712 | |
9713 | case TRUTH_NOT_EXPR: |
9714 | /* Note that the operand of this must be an int |
9715 | and its values must be 0 or 1. |
9716 | ("true" is a fixed value perhaps depending on the language, |
9717 | but we don't handle values other than 1 correctly yet.) */ |
9718 | tem = fold_truth_not_expr (loc, arg: arg0); |
9719 | if (!tem) |
9720 | return NULL_TREE; |
9721 | return fold_convert_loc (loc, type, arg: tem); |
9722 | |
9723 | case INDIRECT_REF: |
9724 | /* Fold *&X to X if X is an lvalue. */ |
9725 | if (TREE_CODE (op0) == ADDR_EXPR) |
9726 | { |
9727 | tree op00 = TREE_OPERAND (op0, 0); |
9728 | if ((VAR_P (op00) |
9729 | || TREE_CODE (op00) == PARM_DECL |
9730 | || TREE_CODE (op00) == RESULT_DECL) |
9731 | && !TREE_READONLY (op00)) |
9732 | return op00; |
9733 | } |
9734 | return NULL_TREE; |
9735 | |
9736 | default: |
9737 | return NULL_TREE; |
9738 | } /* switch (code) */ |
9739 | } |
9740 | |
9741 | |
9742 | /* If the operation was a conversion do _not_ mark a resulting constant |
9743 | with TREE_OVERFLOW if the original constant was not. These conversions |
9744 | have implementation defined behavior and retaining the TREE_OVERFLOW |
9745 | flag here would confuse later passes such as VRP. */ |
9746 | tree |
9747 | fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code, |
9748 | tree type, tree op0) |
9749 | { |
9750 | tree res = fold_unary_loc (loc, code, type, op0); |
9751 | if (res |
9752 | && TREE_CODE (res) == INTEGER_CST |
9753 | && TREE_CODE (op0) == INTEGER_CST |
9754 | && CONVERT_EXPR_CODE_P (code)) |
9755 | TREE_OVERFLOW (res) = TREE_OVERFLOW (op0); |
9756 | |
9757 | return res; |
9758 | } |
9759 | |
9760 | /* Fold a binary bitwise/truth expression of code CODE and type TYPE with |
9761 | operands OP0 and OP1. LOC is the location of the resulting expression. |
9762 | ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1. |
9763 | Return the folded expression if folding is successful. Otherwise, |
9764 | return NULL_TREE. */ |
9765 | static tree |
9766 | fold_truth_andor (location_t loc, enum tree_code code, tree type, |
9767 | tree arg0, tree arg1, tree op0, tree op1) |
9768 | { |
9769 | tree tem; |
9770 | |
9771 | /* We only do these simplifications if we are optimizing. */ |
9772 | if (!optimize) |
9773 | return NULL_TREE; |
9774 | |
9775 | /* Check for things like (A || B) && (A || C). We can convert this |
9776 | to A || (B && C). Note that either operator can be any of the four |
9777 | truth and/or operations and the transformation will still be |
9778 | valid. Also note that we only care about order for the |
9779 | ANDIF and ORIF operators. If B contains side effects, this |
9780 | might change the truth-value of A. */ |
9781 | if (TREE_CODE (arg0) == TREE_CODE (arg1) |
9782 | && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR |
9783 | || TREE_CODE (arg0) == TRUTH_ORIF_EXPR |
9784 | || TREE_CODE (arg0) == TRUTH_AND_EXPR |
9785 | || TREE_CODE (arg0) == TRUTH_OR_EXPR) |
9786 | && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1))) |
9787 | { |
9788 | tree a00 = TREE_OPERAND (arg0, 0); |
9789 | tree a01 = TREE_OPERAND (arg0, 1); |
9790 | tree a10 = TREE_OPERAND (arg1, 0); |
9791 | tree a11 = TREE_OPERAND (arg1, 1); |
9792 | bool commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR |
9793 | || TREE_CODE (arg0) == TRUTH_AND_EXPR) |
9794 | && (code == TRUTH_AND_EXPR |
9795 | || code == TRUTH_OR_EXPR)); |
9796 | |
9797 | if (operand_equal_p (arg0: a00, arg1: a10, flags: 0)) |
9798 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, |
9799 | fold_build2_loc (loc, code, type, a01, a11)); |
9800 | else if (commutative && operand_equal_p (arg0: a00, arg1: a11, flags: 0)) |
9801 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a00, |
9802 | fold_build2_loc (loc, code, type, a01, a10)); |
9803 | else if (commutative && operand_equal_p (arg0: a01, arg1: a10, flags: 0)) |
9804 | return fold_build2_loc (loc, TREE_CODE (arg0), type, a01, |
9805 | fold_build2_loc (loc, code, type, a00, a11)); |
9806 | |
9807 | /* This case if tricky because we must either have commutative |
9808 | operators or else A10 must not have side-effects. */ |
9809 | |
9810 | else if ((commutative || ! TREE_SIDE_EFFECTS (a10)) |
9811 | && operand_equal_p (arg0: a01, arg1: a11, flags: 0)) |
9812 | return fold_build2_loc (loc, TREE_CODE (arg0), type, |
9813 | fold_build2_loc (loc, code, type, a00, a10), |
9814 | a01); |
9815 | } |
9816 | |
9817 | /* See if we can build a range comparison. */ |
9818 | if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0) |
9819 | return tem; |
9820 | |
9821 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR) |
9822 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR)) |
9823 | { |
9824 | tem = merge_truthop_with_opposite_arm (loc, op: arg0, cmpop: arg1, rhs_only: true); |
9825 | if (tem) |
9826 | return fold_build2_loc (loc, code, type, tem, arg1); |
9827 | } |
9828 | |
9829 | if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR) |
9830 | || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR)) |
9831 | { |
9832 | tem = merge_truthop_with_opposite_arm (loc, op: arg1, cmpop: arg0, rhs_only: false); |
9833 | if (tem) |
9834 | return fold_build2_loc (loc, code, type, arg0, tem); |
9835 | } |
9836 | |
9837 | /* Check for the possibility of merging component references. If our |
9838 | lhs is another similar operation, try to merge its rhs with our |
9839 | rhs. Then try to merge our lhs and rhs. */ |
9840 | if (TREE_CODE (arg0) == code |
9841 | && (tem = fold_truth_andor_1 (loc, code, truth_type: type, |
9842 | TREE_OPERAND (arg0, 1), rhs: arg1)) != 0) |
9843 | return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem); |
9844 | |
9845 | if ((tem = fold_truth_andor_1 (loc, code, truth_type: type, lhs: arg0, rhs: arg1)) != 0) |
9846 | return tem; |
9847 | |
9848 | bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT; |
9849 | if (param_logical_op_non_short_circuit != -1) |
9850 | logical_op_non_short_circuit |
9851 | = param_logical_op_non_short_circuit; |
9852 | if (logical_op_non_short_circuit |
9853 | && !sanitize_coverage_p () |
9854 | && (code == TRUTH_AND_EXPR |
9855 | || code == TRUTH_ANDIF_EXPR |
9856 | || code == TRUTH_OR_EXPR |
9857 | || code == TRUTH_ORIF_EXPR)) |
9858 | { |
9859 | enum tree_code ncode, icode; |
9860 | |
9861 | ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR) |
9862 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR; |
9863 | icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR; |
9864 | |
9865 | /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)), |
9866 | or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C)) |
9867 | We don't want to pack more than two leafs to a non-IF AND/OR |
9868 | expression. |
9869 | If tree-code of left-hand operand isn't an AND/OR-IF code and not |
9870 | equal to IF-CODE, then we don't want to add right-hand operand. |
9871 | If the inner right-hand side of left-hand operand has |
9872 | side-effects, or isn't simple, then we can't add to it, |
9873 | as otherwise we might destroy if-sequence. */ |
9874 | if (TREE_CODE (arg0) == icode |
9875 | && simple_condition_p (exp: arg1) |
9876 | /* Needed for sequence points to handle trappings, and |
9877 | side-effects. */ |
9878 | && simple_condition_p (TREE_OPERAND (arg0, 1))) |
9879 | { |
9880 | tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1), |
9881 | arg1); |
9882 | return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0), |
9883 | tem); |
9884 | } |
9885 | /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C), |
9886 | or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */ |
9887 | else if (TREE_CODE (arg1) == icode |
9888 | && simple_condition_p (exp: arg0) |
9889 | /* Needed for sequence points to handle trappings, and |
9890 | side-effects. */ |
9891 | && simple_condition_p (TREE_OPERAND (arg1, 0))) |
9892 | { |
9893 | tem = fold_build2_loc (loc, ncode, type, |
9894 | arg0, TREE_OPERAND (arg1, 0)); |
9895 | return fold_build2_loc (loc, icode, type, tem, |
9896 | TREE_OPERAND (arg1, 1)); |
9897 | } |
9898 | /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B) |
9899 | into (A OR B). |
9900 | For sequence point consistancy, we need to check for trapping, |
9901 | and side-effects. */ |
9902 | else if (code == icode && simple_condition_p (exp: arg0) |
9903 | && simple_condition_p (exp: arg1)) |
9904 | return fold_build2_loc (loc, ncode, type, arg0, arg1); |
9905 | } |
9906 | |
9907 | return NULL_TREE; |
9908 | } |
9909 | |
9910 | /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1 |
9911 | by changing CODE to reduce the magnitude of constants involved in |
9912 | ARG0 of the comparison. |
9913 | Returns a canonicalized comparison tree if a simplification was |
9914 | possible, otherwise returns NULL_TREE. |
9915 | Set *STRICT_OVERFLOW_P to true if the canonicalization is only |
9916 | valid if signed overflow is undefined. */ |
9917 | |
9918 | static tree |
9919 | maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type, |
9920 | tree arg0, tree arg1, |
9921 | bool *strict_overflow_p) |
9922 | { |
9923 | enum tree_code code0 = TREE_CODE (arg0); |
9924 | tree t, cst0 = NULL_TREE; |
9925 | int sgn0; |
9926 | |
9927 | /* Match A +- CST code arg1. We can change this only if overflow |
9928 | is undefined. */ |
9929 | if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
9930 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))) |
9931 | /* In principle pointers also have undefined overflow behavior, |
9932 | but that causes problems elsewhere. */ |
9933 | && !POINTER_TYPE_P (TREE_TYPE (arg0)) |
9934 | && (code0 == MINUS_EXPR |
9935 | || code0 == PLUS_EXPR) |
9936 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)) |
9937 | return NULL_TREE; |
9938 | |
9939 | /* Identify the constant in arg0 and its sign. */ |
9940 | cst0 = TREE_OPERAND (arg0, 1); |
9941 | sgn0 = tree_int_cst_sgn (cst0); |
9942 | |
9943 | /* Overflowed constants and zero will cause problems. */ |
9944 | if (integer_zerop (cst0) |
9945 | || TREE_OVERFLOW (cst0)) |
9946 | return NULL_TREE; |
9947 | |
9948 | /* See if we can reduce the magnitude of the constant in |
9949 | arg0 by changing the comparison code. */ |
9950 | /* A - CST < arg1 -> A - CST-1 <= arg1. */ |
9951 | if (code == LT_EXPR |
9952 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) |
9953 | code = LE_EXPR; |
9954 | /* A + CST > arg1 -> A + CST-1 >= arg1. */ |
9955 | else if (code == GT_EXPR |
9956 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) |
9957 | code = GE_EXPR; |
9958 | /* A + CST <= arg1 -> A + CST-1 < arg1. */ |
9959 | else if (code == LE_EXPR |
9960 | && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR)) |
9961 | code = LT_EXPR; |
9962 | /* A - CST >= arg1 -> A - CST-1 > arg1. */ |
9963 | else if (code == GE_EXPR |
9964 | && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR)) |
9965 | code = GT_EXPR; |
9966 | else |
9967 | return NULL_TREE; |
9968 | *strict_overflow_p = true; |
9969 | |
9970 | /* Now build the constant reduced in magnitude. But not if that |
9971 | would produce one outside of its types range. */ |
9972 | if (INTEGRAL_TYPE_P (TREE_TYPE (cst0)) |
9973 | && ((sgn0 == 1 |
9974 | && TYPE_MIN_VALUE (TREE_TYPE (cst0)) |
9975 | && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0)))) |
9976 | || (sgn0 == -1 |
9977 | && TYPE_MAX_VALUE (TREE_TYPE (cst0)) |
9978 | && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0)))))) |
9979 | return NULL_TREE; |
9980 | |
9981 | t = int_const_binop (code: sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR, |
9982 | arg1: cst0, arg2: build_int_cst (TREE_TYPE (cst0), 1)); |
9983 | t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t); |
9984 | t = fold_convert (TREE_TYPE (arg1), t); |
9985 | |
9986 | return fold_build2_loc (loc, code, type, t, arg1); |
9987 | } |
9988 | |
9989 | /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined |
9990 | overflow further. Try to decrease the magnitude of constants involved |
9991 | by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa |
9992 | and put sole constants at the second argument position. |
9993 | Returns the canonicalized tree if changed, otherwise NULL_TREE. */ |
9994 | |
9995 | static tree |
9996 | maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type, |
9997 | tree arg0, tree arg1) |
9998 | { |
9999 | tree t; |
10000 | bool strict_overflow_p; |
10001 | const char * const warnmsg = G_("assuming signed overflow does not occur " |
10002 | "when reducing constant in comparison" ); |
10003 | |
10004 | /* Try canonicalization by simplifying arg0. */ |
10005 | strict_overflow_p = false; |
10006 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1, |
10007 | strict_overflow_p: &strict_overflow_p); |
10008 | if (t) |
10009 | { |
10010 | if (strict_overflow_p) |
10011 | fold_overflow_warning (gmsgid: warnmsg, wc: WARN_STRICT_OVERFLOW_MAGNITUDE); |
10012 | return t; |
10013 | } |
10014 | |
10015 | /* Try canonicalization by simplifying arg1 using the swapped |
10016 | comparison. */ |
10017 | code = swap_tree_comparison (code); |
10018 | strict_overflow_p = false; |
10019 | t = maybe_canonicalize_comparison_1 (loc, code, type, arg0: arg1, arg1: arg0, |
10020 | strict_overflow_p: &strict_overflow_p); |
10021 | if (t && strict_overflow_p) |
10022 | fold_overflow_warning (gmsgid: warnmsg, wc: WARN_STRICT_OVERFLOW_MAGNITUDE); |
10023 | return t; |
10024 | } |
10025 | |
10026 | /* Return whether BASE + OFFSET + BITPOS may wrap around the address |
10027 | space. This is used to avoid issuing overflow warnings for |
10028 | expressions like &p->x which cannot wrap. */ |
10029 | |
10030 | static bool |
10031 | pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos) |
10032 | { |
10033 | if (!POINTER_TYPE_P (TREE_TYPE (base))) |
10034 | return true; |
10035 | |
10036 | if (maybe_lt (a: bitpos, b: 0)) |
10037 | return true; |
10038 | |
10039 | poly_wide_int wi_offset; |
10040 | int precision = TYPE_PRECISION (TREE_TYPE (base)); |
10041 | if (offset == NULL_TREE) |
10042 | wi_offset = wi::zero (precision); |
10043 | else if (!poly_int_tree_p (t: offset) || TREE_OVERFLOW (offset)) |
10044 | return true; |
10045 | else |
10046 | wi_offset = wi::to_poly_wide (t: offset); |
10047 | |
10048 | wi::overflow_type overflow; |
10049 | poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos), |
10050 | precision); |
10051 | poly_wide_int total = wi::add (a: wi_offset, b: units, sgn: UNSIGNED, overflow: &overflow); |
10052 | if (overflow) |
10053 | return true; |
10054 | |
10055 | poly_uint64 total_hwi, size; |
10056 | if (!total.to_uhwi (r: &total_hwi) |
10057 | || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))), |
10058 | value: &size) |
10059 | || known_eq (size, 0U)) |
10060 | return true; |
10061 | |
10062 | if (known_le (total_hwi, size)) |
10063 | return false; |
10064 | |
10065 | /* We can do slightly better for SIZE if we have an ADDR_EXPR of an |
10066 | array. */ |
10067 | if (TREE_CODE (base) == ADDR_EXPR |
10068 | && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))), |
10069 | value: &size) |
10070 | && maybe_ne (a: size, b: 0U) |
10071 | && known_le (total_hwi, size)) |
10072 | return false; |
10073 | |
10074 | return true; |
10075 | } |
10076 | |
10077 | /* Return a positive integer when the symbol DECL is known to have |
10078 | a nonzero address, zero when it's known not to (e.g., it's a weak |
10079 | symbol), and a negative integer when the symbol is not yet in the |
10080 | symbol table and so whether or not its address is zero is unknown. |
10081 | For function local objects always return positive integer. */ |
10082 | static int |
10083 | maybe_nonzero_address (tree decl) |
10084 | { |
10085 | /* Normally, don't do anything for variables and functions before symtab is |
10086 | built; it is quite possible that DECL will be declared weak later. |
10087 | But if folding_initializer, we need a constant answer now, so create |
10088 | the symtab entry and prevent later weak declaration. */ |
10089 | if (DECL_P (decl) && decl_in_symtab_p (decl)) |
10090 | if (struct symtab_node *symbol |
10091 | = (folding_initializer |
10092 | ? symtab_node::get_create (node: decl) |
10093 | : symtab_node::get (decl))) |
10094 | return symbol->nonzero_address (); |
10095 | |
10096 | /* Function local objects are never NULL. */ |
10097 | if (DECL_P (decl) |
10098 | && (DECL_CONTEXT (decl) |
10099 | && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL |
10100 | && auto_var_in_fn_p (decl, DECL_CONTEXT (decl)))) |
10101 | return 1; |
10102 | |
10103 | return -1; |
10104 | } |
10105 | |
10106 | /* Subroutine of fold_binary. This routine performs all of the |
10107 | transformations that are common to the equality/inequality |
10108 | operators (EQ_EXPR and NE_EXPR) and the ordering operators |
10109 | (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than |
10110 | fold_binary should call fold_binary. Fold a comparison with |
10111 | tree code CODE and type TYPE with operands OP0 and OP1. Return |
10112 | the folded comparison or NULL_TREE. */ |
10113 | |
10114 | static tree |
10115 | fold_comparison (location_t loc, enum tree_code code, tree type, |
10116 | tree op0, tree op1) |
10117 | { |
10118 | const bool equality_code = (code == EQ_EXPR || code == NE_EXPR); |
10119 | tree arg0, arg1, tem; |
10120 | |
10121 | arg0 = op0; |
10122 | arg1 = op1; |
10123 | |
10124 | STRIP_SIGN_NOPS (arg0); |
10125 | STRIP_SIGN_NOPS (arg1); |
10126 | |
10127 | /* For comparisons of pointers we can decompose it to a compile time |
10128 | comparison of the base objects and the offsets into the object. |
10129 | This requires at least one operand being an ADDR_EXPR or a |
10130 | POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */ |
10131 | if (POINTER_TYPE_P (TREE_TYPE (arg0)) |
10132 | && (TREE_CODE (arg0) == ADDR_EXPR |
10133 | || TREE_CODE (arg1) == ADDR_EXPR |
10134 | || TREE_CODE (arg0) == POINTER_PLUS_EXPR |
10135 | || TREE_CODE (arg1) == POINTER_PLUS_EXPR)) |
10136 | { |
10137 | tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE; |
10138 | poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0; |
10139 | machine_mode mode; |
10140 | int volatilep, reversep, unsignedp; |
10141 | bool indirect_base0 = false, indirect_base1 = false; |
10142 | |
10143 | /* Get base and offset for the access. Strip ADDR_EXPR for |
10144 | get_inner_reference, but put it back by stripping INDIRECT_REF |
10145 | off the base object if possible. indirect_baseN will be true |
10146 | if baseN is not an address but refers to the object itself. */ |
10147 | base0 = arg0; |
10148 | if (TREE_CODE (arg0) == ADDR_EXPR) |
10149 | { |
10150 | base0 |
10151 | = get_inner_reference (TREE_OPERAND (arg0, 0), |
10152 | &bitsize, &bitpos0, &offset0, &mode, |
10153 | &unsignedp, &reversep, &volatilep); |
10154 | if (INDIRECT_REF_P (base0)) |
10155 | base0 = TREE_OPERAND (base0, 0); |
10156 | else |
10157 | indirect_base0 = true; |
10158 | } |
10159 | else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR) |
10160 | { |
10161 | base0 = TREE_OPERAND (arg0, 0); |
10162 | STRIP_SIGN_NOPS (base0); |
10163 | if (TREE_CODE (base0) == ADDR_EXPR) |
10164 | { |
10165 | base0 |
10166 | = get_inner_reference (TREE_OPERAND (base0, 0), |
10167 | &bitsize, &bitpos0, &offset0, &mode, |
10168 | &unsignedp, &reversep, &volatilep); |
10169 | if (INDIRECT_REF_P (base0)) |
10170 | base0 = TREE_OPERAND (base0, 0); |
10171 | else |
10172 | indirect_base0 = true; |
10173 | } |
10174 | if (offset0 == NULL_TREE || integer_zerop (offset0)) |
10175 | offset0 = TREE_OPERAND (arg0, 1); |
10176 | else |
10177 | offset0 = size_binop (PLUS_EXPR, offset0, |
10178 | TREE_OPERAND (arg0, 1)); |
10179 | if (poly_int_tree_p (t: offset0)) |
10180 | { |
10181 | poly_offset_int tem = wi::sext (a: wi::to_poly_offset (t: offset0), |
10182 | TYPE_PRECISION (sizetype)); |
10183 | tem <<= LOG2_BITS_PER_UNIT; |
10184 | tem += bitpos0; |
10185 | if (tem.to_shwi (r: &bitpos0)) |
10186 | offset0 = NULL_TREE; |
10187 | } |
10188 | } |
10189 | |
10190 | base1 = arg1; |
10191 | if (TREE_CODE (arg1) == ADDR_EXPR) |
10192 | { |
10193 | base1 |
10194 | = get_inner_reference (TREE_OPERAND (arg1, 0), |
10195 | &bitsize, &bitpos1, &offset1, &mode, |
10196 | &unsignedp, &reversep, &volatilep); |
10197 | if (INDIRECT_REF_P (base1)) |
10198 | base1 = TREE_OPERAND (base1, 0); |
10199 | else |
10200 | indirect_base1 = true; |
10201 | } |
10202 | else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR) |
10203 | { |
10204 | base1 = TREE_OPERAND (arg1, 0); |
10205 | STRIP_SIGN_NOPS (base1); |
10206 | if (TREE_CODE (base1) == ADDR_EXPR) |
10207 | { |
10208 | base1 |
10209 | = get_inner_reference (TREE_OPERAND (base1, 0), |
10210 | &bitsize, &bitpos1, &offset1, &mode, |
10211 | &unsignedp, &reversep, &volatilep); |
10212 | if (INDIRECT_REF_P (base1)) |
10213 | base1 = TREE_OPERAND (base1, 0); |
10214 | else |
10215 | indirect_base1 = true; |
10216 | } |
10217 | if (offset1 == NULL_TREE || integer_zerop (offset1)) |
10218 | offset1 = TREE_OPERAND (arg1, 1); |
10219 | else |
10220 | offset1 = size_binop (PLUS_EXPR, offset1, |
10221 | TREE_OPERAND (arg1, 1)); |
10222 | if (poly_int_tree_p (t: offset1)) |
10223 | { |
10224 | poly_offset_int tem = wi::sext (a: wi::to_poly_offset (t: offset1), |
10225 | TYPE_PRECISION (sizetype)); |
10226 | tem <<= LOG2_BITS_PER_UNIT; |
10227 | tem += bitpos1; |
10228 | if (tem.to_shwi (r: &bitpos1)) |
10229 | offset1 = NULL_TREE; |
10230 | } |
10231 | } |
10232 | |
10233 | /* If we have equivalent bases we might be able to simplify. */ |
10234 | if (indirect_base0 == indirect_base1 |
10235 | && operand_equal_p (arg0: base0, arg1: base1, |
10236 | flags: indirect_base0 ? OEP_ADDRESS_OF : 0)) |
10237 | { |
10238 | /* We can fold this expression to a constant if the non-constant |
10239 | offset parts are equal. */ |
10240 | if ((offset0 == offset1 |
10241 | || (offset0 && offset1 |
10242 | && operand_equal_p (arg0: offset0, arg1: offset1, flags: 0))) |
10243 | && (equality_code |
10244 | || (indirect_base0 |
10245 | && (DECL_P (base0) || CONSTANT_CLASS_P (base0))) |
10246 | || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))) |
10247 | { |
10248 | if (!equality_code |
10249 | && maybe_ne (a: bitpos0, b: bitpos1) |
10250 | && (pointer_may_wrap_p (base: base0, offset: offset0, bitpos: bitpos0) |
10251 | || pointer_may_wrap_p (base: base1, offset: offset1, bitpos: bitpos1))) |
10252 | fold_overflow_warning (gmsgid: ("assuming pointer wraparound does not " |
10253 | "occur when comparing P +- C1 with " |
10254 | "P +- C2" ), |
10255 | wc: WARN_STRICT_OVERFLOW_CONDITIONAL); |
10256 | |
10257 | switch (code) |
10258 | { |
10259 | case EQ_EXPR: |
10260 | if (known_eq (bitpos0, bitpos1)) |
10261 | return constant_boolean_node (value: true, type); |
10262 | if (known_ne (bitpos0, bitpos1)) |
10263 | return constant_boolean_node (value: false, type); |
10264 | break; |
10265 | case NE_EXPR: |
10266 | if (known_ne (bitpos0, bitpos1)) |
10267 | return constant_boolean_node (value: true, type); |
10268 | if (known_eq (bitpos0, bitpos1)) |
10269 | return constant_boolean_node (value: false, type); |
10270 | break; |
10271 | case LT_EXPR: |
10272 | if (known_lt (bitpos0, bitpos1)) |
10273 | return constant_boolean_node (value: true, type); |
10274 | if (known_ge (bitpos0, bitpos1)) |
10275 | return constant_boolean_node (value: false, type); |
10276 | break; |
10277 | case LE_EXPR: |
10278 | if (known_le (bitpos0, bitpos1)) |
10279 | return constant_boolean_node (value: true, type); |
10280 | if (known_gt (bitpos0, bitpos1)) |
10281 | return constant_boolean_node (value: false, type); |
10282 | break; |
10283 | case GE_EXPR: |
10284 | if (known_ge (bitpos0, bitpos1)) |
10285 | return constant_boolean_node (value: true, type); |
10286 | if (known_lt (bitpos0, bitpos1)) |
10287 | return constant_boolean_node (value: false, type); |
10288 | break; |
10289 | case GT_EXPR: |
10290 | if (known_gt (bitpos0, bitpos1)) |
10291 | return constant_boolean_node (value: true, type); |
10292 | if (known_le (bitpos0, bitpos1)) |
10293 | return constant_boolean_node (value: false, type); |
10294 | break; |
10295 | default:; |
10296 | } |
10297 | } |
10298 | /* We can simplify the comparison to a comparison of the variable |
10299 | offset parts if the constant offset parts are equal. |
10300 | Be careful to use signed sizetype here because otherwise we |
10301 | mess with array offsets in the wrong way. This is possible |
10302 | because pointer arithmetic is restricted to retain within an |
10303 | object and overflow on pointer differences is undefined as of |
10304 | 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */ |
10305 | else if (known_eq (bitpos0, bitpos1) |
10306 | && (equality_code |
10307 | || (indirect_base0 |
10308 | && (DECL_P (base0) || CONSTANT_CLASS_P (base0))) |
10309 | || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))) |
10310 | { |
10311 | /* By converting to signed sizetype we cover middle-end pointer |
10312 | arithmetic which operates on unsigned pointer types of size |
10313 | type size and ARRAY_REF offsets which are properly sign or |
10314 | zero extended from their type in case it is narrower than |
10315 | sizetype. */ |
10316 | if (offset0 == NULL_TREE) |
10317 | offset0 = build_int_cst (ssizetype, 0); |
10318 | else |
10319 | offset0 = fold_convert_loc (loc, ssizetype, arg: offset0); |
10320 | if (offset1 == NULL_TREE) |
10321 | offset1 = build_int_cst (ssizetype, 0); |
10322 | else |
10323 | offset1 = fold_convert_loc (loc, ssizetype, arg: offset1); |
10324 | |
10325 | if (!equality_code |
10326 | && (pointer_may_wrap_p (base: base0, offset: offset0, bitpos: bitpos0) |
10327 | || pointer_may_wrap_p (base: base1, offset: offset1, bitpos: bitpos1))) |
10328 | fold_overflow_warning (gmsgid: ("assuming pointer wraparound does not " |
10329 | "occur when comparing P +- C1 with " |
10330 | "P +- C2" ), |
10331 | wc: WARN_STRICT_OVERFLOW_COMPARISON); |
10332 | |
10333 | return fold_build2_loc (loc, code, type, offset0, offset1); |
10334 | } |
10335 | } |
10336 | /* For equal offsets we can simplify to a comparison of the |
10337 | base addresses. */ |
10338 | else if (known_eq (bitpos0, bitpos1) |
10339 | && (indirect_base0 |
10340 | ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0) |
10341 | && (indirect_base1 |
10342 | ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1) |
10343 | && ((offset0 == offset1) |
10344 | || (offset0 && offset1 |
10345 | && operand_equal_p (arg0: offset0, arg1: offset1, flags: 0)))) |
10346 | { |
10347 | if (indirect_base0) |
10348 | base0 = build_fold_addr_expr_loc (loc, t: base0); |
10349 | if (indirect_base1) |
10350 | base1 = build_fold_addr_expr_loc (loc, t: base1); |
10351 | return fold_build2_loc (loc, code, type, base0, base1); |
10352 | } |
10353 | /* Comparison between an ordinary (non-weak) symbol and a null |
10354 | pointer can be eliminated since such symbols must have a non |
10355 | null address. In C, relational expressions between pointers |
10356 | to objects and null pointers are undefined. The results |
10357 | below follow the C++ rules with the additional property that |
10358 | every object pointer compares greater than a null pointer. |
10359 | */ |
10360 | else if (((DECL_P (base0) |
10361 | && maybe_nonzero_address (decl: base0) > 0 |
10362 | /* Avoid folding references to struct members at offset 0 to |
10363 | prevent tests like '&ptr->firstmember == 0' from getting |
10364 | eliminated. When ptr is null, although the -> expression |
10365 | is strictly speaking invalid, GCC retains it as a matter |
10366 | of QoI. See PR c/44555. */ |
10367 | && (offset0 == NULL_TREE && known_ne (bitpos0, 0))) |
10368 | || CONSTANT_CLASS_P (base0)) |
10369 | && indirect_base0 |
10370 | /* The caller guarantees that when one of the arguments is |
10371 | constant (i.e., null in this case) it is second. */ |
10372 | && integer_zerop (arg1)) |
10373 | { |
10374 | switch (code) |
10375 | { |
10376 | case EQ_EXPR: |
10377 | case LE_EXPR: |
10378 | case LT_EXPR: |
10379 | return constant_boolean_node (value: false, type); |
10380 | case GE_EXPR: |
10381 | case GT_EXPR: |
10382 | case NE_EXPR: |
10383 | return constant_boolean_node (value: true, type); |
10384 | default: |
10385 | gcc_unreachable (); |
10386 | } |
10387 | } |
10388 | } |
10389 | |
10390 | /* Transform comparisons of the form X +- C1 CMP Y +- C2 to |
10391 | X CMP Y +- C2 +- C1 for signed X, Y. This is valid if |
10392 | the resulting offset is smaller in absolute value than the |
10393 | original one and has the same sign. */ |
10394 | if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
10395 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)) |
10396 | && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
10397 | && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
10398 | && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))) |
10399 | && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR) |
10400 | && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST |
10401 | && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1)))) |
10402 | { |
10403 | tree const1 = TREE_OPERAND (arg0, 1); |
10404 | tree const2 = TREE_OPERAND (arg1, 1); |
10405 | tree variable1 = TREE_OPERAND (arg0, 0); |
10406 | tree variable2 = TREE_OPERAND (arg1, 0); |
10407 | tree cst; |
10408 | const char * const warnmsg = G_("assuming signed overflow does not " |
10409 | "occur when combining constants around " |
10410 | "a comparison" ); |
10411 | |
10412 | /* Put the constant on the side where it doesn't overflow and is |
10413 | of lower absolute value and of same sign than before. */ |
10414 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) |
10415 | ? MINUS_EXPR : PLUS_EXPR, |
10416 | arg1: const2, arg2: const1); |
10417 | if (!TREE_OVERFLOW (cst) |
10418 | && tree_int_cst_compare (t1: const2, t2: cst) == tree_int_cst_sgn (const2) |
10419 | && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2)) |
10420 | { |
10421 | fold_overflow_warning (gmsgid: warnmsg, wc: WARN_STRICT_OVERFLOW_COMPARISON); |
10422 | return fold_build2_loc (loc, code, type, |
10423 | variable1, |
10424 | fold_build2_loc (loc, TREE_CODE (arg1), |
10425 | TREE_TYPE (arg1), |
10426 | variable2, cst)); |
10427 | } |
10428 | |
10429 | cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1) |
10430 | ? MINUS_EXPR : PLUS_EXPR, |
10431 | arg1: const1, arg2: const2); |
10432 | if (!TREE_OVERFLOW (cst) |
10433 | && tree_int_cst_compare (t1: const1, t2: cst) == tree_int_cst_sgn (const1) |
10434 | && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1)) |
10435 | { |
10436 | fold_overflow_warning (gmsgid: warnmsg, wc: WARN_STRICT_OVERFLOW_COMPARISON); |
10437 | return fold_build2_loc (loc, code, type, |
10438 | fold_build2_loc (loc, TREE_CODE (arg0), |
10439 | TREE_TYPE (arg0), |
10440 | variable1, cst), |
10441 | variable2); |
10442 | } |
10443 | } |
10444 | |
10445 | tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1); |
10446 | if (tem) |
10447 | return tem; |
10448 | |
10449 | /* If we are comparing an expression that just has comparisons |
10450 | of two integer values, arithmetic expressions of those comparisons, |
10451 | and constants, we can simplify it. There are only three cases |
10452 | to check: the two values can either be equal, the first can be |
10453 | greater, or the second can be greater. Fold the expression for |
10454 | those three values. Since each value must be 0 or 1, we have |
10455 | eight possibilities, each of which corresponds to the constant 0 |
10456 | or 1 or one of the six possible comparisons. |
10457 | |
10458 | This handles common cases like (a > b) == 0 but also handles |
10459 | expressions like ((x > y) - (y > x)) > 0, which supposedly |
10460 | occur in macroized code. */ |
10461 | |
10462 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST) |
10463 | { |
10464 | tree cval1 = 0, cval2 = 0; |
10465 | |
10466 | if (twoval_comparison_p (arg: arg0, cval1: &cval1, cval2: &cval2) |
10467 | /* Don't handle degenerate cases here; they should already |
10468 | have been handled anyway. */ |
10469 | && cval1 != 0 && cval2 != 0 |
10470 | && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2)) |
10471 | && TREE_TYPE (cval1) == TREE_TYPE (cval2) |
10472 | && INTEGRAL_TYPE_P (TREE_TYPE (cval1)) |
10473 | && TYPE_MAX_VALUE (TREE_TYPE (cval1)) |
10474 | && TYPE_MAX_VALUE (TREE_TYPE (cval2)) |
10475 | && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)), |
10476 | TYPE_MAX_VALUE (TREE_TYPE (cval2)), flags: 0)) |
10477 | { |
10478 | tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1)); |
10479 | tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1)); |
10480 | |
10481 | /* We can't just pass T to eval_subst in case cval1 or cval2 |
10482 | was the same as ARG1. */ |
10483 | |
10484 | tree high_result |
10485 | = fold_build2_loc (loc, code, type, |
10486 | eval_subst (loc, arg: arg0, old0: cval1, new0: maxval, |
10487 | old1: cval2, new1: minval), |
10488 | arg1); |
10489 | tree equal_result |
10490 | = fold_build2_loc (loc, code, type, |
10491 | eval_subst (loc, arg: arg0, old0: cval1, new0: maxval, |
10492 | old1: cval2, new1: maxval), |
10493 | arg1); |
10494 | tree low_result |
10495 | = fold_build2_loc (loc, code, type, |
10496 | eval_subst (loc, arg: arg0, old0: cval1, new0: minval, |
10497 | old1: cval2, new1: maxval), |
10498 | arg1); |
10499 | |
10500 | /* All three of these results should be 0 or 1. Confirm they are. |
10501 | Then use those values to select the proper code to use. */ |
10502 | |
10503 | if (TREE_CODE (high_result) == INTEGER_CST |
10504 | && TREE_CODE (equal_result) == INTEGER_CST |
10505 | && TREE_CODE (low_result) == INTEGER_CST) |
10506 | { |
10507 | /* Make a 3-bit mask with the high-order bit being the |
10508 | value for `>', the next for '=', and the low for '<'. */ |
10509 | switch ((integer_onep (high_result) * 4) |
10510 | + (integer_onep (equal_result) * 2) |
10511 | + integer_onep (low_result)) |
10512 | { |
10513 | case 0: |
10514 | /* Always false. */ |
10515 | return omit_one_operand_loc (loc, type, integer_zero_node, omitted: arg0); |
10516 | case 1: |
10517 | code = LT_EXPR; |
10518 | break; |
10519 | case 2: |
10520 | code = EQ_EXPR; |
10521 | break; |
10522 | case 3: |
10523 | code = LE_EXPR; |
10524 | break; |
10525 | case 4: |
10526 | code = GT_EXPR; |
10527 | break; |
10528 | case 5: |
10529 | code = NE_EXPR; |
10530 | break; |
10531 | case 6: |
10532 | code = GE_EXPR; |
10533 | break; |
10534 | case 7: |
10535 | /* Always true. */ |
10536 | return omit_one_operand_loc (loc, type, integer_one_node, omitted: arg0); |
10537 | } |
10538 | |
10539 | return fold_build2_loc (loc, code, type, cval1, cval2); |
10540 | } |
10541 | } |
10542 | } |
10543 | |
10544 | return NULL_TREE; |
10545 | } |
10546 | |
10547 | |
10548 | /* Subroutine of fold_binary. Optimize complex multiplications of the |
10549 | form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The |
10550 | argument EXPR represents the expression "z" of type TYPE. */ |
10551 | |
10552 | static tree |
10553 | fold_mult_zconjz (location_t loc, tree type, tree expr) |
10554 | { |
10555 | tree itype = TREE_TYPE (type); |
10556 | tree rpart, ipart, tem; |
10557 | |
10558 | if (TREE_CODE (expr) == COMPLEX_EXPR) |
10559 | { |
10560 | rpart = TREE_OPERAND (expr, 0); |
10561 | ipart = TREE_OPERAND (expr, 1); |
10562 | } |
10563 | else if (TREE_CODE (expr) == COMPLEX_CST) |
10564 | { |
10565 | rpart = TREE_REALPART (expr); |
10566 | ipart = TREE_IMAGPART (expr); |
10567 | } |
10568 | else |
10569 | { |
10570 | expr = save_expr (expr); |
10571 | rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr); |
10572 | ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr); |
10573 | } |
10574 | |
10575 | rpart = save_expr (rpart); |
10576 | ipart = save_expr (ipart); |
10577 | tem = fold_build2_loc (loc, PLUS_EXPR, itype, |
10578 | fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart), |
10579 | fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart)); |
10580 | return fold_build2_loc (loc, COMPLEX_EXPR, type, tem, |
10581 | build_zero_cst (itype)); |
10582 | } |
10583 | |
10584 | |
10585 | /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or |
10586 | CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return |
10587 | true if successful. */ |
10588 | |
10589 | static bool |
10590 | vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts) |
10591 | { |
10592 | unsigned HOST_WIDE_INT i, nunits; |
10593 | |
10594 | if (TREE_CODE (arg) == VECTOR_CST |
10595 | && VECTOR_CST_NELTS (arg).is_constant (const_value: &nunits)) |
10596 | { |
10597 | for (i = 0; i < nunits; ++i) |
10598 | elts[i] = VECTOR_CST_ELT (arg, i); |
10599 | } |
10600 | else if (TREE_CODE (arg) == CONSTRUCTOR) |
10601 | { |
10602 | constructor_elt *elt; |
10603 | |
10604 | FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt) |
10605 | if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE) |
10606 | return false; |
10607 | else |
10608 | elts[i] = elt->value; |
10609 | } |
10610 | else |
10611 | return false; |
10612 | for (; i < nelts; i++) |
10613 | elts[i] |
10614 | = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node); |
10615 | return true; |
10616 | } |
10617 | |
10618 | /* Helper routine for fold_vec_perm_cst to check if SEL is a suitable |
10619 | mask for VLA vec_perm folding. |
10620 | REASON if specified, will contain the reason why SEL is not suitable. |
10621 | Used only for debugging and unit-testing. */ |
10622 | |
10623 | static bool |
10624 | valid_mask_for_fold_vec_perm_cst_p (tree arg0, tree arg1, |
10625 | const vec_perm_indices &sel, |
10626 | const char **reason = NULL) |
10627 | { |
10628 | unsigned sel_npatterns = sel.encoding ().npatterns (); |
10629 | unsigned sel_nelts_per_pattern = sel.encoding ().nelts_per_pattern (); |
10630 | |
10631 | if (!(pow2p_hwi (x: sel_npatterns) |
10632 | && pow2p_hwi (VECTOR_CST_NPATTERNS (arg0)) |
10633 | && pow2p_hwi (VECTOR_CST_NPATTERNS (arg1)))) |
10634 | { |
10635 | if (reason) |
10636 | *reason = "npatterns is not power of 2" ; |
10637 | return false; |
10638 | } |
10639 | |
10640 | /* We want to avoid cases where sel.length is not a multiple of npatterns. |
10641 | For eg: sel.length = 2 + 2x, and sel npatterns = 4. */ |
10642 | poly_uint64 esel; |
10643 | if (!multiple_p (a: sel.length (), b: sel_npatterns, multiple: &esel)) |
10644 | { |
10645 | if (reason) |
10646 | *reason = "sel.length is not multiple of sel_npatterns" ; |
10647 | return false; |
10648 | } |
10649 | |
10650 | if (sel_nelts_per_pattern < 3) |
10651 | return true; |
10652 | |
10653 | for (unsigned pattern = 0; pattern < sel_npatterns; pattern++) |
10654 | { |
10655 | poly_uint64 a1 = sel[pattern + sel_npatterns]; |
10656 | poly_uint64 a2 = sel[pattern + 2 * sel_npatterns]; |
10657 | HOST_WIDE_INT step; |
10658 | if (!poly_int64 (a2 - a1).is_constant (const_value: &step)) |
10659 | { |
10660 | if (reason) |
10661 | *reason = "step is not constant" ; |
10662 | return false; |
10663 | } |
10664 | // FIXME: Punt on step < 0 for now, revisit later. |
10665 | if (step < 0) |
10666 | return false; |
10667 | if (step == 0) |
10668 | continue; |
10669 | |
10670 | if (!pow2p_hwi (x: step)) |
10671 | { |
10672 | if (reason) |
10673 | *reason = "step is not power of 2" ; |
10674 | return false; |
10675 | } |
10676 | |
10677 | /* Ensure that stepped sequence of the pattern selects elements |
10678 | only from the same input vector. */ |
10679 | uint64_t q1, qe; |
10680 | poly_uint64 r1, re; |
10681 | poly_uint64 ae = a1 + (esel - 2) * step; |
10682 | poly_uint64 arg_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
10683 | |
10684 | if (!(can_div_trunc_p (a: a1, b: arg_len, quotient: &q1, remainder: &r1) |
10685 | && can_div_trunc_p (a: ae, b: arg_len, quotient: &qe, remainder: &re) |
10686 | && q1 == qe)) |
10687 | { |
10688 | if (reason) |
10689 | *reason = "crossed input vectors" ; |
10690 | return false; |
10691 | } |
10692 | |
10693 | /* Ensure that the stepped sequence always selects from the same |
10694 | input pattern. */ |
10695 | tree arg = ((q1 & 1) == 0) ? arg0 : arg1; |
10696 | unsigned arg_npatterns = VECTOR_CST_NPATTERNS (arg); |
10697 | |
10698 | if (!multiple_p (a: step, b: arg_npatterns)) |
10699 | { |
10700 | if (reason) |
10701 | *reason = "step is not multiple of npatterns" ; |
10702 | return false; |
10703 | } |
10704 | |
10705 | /* If a1 chooses base element from arg, ensure that it's a natural |
10706 | stepped sequence, ie, (arg[2] - arg[1]) == (arg[1] - arg[0]) |
10707 | to preserve arg's encoding. */ |
10708 | |
10709 | if (maybe_lt (a: r1, b: arg_npatterns)) |
10710 | { |
10711 | unsigned HOST_WIDE_INT index; |
10712 | if (!r1.is_constant (const_value: &index)) |
10713 | return false; |
10714 | |
10715 | tree arg_elem0 = vector_cst_elt (arg, index); |
10716 | tree arg_elem1 = vector_cst_elt (arg, index + arg_npatterns); |
10717 | tree arg_elem2 = vector_cst_elt (arg, index + arg_npatterns * 2); |
10718 | |
10719 | tree step1, step2; |
10720 | if (!(step1 = const_binop (code: MINUS_EXPR, arg1: arg_elem1, arg2: arg_elem0)) |
10721 | || !(step2 = const_binop (code: MINUS_EXPR, arg1: arg_elem2, arg2: arg_elem1)) |
10722 | || !operand_equal_p (arg0: step1, arg1: step2, flags: 0)) |
10723 | { |
10724 | if (reason) |
10725 | *reason = "not a natural stepped sequence" ; |
10726 | return false; |
10727 | } |
10728 | } |
10729 | } |
10730 | |
10731 | return true; |
10732 | } |
10733 | |
10734 | /* Try to fold permutation of ARG0 and ARG1 with SEL selector when |
10735 | the input vectors are VECTOR_CST. Return NULL_TREE otherwise. |
10736 | REASON has same purpose as described in |
10737 | valid_mask_for_fold_vec_perm_cst_p. */ |
10738 | |
10739 | static tree |
10740 | fold_vec_perm_cst (tree type, tree arg0, tree arg1, const vec_perm_indices &sel, |
10741 | const char **reason = NULL) |
10742 | { |
10743 | unsigned res_npatterns, res_nelts_per_pattern; |
10744 | unsigned HOST_WIDE_INT res_nelts; |
10745 | |
10746 | /* (1) If SEL is a suitable mask as determined by |
10747 | valid_mask_for_fold_vec_perm_cst_p, then: |
10748 | res_npatterns = max of npatterns between ARG0, ARG1, and SEL |
10749 | res_nelts_per_pattern = max of nelts_per_pattern between |
10750 | ARG0, ARG1 and SEL. |
10751 | (2) If SEL is not a suitable mask, and TYPE is VLS then: |
10752 | res_npatterns = nelts in result vector. |
10753 | res_nelts_per_pattern = 1. |
10754 | This exception is made so that VLS ARG0, ARG1 and SEL work as before. */ |
10755 | if (valid_mask_for_fold_vec_perm_cst_p (arg0, arg1, sel, reason)) |
10756 | { |
10757 | res_npatterns |
10758 | = std::max (VECTOR_CST_NPATTERNS (arg0), |
10759 | b: std::max (VECTOR_CST_NPATTERNS (arg1), |
10760 | b: sel.encoding ().npatterns ())); |
10761 | |
10762 | res_nelts_per_pattern |
10763 | = std::max (VECTOR_CST_NELTS_PER_PATTERN (arg0), |
10764 | b: std::max (VECTOR_CST_NELTS_PER_PATTERN (arg1), |
10765 | b: sel.encoding ().nelts_per_pattern ())); |
10766 | |
10767 | res_nelts = res_npatterns * res_nelts_per_pattern; |
10768 | } |
10769 | else if (TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &res_nelts)) |
10770 | { |
10771 | res_npatterns = res_nelts; |
10772 | res_nelts_per_pattern = 1; |
10773 | } |
10774 | else |
10775 | return NULL_TREE; |
10776 | |
10777 | tree_vector_builder out_elts (type, res_npatterns, res_nelts_per_pattern); |
10778 | for (unsigned i = 0; i < res_nelts; i++) |
10779 | { |
10780 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
10781 | uint64_t q; |
10782 | poly_uint64 r; |
10783 | unsigned HOST_WIDE_INT index; |
10784 | |
10785 | /* Punt if sel[i] /trunc_div len cannot be determined, |
10786 | because the input vector to be chosen will depend on |
10787 | runtime vector length. |
10788 | For example if len == 4 + 4x, and sel[i] == 4, |
10789 | If len at runtime equals 4, we choose arg1[0]. |
10790 | For any other value of len > 4 at runtime, we choose arg0[4]. |
10791 | which makes the element choice dependent on runtime vector length. */ |
10792 | if (!can_div_trunc_p (a: sel[i], b: len, quotient: &q, remainder: &r)) |
10793 | { |
10794 | if (reason) |
10795 | *reason = "cannot divide selector element by arg len" ; |
10796 | return NULL_TREE; |
10797 | } |
10798 | |
10799 | /* sel[i] % len will give the index of element in the chosen input |
10800 | vector. For example if sel[i] == 5 + 4x and len == 4 + 4x, |
10801 | we will choose arg1[1] since (5 + 4x) % (4 + 4x) == 1. */ |
10802 | if (!r.is_constant (const_value: &index)) |
10803 | { |
10804 | if (reason) |
10805 | *reason = "remainder is not constant" ; |
10806 | return NULL_TREE; |
10807 | } |
10808 | |
10809 | tree arg = ((q & 1) == 0) ? arg0 : arg1; |
10810 | tree elem = vector_cst_elt (arg, index); |
10811 | out_elts.quick_push (obj: elem); |
10812 | } |
10813 | |
10814 | return out_elts.build (); |
10815 | } |
10816 | |
10817 | /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL |
10818 | selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful, |
10819 | NULL_TREE otherwise. */ |
10820 | |
10821 | tree |
10822 | fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel) |
10823 | { |
10824 | unsigned int i; |
10825 | unsigned HOST_WIDE_INT nelts; |
10826 | |
10827 | gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), sel.length ()) |
10828 | && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), |
10829 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))); |
10830 | |
10831 | if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type) |
10832 | || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type)) |
10833 | return NULL_TREE; |
10834 | |
10835 | if (TREE_CODE (arg0) == VECTOR_CST |
10836 | && TREE_CODE (arg1) == VECTOR_CST) |
10837 | return fold_vec_perm_cst (type, arg0, arg1, sel); |
10838 | |
10839 | /* For fall back case, we want to ensure we have VLS vectors |
10840 | with equal length. */ |
10841 | if (!sel.length ().is_constant (const_value: &nelts)) |
10842 | return NULL_TREE; |
10843 | |
10844 | gcc_assert (known_eq (sel.length (), |
10845 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))); |
10846 | tree *in_elts = XALLOCAVEC (tree, nelts * 2); |
10847 | if (!vec_cst_ctor_to_array (arg: arg0, nelts, elts: in_elts) |
10848 | || !vec_cst_ctor_to_array (arg: arg1, nelts, elts: in_elts + nelts)) |
10849 | return NULL_TREE; |
10850 | |
10851 | vec<constructor_elt, va_gc> *v; |
10852 | vec_alloc (v, nelems: nelts); |
10853 | for (i = 0; i < nelts; i++) |
10854 | { |
10855 | HOST_WIDE_INT index; |
10856 | if (!sel[i].is_constant (const_value: &index)) |
10857 | return NULL_TREE; |
10858 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, in_elts[index]); |
10859 | } |
10860 | return build_constructor (type, v); |
10861 | } |
10862 | |
10863 | /* Try to fold a pointer difference of type TYPE two address expressions of |
10864 | array references AREF0 and AREF1 using location LOC. Return a |
10865 | simplified expression for the difference or NULL_TREE. */ |
10866 | |
10867 | static tree |
10868 | fold_addr_of_array_ref_difference (location_t loc, tree type, |
10869 | tree aref0, tree aref1, |
10870 | bool use_pointer_diff) |
10871 | { |
10872 | tree base0 = TREE_OPERAND (aref0, 0); |
10873 | tree base1 = TREE_OPERAND (aref1, 0); |
10874 | tree base_offset = build_int_cst (type, 0); |
10875 | |
10876 | /* If the bases are array references as well, recurse. If the bases |
10877 | are pointer indirections compute the difference of the pointers. |
10878 | If the bases are equal, we are set. */ |
10879 | if ((TREE_CODE (base0) == ARRAY_REF |
10880 | && TREE_CODE (base1) == ARRAY_REF |
10881 | && (base_offset |
10882 | = fold_addr_of_array_ref_difference (loc, type, aref0: base0, aref1: base1, |
10883 | use_pointer_diff))) |
10884 | || (INDIRECT_REF_P (base0) |
10885 | && INDIRECT_REF_P (base1) |
10886 | && (base_offset |
10887 | = use_pointer_diff |
10888 | ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type, |
10889 | TREE_OPERAND (base0, 0), |
10890 | TREE_OPERAND (base1, 0)) |
10891 | : fold_binary_loc (loc, MINUS_EXPR, type, |
10892 | fold_convert (type, |
10893 | TREE_OPERAND (base0, 0)), |
10894 | fold_convert (type, |
10895 | TREE_OPERAND (base1, 0))))) |
10896 | || operand_equal_p (arg0: base0, arg1: base1, flags: OEP_ADDRESS_OF)) |
10897 | { |
10898 | tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1)); |
10899 | tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1)); |
10900 | tree esz = fold_convert_loc (loc, type, arg: array_ref_element_size (aref0)); |
10901 | tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1); |
10902 | return fold_build2_loc (loc, PLUS_EXPR, type, |
10903 | base_offset, |
10904 | fold_build2_loc (loc, MULT_EXPR, type, |
10905 | diff, esz)); |
10906 | } |
10907 | return NULL_TREE; |
10908 | } |
10909 | |
10910 | /* If the real or vector real constant CST of type TYPE has an exact |
10911 | inverse, return it, else return NULL. */ |
10912 | |
10913 | tree |
10914 | exact_inverse (tree type, tree cst) |
10915 | { |
10916 | REAL_VALUE_TYPE r; |
10917 | tree unit_type; |
10918 | machine_mode mode; |
10919 | |
10920 | switch (TREE_CODE (cst)) |
10921 | { |
10922 | case REAL_CST: |
10923 | r = TREE_REAL_CST (cst); |
10924 | |
10925 | if (exact_real_inverse (TYPE_MODE (type), &r)) |
10926 | return build_real (type, r); |
10927 | |
10928 | return NULL_TREE; |
10929 | |
10930 | case VECTOR_CST: |
10931 | { |
10932 | unit_type = TREE_TYPE (type); |
10933 | mode = TYPE_MODE (unit_type); |
10934 | |
10935 | tree_vector_builder elts; |
10936 | if (!elts.new_unary_operation (shape: type, vec: cst, allow_stepped_p: false)) |
10937 | return NULL_TREE; |
10938 | unsigned int count = elts.encoded_nelts (); |
10939 | for (unsigned int i = 0; i < count; ++i) |
10940 | { |
10941 | r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i)); |
10942 | if (!exact_real_inverse (mode, &r)) |
10943 | return NULL_TREE; |
10944 | elts.quick_push (obj: build_real (unit_type, r)); |
10945 | } |
10946 | |
10947 | return elts.build (); |
10948 | } |
10949 | |
10950 | default: |
10951 | return NULL_TREE; |
10952 | } |
10953 | } |
10954 | |
10955 | /* Mask out the tz least significant bits of X of type TYPE where |
10956 | tz is the number of trailing zeroes in Y. */ |
10957 | static wide_int |
10958 | mask_with_tz (tree type, const wide_int &x, const wide_int &y) |
10959 | { |
10960 | int tz = wi::ctz (y); |
10961 | if (tz > 0) |
10962 | return wi::mask (width: tz, negate_p: true, TYPE_PRECISION (type)) & x; |
10963 | return x; |
10964 | } |
10965 | |
10966 | /* Return true when T is an address and is known to be nonzero. |
10967 | For floating point we further ensure that T is not denormal. |
10968 | Similar logic is present in nonzero_address in rtlanal.h. |
10969 | |
10970 | If the return value is based on the assumption that signed overflow |
10971 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't |
10972 | change *STRICT_OVERFLOW_P. */ |
10973 | |
10974 | static bool |
10975 | tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p) |
10976 | { |
10977 | tree type = TREE_TYPE (t); |
10978 | enum tree_code code; |
10979 | |
10980 | /* Doing something useful for floating point would need more work. */ |
10981 | if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type)) |
10982 | return false; |
10983 | |
10984 | code = TREE_CODE (t); |
10985 | switch (TREE_CODE_CLASS (code)) |
10986 | { |
10987 | case tcc_unary: |
10988 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), |
10989 | strict_overflow_p); |
10990 | case tcc_binary: |
10991 | case tcc_comparison: |
10992 | return tree_binary_nonzero_warnv_p (code, type, |
10993 | TREE_OPERAND (t, 0), |
10994 | TREE_OPERAND (t, 1), |
10995 | strict_overflow_p); |
10996 | case tcc_constant: |
10997 | case tcc_declaration: |
10998 | case tcc_reference: |
10999 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); |
11000 | |
11001 | default: |
11002 | break; |
11003 | } |
11004 | |
11005 | switch (code) |
11006 | { |
11007 | case TRUTH_NOT_EXPR: |
11008 | return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0), |
11009 | strict_overflow_p); |
11010 | |
11011 | case TRUTH_AND_EXPR: |
11012 | case TRUTH_OR_EXPR: |
11013 | case TRUTH_XOR_EXPR: |
11014 | return tree_binary_nonzero_warnv_p (code, type, |
11015 | TREE_OPERAND (t, 0), |
11016 | TREE_OPERAND (t, 1), |
11017 | strict_overflow_p); |
11018 | |
11019 | case COND_EXPR: |
11020 | case CONSTRUCTOR: |
11021 | case OBJ_TYPE_REF: |
11022 | case ADDR_EXPR: |
11023 | case WITH_SIZE_EXPR: |
11024 | case SSA_NAME: |
11025 | return tree_single_nonzero_warnv_p (t, strict_overflow_p); |
11026 | |
11027 | case COMPOUND_EXPR: |
11028 | case MODIFY_EXPR: |
11029 | case BIND_EXPR: |
11030 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), |
11031 | strict_overflow_p); |
11032 | |
11033 | case SAVE_EXPR: |
11034 | return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0), |
11035 | strict_overflow_p); |
11036 | |
11037 | case CALL_EXPR: |
11038 | { |
11039 | tree fndecl = get_callee_fndecl (t); |
11040 | if (!fndecl) return false; |
11041 | if (flag_delete_null_pointer_checks && !flag_check_new |
11042 | && DECL_IS_OPERATOR_NEW_P (fndecl) |
11043 | && !TREE_NOTHROW (fndecl)) |
11044 | return true; |
11045 | if (flag_delete_null_pointer_checks |
11046 | && lookup_attribute (attr_name: "returns_nonnull" , |
11047 | TYPE_ATTRIBUTES (TREE_TYPE (fndecl)))) |
11048 | return true; |
11049 | return alloca_call_p (t); |
11050 | } |
11051 | |
11052 | default: |
11053 | break; |
11054 | } |
11055 | return false; |
11056 | } |
11057 | |
11058 | /* Return true when T is an address and is known to be nonzero. |
11059 | Handle warnings about undefined signed overflow. */ |
11060 | |
11061 | bool |
11062 | tree_expr_nonzero_p (tree t) |
11063 | { |
11064 | bool ret, strict_overflow_p; |
11065 | |
11066 | strict_overflow_p = false; |
11067 | ret = tree_expr_nonzero_warnv_p (t, strict_overflow_p: &strict_overflow_p); |
11068 | if (strict_overflow_p) |
11069 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur when " |
11070 | "determining that expression is always " |
11071 | "non-zero" ), |
11072 | wc: WARN_STRICT_OVERFLOW_MISC); |
11073 | return ret; |
11074 | } |
11075 | |
11076 | /* Return true if T is known not to be equal to an integer W. */ |
11077 | |
11078 | bool |
11079 | expr_not_equal_to (tree t, const wide_int &w) |
11080 | { |
11081 | int_range_max vr; |
11082 | switch (TREE_CODE (t)) |
11083 | { |
11084 | case INTEGER_CST: |
11085 | return wi::to_wide (t) != w; |
11086 | |
11087 | case SSA_NAME: |
11088 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) |
11089 | return false; |
11090 | |
11091 | get_range_query (cfun)->range_of_expr (r&: vr, expr: t); |
11092 | if (!vr.undefined_p () && !vr.contains_p (w)) |
11093 | return true; |
11094 | /* If T has some known zero bits and W has any of those bits set, |
11095 | then T is known not to be equal to W. */ |
11096 | if (wi::ne_p (x: wi::zext (x: wi::bit_and_not (x: w, y: get_nonzero_bits (t)), |
11097 | TYPE_PRECISION (TREE_TYPE (t))), y: 0)) |
11098 | return true; |
11099 | return false; |
11100 | |
11101 | default: |
11102 | return false; |
11103 | } |
11104 | } |
11105 | |
11106 | /* Fold a binary expression of code CODE and type TYPE with operands |
11107 | OP0 and OP1. LOC is the location of the resulting expression. |
11108 | Return the folded expression if folding is successful. Otherwise, |
11109 | return NULL_TREE. */ |
11110 | |
11111 | tree |
11112 | fold_binary_loc (location_t loc, enum tree_code code, tree type, |
11113 | tree op0, tree op1) |
11114 | { |
11115 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
11116 | tree arg0, arg1, tem; |
11117 | tree t1 = NULL_TREE; |
11118 | bool strict_overflow_p; |
11119 | unsigned int prec; |
11120 | |
11121 | gcc_assert (IS_EXPR_CODE_CLASS (kind) |
11122 | && TREE_CODE_LENGTH (code) == 2 |
11123 | && op0 != NULL_TREE |
11124 | && op1 != NULL_TREE); |
11125 | |
11126 | arg0 = op0; |
11127 | arg1 = op1; |
11128 | |
11129 | /* Strip any conversions that don't change the mode. This is |
11130 | safe for every expression, except for a comparison expression |
11131 | because its signedness is derived from its operands. So, in |
11132 | the latter case, only strip conversions that don't change the |
11133 | signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments |
11134 | preserved. |
11135 | |
11136 | Note that this is done as an internal manipulation within the |
11137 | constant folder, in order to find the simplest representation |
11138 | of the arguments so that their form can be studied. In any |
11139 | cases, the appropriate type conversions should be put back in |
11140 | the tree that will get out of the constant folder. */ |
11141 | |
11142 | if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR) |
11143 | { |
11144 | STRIP_SIGN_NOPS (arg0); |
11145 | STRIP_SIGN_NOPS (arg1); |
11146 | } |
11147 | else |
11148 | { |
11149 | STRIP_NOPS (arg0); |
11150 | STRIP_NOPS (arg1); |
11151 | } |
11152 | |
11153 | /* Note that TREE_CONSTANT isn't enough: static var addresses are |
11154 | constant but we can't do arithmetic on them. */ |
11155 | if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1)) |
11156 | { |
11157 | tem = const_binop (code, type, arg1: arg0, arg2: arg1); |
11158 | if (tem != NULL_TREE) |
11159 | { |
11160 | if (TREE_TYPE (tem) != type) |
11161 | tem = fold_convert_loc (loc, type, arg: tem); |
11162 | return tem; |
11163 | } |
11164 | } |
11165 | |
11166 | /* If this is a commutative operation, and ARG0 is a constant, move it |
11167 | to ARG1 to reduce the number of tests below. */ |
11168 | if (commutative_tree_code (code) |
11169 | && tree_swap_operands_p (arg0, arg1)) |
11170 | return fold_build2_loc (loc, code, type, op1, op0); |
11171 | |
11172 | /* Likewise if this is a comparison, and ARG0 is a constant, move it |
11173 | to ARG1 to reduce the number of tests below. */ |
11174 | if (kind == tcc_comparison |
11175 | && tree_swap_operands_p (arg0, arg1)) |
11176 | return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0); |
11177 | |
11178 | tem = generic_simplify (loc, code, type, op0, op1); |
11179 | if (tem) |
11180 | return tem; |
11181 | |
11182 | /* ARG0 is the first operand of EXPR, and ARG1 is the second operand. |
11183 | |
11184 | First check for cases where an arithmetic operation is applied to a |
11185 | compound, conditional, or comparison operation. Push the arithmetic |
11186 | operation inside the compound or conditional to see if any folding |
11187 | can then be done. Convert comparison to conditional for this purpose. |
11188 | The also optimizes non-constant cases that used to be done in |
11189 | expand_expr. |
11190 | |
11191 | Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR, |
11192 | one of the operands is a comparison and the other is a comparison, a |
11193 | BIT_AND_EXPR with the constant 1, or a truth value. In that case, the |
11194 | code below would make the expression more complex. Change it to a |
11195 | TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to |
11196 | TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */ |
11197 | |
11198 | if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR |
11199 | || code == EQ_EXPR || code == NE_EXPR) |
11200 | && !VECTOR_TYPE_P (TREE_TYPE (arg0)) |
11201 | && ((truth_value_p (TREE_CODE (arg0)) |
11202 | && (truth_value_p (TREE_CODE (arg1)) |
11203 | || (TREE_CODE (arg1) == BIT_AND_EXPR |
11204 | && integer_onep (TREE_OPERAND (arg1, 1))))) |
11205 | || (truth_value_p (TREE_CODE (arg1)) |
11206 | && (truth_value_p (TREE_CODE (arg0)) |
11207 | || (TREE_CODE (arg0) == BIT_AND_EXPR |
11208 | && integer_onep (TREE_OPERAND (arg0, 1))))))) |
11209 | { |
11210 | tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR |
11211 | : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR |
11212 | : TRUTH_XOR_EXPR, |
11213 | boolean_type_node, |
11214 | fold_convert_loc (loc, boolean_type_node, arg: arg0), |
11215 | fold_convert_loc (loc, boolean_type_node, arg: arg1)); |
11216 | |
11217 | if (code == EQ_EXPR) |
11218 | tem = invert_truthvalue_loc (loc, arg: tem); |
11219 | |
11220 | return fold_convert_loc (loc, type, arg: tem); |
11221 | } |
11222 | |
11223 | if (TREE_CODE_CLASS (code) == tcc_binary |
11224 | || TREE_CODE_CLASS (code) == tcc_comparison) |
11225 | { |
11226 | if (TREE_CODE (arg0) == COMPOUND_EXPR) |
11227 | { |
11228 | tem = fold_build2_loc (loc, code, type, |
11229 | fold_convert_loc (loc, TREE_TYPE (op0), |
11230 | TREE_OPERAND (arg0, 1)), op1); |
11231 | return build2_loc (loc, code: COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), |
11232 | arg1: tem); |
11233 | } |
11234 | if (TREE_CODE (arg1) == COMPOUND_EXPR) |
11235 | { |
11236 | tem = fold_build2_loc (loc, code, type, op0, |
11237 | fold_convert_loc (loc, TREE_TYPE (op1), |
11238 | TREE_OPERAND (arg1, 1))); |
11239 | return build2_loc (loc, code: COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), |
11240 | arg1: tem); |
11241 | } |
11242 | |
11243 | if (TREE_CODE (arg0) == COND_EXPR |
11244 | || TREE_CODE (arg0) == VEC_COND_EXPR |
11245 | || COMPARISON_CLASS_P (arg0)) |
11246 | { |
11247 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
11248 | cond: arg0, arg: arg1, |
11249 | /*cond_first_p=*/1); |
11250 | if (tem != NULL_TREE) |
11251 | return tem; |
11252 | } |
11253 | |
11254 | if (TREE_CODE (arg1) == COND_EXPR |
11255 | || TREE_CODE (arg1) == VEC_COND_EXPR |
11256 | || COMPARISON_CLASS_P (arg1)) |
11257 | { |
11258 | tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1, |
11259 | cond: arg1, arg: arg0, |
11260 | /*cond_first_p=*/0); |
11261 | if (tem != NULL_TREE) |
11262 | return tem; |
11263 | } |
11264 | } |
11265 | |
11266 | switch (code) |
11267 | { |
11268 | case MEM_REF: |
11269 | /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */ |
11270 | if (TREE_CODE (arg0) == ADDR_EXPR |
11271 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF) |
11272 | { |
11273 | tree iref = TREE_OPERAND (arg0, 0); |
11274 | return fold_build2 (MEM_REF, type, |
11275 | TREE_OPERAND (iref, 0), |
11276 | int_const_binop (PLUS_EXPR, arg1, |
11277 | TREE_OPERAND (iref, 1))); |
11278 | } |
11279 | |
11280 | /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */ |
11281 | if (TREE_CODE (arg0) == ADDR_EXPR |
11282 | && handled_component_p (TREE_OPERAND (arg0, 0))) |
11283 | { |
11284 | tree base; |
11285 | poly_int64 coffset; |
11286 | base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0), |
11287 | &coffset); |
11288 | if (!base) |
11289 | return NULL_TREE; |
11290 | return fold_build2 (MEM_REF, type, |
11291 | build1 (ADDR_EXPR, TREE_TYPE (arg0), base), |
11292 | int_const_binop (PLUS_EXPR, arg1, |
11293 | size_int (coffset))); |
11294 | } |
11295 | |
11296 | return NULL_TREE; |
11297 | |
11298 | case POINTER_PLUS_EXPR: |
11299 | /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */ |
11300 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) |
11301 | && INTEGRAL_TYPE_P (TREE_TYPE (arg0))) |
11302 | return fold_convert_loc (loc, type, |
11303 | arg: fold_build2_loc (loc, PLUS_EXPR, sizetype, |
11304 | fold_convert_loc (loc, sizetype, |
11305 | arg: arg1), |
11306 | fold_convert_loc (loc, sizetype, |
11307 | arg: arg0))); |
11308 | |
11309 | return NULL_TREE; |
11310 | |
11311 | case PLUS_EXPR: |
11312 | if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type)) |
11313 | { |
11314 | /* X + (X / CST) * -CST is X % CST. */ |
11315 | if (TREE_CODE (arg1) == MULT_EXPR |
11316 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR |
11317 | && operand_equal_p (arg0, |
11318 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), flags: 0)) |
11319 | { |
11320 | tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1); |
11321 | tree cst1 = TREE_OPERAND (arg1, 1); |
11322 | tree sum = fold_binary_loc (loc, code: PLUS_EXPR, TREE_TYPE (cst1), |
11323 | op0: cst1, op1: cst0); |
11324 | if (sum && integer_zerop (sum)) |
11325 | return fold_convert_loc (loc, type, |
11326 | arg: fold_build2_loc (loc, TRUNC_MOD_EXPR, |
11327 | TREE_TYPE (arg0), arg0, |
11328 | cst0)); |
11329 | } |
11330 | } |
11331 | |
11332 | /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or |
11333 | one. Make sure the type is not saturating and has the signedness of |
11334 | the stripped operands, as fold_plusminus_mult_expr will re-associate. |
11335 | ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */ |
11336 | if ((TREE_CODE (arg0) == MULT_EXPR |
11337 | || TREE_CODE (arg1) == MULT_EXPR) |
11338 | && !TYPE_SATURATING (type) |
11339 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0)) |
11340 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1)) |
11341 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
11342 | { |
11343 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
11344 | if (tem) |
11345 | return tem; |
11346 | } |
11347 | |
11348 | if (! FLOAT_TYPE_P (type)) |
11349 | { |
11350 | /* Reassociate (plus (plus (mult) (foo)) (mult)) as |
11351 | (plus (plus (mult) (mult)) (foo)) so that we can |
11352 | take advantage of the factoring cases below. */ |
11353 | if (ANY_INTEGRAL_TYPE_P (type) |
11354 | && TYPE_OVERFLOW_WRAPS (type) |
11355 | && (((TREE_CODE (arg0) == PLUS_EXPR |
11356 | || TREE_CODE (arg0) == MINUS_EXPR) |
11357 | && TREE_CODE (arg1) == MULT_EXPR) |
11358 | || ((TREE_CODE (arg1) == PLUS_EXPR |
11359 | || TREE_CODE (arg1) == MINUS_EXPR) |
11360 | && TREE_CODE (arg0) == MULT_EXPR))) |
11361 | { |
11362 | tree parg0, parg1, parg, marg; |
11363 | enum tree_code pcode; |
11364 | |
11365 | if (TREE_CODE (arg1) == MULT_EXPR) |
11366 | parg = arg0, marg = arg1; |
11367 | else |
11368 | parg = arg1, marg = arg0; |
11369 | pcode = TREE_CODE (parg); |
11370 | parg0 = TREE_OPERAND (parg, 0); |
11371 | parg1 = TREE_OPERAND (parg, 1); |
11372 | STRIP_NOPS (parg0); |
11373 | STRIP_NOPS (parg1); |
11374 | |
11375 | if (TREE_CODE (parg0) == MULT_EXPR |
11376 | && TREE_CODE (parg1) != MULT_EXPR) |
11377 | return fold_build2_loc (loc, pcode, type, |
11378 | fold_build2_loc (loc, PLUS_EXPR, type, |
11379 | fold_convert_loc (loc, type, |
11380 | arg: parg0), |
11381 | fold_convert_loc (loc, type, |
11382 | arg: marg)), |
11383 | fold_convert_loc (loc, type, arg: parg1)); |
11384 | if (TREE_CODE (parg0) != MULT_EXPR |
11385 | && TREE_CODE (parg1) == MULT_EXPR) |
11386 | return |
11387 | fold_build2_loc (loc, PLUS_EXPR, type, |
11388 | fold_convert_loc (loc, type, arg: parg0), |
11389 | fold_build2_loc (loc, pcode, type, |
11390 | fold_convert_loc (loc, type, arg: marg), |
11391 | fold_convert_loc (loc, type, |
11392 | arg: parg1))); |
11393 | } |
11394 | } |
11395 | else |
11396 | { |
11397 | /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y ) |
11398 | to __complex__ ( x, y ). This is not the same for SNaNs or |
11399 | if signed zeros are involved. */ |
11400 | if (!HONOR_SNANS (arg0) |
11401 | && !HONOR_SIGNED_ZEROS (arg0) |
11402 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) |
11403 | { |
11404 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); |
11405 | tree arg0r = fold_unary_loc (loc, code: REALPART_EXPR, type: rtype, op0: arg0); |
11406 | tree arg0i = fold_unary_loc (loc, code: IMAGPART_EXPR, type: rtype, op0: arg0); |
11407 | bool arg0rz = false, arg0iz = false; |
11408 | if ((arg0r && (arg0rz = real_zerop (arg0r))) |
11409 | || (arg0i && (arg0iz = real_zerop (arg0i)))) |
11410 | { |
11411 | tree arg1r = fold_unary_loc (loc, code: REALPART_EXPR, type: rtype, op0: arg1); |
11412 | tree arg1i = fold_unary_loc (loc, code: IMAGPART_EXPR, type: rtype, op0: arg1); |
11413 | if (arg0rz && arg1i && real_zerop (arg1i)) |
11414 | { |
11415 | tree rp = arg1r ? arg1r |
11416 | : build1 (REALPART_EXPR, rtype, arg1); |
11417 | tree ip = arg0i ? arg0i |
11418 | : build1 (IMAGPART_EXPR, rtype, arg0); |
11419 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
11420 | } |
11421 | else if (arg0iz && arg1r && real_zerop (arg1r)) |
11422 | { |
11423 | tree rp = arg0r ? arg0r |
11424 | : build1 (REALPART_EXPR, rtype, arg0); |
11425 | tree ip = arg1i ? arg1i |
11426 | : build1 (IMAGPART_EXPR, rtype, arg1); |
11427 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
11428 | } |
11429 | } |
11430 | } |
11431 | |
11432 | /* Convert a + (b*c + d*e) into (a + b*c) + d*e. |
11433 | We associate floats only if the user has specified |
11434 | -fassociative-math. */ |
11435 | if (flag_associative_math |
11436 | && TREE_CODE (arg1) == PLUS_EXPR |
11437 | && TREE_CODE (arg0) != MULT_EXPR) |
11438 | { |
11439 | tree tree10 = TREE_OPERAND (arg1, 0); |
11440 | tree tree11 = TREE_OPERAND (arg1, 1); |
11441 | if (TREE_CODE (tree11) == MULT_EXPR |
11442 | && TREE_CODE (tree10) == MULT_EXPR) |
11443 | { |
11444 | tree tree0; |
11445 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10); |
11446 | return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11); |
11447 | } |
11448 | } |
11449 | /* Convert (b*c + d*e) + a into b*c + (d*e +a). |
11450 | We associate floats only if the user has specified |
11451 | -fassociative-math. */ |
11452 | if (flag_associative_math |
11453 | && TREE_CODE (arg0) == PLUS_EXPR |
11454 | && TREE_CODE (arg1) != MULT_EXPR) |
11455 | { |
11456 | tree tree00 = TREE_OPERAND (arg0, 0); |
11457 | tree tree01 = TREE_OPERAND (arg0, 1); |
11458 | if (TREE_CODE (tree01) == MULT_EXPR |
11459 | && TREE_CODE (tree00) == MULT_EXPR) |
11460 | { |
11461 | tree tree0; |
11462 | tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1); |
11463 | return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0); |
11464 | } |
11465 | } |
11466 | } |
11467 | |
11468 | bit_rotate: |
11469 | /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A |
11470 | is a rotate of A by C1 bits. */ |
11471 | /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A |
11472 | is a rotate of A by B bits. |
11473 | Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1, |
11474 | though in this case CODE must be | and not + or ^, otherwise |
11475 | it doesn't return A when B is 0. */ |
11476 | { |
11477 | enum tree_code code0, code1; |
11478 | tree rtype; |
11479 | code0 = TREE_CODE (arg0); |
11480 | code1 = TREE_CODE (arg1); |
11481 | if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR) |
11482 | || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR)) |
11483 | && operand_equal_p (TREE_OPERAND (arg0, 0), |
11484 | TREE_OPERAND (arg1, 0), flags: 0) |
11485 | && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)), |
11486 | TYPE_UNSIGNED (rtype)) |
11487 | /* Only create rotates in complete modes. Other cases are not |
11488 | expanded properly. */ |
11489 | && (element_precision (rtype) |
11490 | == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype)))) |
11491 | { |
11492 | tree tree01, tree11; |
11493 | tree orig_tree01, orig_tree11; |
11494 | enum tree_code code01, code11; |
11495 | |
11496 | tree01 = orig_tree01 = TREE_OPERAND (arg0, 1); |
11497 | tree11 = orig_tree11 = TREE_OPERAND (arg1, 1); |
11498 | STRIP_NOPS (tree01); |
11499 | STRIP_NOPS (tree11); |
11500 | code01 = TREE_CODE (tree01); |
11501 | code11 = TREE_CODE (tree11); |
11502 | if (code11 != MINUS_EXPR |
11503 | && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR)) |
11504 | { |
11505 | std::swap (a&: code0, b&: code1); |
11506 | std::swap (a&: code01, b&: code11); |
11507 | std::swap (a&: tree01, b&: tree11); |
11508 | std::swap (a&: orig_tree01, b&: orig_tree11); |
11509 | } |
11510 | if (code01 == INTEGER_CST |
11511 | && code11 == INTEGER_CST |
11512 | && (wi::to_widest (t: tree01) + wi::to_widest (t: tree11) |
11513 | == element_precision (rtype))) |
11514 | { |
11515 | tem = build2_loc (loc, code: LROTATE_EXPR, |
11516 | type: rtype, TREE_OPERAND (arg0, 0), |
11517 | arg1: code0 == LSHIFT_EXPR |
11518 | ? orig_tree01 : orig_tree11); |
11519 | return fold_convert_loc (loc, type, arg: tem); |
11520 | } |
11521 | else if (code11 == MINUS_EXPR) |
11522 | { |
11523 | tree tree110, tree111; |
11524 | tree110 = TREE_OPERAND (tree11, 0); |
11525 | tree111 = TREE_OPERAND (tree11, 1); |
11526 | STRIP_NOPS (tree110); |
11527 | STRIP_NOPS (tree111); |
11528 | if (TREE_CODE (tree110) == INTEGER_CST |
11529 | && compare_tree_int (tree110, |
11530 | element_precision (rtype)) == 0 |
11531 | && operand_equal_p (arg0: tree01, arg1: tree111, flags: 0)) |
11532 | { |
11533 | tem = build2_loc (loc, code: (code0 == LSHIFT_EXPR |
11534 | ? LROTATE_EXPR : RROTATE_EXPR), |
11535 | type: rtype, TREE_OPERAND (arg0, 0), |
11536 | arg1: orig_tree01); |
11537 | return fold_convert_loc (loc, type, arg: tem); |
11538 | } |
11539 | } |
11540 | else if (code == BIT_IOR_EXPR |
11541 | && code11 == BIT_AND_EXPR |
11542 | && pow2p_hwi (x: element_precision (rtype))) |
11543 | { |
11544 | tree tree110, tree111; |
11545 | tree110 = TREE_OPERAND (tree11, 0); |
11546 | tree111 = TREE_OPERAND (tree11, 1); |
11547 | STRIP_NOPS (tree110); |
11548 | STRIP_NOPS (tree111); |
11549 | if (TREE_CODE (tree110) == NEGATE_EXPR |
11550 | && TREE_CODE (tree111) == INTEGER_CST |
11551 | && compare_tree_int (tree111, |
11552 | element_precision (rtype) - 1) == 0 |
11553 | && operand_equal_p (arg0: tree01, TREE_OPERAND (tree110, 0), flags: 0)) |
11554 | { |
11555 | tem = build2_loc (loc, code: (code0 == LSHIFT_EXPR |
11556 | ? LROTATE_EXPR : RROTATE_EXPR), |
11557 | type: rtype, TREE_OPERAND (arg0, 0), |
11558 | arg1: orig_tree01); |
11559 | return fold_convert_loc (loc, type, arg: tem); |
11560 | } |
11561 | } |
11562 | } |
11563 | } |
11564 | |
11565 | associate: |
11566 | /* In most languages, can't associate operations on floats through |
11567 | parentheses. Rather than remember where the parentheses were, we |
11568 | don't associate floats at all, unless the user has specified |
11569 | -fassociative-math. |
11570 | And, we need to make sure type is not saturating. */ |
11571 | |
11572 | if ((! FLOAT_TYPE_P (type) || flag_associative_math) |
11573 | && !TYPE_SATURATING (type) |
11574 | && !TYPE_OVERFLOW_SANITIZED (type)) |
11575 | { |
11576 | tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0; |
11577 | tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1; |
11578 | tree atype = type; |
11579 | bool ok = true; |
11580 | |
11581 | /* Split both trees into variables, constants, and literals. Then |
11582 | associate each group together, the constants with literals, |
11583 | then the result with variables. This increases the chances of |
11584 | literals being recombined later and of generating relocatable |
11585 | expressions for the sum of a constant and literal. */ |
11586 | var0 = split_tree (in: arg0, type, code, |
11587 | minus_varp: &minus_var0, conp: &con0, minus_conp: &minus_con0, |
11588 | litp: &lit0, minus_litp: &minus_lit0, negate_p: 0); |
11589 | var1 = split_tree (in: arg1, type, code, |
11590 | minus_varp: &minus_var1, conp: &con1, minus_conp: &minus_con1, |
11591 | litp: &lit1, minus_litp: &minus_lit1, negate_p: code == MINUS_EXPR); |
11592 | |
11593 | /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */ |
11594 | if (code == MINUS_EXPR) |
11595 | code = PLUS_EXPR; |
11596 | |
11597 | /* With undefined overflow prefer doing association in a type |
11598 | which wraps on overflow, if that is one of the operand types. */ |
11599 | if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)) |
11600 | && !TYPE_OVERFLOW_WRAPS (type)) |
11601 | { |
11602 | if (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
11603 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))) |
11604 | atype = TREE_TYPE (arg0); |
11605 | else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1)) |
11606 | && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1))) |
11607 | atype = TREE_TYPE (arg1); |
11608 | gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type)); |
11609 | } |
11610 | |
11611 | /* With undefined overflow we can only associate constants with one |
11612 | variable, and constants whose association doesn't overflow. */ |
11613 | if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype)) |
11614 | && !TYPE_OVERFLOW_WRAPS (atype)) |
11615 | { |
11616 | if ((var0 && var1) || (minus_var0 && minus_var1)) |
11617 | { |
11618 | /* ??? If split_tree would handle NEGATE_EXPR we could |
11619 | simply reject these cases and the allowed cases would |
11620 | be the var0/minus_var1 ones. */ |
11621 | tree tmp0 = var0 ? var0 : minus_var0; |
11622 | tree tmp1 = var1 ? var1 : minus_var1; |
11623 | bool one_neg = false; |
11624 | |
11625 | if (TREE_CODE (tmp0) == NEGATE_EXPR) |
11626 | { |
11627 | tmp0 = TREE_OPERAND (tmp0, 0); |
11628 | one_neg = !one_neg; |
11629 | } |
11630 | if (CONVERT_EXPR_P (tmp0) |
11631 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0))) |
11632 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0))) |
11633 | <= TYPE_PRECISION (atype))) |
11634 | tmp0 = TREE_OPERAND (tmp0, 0); |
11635 | if (TREE_CODE (tmp1) == NEGATE_EXPR) |
11636 | { |
11637 | tmp1 = TREE_OPERAND (tmp1, 0); |
11638 | one_neg = !one_neg; |
11639 | } |
11640 | if (CONVERT_EXPR_P (tmp1) |
11641 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0))) |
11642 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0))) |
11643 | <= TYPE_PRECISION (atype))) |
11644 | tmp1 = TREE_OPERAND (tmp1, 0); |
11645 | /* The only case we can still associate with two variables |
11646 | is if they cancel out. */ |
11647 | if (!one_neg |
11648 | || !operand_equal_p (arg0: tmp0, arg1: tmp1, flags: 0)) |
11649 | ok = false; |
11650 | } |
11651 | else if ((var0 && minus_var1 |
11652 | && ! operand_equal_p (arg0: var0, arg1: minus_var1, flags: 0)) |
11653 | || (minus_var0 && var1 |
11654 | && ! operand_equal_p (arg0: minus_var0, arg1: var1, flags: 0))) |
11655 | ok = false; |
11656 | } |
11657 | |
11658 | /* Only do something if we found more than two objects. Otherwise, |
11659 | nothing has changed and we risk infinite recursion. */ |
11660 | if (ok |
11661 | && ((var0 != 0) + (var1 != 0) |
11662 | + (minus_var0 != 0) + (minus_var1 != 0) |
11663 | + (con0 != 0) + (con1 != 0) |
11664 | + (minus_con0 != 0) + (minus_con1 != 0) |
11665 | + (lit0 != 0) + (lit1 != 0) |
11666 | + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2) |
11667 | { |
11668 | var0 = associate_trees (loc, t1: var0, t2: var1, code, type: atype); |
11669 | minus_var0 = associate_trees (loc, t1: minus_var0, t2: minus_var1, |
11670 | code, type: atype); |
11671 | con0 = associate_trees (loc, t1: con0, t2: con1, code, type: atype); |
11672 | minus_con0 = associate_trees (loc, t1: minus_con0, t2: minus_con1, |
11673 | code, type: atype); |
11674 | lit0 = associate_trees (loc, t1: lit0, t2: lit1, code, type: atype); |
11675 | minus_lit0 = associate_trees (loc, t1: minus_lit0, t2: minus_lit1, |
11676 | code, type: atype); |
11677 | |
11678 | if (minus_var0 && var0) |
11679 | { |
11680 | var0 = associate_trees (loc, t1: var0, t2: minus_var0, |
11681 | code: MINUS_EXPR, type: atype); |
11682 | minus_var0 = 0; |
11683 | } |
11684 | if (minus_con0 && con0) |
11685 | { |
11686 | con0 = associate_trees (loc, t1: con0, t2: minus_con0, |
11687 | code: MINUS_EXPR, type: atype); |
11688 | minus_con0 = 0; |
11689 | } |
11690 | |
11691 | /* Preserve the MINUS_EXPR if the negative part of the literal is |
11692 | greater than the positive part. Otherwise, the multiplicative |
11693 | folding code (i.e extract_muldiv) may be fooled in case |
11694 | unsigned constants are subtracted, like in the following |
11695 | example: ((X*2 + 4) - 8U)/2. */ |
11696 | if (minus_lit0 && lit0) |
11697 | { |
11698 | if (TREE_CODE (lit0) == INTEGER_CST |
11699 | && TREE_CODE (minus_lit0) == INTEGER_CST |
11700 | && tree_int_cst_lt (t1: lit0, t2: minus_lit0) |
11701 | /* But avoid ending up with only negated parts. */ |
11702 | && (var0 || con0)) |
11703 | { |
11704 | minus_lit0 = associate_trees (loc, t1: minus_lit0, t2: lit0, |
11705 | code: MINUS_EXPR, type: atype); |
11706 | lit0 = 0; |
11707 | } |
11708 | else |
11709 | { |
11710 | lit0 = associate_trees (loc, t1: lit0, t2: minus_lit0, |
11711 | code: MINUS_EXPR, type: atype); |
11712 | minus_lit0 = 0; |
11713 | } |
11714 | } |
11715 | |
11716 | /* Don't introduce overflows through reassociation. */ |
11717 | if ((lit0 && TREE_OVERFLOW_P (lit0)) |
11718 | || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))) |
11719 | return NULL_TREE; |
11720 | |
11721 | /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */ |
11722 | con0 = associate_trees (loc, t1: con0, t2: lit0, code, type: atype); |
11723 | lit0 = 0; |
11724 | minus_con0 = associate_trees (loc, t1: minus_con0, t2: minus_lit0, |
11725 | code, type: atype); |
11726 | minus_lit0 = 0; |
11727 | |
11728 | /* Eliminate minus_con0. */ |
11729 | if (minus_con0) |
11730 | { |
11731 | if (con0) |
11732 | con0 = associate_trees (loc, t1: con0, t2: minus_con0, |
11733 | code: MINUS_EXPR, type: atype); |
11734 | else if (var0) |
11735 | var0 = associate_trees (loc, t1: var0, t2: minus_con0, |
11736 | code: MINUS_EXPR, type: atype); |
11737 | else |
11738 | gcc_unreachable (); |
11739 | minus_con0 = 0; |
11740 | } |
11741 | |
11742 | /* Eliminate minus_var0. */ |
11743 | if (minus_var0) |
11744 | { |
11745 | if (con0) |
11746 | con0 = associate_trees (loc, t1: con0, t2: minus_var0, |
11747 | code: MINUS_EXPR, type: atype); |
11748 | else |
11749 | gcc_unreachable (); |
11750 | minus_var0 = 0; |
11751 | } |
11752 | |
11753 | return |
11754 | fold_convert_loc (loc, type, arg: associate_trees (loc, t1: var0, t2: con0, |
11755 | code, type: atype)); |
11756 | } |
11757 | } |
11758 | |
11759 | return NULL_TREE; |
11760 | |
11761 | case POINTER_DIFF_EXPR: |
11762 | case MINUS_EXPR: |
11763 | /* Fold &a[i] - &a[j] to i-j. */ |
11764 | if (TREE_CODE (arg0) == ADDR_EXPR |
11765 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF |
11766 | && TREE_CODE (arg1) == ADDR_EXPR |
11767 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF) |
11768 | { |
11769 | tree tem = fold_addr_of_array_ref_difference (loc, type, |
11770 | TREE_OPERAND (arg0, 0), |
11771 | TREE_OPERAND (arg1, 0), |
11772 | use_pointer_diff: code |
11773 | == POINTER_DIFF_EXPR); |
11774 | if (tem) |
11775 | return tem; |
11776 | } |
11777 | |
11778 | /* Further transformations are not for pointers. */ |
11779 | if (code == POINTER_DIFF_EXPR) |
11780 | return NULL_TREE; |
11781 | |
11782 | /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */ |
11783 | if (TREE_CODE (arg0) == NEGATE_EXPR |
11784 | && negate_expr_p (t: op1) |
11785 | /* If arg0 is e.g. unsigned int and type is int, then this could |
11786 | introduce UB, because if A is INT_MIN at runtime, the original |
11787 | expression can be well defined while the latter is not. |
11788 | See PR83269. */ |
11789 | && !(ANY_INTEGRAL_TYPE_P (type) |
11790 | && TYPE_OVERFLOW_UNDEFINED (type) |
11791 | && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
11792 | && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))) |
11793 | return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (t: op1), |
11794 | fold_convert_loc (loc, type, |
11795 | TREE_OPERAND (arg0, 0))); |
11796 | |
11797 | /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to |
11798 | __complex__ ( x, -y ). This is not the same for SNaNs or if |
11799 | signed zeros are involved. */ |
11800 | if (!HONOR_SNANS (arg0) |
11801 | && !HONOR_SIGNED_ZEROS (arg0) |
11802 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))) |
11803 | { |
11804 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); |
11805 | tree arg0r = fold_unary_loc (loc, code: REALPART_EXPR, type: rtype, op0: arg0); |
11806 | tree arg0i = fold_unary_loc (loc, code: IMAGPART_EXPR, type: rtype, op0: arg0); |
11807 | bool arg0rz = false, arg0iz = false; |
11808 | if ((arg0r && (arg0rz = real_zerop (arg0r))) |
11809 | || (arg0i && (arg0iz = real_zerop (arg0i)))) |
11810 | { |
11811 | tree arg1r = fold_unary_loc (loc, code: REALPART_EXPR, type: rtype, op0: arg1); |
11812 | tree arg1i = fold_unary_loc (loc, code: IMAGPART_EXPR, type: rtype, op0: arg1); |
11813 | if (arg0rz && arg1i && real_zerop (arg1i)) |
11814 | { |
11815 | tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
11816 | arg1r ? arg1r |
11817 | : build1 (REALPART_EXPR, rtype, arg1)); |
11818 | tree ip = arg0i ? arg0i |
11819 | : build1 (IMAGPART_EXPR, rtype, arg0); |
11820 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
11821 | } |
11822 | else if (arg0iz && arg1r && real_zerop (arg1r)) |
11823 | { |
11824 | tree rp = arg0r ? arg0r |
11825 | : build1 (REALPART_EXPR, rtype, arg0); |
11826 | tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype, |
11827 | arg1i ? arg1i |
11828 | : build1 (IMAGPART_EXPR, rtype, arg1)); |
11829 | return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip); |
11830 | } |
11831 | } |
11832 | } |
11833 | |
11834 | /* A - B -> A + (-B) if B is easily negatable. */ |
11835 | if (negate_expr_p (t: op1) |
11836 | && ! TYPE_OVERFLOW_SANITIZED (type) |
11837 | && ((FLOAT_TYPE_P (type) |
11838 | /* Avoid this transformation if B is a positive REAL_CST. */ |
11839 | && (TREE_CODE (op1) != REAL_CST |
11840 | || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1)))) |
11841 | || INTEGRAL_TYPE_P (type))) |
11842 | return fold_build2_loc (loc, PLUS_EXPR, type, |
11843 | fold_convert_loc (loc, type, arg: arg0), |
11844 | negate_expr (t: op1)); |
11845 | |
11846 | /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or |
11847 | one. Make sure the type is not saturating and has the signedness of |
11848 | the stripped operands, as fold_plusminus_mult_expr will re-associate. |
11849 | ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */ |
11850 | if ((TREE_CODE (arg0) == MULT_EXPR |
11851 | || TREE_CODE (arg1) == MULT_EXPR) |
11852 | && !TYPE_SATURATING (type) |
11853 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0)) |
11854 | && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1)) |
11855 | && (!FLOAT_TYPE_P (type) || flag_associative_math)) |
11856 | { |
11857 | tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1); |
11858 | if (tem) |
11859 | return tem; |
11860 | } |
11861 | |
11862 | goto associate; |
11863 | |
11864 | case MULT_EXPR: |
11865 | if (! FLOAT_TYPE_P (type)) |
11866 | { |
11867 | /* Transform x * -C into -x * C if x is easily negatable. */ |
11868 | if (TREE_CODE (op1) == INTEGER_CST |
11869 | && tree_int_cst_sgn (op1) == -1 |
11870 | && negate_expr_p (t: op0) |
11871 | && negate_expr_p (t: op1) |
11872 | && (tem = negate_expr (t: op1)) != op1 |
11873 | && ! TREE_OVERFLOW (tem)) |
11874 | return fold_build2_loc (loc, MULT_EXPR, type, |
11875 | fold_convert_loc (loc, type, |
11876 | arg: negate_expr (t: op0)), tem); |
11877 | |
11878 | strict_overflow_p = false; |
11879 | if (TREE_CODE (arg1) == INTEGER_CST |
11880 | && (tem = extract_muldiv (t: op0, c: arg1, code, NULL_TREE, |
11881 | strict_overflow_p: &strict_overflow_p)) != 0) |
11882 | { |
11883 | if (strict_overflow_p) |
11884 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not " |
11885 | "occur when simplifying " |
11886 | "multiplication" ), |
11887 | wc: WARN_STRICT_OVERFLOW_MISC); |
11888 | return fold_convert_loc (loc, type, arg: tem); |
11889 | } |
11890 | |
11891 | /* Optimize z * conj(z) for integer complex numbers. */ |
11892 | if (TREE_CODE (arg0) == CONJ_EXPR |
11893 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0)) |
11894 | return fold_mult_zconjz (loc, type, expr: arg1); |
11895 | if (TREE_CODE (arg1) == CONJ_EXPR |
11896 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
11897 | return fold_mult_zconjz (loc, type, expr: arg0); |
11898 | } |
11899 | else |
11900 | { |
11901 | /* Fold z * +-I to __complex__ (-+__imag z, +-__real z). |
11902 | This is not the same for NaNs or if signed zeros are |
11903 | involved. */ |
11904 | if (!HONOR_NANS (arg0) |
11905 | && !HONOR_SIGNED_ZEROS (arg0) |
11906 | && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)) |
11907 | && TREE_CODE (arg1) == COMPLEX_CST |
11908 | && real_zerop (TREE_REALPART (arg1))) |
11909 | { |
11910 | tree rtype = TREE_TYPE (TREE_TYPE (arg0)); |
11911 | if (real_onep (TREE_IMAGPART (arg1))) |
11912 | return |
11913 | fold_build2_loc (loc, COMPLEX_EXPR, type, |
11914 | negate_expr (t: fold_build1_loc (loc, IMAGPART_EXPR, |
11915 | rtype, arg0)), |
11916 | fold_build1_loc (loc, REALPART_EXPR, rtype, arg0)); |
11917 | else if (real_minus_onep (TREE_IMAGPART (arg1))) |
11918 | return |
11919 | fold_build2_loc (loc, COMPLEX_EXPR, type, |
11920 | fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0), |
11921 | negate_expr (t: fold_build1_loc (loc, REALPART_EXPR, |
11922 | rtype, arg0))); |
11923 | } |
11924 | |
11925 | /* Optimize z * conj(z) for floating point complex numbers. |
11926 | Guarded by flag_unsafe_math_optimizations as non-finite |
11927 | imaginary components don't produce scalar results. */ |
11928 | if (flag_unsafe_math_optimizations |
11929 | && TREE_CODE (arg0) == CONJ_EXPR |
11930 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0)) |
11931 | return fold_mult_zconjz (loc, type, expr: arg1); |
11932 | if (flag_unsafe_math_optimizations |
11933 | && TREE_CODE (arg1) == CONJ_EXPR |
11934 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
11935 | return fold_mult_zconjz (loc, type, expr: arg0); |
11936 | } |
11937 | goto associate; |
11938 | |
11939 | case BIT_IOR_EXPR: |
11940 | /* Canonicalize (X & C1) | C2. */ |
11941 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
11942 | && TREE_CODE (arg1) == INTEGER_CST |
11943 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
11944 | { |
11945 | int width = TYPE_PRECISION (type), w; |
11946 | wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1)); |
11947 | wide_int c2 = wi::to_wide (t: arg1); |
11948 | |
11949 | /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */ |
11950 | if ((c1 & c2) == c1) |
11951 | return omit_one_operand_loc (loc, type, result: arg1, |
11952 | TREE_OPERAND (arg0, 0)); |
11953 | |
11954 | wide_int msk = wi::mask (width, negate_p: false, |
11955 | TYPE_PRECISION (TREE_TYPE (arg1))); |
11956 | |
11957 | /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */ |
11958 | if (wi::bit_and_not (x: msk, y: c1 | c2) == 0) |
11959 | { |
11960 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
11961 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1); |
11962 | } |
11963 | |
11964 | /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2, |
11965 | unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some |
11966 | mode which allows further optimizations. */ |
11967 | c1 &= msk; |
11968 | c2 &= msk; |
11969 | wide_int c3 = wi::bit_and_not (x: c1, y: c2); |
11970 | for (w = BITS_PER_UNIT; w <= width; w <<= 1) |
11971 | { |
11972 | wide_int mask = wi::mask (width: w, negate_p: false, |
11973 | TYPE_PRECISION (type)); |
11974 | if (((c1 | c2) & mask) == mask |
11975 | && wi::bit_and_not (x: c1, y: mask) == 0) |
11976 | { |
11977 | c3 = mask; |
11978 | break; |
11979 | } |
11980 | } |
11981 | |
11982 | if (c3 != c1) |
11983 | { |
11984 | tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
11985 | tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem, |
11986 | wide_int_to_tree (type, cst: c3)); |
11987 | return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1); |
11988 | } |
11989 | } |
11990 | |
11991 | /* See if this can be simplified into a rotate first. If that |
11992 | is unsuccessful continue in the association code. */ |
11993 | goto bit_rotate; |
11994 | |
11995 | case BIT_XOR_EXPR: |
11996 | /* Fold (X & 1) ^ 1 as (X & 1) == 0. */ |
11997 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
11998 | && INTEGRAL_TYPE_P (type) |
11999 | && integer_onep (TREE_OPERAND (arg0, 1)) |
12000 | && integer_onep (arg1)) |
12001 | return fold_build2_loc (loc, EQ_EXPR, type, arg0, |
12002 | build_zero_cst (TREE_TYPE (arg0))); |
12003 | |
12004 | /* See if this can be simplified into a rotate first. If that |
12005 | is unsuccessful continue in the association code. */ |
12006 | goto bit_rotate; |
12007 | |
12008 | case BIT_AND_EXPR: |
12009 | /* Fold (X ^ 1) & 1 as (X & 1) == 0. */ |
12010 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
12011 | && INTEGRAL_TYPE_P (type) |
12012 | && integer_onep (TREE_OPERAND (arg0, 1)) |
12013 | && integer_onep (arg1)) |
12014 | { |
12015 | tree tem2; |
12016 | tem = TREE_OPERAND (arg0, 0); |
12017 | tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg: arg1); |
12018 | tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), |
12019 | tem, tem2); |
12020 | return fold_build2_loc (loc, EQ_EXPR, type, tem2, |
12021 | build_zero_cst (TREE_TYPE (tem))); |
12022 | } |
12023 | /* Fold ~X & 1 as (X & 1) == 0. */ |
12024 | if (TREE_CODE (arg0) == BIT_NOT_EXPR |
12025 | && INTEGRAL_TYPE_P (type) |
12026 | && integer_onep (arg1)) |
12027 | { |
12028 | tree tem2; |
12029 | tem = TREE_OPERAND (arg0, 0); |
12030 | tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg: arg1); |
12031 | tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), |
12032 | tem, tem2); |
12033 | return fold_build2_loc (loc, EQ_EXPR, type, tem2, |
12034 | build_zero_cst (TREE_TYPE (tem))); |
12035 | } |
12036 | /* Fold !X & 1 as X == 0. */ |
12037 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR |
12038 | && integer_onep (arg1)) |
12039 | { |
12040 | tem = TREE_OPERAND (arg0, 0); |
12041 | return fold_build2_loc (loc, EQ_EXPR, type, tem, |
12042 | build_zero_cst (TREE_TYPE (tem))); |
12043 | } |
12044 | |
12045 | /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant |
12046 | multiple of 1 << CST. */ |
12047 | if (TREE_CODE (arg1) == INTEGER_CST) |
12048 | { |
12049 | wi::tree_to_wide_ref cst1 = wi::to_wide (t: arg1); |
12050 | wide_int ncst1 = -cst1; |
12051 | if ((cst1 & ncst1) == ncst1 |
12052 | && multiple_of_p (type, arg0, |
12053 | wide_int_to_tree (TREE_TYPE (arg1), cst: ncst1))) |
12054 | return fold_convert_loc (loc, type, arg: arg0); |
12055 | } |
12056 | |
12057 | /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero |
12058 | bits from CST2. */ |
12059 | if (TREE_CODE (arg1) == INTEGER_CST |
12060 | && TREE_CODE (arg0) == MULT_EXPR |
12061 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
12062 | { |
12063 | wi::tree_to_wide_ref warg1 = wi::to_wide (t: arg1); |
12064 | wide_int masked |
12065 | = mask_with_tz (type, x: warg1, y: wi::to_wide (TREE_OPERAND (arg0, 1))); |
12066 | |
12067 | if (masked == 0) |
12068 | return omit_two_operands_loc (loc, type, result: build_zero_cst (type), |
12069 | omitted1: arg0, omitted2: arg1); |
12070 | else if (masked != warg1) |
12071 | { |
12072 | /* Avoid the transform if arg1 is a mask of some |
12073 | mode which allows further optimizations. */ |
12074 | int pop = wi::popcount (warg1); |
12075 | if (!(pop >= BITS_PER_UNIT |
12076 | && pow2p_hwi (x: pop) |
12077 | && wi::mask (width: pop, negate_p: false, precision: warg1.get_precision ()) == warg1)) |
12078 | return fold_build2_loc (loc, code, type, op0, |
12079 | wide_int_to_tree (type, cst: masked)); |
12080 | } |
12081 | } |
12082 | |
12083 | /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */ |
12084 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR |
12085 | && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) |
12086 | { |
12087 | prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0))); |
12088 | |
12089 | wide_int mask = wide_int::from (x: wi::to_wide (t: arg1), precision: prec, sgn: UNSIGNED); |
12090 | if (mask == -1) |
12091 | return |
12092 | fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
12093 | } |
12094 | |
12095 | goto associate; |
12096 | |
12097 | case RDIV_EXPR: |
12098 | /* Don't touch a floating-point divide by zero unless the mode |
12099 | of the constant can represent infinity. */ |
12100 | if (TREE_CODE (arg1) == REAL_CST |
12101 | && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))) |
12102 | && real_zerop (arg1)) |
12103 | return NULL_TREE; |
12104 | |
12105 | /* (-A) / (-B) -> A / B */ |
12106 | if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (t: arg1)) |
12107 | return fold_build2_loc (loc, RDIV_EXPR, type, |
12108 | TREE_OPERAND (arg0, 0), |
12109 | negate_expr (t: arg1)); |
12110 | if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (t: arg0)) |
12111 | return fold_build2_loc (loc, RDIV_EXPR, type, |
12112 | negate_expr (t: arg0), |
12113 | TREE_OPERAND (arg1, 0)); |
12114 | return NULL_TREE; |
12115 | |
12116 | case TRUNC_DIV_EXPR: |
12117 | /* Fall through */ |
12118 | |
12119 | case FLOOR_DIV_EXPR: |
12120 | /* Simplify A / (B << N) where A and B are positive and B is |
12121 | a power of 2, to A >> (N + log2(B)). */ |
12122 | strict_overflow_p = false; |
12123 | if (TREE_CODE (arg1) == LSHIFT_EXPR |
12124 | && (TYPE_UNSIGNED (type) |
12125 | || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) |
12126 | { |
12127 | tree sval = TREE_OPERAND (arg1, 0); |
12128 | if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) |
12129 | { |
12130 | tree sh_cnt = TREE_OPERAND (arg1, 1); |
12131 | tree pow2 = build_int_cst (TREE_TYPE (sh_cnt), |
12132 | wi::exact_log2 (wi::to_wide (t: sval))); |
12133 | |
12134 | if (strict_overflow_p) |
12135 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not " |
12136 | "occur when simplifying A / (B << N)" ), |
12137 | wc: WARN_STRICT_OVERFLOW_MISC); |
12138 | |
12139 | sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt), |
12140 | sh_cnt, pow2); |
12141 | return fold_build2_loc (loc, RSHIFT_EXPR, type, |
12142 | fold_convert_loc (loc, type, arg: arg0), sh_cnt); |
12143 | } |
12144 | } |
12145 | |
12146 | /* Fall through */ |
12147 | |
12148 | case ROUND_DIV_EXPR: |
12149 | case CEIL_DIV_EXPR: |
12150 | case EXACT_DIV_EXPR: |
12151 | if (integer_zerop (arg1)) |
12152 | return NULL_TREE; |
12153 | |
12154 | /* Convert -A / -B to A / B when the type is signed and overflow is |
12155 | undefined. */ |
12156 | if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
12157 | && TREE_CODE (op0) == NEGATE_EXPR |
12158 | && negate_expr_p (t: op1)) |
12159 | { |
12160 | if (ANY_INTEGRAL_TYPE_P (type)) |
12161 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12162 | "when distributing negation across " |
12163 | "division" ), |
12164 | wc: WARN_STRICT_OVERFLOW_MISC); |
12165 | return fold_build2_loc (loc, code, type, |
12166 | fold_convert_loc (loc, type, |
12167 | TREE_OPERAND (arg0, 0)), |
12168 | negate_expr (t: op1)); |
12169 | } |
12170 | if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
12171 | && TREE_CODE (arg1) == NEGATE_EXPR |
12172 | && negate_expr_p (t: op0)) |
12173 | { |
12174 | if (ANY_INTEGRAL_TYPE_P (type)) |
12175 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12176 | "when distributing negation across " |
12177 | "division" ), |
12178 | wc: WARN_STRICT_OVERFLOW_MISC); |
12179 | return fold_build2_loc (loc, code, type, |
12180 | negate_expr (t: op0), |
12181 | fold_convert_loc (loc, type, |
12182 | TREE_OPERAND (arg1, 0))); |
12183 | } |
12184 | |
12185 | /* If arg0 is a multiple of arg1, then rewrite to the fastest div |
12186 | operation, EXACT_DIV_EXPR. |
12187 | |
12188 | Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now. |
12189 | At one time others generated faster code, it's not clear if they do |
12190 | after the last round to changes to the DIV code in expmed.cc. */ |
12191 | if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR) |
12192 | && multiple_of_p (type, arg0, arg1)) |
12193 | return fold_build2_loc (loc, EXACT_DIV_EXPR, type, |
12194 | fold_convert (type, arg0), |
12195 | fold_convert (type, arg1)); |
12196 | |
12197 | strict_overflow_p = false; |
12198 | if (TREE_CODE (arg1) == INTEGER_CST |
12199 | && (tem = extract_muldiv (t: op0, c: arg1, code, NULL_TREE, |
12200 | strict_overflow_p: &strict_overflow_p)) != 0) |
12201 | { |
12202 | if (strict_overflow_p) |
12203 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12204 | "when simplifying division" ), |
12205 | wc: WARN_STRICT_OVERFLOW_MISC); |
12206 | return fold_convert_loc (loc, type, arg: tem); |
12207 | } |
12208 | |
12209 | return NULL_TREE; |
12210 | |
12211 | case CEIL_MOD_EXPR: |
12212 | case FLOOR_MOD_EXPR: |
12213 | case ROUND_MOD_EXPR: |
12214 | case TRUNC_MOD_EXPR: |
12215 | strict_overflow_p = false; |
12216 | if (TREE_CODE (arg1) == INTEGER_CST |
12217 | && (tem = extract_muldiv (t: op0, c: arg1, code, NULL_TREE, |
12218 | strict_overflow_p: &strict_overflow_p)) != 0) |
12219 | { |
12220 | if (strict_overflow_p) |
12221 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12222 | "when simplifying modulus" ), |
12223 | wc: WARN_STRICT_OVERFLOW_MISC); |
12224 | return fold_convert_loc (loc, type, arg: tem); |
12225 | } |
12226 | |
12227 | return NULL_TREE; |
12228 | |
12229 | case LROTATE_EXPR: |
12230 | case RROTATE_EXPR: |
12231 | case RSHIFT_EXPR: |
12232 | case LSHIFT_EXPR: |
12233 | /* Since negative shift count is not well-defined, |
12234 | don't try to compute it in the compiler. */ |
12235 | if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0) |
12236 | return NULL_TREE; |
12237 | |
12238 | prec = element_precision (type); |
12239 | |
12240 | /* If we have a rotate of a bit operation with the rotate count and |
12241 | the second operand of the bit operation both constant, |
12242 | permute the two operations. */ |
12243 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST |
12244 | && (TREE_CODE (arg0) == BIT_AND_EXPR |
12245 | || TREE_CODE (arg0) == BIT_IOR_EXPR |
12246 | || TREE_CODE (arg0) == BIT_XOR_EXPR) |
12247 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
12248 | { |
12249 | tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
12250 | tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
12251 | return fold_build2_loc (loc, TREE_CODE (arg0), type, |
12252 | fold_build2_loc (loc, code, type, |
12253 | arg00, arg1), |
12254 | fold_build2_loc (loc, code, type, |
12255 | arg01, arg1)); |
12256 | } |
12257 | |
12258 | /* Two consecutive rotates adding up to the some integer |
12259 | multiple of the precision of the type can be ignored. */ |
12260 | if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST |
12261 | && TREE_CODE (arg0) == RROTATE_EXPR |
12262 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST |
12263 | && wi::umod_trunc (x: wi::to_wide (t: arg1) |
12264 | + wi::to_wide (TREE_OPERAND (arg0, 1)), |
12265 | y: prec) == 0) |
12266 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
12267 | |
12268 | return NULL_TREE; |
12269 | |
12270 | case MIN_EXPR: |
12271 | case MAX_EXPR: |
12272 | goto associate; |
12273 | |
12274 | case TRUTH_ANDIF_EXPR: |
12275 | /* Note that the operands of this must be ints |
12276 | and their values must be 0 or 1. |
12277 | ("true" is a fixed value perhaps depending on the language.) */ |
12278 | /* If first arg is constant zero, return it. */ |
12279 | if (integer_zerop (arg0)) |
12280 | return fold_convert_loc (loc, type, arg: arg0); |
12281 | /* FALLTHRU */ |
12282 | case TRUTH_AND_EXPR: |
12283 | /* If either arg is constant true, drop it. */ |
12284 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) |
12285 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg1)); |
12286 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1) |
12287 | /* Preserve sequence points. */ |
12288 | && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) |
12289 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg0)); |
12290 | /* If second arg is constant zero, result is zero, but first arg |
12291 | must be evaluated. */ |
12292 | if (integer_zerop (arg1)) |
12293 | return omit_one_operand_loc (loc, type, result: arg1, omitted: arg0); |
12294 | /* Likewise for first arg, but note that only the TRUTH_AND_EXPR |
12295 | case will be handled here. */ |
12296 | if (integer_zerop (arg0)) |
12297 | return omit_one_operand_loc (loc, type, result: arg0, omitted: arg1); |
12298 | |
12299 | /* !X && X is always false. */ |
12300 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR |
12301 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0)) |
12302 | return omit_one_operand_loc (loc, type, integer_zero_node, omitted: arg1); |
12303 | /* X && !X is always false. */ |
12304 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR |
12305 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
12306 | return omit_one_operand_loc (loc, type, integer_zero_node, omitted: arg0); |
12307 | |
12308 | /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y |
12309 | means A >= Y && A != MAX, but in this case we know that |
12310 | A < X <= MAX. */ |
12311 | |
12312 | if (!TREE_SIDE_EFFECTS (arg0) |
12313 | && !TREE_SIDE_EFFECTS (arg1)) |
12314 | { |
12315 | tem = fold_to_nonsharp_ineq_using_bound (loc, ineq: arg0, bound: arg1); |
12316 | if (tem && !operand_equal_p (arg0: tem, arg1: arg0, flags: 0)) |
12317 | return fold_convert (type, |
12318 | fold_build2_loc (loc, code, TREE_TYPE (arg1), |
12319 | tem, arg1)); |
12320 | |
12321 | tem = fold_to_nonsharp_ineq_using_bound (loc, ineq: arg1, bound: arg0); |
12322 | if (tem && !operand_equal_p (arg0: tem, arg1, flags: 0)) |
12323 | return fold_convert (type, |
12324 | fold_build2_loc (loc, code, TREE_TYPE (arg0), |
12325 | arg0, tem)); |
12326 | } |
12327 | |
12328 | if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1)) |
12329 | != NULL_TREE) |
12330 | return tem; |
12331 | |
12332 | return NULL_TREE; |
12333 | |
12334 | case TRUTH_ORIF_EXPR: |
12335 | /* Note that the operands of this must be ints |
12336 | and their values must be 0 or true. |
12337 | ("true" is a fixed value perhaps depending on the language.) */ |
12338 | /* If first arg is constant true, return it. */ |
12339 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) |
12340 | return fold_convert_loc (loc, type, arg: arg0); |
12341 | /* FALLTHRU */ |
12342 | case TRUTH_OR_EXPR: |
12343 | /* If either arg is constant zero, drop it. */ |
12344 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) |
12345 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg1)); |
12346 | if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1) |
12347 | /* Preserve sequence points. */ |
12348 | && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0))) |
12349 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg0)); |
12350 | /* If second arg is constant true, result is true, but we must |
12351 | evaluate first arg. */ |
12352 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) |
12353 | return omit_one_operand_loc (loc, type, result: arg1, omitted: arg0); |
12354 | /* Likewise for first arg, but note this only occurs here for |
12355 | TRUTH_OR_EXPR. */ |
12356 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) |
12357 | return omit_one_operand_loc (loc, type, result: arg0, omitted: arg1); |
12358 | |
12359 | /* !X || X is always true. */ |
12360 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR |
12361 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0)) |
12362 | return omit_one_operand_loc (loc, type, integer_one_node, omitted: arg1); |
12363 | /* X || !X is always true. */ |
12364 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR |
12365 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
12366 | return omit_one_operand_loc (loc, type, integer_one_node, omitted: arg0); |
12367 | |
12368 | /* (X && !Y) || (!X && Y) is X ^ Y */ |
12369 | if (TREE_CODE (arg0) == TRUTH_AND_EXPR |
12370 | && TREE_CODE (arg1) == TRUTH_AND_EXPR) |
12371 | { |
12372 | tree a0, a1, l0, l1, n0, n1; |
12373 | |
12374 | a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0)); |
12375 | a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1)); |
12376 | |
12377 | l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
12378 | l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1)); |
12379 | |
12380 | n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0); |
12381 | n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1); |
12382 | |
12383 | if ((operand_equal_p (arg0: n0, arg1: a0, flags: 0) |
12384 | && operand_equal_p (arg0: n1, arg1: a1, flags: 0)) |
12385 | || (operand_equal_p (arg0: n0, arg1: a1, flags: 0) |
12386 | && operand_equal_p (arg0: n1, arg1: a0, flags: 0))) |
12387 | return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1); |
12388 | } |
12389 | |
12390 | if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1)) |
12391 | != NULL_TREE) |
12392 | return tem; |
12393 | |
12394 | return NULL_TREE; |
12395 | |
12396 | case TRUTH_XOR_EXPR: |
12397 | /* If the second arg is constant zero, drop it. */ |
12398 | if (integer_zerop (arg1)) |
12399 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg0)); |
12400 | /* If the second arg is constant true, this is a logical inversion. */ |
12401 | if (integer_onep (arg1)) |
12402 | { |
12403 | tem = invert_truthvalue_loc (loc, arg: arg0); |
12404 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: tem)); |
12405 | } |
12406 | /* Identical arguments cancel to zero. */ |
12407 | if (operand_equal_p (arg0, arg1, flags: 0)) |
12408 | return omit_one_operand_loc (loc, type, integer_zero_node, omitted: arg0); |
12409 | |
12410 | /* !X ^ X is always true. */ |
12411 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR |
12412 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0)) |
12413 | return omit_one_operand_loc (loc, type, integer_one_node, omitted: arg1); |
12414 | |
12415 | /* X ^ !X is always true. */ |
12416 | if (TREE_CODE (arg1) == TRUTH_NOT_EXPR |
12417 | && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), flags: 0)) |
12418 | return omit_one_operand_loc (loc, type, integer_one_node, omitted: arg0); |
12419 | |
12420 | return NULL_TREE; |
12421 | |
12422 | case EQ_EXPR: |
12423 | case NE_EXPR: |
12424 | STRIP_NOPS (arg0); |
12425 | STRIP_NOPS (arg1); |
12426 | |
12427 | tem = fold_comparison (loc, code, type, op0, op1); |
12428 | if (tem != NULL_TREE) |
12429 | return tem; |
12430 | |
12431 | /* bool_var != 1 becomes !bool_var. */ |
12432 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1) |
12433 | && code == NE_EXPR) |
12434 | return fold_convert_loc (loc, type, |
12435 | arg: fold_build1_loc (loc, TRUTH_NOT_EXPR, |
12436 | TREE_TYPE (arg0), arg0)); |
12437 | |
12438 | /* bool_var == 0 becomes !bool_var. */ |
12439 | if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1) |
12440 | && code == EQ_EXPR) |
12441 | return fold_convert_loc (loc, type, |
12442 | arg: fold_build1_loc (loc, TRUTH_NOT_EXPR, |
12443 | TREE_TYPE (arg0), arg0)); |
12444 | |
12445 | /* !exp != 0 becomes !exp */ |
12446 | if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1) |
12447 | && code == NE_EXPR) |
12448 | return non_lvalue_loc (loc, x: fold_convert_loc (loc, type, arg: arg0)); |
12449 | |
12450 | /* If this is an EQ or NE comparison with zero and ARG0 is |
12451 | (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require |
12452 | two operations, but the latter can be done in one less insn |
12453 | on machines that have only two-operand insns or on which a |
12454 | constant cannot be the first operand. */ |
12455 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
12456 | && integer_zerop (arg1)) |
12457 | { |
12458 | tree arg00 = TREE_OPERAND (arg0, 0); |
12459 | tree arg01 = TREE_OPERAND (arg0, 1); |
12460 | if (TREE_CODE (arg00) == LSHIFT_EXPR |
12461 | && integer_onep (TREE_OPERAND (arg00, 0))) |
12462 | { |
12463 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00), |
12464 | arg01, TREE_OPERAND (arg00, 1)); |
12465 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
12466 | build_one_cst (TREE_TYPE (arg0))); |
12467 | return fold_build2_loc (loc, code, type, |
12468 | fold_convert_loc (loc, TREE_TYPE (arg1), |
12469 | arg: tem), arg1); |
12470 | } |
12471 | else if (TREE_CODE (arg01) == LSHIFT_EXPR |
12472 | && integer_onep (TREE_OPERAND (arg01, 0))) |
12473 | { |
12474 | tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01), |
12475 | arg00, TREE_OPERAND (arg01, 1)); |
12476 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem, |
12477 | build_one_cst (TREE_TYPE (arg0))); |
12478 | return fold_build2_loc (loc, code, type, |
12479 | fold_convert_loc (loc, TREE_TYPE (arg1), |
12480 | arg: tem), arg1); |
12481 | } |
12482 | } |
12483 | |
12484 | /* If this is a comparison of a field, we may be able to simplify it. */ |
12485 | if ((TREE_CODE (arg0) == COMPONENT_REF |
12486 | || TREE_CODE (arg0) == BIT_FIELD_REF) |
12487 | /* Handle the constant case even without -O |
12488 | to make sure the warnings are given. */ |
12489 | && (optimize || TREE_CODE (arg1) == INTEGER_CST)) |
12490 | { |
12491 | t1 = optimize_bit_field_compare (loc, code, compare_type: type, lhs: arg0, rhs: arg1); |
12492 | if (t1) |
12493 | return t1; |
12494 | } |
12495 | |
12496 | /* Optimize comparisons of strlen vs zero to a compare of the |
12497 | first character of the string vs zero. To wit, |
12498 | strlen(ptr) == 0 => *ptr == 0 |
12499 | strlen(ptr) != 0 => *ptr != 0 |
12500 | Other cases should reduce to one of these two (or a constant) |
12501 | due to the return value of strlen being unsigned. */ |
12502 | if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1)) |
12503 | { |
12504 | tree fndecl = get_callee_fndecl (arg0); |
12505 | |
12506 | if (fndecl |
12507 | && fndecl_built_in_p (node: fndecl, name1: BUILT_IN_STRLEN) |
12508 | && call_expr_nargs (arg0) == 1 |
12509 | && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) |
12510 | == POINTER_TYPE)) |
12511 | { |
12512 | tree ptrtype |
12513 | = build_pointer_type (build_qualified_type (char_type_node, |
12514 | TYPE_QUAL_CONST)); |
12515 | tree ptr = fold_convert_loc (loc, type: ptrtype, |
12516 | CALL_EXPR_ARG (arg0, 0)); |
12517 | tree iref = build_fold_indirect_ref_loc (loc, ptr); |
12518 | return fold_build2_loc (loc, code, type, iref, |
12519 | build_int_cst (TREE_TYPE (iref), 0)); |
12520 | } |
12521 | } |
12522 | |
12523 | /* Fold (X >> C) != 0 into X < 0 if C is one less than the width |
12524 | of X. Similarly fold (X >> C) == 0 into X >= 0. */ |
12525 | if (TREE_CODE (arg0) == RSHIFT_EXPR |
12526 | && integer_zerop (arg1) |
12527 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST) |
12528 | { |
12529 | tree arg00 = TREE_OPERAND (arg0, 0); |
12530 | tree arg01 = TREE_OPERAND (arg0, 1); |
12531 | tree itype = TREE_TYPE (arg00); |
12532 | if (wi::to_wide (t: arg01) == element_precision (itype) - 1) |
12533 | { |
12534 | if (TYPE_UNSIGNED (itype)) |
12535 | { |
12536 | itype = signed_type_for (itype); |
12537 | arg00 = fold_convert_loc (loc, type: itype, arg: arg00); |
12538 | } |
12539 | return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, |
12540 | type, arg00, build_zero_cst (itype)); |
12541 | } |
12542 | } |
12543 | |
12544 | /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into |
12545 | (X & C) == 0 when C is a single bit. */ |
12546 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
12547 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR |
12548 | && integer_zerop (arg1) |
12549 | && integer_pow2p (TREE_OPERAND (arg0, 1))) |
12550 | { |
12551 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), |
12552 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), |
12553 | TREE_OPERAND (arg0, 1)); |
12554 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, |
12555 | type, tem, |
12556 | fold_convert_loc (loc, TREE_TYPE (arg0), |
12557 | arg: arg1)); |
12558 | } |
12559 | |
12560 | /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the |
12561 | constant C is a power of two, i.e. a single bit. */ |
12562 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
12563 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR |
12564 | && integer_zerop (arg1) |
12565 | && integer_pow2p (TREE_OPERAND (arg0, 1)) |
12566 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), |
12567 | TREE_OPERAND (arg0, 1), flags: OEP_ONLY_CONST)) |
12568 | { |
12569 | tree arg00 = TREE_OPERAND (arg0, 0); |
12570 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
12571 | arg00, build_int_cst (TREE_TYPE (arg00), 0)); |
12572 | } |
12573 | |
12574 | /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0, |
12575 | when is C is a power of two, i.e. a single bit. */ |
12576 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
12577 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR |
12578 | && integer_zerop (arg1) |
12579 | && integer_pow2p (TREE_OPERAND (arg0, 1)) |
12580 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), |
12581 | TREE_OPERAND (arg0, 1), flags: OEP_ONLY_CONST)) |
12582 | { |
12583 | tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0); |
12584 | tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000), |
12585 | arg000, TREE_OPERAND (arg0, 1)); |
12586 | return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, |
12587 | tem, build_int_cst (TREE_TYPE (tem), 0)); |
12588 | } |
12589 | |
12590 | if (TREE_CODE (arg0) == BIT_XOR_EXPR |
12591 | && TREE_CODE (arg1) == BIT_XOR_EXPR) |
12592 | { |
12593 | tree arg00 = TREE_OPERAND (arg0, 0); |
12594 | tree arg01 = TREE_OPERAND (arg0, 1); |
12595 | tree arg10 = TREE_OPERAND (arg1, 0); |
12596 | tree arg11 = TREE_OPERAND (arg1, 1); |
12597 | tree itype = TREE_TYPE (arg0); |
12598 | |
12599 | /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries. |
12600 | operand_equal_p guarantees no side-effects so we don't need |
12601 | to use omit_one_operand on Z. */ |
12602 | if (operand_equal_p (arg0: arg01, arg1: arg11, flags: 0)) |
12603 | return fold_build2_loc (loc, code, type, arg00, |
12604 | fold_convert_loc (loc, TREE_TYPE (arg00), |
12605 | arg: arg10)); |
12606 | if (operand_equal_p (arg0: arg01, arg1: arg10, flags: 0)) |
12607 | return fold_build2_loc (loc, code, type, arg00, |
12608 | fold_convert_loc (loc, TREE_TYPE (arg00), |
12609 | arg: arg11)); |
12610 | if (operand_equal_p (arg0: arg00, arg1: arg11, flags: 0)) |
12611 | return fold_build2_loc (loc, code, type, arg01, |
12612 | fold_convert_loc (loc, TREE_TYPE (arg01), |
12613 | arg: arg10)); |
12614 | if (operand_equal_p (arg0: arg00, arg1: arg10, flags: 0)) |
12615 | return fold_build2_loc (loc, code, type, arg01, |
12616 | fold_convert_loc (loc, TREE_TYPE (arg01), |
12617 | arg: arg11)); |
12618 | |
12619 | /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */ |
12620 | if (TREE_CODE (arg01) == INTEGER_CST |
12621 | && TREE_CODE (arg11) == INTEGER_CST) |
12622 | { |
12623 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, |
12624 | fold_convert_loc (loc, type: itype, arg: arg11)); |
12625 | tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem); |
12626 | return fold_build2_loc (loc, code, type, tem, |
12627 | fold_convert_loc (loc, type: itype, arg: arg10)); |
12628 | } |
12629 | } |
12630 | |
12631 | /* Attempt to simplify equality/inequality comparisons of complex |
12632 | values. Only lower the comparison if the result is known or |
12633 | can be simplified to a single scalar comparison. */ |
12634 | if ((TREE_CODE (arg0) == COMPLEX_EXPR |
12635 | || TREE_CODE (arg0) == COMPLEX_CST) |
12636 | && (TREE_CODE (arg1) == COMPLEX_EXPR |
12637 | || TREE_CODE (arg1) == COMPLEX_CST)) |
12638 | { |
12639 | tree real0, imag0, real1, imag1; |
12640 | tree rcond, icond; |
12641 | |
12642 | if (TREE_CODE (arg0) == COMPLEX_EXPR) |
12643 | { |
12644 | real0 = TREE_OPERAND (arg0, 0); |
12645 | imag0 = TREE_OPERAND (arg0, 1); |
12646 | } |
12647 | else |
12648 | { |
12649 | real0 = TREE_REALPART (arg0); |
12650 | imag0 = TREE_IMAGPART (arg0); |
12651 | } |
12652 | |
12653 | if (TREE_CODE (arg1) == COMPLEX_EXPR) |
12654 | { |
12655 | real1 = TREE_OPERAND (arg1, 0); |
12656 | imag1 = TREE_OPERAND (arg1, 1); |
12657 | } |
12658 | else |
12659 | { |
12660 | real1 = TREE_REALPART (arg1); |
12661 | imag1 = TREE_IMAGPART (arg1); |
12662 | } |
12663 | |
12664 | rcond = fold_binary_loc (loc, code, type, op0: real0, op1: real1); |
12665 | if (rcond && TREE_CODE (rcond) == INTEGER_CST) |
12666 | { |
12667 | if (integer_zerop (rcond)) |
12668 | { |
12669 | if (code == EQ_EXPR) |
12670 | return omit_two_operands_loc (loc, type, boolean_false_node, |
12671 | omitted1: imag0, omitted2: imag1); |
12672 | return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1); |
12673 | } |
12674 | else |
12675 | { |
12676 | if (code == NE_EXPR) |
12677 | return omit_two_operands_loc (loc, type, boolean_true_node, |
12678 | omitted1: imag0, omitted2: imag1); |
12679 | return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1); |
12680 | } |
12681 | } |
12682 | |
12683 | icond = fold_binary_loc (loc, code, type, op0: imag0, op1: imag1); |
12684 | if (icond && TREE_CODE (icond) == INTEGER_CST) |
12685 | { |
12686 | if (integer_zerop (icond)) |
12687 | { |
12688 | if (code == EQ_EXPR) |
12689 | return omit_two_operands_loc (loc, type, boolean_false_node, |
12690 | omitted1: real0, omitted2: real1); |
12691 | return fold_build2_loc (loc, NE_EXPR, type, real0, real1); |
12692 | } |
12693 | else |
12694 | { |
12695 | if (code == NE_EXPR) |
12696 | return omit_two_operands_loc (loc, type, boolean_true_node, |
12697 | omitted1: real0, omitted2: real1); |
12698 | return fold_build2_loc (loc, EQ_EXPR, type, real0, real1); |
12699 | } |
12700 | } |
12701 | } |
12702 | |
12703 | return NULL_TREE; |
12704 | |
12705 | case LT_EXPR: |
12706 | case GT_EXPR: |
12707 | case LE_EXPR: |
12708 | case GE_EXPR: |
12709 | tem = fold_comparison (loc, code, type, op0, op1); |
12710 | if (tem != NULL_TREE) |
12711 | return tem; |
12712 | |
12713 | /* Transform comparisons of the form X +- C CMP X. */ |
12714 | if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR) |
12715 | && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, flags: 0) |
12716 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST |
12717 | && !HONOR_SNANS (arg0)) |
12718 | { |
12719 | tree arg01 = TREE_OPERAND (arg0, 1); |
12720 | enum tree_code code0 = TREE_CODE (arg0); |
12721 | int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1; |
12722 | |
12723 | /* (X - c) > X becomes false. */ |
12724 | if (code == GT_EXPR |
12725 | && ((code0 == MINUS_EXPR && is_positive >= 0) |
12726 | || (code0 == PLUS_EXPR && is_positive <= 0))) |
12727 | return constant_boolean_node (value: 0, type); |
12728 | |
12729 | /* Likewise (X + c) < X becomes false. */ |
12730 | if (code == LT_EXPR |
12731 | && ((code0 == PLUS_EXPR && is_positive >= 0) |
12732 | || (code0 == MINUS_EXPR && is_positive <= 0))) |
12733 | return constant_boolean_node (value: 0, type); |
12734 | |
12735 | /* Convert (X - c) <= X to true. */ |
12736 | if (!HONOR_NANS (arg1) |
12737 | && code == LE_EXPR |
12738 | && ((code0 == MINUS_EXPR && is_positive >= 0) |
12739 | || (code0 == PLUS_EXPR && is_positive <= 0))) |
12740 | return constant_boolean_node (value: 1, type); |
12741 | |
12742 | /* Convert (X + c) >= X to true. */ |
12743 | if (!HONOR_NANS (arg1) |
12744 | && code == GE_EXPR |
12745 | && ((code0 == PLUS_EXPR && is_positive >= 0) |
12746 | || (code0 == MINUS_EXPR && is_positive <= 0))) |
12747 | return constant_boolean_node (value: 1, type); |
12748 | } |
12749 | |
12750 | /* If we are comparing an ABS_EXPR with a constant, we can |
12751 | convert all the cases into explicit comparisons, but they may |
12752 | well not be faster than doing the ABS and one comparison. |
12753 | But ABS (X) <= C is a range comparison, which becomes a subtraction |
12754 | and a comparison, and is probably faster. */ |
12755 | if (code == LE_EXPR |
12756 | && TREE_CODE (arg1) == INTEGER_CST |
12757 | && TREE_CODE (arg0) == ABS_EXPR |
12758 | && ! TREE_SIDE_EFFECTS (arg0) |
12759 | && (tem = negate_expr (t: arg1)) != 0 |
12760 | && TREE_CODE (tem) == INTEGER_CST |
12761 | && !TREE_OVERFLOW (tem)) |
12762 | return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type, |
12763 | build2 (GE_EXPR, type, |
12764 | TREE_OPERAND (arg0, 0), tem), |
12765 | build2 (LE_EXPR, type, |
12766 | TREE_OPERAND (arg0, 0), arg1)); |
12767 | |
12768 | /* Convert ABS_EXPR<x> >= 0 to true. */ |
12769 | strict_overflow_p = false; |
12770 | if (code == GE_EXPR |
12771 | && (integer_zerop (arg1) |
12772 | || (! HONOR_NANS (arg0) |
12773 | && real_zerop (arg1))) |
12774 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) |
12775 | { |
12776 | if (strict_overflow_p) |
12777 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12778 | "when simplifying comparison of " |
12779 | "absolute value and zero" ), |
12780 | wc: WARN_STRICT_OVERFLOW_CONDITIONAL); |
12781 | return omit_one_operand_loc (loc, type, |
12782 | result: constant_boolean_node (value: true, type), |
12783 | omitted: arg0); |
12784 | } |
12785 | |
12786 | /* Convert ABS_EXPR<x> < 0 to false. */ |
12787 | strict_overflow_p = false; |
12788 | if (code == LT_EXPR |
12789 | && (integer_zerop (arg1) || real_zerop (arg1)) |
12790 | && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) |
12791 | { |
12792 | if (strict_overflow_p) |
12793 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur " |
12794 | "when simplifying comparison of " |
12795 | "absolute value and zero" ), |
12796 | wc: WARN_STRICT_OVERFLOW_CONDITIONAL); |
12797 | return omit_one_operand_loc (loc, type, |
12798 | result: constant_boolean_node (value: false, type), |
12799 | omitted: arg0); |
12800 | } |
12801 | |
12802 | /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0 |
12803 | and similarly for >= into !=. */ |
12804 | if ((code == LT_EXPR || code == GE_EXPR) |
12805 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) |
12806 | && TREE_CODE (arg1) == LSHIFT_EXPR |
12807 | && integer_onep (TREE_OPERAND (arg1, 0))) |
12808 | return build2_loc (loc, code: code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, |
12809 | arg0: build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, |
12810 | TREE_OPERAND (arg1, 1)), |
12811 | arg1: build_zero_cst (TREE_TYPE (arg0))); |
12812 | |
12813 | /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing, |
12814 | otherwise Y might be >= # of bits in X's type and thus e.g. |
12815 | (unsigned char) (1 << Y) for Y 15 might be 0. |
12816 | If the cast is widening, then 1 << Y should have unsigned type, |
12817 | otherwise if Y is number of bits in the signed shift type minus 1, |
12818 | we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y |
12819 | 31 might be 0xffffffff80000000. */ |
12820 | if ((code == LT_EXPR || code == GE_EXPR) |
12821 | && (INTEGRAL_TYPE_P (TREE_TYPE (arg0)) |
12822 | || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0))) |
12823 | && TYPE_UNSIGNED (TREE_TYPE (arg0)) |
12824 | && CONVERT_EXPR_P (arg1) |
12825 | && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR |
12826 | && (element_precision (TREE_TYPE (arg1)) |
12827 | >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))) |
12828 | && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))) |
12829 | || (element_precision (TREE_TYPE (arg1)) |
12830 | == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))) |
12831 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0))) |
12832 | { |
12833 | tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0, |
12834 | TREE_OPERAND (TREE_OPERAND (arg1, 0), 1)); |
12835 | return build2_loc (loc, code: code == LT_EXPR ? EQ_EXPR : NE_EXPR, type, |
12836 | arg0: fold_convert_loc (loc, TREE_TYPE (arg0), arg: tem), |
12837 | arg1: build_zero_cst (TREE_TYPE (arg0))); |
12838 | } |
12839 | |
12840 | return NULL_TREE; |
12841 | |
12842 | case UNORDERED_EXPR: |
12843 | case ORDERED_EXPR: |
12844 | case UNLT_EXPR: |
12845 | case UNLE_EXPR: |
12846 | case UNGT_EXPR: |
12847 | case UNGE_EXPR: |
12848 | case UNEQ_EXPR: |
12849 | case LTGT_EXPR: |
12850 | /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */ |
12851 | { |
12852 | tree targ0 = strip_float_extensions (arg0); |
12853 | tree targ1 = strip_float_extensions (arg1); |
12854 | tree newtype = TREE_TYPE (targ0); |
12855 | |
12856 | if (element_precision (TREE_TYPE (targ1)) > element_precision (newtype)) |
12857 | newtype = TREE_TYPE (targ1); |
12858 | |
12859 | if (element_precision (newtype) < element_precision (TREE_TYPE (arg0))) |
12860 | return fold_build2_loc (loc, code, type, |
12861 | fold_convert_loc (loc, type: newtype, arg: targ0), |
12862 | fold_convert_loc (loc, type: newtype, arg: targ1)); |
12863 | } |
12864 | |
12865 | return NULL_TREE; |
12866 | |
12867 | case COMPOUND_EXPR: |
12868 | /* When pedantic, a compound expression can be neither an lvalue |
12869 | nor an integer constant expression. */ |
12870 | if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1)) |
12871 | return NULL_TREE; |
12872 | /* Don't let (0, 0) be null pointer constant. */ |
12873 | tem = integer_zerop (arg1) ? build1_loc (loc, code: NOP_EXPR, type, arg1) |
12874 | : fold_convert_loc (loc, type, arg: arg1); |
12875 | return tem; |
12876 | |
12877 | default: |
12878 | return NULL_TREE; |
12879 | } /* switch (code) */ |
12880 | } |
12881 | |
12882 | /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M, |
12883 | ((A & N) + B) & M -> (A + B) & M |
12884 | Similarly if (N & M) == 0, |
12885 | ((A | N) + B) & M -> (A + B) & M |
12886 | and for - instead of + (or unary - instead of +) |
12887 | and/or ^ instead of |. |
12888 | If B is constant and (B & M) == 0, fold into A & M. |
12889 | |
12890 | This function is a helper for match.pd patterns. Return non-NULL |
12891 | type in which the simplified operation should be performed only |
12892 | if any optimization is possible. |
12893 | |
12894 | ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR, |
12895 | then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK. |
12896 | Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of |
12897 | +/-. */ |
12898 | tree |
12899 | fold_bit_and_mask (tree type, tree arg1, enum tree_code code, |
12900 | tree arg00, enum tree_code code00, tree arg000, tree arg001, |
12901 | tree arg01, enum tree_code code01, tree arg010, tree arg011, |
12902 | tree *pmop) |
12903 | { |
12904 | gcc_assert (TREE_CODE (arg1) == INTEGER_CST); |
12905 | gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR); |
12906 | wi::tree_to_wide_ref cst1 = wi::to_wide (t: arg1); |
12907 | if (~cst1 == 0 |
12908 | || (cst1 & (cst1 + 1)) != 0 |
12909 | || !INTEGRAL_TYPE_P (type) |
12910 | || (!TYPE_OVERFLOW_WRAPS (type) |
12911 | && TREE_CODE (type) != INTEGER_TYPE) |
12912 | || (wi::max_value (type) & cst1) != cst1) |
12913 | return NULL_TREE; |
12914 | |
12915 | enum tree_code codes[2] = { code00, code01 }; |
12916 | tree arg0xx[4] = { arg000, arg001, arg010, arg011 }; |
12917 | int which = 0; |
12918 | wide_int cst0; |
12919 | |
12920 | /* Now we know that arg0 is (C + D) or (C - D) or -C and |
12921 | arg1 (M) is == (1LL << cst) - 1. |
12922 | Store C into PMOP[0] and D into PMOP[1]. */ |
12923 | pmop[0] = arg00; |
12924 | pmop[1] = arg01; |
12925 | which = code != NEGATE_EXPR; |
12926 | |
12927 | for (; which >= 0; which--) |
12928 | switch (codes[which]) |
12929 | { |
12930 | case BIT_AND_EXPR: |
12931 | case BIT_IOR_EXPR: |
12932 | case BIT_XOR_EXPR: |
12933 | gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST); |
12934 | cst0 = wi::to_wide (t: arg0xx[2 * which + 1]) & cst1; |
12935 | if (codes[which] == BIT_AND_EXPR) |
12936 | { |
12937 | if (cst0 != cst1) |
12938 | break; |
12939 | } |
12940 | else if (cst0 != 0) |
12941 | break; |
12942 | /* If C or D is of the form (A & N) where |
12943 | (N & M) == M, or of the form (A | N) or |
12944 | (A ^ N) where (N & M) == 0, replace it with A. */ |
12945 | pmop[which] = arg0xx[2 * which]; |
12946 | break; |
12947 | case ERROR_MARK: |
12948 | if (TREE_CODE (pmop[which]) != INTEGER_CST) |
12949 | break; |
12950 | /* If C or D is a N where (N & M) == 0, it can be |
12951 | omitted (replaced with 0). */ |
12952 | if ((code == PLUS_EXPR |
12953 | || (code == MINUS_EXPR && which == 0)) |
12954 | && (cst1 & wi::to_wide (t: pmop[which])) == 0) |
12955 | pmop[which] = build_int_cst (type, 0); |
12956 | /* Similarly, with C - N where (-N & M) == 0. */ |
12957 | if (code == MINUS_EXPR |
12958 | && which == 1 |
12959 | && (cst1 & -wi::to_wide (t: pmop[which])) == 0) |
12960 | pmop[which] = build_int_cst (type, 0); |
12961 | break; |
12962 | default: |
12963 | gcc_unreachable (); |
12964 | } |
12965 | |
12966 | /* Only build anything new if we optimized one or both arguments above. */ |
12967 | if (pmop[0] == arg00 && pmop[1] == arg01) |
12968 | return NULL_TREE; |
12969 | |
12970 | if (TYPE_OVERFLOW_WRAPS (type)) |
12971 | return type; |
12972 | else |
12973 | return unsigned_type_for (type); |
12974 | } |
12975 | |
12976 | /* Used by contains_label_[p1]. */ |
12977 | |
12978 | struct contains_label_data |
12979 | { |
12980 | hash_set<tree> *pset; |
12981 | bool inside_switch_p; |
12982 | }; |
12983 | |
12984 | /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is |
12985 | a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise |
12986 | return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */ |
12987 | |
12988 | static tree |
12989 | contains_label_1 (tree *tp, int *walk_subtrees, void *data) |
12990 | { |
12991 | contains_label_data *d = (contains_label_data *) data; |
12992 | switch (TREE_CODE (*tp)) |
12993 | { |
12994 | case LABEL_EXPR: |
12995 | return *tp; |
12996 | |
12997 | case CASE_LABEL_EXPR: |
12998 | if (!d->inside_switch_p) |
12999 | return *tp; |
13000 | return NULL_TREE; |
13001 | |
13002 | case SWITCH_EXPR: |
13003 | if (!d->inside_switch_p) |
13004 | { |
13005 | if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset)) |
13006 | return *tp; |
13007 | d->inside_switch_p = true; |
13008 | if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset)) |
13009 | return *tp; |
13010 | d->inside_switch_p = false; |
13011 | *walk_subtrees = 0; |
13012 | } |
13013 | return NULL_TREE; |
13014 | |
13015 | case GOTO_EXPR: |
13016 | *walk_subtrees = 0; |
13017 | return NULL_TREE; |
13018 | |
13019 | default: |
13020 | return NULL_TREE; |
13021 | } |
13022 | } |
13023 | |
13024 | /* Return whether the sub-tree ST contains a label which is accessible from |
13025 | outside the sub-tree. */ |
13026 | |
13027 | static bool |
13028 | contains_label_p (tree st) |
13029 | { |
13030 | hash_set<tree> pset; |
13031 | contains_label_data data = { .pset: &pset, .inside_switch_p: false }; |
13032 | return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE; |
13033 | } |
13034 | |
13035 | /* Fold a ternary expression of code CODE and type TYPE with operands |
13036 | OP0, OP1, and OP2. Return the folded expression if folding is |
13037 | successful. Otherwise, return NULL_TREE. */ |
13038 | |
13039 | tree |
13040 | fold_ternary_loc (location_t loc, enum tree_code code, tree type, |
13041 | tree op0, tree op1, tree op2) |
13042 | { |
13043 | tree tem; |
13044 | tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE; |
13045 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
13046 | |
13047 | gcc_assert (IS_EXPR_CODE_CLASS (kind) |
13048 | && TREE_CODE_LENGTH (code) == 3); |
13049 | |
13050 | /* If this is a commutative operation, and OP0 is a constant, move it |
13051 | to OP1 to reduce the number of tests below. */ |
13052 | if (commutative_ternary_tree_code (code) |
13053 | && tree_swap_operands_p (arg0: op0, arg1: op1)) |
13054 | return fold_build3_loc (loc, code, type, op1, op0, op2); |
13055 | |
13056 | tem = generic_simplify (loc, code, type, op0, op1, op2); |
13057 | if (tem) |
13058 | return tem; |
13059 | |
13060 | /* Strip any conversions that don't change the mode. This is safe |
13061 | for every expression, except for a comparison expression because |
13062 | its signedness is derived from its operands. So, in the latter |
13063 | case, only strip conversions that don't change the signedness. |
13064 | |
13065 | Note that this is done as an internal manipulation within the |
13066 | constant folder, in order to find the simplest representation of |
13067 | the arguments so that their form can be studied. In any cases, |
13068 | the appropriate type conversions should be put back in the tree |
13069 | that will get out of the constant folder. */ |
13070 | if (op0) |
13071 | { |
13072 | arg0 = op0; |
13073 | STRIP_NOPS (arg0); |
13074 | } |
13075 | |
13076 | if (op1) |
13077 | { |
13078 | arg1 = op1; |
13079 | STRIP_NOPS (arg1); |
13080 | } |
13081 | |
13082 | if (op2) |
13083 | { |
13084 | arg2 = op2; |
13085 | STRIP_NOPS (arg2); |
13086 | } |
13087 | |
13088 | switch (code) |
13089 | { |
13090 | case COMPONENT_REF: |
13091 | if (TREE_CODE (arg0) == CONSTRUCTOR |
13092 | && ! type_contains_placeholder_p (TREE_TYPE (arg0))) |
13093 | { |
13094 | unsigned HOST_WIDE_INT idx; |
13095 | tree field, value; |
13096 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value) |
13097 | if (field == arg1) |
13098 | return value; |
13099 | } |
13100 | return NULL_TREE; |
13101 | |
13102 | case COND_EXPR: |
13103 | case VEC_COND_EXPR: |
13104 | /* Pedantic ANSI C says that a conditional expression is never an lvalue, |
13105 | so all simple results must be passed through pedantic_non_lvalue. */ |
13106 | if (TREE_CODE (arg0) == INTEGER_CST) |
13107 | { |
13108 | tree unused_op = integer_zerop (arg0) ? op1 : op2; |
13109 | tem = integer_zerop (arg0) ? op2 : op1; |
13110 | /* Only optimize constant conditions when the selected branch |
13111 | has the same type as the COND_EXPR. This avoids optimizing |
13112 | away "c ? x : throw", where the throw has a void type. |
13113 | Avoid throwing away that operand which contains label. */ |
13114 | if ((!TREE_SIDE_EFFECTS (unused_op) |
13115 | || !contains_label_p (st: unused_op)) |
13116 | && (! VOID_TYPE_P (TREE_TYPE (tem)) |
13117 | || VOID_TYPE_P (type))) |
13118 | return protected_set_expr_location_unshare (x: tem, loc); |
13119 | return NULL_TREE; |
13120 | } |
13121 | else if (TREE_CODE (arg0) == VECTOR_CST) |
13122 | { |
13123 | unsigned HOST_WIDE_INT nelts; |
13124 | if ((TREE_CODE (arg1) == VECTOR_CST |
13125 | || TREE_CODE (arg1) == CONSTRUCTOR) |
13126 | && (TREE_CODE (arg2) == VECTOR_CST |
13127 | || TREE_CODE (arg2) == CONSTRUCTOR) |
13128 | && TYPE_VECTOR_SUBPARTS (node: type).is_constant (const_value: &nelts)) |
13129 | { |
13130 | vec_perm_builder sel (nelts, nelts, 1); |
13131 | for (unsigned int i = 0; i < nelts; i++) |
13132 | { |
13133 | tree val = VECTOR_CST_ELT (arg0, i); |
13134 | if (integer_all_onesp (val)) |
13135 | sel.quick_push (obj: i); |
13136 | else if (integer_zerop (val)) |
13137 | sel.quick_push (obj: nelts + i); |
13138 | else /* Currently unreachable. */ |
13139 | return NULL_TREE; |
13140 | } |
13141 | vec_perm_indices indices (sel, 2, nelts); |
13142 | tree t = fold_vec_perm (type, arg0: arg1, arg1: arg2, sel: indices); |
13143 | if (t != NULL_TREE) |
13144 | return t; |
13145 | } |
13146 | } |
13147 | |
13148 | /* If we have A op B ? A : C, we may be able to convert this to a |
13149 | simpler expression, depending on the operation and the values |
13150 | of B and C. Signed zeros prevent all of these transformations, |
13151 | for reasons given above each one. |
13152 | |
13153 | Also try swapping the arguments and inverting the conditional. */ |
13154 | if (COMPARISON_CLASS_P (arg0) |
13155 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1: op1) |
13156 | && !HONOR_SIGNED_ZEROS (op1)) |
13157 | { |
13158 | tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0), |
13159 | TREE_OPERAND (arg0, 0), |
13160 | TREE_OPERAND (arg0, 1), |
13161 | arg1: op1, arg2: op2); |
13162 | if (tem) |
13163 | return tem; |
13164 | } |
13165 | |
13166 | if (COMPARISON_CLASS_P (arg0) |
13167 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), arg1: op2) |
13168 | && !HONOR_SIGNED_ZEROS (op2)) |
13169 | { |
13170 | enum tree_code comp_code = TREE_CODE (arg0); |
13171 | tree arg00 = TREE_OPERAND (arg0, 0); |
13172 | tree arg01 = TREE_OPERAND (arg0, 1); |
13173 | comp_code = invert_tree_comparison (code: comp_code, honor_nans: HONOR_NANS (arg00)); |
13174 | if (comp_code != ERROR_MARK) |
13175 | tem = fold_cond_expr_with_comparison (loc, type, comp_code, |
13176 | arg00, |
13177 | arg01, |
13178 | arg1: op2, arg2: op1); |
13179 | if (tem) |
13180 | return tem; |
13181 | } |
13182 | |
13183 | /* If the second operand is simpler than the third, swap them |
13184 | since that produces better jump optimization results. */ |
13185 | if (truth_value_p (TREE_CODE (arg0)) |
13186 | && tree_swap_operands_p (arg0: op1, arg1: op2)) |
13187 | { |
13188 | location_t loc0 = expr_location_or (t: arg0, loc); |
13189 | /* See if this can be inverted. If it can't, possibly because |
13190 | it was a floating-point inequality comparison, don't do |
13191 | anything. */ |
13192 | tem = fold_invert_truthvalue (loc: loc0, arg: arg0); |
13193 | if (tem) |
13194 | return fold_build3_loc (loc, code, type, tem, op2, op1); |
13195 | } |
13196 | |
13197 | /* Convert A ? 1 : 0 to simply A. */ |
13198 | if ((code == VEC_COND_EXPR ? integer_all_onesp (op1) |
13199 | : (integer_onep (op1) |
13200 | && !VECTOR_TYPE_P (type))) |
13201 | && integer_zerop (op2) |
13202 | /* If we try to convert OP0 to our type, the |
13203 | call to fold will try to move the conversion inside |
13204 | a COND, which will recurse. In that case, the COND_EXPR |
13205 | is probably the best choice, so leave it alone. */ |
13206 | && type == TREE_TYPE (arg0)) |
13207 | return protected_set_expr_location_unshare (x: arg0, loc); |
13208 | |
13209 | /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR |
13210 | over COND_EXPR in cases such as floating point comparisons. */ |
13211 | if (integer_zerop (op1) |
13212 | && code == COND_EXPR |
13213 | && integer_onep (op2) |
13214 | && !VECTOR_TYPE_P (type) |
13215 | && truth_value_p (TREE_CODE (arg0))) |
13216 | return fold_convert_loc (loc, type, |
13217 | arg: invert_truthvalue_loc (loc, arg: arg0)); |
13218 | |
13219 | /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */ |
13220 | if (TREE_CODE (arg0) == LT_EXPR |
13221 | && integer_zerop (TREE_OPERAND (arg0, 1)) |
13222 | && integer_zerop (op2) |
13223 | && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), val: arg1))) |
13224 | { |
13225 | /* sign_bit_p looks through both zero and sign extensions, |
13226 | but for this optimization only sign extensions are |
13227 | usable. */ |
13228 | tree tem2 = TREE_OPERAND (arg0, 0); |
13229 | while (tem != tem2) |
13230 | { |
13231 | if (TREE_CODE (tem2) != NOP_EXPR |
13232 | || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0)))) |
13233 | { |
13234 | tem = NULL_TREE; |
13235 | break; |
13236 | } |
13237 | tem2 = TREE_OPERAND (tem2, 0); |
13238 | } |
13239 | /* sign_bit_p only checks ARG1 bits within A's precision. |
13240 | If <sign bit of A> has wider type than A, bits outside |
13241 | of A's precision in <sign bit of A> need to be checked. |
13242 | If they are all 0, this optimization needs to be done |
13243 | in unsigned A's type, if they are all 1 in signed A's type, |
13244 | otherwise this can't be done. */ |
13245 | if (tem |
13246 | && TYPE_PRECISION (TREE_TYPE (tem)) |
13247 | < TYPE_PRECISION (TREE_TYPE (arg1)) |
13248 | && TYPE_PRECISION (TREE_TYPE (tem)) |
13249 | < TYPE_PRECISION (type)) |
13250 | { |
13251 | int inner_width, outer_width; |
13252 | tree tem_type; |
13253 | |
13254 | inner_width = TYPE_PRECISION (TREE_TYPE (tem)); |
13255 | outer_width = TYPE_PRECISION (TREE_TYPE (arg1)); |
13256 | if (outer_width > TYPE_PRECISION (type)) |
13257 | outer_width = TYPE_PRECISION (type); |
13258 | |
13259 | wide_int mask = wi::shifted_mask |
13260 | (start: inner_width, width: outer_width - inner_width, negate_p: false, |
13261 | TYPE_PRECISION (TREE_TYPE (arg1))); |
13262 | |
13263 | wide_int common = mask & wi::to_wide (t: arg1); |
13264 | if (common == mask) |
13265 | { |
13266 | tem_type = signed_type_for (TREE_TYPE (tem)); |
13267 | tem = fold_convert_loc (loc, type: tem_type, arg: tem); |
13268 | } |
13269 | else if (common == 0) |
13270 | { |
13271 | tem_type = unsigned_type_for (TREE_TYPE (tem)); |
13272 | tem = fold_convert_loc (loc, type: tem_type, arg: tem); |
13273 | } |
13274 | else |
13275 | tem = NULL; |
13276 | } |
13277 | |
13278 | if (tem) |
13279 | return |
13280 | fold_convert_loc (loc, type, |
13281 | arg: fold_build2_loc (loc, BIT_AND_EXPR, |
13282 | TREE_TYPE (tem), tem, |
13283 | fold_convert_loc (loc, |
13284 | TREE_TYPE (tem), |
13285 | arg: arg1))); |
13286 | } |
13287 | |
13288 | /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was |
13289 | already handled above. */ |
13290 | if (TREE_CODE (arg0) == BIT_AND_EXPR |
13291 | && integer_onep (TREE_OPERAND (arg0, 1)) |
13292 | && integer_zerop (op2) |
13293 | && integer_pow2p (arg1)) |
13294 | { |
13295 | tree tem = TREE_OPERAND (arg0, 0); |
13296 | STRIP_NOPS (tem); |
13297 | if (TREE_CODE (tem) == RSHIFT_EXPR |
13298 | && tree_fits_uhwi_p (TREE_OPERAND (tem, 1)) |
13299 | && (unsigned HOST_WIDE_INT) tree_log2 (arg1) |
13300 | == tree_to_uhwi (TREE_OPERAND (tem, 1))) |
13301 | return fold_build2_loc (loc, BIT_AND_EXPR, type, |
13302 | fold_convert_loc (loc, type, |
13303 | TREE_OPERAND (tem, 0)), |
13304 | op1); |
13305 | } |
13306 | |
13307 | /* A & N ? N : 0 is simply A & N if N is a power of two. This |
13308 | is probably obsolete because the first operand should be a |
13309 | truth value (that's why we have the two cases above), but let's |
13310 | leave it in until we can confirm this for all front-ends. */ |
13311 | if (integer_zerop (op2) |
13312 | && TREE_CODE (arg0) == NE_EXPR |
13313 | && integer_zerop (TREE_OPERAND (arg0, 1)) |
13314 | && integer_pow2p (arg1) |
13315 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR |
13316 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), |
13317 | arg1, flags: OEP_ONLY_CONST) |
13318 | /* operand_equal_p compares just value, not precision, so e.g. |
13319 | arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR |
13320 | second operand 32-bit -128, which is not a power of two (or vice |
13321 | versa. */ |
13322 | && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))) |
13323 | return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)); |
13324 | |
13325 | /* Disable the transformations below for vectors, since |
13326 | fold_binary_op_with_conditional_arg may undo them immediately, |
13327 | yielding an infinite loop. */ |
13328 | if (code == VEC_COND_EXPR) |
13329 | return NULL_TREE; |
13330 | |
13331 | /* Convert A ? B : 0 into A && B if A and B are truth values. */ |
13332 | if (integer_zerop (op2) |
13333 | && truth_value_p (TREE_CODE (arg0)) |
13334 | && truth_value_p (TREE_CODE (arg1)) |
13335 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) |
13336 | return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR |
13337 | : TRUTH_ANDIF_EXPR, |
13338 | type, fold_convert_loc (loc, type, arg: arg0), op1); |
13339 | |
13340 | /* Convert A ? B : 1 into !A || B if A and B are truth values. */ |
13341 | if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2) |
13342 | && truth_value_p (TREE_CODE (arg0)) |
13343 | && truth_value_p (TREE_CODE (arg1)) |
13344 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) |
13345 | { |
13346 | location_t loc0 = expr_location_or (t: arg0, loc); |
13347 | /* Only perform transformation if ARG0 is easily inverted. */ |
13348 | tem = fold_invert_truthvalue (loc: loc0, arg: arg0); |
13349 | if (tem) |
13350 | return fold_build2_loc (loc, code == VEC_COND_EXPR |
13351 | ? BIT_IOR_EXPR |
13352 | : TRUTH_ORIF_EXPR, |
13353 | type, fold_convert_loc (loc, type, arg: tem), |
13354 | op1); |
13355 | } |
13356 | |
13357 | /* Convert A ? 0 : B into !A && B if A and B are truth values. */ |
13358 | if (integer_zerop (arg1) |
13359 | && truth_value_p (TREE_CODE (arg0)) |
13360 | && truth_value_p (TREE_CODE (op2)) |
13361 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) |
13362 | { |
13363 | location_t loc0 = expr_location_or (t: arg0, loc); |
13364 | /* Only perform transformation if ARG0 is easily inverted. */ |
13365 | tem = fold_invert_truthvalue (loc: loc0, arg: arg0); |
13366 | if (tem) |
13367 | return fold_build2_loc (loc, code == VEC_COND_EXPR |
13368 | ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR, |
13369 | type, fold_convert_loc (loc, type, arg: tem), |
13370 | op2); |
13371 | } |
13372 | |
13373 | /* Convert A ? 1 : B into A || B if A and B are truth values. */ |
13374 | if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1) |
13375 | && truth_value_p (TREE_CODE (arg0)) |
13376 | && truth_value_p (TREE_CODE (op2)) |
13377 | && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type))) |
13378 | return fold_build2_loc (loc, code == VEC_COND_EXPR |
13379 | ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR, |
13380 | type, fold_convert_loc (loc, type, arg: arg0), op2); |
13381 | |
13382 | return NULL_TREE; |
13383 | |
13384 | case CALL_EXPR: |
13385 | /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses |
13386 | of fold_ternary on them. */ |
13387 | gcc_unreachable (); |
13388 | |
13389 | case BIT_FIELD_REF: |
13390 | if (TREE_CODE (arg0) == VECTOR_CST |
13391 | && (type == TREE_TYPE (TREE_TYPE (arg0)) |
13392 | || (VECTOR_TYPE_P (type) |
13393 | && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))) |
13394 | && tree_fits_uhwi_p (op1) |
13395 | && tree_fits_uhwi_p (op2)) |
13396 | { |
13397 | tree eltype = TREE_TYPE (TREE_TYPE (arg0)); |
13398 | unsigned HOST_WIDE_INT width |
13399 | = (TREE_CODE (eltype) == BOOLEAN_TYPE |
13400 | ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype))); |
13401 | unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1); |
13402 | unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2); |
13403 | |
13404 | if (n != 0 |
13405 | && (idx % width) == 0 |
13406 | && (n % width) == 0 |
13407 | && known_le ((idx + n) / width, |
13408 | TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))) |
13409 | { |
13410 | idx = idx / width; |
13411 | n = n / width; |
13412 | |
13413 | if (TREE_CODE (arg0) == VECTOR_CST) |
13414 | { |
13415 | if (n == 1) |
13416 | { |
13417 | tem = VECTOR_CST_ELT (arg0, idx); |
13418 | if (VECTOR_TYPE_P (type)) |
13419 | tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem); |
13420 | return tem; |
13421 | } |
13422 | |
13423 | tree_vector_builder vals (type, n, 1); |
13424 | for (unsigned i = 0; i < n; ++i) |
13425 | vals.quick_push (VECTOR_CST_ELT (arg0, idx + i)); |
13426 | return vals.build (); |
13427 | } |
13428 | } |
13429 | } |
13430 | |
13431 | /* On constants we can use native encode/interpret to constant |
13432 | fold (nearly) all BIT_FIELD_REFs. */ |
13433 | if (CONSTANT_CLASS_P (arg0) |
13434 | && can_native_interpret_type_p (type) |
13435 | && BITS_PER_UNIT == 8 |
13436 | && tree_fits_uhwi_p (op1) |
13437 | && tree_fits_uhwi_p (op2)) |
13438 | { |
13439 | unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2); |
13440 | unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1); |
13441 | /* Limit us to a reasonable amount of work. To relax the |
13442 | other limitations we need bit-shifting of the buffer |
13443 | and rounding up the size. */ |
13444 | if (bitpos % BITS_PER_UNIT == 0 |
13445 | && bitsize % BITS_PER_UNIT == 0 |
13446 | && bitsize <= MAX_BITSIZE_MODE_ANY_MODE) |
13447 | { |
13448 | unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT]; |
13449 | unsigned HOST_WIDE_INT len |
13450 | = native_encode_expr (expr: arg0, ptr: b, len: bitsize / BITS_PER_UNIT, |
13451 | off: bitpos / BITS_PER_UNIT); |
13452 | if (len > 0 |
13453 | && len * BITS_PER_UNIT >= bitsize) |
13454 | { |
13455 | tree v = native_interpret_expr (type, ptr: b, |
13456 | len: bitsize / BITS_PER_UNIT); |
13457 | if (v) |
13458 | return v; |
13459 | } |
13460 | } |
13461 | } |
13462 | |
13463 | return NULL_TREE; |
13464 | |
13465 | case VEC_PERM_EXPR: |
13466 | /* Perform constant folding of BIT_INSERT_EXPR. */ |
13467 | if (TREE_CODE (arg2) == VECTOR_CST |
13468 | && TREE_CODE (op0) == VECTOR_CST |
13469 | && TREE_CODE (op1) == VECTOR_CST) |
13470 | { |
13471 | /* Build a vector of integers from the tree mask. */ |
13472 | vec_perm_builder builder; |
13473 | if (!tree_to_vec_perm_builder (&builder, arg2)) |
13474 | return NULL_TREE; |
13475 | |
13476 | /* Create a vec_perm_indices for the integer vector. */ |
13477 | poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (node: type); |
13478 | bool single_arg = (op0 == op1); |
13479 | vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts); |
13480 | return fold_vec_perm (type, arg0: op0, arg1: op1, sel); |
13481 | } |
13482 | return NULL_TREE; |
13483 | |
13484 | case BIT_INSERT_EXPR: |
13485 | /* Perform (partial) constant folding of BIT_INSERT_EXPR. */ |
13486 | if (TREE_CODE (arg0) == INTEGER_CST |
13487 | && TREE_CODE (arg1) == INTEGER_CST) |
13488 | { |
13489 | unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2); |
13490 | unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1)); |
13491 | wide_int tem = (wi::to_wide (t: arg0) |
13492 | & wi::shifted_mask (start: bitpos, width: bitsize, negate_p: true, |
13493 | TYPE_PRECISION (type))); |
13494 | wide_int tem2 |
13495 | = wi::lshift (x: wi::zext (x: wi::to_wide (t: arg1, TYPE_PRECISION (type)), |
13496 | offset: bitsize), y: bitpos); |
13497 | return wide_int_to_tree (type, cst: wi::bit_or (x: tem, y: tem2)); |
13498 | } |
13499 | else if (TREE_CODE (arg0) == VECTOR_CST |
13500 | && CONSTANT_CLASS_P (arg1) |
13501 | && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)), |
13502 | TREE_TYPE (arg1))) |
13503 | { |
13504 | unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2); |
13505 | unsigned HOST_WIDE_INT elsize |
13506 | = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1))); |
13507 | if (bitpos % elsize == 0) |
13508 | { |
13509 | unsigned k = bitpos / elsize; |
13510 | unsigned HOST_WIDE_INT nelts; |
13511 | if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, flags: 0)) |
13512 | return arg0; |
13513 | else if (VECTOR_CST_NELTS (arg0).is_constant (const_value: &nelts)) |
13514 | { |
13515 | tree_vector_builder elts (type, nelts, 1); |
13516 | elts.quick_grow (len: nelts); |
13517 | for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i) |
13518 | elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i)); |
13519 | return elts.build (); |
13520 | } |
13521 | } |
13522 | } |
13523 | return NULL_TREE; |
13524 | |
13525 | default: |
13526 | return NULL_TREE; |
13527 | } /* switch (code) */ |
13528 | } |
13529 | |
13530 | /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR |
13531 | of an array (or vector). *CTOR_IDX if non-NULL is updated with the |
13532 | constructor element index of the value returned. If the element is |
13533 | not found NULL_TREE is returned and *CTOR_IDX is updated to |
13534 | the index of the element after the ACCESS_INDEX position (which |
13535 | may be outside of the CTOR array). */ |
13536 | |
13537 | tree |
13538 | get_array_ctor_element_at_index (tree ctor, offset_int access_index, |
13539 | unsigned *ctor_idx) |
13540 | { |
13541 | tree index_type = NULL_TREE; |
13542 | signop index_sgn = UNSIGNED; |
13543 | offset_int low_bound = 0; |
13544 | |
13545 | if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE) |
13546 | { |
13547 | tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor)); |
13548 | if (domain_type && TYPE_MIN_VALUE (domain_type)) |
13549 | { |
13550 | /* Static constructors for variably sized objects makes no sense. */ |
13551 | gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST); |
13552 | index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type)); |
13553 | /* ??? When it is obvious that the range is signed, treat it so. */ |
13554 | if (TYPE_UNSIGNED (index_type) |
13555 | && TYPE_MAX_VALUE (domain_type) |
13556 | && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type), |
13557 | TYPE_MIN_VALUE (domain_type))) |
13558 | { |
13559 | index_sgn = SIGNED; |
13560 | low_bound |
13561 | = offset_int::from (x: wi::to_wide (TYPE_MIN_VALUE (domain_type)), |
13562 | sgn: SIGNED); |
13563 | } |
13564 | else |
13565 | { |
13566 | index_sgn = TYPE_SIGN (index_type); |
13567 | low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type)); |
13568 | } |
13569 | } |
13570 | } |
13571 | |
13572 | if (index_type) |
13573 | access_index = wi::ext (x: access_index, TYPE_PRECISION (index_type), |
13574 | sgn: index_sgn); |
13575 | |
13576 | offset_int index = low_bound; |
13577 | if (index_type) |
13578 | index = wi::ext (x: index, TYPE_PRECISION (index_type), sgn: index_sgn); |
13579 | |
13580 | offset_int max_index = index; |
13581 | unsigned cnt; |
13582 | tree cfield, cval; |
13583 | bool first_p = true; |
13584 | |
13585 | FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval) |
13586 | { |
13587 | /* Array constructor might explicitly set index, or specify a range, |
13588 | or leave index NULL meaning that it is next index after previous |
13589 | one. */ |
13590 | if (cfield) |
13591 | { |
13592 | if (TREE_CODE (cfield) == INTEGER_CST) |
13593 | max_index = index |
13594 | = offset_int::from (x: wi::to_wide (t: cfield), sgn: index_sgn); |
13595 | else |
13596 | { |
13597 | gcc_assert (TREE_CODE (cfield) == RANGE_EXPR); |
13598 | index = offset_int::from (x: wi::to_wide (TREE_OPERAND (cfield, 0)), |
13599 | sgn: index_sgn); |
13600 | max_index |
13601 | = offset_int::from (x: wi::to_wide (TREE_OPERAND (cfield, 1)), |
13602 | sgn: index_sgn); |
13603 | gcc_checking_assert (wi::le_p (index, max_index, index_sgn)); |
13604 | } |
13605 | } |
13606 | else if (!first_p) |
13607 | { |
13608 | index = max_index + 1; |
13609 | if (index_type) |
13610 | index = wi::ext (x: index, TYPE_PRECISION (index_type), sgn: index_sgn); |
13611 | gcc_checking_assert (wi::gt_p (index, max_index, index_sgn)); |
13612 | max_index = index; |
13613 | } |
13614 | else |
13615 | first_p = false; |
13616 | |
13617 | /* Do we have match? */ |
13618 | if (wi::cmp (x: access_index, y: index, sgn: index_sgn) >= 0) |
13619 | { |
13620 | if (wi::cmp (x: access_index, y: max_index, sgn: index_sgn) <= 0) |
13621 | { |
13622 | if (ctor_idx) |
13623 | *ctor_idx = cnt; |
13624 | return cval; |
13625 | } |
13626 | } |
13627 | else if (in_gimple_form) |
13628 | /* We're past the element we search for. Note during parsing |
13629 | the elements might not be sorted. |
13630 | ??? We should use a binary search and a flag on the |
13631 | CONSTRUCTOR as to whether elements are sorted in declaration |
13632 | order. */ |
13633 | break; |
13634 | } |
13635 | if (ctor_idx) |
13636 | *ctor_idx = cnt; |
13637 | return NULL_TREE; |
13638 | } |
13639 | |
13640 | /* Perform constant folding and related simplification of EXPR. |
13641 | The related simplifications include x*1 => x, x*0 => 0, etc., |
13642 | and application of the associative law. |
13643 | NOP_EXPR conversions may be removed freely (as long as we |
13644 | are careful not to change the type of the overall expression). |
13645 | We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR, |
13646 | but we can constant-fold them if they have constant operands. */ |
13647 | |
13648 | #ifdef ENABLE_FOLD_CHECKING |
13649 | # define fold(x) fold_1 (x) |
13650 | static tree fold_1 (tree); |
13651 | static |
13652 | #endif |
13653 | tree |
13654 | fold (tree expr) |
13655 | { |
13656 | const tree t = expr; |
13657 | enum tree_code code = TREE_CODE (t); |
13658 | enum tree_code_class kind = TREE_CODE_CLASS (code); |
13659 | tree tem; |
13660 | location_t loc = EXPR_LOCATION (expr); |
13661 | |
13662 | /* Return right away if a constant. */ |
13663 | if (kind == tcc_constant) |
13664 | return t; |
13665 | |
13666 | /* CALL_EXPR-like objects with variable numbers of operands are |
13667 | treated specially. */ |
13668 | if (kind == tcc_vl_exp) |
13669 | { |
13670 | if (code == CALL_EXPR) |
13671 | { |
13672 | tem = fold_call_expr (loc, expr, false); |
13673 | return tem ? tem : expr; |
13674 | } |
13675 | return expr; |
13676 | } |
13677 | |
13678 | if (IS_EXPR_CODE_CLASS (kind)) |
13679 | { |
13680 | tree type = TREE_TYPE (t); |
13681 | tree op0, op1, op2; |
13682 | |
13683 | switch (TREE_CODE_LENGTH (code)) |
13684 | { |
13685 | case 1: |
13686 | op0 = TREE_OPERAND (t, 0); |
13687 | tem = fold_unary_loc (loc, code, type, op0); |
13688 | return tem ? tem : expr; |
13689 | case 2: |
13690 | op0 = TREE_OPERAND (t, 0); |
13691 | op1 = TREE_OPERAND (t, 1); |
13692 | tem = fold_binary_loc (loc, code, type, op0, op1); |
13693 | return tem ? tem : expr; |
13694 | case 3: |
13695 | op0 = TREE_OPERAND (t, 0); |
13696 | op1 = TREE_OPERAND (t, 1); |
13697 | op2 = TREE_OPERAND (t, 2); |
13698 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
13699 | return tem ? tem : expr; |
13700 | default: |
13701 | break; |
13702 | } |
13703 | } |
13704 | |
13705 | switch (code) |
13706 | { |
13707 | case ARRAY_REF: |
13708 | { |
13709 | tree op0 = TREE_OPERAND (t, 0); |
13710 | tree op1 = TREE_OPERAND (t, 1); |
13711 | |
13712 | if (TREE_CODE (op1) == INTEGER_CST |
13713 | && TREE_CODE (op0) == CONSTRUCTOR |
13714 | && ! type_contains_placeholder_p (TREE_TYPE (op0))) |
13715 | { |
13716 | tree val = get_array_ctor_element_at_index (ctor: op0, |
13717 | access_index: wi::to_offset (t: op1)); |
13718 | if (val) |
13719 | return val; |
13720 | } |
13721 | |
13722 | return t; |
13723 | } |
13724 | |
13725 | /* Return a VECTOR_CST if possible. */ |
13726 | case CONSTRUCTOR: |
13727 | { |
13728 | tree type = TREE_TYPE (t); |
13729 | if (TREE_CODE (type) != VECTOR_TYPE) |
13730 | return t; |
13731 | |
13732 | unsigned i; |
13733 | tree val; |
13734 | FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val) |
13735 | if (! CONSTANT_CLASS_P (val)) |
13736 | return t; |
13737 | |
13738 | return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t)); |
13739 | } |
13740 | |
13741 | case CONST_DECL: |
13742 | return fold (DECL_INITIAL (t)); |
13743 | |
13744 | default: |
13745 | return t; |
13746 | } /* switch (code) */ |
13747 | } |
13748 | |
13749 | #ifdef ENABLE_FOLD_CHECKING |
13750 | #undef fold |
13751 | |
13752 | static void fold_checksum_tree (const_tree, struct md5_ctx *, |
13753 | hash_table<nofree_ptr_hash<const tree_node> > *); |
13754 | static void fold_check_failed (const_tree, const_tree); |
13755 | void print_fold_checksum (const_tree); |
13756 | |
13757 | /* When --enable-checking=fold, compute a digest of expr before |
13758 | and after actual fold call to see if fold did not accidentally |
13759 | change original expr. */ |
13760 | |
13761 | tree |
13762 | fold (tree expr) |
13763 | { |
13764 | tree ret; |
13765 | struct md5_ctx ctx; |
13766 | unsigned char checksum_before[16], checksum_after[16]; |
13767 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
13768 | |
13769 | md5_init_ctx (&ctx); |
13770 | fold_checksum_tree (expr, &ctx, &ht); |
13771 | md5_finish_ctx (&ctx, checksum_before); |
13772 | ht.empty (); |
13773 | |
13774 | ret = fold_1 (expr); |
13775 | |
13776 | md5_init_ctx (&ctx); |
13777 | fold_checksum_tree (expr, &ctx, &ht); |
13778 | md5_finish_ctx (&ctx, checksum_after); |
13779 | |
13780 | if (memcmp (checksum_before, checksum_after, 16)) |
13781 | fold_check_failed (expr, ret); |
13782 | |
13783 | return ret; |
13784 | } |
13785 | |
13786 | void |
13787 | print_fold_checksum (const_tree expr) |
13788 | { |
13789 | struct md5_ctx ctx; |
13790 | unsigned char checksum[16], cnt; |
13791 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
13792 | |
13793 | md5_init_ctx (&ctx); |
13794 | fold_checksum_tree (expr, &ctx, &ht); |
13795 | md5_finish_ctx (&ctx, checksum); |
13796 | for (cnt = 0; cnt < 16; ++cnt) |
13797 | fprintf (stderr, "%02x" , checksum[cnt]); |
13798 | putc ('\n', stderr); |
13799 | } |
13800 | |
13801 | static void |
13802 | fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED) |
13803 | { |
13804 | internal_error ("fold check: original tree changed by fold" ); |
13805 | } |
13806 | |
13807 | static void |
13808 | fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, |
13809 | hash_table<nofree_ptr_hash <const tree_node> > *ht) |
13810 | { |
13811 | const tree_node **slot; |
13812 | enum tree_code code; |
13813 | union tree_node *buf; |
13814 | int i, len; |
13815 | |
13816 | recursive_label: |
13817 | if (expr == NULL) |
13818 | return; |
13819 | slot = ht->find_slot (expr, INSERT); |
13820 | if (*slot != NULL) |
13821 | return; |
13822 | *slot = expr; |
13823 | code = TREE_CODE (expr); |
13824 | if (TREE_CODE_CLASS (code) == tcc_declaration |
13825 | && HAS_DECL_ASSEMBLER_NAME_P (expr)) |
13826 | { |
13827 | /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */ |
13828 | size_t sz = tree_size (expr); |
13829 | buf = XALLOCAVAR (union tree_node, sz); |
13830 | memcpy ((char *) buf, expr, sz); |
13831 | SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL); |
13832 | buf->decl_with_vis.symtab_node = NULL; |
13833 | buf->base.nowarning_flag = 0; |
13834 | expr = (tree) buf; |
13835 | } |
13836 | else if (TREE_CODE_CLASS (code) == tcc_type |
13837 | && (TYPE_POINTER_TO (expr) |
13838 | || TYPE_REFERENCE_TO (expr) |
13839 | || TYPE_CACHED_VALUES_P (expr) |
13840 | || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) |
13841 | || TYPE_NEXT_VARIANT (expr) |
13842 | || TYPE_ALIAS_SET_KNOWN_P (expr))) |
13843 | { |
13844 | /* Allow these fields to be modified. */ |
13845 | tree tmp; |
13846 | size_t sz = tree_size (expr); |
13847 | buf = XALLOCAVAR (union tree_node, sz); |
13848 | memcpy ((char *) buf, expr, sz); |
13849 | expr = tmp = (tree) buf; |
13850 | TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0; |
13851 | TYPE_POINTER_TO (tmp) = NULL; |
13852 | TYPE_REFERENCE_TO (tmp) = NULL; |
13853 | TYPE_NEXT_VARIANT (tmp) = NULL; |
13854 | TYPE_ALIAS_SET (tmp) = -1; |
13855 | if (TYPE_CACHED_VALUES_P (tmp)) |
13856 | { |
13857 | TYPE_CACHED_VALUES_P (tmp) = 0; |
13858 | TYPE_CACHED_VALUES (tmp) = NULL; |
13859 | } |
13860 | } |
13861 | else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr))) |
13862 | { |
13863 | /* Allow the no-warning bit to be set. Perhaps we shouldn't allow |
13864 | that and change builtins.cc etc. instead - see PR89543. */ |
13865 | size_t sz = tree_size (expr); |
13866 | buf = XALLOCAVAR (union tree_node, sz); |
13867 | memcpy ((char *) buf, expr, sz); |
13868 | buf->base.nowarning_flag = 0; |
13869 | expr = (tree) buf; |
13870 | } |
13871 | md5_process_bytes (expr, tree_size (expr), ctx); |
13872 | if (CODE_CONTAINS_STRUCT (code, TS_TYPED)) |
13873 | fold_checksum_tree (TREE_TYPE (expr), ctx, ht); |
13874 | if (TREE_CODE_CLASS (code) != tcc_type |
13875 | && TREE_CODE_CLASS (code) != tcc_declaration |
13876 | && code != TREE_LIST |
13877 | && code != SSA_NAME |
13878 | && CODE_CONTAINS_STRUCT (code, TS_COMMON)) |
13879 | fold_checksum_tree (TREE_CHAIN (expr), ctx, ht); |
13880 | switch (TREE_CODE_CLASS (code)) |
13881 | { |
13882 | case tcc_constant: |
13883 | switch (code) |
13884 | { |
13885 | case STRING_CST: |
13886 | md5_process_bytes (TREE_STRING_POINTER (expr), |
13887 | TREE_STRING_LENGTH (expr), ctx); |
13888 | break; |
13889 | case COMPLEX_CST: |
13890 | fold_checksum_tree (TREE_REALPART (expr), ctx, ht); |
13891 | fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht); |
13892 | break; |
13893 | case VECTOR_CST: |
13894 | len = vector_cst_encoded_nelts (expr); |
13895 | for (i = 0; i < len; ++i) |
13896 | fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht); |
13897 | break; |
13898 | default: |
13899 | break; |
13900 | } |
13901 | break; |
13902 | case tcc_exceptional: |
13903 | switch (code) |
13904 | { |
13905 | case TREE_LIST: |
13906 | fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht); |
13907 | fold_checksum_tree (TREE_VALUE (expr), ctx, ht); |
13908 | expr = TREE_CHAIN (expr); |
13909 | goto recursive_label; |
13910 | break; |
13911 | case TREE_VEC: |
13912 | for (i = 0; i < TREE_VEC_LENGTH (expr); ++i) |
13913 | fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht); |
13914 | break; |
13915 | default: |
13916 | break; |
13917 | } |
13918 | break; |
13919 | case tcc_expression: |
13920 | case tcc_reference: |
13921 | case tcc_comparison: |
13922 | case tcc_unary: |
13923 | case tcc_binary: |
13924 | case tcc_statement: |
13925 | case tcc_vl_exp: |
13926 | len = TREE_OPERAND_LENGTH (expr); |
13927 | for (i = 0; i < len; ++i) |
13928 | fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht); |
13929 | break; |
13930 | case tcc_declaration: |
13931 | fold_checksum_tree (DECL_NAME (expr), ctx, ht); |
13932 | fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht); |
13933 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON)) |
13934 | { |
13935 | fold_checksum_tree (DECL_SIZE (expr), ctx, ht); |
13936 | fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht); |
13937 | fold_checksum_tree (DECL_INITIAL (expr), ctx, ht); |
13938 | fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht); |
13939 | fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht); |
13940 | } |
13941 | |
13942 | if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON)) |
13943 | { |
13944 | if (TREE_CODE (expr) == FUNCTION_DECL) |
13945 | { |
13946 | fold_checksum_tree (DECL_VINDEX (expr), ctx, ht); |
13947 | fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht); |
13948 | } |
13949 | fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht); |
13950 | } |
13951 | break; |
13952 | case tcc_type: |
13953 | if (TREE_CODE (expr) == ENUMERAL_TYPE) |
13954 | fold_checksum_tree (TYPE_VALUES (expr), ctx, ht); |
13955 | fold_checksum_tree (TYPE_SIZE (expr), ctx, ht); |
13956 | fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht); |
13957 | fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht); |
13958 | fold_checksum_tree (TYPE_NAME (expr), ctx, ht); |
13959 | if (INTEGRAL_TYPE_P (expr) |
13960 | || SCALAR_FLOAT_TYPE_P (expr)) |
13961 | { |
13962 | fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht); |
13963 | fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht); |
13964 | } |
13965 | fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht); |
13966 | if (RECORD_OR_UNION_TYPE_P (expr)) |
13967 | fold_checksum_tree (TYPE_BINFO (expr), ctx, ht); |
13968 | fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht); |
13969 | break; |
13970 | default: |
13971 | break; |
13972 | } |
13973 | } |
13974 | |
13975 | /* Helper function for outputting the checksum of a tree T. When |
13976 | debugging with gdb, you can "define mynext" to be "next" followed |
13977 | by "call debug_fold_checksum (op0)", then just trace down till the |
13978 | outputs differ. */ |
13979 | |
13980 | DEBUG_FUNCTION void |
13981 | debug_fold_checksum (const_tree t) |
13982 | { |
13983 | int i; |
13984 | unsigned char checksum[16]; |
13985 | struct md5_ctx ctx; |
13986 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
13987 | |
13988 | md5_init_ctx (&ctx); |
13989 | fold_checksum_tree (t, &ctx, &ht); |
13990 | md5_finish_ctx (&ctx, checksum); |
13991 | ht.empty (); |
13992 | |
13993 | for (i = 0; i < 16; i++) |
13994 | fprintf (stderr, "%d " , checksum[i]); |
13995 | |
13996 | fprintf (stderr, "\n" ); |
13997 | } |
13998 | |
13999 | #endif |
14000 | |
14001 | /* Fold a unary tree expression with code CODE of type TYPE with an |
14002 | operand OP0. LOC is the location of the resulting expression. |
14003 | Return a folded expression if successful. Otherwise, return a tree |
14004 | expression with code CODE of type TYPE with an operand OP0. */ |
14005 | |
14006 | tree |
14007 | fold_build1_loc (location_t loc, |
14008 | enum tree_code code, tree type, tree op0 MEM_STAT_DECL) |
14009 | { |
14010 | tree tem; |
14011 | #ifdef ENABLE_FOLD_CHECKING |
14012 | unsigned char checksum_before[16], checksum_after[16]; |
14013 | struct md5_ctx ctx; |
14014 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
14015 | |
14016 | md5_init_ctx (&ctx); |
14017 | fold_checksum_tree (op0, &ctx, &ht); |
14018 | md5_finish_ctx (&ctx, checksum_before); |
14019 | ht.empty (); |
14020 | #endif |
14021 | |
14022 | tem = fold_unary_loc (loc, code, type, op0); |
14023 | if (!tem) |
14024 | tem = build1_loc (loc, code, type, arg1: op0 PASS_MEM_STAT); |
14025 | |
14026 | #ifdef ENABLE_FOLD_CHECKING |
14027 | md5_init_ctx (&ctx); |
14028 | fold_checksum_tree (op0, &ctx, &ht); |
14029 | md5_finish_ctx (&ctx, checksum_after); |
14030 | |
14031 | if (memcmp (checksum_before, checksum_after, 16)) |
14032 | fold_check_failed (op0, tem); |
14033 | #endif |
14034 | return tem; |
14035 | } |
14036 | |
14037 | /* Fold a binary tree expression with code CODE of type TYPE with |
14038 | operands OP0 and OP1. LOC is the location of the resulting |
14039 | expression. Return a folded expression if successful. Otherwise, |
14040 | return a tree expression with code CODE of type TYPE with operands |
14041 | OP0 and OP1. */ |
14042 | |
14043 | tree |
14044 | fold_build2_loc (location_t loc, |
14045 | enum tree_code code, tree type, tree op0, tree op1 |
14046 | MEM_STAT_DECL) |
14047 | { |
14048 | tree tem; |
14049 | #ifdef ENABLE_FOLD_CHECKING |
14050 | unsigned char checksum_before_op0[16], |
14051 | checksum_before_op1[16], |
14052 | checksum_after_op0[16], |
14053 | checksum_after_op1[16]; |
14054 | struct md5_ctx ctx; |
14055 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
14056 | |
14057 | md5_init_ctx (&ctx); |
14058 | fold_checksum_tree (op0, &ctx, &ht); |
14059 | md5_finish_ctx (&ctx, checksum_before_op0); |
14060 | ht.empty (); |
14061 | |
14062 | md5_init_ctx (&ctx); |
14063 | fold_checksum_tree (op1, &ctx, &ht); |
14064 | md5_finish_ctx (&ctx, checksum_before_op1); |
14065 | ht.empty (); |
14066 | #endif |
14067 | |
14068 | tem = fold_binary_loc (loc, code, type, op0, op1); |
14069 | if (!tem) |
14070 | tem = build2_loc (loc, code, type, arg0: op0, arg1: op1 PASS_MEM_STAT); |
14071 | |
14072 | #ifdef ENABLE_FOLD_CHECKING |
14073 | md5_init_ctx (&ctx); |
14074 | fold_checksum_tree (op0, &ctx, &ht); |
14075 | md5_finish_ctx (&ctx, checksum_after_op0); |
14076 | ht.empty (); |
14077 | |
14078 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) |
14079 | fold_check_failed (op0, tem); |
14080 | |
14081 | md5_init_ctx (&ctx); |
14082 | fold_checksum_tree (op1, &ctx, &ht); |
14083 | md5_finish_ctx (&ctx, checksum_after_op1); |
14084 | |
14085 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) |
14086 | fold_check_failed (op1, tem); |
14087 | #endif |
14088 | return tem; |
14089 | } |
14090 | |
14091 | /* Fold a ternary tree expression with code CODE of type TYPE with |
14092 | operands OP0, OP1, and OP2. Return a folded expression if |
14093 | successful. Otherwise, return a tree expression with code CODE of |
14094 | type TYPE with operands OP0, OP1, and OP2. */ |
14095 | |
14096 | tree |
14097 | fold_build3_loc (location_t loc, enum tree_code code, tree type, |
14098 | tree op0, tree op1, tree op2 MEM_STAT_DECL) |
14099 | { |
14100 | tree tem; |
14101 | #ifdef ENABLE_FOLD_CHECKING |
14102 | unsigned char checksum_before_op0[16], |
14103 | checksum_before_op1[16], |
14104 | checksum_before_op2[16], |
14105 | checksum_after_op0[16], |
14106 | checksum_after_op1[16], |
14107 | checksum_after_op2[16]; |
14108 | struct md5_ctx ctx; |
14109 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
14110 | |
14111 | md5_init_ctx (&ctx); |
14112 | fold_checksum_tree (op0, &ctx, &ht); |
14113 | md5_finish_ctx (&ctx, checksum_before_op0); |
14114 | ht.empty (); |
14115 | |
14116 | md5_init_ctx (&ctx); |
14117 | fold_checksum_tree (op1, &ctx, &ht); |
14118 | md5_finish_ctx (&ctx, checksum_before_op1); |
14119 | ht.empty (); |
14120 | |
14121 | md5_init_ctx (&ctx); |
14122 | fold_checksum_tree (op2, &ctx, &ht); |
14123 | md5_finish_ctx (&ctx, checksum_before_op2); |
14124 | ht.empty (); |
14125 | #endif |
14126 | |
14127 | gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp); |
14128 | tem = fold_ternary_loc (loc, code, type, op0, op1, op2); |
14129 | if (!tem) |
14130 | tem = build3_loc (loc, code, type, arg0: op0, arg1: op1, arg2: op2 PASS_MEM_STAT); |
14131 | |
14132 | #ifdef ENABLE_FOLD_CHECKING |
14133 | md5_init_ctx (&ctx); |
14134 | fold_checksum_tree (op0, &ctx, &ht); |
14135 | md5_finish_ctx (&ctx, checksum_after_op0); |
14136 | ht.empty (); |
14137 | |
14138 | if (memcmp (checksum_before_op0, checksum_after_op0, 16)) |
14139 | fold_check_failed (op0, tem); |
14140 | |
14141 | md5_init_ctx (&ctx); |
14142 | fold_checksum_tree (op1, &ctx, &ht); |
14143 | md5_finish_ctx (&ctx, checksum_after_op1); |
14144 | ht.empty (); |
14145 | |
14146 | if (memcmp (checksum_before_op1, checksum_after_op1, 16)) |
14147 | fold_check_failed (op1, tem); |
14148 | |
14149 | md5_init_ctx (&ctx); |
14150 | fold_checksum_tree (op2, &ctx, &ht); |
14151 | md5_finish_ctx (&ctx, checksum_after_op2); |
14152 | |
14153 | if (memcmp (checksum_before_op2, checksum_after_op2, 16)) |
14154 | fold_check_failed (op2, tem); |
14155 | #endif |
14156 | return tem; |
14157 | } |
14158 | |
14159 | /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS |
14160 | arguments in ARGARRAY, and a null static chain. |
14161 | Return a folded expression if successful. Otherwise, return a CALL_EXPR |
14162 | of type TYPE from the given operands as constructed by build_call_array. */ |
14163 | |
14164 | tree |
14165 | fold_build_call_array_loc (location_t loc, tree type, tree fn, |
14166 | int nargs, tree *argarray) |
14167 | { |
14168 | tree tem; |
14169 | #ifdef ENABLE_FOLD_CHECKING |
14170 | unsigned char checksum_before_fn[16], |
14171 | checksum_before_arglist[16], |
14172 | checksum_after_fn[16], |
14173 | checksum_after_arglist[16]; |
14174 | struct md5_ctx ctx; |
14175 | hash_table<nofree_ptr_hash<const tree_node> > ht (32); |
14176 | int i; |
14177 | |
14178 | md5_init_ctx (&ctx); |
14179 | fold_checksum_tree (fn, &ctx, &ht); |
14180 | md5_finish_ctx (&ctx, checksum_before_fn); |
14181 | ht.empty (); |
14182 | |
14183 | md5_init_ctx (&ctx); |
14184 | for (i = 0; i < nargs; i++) |
14185 | fold_checksum_tree (argarray[i], &ctx, &ht); |
14186 | md5_finish_ctx (&ctx, checksum_before_arglist); |
14187 | ht.empty (); |
14188 | #endif |
14189 | |
14190 | tem = fold_builtin_call_array (loc, type, fn, nargs, argarray); |
14191 | if (!tem) |
14192 | tem = build_call_array_loc (loc, type, fn, nargs, argarray); |
14193 | |
14194 | #ifdef ENABLE_FOLD_CHECKING |
14195 | md5_init_ctx (&ctx); |
14196 | fold_checksum_tree (fn, &ctx, &ht); |
14197 | md5_finish_ctx (&ctx, checksum_after_fn); |
14198 | ht.empty (); |
14199 | |
14200 | if (memcmp (checksum_before_fn, checksum_after_fn, 16)) |
14201 | fold_check_failed (fn, tem); |
14202 | |
14203 | md5_init_ctx (&ctx); |
14204 | for (i = 0; i < nargs; i++) |
14205 | fold_checksum_tree (argarray[i], &ctx, &ht); |
14206 | md5_finish_ctx (&ctx, checksum_after_arglist); |
14207 | |
14208 | if (memcmp (checksum_before_arglist, checksum_after_arglist, 16)) |
14209 | fold_check_failed (NULL_TREE, tem); |
14210 | #endif |
14211 | return tem; |
14212 | } |
14213 | |
14214 | /* Perform constant folding and related simplification of initializer |
14215 | expression EXPR. These behave identically to "fold_buildN" but ignore |
14216 | potential run-time traps and exceptions that fold must preserve. */ |
14217 | |
14218 | #define START_FOLD_INIT \ |
14219 | int saved_signaling_nans = flag_signaling_nans;\ |
14220 | int saved_trapping_math = flag_trapping_math;\ |
14221 | int saved_rounding_math = flag_rounding_math;\ |
14222 | int saved_trapv = flag_trapv;\ |
14223 | int saved_folding_initializer = folding_initializer;\ |
14224 | flag_signaling_nans = 0;\ |
14225 | flag_trapping_math = 0;\ |
14226 | flag_rounding_math = 0;\ |
14227 | flag_trapv = 0;\ |
14228 | folding_initializer = 1; |
14229 | |
14230 | #define END_FOLD_INIT \ |
14231 | flag_signaling_nans = saved_signaling_nans;\ |
14232 | flag_trapping_math = saved_trapping_math;\ |
14233 | flag_rounding_math = saved_rounding_math;\ |
14234 | flag_trapv = saved_trapv;\ |
14235 | folding_initializer = saved_folding_initializer; |
14236 | |
14237 | tree |
14238 | fold_init (tree expr) |
14239 | { |
14240 | tree result; |
14241 | START_FOLD_INIT; |
14242 | |
14243 | result = fold (expr); |
14244 | |
14245 | END_FOLD_INIT; |
14246 | return result; |
14247 | } |
14248 | |
14249 | tree |
14250 | fold_build1_initializer_loc (location_t loc, enum tree_code code, |
14251 | tree type, tree op) |
14252 | { |
14253 | tree result; |
14254 | START_FOLD_INIT; |
14255 | |
14256 | result = fold_build1_loc (loc, code, type, op0: op); |
14257 | |
14258 | END_FOLD_INIT; |
14259 | return result; |
14260 | } |
14261 | |
14262 | tree |
14263 | fold_build2_initializer_loc (location_t loc, enum tree_code code, |
14264 | tree type, tree op0, tree op1) |
14265 | { |
14266 | tree result; |
14267 | START_FOLD_INIT; |
14268 | |
14269 | result = fold_build2_loc (loc, code, type, op0, op1); |
14270 | |
14271 | END_FOLD_INIT; |
14272 | return result; |
14273 | } |
14274 | |
14275 | tree |
14276 | fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn, |
14277 | int nargs, tree *argarray) |
14278 | { |
14279 | tree result; |
14280 | START_FOLD_INIT; |
14281 | |
14282 | result = fold_build_call_array_loc (loc, type, fn, nargs, argarray); |
14283 | |
14284 | END_FOLD_INIT; |
14285 | return result; |
14286 | } |
14287 | |
14288 | tree |
14289 | fold_binary_initializer_loc (location_t loc, tree_code code, tree type, |
14290 | tree lhs, tree rhs) |
14291 | { |
14292 | tree result; |
14293 | START_FOLD_INIT; |
14294 | |
14295 | result = fold_binary_loc (loc, code, type, op0: lhs, op1: rhs); |
14296 | |
14297 | END_FOLD_INIT; |
14298 | return result; |
14299 | } |
14300 | |
14301 | #undef START_FOLD_INIT |
14302 | #undef END_FOLD_INIT |
14303 | |
14304 | /* Determine if first argument is a multiple of second argument. Return |
14305 | false if it is not, or we cannot easily determined it to be. |
14306 | |
14307 | An example of the sort of thing we care about (at this point; this routine |
14308 | could surely be made more general, and expanded to do what the *_DIV_EXPR's |
14309 | fold cases do now) is discovering that |
14310 | |
14311 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) |
14312 | |
14313 | is a multiple of |
14314 | |
14315 | SAVE_EXPR (J * 8) |
14316 | |
14317 | when we know that the two SAVE_EXPR (J * 8) nodes are the same node. |
14318 | |
14319 | This code also handles discovering that |
14320 | |
14321 | SAVE_EXPR (I) * SAVE_EXPR (J * 8) |
14322 | |
14323 | is a multiple of 8 so we don't have to worry about dealing with a |
14324 | possible remainder. |
14325 | |
14326 | Note that we *look* inside a SAVE_EXPR only to determine how it was |
14327 | calculated; it is not safe for fold to do much of anything else with the |
14328 | internals of a SAVE_EXPR, since it cannot know when it will be evaluated |
14329 | at run time. For example, the latter example above *cannot* be implemented |
14330 | as SAVE_EXPR (I) * J or any variant thereof, since the value of J at |
14331 | evaluation time of the original SAVE_EXPR is not necessarily the same at |
14332 | the time the new expression is evaluated. The only optimization of this |
14333 | sort that would be valid is changing |
14334 | |
14335 | SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8) |
14336 | |
14337 | divided by 8 to |
14338 | |
14339 | SAVE_EXPR (I) * SAVE_EXPR (J) |
14340 | |
14341 | (where the same SAVE_EXPR (J) is used in the original and the |
14342 | transformed version). |
14343 | |
14344 | NOWRAP specifies whether all outer operations in TYPE should |
14345 | be considered not wrapping. Any type conversion within TOP acts |
14346 | as a barrier and we will fall back to NOWRAP being false. |
14347 | NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends |
14348 | as not wrapping even though they are generally using unsigned arithmetic. */ |
14349 | |
14350 | bool |
14351 | multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap) |
14352 | { |
14353 | gimple *stmt; |
14354 | tree op1, op2; |
14355 | |
14356 | if (operand_equal_p (arg0: top, arg1: bottom, flags: 0)) |
14357 | return true; |
14358 | |
14359 | if (TREE_CODE (type) != INTEGER_TYPE) |
14360 | return false; |
14361 | |
14362 | switch (TREE_CODE (top)) |
14363 | { |
14364 | case BIT_AND_EXPR: |
14365 | /* Bitwise and provides a power of two multiple. If the mask is |
14366 | a multiple of BOTTOM then TOP is a multiple of BOTTOM. */ |
14367 | if (!integer_pow2p (bottom)) |
14368 | return false; |
14369 | return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap) |
14370 | || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap)); |
14371 | |
14372 | case MULT_EXPR: |
14373 | /* If the multiplication can wrap we cannot recurse further unless |
14374 | the bottom is a power of two which is where wrapping does not |
14375 | matter. */ |
14376 | if (!nowrap |
14377 | && !TYPE_OVERFLOW_UNDEFINED (type) |
14378 | && !integer_pow2p (bottom)) |
14379 | return false; |
14380 | if (TREE_CODE (bottom) == INTEGER_CST) |
14381 | { |
14382 | op1 = TREE_OPERAND (top, 0); |
14383 | op2 = TREE_OPERAND (top, 1); |
14384 | if (TREE_CODE (op1) == INTEGER_CST) |
14385 | std::swap (a&: op1, b&: op2); |
14386 | if (TREE_CODE (op2) == INTEGER_CST) |
14387 | { |
14388 | if (multiple_of_p (type, top: op2, bottom, nowrap)) |
14389 | return true; |
14390 | /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */ |
14391 | if (multiple_of_p (type, top: bottom, bottom: op2, nowrap)) |
14392 | { |
14393 | widest_int w = wi::sdiv_trunc (x: wi::to_widest (t: bottom), |
14394 | y: wi::to_widest (t: op2)); |
14395 | if (wi::fits_to_tree_p (x: w, TREE_TYPE (bottom))) |
14396 | { |
14397 | op2 = wide_int_to_tree (TREE_TYPE (bottom), cst: w); |
14398 | return multiple_of_p (type, top: op1, bottom: op2, nowrap); |
14399 | } |
14400 | } |
14401 | return multiple_of_p (type, top: op1, bottom, nowrap); |
14402 | } |
14403 | } |
14404 | return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap) |
14405 | || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap)); |
14406 | |
14407 | case LSHIFT_EXPR: |
14408 | /* Handle X << CST as X * (1 << CST) and only process the constant. */ |
14409 | if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST) |
14410 | { |
14411 | op1 = TREE_OPERAND (top, 1); |
14412 | if (wi::to_widest (t: op1) < TYPE_PRECISION (type)) |
14413 | { |
14414 | wide_int mul_op |
14415 | = wi::one (TYPE_PRECISION (type)) << wi::to_wide (t: op1); |
14416 | return multiple_of_p (type, |
14417 | top: wide_int_to_tree (type, cst: mul_op), bottom, |
14418 | nowrap); |
14419 | } |
14420 | } |
14421 | return false; |
14422 | |
14423 | case MINUS_EXPR: |
14424 | case PLUS_EXPR: |
14425 | /* If the addition or subtraction can wrap we cannot recurse further |
14426 | unless bottom is a power of two which is where wrapping does not |
14427 | matter. */ |
14428 | if (!nowrap |
14429 | && !TYPE_OVERFLOW_UNDEFINED (type) |
14430 | && !integer_pow2p (bottom)) |
14431 | return false; |
14432 | |
14433 | /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has |
14434 | unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3, |
14435 | but 0xfffffffd is not. */ |
14436 | op1 = TREE_OPERAND (top, 1); |
14437 | if (TREE_CODE (top) == PLUS_EXPR |
14438 | && nowrap |
14439 | && TYPE_UNSIGNED (type) |
14440 | && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1)) |
14441 | op1 = fold_build1 (NEGATE_EXPR, type, op1); |
14442 | |
14443 | /* It is impossible to prove if op0 +- op1 is multiple of bottom |
14444 | precisely, so be conservative here checking if both op0 and op1 |
14445 | are multiple of bottom. Note we check the second operand first |
14446 | since it's usually simpler. */ |
14447 | return (multiple_of_p (type, top: op1, bottom, nowrap) |
14448 | && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap)); |
14449 | |
14450 | CASE_CONVERT: |
14451 | /* Can't handle conversions from non-integral or wider integral type. */ |
14452 | if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE) |
14453 | || (TYPE_PRECISION (type) |
14454 | < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0))))) |
14455 | return false; |
14456 | /* NOWRAP only extends to operations in the outermost type so |
14457 | make sure to strip it off here. */ |
14458 | return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)), |
14459 | TREE_OPERAND (top, 0), bottom, nowrap: false); |
14460 | |
14461 | case SAVE_EXPR: |
14462 | return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap); |
14463 | |
14464 | case COND_EXPR: |
14465 | return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap) |
14466 | && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap)); |
14467 | |
14468 | case INTEGER_CST: |
14469 | if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom)) |
14470 | return false; |
14471 | return wi::multiple_of_p (x: wi::to_widest (t: top), y: wi::to_widest (t: bottom), |
14472 | sgn: SIGNED); |
14473 | |
14474 | case SSA_NAME: |
14475 | if (TREE_CODE (bottom) == INTEGER_CST |
14476 | && (stmt = SSA_NAME_DEF_STMT (top)) != NULL |
14477 | && gimple_code (g: stmt) == GIMPLE_ASSIGN) |
14478 | { |
14479 | enum tree_code code = gimple_assign_rhs_code (gs: stmt); |
14480 | |
14481 | /* Check for special cases to see if top is defined as multiple |
14482 | of bottom: |
14483 | |
14484 | top = (X & ~(bottom - 1) ; bottom is power of 2 |
14485 | |
14486 | or |
14487 | |
14488 | Y = X % bottom |
14489 | top = X - Y. */ |
14490 | if (code == BIT_AND_EXPR |
14491 | && (op2 = gimple_assign_rhs2 (gs: stmt)) != NULL_TREE |
14492 | && TREE_CODE (op2) == INTEGER_CST |
14493 | && integer_pow2p (bottom) |
14494 | && wi::multiple_of_p (x: wi::to_widest (t: op2), |
14495 | y: wi::to_widest (t: bottom), sgn: UNSIGNED)) |
14496 | return true; |
14497 | |
14498 | op1 = gimple_assign_rhs1 (gs: stmt); |
14499 | if (code == MINUS_EXPR |
14500 | && (op2 = gimple_assign_rhs2 (gs: stmt)) != NULL_TREE |
14501 | && TREE_CODE (op2) == SSA_NAME |
14502 | && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL |
14503 | && gimple_code (g: stmt) == GIMPLE_ASSIGN |
14504 | && (code = gimple_assign_rhs_code (gs: stmt)) == TRUNC_MOD_EXPR |
14505 | && operand_equal_p (arg0: op1, arg1: gimple_assign_rhs1 (gs: stmt), flags: 0) |
14506 | && operand_equal_p (arg0: bottom, arg1: gimple_assign_rhs2 (gs: stmt), flags: 0)) |
14507 | return true; |
14508 | } |
14509 | |
14510 | /* fall through */ |
14511 | |
14512 | default: |
14513 | if (POLY_INT_CST_P (top) && poly_int_tree_p (t: bottom)) |
14514 | return multiple_p (a: wi::to_poly_widest (t: top), |
14515 | b: wi::to_poly_widest (t: bottom)); |
14516 | |
14517 | return false; |
14518 | } |
14519 | } |
14520 | |
14521 | /* Return true if expression X cannot be (or contain) a NaN or infinity. |
14522 | This function returns true for integer expressions, and returns |
14523 | false if uncertain. */ |
14524 | |
14525 | bool |
14526 | tree_expr_finite_p (const_tree x) |
14527 | { |
14528 | machine_mode mode = element_mode (x); |
14529 | if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode)) |
14530 | return true; |
14531 | switch (TREE_CODE (x)) |
14532 | { |
14533 | case REAL_CST: |
14534 | return real_isfinite (TREE_REAL_CST_PTR (x)); |
14535 | case COMPLEX_CST: |
14536 | return tree_expr_finite_p (TREE_REALPART (x)) |
14537 | && tree_expr_finite_p (TREE_IMAGPART (x)); |
14538 | case FLOAT_EXPR: |
14539 | return true; |
14540 | case ABS_EXPR: |
14541 | case CONVERT_EXPR: |
14542 | case NON_LVALUE_EXPR: |
14543 | case NEGATE_EXPR: |
14544 | case SAVE_EXPR: |
14545 | return tree_expr_finite_p (TREE_OPERAND (x, 0)); |
14546 | case MIN_EXPR: |
14547 | case MAX_EXPR: |
14548 | return tree_expr_finite_p (TREE_OPERAND (x, 0)) |
14549 | && tree_expr_finite_p (TREE_OPERAND (x, 1)); |
14550 | case COND_EXPR: |
14551 | return tree_expr_finite_p (TREE_OPERAND (x, 1)) |
14552 | && tree_expr_finite_p (TREE_OPERAND (x, 2)); |
14553 | case CALL_EXPR: |
14554 | switch (get_call_combined_fn (x)) |
14555 | { |
14556 | CASE_CFN_FABS: |
14557 | CASE_CFN_FABS_FN: |
14558 | return tree_expr_finite_p (CALL_EXPR_ARG (x, 0)); |
14559 | CASE_CFN_FMAX: |
14560 | CASE_CFN_FMAX_FN: |
14561 | CASE_CFN_FMIN: |
14562 | CASE_CFN_FMIN_FN: |
14563 | return tree_expr_finite_p (CALL_EXPR_ARG (x, 0)) |
14564 | && tree_expr_finite_p (CALL_EXPR_ARG (x, 1)); |
14565 | default: |
14566 | return false; |
14567 | } |
14568 | |
14569 | default: |
14570 | return false; |
14571 | } |
14572 | } |
14573 | |
14574 | /* Return true if expression X evaluates to an infinity. |
14575 | This function returns false for integer expressions. */ |
14576 | |
14577 | bool |
14578 | tree_expr_infinite_p (const_tree x) |
14579 | { |
14580 | if (!HONOR_INFINITIES (x)) |
14581 | return false; |
14582 | switch (TREE_CODE (x)) |
14583 | { |
14584 | case REAL_CST: |
14585 | return real_isinf (TREE_REAL_CST_PTR (x)); |
14586 | case ABS_EXPR: |
14587 | case NEGATE_EXPR: |
14588 | case NON_LVALUE_EXPR: |
14589 | case SAVE_EXPR: |
14590 | return tree_expr_infinite_p (TREE_OPERAND (x, 0)); |
14591 | case COND_EXPR: |
14592 | return tree_expr_infinite_p (TREE_OPERAND (x, 1)) |
14593 | && tree_expr_infinite_p (TREE_OPERAND (x, 2)); |
14594 | default: |
14595 | return false; |
14596 | } |
14597 | } |
14598 | |
14599 | /* Return true if expression X could evaluate to an infinity. |
14600 | This function returns false for integer expressions, and returns |
14601 | true if uncertain. */ |
14602 | |
14603 | bool |
14604 | tree_expr_maybe_infinite_p (const_tree x) |
14605 | { |
14606 | if (!HONOR_INFINITIES (x)) |
14607 | return false; |
14608 | switch (TREE_CODE (x)) |
14609 | { |
14610 | case REAL_CST: |
14611 | return real_isinf (TREE_REAL_CST_PTR (x)); |
14612 | case FLOAT_EXPR: |
14613 | return false; |
14614 | case ABS_EXPR: |
14615 | case NEGATE_EXPR: |
14616 | return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0)); |
14617 | case COND_EXPR: |
14618 | return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1)) |
14619 | || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2)); |
14620 | default: |
14621 | return true; |
14622 | } |
14623 | } |
14624 | |
14625 | /* Return true if expression X evaluates to a signaling NaN. |
14626 | This function returns false for integer expressions. */ |
14627 | |
14628 | bool |
14629 | tree_expr_signaling_nan_p (const_tree x) |
14630 | { |
14631 | if (!HONOR_SNANS (x)) |
14632 | return false; |
14633 | switch (TREE_CODE (x)) |
14634 | { |
14635 | case REAL_CST: |
14636 | return real_issignaling_nan (TREE_REAL_CST_PTR (x)); |
14637 | case NON_LVALUE_EXPR: |
14638 | case SAVE_EXPR: |
14639 | return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0)); |
14640 | case COND_EXPR: |
14641 | return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1)) |
14642 | && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2)); |
14643 | default: |
14644 | return false; |
14645 | } |
14646 | } |
14647 | |
14648 | /* Return true if expression X could evaluate to a signaling NaN. |
14649 | This function returns false for integer expressions, and returns |
14650 | true if uncertain. */ |
14651 | |
14652 | bool |
14653 | tree_expr_maybe_signaling_nan_p (const_tree x) |
14654 | { |
14655 | if (!HONOR_SNANS (x)) |
14656 | return false; |
14657 | switch (TREE_CODE (x)) |
14658 | { |
14659 | case REAL_CST: |
14660 | return real_issignaling_nan (TREE_REAL_CST_PTR (x)); |
14661 | case FLOAT_EXPR: |
14662 | return false; |
14663 | case ABS_EXPR: |
14664 | case CONVERT_EXPR: |
14665 | case NEGATE_EXPR: |
14666 | case NON_LVALUE_EXPR: |
14667 | case SAVE_EXPR: |
14668 | return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0)); |
14669 | case MIN_EXPR: |
14670 | case MAX_EXPR: |
14671 | return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0)) |
14672 | || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1)); |
14673 | case COND_EXPR: |
14674 | return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1)) |
14675 | || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2)); |
14676 | case CALL_EXPR: |
14677 | switch (get_call_combined_fn (x)) |
14678 | { |
14679 | CASE_CFN_FABS: |
14680 | CASE_CFN_FABS_FN: |
14681 | return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0)); |
14682 | CASE_CFN_FMAX: |
14683 | CASE_CFN_FMAX_FN: |
14684 | CASE_CFN_FMIN: |
14685 | CASE_CFN_FMIN_FN: |
14686 | return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0)) |
14687 | || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1)); |
14688 | default: |
14689 | return true; |
14690 | } |
14691 | default: |
14692 | return true; |
14693 | } |
14694 | } |
14695 | |
14696 | /* Return true if expression X evaluates to a NaN. |
14697 | This function returns false for integer expressions. */ |
14698 | |
14699 | bool |
14700 | tree_expr_nan_p (const_tree x) |
14701 | { |
14702 | if (!HONOR_NANS (x)) |
14703 | return false; |
14704 | switch (TREE_CODE (x)) |
14705 | { |
14706 | case REAL_CST: |
14707 | return real_isnan (TREE_REAL_CST_PTR (x)); |
14708 | case NON_LVALUE_EXPR: |
14709 | case SAVE_EXPR: |
14710 | return tree_expr_nan_p (TREE_OPERAND (x, 0)); |
14711 | case COND_EXPR: |
14712 | return tree_expr_nan_p (TREE_OPERAND (x, 1)) |
14713 | && tree_expr_nan_p (TREE_OPERAND (x, 2)); |
14714 | default: |
14715 | return false; |
14716 | } |
14717 | } |
14718 | |
14719 | /* Return true if expression X could evaluate to a NaN. |
14720 | This function returns false for integer expressions, and returns |
14721 | true if uncertain. */ |
14722 | |
14723 | bool |
14724 | tree_expr_maybe_nan_p (const_tree x) |
14725 | { |
14726 | if (!HONOR_NANS (x)) |
14727 | return false; |
14728 | switch (TREE_CODE (x)) |
14729 | { |
14730 | case REAL_CST: |
14731 | return real_isnan (TREE_REAL_CST_PTR (x)); |
14732 | case FLOAT_EXPR: |
14733 | return false; |
14734 | case PLUS_EXPR: |
14735 | case MINUS_EXPR: |
14736 | case MULT_EXPR: |
14737 | return !tree_expr_finite_p (TREE_OPERAND (x, 0)) |
14738 | || !tree_expr_finite_p (TREE_OPERAND (x, 1)); |
14739 | case ABS_EXPR: |
14740 | case CONVERT_EXPR: |
14741 | case NEGATE_EXPR: |
14742 | case NON_LVALUE_EXPR: |
14743 | case SAVE_EXPR: |
14744 | return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0)); |
14745 | case MIN_EXPR: |
14746 | case MAX_EXPR: |
14747 | return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0)) |
14748 | || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1)); |
14749 | case COND_EXPR: |
14750 | return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1)) |
14751 | || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2)); |
14752 | case CALL_EXPR: |
14753 | switch (get_call_combined_fn (x)) |
14754 | { |
14755 | CASE_CFN_FABS: |
14756 | CASE_CFN_FABS_FN: |
14757 | return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0)); |
14758 | CASE_CFN_FMAX: |
14759 | CASE_CFN_FMAX_FN: |
14760 | CASE_CFN_FMIN: |
14761 | CASE_CFN_FMIN_FN: |
14762 | return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0)) |
14763 | || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1)); |
14764 | default: |
14765 | return true; |
14766 | } |
14767 | default: |
14768 | return true; |
14769 | } |
14770 | } |
14771 | |
14772 | /* Return true if expression X could evaluate to -0.0. |
14773 | This function returns true if uncertain. */ |
14774 | |
14775 | bool |
14776 | tree_expr_maybe_real_minus_zero_p (const_tree x) |
14777 | { |
14778 | if (!HONOR_SIGNED_ZEROS (x)) |
14779 | return false; |
14780 | switch (TREE_CODE (x)) |
14781 | { |
14782 | case REAL_CST: |
14783 | return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x)); |
14784 | case INTEGER_CST: |
14785 | case FLOAT_EXPR: |
14786 | case ABS_EXPR: |
14787 | return false; |
14788 | case NON_LVALUE_EXPR: |
14789 | case SAVE_EXPR: |
14790 | return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0)); |
14791 | case COND_EXPR: |
14792 | return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1)) |
14793 | || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2)); |
14794 | case CALL_EXPR: |
14795 | switch (get_call_combined_fn (x)) |
14796 | { |
14797 | CASE_CFN_FABS: |
14798 | CASE_CFN_FABS_FN: |
14799 | return false; |
14800 | default: |
14801 | break; |
14802 | } |
14803 | default: |
14804 | break; |
14805 | } |
14806 | /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X)) |
14807 | * but currently those predicates require tree and not const_tree. */ |
14808 | return true; |
14809 | } |
14810 | |
14811 | #define tree_expr_nonnegative_warnv_p(X, Y) \ |
14812 | _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0 |
14813 | |
14814 | #define RECURSE(X) \ |
14815 | ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1)) |
14816 | |
14817 | /* Return true if CODE or TYPE is known to be non-negative. */ |
14818 | |
14819 | static bool |
14820 | tree_simple_nonnegative_warnv_p (enum tree_code code, tree type) |
14821 | { |
14822 | if (!VECTOR_TYPE_P (type) |
14823 | && (TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type)) |
14824 | && truth_value_p (code)) |
14825 | /* Truth values evaluate to 0 or 1, which is nonnegative unless we |
14826 | have a signed:1 type (where the value is -1 and 0). */ |
14827 | return true; |
14828 | return false; |
14829 | } |
14830 | |
14831 | /* Return true if (CODE OP0) is known to be non-negative. If the return |
14832 | value is based on the assumption that signed overflow is undefined, |
14833 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
14834 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
14835 | |
14836 | bool |
14837 | tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
14838 | bool *strict_overflow_p, int depth) |
14839 | { |
14840 | if (TYPE_UNSIGNED (type)) |
14841 | return true; |
14842 | |
14843 | switch (code) |
14844 | { |
14845 | case ABS_EXPR: |
14846 | /* We can't return 1 if flag_wrapv is set because |
14847 | ABS_EXPR<INT_MIN> = INT_MIN. */ |
14848 | if (!ANY_INTEGRAL_TYPE_P (type)) |
14849 | return true; |
14850 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
14851 | { |
14852 | *strict_overflow_p = true; |
14853 | return true; |
14854 | } |
14855 | break; |
14856 | |
14857 | case NON_LVALUE_EXPR: |
14858 | case FLOAT_EXPR: |
14859 | case FIX_TRUNC_EXPR: |
14860 | return RECURSE (op0); |
14861 | |
14862 | CASE_CONVERT: |
14863 | { |
14864 | tree inner_type = TREE_TYPE (op0); |
14865 | tree outer_type = type; |
14866 | |
14867 | if (SCALAR_FLOAT_TYPE_P (outer_type)) |
14868 | { |
14869 | if (SCALAR_FLOAT_TYPE_P (inner_type)) |
14870 | return RECURSE (op0); |
14871 | if (INTEGRAL_TYPE_P (inner_type)) |
14872 | { |
14873 | if (TYPE_UNSIGNED (inner_type)) |
14874 | return true; |
14875 | return RECURSE (op0); |
14876 | } |
14877 | } |
14878 | else if (INTEGRAL_TYPE_P (outer_type)) |
14879 | { |
14880 | if (SCALAR_FLOAT_TYPE_P (inner_type)) |
14881 | return RECURSE (op0); |
14882 | if (INTEGRAL_TYPE_P (inner_type)) |
14883 | return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type) |
14884 | && TYPE_UNSIGNED (inner_type); |
14885 | } |
14886 | } |
14887 | break; |
14888 | |
14889 | default: |
14890 | return tree_simple_nonnegative_warnv_p (code, type); |
14891 | } |
14892 | |
14893 | /* We don't know sign of `t', so be conservative and return false. */ |
14894 | return false; |
14895 | } |
14896 | |
14897 | /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return |
14898 | value is based on the assumption that signed overflow is undefined, |
14899 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
14900 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
14901 | |
14902 | bool |
14903 | tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0, |
14904 | tree op1, bool *strict_overflow_p, |
14905 | int depth) |
14906 | { |
14907 | if (TYPE_UNSIGNED (type)) |
14908 | return true; |
14909 | |
14910 | switch (code) |
14911 | { |
14912 | case POINTER_PLUS_EXPR: |
14913 | case PLUS_EXPR: |
14914 | if (FLOAT_TYPE_P (type)) |
14915 | return RECURSE (op0) && RECURSE (op1); |
14916 | |
14917 | /* zero_extend(x) + zero_extend(y) is non-negative if x and y are |
14918 | both unsigned and at least 2 bits shorter than the result. */ |
14919 | if (TREE_CODE (type) == INTEGER_TYPE |
14920 | && TREE_CODE (op0) == NOP_EXPR |
14921 | && TREE_CODE (op1) == NOP_EXPR) |
14922 | { |
14923 | tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0)); |
14924 | tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0)); |
14925 | if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1) |
14926 | && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2)) |
14927 | { |
14928 | unsigned int prec = MAX (TYPE_PRECISION (inner1), |
14929 | TYPE_PRECISION (inner2)) + 1; |
14930 | return prec < TYPE_PRECISION (type); |
14931 | } |
14932 | } |
14933 | break; |
14934 | |
14935 | case MULT_EXPR: |
14936 | if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type)) |
14937 | { |
14938 | /* x * x is always non-negative for floating point x |
14939 | or without overflow. */ |
14940 | if (operand_equal_p (arg0: op0, arg1: op1, flags: 0) |
14941 | || (RECURSE (op0) && RECURSE (op1))) |
14942 | { |
14943 | if (ANY_INTEGRAL_TYPE_P (type) |
14944 | && TYPE_OVERFLOW_UNDEFINED (type)) |
14945 | *strict_overflow_p = true; |
14946 | return true; |
14947 | } |
14948 | } |
14949 | |
14950 | /* zero_extend(x) * zero_extend(y) is non-negative if x and y are |
14951 | both unsigned and their total bits is shorter than the result. */ |
14952 | if (TREE_CODE (type) == INTEGER_TYPE |
14953 | && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST) |
14954 | && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST)) |
14955 | { |
14956 | tree inner0 = (TREE_CODE (op0) == NOP_EXPR) |
14957 | ? TREE_TYPE (TREE_OPERAND (op0, 0)) |
14958 | : TREE_TYPE (op0); |
14959 | tree inner1 = (TREE_CODE (op1) == NOP_EXPR) |
14960 | ? TREE_TYPE (TREE_OPERAND (op1, 0)) |
14961 | : TREE_TYPE (op1); |
14962 | |
14963 | bool unsigned0 = TYPE_UNSIGNED (inner0); |
14964 | bool unsigned1 = TYPE_UNSIGNED (inner1); |
14965 | |
14966 | if (TREE_CODE (op0) == INTEGER_CST) |
14967 | unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0; |
14968 | |
14969 | if (TREE_CODE (op1) == INTEGER_CST) |
14970 | unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0; |
14971 | |
14972 | if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0 |
14973 | && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1) |
14974 | { |
14975 | unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST) |
14976 | ? tree_int_cst_min_precision (op0, UNSIGNED) |
14977 | : TYPE_PRECISION (inner0); |
14978 | |
14979 | unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST) |
14980 | ? tree_int_cst_min_precision (op1, UNSIGNED) |
14981 | : TYPE_PRECISION (inner1); |
14982 | |
14983 | return precision0 + precision1 < TYPE_PRECISION (type); |
14984 | } |
14985 | } |
14986 | return false; |
14987 | |
14988 | case BIT_AND_EXPR: |
14989 | return RECURSE (op0) || RECURSE (op1); |
14990 | |
14991 | case MAX_EXPR: |
14992 | /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate |
14993 | things. */ |
14994 | if (tree_expr_maybe_nan_p (x: op0) || tree_expr_maybe_nan_p (x: op1)) |
14995 | return RECURSE (op0) && RECURSE (op1); |
14996 | return RECURSE (op0) || RECURSE (op1); |
14997 | |
14998 | case BIT_IOR_EXPR: |
14999 | case BIT_XOR_EXPR: |
15000 | case MIN_EXPR: |
15001 | case RDIV_EXPR: |
15002 | case TRUNC_DIV_EXPR: |
15003 | case CEIL_DIV_EXPR: |
15004 | case FLOOR_DIV_EXPR: |
15005 | case ROUND_DIV_EXPR: |
15006 | return RECURSE (op0) && RECURSE (op1); |
15007 | |
15008 | case TRUNC_MOD_EXPR: |
15009 | return RECURSE (op0); |
15010 | |
15011 | case FLOOR_MOD_EXPR: |
15012 | return RECURSE (op1); |
15013 | |
15014 | case CEIL_MOD_EXPR: |
15015 | case ROUND_MOD_EXPR: |
15016 | default: |
15017 | return tree_simple_nonnegative_warnv_p (code, type); |
15018 | } |
15019 | |
15020 | /* We don't know sign of `t', so be conservative and return false. */ |
15021 | return false; |
15022 | } |
15023 | |
15024 | /* Return true if T is known to be non-negative. If the return |
15025 | value is based on the assumption that signed overflow is undefined, |
15026 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
15027 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
15028 | |
15029 | bool |
15030 | tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth) |
15031 | { |
15032 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
15033 | return true; |
15034 | |
15035 | switch (TREE_CODE (t)) |
15036 | { |
15037 | case INTEGER_CST: |
15038 | return tree_int_cst_sgn (t) >= 0; |
15039 | |
15040 | case REAL_CST: |
15041 | return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t)); |
15042 | |
15043 | case FIXED_CST: |
15044 | return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t)); |
15045 | |
15046 | case COND_EXPR: |
15047 | return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2)); |
15048 | |
15049 | case SSA_NAME: |
15050 | /* Limit the depth of recursion to avoid quadratic behavior. |
15051 | This is expected to catch almost all occurrences in practice. |
15052 | If this code misses important cases that unbounded recursion |
15053 | would not, passes that need this information could be revised |
15054 | to provide it through dataflow propagation. */ |
15055 | return (!name_registered_for_update_p (t) |
15056 | && depth < param_max_ssa_name_query_depth |
15057 | && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t), |
15058 | strict_overflow_p, depth)); |
15059 | |
15060 | default: |
15061 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t)); |
15062 | } |
15063 | } |
15064 | |
15065 | /* Return true if T is known to be non-negative. If the return |
15066 | value is based on the assumption that signed overflow is undefined, |
15067 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
15068 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
15069 | |
15070 | bool |
15071 | tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1, |
15072 | bool *strict_overflow_p, int depth) |
15073 | { |
15074 | switch (fn) |
15075 | { |
15076 | CASE_CFN_ACOS: |
15077 | CASE_CFN_ACOS_FN: |
15078 | CASE_CFN_ACOSH: |
15079 | CASE_CFN_ACOSH_FN: |
15080 | CASE_CFN_CABS: |
15081 | CASE_CFN_CABS_FN: |
15082 | CASE_CFN_COSH: |
15083 | CASE_CFN_COSH_FN: |
15084 | CASE_CFN_ERFC: |
15085 | CASE_CFN_ERFC_FN: |
15086 | CASE_CFN_EXP: |
15087 | CASE_CFN_EXP_FN: |
15088 | CASE_CFN_EXP10: |
15089 | CASE_CFN_EXP2: |
15090 | CASE_CFN_EXP2_FN: |
15091 | CASE_CFN_FABS: |
15092 | CASE_CFN_FABS_FN: |
15093 | CASE_CFN_FDIM: |
15094 | CASE_CFN_FDIM_FN: |
15095 | CASE_CFN_HYPOT: |
15096 | CASE_CFN_HYPOT_FN: |
15097 | CASE_CFN_POW10: |
15098 | CASE_CFN_FFS: |
15099 | CASE_CFN_PARITY: |
15100 | CASE_CFN_POPCOUNT: |
15101 | CASE_CFN_CLZ: |
15102 | CASE_CFN_CLRSB: |
15103 | case CFN_BUILT_IN_BSWAP16: |
15104 | case CFN_BUILT_IN_BSWAP32: |
15105 | case CFN_BUILT_IN_BSWAP64: |
15106 | case CFN_BUILT_IN_BSWAP128: |
15107 | /* Always true. */ |
15108 | return true; |
15109 | |
15110 | CASE_CFN_SQRT: |
15111 | CASE_CFN_SQRT_FN: |
15112 | /* sqrt(-0.0) is -0.0. */ |
15113 | if (!HONOR_SIGNED_ZEROS (type)) |
15114 | return true; |
15115 | return RECURSE (arg0); |
15116 | |
15117 | CASE_CFN_ASINH: |
15118 | CASE_CFN_ASINH_FN: |
15119 | CASE_CFN_ATAN: |
15120 | CASE_CFN_ATAN_FN: |
15121 | CASE_CFN_ATANH: |
15122 | CASE_CFN_ATANH_FN: |
15123 | CASE_CFN_CBRT: |
15124 | CASE_CFN_CBRT_FN: |
15125 | CASE_CFN_CEIL: |
15126 | CASE_CFN_CEIL_FN: |
15127 | CASE_CFN_ERF: |
15128 | CASE_CFN_ERF_FN: |
15129 | CASE_CFN_EXPM1: |
15130 | CASE_CFN_EXPM1_FN: |
15131 | CASE_CFN_FLOOR: |
15132 | CASE_CFN_FLOOR_FN: |
15133 | CASE_CFN_FMOD: |
15134 | CASE_CFN_FMOD_FN: |
15135 | CASE_CFN_FREXP: |
15136 | CASE_CFN_FREXP_FN: |
15137 | CASE_CFN_ICEIL: |
15138 | CASE_CFN_IFLOOR: |
15139 | CASE_CFN_IRINT: |
15140 | CASE_CFN_IROUND: |
15141 | CASE_CFN_LCEIL: |
15142 | CASE_CFN_LDEXP: |
15143 | CASE_CFN_LFLOOR: |
15144 | CASE_CFN_LLCEIL: |
15145 | CASE_CFN_LLFLOOR: |
15146 | CASE_CFN_LLRINT: |
15147 | CASE_CFN_LLRINT_FN: |
15148 | CASE_CFN_LLROUND: |
15149 | CASE_CFN_LLROUND_FN: |
15150 | CASE_CFN_LRINT: |
15151 | CASE_CFN_LRINT_FN: |
15152 | CASE_CFN_LROUND: |
15153 | CASE_CFN_LROUND_FN: |
15154 | CASE_CFN_MODF: |
15155 | CASE_CFN_MODF_FN: |
15156 | CASE_CFN_NEARBYINT: |
15157 | CASE_CFN_NEARBYINT_FN: |
15158 | CASE_CFN_RINT: |
15159 | CASE_CFN_RINT_FN: |
15160 | CASE_CFN_ROUND: |
15161 | CASE_CFN_ROUND_FN: |
15162 | CASE_CFN_ROUNDEVEN: |
15163 | CASE_CFN_ROUNDEVEN_FN: |
15164 | CASE_CFN_SCALB: |
15165 | CASE_CFN_SCALBLN: |
15166 | CASE_CFN_SCALBLN_FN: |
15167 | CASE_CFN_SCALBN: |
15168 | CASE_CFN_SCALBN_FN: |
15169 | CASE_CFN_SIGNBIT: |
15170 | CASE_CFN_SIGNIFICAND: |
15171 | CASE_CFN_SINH: |
15172 | CASE_CFN_SINH_FN: |
15173 | CASE_CFN_TANH: |
15174 | CASE_CFN_TANH_FN: |
15175 | CASE_CFN_TRUNC: |
15176 | CASE_CFN_TRUNC_FN: |
15177 | /* True if the 1st argument is nonnegative. */ |
15178 | return RECURSE (arg0); |
15179 | |
15180 | CASE_CFN_FMAX: |
15181 | CASE_CFN_FMAX_FN: |
15182 | /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate |
15183 | things. In the presence of sNaNs, we're only guaranteed to be |
15184 | non-negative if both operands are non-negative. In the presence |
15185 | of qNaNs, we're non-negative if either operand is non-negative |
15186 | and can't be a qNaN, or if both operands are non-negative. */ |
15187 | if (tree_expr_maybe_signaling_nan_p (x: arg0) || |
15188 | tree_expr_maybe_signaling_nan_p (x: arg1)) |
15189 | return RECURSE (arg0) && RECURSE (arg1); |
15190 | return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (x: arg0) |
15191 | || RECURSE (arg1)) |
15192 | : (RECURSE (arg1) |
15193 | && !tree_expr_maybe_nan_p (x: arg1)); |
15194 | |
15195 | CASE_CFN_FMIN: |
15196 | CASE_CFN_FMIN_FN: |
15197 | /* True if the 1st AND 2nd arguments are nonnegative. */ |
15198 | return RECURSE (arg0) && RECURSE (arg1); |
15199 | |
15200 | CASE_CFN_COPYSIGN: |
15201 | CASE_CFN_COPYSIGN_FN: |
15202 | /* True if the 2nd argument is nonnegative. */ |
15203 | return RECURSE (arg1); |
15204 | |
15205 | CASE_CFN_POWI: |
15206 | /* True if the 1st argument is nonnegative or the second |
15207 | argument is an even integer. */ |
15208 | if (TREE_CODE (arg1) == INTEGER_CST |
15209 | && (TREE_INT_CST_LOW (arg1) & 1) == 0) |
15210 | return true; |
15211 | return RECURSE (arg0); |
15212 | |
15213 | CASE_CFN_POW: |
15214 | CASE_CFN_POW_FN: |
15215 | /* True if the 1st argument is nonnegative or the second |
15216 | argument is an even integer valued real. */ |
15217 | if (TREE_CODE (arg1) == REAL_CST) |
15218 | { |
15219 | REAL_VALUE_TYPE c; |
15220 | HOST_WIDE_INT n; |
15221 | |
15222 | c = TREE_REAL_CST (arg1); |
15223 | n = real_to_integer (&c); |
15224 | if ((n & 1) == 0) |
15225 | { |
15226 | REAL_VALUE_TYPE cint; |
15227 | real_from_integer (&cint, VOIDmode, n, SIGNED); |
15228 | if (real_identical (&c, &cint)) |
15229 | return true; |
15230 | } |
15231 | } |
15232 | return RECURSE (arg0); |
15233 | |
15234 | default: |
15235 | break; |
15236 | } |
15237 | return tree_simple_nonnegative_warnv_p (code: CALL_EXPR, type); |
15238 | } |
15239 | |
15240 | /* Return true if T is known to be non-negative. If the return |
15241 | value is based on the assumption that signed overflow is undefined, |
15242 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
15243 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
15244 | |
15245 | static bool |
15246 | tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth) |
15247 | { |
15248 | enum tree_code code = TREE_CODE (t); |
15249 | if (TYPE_UNSIGNED (TREE_TYPE (t))) |
15250 | return true; |
15251 | |
15252 | switch (code) |
15253 | { |
15254 | case TARGET_EXPR: |
15255 | { |
15256 | tree temp = TARGET_EXPR_SLOT (t); |
15257 | t = TARGET_EXPR_INITIAL (t); |
15258 | |
15259 | /* If the initializer is non-void, then it's a normal expression |
15260 | that will be assigned to the slot. */ |
15261 | if (!VOID_TYPE_P (TREE_TYPE (t))) |
15262 | return RECURSE (t); |
15263 | |
15264 | /* Otherwise, the initializer sets the slot in some way. One common |
15265 | way is an assignment statement at the end of the initializer. */ |
15266 | while (1) |
15267 | { |
15268 | if (TREE_CODE (t) == BIND_EXPR) |
15269 | t = expr_last (BIND_EXPR_BODY (t)); |
15270 | else if (TREE_CODE (t) == TRY_FINALLY_EXPR |
15271 | || TREE_CODE (t) == TRY_CATCH_EXPR) |
15272 | t = expr_last (TREE_OPERAND (t, 0)); |
15273 | else if (TREE_CODE (t) == STATEMENT_LIST) |
15274 | t = expr_last (t); |
15275 | else |
15276 | break; |
15277 | } |
15278 | if (TREE_CODE (t) == MODIFY_EXPR |
15279 | && TREE_OPERAND (t, 0) == temp) |
15280 | return RECURSE (TREE_OPERAND (t, 1)); |
15281 | |
15282 | return false; |
15283 | } |
15284 | |
15285 | case CALL_EXPR: |
15286 | { |
15287 | tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE; |
15288 | tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE; |
15289 | |
15290 | return tree_call_nonnegative_warnv_p (TREE_TYPE (t), |
15291 | fn: get_call_combined_fn (t), |
15292 | arg0, |
15293 | arg1, |
15294 | strict_overflow_p, depth); |
15295 | } |
15296 | case COMPOUND_EXPR: |
15297 | case MODIFY_EXPR: |
15298 | return RECURSE (TREE_OPERAND (t, 1)); |
15299 | |
15300 | case BIND_EXPR: |
15301 | return RECURSE (expr_last (TREE_OPERAND (t, 1))); |
15302 | |
15303 | case SAVE_EXPR: |
15304 | return RECURSE (TREE_OPERAND (t, 0)); |
15305 | |
15306 | default: |
15307 | return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t)); |
15308 | } |
15309 | } |
15310 | |
15311 | #undef RECURSE |
15312 | #undef tree_expr_nonnegative_warnv_p |
15313 | |
15314 | /* Return true if T is known to be non-negative. If the return |
15315 | value is based on the assumption that signed overflow is undefined, |
15316 | set *STRICT_OVERFLOW_P to true; otherwise, don't change |
15317 | *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */ |
15318 | |
15319 | bool |
15320 | tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth) |
15321 | { |
15322 | enum tree_code code; |
15323 | if (t == error_mark_node) |
15324 | return false; |
15325 | |
15326 | code = TREE_CODE (t); |
15327 | switch (TREE_CODE_CLASS (code)) |
15328 | { |
15329 | case tcc_binary: |
15330 | case tcc_comparison: |
15331 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), |
15332 | TREE_TYPE (t), |
15333 | TREE_OPERAND (t, 0), |
15334 | TREE_OPERAND (t, 1), |
15335 | strict_overflow_p, depth); |
15336 | |
15337 | case tcc_unary: |
15338 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), |
15339 | TREE_TYPE (t), |
15340 | TREE_OPERAND (t, 0), |
15341 | strict_overflow_p, depth); |
15342 | |
15343 | case tcc_constant: |
15344 | case tcc_declaration: |
15345 | case tcc_reference: |
15346 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth); |
15347 | |
15348 | default: |
15349 | break; |
15350 | } |
15351 | |
15352 | switch (code) |
15353 | { |
15354 | case TRUTH_AND_EXPR: |
15355 | case TRUTH_OR_EXPR: |
15356 | case TRUTH_XOR_EXPR: |
15357 | return tree_binary_nonnegative_warnv_p (TREE_CODE (t), |
15358 | TREE_TYPE (t), |
15359 | TREE_OPERAND (t, 0), |
15360 | TREE_OPERAND (t, 1), |
15361 | strict_overflow_p, depth); |
15362 | case TRUTH_NOT_EXPR: |
15363 | return tree_unary_nonnegative_warnv_p (TREE_CODE (t), |
15364 | TREE_TYPE (t), |
15365 | TREE_OPERAND (t, 0), |
15366 | strict_overflow_p, depth); |
15367 | |
15368 | case COND_EXPR: |
15369 | case CONSTRUCTOR: |
15370 | case OBJ_TYPE_REF: |
15371 | case ADDR_EXPR: |
15372 | case WITH_SIZE_EXPR: |
15373 | case SSA_NAME: |
15374 | return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth); |
15375 | |
15376 | default: |
15377 | return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth); |
15378 | } |
15379 | } |
15380 | |
15381 | /* Return true if `t' is known to be non-negative. Handle warnings |
15382 | about undefined signed overflow. */ |
15383 | |
15384 | bool |
15385 | tree_expr_nonnegative_p (tree t) |
15386 | { |
15387 | bool ret, strict_overflow_p; |
15388 | |
15389 | strict_overflow_p = false; |
15390 | ret = tree_expr_nonnegative_warnv_p (t, strict_overflow_p: &strict_overflow_p); |
15391 | if (strict_overflow_p) |
15392 | fold_overflow_warning (gmsgid: ("assuming signed overflow does not occur when " |
15393 | "determining that expression is always " |
15394 | "non-negative" ), |
15395 | wc: WARN_STRICT_OVERFLOW_MISC); |
15396 | return ret; |
15397 | } |
15398 | |
15399 | |
15400 | /* Return true when (CODE OP0) is an address and is known to be nonzero. |
15401 | For floating point we further ensure that T is not denormal. |
15402 | Similar logic is present in nonzero_address in rtlanal.h. |
15403 | |
15404 | If the return value is based on the assumption that signed overflow |
15405 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't |
15406 | change *STRICT_OVERFLOW_P. */ |
15407 | |
15408 | bool |
15409 | tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0, |
15410 | bool *strict_overflow_p) |
15411 | { |
15412 | switch (code) |
15413 | { |
15414 | case ABS_EXPR: |
15415 | return tree_expr_nonzero_warnv_p (t: op0, |
15416 | strict_overflow_p); |
15417 | |
15418 | case NOP_EXPR: |
15419 | { |
15420 | tree inner_type = TREE_TYPE (op0); |
15421 | tree outer_type = type; |
15422 | |
15423 | return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type) |
15424 | && tree_expr_nonzero_warnv_p (t: op0, |
15425 | strict_overflow_p)); |
15426 | } |
15427 | break; |
15428 | |
15429 | case NON_LVALUE_EXPR: |
15430 | return tree_expr_nonzero_warnv_p (t: op0, |
15431 | strict_overflow_p); |
15432 | |
15433 | default: |
15434 | break; |
15435 | } |
15436 | |
15437 | return false; |
15438 | } |
15439 | |
15440 | /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero. |
15441 | For floating point we further ensure that T is not denormal. |
15442 | Similar logic is present in nonzero_address in rtlanal.h. |
15443 | |
15444 | If the return value is based on the assumption that signed overflow |
15445 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't |
15446 | change *STRICT_OVERFLOW_P. */ |
15447 | |
15448 | bool |
15449 | tree_binary_nonzero_warnv_p (enum tree_code code, |
15450 | tree type, |
15451 | tree op0, |
15452 | tree op1, bool *strict_overflow_p) |
15453 | { |
15454 | bool sub_strict_overflow_p; |
15455 | switch (code) |
15456 | { |
15457 | case POINTER_PLUS_EXPR: |
15458 | case PLUS_EXPR: |
15459 | if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type)) |
15460 | { |
15461 | /* With the presence of negative values it is hard |
15462 | to say something. */ |
15463 | sub_strict_overflow_p = false; |
15464 | if (!tree_expr_nonnegative_warnv_p (t: op0, |
15465 | strict_overflow_p: &sub_strict_overflow_p) |
15466 | || !tree_expr_nonnegative_warnv_p (t: op1, |
15467 | strict_overflow_p: &sub_strict_overflow_p)) |
15468 | return false; |
15469 | /* One of operands must be positive and the other non-negative. */ |
15470 | /* We don't set *STRICT_OVERFLOW_P here: even if this value |
15471 | overflows, on a twos-complement machine the sum of two |
15472 | nonnegative numbers can never be zero. */ |
15473 | return (tree_expr_nonzero_warnv_p (t: op0, |
15474 | strict_overflow_p) |
15475 | || tree_expr_nonzero_warnv_p (t: op1, |
15476 | strict_overflow_p)); |
15477 | } |
15478 | break; |
15479 | |
15480 | case MULT_EXPR: |
15481 | if (TYPE_OVERFLOW_UNDEFINED (type)) |
15482 | { |
15483 | if (tree_expr_nonzero_warnv_p (t: op0, |
15484 | strict_overflow_p) |
15485 | && tree_expr_nonzero_warnv_p (t: op1, |
15486 | strict_overflow_p)) |
15487 | { |
15488 | *strict_overflow_p = true; |
15489 | return true; |
15490 | } |
15491 | } |
15492 | break; |
15493 | |
15494 | case MIN_EXPR: |
15495 | sub_strict_overflow_p = false; |
15496 | if (tree_expr_nonzero_warnv_p (t: op0, |
15497 | strict_overflow_p: &sub_strict_overflow_p) |
15498 | && tree_expr_nonzero_warnv_p (t: op1, |
15499 | strict_overflow_p: &sub_strict_overflow_p)) |
15500 | { |
15501 | if (sub_strict_overflow_p) |
15502 | *strict_overflow_p = true; |
15503 | } |
15504 | break; |
15505 | |
15506 | case MAX_EXPR: |
15507 | sub_strict_overflow_p = false; |
15508 | if (tree_expr_nonzero_warnv_p (t: op0, |
15509 | strict_overflow_p: &sub_strict_overflow_p)) |
15510 | { |
15511 | if (sub_strict_overflow_p) |
15512 | *strict_overflow_p = true; |
15513 | |
15514 | /* When both operands are nonzero, then MAX must be too. */ |
15515 | if (tree_expr_nonzero_warnv_p (t: op1, |
15516 | strict_overflow_p)) |
15517 | return true; |
15518 | |
15519 | /* MAX where operand 0 is positive is positive. */ |
15520 | return tree_expr_nonnegative_warnv_p (t: op0, |
15521 | strict_overflow_p); |
15522 | } |
15523 | /* MAX where operand 1 is positive is positive. */ |
15524 | else if (tree_expr_nonzero_warnv_p (t: op1, |
15525 | strict_overflow_p: &sub_strict_overflow_p) |
15526 | && tree_expr_nonnegative_warnv_p (t: op1, |
15527 | strict_overflow_p: &sub_strict_overflow_p)) |
15528 | { |
15529 | if (sub_strict_overflow_p) |
15530 | *strict_overflow_p = true; |
15531 | return true; |
15532 | } |
15533 | break; |
15534 | |
15535 | case BIT_IOR_EXPR: |
15536 | return (tree_expr_nonzero_warnv_p (t: op1, |
15537 | strict_overflow_p) |
15538 | || tree_expr_nonzero_warnv_p (t: op0, |
15539 | strict_overflow_p)); |
15540 | |
15541 | default: |
15542 | break; |
15543 | } |
15544 | |
15545 | return false; |
15546 | } |
15547 | |
15548 | /* Return true when T is an address and is known to be nonzero. |
15549 | For floating point we further ensure that T is not denormal. |
15550 | Similar logic is present in nonzero_address in rtlanal.h. |
15551 | |
15552 | If the return value is based on the assumption that signed overflow |
15553 | is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't |
15554 | change *STRICT_OVERFLOW_P. */ |
15555 | |
15556 | bool |
15557 | tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p) |
15558 | { |
15559 | bool sub_strict_overflow_p; |
15560 | switch (TREE_CODE (t)) |
15561 | { |
15562 | case INTEGER_CST: |
15563 | return !integer_zerop (t); |
15564 | |
15565 | case ADDR_EXPR: |
15566 | { |
15567 | tree base = TREE_OPERAND (t, 0); |
15568 | |
15569 | if (!DECL_P (base)) |
15570 | base = get_base_address (t: base); |
15571 | |
15572 | if (base && TREE_CODE (base) == TARGET_EXPR) |
15573 | base = TARGET_EXPR_SLOT (base); |
15574 | |
15575 | if (!base) |
15576 | return false; |
15577 | |
15578 | /* For objects in symbol table check if we know they are non-zero. |
15579 | Don't do anything for variables and functions before symtab is built; |
15580 | it is quite possible that they will be declared weak later. */ |
15581 | int nonzero_addr = maybe_nonzero_address (decl: base); |
15582 | if (nonzero_addr >= 0) |
15583 | return nonzero_addr; |
15584 | |
15585 | /* Constants are never weak. */ |
15586 | if (CONSTANT_CLASS_P (base)) |
15587 | return true; |
15588 | |
15589 | return false; |
15590 | } |
15591 | |
15592 | case COND_EXPR: |
15593 | sub_strict_overflow_p = false; |
15594 | if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1), |
15595 | strict_overflow_p: &sub_strict_overflow_p) |
15596 | && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2), |
15597 | strict_overflow_p: &sub_strict_overflow_p)) |
15598 | { |
15599 | if (sub_strict_overflow_p) |
15600 | *strict_overflow_p = true; |
15601 | return true; |
15602 | } |
15603 | break; |
15604 | |
15605 | case SSA_NAME: |
15606 | if (!INTEGRAL_TYPE_P (TREE_TYPE (t))) |
15607 | break; |
15608 | return expr_not_equal_to (t, w: wi::zero (TYPE_PRECISION (TREE_TYPE (t)))); |
15609 | |
15610 | default: |
15611 | break; |
15612 | } |
15613 | return false; |
15614 | } |
15615 | |
15616 | #define integer_valued_real_p(X) \ |
15617 | _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0 |
15618 | |
15619 | #define RECURSE(X) \ |
15620 | ((integer_valued_real_p) (X, depth + 1)) |
15621 | |
15622 | /* Return true if the floating point result of (CODE OP0) has an |
15623 | integer value. We also allow +Inf, -Inf and NaN to be considered |
15624 | integer values. Return false for signaling NaN. |
15625 | |
15626 | DEPTH is the current nesting depth of the query. */ |
15627 | |
15628 | bool |
15629 | integer_valued_real_unary_p (tree_code code, tree op0, int depth) |
15630 | { |
15631 | switch (code) |
15632 | { |
15633 | case FLOAT_EXPR: |
15634 | return true; |
15635 | |
15636 | case ABS_EXPR: |
15637 | return RECURSE (op0); |
15638 | |
15639 | CASE_CONVERT: |
15640 | { |
15641 | tree type = TREE_TYPE (op0); |
15642 | if (TREE_CODE (type) == INTEGER_TYPE) |
15643 | return true; |
15644 | if (SCALAR_FLOAT_TYPE_P (type)) |
15645 | return RECURSE (op0); |
15646 | break; |
15647 | } |
15648 | |
15649 | default: |
15650 | break; |
15651 | } |
15652 | return false; |
15653 | } |
15654 | |
15655 | /* Return true if the floating point result of (CODE OP0 OP1) has an |
15656 | integer value. We also allow +Inf, -Inf and NaN to be considered |
15657 | integer values. Return false for signaling NaN. |
15658 | |
15659 | DEPTH is the current nesting depth of the query. */ |
15660 | |
15661 | bool |
15662 | integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth) |
15663 | { |
15664 | switch (code) |
15665 | { |
15666 | case PLUS_EXPR: |
15667 | case MINUS_EXPR: |
15668 | case MULT_EXPR: |
15669 | case MIN_EXPR: |
15670 | case MAX_EXPR: |
15671 | return RECURSE (op0) && RECURSE (op1); |
15672 | |
15673 | default: |
15674 | break; |
15675 | } |
15676 | return false; |
15677 | } |
15678 | |
15679 | /* Return true if the floating point result of calling FNDECL with arguments |
15680 | ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be |
15681 | considered integer values. Return false for signaling NaN. If FNDECL |
15682 | takes fewer than 2 arguments, the remaining ARGn are null. |
15683 | |
15684 | DEPTH is the current nesting depth of the query. */ |
15685 | |
15686 | bool |
15687 | integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth) |
15688 | { |
15689 | switch (fn) |
15690 | { |
15691 | CASE_CFN_CEIL: |
15692 | CASE_CFN_CEIL_FN: |
15693 | CASE_CFN_FLOOR: |
15694 | CASE_CFN_FLOOR_FN: |
15695 | CASE_CFN_NEARBYINT: |
15696 | CASE_CFN_NEARBYINT_FN: |
15697 | CASE_CFN_RINT: |
15698 | CASE_CFN_RINT_FN: |
15699 | CASE_CFN_ROUND: |
15700 | CASE_CFN_ROUND_FN: |
15701 | CASE_CFN_ROUNDEVEN: |
15702 | CASE_CFN_ROUNDEVEN_FN: |
15703 | CASE_CFN_TRUNC: |
15704 | CASE_CFN_TRUNC_FN: |
15705 | return true; |
15706 | |
15707 | CASE_CFN_FMIN: |
15708 | CASE_CFN_FMIN_FN: |
15709 | CASE_CFN_FMAX: |
15710 | CASE_CFN_FMAX_FN: |
15711 | return RECURSE (arg0) && RECURSE (arg1); |
15712 | |
15713 | default: |
15714 | break; |
15715 | } |
15716 | return false; |
15717 | } |
15718 | |
15719 | /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS) |
15720 | has an integer value. We also allow +Inf, -Inf and NaN to be |
15721 | considered integer values. Return false for signaling NaN. |
15722 | |
15723 | DEPTH is the current nesting depth of the query. */ |
15724 | |
15725 | bool |
15726 | integer_valued_real_single_p (tree t, int depth) |
15727 | { |
15728 | switch (TREE_CODE (t)) |
15729 | { |
15730 | case REAL_CST: |
15731 | return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t))); |
15732 | |
15733 | case COND_EXPR: |
15734 | return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2)); |
15735 | |
15736 | case SSA_NAME: |
15737 | /* Limit the depth of recursion to avoid quadratic behavior. |
15738 | This is expected to catch almost all occurrences in practice. |
15739 | If this code misses important cases that unbounded recursion |
15740 | would not, passes that need this information could be revised |
15741 | to provide it through dataflow propagation. */ |
15742 | return (!name_registered_for_update_p (t) |
15743 | && depth < param_max_ssa_name_query_depth |
15744 | && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t), |
15745 | depth)); |
15746 | |
15747 | default: |
15748 | break; |
15749 | } |
15750 | return false; |
15751 | } |
15752 | |
15753 | /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS) |
15754 | has an integer value. We also allow +Inf, -Inf and NaN to be |
15755 | considered integer values. Return false for signaling NaN. |
15756 | |
15757 | DEPTH is the current nesting depth of the query. */ |
15758 | |
15759 | static bool |
15760 | integer_valued_real_invalid_p (tree t, int depth) |
15761 | { |
15762 | switch (TREE_CODE (t)) |
15763 | { |
15764 | case COMPOUND_EXPR: |
15765 | case MODIFY_EXPR: |
15766 | case BIND_EXPR: |
15767 | return RECURSE (TREE_OPERAND (t, 1)); |
15768 | |
15769 | case SAVE_EXPR: |
15770 | return RECURSE (TREE_OPERAND (t, 0)); |
15771 | |
15772 | default: |
15773 | break; |
15774 | } |
15775 | return false; |
15776 | } |
15777 | |
15778 | #undef RECURSE |
15779 | #undef integer_valued_real_p |
15780 | |
15781 | /* Return true if the floating point expression T has an integer value. |
15782 | We also allow +Inf, -Inf and NaN to be considered integer values. |
15783 | Return false for signaling NaN. |
15784 | |
15785 | DEPTH is the current nesting depth of the query. */ |
15786 | |
15787 | bool |
15788 | integer_valued_real_p (tree t, int depth) |
15789 | { |
15790 | if (t == error_mark_node) |
15791 | return false; |
15792 | |
15793 | STRIP_ANY_LOCATION_WRAPPER (t); |
15794 | |
15795 | tree_code code = TREE_CODE (t); |
15796 | switch (TREE_CODE_CLASS (code)) |
15797 | { |
15798 | case tcc_binary: |
15799 | case tcc_comparison: |
15800 | return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0), |
15801 | TREE_OPERAND (t, 1), depth); |
15802 | |
15803 | case tcc_unary: |
15804 | return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth); |
15805 | |
15806 | case tcc_constant: |
15807 | case tcc_declaration: |
15808 | case tcc_reference: |
15809 | return integer_valued_real_single_p (t, depth); |
15810 | |
15811 | default: |
15812 | break; |
15813 | } |
15814 | |
15815 | switch (code) |
15816 | { |
15817 | case COND_EXPR: |
15818 | case SSA_NAME: |
15819 | return integer_valued_real_single_p (t, depth); |
15820 | |
15821 | case CALL_EXPR: |
15822 | { |
15823 | tree arg0 = (call_expr_nargs (t) > 0 |
15824 | ? CALL_EXPR_ARG (t, 0) |
15825 | : NULL_TREE); |
15826 | tree arg1 = (call_expr_nargs (t) > 1 |
15827 | ? CALL_EXPR_ARG (t, 1) |
15828 | : NULL_TREE); |
15829 | return integer_valued_real_call_p (fn: get_call_combined_fn (t), |
15830 | arg0, arg1, depth); |
15831 | } |
15832 | |
15833 | default: |
15834 | return integer_valued_real_invalid_p (t, depth); |
15835 | } |
15836 | } |
15837 | |
15838 | /* Given the components of a binary expression CODE, TYPE, OP0 and OP1, |
15839 | attempt to fold the expression to a constant without modifying TYPE, |
15840 | OP0 or OP1. |
15841 | |
15842 | If the expression could be simplified to a constant, then return |
15843 | the constant. If the expression would not be simplified to a |
15844 | constant, then return NULL_TREE. */ |
15845 | |
15846 | tree |
15847 | fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1) |
15848 | { |
15849 | tree tem = fold_binary (code, type, op0, op1); |
15850 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; |
15851 | } |
15852 | |
15853 | /* Given the components of a unary expression CODE, TYPE and OP0, |
15854 | attempt to fold the expression to a constant without modifying |
15855 | TYPE or OP0. |
15856 | |
15857 | If the expression could be simplified to a constant, then return |
15858 | the constant. If the expression would not be simplified to a |
15859 | constant, then return NULL_TREE. */ |
15860 | |
15861 | tree |
15862 | fold_unary_to_constant (enum tree_code code, tree type, tree op0) |
15863 | { |
15864 | tree tem = fold_unary (code, type, op0); |
15865 | return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE; |
15866 | } |
15867 | |
15868 | /* If EXP represents referencing an element in a constant string |
15869 | (either via pointer arithmetic or array indexing), return the |
15870 | tree representing the value accessed, otherwise return NULL. */ |
15871 | |
15872 | tree |
15873 | fold_read_from_constant_string (tree exp) |
15874 | { |
15875 | if ((INDIRECT_REF_P (exp) |
15876 | || TREE_CODE (exp) == ARRAY_REF) |
15877 | && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE) |
15878 | { |
15879 | tree exp1 = TREE_OPERAND (exp, 0); |
15880 | tree index; |
15881 | tree string; |
15882 | location_t loc = EXPR_LOCATION (exp); |
15883 | |
15884 | if (INDIRECT_REF_P (exp)) |
15885 | string = string_constant (exp1, &index, NULL, NULL); |
15886 | else |
15887 | { |
15888 | tree low_bound = array_ref_low_bound (exp); |
15889 | index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1)); |
15890 | |
15891 | /* Optimize the special-case of a zero lower bound. |
15892 | |
15893 | We convert the low_bound to sizetype to avoid some problems |
15894 | with constant folding. (E.g. suppose the lower bound is 1, |
15895 | and its mode is QI. Without the conversion,l (ARRAY |
15896 | +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1)) |
15897 | +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */ |
15898 | if (! integer_zerop (low_bound)) |
15899 | index = size_diffop_loc (loc, arg0: index, |
15900 | arg1: fold_convert_loc (loc, sizetype, arg: low_bound)); |
15901 | |
15902 | string = exp1; |
15903 | } |
15904 | |
15905 | scalar_int_mode char_mode; |
15906 | if (string |
15907 | && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string))) |
15908 | && TREE_CODE (string) == STRING_CST |
15909 | && tree_fits_uhwi_p (index) |
15910 | && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0 |
15911 | && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))), |
15912 | int_mode: &char_mode) |
15913 | && GET_MODE_SIZE (mode: char_mode) == 1) |
15914 | return build_int_cst_type (TREE_TYPE (exp), |
15915 | (TREE_STRING_POINTER (string) |
15916 | [TREE_INT_CST_LOW (index)])); |
15917 | } |
15918 | return NULL; |
15919 | } |
15920 | |
15921 | /* Folds a read from vector element at IDX of vector ARG. */ |
15922 | |
15923 | tree |
15924 | fold_read_from_vector (tree arg, poly_uint64 idx) |
15925 | { |
15926 | unsigned HOST_WIDE_INT i; |
15927 | if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg))) |
15928 | && known_ge (idx, 0u) |
15929 | && idx.is_constant (const_value: &i)) |
15930 | { |
15931 | if (TREE_CODE (arg) == VECTOR_CST) |
15932 | return VECTOR_CST_ELT (arg, i); |
15933 | else if (TREE_CODE (arg) == CONSTRUCTOR) |
15934 | { |
15935 | if (CONSTRUCTOR_NELTS (arg) |
15936 | && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value))) |
15937 | return NULL_TREE; |
15938 | if (i >= CONSTRUCTOR_NELTS (arg)) |
15939 | return build_zero_cst (TREE_TYPE (TREE_TYPE (arg))); |
15940 | return CONSTRUCTOR_ELT (arg, i)->value; |
15941 | } |
15942 | } |
15943 | return NULL_TREE; |
15944 | } |
15945 | |
15946 | /* Return the tree for neg (ARG0) when ARG0 is known to be either |
15947 | an integer constant, real, or fixed-point constant. |
15948 | |
15949 | TYPE is the type of the result. */ |
15950 | |
15951 | static tree |
15952 | fold_negate_const (tree arg0, tree type) |
15953 | { |
15954 | tree t = NULL_TREE; |
15955 | |
15956 | switch (TREE_CODE (arg0)) |
15957 | { |
15958 | case REAL_CST: |
15959 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
15960 | break; |
15961 | |
15962 | case FIXED_CST: |
15963 | { |
15964 | FIXED_VALUE_TYPE f; |
15965 | bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR, |
15966 | &(TREE_FIXED_CST (arg0)), NULL, |
15967 | TYPE_SATURATING (type)); |
15968 | t = build_fixed (type, f); |
15969 | /* Propagate overflow flags. */ |
15970 | if (overflow_p | TREE_OVERFLOW (arg0)) |
15971 | TREE_OVERFLOW (t) = 1; |
15972 | break; |
15973 | } |
15974 | |
15975 | default: |
15976 | if (poly_int_tree_p (t: arg0)) |
15977 | { |
15978 | wi::overflow_type overflow; |
15979 | poly_wide_int res = wi::neg (a: wi::to_poly_wide (t: arg0), overflow: &overflow); |
15980 | t = force_fit_type (type, res, 1, |
15981 | (overflow && ! TYPE_UNSIGNED (type)) |
15982 | || TREE_OVERFLOW (arg0)); |
15983 | break; |
15984 | } |
15985 | |
15986 | gcc_unreachable (); |
15987 | } |
15988 | |
15989 | return t; |
15990 | } |
15991 | |
15992 | /* Return the tree for abs (ARG0) when ARG0 is known to be either |
15993 | an integer constant or real constant. |
15994 | |
15995 | TYPE is the type of the result. */ |
15996 | |
15997 | tree |
15998 | fold_abs_const (tree arg0, tree type) |
15999 | { |
16000 | tree t = NULL_TREE; |
16001 | |
16002 | switch (TREE_CODE (arg0)) |
16003 | { |
16004 | case INTEGER_CST: |
16005 | { |
16006 | /* If the value is unsigned or non-negative, then the absolute value |
16007 | is the same as the ordinary value. */ |
16008 | wide_int val = wi::to_wide (t: arg0); |
16009 | wi::overflow_type overflow = wi::OVF_NONE; |
16010 | if (!wi::neg_p (x: val, TYPE_SIGN (TREE_TYPE (arg0)))) |
16011 | ; |
16012 | |
16013 | /* If the value is negative, then the absolute value is |
16014 | its negation. */ |
16015 | else |
16016 | val = wi::neg (x: val, overflow: &overflow); |
16017 | |
16018 | /* Force to the destination type, set TREE_OVERFLOW for signed |
16019 | TYPE only. */ |
16020 | t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0)); |
16021 | } |
16022 | break; |
16023 | |
16024 | case REAL_CST: |
16025 | if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))) |
16026 | t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0))); |
16027 | else |
16028 | t = arg0; |
16029 | break; |
16030 | |
16031 | default: |
16032 | gcc_unreachable (); |
16033 | } |
16034 | |
16035 | return t; |
16036 | } |
16037 | |
16038 | /* Return the tree for not (ARG0) when ARG0 is known to be an integer |
16039 | constant. TYPE is the type of the result. */ |
16040 | |
16041 | static tree |
16042 | fold_not_const (const_tree arg0, tree type) |
16043 | { |
16044 | gcc_assert (TREE_CODE (arg0) == INTEGER_CST); |
16045 | |
16046 | return force_fit_type (type, ~wi::to_wide (t: arg0), 0, TREE_OVERFLOW (arg0)); |
16047 | } |
16048 | |
16049 | /* Given CODE, a relational operator, the target type, TYPE and two |
16050 | constant operands OP0 and OP1, return the result of the |
16051 | relational operation. If the result is not a compile time |
16052 | constant, then return NULL_TREE. */ |
16053 | |
16054 | static tree |
16055 | fold_relational_const (enum tree_code code, tree type, tree op0, tree op1) |
16056 | { |
16057 | int result, invert; |
16058 | |
16059 | /* From here on, the only cases we handle are when the result is |
16060 | known to be a constant. */ |
16061 | |
16062 | if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST) |
16063 | { |
16064 | const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0); |
16065 | const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1); |
16066 | |
16067 | /* Handle the cases where either operand is a NaN. */ |
16068 | if (real_isnan (c0) || real_isnan (c1)) |
16069 | { |
16070 | switch (code) |
16071 | { |
16072 | case EQ_EXPR: |
16073 | case ORDERED_EXPR: |
16074 | result = 0; |
16075 | break; |
16076 | |
16077 | case NE_EXPR: |
16078 | case UNORDERED_EXPR: |
16079 | case UNLT_EXPR: |
16080 | case UNLE_EXPR: |
16081 | case UNGT_EXPR: |
16082 | case UNGE_EXPR: |
16083 | case UNEQ_EXPR: |
16084 | result = 1; |
16085 | break; |
16086 | |
16087 | case LT_EXPR: |
16088 | case LE_EXPR: |
16089 | case GT_EXPR: |
16090 | case GE_EXPR: |
16091 | case LTGT_EXPR: |
16092 | if (flag_trapping_math) |
16093 | return NULL_TREE; |
16094 | result = 0; |
16095 | break; |
16096 | |
16097 | default: |
16098 | gcc_unreachable (); |
16099 | } |
16100 | |
16101 | return constant_boolean_node (value: result, type); |
16102 | } |
16103 | |
16104 | return constant_boolean_node (value: real_compare (code, c0, c1), type); |
16105 | } |
16106 | |
16107 | if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST) |
16108 | { |
16109 | const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0); |
16110 | const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1); |
16111 | return constant_boolean_node (value: fixed_compare (code, c0, c1), type); |
16112 | } |
16113 | |
16114 | /* Handle equality/inequality of complex constants. */ |
16115 | if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST) |
16116 | { |
16117 | tree rcond = fold_relational_const (code, type, |
16118 | TREE_REALPART (op0), |
16119 | TREE_REALPART (op1)); |
16120 | tree icond = fold_relational_const (code, type, |
16121 | TREE_IMAGPART (op0), |
16122 | TREE_IMAGPART (op1)); |
16123 | if (code == EQ_EXPR) |
16124 | return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond); |
16125 | else if (code == NE_EXPR) |
16126 | return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond); |
16127 | else |
16128 | return NULL_TREE; |
16129 | } |
16130 | |
16131 | if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST) |
16132 | { |
16133 | if (!VECTOR_TYPE_P (type)) |
16134 | { |
16135 | /* Have vector comparison with scalar boolean result. */ |
16136 | gcc_assert ((code == EQ_EXPR || code == NE_EXPR) |
16137 | && known_eq (VECTOR_CST_NELTS (op0), |
16138 | VECTOR_CST_NELTS (op1))); |
16139 | unsigned HOST_WIDE_INT nunits; |
16140 | if (!VECTOR_CST_NELTS (op0).is_constant (const_value: &nunits)) |
16141 | return NULL_TREE; |
16142 | for (unsigned i = 0; i < nunits; i++) |
16143 | { |
16144 | tree elem0 = VECTOR_CST_ELT (op0, i); |
16145 | tree elem1 = VECTOR_CST_ELT (op1, i); |
16146 | tree tmp = fold_relational_const (code: EQ_EXPR, type, op0: elem0, op1: elem1); |
16147 | if (tmp == NULL_TREE) |
16148 | return NULL_TREE; |
16149 | if (integer_zerop (tmp)) |
16150 | return constant_boolean_node (value: code == NE_EXPR, type); |
16151 | } |
16152 | return constant_boolean_node (value: code == EQ_EXPR, type); |
16153 | } |
16154 | tree_vector_builder elts; |
16155 | if (!elts.new_binary_operation (shape: type, vec1: op0, vec2: op1, allow_stepped_p: false)) |
16156 | return NULL_TREE; |
16157 | unsigned int count = elts.encoded_nelts (); |
16158 | for (unsigned i = 0; i < count; i++) |
16159 | { |
16160 | tree elem_type = TREE_TYPE (type); |
16161 | tree elem0 = VECTOR_CST_ELT (op0, i); |
16162 | tree elem1 = VECTOR_CST_ELT (op1, i); |
16163 | |
16164 | tree tem = fold_relational_const (code, type: elem_type, |
16165 | op0: elem0, op1: elem1); |
16166 | |
16167 | if (tem == NULL_TREE) |
16168 | return NULL_TREE; |
16169 | |
16170 | elts.quick_push (obj: build_int_cst (elem_type, |
16171 | integer_zerop (tem) ? 0 : -1)); |
16172 | } |
16173 | |
16174 | return elts.build (); |
16175 | } |
16176 | |
16177 | /* From here on we only handle LT, LE, GT, GE, EQ and NE. |
16178 | |
16179 | To compute GT, swap the arguments and do LT. |
16180 | To compute GE, do LT and invert the result. |
16181 | To compute LE, swap the arguments, do LT and invert the result. |
16182 | To compute NE, do EQ and invert the result. |
16183 | |
16184 | Therefore, the code below must handle only EQ and LT. */ |
16185 | |
16186 | if (code == LE_EXPR || code == GT_EXPR) |
16187 | { |
16188 | std::swap (a&: op0, b&: op1); |
16189 | code = swap_tree_comparison (code); |
16190 | } |
16191 | |
16192 | /* Note that it is safe to invert for real values here because we |
16193 | have already handled the one case that it matters. */ |
16194 | |
16195 | invert = 0; |
16196 | if (code == NE_EXPR || code == GE_EXPR) |
16197 | { |
16198 | invert = 1; |
16199 | code = invert_tree_comparison (code, honor_nans: false); |
16200 | } |
16201 | |
16202 | /* Compute a result for LT or EQ if args permit; |
16203 | Otherwise return T. */ |
16204 | if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST) |
16205 | { |
16206 | if (code == EQ_EXPR) |
16207 | result = tree_int_cst_equal (op0, op1); |
16208 | else |
16209 | result = tree_int_cst_lt (t1: op0, t2: op1); |
16210 | } |
16211 | else |
16212 | return NULL_TREE; |
16213 | |
16214 | if (invert) |
16215 | result ^= 1; |
16216 | return constant_boolean_node (value: result, type); |
16217 | } |
16218 | |
16219 | /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the |
16220 | indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR |
16221 | itself. */ |
16222 | |
16223 | tree |
16224 | fold_build_cleanup_point_expr (tree type, tree expr) |
16225 | { |
16226 | /* If the expression does not have side effects then we don't have to wrap |
16227 | it with a cleanup point expression. */ |
16228 | if (!TREE_SIDE_EFFECTS (expr)) |
16229 | return expr; |
16230 | |
16231 | /* If the expression is a return, check to see if the expression inside the |
16232 | return has no side effects or the right hand side of the modify expression |
16233 | inside the return. If either don't have side effects set we don't need to |
16234 | wrap the expression in a cleanup point expression. Note we don't check the |
16235 | left hand side of the modify because it should always be a return decl. */ |
16236 | if (TREE_CODE (expr) == RETURN_EXPR) |
16237 | { |
16238 | tree op = TREE_OPERAND (expr, 0); |
16239 | if (!op || !TREE_SIDE_EFFECTS (op)) |
16240 | return expr; |
16241 | op = TREE_OPERAND (op, 1); |
16242 | if (!TREE_SIDE_EFFECTS (op)) |
16243 | return expr; |
16244 | } |
16245 | |
16246 | return build1_loc (EXPR_LOCATION (expr), code: CLEANUP_POINT_EXPR, type, arg1: expr); |
16247 | } |
16248 | |
16249 | /* Given a pointer value OP0 and a type TYPE, return a simplified version |
16250 | of an indirection through OP0, or NULL_TREE if no simplification is |
16251 | possible. */ |
16252 | |
16253 | tree |
16254 | fold_indirect_ref_1 (location_t loc, tree type, tree op0) |
16255 | { |
16256 | tree sub = op0; |
16257 | tree subtype; |
16258 | poly_uint64 const_op01; |
16259 | |
16260 | STRIP_NOPS (sub); |
16261 | subtype = TREE_TYPE (sub); |
16262 | if (!POINTER_TYPE_P (subtype) |
16263 | || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0))) |
16264 | return NULL_TREE; |
16265 | |
16266 | if (TREE_CODE (sub) == ADDR_EXPR) |
16267 | { |
16268 | tree op = TREE_OPERAND (sub, 0); |
16269 | tree optype = TREE_TYPE (op); |
16270 | |
16271 | /* *&CONST_DECL -> to the value of the const decl. */ |
16272 | if (TREE_CODE (op) == CONST_DECL) |
16273 | return DECL_INITIAL (op); |
16274 | /* *&p => p; make sure to handle *&"str"[cst] here. */ |
16275 | if (type == optype) |
16276 | { |
16277 | tree fop = fold_read_from_constant_string (exp: op); |
16278 | if (fop) |
16279 | return fop; |
16280 | else |
16281 | return op; |
16282 | } |
16283 | /* *(foo *)&fooarray => fooarray[0] */ |
16284 | else if (TREE_CODE (optype) == ARRAY_TYPE |
16285 | && type == TREE_TYPE (optype) |
16286 | && (!in_gimple_form |
16287 | || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)) |
16288 | { |
16289 | tree type_domain = TYPE_DOMAIN (optype); |
16290 | tree min_val = size_zero_node; |
16291 | if (type_domain && TYPE_MIN_VALUE (type_domain)) |
16292 | min_val = TYPE_MIN_VALUE (type_domain); |
16293 | if (in_gimple_form |
16294 | && TREE_CODE (min_val) != INTEGER_CST) |
16295 | return NULL_TREE; |
16296 | return build4_loc (loc, code: ARRAY_REF, type, arg0: op, arg1: min_val, |
16297 | NULL_TREE, NULL_TREE); |
16298 | } |
16299 | /* *(foo *)&complexfoo => __real__ complexfoo */ |
16300 | else if (TREE_CODE (optype) == COMPLEX_TYPE |
16301 | && type == TREE_TYPE (optype)) |
16302 | return fold_build1_loc (loc, code: REALPART_EXPR, type, op0: op); |
16303 | /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */ |
16304 | else if (VECTOR_TYPE_P (optype) |
16305 | && type == TREE_TYPE (optype)) |
16306 | { |
16307 | tree part_width = TYPE_SIZE (type); |
16308 | tree index = bitsize_int (0); |
16309 | return fold_build3_loc (loc, code: BIT_FIELD_REF, type, op0: op, op1: part_width, |
16310 | op2: index); |
16311 | } |
16312 | } |
16313 | |
16314 | if (TREE_CODE (sub) == POINTER_PLUS_EXPR |
16315 | && poly_int_tree_p (TREE_OPERAND (sub, 1), value: &const_op01)) |
16316 | { |
16317 | tree op00 = TREE_OPERAND (sub, 0); |
16318 | tree op01 = TREE_OPERAND (sub, 1); |
16319 | |
16320 | STRIP_NOPS (op00); |
16321 | if (TREE_CODE (op00) == ADDR_EXPR) |
16322 | { |
16323 | tree op00type; |
16324 | op00 = TREE_OPERAND (op00, 0); |
16325 | op00type = TREE_TYPE (op00); |
16326 | |
16327 | /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */ |
16328 | if (VECTOR_TYPE_P (op00type) |
16329 | && type == TREE_TYPE (op00type) |
16330 | /* POINTER_PLUS_EXPR second operand is sizetype, unsigned, |
16331 | but we want to treat offsets with MSB set as negative. |
16332 | For the code below negative offsets are invalid and |
16333 | TYPE_SIZE of the element is something unsigned, so |
16334 | check whether op01 fits into poly_int64, which implies |
16335 | it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and |
16336 | then just use poly_uint64 because we want to treat the |
16337 | value as unsigned. */ |
16338 | && tree_fits_poly_int64_p (op01)) |
16339 | { |
16340 | tree part_width = TYPE_SIZE (type); |
16341 | poly_uint64 max_offset |
16342 | = (tree_to_uhwi (part_width) / BITS_PER_UNIT |
16343 | * TYPE_VECTOR_SUBPARTS (node: op00type)); |
16344 | if (known_lt (const_op01, max_offset)) |
16345 | { |
16346 | tree index = bitsize_int (const_op01 * BITS_PER_UNIT); |
16347 | return fold_build3_loc (loc, |
16348 | code: BIT_FIELD_REF, type, op0: op00, |
16349 | op1: part_width, op2: index); |
16350 | } |
16351 | } |
16352 | /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */ |
16353 | else if (TREE_CODE (op00type) == COMPLEX_TYPE |
16354 | && type == TREE_TYPE (op00type)) |
16355 | { |
16356 | if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)), |
16357 | const_op01)) |
16358 | return fold_build1_loc (loc, code: IMAGPART_EXPR, type, op0: op00); |
16359 | } |
16360 | /* ((foo *)&fooarray)[1] => fooarray[1] */ |
16361 | else if (TREE_CODE (op00type) == ARRAY_TYPE |
16362 | && type == TREE_TYPE (op00type)) |
16363 | { |
16364 | tree type_domain = TYPE_DOMAIN (op00type); |
16365 | tree min_val = size_zero_node; |
16366 | if (type_domain && TYPE_MIN_VALUE (type_domain)) |
16367 | min_val = TYPE_MIN_VALUE (type_domain); |
16368 | poly_uint64 type_size, index; |
16369 | if (poly_int_tree_p (t: min_val) |
16370 | && poly_int_tree_p (TYPE_SIZE_UNIT (type), value: &type_size) |
16371 | && multiple_p (a: const_op01, b: type_size, multiple: &index)) |
16372 | { |
16373 | poly_offset_int off = index + wi::to_poly_offset (t: min_val); |
16374 | op01 = wide_int_to_tree (sizetype, cst: off); |
16375 | return build4_loc (loc, code: ARRAY_REF, type, arg0: op00, arg1: op01, |
16376 | NULL_TREE, NULL_TREE); |
16377 | } |
16378 | } |
16379 | } |
16380 | } |
16381 | |
16382 | /* *(foo *)fooarrptr => (*fooarrptr)[0] */ |
16383 | if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE |
16384 | && type == TREE_TYPE (TREE_TYPE (subtype)) |
16385 | && (!in_gimple_form |
16386 | || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)) |
16387 | { |
16388 | tree type_domain; |
16389 | tree min_val = size_zero_node; |
16390 | sub = build_fold_indirect_ref_loc (loc, sub); |
16391 | type_domain = TYPE_DOMAIN (TREE_TYPE (sub)); |
16392 | if (type_domain && TYPE_MIN_VALUE (type_domain)) |
16393 | min_val = TYPE_MIN_VALUE (type_domain); |
16394 | if (in_gimple_form |
16395 | && TREE_CODE (min_val) != INTEGER_CST) |
16396 | return NULL_TREE; |
16397 | return build4_loc (loc, code: ARRAY_REF, type, arg0: sub, arg1: min_val, NULL_TREE, |
16398 | NULL_TREE); |
16399 | } |
16400 | |
16401 | return NULL_TREE; |
16402 | } |
16403 | |
16404 | /* Builds an expression for an indirection through T, simplifying some |
16405 | cases. */ |
16406 | |
16407 | tree |
16408 | build_fold_indirect_ref_loc (location_t loc, tree t) |
16409 | { |
16410 | tree type = TREE_TYPE (TREE_TYPE (t)); |
16411 | tree sub = fold_indirect_ref_1 (loc, type, op0: t); |
16412 | |
16413 | if (sub) |
16414 | return sub; |
16415 | |
16416 | return build1_loc (loc, code: INDIRECT_REF, type, arg1: t); |
16417 | } |
16418 | |
16419 | /* Given an INDIRECT_REF T, return either T or a simplified version. */ |
16420 | |
16421 | tree |
16422 | fold_indirect_ref_loc (location_t loc, tree t) |
16423 | { |
16424 | tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0)); |
16425 | |
16426 | if (sub) |
16427 | return sub; |
16428 | else |
16429 | return t; |
16430 | } |
16431 | |
16432 | /* Strip non-trapping, non-side-effecting tree nodes from an expression |
16433 | whose result is ignored. The type of the returned tree need not be |
16434 | the same as the original expression. */ |
16435 | |
16436 | tree |
16437 | fold_ignored_result (tree t) |
16438 | { |
16439 | if (!TREE_SIDE_EFFECTS (t)) |
16440 | return integer_zero_node; |
16441 | |
16442 | for (;;) |
16443 | switch (TREE_CODE_CLASS (TREE_CODE (t))) |
16444 | { |
16445 | case tcc_unary: |
16446 | t = TREE_OPERAND (t, 0); |
16447 | break; |
16448 | |
16449 | case tcc_binary: |
16450 | case tcc_comparison: |
16451 | if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) |
16452 | t = TREE_OPERAND (t, 0); |
16453 | else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))) |
16454 | t = TREE_OPERAND (t, 1); |
16455 | else |
16456 | return t; |
16457 | break; |
16458 | |
16459 | case tcc_expression: |
16460 | switch (TREE_CODE (t)) |
16461 | { |
16462 | case COMPOUND_EXPR: |
16463 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))) |
16464 | return t; |
16465 | t = TREE_OPERAND (t, 0); |
16466 | break; |
16467 | |
16468 | case COND_EXPR: |
16469 | if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)) |
16470 | || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2))) |
16471 | return t; |
16472 | t = TREE_OPERAND (t, 0); |
16473 | break; |
16474 | |
16475 | default: |
16476 | return t; |
16477 | } |
16478 | break; |
16479 | |
16480 | default: |
16481 | return t; |
16482 | } |
16483 | } |
16484 | |
16485 | /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */ |
16486 | |
16487 | tree |
16488 | round_up_loc (location_t loc, tree value, unsigned int divisor) |
16489 | { |
16490 | tree div = NULL_TREE; |
16491 | |
16492 | if (divisor == 1) |
16493 | return value; |
16494 | |
16495 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
16496 | have to do anything. Only do this when we are not given a const, |
16497 | because in that case, this check is more expensive than just |
16498 | doing it. */ |
16499 | if (TREE_CODE (value) != INTEGER_CST) |
16500 | { |
16501 | div = build_int_cst (TREE_TYPE (value), divisor); |
16502 | |
16503 | if (multiple_of_p (TREE_TYPE (value), top: value, bottom: div)) |
16504 | return value; |
16505 | } |
16506 | |
16507 | /* If divisor is a power of two, simplify this to bit manipulation. */ |
16508 | if (pow2_or_zerop (x: divisor)) |
16509 | { |
16510 | if (TREE_CODE (value) == INTEGER_CST) |
16511 | { |
16512 | wide_int val = wi::to_wide (t: value); |
16513 | bool overflow_p; |
16514 | |
16515 | if ((val & (divisor - 1)) == 0) |
16516 | return value; |
16517 | |
16518 | overflow_p = TREE_OVERFLOW (value); |
16519 | val += divisor - 1; |
16520 | val &= (int) -divisor; |
16521 | if (val == 0) |
16522 | overflow_p = true; |
16523 | |
16524 | return force_fit_type (TREE_TYPE (value), val, -1, overflow_p); |
16525 | } |
16526 | else |
16527 | { |
16528 | tree t; |
16529 | |
16530 | t = build_int_cst (TREE_TYPE (value), divisor - 1); |
16531 | value = size_binop_loc (loc, code: PLUS_EXPR, arg0: value, arg1: t); |
16532 | t = build_int_cst (TREE_TYPE (value), - (int) divisor); |
16533 | value = size_binop_loc (loc, code: BIT_AND_EXPR, arg0: value, arg1: t); |
16534 | } |
16535 | } |
16536 | else |
16537 | { |
16538 | if (!div) |
16539 | div = build_int_cst (TREE_TYPE (value), divisor); |
16540 | value = size_binop_loc (loc, code: CEIL_DIV_EXPR, arg0: value, arg1: div); |
16541 | value = size_binop_loc (loc, code: MULT_EXPR, arg0: value, arg1: div); |
16542 | } |
16543 | |
16544 | return value; |
16545 | } |
16546 | |
16547 | /* Likewise, but round down. */ |
16548 | |
16549 | tree |
16550 | round_down_loc (location_t loc, tree value, int divisor) |
16551 | { |
16552 | tree div = NULL_TREE; |
16553 | |
16554 | gcc_assert (divisor > 0); |
16555 | if (divisor == 1) |
16556 | return value; |
16557 | |
16558 | /* See if VALUE is already a multiple of DIVISOR. If so, we don't |
16559 | have to do anything. Only do this when we are not given a const, |
16560 | because in that case, this check is more expensive than just |
16561 | doing it. */ |
16562 | if (TREE_CODE (value) != INTEGER_CST) |
16563 | { |
16564 | div = build_int_cst (TREE_TYPE (value), divisor); |
16565 | |
16566 | if (multiple_of_p (TREE_TYPE (value), top: value, bottom: div)) |
16567 | return value; |
16568 | } |
16569 | |
16570 | /* If divisor is a power of two, simplify this to bit manipulation. */ |
16571 | if (pow2_or_zerop (x: divisor)) |
16572 | { |
16573 | tree t; |
16574 | |
16575 | t = build_int_cst (TREE_TYPE (value), -divisor); |
16576 | value = size_binop_loc (loc, code: BIT_AND_EXPR, arg0: value, arg1: t); |
16577 | } |
16578 | else |
16579 | { |
16580 | if (!div) |
16581 | div = build_int_cst (TREE_TYPE (value), divisor); |
16582 | value = size_binop_loc (loc, code: FLOOR_DIV_EXPR, arg0: value, arg1: div); |
16583 | value = size_binop_loc (loc, code: MULT_EXPR, arg0: value, arg1: div); |
16584 | } |
16585 | |
16586 | return value; |
16587 | } |
16588 | |
16589 | /* Returns the pointer to the base of the object addressed by EXP and |
16590 | extracts the information about the offset of the access, storing it |
16591 | to PBITPOS and POFFSET. */ |
16592 | |
16593 | static tree |
16594 | split_address_to_core_and_offset (tree exp, |
16595 | poly_int64 *pbitpos, tree *poffset) |
16596 | { |
16597 | tree core; |
16598 | machine_mode mode; |
16599 | int unsignedp, reversep, volatilep; |
16600 | poly_int64 bitsize; |
16601 | location_t loc = EXPR_LOCATION (exp); |
16602 | |
16603 | if (TREE_CODE (exp) == SSA_NAME) |
16604 | if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp))) |
16605 | if (gimple_assign_rhs_code (gs: def) == ADDR_EXPR) |
16606 | exp = gimple_assign_rhs1 (gs: def); |
16607 | |
16608 | if (TREE_CODE (exp) == ADDR_EXPR) |
16609 | { |
16610 | core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos, |
16611 | poffset, &mode, &unsignedp, &reversep, |
16612 | &volatilep); |
16613 | core = build_fold_addr_expr_loc (loc, t: core); |
16614 | } |
16615 | else if (TREE_CODE (exp) == POINTER_PLUS_EXPR) |
16616 | { |
16617 | core = TREE_OPERAND (exp, 0); |
16618 | STRIP_NOPS (core); |
16619 | *pbitpos = 0; |
16620 | *poffset = TREE_OPERAND (exp, 1); |
16621 | if (poly_int_tree_p (t: *poffset)) |
16622 | { |
16623 | poly_offset_int tem |
16624 | = wi::sext (a: wi::to_poly_offset (t: *poffset), |
16625 | TYPE_PRECISION (TREE_TYPE (*poffset))); |
16626 | tem <<= LOG2_BITS_PER_UNIT; |
16627 | if (tem.to_shwi (r: pbitpos)) |
16628 | *poffset = NULL_TREE; |
16629 | } |
16630 | } |
16631 | else |
16632 | { |
16633 | core = exp; |
16634 | *pbitpos = 0; |
16635 | *poffset = NULL_TREE; |
16636 | } |
16637 | |
16638 | return core; |
16639 | } |
16640 | |
16641 | /* Returns true if addresses of E1 and E2 differ by a constant, false |
16642 | otherwise. If they do, E1 - E2 is stored in *DIFF. */ |
16643 | |
16644 | bool |
16645 | ptr_difference_const (tree e1, tree e2, poly_int64 *diff) |
16646 | { |
16647 | tree core1, core2; |
16648 | poly_int64 bitpos1, bitpos2; |
16649 | tree toffset1, toffset2, tdiff, type; |
16650 | |
16651 | core1 = split_address_to_core_and_offset (exp: e1, pbitpos: &bitpos1, poffset: &toffset1); |
16652 | core2 = split_address_to_core_and_offset (exp: e2, pbitpos: &bitpos2, poffset: &toffset2); |
16653 | |
16654 | poly_int64 bytepos1, bytepos2; |
16655 | if (!multiple_p (a: bitpos1, BITS_PER_UNIT, multiple: &bytepos1) |
16656 | || !multiple_p (a: bitpos2, BITS_PER_UNIT, multiple: &bytepos2) |
16657 | || !operand_equal_p (arg0: core1, arg1: core2, flags: 0)) |
16658 | return false; |
16659 | |
16660 | if (toffset1 && toffset2) |
16661 | { |
16662 | type = TREE_TYPE (toffset1); |
16663 | if (type != TREE_TYPE (toffset2)) |
16664 | toffset2 = fold_convert (type, toffset2); |
16665 | |
16666 | tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2); |
16667 | if (!cst_and_fits_in_hwi (tdiff)) |
16668 | return false; |
16669 | |
16670 | *diff = int_cst_value (tdiff); |
16671 | } |
16672 | else if (toffset1 || toffset2) |
16673 | { |
16674 | /* If only one of the offsets is non-constant, the difference cannot |
16675 | be a constant. */ |
16676 | return false; |
16677 | } |
16678 | else |
16679 | *diff = 0; |
16680 | |
16681 | *diff += bytepos1 - bytepos2; |
16682 | return true; |
16683 | } |
16684 | |
16685 | /* Return OFF converted to a pointer offset type suitable as offset for |
16686 | POINTER_PLUS_EXPR. Use location LOC for this conversion. */ |
16687 | tree |
16688 | convert_to_ptrofftype_loc (location_t loc, tree off) |
16689 | { |
16690 | if (ptrofftype_p (TREE_TYPE (off))) |
16691 | return off; |
16692 | return fold_convert_loc (loc, sizetype, arg: off); |
16693 | } |
16694 | |
16695 | /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */ |
16696 | tree |
16697 | fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off) |
16698 | { |
16699 | return fold_build2_loc (loc, code: POINTER_PLUS_EXPR, TREE_TYPE (ptr), |
16700 | op0: ptr, op1: convert_to_ptrofftype_loc (loc, off)); |
16701 | } |
16702 | |
16703 | /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */ |
16704 | tree |
16705 | fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off) |
16706 | { |
16707 | return fold_build2_loc (loc, code: POINTER_PLUS_EXPR, TREE_TYPE (ptr), |
16708 | op0: ptr, size_int (off)); |
16709 | } |
16710 | |
16711 | /* Return a pointer to a NUL-terminated string containing the sequence |
16712 | of bytes corresponding to the representation of the object referred to |
16713 | by SRC (or a subsequence of such bytes within it if SRC is a reference |
16714 | to an initialized constant array plus some constant offset). |
16715 | Set *STRSIZE the number of bytes in the constant sequence including |
16716 | the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET |
16717 | where A is the array that stores the constant sequence that SRC points |
16718 | to and OFFSET is the byte offset of SRC from the beginning of A. SRC |
16719 | need not point to a string or even an array of characters but may point |
16720 | to an object of any type. */ |
16721 | |
16722 | const char * |
16723 | getbyterep (tree src, unsigned HOST_WIDE_INT *strsize) |
16724 | { |
16725 | /* The offset into the array A storing the string, and A's byte size. */ |
16726 | tree offset_node; |
16727 | tree mem_size; |
16728 | |
16729 | if (strsize) |
16730 | *strsize = 0; |
16731 | |
16732 | if (strsize) |
16733 | src = byte_representation (src, &offset_node, &mem_size, NULL); |
16734 | else |
16735 | src = string_constant (src, &offset_node, &mem_size, NULL); |
16736 | if (!src) |
16737 | return NULL; |
16738 | |
16739 | unsigned HOST_WIDE_INT offset = 0; |
16740 | if (offset_node != NULL_TREE) |
16741 | { |
16742 | if (!tree_fits_uhwi_p (offset_node)) |
16743 | return NULL; |
16744 | else |
16745 | offset = tree_to_uhwi (offset_node); |
16746 | } |
16747 | |
16748 | if (!tree_fits_uhwi_p (mem_size)) |
16749 | return NULL; |
16750 | |
16751 | /* ARRAY_SIZE is the byte size of the array the constant sequence |
16752 | is stored in and equal to sizeof A. INIT_BYTES is the number |
16753 | of bytes in the constant sequence used to initialize the array, |
16754 | including any embedded NULs as well as the terminating NUL (for |
16755 | strings), but not including any trailing zeros/NULs past |
16756 | the terminating one appended implicitly to a string literal to |
16757 | zero out the remainder of the array it's stored in. For example, |
16758 | given: |
16759 | const char a[7] = "abc\0d"; |
16760 | n = strlen (a + 1); |
16761 | ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid |
16762 | (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES |
16763 | is equal to strlen (A) + 1. */ |
16764 | const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size); |
16765 | unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src); |
16766 | const char *string = TREE_STRING_POINTER (src); |
16767 | |
16768 | /* Ideally this would turn into a gcc_checking_assert over time. */ |
16769 | if (init_bytes > array_size) |
16770 | init_bytes = array_size; |
16771 | |
16772 | if (init_bytes == 0 || offset >= array_size) |
16773 | return NULL; |
16774 | |
16775 | if (strsize) |
16776 | { |
16777 | /* Compute and store the number of characters from the beginning |
16778 | of the substring at OFFSET to the end, including the terminating |
16779 | nul. Offsets past the initial length refer to null strings. */ |
16780 | if (offset < init_bytes) |
16781 | *strsize = init_bytes - offset; |
16782 | else |
16783 | *strsize = 1; |
16784 | } |
16785 | else |
16786 | { |
16787 | tree eltype = TREE_TYPE (TREE_TYPE (src)); |
16788 | /* Support only properly NUL-terminated single byte strings. */ |
16789 | if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1) |
16790 | return NULL; |
16791 | if (string[init_bytes - 1] != '\0') |
16792 | return NULL; |
16793 | } |
16794 | |
16795 | return offset < init_bytes ? string + offset : "" ; |
16796 | } |
16797 | |
16798 | /* Return a pointer to a NUL-terminated string corresponding to |
16799 | the expression STR referencing a constant string, possibly |
16800 | involving a constant offset. Return null if STR either doesn't |
16801 | reference a constant string or if it involves a nonconstant |
16802 | offset. */ |
16803 | |
16804 | const char * |
16805 | c_getstr (tree str) |
16806 | { |
16807 | return getbyterep (src: str, NULL); |
16808 | } |
16809 | |
16810 | /* Given a tree T, compute which bits in T may be nonzero. */ |
16811 | |
16812 | wide_int |
16813 | tree_nonzero_bits (const_tree t) |
16814 | { |
16815 | switch (TREE_CODE (t)) |
16816 | { |
16817 | case INTEGER_CST: |
16818 | return wi::to_wide (t); |
16819 | case SSA_NAME: |
16820 | return get_nonzero_bits (t); |
16821 | case NON_LVALUE_EXPR: |
16822 | case SAVE_EXPR: |
16823 | return tree_nonzero_bits (TREE_OPERAND (t, 0)); |
16824 | case BIT_AND_EXPR: |
16825 | return wi::bit_and (x: tree_nonzero_bits (TREE_OPERAND (t, 0)), |
16826 | y: tree_nonzero_bits (TREE_OPERAND (t, 1))); |
16827 | case BIT_IOR_EXPR: |
16828 | case BIT_XOR_EXPR: |
16829 | return wi::bit_or (x: tree_nonzero_bits (TREE_OPERAND (t, 0)), |
16830 | y: tree_nonzero_bits (TREE_OPERAND (t, 1))); |
16831 | case COND_EXPR: |
16832 | return wi::bit_or (x: tree_nonzero_bits (TREE_OPERAND (t, 1)), |
16833 | y: tree_nonzero_bits (TREE_OPERAND (t, 2))); |
16834 | CASE_CONVERT: |
16835 | return wide_int::from (x: tree_nonzero_bits (TREE_OPERAND (t, 0)), |
16836 | TYPE_PRECISION (TREE_TYPE (t)), |
16837 | TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0)))); |
16838 | case PLUS_EXPR: |
16839 | if (INTEGRAL_TYPE_P (TREE_TYPE (t))) |
16840 | { |
16841 | wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0)); |
16842 | wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1)); |
16843 | if (wi::bit_and (x: nzbits1, y: nzbits2) == 0) |
16844 | return wi::bit_or (x: nzbits1, y: nzbits2); |
16845 | } |
16846 | break; |
16847 | case LSHIFT_EXPR: |
16848 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) |
16849 | { |
16850 | tree type = TREE_TYPE (t); |
16851 | wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0)); |
16852 | wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), |
16853 | TYPE_PRECISION (type)); |
16854 | return wi::neg_p (x: arg1) |
16855 | ? wi::rshift (x: nzbits, y: -arg1, TYPE_SIGN (type)) |
16856 | : wi::lshift (x: nzbits, y: arg1); |
16857 | } |
16858 | break; |
16859 | case RSHIFT_EXPR: |
16860 | if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST) |
16861 | { |
16862 | tree type = TREE_TYPE (t); |
16863 | wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0)); |
16864 | wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1), |
16865 | TYPE_PRECISION (type)); |
16866 | return wi::neg_p (x: arg1) |
16867 | ? wi::lshift (x: nzbits, y: -arg1) |
16868 | : wi::rshift (x: nzbits, y: arg1, TYPE_SIGN (type)); |
16869 | } |
16870 | break; |
16871 | default: |
16872 | break; |
16873 | } |
16874 | |
16875 | return wi::shwi (val: -1, TYPE_PRECISION (TREE_TYPE (t))); |
16876 | } |
16877 | |
16878 | /* Helper function for address compare simplifications in match.pd. |
16879 | OP0 and OP1 are ADDR_EXPR operands being compared by CODE. |
16880 | TYPE is the type of comparison operands. |
16881 | BASE0, BASE1, OFF0 and OFF1 are set by the function. |
16882 | GENERIC is true if GENERIC folding and false for GIMPLE folding. |
16883 | Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1}, |
16884 | 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1, |
16885 | and 2 if unknown. */ |
16886 | |
16887 | int |
16888 | address_compare (tree_code code, tree type, tree op0, tree op1, |
16889 | tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1, |
16890 | bool generic) |
16891 | { |
16892 | if (TREE_CODE (op0) == SSA_NAME) |
16893 | op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0)); |
16894 | if (TREE_CODE (op1) == SSA_NAME) |
16895 | op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1)); |
16896 | gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR); |
16897 | gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR); |
16898 | base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0); |
16899 | base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1); |
16900 | if (base0 && TREE_CODE (base0) == MEM_REF) |
16901 | { |
16902 | off0 += mem_ref_offset (base0).force_shwi (); |
16903 | base0 = TREE_OPERAND (base0, 0); |
16904 | } |
16905 | if (base1 && TREE_CODE (base1) == MEM_REF) |
16906 | { |
16907 | off1 += mem_ref_offset (base1).force_shwi (); |
16908 | base1 = TREE_OPERAND (base1, 0); |
16909 | } |
16910 | if (base0 == NULL_TREE || base1 == NULL_TREE) |
16911 | return 2; |
16912 | |
16913 | int equal = 2; |
16914 | /* Punt in GENERIC on variables with value expressions; |
16915 | the value expressions might point to fields/elements |
16916 | of other vars etc. */ |
16917 | if (generic |
16918 | && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0)) |
16919 | || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1)))) |
16920 | return 2; |
16921 | else if (decl_in_symtab_p (decl: base0) && decl_in_symtab_p (decl: base1)) |
16922 | { |
16923 | symtab_node *node0 = symtab_node::get_create (node: base0); |
16924 | symtab_node *node1 = symtab_node::get_create (node: base1); |
16925 | equal = node0->equal_address_to (s2: node1); |
16926 | } |
16927 | else if ((DECL_P (base0) |
16928 | || TREE_CODE (base0) == SSA_NAME |
16929 | || TREE_CODE (base0) == STRING_CST) |
16930 | && (DECL_P (base1) |
16931 | || TREE_CODE (base1) == SSA_NAME |
16932 | || TREE_CODE (base1) == STRING_CST)) |
16933 | equal = (base0 == base1); |
16934 | /* Assume different STRING_CSTs with the same content will be |
16935 | merged. */ |
16936 | if (equal == 0 |
16937 | && TREE_CODE (base0) == STRING_CST |
16938 | && TREE_CODE (base1) == STRING_CST |
16939 | && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1) |
16940 | && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1), |
16941 | TREE_STRING_LENGTH (base0)) == 0) |
16942 | equal = 1; |
16943 | if (equal == 1) |
16944 | { |
16945 | if (code == EQ_EXPR |
16946 | || code == NE_EXPR |
16947 | /* If the offsets are equal we can ignore overflow. */ |
16948 | || known_eq (off0, off1) |
16949 | || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)) |
16950 | /* Or if we compare using pointers to decls or strings. */ |
16951 | || (POINTER_TYPE_P (type) |
16952 | && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST))) |
16953 | return 1; |
16954 | return 2; |
16955 | } |
16956 | if (equal != 0) |
16957 | return equal; |
16958 | if (code != EQ_EXPR && code != NE_EXPR) |
16959 | return 2; |
16960 | |
16961 | /* At this point we know (or assume) the two pointers point at |
16962 | different objects. */ |
16963 | HOST_WIDE_INT ioff0 = -1, ioff1 = -1; |
16964 | off0.is_constant (const_value: &ioff0); |
16965 | off1.is_constant (const_value: &ioff1); |
16966 | /* Punt on non-zero offsets from functions. */ |
16967 | if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0) |
16968 | || (TREE_CODE (base1) == FUNCTION_DECL && ioff1)) |
16969 | return 2; |
16970 | /* Or if the bases are neither decls nor string literals. */ |
16971 | if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST) |
16972 | return 2; |
16973 | if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST) |
16974 | return 2; |
16975 | /* For initializers, assume addresses of different functions are |
16976 | different. */ |
16977 | if (folding_initializer |
16978 | && TREE_CODE (base0) == FUNCTION_DECL |
16979 | && TREE_CODE (base1) == FUNCTION_DECL) |
16980 | return 0; |
16981 | |
16982 | /* Compute whether one address points to the start of one |
16983 | object and another one to the end of another one. */ |
16984 | poly_int64 size0 = 0, size1 = 0; |
16985 | if (TREE_CODE (base0) == STRING_CST) |
16986 | { |
16987 | if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0)) |
16988 | equal = 2; |
16989 | else |
16990 | size0 = TREE_STRING_LENGTH (base0); |
16991 | } |
16992 | else if (TREE_CODE (base0) == FUNCTION_DECL) |
16993 | size0 = 1; |
16994 | else |
16995 | { |
16996 | tree sz0 = DECL_SIZE_UNIT (base0); |
16997 | if (!tree_fits_poly_int64_p (sz0)) |
16998 | equal = 2; |
16999 | else |
17000 | size0 = tree_to_poly_int64 (sz0); |
17001 | } |
17002 | if (TREE_CODE (base1) == STRING_CST) |
17003 | { |
17004 | if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1)) |
17005 | equal = 2; |
17006 | else |
17007 | size1 = TREE_STRING_LENGTH (base1); |
17008 | } |
17009 | else if (TREE_CODE (base1) == FUNCTION_DECL) |
17010 | size1 = 1; |
17011 | else |
17012 | { |
17013 | tree sz1 = DECL_SIZE_UNIT (base1); |
17014 | if (!tree_fits_poly_int64_p (sz1)) |
17015 | equal = 2; |
17016 | else |
17017 | size1 = tree_to_poly_int64 (sz1); |
17018 | } |
17019 | if (equal == 0) |
17020 | { |
17021 | /* If one offset is pointing (or could be) to the beginning of one |
17022 | object and the other is pointing to one past the last byte of the |
17023 | other object, punt. */ |
17024 | if (maybe_eq (a: off0, b: 0) && maybe_eq (a: off1, b: size1)) |
17025 | equal = 2; |
17026 | else if (maybe_eq (a: off1, b: 0) && maybe_eq (a: off0, b: size0)) |
17027 | equal = 2; |
17028 | /* If both offsets are the same, there are some cases we know that are |
17029 | ok. Either if we know they aren't zero, or if we know both sizes |
17030 | are no zero. */ |
17031 | if (equal == 2 |
17032 | && known_eq (off0, off1) |
17033 | && (known_ne (off0, 0) |
17034 | || (known_ne (size0, 0) && known_ne (size1, 0)))) |
17035 | equal = 0; |
17036 | } |
17037 | |
17038 | /* At this point, equal is 2 if either one or both pointers are out of |
17039 | bounds of their object, or one points to start of its object and the |
17040 | other points to end of its object. This is unspecified behavior |
17041 | e.g. in C++. Otherwise equal is 0. */ |
17042 | if (folding_cxx_constexpr && equal) |
17043 | return equal; |
17044 | |
17045 | /* When both pointers point to string literals, even when equal is 0, |
17046 | due to tail merging of string literals the pointers might be the same. */ |
17047 | if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST) |
17048 | { |
17049 | if (ioff0 < 0 |
17050 | || ioff1 < 0 |
17051 | || ioff0 > TREE_STRING_LENGTH (base0) |
17052 | || ioff1 > TREE_STRING_LENGTH (base1)) |
17053 | return 2; |
17054 | |
17055 | /* If the bytes in the string literals starting at the pointers |
17056 | differ, the pointers need to be different. */ |
17057 | if (memcmp (TREE_STRING_POINTER (base0) + ioff0, |
17058 | TREE_STRING_POINTER (base1) + ioff1, |
17059 | MIN (TREE_STRING_LENGTH (base0) - ioff0, |
17060 | TREE_STRING_LENGTH (base1) - ioff1)) == 0) |
17061 | { |
17062 | HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1); |
17063 | if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin, |
17064 | TREE_STRING_POINTER (base1) + ioff1 - ioffmin, |
17065 | n: ioffmin) == 0) |
17066 | /* If even the bytes in the string literal before the |
17067 | pointers are the same, the string literals could be |
17068 | tail merged. */ |
17069 | return 2; |
17070 | } |
17071 | return 0; |
17072 | } |
17073 | |
17074 | if (folding_cxx_constexpr) |
17075 | return 0; |
17076 | |
17077 | /* If this is a pointer comparison, ignore for now even |
17078 | valid equalities where one pointer is the offset zero |
17079 | of one object and the other to one past end of another one. */ |
17080 | if (!INTEGRAL_TYPE_P (type)) |
17081 | return 0; |
17082 | |
17083 | /* Assume that string literals can't be adjacent to variables |
17084 | (automatic or global). */ |
17085 | if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST) |
17086 | return 0; |
17087 | |
17088 | /* Assume that automatic variables can't be adjacent to global |
17089 | variables. */ |
17090 | if (is_global_var (t: base0) != is_global_var (t: base1)) |
17091 | return 0; |
17092 | |
17093 | return equal; |
17094 | } |
17095 | |
17096 | /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */ |
17097 | tree |
17098 | ctor_single_nonzero_element (const_tree t) |
17099 | { |
17100 | unsigned HOST_WIDE_INT idx; |
17101 | constructor_elt *ce; |
17102 | tree elt = NULL_TREE; |
17103 | |
17104 | if (TREE_CODE (t) != CONSTRUCTOR) |
17105 | return NULL_TREE; |
17106 | for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), ix: idx, ptr: &ce); idx++) |
17107 | if (!integer_zerop (ce->value) && !real_zerop (ce->value)) |
17108 | { |
17109 | if (elt) |
17110 | return NULL_TREE; |
17111 | elt = ce->value; |
17112 | } |
17113 | return elt; |
17114 | } |
17115 | |
17116 | #if CHECKING_P |
17117 | |
17118 | namespace selftest { |
17119 | |
17120 | /* Helper functions for writing tests of folding trees. */ |
17121 | |
17122 | /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */ |
17123 | |
17124 | static void |
17125 | assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs, |
17126 | tree constant) |
17127 | { |
17128 | ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs)); |
17129 | } |
17130 | |
17131 | /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR |
17132 | wrapping WRAPPED_EXPR. */ |
17133 | |
17134 | static void |
17135 | assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs, |
17136 | tree wrapped_expr) |
17137 | { |
17138 | tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs); |
17139 | ASSERT_NE (wrapped_expr, result); |
17140 | ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result)); |
17141 | ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0)); |
17142 | } |
17143 | |
17144 | /* Verify that various arithmetic binary operations are folded |
17145 | correctly. */ |
17146 | |
17147 | static void |
17148 | test_arithmetic_folding () |
17149 | { |
17150 | tree type = integer_type_node; |
17151 | tree x = create_tmp_var_raw (type, "x" ); |
17152 | tree zero = build_zero_cst (type); |
17153 | tree one = build_int_cst (type, 1); |
17154 | |
17155 | /* Addition. */ |
17156 | /* 1 <-- (0 + 1) */ |
17157 | assert_binop_folds_to_const (lhs: zero, code: PLUS_EXPR, rhs: one, |
17158 | constant: one); |
17159 | assert_binop_folds_to_const (lhs: one, code: PLUS_EXPR, rhs: zero, |
17160 | constant: one); |
17161 | |
17162 | /* (nonlvalue)x <-- (x + 0) */ |
17163 | assert_binop_folds_to_nonlvalue (lhs: x, code: PLUS_EXPR, rhs: zero, |
17164 | wrapped_expr: x); |
17165 | |
17166 | /* Subtraction. */ |
17167 | /* 0 <-- (x - x) */ |
17168 | assert_binop_folds_to_const (lhs: x, code: MINUS_EXPR, rhs: x, |
17169 | constant: zero); |
17170 | assert_binop_folds_to_nonlvalue (lhs: x, code: MINUS_EXPR, rhs: zero, |
17171 | wrapped_expr: x); |
17172 | |
17173 | /* Multiplication. */ |
17174 | /* 0 <-- (x * 0) */ |
17175 | assert_binop_folds_to_const (lhs: x, code: MULT_EXPR, rhs: zero, |
17176 | constant: zero); |
17177 | |
17178 | /* (nonlvalue)x <-- (x * 1) */ |
17179 | assert_binop_folds_to_nonlvalue (lhs: x, code: MULT_EXPR, rhs: one, |
17180 | wrapped_expr: x); |
17181 | } |
17182 | |
17183 | namespace test_fold_vec_perm_cst { |
17184 | |
17185 | /* Build a VECTOR_CST corresponding to VMODE, and has |
17186 | encoding given by NPATTERNS, NELTS_PER_PATTERN and STEP. |
17187 | Fill it with randomized elements, using rand() % THRESHOLD. */ |
17188 | |
17189 | static tree |
17190 | build_vec_cst_rand (machine_mode vmode, unsigned npatterns, |
17191 | unsigned nelts_per_pattern, |
17192 | int step = 0, bool natural_stepped = false, |
17193 | int threshold = 100) |
17194 | { |
17195 | tree inner_type = lang_hooks.types.type_for_mode (GET_MODE_INNER (vmode), 1); |
17196 | tree vectype = build_vector_type_for_mode (inner_type, vmode); |
17197 | tree_vector_builder builder (vectype, npatterns, nelts_per_pattern); |
17198 | |
17199 | // Fill a0 for each pattern |
17200 | for (unsigned i = 0; i < npatterns; i++) |
17201 | builder.quick_push (obj: build_int_cst (inner_type, rand () % threshold)); |
17202 | |
17203 | if (nelts_per_pattern == 1) |
17204 | return builder.build (); |
17205 | |
17206 | // Fill a1 for each pattern |
17207 | for (unsigned i = 0; i < npatterns; i++) |
17208 | { |
17209 | tree a1; |
17210 | if (natural_stepped) |
17211 | { |
17212 | tree a0 = builder[i]; |
17213 | wide_int a0_val = wi::to_wide (t: a0); |
17214 | wide_int a1_val = a0_val + step; |
17215 | a1 = wide_int_to_tree (type: inner_type, cst: a1_val); |
17216 | } |
17217 | else |
17218 | a1 = build_int_cst (inner_type, rand () % threshold); |
17219 | builder.quick_push (obj: a1); |
17220 | } |
17221 | if (nelts_per_pattern == 2) |
17222 | return builder.build (); |
17223 | |
17224 | for (unsigned i = npatterns * 2; i < npatterns * nelts_per_pattern; i++) |
17225 | { |
17226 | tree prev_elem = builder[i - npatterns]; |
17227 | wide_int prev_elem_val = wi::to_wide (t: prev_elem); |
17228 | wide_int val = prev_elem_val + step; |
17229 | builder.quick_push (obj: wide_int_to_tree (type: inner_type, cst: val)); |
17230 | } |
17231 | |
17232 | return builder.build (); |
17233 | } |
17234 | |
17235 | /* Validate result of VEC_PERM_EXPR folding for the unit-tests below, |
17236 | when result is VLA. */ |
17237 | |
17238 | static void |
17239 | validate_res (unsigned npatterns, unsigned nelts_per_pattern, |
17240 | tree res, tree *expected_res) |
17241 | { |
17242 | /* Actual npatterns and encoded_elts in res may be less than expected due |
17243 | to canonicalization. */ |
17244 | ASSERT_TRUE (res != NULL_TREE); |
17245 | ASSERT_TRUE (VECTOR_CST_NPATTERNS (res) <= npatterns); |
17246 | ASSERT_TRUE (vector_cst_encoded_nelts (res) <= npatterns * nelts_per_pattern); |
17247 | |
17248 | for (unsigned i = 0; i < npatterns * nelts_per_pattern; i++) |
17249 | ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0)); |
17250 | } |
17251 | |
17252 | /* Validate result of VEC_PERM_EXPR folding for the unit-tests below, |
17253 | when the result is VLS. */ |
17254 | |
17255 | static void |
17256 | validate_res_vls (tree res, tree *expected_res, unsigned expected_nelts) |
17257 | { |
17258 | ASSERT_TRUE (known_eq (VECTOR_CST_NELTS (res), expected_nelts)); |
17259 | for (unsigned i = 0; i < expected_nelts; i++) |
17260 | ASSERT_TRUE (operand_equal_p (VECTOR_CST_ELT (res, i), expected_res[i], 0)); |
17261 | } |
17262 | |
17263 | /* Helper routine to push multiple elements into BUILDER. */ |
17264 | template<unsigned N> |
17265 | static void builder_push_elems (vec_perm_builder& builder, |
17266 | poly_uint64 (&elems)[N]) |
17267 | { |
17268 | for (unsigned i = 0; i < N; i++) |
17269 | builder.quick_push (obj: elems[i]); |
17270 | } |
17271 | |
17272 | #define ARG0(index) vector_cst_elt (arg0, index) |
17273 | #define ARG1(index) vector_cst_elt (arg1, index) |
17274 | |
17275 | /* Test cases where result is VNx4SI and input vectors are V4SI. */ |
17276 | |
17277 | static void |
17278 | test_vnx4si_v4si (machine_mode vnx4si_mode, machine_mode v4si_mode) |
17279 | { |
17280 | for (int i = 0; i < 10; i++) |
17281 | { |
17282 | /* Case 1: |
17283 | sel = { 0, 4, 1, 5, ... } |
17284 | res = { arg[0], arg1[0], arg0[1], arg1[1], ...} // (4, 1) */ |
17285 | { |
17286 | tree arg0 = build_vec_cst_rand (vmode: v4si_mode, npatterns: 4, nelts_per_pattern: 1, step: 0); |
17287 | tree arg1 = build_vec_cst_rand (vmode: v4si_mode, npatterns: 4, nelts_per_pattern: 1, step: 0); |
17288 | |
17289 | tree inner_type |
17290 | = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1); |
17291 | tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode); |
17292 | |
17293 | poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (node: res_type); |
17294 | vec_perm_builder builder (res_len, 4, 1); |
17295 | poly_uint64 mask_elems[] = { 0, 4, 1, 5 }; |
17296 | builder_push_elems (builder, elems&: mask_elems); |
17297 | |
17298 | vec_perm_indices sel (builder, 2, res_len); |
17299 | tree res = fold_vec_perm_cst (type: res_type, arg0, arg1, sel); |
17300 | |
17301 | tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) }; |
17302 | validate_res (npatterns: 4, nelts_per_pattern: 1, res, expected_res); |
17303 | } |
17304 | |
17305 | /* Case 2: Same as case 1, but contains an out of bounds access which |
17306 | should wrap around. |
17307 | sel = {0, 8, 4, 12, ...} (4, 1) |
17308 | res = { arg0[0], arg0[0], arg1[0], arg1[0], ... } (4, 1). */ |
17309 | { |
17310 | tree arg0 = build_vec_cst_rand (vmode: v4si_mode, npatterns: 4, nelts_per_pattern: 1, step: 0); |
17311 | tree arg1 = build_vec_cst_rand (vmode: v4si_mode, npatterns: 4, nelts_per_pattern: 1, step: 0); |
17312 | |
17313 | tree inner_type |
17314 | = lang_hooks.types.type_for_mode (GET_MODE_INNER (vnx4si_mode), 1); |
17315 | tree res_type = build_vector_type_for_mode (inner_type, vnx4si_mode); |
17316 | |
17317 | poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (node: res_type); |
17318 | vec_perm_builder builder (res_len, 4, 1); |
17319 | poly_uint64 mask_elems[] = { 0, 8, 4, 12 }; |
17320 | builder_push_elems (builder, elems&: mask_elems); |
17321 | |
17322 | vec_perm_indices sel (builder, 2, res_len); |
17323 | tree res = fold_vec_perm_cst (type: res_type, arg0, arg1, sel); |
17324 | |
17325 | tree expected_res[] = { ARG0(0), ARG0(0), ARG1(0), ARG1(0) }; |
17326 | validate_res (npatterns: 4, nelts_per_pattern: 1, res, expected_res); |
17327 | } |
17328 | } |
17329 | } |
17330 | |
17331 | /* Test cases where result is V4SI and input vectors are VNx4SI. */ |
17332 | |
17333 | static void |
17334 | test_v4si_vnx4si (machine_mode v4si_mode, machine_mode vnx4si_mode) |
17335 | { |
17336 | for (int i = 0; i < 10; i++) |
17337 | { |
17338 | /* Case 1: |
17339 | sel = { 0, 1, 2, 3} |
17340 | res = { arg0[0], arg0[1], arg0[2], arg0[3] }. */ |
17341 | { |
17342 | tree arg0 = build_vec_cst_rand (vmode: vnx4si_mode, npatterns: 4, nelts_per_pattern: 1); |
17343 | tree arg1 = build_vec_cst_rand (vmode: vnx4si_mode, npatterns: 4, nelts_per_pattern: 1); |
17344 | |
17345 | tree inner_type |
17346 | = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1); |
17347 | tree res_type = build_vector_type_for_mode (inner_type, v4si_mode); |
17348 | |
17349 | poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (node: res_type); |
17350 | vec_perm_builder builder (res_len, 4, 1); |
17351 | poly_uint64 mask_elems[] = {0, 1, 2, 3}; |
17352 | builder_push_elems (builder, elems&: mask_elems); |
17353 | |
17354 | vec_perm_indices sel (builder, 2, res_len); |
17355 | tree res = fold_vec_perm_cst (type: res_type, arg0, arg1, sel); |
17356 | |
17357 | tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2), ARG0(3) }; |
17358 | validate_res_vls (res, expected_res, expected_nelts: 4); |
17359 | } |
17360 | |
17361 | /* Case 2: Same as Case 1, but crossing input vector. |
17362 | sel = {0, 2, 4, 6} |
17363 | In this case,the index 4 is ambiguous since len = 4 + 4x. |
17364 | Since we cannot determine, which vector to choose from during |
17365 | compile time, should return NULL_TREE. */ |
17366 | { |
17367 | tree arg0 = build_vec_cst_rand (vmode: vnx4si_mode, npatterns: 4, nelts_per_pattern: 1); |
17368 | tree arg1 = build_vec_cst_rand (vmode: vnx4si_mode, npatterns: 4, nelts_per_pattern: 1); |
17369 | |
17370 | tree inner_type |
17371 | = lang_hooks.types.type_for_mode (GET_MODE_INNER (v4si_mode), 1); |
17372 | tree res_type = build_vector_type_for_mode (inner_type, v4si_mode); |
17373 | |
17374 | poly_uint64 res_len = TYPE_VECTOR_SUBPARTS (node: res_type); |
17375 | vec_perm_builder builder (res_len, 4, 1); |
17376 | poly_uint64 mask_elems[] = {0, 2, 4, 6}; |
17377 | builder_push_elems (builder, elems&: mask_elems); |
17378 | |
17379 | vec_perm_indices sel (builder, 2, res_len); |
17380 | const char *reason; |
17381 | tree res = fold_vec_perm_cst (type: res_type, arg0, arg1, sel, reason: &reason); |
17382 | |
17383 | ASSERT_TRUE (res == NULL_TREE); |
17384 | ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len" )); |
17385 | } |
17386 | } |
17387 | } |
17388 | |
17389 | /* Test all input vectors. */ |
17390 | |
17391 | static void |
17392 | test_all_nunits (machine_mode vmode) |
17393 | { |
17394 | /* Test with 10 different inputs. */ |
17395 | for (int i = 0; i < 10; i++) |
17396 | { |
17397 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17398 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17399 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17400 | |
17401 | /* Case 1: mask = {0, ...} // (1, 1) |
17402 | res = { arg0[0], ... } // (1, 1) */ |
17403 | { |
17404 | vec_perm_builder builder (len, 1, 1); |
17405 | builder.quick_push (obj: 0); |
17406 | vec_perm_indices sel (builder, 2, len); |
17407 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17408 | tree expected_res[] = { ARG0(0) }; |
17409 | validate_res (npatterns: 1, nelts_per_pattern: 1, res, expected_res); |
17410 | } |
17411 | |
17412 | /* Case 2: mask = {len, ...} // (1, 1) |
17413 | res = { arg1[0], ... } // (1, 1) */ |
17414 | { |
17415 | vec_perm_builder builder (len, 1, 1); |
17416 | builder.quick_push (obj: len); |
17417 | vec_perm_indices sel (builder, 2, len); |
17418 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17419 | |
17420 | tree expected_res[] = { ARG1(0) }; |
17421 | validate_res (npatterns: 1, nelts_per_pattern: 1, res, expected_res); |
17422 | } |
17423 | } |
17424 | } |
17425 | |
17426 | /* Test all vectors which contain at-least 2 elements. */ |
17427 | |
17428 | static void |
17429 | test_nunits_min_2 (machine_mode vmode) |
17430 | { |
17431 | for (int i = 0; i < 10; i++) |
17432 | { |
17433 | /* Case 1: mask = { 0, len, ... } // (2, 1) |
17434 | res = { arg0[0], arg1[0], ... } // (2, 1) */ |
17435 | { |
17436 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17437 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17438 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17439 | |
17440 | vec_perm_builder builder (len, 2, 1); |
17441 | poly_uint64 mask_elems[] = { 0, len }; |
17442 | builder_push_elems (builder, elems&: mask_elems); |
17443 | |
17444 | vec_perm_indices sel (builder, 2, len); |
17445 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17446 | |
17447 | tree expected_res[] = { ARG0(0), ARG1(0) }; |
17448 | validate_res (npatterns: 2, nelts_per_pattern: 1, res, expected_res); |
17449 | } |
17450 | |
17451 | /* Case 2: mask = { 0, len, 1, len+1, ... } // (2, 2) |
17452 | res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) */ |
17453 | { |
17454 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17455 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17456 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17457 | |
17458 | vec_perm_builder builder (len, 2, 2); |
17459 | poly_uint64 mask_elems[] = { 0, len, 1, len + 1 }; |
17460 | builder_push_elems (builder, elems&: mask_elems); |
17461 | |
17462 | vec_perm_indices sel (builder, 2, len); |
17463 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17464 | |
17465 | tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) }; |
17466 | validate_res (npatterns: 2, nelts_per_pattern: 2, res, expected_res); |
17467 | } |
17468 | |
17469 | /* Case 4: mask = {0, 0, 1, ...} // (1, 3) |
17470 | Test that the stepped sequence of the pattern selects from |
17471 | same input pattern. Since input vectors have npatterns = 2, |
17472 | and step (a2 - a1) = 1, step is not a multiple of npatterns |
17473 | in input vector. So return NULL_TREE. */ |
17474 | { |
17475 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 2, nelts_per_pattern: 3, step: 1, natural_stepped: true); |
17476 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 2, nelts_per_pattern: 3, step: 1); |
17477 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17478 | |
17479 | vec_perm_builder builder (len, 1, 3); |
17480 | poly_uint64 mask_elems[] = { 0, 0, 1 }; |
17481 | builder_push_elems (builder, elems&: mask_elems); |
17482 | |
17483 | vec_perm_indices sel (builder, 2, len); |
17484 | const char *reason; |
17485 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, |
17486 | reason: &reason); |
17487 | ASSERT_TRUE (res == NULL_TREE); |
17488 | ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns" )); |
17489 | } |
17490 | |
17491 | /* Case 5: mask = {len, 0, 1, ...} // (1, 3) |
17492 | Test that stepped sequence of the pattern selects from arg0. |
17493 | res = { arg1[0], arg0[0], arg0[1], ... } // (1, 3) */ |
17494 | { |
17495 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1, natural_stepped: true); |
17496 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17497 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17498 | |
17499 | vec_perm_builder builder (len, 1, 3); |
17500 | poly_uint64 mask_elems[] = { len, 0, 1 }; |
17501 | builder_push_elems (builder, elems&: mask_elems); |
17502 | |
17503 | vec_perm_indices sel (builder, 2, len); |
17504 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17505 | |
17506 | tree expected_res[] = { ARG1(0), ARG0(0), ARG0(1) }; |
17507 | validate_res (npatterns: 1, nelts_per_pattern: 3, res, expected_res); |
17508 | } |
17509 | |
17510 | /* Case 6: PR111648 - a1 chooses base element from input vector arg. |
17511 | In this case ensure that arg has a natural stepped sequence |
17512 | to preserve arg's encoding. |
17513 | |
17514 | As a concrete example, consider: |
17515 | arg0: { -16, -9, -10, ... } // (1, 3) |
17516 | arg1: { -12, -5, -6, ... } // (1, 3) |
17517 | sel = { 0, len, len + 1, ... } // (1, 3) |
17518 | |
17519 | This will create res with following encoding: |
17520 | res = { arg0[0], arg1[0], arg1[1], ... } // (1, 3) |
17521 | = { -16, -12, -5, ... } |
17522 | |
17523 | The step in above encoding would be: (-5) - (-12) = 7 |
17524 | And hence res[3] would be computed as -5 + 7 = 2. |
17525 | instead of arg1[2], ie, -6. |
17526 | Ensure that valid_mask_for_fold_vec_perm_cst returns false |
17527 | for this case. */ |
17528 | { |
17529 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17530 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17531 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17532 | |
17533 | vec_perm_builder builder (len, 1, 3); |
17534 | poly_uint64 mask_elems[] = { 0, len, len+1 }; |
17535 | builder_push_elems (builder, elems&: mask_elems); |
17536 | |
17537 | vec_perm_indices sel (builder, 2, len); |
17538 | const char *reason; |
17539 | /* FIXME: It may happen that build_vec_cst_rand may build a natural |
17540 | stepped pattern, even if we didn't explicitly tell it to. So folding |
17541 | may not always fail, but if it does, ensure that's because arg1 does |
17542 | not have a natural stepped sequence (and not due to other reason) */ |
17543 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, reason: &reason); |
17544 | if (res == NULL_TREE) |
17545 | ASSERT_TRUE (!strcmp (reason, "not a natural stepped sequence" )); |
17546 | } |
17547 | |
17548 | /* Case 7: Same as Case 6, except that arg1 contains natural stepped |
17549 | sequence and thus folding should be valid for this case. */ |
17550 | { |
17551 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17552 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1, natural_stepped: true); |
17553 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17554 | |
17555 | vec_perm_builder builder (len, 1, 3); |
17556 | poly_uint64 mask_elems[] = { 0, len, len+1 }; |
17557 | builder_push_elems (builder, elems&: mask_elems); |
17558 | |
17559 | vec_perm_indices sel (builder, 2, len); |
17560 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17561 | |
17562 | tree expected_res[] = { ARG0(0), ARG1(0), ARG1(1) }; |
17563 | validate_res (npatterns: 1, nelts_per_pattern: 3, res, expected_res); |
17564 | } |
17565 | } |
17566 | } |
17567 | |
17568 | /* Test all vectors which contain at-least 4 elements. */ |
17569 | |
17570 | static void |
17571 | test_nunits_min_4 (machine_mode vmode) |
17572 | { |
17573 | for (int i = 0; i < 10; i++) |
17574 | { |
17575 | /* Case 1: mask = { 0, len, 1, len+1, ... } // (4, 1) |
17576 | res: { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (4, 1) */ |
17577 | { |
17578 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17579 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17580 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17581 | |
17582 | vec_perm_builder builder (len, 4, 1); |
17583 | poly_uint64 mask_elems[] = { 0, len, 1, len + 1 }; |
17584 | builder_push_elems (builder, elems&: mask_elems); |
17585 | |
17586 | vec_perm_indices sel (builder, 2, len); |
17587 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17588 | |
17589 | tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) }; |
17590 | validate_res (npatterns: 4, nelts_per_pattern: 1, res, expected_res); |
17591 | } |
17592 | |
17593 | /* Case 2: sel = {0, 1, 2, ...} // (1, 3) |
17594 | res: { arg0[0], arg0[1], arg0[2], ... } // (1, 3) */ |
17595 | { |
17596 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17597 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17598 | poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17599 | |
17600 | vec_perm_builder builder (arg0_len, 1, 3); |
17601 | poly_uint64 mask_elems[] = {0, 1, 2}; |
17602 | builder_push_elems (builder, elems&: mask_elems); |
17603 | |
17604 | vec_perm_indices sel (builder, 2, arg0_len); |
17605 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17606 | tree expected_res[] = { ARG0(0), ARG0(1), ARG0(2) }; |
17607 | validate_res (npatterns: 1, nelts_per_pattern: 3, res, expected_res); |
17608 | } |
17609 | |
17610 | /* Case 3: sel = {len, len+1, len+2, ...} // (1, 3) |
17611 | res: { arg1[0], arg1[1], arg1[2], ... } // (1, 3) */ |
17612 | { |
17613 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17614 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17615 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17616 | |
17617 | vec_perm_builder builder (len, 1, 3); |
17618 | poly_uint64 mask_elems[] = {len, len + 1, len + 2}; |
17619 | builder_push_elems (builder, elems&: mask_elems); |
17620 | |
17621 | vec_perm_indices sel (builder, 2, len); |
17622 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17623 | tree expected_res[] = { ARG1(0), ARG1(1), ARG1(2) }; |
17624 | validate_res (npatterns: 1, nelts_per_pattern: 3, res, expected_res); |
17625 | } |
17626 | |
17627 | /* Case 4: |
17628 | sel = { len, 0, 2, ... } // (1, 3) |
17629 | This should return NULL because we cross the input vectors. |
17630 | Because, |
17631 | Let's assume len = C + Cx |
17632 | a1 = 0 |
17633 | S = 2 |
17634 | esel = arg0_len / sel_npatterns = C + Cx |
17635 | ae = 0 + (esel - 2) * S |
17636 | = 0 + (C + Cx - 2) * 2 |
17637 | = 2(C-2) + 2Cx |
17638 | |
17639 | For C >= 4: |
17640 | Let q1 = a1 / arg0_len = 0 / (C + Cx) = 0 |
17641 | Let qe = ae / arg0_len = (2(C-2) + 2Cx) / (C + Cx) = 1 |
17642 | Since q1 != qe, we cross input vectors. |
17643 | So return NULL_TREE. */ |
17644 | { |
17645 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17646 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 2); |
17647 | poly_uint64 arg0_len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17648 | |
17649 | vec_perm_builder builder (arg0_len, 1, 3); |
17650 | poly_uint64 mask_elems[] = { arg0_len, 0, 2 }; |
17651 | builder_push_elems (builder, elems&: mask_elems); |
17652 | |
17653 | vec_perm_indices sel (builder, 2, arg0_len); |
17654 | const char *reason; |
17655 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, reason: &reason); |
17656 | ASSERT_TRUE (res == NULL_TREE); |
17657 | ASSERT_TRUE (!strcmp (reason, "crossed input vectors" )); |
17658 | } |
17659 | |
17660 | /* Case 5: npatterns(arg0) = 4 > npatterns(sel) = 2 |
17661 | mask = { 0, len, 1, len + 1, ...} // (2, 2) |
17662 | res = { arg0[0], arg1[0], arg0[1], arg1[1], ... } // (2, 2) |
17663 | |
17664 | Note that fold_vec_perm_cst will set |
17665 | res_npatterns = max(4, max(4, 2)) = 4 |
17666 | However after canonicalizing, we will end up with shape (2, 2). */ |
17667 | { |
17668 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 4, nelts_per_pattern: 1); |
17669 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 4, nelts_per_pattern: 1); |
17670 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17671 | |
17672 | vec_perm_builder builder (len, 2, 2); |
17673 | poly_uint64 mask_elems[] = { 0, len, 1, len + 1 }; |
17674 | builder_push_elems (builder, elems&: mask_elems); |
17675 | |
17676 | vec_perm_indices sel (builder, 2, len); |
17677 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17678 | tree expected_res[] = { ARG0(0), ARG1(0), ARG0(1), ARG1(1) }; |
17679 | validate_res (npatterns: 2, nelts_per_pattern: 2, res, expected_res); |
17680 | } |
17681 | |
17682 | /* Case 6: Test combination in sel, where one pattern is dup and other |
17683 | is stepped sequence. |
17684 | sel = { 0, 0, 0, 1, 0, 2, ... } // (2, 3) |
17685 | res = { arg0[0], arg0[0], arg0[0], |
17686 | arg0[1], arg0[0], arg0[2], ... } // (2, 3) */ |
17687 | { |
17688 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17689 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17690 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17691 | |
17692 | vec_perm_builder builder (len, 2, 3); |
17693 | poly_uint64 mask_elems[] = { 0, 0, 0, 1, 0, 2 }; |
17694 | builder_push_elems (builder, elems&: mask_elems); |
17695 | |
17696 | vec_perm_indices sel (builder, 2, len); |
17697 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17698 | |
17699 | tree expected_res[] = { ARG0(0), ARG0(0), ARG0(0), |
17700 | ARG0(1), ARG0(0), ARG0(2) }; |
17701 | validate_res (npatterns: 2, nelts_per_pattern: 3, res, expected_res); |
17702 | } |
17703 | |
17704 | /* Case 7: PR111048: Check that we set arg_npatterns correctly, |
17705 | when arg0, arg1 and sel have different number of patterns. |
17706 | arg0 is of shape (1, 1) |
17707 | arg1 is of shape (4, 1) |
17708 | sel is of shape (2, 3) = {1, len, 2, len+1, 3, len+2, ...} |
17709 | |
17710 | In this case the pattern: {len, len+1, len+2, ...} chooses arg1. |
17711 | However, |
17712 | step = (len+2) - (len+1) = 1 |
17713 | arg_npatterns = VECTOR_CST_NPATTERNS (arg1) = 4 |
17714 | Since step is not a multiple of arg_npatterns, |
17715 | valid_mask_for_fold_vec_perm_cst should return false, |
17716 | and thus fold_vec_perm_cst should return NULL_TREE. */ |
17717 | { |
17718 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 1); |
17719 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 4, nelts_per_pattern: 1); |
17720 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17721 | |
17722 | vec_perm_builder builder (len, 2, 3); |
17723 | poly_uint64 mask_elems[] = { 0, len, 1, len + 1, 2, len + 2 }; |
17724 | builder_push_elems (builder, elems&: mask_elems); |
17725 | |
17726 | vec_perm_indices sel (builder, 2, len); |
17727 | const char *reason; |
17728 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, reason: &reason); |
17729 | |
17730 | ASSERT_TRUE (res == NULL_TREE); |
17731 | ASSERT_TRUE (!strcmp (reason, "step is not multiple of npatterns" )); |
17732 | } |
17733 | } |
17734 | } |
17735 | |
17736 | /* Test all vectors which contain at-least 8 elements. */ |
17737 | |
17738 | static void |
17739 | test_nunits_min_8 (machine_mode vmode) |
17740 | { |
17741 | for (int i = 0; i < 10; i++) |
17742 | { |
17743 | /* Case 1: sel_npatterns (4) > input npatterns (2) |
17744 | sel: { 0, 0, 1, len, 2, 0, 3, len, 4, 0, 5, len, ...} // (4, 3) |
17745 | res: { arg0[0], arg0[0], arg0[0], arg1[0], |
17746 | arg0[2], arg0[0], arg0[3], arg1[0], |
17747 | arg0[4], arg0[0], arg0[5], arg1[0], ... } // (4, 3) */ |
17748 | { |
17749 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 2, nelts_per_pattern: 3, step: 2); |
17750 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 2, nelts_per_pattern: 3, step: 2); |
17751 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17752 | |
17753 | vec_perm_builder builder(len, 4, 3); |
17754 | poly_uint64 mask_elems[] = { 0, 0, 1, len, 2, 0, 3, len, |
17755 | 4, 0, 5, len }; |
17756 | builder_push_elems (builder, elems&: mask_elems); |
17757 | |
17758 | vec_perm_indices sel (builder, 2, len); |
17759 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel); |
17760 | |
17761 | tree expected_res[] = { ARG0(0), ARG0(0), ARG0(1), ARG1(0), |
17762 | ARG0(2), ARG0(0), ARG0(3), ARG1(0), |
17763 | ARG0(4), ARG0(0), ARG0(5), ARG1(0) }; |
17764 | validate_res (npatterns: 4, nelts_per_pattern: 3, res, expected_res); |
17765 | } |
17766 | } |
17767 | } |
17768 | |
17769 | /* Test vectors for which nunits[0] <= 4. */ |
17770 | |
17771 | static void |
17772 | test_nunits_max_4 (machine_mode vmode) |
17773 | { |
17774 | /* Case 1: mask = {0, 4, ...} // (1, 2) |
17775 | This should return NULL_TREE because the index 4 may choose |
17776 | from either arg0 or arg1 depending on vector length. */ |
17777 | { |
17778 | tree arg0 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17779 | tree arg1 = build_vec_cst_rand (vmode, npatterns: 1, nelts_per_pattern: 3, step: 1); |
17780 | poly_uint64 len = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)); |
17781 | |
17782 | vec_perm_builder builder (len, 1, 2); |
17783 | poly_uint64 mask_elems[] = {0, 4}; |
17784 | builder_push_elems (builder, elems&: mask_elems); |
17785 | |
17786 | vec_perm_indices sel (builder, 2, len); |
17787 | const char *reason; |
17788 | tree res = fold_vec_perm_cst (TREE_TYPE (arg0), arg0, arg1, sel, reason: &reason); |
17789 | ASSERT_TRUE (res == NULL_TREE); |
17790 | ASSERT_TRUE (reason != NULL); |
17791 | ASSERT_TRUE (!strcmp (reason, "cannot divide selector element by arg len" )); |
17792 | } |
17793 | } |
17794 | |
17795 | #undef ARG0 |
17796 | #undef ARG1 |
17797 | |
17798 | /* Return true if SIZE is of the form C + Cx and C is power of 2. */ |
17799 | |
17800 | static bool |
17801 | is_simple_vla_size (poly_uint64 size) |
17802 | { |
17803 | if (size.is_constant () |
17804 | || !pow2p_hwi (x: size.coeffs[0])) |
17805 | return false; |
17806 | for (unsigned i = 1; i < ARRAY_SIZE (size.coeffs); ++i) |
17807 | if (size.coeffs[i] != (i <= 1 ? size.coeffs[0] : 0)) |
17808 | return false; |
17809 | return true; |
17810 | } |
17811 | |
17812 | /* Execute fold_vec_perm_cst unit tests. */ |
17813 | |
17814 | static void |
17815 | test () |
17816 | { |
17817 | machine_mode vnx4si_mode = E_VOIDmode; |
17818 | machine_mode v4si_mode = E_VOIDmode; |
17819 | |
17820 | machine_mode vmode; |
17821 | FOR_EACH_MODE_IN_CLASS (vmode, MODE_VECTOR_INT) |
17822 | { |
17823 | /* Obtain modes corresponding to VNx4SI and V4SI, |
17824 | to call mixed mode tests below. |
17825 | FIXME: Is there a better way to do this ? */ |
17826 | if (GET_MODE_INNER (vmode) == SImode) |
17827 | { |
17828 | poly_uint64 nunits = GET_MODE_NUNITS (mode: vmode); |
17829 | if (is_simple_vla_size (size: nunits) |
17830 | && nunits.coeffs[0] == 4) |
17831 | vnx4si_mode = vmode; |
17832 | else if (known_eq (nunits, poly_uint64 (4))) |
17833 | v4si_mode = vmode; |
17834 | } |
17835 | |
17836 | if (!is_simple_vla_size (size: GET_MODE_NUNITS (mode: vmode)) |
17837 | || !targetm.vector_mode_supported_p (vmode)) |
17838 | continue; |
17839 | |
17840 | poly_uint64 nunits = GET_MODE_NUNITS (mode: vmode); |
17841 | test_all_nunits (vmode); |
17842 | if (nunits.coeffs[0] >= 2) |
17843 | test_nunits_min_2 (vmode); |
17844 | if (nunits.coeffs[0] >= 4) |
17845 | test_nunits_min_4 (vmode); |
17846 | if (nunits.coeffs[0] >= 8) |
17847 | test_nunits_min_8 (vmode); |
17848 | |
17849 | if (nunits.coeffs[0] <= 4) |
17850 | test_nunits_max_4 (vmode); |
17851 | } |
17852 | |
17853 | if (vnx4si_mode != E_VOIDmode && v4si_mode != E_VOIDmode |
17854 | && targetm.vector_mode_supported_p (vnx4si_mode) |
17855 | && targetm.vector_mode_supported_p (v4si_mode)) |
17856 | { |
17857 | test_vnx4si_v4si (vnx4si_mode, v4si_mode); |
17858 | test_v4si_vnx4si (v4si_mode, vnx4si_mode); |
17859 | } |
17860 | } |
17861 | } // end of test_fold_vec_perm_cst namespace |
17862 | |
17863 | /* Verify that various binary operations on vectors are folded |
17864 | correctly. */ |
17865 | |
17866 | static void |
17867 | test_vector_folding () |
17868 | { |
17869 | tree inner_type = integer_type_node; |
17870 | tree type = build_vector_type (inner_type, 4); |
17871 | tree zero = build_zero_cst (type); |
17872 | tree one = build_one_cst (type); |
17873 | tree index = build_index_vector (type, 0, 1); |
17874 | |
17875 | /* Verify equality tests that return a scalar boolean result. */ |
17876 | tree res_type = boolean_type_node; |
17877 | ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one))); |
17878 | ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero))); |
17879 | ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one))); |
17880 | ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one))); |
17881 | ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one))); |
17882 | ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, |
17883 | index, one))); |
17884 | ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, |
17885 | index, index))); |
17886 | ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, |
17887 | index, index))); |
17888 | } |
17889 | |
17890 | /* Verify folding of VEC_DUPLICATE_EXPRs. */ |
17891 | |
17892 | static void |
17893 | test_vec_duplicate_folding () |
17894 | { |
17895 | scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype); |
17896 | machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode); |
17897 | /* This will be 1 if VEC_MODE isn't a vector mode. */ |
17898 | poly_uint64 nunits = GET_MODE_NUNITS (mode: vec_mode); |
17899 | |
17900 | tree type = build_vector_type (ssizetype, nunits); |
17901 | tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5)); |
17902 | tree dup5_cst = build_vector_from_val (type, ssize_int (5)); |
17903 | ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0)); |
17904 | } |
17905 | |
17906 | /* Run all of the selftests within this file. */ |
17907 | |
17908 | void |
17909 | fold_const_cc_tests () |
17910 | { |
17911 | test_arithmetic_folding (); |
17912 | test_vector_folding (); |
17913 | test_vec_duplicate_folding (); |
17914 | test_fold_vec_perm_cst::test (); |
17915 | } |
17916 | |
17917 | } // namespace selftest |
17918 | |
17919 | #endif /* CHECKING_P */ |
17920 | |