]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fold-const.c
poly_int: GET_MODE_BITSIZE
[thirdparty/gcc.git] / gcc / fold-const.c
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2018 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 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
86
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
90
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
111 };
112
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int twoval_comparison_p (tree, tree *, tree *, int *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (location_t,
133 enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static tree fold_negate_expr (location_t, tree);
142
143
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
146
147 static location_t
148 expr_location_or (tree t, location_t loc)
149 {
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
152 }
153
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
156
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
159 {
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
165 {
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
168 }
169 return x;
170 }
171 \f
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
175
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
178 {
179 widest_int quo;
180
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
184
185 return NULL_TREE;
186 }
187 \f
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
196
197 static int fold_deferring_overflow_warnings;
198
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
203
204 static const char* fold_deferred_overflow_warning;
205
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
208
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
210
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
213
214 void
215 fold_defer_overflow_warnings (void)
216 {
217 ++fold_deferring_overflow_warnings;
218 }
219
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
228
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
231 {
232 const char *warnmsg;
233 location_t locus;
234
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
238 {
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
244 }
245
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
248
249 if (!issue || warnmsg == NULL)
250 return;
251
252 if (gimple_no_warning_p (stmt))
253 return;
254
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
259
260 if (!issue_strict_overflow_warning (code))
261 return;
262
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
268 }
269
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
272
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
275 {
276 fold_undefer_overflow_warnings (false, NULL, 0);
277 }
278
279 /* Whether we are deferring overflow warnings. */
280
281 bool
282 fold_deferring_overflow_warnings_p (void)
283 {
284 return fold_deferring_overflow_warnings > 0;
285 }
286
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
289
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
292 {
293 if (fold_deferring_overflow_warnings > 0)
294 {
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
297 {
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
300 }
301 }
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
304 }
305 \f
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
308
309 bool
310 negate_mathfn_p (combined_fn fn)
311 {
312 switch (fn)
313 {
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
338
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
344
345 default:
346 break;
347 }
348 return false;
349 }
350
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
353
354 bool
355 may_negate_without_overflow_p (const_tree t)
356 {
357 tree type;
358
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
360
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
364
365 return !wi::only_sign_bit_p (wi::to_wide (t));
366 }
367
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
370
371 static bool
372 negate_expr_p (tree t)
373 {
374 tree type;
375
376 if (t == 0)
377 return false;
378
379 type = TREE_TYPE (t);
380
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
383 {
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
386 return true;
387
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
393
394 case FIXED_CST:
395 return true;
396
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
399
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
404
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
408
409 case VECTOR_CST:
410 {
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
413
414 /* Steps don't prevent negation. */
415 unsigned int count = vector_cst_encoded_nelts (t);
416 for (unsigned int i = 0; i < count; ++i)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
418 return false;
419
420 return true;
421 }
422
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
426
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
429
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (ANY_INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
441
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! ANY_INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
448
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && (wi::popcount
458 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && (wi::popcount
461 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
462 break;
463
464 /* Fall through. */
465
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
471
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 if (TYPE_UNSIGNED (type))
476 break;
477 if (negate_expr_p (TREE_OPERAND (t, 0)))
478 return true;
479 /* In general we can't negate B in A / B, because if A is INT_MIN and
480 B is 1, we may turn this into INT_MIN / -1 which is undefined
481 and actually traps on some architectures. */
482 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
483 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
484 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
485 && ! integer_onep (TREE_OPERAND (t, 1))))
486 return negate_expr_p (TREE_OPERAND (t, 1));
487 break;
488
489 case NOP_EXPR:
490 /* Negate -((double)float) as (double)(-float). */
491 if (TREE_CODE (type) == REAL_TYPE)
492 {
493 tree tem = strip_float_extensions (t);
494 if (tem != t)
495 return negate_expr_p (tem);
496 }
497 break;
498
499 case CALL_EXPR:
500 /* Negate -f(x) as f(-x). */
501 if (negate_mathfn_p (get_call_combined_fn (t)))
502 return negate_expr_p (CALL_EXPR_ARG (t, 0));
503 break;
504
505 case RSHIFT_EXPR:
506 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
507 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
508 {
509 tree op1 = TREE_OPERAND (t, 1);
510 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
511 return true;
512 }
513 break;
514
515 default:
516 break;
517 }
518 return false;
519 }
520
521 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
522 simplification is possible.
523 If negate_expr_p would return true for T, NULL_TREE will never be
524 returned. */
525
526 static tree
527 fold_negate_expr_1 (location_t loc, tree t)
528 {
529 tree type = TREE_TYPE (t);
530 tree tem;
531
532 switch (TREE_CODE (t))
533 {
534 /* Convert - (~A) to A + 1. */
535 case BIT_NOT_EXPR:
536 if (INTEGRAL_TYPE_P (type))
537 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
538 build_one_cst (type));
539 break;
540
541 case INTEGER_CST:
542 tem = fold_negate_const (t, type);
543 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
544 || (ANY_INTEGRAL_TYPE_P (type)
545 && !TYPE_OVERFLOW_TRAPS (type)
546 && TYPE_OVERFLOW_WRAPS (type))
547 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
548 return tem;
549 break;
550
551 case POLY_INT_CST:
552 case REAL_CST:
553 case FIXED_CST:
554 tem = fold_negate_const (t, type);
555 return tem;
556
557 case COMPLEX_CST:
558 {
559 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
560 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
561 if (rpart && ipart)
562 return build_complex (type, rpart, ipart);
563 }
564 break;
565
566 case VECTOR_CST:
567 {
568 tree_vector_builder elts;
569 elts.new_unary_operation (type, t, true);
570 unsigned int count = elts.encoded_nelts ();
571 for (unsigned int i = 0; i < count; ++i)
572 {
573 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
574 if (elt == NULL_TREE)
575 return NULL_TREE;
576 elts.quick_push (elt);
577 }
578
579 return elts.build ();
580 }
581
582 case COMPLEX_EXPR:
583 if (negate_expr_p (t))
584 return fold_build2_loc (loc, COMPLEX_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
586 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
587 break;
588
589 case CONJ_EXPR:
590 if (negate_expr_p (t))
591 return fold_build1_loc (loc, CONJ_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
593 break;
594
595 case NEGATE_EXPR:
596 if (!TYPE_OVERFLOW_SANITIZED (type))
597 return TREE_OPERAND (t, 0);
598 break;
599
600 case PLUS_EXPR:
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
602 && !HONOR_SIGNED_ZEROS (element_mode (type)))
603 {
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t, 1)))
606 {
607 tem = negate_expr (TREE_OPERAND (t, 1));
608 return fold_build2_loc (loc, MINUS_EXPR, type,
609 tem, TREE_OPERAND (t, 0));
610 }
611
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t, 0)))
614 {
615 tem = negate_expr (TREE_OPERAND (t, 0));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 1));
618 }
619 }
620 break;
621
622 case MINUS_EXPR:
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
625 && !HONOR_SIGNED_ZEROS (element_mode (type)))
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
628 break;
629
630 case MULT_EXPR:
631 if (TYPE_UNSIGNED (type))
632 break;
633
634 /* Fall through. */
635
636 case RDIV_EXPR:
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
638 {
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
641 return fold_build2_loc (loc, TREE_CODE (t), type,
642 TREE_OPERAND (t, 0), negate_expr (tem));
643 tem = TREE_OPERAND (t, 0);
644 if (negate_expr_p (tem))
645 return fold_build2_loc (loc, TREE_CODE (t), type,
646 negate_expr (tem), TREE_OPERAND (t, 1));
647 }
648 break;
649
650 case TRUNC_DIV_EXPR:
651 case ROUND_DIV_EXPR:
652 case EXACT_DIV_EXPR:
653 if (TYPE_UNSIGNED (type))
654 break;
655 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (TREE_OPERAND (t, 0)),
658 TREE_OPERAND (t, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
664 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t, 1))))
666 && negate_expr_p (TREE_OPERAND (t, 1)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 TREE_OPERAND (t, 0),
669 negate_expr (TREE_OPERAND (t, 1)));
670 break;
671
672 case NOP_EXPR:
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type) == REAL_TYPE)
675 {
676 tem = strip_float_extensions (t);
677 if (tem != t && negate_expr_p (tem))
678 return fold_convert_loc (loc, type, negate_expr (tem));
679 }
680 break;
681
682 case CALL_EXPR:
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t))
685 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
686 {
687 tree fndecl, arg;
688
689 fndecl = get_callee_fndecl (t);
690 arg = negate_expr (CALL_EXPR_ARG (t, 0));
691 return build_call_expr_loc (loc, fndecl, 1, arg);
692 }
693 break;
694
695 case RSHIFT_EXPR:
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
698 {
699 tree op1 = TREE_OPERAND (t, 1);
700 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
701 {
702 tree ntype = TYPE_UNSIGNED (type)
703 ? signed_type_for (type)
704 : unsigned_type_for (type);
705 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
706 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
707 return fold_convert_loc (loc, type, temp);
708 }
709 }
710 break;
711
712 default:
713 break;
714 }
715
716 return NULL_TREE;
717 }
718
719 /* A wrapper for fold_negate_expr_1. */
720
721 static tree
722 fold_negate_expr (location_t loc, tree t)
723 {
724 tree type = TREE_TYPE (t);
725 STRIP_SIGN_NOPS (t);
726 tree tem = fold_negate_expr_1 (loc, t);
727 if (tem == NULL_TREE)
728 return NULL_TREE;
729 return fold_convert_loc (loc, type, tem);
730 }
731
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
734 return NULL_TREE. */
735
736 static tree
737 negate_expr (tree t)
738 {
739 tree type, tem;
740 location_t loc;
741
742 if (t == NULL_TREE)
743 return NULL_TREE;
744
745 loc = EXPR_LOCATION (t);
746 type = TREE_TYPE (t);
747 STRIP_SIGN_NOPS (t);
748
749 tem = fold_negate_expr (loc, t);
750 if (!tem)
751 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
752 return fold_convert_loc (loc, type, tem);
753 }
754 \f
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
762
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
766
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
772
773 If IN is itself a literal or constant, return it as appropriate.
774
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
777
778 static tree
779 split_tree (tree in, tree type, enum tree_code code,
780 tree *minus_varp, tree *conp, tree *minus_conp,
781 tree *litp, tree *minus_litp, int negate_p)
782 {
783 tree var = 0;
784 *minus_varp = 0;
785 *conp = 0;
786 *minus_conp = 0;
787 *litp = 0;
788 *minus_litp = 0;
789
790 /* Strip any conversions that don't change the machine mode or signedness. */
791 STRIP_SIGN_NOPS (in);
792
793 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
794 || TREE_CODE (in) == FIXED_CST)
795 *litp = in;
796 else if (TREE_CODE (in) == code
797 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
798 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
799 /* We can associate addition and subtraction together (even
800 though the C standard doesn't say so) for integers because
801 the value is not affected. For reals, the value might be
802 affected, so we can't. */
803 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
804 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR
806 && (TREE_CODE (in) == PLUS_EXPR
807 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
808 {
809 tree op0 = TREE_OPERAND (in, 0);
810 tree op1 = TREE_OPERAND (in, 1);
811 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
812 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
813
814 /* First see if either of the operands is a literal, then a constant. */
815 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
816 || TREE_CODE (op0) == FIXED_CST)
817 *litp = op0, op0 = 0;
818 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
819 || TREE_CODE (op1) == FIXED_CST)
820 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
821
822 if (op0 != 0 && TREE_CONSTANT (op0))
823 *conp = op0, op0 = 0;
824 else if (op1 != 0 && TREE_CONSTANT (op1))
825 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
826
827 /* If we haven't dealt with either operand, this is not a case we can
828 decompose. Otherwise, VAR is either of the ones remaining, if any. */
829 if (op0 != 0 && op1 != 0)
830 var = in;
831 else if (op0 != 0)
832 var = op0;
833 else
834 var = op1, neg_var_p = neg1_p;
835
836 /* Now do any needed negations. */
837 if (neg_litp_p)
838 *minus_litp = *litp, *litp = 0;
839 if (neg_conp_p && *conp)
840 *minus_conp = *conp, *conp = 0;
841 if (neg_var_p && var)
842 *minus_varp = var, var = 0;
843 }
844 else if (TREE_CONSTANT (in))
845 *conp = in;
846 else if (TREE_CODE (in) == BIT_NOT_EXPR
847 && code == PLUS_EXPR)
848 {
849 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
850 when IN is constant. */
851 *litp = build_minus_one_cst (type);
852 *minus_varp = TREE_OPERAND (in, 0);
853 }
854 else
855 var = in;
856
857 if (negate_p)
858 {
859 if (*litp)
860 *minus_litp = *litp, *litp = 0;
861 else if (*minus_litp)
862 *litp = *minus_litp, *minus_litp = 0;
863 if (*conp)
864 *minus_conp = *conp, *conp = 0;
865 else if (*minus_conp)
866 *conp = *minus_conp, *minus_conp = 0;
867 if (var)
868 *minus_varp = var, var = 0;
869 else if (*minus_varp)
870 var = *minus_varp, *minus_varp = 0;
871 }
872
873 if (*litp
874 && TREE_OVERFLOW_P (*litp))
875 *litp = drop_tree_overflow (*litp);
876 if (*minus_litp
877 && TREE_OVERFLOW_P (*minus_litp))
878 *minus_litp = drop_tree_overflow (*minus_litp);
879
880 return var;
881 }
882
883 /* Re-associate trees split by the above function. T1 and T2 are
884 either expressions to associate or null. Return the new
885 expression, if any. LOC is the location of the new expression. If
886 we build an operation, do it in TYPE and with CODE. */
887
888 static tree
889 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
890 {
891 if (t1 == 0)
892 {
893 gcc_assert (t2 == 0 || code != MINUS_EXPR);
894 return t2;
895 }
896 else if (t2 == 0)
897 return t1;
898
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
903 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
904 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
905 {
906 if (code == PLUS_EXPR)
907 {
908 if (TREE_CODE (t1) == NEGATE_EXPR)
909 return build2_loc (loc, MINUS_EXPR, type,
910 fold_convert_loc (loc, type, t2),
911 fold_convert_loc (loc, type,
912 TREE_OPERAND (t1, 0)));
913 else if (TREE_CODE (t2) == NEGATE_EXPR)
914 return build2_loc (loc, MINUS_EXPR, type,
915 fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type,
917 TREE_OPERAND (t2, 0)));
918 else if (integer_zerop (t2))
919 return fold_convert_loc (loc, type, t1);
920 }
921 else if (code == MINUS_EXPR)
922 {
923 if (integer_zerop (t2))
924 return fold_convert_loc (loc, type, t1);
925 }
926
927 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
928 fold_convert_loc (loc, type, t2));
929 }
930
931 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 fold_convert_loc (loc, type, t2));
933 }
934 \f
935 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
936 for use in int_const_binop, size_binop and size_diffop. */
937
938 static bool
939 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
940 {
941 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
942 return false;
943 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
944 return false;
945
946 switch (code)
947 {
948 case LSHIFT_EXPR:
949 case RSHIFT_EXPR:
950 case LROTATE_EXPR:
951 case RROTATE_EXPR:
952 return true;
953
954 default:
955 break;
956 }
957
958 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
959 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
960 && TYPE_MODE (type1) == TYPE_MODE (type2);
961 }
962
963 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
964
965 static tree
966 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
967 int overflowable)
968 {
969 wide_int res;
970 tree t;
971 tree type = TREE_TYPE (parg1);
972 signop sign = TYPE_SIGN (type);
973 bool overflow = false;
974
975 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
976 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
977
978 switch (code)
979 {
980 case BIT_IOR_EXPR:
981 res = wi::bit_or (arg1, arg2);
982 break;
983
984 case BIT_XOR_EXPR:
985 res = wi::bit_xor (arg1, arg2);
986 break;
987
988 case BIT_AND_EXPR:
989 res = wi::bit_and (arg1, arg2);
990 break;
991
992 case RSHIFT_EXPR:
993 case LSHIFT_EXPR:
994 if (wi::neg_p (arg2))
995 {
996 arg2 = -arg2;
997 if (code == RSHIFT_EXPR)
998 code = LSHIFT_EXPR;
999 else
1000 code = RSHIFT_EXPR;
1001 }
1002
1003 if (code == RSHIFT_EXPR)
1004 /* It's unclear from the C standard whether shifts can overflow.
1005 The following code ignores overflow; perhaps a C standard
1006 interpretation ruling is needed. */
1007 res = wi::rshift (arg1, arg2, sign);
1008 else
1009 res = wi::lshift (arg1, arg2);
1010 break;
1011
1012 case RROTATE_EXPR:
1013 case LROTATE_EXPR:
1014 if (wi::neg_p (arg2))
1015 {
1016 arg2 = -arg2;
1017 if (code == RROTATE_EXPR)
1018 code = LROTATE_EXPR;
1019 else
1020 code = RROTATE_EXPR;
1021 }
1022
1023 if (code == RROTATE_EXPR)
1024 res = wi::rrotate (arg1, arg2);
1025 else
1026 res = wi::lrotate (arg1, arg2);
1027 break;
1028
1029 case PLUS_EXPR:
1030 res = wi::add (arg1, arg2, sign, &overflow);
1031 break;
1032
1033 case MINUS_EXPR:
1034 res = wi::sub (arg1, arg2, sign, &overflow);
1035 break;
1036
1037 case MULT_EXPR:
1038 res = wi::mul (arg1, arg2, sign, &overflow);
1039 break;
1040
1041 case MULT_HIGHPART_EXPR:
1042 res = wi::mul_high (arg1, arg2, sign);
1043 break;
1044
1045 case TRUNC_DIV_EXPR:
1046 case EXACT_DIV_EXPR:
1047 if (arg2 == 0)
1048 return NULL_TREE;
1049 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1050 break;
1051
1052 case FLOOR_DIV_EXPR:
1053 if (arg2 == 0)
1054 return NULL_TREE;
1055 res = wi::div_floor (arg1, arg2, sign, &overflow);
1056 break;
1057
1058 case CEIL_DIV_EXPR:
1059 if (arg2 == 0)
1060 return NULL_TREE;
1061 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1062 break;
1063
1064 case ROUND_DIV_EXPR:
1065 if (arg2 == 0)
1066 return NULL_TREE;
1067 res = wi::div_round (arg1, arg2, sign, &overflow);
1068 break;
1069
1070 case TRUNC_MOD_EXPR:
1071 if (arg2 == 0)
1072 return NULL_TREE;
1073 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1074 break;
1075
1076 case FLOOR_MOD_EXPR:
1077 if (arg2 == 0)
1078 return NULL_TREE;
1079 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1080 break;
1081
1082 case CEIL_MOD_EXPR:
1083 if (arg2 == 0)
1084 return NULL_TREE;
1085 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1086 break;
1087
1088 case ROUND_MOD_EXPR:
1089 if (arg2 == 0)
1090 return NULL_TREE;
1091 res = wi::mod_round (arg1, arg2, sign, &overflow);
1092 break;
1093
1094 case MIN_EXPR:
1095 res = wi::min (arg1, arg2, sign);
1096 break;
1097
1098 case MAX_EXPR:
1099 res = wi::max (arg1, arg2, sign);
1100 break;
1101
1102 default:
1103 return NULL_TREE;
1104 }
1105
1106 t = force_fit_type (type, res, overflowable,
1107 (((sign == SIGNED || overflowable == -1)
1108 && overflow)
1109 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1110
1111 return t;
1112 }
1113
1114 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1115 to produce a new constant. Return NULL_TREE if we don't know how
1116 to evaluate CODE at compile-time. */
1117
1118 static tree
1119 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1120 int overflowable)
1121 {
1122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1123 return int_const_binop_2 (code, arg1, arg2, overflowable);
1124
1125 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1126
1127 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1128 {
1129 poly_wide_int res;
1130 bool overflow;
1131 tree type = TREE_TYPE (arg1);
1132 signop sign = TYPE_SIGN (type);
1133 switch (code)
1134 {
1135 case PLUS_EXPR:
1136 res = wi::add (wi::to_poly_wide (arg1),
1137 wi::to_poly_wide (arg2), sign, &overflow);
1138 break;
1139
1140 case MINUS_EXPR:
1141 res = wi::sub (wi::to_poly_wide (arg1),
1142 wi::to_poly_wide (arg2), sign, &overflow);
1143 break;
1144
1145 case MULT_EXPR:
1146 if (TREE_CODE (arg2) == INTEGER_CST)
1147 res = wi::mul (wi::to_poly_wide (arg1),
1148 wi::to_wide (arg2), sign, &overflow);
1149 else if (TREE_CODE (arg1) == INTEGER_CST)
1150 res = wi::mul (wi::to_poly_wide (arg2),
1151 wi::to_wide (arg1), sign, &overflow);
1152 else
1153 return NULL_TREE;
1154 break;
1155
1156 case LSHIFT_EXPR:
1157 if (TREE_CODE (arg2) == INTEGER_CST)
1158 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1159 else
1160 return NULL_TREE;
1161 break;
1162
1163 case BIT_IOR_EXPR:
1164 if (TREE_CODE (arg2) != INTEGER_CST
1165 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1166 &res))
1167 return NULL_TREE;
1168 break;
1169
1170 default:
1171 return NULL_TREE;
1172 }
1173 return force_fit_type (type, res, overflowable,
1174 (((sign == SIGNED || overflowable == -1)
1175 && overflow)
1176 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1177 }
1178
1179 return NULL_TREE;
1180 }
1181
1182 tree
1183 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1184 {
1185 return int_const_binop_1 (code, arg1, arg2, 1);
1186 }
1187
1188 /* Return true if binary operation OP distributes over addition in operand
1189 OPNO, with the other operand being held constant. OPNO counts from 1. */
1190
1191 static bool
1192 distributes_over_addition_p (tree_code op, int opno)
1193 {
1194 switch (op)
1195 {
1196 case PLUS_EXPR:
1197 case MINUS_EXPR:
1198 case MULT_EXPR:
1199 return true;
1200
1201 case LSHIFT_EXPR:
1202 return opno == 1;
1203
1204 default:
1205 return false;
1206 }
1207 }
1208
1209 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1210 constant. We assume ARG1 and ARG2 have the same data type, or at least
1211 are the same kind of constant and the same machine mode. Return zero if
1212 combining the constants is not allowed in the current operating mode. */
1213
1214 static tree
1215 const_binop (enum tree_code code, tree arg1, tree arg2)
1216 {
1217 /* Sanity check for the recursive cases. */
1218 if (!arg1 || !arg2)
1219 return NULL_TREE;
1220
1221 STRIP_NOPS (arg1);
1222 STRIP_NOPS (arg2);
1223
1224 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1225 {
1226 if (code == POINTER_PLUS_EXPR)
1227 return int_const_binop (PLUS_EXPR,
1228 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1229
1230 return int_const_binop (code, arg1, arg2);
1231 }
1232
1233 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1234 {
1235 machine_mode mode;
1236 REAL_VALUE_TYPE d1;
1237 REAL_VALUE_TYPE d2;
1238 REAL_VALUE_TYPE value;
1239 REAL_VALUE_TYPE result;
1240 bool inexact;
1241 tree t, type;
1242
1243 /* The following codes are handled by real_arithmetic. */
1244 switch (code)
1245 {
1246 case PLUS_EXPR:
1247 case MINUS_EXPR:
1248 case MULT_EXPR:
1249 case RDIV_EXPR:
1250 case MIN_EXPR:
1251 case MAX_EXPR:
1252 break;
1253
1254 default:
1255 return NULL_TREE;
1256 }
1257
1258 d1 = TREE_REAL_CST (arg1);
1259 d2 = TREE_REAL_CST (arg2);
1260
1261 type = TREE_TYPE (arg1);
1262 mode = TYPE_MODE (type);
1263
1264 /* Don't perform operation if we honor signaling NaNs and
1265 either operand is a signaling NaN. */
1266 if (HONOR_SNANS (mode)
1267 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1268 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1269 return NULL_TREE;
1270
1271 /* Don't perform operation if it would raise a division
1272 by zero exception. */
1273 if (code == RDIV_EXPR
1274 && real_equal (&d2, &dconst0)
1275 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1276 return NULL_TREE;
1277
1278 /* If either operand is a NaN, just return it. Otherwise, set up
1279 for floating-point trap; we return an overflow. */
1280 if (REAL_VALUE_ISNAN (d1))
1281 {
1282 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1283 is off. */
1284 d1.signalling = 0;
1285 t = build_real (type, d1);
1286 return t;
1287 }
1288 else if (REAL_VALUE_ISNAN (d2))
1289 {
1290 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1291 is off. */
1292 d2.signalling = 0;
1293 t = build_real (type, d2);
1294 return t;
1295 }
1296
1297 inexact = real_arithmetic (&value, code, &d1, &d2);
1298 real_convert (&result, mode, &value);
1299
1300 /* Don't constant fold this floating point operation if
1301 the result has overflowed and flag_trapping_math. */
1302 if (flag_trapping_math
1303 && MODE_HAS_INFINITIES (mode)
1304 && REAL_VALUE_ISINF (result)
1305 && !REAL_VALUE_ISINF (d1)
1306 && !REAL_VALUE_ISINF (d2))
1307 return NULL_TREE;
1308
1309 /* Don't constant fold this floating point operation if the
1310 result may dependent upon the run-time rounding mode and
1311 flag_rounding_math is set, or if GCC's software emulation
1312 is unable to accurately represent the result. */
1313 if ((flag_rounding_math
1314 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1315 && (inexact || !real_identical (&result, &value)))
1316 return NULL_TREE;
1317
1318 t = build_real (type, result);
1319
1320 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1321 return t;
1322 }
1323
1324 if (TREE_CODE (arg1) == FIXED_CST)
1325 {
1326 FIXED_VALUE_TYPE f1;
1327 FIXED_VALUE_TYPE f2;
1328 FIXED_VALUE_TYPE result;
1329 tree t, type;
1330 int sat_p;
1331 bool overflow_p;
1332
1333 /* The following codes are handled by fixed_arithmetic. */
1334 switch (code)
1335 {
1336 case PLUS_EXPR:
1337 case MINUS_EXPR:
1338 case MULT_EXPR:
1339 case TRUNC_DIV_EXPR:
1340 if (TREE_CODE (arg2) != FIXED_CST)
1341 return NULL_TREE;
1342 f2 = TREE_FIXED_CST (arg2);
1343 break;
1344
1345 case LSHIFT_EXPR:
1346 case RSHIFT_EXPR:
1347 {
1348 if (TREE_CODE (arg2) != INTEGER_CST)
1349 return NULL_TREE;
1350 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1351 f2.data.high = w2.elt (1);
1352 f2.data.low = w2.ulow ();
1353 f2.mode = SImode;
1354 }
1355 break;
1356
1357 default:
1358 return NULL_TREE;
1359 }
1360
1361 f1 = TREE_FIXED_CST (arg1);
1362 type = TREE_TYPE (arg1);
1363 sat_p = TYPE_SATURATING (type);
1364 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1365 t = build_fixed (type, result);
1366 /* Propagate overflow flags. */
1367 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1368 TREE_OVERFLOW (t) = 1;
1369 return t;
1370 }
1371
1372 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1373 {
1374 tree type = TREE_TYPE (arg1);
1375 tree r1 = TREE_REALPART (arg1);
1376 tree i1 = TREE_IMAGPART (arg1);
1377 tree r2 = TREE_REALPART (arg2);
1378 tree i2 = TREE_IMAGPART (arg2);
1379 tree real, imag;
1380
1381 switch (code)
1382 {
1383 case PLUS_EXPR:
1384 case MINUS_EXPR:
1385 real = const_binop (code, r1, r2);
1386 imag = const_binop (code, i1, i2);
1387 break;
1388
1389 case MULT_EXPR:
1390 if (COMPLEX_FLOAT_TYPE_P (type))
1391 return do_mpc_arg2 (arg1, arg2, type,
1392 /* do_nonfinite= */ folding_initializer,
1393 mpc_mul);
1394
1395 real = const_binop (MINUS_EXPR,
1396 const_binop (MULT_EXPR, r1, r2),
1397 const_binop (MULT_EXPR, i1, i2));
1398 imag = const_binop (PLUS_EXPR,
1399 const_binop (MULT_EXPR, r1, i2),
1400 const_binop (MULT_EXPR, i1, r2));
1401 break;
1402
1403 case RDIV_EXPR:
1404 if (COMPLEX_FLOAT_TYPE_P (type))
1405 return do_mpc_arg2 (arg1, arg2, type,
1406 /* do_nonfinite= */ folding_initializer,
1407 mpc_div);
1408 /* Fallthru. */
1409 case TRUNC_DIV_EXPR:
1410 case CEIL_DIV_EXPR:
1411 case FLOOR_DIV_EXPR:
1412 case ROUND_DIV_EXPR:
1413 if (flag_complex_method == 0)
1414 {
1415 /* Keep this algorithm in sync with
1416 tree-complex.c:expand_complex_div_straight().
1417
1418 Expand complex division to scalars, straightforward algorithm.
1419 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1420 t = br*br + bi*bi
1421 */
1422 tree magsquared
1423 = const_binop (PLUS_EXPR,
1424 const_binop (MULT_EXPR, r2, r2),
1425 const_binop (MULT_EXPR, i2, i2));
1426 tree t1
1427 = const_binop (PLUS_EXPR,
1428 const_binop (MULT_EXPR, r1, r2),
1429 const_binop (MULT_EXPR, i1, i2));
1430 tree t2
1431 = const_binop (MINUS_EXPR,
1432 const_binop (MULT_EXPR, i1, r2),
1433 const_binop (MULT_EXPR, r1, i2));
1434
1435 real = const_binop (code, t1, magsquared);
1436 imag = const_binop (code, t2, magsquared);
1437 }
1438 else
1439 {
1440 /* Keep this algorithm in sync with
1441 tree-complex.c:expand_complex_div_wide().
1442
1443 Expand complex division to scalars, modified algorithm to minimize
1444 overflow with wide input ranges. */
1445 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1446 fold_abs_const (r2, TREE_TYPE (type)),
1447 fold_abs_const (i2, TREE_TYPE (type)));
1448
1449 if (integer_nonzerop (compare))
1450 {
1451 /* In the TRUE branch, we compute
1452 ratio = br/bi;
1453 div = (br * ratio) + bi;
1454 tr = (ar * ratio) + ai;
1455 ti = (ai * ratio) - ar;
1456 tr = tr / div;
1457 ti = ti / div; */
1458 tree ratio = const_binop (code, r2, i2);
1459 tree div = const_binop (PLUS_EXPR, i2,
1460 const_binop (MULT_EXPR, r2, ratio));
1461 real = const_binop (MULT_EXPR, r1, ratio);
1462 real = const_binop (PLUS_EXPR, real, i1);
1463 real = const_binop (code, real, div);
1464
1465 imag = const_binop (MULT_EXPR, i1, ratio);
1466 imag = const_binop (MINUS_EXPR, imag, r1);
1467 imag = const_binop (code, imag, div);
1468 }
1469 else
1470 {
1471 /* In the FALSE branch, we compute
1472 ratio = d/c;
1473 divisor = (d * ratio) + c;
1474 tr = (b * ratio) + a;
1475 ti = b - (a * ratio);
1476 tr = tr / div;
1477 ti = ti / div; */
1478 tree ratio = const_binop (code, i2, r2);
1479 tree div = const_binop (PLUS_EXPR, r2,
1480 const_binop (MULT_EXPR, i2, ratio));
1481
1482 real = const_binop (MULT_EXPR, i1, ratio);
1483 real = const_binop (PLUS_EXPR, real, r1);
1484 real = const_binop (code, real, div);
1485
1486 imag = const_binop (MULT_EXPR, r1, ratio);
1487 imag = const_binop (MINUS_EXPR, i1, imag);
1488 imag = const_binop (code, imag, div);
1489 }
1490 }
1491 break;
1492
1493 default:
1494 return NULL_TREE;
1495 }
1496
1497 if (real && imag)
1498 return build_complex (type, real, imag);
1499 }
1500
1501 if (TREE_CODE (arg1) == VECTOR_CST
1502 && TREE_CODE (arg2) == VECTOR_CST
1503 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1504 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1505 {
1506 tree type = TREE_TYPE (arg1);
1507 bool step_ok_p;
1508 if (VECTOR_CST_STEPPED_P (arg1)
1509 && VECTOR_CST_STEPPED_P (arg2))
1510 /* We can operate directly on the encoding if:
1511
1512 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1513 implies
1514 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1515
1516 Addition and subtraction are the supported operators
1517 for which this is true. */
1518 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1519 else if (VECTOR_CST_STEPPED_P (arg1))
1520 /* We can operate directly on stepped encodings if:
1521
1522 a3 - a2 == a2 - a1
1523 implies:
1524 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1525
1526 which is true if (x -> x op c) distributes over addition. */
1527 step_ok_p = distributes_over_addition_p (code, 1);
1528 else
1529 /* Similarly in reverse. */
1530 step_ok_p = distributes_over_addition_p (code, 2);
1531 tree_vector_builder elts;
1532 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1533 return NULL_TREE;
1534 unsigned int count = elts.encoded_nelts ();
1535 for (unsigned int i = 0; i < count; ++i)
1536 {
1537 tree elem1 = VECTOR_CST_ELT (arg1, i);
1538 tree elem2 = VECTOR_CST_ELT (arg2, i);
1539
1540 tree elt = const_binop (code, elem1, elem2);
1541
1542 /* It is possible that const_binop cannot handle the given
1543 code and return NULL_TREE */
1544 if (elt == NULL_TREE)
1545 return NULL_TREE;
1546 elts.quick_push (elt);
1547 }
1548
1549 return elts.build ();
1550 }
1551
1552 /* Shifts allow a scalar offset for a vector. */
1553 if (TREE_CODE (arg1) == VECTOR_CST
1554 && TREE_CODE (arg2) == INTEGER_CST)
1555 {
1556 tree type = TREE_TYPE (arg1);
1557 bool step_ok_p = distributes_over_addition_p (code, 1);
1558 tree_vector_builder elts;
1559 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1560 return NULL_TREE;
1561 unsigned int count = elts.encoded_nelts ();
1562 for (unsigned int i = 0; i < count; ++i)
1563 {
1564 tree elem1 = VECTOR_CST_ELT (arg1, i);
1565
1566 tree elt = const_binop (code, elem1, arg2);
1567
1568 /* It is possible that const_binop cannot handle the given
1569 code and return NULL_TREE. */
1570 if (elt == NULL_TREE)
1571 return NULL_TREE;
1572 elts.quick_push (elt);
1573 }
1574
1575 return elts.build ();
1576 }
1577 return NULL_TREE;
1578 }
1579
1580 /* Overload that adds a TYPE parameter to be able to dispatch
1581 to fold_relational_const. */
1582
1583 tree
1584 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1585 {
1586 if (TREE_CODE_CLASS (code) == tcc_comparison)
1587 return fold_relational_const (code, type, arg1, arg2);
1588
1589 /* ??? Until we make the const_binop worker take the type of the
1590 result as argument put those cases that need it here. */
1591 switch (code)
1592 {
1593 case VEC_SERIES_EXPR:
1594 if (CONSTANT_CLASS_P (arg1)
1595 && CONSTANT_CLASS_P (arg2))
1596 return build_vec_series (type, arg1, arg2);
1597 return NULL_TREE;
1598
1599 case COMPLEX_EXPR:
1600 if ((TREE_CODE (arg1) == REAL_CST
1601 && TREE_CODE (arg2) == REAL_CST)
1602 || (TREE_CODE (arg1) == INTEGER_CST
1603 && TREE_CODE (arg2) == INTEGER_CST))
1604 return build_complex (type, arg1, arg2);
1605 return NULL_TREE;
1606
1607 case POINTER_DIFF_EXPR:
1608 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1609 {
1610 offset_int res = wi::sub (wi::to_offset (arg1),
1611 wi::to_offset (arg2));
1612 return force_fit_type (type, res, 1,
1613 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1614 }
1615 return NULL_TREE;
1616
1617 case VEC_PACK_TRUNC_EXPR:
1618 case VEC_PACK_FIX_TRUNC_EXPR:
1619 {
1620 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1621
1622 if (TREE_CODE (arg1) != VECTOR_CST
1623 || TREE_CODE (arg2) != VECTOR_CST)
1624 return NULL_TREE;
1625
1626 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1627 return NULL_TREE;
1628
1629 out_nelts = in_nelts * 2;
1630 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1631 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1632
1633 tree_vector_builder elts (type, out_nelts, 1);
1634 for (i = 0; i < out_nelts; i++)
1635 {
1636 tree elt = (i < in_nelts
1637 ? VECTOR_CST_ELT (arg1, i)
1638 : VECTOR_CST_ELT (arg2, i - in_nelts));
1639 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1640 ? NOP_EXPR : FIX_TRUNC_EXPR,
1641 TREE_TYPE (type), elt);
1642 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1643 return NULL_TREE;
1644 elts.quick_push (elt);
1645 }
1646
1647 return elts.build ();
1648 }
1649
1650 case VEC_WIDEN_MULT_LO_EXPR:
1651 case VEC_WIDEN_MULT_HI_EXPR:
1652 case VEC_WIDEN_MULT_EVEN_EXPR:
1653 case VEC_WIDEN_MULT_ODD_EXPR:
1654 {
1655 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1656
1657 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1658 return NULL_TREE;
1659
1660 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1661 return NULL_TREE;
1662 out_nelts = in_nelts / 2;
1663 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1664 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1665
1666 if (code == VEC_WIDEN_MULT_LO_EXPR)
1667 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1668 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1669 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1670 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1671 scale = 1, ofs = 0;
1672 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1673 scale = 1, ofs = 1;
1674
1675 tree_vector_builder elts (type, out_nelts, 1);
1676 for (out = 0; out < out_nelts; out++)
1677 {
1678 unsigned int in = (out << scale) + ofs;
1679 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1680 VECTOR_CST_ELT (arg1, in));
1681 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1682 VECTOR_CST_ELT (arg2, in));
1683
1684 if (t1 == NULL_TREE || t2 == NULL_TREE)
1685 return NULL_TREE;
1686 tree elt = const_binop (MULT_EXPR, t1, t2);
1687 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1688 return NULL_TREE;
1689 elts.quick_push (elt);
1690 }
1691
1692 return elts.build ();
1693 }
1694
1695 default:;
1696 }
1697
1698 if (TREE_CODE_CLASS (code) != tcc_binary)
1699 return NULL_TREE;
1700
1701 /* Make sure type and arg0 have the same saturating flag. */
1702 gcc_checking_assert (TYPE_SATURATING (type)
1703 == TYPE_SATURATING (TREE_TYPE (arg1)));
1704
1705 return const_binop (code, arg1, arg2);
1706 }
1707
1708 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1709 Return zero if computing the constants is not possible. */
1710
1711 tree
1712 const_unop (enum tree_code code, tree type, tree arg0)
1713 {
1714 /* Don't perform the operation, other than NEGATE and ABS, if
1715 flag_signaling_nans is on and the operand is a signaling NaN. */
1716 if (TREE_CODE (arg0) == REAL_CST
1717 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1718 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1719 && code != NEGATE_EXPR
1720 && code != ABS_EXPR)
1721 return NULL_TREE;
1722
1723 switch (code)
1724 {
1725 CASE_CONVERT:
1726 case FLOAT_EXPR:
1727 case FIX_TRUNC_EXPR:
1728 case FIXED_CONVERT_EXPR:
1729 return fold_convert_const (code, type, arg0);
1730
1731 case ADDR_SPACE_CONVERT_EXPR:
1732 /* If the source address is 0, and the source address space
1733 cannot have a valid object at 0, fold to dest type null. */
1734 if (integer_zerop (arg0)
1735 && !(targetm.addr_space.zero_address_valid
1736 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1737 return fold_convert_const (code, type, arg0);
1738 break;
1739
1740 case VIEW_CONVERT_EXPR:
1741 return fold_view_convert_expr (type, arg0);
1742
1743 case NEGATE_EXPR:
1744 {
1745 /* Can't call fold_negate_const directly here as that doesn't
1746 handle all cases and we might not be able to negate some
1747 constants. */
1748 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1749 if (tem && CONSTANT_CLASS_P (tem))
1750 return tem;
1751 break;
1752 }
1753
1754 case ABS_EXPR:
1755 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1756 return fold_abs_const (arg0, type);
1757 break;
1758
1759 case CONJ_EXPR:
1760 if (TREE_CODE (arg0) == COMPLEX_CST)
1761 {
1762 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1763 TREE_TYPE (type));
1764 return build_complex (type, TREE_REALPART (arg0), ipart);
1765 }
1766 break;
1767
1768 case BIT_NOT_EXPR:
1769 if (TREE_CODE (arg0) == INTEGER_CST)
1770 return fold_not_const (arg0, type);
1771 else if (POLY_INT_CST_P (arg0))
1772 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1773 /* Perform BIT_NOT_EXPR on each element individually. */
1774 else if (TREE_CODE (arg0) == VECTOR_CST)
1775 {
1776 tree elem;
1777
1778 /* This can cope with stepped encodings because ~x == -1 - x. */
1779 tree_vector_builder elements;
1780 elements.new_unary_operation (type, arg0, true);
1781 unsigned int i, count = elements.encoded_nelts ();
1782 for (i = 0; i < count; ++i)
1783 {
1784 elem = VECTOR_CST_ELT (arg0, i);
1785 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1786 if (elem == NULL_TREE)
1787 break;
1788 elements.quick_push (elem);
1789 }
1790 if (i == count)
1791 return elements.build ();
1792 }
1793 break;
1794
1795 case TRUTH_NOT_EXPR:
1796 if (TREE_CODE (arg0) == INTEGER_CST)
1797 return constant_boolean_node (integer_zerop (arg0), type);
1798 break;
1799
1800 case REALPART_EXPR:
1801 if (TREE_CODE (arg0) == COMPLEX_CST)
1802 return fold_convert (type, TREE_REALPART (arg0));
1803 break;
1804
1805 case IMAGPART_EXPR:
1806 if (TREE_CODE (arg0) == COMPLEX_CST)
1807 return fold_convert (type, TREE_IMAGPART (arg0));
1808 break;
1809
1810 case VEC_UNPACK_LO_EXPR:
1811 case VEC_UNPACK_HI_EXPR:
1812 case VEC_UNPACK_FLOAT_LO_EXPR:
1813 case VEC_UNPACK_FLOAT_HI_EXPR:
1814 {
1815 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1816 enum tree_code subcode;
1817
1818 if (TREE_CODE (arg0) != VECTOR_CST)
1819 return NULL_TREE;
1820
1821 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1822 return NULL_TREE;
1823 out_nelts = in_nelts / 2;
1824 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1825
1826 unsigned int offset = 0;
1827 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1828 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1829 offset = out_nelts;
1830
1831 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1832 subcode = NOP_EXPR;
1833 else
1834 subcode = FLOAT_EXPR;
1835
1836 tree_vector_builder elts (type, out_nelts, 1);
1837 for (i = 0; i < out_nelts; i++)
1838 {
1839 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1840 VECTOR_CST_ELT (arg0, i + offset));
1841 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1842 return NULL_TREE;
1843 elts.quick_push (elt);
1844 }
1845
1846 return elts.build ();
1847 }
1848
1849 case VEC_DUPLICATE_EXPR:
1850 if (CONSTANT_CLASS_P (arg0))
1851 return build_vector_from_val (type, arg0);
1852 return NULL_TREE;
1853
1854 default:
1855 break;
1856 }
1857
1858 return NULL_TREE;
1859 }
1860
1861 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1862 indicates which particular sizetype to create. */
1863
1864 tree
1865 size_int_kind (poly_int64 number, enum size_type_kind kind)
1866 {
1867 return build_int_cst (sizetype_tab[(int) kind], number);
1868 }
1869 \f
1870 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1871 is a tree code. The type of the result is taken from the operands.
1872 Both must be equivalent integer types, ala int_binop_types_match_p.
1873 If the operands are constant, so is the result. */
1874
1875 tree
1876 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1877 {
1878 tree type = TREE_TYPE (arg0);
1879
1880 if (arg0 == error_mark_node || arg1 == error_mark_node)
1881 return error_mark_node;
1882
1883 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1884 TREE_TYPE (arg1)));
1885
1886 /* Handle the special case of two poly_int constants faster. */
1887 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1888 {
1889 /* And some specific cases even faster than that. */
1890 if (code == PLUS_EXPR)
1891 {
1892 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1893 return arg1;
1894 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1895 return arg0;
1896 }
1897 else if (code == MINUS_EXPR)
1898 {
1899 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1900 return arg0;
1901 }
1902 else if (code == MULT_EXPR)
1903 {
1904 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1905 return arg1;
1906 }
1907
1908 /* Handle general case of two integer constants. For sizetype
1909 constant calculations we always want to know about overflow,
1910 even in the unsigned case. */
1911 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1912 if (res != NULL_TREE)
1913 return res;
1914 }
1915
1916 return fold_build2_loc (loc, code, type, arg0, arg1);
1917 }
1918
1919 /* Given two values, either both of sizetype or both of bitsizetype,
1920 compute the difference between the two values. Return the value
1921 in signed type corresponding to the type of the operands. */
1922
1923 tree
1924 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1925 {
1926 tree type = TREE_TYPE (arg0);
1927 tree ctype;
1928
1929 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1930 TREE_TYPE (arg1)));
1931
1932 /* If the type is already signed, just do the simple thing. */
1933 if (!TYPE_UNSIGNED (type))
1934 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1935
1936 if (type == sizetype)
1937 ctype = ssizetype;
1938 else if (type == bitsizetype)
1939 ctype = sbitsizetype;
1940 else
1941 ctype = signed_type_for (type);
1942
1943 /* If either operand is not a constant, do the conversions to the signed
1944 type and subtract. The hardware will do the right thing with any
1945 overflow in the subtraction. */
1946 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1947 return size_binop_loc (loc, MINUS_EXPR,
1948 fold_convert_loc (loc, ctype, arg0),
1949 fold_convert_loc (loc, ctype, arg1));
1950
1951 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1952 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1953 overflow) and negate (which can't either). Special-case a result
1954 of zero while we're here. */
1955 if (tree_int_cst_equal (arg0, arg1))
1956 return build_int_cst (ctype, 0);
1957 else if (tree_int_cst_lt (arg1, arg0))
1958 return fold_convert_loc (loc, ctype,
1959 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1960 else
1961 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1962 fold_convert_loc (loc, ctype,
1963 size_binop_loc (loc,
1964 MINUS_EXPR,
1965 arg1, arg0)));
1966 }
1967 \f
1968 /* A subroutine of fold_convert_const handling conversions of an
1969 INTEGER_CST to another integer type. */
1970
1971 static tree
1972 fold_convert_const_int_from_int (tree type, const_tree arg1)
1973 {
1974 /* Given an integer constant, make new constant with new type,
1975 appropriately sign-extended or truncated. Use widest_int
1976 so that any extension is done according ARG1's type. */
1977 return force_fit_type (type, wi::to_widest (arg1),
1978 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1979 TREE_OVERFLOW (arg1));
1980 }
1981
1982 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1983 to an integer type. */
1984
1985 static tree
1986 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1987 {
1988 bool overflow = false;
1989 tree t;
1990
1991 /* The following code implements the floating point to integer
1992 conversion rules required by the Java Language Specification,
1993 that IEEE NaNs are mapped to zero and values that overflow
1994 the target precision saturate, i.e. values greater than
1995 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1996 are mapped to INT_MIN. These semantics are allowed by the
1997 C and C++ standards that simply state that the behavior of
1998 FP-to-integer conversion is unspecified upon overflow. */
1999
2000 wide_int val;
2001 REAL_VALUE_TYPE r;
2002 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2003
2004 switch (code)
2005 {
2006 case FIX_TRUNC_EXPR:
2007 real_trunc (&r, VOIDmode, &x);
2008 break;
2009
2010 default:
2011 gcc_unreachable ();
2012 }
2013
2014 /* If R is NaN, return zero and show we have an overflow. */
2015 if (REAL_VALUE_ISNAN (r))
2016 {
2017 overflow = true;
2018 val = wi::zero (TYPE_PRECISION (type));
2019 }
2020
2021 /* See if R is less than the lower bound or greater than the
2022 upper bound. */
2023
2024 if (! overflow)
2025 {
2026 tree lt = TYPE_MIN_VALUE (type);
2027 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2028 if (real_less (&r, &l))
2029 {
2030 overflow = true;
2031 val = wi::to_wide (lt);
2032 }
2033 }
2034
2035 if (! overflow)
2036 {
2037 tree ut = TYPE_MAX_VALUE (type);
2038 if (ut)
2039 {
2040 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2041 if (real_less (&u, &r))
2042 {
2043 overflow = true;
2044 val = wi::to_wide (ut);
2045 }
2046 }
2047 }
2048
2049 if (! overflow)
2050 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2051
2052 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2053 return t;
2054 }
2055
2056 /* A subroutine of fold_convert_const handling conversions of a
2057 FIXED_CST to an integer type. */
2058
2059 static tree
2060 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2061 {
2062 tree t;
2063 double_int temp, temp_trunc;
2064 scalar_mode mode;
2065
2066 /* Right shift FIXED_CST to temp by fbit. */
2067 temp = TREE_FIXED_CST (arg1).data;
2068 mode = TREE_FIXED_CST (arg1).mode;
2069 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2070 {
2071 temp = temp.rshift (GET_MODE_FBIT (mode),
2072 HOST_BITS_PER_DOUBLE_INT,
2073 SIGNED_FIXED_POINT_MODE_P (mode));
2074
2075 /* Left shift temp to temp_trunc by fbit. */
2076 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2077 HOST_BITS_PER_DOUBLE_INT,
2078 SIGNED_FIXED_POINT_MODE_P (mode));
2079 }
2080 else
2081 {
2082 temp = double_int_zero;
2083 temp_trunc = double_int_zero;
2084 }
2085
2086 /* If FIXED_CST is negative, we need to round the value toward 0.
2087 By checking if the fractional bits are not zero to add 1 to temp. */
2088 if (SIGNED_FIXED_POINT_MODE_P (mode)
2089 && temp_trunc.is_negative ()
2090 && TREE_FIXED_CST (arg1).data != temp_trunc)
2091 temp += double_int_one;
2092
2093 /* Given a fixed-point constant, make new constant with new type,
2094 appropriately sign-extended or truncated. */
2095 t = force_fit_type (type, temp, -1,
2096 (temp.is_negative ()
2097 && (TYPE_UNSIGNED (type)
2098 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2099 | TREE_OVERFLOW (arg1));
2100
2101 return t;
2102 }
2103
2104 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2105 to another floating point type. */
2106
2107 static tree
2108 fold_convert_const_real_from_real (tree type, const_tree arg1)
2109 {
2110 REAL_VALUE_TYPE value;
2111 tree t;
2112
2113 /* Don't perform the operation if flag_signaling_nans is on
2114 and the operand is a signaling NaN. */
2115 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2116 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2117 return NULL_TREE;
2118
2119 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2120 t = build_real (type, value);
2121
2122 /* If converting an infinity or NAN to a representation that doesn't
2123 have one, set the overflow bit so that we can produce some kind of
2124 error message at the appropriate point if necessary. It's not the
2125 most user-friendly message, but it's better than nothing. */
2126 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2127 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2128 TREE_OVERFLOW (t) = 1;
2129 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2130 && !MODE_HAS_NANS (TYPE_MODE (type)))
2131 TREE_OVERFLOW (t) = 1;
2132 /* Regular overflow, conversion produced an infinity in a mode that
2133 can't represent them. */
2134 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2135 && REAL_VALUE_ISINF (value)
2136 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2137 TREE_OVERFLOW (t) = 1;
2138 else
2139 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2140 return t;
2141 }
2142
2143 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2144 to a floating point type. */
2145
2146 static tree
2147 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2148 {
2149 REAL_VALUE_TYPE value;
2150 tree t;
2151
2152 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2153 &TREE_FIXED_CST (arg1));
2154 t = build_real (type, value);
2155
2156 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2157 return t;
2158 }
2159
2160 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2161 to another fixed-point type. */
2162
2163 static tree
2164 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2165 {
2166 FIXED_VALUE_TYPE value;
2167 tree t;
2168 bool overflow_p;
2169
2170 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2171 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2172 t = build_fixed (type, value);
2173
2174 /* Propagate overflow flags. */
2175 if (overflow_p | TREE_OVERFLOW (arg1))
2176 TREE_OVERFLOW (t) = 1;
2177 return t;
2178 }
2179
2180 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2181 to a fixed-point type. */
2182
2183 static tree
2184 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2185 {
2186 FIXED_VALUE_TYPE value;
2187 tree t;
2188 bool overflow_p;
2189 double_int di;
2190
2191 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2192
2193 di.low = TREE_INT_CST_ELT (arg1, 0);
2194 if (TREE_INT_CST_NUNITS (arg1) == 1)
2195 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2196 else
2197 di.high = TREE_INT_CST_ELT (arg1, 1);
2198
2199 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2200 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2201 TYPE_SATURATING (type));
2202 t = build_fixed (type, value);
2203
2204 /* Propagate overflow flags. */
2205 if (overflow_p | TREE_OVERFLOW (arg1))
2206 TREE_OVERFLOW (t) = 1;
2207 return t;
2208 }
2209
2210 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2211 to a fixed-point type. */
2212
2213 static tree
2214 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2215 {
2216 FIXED_VALUE_TYPE value;
2217 tree t;
2218 bool overflow_p;
2219
2220 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2221 &TREE_REAL_CST (arg1),
2222 TYPE_SATURATING (type));
2223 t = build_fixed (type, value);
2224
2225 /* Propagate overflow flags. */
2226 if (overflow_p | TREE_OVERFLOW (arg1))
2227 TREE_OVERFLOW (t) = 1;
2228 return t;
2229 }
2230
2231 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2232 type TYPE. If no simplification can be done return NULL_TREE. */
2233
2234 static tree
2235 fold_convert_const (enum tree_code code, tree type, tree arg1)
2236 {
2237 tree arg_type = TREE_TYPE (arg1);
2238 if (arg_type == type)
2239 return arg1;
2240
2241 /* We can't widen types, since the runtime value could overflow the
2242 original type before being extended to the new type. */
2243 if (POLY_INT_CST_P (arg1)
2244 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2245 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2246 return build_poly_int_cst (type,
2247 poly_wide_int::from (poly_int_cst_value (arg1),
2248 TYPE_PRECISION (type),
2249 TYPE_SIGN (arg_type)));
2250
2251 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2252 || TREE_CODE (type) == OFFSET_TYPE)
2253 {
2254 if (TREE_CODE (arg1) == INTEGER_CST)
2255 return fold_convert_const_int_from_int (type, arg1);
2256 else if (TREE_CODE (arg1) == REAL_CST)
2257 return fold_convert_const_int_from_real (code, type, arg1);
2258 else if (TREE_CODE (arg1) == FIXED_CST)
2259 return fold_convert_const_int_from_fixed (type, arg1);
2260 }
2261 else if (TREE_CODE (type) == REAL_TYPE)
2262 {
2263 if (TREE_CODE (arg1) == INTEGER_CST)
2264 return build_real_from_int_cst (type, arg1);
2265 else if (TREE_CODE (arg1) == REAL_CST)
2266 return fold_convert_const_real_from_real (type, arg1);
2267 else if (TREE_CODE (arg1) == FIXED_CST)
2268 return fold_convert_const_real_from_fixed (type, arg1);
2269 }
2270 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2271 {
2272 if (TREE_CODE (arg1) == FIXED_CST)
2273 return fold_convert_const_fixed_from_fixed (type, arg1);
2274 else if (TREE_CODE (arg1) == INTEGER_CST)
2275 return fold_convert_const_fixed_from_int (type, arg1);
2276 else if (TREE_CODE (arg1) == REAL_CST)
2277 return fold_convert_const_fixed_from_real (type, arg1);
2278 }
2279 else if (TREE_CODE (type) == VECTOR_TYPE)
2280 {
2281 if (TREE_CODE (arg1) == VECTOR_CST
2282 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2283 {
2284 tree elttype = TREE_TYPE (type);
2285 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2286 /* We can't handle steps directly when extending, since the
2287 values need to wrap at the original precision first. */
2288 bool step_ok_p
2289 = (INTEGRAL_TYPE_P (elttype)
2290 && INTEGRAL_TYPE_P (arg1_elttype)
2291 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2292 tree_vector_builder v;
2293 if (!v.new_unary_operation (type, arg1, step_ok_p))
2294 return NULL_TREE;
2295 unsigned int len = v.encoded_nelts ();
2296 for (unsigned int i = 0; i < len; ++i)
2297 {
2298 tree elt = VECTOR_CST_ELT (arg1, i);
2299 tree cvt = fold_convert_const (code, elttype, elt);
2300 if (cvt == NULL_TREE)
2301 return NULL_TREE;
2302 v.quick_push (cvt);
2303 }
2304 return v.build ();
2305 }
2306 }
2307 return NULL_TREE;
2308 }
2309
2310 /* Construct a vector of zero elements of vector type TYPE. */
2311
2312 static tree
2313 build_zero_vector (tree type)
2314 {
2315 tree t;
2316
2317 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2318 return build_vector_from_val (type, t);
2319 }
2320
2321 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2322
2323 bool
2324 fold_convertible_p (const_tree type, const_tree arg)
2325 {
2326 tree orig = TREE_TYPE (arg);
2327
2328 if (type == orig)
2329 return true;
2330
2331 if (TREE_CODE (arg) == ERROR_MARK
2332 || TREE_CODE (type) == ERROR_MARK
2333 || TREE_CODE (orig) == ERROR_MARK)
2334 return false;
2335
2336 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2337 return true;
2338
2339 switch (TREE_CODE (type))
2340 {
2341 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2342 case POINTER_TYPE: case REFERENCE_TYPE:
2343 case OFFSET_TYPE:
2344 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2345 || TREE_CODE (orig) == OFFSET_TYPE);
2346
2347 case REAL_TYPE:
2348 case FIXED_POINT_TYPE:
2349 case VECTOR_TYPE:
2350 case VOID_TYPE:
2351 return TREE_CODE (type) == TREE_CODE (orig);
2352
2353 default:
2354 return false;
2355 }
2356 }
2357
2358 /* Convert expression ARG to type TYPE. Used by the middle-end for
2359 simple conversions in preference to calling the front-end's convert. */
2360
2361 tree
2362 fold_convert_loc (location_t loc, tree type, tree arg)
2363 {
2364 tree orig = TREE_TYPE (arg);
2365 tree tem;
2366
2367 if (type == orig)
2368 return arg;
2369
2370 if (TREE_CODE (arg) == ERROR_MARK
2371 || TREE_CODE (type) == ERROR_MARK
2372 || TREE_CODE (orig) == ERROR_MARK)
2373 return error_mark_node;
2374
2375 switch (TREE_CODE (type))
2376 {
2377 case POINTER_TYPE:
2378 case REFERENCE_TYPE:
2379 /* Handle conversions between pointers to different address spaces. */
2380 if (POINTER_TYPE_P (orig)
2381 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2382 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2383 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2384 /* fall through */
2385
2386 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2387 case OFFSET_TYPE:
2388 if (TREE_CODE (arg) == INTEGER_CST)
2389 {
2390 tem = fold_convert_const (NOP_EXPR, type, arg);
2391 if (tem != NULL_TREE)
2392 return tem;
2393 }
2394 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2395 || TREE_CODE (orig) == OFFSET_TYPE)
2396 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2397 if (TREE_CODE (orig) == COMPLEX_TYPE)
2398 return fold_convert_loc (loc, type,
2399 fold_build1_loc (loc, REALPART_EXPR,
2400 TREE_TYPE (orig), arg));
2401 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2402 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2403 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2404
2405 case REAL_TYPE:
2406 if (TREE_CODE (arg) == INTEGER_CST)
2407 {
2408 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2409 if (tem != NULL_TREE)
2410 return tem;
2411 }
2412 else if (TREE_CODE (arg) == REAL_CST)
2413 {
2414 tem = fold_convert_const (NOP_EXPR, type, arg);
2415 if (tem != NULL_TREE)
2416 return tem;
2417 }
2418 else if (TREE_CODE (arg) == FIXED_CST)
2419 {
2420 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2421 if (tem != NULL_TREE)
2422 return tem;
2423 }
2424
2425 switch (TREE_CODE (orig))
2426 {
2427 case INTEGER_TYPE:
2428 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2429 case POINTER_TYPE: case REFERENCE_TYPE:
2430 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2431
2432 case REAL_TYPE:
2433 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2434
2435 case FIXED_POINT_TYPE:
2436 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2437
2438 case COMPLEX_TYPE:
2439 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2440 return fold_convert_loc (loc, type, tem);
2441
2442 default:
2443 gcc_unreachable ();
2444 }
2445
2446 case FIXED_POINT_TYPE:
2447 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2448 || TREE_CODE (arg) == REAL_CST)
2449 {
2450 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2451 if (tem != NULL_TREE)
2452 goto fold_convert_exit;
2453 }
2454
2455 switch (TREE_CODE (orig))
2456 {
2457 case FIXED_POINT_TYPE:
2458 case INTEGER_TYPE:
2459 case ENUMERAL_TYPE:
2460 case BOOLEAN_TYPE:
2461 case REAL_TYPE:
2462 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2463
2464 case COMPLEX_TYPE:
2465 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2466 return fold_convert_loc (loc, type, tem);
2467
2468 default:
2469 gcc_unreachable ();
2470 }
2471
2472 case COMPLEX_TYPE:
2473 switch (TREE_CODE (orig))
2474 {
2475 case INTEGER_TYPE:
2476 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2477 case POINTER_TYPE: case REFERENCE_TYPE:
2478 case REAL_TYPE:
2479 case FIXED_POINT_TYPE:
2480 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2481 fold_convert_loc (loc, TREE_TYPE (type), arg),
2482 fold_convert_loc (loc, TREE_TYPE (type),
2483 integer_zero_node));
2484 case COMPLEX_TYPE:
2485 {
2486 tree rpart, ipart;
2487
2488 if (TREE_CODE (arg) == COMPLEX_EXPR)
2489 {
2490 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2491 TREE_OPERAND (arg, 0));
2492 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2493 TREE_OPERAND (arg, 1));
2494 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2495 }
2496
2497 arg = save_expr (arg);
2498 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2499 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2500 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2501 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2502 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2503 }
2504
2505 default:
2506 gcc_unreachable ();
2507 }
2508
2509 case VECTOR_TYPE:
2510 if (integer_zerop (arg))
2511 return build_zero_vector (type);
2512 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2513 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2514 || TREE_CODE (orig) == VECTOR_TYPE);
2515 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2516
2517 case VOID_TYPE:
2518 tem = fold_ignored_result (arg);
2519 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2520
2521 default:
2522 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2523 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2524 gcc_unreachable ();
2525 }
2526 fold_convert_exit:
2527 protected_set_expr_location_unshare (tem, loc);
2528 return tem;
2529 }
2530 \f
2531 /* Return false if expr can be assumed not to be an lvalue, true
2532 otherwise. */
2533
2534 static bool
2535 maybe_lvalue_p (const_tree x)
2536 {
2537 /* We only need to wrap lvalue tree codes. */
2538 switch (TREE_CODE (x))
2539 {
2540 case VAR_DECL:
2541 case PARM_DECL:
2542 case RESULT_DECL:
2543 case LABEL_DECL:
2544 case FUNCTION_DECL:
2545 case SSA_NAME:
2546
2547 case COMPONENT_REF:
2548 case MEM_REF:
2549 case INDIRECT_REF:
2550 case ARRAY_REF:
2551 case ARRAY_RANGE_REF:
2552 case BIT_FIELD_REF:
2553 case OBJ_TYPE_REF:
2554
2555 case REALPART_EXPR:
2556 case IMAGPART_EXPR:
2557 case PREINCREMENT_EXPR:
2558 case PREDECREMENT_EXPR:
2559 case SAVE_EXPR:
2560 case TRY_CATCH_EXPR:
2561 case WITH_CLEANUP_EXPR:
2562 case COMPOUND_EXPR:
2563 case MODIFY_EXPR:
2564 case TARGET_EXPR:
2565 case COND_EXPR:
2566 case BIND_EXPR:
2567 break;
2568
2569 default:
2570 /* Assume the worst for front-end tree codes. */
2571 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2572 break;
2573 return false;
2574 }
2575
2576 return true;
2577 }
2578
2579 /* Return an expr equal to X but certainly not valid as an lvalue. */
2580
2581 tree
2582 non_lvalue_loc (location_t loc, tree x)
2583 {
2584 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2585 us. */
2586 if (in_gimple_form)
2587 return x;
2588
2589 if (! maybe_lvalue_p (x))
2590 return x;
2591 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2592 }
2593
2594 /* When pedantic, return an expr equal to X but certainly not valid as a
2595 pedantic lvalue. Otherwise, return X. */
2596
2597 static tree
2598 pedantic_non_lvalue_loc (location_t loc, tree x)
2599 {
2600 return protected_set_expr_location_unshare (x, loc);
2601 }
2602 \f
2603 /* Given a tree comparison code, return the code that is the logical inverse.
2604 It is generally not safe to do this for floating-point comparisons, except
2605 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2606 ERROR_MARK in this case. */
2607
2608 enum tree_code
2609 invert_tree_comparison (enum tree_code code, bool honor_nans)
2610 {
2611 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2612 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2613 return ERROR_MARK;
2614
2615 switch (code)
2616 {
2617 case EQ_EXPR:
2618 return NE_EXPR;
2619 case NE_EXPR:
2620 return EQ_EXPR;
2621 case GT_EXPR:
2622 return honor_nans ? UNLE_EXPR : LE_EXPR;
2623 case GE_EXPR:
2624 return honor_nans ? UNLT_EXPR : LT_EXPR;
2625 case LT_EXPR:
2626 return honor_nans ? UNGE_EXPR : GE_EXPR;
2627 case LE_EXPR:
2628 return honor_nans ? UNGT_EXPR : GT_EXPR;
2629 case LTGT_EXPR:
2630 return UNEQ_EXPR;
2631 case UNEQ_EXPR:
2632 return LTGT_EXPR;
2633 case UNGT_EXPR:
2634 return LE_EXPR;
2635 case UNGE_EXPR:
2636 return LT_EXPR;
2637 case UNLT_EXPR:
2638 return GE_EXPR;
2639 case UNLE_EXPR:
2640 return GT_EXPR;
2641 case ORDERED_EXPR:
2642 return UNORDERED_EXPR;
2643 case UNORDERED_EXPR:
2644 return ORDERED_EXPR;
2645 default:
2646 gcc_unreachable ();
2647 }
2648 }
2649
2650 /* Similar, but return the comparison that results if the operands are
2651 swapped. This is safe for floating-point. */
2652
2653 enum tree_code
2654 swap_tree_comparison (enum tree_code code)
2655 {
2656 switch (code)
2657 {
2658 case EQ_EXPR:
2659 case NE_EXPR:
2660 case ORDERED_EXPR:
2661 case UNORDERED_EXPR:
2662 case LTGT_EXPR:
2663 case UNEQ_EXPR:
2664 return code;
2665 case GT_EXPR:
2666 return LT_EXPR;
2667 case GE_EXPR:
2668 return LE_EXPR;
2669 case LT_EXPR:
2670 return GT_EXPR;
2671 case LE_EXPR:
2672 return GE_EXPR;
2673 case UNGT_EXPR:
2674 return UNLT_EXPR;
2675 case UNGE_EXPR:
2676 return UNLE_EXPR;
2677 case UNLT_EXPR:
2678 return UNGT_EXPR;
2679 case UNLE_EXPR:
2680 return UNGE_EXPR;
2681 default:
2682 gcc_unreachable ();
2683 }
2684 }
2685
2686
2687 /* Convert a comparison tree code from an enum tree_code representation
2688 into a compcode bit-based encoding. This function is the inverse of
2689 compcode_to_comparison. */
2690
2691 static enum comparison_code
2692 comparison_to_compcode (enum tree_code code)
2693 {
2694 switch (code)
2695 {
2696 case LT_EXPR:
2697 return COMPCODE_LT;
2698 case EQ_EXPR:
2699 return COMPCODE_EQ;
2700 case LE_EXPR:
2701 return COMPCODE_LE;
2702 case GT_EXPR:
2703 return COMPCODE_GT;
2704 case NE_EXPR:
2705 return COMPCODE_NE;
2706 case GE_EXPR:
2707 return COMPCODE_GE;
2708 case ORDERED_EXPR:
2709 return COMPCODE_ORD;
2710 case UNORDERED_EXPR:
2711 return COMPCODE_UNORD;
2712 case UNLT_EXPR:
2713 return COMPCODE_UNLT;
2714 case UNEQ_EXPR:
2715 return COMPCODE_UNEQ;
2716 case UNLE_EXPR:
2717 return COMPCODE_UNLE;
2718 case UNGT_EXPR:
2719 return COMPCODE_UNGT;
2720 case LTGT_EXPR:
2721 return COMPCODE_LTGT;
2722 case UNGE_EXPR:
2723 return COMPCODE_UNGE;
2724 default:
2725 gcc_unreachable ();
2726 }
2727 }
2728
2729 /* Convert a compcode bit-based encoding of a comparison operator back
2730 to GCC's enum tree_code representation. This function is the
2731 inverse of comparison_to_compcode. */
2732
2733 static enum tree_code
2734 compcode_to_comparison (enum comparison_code code)
2735 {
2736 switch (code)
2737 {
2738 case COMPCODE_LT:
2739 return LT_EXPR;
2740 case COMPCODE_EQ:
2741 return EQ_EXPR;
2742 case COMPCODE_LE:
2743 return LE_EXPR;
2744 case COMPCODE_GT:
2745 return GT_EXPR;
2746 case COMPCODE_NE:
2747 return NE_EXPR;
2748 case COMPCODE_GE:
2749 return GE_EXPR;
2750 case COMPCODE_ORD:
2751 return ORDERED_EXPR;
2752 case COMPCODE_UNORD:
2753 return UNORDERED_EXPR;
2754 case COMPCODE_UNLT:
2755 return UNLT_EXPR;
2756 case COMPCODE_UNEQ:
2757 return UNEQ_EXPR;
2758 case COMPCODE_UNLE:
2759 return UNLE_EXPR;
2760 case COMPCODE_UNGT:
2761 return UNGT_EXPR;
2762 case COMPCODE_LTGT:
2763 return LTGT_EXPR;
2764 case COMPCODE_UNGE:
2765 return UNGE_EXPR;
2766 default:
2767 gcc_unreachable ();
2768 }
2769 }
2770
2771 /* Return a tree for the comparison which is the combination of
2772 doing the AND or OR (depending on CODE) of the two operations LCODE
2773 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2774 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2775 if this makes the transformation invalid. */
2776
2777 tree
2778 combine_comparisons (location_t loc,
2779 enum tree_code code, enum tree_code lcode,
2780 enum tree_code rcode, tree truth_type,
2781 tree ll_arg, tree lr_arg)
2782 {
2783 bool honor_nans = HONOR_NANS (ll_arg);
2784 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2785 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2786 int compcode;
2787
2788 switch (code)
2789 {
2790 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2791 compcode = lcompcode & rcompcode;
2792 break;
2793
2794 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2795 compcode = lcompcode | rcompcode;
2796 break;
2797
2798 default:
2799 return NULL_TREE;
2800 }
2801
2802 if (!honor_nans)
2803 {
2804 /* Eliminate unordered comparisons, as well as LTGT and ORD
2805 which are not used unless the mode has NaNs. */
2806 compcode &= ~COMPCODE_UNORD;
2807 if (compcode == COMPCODE_LTGT)
2808 compcode = COMPCODE_NE;
2809 else if (compcode == COMPCODE_ORD)
2810 compcode = COMPCODE_TRUE;
2811 }
2812 else if (flag_trapping_math)
2813 {
2814 /* Check that the original operation and the optimized ones will trap
2815 under the same condition. */
2816 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2817 && (lcompcode != COMPCODE_EQ)
2818 && (lcompcode != COMPCODE_ORD);
2819 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2820 && (rcompcode != COMPCODE_EQ)
2821 && (rcompcode != COMPCODE_ORD);
2822 bool trap = (compcode & COMPCODE_UNORD) == 0
2823 && (compcode != COMPCODE_EQ)
2824 && (compcode != COMPCODE_ORD);
2825
2826 /* In a short-circuited boolean expression the LHS might be
2827 such that the RHS, if evaluated, will never trap. For
2828 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2829 if neither x nor y is NaN. (This is a mixed blessing: for
2830 example, the expression above will never trap, hence
2831 optimizing it to x < y would be invalid). */
2832 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2833 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2834 rtrap = false;
2835
2836 /* If the comparison was short-circuited, and only the RHS
2837 trapped, we may now generate a spurious trap. */
2838 if (rtrap && !ltrap
2839 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2840 return NULL_TREE;
2841
2842 /* If we changed the conditions that cause a trap, we lose. */
2843 if ((ltrap || rtrap) != trap)
2844 return NULL_TREE;
2845 }
2846
2847 if (compcode == COMPCODE_TRUE)
2848 return constant_boolean_node (true, truth_type);
2849 else if (compcode == COMPCODE_FALSE)
2850 return constant_boolean_node (false, truth_type);
2851 else
2852 {
2853 enum tree_code tcode;
2854
2855 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2856 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2857 }
2858 }
2859 \f
2860 /* Return nonzero if two operands (typically of the same tree node)
2861 are necessarily equal. FLAGS modifies behavior as follows:
2862
2863 If OEP_ONLY_CONST is set, only return nonzero for constants.
2864 This function tests whether the operands are indistinguishable;
2865 it does not test whether they are equal using C's == operation.
2866 The distinction is important for IEEE floating point, because
2867 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2868 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2869
2870 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2871 even though it may hold multiple values during a function.
2872 This is because a GCC tree node guarantees that nothing else is
2873 executed between the evaluation of its "operands" (which may often
2874 be evaluated in arbitrary order). Hence if the operands themselves
2875 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2876 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2877 unset means assuming isochronic (or instantaneous) tree equivalence.
2878 Unless comparing arbitrary expression trees, such as from different
2879 statements, this flag can usually be left unset.
2880
2881 If OEP_PURE_SAME is set, then pure functions with identical arguments
2882 are considered the same. It is used when the caller has other ways
2883 to ensure that global memory is unchanged in between.
2884
2885 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2886 not values of expressions.
2887
2888 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2889 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2890
2891 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2892 any operand with side effect. This is unnecesarily conservative in the
2893 case we know that arg0 and arg1 are in disjoint code paths (such as in
2894 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2895 addresses with TREE_CONSTANT flag set so we know that &var == &var
2896 even if var is volatile. */
2897
2898 int
2899 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2900 {
2901 /* When checking, verify at the outermost operand_equal_p call that
2902 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2903 hash value. */
2904 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2905 {
2906 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2907 {
2908 if (arg0 != arg1)
2909 {
2910 inchash::hash hstate0 (0), hstate1 (0);
2911 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2912 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2913 hashval_t h0 = hstate0.end ();
2914 hashval_t h1 = hstate1.end ();
2915 gcc_assert (h0 == h1);
2916 }
2917 return 1;
2918 }
2919 else
2920 return 0;
2921 }
2922
2923 /* If either is ERROR_MARK, they aren't equal. */
2924 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2925 || TREE_TYPE (arg0) == error_mark_node
2926 || TREE_TYPE (arg1) == error_mark_node)
2927 return 0;
2928
2929 /* Similar, if either does not have a type (like a released SSA name),
2930 they aren't equal. */
2931 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2932 return 0;
2933
2934 /* We cannot consider pointers to different address space equal. */
2935 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2936 && POINTER_TYPE_P (TREE_TYPE (arg1))
2937 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2938 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2939 return 0;
2940
2941 /* Check equality of integer constants before bailing out due to
2942 precision differences. */
2943 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2944 {
2945 /* Address of INTEGER_CST is not defined; check that we did not forget
2946 to drop the OEP_ADDRESS_OF flags. */
2947 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2948 return tree_int_cst_equal (arg0, arg1);
2949 }
2950
2951 if (!(flags & OEP_ADDRESS_OF))
2952 {
2953 /* If both types don't have the same signedness, then we can't consider
2954 them equal. We must check this before the STRIP_NOPS calls
2955 because they may change the signedness of the arguments. As pointers
2956 strictly don't have a signedness, require either two pointers or
2957 two non-pointers as well. */
2958 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2959 || POINTER_TYPE_P (TREE_TYPE (arg0))
2960 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2961 return 0;
2962
2963 /* If both types don't have the same precision, then it is not safe
2964 to strip NOPs. */
2965 if (element_precision (TREE_TYPE (arg0))
2966 != element_precision (TREE_TYPE (arg1)))
2967 return 0;
2968
2969 STRIP_NOPS (arg0);
2970 STRIP_NOPS (arg1);
2971 }
2972 #if 0
2973 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2974 sanity check once the issue is solved. */
2975 else
2976 /* Addresses of conversions and SSA_NAMEs (and many other things)
2977 are not defined. Check that we did not forget to drop the
2978 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2979 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2980 && TREE_CODE (arg0) != SSA_NAME);
2981 #endif
2982
2983 /* In case both args are comparisons but with different comparison
2984 code, try to swap the comparison operands of one arg to produce
2985 a match and compare that variant. */
2986 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2987 && COMPARISON_CLASS_P (arg0)
2988 && COMPARISON_CLASS_P (arg1))
2989 {
2990 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2991
2992 if (TREE_CODE (arg0) == swap_code)
2993 return operand_equal_p (TREE_OPERAND (arg0, 0),
2994 TREE_OPERAND (arg1, 1), flags)
2995 && operand_equal_p (TREE_OPERAND (arg0, 1),
2996 TREE_OPERAND (arg1, 0), flags);
2997 }
2998
2999 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3000 {
3001 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3002 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3003 ;
3004 else if (flags & OEP_ADDRESS_OF)
3005 {
3006 /* If we are interested in comparing addresses ignore
3007 MEM_REF wrappings of the base that can appear just for
3008 TBAA reasons. */
3009 if (TREE_CODE (arg0) == MEM_REF
3010 && DECL_P (arg1)
3011 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3012 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3013 && integer_zerop (TREE_OPERAND (arg0, 1)))
3014 return 1;
3015 else if (TREE_CODE (arg1) == MEM_REF
3016 && DECL_P (arg0)
3017 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3018 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3019 && integer_zerop (TREE_OPERAND (arg1, 1)))
3020 return 1;
3021 return 0;
3022 }
3023 else
3024 return 0;
3025 }
3026
3027 /* When not checking adddresses, this is needed for conversions and for
3028 COMPONENT_REF. Might as well play it safe and always test this. */
3029 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3030 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3031 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3032 && !(flags & OEP_ADDRESS_OF)))
3033 return 0;
3034
3035 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3036 We don't care about side effects in that case because the SAVE_EXPR
3037 takes care of that for us. In all other cases, two expressions are
3038 equal if they have no side effects. If we have two identical
3039 expressions with side effects that should be treated the same due
3040 to the only side effects being identical SAVE_EXPR's, that will
3041 be detected in the recursive calls below.
3042 If we are taking an invariant address of two identical objects
3043 they are necessarily equal as well. */
3044 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3045 && (TREE_CODE (arg0) == SAVE_EXPR
3046 || (flags & OEP_MATCH_SIDE_EFFECTS)
3047 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3048 return 1;
3049
3050 /* Next handle constant cases, those for which we can return 1 even
3051 if ONLY_CONST is set. */
3052 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3053 switch (TREE_CODE (arg0))
3054 {
3055 case INTEGER_CST:
3056 return tree_int_cst_equal (arg0, arg1);
3057
3058 case FIXED_CST:
3059 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3060 TREE_FIXED_CST (arg1));
3061
3062 case REAL_CST:
3063 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3064 return 1;
3065
3066
3067 if (!HONOR_SIGNED_ZEROS (arg0))
3068 {
3069 /* If we do not distinguish between signed and unsigned zero,
3070 consider them equal. */
3071 if (real_zerop (arg0) && real_zerop (arg1))
3072 return 1;
3073 }
3074 return 0;
3075
3076 case VECTOR_CST:
3077 {
3078 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3079 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3080 return 0;
3081
3082 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3083 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3084 return 0;
3085
3086 unsigned int count = vector_cst_encoded_nelts (arg0);
3087 for (unsigned int i = 0; i < count; ++i)
3088 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3089 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3090 return 0;
3091 return 1;
3092 }
3093
3094 case COMPLEX_CST:
3095 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3096 flags)
3097 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3098 flags));
3099
3100 case STRING_CST:
3101 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3102 && ! memcmp (TREE_STRING_POINTER (arg0),
3103 TREE_STRING_POINTER (arg1),
3104 TREE_STRING_LENGTH (arg0)));
3105
3106 case ADDR_EXPR:
3107 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3108 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3109 flags | OEP_ADDRESS_OF
3110 | OEP_MATCH_SIDE_EFFECTS);
3111 case CONSTRUCTOR:
3112 /* In GIMPLE empty constructors are allowed in initializers of
3113 aggregates. */
3114 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3115 default:
3116 break;
3117 }
3118
3119 if (flags & OEP_ONLY_CONST)
3120 return 0;
3121
3122 /* Define macros to test an operand from arg0 and arg1 for equality and a
3123 variant that allows null and views null as being different from any
3124 non-null value. In the latter case, if either is null, the both
3125 must be; otherwise, do the normal comparison. */
3126 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3127 TREE_OPERAND (arg1, N), flags)
3128
3129 #define OP_SAME_WITH_NULL(N) \
3130 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3131 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3132
3133 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3134 {
3135 case tcc_unary:
3136 /* Two conversions are equal only if signedness and modes match. */
3137 switch (TREE_CODE (arg0))
3138 {
3139 CASE_CONVERT:
3140 case FIX_TRUNC_EXPR:
3141 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3142 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3143 return 0;
3144 break;
3145 default:
3146 break;
3147 }
3148
3149 return OP_SAME (0);
3150
3151
3152 case tcc_comparison:
3153 case tcc_binary:
3154 if (OP_SAME (0) && OP_SAME (1))
3155 return 1;
3156
3157 /* For commutative ops, allow the other order. */
3158 return (commutative_tree_code (TREE_CODE (arg0))
3159 && operand_equal_p (TREE_OPERAND (arg0, 0),
3160 TREE_OPERAND (arg1, 1), flags)
3161 && operand_equal_p (TREE_OPERAND (arg0, 1),
3162 TREE_OPERAND (arg1, 0), flags));
3163
3164 case tcc_reference:
3165 /* If either of the pointer (or reference) expressions we are
3166 dereferencing contain a side effect, these cannot be equal,
3167 but their addresses can be. */
3168 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3169 && (TREE_SIDE_EFFECTS (arg0)
3170 || TREE_SIDE_EFFECTS (arg1)))
3171 return 0;
3172
3173 switch (TREE_CODE (arg0))
3174 {
3175 case INDIRECT_REF:
3176 if (!(flags & OEP_ADDRESS_OF)
3177 && (TYPE_ALIGN (TREE_TYPE (arg0))
3178 != TYPE_ALIGN (TREE_TYPE (arg1))))
3179 return 0;
3180 flags &= ~OEP_ADDRESS_OF;
3181 return OP_SAME (0);
3182
3183 case IMAGPART_EXPR:
3184 /* Require the same offset. */
3185 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3186 TYPE_SIZE (TREE_TYPE (arg1)),
3187 flags & ~OEP_ADDRESS_OF))
3188 return 0;
3189
3190 /* Fallthru. */
3191 case REALPART_EXPR:
3192 case VIEW_CONVERT_EXPR:
3193 return OP_SAME (0);
3194
3195 case TARGET_MEM_REF:
3196 case MEM_REF:
3197 if (!(flags & OEP_ADDRESS_OF))
3198 {
3199 /* Require equal access sizes */
3200 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3201 && (!TYPE_SIZE (TREE_TYPE (arg0))
3202 || !TYPE_SIZE (TREE_TYPE (arg1))
3203 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3204 TYPE_SIZE (TREE_TYPE (arg1)),
3205 flags)))
3206 return 0;
3207 /* Verify that access happens in similar types. */
3208 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3209 return 0;
3210 /* Verify that accesses are TBAA compatible. */
3211 if (!alias_ptr_types_compatible_p
3212 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3213 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3214 || (MR_DEPENDENCE_CLIQUE (arg0)
3215 != MR_DEPENDENCE_CLIQUE (arg1))
3216 || (MR_DEPENDENCE_BASE (arg0)
3217 != MR_DEPENDENCE_BASE (arg1)))
3218 return 0;
3219 /* Verify that alignment is compatible. */
3220 if (TYPE_ALIGN (TREE_TYPE (arg0))
3221 != TYPE_ALIGN (TREE_TYPE (arg1)))
3222 return 0;
3223 }
3224 flags &= ~OEP_ADDRESS_OF;
3225 return (OP_SAME (0) && OP_SAME (1)
3226 /* TARGET_MEM_REF require equal extra operands. */
3227 && (TREE_CODE (arg0) != TARGET_MEM_REF
3228 || (OP_SAME_WITH_NULL (2)
3229 && OP_SAME_WITH_NULL (3)
3230 && OP_SAME_WITH_NULL (4))));
3231
3232 case ARRAY_REF:
3233 case ARRAY_RANGE_REF:
3234 if (!OP_SAME (0))
3235 return 0;
3236 flags &= ~OEP_ADDRESS_OF;
3237 /* Compare the array index by value if it is constant first as we
3238 may have different types but same value here. */
3239 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3240 TREE_OPERAND (arg1, 1))
3241 || OP_SAME (1))
3242 && OP_SAME_WITH_NULL (2)
3243 && OP_SAME_WITH_NULL (3)
3244 /* Compare low bound and element size as with OEP_ADDRESS_OF
3245 we have to account for the offset of the ref. */
3246 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3247 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3248 || (operand_equal_p (array_ref_low_bound
3249 (CONST_CAST_TREE (arg0)),
3250 array_ref_low_bound
3251 (CONST_CAST_TREE (arg1)), flags)
3252 && operand_equal_p (array_ref_element_size
3253 (CONST_CAST_TREE (arg0)),
3254 array_ref_element_size
3255 (CONST_CAST_TREE (arg1)),
3256 flags))));
3257
3258 case COMPONENT_REF:
3259 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3260 may be NULL when we're called to compare MEM_EXPRs. */
3261 if (!OP_SAME_WITH_NULL (0)
3262 || !OP_SAME (1))
3263 return 0;
3264 flags &= ~OEP_ADDRESS_OF;
3265 return OP_SAME_WITH_NULL (2);
3266
3267 case BIT_FIELD_REF:
3268 if (!OP_SAME (0))
3269 return 0;
3270 flags &= ~OEP_ADDRESS_OF;
3271 return OP_SAME (1) && OP_SAME (2);
3272
3273 default:
3274 return 0;
3275 }
3276
3277 case tcc_expression:
3278 switch (TREE_CODE (arg0))
3279 {
3280 case ADDR_EXPR:
3281 /* Be sure we pass right ADDRESS_OF flag. */
3282 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3283 return operand_equal_p (TREE_OPERAND (arg0, 0),
3284 TREE_OPERAND (arg1, 0),
3285 flags | OEP_ADDRESS_OF);
3286
3287 case TRUTH_NOT_EXPR:
3288 return OP_SAME (0);
3289
3290 case TRUTH_ANDIF_EXPR:
3291 case TRUTH_ORIF_EXPR:
3292 return OP_SAME (0) && OP_SAME (1);
3293
3294 case FMA_EXPR:
3295 case WIDEN_MULT_PLUS_EXPR:
3296 case WIDEN_MULT_MINUS_EXPR:
3297 if (!OP_SAME (2))
3298 return 0;
3299 /* The multiplcation operands are commutative. */
3300 /* FALLTHRU */
3301
3302 case TRUTH_AND_EXPR:
3303 case TRUTH_OR_EXPR:
3304 case TRUTH_XOR_EXPR:
3305 if (OP_SAME (0) && OP_SAME (1))
3306 return 1;
3307
3308 /* Otherwise take into account this is a commutative operation. */
3309 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3310 TREE_OPERAND (arg1, 1), flags)
3311 && operand_equal_p (TREE_OPERAND (arg0, 1),
3312 TREE_OPERAND (arg1, 0), flags));
3313
3314 case COND_EXPR:
3315 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3316 return 0;
3317 flags &= ~OEP_ADDRESS_OF;
3318 return OP_SAME (0);
3319
3320 case BIT_INSERT_EXPR:
3321 /* BIT_INSERT_EXPR has an implict operand as the type precision
3322 of op1. Need to check to make sure they are the same. */
3323 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3324 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3325 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3326 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3327 return false;
3328 /* FALLTHRU */
3329
3330 case VEC_COND_EXPR:
3331 case DOT_PROD_EXPR:
3332 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3333
3334 case MODIFY_EXPR:
3335 case INIT_EXPR:
3336 case COMPOUND_EXPR:
3337 case PREDECREMENT_EXPR:
3338 case PREINCREMENT_EXPR:
3339 case POSTDECREMENT_EXPR:
3340 case POSTINCREMENT_EXPR:
3341 if (flags & OEP_LEXICOGRAPHIC)
3342 return OP_SAME (0) && OP_SAME (1);
3343 return 0;
3344
3345 case CLEANUP_POINT_EXPR:
3346 case EXPR_STMT:
3347 if (flags & OEP_LEXICOGRAPHIC)
3348 return OP_SAME (0);
3349 return 0;
3350
3351 default:
3352 return 0;
3353 }
3354
3355 case tcc_vl_exp:
3356 switch (TREE_CODE (arg0))
3357 {
3358 case CALL_EXPR:
3359 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3360 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3361 /* If not both CALL_EXPRs are either internal or normal function
3362 functions, then they are not equal. */
3363 return 0;
3364 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3365 {
3366 /* If the CALL_EXPRs call different internal functions, then they
3367 are not equal. */
3368 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3369 return 0;
3370 }
3371 else
3372 {
3373 /* If the CALL_EXPRs call different functions, then they are not
3374 equal. */
3375 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3376 flags))
3377 return 0;
3378 }
3379
3380 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3381 {
3382 unsigned int cef = call_expr_flags (arg0);
3383 if (flags & OEP_PURE_SAME)
3384 cef &= ECF_CONST | ECF_PURE;
3385 else
3386 cef &= ECF_CONST;
3387 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3388 return 0;
3389 }
3390
3391 /* Now see if all the arguments are the same. */
3392 {
3393 const_call_expr_arg_iterator iter0, iter1;
3394 const_tree a0, a1;
3395 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3396 a1 = first_const_call_expr_arg (arg1, &iter1);
3397 a0 && a1;
3398 a0 = next_const_call_expr_arg (&iter0),
3399 a1 = next_const_call_expr_arg (&iter1))
3400 if (! operand_equal_p (a0, a1, flags))
3401 return 0;
3402
3403 /* If we get here and both argument lists are exhausted
3404 then the CALL_EXPRs are equal. */
3405 return ! (a0 || a1);
3406 }
3407 default:
3408 return 0;
3409 }
3410
3411 case tcc_declaration:
3412 /* Consider __builtin_sqrt equal to sqrt. */
3413 return (TREE_CODE (arg0) == FUNCTION_DECL
3414 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3415 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3416 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3417
3418 case tcc_exceptional:
3419 if (TREE_CODE (arg0) == CONSTRUCTOR)
3420 {
3421 /* In GIMPLE constructors are used only to build vectors from
3422 elements. Individual elements in the constructor must be
3423 indexed in increasing order and form an initial sequence.
3424
3425 We make no effort to compare constructors in generic.
3426 (see sem_variable::equals in ipa-icf which can do so for
3427 constants). */
3428 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3429 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3430 return 0;
3431
3432 /* Be sure that vectors constructed have the same representation.
3433 We only tested element precision and modes to match.
3434 Vectors may be BLKmode and thus also check that the number of
3435 parts match. */
3436 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3437 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3438 return 0;
3439
3440 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3441 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3442 unsigned int len = vec_safe_length (v0);
3443
3444 if (len != vec_safe_length (v1))
3445 return 0;
3446
3447 for (unsigned int i = 0; i < len; i++)
3448 {
3449 constructor_elt *c0 = &(*v0)[i];
3450 constructor_elt *c1 = &(*v1)[i];
3451
3452 if (!operand_equal_p (c0->value, c1->value, flags)
3453 /* In GIMPLE the indexes can be either NULL or matching i.
3454 Double check this so we won't get false
3455 positives for GENERIC. */
3456 || (c0->index
3457 && (TREE_CODE (c0->index) != INTEGER_CST
3458 || !compare_tree_int (c0->index, i)))
3459 || (c1->index
3460 && (TREE_CODE (c1->index) != INTEGER_CST
3461 || !compare_tree_int (c1->index, i))))
3462 return 0;
3463 }
3464 return 1;
3465 }
3466 else if (TREE_CODE (arg0) == STATEMENT_LIST
3467 && (flags & OEP_LEXICOGRAPHIC))
3468 {
3469 /* Compare the STATEMENT_LISTs. */
3470 tree_stmt_iterator tsi1, tsi2;
3471 tree body1 = CONST_CAST_TREE (arg0);
3472 tree body2 = CONST_CAST_TREE (arg1);
3473 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3474 tsi_next (&tsi1), tsi_next (&tsi2))
3475 {
3476 /* The lists don't have the same number of statements. */
3477 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3478 return 0;
3479 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3480 return 1;
3481 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3482 OEP_LEXICOGRAPHIC))
3483 return 0;
3484 }
3485 }
3486 return 0;
3487
3488 case tcc_statement:
3489 switch (TREE_CODE (arg0))
3490 {
3491 case RETURN_EXPR:
3492 if (flags & OEP_LEXICOGRAPHIC)
3493 return OP_SAME_WITH_NULL (0);
3494 return 0;
3495 default:
3496 return 0;
3497 }
3498
3499 default:
3500 return 0;
3501 }
3502
3503 #undef OP_SAME
3504 #undef OP_SAME_WITH_NULL
3505 }
3506 \f
3507 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3508 with a different signedness or a narrower precision. */
3509
3510 static bool
3511 operand_equal_for_comparison_p (tree arg0, tree arg1)
3512 {
3513 if (operand_equal_p (arg0, arg1, 0))
3514 return true;
3515
3516 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3517 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3518 return false;
3519
3520 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3521 and see if the inner values are the same. This removes any
3522 signedness comparison, which doesn't matter here. */
3523 tree op0 = arg0;
3524 tree op1 = arg1;
3525 STRIP_NOPS (op0);
3526 STRIP_NOPS (op1);
3527 if (operand_equal_p (op0, op1, 0))
3528 return true;
3529
3530 /* Discard a single widening conversion from ARG1 and see if the inner
3531 value is the same as ARG0. */
3532 if (CONVERT_EXPR_P (arg1)
3533 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3534 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3535 < TYPE_PRECISION (TREE_TYPE (arg1))
3536 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3537 return true;
3538
3539 return false;
3540 }
3541 \f
3542 /* See if ARG is an expression that is either a comparison or is performing
3543 arithmetic on comparisons. The comparisons must only be comparing
3544 two different values, which will be stored in *CVAL1 and *CVAL2; if
3545 they are nonzero it means that some operands have already been found.
3546 No variables may be used anywhere else in the expression except in the
3547 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3548 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3549
3550 If this is true, return 1. Otherwise, return zero. */
3551
3552 static int
3553 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3554 {
3555 enum tree_code code = TREE_CODE (arg);
3556 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3557
3558 /* We can handle some of the tcc_expression cases here. */
3559 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3560 tclass = tcc_unary;
3561 else if (tclass == tcc_expression
3562 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3563 || code == COMPOUND_EXPR))
3564 tclass = tcc_binary;
3565
3566 else if (tclass == tcc_expression && code == SAVE_EXPR
3567 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3568 {
3569 /* If we've already found a CVAL1 or CVAL2, this expression is
3570 two complex to handle. */
3571 if (*cval1 || *cval2)
3572 return 0;
3573
3574 tclass = tcc_unary;
3575 *save_p = 1;
3576 }
3577
3578 switch (tclass)
3579 {
3580 case tcc_unary:
3581 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3582
3583 case tcc_binary:
3584 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3585 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3586 cval1, cval2, save_p));
3587
3588 case tcc_constant:
3589 return 1;
3590
3591 case tcc_expression:
3592 if (code == COND_EXPR)
3593 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3594 cval1, cval2, save_p)
3595 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3596 cval1, cval2, save_p)
3597 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3598 cval1, cval2, save_p));
3599 return 0;
3600
3601 case tcc_comparison:
3602 /* First see if we can handle the first operand, then the second. For
3603 the second operand, we know *CVAL1 can't be zero. It must be that
3604 one side of the comparison is each of the values; test for the
3605 case where this isn't true by failing if the two operands
3606 are the same. */
3607
3608 if (operand_equal_p (TREE_OPERAND (arg, 0),
3609 TREE_OPERAND (arg, 1), 0))
3610 return 0;
3611
3612 if (*cval1 == 0)
3613 *cval1 = TREE_OPERAND (arg, 0);
3614 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3615 ;
3616 else if (*cval2 == 0)
3617 *cval2 = TREE_OPERAND (arg, 0);
3618 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3619 ;
3620 else
3621 return 0;
3622
3623 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3624 ;
3625 else if (*cval2 == 0)
3626 *cval2 = TREE_OPERAND (arg, 1);
3627 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3628 ;
3629 else
3630 return 0;
3631
3632 return 1;
3633
3634 default:
3635 return 0;
3636 }
3637 }
3638 \f
3639 /* ARG is a tree that is known to contain just arithmetic operations and
3640 comparisons. Evaluate the operations in the tree substituting NEW0 for
3641 any occurrence of OLD0 as an operand of a comparison and likewise for
3642 NEW1 and OLD1. */
3643
3644 static tree
3645 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3646 tree old1, tree new1)
3647 {
3648 tree type = TREE_TYPE (arg);
3649 enum tree_code code = TREE_CODE (arg);
3650 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3651
3652 /* We can handle some of the tcc_expression cases here. */
3653 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3654 tclass = tcc_unary;
3655 else if (tclass == tcc_expression
3656 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3657 tclass = tcc_binary;
3658
3659 switch (tclass)
3660 {
3661 case tcc_unary:
3662 return fold_build1_loc (loc, code, type,
3663 eval_subst (loc, TREE_OPERAND (arg, 0),
3664 old0, new0, old1, new1));
3665
3666 case tcc_binary:
3667 return fold_build2_loc (loc, code, type,
3668 eval_subst (loc, TREE_OPERAND (arg, 0),
3669 old0, new0, old1, new1),
3670 eval_subst (loc, TREE_OPERAND (arg, 1),
3671 old0, new0, old1, new1));
3672
3673 case tcc_expression:
3674 switch (code)
3675 {
3676 case SAVE_EXPR:
3677 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3678 old1, new1);
3679
3680 case COMPOUND_EXPR:
3681 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3682 old1, new1);
3683
3684 case COND_EXPR:
3685 return fold_build3_loc (loc, code, type,
3686 eval_subst (loc, TREE_OPERAND (arg, 0),
3687 old0, new0, old1, new1),
3688 eval_subst (loc, TREE_OPERAND (arg, 1),
3689 old0, new0, old1, new1),
3690 eval_subst (loc, TREE_OPERAND (arg, 2),
3691 old0, new0, old1, new1));
3692 default:
3693 break;
3694 }
3695 /* Fall through - ??? */
3696
3697 case tcc_comparison:
3698 {
3699 tree arg0 = TREE_OPERAND (arg, 0);
3700 tree arg1 = TREE_OPERAND (arg, 1);
3701
3702 /* We need to check both for exact equality and tree equality. The
3703 former will be true if the operand has a side-effect. In that
3704 case, we know the operand occurred exactly once. */
3705
3706 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3707 arg0 = new0;
3708 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3709 arg0 = new1;
3710
3711 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3712 arg1 = new0;
3713 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3714 arg1 = new1;
3715
3716 return fold_build2_loc (loc, code, type, arg0, arg1);
3717 }
3718
3719 default:
3720 return arg;
3721 }
3722 }
3723 \f
3724 /* Return a tree for the case when the result of an expression is RESULT
3725 converted to TYPE and OMITTED was previously an operand of the expression
3726 but is now not needed (e.g., we folded OMITTED * 0).
3727
3728 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3729 the conversion of RESULT to TYPE. */
3730
3731 tree
3732 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3733 {
3734 tree t = fold_convert_loc (loc, type, result);
3735
3736 /* If the resulting operand is an empty statement, just return the omitted
3737 statement casted to void. */
3738 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3739 return build1_loc (loc, NOP_EXPR, void_type_node,
3740 fold_ignored_result (omitted));
3741
3742 if (TREE_SIDE_EFFECTS (omitted))
3743 return build2_loc (loc, COMPOUND_EXPR, type,
3744 fold_ignored_result (omitted), t);
3745
3746 return non_lvalue_loc (loc, t);
3747 }
3748
3749 /* Return a tree for the case when the result of an expression is RESULT
3750 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3751 of the expression but are now not needed.
3752
3753 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3754 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3755 evaluated before OMITTED2. Otherwise, if neither has side effects,
3756 just do the conversion of RESULT to TYPE. */
3757
3758 tree
3759 omit_two_operands_loc (location_t loc, tree type, tree result,
3760 tree omitted1, tree omitted2)
3761 {
3762 tree t = fold_convert_loc (loc, type, result);
3763
3764 if (TREE_SIDE_EFFECTS (omitted2))
3765 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3766 if (TREE_SIDE_EFFECTS (omitted1))
3767 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3768
3769 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3770 }
3771
3772 \f
3773 /* Return a simplified tree node for the truth-negation of ARG. This
3774 never alters ARG itself. We assume that ARG is an operation that
3775 returns a truth value (0 or 1).
3776
3777 FIXME: one would think we would fold the result, but it causes
3778 problems with the dominator optimizer. */
3779
3780 static tree
3781 fold_truth_not_expr (location_t loc, tree arg)
3782 {
3783 tree type = TREE_TYPE (arg);
3784 enum tree_code code = TREE_CODE (arg);
3785 location_t loc1, loc2;
3786
3787 /* If this is a comparison, we can simply invert it, except for
3788 floating-point non-equality comparisons, in which case we just
3789 enclose a TRUTH_NOT_EXPR around what we have. */
3790
3791 if (TREE_CODE_CLASS (code) == tcc_comparison)
3792 {
3793 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3794 if (FLOAT_TYPE_P (op_type)
3795 && flag_trapping_math
3796 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3797 && code != NE_EXPR && code != EQ_EXPR)
3798 return NULL_TREE;
3799
3800 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3801 if (code == ERROR_MARK)
3802 return NULL_TREE;
3803
3804 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3805 TREE_OPERAND (arg, 1));
3806 if (TREE_NO_WARNING (arg))
3807 TREE_NO_WARNING (ret) = 1;
3808 return ret;
3809 }
3810
3811 switch (code)
3812 {
3813 case INTEGER_CST:
3814 return constant_boolean_node (integer_zerop (arg), type);
3815
3816 case TRUTH_AND_EXPR:
3817 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3818 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3819 return build2_loc (loc, TRUTH_OR_EXPR, type,
3820 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3821 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3822
3823 case TRUTH_OR_EXPR:
3824 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3825 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3826 return build2_loc (loc, TRUTH_AND_EXPR, type,
3827 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3828 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3829
3830 case TRUTH_XOR_EXPR:
3831 /* Here we can invert either operand. We invert the first operand
3832 unless the second operand is a TRUTH_NOT_EXPR in which case our
3833 result is the XOR of the first operand with the inside of the
3834 negation of the second operand. */
3835
3836 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3837 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3838 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3839 else
3840 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3841 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3842 TREE_OPERAND (arg, 1));
3843
3844 case TRUTH_ANDIF_EXPR:
3845 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3846 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3847 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3848 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3849 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3850
3851 case TRUTH_ORIF_EXPR:
3852 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3853 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3854 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3855 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3856 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3857
3858 case TRUTH_NOT_EXPR:
3859 return TREE_OPERAND (arg, 0);
3860
3861 case COND_EXPR:
3862 {
3863 tree arg1 = TREE_OPERAND (arg, 1);
3864 tree arg2 = TREE_OPERAND (arg, 2);
3865
3866 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3867 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3868
3869 /* A COND_EXPR may have a throw as one operand, which
3870 then has void type. Just leave void operands
3871 as they are. */
3872 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3873 VOID_TYPE_P (TREE_TYPE (arg1))
3874 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3875 VOID_TYPE_P (TREE_TYPE (arg2))
3876 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3877 }
3878
3879 case COMPOUND_EXPR:
3880 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3881 return build2_loc (loc, COMPOUND_EXPR, type,
3882 TREE_OPERAND (arg, 0),
3883 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3884
3885 case NON_LVALUE_EXPR:
3886 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3887 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3888
3889 CASE_CONVERT:
3890 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3891 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3892
3893 /* fall through */
3894
3895 case FLOAT_EXPR:
3896 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3897 return build1_loc (loc, TREE_CODE (arg), type,
3898 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3899
3900 case BIT_AND_EXPR:
3901 if (!integer_onep (TREE_OPERAND (arg, 1)))
3902 return NULL_TREE;
3903 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3904
3905 case SAVE_EXPR:
3906 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3907
3908 case CLEANUP_POINT_EXPR:
3909 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3910 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3911 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3912
3913 default:
3914 return NULL_TREE;
3915 }
3916 }
3917
3918 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3919 assume that ARG is an operation that returns a truth value (0 or 1
3920 for scalars, 0 or -1 for vectors). Return the folded expression if
3921 folding is successful. Otherwise, return NULL_TREE. */
3922
3923 static tree
3924 fold_invert_truthvalue (location_t loc, tree arg)
3925 {
3926 tree type = TREE_TYPE (arg);
3927 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3928 ? BIT_NOT_EXPR
3929 : TRUTH_NOT_EXPR,
3930 type, arg);
3931 }
3932
3933 /* Return a simplified tree node for the truth-negation of ARG. This
3934 never alters ARG itself. We assume that ARG is an operation that
3935 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3936
3937 tree
3938 invert_truthvalue_loc (location_t loc, tree arg)
3939 {
3940 if (TREE_CODE (arg) == ERROR_MARK)
3941 return arg;
3942
3943 tree type = TREE_TYPE (arg);
3944 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3945 ? BIT_NOT_EXPR
3946 : TRUTH_NOT_EXPR,
3947 type, arg);
3948 }
3949 \f
3950 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3951 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3952 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3953 is the original memory reference used to preserve the alias set of
3954 the access. */
3955
3956 static tree
3957 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3958 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3959 int unsignedp, int reversep)
3960 {
3961 tree result, bftype;
3962
3963 /* Attempt not to lose the access path if possible. */
3964 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3965 {
3966 tree ninner = TREE_OPERAND (orig_inner, 0);
3967 machine_mode nmode;
3968 poly_int64 nbitsize, nbitpos;
3969 tree noffset;
3970 int nunsignedp, nreversep, nvolatilep = 0;
3971 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3972 &noffset, &nmode, &nunsignedp,
3973 &nreversep, &nvolatilep);
3974 if (base == inner
3975 && noffset == NULL_TREE
3976 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3977 && !reversep
3978 && !nreversep
3979 && !nvolatilep)
3980 {
3981 inner = ninner;
3982 bitpos -= nbitpos;
3983 }
3984 }
3985
3986 alias_set_type iset = get_alias_set (orig_inner);
3987 if (iset == 0 && get_alias_set (inner) != iset)
3988 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3989 build_fold_addr_expr (inner),
3990 build_int_cst (ptr_type_node, 0));
3991
3992 if (known_eq (bitpos, 0) && !reversep)
3993 {
3994 tree size = TYPE_SIZE (TREE_TYPE (inner));
3995 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3996 || POINTER_TYPE_P (TREE_TYPE (inner)))
3997 && tree_fits_shwi_p (size)
3998 && tree_to_shwi (size) == bitsize)
3999 return fold_convert_loc (loc, type, inner);
4000 }
4001
4002 bftype = type;
4003 if (TYPE_PRECISION (bftype) != bitsize
4004 || TYPE_UNSIGNED (bftype) == !unsignedp)
4005 bftype = build_nonstandard_integer_type (bitsize, 0);
4006
4007 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4008 bitsize_int (bitsize), bitsize_int (bitpos));
4009 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4010
4011 if (bftype != type)
4012 result = fold_convert_loc (loc, type, result);
4013
4014 return result;
4015 }
4016
4017 /* Optimize a bit-field compare.
4018
4019 There are two cases: First is a compare against a constant and the
4020 second is a comparison of two items where the fields are at the same
4021 bit position relative to the start of a chunk (byte, halfword, word)
4022 large enough to contain it. In these cases we can avoid the shift
4023 implicit in bitfield extractions.
4024
4025 For constants, we emit a compare of the shifted constant with the
4026 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4027 compared. For two fields at the same position, we do the ANDs with the
4028 similar mask and compare the result of the ANDs.
4029
4030 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4031 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4032 are the left and right operands of the comparison, respectively.
4033
4034 If the optimization described above can be done, we return the resulting
4035 tree. Otherwise we return zero. */
4036
4037 static tree
4038 optimize_bit_field_compare (location_t loc, enum tree_code code,
4039 tree compare_type, tree lhs, tree rhs)
4040 {
4041 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4042 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4043 tree type = TREE_TYPE (lhs);
4044 tree unsigned_type;
4045 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4046 machine_mode lmode, rmode;
4047 scalar_int_mode nmode;
4048 int lunsignedp, runsignedp;
4049 int lreversep, rreversep;
4050 int lvolatilep = 0, rvolatilep = 0;
4051 tree linner, rinner = NULL_TREE;
4052 tree mask;
4053 tree offset;
4054
4055 /* Get all the information about the extractions being done. If the bit size
4056 is the same as the size of the underlying object, we aren't doing an
4057 extraction at all and so can do nothing. We also don't want to
4058 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4059 then will no longer be able to replace it. */
4060 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4061 &lunsignedp, &lreversep, &lvolatilep);
4062 if (linner == lhs
4063 || !known_size_p (plbitsize)
4064 || !plbitsize.is_constant (&lbitsize)
4065 || !plbitpos.is_constant (&lbitpos)
4066 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4067 || offset != 0
4068 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4069 || lvolatilep)
4070 return 0;
4071
4072 if (const_p)
4073 rreversep = lreversep;
4074 else
4075 {
4076 /* If this is not a constant, we can only do something if bit positions,
4077 sizes, signedness and storage order are the same. */
4078 rinner
4079 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4080 &runsignedp, &rreversep, &rvolatilep);
4081
4082 if (rinner == rhs
4083 || maybe_ne (lbitpos, rbitpos)
4084 || maybe_ne (lbitsize, rbitsize)
4085 || lunsignedp != runsignedp
4086 || lreversep != rreversep
4087 || offset != 0
4088 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4089 || rvolatilep)
4090 return 0;
4091 }
4092
4093 /* Honor the C++ memory model and mimic what RTL expansion does. */
4094 poly_uint64 bitstart = 0;
4095 poly_uint64 bitend = 0;
4096 if (TREE_CODE (lhs) == COMPONENT_REF)
4097 {
4098 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4099 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4100 return 0;
4101 }
4102
4103 /* See if we can find a mode to refer to this field. We should be able to,
4104 but fail if we can't. */
4105 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4106 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4107 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4108 TYPE_ALIGN (TREE_TYPE (rinner))),
4109 BITS_PER_WORD, false, &nmode))
4110 return 0;
4111
4112 /* Set signed and unsigned types of the precision of this mode for the
4113 shifts below. */
4114 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4115
4116 /* Compute the bit position and size for the new reference and our offset
4117 within it. If the new reference is the same size as the original, we
4118 won't optimize anything, so return zero. */
4119 nbitsize = GET_MODE_BITSIZE (nmode);
4120 nbitpos = lbitpos & ~ (nbitsize - 1);
4121 lbitpos -= nbitpos;
4122 if (nbitsize == lbitsize)
4123 return 0;
4124
4125 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4126 lbitpos = nbitsize - lbitsize - lbitpos;
4127
4128 /* Make the mask to be used against the extracted field. */
4129 mask = build_int_cst_type (unsigned_type, -1);
4130 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4131 mask = const_binop (RSHIFT_EXPR, mask,
4132 size_int (nbitsize - lbitsize - lbitpos));
4133
4134 if (! const_p)
4135 {
4136 if (nbitpos < 0)
4137 return 0;
4138
4139 /* If not comparing with constant, just rework the comparison
4140 and return. */
4141 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4142 nbitsize, nbitpos, 1, lreversep);
4143 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4144 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4145 nbitsize, nbitpos, 1, rreversep);
4146 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4147 return fold_build2_loc (loc, code, compare_type, t1, t2);
4148 }
4149
4150 /* Otherwise, we are handling the constant case. See if the constant is too
4151 big for the field. Warn and return a tree for 0 (false) if so. We do
4152 this not only for its own sake, but to avoid having to test for this
4153 error case below. If we didn't, we might generate wrong code.
4154
4155 For unsigned fields, the constant shifted right by the field length should
4156 be all zero. For signed fields, the high-order bits should agree with
4157 the sign bit. */
4158
4159 if (lunsignedp)
4160 {
4161 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4162 {
4163 warning (0, "comparison is always %d due to width of bit-field",
4164 code == NE_EXPR);
4165 return constant_boolean_node (code == NE_EXPR, compare_type);
4166 }
4167 }
4168 else
4169 {
4170 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4171 if (tem != 0 && tem != -1)
4172 {
4173 warning (0, "comparison is always %d due to width of bit-field",
4174 code == NE_EXPR);
4175 return constant_boolean_node (code == NE_EXPR, compare_type);
4176 }
4177 }
4178
4179 if (nbitpos < 0)
4180 return 0;
4181
4182 /* Single-bit compares should always be against zero. */
4183 if (lbitsize == 1 && ! integer_zerop (rhs))
4184 {
4185 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4186 rhs = build_int_cst (type, 0);
4187 }
4188
4189 /* Make a new bitfield reference, shift the constant over the
4190 appropriate number of bits and mask it with the computed mask
4191 (in case this was a signed field). If we changed it, make a new one. */
4192 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4193 nbitsize, nbitpos, 1, lreversep);
4194
4195 rhs = const_binop (BIT_AND_EXPR,
4196 const_binop (LSHIFT_EXPR,
4197 fold_convert_loc (loc, unsigned_type, rhs),
4198 size_int (lbitpos)),
4199 mask);
4200
4201 lhs = build2_loc (loc, code, compare_type,
4202 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4203 return lhs;
4204 }
4205 \f
4206 /* Subroutine for fold_truth_andor_1: decode a field reference.
4207
4208 If EXP is a comparison reference, we return the innermost reference.
4209
4210 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4211 set to the starting bit number.
4212
4213 If the innermost field can be completely contained in a mode-sized
4214 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4215
4216 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4217 otherwise it is not changed.
4218
4219 *PUNSIGNEDP is set to the signedness of the field.
4220
4221 *PREVERSEP is set to the storage order of the field.
4222
4223 *PMASK is set to the mask used. This is either contained in a
4224 BIT_AND_EXPR or derived from the width of the field.
4225
4226 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4227
4228 Return 0 if this is not a component reference or is one that we can't
4229 do anything with. */
4230
4231 static tree
4232 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4233 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4234 int *punsignedp, int *preversep, int *pvolatilep,
4235 tree *pmask, tree *pand_mask)
4236 {
4237 tree exp = *exp_;
4238 tree outer_type = 0;
4239 tree and_mask = 0;
4240 tree mask, inner, offset;
4241 tree unsigned_type;
4242 unsigned int precision;
4243
4244 /* All the optimizations using this function assume integer fields.
4245 There are problems with FP fields since the type_for_size call
4246 below can fail for, e.g., XFmode. */
4247 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4248 return 0;
4249
4250 /* We are interested in the bare arrangement of bits, so strip everything
4251 that doesn't affect the machine mode. However, record the type of the
4252 outermost expression if it may matter below. */
4253 if (CONVERT_EXPR_P (exp)
4254 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4255 outer_type = TREE_TYPE (exp);
4256 STRIP_NOPS (exp);
4257
4258 if (TREE_CODE (exp) == BIT_AND_EXPR)
4259 {
4260 and_mask = TREE_OPERAND (exp, 1);
4261 exp = TREE_OPERAND (exp, 0);
4262 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4263 if (TREE_CODE (and_mask) != INTEGER_CST)
4264 return 0;
4265 }
4266
4267 poly_int64 poly_bitsize, poly_bitpos;
4268 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4269 pmode, punsignedp, preversep, pvolatilep);
4270 if ((inner == exp && and_mask == 0)
4271 || !poly_bitsize.is_constant (pbitsize)
4272 || !poly_bitpos.is_constant (pbitpos)
4273 || *pbitsize < 0
4274 || offset != 0
4275 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4276 /* Reject out-of-bound accesses (PR79731). */
4277 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4278 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4279 *pbitpos + *pbitsize) < 0))
4280 return 0;
4281
4282 *exp_ = exp;
4283
4284 /* If the number of bits in the reference is the same as the bitsize of
4285 the outer type, then the outer type gives the signedness. Otherwise
4286 (in case of a small bitfield) the signedness is unchanged. */
4287 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4288 *punsignedp = TYPE_UNSIGNED (outer_type);
4289
4290 /* Compute the mask to access the bitfield. */
4291 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4292 precision = TYPE_PRECISION (unsigned_type);
4293
4294 mask = build_int_cst_type (unsigned_type, -1);
4295
4296 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4297 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4298
4299 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4300 if (and_mask != 0)
4301 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4302 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4303
4304 *pmask = mask;
4305 *pand_mask = and_mask;
4306 return inner;
4307 }
4308
4309 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4310 bit positions and MASK is SIGNED. */
4311
4312 static int
4313 all_ones_mask_p (const_tree mask, unsigned int size)
4314 {
4315 tree type = TREE_TYPE (mask);
4316 unsigned int precision = TYPE_PRECISION (type);
4317
4318 /* If this function returns true when the type of the mask is
4319 UNSIGNED, then there will be errors. In particular see
4320 gcc.c-torture/execute/990326-1.c. There does not appear to be
4321 any documentation paper trail as to why this is so. But the pre
4322 wide-int worked with that restriction and it has been preserved
4323 here. */
4324 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4325 return false;
4326
4327 return wi::mask (size, false, precision) == wi::to_wide (mask);
4328 }
4329
4330 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4331 represents the sign bit of EXP's type. If EXP represents a sign
4332 or zero extension, also test VAL against the unextended type.
4333 The return value is the (sub)expression whose sign bit is VAL,
4334 or NULL_TREE otherwise. */
4335
4336 tree
4337 sign_bit_p (tree exp, const_tree val)
4338 {
4339 int width;
4340 tree t;
4341
4342 /* Tree EXP must have an integral type. */
4343 t = TREE_TYPE (exp);
4344 if (! INTEGRAL_TYPE_P (t))
4345 return NULL_TREE;
4346
4347 /* Tree VAL must be an integer constant. */
4348 if (TREE_CODE (val) != INTEGER_CST
4349 || TREE_OVERFLOW (val))
4350 return NULL_TREE;
4351
4352 width = TYPE_PRECISION (t);
4353 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4354 return exp;
4355
4356 /* Handle extension from a narrower type. */
4357 if (TREE_CODE (exp) == NOP_EXPR
4358 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4359 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4360
4361 return NULL_TREE;
4362 }
4363
4364 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4365 to be evaluated unconditionally. */
4366
4367 static int
4368 simple_operand_p (const_tree exp)
4369 {
4370 /* Strip any conversions that don't change the machine mode. */
4371 STRIP_NOPS (exp);
4372
4373 return (CONSTANT_CLASS_P (exp)
4374 || TREE_CODE (exp) == SSA_NAME
4375 || (DECL_P (exp)
4376 && ! TREE_ADDRESSABLE (exp)
4377 && ! TREE_THIS_VOLATILE (exp)
4378 && ! DECL_NONLOCAL (exp)
4379 /* Don't regard global variables as simple. They may be
4380 allocated in ways unknown to the compiler (shared memory,
4381 #pragma weak, etc). */
4382 && ! TREE_PUBLIC (exp)
4383 && ! DECL_EXTERNAL (exp)
4384 /* Weakrefs are not safe to be read, since they can be NULL.
4385 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4386 have DECL_WEAK flag set. */
4387 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4388 /* Loading a static variable is unduly expensive, but global
4389 registers aren't expensive. */
4390 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4391 }
4392
4393 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4394 to be evaluated unconditionally.
4395 I addition to simple_operand_p, we assume that comparisons, conversions,
4396 and logic-not operations are simple, if their operands are simple, too. */
4397
4398 static bool
4399 simple_operand_p_2 (tree exp)
4400 {
4401 enum tree_code code;
4402
4403 if (TREE_SIDE_EFFECTS (exp)
4404 || tree_could_trap_p (exp))
4405 return false;
4406
4407 while (CONVERT_EXPR_P (exp))
4408 exp = TREE_OPERAND (exp, 0);
4409
4410 code = TREE_CODE (exp);
4411
4412 if (TREE_CODE_CLASS (code) == tcc_comparison)
4413 return (simple_operand_p (TREE_OPERAND (exp, 0))
4414 && simple_operand_p (TREE_OPERAND (exp, 1)));
4415
4416 if (code == TRUTH_NOT_EXPR)
4417 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4418
4419 return simple_operand_p (exp);
4420 }
4421
4422 \f
4423 /* The following functions are subroutines to fold_range_test and allow it to
4424 try to change a logical combination of comparisons into a range test.
4425
4426 For example, both
4427 X == 2 || X == 3 || X == 4 || X == 5
4428 and
4429 X >= 2 && X <= 5
4430 are converted to
4431 (unsigned) (X - 2) <= 3
4432
4433 We describe each set of comparisons as being either inside or outside
4434 a range, using a variable named like IN_P, and then describe the
4435 range with a lower and upper bound. If one of the bounds is omitted,
4436 it represents either the highest or lowest value of the type.
4437
4438 In the comments below, we represent a range by two numbers in brackets
4439 preceded by a "+" to designate being inside that range, or a "-" to
4440 designate being outside that range, so the condition can be inverted by
4441 flipping the prefix. An omitted bound is represented by a "-". For
4442 example, "- [-, 10]" means being outside the range starting at the lowest
4443 possible value and ending at 10, in other words, being greater than 10.
4444 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4445 always false.
4446
4447 We set up things so that the missing bounds are handled in a consistent
4448 manner so neither a missing bound nor "true" and "false" need to be
4449 handled using a special case. */
4450
4451 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4452 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4453 and UPPER1_P are nonzero if the respective argument is an upper bound
4454 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4455 must be specified for a comparison. ARG1 will be converted to ARG0's
4456 type if both are specified. */
4457
4458 static tree
4459 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4460 tree arg1, int upper1_p)
4461 {
4462 tree tem;
4463 int result;
4464 int sgn0, sgn1;
4465
4466 /* If neither arg represents infinity, do the normal operation.
4467 Else, if not a comparison, return infinity. Else handle the special
4468 comparison rules. Note that most of the cases below won't occur, but
4469 are handled for consistency. */
4470
4471 if (arg0 != 0 && arg1 != 0)
4472 {
4473 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4474 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4475 STRIP_NOPS (tem);
4476 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4477 }
4478
4479 if (TREE_CODE_CLASS (code) != tcc_comparison)
4480 return 0;
4481
4482 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4483 for neither. In real maths, we cannot assume open ended ranges are
4484 the same. But, this is computer arithmetic, where numbers are finite.
4485 We can therefore make the transformation of any unbounded range with
4486 the value Z, Z being greater than any representable number. This permits
4487 us to treat unbounded ranges as equal. */
4488 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4489 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4490 switch (code)
4491 {
4492 case EQ_EXPR:
4493 result = sgn0 == sgn1;
4494 break;
4495 case NE_EXPR:
4496 result = sgn0 != sgn1;
4497 break;
4498 case LT_EXPR:
4499 result = sgn0 < sgn1;
4500 break;
4501 case LE_EXPR:
4502 result = sgn0 <= sgn1;
4503 break;
4504 case GT_EXPR:
4505 result = sgn0 > sgn1;
4506 break;
4507 case GE_EXPR:
4508 result = sgn0 >= sgn1;
4509 break;
4510 default:
4511 gcc_unreachable ();
4512 }
4513
4514 return constant_boolean_node (result, type);
4515 }
4516 \f
4517 /* Helper routine for make_range. Perform one step for it, return
4518 new expression if the loop should continue or NULL_TREE if it should
4519 stop. */
4520
4521 tree
4522 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4523 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4524 bool *strict_overflow_p)
4525 {
4526 tree arg0_type = TREE_TYPE (arg0);
4527 tree n_low, n_high, low = *p_low, high = *p_high;
4528 int in_p = *p_in_p, n_in_p;
4529
4530 switch (code)
4531 {
4532 case TRUTH_NOT_EXPR:
4533 /* We can only do something if the range is testing for zero. */
4534 if (low == NULL_TREE || high == NULL_TREE
4535 || ! integer_zerop (low) || ! integer_zerop (high))
4536 return NULL_TREE;
4537 *p_in_p = ! in_p;
4538 return arg0;
4539
4540 case EQ_EXPR: case NE_EXPR:
4541 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4542 /* We can only do something if the range is testing for zero
4543 and if the second operand is an integer constant. Note that
4544 saying something is "in" the range we make is done by
4545 complementing IN_P since it will set in the initial case of
4546 being not equal to zero; "out" is leaving it alone. */
4547 if (low == NULL_TREE || high == NULL_TREE
4548 || ! integer_zerop (low) || ! integer_zerop (high)
4549 || TREE_CODE (arg1) != INTEGER_CST)
4550 return NULL_TREE;
4551
4552 switch (code)
4553 {
4554 case NE_EXPR: /* - [c, c] */
4555 low = high = arg1;
4556 break;
4557 case EQ_EXPR: /* + [c, c] */
4558 in_p = ! in_p, low = high = arg1;
4559 break;
4560 case GT_EXPR: /* - [-, c] */
4561 low = 0, high = arg1;
4562 break;
4563 case GE_EXPR: /* + [c, -] */
4564 in_p = ! in_p, low = arg1, high = 0;
4565 break;
4566 case LT_EXPR: /* - [c, -] */
4567 low = arg1, high = 0;
4568 break;
4569 case LE_EXPR: /* + [-, c] */
4570 in_p = ! in_p, low = 0, high = arg1;
4571 break;
4572 default:
4573 gcc_unreachable ();
4574 }
4575
4576 /* If this is an unsigned comparison, we also know that EXP is
4577 greater than or equal to zero. We base the range tests we make
4578 on that fact, so we record it here so we can parse existing
4579 range tests. We test arg0_type since often the return type
4580 of, e.g. EQ_EXPR, is boolean. */
4581 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4582 {
4583 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4584 in_p, low, high, 1,
4585 build_int_cst (arg0_type, 0),
4586 NULL_TREE))
4587 return NULL_TREE;
4588
4589 in_p = n_in_p, low = n_low, high = n_high;
4590
4591 /* If the high bound is missing, but we have a nonzero low
4592 bound, reverse the range so it goes from zero to the low bound
4593 minus 1. */
4594 if (high == 0 && low && ! integer_zerop (low))
4595 {
4596 in_p = ! in_p;
4597 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4598 build_int_cst (TREE_TYPE (low), 1), 0);
4599 low = build_int_cst (arg0_type, 0);
4600 }
4601 }
4602
4603 *p_low = low;
4604 *p_high = high;
4605 *p_in_p = in_p;
4606 return arg0;
4607
4608 case NEGATE_EXPR:
4609 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4610 low and high are non-NULL, then normalize will DTRT. */
4611 if (!TYPE_UNSIGNED (arg0_type)
4612 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4613 {
4614 if (low == NULL_TREE)
4615 low = TYPE_MIN_VALUE (arg0_type);
4616 if (high == NULL_TREE)
4617 high = TYPE_MAX_VALUE (arg0_type);
4618 }
4619
4620 /* (-x) IN [a,b] -> x in [-b, -a] */
4621 n_low = range_binop (MINUS_EXPR, exp_type,
4622 build_int_cst (exp_type, 0),
4623 0, high, 1);
4624 n_high = range_binop (MINUS_EXPR, exp_type,
4625 build_int_cst (exp_type, 0),
4626 0, low, 0);
4627 if (n_high != 0 && TREE_OVERFLOW (n_high))
4628 return NULL_TREE;
4629 goto normalize;
4630
4631 case BIT_NOT_EXPR:
4632 /* ~ X -> -X - 1 */
4633 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4634 build_int_cst (exp_type, 1));
4635
4636 case PLUS_EXPR:
4637 case MINUS_EXPR:
4638 if (TREE_CODE (arg1) != INTEGER_CST)
4639 return NULL_TREE;
4640
4641 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4642 move a constant to the other side. */
4643 if (!TYPE_UNSIGNED (arg0_type)
4644 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4645 return NULL_TREE;
4646
4647 /* If EXP is signed, any overflow in the computation is undefined,
4648 so we don't worry about it so long as our computations on
4649 the bounds don't overflow. For unsigned, overflow is defined
4650 and this is exactly the right thing. */
4651 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4652 arg0_type, low, 0, arg1, 0);
4653 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4654 arg0_type, high, 1, arg1, 0);
4655 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4656 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4657 return NULL_TREE;
4658
4659 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4660 *strict_overflow_p = true;
4661
4662 normalize:
4663 /* Check for an unsigned range which has wrapped around the maximum
4664 value thus making n_high < n_low, and normalize it. */
4665 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4666 {
4667 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4668 build_int_cst (TREE_TYPE (n_high), 1), 0);
4669 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4670 build_int_cst (TREE_TYPE (n_low), 1), 0);
4671
4672 /* If the range is of the form +/- [ x+1, x ], we won't
4673 be able to normalize it. But then, it represents the
4674 whole range or the empty set, so make it
4675 +/- [ -, - ]. */
4676 if (tree_int_cst_equal (n_low, low)
4677 && tree_int_cst_equal (n_high, high))
4678 low = high = 0;
4679 else
4680 in_p = ! in_p;
4681 }
4682 else
4683 low = n_low, high = n_high;
4684
4685 *p_low = low;
4686 *p_high = high;
4687 *p_in_p = in_p;
4688 return arg0;
4689
4690 CASE_CONVERT:
4691 case NON_LVALUE_EXPR:
4692 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4693 return NULL_TREE;
4694
4695 if (! INTEGRAL_TYPE_P (arg0_type)
4696 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4697 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4698 return NULL_TREE;
4699
4700 n_low = low, n_high = high;
4701
4702 if (n_low != 0)
4703 n_low = fold_convert_loc (loc, arg0_type, n_low);
4704
4705 if (n_high != 0)
4706 n_high = fold_convert_loc (loc, arg0_type, n_high);
4707
4708 /* If we're converting arg0 from an unsigned type, to exp,
4709 a signed type, we will be doing the comparison as unsigned.
4710 The tests above have already verified that LOW and HIGH
4711 are both positive.
4712
4713 So we have to ensure that we will handle large unsigned
4714 values the same way that the current signed bounds treat
4715 negative values. */
4716
4717 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4718 {
4719 tree high_positive;
4720 tree equiv_type;
4721 /* For fixed-point modes, we need to pass the saturating flag
4722 as the 2nd parameter. */
4723 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4724 equiv_type
4725 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4726 TYPE_SATURATING (arg0_type));
4727 else
4728 equiv_type
4729 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4730
4731 /* A range without an upper bound is, naturally, unbounded.
4732 Since convert would have cropped a very large value, use
4733 the max value for the destination type. */
4734 high_positive
4735 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4736 : TYPE_MAX_VALUE (arg0_type);
4737
4738 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4739 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4740 fold_convert_loc (loc, arg0_type,
4741 high_positive),
4742 build_int_cst (arg0_type, 1));
4743
4744 /* If the low bound is specified, "and" the range with the
4745 range for which the original unsigned value will be
4746 positive. */
4747 if (low != 0)
4748 {
4749 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4750 1, fold_convert_loc (loc, arg0_type,
4751 integer_zero_node),
4752 high_positive))
4753 return NULL_TREE;
4754
4755 in_p = (n_in_p == in_p);
4756 }
4757 else
4758 {
4759 /* Otherwise, "or" the range with the range of the input
4760 that will be interpreted as negative. */
4761 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4762 1, fold_convert_loc (loc, arg0_type,
4763 integer_zero_node),
4764 high_positive))
4765 return NULL_TREE;
4766
4767 in_p = (in_p != n_in_p);
4768 }
4769 }
4770
4771 *p_low = n_low;
4772 *p_high = n_high;
4773 *p_in_p = in_p;
4774 return arg0;
4775
4776 default:
4777 return NULL_TREE;
4778 }
4779 }
4780
4781 /* Given EXP, a logical expression, set the range it is testing into
4782 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4783 actually being tested. *PLOW and *PHIGH will be made of the same
4784 type as the returned expression. If EXP is not a comparison, we
4785 will most likely not be returning a useful value and range. Set
4786 *STRICT_OVERFLOW_P to true if the return value is only valid
4787 because signed overflow is undefined; otherwise, do not change
4788 *STRICT_OVERFLOW_P. */
4789
4790 tree
4791 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4792 bool *strict_overflow_p)
4793 {
4794 enum tree_code code;
4795 tree arg0, arg1 = NULL_TREE;
4796 tree exp_type, nexp;
4797 int in_p;
4798 tree low, high;
4799 location_t loc = EXPR_LOCATION (exp);
4800
4801 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4802 and see if we can refine the range. Some of the cases below may not
4803 happen, but it doesn't seem worth worrying about this. We "continue"
4804 the outer loop when we've changed something; otherwise we "break"
4805 the switch, which will "break" the while. */
4806
4807 in_p = 0;
4808 low = high = build_int_cst (TREE_TYPE (exp), 0);
4809
4810 while (1)
4811 {
4812 code = TREE_CODE (exp);
4813 exp_type = TREE_TYPE (exp);
4814 arg0 = NULL_TREE;
4815
4816 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4817 {
4818 if (TREE_OPERAND_LENGTH (exp) > 0)
4819 arg0 = TREE_OPERAND (exp, 0);
4820 if (TREE_CODE_CLASS (code) == tcc_binary
4821 || TREE_CODE_CLASS (code) == tcc_comparison
4822 || (TREE_CODE_CLASS (code) == tcc_expression
4823 && TREE_OPERAND_LENGTH (exp) > 1))
4824 arg1 = TREE_OPERAND (exp, 1);
4825 }
4826 if (arg0 == NULL_TREE)
4827 break;
4828
4829 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4830 &high, &in_p, strict_overflow_p);
4831 if (nexp == NULL_TREE)
4832 break;
4833 exp = nexp;
4834 }
4835
4836 /* If EXP is a constant, we can evaluate whether this is true or false. */
4837 if (TREE_CODE (exp) == INTEGER_CST)
4838 {
4839 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4840 exp, 0, low, 0))
4841 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4842 exp, 1, high, 1)));
4843 low = high = 0;
4844 exp = 0;
4845 }
4846
4847 *pin_p = in_p, *plow = low, *phigh = high;
4848 return exp;
4849 }
4850
4851 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4852 a bitwise check i.e. when
4853 LOW == 0xXX...X00...0
4854 HIGH == 0xXX...X11...1
4855 Return corresponding mask in MASK and stem in VALUE. */
4856
4857 static bool
4858 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4859 tree *value)
4860 {
4861 if (TREE_CODE (low) != INTEGER_CST
4862 || TREE_CODE (high) != INTEGER_CST)
4863 return false;
4864
4865 unsigned prec = TYPE_PRECISION (type);
4866 wide_int lo = wi::to_wide (low, prec);
4867 wide_int hi = wi::to_wide (high, prec);
4868
4869 wide_int end_mask = lo ^ hi;
4870 if ((end_mask & (end_mask + 1)) != 0
4871 || (lo & end_mask) != 0)
4872 return false;
4873
4874 wide_int stem_mask = ~end_mask;
4875 wide_int stem = lo & stem_mask;
4876 if (stem != (hi & stem_mask))
4877 return false;
4878
4879 *mask = wide_int_to_tree (type, stem_mask);
4880 *value = wide_int_to_tree (type, stem);
4881
4882 return true;
4883 }
4884 \f
4885 /* Helper routine for build_range_check and match.pd. Return the type to
4886 perform the check or NULL if it shouldn't be optimized. */
4887
4888 tree
4889 range_check_type (tree etype)
4890 {
4891 /* First make sure that arithmetics in this type is valid, then make sure
4892 that it wraps around. */
4893 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4894 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4895 TYPE_UNSIGNED (etype));
4896
4897 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4898 {
4899 tree utype, minv, maxv;
4900
4901 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4902 for the type in question, as we rely on this here. */
4903 utype = unsigned_type_for (etype);
4904 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4905 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4906 build_int_cst (TREE_TYPE (maxv), 1), 1);
4907 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4908
4909 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4910 minv, 1, maxv, 1)))
4911 etype = utype;
4912 else
4913 return NULL_TREE;
4914 }
4915 return etype;
4916 }
4917
4918 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4919 type, TYPE, return an expression to test if EXP is in (or out of, depending
4920 on IN_P) the range. Return 0 if the test couldn't be created. */
4921
4922 tree
4923 build_range_check (location_t loc, tree type, tree exp, int in_p,
4924 tree low, tree high)
4925 {
4926 tree etype = TREE_TYPE (exp), mask, value;
4927
4928 /* Disable this optimization for function pointer expressions
4929 on targets that require function pointer canonicalization. */
4930 if (targetm.have_canonicalize_funcptr_for_compare ()
4931 && TREE_CODE (etype) == POINTER_TYPE
4932 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4933 return NULL_TREE;
4934
4935 if (! in_p)
4936 {
4937 value = build_range_check (loc, type, exp, 1, low, high);
4938 if (value != 0)
4939 return invert_truthvalue_loc (loc, value);
4940
4941 return 0;
4942 }
4943
4944 if (low == 0 && high == 0)
4945 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4946
4947 if (low == 0)
4948 return fold_build2_loc (loc, LE_EXPR, type, exp,
4949 fold_convert_loc (loc, etype, high));
4950
4951 if (high == 0)
4952 return fold_build2_loc (loc, GE_EXPR, type, exp,
4953 fold_convert_loc (loc, etype, low));
4954
4955 if (operand_equal_p (low, high, 0))
4956 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4957 fold_convert_loc (loc, etype, low));
4958
4959 if (TREE_CODE (exp) == BIT_AND_EXPR
4960 && maskable_range_p (low, high, etype, &mask, &value))
4961 return fold_build2_loc (loc, EQ_EXPR, type,
4962 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4963 exp, mask),
4964 value);
4965
4966 if (integer_zerop (low))
4967 {
4968 if (! TYPE_UNSIGNED (etype))
4969 {
4970 etype = unsigned_type_for (etype);
4971 high = fold_convert_loc (loc, etype, high);
4972 exp = fold_convert_loc (loc, etype, exp);
4973 }
4974 return build_range_check (loc, type, exp, 1, 0, high);
4975 }
4976
4977 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4978 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4979 {
4980 int prec = TYPE_PRECISION (etype);
4981
4982 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4983 {
4984 if (TYPE_UNSIGNED (etype))
4985 {
4986 tree signed_etype = signed_type_for (etype);
4987 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4988 etype
4989 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4990 else
4991 etype = signed_etype;
4992 exp = fold_convert_loc (loc, etype, exp);
4993 }
4994 return fold_build2_loc (loc, GT_EXPR, type, exp,
4995 build_int_cst (etype, 0));
4996 }
4997 }
4998
4999 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5000 This requires wrap-around arithmetics for the type of the expression. */
5001 etype = range_check_type (etype);
5002 if (etype == NULL_TREE)
5003 return NULL_TREE;
5004
5005 if (POINTER_TYPE_P (etype))
5006 etype = unsigned_type_for (etype);
5007
5008 high = fold_convert_loc (loc, etype, high);
5009 low = fold_convert_loc (loc, etype, low);
5010 exp = fold_convert_loc (loc, etype, exp);
5011
5012 value = const_binop (MINUS_EXPR, high, low);
5013
5014 if (value != 0 && !TREE_OVERFLOW (value))
5015 return build_range_check (loc, type,
5016 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5017 1, build_int_cst (etype, 0), value);
5018
5019 return 0;
5020 }
5021 \f
5022 /* Return the predecessor of VAL in its type, handling the infinite case. */
5023
5024 static tree
5025 range_predecessor (tree val)
5026 {
5027 tree type = TREE_TYPE (val);
5028
5029 if (INTEGRAL_TYPE_P (type)
5030 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5031 return 0;
5032 else
5033 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5034 build_int_cst (TREE_TYPE (val), 1), 0);
5035 }
5036
5037 /* Return the successor of VAL in its type, handling the infinite case. */
5038
5039 static tree
5040 range_successor (tree val)
5041 {
5042 tree type = TREE_TYPE (val);
5043
5044 if (INTEGRAL_TYPE_P (type)
5045 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5046 return 0;
5047 else
5048 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5049 build_int_cst (TREE_TYPE (val), 1), 0);
5050 }
5051
5052 /* Given two ranges, see if we can merge them into one. Return 1 if we
5053 can, 0 if we can't. Set the output range into the specified parameters. */
5054
5055 bool
5056 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5057 tree high0, int in1_p, tree low1, tree high1)
5058 {
5059 int no_overlap;
5060 int subset;
5061 int temp;
5062 tree tem;
5063 int in_p;
5064 tree low, high;
5065 int lowequal = ((low0 == 0 && low1 == 0)
5066 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5067 low0, 0, low1, 0)));
5068 int highequal = ((high0 == 0 && high1 == 0)
5069 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5070 high0, 1, high1, 1)));
5071
5072 /* Make range 0 be the range that starts first, or ends last if they
5073 start at the same value. Swap them if it isn't. */
5074 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5075 low0, 0, low1, 0))
5076 || (lowequal
5077 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5078 high1, 1, high0, 1))))
5079 {
5080 temp = in0_p, in0_p = in1_p, in1_p = temp;
5081 tem = low0, low0 = low1, low1 = tem;
5082 tem = high0, high0 = high1, high1 = tem;
5083 }
5084
5085 /* Now flag two cases, whether the ranges are disjoint or whether the
5086 second range is totally subsumed in the first. Note that the tests
5087 below are simplified by the ones above. */
5088 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5089 high0, 1, low1, 0));
5090 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5091 high1, 1, high0, 1));
5092
5093 /* We now have four cases, depending on whether we are including or
5094 excluding the two ranges. */
5095 if (in0_p && in1_p)
5096 {
5097 /* If they don't overlap, the result is false. If the second range
5098 is a subset it is the result. Otherwise, the range is from the start
5099 of the second to the end of the first. */
5100 if (no_overlap)
5101 in_p = 0, low = high = 0;
5102 else if (subset)
5103 in_p = 1, low = low1, high = high1;
5104 else
5105 in_p = 1, low = low1, high = high0;
5106 }
5107
5108 else if (in0_p && ! in1_p)
5109 {
5110 /* If they don't overlap, the result is the first range. If they are
5111 equal, the result is false. If the second range is a subset of the
5112 first, and the ranges begin at the same place, we go from just after
5113 the end of the second range to the end of the first. If the second
5114 range is not a subset of the first, or if it is a subset and both
5115 ranges end at the same place, the range starts at the start of the
5116 first range and ends just before the second range.
5117 Otherwise, we can't describe this as a single range. */
5118 if (no_overlap)
5119 in_p = 1, low = low0, high = high0;
5120 else if (lowequal && highequal)
5121 in_p = 0, low = high = 0;
5122 else if (subset && lowequal)
5123 {
5124 low = range_successor (high1);
5125 high = high0;
5126 in_p = 1;
5127 if (low == 0)
5128 {
5129 /* We are in the weird situation where high0 > high1 but
5130 high1 has no successor. Punt. */
5131 return 0;
5132 }
5133 }
5134 else if (! subset || highequal)
5135 {
5136 low = low0;
5137 high = range_predecessor (low1);
5138 in_p = 1;
5139 if (high == 0)
5140 {
5141 /* low0 < low1 but low1 has no predecessor. Punt. */
5142 return 0;
5143 }
5144 }
5145 else
5146 return 0;
5147 }
5148
5149 else if (! in0_p && in1_p)
5150 {
5151 /* If they don't overlap, the result is the second range. If the second
5152 is a subset of the first, the result is false. Otherwise,
5153 the range starts just after the first range and ends at the
5154 end of the second. */
5155 if (no_overlap)
5156 in_p = 1, low = low1, high = high1;
5157 else if (subset || highequal)
5158 in_p = 0, low = high = 0;
5159 else
5160 {
5161 low = range_successor (high0);
5162 high = high1;
5163 in_p = 1;
5164 if (low == 0)
5165 {
5166 /* high1 > high0 but high0 has no successor. Punt. */
5167 return 0;
5168 }
5169 }
5170 }
5171
5172 else
5173 {
5174 /* The case where we are excluding both ranges. Here the complex case
5175 is if they don't overlap. In that case, the only time we have a
5176 range is if they are adjacent. If the second is a subset of the
5177 first, the result is the first. Otherwise, the range to exclude
5178 starts at the beginning of the first range and ends at the end of the
5179 second. */
5180 if (no_overlap)
5181 {
5182 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5183 range_successor (high0),
5184 1, low1, 0)))
5185 in_p = 0, low = low0, high = high1;
5186 else
5187 {
5188 /* Canonicalize - [min, x] into - [-, x]. */
5189 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5190 switch (TREE_CODE (TREE_TYPE (low0)))
5191 {
5192 case ENUMERAL_TYPE:
5193 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5194 GET_MODE_BITSIZE
5195 (TYPE_MODE (TREE_TYPE (low0)))))
5196 break;
5197 /* FALLTHROUGH */
5198 case INTEGER_TYPE:
5199 if (tree_int_cst_equal (low0,
5200 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5201 low0 = 0;
5202 break;
5203 case POINTER_TYPE:
5204 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5205 && integer_zerop (low0))
5206 low0 = 0;
5207 break;
5208 default:
5209 break;
5210 }
5211
5212 /* Canonicalize - [x, max] into - [x, -]. */
5213 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5214 switch (TREE_CODE (TREE_TYPE (high1)))
5215 {
5216 case ENUMERAL_TYPE:
5217 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5218 GET_MODE_BITSIZE
5219 (TYPE_MODE (TREE_TYPE (high1)))))
5220 break;
5221 /* FALLTHROUGH */
5222 case INTEGER_TYPE:
5223 if (tree_int_cst_equal (high1,
5224 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5225 high1 = 0;
5226 break;
5227 case POINTER_TYPE:
5228 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5229 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5230 high1, 1,
5231 build_int_cst (TREE_TYPE (high1), 1),
5232 1)))
5233 high1 = 0;
5234 break;
5235 default:
5236 break;
5237 }
5238
5239 /* The ranges might be also adjacent between the maximum and
5240 minimum values of the given type. For
5241 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5242 return + [x + 1, y - 1]. */
5243 if (low0 == 0 && high1 == 0)
5244 {
5245 low = range_successor (high0);
5246 high = range_predecessor (low1);
5247 if (low == 0 || high == 0)
5248 return 0;
5249
5250 in_p = 1;
5251 }
5252 else
5253 return 0;
5254 }
5255 }
5256 else if (subset)
5257 in_p = 0, low = low0, high = high0;
5258 else
5259 in_p = 0, low = low0, high = high1;
5260 }
5261
5262 *pin_p = in_p, *plow = low, *phigh = high;
5263 return 1;
5264 }
5265 \f
5266
5267 /* Subroutine of fold, looking inside expressions of the form
5268 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5269 of the COND_EXPR. This function is being used also to optimize
5270 A op B ? C : A, by reversing the comparison first.
5271
5272 Return a folded expression whose code is not a COND_EXPR
5273 anymore, or NULL_TREE if no folding opportunity is found. */
5274
5275 static tree
5276 fold_cond_expr_with_comparison (location_t loc, tree type,
5277 tree arg0, tree arg1, tree arg2)
5278 {
5279 enum tree_code comp_code = TREE_CODE (arg0);
5280 tree arg00 = TREE_OPERAND (arg0, 0);
5281 tree arg01 = TREE_OPERAND (arg0, 1);
5282 tree arg1_type = TREE_TYPE (arg1);
5283 tree tem;
5284
5285 STRIP_NOPS (arg1);
5286 STRIP_NOPS (arg2);
5287
5288 /* If we have A op 0 ? A : -A, consider applying the following
5289 transformations:
5290
5291 A == 0? A : -A same as -A
5292 A != 0? A : -A same as A
5293 A >= 0? A : -A same as abs (A)
5294 A > 0? A : -A same as abs (A)
5295 A <= 0? A : -A same as -abs (A)
5296 A < 0? A : -A same as -abs (A)
5297
5298 None of these transformations work for modes with signed
5299 zeros. If A is +/-0, the first two transformations will
5300 change the sign of the result (from +0 to -0, or vice
5301 versa). The last four will fix the sign of the result,
5302 even though the original expressions could be positive or
5303 negative, depending on the sign of A.
5304
5305 Note that all these transformations are correct if A is
5306 NaN, since the two alternatives (A and -A) are also NaNs. */
5307 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5308 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5309 ? real_zerop (arg01)
5310 : integer_zerop (arg01))
5311 && ((TREE_CODE (arg2) == NEGATE_EXPR
5312 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5313 /* In the case that A is of the form X-Y, '-A' (arg2) may
5314 have already been folded to Y-X, check for that. */
5315 || (TREE_CODE (arg1) == MINUS_EXPR
5316 && TREE_CODE (arg2) == MINUS_EXPR
5317 && operand_equal_p (TREE_OPERAND (arg1, 0),
5318 TREE_OPERAND (arg2, 1), 0)
5319 && operand_equal_p (TREE_OPERAND (arg1, 1),
5320 TREE_OPERAND (arg2, 0), 0))))
5321 switch (comp_code)
5322 {
5323 case EQ_EXPR:
5324 case UNEQ_EXPR:
5325 tem = fold_convert_loc (loc, arg1_type, arg1);
5326 return fold_convert_loc (loc, type, negate_expr (tem));
5327 case NE_EXPR:
5328 case LTGT_EXPR:
5329 return fold_convert_loc (loc, type, arg1);
5330 case UNGE_EXPR:
5331 case UNGT_EXPR:
5332 if (flag_trapping_math)
5333 break;
5334 /* Fall through. */
5335 case GE_EXPR:
5336 case GT_EXPR:
5337 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5338 break;
5339 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5340 return fold_convert_loc (loc, type, tem);
5341 case UNLE_EXPR:
5342 case UNLT_EXPR:
5343 if (flag_trapping_math)
5344 break;
5345 /* FALLTHRU */
5346 case LE_EXPR:
5347 case LT_EXPR:
5348 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5349 break;
5350 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5351 return negate_expr (fold_convert_loc (loc, type, tem));
5352 default:
5353 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5354 break;
5355 }
5356
5357 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5358 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5359 both transformations are correct when A is NaN: A != 0
5360 is then true, and A == 0 is false. */
5361
5362 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5363 && integer_zerop (arg01) && integer_zerop (arg2))
5364 {
5365 if (comp_code == NE_EXPR)
5366 return fold_convert_loc (loc, type, arg1);
5367 else if (comp_code == EQ_EXPR)
5368 return build_zero_cst (type);
5369 }
5370
5371 /* Try some transformations of A op B ? A : B.
5372
5373 A == B? A : B same as B
5374 A != B? A : B same as A
5375 A >= B? A : B same as max (A, B)
5376 A > B? A : B same as max (B, A)
5377 A <= B? A : B same as min (A, B)
5378 A < B? A : B same as min (B, A)
5379
5380 As above, these transformations don't work in the presence
5381 of signed zeros. For example, if A and B are zeros of
5382 opposite sign, the first two transformations will change
5383 the sign of the result. In the last four, the original
5384 expressions give different results for (A=+0, B=-0) and
5385 (A=-0, B=+0), but the transformed expressions do not.
5386
5387 The first two transformations are correct if either A or B
5388 is a NaN. In the first transformation, the condition will
5389 be false, and B will indeed be chosen. In the case of the
5390 second transformation, the condition A != B will be true,
5391 and A will be chosen.
5392
5393 The conversions to max() and min() are not correct if B is
5394 a number and A is not. The conditions in the original
5395 expressions will be false, so all four give B. The min()
5396 and max() versions would give a NaN instead. */
5397 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5398 && operand_equal_for_comparison_p (arg01, arg2)
5399 /* Avoid these transformations if the COND_EXPR may be used
5400 as an lvalue in the C++ front-end. PR c++/19199. */
5401 && (in_gimple_form
5402 || VECTOR_TYPE_P (type)
5403 || (! lang_GNU_CXX ()
5404 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5405 || ! maybe_lvalue_p (arg1)
5406 || ! maybe_lvalue_p (arg2)))
5407 {
5408 tree comp_op0 = arg00;
5409 tree comp_op1 = arg01;
5410 tree comp_type = TREE_TYPE (comp_op0);
5411
5412 switch (comp_code)
5413 {
5414 case EQ_EXPR:
5415 return fold_convert_loc (loc, type, arg2);
5416 case NE_EXPR:
5417 return fold_convert_loc (loc, type, arg1);
5418 case LE_EXPR:
5419 case LT_EXPR:
5420 case UNLE_EXPR:
5421 case UNLT_EXPR:
5422 /* In C++ a ?: expression can be an lvalue, so put the
5423 operand which will be used if they are equal first
5424 so that we can convert this back to the
5425 corresponding COND_EXPR. */
5426 if (!HONOR_NANS (arg1))
5427 {
5428 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5429 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5430 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5431 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5432 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5433 comp_op1, comp_op0);
5434 return fold_convert_loc (loc, type, tem);
5435 }
5436 break;
5437 case GE_EXPR:
5438 case GT_EXPR:
5439 case UNGE_EXPR:
5440 case UNGT_EXPR:
5441 if (!HONOR_NANS (arg1))
5442 {
5443 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5444 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5445 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5446 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5447 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5448 comp_op1, comp_op0);
5449 return fold_convert_loc (loc, type, tem);
5450 }
5451 break;
5452 case UNEQ_EXPR:
5453 if (!HONOR_NANS (arg1))
5454 return fold_convert_loc (loc, type, arg2);
5455 break;
5456 case LTGT_EXPR:
5457 if (!HONOR_NANS (arg1))
5458 return fold_convert_loc (loc, type, arg1);
5459 break;
5460 default:
5461 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5462 break;
5463 }
5464 }
5465
5466 return NULL_TREE;
5467 }
5468
5469
5470 \f
5471 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5472 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5473 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5474 false) >= 2)
5475 #endif
5476
5477 /* EXP is some logical combination of boolean tests. See if we can
5478 merge it into some range test. Return the new tree if so. */
5479
5480 static tree
5481 fold_range_test (location_t loc, enum tree_code code, tree type,
5482 tree op0, tree op1)
5483 {
5484 int or_op = (code == TRUTH_ORIF_EXPR
5485 || code == TRUTH_OR_EXPR);
5486 int in0_p, in1_p, in_p;
5487 tree low0, low1, low, high0, high1, high;
5488 bool strict_overflow_p = false;
5489 tree tem, lhs, rhs;
5490 const char * const warnmsg = G_("assuming signed overflow does not occur "
5491 "when simplifying range test");
5492
5493 if (!INTEGRAL_TYPE_P (type))
5494 return 0;
5495
5496 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5497 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5498
5499 /* If this is an OR operation, invert both sides; we will invert
5500 again at the end. */
5501 if (or_op)
5502 in0_p = ! in0_p, in1_p = ! in1_p;
5503
5504 /* If both expressions are the same, if we can merge the ranges, and we
5505 can build the range test, return it or it inverted. If one of the
5506 ranges is always true or always false, consider it to be the same
5507 expression as the other. */
5508 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5509 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5510 in1_p, low1, high1)
5511 && (tem = (build_range_check (loc, type,
5512 lhs != 0 ? lhs
5513 : rhs != 0 ? rhs : integer_zero_node,
5514 in_p, low, high))) != 0)
5515 {
5516 if (strict_overflow_p)
5517 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5518 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5519 }
5520
5521 /* On machines where the branch cost is expensive, if this is a
5522 short-circuited branch and the underlying object on both sides
5523 is the same, make a non-short-circuit operation. */
5524 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5525 && !flag_sanitize_coverage
5526 && lhs != 0 && rhs != 0
5527 && (code == TRUTH_ANDIF_EXPR
5528 || code == TRUTH_ORIF_EXPR)
5529 && operand_equal_p (lhs, rhs, 0))
5530 {
5531 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5532 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5533 which cases we can't do this. */
5534 if (simple_operand_p (lhs))
5535 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5536 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5537 type, op0, op1);
5538
5539 else if (!lang_hooks.decls.global_bindings_p ()
5540 && !CONTAINS_PLACEHOLDER_P (lhs))
5541 {
5542 tree common = save_expr (lhs);
5543
5544 if ((lhs = build_range_check (loc, type, common,
5545 or_op ? ! in0_p : in0_p,
5546 low0, high0)) != 0
5547 && (rhs = build_range_check (loc, type, common,
5548 or_op ? ! in1_p : in1_p,
5549 low1, high1)) != 0)
5550 {
5551 if (strict_overflow_p)
5552 fold_overflow_warning (warnmsg,
5553 WARN_STRICT_OVERFLOW_COMPARISON);
5554 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5555 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5556 type, lhs, rhs);
5557 }
5558 }
5559 }
5560
5561 return 0;
5562 }
5563 \f
5564 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5565 bit value. Arrange things so the extra bits will be set to zero if and
5566 only if C is signed-extended to its full width. If MASK is nonzero,
5567 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5568
5569 static tree
5570 unextend (tree c, int p, int unsignedp, tree mask)
5571 {
5572 tree type = TREE_TYPE (c);
5573 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5574 tree temp;
5575
5576 if (p == modesize || unsignedp)
5577 return c;
5578
5579 /* We work by getting just the sign bit into the low-order bit, then
5580 into the high-order bit, then sign-extend. We then XOR that value
5581 with C. */
5582 temp = build_int_cst (TREE_TYPE (c),
5583 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5584
5585 /* We must use a signed type in order to get an arithmetic right shift.
5586 However, we must also avoid introducing accidental overflows, so that
5587 a subsequent call to integer_zerop will work. Hence we must
5588 do the type conversion here. At this point, the constant is either
5589 zero or one, and the conversion to a signed type can never overflow.
5590 We could get an overflow if this conversion is done anywhere else. */
5591 if (TYPE_UNSIGNED (type))
5592 temp = fold_convert (signed_type_for (type), temp);
5593
5594 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5595 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5596 if (mask != 0)
5597 temp = const_binop (BIT_AND_EXPR, temp,
5598 fold_convert (TREE_TYPE (c), mask));
5599 /* If necessary, convert the type back to match the type of C. */
5600 if (TYPE_UNSIGNED (type))
5601 temp = fold_convert (type, temp);
5602
5603 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5604 }
5605 \f
5606 /* For an expression that has the form
5607 (A && B) || ~B
5608 or
5609 (A || B) && ~B,
5610 we can drop one of the inner expressions and simplify to
5611 A || ~B
5612 or
5613 A && ~B
5614 LOC is the location of the resulting expression. OP is the inner
5615 logical operation; the left-hand side in the examples above, while CMPOP
5616 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5617 removing a condition that guards another, as in
5618 (A != NULL && A->...) || A == NULL
5619 which we must not transform. If RHS_ONLY is true, only eliminate the
5620 right-most operand of the inner logical operation. */
5621
5622 static tree
5623 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5624 bool rhs_only)
5625 {
5626 tree type = TREE_TYPE (cmpop);
5627 enum tree_code code = TREE_CODE (cmpop);
5628 enum tree_code truthop_code = TREE_CODE (op);
5629 tree lhs = TREE_OPERAND (op, 0);
5630 tree rhs = TREE_OPERAND (op, 1);
5631 tree orig_lhs = lhs, orig_rhs = rhs;
5632 enum tree_code rhs_code = TREE_CODE (rhs);
5633 enum tree_code lhs_code = TREE_CODE (lhs);
5634 enum tree_code inv_code;
5635
5636 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5637 return NULL_TREE;
5638
5639 if (TREE_CODE_CLASS (code) != tcc_comparison)
5640 return NULL_TREE;
5641
5642 if (rhs_code == truthop_code)
5643 {
5644 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5645 if (newrhs != NULL_TREE)
5646 {
5647 rhs = newrhs;
5648 rhs_code = TREE_CODE (rhs);
5649 }
5650 }
5651 if (lhs_code == truthop_code && !rhs_only)
5652 {
5653 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5654 if (newlhs != NULL_TREE)
5655 {
5656 lhs = newlhs;
5657 lhs_code = TREE_CODE (lhs);
5658 }
5659 }
5660
5661 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5662 if (inv_code == rhs_code
5663 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5664 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5665 return lhs;
5666 if (!rhs_only && inv_code == lhs_code
5667 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5668 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5669 return rhs;
5670 if (rhs != orig_rhs || lhs != orig_lhs)
5671 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5672 lhs, rhs);
5673 return NULL_TREE;
5674 }
5675
5676 /* Find ways of folding logical expressions of LHS and RHS:
5677 Try to merge two comparisons to the same innermost item.
5678 Look for range tests like "ch >= '0' && ch <= '9'".
5679 Look for combinations of simple terms on machines with expensive branches
5680 and evaluate the RHS unconditionally.
5681
5682 For example, if we have p->a == 2 && p->b == 4 and we can make an
5683 object large enough to span both A and B, we can do this with a comparison
5684 against the object ANDed with the a mask.
5685
5686 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5687 operations to do this with one comparison.
5688
5689 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5690 function and the one above.
5691
5692 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5693 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5694
5695 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5696 two operands.
5697
5698 We return the simplified tree or 0 if no optimization is possible. */
5699
5700 static tree
5701 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5702 tree lhs, tree rhs)
5703 {
5704 /* If this is the "or" of two comparisons, we can do something if
5705 the comparisons are NE_EXPR. If this is the "and", we can do something
5706 if the comparisons are EQ_EXPR. I.e.,
5707 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5708
5709 WANTED_CODE is this operation code. For single bit fields, we can
5710 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5711 comparison for one-bit fields. */
5712
5713 enum tree_code wanted_code;
5714 enum tree_code lcode, rcode;
5715 tree ll_arg, lr_arg, rl_arg, rr_arg;
5716 tree ll_inner, lr_inner, rl_inner, rr_inner;
5717 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5718 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5719 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5720 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5721 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5722 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5723 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5724 scalar_int_mode lnmode, rnmode;
5725 tree ll_mask, lr_mask, rl_mask, rr_mask;
5726 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5727 tree l_const, r_const;
5728 tree lntype, rntype, result;
5729 HOST_WIDE_INT first_bit, end_bit;
5730 int volatilep;
5731
5732 /* Start by getting the comparison codes. Fail if anything is volatile.
5733 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5734 it were surrounded with a NE_EXPR. */
5735
5736 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5737 return 0;
5738
5739 lcode = TREE_CODE (lhs);
5740 rcode = TREE_CODE (rhs);
5741
5742 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5743 {
5744 lhs = build2 (NE_EXPR, truth_type, lhs,
5745 build_int_cst (TREE_TYPE (lhs), 0));
5746 lcode = NE_EXPR;
5747 }
5748
5749 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5750 {
5751 rhs = build2 (NE_EXPR, truth_type, rhs,
5752 build_int_cst (TREE_TYPE (rhs), 0));
5753 rcode = NE_EXPR;
5754 }
5755
5756 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5757 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5758 return 0;
5759
5760 ll_arg = TREE_OPERAND (lhs, 0);
5761 lr_arg = TREE_OPERAND (lhs, 1);
5762 rl_arg = TREE_OPERAND (rhs, 0);
5763 rr_arg = TREE_OPERAND (rhs, 1);
5764
5765 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5766 if (simple_operand_p (ll_arg)
5767 && simple_operand_p (lr_arg))
5768 {
5769 if (operand_equal_p (ll_arg, rl_arg, 0)
5770 && operand_equal_p (lr_arg, rr_arg, 0))
5771 {
5772 result = combine_comparisons (loc, code, lcode, rcode,
5773 truth_type, ll_arg, lr_arg);
5774 if (result)
5775 return result;
5776 }
5777 else if (operand_equal_p (ll_arg, rr_arg, 0)
5778 && operand_equal_p (lr_arg, rl_arg, 0))
5779 {
5780 result = combine_comparisons (loc, code, lcode,
5781 swap_tree_comparison (rcode),
5782 truth_type, ll_arg, lr_arg);
5783 if (result)
5784 return result;
5785 }
5786 }
5787
5788 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5789 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5790
5791 /* If the RHS can be evaluated unconditionally and its operands are
5792 simple, it wins to evaluate the RHS unconditionally on machines
5793 with expensive branches. In this case, this isn't a comparison
5794 that can be merged. */
5795
5796 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5797 false) >= 2
5798 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5799 && simple_operand_p (rl_arg)
5800 && simple_operand_p (rr_arg))
5801 {
5802 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5803 if (code == TRUTH_OR_EXPR
5804 && lcode == NE_EXPR && integer_zerop (lr_arg)
5805 && rcode == NE_EXPR && integer_zerop (rr_arg)
5806 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5807 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5808 return build2_loc (loc, NE_EXPR, truth_type,
5809 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5810 ll_arg, rl_arg),
5811 build_int_cst (TREE_TYPE (ll_arg), 0));
5812
5813 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5814 if (code == TRUTH_AND_EXPR
5815 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5816 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5817 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5818 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5819 return build2_loc (loc, EQ_EXPR, truth_type,
5820 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5821 ll_arg, rl_arg),
5822 build_int_cst (TREE_TYPE (ll_arg), 0));
5823 }
5824
5825 /* See if the comparisons can be merged. Then get all the parameters for
5826 each side. */
5827
5828 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5829 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5830 return 0;
5831
5832 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5833 volatilep = 0;
5834 ll_inner = decode_field_reference (loc, &ll_arg,
5835 &ll_bitsize, &ll_bitpos, &ll_mode,
5836 &ll_unsignedp, &ll_reversep, &volatilep,
5837 &ll_mask, &ll_and_mask);
5838 lr_inner = decode_field_reference (loc, &lr_arg,
5839 &lr_bitsize, &lr_bitpos, &lr_mode,
5840 &lr_unsignedp, &lr_reversep, &volatilep,
5841 &lr_mask, &lr_and_mask);
5842 rl_inner = decode_field_reference (loc, &rl_arg,
5843 &rl_bitsize, &rl_bitpos, &rl_mode,
5844 &rl_unsignedp, &rl_reversep, &volatilep,
5845 &rl_mask, &rl_and_mask);
5846 rr_inner = decode_field_reference (loc, &rr_arg,
5847 &rr_bitsize, &rr_bitpos, &rr_mode,
5848 &rr_unsignedp, &rr_reversep, &volatilep,
5849 &rr_mask, &rr_and_mask);
5850
5851 /* It must be true that the inner operation on the lhs of each
5852 comparison must be the same if we are to be able to do anything.
5853 Then see if we have constants. If not, the same must be true for
5854 the rhs's. */
5855 if (volatilep
5856 || ll_reversep != rl_reversep
5857 || ll_inner == 0 || rl_inner == 0
5858 || ! operand_equal_p (ll_inner, rl_inner, 0))
5859 return 0;
5860
5861 if (TREE_CODE (lr_arg) == INTEGER_CST
5862 && TREE_CODE (rr_arg) == INTEGER_CST)
5863 {
5864 l_const = lr_arg, r_const = rr_arg;
5865 lr_reversep = ll_reversep;
5866 }
5867 else if (lr_reversep != rr_reversep
5868 || lr_inner == 0 || rr_inner == 0
5869 || ! operand_equal_p (lr_inner, rr_inner, 0))
5870 return 0;
5871 else
5872 l_const = r_const = 0;
5873
5874 /* If either comparison code is not correct for our logical operation,
5875 fail. However, we can convert a one-bit comparison against zero into
5876 the opposite comparison against that bit being set in the field. */
5877
5878 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5879 if (lcode != wanted_code)
5880 {
5881 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5882 {
5883 /* Make the left operand unsigned, since we are only interested
5884 in the value of one bit. Otherwise we are doing the wrong
5885 thing below. */
5886 ll_unsignedp = 1;
5887 l_const = ll_mask;
5888 }
5889 else
5890 return 0;
5891 }
5892
5893 /* This is analogous to the code for l_const above. */
5894 if (rcode != wanted_code)
5895 {
5896 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5897 {
5898 rl_unsignedp = 1;
5899 r_const = rl_mask;
5900 }
5901 else
5902 return 0;
5903 }
5904
5905 /* See if we can find a mode that contains both fields being compared on
5906 the left. If we can't, fail. Otherwise, update all constants and masks
5907 to be relative to a field of that size. */
5908 first_bit = MIN (ll_bitpos, rl_bitpos);
5909 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5910 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5911 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5912 volatilep, &lnmode))
5913 return 0;
5914
5915 lnbitsize = GET_MODE_BITSIZE (lnmode);
5916 lnbitpos = first_bit & ~ (lnbitsize - 1);
5917 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5918 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5919
5920 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5921 {
5922 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5923 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5924 }
5925
5926 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5927 size_int (xll_bitpos));
5928 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5929 size_int (xrl_bitpos));
5930
5931 if (l_const)
5932 {
5933 l_const = fold_convert_loc (loc, lntype, l_const);
5934 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5935 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5936 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5937 fold_build1_loc (loc, BIT_NOT_EXPR,
5938 lntype, ll_mask))))
5939 {
5940 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5941
5942 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5943 }
5944 }
5945 if (r_const)
5946 {
5947 r_const = fold_convert_loc (loc, lntype, r_const);
5948 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5949 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5950 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5951 fold_build1_loc (loc, BIT_NOT_EXPR,
5952 lntype, rl_mask))))
5953 {
5954 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5955
5956 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5957 }
5958 }
5959
5960 /* If the right sides are not constant, do the same for it. Also,
5961 disallow this optimization if a size or signedness mismatch occurs
5962 between the left and right sides. */
5963 if (l_const == 0)
5964 {
5965 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5966 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5967 /* Make sure the two fields on the right
5968 correspond to the left without being swapped. */
5969 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5970 return 0;
5971
5972 first_bit = MIN (lr_bitpos, rr_bitpos);
5973 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5974 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5975 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5976 volatilep, &rnmode))
5977 return 0;
5978
5979 rnbitsize = GET_MODE_BITSIZE (rnmode);
5980 rnbitpos = first_bit & ~ (rnbitsize - 1);
5981 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5982 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5983
5984 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5985 {
5986 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5987 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5988 }
5989
5990 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5991 rntype, lr_mask),
5992 size_int (xlr_bitpos));
5993 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5994 rntype, rr_mask),
5995 size_int (xrr_bitpos));
5996
5997 /* Make a mask that corresponds to both fields being compared.
5998 Do this for both items being compared. If the operands are the
5999 same size and the bits being compared are in the same position
6000 then we can do this by masking both and comparing the masked
6001 results. */
6002 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6003 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6004 if (lnbitsize == rnbitsize
6005 && xll_bitpos == xlr_bitpos
6006 && lnbitpos >= 0
6007 && rnbitpos >= 0)
6008 {
6009 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6010 lntype, lnbitsize, lnbitpos,
6011 ll_unsignedp || rl_unsignedp, ll_reversep);
6012 if (! all_ones_mask_p (ll_mask, lnbitsize))
6013 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6014
6015 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6016 rntype, rnbitsize, rnbitpos,
6017 lr_unsignedp || rr_unsignedp, lr_reversep);
6018 if (! all_ones_mask_p (lr_mask, rnbitsize))
6019 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6020
6021 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6022 }
6023
6024 /* There is still another way we can do something: If both pairs of
6025 fields being compared are adjacent, we may be able to make a wider
6026 field containing them both.
6027
6028 Note that we still must mask the lhs/rhs expressions. Furthermore,
6029 the mask must be shifted to account for the shift done by
6030 make_bit_field_ref. */
6031 if (((ll_bitsize + ll_bitpos == rl_bitpos
6032 && lr_bitsize + lr_bitpos == rr_bitpos)
6033 || (ll_bitpos == rl_bitpos + rl_bitsize
6034 && lr_bitpos == rr_bitpos + rr_bitsize))
6035 && ll_bitpos >= 0
6036 && rl_bitpos >= 0
6037 && lr_bitpos >= 0
6038 && rr_bitpos >= 0)
6039 {
6040 tree type;
6041
6042 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6043 ll_bitsize + rl_bitsize,
6044 MIN (ll_bitpos, rl_bitpos),
6045 ll_unsignedp, ll_reversep);
6046 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6047 lr_bitsize + rr_bitsize,
6048 MIN (lr_bitpos, rr_bitpos),
6049 lr_unsignedp, lr_reversep);
6050
6051 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6052 size_int (MIN (xll_bitpos, xrl_bitpos)));
6053 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6054 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6055
6056 /* Convert to the smaller type before masking out unwanted bits. */
6057 type = lntype;
6058 if (lntype != rntype)
6059 {
6060 if (lnbitsize > rnbitsize)
6061 {
6062 lhs = fold_convert_loc (loc, rntype, lhs);
6063 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6064 type = rntype;
6065 }
6066 else if (lnbitsize < rnbitsize)
6067 {
6068 rhs = fold_convert_loc (loc, lntype, rhs);
6069 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6070 type = lntype;
6071 }
6072 }
6073
6074 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6075 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6076
6077 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6078 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6079
6080 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6081 }
6082
6083 return 0;
6084 }
6085
6086 /* Handle the case of comparisons with constants. If there is something in
6087 common between the masks, those bits of the constants must be the same.
6088 If not, the condition is always false. Test for this to avoid generating
6089 incorrect code below. */
6090 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6091 if (! integer_zerop (result)
6092 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6093 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6094 {
6095 if (wanted_code == NE_EXPR)
6096 {
6097 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6098 return constant_boolean_node (true, truth_type);
6099 }
6100 else
6101 {
6102 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6103 return constant_boolean_node (false, truth_type);
6104 }
6105 }
6106
6107 if (lnbitpos < 0)
6108 return 0;
6109
6110 /* Construct the expression we will return. First get the component
6111 reference we will make. Unless the mask is all ones the width of
6112 that field, perform the mask operation. Then compare with the
6113 merged constant. */
6114 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6115 lntype, lnbitsize, lnbitpos,
6116 ll_unsignedp || rl_unsignedp, ll_reversep);
6117
6118 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6119 if (! all_ones_mask_p (ll_mask, lnbitsize))
6120 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6121
6122 return build2_loc (loc, wanted_code, truth_type, result,
6123 const_binop (BIT_IOR_EXPR, l_const, r_const));
6124 }
6125 \f
6126 /* T is an integer expression that is being multiplied, divided, or taken a
6127 modulus (CODE says which and what kind of divide or modulus) by a
6128 constant C. See if we can eliminate that operation by folding it with
6129 other operations already in T. WIDE_TYPE, if non-null, is a type that
6130 should be used for the computation if wider than our type.
6131
6132 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6133 (X * 2) + (Y * 4). We must, however, be assured that either the original
6134 expression would not overflow or that overflow is undefined for the type
6135 in the language in question.
6136
6137 If we return a non-null expression, it is an equivalent form of the
6138 original computation, but need not be in the original type.
6139
6140 We set *STRICT_OVERFLOW_P to true if the return values depends on
6141 signed overflow being undefined. Otherwise we do not change
6142 *STRICT_OVERFLOW_P. */
6143
6144 static tree
6145 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6146 bool *strict_overflow_p)
6147 {
6148 /* To avoid exponential search depth, refuse to allow recursion past
6149 three levels. Beyond that (1) it's highly unlikely that we'll find
6150 something interesting and (2) we've probably processed it before
6151 when we built the inner expression. */
6152
6153 static int depth;
6154 tree ret;
6155
6156 if (depth > 3)
6157 return NULL;
6158
6159 depth++;
6160 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6161 depth--;
6162
6163 return ret;
6164 }
6165
6166 static tree
6167 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6168 bool *strict_overflow_p)
6169 {
6170 tree type = TREE_TYPE (t);
6171 enum tree_code tcode = TREE_CODE (t);
6172 tree ctype = (wide_type != 0
6173 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6174 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6175 ? wide_type : type);
6176 tree t1, t2;
6177 int same_p = tcode == code;
6178 tree op0 = NULL_TREE, op1 = NULL_TREE;
6179 bool sub_strict_overflow_p;
6180
6181 /* Don't deal with constants of zero here; they confuse the code below. */
6182 if (integer_zerop (c))
6183 return NULL_TREE;
6184
6185 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6186 op0 = TREE_OPERAND (t, 0);
6187
6188 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6189 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6190
6191 /* Note that we need not handle conditional operations here since fold
6192 already handles those cases. So just do arithmetic here. */
6193 switch (tcode)
6194 {
6195 case INTEGER_CST:
6196 /* For a constant, we can always simplify if we are a multiply
6197 or (for divide and modulus) if it is a multiple of our constant. */
6198 if (code == MULT_EXPR
6199 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6200 TYPE_SIGN (type)))
6201 {
6202 tree tem = const_binop (code, fold_convert (ctype, t),
6203 fold_convert (ctype, c));
6204 /* If the multiplication overflowed, we lost information on it.
6205 See PR68142 and PR69845. */
6206 if (TREE_OVERFLOW (tem))
6207 return NULL_TREE;
6208 return tem;
6209 }
6210 break;
6211
6212 CASE_CONVERT: case NON_LVALUE_EXPR:
6213 /* If op0 is an expression ... */
6214 if ((COMPARISON_CLASS_P (op0)
6215 || UNARY_CLASS_P (op0)
6216 || BINARY_CLASS_P (op0)
6217 || VL_EXP_CLASS_P (op0)
6218 || EXPRESSION_CLASS_P (op0))
6219 /* ... and has wrapping overflow, and its type is smaller
6220 than ctype, then we cannot pass through as widening. */
6221 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6222 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6223 && (TYPE_PRECISION (ctype)
6224 > TYPE_PRECISION (TREE_TYPE (op0))))
6225 /* ... or this is a truncation (t is narrower than op0),
6226 then we cannot pass through this narrowing. */
6227 || (TYPE_PRECISION (type)
6228 < TYPE_PRECISION (TREE_TYPE (op0)))
6229 /* ... or signedness changes for division or modulus,
6230 then we cannot pass through this conversion. */
6231 || (code != MULT_EXPR
6232 && (TYPE_UNSIGNED (ctype)
6233 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6234 /* ... or has undefined overflow while the converted to
6235 type has not, we cannot do the operation in the inner type
6236 as that would introduce undefined overflow. */
6237 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6238 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6239 && !TYPE_OVERFLOW_UNDEFINED (type))))
6240 break;
6241
6242 /* Pass the constant down and see if we can make a simplification. If
6243 we can, replace this expression with the inner simplification for
6244 possible later conversion to our or some other type. */
6245 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6246 && TREE_CODE (t2) == INTEGER_CST
6247 && !TREE_OVERFLOW (t2)
6248 && (t1 = extract_muldiv (op0, t2, code,
6249 code == MULT_EXPR ? ctype : NULL_TREE,
6250 strict_overflow_p)) != 0)
6251 return t1;
6252 break;
6253
6254 case ABS_EXPR:
6255 /* If widening the type changes it from signed to unsigned, then we
6256 must avoid building ABS_EXPR itself as unsigned. */
6257 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6258 {
6259 tree cstype = (*signed_type_for) (ctype);
6260 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6261 != 0)
6262 {
6263 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6264 return fold_convert (ctype, t1);
6265 }
6266 break;
6267 }
6268 /* If the constant is negative, we cannot simplify this. */
6269 if (tree_int_cst_sgn (c) == -1)
6270 break;
6271 /* FALLTHROUGH */
6272 case NEGATE_EXPR:
6273 /* For division and modulus, type can't be unsigned, as e.g.
6274 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6275 For signed types, even with wrapping overflow, this is fine. */
6276 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6277 break;
6278 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6279 != 0)
6280 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6281 break;
6282
6283 case MIN_EXPR: case MAX_EXPR:
6284 /* If widening the type changes the signedness, then we can't perform
6285 this optimization as that changes the result. */
6286 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6287 break;
6288
6289 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6290 sub_strict_overflow_p = false;
6291 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6292 &sub_strict_overflow_p)) != 0
6293 && (t2 = extract_muldiv (op1, c, code, wide_type,
6294 &sub_strict_overflow_p)) != 0)
6295 {
6296 if (tree_int_cst_sgn (c) < 0)
6297 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6298 if (sub_strict_overflow_p)
6299 *strict_overflow_p = true;
6300 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6301 fold_convert (ctype, t2));
6302 }
6303 break;
6304
6305 case LSHIFT_EXPR: case RSHIFT_EXPR:
6306 /* If the second operand is constant, this is a multiplication
6307 or floor division, by a power of two, so we can treat it that
6308 way unless the multiplier or divisor overflows. Signed
6309 left-shift overflow is implementation-defined rather than
6310 undefined in C90, so do not convert signed left shift into
6311 multiplication. */
6312 if (TREE_CODE (op1) == INTEGER_CST
6313 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6314 /* const_binop may not detect overflow correctly,
6315 so check for it explicitly here. */
6316 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6317 wi::to_wide (op1))
6318 && (t1 = fold_convert (ctype,
6319 const_binop (LSHIFT_EXPR, size_one_node,
6320 op1))) != 0
6321 && !TREE_OVERFLOW (t1))
6322 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6323 ? MULT_EXPR : FLOOR_DIV_EXPR,
6324 ctype,
6325 fold_convert (ctype, op0),
6326 t1),
6327 c, code, wide_type, strict_overflow_p);
6328 break;
6329
6330 case PLUS_EXPR: case MINUS_EXPR:
6331 /* See if we can eliminate the operation on both sides. If we can, we
6332 can return a new PLUS or MINUS. If we can't, the only remaining
6333 cases where we can do anything are if the second operand is a
6334 constant. */
6335 sub_strict_overflow_p = false;
6336 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6337 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6338 if (t1 != 0 && t2 != 0
6339 && TYPE_OVERFLOW_WRAPS (ctype)
6340 && (code == MULT_EXPR
6341 /* If not multiplication, we can only do this if both operands
6342 are divisible by c. */
6343 || (multiple_of_p (ctype, op0, c)
6344 && multiple_of_p (ctype, op1, c))))
6345 {
6346 if (sub_strict_overflow_p)
6347 *strict_overflow_p = true;
6348 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6349 fold_convert (ctype, t2));
6350 }
6351
6352 /* If this was a subtraction, negate OP1 and set it to be an addition.
6353 This simplifies the logic below. */
6354 if (tcode == MINUS_EXPR)
6355 {
6356 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6357 /* If OP1 was not easily negatable, the constant may be OP0. */
6358 if (TREE_CODE (op0) == INTEGER_CST)
6359 {
6360 std::swap (op0, op1);
6361 std::swap (t1, t2);
6362 }
6363 }
6364
6365 if (TREE_CODE (op1) != INTEGER_CST)
6366 break;
6367
6368 /* If either OP1 or C are negative, this optimization is not safe for
6369 some of the division and remainder types while for others we need
6370 to change the code. */
6371 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6372 {
6373 if (code == CEIL_DIV_EXPR)
6374 code = FLOOR_DIV_EXPR;
6375 else if (code == FLOOR_DIV_EXPR)
6376 code = CEIL_DIV_EXPR;
6377 else if (code != MULT_EXPR
6378 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6379 break;
6380 }
6381
6382 /* If it's a multiply or a division/modulus operation of a multiple
6383 of our constant, do the operation and verify it doesn't overflow. */
6384 if (code == MULT_EXPR
6385 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6386 TYPE_SIGN (type)))
6387 {
6388 op1 = const_binop (code, fold_convert (ctype, op1),
6389 fold_convert (ctype, c));
6390 /* We allow the constant to overflow with wrapping semantics. */
6391 if (op1 == 0
6392 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6393 break;
6394 }
6395 else
6396 break;
6397
6398 /* If we have an unsigned type, we cannot widen the operation since it
6399 will change the result if the original computation overflowed. */
6400 if (TYPE_UNSIGNED (ctype) && ctype != type)
6401 break;
6402
6403 /* The last case is if we are a multiply. In that case, we can
6404 apply the distributive law to commute the multiply and addition
6405 if the multiplication of the constants doesn't overflow
6406 and overflow is defined. With undefined overflow
6407 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6408 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6409 return fold_build2 (tcode, ctype,
6410 fold_build2 (code, ctype,
6411 fold_convert (ctype, op0),
6412 fold_convert (ctype, c)),
6413 op1);
6414
6415 break;
6416
6417 case MULT_EXPR:
6418 /* We have a special case here if we are doing something like
6419 (C * 8) % 4 since we know that's zero. */
6420 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6421 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6422 /* If the multiplication can overflow we cannot optimize this. */
6423 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6424 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6425 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6426 TYPE_SIGN (type)))
6427 {
6428 *strict_overflow_p = true;
6429 return omit_one_operand (type, integer_zero_node, op0);
6430 }
6431
6432 /* ... fall through ... */
6433
6434 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6435 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6436 /* If we can extract our operation from the LHS, do so and return a
6437 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6438 do something only if the second operand is a constant. */
6439 if (same_p
6440 && TYPE_OVERFLOW_WRAPS (ctype)
6441 && (t1 = extract_muldiv (op0, c, code, wide_type,
6442 strict_overflow_p)) != 0)
6443 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6444 fold_convert (ctype, op1));
6445 else if (tcode == MULT_EXPR && code == MULT_EXPR
6446 && TYPE_OVERFLOW_WRAPS (ctype)
6447 && (t1 = extract_muldiv (op1, c, code, wide_type,
6448 strict_overflow_p)) != 0)
6449 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6450 fold_convert (ctype, t1));
6451 else if (TREE_CODE (op1) != INTEGER_CST)
6452 return 0;
6453
6454 /* If these are the same operation types, we can associate them
6455 assuming no overflow. */
6456 if (tcode == code)
6457 {
6458 bool overflow_p = false;
6459 bool overflow_mul_p;
6460 signop sign = TYPE_SIGN (ctype);
6461 unsigned prec = TYPE_PRECISION (ctype);
6462 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6463 wi::to_wide (c, prec),
6464 sign, &overflow_mul_p);
6465 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6466 if (overflow_mul_p
6467 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6468 overflow_p = true;
6469 if (!overflow_p)
6470 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6471 wide_int_to_tree (ctype, mul));
6472 }
6473
6474 /* If these operations "cancel" each other, we have the main
6475 optimizations of this pass, which occur when either constant is a
6476 multiple of the other, in which case we replace this with either an
6477 operation or CODE or TCODE.
6478
6479 If we have an unsigned type, we cannot do this since it will change
6480 the result if the original computation overflowed. */
6481 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6482 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6483 || (tcode == MULT_EXPR
6484 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6485 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6486 && code != MULT_EXPR)))
6487 {
6488 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6489 TYPE_SIGN (type)))
6490 {
6491 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6492 *strict_overflow_p = true;
6493 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6494 fold_convert (ctype,
6495 const_binop (TRUNC_DIV_EXPR,
6496 op1, c)));
6497 }
6498 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6499 TYPE_SIGN (type)))
6500 {
6501 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6502 *strict_overflow_p = true;
6503 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6504 fold_convert (ctype,
6505 const_binop (TRUNC_DIV_EXPR,
6506 c, op1)));
6507 }
6508 }
6509 break;
6510
6511 default:
6512 break;
6513 }
6514
6515 return 0;
6516 }
6517 \f
6518 /* Return a node which has the indicated constant VALUE (either 0 or
6519 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6520 and is of the indicated TYPE. */
6521
6522 tree
6523 constant_boolean_node (bool value, tree type)
6524 {
6525 if (type == integer_type_node)
6526 return value ? integer_one_node : integer_zero_node;
6527 else if (type == boolean_type_node)
6528 return value ? boolean_true_node : boolean_false_node;
6529 else if (TREE_CODE (type) == VECTOR_TYPE)
6530 return build_vector_from_val (type,
6531 build_int_cst (TREE_TYPE (type),
6532 value ? -1 : 0));
6533 else
6534 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6535 }
6536
6537
6538 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6539 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6540 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6541 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6542 COND is the first argument to CODE; otherwise (as in the example
6543 given here), it is the second argument. TYPE is the type of the
6544 original expression. Return NULL_TREE if no simplification is
6545 possible. */
6546
6547 static tree
6548 fold_binary_op_with_conditional_arg (location_t loc,
6549 enum tree_code code,
6550 tree type, tree op0, tree op1,
6551 tree cond, tree arg, int cond_first_p)
6552 {
6553 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6554 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6555 tree test, true_value, false_value;
6556 tree lhs = NULL_TREE;
6557 tree rhs = NULL_TREE;
6558 enum tree_code cond_code = COND_EXPR;
6559
6560 if (TREE_CODE (cond) == COND_EXPR
6561 || TREE_CODE (cond) == VEC_COND_EXPR)
6562 {
6563 test = TREE_OPERAND (cond, 0);
6564 true_value = TREE_OPERAND (cond, 1);
6565 false_value = TREE_OPERAND (cond, 2);
6566 /* If this operand throws an expression, then it does not make
6567 sense to try to perform a logical or arithmetic operation
6568 involving it. */
6569 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6570 lhs = true_value;
6571 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6572 rhs = false_value;
6573 }
6574 else if (!(TREE_CODE (type) != VECTOR_TYPE
6575 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6576 {
6577 tree testtype = TREE_TYPE (cond);
6578 test = cond;
6579 true_value = constant_boolean_node (true, testtype);
6580 false_value = constant_boolean_node (false, testtype);
6581 }
6582 else
6583 /* Detect the case of mixing vector and scalar types - bail out. */
6584 return NULL_TREE;
6585
6586 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6587 cond_code = VEC_COND_EXPR;
6588
6589 /* This transformation is only worthwhile if we don't have to wrap ARG
6590 in a SAVE_EXPR and the operation can be simplified without recursing
6591 on at least one of the branches once its pushed inside the COND_EXPR. */
6592 if (!TREE_CONSTANT (arg)
6593 && (TREE_SIDE_EFFECTS (arg)
6594 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6595 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6596 return NULL_TREE;
6597
6598 arg = fold_convert_loc (loc, arg_type, arg);
6599 if (lhs == 0)
6600 {
6601 true_value = fold_convert_loc (loc, cond_type, true_value);
6602 if (cond_first_p)
6603 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6604 else
6605 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6606 }
6607 if (rhs == 0)
6608 {
6609 false_value = fold_convert_loc (loc, cond_type, false_value);
6610 if (cond_first_p)
6611 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6612 else
6613 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6614 }
6615
6616 /* Check that we have simplified at least one of the branches. */
6617 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6618 return NULL_TREE;
6619
6620 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6621 }
6622
6623 \f
6624 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6625
6626 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6627 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6628 ADDEND is the same as X.
6629
6630 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6631 and finite. The problematic cases are when X is zero, and its mode
6632 has signed zeros. In the case of rounding towards -infinity,
6633 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6634 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6635
6636 bool
6637 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6638 {
6639 if (!real_zerop (addend))
6640 return false;
6641
6642 /* Don't allow the fold with -fsignaling-nans. */
6643 if (HONOR_SNANS (element_mode (type)))
6644 return false;
6645
6646 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6647 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6648 return true;
6649
6650 /* In a vector or complex, we would need to check the sign of all zeros. */
6651 if (TREE_CODE (addend) != REAL_CST)
6652 return false;
6653
6654 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6655 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6656 negate = !negate;
6657
6658 /* The mode has signed zeros, and we have to honor their sign.
6659 In this situation, there is only one case we can return true for.
6660 X - 0 is the same as X unless rounding towards -infinity is
6661 supported. */
6662 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6663 }
6664
6665 /* Subroutine of match.pd that optimizes comparisons of a division by
6666 a nonzero integer constant against an integer constant, i.e.
6667 X/C1 op C2.
6668
6669 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6670 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6671
6672 enum tree_code
6673 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6674 tree *hi, bool *neg_overflow)
6675 {
6676 tree prod, tmp, type = TREE_TYPE (c1);
6677 signop sign = TYPE_SIGN (type);
6678 bool overflow;
6679
6680 /* We have to do this the hard way to detect unsigned overflow.
6681 prod = int_const_binop (MULT_EXPR, c1, c2); */
6682 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6683 prod = force_fit_type (type, val, -1, overflow);
6684 *neg_overflow = false;
6685
6686 if (sign == UNSIGNED)
6687 {
6688 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6689 *lo = prod;
6690
6691 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6692 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6693 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6694 }
6695 else if (tree_int_cst_sgn (c1) >= 0)
6696 {
6697 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6698 switch (tree_int_cst_sgn (c2))
6699 {
6700 case -1:
6701 *neg_overflow = true;
6702 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6703 *hi = prod;
6704 break;
6705
6706 case 0:
6707 *lo = fold_negate_const (tmp, type);
6708 *hi = tmp;
6709 break;
6710
6711 case 1:
6712 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6713 *lo = prod;
6714 break;
6715
6716 default:
6717 gcc_unreachable ();
6718 }
6719 }
6720 else
6721 {
6722 /* A negative divisor reverses the relational operators. */
6723 code = swap_tree_comparison (code);
6724
6725 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6726 switch (tree_int_cst_sgn (c2))
6727 {
6728 case -1:
6729 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6730 *lo = prod;
6731 break;
6732
6733 case 0:
6734 *hi = fold_negate_const (tmp, type);
6735 *lo = tmp;
6736 break;
6737
6738 case 1:
6739 *neg_overflow = true;
6740 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6741 *hi = prod;
6742 break;
6743
6744 default:
6745 gcc_unreachable ();
6746 }
6747 }
6748
6749 if (code != EQ_EXPR && code != NE_EXPR)
6750 return code;
6751
6752 if (TREE_OVERFLOW (*lo)
6753 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6754 *lo = NULL_TREE;
6755 if (TREE_OVERFLOW (*hi)
6756 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6757 *hi = NULL_TREE;
6758
6759 return code;
6760 }
6761
6762
6763 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6764 equality/inequality test, then return a simplified form of the test
6765 using a sign testing. Otherwise return NULL. TYPE is the desired
6766 result type. */
6767
6768 static tree
6769 fold_single_bit_test_into_sign_test (location_t loc,
6770 enum tree_code code, tree arg0, tree arg1,
6771 tree result_type)
6772 {
6773 /* If this is testing a single bit, we can optimize the test. */
6774 if ((code == NE_EXPR || code == EQ_EXPR)
6775 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6776 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6777 {
6778 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6779 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6780 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6781
6782 if (arg00 != NULL_TREE
6783 /* This is only a win if casting to a signed type is cheap,
6784 i.e. when arg00's type is not a partial mode. */
6785 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6786 {
6787 tree stype = signed_type_for (TREE_TYPE (arg00));
6788 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6789 result_type,
6790 fold_convert_loc (loc, stype, arg00),
6791 build_int_cst (stype, 0));
6792 }
6793 }
6794
6795 return NULL_TREE;
6796 }
6797
6798 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6799 equality/inequality test, then return a simplified form of
6800 the test using shifts and logical operations. Otherwise return
6801 NULL. TYPE is the desired result type. */
6802
6803 tree
6804 fold_single_bit_test (location_t loc, enum tree_code code,
6805 tree arg0, tree arg1, tree result_type)
6806 {
6807 /* If this is testing a single bit, we can optimize the test. */
6808 if ((code == NE_EXPR || code == EQ_EXPR)
6809 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6810 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6811 {
6812 tree inner = TREE_OPERAND (arg0, 0);
6813 tree type = TREE_TYPE (arg0);
6814 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6815 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6816 int ops_unsigned;
6817 tree signed_type, unsigned_type, intermediate_type;
6818 tree tem, one;
6819
6820 /* First, see if we can fold the single bit test into a sign-bit
6821 test. */
6822 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6823 result_type);
6824 if (tem)
6825 return tem;
6826
6827 /* Otherwise we have (A & C) != 0 where C is a single bit,
6828 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6829 Similarly for (A & C) == 0. */
6830
6831 /* If INNER is a right shift of a constant and it plus BITNUM does
6832 not overflow, adjust BITNUM and INNER. */
6833 if (TREE_CODE (inner) == RSHIFT_EXPR
6834 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6835 && bitnum < TYPE_PRECISION (type)
6836 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6837 TYPE_PRECISION (type) - bitnum))
6838 {
6839 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6840 inner = TREE_OPERAND (inner, 0);
6841 }
6842
6843 /* If we are going to be able to omit the AND below, we must do our
6844 operations as unsigned. If we must use the AND, we have a choice.
6845 Normally unsigned is faster, but for some machines signed is. */
6846 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6847 && !flag_syntax_only) ? 0 : 1;
6848
6849 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6850 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6851 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6852 inner = fold_convert_loc (loc, intermediate_type, inner);
6853
6854 if (bitnum != 0)
6855 inner = build2 (RSHIFT_EXPR, intermediate_type,
6856 inner, size_int (bitnum));
6857
6858 one = build_int_cst (intermediate_type, 1);
6859
6860 if (code == EQ_EXPR)
6861 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6862
6863 /* Put the AND last so it can combine with more things. */
6864 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6865
6866 /* Make sure to return the proper type. */
6867 inner = fold_convert_loc (loc, result_type, inner);
6868
6869 return inner;
6870 }
6871 return NULL_TREE;
6872 }
6873
6874 /* Test whether it is preferable two swap two operands, ARG0 and
6875 ARG1, for example because ARG0 is an integer constant and ARG1
6876 isn't. */
6877
6878 bool
6879 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6880 {
6881 if (CONSTANT_CLASS_P (arg1))
6882 return 0;
6883 if (CONSTANT_CLASS_P (arg0))
6884 return 1;
6885
6886 STRIP_NOPS (arg0);
6887 STRIP_NOPS (arg1);
6888
6889 if (TREE_CONSTANT (arg1))
6890 return 0;
6891 if (TREE_CONSTANT (arg0))
6892 return 1;
6893
6894 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6895 for commutative and comparison operators. Ensuring a canonical
6896 form allows the optimizers to find additional redundancies without
6897 having to explicitly check for both orderings. */
6898 if (TREE_CODE (arg0) == SSA_NAME
6899 && TREE_CODE (arg1) == SSA_NAME
6900 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6901 return 1;
6902
6903 /* Put SSA_NAMEs last. */
6904 if (TREE_CODE (arg1) == SSA_NAME)
6905 return 0;
6906 if (TREE_CODE (arg0) == SSA_NAME)
6907 return 1;
6908
6909 /* Put variables last. */
6910 if (DECL_P (arg1))
6911 return 0;
6912 if (DECL_P (arg0))
6913 return 1;
6914
6915 return 0;
6916 }
6917
6918
6919 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6920 means A >= Y && A != MAX, but in this case we know that
6921 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6922
6923 static tree
6924 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6925 {
6926 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6927
6928 if (TREE_CODE (bound) == LT_EXPR)
6929 a = TREE_OPERAND (bound, 0);
6930 else if (TREE_CODE (bound) == GT_EXPR)
6931 a = TREE_OPERAND (bound, 1);
6932 else
6933 return NULL_TREE;
6934
6935 typea = TREE_TYPE (a);
6936 if (!INTEGRAL_TYPE_P (typea)
6937 && !POINTER_TYPE_P (typea))
6938 return NULL_TREE;
6939
6940 if (TREE_CODE (ineq) == LT_EXPR)
6941 {
6942 a1 = TREE_OPERAND (ineq, 1);
6943 y = TREE_OPERAND (ineq, 0);
6944 }
6945 else if (TREE_CODE (ineq) == GT_EXPR)
6946 {
6947 a1 = TREE_OPERAND (ineq, 0);
6948 y = TREE_OPERAND (ineq, 1);
6949 }
6950 else
6951 return NULL_TREE;
6952
6953 if (TREE_TYPE (a1) != typea)
6954 return NULL_TREE;
6955
6956 if (POINTER_TYPE_P (typea))
6957 {
6958 /* Convert the pointer types into integer before taking the difference. */
6959 tree ta = fold_convert_loc (loc, ssizetype, a);
6960 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6961 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6962 }
6963 else
6964 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6965
6966 if (!diff || !integer_onep (diff))
6967 return NULL_TREE;
6968
6969 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6970 }
6971
6972 /* Fold a sum or difference of at least one multiplication.
6973 Returns the folded tree or NULL if no simplification could be made. */
6974
6975 static tree
6976 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6977 tree arg0, tree arg1)
6978 {
6979 tree arg00, arg01, arg10, arg11;
6980 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6981
6982 /* (A * C) +- (B * C) -> (A+-B) * C.
6983 (A * C) +- A -> A * (C+-1).
6984 We are most concerned about the case where C is a constant,
6985 but other combinations show up during loop reduction. Since
6986 it is not difficult, try all four possibilities. */
6987
6988 if (TREE_CODE (arg0) == MULT_EXPR)
6989 {
6990 arg00 = TREE_OPERAND (arg0, 0);
6991 arg01 = TREE_OPERAND (arg0, 1);
6992 }
6993 else if (TREE_CODE (arg0) == INTEGER_CST)
6994 {
6995 arg00 = build_one_cst (type);
6996 arg01 = arg0;
6997 }
6998 else
6999 {
7000 /* We cannot generate constant 1 for fract. */
7001 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7002 return NULL_TREE;
7003 arg00 = arg0;
7004 arg01 = build_one_cst (type);
7005 }
7006 if (TREE_CODE (arg1) == MULT_EXPR)
7007 {
7008 arg10 = TREE_OPERAND (arg1, 0);
7009 arg11 = TREE_OPERAND (arg1, 1);
7010 }
7011 else if (TREE_CODE (arg1) == INTEGER_CST)
7012 {
7013 arg10 = build_one_cst (type);
7014 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7015 the purpose of this canonicalization. */
7016 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7017 && negate_expr_p (arg1)
7018 && code == PLUS_EXPR)
7019 {
7020 arg11 = negate_expr (arg1);
7021 code = MINUS_EXPR;
7022 }
7023 else
7024 arg11 = arg1;
7025 }
7026 else
7027 {
7028 /* We cannot generate constant 1 for fract. */
7029 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7030 return NULL_TREE;
7031 arg10 = arg1;
7032 arg11 = build_one_cst (type);
7033 }
7034 same = NULL_TREE;
7035
7036 /* Prefer factoring a common non-constant. */
7037 if (operand_equal_p (arg00, arg10, 0))
7038 same = arg00, alt0 = arg01, alt1 = arg11;
7039 else if (operand_equal_p (arg01, arg11, 0))
7040 same = arg01, alt0 = arg00, alt1 = arg10;
7041 else if (operand_equal_p (arg00, arg11, 0))
7042 same = arg00, alt0 = arg01, alt1 = arg10;
7043 else if (operand_equal_p (arg01, arg10, 0))
7044 same = arg01, alt0 = arg00, alt1 = arg11;
7045
7046 /* No identical multiplicands; see if we can find a common
7047 power-of-two factor in non-power-of-two multiplies. This
7048 can help in multi-dimensional array access. */
7049 else if (tree_fits_shwi_p (arg01)
7050 && tree_fits_shwi_p (arg11))
7051 {
7052 HOST_WIDE_INT int01, int11, tmp;
7053 bool swap = false;
7054 tree maybe_same;
7055 int01 = tree_to_shwi (arg01);
7056 int11 = tree_to_shwi (arg11);
7057
7058 /* Move min of absolute values to int11. */
7059 if (absu_hwi (int01) < absu_hwi (int11))
7060 {
7061 tmp = int01, int01 = int11, int11 = tmp;
7062 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7063 maybe_same = arg01;
7064 swap = true;
7065 }
7066 else
7067 maybe_same = arg11;
7068
7069 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7070 /* The remainder should not be a constant, otherwise we
7071 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7072 increased the number of multiplications necessary. */
7073 && TREE_CODE (arg10) != INTEGER_CST)
7074 {
7075 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7076 build_int_cst (TREE_TYPE (arg00),
7077 int01 / int11));
7078 alt1 = arg10;
7079 same = maybe_same;
7080 if (swap)
7081 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7082 }
7083 }
7084
7085 if (!same)
7086 return NULL_TREE;
7087
7088 if (! INTEGRAL_TYPE_P (type)
7089 || TYPE_OVERFLOW_WRAPS (type)
7090 /* We are neither factoring zero nor minus one. */
7091 || TREE_CODE (same) == INTEGER_CST)
7092 return fold_build2_loc (loc, MULT_EXPR, type,
7093 fold_build2_loc (loc, code, type,
7094 fold_convert_loc (loc, type, alt0),
7095 fold_convert_loc (loc, type, alt1)),
7096 fold_convert_loc (loc, type, same));
7097
7098 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7099 same may be minus one and thus the multiplication may overflow. Perform
7100 the operations in an unsigned type. */
7101 tree utype = unsigned_type_for (type);
7102 tree tem = fold_build2_loc (loc, code, utype,
7103 fold_convert_loc (loc, utype, alt0),
7104 fold_convert_loc (loc, utype, alt1));
7105 /* If the sum evaluated to a constant that is not -INF the multiplication
7106 cannot overflow. */
7107 if (TREE_CODE (tem) == INTEGER_CST
7108 && (wi::to_wide (tem)
7109 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7110 return fold_build2_loc (loc, MULT_EXPR, type,
7111 fold_convert (type, tem), same);
7112
7113 return fold_convert_loc (loc, type,
7114 fold_build2_loc (loc, MULT_EXPR, utype, tem,
7115 fold_convert_loc (loc, utype, same)));
7116 }
7117
7118 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7119 specified by EXPR into the buffer PTR of length LEN bytes.
7120 Return the number of bytes placed in the buffer, or zero
7121 upon failure. */
7122
7123 static int
7124 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7125 {
7126 tree type = TREE_TYPE (expr);
7127 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7128 int byte, offset, word, words;
7129 unsigned char value;
7130
7131 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7132 return 0;
7133 if (off == -1)
7134 off = 0;
7135
7136 if (ptr == NULL)
7137 /* Dry run. */
7138 return MIN (len, total_bytes - off);
7139
7140 words = total_bytes / UNITS_PER_WORD;
7141
7142 for (byte = 0; byte < total_bytes; byte++)
7143 {
7144 int bitpos = byte * BITS_PER_UNIT;
7145 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7146 number of bytes. */
7147 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7148
7149 if (total_bytes > UNITS_PER_WORD)
7150 {
7151 word = byte / UNITS_PER_WORD;
7152 if (WORDS_BIG_ENDIAN)
7153 word = (words - 1) - word;
7154 offset = word * UNITS_PER_WORD;
7155 if (BYTES_BIG_ENDIAN)
7156 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7157 else
7158 offset += byte % UNITS_PER_WORD;
7159 }
7160 else
7161 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7162 if (offset >= off && offset - off < len)
7163 ptr[offset - off] = value;
7164 }
7165 return MIN (len, total_bytes - off);
7166 }
7167
7168
7169 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7170 specified by EXPR into the buffer PTR of length LEN bytes.
7171 Return the number of bytes placed in the buffer, or zero
7172 upon failure. */
7173
7174 static int
7175 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7176 {
7177 tree type = TREE_TYPE (expr);
7178 scalar_mode mode = SCALAR_TYPE_MODE (type);
7179 int total_bytes = GET_MODE_SIZE (mode);
7180 FIXED_VALUE_TYPE value;
7181 tree i_value, i_type;
7182
7183 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7184 return 0;
7185
7186 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7187
7188 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7189 return 0;
7190
7191 value = TREE_FIXED_CST (expr);
7192 i_value = double_int_to_tree (i_type, value.data);
7193
7194 return native_encode_int (i_value, ptr, len, off);
7195 }
7196
7197
7198 /* Subroutine of native_encode_expr. Encode the REAL_CST
7199 specified by EXPR into the buffer PTR of length LEN bytes.
7200 Return the number of bytes placed in the buffer, or zero
7201 upon failure. */
7202
7203 static int
7204 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7205 {
7206 tree type = TREE_TYPE (expr);
7207 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7208 int byte, offset, word, words, bitpos;
7209 unsigned char value;
7210
7211 /* There are always 32 bits in each long, no matter the size of
7212 the hosts long. We handle floating point representations with
7213 up to 192 bits. */
7214 long tmp[6];
7215
7216 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7217 return 0;
7218 if (off == -1)
7219 off = 0;
7220
7221 if (ptr == NULL)
7222 /* Dry run. */
7223 return MIN (len, total_bytes - off);
7224
7225 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7226
7227 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7228
7229 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7230 bitpos += BITS_PER_UNIT)
7231 {
7232 byte = (bitpos / BITS_PER_UNIT) & 3;
7233 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7234
7235 if (UNITS_PER_WORD < 4)
7236 {
7237 word = byte / UNITS_PER_WORD;
7238 if (WORDS_BIG_ENDIAN)
7239 word = (words - 1) - word;
7240 offset = word * UNITS_PER_WORD;
7241 if (BYTES_BIG_ENDIAN)
7242 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7243 else
7244 offset += byte % UNITS_PER_WORD;
7245 }
7246 else
7247 {
7248 offset = byte;
7249 if (BYTES_BIG_ENDIAN)
7250 {
7251 /* Reverse bytes within each long, or within the entire float
7252 if it's smaller than a long (for HFmode). */
7253 offset = MIN (3, total_bytes - 1) - offset;
7254 gcc_assert (offset >= 0);
7255 }
7256 }
7257 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7258 if (offset >= off
7259 && offset - off < len)
7260 ptr[offset - off] = value;
7261 }
7262 return MIN (len, total_bytes - off);
7263 }
7264
7265 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7266 specified by EXPR into the buffer PTR of length LEN bytes.
7267 Return the number of bytes placed in the buffer, or zero
7268 upon failure. */
7269
7270 static int
7271 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7272 {
7273 int rsize, isize;
7274 tree part;
7275
7276 part = TREE_REALPART (expr);
7277 rsize = native_encode_expr (part, ptr, len, off);
7278 if (off == -1 && rsize == 0)
7279 return 0;
7280 part = TREE_IMAGPART (expr);
7281 if (off != -1)
7282 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7283 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7284 len - rsize, off);
7285 if (off == -1 && isize != rsize)
7286 return 0;
7287 return rsize + isize;
7288 }
7289
7290
7291 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7292 specified by EXPR into the buffer PTR of length LEN bytes.
7293 Return the number of bytes placed in the buffer, or zero
7294 upon failure. */
7295
7296 static int
7297 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7298 {
7299 unsigned HOST_WIDE_INT i, count;
7300 int size, offset;
7301 tree itype, elem;
7302
7303 offset = 0;
7304 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7305 return 0;
7306 itype = TREE_TYPE (TREE_TYPE (expr));
7307 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7308 for (i = 0; i < count; i++)
7309 {
7310 if (off >= size)
7311 {
7312 off -= size;
7313 continue;
7314 }
7315 elem = VECTOR_CST_ELT (expr, i);
7316 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7317 len - offset, off);
7318 if ((off == -1 && res != size) || res == 0)
7319 return 0;
7320 offset += res;
7321 if (offset >= len)
7322 return offset;
7323 if (off != -1)
7324 off = 0;
7325 }
7326 return offset;
7327 }
7328
7329
7330 /* Subroutine of native_encode_expr. Encode the STRING_CST
7331 specified by EXPR into the buffer PTR of length LEN bytes.
7332 Return the number of bytes placed in the buffer, or zero
7333 upon failure. */
7334
7335 static int
7336 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7337 {
7338 tree type = TREE_TYPE (expr);
7339
7340 /* Wide-char strings are encoded in target byte-order so native
7341 encoding them is trivial. */
7342 if (BITS_PER_UNIT != CHAR_BIT
7343 || TREE_CODE (type) != ARRAY_TYPE
7344 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7345 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7346 return 0;
7347
7348 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7349 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7350 return 0;
7351 if (off == -1)
7352 off = 0;
7353 if (ptr == NULL)
7354 /* Dry run. */;
7355 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7356 {
7357 int written = 0;
7358 if (off < TREE_STRING_LENGTH (expr))
7359 {
7360 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7361 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7362 }
7363 memset (ptr + written, 0,
7364 MIN (total_bytes - written, len - written));
7365 }
7366 else
7367 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7368 return MIN (total_bytes - off, len);
7369 }
7370
7371
7372 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7373 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7374 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7375 anything, just do a dry run. If OFF is not -1 then start
7376 the encoding at byte offset OFF and encode at most LEN bytes.
7377 Return the number of bytes placed in the buffer, or zero upon failure. */
7378
7379 int
7380 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7381 {
7382 /* We don't support starting at negative offset and -1 is special. */
7383 if (off < -1)
7384 return 0;
7385
7386 switch (TREE_CODE (expr))
7387 {
7388 case INTEGER_CST:
7389 return native_encode_int (expr, ptr, len, off);
7390
7391 case REAL_CST:
7392 return native_encode_real (expr, ptr, len, off);
7393
7394 case FIXED_CST:
7395 return native_encode_fixed (expr, ptr, len, off);
7396
7397 case COMPLEX_CST:
7398 return native_encode_complex (expr, ptr, len, off);
7399
7400 case VECTOR_CST:
7401 return native_encode_vector (expr, ptr, len, off);
7402
7403 case STRING_CST:
7404 return native_encode_string (expr, ptr, len, off);
7405
7406 default:
7407 return 0;
7408 }
7409 }
7410
7411
7412 /* Subroutine of native_interpret_expr. Interpret the contents of
7413 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7414 If the buffer cannot be interpreted, return NULL_TREE. */
7415
7416 static tree
7417 native_interpret_int (tree type, const unsigned char *ptr, int len)
7418 {
7419 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7420
7421 if (total_bytes > len
7422 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7423 return NULL_TREE;
7424
7425 wide_int result = wi::from_buffer (ptr, total_bytes);
7426
7427 return wide_int_to_tree (type, result);
7428 }
7429
7430
7431 /* Subroutine of native_interpret_expr. Interpret the contents of
7432 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7433 If the buffer cannot be interpreted, return NULL_TREE. */
7434
7435 static tree
7436 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7437 {
7438 scalar_mode mode = SCALAR_TYPE_MODE (type);
7439 int total_bytes = GET_MODE_SIZE (mode);
7440 double_int result;
7441 FIXED_VALUE_TYPE fixed_value;
7442
7443 if (total_bytes > len
7444 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7445 return NULL_TREE;
7446
7447 result = double_int::from_buffer (ptr, total_bytes);
7448 fixed_value = fixed_from_double_int (result, mode);
7449
7450 return build_fixed (type, fixed_value);
7451 }
7452
7453
7454 /* Subroutine of native_interpret_expr. Interpret the contents of
7455 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7456 If the buffer cannot be interpreted, return NULL_TREE. */
7457
7458 static tree
7459 native_interpret_real (tree type, const unsigned char *ptr, int len)
7460 {
7461 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7462 int total_bytes = GET_MODE_SIZE (mode);
7463 unsigned char value;
7464 /* There are always 32 bits in each long, no matter the size of
7465 the hosts long. We handle floating point representations with
7466 up to 192 bits. */
7467 REAL_VALUE_TYPE r;
7468 long tmp[6];
7469
7470 if (total_bytes > len || total_bytes > 24)
7471 return NULL_TREE;
7472 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7473
7474 memset (tmp, 0, sizeof (tmp));
7475 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7476 bitpos += BITS_PER_UNIT)
7477 {
7478 /* Both OFFSET and BYTE index within a long;
7479 bitpos indexes the whole float. */
7480 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7481 if (UNITS_PER_WORD < 4)
7482 {
7483 int word = byte / UNITS_PER_WORD;
7484 if (WORDS_BIG_ENDIAN)
7485 word = (words - 1) - word;
7486 offset = word * UNITS_PER_WORD;
7487 if (BYTES_BIG_ENDIAN)
7488 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7489 else
7490 offset += byte % UNITS_PER_WORD;
7491 }
7492 else
7493 {
7494 offset = byte;
7495 if (BYTES_BIG_ENDIAN)
7496 {
7497 /* Reverse bytes within each long, or within the entire float
7498 if it's smaller than a long (for HFmode). */
7499 offset = MIN (3, total_bytes - 1) - offset;
7500 gcc_assert (offset >= 0);
7501 }
7502 }
7503 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7504
7505 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7506 }
7507
7508 real_from_target (&r, tmp, mode);
7509 return build_real (type, r);
7510 }
7511
7512
7513 /* Subroutine of native_interpret_expr. Interpret the contents of
7514 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7515 If the buffer cannot be interpreted, return NULL_TREE. */
7516
7517 static tree
7518 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7519 {
7520 tree etype, rpart, ipart;
7521 int size;
7522
7523 etype = TREE_TYPE (type);
7524 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7525 if (size * 2 > len)
7526 return NULL_TREE;
7527 rpart = native_interpret_expr (etype, ptr, size);
7528 if (!rpart)
7529 return NULL_TREE;
7530 ipart = native_interpret_expr (etype, ptr+size, size);
7531 if (!ipart)
7532 return NULL_TREE;
7533 return build_complex (type, rpart, ipart);
7534 }
7535
7536
7537 /* Subroutine of native_interpret_expr. Interpret the contents of
7538 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7539 If the buffer cannot be interpreted, return NULL_TREE. */
7540
7541 static tree
7542 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7543 {
7544 tree etype, elem;
7545 unsigned int i, size;
7546 unsigned HOST_WIDE_INT count;
7547
7548 etype = TREE_TYPE (type);
7549 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7550 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7551 || size * count > len)
7552 return NULL_TREE;
7553
7554 tree_vector_builder elements (type, count, 1);
7555 for (i = 0; i < count; ++i)
7556 {
7557 elem = native_interpret_expr (etype, ptr+(i*size), size);
7558 if (!elem)
7559 return NULL_TREE;
7560 elements.quick_push (elem);
7561 }
7562 return elements.build ();
7563 }
7564
7565
7566 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7567 the buffer PTR of length LEN as a constant of type TYPE. For
7568 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7569 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7570 return NULL_TREE. */
7571
7572 tree
7573 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7574 {
7575 switch (TREE_CODE (type))
7576 {
7577 case INTEGER_TYPE:
7578 case ENUMERAL_TYPE:
7579 case BOOLEAN_TYPE:
7580 case POINTER_TYPE:
7581 case REFERENCE_TYPE:
7582 return native_interpret_int (type, ptr, len);
7583
7584 case REAL_TYPE:
7585 return native_interpret_real (type, ptr, len);
7586
7587 case FIXED_POINT_TYPE:
7588 return native_interpret_fixed (type, ptr, len);
7589
7590 case COMPLEX_TYPE:
7591 return native_interpret_complex (type, ptr, len);
7592
7593 case VECTOR_TYPE:
7594 return native_interpret_vector (type, ptr, len);
7595
7596 default:
7597 return NULL_TREE;
7598 }
7599 }
7600
7601 /* Returns true if we can interpret the contents of a native encoding
7602 as TYPE. */
7603
7604 static bool
7605 can_native_interpret_type_p (tree type)
7606 {
7607 switch (TREE_CODE (type))
7608 {
7609 case INTEGER_TYPE:
7610 case ENUMERAL_TYPE:
7611 case BOOLEAN_TYPE:
7612 case POINTER_TYPE:
7613 case REFERENCE_TYPE:
7614 case FIXED_POINT_TYPE:
7615 case REAL_TYPE:
7616 case COMPLEX_TYPE:
7617 case VECTOR_TYPE:
7618 return true;
7619 default:
7620 return false;
7621 }
7622 }
7623
7624
7625 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7626 TYPE at compile-time. If we're unable to perform the conversion
7627 return NULL_TREE. */
7628
7629 static tree
7630 fold_view_convert_expr (tree type, tree expr)
7631 {
7632 /* We support up to 512-bit values (for V8DFmode). */
7633 unsigned char buffer[64];
7634 int len;
7635
7636 /* Check that the host and target are sane. */
7637 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7638 return NULL_TREE;
7639
7640 len = native_encode_expr (expr, buffer, sizeof (buffer));
7641 if (len == 0)
7642 return NULL_TREE;
7643
7644 return native_interpret_expr (type, buffer, len);
7645 }
7646
7647 /* Build an expression for the address of T. Folds away INDIRECT_REF
7648 to avoid confusing the gimplify process. */
7649
7650 tree
7651 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7652 {
7653 /* The size of the object is not relevant when talking about its address. */
7654 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7655 t = TREE_OPERAND (t, 0);
7656
7657 if (TREE_CODE (t) == INDIRECT_REF)
7658 {
7659 t = TREE_OPERAND (t, 0);
7660
7661 if (TREE_TYPE (t) != ptrtype)
7662 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7663 }
7664 else if (TREE_CODE (t) == MEM_REF
7665 && integer_zerop (TREE_OPERAND (t, 1)))
7666 return TREE_OPERAND (t, 0);
7667 else if (TREE_CODE (t) == MEM_REF
7668 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7669 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7670 TREE_OPERAND (t, 0),
7671 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7672 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7673 {
7674 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7675
7676 if (TREE_TYPE (t) != ptrtype)
7677 t = fold_convert_loc (loc, ptrtype, t);
7678 }
7679 else
7680 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7681
7682 return t;
7683 }
7684
7685 /* Build an expression for the address of T. */
7686
7687 tree
7688 build_fold_addr_expr_loc (location_t loc, tree t)
7689 {
7690 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7691
7692 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7693 }
7694
7695 /* Fold a unary expression of code CODE and type TYPE with operand
7696 OP0. Return the folded expression if folding is successful.
7697 Otherwise, return NULL_TREE. */
7698
7699 tree
7700 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7701 {
7702 tree tem;
7703 tree arg0;
7704 enum tree_code_class kind = TREE_CODE_CLASS (code);
7705
7706 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7707 && TREE_CODE_LENGTH (code) == 1);
7708
7709 arg0 = op0;
7710 if (arg0)
7711 {
7712 if (CONVERT_EXPR_CODE_P (code)
7713 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7714 {
7715 /* Don't use STRIP_NOPS, because signedness of argument type
7716 matters. */
7717 STRIP_SIGN_NOPS (arg0);
7718 }
7719 else
7720 {
7721 /* Strip any conversions that don't change the mode. This
7722 is safe for every expression, except for a comparison
7723 expression because its signedness is derived from its
7724 operands.
7725
7726 Note that this is done as an internal manipulation within
7727 the constant folder, in order to find the simplest
7728 representation of the arguments so that their form can be
7729 studied. In any cases, the appropriate type conversions
7730 should be put back in the tree that will get out of the
7731 constant folder. */
7732 STRIP_NOPS (arg0);
7733 }
7734
7735 if (CONSTANT_CLASS_P (arg0))
7736 {
7737 tree tem = const_unop (code, type, arg0);
7738 if (tem)
7739 {
7740 if (TREE_TYPE (tem) != type)
7741 tem = fold_convert_loc (loc, type, tem);
7742 return tem;
7743 }
7744 }
7745 }
7746
7747 tem = generic_simplify (loc, code, type, op0);
7748 if (tem)
7749 return tem;
7750
7751 if (TREE_CODE_CLASS (code) == tcc_unary)
7752 {
7753 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7754 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7755 fold_build1_loc (loc, code, type,
7756 fold_convert_loc (loc, TREE_TYPE (op0),
7757 TREE_OPERAND (arg0, 1))));
7758 else if (TREE_CODE (arg0) == COND_EXPR)
7759 {
7760 tree arg01 = TREE_OPERAND (arg0, 1);
7761 tree arg02 = TREE_OPERAND (arg0, 2);
7762 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7763 arg01 = fold_build1_loc (loc, code, type,
7764 fold_convert_loc (loc,
7765 TREE_TYPE (op0), arg01));
7766 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7767 arg02 = fold_build1_loc (loc, code, type,
7768 fold_convert_loc (loc,
7769 TREE_TYPE (op0), arg02));
7770 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7771 arg01, arg02);
7772
7773 /* If this was a conversion, and all we did was to move into
7774 inside the COND_EXPR, bring it back out. But leave it if
7775 it is a conversion from integer to integer and the
7776 result precision is no wider than a word since such a
7777 conversion is cheap and may be optimized away by combine,
7778 while it couldn't if it were outside the COND_EXPR. Then return
7779 so we don't get into an infinite recursion loop taking the
7780 conversion out and then back in. */
7781
7782 if ((CONVERT_EXPR_CODE_P (code)
7783 || code == NON_LVALUE_EXPR)
7784 && TREE_CODE (tem) == COND_EXPR
7785 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7786 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7787 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7788 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7789 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7790 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7791 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7792 && (INTEGRAL_TYPE_P
7793 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7794 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7795 || flag_syntax_only))
7796 tem = build1_loc (loc, code, type,
7797 build3 (COND_EXPR,
7798 TREE_TYPE (TREE_OPERAND
7799 (TREE_OPERAND (tem, 1), 0)),
7800 TREE_OPERAND (tem, 0),
7801 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7802 TREE_OPERAND (TREE_OPERAND (tem, 2),
7803 0)));
7804 return tem;
7805 }
7806 }
7807
7808 switch (code)
7809 {
7810 case NON_LVALUE_EXPR:
7811 if (!maybe_lvalue_p (op0))
7812 return fold_convert_loc (loc, type, op0);
7813 return NULL_TREE;
7814
7815 CASE_CONVERT:
7816 case FLOAT_EXPR:
7817 case FIX_TRUNC_EXPR:
7818 if (COMPARISON_CLASS_P (op0))
7819 {
7820 /* If we have (type) (a CMP b) and type is an integral type, return
7821 new expression involving the new type. Canonicalize
7822 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7823 non-integral type.
7824 Do not fold the result as that would not simplify further, also
7825 folding again results in recursions. */
7826 if (TREE_CODE (type) == BOOLEAN_TYPE)
7827 return build2_loc (loc, TREE_CODE (op0), type,
7828 TREE_OPERAND (op0, 0),
7829 TREE_OPERAND (op0, 1));
7830 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7831 && TREE_CODE (type) != VECTOR_TYPE)
7832 return build3_loc (loc, COND_EXPR, type, op0,
7833 constant_boolean_node (true, type),
7834 constant_boolean_node (false, type));
7835 }
7836
7837 /* Handle (T *)&A.B.C for A being of type T and B and C
7838 living at offset zero. This occurs frequently in
7839 C++ upcasting and then accessing the base. */
7840 if (TREE_CODE (op0) == ADDR_EXPR
7841 && POINTER_TYPE_P (type)
7842 && handled_component_p (TREE_OPERAND (op0, 0)))
7843 {
7844 poly_int64 bitsize, bitpos;
7845 tree offset;
7846 machine_mode mode;
7847 int unsignedp, reversep, volatilep;
7848 tree base
7849 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7850 &offset, &mode, &unsignedp, &reversep,
7851 &volatilep);
7852 /* If the reference was to a (constant) zero offset, we can use
7853 the address of the base if it has the same base type
7854 as the result type and the pointer type is unqualified. */
7855 if (!offset
7856 && known_eq (bitpos, 0)
7857 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7858 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7859 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7860 return fold_convert_loc (loc, type,
7861 build_fold_addr_expr_loc (loc, base));
7862 }
7863
7864 if (TREE_CODE (op0) == MODIFY_EXPR
7865 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7866 /* Detect assigning a bitfield. */
7867 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7868 && DECL_BIT_FIELD
7869 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7870 {
7871 /* Don't leave an assignment inside a conversion
7872 unless assigning a bitfield. */
7873 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7874 /* First do the assignment, then return converted constant. */
7875 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7876 TREE_NO_WARNING (tem) = 1;
7877 TREE_USED (tem) = 1;
7878 return tem;
7879 }
7880
7881 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7882 constants (if x has signed type, the sign bit cannot be set
7883 in c). This folds extension into the BIT_AND_EXPR.
7884 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7885 very likely don't have maximal range for their precision and this
7886 transformation effectively doesn't preserve non-maximal ranges. */
7887 if (TREE_CODE (type) == INTEGER_TYPE
7888 && TREE_CODE (op0) == BIT_AND_EXPR
7889 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7890 {
7891 tree and_expr = op0;
7892 tree and0 = TREE_OPERAND (and_expr, 0);
7893 tree and1 = TREE_OPERAND (and_expr, 1);
7894 int change = 0;
7895
7896 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7897 || (TYPE_PRECISION (type)
7898 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7899 change = 1;
7900 else if (TYPE_PRECISION (TREE_TYPE (and1))
7901 <= HOST_BITS_PER_WIDE_INT
7902 && tree_fits_uhwi_p (and1))
7903 {
7904 unsigned HOST_WIDE_INT cst;
7905
7906 cst = tree_to_uhwi (and1);
7907 cst &= HOST_WIDE_INT_M1U
7908 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7909 change = (cst == 0);
7910 if (change
7911 && !flag_syntax_only
7912 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7913 == ZERO_EXTEND))
7914 {
7915 tree uns = unsigned_type_for (TREE_TYPE (and0));
7916 and0 = fold_convert_loc (loc, uns, and0);
7917 and1 = fold_convert_loc (loc, uns, and1);
7918 }
7919 }
7920 if (change)
7921 {
7922 tem = force_fit_type (type, wi::to_widest (and1), 0,
7923 TREE_OVERFLOW (and1));
7924 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7925 fold_convert_loc (loc, type, and0), tem);
7926 }
7927 }
7928
7929 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7930 cast (T1)X will fold away. We assume that this happens when X itself
7931 is a cast. */
7932 if (POINTER_TYPE_P (type)
7933 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7934 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7935 {
7936 tree arg00 = TREE_OPERAND (arg0, 0);
7937 tree arg01 = TREE_OPERAND (arg0, 1);
7938
7939 return fold_build_pointer_plus_loc
7940 (loc, fold_convert_loc (loc, type, arg00), arg01);
7941 }
7942
7943 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7944 of the same precision, and X is an integer type not narrower than
7945 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7946 if (INTEGRAL_TYPE_P (type)
7947 && TREE_CODE (op0) == BIT_NOT_EXPR
7948 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7949 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7950 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7951 {
7952 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7953 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7954 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7955 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7956 fold_convert_loc (loc, type, tem));
7957 }
7958
7959 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7960 type of X and Y (integer types only). */
7961 if (INTEGRAL_TYPE_P (type)
7962 && TREE_CODE (op0) == MULT_EXPR
7963 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7964 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7965 {
7966 /* Be careful not to introduce new overflows. */
7967 tree mult_type;
7968 if (TYPE_OVERFLOW_WRAPS (type))
7969 mult_type = type;
7970 else
7971 mult_type = unsigned_type_for (type);
7972
7973 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7974 {
7975 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7976 fold_convert_loc (loc, mult_type,
7977 TREE_OPERAND (op0, 0)),
7978 fold_convert_loc (loc, mult_type,
7979 TREE_OPERAND (op0, 1)));
7980 return fold_convert_loc (loc, type, tem);
7981 }
7982 }
7983
7984 return NULL_TREE;
7985
7986 case VIEW_CONVERT_EXPR:
7987 if (TREE_CODE (op0) == MEM_REF)
7988 {
7989 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7990 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7991 tem = fold_build2_loc (loc, MEM_REF, type,
7992 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7993 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7994 return tem;
7995 }
7996
7997 return NULL_TREE;
7998
7999 case NEGATE_EXPR:
8000 tem = fold_negate_expr (loc, arg0);
8001 if (tem)
8002 return fold_convert_loc (loc, type, tem);
8003 return NULL_TREE;
8004
8005 case ABS_EXPR:
8006 /* Convert fabs((double)float) into (double)fabsf(float). */
8007 if (TREE_CODE (arg0) == NOP_EXPR
8008 && TREE_CODE (type) == REAL_TYPE)
8009 {
8010 tree targ0 = strip_float_extensions (arg0);
8011 if (targ0 != arg0)
8012 return fold_convert_loc (loc, type,
8013 fold_build1_loc (loc, ABS_EXPR,
8014 TREE_TYPE (targ0),
8015 targ0));
8016 }
8017 return NULL_TREE;
8018
8019 case BIT_NOT_EXPR:
8020 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8021 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8022 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8023 fold_convert_loc (loc, type,
8024 TREE_OPERAND (arg0, 0)))))
8025 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8026 fold_convert_loc (loc, type,
8027 TREE_OPERAND (arg0, 1)));
8028 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8029 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8030 fold_convert_loc (loc, type,
8031 TREE_OPERAND (arg0, 1)))))
8032 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8033 fold_convert_loc (loc, type,
8034 TREE_OPERAND (arg0, 0)), tem);
8035
8036 return NULL_TREE;
8037
8038 case TRUTH_NOT_EXPR:
8039 /* Note that the operand of this must be an int
8040 and its values must be 0 or 1.
8041 ("true" is a fixed value perhaps depending on the language,
8042 but we don't handle values other than 1 correctly yet.) */
8043 tem = fold_truth_not_expr (loc, arg0);
8044 if (!tem)
8045 return NULL_TREE;
8046 return fold_convert_loc (loc, type, tem);
8047
8048 case INDIRECT_REF:
8049 /* Fold *&X to X if X is an lvalue. */
8050 if (TREE_CODE (op0) == ADDR_EXPR)
8051 {
8052 tree op00 = TREE_OPERAND (op0, 0);
8053 if ((VAR_P (op00)
8054 || TREE_CODE (op00) == PARM_DECL
8055 || TREE_CODE (op00) == RESULT_DECL)
8056 && !TREE_READONLY (op00))
8057 return op00;
8058 }
8059 return NULL_TREE;
8060
8061 default:
8062 return NULL_TREE;
8063 } /* switch (code) */
8064 }
8065
8066
8067 /* If the operation was a conversion do _not_ mark a resulting constant
8068 with TREE_OVERFLOW if the original constant was not. These conversions
8069 have implementation defined behavior and retaining the TREE_OVERFLOW
8070 flag here would confuse later passes such as VRP. */
8071 tree
8072 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8073 tree type, tree op0)
8074 {
8075 tree res = fold_unary_loc (loc, code, type, op0);
8076 if (res
8077 && TREE_CODE (res) == INTEGER_CST
8078 && TREE_CODE (op0) == INTEGER_CST
8079 && CONVERT_EXPR_CODE_P (code))
8080 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8081
8082 return res;
8083 }
8084
8085 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8086 operands OP0 and OP1. LOC is the location of the resulting expression.
8087 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8088 Return the folded expression if folding is successful. Otherwise,
8089 return NULL_TREE. */
8090 static tree
8091 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8092 tree arg0, tree arg1, tree op0, tree op1)
8093 {
8094 tree tem;
8095
8096 /* We only do these simplifications if we are optimizing. */
8097 if (!optimize)
8098 return NULL_TREE;
8099
8100 /* Check for things like (A || B) && (A || C). We can convert this
8101 to A || (B && C). Note that either operator can be any of the four
8102 truth and/or operations and the transformation will still be
8103 valid. Also note that we only care about order for the
8104 ANDIF and ORIF operators. If B contains side effects, this
8105 might change the truth-value of A. */
8106 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8107 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8108 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8109 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8110 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8111 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8112 {
8113 tree a00 = TREE_OPERAND (arg0, 0);
8114 tree a01 = TREE_OPERAND (arg0, 1);
8115 tree a10 = TREE_OPERAND (arg1, 0);
8116 tree a11 = TREE_OPERAND (arg1, 1);
8117 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8118 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8119 && (code == TRUTH_AND_EXPR
8120 || code == TRUTH_OR_EXPR));
8121
8122 if (operand_equal_p (a00, a10, 0))
8123 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8124 fold_build2_loc (loc, code, type, a01, a11));
8125 else if (commutative && operand_equal_p (a00, a11, 0))
8126 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8127 fold_build2_loc (loc, code, type, a01, a10));
8128 else if (commutative && operand_equal_p (a01, a10, 0))
8129 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8130 fold_build2_loc (loc, code, type, a00, a11));
8131
8132 /* This case if tricky because we must either have commutative
8133 operators or else A10 must not have side-effects. */
8134
8135 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8136 && operand_equal_p (a01, a11, 0))
8137 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8138 fold_build2_loc (loc, code, type, a00, a10),
8139 a01);
8140 }
8141
8142 /* See if we can build a range comparison. */
8143 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8144 return tem;
8145
8146 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8147 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8148 {
8149 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8150 if (tem)
8151 return fold_build2_loc (loc, code, type, tem, arg1);
8152 }
8153
8154 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8155 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8156 {
8157 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8158 if (tem)
8159 return fold_build2_loc (loc, code, type, arg0, tem);
8160 }
8161
8162 /* Check for the possibility of merging component references. If our
8163 lhs is another similar operation, try to merge its rhs with our
8164 rhs. Then try to merge our lhs and rhs. */
8165 if (TREE_CODE (arg0) == code
8166 && (tem = fold_truth_andor_1 (loc, code, type,
8167 TREE_OPERAND (arg0, 1), arg1)) != 0)
8168 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8169
8170 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8171 return tem;
8172
8173 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8174 && !flag_sanitize_coverage
8175 && (code == TRUTH_AND_EXPR
8176 || code == TRUTH_ANDIF_EXPR
8177 || code == TRUTH_OR_EXPR
8178 || code == TRUTH_ORIF_EXPR))
8179 {
8180 enum tree_code ncode, icode;
8181
8182 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8183 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8184 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8185
8186 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8187 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8188 We don't want to pack more than two leafs to a non-IF AND/OR
8189 expression.
8190 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8191 equal to IF-CODE, then we don't want to add right-hand operand.
8192 If the inner right-hand side of left-hand operand has
8193 side-effects, or isn't simple, then we can't add to it,
8194 as otherwise we might destroy if-sequence. */
8195 if (TREE_CODE (arg0) == icode
8196 && simple_operand_p_2 (arg1)
8197 /* Needed for sequence points to handle trappings, and
8198 side-effects. */
8199 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8200 {
8201 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8202 arg1);
8203 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8204 tem);
8205 }
8206 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8207 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8208 else if (TREE_CODE (arg1) == icode
8209 && simple_operand_p_2 (arg0)
8210 /* Needed for sequence points to handle trappings, and
8211 side-effects. */
8212 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8213 {
8214 tem = fold_build2_loc (loc, ncode, type,
8215 arg0, TREE_OPERAND (arg1, 0));
8216 return fold_build2_loc (loc, icode, type, tem,
8217 TREE_OPERAND (arg1, 1));
8218 }
8219 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8220 into (A OR B).
8221 For sequence point consistancy, we need to check for trapping,
8222 and side-effects. */
8223 else if (code == icode && simple_operand_p_2 (arg0)
8224 && simple_operand_p_2 (arg1))
8225 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8226 }
8227
8228 return NULL_TREE;
8229 }
8230
8231 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8232 by changing CODE to reduce the magnitude of constants involved in
8233 ARG0 of the comparison.
8234 Returns a canonicalized comparison tree if a simplification was
8235 possible, otherwise returns NULL_TREE.
8236 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8237 valid if signed overflow is undefined. */
8238
8239 static tree
8240 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8241 tree arg0, tree arg1,
8242 bool *strict_overflow_p)
8243 {
8244 enum tree_code code0 = TREE_CODE (arg0);
8245 tree t, cst0 = NULL_TREE;
8246 int sgn0;
8247
8248 /* Match A +- CST code arg1. We can change this only if overflow
8249 is undefined. */
8250 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8251 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8252 /* In principle pointers also have undefined overflow behavior,
8253 but that causes problems elsewhere. */
8254 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8255 && (code0 == MINUS_EXPR
8256 || code0 == PLUS_EXPR)
8257 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8258 return NULL_TREE;
8259
8260 /* Identify the constant in arg0 and its sign. */
8261 cst0 = TREE_OPERAND (arg0, 1);
8262 sgn0 = tree_int_cst_sgn (cst0);
8263
8264 /* Overflowed constants and zero will cause problems. */
8265 if (integer_zerop (cst0)
8266 || TREE_OVERFLOW (cst0))
8267 return NULL_TREE;
8268
8269 /* See if we can reduce the magnitude of the constant in
8270 arg0 by changing the comparison code. */
8271 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8272 if (code == LT_EXPR
8273 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8274 code = LE_EXPR;
8275 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8276 else if (code == GT_EXPR
8277 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8278 code = GE_EXPR;
8279 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8280 else if (code == LE_EXPR
8281 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8282 code = LT_EXPR;
8283 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8284 else if (code == GE_EXPR
8285 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8286 code = GT_EXPR;
8287 else
8288 return NULL_TREE;
8289 *strict_overflow_p = true;
8290
8291 /* Now build the constant reduced in magnitude. But not if that
8292 would produce one outside of its types range. */
8293 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8294 && ((sgn0 == 1
8295 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8296 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8297 || (sgn0 == -1
8298 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8299 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8300 return NULL_TREE;
8301
8302 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8303 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8304 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8305 t = fold_convert (TREE_TYPE (arg1), t);
8306
8307 return fold_build2_loc (loc, code, type, t, arg1);
8308 }
8309
8310 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8311 overflow further. Try to decrease the magnitude of constants involved
8312 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8313 and put sole constants at the second argument position.
8314 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8315
8316 static tree
8317 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8318 tree arg0, tree arg1)
8319 {
8320 tree t;
8321 bool strict_overflow_p;
8322 const char * const warnmsg = G_("assuming signed overflow does not occur "
8323 "when reducing constant in comparison");
8324
8325 /* Try canonicalization by simplifying arg0. */
8326 strict_overflow_p = false;
8327 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8328 &strict_overflow_p);
8329 if (t)
8330 {
8331 if (strict_overflow_p)
8332 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8333 return t;
8334 }
8335
8336 /* Try canonicalization by simplifying arg1 using the swapped
8337 comparison. */
8338 code = swap_tree_comparison (code);
8339 strict_overflow_p = false;
8340 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8341 &strict_overflow_p);
8342 if (t && strict_overflow_p)
8343 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8344 return t;
8345 }
8346
8347 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8348 space. This is used to avoid issuing overflow warnings for
8349 expressions like &p->x which can not wrap. */
8350
8351 static bool
8352 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8353 {
8354 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8355 return true;
8356
8357 if (maybe_lt (bitpos, 0))
8358 return true;
8359
8360 poly_wide_int wi_offset;
8361 int precision = TYPE_PRECISION (TREE_TYPE (base));
8362 if (offset == NULL_TREE)
8363 wi_offset = wi::zero (precision);
8364 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8365 return true;
8366 else
8367 wi_offset = wi::to_poly_wide (offset);
8368
8369 bool overflow;
8370 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8371 precision);
8372 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8373 if (overflow)
8374 return true;
8375
8376 poly_uint64 total_hwi, size;
8377 if (!total.to_uhwi (&total_hwi)
8378 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8379 &size)
8380 || known_eq (size, 0U))
8381 return true;
8382
8383 if (known_le (total_hwi, size))
8384 return false;
8385
8386 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8387 array. */
8388 if (TREE_CODE (base) == ADDR_EXPR
8389 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8390 &size)
8391 && maybe_ne (size, 0U)
8392 && known_le (total_hwi, size))
8393 return false;
8394
8395 return true;
8396 }
8397
8398 /* Return a positive integer when the symbol DECL is known to have
8399 a nonzero address, zero when it's known not to (e.g., it's a weak
8400 symbol), and a negative integer when the symbol is not yet in the
8401 symbol table and so whether or not its address is zero is unknown.
8402 For function local objects always return positive integer. */
8403 static int
8404 maybe_nonzero_address (tree decl)
8405 {
8406 if (DECL_P (decl) && decl_in_symtab_p (decl))
8407 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8408 return symbol->nonzero_address ();
8409
8410 /* Function local objects are never NULL. */
8411 if (DECL_P (decl)
8412 && (DECL_CONTEXT (decl)
8413 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8414 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8415 return 1;
8416
8417 return -1;
8418 }
8419
8420 /* Subroutine of fold_binary. This routine performs all of the
8421 transformations that are common to the equality/inequality
8422 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8423 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8424 fold_binary should call fold_binary. Fold a comparison with
8425 tree code CODE and type TYPE with operands OP0 and OP1. Return
8426 the folded comparison or NULL_TREE. */
8427
8428 static tree
8429 fold_comparison (location_t loc, enum tree_code code, tree type,
8430 tree op0, tree op1)
8431 {
8432 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8433 tree arg0, arg1, tem;
8434
8435 arg0 = op0;
8436 arg1 = op1;
8437
8438 STRIP_SIGN_NOPS (arg0);
8439 STRIP_SIGN_NOPS (arg1);
8440
8441 /* For comparisons of pointers we can decompose it to a compile time
8442 comparison of the base objects and the offsets into the object.
8443 This requires at least one operand being an ADDR_EXPR or a
8444 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8445 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8446 && (TREE_CODE (arg0) == ADDR_EXPR
8447 || TREE_CODE (arg1) == ADDR_EXPR
8448 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8449 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8450 {
8451 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8452 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8453 machine_mode mode;
8454 int volatilep, reversep, unsignedp;
8455 bool indirect_base0 = false, indirect_base1 = false;
8456
8457 /* Get base and offset for the access. Strip ADDR_EXPR for
8458 get_inner_reference, but put it back by stripping INDIRECT_REF
8459 off the base object if possible. indirect_baseN will be true
8460 if baseN is not an address but refers to the object itself. */
8461 base0 = arg0;
8462 if (TREE_CODE (arg0) == ADDR_EXPR)
8463 {
8464 base0
8465 = get_inner_reference (TREE_OPERAND (arg0, 0),
8466 &bitsize, &bitpos0, &offset0, &mode,
8467 &unsignedp, &reversep, &volatilep);
8468 if (TREE_CODE (base0) == INDIRECT_REF)
8469 base0 = TREE_OPERAND (base0, 0);
8470 else
8471 indirect_base0 = true;
8472 }
8473 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8474 {
8475 base0 = TREE_OPERAND (arg0, 0);
8476 STRIP_SIGN_NOPS (base0);
8477 if (TREE_CODE (base0) == ADDR_EXPR)
8478 {
8479 base0
8480 = get_inner_reference (TREE_OPERAND (base0, 0),
8481 &bitsize, &bitpos0, &offset0, &mode,
8482 &unsignedp, &reversep, &volatilep);
8483 if (TREE_CODE (base0) == INDIRECT_REF)
8484 base0 = TREE_OPERAND (base0, 0);
8485 else
8486 indirect_base0 = true;
8487 }
8488 if (offset0 == NULL_TREE || integer_zerop (offset0))
8489 offset0 = TREE_OPERAND (arg0, 1);
8490 else
8491 offset0 = size_binop (PLUS_EXPR, offset0,
8492 TREE_OPERAND (arg0, 1));
8493 if (poly_int_tree_p (offset0))
8494 {
8495 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8496 TYPE_PRECISION (sizetype));
8497 tem <<= LOG2_BITS_PER_UNIT;
8498 tem += bitpos0;
8499 if (tem.to_shwi (&bitpos0))
8500 offset0 = NULL_TREE;
8501 }
8502 }
8503
8504 base1 = arg1;
8505 if (TREE_CODE (arg1) == ADDR_EXPR)
8506 {
8507 base1
8508 = get_inner_reference (TREE_OPERAND (arg1, 0),
8509 &bitsize, &bitpos1, &offset1, &mode,
8510 &unsignedp, &reversep, &volatilep);
8511 if (TREE_CODE (base1) == INDIRECT_REF)
8512 base1 = TREE_OPERAND (base1, 0);
8513 else
8514 indirect_base1 = true;
8515 }
8516 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8517 {
8518 base1 = TREE_OPERAND (arg1, 0);
8519 STRIP_SIGN_NOPS (base1);
8520 if (TREE_CODE (base1) == ADDR_EXPR)
8521 {
8522 base1
8523 = get_inner_reference (TREE_OPERAND (base1, 0),
8524 &bitsize, &bitpos1, &offset1, &mode,
8525 &unsignedp, &reversep, &volatilep);
8526 if (TREE_CODE (base1) == INDIRECT_REF)
8527 base1 = TREE_OPERAND (base1, 0);
8528 else
8529 indirect_base1 = true;
8530 }
8531 if (offset1 == NULL_TREE || integer_zerop (offset1))
8532 offset1 = TREE_OPERAND (arg1, 1);
8533 else
8534 offset1 = size_binop (PLUS_EXPR, offset1,
8535 TREE_OPERAND (arg1, 1));
8536 if (poly_int_tree_p (offset1))
8537 {
8538 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8539 TYPE_PRECISION (sizetype));
8540 tem <<= LOG2_BITS_PER_UNIT;
8541 tem += bitpos1;
8542 if (tem.to_shwi (&bitpos1))
8543 offset1 = NULL_TREE;
8544 }
8545 }
8546
8547 /* If we have equivalent bases we might be able to simplify. */
8548 if (indirect_base0 == indirect_base1
8549 && operand_equal_p (base0, base1,
8550 indirect_base0 ? OEP_ADDRESS_OF : 0))
8551 {
8552 /* We can fold this expression to a constant if the non-constant
8553 offset parts are equal. */
8554 if (offset0 == offset1
8555 || (offset0 && offset1
8556 && operand_equal_p (offset0, offset1, 0)))
8557 {
8558 if (!equality_code
8559 && maybe_ne (bitpos0, bitpos1)
8560 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8561 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8562 fold_overflow_warning (("assuming pointer wraparound does not "
8563 "occur when comparing P +- C1 with "
8564 "P +- C2"),
8565 WARN_STRICT_OVERFLOW_CONDITIONAL);
8566
8567 switch (code)
8568 {
8569 case EQ_EXPR:
8570 if (known_eq (bitpos0, bitpos1))
8571 return boolean_true_node;
8572 if (known_ne (bitpos0, bitpos1))
8573 return boolean_false_node;
8574 break;
8575 case NE_EXPR:
8576 if (known_ne (bitpos0, bitpos1))
8577 return boolean_true_node;
8578 if (known_eq (bitpos0, bitpos1))
8579 return boolean_false_node;
8580 break;
8581 case LT_EXPR:
8582 if (known_lt (bitpos0, bitpos1))
8583 return boolean_true_node;
8584 if (known_ge (bitpos0, bitpos1))
8585 return boolean_false_node;
8586 break;
8587 case LE_EXPR:
8588 if (known_le (bitpos0, bitpos1))
8589 return boolean_true_node;
8590 if (known_gt (bitpos0, bitpos1))
8591 return boolean_false_node;
8592 break;
8593 case GE_EXPR:
8594 if (known_ge (bitpos0, bitpos1))
8595 return boolean_true_node;
8596 if (known_lt (bitpos0, bitpos1))
8597 return boolean_false_node;
8598 break;
8599 case GT_EXPR:
8600 if (known_gt (bitpos0, bitpos1))
8601 return boolean_true_node;
8602 if (known_le (bitpos0, bitpos1))
8603 return boolean_false_node;
8604 break;
8605 default:;
8606 }
8607 }
8608 /* We can simplify the comparison to a comparison of the variable
8609 offset parts if the constant offset parts are equal.
8610 Be careful to use signed sizetype here because otherwise we
8611 mess with array offsets in the wrong way. This is possible
8612 because pointer arithmetic is restricted to retain within an
8613 object and overflow on pointer differences is undefined as of
8614 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8615 else if (known_eq (bitpos0, bitpos1))
8616 {
8617 /* By converting to signed sizetype we cover middle-end pointer
8618 arithmetic which operates on unsigned pointer types of size
8619 type size and ARRAY_REF offsets which are properly sign or
8620 zero extended from their type in case it is narrower than
8621 sizetype. */
8622 if (offset0 == NULL_TREE)
8623 offset0 = build_int_cst (ssizetype, 0);
8624 else
8625 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8626 if (offset1 == NULL_TREE)
8627 offset1 = build_int_cst (ssizetype, 0);
8628 else
8629 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8630
8631 if (!equality_code
8632 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8633 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8634 fold_overflow_warning (("assuming pointer wraparound does not "
8635 "occur when comparing P +- C1 with "
8636 "P +- C2"),
8637 WARN_STRICT_OVERFLOW_COMPARISON);
8638
8639 return fold_build2_loc (loc, code, type, offset0, offset1);
8640 }
8641 }
8642 /* For equal offsets we can simplify to a comparison of the
8643 base addresses. */
8644 else if (known_eq (bitpos0, bitpos1)
8645 && (indirect_base0
8646 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8647 && (indirect_base1
8648 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8649 && ((offset0 == offset1)
8650 || (offset0 && offset1
8651 && operand_equal_p (offset0, offset1, 0))))
8652 {
8653 if (indirect_base0)
8654 base0 = build_fold_addr_expr_loc (loc, base0);
8655 if (indirect_base1)
8656 base1 = build_fold_addr_expr_loc (loc, base1);
8657 return fold_build2_loc (loc, code, type, base0, base1);
8658 }
8659 /* Comparison between an ordinary (non-weak) symbol and a null
8660 pointer can be eliminated since such symbols must have a non
8661 null address. In C, relational expressions between pointers
8662 to objects and null pointers are undefined. The results
8663 below follow the C++ rules with the additional property that
8664 every object pointer compares greater than a null pointer.
8665 */
8666 else if (((DECL_P (base0)
8667 && maybe_nonzero_address (base0) > 0
8668 /* Avoid folding references to struct members at offset 0 to
8669 prevent tests like '&ptr->firstmember == 0' from getting
8670 eliminated. When ptr is null, although the -> expression
8671 is strictly speaking invalid, GCC retains it as a matter
8672 of QoI. See PR c/44555. */
8673 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8674 || CONSTANT_CLASS_P (base0))
8675 && indirect_base0
8676 /* The caller guarantees that when one of the arguments is
8677 constant (i.e., null in this case) it is second. */
8678 && integer_zerop (arg1))
8679 {
8680 switch (code)
8681 {
8682 case EQ_EXPR:
8683 case LE_EXPR:
8684 case LT_EXPR:
8685 return constant_boolean_node (false, type);
8686 case GE_EXPR:
8687 case GT_EXPR:
8688 case NE_EXPR:
8689 return constant_boolean_node (true, type);
8690 default:
8691 gcc_unreachable ();
8692 }
8693 }
8694 }
8695
8696 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8697 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8698 the resulting offset is smaller in absolute value than the
8699 original one and has the same sign. */
8700 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8701 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8702 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8703 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8704 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8705 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8706 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8707 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8708 {
8709 tree const1 = TREE_OPERAND (arg0, 1);
8710 tree const2 = TREE_OPERAND (arg1, 1);
8711 tree variable1 = TREE_OPERAND (arg0, 0);
8712 tree variable2 = TREE_OPERAND (arg1, 0);
8713 tree cst;
8714 const char * const warnmsg = G_("assuming signed overflow does not "
8715 "occur when combining constants around "
8716 "a comparison");
8717
8718 /* Put the constant on the side where it doesn't overflow and is
8719 of lower absolute value and of same sign than before. */
8720 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8721 ? MINUS_EXPR : PLUS_EXPR,
8722 const2, const1);
8723 if (!TREE_OVERFLOW (cst)
8724 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8725 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8726 {
8727 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8728 return fold_build2_loc (loc, code, type,
8729 variable1,
8730 fold_build2_loc (loc, TREE_CODE (arg1),
8731 TREE_TYPE (arg1),
8732 variable2, cst));
8733 }
8734
8735 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8736 ? MINUS_EXPR : PLUS_EXPR,
8737 const1, const2);
8738 if (!TREE_OVERFLOW (cst)
8739 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8740 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8741 {
8742 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8743 return fold_build2_loc (loc, code, type,
8744 fold_build2_loc (loc, TREE_CODE (arg0),
8745 TREE_TYPE (arg0),
8746 variable1, cst),
8747 variable2);
8748 }
8749 }
8750
8751 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8752 if (tem)
8753 return tem;
8754
8755 /* If we are comparing an expression that just has comparisons
8756 of two integer values, arithmetic expressions of those comparisons,
8757 and constants, we can simplify it. There are only three cases
8758 to check: the two values can either be equal, the first can be
8759 greater, or the second can be greater. Fold the expression for
8760 those three values. Since each value must be 0 or 1, we have
8761 eight possibilities, each of which corresponds to the constant 0
8762 or 1 or one of the six possible comparisons.
8763
8764 This handles common cases like (a > b) == 0 but also handles
8765 expressions like ((x > y) - (y > x)) > 0, which supposedly
8766 occur in macroized code. */
8767
8768 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8769 {
8770 tree cval1 = 0, cval2 = 0;
8771 int save_p = 0;
8772
8773 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8774 /* Don't handle degenerate cases here; they should already
8775 have been handled anyway. */
8776 && cval1 != 0 && cval2 != 0
8777 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8778 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8779 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8780 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8781 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8782 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8783 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8784 {
8785 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8786 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8787
8788 /* We can't just pass T to eval_subst in case cval1 or cval2
8789 was the same as ARG1. */
8790
8791 tree high_result
8792 = fold_build2_loc (loc, code, type,
8793 eval_subst (loc, arg0, cval1, maxval,
8794 cval2, minval),
8795 arg1);
8796 tree equal_result
8797 = fold_build2_loc (loc, code, type,
8798 eval_subst (loc, arg0, cval1, maxval,
8799 cval2, maxval),
8800 arg1);
8801 tree low_result
8802 = fold_build2_loc (loc, code, type,
8803 eval_subst (loc, arg0, cval1, minval,
8804 cval2, maxval),
8805 arg1);
8806
8807 /* All three of these results should be 0 or 1. Confirm they are.
8808 Then use those values to select the proper code to use. */
8809
8810 if (TREE_CODE (high_result) == INTEGER_CST
8811 && TREE_CODE (equal_result) == INTEGER_CST
8812 && TREE_CODE (low_result) == INTEGER_CST)
8813 {
8814 /* Make a 3-bit mask with the high-order bit being the
8815 value for `>', the next for '=', and the low for '<'. */
8816 switch ((integer_onep (high_result) * 4)
8817 + (integer_onep (equal_result) * 2)
8818 + integer_onep (low_result))
8819 {
8820 case 0:
8821 /* Always false. */
8822 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8823 case 1:
8824 code = LT_EXPR;
8825 break;
8826 case 2:
8827 code = EQ_EXPR;
8828 break;
8829 case 3:
8830 code = LE_EXPR;
8831 break;
8832 case 4:
8833 code = GT_EXPR;
8834 break;
8835 case 5:
8836 code = NE_EXPR;
8837 break;
8838 case 6:
8839 code = GE_EXPR;
8840 break;
8841 case 7:
8842 /* Always true. */
8843 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8844 }
8845
8846 if (save_p)
8847 {
8848 tem = save_expr (build2 (code, type, cval1, cval2));
8849 protected_set_expr_location (tem, loc);
8850 return tem;
8851 }
8852 return fold_build2_loc (loc, code, type, cval1, cval2);
8853 }
8854 }
8855 }
8856
8857 return NULL_TREE;
8858 }
8859
8860
8861 /* Subroutine of fold_binary. Optimize complex multiplications of the
8862 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8863 argument EXPR represents the expression "z" of type TYPE. */
8864
8865 static tree
8866 fold_mult_zconjz (location_t loc, tree type, tree expr)
8867 {
8868 tree itype = TREE_TYPE (type);
8869 tree rpart, ipart, tem;
8870
8871 if (TREE_CODE (expr) == COMPLEX_EXPR)
8872 {
8873 rpart = TREE_OPERAND (expr, 0);
8874 ipart = TREE_OPERAND (expr, 1);
8875 }
8876 else if (TREE_CODE (expr) == COMPLEX_CST)
8877 {
8878 rpart = TREE_REALPART (expr);
8879 ipart = TREE_IMAGPART (expr);
8880 }
8881 else
8882 {
8883 expr = save_expr (expr);
8884 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8885 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8886 }
8887
8888 rpart = save_expr (rpart);
8889 ipart = save_expr (ipart);
8890 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8891 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8892 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8893 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8894 build_zero_cst (itype));
8895 }
8896
8897
8898 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8899 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8900 true if successful. */
8901
8902 static bool
8903 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8904 {
8905 unsigned HOST_WIDE_INT i, nunits;
8906
8907 if (TREE_CODE (arg) == VECTOR_CST
8908 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8909 {
8910 for (i = 0; i < nunits; ++i)
8911 elts[i] = VECTOR_CST_ELT (arg, i);
8912 }
8913 else if (TREE_CODE (arg) == CONSTRUCTOR)
8914 {
8915 constructor_elt *elt;
8916
8917 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8918 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8919 return false;
8920 else
8921 elts[i] = elt->value;
8922 }
8923 else
8924 return false;
8925 for (; i < nelts; i++)
8926 elts[i]
8927 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8928 return true;
8929 }
8930
8931 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8932 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8933 NULL_TREE otherwise. */
8934
8935 static tree
8936 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8937 {
8938 unsigned int i;
8939 unsigned HOST_WIDE_INT nelts;
8940 bool need_ctor = false;
8941
8942 if (!sel.length ().is_constant (&nelts))
8943 return NULL_TREE;
8944 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8945 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8946 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8947 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8948 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8949 return NULL_TREE;
8950
8951 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8952 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8953 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8954 return NULL_TREE;
8955
8956 tree_vector_builder out_elts (type, nelts, 1);
8957 for (i = 0; i < nelts; i++)
8958 {
8959 HOST_WIDE_INT index;
8960 if (!sel[i].is_constant (&index))
8961 return NULL_TREE;
8962 if (!CONSTANT_CLASS_P (in_elts[index]))
8963 need_ctor = true;
8964 out_elts.quick_push (unshare_expr (in_elts[index]));
8965 }
8966
8967 if (need_ctor)
8968 {
8969 vec<constructor_elt, va_gc> *v;
8970 vec_alloc (v, nelts);
8971 for (i = 0; i < nelts; i++)
8972 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8973 return build_constructor (type, v);
8974 }
8975 else
8976 return out_elts.build ();
8977 }
8978
8979 /* Try to fold a pointer difference of type TYPE two address expressions of
8980 array references AREF0 and AREF1 using location LOC. Return a
8981 simplified expression for the difference or NULL_TREE. */
8982
8983 static tree
8984 fold_addr_of_array_ref_difference (location_t loc, tree type,
8985 tree aref0, tree aref1,
8986 bool use_pointer_diff)
8987 {
8988 tree base0 = TREE_OPERAND (aref0, 0);
8989 tree base1 = TREE_OPERAND (aref1, 0);
8990 tree base_offset = build_int_cst (type, 0);
8991
8992 /* If the bases are array references as well, recurse. If the bases
8993 are pointer indirections compute the difference of the pointers.
8994 If the bases are equal, we are set. */
8995 if ((TREE_CODE (base0) == ARRAY_REF
8996 && TREE_CODE (base1) == ARRAY_REF
8997 && (base_offset
8998 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
8999 use_pointer_diff)))
9000 || (INDIRECT_REF_P (base0)
9001 && INDIRECT_REF_P (base1)
9002 && (base_offset
9003 = use_pointer_diff
9004 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9005 TREE_OPERAND (base0, 0),
9006 TREE_OPERAND (base1, 0))
9007 : fold_binary_loc (loc, MINUS_EXPR, type,
9008 fold_convert (type,
9009 TREE_OPERAND (base0, 0)),
9010 fold_convert (type,
9011 TREE_OPERAND (base1, 0)))))
9012 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9013 {
9014 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9015 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9016 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9017 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9018 return fold_build2_loc (loc, PLUS_EXPR, type,
9019 base_offset,
9020 fold_build2_loc (loc, MULT_EXPR, type,
9021 diff, esz));
9022 }
9023 return NULL_TREE;
9024 }
9025
9026 /* If the real or vector real constant CST of type TYPE has an exact
9027 inverse, return it, else return NULL. */
9028
9029 tree
9030 exact_inverse (tree type, tree cst)
9031 {
9032 REAL_VALUE_TYPE r;
9033 tree unit_type;
9034 machine_mode mode;
9035
9036 switch (TREE_CODE (cst))
9037 {
9038 case REAL_CST:
9039 r = TREE_REAL_CST (cst);
9040
9041 if (exact_real_inverse (TYPE_MODE (type), &r))
9042 return build_real (type, r);
9043
9044 return NULL_TREE;
9045
9046 case VECTOR_CST:
9047 {
9048 unit_type = TREE_TYPE (type);
9049 mode = TYPE_MODE (unit_type);
9050
9051 tree_vector_builder elts;
9052 if (!elts.new_unary_operation (type, cst, false))
9053 return NULL_TREE;
9054 unsigned int count = elts.encoded_nelts ();
9055 for (unsigned int i = 0; i < count; ++i)
9056 {
9057 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9058 if (!exact_real_inverse (mode, &r))
9059 return NULL_TREE;
9060 elts.quick_push (build_real (unit_type, r));
9061 }
9062
9063 return elts.build ();
9064 }
9065
9066 default:
9067 return NULL_TREE;
9068 }
9069 }
9070
9071 /* Mask out the tz least significant bits of X of type TYPE where
9072 tz is the number of trailing zeroes in Y. */
9073 static wide_int
9074 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9075 {
9076 int tz = wi::ctz (y);
9077 if (tz > 0)
9078 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9079 return x;
9080 }
9081
9082 /* Return true when T is an address and is known to be nonzero.
9083 For floating point we further ensure that T is not denormal.
9084 Similar logic is present in nonzero_address in rtlanal.h.
9085
9086 If the return value is based on the assumption that signed overflow
9087 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9088 change *STRICT_OVERFLOW_P. */
9089
9090 static bool
9091 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9092 {
9093 tree type = TREE_TYPE (t);
9094 enum tree_code code;
9095
9096 /* Doing something useful for floating point would need more work. */
9097 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9098 return false;
9099
9100 code = TREE_CODE (t);
9101 switch (TREE_CODE_CLASS (code))
9102 {
9103 case tcc_unary:
9104 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9105 strict_overflow_p);
9106 case tcc_binary:
9107 case tcc_comparison:
9108 return tree_binary_nonzero_warnv_p (code, type,
9109 TREE_OPERAND (t, 0),
9110 TREE_OPERAND (t, 1),
9111 strict_overflow_p);
9112 case tcc_constant:
9113 case tcc_declaration:
9114 case tcc_reference:
9115 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9116
9117 default:
9118 break;
9119 }
9120
9121 switch (code)
9122 {
9123 case TRUTH_NOT_EXPR:
9124 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9125 strict_overflow_p);
9126
9127 case TRUTH_AND_EXPR:
9128 case TRUTH_OR_EXPR:
9129 case TRUTH_XOR_EXPR:
9130 return tree_binary_nonzero_warnv_p (code, type,
9131 TREE_OPERAND (t, 0),
9132 TREE_OPERAND (t, 1),
9133 strict_overflow_p);
9134
9135 case COND_EXPR:
9136 case CONSTRUCTOR:
9137 case OBJ_TYPE_REF:
9138 case ASSERT_EXPR:
9139 case ADDR_EXPR:
9140 case WITH_SIZE_EXPR:
9141 case SSA_NAME:
9142 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9143
9144 case COMPOUND_EXPR:
9145 case MODIFY_EXPR:
9146 case BIND_EXPR:
9147 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9148 strict_overflow_p);
9149
9150 case SAVE_EXPR:
9151 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9152 strict_overflow_p);
9153
9154 case CALL_EXPR:
9155 {
9156 tree fndecl = get_callee_fndecl (t);
9157 if (!fndecl) return false;
9158 if (flag_delete_null_pointer_checks && !flag_check_new
9159 && DECL_IS_OPERATOR_NEW (fndecl)
9160 && !TREE_NOTHROW (fndecl))
9161 return true;
9162 if (flag_delete_null_pointer_checks
9163 && lookup_attribute ("returns_nonnull",
9164 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9165 return true;
9166 return alloca_call_p (t);
9167 }
9168
9169 default:
9170 break;
9171 }
9172 return false;
9173 }
9174
9175 /* Return true when T is an address and is known to be nonzero.
9176 Handle warnings about undefined signed overflow. */
9177
9178 bool
9179 tree_expr_nonzero_p (tree t)
9180 {
9181 bool ret, strict_overflow_p;
9182
9183 strict_overflow_p = false;
9184 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9185 if (strict_overflow_p)
9186 fold_overflow_warning (("assuming signed overflow does not occur when "
9187 "determining that expression is always "
9188 "non-zero"),
9189 WARN_STRICT_OVERFLOW_MISC);
9190 return ret;
9191 }
9192
9193 /* Return true if T is known not to be equal to an integer W. */
9194
9195 bool
9196 expr_not_equal_to (tree t, const wide_int &w)
9197 {
9198 wide_int min, max, nz;
9199 value_range_type rtype;
9200 switch (TREE_CODE (t))
9201 {
9202 case INTEGER_CST:
9203 return wi::to_wide (t) != w;
9204
9205 case SSA_NAME:
9206 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9207 return false;
9208 rtype = get_range_info (t, &min, &max);
9209 if (rtype == VR_RANGE)
9210 {
9211 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9212 return true;
9213 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9214 return true;
9215 }
9216 else if (rtype == VR_ANTI_RANGE
9217 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9218 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9219 return true;
9220 /* If T has some known zero bits and W has any of those bits set,
9221 then T is known not to be equal to W. */
9222 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9223 TYPE_PRECISION (TREE_TYPE (t))), 0))
9224 return true;
9225 return false;
9226
9227 default:
9228 return false;
9229 }
9230 }
9231
9232 /* Fold a binary expression of code CODE and type TYPE with operands
9233 OP0 and OP1. LOC is the location of the resulting expression.
9234 Return the folded expression if folding is successful. Otherwise,
9235 return NULL_TREE. */
9236
9237 tree
9238 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9239 tree op0, tree op1)
9240 {
9241 enum tree_code_class kind = TREE_CODE_CLASS (code);
9242 tree arg0, arg1, tem;
9243 tree t1 = NULL_TREE;
9244 bool strict_overflow_p;
9245 unsigned int prec;
9246
9247 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9248 && TREE_CODE_LENGTH (code) == 2
9249 && op0 != NULL_TREE
9250 && op1 != NULL_TREE);
9251
9252 arg0 = op0;
9253 arg1 = op1;
9254
9255 /* Strip any conversions that don't change the mode. This is
9256 safe for every expression, except for a comparison expression
9257 because its signedness is derived from its operands. So, in
9258 the latter case, only strip conversions that don't change the
9259 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9260 preserved.
9261
9262 Note that this is done as an internal manipulation within the
9263 constant folder, in order to find the simplest representation
9264 of the arguments so that their form can be studied. In any
9265 cases, the appropriate type conversions should be put back in
9266 the tree that will get out of the constant folder. */
9267
9268 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9269 {
9270 STRIP_SIGN_NOPS (arg0);
9271 STRIP_SIGN_NOPS (arg1);
9272 }
9273 else
9274 {
9275 STRIP_NOPS (arg0);
9276 STRIP_NOPS (arg1);
9277 }
9278
9279 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9280 constant but we can't do arithmetic on them. */
9281 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9282 {
9283 tem = const_binop (code, type, arg0, arg1);
9284 if (tem != NULL_TREE)
9285 {
9286 if (TREE_TYPE (tem) != type)
9287 tem = fold_convert_loc (loc, type, tem);
9288 return tem;
9289 }
9290 }
9291
9292 /* If this is a commutative operation, and ARG0 is a constant, move it
9293 to ARG1 to reduce the number of tests below. */
9294 if (commutative_tree_code (code)
9295 && tree_swap_operands_p (arg0, arg1))
9296 return fold_build2_loc (loc, code, type, op1, op0);
9297
9298 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9299 to ARG1 to reduce the number of tests below. */
9300 if (kind == tcc_comparison
9301 && tree_swap_operands_p (arg0, arg1))
9302 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9303
9304 tem = generic_simplify (loc, code, type, op0, op1);
9305 if (tem)
9306 return tem;
9307
9308 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9309
9310 First check for cases where an arithmetic operation is applied to a
9311 compound, conditional, or comparison operation. Push the arithmetic
9312 operation inside the compound or conditional to see if any folding
9313 can then be done. Convert comparison to conditional for this purpose.
9314 The also optimizes non-constant cases that used to be done in
9315 expand_expr.
9316
9317 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9318 one of the operands is a comparison and the other is a comparison, a
9319 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9320 code below would make the expression more complex. Change it to a
9321 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9322 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9323
9324 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9325 || code == EQ_EXPR || code == NE_EXPR)
9326 && TREE_CODE (type) != VECTOR_TYPE
9327 && ((truth_value_p (TREE_CODE (arg0))
9328 && (truth_value_p (TREE_CODE (arg1))
9329 || (TREE_CODE (arg1) == BIT_AND_EXPR
9330 && integer_onep (TREE_OPERAND (arg1, 1)))))
9331 || (truth_value_p (TREE_CODE (arg1))
9332 && (truth_value_p (TREE_CODE (arg0))
9333 || (TREE_CODE (arg0) == BIT_AND_EXPR
9334 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9335 {
9336 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9337 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9338 : TRUTH_XOR_EXPR,
9339 boolean_type_node,
9340 fold_convert_loc (loc, boolean_type_node, arg0),
9341 fold_convert_loc (loc, boolean_type_node, arg1));
9342
9343 if (code == EQ_EXPR)
9344 tem = invert_truthvalue_loc (loc, tem);
9345
9346 return fold_convert_loc (loc, type, tem);
9347 }
9348
9349 if (TREE_CODE_CLASS (code) == tcc_binary
9350 || TREE_CODE_CLASS (code) == tcc_comparison)
9351 {
9352 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9353 {
9354 tem = fold_build2_loc (loc, code, type,
9355 fold_convert_loc (loc, TREE_TYPE (op0),
9356 TREE_OPERAND (arg0, 1)), op1);
9357 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9358 tem);
9359 }
9360 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9361 {
9362 tem = fold_build2_loc (loc, code, type, op0,
9363 fold_convert_loc (loc, TREE_TYPE (op1),
9364 TREE_OPERAND (arg1, 1)));
9365 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9366 tem);
9367 }
9368
9369 if (TREE_CODE (arg0) == COND_EXPR
9370 || TREE_CODE (arg0) == VEC_COND_EXPR
9371 || COMPARISON_CLASS_P (arg0))
9372 {
9373 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9374 arg0, arg1,
9375 /*cond_first_p=*/1);
9376 if (tem != NULL_TREE)
9377 return tem;
9378 }
9379
9380 if (TREE_CODE (arg1) == COND_EXPR
9381 || TREE_CODE (arg1) == VEC_COND_EXPR
9382 || COMPARISON_CLASS_P (arg1))
9383 {
9384 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9385 arg1, arg0,
9386 /*cond_first_p=*/0);
9387 if (tem != NULL_TREE)
9388 return tem;
9389 }
9390 }
9391
9392 switch (code)
9393 {
9394 case MEM_REF:
9395 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9396 if (TREE_CODE (arg0) == ADDR_EXPR
9397 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9398 {
9399 tree iref = TREE_OPERAND (arg0, 0);
9400 return fold_build2 (MEM_REF, type,
9401 TREE_OPERAND (iref, 0),
9402 int_const_binop (PLUS_EXPR, arg1,
9403 TREE_OPERAND (iref, 1)));
9404 }
9405
9406 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9407 if (TREE_CODE (arg0) == ADDR_EXPR
9408 && handled_component_p (TREE_OPERAND (arg0, 0)))
9409 {
9410 tree base;
9411 poly_int64 coffset;
9412 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9413 &coffset);
9414 if (!base)
9415 return NULL_TREE;
9416 return fold_build2 (MEM_REF, type,
9417 build_fold_addr_expr (base),
9418 int_const_binop (PLUS_EXPR, arg1,
9419 size_int (coffset)));
9420 }
9421
9422 return NULL_TREE;
9423
9424 case POINTER_PLUS_EXPR:
9425 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9426 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9427 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9428 return fold_convert_loc (loc, type,
9429 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9430 fold_convert_loc (loc, sizetype,
9431 arg1),
9432 fold_convert_loc (loc, sizetype,
9433 arg0)));
9434
9435 return NULL_TREE;
9436
9437 case PLUS_EXPR:
9438 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9439 {
9440 /* X + (X / CST) * -CST is X % CST. */
9441 if (TREE_CODE (arg1) == MULT_EXPR
9442 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9443 && operand_equal_p (arg0,
9444 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9445 {
9446 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9447 tree cst1 = TREE_OPERAND (arg1, 1);
9448 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9449 cst1, cst0);
9450 if (sum && integer_zerop (sum))
9451 return fold_convert_loc (loc, type,
9452 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9453 TREE_TYPE (arg0), arg0,
9454 cst0));
9455 }
9456 }
9457
9458 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9459 one. Make sure the type is not saturating and has the signedness of
9460 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9461 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9462 if ((TREE_CODE (arg0) == MULT_EXPR
9463 || TREE_CODE (arg1) == MULT_EXPR)
9464 && !TYPE_SATURATING (type)
9465 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9466 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9467 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9468 {
9469 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9470 if (tem)
9471 return tem;
9472 }
9473
9474 if (! FLOAT_TYPE_P (type))
9475 {
9476 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9477 (plus (plus (mult) (mult)) (foo)) so that we can
9478 take advantage of the factoring cases below. */
9479 if (ANY_INTEGRAL_TYPE_P (type)
9480 && TYPE_OVERFLOW_WRAPS (type)
9481 && (((TREE_CODE (arg0) == PLUS_EXPR
9482 || TREE_CODE (arg0) == MINUS_EXPR)
9483 && TREE_CODE (arg1) == MULT_EXPR)
9484 || ((TREE_CODE (arg1) == PLUS_EXPR
9485 || TREE_CODE (arg1) == MINUS_EXPR)
9486 && TREE_CODE (arg0) == MULT_EXPR)))
9487 {
9488 tree parg0, parg1, parg, marg;
9489 enum tree_code pcode;
9490
9491 if (TREE_CODE (arg1) == MULT_EXPR)
9492 parg = arg0, marg = arg1;
9493 else
9494 parg = arg1, marg = arg0;
9495 pcode = TREE_CODE (parg);
9496 parg0 = TREE_OPERAND (parg, 0);
9497 parg1 = TREE_OPERAND (parg, 1);
9498 STRIP_NOPS (parg0);
9499 STRIP_NOPS (parg1);
9500
9501 if (TREE_CODE (parg0) == MULT_EXPR
9502 && TREE_CODE (parg1) != MULT_EXPR)
9503 return fold_build2_loc (loc, pcode, type,
9504 fold_build2_loc (loc, PLUS_EXPR, type,
9505 fold_convert_loc (loc, type,
9506 parg0),
9507 fold_convert_loc (loc, type,
9508 marg)),
9509 fold_convert_loc (loc, type, parg1));
9510 if (TREE_CODE (parg0) != MULT_EXPR
9511 && TREE_CODE (parg1) == MULT_EXPR)
9512 return
9513 fold_build2_loc (loc, PLUS_EXPR, type,
9514 fold_convert_loc (loc, type, parg0),
9515 fold_build2_loc (loc, pcode, type,
9516 fold_convert_loc (loc, type, marg),
9517 fold_convert_loc (loc, type,
9518 parg1)));
9519 }
9520 }
9521 else
9522 {
9523 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9524 to __complex__ ( x, y ). This is not the same for SNaNs or
9525 if signed zeros are involved. */
9526 if (!HONOR_SNANS (element_mode (arg0))
9527 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9528 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9529 {
9530 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9531 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9532 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9533 bool arg0rz = false, arg0iz = false;
9534 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9535 || (arg0i && (arg0iz = real_zerop (arg0i))))
9536 {
9537 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9538 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9539 if (arg0rz && arg1i && real_zerop (arg1i))
9540 {
9541 tree rp = arg1r ? arg1r
9542 : build1 (REALPART_EXPR, rtype, arg1);
9543 tree ip = arg0i ? arg0i
9544 : build1 (IMAGPART_EXPR, rtype, arg0);
9545 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9546 }
9547 else if (arg0iz && arg1r && real_zerop (arg1r))
9548 {
9549 tree rp = arg0r ? arg0r
9550 : build1 (REALPART_EXPR, rtype, arg0);
9551 tree ip = arg1i ? arg1i
9552 : build1 (IMAGPART_EXPR, rtype, arg1);
9553 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9554 }
9555 }
9556 }
9557
9558 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9559 We associate floats only if the user has specified
9560 -fassociative-math. */
9561 if (flag_associative_math
9562 && TREE_CODE (arg1) == PLUS_EXPR
9563 && TREE_CODE (arg0) != MULT_EXPR)
9564 {
9565 tree tree10 = TREE_OPERAND (arg1, 0);
9566 tree tree11 = TREE_OPERAND (arg1, 1);
9567 if (TREE_CODE (tree11) == MULT_EXPR
9568 && TREE_CODE (tree10) == MULT_EXPR)
9569 {
9570 tree tree0;
9571 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9572 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9573 }
9574 }
9575 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9576 We associate floats only if the user has specified
9577 -fassociative-math. */
9578 if (flag_associative_math
9579 && TREE_CODE (arg0) == PLUS_EXPR
9580 && TREE_CODE (arg1) != MULT_EXPR)
9581 {
9582 tree tree00 = TREE_OPERAND (arg0, 0);
9583 tree tree01 = TREE_OPERAND (arg0, 1);
9584 if (TREE_CODE (tree01) == MULT_EXPR
9585 && TREE_CODE (tree00) == MULT_EXPR)
9586 {
9587 tree tree0;
9588 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9589 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9590 }
9591 }
9592 }
9593
9594 bit_rotate:
9595 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9596 is a rotate of A by C1 bits. */
9597 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9598 is a rotate of A by B bits.
9599 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9600 though in this case CODE must be | and not + or ^, otherwise
9601 it doesn't return A when B is 0. */
9602 {
9603 enum tree_code code0, code1;
9604 tree rtype;
9605 code0 = TREE_CODE (arg0);
9606 code1 = TREE_CODE (arg1);
9607 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9608 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9609 && operand_equal_p (TREE_OPERAND (arg0, 0),
9610 TREE_OPERAND (arg1, 0), 0)
9611 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9612 TYPE_UNSIGNED (rtype))
9613 /* Only create rotates in complete modes. Other cases are not
9614 expanded properly. */
9615 && (element_precision (rtype)
9616 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9617 {
9618 tree tree01, tree11;
9619 tree orig_tree01, orig_tree11;
9620 enum tree_code code01, code11;
9621
9622 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9623 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9624 STRIP_NOPS (tree01);
9625 STRIP_NOPS (tree11);
9626 code01 = TREE_CODE (tree01);
9627 code11 = TREE_CODE (tree11);
9628 if (code11 != MINUS_EXPR
9629 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9630 {
9631 std::swap (code0, code1);
9632 std::swap (code01, code11);
9633 std::swap (tree01, tree11);
9634 std::swap (orig_tree01, orig_tree11);
9635 }
9636 if (code01 == INTEGER_CST
9637 && code11 == INTEGER_CST
9638 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9639 == element_precision (rtype)))
9640 {
9641 tem = build2_loc (loc, LROTATE_EXPR,
9642 rtype, TREE_OPERAND (arg0, 0),
9643 code0 == LSHIFT_EXPR
9644 ? orig_tree01 : orig_tree11);
9645 return fold_convert_loc (loc, type, tem);
9646 }
9647 else if (code11 == MINUS_EXPR)
9648 {
9649 tree tree110, tree111;
9650 tree110 = TREE_OPERAND (tree11, 0);
9651 tree111 = TREE_OPERAND (tree11, 1);
9652 STRIP_NOPS (tree110);
9653 STRIP_NOPS (tree111);
9654 if (TREE_CODE (tree110) == INTEGER_CST
9655 && compare_tree_int (tree110,
9656 element_precision (rtype)) == 0
9657 && operand_equal_p (tree01, tree111, 0))
9658 {
9659 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9660 ? LROTATE_EXPR : RROTATE_EXPR),
9661 rtype, TREE_OPERAND (arg0, 0),
9662 orig_tree01);
9663 return fold_convert_loc (loc, type, tem);
9664 }
9665 }
9666 else if (code == BIT_IOR_EXPR
9667 && code11 == BIT_AND_EXPR
9668 && pow2p_hwi (element_precision (rtype)))
9669 {
9670 tree tree110, tree111;
9671 tree110 = TREE_OPERAND (tree11, 0);
9672 tree111 = TREE_OPERAND (tree11, 1);
9673 STRIP_NOPS (tree110);
9674 STRIP_NOPS (tree111);
9675 if (TREE_CODE (tree110) == NEGATE_EXPR
9676 && TREE_CODE (tree111) == INTEGER_CST
9677 && compare_tree_int (tree111,
9678 element_precision (rtype) - 1) == 0
9679 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9680 {
9681 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9682 ? LROTATE_EXPR : RROTATE_EXPR),
9683 rtype, TREE_OPERAND (arg0, 0),
9684 orig_tree01);
9685 return fold_convert_loc (loc, type, tem);
9686 }
9687 }
9688 }
9689 }
9690
9691 associate:
9692 /* In most languages, can't associate operations on floats through
9693 parentheses. Rather than remember where the parentheses were, we
9694 don't associate floats at all, unless the user has specified
9695 -fassociative-math.
9696 And, we need to make sure type is not saturating. */
9697
9698 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9699 && !TYPE_SATURATING (type))
9700 {
9701 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9702 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9703 tree atype = type;
9704 bool ok = true;
9705
9706 /* Split both trees into variables, constants, and literals. Then
9707 associate each group together, the constants with literals,
9708 then the result with variables. This increases the chances of
9709 literals being recombined later and of generating relocatable
9710 expressions for the sum of a constant and literal. */
9711 var0 = split_tree (arg0, type, code,
9712 &minus_var0, &con0, &minus_con0,
9713 &lit0, &minus_lit0, 0);
9714 var1 = split_tree (arg1, type, code,
9715 &minus_var1, &con1, &minus_con1,
9716 &lit1, &minus_lit1, code == MINUS_EXPR);
9717
9718 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9719 if (code == MINUS_EXPR)
9720 code = PLUS_EXPR;
9721
9722 /* With undefined overflow prefer doing association in a type
9723 which wraps on overflow, if that is one of the operand types. */
9724 if (POINTER_TYPE_P (type)
9725 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9726 {
9727 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9728 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9729 atype = TREE_TYPE (arg0);
9730 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9731 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9732 atype = TREE_TYPE (arg1);
9733 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9734 }
9735
9736 /* With undefined overflow we can only associate constants with one
9737 variable, and constants whose association doesn't overflow. */
9738 if (POINTER_TYPE_P (atype)
9739 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9740 {
9741 if ((var0 && var1) || (minus_var0 && minus_var1))
9742 {
9743 /* ??? If split_tree would handle NEGATE_EXPR we could
9744 simply reject these cases and the allowed cases would
9745 be the var0/minus_var1 ones. */
9746 tree tmp0 = var0 ? var0 : minus_var0;
9747 tree tmp1 = var1 ? var1 : minus_var1;
9748 bool one_neg = false;
9749
9750 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9751 {
9752 tmp0 = TREE_OPERAND (tmp0, 0);
9753 one_neg = !one_neg;
9754 }
9755 if (CONVERT_EXPR_P (tmp0)
9756 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9757 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9758 <= TYPE_PRECISION (atype)))
9759 tmp0 = TREE_OPERAND (tmp0, 0);
9760 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9761 {
9762 tmp1 = TREE_OPERAND (tmp1, 0);
9763 one_neg = !one_neg;
9764 }
9765 if (CONVERT_EXPR_P (tmp1)
9766 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9767 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9768 <= TYPE_PRECISION (atype)))
9769 tmp1 = TREE_OPERAND (tmp1, 0);
9770 /* The only case we can still associate with two variables
9771 is if they cancel out. */
9772 if (!one_neg
9773 || !operand_equal_p (tmp0, tmp1, 0))
9774 ok = false;
9775 }
9776 else if ((var0 && minus_var1
9777 && ! operand_equal_p (var0, minus_var1, 0))
9778 || (minus_var0 && var1
9779 && ! operand_equal_p (minus_var0, var1, 0)))
9780 ok = false;
9781 }
9782
9783 /* Only do something if we found more than two objects. Otherwise,
9784 nothing has changed and we risk infinite recursion. */
9785 if (ok
9786 && ((var0 != 0) + (var1 != 0)
9787 + (minus_var0 != 0) + (minus_var1 != 0)
9788 + (con0 != 0) + (con1 != 0)
9789 + (minus_con0 != 0) + (minus_con1 != 0)
9790 + (lit0 != 0) + (lit1 != 0)
9791 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9792 {
9793 var0 = associate_trees (loc, var0, var1, code, atype);
9794 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9795 code, atype);
9796 con0 = associate_trees (loc, con0, con1, code, atype);
9797 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9798 code, atype);
9799 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9800 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9801 code, atype);
9802
9803 if (minus_var0 && var0)
9804 {
9805 var0 = associate_trees (loc, var0, minus_var0,
9806 MINUS_EXPR, atype);
9807 minus_var0 = 0;
9808 }
9809 if (minus_con0 && con0)
9810 {
9811 con0 = associate_trees (loc, con0, minus_con0,
9812 MINUS_EXPR, atype);
9813 minus_con0 = 0;
9814 }
9815
9816 /* Preserve the MINUS_EXPR if the negative part of the literal is
9817 greater than the positive part. Otherwise, the multiplicative
9818 folding code (i.e extract_muldiv) may be fooled in case
9819 unsigned constants are subtracted, like in the following
9820 example: ((X*2 + 4) - 8U)/2. */
9821 if (minus_lit0 && lit0)
9822 {
9823 if (TREE_CODE (lit0) == INTEGER_CST
9824 && TREE_CODE (minus_lit0) == INTEGER_CST
9825 && tree_int_cst_lt (lit0, minus_lit0)
9826 /* But avoid ending up with only negated parts. */
9827 && (var0 || con0))
9828 {
9829 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9830 MINUS_EXPR, atype);
9831 lit0 = 0;
9832 }
9833 else
9834 {
9835 lit0 = associate_trees (loc, lit0, minus_lit0,
9836 MINUS_EXPR, atype);
9837 minus_lit0 = 0;
9838 }
9839 }
9840
9841 /* Don't introduce overflows through reassociation. */
9842 if ((lit0 && TREE_OVERFLOW_P (lit0))
9843 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9844 return NULL_TREE;
9845
9846 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9847 con0 = associate_trees (loc, con0, lit0, code, atype);
9848 lit0 = 0;
9849 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9850 code, atype);
9851 minus_lit0 = 0;
9852
9853 /* Eliminate minus_con0. */
9854 if (minus_con0)
9855 {
9856 if (con0)
9857 con0 = associate_trees (loc, con0, minus_con0,
9858 MINUS_EXPR, atype);
9859 else if (var0)
9860 var0 = associate_trees (loc, var0, minus_con0,
9861 MINUS_EXPR, atype);
9862 else
9863 gcc_unreachable ();
9864 minus_con0 = 0;
9865 }
9866
9867 /* Eliminate minus_var0. */
9868 if (minus_var0)
9869 {
9870 if (con0)
9871 con0 = associate_trees (loc, con0, minus_var0,
9872 MINUS_EXPR, atype);
9873 else
9874 gcc_unreachable ();
9875 minus_var0 = 0;
9876 }
9877
9878 return
9879 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9880 code, atype));
9881 }
9882 }
9883
9884 return NULL_TREE;
9885
9886 case POINTER_DIFF_EXPR:
9887 case MINUS_EXPR:
9888 /* Fold &a[i] - &a[j] to i-j. */
9889 if (TREE_CODE (arg0) == ADDR_EXPR
9890 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9891 && TREE_CODE (arg1) == ADDR_EXPR
9892 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9893 {
9894 tree tem = fold_addr_of_array_ref_difference (loc, type,
9895 TREE_OPERAND (arg0, 0),
9896 TREE_OPERAND (arg1, 0),
9897 code
9898 == POINTER_DIFF_EXPR);
9899 if (tem)
9900 return tem;
9901 }
9902
9903 /* Further transformations are not for pointers. */
9904 if (code == POINTER_DIFF_EXPR)
9905 return NULL_TREE;
9906
9907 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9908 if (TREE_CODE (arg0) == NEGATE_EXPR
9909 && negate_expr_p (op1)
9910 /* If arg0 is e.g. unsigned int and type is int, then this could
9911 introduce UB, because if A is INT_MIN at runtime, the original
9912 expression can be well defined while the latter is not.
9913 See PR83269. */
9914 && !(ANY_INTEGRAL_TYPE_P (type)
9915 && TYPE_OVERFLOW_UNDEFINED (type)
9916 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9917 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9918 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9919 fold_convert_loc (loc, type,
9920 TREE_OPERAND (arg0, 0)));
9921
9922 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9923 __complex__ ( x, -y ). This is not the same for SNaNs or if
9924 signed zeros are involved. */
9925 if (!HONOR_SNANS (element_mode (arg0))
9926 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9927 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9928 {
9929 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9930 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9931 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9932 bool arg0rz = false, arg0iz = false;
9933 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9934 || (arg0i && (arg0iz = real_zerop (arg0i))))
9935 {
9936 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9937 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9938 if (arg0rz && arg1i && real_zerop (arg1i))
9939 {
9940 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9941 arg1r ? arg1r
9942 : build1 (REALPART_EXPR, rtype, arg1));
9943 tree ip = arg0i ? arg0i
9944 : build1 (IMAGPART_EXPR, rtype, arg0);
9945 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9946 }
9947 else if (arg0iz && arg1r && real_zerop (arg1r))
9948 {
9949 tree rp = arg0r ? arg0r
9950 : build1 (REALPART_EXPR, rtype, arg0);
9951 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9952 arg1i ? arg1i
9953 : build1 (IMAGPART_EXPR, rtype, arg1));
9954 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9955 }
9956 }
9957 }
9958
9959 /* A - B -> A + (-B) if B is easily negatable. */
9960 if (negate_expr_p (op1)
9961 && ! TYPE_OVERFLOW_SANITIZED (type)
9962 && ((FLOAT_TYPE_P (type)
9963 /* Avoid this transformation if B is a positive REAL_CST. */
9964 && (TREE_CODE (op1) != REAL_CST
9965 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9966 || INTEGRAL_TYPE_P (type)))
9967 return fold_build2_loc (loc, PLUS_EXPR, type,
9968 fold_convert_loc (loc, type, arg0),
9969 negate_expr (op1));
9970
9971 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9972 one. Make sure the type is not saturating and has the signedness of
9973 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9974 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9975 if ((TREE_CODE (arg0) == MULT_EXPR
9976 || TREE_CODE (arg1) == MULT_EXPR)
9977 && !TYPE_SATURATING (type)
9978 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9979 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9980 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9981 {
9982 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9983 if (tem)
9984 return tem;
9985 }
9986
9987 goto associate;
9988
9989 case MULT_EXPR:
9990 if (! FLOAT_TYPE_P (type))
9991 {
9992 /* Transform x * -C into -x * C if x is easily negatable. */
9993 if (TREE_CODE (op1) == INTEGER_CST
9994 && tree_int_cst_sgn (op1) == -1
9995 && negate_expr_p (op0)
9996 && negate_expr_p (op1)
9997 && (tem = negate_expr (op1)) != op1
9998 && ! TREE_OVERFLOW (tem))
9999 return fold_build2_loc (loc, MULT_EXPR, type,
10000 fold_convert_loc (loc, type,
10001 negate_expr (op0)), tem);
10002
10003 strict_overflow_p = false;
10004 if (TREE_CODE (arg1) == INTEGER_CST
10005 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10006 &strict_overflow_p)) != 0)
10007 {
10008 if (strict_overflow_p)
10009 fold_overflow_warning (("assuming signed overflow does not "
10010 "occur when simplifying "
10011 "multiplication"),
10012 WARN_STRICT_OVERFLOW_MISC);
10013 return fold_convert_loc (loc, type, tem);
10014 }
10015
10016 /* Optimize z * conj(z) for integer complex numbers. */
10017 if (TREE_CODE (arg0) == CONJ_EXPR
10018 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10019 return fold_mult_zconjz (loc, type, arg1);
10020 if (TREE_CODE (arg1) == CONJ_EXPR
10021 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10022 return fold_mult_zconjz (loc, type, arg0);
10023 }
10024 else
10025 {
10026 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10027 This is not the same for NaNs or if signed zeros are
10028 involved. */
10029 if (!HONOR_NANS (arg0)
10030 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10031 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10032 && TREE_CODE (arg1) == COMPLEX_CST
10033 && real_zerop (TREE_REALPART (arg1)))
10034 {
10035 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10036 if (real_onep (TREE_IMAGPART (arg1)))
10037 return
10038 fold_build2_loc (loc, COMPLEX_EXPR, type,
10039 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10040 rtype, arg0)),
10041 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10042 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10043 return
10044 fold_build2_loc (loc, COMPLEX_EXPR, type,
10045 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10046 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10047 rtype, arg0)));
10048 }
10049
10050 /* Optimize z * conj(z) for floating point complex numbers.
10051 Guarded by flag_unsafe_math_optimizations as non-finite
10052 imaginary components don't produce scalar results. */
10053 if (flag_unsafe_math_optimizations
10054 && TREE_CODE (arg0) == CONJ_EXPR
10055 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10056 return fold_mult_zconjz (loc, type, arg1);
10057 if (flag_unsafe_math_optimizations
10058 && TREE_CODE (arg1) == CONJ_EXPR
10059 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10060 return fold_mult_zconjz (loc, type, arg0);
10061 }
10062 goto associate;
10063
10064 case BIT_IOR_EXPR:
10065 /* Canonicalize (X & C1) | C2. */
10066 if (TREE_CODE (arg0) == BIT_AND_EXPR
10067 && TREE_CODE (arg1) == INTEGER_CST
10068 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10069 {
10070 int width = TYPE_PRECISION (type), w;
10071 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10072 wide_int c2 = wi::to_wide (arg1);
10073
10074 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10075 if ((c1 & c2) == c1)
10076 return omit_one_operand_loc (loc, type, arg1,
10077 TREE_OPERAND (arg0, 0));
10078
10079 wide_int msk = wi::mask (width, false,
10080 TYPE_PRECISION (TREE_TYPE (arg1)));
10081
10082 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10083 if (wi::bit_and_not (msk, c1 | c2) == 0)
10084 {
10085 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10086 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10087 }
10088
10089 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10090 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10091 mode which allows further optimizations. */
10092 c1 &= msk;
10093 c2 &= msk;
10094 wide_int c3 = wi::bit_and_not (c1, c2);
10095 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10096 {
10097 wide_int mask = wi::mask (w, false,
10098 TYPE_PRECISION (type));
10099 if (((c1 | c2) & mask) == mask
10100 && wi::bit_and_not (c1, mask) == 0)
10101 {
10102 c3 = mask;
10103 break;
10104 }
10105 }
10106
10107 if (c3 != c1)
10108 {
10109 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10110 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10111 wide_int_to_tree (type, c3));
10112 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10113 }
10114 }
10115
10116 /* See if this can be simplified into a rotate first. If that
10117 is unsuccessful continue in the association code. */
10118 goto bit_rotate;
10119
10120 case BIT_XOR_EXPR:
10121 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10122 if (TREE_CODE (arg0) == BIT_AND_EXPR
10123 && INTEGRAL_TYPE_P (type)
10124 && integer_onep (TREE_OPERAND (arg0, 1))
10125 && integer_onep (arg1))
10126 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10127 build_zero_cst (TREE_TYPE (arg0)));
10128
10129 /* See if this can be simplified into a rotate first. If that
10130 is unsuccessful continue in the association code. */
10131 goto bit_rotate;
10132
10133 case BIT_AND_EXPR:
10134 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10135 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10136 && INTEGRAL_TYPE_P (type)
10137 && integer_onep (TREE_OPERAND (arg0, 1))
10138 && integer_onep (arg1))
10139 {
10140 tree tem2;
10141 tem = TREE_OPERAND (arg0, 0);
10142 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10143 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10144 tem, tem2);
10145 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10146 build_zero_cst (TREE_TYPE (tem)));
10147 }
10148 /* Fold ~X & 1 as (X & 1) == 0. */
10149 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10150 && INTEGRAL_TYPE_P (type)
10151 && integer_onep (arg1))
10152 {
10153 tree tem2;
10154 tem = TREE_OPERAND (arg0, 0);
10155 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10156 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10157 tem, tem2);
10158 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10159 build_zero_cst (TREE_TYPE (tem)));
10160 }
10161 /* Fold !X & 1 as X == 0. */
10162 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10163 && integer_onep (arg1))
10164 {
10165 tem = TREE_OPERAND (arg0, 0);
10166 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10167 build_zero_cst (TREE_TYPE (tem)));
10168 }
10169
10170 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10171 multiple of 1 << CST. */
10172 if (TREE_CODE (arg1) == INTEGER_CST)
10173 {
10174 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10175 wide_int ncst1 = -cst1;
10176 if ((cst1 & ncst1) == ncst1
10177 && multiple_of_p (type, arg0,
10178 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10179 return fold_convert_loc (loc, type, arg0);
10180 }
10181
10182 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10183 bits from CST2. */
10184 if (TREE_CODE (arg1) == INTEGER_CST
10185 && TREE_CODE (arg0) == MULT_EXPR
10186 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10187 {
10188 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10189 wide_int masked
10190 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10191
10192 if (masked == 0)
10193 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10194 arg0, arg1);
10195 else if (masked != warg1)
10196 {
10197 /* Avoid the transform if arg1 is a mask of some
10198 mode which allows further optimizations. */
10199 int pop = wi::popcount (warg1);
10200 if (!(pop >= BITS_PER_UNIT
10201 && pow2p_hwi (pop)
10202 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10203 return fold_build2_loc (loc, code, type, op0,
10204 wide_int_to_tree (type, masked));
10205 }
10206 }
10207
10208 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10209 ((A & N) + B) & M -> (A + B) & M
10210 Similarly if (N & M) == 0,
10211 ((A | N) + B) & M -> (A + B) & M
10212 and for - instead of + (or unary - instead of +)
10213 and/or ^ instead of |.
10214 If B is constant and (B & M) == 0, fold into A & M. */
10215 if (TREE_CODE (arg1) == INTEGER_CST)
10216 {
10217 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10218 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10219 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10220 && (TREE_CODE (arg0) == PLUS_EXPR
10221 || TREE_CODE (arg0) == MINUS_EXPR
10222 || TREE_CODE (arg0) == NEGATE_EXPR)
10223 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10224 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10225 {
10226 tree pmop[2];
10227 int which = 0;
10228 wide_int cst0;
10229
10230 /* Now we know that arg0 is (C + D) or (C - D) or
10231 -C and arg1 (M) is == (1LL << cst) - 1.
10232 Store C into PMOP[0] and D into PMOP[1]. */
10233 pmop[0] = TREE_OPERAND (arg0, 0);
10234 pmop[1] = NULL;
10235 if (TREE_CODE (arg0) != NEGATE_EXPR)
10236 {
10237 pmop[1] = TREE_OPERAND (arg0, 1);
10238 which = 1;
10239 }
10240
10241 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10242 which = -1;
10243
10244 for (; which >= 0; which--)
10245 switch (TREE_CODE (pmop[which]))
10246 {
10247 case BIT_AND_EXPR:
10248 case BIT_IOR_EXPR:
10249 case BIT_XOR_EXPR:
10250 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10251 != INTEGER_CST)
10252 break;
10253 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10254 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10255 {
10256 if (cst0 != cst1)
10257 break;
10258 }
10259 else if (cst0 != 0)
10260 break;
10261 /* If C or D is of the form (A & N) where
10262 (N & M) == M, or of the form (A | N) or
10263 (A ^ N) where (N & M) == 0, replace it with A. */
10264 pmop[which] = TREE_OPERAND (pmop[which], 0);
10265 break;
10266 case INTEGER_CST:
10267 /* If C or D is a N where (N & M) == 0, it can be
10268 omitted (assumed 0). */
10269 if ((TREE_CODE (arg0) == PLUS_EXPR
10270 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10271 && (cst1 & wi::to_wide (pmop[which])) == 0)
10272 pmop[which] = NULL;
10273 break;
10274 default:
10275 break;
10276 }
10277
10278 /* Only build anything new if we optimized one or both arguments
10279 above. */
10280 if (pmop[0] != TREE_OPERAND (arg0, 0)
10281 || (TREE_CODE (arg0) != NEGATE_EXPR
10282 && pmop[1] != TREE_OPERAND (arg0, 1)))
10283 {
10284 tree utype = TREE_TYPE (arg0);
10285 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10286 {
10287 /* Perform the operations in a type that has defined
10288 overflow behavior. */
10289 utype = unsigned_type_for (TREE_TYPE (arg0));
10290 if (pmop[0] != NULL)
10291 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10292 if (pmop[1] != NULL)
10293 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10294 }
10295
10296 if (TREE_CODE (arg0) == NEGATE_EXPR)
10297 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10298 else if (TREE_CODE (arg0) == PLUS_EXPR)
10299 {
10300 if (pmop[0] != NULL && pmop[1] != NULL)
10301 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10302 pmop[0], pmop[1]);
10303 else if (pmop[0] != NULL)
10304 tem = pmop[0];
10305 else if (pmop[1] != NULL)
10306 tem = pmop[1];
10307 else
10308 return build_int_cst (type, 0);
10309 }
10310 else if (pmop[0] == NULL)
10311 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10312 else
10313 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10314 pmop[0], pmop[1]);
10315 /* TEM is now the new binary +, - or unary - replacement. */
10316 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10317 fold_convert_loc (loc, utype, arg1));
10318 return fold_convert_loc (loc, type, tem);
10319 }
10320 }
10321 }
10322
10323 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10324 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10325 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10326 {
10327 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10328
10329 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10330 if (mask == -1)
10331 return
10332 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10333 }
10334
10335 goto associate;
10336
10337 case RDIV_EXPR:
10338 /* Don't touch a floating-point divide by zero unless the mode
10339 of the constant can represent infinity. */
10340 if (TREE_CODE (arg1) == REAL_CST
10341 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10342 && real_zerop (arg1))
10343 return NULL_TREE;
10344
10345 /* (-A) / (-B) -> A / B */
10346 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10347 return fold_build2_loc (loc, RDIV_EXPR, type,
10348 TREE_OPERAND (arg0, 0),
10349 negate_expr (arg1));
10350 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10351 return fold_build2_loc (loc, RDIV_EXPR, type,
10352 negate_expr (arg0),
10353 TREE_OPERAND (arg1, 0));
10354 return NULL_TREE;
10355
10356 case TRUNC_DIV_EXPR:
10357 /* Fall through */
10358
10359 case FLOOR_DIV_EXPR:
10360 /* Simplify A / (B << N) where A and B are positive and B is
10361 a power of 2, to A >> (N + log2(B)). */
10362 strict_overflow_p = false;
10363 if (TREE_CODE (arg1) == LSHIFT_EXPR
10364 && (TYPE_UNSIGNED (type)
10365 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10366 {
10367 tree sval = TREE_OPERAND (arg1, 0);
10368 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10369 {
10370 tree sh_cnt = TREE_OPERAND (arg1, 1);
10371 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10372 wi::exact_log2 (wi::to_wide (sval)));
10373
10374 if (strict_overflow_p)
10375 fold_overflow_warning (("assuming signed overflow does not "
10376 "occur when simplifying A / (B << N)"),
10377 WARN_STRICT_OVERFLOW_MISC);
10378
10379 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10380 sh_cnt, pow2);
10381 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10382 fold_convert_loc (loc, type, arg0), sh_cnt);
10383 }
10384 }
10385
10386 /* Fall through */
10387
10388 case ROUND_DIV_EXPR:
10389 case CEIL_DIV_EXPR:
10390 case EXACT_DIV_EXPR:
10391 if (integer_zerop (arg1))
10392 return NULL_TREE;
10393
10394 /* Convert -A / -B to A / B when the type is signed and overflow is
10395 undefined. */
10396 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10397 && TREE_CODE (op0) == NEGATE_EXPR
10398 && negate_expr_p (op1))
10399 {
10400 if (INTEGRAL_TYPE_P (type))
10401 fold_overflow_warning (("assuming signed overflow does not occur "
10402 "when distributing negation across "
10403 "division"),
10404 WARN_STRICT_OVERFLOW_MISC);
10405 return fold_build2_loc (loc, code, type,
10406 fold_convert_loc (loc, type,
10407 TREE_OPERAND (arg0, 0)),
10408 negate_expr (op1));
10409 }
10410 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10411 && TREE_CODE (arg1) == NEGATE_EXPR
10412 && negate_expr_p (op0))
10413 {
10414 if (INTEGRAL_TYPE_P (type))
10415 fold_overflow_warning (("assuming signed overflow does not occur "
10416 "when distributing negation across "
10417 "division"),
10418 WARN_STRICT_OVERFLOW_MISC);
10419 return fold_build2_loc (loc, code, type,
10420 negate_expr (op0),
10421 fold_convert_loc (loc, type,
10422 TREE_OPERAND (arg1, 0)));
10423 }
10424
10425 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10426 operation, EXACT_DIV_EXPR.
10427
10428 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10429 At one time others generated faster code, it's not clear if they do
10430 after the last round to changes to the DIV code in expmed.c. */
10431 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10432 && multiple_of_p (type, arg0, arg1))
10433 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10434 fold_convert (type, arg0),
10435 fold_convert (type, arg1));
10436
10437 strict_overflow_p = false;
10438 if (TREE_CODE (arg1) == INTEGER_CST
10439 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10440 &strict_overflow_p)) != 0)
10441 {
10442 if (strict_overflow_p)
10443 fold_overflow_warning (("assuming signed overflow does not occur "
10444 "when simplifying division"),
10445 WARN_STRICT_OVERFLOW_MISC);
10446 return fold_convert_loc (loc, type, tem);
10447 }
10448
10449 return NULL_TREE;
10450
10451 case CEIL_MOD_EXPR:
10452 case FLOOR_MOD_EXPR:
10453 case ROUND_MOD_EXPR:
10454 case TRUNC_MOD_EXPR:
10455 strict_overflow_p = false;
10456 if (TREE_CODE (arg1) == INTEGER_CST
10457 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10458 &strict_overflow_p)) != 0)
10459 {
10460 if (strict_overflow_p)
10461 fold_overflow_warning (("assuming signed overflow does not occur "
10462 "when simplifying modulus"),
10463 WARN_STRICT_OVERFLOW_MISC);
10464 return fold_convert_loc (loc, type, tem);
10465 }
10466
10467 return NULL_TREE;
10468
10469 case LROTATE_EXPR:
10470 case RROTATE_EXPR:
10471 case RSHIFT_EXPR:
10472 case LSHIFT_EXPR:
10473 /* Since negative shift count is not well-defined,
10474 don't try to compute it in the compiler. */
10475 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10476 return NULL_TREE;
10477
10478 prec = element_precision (type);
10479
10480 /* If we have a rotate of a bit operation with the rotate count and
10481 the second operand of the bit operation both constant,
10482 permute the two operations. */
10483 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10484 && (TREE_CODE (arg0) == BIT_AND_EXPR
10485 || TREE_CODE (arg0) == BIT_IOR_EXPR
10486 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10487 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10488 {
10489 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10490 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10491 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10492 fold_build2_loc (loc, code, type,
10493 arg00, arg1),
10494 fold_build2_loc (loc, code, type,
10495 arg01, arg1));
10496 }
10497
10498 /* Two consecutive rotates adding up to the some integer
10499 multiple of the precision of the type can be ignored. */
10500 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10501 && TREE_CODE (arg0) == RROTATE_EXPR
10502 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10503 && wi::umod_trunc (wi::to_wide (arg1)
10504 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10505 prec) == 0)
10506 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10507
10508 return NULL_TREE;
10509
10510 case MIN_EXPR:
10511 case MAX_EXPR:
10512 goto associate;
10513
10514 case TRUTH_ANDIF_EXPR:
10515 /* Note that the operands of this must be ints
10516 and their values must be 0 or 1.
10517 ("true" is a fixed value perhaps depending on the language.) */
10518 /* If first arg is constant zero, return it. */
10519 if (integer_zerop (arg0))
10520 return fold_convert_loc (loc, type, arg0);
10521 /* FALLTHRU */
10522 case TRUTH_AND_EXPR:
10523 /* If either arg is constant true, drop it. */
10524 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10525 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10526 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10527 /* Preserve sequence points. */
10528 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10529 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10530 /* If second arg is constant zero, result is zero, but first arg
10531 must be evaluated. */
10532 if (integer_zerop (arg1))
10533 return omit_one_operand_loc (loc, type, arg1, arg0);
10534 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10535 case will be handled here. */
10536 if (integer_zerop (arg0))
10537 return omit_one_operand_loc (loc, type, arg0, arg1);
10538
10539 /* !X && X is always false. */
10540 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10541 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10542 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10543 /* X && !X is always false. */
10544 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10545 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10546 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10547
10548 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10549 means A >= Y && A != MAX, but in this case we know that
10550 A < X <= MAX. */
10551
10552 if (!TREE_SIDE_EFFECTS (arg0)
10553 && !TREE_SIDE_EFFECTS (arg1))
10554 {
10555 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10556 if (tem && !operand_equal_p (tem, arg0, 0))
10557 return fold_build2_loc (loc, code, type, tem, arg1);
10558
10559 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10560 if (tem && !operand_equal_p (tem, arg1, 0))
10561 return fold_build2_loc (loc, code, type, arg0, tem);
10562 }
10563
10564 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10565 != NULL_TREE)
10566 return tem;
10567
10568 return NULL_TREE;
10569
10570 case TRUTH_ORIF_EXPR:
10571 /* Note that the operands of this must be ints
10572 and their values must be 0 or true.
10573 ("true" is a fixed value perhaps depending on the language.) */
10574 /* If first arg is constant true, return it. */
10575 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10576 return fold_convert_loc (loc, type, arg0);
10577 /* FALLTHRU */
10578 case TRUTH_OR_EXPR:
10579 /* If either arg is constant zero, drop it. */
10580 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10581 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10582 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10583 /* Preserve sequence points. */
10584 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10585 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10586 /* If second arg is constant true, result is true, but we must
10587 evaluate first arg. */
10588 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10589 return omit_one_operand_loc (loc, type, arg1, arg0);
10590 /* Likewise for first arg, but note this only occurs here for
10591 TRUTH_OR_EXPR. */
10592 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10593 return omit_one_operand_loc (loc, type, arg0, arg1);
10594
10595 /* !X || X is always true. */
10596 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10597 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10598 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10599 /* X || !X is always true. */
10600 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10601 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10602 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10603
10604 /* (X && !Y) || (!X && Y) is X ^ Y */
10605 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10606 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10607 {
10608 tree a0, a1, l0, l1, n0, n1;
10609
10610 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10611 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10612
10613 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10614 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10615
10616 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10617 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10618
10619 if ((operand_equal_p (n0, a0, 0)
10620 && operand_equal_p (n1, a1, 0))
10621 || (operand_equal_p (n0, a1, 0)
10622 && operand_equal_p (n1, a0, 0)))
10623 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10624 }
10625
10626 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10627 != NULL_TREE)
10628 return tem;
10629
10630 return NULL_TREE;
10631
10632 case TRUTH_XOR_EXPR:
10633 /* If the second arg is constant zero, drop it. */
10634 if (integer_zerop (arg1))
10635 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10636 /* If the second arg is constant true, this is a logical inversion. */
10637 if (integer_onep (arg1))
10638 {
10639 tem = invert_truthvalue_loc (loc, arg0);
10640 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10641 }
10642 /* Identical arguments cancel to zero. */
10643 if (operand_equal_p (arg0, arg1, 0))
10644 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10645
10646 /* !X ^ X is always true. */
10647 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10648 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10649 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10650
10651 /* X ^ !X is always true. */
10652 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10653 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10654 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10655
10656 return NULL_TREE;
10657
10658 case EQ_EXPR:
10659 case NE_EXPR:
10660 STRIP_NOPS (arg0);
10661 STRIP_NOPS (arg1);
10662
10663 tem = fold_comparison (loc, code, type, op0, op1);
10664 if (tem != NULL_TREE)
10665 return tem;
10666
10667 /* bool_var != 1 becomes !bool_var. */
10668 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10669 && code == NE_EXPR)
10670 return fold_convert_loc (loc, type,
10671 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10672 TREE_TYPE (arg0), arg0));
10673
10674 /* bool_var == 0 becomes !bool_var. */
10675 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10676 && code == EQ_EXPR)
10677 return fold_convert_loc (loc, type,
10678 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10679 TREE_TYPE (arg0), arg0));
10680
10681 /* !exp != 0 becomes !exp */
10682 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10683 && code == NE_EXPR)
10684 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10685
10686 /* If this is an EQ or NE comparison with zero and ARG0 is
10687 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10688 two operations, but the latter can be done in one less insn
10689 on machines that have only two-operand insns or on which a
10690 constant cannot be the first operand. */
10691 if (TREE_CODE (arg0) == BIT_AND_EXPR
10692 && integer_zerop (arg1))
10693 {
10694 tree arg00 = TREE_OPERAND (arg0, 0);
10695 tree arg01 = TREE_OPERAND (arg0, 1);
10696 if (TREE_CODE (arg00) == LSHIFT_EXPR
10697 && integer_onep (TREE_OPERAND (arg00, 0)))
10698 {
10699 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10700 arg01, TREE_OPERAND (arg00, 1));
10701 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10702 build_int_cst (TREE_TYPE (arg0), 1));
10703 return fold_build2_loc (loc, code, type,
10704 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10705 arg1);
10706 }
10707 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10708 && integer_onep (TREE_OPERAND (arg01, 0)))
10709 {
10710 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10711 arg00, TREE_OPERAND (arg01, 1));
10712 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10713 build_int_cst (TREE_TYPE (arg0), 1));
10714 return fold_build2_loc (loc, code, type,
10715 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10716 arg1);
10717 }
10718 }
10719
10720 /* If this is an NE or EQ comparison of zero against the result of a
10721 signed MOD operation whose second operand is a power of 2, make
10722 the MOD operation unsigned since it is simpler and equivalent. */
10723 if (integer_zerop (arg1)
10724 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10725 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10726 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10727 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10728 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10729 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10730 {
10731 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10732 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10733 fold_convert_loc (loc, newtype,
10734 TREE_OPERAND (arg0, 0)),
10735 fold_convert_loc (loc, newtype,
10736 TREE_OPERAND (arg0, 1)));
10737
10738 return fold_build2_loc (loc, code, type, newmod,
10739 fold_convert_loc (loc, newtype, arg1));
10740 }
10741
10742 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10743 C1 is a valid shift constant, and C2 is a power of two, i.e.
10744 a single bit. */
10745 if (TREE_CODE (arg0) == BIT_AND_EXPR
10746 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10747 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10748 == INTEGER_CST
10749 && integer_pow2p (TREE_OPERAND (arg0, 1))
10750 && integer_zerop (arg1))
10751 {
10752 tree itype = TREE_TYPE (arg0);
10753 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10754 prec = TYPE_PRECISION (itype);
10755
10756 /* Check for a valid shift count. */
10757 if (wi::ltu_p (wi::to_wide (arg001), prec))
10758 {
10759 tree arg01 = TREE_OPERAND (arg0, 1);
10760 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10761 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10762 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10763 can be rewritten as (X & (C2 << C1)) != 0. */
10764 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10765 {
10766 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10767 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10768 return fold_build2_loc (loc, code, type, tem,
10769 fold_convert_loc (loc, itype, arg1));
10770 }
10771 /* Otherwise, for signed (arithmetic) shifts,
10772 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10773 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10774 else if (!TYPE_UNSIGNED (itype))
10775 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10776 arg000, build_int_cst (itype, 0));
10777 /* Otherwise, of unsigned (logical) shifts,
10778 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10779 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10780 else
10781 return omit_one_operand_loc (loc, type,
10782 code == EQ_EXPR ? integer_one_node
10783 : integer_zero_node,
10784 arg000);
10785 }
10786 }
10787
10788 /* If this is a comparison of a field, we may be able to simplify it. */
10789 if ((TREE_CODE (arg0) == COMPONENT_REF
10790 || TREE_CODE (arg0) == BIT_FIELD_REF)
10791 /* Handle the constant case even without -O
10792 to make sure the warnings are given. */
10793 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10794 {
10795 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10796 if (t1)
10797 return t1;
10798 }
10799
10800 /* Optimize comparisons of strlen vs zero to a compare of the
10801 first character of the string vs zero. To wit,
10802 strlen(ptr) == 0 => *ptr == 0
10803 strlen(ptr) != 0 => *ptr != 0
10804 Other cases should reduce to one of these two (or a constant)
10805 due to the return value of strlen being unsigned. */
10806 if (TREE_CODE (arg0) == CALL_EXPR
10807 && integer_zerop (arg1))
10808 {
10809 tree fndecl = get_callee_fndecl (arg0);
10810
10811 if (fndecl
10812 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10813 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10814 && call_expr_nargs (arg0) == 1
10815 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10816 {
10817 tree iref = build_fold_indirect_ref_loc (loc,
10818 CALL_EXPR_ARG (arg0, 0));
10819 return fold_build2_loc (loc, code, type, iref,
10820 build_int_cst (TREE_TYPE (iref), 0));
10821 }
10822 }
10823
10824 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10825 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10826 if (TREE_CODE (arg0) == RSHIFT_EXPR
10827 && integer_zerop (arg1)
10828 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10829 {
10830 tree arg00 = TREE_OPERAND (arg0, 0);
10831 tree arg01 = TREE_OPERAND (arg0, 1);
10832 tree itype = TREE_TYPE (arg00);
10833 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10834 {
10835 if (TYPE_UNSIGNED (itype))
10836 {
10837 itype = signed_type_for (itype);
10838 arg00 = fold_convert_loc (loc, itype, arg00);
10839 }
10840 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10841 type, arg00, build_zero_cst (itype));
10842 }
10843 }
10844
10845 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10846 (X & C) == 0 when C is a single bit. */
10847 if (TREE_CODE (arg0) == BIT_AND_EXPR
10848 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10849 && integer_zerop (arg1)
10850 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10851 {
10852 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10853 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10854 TREE_OPERAND (arg0, 1));
10855 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10856 type, tem,
10857 fold_convert_loc (loc, TREE_TYPE (arg0),
10858 arg1));
10859 }
10860
10861 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10862 constant C is a power of two, i.e. a single bit. */
10863 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10864 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10865 && integer_zerop (arg1)
10866 && integer_pow2p (TREE_OPERAND (arg0, 1))
10867 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10868 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10869 {
10870 tree arg00 = TREE_OPERAND (arg0, 0);
10871 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10872 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10873 }
10874
10875 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10876 when is C is a power of two, i.e. a single bit. */
10877 if (TREE_CODE (arg0) == BIT_AND_EXPR
10878 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10879 && integer_zerop (arg1)
10880 && integer_pow2p (TREE_OPERAND (arg0, 1))
10881 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10882 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10883 {
10884 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10885 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10886 arg000, TREE_OPERAND (arg0, 1));
10887 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10888 tem, build_int_cst (TREE_TYPE (tem), 0));
10889 }
10890
10891 if (integer_zerop (arg1)
10892 && tree_expr_nonzero_p (arg0))
10893 {
10894 tree res = constant_boolean_node (code==NE_EXPR, type);
10895 return omit_one_operand_loc (loc, type, res, arg0);
10896 }
10897
10898 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10899 if (TREE_CODE (arg0) == BIT_AND_EXPR
10900 && TREE_CODE (arg1) == BIT_AND_EXPR)
10901 {
10902 tree arg00 = TREE_OPERAND (arg0, 0);
10903 tree arg01 = TREE_OPERAND (arg0, 1);
10904 tree arg10 = TREE_OPERAND (arg1, 0);
10905 tree arg11 = TREE_OPERAND (arg1, 1);
10906 tree itype = TREE_TYPE (arg0);
10907
10908 if (operand_equal_p (arg01, arg11, 0))
10909 {
10910 tem = fold_convert_loc (loc, itype, arg10);
10911 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10912 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10913 return fold_build2_loc (loc, code, type, tem,
10914 build_zero_cst (itype));
10915 }
10916 if (operand_equal_p (arg01, arg10, 0))
10917 {
10918 tem = fold_convert_loc (loc, itype, arg11);
10919 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10920 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10921 return fold_build2_loc (loc, code, type, tem,
10922 build_zero_cst (itype));
10923 }
10924 if (operand_equal_p (arg00, arg11, 0))
10925 {
10926 tem = fold_convert_loc (loc, itype, arg10);
10927 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10928 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10929 return fold_build2_loc (loc, code, type, tem,
10930 build_zero_cst (itype));
10931 }
10932 if (operand_equal_p (arg00, arg10, 0))
10933 {
10934 tem = fold_convert_loc (loc, itype, arg11);
10935 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10936 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10937 return fold_build2_loc (loc, code, type, tem,
10938 build_zero_cst (itype));
10939 }
10940 }
10941
10942 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10943 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10944 {
10945 tree arg00 = TREE_OPERAND (arg0, 0);
10946 tree arg01 = TREE_OPERAND (arg0, 1);
10947 tree arg10 = TREE_OPERAND (arg1, 0);
10948 tree arg11 = TREE_OPERAND (arg1, 1);
10949 tree itype = TREE_TYPE (arg0);
10950
10951 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10952 operand_equal_p guarantees no side-effects so we don't need
10953 to use omit_one_operand on Z. */
10954 if (operand_equal_p (arg01, arg11, 0))
10955 return fold_build2_loc (loc, code, type, arg00,
10956 fold_convert_loc (loc, TREE_TYPE (arg00),
10957 arg10));
10958 if (operand_equal_p (arg01, arg10, 0))
10959 return fold_build2_loc (loc, code, type, arg00,
10960 fold_convert_loc (loc, TREE_TYPE (arg00),
10961 arg11));
10962 if (operand_equal_p (arg00, arg11, 0))
10963 return fold_build2_loc (loc, code, type, arg01,
10964 fold_convert_loc (loc, TREE_TYPE (arg01),
10965 arg10));
10966 if (operand_equal_p (arg00, arg10, 0))
10967 return fold_build2_loc (loc, code, type, arg01,
10968 fold_convert_loc (loc, TREE_TYPE (arg01),
10969 arg11));
10970
10971 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10972 if (TREE_CODE (arg01) == INTEGER_CST
10973 && TREE_CODE (arg11) == INTEGER_CST)
10974 {
10975 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10976 fold_convert_loc (loc, itype, arg11));
10977 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10978 return fold_build2_loc (loc, code, type, tem,
10979 fold_convert_loc (loc, itype, arg10));
10980 }
10981 }
10982
10983 /* Attempt to simplify equality/inequality comparisons of complex
10984 values. Only lower the comparison if the result is known or
10985 can be simplified to a single scalar comparison. */
10986 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10987 || TREE_CODE (arg0) == COMPLEX_CST)
10988 && (TREE_CODE (arg1) == COMPLEX_EXPR
10989 || TREE_CODE (arg1) == COMPLEX_CST))
10990 {
10991 tree real0, imag0, real1, imag1;
10992 tree rcond, icond;
10993
10994 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10995 {
10996 real0 = TREE_OPERAND (arg0, 0);
10997 imag0 = TREE_OPERAND (arg0, 1);
10998 }
10999 else
11000 {
11001 real0 = TREE_REALPART (arg0);
11002 imag0 = TREE_IMAGPART (arg0);
11003 }
11004
11005 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11006 {
11007 real1 = TREE_OPERAND (arg1, 0);
11008 imag1 = TREE_OPERAND (arg1, 1);
11009 }
11010 else
11011 {
11012 real1 = TREE_REALPART (arg1);
11013 imag1 = TREE_IMAGPART (arg1);
11014 }
11015
11016 rcond = fold_binary_loc (loc, code, type, real0, real1);
11017 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11018 {
11019 if (integer_zerop (rcond))
11020 {
11021 if (code == EQ_EXPR)
11022 return omit_two_operands_loc (loc, type, boolean_false_node,
11023 imag0, imag1);
11024 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11025 }
11026 else
11027 {
11028 if (code == NE_EXPR)
11029 return omit_two_operands_loc (loc, type, boolean_true_node,
11030 imag0, imag1);
11031 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11032 }
11033 }
11034
11035 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11036 if (icond && TREE_CODE (icond) == INTEGER_CST)
11037 {
11038 if (integer_zerop (icond))
11039 {
11040 if (code == EQ_EXPR)
11041 return omit_two_operands_loc (loc, type, boolean_false_node,
11042 real0, real1);
11043 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11044 }
11045 else
11046 {
11047 if (code == NE_EXPR)
11048 return omit_two_operands_loc (loc, type, boolean_true_node,
11049 real0, real1);
11050 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11051 }
11052 }
11053 }
11054
11055 return NULL_TREE;
11056
11057 case LT_EXPR:
11058 case GT_EXPR:
11059 case LE_EXPR:
11060 case GE_EXPR:
11061 tem = fold_comparison (loc, code, type, op0, op1);
11062 if (tem != NULL_TREE)
11063 return tem;
11064
11065 /* Transform comparisons of the form X +- C CMP X. */
11066 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11067 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11068 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11069 && !HONOR_SNANS (arg0))
11070 {
11071 tree arg01 = TREE_OPERAND (arg0, 1);
11072 enum tree_code code0 = TREE_CODE (arg0);
11073 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11074
11075 /* (X - c) > X becomes false. */
11076 if (code == GT_EXPR
11077 && ((code0 == MINUS_EXPR && is_positive >= 0)
11078 || (code0 == PLUS_EXPR && is_positive <= 0)))
11079 return constant_boolean_node (0, type);
11080
11081 /* Likewise (X + c) < X becomes false. */
11082 if (code == LT_EXPR
11083 && ((code0 == PLUS_EXPR && is_positive >= 0)
11084 || (code0 == MINUS_EXPR && is_positive <= 0)))
11085 return constant_boolean_node (0, type);
11086
11087 /* Convert (X - c) <= X to true. */
11088 if (!HONOR_NANS (arg1)
11089 && code == LE_EXPR
11090 && ((code0 == MINUS_EXPR && is_positive >= 0)
11091 || (code0 == PLUS_EXPR && is_positive <= 0)))
11092 return constant_boolean_node (1, type);
11093
11094 /* Convert (X + c) >= X to true. */
11095 if (!HONOR_NANS (arg1)
11096 && code == GE_EXPR
11097 && ((code0 == PLUS_EXPR && is_positive >= 0)
11098 || (code0 == MINUS_EXPR && is_positive <= 0)))
11099 return constant_boolean_node (1, type);
11100 }
11101
11102 /* If we are comparing an ABS_EXPR with a constant, we can
11103 convert all the cases into explicit comparisons, but they may
11104 well not be faster than doing the ABS and one comparison.
11105 But ABS (X) <= C is a range comparison, which becomes a subtraction
11106 and a comparison, and is probably faster. */
11107 if (code == LE_EXPR
11108 && TREE_CODE (arg1) == INTEGER_CST
11109 && TREE_CODE (arg0) == ABS_EXPR
11110 && ! TREE_SIDE_EFFECTS (arg0)
11111 && (tem = negate_expr (arg1)) != 0
11112 && TREE_CODE (tem) == INTEGER_CST
11113 && !TREE_OVERFLOW (tem))
11114 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11115 build2 (GE_EXPR, type,
11116 TREE_OPERAND (arg0, 0), tem),
11117 build2 (LE_EXPR, type,
11118 TREE_OPERAND (arg0, 0), arg1));
11119
11120 /* Convert ABS_EXPR<x> >= 0 to true. */
11121 strict_overflow_p = false;
11122 if (code == GE_EXPR
11123 && (integer_zerop (arg1)
11124 || (! HONOR_NANS (arg0)
11125 && real_zerop (arg1)))
11126 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11127 {
11128 if (strict_overflow_p)
11129 fold_overflow_warning (("assuming signed overflow does not occur "
11130 "when simplifying comparison of "
11131 "absolute value and zero"),
11132 WARN_STRICT_OVERFLOW_CONDITIONAL);
11133 return omit_one_operand_loc (loc, type,
11134 constant_boolean_node (true, type),
11135 arg0);
11136 }
11137
11138 /* Convert ABS_EXPR<x> < 0 to false. */
11139 strict_overflow_p = false;
11140 if (code == LT_EXPR
11141 && (integer_zerop (arg1) || real_zerop (arg1))
11142 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11143 {
11144 if (strict_overflow_p)
11145 fold_overflow_warning (("assuming signed overflow does not occur "
11146 "when simplifying comparison of "
11147 "absolute value and zero"),
11148 WARN_STRICT_OVERFLOW_CONDITIONAL);
11149 return omit_one_operand_loc (loc, type,
11150 constant_boolean_node (false, type),
11151 arg0);
11152 }
11153
11154 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11155 and similarly for >= into !=. */
11156 if ((code == LT_EXPR || code == GE_EXPR)
11157 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11158 && TREE_CODE (arg1) == LSHIFT_EXPR
11159 && integer_onep (TREE_OPERAND (arg1, 0)))
11160 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11161 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11162 TREE_OPERAND (arg1, 1)),
11163 build_zero_cst (TREE_TYPE (arg0)));
11164
11165 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11166 otherwise Y might be >= # of bits in X's type and thus e.g.
11167 (unsigned char) (1 << Y) for Y 15 might be 0.
11168 If the cast is widening, then 1 << Y should have unsigned type,
11169 otherwise if Y is number of bits in the signed shift type minus 1,
11170 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11171 31 might be 0xffffffff80000000. */
11172 if ((code == LT_EXPR || code == GE_EXPR)
11173 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11174 && CONVERT_EXPR_P (arg1)
11175 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11176 && (element_precision (TREE_TYPE (arg1))
11177 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11178 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11179 || (element_precision (TREE_TYPE (arg1))
11180 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11181 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11182 {
11183 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11184 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11185 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11186 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11187 build_zero_cst (TREE_TYPE (arg0)));
11188 }
11189
11190 return NULL_TREE;
11191
11192 case UNORDERED_EXPR:
11193 case ORDERED_EXPR:
11194 case UNLT_EXPR:
11195 case UNLE_EXPR:
11196 case UNGT_EXPR:
11197 case UNGE_EXPR:
11198 case UNEQ_EXPR:
11199 case LTGT_EXPR:
11200 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11201 {
11202 tree targ0 = strip_float_extensions (arg0);
11203 tree targ1 = strip_float_extensions (arg1);
11204 tree newtype = TREE_TYPE (targ0);
11205
11206 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11207 newtype = TREE_TYPE (targ1);
11208
11209 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11210 return fold_build2_loc (loc, code, type,
11211 fold_convert_loc (loc, newtype, targ0),
11212 fold_convert_loc (loc, newtype, targ1));
11213 }
11214
11215 return NULL_TREE;
11216
11217 case COMPOUND_EXPR:
11218 /* When pedantic, a compound expression can be neither an lvalue
11219 nor an integer constant expression. */
11220 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11221 return NULL_TREE;
11222 /* Don't let (0, 0) be null pointer constant. */
11223 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11224 : fold_convert_loc (loc, type, arg1);
11225 return pedantic_non_lvalue_loc (loc, tem);
11226
11227 case ASSERT_EXPR:
11228 /* An ASSERT_EXPR should never be passed to fold_binary. */
11229 gcc_unreachable ();
11230
11231 default:
11232 return NULL_TREE;
11233 } /* switch (code) */
11234 }
11235
11236 /* Used by contains_label_[p1]. */
11237
11238 struct contains_label_data
11239 {
11240 hash_set<tree> *pset;
11241 bool inside_switch_p;
11242 };
11243
11244 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11245 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11246 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11247
11248 static tree
11249 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11250 {
11251 contains_label_data *d = (contains_label_data *) data;
11252 switch (TREE_CODE (*tp))
11253 {
11254 case LABEL_EXPR:
11255 return *tp;
11256
11257 case CASE_LABEL_EXPR:
11258 if (!d->inside_switch_p)
11259 return *tp;
11260 return NULL_TREE;
11261
11262 case SWITCH_EXPR:
11263 if (!d->inside_switch_p)
11264 {
11265 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11266 return *tp;
11267 d->inside_switch_p = true;
11268 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11269 return *tp;
11270 d->inside_switch_p = false;
11271 *walk_subtrees = 0;
11272 }
11273 return NULL_TREE;
11274
11275 case GOTO_EXPR:
11276 *walk_subtrees = 0;
11277 return NULL_TREE;
11278
11279 default:
11280 return NULL_TREE;
11281 }
11282 }
11283
11284 /* Return whether the sub-tree ST contains a label which is accessible from
11285 outside the sub-tree. */
11286
11287 static bool
11288 contains_label_p (tree st)
11289 {
11290 hash_set<tree> pset;
11291 contains_label_data data = { &pset, false };
11292 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11293 }
11294
11295 /* Fold a ternary expression of code CODE and type TYPE with operands
11296 OP0, OP1, and OP2. Return the folded expression if folding is
11297 successful. Otherwise, return NULL_TREE. */
11298
11299 tree
11300 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11301 tree op0, tree op1, tree op2)
11302 {
11303 tree tem;
11304 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11305 enum tree_code_class kind = TREE_CODE_CLASS (code);
11306
11307 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11308 && TREE_CODE_LENGTH (code) == 3);
11309
11310 /* If this is a commutative operation, and OP0 is a constant, move it
11311 to OP1 to reduce the number of tests below. */
11312 if (commutative_ternary_tree_code (code)
11313 && tree_swap_operands_p (op0, op1))
11314 return fold_build3_loc (loc, code, type, op1, op0, op2);
11315
11316 tem = generic_simplify (loc, code, type, op0, op1, op2);
11317 if (tem)
11318 return tem;
11319
11320 /* Strip any conversions that don't change the mode. This is safe
11321 for every expression, except for a comparison expression because
11322 its signedness is derived from its operands. So, in the latter
11323 case, only strip conversions that don't change the signedness.
11324
11325 Note that this is done as an internal manipulation within the
11326 constant folder, in order to find the simplest representation of
11327 the arguments so that their form can be studied. In any cases,
11328 the appropriate type conversions should be put back in the tree
11329 that will get out of the constant folder. */
11330 if (op0)
11331 {
11332 arg0 = op0;
11333 STRIP_NOPS (arg0);
11334 }
11335
11336 if (op1)
11337 {
11338 arg1 = op1;
11339 STRIP_NOPS (arg1);
11340 }
11341
11342 if (op2)
11343 {
11344 arg2 = op2;
11345 STRIP_NOPS (arg2);
11346 }
11347
11348 switch (code)
11349 {
11350 case COMPONENT_REF:
11351 if (TREE_CODE (arg0) == CONSTRUCTOR
11352 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11353 {
11354 unsigned HOST_WIDE_INT idx;
11355 tree field, value;
11356 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11357 if (field == arg1)
11358 return value;
11359 }
11360 return NULL_TREE;
11361
11362 case COND_EXPR:
11363 case VEC_COND_EXPR:
11364 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11365 so all simple results must be passed through pedantic_non_lvalue. */
11366 if (TREE_CODE (arg0) == INTEGER_CST)
11367 {
11368 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11369 tem = integer_zerop (arg0) ? op2 : op1;
11370 /* Only optimize constant conditions when the selected branch
11371 has the same type as the COND_EXPR. This avoids optimizing
11372 away "c ? x : throw", where the throw has a void type.
11373 Avoid throwing away that operand which contains label. */
11374 if ((!TREE_SIDE_EFFECTS (unused_op)
11375 || !contains_label_p (unused_op))
11376 && (! VOID_TYPE_P (TREE_TYPE (tem))
11377 || VOID_TYPE_P (type)))
11378 return pedantic_non_lvalue_loc (loc, tem);
11379 return NULL_TREE;
11380 }
11381 else if (TREE_CODE (arg0) == VECTOR_CST)
11382 {
11383 unsigned HOST_WIDE_INT nelts;
11384 if ((TREE_CODE (arg1) == VECTOR_CST
11385 || TREE_CODE (arg1) == CONSTRUCTOR)
11386 && (TREE_CODE (arg2) == VECTOR_CST
11387 || TREE_CODE (arg2) == CONSTRUCTOR)
11388 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11389 {
11390 vec_perm_builder sel (nelts, nelts, 1);
11391 for (unsigned int i = 0; i < nelts; i++)
11392 {
11393 tree val = VECTOR_CST_ELT (arg0, i);
11394 if (integer_all_onesp (val))
11395 sel.quick_push (i);
11396 else if (integer_zerop (val))
11397 sel.quick_push (nelts + i);
11398 else /* Currently unreachable. */
11399 return NULL_TREE;
11400 }
11401 tree t = fold_vec_perm (type, arg1, arg2,
11402 vec_perm_indices (sel, 2, nelts));
11403 if (t != NULL_TREE)
11404 return t;
11405 }
11406 }
11407
11408 /* If we have A op B ? A : C, we may be able to convert this to a
11409 simpler expression, depending on the operation and the values
11410 of B and C. Signed zeros prevent all of these transformations,
11411 for reasons given above each one.
11412
11413 Also try swapping the arguments and inverting the conditional. */
11414 if (COMPARISON_CLASS_P (arg0)
11415 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11416 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11417 {
11418 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11419 if (tem)
11420 return tem;
11421 }
11422
11423 if (COMPARISON_CLASS_P (arg0)
11424 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11425 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11426 {
11427 location_t loc0 = expr_location_or (arg0, loc);
11428 tem = fold_invert_truthvalue (loc0, arg0);
11429 if (tem && COMPARISON_CLASS_P (tem))
11430 {
11431 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11432 if (tem)
11433 return tem;
11434 }
11435 }
11436
11437 /* If the second operand is simpler than the third, swap them
11438 since that produces better jump optimization results. */
11439 if (truth_value_p (TREE_CODE (arg0))
11440 && tree_swap_operands_p (op1, op2))
11441 {
11442 location_t loc0 = expr_location_or (arg0, loc);
11443 /* See if this can be inverted. If it can't, possibly because
11444 it was a floating-point inequality comparison, don't do
11445 anything. */
11446 tem = fold_invert_truthvalue (loc0, arg0);
11447 if (tem)
11448 return fold_build3_loc (loc, code, type, tem, op2, op1);
11449 }
11450
11451 /* Convert A ? 1 : 0 to simply A. */
11452 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11453 : (integer_onep (op1)
11454 && !VECTOR_TYPE_P (type)))
11455 && integer_zerop (op2)
11456 /* If we try to convert OP0 to our type, the
11457 call to fold will try to move the conversion inside
11458 a COND, which will recurse. In that case, the COND_EXPR
11459 is probably the best choice, so leave it alone. */
11460 && type == TREE_TYPE (arg0))
11461 return pedantic_non_lvalue_loc (loc, arg0);
11462
11463 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11464 over COND_EXPR in cases such as floating point comparisons. */
11465 if (integer_zerop (op1)
11466 && code == COND_EXPR
11467 && integer_onep (op2)
11468 && !VECTOR_TYPE_P (type)
11469 && truth_value_p (TREE_CODE (arg0)))
11470 return pedantic_non_lvalue_loc (loc,
11471 fold_convert_loc (loc, type,
11472 invert_truthvalue_loc (loc,
11473 arg0)));
11474
11475 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11476 if (TREE_CODE (arg0) == LT_EXPR
11477 && integer_zerop (TREE_OPERAND (arg0, 1))
11478 && integer_zerop (op2)
11479 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11480 {
11481 /* sign_bit_p looks through both zero and sign extensions,
11482 but for this optimization only sign extensions are
11483 usable. */
11484 tree tem2 = TREE_OPERAND (arg0, 0);
11485 while (tem != tem2)
11486 {
11487 if (TREE_CODE (tem2) != NOP_EXPR
11488 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11489 {
11490 tem = NULL_TREE;
11491 break;
11492 }
11493 tem2 = TREE_OPERAND (tem2, 0);
11494 }
11495 /* sign_bit_p only checks ARG1 bits within A's precision.
11496 If <sign bit of A> has wider type than A, bits outside
11497 of A's precision in <sign bit of A> need to be checked.
11498 If they are all 0, this optimization needs to be done
11499 in unsigned A's type, if they are all 1 in signed A's type,
11500 otherwise this can't be done. */
11501 if (tem
11502 && TYPE_PRECISION (TREE_TYPE (tem))
11503 < TYPE_PRECISION (TREE_TYPE (arg1))
11504 && TYPE_PRECISION (TREE_TYPE (tem))
11505 < TYPE_PRECISION (type))
11506 {
11507 int inner_width, outer_width;
11508 tree tem_type;
11509
11510 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11511 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11512 if (outer_width > TYPE_PRECISION (type))
11513 outer_width = TYPE_PRECISION (type);
11514
11515 wide_int mask = wi::shifted_mask
11516 (inner_width, outer_width - inner_width, false,
11517 TYPE_PRECISION (TREE_TYPE (arg1)));
11518
11519 wide_int common = mask & wi::to_wide (arg1);
11520 if (common == mask)
11521 {
11522 tem_type = signed_type_for (TREE_TYPE (tem));
11523 tem = fold_convert_loc (loc, tem_type, tem);
11524 }
11525 else if (common == 0)
11526 {
11527 tem_type = unsigned_type_for (TREE_TYPE (tem));
11528 tem = fold_convert_loc (loc, tem_type, tem);
11529 }
11530 else
11531 tem = NULL;
11532 }
11533
11534 if (tem)
11535 return
11536 fold_convert_loc (loc, type,
11537 fold_build2_loc (loc, BIT_AND_EXPR,
11538 TREE_TYPE (tem), tem,
11539 fold_convert_loc (loc,
11540 TREE_TYPE (tem),
11541 arg1)));
11542 }
11543
11544 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11545 already handled above. */
11546 if (TREE_CODE (arg0) == BIT_AND_EXPR
11547 && integer_onep (TREE_OPERAND (arg0, 1))
11548 && integer_zerop (op2)
11549 && integer_pow2p (arg1))
11550 {
11551 tree tem = TREE_OPERAND (arg0, 0);
11552 STRIP_NOPS (tem);
11553 if (TREE_CODE (tem) == RSHIFT_EXPR
11554 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11555 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11556 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11557 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11558 fold_convert_loc (loc, type,
11559 TREE_OPERAND (tem, 0)),
11560 op1);
11561 }
11562
11563 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11564 is probably obsolete because the first operand should be a
11565 truth value (that's why we have the two cases above), but let's
11566 leave it in until we can confirm this for all front-ends. */
11567 if (integer_zerop (op2)
11568 && TREE_CODE (arg0) == NE_EXPR
11569 && integer_zerop (TREE_OPERAND (arg0, 1))
11570 && integer_pow2p (arg1)
11571 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11572 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11573 arg1, OEP_ONLY_CONST))
11574 return pedantic_non_lvalue_loc (loc,
11575 fold_convert_loc (loc, type,
11576 TREE_OPERAND (arg0, 0)));
11577
11578 /* Disable the transformations below for vectors, since
11579 fold_binary_op_with_conditional_arg may undo them immediately,
11580 yielding an infinite loop. */
11581 if (code == VEC_COND_EXPR)
11582 return NULL_TREE;
11583
11584 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11585 if (integer_zerop (op2)
11586 && truth_value_p (TREE_CODE (arg0))
11587 && truth_value_p (TREE_CODE (arg1))
11588 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11589 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11590 : TRUTH_ANDIF_EXPR,
11591 type, fold_convert_loc (loc, type, arg0), op1);
11592
11593 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11594 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11595 && truth_value_p (TREE_CODE (arg0))
11596 && truth_value_p (TREE_CODE (arg1))
11597 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11598 {
11599 location_t loc0 = expr_location_or (arg0, loc);
11600 /* Only perform transformation if ARG0 is easily inverted. */
11601 tem = fold_invert_truthvalue (loc0, arg0);
11602 if (tem)
11603 return fold_build2_loc (loc, code == VEC_COND_EXPR
11604 ? BIT_IOR_EXPR
11605 : TRUTH_ORIF_EXPR,
11606 type, fold_convert_loc (loc, type, tem),
11607 op1);
11608 }
11609
11610 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11611 if (integer_zerop (arg1)
11612 && truth_value_p (TREE_CODE (arg0))
11613 && truth_value_p (TREE_CODE (op2))
11614 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11615 {
11616 location_t loc0 = expr_location_or (arg0, loc);
11617 /* Only perform transformation if ARG0 is easily inverted. */
11618 tem = fold_invert_truthvalue (loc0, arg0);
11619 if (tem)
11620 return fold_build2_loc (loc, code == VEC_COND_EXPR
11621 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11622 type, fold_convert_loc (loc, type, tem),
11623 op2);
11624 }
11625
11626 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11627 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11628 && truth_value_p (TREE_CODE (arg0))
11629 && truth_value_p (TREE_CODE (op2))
11630 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11631 return fold_build2_loc (loc, code == VEC_COND_EXPR
11632 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11633 type, fold_convert_loc (loc, type, arg0), op2);
11634
11635 return NULL_TREE;
11636
11637 case CALL_EXPR:
11638 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11639 of fold_ternary on them. */
11640 gcc_unreachable ();
11641
11642 case BIT_FIELD_REF:
11643 if (TREE_CODE (arg0) == VECTOR_CST
11644 && (type == TREE_TYPE (TREE_TYPE (arg0))
11645 || (TREE_CODE (type) == VECTOR_TYPE
11646 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11647 {
11648 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11649 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11650 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11651 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11652
11653 if (n != 0
11654 && (idx % width) == 0
11655 && (n % width) == 0
11656 && known_le ((idx + n) / width,
11657 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11658 {
11659 idx = idx / width;
11660 n = n / width;
11661
11662 if (TREE_CODE (arg0) == VECTOR_CST)
11663 {
11664 if (n == 1)
11665 return VECTOR_CST_ELT (arg0, idx);
11666
11667 tree_vector_builder vals (type, n, 1);
11668 for (unsigned i = 0; i < n; ++i)
11669 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11670 return vals.build ();
11671 }
11672 }
11673 }
11674
11675 /* On constants we can use native encode/interpret to constant
11676 fold (nearly) all BIT_FIELD_REFs. */
11677 if (CONSTANT_CLASS_P (arg0)
11678 && can_native_interpret_type_p (type)
11679 && BITS_PER_UNIT == 8
11680 && tree_fits_uhwi_p (op1)
11681 && tree_fits_uhwi_p (op2))
11682 {
11683 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11684 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11685 /* Limit us to a reasonable amount of work. To relax the
11686 other limitations we need bit-shifting of the buffer
11687 and rounding up the size. */
11688 if (bitpos % BITS_PER_UNIT == 0
11689 && bitsize % BITS_PER_UNIT == 0
11690 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11691 {
11692 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11693 unsigned HOST_WIDE_INT len
11694 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11695 bitpos / BITS_PER_UNIT);
11696 if (len > 0
11697 && len * BITS_PER_UNIT >= bitsize)
11698 {
11699 tree v = native_interpret_expr (type, b,
11700 bitsize / BITS_PER_UNIT);
11701 if (v)
11702 return v;
11703 }
11704 }
11705 }
11706
11707 return NULL_TREE;
11708
11709 case FMA_EXPR:
11710 /* For integers we can decompose the FMA if possible. */
11711 if (TREE_CODE (arg0) == INTEGER_CST
11712 && TREE_CODE (arg1) == INTEGER_CST)
11713 return fold_build2_loc (loc, PLUS_EXPR, type,
11714 const_binop (MULT_EXPR, arg0, arg1), arg2);
11715 if (integer_zerop (arg2))
11716 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11717
11718 return fold_fma (loc, type, arg0, arg1, arg2);
11719
11720 case VEC_PERM_EXPR:
11721 if (TREE_CODE (arg2) == VECTOR_CST)
11722 {
11723 /* Build a vector of integers from the tree mask. */
11724 vec_perm_builder builder;
11725 if (!tree_to_vec_perm_builder (&builder, arg2))
11726 return NULL_TREE;
11727
11728 /* Create a vec_perm_indices for the integer vector. */
11729 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11730 bool single_arg = (op0 == op1);
11731 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11732
11733 /* Check for cases that fold to OP0 or OP1 in their original
11734 element order. */
11735 if (sel.series_p (0, 1, 0, 1))
11736 return op0;
11737 if (sel.series_p (0, 1, nelts, 1))
11738 return op1;
11739
11740 if (!single_arg)
11741 {
11742 if (sel.all_from_input_p (0))
11743 op1 = op0;
11744 else if (sel.all_from_input_p (1))
11745 {
11746 op0 = op1;
11747 sel.rotate_inputs (1);
11748 }
11749 }
11750
11751 if ((TREE_CODE (op0) == VECTOR_CST
11752 || TREE_CODE (op0) == CONSTRUCTOR)
11753 && (TREE_CODE (op1) == VECTOR_CST
11754 || TREE_CODE (op1) == CONSTRUCTOR))
11755 {
11756 tree t = fold_vec_perm (type, op0, op1, sel);
11757 if (t != NULL_TREE)
11758 return t;
11759 }
11760
11761 bool changed = (op0 == op1 && !single_arg);
11762
11763 /* Generate a canonical form of the selector. */
11764 if (arg2 == op2 && sel.encoding () != builder)
11765 {
11766 /* Some targets are deficient and fail to expand a single
11767 argument permutation while still allowing an equivalent
11768 2-argument version. */
11769 if (sel.ninputs () == 2
11770 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11771 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11772 else
11773 {
11774 vec_perm_indices sel2 (builder, 2, nelts);
11775 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11776 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11777 else
11778 /* Not directly supported with either encoding,
11779 so use the preferred form. */
11780 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11781 }
11782 changed = true;
11783 }
11784
11785 if (changed)
11786 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11787 }
11788 return NULL_TREE;
11789
11790 case BIT_INSERT_EXPR:
11791 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11792 if (TREE_CODE (arg0) == INTEGER_CST
11793 && TREE_CODE (arg1) == INTEGER_CST)
11794 {
11795 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11796 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11797 wide_int tem = (wi::to_wide (arg0)
11798 & wi::shifted_mask (bitpos, bitsize, true,
11799 TYPE_PRECISION (type)));
11800 wide_int tem2
11801 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11802 bitsize), bitpos);
11803 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11804 }
11805 else if (TREE_CODE (arg0) == VECTOR_CST
11806 && CONSTANT_CLASS_P (arg1)
11807 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11808 TREE_TYPE (arg1)))
11809 {
11810 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11811 unsigned HOST_WIDE_INT elsize
11812 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11813 if (bitpos % elsize == 0)
11814 {
11815 unsigned k = bitpos / elsize;
11816 unsigned HOST_WIDE_INT nelts;
11817 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11818 return arg0;
11819 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11820 {
11821 tree_vector_builder elts (type, nelts, 1);
11822 elts.quick_grow (nelts);
11823 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11824 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11825 return elts.build ();
11826 }
11827 }
11828 }
11829 return NULL_TREE;
11830
11831 default:
11832 return NULL_TREE;
11833 } /* switch (code) */
11834 }
11835
11836 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11837 of an array (or vector). */
11838
11839 tree
11840 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11841 {
11842 tree index_type = NULL_TREE;
11843 offset_int low_bound = 0;
11844
11845 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11846 {
11847 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11848 if (domain_type && TYPE_MIN_VALUE (domain_type))
11849 {
11850 /* Static constructors for variably sized objects makes no sense. */
11851 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11852 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11853 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11854 }
11855 }
11856
11857 if (index_type)
11858 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11859 TYPE_SIGN (index_type));
11860
11861 offset_int index = low_bound - 1;
11862 if (index_type)
11863 index = wi::ext (index, TYPE_PRECISION (index_type),
11864 TYPE_SIGN (index_type));
11865
11866 offset_int max_index;
11867 unsigned HOST_WIDE_INT cnt;
11868 tree cfield, cval;
11869
11870 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11871 {
11872 /* Array constructor might explicitly set index, or specify a range,
11873 or leave index NULL meaning that it is next index after previous
11874 one. */
11875 if (cfield)
11876 {
11877 if (TREE_CODE (cfield) == INTEGER_CST)
11878 max_index = index = wi::to_offset (cfield);
11879 else
11880 {
11881 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11882 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11883 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11884 }
11885 }
11886 else
11887 {
11888 index += 1;
11889 if (index_type)
11890 index = wi::ext (index, TYPE_PRECISION (index_type),
11891 TYPE_SIGN (index_type));
11892 max_index = index;
11893 }
11894
11895 /* Do we have match? */
11896 if (wi::cmpu (access_index, index) >= 0
11897 && wi::cmpu (access_index, max_index) <= 0)
11898 return cval;
11899 }
11900 return NULL_TREE;
11901 }
11902
11903 /* Perform constant folding and related simplification of EXPR.
11904 The related simplifications include x*1 => x, x*0 => 0, etc.,
11905 and application of the associative law.
11906 NOP_EXPR conversions may be removed freely (as long as we
11907 are careful not to change the type of the overall expression).
11908 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11909 but we can constant-fold them if they have constant operands. */
11910
11911 #ifdef ENABLE_FOLD_CHECKING
11912 # define fold(x) fold_1 (x)
11913 static tree fold_1 (tree);
11914 static
11915 #endif
11916 tree
11917 fold (tree expr)
11918 {
11919 const tree t = expr;
11920 enum tree_code code = TREE_CODE (t);
11921 enum tree_code_class kind = TREE_CODE_CLASS (code);
11922 tree tem;
11923 location_t loc = EXPR_LOCATION (expr);
11924
11925 /* Return right away if a constant. */
11926 if (kind == tcc_constant)
11927 return t;
11928
11929 /* CALL_EXPR-like objects with variable numbers of operands are
11930 treated specially. */
11931 if (kind == tcc_vl_exp)
11932 {
11933 if (code == CALL_EXPR)
11934 {
11935 tem = fold_call_expr (loc, expr, false);
11936 return tem ? tem : expr;
11937 }
11938 return expr;
11939 }
11940
11941 if (IS_EXPR_CODE_CLASS (kind))
11942 {
11943 tree type = TREE_TYPE (t);
11944 tree op0, op1, op2;
11945
11946 switch (TREE_CODE_LENGTH (code))
11947 {
11948 case 1:
11949 op0 = TREE_OPERAND (t, 0);
11950 tem = fold_unary_loc (loc, code, type, op0);
11951 return tem ? tem : expr;
11952 case 2:
11953 op0 = TREE_OPERAND (t, 0);
11954 op1 = TREE_OPERAND (t, 1);
11955 tem = fold_binary_loc (loc, code, type, op0, op1);
11956 return tem ? tem : expr;
11957 case 3:
11958 op0 = TREE_OPERAND (t, 0);
11959 op1 = TREE_OPERAND (t, 1);
11960 op2 = TREE_OPERAND (t, 2);
11961 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11962 return tem ? tem : expr;
11963 default:
11964 break;
11965 }
11966 }
11967
11968 switch (code)
11969 {
11970 case ARRAY_REF:
11971 {
11972 tree op0 = TREE_OPERAND (t, 0);
11973 tree op1 = TREE_OPERAND (t, 1);
11974
11975 if (TREE_CODE (op1) == INTEGER_CST
11976 && TREE_CODE (op0) == CONSTRUCTOR
11977 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11978 {
11979 tree val = get_array_ctor_element_at_index (op0,
11980 wi::to_offset (op1));
11981 if (val)
11982 return val;
11983 }
11984
11985 return t;
11986 }
11987
11988 /* Return a VECTOR_CST if possible. */
11989 case CONSTRUCTOR:
11990 {
11991 tree type = TREE_TYPE (t);
11992 if (TREE_CODE (type) != VECTOR_TYPE)
11993 return t;
11994
11995 unsigned i;
11996 tree val;
11997 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11998 if (! CONSTANT_CLASS_P (val))
11999 return t;
12000
12001 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12002 }
12003
12004 case CONST_DECL:
12005 return fold (DECL_INITIAL (t));
12006
12007 default:
12008 return t;
12009 } /* switch (code) */
12010 }
12011
12012 #ifdef ENABLE_FOLD_CHECKING
12013 #undef fold
12014
12015 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12016 hash_table<nofree_ptr_hash<const tree_node> > *);
12017 static void fold_check_failed (const_tree, const_tree);
12018 void print_fold_checksum (const_tree);
12019
12020 /* When --enable-checking=fold, compute a digest of expr before
12021 and after actual fold call to see if fold did not accidentally
12022 change original expr. */
12023
12024 tree
12025 fold (tree expr)
12026 {
12027 tree ret;
12028 struct md5_ctx ctx;
12029 unsigned char checksum_before[16], checksum_after[16];
12030 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12031
12032 md5_init_ctx (&ctx);
12033 fold_checksum_tree (expr, &ctx, &ht);
12034 md5_finish_ctx (&ctx, checksum_before);
12035 ht.empty ();
12036
12037 ret = fold_1 (expr);
12038
12039 md5_init_ctx (&ctx);
12040 fold_checksum_tree (expr, &ctx, &ht);
12041 md5_finish_ctx (&ctx, checksum_after);
12042
12043 if (memcmp (checksum_before, checksum_after, 16))
12044 fold_check_failed (expr, ret);
12045
12046 return ret;
12047 }
12048
12049 void
12050 print_fold_checksum (const_tree expr)
12051 {
12052 struct md5_ctx ctx;
12053 unsigned char checksum[16], cnt;
12054 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12055
12056 md5_init_ctx (&ctx);
12057 fold_checksum_tree (expr, &ctx, &ht);
12058 md5_finish_ctx (&ctx, checksum);
12059 for (cnt = 0; cnt < 16; ++cnt)
12060 fprintf (stderr, "%02x", checksum[cnt]);
12061 putc ('\n', stderr);
12062 }
12063
12064 static void
12065 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12066 {
12067 internal_error ("fold check: original tree changed by fold");
12068 }
12069
12070 static void
12071 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12072 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12073 {
12074 const tree_node **slot;
12075 enum tree_code code;
12076 union tree_node buf;
12077 int i, len;
12078
12079 recursive_label:
12080 if (expr == NULL)
12081 return;
12082 slot = ht->find_slot (expr, INSERT);
12083 if (*slot != NULL)
12084 return;
12085 *slot = expr;
12086 code = TREE_CODE (expr);
12087 if (TREE_CODE_CLASS (code) == tcc_declaration
12088 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12089 {
12090 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12091 memcpy ((char *) &buf, expr, tree_size (expr));
12092 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12093 buf.decl_with_vis.symtab_node = NULL;
12094 expr = (tree) &buf;
12095 }
12096 else if (TREE_CODE_CLASS (code) == tcc_type
12097 && (TYPE_POINTER_TO (expr)
12098 || TYPE_REFERENCE_TO (expr)
12099 || TYPE_CACHED_VALUES_P (expr)
12100 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12101 || TYPE_NEXT_VARIANT (expr)
12102 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12103 {
12104 /* Allow these fields to be modified. */
12105 tree tmp;
12106 memcpy ((char *) &buf, expr, tree_size (expr));
12107 expr = tmp = (tree) &buf;
12108 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12109 TYPE_POINTER_TO (tmp) = NULL;
12110 TYPE_REFERENCE_TO (tmp) = NULL;
12111 TYPE_NEXT_VARIANT (tmp) = NULL;
12112 TYPE_ALIAS_SET (tmp) = -1;
12113 if (TYPE_CACHED_VALUES_P (tmp))
12114 {
12115 TYPE_CACHED_VALUES_P (tmp) = 0;
12116 TYPE_CACHED_VALUES (tmp) = NULL;
12117 }
12118 }
12119 md5_process_bytes (expr, tree_size (expr), ctx);
12120 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12121 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12122 if (TREE_CODE_CLASS (code) != tcc_type
12123 && TREE_CODE_CLASS (code) != tcc_declaration
12124 && code != TREE_LIST
12125 && code != SSA_NAME
12126 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12127 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12128 switch (TREE_CODE_CLASS (code))
12129 {
12130 case tcc_constant:
12131 switch (code)
12132 {
12133 case STRING_CST:
12134 md5_process_bytes (TREE_STRING_POINTER (expr),
12135 TREE_STRING_LENGTH (expr), ctx);
12136 break;
12137 case COMPLEX_CST:
12138 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12139 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12140 break;
12141 case VECTOR_CST:
12142 len = vector_cst_encoded_nelts (expr);
12143 for (i = 0; i < len; ++i)
12144 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12145 break;
12146 default:
12147 break;
12148 }
12149 break;
12150 case tcc_exceptional:
12151 switch (code)
12152 {
12153 case TREE_LIST:
12154 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12155 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12156 expr = TREE_CHAIN (expr);
12157 goto recursive_label;
12158 break;
12159 case TREE_VEC:
12160 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12161 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12162 break;
12163 default:
12164 break;
12165 }
12166 break;
12167 case tcc_expression:
12168 case tcc_reference:
12169 case tcc_comparison:
12170 case tcc_unary:
12171 case tcc_binary:
12172 case tcc_statement:
12173 case tcc_vl_exp:
12174 len = TREE_OPERAND_LENGTH (expr);
12175 for (i = 0; i < len; ++i)
12176 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12177 break;
12178 case tcc_declaration:
12179 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12180 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12181 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12182 {
12183 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12184 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12185 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12186 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12187 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12188 }
12189
12190 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12191 {
12192 if (TREE_CODE (expr) == FUNCTION_DECL)
12193 {
12194 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12195 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12196 }
12197 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12198 }
12199 break;
12200 case tcc_type:
12201 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12202 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12203 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12204 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12205 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12206 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12207 if (INTEGRAL_TYPE_P (expr)
12208 || SCALAR_FLOAT_TYPE_P (expr))
12209 {
12210 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12211 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12212 }
12213 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12214 if (TREE_CODE (expr) == RECORD_TYPE
12215 || TREE_CODE (expr) == UNION_TYPE
12216 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12217 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12218 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12219 break;
12220 default:
12221 break;
12222 }
12223 }
12224
12225 /* Helper function for outputting the checksum of a tree T. When
12226 debugging with gdb, you can "define mynext" to be "next" followed
12227 by "call debug_fold_checksum (op0)", then just trace down till the
12228 outputs differ. */
12229
12230 DEBUG_FUNCTION void
12231 debug_fold_checksum (const_tree t)
12232 {
12233 int i;
12234 unsigned char checksum[16];
12235 struct md5_ctx ctx;
12236 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12237
12238 md5_init_ctx (&ctx);
12239 fold_checksum_tree (t, &ctx, &ht);
12240 md5_finish_ctx (&ctx, checksum);
12241 ht.empty ();
12242
12243 for (i = 0; i < 16; i++)
12244 fprintf (stderr, "%d ", checksum[i]);
12245
12246 fprintf (stderr, "\n");
12247 }
12248
12249 #endif
12250
12251 /* Fold a unary tree expression with code CODE of type TYPE with an
12252 operand OP0. LOC is the location of the resulting expression.
12253 Return a folded expression if successful. Otherwise, return a tree
12254 expression with code CODE of type TYPE with an operand OP0. */
12255
12256 tree
12257 fold_build1_loc (location_t loc,
12258 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12259 {
12260 tree tem;
12261 #ifdef ENABLE_FOLD_CHECKING
12262 unsigned char checksum_before[16], checksum_after[16];
12263 struct md5_ctx ctx;
12264 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12265
12266 md5_init_ctx (&ctx);
12267 fold_checksum_tree (op0, &ctx, &ht);
12268 md5_finish_ctx (&ctx, checksum_before);
12269 ht.empty ();
12270 #endif
12271
12272 tem = fold_unary_loc (loc, code, type, op0);
12273 if (!tem)
12274 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12275
12276 #ifdef ENABLE_FOLD_CHECKING
12277 md5_init_ctx (&ctx);
12278 fold_checksum_tree (op0, &ctx, &ht);
12279 md5_finish_ctx (&ctx, checksum_after);
12280
12281 if (memcmp (checksum_before, checksum_after, 16))
12282 fold_check_failed (op0, tem);
12283 #endif
12284 return tem;
12285 }
12286
12287 /* Fold a binary tree expression with code CODE of type TYPE with
12288 operands OP0 and OP1. LOC is the location of the resulting
12289 expression. Return a folded expression if successful. Otherwise,
12290 return a tree expression with code CODE of type TYPE with operands
12291 OP0 and OP1. */
12292
12293 tree
12294 fold_build2_loc (location_t loc,
12295 enum tree_code code, tree type, tree op0, tree op1
12296 MEM_STAT_DECL)
12297 {
12298 tree tem;
12299 #ifdef ENABLE_FOLD_CHECKING
12300 unsigned char checksum_before_op0[16],
12301 checksum_before_op1[16],
12302 checksum_after_op0[16],
12303 checksum_after_op1[16];
12304 struct md5_ctx ctx;
12305 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12306
12307 md5_init_ctx (&ctx);
12308 fold_checksum_tree (op0, &ctx, &ht);
12309 md5_finish_ctx (&ctx, checksum_before_op0);
12310 ht.empty ();
12311
12312 md5_init_ctx (&ctx);
12313 fold_checksum_tree (op1, &ctx, &ht);
12314 md5_finish_ctx (&ctx, checksum_before_op1);
12315 ht.empty ();
12316 #endif
12317
12318 tem = fold_binary_loc (loc, code, type, op0, op1);
12319 if (!tem)
12320 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12321
12322 #ifdef ENABLE_FOLD_CHECKING
12323 md5_init_ctx (&ctx);
12324 fold_checksum_tree (op0, &ctx, &ht);
12325 md5_finish_ctx (&ctx, checksum_after_op0);
12326 ht.empty ();
12327
12328 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12329 fold_check_failed (op0, tem);
12330
12331 md5_init_ctx (&ctx);
12332 fold_checksum_tree (op1, &ctx, &ht);
12333 md5_finish_ctx (&ctx, checksum_after_op1);
12334
12335 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12336 fold_check_failed (op1, tem);
12337 #endif
12338 return tem;
12339 }
12340
12341 /* Fold a ternary tree expression with code CODE of type TYPE with
12342 operands OP0, OP1, and OP2. Return a folded expression if
12343 successful. Otherwise, return a tree expression with code CODE of
12344 type TYPE with operands OP0, OP1, and OP2. */
12345
12346 tree
12347 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12348 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12349 {
12350 tree tem;
12351 #ifdef ENABLE_FOLD_CHECKING
12352 unsigned char checksum_before_op0[16],
12353 checksum_before_op1[16],
12354 checksum_before_op2[16],
12355 checksum_after_op0[16],
12356 checksum_after_op1[16],
12357 checksum_after_op2[16];
12358 struct md5_ctx ctx;
12359 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12360
12361 md5_init_ctx (&ctx);
12362 fold_checksum_tree (op0, &ctx, &ht);
12363 md5_finish_ctx (&ctx, checksum_before_op0);
12364 ht.empty ();
12365
12366 md5_init_ctx (&ctx);
12367 fold_checksum_tree (op1, &ctx, &ht);
12368 md5_finish_ctx (&ctx, checksum_before_op1);
12369 ht.empty ();
12370
12371 md5_init_ctx (&ctx);
12372 fold_checksum_tree (op2, &ctx, &ht);
12373 md5_finish_ctx (&ctx, checksum_before_op2);
12374 ht.empty ();
12375 #endif
12376
12377 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12378 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12379 if (!tem)
12380 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12381
12382 #ifdef ENABLE_FOLD_CHECKING
12383 md5_init_ctx (&ctx);
12384 fold_checksum_tree (op0, &ctx, &ht);
12385 md5_finish_ctx (&ctx, checksum_after_op0);
12386 ht.empty ();
12387
12388 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12389 fold_check_failed (op0, tem);
12390
12391 md5_init_ctx (&ctx);
12392 fold_checksum_tree (op1, &ctx, &ht);
12393 md5_finish_ctx (&ctx, checksum_after_op1);
12394 ht.empty ();
12395
12396 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12397 fold_check_failed (op1, tem);
12398
12399 md5_init_ctx (&ctx);
12400 fold_checksum_tree (op2, &ctx, &ht);
12401 md5_finish_ctx (&ctx, checksum_after_op2);
12402
12403 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12404 fold_check_failed (op2, tem);
12405 #endif
12406 return tem;
12407 }
12408
12409 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12410 arguments in ARGARRAY, and a null static chain.
12411 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12412 of type TYPE from the given operands as constructed by build_call_array. */
12413
12414 tree
12415 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12416 int nargs, tree *argarray)
12417 {
12418 tree tem;
12419 #ifdef ENABLE_FOLD_CHECKING
12420 unsigned char checksum_before_fn[16],
12421 checksum_before_arglist[16],
12422 checksum_after_fn[16],
12423 checksum_after_arglist[16];
12424 struct md5_ctx ctx;
12425 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12426 int i;
12427
12428 md5_init_ctx (&ctx);
12429 fold_checksum_tree (fn, &ctx, &ht);
12430 md5_finish_ctx (&ctx, checksum_before_fn);
12431 ht.empty ();
12432
12433 md5_init_ctx (&ctx);
12434 for (i = 0; i < nargs; i++)
12435 fold_checksum_tree (argarray[i], &ctx, &ht);
12436 md5_finish_ctx (&ctx, checksum_before_arglist);
12437 ht.empty ();
12438 #endif
12439
12440 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12441 if (!tem)
12442 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12443
12444 #ifdef ENABLE_FOLD_CHECKING
12445 md5_init_ctx (&ctx);
12446 fold_checksum_tree (fn, &ctx, &ht);
12447 md5_finish_ctx (&ctx, checksum_after_fn);
12448 ht.empty ();
12449
12450 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12451 fold_check_failed (fn, tem);
12452
12453 md5_init_ctx (&ctx);
12454 for (i = 0; i < nargs; i++)
12455 fold_checksum_tree (argarray[i], &ctx, &ht);
12456 md5_finish_ctx (&ctx, checksum_after_arglist);
12457
12458 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12459 fold_check_failed (NULL_TREE, tem);
12460 #endif
12461 return tem;
12462 }
12463
12464 /* Perform constant folding and related simplification of initializer
12465 expression EXPR. These behave identically to "fold_buildN" but ignore
12466 potential run-time traps and exceptions that fold must preserve. */
12467
12468 #define START_FOLD_INIT \
12469 int saved_signaling_nans = flag_signaling_nans;\
12470 int saved_trapping_math = flag_trapping_math;\
12471 int saved_rounding_math = flag_rounding_math;\
12472 int saved_trapv = flag_trapv;\
12473 int saved_folding_initializer = folding_initializer;\
12474 flag_signaling_nans = 0;\
12475 flag_trapping_math = 0;\
12476 flag_rounding_math = 0;\
12477 flag_trapv = 0;\
12478 folding_initializer = 1;
12479
12480 #define END_FOLD_INIT \
12481 flag_signaling_nans = saved_signaling_nans;\
12482 flag_trapping_math = saved_trapping_math;\
12483 flag_rounding_math = saved_rounding_math;\
12484 flag_trapv = saved_trapv;\
12485 folding_initializer = saved_folding_initializer;
12486
12487 tree
12488 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12489 tree type, tree op)
12490 {
12491 tree result;
12492 START_FOLD_INIT;
12493
12494 result = fold_build1_loc (loc, code, type, op);
12495
12496 END_FOLD_INIT;
12497 return result;
12498 }
12499
12500 tree
12501 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12502 tree type, tree op0, tree op1)
12503 {
12504 tree result;
12505 START_FOLD_INIT;
12506
12507 result = fold_build2_loc (loc, code, type, op0, op1);
12508
12509 END_FOLD_INIT;
12510 return result;
12511 }
12512
12513 tree
12514 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12515 int nargs, tree *argarray)
12516 {
12517 tree result;
12518 START_FOLD_INIT;
12519
12520 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12521
12522 END_FOLD_INIT;
12523 return result;
12524 }
12525
12526 #undef START_FOLD_INIT
12527 #undef END_FOLD_INIT
12528
12529 /* Determine if first argument is a multiple of second argument. Return 0 if
12530 it is not, or we cannot easily determined it to be.
12531
12532 An example of the sort of thing we care about (at this point; this routine
12533 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12534 fold cases do now) is discovering that
12535
12536 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12537
12538 is a multiple of
12539
12540 SAVE_EXPR (J * 8)
12541
12542 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12543
12544 This code also handles discovering that
12545
12546 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12547
12548 is a multiple of 8 so we don't have to worry about dealing with a
12549 possible remainder.
12550
12551 Note that we *look* inside a SAVE_EXPR only to determine how it was
12552 calculated; it is not safe for fold to do much of anything else with the
12553 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12554 at run time. For example, the latter example above *cannot* be implemented
12555 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12556 evaluation time of the original SAVE_EXPR is not necessarily the same at
12557 the time the new expression is evaluated. The only optimization of this
12558 sort that would be valid is changing
12559
12560 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12561
12562 divided by 8 to
12563
12564 SAVE_EXPR (I) * SAVE_EXPR (J)
12565
12566 (where the same SAVE_EXPR (J) is used in the original and the
12567 transformed version). */
12568
12569 int
12570 multiple_of_p (tree type, const_tree top, const_tree bottom)
12571 {
12572 gimple *stmt;
12573 tree t1, op1, op2;
12574
12575 if (operand_equal_p (top, bottom, 0))
12576 return 1;
12577
12578 if (TREE_CODE (type) != INTEGER_TYPE)
12579 return 0;
12580
12581 switch (TREE_CODE (top))
12582 {
12583 case BIT_AND_EXPR:
12584 /* Bitwise and provides a power of two multiple. If the mask is
12585 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12586 if (!integer_pow2p (bottom))
12587 return 0;
12588 /* FALLTHRU */
12589
12590 case MULT_EXPR:
12591 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12592 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12593
12594 case MINUS_EXPR:
12595 /* It is impossible to prove if op0 - op1 is multiple of bottom
12596 precisely, so be conservative here checking if both op0 and op1
12597 are multiple of bottom. Note we check the second operand first
12598 since it's usually simpler. */
12599 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12600 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12601
12602 case PLUS_EXPR:
12603 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12604 as op0 - 3 if the expression has unsigned type. For example,
12605 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12606 op1 = TREE_OPERAND (top, 1);
12607 if (TYPE_UNSIGNED (type)
12608 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12609 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12610 return (multiple_of_p (type, op1, bottom)
12611 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12612
12613 case LSHIFT_EXPR:
12614 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12615 {
12616 op1 = TREE_OPERAND (top, 1);
12617 /* const_binop may not detect overflow correctly,
12618 so check for it explicitly here. */
12619 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12620 wi::to_wide (op1))
12621 && (t1 = fold_convert (type,
12622 const_binop (LSHIFT_EXPR, size_one_node,
12623 op1))) != 0
12624 && !TREE_OVERFLOW (t1))
12625 return multiple_of_p (type, t1, bottom);
12626 }
12627 return 0;
12628
12629 case NOP_EXPR:
12630 /* Can't handle conversions from non-integral or wider integral type. */
12631 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12632 || (TYPE_PRECISION (type)
12633 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12634 return 0;
12635
12636 /* fall through */
12637
12638 case SAVE_EXPR:
12639 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12640
12641 case COND_EXPR:
12642 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12643 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12644
12645 case INTEGER_CST:
12646 if (TREE_CODE (bottom) != INTEGER_CST
12647 || integer_zerop (bottom)
12648 || (TYPE_UNSIGNED (type)
12649 && (tree_int_cst_sgn (top) < 0
12650 || tree_int_cst_sgn (bottom) < 0)))
12651 return 0;
12652 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12653 SIGNED);
12654
12655 case SSA_NAME:
12656 if (TREE_CODE (bottom) == INTEGER_CST
12657 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12658 && gimple_code (stmt) == GIMPLE_ASSIGN)
12659 {
12660 enum tree_code code = gimple_assign_rhs_code (stmt);
12661
12662 /* Check for special cases to see if top is defined as multiple
12663 of bottom:
12664
12665 top = (X & ~(bottom - 1) ; bottom is power of 2
12666
12667 or
12668
12669 Y = X % bottom
12670 top = X - Y. */
12671 if (code == BIT_AND_EXPR
12672 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12673 && TREE_CODE (op2) == INTEGER_CST
12674 && integer_pow2p (bottom)
12675 && wi::multiple_of_p (wi::to_widest (op2),
12676 wi::to_widest (bottom), UNSIGNED))
12677 return 1;
12678
12679 op1 = gimple_assign_rhs1 (stmt);
12680 if (code == MINUS_EXPR
12681 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12682 && TREE_CODE (op2) == SSA_NAME
12683 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12684 && gimple_code (stmt) == GIMPLE_ASSIGN
12685 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12686 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12687 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12688 return 1;
12689 }
12690
12691 /* fall through */
12692
12693 default:
12694 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12695 return multiple_p (wi::to_poly_widest (top),
12696 wi::to_poly_widest (bottom));
12697
12698 return 0;
12699 }
12700 }
12701
12702 #define tree_expr_nonnegative_warnv_p(X, Y) \
12703 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12704
12705 #define RECURSE(X) \
12706 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12707
12708 /* Return true if CODE or TYPE is known to be non-negative. */
12709
12710 static bool
12711 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12712 {
12713 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12714 && truth_value_p (code))
12715 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12716 have a signed:1 type (where the value is -1 and 0). */
12717 return true;
12718 return false;
12719 }
12720
12721 /* Return true if (CODE OP0) is known to be non-negative. If the return
12722 value is based on the assumption that signed overflow is undefined,
12723 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12724 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12725
12726 bool
12727 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12728 bool *strict_overflow_p, int depth)
12729 {
12730 if (TYPE_UNSIGNED (type))
12731 return true;
12732
12733 switch (code)
12734 {
12735 case ABS_EXPR:
12736 /* We can't return 1 if flag_wrapv is set because
12737 ABS_EXPR<INT_MIN> = INT_MIN. */
12738 if (!ANY_INTEGRAL_TYPE_P (type))
12739 return true;
12740 if (TYPE_OVERFLOW_UNDEFINED (type))
12741 {
12742 *strict_overflow_p = true;
12743 return true;
12744 }
12745 break;
12746
12747 case NON_LVALUE_EXPR:
12748 case FLOAT_EXPR:
12749 case FIX_TRUNC_EXPR:
12750 return RECURSE (op0);
12751
12752 CASE_CONVERT:
12753 {
12754 tree inner_type = TREE_TYPE (op0);
12755 tree outer_type = type;
12756
12757 if (TREE_CODE (outer_type) == REAL_TYPE)
12758 {
12759 if (TREE_CODE (inner_type) == REAL_TYPE)
12760 return RECURSE (op0);
12761 if (INTEGRAL_TYPE_P (inner_type))
12762 {
12763 if (TYPE_UNSIGNED (inner_type))
12764 return true;
12765 return RECURSE (op0);
12766 }
12767 }
12768 else if (INTEGRAL_TYPE_P (outer_type))
12769 {
12770 if (TREE_CODE (inner_type) == REAL_TYPE)
12771 return RECURSE (op0);
12772 if (INTEGRAL_TYPE_P (inner_type))
12773 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12774 && TYPE_UNSIGNED (inner_type);
12775 }
12776 }
12777 break;
12778
12779 default:
12780 return tree_simple_nonnegative_warnv_p (code, type);
12781 }
12782
12783 /* We don't know sign of `t', so be conservative and return false. */
12784 return false;
12785 }
12786
12787 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12788 value is based on the assumption that signed overflow is undefined,
12789 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12790 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12791
12792 bool
12793 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12794 tree op1, bool *strict_overflow_p,
12795 int depth)
12796 {
12797 if (TYPE_UNSIGNED (type))
12798 return true;
12799
12800 switch (code)
12801 {
12802 case POINTER_PLUS_EXPR:
12803 case PLUS_EXPR:
12804 if (FLOAT_TYPE_P (type))
12805 return RECURSE (op0) && RECURSE (op1);
12806
12807 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12808 both unsigned and at least 2 bits shorter than the result. */
12809 if (TREE_CODE (type) == INTEGER_TYPE
12810 && TREE_CODE (op0) == NOP_EXPR
12811 && TREE_CODE (op1) == NOP_EXPR)
12812 {
12813 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12814 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12815 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12816 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12817 {
12818 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12819 TYPE_PRECISION (inner2)) + 1;
12820 return prec < TYPE_PRECISION (type);
12821 }
12822 }
12823 break;
12824
12825 case MULT_EXPR:
12826 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12827 {
12828 /* x * x is always non-negative for floating point x
12829 or without overflow. */
12830 if (operand_equal_p (op0, op1, 0)
12831 || (RECURSE (op0) && RECURSE (op1)))
12832 {
12833 if (ANY_INTEGRAL_TYPE_P (type)
12834 && TYPE_OVERFLOW_UNDEFINED (type))
12835 *strict_overflow_p = true;
12836 return true;
12837 }
12838 }
12839
12840 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12841 both unsigned and their total bits is shorter than the result. */
12842 if (TREE_CODE (type) == INTEGER_TYPE
12843 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12844 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12845 {
12846 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12847 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12848 : TREE_TYPE (op0);
12849 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12850 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12851 : TREE_TYPE (op1);
12852
12853 bool unsigned0 = TYPE_UNSIGNED (inner0);
12854 bool unsigned1 = TYPE_UNSIGNED (inner1);
12855
12856 if (TREE_CODE (op0) == INTEGER_CST)
12857 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12858
12859 if (TREE_CODE (op1) == INTEGER_CST)
12860 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12861
12862 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12863 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12864 {
12865 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12866 ? tree_int_cst_min_precision (op0, UNSIGNED)
12867 : TYPE_PRECISION (inner0);
12868
12869 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12870 ? tree_int_cst_min_precision (op1, UNSIGNED)
12871 : TYPE_PRECISION (inner1);
12872
12873 return precision0 + precision1 < TYPE_PRECISION (type);
12874 }
12875 }
12876 return false;
12877
12878 case BIT_AND_EXPR:
12879 case MAX_EXPR:
12880 return RECURSE (op0) || RECURSE (op1);
12881
12882 case BIT_IOR_EXPR:
12883 case BIT_XOR_EXPR:
12884 case MIN_EXPR:
12885 case RDIV_EXPR:
12886 case TRUNC_DIV_EXPR:
12887 case CEIL_DIV_EXPR:
12888 case FLOOR_DIV_EXPR:
12889 case ROUND_DIV_EXPR:
12890 return RECURSE (op0) && RECURSE (op1);
12891
12892 case TRUNC_MOD_EXPR:
12893 return RECURSE (op0);
12894
12895 case FLOOR_MOD_EXPR:
12896 return RECURSE (op1);
12897
12898 case CEIL_MOD_EXPR:
12899 case ROUND_MOD_EXPR:
12900 default:
12901 return tree_simple_nonnegative_warnv_p (code, type);
12902 }
12903
12904 /* We don't know sign of `t', so be conservative and return false. */
12905 return false;
12906 }
12907
12908 /* Return true if T is known to be non-negative. If the return
12909 value is based on the assumption that signed overflow is undefined,
12910 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12911 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12912
12913 bool
12914 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12915 {
12916 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12917 return true;
12918
12919 switch (TREE_CODE (t))
12920 {
12921 case INTEGER_CST:
12922 return tree_int_cst_sgn (t) >= 0;
12923
12924 case REAL_CST:
12925 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12926
12927 case FIXED_CST:
12928 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12929
12930 case COND_EXPR:
12931 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12932
12933 case SSA_NAME:
12934 /* Limit the depth of recursion to avoid quadratic behavior.
12935 This is expected to catch almost all occurrences in practice.
12936 If this code misses important cases that unbounded recursion
12937 would not, passes that need this information could be revised
12938 to provide it through dataflow propagation. */
12939 return (!name_registered_for_update_p (t)
12940 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12941 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12942 strict_overflow_p, depth));
12943
12944 default:
12945 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12946 }
12947 }
12948
12949 /* Return true if T is known to be non-negative. If the return
12950 value is based on the assumption that signed overflow is undefined,
12951 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12952 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12953
12954 bool
12955 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12956 bool *strict_overflow_p, int depth)
12957 {
12958 switch (fn)
12959 {
12960 CASE_CFN_ACOS:
12961 CASE_CFN_ACOSH:
12962 CASE_CFN_CABS:
12963 CASE_CFN_COSH:
12964 CASE_CFN_ERFC:
12965 CASE_CFN_EXP:
12966 CASE_CFN_EXP10:
12967 CASE_CFN_EXP2:
12968 CASE_CFN_FABS:
12969 CASE_CFN_FDIM:
12970 CASE_CFN_HYPOT:
12971 CASE_CFN_POW10:
12972 CASE_CFN_FFS:
12973 CASE_CFN_PARITY:
12974 CASE_CFN_POPCOUNT:
12975 CASE_CFN_CLZ:
12976 CASE_CFN_CLRSB:
12977 case CFN_BUILT_IN_BSWAP32:
12978 case CFN_BUILT_IN_BSWAP64:
12979 /* Always true. */
12980 return true;
12981
12982 CASE_CFN_SQRT:
12983 CASE_CFN_SQRT_FN:
12984 /* sqrt(-0.0) is -0.0. */
12985 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12986 return true;
12987 return RECURSE (arg0);
12988
12989 CASE_CFN_ASINH:
12990 CASE_CFN_ATAN:
12991 CASE_CFN_ATANH:
12992 CASE_CFN_CBRT:
12993 CASE_CFN_CEIL:
12994 CASE_CFN_CEIL_FN:
12995 CASE_CFN_ERF:
12996 CASE_CFN_EXPM1:
12997 CASE_CFN_FLOOR:
12998 CASE_CFN_FLOOR_FN:
12999 CASE_CFN_FMOD:
13000 CASE_CFN_FREXP:
13001 CASE_CFN_ICEIL:
13002 CASE_CFN_IFLOOR:
13003 CASE_CFN_IRINT:
13004 CASE_CFN_IROUND:
13005 CASE_CFN_LCEIL:
13006 CASE_CFN_LDEXP:
13007 CASE_CFN_LFLOOR:
13008 CASE_CFN_LLCEIL:
13009 CASE_CFN_LLFLOOR:
13010 CASE_CFN_LLRINT:
13011 CASE_CFN_LLROUND:
13012 CASE_CFN_LRINT:
13013 CASE_CFN_LROUND:
13014 CASE_CFN_MODF:
13015 CASE_CFN_NEARBYINT:
13016 CASE_CFN_NEARBYINT_FN:
13017 CASE_CFN_RINT:
13018 CASE_CFN_RINT_FN:
13019 CASE_CFN_ROUND:
13020 CASE_CFN_ROUND_FN:
13021 CASE_CFN_SCALB:
13022 CASE_CFN_SCALBLN:
13023 CASE_CFN_SCALBN:
13024 CASE_CFN_SIGNBIT:
13025 CASE_CFN_SIGNIFICAND:
13026 CASE_CFN_SINH:
13027 CASE_CFN_TANH:
13028 CASE_CFN_TRUNC:
13029 CASE_CFN_TRUNC_FN:
13030 /* True if the 1st argument is nonnegative. */
13031 return RECURSE (arg0);
13032
13033 CASE_CFN_FMAX:
13034 CASE_CFN_FMAX_FN:
13035 /* True if the 1st OR 2nd arguments are nonnegative. */
13036 return RECURSE (arg0) || RECURSE (arg1);
13037
13038 CASE_CFN_FMIN:
13039 CASE_CFN_FMIN_FN:
13040 /* True if the 1st AND 2nd arguments are nonnegative. */
13041 return RECURSE (arg0) && RECURSE (arg1);
13042
13043 CASE_CFN_COPYSIGN:
13044 CASE_CFN_COPYSIGN_FN:
13045 /* True if the 2nd argument is nonnegative. */
13046 return RECURSE (arg1);
13047
13048 CASE_CFN_POWI:
13049 /* True if the 1st argument is nonnegative or the second
13050 argument is an even integer. */
13051 if (TREE_CODE (arg1) == INTEGER_CST
13052 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13053 return true;
13054 return RECURSE (arg0);
13055
13056 CASE_CFN_POW:
13057 /* True if the 1st argument is nonnegative or the second
13058 argument is an even integer valued real. */
13059 if (TREE_CODE (arg1) == REAL_CST)
13060 {
13061 REAL_VALUE_TYPE c;
13062 HOST_WIDE_INT n;
13063
13064 c = TREE_REAL_CST (arg1);
13065 n = real_to_integer (&c);
13066 if ((n & 1) == 0)
13067 {
13068 REAL_VALUE_TYPE cint;
13069 real_from_integer (&cint, VOIDmode, n, SIGNED);
13070 if (real_identical (&c, &cint))
13071 return true;
13072 }
13073 }
13074 return RECURSE (arg0);
13075
13076 default:
13077 break;
13078 }
13079 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13080 }
13081
13082 /* Return true if T is known to be non-negative. If the return
13083 value is based on the assumption that signed overflow is undefined,
13084 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13085 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13086
13087 static bool
13088 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13089 {
13090 enum tree_code code = TREE_CODE (t);
13091 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13092 return true;
13093
13094 switch (code)
13095 {
13096 case TARGET_EXPR:
13097 {
13098 tree temp = TARGET_EXPR_SLOT (t);
13099 t = TARGET_EXPR_INITIAL (t);
13100
13101 /* If the initializer is non-void, then it's a normal expression
13102 that will be assigned to the slot. */
13103 if (!VOID_TYPE_P (t))
13104 return RECURSE (t);
13105
13106 /* Otherwise, the initializer sets the slot in some way. One common
13107 way is an assignment statement at the end of the initializer. */
13108 while (1)
13109 {
13110 if (TREE_CODE (t) == BIND_EXPR)
13111 t = expr_last (BIND_EXPR_BODY (t));
13112 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13113 || TREE_CODE (t) == TRY_CATCH_EXPR)
13114 t = expr_last (TREE_OPERAND (t, 0));
13115 else if (TREE_CODE (t) == STATEMENT_LIST)
13116 t = expr_last (t);
13117 else
13118 break;
13119 }
13120 if (TREE_CODE (t) == MODIFY_EXPR
13121 && TREE_OPERAND (t, 0) == temp)
13122 return RECURSE (TREE_OPERAND (t, 1));
13123
13124 return false;
13125 }
13126
13127 case CALL_EXPR:
13128 {
13129 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13130 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13131
13132 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13133 get_call_combined_fn (t),
13134 arg0,
13135 arg1,
13136 strict_overflow_p, depth);
13137 }
13138 case COMPOUND_EXPR:
13139 case MODIFY_EXPR:
13140 return RECURSE (TREE_OPERAND (t, 1));
13141
13142 case BIND_EXPR:
13143 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13144
13145 case SAVE_EXPR:
13146 return RECURSE (TREE_OPERAND (t, 0));
13147
13148 default:
13149 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13150 }
13151 }
13152
13153 #undef RECURSE
13154 #undef tree_expr_nonnegative_warnv_p
13155
13156 /* Return true if T is known to be non-negative. If the return
13157 value is based on the assumption that signed overflow is undefined,
13158 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13159 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13160
13161 bool
13162 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13163 {
13164 enum tree_code code;
13165 if (t == error_mark_node)
13166 return false;
13167
13168 code = TREE_CODE (t);
13169 switch (TREE_CODE_CLASS (code))
13170 {
13171 case tcc_binary:
13172 case tcc_comparison:
13173 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13174 TREE_TYPE (t),
13175 TREE_OPERAND (t, 0),
13176 TREE_OPERAND (t, 1),
13177 strict_overflow_p, depth);
13178
13179 case tcc_unary:
13180 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13181 TREE_TYPE (t),
13182 TREE_OPERAND (t, 0),
13183 strict_overflow_p, depth);
13184
13185 case tcc_constant:
13186 case tcc_declaration:
13187 case tcc_reference:
13188 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13189
13190 default:
13191 break;
13192 }
13193
13194 switch (code)
13195 {
13196 case TRUTH_AND_EXPR:
13197 case TRUTH_OR_EXPR:
13198 case TRUTH_XOR_EXPR:
13199 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13200 TREE_TYPE (t),
13201 TREE_OPERAND (t, 0),
13202 TREE_OPERAND (t, 1),
13203 strict_overflow_p, depth);
13204 case TRUTH_NOT_EXPR:
13205 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13206 TREE_TYPE (t),
13207 TREE_OPERAND (t, 0),
13208 strict_overflow_p, depth);
13209
13210 case COND_EXPR:
13211 case CONSTRUCTOR:
13212 case OBJ_TYPE_REF:
13213 case ASSERT_EXPR:
13214 case ADDR_EXPR:
13215 case WITH_SIZE_EXPR:
13216 case SSA_NAME:
13217 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13218
13219 default:
13220 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13221 }
13222 }
13223
13224 /* Return true if `t' is known to be non-negative. Handle warnings
13225 about undefined signed overflow. */
13226
13227 bool
13228 tree_expr_nonnegative_p (tree t)
13229 {
13230 bool ret, strict_overflow_p;
13231
13232 strict_overflow_p = false;
13233 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13234 if (strict_overflow_p)
13235 fold_overflow_warning (("assuming signed overflow does not occur when "
13236 "determining that expression is always "
13237 "non-negative"),
13238 WARN_STRICT_OVERFLOW_MISC);
13239 return ret;
13240 }
13241
13242
13243 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13244 For floating point we further ensure that T is not denormal.
13245 Similar logic is present in nonzero_address in rtlanal.h.
13246
13247 If the return value is based on the assumption that signed overflow
13248 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13249 change *STRICT_OVERFLOW_P. */
13250
13251 bool
13252 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13253 bool *strict_overflow_p)
13254 {
13255 switch (code)
13256 {
13257 case ABS_EXPR:
13258 return tree_expr_nonzero_warnv_p (op0,
13259 strict_overflow_p);
13260
13261 case NOP_EXPR:
13262 {
13263 tree inner_type = TREE_TYPE (op0);
13264 tree outer_type = type;
13265
13266 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13267 && tree_expr_nonzero_warnv_p (op0,
13268 strict_overflow_p));
13269 }
13270 break;
13271
13272 case NON_LVALUE_EXPR:
13273 return tree_expr_nonzero_warnv_p (op0,
13274 strict_overflow_p);
13275
13276 default:
13277 break;
13278 }
13279
13280 return false;
13281 }
13282
13283 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13284 For floating point we further ensure that T is not denormal.
13285 Similar logic is present in nonzero_address in rtlanal.h.
13286
13287 If the return value is based on the assumption that signed overflow
13288 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13289 change *STRICT_OVERFLOW_P. */
13290
13291 bool
13292 tree_binary_nonzero_warnv_p (enum tree_code code,
13293 tree type,
13294 tree op0,
13295 tree op1, bool *strict_overflow_p)
13296 {
13297 bool sub_strict_overflow_p;
13298 switch (code)
13299 {
13300 case POINTER_PLUS_EXPR:
13301 case PLUS_EXPR:
13302 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13303 {
13304 /* With the presence of negative values it is hard
13305 to say something. */
13306 sub_strict_overflow_p = false;
13307 if (!tree_expr_nonnegative_warnv_p (op0,
13308 &sub_strict_overflow_p)
13309 || !tree_expr_nonnegative_warnv_p (op1,
13310 &sub_strict_overflow_p))
13311 return false;
13312 /* One of operands must be positive and the other non-negative. */
13313 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13314 overflows, on a twos-complement machine the sum of two
13315 nonnegative numbers can never be zero. */
13316 return (tree_expr_nonzero_warnv_p (op0,
13317 strict_overflow_p)
13318 || tree_expr_nonzero_warnv_p (op1,
13319 strict_overflow_p));
13320 }
13321 break;
13322
13323 case MULT_EXPR:
13324 if (TYPE_OVERFLOW_UNDEFINED (type))
13325 {
13326 if (tree_expr_nonzero_warnv_p (op0,
13327 strict_overflow_p)
13328 && tree_expr_nonzero_warnv_p (op1,
13329 strict_overflow_p))
13330 {
13331 *strict_overflow_p = true;
13332 return true;
13333 }
13334 }
13335 break;
13336
13337 case MIN_EXPR:
13338 sub_strict_overflow_p = false;
13339 if (tree_expr_nonzero_warnv_p (op0,
13340 &sub_strict_overflow_p)
13341 && tree_expr_nonzero_warnv_p (op1,
13342 &sub_strict_overflow_p))
13343 {
13344 if (sub_strict_overflow_p)
13345 *strict_overflow_p = true;
13346 }
13347 break;
13348
13349 case MAX_EXPR:
13350 sub_strict_overflow_p = false;
13351 if (tree_expr_nonzero_warnv_p (op0,
13352 &sub_strict_overflow_p))
13353 {
13354 if (sub_strict_overflow_p)
13355 *strict_overflow_p = true;
13356
13357 /* When both operands are nonzero, then MAX must be too. */
13358 if (tree_expr_nonzero_warnv_p (op1,
13359 strict_overflow_p))
13360 return true;
13361
13362 /* MAX where operand 0 is positive is positive. */
13363 return tree_expr_nonnegative_warnv_p (op0,
13364 strict_overflow_p);
13365 }
13366 /* MAX where operand 1 is positive is positive. */
13367 else if (tree_expr_nonzero_warnv_p (op1,
13368 &sub_strict_overflow_p)
13369 && tree_expr_nonnegative_warnv_p (op1,
13370 &sub_strict_overflow_p))
13371 {
13372 if (sub_strict_overflow_p)
13373 *strict_overflow_p = true;
13374 return true;
13375 }
13376 break;
13377
13378 case BIT_IOR_EXPR:
13379 return (tree_expr_nonzero_warnv_p (op1,
13380 strict_overflow_p)
13381 || tree_expr_nonzero_warnv_p (op0,
13382 strict_overflow_p));
13383
13384 default:
13385 break;
13386 }
13387
13388 return false;
13389 }
13390
13391 /* Return true when T is an address and is known to be nonzero.
13392 For floating point we further ensure that T is not denormal.
13393 Similar logic is present in nonzero_address in rtlanal.h.
13394
13395 If the return value is based on the assumption that signed overflow
13396 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13397 change *STRICT_OVERFLOW_P. */
13398
13399 bool
13400 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13401 {
13402 bool sub_strict_overflow_p;
13403 switch (TREE_CODE (t))
13404 {
13405 case INTEGER_CST:
13406 return !integer_zerop (t);
13407
13408 case ADDR_EXPR:
13409 {
13410 tree base = TREE_OPERAND (t, 0);
13411
13412 if (!DECL_P (base))
13413 base = get_base_address (base);
13414
13415 if (base && TREE_CODE (base) == TARGET_EXPR)
13416 base = TARGET_EXPR_SLOT (base);
13417
13418 if (!base)
13419 return false;
13420
13421 /* For objects in symbol table check if we know they are non-zero.
13422 Don't do anything for variables and functions before symtab is built;
13423 it is quite possible that they will be declared weak later. */
13424 int nonzero_addr = maybe_nonzero_address (base);
13425 if (nonzero_addr >= 0)
13426 return nonzero_addr;
13427
13428 /* Constants are never weak. */
13429 if (CONSTANT_CLASS_P (base))
13430 return true;
13431
13432 return false;
13433 }
13434
13435 case COND_EXPR:
13436 sub_strict_overflow_p = false;
13437 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13438 &sub_strict_overflow_p)
13439 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13440 &sub_strict_overflow_p))
13441 {
13442 if (sub_strict_overflow_p)
13443 *strict_overflow_p = true;
13444 return true;
13445 }
13446 break;
13447
13448 case SSA_NAME:
13449 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13450 break;
13451 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13452
13453 default:
13454 break;
13455 }
13456 return false;
13457 }
13458
13459 #define integer_valued_real_p(X) \
13460 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13461
13462 #define RECURSE(X) \
13463 ((integer_valued_real_p) (X, depth + 1))
13464
13465 /* Return true if the floating point result of (CODE OP0) has an
13466 integer value. We also allow +Inf, -Inf and NaN to be considered
13467 integer values. Return false for signaling NaN.
13468
13469 DEPTH is the current nesting depth of the query. */
13470
13471 bool
13472 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13473 {
13474 switch (code)
13475 {
13476 case FLOAT_EXPR:
13477 return true;
13478
13479 case ABS_EXPR:
13480 return RECURSE (op0);
13481
13482 CASE_CONVERT:
13483 {
13484 tree type = TREE_TYPE (op0);
13485 if (TREE_CODE (type) == INTEGER_TYPE)
13486 return true;
13487 if (TREE_CODE (type) == REAL_TYPE)
13488 return RECURSE (op0);
13489 break;
13490 }
13491
13492 default:
13493 break;
13494 }
13495 return false;
13496 }
13497
13498 /* Return true if the floating point result of (CODE OP0 OP1) has an
13499 integer value. We also allow +Inf, -Inf and NaN to be considered
13500 integer values. Return false for signaling NaN.
13501
13502 DEPTH is the current nesting depth of the query. */
13503
13504 bool
13505 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13506 {
13507 switch (code)
13508 {
13509 case PLUS_EXPR:
13510 case MINUS_EXPR:
13511 case MULT_EXPR:
13512 case MIN_EXPR:
13513 case MAX_EXPR:
13514 return RECURSE (op0) && RECURSE (op1);
13515
13516 default:
13517 break;
13518 }
13519 return false;
13520 }
13521
13522 /* Return true if the floating point result of calling FNDECL with arguments
13523 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13524 considered integer values. Return false for signaling NaN. If FNDECL
13525 takes fewer than 2 arguments, the remaining ARGn are null.
13526
13527 DEPTH is the current nesting depth of the query. */
13528
13529 bool
13530 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13531 {
13532 switch (fn)
13533 {
13534 CASE_CFN_CEIL:
13535 CASE_CFN_CEIL_FN:
13536 CASE_CFN_FLOOR:
13537 CASE_CFN_FLOOR_FN:
13538 CASE_CFN_NEARBYINT:
13539 CASE_CFN_NEARBYINT_FN:
13540 CASE_CFN_RINT:
13541 CASE_CFN_RINT_FN:
13542 CASE_CFN_ROUND:
13543 CASE_CFN_ROUND_FN:
13544 CASE_CFN_TRUNC:
13545 CASE_CFN_TRUNC_FN:
13546 return true;
13547
13548 CASE_CFN_FMIN:
13549 CASE_CFN_FMIN_FN:
13550 CASE_CFN_FMAX:
13551 CASE_CFN_FMAX_FN:
13552 return RECURSE (arg0) && RECURSE (arg1);
13553
13554 default:
13555 break;
13556 }
13557 return false;
13558 }
13559
13560 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13561 has an integer value. We also allow +Inf, -Inf and NaN to be
13562 considered integer values. Return false for signaling NaN.
13563
13564 DEPTH is the current nesting depth of the query. */
13565
13566 bool
13567 integer_valued_real_single_p (tree t, int depth)
13568 {
13569 switch (TREE_CODE (t))
13570 {
13571 case REAL_CST:
13572 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13573
13574 case COND_EXPR:
13575 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13576
13577 case SSA_NAME:
13578 /* Limit the depth of recursion to avoid quadratic behavior.
13579 This is expected to catch almost all occurrences in practice.
13580 If this code misses important cases that unbounded recursion
13581 would not, passes that need this information could be revised
13582 to provide it through dataflow propagation. */
13583 return (!name_registered_for_update_p (t)
13584 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13585 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13586 depth));
13587
13588 default:
13589 break;
13590 }
13591 return false;
13592 }
13593
13594 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13595 has an integer value. We also allow +Inf, -Inf and NaN to be
13596 considered integer values. Return false for signaling NaN.
13597
13598 DEPTH is the current nesting depth of the query. */
13599
13600 static bool
13601 integer_valued_real_invalid_p (tree t, int depth)
13602 {
13603 switch (TREE_CODE (t))
13604 {
13605 case COMPOUND_EXPR:
13606 case MODIFY_EXPR:
13607 case BIND_EXPR:
13608 return RECURSE (TREE_OPERAND (t, 1));
13609
13610 case SAVE_EXPR:
13611 return RECURSE (TREE_OPERAND (t, 0));
13612
13613 default:
13614 break;
13615 }
13616 return false;
13617 }
13618
13619 #undef RECURSE
13620 #undef integer_valued_real_p
13621
13622 /* Return true if the floating point expression T has an integer value.
13623 We also allow +Inf, -Inf and NaN to be considered integer values.
13624 Return false for signaling NaN.
13625
13626 DEPTH is the current nesting depth of the query. */
13627
13628 bool
13629 integer_valued_real_p (tree t, int depth)
13630 {
13631 if (t == error_mark_node)
13632 return false;
13633
13634 tree_code code = TREE_CODE (t);
13635 switch (TREE_CODE_CLASS (code))
13636 {
13637 case tcc_binary:
13638 case tcc_comparison:
13639 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13640 TREE_OPERAND (t, 1), depth);
13641
13642 case tcc_unary:
13643 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13644
13645 case tcc_constant:
13646 case tcc_declaration:
13647 case tcc_reference:
13648 return integer_valued_real_single_p (t, depth);
13649
13650 default:
13651 break;
13652 }
13653
13654 switch (code)
13655 {
13656 case COND_EXPR:
13657 case SSA_NAME:
13658 return integer_valued_real_single_p (t, depth);
13659
13660 case CALL_EXPR:
13661 {
13662 tree arg0 = (call_expr_nargs (t) > 0
13663 ? CALL_EXPR_ARG (t, 0)
13664 : NULL_TREE);
13665 tree arg1 = (call_expr_nargs (t) > 1
13666 ? CALL_EXPR_ARG (t, 1)
13667 : NULL_TREE);
13668 return integer_valued_real_call_p (get_call_combined_fn (t),
13669 arg0, arg1, depth);
13670 }
13671
13672 default:
13673 return integer_valued_real_invalid_p (t, depth);
13674 }
13675 }
13676
13677 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13678 attempt to fold the expression to a constant without modifying TYPE,
13679 OP0 or OP1.
13680
13681 If the expression could be simplified to a constant, then return
13682 the constant. If the expression would not be simplified to a
13683 constant, then return NULL_TREE. */
13684
13685 tree
13686 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13687 {
13688 tree tem = fold_binary (code, type, op0, op1);
13689 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13690 }
13691
13692 /* Given the components of a unary expression CODE, TYPE and OP0,
13693 attempt to fold the expression to a constant without modifying
13694 TYPE or OP0.
13695
13696 If the expression could be simplified to a constant, then return
13697 the constant. If the expression would not be simplified to a
13698 constant, then return NULL_TREE. */
13699
13700 tree
13701 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13702 {
13703 tree tem = fold_unary (code, type, op0);
13704 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13705 }
13706
13707 /* If EXP represents referencing an element in a constant string
13708 (either via pointer arithmetic or array indexing), return the
13709 tree representing the value accessed, otherwise return NULL. */
13710
13711 tree
13712 fold_read_from_constant_string (tree exp)
13713 {
13714 if ((TREE_CODE (exp) == INDIRECT_REF
13715 || TREE_CODE (exp) == ARRAY_REF)
13716 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13717 {
13718 tree exp1 = TREE_OPERAND (exp, 0);
13719 tree index;
13720 tree string;
13721 location_t loc = EXPR_LOCATION (exp);
13722
13723 if (TREE_CODE (exp) == INDIRECT_REF)
13724 string = string_constant (exp1, &index);
13725 else
13726 {
13727 tree low_bound = array_ref_low_bound (exp);
13728 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13729
13730 /* Optimize the special-case of a zero lower bound.
13731
13732 We convert the low_bound to sizetype to avoid some problems
13733 with constant folding. (E.g. suppose the lower bound is 1,
13734 and its mode is QI. Without the conversion,l (ARRAY
13735 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13736 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13737 if (! integer_zerop (low_bound))
13738 index = size_diffop_loc (loc, index,
13739 fold_convert_loc (loc, sizetype, low_bound));
13740
13741 string = exp1;
13742 }
13743
13744 scalar_int_mode char_mode;
13745 if (string
13746 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13747 && TREE_CODE (string) == STRING_CST
13748 && TREE_CODE (index) == INTEGER_CST
13749 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13750 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13751 &char_mode)
13752 && GET_MODE_SIZE (char_mode) == 1)
13753 return build_int_cst_type (TREE_TYPE (exp),
13754 (TREE_STRING_POINTER (string)
13755 [TREE_INT_CST_LOW (index)]));
13756 }
13757 return NULL;
13758 }
13759
13760 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13761 an integer constant, real, or fixed-point constant.
13762
13763 TYPE is the type of the result. */
13764
13765 static tree
13766 fold_negate_const (tree arg0, tree type)
13767 {
13768 tree t = NULL_TREE;
13769
13770 switch (TREE_CODE (arg0))
13771 {
13772 case REAL_CST:
13773 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13774 break;
13775
13776 case FIXED_CST:
13777 {
13778 FIXED_VALUE_TYPE f;
13779 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13780 &(TREE_FIXED_CST (arg0)), NULL,
13781 TYPE_SATURATING (type));
13782 t = build_fixed (type, f);
13783 /* Propagate overflow flags. */
13784 if (overflow_p | TREE_OVERFLOW (arg0))
13785 TREE_OVERFLOW (t) = 1;
13786 break;
13787 }
13788
13789 default:
13790 if (poly_int_tree_p (arg0))
13791 {
13792 bool overflow;
13793 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13794 t = force_fit_type (type, res, 1,
13795 (overflow && ! TYPE_UNSIGNED (type))
13796 || TREE_OVERFLOW (arg0));
13797 break;
13798 }
13799
13800 gcc_unreachable ();
13801 }
13802
13803 return t;
13804 }
13805
13806 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13807 an integer constant or real constant.
13808
13809 TYPE is the type of the result. */
13810
13811 tree
13812 fold_abs_const (tree arg0, tree type)
13813 {
13814 tree t = NULL_TREE;
13815
13816 switch (TREE_CODE (arg0))
13817 {
13818 case INTEGER_CST:
13819 {
13820 /* If the value is unsigned or non-negative, then the absolute value
13821 is the same as the ordinary value. */
13822 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13823 t = arg0;
13824
13825 /* If the value is negative, then the absolute value is
13826 its negation. */
13827 else
13828 {
13829 bool overflow;
13830 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13831 t = force_fit_type (type, val, -1,
13832 overflow | TREE_OVERFLOW (arg0));
13833 }
13834 }
13835 break;
13836
13837 case REAL_CST:
13838 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13839 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13840 else
13841 t = arg0;
13842 break;
13843
13844 default:
13845 gcc_unreachable ();
13846 }
13847
13848 return t;
13849 }
13850
13851 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13852 constant. TYPE is the type of the result. */
13853
13854 static tree
13855 fold_not_const (const_tree arg0, tree type)
13856 {
13857 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13858
13859 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13860 }
13861
13862 /* Given CODE, a relational operator, the target type, TYPE and two
13863 constant operands OP0 and OP1, return the result of the
13864 relational operation. If the result is not a compile time
13865 constant, then return NULL_TREE. */
13866
13867 static tree
13868 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13869 {
13870 int result, invert;
13871
13872 /* From here on, the only cases we handle are when the result is
13873 known to be a constant. */
13874
13875 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13876 {
13877 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13878 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13879
13880 /* Handle the cases where either operand is a NaN. */
13881 if (real_isnan (c0) || real_isnan (c1))
13882 {
13883 switch (code)
13884 {
13885 case EQ_EXPR:
13886 case ORDERED_EXPR:
13887 result = 0;
13888 break;
13889
13890 case NE_EXPR:
13891 case UNORDERED_EXPR:
13892 case UNLT_EXPR:
13893 case UNLE_EXPR:
13894 case UNGT_EXPR:
13895 case UNGE_EXPR:
13896 case UNEQ_EXPR:
13897 result = 1;
13898 break;
13899
13900 case LT_EXPR:
13901 case LE_EXPR:
13902 case GT_EXPR:
13903 case GE_EXPR:
13904 case LTGT_EXPR:
13905 if (flag_trapping_math)
13906 return NULL_TREE;
13907 result = 0;
13908 break;
13909
13910 default:
13911 gcc_unreachable ();
13912 }
13913
13914 return constant_boolean_node (result, type);
13915 }
13916
13917 return constant_boolean_node (real_compare (code, c0, c1), type);
13918 }
13919
13920 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13921 {
13922 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13923 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13924 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13925 }
13926
13927 /* Handle equality/inequality of complex constants. */
13928 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13929 {
13930 tree rcond = fold_relational_const (code, type,
13931 TREE_REALPART (op0),
13932 TREE_REALPART (op1));
13933 tree icond = fold_relational_const (code, type,
13934 TREE_IMAGPART (op0),
13935 TREE_IMAGPART (op1));
13936 if (code == EQ_EXPR)
13937 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13938 else if (code == NE_EXPR)
13939 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13940 else
13941 return NULL_TREE;
13942 }
13943
13944 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13945 {
13946 if (!VECTOR_TYPE_P (type))
13947 {
13948 /* Have vector comparison with scalar boolean result. */
13949 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13950 && known_eq (VECTOR_CST_NELTS (op0),
13951 VECTOR_CST_NELTS (op1)));
13952 unsigned HOST_WIDE_INT nunits;
13953 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
13954 return NULL_TREE;
13955 for (unsigned i = 0; i < nunits; i++)
13956 {
13957 tree elem0 = VECTOR_CST_ELT (op0, i);
13958 tree elem1 = VECTOR_CST_ELT (op1, i);
13959 tree tmp = fold_relational_const (code, type, elem0, elem1);
13960 if (tmp == NULL_TREE)
13961 return NULL_TREE;
13962 if (integer_zerop (tmp))
13963 return constant_boolean_node (false, type);
13964 }
13965 return constant_boolean_node (true, type);
13966 }
13967 tree_vector_builder elts;
13968 if (!elts.new_binary_operation (type, op0, op1, false))
13969 return NULL_TREE;
13970 unsigned int count = elts.encoded_nelts ();
13971 for (unsigned i = 0; i < count; i++)
13972 {
13973 tree elem_type = TREE_TYPE (type);
13974 tree elem0 = VECTOR_CST_ELT (op0, i);
13975 tree elem1 = VECTOR_CST_ELT (op1, i);
13976
13977 tree tem = fold_relational_const (code, elem_type,
13978 elem0, elem1);
13979
13980 if (tem == NULL_TREE)
13981 return NULL_TREE;
13982
13983 elts.quick_push (build_int_cst (elem_type,
13984 integer_zerop (tem) ? 0 : -1));
13985 }
13986
13987 return elts.build ();
13988 }
13989
13990 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13991
13992 To compute GT, swap the arguments and do LT.
13993 To compute GE, do LT and invert the result.
13994 To compute LE, swap the arguments, do LT and invert the result.
13995 To compute NE, do EQ and invert the result.
13996
13997 Therefore, the code below must handle only EQ and LT. */
13998
13999 if (code == LE_EXPR || code == GT_EXPR)
14000 {
14001 std::swap (op0, op1);
14002 code = swap_tree_comparison (code);
14003 }
14004
14005 /* Note that it is safe to invert for real values here because we
14006 have already handled the one case that it matters. */
14007
14008 invert = 0;
14009 if (code == NE_EXPR || code == GE_EXPR)
14010 {
14011 invert = 1;
14012 code = invert_tree_comparison (code, false);
14013 }
14014
14015 /* Compute a result for LT or EQ if args permit;
14016 Otherwise return T. */
14017 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14018 {
14019 if (code == EQ_EXPR)
14020 result = tree_int_cst_equal (op0, op1);
14021 else
14022 result = tree_int_cst_lt (op0, op1);
14023 }
14024 else
14025 return NULL_TREE;
14026
14027 if (invert)
14028 result ^= 1;
14029 return constant_boolean_node (result, type);
14030 }
14031
14032 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14033 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14034 itself. */
14035
14036 tree
14037 fold_build_cleanup_point_expr (tree type, tree expr)
14038 {
14039 /* If the expression does not have side effects then we don't have to wrap
14040 it with a cleanup point expression. */
14041 if (!TREE_SIDE_EFFECTS (expr))
14042 return expr;
14043
14044 /* If the expression is a return, check to see if the expression inside the
14045 return has no side effects or the right hand side of the modify expression
14046 inside the return. If either don't have side effects set we don't need to
14047 wrap the expression in a cleanup point expression. Note we don't check the
14048 left hand side of the modify because it should always be a return decl. */
14049 if (TREE_CODE (expr) == RETURN_EXPR)
14050 {
14051 tree op = TREE_OPERAND (expr, 0);
14052 if (!op || !TREE_SIDE_EFFECTS (op))
14053 return expr;
14054 op = TREE_OPERAND (op, 1);
14055 if (!TREE_SIDE_EFFECTS (op))
14056 return expr;
14057 }
14058
14059 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14060 }
14061
14062 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14063 of an indirection through OP0, or NULL_TREE if no simplification is
14064 possible. */
14065
14066 tree
14067 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14068 {
14069 tree sub = op0;
14070 tree subtype;
14071 poly_uint64 const_op01;
14072
14073 STRIP_NOPS (sub);
14074 subtype = TREE_TYPE (sub);
14075 if (!POINTER_TYPE_P (subtype)
14076 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14077 return NULL_TREE;
14078
14079 if (TREE_CODE (sub) == ADDR_EXPR)
14080 {
14081 tree op = TREE_OPERAND (sub, 0);
14082 tree optype = TREE_TYPE (op);
14083 /* *&CONST_DECL -> to the value of the const decl. */
14084 if (TREE_CODE (op) == CONST_DECL)
14085 return DECL_INITIAL (op);
14086 /* *&p => p; make sure to handle *&"str"[cst] here. */
14087 if (type == optype)
14088 {
14089 tree fop = fold_read_from_constant_string (op);
14090 if (fop)
14091 return fop;
14092 else
14093 return op;
14094 }
14095 /* *(foo *)&fooarray => fooarray[0] */
14096 else if (TREE_CODE (optype) == ARRAY_TYPE
14097 && type == TREE_TYPE (optype)
14098 && (!in_gimple_form
14099 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14100 {
14101 tree type_domain = TYPE_DOMAIN (optype);
14102 tree min_val = size_zero_node;
14103 if (type_domain && TYPE_MIN_VALUE (type_domain))
14104 min_val = TYPE_MIN_VALUE (type_domain);
14105 if (in_gimple_form
14106 && TREE_CODE (min_val) != INTEGER_CST)
14107 return NULL_TREE;
14108 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14109 NULL_TREE, NULL_TREE);
14110 }
14111 /* *(foo *)&complexfoo => __real__ complexfoo */
14112 else if (TREE_CODE (optype) == COMPLEX_TYPE
14113 && type == TREE_TYPE (optype))
14114 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14115 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14116 else if (TREE_CODE (optype) == VECTOR_TYPE
14117 && type == TREE_TYPE (optype))
14118 {
14119 tree part_width = TYPE_SIZE (type);
14120 tree index = bitsize_int (0);
14121 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14122 }
14123 }
14124
14125 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14126 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14127 {
14128 tree op00 = TREE_OPERAND (sub, 0);
14129 tree op01 = TREE_OPERAND (sub, 1);
14130
14131 STRIP_NOPS (op00);
14132 if (TREE_CODE (op00) == ADDR_EXPR)
14133 {
14134 tree op00type;
14135 op00 = TREE_OPERAND (op00, 0);
14136 op00type = TREE_TYPE (op00);
14137
14138 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14139 if (TREE_CODE (op00type) == VECTOR_TYPE
14140 && type == TREE_TYPE (op00type))
14141 {
14142 tree part_width = TYPE_SIZE (type);
14143 poly_uint64 max_offset
14144 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14145 * TYPE_VECTOR_SUBPARTS (op00type));
14146 if (known_lt (const_op01, max_offset))
14147 {
14148 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14149 return fold_build3_loc (loc,
14150 BIT_FIELD_REF, type, op00,
14151 part_width, index);
14152 }
14153 }
14154 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14155 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14156 && type == TREE_TYPE (op00type))
14157 {
14158 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14159 const_op01))
14160 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14161 }
14162 /* ((foo *)&fooarray)[1] => fooarray[1] */
14163 else if (TREE_CODE (op00type) == ARRAY_TYPE
14164 && type == TREE_TYPE (op00type))
14165 {
14166 tree type_domain = TYPE_DOMAIN (op00type);
14167 tree min = size_zero_node;
14168 if (type_domain && TYPE_MIN_VALUE (type_domain))
14169 min = TYPE_MIN_VALUE (type_domain);
14170 offset_int off = wi::to_offset (op01);
14171 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14172 offset_int remainder;
14173 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14174 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
14175 {
14176 off = off + wi::to_offset (min);
14177 op01 = wide_int_to_tree (sizetype, off);
14178 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14179 NULL_TREE, NULL_TREE);
14180 }
14181 }
14182 }
14183 }
14184
14185 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14186 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14187 && type == TREE_TYPE (TREE_TYPE (subtype))
14188 && (!in_gimple_form
14189 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14190 {
14191 tree type_domain;
14192 tree min_val = size_zero_node;
14193 sub = build_fold_indirect_ref_loc (loc, sub);
14194 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14195 if (type_domain && TYPE_MIN_VALUE (type_domain))
14196 min_val = TYPE_MIN_VALUE (type_domain);
14197 if (in_gimple_form
14198 && TREE_CODE (min_val) != INTEGER_CST)
14199 return NULL_TREE;
14200 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14201 NULL_TREE);
14202 }
14203
14204 return NULL_TREE;
14205 }
14206
14207 /* Builds an expression for an indirection through T, simplifying some
14208 cases. */
14209
14210 tree
14211 build_fold_indirect_ref_loc (location_t loc, tree t)
14212 {
14213 tree type = TREE_TYPE (TREE_TYPE (t));
14214 tree sub = fold_indirect_ref_1 (loc, type, t);
14215
14216 if (sub)
14217 return sub;
14218
14219 return build1_loc (loc, INDIRECT_REF, type, t);
14220 }
14221
14222 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14223
14224 tree
14225 fold_indirect_ref_loc (location_t loc, tree t)
14226 {
14227 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14228
14229 if (sub)
14230 return sub;
14231 else
14232 return t;
14233 }
14234
14235 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14236 whose result is ignored. The type of the returned tree need not be
14237 the same as the original expression. */
14238
14239 tree
14240 fold_ignored_result (tree t)
14241 {
14242 if (!TREE_SIDE_EFFECTS (t))
14243 return integer_zero_node;
14244
14245 for (;;)
14246 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14247 {
14248 case tcc_unary:
14249 t = TREE_OPERAND (t, 0);
14250 break;
14251
14252 case tcc_binary:
14253 case tcc_comparison:
14254 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14255 t = TREE_OPERAND (t, 0);
14256 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14257 t = TREE_OPERAND (t, 1);
14258 else
14259 return t;
14260 break;
14261
14262 case tcc_expression:
14263 switch (TREE_CODE (t))
14264 {
14265 case COMPOUND_EXPR:
14266 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14267 return t;
14268 t = TREE_OPERAND (t, 0);
14269 break;
14270
14271 case COND_EXPR:
14272 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14273 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14274 return t;
14275 t = TREE_OPERAND (t, 0);
14276 break;
14277
14278 default:
14279 return t;
14280 }
14281 break;
14282
14283 default:
14284 return t;
14285 }
14286 }
14287
14288 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14289
14290 tree
14291 round_up_loc (location_t loc, tree value, unsigned int divisor)
14292 {
14293 tree div = NULL_TREE;
14294
14295 if (divisor == 1)
14296 return value;
14297
14298 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14299 have to do anything. Only do this when we are not given a const,
14300 because in that case, this check is more expensive than just
14301 doing it. */
14302 if (TREE_CODE (value) != INTEGER_CST)
14303 {
14304 div = build_int_cst (TREE_TYPE (value), divisor);
14305
14306 if (multiple_of_p (TREE_TYPE (value), value, div))
14307 return value;
14308 }
14309
14310 /* If divisor is a power of two, simplify this to bit manipulation. */
14311 if (pow2_or_zerop (divisor))
14312 {
14313 if (TREE_CODE (value) == INTEGER_CST)
14314 {
14315 wide_int val = wi::to_wide (value);
14316 bool overflow_p;
14317
14318 if ((val & (divisor - 1)) == 0)
14319 return value;
14320
14321 overflow_p = TREE_OVERFLOW (value);
14322 val += divisor - 1;
14323 val &= (int) -divisor;
14324 if (val == 0)
14325 overflow_p = true;
14326
14327 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14328 }
14329 else
14330 {
14331 tree t;
14332
14333 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14334 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14335 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14336 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14337 }
14338 }
14339 else
14340 {
14341 if (!div)
14342 div = build_int_cst (TREE_TYPE (value), divisor);
14343 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14344 value = size_binop_loc (loc, MULT_EXPR, value, div);
14345 }
14346
14347 return value;
14348 }
14349
14350 /* Likewise, but round down. */
14351
14352 tree
14353 round_down_loc (location_t loc, tree value, int divisor)
14354 {
14355 tree div = NULL_TREE;
14356
14357 gcc_assert (divisor > 0);
14358 if (divisor == 1)
14359 return value;
14360
14361 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14362 have to do anything. Only do this when we are not given a const,
14363 because in that case, this check is more expensive than just
14364 doing it. */
14365 if (TREE_CODE (value) != INTEGER_CST)
14366 {
14367 div = build_int_cst (TREE_TYPE (value), divisor);
14368
14369 if (multiple_of_p (TREE_TYPE (value), value, div))
14370 return value;
14371 }
14372
14373 /* If divisor is a power of two, simplify this to bit manipulation. */
14374 if (pow2_or_zerop (divisor))
14375 {
14376 tree t;
14377
14378 t = build_int_cst (TREE_TYPE (value), -divisor);
14379 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14380 }
14381 else
14382 {
14383 if (!div)
14384 div = build_int_cst (TREE_TYPE (value), divisor);
14385 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14386 value = size_binop_loc (loc, MULT_EXPR, value, div);
14387 }
14388
14389 return value;
14390 }
14391
14392 /* Returns the pointer to the base of the object addressed by EXP and
14393 extracts the information about the offset of the access, storing it
14394 to PBITPOS and POFFSET. */
14395
14396 static tree
14397 split_address_to_core_and_offset (tree exp,
14398 poly_int64_pod *pbitpos, tree *poffset)
14399 {
14400 tree core;
14401 machine_mode mode;
14402 int unsignedp, reversep, volatilep;
14403 poly_int64 bitsize;
14404 location_t loc = EXPR_LOCATION (exp);
14405
14406 if (TREE_CODE (exp) == ADDR_EXPR)
14407 {
14408 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14409 poffset, &mode, &unsignedp, &reversep,
14410 &volatilep);
14411 core = build_fold_addr_expr_loc (loc, core);
14412 }
14413 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14414 {
14415 core = TREE_OPERAND (exp, 0);
14416 STRIP_NOPS (core);
14417 *pbitpos = 0;
14418 *poffset = TREE_OPERAND (exp, 1);
14419 if (poly_int_tree_p (*poffset))
14420 {
14421 poly_offset_int tem
14422 = wi::sext (wi::to_poly_offset (*poffset),
14423 TYPE_PRECISION (TREE_TYPE (*poffset)));
14424 tem <<= LOG2_BITS_PER_UNIT;
14425 if (tem.to_shwi (pbitpos))
14426 *poffset = NULL_TREE;
14427 }
14428 }
14429 else
14430 {
14431 core = exp;
14432 *pbitpos = 0;
14433 *poffset = NULL_TREE;
14434 }
14435
14436 return core;
14437 }
14438
14439 /* Returns true if addresses of E1 and E2 differ by a constant, false
14440 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14441
14442 bool
14443 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14444 {
14445 tree core1, core2;
14446 poly_int64 bitpos1, bitpos2;
14447 tree toffset1, toffset2, tdiff, type;
14448
14449 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14450 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14451
14452 poly_int64 bytepos1, bytepos2;
14453 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14454 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14455 || !operand_equal_p (core1, core2, 0))
14456 return false;
14457
14458 if (toffset1 && toffset2)
14459 {
14460 type = TREE_TYPE (toffset1);
14461 if (type != TREE_TYPE (toffset2))
14462 toffset2 = fold_convert (type, toffset2);
14463
14464 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14465 if (!cst_and_fits_in_hwi (tdiff))
14466 return false;
14467
14468 *diff = int_cst_value (tdiff);
14469 }
14470 else if (toffset1 || toffset2)
14471 {
14472 /* If only one of the offsets is non-constant, the difference cannot
14473 be a constant. */
14474 return false;
14475 }
14476 else
14477 *diff = 0;
14478
14479 *diff += bytepos1 - bytepos2;
14480 return true;
14481 }
14482
14483 /* Return OFF converted to a pointer offset type suitable as offset for
14484 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14485 tree
14486 convert_to_ptrofftype_loc (location_t loc, tree off)
14487 {
14488 return fold_convert_loc (loc, sizetype, off);
14489 }
14490
14491 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14492 tree
14493 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14494 {
14495 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14496 ptr, convert_to_ptrofftype_loc (loc, off));
14497 }
14498
14499 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14500 tree
14501 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14502 {
14503 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14504 ptr, size_int (off));
14505 }
14506
14507 /* Return a char pointer for a C string if it is a string constant
14508 or sum of string constant and integer constant. We only support
14509 string constants properly terminated with '\0' character.
14510 If STRLEN is a valid pointer, length (including terminating character)
14511 of returned string is stored to the argument. */
14512
14513 const char *
14514 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14515 {
14516 tree offset_node;
14517
14518 if (strlen)
14519 *strlen = 0;
14520
14521 src = string_constant (src, &offset_node);
14522 if (src == 0)
14523 return NULL;
14524
14525 unsigned HOST_WIDE_INT offset = 0;
14526 if (offset_node != NULL_TREE)
14527 {
14528 if (!tree_fits_uhwi_p (offset_node))
14529 return NULL;
14530 else
14531 offset = tree_to_uhwi (offset_node);
14532 }
14533
14534 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14535 const char *string = TREE_STRING_POINTER (src);
14536
14537 /* Support only properly null-terminated strings. */
14538 if (string_length == 0
14539 || string[string_length - 1] != '\0'
14540 || offset >= string_length)
14541 return NULL;
14542
14543 if (strlen)
14544 *strlen = string_length - offset;
14545 return string + offset;
14546 }
14547
14548 #if CHECKING_P
14549
14550 namespace selftest {
14551
14552 /* Helper functions for writing tests of folding trees. */
14553
14554 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14555
14556 static void
14557 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14558 tree constant)
14559 {
14560 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14561 }
14562
14563 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14564 wrapping WRAPPED_EXPR. */
14565
14566 static void
14567 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14568 tree wrapped_expr)
14569 {
14570 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14571 ASSERT_NE (wrapped_expr, result);
14572 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14573 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14574 }
14575
14576 /* Verify that various arithmetic binary operations are folded
14577 correctly. */
14578
14579 static void
14580 test_arithmetic_folding ()
14581 {
14582 tree type = integer_type_node;
14583 tree x = create_tmp_var_raw (type, "x");
14584 tree zero = build_zero_cst (type);
14585 tree one = build_int_cst (type, 1);
14586
14587 /* Addition. */
14588 /* 1 <-- (0 + 1) */
14589 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14590 one);
14591 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14592 one);
14593
14594 /* (nonlvalue)x <-- (x + 0) */
14595 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14596 x);
14597
14598 /* Subtraction. */
14599 /* 0 <-- (x - x) */
14600 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14601 zero);
14602 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14603 x);
14604
14605 /* Multiplication. */
14606 /* 0 <-- (x * 0) */
14607 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14608 zero);
14609
14610 /* (nonlvalue)x <-- (x * 1) */
14611 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14612 x);
14613 }
14614
14615 /* Verify that various binary operations on vectors are folded
14616 correctly. */
14617
14618 static void
14619 test_vector_folding ()
14620 {
14621 tree inner_type = integer_type_node;
14622 tree type = build_vector_type (inner_type, 4);
14623 tree zero = build_zero_cst (type);
14624 tree one = build_one_cst (type);
14625
14626 /* Verify equality tests that return a scalar boolean result. */
14627 tree res_type = boolean_type_node;
14628 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14629 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14630 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14631 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14632 }
14633
14634 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14635
14636 static void
14637 test_vec_duplicate_folding ()
14638 {
14639 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14640 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14641 /* This will be 1 if VEC_MODE isn't a vector mode. */
14642 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14643
14644 tree type = build_vector_type (ssizetype, nunits);
14645 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14646 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14647 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14648 }
14649
14650 /* Run all of the selftests within this file. */
14651
14652 void
14653 fold_const_c_tests ()
14654 {
14655 test_arithmetic_folding ();
14656 test_vector_folding ();
14657 test_vec_duplicate_folding ();
14658 }
14659
14660 } // namespace selftest
14661
14662 #endif /* CHECKING_P */