]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fold-const.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[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 *);
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 /* In general we can't negate A in A / B, because if A is INT_MIN and
478 B is not 1 we change the sign of the result. */
479 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
480 && negate_expr_p (TREE_OPERAND (t, 0)))
481 return true;
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
487 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t, 1))))
489 return negate_expr_p (TREE_OPERAND (t, 1));
490 break;
491
492 case NOP_EXPR:
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type) == REAL_TYPE)
495 {
496 tree tem = strip_float_extensions (t);
497 if (tem != t)
498 return negate_expr_p (tem);
499 }
500 break;
501
502 case CALL_EXPR:
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t)))
505 return negate_expr_p (CALL_EXPR_ARG (t, 0));
506 break;
507
508 case RSHIFT_EXPR:
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
511 {
512 tree op1 = TREE_OPERAND (t, 1);
513 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
514 return true;
515 }
516 break;
517
518 default:
519 break;
520 }
521 return false;
522 }
523
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
528
529 static tree
530 fold_negate_expr_1 (location_t loc, tree t)
531 {
532 tree type = TREE_TYPE (t);
533 tree tem;
534
535 switch (TREE_CODE (t))
536 {
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_one_cst (type));
542 break;
543
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || (ANY_INTEGRAL_TYPE_P (type)
548 && !TYPE_OVERFLOW_TRAPS (type)
549 && TYPE_OVERFLOW_WRAPS (type))
550 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
551 return tem;
552 break;
553
554 case POLY_INT_CST:
555 case REAL_CST:
556 case FIXED_CST:
557 tem = fold_negate_const (t, type);
558 return tem;
559
560 case COMPLEX_CST:
561 {
562 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
563 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
564 if (rpart && ipart)
565 return build_complex (type, rpart, ipart);
566 }
567 break;
568
569 case VECTOR_CST:
570 {
571 tree_vector_builder elts;
572 elts.new_unary_operation (type, t, true);
573 unsigned int count = elts.encoded_nelts ();
574 for (unsigned int i = 0; i < count; ++i)
575 {
576 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
577 if (elt == NULL_TREE)
578 return NULL_TREE;
579 elts.quick_push (elt);
580 }
581
582 return elts.build ();
583 }
584
585 case COMPLEX_EXPR:
586 if (negate_expr_p (t))
587 return fold_build2_loc (loc, COMPLEX_EXPR, type,
588 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
589 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
590 break;
591
592 case CONJ_EXPR:
593 if (negate_expr_p (t))
594 return fold_build1_loc (loc, CONJ_EXPR, type,
595 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
596 break;
597
598 case NEGATE_EXPR:
599 if (!TYPE_OVERFLOW_SANITIZED (type))
600 return TREE_OPERAND (t, 0);
601 break;
602
603 case PLUS_EXPR:
604 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
605 && !HONOR_SIGNED_ZEROS (element_mode (type)))
606 {
607 /* -(A + B) -> (-B) - A. */
608 if (negate_expr_p (TREE_OPERAND (t, 1)))
609 {
610 tem = negate_expr (TREE_OPERAND (t, 1));
611 return fold_build2_loc (loc, MINUS_EXPR, type,
612 tem, TREE_OPERAND (t, 0));
613 }
614
615 /* -(A + B) -> (-A) - B. */
616 if (negate_expr_p (TREE_OPERAND (t, 0)))
617 {
618 tem = negate_expr (TREE_OPERAND (t, 0));
619 return fold_build2_loc (loc, MINUS_EXPR, type,
620 tem, TREE_OPERAND (t, 1));
621 }
622 }
623 break;
624
625 case MINUS_EXPR:
626 /* - (A - B) -> B - A */
627 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
628 && !HONOR_SIGNED_ZEROS (element_mode (type)))
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
631 break;
632
633 case MULT_EXPR:
634 if (TYPE_UNSIGNED (type))
635 break;
636
637 /* Fall through. */
638
639 case RDIV_EXPR:
640 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
641 {
642 tem = TREE_OPERAND (t, 1);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 TREE_OPERAND (t, 0), negate_expr (tem));
646 tem = TREE_OPERAND (t, 0);
647 if (negate_expr_p (tem))
648 return fold_build2_loc (loc, TREE_CODE (t), type,
649 negate_expr (tem), TREE_OPERAND (t, 1));
650 }
651 break;
652
653 case TRUNC_DIV_EXPR:
654 case ROUND_DIV_EXPR:
655 case EXACT_DIV_EXPR:
656 if (TYPE_UNSIGNED (type))
657 break;
658 /* In general we can't negate A in A / B, because if A is INT_MIN and
659 B is not 1 we change the sign of the result. */
660 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
661 && negate_expr_p (TREE_OPERAND (t, 0)))
662 return fold_build2_loc (loc, TREE_CODE (t), type,
663 negate_expr (TREE_OPERAND (t, 0)),
664 TREE_OPERAND (t, 1));
665 /* In general we can't negate B in A / B, because if A is INT_MIN and
666 B is 1, we may turn this into INT_MIN / -1 which is undefined
667 and actually traps on some architectures. */
668 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
670 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
671 && ! integer_onep (TREE_OPERAND (t, 1))))
672 && negate_expr_p (TREE_OPERAND (t, 1)))
673 return fold_build2_loc (loc, TREE_CODE (t), type,
674 TREE_OPERAND (t, 0),
675 negate_expr (TREE_OPERAND (t, 1)));
676 break;
677
678 case NOP_EXPR:
679 /* Convert -((double)float) into (double)(-float). */
680 if (TREE_CODE (type) == REAL_TYPE)
681 {
682 tem = strip_float_extensions (t);
683 if (tem != t && negate_expr_p (tem))
684 return fold_convert_loc (loc, type, negate_expr (tem));
685 }
686 break;
687
688 case CALL_EXPR:
689 /* Negate -f(x) as f(-x). */
690 if (negate_mathfn_p (get_call_combined_fn (t))
691 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
692 {
693 tree fndecl, arg;
694
695 fndecl = get_callee_fndecl (t);
696 arg = negate_expr (CALL_EXPR_ARG (t, 0));
697 return build_call_expr_loc (loc, fndecl, 1, arg);
698 }
699 break;
700
701 case RSHIFT_EXPR:
702 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
703 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
704 {
705 tree op1 = TREE_OPERAND (t, 1);
706 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
707 {
708 tree ntype = TYPE_UNSIGNED (type)
709 ? signed_type_for (type)
710 : unsigned_type_for (type);
711 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
712 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
713 return fold_convert_loc (loc, type, temp);
714 }
715 }
716 break;
717
718 default:
719 break;
720 }
721
722 return NULL_TREE;
723 }
724
725 /* A wrapper for fold_negate_expr_1. */
726
727 static tree
728 fold_negate_expr (location_t loc, tree t)
729 {
730 tree type = TREE_TYPE (t);
731 STRIP_SIGN_NOPS (t);
732 tree tem = fold_negate_expr_1 (loc, t);
733 if (tem == NULL_TREE)
734 return NULL_TREE;
735 return fold_convert_loc (loc, type, tem);
736 }
737
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
740 return NULL_TREE. */
741
742 static tree
743 negate_expr (tree t)
744 {
745 tree type, tem;
746 location_t loc;
747
748 if (t == NULL_TREE)
749 return NULL_TREE;
750
751 loc = EXPR_LOCATION (t);
752 type = TREE_TYPE (t);
753 STRIP_SIGN_NOPS (t);
754
755 tem = fold_negate_expr (loc, t);
756 if (!tem)
757 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758 return fold_convert_loc (loc, type, tem);
759 }
760 \f
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
768
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
772
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead. If a variable part is of pointer
775 type, it is negated after converting to TYPE. This prevents us from
776 generating illegal MINUS pointer expression. LOC is the location of
777 the converted variable part.
778
779 If IN is itself a literal or constant, return it as appropriate.
780
781 Note that we do not guarantee that any of the three values will be the
782 same type as IN, but they will have the same signedness and mode. */
783
784 static tree
785 split_tree (tree in, tree type, enum tree_code code,
786 tree *minus_varp, tree *conp, tree *minus_conp,
787 tree *litp, tree *minus_litp, int negate_p)
788 {
789 tree var = 0;
790 *minus_varp = 0;
791 *conp = 0;
792 *minus_conp = 0;
793 *litp = 0;
794 *minus_litp = 0;
795
796 /* Strip any conversions that don't change the machine mode or signedness. */
797 STRIP_SIGN_NOPS (in);
798
799 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
800 || TREE_CODE (in) == FIXED_CST)
801 *litp = in;
802 else if (TREE_CODE (in) == code
803 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
804 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
805 /* We can associate addition and subtraction together (even
806 though the C standard doesn't say so) for integers because
807 the value is not affected. For reals, the value might be
808 affected, so we can't. */
809 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
810 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
811 || (code == MINUS_EXPR
812 && (TREE_CODE (in) == PLUS_EXPR
813 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
814 {
815 tree op0 = TREE_OPERAND (in, 0);
816 tree op1 = TREE_OPERAND (in, 1);
817 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
818 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
819
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
822 || TREE_CODE (op0) == FIXED_CST)
823 *litp = op0, op0 = 0;
824 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
825 || TREE_CODE (op1) == FIXED_CST)
826 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
827
828 if (op0 != 0 && TREE_CONSTANT (op0))
829 *conp = op0, op0 = 0;
830 else if (op1 != 0 && TREE_CONSTANT (op1))
831 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
832
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0 != 0 && op1 != 0)
836 var = in;
837 else if (op0 != 0)
838 var = op0;
839 else
840 var = op1, neg_var_p = neg1_p;
841
842 /* Now do any needed negations. */
843 if (neg_litp_p)
844 *minus_litp = *litp, *litp = 0;
845 if (neg_conp_p && *conp)
846 *minus_conp = *conp, *conp = 0;
847 if (neg_var_p && var)
848 *minus_varp = var, var = 0;
849 }
850 else if (TREE_CONSTANT (in))
851 *conp = in;
852 else if (TREE_CODE (in) == BIT_NOT_EXPR
853 && code == PLUS_EXPR)
854 {
855 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *litp = build_minus_one_cst (type);
858 *minus_varp = TREE_OPERAND (in, 0);
859 }
860 else
861 var = in;
862
863 if (negate_p)
864 {
865 if (*litp)
866 *minus_litp = *litp, *litp = 0;
867 else if (*minus_litp)
868 *litp = *minus_litp, *minus_litp = 0;
869 if (*conp)
870 *minus_conp = *conp, *conp = 0;
871 else if (*minus_conp)
872 *conp = *minus_conp, *minus_conp = 0;
873 if (var)
874 *minus_varp = var, var = 0;
875 else if (*minus_varp)
876 var = *minus_varp, *minus_varp = 0;
877 }
878
879 if (*litp
880 && TREE_OVERFLOW_P (*litp))
881 *litp = drop_tree_overflow (*litp);
882 if (*minus_litp
883 && TREE_OVERFLOW_P (*minus_litp))
884 *minus_litp = drop_tree_overflow (*minus_litp);
885
886 return var;
887 }
888
889 /* Re-associate trees split by the above function. T1 and T2 are
890 either expressions to associate or null. Return the new
891 expression, if any. LOC is the location of the new expression. If
892 we build an operation, do it in TYPE and with CODE. */
893
894 static tree
895 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
896 {
897 if (t1 == 0)
898 {
899 gcc_assert (t2 == 0 || code != MINUS_EXPR);
900 return t2;
901 }
902 else if (t2 == 0)
903 return t1;
904
905 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
906 try to fold this since we will have infinite recursion. But do
907 deal with any NEGATE_EXPRs. */
908 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
909 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
910 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
911 {
912 if (code == PLUS_EXPR)
913 {
914 if (TREE_CODE (t1) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t2),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t1, 0)));
919 else if (TREE_CODE (t2) == NEGATE_EXPR)
920 return build2_loc (loc, MINUS_EXPR, type,
921 fold_convert_loc (loc, type, t1),
922 fold_convert_loc (loc, type,
923 TREE_OPERAND (t2, 0)));
924 else if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
926 }
927 else if (code == MINUS_EXPR)
928 {
929 if (integer_zerop (t2))
930 return fold_convert_loc (loc, type, t1);
931 }
932
933 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
934 fold_convert_loc (loc, type, t2));
935 }
936
937 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
938 fold_convert_loc (loc, type, t2));
939 }
940 \f
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
943
944 static bool
945 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
946 {
947 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
948 return false;
949 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return false;
951
952 switch (code)
953 {
954 case LSHIFT_EXPR:
955 case RSHIFT_EXPR:
956 case LROTATE_EXPR:
957 case RROTATE_EXPR:
958 return true;
959
960 default:
961 break;
962 }
963
964 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
965 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
966 && TYPE_MODE (type1) == TYPE_MODE (type2);
967 }
968
969 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
970 a new constant in RES. Return FALSE if we don't know how to
971 evaluate CODE at compile-time. */
972
973 bool
974 wide_int_binop (wide_int &res,
975 enum tree_code code, const wide_int &arg1, const wide_int &arg2,
976 signop sign, wi::overflow_type *overflow)
977 {
978 wide_int tmp;
979 *overflow = wi::OVF_NONE;
980 switch (code)
981 {
982 case BIT_IOR_EXPR:
983 res = wi::bit_or (arg1, arg2);
984 break;
985
986 case BIT_XOR_EXPR:
987 res = wi::bit_xor (arg1, arg2);
988 break;
989
990 case BIT_AND_EXPR:
991 res = wi::bit_and (arg1, arg2);
992 break;
993
994 case RSHIFT_EXPR:
995 case LSHIFT_EXPR:
996 if (wi::neg_p (arg2))
997 {
998 tmp = -arg2;
999 if (code == RSHIFT_EXPR)
1000 code = LSHIFT_EXPR;
1001 else
1002 code = RSHIFT_EXPR;
1003 }
1004 else
1005 tmp = arg2;
1006
1007 if (code == RSHIFT_EXPR)
1008 /* It's unclear from the C standard whether shifts can overflow.
1009 The following code ignores overflow; perhaps a C standard
1010 interpretation ruling is needed. */
1011 res = wi::rshift (arg1, tmp, sign);
1012 else
1013 res = wi::lshift (arg1, tmp);
1014 break;
1015
1016 case RROTATE_EXPR:
1017 case LROTATE_EXPR:
1018 if (wi::neg_p (arg2))
1019 {
1020 tmp = -arg2;
1021 if (code == RROTATE_EXPR)
1022 code = LROTATE_EXPR;
1023 else
1024 code = RROTATE_EXPR;
1025 }
1026 else
1027 tmp = arg2;
1028
1029 if (code == RROTATE_EXPR)
1030 res = wi::rrotate (arg1, tmp);
1031 else
1032 res = wi::lrotate (arg1, tmp);
1033 break;
1034
1035 case PLUS_EXPR:
1036 res = wi::add (arg1, arg2, sign, overflow);
1037 break;
1038
1039 case MINUS_EXPR:
1040 res = wi::sub (arg1, arg2, sign, overflow);
1041 break;
1042
1043 case MULT_EXPR:
1044 res = wi::mul (arg1, arg2, sign, overflow);
1045 break;
1046
1047 case MULT_HIGHPART_EXPR:
1048 res = wi::mul_high (arg1, arg2, sign);
1049 break;
1050
1051 case TRUNC_DIV_EXPR:
1052 case EXACT_DIV_EXPR:
1053 if (arg2 == 0)
1054 return false;
1055 res = wi::div_trunc (arg1, arg2, sign, overflow);
1056 break;
1057
1058 case FLOOR_DIV_EXPR:
1059 if (arg2 == 0)
1060 return false;
1061 res = wi::div_floor (arg1, arg2, sign, overflow);
1062 break;
1063
1064 case CEIL_DIV_EXPR:
1065 if (arg2 == 0)
1066 return false;
1067 res = wi::div_ceil (arg1, arg2, sign, overflow);
1068 break;
1069
1070 case ROUND_DIV_EXPR:
1071 if (arg2 == 0)
1072 return false;
1073 res = wi::div_round (arg1, arg2, sign, overflow);
1074 break;
1075
1076 case TRUNC_MOD_EXPR:
1077 if (arg2 == 0)
1078 return false;
1079 res = wi::mod_trunc (arg1, arg2, sign, overflow);
1080 break;
1081
1082 case FLOOR_MOD_EXPR:
1083 if (arg2 == 0)
1084 return false;
1085 res = wi::mod_floor (arg1, arg2, sign, overflow);
1086 break;
1087
1088 case CEIL_MOD_EXPR:
1089 if (arg2 == 0)
1090 return false;
1091 res = wi::mod_ceil (arg1, arg2, sign, overflow);
1092 break;
1093
1094 case ROUND_MOD_EXPR:
1095 if (arg2 == 0)
1096 return false;
1097 res = wi::mod_round (arg1, arg2, sign, overflow);
1098 break;
1099
1100 case MIN_EXPR:
1101 res = wi::min (arg1, arg2, sign);
1102 break;
1103
1104 case MAX_EXPR:
1105 res = wi::max (arg1, arg2, sign);
1106 break;
1107
1108 default:
1109 return false;
1110 }
1111 return true;
1112 }
1113
1114 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1115 produce a new constant in RES. Return FALSE if we don't know how
1116 to evaluate CODE at compile-time. */
1117
1118 static bool
1119 poly_int_binop (poly_wide_int &res, enum tree_code code,
1120 const_tree arg1, const_tree arg2,
1121 signop sign, wi::overflow_type *overflow)
1122 {
1123 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1124 gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1125 switch (code)
1126 {
1127 case PLUS_EXPR:
1128 res = wi::add (wi::to_poly_wide (arg1),
1129 wi::to_poly_wide (arg2), sign, overflow);
1130 break;
1131
1132 case MINUS_EXPR:
1133 res = wi::sub (wi::to_poly_wide (arg1),
1134 wi::to_poly_wide (arg2), sign, overflow);
1135 break;
1136
1137 case MULT_EXPR:
1138 if (TREE_CODE (arg2) == INTEGER_CST)
1139 res = wi::mul (wi::to_poly_wide (arg1),
1140 wi::to_wide (arg2), sign, overflow);
1141 else if (TREE_CODE (arg1) == INTEGER_CST)
1142 res = wi::mul (wi::to_poly_wide (arg2),
1143 wi::to_wide (arg1), sign, overflow);
1144 else
1145 return NULL_TREE;
1146 break;
1147
1148 case LSHIFT_EXPR:
1149 if (TREE_CODE (arg2) == INTEGER_CST)
1150 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1151 else
1152 return false;
1153 break;
1154
1155 case BIT_IOR_EXPR:
1156 if (TREE_CODE (arg2) != INTEGER_CST
1157 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1158 &res))
1159 return false;
1160 break;
1161
1162 default:
1163 return false;
1164 }
1165 return true;
1166 }
1167
1168 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1169 produce a new constant. Return NULL_TREE if we don't know how to
1170 evaluate CODE at compile-time. */
1171
1172 tree
1173 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1174 int overflowable)
1175 {
1176 bool success = false;
1177 poly_wide_int poly_res;
1178 tree type = TREE_TYPE (arg1);
1179 signop sign = TYPE_SIGN (type);
1180 wi::overflow_type overflow = wi::OVF_NONE;
1181
1182 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1183 {
1184 wide_int warg1 = wi::to_wide (arg1), res;
1185 wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (type));
1186 success = wide_int_binop (res, code, warg1, warg2, sign, &overflow);
1187 poly_res = res;
1188 }
1189 else if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1190 success = poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow);
1191 if (success)
1192 return force_fit_type (type, poly_res, overflowable,
1193 (((sign == SIGNED || overflowable == -1)
1194 && overflow)
1195 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1196 return NULL_TREE;
1197 }
1198
1199 /* Return true if binary operation OP distributes over addition in operand
1200 OPNO, with the other operand being held constant. OPNO counts from 1. */
1201
1202 static bool
1203 distributes_over_addition_p (tree_code op, int opno)
1204 {
1205 switch (op)
1206 {
1207 case PLUS_EXPR:
1208 case MINUS_EXPR:
1209 case MULT_EXPR:
1210 return true;
1211
1212 case LSHIFT_EXPR:
1213 return opno == 1;
1214
1215 default:
1216 return false;
1217 }
1218 }
1219
1220 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1221 constant. We assume ARG1 and ARG2 have the same data type, or at least
1222 are the same kind of constant and the same machine mode. Return zero if
1223 combining the constants is not allowed in the current operating mode. */
1224
1225 static tree
1226 const_binop (enum tree_code code, tree arg1, tree arg2)
1227 {
1228 /* Sanity check for the recursive cases. */
1229 if (!arg1 || !arg2)
1230 return NULL_TREE;
1231
1232 STRIP_NOPS (arg1);
1233 STRIP_NOPS (arg2);
1234
1235 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1236 {
1237 if (code == POINTER_PLUS_EXPR)
1238 return int_const_binop (PLUS_EXPR,
1239 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1240
1241 return int_const_binop (code, arg1, arg2);
1242 }
1243
1244 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1245 {
1246 machine_mode mode;
1247 REAL_VALUE_TYPE d1;
1248 REAL_VALUE_TYPE d2;
1249 REAL_VALUE_TYPE value;
1250 REAL_VALUE_TYPE result;
1251 bool inexact;
1252 tree t, type;
1253
1254 /* The following codes are handled by real_arithmetic. */
1255 switch (code)
1256 {
1257 case PLUS_EXPR:
1258 case MINUS_EXPR:
1259 case MULT_EXPR:
1260 case RDIV_EXPR:
1261 case MIN_EXPR:
1262 case MAX_EXPR:
1263 break;
1264
1265 default:
1266 return NULL_TREE;
1267 }
1268
1269 d1 = TREE_REAL_CST (arg1);
1270 d2 = TREE_REAL_CST (arg2);
1271
1272 type = TREE_TYPE (arg1);
1273 mode = TYPE_MODE (type);
1274
1275 /* Don't perform operation if we honor signaling NaNs and
1276 either operand is a signaling NaN. */
1277 if (HONOR_SNANS (mode)
1278 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1279 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1280 return NULL_TREE;
1281
1282 /* Don't perform operation if it would raise a division
1283 by zero exception. */
1284 if (code == RDIV_EXPR
1285 && real_equal (&d2, &dconst0)
1286 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1287 return NULL_TREE;
1288
1289 /* If either operand is a NaN, just return it. Otherwise, set up
1290 for floating-point trap; we return an overflow. */
1291 if (REAL_VALUE_ISNAN (d1))
1292 {
1293 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1294 is off. */
1295 d1.signalling = 0;
1296 t = build_real (type, d1);
1297 return t;
1298 }
1299 else if (REAL_VALUE_ISNAN (d2))
1300 {
1301 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1302 is off. */
1303 d2.signalling = 0;
1304 t = build_real (type, d2);
1305 return t;
1306 }
1307
1308 inexact = real_arithmetic (&value, code, &d1, &d2);
1309 real_convert (&result, mode, &value);
1310
1311 /* Don't constant fold this floating point operation if
1312 the result has overflowed and flag_trapping_math. */
1313 if (flag_trapping_math
1314 && MODE_HAS_INFINITIES (mode)
1315 && REAL_VALUE_ISINF (result)
1316 && !REAL_VALUE_ISINF (d1)
1317 && !REAL_VALUE_ISINF (d2))
1318 return NULL_TREE;
1319
1320 /* Don't constant fold this floating point operation if the
1321 result may dependent upon the run-time rounding mode and
1322 flag_rounding_math is set, or if GCC's software emulation
1323 is unable to accurately represent the result. */
1324 if ((flag_rounding_math
1325 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1326 && (inexact || !real_identical (&result, &value)))
1327 return NULL_TREE;
1328
1329 t = build_real (type, result);
1330
1331 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1332 return t;
1333 }
1334
1335 if (TREE_CODE (arg1) == FIXED_CST)
1336 {
1337 FIXED_VALUE_TYPE f1;
1338 FIXED_VALUE_TYPE f2;
1339 FIXED_VALUE_TYPE result;
1340 tree t, type;
1341 int sat_p;
1342 bool overflow_p;
1343
1344 /* The following codes are handled by fixed_arithmetic. */
1345 switch (code)
1346 {
1347 case PLUS_EXPR:
1348 case MINUS_EXPR:
1349 case MULT_EXPR:
1350 case TRUNC_DIV_EXPR:
1351 if (TREE_CODE (arg2) != FIXED_CST)
1352 return NULL_TREE;
1353 f2 = TREE_FIXED_CST (arg2);
1354 break;
1355
1356 case LSHIFT_EXPR:
1357 case RSHIFT_EXPR:
1358 {
1359 if (TREE_CODE (arg2) != INTEGER_CST)
1360 return NULL_TREE;
1361 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1362 f2.data.high = w2.elt (1);
1363 f2.data.low = w2.ulow ();
1364 f2.mode = SImode;
1365 }
1366 break;
1367
1368 default:
1369 return NULL_TREE;
1370 }
1371
1372 f1 = TREE_FIXED_CST (arg1);
1373 type = TREE_TYPE (arg1);
1374 sat_p = TYPE_SATURATING (type);
1375 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1376 t = build_fixed (type, result);
1377 /* Propagate overflow flags. */
1378 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1379 TREE_OVERFLOW (t) = 1;
1380 return t;
1381 }
1382
1383 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1384 {
1385 tree type = TREE_TYPE (arg1);
1386 tree r1 = TREE_REALPART (arg1);
1387 tree i1 = TREE_IMAGPART (arg1);
1388 tree r2 = TREE_REALPART (arg2);
1389 tree i2 = TREE_IMAGPART (arg2);
1390 tree real, imag;
1391
1392 switch (code)
1393 {
1394 case PLUS_EXPR:
1395 case MINUS_EXPR:
1396 real = const_binop (code, r1, r2);
1397 imag = const_binop (code, i1, i2);
1398 break;
1399
1400 case MULT_EXPR:
1401 if (COMPLEX_FLOAT_TYPE_P (type))
1402 return do_mpc_arg2 (arg1, arg2, type,
1403 /* do_nonfinite= */ folding_initializer,
1404 mpc_mul);
1405
1406 real = const_binop (MINUS_EXPR,
1407 const_binop (MULT_EXPR, r1, r2),
1408 const_binop (MULT_EXPR, i1, i2));
1409 imag = const_binop (PLUS_EXPR,
1410 const_binop (MULT_EXPR, r1, i2),
1411 const_binop (MULT_EXPR, i1, r2));
1412 break;
1413
1414 case RDIV_EXPR:
1415 if (COMPLEX_FLOAT_TYPE_P (type))
1416 return do_mpc_arg2 (arg1, arg2, type,
1417 /* do_nonfinite= */ folding_initializer,
1418 mpc_div);
1419 /* Fallthru. */
1420 case TRUNC_DIV_EXPR:
1421 case CEIL_DIV_EXPR:
1422 case FLOOR_DIV_EXPR:
1423 case ROUND_DIV_EXPR:
1424 if (flag_complex_method == 0)
1425 {
1426 /* Keep this algorithm in sync with
1427 tree-complex.c:expand_complex_div_straight().
1428
1429 Expand complex division to scalars, straightforward algorithm.
1430 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1431 t = br*br + bi*bi
1432 */
1433 tree magsquared
1434 = const_binop (PLUS_EXPR,
1435 const_binop (MULT_EXPR, r2, r2),
1436 const_binop (MULT_EXPR, i2, i2));
1437 tree t1
1438 = const_binop (PLUS_EXPR,
1439 const_binop (MULT_EXPR, r1, r2),
1440 const_binop (MULT_EXPR, i1, i2));
1441 tree t2
1442 = const_binop (MINUS_EXPR,
1443 const_binop (MULT_EXPR, i1, r2),
1444 const_binop (MULT_EXPR, r1, i2));
1445
1446 real = const_binop (code, t1, magsquared);
1447 imag = const_binop (code, t2, magsquared);
1448 }
1449 else
1450 {
1451 /* Keep this algorithm in sync with
1452 tree-complex.c:expand_complex_div_wide().
1453
1454 Expand complex division to scalars, modified algorithm to minimize
1455 overflow with wide input ranges. */
1456 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1457 fold_abs_const (r2, TREE_TYPE (type)),
1458 fold_abs_const (i2, TREE_TYPE (type)));
1459
1460 if (integer_nonzerop (compare))
1461 {
1462 /* In the TRUE branch, we compute
1463 ratio = br/bi;
1464 div = (br * ratio) + bi;
1465 tr = (ar * ratio) + ai;
1466 ti = (ai * ratio) - ar;
1467 tr = tr / div;
1468 ti = ti / div; */
1469 tree ratio = const_binop (code, r2, i2);
1470 tree div = const_binop (PLUS_EXPR, i2,
1471 const_binop (MULT_EXPR, r2, ratio));
1472 real = const_binop (MULT_EXPR, r1, ratio);
1473 real = const_binop (PLUS_EXPR, real, i1);
1474 real = const_binop (code, real, div);
1475
1476 imag = const_binop (MULT_EXPR, i1, ratio);
1477 imag = const_binop (MINUS_EXPR, imag, r1);
1478 imag = const_binop (code, imag, div);
1479 }
1480 else
1481 {
1482 /* In the FALSE branch, we compute
1483 ratio = d/c;
1484 divisor = (d * ratio) + c;
1485 tr = (b * ratio) + a;
1486 ti = b - (a * ratio);
1487 tr = tr / div;
1488 ti = ti / div; */
1489 tree ratio = const_binop (code, i2, r2);
1490 tree div = const_binop (PLUS_EXPR, r2,
1491 const_binop (MULT_EXPR, i2, ratio));
1492
1493 real = const_binop (MULT_EXPR, i1, ratio);
1494 real = const_binop (PLUS_EXPR, real, r1);
1495 real = const_binop (code, real, div);
1496
1497 imag = const_binop (MULT_EXPR, r1, ratio);
1498 imag = const_binop (MINUS_EXPR, i1, imag);
1499 imag = const_binop (code, imag, div);
1500 }
1501 }
1502 break;
1503
1504 default:
1505 return NULL_TREE;
1506 }
1507
1508 if (real && imag)
1509 return build_complex (type, real, imag);
1510 }
1511
1512 if (TREE_CODE (arg1) == VECTOR_CST
1513 && TREE_CODE (arg2) == VECTOR_CST
1514 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1515 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1516 {
1517 tree type = TREE_TYPE (arg1);
1518 bool step_ok_p;
1519 if (VECTOR_CST_STEPPED_P (arg1)
1520 && VECTOR_CST_STEPPED_P (arg2))
1521 /* We can operate directly on the encoding if:
1522
1523 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1524 implies
1525 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1526
1527 Addition and subtraction are the supported operators
1528 for which this is true. */
1529 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1530 else if (VECTOR_CST_STEPPED_P (arg1))
1531 /* We can operate directly on stepped encodings if:
1532
1533 a3 - a2 == a2 - a1
1534 implies:
1535 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1536
1537 which is true if (x -> x op c) distributes over addition. */
1538 step_ok_p = distributes_over_addition_p (code, 1);
1539 else
1540 /* Similarly in reverse. */
1541 step_ok_p = distributes_over_addition_p (code, 2);
1542 tree_vector_builder elts;
1543 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1544 return NULL_TREE;
1545 unsigned int count = elts.encoded_nelts ();
1546 for (unsigned int i = 0; i < count; ++i)
1547 {
1548 tree elem1 = VECTOR_CST_ELT (arg1, i);
1549 tree elem2 = VECTOR_CST_ELT (arg2, i);
1550
1551 tree elt = const_binop (code, elem1, elem2);
1552
1553 /* It is possible that const_binop cannot handle the given
1554 code and return NULL_TREE */
1555 if (elt == NULL_TREE)
1556 return NULL_TREE;
1557 elts.quick_push (elt);
1558 }
1559
1560 return elts.build ();
1561 }
1562
1563 /* Shifts allow a scalar offset for a vector. */
1564 if (TREE_CODE (arg1) == VECTOR_CST
1565 && TREE_CODE (arg2) == INTEGER_CST)
1566 {
1567 tree type = TREE_TYPE (arg1);
1568 bool step_ok_p = distributes_over_addition_p (code, 1);
1569 tree_vector_builder elts;
1570 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1571 return NULL_TREE;
1572 unsigned int count = elts.encoded_nelts ();
1573 for (unsigned int i = 0; i < count; ++i)
1574 {
1575 tree elem1 = VECTOR_CST_ELT (arg1, i);
1576
1577 tree elt = const_binop (code, elem1, arg2);
1578
1579 /* It is possible that const_binop cannot handle the given
1580 code and return NULL_TREE. */
1581 if (elt == NULL_TREE)
1582 return NULL_TREE;
1583 elts.quick_push (elt);
1584 }
1585
1586 return elts.build ();
1587 }
1588 return NULL_TREE;
1589 }
1590
1591 /* Overload that adds a TYPE parameter to be able to dispatch
1592 to fold_relational_const. */
1593
1594 tree
1595 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1596 {
1597 if (TREE_CODE_CLASS (code) == tcc_comparison)
1598 return fold_relational_const (code, type, arg1, arg2);
1599
1600 /* ??? Until we make the const_binop worker take the type of the
1601 result as argument put those cases that need it here. */
1602 switch (code)
1603 {
1604 case VEC_SERIES_EXPR:
1605 if (CONSTANT_CLASS_P (arg1)
1606 && CONSTANT_CLASS_P (arg2))
1607 return build_vec_series (type, arg1, arg2);
1608 return NULL_TREE;
1609
1610 case COMPLEX_EXPR:
1611 if ((TREE_CODE (arg1) == REAL_CST
1612 && TREE_CODE (arg2) == REAL_CST)
1613 || (TREE_CODE (arg1) == INTEGER_CST
1614 && TREE_CODE (arg2) == INTEGER_CST))
1615 return build_complex (type, arg1, arg2);
1616 return NULL_TREE;
1617
1618 case POINTER_DIFF_EXPR:
1619 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1620 {
1621 poly_offset_int res = (wi::to_poly_offset (arg1)
1622 - wi::to_poly_offset (arg2));
1623 return force_fit_type (type, res, 1,
1624 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1625 }
1626 return NULL_TREE;
1627
1628 case VEC_PACK_TRUNC_EXPR:
1629 case VEC_PACK_FIX_TRUNC_EXPR:
1630 case VEC_PACK_FLOAT_EXPR:
1631 {
1632 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1633
1634 if (TREE_CODE (arg1) != VECTOR_CST
1635 || TREE_CODE (arg2) != VECTOR_CST)
1636 return NULL_TREE;
1637
1638 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1639 return NULL_TREE;
1640
1641 out_nelts = in_nelts * 2;
1642 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1643 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1644
1645 tree_vector_builder elts (type, out_nelts, 1);
1646 for (i = 0; i < out_nelts; i++)
1647 {
1648 tree elt = (i < in_nelts
1649 ? VECTOR_CST_ELT (arg1, i)
1650 : VECTOR_CST_ELT (arg2, i - in_nelts));
1651 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1652 ? NOP_EXPR
1653 : code == VEC_PACK_FLOAT_EXPR
1654 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1655 TREE_TYPE (type), elt);
1656 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1657 return NULL_TREE;
1658 elts.quick_push (elt);
1659 }
1660
1661 return elts.build ();
1662 }
1663
1664 case VEC_WIDEN_MULT_LO_EXPR:
1665 case VEC_WIDEN_MULT_HI_EXPR:
1666 case VEC_WIDEN_MULT_EVEN_EXPR:
1667 case VEC_WIDEN_MULT_ODD_EXPR:
1668 {
1669 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1670
1671 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1672 return NULL_TREE;
1673
1674 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1675 return NULL_TREE;
1676 out_nelts = in_nelts / 2;
1677 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1678 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1679
1680 if (code == VEC_WIDEN_MULT_LO_EXPR)
1681 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1682 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1683 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1684 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1685 scale = 1, ofs = 0;
1686 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1687 scale = 1, ofs = 1;
1688
1689 tree_vector_builder elts (type, out_nelts, 1);
1690 for (out = 0; out < out_nelts; out++)
1691 {
1692 unsigned int in = (out << scale) + ofs;
1693 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1694 VECTOR_CST_ELT (arg1, in));
1695 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1696 VECTOR_CST_ELT (arg2, in));
1697
1698 if (t1 == NULL_TREE || t2 == NULL_TREE)
1699 return NULL_TREE;
1700 tree elt = const_binop (MULT_EXPR, t1, t2);
1701 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1702 return NULL_TREE;
1703 elts.quick_push (elt);
1704 }
1705
1706 return elts.build ();
1707 }
1708
1709 default:;
1710 }
1711
1712 if (TREE_CODE_CLASS (code) != tcc_binary)
1713 return NULL_TREE;
1714
1715 /* Make sure type and arg0 have the same saturating flag. */
1716 gcc_checking_assert (TYPE_SATURATING (type)
1717 == TYPE_SATURATING (TREE_TYPE (arg1)));
1718
1719 return const_binop (code, arg1, arg2);
1720 }
1721
1722 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1723 Return zero if computing the constants is not possible. */
1724
1725 tree
1726 const_unop (enum tree_code code, tree type, tree arg0)
1727 {
1728 /* Don't perform the operation, other than NEGATE and ABS, if
1729 flag_signaling_nans is on and the operand is a signaling NaN. */
1730 if (TREE_CODE (arg0) == REAL_CST
1731 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1732 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1733 && code != NEGATE_EXPR
1734 && code != ABS_EXPR
1735 && code != ABSU_EXPR)
1736 return NULL_TREE;
1737
1738 switch (code)
1739 {
1740 CASE_CONVERT:
1741 case FLOAT_EXPR:
1742 case FIX_TRUNC_EXPR:
1743 case FIXED_CONVERT_EXPR:
1744 return fold_convert_const (code, type, arg0);
1745
1746 case ADDR_SPACE_CONVERT_EXPR:
1747 /* If the source address is 0, and the source address space
1748 cannot have a valid object at 0, fold to dest type null. */
1749 if (integer_zerop (arg0)
1750 && !(targetm.addr_space.zero_address_valid
1751 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1752 return fold_convert_const (code, type, arg0);
1753 break;
1754
1755 case VIEW_CONVERT_EXPR:
1756 return fold_view_convert_expr (type, arg0);
1757
1758 case NEGATE_EXPR:
1759 {
1760 /* Can't call fold_negate_const directly here as that doesn't
1761 handle all cases and we might not be able to negate some
1762 constants. */
1763 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1764 if (tem && CONSTANT_CLASS_P (tem))
1765 return tem;
1766 break;
1767 }
1768
1769 case ABS_EXPR:
1770 case ABSU_EXPR:
1771 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1772 return fold_abs_const (arg0, type);
1773 break;
1774
1775 case CONJ_EXPR:
1776 if (TREE_CODE (arg0) == COMPLEX_CST)
1777 {
1778 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1779 TREE_TYPE (type));
1780 return build_complex (type, TREE_REALPART (arg0), ipart);
1781 }
1782 break;
1783
1784 case BIT_NOT_EXPR:
1785 if (TREE_CODE (arg0) == INTEGER_CST)
1786 return fold_not_const (arg0, type);
1787 else if (POLY_INT_CST_P (arg0))
1788 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1789 /* Perform BIT_NOT_EXPR on each element individually. */
1790 else if (TREE_CODE (arg0) == VECTOR_CST)
1791 {
1792 tree elem;
1793
1794 /* This can cope with stepped encodings because ~x == -1 - x. */
1795 tree_vector_builder elements;
1796 elements.new_unary_operation (type, arg0, true);
1797 unsigned int i, count = elements.encoded_nelts ();
1798 for (i = 0; i < count; ++i)
1799 {
1800 elem = VECTOR_CST_ELT (arg0, i);
1801 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1802 if (elem == NULL_TREE)
1803 break;
1804 elements.quick_push (elem);
1805 }
1806 if (i == count)
1807 return elements.build ();
1808 }
1809 break;
1810
1811 case TRUTH_NOT_EXPR:
1812 if (TREE_CODE (arg0) == INTEGER_CST)
1813 return constant_boolean_node (integer_zerop (arg0), type);
1814 break;
1815
1816 case REALPART_EXPR:
1817 if (TREE_CODE (arg0) == COMPLEX_CST)
1818 return fold_convert (type, TREE_REALPART (arg0));
1819 break;
1820
1821 case IMAGPART_EXPR:
1822 if (TREE_CODE (arg0) == COMPLEX_CST)
1823 return fold_convert (type, TREE_IMAGPART (arg0));
1824 break;
1825
1826 case VEC_UNPACK_LO_EXPR:
1827 case VEC_UNPACK_HI_EXPR:
1828 case VEC_UNPACK_FLOAT_LO_EXPR:
1829 case VEC_UNPACK_FLOAT_HI_EXPR:
1830 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1831 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1832 {
1833 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1834 enum tree_code subcode;
1835
1836 if (TREE_CODE (arg0) != VECTOR_CST)
1837 return NULL_TREE;
1838
1839 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1840 return NULL_TREE;
1841 out_nelts = in_nelts / 2;
1842 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1843
1844 unsigned int offset = 0;
1845 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1846 || code == VEC_UNPACK_FLOAT_LO_EXPR
1847 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1848 offset = out_nelts;
1849
1850 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1851 subcode = NOP_EXPR;
1852 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1853 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1854 subcode = FLOAT_EXPR;
1855 else
1856 subcode = FIX_TRUNC_EXPR;
1857
1858 tree_vector_builder elts (type, out_nelts, 1);
1859 for (i = 0; i < out_nelts; i++)
1860 {
1861 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1862 VECTOR_CST_ELT (arg0, i + offset));
1863 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1864 return NULL_TREE;
1865 elts.quick_push (elt);
1866 }
1867
1868 return elts.build ();
1869 }
1870
1871 case VEC_DUPLICATE_EXPR:
1872 if (CONSTANT_CLASS_P (arg0))
1873 return build_vector_from_val (type, arg0);
1874 return NULL_TREE;
1875
1876 default:
1877 break;
1878 }
1879
1880 return NULL_TREE;
1881 }
1882
1883 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1884 indicates which particular sizetype to create. */
1885
1886 tree
1887 size_int_kind (poly_int64 number, enum size_type_kind kind)
1888 {
1889 return build_int_cst (sizetype_tab[(int) kind], number);
1890 }
1891 \f
1892 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1893 is a tree code. The type of the result is taken from the operands.
1894 Both must be equivalent integer types, ala int_binop_types_match_p.
1895 If the operands are constant, so is the result. */
1896
1897 tree
1898 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1899 {
1900 tree type = TREE_TYPE (arg0);
1901
1902 if (arg0 == error_mark_node || arg1 == error_mark_node)
1903 return error_mark_node;
1904
1905 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1906 TREE_TYPE (arg1)));
1907
1908 /* Handle the special case of two poly_int constants faster. */
1909 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1910 {
1911 /* And some specific cases even faster than that. */
1912 if (code == PLUS_EXPR)
1913 {
1914 if (integer_zerop (arg0)
1915 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1916 return arg1;
1917 if (integer_zerop (arg1)
1918 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1919 return arg0;
1920 }
1921 else if (code == MINUS_EXPR)
1922 {
1923 if (integer_zerop (arg1)
1924 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1925 return arg0;
1926 }
1927 else if (code == MULT_EXPR)
1928 {
1929 if (integer_onep (arg0)
1930 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1931 return arg1;
1932 }
1933
1934 /* Handle general case of two integer constants. For sizetype
1935 constant calculations we always want to know about overflow,
1936 even in the unsigned case. */
1937 tree res = int_const_binop (code, arg0, arg1, -1);
1938 if (res != NULL_TREE)
1939 return res;
1940 }
1941
1942 return fold_build2_loc (loc, code, type, arg0, arg1);
1943 }
1944
1945 /* Given two values, either both of sizetype or both of bitsizetype,
1946 compute the difference between the two values. Return the value
1947 in signed type corresponding to the type of the operands. */
1948
1949 tree
1950 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1951 {
1952 tree type = TREE_TYPE (arg0);
1953 tree ctype;
1954
1955 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1956 TREE_TYPE (arg1)));
1957
1958 /* If the type is already signed, just do the simple thing. */
1959 if (!TYPE_UNSIGNED (type))
1960 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1961
1962 if (type == sizetype)
1963 ctype = ssizetype;
1964 else if (type == bitsizetype)
1965 ctype = sbitsizetype;
1966 else
1967 ctype = signed_type_for (type);
1968
1969 /* If either operand is not a constant, do the conversions to the signed
1970 type and subtract. The hardware will do the right thing with any
1971 overflow in the subtraction. */
1972 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1973 return size_binop_loc (loc, MINUS_EXPR,
1974 fold_convert_loc (loc, ctype, arg0),
1975 fold_convert_loc (loc, ctype, arg1));
1976
1977 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1978 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1979 overflow) and negate (which can't either). Special-case a result
1980 of zero while we're here. */
1981 if (tree_int_cst_equal (arg0, arg1))
1982 return build_int_cst (ctype, 0);
1983 else if (tree_int_cst_lt (arg1, arg0))
1984 return fold_convert_loc (loc, ctype,
1985 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1986 else
1987 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1988 fold_convert_loc (loc, ctype,
1989 size_binop_loc (loc,
1990 MINUS_EXPR,
1991 arg1, arg0)));
1992 }
1993 \f
1994 /* A subroutine of fold_convert_const handling conversions of an
1995 INTEGER_CST to another integer type. */
1996
1997 static tree
1998 fold_convert_const_int_from_int (tree type, const_tree arg1)
1999 {
2000 /* Given an integer constant, make new constant with new type,
2001 appropriately sign-extended or truncated. Use widest_int
2002 so that any extension is done according ARG1's type. */
2003 return force_fit_type (type, wi::to_widest (arg1),
2004 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2005 TREE_OVERFLOW (arg1));
2006 }
2007
2008 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2009 to an integer type. */
2010
2011 static tree
2012 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2013 {
2014 bool overflow = false;
2015 tree t;
2016
2017 /* The following code implements the floating point to integer
2018 conversion rules required by the Java Language Specification,
2019 that IEEE NaNs are mapped to zero and values that overflow
2020 the target precision saturate, i.e. values greater than
2021 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2022 are mapped to INT_MIN. These semantics are allowed by the
2023 C and C++ standards that simply state that the behavior of
2024 FP-to-integer conversion is unspecified upon overflow. */
2025
2026 wide_int val;
2027 REAL_VALUE_TYPE r;
2028 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2029
2030 switch (code)
2031 {
2032 case FIX_TRUNC_EXPR:
2033 real_trunc (&r, VOIDmode, &x);
2034 break;
2035
2036 default:
2037 gcc_unreachable ();
2038 }
2039
2040 /* If R is NaN, return zero and show we have an overflow. */
2041 if (REAL_VALUE_ISNAN (r))
2042 {
2043 overflow = true;
2044 val = wi::zero (TYPE_PRECISION (type));
2045 }
2046
2047 /* See if R is less than the lower bound or greater than the
2048 upper bound. */
2049
2050 if (! overflow)
2051 {
2052 tree lt = TYPE_MIN_VALUE (type);
2053 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2054 if (real_less (&r, &l))
2055 {
2056 overflow = true;
2057 val = wi::to_wide (lt);
2058 }
2059 }
2060
2061 if (! overflow)
2062 {
2063 tree ut = TYPE_MAX_VALUE (type);
2064 if (ut)
2065 {
2066 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2067 if (real_less (&u, &r))
2068 {
2069 overflow = true;
2070 val = wi::to_wide (ut);
2071 }
2072 }
2073 }
2074
2075 if (! overflow)
2076 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2077
2078 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2079 return t;
2080 }
2081
2082 /* A subroutine of fold_convert_const handling conversions of a
2083 FIXED_CST to an integer type. */
2084
2085 static tree
2086 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2087 {
2088 tree t;
2089 double_int temp, temp_trunc;
2090 scalar_mode mode;
2091
2092 /* Right shift FIXED_CST to temp by fbit. */
2093 temp = TREE_FIXED_CST (arg1).data;
2094 mode = TREE_FIXED_CST (arg1).mode;
2095 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2096 {
2097 temp = temp.rshift (GET_MODE_FBIT (mode),
2098 HOST_BITS_PER_DOUBLE_INT,
2099 SIGNED_FIXED_POINT_MODE_P (mode));
2100
2101 /* Left shift temp to temp_trunc by fbit. */
2102 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2103 HOST_BITS_PER_DOUBLE_INT,
2104 SIGNED_FIXED_POINT_MODE_P (mode));
2105 }
2106 else
2107 {
2108 temp = double_int_zero;
2109 temp_trunc = double_int_zero;
2110 }
2111
2112 /* If FIXED_CST is negative, we need to round the value toward 0.
2113 By checking if the fractional bits are not zero to add 1 to temp. */
2114 if (SIGNED_FIXED_POINT_MODE_P (mode)
2115 && temp_trunc.is_negative ()
2116 && TREE_FIXED_CST (arg1).data != temp_trunc)
2117 temp += double_int_one;
2118
2119 /* Given a fixed-point constant, make new constant with new type,
2120 appropriately sign-extended or truncated. */
2121 t = force_fit_type (type, temp, -1,
2122 (temp.is_negative ()
2123 && (TYPE_UNSIGNED (type)
2124 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2125 | TREE_OVERFLOW (arg1));
2126
2127 return t;
2128 }
2129
2130 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2131 to another floating point type. */
2132
2133 static tree
2134 fold_convert_const_real_from_real (tree type, const_tree arg1)
2135 {
2136 REAL_VALUE_TYPE value;
2137 tree t;
2138
2139 /* Don't perform the operation if flag_signaling_nans is on
2140 and the operand is a signaling NaN. */
2141 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2142 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2143 return NULL_TREE;
2144
2145 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2146 t = build_real (type, value);
2147
2148 /* If converting an infinity or NAN to a representation that doesn't
2149 have one, set the overflow bit so that we can produce some kind of
2150 error message at the appropriate point if necessary. It's not the
2151 most user-friendly message, but it's better than nothing. */
2152 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2153 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2154 TREE_OVERFLOW (t) = 1;
2155 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2156 && !MODE_HAS_NANS (TYPE_MODE (type)))
2157 TREE_OVERFLOW (t) = 1;
2158 /* Regular overflow, conversion produced an infinity in a mode that
2159 can't represent them. */
2160 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2161 && REAL_VALUE_ISINF (value)
2162 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2163 TREE_OVERFLOW (t) = 1;
2164 else
2165 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2166 return t;
2167 }
2168
2169 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2170 to a floating point type. */
2171
2172 static tree
2173 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2174 {
2175 REAL_VALUE_TYPE value;
2176 tree t;
2177
2178 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2179 &TREE_FIXED_CST (arg1));
2180 t = build_real (type, value);
2181
2182 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2183 return t;
2184 }
2185
2186 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2187 to another fixed-point type. */
2188
2189 static tree
2190 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2191 {
2192 FIXED_VALUE_TYPE value;
2193 tree t;
2194 bool overflow_p;
2195
2196 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2197 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2198 t = build_fixed (type, value);
2199
2200 /* Propagate overflow flags. */
2201 if (overflow_p | TREE_OVERFLOW (arg1))
2202 TREE_OVERFLOW (t) = 1;
2203 return t;
2204 }
2205
2206 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2207 to a fixed-point type. */
2208
2209 static tree
2210 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2211 {
2212 FIXED_VALUE_TYPE value;
2213 tree t;
2214 bool overflow_p;
2215 double_int di;
2216
2217 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2218
2219 di.low = TREE_INT_CST_ELT (arg1, 0);
2220 if (TREE_INT_CST_NUNITS (arg1) == 1)
2221 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2222 else
2223 di.high = TREE_INT_CST_ELT (arg1, 1);
2224
2225 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2226 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2227 TYPE_SATURATING (type));
2228 t = build_fixed (type, value);
2229
2230 /* Propagate overflow flags. */
2231 if (overflow_p | TREE_OVERFLOW (arg1))
2232 TREE_OVERFLOW (t) = 1;
2233 return t;
2234 }
2235
2236 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2237 to a fixed-point type. */
2238
2239 static tree
2240 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2241 {
2242 FIXED_VALUE_TYPE value;
2243 tree t;
2244 bool overflow_p;
2245
2246 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2247 &TREE_REAL_CST (arg1),
2248 TYPE_SATURATING (type));
2249 t = build_fixed (type, value);
2250
2251 /* Propagate overflow flags. */
2252 if (overflow_p | TREE_OVERFLOW (arg1))
2253 TREE_OVERFLOW (t) = 1;
2254 return t;
2255 }
2256
2257 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2258 type TYPE. If no simplification can be done return NULL_TREE. */
2259
2260 static tree
2261 fold_convert_const (enum tree_code code, tree type, tree arg1)
2262 {
2263 tree arg_type = TREE_TYPE (arg1);
2264 if (arg_type == type)
2265 return arg1;
2266
2267 /* We can't widen types, since the runtime value could overflow the
2268 original type before being extended to the new type. */
2269 if (POLY_INT_CST_P (arg1)
2270 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2271 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2272 return build_poly_int_cst (type,
2273 poly_wide_int::from (poly_int_cst_value (arg1),
2274 TYPE_PRECISION (type),
2275 TYPE_SIGN (arg_type)));
2276
2277 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2278 || TREE_CODE (type) == OFFSET_TYPE)
2279 {
2280 if (TREE_CODE (arg1) == INTEGER_CST)
2281 return fold_convert_const_int_from_int (type, arg1);
2282 else if (TREE_CODE (arg1) == REAL_CST)
2283 return fold_convert_const_int_from_real (code, type, arg1);
2284 else if (TREE_CODE (arg1) == FIXED_CST)
2285 return fold_convert_const_int_from_fixed (type, arg1);
2286 }
2287 else if (TREE_CODE (type) == REAL_TYPE)
2288 {
2289 if (TREE_CODE (arg1) == INTEGER_CST)
2290 return build_real_from_int_cst (type, arg1);
2291 else if (TREE_CODE (arg1) == REAL_CST)
2292 return fold_convert_const_real_from_real (type, arg1);
2293 else if (TREE_CODE (arg1) == FIXED_CST)
2294 return fold_convert_const_real_from_fixed (type, arg1);
2295 }
2296 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2297 {
2298 if (TREE_CODE (arg1) == FIXED_CST)
2299 return fold_convert_const_fixed_from_fixed (type, arg1);
2300 else if (TREE_CODE (arg1) == INTEGER_CST)
2301 return fold_convert_const_fixed_from_int (type, arg1);
2302 else if (TREE_CODE (arg1) == REAL_CST)
2303 return fold_convert_const_fixed_from_real (type, arg1);
2304 }
2305 else if (TREE_CODE (type) == VECTOR_TYPE)
2306 {
2307 if (TREE_CODE (arg1) == VECTOR_CST
2308 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2309 {
2310 tree elttype = TREE_TYPE (type);
2311 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2312 /* We can't handle steps directly when extending, since the
2313 values need to wrap at the original precision first. */
2314 bool step_ok_p
2315 = (INTEGRAL_TYPE_P (elttype)
2316 && INTEGRAL_TYPE_P (arg1_elttype)
2317 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2318 tree_vector_builder v;
2319 if (!v.new_unary_operation (type, arg1, step_ok_p))
2320 return NULL_TREE;
2321 unsigned int len = v.encoded_nelts ();
2322 for (unsigned int i = 0; i < len; ++i)
2323 {
2324 tree elt = VECTOR_CST_ELT (arg1, i);
2325 tree cvt = fold_convert_const (code, elttype, elt);
2326 if (cvt == NULL_TREE)
2327 return NULL_TREE;
2328 v.quick_push (cvt);
2329 }
2330 return v.build ();
2331 }
2332 }
2333 return NULL_TREE;
2334 }
2335
2336 /* Construct a vector of zero elements of vector type TYPE. */
2337
2338 static tree
2339 build_zero_vector (tree type)
2340 {
2341 tree t;
2342
2343 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2344 return build_vector_from_val (type, t);
2345 }
2346
2347 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2348
2349 bool
2350 fold_convertible_p (const_tree type, const_tree arg)
2351 {
2352 tree orig = TREE_TYPE (arg);
2353
2354 if (type == orig)
2355 return true;
2356
2357 if (TREE_CODE (arg) == ERROR_MARK
2358 || TREE_CODE (type) == ERROR_MARK
2359 || TREE_CODE (orig) == ERROR_MARK)
2360 return false;
2361
2362 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2363 return true;
2364
2365 switch (TREE_CODE (type))
2366 {
2367 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2368 case POINTER_TYPE: case REFERENCE_TYPE:
2369 case OFFSET_TYPE:
2370 return (INTEGRAL_TYPE_P (orig)
2371 || (POINTER_TYPE_P (orig)
2372 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2373 || TREE_CODE (orig) == OFFSET_TYPE);
2374
2375 case REAL_TYPE:
2376 case FIXED_POINT_TYPE:
2377 case VECTOR_TYPE:
2378 case VOID_TYPE:
2379 return TREE_CODE (type) == TREE_CODE (orig);
2380
2381 default:
2382 return false;
2383 }
2384 }
2385
2386 /* Convert expression ARG to type TYPE. Used by the middle-end for
2387 simple conversions in preference to calling the front-end's convert. */
2388
2389 tree
2390 fold_convert_loc (location_t loc, tree type, tree arg)
2391 {
2392 tree orig = TREE_TYPE (arg);
2393 tree tem;
2394
2395 if (type == orig)
2396 return arg;
2397
2398 if (TREE_CODE (arg) == ERROR_MARK
2399 || TREE_CODE (type) == ERROR_MARK
2400 || TREE_CODE (orig) == ERROR_MARK)
2401 return error_mark_node;
2402
2403 switch (TREE_CODE (type))
2404 {
2405 case POINTER_TYPE:
2406 case REFERENCE_TYPE:
2407 /* Handle conversions between pointers to different address spaces. */
2408 if (POINTER_TYPE_P (orig)
2409 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2410 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2411 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2412 /* fall through */
2413
2414 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2415 case OFFSET_TYPE:
2416 if (TREE_CODE (arg) == INTEGER_CST)
2417 {
2418 tem = fold_convert_const (NOP_EXPR, type, arg);
2419 if (tem != NULL_TREE)
2420 return tem;
2421 }
2422 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2423 || TREE_CODE (orig) == OFFSET_TYPE)
2424 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2425 if (TREE_CODE (orig) == COMPLEX_TYPE)
2426 return fold_convert_loc (loc, type,
2427 fold_build1_loc (loc, REALPART_EXPR,
2428 TREE_TYPE (orig), arg));
2429 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2430 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2431 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2432
2433 case REAL_TYPE:
2434 if (TREE_CODE (arg) == INTEGER_CST)
2435 {
2436 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2437 if (tem != NULL_TREE)
2438 return tem;
2439 }
2440 else if (TREE_CODE (arg) == REAL_CST)
2441 {
2442 tem = fold_convert_const (NOP_EXPR, type, arg);
2443 if (tem != NULL_TREE)
2444 return tem;
2445 }
2446 else if (TREE_CODE (arg) == FIXED_CST)
2447 {
2448 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2449 if (tem != NULL_TREE)
2450 return tem;
2451 }
2452
2453 switch (TREE_CODE (orig))
2454 {
2455 case INTEGER_TYPE:
2456 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2457 case POINTER_TYPE: case REFERENCE_TYPE:
2458 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2459
2460 case REAL_TYPE:
2461 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2462
2463 case FIXED_POINT_TYPE:
2464 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2465
2466 case COMPLEX_TYPE:
2467 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2468 return fold_convert_loc (loc, type, tem);
2469
2470 default:
2471 gcc_unreachable ();
2472 }
2473
2474 case FIXED_POINT_TYPE:
2475 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2476 || TREE_CODE (arg) == REAL_CST)
2477 {
2478 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2479 if (tem != NULL_TREE)
2480 goto fold_convert_exit;
2481 }
2482
2483 switch (TREE_CODE (orig))
2484 {
2485 case FIXED_POINT_TYPE:
2486 case INTEGER_TYPE:
2487 case ENUMERAL_TYPE:
2488 case BOOLEAN_TYPE:
2489 case REAL_TYPE:
2490 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2491
2492 case COMPLEX_TYPE:
2493 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2494 return fold_convert_loc (loc, type, tem);
2495
2496 default:
2497 gcc_unreachable ();
2498 }
2499
2500 case COMPLEX_TYPE:
2501 switch (TREE_CODE (orig))
2502 {
2503 case INTEGER_TYPE:
2504 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2505 case POINTER_TYPE: case REFERENCE_TYPE:
2506 case REAL_TYPE:
2507 case FIXED_POINT_TYPE:
2508 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2509 fold_convert_loc (loc, TREE_TYPE (type), arg),
2510 fold_convert_loc (loc, TREE_TYPE (type),
2511 integer_zero_node));
2512 case COMPLEX_TYPE:
2513 {
2514 tree rpart, ipart;
2515
2516 if (TREE_CODE (arg) == COMPLEX_EXPR)
2517 {
2518 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2519 TREE_OPERAND (arg, 0));
2520 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2521 TREE_OPERAND (arg, 1));
2522 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2523 }
2524
2525 arg = save_expr (arg);
2526 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2527 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2528 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2529 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2530 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2531 }
2532
2533 default:
2534 gcc_unreachable ();
2535 }
2536
2537 case VECTOR_TYPE:
2538 if (integer_zerop (arg))
2539 return build_zero_vector (type);
2540 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2541 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2542 || TREE_CODE (orig) == VECTOR_TYPE);
2543 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2544
2545 case VOID_TYPE:
2546 tem = fold_ignored_result (arg);
2547 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2548
2549 default:
2550 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2551 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2552 gcc_unreachable ();
2553 }
2554 fold_convert_exit:
2555 protected_set_expr_location_unshare (tem, loc);
2556 return tem;
2557 }
2558 \f
2559 /* Return false if expr can be assumed not to be an lvalue, true
2560 otherwise. */
2561
2562 static bool
2563 maybe_lvalue_p (const_tree x)
2564 {
2565 /* We only need to wrap lvalue tree codes. */
2566 switch (TREE_CODE (x))
2567 {
2568 case VAR_DECL:
2569 case PARM_DECL:
2570 case RESULT_DECL:
2571 case LABEL_DECL:
2572 case FUNCTION_DECL:
2573 case SSA_NAME:
2574
2575 case COMPONENT_REF:
2576 case MEM_REF:
2577 case INDIRECT_REF:
2578 case ARRAY_REF:
2579 case ARRAY_RANGE_REF:
2580 case BIT_FIELD_REF:
2581 case OBJ_TYPE_REF:
2582
2583 case REALPART_EXPR:
2584 case IMAGPART_EXPR:
2585 case PREINCREMENT_EXPR:
2586 case PREDECREMENT_EXPR:
2587 case SAVE_EXPR:
2588 case TRY_CATCH_EXPR:
2589 case WITH_CLEANUP_EXPR:
2590 case COMPOUND_EXPR:
2591 case MODIFY_EXPR:
2592 case TARGET_EXPR:
2593 case COND_EXPR:
2594 case BIND_EXPR:
2595 break;
2596
2597 default:
2598 /* Assume the worst for front-end tree codes. */
2599 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2600 break;
2601 return false;
2602 }
2603
2604 return true;
2605 }
2606
2607 /* Return an expr equal to X but certainly not valid as an lvalue. */
2608
2609 tree
2610 non_lvalue_loc (location_t loc, tree x)
2611 {
2612 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2613 us. */
2614 if (in_gimple_form)
2615 return x;
2616
2617 if (! maybe_lvalue_p (x))
2618 return x;
2619 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2620 }
2621
2622 /* When pedantic, return an expr equal to X but certainly not valid as a
2623 pedantic lvalue. Otherwise, return X. */
2624
2625 static tree
2626 pedantic_non_lvalue_loc (location_t loc, tree x)
2627 {
2628 return protected_set_expr_location_unshare (x, loc);
2629 }
2630 \f
2631 /* Given a tree comparison code, return the code that is the logical inverse.
2632 It is generally not safe to do this for floating-point comparisons, except
2633 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2634 ERROR_MARK in this case. */
2635
2636 enum tree_code
2637 invert_tree_comparison (enum tree_code code, bool honor_nans)
2638 {
2639 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2640 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2641 return ERROR_MARK;
2642
2643 switch (code)
2644 {
2645 case EQ_EXPR:
2646 return NE_EXPR;
2647 case NE_EXPR:
2648 return EQ_EXPR;
2649 case GT_EXPR:
2650 return honor_nans ? UNLE_EXPR : LE_EXPR;
2651 case GE_EXPR:
2652 return honor_nans ? UNLT_EXPR : LT_EXPR;
2653 case LT_EXPR:
2654 return honor_nans ? UNGE_EXPR : GE_EXPR;
2655 case LE_EXPR:
2656 return honor_nans ? UNGT_EXPR : GT_EXPR;
2657 case LTGT_EXPR:
2658 return UNEQ_EXPR;
2659 case UNEQ_EXPR:
2660 return LTGT_EXPR;
2661 case UNGT_EXPR:
2662 return LE_EXPR;
2663 case UNGE_EXPR:
2664 return LT_EXPR;
2665 case UNLT_EXPR:
2666 return GE_EXPR;
2667 case UNLE_EXPR:
2668 return GT_EXPR;
2669 case ORDERED_EXPR:
2670 return UNORDERED_EXPR;
2671 case UNORDERED_EXPR:
2672 return ORDERED_EXPR;
2673 default:
2674 gcc_unreachable ();
2675 }
2676 }
2677
2678 /* Similar, but return the comparison that results if the operands are
2679 swapped. This is safe for floating-point. */
2680
2681 enum tree_code
2682 swap_tree_comparison (enum tree_code code)
2683 {
2684 switch (code)
2685 {
2686 case EQ_EXPR:
2687 case NE_EXPR:
2688 case ORDERED_EXPR:
2689 case UNORDERED_EXPR:
2690 case LTGT_EXPR:
2691 case UNEQ_EXPR:
2692 return code;
2693 case GT_EXPR:
2694 return LT_EXPR;
2695 case GE_EXPR:
2696 return LE_EXPR;
2697 case LT_EXPR:
2698 return GT_EXPR;
2699 case LE_EXPR:
2700 return GE_EXPR;
2701 case UNGT_EXPR:
2702 return UNLT_EXPR;
2703 case UNGE_EXPR:
2704 return UNLE_EXPR;
2705 case UNLT_EXPR:
2706 return UNGT_EXPR;
2707 case UNLE_EXPR:
2708 return UNGE_EXPR;
2709 default:
2710 gcc_unreachable ();
2711 }
2712 }
2713
2714
2715 /* Convert a comparison tree code from an enum tree_code representation
2716 into a compcode bit-based encoding. This function is the inverse of
2717 compcode_to_comparison. */
2718
2719 static enum comparison_code
2720 comparison_to_compcode (enum tree_code code)
2721 {
2722 switch (code)
2723 {
2724 case LT_EXPR:
2725 return COMPCODE_LT;
2726 case EQ_EXPR:
2727 return COMPCODE_EQ;
2728 case LE_EXPR:
2729 return COMPCODE_LE;
2730 case GT_EXPR:
2731 return COMPCODE_GT;
2732 case NE_EXPR:
2733 return COMPCODE_NE;
2734 case GE_EXPR:
2735 return COMPCODE_GE;
2736 case ORDERED_EXPR:
2737 return COMPCODE_ORD;
2738 case UNORDERED_EXPR:
2739 return COMPCODE_UNORD;
2740 case UNLT_EXPR:
2741 return COMPCODE_UNLT;
2742 case UNEQ_EXPR:
2743 return COMPCODE_UNEQ;
2744 case UNLE_EXPR:
2745 return COMPCODE_UNLE;
2746 case UNGT_EXPR:
2747 return COMPCODE_UNGT;
2748 case LTGT_EXPR:
2749 return COMPCODE_LTGT;
2750 case UNGE_EXPR:
2751 return COMPCODE_UNGE;
2752 default:
2753 gcc_unreachable ();
2754 }
2755 }
2756
2757 /* Convert a compcode bit-based encoding of a comparison operator back
2758 to GCC's enum tree_code representation. This function is the
2759 inverse of comparison_to_compcode. */
2760
2761 static enum tree_code
2762 compcode_to_comparison (enum comparison_code code)
2763 {
2764 switch (code)
2765 {
2766 case COMPCODE_LT:
2767 return LT_EXPR;
2768 case COMPCODE_EQ:
2769 return EQ_EXPR;
2770 case COMPCODE_LE:
2771 return LE_EXPR;
2772 case COMPCODE_GT:
2773 return GT_EXPR;
2774 case COMPCODE_NE:
2775 return NE_EXPR;
2776 case COMPCODE_GE:
2777 return GE_EXPR;
2778 case COMPCODE_ORD:
2779 return ORDERED_EXPR;
2780 case COMPCODE_UNORD:
2781 return UNORDERED_EXPR;
2782 case COMPCODE_UNLT:
2783 return UNLT_EXPR;
2784 case COMPCODE_UNEQ:
2785 return UNEQ_EXPR;
2786 case COMPCODE_UNLE:
2787 return UNLE_EXPR;
2788 case COMPCODE_UNGT:
2789 return UNGT_EXPR;
2790 case COMPCODE_LTGT:
2791 return LTGT_EXPR;
2792 case COMPCODE_UNGE:
2793 return UNGE_EXPR;
2794 default:
2795 gcc_unreachable ();
2796 }
2797 }
2798
2799 /* Return true if COND1 tests the opposite condition of COND2. */
2800
2801 bool
2802 inverse_conditions_p (const_tree cond1, const_tree cond2)
2803 {
2804 return (COMPARISON_CLASS_P (cond1)
2805 && COMPARISON_CLASS_P (cond2)
2806 && (invert_tree_comparison
2807 (TREE_CODE (cond1),
2808 HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2809 && operand_equal_p (TREE_OPERAND (cond1, 0),
2810 TREE_OPERAND (cond2, 0), 0)
2811 && operand_equal_p (TREE_OPERAND (cond1, 1),
2812 TREE_OPERAND (cond2, 1), 0));
2813 }
2814
2815 /* Return a tree for the comparison which is the combination of
2816 doing the AND or OR (depending on CODE) of the two operations LCODE
2817 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2818 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2819 if this makes the transformation invalid. */
2820
2821 tree
2822 combine_comparisons (location_t loc,
2823 enum tree_code code, enum tree_code lcode,
2824 enum tree_code rcode, tree truth_type,
2825 tree ll_arg, tree lr_arg)
2826 {
2827 bool honor_nans = HONOR_NANS (ll_arg);
2828 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2829 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2830 int compcode;
2831
2832 switch (code)
2833 {
2834 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2835 compcode = lcompcode & rcompcode;
2836 break;
2837
2838 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2839 compcode = lcompcode | rcompcode;
2840 break;
2841
2842 default:
2843 return NULL_TREE;
2844 }
2845
2846 if (!honor_nans)
2847 {
2848 /* Eliminate unordered comparisons, as well as LTGT and ORD
2849 which are not used unless the mode has NaNs. */
2850 compcode &= ~COMPCODE_UNORD;
2851 if (compcode == COMPCODE_LTGT)
2852 compcode = COMPCODE_NE;
2853 else if (compcode == COMPCODE_ORD)
2854 compcode = COMPCODE_TRUE;
2855 }
2856 else if (flag_trapping_math)
2857 {
2858 /* Check that the original operation and the optimized ones will trap
2859 under the same condition. */
2860 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2861 && (lcompcode != COMPCODE_EQ)
2862 && (lcompcode != COMPCODE_ORD);
2863 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2864 && (rcompcode != COMPCODE_EQ)
2865 && (rcompcode != COMPCODE_ORD);
2866 bool trap = (compcode & COMPCODE_UNORD) == 0
2867 && (compcode != COMPCODE_EQ)
2868 && (compcode != COMPCODE_ORD);
2869
2870 /* In a short-circuited boolean expression the LHS might be
2871 such that the RHS, if evaluated, will never trap. For
2872 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2873 if neither x nor y is NaN. (This is a mixed blessing: for
2874 example, the expression above will never trap, hence
2875 optimizing it to x < y would be invalid). */
2876 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2877 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2878 rtrap = false;
2879
2880 /* If the comparison was short-circuited, and only the RHS
2881 trapped, we may now generate a spurious trap. */
2882 if (rtrap && !ltrap
2883 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2884 return NULL_TREE;
2885
2886 /* If we changed the conditions that cause a trap, we lose. */
2887 if ((ltrap || rtrap) != trap)
2888 return NULL_TREE;
2889 }
2890
2891 if (compcode == COMPCODE_TRUE)
2892 return constant_boolean_node (true, truth_type);
2893 else if (compcode == COMPCODE_FALSE)
2894 return constant_boolean_node (false, truth_type);
2895 else
2896 {
2897 enum tree_code tcode;
2898
2899 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2900 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2901 }
2902 }
2903 \f
2904 /* Return nonzero if two operands (typically of the same tree node)
2905 are necessarily equal. FLAGS modifies behavior as follows:
2906
2907 If OEP_ONLY_CONST is set, only return nonzero for constants.
2908 This function tests whether the operands are indistinguishable;
2909 it does not test whether they are equal using C's == operation.
2910 The distinction is important for IEEE floating point, because
2911 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2912 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2913
2914 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2915 even though it may hold multiple values during a function.
2916 This is because a GCC tree node guarantees that nothing else is
2917 executed between the evaluation of its "operands" (which may often
2918 be evaluated in arbitrary order). Hence if the operands themselves
2919 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2920 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2921 unset means assuming isochronic (or instantaneous) tree equivalence.
2922 Unless comparing arbitrary expression trees, such as from different
2923 statements, this flag can usually be left unset.
2924
2925 If OEP_PURE_SAME is set, then pure functions with identical arguments
2926 are considered the same. It is used when the caller has other ways
2927 to ensure that global memory is unchanged in between.
2928
2929 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2930 not values of expressions.
2931
2932 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2933 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2934
2935 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2936 any operand with side effect. This is unnecesarily conservative in the
2937 case we know that arg0 and arg1 are in disjoint code paths (such as in
2938 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2939 addresses with TREE_CONSTANT flag set so we know that &var == &var
2940 even if var is volatile. */
2941
2942 int
2943 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2944 {
2945 STRIP_ANY_LOCATION_WRAPPER (arg0);
2946 STRIP_ANY_LOCATION_WRAPPER (arg1);
2947
2948 /* When checking, verify at the outermost operand_equal_p call that
2949 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2950 hash value. */
2951 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2952 {
2953 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2954 {
2955 if (arg0 != arg1)
2956 {
2957 inchash::hash hstate0 (0), hstate1 (0);
2958 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2959 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2960 hashval_t h0 = hstate0.end ();
2961 hashval_t h1 = hstate1.end ();
2962 gcc_assert (h0 == h1);
2963 }
2964 return 1;
2965 }
2966 else
2967 return 0;
2968 }
2969
2970 /* If either is ERROR_MARK, they aren't equal. */
2971 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2972 || TREE_TYPE (arg0) == error_mark_node
2973 || TREE_TYPE (arg1) == error_mark_node)
2974 return 0;
2975
2976 /* Similar, if either does not have a type (like a released SSA name),
2977 they aren't equal. */
2978 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2979 return 0;
2980
2981 /* We cannot consider pointers to different address space equal. */
2982 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2983 && POINTER_TYPE_P (TREE_TYPE (arg1))
2984 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2985 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2986 return 0;
2987
2988 /* Check equality of integer constants before bailing out due to
2989 precision differences. */
2990 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2991 {
2992 /* Address of INTEGER_CST is not defined; check that we did not forget
2993 to drop the OEP_ADDRESS_OF flags. */
2994 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2995 return tree_int_cst_equal (arg0, arg1);
2996 }
2997
2998 if (!(flags & OEP_ADDRESS_OF))
2999 {
3000 /* If both types don't have the same signedness, then we can't consider
3001 them equal. We must check this before the STRIP_NOPS calls
3002 because they may change the signedness of the arguments. As pointers
3003 strictly don't have a signedness, require either two pointers or
3004 two non-pointers as well. */
3005 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3006 || POINTER_TYPE_P (TREE_TYPE (arg0))
3007 != POINTER_TYPE_P (TREE_TYPE (arg1)))
3008 return 0;
3009
3010 /* If both types don't have the same precision, then it is not safe
3011 to strip NOPs. */
3012 if (element_precision (TREE_TYPE (arg0))
3013 != element_precision (TREE_TYPE (arg1)))
3014 return 0;
3015
3016 STRIP_NOPS (arg0);
3017 STRIP_NOPS (arg1);
3018 }
3019 #if 0
3020 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3021 sanity check once the issue is solved. */
3022 else
3023 /* Addresses of conversions and SSA_NAMEs (and many other things)
3024 are not defined. Check that we did not forget to drop the
3025 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3026 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3027 && TREE_CODE (arg0) != SSA_NAME);
3028 #endif
3029
3030 /* In case both args are comparisons but with different comparison
3031 code, try to swap the comparison operands of one arg to produce
3032 a match and compare that variant. */
3033 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3034 && COMPARISON_CLASS_P (arg0)
3035 && COMPARISON_CLASS_P (arg1))
3036 {
3037 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3038
3039 if (TREE_CODE (arg0) == swap_code)
3040 return operand_equal_p (TREE_OPERAND (arg0, 0),
3041 TREE_OPERAND (arg1, 1), flags)
3042 && operand_equal_p (TREE_OPERAND (arg0, 1),
3043 TREE_OPERAND (arg1, 0), flags);
3044 }
3045
3046 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3047 {
3048 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3049 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3050 ;
3051 else if (flags & OEP_ADDRESS_OF)
3052 {
3053 /* If we are interested in comparing addresses ignore
3054 MEM_REF wrappings of the base that can appear just for
3055 TBAA reasons. */
3056 if (TREE_CODE (arg0) == MEM_REF
3057 && DECL_P (arg1)
3058 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3059 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3060 && integer_zerop (TREE_OPERAND (arg0, 1)))
3061 return 1;
3062 else if (TREE_CODE (arg1) == MEM_REF
3063 && DECL_P (arg0)
3064 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3065 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3066 && integer_zerop (TREE_OPERAND (arg1, 1)))
3067 return 1;
3068 return 0;
3069 }
3070 else
3071 return 0;
3072 }
3073
3074 /* When not checking adddresses, this is needed for conversions and for
3075 COMPONENT_REF. Might as well play it safe and always test this. */
3076 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3077 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3078 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3079 && !(flags & OEP_ADDRESS_OF)))
3080 return 0;
3081
3082 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3083 We don't care about side effects in that case because the SAVE_EXPR
3084 takes care of that for us. In all other cases, two expressions are
3085 equal if they have no side effects. If we have two identical
3086 expressions with side effects that should be treated the same due
3087 to the only side effects being identical SAVE_EXPR's, that will
3088 be detected in the recursive calls below.
3089 If we are taking an invariant address of two identical objects
3090 they are necessarily equal as well. */
3091 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3092 && (TREE_CODE (arg0) == SAVE_EXPR
3093 || (flags & OEP_MATCH_SIDE_EFFECTS)
3094 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3095 return 1;
3096
3097 /* Next handle constant cases, those for which we can return 1 even
3098 if ONLY_CONST is set. */
3099 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3100 switch (TREE_CODE (arg0))
3101 {
3102 case INTEGER_CST:
3103 return tree_int_cst_equal (arg0, arg1);
3104
3105 case FIXED_CST:
3106 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3107 TREE_FIXED_CST (arg1));
3108
3109 case REAL_CST:
3110 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3111 return 1;
3112
3113
3114 if (!HONOR_SIGNED_ZEROS (arg0))
3115 {
3116 /* If we do not distinguish between signed and unsigned zero,
3117 consider them equal. */
3118 if (real_zerop (arg0) && real_zerop (arg1))
3119 return 1;
3120 }
3121 return 0;
3122
3123 case VECTOR_CST:
3124 {
3125 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3126 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3127 return 0;
3128
3129 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3130 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3131 return 0;
3132
3133 unsigned int count = vector_cst_encoded_nelts (arg0);
3134 for (unsigned int i = 0; i < count; ++i)
3135 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3136 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3137 return 0;
3138 return 1;
3139 }
3140
3141 case COMPLEX_CST:
3142 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3143 flags)
3144 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3145 flags));
3146
3147 case STRING_CST:
3148 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3149 && ! memcmp (TREE_STRING_POINTER (arg0),
3150 TREE_STRING_POINTER (arg1),
3151 TREE_STRING_LENGTH (arg0)));
3152
3153 case ADDR_EXPR:
3154 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3155 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3156 flags | OEP_ADDRESS_OF
3157 | OEP_MATCH_SIDE_EFFECTS);
3158 case CONSTRUCTOR:
3159 /* In GIMPLE empty constructors are allowed in initializers of
3160 aggregates. */
3161 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3162 default:
3163 break;
3164 }
3165
3166 if (flags & OEP_ONLY_CONST)
3167 return 0;
3168
3169 /* Define macros to test an operand from arg0 and arg1 for equality and a
3170 variant that allows null and views null as being different from any
3171 non-null value. In the latter case, if either is null, the both
3172 must be; otherwise, do the normal comparison. */
3173 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3174 TREE_OPERAND (arg1, N), flags)
3175
3176 #define OP_SAME_WITH_NULL(N) \
3177 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3178 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3179
3180 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3181 {
3182 case tcc_unary:
3183 /* Two conversions are equal only if signedness and modes match. */
3184 switch (TREE_CODE (arg0))
3185 {
3186 CASE_CONVERT:
3187 case FIX_TRUNC_EXPR:
3188 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3189 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3190 return 0;
3191 break;
3192 default:
3193 break;
3194 }
3195
3196 return OP_SAME (0);
3197
3198
3199 case tcc_comparison:
3200 case tcc_binary:
3201 if (OP_SAME (0) && OP_SAME (1))
3202 return 1;
3203
3204 /* For commutative ops, allow the other order. */
3205 return (commutative_tree_code (TREE_CODE (arg0))
3206 && operand_equal_p (TREE_OPERAND (arg0, 0),
3207 TREE_OPERAND (arg1, 1), flags)
3208 && operand_equal_p (TREE_OPERAND (arg0, 1),
3209 TREE_OPERAND (arg1, 0), flags));
3210
3211 case tcc_reference:
3212 /* If either of the pointer (or reference) expressions we are
3213 dereferencing contain a side effect, these cannot be equal,
3214 but their addresses can be. */
3215 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3216 && (TREE_SIDE_EFFECTS (arg0)
3217 || TREE_SIDE_EFFECTS (arg1)))
3218 return 0;
3219
3220 switch (TREE_CODE (arg0))
3221 {
3222 case INDIRECT_REF:
3223 if (!(flags & OEP_ADDRESS_OF)
3224 && (TYPE_ALIGN (TREE_TYPE (arg0))
3225 != TYPE_ALIGN (TREE_TYPE (arg1))))
3226 return 0;
3227 flags &= ~OEP_ADDRESS_OF;
3228 return OP_SAME (0);
3229
3230 case IMAGPART_EXPR:
3231 /* Require the same offset. */
3232 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3233 TYPE_SIZE (TREE_TYPE (arg1)),
3234 flags & ~OEP_ADDRESS_OF))
3235 return 0;
3236
3237 /* Fallthru. */
3238 case REALPART_EXPR:
3239 case VIEW_CONVERT_EXPR:
3240 return OP_SAME (0);
3241
3242 case TARGET_MEM_REF:
3243 case MEM_REF:
3244 if (!(flags & OEP_ADDRESS_OF))
3245 {
3246 /* Require equal access sizes */
3247 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3248 && (!TYPE_SIZE (TREE_TYPE (arg0))
3249 || !TYPE_SIZE (TREE_TYPE (arg1))
3250 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3251 TYPE_SIZE (TREE_TYPE (arg1)),
3252 flags)))
3253 return 0;
3254 /* Verify that access happens in similar types. */
3255 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3256 return 0;
3257 /* Verify that accesses are TBAA compatible. */
3258 if (!alias_ptr_types_compatible_p
3259 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3260 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3261 || (MR_DEPENDENCE_CLIQUE (arg0)
3262 != MR_DEPENDENCE_CLIQUE (arg1))
3263 || (MR_DEPENDENCE_BASE (arg0)
3264 != MR_DEPENDENCE_BASE (arg1)))
3265 return 0;
3266 /* Verify that alignment is compatible. */
3267 if (TYPE_ALIGN (TREE_TYPE (arg0))
3268 != TYPE_ALIGN (TREE_TYPE (arg1)))
3269 return 0;
3270 }
3271 flags &= ~OEP_ADDRESS_OF;
3272 return (OP_SAME (0) && OP_SAME (1)
3273 /* TARGET_MEM_REF require equal extra operands. */
3274 && (TREE_CODE (arg0) != TARGET_MEM_REF
3275 || (OP_SAME_WITH_NULL (2)
3276 && OP_SAME_WITH_NULL (3)
3277 && OP_SAME_WITH_NULL (4))));
3278
3279 case ARRAY_REF:
3280 case ARRAY_RANGE_REF:
3281 if (!OP_SAME (0))
3282 return 0;
3283 flags &= ~OEP_ADDRESS_OF;
3284 /* Compare the array index by value if it is constant first as we
3285 may have different types but same value here. */
3286 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3287 TREE_OPERAND (arg1, 1))
3288 || OP_SAME (1))
3289 && OP_SAME_WITH_NULL (2)
3290 && OP_SAME_WITH_NULL (3)
3291 /* Compare low bound and element size as with OEP_ADDRESS_OF
3292 we have to account for the offset of the ref. */
3293 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3294 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3295 || (operand_equal_p (array_ref_low_bound
3296 (CONST_CAST_TREE (arg0)),
3297 array_ref_low_bound
3298 (CONST_CAST_TREE (arg1)), flags)
3299 && operand_equal_p (array_ref_element_size
3300 (CONST_CAST_TREE (arg0)),
3301 array_ref_element_size
3302 (CONST_CAST_TREE (arg1)),
3303 flags))));
3304
3305 case COMPONENT_REF:
3306 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3307 may be NULL when we're called to compare MEM_EXPRs. */
3308 if (!OP_SAME_WITH_NULL (0)
3309 || !OP_SAME (1))
3310 return 0;
3311 flags &= ~OEP_ADDRESS_OF;
3312 return OP_SAME_WITH_NULL (2);
3313
3314 case BIT_FIELD_REF:
3315 if (!OP_SAME (0))
3316 return 0;
3317 flags &= ~OEP_ADDRESS_OF;
3318 return OP_SAME (1) && OP_SAME (2);
3319
3320 default:
3321 return 0;
3322 }
3323
3324 case tcc_expression:
3325 switch (TREE_CODE (arg0))
3326 {
3327 case ADDR_EXPR:
3328 /* Be sure we pass right ADDRESS_OF flag. */
3329 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3330 return operand_equal_p (TREE_OPERAND (arg0, 0),
3331 TREE_OPERAND (arg1, 0),
3332 flags | OEP_ADDRESS_OF);
3333
3334 case TRUTH_NOT_EXPR:
3335 return OP_SAME (0);
3336
3337 case TRUTH_ANDIF_EXPR:
3338 case TRUTH_ORIF_EXPR:
3339 return OP_SAME (0) && OP_SAME (1);
3340
3341 case WIDEN_MULT_PLUS_EXPR:
3342 case WIDEN_MULT_MINUS_EXPR:
3343 if (!OP_SAME (2))
3344 return 0;
3345 /* The multiplcation operands are commutative. */
3346 /* FALLTHRU */
3347
3348 case TRUTH_AND_EXPR:
3349 case TRUTH_OR_EXPR:
3350 case TRUTH_XOR_EXPR:
3351 if (OP_SAME (0) && OP_SAME (1))
3352 return 1;
3353
3354 /* Otherwise take into account this is a commutative operation. */
3355 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3356 TREE_OPERAND (arg1, 1), flags)
3357 && operand_equal_p (TREE_OPERAND (arg0, 1),
3358 TREE_OPERAND (arg1, 0), flags));
3359
3360 case COND_EXPR:
3361 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3362 return 0;
3363 flags &= ~OEP_ADDRESS_OF;
3364 return OP_SAME (0);
3365
3366 case BIT_INSERT_EXPR:
3367 /* BIT_INSERT_EXPR has an implict operand as the type precision
3368 of op1. Need to check to make sure they are the same. */
3369 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3370 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3371 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3372 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3373 return false;
3374 /* FALLTHRU */
3375
3376 case VEC_COND_EXPR:
3377 case DOT_PROD_EXPR:
3378 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3379
3380 case MODIFY_EXPR:
3381 case INIT_EXPR:
3382 case COMPOUND_EXPR:
3383 case PREDECREMENT_EXPR:
3384 case PREINCREMENT_EXPR:
3385 case POSTDECREMENT_EXPR:
3386 case POSTINCREMENT_EXPR:
3387 if (flags & OEP_LEXICOGRAPHIC)
3388 return OP_SAME (0) && OP_SAME (1);
3389 return 0;
3390
3391 case CLEANUP_POINT_EXPR:
3392 case EXPR_STMT:
3393 case SAVE_EXPR:
3394 if (flags & OEP_LEXICOGRAPHIC)
3395 return OP_SAME (0);
3396 return 0;
3397
3398 default:
3399 return 0;
3400 }
3401
3402 case tcc_vl_exp:
3403 switch (TREE_CODE (arg0))
3404 {
3405 case CALL_EXPR:
3406 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3407 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3408 /* If not both CALL_EXPRs are either internal or normal function
3409 functions, then they are not equal. */
3410 return 0;
3411 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3412 {
3413 /* If the CALL_EXPRs call different internal functions, then they
3414 are not equal. */
3415 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3416 return 0;
3417 }
3418 else
3419 {
3420 /* If the CALL_EXPRs call different functions, then they are not
3421 equal. */
3422 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3423 flags))
3424 return 0;
3425 }
3426
3427 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3428 {
3429 unsigned int cef = call_expr_flags (arg0);
3430 if (flags & OEP_PURE_SAME)
3431 cef &= ECF_CONST | ECF_PURE;
3432 else
3433 cef &= ECF_CONST;
3434 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3435 return 0;
3436 }
3437
3438 /* Now see if all the arguments are the same. */
3439 {
3440 const_call_expr_arg_iterator iter0, iter1;
3441 const_tree a0, a1;
3442 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3443 a1 = first_const_call_expr_arg (arg1, &iter1);
3444 a0 && a1;
3445 a0 = next_const_call_expr_arg (&iter0),
3446 a1 = next_const_call_expr_arg (&iter1))
3447 if (! operand_equal_p (a0, a1, flags))
3448 return 0;
3449
3450 /* If we get here and both argument lists are exhausted
3451 then the CALL_EXPRs are equal. */
3452 return ! (a0 || a1);
3453 }
3454 default:
3455 return 0;
3456 }
3457
3458 case tcc_declaration:
3459 /* Consider __builtin_sqrt equal to sqrt. */
3460 return (TREE_CODE (arg0) == FUNCTION_DECL
3461 && fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3462 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3463 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3464
3465 case tcc_exceptional:
3466 if (TREE_CODE (arg0) == CONSTRUCTOR)
3467 {
3468 /* In GIMPLE constructors are used only to build vectors from
3469 elements. Individual elements in the constructor must be
3470 indexed in increasing order and form an initial sequence.
3471
3472 We make no effort to compare constructors in generic.
3473 (see sem_variable::equals in ipa-icf which can do so for
3474 constants). */
3475 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3476 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3477 return 0;
3478
3479 /* Be sure that vectors constructed have the same representation.
3480 We only tested element precision and modes to match.
3481 Vectors may be BLKmode and thus also check that the number of
3482 parts match. */
3483 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3484 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3485 return 0;
3486
3487 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3488 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3489 unsigned int len = vec_safe_length (v0);
3490
3491 if (len != vec_safe_length (v1))
3492 return 0;
3493
3494 for (unsigned int i = 0; i < len; i++)
3495 {
3496 constructor_elt *c0 = &(*v0)[i];
3497 constructor_elt *c1 = &(*v1)[i];
3498
3499 if (!operand_equal_p (c0->value, c1->value, flags)
3500 /* In GIMPLE the indexes can be either NULL or matching i.
3501 Double check this so we won't get false
3502 positives for GENERIC. */
3503 || (c0->index
3504 && (TREE_CODE (c0->index) != INTEGER_CST
3505 || !compare_tree_int (c0->index, i)))
3506 || (c1->index
3507 && (TREE_CODE (c1->index) != INTEGER_CST
3508 || !compare_tree_int (c1->index, i))))
3509 return 0;
3510 }
3511 return 1;
3512 }
3513 else if (TREE_CODE (arg0) == STATEMENT_LIST
3514 && (flags & OEP_LEXICOGRAPHIC))
3515 {
3516 /* Compare the STATEMENT_LISTs. */
3517 tree_stmt_iterator tsi1, tsi2;
3518 tree body1 = CONST_CAST_TREE (arg0);
3519 tree body2 = CONST_CAST_TREE (arg1);
3520 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3521 tsi_next (&tsi1), tsi_next (&tsi2))
3522 {
3523 /* The lists don't have the same number of statements. */
3524 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3525 return 0;
3526 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3527 return 1;
3528 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3529 flags & (OEP_LEXICOGRAPHIC
3530 | OEP_NO_HASH_CHECK)))
3531 return 0;
3532 }
3533 }
3534 return 0;
3535
3536 case tcc_statement:
3537 switch (TREE_CODE (arg0))
3538 {
3539 case RETURN_EXPR:
3540 if (flags & OEP_LEXICOGRAPHIC)
3541 return OP_SAME_WITH_NULL (0);
3542 return 0;
3543 case DEBUG_BEGIN_STMT:
3544 if (flags & OEP_LEXICOGRAPHIC)
3545 return 1;
3546 return 0;
3547 default:
3548 return 0;
3549 }
3550
3551 default:
3552 return 0;
3553 }
3554
3555 #undef OP_SAME
3556 #undef OP_SAME_WITH_NULL
3557 }
3558 \f
3559 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3560 with a different signedness or a narrower precision. */
3561
3562 static bool
3563 operand_equal_for_comparison_p (tree arg0, tree arg1)
3564 {
3565 if (operand_equal_p (arg0, arg1, 0))
3566 return true;
3567
3568 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3569 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3570 return false;
3571
3572 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3573 and see if the inner values are the same. This removes any
3574 signedness comparison, which doesn't matter here. */
3575 tree op0 = arg0;
3576 tree op1 = arg1;
3577 STRIP_NOPS (op0);
3578 STRIP_NOPS (op1);
3579 if (operand_equal_p (op0, op1, 0))
3580 return true;
3581
3582 /* Discard a single widening conversion from ARG1 and see if the inner
3583 value is the same as ARG0. */
3584 if (CONVERT_EXPR_P (arg1)
3585 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3586 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3587 < TYPE_PRECISION (TREE_TYPE (arg1))
3588 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3589 return true;
3590
3591 return false;
3592 }
3593 \f
3594 /* See if ARG is an expression that is either a comparison or is performing
3595 arithmetic on comparisons. The comparisons must only be comparing
3596 two different values, which will be stored in *CVAL1 and *CVAL2; if
3597 they are nonzero it means that some operands have already been found.
3598 No variables may be used anywhere else in the expression except in the
3599 comparisons.
3600
3601 If this is true, return 1. Otherwise, return zero. */
3602
3603 static int
3604 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3605 {
3606 enum tree_code code = TREE_CODE (arg);
3607 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3608
3609 /* We can handle some of the tcc_expression cases here. */
3610 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3611 tclass = tcc_unary;
3612 else if (tclass == tcc_expression
3613 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3614 || code == COMPOUND_EXPR))
3615 tclass = tcc_binary;
3616
3617 switch (tclass)
3618 {
3619 case tcc_unary:
3620 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3621
3622 case tcc_binary:
3623 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3624 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3625
3626 case tcc_constant:
3627 return 1;
3628
3629 case tcc_expression:
3630 if (code == COND_EXPR)
3631 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3632 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3633 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3634 return 0;
3635
3636 case tcc_comparison:
3637 /* First see if we can handle the first operand, then the second. For
3638 the second operand, we know *CVAL1 can't be zero. It must be that
3639 one side of the comparison is each of the values; test for the
3640 case where this isn't true by failing if the two operands
3641 are the same. */
3642
3643 if (operand_equal_p (TREE_OPERAND (arg, 0),
3644 TREE_OPERAND (arg, 1), 0))
3645 return 0;
3646
3647 if (*cval1 == 0)
3648 *cval1 = TREE_OPERAND (arg, 0);
3649 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3650 ;
3651 else if (*cval2 == 0)
3652 *cval2 = TREE_OPERAND (arg, 0);
3653 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3654 ;
3655 else
3656 return 0;
3657
3658 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3659 ;
3660 else if (*cval2 == 0)
3661 *cval2 = TREE_OPERAND (arg, 1);
3662 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3663 ;
3664 else
3665 return 0;
3666
3667 return 1;
3668
3669 default:
3670 return 0;
3671 }
3672 }
3673 \f
3674 /* ARG is a tree that is known to contain just arithmetic operations and
3675 comparisons. Evaluate the operations in the tree substituting NEW0 for
3676 any occurrence of OLD0 as an operand of a comparison and likewise for
3677 NEW1 and OLD1. */
3678
3679 static tree
3680 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3681 tree old1, tree new1)
3682 {
3683 tree type = TREE_TYPE (arg);
3684 enum tree_code code = TREE_CODE (arg);
3685 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3686
3687 /* We can handle some of the tcc_expression cases here. */
3688 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3689 tclass = tcc_unary;
3690 else if (tclass == tcc_expression
3691 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3692 tclass = tcc_binary;
3693
3694 switch (tclass)
3695 {
3696 case tcc_unary:
3697 return fold_build1_loc (loc, code, type,
3698 eval_subst (loc, TREE_OPERAND (arg, 0),
3699 old0, new0, old1, new1));
3700
3701 case tcc_binary:
3702 return fold_build2_loc (loc, code, type,
3703 eval_subst (loc, TREE_OPERAND (arg, 0),
3704 old0, new0, old1, new1),
3705 eval_subst (loc, TREE_OPERAND (arg, 1),
3706 old0, new0, old1, new1));
3707
3708 case tcc_expression:
3709 switch (code)
3710 {
3711 case SAVE_EXPR:
3712 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3713 old1, new1);
3714
3715 case COMPOUND_EXPR:
3716 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3717 old1, new1);
3718
3719 case COND_EXPR:
3720 return fold_build3_loc (loc, code, type,
3721 eval_subst (loc, TREE_OPERAND (arg, 0),
3722 old0, new0, old1, new1),
3723 eval_subst (loc, TREE_OPERAND (arg, 1),
3724 old0, new0, old1, new1),
3725 eval_subst (loc, TREE_OPERAND (arg, 2),
3726 old0, new0, old1, new1));
3727 default:
3728 break;
3729 }
3730 /* Fall through - ??? */
3731
3732 case tcc_comparison:
3733 {
3734 tree arg0 = TREE_OPERAND (arg, 0);
3735 tree arg1 = TREE_OPERAND (arg, 1);
3736
3737 /* We need to check both for exact equality and tree equality. The
3738 former will be true if the operand has a side-effect. In that
3739 case, we know the operand occurred exactly once. */
3740
3741 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3742 arg0 = new0;
3743 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3744 arg0 = new1;
3745
3746 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3747 arg1 = new0;
3748 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3749 arg1 = new1;
3750
3751 return fold_build2_loc (loc, code, type, arg0, arg1);
3752 }
3753
3754 default:
3755 return arg;
3756 }
3757 }
3758 \f
3759 /* Return a tree for the case when the result of an expression is RESULT
3760 converted to TYPE and OMITTED was previously an operand of the expression
3761 but is now not needed (e.g., we folded OMITTED * 0).
3762
3763 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3764 the conversion of RESULT to TYPE. */
3765
3766 tree
3767 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3768 {
3769 tree t = fold_convert_loc (loc, type, result);
3770
3771 /* If the resulting operand is an empty statement, just return the omitted
3772 statement casted to void. */
3773 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3774 return build1_loc (loc, NOP_EXPR, void_type_node,
3775 fold_ignored_result (omitted));
3776
3777 if (TREE_SIDE_EFFECTS (omitted))
3778 return build2_loc (loc, COMPOUND_EXPR, type,
3779 fold_ignored_result (omitted), t);
3780
3781 return non_lvalue_loc (loc, t);
3782 }
3783
3784 /* Return a tree for the case when the result of an expression is RESULT
3785 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3786 of the expression but are now not needed.
3787
3788 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3789 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3790 evaluated before OMITTED2. Otherwise, if neither has side effects,
3791 just do the conversion of RESULT to TYPE. */
3792
3793 tree
3794 omit_two_operands_loc (location_t loc, tree type, tree result,
3795 tree omitted1, tree omitted2)
3796 {
3797 tree t = fold_convert_loc (loc, type, result);
3798
3799 if (TREE_SIDE_EFFECTS (omitted2))
3800 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3801 if (TREE_SIDE_EFFECTS (omitted1))
3802 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3803
3804 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3805 }
3806
3807 \f
3808 /* Return a simplified tree node for the truth-negation of ARG. This
3809 never alters ARG itself. We assume that ARG is an operation that
3810 returns a truth value (0 or 1).
3811
3812 FIXME: one would think we would fold the result, but it causes
3813 problems with the dominator optimizer. */
3814
3815 static tree
3816 fold_truth_not_expr (location_t loc, tree arg)
3817 {
3818 tree type = TREE_TYPE (arg);
3819 enum tree_code code = TREE_CODE (arg);
3820 location_t loc1, loc2;
3821
3822 /* If this is a comparison, we can simply invert it, except for
3823 floating-point non-equality comparisons, in which case we just
3824 enclose a TRUTH_NOT_EXPR around what we have. */
3825
3826 if (TREE_CODE_CLASS (code) == tcc_comparison)
3827 {
3828 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3829 if (FLOAT_TYPE_P (op_type)
3830 && flag_trapping_math
3831 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3832 && code != NE_EXPR && code != EQ_EXPR)
3833 return NULL_TREE;
3834
3835 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3836 if (code == ERROR_MARK)
3837 return NULL_TREE;
3838
3839 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3840 TREE_OPERAND (arg, 1));
3841 if (TREE_NO_WARNING (arg))
3842 TREE_NO_WARNING (ret) = 1;
3843 return ret;
3844 }
3845
3846 switch (code)
3847 {
3848 case INTEGER_CST:
3849 return constant_boolean_node (integer_zerop (arg), type);
3850
3851 case TRUTH_AND_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_OR_EXPR, type,
3855 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3856 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3857
3858 case TRUTH_OR_EXPR:
3859 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3860 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3861 return build2_loc (loc, TRUTH_AND_EXPR, type,
3862 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3863 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3864
3865 case TRUTH_XOR_EXPR:
3866 /* Here we can invert either operand. We invert the first operand
3867 unless the second operand is a TRUTH_NOT_EXPR in which case our
3868 result is the XOR of the first operand with the inside of the
3869 negation of the second operand. */
3870
3871 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3872 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3873 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3874 else
3875 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3876 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3877 TREE_OPERAND (arg, 1));
3878
3879 case TRUTH_ANDIF_EXPR:
3880 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3881 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3882 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3883 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3884 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3885
3886 case TRUTH_ORIF_EXPR:
3887 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3888 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3889 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3890 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3891 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3892
3893 case TRUTH_NOT_EXPR:
3894 return TREE_OPERAND (arg, 0);
3895
3896 case COND_EXPR:
3897 {
3898 tree arg1 = TREE_OPERAND (arg, 1);
3899 tree arg2 = TREE_OPERAND (arg, 2);
3900
3901 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3902 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3903
3904 /* A COND_EXPR may have a throw as one operand, which
3905 then has void type. Just leave void operands
3906 as they are. */
3907 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3908 VOID_TYPE_P (TREE_TYPE (arg1))
3909 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3910 VOID_TYPE_P (TREE_TYPE (arg2))
3911 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3912 }
3913
3914 case COMPOUND_EXPR:
3915 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3916 return build2_loc (loc, COMPOUND_EXPR, type,
3917 TREE_OPERAND (arg, 0),
3918 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3919
3920 case NON_LVALUE_EXPR:
3921 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3922 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3923
3924 CASE_CONVERT:
3925 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3926 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3927
3928 /* fall through */
3929
3930 case FLOAT_EXPR:
3931 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3932 return build1_loc (loc, TREE_CODE (arg), type,
3933 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3934
3935 case BIT_AND_EXPR:
3936 if (!integer_onep (TREE_OPERAND (arg, 1)))
3937 return NULL_TREE;
3938 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3939
3940 case SAVE_EXPR:
3941 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3942
3943 case CLEANUP_POINT_EXPR:
3944 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3945 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3946 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3947
3948 default:
3949 return NULL_TREE;
3950 }
3951 }
3952
3953 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3954 assume that ARG is an operation that returns a truth value (0 or 1
3955 for scalars, 0 or -1 for vectors). Return the folded expression if
3956 folding is successful. Otherwise, return NULL_TREE. */
3957
3958 static tree
3959 fold_invert_truthvalue (location_t loc, tree arg)
3960 {
3961 tree type = TREE_TYPE (arg);
3962 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3963 ? BIT_NOT_EXPR
3964 : TRUTH_NOT_EXPR,
3965 type, arg);
3966 }
3967
3968 /* Return a simplified tree node for the truth-negation of ARG. This
3969 never alters ARG itself. We assume that ARG is an operation that
3970 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3971
3972 tree
3973 invert_truthvalue_loc (location_t loc, tree arg)
3974 {
3975 if (TREE_CODE (arg) == ERROR_MARK)
3976 return arg;
3977
3978 tree type = TREE_TYPE (arg);
3979 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3980 ? BIT_NOT_EXPR
3981 : TRUTH_NOT_EXPR,
3982 type, arg);
3983 }
3984 \f
3985 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3986 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3987 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3988 is the original memory reference used to preserve the alias set of
3989 the access. */
3990
3991 static tree
3992 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3993 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3994 int unsignedp, int reversep)
3995 {
3996 tree result, bftype;
3997
3998 /* Attempt not to lose the access path if possible. */
3999 if (TREE_CODE (orig_inner) == COMPONENT_REF)
4000 {
4001 tree ninner = TREE_OPERAND (orig_inner, 0);
4002 machine_mode nmode;
4003 poly_int64 nbitsize, nbitpos;
4004 tree noffset;
4005 int nunsignedp, nreversep, nvolatilep = 0;
4006 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4007 &noffset, &nmode, &nunsignedp,
4008 &nreversep, &nvolatilep);
4009 if (base == inner
4010 && noffset == NULL_TREE
4011 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4012 && !reversep
4013 && !nreversep
4014 && !nvolatilep)
4015 {
4016 inner = ninner;
4017 bitpos -= nbitpos;
4018 }
4019 }
4020
4021 alias_set_type iset = get_alias_set (orig_inner);
4022 if (iset == 0 && get_alias_set (inner) != iset)
4023 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4024 build_fold_addr_expr (inner),
4025 build_int_cst (ptr_type_node, 0));
4026
4027 if (known_eq (bitpos, 0) && !reversep)
4028 {
4029 tree size = TYPE_SIZE (TREE_TYPE (inner));
4030 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4031 || POINTER_TYPE_P (TREE_TYPE (inner)))
4032 && tree_fits_shwi_p (size)
4033 && tree_to_shwi (size) == bitsize)
4034 return fold_convert_loc (loc, type, inner);
4035 }
4036
4037 bftype = type;
4038 if (TYPE_PRECISION (bftype) != bitsize
4039 || TYPE_UNSIGNED (bftype) == !unsignedp)
4040 bftype = build_nonstandard_integer_type (bitsize, 0);
4041
4042 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4043 bitsize_int (bitsize), bitsize_int (bitpos));
4044 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4045
4046 if (bftype != type)
4047 result = fold_convert_loc (loc, type, result);
4048
4049 return result;
4050 }
4051
4052 /* Optimize a bit-field compare.
4053
4054 There are two cases: First is a compare against a constant and the
4055 second is a comparison of two items where the fields are at the same
4056 bit position relative to the start of a chunk (byte, halfword, word)
4057 large enough to contain it. In these cases we can avoid the shift
4058 implicit in bitfield extractions.
4059
4060 For constants, we emit a compare of the shifted constant with the
4061 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4062 compared. For two fields at the same position, we do the ANDs with the
4063 similar mask and compare the result of the ANDs.
4064
4065 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4066 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4067 are the left and right operands of the comparison, respectively.
4068
4069 If the optimization described above can be done, we return the resulting
4070 tree. Otherwise we return zero. */
4071
4072 static tree
4073 optimize_bit_field_compare (location_t loc, enum tree_code code,
4074 tree compare_type, tree lhs, tree rhs)
4075 {
4076 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4077 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4078 tree type = TREE_TYPE (lhs);
4079 tree unsigned_type;
4080 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4081 machine_mode lmode, rmode;
4082 scalar_int_mode nmode;
4083 int lunsignedp, runsignedp;
4084 int lreversep, rreversep;
4085 int lvolatilep = 0, rvolatilep = 0;
4086 tree linner, rinner = NULL_TREE;
4087 tree mask;
4088 tree offset;
4089
4090 /* Get all the information about the extractions being done. If the bit size
4091 is the same as the size of the underlying object, we aren't doing an
4092 extraction at all and so can do nothing. We also don't want to
4093 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4094 then will no longer be able to replace it. */
4095 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4096 &lunsignedp, &lreversep, &lvolatilep);
4097 if (linner == lhs
4098 || !known_size_p (plbitsize)
4099 || !plbitsize.is_constant (&lbitsize)
4100 || !plbitpos.is_constant (&lbitpos)
4101 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4102 || offset != 0
4103 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4104 || lvolatilep)
4105 return 0;
4106
4107 if (const_p)
4108 rreversep = lreversep;
4109 else
4110 {
4111 /* If this is not a constant, we can only do something if bit positions,
4112 sizes, signedness and storage order are the same. */
4113 rinner
4114 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4115 &runsignedp, &rreversep, &rvolatilep);
4116
4117 if (rinner == rhs
4118 || maybe_ne (lbitpos, rbitpos)
4119 || maybe_ne (lbitsize, rbitsize)
4120 || lunsignedp != runsignedp
4121 || lreversep != rreversep
4122 || offset != 0
4123 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4124 || rvolatilep)
4125 return 0;
4126 }
4127
4128 /* Honor the C++ memory model and mimic what RTL expansion does. */
4129 poly_uint64 bitstart = 0;
4130 poly_uint64 bitend = 0;
4131 if (TREE_CODE (lhs) == COMPONENT_REF)
4132 {
4133 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4134 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4135 return 0;
4136 }
4137
4138 /* See if we can find a mode to refer to this field. We should be able to,
4139 but fail if we can't. */
4140 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4141 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4142 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4143 TYPE_ALIGN (TREE_TYPE (rinner))),
4144 BITS_PER_WORD, false, &nmode))
4145 return 0;
4146
4147 /* Set signed and unsigned types of the precision of this mode for the
4148 shifts below. */
4149 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4150
4151 /* Compute the bit position and size for the new reference and our offset
4152 within it. If the new reference is the same size as the original, we
4153 won't optimize anything, so return zero. */
4154 nbitsize = GET_MODE_BITSIZE (nmode);
4155 nbitpos = lbitpos & ~ (nbitsize - 1);
4156 lbitpos -= nbitpos;
4157 if (nbitsize == lbitsize)
4158 return 0;
4159
4160 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4161 lbitpos = nbitsize - lbitsize - lbitpos;
4162
4163 /* Make the mask to be used against the extracted field. */
4164 mask = build_int_cst_type (unsigned_type, -1);
4165 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4166 mask = const_binop (RSHIFT_EXPR, mask,
4167 size_int (nbitsize - lbitsize - lbitpos));
4168
4169 if (! const_p)
4170 {
4171 if (nbitpos < 0)
4172 return 0;
4173
4174 /* If not comparing with constant, just rework the comparison
4175 and return. */
4176 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4177 nbitsize, nbitpos, 1, lreversep);
4178 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4179 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4180 nbitsize, nbitpos, 1, rreversep);
4181 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4182 return fold_build2_loc (loc, code, compare_type, t1, t2);
4183 }
4184
4185 /* Otherwise, we are handling the constant case. See if the constant is too
4186 big for the field. Warn and return a tree for 0 (false) if so. We do
4187 this not only for its own sake, but to avoid having to test for this
4188 error case below. If we didn't, we might generate wrong code.
4189
4190 For unsigned fields, the constant shifted right by the field length should
4191 be all zero. For signed fields, the high-order bits should agree with
4192 the sign bit. */
4193
4194 if (lunsignedp)
4195 {
4196 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4197 {
4198 warning (0, "comparison is always %d due to width of bit-field",
4199 code == NE_EXPR);
4200 return constant_boolean_node (code == NE_EXPR, compare_type);
4201 }
4202 }
4203 else
4204 {
4205 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4206 if (tem != 0 && tem != -1)
4207 {
4208 warning (0, "comparison is always %d due to width of bit-field",
4209 code == NE_EXPR);
4210 return constant_boolean_node (code == NE_EXPR, compare_type);
4211 }
4212 }
4213
4214 if (nbitpos < 0)
4215 return 0;
4216
4217 /* Single-bit compares should always be against zero. */
4218 if (lbitsize == 1 && ! integer_zerop (rhs))
4219 {
4220 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4221 rhs = build_int_cst (type, 0);
4222 }
4223
4224 /* Make a new bitfield reference, shift the constant over the
4225 appropriate number of bits and mask it with the computed mask
4226 (in case this was a signed field). If we changed it, make a new one. */
4227 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4228 nbitsize, nbitpos, 1, lreversep);
4229
4230 rhs = const_binop (BIT_AND_EXPR,
4231 const_binop (LSHIFT_EXPR,
4232 fold_convert_loc (loc, unsigned_type, rhs),
4233 size_int (lbitpos)),
4234 mask);
4235
4236 lhs = build2_loc (loc, code, compare_type,
4237 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4238 return lhs;
4239 }
4240 \f
4241 /* Subroutine for fold_truth_andor_1: decode a field reference.
4242
4243 If EXP is a comparison reference, we return the innermost reference.
4244
4245 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4246 set to the starting bit number.
4247
4248 If the innermost field can be completely contained in a mode-sized
4249 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4250
4251 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4252 otherwise it is not changed.
4253
4254 *PUNSIGNEDP is set to the signedness of the field.
4255
4256 *PREVERSEP is set to the storage order of the field.
4257
4258 *PMASK is set to the mask used. This is either contained in a
4259 BIT_AND_EXPR or derived from the width of the field.
4260
4261 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4262
4263 Return 0 if this is not a component reference or is one that we can't
4264 do anything with. */
4265
4266 static tree
4267 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4268 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4269 int *punsignedp, int *preversep, int *pvolatilep,
4270 tree *pmask, tree *pand_mask)
4271 {
4272 tree exp = *exp_;
4273 tree outer_type = 0;
4274 tree and_mask = 0;
4275 tree mask, inner, offset;
4276 tree unsigned_type;
4277 unsigned int precision;
4278
4279 /* All the optimizations using this function assume integer fields.
4280 There are problems with FP fields since the type_for_size call
4281 below can fail for, e.g., XFmode. */
4282 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4283 return 0;
4284
4285 /* We are interested in the bare arrangement of bits, so strip everything
4286 that doesn't affect the machine mode. However, record the type of the
4287 outermost expression if it may matter below. */
4288 if (CONVERT_EXPR_P (exp)
4289 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4290 outer_type = TREE_TYPE (exp);
4291 STRIP_NOPS (exp);
4292
4293 if (TREE_CODE (exp) == BIT_AND_EXPR)
4294 {
4295 and_mask = TREE_OPERAND (exp, 1);
4296 exp = TREE_OPERAND (exp, 0);
4297 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4298 if (TREE_CODE (and_mask) != INTEGER_CST)
4299 return 0;
4300 }
4301
4302 poly_int64 poly_bitsize, poly_bitpos;
4303 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4304 pmode, punsignedp, preversep, pvolatilep);
4305 if ((inner == exp && and_mask == 0)
4306 || !poly_bitsize.is_constant (pbitsize)
4307 || !poly_bitpos.is_constant (pbitpos)
4308 || *pbitsize < 0
4309 || offset != 0
4310 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4311 /* Reject out-of-bound accesses (PR79731). */
4312 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4313 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4314 *pbitpos + *pbitsize) < 0))
4315 return 0;
4316
4317 *exp_ = exp;
4318
4319 /* If the number of bits in the reference is the same as the bitsize of
4320 the outer type, then the outer type gives the signedness. Otherwise
4321 (in case of a small bitfield) the signedness is unchanged. */
4322 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4323 *punsignedp = TYPE_UNSIGNED (outer_type);
4324
4325 /* Compute the mask to access the bitfield. */
4326 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4327 precision = TYPE_PRECISION (unsigned_type);
4328
4329 mask = build_int_cst_type (unsigned_type, -1);
4330
4331 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4332 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4333
4334 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4335 if (and_mask != 0)
4336 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4337 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4338
4339 *pmask = mask;
4340 *pand_mask = and_mask;
4341 return inner;
4342 }
4343
4344 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4345 bit positions and MASK is SIGNED. */
4346
4347 static int
4348 all_ones_mask_p (const_tree mask, unsigned int size)
4349 {
4350 tree type = TREE_TYPE (mask);
4351 unsigned int precision = TYPE_PRECISION (type);
4352
4353 /* If this function returns true when the type of the mask is
4354 UNSIGNED, then there will be errors. In particular see
4355 gcc.c-torture/execute/990326-1.c. There does not appear to be
4356 any documentation paper trail as to why this is so. But the pre
4357 wide-int worked with that restriction and it has been preserved
4358 here. */
4359 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4360 return false;
4361
4362 return wi::mask (size, false, precision) == wi::to_wide (mask);
4363 }
4364
4365 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4366 represents the sign bit of EXP's type. If EXP represents a sign
4367 or zero extension, also test VAL against the unextended type.
4368 The return value is the (sub)expression whose sign bit is VAL,
4369 or NULL_TREE otherwise. */
4370
4371 tree
4372 sign_bit_p (tree exp, const_tree val)
4373 {
4374 int width;
4375 tree t;
4376
4377 /* Tree EXP must have an integral type. */
4378 t = TREE_TYPE (exp);
4379 if (! INTEGRAL_TYPE_P (t))
4380 return NULL_TREE;
4381
4382 /* Tree VAL must be an integer constant. */
4383 if (TREE_CODE (val) != INTEGER_CST
4384 || TREE_OVERFLOW (val))
4385 return NULL_TREE;
4386
4387 width = TYPE_PRECISION (t);
4388 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4389 return exp;
4390
4391 /* Handle extension from a narrower type. */
4392 if (TREE_CODE (exp) == NOP_EXPR
4393 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4394 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4395
4396 return NULL_TREE;
4397 }
4398
4399 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4400 to be evaluated unconditionally. */
4401
4402 static int
4403 simple_operand_p (const_tree exp)
4404 {
4405 /* Strip any conversions that don't change the machine mode. */
4406 STRIP_NOPS (exp);
4407
4408 return (CONSTANT_CLASS_P (exp)
4409 || TREE_CODE (exp) == SSA_NAME
4410 || (DECL_P (exp)
4411 && ! TREE_ADDRESSABLE (exp)
4412 && ! TREE_THIS_VOLATILE (exp)
4413 && ! DECL_NONLOCAL (exp)
4414 /* Don't regard global variables as simple. They may be
4415 allocated in ways unknown to the compiler (shared memory,
4416 #pragma weak, etc). */
4417 && ! TREE_PUBLIC (exp)
4418 && ! DECL_EXTERNAL (exp)
4419 /* Weakrefs are not safe to be read, since they can be NULL.
4420 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4421 have DECL_WEAK flag set. */
4422 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4423 /* Loading a static variable is unduly expensive, but global
4424 registers aren't expensive. */
4425 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4426 }
4427
4428 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4429 to be evaluated unconditionally.
4430 I addition to simple_operand_p, we assume that comparisons, conversions,
4431 and logic-not operations are simple, if their operands are simple, too. */
4432
4433 static bool
4434 simple_operand_p_2 (tree exp)
4435 {
4436 enum tree_code code;
4437
4438 if (TREE_SIDE_EFFECTS (exp)
4439 || tree_could_trap_p (exp))
4440 return false;
4441
4442 while (CONVERT_EXPR_P (exp))
4443 exp = TREE_OPERAND (exp, 0);
4444
4445 code = TREE_CODE (exp);
4446
4447 if (TREE_CODE_CLASS (code) == tcc_comparison)
4448 return (simple_operand_p (TREE_OPERAND (exp, 0))
4449 && simple_operand_p (TREE_OPERAND (exp, 1)));
4450
4451 if (code == TRUTH_NOT_EXPR)
4452 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4453
4454 return simple_operand_p (exp);
4455 }
4456
4457 \f
4458 /* The following functions are subroutines to fold_range_test and allow it to
4459 try to change a logical combination of comparisons into a range test.
4460
4461 For example, both
4462 X == 2 || X == 3 || X == 4 || X == 5
4463 and
4464 X >= 2 && X <= 5
4465 are converted to
4466 (unsigned) (X - 2) <= 3
4467
4468 We describe each set of comparisons as being either inside or outside
4469 a range, using a variable named like IN_P, and then describe the
4470 range with a lower and upper bound. If one of the bounds is omitted,
4471 it represents either the highest or lowest value of the type.
4472
4473 In the comments below, we represent a range by two numbers in brackets
4474 preceded by a "+" to designate being inside that range, or a "-" to
4475 designate being outside that range, so the condition can be inverted by
4476 flipping the prefix. An omitted bound is represented by a "-". For
4477 example, "- [-, 10]" means being outside the range starting at the lowest
4478 possible value and ending at 10, in other words, being greater than 10.
4479 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4480 always false.
4481
4482 We set up things so that the missing bounds are handled in a consistent
4483 manner so neither a missing bound nor "true" and "false" need to be
4484 handled using a special case. */
4485
4486 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4487 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4488 and UPPER1_P are nonzero if the respective argument is an upper bound
4489 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4490 must be specified for a comparison. ARG1 will be converted to ARG0's
4491 type if both are specified. */
4492
4493 static tree
4494 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4495 tree arg1, int upper1_p)
4496 {
4497 tree tem;
4498 int result;
4499 int sgn0, sgn1;
4500
4501 /* If neither arg represents infinity, do the normal operation.
4502 Else, if not a comparison, return infinity. Else handle the special
4503 comparison rules. Note that most of the cases below won't occur, but
4504 are handled for consistency. */
4505
4506 if (arg0 != 0 && arg1 != 0)
4507 {
4508 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4509 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4510 STRIP_NOPS (tem);
4511 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4512 }
4513
4514 if (TREE_CODE_CLASS (code) != tcc_comparison)
4515 return 0;
4516
4517 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4518 for neither. In real maths, we cannot assume open ended ranges are
4519 the same. But, this is computer arithmetic, where numbers are finite.
4520 We can therefore make the transformation of any unbounded range with
4521 the value Z, Z being greater than any representable number. This permits
4522 us to treat unbounded ranges as equal. */
4523 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4524 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4525 switch (code)
4526 {
4527 case EQ_EXPR:
4528 result = sgn0 == sgn1;
4529 break;
4530 case NE_EXPR:
4531 result = sgn0 != sgn1;
4532 break;
4533 case LT_EXPR:
4534 result = sgn0 < sgn1;
4535 break;
4536 case LE_EXPR:
4537 result = sgn0 <= sgn1;
4538 break;
4539 case GT_EXPR:
4540 result = sgn0 > sgn1;
4541 break;
4542 case GE_EXPR:
4543 result = sgn0 >= sgn1;
4544 break;
4545 default:
4546 gcc_unreachable ();
4547 }
4548
4549 return constant_boolean_node (result, type);
4550 }
4551 \f
4552 /* Helper routine for make_range. Perform one step for it, return
4553 new expression if the loop should continue or NULL_TREE if it should
4554 stop. */
4555
4556 tree
4557 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4558 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4559 bool *strict_overflow_p)
4560 {
4561 tree arg0_type = TREE_TYPE (arg0);
4562 tree n_low, n_high, low = *p_low, high = *p_high;
4563 int in_p = *p_in_p, n_in_p;
4564
4565 switch (code)
4566 {
4567 case TRUTH_NOT_EXPR:
4568 /* We can only do something if the range is testing for zero. */
4569 if (low == NULL_TREE || high == NULL_TREE
4570 || ! integer_zerop (low) || ! integer_zerop (high))
4571 return NULL_TREE;
4572 *p_in_p = ! in_p;
4573 return arg0;
4574
4575 case EQ_EXPR: case NE_EXPR:
4576 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4577 /* We can only do something if the range is testing for zero
4578 and if the second operand is an integer constant. Note that
4579 saying something is "in" the range we make is done by
4580 complementing IN_P since it will set in the initial case of
4581 being not equal to zero; "out" is leaving it alone. */
4582 if (low == NULL_TREE || high == NULL_TREE
4583 || ! integer_zerop (low) || ! integer_zerop (high)
4584 || TREE_CODE (arg1) != INTEGER_CST)
4585 return NULL_TREE;
4586
4587 switch (code)
4588 {
4589 case NE_EXPR: /* - [c, c] */
4590 low = high = arg1;
4591 break;
4592 case EQ_EXPR: /* + [c, c] */
4593 in_p = ! in_p, low = high = arg1;
4594 break;
4595 case GT_EXPR: /* - [-, c] */
4596 low = 0, high = arg1;
4597 break;
4598 case GE_EXPR: /* + [c, -] */
4599 in_p = ! in_p, low = arg1, high = 0;
4600 break;
4601 case LT_EXPR: /* - [c, -] */
4602 low = arg1, high = 0;
4603 break;
4604 case LE_EXPR: /* + [-, c] */
4605 in_p = ! in_p, low = 0, high = arg1;
4606 break;
4607 default:
4608 gcc_unreachable ();
4609 }
4610
4611 /* If this is an unsigned comparison, we also know that EXP is
4612 greater than or equal to zero. We base the range tests we make
4613 on that fact, so we record it here so we can parse existing
4614 range tests. We test arg0_type since often the return type
4615 of, e.g. EQ_EXPR, is boolean. */
4616 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4617 {
4618 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4619 in_p, low, high, 1,
4620 build_int_cst (arg0_type, 0),
4621 NULL_TREE))
4622 return NULL_TREE;
4623
4624 in_p = n_in_p, low = n_low, high = n_high;
4625
4626 /* If the high bound is missing, but we have a nonzero low
4627 bound, reverse the range so it goes from zero to the low bound
4628 minus 1. */
4629 if (high == 0 && low && ! integer_zerop (low))
4630 {
4631 in_p = ! in_p;
4632 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4633 build_int_cst (TREE_TYPE (low), 1), 0);
4634 low = build_int_cst (arg0_type, 0);
4635 }
4636 }
4637
4638 *p_low = low;
4639 *p_high = high;
4640 *p_in_p = in_p;
4641 return arg0;
4642
4643 case NEGATE_EXPR:
4644 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4645 low and high are non-NULL, then normalize will DTRT. */
4646 if (!TYPE_UNSIGNED (arg0_type)
4647 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4648 {
4649 if (low == NULL_TREE)
4650 low = TYPE_MIN_VALUE (arg0_type);
4651 if (high == NULL_TREE)
4652 high = TYPE_MAX_VALUE (arg0_type);
4653 }
4654
4655 /* (-x) IN [a,b] -> x in [-b, -a] */
4656 n_low = range_binop (MINUS_EXPR, exp_type,
4657 build_int_cst (exp_type, 0),
4658 0, high, 1);
4659 n_high = range_binop (MINUS_EXPR, exp_type,
4660 build_int_cst (exp_type, 0),
4661 0, low, 0);
4662 if (n_high != 0 && TREE_OVERFLOW (n_high))
4663 return NULL_TREE;
4664 goto normalize;
4665
4666 case BIT_NOT_EXPR:
4667 /* ~ X -> -X - 1 */
4668 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4669 build_int_cst (exp_type, 1));
4670
4671 case PLUS_EXPR:
4672 case MINUS_EXPR:
4673 if (TREE_CODE (arg1) != INTEGER_CST)
4674 return NULL_TREE;
4675
4676 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4677 move a constant to the other side. */
4678 if (!TYPE_UNSIGNED (arg0_type)
4679 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4680 return NULL_TREE;
4681
4682 /* If EXP is signed, any overflow in the computation is undefined,
4683 so we don't worry about it so long as our computations on
4684 the bounds don't overflow. For unsigned, overflow is defined
4685 and this is exactly the right thing. */
4686 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4687 arg0_type, low, 0, arg1, 0);
4688 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4689 arg0_type, high, 1, arg1, 0);
4690 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4691 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4692 return NULL_TREE;
4693
4694 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4695 *strict_overflow_p = true;
4696
4697 normalize:
4698 /* Check for an unsigned range which has wrapped around the maximum
4699 value thus making n_high < n_low, and normalize it. */
4700 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4701 {
4702 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4703 build_int_cst (TREE_TYPE (n_high), 1), 0);
4704 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4705 build_int_cst (TREE_TYPE (n_low), 1), 0);
4706
4707 /* If the range is of the form +/- [ x+1, x ], we won't
4708 be able to normalize it. But then, it represents the
4709 whole range or the empty set, so make it
4710 +/- [ -, - ]. */
4711 if (tree_int_cst_equal (n_low, low)
4712 && tree_int_cst_equal (n_high, high))
4713 low = high = 0;
4714 else
4715 in_p = ! in_p;
4716 }
4717 else
4718 low = n_low, high = n_high;
4719
4720 *p_low = low;
4721 *p_high = high;
4722 *p_in_p = in_p;
4723 return arg0;
4724
4725 CASE_CONVERT:
4726 case NON_LVALUE_EXPR:
4727 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4728 return NULL_TREE;
4729
4730 if (! INTEGRAL_TYPE_P (arg0_type)
4731 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4732 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4733 return NULL_TREE;
4734
4735 n_low = low, n_high = high;
4736
4737 if (n_low != 0)
4738 n_low = fold_convert_loc (loc, arg0_type, n_low);
4739
4740 if (n_high != 0)
4741 n_high = fold_convert_loc (loc, arg0_type, n_high);
4742
4743 /* If we're converting arg0 from an unsigned type, to exp,
4744 a signed type, we will be doing the comparison as unsigned.
4745 The tests above have already verified that LOW and HIGH
4746 are both positive.
4747
4748 So we have to ensure that we will handle large unsigned
4749 values the same way that the current signed bounds treat
4750 negative values. */
4751
4752 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4753 {
4754 tree high_positive;
4755 tree equiv_type;
4756 /* For fixed-point modes, we need to pass the saturating flag
4757 as the 2nd parameter. */
4758 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4759 equiv_type
4760 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4761 TYPE_SATURATING (arg0_type));
4762 else
4763 equiv_type
4764 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4765
4766 /* A range without an upper bound is, naturally, unbounded.
4767 Since convert would have cropped a very large value, use
4768 the max value for the destination type. */
4769 high_positive
4770 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4771 : TYPE_MAX_VALUE (arg0_type);
4772
4773 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4774 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4775 fold_convert_loc (loc, arg0_type,
4776 high_positive),
4777 build_int_cst (arg0_type, 1));
4778
4779 /* If the low bound is specified, "and" the range with the
4780 range for which the original unsigned value will be
4781 positive. */
4782 if (low != 0)
4783 {
4784 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4785 1, fold_convert_loc (loc, arg0_type,
4786 integer_zero_node),
4787 high_positive))
4788 return NULL_TREE;
4789
4790 in_p = (n_in_p == in_p);
4791 }
4792 else
4793 {
4794 /* Otherwise, "or" the range with the range of the input
4795 that will be interpreted as negative. */
4796 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4797 1, fold_convert_loc (loc, arg0_type,
4798 integer_zero_node),
4799 high_positive))
4800 return NULL_TREE;
4801
4802 in_p = (in_p != n_in_p);
4803 }
4804 }
4805
4806 *p_low = n_low;
4807 *p_high = n_high;
4808 *p_in_p = in_p;
4809 return arg0;
4810
4811 default:
4812 return NULL_TREE;
4813 }
4814 }
4815
4816 /* Given EXP, a logical expression, set the range it is testing into
4817 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4818 actually being tested. *PLOW and *PHIGH will be made of the same
4819 type as the returned expression. If EXP is not a comparison, we
4820 will most likely not be returning a useful value and range. Set
4821 *STRICT_OVERFLOW_P to true if the return value is only valid
4822 because signed overflow is undefined; otherwise, do not change
4823 *STRICT_OVERFLOW_P. */
4824
4825 tree
4826 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4827 bool *strict_overflow_p)
4828 {
4829 enum tree_code code;
4830 tree arg0, arg1 = NULL_TREE;
4831 tree exp_type, nexp;
4832 int in_p;
4833 tree low, high;
4834 location_t loc = EXPR_LOCATION (exp);
4835
4836 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4837 and see if we can refine the range. Some of the cases below may not
4838 happen, but it doesn't seem worth worrying about this. We "continue"
4839 the outer loop when we've changed something; otherwise we "break"
4840 the switch, which will "break" the while. */
4841
4842 in_p = 0;
4843 low = high = build_int_cst (TREE_TYPE (exp), 0);
4844
4845 while (1)
4846 {
4847 code = TREE_CODE (exp);
4848 exp_type = TREE_TYPE (exp);
4849 arg0 = NULL_TREE;
4850
4851 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4852 {
4853 if (TREE_OPERAND_LENGTH (exp) > 0)
4854 arg0 = TREE_OPERAND (exp, 0);
4855 if (TREE_CODE_CLASS (code) == tcc_binary
4856 || TREE_CODE_CLASS (code) == tcc_comparison
4857 || (TREE_CODE_CLASS (code) == tcc_expression
4858 && TREE_OPERAND_LENGTH (exp) > 1))
4859 arg1 = TREE_OPERAND (exp, 1);
4860 }
4861 if (arg0 == NULL_TREE)
4862 break;
4863
4864 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4865 &high, &in_p, strict_overflow_p);
4866 if (nexp == NULL_TREE)
4867 break;
4868 exp = nexp;
4869 }
4870
4871 /* If EXP is a constant, we can evaluate whether this is true or false. */
4872 if (TREE_CODE (exp) == INTEGER_CST)
4873 {
4874 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4875 exp, 0, low, 0))
4876 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4877 exp, 1, high, 1)));
4878 low = high = 0;
4879 exp = 0;
4880 }
4881
4882 *pin_p = in_p, *plow = low, *phigh = high;
4883 return exp;
4884 }
4885
4886 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4887 a bitwise check i.e. when
4888 LOW == 0xXX...X00...0
4889 HIGH == 0xXX...X11...1
4890 Return corresponding mask in MASK and stem in VALUE. */
4891
4892 static bool
4893 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4894 tree *value)
4895 {
4896 if (TREE_CODE (low) != INTEGER_CST
4897 || TREE_CODE (high) != INTEGER_CST)
4898 return false;
4899
4900 unsigned prec = TYPE_PRECISION (type);
4901 wide_int lo = wi::to_wide (low, prec);
4902 wide_int hi = wi::to_wide (high, prec);
4903
4904 wide_int end_mask = lo ^ hi;
4905 if ((end_mask & (end_mask + 1)) != 0
4906 || (lo & end_mask) != 0)
4907 return false;
4908
4909 wide_int stem_mask = ~end_mask;
4910 wide_int stem = lo & stem_mask;
4911 if (stem != (hi & stem_mask))
4912 return false;
4913
4914 *mask = wide_int_to_tree (type, stem_mask);
4915 *value = wide_int_to_tree (type, stem);
4916
4917 return true;
4918 }
4919 \f
4920 /* Helper routine for build_range_check and match.pd. Return the type to
4921 perform the check or NULL if it shouldn't be optimized. */
4922
4923 tree
4924 range_check_type (tree etype)
4925 {
4926 /* First make sure that arithmetics in this type is valid, then make sure
4927 that it wraps around. */
4928 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4929 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4930 TYPE_UNSIGNED (etype));
4931
4932 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4933 {
4934 tree utype, minv, maxv;
4935
4936 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4937 for the type in question, as we rely on this here. */
4938 utype = unsigned_type_for (etype);
4939 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4940 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4941 build_int_cst (TREE_TYPE (maxv), 1), 1);
4942 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4943
4944 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4945 minv, 1, maxv, 1)))
4946 etype = utype;
4947 else
4948 return NULL_TREE;
4949 }
4950 return etype;
4951 }
4952
4953 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4954 type, TYPE, return an expression to test if EXP is in (or out of, depending
4955 on IN_P) the range. Return 0 if the test couldn't be created. */
4956
4957 tree
4958 build_range_check (location_t loc, tree type, tree exp, int in_p,
4959 tree low, tree high)
4960 {
4961 tree etype = TREE_TYPE (exp), mask, value;
4962
4963 /* Disable this optimization for function pointer expressions
4964 on targets that require function pointer canonicalization. */
4965 if (targetm.have_canonicalize_funcptr_for_compare ()
4966 && POINTER_TYPE_P (etype)
4967 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
4968 return NULL_TREE;
4969
4970 if (! in_p)
4971 {
4972 value = build_range_check (loc, type, exp, 1, low, high);
4973 if (value != 0)
4974 return invert_truthvalue_loc (loc, value);
4975
4976 return 0;
4977 }
4978
4979 if (low == 0 && high == 0)
4980 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4981
4982 if (low == 0)
4983 return fold_build2_loc (loc, LE_EXPR, type, exp,
4984 fold_convert_loc (loc, etype, high));
4985
4986 if (high == 0)
4987 return fold_build2_loc (loc, GE_EXPR, type, exp,
4988 fold_convert_loc (loc, etype, low));
4989
4990 if (operand_equal_p (low, high, 0))
4991 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4992 fold_convert_loc (loc, etype, low));
4993
4994 if (TREE_CODE (exp) == BIT_AND_EXPR
4995 && maskable_range_p (low, high, etype, &mask, &value))
4996 return fold_build2_loc (loc, EQ_EXPR, type,
4997 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4998 exp, mask),
4999 value);
5000
5001 if (integer_zerop (low))
5002 {
5003 if (! TYPE_UNSIGNED (etype))
5004 {
5005 etype = unsigned_type_for (etype);
5006 high = fold_convert_loc (loc, etype, high);
5007 exp = fold_convert_loc (loc, etype, exp);
5008 }
5009 return build_range_check (loc, type, exp, 1, 0, high);
5010 }
5011
5012 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5013 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5014 {
5015 int prec = TYPE_PRECISION (etype);
5016
5017 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5018 {
5019 if (TYPE_UNSIGNED (etype))
5020 {
5021 tree signed_etype = signed_type_for (etype);
5022 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5023 etype
5024 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5025 else
5026 etype = signed_etype;
5027 exp = fold_convert_loc (loc, etype, exp);
5028 }
5029 return fold_build2_loc (loc, GT_EXPR, type, exp,
5030 build_int_cst (etype, 0));
5031 }
5032 }
5033
5034 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5035 This requires wrap-around arithmetics for the type of the expression. */
5036 etype = range_check_type (etype);
5037 if (etype == NULL_TREE)
5038 return NULL_TREE;
5039
5040 if (POINTER_TYPE_P (etype))
5041 etype = unsigned_type_for (etype);
5042
5043 high = fold_convert_loc (loc, etype, high);
5044 low = fold_convert_loc (loc, etype, low);
5045 exp = fold_convert_loc (loc, etype, exp);
5046
5047 value = const_binop (MINUS_EXPR, high, low);
5048
5049 if (value != 0 && !TREE_OVERFLOW (value))
5050 return build_range_check (loc, type,
5051 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5052 1, build_int_cst (etype, 0), value);
5053
5054 return 0;
5055 }
5056 \f
5057 /* Return the predecessor of VAL in its type, handling the infinite case. */
5058
5059 static tree
5060 range_predecessor (tree val)
5061 {
5062 tree type = TREE_TYPE (val);
5063
5064 if (INTEGRAL_TYPE_P (type)
5065 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5066 return 0;
5067 else
5068 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5069 build_int_cst (TREE_TYPE (val), 1), 0);
5070 }
5071
5072 /* Return the successor of VAL in its type, handling the infinite case. */
5073
5074 static tree
5075 range_successor (tree val)
5076 {
5077 tree type = TREE_TYPE (val);
5078
5079 if (INTEGRAL_TYPE_P (type)
5080 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5081 return 0;
5082 else
5083 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5084 build_int_cst (TREE_TYPE (val), 1), 0);
5085 }
5086
5087 /* Given two ranges, see if we can merge them into one. Return 1 if we
5088 can, 0 if we can't. Set the output range into the specified parameters. */
5089
5090 bool
5091 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5092 tree high0, int in1_p, tree low1, tree high1)
5093 {
5094 int no_overlap;
5095 int subset;
5096 int temp;
5097 tree tem;
5098 int in_p;
5099 tree low, high;
5100 int lowequal = ((low0 == 0 && low1 == 0)
5101 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5102 low0, 0, low1, 0)));
5103 int highequal = ((high0 == 0 && high1 == 0)
5104 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5105 high0, 1, high1, 1)));
5106
5107 /* Make range 0 be the range that starts first, or ends last if they
5108 start at the same value. Swap them if it isn't. */
5109 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5110 low0, 0, low1, 0))
5111 || (lowequal
5112 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5113 high1, 1, high0, 1))))
5114 {
5115 temp = in0_p, in0_p = in1_p, in1_p = temp;
5116 tem = low0, low0 = low1, low1 = tem;
5117 tem = high0, high0 = high1, high1 = tem;
5118 }
5119
5120 /* If the second range is != high1 where high1 is the type maximum of
5121 the type, try first merging with < high1 range. */
5122 if (low1
5123 && high1
5124 && TREE_CODE (low1) == INTEGER_CST
5125 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5126 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5127 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5128 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5129 && operand_equal_p (low1, high1, 0))
5130 {
5131 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5132 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5133 !in1_p, NULL_TREE, range_predecessor (low1)))
5134 return true;
5135 /* Similarly for the second range != low1 where low1 is the type minimum
5136 of the type, try first merging with > low1 range. */
5137 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5138 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5139 !in1_p, range_successor (low1), NULL_TREE))
5140 return true;
5141 }
5142
5143 /* Now flag two cases, whether the ranges are disjoint or whether the
5144 second range is totally subsumed in the first. Note that the tests
5145 below are simplified by the ones above. */
5146 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5147 high0, 1, low1, 0));
5148 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5149 high1, 1, high0, 1));
5150
5151 /* We now have four cases, depending on whether we are including or
5152 excluding the two ranges. */
5153 if (in0_p && in1_p)
5154 {
5155 /* If they don't overlap, the result is false. If the second range
5156 is a subset it is the result. Otherwise, the range is from the start
5157 of the second to the end of the first. */
5158 if (no_overlap)
5159 in_p = 0, low = high = 0;
5160 else if (subset)
5161 in_p = 1, low = low1, high = high1;
5162 else
5163 in_p = 1, low = low1, high = high0;
5164 }
5165
5166 else if (in0_p && ! in1_p)
5167 {
5168 /* If they don't overlap, the result is the first range. If they are
5169 equal, the result is false. If the second range is a subset of the
5170 first, and the ranges begin at the same place, we go from just after
5171 the end of the second range to the end of the first. If the second
5172 range is not a subset of the first, or if it is a subset and both
5173 ranges end at the same place, the range starts at the start of the
5174 first range and ends just before the second range.
5175 Otherwise, we can't describe this as a single range. */
5176 if (no_overlap)
5177 in_p = 1, low = low0, high = high0;
5178 else if (lowequal && highequal)
5179 in_p = 0, low = high = 0;
5180 else if (subset && lowequal)
5181 {
5182 low = range_successor (high1);
5183 high = high0;
5184 in_p = 1;
5185 if (low == 0)
5186 {
5187 /* We are in the weird situation where high0 > high1 but
5188 high1 has no successor. Punt. */
5189 return 0;
5190 }
5191 }
5192 else if (! subset || highequal)
5193 {
5194 low = low0;
5195 high = range_predecessor (low1);
5196 in_p = 1;
5197 if (high == 0)
5198 {
5199 /* low0 < low1 but low1 has no predecessor. Punt. */
5200 return 0;
5201 }
5202 }
5203 else
5204 return 0;
5205 }
5206
5207 else if (! in0_p && in1_p)
5208 {
5209 /* If they don't overlap, the result is the second range. If the second
5210 is a subset of the first, the result is false. Otherwise,
5211 the range starts just after the first range and ends at the
5212 end of the second. */
5213 if (no_overlap)
5214 in_p = 1, low = low1, high = high1;
5215 else if (subset || highequal)
5216 in_p = 0, low = high = 0;
5217 else
5218 {
5219 low = range_successor (high0);
5220 high = high1;
5221 in_p = 1;
5222 if (low == 0)
5223 {
5224 /* high1 > high0 but high0 has no successor. Punt. */
5225 return 0;
5226 }
5227 }
5228 }
5229
5230 else
5231 {
5232 /* The case where we are excluding both ranges. Here the complex case
5233 is if they don't overlap. In that case, the only time we have a
5234 range is if they are adjacent. If the second is a subset of the
5235 first, the result is the first. Otherwise, the range to exclude
5236 starts at the beginning of the first range and ends at the end of the
5237 second. */
5238 if (no_overlap)
5239 {
5240 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5241 range_successor (high0),
5242 1, low1, 0)))
5243 in_p = 0, low = low0, high = high1;
5244 else
5245 {
5246 /* Canonicalize - [min, x] into - [-, x]. */
5247 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5248 switch (TREE_CODE (TREE_TYPE (low0)))
5249 {
5250 case ENUMERAL_TYPE:
5251 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5252 GET_MODE_BITSIZE
5253 (TYPE_MODE (TREE_TYPE (low0)))))
5254 break;
5255 /* FALLTHROUGH */
5256 case INTEGER_TYPE:
5257 if (tree_int_cst_equal (low0,
5258 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5259 low0 = 0;
5260 break;
5261 case POINTER_TYPE:
5262 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5263 && integer_zerop (low0))
5264 low0 = 0;
5265 break;
5266 default:
5267 break;
5268 }
5269
5270 /* Canonicalize - [x, max] into - [x, -]. */
5271 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5272 switch (TREE_CODE (TREE_TYPE (high1)))
5273 {
5274 case ENUMERAL_TYPE:
5275 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5276 GET_MODE_BITSIZE
5277 (TYPE_MODE (TREE_TYPE (high1)))))
5278 break;
5279 /* FALLTHROUGH */
5280 case INTEGER_TYPE:
5281 if (tree_int_cst_equal (high1,
5282 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5283 high1 = 0;
5284 break;
5285 case POINTER_TYPE:
5286 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5287 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5288 high1, 1,
5289 build_int_cst (TREE_TYPE (high1), 1),
5290 1)))
5291 high1 = 0;
5292 break;
5293 default:
5294 break;
5295 }
5296
5297 /* The ranges might be also adjacent between the maximum and
5298 minimum values of the given type. For
5299 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5300 return + [x + 1, y - 1]. */
5301 if (low0 == 0 && high1 == 0)
5302 {
5303 low = range_successor (high0);
5304 high = range_predecessor (low1);
5305 if (low == 0 || high == 0)
5306 return 0;
5307
5308 in_p = 1;
5309 }
5310 else
5311 return 0;
5312 }
5313 }
5314 else if (subset)
5315 in_p = 0, low = low0, high = high0;
5316 else
5317 in_p = 0, low = low0, high = high1;
5318 }
5319
5320 *pin_p = in_p, *plow = low, *phigh = high;
5321 return 1;
5322 }
5323 \f
5324
5325 /* Subroutine of fold, looking inside expressions of the form
5326 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5327 of the COND_EXPR. This function is being used also to optimize
5328 A op B ? C : A, by reversing the comparison first.
5329
5330 Return a folded expression whose code is not a COND_EXPR
5331 anymore, or NULL_TREE if no folding opportunity is found. */
5332
5333 static tree
5334 fold_cond_expr_with_comparison (location_t loc, tree type,
5335 tree arg0, tree arg1, tree arg2)
5336 {
5337 enum tree_code comp_code = TREE_CODE (arg0);
5338 tree arg00 = TREE_OPERAND (arg0, 0);
5339 tree arg01 = TREE_OPERAND (arg0, 1);
5340 tree arg1_type = TREE_TYPE (arg1);
5341 tree tem;
5342
5343 STRIP_NOPS (arg1);
5344 STRIP_NOPS (arg2);
5345
5346 /* If we have A op 0 ? A : -A, consider applying the following
5347 transformations:
5348
5349 A == 0? A : -A same as -A
5350 A != 0? A : -A same as A
5351 A >= 0? A : -A same as abs (A)
5352 A > 0? A : -A same as abs (A)
5353 A <= 0? A : -A same as -abs (A)
5354 A < 0? A : -A same as -abs (A)
5355
5356 None of these transformations work for modes with signed
5357 zeros. If A is +/-0, the first two transformations will
5358 change the sign of the result (from +0 to -0, or vice
5359 versa). The last four will fix the sign of the result,
5360 even though the original expressions could be positive or
5361 negative, depending on the sign of A.
5362
5363 Note that all these transformations are correct if A is
5364 NaN, since the two alternatives (A and -A) are also NaNs. */
5365 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5366 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5367 ? real_zerop (arg01)
5368 : integer_zerop (arg01))
5369 && ((TREE_CODE (arg2) == NEGATE_EXPR
5370 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5371 /* In the case that A is of the form X-Y, '-A' (arg2) may
5372 have already been folded to Y-X, check for that. */
5373 || (TREE_CODE (arg1) == MINUS_EXPR
5374 && TREE_CODE (arg2) == MINUS_EXPR
5375 && operand_equal_p (TREE_OPERAND (arg1, 0),
5376 TREE_OPERAND (arg2, 1), 0)
5377 && operand_equal_p (TREE_OPERAND (arg1, 1),
5378 TREE_OPERAND (arg2, 0), 0))))
5379 switch (comp_code)
5380 {
5381 case EQ_EXPR:
5382 case UNEQ_EXPR:
5383 tem = fold_convert_loc (loc, arg1_type, arg1);
5384 return fold_convert_loc (loc, type, negate_expr (tem));
5385 case NE_EXPR:
5386 case LTGT_EXPR:
5387 return fold_convert_loc (loc, type, arg1);
5388 case UNGE_EXPR:
5389 case UNGT_EXPR:
5390 if (flag_trapping_math)
5391 break;
5392 /* Fall through. */
5393 case GE_EXPR:
5394 case GT_EXPR:
5395 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5396 break;
5397 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5398 return fold_convert_loc (loc, type, tem);
5399 case UNLE_EXPR:
5400 case UNLT_EXPR:
5401 if (flag_trapping_math)
5402 break;
5403 /* FALLTHRU */
5404 case LE_EXPR:
5405 case LT_EXPR:
5406 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5407 break;
5408 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5409 return negate_expr (fold_convert_loc (loc, type, tem));
5410 default:
5411 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5412 break;
5413 }
5414
5415 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5416 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5417 both transformations are correct when A is NaN: A != 0
5418 is then true, and A == 0 is false. */
5419
5420 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5421 && integer_zerop (arg01) && integer_zerop (arg2))
5422 {
5423 if (comp_code == NE_EXPR)
5424 return fold_convert_loc (loc, type, arg1);
5425 else if (comp_code == EQ_EXPR)
5426 return build_zero_cst (type);
5427 }
5428
5429 /* Try some transformations of A op B ? A : B.
5430
5431 A == B? A : B same as B
5432 A != B? A : B same as A
5433 A >= B? A : B same as max (A, B)
5434 A > B? A : B same as max (B, A)
5435 A <= B? A : B same as min (A, B)
5436 A < B? A : B same as min (B, A)
5437
5438 As above, these transformations don't work in the presence
5439 of signed zeros. For example, if A and B are zeros of
5440 opposite sign, the first two transformations will change
5441 the sign of the result. In the last four, the original
5442 expressions give different results for (A=+0, B=-0) and
5443 (A=-0, B=+0), but the transformed expressions do not.
5444
5445 The first two transformations are correct if either A or B
5446 is a NaN. In the first transformation, the condition will
5447 be false, and B will indeed be chosen. In the case of the
5448 second transformation, the condition A != B will be true,
5449 and A will be chosen.
5450
5451 The conversions to max() and min() are not correct if B is
5452 a number and A is not. The conditions in the original
5453 expressions will be false, so all four give B. The min()
5454 and max() versions would give a NaN instead. */
5455 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5456 && operand_equal_for_comparison_p (arg01, arg2)
5457 /* Avoid these transformations if the COND_EXPR may be used
5458 as an lvalue in the C++ front-end. PR c++/19199. */
5459 && (in_gimple_form
5460 || VECTOR_TYPE_P (type)
5461 || (! lang_GNU_CXX ()
5462 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5463 || ! maybe_lvalue_p (arg1)
5464 || ! maybe_lvalue_p (arg2)))
5465 {
5466 tree comp_op0 = arg00;
5467 tree comp_op1 = arg01;
5468 tree comp_type = TREE_TYPE (comp_op0);
5469
5470 switch (comp_code)
5471 {
5472 case EQ_EXPR:
5473 return fold_convert_loc (loc, type, arg2);
5474 case NE_EXPR:
5475 return fold_convert_loc (loc, type, arg1);
5476 case LE_EXPR:
5477 case LT_EXPR:
5478 case UNLE_EXPR:
5479 case UNLT_EXPR:
5480 /* In C++ a ?: expression can be an lvalue, so put the
5481 operand which will be used if they are equal first
5482 so that we can convert this back to the
5483 corresponding COND_EXPR. */
5484 if (!HONOR_NANS (arg1))
5485 {
5486 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5487 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5488 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5489 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5490 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5491 comp_op1, comp_op0);
5492 return fold_convert_loc (loc, type, tem);
5493 }
5494 break;
5495 case GE_EXPR:
5496 case GT_EXPR:
5497 case UNGE_EXPR:
5498 case UNGT_EXPR:
5499 if (!HONOR_NANS (arg1))
5500 {
5501 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5502 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5503 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5504 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5505 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5506 comp_op1, comp_op0);
5507 return fold_convert_loc (loc, type, tem);
5508 }
5509 break;
5510 case UNEQ_EXPR:
5511 if (!HONOR_NANS (arg1))
5512 return fold_convert_loc (loc, type, arg2);
5513 break;
5514 case LTGT_EXPR:
5515 if (!HONOR_NANS (arg1))
5516 return fold_convert_loc (loc, type, arg1);
5517 break;
5518 default:
5519 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5520 break;
5521 }
5522 }
5523
5524 return NULL_TREE;
5525 }
5526
5527
5528 \f
5529 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5530 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5531 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5532 false) >= 2)
5533 #endif
5534
5535 /* EXP is some logical combination of boolean tests. See if we can
5536 merge it into some range test. Return the new tree if so. */
5537
5538 static tree
5539 fold_range_test (location_t loc, enum tree_code code, tree type,
5540 tree op0, tree op1)
5541 {
5542 int or_op = (code == TRUTH_ORIF_EXPR
5543 || code == TRUTH_OR_EXPR);
5544 int in0_p, in1_p, in_p;
5545 tree low0, low1, low, high0, high1, high;
5546 bool strict_overflow_p = false;
5547 tree tem, lhs, rhs;
5548 const char * const warnmsg = G_("assuming signed overflow does not occur "
5549 "when simplifying range test");
5550
5551 if (!INTEGRAL_TYPE_P (type))
5552 return 0;
5553
5554 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5555 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5556
5557 /* If this is an OR operation, invert both sides; we will invert
5558 again at the end. */
5559 if (or_op)
5560 in0_p = ! in0_p, in1_p = ! in1_p;
5561
5562 /* If both expressions are the same, if we can merge the ranges, and we
5563 can build the range test, return it or it inverted. If one of the
5564 ranges is always true or always false, consider it to be the same
5565 expression as the other. */
5566 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5567 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5568 in1_p, low1, high1)
5569 && (tem = (build_range_check (loc, type,
5570 lhs != 0 ? lhs
5571 : rhs != 0 ? rhs : integer_zero_node,
5572 in_p, low, high))) != 0)
5573 {
5574 if (strict_overflow_p)
5575 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5576 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5577 }
5578
5579 /* On machines where the branch cost is expensive, if this is a
5580 short-circuited branch and the underlying object on both sides
5581 is the same, make a non-short-circuit operation. */
5582 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
5583 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
5584 logical_op_non_short_circuit
5585 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
5586 if (logical_op_non_short_circuit
5587 && !flag_sanitize_coverage
5588 && lhs != 0 && rhs != 0
5589 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5590 && operand_equal_p (lhs, rhs, 0))
5591 {
5592 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5593 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5594 which cases we can't do this. */
5595 if (simple_operand_p (lhs))
5596 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5597 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5598 type, op0, op1);
5599
5600 else if (!lang_hooks.decls.global_bindings_p ()
5601 && !CONTAINS_PLACEHOLDER_P (lhs))
5602 {
5603 tree common = save_expr (lhs);
5604
5605 if ((lhs = build_range_check (loc, type, common,
5606 or_op ? ! in0_p : in0_p,
5607 low0, high0)) != 0
5608 && (rhs = build_range_check (loc, type, common,
5609 or_op ? ! in1_p : in1_p,
5610 low1, high1)) != 0)
5611 {
5612 if (strict_overflow_p)
5613 fold_overflow_warning (warnmsg,
5614 WARN_STRICT_OVERFLOW_COMPARISON);
5615 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5616 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5617 type, lhs, rhs);
5618 }
5619 }
5620 }
5621
5622 return 0;
5623 }
5624 \f
5625 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5626 bit value. Arrange things so the extra bits will be set to zero if and
5627 only if C is signed-extended to its full width. If MASK is nonzero,
5628 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5629
5630 static tree
5631 unextend (tree c, int p, int unsignedp, tree mask)
5632 {
5633 tree type = TREE_TYPE (c);
5634 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5635 tree temp;
5636
5637 if (p == modesize || unsignedp)
5638 return c;
5639
5640 /* We work by getting just the sign bit into the low-order bit, then
5641 into the high-order bit, then sign-extend. We then XOR that value
5642 with C. */
5643 temp = build_int_cst (TREE_TYPE (c),
5644 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5645
5646 /* We must use a signed type in order to get an arithmetic right shift.
5647 However, we must also avoid introducing accidental overflows, so that
5648 a subsequent call to integer_zerop will work. Hence we must
5649 do the type conversion here. At this point, the constant is either
5650 zero or one, and the conversion to a signed type can never overflow.
5651 We could get an overflow if this conversion is done anywhere else. */
5652 if (TYPE_UNSIGNED (type))
5653 temp = fold_convert (signed_type_for (type), temp);
5654
5655 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5656 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5657 if (mask != 0)
5658 temp = const_binop (BIT_AND_EXPR, temp,
5659 fold_convert (TREE_TYPE (c), mask));
5660 /* If necessary, convert the type back to match the type of C. */
5661 if (TYPE_UNSIGNED (type))
5662 temp = fold_convert (type, temp);
5663
5664 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5665 }
5666 \f
5667 /* For an expression that has the form
5668 (A && B) || ~B
5669 or
5670 (A || B) && ~B,
5671 we can drop one of the inner expressions and simplify to
5672 A || ~B
5673 or
5674 A && ~B
5675 LOC is the location of the resulting expression. OP is the inner
5676 logical operation; the left-hand side in the examples above, while CMPOP
5677 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5678 removing a condition that guards another, as in
5679 (A != NULL && A->...) || A == NULL
5680 which we must not transform. If RHS_ONLY is true, only eliminate the
5681 right-most operand of the inner logical operation. */
5682
5683 static tree
5684 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5685 bool rhs_only)
5686 {
5687 tree type = TREE_TYPE (cmpop);
5688 enum tree_code code = TREE_CODE (cmpop);
5689 enum tree_code truthop_code = TREE_CODE (op);
5690 tree lhs = TREE_OPERAND (op, 0);
5691 tree rhs = TREE_OPERAND (op, 1);
5692 tree orig_lhs = lhs, orig_rhs = rhs;
5693 enum tree_code rhs_code = TREE_CODE (rhs);
5694 enum tree_code lhs_code = TREE_CODE (lhs);
5695 enum tree_code inv_code;
5696
5697 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5698 return NULL_TREE;
5699
5700 if (TREE_CODE_CLASS (code) != tcc_comparison)
5701 return NULL_TREE;
5702
5703 if (rhs_code == truthop_code)
5704 {
5705 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5706 if (newrhs != NULL_TREE)
5707 {
5708 rhs = newrhs;
5709 rhs_code = TREE_CODE (rhs);
5710 }
5711 }
5712 if (lhs_code == truthop_code && !rhs_only)
5713 {
5714 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5715 if (newlhs != NULL_TREE)
5716 {
5717 lhs = newlhs;
5718 lhs_code = TREE_CODE (lhs);
5719 }
5720 }
5721
5722 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5723 if (inv_code == rhs_code
5724 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5725 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5726 return lhs;
5727 if (!rhs_only && inv_code == lhs_code
5728 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5729 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5730 return rhs;
5731 if (rhs != orig_rhs || lhs != orig_lhs)
5732 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5733 lhs, rhs);
5734 return NULL_TREE;
5735 }
5736
5737 /* Find ways of folding logical expressions of LHS and RHS:
5738 Try to merge two comparisons to the same innermost item.
5739 Look for range tests like "ch >= '0' && ch <= '9'".
5740 Look for combinations of simple terms on machines with expensive branches
5741 and evaluate the RHS unconditionally.
5742
5743 For example, if we have p->a == 2 && p->b == 4 and we can make an
5744 object large enough to span both A and B, we can do this with a comparison
5745 against the object ANDed with the a mask.
5746
5747 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5748 operations to do this with one comparison.
5749
5750 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5751 function and the one above.
5752
5753 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5754 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5755
5756 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5757 two operands.
5758
5759 We return the simplified tree or 0 if no optimization is possible. */
5760
5761 static tree
5762 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5763 tree lhs, tree rhs)
5764 {
5765 /* If this is the "or" of two comparisons, we can do something if
5766 the comparisons are NE_EXPR. If this is the "and", we can do something
5767 if the comparisons are EQ_EXPR. I.e.,
5768 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5769
5770 WANTED_CODE is this operation code. For single bit fields, we can
5771 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5772 comparison for one-bit fields. */
5773
5774 enum tree_code wanted_code;
5775 enum tree_code lcode, rcode;
5776 tree ll_arg, lr_arg, rl_arg, rr_arg;
5777 tree ll_inner, lr_inner, rl_inner, rr_inner;
5778 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5779 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5780 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5781 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5782 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5783 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5784 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5785 scalar_int_mode lnmode, rnmode;
5786 tree ll_mask, lr_mask, rl_mask, rr_mask;
5787 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5788 tree l_const, r_const;
5789 tree lntype, rntype, result;
5790 HOST_WIDE_INT first_bit, end_bit;
5791 int volatilep;
5792
5793 /* Start by getting the comparison codes. Fail if anything is volatile.
5794 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5795 it were surrounded with a NE_EXPR. */
5796
5797 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5798 return 0;
5799
5800 lcode = TREE_CODE (lhs);
5801 rcode = TREE_CODE (rhs);
5802
5803 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5804 {
5805 lhs = build2 (NE_EXPR, truth_type, lhs,
5806 build_int_cst (TREE_TYPE (lhs), 0));
5807 lcode = NE_EXPR;
5808 }
5809
5810 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5811 {
5812 rhs = build2 (NE_EXPR, truth_type, rhs,
5813 build_int_cst (TREE_TYPE (rhs), 0));
5814 rcode = NE_EXPR;
5815 }
5816
5817 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5818 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5819 return 0;
5820
5821 ll_arg = TREE_OPERAND (lhs, 0);
5822 lr_arg = TREE_OPERAND (lhs, 1);
5823 rl_arg = TREE_OPERAND (rhs, 0);
5824 rr_arg = TREE_OPERAND (rhs, 1);
5825
5826 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5827 if (simple_operand_p (ll_arg)
5828 && simple_operand_p (lr_arg))
5829 {
5830 if (operand_equal_p (ll_arg, rl_arg, 0)
5831 && operand_equal_p (lr_arg, rr_arg, 0))
5832 {
5833 result = combine_comparisons (loc, code, lcode, rcode,
5834 truth_type, ll_arg, lr_arg);
5835 if (result)
5836 return result;
5837 }
5838 else if (operand_equal_p (ll_arg, rr_arg, 0)
5839 && operand_equal_p (lr_arg, rl_arg, 0))
5840 {
5841 result = combine_comparisons (loc, code, lcode,
5842 swap_tree_comparison (rcode),
5843 truth_type, ll_arg, lr_arg);
5844 if (result)
5845 return result;
5846 }
5847 }
5848
5849 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5850 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5851
5852 /* If the RHS can be evaluated unconditionally and its operands are
5853 simple, it wins to evaluate the RHS unconditionally on machines
5854 with expensive branches. In this case, this isn't a comparison
5855 that can be merged. */
5856
5857 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5858 false) >= 2
5859 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5860 && simple_operand_p (rl_arg)
5861 && simple_operand_p (rr_arg))
5862 {
5863 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5864 if (code == TRUTH_OR_EXPR
5865 && lcode == NE_EXPR && integer_zerop (lr_arg)
5866 && rcode == NE_EXPR && integer_zerop (rr_arg)
5867 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5868 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5869 return build2_loc (loc, NE_EXPR, truth_type,
5870 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5871 ll_arg, rl_arg),
5872 build_int_cst (TREE_TYPE (ll_arg), 0));
5873
5874 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5875 if (code == TRUTH_AND_EXPR
5876 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5877 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5878 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5879 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5880 return build2_loc (loc, EQ_EXPR, truth_type,
5881 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5882 ll_arg, rl_arg),
5883 build_int_cst (TREE_TYPE (ll_arg), 0));
5884 }
5885
5886 /* See if the comparisons can be merged. Then get all the parameters for
5887 each side. */
5888
5889 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5890 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5891 return 0;
5892
5893 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5894 volatilep = 0;
5895 ll_inner = decode_field_reference (loc, &ll_arg,
5896 &ll_bitsize, &ll_bitpos, &ll_mode,
5897 &ll_unsignedp, &ll_reversep, &volatilep,
5898 &ll_mask, &ll_and_mask);
5899 lr_inner = decode_field_reference (loc, &lr_arg,
5900 &lr_bitsize, &lr_bitpos, &lr_mode,
5901 &lr_unsignedp, &lr_reversep, &volatilep,
5902 &lr_mask, &lr_and_mask);
5903 rl_inner = decode_field_reference (loc, &rl_arg,
5904 &rl_bitsize, &rl_bitpos, &rl_mode,
5905 &rl_unsignedp, &rl_reversep, &volatilep,
5906 &rl_mask, &rl_and_mask);
5907 rr_inner = decode_field_reference (loc, &rr_arg,
5908 &rr_bitsize, &rr_bitpos, &rr_mode,
5909 &rr_unsignedp, &rr_reversep, &volatilep,
5910 &rr_mask, &rr_and_mask);
5911
5912 /* It must be true that the inner operation on the lhs of each
5913 comparison must be the same if we are to be able to do anything.
5914 Then see if we have constants. If not, the same must be true for
5915 the rhs's. */
5916 if (volatilep
5917 || ll_reversep != rl_reversep
5918 || ll_inner == 0 || rl_inner == 0
5919 || ! operand_equal_p (ll_inner, rl_inner, 0))
5920 return 0;
5921
5922 if (TREE_CODE (lr_arg) == INTEGER_CST
5923 && TREE_CODE (rr_arg) == INTEGER_CST)
5924 {
5925 l_const = lr_arg, r_const = rr_arg;
5926 lr_reversep = ll_reversep;
5927 }
5928 else if (lr_reversep != rr_reversep
5929 || lr_inner == 0 || rr_inner == 0
5930 || ! operand_equal_p (lr_inner, rr_inner, 0))
5931 return 0;
5932 else
5933 l_const = r_const = 0;
5934
5935 /* If either comparison code is not correct for our logical operation,
5936 fail. However, we can convert a one-bit comparison against zero into
5937 the opposite comparison against that bit being set in the field. */
5938
5939 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5940 if (lcode != wanted_code)
5941 {
5942 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5943 {
5944 /* Make the left operand unsigned, since we are only interested
5945 in the value of one bit. Otherwise we are doing the wrong
5946 thing below. */
5947 ll_unsignedp = 1;
5948 l_const = ll_mask;
5949 }
5950 else
5951 return 0;
5952 }
5953
5954 /* This is analogous to the code for l_const above. */
5955 if (rcode != wanted_code)
5956 {
5957 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5958 {
5959 rl_unsignedp = 1;
5960 r_const = rl_mask;
5961 }
5962 else
5963 return 0;
5964 }
5965
5966 /* See if we can find a mode that contains both fields being compared on
5967 the left. If we can't, fail. Otherwise, update all constants and masks
5968 to be relative to a field of that size. */
5969 first_bit = MIN (ll_bitpos, rl_bitpos);
5970 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5971 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5972 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5973 volatilep, &lnmode))
5974 return 0;
5975
5976 lnbitsize = GET_MODE_BITSIZE (lnmode);
5977 lnbitpos = first_bit & ~ (lnbitsize - 1);
5978 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5979 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5980
5981 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5982 {
5983 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5984 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5985 }
5986
5987 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5988 size_int (xll_bitpos));
5989 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5990 size_int (xrl_bitpos));
5991
5992 if (l_const)
5993 {
5994 l_const = fold_convert_loc (loc, lntype, l_const);
5995 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5996 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5997 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5998 fold_build1_loc (loc, BIT_NOT_EXPR,
5999 lntype, ll_mask))))
6000 {
6001 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6002
6003 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6004 }
6005 }
6006 if (r_const)
6007 {
6008 r_const = fold_convert_loc (loc, lntype, r_const);
6009 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6010 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
6011 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6012 fold_build1_loc (loc, BIT_NOT_EXPR,
6013 lntype, rl_mask))))
6014 {
6015 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6016
6017 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6018 }
6019 }
6020
6021 /* If the right sides are not constant, do the same for it. Also,
6022 disallow this optimization if a size, signedness or storage order
6023 mismatch occurs between the left and right sides. */
6024 if (l_const == 0)
6025 {
6026 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6027 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6028 || ll_reversep != lr_reversep
6029 /* Make sure the two fields on the right
6030 correspond to the left without being swapped. */
6031 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6032 return 0;
6033
6034 first_bit = MIN (lr_bitpos, rr_bitpos);
6035 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6036 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6037 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6038 volatilep, &rnmode))
6039 return 0;
6040
6041 rnbitsize = GET_MODE_BITSIZE (rnmode);
6042 rnbitpos = first_bit & ~ (rnbitsize - 1);
6043 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6044 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6045
6046 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6047 {
6048 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6049 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6050 }
6051
6052 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6053 rntype, lr_mask),
6054 size_int (xlr_bitpos));
6055 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6056 rntype, rr_mask),
6057 size_int (xrr_bitpos));
6058
6059 /* Make a mask that corresponds to both fields being compared.
6060 Do this for both items being compared. If the operands are the
6061 same size and the bits being compared are in the same position
6062 then we can do this by masking both and comparing the masked
6063 results. */
6064 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6065 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6066 if (lnbitsize == rnbitsize
6067 && xll_bitpos == xlr_bitpos
6068 && lnbitpos >= 0
6069 && rnbitpos >= 0)
6070 {
6071 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6072 lntype, lnbitsize, lnbitpos,
6073 ll_unsignedp || rl_unsignedp, ll_reversep);
6074 if (! all_ones_mask_p (ll_mask, lnbitsize))
6075 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6076
6077 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6078 rntype, rnbitsize, rnbitpos,
6079 lr_unsignedp || rr_unsignedp, lr_reversep);
6080 if (! all_ones_mask_p (lr_mask, rnbitsize))
6081 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6082
6083 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6084 }
6085
6086 /* There is still another way we can do something: If both pairs of
6087 fields being compared are adjacent, we may be able to make a wider
6088 field containing them both.
6089
6090 Note that we still must mask the lhs/rhs expressions. Furthermore,
6091 the mask must be shifted to account for the shift done by
6092 make_bit_field_ref. */
6093 if (((ll_bitsize + ll_bitpos == rl_bitpos
6094 && lr_bitsize + lr_bitpos == rr_bitpos)
6095 || (ll_bitpos == rl_bitpos + rl_bitsize
6096 && lr_bitpos == rr_bitpos + rr_bitsize))
6097 && ll_bitpos >= 0
6098 && rl_bitpos >= 0
6099 && lr_bitpos >= 0
6100 && rr_bitpos >= 0)
6101 {
6102 tree type;
6103
6104 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6105 ll_bitsize + rl_bitsize,
6106 MIN (ll_bitpos, rl_bitpos),
6107 ll_unsignedp, ll_reversep);
6108 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6109 lr_bitsize + rr_bitsize,
6110 MIN (lr_bitpos, rr_bitpos),
6111 lr_unsignedp, lr_reversep);
6112
6113 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6114 size_int (MIN (xll_bitpos, xrl_bitpos)));
6115 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6116 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6117
6118 /* Convert to the smaller type before masking out unwanted bits. */
6119 type = lntype;
6120 if (lntype != rntype)
6121 {
6122 if (lnbitsize > rnbitsize)
6123 {
6124 lhs = fold_convert_loc (loc, rntype, lhs);
6125 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6126 type = rntype;
6127 }
6128 else if (lnbitsize < rnbitsize)
6129 {
6130 rhs = fold_convert_loc (loc, lntype, rhs);
6131 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6132 type = lntype;
6133 }
6134 }
6135
6136 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6137 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6138
6139 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6140 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6141
6142 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6143 }
6144
6145 return 0;
6146 }
6147
6148 /* Handle the case of comparisons with constants. If there is something in
6149 common between the masks, those bits of the constants must be the same.
6150 If not, the condition is always false. Test for this to avoid generating
6151 incorrect code below. */
6152 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6153 if (! integer_zerop (result)
6154 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6155 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6156 {
6157 if (wanted_code == NE_EXPR)
6158 {
6159 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6160 return constant_boolean_node (true, truth_type);
6161 }
6162 else
6163 {
6164 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6165 return constant_boolean_node (false, truth_type);
6166 }
6167 }
6168
6169 if (lnbitpos < 0)
6170 return 0;
6171
6172 /* Construct the expression we will return. First get the component
6173 reference we will make. Unless the mask is all ones the width of
6174 that field, perform the mask operation. Then compare with the
6175 merged constant. */
6176 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6177 lntype, lnbitsize, lnbitpos,
6178 ll_unsignedp || rl_unsignedp, ll_reversep);
6179
6180 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6181 if (! all_ones_mask_p (ll_mask, lnbitsize))
6182 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6183
6184 return build2_loc (loc, wanted_code, truth_type, result,
6185 const_binop (BIT_IOR_EXPR, l_const, r_const));
6186 }
6187 \f
6188 /* T is an integer expression that is being multiplied, divided, or taken a
6189 modulus (CODE says which and what kind of divide or modulus) by a
6190 constant C. See if we can eliminate that operation by folding it with
6191 other operations already in T. WIDE_TYPE, if non-null, is a type that
6192 should be used for the computation if wider than our type.
6193
6194 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6195 (X * 2) + (Y * 4). We must, however, be assured that either the original
6196 expression would not overflow or that overflow is undefined for the type
6197 in the language in question.
6198
6199 If we return a non-null expression, it is an equivalent form of the
6200 original computation, but need not be in the original type.
6201
6202 We set *STRICT_OVERFLOW_P to true if the return values depends on
6203 signed overflow being undefined. Otherwise we do not change
6204 *STRICT_OVERFLOW_P. */
6205
6206 static tree
6207 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6208 bool *strict_overflow_p)
6209 {
6210 /* To avoid exponential search depth, refuse to allow recursion past
6211 three levels. Beyond that (1) it's highly unlikely that we'll find
6212 something interesting and (2) we've probably processed it before
6213 when we built the inner expression. */
6214
6215 static int depth;
6216 tree ret;
6217
6218 if (depth > 3)
6219 return NULL;
6220
6221 depth++;
6222 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6223 depth--;
6224
6225 return ret;
6226 }
6227
6228 static tree
6229 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6230 bool *strict_overflow_p)
6231 {
6232 tree type = TREE_TYPE (t);
6233 enum tree_code tcode = TREE_CODE (t);
6234 tree ctype = (wide_type != 0
6235 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6236 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6237 ? wide_type : type);
6238 tree t1, t2;
6239 int same_p = tcode == code;
6240 tree op0 = NULL_TREE, op1 = NULL_TREE;
6241 bool sub_strict_overflow_p;
6242
6243 /* Don't deal with constants of zero here; they confuse the code below. */
6244 if (integer_zerop (c))
6245 return NULL_TREE;
6246
6247 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6248 op0 = TREE_OPERAND (t, 0);
6249
6250 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6251 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6252
6253 /* Note that we need not handle conditional operations here since fold
6254 already handles those cases. So just do arithmetic here. */
6255 switch (tcode)
6256 {
6257 case INTEGER_CST:
6258 /* For a constant, we can always simplify if we are a multiply
6259 or (for divide and modulus) if it is a multiple of our constant. */
6260 if (code == MULT_EXPR
6261 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6262 TYPE_SIGN (type)))
6263 {
6264 tree tem = const_binop (code, fold_convert (ctype, t),
6265 fold_convert (ctype, c));
6266 /* If the multiplication overflowed, we lost information on it.
6267 See PR68142 and PR69845. */
6268 if (TREE_OVERFLOW (tem))
6269 return NULL_TREE;
6270 return tem;
6271 }
6272 break;
6273
6274 CASE_CONVERT: case NON_LVALUE_EXPR:
6275 /* If op0 is an expression ... */
6276 if ((COMPARISON_CLASS_P (op0)
6277 || UNARY_CLASS_P (op0)
6278 || BINARY_CLASS_P (op0)
6279 || VL_EXP_CLASS_P (op0)
6280 || EXPRESSION_CLASS_P (op0))
6281 /* ... and has wrapping overflow, and its type is smaller
6282 than ctype, then we cannot pass through as widening. */
6283 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6284 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6285 && (TYPE_PRECISION (ctype)
6286 > TYPE_PRECISION (TREE_TYPE (op0))))
6287 /* ... or this is a truncation (t is narrower than op0),
6288 then we cannot pass through this narrowing. */
6289 || (TYPE_PRECISION (type)
6290 < TYPE_PRECISION (TREE_TYPE (op0)))
6291 /* ... or signedness changes for division or modulus,
6292 then we cannot pass through this conversion. */
6293 || (code != MULT_EXPR
6294 && (TYPE_UNSIGNED (ctype)
6295 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6296 /* ... or has undefined overflow while the converted to
6297 type has not, we cannot do the operation in the inner type
6298 as that would introduce undefined overflow. */
6299 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6300 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6301 && !TYPE_OVERFLOW_UNDEFINED (type))))
6302 break;
6303
6304 /* Pass the constant down and see if we can make a simplification. If
6305 we can, replace this expression with the inner simplification for
6306 possible later conversion to our or some other type. */
6307 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6308 && TREE_CODE (t2) == INTEGER_CST
6309 && !TREE_OVERFLOW (t2)
6310 && (t1 = extract_muldiv (op0, t2, code,
6311 code == MULT_EXPR ? ctype : NULL_TREE,
6312 strict_overflow_p)) != 0)
6313 return t1;
6314 break;
6315
6316 case ABS_EXPR:
6317 /* If widening the type changes it from signed to unsigned, then we
6318 must avoid building ABS_EXPR itself as unsigned. */
6319 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6320 {
6321 tree cstype = (*signed_type_for) (ctype);
6322 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6323 != 0)
6324 {
6325 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6326 return fold_convert (ctype, t1);
6327 }
6328 break;
6329 }
6330 /* If the constant is negative, we cannot simplify this. */
6331 if (tree_int_cst_sgn (c) == -1)
6332 break;
6333 /* FALLTHROUGH */
6334 case NEGATE_EXPR:
6335 /* For division and modulus, type can't be unsigned, as e.g.
6336 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6337 For signed types, even with wrapping overflow, this is fine. */
6338 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6339 break;
6340 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6341 != 0)
6342 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6343 break;
6344
6345 case MIN_EXPR: case MAX_EXPR:
6346 /* If widening the type changes the signedness, then we can't perform
6347 this optimization as that changes the result. */
6348 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6349 break;
6350
6351 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6352 sub_strict_overflow_p = false;
6353 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6354 &sub_strict_overflow_p)) != 0
6355 && (t2 = extract_muldiv (op1, c, code, wide_type,
6356 &sub_strict_overflow_p)) != 0)
6357 {
6358 if (tree_int_cst_sgn (c) < 0)
6359 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6360 if (sub_strict_overflow_p)
6361 *strict_overflow_p = true;
6362 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6363 fold_convert (ctype, t2));
6364 }
6365 break;
6366
6367 case LSHIFT_EXPR: case RSHIFT_EXPR:
6368 /* If the second operand is constant, this is a multiplication
6369 or floor division, by a power of two, so we can treat it that
6370 way unless the multiplier or divisor overflows. Signed
6371 left-shift overflow is implementation-defined rather than
6372 undefined in C90, so do not convert signed left shift into
6373 multiplication. */
6374 if (TREE_CODE (op1) == INTEGER_CST
6375 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6376 /* const_binop may not detect overflow correctly,
6377 so check for it explicitly here. */
6378 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6379 wi::to_wide (op1))
6380 && (t1 = fold_convert (ctype,
6381 const_binop (LSHIFT_EXPR, size_one_node,
6382 op1))) != 0
6383 && !TREE_OVERFLOW (t1))
6384 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6385 ? MULT_EXPR : FLOOR_DIV_EXPR,
6386 ctype,
6387 fold_convert (ctype, op0),
6388 t1),
6389 c, code, wide_type, strict_overflow_p);
6390 break;
6391
6392 case PLUS_EXPR: case MINUS_EXPR:
6393 /* See if we can eliminate the operation on both sides. If we can, we
6394 can return a new PLUS or MINUS. If we can't, the only remaining
6395 cases where we can do anything are if the second operand is a
6396 constant. */
6397 sub_strict_overflow_p = false;
6398 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6399 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6400 if (t1 != 0 && t2 != 0
6401 && TYPE_OVERFLOW_WRAPS (ctype)
6402 && (code == MULT_EXPR
6403 /* If not multiplication, we can only do this if both operands
6404 are divisible by c. */
6405 || (multiple_of_p (ctype, op0, c)
6406 && multiple_of_p (ctype, op1, c))))
6407 {
6408 if (sub_strict_overflow_p)
6409 *strict_overflow_p = true;
6410 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6411 fold_convert (ctype, t2));
6412 }
6413
6414 /* If this was a subtraction, negate OP1 and set it to be an addition.
6415 This simplifies the logic below. */
6416 if (tcode == MINUS_EXPR)
6417 {
6418 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6419 /* If OP1 was not easily negatable, the constant may be OP0. */
6420 if (TREE_CODE (op0) == INTEGER_CST)
6421 {
6422 std::swap (op0, op1);
6423 std::swap (t1, t2);
6424 }
6425 }
6426
6427 if (TREE_CODE (op1) != INTEGER_CST)
6428 break;
6429
6430 /* If either OP1 or C are negative, this optimization is not safe for
6431 some of the division and remainder types while for others we need
6432 to change the code. */
6433 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6434 {
6435 if (code == CEIL_DIV_EXPR)
6436 code = FLOOR_DIV_EXPR;
6437 else if (code == FLOOR_DIV_EXPR)
6438 code = CEIL_DIV_EXPR;
6439 else if (code != MULT_EXPR
6440 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6441 break;
6442 }
6443
6444 /* If it's a multiply or a division/modulus operation of a multiple
6445 of our constant, do the operation and verify it doesn't overflow. */
6446 if (code == MULT_EXPR
6447 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6448 TYPE_SIGN (type)))
6449 {
6450 op1 = const_binop (code, fold_convert (ctype, op1),
6451 fold_convert (ctype, c));
6452 /* We allow the constant to overflow with wrapping semantics. */
6453 if (op1 == 0
6454 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6455 break;
6456 }
6457 else
6458 break;
6459
6460 /* If we have an unsigned type, we cannot widen the operation since it
6461 will change the result if the original computation overflowed. */
6462 if (TYPE_UNSIGNED (ctype) && ctype != type)
6463 break;
6464
6465 /* The last case is if we are a multiply. In that case, we can
6466 apply the distributive law to commute the multiply and addition
6467 if the multiplication of the constants doesn't overflow
6468 and overflow is defined. With undefined overflow
6469 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6470 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6471 return fold_build2 (tcode, ctype,
6472 fold_build2 (code, ctype,
6473 fold_convert (ctype, op0),
6474 fold_convert (ctype, c)),
6475 op1);
6476
6477 break;
6478
6479 case MULT_EXPR:
6480 /* We have a special case here if we are doing something like
6481 (C * 8) % 4 since we know that's zero. */
6482 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6483 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6484 /* If the multiplication can overflow we cannot optimize this. */
6485 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6486 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6487 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6488 TYPE_SIGN (type)))
6489 {
6490 *strict_overflow_p = true;
6491 return omit_one_operand (type, integer_zero_node, op0);
6492 }
6493
6494 /* ... fall through ... */
6495
6496 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6497 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6498 /* If we can extract our operation from the LHS, do so and return a
6499 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6500 do something only if the second operand is a constant. */
6501 if (same_p
6502 && TYPE_OVERFLOW_WRAPS (ctype)
6503 && (t1 = extract_muldiv (op0, c, code, wide_type,
6504 strict_overflow_p)) != 0)
6505 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6506 fold_convert (ctype, op1));
6507 else if (tcode == MULT_EXPR && code == MULT_EXPR
6508 && TYPE_OVERFLOW_WRAPS (ctype)
6509 && (t1 = extract_muldiv (op1, c, code, wide_type,
6510 strict_overflow_p)) != 0)
6511 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6512 fold_convert (ctype, t1));
6513 else if (TREE_CODE (op1) != INTEGER_CST)
6514 return 0;
6515
6516 /* If these are the same operation types, we can associate them
6517 assuming no overflow. */
6518 if (tcode == code)
6519 {
6520 bool overflow_p = false;
6521 wi::overflow_type overflow_mul;
6522 signop sign = TYPE_SIGN (ctype);
6523 unsigned prec = TYPE_PRECISION (ctype);
6524 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6525 wi::to_wide (c, prec),
6526 sign, &overflow_mul);
6527 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6528 if (overflow_mul
6529 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6530 overflow_p = true;
6531 if (!overflow_p)
6532 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6533 wide_int_to_tree (ctype, mul));
6534 }
6535
6536 /* If these operations "cancel" each other, we have the main
6537 optimizations of this pass, which occur when either constant is a
6538 multiple of the other, in which case we replace this with either an
6539 operation or CODE or TCODE.
6540
6541 If we have an unsigned type, we cannot do this since it will change
6542 the result if the original computation overflowed. */
6543 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6544 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6545 || (tcode == MULT_EXPR
6546 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6547 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6548 && code != MULT_EXPR)))
6549 {
6550 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6551 TYPE_SIGN (type)))
6552 {
6553 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6554 *strict_overflow_p = true;
6555 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6556 fold_convert (ctype,
6557 const_binop (TRUNC_DIV_EXPR,
6558 op1, c)));
6559 }
6560 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6561 TYPE_SIGN (type)))
6562 {
6563 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6564 *strict_overflow_p = true;
6565 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6566 fold_convert (ctype,
6567 const_binop (TRUNC_DIV_EXPR,
6568 c, op1)));
6569 }
6570 }
6571 break;
6572
6573 default:
6574 break;
6575 }
6576
6577 return 0;
6578 }
6579 \f
6580 /* Return a node which has the indicated constant VALUE (either 0 or
6581 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6582 and is of the indicated TYPE. */
6583
6584 tree
6585 constant_boolean_node (bool value, tree type)
6586 {
6587 if (type == integer_type_node)
6588 return value ? integer_one_node : integer_zero_node;
6589 else if (type == boolean_type_node)
6590 return value ? boolean_true_node : boolean_false_node;
6591 else if (TREE_CODE (type) == VECTOR_TYPE)
6592 return build_vector_from_val (type,
6593 build_int_cst (TREE_TYPE (type),
6594 value ? -1 : 0));
6595 else
6596 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6597 }
6598
6599
6600 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6601 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6602 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6603 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6604 COND is the first argument to CODE; otherwise (as in the example
6605 given here), it is the second argument. TYPE is the type of the
6606 original expression. Return NULL_TREE if no simplification is
6607 possible. */
6608
6609 static tree
6610 fold_binary_op_with_conditional_arg (location_t loc,
6611 enum tree_code code,
6612 tree type, tree op0, tree op1,
6613 tree cond, tree arg, int cond_first_p)
6614 {
6615 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6616 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6617 tree test, true_value, false_value;
6618 tree lhs = NULL_TREE;
6619 tree rhs = NULL_TREE;
6620 enum tree_code cond_code = COND_EXPR;
6621
6622 /* Do not move possibly trapping operations into the conditional as this
6623 pessimizes code and causes gimplification issues when applied late. */
6624 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6625 ANY_INTEGRAL_TYPE_P (type)
6626 && TYPE_OVERFLOW_TRAPS (type), op1))
6627 return NULL_TREE;
6628
6629 if (TREE_CODE (cond) == COND_EXPR
6630 || TREE_CODE (cond) == VEC_COND_EXPR)
6631 {
6632 test = TREE_OPERAND (cond, 0);
6633 true_value = TREE_OPERAND (cond, 1);
6634 false_value = TREE_OPERAND (cond, 2);
6635 /* If this operand throws an expression, then it does not make
6636 sense to try to perform a logical or arithmetic operation
6637 involving it. */
6638 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6639 lhs = true_value;
6640 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6641 rhs = false_value;
6642 }
6643 else if (!(TREE_CODE (type) != VECTOR_TYPE
6644 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6645 {
6646 tree testtype = TREE_TYPE (cond);
6647 test = cond;
6648 true_value = constant_boolean_node (true, testtype);
6649 false_value = constant_boolean_node (false, testtype);
6650 }
6651 else
6652 /* Detect the case of mixing vector and scalar types - bail out. */
6653 return NULL_TREE;
6654
6655 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6656 cond_code = VEC_COND_EXPR;
6657
6658 /* This transformation is only worthwhile if we don't have to wrap ARG
6659 in a SAVE_EXPR and the operation can be simplified without recursing
6660 on at least one of the branches once its pushed inside the COND_EXPR. */
6661 if (!TREE_CONSTANT (arg)
6662 && (TREE_SIDE_EFFECTS (arg)
6663 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6664 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6665 return NULL_TREE;
6666
6667 arg = fold_convert_loc (loc, arg_type, arg);
6668 if (lhs == 0)
6669 {
6670 true_value = fold_convert_loc (loc, cond_type, true_value);
6671 if (cond_first_p)
6672 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6673 else
6674 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6675 }
6676 if (rhs == 0)
6677 {
6678 false_value = fold_convert_loc (loc, cond_type, false_value);
6679 if (cond_first_p)
6680 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6681 else
6682 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6683 }
6684
6685 /* Check that we have simplified at least one of the branches. */
6686 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6687 return NULL_TREE;
6688
6689 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6690 }
6691
6692 \f
6693 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6694
6695 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6696 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6697 ADDEND is the same as X.
6698
6699 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6700 and finite. The problematic cases are when X is zero, and its mode
6701 has signed zeros. In the case of rounding towards -infinity,
6702 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6703 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6704
6705 bool
6706 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6707 {
6708 if (!real_zerop (addend))
6709 return false;
6710
6711 /* Don't allow the fold with -fsignaling-nans. */
6712 if (HONOR_SNANS (element_mode (type)))
6713 return false;
6714
6715 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6716 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6717 return true;
6718
6719 /* In a vector or complex, we would need to check the sign of all zeros. */
6720 if (TREE_CODE (addend) != REAL_CST)
6721 return false;
6722
6723 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6724 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6725 negate = !negate;
6726
6727 /* The mode has signed zeros, and we have to honor their sign.
6728 In this situation, there is only one case we can return true for.
6729 X - 0 is the same as X unless rounding towards -infinity is
6730 supported. */
6731 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6732 }
6733
6734 /* Subroutine of match.pd that optimizes comparisons of a division by
6735 a nonzero integer constant against an integer constant, i.e.
6736 X/C1 op C2.
6737
6738 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6739 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6740
6741 enum tree_code
6742 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6743 tree *hi, bool *neg_overflow)
6744 {
6745 tree prod, tmp, type = TREE_TYPE (c1);
6746 signop sign = TYPE_SIGN (type);
6747 wi::overflow_type overflow;
6748
6749 /* We have to do this the hard way to detect unsigned overflow.
6750 prod = int_const_binop (MULT_EXPR, c1, c2); */
6751 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6752 prod = force_fit_type (type, val, -1, overflow);
6753 *neg_overflow = false;
6754
6755 if (sign == UNSIGNED)
6756 {
6757 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6758 *lo = prod;
6759
6760 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6761 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6762 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6763 }
6764 else if (tree_int_cst_sgn (c1) >= 0)
6765 {
6766 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6767 switch (tree_int_cst_sgn (c2))
6768 {
6769 case -1:
6770 *neg_overflow = true;
6771 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6772 *hi = prod;
6773 break;
6774
6775 case 0:
6776 *lo = fold_negate_const (tmp, type);
6777 *hi = tmp;
6778 break;
6779
6780 case 1:
6781 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6782 *lo = prod;
6783 break;
6784
6785 default:
6786 gcc_unreachable ();
6787 }
6788 }
6789 else
6790 {
6791 /* A negative divisor reverses the relational operators. */
6792 code = swap_tree_comparison (code);
6793
6794 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6795 switch (tree_int_cst_sgn (c2))
6796 {
6797 case -1:
6798 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6799 *lo = prod;
6800 break;
6801
6802 case 0:
6803 *hi = fold_negate_const (tmp, type);
6804 *lo = tmp;
6805 break;
6806
6807 case 1:
6808 *neg_overflow = true;
6809 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6810 *hi = prod;
6811 break;
6812
6813 default:
6814 gcc_unreachable ();
6815 }
6816 }
6817
6818 if (code != EQ_EXPR && code != NE_EXPR)
6819 return code;
6820
6821 if (TREE_OVERFLOW (*lo)
6822 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6823 *lo = NULL_TREE;
6824 if (TREE_OVERFLOW (*hi)
6825 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6826 *hi = NULL_TREE;
6827
6828 return code;
6829 }
6830
6831
6832 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6833 equality/inequality test, then return a simplified form of the test
6834 using a sign testing. Otherwise return NULL. TYPE is the desired
6835 result type. */
6836
6837 static tree
6838 fold_single_bit_test_into_sign_test (location_t loc,
6839 enum tree_code code, tree arg0, tree arg1,
6840 tree result_type)
6841 {
6842 /* If this is testing a single bit, we can optimize the test. */
6843 if ((code == NE_EXPR || code == EQ_EXPR)
6844 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6845 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6846 {
6847 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6848 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6849 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6850
6851 if (arg00 != NULL_TREE
6852 /* This is only a win if casting to a signed type is cheap,
6853 i.e. when arg00's type is not a partial mode. */
6854 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6855 {
6856 tree stype = signed_type_for (TREE_TYPE (arg00));
6857 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6858 result_type,
6859 fold_convert_loc (loc, stype, arg00),
6860 build_int_cst (stype, 0));
6861 }
6862 }
6863
6864 return NULL_TREE;
6865 }
6866
6867 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6868 equality/inequality test, then return a simplified form of
6869 the test using shifts and logical operations. Otherwise return
6870 NULL. TYPE is the desired result type. */
6871
6872 tree
6873 fold_single_bit_test (location_t loc, enum tree_code code,
6874 tree arg0, tree arg1, tree result_type)
6875 {
6876 /* If this is testing a single bit, we can optimize the test. */
6877 if ((code == NE_EXPR || code == EQ_EXPR)
6878 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6879 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6880 {
6881 tree inner = TREE_OPERAND (arg0, 0);
6882 tree type = TREE_TYPE (arg0);
6883 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6884 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6885 int ops_unsigned;
6886 tree signed_type, unsigned_type, intermediate_type;
6887 tree tem, one;
6888
6889 /* First, see if we can fold the single bit test into a sign-bit
6890 test. */
6891 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6892 result_type);
6893 if (tem)
6894 return tem;
6895
6896 /* Otherwise we have (A & C) != 0 where C is a single bit,
6897 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6898 Similarly for (A & C) == 0. */
6899
6900 /* If INNER is a right shift of a constant and it plus BITNUM does
6901 not overflow, adjust BITNUM and INNER. */
6902 if (TREE_CODE (inner) == RSHIFT_EXPR
6903 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6904 && bitnum < TYPE_PRECISION (type)
6905 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6906 TYPE_PRECISION (type) - bitnum))
6907 {
6908 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6909 inner = TREE_OPERAND (inner, 0);
6910 }
6911
6912 /* If we are going to be able to omit the AND below, we must do our
6913 operations as unsigned. If we must use the AND, we have a choice.
6914 Normally unsigned is faster, but for some machines signed is. */
6915 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6916 && !flag_syntax_only) ? 0 : 1;
6917
6918 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6919 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6920 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6921 inner = fold_convert_loc (loc, intermediate_type, inner);
6922
6923 if (bitnum != 0)
6924 inner = build2 (RSHIFT_EXPR, intermediate_type,
6925 inner, size_int (bitnum));
6926
6927 one = build_int_cst (intermediate_type, 1);
6928
6929 if (code == EQ_EXPR)
6930 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6931
6932 /* Put the AND last so it can combine with more things. */
6933 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6934
6935 /* Make sure to return the proper type. */
6936 inner = fold_convert_loc (loc, result_type, inner);
6937
6938 return inner;
6939 }
6940 return NULL_TREE;
6941 }
6942
6943 /* Test whether it is preferable two swap two operands, ARG0 and
6944 ARG1, for example because ARG0 is an integer constant and ARG1
6945 isn't. */
6946
6947 bool
6948 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6949 {
6950 if (CONSTANT_CLASS_P (arg1))
6951 return 0;
6952 if (CONSTANT_CLASS_P (arg0))
6953 return 1;
6954
6955 STRIP_NOPS (arg0);
6956 STRIP_NOPS (arg1);
6957
6958 if (TREE_CONSTANT (arg1))
6959 return 0;
6960 if (TREE_CONSTANT (arg0))
6961 return 1;
6962
6963 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6964 for commutative and comparison operators. Ensuring a canonical
6965 form allows the optimizers to find additional redundancies without
6966 having to explicitly check for both orderings. */
6967 if (TREE_CODE (arg0) == SSA_NAME
6968 && TREE_CODE (arg1) == SSA_NAME
6969 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6970 return 1;
6971
6972 /* Put SSA_NAMEs last. */
6973 if (TREE_CODE (arg1) == SSA_NAME)
6974 return 0;
6975 if (TREE_CODE (arg0) == SSA_NAME)
6976 return 1;
6977
6978 /* Put variables last. */
6979 if (DECL_P (arg1))
6980 return 0;
6981 if (DECL_P (arg0))
6982 return 1;
6983
6984 return 0;
6985 }
6986
6987
6988 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6989 means A >= Y && A != MAX, but in this case we know that
6990 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6991
6992 static tree
6993 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6994 {
6995 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6996
6997 if (TREE_CODE (bound) == LT_EXPR)
6998 a = TREE_OPERAND (bound, 0);
6999 else if (TREE_CODE (bound) == GT_EXPR)
7000 a = TREE_OPERAND (bound, 1);
7001 else
7002 return NULL_TREE;
7003
7004 typea = TREE_TYPE (a);
7005 if (!INTEGRAL_TYPE_P (typea)
7006 && !POINTER_TYPE_P (typea))
7007 return NULL_TREE;
7008
7009 if (TREE_CODE (ineq) == LT_EXPR)
7010 {
7011 a1 = TREE_OPERAND (ineq, 1);
7012 y = TREE_OPERAND (ineq, 0);
7013 }
7014 else if (TREE_CODE (ineq) == GT_EXPR)
7015 {
7016 a1 = TREE_OPERAND (ineq, 0);
7017 y = TREE_OPERAND (ineq, 1);
7018 }
7019 else
7020 return NULL_TREE;
7021
7022 if (TREE_TYPE (a1) != typea)
7023 return NULL_TREE;
7024
7025 if (POINTER_TYPE_P (typea))
7026 {
7027 /* Convert the pointer types into integer before taking the difference. */
7028 tree ta = fold_convert_loc (loc, ssizetype, a);
7029 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7030 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7031 }
7032 else
7033 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7034
7035 if (!diff || !integer_onep (diff))
7036 return NULL_TREE;
7037
7038 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7039 }
7040
7041 /* Fold a sum or difference of at least one multiplication.
7042 Returns the folded tree or NULL if no simplification could be made. */
7043
7044 static tree
7045 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7046 tree arg0, tree arg1)
7047 {
7048 tree arg00, arg01, arg10, arg11;
7049 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7050
7051 /* (A * C) +- (B * C) -> (A+-B) * C.
7052 (A * C) +- A -> A * (C+-1).
7053 We are most concerned about the case where C is a constant,
7054 but other combinations show up during loop reduction. Since
7055 it is not difficult, try all four possibilities. */
7056
7057 if (TREE_CODE (arg0) == MULT_EXPR)
7058 {
7059 arg00 = TREE_OPERAND (arg0, 0);
7060 arg01 = TREE_OPERAND (arg0, 1);
7061 }
7062 else if (TREE_CODE (arg0) == INTEGER_CST)
7063 {
7064 arg00 = build_one_cst (type);
7065 arg01 = arg0;
7066 }
7067 else
7068 {
7069 /* We cannot generate constant 1 for fract. */
7070 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7071 return NULL_TREE;
7072 arg00 = arg0;
7073 arg01 = build_one_cst (type);
7074 }
7075 if (TREE_CODE (arg1) == MULT_EXPR)
7076 {
7077 arg10 = TREE_OPERAND (arg1, 0);
7078 arg11 = TREE_OPERAND (arg1, 1);
7079 }
7080 else if (TREE_CODE (arg1) == INTEGER_CST)
7081 {
7082 arg10 = build_one_cst (type);
7083 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7084 the purpose of this canonicalization. */
7085 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7086 && negate_expr_p (arg1)
7087 && code == PLUS_EXPR)
7088 {
7089 arg11 = negate_expr (arg1);
7090 code = MINUS_EXPR;
7091 }
7092 else
7093 arg11 = arg1;
7094 }
7095 else
7096 {
7097 /* We cannot generate constant 1 for fract. */
7098 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7099 return NULL_TREE;
7100 arg10 = arg1;
7101 arg11 = build_one_cst (type);
7102 }
7103 same = NULL_TREE;
7104
7105 /* Prefer factoring a common non-constant. */
7106 if (operand_equal_p (arg00, arg10, 0))
7107 same = arg00, alt0 = arg01, alt1 = arg11;
7108 else if (operand_equal_p (arg01, arg11, 0))
7109 same = arg01, alt0 = arg00, alt1 = arg10;
7110 else if (operand_equal_p (arg00, arg11, 0))
7111 same = arg00, alt0 = arg01, alt1 = arg10;
7112 else if (operand_equal_p (arg01, arg10, 0))
7113 same = arg01, alt0 = arg00, alt1 = arg11;
7114
7115 /* No identical multiplicands; see if we can find a common
7116 power-of-two factor in non-power-of-two multiplies. This
7117 can help in multi-dimensional array access. */
7118 else if (tree_fits_shwi_p (arg01)
7119 && tree_fits_shwi_p (arg11))
7120 {
7121 HOST_WIDE_INT int01, int11, tmp;
7122 bool swap = false;
7123 tree maybe_same;
7124 int01 = tree_to_shwi (arg01);
7125 int11 = tree_to_shwi (arg11);
7126
7127 /* Move min of absolute values to int11. */
7128 if (absu_hwi (int01) < absu_hwi (int11))
7129 {
7130 tmp = int01, int01 = int11, int11 = tmp;
7131 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7132 maybe_same = arg01;
7133 swap = true;
7134 }
7135 else
7136 maybe_same = arg11;
7137
7138 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7139 /* The remainder should not be a constant, otherwise we
7140 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7141 increased the number of multiplications necessary. */
7142 && TREE_CODE (arg10) != INTEGER_CST)
7143 {
7144 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7145 build_int_cst (TREE_TYPE (arg00),
7146 int01 / int11));
7147 alt1 = arg10;
7148 same = maybe_same;
7149 if (swap)
7150 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7151 }
7152 }
7153
7154 if (!same)
7155 return NULL_TREE;
7156
7157 if (! ANY_INTEGRAL_TYPE_P (type)
7158 || TYPE_OVERFLOW_WRAPS (type)
7159 /* We are neither factoring zero nor minus one. */
7160 || TREE_CODE (same) == INTEGER_CST)
7161 return fold_build2_loc (loc, MULT_EXPR, type,
7162 fold_build2_loc (loc, code, type,
7163 fold_convert_loc (loc, type, alt0),
7164 fold_convert_loc (loc, type, alt1)),
7165 fold_convert_loc (loc, type, same));
7166
7167 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7168 same may be minus one and thus the multiplication may overflow. Perform
7169 the sum operation in an unsigned type. */
7170 tree utype = unsigned_type_for (type);
7171 tree tem = fold_build2_loc (loc, code, utype,
7172 fold_convert_loc (loc, utype, alt0),
7173 fold_convert_loc (loc, utype, alt1));
7174 /* If the sum evaluated to a constant that is not -INF the multiplication
7175 cannot overflow. */
7176 if (TREE_CODE (tem) == INTEGER_CST
7177 && (wi::to_wide (tem)
7178 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7179 return fold_build2_loc (loc, MULT_EXPR, type,
7180 fold_convert (type, tem), same);
7181
7182 /* Do not resort to unsigned multiplication because
7183 we lose the no-overflow property of the expression. */
7184 return NULL_TREE;
7185 }
7186
7187 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7188 specified by EXPR into the buffer PTR of length LEN bytes.
7189 Return the number of bytes placed in the buffer, or zero
7190 upon failure. */
7191
7192 static int
7193 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7194 {
7195 tree type = TREE_TYPE (expr);
7196 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7197 int byte, offset, word, words;
7198 unsigned char value;
7199
7200 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7201 return 0;
7202 if (off == -1)
7203 off = 0;
7204
7205 if (ptr == NULL)
7206 /* Dry run. */
7207 return MIN (len, total_bytes - off);
7208
7209 words = total_bytes / UNITS_PER_WORD;
7210
7211 for (byte = 0; byte < total_bytes; byte++)
7212 {
7213 int bitpos = byte * BITS_PER_UNIT;
7214 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7215 number of bytes. */
7216 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7217
7218 if (total_bytes > UNITS_PER_WORD)
7219 {
7220 word = byte / UNITS_PER_WORD;
7221 if (WORDS_BIG_ENDIAN)
7222 word = (words - 1) - word;
7223 offset = word * UNITS_PER_WORD;
7224 if (BYTES_BIG_ENDIAN)
7225 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7226 else
7227 offset += byte % UNITS_PER_WORD;
7228 }
7229 else
7230 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7231 if (offset >= off && offset - off < len)
7232 ptr[offset - off] = value;
7233 }
7234 return MIN (len, total_bytes - off);
7235 }
7236
7237
7238 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7239 specified by EXPR into the buffer PTR of length LEN bytes.
7240 Return the number of bytes placed in the buffer, or zero
7241 upon failure. */
7242
7243 static int
7244 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7245 {
7246 tree type = TREE_TYPE (expr);
7247 scalar_mode mode = SCALAR_TYPE_MODE (type);
7248 int total_bytes = GET_MODE_SIZE (mode);
7249 FIXED_VALUE_TYPE value;
7250 tree i_value, i_type;
7251
7252 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7253 return 0;
7254
7255 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7256
7257 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7258 return 0;
7259
7260 value = TREE_FIXED_CST (expr);
7261 i_value = double_int_to_tree (i_type, value.data);
7262
7263 return native_encode_int (i_value, ptr, len, off);
7264 }
7265
7266
7267 /* Subroutine of native_encode_expr. Encode the REAL_CST
7268 specified by EXPR into the buffer PTR of length LEN bytes.
7269 Return the number of bytes placed in the buffer, or zero
7270 upon failure. */
7271
7272 static int
7273 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7274 {
7275 tree type = TREE_TYPE (expr);
7276 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7277 int byte, offset, word, words, bitpos;
7278 unsigned char value;
7279
7280 /* There are always 32 bits in each long, no matter the size of
7281 the hosts long. We handle floating point representations with
7282 up to 192 bits. */
7283 long tmp[6];
7284
7285 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7286 return 0;
7287 if (off == -1)
7288 off = 0;
7289
7290 if (ptr == NULL)
7291 /* Dry run. */
7292 return MIN (len, total_bytes - off);
7293
7294 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7295
7296 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7297
7298 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7299 bitpos += BITS_PER_UNIT)
7300 {
7301 byte = (bitpos / BITS_PER_UNIT) & 3;
7302 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7303
7304 if (UNITS_PER_WORD < 4)
7305 {
7306 word = byte / UNITS_PER_WORD;
7307 if (WORDS_BIG_ENDIAN)
7308 word = (words - 1) - word;
7309 offset = word * UNITS_PER_WORD;
7310 if (BYTES_BIG_ENDIAN)
7311 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7312 else
7313 offset += byte % UNITS_PER_WORD;
7314 }
7315 else
7316 {
7317 offset = byte;
7318 if (BYTES_BIG_ENDIAN)
7319 {
7320 /* Reverse bytes within each long, or within the entire float
7321 if it's smaller than a long (for HFmode). */
7322 offset = MIN (3, total_bytes - 1) - offset;
7323 gcc_assert (offset >= 0);
7324 }
7325 }
7326 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7327 if (offset >= off
7328 && offset - off < len)
7329 ptr[offset - off] = value;
7330 }
7331 return MIN (len, total_bytes - off);
7332 }
7333
7334 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7335 specified by EXPR into the buffer PTR of length LEN bytes.
7336 Return the number of bytes placed in the buffer, or zero
7337 upon failure. */
7338
7339 static int
7340 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7341 {
7342 int rsize, isize;
7343 tree part;
7344
7345 part = TREE_REALPART (expr);
7346 rsize = native_encode_expr (part, ptr, len, off);
7347 if (off == -1 && rsize == 0)
7348 return 0;
7349 part = TREE_IMAGPART (expr);
7350 if (off != -1)
7351 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7352 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7353 len - rsize, off);
7354 if (off == -1 && isize != rsize)
7355 return 0;
7356 return rsize + isize;
7357 }
7358
7359
7360 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7361 specified by EXPR into the buffer PTR of length LEN bytes.
7362 Return the number of bytes placed in the buffer, or zero
7363 upon failure. */
7364
7365 static int
7366 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7367 {
7368 unsigned HOST_WIDE_INT i, count;
7369 int size, offset;
7370 tree itype, elem;
7371
7372 offset = 0;
7373 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7374 return 0;
7375 itype = TREE_TYPE (TREE_TYPE (expr));
7376 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7377 for (i = 0; i < count; i++)
7378 {
7379 if (off >= size)
7380 {
7381 off -= size;
7382 continue;
7383 }
7384 elem = VECTOR_CST_ELT (expr, i);
7385 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7386 len - offset, off);
7387 if ((off == -1 && res != size) || res == 0)
7388 return 0;
7389 offset += res;
7390 if (offset >= len)
7391 return (off == -1 && i < count - 1) ? 0 : offset;
7392 if (off != -1)
7393 off = 0;
7394 }
7395 return offset;
7396 }
7397
7398
7399 /* Subroutine of native_encode_expr. Encode the STRING_CST
7400 specified by EXPR into the buffer PTR of length LEN bytes.
7401 Return the number of bytes placed in the buffer, or zero
7402 upon failure. */
7403
7404 static int
7405 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7406 {
7407 tree type = TREE_TYPE (expr);
7408
7409 /* Wide-char strings are encoded in target byte-order so native
7410 encoding them is trivial. */
7411 if (BITS_PER_UNIT != CHAR_BIT
7412 || TREE_CODE (type) != ARRAY_TYPE
7413 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7414 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7415 return 0;
7416
7417 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7418 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7419 return 0;
7420 if (off == -1)
7421 off = 0;
7422 if (ptr == NULL)
7423 /* Dry run. */;
7424 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7425 {
7426 int written = 0;
7427 if (off < TREE_STRING_LENGTH (expr))
7428 {
7429 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7430 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7431 }
7432 memset (ptr + written, 0,
7433 MIN (total_bytes - written, len - written));
7434 }
7435 else
7436 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7437 return MIN (total_bytes - off, len);
7438 }
7439
7440
7441 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7442 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7443 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7444 anything, just do a dry run. If OFF is not -1 then start
7445 the encoding at byte offset OFF and encode at most LEN bytes.
7446 Return the number of bytes placed in the buffer, or zero upon failure. */
7447
7448 int
7449 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7450 {
7451 /* We don't support starting at negative offset and -1 is special. */
7452 if (off < -1)
7453 return 0;
7454
7455 switch (TREE_CODE (expr))
7456 {
7457 case INTEGER_CST:
7458 return native_encode_int (expr, ptr, len, off);
7459
7460 case REAL_CST:
7461 return native_encode_real (expr, ptr, len, off);
7462
7463 case FIXED_CST:
7464 return native_encode_fixed (expr, ptr, len, off);
7465
7466 case COMPLEX_CST:
7467 return native_encode_complex (expr, ptr, len, off);
7468
7469 case VECTOR_CST:
7470 return native_encode_vector (expr, ptr, len, off);
7471
7472 case STRING_CST:
7473 return native_encode_string (expr, ptr, len, off);
7474
7475 default:
7476 return 0;
7477 }
7478 }
7479
7480
7481 /* Subroutine of native_interpret_expr. Interpret the contents of
7482 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7483 If the buffer cannot be interpreted, return NULL_TREE. */
7484
7485 static tree
7486 native_interpret_int (tree type, const unsigned char *ptr, int len)
7487 {
7488 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7489
7490 if (total_bytes > len
7491 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7492 return NULL_TREE;
7493
7494 wide_int result = wi::from_buffer (ptr, total_bytes);
7495
7496 return wide_int_to_tree (type, result);
7497 }
7498
7499
7500 /* Subroutine of native_interpret_expr. Interpret the contents of
7501 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7502 If the buffer cannot be interpreted, return NULL_TREE. */
7503
7504 static tree
7505 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7506 {
7507 scalar_mode mode = SCALAR_TYPE_MODE (type);
7508 int total_bytes = GET_MODE_SIZE (mode);
7509 double_int result;
7510 FIXED_VALUE_TYPE fixed_value;
7511
7512 if (total_bytes > len
7513 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7514 return NULL_TREE;
7515
7516 result = double_int::from_buffer (ptr, total_bytes);
7517 fixed_value = fixed_from_double_int (result, mode);
7518
7519 return build_fixed (type, fixed_value);
7520 }
7521
7522
7523 /* Subroutine of native_interpret_expr. Interpret the contents of
7524 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7525 If the buffer cannot be interpreted, return NULL_TREE. */
7526
7527 static tree
7528 native_interpret_real (tree type, const unsigned char *ptr, int len)
7529 {
7530 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7531 int total_bytes = GET_MODE_SIZE (mode);
7532 unsigned char value;
7533 /* There are always 32 bits in each long, no matter the size of
7534 the hosts long. We handle floating point representations with
7535 up to 192 bits. */
7536 REAL_VALUE_TYPE r;
7537 long tmp[6];
7538
7539 if (total_bytes > len || total_bytes > 24)
7540 return NULL_TREE;
7541 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7542
7543 memset (tmp, 0, sizeof (tmp));
7544 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7545 bitpos += BITS_PER_UNIT)
7546 {
7547 /* Both OFFSET and BYTE index within a long;
7548 bitpos indexes the whole float. */
7549 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7550 if (UNITS_PER_WORD < 4)
7551 {
7552 int word = byte / UNITS_PER_WORD;
7553 if (WORDS_BIG_ENDIAN)
7554 word = (words - 1) - word;
7555 offset = word * UNITS_PER_WORD;
7556 if (BYTES_BIG_ENDIAN)
7557 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7558 else
7559 offset += byte % UNITS_PER_WORD;
7560 }
7561 else
7562 {
7563 offset = byte;
7564 if (BYTES_BIG_ENDIAN)
7565 {
7566 /* Reverse bytes within each long, or within the entire float
7567 if it's smaller than a long (for HFmode). */
7568 offset = MIN (3, total_bytes - 1) - offset;
7569 gcc_assert (offset >= 0);
7570 }
7571 }
7572 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7573
7574 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7575 }
7576
7577 real_from_target (&r, tmp, mode);
7578 return build_real (type, r);
7579 }
7580
7581
7582 /* Subroutine of native_interpret_expr. Interpret the contents of
7583 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7584 If the buffer cannot be interpreted, return NULL_TREE. */
7585
7586 static tree
7587 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7588 {
7589 tree etype, rpart, ipart;
7590 int size;
7591
7592 etype = TREE_TYPE (type);
7593 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7594 if (size * 2 > len)
7595 return NULL_TREE;
7596 rpart = native_interpret_expr (etype, ptr, size);
7597 if (!rpart)
7598 return NULL_TREE;
7599 ipart = native_interpret_expr (etype, ptr+size, size);
7600 if (!ipart)
7601 return NULL_TREE;
7602 return build_complex (type, rpart, ipart);
7603 }
7604
7605
7606 /* Subroutine of native_interpret_expr. Interpret the contents of
7607 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7608 If the buffer cannot be interpreted, return NULL_TREE. */
7609
7610 static tree
7611 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7612 {
7613 tree etype, elem;
7614 unsigned int i, size;
7615 unsigned HOST_WIDE_INT count;
7616
7617 etype = TREE_TYPE (type);
7618 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7619 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7620 || size * count > len)
7621 return NULL_TREE;
7622
7623 tree_vector_builder elements (type, count, 1);
7624 for (i = 0; i < count; ++i)
7625 {
7626 elem = native_interpret_expr (etype, ptr+(i*size), size);
7627 if (!elem)
7628 return NULL_TREE;
7629 elements.quick_push (elem);
7630 }
7631 return elements.build ();
7632 }
7633
7634
7635 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7636 the buffer PTR of length LEN as a constant of type TYPE. For
7637 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7638 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7639 return NULL_TREE. */
7640
7641 tree
7642 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7643 {
7644 switch (TREE_CODE (type))
7645 {
7646 case INTEGER_TYPE:
7647 case ENUMERAL_TYPE:
7648 case BOOLEAN_TYPE:
7649 case POINTER_TYPE:
7650 case REFERENCE_TYPE:
7651 return native_interpret_int (type, ptr, len);
7652
7653 case REAL_TYPE:
7654 return native_interpret_real (type, ptr, len);
7655
7656 case FIXED_POINT_TYPE:
7657 return native_interpret_fixed (type, ptr, len);
7658
7659 case COMPLEX_TYPE:
7660 return native_interpret_complex (type, ptr, len);
7661
7662 case VECTOR_TYPE:
7663 return native_interpret_vector (type, ptr, len);
7664
7665 default:
7666 return NULL_TREE;
7667 }
7668 }
7669
7670 /* Returns true if we can interpret the contents of a native encoding
7671 as TYPE. */
7672
7673 static bool
7674 can_native_interpret_type_p (tree type)
7675 {
7676 switch (TREE_CODE (type))
7677 {
7678 case INTEGER_TYPE:
7679 case ENUMERAL_TYPE:
7680 case BOOLEAN_TYPE:
7681 case POINTER_TYPE:
7682 case REFERENCE_TYPE:
7683 case FIXED_POINT_TYPE:
7684 case REAL_TYPE:
7685 case COMPLEX_TYPE:
7686 case VECTOR_TYPE:
7687 return true;
7688 default:
7689 return false;
7690 }
7691 }
7692
7693
7694 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7695 TYPE at compile-time. If we're unable to perform the conversion
7696 return NULL_TREE. */
7697
7698 static tree
7699 fold_view_convert_expr (tree type, tree expr)
7700 {
7701 /* We support up to 512-bit values (for V8DFmode). */
7702 unsigned char buffer[64];
7703 int len;
7704
7705 /* Check that the host and target are sane. */
7706 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7707 return NULL_TREE;
7708
7709 len = native_encode_expr (expr, buffer, sizeof (buffer));
7710 if (len == 0)
7711 return NULL_TREE;
7712
7713 return native_interpret_expr (type, buffer, len);
7714 }
7715
7716 /* Build an expression for the address of T. Folds away INDIRECT_REF
7717 to avoid confusing the gimplify process. */
7718
7719 tree
7720 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7721 {
7722 /* The size of the object is not relevant when talking about its address. */
7723 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7724 t = TREE_OPERAND (t, 0);
7725
7726 if (TREE_CODE (t) == INDIRECT_REF)
7727 {
7728 t = TREE_OPERAND (t, 0);
7729
7730 if (TREE_TYPE (t) != ptrtype)
7731 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7732 }
7733 else if (TREE_CODE (t) == MEM_REF
7734 && integer_zerop (TREE_OPERAND (t, 1)))
7735 return TREE_OPERAND (t, 0);
7736 else if (TREE_CODE (t) == MEM_REF
7737 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7738 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7739 TREE_OPERAND (t, 0),
7740 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7741 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7742 {
7743 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7744
7745 if (TREE_TYPE (t) != ptrtype)
7746 t = fold_convert_loc (loc, ptrtype, t);
7747 }
7748 else
7749 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7750
7751 return t;
7752 }
7753
7754 /* Build an expression for the address of T. */
7755
7756 tree
7757 build_fold_addr_expr_loc (location_t loc, tree t)
7758 {
7759 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7760
7761 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7762 }
7763
7764 /* Fold a unary expression of code CODE and type TYPE with operand
7765 OP0. Return the folded expression if folding is successful.
7766 Otherwise, return NULL_TREE. */
7767
7768 tree
7769 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7770 {
7771 tree tem;
7772 tree arg0;
7773 enum tree_code_class kind = TREE_CODE_CLASS (code);
7774
7775 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7776 && TREE_CODE_LENGTH (code) == 1);
7777
7778 arg0 = op0;
7779 if (arg0)
7780 {
7781 if (CONVERT_EXPR_CODE_P (code)
7782 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7783 {
7784 /* Don't use STRIP_NOPS, because signedness of argument type
7785 matters. */
7786 STRIP_SIGN_NOPS (arg0);
7787 }
7788 else
7789 {
7790 /* Strip any conversions that don't change the mode. This
7791 is safe for every expression, except for a comparison
7792 expression because its signedness is derived from its
7793 operands.
7794
7795 Note that this is done as an internal manipulation within
7796 the constant folder, in order to find the simplest
7797 representation of the arguments so that their form can be
7798 studied. In any cases, the appropriate type conversions
7799 should be put back in the tree that will get out of the
7800 constant folder. */
7801 STRIP_NOPS (arg0);
7802 }
7803
7804 if (CONSTANT_CLASS_P (arg0))
7805 {
7806 tree tem = const_unop (code, type, arg0);
7807 if (tem)
7808 {
7809 if (TREE_TYPE (tem) != type)
7810 tem = fold_convert_loc (loc, type, tem);
7811 return tem;
7812 }
7813 }
7814 }
7815
7816 tem = generic_simplify (loc, code, type, op0);
7817 if (tem)
7818 return tem;
7819
7820 if (TREE_CODE_CLASS (code) == tcc_unary)
7821 {
7822 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7823 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7824 fold_build1_loc (loc, code, type,
7825 fold_convert_loc (loc, TREE_TYPE (op0),
7826 TREE_OPERAND (arg0, 1))));
7827 else if (TREE_CODE (arg0) == COND_EXPR)
7828 {
7829 tree arg01 = TREE_OPERAND (arg0, 1);
7830 tree arg02 = TREE_OPERAND (arg0, 2);
7831 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7832 arg01 = fold_build1_loc (loc, code, type,
7833 fold_convert_loc (loc,
7834 TREE_TYPE (op0), arg01));
7835 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7836 arg02 = fold_build1_loc (loc, code, type,
7837 fold_convert_loc (loc,
7838 TREE_TYPE (op0), arg02));
7839 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7840 arg01, arg02);
7841
7842 /* If this was a conversion, and all we did was to move into
7843 inside the COND_EXPR, bring it back out. But leave it if
7844 it is a conversion from integer to integer and the
7845 result precision is no wider than a word since such a
7846 conversion is cheap and may be optimized away by combine,
7847 while it couldn't if it were outside the COND_EXPR. Then return
7848 so we don't get into an infinite recursion loop taking the
7849 conversion out and then back in. */
7850
7851 if ((CONVERT_EXPR_CODE_P (code)
7852 || code == NON_LVALUE_EXPR)
7853 && TREE_CODE (tem) == COND_EXPR
7854 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7855 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7856 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7857 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7858 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7859 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7860 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7861 && (INTEGRAL_TYPE_P
7862 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7863 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7864 || flag_syntax_only))
7865 tem = build1_loc (loc, code, type,
7866 build3 (COND_EXPR,
7867 TREE_TYPE (TREE_OPERAND
7868 (TREE_OPERAND (tem, 1), 0)),
7869 TREE_OPERAND (tem, 0),
7870 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7871 TREE_OPERAND (TREE_OPERAND (tem, 2),
7872 0)));
7873 return tem;
7874 }
7875 }
7876
7877 switch (code)
7878 {
7879 case NON_LVALUE_EXPR:
7880 if (!maybe_lvalue_p (op0))
7881 return fold_convert_loc (loc, type, op0);
7882 return NULL_TREE;
7883
7884 CASE_CONVERT:
7885 case FLOAT_EXPR:
7886 case FIX_TRUNC_EXPR:
7887 if (COMPARISON_CLASS_P (op0))
7888 {
7889 /* If we have (type) (a CMP b) and type is an integral type, return
7890 new expression involving the new type. Canonicalize
7891 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7892 non-integral type.
7893 Do not fold the result as that would not simplify further, also
7894 folding again results in recursions. */
7895 if (TREE_CODE (type) == BOOLEAN_TYPE)
7896 return build2_loc (loc, TREE_CODE (op0), type,
7897 TREE_OPERAND (op0, 0),
7898 TREE_OPERAND (op0, 1));
7899 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7900 && TREE_CODE (type) != VECTOR_TYPE)
7901 return build3_loc (loc, COND_EXPR, type, op0,
7902 constant_boolean_node (true, type),
7903 constant_boolean_node (false, type));
7904 }
7905
7906 /* Handle (T *)&A.B.C for A being of type T and B and C
7907 living at offset zero. This occurs frequently in
7908 C++ upcasting and then accessing the base. */
7909 if (TREE_CODE (op0) == ADDR_EXPR
7910 && POINTER_TYPE_P (type)
7911 && handled_component_p (TREE_OPERAND (op0, 0)))
7912 {
7913 poly_int64 bitsize, bitpos;
7914 tree offset;
7915 machine_mode mode;
7916 int unsignedp, reversep, volatilep;
7917 tree base
7918 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7919 &offset, &mode, &unsignedp, &reversep,
7920 &volatilep);
7921 /* If the reference was to a (constant) zero offset, we can use
7922 the address of the base if it has the same base type
7923 as the result type and the pointer type is unqualified. */
7924 if (!offset
7925 && known_eq (bitpos, 0)
7926 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7927 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7928 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7929 return fold_convert_loc (loc, type,
7930 build_fold_addr_expr_loc (loc, base));
7931 }
7932
7933 if (TREE_CODE (op0) == MODIFY_EXPR
7934 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7935 /* Detect assigning a bitfield. */
7936 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7937 && DECL_BIT_FIELD
7938 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7939 {
7940 /* Don't leave an assignment inside a conversion
7941 unless assigning a bitfield. */
7942 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7943 /* First do the assignment, then return converted constant. */
7944 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7945 TREE_NO_WARNING (tem) = 1;
7946 TREE_USED (tem) = 1;
7947 return tem;
7948 }
7949
7950 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7951 constants (if x has signed type, the sign bit cannot be set
7952 in c). This folds extension into the BIT_AND_EXPR.
7953 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7954 very likely don't have maximal range for their precision and this
7955 transformation effectively doesn't preserve non-maximal ranges. */
7956 if (TREE_CODE (type) == INTEGER_TYPE
7957 && TREE_CODE (op0) == BIT_AND_EXPR
7958 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7959 {
7960 tree and_expr = op0;
7961 tree and0 = TREE_OPERAND (and_expr, 0);
7962 tree and1 = TREE_OPERAND (and_expr, 1);
7963 int change = 0;
7964
7965 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7966 || (TYPE_PRECISION (type)
7967 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7968 change = 1;
7969 else if (TYPE_PRECISION (TREE_TYPE (and1))
7970 <= HOST_BITS_PER_WIDE_INT
7971 && tree_fits_uhwi_p (and1))
7972 {
7973 unsigned HOST_WIDE_INT cst;
7974
7975 cst = tree_to_uhwi (and1);
7976 cst &= HOST_WIDE_INT_M1U
7977 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7978 change = (cst == 0);
7979 if (change
7980 && !flag_syntax_only
7981 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7982 == ZERO_EXTEND))
7983 {
7984 tree uns = unsigned_type_for (TREE_TYPE (and0));
7985 and0 = fold_convert_loc (loc, uns, and0);
7986 and1 = fold_convert_loc (loc, uns, and1);
7987 }
7988 }
7989 if (change)
7990 {
7991 tem = force_fit_type (type, wi::to_widest (and1), 0,
7992 TREE_OVERFLOW (and1));
7993 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7994 fold_convert_loc (loc, type, and0), tem);
7995 }
7996 }
7997
7998 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7999 cast (T1)X will fold away. We assume that this happens when X itself
8000 is a cast. */
8001 if (POINTER_TYPE_P (type)
8002 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8003 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
8004 {
8005 tree arg00 = TREE_OPERAND (arg0, 0);
8006 tree arg01 = TREE_OPERAND (arg0, 1);
8007
8008 return fold_build_pointer_plus_loc
8009 (loc, fold_convert_loc (loc, type, arg00), arg01);
8010 }
8011
8012 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8013 of the same precision, and X is an integer type not narrower than
8014 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8015 if (INTEGRAL_TYPE_P (type)
8016 && TREE_CODE (op0) == BIT_NOT_EXPR
8017 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8018 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8019 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8020 {
8021 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8022 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8023 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8024 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8025 fold_convert_loc (loc, type, tem));
8026 }
8027
8028 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8029 type of X and Y (integer types only). */
8030 if (INTEGRAL_TYPE_P (type)
8031 && TREE_CODE (op0) == MULT_EXPR
8032 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8033 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8034 {
8035 /* Be careful not to introduce new overflows. */
8036 tree mult_type;
8037 if (TYPE_OVERFLOW_WRAPS (type))
8038 mult_type = type;
8039 else
8040 mult_type = unsigned_type_for (type);
8041
8042 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8043 {
8044 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8045 fold_convert_loc (loc, mult_type,
8046 TREE_OPERAND (op0, 0)),
8047 fold_convert_loc (loc, mult_type,
8048 TREE_OPERAND (op0, 1)));
8049 return fold_convert_loc (loc, type, tem);
8050 }
8051 }
8052
8053 return NULL_TREE;
8054
8055 case VIEW_CONVERT_EXPR:
8056 if (TREE_CODE (op0) == MEM_REF)
8057 {
8058 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
8059 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
8060 tem = fold_build2_loc (loc, MEM_REF, type,
8061 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8062 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8063 return tem;
8064 }
8065
8066 return NULL_TREE;
8067
8068 case NEGATE_EXPR:
8069 tem = fold_negate_expr (loc, arg0);
8070 if (tem)
8071 return fold_convert_loc (loc, type, tem);
8072 return NULL_TREE;
8073
8074 case ABS_EXPR:
8075 /* Convert fabs((double)float) into (double)fabsf(float). */
8076 if (TREE_CODE (arg0) == NOP_EXPR
8077 && TREE_CODE (type) == REAL_TYPE)
8078 {
8079 tree targ0 = strip_float_extensions (arg0);
8080 if (targ0 != arg0)
8081 return fold_convert_loc (loc, type,
8082 fold_build1_loc (loc, ABS_EXPR,
8083 TREE_TYPE (targ0),
8084 targ0));
8085 }
8086 return NULL_TREE;
8087
8088 case BIT_NOT_EXPR:
8089 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8090 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8091 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8092 fold_convert_loc (loc, type,
8093 TREE_OPERAND (arg0, 0)))))
8094 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8095 fold_convert_loc (loc, type,
8096 TREE_OPERAND (arg0, 1)));
8097 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8098 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8099 fold_convert_loc (loc, type,
8100 TREE_OPERAND (arg0, 1)))))
8101 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8102 fold_convert_loc (loc, type,
8103 TREE_OPERAND (arg0, 0)), tem);
8104
8105 return NULL_TREE;
8106
8107 case TRUTH_NOT_EXPR:
8108 /* Note that the operand of this must be an int
8109 and its values must be 0 or 1.
8110 ("true" is a fixed value perhaps depending on the language,
8111 but we don't handle values other than 1 correctly yet.) */
8112 tem = fold_truth_not_expr (loc, arg0);
8113 if (!tem)
8114 return NULL_TREE;
8115 return fold_convert_loc (loc, type, tem);
8116
8117 case INDIRECT_REF:
8118 /* Fold *&X to X if X is an lvalue. */
8119 if (TREE_CODE (op0) == ADDR_EXPR)
8120 {
8121 tree op00 = TREE_OPERAND (op0, 0);
8122 if ((VAR_P (op00)
8123 || TREE_CODE (op00) == PARM_DECL
8124 || TREE_CODE (op00) == RESULT_DECL)
8125 && !TREE_READONLY (op00))
8126 return op00;
8127 }
8128 return NULL_TREE;
8129
8130 default:
8131 return NULL_TREE;
8132 } /* switch (code) */
8133 }
8134
8135
8136 /* If the operation was a conversion do _not_ mark a resulting constant
8137 with TREE_OVERFLOW if the original constant was not. These conversions
8138 have implementation defined behavior and retaining the TREE_OVERFLOW
8139 flag here would confuse later passes such as VRP. */
8140 tree
8141 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8142 tree type, tree op0)
8143 {
8144 tree res = fold_unary_loc (loc, code, type, op0);
8145 if (res
8146 && TREE_CODE (res) == INTEGER_CST
8147 && TREE_CODE (op0) == INTEGER_CST
8148 && CONVERT_EXPR_CODE_P (code))
8149 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8150
8151 return res;
8152 }
8153
8154 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8155 operands OP0 and OP1. LOC is the location of the resulting expression.
8156 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8157 Return the folded expression if folding is successful. Otherwise,
8158 return NULL_TREE. */
8159 static tree
8160 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8161 tree arg0, tree arg1, tree op0, tree op1)
8162 {
8163 tree tem;
8164
8165 /* We only do these simplifications if we are optimizing. */
8166 if (!optimize)
8167 return NULL_TREE;
8168
8169 /* Check for things like (A || B) && (A || C). We can convert this
8170 to A || (B && C). Note that either operator can be any of the four
8171 truth and/or operations and the transformation will still be
8172 valid. Also note that we only care about order for the
8173 ANDIF and ORIF operators. If B contains side effects, this
8174 might change the truth-value of A. */
8175 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8176 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8177 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8178 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8179 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8180 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8181 {
8182 tree a00 = TREE_OPERAND (arg0, 0);
8183 tree a01 = TREE_OPERAND (arg0, 1);
8184 tree a10 = TREE_OPERAND (arg1, 0);
8185 tree a11 = TREE_OPERAND (arg1, 1);
8186 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8187 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8188 && (code == TRUTH_AND_EXPR
8189 || code == TRUTH_OR_EXPR));
8190
8191 if (operand_equal_p (a00, a10, 0))
8192 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8193 fold_build2_loc (loc, code, type, a01, a11));
8194 else if (commutative && operand_equal_p (a00, a11, 0))
8195 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8196 fold_build2_loc (loc, code, type, a01, a10));
8197 else if (commutative && operand_equal_p (a01, a10, 0))
8198 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8199 fold_build2_loc (loc, code, type, a00, a11));
8200
8201 /* This case if tricky because we must either have commutative
8202 operators or else A10 must not have side-effects. */
8203
8204 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8205 && operand_equal_p (a01, a11, 0))
8206 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8207 fold_build2_loc (loc, code, type, a00, a10),
8208 a01);
8209 }
8210
8211 /* See if we can build a range comparison. */
8212 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8213 return tem;
8214
8215 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8216 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8217 {
8218 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8219 if (tem)
8220 return fold_build2_loc (loc, code, type, tem, arg1);
8221 }
8222
8223 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8224 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8225 {
8226 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8227 if (tem)
8228 return fold_build2_loc (loc, code, type, arg0, tem);
8229 }
8230
8231 /* Check for the possibility of merging component references. If our
8232 lhs is another similar operation, try to merge its rhs with our
8233 rhs. Then try to merge our lhs and rhs. */
8234 if (TREE_CODE (arg0) == code
8235 && (tem = fold_truth_andor_1 (loc, code, type,
8236 TREE_OPERAND (arg0, 1), arg1)) != 0)
8237 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8238
8239 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8240 return tem;
8241
8242 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
8243 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
8244 logical_op_non_short_circuit
8245 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
8246 if (logical_op_non_short_circuit
8247 && !flag_sanitize_coverage
8248 && (code == TRUTH_AND_EXPR
8249 || code == TRUTH_ANDIF_EXPR
8250 || code == TRUTH_OR_EXPR
8251 || code == TRUTH_ORIF_EXPR))
8252 {
8253 enum tree_code ncode, icode;
8254
8255 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8256 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8257 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8258
8259 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8260 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8261 We don't want to pack more than two leafs to a non-IF AND/OR
8262 expression.
8263 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8264 equal to IF-CODE, then we don't want to add right-hand operand.
8265 If the inner right-hand side of left-hand operand has
8266 side-effects, or isn't simple, then we can't add to it,
8267 as otherwise we might destroy if-sequence. */
8268 if (TREE_CODE (arg0) == icode
8269 && simple_operand_p_2 (arg1)
8270 /* Needed for sequence points to handle trappings, and
8271 side-effects. */
8272 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8273 {
8274 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8275 arg1);
8276 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8277 tem);
8278 }
8279 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8280 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8281 else if (TREE_CODE (arg1) == icode
8282 && simple_operand_p_2 (arg0)
8283 /* Needed for sequence points to handle trappings, and
8284 side-effects. */
8285 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8286 {
8287 tem = fold_build2_loc (loc, ncode, type,
8288 arg0, TREE_OPERAND (arg1, 0));
8289 return fold_build2_loc (loc, icode, type, tem,
8290 TREE_OPERAND (arg1, 1));
8291 }
8292 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8293 into (A OR B).
8294 For sequence point consistancy, we need to check for trapping,
8295 and side-effects. */
8296 else if (code == icode && simple_operand_p_2 (arg0)
8297 && simple_operand_p_2 (arg1))
8298 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8299 }
8300
8301 return NULL_TREE;
8302 }
8303
8304 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8305 by changing CODE to reduce the magnitude of constants involved in
8306 ARG0 of the comparison.
8307 Returns a canonicalized comparison tree if a simplification was
8308 possible, otherwise returns NULL_TREE.
8309 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8310 valid if signed overflow is undefined. */
8311
8312 static tree
8313 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8314 tree arg0, tree arg1,
8315 bool *strict_overflow_p)
8316 {
8317 enum tree_code code0 = TREE_CODE (arg0);
8318 tree t, cst0 = NULL_TREE;
8319 int sgn0;
8320
8321 /* Match A +- CST code arg1. We can change this only if overflow
8322 is undefined. */
8323 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8324 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8325 /* In principle pointers also have undefined overflow behavior,
8326 but that causes problems elsewhere. */
8327 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8328 && (code0 == MINUS_EXPR
8329 || code0 == PLUS_EXPR)
8330 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8331 return NULL_TREE;
8332
8333 /* Identify the constant in arg0 and its sign. */
8334 cst0 = TREE_OPERAND (arg0, 1);
8335 sgn0 = tree_int_cst_sgn (cst0);
8336
8337 /* Overflowed constants and zero will cause problems. */
8338 if (integer_zerop (cst0)
8339 || TREE_OVERFLOW (cst0))
8340 return NULL_TREE;
8341
8342 /* See if we can reduce the magnitude of the constant in
8343 arg0 by changing the comparison code. */
8344 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8345 if (code == LT_EXPR
8346 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8347 code = LE_EXPR;
8348 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8349 else if (code == GT_EXPR
8350 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8351 code = GE_EXPR;
8352 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8353 else if (code == LE_EXPR
8354 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8355 code = LT_EXPR;
8356 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8357 else if (code == GE_EXPR
8358 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8359 code = GT_EXPR;
8360 else
8361 return NULL_TREE;
8362 *strict_overflow_p = true;
8363
8364 /* Now build the constant reduced in magnitude. But not if that
8365 would produce one outside of its types range. */
8366 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8367 && ((sgn0 == 1
8368 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8369 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8370 || (sgn0 == -1
8371 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8372 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8373 return NULL_TREE;
8374
8375 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8376 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8377 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8378 t = fold_convert (TREE_TYPE (arg1), t);
8379
8380 return fold_build2_loc (loc, code, type, t, arg1);
8381 }
8382
8383 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8384 overflow further. Try to decrease the magnitude of constants involved
8385 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8386 and put sole constants at the second argument position.
8387 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8388
8389 static tree
8390 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8391 tree arg0, tree arg1)
8392 {
8393 tree t;
8394 bool strict_overflow_p;
8395 const char * const warnmsg = G_("assuming signed overflow does not occur "
8396 "when reducing constant in comparison");
8397
8398 /* Try canonicalization by simplifying arg0. */
8399 strict_overflow_p = false;
8400 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8401 &strict_overflow_p);
8402 if (t)
8403 {
8404 if (strict_overflow_p)
8405 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8406 return t;
8407 }
8408
8409 /* Try canonicalization by simplifying arg1 using the swapped
8410 comparison. */
8411 code = swap_tree_comparison (code);
8412 strict_overflow_p = false;
8413 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8414 &strict_overflow_p);
8415 if (t && strict_overflow_p)
8416 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8417 return t;
8418 }
8419
8420 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8421 space. This is used to avoid issuing overflow warnings for
8422 expressions like &p->x which can not wrap. */
8423
8424 static bool
8425 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8426 {
8427 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8428 return true;
8429
8430 if (maybe_lt (bitpos, 0))
8431 return true;
8432
8433 poly_wide_int wi_offset;
8434 int precision = TYPE_PRECISION (TREE_TYPE (base));
8435 if (offset == NULL_TREE)
8436 wi_offset = wi::zero (precision);
8437 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8438 return true;
8439 else
8440 wi_offset = wi::to_poly_wide (offset);
8441
8442 wi::overflow_type overflow;
8443 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8444 precision);
8445 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8446 if (overflow)
8447 return true;
8448
8449 poly_uint64 total_hwi, size;
8450 if (!total.to_uhwi (&total_hwi)
8451 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8452 &size)
8453 || known_eq (size, 0U))
8454 return true;
8455
8456 if (known_le (total_hwi, size))
8457 return false;
8458
8459 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8460 array. */
8461 if (TREE_CODE (base) == ADDR_EXPR
8462 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8463 &size)
8464 && maybe_ne (size, 0U)
8465 && known_le (total_hwi, size))
8466 return false;
8467
8468 return true;
8469 }
8470
8471 /* Return a positive integer when the symbol DECL is known to have
8472 a nonzero address, zero when it's known not to (e.g., it's a weak
8473 symbol), and a negative integer when the symbol is not yet in the
8474 symbol table and so whether or not its address is zero is unknown.
8475 For function local objects always return positive integer. */
8476 static int
8477 maybe_nonzero_address (tree decl)
8478 {
8479 if (DECL_P (decl) && decl_in_symtab_p (decl))
8480 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8481 return symbol->nonzero_address ();
8482
8483 /* Function local objects are never NULL. */
8484 if (DECL_P (decl)
8485 && (DECL_CONTEXT (decl)
8486 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8487 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8488 return 1;
8489
8490 return -1;
8491 }
8492
8493 /* Subroutine of fold_binary. This routine performs all of the
8494 transformations that are common to the equality/inequality
8495 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8496 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8497 fold_binary should call fold_binary. Fold a comparison with
8498 tree code CODE and type TYPE with operands OP0 and OP1. Return
8499 the folded comparison or NULL_TREE. */
8500
8501 static tree
8502 fold_comparison (location_t loc, enum tree_code code, tree type,
8503 tree op0, tree op1)
8504 {
8505 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8506 tree arg0, arg1, tem;
8507
8508 arg0 = op0;
8509 arg1 = op1;
8510
8511 STRIP_SIGN_NOPS (arg0);
8512 STRIP_SIGN_NOPS (arg1);
8513
8514 /* For comparisons of pointers we can decompose it to a compile time
8515 comparison of the base objects and the offsets into the object.
8516 This requires at least one operand being an ADDR_EXPR or a
8517 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8518 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8519 && (TREE_CODE (arg0) == ADDR_EXPR
8520 || TREE_CODE (arg1) == ADDR_EXPR
8521 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8522 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8523 {
8524 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8525 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8526 machine_mode mode;
8527 int volatilep, reversep, unsignedp;
8528 bool indirect_base0 = false, indirect_base1 = false;
8529
8530 /* Get base and offset for the access. Strip ADDR_EXPR for
8531 get_inner_reference, but put it back by stripping INDIRECT_REF
8532 off the base object if possible. indirect_baseN will be true
8533 if baseN is not an address but refers to the object itself. */
8534 base0 = arg0;
8535 if (TREE_CODE (arg0) == ADDR_EXPR)
8536 {
8537 base0
8538 = get_inner_reference (TREE_OPERAND (arg0, 0),
8539 &bitsize, &bitpos0, &offset0, &mode,
8540 &unsignedp, &reversep, &volatilep);
8541 if (TREE_CODE (base0) == INDIRECT_REF)
8542 base0 = TREE_OPERAND (base0, 0);
8543 else
8544 indirect_base0 = true;
8545 }
8546 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8547 {
8548 base0 = TREE_OPERAND (arg0, 0);
8549 STRIP_SIGN_NOPS (base0);
8550 if (TREE_CODE (base0) == ADDR_EXPR)
8551 {
8552 base0
8553 = get_inner_reference (TREE_OPERAND (base0, 0),
8554 &bitsize, &bitpos0, &offset0, &mode,
8555 &unsignedp, &reversep, &volatilep);
8556 if (TREE_CODE (base0) == INDIRECT_REF)
8557 base0 = TREE_OPERAND (base0, 0);
8558 else
8559 indirect_base0 = true;
8560 }
8561 if (offset0 == NULL_TREE || integer_zerop (offset0))
8562 offset0 = TREE_OPERAND (arg0, 1);
8563 else
8564 offset0 = size_binop (PLUS_EXPR, offset0,
8565 TREE_OPERAND (arg0, 1));
8566 if (poly_int_tree_p (offset0))
8567 {
8568 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8569 TYPE_PRECISION (sizetype));
8570 tem <<= LOG2_BITS_PER_UNIT;
8571 tem += bitpos0;
8572 if (tem.to_shwi (&bitpos0))
8573 offset0 = NULL_TREE;
8574 }
8575 }
8576
8577 base1 = arg1;
8578 if (TREE_CODE (arg1) == ADDR_EXPR)
8579 {
8580 base1
8581 = get_inner_reference (TREE_OPERAND (arg1, 0),
8582 &bitsize, &bitpos1, &offset1, &mode,
8583 &unsignedp, &reversep, &volatilep);
8584 if (TREE_CODE (base1) == INDIRECT_REF)
8585 base1 = TREE_OPERAND (base1, 0);
8586 else
8587 indirect_base1 = true;
8588 }
8589 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8590 {
8591 base1 = TREE_OPERAND (arg1, 0);
8592 STRIP_SIGN_NOPS (base1);
8593 if (TREE_CODE (base1) == ADDR_EXPR)
8594 {
8595 base1
8596 = get_inner_reference (TREE_OPERAND (base1, 0),
8597 &bitsize, &bitpos1, &offset1, &mode,
8598 &unsignedp, &reversep, &volatilep);
8599 if (TREE_CODE (base1) == INDIRECT_REF)
8600 base1 = TREE_OPERAND (base1, 0);
8601 else
8602 indirect_base1 = true;
8603 }
8604 if (offset1 == NULL_TREE || integer_zerop (offset1))
8605 offset1 = TREE_OPERAND (arg1, 1);
8606 else
8607 offset1 = size_binop (PLUS_EXPR, offset1,
8608 TREE_OPERAND (arg1, 1));
8609 if (poly_int_tree_p (offset1))
8610 {
8611 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8612 TYPE_PRECISION (sizetype));
8613 tem <<= LOG2_BITS_PER_UNIT;
8614 tem += bitpos1;
8615 if (tem.to_shwi (&bitpos1))
8616 offset1 = NULL_TREE;
8617 }
8618 }
8619
8620 /* If we have equivalent bases we might be able to simplify. */
8621 if (indirect_base0 == indirect_base1
8622 && operand_equal_p (base0, base1,
8623 indirect_base0 ? OEP_ADDRESS_OF : 0))
8624 {
8625 /* We can fold this expression to a constant if the non-constant
8626 offset parts are equal. */
8627 if ((offset0 == offset1
8628 || (offset0 && offset1
8629 && operand_equal_p (offset0, offset1, 0)))
8630 && (equality_code
8631 || (indirect_base0
8632 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8633 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8634 {
8635 if (!equality_code
8636 && maybe_ne (bitpos0, bitpos1)
8637 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8638 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8639 fold_overflow_warning (("assuming pointer wraparound does not "
8640 "occur when comparing P +- C1 with "
8641 "P +- C2"),
8642 WARN_STRICT_OVERFLOW_CONDITIONAL);
8643
8644 switch (code)
8645 {
8646 case EQ_EXPR:
8647 if (known_eq (bitpos0, bitpos1))
8648 return constant_boolean_node (true, type);
8649 if (known_ne (bitpos0, bitpos1))
8650 return constant_boolean_node (false, type);
8651 break;
8652 case NE_EXPR:
8653 if (known_ne (bitpos0, bitpos1))
8654 return constant_boolean_node (true, type);
8655 if (known_eq (bitpos0, bitpos1))
8656 return constant_boolean_node (false, type);
8657 break;
8658 case LT_EXPR:
8659 if (known_lt (bitpos0, bitpos1))
8660 return constant_boolean_node (true, type);
8661 if (known_ge (bitpos0, bitpos1))
8662 return constant_boolean_node (false, type);
8663 break;
8664 case LE_EXPR:
8665 if (known_le (bitpos0, bitpos1))
8666 return constant_boolean_node (true, type);
8667 if (known_gt (bitpos0, bitpos1))
8668 return constant_boolean_node (false, type);
8669 break;
8670 case GE_EXPR:
8671 if (known_ge (bitpos0, bitpos1))
8672 return constant_boolean_node (true, type);
8673 if (known_lt (bitpos0, bitpos1))
8674 return constant_boolean_node (false, type);
8675 break;
8676 case GT_EXPR:
8677 if (known_gt (bitpos0, bitpos1))
8678 return constant_boolean_node (true, type);
8679 if (known_le (bitpos0, bitpos1))
8680 return constant_boolean_node (false, type);
8681 break;
8682 default:;
8683 }
8684 }
8685 /* We can simplify the comparison to a comparison of the variable
8686 offset parts if the constant offset parts are equal.
8687 Be careful to use signed sizetype here because otherwise we
8688 mess with array offsets in the wrong way. This is possible
8689 because pointer arithmetic is restricted to retain within an
8690 object and overflow on pointer differences is undefined as of
8691 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8692 else if (known_eq (bitpos0, bitpos1)
8693 && (equality_code
8694 || (indirect_base0
8695 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8696 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8697 {
8698 /* By converting to signed sizetype we cover middle-end pointer
8699 arithmetic which operates on unsigned pointer types of size
8700 type size and ARRAY_REF offsets which are properly sign or
8701 zero extended from their type in case it is narrower than
8702 sizetype. */
8703 if (offset0 == NULL_TREE)
8704 offset0 = build_int_cst (ssizetype, 0);
8705 else
8706 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8707 if (offset1 == NULL_TREE)
8708 offset1 = build_int_cst (ssizetype, 0);
8709 else
8710 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8711
8712 if (!equality_code
8713 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8714 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8715 fold_overflow_warning (("assuming pointer wraparound does not "
8716 "occur when comparing P +- C1 with "
8717 "P +- C2"),
8718 WARN_STRICT_OVERFLOW_COMPARISON);
8719
8720 return fold_build2_loc (loc, code, type, offset0, offset1);
8721 }
8722 }
8723 /* For equal offsets we can simplify to a comparison of the
8724 base addresses. */
8725 else if (known_eq (bitpos0, bitpos1)
8726 && (indirect_base0
8727 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8728 && (indirect_base1
8729 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8730 && ((offset0 == offset1)
8731 || (offset0 && offset1
8732 && operand_equal_p (offset0, offset1, 0))))
8733 {
8734 if (indirect_base0)
8735 base0 = build_fold_addr_expr_loc (loc, base0);
8736 if (indirect_base1)
8737 base1 = build_fold_addr_expr_loc (loc, base1);
8738 return fold_build2_loc (loc, code, type, base0, base1);
8739 }
8740 /* Comparison between an ordinary (non-weak) symbol and a null
8741 pointer can be eliminated since such symbols must have a non
8742 null address. In C, relational expressions between pointers
8743 to objects and null pointers are undefined. The results
8744 below follow the C++ rules with the additional property that
8745 every object pointer compares greater than a null pointer.
8746 */
8747 else if (((DECL_P (base0)
8748 && maybe_nonzero_address (base0) > 0
8749 /* Avoid folding references to struct members at offset 0 to
8750 prevent tests like '&ptr->firstmember == 0' from getting
8751 eliminated. When ptr is null, although the -> expression
8752 is strictly speaking invalid, GCC retains it as a matter
8753 of QoI. See PR c/44555. */
8754 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8755 || CONSTANT_CLASS_P (base0))
8756 && indirect_base0
8757 /* The caller guarantees that when one of the arguments is
8758 constant (i.e., null in this case) it is second. */
8759 && integer_zerop (arg1))
8760 {
8761 switch (code)
8762 {
8763 case EQ_EXPR:
8764 case LE_EXPR:
8765 case LT_EXPR:
8766 return constant_boolean_node (false, type);
8767 case GE_EXPR:
8768 case GT_EXPR:
8769 case NE_EXPR:
8770 return constant_boolean_node (true, type);
8771 default:
8772 gcc_unreachable ();
8773 }
8774 }
8775 }
8776
8777 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8778 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8779 the resulting offset is smaller in absolute value than the
8780 original one and has the same sign. */
8781 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8782 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8783 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8784 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8785 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8786 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8787 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8788 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8789 {
8790 tree const1 = TREE_OPERAND (arg0, 1);
8791 tree const2 = TREE_OPERAND (arg1, 1);
8792 tree variable1 = TREE_OPERAND (arg0, 0);
8793 tree variable2 = TREE_OPERAND (arg1, 0);
8794 tree cst;
8795 const char * const warnmsg = G_("assuming signed overflow does not "
8796 "occur when combining constants around "
8797 "a comparison");
8798
8799 /* Put the constant on the side where it doesn't overflow and is
8800 of lower absolute value and of same sign than before. */
8801 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8802 ? MINUS_EXPR : PLUS_EXPR,
8803 const2, const1);
8804 if (!TREE_OVERFLOW (cst)
8805 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8806 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8807 {
8808 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8809 return fold_build2_loc (loc, code, type,
8810 variable1,
8811 fold_build2_loc (loc, TREE_CODE (arg1),
8812 TREE_TYPE (arg1),
8813 variable2, cst));
8814 }
8815
8816 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8817 ? MINUS_EXPR : PLUS_EXPR,
8818 const1, const2);
8819 if (!TREE_OVERFLOW (cst)
8820 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8821 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8822 {
8823 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8824 return fold_build2_loc (loc, code, type,
8825 fold_build2_loc (loc, TREE_CODE (arg0),
8826 TREE_TYPE (arg0),
8827 variable1, cst),
8828 variable2);
8829 }
8830 }
8831
8832 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8833 if (tem)
8834 return tem;
8835
8836 /* If we are comparing an expression that just has comparisons
8837 of two integer values, arithmetic expressions of those comparisons,
8838 and constants, we can simplify it. There are only three cases
8839 to check: the two values can either be equal, the first can be
8840 greater, or the second can be greater. Fold the expression for
8841 those three values. Since each value must be 0 or 1, we have
8842 eight possibilities, each of which corresponds to the constant 0
8843 or 1 or one of the six possible comparisons.
8844
8845 This handles common cases like (a > b) == 0 but also handles
8846 expressions like ((x > y) - (y > x)) > 0, which supposedly
8847 occur in macroized code. */
8848
8849 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8850 {
8851 tree cval1 = 0, cval2 = 0;
8852
8853 if (twoval_comparison_p (arg0, &cval1, &cval2)
8854 /* Don't handle degenerate cases here; they should already
8855 have been handled anyway. */
8856 && cval1 != 0 && cval2 != 0
8857 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8858 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8859 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8860 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8861 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8862 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8863 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8864 {
8865 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8866 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8867
8868 /* We can't just pass T to eval_subst in case cval1 or cval2
8869 was the same as ARG1. */
8870
8871 tree high_result
8872 = fold_build2_loc (loc, code, type,
8873 eval_subst (loc, arg0, cval1, maxval,
8874 cval2, minval),
8875 arg1);
8876 tree equal_result
8877 = fold_build2_loc (loc, code, type,
8878 eval_subst (loc, arg0, cval1, maxval,
8879 cval2, maxval),
8880 arg1);
8881 tree low_result
8882 = fold_build2_loc (loc, code, type,
8883 eval_subst (loc, arg0, cval1, minval,
8884 cval2, maxval),
8885 arg1);
8886
8887 /* All three of these results should be 0 or 1. Confirm they are.
8888 Then use those values to select the proper code to use. */
8889
8890 if (TREE_CODE (high_result) == INTEGER_CST
8891 && TREE_CODE (equal_result) == INTEGER_CST
8892 && TREE_CODE (low_result) == INTEGER_CST)
8893 {
8894 /* Make a 3-bit mask with the high-order bit being the
8895 value for `>', the next for '=', and the low for '<'. */
8896 switch ((integer_onep (high_result) * 4)
8897 + (integer_onep (equal_result) * 2)
8898 + integer_onep (low_result))
8899 {
8900 case 0:
8901 /* Always false. */
8902 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8903 case 1:
8904 code = LT_EXPR;
8905 break;
8906 case 2:
8907 code = EQ_EXPR;
8908 break;
8909 case 3:
8910 code = LE_EXPR;
8911 break;
8912 case 4:
8913 code = GT_EXPR;
8914 break;
8915 case 5:
8916 code = NE_EXPR;
8917 break;
8918 case 6:
8919 code = GE_EXPR;
8920 break;
8921 case 7:
8922 /* Always true. */
8923 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8924 }
8925
8926 return fold_build2_loc (loc, code, type, cval1, cval2);
8927 }
8928 }
8929 }
8930
8931 return NULL_TREE;
8932 }
8933
8934
8935 /* Subroutine of fold_binary. Optimize complex multiplications of the
8936 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8937 argument EXPR represents the expression "z" of type TYPE. */
8938
8939 static tree
8940 fold_mult_zconjz (location_t loc, tree type, tree expr)
8941 {
8942 tree itype = TREE_TYPE (type);
8943 tree rpart, ipart, tem;
8944
8945 if (TREE_CODE (expr) == COMPLEX_EXPR)
8946 {
8947 rpart = TREE_OPERAND (expr, 0);
8948 ipart = TREE_OPERAND (expr, 1);
8949 }
8950 else if (TREE_CODE (expr) == COMPLEX_CST)
8951 {
8952 rpart = TREE_REALPART (expr);
8953 ipart = TREE_IMAGPART (expr);
8954 }
8955 else
8956 {
8957 expr = save_expr (expr);
8958 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8959 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8960 }
8961
8962 rpart = save_expr (rpart);
8963 ipart = save_expr (ipart);
8964 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8965 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8966 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8967 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8968 build_zero_cst (itype));
8969 }
8970
8971
8972 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8973 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8974 true if successful. */
8975
8976 static bool
8977 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8978 {
8979 unsigned HOST_WIDE_INT i, nunits;
8980
8981 if (TREE_CODE (arg) == VECTOR_CST
8982 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8983 {
8984 for (i = 0; i < nunits; ++i)
8985 elts[i] = VECTOR_CST_ELT (arg, i);
8986 }
8987 else if (TREE_CODE (arg) == CONSTRUCTOR)
8988 {
8989 constructor_elt *elt;
8990
8991 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8992 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8993 return false;
8994 else
8995 elts[i] = elt->value;
8996 }
8997 else
8998 return false;
8999 for (; i < nelts; i++)
9000 elts[i]
9001 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9002 return true;
9003 }
9004
9005 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9006 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9007 NULL_TREE otherwise. */
9008
9009 static tree
9010 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
9011 {
9012 unsigned int i;
9013 unsigned HOST_WIDE_INT nelts;
9014 bool need_ctor = false;
9015
9016 if (!sel.length ().is_constant (&nelts))
9017 return NULL_TREE;
9018 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
9019 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
9020 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
9021 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9022 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9023 return NULL_TREE;
9024
9025 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
9026 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
9027 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
9028 return NULL_TREE;
9029
9030 tree_vector_builder out_elts (type, nelts, 1);
9031 for (i = 0; i < nelts; i++)
9032 {
9033 HOST_WIDE_INT index;
9034 if (!sel[i].is_constant (&index))
9035 return NULL_TREE;
9036 if (!CONSTANT_CLASS_P (in_elts[index]))
9037 need_ctor = true;
9038 out_elts.quick_push (unshare_expr (in_elts[index]));
9039 }
9040
9041 if (need_ctor)
9042 {
9043 vec<constructor_elt, va_gc> *v;
9044 vec_alloc (v, nelts);
9045 for (i = 0; i < nelts; i++)
9046 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
9047 return build_constructor (type, v);
9048 }
9049 else
9050 return out_elts.build ();
9051 }
9052
9053 /* Try to fold a pointer difference of type TYPE two address expressions of
9054 array references AREF0 and AREF1 using location LOC. Return a
9055 simplified expression for the difference or NULL_TREE. */
9056
9057 static tree
9058 fold_addr_of_array_ref_difference (location_t loc, tree type,
9059 tree aref0, tree aref1,
9060 bool use_pointer_diff)
9061 {
9062 tree base0 = TREE_OPERAND (aref0, 0);
9063 tree base1 = TREE_OPERAND (aref1, 0);
9064 tree base_offset = build_int_cst (type, 0);
9065
9066 /* If the bases are array references as well, recurse. If the bases
9067 are pointer indirections compute the difference of the pointers.
9068 If the bases are equal, we are set. */
9069 if ((TREE_CODE (base0) == ARRAY_REF
9070 && TREE_CODE (base1) == ARRAY_REF
9071 && (base_offset
9072 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9073 use_pointer_diff)))
9074 || (INDIRECT_REF_P (base0)
9075 && INDIRECT_REF_P (base1)
9076 && (base_offset
9077 = use_pointer_diff
9078 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9079 TREE_OPERAND (base0, 0),
9080 TREE_OPERAND (base1, 0))
9081 : fold_binary_loc (loc, MINUS_EXPR, type,
9082 fold_convert (type,
9083 TREE_OPERAND (base0, 0)),
9084 fold_convert (type,
9085 TREE_OPERAND (base1, 0)))))
9086 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9087 {
9088 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9089 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9090 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9091 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9092 return fold_build2_loc (loc, PLUS_EXPR, type,
9093 base_offset,
9094 fold_build2_loc (loc, MULT_EXPR, type,
9095 diff, esz));
9096 }
9097 return NULL_TREE;
9098 }
9099
9100 /* If the real or vector real constant CST of type TYPE has an exact
9101 inverse, return it, else return NULL. */
9102
9103 tree
9104 exact_inverse (tree type, tree cst)
9105 {
9106 REAL_VALUE_TYPE r;
9107 tree unit_type;
9108 machine_mode mode;
9109
9110 switch (TREE_CODE (cst))
9111 {
9112 case REAL_CST:
9113 r = TREE_REAL_CST (cst);
9114
9115 if (exact_real_inverse (TYPE_MODE (type), &r))
9116 return build_real (type, r);
9117
9118 return NULL_TREE;
9119
9120 case VECTOR_CST:
9121 {
9122 unit_type = TREE_TYPE (type);
9123 mode = TYPE_MODE (unit_type);
9124
9125 tree_vector_builder elts;
9126 if (!elts.new_unary_operation (type, cst, false))
9127 return NULL_TREE;
9128 unsigned int count = elts.encoded_nelts ();
9129 for (unsigned int i = 0; i < count; ++i)
9130 {
9131 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9132 if (!exact_real_inverse (mode, &r))
9133 return NULL_TREE;
9134 elts.quick_push (build_real (unit_type, r));
9135 }
9136
9137 return elts.build ();
9138 }
9139
9140 default:
9141 return NULL_TREE;
9142 }
9143 }
9144
9145 /* Mask out the tz least significant bits of X of type TYPE where
9146 tz is the number of trailing zeroes in Y. */
9147 static wide_int
9148 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9149 {
9150 int tz = wi::ctz (y);
9151 if (tz > 0)
9152 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9153 return x;
9154 }
9155
9156 /* Return true when T is an address and is known to be nonzero.
9157 For floating point we further ensure that T is not denormal.
9158 Similar logic is present in nonzero_address in rtlanal.h.
9159
9160 If the return value is based on the assumption that signed overflow
9161 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9162 change *STRICT_OVERFLOW_P. */
9163
9164 static bool
9165 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9166 {
9167 tree type = TREE_TYPE (t);
9168 enum tree_code code;
9169
9170 /* Doing something useful for floating point would need more work. */
9171 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9172 return false;
9173
9174 code = TREE_CODE (t);
9175 switch (TREE_CODE_CLASS (code))
9176 {
9177 case tcc_unary:
9178 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9179 strict_overflow_p);
9180 case tcc_binary:
9181 case tcc_comparison:
9182 return tree_binary_nonzero_warnv_p (code, type,
9183 TREE_OPERAND (t, 0),
9184 TREE_OPERAND (t, 1),
9185 strict_overflow_p);
9186 case tcc_constant:
9187 case tcc_declaration:
9188 case tcc_reference:
9189 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9190
9191 default:
9192 break;
9193 }
9194
9195 switch (code)
9196 {
9197 case TRUTH_NOT_EXPR:
9198 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9199 strict_overflow_p);
9200
9201 case TRUTH_AND_EXPR:
9202 case TRUTH_OR_EXPR:
9203 case TRUTH_XOR_EXPR:
9204 return tree_binary_nonzero_warnv_p (code, type,
9205 TREE_OPERAND (t, 0),
9206 TREE_OPERAND (t, 1),
9207 strict_overflow_p);
9208
9209 case COND_EXPR:
9210 case CONSTRUCTOR:
9211 case OBJ_TYPE_REF:
9212 case ASSERT_EXPR:
9213 case ADDR_EXPR:
9214 case WITH_SIZE_EXPR:
9215 case SSA_NAME:
9216 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9217
9218 case COMPOUND_EXPR:
9219 case MODIFY_EXPR:
9220 case BIND_EXPR:
9221 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9222 strict_overflow_p);
9223
9224 case SAVE_EXPR:
9225 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9226 strict_overflow_p);
9227
9228 case CALL_EXPR:
9229 {
9230 tree fndecl = get_callee_fndecl (t);
9231 if (!fndecl) return false;
9232 if (flag_delete_null_pointer_checks && !flag_check_new
9233 && DECL_IS_OPERATOR_NEW (fndecl)
9234 && !TREE_NOTHROW (fndecl))
9235 return true;
9236 if (flag_delete_null_pointer_checks
9237 && lookup_attribute ("returns_nonnull",
9238 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9239 return true;
9240 return alloca_call_p (t);
9241 }
9242
9243 default:
9244 break;
9245 }
9246 return false;
9247 }
9248
9249 /* Return true when T is an address and is known to be nonzero.
9250 Handle warnings about undefined signed overflow. */
9251
9252 bool
9253 tree_expr_nonzero_p (tree t)
9254 {
9255 bool ret, strict_overflow_p;
9256
9257 strict_overflow_p = false;
9258 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9259 if (strict_overflow_p)
9260 fold_overflow_warning (("assuming signed overflow does not occur when "
9261 "determining that expression is always "
9262 "non-zero"),
9263 WARN_STRICT_OVERFLOW_MISC);
9264 return ret;
9265 }
9266
9267 /* Return true if T is known not to be equal to an integer W. */
9268
9269 bool
9270 expr_not_equal_to (tree t, const wide_int &w)
9271 {
9272 wide_int min, max, nz;
9273 value_range_kind rtype;
9274 switch (TREE_CODE (t))
9275 {
9276 case INTEGER_CST:
9277 return wi::to_wide (t) != w;
9278
9279 case SSA_NAME:
9280 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9281 return false;
9282 rtype = get_range_info (t, &min, &max);
9283 if (rtype == VR_RANGE)
9284 {
9285 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9286 return true;
9287 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9288 return true;
9289 }
9290 else if (rtype == VR_ANTI_RANGE
9291 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9292 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9293 return true;
9294 /* If T has some known zero bits and W has any of those bits set,
9295 then T is known not to be equal to W. */
9296 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9297 TYPE_PRECISION (TREE_TYPE (t))), 0))
9298 return true;
9299 return false;
9300
9301 default:
9302 return false;
9303 }
9304 }
9305
9306 /* Fold a binary expression of code CODE and type TYPE with operands
9307 OP0 and OP1. LOC is the location of the resulting expression.
9308 Return the folded expression if folding is successful. Otherwise,
9309 return NULL_TREE. */
9310
9311 tree
9312 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9313 tree op0, tree op1)
9314 {
9315 enum tree_code_class kind = TREE_CODE_CLASS (code);
9316 tree arg0, arg1, tem;
9317 tree t1 = NULL_TREE;
9318 bool strict_overflow_p;
9319 unsigned int prec;
9320
9321 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9322 && TREE_CODE_LENGTH (code) == 2
9323 && op0 != NULL_TREE
9324 && op1 != NULL_TREE);
9325
9326 arg0 = op0;
9327 arg1 = op1;
9328
9329 /* Strip any conversions that don't change the mode. This is
9330 safe for every expression, except for a comparison expression
9331 because its signedness is derived from its operands. So, in
9332 the latter case, only strip conversions that don't change the
9333 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9334 preserved.
9335
9336 Note that this is done as an internal manipulation within the
9337 constant folder, in order to find the simplest representation
9338 of the arguments so that their form can be studied. In any
9339 cases, the appropriate type conversions should be put back in
9340 the tree that will get out of the constant folder. */
9341
9342 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9343 {
9344 STRIP_SIGN_NOPS (arg0);
9345 STRIP_SIGN_NOPS (arg1);
9346 }
9347 else
9348 {
9349 STRIP_NOPS (arg0);
9350 STRIP_NOPS (arg1);
9351 }
9352
9353 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9354 constant but we can't do arithmetic on them. */
9355 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9356 {
9357 tem = const_binop (code, type, arg0, arg1);
9358 if (tem != NULL_TREE)
9359 {
9360 if (TREE_TYPE (tem) != type)
9361 tem = fold_convert_loc (loc, type, tem);
9362 return tem;
9363 }
9364 }
9365
9366 /* If this is a commutative operation, and ARG0 is a constant, move it
9367 to ARG1 to reduce the number of tests below. */
9368 if (commutative_tree_code (code)
9369 && tree_swap_operands_p (arg0, arg1))
9370 return fold_build2_loc (loc, code, type, op1, op0);
9371
9372 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9373 to ARG1 to reduce the number of tests below. */
9374 if (kind == tcc_comparison
9375 && tree_swap_operands_p (arg0, arg1))
9376 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9377
9378 tem = generic_simplify (loc, code, type, op0, op1);
9379 if (tem)
9380 return tem;
9381
9382 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9383
9384 First check for cases where an arithmetic operation is applied to a
9385 compound, conditional, or comparison operation. Push the arithmetic
9386 operation inside the compound or conditional to see if any folding
9387 can then be done. Convert comparison to conditional for this purpose.
9388 The also optimizes non-constant cases that used to be done in
9389 expand_expr.
9390
9391 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9392 one of the operands is a comparison and the other is a comparison, a
9393 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9394 code below would make the expression more complex. Change it to a
9395 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9396 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9397
9398 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9399 || code == EQ_EXPR || code == NE_EXPR)
9400 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9401 && ((truth_value_p (TREE_CODE (arg0))
9402 && (truth_value_p (TREE_CODE (arg1))
9403 || (TREE_CODE (arg1) == BIT_AND_EXPR
9404 && integer_onep (TREE_OPERAND (arg1, 1)))))
9405 || (truth_value_p (TREE_CODE (arg1))
9406 && (truth_value_p (TREE_CODE (arg0))
9407 || (TREE_CODE (arg0) == BIT_AND_EXPR
9408 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9409 {
9410 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9411 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9412 : TRUTH_XOR_EXPR,
9413 boolean_type_node,
9414 fold_convert_loc (loc, boolean_type_node, arg0),
9415 fold_convert_loc (loc, boolean_type_node, arg1));
9416
9417 if (code == EQ_EXPR)
9418 tem = invert_truthvalue_loc (loc, tem);
9419
9420 return fold_convert_loc (loc, type, tem);
9421 }
9422
9423 if (TREE_CODE_CLASS (code) == tcc_binary
9424 || TREE_CODE_CLASS (code) == tcc_comparison)
9425 {
9426 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9427 {
9428 tem = fold_build2_loc (loc, code, type,
9429 fold_convert_loc (loc, TREE_TYPE (op0),
9430 TREE_OPERAND (arg0, 1)), op1);
9431 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9432 tem);
9433 }
9434 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9435 {
9436 tem = fold_build2_loc (loc, code, type, op0,
9437 fold_convert_loc (loc, TREE_TYPE (op1),
9438 TREE_OPERAND (arg1, 1)));
9439 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9440 tem);
9441 }
9442
9443 if (TREE_CODE (arg0) == COND_EXPR
9444 || TREE_CODE (arg0) == VEC_COND_EXPR
9445 || COMPARISON_CLASS_P (arg0))
9446 {
9447 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9448 arg0, arg1,
9449 /*cond_first_p=*/1);
9450 if (tem != NULL_TREE)
9451 return tem;
9452 }
9453
9454 if (TREE_CODE (arg1) == COND_EXPR
9455 || TREE_CODE (arg1) == VEC_COND_EXPR
9456 || COMPARISON_CLASS_P (arg1))
9457 {
9458 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9459 arg1, arg0,
9460 /*cond_first_p=*/0);
9461 if (tem != NULL_TREE)
9462 return tem;
9463 }
9464 }
9465
9466 switch (code)
9467 {
9468 case MEM_REF:
9469 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9470 if (TREE_CODE (arg0) == ADDR_EXPR
9471 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9472 {
9473 tree iref = TREE_OPERAND (arg0, 0);
9474 return fold_build2 (MEM_REF, type,
9475 TREE_OPERAND (iref, 0),
9476 int_const_binop (PLUS_EXPR, arg1,
9477 TREE_OPERAND (iref, 1)));
9478 }
9479
9480 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9481 if (TREE_CODE (arg0) == ADDR_EXPR
9482 && handled_component_p (TREE_OPERAND (arg0, 0)))
9483 {
9484 tree base;
9485 poly_int64 coffset;
9486 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9487 &coffset);
9488 if (!base)
9489 return NULL_TREE;
9490 return fold_build2 (MEM_REF, type,
9491 build_fold_addr_expr (base),
9492 int_const_binop (PLUS_EXPR, arg1,
9493 size_int (coffset)));
9494 }
9495
9496 return NULL_TREE;
9497
9498 case POINTER_PLUS_EXPR:
9499 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9500 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9501 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9502 return fold_convert_loc (loc, type,
9503 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9504 fold_convert_loc (loc, sizetype,
9505 arg1),
9506 fold_convert_loc (loc, sizetype,
9507 arg0)));
9508
9509 return NULL_TREE;
9510
9511 case PLUS_EXPR:
9512 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9513 {
9514 /* X + (X / CST) * -CST is X % CST. */
9515 if (TREE_CODE (arg1) == MULT_EXPR
9516 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9517 && operand_equal_p (arg0,
9518 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9519 {
9520 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9521 tree cst1 = TREE_OPERAND (arg1, 1);
9522 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9523 cst1, cst0);
9524 if (sum && integer_zerop (sum))
9525 return fold_convert_loc (loc, type,
9526 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9527 TREE_TYPE (arg0), arg0,
9528 cst0));
9529 }
9530 }
9531
9532 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9533 one. Make sure the type is not saturating and has the signedness of
9534 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9535 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9536 if ((TREE_CODE (arg0) == MULT_EXPR
9537 || TREE_CODE (arg1) == MULT_EXPR)
9538 && !TYPE_SATURATING (type)
9539 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9540 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9541 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9542 {
9543 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9544 if (tem)
9545 return tem;
9546 }
9547
9548 if (! FLOAT_TYPE_P (type))
9549 {
9550 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9551 (plus (plus (mult) (mult)) (foo)) so that we can
9552 take advantage of the factoring cases below. */
9553 if (ANY_INTEGRAL_TYPE_P (type)
9554 && TYPE_OVERFLOW_WRAPS (type)
9555 && (((TREE_CODE (arg0) == PLUS_EXPR
9556 || TREE_CODE (arg0) == MINUS_EXPR)
9557 && TREE_CODE (arg1) == MULT_EXPR)
9558 || ((TREE_CODE (arg1) == PLUS_EXPR
9559 || TREE_CODE (arg1) == MINUS_EXPR)
9560 && TREE_CODE (arg0) == MULT_EXPR)))
9561 {
9562 tree parg0, parg1, parg, marg;
9563 enum tree_code pcode;
9564
9565 if (TREE_CODE (arg1) == MULT_EXPR)
9566 parg = arg0, marg = arg1;
9567 else
9568 parg = arg1, marg = arg0;
9569 pcode = TREE_CODE (parg);
9570 parg0 = TREE_OPERAND (parg, 0);
9571 parg1 = TREE_OPERAND (parg, 1);
9572 STRIP_NOPS (parg0);
9573 STRIP_NOPS (parg1);
9574
9575 if (TREE_CODE (parg0) == MULT_EXPR
9576 && TREE_CODE (parg1) != MULT_EXPR)
9577 return fold_build2_loc (loc, pcode, type,
9578 fold_build2_loc (loc, PLUS_EXPR, type,
9579 fold_convert_loc (loc, type,
9580 parg0),
9581 fold_convert_loc (loc, type,
9582 marg)),
9583 fold_convert_loc (loc, type, parg1));
9584 if (TREE_CODE (parg0) != MULT_EXPR
9585 && TREE_CODE (parg1) == MULT_EXPR)
9586 return
9587 fold_build2_loc (loc, PLUS_EXPR, type,
9588 fold_convert_loc (loc, type, parg0),
9589 fold_build2_loc (loc, pcode, type,
9590 fold_convert_loc (loc, type, marg),
9591 fold_convert_loc (loc, type,
9592 parg1)));
9593 }
9594 }
9595 else
9596 {
9597 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9598 to __complex__ ( x, y ). This is not the same for SNaNs or
9599 if signed zeros are involved. */
9600 if (!HONOR_SNANS (element_mode (arg0))
9601 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9602 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9603 {
9604 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9605 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9606 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9607 bool arg0rz = false, arg0iz = false;
9608 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9609 || (arg0i && (arg0iz = real_zerop (arg0i))))
9610 {
9611 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9612 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9613 if (arg0rz && arg1i && real_zerop (arg1i))
9614 {
9615 tree rp = arg1r ? arg1r
9616 : build1 (REALPART_EXPR, rtype, arg1);
9617 tree ip = arg0i ? arg0i
9618 : build1 (IMAGPART_EXPR, rtype, arg0);
9619 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9620 }
9621 else if (arg0iz && arg1r && real_zerop (arg1r))
9622 {
9623 tree rp = arg0r ? arg0r
9624 : build1 (REALPART_EXPR, rtype, arg0);
9625 tree ip = arg1i ? arg1i
9626 : build1 (IMAGPART_EXPR, rtype, arg1);
9627 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9628 }
9629 }
9630 }
9631
9632 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9633 We associate floats only if the user has specified
9634 -fassociative-math. */
9635 if (flag_associative_math
9636 && TREE_CODE (arg1) == PLUS_EXPR
9637 && TREE_CODE (arg0) != MULT_EXPR)
9638 {
9639 tree tree10 = TREE_OPERAND (arg1, 0);
9640 tree tree11 = TREE_OPERAND (arg1, 1);
9641 if (TREE_CODE (tree11) == MULT_EXPR
9642 && TREE_CODE (tree10) == MULT_EXPR)
9643 {
9644 tree tree0;
9645 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9646 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9647 }
9648 }
9649 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9650 We associate floats only if the user has specified
9651 -fassociative-math. */
9652 if (flag_associative_math
9653 && TREE_CODE (arg0) == PLUS_EXPR
9654 && TREE_CODE (arg1) != MULT_EXPR)
9655 {
9656 tree tree00 = TREE_OPERAND (arg0, 0);
9657 tree tree01 = TREE_OPERAND (arg0, 1);
9658 if (TREE_CODE (tree01) == MULT_EXPR
9659 && TREE_CODE (tree00) == MULT_EXPR)
9660 {
9661 tree tree0;
9662 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9663 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9664 }
9665 }
9666 }
9667
9668 bit_rotate:
9669 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9670 is a rotate of A by C1 bits. */
9671 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9672 is a rotate of A by B bits.
9673 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9674 though in this case CODE must be | and not + or ^, otherwise
9675 it doesn't return A when B is 0. */
9676 {
9677 enum tree_code code0, code1;
9678 tree rtype;
9679 code0 = TREE_CODE (arg0);
9680 code1 = TREE_CODE (arg1);
9681 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9682 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9683 && operand_equal_p (TREE_OPERAND (arg0, 0),
9684 TREE_OPERAND (arg1, 0), 0)
9685 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9686 TYPE_UNSIGNED (rtype))
9687 /* Only create rotates in complete modes. Other cases are not
9688 expanded properly. */
9689 && (element_precision (rtype)
9690 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9691 {
9692 tree tree01, tree11;
9693 tree orig_tree01, orig_tree11;
9694 enum tree_code code01, code11;
9695
9696 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9697 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9698 STRIP_NOPS (tree01);
9699 STRIP_NOPS (tree11);
9700 code01 = TREE_CODE (tree01);
9701 code11 = TREE_CODE (tree11);
9702 if (code11 != MINUS_EXPR
9703 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9704 {
9705 std::swap (code0, code1);
9706 std::swap (code01, code11);
9707 std::swap (tree01, tree11);
9708 std::swap (orig_tree01, orig_tree11);
9709 }
9710 if (code01 == INTEGER_CST
9711 && code11 == INTEGER_CST
9712 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9713 == element_precision (rtype)))
9714 {
9715 tem = build2_loc (loc, LROTATE_EXPR,
9716 rtype, TREE_OPERAND (arg0, 0),
9717 code0 == LSHIFT_EXPR
9718 ? orig_tree01 : orig_tree11);
9719 return fold_convert_loc (loc, type, tem);
9720 }
9721 else if (code11 == MINUS_EXPR)
9722 {
9723 tree tree110, tree111;
9724 tree110 = TREE_OPERAND (tree11, 0);
9725 tree111 = TREE_OPERAND (tree11, 1);
9726 STRIP_NOPS (tree110);
9727 STRIP_NOPS (tree111);
9728 if (TREE_CODE (tree110) == INTEGER_CST
9729 && compare_tree_int (tree110,
9730 element_precision (rtype)) == 0
9731 && operand_equal_p (tree01, tree111, 0))
9732 {
9733 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9734 ? LROTATE_EXPR : RROTATE_EXPR),
9735 rtype, TREE_OPERAND (arg0, 0),
9736 orig_tree01);
9737 return fold_convert_loc (loc, type, tem);
9738 }
9739 }
9740 else if (code == BIT_IOR_EXPR
9741 && code11 == BIT_AND_EXPR
9742 && pow2p_hwi (element_precision (rtype)))
9743 {
9744 tree tree110, tree111;
9745 tree110 = TREE_OPERAND (tree11, 0);
9746 tree111 = TREE_OPERAND (tree11, 1);
9747 STRIP_NOPS (tree110);
9748 STRIP_NOPS (tree111);
9749 if (TREE_CODE (tree110) == NEGATE_EXPR
9750 && TREE_CODE (tree111) == INTEGER_CST
9751 && compare_tree_int (tree111,
9752 element_precision (rtype) - 1) == 0
9753 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9754 {
9755 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9756 ? LROTATE_EXPR : RROTATE_EXPR),
9757 rtype, TREE_OPERAND (arg0, 0),
9758 orig_tree01);
9759 return fold_convert_loc (loc, type, tem);
9760 }
9761 }
9762 }
9763 }
9764
9765 associate:
9766 /* In most languages, can't associate operations on floats through
9767 parentheses. Rather than remember where the parentheses were, we
9768 don't associate floats at all, unless the user has specified
9769 -fassociative-math.
9770 And, we need to make sure type is not saturating. */
9771
9772 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9773 && !TYPE_SATURATING (type))
9774 {
9775 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9776 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9777 tree atype = type;
9778 bool ok = true;
9779
9780 /* Split both trees into variables, constants, and literals. Then
9781 associate each group together, the constants with literals,
9782 then the result with variables. This increases the chances of
9783 literals being recombined later and of generating relocatable
9784 expressions for the sum of a constant and literal. */
9785 var0 = split_tree (arg0, type, code,
9786 &minus_var0, &con0, &minus_con0,
9787 &lit0, &minus_lit0, 0);
9788 var1 = split_tree (arg1, type, code,
9789 &minus_var1, &con1, &minus_con1,
9790 &lit1, &minus_lit1, code == MINUS_EXPR);
9791
9792 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9793 if (code == MINUS_EXPR)
9794 code = PLUS_EXPR;
9795
9796 /* With undefined overflow prefer doing association in a type
9797 which wraps on overflow, if that is one of the operand types. */
9798 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9799 && !TYPE_OVERFLOW_WRAPS (type))
9800 {
9801 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9802 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9803 atype = TREE_TYPE (arg0);
9804 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9805 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9806 atype = TREE_TYPE (arg1);
9807 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9808 }
9809
9810 /* With undefined overflow we can only associate constants with one
9811 variable, and constants whose association doesn't overflow. */
9812 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9813 && !TYPE_OVERFLOW_WRAPS (atype))
9814 {
9815 if ((var0 && var1) || (minus_var0 && minus_var1))
9816 {
9817 /* ??? If split_tree would handle NEGATE_EXPR we could
9818 simply reject these cases and the allowed cases would
9819 be the var0/minus_var1 ones. */
9820 tree tmp0 = var0 ? var0 : minus_var0;
9821 tree tmp1 = var1 ? var1 : minus_var1;
9822 bool one_neg = false;
9823
9824 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9825 {
9826 tmp0 = TREE_OPERAND (tmp0, 0);
9827 one_neg = !one_neg;
9828 }
9829 if (CONVERT_EXPR_P (tmp0)
9830 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9831 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9832 <= TYPE_PRECISION (atype)))
9833 tmp0 = TREE_OPERAND (tmp0, 0);
9834 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9835 {
9836 tmp1 = TREE_OPERAND (tmp1, 0);
9837 one_neg = !one_neg;
9838 }
9839 if (CONVERT_EXPR_P (tmp1)
9840 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9841 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9842 <= TYPE_PRECISION (atype)))
9843 tmp1 = TREE_OPERAND (tmp1, 0);
9844 /* The only case we can still associate with two variables
9845 is if they cancel out. */
9846 if (!one_neg
9847 || !operand_equal_p (tmp0, tmp1, 0))
9848 ok = false;
9849 }
9850 else if ((var0 && minus_var1
9851 && ! operand_equal_p (var0, minus_var1, 0))
9852 || (minus_var0 && var1
9853 && ! operand_equal_p (minus_var0, var1, 0)))
9854 ok = false;
9855 }
9856
9857 /* Only do something if we found more than two objects. Otherwise,
9858 nothing has changed and we risk infinite recursion. */
9859 if (ok
9860 && ((var0 != 0) + (var1 != 0)
9861 + (minus_var0 != 0) + (minus_var1 != 0)
9862 + (con0 != 0) + (con1 != 0)
9863 + (minus_con0 != 0) + (minus_con1 != 0)
9864 + (lit0 != 0) + (lit1 != 0)
9865 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9866 {
9867 var0 = associate_trees (loc, var0, var1, code, atype);
9868 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9869 code, atype);
9870 con0 = associate_trees (loc, con0, con1, code, atype);
9871 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9872 code, atype);
9873 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9874 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9875 code, atype);
9876
9877 if (minus_var0 && var0)
9878 {
9879 var0 = associate_trees (loc, var0, minus_var0,
9880 MINUS_EXPR, atype);
9881 minus_var0 = 0;
9882 }
9883 if (minus_con0 && con0)
9884 {
9885 con0 = associate_trees (loc, con0, minus_con0,
9886 MINUS_EXPR, atype);
9887 minus_con0 = 0;
9888 }
9889
9890 /* Preserve the MINUS_EXPR if the negative part of the literal is
9891 greater than the positive part. Otherwise, the multiplicative
9892 folding code (i.e extract_muldiv) may be fooled in case
9893 unsigned constants are subtracted, like in the following
9894 example: ((X*2 + 4) - 8U)/2. */
9895 if (minus_lit0 && lit0)
9896 {
9897 if (TREE_CODE (lit0) == INTEGER_CST
9898 && TREE_CODE (minus_lit0) == INTEGER_CST
9899 && tree_int_cst_lt (lit0, minus_lit0)
9900 /* But avoid ending up with only negated parts. */
9901 && (var0 || con0))
9902 {
9903 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9904 MINUS_EXPR, atype);
9905 lit0 = 0;
9906 }
9907 else
9908 {
9909 lit0 = associate_trees (loc, lit0, minus_lit0,
9910 MINUS_EXPR, atype);
9911 minus_lit0 = 0;
9912 }
9913 }
9914
9915 /* Don't introduce overflows through reassociation. */
9916 if ((lit0 && TREE_OVERFLOW_P (lit0))
9917 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9918 return NULL_TREE;
9919
9920 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9921 con0 = associate_trees (loc, con0, lit0, code, atype);
9922 lit0 = 0;
9923 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9924 code, atype);
9925 minus_lit0 = 0;
9926
9927 /* Eliminate minus_con0. */
9928 if (minus_con0)
9929 {
9930 if (con0)
9931 con0 = associate_trees (loc, con0, minus_con0,
9932 MINUS_EXPR, atype);
9933 else if (var0)
9934 var0 = associate_trees (loc, var0, minus_con0,
9935 MINUS_EXPR, atype);
9936 else
9937 gcc_unreachable ();
9938 minus_con0 = 0;
9939 }
9940
9941 /* Eliminate minus_var0. */
9942 if (minus_var0)
9943 {
9944 if (con0)
9945 con0 = associate_trees (loc, con0, minus_var0,
9946 MINUS_EXPR, atype);
9947 else
9948 gcc_unreachable ();
9949 minus_var0 = 0;
9950 }
9951
9952 return
9953 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9954 code, atype));
9955 }
9956 }
9957
9958 return NULL_TREE;
9959
9960 case POINTER_DIFF_EXPR:
9961 case MINUS_EXPR:
9962 /* Fold &a[i] - &a[j] to i-j. */
9963 if (TREE_CODE (arg0) == ADDR_EXPR
9964 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9965 && TREE_CODE (arg1) == ADDR_EXPR
9966 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9967 {
9968 tree tem = fold_addr_of_array_ref_difference (loc, type,
9969 TREE_OPERAND (arg0, 0),
9970 TREE_OPERAND (arg1, 0),
9971 code
9972 == POINTER_DIFF_EXPR);
9973 if (tem)
9974 return tem;
9975 }
9976
9977 /* Further transformations are not for pointers. */
9978 if (code == POINTER_DIFF_EXPR)
9979 return NULL_TREE;
9980
9981 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9982 if (TREE_CODE (arg0) == NEGATE_EXPR
9983 && negate_expr_p (op1)
9984 /* If arg0 is e.g. unsigned int and type is int, then this could
9985 introduce UB, because if A is INT_MIN at runtime, the original
9986 expression can be well defined while the latter is not.
9987 See PR83269. */
9988 && !(ANY_INTEGRAL_TYPE_P (type)
9989 && TYPE_OVERFLOW_UNDEFINED (type)
9990 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9991 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9992 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9993 fold_convert_loc (loc, type,
9994 TREE_OPERAND (arg0, 0)));
9995
9996 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9997 __complex__ ( x, -y ). This is not the same for SNaNs or if
9998 signed zeros are involved. */
9999 if (!HONOR_SNANS (element_mode (arg0))
10000 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10001 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10002 {
10003 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10004 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10005 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10006 bool arg0rz = false, arg0iz = false;
10007 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10008 || (arg0i && (arg0iz = real_zerop (arg0i))))
10009 {
10010 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10011 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10012 if (arg0rz && arg1i && real_zerop (arg1i))
10013 {
10014 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10015 arg1r ? arg1r
10016 : build1 (REALPART_EXPR, rtype, arg1));
10017 tree ip = arg0i ? arg0i
10018 : build1 (IMAGPART_EXPR, rtype, arg0);
10019 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10020 }
10021 else if (arg0iz && arg1r && real_zerop (arg1r))
10022 {
10023 tree rp = arg0r ? arg0r
10024 : build1 (REALPART_EXPR, rtype, arg0);
10025 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10026 arg1i ? arg1i
10027 : build1 (IMAGPART_EXPR, rtype, arg1));
10028 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10029 }
10030 }
10031 }
10032
10033 /* A - B -> A + (-B) if B is easily negatable. */
10034 if (negate_expr_p (op1)
10035 && ! TYPE_OVERFLOW_SANITIZED (type)
10036 && ((FLOAT_TYPE_P (type)
10037 /* Avoid this transformation if B is a positive REAL_CST. */
10038 && (TREE_CODE (op1) != REAL_CST
10039 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
10040 || INTEGRAL_TYPE_P (type)))
10041 return fold_build2_loc (loc, PLUS_EXPR, type,
10042 fold_convert_loc (loc, type, arg0),
10043 negate_expr (op1));
10044
10045 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10046 one. Make sure the type is not saturating and has the signedness of
10047 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10048 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10049 if ((TREE_CODE (arg0) == MULT_EXPR
10050 || TREE_CODE (arg1) == MULT_EXPR)
10051 && !TYPE_SATURATING (type)
10052 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10053 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10054 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10055 {
10056 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10057 if (tem)
10058 return tem;
10059 }
10060
10061 goto associate;
10062
10063 case MULT_EXPR:
10064 if (! FLOAT_TYPE_P (type))
10065 {
10066 /* Transform x * -C into -x * C if x is easily negatable. */
10067 if (TREE_CODE (op1) == INTEGER_CST
10068 && tree_int_cst_sgn (op1) == -1
10069 && negate_expr_p (op0)
10070 && negate_expr_p (op1)
10071 && (tem = negate_expr (op1)) != op1
10072 && ! TREE_OVERFLOW (tem))
10073 return fold_build2_loc (loc, MULT_EXPR, type,
10074 fold_convert_loc (loc, type,
10075 negate_expr (op0)), tem);
10076
10077 strict_overflow_p = false;
10078 if (TREE_CODE (arg1) == INTEGER_CST
10079 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10080 &strict_overflow_p)) != 0)
10081 {
10082 if (strict_overflow_p)
10083 fold_overflow_warning (("assuming signed overflow does not "
10084 "occur when simplifying "
10085 "multiplication"),
10086 WARN_STRICT_OVERFLOW_MISC);
10087 return fold_convert_loc (loc, type, tem);
10088 }
10089
10090 /* Optimize z * conj(z) for integer complex numbers. */
10091 if (TREE_CODE (arg0) == CONJ_EXPR
10092 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10093 return fold_mult_zconjz (loc, type, arg1);
10094 if (TREE_CODE (arg1) == CONJ_EXPR
10095 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10096 return fold_mult_zconjz (loc, type, arg0);
10097 }
10098 else
10099 {
10100 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10101 This is not the same for NaNs or if signed zeros are
10102 involved. */
10103 if (!HONOR_NANS (arg0)
10104 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10105 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10106 && TREE_CODE (arg1) == COMPLEX_CST
10107 && real_zerop (TREE_REALPART (arg1)))
10108 {
10109 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10110 if (real_onep (TREE_IMAGPART (arg1)))
10111 return
10112 fold_build2_loc (loc, COMPLEX_EXPR, type,
10113 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10114 rtype, arg0)),
10115 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10116 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10117 return
10118 fold_build2_loc (loc, COMPLEX_EXPR, type,
10119 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10120 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10121 rtype, arg0)));
10122 }
10123
10124 /* Optimize z * conj(z) for floating point complex numbers.
10125 Guarded by flag_unsafe_math_optimizations as non-finite
10126 imaginary components don't produce scalar results. */
10127 if (flag_unsafe_math_optimizations
10128 && TREE_CODE (arg0) == CONJ_EXPR
10129 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10130 return fold_mult_zconjz (loc, type, arg1);
10131 if (flag_unsafe_math_optimizations
10132 && TREE_CODE (arg1) == CONJ_EXPR
10133 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10134 return fold_mult_zconjz (loc, type, arg0);
10135 }
10136 goto associate;
10137
10138 case BIT_IOR_EXPR:
10139 /* Canonicalize (X & C1) | C2. */
10140 if (TREE_CODE (arg0) == BIT_AND_EXPR
10141 && TREE_CODE (arg1) == INTEGER_CST
10142 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10143 {
10144 int width = TYPE_PRECISION (type), w;
10145 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10146 wide_int c2 = wi::to_wide (arg1);
10147
10148 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10149 if ((c1 & c2) == c1)
10150 return omit_one_operand_loc (loc, type, arg1,
10151 TREE_OPERAND (arg0, 0));
10152
10153 wide_int msk = wi::mask (width, false,
10154 TYPE_PRECISION (TREE_TYPE (arg1)));
10155
10156 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10157 if (wi::bit_and_not (msk, c1 | c2) == 0)
10158 {
10159 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10160 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10161 }
10162
10163 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10164 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10165 mode which allows further optimizations. */
10166 c1 &= msk;
10167 c2 &= msk;
10168 wide_int c3 = wi::bit_and_not (c1, c2);
10169 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10170 {
10171 wide_int mask = wi::mask (w, false,
10172 TYPE_PRECISION (type));
10173 if (((c1 | c2) & mask) == mask
10174 && wi::bit_and_not (c1, mask) == 0)
10175 {
10176 c3 = mask;
10177 break;
10178 }
10179 }
10180
10181 if (c3 != c1)
10182 {
10183 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10184 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10185 wide_int_to_tree (type, c3));
10186 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10187 }
10188 }
10189
10190 /* See if this can be simplified into a rotate first. If that
10191 is unsuccessful continue in the association code. */
10192 goto bit_rotate;
10193
10194 case BIT_XOR_EXPR:
10195 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10196 if (TREE_CODE (arg0) == BIT_AND_EXPR
10197 && INTEGRAL_TYPE_P (type)
10198 && integer_onep (TREE_OPERAND (arg0, 1))
10199 && integer_onep (arg1))
10200 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10201 build_zero_cst (TREE_TYPE (arg0)));
10202
10203 /* See if this can be simplified into a rotate first. If that
10204 is unsuccessful continue in the association code. */
10205 goto bit_rotate;
10206
10207 case BIT_AND_EXPR:
10208 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10209 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10210 && INTEGRAL_TYPE_P (type)
10211 && integer_onep (TREE_OPERAND (arg0, 1))
10212 && integer_onep (arg1))
10213 {
10214 tree tem2;
10215 tem = TREE_OPERAND (arg0, 0);
10216 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10217 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10218 tem, tem2);
10219 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10220 build_zero_cst (TREE_TYPE (tem)));
10221 }
10222 /* Fold ~X & 1 as (X & 1) == 0. */
10223 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10224 && INTEGRAL_TYPE_P (type)
10225 && integer_onep (arg1))
10226 {
10227 tree tem2;
10228 tem = TREE_OPERAND (arg0, 0);
10229 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10230 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10231 tem, tem2);
10232 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10233 build_zero_cst (TREE_TYPE (tem)));
10234 }
10235 /* Fold !X & 1 as X == 0. */
10236 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10237 && integer_onep (arg1))
10238 {
10239 tem = TREE_OPERAND (arg0, 0);
10240 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10241 build_zero_cst (TREE_TYPE (tem)));
10242 }
10243
10244 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10245 multiple of 1 << CST. */
10246 if (TREE_CODE (arg1) == INTEGER_CST)
10247 {
10248 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10249 wide_int ncst1 = -cst1;
10250 if ((cst1 & ncst1) == ncst1
10251 && multiple_of_p (type, arg0,
10252 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10253 return fold_convert_loc (loc, type, arg0);
10254 }
10255
10256 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10257 bits from CST2. */
10258 if (TREE_CODE (arg1) == INTEGER_CST
10259 && TREE_CODE (arg0) == MULT_EXPR
10260 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10261 {
10262 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10263 wide_int masked
10264 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10265
10266 if (masked == 0)
10267 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10268 arg0, arg1);
10269 else if (masked != warg1)
10270 {
10271 /* Avoid the transform if arg1 is a mask of some
10272 mode which allows further optimizations. */
10273 int pop = wi::popcount (warg1);
10274 if (!(pop >= BITS_PER_UNIT
10275 && pow2p_hwi (pop)
10276 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10277 return fold_build2_loc (loc, code, type, op0,
10278 wide_int_to_tree (type, masked));
10279 }
10280 }
10281
10282 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10283 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10284 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10285 {
10286 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10287
10288 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10289 if (mask == -1)
10290 return
10291 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10292 }
10293
10294 goto associate;
10295
10296 case RDIV_EXPR:
10297 /* Don't touch a floating-point divide by zero unless the mode
10298 of the constant can represent infinity. */
10299 if (TREE_CODE (arg1) == REAL_CST
10300 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10301 && real_zerop (arg1))
10302 return NULL_TREE;
10303
10304 /* (-A) / (-B) -> A / B */
10305 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10306 return fold_build2_loc (loc, RDIV_EXPR, type,
10307 TREE_OPERAND (arg0, 0),
10308 negate_expr (arg1));
10309 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10310 return fold_build2_loc (loc, RDIV_EXPR, type,
10311 negate_expr (arg0),
10312 TREE_OPERAND (arg1, 0));
10313 return NULL_TREE;
10314
10315 case TRUNC_DIV_EXPR:
10316 /* Fall through */
10317
10318 case FLOOR_DIV_EXPR:
10319 /* Simplify A / (B << N) where A and B are positive and B is
10320 a power of 2, to A >> (N + log2(B)). */
10321 strict_overflow_p = false;
10322 if (TREE_CODE (arg1) == LSHIFT_EXPR
10323 && (TYPE_UNSIGNED (type)
10324 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10325 {
10326 tree sval = TREE_OPERAND (arg1, 0);
10327 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10328 {
10329 tree sh_cnt = TREE_OPERAND (arg1, 1);
10330 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10331 wi::exact_log2 (wi::to_wide (sval)));
10332
10333 if (strict_overflow_p)
10334 fold_overflow_warning (("assuming signed overflow does not "
10335 "occur when simplifying A / (B << N)"),
10336 WARN_STRICT_OVERFLOW_MISC);
10337
10338 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10339 sh_cnt, pow2);
10340 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10341 fold_convert_loc (loc, type, arg0), sh_cnt);
10342 }
10343 }
10344
10345 /* Fall through */
10346
10347 case ROUND_DIV_EXPR:
10348 case CEIL_DIV_EXPR:
10349 case EXACT_DIV_EXPR:
10350 if (integer_zerop (arg1))
10351 return NULL_TREE;
10352
10353 /* Convert -A / -B to A / B when the type is signed and overflow is
10354 undefined. */
10355 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10356 && TREE_CODE (op0) == NEGATE_EXPR
10357 && negate_expr_p (op1))
10358 {
10359 if (INTEGRAL_TYPE_P (type))
10360 fold_overflow_warning (("assuming signed overflow does not occur "
10361 "when distributing negation across "
10362 "division"),
10363 WARN_STRICT_OVERFLOW_MISC);
10364 return fold_build2_loc (loc, code, type,
10365 fold_convert_loc (loc, type,
10366 TREE_OPERAND (arg0, 0)),
10367 negate_expr (op1));
10368 }
10369 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10370 && TREE_CODE (arg1) == NEGATE_EXPR
10371 && negate_expr_p (op0))
10372 {
10373 if (INTEGRAL_TYPE_P (type))
10374 fold_overflow_warning (("assuming signed overflow does not occur "
10375 "when distributing negation across "
10376 "division"),
10377 WARN_STRICT_OVERFLOW_MISC);
10378 return fold_build2_loc (loc, code, type,
10379 negate_expr (op0),
10380 fold_convert_loc (loc, type,
10381 TREE_OPERAND (arg1, 0)));
10382 }
10383
10384 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10385 operation, EXACT_DIV_EXPR.
10386
10387 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10388 At one time others generated faster code, it's not clear if they do
10389 after the last round to changes to the DIV code in expmed.c. */
10390 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10391 && multiple_of_p (type, arg0, arg1))
10392 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10393 fold_convert (type, arg0),
10394 fold_convert (type, arg1));
10395
10396 strict_overflow_p = false;
10397 if (TREE_CODE (arg1) == INTEGER_CST
10398 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10399 &strict_overflow_p)) != 0)
10400 {
10401 if (strict_overflow_p)
10402 fold_overflow_warning (("assuming signed overflow does not occur "
10403 "when simplifying division"),
10404 WARN_STRICT_OVERFLOW_MISC);
10405 return fold_convert_loc (loc, type, tem);
10406 }
10407
10408 return NULL_TREE;
10409
10410 case CEIL_MOD_EXPR:
10411 case FLOOR_MOD_EXPR:
10412 case ROUND_MOD_EXPR:
10413 case TRUNC_MOD_EXPR:
10414 strict_overflow_p = false;
10415 if (TREE_CODE (arg1) == INTEGER_CST
10416 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10417 &strict_overflow_p)) != 0)
10418 {
10419 if (strict_overflow_p)
10420 fold_overflow_warning (("assuming signed overflow does not occur "
10421 "when simplifying modulus"),
10422 WARN_STRICT_OVERFLOW_MISC);
10423 return fold_convert_loc (loc, type, tem);
10424 }
10425
10426 return NULL_TREE;
10427
10428 case LROTATE_EXPR:
10429 case RROTATE_EXPR:
10430 case RSHIFT_EXPR:
10431 case LSHIFT_EXPR:
10432 /* Since negative shift count is not well-defined,
10433 don't try to compute it in the compiler. */
10434 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10435 return NULL_TREE;
10436
10437 prec = element_precision (type);
10438
10439 /* If we have a rotate of a bit operation with the rotate count and
10440 the second operand of the bit operation both constant,
10441 permute the two operations. */
10442 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10443 && (TREE_CODE (arg0) == BIT_AND_EXPR
10444 || TREE_CODE (arg0) == BIT_IOR_EXPR
10445 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10446 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10447 {
10448 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10449 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10450 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10451 fold_build2_loc (loc, code, type,
10452 arg00, arg1),
10453 fold_build2_loc (loc, code, type,
10454 arg01, arg1));
10455 }
10456
10457 /* Two consecutive rotates adding up to the some integer
10458 multiple of the precision of the type can be ignored. */
10459 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10460 && TREE_CODE (arg0) == RROTATE_EXPR
10461 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10462 && wi::umod_trunc (wi::to_wide (arg1)
10463 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10464 prec) == 0)
10465 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10466
10467 return NULL_TREE;
10468
10469 case MIN_EXPR:
10470 case MAX_EXPR:
10471 goto associate;
10472
10473 case TRUTH_ANDIF_EXPR:
10474 /* Note that the operands of this must be ints
10475 and their values must be 0 or 1.
10476 ("true" is a fixed value perhaps depending on the language.) */
10477 /* If first arg is constant zero, return it. */
10478 if (integer_zerop (arg0))
10479 return fold_convert_loc (loc, type, arg0);
10480 /* FALLTHRU */
10481 case TRUTH_AND_EXPR:
10482 /* If either arg is constant true, drop it. */
10483 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10484 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10485 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10486 /* Preserve sequence points. */
10487 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10488 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10489 /* If second arg is constant zero, result is zero, but first arg
10490 must be evaluated. */
10491 if (integer_zerop (arg1))
10492 return omit_one_operand_loc (loc, type, arg1, arg0);
10493 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10494 case will be handled here. */
10495 if (integer_zerop (arg0))
10496 return omit_one_operand_loc (loc, type, arg0, arg1);
10497
10498 /* !X && X is always false. */
10499 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10500 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10501 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10502 /* X && !X is always false. */
10503 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10504 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10505 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10506
10507 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10508 means A >= Y && A != MAX, but in this case we know that
10509 A < X <= MAX. */
10510
10511 if (!TREE_SIDE_EFFECTS (arg0)
10512 && !TREE_SIDE_EFFECTS (arg1))
10513 {
10514 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10515 if (tem && !operand_equal_p (tem, arg0, 0))
10516 return fold_build2_loc (loc, code, type, tem, arg1);
10517
10518 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10519 if (tem && !operand_equal_p (tem, arg1, 0))
10520 return fold_build2_loc (loc, code, type, arg0, tem);
10521 }
10522
10523 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10524 != NULL_TREE)
10525 return tem;
10526
10527 return NULL_TREE;
10528
10529 case TRUTH_ORIF_EXPR:
10530 /* Note that the operands of this must be ints
10531 and their values must be 0 or true.
10532 ("true" is a fixed value perhaps depending on the language.) */
10533 /* If first arg is constant true, return it. */
10534 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10535 return fold_convert_loc (loc, type, arg0);
10536 /* FALLTHRU */
10537 case TRUTH_OR_EXPR:
10538 /* If either arg is constant zero, drop it. */
10539 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10540 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10541 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10542 /* Preserve sequence points. */
10543 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10544 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10545 /* If second arg is constant true, result is true, but we must
10546 evaluate first arg. */
10547 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10548 return omit_one_operand_loc (loc, type, arg1, arg0);
10549 /* Likewise for first arg, but note this only occurs here for
10550 TRUTH_OR_EXPR. */
10551 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10552 return omit_one_operand_loc (loc, type, arg0, arg1);
10553
10554 /* !X || X is always true. */
10555 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10556 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10557 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10558 /* X || !X is always true. */
10559 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10560 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10561 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10562
10563 /* (X && !Y) || (!X && Y) is X ^ Y */
10564 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10565 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10566 {
10567 tree a0, a1, l0, l1, n0, n1;
10568
10569 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10570 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10571
10572 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10573 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10574
10575 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10576 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10577
10578 if ((operand_equal_p (n0, a0, 0)
10579 && operand_equal_p (n1, a1, 0))
10580 || (operand_equal_p (n0, a1, 0)
10581 && operand_equal_p (n1, a0, 0)))
10582 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10583 }
10584
10585 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10586 != NULL_TREE)
10587 return tem;
10588
10589 return NULL_TREE;
10590
10591 case TRUTH_XOR_EXPR:
10592 /* If the second arg is constant zero, drop it. */
10593 if (integer_zerop (arg1))
10594 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10595 /* If the second arg is constant true, this is a logical inversion. */
10596 if (integer_onep (arg1))
10597 {
10598 tem = invert_truthvalue_loc (loc, arg0);
10599 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10600 }
10601 /* Identical arguments cancel to zero. */
10602 if (operand_equal_p (arg0, arg1, 0))
10603 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10604
10605 /* !X ^ X is always true. */
10606 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10607 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10608 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10609
10610 /* X ^ !X is always true. */
10611 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10612 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10613 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10614
10615 return NULL_TREE;
10616
10617 case EQ_EXPR:
10618 case NE_EXPR:
10619 STRIP_NOPS (arg0);
10620 STRIP_NOPS (arg1);
10621
10622 tem = fold_comparison (loc, code, type, op0, op1);
10623 if (tem != NULL_TREE)
10624 return tem;
10625
10626 /* bool_var != 1 becomes !bool_var. */
10627 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10628 && code == NE_EXPR)
10629 return fold_convert_loc (loc, type,
10630 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10631 TREE_TYPE (arg0), arg0));
10632
10633 /* bool_var == 0 becomes !bool_var. */
10634 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10635 && code == EQ_EXPR)
10636 return fold_convert_loc (loc, type,
10637 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10638 TREE_TYPE (arg0), arg0));
10639
10640 /* !exp != 0 becomes !exp */
10641 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10642 && code == NE_EXPR)
10643 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10644
10645 /* If this is an EQ or NE comparison with zero and ARG0 is
10646 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10647 two operations, but the latter can be done in one less insn
10648 on machines that have only two-operand insns or on which a
10649 constant cannot be the first operand. */
10650 if (TREE_CODE (arg0) == BIT_AND_EXPR
10651 && integer_zerop (arg1))
10652 {
10653 tree arg00 = TREE_OPERAND (arg0, 0);
10654 tree arg01 = TREE_OPERAND (arg0, 1);
10655 if (TREE_CODE (arg00) == LSHIFT_EXPR
10656 && integer_onep (TREE_OPERAND (arg00, 0)))
10657 {
10658 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10659 arg01, TREE_OPERAND (arg00, 1));
10660 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10661 build_int_cst (TREE_TYPE (arg0), 1));
10662 return fold_build2_loc (loc, code, type,
10663 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10664 arg1);
10665 }
10666 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10667 && integer_onep (TREE_OPERAND (arg01, 0)))
10668 {
10669 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10670 arg00, TREE_OPERAND (arg01, 1));
10671 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10672 build_int_cst (TREE_TYPE (arg0), 1));
10673 return fold_build2_loc (loc, code, type,
10674 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10675 arg1);
10676 }
10677 }
10678
10679 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10680 C1 is a valid shift constant, and C2 is a power of two, i.e.
10681 a single bit. */
10682 if (TREE_CODE (arg0) == BIT_AND_EXPR
10683 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10684 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10685 == INTEGER_CST
10686 && integer_pow2p (TREE_OPERAND (arg0, 1))
10687 && integer_zerop (arg1))
10688 {
10689 tree itype = TREE_TYPE (arg0);
10690 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10691 prec = TYPE_PRECISION (itype);
10692
10693 /* Check for a valid shift count. */
10694 if (wi::ltu_p (wi::to_wide (arg001), prec))
10695 {
10696 tree arg01 = TREE_OPERAND (arg0, 1);
10697 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10698 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10699 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10700 can be rewritten as (X & (C2 << C1)) != 0. */
10701 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10702 {
10703 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10704 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10705 return fold_build2_loc (loc, code, type, tem,
10706 fold_convert_loc (loc, itype, arg1));
10707 }
10708 /* Otherwise, for signed (arithmetic) shifts,
10709 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10710 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10711 else if (!TYPE_UNSIGNED (itype))
10712 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10713 arg000, build_int_cst (itype, 0));
10714 /* Otherwise, of unsigned (logical) shifts,
10715 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10716 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10717 else
10718 return omit_one_operand_loc (loc, type,
10719 code == EQ_EXPR ? integer_one_node
10720 : integer_zero_node,
10721 arg000);
10722 }
10723 }
10724
10725 /* If this is a comparison of a field, we may be able to simplify it. */
10726 if ((TREE_CODE (arg0) == COMPONENT_REF
10727 || TREE_CODE (arg0) == BIT_FIELD_REF)
10728 /* Handle the constant case even without -O
10729 to make sure the warnings are given. */
10730 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10731 {
10732 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10733 if (t1)
10734 return t1;
10735 }
10736
10737 /* Optimize comparisons of strlen vs zero to a compare of the
10738 first character of the string vs zero. To wit,
10739 strlen(ptr) == 0 => *ptr == 0
10740 strlen(ptr) != 0 => *ptr != 0
10741 Other cases should reduce to one of these two (or a constant)
10742 due to the return value of strlen being unsigned. */
10743 if (TREE_CODE (arg0) == CALL_EXPR
10744 && integer_zerop (arg1))
10745 {
10746 tree fndecl = get_callee_fndecl (arg0);
10747
10748 if (fndecl
10749 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
10750 && call_expr_nargs (arg0) == 1
10751 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10752 {
10753 tree iref = build_fold_indirect_ref_loc (loc,
10754 CALL_EXPR_ARG (arg0, 0));
10755 return fold_build2_loc (loc, code, type, iref,
10756 build_int_cst (TREE_TYPE (iref), 0));
10757 }
10758 }
10759
10760 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10761 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10762 if (TREE_CODE (arg0) == RSHIFT_EXPR
10763 && integer_zerop (arg1)
10764 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10765 {
10766 tree arg00 = TREE_OPERAND (arg0, 0);
10767 tree arg01 = TREE_OPERAND (arg0, 1);
10768 tree itype = TREE_TYPE (arg00);
10769 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10770 {
10771 if (TYPE_UNSIGNED (itype))
10772 {
10773 itype = signed_type_for (itype);
10774 arg00 = fold_convert_loc (loc, itype, arg00);
10775 }
10776 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10777 type, arg00, build_zero_cst (itype));
10778 }
10779 }
10780
10781 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10782 (X & C) == 0 when C is a single bit. */
10783 if (TREE_CODE (arg0) == BIT_AND_EXPR
10784 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10785 && integer_zerop (arg1)
10786 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10787 {
10788 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10789 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10790 TREE_OPERAND (arg0, 1));
10791 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10792 type, tem,
10793 fold_convert_loc (loc, TREE_TYPE (arg0),
10794 arg1));
10795 }
10796
10797 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10798 constant C is a power of two, i.e. a single bit. */
10799 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10800 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10801 && integer_zerop (arg1)
10802 && integer_pow2p (TREE_OPERAND (arg0, 1))
10803 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10804 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10805 {
10806 tree arg00 = TREE_OPERAND (arg0, 0);
10807 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10808 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10809 }
10810
10811 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10812 when is C is a power of two, i.e. a single bit. */
10813 if (TREE_CODE (arg0) == BIT_AND_EXPR
10814 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10815 && integer_zerop (arg1)
10816 && integer_pow2p (TREE_OPERAND (arg0, 1))
10817 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10818 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10819 {
10820 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10821 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10822 arg000, TREE_OPERAND (arg0, 1));
10823 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10824 tem, build_int_cst (TREE_TYPE (tem), 0));
10825 }
10826
10827 if (integer_zerop (arg1)
10828 && tree_expr_nonzero_p (arg0))
10829 {
10830 tree res = constant_boolean_node (code==NE_EXPR, type);
10831 return omit_one_operand_loc (loc, type, res, arg0);
10832 }
10833
10834 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10835 if (TREE_CODE (arg0) == BIT_AND_EXPR
10836 && TREE_CODE (arg1) == BIT_AND_EXPR)
10837 {
10838 tree arg00 = TREE_OPERAND (arg0, 0);
10839 tree arg01 = TREE_OPERAND (arg0, 1);
10840 tree arg10 = TREE_OPERAND (arg1, 0);
10841 tree arg11 = TREE_OPERAND (arg1, 1);
10842 tree itype = TREE_TYPE (arg0);
10843
10844 if (operand_equal_p (arg01, arg11, 0))
10845 {
10846 tem = fold_convert_loc (loc, itype, arg10);
10847 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10848 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10849 return fold_build2_loc (loc, code, type, tem,
10850 build_zero_cst (itype));
10851 }
10852 if (operand_equal_p (arg01, arg10, 0))
10853 {
10854 tem = fold_convert_loc (loc, itype, arg11);
10855 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10856 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10857 return fold_build2_loc (loc, code, type, tem,
10858 build_zero_cst (itype));
10859 }
10860 if (operand_equal_p (arg00, arg11, 0))
10861 {
10862 tem = fold_convert_loc (loc, itype, arg10);
10863 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10864 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10865 return fold_build2_loc (loc, code, type, tem,
10866 build_zero_cst (itype));
10867 }
10868 if (operand_equal_p (arg00, arg10, 0))
10869 {
10870 tem = fold_convert_loc (loc, itype, arg11);
10871 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10872 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10873 return fold_build2_loc (loc, code, type, tem,
10874 build_zero_cst (itype));
10875 }
10876 }
10877
10878 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10879 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10880 {
10881 tree arg00 = TREE_OPERAND (arg0, 0);
10882 tree arg01 = TREE_OPERAND (arg0, 1);
10883 tree arg10 = TREE_OPERAND (arg1, 0);
10884 tree arg11 = TREE_OPERAND (arg1, 1);
10885 tree itype = TREE_TYPE (arg0);
10886
10887 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10888 operand_equal_p guarantees no side-effects so we don't need
10889 to use omit_one_operand on Z. */
10890 if (operand_equal_p (arg01, arg11, 0))
10891 return fold_build2_loc (loc, code, type, arg00,
10892 fold_convert_loc (loc, TREE_TYPE (arg00),
10893 arg10));
10894 if (operand_equal_p (arg01, arg10, 0))
10895 return fold_build2_loc (loc, code, type, arg00,
10896 fold_convert_loc (loc, TREE_TYPE (arg00),
10897 arg11));
10898 if (operand_equal_p (arg00, arg11, 0))
10899 return fold_build2_loc (loc, code, type, arg01,
10900 fold_convert_loc (loc, TREE_TYPE (arg01),
10901 arg10));
10902 if (operand_equal_p (arg00, arg10, 0))
10903 return fold_build2_loc (loc, code, type, arg01,
10904 fold_convert_loc (loc, TREE_TYPE (arg01),
10905 arg11));
10906
10907 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10908 if (TREE_CODE (arg01) == INTEGER_CST
10909 && TREE_CODE (arg11) == INTEGER_CST)
10910 {
10911 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10912 fold_convert_loc (loc, itype, arg11));
10913 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10914 return fold_build2_loc (loc, code, type, tem,
10915 fold_convert_loc (loc, itype, arg10));
10916 }
10917 }
10918
10919 /* Attempt to simplify equality/inequality comparisons of complex
10920 values. Only lower the comparison if the result is known or
10921 can be simplified to a single scalar comparison. */
10922 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10923 || TREE_CODE (arg0) == COMPLEX_CST)
10924 && (TREE_CODE (arg1) == COMPLEX_EXPR
10925 || TREE_CODE (arg1) == COMPLEX_CST))
10926 {
10927 tree real0, imag0, real1, imag1;
10928 tree rcond, icond;
10929
10930 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10931 {
10932 real0 = TREE_OPERAND (arg0, 0);
10933 imag0 = TREE_OPERAND (arg0, 1);
10934 }
10935 else
10936 {
10937 real0 = TREE_REALPART (arg0);
10938 imag0 = TREE_IMAGPART (arg0);
10939 }
10940
10941 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10942 {
10943 real1 = TREE_OPERAND (arg1, 0);
10944 imag1 = TREE_OPERAND (arg1, 1);
10945 }
10946 else
10947 {
10948 real1 = TREE_REALPART (arg1);
10949 imag1 = TREE_IMAGPART (arg1);
10950 }
10951
10952 rcond = fold_binary_loc (loc, code, type, real0, real1);
10953 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10954 {
10955 if (integer_zerop (rcond))
10956 {
10957 if (code == EQ_EXPR)
10958 return omit_two_operands_loc (loc, type, boolean_false_node,
10959 imag0, imag1);
10960 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10961 }
10962 else
10963 {
10964 if (code == NE_EXPR)
10965 return omit_two_operands_loc (loc, type, boolean_true_node,
10966 imag0, imag1);
10967 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10968 }
10969 }
10970
10971 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10972 if (icond && TREE_CODE (icond) == INTEGER_CST)
10973 {
10974 if (integer_zerop (icond))
10975 {
10976 if (code == EQ_EXPR)
10977 return omit_two_operands_loc (loc, type, boolean_false_node,
10978 real0, real1);
10979 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10980 }
10981 else
10982 {
10983 if (code == NE_EXPR)
10984 return omit_two_operands_loc (loc, type, boolean_true_node,
10985 real0, real1);
10986 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10987 }
10988 }
10989 }
10990
10991 return NULL_TREE;
10992
10993 case LT_EXPR:
10994 case GT_EXPR:
10995 case LE_EXPR:
10996 case GE_EXPR:
10997 tem = fold_comparison (loc, code, type, op0, op1);
10998 if (tem != NULL_TREE)
10999 return tem;
11000
11001 /* Transform comparisons of the form X +- C CMP X. */
11002 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11003 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11004 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11005 && !HONOR_SNANS (arg0))
11006 {
11007 tree arg01 = TREE_OPERAND (arg0, 1);
11008 enum tree_code code0 = TREE_CODE (arg0);
11009 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11010
11011 /* (X - c) > X becomes false. */
11012 if (code == GT_EXPR
11013 && ((code0 == MINUS_EXPR && is_positive >= 0)
11014 || (code0 == PLUS_EXPR && is_positive <= 0)))
11015 return constant_boolean_node (0, type);
11016
11017 /* Likewise (X + c) < X becomes false. */
11018 if (code == LT_EXPR
11019 && ((code0 == PLUS_EXPR && is_positive >= 0)
11020 || (code0 == MINUS_EXPR && is_positive <= 0)))
11021 return constant_boolean_node (0, type);
11022
11023 /* Convert (X - c) <= X to true. */
11024 if (!HONOR_NANS (arg1)
11025 && code == LE_EXPR
11026 && ((code0 == MINUS_EXPR && is_positive >= 0)
11027 || (code0 == PLUS_EXPR && is_positive <= 0)))
11028 return constant_boolean_node (1, type);
11029
11030 /* Convert (X + c) >= X to true. */
11031 if (!HONOR_NANS (arg1)
11032 && code == GE_EXPR
11033 && ((code0 == PLUS_EXPR && is_positive >= 0)
11034 || (code0 == MINUS_EXPR && is_positive <= 0)))
11035 return constant_boolean_node (1, type);
11036 }
11037
11038 /* If we are comparing an ABS_EXPR with a constant, we can
11039 convert all the cases into explicit comparisons, but they may
11040 well not be faster than doing the ABS and one comparison.
11041 But ABS (X) <= C is a range comparison, which becomes a subtraction
11042 and a comparison, and is probably faster. */
11043 if (code == LE_EXPR
11044 && TREE_CODE (arg1) == INTEGER_CST
11045 && TREE_CODE (arg0) == ABS_EXPR
11046 && ! TREE_SIDE_EFFECTS (arg0)
11047 && (tem = negate_expr (arg1)) != 0
11048 && TREE_CODE (tem) == INTEGER_CST
11049 && !TREE_OVERFLOW (tem))
11050 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11051 build2 (GE_EXPR, type,
11052 TREE_OPERAND (arg0, 0), tem),
11053 build2 (LE_EXPR, type,
11054 TREE_OPERAND (arg0, 0), arg1));
11055
11056 /* Convert ABS_EXPR<x> >= 0 to true. */
11057 strict_overflow_p = false;
11058 if (code == GE_EXPR
11059 && (integer_zerop (arg1)
11060 || (! HONOR_NANS (arg0)
11061 && real_zerop (arg1)))
11062 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11063 {
11064 if (strict_overflow_p)
11065 fold_overflow_warning (("assuming signed overflow does not occur "
11066 "when simplifying comparison of "
11067 "absolute value and zero"),
11068 WARN_STRICT_OVERFLOW_CONDITIONAL);
11069 return omit_one_operand_loc (loc, type,
11070 constant_boolean_node (true, type),
11071 arg0);
11072 }
11073
11074 /* Convert ABS_EXPR<x> < 0 to false. */
11075 strict_overflow_p = false;
11076 if (code == LT_EXPR
11077 && (integer_zerop (arg1) || real_zerop (arg1))
11078 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11079 {
11080 if (strict_overflow_p)
11081 fold_overflow_warning (("assuming signed overflow does not occur "
11082 "when simplifying comparison of "
11083 "absolute value and zero"),
11084 WARN_STRICT_OVERFLOW_CONDITIONAL);
11085 return omit_one_operand_loc (loc, type,
11086 constant_boolean_node (false, type),
11087 arg0);
11088 }
11089
11090 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11091 and similarly for >= into !=. */
11092 if ((code == LT_EXPR || code == GE_EXPR)
11093 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11094 && TREE_CODE (arg1) == LSHIFT_EXPR
11095 && integer_onep (TREE_OPERAND (arg1, 0)))
11096 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11097 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11098 TREE_OPERAND (arg1, 1)),
11099 build_zero_cst (TREE_TYPE (arg0)));
11100
11101 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11102 otherwise Y might be >= # of bits in X's type and thus e.g.
11103 (unsigned char) (1 << Y) for Y 15 might be 0.
11104 If the cast is widening, then 1 << Y should have unsigned type,
11105 otherwise if Y is number of bits in the signed shift type minus 1,
11106 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11107 31 might be 0xffffffff80000000. */
11108 if ((code == LT_EXPR || code == GE_EXPR)
11109 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11110 && CONVERT_EXPR_P (arg1)
11111 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11112 && (element_precision (TREE_TYPE (arg1))
11113 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11114 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11115 || (element_precision (TREE_TYPE (arg1))
11116 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11117 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11118 {
11119 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11120 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11121 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11122 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11123 build_zero_cst (TREE_TYPE (arg0)));
11124 }
11125
11126 return NULL_TREE;
11127
11128 case UNORDERED_EXPR:
11129 case ORDERED_EXPR:
11130 case UNLT_EXPR:
11131 case UNLE_EXPR:
11132 case UNGT_EXPR:
11133 case UNGE_EXPR:
11134 case UNEQ_EXPR:
11135 case LTGT_EXPR:
11136 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11137 {
11138 tree targ0 = strip_float_extensions (arg0);
11139 tree targ1 = strip_float_extensions (arg1);
11140 tree newtype = TREE_TYPE (targ0);
11141
11142 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11143 newtype = TREE_TYPE (targ1);
11144
11145 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11146 return fold_build2_loc (loc, code, type,
11147 fold_convert_loc (loc, newtype, targ0),
11148 fold_convert_loc (loc, newtype, targ1));
11149 }
11150
11151 return NULL_TREE;
11152
11153 case COMPOUND_EXPR:
11154 /* When pedantic, a compound expression can be neither an lvalue
11155 nor an integer constant expression. */
11156 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11157 return NULL_TREE;
11158 /* Don't let (0, 0) be null pointer constant. */
11159 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11160 : fold_convert_loc (loc, type, arg1);
11161 return pedantic_non_lvalue_loc (loc, tem);
11162
11163 case ASSERT_EXPR:
11164 /* An ASSERT_EXPR should never be passed to fold_binary. */
11165 gcc_unreachable ();
11166
11167 default:
11168 return NULL_TREE;
11169 } /* switch (code) */
11170 }
11171
11172 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11173 ((A & N) + B) & M -> (A + B) & M
11174 Similarly if (N & M) == 0,
11175 ((A | N) + B) & M -> (A + B) & M
11176 and for - instead of + (or unary - instead of +)
11177 and/or ^ instead of |.
11178 If B is constant and (B & M) == 0, fold into A & M.
11179
11180 This function is a helper for match.pd patterns. Return non-NULL
11181 type in which the simplified operation should be performed only
11182 if any optimization is possible.
11183
11184 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
11185 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
11186 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
11187 +/-. */
11188 tree
11189 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
11190 tree arg00, enum tree_code code00, tree arg000, tree arg001,
11191 tree arg01, enum tree_code code01, tree arg010, tree arg011,
11192 tree *pmop)
11193 {
11194 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
11195 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
11196 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11197 if (~cst1 == 0
11198 || (cst1 & (cst1 + 1)) != 0
11199 || !INTEGRAL_TYPE_P (type)
11200 || (!TYPE_OVERFLOW_WRAPS (type)
11201 && TREE_CODE (type) != INTEGER_TYPE)
11202 || (wi::max_value (type) & cst1) != cst1)
11203 return NULL_TREE;
11204
11205 enum tree_code codes[2] = { code00, code01 };
11206 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
11207 int which = 0;
11208 wide_int cst0;
11209
11210 /* Now we know that arg0 is (C + D) or (C - D) or -C and
11211 arg1 (M) is == (1LL << cst) - 1.
11212 Store C into PMOP[0] and D into PMOP[1]. */
11213 pmop[0] = arg00;
11214 pmop[1] = arg01;
11215 which = code != NEGATE_EXPR;
11216
11217 for (; which >= 0; which--)
11218 switch (codes[which])
11219 {
11220 case BIT_AND_EXPR:
11221 case BIT_IOR_EXPR:
11222 case BIT_XOR_EXPR:
11223 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
11224 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
11225 if (codes[which] == BIT_AND_EXPR)
11226 {
11227 if (cst0 != cst1)
11228 break;
11229 }
11230 else if (cst0 != 0)
11231 break;
11232 /* If C or D is of the form (A & N) where
11233 (N & M) == M, or of the form (A | N) or
11234 (A ^ N) where (N & M) == 0, replace it with A. */
11235 pmop[which] = arg0xx[2 * which];
11236 break;
11237 case ERROR_MARK:
11238 if (TREE_CODE (pmop[which]) != INTEGER_CST)
11239 break;
11240 /* If C or D is a N where (N & M) == 0, it can be
11241 omitted (replaced with 0). */
11242 if ((code == PLUS_EXPR
11243 || (code == MINUS_EXPR && which == 0))
11244 && (cst1 & wi::to_wide (pmop[which])) == 0)
11245 pmop[which] = build_int_cst (type, 0);
11246 /* Similarly, with C - N where (-N & M) == 0. */
11247 if (code == MINUS_EXPR
11248 && which == 1
11249 && (cst1 & -wi::to_wide (pmop[which])) == 0)
11250 pmop[which] = build_int_cst (type, 0);
11251 break;
11252 default:
11253 gcc_unreachable ();
11254 }
11255
11256 /* Only build anything new if we optimized one or both arguments above. */
11257 if (pmop[0] == arg00 && pmop[1] == arg01)
11258 return NULL_TREE;
11259
11260 if (TYPE_OVERFLOW_WRAPS (type))
11261 return type;
11262 else
11263 return unsigned_type_for (type);
11264 }
11265
11266 /* Used by contains_label_[p1]. */
11267
11268 struct contains_label_data
11269 {
11270 hash_set<tree> *pset;
11271 bool inside_switch_p;
11272 };
11273
11274 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11275 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11276 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11277
11278 static tree
11279 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11280 {
11281 contains_label_data *d = (contains_label_data *) data;
11282 switch (TREE_CODE (*tp))
11283 {
11284 case LABEL_EXPR:
11285 return *tp;
11286
11287 case CASE_LABEL_EXPR:
11288 if (!d->inside_switch_p)
11289 return *tp;
11290 return NULL_TREE;
11291
11292 case SWITCH_EXPR:
11293 if (!d->inside_switch_p)
11294 {
11295 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11296 return *tp;
11297 d->inside_switch_p = true;
11298 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11299 return *tp;
11300 d->inside_switch_p = false;
11301 *walk_subtrees = 0;
11302 }
11303 return NULL_TREE;
11304
11305 case GOTO_EXPR:
11306 *walk_subtrees = 0;
11307 return NULL_TREE;
11308
11309 default:
11310 return NULL_TREE;
11311 }
11312 }
11313
11314 /* Return whether the sub-tree ST contains a label which is accessible from
11315 outside the sub-tree. */
11316
11317 static bool
11318 contains_label_p (tree st)
11319 {
11320 hash_set<tree> pset;
11321 contains_label_data data = { &pset, false };
11322 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11323 }
11324
11325 /* Fold a ternary expression of code CODE and type TYPE with operands
11326 OP0, OP1, and OP2. Return the folded expression if folding is
11327 successful. Otherwise, return NULL_TREE. */
11328
11329 tree
11330 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11331 tree op0, tree op1, tree op2)
11332 {
11333 tree tem;
11334 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11335 enum tree_code_class kind = TREE_CODE_CLASS (code);
11336
11337 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11338 && TREE_CODE_LENGTH (code) == 3);
11339
11340 /* If this is a commutative operation, and OP0 is a constant, move it
11341 to OP1 to reduce the number of tests below. */
11342 if (commutative_ternary_tree_code (code)
11343 && tree_swap_operands_p (op0, op1))
11344 return fold_build3_loc (loc, code, type, op1, op0, op2);
11345
11346 tem = generic_simplify (loc, code, type, op0, op1, op2);
11347 if (tem)
11348 return tem;
11349
11350 /* Strip any conversions that don't change the mode. This is safe
11351 for every expression, except for a comparison expression because
11352 its signedness is derived from its operands. So, in the latter
11353 case, only strip conversions that don't change the signedness.
11354
11355 Note that this is done as an internal manipulation within the
11356 constant folder, in order to find the simplest representation of
11357 the arguments so that their form can be studied. In any cases,
11358 the appropriate type conversions should be put back in the tree
11359 that will get out of the constant folder. */
11360 if (op0)
11361 {
11362 arg0 = op0;
11363 STRIP_NOPS (arg0);
11364 }
11365
11366 if (op1)
11367 {
11368 arg1 = op1;
11369 STRIP_NOPS (arg1);
11370 }
11371
11372 if (op2)
11373 {
11374 arg2 = op2;
11375 STRIP_NOPS (arg2);
11376 }
11377
11378 switch (code)
11379 {
11380 case COMPONENT_REF:
11381 if (TREE_CODE (arg0) == CONSTRUCTOR
11382 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11383 {
11384 unsigned HOST_WIDE_INT idx;
11385 tree field, value;
11386 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11387 if (field == arg1)
11388 return value;
11389 }
11390 return NULL_TREE;
11391
11392 case COND_EXPR:
11393 case VEC_COND_EXPR:
11394 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11395 so all simple results must be passed through pedantic_non_lvalue. */
11396 if (TREE_CODE (arg0) == INTEGER_CST)
11397 {
11398 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11399 tem = integer_zerop (arg0) ? op2 : op1;
11400 /* Only optimize constant conditions when the selected branch
11401 has the same type as the COND_EXPR. This avoids optimizing
11402 away "c ? x : throw", where the throw has a void type.
11403 Avoid throwing away that operand which contains label. */
11404 if ((!TREE_SIDE_EFFECTS (unused_op)
11405 || !contains_label_p (unused_op))
11406 && (! VOID_TYPE_P (TREE_TYPE (tem))
11407 || VOID_TYPE_P (type)))
11408 return pedantic_non_lvalue_loc (loc, tem);
11409 return NULL_TREE;
11410 }
11411 else if (TREE_CODE (arg0) == VECTOR_CST)
11412 {
11413 unsigned HOST_WIDE_INT nelts;
11414 if ((TREE_CODE (arg1) == VECTOR_CST
11415 || TREE_CODE (arg1) == CONSTRUCTOR)
11416 && (TREE_CODE (arg2) == VECTOR_CST
11417 || TREE_CODE (arg2) == CONSTRUCTOR)
11418 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11419 {
11420 vec_perm_builder sel (nelts, nelts, 1);
11421 for (unsigned int i = 0; i < nelts; i++)
11422 {
11423 tree val = VECTOR_CST_ELT (arg0, i);
11424 if (integer_all_onesp (val))
11425 sel.quick_push (i);
11426 else if (integer_zerop (val))
11427 sel.quick_push (nelts + i);
11428 else /* Currently unreachable. */
11429 return NULL_TREE;
11430 }
11431 vec_perm_indices indices (sel, 2, nelts);
11432 tree t = fold_vec_perm (type, arg1, arg2, indices);
11433 if (t != NULL_TREE)
11434 return t;
11435 }
11436 }
11437
11438 /* If we have A op B ? A : C, we may be able to convert this to a
11439 simpler expression, depending on the operation and the values
11440 of B and C. Signed zeros prevent all of these transformations,
11441 for reasons given above each one.
11442
11443 Also try swapping the arguments and inverting the conditional. */
11444 if (COMPARISON_CLASS_P (arg0)
11445 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11446 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11447 {
11448 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11449 if (tem)
11450 return tem;
11451 }
11452
11453 if (COMPARISON_CLASS_P (arg0)
11454 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11455 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11456 {
11457 location_t loc0 = expr_location_or (arg0, loc);
11458 tem = fold_invert_truthvalue (loc0, arg0);
11459 if (tem && COMPARISON_CLASS_P (tem))
11460 {
11461 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11462 if (tem)
11463 return tem;
11464 }
11465 }
11466
11467 /* If the second operand is simpler than the third, swap them
11468 since that produces better jump optimization results. */
11469 if (truth_value_p (TREE_CODE (arg0))
11470 && tree_swap_operands_p (op1, op2))
11471 {
11472 location_t loc0 = expr_location_or (arg0, loc);
11473 /* See if this can be inverted. If it can't, possibly because
11474 it was a floating-point inequality comparison, don't do
11475 anything. */
11476 tem = fold_invert_truthvalue (loc0, arg0);
11477 if (tem)
11478 return fold_build3_loc (loc, code, type, tem, op2, op1);
11479 }
11480
11481 /* Convert A ? 1 : 0 to simply A. */
11482 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11483 : (integer_onep (op1)
11484 && !VECTOR_TYPE_P (type)))
11485 && integer_zerop (op2)
11486 /* If we try to convert OP0 to our type, the
11487 call to fold will try to move the conversion inside
11488 a COND, which will recurse. In that case, the COND_EXPR
11489 is probably the best choice, so leave it alone. */
11490 && type == TREE_TYPE (arg0))
11491 return pedantic_non_lvalue_loc (loc, arg0);
11492
11493 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11494 over COND_EXPR in cases such as floating point comparisons. */
11495 if (integer_zerop (op1)
11496 && code == COND_EXPR
11497 && integer_onep (op2)
11498 && !VECTOR_TYPE_P (type)
11499 && truth_value_p (TREE_CODE (arg0)))
11500 return pedantic_non_lvalue_loc (loc,
11501 fold_convert_loc (loc, type,
11502 invert_truthvalue_loc (loc,
11503 arg0)));
11504
11505 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11506 if (TREE_CODE (arg0) == LT_EXPR
11507 && integer_zerop (TREE_OPERAND (arg0, 1))
11508 && integer_zerop (op2)
11509 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11510 {
11511 /* sign_bit_p looks through both zero and sign extensions,
11512 but for this optimization only sign extensions are
11513 usable. */
11514 tree tem2 = TREE_OPERAND (arg0, 0);
11515 while (tem != tem2)
11516 {
11517 if (TREE_CODE (tem2) != NOP_EXPR
11518 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11519 {
11520 tem = NULL_TREE;
11521 break;
11522 }
11523 tem2 = TREE_OPERAND (tem2, 0);
11524 }
11525 /* sign_bit_p only checks ARG1 bits within A's precision.
11526 If <sign bit of A> has wider type than A, bits outside
11527 of A's precision in <sign bit of A> need to be checked.
11528 If they are all 0, this optimization needs to be done
11529 in unsigned A's type, if they are all 1 in signed A's type,
11530 otherwise this can't be done. */
11531 if (tem
11532 && TYPE_PRECISION (TREE_TYPE (tem))
11533 < TYPE_PRECISION (TREE_TYPE (arg1))
11534 && TYPE_PRECISION (TREE_TYPE (tem))
11535 < TYPE_PRECISION (type))
11536 {
11537 int inner_width, outer_width;
11538 tree tem_type;
11539
11540 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11541 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11542 if (outer_width > TYPE_PRECISION (type))
11543 outer_width = TYPE_PRECISION (type);
11544
11545 wide_int mask = wi::shifted_mask
11546 (inner_width, outer_width - inner_width, false,
11547 TYPE_PRECISION (TREE_TYPE (arg1)));
11548
11549 wide_int common = mask & wi::to_wide (arg1);
11550 if (common == mask)
11551 {
11552 tem_type = signed_type_for (TREE_TYPE (tem));
11553 tem = fold_convert_loc (loc, tem_type, tem);
11554 }
11555 else if (common == 0)
11556 {
11557 tem_type = unsigned_type_for (TREE_TYPE (tem));
11558 tem = fold_convert_loc (loc, tem_type, tem);
11559 }
11560 else
11561 tem = NULL;
11562 }
11563
11564 if (tem)
11565 return
11566 fold_convert_loc (loc, type,
11567 fold_build2_loc (loc, BIT_AND_EXPR,
11568 TREE_TYPE (tem), tem,
11569 fold_convert_loc (loc,
11570 TREE_TYPE (tem),
11571 arg1)));
11572 }
11573
11574 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11575 already handled above. */
11576 if (TREE_CODE (arg0) == BIT_AND_EXPR
11577 && integer_onep (TREE_OPERAND (arg0, 1))
11578 && integer_zerop (op2)
11579 && integer_pow2p (arg1))
11580 {
11581 tree tem = TREE_OPERAND (arg0, 0);
11582 STRIP_NOPS (tem);
11583 if (TREE_CODE (tem) == RSHIFT_EXPR
11584 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11585 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11586 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11587 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11588 fold_convert_loc (loc, type,
11589 TREE_OPERAND (tem, 0)),
11590 op1);
11591 }
11592
11593 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11594 is probably obsolete because the first operand should be a
11595 truth value (that's why we have the two cases above), but let's
11596 leave it in until we can confirm this for all front-ends. */
11597 if (integer_zerop (op2)
11598 && TREE_CODE (arg0) == NE_EXPR
11599 && integer_zerop (TREE_OPERAND (arg0, 1))
11600 && integer_pow2p (arg1)
11601 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11602 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11603 arg1, OEP_ONLY_CONST)
11604 /* operand_equal_p compares just value, not precision, so e.g.
11605 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
11606 second operand 32-bit -128, which is not a power of two (or vice
11607 versa. */
11608 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
11609 return pedantic_non_lvalue_loc (loc,
11610 fold_convert_loc (loc, type,
11611 TREE_OPERAND (arg0,
11612 0)));
11613
11614 /* Disable the transformations below for vectors, since
11615 fold_binary_op_with_conditional_arg may undo them immediately,
11616 yielding an infinite loop. */
11617 if (code == VEC_COND_EXPR)
11618 return NULL_TREE;
11619
11620 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11621 if (integer_zerop (op2)
11622 && truth_value_p (TREE_CODE (arg0))
11623 && truth_value_p (TREE_CODE (arg1))
11624 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11625 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11626 : TRUTH_ANDIF_EXPR,
11627 type, fold_convert_loc (loc, type, arg0), op1);
11628
11629 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11630 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11631 && truth_value_p (TREE_CODE (arg0))
11632 && truth_value_p (TREE_CODE (arg1))
11633 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11634 {
11635 location_t loc0 = expr_location_or (arg0, loc);
11636 /* Only perform transformation if ARG0 is easily inverted. */
11637 tem = fold_invert_truthvalue (loc0, arg0);
11638 if (tem)
11639 return fold_build2_loc (loc, code == VEC_COND_EXPR
11640 ? BIT_IOR_EXPR
11641 : TRUTH_ORIF_EXPR,
11642 type, fold_convert_loc (loc, type, tem),
11643 op1);
11644 }
11645
11646 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11647 if (integer_zerop (arg1)
11648 && truth_value_p (TREE_CODE (arg0))
11649 && truth_value_p (TREE_CODE (op2))
11650 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11651 {
11652 location_t loc0 = expr_location_or (arg0, loc);
11653 /* Only perform transformation if ARG0 is easily inverted. */
11654 tem = fold_invert_truthvalue (loc0, arg0);
11655 if (tem)
11656 return fold_build2_loc (loc, code == VEC_COND_EXPR
11657 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11658 type, fold_convert_loc (loc, type, tem),
11659 op2);
11660 }
11661
11662 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11663 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11664 && truth_value_p (TREE_CODE (arg0))
11665 && truth_value_p (TREE_CODE (op2))
11666 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11667 return fold_build2_loc (loc, code == VEC_COND_EXPR
11668 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11669 type, fold_convert_loc (loc, type, arg0), op2);
11670
11671 return NULL_TREE;
11672
11673 case CALL_EXPR:
11674 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11675 of fold_ternary on them. */
11676 gcc_unreachable ();
11677
11678 case BIT_FIELD_REF:
11679 if (TREE_CODE (arg0) == VECTOR_CST
11680 && (type == TREE_TYPE (TREE_TYPE (arg0))
11681 || (VECTOR_TYPE_P (type)
11682 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11683 && tree_fits_uhwi_p (op1)
11684 && tree_fits_uhwi_p (op2))
11685 {
11686 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11687 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11688 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11689 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11690
11691 if (n != 0
11692 && (idx % width) == 0
11693 && (n % width) == 0
11694 && known_le ((idx + n) / width,
11695 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11696 {
11697 idx = idx / width;
11698 n = n / width;
11699
11700 if (TREE_CODE (arg0) == VECTOR_CST)
11701 {
11702 if (n == 1)
11703 {
11704 tem = VECTOR_CST_ELT (arg0, idx);
11705 if (VECTOR_TYPE_P (type))
11706 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11707 return tem;
11708 }
11709
11710 tree_vector_builder vals (type, n, 1);
11711 for (unsigned i = 0; i < n; ++i)
11712 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11713 return vals.build ();
11714 }
11715 }
11716 }
11717
11718 /* On constants we can use native encode/interpret to constant
11719 fold (nearly) all BIT_FIELD_REFs. */
11720 if (CONSTANT_CLASS_P (arg0)
11721 && can_native_interpret_type_p (type)
11722 && BITS_PER_UNIT == 8
11723 && tree_fits_uhwi_p (op1)
11724 && tree_fits_uhwi_p (op2))
11725 {
11726 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11727 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11728 /* Limit us to a reasonable amount of work. To relax the
11729 other limitations we need bit-shifting of the buffer
11730 and rounding up the size. */
11731 if (bitpos % BITS_PER_UNIT == 0
11732 && bitsize % BITS_PER_UNIT == 0
11733 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11734 {
11735 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11736 unsigned HOST_WIDE_INT len
11737 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11738 bitpos / BITS_PER_UNIT);
11739 if (len > 0
11740 && len * BITS_PER_UNIT >= bitsize)
11741 {
11742 tree v = native_interpret_expr (type, b,
11743 bitsize / BITS_PER_UNIT);
11744 if (v)
11745 return v;
11746 }
11747 }
11748 }
11749
11750 return NULL_TREE;
11751
11752 case VEC_PERM_EXPR:
11753 if (TREE_CODE (arg2) == VECTOR_CST)
11754 {
11755 /* Build a vector of integers from the tree mask. */
11756 vec_perm_builder builder;
11757 if (!tree_to_vec_perm_builder (&builder, arg2))
11758 return NULL_TREE;
11759
11760 /* Create a vec_perm_indices for the integer vector. */
11761 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11762 bool single_arg = (op0 == op1);
11763 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11764
11765 /* Check for cases that fold to OP0 or OP1 in their original
11766 element order. */
11767 if (sel.series_p (0, 1, 0, 1))
11768 return op0;
11769 if (sel.series_p (0, 1, nelts, 1))
11770 return op1;
11771
11772 if (!single_arg)
11773 {
11774 if (sel.all_from_input_p (0))
11775 op1 = op0;
11776 else if (sel.all_from_input_p (1))
11777 {
11778 op0 = op1;
11779 sel.rotate_inputs (1);
11780 }
11781 }
11782
11783 if ((TREE_CODE (op0) == VECTOR_CST
11784 || TREE_CODE (op0) == CONSTRUCTOR)
11785 && (TREE_CODE (op1) == VECTOR_CST
11786 || TREE_CODE (op1) == CONSTRUCTOR))
11787 {
11788 tree t = fold_vec_perm (type, op0, op1, sel);
11789 if (t != NULL_TREE)
11790 return t;
11791 }
11792
11793 bool changed = (op0 == op1 && !single_arg);
11794
11795 /* Generate a canonical form of the selector. */
11796 if (arg2 == op2 && sel.encoding () != builder)
11797 {
11798 /* Some targets are deficient and fail to expand a single
11799 argument permutation while still allowing an equivalent
11800 2-argument version. */
11801 if (sel.ninputs () == 2
11802 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11803 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11804 else
11805 {
11806 vec_perm_indices sel2 (builder, 2, nelts);
11807 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11808 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11809 else
11810 /* Not directly supported with either encoding,
11811 so use the preferred form. */
11812 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11813 }
11814 changed = true;
11815 }
11816
11817 if (changed)
11818 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11819 }
11820 return NULL_TREE;
11821
11822 case BIT_INSERT_EXPR:
11823 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11824 if (TREE_CODE (arg0) == INTEGER_CST
11825 && TREE_CODE (arg1) == INTEGER_CST)
11826 {
11827 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11828 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11829 wide_int tem = (wi::to_wide (arg0)
11830 & wi::shifted_mask (bitpos, bitsize, true,
11831 TYPE_PRECISION (type)));
11832 wide_int tem2
11833 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11834 bitsize), bitpos);
11835 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11836 }
11837 else if (TREE_CODE (arg0) == VECTOR_CST
11838 && CONSTANT_CLASS_P (arg1)
11839 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11840 TREE_TYPE (arg1)))
11841 {
11842 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11843 unsigned HOST_WIDE_INT elsize
11844 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11845 if (bitpos % elsize == 0)
11846 {
11847 unsigned k = bitpos / elsize;
11848 unsigned HOST_WIDE_INT nelts;
11849 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11850 return arg0;
11851 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11852 {
11853 tree_vector_builder elts (type, nelts, 1);
11854 elts.quick_grow (nelts);
11855 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11856 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11857 return elts.build ();
11858 }
11859 }
11860 }
11861 return NULL_TREE;
11862
11863 default:
11864 return NULL_TREE;
11865 } /* switch (code) */
11866 }
11867
11868 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11869 of an array (or vector). */
11870
11871 tree
11872 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11873 {
11874 tree index_type = NULL_TREE;
11875 offset_int low_bound = 0;
11876
11877 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11878 {
11879 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11880 if (domain_type && TYPE_MIN_VALUE (domain_type))
11881 {
11882 /* Static constructors for variably sized objects makes no sense. */
11883 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11884 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11885 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11886 }
11887 }
11888
11889 if (index_type)
11890 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11891 TYPE_SIGN (index_type));
11892
11893 offset_int index = low_bound - 1;
11894 if (index_type)
11895 index = wi::ext (index, TYPE_PRECISION (index_type),
11896 TYPE_SIGN (index_type));
11897
11898 offset_int max_index;
11899 unsigned HOST_WIDE_INT cnt;
11900 tree cfield, cval;
11901
11902 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11903 {
11904 /* Array constructor might explicitly set index, or specify a range,
11905 or leave index NULL meaning that it is next index after previous
11906 one. */
11907 if (cfield)
11908 {
11909 if (TREE_CODE (cfield) == INTEGER_CST)
11910 max_index = index = wi::to_offset (cfield);
11911 else
11912 {
11913 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11914 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11915 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11916 }
11917 }
11918 else
11919 {
11920 index += 1;
11921 if (index_type)
11922 index = wi::ext (index, TYPE_PRECISION (index_type),
11923 TYPE_SIGN (index_type));
11924 max_index = index;
11925 }
11926
11927 /* Do we have match? */
11928 if (wi::cmpu (access_index, index) >= 0
11929 && wi::cmpu (access_index, max_index) <= 0)
11930 return cval;
11931 }
11932 return NULL_TREE;
11933 }
11934
11935 /* Perform constant folding and related simplification of EXPR.
11936 The related simplifications include x*1 => x, x*0 => 0, etc.,
11937 and application of the associative law.
11938 NOP_EXPR conversions may be removed freely (as long as we
11939 are careful not to change the type of the overall expression).
11940 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11941 but we can constant-fold them if they have constant operands. */
11942
11943 #ifdef ENABLE_FOLD_CHECKING
11944 # define fold(x) fold_1 (x)
11945 static tree fold_1 (tree);
11946 static
11947 #endif
11948 tree
11949 fold (tree expr)
11950 {
11951 const tree t = expr;
11952 enum tree_code code = TREE_CODE (t);
11953 enum tree_code_class kind = TREE_CODE_CLASS (code);
11954 tree tem;
11955 location_t loc = EXPR_LOCATION (expr);
11956
11957 /* Return right away if a constant. */
11958 if (kind == tcc_constant)
11959 return t;
11960
11961 /* CALL_EXPR-like objects with variable numbers of operands are
11962 treated specially. */
11963 if (kind == tcc_vl_exp)
11964 {
11965 if (code == CALL_EXPR)
11966 {
11967 tem = fold_call_expr (loc, expr, false);
11968 return tem ? tem : expr;
11969 }
11970 return expr;
11971 }
11972
11973 if (IS_EXPR_CODE_CLASS (kind))
11974 {
11975 tree type = TREE_TYPE (t);
11976 tree op0, op1, op2;
11977
11978 switch (TREE_CODE_LENGTH (code))
11979 {
11980 case 1:
11981 op0 = TREE_OPERAND (t, 0);
11982 tem = fold_unary_loc (loc, code, type, op0);
11983 return tem ? tem : expr;
11984 case 2:
11985 op0 = TREE_OPERAND (t, 0);
11986 op1 = TREE_OPERAND (t, 1);
11987 tem = fold_binary_loc (loc, code, type, op0, op1);
11988 return tem ? tem : expr;
11989 case 3:
11990 op0 = TREE_OPERAND (t, 0);
11991 op1 = TREE_OPERAND (t, 1);
11992 op2 = TREE_OPERAND (t, 2);
11993 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11994 return tem ? tem : expr;
11995 default:
11996 break;
11997 }
11998 }
11999
12000 switch (code)
12001 {
12002 case ARRAY_REF:
12003 {
12004 tree op0 = TREE_OPERAND (t, 0);
12005 tree op1 = TREE_OPERAND (t, 1);
12006
12007 if (TREE_CODE (op1) == INTEGER_CST
12008 && TREE_CODE (op0) == CONSTRUCTOR
12009 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12010 {
12011 tree val = get_array_ctor_element_at_index (op0,
12012 wi::to_offset (op1));
12013 if (val)
12014 return val;
12015 }
12016
12017 return t;
12018 }
12019
12020 /* Return a VECTOR_CST if possible. */
12021 case CONSTRUCTOR:
12022 {
12023 tree type = TREE_TYPE (t);
12024 if (TREE_CODE (type) != VECTOR_TYPE)
12025 return t;
12026
12027 unsigned i;
12028 tree val;
12029 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12030 if (! CONSTANT_CLASS_P (val))
12031 return t;
12032
12033 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12034 }
12035
12036 case CONST_DECL:
12037 return fold (DECL_INITIAL (t));
12038
12039 default:
12040 return t;
12041 } /* switch (code) */
12042 }
12043
12044 #ifdef ENABLE_FOLD_CHECKING
12045 #undef fold
12046
12047 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12048 hash_table<nofree_ptr_hash<const tree_node> > *);
12049 static void fold_check_failed (const_tree, const_tree);
12050 void print_fold_checksum (const_tree);
12051
12052 /* When --enable-checking=fold, compute a digest of expr before
12053 and after actual fold call to see if fold did not accidentally
12054 change original expr. */
12055
12056 tree
12057 fold (tree expr)
12058 {
12059 tree ret;
12060 struct md5_ctx ctx;
12061 unsigned char checksum_before[16], checksum_after[16];
12062 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12063
12064 md5_init_ctx (&ctx);
12065 fold_checksum_tree (expr, &ctx, &ht);
12066 md5_finish_ctx (&ctx, checksum_before);
12067 ht.empty ();
12068
12069 ret = fold_1 (expr);
12070
12071 md5_init_ctx (&ctx);
12072 fold_checksum_tree (expr, &ctx, &ht);
12073 md5_finish_ctx (&ctx, checksum_after);
12074
12075 if (memcmp (checksum_before, checksum_after, 16))
12076 fold_check_failed (expr, ret);
12077
12078 return ret;
12079 }
12080
12081 void
12082 print_fold_checksum (const_tree expr)
12083 {
12084 struct md5_ctx ctx;
12085 unsigned char checksum[16], cnt;
12086 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12087
12088 md5_init_ctx (&ctx);
12089 fold_checksum_tree (expr, &ctx, &ht);
12090 md5_finish_ctx (&ctx, checksum);
12091 for (cnt = 0; cnt < 16; ++cnt)
12092 fprintf (stderr, "%02x", checksum[cnt]);
12093 putc ('\n', stderr);
12094 }
12095
12096 static void
12097 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12098 {
12099 internal_error ("fold check: original tree changed by fold");
12100 }
12101
12102 static void
12103 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12104 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12105 {
12106 const tree_node **slot;
12107 enum tree_code code;
12108 union tree_node buf;
12109 int i, len;
12110
12111 recursive_label:
12112 if (expr == NULL)
12113 return;
12114 slot = ht->find_slot (expr, INSERT);
12115 if (*slot != NULL)
12116 return;
12117 *slot = expr;
12118 code = TREE_CODE (expr);
12119 if (TREE_CODE_CLASS (code) == tcc_declaration
12120 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12121 {
12122 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12123 memcpy ((char *) &buf, expr, tree_size (expr));
12124 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12125 buf.decl_with_vis.symtab_node = NULL;
12126 expr = (tree) &buf;
12127 }
12128 else if (TREE_CODE_CLASS (code) == tcc_type
12129 && (TYPE_POINTER_TO (expr)
12130 || TYPE_REFERENCE_TO (expr)
12131 || TYPE_CACHED_VALUES_P (expr)
12132 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12133 || TYPE_NEXT_VARIANT (expr)
12134 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12135 {
12136 /* Allow these fields to be modified. */
12137 tree tmp;
12138 memcpy ((char *) &buf, expr, tree_size (expr));
12139 expr = tmp = (tree) &buf;
12140 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12141 TYPE_POINTER_TO (tmp) = NULL;
12142 TYPE_REFERENCE_TO (tmp) = NULL;
12143 TYPE_NEXT_VARIANT (tmp) = NULL;
12144 TYPE_ALIAS_SET (tmp) = -1;
12145 if (TYPE_CACHED_VALUES_P (tmp))
12146 {
12147 TYPE_CACHED_VALUES_P (tmp) = 0;
12148 TYPE_CACHED_VALUES (tmp) = NULL;
12149 }
12150 }
12151 md5_process_bytes (expr, tree_size (expr), ctx);
12152 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12153 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12154 if (TREE_CODE_CLASS (code) != tcc_type
12155 && TREE_CODE_CLASS (code) != tcc_declaration
12156 && code != TREE_LIST
12157 && code != SSA_NAME
12158 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12159 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12160 switch (TREE_CODE_CLASS (code))
12161 {
12162 case tcc_constant:
12163 switch (code)
12164 {
12165 case STRING_CST:
12166 md5_process_bytes (TREE_STRING_POINTER (expr),
12167 TREE_STRING_LENGTH (expr), ctx);
12168 break;
12169 case COMPLEX_CST:
12170 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12171 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12172 break;
12173 case VECTOR_CST:
12174 len = vector_cst_encoded_nelts (expr);
12175 for (i = 0; i < len; ++i)
12176 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12177 break;
12178 default:
12179 break;
12180 }
12181 break;
12182 case tcc_exceptional:
12183 switch (code)
12184 {
12185 case TREE_LIST:
12186 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12187 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12188 expr = TREE_CHAIN (expr);
12189 goto recursive_label;
12190 break;
12191 case TREE_VEC:
12192 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12193 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12194 break;
12195 default:
12196 break;
12197 }
12198 break;
12199 case tcc_expression:
12200 case tcc_reference:
12201 case tcc_comparison:
12202 case tcc_unary:
12203 case tcc_binary:
12204 case tcc_statement:
12205 case tcc_vl_exp:
12206 len = TREE_OPERAND_LENGTH (expr);
12207 for (i = 0; i < len; ++i)
12208 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12209 break;
12210 case tcc_declaration:
12211 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12212 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12213 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12214 {
12215 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12216 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12217 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12218 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12219 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12220 }
12221
12222 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12223 {
12224 if (TREE_CODE (expr) == FUNCTION_DECL)
12225 {
12226 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12227 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12228 }
12229 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12230 }
12231 break;
12232 case tcc_type:
12233 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12234 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12235 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12236 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12237 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12238 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12239 if (INTEGRAL_TYPE_P (expr)
12240 || SCALAR_FLOAT_TYPE_P (expr))
12241 {
12242 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12243 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12244 }
12245 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12246 if (TREE_CODE (expr) == RECORD_TYPE
12247 || TREE_CODE (expr) == UNION_TYPE
12248 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12249 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12250 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12251 break;
12252 default:
12253 break;
12254 }
12255 }
12256
12257 /* Helper function for outputting the checksum of a tree T. When
12258 debugging with gdb, you can "define mynext" to be "next" followed
12259 by "call debug_fold_checksum (op0)", then just trace down till the
12260 outputs differ. */
12261
12262 DEBUG_FUNCTION void
12263 debug_fold_checksum (const_tree t)
12264 {
12265 int i;
12266 unsigned char checksum[16];
12267 struct md5_ctx ctx;
12268 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12269
12270 md5_init_ctx (&ctx);
12271 fold_checksum_tree (t, &ctx, &ht);
12272 md5_finish_ctx (&ctx, checksum);
12273 ht.empty ();
12274
12275 for (i = 0; i < 16; i++)
12276 fprintf (stderr, "%d ", checksum[i]);
12277
12278 fprintf (stderr, "\n");
12279 }
12280
12281 #endif
12282
12283 /* Fold a unary tree expression with code CODE of type TYPE with an
12284 operand OP0. LOC is the location of the resulting expression.
12285 Return a folded expression if successful. Otherwise, return a tree
12286 expression with code CODE of type TYPE with an operand OP0. */
12287
12288 tree
12289 fold_build1_loc (location_t loc,
12290 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12291 {
12292 tree tem;
12293 #ifdef ENABLE_FOLD_CHECKING
12294 unsigned char checksum_before[16], checksum_after[16];
12295 struct md5_ctx ctx;
12296 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12297
12298 md5_init_ctx (&ctx);
12299 fold_checksum_tree (op0, &ctx, &ht);
12300 md5_finish_ctx (&ctx, checksum_before);
12301 ht.empty ();
12302 #endif
12303
12304 tem = fold_unary_loc (loc, code, type, op0);
12305 if (!tem)
12306 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12307
12308 #ifdef ENABLE_FOLD_CHECKING
12309 md5_init_ctx (&ctx);
12310 fold_checksum_tree (op0, &ctx, &ht);
12311 md5_finish_ctx (&ctx, checksum_after);
12312
12313 if (memcmp (checksum_before, checksum_after, 16))
12314 fold_check_failed (op0, tem);
12315 #endif
12316 return tem;
12317 }
12318
12319 /* Fold a binary tree expression with code CODE of type TYPE with
12320 operands OP0 and OP1. LOC is the location of the resulting
12321 expression. Return a folded expression if successful. Otherwise,
12322 return a tree expression with code CODE of type TYPE with operands
12323 OP0 and OP1. */
12324
12325 tree
12326 fold_build2_loc (location_t loc,
12327 enum tree_code code, tree type, tree op0, tree op1
12328 MEM_STAT_DECL)
12329 {
12330 tree tem;
12331 #ifdef ENABLE_FOLD_CHECKING
12332 unsigned char checksum_before_op0[16],
12333 checksum_before_op1[16],
12334 checksum_after_op0[16],
12335 checksum_after_op1[16];
12336 struct md5_ctx ctx;
12337 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12338
12339 md5_init_ctx (&ctx);
12340 fold_checksum_tree (op0, &ctx, &ht);
12341 md5_finish_ctx (&ctx, checksum_before_op0);
12342 ht.empty ();
12343
12344 md5_init_ctx (&ctx);
12345 fold_checksum_tree (op1, &ctx, &ht);
12346 md5_finish_ctx (&ctx, checksum_before_op1);
12347 ht.empty ();
12348 #endif
12349
12350 tem = fold_binary_loc (loc, code, type, op0, op1);
12351 if (!tem)
12352 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12353
12354 #ifdef ENABLE_FOLD_CHECKING
12355 md5_init_ctx (&ctx);
12356 fold_checksum_tree (op0, &ctx, &ht);
12357 md5_finish_ctx (&ctx, checksum_after_op0);
12358 ht.empty ();
12359
12360 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12361 fold_check_failed (op0, tem);
12362
12363 md5_init_ctx (&ctx);
12364 fold_checksum_tree (op1, &ctx, &ht);
12365 md5_finish_ctx (&ctx, checksum_after_op1);
12366
12367 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12368 fold_check_failed (op1, tem);
12369 #endif
12370 return tem;
12371 }
12372
12373 /* Fold a ternary tree expression with code CODE of type TYPE with
12374 operands OP0, OP1, and OP2. Return a folded expression if
12375 successful. Otherwise, return a tree expression with code CODE of
12376 type TYPE with operands OP0, OP1, and OP2. */
12377
12378 tree
12379 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12380 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12381 {
12382 tree tem;
12383 #ifdef ENABLE_FOLD_CHECKING
12384 unsigned char checksum_before_op0[16],
12385 checksum_before_op1[16],
12386 checksum_before_op2[16],
12387 checksum_after_op0[16],
12388 checksum_after_op1[16],
12389 checksum_after_op2[16];
12390 struct md5_ctx ctx;
12391 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12392
12393 md5_init_ctx (&ctx);
12394 fold_checksum_tree (op0, &ctx, &ht);
12395 md5_finish_ctx (&ctx, checksum_before_op0);
12396 ht.empty ();
12397
12398 md5_init_ctx (&ctx);
12399 fold_checksum_tree (op1, &ctx, &ht);
12400 md5_finish_ctx (&ctx, checksum_before_op1);
12401 ht.empty ();
12402
12403 md5_init_ctx (&ctx);
12404 fold_checksum_tree (op2, &ctx, &ht);
12405 md5_finish_ctx (&ctx, checksum_before_op2);
12406 ht.empty ();
12407 #endif
12408
12409 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12410 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12411 if (!tem)
12412 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12413
12414 #ifdef ENABLE_FOLD_CHECKING
12415 md5_init_ctx (&ctx);
12416 fold_checksum_tree (op0, &ctx, &ht);
12417 md5_finish_ctx (&ctx, checksum_after_op0);
12418 ht.empty ();
12419
12420 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12421 fold_check_failed (op0, tem);
12422
12423 md5_init_ctx (&ctx);
12424 fold_checksum_tree (op1, &ctx, &ht);
12425 md5_finish_ctx (&ctx, checksum_after_op1);
12426 ht.empty ();
12427
12428 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12429 fold_check_failed (op1, tem);
12430
12431 md5_init_ctx (&ctx);
12432 fold_checksum_tree (op2, &ctx, &ht);
12433 md5_finish_ctx (&ctx, checksum_after_op2);
12434
12435 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12436 fold_check_failed (op2, tem);
12437 #endif
12438 return tem;
12439 }
12440
12441 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12442 arguments in ARGARRAY, and a null static chain.
12443 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12444 of type TYPE from the given operands as constructed by build_call_array. */
12445
12446 tree
12447 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12448 int nargs, tree *argarray)
12449 {
12450 tree tem;
12451 #ifdef ENABLE_FOLD_CHECKING
12452 unsigned char checksum_before_fn[16],
12453 checksum_before_arglist[16],
12454 checksum_after_fn[16],
12455 checksum_after_arglist[16];
12456 struct md5_ctx ctx;
12457 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12458 int i;
12459
12460 md5_init_ctx (&ctx);
12461 fold_checksum_tree (fn, &ctx, &ht);
12462 md5_finish_ctx (&ctx, checksum_before_fn);
12463 ht.empty ();
12464
12465 md5_init_ctx (&ctx);
12466 for (i = 0; i < nargs; i++)
12467 fold_checksum_tree (argarray[i], &ctx, &ht);
12468 md5_finish_ctx (&ctx, checksum_before_arglist);
12469 ht.empty ();
12470 #endif
12471
12472 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12473 if (!tem)
12474 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12475
12476 #ifdef ENABLE_FOLD_CHECKING
12477 md5_init_ctx (&ctx);
12478 fold_checksum_tree (fn, &ctx, &ht);
12479 md5_finish_ctx (&ctx, checksum_after_fn);
12480 ht.empty ();
12481
12482 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12483 fold_check_failed (fn, tem);
12484
12485 md5_init_ctx (&ctx);
12486 for (i = 0; i < nargs; i++)
12487 fold_checksum_tree (argarray[i], &ctx, &ht);
12488 md5_finish_ctx (&ctx, checksum_after_arglist);
12489
12490 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12491 fold_check_failed (NULL_TREE, tem);
12492 #endif
12493 return tem;
12494 }
12495
12496 /* Perform constant folding and related simplification of initializer
12497 expression EXPR. These behave identically to "fold_buildN" but ignore
12498 potential run-time traps and exceptions that fold must preserve. */
12499
12500 #define START_FOLD_INIT \
12501 int saved_signaling_nans = flag_signaling_nans;\
12502 int saved_trapping_math = flag_trapping_math;\
12503 int saved_rounding_math = flag_rounding_math;\
12504 int saved_trapv = flag_trapv;\
12505 int saved_folding_initializer = folding_initializer;\
12506 flag_signaling_nans = 0;\
12507 flag_trapping_math = 0;\
12508 flag_rounding_math = 0;\
12509 flag_trapv = 0;\
12510 folding_initializer = 1;
12511
12512 #define END_FOLD_INIT \
12513 flag_signaling_nans = saved_signaling_nans;\
12514 flag_trapping_math = saved_trapping_math;\
12515 flag_rounding_math = saved_rounding_math;\
12516 flag_trapv = saved_trapv;\
12517 folding_initializer = saved_folding_initializer;
12518
12519 tree
12520 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12521 tree type, tree op)
12522 {
12523 tree result;
12524 START_FOLD_INIT;
12525
12526 result = fold_build1_loc (loc, code, type, op);
12527
12528 END_FOLD_INIT;
12529 return result;
12530 }
12531
12532 tree
12533 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12534 tree type, tree op0, tree op1)
12535 {
12536 tree result;
12537 START_FOLD_INIT;
12538
12539 result = fold_build2_loc (loc, code, type, op0, op1);
12540
12541 END_FOLD_INIT;
12542 return result;
12543 }
12544
12545 tree
12546 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12547 int nargs, tree *argarray)
12548 {
12549 tree result;
12550 START_FOLD_INIT;
12551
12552 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12553
12554 END_FOLD_INIT;
12555 return result;
12556 }
12557
12558 #undef START_FOLD_INIT
12559 #undef END_FOLD_INIT
12560
12561 /* Determine if first argument is a multiple of second argument. Return 0 if
12562 it is not, or we cannot easily determined it to be.
12563
12564 An example of the sort of thing we care about (at this point; this routine
12565 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12566 fold cases do now) is discovering that
12567
12568 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12569
12570 is a multiple of
12571
12572 SAVE_EXPR (J * 8)
12573
12574 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12575
12576 This code also handles discovering that
12577
12578 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12579
12580 is a multiple of 8 so we don't have to worry about dealing with a
12581 possible remainder.
12582
12583 Note that we *look* inside a SAVE_EXPR only to determine how it was
12584 calculated; it is not safe for fold to do much of anything else with the
12585 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12586 at run time. For example, the latter example above *cannot* be implemented
12587 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12588 evaluation time of the original SAVE_EXPR is not necessarily the same at
12589 the time the new expression is evaluated. The only optimization of this
12590 sort that would be valid is changing
12591
12592 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12593
12594 divided by 8 to
12595
12596 SAVE_EXPR (I) * SAVE_EXPR (J)
12597
12598 (where the same SAVE_EXPR (J) is used in the original and the
12599 transformed version). */
12600
12601 int
12602 multiple_of_p (tree type, const_tree top, const_tree bottom)
12603 {
12604 gimple *stmt;
12605 tree t1, op1, op2;
12606
12607 if (operand_equal_p (top, bottom, 0))
12608 return 1;
12609
12610 if (TREE_CODE (type) != INTEGER_TYPE)
12611 return 0;
12612
12613 switch (TREE_CODE (top))
12614 {
12615 case BIT_AND_EXPR:
12616 /* Bitwise and provides a power of two multiple. If the mask is
12617 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12618 if (!integer_pow2p (bottom))
12619 return 0;
12620 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12621 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12622
12623 case MULT_EXPR:
12624 if (TREE_CODE (bottom) == INTEGER_CST)
12625 {
12626 op1 = TREE_OPERAND (top, 0);
12627 op2 = TREE_OPERAND (top, 1);
12628 if (TREE_CODE (op1) == INTEGER_CST)
12629 std::swap (op1, op2);
12630 if (TREE_CODE (op2) == INTEGER_CST)
12631 {
12632 if (multiple_of_p (type, op2, bottom))
12633 return 1;
12634 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12635 if (multiple_of_p (type, bottom, op2))
12636 {
12637 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12638 wi::to_widest (op2));
12639 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12640 {
12641 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12642 return multiple_of_p (type, op1, op2);
12643 }
12644 }
12645 return multiple_of_p (type, op1, bottom);
12646 }
12647 }
12648 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12649 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12650
12651 case MINUS_EXPR:
12652 /* It is impossible to prove if op0 - op1 is multiple of bottom
12653 precisely, so be conservative here checking if both op0 and op1
12654 are multiple of bottom. Note we check the second operand first
12655 since it's usually simpler. */
12656 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12657 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12658
12659 case PLUS_EXPR:
12660 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12661 as op0 - 3 if the expression has unsigned type. For example,
12662 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12663 op1 = TREE_OPERAND (top, 1);
12664 if (TYPE_UNSIGNED (type)
12665 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12666 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12667 return (multiple_of_p (type, op1, bottom)
12668 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12669
12670 case LSHIFT_EXPR:
12671 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12672 {
12673 op1 = TREE_OPERAND (top, 1);
12674 /* const_binop may not detect overflow correctly,
12675 so check for it explicitly here. */
12676 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12677 wi::to_wide (op1))
12678 && (t1 = fold_convert (type,
12679 const_binop (LSHIFT_EXPR, size_one_node,
12680 op1))) != 0
12681 && !TREE_OVERFLOW (t1))
12682 return multiple_of_p (type, t1, bottom);
12683 }
12684 return 0;
12685
12686 case NOP_EXPR:
12687 /* Can't handle conversions from non-integral or wider integral type. */
12688 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12689 || (TYPE_PRECISION (type)
12690 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12691 return 0;
12692
12693 /* fall through */
12694
12695 case SAVE_EXPR:
12696 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12697
12698 case COND_EXPR:
12699 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12700 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12701
12702 case INTEGER_CST:
12703 if (TREE_CODE (bottom) != INTEGER_CST
12704 || integer_zerop (bottom)
12705 || (TYPE_UNSIGNED (type)
12706 && (tree_int_cst_sgn (top) < 0
12707 || tree_int_cst_sgn (bottom) < 0)))
12708 return 0;
12709 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12710 SIGNED);
12711
12712 case SSA_NAME:
12713 if (TREE_CODE (bottom) == INTEGER_CST
12714 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12715 && gimple_code (stmt) == GIMPLE_ASSIGN)
12716 {
12717 enum tree_code code = gimple_assign_rhs_code (stmt);
12718
12719 /* Check for special cases to see if top is defined as multiple
12720 of bottom:
12721
12722 top = (X & ~(bottom - 1) ; bottom is power of 2
12723
12724 or
12725
12726 Y = X % bottom
12727 top = X - Y. */
12728 if (code == BIT_AND_EXPR
12729 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12730 && TREE_CODE (op2) == INTEGER_CST
12731 && integer_pow2p (bottom)
12732 && wi::multiple_of_p (wi::to_widest (op2),
12733 wi::to_widest (bottom), UNSIGNED))
12734 return 1;
12735
12736 op1 = gimple_assign_rhs1 (stmt);
12737 if (code == MINUS_EXPR
12738 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12739 && TREE_CODE (op2) == SSA_NAME
12740 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12741 && gimple_code (stmt) == GIMPLE_ASSIGN
12742 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12743 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12744 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12745 return 1;
12746 }
12747
12748 /* fall through */
12749
12750 default:
12751 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12752 return multiple_p (wi::to_poly_widest (top),
12753 wi::to_poly_widest (bottom));
12754
12755 return 0;
12756 }
12757 }
12758
12759 #define tree_expr_nonnegative_warnv_p(X, Y) \
12760 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12761
12762 #define RECURSE(X) \
12763 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12764
12765 /* Return true if CODE or TYPE is known to be non-negative. */
12766
12767 static bool
12768 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12769 {
12770 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12771 && truth_value_p (code))
12772 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12773 have a signed:1 type (where the value is -1 and 0). */
12774 return true;
12775 return false;
12776 }
12777
12778 /* Return true if (CODE OP0) is known to be non-negative. If the return
12779 value is based on the assumption that signed overflow is undefined,
12780 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12781 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12782
12783 bool
12784 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12785 bool *strict_overflow_p, int depth)
12786 {
12787 if (TYPE_UNSIGNED (type))
12788 return true;
12789
12790 switch (code)
12791 {
12792 case ABS_EXPR:
12793 /* We can't return 1 if flag_wrapv is set because
12794 ABS_EXPR<INT_MIN> = INT_MIN. */
12795 if (!ANY_INTEGRAL_TYPE_P (type))
12796 return true;
12797 if (TYPE_OVERFLOW_UNDEFINED (type))
12798 {
12799 *strict_overflow_p = true;
12800 return true;
12801 }
12802 break;
12803
12804 case NON_LVALUE_EXPR:
12805 case FLOAT_EXPR:
12806 case FIX_TRUNC_EXPR:
12807 return RECURSE (op0);
12808
12809 CASE_CONVERT:
12810 {
12811 tree inner_type = TREE_TYPE (op0);
12812 tree outer_type = type;
12813
12814 if (TREE_CODE (outer_type) == REAL_TYPE)
12815 {
12816 if (TREE_CODE (inner_type) == REAL_TYPE)
12817 return RECURSE (op0);
12818 if (INTEGRAL_TYPE_P (inner_type))
12819 {
12820 if (TYPE_UNSIGNED (inner_type))
12821 return true;
12822 return RECURSE (op0);
12823 }
12824 }
12825 else if (INTEGRAL_TYPE_P (outer_type))
12826 {
12827 if (TREE_CODE (inner_type) == REAL_TYPE)
12828 return RECURSE (op0);
12829 if (INTEGRAL_TYPE_P (inner_type))
12830 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12831 && TYPE_UNSIGNED (inner_type);
12832 }
12833 }
12834 break;
12835
12836 default:
12837 return tree_simple_nonnegative_warnv_p (code, type);
12838 }
12839
12840 /* We don't know sign of `t', so be conservative and return false. */
12841 return false;
12842 }
12843
12844 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12845 value is based on the assumption that signed overflow is undefined,
12846 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12847 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12848
12849 bool
12850 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12851 tree op1, bool *strict_overflow_p,
12852 int depth)
12853 {
12854 if (TYPE_UNSIGNED (type))
12855 return true;
12856
12857 switch (code)
12858 {
12859 case POINTER_PLUS_EXPR:
12860 case PLUS_EXPR:
12861 if (FLOAT_TYPE_P (type))
12862 return RECURSE (op0) && RECURSE (op1);
12863
12864 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12865 both unsigned and at least 2 bits shorter than the result. */
12866 if (TREE_CODE (type) == INTEGER_TYPE
12867 && TREE_CODE (op0) == NOP_EXPR
12868 && TREE_CODE (op1) == NOP_EXPR)
12869 {
12870 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12871 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12872 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12873 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12874 {
12875 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12876 TYPE_PRECISION (inner2)) + 1;
12877 return prec < TYPE_PRECISION (type);
12878 }
12879 }
12880 break;
12881
12882 case MULT_EXPR:
12883 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12884 {
12885 /* x * x is always non-negative for floating point x
12886 or without overflow. */
12887 if (operand_equal_p (op0, op1, 0)
12888 || (RECURSE (op0) && RECURSE (op1)))
12889 {
12890 if (ANY_INTEGRAL_TYPE_P (type)
12891 && TYPE_OVERFLOW_UNDEFINED (type))
12892 *strict_overflow_p = true;
12893 return true;
12894 }
12895 }
12896
12897 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12898 both unsigned and their total bits is shorter than the result. */
12899 if (TREE_CODE (type) == INTEGER_TYPE
12900 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12901 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12902 {
12903 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12904 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12905 : TREE_TYPE (op0);
12906 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12907 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12908 : TREE_TYPE (op1);
12909
12910 bool unsigned0 = TYPE_UNSIGNED (inner0);
12911 bool unsigned1 = TYPE_UNSIGNED (inner1);
12912
12913 if (TREE_CODE (op0) == INTEGER_CST)
12914 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12915
12916 if (TREE_CODE (op1) == INTEGER_CST)
12917 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12918
12919 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12920 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12921 {
12922 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12923 ? tree_int_cst_min_precision (op0, UNSIGNED)
12924 : TYPE_PRECISION (inner0);
12925
12926 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12927 ? tree_int_cst_min_precision (op1, UNSIGNED)
12928 : TYPE_PRECISION (inner1);
12929
12930 return precision0 + precision1 < TYPE_PRECISION (type);
12931 }
12932 }
12933 return false;
12934
12935 case BIT_AND_EXPR:
12936 case MAX_EXPR:
12937 return RECURSE (op0) || RECURSE (op1);
12938
12939 case BIT_IOR_EXPR:
12940 case BIT_XOR_EXPR:
12941 case MIN_EXPR:
12942 case RDIV_EXPR:
12943 case TRUNC_DIV_EXPR:
12944 case CEIL_DIV_EXPR:
12945 case FLOOR_DIV_EXPR:
12946 case ROUND_DIV_EXPR:
12947 return RECURSE (op0) && RECURSE (op1);
12948
12949 case TRUNC_MOD_EXPR:
12950 return RECURSE (op0);
12951
12952 case FLOOR_MOD_EXPR:
12953 return RECURSE (op1);
12954
12955 case CEIL_MOD_EXPR:
12956 case ROUND_MOD_EXPR:
12957 default:
12958 return tree_simple_nonnegative_warnv_p (code, type);
12959 }
12960
12961 /* We don't know sign of `t', so be conservative and return false. */
12962 return false;
12963 }
12964
12965 /* Return true if T is known to be non-negative. If the return
12966 value is based on the assumption that signed overflow is undefined,
12967 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12968 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12969
12970 bool
12971 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12972 {
12973 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12974 return true;
12975
12976 switch (TREE_CODE (t))
12977 {
12978 case INTEGER_CST:
12979 return tree_int_cst_sgn (t) >= 0;
12980
12981 case REAL_CST:
12982 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12983
12984 case FIXED_CST:
12985 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12986
12987 case COND_EXPR:
12988 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12989
12990 case SSA_NAME:
12991 /* Limit the depth of recursion to avoid quadratic behavior.
12992 This is expected to catch almost all occurrences in practice.
12993 If this code misses important cases that unbounded recursion
12994 would not, passes that need this information could be revised
12995 to provide it through dataflow propagation. */
12996 return (!name_registered_for_update_p (t)
12997 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12998 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12999 strict_overflow_p, depth));
13000
13001 default:
13002 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13003 }
13004 }
13005
13006 /* Return true if T is known to be non-negative. If the return
13007 value is based on the assumption that signed overflow is undefined,
13008 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13009 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13010
13011 bool
13012 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13013 bool *strict_overflow_p, int depth)
13014 {
13015 switch (fn)
13016 {
13017 CASE_CFN_ACOS:
13018 CASE_CFN_ACOSH:
13019 CASE_CFN_CABS:
13020 CASE_CFN_COSH:
13021 CASE_CFN_ERFC:
13022 CASE_CFN_EXP:
13023 CASE_CFN_EXP10:
13024 CASE_CFN_EXP2:
13025 CASE_CFN_FABS:
13026 CASE_CFN_FDIM:
13027 CASE_CFN_HYPOT:
13028 CASE_CFN_POW10:
13029 CASE_CFN_FFS:
13030 CASE_CFN_PARITY:
13031 CASE_CFN_POPCOUNT:
13032 CASE_CFN_CLZ:
13033 CASE_CFN_CLRSB:
13034 case CFN_BUILT_IN_BSWAP32:
13035 case CFN_BUILT_IN_BSWAP64:
13036 /* Always true. */
13037 return true;
13038
13039 CASE_CFN_SQRT:
13040 CASE_CFN_SQRT_FN:
13041 /* sqrt(-0.0) is -0.0. */
13042 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13043 return true;
13044 return RECURSE (arg0);
13045
13046 CASE_CFN_ASINH:
13047 CASE_CFN_ATAN:
13048 CASE_CFN_ATANH:
13049 CASE_CFN_CBRT:
13050 CASE_CFN_CEIL:
13051 CASE_CFN_CEIL_FN:
13052 CASE_CFN_ERF:
13053 CASE_CFN_EXPM1:
13054 CASE_CFN_FLOOR:
13055 CASE_CFN_FLOOR_FN:
13056 CASE_CFN_FMOD:
13057 CASE_CFN_FREXP:
13058 CASE_CFN_ICEIL:
13059 CASE_CFN_IFLOOR:
13060 CASE_CFN_IRINT:
13061 CASE_CFN_IROUND:
13062 CASE_CFN_LCEIL:
13063 CASE_CFN_LDEXP:
13064 CASE_CFN_LFLOOR:
13065 CASE_CFN_LLCEIL:
13066 CASE_CFN_LLFLOOR:
13067 CASE_CFN_LLRINT:
13068 CASE_CFN_LLROUND:
13069 CASE_CFN_LRINT:
13070 CASE_CFN_LROUND:
13071 CASE_CFN_MODF:
13072 CASE_CFN_NEARBYINT:
13073 CASE_CFN_NEARBYINT_FN:
13074 CASE_CFN_RINT:
13075 CASE_CFN_RINT_FN:
13076 CASE_CFN_ROUND:
13077 CASE_CFN_ROUND_FN:
13078 CASE_CFN_SCALB:
13079 CASE_CFN_SCALBLN:
13080 CASE_CFN_SCALBN:
13081 CASE_CFN_SIGNBIT:
13082 CASE_CFN_SIGNIFICAND:
13083 CASE_CFN_SINH:
13084 CASE_CFN_TANH:
13085 CASE_CFN_TRUNC:
13086 CASE_CFN_TRUNC_FN:
13087 /* True if the 1st argument is nonnegative. */
13088 return RECURSE (arg0);
13089
13090 CASE_CFN_FMAX:
13091 CASE_CFN_FMAX_FN:
13092 /* True if the 1st OR 2nd arguments are nonnegative. */
13093 return RECURSE (arg0) || RECURSE (arg1);
13094
13095 CASE_CFN_FMIN:
13096 CASE_CFN_FMIN_FN:
13097 /* True if the 1st AND 2nd arguments are nonnegative. */
13098 return RECURSE (arg0) && RECURSE (arg1);
13099
13100 CASE_CFN_COPYSIGN:
13101 CASE_CFN_COPYSIGN_FN:
13102 /* True if the 2nd argument is nonnegative. */
13103 return RECURSE (arg1);
13104
13105 CASE_CFN_POWI:
13106 /* True if the 1st argument is nonnegative or the second
13107 argument is an even integer. */
13108 if (TREE_CODE (arg1) == INTEGER_CST
13109 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13110 return true;
13111 return RECURSE (arg0);
13112
13113 CASE_CFN_POW:
13114 /* True if the 1st argument is nonnegative or the second
13115 argument is an even integer valued real. */
13116 if (TREE_CODE (arg1) == REAL_CST)
13117 {
13118 REAL_VALUE_TYPE c;
13119 HOST_WIDE_INT n;
13120
13121 c = TREE_REAL_CST (arg1);
13122 n = real_to_integer (&c);
13123 if ((n & 1) == 0)
13124 {
13125 REAL_VALUE_TYPE cint;
13126 real_from_integer (&cint, VOIDmode, n, SIGNED);
13127 if (real_identical (&c, &cint))
13128 return true;
13129 }
13130 }
13131 return RECURSE (arg0);
13132
13133 default:
13134 break;
13135 }
13136 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13137 }
13138
13139 /* Return true if T is known to be non-negative. If the return
13140 value is based on the assumption that signed overflow is undefined,
13141 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13142 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13143
13144 static bool
13145 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13146 {
13147 enum tree_code code = TREE_CODE (t);
13148 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13149 return true;
13150
13151 switch (code)
13152 {
13153 case TARGET_EXPR:
13154 {
13155 tree temp = TARGET_EXPR_SLOT (t);
13156 t = TARGET_EXPR_INITIAL (t);
13157
13158 /* If the initializer is non-void, then it's a normal expression
13159 that will be assigned to the slot. */
13160 if (!VOID_TYPE_P (t))
13161 return RECURSE (t);
13162
13163 /* Otherwise, the initializer sets the slot in some way. One common
13164 way is an assignment statement at the end of the initializer. */
13165 while (1)
13166 {
13167 if (TREE_CODE (t) == BIND_EXPR)
13168 t = expr_last (BIND_EXPR_BODY (t));
13169 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13170 || TREE_CODE (t) == TRY_CATCH_EXPR)
13171 t = expr_last (TREE_OPERAND (t, 0));
13172 else if (TREE_CODE (t) == STATEMENT_LIST)
13173 t = expr_last (t);
13174 else
13175 break;
13176 }
13177 if (TREE_CODE (t) == MODIFY_EXPR
13178 && TREE_OPERAND (t, 0) == temp)
13179 return RECURSE (TREE_OPERAND (t, 1));
13180
13181 return false;
13182 }
13183
13184 case CALL_EXPR:
13185 {
13186 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13187 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13188
13189 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13190 get_call_combined_fn (t),
13191 arg0,
13192 arg1,
13193 strict_overflow_p, depth);
13194 }
13195 case COMPOUND_EXPR:
13196 case MODIFY_EXPR:
13197 return RECURSE (TREE_OPERAND (t, 1));
13198
13199 case BIND_EXPR:
13200 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13201
13202 case SAVE_EXPR:
13203 return RECURSE (TREE_OPERAND (t, 0));
13204
13205 default:
13206 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13207 }
13208 }
13209
13210 #undef RECURSE
13211 #undef tree_expr_nonnegative_warnv_p
13212
13213 /* Return true if T is known to be non-negative. If the return
13214 value is based on the assumption that signed overflow is undefined,
13215 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13216 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13217
13218 bool
13219 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13220 {
13221 enum tree_code code;
13222 if (t == error_mark_node)
13223 return false;
13224
13225 code = TREE_CODE (t);
13226 switch (TREE_CODE_CLASS (code))
13227 {
13228 case tcc_binary:
13229 case tcc_comparison:
13230 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13231 TREE_TYPE (t),
13232 TREE_OPERAND (t, 0),
13233 TREE_OPERAND (t, 1),
13234 strict_overflow_p, depth);
13235
13236 case tcc_unary:
13237 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13238 TREE_TYPE (t),
13239 TREE_OPERAND (t, 0),
13240 strict_overflow_p, depth);
13241
13242 case tcc_constant:
13243 case tcc_declaration:
13244 case tcc_reference:
13245 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13246
13247 default:
13248 break;
13249 }
13250
13251 switch (code)
13252 {
13253 case TRUTH_AND_EXPR:
13254 case TRUTH_OR_EXPR:
13255 case TRUTH_XOR_EXPR:
13256 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13257 TREE_TYPE (t),
13258 TREE_OPERAND (t, 0),
13259 TREE_OPERAND (t, 1),
13260 strict_overflow_p, depth);
13261 case TRUTH_NOT_EXPR:
13262 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13263 TREE_TYPE (t),
13264 TREE_OPERAND (t, 0),
13265 strict_overflow_p, depth);
13266
13267 case COND_EXPR:
13268 case CONSTRUCTOR:
13269 case OBJ_TYPE_REF:
13270 case ASSERT_EXPR:
13271 case ADDR_EXPR:
13272 case WITH_SIZE_EXPR:
13273 case SSA_NAME:
13274 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13275
13276 default:
13277 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13278 }
13279 }
13280
13281 /* Return true if `t' is known to be non-negative. Handle warnings
13282 about undefined signed overflow. */
13283
13284 bool
13285 tree_expr_nonnegative_p (tree t)
13286 {
13287 bool ret, strict_overflow_p;
13288
13289 strict_overflow_p = false;
13290 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13291 if (strict_overflow_p)
13292 fold_overflow_warning (("assuming signed overflow does not occur when "
13293 "determining that expression is always "
13294 "non-negative"),
13295 WARN_STRICT_OVERFLOW_MISC);
13296 return ret;
13297 }
13298
13299
13300 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13301 For floating point we further ensure that T is not denormal.
13302 Similar logic is present in nonzero_address in rtlanal.h.
13303
13304 If the return value is based on the assumption that signed overflow
13305 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13306 change *STRICT_OVERFLOW_P. */
13307
13308 bool
13309 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13310 bool *strict_overflow_p)
13311 {
13312 switch (code)
13313 {
13314 case ABS_EXPR:
13315 return tree_expr_nonzero_warnv_p (op0,
13316 strict_overflow_p);
13317
13318 case NOP_EXPR:
13319 {
13320 tree inner_type = TREE_TYPE (op0);
13321 tree outer_type = type;
13322
13323 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13324 && tree_expr_nonzero_warnv_p (op0,
13325 strict_overflow_p));
13326 }
13327 break;
13328
13329 case NON_LVALUE_EXPR:
13330 return tree_expr_nonzero_warnv_p (op0,
13331 strict_overflow_p);
13332
13333 default:
13334 break;
13335 }
13336
13337 return false;
13338 }
13339
13340 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13341 For floating point we further ensure that T is not denormal.
13342 Similar logic is present in nonzero_address in rtlanal.h.
13343
13344 If the return value is based on the assumption that signed overflow
13345 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13346 change *STRICT_OVERFLOW_P. */
13347
13348 bool
13349 tree_binary_nonzero_warnv_p (enum tree_code code,
13350 tree type,
13351 tree op0,
13352 tree op1, bool *strict_overflow_p)
13353 {
13354 bool sub_strict_overflow_p;
13355 switch (code)
13356 {
13357 case POINTER_PLUS_EXPR:
13358 case PLUS_EXPR:
13359 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13360 {
13361 /* With the presence of negative values it is hard
13362 to say something. */
13363 sub_strict_overflow_p = false;
13364 if (!tree_expr_nonnegative_warnv_p (op0,
13365 &sub_strict_overflow_p)
13366 || !tree_expr_nonnegative_warnv_p (op1,
13367 &sub_strict_overflow_p))
13368 return false;
13369 /* One of operands must be positive and the other non-negative. */
13370 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13371 overflows, on a twos-complement machine the sum of two
13372 nonnegative numbers can never be zero. */
13373 return (tree_expr_nonzero_warnv_p (op0,
13374 strict_overflow_p)
13375 || tree_expr_nonzero_warnv_p (op1,
13376 strict_overflow_p));
13377 }
13378 break;
13379
13380 case MULT_EXPR:
13381 if (TYPE_OVERFLOW_UNDEFINED (type))
13382 {
13383 if (tree_expr_nonzero_warnv_p (op0,
13384 strict_overflow_p)
13385 && tree_expr_nonzero_warnv_p (op1,
13386 strict_overflow_p))
13387 {
13388 *strict_overflow_p = true;
13389 return true;
13390 }
13391 }
13392 break;
13393
13394 case MIN_EXPR:
13395 sub_strict_overflow_p = false;
13396 if (tree_expr_nonzero_warnv_p (op0,
13397 &sub_strict_overflow_p)
13398 && tree_expr_nonzero_warnv_p (op1,
13399 &sub_strict_overflow_p))
13400 {
13401 if (sub_strict_overflow_p)
13402 *strict_overflow_p = true;
13403 }
13404 break;
13405
13406 case MAX_EXPR:
13407 sub_strict_overflow_p = false;
13408 if (tree_expr_nonzero_warnv_p (op0,
13409 &sub_strict_overflow_p))
13410 {
13411 if (sub_strict_overflow_p)
13412 *strict_overflow_p = true;
13413
13414 /* When both operands are nonzero, then MAX must be too. */
13415 if (tree_expr_nonzero_warnv_p (op1,
13416 strict_overflow_p))
13417 return true;
13418
13419 /* MAX where operand 0 is positive is positive. */
13420 return tree_expr_nonnegative_warnv_p (op0,
13421 strict_overflow_p);
13422 }
13423 /* MAX where operand 1 is positive is positive. */
13424 else if (tree_expr_nonzero_warnv_p (op1,
13425 &sub_strict_overflow_p)
13426 && tree_expr_nonnegative_warnv_p (op1,
13427 &sub_strict_overflow_p))
13428 {
13429 if (sub_strict_overflow_p)
13430 *strict_overflow_p = true;
13431 return true;
13432 }
13433 break;
13434
13435 case BIT_IOR_EXPR:
13436 return (tree_expr_nonzero_warnv_p (op1,
13437 strict_overflow_p)
13438 || tree_expr_nonzero_warnv_p (op0,
13439 strict_overflow_p));
13440
13441 default:
13442 break;
13443 }
13444
13445 return false;
13446 }
13447
13448 /* Return true when T is an address and is known to be nonzero.
13449 For floating point we further ensure that T is not denormal.
13450 Similar logic is present in nonzero_address in rtlanal.h.
13451
13452 If the return value is based on the assumption that signed overflow
13453 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13454 change *STRICT_OVERFLOW_P. */
13455
13456 bool
13457 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13458 {
13459 bool sub_strict_overflow_p;
13460 switch (TREE_CODE (t))
13461 {
13462 case INTEGER_CST:
13463 return !integer_zerop (t);
13464
13465 case ADDR_EXPR:
13466 {
13467 tree base = TREE_OPERAND (t, 0);
13468
13469 if (!DECL_P (base))
13470 base = get_base_address (base);
13471
13472 if (base && TREE_CODE (base) == TARGET_EXPR)
13473 base = TARGET_EXPR_SLOT (base);
13474
13475 if (!base)
13476 return false;
13477
13478 /* For objects in symbol table check if we know they are non-zero.
13479 Don't do anything for variables and functions before symtab is built;
13480 it is quite possible that they will be declared weak later. */
13481 int nonzero_addr = maybe_nonzero_address (base);
13482 if (nonzero_addr >= 0)
13483 return nonzero_addr;
13484
13485 /* Constants are never weak. */
13486 if (CONSTANT_CLASS_P (base))
13487 return true;
13488
13489 return false;
13490 }
13491
13492 case COND_EXPR:
13493 sub_strict_overflow_p = false;
13494 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13495 &sub_strict_overflow_p)
13496 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13497 &sub_strict_overflow_p))
13498 {
13499 if (sub_strict_overflow_p)
13500 *strict_overflow_p = true;
13501 return true;
13502 }
13503 break;
13504
13505 case SSA_NAME:
13506 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13507 break;
13508 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13509
13510 default:
13511 break;
13512 }
13513 return false;
13514 }
13515
13516 #define integer_valued_real_p(X) \
13517 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13518
13519 #define RECURSE(X) \
13520 ((integer_valued_real_p) (X, depth + 1))
13521
13522 /* Return true if the floating point result of (CODE OP0) has an
13523 integer value. We also allow +Inf, -Inf and NaN to be considered
13524 integer values. Return false for signaling NaN.
13525
13526 DEPTH is the current nesting depth of the query. */
13527
13528 bool
13529 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13530 {
13531 switch (code)
13532 {
13533 case FLOAT_EXPR:
13534 return true;
13535
13536 case ABS_EXPR:
13537 return RECURSE (op0);
13538
13539 CASE_CONVERT:
13540 {
13541 tree type = TREE_TYPE (op0);
13542 if (TREE_CODE (type) == INTEGER_TYPE)
13543 return true;
13544 if (TREE_CODE (type) == REAL_TYPE)
13545 return RECURSE (op0);
13546 break;
13547 }
13548
13549 default:
13550 break;
13551 }
13552 return false;
13553 }
13554
13555 /* Return true if the floating point result of (CODE OP0 OP1) has an
13556 integer value. We also allow +Inf, -Inf and NaN to be considered
13557 integer values. Return false for signaling NaN.
13558
13559 DEPTH is the current nesting depth of the query. */
13560
13561 bool
13562 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13563 {
13564 switch (code)
13565 {
13566 case PLUS_EXPR:
13567 case MINUS_EXPR:
13568 case MULT_EXPR:
13569 case MIN_EXPR:
13570 case MAX_EXPR:
13571 return RECURSE (op0) && RECURSE (op1);
13572
13573 default:
13574 break;
13575 }
13576 return false;
13577 }
13578
13579 /* Return true if the floating point result of calling FNDECL with arguments
13580 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13581 considered integer values. Return false for signaling NaN. If FNDECL
13582 takes fewer than 2 arguments, the remaining ARGn are null.
13583
13584 DEPTH is the current nesting depth of the query. */
13585
13586 bool
13587 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13588 {
13589 switch (fn)
13590 {
13591 CASE_CFN_CEIL:
13592 CASE_CFN_CEIL_FN:
13593 CASE_CFN_FLOOR:
13594 CASE_CFN_FLOOR_FN:
13595 CASE_CFN_NEARBYINT:
13596 CASE_CFN_NEARBYINT_FN:
13597 CASE_CFN_RINT:
13598 CASE_CFN_RINT_FN:
13599 CASE_CFN_ROUND:
13600 CASE_CFN_ROUND_FN:
13601 CASE_CFN_TRUNC:
13602 CASE_CFN_TRUNC_FN:
13603 return true;
13604
13605 CASE_CFN_FMIN:
13606 CASE_CFN_FMIN_FN:
13607 CASE_CFN_FMAX:
13608 CASE_CFN_FMAX_FN:
13609 return RECURSE (arg0) && RECURSE (arg1);
13610
13611 default:
13612 break;
13613 }
13614 return false;
13615 }
13616
13617 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13618 has an integer value. We also allow +Inf, -Inf and NaN to be
13619 considered integer values. Return false for signaling NaN.
13620
13621 DEPTH is the current nesting depth of the query. */
13622
13623 bool
13624 integer_valued_real_single_p (tree t, int depth)
13625 {
13626 switch (TREE_CODE (t))
13627 {
13628 case REAL_CST:
13629 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13630
13631 case COND_EXPR:
13632 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13633
13634 case SSA_NAME:
13635 /* Limit the depth of recursion to avoid quadratic behavior.
13636 This is expected to catch almost all occurrences in practice.
13637 If this code misses important cases that unbounded recursion
13638 would not, passes that need this information could be revised
13639 to provide it through dataflow propagation. */
13640 return (!name_registered_for_update_p (t)
13641 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13642 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13643 depth));
13644
13645 default:
13646 break;
13647 }
13648 return false;
13649 }
13650
13651 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13652 has an integer value. We also allow +Inf, -Inf and NaN to be
13653 considered integer values. Return false for signaling NaN.
13654
13655 DEPTH is the current nesting depth of the query. */
13656
13657 static bool
13658 integer_valued_real_invalid_p (tree t, int depth)
13659 {
13660 switch (TREE_CODE (t))
13661 {
13662 case COMPOUND_EXPR:
13663 case MODIFY_EXPR:
13664 case BIND_EXPR:
13665 return RECURSE (TREE_OPERAND (t, 1));
13666
13667 case SAVE_EXPR:
13668 return RECURSE (TREE_OPERAND (t, 0));
13669
13670 default:
13671 break;
13672 }
13673 return false;
13674 }
13675
13676 #undef RECURSE
13677 #undef integer_valued_real_p
13678
13679 /* Return true if the floating point expression T has an integer value.
13680 We also allow +Inf, -Inf and NaN to be considered integer values.
13681 Return false for signaling NaN.
13682
13683 DEPTH is the current nesting depth of the query. */
13684
13685 bool
13686 integer_valued_real_p (tree t, int depth)
13687 {
13688 if (t == error_mark_node)
13689 return false;
13690
13691 STRIP_ANY_LOCATION_WRAPPER (t);
13692
13693 tree_code code = TREE_CODE (t);
13694 switch (TREE_CODE_CLASS (code))
13695 {
13696 case tcc_binary:
13697 case tcc_comparison:
13698 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13699 TREE_OPERAND (t, 1), depth);
13700
13701 case tcc_unary:
13702 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13703
13704 case tcc_constant:
13705 case tcc_declaration:
13706 case tcc_reference:
13707 return integer_valued_real_single_p (t, depth);
13708
13709 default:
13710 break;
13711 }
13712
13713 switch (code)
13714 {
13715 case COND_EXPR:
13716 case SSA_NAME:
13717 return integer_valued_real_single_p (t, depth);
13718
13719 case CALL_EXPR:
13720 {
13721 tree arg0 = (call_expr_nargs (t) > 0
13722 ? CALL_EXPR_ARG (t, 0)
13723 : NULL_TREE);
13724 tree arg1 = (call_expr_nargs (t) > 1
13725 ? CALL_EXPR_ARG (t, 1)
13726 : NULL_TREE);
13727 return integer_valued_real_call_p (get_call_combined_fn (t),
13728 arg0, arg1, depth);
13729 }
13730
13731 default:
13732 return integer_valued_real_invalid_p (t, depth);
13733 }
13734 }
13735
13736 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13737 attempt to fold the expression to a constant without modifying TYPE,
13738 OP0 or OP1.
13739
13740 If the expression could be simplified to a constant, then return
13741 the constant. If the expression would not be simplified to a
13742 constant, then return NULL_TREE. */
13743
13744 tree
13745 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13746 {
13747 tree tem = fold_binary (code, type, op0, op1);
13748 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13749 }
13750
13751 /* Given the components of a unary expression CODE, TYPE and OP0,
13752 attempt to fold the expression to a constant without modifying
13753 TYPE or OP0.
13754
13755 If the expression could be simplified to a constant, then return
13756 the constant. If the expression would not be simplified to a
13757 constant, then return NULL_TREE. */
13758
13759 tree
13760 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13761 {
13762 tree tem = fold_unary (code, type, op0);
13763 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13764 }
13765
13766 /* If EXP represents referencing an element in a constant string
13767 (either via pointer arithmetic or array indexing), return the
13768 tree representing the value accessed, otherwise return NULL. */
13769
13770 tree
13771 fold_read_from_constant_string (tree exp)
13772 {
13773 if ((TREE_CODE (exp) == INDIRECT_REF
13774 || TREE_CODE (exp) == ARRAY_REF)
13775 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13776 {
13777 tree exp1 = TREE_OPERAND (exp, 0);
13778 tree index;
13779 tree string;
13780 location_t loc = EXPR_LOCATION (exp);
13781
13782 if (TREE_CODE (exp) == INDIRECT_REF)
13783 string = string_constant (exp1, &index, NULL, NULL);
13784 else
13785 {
13786 tree low_bound = array_ref_low_bound (exp);
13787 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13788
13789 /* Optimize the special-case of a zero lower bound.
13790
13791 We convert the low_bound to sizetype to avoid some problems
13792 with constant folding. (E.g. suppose the lower bound is 1,
13793 and its mode is QI. Without the conversion,l (ARRAY
13794 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13795 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13796 if (! integer_zerop (low_bound))
13797 index = size_diffop_loc (loc, index,
13798 fold_convert_loc (loc, sizetype, low_bound));
13799
13800 string = exp1;
13801 }
13802
13803 scalar_int_mode char_mode;
13804 if (string
13805 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13806 && TREE_CODE (string) == STRING_CST
13807 && TREE_CODE (index) == INTEGER_CST
13808 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13809 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13810 &char_mode)
13811 && GET_MODE_SIZE (char_mode) == 1)
13812 return build_int_cst_type (TREE_TYPE (exp),
13813 (TREE_STRING_POINTER (string)
13814 [TREE_INT_CST_LOW (index)]));
13815 }
13816 return NULL;
13817 }
13818
13819 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13820 an integer constant, real, or fixed-point constant.
13821
13822 TYPE is the type of the result. */
13823
13824 static tree
13825 fold_negate_const (tree arg0, tree type)
13826 {
13827 tree t = NULL_TREE;
13828
13829 switch (TREE_CODE (arg0))
13830 {
13831 case REAL_CST:
13832 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13833 break;
13834
13835 case FIXED_CST:
13836 {
13837 FIXED_VALUE_TYPE f;
13838 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13839 &(TREE_FIXED_CST (arg0)), NULL,
13840 TYPE_SATURATING (type));
13841 t = build_fixed (type, f);
13842 /* Propagate overflow flags. */
13843 if (overflow_p | TREE_OVERFLOW (arg0))
13844 TREE_OVERFLOW (t) = 1;
13845 break;
13846 }
13847
13848 default:
13849 if (poly_int_tree_p (arg0))
13850 {
13851 wi::overflow_type overflow;
13852 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13853 t = force_fit_type (type, res, 1,
13854 (overflow && ! TYPE_UNSIGNED (type))
13855 || TREE_OVERFLOW (arg0));
13856 break;
13857 }
13858
13859 gcc_unreachable ();
13860 }
13861
13862 return t;
13863 }
13864
13865 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13866 an integer constant or real constant.
13867
13868 TYPE is the type of the result. */
13869
13870 tree
13871 fold_abs_const (tree arg0, tree type)
13872 {
13873 tree t = NULL_TREE;
13874
13875 switch (TREE_CODE (arg0))
13876 {
13877 case INTEGER_CST:
13878 {
13879 /* If the value is unsigned or non-negative, then the absolute value
13880 is the same as the ordinary value. */
13881 wide_int val = wi::to_wide (arg0);
13882 wi::overflow_type overflow = wi::OVF_NONE;
13883 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
13884 ;
13885
13886 /* If the value is negative, then the absolute value is
13887 its negation. */
13888 else
13889 val = wi::neg (val, &overflow);
13890
13891 /* Force to the destination type, set TREE_OVERFLOW for signed
13892 TYPE only. */
13893 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
13894 }
13895 break;
13896
13897 case REAL_CST:
13898 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13899 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13900 else
13901 t = arg0;
13902 break;
13903
13904 default:
13905 gcc_unreachable ();
13906 }
13907
13908 return t;
13909 }
13910
13911 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13912 constant. TYPE is the type of the result. */
13913
13914 static tree
13915 fold_not_const (const_tree arg0, tree type)
13916 {
13917 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13918
13919 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13920 }
13921
13922 /* Given CODE, a relational operator, the target type, TYPE and two
13923 constant operands OP0 and OP1, return the result of the
13924 relational operation. If the result is not a compile time
13925 constant, then return NULL_TREE. */
13926
13927 static tree
13928 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13929 {
13930 int result, invert;
13931
13932 /* From here on, the only cases we handle are when the result is
13933 known to be a constant. */
13934
13935 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13936 {
13937 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13938 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13939
13940 /* Handle the cases where either operand is a NaN. */
13941 if (real_isnan (c0) || real_isnan (c1))
13942 {
13943 switch (code)
13944 {
13945 case EQ_EXPR:
13946 case ORDERED_EXPR:
13947 result = 0;
13948 break;
13949
13950 case NE_EXPR:
13951 case UNORDERED_EXPR:
13952 case UNLT_EXPR:
13953 case UNLE_EXPR:
13954 case UNGT_EXPR:
13955 case UNGE_EXPR:
13956 case UNEQ_EXPR:
13957 result = 1;
13958 break;
13959
13960 case LT_EXPR:
13961 case LE_EXPR:
13962 case GT_EXPR:
13963 case GE_EXPR:
13964 case LTGT_EXPR:
13965 if (flag_trapping_math)
13966 return NULL_TREE;
13967 result = 0;
13968 break;
13969
13970 default:
13971 gcc_unreachable ();
13972 }
13973
13974 return constant_boolean_node (result, type);
13975 }
13976
13977 return constant_boolean_node (real_compare (code, c0, c1), type);
13978 }
13979
13980 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13981 {
13982 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13983 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13984 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13985 }
13986
13987 /* Handle equality/inequality of complex constants. */
13988 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13989 {
13990 tree rcond = fold_relational_const (code, type,
13991 TREE_REALPART (op0),
13992 TREE_REALPART (op1));
13993 tree icond = fold_relational_const (code, type,
13994 TREE_IMAGPART (op0),
13995 TREE_IMAGPART (op1));
13996 if (code == EQ_EXPR)
13997 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13998 else if (code == NE_EXPR)
13999 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14000 else
14001 return NULL_TREE;
14002 }
14003
14004 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14005 {
14006 if (!VECTOR_TYPE_P (type))
14007 {
14008 /* Have vector comparison with scalar boolean result. */
14009 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14010 && known_eq (VECTOR_CST_NELTS (op0),
14011 VECTOR_CST_NELTS (op1)));
14012 unsigned HOST_WIDE_INT nunits;
14013 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
14014 return NULL_TREE;
14015 for (unsigned i = 0; i < nunits; i++)
14016 {
14017 tree elem0 = VECTOR_CST_ELT (op0, i);
14018 tree elem1 = VECTOR_CST_ELT (op1, i);
14019 tree tmp = fold_relational_const (code, type, elem0, elem1);
14020 if (tmp == NULL_TREE)
14021 return NULL_TREE;
14022 if (integer_zerop (tmp))
14023 return constant_boolean_node (false, type);
14024 }
14025 return constant_boolean_node (true, type);
14026 }
14027 tree_vector_builder elts;
14028 if (!elts.new_binary_operation (type, op0, op1, false))
14029 return NULL_TREE;
14030 unsigned int count = elts.encoded_nelts ();
14031 for (unsigned i = 0; i < count; i++)
14032 {
14033 tree elem_type = TREE_TYPE (type);
14034 tree elem0 = VECTOR_CST_ELT (op0, i);
14035 tree elem1 = VECTOR_CST_ELT (op1, i);
14036
14037 tree tem = fold_relational_const (code, elem_type,
14038 elem0, elem1);
14039
14040 if (tem == NULL_TREE)
14041 return NULL_TREE;
14042
14043 elts.quick_push (build_int_cst (elem_type,
14044 integer_zerop (tem) ? 0 : -1));
14045 }
14046
14047 return elts.build ();
14048 }
14049
14050 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14051
14052 To compute GT, swap the arguments and do LT.
14053 To compute GE, do LT and invert the result.
14054 To compute LE, swap the arguments, do LT and invert the result.
14055 To compute NE, do EQ and invert the result.
14056
14057 Therefore, the code below must handle only EQ and LT. */
14058
14059 if (code == LE_EXPR || code == GT_EXPR)
14060 {
14061 std::swap (op0, op1);
14062 code = swap_tree_comparison (code);
14063 }
14064
14065 /* Note that it is safe to invert for real values here because we
14066 have already handled the one case that it matters. */
14067
14068 invert = 0;
14069 if (code == NE_EXPR || code == GE_EXPR)
14070 {
14071 invert = 1;
14072 code = invert_tree_comparison (code, false);
14073 }
14074
14075 /* Compute a result for LT or EQ if args permit;
14076 Otherwise return T. */
14077 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14078 {
14079 if (code == EQ_EXPR)
14080 result = tree_int_cst_equal (op0, op1);
14081 else
14082 result = tree_int_cst_lt (op0, op1);
14083 }
14084 else
14085 return NULL_TREE;
14086
14087 if (invert)
14088 result ^= 1;
14089 return constant_boolean_node (result, type);
14090 }
14091
14092 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14093 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14094 itself. */
14095
14096 tree
14097 fold_build_cleanup_point_expr (tree type, tree expr)
14098 {
14099 /* If the expression does not have side effects then we don't have to wrap
14100 it with a cleanup point expression. */
14101 if (!TREE_SIDE_EFFECTS (expr))
14102 return expr;
14103
14104 /* If the expression is a return, check to see if the expression inside the
14105 return has no side effects or the right hand side of the modify expression
14106 inside the return. If either don't have side effects set we don't need to
14107 wrap the expression in a cleanup point expression. Note we don't check the
14108 left hand side of the modify because it should always be a return decl. */
14109 if (TREE_CODE (expr) == RETURN_EXPR)
14110 {
14111 tree op = TREE_OPERAND (expr, 0);
14112 if (!op || !TREE_SIDE_EFFECTS (op))
14113 return expr;
14114 op = TREE_OPERAND (op, 1);
14115 if (!TREE_SIDE_EFFECTS (op))
14116 return expr;
14117 }
14118
14119 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14120 }
14121
14122 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14123 of an indirection through OP0, or NULL_TREE if no simplification is
14124 possible. */
14125
14126 tree
14127 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14128 {
14129 tree sub = op0;
14130 tree subtype;
14131 poly_uint64 const_op01;
14132
14133 STRIP_NOPS (sub);
14134 subtype = TREE_TYPE (sub);
14135 if (!POINTER_TYPE_P (subtype)
14136 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14137 return NULL_TREE;
14138
14139 if (TREE_CODE (sub) == ADDR_EXPR)
14140 {
14141 tree op = TREE_OPERAND (sub, 0);
14142 tree optype = TREE_TYPE (op);
14143
14144 /* *&CONST_DECL -> to the value of the const decl. */
14145 if (TREE_CODE (op) == CONST_DECL)
14146 return DECL_INITIAL (op);
14147 /* *&p => p; make sure to handle *&"str"[cst] here. */
14148 if (type == optype)
14149 {
14150 tree fop = fold_read_from_constant_string (op);
14151 if (fop)
14152 return fop;
14153 else
14154 return op;
14155 }
14156 /* *(foo *)&fooarray => fooarray[0] */
14157 else if (TREE_CODE (optype) == ARRAY_TYPE
14158 && type == TREE_TYPE (optype)
14159 && (!in_gimple_form
14160 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14161 {
14162 tree type_domain = TYPE_DOMAIN (optype);
14163 tree min_val = size_zero_node;
14164 if (type_domain && TYPE_MIN_VALUE (type_domain))
14165 min_val = TYPE_MIN_VALUE (type_domain);
14166 if (in_gimple_form
14167 && TREE_CODE (min_val) != INTEGER_CST)
14168 return NULL_TREE;
14169 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14170 NULL_TREE, NULL_TREE);
14171 }
14172 /* *(foo *)&complexfoo => __real__ complexfoo */
14173 else if (TREE_CODE (optype) == COMPLEX_TYPE
14174 && type == TREE_TYPE (optype))
14175 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14176 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14177 else if (VECTOR_TYPE_P (optype)
14178 && type == TREE_TYPE (optype))
14179 {
14180 tree part_width = TYPE_SIZE (type);
14181 tree index = bitsize_int (0);
14182 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14183 index);
14184 }
14185 }
14186
14187 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14188 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14189 {
14190 tree op00 = TREE_OPERAND (sub, 0);
14191 tree op01 = TREE_OPERAND (sub, 1);
14192
14193 STRIP_NOPS (op00);
14194 if (TREE_CODE (op00) == ADDR_EXPR)
14195 {
14196 tree op00type;
14197 op00 = TREE_OPERAND (op00, 0);
14198 op00type = TREE_TYPE (op00);
14199
14200 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14201 if (VECTOR_TYPE_P (op00type)
14202 && type == TREE_TYPE (op00type)
14203 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14204 but we want to treat offsets with MSB set as negative.
14205 For the code below negative offsets are invalid and
14206 TYPE_SIZE of the element is something unsigned, so
14207 check whether op01 fits into poly_int64, which implies
14208 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14209 then just use poly_uint64 because we want to treat the
14210 value as unsigned. */
14211 && tree_fits_poly_int64_p (op01))
14212 {
14213 tree part_width = TYPE_SIZE (type);
14214 poly_uint64 max_offset
14215 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14216 * TYPE_VECTOR_SUBPARTS (op00type));
14217 if (known_lt (const_op01, max_offset))
14218 {
14219 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14220 return fold_build3_loc (loc,
14221 BIT_FIELD_REF, type, op00,
14222 part_width, index);
14223 }
14224 }
14225 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14226 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14227 && type == TREE_TYPE (op00type))
14228 {
14229 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14230 const_op01))
14231 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14232 }
14233 /* ((foo *)&fooarray)[1] => fooarray[1] */
14234 else if (TREE_CODE (op00type) == ARRAY_TYPE
14235 && type == TREE_TYPE (op00type))
14236 {
14237 tree type_domain = TYPE_DOMAIN (op00type);
14238 tree min_val = size_zero_node;
14239 if (type_domain && TYPE_MIN_VALUE (type_domain))
14240 min_val = TYPE_MIN_VALUE (type_domain);
14241 poly_uint64 type_size, index;
14242 if (poly_int_tree_p (min_val)
14243 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14244 && multiple_p (const_op01, type_size, &index))
14245 {
14246 poly_offset_int off = index + wi::to_poly_offset (min_val);
14247 op01 = wide_int_to_tree (sizetype, off);
14248 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14249 NULL_TREE, NULL_TREE);
14250 }
14251 }
14252 }
14253 }
14254
14255 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14256 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14257 && type == TREE_TYPE (TREE_TYPE (subtype))
14258 && (!in_gimple_form
14259 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14260 {
14261 tree type_domain;
14262 tree min_val = size_zero_node;
14263 sub = build_fold_indirect_ref_loc (loc, sub);
14264 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14265 if (type_domain && TYPE_MIN_VALUE (type_domain))
14266 min_val = TYPE_MIN_VALUE (type_domain);
14267 if (in_gimple_form
14268 && TREE_CODE (min_val) != INTEGER_CST)
14269 return NULL_TREE;
14270 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14271 NULL_TREE);
14272 }
14273
14274 return NULL_TREE;
14275 }
14276
14277 /* Builds an expression for an indirection through T, simplifying some
14278 cases. */
14279
14280 tree
14281 build_fold_indirect_ref_loc (location_t loc, tree t)
14282 {
14283 tree type = TREE_TYPE (TREE_TYPE (t));
14284 tree sub = fold_indirect_ref_1 (loc, type, t);
14285
14286 if (sub)
14287 return sub;
14288
14289 return build1_loc (loc, INDIRECT_REF, type, t);
14290 }
14291
14292 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14293
14294 tree
14295 fold_indirect_ref_loc (location_t loc, tree t)
14296 {
14297 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14298
14299 if (sub)
14300 return sub;
14301 else
14302 return t;
14303 }
14304
14305 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14306 whose result is ignored. The type of the returned tree need not be
14307 the same as the original expression. */
14308
14309 tree
14310 fold_ignored_result (tree t)
14311 {
14312 if (!TREE_SIDE_EFFECTS (t))
14313 return integer_zero_node;
14314
14315 for (;;)
14316 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14317 {
14318 case tcc_unary:
14319 t = TREE_OPERAND (t, 0);
14320 break;
14321
14322 case tcc_binary:
14323 case tcc_comparison:
14324 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14325 t = TREE_OPERAND (t, 0);
14326 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14327 t = TREE_OPERAND (t, 1);
14328 else
14329 return t;
14330 break;
14331
14332 case tcc_expression:
14333 switch (TREE_CODE (t))
14334 {
14335 case COMPOUND_EXPR:
14336 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14337 return t;
14338 t = TREE_OPERAND (t, 0);
14339 break;
14340
14341 case COND_EXPR:
14342 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14343 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14344 return t;
14345 t = TREE_OPERAND (t, 0);
14346 break;
14347
14348 default:
14349 return t;
14350 }
14351 break;
14352
14353 default:
14354 return t;
14355 }
14356 }
14357
14358 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14359
14360 tree
14361 round_up_loc (location_t loc, tree value, unsigned int divisor)
14362 {
14363 tree div = NULL_TREE;
14364
14365 if (divisor == 1)
14366 return value;
14367
14368 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14369 have to do anything. Only do this when we are not given a const,
14370 because in that case, this check is more expensive than just
14371 doing it. */
14372 if (TREE_CODE (value) != INTEGER_CST)
14373 {
14374 div = build_int_cst (TREE_TYPE (value), divisor);
14375
14376 if (multiple_of_p (TREE_TYPE (value), value, div))
14377 return value;
14378 }
14379
14380 /* If divisor is a power of two, simplify this to bit manipulation. */
14381 if (pow2_or_zerop (divisor))
14382 {
14383 if (TREE_CODE (value) == INTEGER_CST)
14384 {
14385 wide_int val = wi::to_wide (value);
14386 bool overflow_p;
14387
14388 if ((val & (divisor - 1)) == 0)
14389 return value;
14390
14391 overflow_p = TREE_OVERFLOW (value);
14392 val += divisor - 1;
14393 val &= (int) -divisor;
14394 if (val == 0)
14395 overflow_p = true;
14396
14397 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14398 }
14399 else
14400 {
14401 tree t;
14402
14403 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14404 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14405 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14406 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14407 }
14408 }
14409 else
14410 {
14411 if (!div)
14412 div = build_int_cst (TREE_TYPE (value), divisor);
14413 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14414 value = size_binop_loc (loc, MULT_EXPR, value, div);
14415 }
14416
14417 return value;
14418 }
14419
14420 /* Likewise, but round down. */
14421
14422 tree
14423 round_down_loc (location_t loc, tree value, int divisor)
14424 {
14425 tree div = NULL_TREE;
14426
14427 gcc_assert (divisor > 0);
14428 if (divisor == 1)
14429 return value;
14430
14431 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14432 have to do anything. Only do this when we are not given a const,
14433 because in that case, this check is more expensive than just
14434 doing it. */
14435 if (TREE_CODE (value) != INTEGER_CST)
14436 {
14437 div = build_int_cst (TREE_TYPE (value), divisor);
14438
14439 if (multiple_of_p (TREE_TYPE (value), value, div))
14440 return value;
14441 }
14442
14443 /* If divisor is a power of two, simplify this to bit manipulation. */
14444 if (pow2_or_zerop (divisor))
14445 {
14446 tree t;
14447
14448 t = build_int_cst (TREE_TYPE (value), -divisor);
14449 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14450 }
14451 else
14452 {
14453 if (!div)
14454 div = build_int_cst (TREE_TYPE (value), divisor);
14455 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14456 value = size_binop_loc (loc, MULT_EXPR, value, div);
14457 }
14458
14459 return value;
14460 }
14461
14462 /* Returns the pointer to the base of the object addressed by EXP and
14463 extracts the information about the offset of the access, storing it
14464 to PBITPOS and POFFSET. */
14465
14466 static tree
14467 split_address_to_core_and_offset (tree exp,
14468 poly_int64_pod *pbitpos, tree *poffset)
14469 {
14470 tree core;
14471 machine_mode mode;
14472 int unsignedp, reversep, volatilep;
14473 poly_int64 bitsize;
14474 location_t loc = EXPR_LOCATION (exp);
14475
14476 if (TREE_CODE (exp) == ADDR_EXPR)
14477 {
14478 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14479 poffset, &mode, &unsignedp, &reversep,
14480 &volatilep);
14481 core = build_fold_addr_expr_loc (loc, core);
14482 }
14483 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14484 {
14485 core = TREE_OPERAND (exp, 0);
14486 STRIP_NOPS (core);
14487 *pbitpos = 0;
14488 *poffset = TREE_OPERAND (exp, 1);
14489 if (poly_int_tree_p (*poffset))
14490 {
14491 poly_offset_int tem
14492 = wi::sext (wi::to_poly_offset (*poffset),
14493 TYPE_PRECISION (TREE_TYPE (*poffset)));
14494 tem <<= LOG2_BITS_PER_UNIT;
14495 if (tem.to_shwi (pbitpos))
14496 *poffset = NULL_TREE;
14497 }
14498 }
14499 else
14500 {
14501 core = exp;
14502 *pbitpos = 0;
14503 *poffset = NULL_TREE;
14504 }
14505
14506 return core;
14507 }
14508
14509 /* Returns true if addresses of E1 and E2 differ by a constant, false
14510 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14511
14512 bool
14513 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14514 {
14515 tree core1, core2;
14516 poly_int64 bitpos1, bitpos2;
14517 tree toffset1, toffset2, tdiff, type;
14518
14519 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14520 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14521
14522 poly_int64 bytepos1, bytepos2;
14523 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14524 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14525 || !operand_equal_p (core1, core2, 0))
14526 return false;
14527
14528 if (toffset1 && toffset2)
14529 {
14530 type = TREE_TYPE (toffset1);
14531 if (type != TREE_TYPE (toffset2))
14532 toffset2 = fold_convert (type, toffset2);
14533
14534 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14535 if (!cst_and_fits_in_hwi (tdiff))
14536 return false;
14537
14538 *diff = int_cst_value (tdiff);
14539 }
14540 else if (toffset1 || toffset2)
14541 {
14542 /* If only one of the offsets is non-constant, the difference cannot
14543 be a constant. */
14544 return false;
14545 }
14546 else
14547 *diff = 0;
14548
14549 *diff += bytepos1 - bytepos2;
14550 return true;
14551 }
14552
14553 /* Return OFF converted to a pointer offset type suitable as offset for
14554 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14555 tree
14556 convert_to_ptrofftype_loc (location_t loc, tree off)
14557 {
14558 return fold_convert_loc (loc, sizetype, off);
14559 }
14560
14561 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14562 tree
14563 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14564 {
14565 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14566 ptr, convert_to_ptrofftype_loc (loc, off));
14567 }
14568
14569 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14570 tree
14571 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14572 {
14573 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14574 ptr, size_int (off));
14575 }
14576
14577 /* Return a pointer P to a NUL-terminated string representing the sequence
14578 of constant characters referred to by SRC (or a subsequence of such
14579 characters within it if SRC is a reference to a string plus some
14580 constant offset). If STRLEN is non-null, store the number of bytes
14581 in the string constant including the terminating NUL char. *STRLEN is
14582 typically strlen(P) + 1 in the absence of embedded NUL characters. */
14583
14584 const char *
14585 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen /* = NULL */)
14586 {
14587 tree offset_node;
14588 tree mem_size;
14589
14590 if (strlen)
14591 *strlen = 0;
14592
14593 src = string_constant (src, &offset_node, &mem_size, NULL);
14594 if (src == 0)
14595 return NULL;
14596
14597 unsigned HOST_WIDE_INT offset = 0;
14598 if (offset_node != NULL_TREE)
14599 {
14600 if (!tree_fits_uhwi_p (offset_node))
14601 return NULL;
14602 else
14603 offset = tree_to_uhwi (offset_node);
14604 }
14605
14606 if (!tree_fits_uhwi_p (mem_size))
14607 return NULL;
14608
14609 /* STRING_LENGTH is the size of the string literal, including any
14610 embedded NULs. STRING_SIZE is the size of the array the string
14611 literal is stored in. */
14612 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14613 unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size);
14614
14615 /* Ideally this would turn into a gcc_checking_assert over time. */
14616 if (string_length > string_size)
14617 string_length = string_size;
14618
14619 const char *string = TREE_STRING_POINTER (src);
14620
14621 /* Ideally this would turn into a gcc_checking_assert over time. */
14622 if (string_length > string_size)
14623 string_length = string_size;
14624
14625 if (string_length == 0
14626 || offset >= string_size)
14627 return NULL;
14628
14629 if (strlen)
14630 {
14631 /* Compute and store the length of the substring at OFFSET.
14632 All offsets past the initial length refer to null strings. */
14633 if (offset < string_length)
14634 *strlen = string_length - offset;
14635 else
14636 *strlen = 1;
14637 }
14638 else
14639 {
14640 tree eltype = TREE_TYPE (TREE_TYPE (src));
14641 /* Support only properly NUL-terminated single byte strings. */
14642 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
14643 return NULL;
14644 if (string[string_length - 1] != '\0')
14645 return NULL;
14646 }
14647
14648 return offset < string_length ? string + offset : "";
14649 }
14650
14651 /* Given a tree T, compute which bits in T may be nonzero. */
14652
14653 wide_int
14654 tree_nonzero_bits (const_tree t)
14655 {
14656 switch (TREE_CODE (t))
14657 {
14658 case INTEGER_CST:
14659 return wi::to_wide (t);
14660 case SSA_NAME:
14661 return get_nonzero_bits (t);
14662 case NON_LVALUE_EXPR:
14663 case SAVE_EXPR:
14664 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14665 case BIT_AND_EXPR:
14666 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14667 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14668 case BIT_IOR_EXPR:
14669 case BIT_XOR_EXPR:
14670 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14671 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14672 case COND_EXPR:
14673 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14674 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14675 CASE_CONVERT:
14676 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14677 TYPE_PRECISION (TREE_TYPE (t)),
14678 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14679 case PLUS_EXPR:
14680 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14681 {
14682 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14683 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14684 if (wi::bit_and (nzbits1, nzbits2) == 0)
14685 return wi::bit_or (nzbits1, nzbits2);
14686 }
14687 break;
14688 case LSHIFT_EXPR:
14689 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14690 {
14691 tree type = TREE_TYPE (t);
14692 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14693 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14694 TYPE_PRECISION (type));
14695 return wi::neg_p (arg1)
14696 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14697 : wi::lshift (nzbits, arg1);
14698 }
14699 break;
14700 case RSHIFT_EXPR:
14701 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14702 {
14703 tree type = TREE_TYPE (t);
14704 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14705 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14706 TYPE_PRECISION (type));
14707 return wi::neg_p (arg1)
14708 ? wi::lshift (nzbits, -arg1)
14709 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14710 }
14711 break;
14712 default:
14713 break;
14714 }
14715
14716 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14717 }
14718
14719 #if CHECKING_P
14720
14721 namespace selftest {
14722
14723 /* Helper functions for writing tests of folding trees. */
14724
14725 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14726
14727 static void
14728 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14729 tree constant)
14730 {
14731 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14732 }
14733
14734 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14735 wrapping WRAPPED_EXPR. */
14736
14737 static void
14738 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14739 tree wrapped_expr)
14740 {
14741 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14742 ASSERT_NE (wrapped_expr, result);
14743 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14744 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14745 }
14746
14747 /* Verify that various arithmetic binary operations are folded
14748 correctly. */
14749
14750 static void
14751 test_arithmetic_folding ()
14752 {
14753 tree type = integer_type_node;
14754 tree x = create_tmp_var_raw (type, "x");
14755 tree zero = build_zero_cst (type);
14756 tree one = build_int_cst (type, 1);
14757
14758 /* Addition. */
14759 /* 1 <-- (0 + 1) */
14760 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14761 one);
14762 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14763 one);
14764
14765 /* (nonlvalue)x <-- (x + 0) */
14766 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14767 x);
14768
14769 /* Subtraction. */
14770 /* 0 <-- (x - x) */
14771 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14772 zero);
14773 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14774 x);
14775
14776 /* Multiplication. */
14777 /* 0 <-- (x * 0) */
14778 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14779 zero);
14780
14781 /* (nonlvalue)x <-- (x * 1) */
14782 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14783 x);
14784 }
14785
14786 /* Verify that various binary operations on vectors are folded
14787 correctly. */
14788
14789 static void
14790 test_vector_folding ()
14791 {
14792 tree inner_type = integer_type_node;
14793 tree type = build_vector_type (inner_type, 4);
14794 tree zero = build_zero_cst (type);
14795 tree one = build_one_cst (type);
14796
14797 /* Verify equality tests that return a scalar boolean result. */
14798 tree res_type = boolean_type_node;
14799 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14800 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14801 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14802 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14803 }
14804
14805 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14806
14807 static void
14808 test_vec_duplicate_folding ()
14809 {
14810 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14811 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14812 /* This will be 1 if VEC_MODE isn't a vector mode. */
14813 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14814
14815 tree type = build_vector_type (ssizetype, nunits);
14816 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14817 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14818 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14819 }
14820
14821 /* Run all of the selftests within this file. */
14822
14823 void
14824 fold_const_c_tests ()
14825 {
14826 test_arithmetic_folding ();
14827 test_vector_folding ();
14828 test_vec_duplicate_folding ();
14829 }
14830
14831 } // namespace selftest
14832
14833 #endif /* CHECKING_P */